54 #define CHANNEL_OBJECT_PRIVATE
57 #define CHANNEL_FILE_PRIVATE
63 #include "core/or/channelpadding.h"
70 #include "core/or/dos.h"
122 channel_id_hash, channel_id_eq);
123 HT_GENERATE2(channel_gid_map,
channel_t, gidmap_node,
124 channel_id_hash, channel_id_eq,
130 static uint64_t n_channels_allocated = 0;
140 static
HT_HEAD(channel_idmap, channel_idmap_entry_t) channel_identity_map =
143 typedef struct channel_idmap_entry_t {
144 HT_ENTRY(channel_idmap_entry_t) node;
146 TOR_LIST_HEAD(channel_list_t,
channel_t) channel_list;
147 } channel_idmap_entry_t;
149 static inline unsigned
150 channel_idmap_hash(
const channel_idmap_entry_t *ent)
152 return (
unsigned) siphash24g(ent->digest,
DIGEST_LEN);
156 channel_idmap_eq(
const channel_idmap_entry_t *a,
157 const channel_idmap_entry_t *b)
162 HT_PROTOTYPE(channel_idmap, channel_idmap_entry_t, node, channel_idmap_hash,
164 HT_GENERATE2(channel_idmap, channel_idmap_entry_t, node, channel_idmap_hash,
327 descr =
"channel error";
330 descr =
"temporarily suspended for maintenance";
340 descr =
"unknown or invalid channel state";
362 descr =
"channel listener error";
369 descr =
"unknown or invalid channel listener state";
395 "Registering channel %p (ID %"PRIu64
") "
396 "in state %s (%d) with digest %s",
404 channel_t *oldval = HT_REPLACE(channel_gid_map, &channel_gid_map, chan);
408 if (CHANNEL_FINISHED(chan)) {
418 if (!CHANNEL_IS_CLOSING(chan)) {
425 "Channel %p (global ID %"PRIu64
") "
426 "in state %s (%d) registered with no identity digest",
452 if (CHANNEL_FINISHED(chan)) {
462 channel_t *oldval = HT_REMOVE(channel_gid_map, &channel_gid_map, chan);
470 !(CHANNEL_CONDEMNED(chan))) {
491 "Registering channel listener %p (ID %"PRIu64
") "
505 if (!finished_listeners) finished_listeners =
smartlist_new();
562 channel_idmap_entry_t *ent, search;
573 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
575 ent = tor_malloc(
sizeof(channel_idmap_entry_t));
577 TOR_LIST_INIT(&ent->channel_list);
578 HT_INSERT(channel_idmap, &channel_identity_map, ent);
580 TOR_LIST_INSERT_HEAD(&ent->channel_list, chan, next_with_same_id);
583 "Added channel %p (global ID %"PRIu64
") "
584 "to identity map in state %s (%d) with digest %s",
599 channel_idmap_entry_t *ent, search;
607 TOR_LIST_REMOVE(chan, next_with_same_id);
610 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
616 if (TOR_LIST_EMPTY(&ent->channel_list)) {
617 HT_REMOVE(channel_idmap, &channel_identity_map, ent);
622 "Removed channel %p (global ID %"PRIu64
") from "
623 "identity map in state %s (%d) with digest %s",
630 "Trying to remove channel %p (global ID %"PRIu64
") with "
631 "digest %s from identity map, but couldn't find any with "
656 rv = HT_FIND(channel_gid_map, &channel_gid_map, &lookup);
668 const char *rsa_id_digest,
701 channel_idmap_entry_t *ent, search;
712 memcpy(search.digest, rsa_id_digest,
DIGEST_LEN);
713 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
715 rv = TOR_LIST_FIRST(&ent->channel_list);
735 return TOR_LIST_NEXT(chan, next_with_same_id);
750 channel_idmap_entry_t **iter;
752 int total_dirauth_connections = 0, total_dirauths = 0;
753 int total_relay_connections = 0, total_relays = 0, total_canonical = 0;
754 int total_half_canonical = 0;
755 int total_gt_one_connection = 0, total_gt_two_connections = 0;
756 int total_gt_four_connections = 0;
758 HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
759 int connections_to_relay = 0;
760 const char *id_digest = (
char *) (*iter)->digest;
768 const bool is_dirauth = router_digest_is_trusted_dir(id_digest);
772 for (chan = TOR_LIST_FIRST(&(*iter)->channel_list); chan;
775 if (CHANNEL_CONDEMNED(chan) || !CHANNEL_IS_OPEN(chan))
778 connections_to_relay++;
779 total_relay_connections++;
781 total_dirauth_connections++;
786 total_half_canonical++;
790 if (connections_to_relay > 1) total_gt_one_connection++;
791 if (connections_to_relay > 2) total_gt_two_connections++;
792 if (connections_to_relay > 4) total_gt_four_connections++;
798 #define MIN_RELAY_CONNECTIONS_TO_WARN 25
802 #define MAX_AVG_RELAY_CONNECTIONS 1.5
806 #define MAX_AVG_DIRAUTH_CONNECTIONS 4
810 const int max_tolerable_connections = (int)(
811 (total_relays-total_dirauths) * MAX_AVG_RELAY_CONNECTIONS +
812 total_dirauths * MAX_AVG_DIRAUTH_CONNECTIONS);
815 if (total_relays > MIN_RELAY_CONNECTIONS_TO_WARN &&
816 total_relay_connections > max_tolerable_connections) {
818 "Your relay has a very large number of connections to other relays. "
819 "Is your outbound address the same as your relay address? "
820 "Found %d connections to %d relays. Found %d current canonical "
821 "connections, in %d of which we were a non-canonical peer. "
822 "%d relays had more than 1 connection, %d had more than 2, and "
823 "%d had more than 4 connections.",
824 total_relay_connections, total_relays, total_canonical,
825 total_half_canonical, total_gt_one_connection,
826 total_gt_two_connections, total_gt_four_connections);
828 log_info(
LD_OR,
"Performed connection pruning. "
829 "Found %d connections to %d relays. Found %d current canonical "
830 "connections, in %d of which we were a non-canonical peer. "
831 "%d relays had more than 1 connection, %d had more than 2, and "
832 "%d had more than 4 connections.",
833 total_relay_connections, total_relays, total_canonical,
834 total_half_canonical, total_gt_one_connection,
835 total_gt_two_connections, total_gt_four_connections);
861 memset(&chan->next_with_same_id, 0,
sizeof(chan->next_with_same_id));
913 "Freeing channel %"PRIu64
" at %p",
917 scheduler_release_channel(chan);
930 channel_handles_clear(chan);
941 circuitmux_free(chan->
cmux);
959 "Freeing channel_listener_t %"PRIu64
" at %p",
986 "Force-freeing channel %"PRIu64
" at %p",
990 scheduler_release_channel(chan);
1003 channel_handles_clear(chan);
1012 circuitmux_free(chan->
cmux);
1030 "Force-freeing channel_listener_t %"PRIu64
" at %p",
1046 } SMARTLIST_FOREACH_END(qchan);
1063 channel_listener_fn_ptr listener)
1069 "Setting listener callback for channel listener %p "
1070 "(global ID %"PRIu64
") to %p",
1084 channel_cell_handler_fn_ptr
1089 if (CHANNEL_CAN_HANDLE_CELLS(chan))
1103 channel_cell_handler_fn_ptr cell_handler)
1109 "Setting cell_handler callback for channel %p to %p",
1110 chan, cell_handler);
1143 if (CHANNEL_CONDEMNED(chan))
1147 "Closing channel %p (global ID %"PRIu64
") "
1187 "Closing channel listener %p (global ID %"PRIu64
") "
1198 chan_l->
close(chan_l);
1221 if (CHANNEL_CONDEMNED(chan))
1225 "Closing channel %p (global ID %"PRIu64
") "
1226 "due to lower-layer event",
1249 if (CHANNEL_CONDEMNED(chan))
1253 "Closing channel %p due to lower-layer error",
1277 if (CHANNEL_FINISHED(chan))
1304 int state_not_in_map;
1309 "Clearing remote endpoint digest on channel %p with "
1310 "global ID %"PRIu64,
1313 state_not_in_map = CHANNEL_CONDEMNED(chan);
1332 const char *identity_digest,
1335 int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
1340 "Setting remote endpoint digest on channel %p with "
1341 "global ID %"PRIu64
" to digest %s",
1346 state_not_in_map = CHANNEL_CONDEMNED(chan);
1349 !state_not_in_map &&
1352 should_be_in_digest_map =
1353 !state_not_in_map &&
1358 if (was_in_digest_map)
1364 if (identity_digest) {
1379 if (should_be_in_digest_map)
1392 int state_not_in_map;
1397 "Clearing remote endpoint identity on channel %p with "
1398 "global ID %"PRIu64,
1401 state_not_in_map = CHANNEL_CONDEMNED(chan);
1440 cell_bytes = get_cell_network_size(chan->wide_circ_ids);
1443 if (!CHANNEL_IS_OPEN(chan)) {
1455 chan->n_bytes_xmitted += cell_bytes;
1491 if (CHANNEL_IS_CLOSING(chan)) {
1492 log_debug(
LD_CHANNEL,
"Discarding %p on closing channel %p with "
1493 "global ID %"PRIu64, cell, chan,
1498 "Writing %p to channel %p with global ID "
1507 packed_cell_free(cell);
1522 unsigned char was_active, is_active;
1523 unsigned char was_in_id_map, is_in_id_map;
1526 from_state = chan->
state;
1533 if (from_state == to_state) {
1535 "Got no-op transition from \"%s\" to itself on channel %p"
1536 "(global ID %"PRIu64
")",
1550 "Changing state of channel %p (global ID %"PRIu64
1551 ") from \"%s\" to \"%s\"",
1557 chan->
state = to_state;
1567 if (was_active && !is_active) {
1569 if (!finished_channels) finished_channels =
smartlist_new();
1574 else if (!was_active && is_active) {
1590 else if (was_in_id_map && !is_in_id_map)
1602 scheduler_release_channel(chan);
1643 unsigned char was_active, is_active;
1646 from_state = chan_l->
state;
1653 if (from_state == to_state) {
1655 "Got no-op transition from \"%s\" to itself on channel "
1656 "listener %p (global ID %"PRIu64
")",
1670 "Changing state of channel listener %p (global ID %"PRIu64
1671 "from \"%s\" to \"%s\"",
1676 chan_l->
state = to_state;
1686 if (was_active && !is_active) {
1688 if (!finished_listeners) finished_listeners =
smartlist_new();
1693 else if (!was_active && is_active) {
1709 #define MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED 256
1732 unsigned int unlimited = 0;
1733 ssize_t flushed = 0;
1734 int clamped_num_cells;
1738 if (num_cells < 0) unlimited = 1;
1739 if (!unlimited && num_cells <= flushed)
goto done;
1742 if (CHANNEL_IS_OPEN(chan)) {
1746 clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
1748 if (num_cells - flushed >
1749 MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED) {
1750 clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
1752 clamped_num_cells = (int)(num_cells - flushed);
1758 chan, clamped_num_cells);
1820 "Processing queue of incoming connections for channel "
1821 "listener %p (global ID %"PRIu64
")",
1831 "Handling incoming channel %p (%"PRIu64
") "
1832 "for listener %p (%"PRIu64
")",
1839 listener->
listener(listener, chan);
1840 } SMARTLIST_FOREACH_END(chan);
1863 time_t now = time(NULL);
1864 int close_origin_circuits = 0;
1877 char *transport_name = NULL;
1878 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
1879 if (chan->get_transport_name(chan, &transport_name) < 0)
1880 transport_name = NULL;
1883 &remote_addr, transport_name,
1887 if (tlschan && tlschan->conn) {
1888 dos_new_client_conn(tlschan->conn, transport_name);
1900 }
else if (rend_service_allow_non_anonymous_connection(
get_options()) &&
1902 CHANNELPADDING_SOS_PARAM,
1903 CHANNELPADDING_SOS_DEFAULT, 0, 1)) {
1907 }
else if (
get_options()->ReducedConnectionPadding) {
1928 int need_to_queue = 0;
1935 "Queueing incoming channel %p (global ID %"PRIu64
") on "
1936 "channel listener %p (global ID %"PRIu64
")",
1941 if (!(listener->
listener)) need_to_queue = 1;
1957 if (!need_to_queue) {
1959 listener->
listener(listener, incoming);
1979 tor_assert(CHANNEL_IS_CLOSING(chan) || CHANNEL_IS_MAINT(chan) ||
1980 CHANNEL_IS_OPEN(chan));
1991 chan->n_bytes_recved += get_cell_network_size(chan->wide_circ_ids);
1994 "Processing incoming cell_t %p for channel %p (global ID "
1995 "%"PRIu64
")", cell, chan,
2009 if (chan->wide_circ_ids) {
2010 if (packed_cell->
body[4] == CELL_DESTROY) {
2015 if (packed_cell->
body[2] == CELL_DESTROY) {
2035 log_warn(
LD_BUG,
"Attempted to send a destroy cell for circID 0 "
2036 "on a channel %"PRIu64
" at %p in state %s (%d)",
2044 if (!CHANNEL_CONDEMNED(chan) && chan->
cmux) {
2046 circuitmux_append_destroy_cell(chan, chan->
cmux, circ_id, reason);
2048 "Sending destroy (circID %u) on channel %p "
2049 "(global ID %"PRIu64
")",
2050 (
unsigned)circ_id, chan,
2054 "Someone called channel_send_destroy() for circID %u "
2055 "on a channel %"PRIu64
" at %p in state %s (%d)",
2073 if (all_channels && smartlist_len(all_channels) > 0) {
2075 "Dumping statistics about %d channels:",
2076 smartlist_len(all_channels));
2078 "%d are active, and %d are done and waiting for cleanup",
2079 (active_channels != NULL) ?
2080 smartlist_len(active_channels) : 0,
2081 (finished_channels != NULL) ?
2082 smartlist_len(finished_channels) : 0);
2088 "Done spamming about channels now");
2091 "No channels to dump");
2104 if (all_listeners && smartlist_len(all_listeners) > 0) {
2106 "Dumping statistics about %d channel listeners:",
2107 smartlist_len(all_listeners));
2109 "%d are active and %d are done and waiting for cleanup",
2110 (active_listeners != NULL) ?
2111 smartlist_len(active_listeners) : 0,
2112 (finished_listeners != NULL) ?
2113 smartlist_len(finished_listeners) : 0);
2119 "Done spamming about channel listeners now");
2122 "No channel listeners to dump");
2138 if (!finished_channels || smartlist_len(finished_channels) == 0)
return;
2149 } SMARTLIST_FOREACH_END(curr);
2164 if (!finished_listeners || smartlist_len(finished_listeners) == 0)
return;
2174 channel_listener_free(tmp);
2175 } SMARTLIST_FOREACH_END(curr);
2184 if (!channels)
return;
2190 "Cleaning up channel %p (global ID %"PRIu64
") "
2192 curr, (curr->global_identifier),
2200 if (mark_for_close) {
2201 if (!CHANNEL_CONDEMNED(curr)) {
2205 }
else channel_free(curr);
2206 } SMARTLIST_FOREACH_END(curr);
2215 if (!listeners)
return;
2221 "Cleaning up channel listener %p (global ID %"PRIu64
") "
2223 curr, (curr->global_identifier),
2226 if (mark_for_close) {
2233 }
else channel_listener_free(curr);
2234 } SMARTLIST_FOREACH_END(curr);
2249 "Shutting down channels...");
2252 if (finished_channels) {
2254 smartlist_free(finished_channels);
2255 finished_channels = NULL;
2259 if (finished_listeners) {
2261 smartlist_free(finished_listeners);
2262 finished_listeners = NULL;
2266 if (active_channels) {
2268 smartlist_free(active_channels);
2269 active_channels = NULL;
2273 if (active_listeners) {
2275 smartlist_free(active_listeners);
2276 active_listeners = NULL;
2282 smartlist_free(all_channels);
2283 all_channels = NULL;
2287 if (all_listeners) {
2289 smartlist_free(all_listeners);
2290 all_listeners = NULL;
2295 "Freeing channel_identity_map");
2297 HT_CLEAR(channel_idmap, &channel_identity_map);
2301 "Freeing channel_gid_map");
2302 HT_CLEAR(channel_gid_map, &channel_gid_map);
2305 "Done cleaning up after channels");
2320 const char *id_digest,
2339 int a_is_canonical, b_is_canonical;
2355 if (a_is_canonical && !b_is_canonical)
return 1;
2356 if (!a_is_canonical && b_is_canonical)
return 0;
2407 bool for_origin_circ,
2408 const char **msg_out,
2412 int n_inprogress_goodaddr = 0, n_old = 0;
2413 int n_noncanonical = 0;
2425 if (CHANNEL_CONDEMNED(chan))
2439 const bool matches_target =
2444 if (!CHANNEL_IS_OPEN(chan)) {
2447 if (matches_target) {
2448 ++n_inprogress_goodaddr;
2449 if (for_origin_circ) {
2482 *msg_out =
"Connection is fine; using it.";
2485 }
else if (n_inprogress_goodaddr) {
2486 *msg_out =
"Connection in progress; waiting.";
2489 }
else if (n_old || n_noncanonical) {
2490 *msg_out =
"Connections all too old, or too non-canonical. "
2491 " Launching a new one.";
2495 *msg_out =
"Not connected. Connecting.";
2539 double avg, interval, age;
2540 time_t now = time(NULL);
2542 int have_remote_addr;
2543 char *remote_addr_str;
2550 "Channel %"PRIu64
" (at %p) with transport %s is in state "
2556 " * Channel %"PRIu64
" was created at %"PRIu64
2557 " (%"PRIu64
" seconds ago) "
2558 "and last active at %"PRIu64
" (%"PRIu64
" seconds ago)",
2562 (uint64_t)(chan->timestamp_active),
2563 (uint64_t)(now - chan->timestamp_active));
2568 " * Channel %"PRIu64
" says it is connected "
2569 "to an OR with digest %s",
2574 " * Channel %"PRIu64
" does not know the digest"
2575 " of the OR it is connected to",
2581 if (have_remote_addr) {
2585 " * Channel %"PRIu64
" says its remote address"
2586 " is %s, and gives a canonical description of \"%s\" and an "
2587 "actual description of \"%s\"",
2589 safe_str(remote_addr_str),
2597 " * Channel %"PRIu64
" does not know its remote "
2598 "address, but gives a canonical description of \"%s\" and an "
2599 "actual description of \"%s\"",
2608 " * Channel %"PRIu64
" has these marks: %s %s %s %s %s",
2611 "bad_for_new_circs" :
"!bad_for_new_circs",
2613 "canonical" :
"!canonical",
2615 "client" :
"!client",
2619 "incoming" :
"outgoing");
2623 " * Channel %"PRIu64
" has %d active circuits out of"
2626 (chan->
cmux != NULL) ?
2628 (chan->
cmux != NULL) ?
2633 " * Channel %"PRIu64
" was last used by a "
2634 "client at %"PRIu64
" (%"PRIu64
" seconds ago)",
2639 " * Channel %"PRIu64
" last received a cell "
2640 "at %"PRIu64
" (%"PRIu64
" seconds ago)",
2645 " * Channel %"PRIu64
" last transmitted a cell "
2646 "at %"PRIu64
" (%"PRIu64
" seconds ago)",
2653 " * Channel %"PRIu64
" has received "
2654 "%"PRIu64
" bytes in %"PRIu64
" cells and transmitted "
2655 "%"PRIu64
" bytes in %"PRIu64
" cells",
2657 (chan->n_bytes_recved),
2659 (chan->n_bytes_xmitted),
2663 if (chan->n_bytes_recved > 0) {
2664 avg = (double)(chan->n_bytes_recved) / age;
2666 " * Channel %"PRIu64
" has averaged %f "
2667 "bytes received per second",
2674 " * Channel %"PRIu64
" has averaged %f "
2675 "cells received per second",
2677 }
else if (avg >= 0.0) {
2678 interval = 1.0 / avg;
2680 " * Channel %"PRIu64
" has averaged %f "
2681 "seconds between received cells",
2685 if (chan->n_bytes_xmitted > 0) {
2686 avg = (double)(chan->n_bytes_xmitted) / age;
2688 " * Channel %"PRIu64
" has averaged %f "
2689 "bytes transmitted per second",
2696 " * Channel %"PRIu64
" has averaged %f "
2697 "cells transmitted per second",
2699 }
else if (avg >= 0.0) {
2700 interval = 1.0 / avg;
2702 " * Channel %"PRIu64
" has averaged %f "
2703 "seconds between transmitted cells",
2721 double avg, interval, age;
2722 time_t now = time(NULL);
2729 "Channel listener %"PRIu64
" (at %p) with transport %s is in "
2735 " * Channel listener %"PRIu64
" was created at %"PRIu64
2736 " (%"PRIu64
" seconds ago) "
2737 "and last active at %"PRIu64
" (%"PRIu64
" seconds ago)",
2741 (uint64_t)(chan_l->timestamp_active),
2742 (uint64_t)(now - chan_l->timestamp_active));
2745 " * Channel listener %"PRIu64
" last accepted an incoming "
2746 "channel at %"PRIu64
" (%"PRIu64
" seconds ago) "
2747 "and has accepted %"PRIu64
" channels in total",
2763 " * Channel listener %"PRIu64
" has averaged %f incoming "
2764 "channels per second",
2766 }
else if (avg >= 0.0) {
2767 interval = 1.0 / avg;
2769 " * Channel listener %"PRIu64
" has averaged %f seconds "
2770 "between incoming channels",
2841 return chan->get_remote_addr(chan, addr_out);
3065 result = chan->num_cells_writeable(chan);
3066 if (result < 0) result = 0;
3088 time_t now = time(NULL);
3104 time_t now = time(NULL);
3124 time_t now = time(NULL);
3129 chan->timestamp_active = now;
3141 time_t now = time(NULL);
3145 chan_l->timestamp_active = now;
3158 time_t now = time(NULL);
3162 chan_l->timestamp_active = now;
3175 time_t now = time(NULL);
3191 time_t now = time(NULL);
3195 chan->timestamp_active = now;
3211 time_t now = time(NULL);
3216 chan->timestamp_active = now;
3297 IF_BUG_ONCE(!target_ipv4_addr && !target_ipv6_addr)
3300 if (target_ipv4_addr && chan->
matches_target(chan, target_ipv4_addr))
3303 if (target_ipv6_addr && chan->
matches_target(chan, target_ipv6_addr))
3321 chan->num_p_circuits;
3333 int consider_identity))
3342 if (! consider_identity) {
3350 our_identity = started_here ?
3353 if (identity_rcvd) {
3365 channel_sort_by_ed25519_identity(
const void **a_,
const void **b_)
3370 &b->ed25519_identity.pubkey,
3388 if (PREDICT_LIKELY(!TOR_LIST_NEXT(chan, next_with_same_id))) {
3390 time(NULL), BASE_CHAN_TO_TLS(chan)->conn, force);
3396 TOR_LIST_FOREACH(chan, lst, next_with_same_id) {
3397 if (BASE_CHAN_TO_TLS(chan)->conn) {
3410 if (!common_ed25519_identity)
3411 common_ed25519_identity = &channel->ed25519_identity;
3414 common_ed25519_identity)) {
3417 common_ed25519_identity = &channel->ed25519_identity;
3421 } SMARTLIST_FOREACH_END(channel);
3428 smartlist_free(or_conns);
3429 smartlist_free(channels);
3441 channel_idmap_entry_t *ent;
3442 channel_idmap_entry_t search;
3443 memset(&search, 0,
sizeof(search));
3445 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
3453 channel_idmap_entry_t **iter;
3454 HT_FOREACH(iter, channel_idmap, &channel_identity_map) {