Tor 0.4.9.13
Loading...
Searching...
No Matches
channel.c
Go to the documentation of this file.
1/* * Copyright (c) 2012-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file channel.c
6 *
7 * \brief OR/OP-to-OR channel abstraction layer. A channel's job is to
8 * transfer cells from Tor instance to Tor instance. Currently, there is only
9 * one implementation of the channel abstraction: in channeltls.c.
10 *
11 * Channels are a higher-level abstraction than or_connection_t: In general,
12 * any means that two Tor relays use to exchange cells, or any means that a
13 * relay and a client use to exchange cells, is a channel.
14 *
15 * Channels differ from pluggable transports in that they do not wrap an
16 * underlying protocol over which cells are transmitted: they <em>are</em> the
17 * underlying protocol.
18 *
19 * This module defines the generic parts of the channel_t interface, and
20 * provides the machinery necessary for specialized implementations to be
21 * created. At present, there is one specialized implementation in
22 * channeltls.c, which uses connection_or.c to send cells over a TLS
23 * connection.
24 *
25 * Every channel implementation is responsible for being able to transmit
26 * cells that are passed to it
27 *
28 * For *inbound* cells, the entry point is: channel_process_cell(). It takes a
29 * cell and will pass it to the cell handler set by
30 * channel_set_cell_handlers(). Currently, this is passed back to the command
31 * subsystem which is command_process_cell().
32 *
33 * NOTE: For now, the separation between channels and specialized channels
34 * (like channeltls) is not that well defined. So the channeltls layer calls
35 * channel_process_cell() which originally comes from the connection subsystem.
36 * This should be hopefully be fixed with #23993.
37 *
38 * For *outbound* cells, the entry point is: channel_write_packed_cell().
39 * Only packed cells are dequeued from the circuit queue by the scheduler
40 * which uses channel_flush_from_first_active_circuit() to decide which cells
41 * to flush from which circuit on the channel. They are then passed down to
42 * the channel subsystem. This calls the low layer with the function pointer
43 * .write_packed_cell().
44 *
45 * Each specialized channel (currently only channeltls_t) MUST implement a
46 * series of function found in channel_t. See channel.h for more
47 * documentation.
48 **/
49
50/*
51 * Define this so channel.h gives us things only channel_t subclasses
52 * should touch.
53 */
54#define CHANNEL_OBJECT_PRIVATE
55
56/* This one's for stuff only channel.c and the test suite should see */
57#define CHANNEL_FILE_PRIVATE
58
59#include "core/or/or.h"
60#include "app/config/config.h"
62#include "core/or/channel.h"
63#include "core/or/channelpadding.h"
64#include "core/or/channeltls.h"
66#include "core/or/circuitlist.h"
67#include "core/or/circuitmux.h"
69#include "core/or/connection_or.h" /* For var_cell_free() */
70#include "core/or/dos.h"
71#include "core/or/relay.h"
72#include "core/or/scheduler.h"
83#include "lib/evloop/timers.h"
85
88
89/* Global lists of channels */
90
91/* All channel_t instances */
92static smartlist_t *all_channels = NULL;
93
94/* All channel_t instances not in ERROR or CLOSED states */
95static smartlist_t *active_channels = NULL;
96
97/* All channel_t instances in ERROR or CLOSED states */
98static smartlist_t *finished_channels = NULL;
99
100/* All channel_listener_t instances */
101static smartlist_t *all_listeners = NULL;
102
103/* All channel_listener_t instances in LISTENING state */
104static smartlist_t *active_listeners = NULL;
105
106/* All channel_listener_t instances in LISTENING state */
107static smartlist_t *finished_listeners = NULL;
108
109/** Map from channel->global_identifier to channel. Contains the same
110 * elements as all_channels. */
111static HT_HEAD(channel_gid_map, channel_t) channel_gid_map = HT_INITIALIZER();
112
113static unsigned
114channel_id_hash(const channel_t *chan)
115{
116 return (unsigned) chan->global_identifier;
117}
118static int
119channel_id_eq(const channel_t *a, const channel_t *b)
120{
121 return a->global_identifier == b->global_identifier;
122}
123HT_PROTOTYPE(channel_gid_map, channel_t, gidmap_node,
124 channel_id_hash, channel_id_eq);
125HT_GENERATE2(channel_gid_map, channel_t, gidmap_node,
126 channel_id_hash, channel_id_eq,
128
129HANDLE_IMPL(channel, channel_t,)
130
131/* Counter for ID numbers */
132static uint64_t n_channels_allocated = 0;
133
134/* Digest->channel map
135 *
136 * Similar to the one used in connection_or.c, this maps from the identity
137 * digest of a remote endpoint to a channel_t to that endpoint. Channels
138 * should be placed here when registered and removed when they close or error.
139 * If more than one channel exists, follow the next_with_same_id pointer
140 * as a linked list.
141 */
142static HT_HEAD(channel_idmap, channel_idmap_entry_t) channel_identity_map =
143 HT_INITIALIZER();
144
145typedef struct channel_idmap_entry_t {
146 HT_ENTRY(channel_idmap_entry_t) node;
147 uint8_t digest[DIGEST_LEN];
148 TOR_LIST_HEAD(channel_list_t, channel_t) channel_list;
149} channel_idmap_entry_t;
150
151static inline unsigned
152channel_idmap_hash(const channel_idmap_entry_t *ent)
153{
154 return (unsigned) siphash24g(ent->digest, DIGEST_LEN);
155}
156
157static inline int
158channel_idmap_eq(const channel_idmap_entry_t *a,
159 const channel_idmap_entry_t *b)
160{
161 return tor_memeq(a->digest, b->digest, DIGEST_LEN);
162}
163
164HT_PROTOTYPE(channel_idmap, channel_idmap_entry_t, node, channel_idmap_hash,
165 channel_idmap_eq);
166HT_GENERATE2(channel_idmap, channel_idmap_entry_t, node, channel_idmap_hash,
167 channel_idmap_eq, 0.5, tor_reallocarray_, tor_free_);
168
169/* Functions to maintain the digest map */
171
172static void channel_force_xfree(channel_t *chan);
173static void channel_free_list(smartlist_t *channels,
174 int mark_for_close);
175static void channel_listener_free_list(smartlist_t *channels,
176 int mark_for_close);
178
179/***********************************
180 * Channel state utility functions *
181 **********************************/
182
183/**
184 * Indicate whether a given channel state is valid.
185 */
186int
188{
189 int is_valid;
190
191 switch (state) {
198 is_valid = 1;
199 break;
201 default:
202 is_valid = 0;
203 }
204
205 return is_valid;
206}
207
208/**
209 * Indicate whether a given channel listener state is valid.
210 */
211int
213{
214 int is_valid;
215
216 switch (state) {
221 is_valid = 1;
222 break;
224 default:
225 is_valid = 0;
226 }
227
228 return is_valid;
229}
230
231/**
232 * Indicate whether a channel state transition is valid.
233 *
234 * This function takes two channel states and indicates whether a
235 * transition between them is permitted (see the state definitions and
236 * transition table in or.h at the channel_state_t typedef).
237 */
238int
240{
241 int is_valid;
242
243 switch (from) {
245 is_valid = (to == CHANNEL_STATE_OPENING);
246 break;
248 is_valid = (to == CHANNEL_STATE_CLOSED ||
249 to == CHANNEL_STATE_ERROR);
250 break;
252 is_valid = 0;
253 break;
255 is_valid = (to == CHANNEL_STATE_CLOSING ||
256 to == CHANNEL_STATE_ERROR ||
257 to == CHANNEL_STATE_OPEN);
258 break;
260 is_valid = (to == CHANNEL_STATE_CLOSING ||
261 to == CHANNEL_STATE_ERROR ||
262 to == CHANNEL_STATE_OPEN);
263 break;
265 is_valid = (to == CHANNEL_STATE_CLOSING ||
266 to == CHANNEL_STATE_ERROR ||
267 to == CHANNEL_STATE_MAINT);
268 break;
270 default:
271 is_valid = 0;
272 }
273
274 return is_valid;
275}
276
277/**
278 * Indicate whether a channel listener state transition is valid.
279 *
280 * This function takes two channel listener states and indicates whether a
281 * transition between them is permitted (see the state definitions and
282 * transition table in or.h at the channel_listener_state_t typedef).
283 */
284int
287{
288 int is_valid;
289
290 switch (from) {
292 is_valid = (to == CHANNEL_LISTENER_STATE_LISTENING);
293 break;
295 is_valid = (to == CHANNEL_LISTENER_STATE_CLOSED ||
297 break;
299 is_valid = 0;
300 break;
302 is_valid = (to == CHANNEL_LISTENER_STATE_CLOSING ||
304 break;
306 default:
307 is_valid = 0;
308 }
309
310 return is_valid;
311}
312
313/**
314 * Return a human-readable description for a channel state.
315 */
316const char *
318{
319 const char *descr;
320
321 switch (state) {
323 descr = "closed";
324 break;
326 descr = "closing";
327 break;
329 descr = "channel error";
330 break;
332 descr = "temporarily suspended for maintenance";
333 break;
335 descr = "opening";
336 break;
338 descr = "open";
339 break;
341 default:
342 descr = "unknown or invalid channel state";
343 }
344
345 return descr;
346}
347
348/**
349 * Return a human-readable description for a channel listener state.
350 */
351const char *
353{
354 const char *descr;
355
356 switch (state) {
358 descr = "closed";
359 break;
361 descr = "closing";
362 break;
364 descr = "channel listener error";
365 break;
367 descr = "listening";
368 break;
370 default:
371 descr = "unknown or invalid channel listener state";
372 }
373
374 return descr;
375}
376
377/***************************************
378 * Channel registration/unregistration *
379 ***************************************/
380
381/**
382 * Register a channel.
383 *
384 * This function registers a newly created channel in the global lists/maps
385 * of active channels.
386 */
387void
389{
390 tor_assert(chan);
392
393 /* No-op if already registered */
394 if (chan->registered) return;
395
396 log_debug(LD_CHANNEL,
397 "Registering channel %p (ID %"PRIu64 ") "
398 "in state %s (%d) with digest %s",
399 chan, (chan->global_identifier),
400 channel_state_to_string(chan->state), chan->state,
402
403 /* Make sure we have all_channels, then add it */
404 if (!all_channels) all_channels = smartlist_new();
405 smartlist_add(all_channels, chan);
406 channel_t *oldval = HT_REPLACE(channel_gid_map, &channel_gid_map, chan);
407 tor_assert(! oldval);
408
409 /* Is it finished? */
410 if (CHANNEL_FINISHED(chan)) {
411 /* Put it in the finished list, creating it if necessary */
412 if (!finished_channels) finished_channels = smartlist_new();
413 smartlist_add(finished_channels, chan);
415 } else {
416 /* Put it in the active list, creating it if necessary */
417 if (!active_channels) active_channels = smartlist_new();
418 smartlist_add(active_channels, chan);
419
420 if (!CHANNEL_IS_CLOSING(chan)) {
421 /* It should have a digest set */
423 /* Yeah, we're good, add it to the map */
425 } else {
426 log_info(LD_CHANNEL,
427 "Channel %p (global ID %"PRIu64 ") "
428 "in state %s (%d) registered with no identity digest",
429 chan, (chan->global_identifier),
430 channel_state_to_string(chan->state), chan->state);
431 }
432 }
433 }
434
435 /* Mark it as registered */
436 chan->registered = 1;
437}
438
439/**
440 * Unregister a channel.
441 *
442 * This function removes a channel from the global lists and maps and is used
443 * when freeing a closed/errored channel.
444 */
445void
447{
448 tor_assert(chan);
449
450 /* No-op if not registered */
451 if (!(chan->registered)) return;
452
453 /* Is it finished? */
454 if (CHANNEL_FINISHED(chan)) {
455 /* Get it out of the finished list */
456 if (finished_channels) smartlist_remove(finished_channels, chan);
457 } else {
458 /* Get it out of the active list */
459 if (active_channels) smartlist_remove(active_channels, chan);
460 }
461
462 /* Get it out of all_channels */
463 if (all_channels) smartlist_remove(all_channels, chan);
464 channel_t *oldval = HT_REMOVE(channel_gid_map, &channel_gid_map, chan);
465 tor_assert(oldval == NULL || oldval == chan);
466
467 /* Mark it as unregistered */
468 chan->registered = 0;
469
470 /* Should it be in the digest map? */
472 !(CHANNEL_CONDEMNED(chan))) {
473 /* Remove it */
475 }
476}
477
478/**
479 * Register a channel listener.
480 *
481 * This function registers a newly created channel listener in the global
482 * lists/maps of active channel listeners.
483 */
484void
486{
487 tor_assert(chan_l);
488
489 /* No-op if already registered */
490 if (chan_l->registered) return;
491
492 log_debug(LD_CHANNEL,
493 "Registering channel listener %p (ID %"PRIu64 ") "
494 "in state %s (%d)",
495 chan_l, (chan_l->global_identifier),
497 chan_l->state);
498
499 /* Make sure we have all_listeners, then add it */
500 if (!all_listeners) all_listeners = smartlist_new();
501 smartlist_add(all_listeners, chan_l);
502
503 /* Is it finished? */
504 if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
506 /* Put it in the finished list, creating it if necessary */
507 if (!finished_listeners) finished_listeners = smartlist_new();
508 smartlist_add(finished_listeners, chan_l);
509 } else {
510 /* Put it in the active list, creating it if necessary */
511 if (!active_listeners) active_listeners = smartlist_new();
512 smartlist_add(active_listeners, chan_l);
513 }
514
515 /* Mark it as registered */
516 chan_l->registered = 1;
517}
518
519/**
520 * Unregister a channel listener.
521 *
522 * This function removes a channel listener from the global lists and maps
523 * and is used when freeing a closed/errored channel listener.
524 */
525void
527{
528 tor_assert(chan_l);
529
530 /* No-op if not registered */
531 if (!(chan_l->registered)) return;
532
533 /* Is it finished? */
534 if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
536 /* Get it out of the finished list */
537 if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
538 } else {
539 /* Get it out of the active list */
540 if (active_listeners) smartlist_remove(active_listeners, chan_l);
541 }
542
543 /* Get it out of all_listeners */
544 if (all_listeners) smartlist_remove(all_listeners, chan_l);
545
546 /* Mark it as unregistered */
547 chan_l->registered = 0;
548}
549
550/*********************************
551 * Channel digest map maintenance
552 *********************************/
553
554/**
555 * Add a channel to the digest map.
556 *
557 * This function adds a channel to the digest map and inserts it into the
558 * correct linked list if channels with that remote endpoint identity digest
559 * already exist.
560 */
561STATIC void
563{
564 channel_idmap_entry_t *ent, search;
565
566 tor_assert(chan);
567
568 /* Assert that the state makes sense */
569 tor_assert(!CHANNEL_CONDEMNED(chan));
570
571 /* Assert that there is a digest */
573
574 memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
575 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
576 if (! ent) {
577 ent = tor_malloc(sizeof(channel_idmap_entry_t));
578 memcpy(ent->digest, chan->identity_digest, DIGEST_LEN);
579 TOR_LIST_INIT(&ent->channel_list);
580 HT_INSERT(channel_idmap, &channel_identity_map, ent);
581 }
582 TOR_LIST_INSERT_HEAD(&ent->channel_list, chan, next_with_same_id);
583
584 log_debug(LD_CHANNEL,
585 "Added channel %p (global ID %"PRIu64 ") "
586 "to identity map in state %s (%d) with digest %s",
587 chan, (chan->global_identifier),
588 channel_state_to_string(chan->state), chan->state,
590}
591
592/**
593 * Remove a channel from the digest map.
594 *
595 * This function removes a channel from the digest map and the linked list of
596 * channels for that digest if more than one exists.
597 */
598static void
600{
601 channel_idmap_entry_t *ent, search;
602
603 tor_assert(chan);
604
605 /* Assert that there is a digest */
607
608 /* Pull it out of its list, wherever that list is */
609 TOR_LIST_REMOVE(chan, next_with_same_id);
610
611 memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
612 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
613
614 /* Look for it in the map */
615 if (ent) {
616 /* Okay, it's here */
617
618 if (TOR_LIST_EMPTY(&ent->channel_list)) {
619 HT_REMOVE(channel_idmap, &channel_identity_map, ent);
620 tor_free(ent);
621 }
622
623 log_debug(LD_CHANNEL,
624 "Removed channel %p (global ID %"PRIu64 ") from "
625 "identity map in state %s (%d) with digest %s",
626 chan, (chan->global_identifier),
627 channel_state_to_string(chan->state), chan->state,
629 } else {
630 /* Shouldn't happen */
631 log_warn(LD_BUG,
632 "Trying to remove channel %p (global ID %"PRIu64 ") with "
633 "digest %s from identity map, but couldn't find any with "
634 "that digest",
635 chan, (chan->global_identifier),
637 }
638}
639
640/****************************
641 * Channel lookup functions *
642 ***************************/
643
644/**
645 * Find channel by global ID.
646 *
647 * This function searches for a channel by the global_identifier assigned
648 * at initialization time. This identifier is unique for the lifetime of the
649 * Tor process.
650 */
651channel_t *
652channel_find_by_global_id(uint64_t global_identifier)
653{
654 channel_t lookup;
655 channel_t *rv = NULL;
656
657 lookup.global_identifier = global_identifier;
658 rv = HT_FIND(channel_gid_map, &channel_gid_map, &lookup);
659 if (rv) {
660 tor_assert(rv->global_identifier == global_identifier);
661 }
662
663 return rv;
664}
665
666/** Return true iff <b>chan</b> matches <b>rsa_id_digest</b> and <b>ed_id</b>.
667 * as its identity keys. If either is NULL, do not check for a match. */
668int
670 const char *rsa_id_digest,
671 const ed25519_public_key_t *ed_id)
672{
673 if (BUG(!chan))
674 return 0;
675 if (rsa_id_digest) {
676 if (tor_memneq(rsa_id_digest, chan->identity_digest, DIGEST_LEN))
677 return 0;
678 }
679 if (ed_id) {
680 if (tor_memneq(ed_id->pubkey, chan->ed25519_identity.pubkey,
682 return 0;
683 }
684 return 1;
685}
686
687/**
688 * Find channel by RSA/Ed25519 identity of of the remote endpoint.
689 *
690 * This function looks up a channel by the digest of its remote endpoint's RSA
691 * identity key. If <b>ed_id</b> is provided and nonzero, only a channel
692 * matching the <b>ed_id</b> will be returned.
693 *
694 * It's possible that more than one channel to a given endpoint exists. Use
695 * channel_next_with_rsa_identity() to walk the list of channels; make sure
696 * to test for Ed25519 identity match too (as appropriate)
697 */
698channel_t *
699channel_find_by_remote_identity(const char *rsa_id_digest,
700 const ed25519_public_key_t *ed_id)
701{
702 channel_t *rv = NULL;
703 channel_idmap_entry_t *ent, search;
704
705 tor_assert(rsa_id_digest); /* For now, we require that every channel have
706 * an RSA identity, and that every lookup
707 * contain an RSA identity */
708 if (ed_id && ed25519_public_key_is_zero(ed_id)) {
709 /* Treat zero as meaning "We don't care about the presence or absence of
710 * an Ed key", not "There must be no Ed key". */
711 ed_id = NULL;
712 }
713
714 memcpy(search.digest, rsa_id_digest, DIGEST_LEN);
715 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
716 if (ent) {
717 rv = TOR_LIST_FIRST(&ent->channel_list);
718 }
719 while (rv && ! channel_remote_identity_matches(rv, rsa_id_digest, ed_id)) {
721 }
722
723 return rv;
724}
725
726/**
727 * Get next channel with digest.
728 *
729 * This function takes a channel and finds the next channel in the list
730 * with the same digest.
731 */
732channel_t *
734{
735 tor_assert(chan);
736
737 return TOR_LIST_NEXT(chan, next_with_same_id);
738}
739
740/**
741 * Relays run this once an hour to look over our list of channels to other
742 * relays. It prints out some statistics if there are multiple connections
743 * to many relays.
744 *
745 * This function is similar to connection_or_set_bad_connections(),
746 * and probably could be adapted to replace it, if it was modified to actually
747 * take action on any of these connections.
748 */
749void
751{
752 channel_idmap_entry_t **iter;
753 channel_t *chan;
754 int total_dirauth_connections = 0, total_dirauths = 0;
755 int total_relay_connections = 0, total_relays = 0, total_canonical = 0;
756 int total_half_canonical = 0;
757 int total_gt_one_connection = 0, total_gt_two_connections = 0;
758 int total_gt_four_connections = 0;
759
760 HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
761 int connections_to_relay = 0;
762 const char *id_digest = (char *) (*iter)->digest;
763
764 /* Only consider relay connections */
766 continue;
767
768 total_relays++;
769
770 const bool is_dirauth = router_digest_is_trusted_dir(id_digest);
771 if (is_dirauth)
772 total_dirauths++;
773
774 for (chan = TOR_LIST_FIRST(&(*iter)->channel_list); chan;
775 chan = channel_next_with_rsa_identity(chan)) {
776
777 if (CHANNEL_CONDEMNED(chan) || !CHANNEL_IS_OPEN(chan))
778 continue;
779
780 connections_to_relay++;
781 total_relay_connections++;
782 if (is_dirauth)
783 total_dirauth_connections++;
784
785 if (chan->is_canonical(chan)) total_canonical++;
786
787 if (!chan->is_canonical_to_peer && chan->is_canonical(chan)) {
788 total_half_canonical++;
789 }
790 }
791
792 if (connections_to_relay > 1) total_gt_one_connection++;
793 if (connections_to_relay > 2) total_gt_two_connections++;
794 if (connections_to_relay > 4) total_gt_four_connections++;
795 }
796
797 /* Don't bother warning about excessive connections unless we have
798 * at least this many connections, total.
799 */
800#define MIN_RELAY_CONNECTIONS_TO_WARN 25
801 /* If the average number of connections for a regular relay is more than
802 * this, that's too high.
803 */
804#define MAX_AVG_RELAY_CONNECTIONS 1.5
805 /* If the average number of connections for a dirauth is more than
806 * this, that's too high.
807 */
808#define MAX_AVG_DIRAUTH_CONNECTIONS 4
809
810 /* How many connections total would be okay, given the number of
811 * relays and dirauths that we have connections to? */
812 const int max_tolerable_connections = (int)(
813 (total_relays-total_dirauths) * MAX_AVG_RELAY_CONNECTIONS +
814 total_dirauths * MAX_AVG_DIRAUTH_CONNECTIONS);
815
816 /* If we average 1.5 or more connections per relay, something is wrong */
817 if (total_relays > MIN_RELAY_CONNECTIONS_TO_WARN &&
818 total_relay_connections > max_tolerable_connections) {
819 log_notice(LD_OR,
820 "Your relay has a very large number of connections to other relays. "
821 "Is your outbound address the same as your relay address? "
822 "Found %d connections to authorities, %d connections to %d relays. "
823 "Found %d current canonical connections, "
824 "in %d of which we were a non-canonical peer. "
825 "%d relays had more than 1 connection, %d had more than 2, and "
826 "%d had more than 4 connections.",
827 total_dirauth_connections, total_relay_connections,
828 total_relays, total_canonical, total_half_canonical,
829 total_gt_one_connection, total_gt_two_connections,
830 total_gt_four_connections);
831 } else {
832 log_info(LD_OR, "Performed connection pruning. "
833 "Found %d connections to authorities, %d connections to %d relays. "
834 "Found %d current canonical connections, "
835 "in %d of which we were a non-canonical peer. "
836 "%d relays had more than 1 connection, %d had more than 2, and "
837 "%d had more than 4 connections.",
838 total_dirauth_connections, total_relay_connections,
839 total_relays, total_canonical, total_half_canonical,
840 total_gt_one_connection, total_gt_two_connections,
841 total_gt_four_connections);
842 }
843}
844
845/**
846 * Initialize a channel.
847 *
848 * This function should be called by subclasses to set up some per-channel
849 * variables. I.e., this is the superclass constructor. Before this, the
850 * channel should be allocated with tor_malloc_zero().
851 */
852void
854{
855 tor_assert(chan);
856
857 /* Assign an ID and bump the counter */
858 chan->global_identifier = ++n_channels_allocated;
859
860 /* Init timestamp */
861 chan->timestamp_last_had_circuits = time(NULL);
862
863 /* Warn about exhausted circuit IDs no more than hourly. */
865
866 /* Initialize list entries. */
867 memset(&chan->next_with_same_id, 0, sizeof(chan->next_with_same_id));
868
869 /* Timestamp it */
871
872 /* It hasn't been open yet. */
873 chan->has_been_open = 0;
874
875 /* Scheduler state is idle */
876 chan->scheduler_state = SCHED_CHAN_IDLE;
877
878 /* Channel is not in the scheduler heap. */
879 chan->sched_heap_idx = -1;
880
882}
883
884/**
885 * Initialize a channel listener.
886 *
887 * This function should be called by subclasses to set up some per-channel
888 * variables. I.e., this is the superclass constructor. Before this, the
889 * channel listener should be allocated with tor_malloc_zero().
890 */
891void
893{
894 tor_assert(chan_l);
895
896 /* Assign an ID and bump the counter */
897 chan_l->global_identifier = ++n_channels_allocated;
898
899 /* Timestamp it */
901}
902
903/**
904 * Free a channel; nothing outside of channel.c and subclasses should call
905 * this - it frees channels after they have closed and been unregistered.
906 */
907void
909{
910 if (!chan) return;
911
912 /* It must be closed or errored */
913 tor_assert(CHANNEL_FINISHED(chan));
914
915 /* It must be deregistered */
916 tor_assert(!(chan->registered));
917
918 /* Direct destruction releases the weak handle even if closure was
919 * bypassed; freeing a channel is never evidence of establishment failure. */
921
922 log_debug(LD_CHANNEL,
923 "Freeing channel %"PRIu64 " at %p",
924 (chan->global_identifier), chan);
925
926 /* Get this one out of the scheduler */
927 scheduler_release_channel(chan);
928
929 /*
930 * Get rid of cmux policy before we do anything, so cmux policies don't
931 * see channels in weird half-freed states.
932 */
933 if (chan->cmux) {
934 circuitmux_set_policy(chan->cmux, NULL);
935 }
936
937 /* Remove all timers and associated handle entries now */
938 timer_free(chan->padding_timer);
939 channel_handle_free(chan->timer_handle);
940 channel_handles_clear(chan);
941
942 /* Call a free method if there is one */
943 if (chan->free_fn) chan->free_fn(chan);
944
946
947 /* Get rid of cmux */
948 if (chan->cmux) {
951 circuitmux_free(chan->cmux);
952 chan->cmux = NULL;
953 }
954
955 tor_free(chan);
956}
957
958/**
959 * Free a channel listener; nothing outside of channel.c and subclasses
960 * should call this - it frees channel listeners after they have closed and
961 * been unregistered.
962 */
963void
965{
966 if (!chan_l) return;
967
968 log_debug(LD_CHANNEL,
969 "Freeing channel_listener_t %"PRIu64 " at %p",
970 (chan_l->global_identifier),
971 chan_l);
972
973 /* It must be closed or errored */
976 /* It must be deregistered */
977 tor_assert(!(chan_l->registered));
978
979 /* Call a free method if there is one */
980 if (chan_l->free_fn) chan_l->free_fn(chan_l);
981
982 tor_free(chan_l);
983}
984
985/**
986 * Free a channel and skip the state/registration asserts; this internal-
987 * use-only function should be called only from channel_free_all() when
988 * shutting down the Tor process.
989 */
990static void
992{
993 tor_assert(chan);
994 /* Shutdown bypasses normal state transitions and channel_free_(). This
995 * releases the handle before subclass cleanup, without blaming the guard. */
997
998 log_debug(LD_CHANNEL,
999 "Force-freeing channel %"PRIu64 " at %p",
1000 (chan->global_identifier), chan);
1001
1002 /* Get this one out of the scheduler */
1003 scheduler_release_channel(chan);
1004
1005 /*
1006 * Get rid of cmux policy before we do anything, so cmux policies don't
1007 * see channels in weird half-freed states.
1008 */
1009 if (chan->cmux) {
1010 circuitmux_set_policy(chan->cmux, NULL);
1011 }
1012
1013 /* Remove all timers and associated handle entries now */
1014 timer_free(chan->padding_timer);
1015 channel_handle_free(chan->timer_handle);
1016 channel_handles_clear(chan);
1017
1018 /* Call a free method if there is one */
1019 if (chan->free_fn) chan->free_fn(chan);
1020
1022
1023 /* Get rid of cmux */
1024 if (chan->cmux) {
1025 circuitmux_free(chan->cmux);
1026 chan->cmux = NULL;
1027 }
1028
1029 tor_free(chan);
1030}
1031
1032/**
1033 * Free a channel listener and skip the state/registration asserts; this
1034 * internal-use-only function should be called only from channel_free_all()
1035 * when shutting down the Tor process.
1036 */
1037static void
1039{
1040 tor_assert(chan_l);
1041
1042 log_debug(LD_CHANNEL,
1043 "Force-freeing channel_listener_t %"PRIu64 " at %p",
1044 (chan_l->global_identifier),
1045 chan_l);
1046
1047 /* Call a free method if there is one */
1048 if (chan_l->free_fn) chan_l->free_fn(chan_l);
1049
1050 /*
1051 * The incoming list just gets emptied and freed; we request close on
1052 * any channels we find there, but since we got called while shutting
1053 * down they will get deregistered and freed elsewhere anyway.
1054 */
1055 if (chan_l->incoming_list) {
1057 channel_t *, qchan) {
1059 } SMARTLIST_FOREACH_END(qchan);
1060
1061 smartlist_free(chan_l->incoming_list);
1062 chan_l->incoming_list = NULL;
1063 }
1064
1065 tor_free(chan_l);
1066}
1067
1068/**
1069 * Set the listener for a channel listener.
1070 *
1071 * This function sets the handler for new incoming channels on a channel
1072 * listener.
1073 */
1074void
1076 channel_listener_fn_ptr listener)
1077{
1078 tor_assert(chan_l);
1080
1081 log_debug(LD_CHANNEL,
1082 "Setting listener callback for channel listener %p "
1083 "(global ID %"PRIu64 ") to %p",
1084 chan_l, (chan_l->global_identifier),
1085 listener);
1086
1087 chan_l->listener = listener;
1088 if (chan_l->listener) channel_listener_process_incoming(chan_l);
1089}
1090
1091/**
1092 * Return the fixed-length cell handler for a channel.
1093 *
1094 * This function gets the handler for incoming fixed-length cells installed
1095 * on a channel.
1096 */
1097channel_cell_handler_fn_ptr
1099{
1100 tor_assert(chan);
1101
1102 if (CHANNEL_CAN_HANDLE_CELLS(chan))
1103 return chan->cell_handler;
1104
1105 return NULL;
1106}
1107
1108/**
1109 * Set both cell handlers for a channel.
1110 *
1111 * This function sets both the fixed-length and variable length cell handlers
1112 * for a channel.
1113 */
1114void
1116 channel_cell_handler_fn_ptr cell_handler)
1117{
1118 tor_assert(chan);
1119 tor_assert(CHANNEL_CAN_HANDLE_CELLS(chan));
1120
1121 log_debug(LD_CHANNEL,
1122 "Setting cell_handler callback for channel %p to %p",
1123 chan, cell_handler);
1124
1125 /* Change them */
1126 chan->cell_handler = cell_handler;
1127}
1128
1129/*
1130 * On closing channels
1131 *
1132 * There are three functions that close channels, for use in
1133 * different circumstances:
1134 *
1135 * - Use channel_mark_for_close() for most cases
1136 * - Use channel_close_from_lower_layer() if you are connection_or.c
1137 * and the other end closes the underlying connection.
1138 * - Use channel_close_for_error() if you are connection_or.c and
1139 * some sort of error has occurred.
1140 */
1141
1142/**
1143 * Mark a channel for closure.
1144 *
1145 * This function tries to close a channel_t; it will go into the CLOSING
1146 * state, and eventually the lower layer should put it into the CLOSED or
1147 * ERROR state. Then, channel_run_cleanup() will eventually free it.
1148 */
1149void
1151{
1152 tor_assert(chan != NULL);
1153 tor_assert(chan->close != NULL);
1154
1155 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1156 if (CHANNEL_CONDEMNED(chan))
1157 return;
1158
1159 log_debug(LD_CHANNEL,
1160 "Closing channel %p (global ID %"PRIu64 ") "
1161 "by request",
1162 chan, (chan->global_identifier));
1163
1164 /* Note closing by request from above */
1165 chan->reason_for_closing = CHANNEL_CLOSE_REQUESTED;
1166
1167 /* Change state to CLOSING */
1169
1170 /* Tell the lower layer */
1171 chan->close(chan);
1172
1173 /*
1174 * It's up to the lower layer to change state to CLOSED or ERROR when we're
1175 * ready; we'll try to free channels that are in the finished list from
1176 * channel_run_cleanup(). The lower layer should do this by calling
1177 * channel_closed().
1178 */
1179}
1180
1181/**
1182 * Mark a channel listener for closure.
1183 *
1184 * This function tries to close a channel_listener_t; it will go into the
1185 * CLOSING state, and eventually the lower layer should put it into the CLOSED
1186 * or ERROR state. Then, channel_run_cleanup() will eventually free it.
1187 */
1188void
1190{
1191 tor_assert(chan_l != NULL);
1192 tor_assert(chan_l->close != NULL);
1193
1194 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1195 if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
1197 chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
1198
1199 log_debug(LD_CHANNEL,
1200 "Closing channel listener %p (global ID %"PRIu64 ") "
1201 "by request",
1202 chan_l, (chan_l->global_identifier));
1203
1204 /* Note closing by request from above */
1205 chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_REQUESTED;
1206
1207 /* Change state to CLOSING */
1209
1210 /* Tell the lower layer */
1211 chan_l->close(chan_l);
1212
1213 /*
1214 * It's up to the lower layer to change state to CLOSED or ERROR when we're
1215 * ready; we'll try to free channels that are in the finished list from
1216 * channel_run_cleanup(). The lower layer should do this by calling
1217 * channel_listener_closed().
1218 */
1219}
1220
1221/**
1222 * Close a channel from the lower layer.
1223 *
1224 * Notify the channel code that the channel is being closed due to a non-error
1225 * condition in the lower layer. This does not call the close() method, since
1226 * the lower layer already knows.
1227 */
1228void
1230{
1231 tor_assert(chan != NULL);
1232
1233 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1234 if (CHANNEL_CONDEMNED(chan))
1235 return;
1236
1237 log_debug(LD_CHANNEL,
1238 "Closing channel %p (global ID %"PRIu64 ") "
1239 "due to lower-layer event",
1240 chan, (chan->global_identifier));
1241
1242 /* Note closing by event from below */
1243 chan->reason_for_closing = CHANNEL_CLOSE_FROM_BELOW;
1244
1245 /* Change state to CLOSING */
1247}
1248
1249/**
1250 * Notify that the channel is being closed due to an error condition.
1251 *
1252 * This function is called by the lower layer implementing the transport
1253 * when a channel must be closed due to an error condition. This does not
1254 * call the channel's close method, since the lower layer already knows.
1255 */
1256void
1258{
1259 tor_assert(chan != NULL);
1260
1261 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1262 if (CHANNEL_CONDEMNED(chan))
1263 return;
1264
1265 log_debug(LD_CHANNEL,
1266 "Closing channel %p due to lower-layer error",
1267 chan);
1268
1269 /* Note closing by event from below */
1270 chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
1271
1272 /* Change state to CLOSING */
1274}
1275
1276/** Discards any unused permission to blame this channel's selected guard.
1277 * Releases the channel-owned weak handle without changing guard reachability
1278 * or closing the channel. It does not cancel requests using the channel.
1279 * This also ends attribution after successful establishment: "cancelled"
1280 * refers to permission to report failure, not to whether the connection
1281 * succeeded.
1282 *
1283 * After failure reporting, success, or earlier cancellation has consumed the
1284 * handle, this function is a no-op and cannot undo a reported failure.
1285 *
1286 * Callers ending attribution without reporting failure must call this before
1287 * invoking callbacks that could report another error. Callers must install
1288 * the handle only at launch and must never reinstall it after clearing. */
1289void
1291{
1292 if (!chan)
1293 return;
1294 struct entry_guard_handle_t *handle = chan->establishment_guard;
1295 chan->establishment_guard = NULL;
1297}
1298
1299/** Called by the OR failure finalizer. This function clears the channel
1300 * handle before delivering the failure, so reentrant callbacks and shared
1301 * requests cannot count the attempt twice.
1302 * OPENING plus outgoing excludes maintenance and all established traffic. */
1303void
1305{
1306 if (!chan)
1307 return;
1308 struct entry_guard_handle_t *handle = chan->establishment_guard;
1309 chan->establishment_guard = NULL;
1310 if (!handle)
1311 return;
1312 if (!chan->is_incoming && chan->state == CHANNEL_STATE_OPENING &&
1313 !chan->has_been_open && !net_is_disabled())
1316}
1317
1318/**
1319 * Notify that the lower layer is finished closing the channel.
1320 *
1321 * This function should be called by the lower layer when a channel
1322 * is finished closing and it should be regarded as inactive and
1323 * freed by the channel code.
1324 */
1325void
1327{
1328 tor_assert(chan);
1329 tor_assert(CHANNEL_CONDEMNED(chan));
1330 /* The required transition to a condemned state already clears this handle.
1331 * This defensive cleanup precedes early return and circuit callbacks in
1332 * case a future teardown path leaves an association behind. It is redundant
1333 * today and never infers failure from closure. */
1335
1336 /* No-op if already inactive */
1337 if (CHANNEL_FINISHED(chan))
1338 return;
1339
1340 /* Inform any pending (not attached) circs that they should
1341 * give up. */
1342 if (! chan->has_been_open)
1343 circuit_n_chan_done(chan, 0);
1344
1345 /* Now close all the attached circuits on it. */
1346 circuit_unlink_all_from_channel(chan, END_CIRC_REASON_CHANNEL_CLOSED);
1347
1348 if (chan->reason_for_closing != CHANNEL_CLOSE_FOR_ERROR) {
1350 } else {
1352 }
1353}
1354
1355/**
1356 * Clear the identity_digest of a channel.
1357 *
1358 * This function clears the identity digest of the remote endpoint for a
1359 * channel; this is intended for use by the lower layer.
1360 */
1361void
1363{
1364 int state_not_in_map;
1365
1366 tor_assert(chan);
1367
1368 log_debug(LD_CHANNEL,
1369 "Clearing remote endpoint digest on channel %p with "
1370 "global ID %"PRIu64,
1371 chan, (chan->global_identifier));
1372
1373 state_not_in_map = CHANNEL_CONDEMNED(chan);
1374
1375 if (!state_not_in_map && chan->registered &&
1377 /* if it's registered get it out of the digest map */
1379
1380 memset(chan->identity_digest, 0,
1381 sizeof(chan->identity_digest));
1382}
1383
1384/**
1385 * Set the identity_digest of a channel.
1386 *
1387 * This function sets the identity digest of the remote endpoint for a
1388 * channel; this is intended for use by the lower layer.
1389 */
1390void
1392 const char *identity_digest,
1393 const ed25519_public_key_t *ed_identity)
1394{
1395 int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
1396
1397 tor_assert(chan);
1398
1399 log_debug(LD_CHANNEL,
1400 "Setting remote endpoint digest on channel %p with "
1401 "global ID %"PRIu64 " to digest %s",
1402 chan, (chan->global_identifier),
1403 identity_digest ?
1404 hex_str(identity_digest, DIGEST_LEN) : "(null)");
1405
1406 state_not_in_map = CHANNEL_CONDEMNED(chan);
1407
1408 was_in_digest_map =
1409 !state_not_in_map &&
1410 chan->registered &&
1412 should_be_in_digest_map =
1413 !state_not_in_map &&
1414 chan->registered &&
1415 (identity_digest &&
1416 !tor_digest_is_zero(identity_digest));
1417
1418 if (was_in_digest_map)
1419 /* We should always remove it; we'll add it back if we're writing
1420 * in a new digest.
1421 */
1423
1424 if (identity_digest) {
1425 memcpy(chan->identity_digest,
1426 identity_digest,
1427 sizeof(chan->identity_digest));
1428 } else {
1429 memset(chan->identity_digest, 0,
1430 sizeof(chan->identity_digest));
1431 }
1432 if (ed_identity) {
1433 memcpy(&chan->ed25519_identity, ed_identity, sizeof(*ed_identity));
1434 } else {
1435 memset(&chan->ed25519_identity, 0, sizeof(*ed_identity));
1436 }
1437
1438 /* Put it in the digest map if we should */
1439 if (should_be_in_digest_map)
1441}
1442
1443/**
1444 * Clear the remote end metadata (identity_digest) of a channel.
1445 *
1446 * This function clears all the remote end info from a channel; this is
1447 * intended for use by the lower layer.
1448 */
1449void
1451{
1452 int state_not_in_map;
1453
1454 tor_assert(chan);
1455
1456 log_debug(LD_CHANNEL,
1457 "Clearing remote endpoint identity on channel %p with "
1458 "global ID %"PRIu64,
1459 chan, (chan->global_identifier));
1460
1461 state_not_in_map = CHANNEL_CONDEMNED(chan);
1462
1463 if (!state_not_in_map && chan->registered &&
1465 /* if it's registered get it out of the digest map */
1467
1468 memset(chan->identity_digest, 0,
1469 sizeof(chan->identity_digest));
1470}
1471
1472/**
1473 * Write to a channel the given packed cell.
1474 *
1475 * Two possible errors can happen. Either the channel is not opened or the
1476 * lower layer (specialized channel) failed to write it. In both cases, it is
1477 * the caller responsibility to free the cell.
1478 */
1479static int
1481{
1482 int ret = -1;
1483 size_t cell_bytes;
1484 uint8_t command = packed_cell_get_command(cell, chan->wide_circ_ids);
1485
1486 tor_assert(chan);
1487 tor_assert(cell);
1488
1489 /* Assert that the state makes sense for a cell write */
1490 tor_assert(CHANNEL_CAN_HANDLE_CELLS(chan));
1491
1492 {
1493 circid_t circ_id;
1494 if (packed_cell_is_destroy(chan, cell, &circ_id)) {
1495 channel_note_destroy_not_pending(chan, circ_id);
1496 }
1497 }
1498
1499 /* For statistical purposes, figure out how big this cell is */
1500 cell_bytes = get_cell_network_size(chan->wide_circ_ids);
1501
1502 /* Can we send it right out? If so, try */
1503 if (!CHANNEL_IS_OPEN(chan)) {
1504 goto done;
1505 }
1506
1507 /* Write the cell on the connection's outbuf. */
1508 if (chan->write_packed_cell(chan, cell) < 0) {
1509 goto done;
1510 }
1511 /* Timestamp for transmission */
1513 /* Update the counter */
1514 ++(chan->n_cells_xmitted);
1515 chan->n_bytes_xmitted += cell_bytes;
1516 /* Successfully sent the cell. */
1517 ret = 0;
1518
1519 /* Update padding statistics for the packed codepath.. */
1521 if (command == CELL_PADDING)
1523 if (chan->padding_enabled) {
1525 if (command == CELL_PADDING)
1527 }
1528
1529 done:
1530 return ret;
1531}
1532
1533/**
1534 * Write a packed cell to a channel.
1535 *
1536 * Write a packed cell to a channel using the write_cell() method. This is
1537 * called by the transport-independent code to deliver a packed cell to a
1538 * channel for transmission.
1539 *
1540 * Return 0 on success else a negative value. In both cases, the caller should
1541 * not access the cell anymore, it is freed both on success and error.
1542 */
1543int
1545{
1546 int ret = -1;
1547
1548 tor_assert(chan);
1549 tor_assert(cell);
1550
1551 if (CHANNEL_IS_CLOSING(chan)) {
1552 log_debug(LD_CHANNEL, "Discarding %p on closing channel %p with "
1553 "global ID %"PRIu64, cell, chan,
1554 (chan->global_identifier));
1555 goto end;
1556 }
1557 log_debug(LD_CHANNEL,
1558 "Writing %p to channel %p with global ID "
1559 "%"PRIu64, cell, chan, (chan->global_identifier));
1560
1561 ret = write_packed_cell(chan, cell);
1562
1563 end:
1564 /* Whatever happens, we free the cell. Either an error occurred or the cell
1565 * was put on the connection outbuf, both cases we have ownership of the
1566 * cell and we free it. */
1567 packed_cell_free(cell);
1568 return ret;
1569}
1570
1571/**
1572 * Change channel state.
1573 *
1574 * This internal and subclass use only function is used to change channel
1575 * state, performing all transition validity checks and whatever actions
1576 * are appropriate to the state transition in question.
1577 */
1578static void
1580{
1581 channel_state_t from_state;
1582 unsigned char was_active, is_active;
1583 unsigned char was_in_id_map, is_in_id_map;
1584
1585 tor_assert(chan);
1586 from_state = chan->state;
1587
1591
1592 /* If we're going to a closing or closed state, we must have a reason set */
1593 if (from_state != to_state &&
1594 (to_state == CHANNEL_STATE_CLOSING ||
1595 to_state == CHANNEL_STATE_CLOSED ||
1596 to_state == CHANNEL_STATE_ERROR)) {
1597 tor_assert(chan->reason_for_closing != CHANNEL_NOT_CLOSING);
1598 }
1599
1600 /* Validate the transition before releasing attribution, including for
1601 * no-op transitions. Success and generic closure clear permission before
1602 * callbacks; OR error hooks must report eligible failure first. */
1603 if (to_state == CHANNEL_STATE_OPEN || to_state == CHANNEL_STATE_CLOSING ||
1604 to_state == CHANNEL_STATE_CLOSED || to_state == CHANNEL_STATE_ERROR)
1606
1607 /* Check for no-op transitions */
1608 if (from_state == to_state) {
1609 log_debug(LD_CHANNEL,
1610 "Got no-op transition from \"%s\" to itself on channel %p"
1611 "(global ID %"PRIu64 ")",
1612 channel_state_to_string(to_state),
1613 chan, (chan->global_identifier));
1614 return;
1615 }
1616
1617 log_debug(LD_CHANNEL,
1618 "Changing state of channel %p (global ID %"PRIu64
1619 ") from \"%s\" to \"%s\"",
1620 chan,
1621 (chan->global_identifier),
1623 channel_state_to_string(to_state));
1624
1625 chan->state = to_state;
1626
1627 /* Need to add to the right lists if the channel is registered */
1628 if (chan->registered) {
1629 was_active = !(from_state == CHANNEL_STATE_CLOSED ||
1630 from_state == CHANNEL_STATE_ERROR);
1631 is_active = !(to_state == CHANNEL_STATE_CLOSED ||
1632 to_state == CHANNEL_STATE_ERROR);
1633
1634 /* Need to take off active list and put on finished list? */
1635 if (was_active && !is_active) {
1636 if (active_channels) smartlist_remove(active_channels, chan);
1637 if (!finished_channels) finished_channels = smartlist_new();
1638 smartlist_add(finished_channels, chan);
1640 }
1641 /* Need to put on active list? */
1642 else if (!was_active && is_active) {
1643 if (finished_channels) smartlist_remove(finished_channels, chan);
1644 if (!active_channels) active_channels = smartlist_new();
1645 smartlist_add(active_channels, chan);
1646 }
1647
1648 if (!tor_digest_is_zero(chan->identity_digest)) {
1649 /* Now we need to handle the identity map */
1650 was_in_id_map = !(from_state == CHANNEL_STATE_CLOSING ||
1651 from_state == CHANNEL_STATE_CLOSED ||
1652 from_state == CHANNEL_STATE_ERROR);
1653 is_in_id_map = !(to_state == CHANNEL_STATE_CLOSING ||
1654 to_state == CHANNEL_STATE_CLOSED ||
1655 to_state == CHANNEL_STATE_ERROR);
1656
1657 if (!was_in_id_map && is_in_id_map) channel_add_to_digest_map(chan);
1658 else if (was_in_id_map && !is_in_id_map)
1660 }
1661 }
1662
1663 /*
1664 * If we're going to a closed/closing state, we don't need scheduling any
1665 * more; in CHANNEL_STATE_MAINT we can't accept writes.
1666 */
1667 if (to_state == CHANNEL_STATE_CLOSING ||
1668 to_state == CHANNEL_STATE_CLOSED ||
1669 to_state == CHANNEL_STATE_ERROR) {
1670 scheduler_release_channel(chan);
1671 } else if (to_state == CHANNEL_STATE_MAINT) {
1673 }
1674}
1675
1676/**
1677 * As channel_change_state_, but change the state to any state but open.
1678 */
1679void
1681{
1682 tor_assert(to_state != CHANNEL_STATE_OPEN);
1683 channel_change_state_(chan, to_state);
1684}
1685
1686/**
1687 * As channel_change_state, but change the state to open.
1688 */
1689void
1691{
1693
1694 /* Tell circuits if we opened and stuff */
1696 chan->has_been_open = 1;
1697}
1698
1699/**
1700 * Change channel listener state.
1701 *
1702 * This internal and subclass use only function is used to change channel
1703 * listener state, performing all transition validity checks and whatever
1704 * actions are appropriate to the state transition in question.
1705 */
1706void
1708 channel_listener_state_t to_state)
1709{
1710 channel_listener_state_t from_state;
1711 unsigned char was_active, is_active;
1712
1713 tor_assert(chan_l);
1714 from_state = chan_l->state;
1715
1719
1720 /* Check for no-op transitions */
1721 if (from_state == to_state) {
1722 log_debug(LD_CHANNEL,
1723 "Got no-op transition from \"%s\" to itself on channel "
1724 "listener %p (global ID %"PRIu64 ")",
1726 chan_l, (chan_l->global_identifier));
1727 return;
1728 }
1729
1730 /* If we're going to a closing or closed state, we must have a reason set */
1731 if (to_state == CHANNEL_LISTENER_STATE_CLOSING ||
1732 to_state == CHANNEL_LISTENER_STATE_CLOSED ||
1733 to_state == CHANNEL_LISTENER_STATE_ERROR) {
1734 tor_assert(chan_l->reason_for_closing != CHANNEL_LISTENER_NOT_CLOSING);
1735 }
1736
1737 log_debug(LD_CHANNEL,
1738 "Changing state of channel listener %p (global ID %"PRIu64
1739 "from \"%s\" to \"%s\"",
1740 chan_l, (chan_l->global_identifier),
1743
1744 chan_l->state = to_state;
1745
1746 /* Need to add to the right lists if the channel listener is registered */
1747 if (chan_l->registered) {
1748 was_active = !(from_state == CHANNEL_LISTENER_STATE_CLOSED ||
1749 from_state == CHANNEL_LISTENER_STATE_ERROR);
1750 is_active = !(to_state == CHANNEL_LISTENER_STATE_CLOSED ||
1751 to_state == CHANNEL_LISTENER_STATE_ERROR);
1752
1753 /* Need to take off active list and put on finished list? */
1754 if (was_active && !is_active) {
1755 if (active_listeners) smartlist_remove(active_listeners, chan_l);
1756 if (!finished_listeners) finished_listeners = smartlist_new();
1757 smartlist_add(finished_listeners, chan_l);
1759 }
1760 /* Need to put on active list? */
1761 else if (!was_active && is_active) {
1762 if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
1763 if (!active_listeners) active_listeners = smartlist_new();
1764 smartlist_add(active_listeners, chan_l);
1765 }
1766 }
1767
1768 if (to_state == CHANNEL_LISTENER_STATE_CLOSED ||
1769 to_state == CHANNEL_LISTENER_STATE_ERROR) {
1770 tor_assert(!(chan_l->incoming_list) ||
1771 smartlist_len(chan_l->incoming_list) == 0);
1772 }
1773}
1774
1775/* Maximum number of cells that is allowed to flush at once within
1776 * channel_flush_some_cells(). */
1777#define MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED 256
1778
1779/**
1780 * Try to flush cells of the given channel chan up to a maximum of num_cells.
1781 *
1782 * This is called by the scheduler when it wants to flush cells from the
1783 * channel's circuit queue(s) to the connection outbuf (not yet on the wire).
1784 *
1785 * If the channel is not in state CHANNEL_STATE_OPEN, this does nothing and
1786 * will return 0 meaning no cells were flushed.
1787 *
1788 * If num_cells is -1, we'll try to flush up to the maximum cells allowed
1789 * defined in MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED.
1790 *
1791 * On success, the number of flushed cells are returned and it can never be
1792 * above num_cells. If 0 is returned, no cells were flushed either because the
1793 * channel was not opened or we had no cells on the channel. A negative number
1794 * can NOT be sent back.
1795 *
1796 * This function is part of the fast path. */
1797MOCK_IMPL(ssize_t,
1798channel_flush_some_cells, (channel_t *chan, ssize_t num_cells))
1799{
1800 unsigned int unlimited = 0;
1801 ssize_t flushed = 0;
1802 int clamped_num_cells;
1803
1804 tor_assert(chan);
1805
1806 if (num_cells < 0) unlimited = 1;
1807 if (!unlimited && num_cells <= flushed) goto done;
1808
1809 /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
1810 if (CHANNEL_IS_OPEN(chan)) {
1811 if (circuitmux_num_cells(chan->cmux) > 0) {
1812 /* Calculate number of cells, including clamp */
1813 if (unlimited) {
1814 clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
1815 } else {
1816 if (num_cells - flushed >
1817 MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED) {
1818 clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
1819 } else {
1820 clamped_num_cells = (int)(num_cells - flushed);
1821 }
1822 }
1823
1824 /* Try to get more cells from any active circuits */
1826 chan, clamped_num_cells);
1827 }
1828 }
1829
1830 done:
1831 return flushed;
1832}
1833
1834/**
1835 * Check if any cells are available.
1836 *
1837 * This is used by the scheduler to know if the channel has more to flush
1838 * after a scheduling round.
1839 */
1840MOCK_IMPL(int,
1842{
1843 tor_assert(chan);
1844
1845 if (circuitmux_num_cells(chan->cmux) > 0) return 1;
1846
1847 /* Else no */
1848 return 0;
1849}
1850
1851/**
1852 * Notify the channel we're done flushing the output in the lower layer.
1853 *
1854 * Connection.c will call this when we've flushed the output; there's some
1855 * dirreq-related maintenance to do.
1856 */
1857void
1859{
1860 tor_assert(chan);
1861
1862 if (chan->dirreq_id != 0)
1863 geoip_change_dirreq_state(chan->dirreq_id,
1864 DIRREQ_TUNNELED,
1866}
1867
1868/**
1869 * Process the queue of incoming channels on a listener.
1870 *
1871 * Use a listener's registered callback to process as many entries in the
1872 * queue of incoming channels as possible.
1873 */
1874void
1876{
1877 tor_assert(listener);
1878
1879 /*
1880 * CHANNEL_LISTENER_STATE_CLOSING permitted because we drain the queue
1881 * while closing a listener.
1882 */
1885 tor_assert(listener->listener);
1886
1887 log_debug(LD_CHANNEL,
1888 "Processing queue of incoming connections for channel "
1889 "listener %p (global ID %"PRIu64 ")",
1890 listener, (listener->global_identifier));
1891
1892 if (!(listener->incoming_list)) return;
1893
1895 channel_t *, chan) {
1896 tor_assert(chan);
1897
1898 log_debug(LD_CHANNEL,
1899 "Handling incoming channel %p (%"PRIu64 ") "
1900 "for listener %p (%"PRIu64 ")",
1901 chan,
1902 (chan->global_identifier),
1903 listener,
1904 (listener->global_identifier));
1905 /* Make sure this is set correctly */
1907 listener->listener(listener, chan);
1908 } SMARTLIST_FOREACH_END(chan);
1909
1910 smartlist_free(listener->incoming_list);
1911 listener->incoming_list = NULL;
1912}
1913
1914/**
1915 * Take actions required when a channel becomes open.
1916 *
1917 * Handle actions we should do when we know a channel is open; a lot of
1918 * this comes from the old connection_or_set_state_open() of connection_or.c.
1919 *
1920 * Because of this mechanism, future channel_t subclasses should take care
1921 * not to change a channel from CHANNEL_STATE_OPENING to CHANNEL_STATE_OPEN
1922 * until there is positive confirmation that the network is operational.
1923 * In particular, anything UDP-based should not make this transition until a
1924 * packet is received from the other side.
1925 */
1926void
1928{
1929 tor_addr_t remote_addr;
1930 int started_here;
1931
1932 tor_assert(chan);
1933
1934 started_here = channel_is_outgoing(chan);
1935
1936 if (started_here) {
1939 } else {
1940 /* only report it to the geoip module if it's a client and it hasn't
1941 * already been set up for tracking earlier. (Incoming TLS connections
1942 * are tracked before the handshake.) */
1943 if (channel_is_client(chan)) {
1944 if (channel_get_addr_if_possible(chan, &remote_addr)) {
1945 channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
1946 if (!tlschan->conn->tracked_for_dos_mitigation) {
1947 char *transport_name = NULL;
1948 if (chan->get_transport_name(chan, &transport_name) < 0) {
1949 transport_name = NULL;
1950 }
1951 geoip_note_client_seen(GEOIP_CLIENT_CONNECT,
1952 &remote_addr, transport_name,
1953 time(NULL));
1954 if (tlschan && tlschan->conn) {
1955 dos_new_client_conn(tlschan->conn, transport_name);
1956 }
1957 tor_free(transport_name);
1958 }
1959 }
1960 /* Otherwise the underlying transport can't tell us this, so skip it */
1961 }
1962 }
1963
1964 /* Disable or reduce padding according to user prefs. */
1965 if (chan->padding_enabled || get_options()->ConnectionPadding == 1) {
1966 if (!get_options()->ConnectionPadding) {
1967 /* Disable if torrc disabled */
1969 } else if (hs_service_allow_non_anonymous_connection(get_options()) &&
1971 CHANNELPADDING_SOS_PARAM,
1972 CHANNELPADDING_SOS_DEFAULT, 0, 1)) {
1973 /* Disable if we're using RSOS and the consensus disabled padding
1974 * for RSOS */
1976 } else if (get_options()->ReducedConnectionPadding) {
1977 /* Padding can be forced and/or reduced by clients, regardless of if
1978 * the channel supports it */
1980 }
1981 }
1982
1983 circuit_n_chan_done(chan, 1);
1984}
1985
1986/**
1987 * Queue an incoming channel on a listener.
1988 *
1989 * Internal and subclass use only function to queue an incoming channel from
1990 * a listener. A subclass of channel_listener_t should call this when a new
1991 * incoming channel is created.
1992 */
1993void
1995 channel_t *incoming)
1996{
1997 int need_to_queue = 0;
1998
1999 tor_assert(listener);
2001 tor_assert(incoming);
2002
2003 log_debug(LD_CHANNEL,
2004 "Queueing incoming channel %p (global ID %"PRIu64 ") on "
2005 "channel listener %p (global ID %"PRIu64 ")",
2006 incoming, (incoming->global_identifier),
2007 listener, (listener->global_identifier));
2008
2009 /* Do we need to queue it, or can we just call the listener right away? */
2010 if (!(listener->listener)) need_to_queue = 1;
2011 if (listener->incoming_list &&
2012 (smartlist_len(listener->incoming_list) > 0))
2013 need_to_queue = 1;
2014
2015 /* If we need to queue and have no queue, create one */
2016 if (need_to_queue && !(listener->incoming_list)) {
2017 listener->incoming_list = smartlist_new();
2018 }
2019
2020 /* Bump the counter and timestamp it */
2023 ++(listener->n_accepted);
2024
2025 /* If we don't need to queue, process it right away */
2026 if (!need_to_queue) {
2027 tor_assert(listener->listener);
2028 listener->listener(listener, incoming);
2029 }
2030 /*
2031 * Otherwise, we need to queue; queue and then process the queue if
2032 * we can.
2033 */
2034 else {
2035 tor_assert(listener->incoming_list);
2036 smartlist_add(listener->incoming_list, incoming);
2037 if (listener->listener) channel_listener_process_incoming(listener);
2038 }
2039}
2040
2041/**
2042 * Process a cell from the given channel.
2043 */
2044void
2046{
2047 tor_assert(chan);
2048 tor_assert(CHANNEL_IS_CLOSING(chan) || CHANNEL_IS_MAINT(chan) ||
2049 CHANNEL_IS_OPEN(chan));
2050 tor_assert(cell);
2051
2052 /* Nothing we can do if we have no registered cell handlers */
2053 if (!chan->cell_handler)
2054 return;
2055
2056 /* Timestamp for receiving */
2058 /* Update received counter. */
2059 ++(chan->n_cells_recved);
2060 chan->n_bytes_recved += get_cell_network_size(chan->wide_circ_ids);
2061
2062 log_debug(LD_CHANNEL,
2063 "Processing incoming cell_t %p for channel %p (global ID "
2064 "%"PRIu64 ")", cell, chan,
2065 (chan->global_identifier));
2066 chan->cell_handler(chan, cell);
2067}
2068
2069/** If <b>packed_cell</b> on <b>chan</b> is a destroy cell, then set
2070 * *<b>circid_out</b> to its circuit ID, and return true. Otherwise, return
2071 * false. */
2072/* XXXX Move this function. */
2073int
2075 const packed_cell_t *packed_cell,
2076 circid_t *circid_out)
2077{
2078 if (chan->wide_circ_ids) {
2079 if (packed_cell->body[4] == CELL_DESTROY) {
2080 *circid_out = ntohl(get_uint32(packed_cell->body));
2081 return 1;
2082 }
2083 } else {
2084 if (packed_cell->body[2] == CELL_DESTROY) {
2085 *circid_out = ntohs(get_uint16(packed_cell->body));
2086 return 1;
2087 }
2088 }
2089 return 0;
2090}
2091
2092/**
2093 * Send destroy cell on a channel.
2094 *
2095 * Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
2096 * onto channel <b>chan</b>. Don't perform range-checking on reason:
2097 * we may want to propagate reasons from other cells.
2098 */
2099int
2100channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
2101{
2102 tor_assert(chan);
2103 if (circ_id == 0) {
2104 log_warn(LD_BUG, "Attempted to send a destroy cell for circID 0 "
2105 "on a channel %"PRIu64 " at %p in state %s (%d)",
2106 (chan->global_identifier),
2107 chan, channel_state_to_string(chan->state),
2108 chan->state);
2109 return 0;
2110 }
2111
2112 /* Check to make sure we can send on this channel first */
2113 if (!CHANNEL_CONDEMNED(chan) && chan->cmux) {
2114 channel_note_destroy_pending(chan, circ_id);
2115 circuitmux_append_destroy_cell(chan, chan->cmux, circ_id, reason);
2116 log_debug(LD_OR,
2117 "Sending destroy (circID %u) on channel %p "
2118 "(global ID %"PRIu64 ")",
2119 (unsigned)circ_id, chan,
2120 (chan->global_identifier));
2121 } else {
2122 log_warn(LD_BUG,
2123 "Someone called channel_send_destroy() for circID %u "
2124 "on a channel %"PRIu64 " at %p in state %s (%d)",
2125 (unsigned)circ_id, (chan->global_identifier),
2126 chan, channel_state_to_string(chan->state),
2127 chan->state);
2128 }
2129
2130 return 0;
2131}
2132
2133/**
2134 * Dump channel statistics to the log.
2135 *
2136 * This is called from dumpstats() in main.c and spams the log with
2137 * statistics on channels.
2138 */
2139void
2141{
2142 if (all_channels && smartlist_len(all_channels) > 0) {
2143 tor_log(severity, LD_GENERAL,
2144 "Dumping statistics about %d channels:",
2145 smartlist_len(all_channels));
2146 tor_log(severity, LD_GENERAL,
2147 "%d are active, and %d are done and waiting for cleanup",
2148 (active_channels != NULL) ?
2149 smartlist_len(active_channels) : 0,
2150 (finished_channels != NULL) ?
2151 smartlist_len(finished_channels) : 0);
2152
2153 SMARTLIST_FOREACH(all_channels, channel_t *, chan,
2154 channel_dump_statistics(chan, severity));
2155
2156 tor_log(severity, LD_GENERAL,
2157 "Done spamming about channels now");
2158 } else {
2159 tor_log(severity, LD_GENERAL,
2160 "No channels to dump");
2161 }
2162}
2163
2164/**
2165 * Dump channel listener statistics to the log.
2166 *
2167 * This is called from dumpstats() in main.c and spams the log with
2168 * statistics on channel listeners.
2169 */
2170void
2172{
2173 if (all_listeners && smartlist_len(all_listeners) > 0) {
2174 tor_log(severity, LD_GENERAL,
2175 "Dumping statistics about %d channel listeners:",
2176 smartlist_len(all_listeners));
2177 tor_log(severity, LD_GENERAL,
2178 "%d are active and %d are done and waiting for cleanup",
2179 (active_listeners != NULL) ?
2180 smartlist_len(active_listeners) : 0,
2181 (finished_listeners != NULL) ?
2182 smartlist_len(finished_listeners) : 0);
2183
2184 SMARTLIST_FOREACH(all_listeners, channel_listener_t *, chan_l,
2185 channel_listener_dump_statistics(chan_l, severity));
2186
2187 tor_log(severity, LD_GENERAL,
2188 "Done spamming about channel listeners now");
2189 } else {
2190 tor_log(severity, LD_GENERAL,
2191 "No channel listeners to dump");
2192 }
2193}
2194
2195/**
2196 * Clean up channels.
2197 *
2198 * This gets called periodically from run_scheduled_events() in main.c;
2199 * it cleans up after closed channels.
2200 */
2201void
2203{
2204 channel_t *tmp = NULL;
2205
2206 /* Check if we need to do anything */
2207 if (!finished_channels || smartlist_len(finished_channels) == 0) return;
2208
2209 /* Iterate through finished_channels and get rid of them */
2210 SMARTLIST_FOREACH_BEGIN(finished_channels, channel_t *, curr) {
2211 tmp = curr;
2212 /* Remove it from the list */
2213 SMARTLIST_DEL_CURRENT(finished_channels, curr);
2214 /* Also unregister it */
2215 channel_unregister(tmp);
2216 /* ... and free it */
2217 channel_free(tmp);
2218 } SMARTLIST_FOREACH_END(curr);
2219}
2220
2221/**
2222 * Clean up channel listeners.
2223 *
2224 * This gets called periodically from run_scheduled_events() in main.c;
2225 * it cleans up after closed channel listeners.
2226 */
2227void
2229{
2230 channel_listener_t *tmp = NULL;
2231
2232 /* Check if we need to do anything */
2233 if (!finished_listeners || smartlist_len(finished_listeners) == 0) return;
2234
2235 /* Iterate through finished_channels and get rid of them */
2236 SMARTLIST_FOREACH_BEGIN(finished_listeners, channel_listener_t *, curr) {
2237 tmp = curr;
2238 /* Remove it from the list */
2239 SMARTLIST_DEL_CURRENT(finished_listeners, curr);
2240 /* Also unregister it */
2242 /* ... and free it */
2243 channel_listener_free(tmp);
2244 } SMARTLIST_FOREACH_END(curr);
2245}
2246
2247/**
2248 * Free a list of channels for channel_free_all().
2249 */
2250static void
2251channel_free_list(smartlist_t *channels, int mark_for_close)
2252{
2253 if (!channels) return;
2254
2255 SMARTLIST_FOREACH_BEGIN(channels, channel_t *, curr) {
2256 /* Deregister and free it */
2257 tor_assert(curr);
2258 log_debug(LD_CHANNEL,
2259 "Cleaning up channel %p (global ID %"PRIu64 ") "
2260 "in state %s (%d)",
2261 curr, (curr->global_identifier),
2262 channel_state_to_string(curr->state), curr->state);
2263 /* Detach circuits early so they can find the channel */
2264 if (curr->cmux) {
2265 circuitmux_detach_all_circuits(curr->cmux, NULL);
2266 }
2267 SMARTLIST_DEL_CURRENT(channels, curr);
2268 channel_unregister(curr);
2269 if (mark_for_close) {
2270 if (!CHANNEL_CONDEMNED(curr)) {
2272 }
2273 channel_force_xfree(curr);
2274 } else channel_free(curr);
2275 } SMARTLIST_FOREACH_END(curr);
2276}
2277
2278/**
2279 * Free a list of channel listeners for channel_free_all().
2280 */
2281static void
2282channel_listener_free_list(smartlist_t *listeners, int mark_for_close)
2283{
2284 if (!listeners) return;
2285
2286 SMARTLIST_FOREACH_BEGIN(listeners, channel_listener_t *, curr) {
2287 /* Deregister and free it */
2288 tor_assert(curr);
2289 log_debug(LD_CHANNEL,
2290 "Cleaning up channel listener %p (global ID %"PRIu64 ") "
2291 "in state %s (%d)",
2292 curr, (curr->global_identifier),
2293 channel_listener_state_to_string(curr->state), curr->state);
2295 if (mark_for_close) {
2296 if (!(curr->state == CHANNEL_LISTENER_STATE_CLOSING ||
2297 curr->state == CHANNEL_LISTENER_STATE_CLOSED ||
2298 curr->state == CHANNEL_LISTENER_STATE_ERROR)) {
2300 }
2302 } else channel_listener_free(curr);
2303 } SMARTLIST_FOREACH_END(curr);
2304}
2305
2306/**
2307 * Close all channels and free everything.
2308 *
2309 * This gets called from tor_free_all() in main.c to clean up on exit.
2310 * It will close all registered channels and free associated storage,
2311 * then free the all_channels, active_channels, listening_channels and
2312 * finished_channels lists and also channel_identity_map.
2313 */
2314void
2316{
2317 log_debug(LD_CHANNEL,
2318 "Shutting down channels...");
2319
2320 /* First, let's go for finished channels */
2321 if (finished_channels) {
2322 channel_free_list(finished_channels, 0);
2323 smartlist_free(finished_channels);
2324 finished_channels = NULL;
2325 }
2326
2327 /* Now the finished listeners */
2328 if (finished_listeners) {
2329 channel_listener_free_list(finished_listeners, 0);
2330 smartlist_free(finished_listeners);
2331 finished_listeners = NULL;
2332 }
2333
2334 /* Now all active channels */
2335 if (active_channels) {
2336 channel_free_list(active_channels, 1);
2337 smartlist_free(active_channels);
2338 active_channels = NULL;
2339 }
2340
2341 /* Now all active listeners */
2342 if (active_listeners) {
2343 channel_listener_free_list(active_listeners, 1);
2344 smartlist_free(active_listeners);
2345 active_listeners = NULL;
2346 }
2347
2348 /* Now all channels, in case any are left over */
2349 if (all_channels) {
2350 channel_free_list(all_channels, 1);
2351 smartlist_free(all_channels);
2352 all_channels = NULL;
2353 }
2354
2355 /* Now all listeners, in case any are left over */
2356 if (all_listeners) {
2357 channel_listener_free_list(all_listeners, 1);
2358 smartlist_free(all_listeners);
2359 all_listeners = NULL;
2360 }
2361
2362 /* Now free channel_identity_map */
2363 log_debug(LD_CHANNEL,
2364 "Freeing channel_identity_map");
2365 /* Geez, anything still left over just won't die ... let it leak then */
2366 HT_CLEAR(channel_idmap, &channel_identity_map);
2367
2368 /* Same with channel_gid_map */
2369 log_debug(LD_CHANNEL,
2370 "Freeing channel_gid_map");
2371 HT_CLEAR(channel_gid_map, &channel_gid_map);
2372
2373 log_debug(LD_CHANNEL,
2374 "Done cleaning up after channels");
2375}
2376
2377/**
2378 * Connects to a given addr/port/digest. guard_state is borrowed only during
2379 * this synchronous launch; the new channel takes an independent weak handle.
2380 * Reuse decisions happen before this call and never replace a handle.
2381 * for_origin_circ identifies local circuit launches even without a guard
2382 * selection (for example, a fallback directory request).
2383 *
2384 * This sets up a new outgoing channel; in the future if multiple
2385 * channel_t subclasses are available, this is where the selection policy
2386 * should go. It may also be desirable to fold port into tor_addr_t
2387 * or make a new type including a tor_addr_t and port, so we have a
2388 * single abstract object encapsulating all the protocol details of
2389 * how to contact an OR.
2390 */
2391channel_t *
2392channel_connect(const tor_addr_t *addr, uint16_t port,
2393 const char *id_digest,
2394 const ed25519_public_key_t *ed_id,
2395 const struct circuit_guard_state_t *guard_state,
2396 bool for_origin_circ)
2397{
2398 return channel_tls_connect(addr, port, id_digest, ed_id, guard_state,
2399 for_origin_circ);
2400}
2401
2402/**
2403 * Decide which of two channels to prefer for extending a circuit.
2404 *
2405 * This function is called while extending a circuit and returns true iff
2406 * a is 'better' than b. The most important criterion here is that a
2407 * canonical channel is always better than a non-canonical one, but the
2408 * number of circuits and the age are used as tie-breakers.
2409 *
2410 * This is based on the former connection_or_is_better() of connection_or.c
2411 */
2412int
2414{
2415 int a_is_canonical, b_is_canonical;
2416
2417 tor_assert(a);
2418 tor_assert(b);
2419
2420 /* If one channel is bad for new circuits, and the other isn't,
2421 * use the one that is still good. */
2423 return 1;
2425 return 0;
2426
2427 /* Check if one is canonical and the other isn't first */
2428 a_is_canonical = channel_is_canonical(a);
2429 b_is_canonical = channel_is_canonical(b);
2430
2431 if (a_is_canonical && !b_is_canonical) return 1;
2432 if (!a_is_canonical && b_is_canonical) return 0;
2433
2434 /* Check if we suspect that one of the channels will be preferred
2435 * by the peer */
2436 if (a->is_canonical_to_peer && !b->is_canonical_to_peer) return 1;
2437 if (!a->is_canonical_to_peer && b->is_canonical_to_peer) return 0;
2438
2439 /*
2440 * Okay, if we're here they tied on canonicity. Prefer the older
2441 * connection, so that the adversary can't create a new connection
2442 * and try to switch us over to it (which will leak information
2443 * about long-lived circuits). Additionally, switching connections
2444 * too often makes us more vulnerable to attacks like Torscan and
2445 * passive netflow-based equivalents.
2446 *
2447 * Connections will still only live for at most a week, due to
2448 * the check in connection_or_group_set_badness() against
2449 * TIME_BEFORE_OR_CONN_IS_TOO_OLD, which marks old connections as
2450 * unusable for new circuits after 1 week. That check sets
2451 * is_bad_for_new_circs, which is checked in channel_get_for_extend().
2452 *
2453 * We check channel_is_bad_for_new_circs() above here anyway, for safety.
2454 */
2455 if (channel_when_created(a) < channel_when_created(b)) return 1;
2456 else if (channel_when_created(a) > channel_when_created(b)) return 0;
2457
2458 if (channel_num_circuits(a) > channel_num_circuits(b)) return 1;
2459 else return 0;
2460}
2461
2462/**
2463 * Get a channel to extend a circuit.
2464 *
2465 * Given the desired relay identity, pick a suitable channel to extend a
2466 * circuit to the target IPv4 or IPv6 address requested by the client. Search
2467 * for an existing channel for the requested endpoint. Make sure the channel
2468 * is usable for new circuits, and matches one of the target addresses.
2469 *
2470 * Try to return the best channel. But if there is no good channel, set
2471 * *msg_out to a message describing the channel's state and our next action,
2472 * and set *launch_out to a boolean indicated whether the caller should try to
2473 * launch a new channel with channel_connect().
2474 *
2475 * If `for_origin_circ` is set, mark the channel as interesting for origin
2476 * circuits, and therefore interesting for our bootstrapping reports.
2477 */
2479channel_get_for_extend,(const char *rsa_id_digest,
2480 const ed25519_public_key_t *ed_id,
2481 const tor_addr_t *target_ipv4_addr,
2482 const tor_addr_t *target_ipv6_addr,
2483 bool for_origin_circ,
2484 const char **msg_out,
2485 int *launch_out))
2486{
2487 channel_t *chan, *best = NULL;
2488 int n_inprogress_goodaddr = 0, n_old = 0;
2489 int n_noncanonical = 0;
2490
2491 tor_assert(msg_out);
2492 tor_assert(launch_out);
2493
2494 chan = channel_find_by_remote_identity(rsa_id_digest, ed_id);
2495
2496 /* Walk the list of channels */
2497 for (; chan; chan = channel_next_with_rsa_identity(chan)) {
2499 rsa_id_digest, DIGEST_LEN));
2500
2501 if (CHANNEL_CONDEMNED(chan))
2502 continue;
2503
2504 /* Never return a channel on which the other end appears to be
2505 * a client. */
2506 if (channel_is_client(chan)) {
2507 continue;
2508 }
2509
2510 /* The Ed25519 key has to match too */
2511 if (!channel_remote_identity_matches(chan, rsa_id_digest, ed_id)) {
2512 continue;
2513 }
2514
2515 const bool matches_target =
2517 target_ipv4_addr,
2518 target_ipv6_addr);
2519 /* Never return a non-open connection. */
2520 if (!CHANNEL_IS_OPEN(chan)) {
2521 /* If the address matches, don't launch a new connection for this
2522 * circuit. */
2523 if (matches_target) {
2524 ++n_inprogress_goodaddr;
2525 if (for_origin_circ) {
2526 /* We were looking for a connection for an origin circuit; this one
2527 * matches, so we'll note that we decided to use it for an origin
2528 * circuit. */
2530 }
2531 }
2532 continue;
2533 }
2534
2535 /* Never return a connection that shouldn't be used for circs. */
2536 if (channel_is_bad_for_new_circs(chan)) {
2537 ++n_old;
2538 continue;
2539 }
2540
2541 /* Only return canonical connections or connections where the address
2542 * is the address we wanted. */
2543 if (!channel_is_canonical(chan) && !matches_target) {
2544 ++n_noncanonical;
2545 continue;
2546 }
2547
2548 if (!best) {
2549 best = chan; /* If we have no 'best' so far, this one is good enough. */
2550 continue;
2551 }
2552
2553 if (channel_is_better(chan, best))
2554 best = chan;
2555 }
2556
2557 if (best) {
2558 *msg_out = "Connection is fine; using it.";
2559 *launch_out = 0;
2560 return best;
2561 } else if (n_inprogress_goodaddr) {
2562 *msg_out = "Connection in progress; waiting.";
2563 *launch_out = 0;
2564 return NULL;
2565 } else if (n_old || n_noncanonical) {
2566 *msg_out = "Connections all too old, or too non-canonical. "
2567 " Launching a new one.";
2568 *launch_out = 1;
2569 return NULL;
2570 } else {
2571 *msg_out = "Not connected. Connecting.";
2572 *launch_out = 1;
2573 return NULL;
2574 }
2575}
2576
2577/**
2578 * Describe the transport subclass for a channel.
2579 *
2580 * Invoke a method to get a string description of the lower-layer
2581 * transport for this channel.
2582 */
2583const char *
2585{
2586 tor_assert(chan);
2588
2589 return chan->describe_transport(chan);
2590}
2591
2592/**
2593 * Describe the transport subclass for a channel listener.
2594 *
2595 * Invoke a method to get a string description of the lower-layer
2596 * transport for this channel listener.
2597 */
2598const char *
2600{
2601 tor_assert(chan_l);
2603
2604 return chan_l->describe_transport(chan_l);
2605}
2606
2607/**
2608 * Dump channel statistics.
2609 *
2610 * Dump statistics for one channel to the log.
2611 */
2612MOCK_IMPL(void,
2613channel_dump_statistics, (channel_t *chan, int severity))
2614{
2615 double avg, interval, age;
2616 time_t now = time(NULL);
2617 tor_addr_t remote_addr;
2618 int have_remote_addr;
2619 char *remote_addr_str;
2620
2621 tor_assert(chan);
2622
2623 age = (double)(now - chan->timestamp_created);
2624
2625 tor_log(severity, LD_GENERAL,
2626 "Channel %"PRIu64 " (at %p) with transport %s is in state "
2627 "%s (%d)",
2628 (chan->global_identifier), chan,
2630 channel_state_to_string(chan->state), chan->state);
2631 tor_log(severity, LD_GENERAL,
2632 " * Channel %"PRIu64 " was created at %"PRIu64
2633 " (%"PRIu64 " seconds ago) "
2634 "and last active at %"PRIu64 " (%"PRIu64 " seconds ago)",
2635 (chan->global_identifier),
2636 (uint64_t)(chan->timestamp_created),
2637 (uint64_t)(now - chan->timestamp_created),
2638 (uint64_t)(chan->timestamp_active),
2639 (uint64_t)(now - chan->timestamp_active));
2640
2641 /* Handle digest. */
2642 if (!tor_digest_is_zero(chan->identity_digest)) {
2643 tor_log(severity, LD_GENERAL,
2644 " * Channel %"PRIu64 " says it is connected "
2645 "to an OR with digest %s",
2646 (chan->global_identifier),
2648 } else {
2649 tor_log(severity, LD_GENERAL,
2650 " * Channel %"PRIu64 " does not know the digest"
2651 " of the OR it is connected to",
2652 (chan->global_identifier));
2653 }
2654
2655 /* Handle remote address and descriptions */
2656 have_remote_addr = channel_get_addr_if_possible(chan, &remote_addr);
2657 if (have_remote_addr) {
2658 char *actual = tor_strdup(channel_describe_peer(chan));
2659 remote_addr_str = tor_addr_to_str_dup(&remote_addr);
2660 tor_log(severity, LD_GENERAL,
2661 " * Channel %"PRIu64 " says its remote address"
2662 " is %s, and gives a canonical description of \"%s\" and an "
2663 "actual description of \"%s\"",
2664 (chan->global_identifier),
2665 safe_str(remote_addr_str),
2666 safe_str(channel_describe_peer(chan)),
2667 safe_str(actual));
2668 tor_free(remote_addr_str);
2669 tor_free(actual);
2670 } else {
2671 char *actual = tor_strdup(channel_describe_peer(chan));
2672 tor_log(severity, LD_GENERAL,
2673 " * Channel %"PRIu64 " does not know its remote "
2674 "address, but gives a canonical description of \"%s\" and an "
2675 "actual description of \"%s\"",
2676 (chan->global_identifier),
2678 actual);
2679 tor_free(actual);
2680 }
2681
2682 /* Handle marks */
2683 tor_log(severity, LD_GENERAL,
2684 " * Channel %"PRIu64 " has these marks: %s %s %s %s %s",
2685 (chan->global_identifier),
2687 "bad_for_new_circs" : "!bad_for_new_circs",
2688 channel_is_canonical(chan) ?
2689 "canonical" : "!canonical",
2690 channel_is_client(chan) ?
2691 "client" : "!client",
2692 channel_is_local(chan) ?
2693 "local" : "!local",
2694 channel_is_incoming(chan) ?
2695 "incoming" : "outgoing");
2696
2697 /* Describe circuits */
2698 tor_log(severity, LD_GENERAL,
2699 " * Channel %"PRIu64 " has %d active circuits out of"
2700 " %d in total",
2701 (chan->global_identifier),
2702 (chan->cmux != NULL) ?
2704 (chan->cmux != NULL) ?
2705 circuitmux_num_circuits(chan->cmux) : 0);
2706
2707 /* Describe timestamps */
2708 if (chan->timestamp_client == 0) {
2709 tor_log(severity, LD_GENERAL,
2710 " * Channel %"PRIu64 " was never used by a "
2711 "client", (chan->global_identifier));
2712 } else {
2713 tor_log(severity, LD_GENERAL,
2714 " * Channel %"PRIu64 " was last used by a "
2715 "client at %"PRIu64 " (%"PRIu64 " seconds ago)",
2716 (chan->global_identifier),
2717 (uint64_t)(chan->timestamp_client),
2718 (uint64_t)(now - chan->timestamp_client));
2719 }
2720 if (chan->timestamp_recv == 0) {
2721 tor_log(severity, LD_GENERAL,
2722 " * Channel %"PRIu64 " never received a cell",
2723 (chan->global_identifier));
2724 } else {
2725 tor_log(severity, LD_GENERAL,
2726 " * Channel %"PRIu64 " last received a cell "
2727 "at %"PRIu64 " (%"PRIu64 " seconds ago)",
2728 (chan->global_identifier),
2729 (uint64_t)(chan->timestamp_recv),
2730 (uint64_t)(now - chan->timestamp_recv));
2731 }
2732 if (chan->timestamp_xmit == 0) {
2733 tor_log(severity, LD_GENERAL,
2734 " * Channel %"PRIu64 " never transmitted a cell",
2735 (chan->global_identifier));
2736 } else {
2737 tor_log(severity, LD_GENERAL,
2738 " * Channel %"PRIu64 " last transmitted a cell "
2739 "at %"PRIu64 " (%"PRIu64 " seconds ago)",
2740 (chan->global_identifier),
2741 (uint64_t)(chan->timestamp_xmit),
2742 (uint64_t)(now - chan->timestamp_xmit));
2743 }
2744
2745 /* Describe counters and rates */
2746 tor_log(severity, LD_GENERAL,
2747 " * Channel %"PRIu64 " has received "
2748 "%"PRIu64 " bytes in %"PRIu64 " cells and transmitted "
2749 "%"PRIu64 " bytes in %"PRIu64 " cells",
2750 (chan->global_identifier),
2751 (chan->n_bytes_recved),
2752 (chan->n_cells_recved),
2753 (chan->n_bytes_xmitted),
2754 (chan->n_cells_xmitted));
2755 if (now > chan->timestamp_created &&
2756 chan->timestamp_created > 0) {
2757 if (chan->n_bytes_recved > 0) {
2758 avg = (double)(chan->n_bytes_recved) / age;
2759 tor_log(severity, LD_GENERAL,
2760 " * Channel %"PRIu64 " has averaged %f "
2761 "bytes received per second",
2762 (chan->global_identifier), avg);
2763 }
2764 if (chan->n_cells_recved > 0) {
2765 avg = (double)(chan->n_cells_recved) / age;
2766 if (avg >= 1.0) {
2767 tor_log(severity, LD_GENERAL,
2768 " * Channel %"PRIu64 " has averaged %f "
2769 "cells received per second",
2770 (chan->global_identifier), avg);
2771 } else if (avg >= 0.0) {
2772 interval = 1.0 / avg;
2773 tor_log(severity, LD_GENERAL,
2774 " * Channel %"PRIu64 " has averaged %f "
2775 "seconds between received cells",
2776 (chan->global_identifier), interval);
2777 }
2778 }
2779 if (chan->n_bytes_xmitted > 0) {
2780 avg = (double)(chan->n_bytes_xmitted) / age;
2781 tor_log(severity, LD_GENERAL,
2782 " * Channel %"PRIu64 " has averaged %f "
2783 "bytes transmitted per second",
2784 (chan->global_identifier), avg);
2785 }
2786 if (chan->n_cells_xmitted > 0) {
2787 avg = (double)(chan->n_cells_xmitted) / age;
2788 if (avg >= 1.0) {
2789 tor_log(severity, LD_GENERAL,
2790 " * Channel %"PRIu64 " has averaged %f "
2791 "cells transmitted per second",
2792 (chan->global_identifier), avg);
2793 } else if (avg >= 0.0) {
2794 interval = 1.0 / avg;
2795 tor_log(severity, LD_GENERAL,
2796 " * Channel %"PRIu64 " has averaged %f "
2797 "seconds between transmitted cells",
2798 (chan->global_identifier), interval);
2799 }
2800 }
2801 }
2802
2803 /* Dump anything the lower layer has to say */
2804 channel_dump_transport_statistics(chan, severity);
2805}
2806
2807/**
2808 * Dump channel listener statistics.
2809 *
2810 * Dump statistics for one channel listener to the log.
2811 */
2812void
2814{
2815 double avg, interval, age;
2816 time_t now = time(NULL);
2817
2818 tor_assert(chan_l);
2819
2820 age = (double)(now - chan_l->timestamp_created);
2821
2822 tor_log(severity, LD_GENERAL,
2823 "Channel listener %"PRIu64 " (at %p) with transport %s is in "
2824 "state %s (%d)",
2825 (chan_l->global_identifier), chan_l,
2827 channel_listener_state_to_string(chan_l->state), chan_l->state);
2828 tor_log(severity, LD_GENERAL,
2829 " * Channel listener %"PRIu64 " was created at %"PRIu64
2830 " (%"PRIu64 " seconds ago) "
2831 "and last active at %"PRIu64 " (%"PRIu64 " seconds ago)",
2832 (chan_l->global_identifier),
2833 (uint64_t)(chan_l->timestamp_created),
2834 (uint64_t)(now - chan_l->timestamp_created),
2835 (uint64_t)(chan_l->timestamp_active),
2836 (uint64_t)(now - chan_l->timestamp_active));
2837
2838 tor_log(severity, LD_GENERAL,
2839 " * Channel listener %"PRIu64 " last accepted an incoming "
2840 "channel at %"PRIu64 " (%"PRIu64 " seconds ago) "
2841 "and has accepted %"PRIu64 " channels in total",
2842 (chan_l->global_identifier),
2843 (uint64_t)(chan_l->timestamp_accepted),
2844 (uint64_t)(now - chan_l->timestamp_accepted),
2845 (uint64_t)(chan_l->n_accepted));
2846
2847 /*
2848 * If it's sensible to do so, get the rate of incoming channels on this
2849 * listener
2850 */
2851 if (now > chan_l->timestamp_created &&
2852 chan_l->timestamp_created > 0 &&
2853 chan_l->n_accepted > 0) {
2854 avg = (double)(chan_l->n_accepted) / age;
2855 if (avg >= 1.0) {
2856 tor_log(severity, LD_GENERAL,
2857 " * Channel listener %"PRIu64 " has averaged %f incoming "
2858 "channels per second",
2859 (chan_l->global_identifier), avg);
2860 } else if (avg >= 0.0) {
2861 interval = 1.0 / avg;
2862 tor_log(severity, LD_GENERAL,
2863 " * Channel listener %"PRIu64 " has averaged %f seconds "
2864 "between incoming channels",
2865 (chan_l->global_identifier), interval);
2866 }
2867 }
2868
2869 /* Dump anything the lower layer has to say */
2871}
2872
2873/**
2874 * Invoke transport-specific stats dump for channel.
2875 *
2876 * If there is a lower-layer statistics dump method, invoke it.
2877 */
2878void
2880{
2881 tor_assert(chan);
2882
2883 if (chan->dumpstats) chan->dumpstats(chan, severity);
2884}
2885
2886/**
2887 * Invoke transport-specific stats dump for channel listener.
2888 *
2889 * If there is a lower-layer statistics dump method, invoke it.
2890 */
2891void
2893 int severity)
2894{
2895 tor_assert(chan_l);
2896
2897 if (chan_l->dumpstats) chan_l->dumpstats(chan_l, severity);
2898}
2899
2900/**
2901 * Return text description of the remote endpoint canonical address.
2902 *
2903 * This function returns a human-readable string for logging; nothing
2904 * should parse it or rely on a particular format.
2905 *
2906 * Subsequent calls to this function may invalidate its return value.
2907 */
2908MOCK_IMPL(const char *,
2910{
2911 tor_assert(chan);
2913
2914 return chan->describe_peer(chan);
2915}
2916
2917/**
2918 * Get the remote address for this channel, if possible.
2919 *
2920 * Write the remote address out to a tor_addr_t if the underlying transport
2921 * supports this operation, and return 1. Return 0 if the underlying transport
2922 * doesn't let us do this.
2923 *
2924 * Always returns the "real" address of the peer -- the one we're connected to
2925 * on the internet.
2926 */
2927MOCK_IMPL(int,
2929 tor_addr_t *addr_out))
2930{
2931 tor_assert(chan);
2932 tor_assert(addr_out);
2933 tor_assert(chan->get_remote_addr);
2934
2935 return chan->get_remote_addr(chan, addr_out);
2936}
2937
2938/**
2939 * Return true iff the channel has any cells on the connection outbuf waiting
2940 * to be sent onto the network.
2941 */
2942int
2944{
2945 tor_assert(chan);
2947
2948 /* Check with the lower layer */
2949 return chan->has_queued_writes(chan);
2950}
2951
2952/**
2953 * Check the is_bad_for_new_circs flag.
2954 *
2955 * This function returns the is_bad_for_new_circs flag of the specified
2956 * channel.
2957 */
2958int
2960{
2961 tor_assert(chan);
2962
2963 return chan->is_bad_for_new_circs;
2964}
2965
2966/**
2967 * Mark a channel as bad for new circuits.
2968 *
2969 * Set the is_bad_for_new_circs_flag on chan.
2970 */
2971void
2973{
2974 tor_assert(chan);
2975
2976 chan->is_bad_for_new_circs = 1;
2977}
2978
2979/**
2980 * Get the client flag.
2981 *
2982 * This returns the client flag of a channel, which will be set if
2983 * command_process_create_cell() in command.c thinks this is a connection
2984 * from a client.
2985 */
2986int
2988{
2989 tor_assert(chan);
2990
2991 return chan->is_client;
2992}
2993
2994/**
2995 * Set the client flag.
2996 *
2997 * Mark a channel as being from a client.
2998 */
2999void
3001{
3002 tor_assert(chan);
3003
3004 chan->is_client = 1;
3005}
3006
3007/**
3008 * Clear the client flag.
3009 *
3010 * Mark a channel as being _not_ from a client.
3011 */
3012void
3014{
3015 tor_assert(chan);
3016
3017 chan->is_client = 0;
3018}
3019
3020/**
3021 * Get the canonical flag for a channel.
3022 *
3023 * This returns the is_canonical for a channel; this flag is determined by
3024 * the lower layer and can't be set in a transport-independent way.
3025 */
3026int
3028{
3029 tor_assert(chan);
3030 tor_assert(chan->is_canonical);
3031
3032 return chan->is_canonical(chan);
3033}
3034
3035/**
3036 * Test incoming flag.
3037 *
3038 * This function gets the incoming flag; this is set when a listener spawns
3039 * a channel. If this returns true the channel was remotely initiated.
3040 */
3041int
3043{
3044 tor_assert(chan);
3045
3046 return chan->is_incoming;
3047}
3048
3049/**
3050 * Set the incoming flag.
3051 *
3052 * This function is called when a channel arrives on a listening channel
3053 * to mark it as incoming.
3054 */
3055void
3057{
3058 tor_assert(chan);
3059
3060 chan->is_incoming = 1;
3061}
3062
3063/**
3064 * Test local flag.
3065 *
3066 * This function gets the local flag; the lower layer should set this when
3067 * setting up the channel if is_local_addr() is true for all of the
3068 * destinations it will communicate with on behalf of this channel. It's
3069 * used to decide whether to declare the network reachable when seeing incoming
3070 * traffic on the channel.
3071 */
3072int
3074{
3075 tor_assert(chan);
3076
3077 return chan->is_local;
3078}
3079
3080/**
3081 * Set the local flag.
3082 *
3083 * This internal-only function should be called by the lower layer if the
3084 * channel is to a local address. See channel_is_local() above or the
3085 * description of the is_local bit in channel.h.
3086 */
3087void
3089{
3090 tor_assert(chan);
3091
3092 chan->is_local = 1;
3093}
3094
3095/**
3096 * Mark a channel as remote.
3097 *
3098 * This internal-only function should be called by the lower layer if the
3099 * channel is not to a local address but has previously been marked local.
3100 * See channel_is_local() above or the description of the is_local bit in
3101 * channel.h
3102 */
3103void
3105{
3106 tor_assert(chan);
3107
3108 chan->is_local = 0;
3109}
3110
3111/**
3112 * Test outgoing flag.
3113 *
3114 * This function gets the outgoing flag; this is the inverse of the incoming
3115 * bit set when a listener spawns a channel. If this returns true the channel
3116 * was locally initiated.
3117 */
3118int
3120{
3121 tor_assert(chan);
3122
3123 return !(chan->is_incoming);
3124}
3125
3126/**
3127 * Mark a channel as outgoing.
3128 *
3129 * This function clears the incoming flag and thus marks a channel as
3130 * outgoing.
3131 */
3132void
3134{
3135 tor_assert(chan);
3136
3137 chan->is_incoming = 0;
3138}
3139
3140/************************
3141 * Flow control queries *
3142 ***********************/
3143
3144/**
3145 * Estimate the number of writeable cells.
3146 *
3147 * Ask the lower layer for an estimate of how many cells it can accept.
3148 */
3149int
3151{
3152 int result;
3153
3154 tor_assert(chan);
3155 tor_assert(chan->num_cells_writeable);
3156
3157 if (chan->state == CHANNEL_STATE_OPEN) {
3158 /* Query lower layer */
3159 result = chan->num_cells_writeable(chan);
3160 if (result < 0) result = 0;
3161 } else {
3162 /* No cells are writeable in any other state */
3163 result = 0;
3164 }
3165
3166 return result;
3167}
3168
3169/*********************
3170 * Timestamp updates *
3171 ********************/
3172
3173/**
3174 * Update the created timestamp for a channel.
3175 *
3176 * This updates the channel's created timestamp and should only be called
3177 * from channel_init().
3178 */
3179void
3181{
3182 time_t now = time(NULL);
3183
3184 tor_assert(chan);
3185
3186 chan->timestamp_created = now;
3187}
3188
3189/**
3190 * Update the created timestamp for a channel listener.
3191 *
3192 * This updates the channel listener's created timestamp and should only be
3193 * called from channel_init_listener().
3194 */
3195void
3197{
3198 time_t now = time(NULL);
3199
3200 tor_assert(chan_l);
3201
3202 chan_l->timestamp_created = now;
3203}
3204
3205/**
3206 * Update the last active timestamp for a channel.
3207 *
3208 * This function updates the channel's last active timestamp; it should be
3209 * called by the lower layer whenever there is activity on the channel which
3210 * does not lead to a cell being transmitted or received; the active timestamp
3211 * is also updated from channel_timestamp_recv() and channel_timestamp_xmit(),
3212 * but it should be updated for things like the v3 handshake and stuff that
3213 * produce activity only visible to the lower layer.
3214 */
3215void
3217{
3218 time_t now = time(NULL);
3219
3220 tor_assert(chan);
3221 monotime_coarse_get(&chan->timestamp_xfer);
3222
3223 chan->timestamp_active = now;
3224
3225 /* Clear any potential netflow padding timer. We're active */
3226 monotime_coarse_zero(&chan->next_padding_time);
3227}
3228
3229/**
3230 * Update the last active timestamp for a channel listener.
3231 */
3232void
3234{
3235 time_t now = time(NULL);
3236
3237 tor_assert(chan_l);
3238
3239 chan_l->timestamp_active = now;
3240}
3241
3242/**
3243 * Update the last accepted timestamp.
3244 *
3245 * This function updates the channel listener's last accepted timestamp; it
3246 * should be called whenever a new incoming channel is accepted on a
3247 * listener.
3248 */
3249void
3251{
3252 time_t now = time(NULL);
3253
3254 tor_assert(chan_l);
3255
3256 chan_l->timestamp_active = now;
3257 chan_l->timestamp_accepted = now;
3258}
3259
3260/**
3261 * Update client timestamp.
3262 *
3263 * This function is called by relay.c to timestamp a channel that appears to
3264 * be used as a client.
3265 */
3266void
3268{
3269 time_t now = time(NULL);
3270
3271 tor_assert(chan);
3272
3273 chan->timestamp_client = now;
3274}
3275
3276/**
3277 * Update the recv timestamp.
3278 *
3279 * This is called whenever we get an incoming cell from the lower layer.
3280 * This also updates the active timestamp.
3281 */
3282void
3284{
3285 time_t now = time(NULL);
3286 tor_assert(chan);
3287 monotime_coarse_get(&chan->timestamp_xfer);
3288
3289 chan->timestamp_active = now;
3290 chan->timestamp_recv = now;
3291
3292 /* Clear any potential netflow padding timer. We're active */
3293 monotime_coarse_zero(&chan->next_padding_time);
3294}
3295
3296/**
3297 * Update the xmit timestamp.
3298 *
3299 * This is called whenever we pass an outgoing cell to the lower layer. This
3300 * also updates the active timestamp.
3301 */
3302void
3304{
3305 time_t now = time(NULL);
3306 tor_assert(chan);
3307
3308 monotime_coarse_get(&chan->timestamp_xfer);
3309
3310 chan->timestamp_active = now;
3311 chan->timestamp_xmit = now;
3312
3313 /* Clear any potential netflow padding timer. We're active */
3314 monotime_coarse_zero(&chan->next_padding_time);
3315}
3316
3317/***************************************************************
3318 * Timestamp queries - see above for definitions of timestamps *
3319 **************************************************************/
3320
3321/**
3322 * Query created timestamp for a channel.
3323 */
3324time_t
3326{
3327 tor_assert(chan);
3328
3329 return chan->timestamp_created;
3330}
3331
3332/**
3333 * Query client timestamp.
3334 */
3335time_t
3337{
3338 tor_assert(chan);
3339
3340 return chan->timestamp_client;
3341}
3342
3343/**
3344 * Query xmit timestamp.
3345 */
3346time_t
3348{
3349 tor_assert(chan);
3350
3351 return chan->timestamp_xmit;
3352}
3353
3354/**
3355 * Check if a channel matches an extend_info_t.
3356 *
3357 * This function calls the lower layer and asks if this channel matches a
3358 * given extend_info_t.
3359 *
3360 * NOTE that this function only checks for an address/port match, and should
3361 * be used only when no identity is available.
3362 */
3363int
3365{
3366 tor_assert(chan);
3368 tor_assert(extend_info);
3369
3370 return chan->matches_extend_info(chan, extend_info);
3371}
3372
3373/**
3374 * Check if a channel matches the given target IPv4 or IPv6 addresses.
3375 * If either address matches, return true. If neither address matches,
3376 * return false.
3377 *
3378 * Both addresses can't be NULL.
3379 *
3380 * This function calls into the lower layer and asks if this channel thinks
3381 * it matches the target addresses for circuit extension purposes.
3382 */
3383STATIC bool
3385 const tor_addr_t *target_ipv4_addr,
3386 const tor_addr_t *target_ipv6_addr)
3387{
3388 tor_assert(chan);
3390
3391 IF_BUG_ONCE(!target_ipv4_addr && !target_ipv6_addr)
3392 return false;
3393
3394 if (target_ipv4_addr && chan->matches_target(chan, target_ipv4_addr))
3395 return true;
3396
3397 if (target_ipv6_addr && chan->matches_target(chan, target_ipv6_addr))
3398 return true;
3399
3400 return false;
3401}
3402
3403/**
3404 * Return the total number of circuits used by a channel.
3405 *
3406 * @param chan Channel to query
3407 * @return Number of circuits using this as n_chan or p_chan
3408 */
3409unsigned int
3411{
3412 tor_assert(chan);
3413
3414 return chan->num_n_circuits +
3415 chan->num_p_circuits;
3416}
3417
3418/**
3419 * Set up circuit ID generation.
3420 *
3421 * This is called when setting up a channel and replaces the old
3422 * connection_or_set_circid_type().
3423 */
3424MOCK_IMPL(void,
3426 crypto_pk_t *identity_rcvd,
3427 int consider_identity))
3428{
3429 int started_here;
3430 crypto_pk_t *our_identity;
3431
3432 tor_assert(chan);
3433
3434 started_here = channel_is_outgoing(chan);
3435
3436 if (! consider_identity) {
3437 if (started_here)
3439 else
3441 return;
3442 }
3443
3444 our_identity = started_here ?
3445 get_tlsclient_identity_key() : get_server_identity_key();
3446
3447 if (identity_rcvd) {
3448 if (crypto_pk_cmp_keys(our_identity, identity_rcvd) < 0) {
3450 } else {
3452 }
3453 } else {
3455 }
3456}
3457
3458static int
3459channel_sort_by_ed25519_identity(const void **a_, const void **b_)
3460{
3461 const channel_t *a = *a_,
3462 *b = *b_;
3463 return fast_memcmp(&a->ed25519_identity.pubkey,
3464 &b->ed25519_identity.pubkey,
3465 sizeof(a->ed25519_identity.pubkey));
3466}
3467
3468/** Helper for channel_update_bad_for_new_circs(): Perform the
3469 * channel_update_bad_for_new_circs operation on all channels in <b>lst</b>,
3470 * all of which MUST have the same RSA ID. (They MAY have different
3471 * Ed25519 IDs.) */
3472static void
3473channel_rsa_id_group_set_badness(struct channel_list_t *lst, int force)
3474{
3475 /*XXXX This function should really be about channels. 15056 */
3476 channel_t *chan = TOR_LIST_FIRST(lst);
3477
3478 if (!chan)
3479 return;
3480
3481 /* if there is only one channel, don't bother looping */
3482 if (PREDICT_LIKELY(!TOR_LIST_NEXT(chan, next_with_same_id))) {
3484 time(NULL), BASE_CHAN_TO_TLS(chan)->conn, force);
3485 return;
3486 }
3487
3488 smartlist_t *channels = smartlist_new();
3489
3490 TOR_LIST_FOREACH(chan, lst, next_with_same_id) {
3491 if (BASE_CHAN_TO_TLS(chan)->conn) {
3492 smartlist_add(channels, chan);
3493 }
3494 }
3495
3496 smartlist_sort(channels, channel_sort_by_ed25519_identity);
3497
3498 const ed25519_public_key_t *common_ed25519_identity = NULL;
3499 /* it would be more efficient to do a slice, but this case is rare */
3500 smartlist_t *or_conns = smartlist_new();
3501 SMARTLIST_FOREACH_BEGIN(channels, channel_t *, channel) {
3502 tor_assert(channel); // Suppresses some compiler warnings.
3503
3504 if (!common_ed25519_identity)
3505 common_ed25519_identity = &channel->ed25519_identity;
3506
3507 if (! ed25519_pubkey_eq(&channel->ed25519_identity,
3508 common_ed25519_identity)) {
3509 connection_or_group_set_badness_(or_conns, force);
3510 smartlist_clear(or_conns);
3511 common_ed25519_identity = &channel->ed25519_identity;
3512 }
3513
3514 smartlist_add(or_conns, BASE_CHAN_TO_TLS(channel)->conn);
3515 } SMARTLIST_FOREACH_END(channel);
3516
3517 connection_or_group_set_badness_(or_conns, force);
3518
3519 /* XXXX 15056 we may want to do something special with connections that have
3520 * no set Ed25519 identity! */
3521
3522 smartlist_free(or_conns);
3523 smartlist_free(channels);
3524}
3525
3526/** Go through all the channels (or if <b>digest</b> is non-NULL, just
3527 * the OR connections with that digest), and set the is_bad_for_new_circs
3528 * flag based on the rules in connection_or_group_set_badness() (or just
3529 * always set it if <b>force</b> is true).
3530 */
3531void
3532channel_update_bad_for_new_circs(const char *digest, int force)
3533{
3534 if (digest) {
3535 channel_idmap_entry_t *ent;
3536 channel_idmap_entry_t search;
3537 memset(&search, 0, sizeof(search));
3538 memcpy(search.digest, digest, DIGEST_LEN);
3539 ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
3540 if (ent) {
3541 channel_rsa_id_group_set_badness(&ent->channel_list, force);
3542 }
3543 return;
3544 }
3545
3546 /* no digest; just look at everything. */
3547 channel_idmap_entry_t **iter;
3548 HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
3549 channel_rsa_id_group_set_badness(&(*iter)->channel_list, force);
3550 }
3551}
void tor_addr_make_unspec(tor_addr_t *a)
Definition address.c:225
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition address.c:1164
const char * hex_str(const char *from, size_t fromlen)
Definition binascii.c:34
static uint16_t get_uint16(const void *cp)
Definition bytes.h:42
static uint32_t get_uint32(const void *cp)
Definition bytes.h:54
Cell queue structures.
int channel_has_queued_writes(channel_t *chan)
Definition channel.c:2943
void channel_timestamp_active(channel_t *chan)
Definition channel.c:3216
void channel_set_circid_type(channel_t *chan, crypto_pk_t *identity_rcvd, int consider_identity)
Definition channel.c:3427
int channel_is_better(channel_t *a, channel_t *b)
Definition channel.c:2413
int channel_is_bad_for_new_circs(channel_t *chan)
Definition channel.c:2959
static void channel_remove_from_digest_map(channel_t *chan)
Definition channel.c:599
int channel_is_outgoing(channel_t *chan)
Definition channel.c:3119
void channel_listener_process_incoming(channel_listener_t *listener)
Definition channel.c:1875
void channel_timestamp_recv(channel_t *chan)
Definition channel.c:3283
void channel_run_cleanup(void)
Definition channel.c:2202
void channel_update_bad_for_new_circs(const char *digest, int force)
Definition channel.c:3532
void channel_dumpstats(int severity)
Definition channel.c:2140
void channel_timestamp_client(channel_t *chan)
Definition channel.c:3267
void channel_set_identity_digest(channel_t *chan, const char *identity_digest, const ed25519_public_key_t *ed_identity)
Definition channel.c:1391
void channel_listener_unregister(channel_listener_t *chan_l)
Definition channel.c:526
void channel_listener_dump_statistics(channel_listener_t *chan_l, int severity)
Definition channel.c:2813
void channel_mark_local(channel_t *chan)
Definition channel.c:3088
void channel_set_cell_handlers(channel_t *chan, channel_cell_handler_fn_ptr cell_handler)
Definition channel.c:1115
void channel_mark_client(channel_t *chan)
Definition channel.c:3000
time_t channel_when_last_xmit(channel_t *chan)
Definition channel.c:3347
void channel_listener_run_cleanup(void)
Definition channel.c:2228
static void channel_force_xfree(channel_t *chan)
Definition channel.c:991
void channel_mark_incoming(channel_t *chan)
Definition channel.c:3056
void channel_closed(channel_t *chan)
Definition channel.c:1326
void channel_note_establishment_failure(channel_t *chan)
Definition channel.c:1304
void channel_init_listener(channel_listener_t *chan_l)
Definition channel.c:892
int channel_listener_state_can_transition(channel_listener_state_t from, channel_listener_state_t to)
Definition channel.c:285
STATIC void channel_add_to_digest_map(channel_t *chan)
Definition channel.c:562
int channel_state_is_valid(channel_state_t state)
Definition channel.c:187
channel_t * channel_get_for_extend(const char *rsa_id_digest, const ed25519_public_key_t *ed_id, const tor_addr_t *target_ipv4_addr, const tor_addr_t *target_ipv6_addr, bool for_origin_circ, const char **msg_out, int *launch_out)
Definition channel.c:2485
void channel_do_open_actions(channel_t *chan)
Definition channel.c:1927
void channel_listener_mark_for_close(channel_listener_t *chan_l)
Definition channel.c:1189
void channel_close_from_lower_layer(channel_t *chan)
Definition channel.c:1229
void channel_check_for_duplicates(void)
Definition channel.c:750
void channel_process_cell(channel_t *chan, cell_t *cell)
Definition channel.c:2045
void channel_change_state_open(channel_t *chan)
Definition channel.c:1690
void channel_listener_queue_incoming(channel_listener_t *listener, channel_t *incoming)
Definition channel.c:1994
int channel_is_canonical(channel_t *chan)
Definition channel.c:3027
int channel_listener_state_is_valid(channel_listener_state_t state)
Definition channel.c:212
const char * channel_state_to_string(channel_state_t state)
Definition channel.c:317
void channel_timestamp_created(channel_t *chan)
Definition channel.c:3180
static void channel_free_list(smartlist_t *channels, int mark_for_close)
Definition channel.c:2251
int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
Definition channel.c:3364
int channel_state_can_transition(channel_state_t from, channel_state_t to)
Definition channel.c:239
const char * channel_describe_transport(channel_t *chan)
Definition channel.c:2584
int channel_remote_identity_matches(const channel_t *chan, const char *rsa_id_digest, const ed25519_public_key_t *ed_id)
Definition channel.c:669
void channel_listener_set_listener_fn(channel_listener_t *chan_l, channel_listener_fn_ptr listener)
Definition channel.c:1075
void channel_register(channel_t *chan)
Definition channel.c:388
time_t channel_when_last_client(channel_t *chan)
Definition channel.c:3336
void channel_listener_timestamp_created(channel_listener_t *chan_l)
Definition channel.c:3196
void channel_listener_timestamp_active(channel_listener_t *chan_l)
Definition channel.c:3233
void channel_listener_register(channel_listener_t *chan_l)
Definition channel.c:485
const char * channel_listener_describe_transport(channel_listener_t *chan_l)
Definition channel.c:2599
void channel_listener_dump_transport_statistics(channel_listener_t *chan_l, int severity)
Definition channel.c:2892
static void channel_change_state_(channel_t *chan, channel_state_t to_state)
Definition channel.c:1579
int channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
Definition channel.c:2100
void channel_mark_bad_for_new_circs(channel_t *chan)
Definition channel.c:2972
int channel_is_incoming(channel_t *chan)
Definition channel.c:3042
int channel_is_client(const channel_t *chan)
Definition channel.c:2987
const char * channel_listener_state_to_string(channel_listener_state_t state)
Definition channel.c:352
channel_t * channel_find_by_remote_identity(const char *rsa_id_digest, const ed25519_public_key_t *ed_id)
Definition channel.c:699
void channel_mark_remote(channel_t *chan)
Definition channel.c:3104
void channel_unregister(channel_t *chan)
Definition channel.c:446
static void channel_rsa_id_group_set_badness(struct channel_list_t *lst, int force)
Definition channel.c:3473
channel_cell_handler_fn_ptr channel_get_cell_handler(channel_t *chan)
Definition channel.c:1098
ssize_t channel_flush_some_cells(channel_t *chan, ssize_t num_cells)
Definition channel.c:1798
static void channel_listener_free_list(smartlist_t *channels, int mark_for_close)
Definition channel.c:2282
int packed_cell_is_destroy(channel_t *chan, const packed_cell_t *packed_cell, circid_t *circid_out)
Definition channel.c:2074
STATIC bool channel_matches_target_addr_for_extend(channel_t *chan, const tor_addr_t *target_ipv4_addr, const tor_addr_t *target_ipv6_addr)
Definition channel.c:3384
void channel_close_for_error(channel_t *chan)
Definition channel.c:1257
void channel_listener_free_(channel_listener_t *chan_l)
Definition channel.c:964
void channel_listener_dumpstats(int severity)
Definition channel.c:2171
static int write_packed_cell(channel_t *chan, packed_cell_t *cell)
Definition channel.c:1480
int channel_is_local(channel_t *chan)
Definition channel.c:3073
void channel_listener_timestamp_accepted(channel_listener_t *chan_l)
Definition channel.c:3250
void channel_notify_flushed(channel_t *chan)
Definition channel.c:1858
int channel_num_cells_writeable(channel_t *chan)
Definition channel.c:3150
channel_t * channel_find_by_global_id(uint64_t global_identifier)
Definition channel.c:652
void channel_timestamp_xmit(channel_t *chan)
Definition channel.c:3303
int channel_get_addr_if_possible(const channel_t *chan, tor_addr_t *addr_out)
Definition channel.c:2929
channel_t * channel_next_with_rsa_identity(channel_t *chan)
Definition channel.c:733
const char * channel_describe_peer(channel_t *chan)
Definition channel.c:2909
channel_t * channel_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, const struct circuit_guard_state_t *guard_state, bool for_origin_circ)
Definition channel.c:2392
void channel_mark_for_close(channel_t *chan)
Definition channel.c:1150
void channel_clear_identity_digest(channel_t *chan)
Definition channel.c:1362
void channel_clear_remote_end(channel_t *chan)
Definition channel.c:1450
void channel_listener_change_state(channel_listener_t *chan_l, channel_listener_state_t to_state)
Definition channel.c:1707
void channel_mark_outgoing(channel_t *chan)
Definition channel.c:3133
static void channel_listener_force_xfree(channel_listener_t *chan_l)
Definition channel.c:1038
void channel_dump_statistics(channel_t *chan, int severity)
Definition channel.c:2613
void channel_free_all(void)
Definition channel.c:2315
int channel_more_to_flush(channel_t *chan)
Definition channel.c:1841
void channel_dump_transport_statistics(channel_t *chan, int severity)
Definition channel.c:2879
unsigned int channel_num_circuits(channel_t *chan)
Definition channel.c:3410
void channel_clear_client(channel_t *chan)
Definition channel.c:3013
int channel_write_packed_cell(channel_t *chan, packed_cell_t *cell)
Definition channel.c:1544
time_t channel_when_created(channel_t *chan)
Definition channel.c:3325
void channel_init(channel_t *chan)
Definition channel.c:853
void channel_note_establishment_cancelled(channel_t *chan)
Definition channel.c:1290
void channel_free_(channel_t *chan)
Definition channel.c:908
void channel_change_state(channel_t *chan, channel_state_t to_state)
Definition channel.c:1680
Header file for channel.c.
void channel_mark_as_used_for_origin_circuit(channel_t *chan)
Definition channeltls.c:418
channel_state_t
Definition channel.h:50
@ CHANNEL_STATE_OPEN
Definition channel.h:82
@ CHANNEL_STATE_CLOSED
Definition channel.h:59
@ CHANNEL_STATE_MAINT
Definition channel.h:94
@ CHANNEL_STATE_OPENING
Definition channel.h:70
@ CHANNEL_STATE_CLOSING
Definition channel.h:106
@ CHANNEL_STATE_ERROR
Definition channel.h:118
@ CHANNEL_STATE_LAST
Definition channel.h:122
@ CIRC_ID_TYPE_NEITHER
Definition channel.h:44
@ CIRC_ID_TYPE_LOWER
Definition channel.h:40
@ CIRC_ID_TYPE_HIGHER
Definition channel.h:41
channel_listener_state_t
Definition channel.h:127
@ CHANNEL_LISTENER_STATE_ERROR
Definition channel.h:167
@ CHANNEL_LISTENER_STATE_LAST
Definition channel.h:171
@ CHANNEL_LISTENER_STATE_LISTENING
Definition channel.h:147
@ CHANNEL_LISTENER_STATE_CLOSING
Definition channel.h:157
@ CHANNEL_LISTENER_STATE_CLOSED
Definition channel.h:136
void channelpadding_disable_padding_on_channel(channel_t *chan)
void channelpadding_reduce_padding_on_channel(channel_t *chan)
channel_t * channel_tls_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, const struct circuit_guard_state_t *guard_state, bool for_origin_circ)
Definition channeltls.c:194
Header file for channeltls.c.
void circuit_n_chan_done(channel_t *chan, int status)
Header file for circuitbuild.c.
void channel_note_destroy_pending(channel_t *chan, circid_t id)
void channel_note_destroy_not_pending(channel_t *chan, circid_t id)
void circuit_unlink_all_from_channel(channel_t *chan, int reason)
Header file for circuitlist.c.
void circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan)
Definition circuitmux.c:324
void circuitmux_detach_all_circuits(circuitmux_t *cmux, smartlist_t *detached_out)
Definition circuitmux.c:214
void circuitmux_set_policy(circuitmux_t *cmux, const circuitmux_policy_t *pol)
Definition circuitmux.c:428
unsigned int circuitmux_num_active_circuits(circuitmux_t *cmux)
Definition circuitmux.c:702
unsigned int circuitmux_num_circuits(circuitmux_t *cmux)
Definition circuitmux.c:714
unsigned int circuitmux_num_cells(circuitmux_t *cmux)
Definition circuitmux.c:690
Header file for circuitmux.c.
circuit_build_times_t * get_circuit_build_times_mutable(void)
void circuit_build_times_network_is_live(circuit_build_times_t *cbt)
Header file for circuitstats.c.
Functions and types for monotonic times.
const or_options_t * get_options(void)
Definition config.c:949
tor_cmdline_mode_t command
Definition config.c:2481
Header file for config.c.
int connection_or_single_set_badness_(time_t now, or_connection_t *or_conn, int force)
int connection_or_digest_is_known_relay(const char *id_digest)
void connection_or_group_set_badness_(smartlist_t *group, int force)
Header file for connection_or.c.
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b)
int tor_memeq(const void *a, const void *b, size_t sz)
Definition di_ops.c:107
#define fast_memcmp(a, b, c)
Definition di_ops.h:28
#define tor_memneq(a, b, sz)
Definition di_ops.h:21
#define DIGEST_LEN
Header file for dirlist.c.
void entry_guard_connection_failed(struct entry_guard_handle_t *handle)
void entry_guard_handle_release(struct entry_guard_handle_t *handle)
Header file for circuitbuild.c.
Header file for geoip_stats.c.
@ DIRREQ_CHANNEL_BUFFER_FLUSHED
Definition geoip_stats.h:76
@ GEOIP_CLIENT_CONNECT
Definition geoip_stats.h:24
Header file containing service data for the HS subsystem.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition log.c:591
#define LD_CHANNEL
Definition log.h:105
#define LD_OR
Definition log.h:92
#define LD_BUG
Definition log.h:86
#define LD_GENERAL
Definition log.h:62
void mainloop_schedule_postloop_cleanup(void)
Definition mainloop.c:1658
Header file for mainloop.c.
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Definition malloc.c:146
void tor_free_(void *mem)
Definition malloc.c:227
#define tor_free(p)
Definition malloc.h:56
int net_is_disabled(void)
Definition netstatus.c:25
Header for netstatus.c.
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
void router_set_status(const char *digest, int up)
Definition nodelist.c:2427
Header file for nodelist.c.
Master header file for Tor-specific functionality.
uint32_t circid_t
Definition or.h:588
OR connection structure.
int channel_flush_from_first_active_circuit(channel_t *chan, int max)
Definition relay.c:3177
uint8_t packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids)
Definition relay.c:3152
Header file for relay.c.
void rep_hist_padding_count_write(padding_type_t type)
Definition rephist.c:2816
Header file for rephist.c.
@ PADDING_TYPE_ENABLED_CELL
Definition rephist.h:158
@ PADDING_TYPE_TOTAL
Definition rephist.h:154
@ PADDING_TYPE_ENABLED_TOTAL
Definition rephist.h:156
@ PADDING_TYPE_CELL
Definition rephist.h:152
crypto_pk_t * get_tlsclient_identity_key(void)
Definition router.c:457
Header file for router.c.
Header file for routerlist.c.
void scheduler_channel_doesnt_want_writes(channel_t *chan)
Definition scheduler.c:512
Header file for scheduler*.c.
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition smartlist.c:334
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
void smartlist_remove(smartlist_t *sl, const void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define SMARTLIST_DEL_CURRENT(sl, var)
channel_listener_state_t state
Definition channel.h:469
const char *(* describe_transport)(channel_listener_t *)
Definition channel.h:499
enum channel_listener_t::@11 reason_for_closing
void(* dumpstats)(channel_listener_t *, int)
Definition channel.h:501
uint64_t global_identifier
Definition channel.h:474
void(* free_fn)(channel_listener_t *)
Definition channel.h:495
unsigned char registered
Definition channel.h:477
smartlist_t * incoming_list
Definition channel.h:507
uint64_t n_accepted
Definition channel.h:513
time_t timestamp_created
Definition channel.h:489
channel_listener_fn_ptr listener
Definition channel.h:504
time_t timestamp_accepted
Definition channel.h:510
void(* close)(channel_listener_t *)
Definition channel.h:497
unsigned int is_local
Definition channel.h:440
void(* free_fn)(channel_t *)
Definition channel.h:317
channel_state_t state
Definition channel.h:193
int sched_heap_idx
Definition channel.h:296
int(* is_canonical)(channel_t *)
Definition channel.h:354
void(* close)(channel_t *)
Definition channel.h:319
monotime_coarse_t next_padding_time
Definition channel.h:233
void(* dumpstats)(channel_t *, int)
Definition channel.h:323
enum channel_t::@9 reason_for_closing
time_t timestamp_last_had_circuits
Definition channel.h:454
unsigned int num_n_circuits
Definition channel.h:411
int(* matches_extend_info)(channel_t *, extend_info_t *)
Definition channel.h:356
circ_id_type_bitfield_t circ_id_type
Definition channel.h:406
uint64_t dirreq_id
Definition channel.h:459
unsigned int padding_enabled
Definition channel.h:215
struct entry_guard_handle_t * establishment_guard
Definition channel.h:433
char identity_digest[DIGEST_LEN]
Definition channel.h:379
uint64_t n_cells_xmitted
Definition channel.h:464
uint64_t n_cells_recved
Definition channel.h:462
uint64_t global_identifier
Definition channel.h:198
int(* write_packed_cell)(channel_t *, packed_cell_t *)
Definition channel.h:366
struct channel_handle_t * timer_handle
Definition channel.h:238
unsigned char registered
Definition channel.h:201
time_t timestamp_client
Definition channel.h:447
unsigned int is_incoming
Definition channel.h:428
const char *(* describe_transport)(channel_t *)
Definition channel.h:321
time_t timestamp_xmit
Definition channel.h:449
tor_addr_t addr_according_to_peer
Definition channel.h:241
channel_cell_handler_fn_ptr cell_handler
Definition channel.h:326
int(* matches_target)(channel_t *, const tor_addr_t *)
Definition channel.h:358
time_t timestamp_recv
Definition channel.h:448
const char *(* describe_peer)(const channel_t *)
Definition channel.h:347
struct ed25519_public_key_t ed25519_identity
Definition channel.h:389
time_t timestamp_created
Definition channel.h:299
unsigned int has_been_open
Definition channel.h:204
monotime_coarse_t timestamp_xfer
Definition channel.h:312
unsigned int is_client
Definition channel.h:425
unsigned int is_bad_for_new_circs
Definition channel.h:420
unsigned int is_canonical_to_peer
Definition channel.h:225
enum channel_t::@10 scheduler_state
circuitmux_t * cmux
Definition channel.h:398
struct tor_timer_t * padding_timer
Definition channel.h:236
ratelim_t last_warned_circ_ids_exhausted
Definition channel.h:444
int(* has_queued_writes)(channel_t *)
Definition channel.h:349
char body[CELL_MAX_NETWORK_SIZE]
int rate
Definition ratelim.h:44
#define STATIC
Definition testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Header for timers.c.
#define tor_assert(expr)
Definition util_bug.h:103
#define IF_BUG_ONCE(cond)
Definition util_bug.h:254
int tor_digest_is_zero(const char *digest)
Definition util_string.c:98
#define ED25519_PUBKEY_LEN