1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2012, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Manage the global circuit list.
14 #include "circuitbuild.h"
15 #include "circuitlist.h"
16 #include "circuituse.h"
17 #include "circuitstats.h"
18 #include "connection.h"
20 #include "connection_edge.h"
21 #include "connection_or.h"
23 #include "networkstatus.h"
27 #include "rendclient.h"
28 #include "rendcommon.h"
30 #include "routerlist.h"
31 #include "routerset.h"
34 /********* START VARIABLES **********/
36 /** A global list of all circuits at this hop. */
37 circuit_t
*global_circuitlist
=NULL
;
39 /** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
40 static smartlist_t
*circuits_pending_chans
= NULL
;
42 static void circuit_free(circuit_t
*circ
);
43 static void circuit_free_cpath(crypt_path_t
*cpath
);
44 static void circuit_free_cpath_node(crypt_path_t
*victim
);
45 static void cpath_ref_decref(crypt_path_reference_t
*cpath_ref
);
47 /********* END VARIABLES ************/
49 /** A map from channel and circuit ID to circuit. (Lookup performance is
50 * very important here, since we need to do it every time a cell arrives.) */
51 typedef struct chan_circid_circuit_map_t
{
52 HT_ENTRY(chan_circid_circuit_map_t
) node
;
56 } chan_circid_circuit_map_t
;
58 /** Helper for hash tables: compare the channel and circuit ID for a and
59 * b, and return less than, equal to, or greater than zero appropriately.
62 chan_circid_entries_eq_(chan_circid_circuit_map_t
*a
,
63 chan_circid_circuit_map_t
*b
)
65 return a
->chan
== b
->chan
&& a
->circ_id
== b
->circ_id
;
68 /** Helper: return a hash based on circuit ID and the pointer value of
69 * chan in <b>a</b>. */
70 static INLINE
unsigned int
71 chan_circid_entry_hash_(chan_circid_circuit_map_t
*a
)
73 return (((unsigned)a
->circ_id
)<<8) ^ (unsigned)(uintptr_t)(a
->chan
);
76 /** Map from [chan,circid] to circuit. */
77 static HT_HEAD(chan_circid_map
, chan_circid_circuit_map_t
)
78 chan_circid_map
= HT_INITIALIZER();
79 HT_PROTOTYPE(chan_circid_map
, chan_circid_circuit_map_t
, node
,
80 chan_circid_entry_hash_
, chan_circid_entries_eq_
)
81 HT_GENERATE(chan_circid_map
, chan_circid_circuit_map_t
, node
,
82 chan_circid_entry_hash_
, chan_circid_entries_eq_
, 0.6,
83 malloc
, realloc
, free
)
85 /** The most recently returned entry from circuit_get_by_circid_chan;
86 * used to improve performance when many cells arrive in a row from the
89 chan_circid_circuit_map_t
*_last_circid_chan_ent
= NULL
;
91 /** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
92 * and/or channel for circ has just changed from <b>old_chan, old_id</b>
93 * to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
94 * the old entry (if any) and adding a new one. */
96 circuit_set_circid_chan_helper(circuit_t
*circ
, int direction
,
100 chan_circid_circuit_map_t search
;
101 chan_circid_circuit_map_t
*found
;
102 channel_t
*old_chan
, **chan_ptr
;
103 circid_t old_id
, *circid_ptr
;
104 int make_active
, attached
= 0;
106 if (direction
== CELL_DIRECTION_OUT
) {
107 chan_ptr
= &circ
->n_chan
;
108 circid_ptr
= &circ
->n_circ_id
;
109 make_active
= circ
->n_chan_cells
.n
> 0;
111 or_circuit_t
*c
= TO_OR_CIRCUIT(circ
);
112 chan_ptr
= &c
->p_chan
;
113 circid_ptr
= &c
->p_circ_id
;
114 make_active
= c
->p_chan_cells
.n
> 0;
116 old_chan
= *chan_ptr
;
117 old_id
= *circid_ptr
;
119 if (id
== old_id
&& chan
== old_chan
)
122 if (_last_circid_chan_ent
&&
123 ((old_id
== _last_circid_chan_ent
->circ_id
&&
124 old_chan
== _last_circid_chan_ent
->chan
) ||
125 (id
== _last_circid_chan_ent
->circ_id
&&
126 chan
== _last_circid_chan_ent
->chan
))) {
127 _last_circid_chan_ent
= NULL
;
132 * If we're changing channels or ID and had an old channel and a non
133 * zero old ID and weren't marked for close (i.e., we should have been
134 * attached), detach the circuit. ID changes require this because
135 * circuitmux hashes on (channel_id, circuit_id).
137 if (old_id
!= 0 && (old_chan
!= chan
|| old_id
!= id
) &&
138 !(circ
->marked_for_close
)) {
139 tor_assert(old_chan
->cmux
);
140 circuitmux_detach_circuit(old_chan
->cmux
, circ
);
143 /* we may need to remove it from the conn-circid map */
144 search
.circ_id
= old_id
;
145 search
.chan
= old_chan
;
146 found
= HT_REMOVE(chan_circid_map
, &chan_circid_map
, &search
);
149 if (direction
== CELL_DIRECTION_OUT
) {
150 /* One fewer circuits use old_chan as n_chan */
151 --(old_chan
->num_n_circuits
);
153 /* One fewer circuits use old_chan as p_chan */
154 --(old_chan
->num_p_circuits
);
159 /* Change the values only after we have possibly made the circuit inactive
160 * on the previous chan. */
167 /* now add the new one to the conn-circid map */
170 found
= HT_FIND(chan_circid_map
, &chan_circid_map
, &search
);
172 found
->circuit
= circ
;
174 found
= tor_malloc_zero(sizeof(chan_circid_circuit_map_t
));
177 found
->circuit
= circ
;
178 HT_INSERT(chan_circid_map
, &chan_circid_map
, found
);
182 * Attach to the circuitmux if we're changing channels or IDs and
183 * have a new channel and ID to use and the circuit is not marked for
186 if (chan
&& id
!= 0 && (old_chan
!= chan
|| old_id
!= id
) &&
187 !(circ
->marked_for_close
)) {
188 tor_assert(chan
->cmux
);
189 circuitmux_attach_circuit(chan
->cmux
, circ
, direction
);
194 * This is a no-op if we have no cells, but if we do it marks us active to
197 if (make_active
&& attached
)
198 update_circuit_on_cmux(circ
, direction
);
200 /* Adjust circuit counts on new channel */
201 if (direction
== CELL_DIRECTION_OUT
) {
202 ++chan
->num_n_circuits
;
204 ++chan
->num_p_circuits
;
208 /** Set the p_conn field of a circuit <b>circ</b>, along
209 * with the corresponding circuit ID, and add the circuit as appropriate
210 * to the (chan,id)-\>circuit map. */
212 circuit_set_p_circid_chan(or_circuit_t
*circ
, circid_t id
,
215 circuit_set_circid_chan_helper(TO_CIRCUIT(circ
), CELL_DIRECTION_IN
,
219 tor_assert(bool_eq(circ
->p_chan_cells
.n
, circ
->next_active_on_p_chan
));
222 /** Set the n_conn field of a circuit <b>circ</b>, along
223 * with the corresponding circuit ID, and add the circuit as appropriate
224 * to the (chan,id)-\>circuit map. */
226 circuit_set_n_circid_chan(circuit_t
*circ
, circid_t id
,
229 circuit_set_circid_chan_helper(circ
, CELL_DIRECTION_OUT
, id
, chan
);
232 tor_assert(bool_eq(circ
->n_chan_cells
.n
, circ
->next_active_on_n_chan
));
235 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
236 * it from lists as appropriate. */
238 circuit_set_state(circuit_t
*circ
, uint8_t state
)
241 if (state
== circ
->state
)
243 if (!circuits_pending_chans
)
244 circuits_pending_chans
= smartlist_new();
245 if (circ
->state
== CIRCUIT_STATE_CHAN_WAIT
) {
246 /* remove from waiting-circuit list. */
247 smartlist_remove(circuits_pending_chans
, circ
);
249 if (state
== CIRCUIT_STATE_CHAN_WAIT
) {
250 /* add to waiting-circuit list. */
251 smartlist_add(circuits_pending_chans
, circ
);
253 if (state
== CIRCUIT_STATE_OPEN
)
254 tor_assert(!circ
->n_chan_onionskin
);
258 /** Add <b>circ</b> to the global list of circuits. This is called only from
259 * within circuit_new.
262 circuit_add(circuit_t
*circ
)
264 if (!global_circuitlist
) { /* first one */
265 global_circuitlist
= circ
;
268 circ
->next
= global_circuitlist
;
269 global_circuitlist
= circ
;
273 /** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
274 * the given connection. */
276 circuit_get_all_pending_on_channel(smartlist_t
*out
, channel_t
*chan
)
281 if (!circuits_pending_chans
)
284 SMARTLIST_FOREACH_BEGIN(circuits_pending_chans
, circuit_t
*, circ
) {
285 if (circ
->marked_for_close
)
289 tor_assert(circ
->state
== CIRCUIT_STATE_CHAN_WAIT
);
290 if (tor_digest_is_zero(circ
->n_hop
->identity_digest
)) {
291 /* Look at addr/port. This is an unkeyed connection. */
292 if (!channel_matches_extend_info(chan
, circ
->n_hop
))
295 /* We expected a key. See if it's the right one. */
296 if (tor_memneq(chan
->identity_digest
,
297 circ
->n_hop
->identity_digest
, DIGEST_LEN
))
300 smartlist_add(out
, circ
);
301 } SMARTLIST_FOREACH_END(circ
);
304 /** Return the number of circuits in state CHAN_WAIT, waiting for the given
307 circuit_count_pending_on_channel(channel_t
*chan
)
310 smartlist_t
*sl
= smartlist_new();
314 circuit_get_all_pending_on_channel(sl
, chan
);
315 cnt
= smartlist_len(sl
);
317 log_debug(LD_CIRC
,"or_conn to %s at %s, %d pending circs",
318 chan
->nickname
? chan
->nickname
: "NULL",
319 channel_get_canonical_remote_descr(chan
),
324 /** Detach from the global circuit list, and deallocate, all
325 * circuits that have been marked for close.
328 circuit_close_all_marked(void)
332 while (global_circuitlist
&& global_circuitlist
->marked_for_close
) {
333 tmp
= global_circuitlist
->next
;
334 circuit_free(global_circuitlist
);
335 global_circuitlist
= tmp
;
338 tmp
= global_circuitlist
;
339 while (tmp
&& tmp
->next
) {
340 if (tmp
->next
->marked_for_close
) {
342 circuit_free(tmp
->next
);
344 /* Need to check new tmp->next; don't advance tmp. */
352 /** Return the head of the global linked list of circuits. */
354 circuit_get_global_list_(void)
356 return global_circuitlist
;
359 /** Function to make circ-\>state human-readable */
361 circuit_state_to_string(int state
)
365 case CIRCUIT_STATE_BUILDING
: return "doing handshakes";
366 case CIRCUIT_STATE_ONIONSKIN_PENDING
: return "processing the onion";
367 case CIRCUIT_STATE_CHAN_WAIT
: return "connecting to server";
368 case CIRCUIT_STATE_OPEN
: return "open";
370 log_warn(LD_BUG
, "Unknown circuit state %d", state
);
371 tor_snprintf(buf
, sizeof(buf
), "unknown state [%d]", state
);
376 /** Map a circuit purpose to a string suitable to be displayed to a
379 circuit_purpose_to_controller_string(uint8_t purpose
)
383 case CIRCUIT_PURPOSE_OR
:
384 case CIRCUIT_PURPOSE_INTRO_POINT
:
385 case CIRCUIT_PURPOSE_REND_POINT_WAITING
:
386 case CIRCUIT_PURPOSE_REND_ESTABLISHED
:
387 return "SERVER"; /* A controller should never see these, actually. */
389 case CIRCUIT_PURPOSE_C_GENERAL
:
391 case CIRCUIT_PURPOSE_C_INTRODUCING
:
392 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
393 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
:
394 return "HS_CLIENT_INTRO";
396 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
397 case CIRCUIT_PURPOSE_C_REND_READY
:
398 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
399 case CIRCUIT_PURPOSE_C_REND_JOINED
:
400 return "HS_CLIENT_REND";
402 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
403 case CIRCUIT_PURPOSE_S_INTRO
:
404 return "HS_SERVICE_INTRO";
406 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
407 case CIRCUIT_PURPOSE_S_REND_JOINED
:
408 return "HS_SERVICE_REND";
410 case CIRCUIT_PURPOSE_TESTING
:
412 case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
:
413 return "MEASURE_TIMEOUT";
414 case CIRCUIT_PURPOSE_CONTROLLER
:
418 tor_snprintf(buf
, sizeof(buf
), "UNKNOWN_%d", (int)purpose
);
423 /** Return a string specifying the state of the hidden-service circuit
424 * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
425 * hidden-service-related circuit purpose. */
427 circuit_purpose_to_controller_hs_state_string(uint8_t purpose
)
432 log_fn(LOG_WARN
, LD_BUG
,
433 "Unrecognized circuit purpose: %d",
435 tor_fragile_assert();
438 case CIRCUIT_PURPOSE_OR
:
439 case CIRCUIT_PURPOSE_C_GENERAL
:
440 case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
:
441 case CIRCUIT_PURPOSE_TESTING
:
442 case CIRCUIT_PURPOSE_CONTROLLER
:
445 case CIRCUIT_PURPOSE_INTRO_POINT
:
446 return "OR_HSSI_ESTABLISHED";
447 case CIRCUIT_PURPOSE_REND_POINT_WAITING
:
448 return "OR_HSCR_ESTABLISHED";
449 case CIRCUIT_PURPOSE_REND_ESTABLISHED
:
450 return "OR_HS_R_JOINED";
452 case CIRCUIT_PURPOSE_C_INTRODUCING
:
453 return "HSCI_CONNECTING";
454 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
455 return "HSCI_INTRO_SENT";
456 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
:
459 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
460 return "HSCR_CONNECTING";
461 case CIRCUIT_PURPOSE_C_REND_READY
:
462 return "HSCR_ESTABLISHED_IDLE";
463 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
464 return "HSCR_ESTABLISHED_WAITING";
465 case CIRCUIT_PURPOSE_C_REND_JOINED
:
466 return "HSCR_JOINED";
468 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
469 return "HSSI_CONNECTING";
470 case CIRCUIT_PURPOSE_S_INTRO
:
471 return "HSSI_ESTABLISHED";
473 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
474 return "HSSR_CONNECTING";
475 case CIRCUIT_PURPOSE_S_REND_JOINED
:
476 return "HSSR_JOINED";
480 /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
482 circuit_purpose_to_string(uint8_t purpose
)
488 case CIRCUIT_PURPOSE_OR
:
489 return "Circuit at relay";
490 case CIRCUIT_PURPOSE_INTRO_POINT
:
491 return "Acting as intro point";
492 case CIRCUIT_PURPOSE_REND_POINT_WAITING
:
493 return "Acting as rendevous (pending)";
494 case CIRCUIT_PURPOSE_REND_ESTABLISHED
:
495 return "Acting as rendevous (established)";
496 case CIRCUIT_PURPOSE_C_GENERAL
:
497 return "General-purpose client";
498 case CIRCUIT_PURPOSE_C_INTRODUCING
:
499 return "Hidden service client: Connecting to intro point";
500 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
501 return "Hidden service client: Waiting for ack from intro point";
502 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
:
503 return "Hidden service client: Received ack from intro point";
504 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
505 return "Hidden service client: Establishing rendezvous point";
506 case CIRCUIT_PURPOSE_C_REND_READY
:
507 return "Hidden service client: Pending rendezvous point";
508 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
509 return "Hidden service client: Pending rendezvous point (ack received)";
510 case CIRCUIT_PURPOSE_C_REND_JOINED
:
511 return "Hidden service client: Active rendezvous point";
512 case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
:
513 return "Measuring circuit timeout";
515 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
516 return "Hidden service: Establishing introduction point";
517 case CIRCUIT_PURPOSE_S_INTRO
:
518 return "Hidden service: Introduction point";
519 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
520 return "Hidden service: Connecting to rendezvous point";
521 case CIRCUIT_PURPOSE_S_REND_JOINED
:
522 return "Hidden service: Active rendezvous point";
524 case CIRCUIT_PURPOSE_TESTING
:
525 return "Testing circuit";
527 case CIRCUIT_PURPOSE_CONTROLLER
:
528 return "Circuit made by controller";
531 tor_snprintf(buf
, sizeof(buf
), "UNKNOWN_%d", (int)purpose
);
536 /** Pick a reasonable package_window to start out for our circuits.
537 * Originally this was hard-coded at 1000, but now the consensus votes
538 * on the answer. See proposal 168. */
540 circuit_initial_package_window(void)
542 int32_t num
= networkstatus_get_param(NULL
, "circwindow", CIRCWINDOW_START
,
543 CIRCWINDOW_START_MIN
,
544 CIRCWINDOW_START_MAX
);
545 /* If the consensus tells us a negative number, we'd assert. */
547 num
= CIRCWINDOW_START
;
551 /** Initialize the common elements in a circuit_t, and add it to the global
554 init_circuit_base(circuit_t
*circ
)
556 tor_gettimeofday(&circ
->timestamp_created
);
558 // Gets reset when we send CREATE_FAST.
559 // circuit_expire_building() expects these to be equal
560 // until the orconn is built.
561 circ
->timestamp_began
= circ
->timestamp_created
;
563 circ
->package_window
= circuit_initial_package_window();
564 circ
->deliver_window
= CIRCWINDOW_START
;
569 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
570 * and <b>p_conn</b>. Add it to the global circuit list.
573 origin_circuit_new(void)
575 origin_circuit_t
*circ
;
576 /* never zero, since a global ID of 0 is treated specially by the
578 static uint32_t n_circuits_allocated
= 1;
580 circ
= tor_malloc_zero(sizeof(origin_circuit_t
));
581 circ
->base_
.magic
= ORIGIN_CIRCUIT_MAGIC
;
583 circ
->next_stream_id
= crypto_rand_int(1<<16);
584 circ
->global_identifier
= n_circuits_allocated
++;
585 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
586 circ
->remaining_relay_early_cells
-= crypto_rand_int(2);
588 init_circuit_base(TO_CIRCUIT(circ
));
590 circ_times
.last_circ_at
= approx_time();
595 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
596 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
598 or_circuit_new(circid_t p_circ_id
, channel_t
*p_chan
)
603 circ
= tor_malloc_zero(sizeof(or_circuit_t
));
604 circ
->base_
.magic
= OR_CIRCUIT_MAGIC
;
607 circuit_set_p_circid_chan(circ
, p_circ_id
, p_chan
);
609 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
611 init_circuit_base(TO_CIRCUIT(circ
));
616 /** Deallocate space associated with circ.
619 circuit_free(circuit_t
*circ
)
626 if (CIRCUIT_IS_ORIGIN(circ
)) {
627 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
629 memlen
= sizeof(origin_circuit_t
);
630 tor_assert(circ
->magic
== ORIGIN_CIRCUIT_MAGIC
);
631 if (ocirc
->build_state
) {
632 extend_info_free(ocirc
->build_state
->chosen_exit
);
633 circuit_free_cpath_node(ocirc
->build_state
->pending_final_cpath
);
634 cpath_ref_decref(ocirc
->build_state
->service_pending_final_cpath_ref
);
636 tor_free(ocirc
->build_state
);
638 circuit_free_cpath(ocirc
->cpath
);
640 crypto_pk_free(ocirc
->intro_key
);
641 rend_data_free(ocirc
->rend_data
);
643 tor_free(ocirc
->dest_address
);
644 if (ocirc
->socks_username
) {
645 memwipe(ocirc
->socks_username
, 0x12, ocirc
->socks_username_len
);
646 tor_free(ocirc
->socks_username
);
648 if (ocirc
->socks_password
) {
649 memwipe(ocirc
->socks_password
, 0x06, ocirc
->socks_password_len
);
650 tor_free(ocirc
->socks_password
);
653 or_circuit_t
*ocirc
= TO_OR_CIRCUIT(circ
);
654 /* Remember cell statistics for this circuit before deallocating. */
655 if (get_options()->CellStatistics
)
656 rep_hist_buffer_stats_add_circ(circ
, time(NULL
));
658 memlen
= sizeof(or_circuit_t
);
659 tor_assert(circ
->magic
== OR_CIRCUIT_MAGIC
);
661 crypto_cipher_free(ocirc
->p_crypto
);
662 crypto_digest_free(ocirc
->p_digest
);
663 crypto_cipher_free(ocirc
->n_crypto
);
664 crypto_digest_free(ocirc
->n_digest
);
666 if (ocirc
->rend_splice
) {
667 or_circuit_t
*other
= ocirc
->rend_splice
;
668 tor_assert(other
->base_
.magic
== OR_CIRCUIT_MAGIC
);
669 other
->rend_splice
= NULL
;
672 /* remove from map. */
673 circuit_set_p_circid_chan(ocirc
, 0, NULL
);
675 /* Clear cell queue _after_ removing it from the map. Otherwise our
676 * "active" checks will be violated. */
677 cell_queue_clear(ô
->p_chan_cells
);
680 extend_info_free(circ
->n_hop
);
681 tor_free(circ
->n_chan_onionskin
);
683 /* Remove from map. */
684 circuit_set_n_circid_chan(circ
, 0, NULL
);
686 /* Clear cell queue _after_ removing it from the map. Otherwise our
687 * "active" checks will be violated. */
688 cell_queue_clear(&circ
->n_chan_cells
);
690 memwipe(mem
, 0xAA, memlen
); /* poison memory */
694 /** Deallocate space associated with the linked list <b>cpath</b>. */
696 circuit_free_cpath(crypt_path_t
*cpath
)
698 crypt_path_t
*victim
, *head
=cpath
;
703 /* it's a doubly linked list, so we have to notice when we've
704 * gone through it once. */
705 while (cpath
->next
&& cpath
->next
!= head
) {
707 cpath
= victim
->next
;
708 circuit_free_cpath_node(victim
);
711 circuit_free_cpath_node(cpath
);
714 /** Release all storage held by circuits. */
716 circuit_free_all(void)
719 while (global_circuitlist
) {
720 next
= global_circuitlist
->next
;
721 if (! CIRCUIT_IS_ORIGIN(global_circuitlist
)) {
722 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(global_circuitlist
);
723 while (or_circ
->resolving_streams
) {
724 edge_connection_t
*next_conn
;
725 next_conn
= or_circ
->resolving_streams
->next_stream
;
726 connection_free(TO_CONN(or_circ
->resolving_streams
));
727 or_circ
->resolving_streams
= next_conn
;
730 circuit_free(global_circuitlist
);
731 global_circuitlist
= next
;
734 smartlist_free(circuits_pending_chans
);
735 circuits_pending_chans
= NULL
;
737 HT_CLEAR(chan_circid_map
, &chan_circid_map
);
740 /** Deallocate space associated with the cpath node <b>victim</b>. */
742 circuit_free_cpath_node(crypt_path_t
*victim
)
747 crypto_cipher_free(victim
->f_crypto
);
748 crypto_cipher_free(victim
->b_crypto
);
749 crypto_digest_free(victim
->f_digest
);
750 crypto_digest_free(victim
->b_digest
);
751 crypto_dh_free(victim
->dh_handshake_state
);
752 extend_info_free(victim
->extend_info
);
754 memwipe(victim
, 0xBB, sizeof(crypt_path_t
)); /* poison memory */
758 /** Release a crypt_path_reference_t*, which may be NULL. */
760 cpath_ref_decref(crypt_path_reference_t
*cpath_ref
)
762 if (cpath_ref
!= NULL
) {
763 if (--(cpath_ref
->refcount
) == 0) {
764 circuit_free_cpath_node(cpath_ref
->cpath
);
770 /** A helper function for circuit_dump_by_conn() below. Log a bunch
771 * of information about circuit <b>circ</b>.
774 circuit_dump_conn_details(int severity
,
776 int conn_array_index
,
781 log(severity
, LD_CIRC
, "Conn %d has %s circuit: circID %d (other side %d), "
782 "state %d (%s), born %ld:",
783 conn_array_index
, type
, this_circid
, other_circid
, circ
->state
,
784 circuit_state_to_string(circ
->state
),
785 (long)circ
->timestamp_began
.tv_sec
);
786 if (CIRCUIT_IS_ORIGIN(circ
)) { /* circ starts at this node */
787 circuit_log_path(severity
, LD_CIRC
, TO_ORIGIN_CIRCUIT(circ
));
791 /** Log, at severity <b>severity</b>, information about each circuit
792 * that is connected to <b>conn</b>.
795 circuit_dump_by_conn(connection_t
*conn
, int severity
)
798 edge_connection_t
*tmpconn
;
800 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
801 circid_t n_circ_id
= circ
->n_circ_id
, p_circ_id
= 0;
803 if (circ
->marked_for_close
) {
807 if (!CIRCUIT_IS_ORIGIN(circ
)) {
808 p_circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
;
811 if (CIRCUIT_IS_ORIGIN(circ
)) {
812 for (tmpconn
=TO_ORIGIN_CIRCUIT(circ
)->p_streams
; tmpconn
;
813 tmpconn
=tmpconn
->next_stream
) {
814 if (TO_CONN(tmpconn
) == conn
) {
815 circuit_dump_conn_details(severity
, circ
, conn
->conn_array_index
,
816 "App-ward", p_circ_id
, n_circ_id
);
821 if (! CIRCUIT_IS_ORIGIN(circ
)) {
822 for (tmpconn
=TO_OR_CIRCUIT(circ
)->n_streams
; tmpconn
;
823 tmpconn
=tmpconn
->next_stream
) {
824 if (TO_CONN(tmpconn
) == conn
) {
825 circuit_dump_conn_details(severity
, circ
, conn
->conn_array_index
,
826 "Exit-ward", n_circ_id
, p_circ_id
);
833 /** A helper function for circuit_dump_by_chan() below. Log a bunch
834 * of information about circuit <b>circ</b>.
837 circuit_dump_chan_details(int severity
,
844 log(severity
, LD_CIRC
, "Conn %p has %s circuit: circID %d (other side %d), "
845 "state %d (%s), born %ld:",
846 chan
, type
, this_circid
, other_circid
, circ
->state
,
847 circuit_state_to_string(circ
->state
),
848 (long)circ
->timestamp_began
.tv_sec
);
849 if (CIRCUIT_IS_ORIGIN(circ
)) { /* circ starts at this node */
850 circuit_log_path(severity
, LD_CIRC
, TO_ORIGIN_CIRCUIT(circ
));
854 /** Log, at severity <b>severity</b>, information about each circuit
855 * that is connected to <b>chan</b>.
858 circuit_dump_by_chan(channel_t
*chan
, int severity
)
864 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
865 circid_t n_circ_id
= circ
->n_circ_id
, p_circ_id
= 0;
867 if (circ
->marked_for_close
) {
871 if (!CIRCUIT_IS_ORIGIN(circ
)) {
872 p_circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
;
875 if (! CIRCUIT_IS_ORIGIN(circ
) && TO_OR_CIRCUIT(circ
)->p_chan
&&
876 TO_OR_CIRCUIT(circ
)->p_chan
== chan
) {
877 circuit_dump_chan_details(severity
, circ
, chan
, "App-ward",
878 p_circ_id
, n_circ_id
);
881 if (circ
->n_chan
&& circ
->n_chan
== chan
) {
882 circuit_dump_chan_details(severity
, circ
, chan
, "Exit-ward",
883 n_circ_id
, p_circ_id
);
886 if (!circ
->n_chan
&& circ
->n_hop
&&
887 channel_matches_extend_info(chan
, circ
->n_hop
) &&
888 tor_memeq(chan
->identity_digest
,
889 circ
->n_hop
->identity_digest
, DIGEST_LEN
)) {
890 circuit_dump_chan_details(severity
, circ
, chan
,
891 (circ
->state
== CIRCUIT_STATE_OPEN
&&
892 !CIRCUIT_IS_ORIGIN(circ
)) ?
893 "Endpoint" : "Pending",
894 n_circ_id
, p_circ_id
);
899 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
900 * such circuit exists. */
902 circuit_get_by_global_id(uint32_t id
)
905 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
906 if (CIRCUIT_IS_ORIGIN(circ
) &&
907 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
== id
) {
908 if (circ
->marked_for_close
)
911 return TO_ORIGIN_CIRCUIT(circ
);
917 /** Return a circ such that:
918 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
919 * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
920 * Return NULL if no such circuit exists.
922 static INLINE circuit_t
*
923 circuit_get_by_circid_channel_impl(circid_t circ_id
, channel_t
*chan
)
925 chan_circid_circuit_map_t search
;
926 chan_circid_circuit_map_t
*found
;
928 if (_last_circid_chan_ent
&&
929 circ_id
== _last_circid_chan_ent
->circ_id
&&
930 chan
== _last_circid_chan_ent
->chan
) {
931 found
= _last_circid_chan_ent
;
933 search
.circ_id
= circ_id
;
935 found
= HT_FIND(chan_circid_map
, &chan_circid_map
, &search
);
936 _last_circid_chan_ent
= found
;
938 if (found
&& found
->circuit
) {
940 "circuit_get_by_circid_channel_impl() returning circuit %p for"
941 " circ_id %d, channel ID " U64_FORMAT
" (%p)",
942 found
->circuit
, circ_id
,
943 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
944 return found
->circuit
;
948 "circuit_get_by_circid_channel_impl() found nothing for"
949 " circ_id %d, channel ID " U64_FORMAT
" (%p)",
951 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
954 /* The rest of this checks for bugs. Disabled by default. */
955 /* We comment it out because coverity complains otherwise.
958 for (circ=global_circuitlist;circ;circ = circ->next) {
959 if (! CIRCUIT_IS_ORIGIN(circ)) {
960 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
961 if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
963 "circuit matches p_chan, but not in hash table (Bug!)");
967 if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
969 "circuit matches n_chan, but not in hash table (Bug!)");
977 /** Return a circ such that:
978 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
979 * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
980 * - circ is not marked for close.
981 * Return NULL if no such circuit exists.
984 circuit_get_by_circid_channel(circid_t circ_id
, channel_t
*chan
)
986 circuit_t
*circ
= circuit_get_by_circid_channel_impl(circ_id
, chan
);
987 if (!circ
|| circ
->marked_for_close
)
993 /** Return a circ such that:
994 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
995 * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
996 * Return NULL if no such circuit exists.
999 circuit_get_by_circid_channel_even_if_marked(circid_t circ_id
,
1002 return circuit_get_by_circid_channel_impl(circ_id
, chan
);
1005 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
1006 * circuit, marked or not, on <b>chan</b>. */
1008 circuit_id_in_use_on_channel(circid_t circ_id
, channel_t
*chan
)
1010 return circuit_get_by_circid_channel_impl(circ_id
, chan
) != NULL
;
1013 /** Return the circuit that a given edge connection is using. */
1015 circuit_get_by_edge_conn(edge_connection_t
*conn
)
1019 circ
= conn
->on_circuit
;
1021 (CIRCUIT_IS_ORIGIN(circ
) ? circ
->magic
== ORIGIN_CIRCUIT_MAGIC
1022 : circ
->magic
== OR_CIRCUIT_MAGIC
));
1027 /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
1028 * circuit from the chan,circid map, and mark it for close if it hasn't
1029 * been marked already.
1032 circuit_unlink_all_from_channel(channel_t
*chan
, int reason
)
1036 channel_unlink_all_circuits(chan
);
1038 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
1040 if (circ
->n_chan
== chan
) {
1041 circuit_set_n_circid_chan(circ
, 0, NULL
);
1044 /* If we didn't request this closure, pass the remote
1045 * bit to mark_for_close. */
1046 if (chan
->reason_for_closing
!= CHANNEL_CLOSE_REQUESTED
)
1047 reason
|= END_CIRC_REASON_FLAG_REMOTE
;
1049 if (! CIRCUIT_IS_ORIGIN(circ
)) {
1050 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1051 if (or_circ
->p_chan
== chan
) {
1052 circuit_set_p_circid_chan(or_circ
, 0, NULL
);
1056 if (mark
&& !circ
->marked_for_close
)
1057 circuit_mark_for_close(circ
, reason
);
1061 /** Return a circ such that
1062 * - circ-\>rend_data-\>onion_address is equal to
1063 * <b>rend_data</b>-\>onion_address,
1064 * - circ-\>rend_data-\>rend_cookie is equal to
1065 * <b>rend_data</b>-\>rend_cookie, and
1066 * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
1068 * Return NULL if no such circuit exists.
1071 circuit_get_ready_rend_circ_by_rend_data(const rend_data_t
*rend_data
)
1075 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
1076 if (!circ
->marked_for_close
&&
1077 circ
->purpose
== CIRCUIT_PURPOSE_C_REND_READY
) {
1078 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1079 if (ocirc
->rend_data
&&
1080 !rend_cmp_service_ids(rend_data
->onion_address
,
1081 ocirc
->rend_data
->onion_address
) &&
1082 tor_memeq(ocirc
->rend_data
->rend_cookie
,
1083 rend_data
->rend_cookie
,
1091 /** Return the first circuit originating here in global_circuitlist after
1092 * <b>start</b> whose purpose is <b>purpose</b>, and where
1093 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
1094 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
1097 circuit_get_next_by_pk_and_purpose(origin_circuit_t
*start
,
1098 const char *digest
, uint8_t purpose
)
1101 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
1103 circ
= global_circuitlist
;
1105 circ
= TO_CIRCUIT(start
)->next
;
1107 for ( ; circ
; circ
= circ
->next
) {
1108 if (circ
->marked_for_close
)
1110 if (circ
->purpose
!= purpose
)
1113 return TO_ORIGIN_CIRCUIT(circ
);
1114 else if (TO_ORIGIN_CIRCUIT(circ
)->rend_data
&&
1115 tor_memeq(TO_ORIGIN_CIRCUIT(circ
)->rend_data
->rend_pk_digest
,
1116 digest
, DIGEST_LEN
))
1117 return TO_ORIGIN_CIRCUIT(circ
);
1122 /** Return the first OR circuit in the global list whose purpose is
1123 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
1125 static or_circuit_t
*
1126 circuit_get_by_rend_token_and_purpose(uint8_t purpose
, const char *token
,
1130 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
1131 if (! circ
->marked_for_close
&&
1132 circ
->purpose
== purpose
&&
1133 tor_memeq(TO_OR_CIRCUIT(circ
)->rend_token
, token
, len
))
1134 return TO_OR_CIRCUIT(circ
);
1139 /** Return the circuit waiting for a rendezvous with the provided cookie.
1140 * Return NULL if no such circuit is found.
1143 circuit_get_rendezvous(const char *cookie
)
1145 return circuit_get_by_rend_token_and_purpose(
1146 CIRCUIT_PURPOSE_REND_POINT_WAITING
,
1147 cookie
, REND_COOKIE_LEN
);
1150 /** Return the circuit waiting for intro cells of the given digest.
1151 * Return NULL if no such circuit is found.
1154 circuit_get_intro_point(const char *digest
)
1156 return circuit_get_by_rend_token_and_purpose(
1157 CIRCUIT_PURPOSE_INTRO_POINT
, digest
,
1161 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
1162 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
1163 * flags in <b>flags</b>, and if info is defined, does not already use info
1164 * as any of its hops; or NULL if no circuit fits this description.
1166 * The <b>purpose</b> argument (currently ignored) refers to the purpose of
1167 * the circuit we want to create, not the purpose of the circuit we want to
1170 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
1173 circuit_find_to_cannibalize(uint8_t purpose
, extend_info_t
*info
,
1177 origin_circuit_t
*best
=NULL
;
1178 int need_uptime
= (flags
& CIRCLAUNCH_NEED_UPTIME
) != 0;
1179 int need_capacity
= (flags
& CIRCLAUNCH_NEED_CAPACITY
) != 0;
1180 int internal
= (flags
& CIRCLAUNCH_IS_INTERNAL
) != 0;
1181 const or_options_t
*options
= get_options();
1183 /* Make sure we're not trying to create a onehop circ by
1184 * cannibalization. */
1185 tor_assert(!(flags
& CIRCLAUNCH_ONEHOP_TUNNEL
));
1188 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
1189 "capacity %d, internal %d",
1190 purpose
, need_uptime
, need_capacity
, internal
);
1192 for (circ_
=global_circuitlist
; circ_
; circ_
= circ_
->next
) {
1193 if (CIRCUIT_IS_ORIGIN(circ_
) &&
1194 circ_
->state
== CIRCUIT_STATE_OPEN
&&
1195 !circ_
->marked_for_close
&&
1196 circ_
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
1197 !circ_
->timestamp_dirty
) {
1198 origin_circuit_t
*circ
= TO_ORIGIN_CIRCUIT(circ_
);
1199 if ((!need_uptime
|| circ
->build_state
->need_uptime
) &&
1200 (!need_capacity
|| circ
->build_state
->need_capacity
) &&
1201 (internal
== circ
->build_state
->is_internal
) &&
1202 circ
->remaining_relay_early_cells
&&
1203 circ
->build_state
->desired_path_len
== DEFAULT_ROUTE_LEN
&&
1204 !circ
->build_state
->onehop_tunnel
&&
1205 !circ
->isolation_values_set
) {
1207 /* need to make sure we don't duplicate hops */
1208 crypt_path_t
*hop
= circ
->cpath
;
1209 const node_t
*ri1
= node_get_by_id(info
->identity_digest
);
1212 if (tor_memeq(hop
->extend_info
->identity_digest
,
1213 info
->identity_digest
, DIGEST_LEN
))
1216 (ri2
= node_get_by_id(hop
->extend_info
->identity_digest
))
1217 && nodes_in_same_family(ri1
, ri2
))
1220 } while (hop
!=circ
->cpath
);
1222 if (options
->ExcludeNodes
) {
1223 /* Make sure no existing nodes in the circuit are excluded for
1224 * general use. (This may be possible if StrictNodes is 0, and we
1225 * thought we needed to use an otherwise excluded node for, say, a
1226 * directory operation.) */
1227 crypt_path_t
*hop
= circ
->cpath
;
1229 if (routerset_contains_extendinfo(options
->ExcludeNodes
,
1233 } while (hop
!= circ
->cpath
);
1235 if (!best
|| (best
->build_state
->need_uptime
&& !need_uptime
))
1244 /** Return the number of hops in circuit's path. */
1246 circuit_get_cpath_len(origin_circuit_t
*circ
)
1249 if (circ
&& circ
->cpath
) {
1250 crypt_path_t
*cpath
, *cpath_next
= NULL
;
1251 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
1252 cpath_next
= cpath
->next
;
1259 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
1260 * aren't that many hops in the list. */
1262 circuit_get_cpath_hop(origin_circuit_t
*circ
, int hopnum
)
1264 if (circ
&& circ
->cpath
&& hopnum
> 0) {
1265 crypt_path_t
*cpath
, *cpath_next
= NULL
;
1266 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
1267 cpath_next
= cpath
->next
;
1275 /** Go through the circuitlist; mark-for-close each circuit that starts
1276 * at us but has not yet been used. */
1278 circuit_mark_all_unused_circs(void)
1282 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
1283 if (CIRCUIT_IS_ORIGIN(circ
) &&
1284 !circ
->marked_for_close
&&
1285 !circ
->timestamp_dirty
)
1286 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
1290 /** Go through the circuitlist; for each circuit that starts at us
1291 * and is dirty, frob its timestamp_dirty so we won't use it for any
1294 * This is useful for letting the user change pseudonyms, so new
1295 * streams will not be linkable to old streams.
1297 /* XXX024 this is a bad name for what this function does */
1299 circuit_expire_all_dirty_circs(void)
1302 const or_options_t
*options
= get_options();
1304 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
1305 if (CIRCUIT_IS_ORIGIN(circ
) &&
1306 !circ
->marked_for_close
&&
1307 circ
->timestamp_dirty
)
1308 /* XXXX024 This is a screwed-up way to say "This is too dirty
1309 * for new circuits. */
1310 circ
->timestamp_dirty
-= options
->MaxCircuitDirtiness
;
1314 /** Mark <b>circ</b> to be closed next time we call
1315 * circuit_close_all_marked(). Do any cleanup needed:
1316 * - If state is onionskin_pending, remove circ from the onion_pending
1318 * - If circ isn't open yet: call circuit_build_failed() if we're
1319 * the origin, and in either case call circuit_rep_hist_note_result()
1321 * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
1322 * failure we just had to the hidden service client module.
1323 * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
1324 * report to the hidden service client module that the intro point
1325 * we just tried may be unreachable.
1326 * - Send appropriate destroys and edge_destroys for conns and
1327 * streams attached to circ.
1328 * - If circ->rend_splice is set (we are the midpoint of a joined
1329 * rendezvous stream), then mark the other circuit to close as well.
1332 circuit_mark_for_close_(circuit_t
*circ
, int reason
, int line
,
1335 int orig_reason
= reason
; /* Passed to the controller */
1336 assert_circuit_ok(circ
);
1340 if (circ
->marked_for_close
) {
1341 log(LOG_WARN
,LD_BUG
,
1342 "Duplicate call to circuit_mark_for_close at %s:%d"
1343 " (first at %s:%d)", file
, line
,
1344 circ
->marked_for_close_file
, circ
->marked_for_close
);
1347 if (reason
== END_CIRC_AT_ORIGIN
) {
1348 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1349 log_warn(LD_BUG
, "Specified 'at-origin' non-reason for ending circuit, "
1350 "but circuit was not at origin. (called %s:%d, purpose=%d)",
1351 file
, line
, circ
->purpose
);
1353 reason
= END_CIRC_REASON_NONE
;
1356 if (CIRCUIT_IS_ORIGIN(circ
)) {
1357 pathbias_check_close(TO_ORIGIN_CIRCUIT(circ
), reason
);
1359 /* We don't send reasons when closing circuits at the origin. */
1360 reason
= END_CIRC_REASON_NONE
;
1363 if (reason
& END_CIRC_REASON_FLAG_REMOTE
)
1364 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
1366 if (reason
< END_CIRC_REASON_MIN_
|| reason
> END_CIRC_REASON_MAX_
) {
1367 if (!(orig_reason
& END_CIRC_REASON_FLAG_REMOTE
))
1368 log_warn(LD_BUG
, "Reason %d out of range at %s:%d", reason
, file
, line
);
1369 reason
= END_CIRC_REASON_NONE
;
1372 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
1373 onion_pending_remove(TO_OR_CIRCUIT(circ
));
1375 /* If the circuit ever became OPEN, we sent it to the reputation history
1376 * module then. If it isn't OPEN, we send it there now to remember which
1377 * links worked and which didn't.
1379 if (circ
->state
!= CIRCUIT_STATE_OPEN
) {
1380 if (CIRCUIT_IS_ORIGIN(circ
)) {
1381 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1382 circuit_build_failed(ocirc
); /* take actions if necessary */
1383 circuit_rep_hist_note_result(ocirc
);
1386 if (circ
->state
== CIRCUIT_STATE_CHAN_WAIT
) {
1387 if (circuits_pending_chans
)
1388 smartlist_remove(circuits_pending_chans
, circ
);
1390 if (CIRCUIT_IS_ORIGIN(circ
)) {
1391 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ
),
1392 (circ
->state
== CIRCUIT_STATE_OPEN
)?CIRC_EVENT_CLOSED
:CIRC_EVENT_FAILED
,
1395 if (circ
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1396 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1397 int timed_out
= (reason
== END_CIRC_REASON_TIMEOUT
);
1398 tor_assert(circ
->state
== CIRCUIT_STATE_OPEN
);
1399 tor_assert(ocirc
->build_state
->chosen_exit
);
1400 tor_assert(ocirc
->rend_data
);
1401 /* treat this like getting a nack from it */
1402 log_info(LD_REND
, "Failed intro circ %s to %s (awaiting ack). %s",
1403 safe_str_client(ocirc
->rend_data
->onion_address
),
1404 safe_str_client(build_state_get_exit_nickname(ocirc
->build_state
)),
1405 timed_out
? "Recording timeout." : "Removing from descriptor.");
1406 rend_client_report_intro_point_failure(ocirc
->build_state
->chosen_exit
,
1409 INTRO_POINT_FAILURE_TIMEOUT
:
1410 INTRO_POINT_FAILURE_GENERIC
);
1411 } else if (circ
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
&&
1412 reason
!= END_CIRC_REASON_TIMEOUT
) {
1413 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1414 if (ocirc
->build_state
->chosen_exit
&& ocirc
->rend_data
) {
1415 log_info(LD_REND
, "Failed intro circ %s to %s "
1416 "(building circuit to intro point). "
1417 "Marking intro point as possibly unreachable.",
1418 safe_str_client(ocirc
->rend_data
->onion_address
),
1419 safe_str_client(build_state_get_exit_nickname(ocirc
->build_state
)));
1420 rend_client_report_intro_point_failure(ocirc
->build_state
->chosen_exit
,
1422 INTRO_POINT_FAILURE_UNREACHABLE
);
1426 circuit_clear_cell_queue(circ
, circ
->n_chan
);
1427 /* Only send destroy if the channel isn't closing anyway */
1428 if (!(circ
->n_chan
->state
== CHANNEL_STATE_CLOSING
||
1429 circ
->n_chan
->state
== CHANNEL_STATE_CLOSED
||
1430 circ
->n_chan
->state
== CHANNEL_STATE_ERROR
)) {
1431 channel_send_destroy(circ
->n_circ_id
, circ
->n_chan
, reason
);
1433 circuitmux_detach_circuit(circ
->n_chan
->cmux
, circ
);
1436 if (! CIRCUIT_IS_ORIGIN(circ
)) {
1437 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1438 edge_connection_t
*conn
;
1439 for (conn
=or_circ
->n_streams
; conn
; conn
=conn
->next_stream
)
1440 connection_edge_destroy(or_circ
->p_circ_id
, conn
);
1441 or_circ
->n_streams
= NULL
;
1443 while (or_circ
->resolving_streams
) {
1444 conn
= or_circ
->resolving_streams
;
1445 or_circ
->resolving_streams
= conn
->next_stream
;
1446 if (!conn
->base_
.marked_for_close
) {
1447 /* The client will see a DESTROY, and infer that the connections
1448 * are closing because the circuit is getting torn down. No need
1449 * to send an end cell. */
1450 conn
->edge_has_sent_end
= 1;
1451 conn
->end_reason
= END_STREAM_REASON_DESTROY
;
1452 conn
->end_reason
|= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
;
1453 connection_mark_for_close(TO_CONN(conn
));
1455 conn
->on_circuit
= NULL
;
1458 if (or_circ
->p_chan
) {
1459 circuit_clear_cell_queue(circ
, or_circ
->p_chan
);
1460 /* Only send destroy if the channel isn't closing anyway */
1461 if (!(or_circ
->p_chan
->state
== CHANNEL_STATE_CLOSING
||
1462 or_circ
->p_chan
->state
== CHANNEL_STATE_CLOSED
||
1463 or_circ
->p_chan
->state
== CHANNEL_STATE_ERROR
)) {
1464 channel_send_destroy(or_circ
->p_circ_id
, or_circ
->p_chan
, reason
);
1466 circuitmux_detach_circuit(or_circ
->p_chan
->cmux
, circ
);
1469 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1470 edge_connection_t
*conn
;
1471 for (conn
=ocirc
->p_streams
; conn
; conn
=conn
->next_stream
)
1472 connection_edge_destroy(circ
->n_circ_id
, conn
);
1473 ocirc
->p_streams
= NULL
;
1476 circ
->marked_for_close
= line
;
1477 circ
->marked_for_close_file
= file
;
1479 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1480 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1481 if (or_circ
->rend_splice
) {
1482 if (!or_circ
->rend_splice
->base_
.marked_for_close
) {
1483 /* do this after marking this circuit, to avoid infinite recursion. */
1484 circuit_mark_for_close(TO_CIRCUIT(or_circ
->rend_splice
), reason
);
1486 or_circ
->rend_splice
= NULL
;
1491 /** Verify that cpath layer <b>cp</b> has all of its invariants
1492 * correct. Trigger an assert if anything is invalid.
1495 assert_cpath_layer_ok(const crypt_path_t
*cp
)
1497 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1498 // tor_assert(cp->port);
1500 tor_assert(cp
->magic
== CRYPT_PATH_MAGIC
);
1503 case CPATH_STATE_OPEN
:
1504 tor_assert(cp
->f_crypto
);
1505 tor_assert(cp
->b_crypto
);
1507 case CPATH_STATE_CLOSED
:
1508 tor_assert(!cp
->dh_handshake_state
);
1510 case CPATH_STATE_AWAITING_KEYS
:
1511 /* tor_assert(cp->dh_handshake_state); */
1514 log_fn(LOG_ERR
, LD_BUG
, "Unexpected state %d", cp
->state
);
1517 tor_assert(cp
->package_window
>= 0);
1518 tor_assert(cp
->deliver_window
>= 0);
1521 /** Verify that cpath <b>cp</b> has all of its invariants
1522 * correct. Trigger an assert if anything is invalid.
1525 assert_cpath_ok(const crypt_path_t
*cp
)
1527 const crypt_path_t
*start
= cp
;
1530 assert_cpath_layer_ok(cp
);
1531 /* layers must be in sequence of: "open* awaiting? closed*" */
1533 if (cp
->state
== CPATH_STATE_AWAITING_KEYS
) {
1534 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1535 } else if (cp
->state
== CPATH_STATE_OPEN
) {
1536 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1541 } while (cp
!= start
);
1544 /** Verify that circuit <b>c</b> has all of its invariants
1545 * correct. Trigger an assert if anything is invalid.
1548 assert_circuit_ok(const circuit_t
*c
)
1550 edge_connection_t
*conn
;
1551 const or_circuit_t
*or_circ
= NULL
;
1552 const origin_circuit_t
*origin_circ
= NULL
;
1555 tor_assert(c
->magic
== ORIGIN_CIRCUIT_MAGIC
|| c
->magic
== OR_CIRCUIT_MAGIC
);
1556 tor_assert(c
->purpose
>= CIRCUIT_PURPOSE_MIN_
&&
1557 c
->purpose
<= CIRCUIT_PURPOSE_MAX_
);
1560 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1561 * never understand. -NM. */
1562 circuit_t
*nonconst_circ
= (circuit_t
*) c
;
1563 if (CIRCUIT_IS_ORIGIN(c
))
1564 origin_circ
= TO_ORIGIN_CIRCUIT(nonconst_circ
);
1566 or_circ
= TO_OR_CIRCUIT(nonconst_circ
);
1570 tor_assert(!c
->n_hop
);
1573 /* We use the _impl variant here to make sure we don't fail on marked
1574 * circuits, which would not be returned by the regular function. */
1575 circuit_t
*c2
= circuit_get_by_circid_channel_impl(c
->n_circ_id
,
1577 tor_assert(c
== c2
);
1580 if (or_circ
&& or_circ
->p_chan
) {
1581 if (or_circ
->p_circ_id
) {
1583 circuit_t
*c2
= circuit_get_by_circid_channel_impl(or_circ
->p_circ_id
,
1585 tor_assert(c
== c2
);
1589 for (conn
= or_circ
->n_streams
; conn
; conn
= conn
->next_stream
)
1590 tor_assert(conn
->base_
.type
== CONN_TYPE_EXIT
);
1592 tor_assert(c
->deliver_window
>= 0);
1593 tor_assert(c
->package_window
>= 0);
1594 if (c
->state
== CIRCUIT_STATE_OPEN
) {
1595 tor_assert(!c
->n_chan_onionskin
);
1597 tor_assert(or_circ
->n_crypto
);
1598 tor_assert(or_circ
->p_crypto
);
1599 tor_assert(or_circ
->n_digest
);
1600 tor_assert(or_circ
->p_digest
);
1603 if (c
->state
== CIRCUIT_STATE_CHAN_WAIT
&& !c
->marked_for_close
) {
1604 tor_assert(circuits_pending_chans
&&
1605 smartlist_isin(circuits_pending_chans
, c
));
1607 tor_assert(!circuits_pending_chans
||
1608 !smartlist_isin(circuits_pending_chans
, c
));
1610 if (origin_circ
&& origin_circ
->cpath
) {
1611 assert_cpath_ok(origin_circ
->cpath
);
1613 if (c
->purpose
== CIRCUIT_PURPOSE_REND_ESTABLISHED
) {
1614 tor_assert(or_circ
);
1615 if (!c
->marked_for_close
) {
1616 tor_assert(or_circ
->rend_splice
);
1617 tor_assert(or_circ
->rend_splice
->rend_splice
== or_circ
);
1619 tor_assert(or_circ
->rend_splice
!= or_circ
);
1621 tor_assert(!or_circ
|| !or_circ
->rend_splice
);