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-2011, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Manage the global circuit list.
15 /********* START VARIABLES **********/
17 /** A global list of all circuits at this hop. */
18 circuit_t
*global_circuitlist
=NULL
;
20 /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
21 static smartlist_t
*circuits_pending_or_conns
=NULL
;
23 static void circuit_free(circuit_t
*circ
);
24 static void circuit_free_cpath(crypt_path_t
*cpath
);
25 static void circuit_free_cpath_node(crypt_path_t
*victim
);
27 /********* END VARIABLES ************/
29 /** A map from OR connection and circuit ID to circuit. (Lookup performance is
30 * very important here, since we need to do it every time a cell arrives.) */
31 typedef struct orconn_circid_circuit_map_t
{
32 HT_ENTRY(orconn_circid_circuit_map_t
) node
;
33 or_connection_t
*or_conn
;
36 } orconn_circid_circuit_map_t
;
38 /** Helper for hash tables: compare the OR connection and circuit ID for a and
39 * b, and return less than, equal to, or greater than zero appropriately.
42 _orconn_circid_entries_eq(orconn_circid_circuit_map_t
*a
,
43 orconn_circid_circuit_map_t
*b
)
45 return a
->or_conn
== b
->or_conn
&& a
->circ_id
== b
->circ_id
;
48 /** Helper: return a hash based on circuit ID and the pointer value of
49 * or_conn in <b>a</b>. */
50 static INLINE
unsigned int
51 _orconn_circid_entry_hash(orconn_circid_circuit_map_t
*a
)
53 return (((unsigned)a
->circ_id
)<<8) ^ (unsigned)(uintptr_t)(a
->or_conn
);
56 /** Map from [orconn,circid] to circuit. */
57 static HT_HEAD(orconn_circid_map
, orconn_circid_circuit_map_t
)
58 orconn_circid_circuit_map
= HT_INITIALIZER();
59 HT_PROTOTYPE(orconn_circid_map
, orconn_circid_circuit_map_t
, node
,
60 _orconn_circid_entry_hash
, _orconn_circid_entries_eq
)
61 HT_GENERATE(orconn_circid_map
, orconn_circid_circuit_map_t
, node
,
62 _orconn_circid_entry_hash
, _orconn_circid_entries_eq
, 0.6,
63 malloc
, realloc
, free
)
65 /** The most recently returned entry from circuit_get_by_circid_orconn;
66 * used to improve performance when many cells arrive in a row from the
69 orconn_circid_circuit_map_t
*_last_circid_orconn_ent
= NULL
;
71 /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
72 * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
73 * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
74 * the old entry (if any) and adding a new one. If <b>active</b> is true,
75 * remove the circuit from the list of active circuits on old_conn and add it
76 * to the list of active circuits on conn.
77 * XXX "active" isn't an arg anymore */
79 circuit_set_circid_orconn_helper(circuit_t
*circ
, int direction
,
81 or_connection_t
*conn
)
83 orconn_circid_circuit_map_t search
;
84 orconn_circid_circuit_map_t
*found
;
85 or_connection_t
*old_conn
, **conn_ptr
;
86 circid_t old_id
, *circid_ptr
;
87 int was_active
, make_active
;
89 if (direction
== CELL_DIRECTION_OUT
) {
90 conn_ptr
= &circ
->n_conn
;
91 circid_ptr
= &circ
->n_circ_id
;
92 was_active
= circ
->next_active_on_n_conn
!= NULL
;
93 make_active
= circ
->n_conn_cells
.n
> 0;
95 or_circuit_t
*c
= TO_OR_CIRCUIT(circ
);
96 conn_ptr
= &c
->p_conn
;
97 circid_ptr
= &c
->p_circ_id
;
98 was_active
= c
->next_active_on_p_conn
!= NULL
;
99 make_active
= c
->p_conn_cells
.n
> 0;
101 old_conn
= *conn_ptr
;
102 old_id
= *circid_ptr
;
104 if (id
== old_id
&& conn
== old_conn
)
107 if (_last_circid_orconn_ent
&&
108 ((old_id
== _last_circid_orconn_ent
->circ_id
&&
109 old_conn
== _last_circid_orconn_ent
->or_conn
) ||
110 (id
== _last_circid_orconn_ent
->circ_id
&&
111 conn
== _last_circid_orconn_ent
->or_conn
))) {
112 _last_circid_orconn_ent
= NULL
;
115 if (old_conn
) { /* we may need to remove it from the conn-circid map */
116 tor_assert(old_conn
->_base
.magic
== OR_CONNECTION_MAGIC
);
117 search
.circ_id
= old_id
;
118 search
.or_conn
= old_conn
;
119 found
= HT_REMOVE(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
122 --old_conn
->n_circuits
;
124 if (was_active
&& old_conn
!= conn
)
125 make_circuit_inactive_on_conn(circ
,old_conn
);
128 /* Change the values only after we have possibly made the circuit inactive
129 * on the previous conn. */
136 /* now add the new one to the conn-circid map */
138 search
.or_conn
= conn
;
139 found
= HT_FIND(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
141 found
->circuit
= circ
;
143 found
= tor_malloc_zero(sizeof(orconn_circid_circuit_map_t
));
145 found
->or_conn
= conn
;
146 found
->circuit
= circ
;
147 HT_INSERT(orconn_circid_map
, &orconn_circid_circuit_map
, found
);
149 if (make_active
&& old_conn
!= conn
)
150 make_circuit_active_on_conn(circ
,conn
);
155 /** Set the p_conn field of a circuit <b>circ</b>, along
156 * with the corresponding circuit ID, and add the circuit as appropriate
157 * to the (orconn,id)-\>circuit map. */
159 circuit_set_p_circid_orconn(or_circuit_t
*circ
, circid_t id
,
160 or_connection_t
*conn
)
162 circuit_set_circid_orconn_helper(TO_CIRCUIT(circ
), CELL_DIRECTION_IN
,
166 tor_assert(bool_eq(circ
->p_conn_cells
.n
, circ
->next_active_on_p_conn
));
169 /** Set the n_conn field of a circuit <b>circ</b>, along
170 * with the corresponding circuit ID, and add the circuit as appropriate
171 * to the (orconn,id)-\>circuit map. */
173 circuit_set_n_circid_orconn(circuit_t
*circ
, circid_t id
,
174 or_connection_t
*conn
)
176 circuit_set_circid_orconn_helper(circ
, CELL_DIRECTION_OUT
, id
, conn
);
179 tor_assert(bool_eq(circ
->n_conn_cells
.n
, circ
->next_active_on_n_conn
));
182 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
183 * it from lists as appropriate. */
185 circuit_set_state(circuit_t
*circ
, uint8_t state
)
188 if (state
== circ
->state
)
190 if (!circuits_pending_or_conns
)
191 circuits_pending_or_conns
= smartlist_create();
192 if (circ
->state
== CIRCUIT_STATE_OR_WAIT
) {
193 /* remove from waiting-circuit list. */
194 smartlist_remove(circuits_pending_or_conns
, circ
);
196 if (state
== CIRCUIT_STATE_OR_WAIT
) {
197 /* add to waiting-circuit list. */
198 smartlist_add(circuits_pending_or_conns
, circ
);
200 if (state
== CIRCUIT_STATE_OPEN
)
201 tor_assert(!circ
->n_conn_onionskin
);
205 /** Add <b>circ</b> to the global list of circuits. This is called only from
206 * within circuit_new.
209 circuit_add(circuit_t
*circ
)
211 if (!global_circuitlist
) { /* first one */
212 global_circuitlist
= circ
;
215 circ
->next
= global_circuitlist
;
216 global_circuitlist
= circ
;
220 /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
221 * the given connection. */
223 circuit_get_all_pending_on_or_conn(smartlist_t
*out
, or_connection_t
*or_conn
)
228 if (!circuits_pending_or_conns
)
231 SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns
, circuit_t
*, circ
) {
232 if (circ
->marked_for_close
)
236 tor_assert(circ
->state
== CIRCUIT_STATE_OR_WAIT
);
237 if (tor_digest_is_zero(circ
->n_hop
->identity_digest
)) {
238 /* Look at addr/port. This is an unkeyed connection. */
239 if (!tor_addr_eq(&circ
->n_hop
->addr
, &or_conn
->_base
.addr
) ||
240 circ
->n_hop
->port
!= or_conn
->_base
.port
)
243 /* We expected a key. See if it's the right one. */
244 if (memcmp(or_conn
->identity_digest
,
245 circ
->n_hop
->identity_digest
, DIGEST_LEN
))
248 smartlist_add(out
, circ
);
249 } SMARTLIST_FOREACH_END(circ
);
252 /** Return the number of circuits in state OR_WAIT, waiting for the given
255 circuit_count_pending_on_or_conn(or_connection_t
*or_conn
)
258 smartlist_t
*sl
= smartlist_create();
259 circuit_get_all_pending_on_or_conn(sl
, or_conn
);
260 cnt
= smartlist_len(sl
);
262 log_debug(LD_CIRC
,"or_conn to %s, %d pending circs",
263 or_conn
->nickname
? or_conn
->nickname
: "NULL", cnt
);
267 /** Detach from the global circuit list, and deallocate, all
268 * circuits that have been marked for close.
271 circuit_close_all_marked(void)
275 while (global_circuitlist
&& global_circuitlist
->marked_for_close
) {
276 tmp
= global_circuitlist
->next
;
277 circuit_free(global_circuitlist
);
278 global_circuitlist
= tmp
;
281 tmp
= global_circuitlist
;
282 while (tmp
&& tmp
->next
) {
283 if (tmp
->next
->marked_for_close
) {
285 circuit_free(tmp
->next
);
287 /* Need to check new tmp->next; don't advance tmp. */
295 /** Return the head of the global linked list of circuits. */
297 _circuit_get_global_list(void)
299 return global_circuitlist
;
302 /** Function to make circ-\>state human-readable */
304 circuit_state_to_string(int state
)
308 case CIRCUIT_STATE_BUILDING
: return "doing handshakes";
309 case CIRCUIT_STATE_ONIONSKIN_PENDING
: return "processing the onion";
310 case CIRCUIT_STATE_OR_WAIT
: return "connecting to server";
311 case CIRCUIT_STATE_OPEN
: return "open";
313 log_warn(LD_BUG
, "Unknown circuit state %d", state
);
314 tor_snprintf(buf
, sizeof(buf
), "unknown state [%d]", state
);
319 /** Map a circuit purpose to a string suitable to be displayed to a
322 circuit_purpose_to_controller_string(uint8_t purpose
)
326 case CIRCUIT_PURPOSE_OR
:
327 case CIRCUIT_PURPOSE_INTRO_POINT
:
328 case CIRCUIT_PURPOSE_REND_POINT_WAITING
:
329 case CIRCUIT_PURPOSE_REND_ESTABLISHED
:
330 return "SERVER"; /* A controller should never see these, actually. */
332 case CIRCUIT_PURPOSE_C_GENERAL
:
334 case CIRCUIT_PURPOSE_C_INTRODUCING
:
335 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
336 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
:
337 return "HS_CLIENT_INTRO";
339 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
340 case CIRCUIT_PURPOSE_C_REND_READY
:
341 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
342 case CIRCUIT_PURPOSE_C_REND_JOINED
:
343 return "HS_CLIENT_REND";
345 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
346 case CIRCUIT_PURPOSE_S_INTRO
:
347 return "HS_SERVICE_INTRO";
349 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
350 case CIRCUIT_PURPOSE_S_REND_JOINED
:
351 return "HS_SERVICE_REND";
353 case CIRCUIT_PURPOSE_TESTING
:
355 case CIRCUIT_PURPOSE_CONTROLLER
:
359 tor_snprintf(buf
, sizeof(buf
), "UNKNOWN_%d", (int)purpose
);
364 /** Pick a reasonable package_window to start out for our circuits.
365 * Originally this was hard-coded at 1000, but now the consensus votes
366 * on the answer. See proposal 168. */
368 circuit_initial_package_window(void)
370 int32_t num
= networkstatus_get_param(NULL
, "circwindow", CIRCWINDOW_START
);
371 /* If the consensus tells us a negative number, we'd assert. */
373 num
= CIRCWINDOW_START
;
377 /** Initialize the common elements in a circuit_t, and add it to the global
380 init_circuit_base(circuit_t
*circ
)
382 circ
->timestamp_created
= time(NULL
);
384 circ
->package_window
= circuit_initial_package_window();
385 circ
->deliver_window
= CIRCWINDOW_START
;
390 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
391 * and <b>p_conn</b>. Add it to the global circuit list.
394 origin_circuit_new(void)
396 origin_circuit_t
*circ
;
397 /* never zero, since a global ID of 0 is treated specially by the
399 static uint32_t n_circuits_allocated
= 1;
401 circ
= tor_malloc_zero(sizeof(origin_circuit_t
));
402 circ
->_base
.magic
= ORIGIN_CIRCUIT_MAGIC
;
404 circ
->next_stream_id
= crypto_rand_int(1<<16);
405 circ
->global_identifier
= n_circuits_allocated
++;
406 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
407 circ
->remaining_relay_early_cells
-= crypto_rand_int(2);
409 init_circuit_base(TO_CIRCUIT(circ
));
414 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
415 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
417 or_circuit_new(circid_t p_circ_id
, or_connection_t
*p_conn
)
422 circ
= tor_malloc_zero(sizeof(or_circuit_t
));
423 circ
->_base
.magic
= OR_CIRCUIT_MAGIC
;
426 circuit_set_p_circid_orconn(circ
, p_circ_id
, p_conn
);
428 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
430 init_circuit_base(TO_CIRCUIT(circ
));
435 /** Deallocate space associated with circ.
438 circuit_free(circuit_t
*circ
)
443 if (CIRCUIT_IS_ORIGIN(circ
)) {
444 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
446 memlen
= sizeof(origin_circuit_t
);
447 tor_assert(circ
->magic
== ORIGIN_CIRCUIT_MAGIC
);
448 if (ocirc
->build_state
) {
449 if (ocirc
->build_state
->chosen_exit
)
450 extend_info_free(ocirc
->build_state
->chosen_exit
);
451 if (ocirc
->build_state
->pending_final_cpath
)
452 circuit_free_cpath_node(ocirc
->build_state
->pending_final_cpath
);
454 tor_free(ocirc
->build_state
);
456 circuit_free_cpath(ocirc
->cpath
);
457 if (ocirc
->intro_key
)
458 crypto_free_pk_env(ocirc
->intro_key
);
459 if (ocirc
->rend_data
)
460 rend_data_free(ocirc
->rend_data
);
462 or_circuit_t
*ocirc
= TO_OR_CIRCUIT(circ
);
464 memlen
= sizeof(or_circuit_t
);
465 tor_assert(circ
->magic
== OR_CIRCUIT_MAGIC
);
468 crypto_free_cipher_env(ocirc
->p_crypto
);
470 crypto_free_digest_env(ocirc
->p_digest
);
472 crypto_free_cipher_env(ocirc
->n_crypto
);
474 crypto_free_digest_env(ocirc
->n_digest
);
476 if (ocirc
->rend_splice
) {
477 or_circuit_t
*other
= ocirc
->rend_splice
;
478 tor_assert(other
->_base
.magic
== OR_CIRCUIT_MAGIC
);
479 other
->rend_splice
= NULL
;
482 /* remove from map. */
483 circuit_set_p_circid_orconn(ocirc
, 0, NULL
);
485 /* Clear cell queue _after_ removing it from the map. Otherwise our
486 * "active" checks will be violated. */
487 cell_queue_clear(ô
->p_conn_cells
);
491 extend_info_free(circ
->n_hop
);
492 tor_free(circ
->n_conn_onionskin
);
494 /* Remove from map. */
495 circuit_set_n_circid_orconn(circ
, 0, NULL
);
497 /* Clear cell queue _after_ removing it from the map. Otherwise our
498 * "active" checks will be violated. */
499 cell_queue_clear(&circ
->n_conn_cells
);
501 memset(circ
, 0xAA, memlen
); /* poison memory */
505 /** Deallocate space associated with the linked list <b>cpath</b>. */
507 circuit_free_cpath(crypt_path_t
*cpath
)
509 crypt_path_t
*victim
, *head
=cpath
;
514 /* it's a doubly linked list, so we have to notice when we've
515 * gone through it once. */
516 while (cpath
->next
&& cpath
->next
!= head
) {
518 cpath
= victim
->next
;
519 circuit_free_cpath_node(victim
);
522 circuit_free_cpath_node(cpath
);
525 /** Release all storage held by circuits. */
527 circuit_free_all(void)
530 while (global_circuitlist
) {
531 next
= global_circuitlist
->next
;
532 if (! CIRCUIT_IS_ORIGIN(global_circuitlist
)) {
533 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(global_circuitlist
);
534 while (or_circ
->resolving_streams
) {
535 edge_connection_t
*next_conn
;
536 next_conn
= or_circ
->resolving_streams
->next_stream
;
537 connection_free(TO_CONN(or_circ
->resolving_streams
));
538 or_circ
->resolving_streams
= next_conn
;
541 circuit_free(global_circuitlist
);
542 global_circuitlist
= next
;
544 if (circuits_pending_or_conns
) {
545 smartlist_free(circuits_pending_or_conns
);
546 circuits_pending_or_conns
= NULL
;
548 HT_CLEAR(orconn_circid_map
, &orconn_circid_circuit_map
);
551 /** Deallocate space associated with the cpath node <b>victim</b>. */
553 circuit_free_cpath_node(crypt_path_t
*victim
)
555 if (victim
->f_crypto
)
556 crypto_free_cipher_env(victim
->f_crypto
);
557 if (victim
->b_crypto
)
558 crypto_free_cipher_env(victim
->b_crypto
);
559 if (victim
->f_digest
)
560 crypto_free_digest_env(victim
->f_digest
);
561 if (victim
->b_digest
)
562 crypto_free_digest_env(victim
->b_digest
);
563 if (victim
->dh_handshake_state
)
564 crypto_dh_free(victim
->dh_handshake_state
);
565 if (victim
->extend_info
)
566 extend_info_free(victim
->extend_info
);
568 memset(victim
, 0xBB, sizeof(crypt_path_t
)); /* poison memory */
572 /** A helper function for circuit_dump_by_conn() below. Log a bunch
573 * of information about circuit <b>circ</b>.
576 circuit_dump_details(int severity
, circuit_t
*circ
, int conn_array_index
,
577 const char *type
, int this_circid
, int other_circid
)
579 log(severity
, LD_CIRC
, "Conn %d has %s circuit: circID %d (other side %d), "
580 "state %d (%s), born %d:",
581 conn_array_index
, type
, this_circid
, other_circid
, circ
->state
,
582 circuit_state_to_string(circ
->state
), (int)circ
->timestamp_created
);
583 if (CIRCUIT_IS_ORIGIN(circ
)) { /* circ starts at this node */
584 circuit_log_path(severity
, LD_CIRC
, TO_ORIGIN_CIRCUIT(circ
));
588 /** Log, at severity <b>severity</b>, information about each circuit
589 * that is connected to <b>conn</b>.
592 circuit_dump_by_conn(connection_t
*conn
, int severity
)
595 edge_connection_t
*tmpconn
;
597 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
598 circid_t n_circ_id
= circ
->n_circ_id
, p_circ_id
= 0;
599 if (circ
->marked_for_close
)
602 if (! CIRCUIT_IS_ORIGIN(circ
))
603 p_circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
;
605 if (! CIRCUIT_IS_ORIGIN(circ
) && TO_OR_CIRCUIT(circ
)->p_conn
&&
606 TO_CONN(TO_OR_CIRCUIT(circ
)->p_conn
) == conn
)
607 circuit_dump_details(severity
, circ
, conn
->conn_array_index
, "App-ward",
608 p_circ_id
, n_circ_id
);
609 if (CIRCUIT_IS_ORIGIN(circ
)) {
610 for (tmpconn
=TO_ORIGIN_CIRCUIT(circ
)->p_streams
; tmpconn
;
611 tmpconn
=tmpconn
->next_stream
) {
612 if (TO_CONN(tmpconn
) == conn
) {
613 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
614 "App-ward", p_circ_id
, n_circ_id
);
618 if (circ
->n_conn
&& TO_CONN(circ
->n_conn
) == conn
)
619 circuit_dump_details(severity
, circ
, conn
->conn_array_index
, "Exit-ward",
620 n_circ_id
, p_circ_id
);
621 if (! CIRCUIT_IS_ORIGIN(circ
)) {
622 for (tmpconn
=TO_OR_CIRCUIT(circ
)->n_streams
; tmpconn
;
623 tmpconn
=tmpconn
->next_stream
) {
624 if (TO_CONN(tmpconn
) == conn
) {
625 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
626 "Exit-ward", n_circ_id
, p_circ_id
);
630 if (!circ
->n_conn
&& circ
->n_hop
&&
631 tor_addr_eq(&circ
->n_hop
->addr
, &conn
->addr
) &&
632 circ
->n_hop
->port
== conn
->port
&&
633 conn
->type
== CONN_TYPE_OR
&&
634 !memcmp(TO_OR_CONN(conn
)->identity_digest
,
635 circ
->n_hop
->identity_digest
, DIGEST_LEN
)) {
636 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
637 (circ
->state
== CIRCUIT_STATE_OPEN
&&
638 !CIRCUIT_IS_ORIGIN(circ
)) ?
639 "Endpoint" : "Pending",
640 n_circ_id
, p_circ_id
);
645 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
646 * such circuit exists. */
648 circuit_get_by_global_id(uint32_t id
)
651 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
652 if (CIRCUIT_IS_ORIGIN(circ
) &&
653 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
== id
) {
654 if (circ
->marked_for_close
)
657 return TO_ORIGIN_CIRCUIT(circ
);
663 /** Return a circ such that:
664 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
665 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
666 * Return NULL if no such circuit exists.
668 static INLINE circuit_t
*
669 circuit_get_by_circid_orconn_impl(circid_t circ_id
, or_connection_t
*conn
)
671 orconn_circid_circuit_map_t search
;
672 orconn_circid_circuit_map_t
*found
;
674 if (_last_circid_orconn_ent
&&
675 circ_id
== _last_circid_orconn_ent
->circ_id
&&
676 conn
== _last_circid_orconn_ent
->or_conn
) {
677 found
= _last_circid_orconn_ent
;
679 search
.circ_id
= circ_id
;
680 search
.or_conn
= conn
;
681 found
= HT_FIND(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
682 _last_circid_orconn_ent
= found
;
684 if (found
&& found
->circuit
)
685 return found
->circuit
;
689 /* The rest of this checks for bugs. Disabled by default. */
692 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
693 if (! CIRCUIT_IS_ORIGIN(circ
)) {
694 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
695 if (or_circ
->p_conn
== conn
&& or_circ
->p_circ_id
== circ_id
) {
697 "circuit matches p_conn, but not in hash table (Bug!)");
701 if (circ
->n_conn
== conn
&& circ
->n_circ_id
== circ_id
) {
703 "circuit matches n_conn, but not in hash table (Bug!)");
711 /** Return a circ such that:
712 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
713 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
714 * - circ is not marked for close.
715 * Return NULL if no such circuit exists.
718 circuit_get_by_circid_orconn(circid_t circ_id
, or_connection_t
*conn
)
720 circuit_t
*circ
= circuit_get_by_circid_orconn_impl(circ_id
, conn
);
721 if (!circ
|| circ
->marked_for_close
)
727 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
728 * circuit, marked or not, on <b>conn</b>. */
730 circuit_id_in_use_on_orconn(circid_t circ_id
, or_connection_t
*conn
)
732 return circuit_get_by_circid_orconn_impl(circ_id
, conn
) != NULL
;
735 /** Return the circuit that a given edge connection is using. */
737 circuit_get_by_edge_conn(edge_connection_t
*conn
)
741 circ
= conn
->on_circuit
;
743 (CIRCUIT_IS_ORIGIN(circ
) ? circ
->magic
== ORIGIN_CIRCUIT_MAGIC
744 : circ
->magic
== OR_CIRCUIT_MAGIC
));
749 /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
750 * circuit from the orconn,circid map, and mark it for close if it hasn't
751 * been marked already.
754 circuit_unlink_all_from_or_conn(or_connection_t
*conn
, int reason
)
758 connection_or_unlink_all_active_circs(conn
);
760 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
762 if (circ
->n_conn
== conn
) {
763 circuit_set_n_circid_orconn(circ
, 0, NULL
);
766 if (! CIRCUIT_IS_ORIGIN(circ
)) {
767 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
768 if (or_circ
->p_conn
== conn
) {
769 circuit_set_p_circid_orconn(or_circ
, 0, NULL
);
773 if (mark
&& !circ
->marked_for_close
)
774 circuit_mark_for_close(circ
, reason
);
778 /** Return a circ such that:
779 * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
780 * - circ-\>purpose is equal to <b>purpose</b>.
782 * Return NULL if no such circuit exists.
785 circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
)
789 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
791 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
792 if (!circ
->marked_for_close
&&
793 circ
->purpose
== purpose
) {
794 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
795 if (ocirc
->rend_data
&&
796 !rend_cmp_service_ids(rend_query
,
797 ocirc
->rend_data
->onion_address
))
804 /** Return the first circuit originating here in global_circuitlist after
805 * <b>start</b> whose purpose is <b>purpose</b>, and where
806 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
807 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
810 circuit_get_next_by_pk_and_purpose(origin_circuit_t
*start
,
811 const char *digest
, uint8_t purpose
)
814 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
816 circ
= global_circuitlist
;
818 circ
= TO_CIRCUIT(start
)->next
;
820 for ( ; circ
; circ
= circ
->next
) {
821 if (circ
->marked_for_close
)
823 if (circ
->purpose
!= purpose
)
826 return TO_ORIGIN_CIRCUIT(circ
);
827 else if (TO_ORIGIN_CIRCUIT(circ
)->rend_data
&&
828 !memcmp(TO_ORIGIN_CIRCUIT(circ
)->rend_data
->rend_pk_digest
,
830 return TO_ORIGIN_CIRCUIT(circ
);
835 /** Return the first OR circuit in the global list whose purpose is
836 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
838 static or_circuit_t
*
839 circuit_get_by_rend_token_and_purpose(uint8_t purpose
, const char *token
,
843 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
844 if (! circ
->marked_for_close
&&
845 circ
->purpose
== purpose
&&
846 ! memcmp(TO_OR_CIRCUIT(circ
)->rend_token
, token
, len
))
847 return TO_OR_CIRCUIT(circ
);
852 /** Return the circuit waiting for a rendezvous with the provided cookie.
853 * Return NULL if no such circuit is found.
856 circuit_get_rendezvous(const char *cookie
)
858 return circuit_get_by_rend_token_and_purpose(
859 CIRCUIT_PURPOSE_REND_POINT_WAITING
,
860 cookie
, REND_COOKIE_LEN
);
863 /** Return the circuit waiting for intro cells of the given digest.
864 * Return NULL if no such circuit is found.
867 circuit_get_intro_point(const char *digest
)
869 return circuit_get_by_rend_token_and_purpose(
870 CIRCUIT_PURPOSE_INTRO_POINT
, digest
,
874 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
875 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
876 * flags in <b>flags</b>, and if info is defined, does not already use info
877 * as any of its hops; or NULL if no circuit fits this description.
879 * The <b>purpose</b> argument (currently ignored) refers to the purpose of
880 * the circuit we want to create, not the purpose of the circuit we want to
883 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
886 circuit_find_to_cannibalize(uint8_t purpose
, extend_info_t
*info
,
890 origin_circuit_t
*best
=NULL
;
891 int need_uptime
= (flags
& CIRCLAUNCH_NEED_UPTIME
) != 0;
892 int need_capacity
= (flags
& CIRCLAUNCH_NEED_CAPACITY
) != 0;
893 int internal
= (flags
& CIRCLAUNCH_IS_INTERNAL
) != 0;
896 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
897 "capacity %d, internal %d",
898 purpose
, need_uptime
, need_capacity
, internal
);
900 for (_circ
=global_circuitlist
; _circ
; _circ
= _circ
->next
) {
901 if (CIRCUIT_IS_ORIGIN(_circ
) &&
902 _circ
->state
== CIRCUIT_STATE_OPEN
&&
903 !_circ
->marked_for_close
&&
904 _circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
905 !_circ
->timestamp_dirty
) {
906 origin_circuit_t
*circ
= TO_ORIGIN_CIRCUIT(_circ
);
907 if ((!need_uptime
|| circ
->build_state
->need_uptime
) &&
908 (!need_capacity
|| circ
->build_state
->need_capacity
) &&
909 (internal
== circ
->build_state
->is_internal
) &&
910 circ
->remaining_relay_early_cells
) {
912 /* need to make sure we don't duplicate hops */
913 crypt_path_t
*hop
= circ
->cpath
;
914 routerinfo_t
*ri1
= router_get_by_digest(info
->identity_digest
);
917 if (!memcmp(hop
->extend_info
->identity_digest
,
918 info
->identity_digest
, DIGEST_LEN
))
921 (ri2
= router_get_by_digest(hop
->extend_info
->identity_digest
))
922 && routers_in_same_family(ri1
, ri2
))
925 } while (hop
!=circ
->cpath
);
927 if (!best
|| (best
->build_state
->need_uptime
&& !need_uptime
))
936 /** Return the number of hops in circuit's path. */
938 circuit_get_cpath_len(origin_circuit_t
*circ
)
941 if (circ
&& circ
->cpath
) {
942 crypt_path_t
*cpath
, *cpath_next
= NULL
;
943 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
944 cpath_next
= cpath
->next
;
951 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
952 * aren't that many hops in the list. */
954 circuit_get_cpath_hop(origin_circuit_t
*circ
, int hopnum
)
956 if (circ
&& circ
->cpath
&& hopnum
> 0) {
957 crypt_path_t
*cpath
, *cpath_next
= NULL
;
958 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
959 cpath_next
= cpath
->next
;
967 /** Go through the circuitlist; mark-for-close each circuit that starts
968 * at us but has not yet been used. */
970 circuit_mark_all_unused_circs(void)
974 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
975 if (CIRCUIT_IS_ORIGIN(circ
) &&
976 !circ
->marked_for_close
&&
977 !circ
->timestamp_dirty
)
978 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
982 /** Go through the circuitlist; for each circuit that starts at us
983 * and is dirty, frob its timestamp_dirty so we won't use it for any
986 * This is useful for letting the user change pseudonyms, so new
987 * streams will not be linkable to old streams.
990 circuit_expire_all_dirty_circs(void)
993 or_options_t
*options
= get_options();
995 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
996 if (CIRCUIT_IS_ORIGIN(circ
) &&
997 !circ
->marked_for_close
&&
998 circ
->timestamp_dirty
)
999 circ
->timestamp_dirty
-= options
->MaxCircuitDirtiness
;
1003 /** Mark <b>circ</b> to be closed next time we call
1004 * circuit_close_all_marked(). Do any cleanup needed:
1005 * - If state is onionskin_pending, remove circ from the onion_pending
1007 * - If circ isn't open yet: call circuit_build_failed() if we're
1008 * the origin, and in either case call circuit_rep_hist_note_result()
1010 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
1011 * just tried from our list of intro points for that service
1013 * - Send appropriate destroys and edge_destroys for conns and
1014 * streams attached to circ.
1015 * - If circ->rend_splice is set (we are the midpoint of a joined
1016 * rendezvous stream), then mark the other circuit to close as well.
1019 _circuit_mark_for_close(circuit_t
*circ
, int reason
, int line
,
1022 int orig_reason
= reason
; /* Passed to the controller */
1023 assert_circuit_ok(circ
);
1027 if (circ
->marked_for_close
) {
1028 log(LOG_WARN
,LD_BUG
,
1029 "Duplicate call to circuit_mark_for_close at %s:%d"
1030 " (first at %s:%d)", file
, line
,
1031 circ
->marked_for_close_file
, circ
->marked_for_close
);
1034 if (reason
== END_CIRC_AT_ORIGIN
) {
1035 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1036 log_warn(LD_BUG
, "Specified 'at-origin' non-reason for ending circuit, "
1037 "but circuit was not at origin. (called %s:%d, purpose=%d)",
1038 file
, line
, circ
->purpose
);
1040 reason
= END_CIRC_REASON_NONE
;
1042 if (CIRCUIT_IS_ORIGIN(circ
)) {
1043 /* We don't send reasons when closing circuits at the origin. */
1044 reason
= END_CIRC_REASON_NONE
;
1047 if (reason
& END_CIRC_REASON_FLAG_REMOTE
)
1048 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
1050 if (reason
< _END_CIRC_REASON_MIN
|| reason
> _END_CIRC_REASON_MAX
) {
1051 if (!(orig_reason
& END_CIRC_REASON_FLAG_REMOTE
))
1052 log_warn(LD_BUG
, "Reason %d out of range at %s:%d", reason
, file
, line
);
1053 reason
= END_CIRC_REASON_NONE
;
1056 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
1057 onion_pending_remove(TO_OR_CIRCUIT(circ
));
1059 /* If the circuit ever became OPEN, we sent it to the reputation history
1060 * module then. If it isn't OPEN, we send it there now to remember which
1061 * links worked and which didn't.
1063 if (circ
->state
!= CIRCUIT_STATE_OPEN
) {
1064 if (CIRCUIT_IS_ORIGIN(circ
)) {
1065 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1066 circuit_build_failed(ocirc
); /* take actions if necessary */
1067 circuit_rep_hist_note_result(ocirc
);
1070 if (circ
->state
== CIRCUIT_STATE_OR_WAIT
) {
1071 if (circuits_pending_or_conns
)
1072 smartlist_remove(circuits_pending_or_conns
, circ
);
1074 if (CIRCUIT_IS_ORIGIN(circ
)) {
1075 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ
),
1076 (circ
->state
== CIRCUIT_STATE_OPEN
)?CIRC_EVENT_CLOSED
:CIRC_EVENT_FAILED
,
1079 if (circ
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1080 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1081 tor_assert(circ
->state
== CIRCUIT_STATE_OPEN
);
1082 tor_assert(ocirc
->build_state
->chosen_exit
);
1083 tor_assert(ocirc
->rend_data
);
1084 /* treat this like getting a nack from it */
1085 log_info(LD_REND
, "Failed intro circ %s to %s (awaiting ack). "
1086 "Removing from descriptor.",
1087 safe_str(ocirc
->rend_data
->onion_address
),
1088 safe_str(build_state_get_exit_nickname(ocirc
->build_state
)));
1089 rend_client_remove_intro_point(ocirc
->build_state
->chosen_exit
,
1093 connection_or_send_destroy(circ
->n_circ_id
, circ
->n_conn
, reason
);
1095 if (! CIRCUIT_IS_ORIGIN(circ
)) {
1096 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1097 edge_connection_t
*conn
;
1098 for (conn
=or_circ
->n_streams
; conn
; conn
=conn
->next_stream
)
1099 connection_edge_destroy(or_circ
->p_circ_id
, conn
);
1100 or_circ
->n_streams
= NULL
;
1102 while (or_circ
->resolving_streams
) {
1103 conn
= or_circ
->resolving_streams
;
1104 or_circ
->resolving_streams
= conn
->next_stream
;
1105 if (!conn
->_base
.marked_for_close
) {
1106 /* The client will see a DESTROY, and infer that the connections
1107 * are closing because the circuit is getting torn down. No need
1108 * to send an end cell. */
1109 conn
->edge_has_sent_end
= 1;
1110 conn
->end_reason
= END_STREAM_REASON_DESTROY
;
1111 conn
->end_reason
|= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
;
1112 connection_mark_for_close(TO_CONN(conn
));
1114 conn
->on_circuit
= NULL
;
1117 if (or_circ
->p_conn
)
1118 connection_or_send_destroy(or_circ
->p_circ_id
, or_circ
->p_conn
, reason
);
1120 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1121 edge_connection_t
*conn
;
1122 for (conn
=ocirc
->p_streams
; conn
; conn
=conn
->next_stream
)
1123 connection_edge_destroy(circ
->n_circ_id
, conn
);
1124 ocirc
->p_streams
= NULL
;
1127 circ
->marked_for_close
= line
;
1128 circ
->marked_for_close_file
= file
;
1130 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1131 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1132 if (or_circ
->rend_splice
) {
1133 if (!or_circ
->rend_splice
->_base
.marked_for_close
) {
1134 /* do this after marking this circuit, to avoid infinite recursion. */
1135 circuit_mark_for_close(TO_CIRCUIT(or_circ
->rend_splice
), reason
);
1137 or_circ
->rend_splice
= NULL
;
1142 /** Verify that cpath layer <b>cp</b> has all of its invariants
1143 * correct. Trigger an assert if anything is invalid.
1146 assert_cpath_layer_ok(const crypt_path_t
*cp
)
1148 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1149 // tor_assert(cp->port);
1151 tor_assert(cp
->magic
== CRYPT_PATH_MAGIC
);
1154 case CPATH_STATE_OPEN
:
1155 tor_assert(cp
->f_crypto
);
1156 tor_assert(cp
->b_crypto
);
1158 case CPATH_STATE_CLOSED
:
1159 tor_assert(!cp
->dh_handshake_state
);
1161 case CPATH_STATE_AWAITING_KEYS
:
1162 /* tor_assert(cp->dh_handshake_state); */
1165 log_fn(LOG_ERR
, LD_BUG
, "Unexpected state %d", cp
->state
);
1168 tor_assert(cp
->package_window
>= 0);
1169 tor_assert(cp
->deliver_window
>= 0);
1172 /** Verify that cpath <b>cp</b> has all of its invariants
1173 * correct. Trigger an assert if anything is invalid.
1176 assert_cpath_ok(const crypt_path_t
*cp
)
1178 const crypt_path_t
*start
= cp
;
1181 assert_cpath_layer_ok(cp
);
1182 /* layers must be in sequence of: "open* awaiting? closed*" */
1184 if (cp
->state
== CPATH_STATE_AWAITING_KEYS
) {
1185 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1186 } else if (cp
->state
== CPATH_STATE_OPEN
) {
1187 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1192 } while (cp
!= start
);
1195 /** Verify that circuit <b>c</b> has all of its invariants
1196 * correct. Trigger an assert if anything is invalid.
1199 assert_circuit_ok(const circuit_t
*c
)
1201 edge_connection_t
*conn
;
1202 const or_circuit_t
*or_circ
= NULL
;
1203 const origin_circuit_t
*origin_circ
= NULL
;
1206 tor_assert(c
->magic
== ORIGIN_CIRCUIT_MAGIC
|| c
->magic
== OR_CIRCUIT_MAGIC
);
1207 tor_assert(c
->purpose
>= _CIRCUIT_PURPOSE_MIN
&&
1208 c
->purpose
<= _CIRCUIT_PURPOSE_MAX
);
1211 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1212 * never understand. -NM. */
1213 circuit_t
*nonconst_circ
= (circuit_t
*) c
;
1214 if (CIRCUIT_IS_ORIGIN(c
))
1215 origin_circ
= TO_ORIGIN_CIRCUIT(nonconst_circ
);
1217 or_circ
= TO_OR_CIRCUIT(nonconst_circ
);
1221 tor_assert(!c
->n_hop
);
1224 /* We use the _impl variant here to make sure we don't fail on marked
1225 * circuits, which would not be returned by the regular function. */
1226 circuit_t
*c2
= circuit_get_by_circid_orconn_impl(c
->n_circ_id
,
1228 tor_assert(c
== c2
);
1231 if (or_circ
&& or_circ
->p_conn
) {
1232 if (or_circ
->p_circ_id
) {
1234 circuit_t
*c2
= circuit_get_by_circid_orconn_impl(or_circ
->p_circ_id
,
1236 tor_assert(c
== c2
);
1239 #if 0 /* false now that rendezvous exits are attached to p_streams */
1241 for (conn
= origin_circ
->p_streams
; conn
; conn
= conn
->next_stream
)
1242 tor_assert(conn
->_base
.type
== CONN_TYPE_AP
);
1245 for (conn
= or_circ
->n_streams
; conn
; conn
= conn
->next_stream
)
1246 tor_assert(conn
->_base
.type
== CONN_TYPE_EXIT
);
1248 tor_assert(c
->deliver_window
>= 0);
1249 tor_assert(c
->package_window
>= 0);
1250 if (c
->state
== CIRCUIT_STATE_OPEN
) {
1251 tor_assert(!c
->n_conn_onionskin
);
1253 tor_assert(or_circ
->n_crypto
);
1254 tor_assert(or_circ
->p_crypto
);
1255 tor_assert(or_circ
->n_digest
);
1256 tor_assert(or_circ
->p_digest
);
1259 if (c
->state
== CIRCUIT_STATE_OR_WAIT
&& !c
->marked_for_close
) {
1260 tor_assert(circuits_pending_or_conns
&&
1261 smartlist_isin(circuits_pending_or_conns
, c
));
1263 tor_assert(!circuits_pending_or_conns
||
1264 !smartlist_isin(circuits_pending_or_conns
, c
));
1266 if (origin_circ
&& origin_circ
->cpath
) {
1267 assert_cpath_ok(origin_circ
->cpath
);
1269 if (c
->purpose
== CIRCUIT_PURPOSE_REND_ESTABLISHED
) {
1270 tor_assert(or_circ
);
1271 if (!c
->marked_for_close
) {
1272 tor_assert(or_circ
->rend_splice
);
1273 tor_assert(or_circ
->rend_splice
->rend_splice
== or_circ
);
1275 tor_assert(or_circ
->rend_splice
!= or_circ
);
1277 tor_assert(!or_circ
|| !or_circ
->rend_splice
);