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-2010, 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_C_MEASURE_TIMEOUT
:
357 case CIRCUIT_PURPOSE_CONTROLLER
:
361 tor_snprintf(buf
, sizeof(buf
), "UNKNOWN_%d", (int)purpose
);
366 /** Pick a reasonable package_window to start out for our circuits.
367 * Originally this was hard-coded at 1000, but now the consensus votes
368 * on the answer. See proposal 168. */
370 circuit_initial_package_window(void)
372 int32_t num
= networkstatus_get_param(NULL
, "circwindow", CIRCWINDOW_START
);
373 /* If the consensus tells us a negative number, we'd assert. */
375 num
= CIRCWINDOW_START
;
379 /** Initialize the common elements in a circuit_t, and add it to the global
382 init_circuit_base(circuit_t
*circ
)
384 circ
->timestamp_created
= time(NULL
);
385 tor_gettimeofday(&circ
->highres_created
);
387 circ
->package_window
= circuit_initial_package_window();
388 circ
->deliver_window
= CIRCWINDOW_START
;
390 /* Initialize the cell_ewma_t structure */
391 circ
->n_cell_ewma
.last_adjusted_tick
= cell_ewma_get_tick();
392 circ
->n_cell_ewma
.cell_count
= 0.0;
393 circ
->n_cell_ewma
.heap_index
= -1;
394 circ
->n_cell_ewma
.is_for_p_conn
= 0;
399 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
400 * and <b>p_conn</b>. Add it to the global circuit list.
403 origin_circuit_new(void)
405 origin_circuit_t
*circ
;
406 /* never zero, since a global ID of 0 is treated specially by the
408 static uint32_t n_circuits_allocated
= 1;
410 circ
= tor_malloc_zero(sizeof(origin_circuit_t
));
411 circ
->_base
.magic
= ORIGIN_CIRCUIT_MAGIC
;
413 circ
->next_stream_id
= crypto_rand_int(1<<16);
414 circ
->global_identifier
= n_circuits_allocated
++;
415 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
416 circ
->remaining_relay_early_cells
-= crypto_rand_int(2);
418 init_circuit_base(TO_CIRCUIT(circ
));
420 circ_times
.last_circ_at
= approx_time();
425 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
426 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
428 or_circuit_new(circid_t p_circ_id
, or_connection_t
*p_conn
)
433 circ
= tor_malloc_zero(sizeof(or_circuit_t
));
434 circ
->_base
.magic
= OR_CIRCUIT_MAGIC
;
437 circuit_set_p_circid_orconn(circ
, p_circ_id
, p_conn
);
439 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
441 init_circuit_base(TO_CIRCUIT(circ
));
443 /* Initialize the cell_ewma_t structure */
445 /* Initialize the cell counts to 0 */
446 circ
->p_cell_ewma
.cell_count
= 0.0;
447 circ
->p_cell_ewma
.last_adjusted_tick
= cell_ewma_get_tick();
448 circ
->p_cell_ewma
.is_for_p_conn
= 1;
450 /* It's not in any heap yet. */
451 circ
->p_cell_ewma
.heap_index
= -1;
456 /** Deallocate space associated with circ.
459 circuit_free(circuit_t
*circ
)
466 if (CIRCUIT_IS_ORIGIN(circ
)) {
467 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
469 memlen
= sizeof(origin_circuit_t
);
470 tor_assert(circ
->magic
== ORIGIN_CIRCUIT_MAGIC
);
471 if (ocirc
->build_state
) {
472 extend_info_free(ocirc
->build_state
->chosen_exit
);
473 circuit_free_cpath_node(ocirc
->build_state
->pending_final_cpath
);
475 tor_free(ocirc
->build_state
);
477 circuit_free_cpath(ocirc
->cpath
);
479 crypto_free_pk_env(ocirc
->intro_key
);
480 rend_data_free(ocirc
->rend_data
);
482 or_circuit_t
*ocirc
= TO_OR_CIRCUIT(circ
);
483 /* Remember cell statistics for this circuit before deallocating. */
484 if (get_options()->CellStatistics
)
485 rep_hist_buffer_stats_add_circ(circ
, time(NULL
));
487 memlen
= sizeof(or_circuit_t
);
488 tor_assert(circ
->magic
== OR_CIRCUIT_MAGIC
);
490 crypto_free_cipher_env(ocirc
->p_crypto
);
491 crypto_free_digest_env(ocirc
->p_digest
);
492 crypto_free_cipher_env(ocirc
->n_crypto
);
493 crypto_free_digest_env(ocirc
->n_digest
);
495 if (ocirc
->rend_splice
) {
496 or_circuit_t
*other
= ocirc
->rend_splice
;
497 tor_assert(other
->_base
.magic
== OR_CIRCUIT_MAGIC
);
498 other
->rend_splice
= NULL
;
501 /* remove from map. */
502 circuit_set_p_circid_orconn(ocirc
, 0, NULL
);
504 /* Clear cell queue _after_ removing it from the map. Otherwise our
505 * "active" checks will be violated. */
506 cell_queue_clear(ô
->p_conn_cells
);
509 extend_info_free(circ
->n_hop
);
510 tor_free(circ
->n_conn_onionskin
);
512 /* Remove from map. */
513 circuit_set_n_circid_orconn(circ
, 0, NULL
);
515 /* Clear cell queue _after_ removing it from the map. Otherwise our
516 * "active" checks will be violated. */
517 cell_queue_clear(&circ
->n_conn_cells
);
519 memset(mem
, 0xAA, memlen
); /* poison memory */
523 /** Deallocate space associated with the linked list <b>cpath</b>. */
525 circuit_free_cpath(crypt_path_t
*cpath
)
527 crypt_path_t
*victim
, *head
=cpath
;
532 /* it's a doubly linked list, so we have to notice when we've
533 * gone through it once. */
534 while (cpath
->next
&& cpath
->next
!= head
) {
536 cpath
= victim
->next
;
537 circuit_free_cpath_node(victim
);
540 circuit_free_cpath_node(cpath
);
543 /** Release all storage held by circuits. */
545 circuit_free_all(void)
548 while (global_circuitlist
) {
549 next
= global_circuitlist
->next
;
550 if (! CIRCUIT_IS_ORIGIN(global_circuitlist
)) {
551 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(global_circuitlist
);
552 while (or_circ
->resolving_streams
) {
553 edge_connection_t
*next_conn
;
554 next_conn
= or_circ
->resolving_streams
->next_stream
;
555 connection_free(TO_CONN(or_circ
->resolving_streams
));
556 or_circ
->resolving_streams
= next_conn
;
559 circuit_free(global_circuitlist
);
560 global_circuitlist
= next
;
563 smartlist_free(circuits_pending_or_conns
);
564 circuits_pending_or_conns
= NULL
;
566 HT_CLEAR(orconn_circid_map
, &orconn_circid_circuit_map
);
569 /** Deallocate space associated with the cpath node <b>victim</b>. */
571 circuit_free_cpath_node(crypt_path_t
*victim
)
576 crypto_free_cipher_env(victim
->f_crypto
);
577 crypto_free_cipher_env(victim
->b_crypto
);
578 crypto_free_digest_env(victim
->f_digest
);
579 crypto_free_digest_env(victim
->b_digest
);
580 crypto_dh_free(victim
->dh_handshake_state
);
581 extend_info_free(victim
->extend_info
);
583 memset(victim
, 0xBB, sizeof(crypt_path_t
)); /* poison memory */
587 /** A helper function for circuit_dump_by_conn() below. Log a bunch
588 * of information about circuit <b>circ</b>.
591 circuit_dump_details(int severity
, circuit_t
*circ
, int conn_array_index
,
592 const char *type
, int this_circid
, int other_circid
)
594 log(severity
, LD_CIRC
, "Conn %d has %s circuit: circID %d (other side %d), "
595 "state %d (%s), born %d:",
596 conn_array_index
, type
, this_circid
, other_circid
, circ
->state
,
597 circuit_state_to_string(circ
->state
), (int)circ
->timestamp_created
);
598 if (CIRCUIT_IS_ORIGIN(circ
)) { /* circ starts at this node */
599 circuit_log_path(severity
, LD_CIRC
, TO_ORIGIN_CIRCUIT(circ
));
603 /** Log, at severity <b>severity</b>, information about each circuit
604 * that is connected to <b>conn</b>.
607 circuit_dump_by_conn(connection_t
*conn
, int severity
)
610 edge_connection_t
*tmpconn
;
612 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
613 circid_t n_circ_id
= circ
->n_circ_id
, p_circ_id
= 0;
614 if (circ
->marked_for_close
)
617 if (! CIRCUIT_IS_ORIGIN(circ
))
618 p_circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
;
620 if (! CIRCUIT_IS_ORIGIN(circ
) && TO_OR_CIRCUIT(circ
)->p_conn
&&
621 TO_CONN(TO_OR_CIRCUIT(circ
)->p_conn
) == conn
)
622 circuit_dump_details(severity
, circ
, conn
->conn_array_index
, "App-ward",
623 p_circ_id
, n_circ_id
);
624 if (CIRCUIT_IS_ORIGIN(circ
)) {
625 for (tmpconn
=TO_ORIGIN_CIRCUIT(circ
)->p_streams
; tmpconn
;
626 tmpconn
=tmpconn
->next_stream
) {
627 if (TO_CONN(tmpconn
) == conn
) {
628 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
629 "App-ward", p_circ_id
, n_circ_id
);
633 if (circ
->n_conn
&& TO_CONN(circ
->n_conn
) == conn
)
634 circuit_dump_details(severity
, circ
, conn
->conn_array_index
, "Exit-ward",
635 n_circ_id
, p_circ_id
);
636 if (! CIRCUIT_IS_ORIGIN(circ
)) {
637 for (tmpconn
=TO_OR_CIRCUIT(circ
)->n_streams
; tmpconn
;
638 tmpconn
=tmpconn
->next_stream
) {
639 if (TO_CONN(tmpconn
) == conn
) {
640 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
641 "Exit-ward", n_circ_id
, p_circ_id
);
645 if (!circ
->n_conn
&& circ
->n_hop
&&
646 tor_addr_eq(&circ
->n_hop
->addr
, &conn
->addr
) &&
647 circ
->n_hop
->port
== conn
->port
&&
648 conn
->type
== CONN_TYPE_OR
&&
649 !memcmp(TO_OR_CONN(conn
)->identity_digest
,
650 circ
->n_hop
->identity_digest
, DIGEST_LEN
)) {
651 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
652 (circ
->state
== CIRCUIT_STATE_OPEN
&&
653 !CIRCUIT_IS_ORIGIN(circ
)) ?
654 "Endpoint" : "Pending",
655 n_circ_id
, p_circ_id
);
660 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
661 * such circuit exists. */
663 circuit_get_by_global_id(uint32_t id
)
666 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
667 if (CIRCUIT_IS_ORIGIN(circ
) &&
668 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
== id
) {
669 if (circ
->marked_for_close
)
672 return TO_ORIGIN_CIRCUIT(circ
);
678 /** Return a circ such that:
679 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
680 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
681 * Return NULL if no such circuit exists.
683 static INLINE circuit_t
*
684 circuit_get_by_circid_orconn_impl(circid_t circ_id
, or_connection_t
*conn
)
686 orconn_circid_circuit_map_t search
;
687 orconn_circid_circuit_map_t
*found
;
689 if (_last_circid_orconn_ent
&&
690 circ_id
== _last_circid_orconn_ent
->circ_id
&&
691 conn
== _last_circid_orconn_ent
->or_conn
) {
692 found
= _last_circid_orconn_ent
;
694 search
.circ_id
= circ_id
;
695 search
.or_conn
= conn
;
696 found
= HT_FIND(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
697 _last_circid_orconn_ent
= found
;
699 if (found
&& found
->circuit
)
700 return found
->circuit
;
704 /* The rest of this checks for bugs. Disabled by default. */
707 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
708 if (! CIRCUIT_IS_ORIGIN(circ
)) {
709 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
710 if (or_circ
->p_conn
== conn
&& or_circ
->p_circ_id
== circ_id
) {
712 "circuit matches p_conn, but not in hash table (Bug!)");
716 if (circ
->n_conn
== conn
&& circ
->n_circ_id
== circ_id
) {
718 "circuit matches n_conn, but not in hash table (Bug!)");
726 /** Return a circ such that:
727 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
728 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
729 * - circ is not marked for close.
730 * Return NULL if no such circuit exists.
733 circuit_get_by_circid_orconn(circid_t circ_id
, or_connection_t
*conn
)
735 circuit_t
*circ
= circuit_get_by_circid_orconn_impl(circ_id
, conn
);
736 if (!circ
|| circ
->marked_for_close
)
742 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
743 * circuit, marked or not, on <b>conn</b>. */
745 circuit_id_in_use_on_orconn(circid_t circ_id
, or_connection_t
*conn
)
747 return circuit_get_by_circid_orconn_impl(circ_id
, conn
) != NULL
;
750 /** Return the circuit that a given edge connection is using. */
752 circuit_get_by_edge_conn(edge_connection_t
*conn
)
756 circ
= conn
->on_circuit
;
758 (CIRCUIT_IS_ORIGIN(circ
) ? circ
->magic
== ORIGIN_CIRCUIT_MAGIC
759 : circ
->magic
== OR_CIRCUIT_MAGIC
));
764 /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
765 * circuit from the orconn,circid map, and mark it for close if it hasn't
766 * been marked already.
769 circuit_unlink_all_from_or_conn(or_connection_t
*conn
, int reason
)
773 connection_or_unlink_all_active_circs(conn
);
775 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
777 if (circ
->n_conn
== conn
) {
778 circuit_set_n_circid_orconn(circ
, 0, NULL
);
781 if (! CIRCUIT_IS_ORIGIN(circ
)) {
782 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
783 if (or_circ
->p_conn
== conn
) {
784 circuit_set_p_circid_orconn(or_circ
, 0, NULL
);
788 if (mark
&& !circ
->marked_for_close
)
789 circuit_mark_for_close(circ
, reason
);
793 /** Return a circ such that:
794 * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
795 * - circ-\>purpose is equal to <b>purpose</b>.
797 * Return NULL if no such circuit exists.
800 circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
)
804 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
806 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
807 if (!circ
->marked_for_close
&&
808 circ
->purpose
== purpose
) {
809 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
810 if (ocirc
->rend_data
&&
811 !rend_cmp_service_ids(rend_query
,
812 ocirc
->rend_data
->onion_address
))
819 /** Return the first circuit originating here in global_circuitlist after
820 * <b>start</b> whose purpose is <b>purpose</b>, and where
821 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
822 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
825 circuit_get_next_by_pk_and_purpose(origin_circuit_t
*start
,
826 const char *digest
, uint8_t purpose
)
829 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
831 circ
= global_circuitlist
;
833 circ
= TO_CIRCUIT(start
)->next
;
835 for ( ; circ
; circ
= circ
->next
) {
836 if (circ
->marked_for_close
)
838 if (circ
->purpose
!= purpose
)
841 return TO_ORIGIN_CIRCUIT(circ
);
842 else if (TO_ORIGIN_CIRCUIT(circ
)->rend_data
&&
843 !memcmp(TO_ORIGIN_CIRCUIT(circ
)->rend_data
->rend_pk_digest
,
845 return TO_ORIGIN_CIRCUIT(circ
);
850 /** Return the first OR circuit in the global list whose purpose is
851 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
853 static or_circuit_t
*
854 circuit_get_by_rend_token_and_purpose(uint8_t purpose
, const char *token
,
858 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
859 if (! circ
->marked_for_close
&&
860 circ
->purpose
== purpose
&&
861 ! memcmp(TO_OR_CIRCUIT(circ
)->rend_token
, token
, len
))
862 return TO_OR_CIRCUIT(circ
);
867 /** Return the circuit waiting for a rendezvous with the provided cookie.
868 * Return NULL if no such circuit is found.
871 circuit_get_rendezvous(const char *cookie
)
873 return circuit_get_by_rend_token_and_purpose(
874 CIRCUIT_PURPOSE_REND_POINT_WAITING
,
875 cookie
, REND_COOKIE_LEN
);
878 /** Return the circuit waiting for intro cells of the given digest.
879 * Return NULL if no such circuit is found.
882 circuit_get_intro_point(const char *digest
)
884 return circuit_get_by_rend_token_and_purpose(
885 CIRCUIT_PURPOSE_INTRO_POINT
, digest
,
889 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
890 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
891 * flags in <b>flags</b>, and if info is defined, does not already use info
892 * as any of its hops; or NULL if no circuit fits this description.
894 * The <b>purpose</b> argument (currently ignored) refers to the purpose of
895 * the circuit we want to create, not the purpose of the circuit we want to
898 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
901 circuit_find_to_cannibalize(uint8_t purpose
, extend_info_t
*info
,
905 origin_circuit_t
*best
=NULL
;
906 int need_uptime
= (flags
& CIRCLAUNCH_NEED_UPTIME
) != 0;
907 int need_capacity
= (flags
& CIRCLAUNCH_NEED_CAPACITY
) != 0;
908 int internal
= (flags
& CIRCLAUNCH_IS_INTERNAL
) != 0;
910 /* Make sure we're not trying to create a onehop circ by
911 * cannibalization. */
912 tor_assert(!(flags
& CIRCLAUNCH_ONEHOP_TUNNEL
));
915 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
916 "capacity %d, internal %d",
917 purpose
, need_uptime
, need_capacity
, internal
);
919 for (_circ
=global_circuitlist
; _circ
; _circ
= _circ
->next
) {
920 if (CIRCUIT_IS_ORIGIN(_circ
) &&
921 _circ
->state
== CIRCUIT_STATE_OPEN
&&
922 !_circ
->marked_for_close
&&
923 _circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
924 !_circ
->timestamp_dirty
) {
925 origin_circuit_t
*circ
= TO_ORIGIN_CIRCUIT(_circ
);
926 if ((!need_uptime
|| circ
->build_state
->need_uptime
) &&
927 (!need_capacity
|| circ
->build_state
->need_capacity
) &&
928 (internal
== circ
->build_state
->is_internal
) &&
929 circ
->remaining_relay_early_cells
&&
930 !circ
->build_state
->onehop_tunnel
) {
932 /* need to make sure we don't duplicate hops */
933 crypt_path_t
*hop
= circ
->cpath
;
934 routerinfo_t
*ri1
= router_get_by_digest(info
->identity_digest
);
937 if (!memcmp(hop
->extend_info
->identity_digest
,
938 info
->identity_digest
, DIGEST_LEN
))
941 (ri2
= router_get_by_digest(hop
->extend_info
->identity_digest
))
942 && routers_in_same_family(ri1
, ri2
))
945 } while (hop
!=circ
->cpath
);
947 if (!best
|| (best
->build_state
->need_uptime
&& !need_uptime
))
956 /** Return the number of hops in circuit's path. */
958 circuit_get_cpath_len(origin_circuit_t
*circ
)
961 if (circ
&& circ
->cpath
) {
962 crypt_path_t
*cpath
, *cpath_next
= NULL
;
963 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
964 cpath_next
= cpath
->next
;
971 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
972 * aren't that many hops in the list. */
974 circuit_get_cpath_hop(origin_circuit_t
*circ
, int hopnum
)
976 if (circ
&& circ
->cpath
&& hopnum
> 0) {
977 crypt_path_t
*cpath
, *cpath_next
= NULL
;
978 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
979 cpath_next
= cpath
->next
;
987 /** Go through the circuitlist; mark-for-close each circuit that starts
988 * at us but has not yet been used. */
990 circuit_mark_all_unused_circs(void)
994 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
995 if (CIRCUIT_IS_ORIGIN(circ
) &&
996 !circ
->marked_for_close
&&
997 !circ
->timestamp_dirty
)
998 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
1002 /** Go through the circuitlist; for each circuit that starts at us
1003 * and is dirty, frob its timestamp_dirty so we won't use it for any
1006 * This is useful for letting the user change pseudonyms, so new
1007 * streams will not be linkable to old streams.
1010 circuit_expire_all_dirty_circs(void)
1013 or_options_t
*options
= get_options();
1015 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
1016 if (CIRCUIT_IS_ORIGIN(circ
) &&
1017 !circ
->marked_for_close
&&
1018 circ
->timestamp_dirty
)
1019 circ
->timestamp_dirty
-= options
->MaxCircuitDirtiness
;
1023 /** Mark <b>circ</b> to be closed next time we call
1024 * circuit_close_all_marked(). Do any cleanup needed:
1025 * - If state is onionskin_pending, remove circ from the onion_pending
1027 * - If circ isn't open yet: call circuit_build_failed() if we're
1028 * the origin, and in either case call circuit_rep_hist_note_result()
1030 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
1031 * just tried from our list of intro points for that service
1033 * - Send appropriate destroys and edge_destroys for conns and
1034 * streams attached to circ.
1035 * - If circ->rend_splice is set (we are the midpoint of a joined
1036 * rendezvous stream), then mark the other circuit to close as well.
1039 _circuit_mark_for_close(circuit_t
*circ
, int reason
, int line
,
1042 int orig_reason
= reason
; /* Passed to the controller */
1043 assert_circuit_ok(circ
);
1047 if (circ
->marked_for_close
) {
1048 log(LOG_WARN
,LD_BUG
,
1049 "Duplicate call to circuit_mark_for_close at %s:%d"
1050 " (first at %s:%d)", file
, line
,
1051 circ
->marked_for_close_file
, circ
->marked_for_close
);
1054 if (reason
== END_CIRC_AT_ORIGIN
) {
1055 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1056 log_warn(LD_BUG
, "Specified 'at-origin' non-reason for ending circuit, "
1057 "but circuit was not at origin. (called %s:%d, purpose=%d)",
1058 file
, line
, circ
->purpose
);
1060 reason
= END_CIRC_REASON_NONE
;
1062 if (CIRCUIT_IS_ORIGIN(circ
)) {
1063 /* We don't send reasons when closing circuits at the origin. */
1064 reason
= END_CIRC_REASON_NONE
;
1067 if (reason
& END_CIRC_REASON_FLAG_REMOTE
)
1068 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
1070 if (reason
< _END_CIRC_REASON_MIN
|| reason
> _END_CIRC_REASON_MAX
) {
1071 if (!(orig_reason
& END_CIRC_REASON_FLAG_REMOTE
))
1072 log_warn(LD_BUG
, "Reason %d out of range at %s:%d", reason
, file
, line
);
1073 reason
= END_CIRC_REASON_NONE
;
1076 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
1077 onion_pending_remove(TO_OR_CIRCUIT(circ
));
1079 /* If the circuit ever became OPEN, we sent it to the reputation history
1080 * module then. If it isn't OPEN, we send it there now to remember which
1081 * links worked and which didn't.
1083 if (circ
->state
!= CIRCUIT_STATE_OPEN
) {
1084 if (CIRCUIT_IS_ORIGIN(circ
)) {
1085 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1086 circuit_build_failed(ocirc
); /* take actions if necessary */
1087 circuit_rep_hist_note_result(ocirc
);
1090 if (circ
->state
== CIRCUIT_STATE_OR_WAIT
) {
1091 if (circuits_pending_or_conns
)
1092 smartlist_remove(circuits_pending_or_conns
, circ
);
1094 if (CIRCUIT_IS_ORIGIN(circ
)) {
1095 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ
),
1096 (circ
->state
== CIRCUIT_STATE_OPEN
)?CIRC_EVENT_CLOSED
:CIRC_EVENT_FAILED
,
1099 if (circ
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1100 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1101 tor_assert(circ
->state
== CIRCUIT_STATE_OPEN
);
1102 tor_assert(ocirc
->build_state
->chosen_exit
);
1103 tor_assert(ocirc
->rend_data
);
1104 /* treat this like getting a nack from it */
1105 log_info(LD_REND
, "Failed intro circ %s to %s (awaiting ack). "
1106 "Removing from descriptor.",
1107 safe_str_client(ocirc
->rend_data
->onion_address
),
1108 safe_str_client(build_state_get_exit_nickname(ocirc
->build_state
)));
1109 rend_client_remove_intro_point(ocirc
->build_state
->chosen_exit
,
1113 connection_or_send_destroy(circ
->n_circ_id
, circ
->n_conn
, reason
);
1115 if (! CIRCUIT_IS_ORIGIN(circ
)) {
1116 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1117 edge_connection_t
*conn
;
1118 for (conn
=or_circ
->n_streams
; conn
; conn
=conn
->next_stream
)
1119 connection_edge_destroy(or_circ
->p_circ_id
, conn
);
1120 or_circ
->n_streams
= NULL
;
1122 while (or_circ
->resolving_streams
) {
1123 conn
= or_circ
->resolving_streams
;
1124 or_circ
->resolving_streams
= conn
->next_stream
;
1125 if (!conn
->_base
.marked_for_close
) {
1126 /* The client will see a DESTROY, and infer that the connections
1127 * are closing because the circuit is getting torn down. No need
1128 * to send an end cell. */
1129 conn
->edge_has_sent_end
= 1;
1130 conn
->end_reason
= END_STREAM_REASON_DESTROY
;
1131 conn
->end_reason
|= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
;
1132 connection_mark_for_close(TO_CONN(conn
));
1134 conn
->on_circuit
= NULL
;
1137 if (or_circ
->p_conn
)
1138 connection_or_send_destroy(or_circ
->p_circ_id
, or_circ
->p_conn
, reason
);
1140 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1141 edge_connection_t
*conn
;
1142 for (conn
=ocirc
->p_streams
; conn
; conn
=conn
->next_stream
)
1143 connection_edge_destroy(circ
->n_circ_id
, conn
);
1144 ocirc
->p_streams
= NULL
;
1147 circ
->marked_for_close
= line
;
1148 circ
->marked_for_close_file
= file
;
1150 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1151 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1152 if (or_circ
->rend_splice
) {
1153 if (!or_circ
->rend_splice
->_base
.marked_for_close
) {
1154 /* do this after marking this circuit, to avoid infinite recursion. */
1155 circuit_mark_for_close(TO_CIRCUIT(or_circ
->rend_splice
), reason
);
1157 or_circ
->rend_splice
= NULL
;
1162 /** Verify that cpath layer <b>cp</b> has all of its invariants
1163 * correct. Trigger an assert if anything is invalid.
1166 assert_cpath_layer_ok(const crypt_path_t
*cp
)
1168 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1169 // tor_assert(cp->port);
1171 tor_assert(cp
->magic
== CRYPT_PATH_MAGIC
);
1174 case CPATH_STATE_OPEN
:
1175 tor_assert(cp
->f_crypto
);
1176 tor_assert(cp
->b_crypto
);
1178 case CPATH_STATE_CLOSED
:
1179 tor_assert(!cp
->dh_handshake_state
);
1181 case CPATH_STATE_AWAITING_KEYS
:
1182 /* tor_assert(cp->dh_handshake_state); */
1185 log_fn(LOG_ERR
, LD_BUG
, "Unexpected state %d", cp
->state
);
1188 tor_assert(cp
->package_window
>= 0);
1189 tor_assert(cp
->deliver_window
>= 0);
1192 /** Verify that cpath <b>cp</b> has all of its invariants
1193 * correct. Trigger an assert if anything is invalid.
1196 assert_cpath_ok(const crypt_path_t
*cp
)
1198 const crypt_path_t
*start
= cp
;
1201 assert_cpath_layer_ok(cp
);
1202 /* layers must be in sequence of: "open* awaiting? closed*" */
1204 if (cp
->state
== CPATH_STATE_AWAITING_KEYS
) {
1205 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1206 } else if (cp
->state
== CPATH_STATE_OPEN
) {
1207 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1212 } while (cp
!= start
);
1215 /** Verify that circuit <b>c</b> has all of its invariants
1216 * correct. Trigger an assert if anything is invalid.
1219 assert_circuit_ok(const circuit_t
*c
)
1221 edge_connection_t
*conn
;
1222 const or_circuit_t
*or_circ
= NULL
;
1223 const origin_circuit_t
*origin_circ
= NULL
;
1226 tor_assert(c
->magic
== ORIGIN_CIRCUIT_MAGIC
|| c
->magic
== OR_CIRCUIT_MAGIC
);
1227 tor_assert(c
->purpose
>= _CIRCUIT_PURPOSE_MIN
&&
1228 c
->purpose
<= _CIRCUIT_PURPOSE_MAX
);
1231 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1232 * never understand. -NM. */
1233 circuit_t
*nonconst_circ
= (circuit_t
*) c
;
1234 if (CIRCUIT_IS_ORIGIN(c
))
1235 origin_circ
= TO_ORIGIN_CIRCUIT(nonconst_circ
);
1237 or_circ
= TO_OR_CIRCUIT(nonconst_circ
);
1241 tor_assert(!c
->n_hop
);
1244 /* We use the _impl variant here to make sure we don't fail on marked
1245 * circuits, which would not be returned by the regular function. */
1246 circuit_t
*c2
= circuit_get_by_circid_orconn_impl(c
->n_circ_id
,
1248 tor_assert(c
== c2
);
1251 if (or_circ
&& or_circ
->p_conn
) {
1252 if (or_circ
->p_circ_id
) {
1254 circuit_t
*c2
= circuit_get_by_circid_orconn_impl(or_circ
->p_circ_id
,
1256 tor_assert(c
== c2
);
1260 for (conn
= or_circ
->n_streams
; conn
; conn
= conn
->next_stream
)
1261 tor_assert(conn
->_base
.type
== CONN_TYPE_EXIT
);
1263 tor_assert(c
->deliver_window
>= 0);
1264 tor_assert(c
->package_window
>= 0);
1265 if (c
->state
== CIRCUIT_STATE_OPEN
) {
1266 tor_assert(!c
->n_conn_onionskin
);
1268 tor_assert(or_circ
->n_crypto
);
1269 tor_assert(or_circ
->p_crypto
);
1270 tor_assert(or_circ
->n_digest
);
1271 tor_assert(or_circ
->p_digest
);
1274 if (c
->state
== CIRCUIT_STATE_OR_WAIT
&& !c
->marked_for_close
) {
1275 tor_assert(circuits_pending_or_conns
&&
1276 smartlist_isin(circuits_pending_or_conns
, c
));
1278 tor_assert(!circuits_pending_or_conns
||
1279 !smartlist_isin(circuits_pending_or_conns
, c
));
1281 if (origin_circ
&& origin_circ
->cpath
) {
1282 assert_cpath_ok(origin_circ
->cpath
);
1284 if (c
->purpose
== CIRCUIT_PURPOSE_REND_ESTABLISHED
) {
1285 tor_assert(or_circ
);
1286 if (!c
->marked_for_close
) {
1287 tor_assert(or_circ
->rend_splice
);
1288 tor_assert(or_circ
->rend_splice
->rend_splice
== or_circ
);
1290 tor_assert(or_circ
->rend_splice
!= or_circ
);
1292 tor_assert(!or_circ
|| !or_circ
->rend_splice
);