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