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.
13 #include "circuitbuild.h"
14 #include "rendclient.h"
15 #include "rendcommon.h"
16 #include "routerlist.h"
19 /********* START VARIABLES **********/
21 /** A global list of all circuits at this hop. */
22 circuit_t
*global_circuitlist
=NULL
;
24 /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
25 static smartlist_t
*circuits_pending_or_conns
=NULL
;
27 static void circuit_free(circuit_t
*circ
);
28 static void circuit_free_cpath(crypt_path_t
*cpath
);
29 static void circuit_free_cpath_node(crypt_path_t
*victim
);
31 /********* END VARIABLES ************/
33 /** A map from OR connection and circuit ID to circuit. (Lookup performance is
34 * very important here, since we need to do it every time a cell arrives.) */
35 typedef struct orconn_circid_circuit_map_t
{
36 HT_ENTRY(orconn_circid_circuit_map_t
) node
;
37 or_connection_t
*or_conn
;
40 } orconn_circid_circuit_map_t
;
42 /** Helper for hash tables: compare the OR connection and circuit ID for a and
43 * b, and return less than, equal to, or greater than zero appropriately.
46 _orconn_circid_entries_eq(orconn_circid_circuit_map_t
*a
,
47 orconn_circid_circuit_map_t
*b
)
49 return a
->or_conn
== b
->or_conn
&& a
->circ_id
== b
->circ_id
;
52 /** Helper: return a hash based on circuit ID and the pointer value of
53 * or_conn in <b>a</b>. */
54 static INLINE
unsigned int
55 _orconn_circid_entry_hash(orconn_circid_circuit_map_t
*a
)
57 return (((unsigned)a
->circ_id
)<<8) ^ (unsigned)(uintptr_t)(a
->or_conn
);
60 /** Map from [orconn,circid] to circuit. */
61 static HT_HEAD(orconn_circid_map
, orconn_circid_circuit_map_t
)
62 orconn_circid_circuit_map
= HT_INITIALIZER();
63 HT_PROTOTYPE(orconn_circid_map
, orconn_circid_circuit_map_t
, node
,
64 _orconn_circid_entry_hash
, _orconn_circid_entries_eq
)
65 HT_GENERATE(orconn_circid_map
, orconn_circid_circuit_map_t
, node
,
66 _orconn_circid_entry_hash
, _orconn_circid_entries_eq
, 0.6,
67 malloc
, realloc
, free
)
69 /** The most recently returned entry from circuit_get_by_circid_orconn;
70 * used to improve performance when many cells arrive in a row from the
73 orconn_circid_circuit_map_t
*_last_circid_orconn_ent
= NULL
;
75 /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
76 * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
77 * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
78 * the old entry (if any) and adding a new one. If <b>active</b> is true,
79 * remove the circuit from the list of active circuits on old_conn and add it
80 * to the list of active circuits on conn.
81 * XXX "active" isn't an arg anymore */
83 circuit_set_circid_orconn_helper(circuit_t
*circ
, int direction
,
85 or_connection_t
*conn
)
87 orconn_circid_circuit_map_t search
;
88 orconn_circid_circuit_map_t
*found
;
89 or_connection_t
*old_conn
, **conn_ptr
;
90 circid_t old_id
, *circid_ptr
;
91 int was_active
, make_active
;
93 if (direction
== CELL_DIRECTION_OUT
) {
94 conn_ptr
= &circ
->n_conn
;
95 circid_ptr
= &circ
->n_circ_id
;
96 was_active
= circ
->next_active_on_n_conn
!= NULL
;
97 make_active
= circ
->n_conn_cells
.n
> 0;
99 or_circuit_t
*c
= TO_OR_CIRCUIT(circ
);
100 conn_ptr
= &c
->p_conn
;
101 circid_ptr
= &c
->p_circ_id
;
102 was_active
= c
->next_active_on_p_conn
!= NULL
;
103 make_active
= c
->p_conn_cells
.n
> 0;
105 old_conn
= *conn_ptr
;
106 old_id
= *circid_ptr
;
108 if (id
== old_id
&& conn
== old_conn
)
111 if (_last_circid_orconn_ent
&&
112 ((old_id
== _last_circid_orconn_ent
->circ_id
&&
113 old_conn
== _last_circid_orconn_ent
->or_conn
) ||
114 (id
== _last_circid_orconn_ent
->circ_id
&&
115 conn
== _last_circid_orconn_ent
->or_conn
))) {
116 _last_circid_orconn_ent
= NULL
;
119 if (old_conn
) { /* we may need to remove it from the conn-circid map */
120 tor_assert(old_conn
->_base
.magic
== OR_CONNECTION_MAGIC
);
121 search
.circ_id
= old_id
;
122 search
.or_conn
= old_conn
;
123 found
= HT_REMOVE(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
126 --old_conn
->n_circuits
;
128 if (was_active
&& old_conn
!= conn
)
129 make_circuit_inactive_on_conn(circ
,old_conn
);
132 /* Change the values only after we have possibly made the circuit inactive
133 * on the previous conn. */
140 /* now add the new one to the conn-circid map */
142 search
.or_conn
= conn
;
143 found
= HT_FIND(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
145 found
->circuit
= circ
;
147 found
= tor_malloc_zero(sizeof(orconn_circid_circuit_map_t
));
149 found
->or_conn
= conn
;
150 found
->circuit
= circ
;
151 HT_INSERT(orconn_circid_map
, &orconn_circid_circuit_map
, found
);
153 if (make_active
&& old_conn
!= conn
)
154 make_circuit_active_on_conn(circ
,conn
);
159 /** Set the p_conn field of a circuit <b>circ</b>, along
160 * with the corresponding circuit ID, and add the circuit as appropriate
161 * to the (orconn,id)-\>circuit map. */
163 circuit_set_p_circid_orconn(or_circuit_t
*circ
, circid_t id
,
164 or_connection_t
*conn
)
166 circuit_set_circid_orconn_helper(TO_CIRCUIT(circ
), CELL_DIRECTION_IN
,
170 tor_assert(bool_eq(circ
->p_conn_cells
.n
, circ
->next_active_on_p_conn
));
173 /** Set the n_conn field of a circuit <b>circ</b>, along
174 * with the corresponding circuit ID, and add the circuit as appropriate
175 * to the (orconn,id)-\>circuit map. */
177 circuit_set_n_circid_orconn(circuit_t
*circ
, circid_t id
,
178 or_connection_t
*conn
)
180 circuit_set_circid_orconn_helper(circ
, CELL_DIRECTION_OUT
, id
, conn
);
183 tor_assert(bool_eq(circ
->n_conn_cells
.n
, circ
->next_active_on_n_conn
));
186 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
187 * it from lists as appropriate. */
189 circuit_set_state(circuit_t
*circ
, uint8_t state
)
192 if (state
== circ
->state
)
194 if (!circuits_pending_or_conns
)
195 circuits_pending_or_conns
= smartlist_create();
196 if (circ
->state
== CIRCUIT_STATE_OR_WAIT
) {
197 /* remove from waiting-circuit list. */
198 smartlist_remove(circuits_pending_or_conns
, circ
);
200 if (state
== CIRCUIT_STATE_OR_WAIT
) {
201 /* add to waiting-circuit list. */
202 smartlist_add(circuits_pending_or_conns
, circ
);
204 if (state
== CIRCUIT_STATE_OPEN
)
205 tor_assert(!circ
->n_conn_onionskin
);
209 /** Add <b>circ</b> to the global list of circuits. This is called only from
210 * within circuit_new.
213 circuit_add(circuit_t
*circ
)
215 if (!global_circuitlist
) { /* first one */
216 global_circuitlist
= circ
;
219 circ
->next
= global_circuitlist
;
220 global_circuitlist
= circ
;
224 /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
225 * the given connection. */
227 circuit_get_all_pending_on_or_conn(smartlist_t
*out
, or_connection_t
*or_conn
)
232 if (!circuits_pending_or_conns
)
235 SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns
, circuit_t
*, circ
) {
236 if (circ
->marked_for_close
)
240 tor_assert(circ
->state
== CIRCUIT_STATE_OR_WAIT
);
241 if (tor_digest_is_zero(circ
->n_hop
->identity_digest
)) {
242 /* Look at addr/port. This is an unkeyed connection. */
243 if (!tor_addr_eq(&circ
->n_hop
->addr
, &or_conn
->_base
.addr
) ||
244 circ
->n_hop
->port
!= or_conn
->_base
.port
)
247 /* We expected a key. See if it's the right one. */
248 if (memcmp(or_conn
->identity_digest
,
249 circ
->n_hop
->identity_digest
, DIGEST_LEN
))
252 smartlist_add(out
, circ
);
253 } SMARTLIST_FOREACH_END(circ
);
256 /** Return the number of circuits in state OR_WAIT, waiting for the given
259 circuit_count_pending_on_or_conn(or_connection_t
*or_conn
)
262 smartlist_t
*sl
= smartlist_create();
263 circuit_get_all_pending_on_or_conn(sl
, or_conn
);
264 cnt
= smartlist_len(sl
);
266 log_debug(LD_CIRC
,"or_conn to %s, %d pending circs",
267 or_conn
->nickname
? or_conn
->nickname
: "NULL", cnt
);
271 /** Detach from the global circuit list, and deallocate, all
272 * circuits that have been marked for close.
275 circuit_close_all_marked(void)
279 while (global_circuitlist
&& global_circuitlist
->marked_for_close
) {
280 tmp
= global_circuitlist
->next
;
281 circuit_free(global_circuitlist
);
282 global_circuitlist
= tmp
;
285 tmp
= global_circuitlist
;
286 while (tmp
&& tmp
->next
) {
287 if (tmp
->next
->marked_for_close
) {
289 circuit_free(tmp
->next
);
291 /* Need to check new tmp->next; don't advance tmp. */
299 /** Return the head of the global linked list of circuits. */
301 _circuit_get_global_list(void)
303 return global_circuitlist
;
306 /** Function to make circ-\>state human-readable */
308 circuit_state_to_string(int state
)
312 case CIRCUIT_STATE_BUILDING
: return "doing handshakes";
313 case CIRCUIT_STATE_ONIONSKIN_PENDING
: return "processing the onion";
314 case CIRCUIT_STATE_OR_WAIT
: return "connecting to server";
315 case CIRCUIT_STATE_OPEN
: return "open";
317 log_warn(LD_BUG
, "Unknown circuit state %d", state
);
318 tor_snprintf(buf
, sizeof(buf
), "unknown state [%d]", state
);
323 /** Map a circuit purpose to a string suitable to be displayed to a
326 circuit_purpose_to_controller_string(uint8_t purpose
)
330 case CIRCUIT_PURPOSE_OR
:
331 case CIRCUIT_PURPOSE_INTRO_POINT
:
332 case CIRCUIT_PURPOSE_REND_POINT_WAITING
:
333 case CIRCUIT_PURPOSE_REND_ESTABLISHED
:
334 return "SERVER"; /* A controller should never see these, actually. */
336 case CIRCUIT_PURPOSE_C_GENERAL
:
338 case CIRCUIT_PURPOSE_C_INTRODUCING
:
339 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
340 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
:
341 return "HS_CLIENT_INTRO";
343 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
344 case CIRCUIT_PURPOSE_C_REND_READY
:
345 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
346 case CIRCUIT_PURPOSE_C_REND_JOINED
:
347 return "HS_CLIENT_REND";
349 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
350 case CIRCUIT_PURPOSE_S_INTRO
:
351 return "HS_SERVICE_INTRO";
353 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
354 case CIRCUIT_PURPOSE_S_REND_JOINED
:
355 return "HS_SERVICE_REND";
357 case CIRCUIT_PURPOSE_TESTING
:
359 case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
:
361 case CIRCUIT_PURPOSE_CONTROLLER
:
365 tor_snprintf(buf
, sizeof(buf
), "UNKNOWN_%d", (int)purpose
);
370 /** Pick a reasonable package_window to start out for our circuits.
371 * Originally this was hard-coded at 1000, but now the consensus votes
372 * on the answer. See proposal 168. */
374 circuit_initial_package_window(void)
376 int32_t num
= networkstatus_get_param(NULL
, "circwindow", CIRCWINDOW_START
);
377 /* If the consensus tells us a negative number, we'd assert. */
379 num
= CIRCWINDOW_START
;
383 /** Initialize the common elements in a circuit_t, and add it to the global
386 init_circuit_base(circuit_t
*circ
)
388 circ
->timestamp_created
= time(NULL
);
389 tor_gettimeofday(&circ
->highres_created
);
391 circ
->package_window
= circuit_initial_package_window();
392 circ
->deliver_window
= CIRCWINDOW_START
;
394 /* Initialize the cell_ewma_t structure */
395 circ
->n_cell_ewma
.last_adjusted_tick
= cell_ewma_get_tick();
396 circ
->n_cell_ewma
.cell_count
= 0.0;
397 circ
->n_cell_ewma
.heap_index
= -1;
398 circ
->n_cell_ewma
.is_for_p_conn
= 0;
403 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
404 * and <b>p_conn</b>. Add it to the global circuit list.
407 origin_circuit_new(void)
409 origin_circuit_t
*circ
;
410 /* never zero, since a global ID of 0 is treated specially by the
412 static uint32_t n_circuits_allocated
= 1;
414 circ
= tor_malloc_zero(sizeof(origin_circuit_t
));
415 circ
->_base
.magic
= ORIGIN_CIRCUIT_MAGIC
;
417 circ
->next_stream_id
= crypto_rand_int(1<<16);
418 circ
->global_identifier
= n_circuits_allocated
++;
419 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
420 circ
->remaining_relay_early_cells
-= crypto_rand_int(2);
422 init_circuit_base(TO_CIRCUIT(circ
));
424 circ_times
.last_circ_at
= approx_time();
429 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
430 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
432 or_circuit_new(circid_t p_circ_id
, or_connection_t
*p_conn
)
437 circ
= tor_malloc_zero(sizeof(or_circuit_t
));
438 circ
->_base
.magic
= OR_CIRCUIT_MAGIC
;
441 circuit_set_p_circid_orconn(circ
, p_circ_id
, p_conn
);
443 circ
->remaining_relay_early_cells
= MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
;
445 init_circuit_base(TO_CIRCUIT(circ
));
447 /* Initialize the cell_ewma_t structure */
449 /* Initialize the cell counts to 0 */
450 circ
->p_cell_ewma
.cell_count
= 0.0;
451 circ
->p_cell_ewma
.last_adjusted_tick
= cell_ewma_get_tick();
452 circ
->p_cell_ewma
.is_for_p_conn
= 1;
454 /* It's not in any heap yet. */
455 circ
->p_cell_ewma
.heap_index
= -1;
460 /** Deallocate space associated with circ.
463 circuit_free(circuit_t
*circ
)
470 if (CIRCUIT_IS_ORIGIN(circ
)) {
471 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
473 memlen
= sizeof(origin_circuit_t
);
474 tor_assert(circ
->magic
== ORIGIN_CIRCUIT_MAGIC
);
475 if (ocirc
->build_state
) {
476 extend_info_free(ocirc
->build_state
->chosen_exit
);
477 circuit_free_cpath_node(ocirc
->build_state
->pending_final_cpath
);
479 tor_free(ocirc
->build_state
);
481 circuit_free_cpath(ocirc
->cpath
);
483 crypto_free_pk_env(ocirc
->intro_key
);
484 rend_data_free(ocirc
->rend_data
);
486 or_circuit_t
*ocirc
= TO_OR_CIRCUIT(circ
);
487 /* Remember cell statistics for this circuit before deallocating. */
488 if (get_options()->CellStatistics
)
489 rep_hist_buffer_stats_add_circ(circ
, time(NULL
));
491 memlen
= sizeof(or_circuit_t
);
492 tor_assert(circ
->magic
== OR_CIRCUIT_MAGIC
);
494 crypto_free_cipher_env(ocirc
->p_crypto
);
495 crypto_free_digest_env(ocirc
->p_digest
);
496 crypto_free_cipher_env(ocirc
->n_crypto
);
497 crypto_free_digest_env(ocirc
->n_digest
);
499 if (ocirc
->rend_splice
) {
500 or_circuit_t
*other
= ocirc
->rend_splice
;
501 tor_assert(other
->_base
.magic
== OR_CIRCUIT_MAGIC
);
502 other
->rend_splice
= NULL
;
505 /* remove from map. */
506 circuit_set_p_circid_orconn(ocirc
, 0, NULL
);
508 /* Clear cell queue _after_ removing it from the map. Otherwise our
509 * "active" checks will be violated. */
510 cell_queue_clear(ô
->p_conn_cells
);
513 extend_info_free(circ
->n_hop
);
514 tor_free(circ
->n_conn_onionskin
);
516 /* Remove from map. */
517 circuit_set_n_circid_orconn(circ
, 0, NULL
);
519 /* Clear cell queue _after_ removing it from the map. Otherwise our
520 * "active" checks will be violated. */
521 cell_queue_clear(&circ
->n_conn_cells
);
523 memset(mem
, 0xAA, memlen
); /* poison memory */
527 /** Deallocate space associated with the linked list <b>cpath</b>. */
529 circuit_free_cpath(crypt_path_t
*cpath
)
531 crypt_path_t
*victim
, *head
=cpath
;
536 /* it's a doubly linked list, so we have to notice when we've
537 * gone through it once. */
538 while (cpath
->next
&& cpath
->next
!= head
) {
540 cpath
= victim
->next
;
541 circuit_free_cpath_node(victim
);
544 circuit_free_cpath_node(cpath
);
547 /** Release all storage held by circuits. */
549 circuit_free_all(void)
552 while (global_circuitlist
) {
553 next
= global_circuitlist
->next
;
554 if (! CIRCUIT_IS_ORIGIN(global_circuitlist
)) {
555 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(global_circuitlist
);
556 while (or_circ
->resolving_streams
) {
557 edge_connection_t
*next_conn
;
558 next_conn
= or_circ
->resolving_streams
->next_stream
;
559 connection_free(TO_CONN(or_circ
->resolving_streams
));
560 or_circ
->resolving_streams
= next_conn
;
563 circuit_free(global_circuitlist
);
564 global_circuitlist
= next
;
567 smartlist_free(circuits_pending_or_conns
);
568 circuits_pending_or_conns
= NULL
;
570 HT_CLEAR(orconn_circid_map
, &orconn_circid_circuit_map
);
573 /** Deallocate space associated with the cpath node <b>victim</b>. */
575 circuit_free_cpath_node(crypt_path_t
*victim
)
580 crypto_free_cipher_env(victim
->f_crypto
);
581 crypto_free_cipher_env(victim
->b_crypto
);
582 crypto_free_digest_env(victim
->f_digest
);
583 crypto_free_digest_env(victim
->b_digest
);
584 crypto_dh_free(victim
->dh_handshake_state
);
585 extend_info_free(victim
->extend_info
);
587 memset(victim
, 0xBB, sizeof(crypt_path_t
)); /* poison memory */
591 /** A helper function for circuit_dump_by_conn() below. Log a bunch
592 * of information about circuit <b>circ</b>.
595 circuit_dump_details(int severity
, circuit_t
*circ
, int conn_array_index
,
596 const char *type
, int this_circid
, int other_circid
)
598 log(severity
, LD_CIRC
, "Conn %d has %s circuit: circID %d (other side %d), "
599 "state %d (%s), born %d:",
600 conn_array_index
, type
, this_circid
, other_circid
, circ
->state
,
601 circuit_state_to_string(circ
->state
), (int)circ
->timestamp_created
);
602 if (CIRCUIT_IS_ORIGIN(circ
)) { /* circ starts at this node */
603 circuit_log_path(severity
, LD_CIRC
, TO_ORIGIN_CIRCUIT(circ
));
607 /** Log, at severity <b>severity</b>, information about each circuit
608 * that is connected to <b>conn</b>.
611 circuit_dump_by_conn(connection_t
*conn
, int severity
)
614 edge_connection_t
*tmpconn
;
616 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
617 circid_t n_circ_id
= circ
->n_circ_id
, p_circ_id
= 0;
618 if (circ
->marked_for_close
)
621 if (! CIRCUIT_IS_ORIGIN(circ
))
622 p_circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
;
624 if (! CIRCUIT_IS_ORIGIN(circ
) && TO_OR_CIRCUIT(circ
)->p_conn
&&
625 TO_CONN(TO_OR_CIRCUIT(circ
)->p_conn
) == conn
)
626 circuit_dump_details(severity
, circ
, conn
->conn_array_index
, "App-ward",
627 p_circ_id
, n_circ_id
);
628 if (CIRCUIT_IS_ORIGIN(circ
)) {
629 for (tmpconn
=TO_ORIGIN_CIRCUIT(circ
)->p_streams
; tmpconn
;
630 tmpconn
=tmpconn
->next_stream
) {
631 if (TO_CONN(tmpconn
) == conn
) {
632 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
633 "App-ward", p_circ_id
, n_circ_id
);
637 if (circ
->n_conn
&& TO_CONN(circ
->n_conn
) == conn
)
638 circuit_dump_details(severity
, circ
, conn
->conn_array_index
, "Exit-ward",
639 n_circ_id
, p_circ_id
);
640 if (! CIRCUIT_IS_ORIGIN(circ
)) {
641 for (tmpconn
=TO_OR_CIRCUIT(circ
)->n_streams
; tmpconn
;
642 tmpconn
=tmpconn
->next_stream
) {
643 if (TO_CONN(tmpconn
) == conn
) {
644 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
645 "Exit-ward", n_circ_id
, p_circ_id
);
649 if (!circ
->n_conn
&& circ
->n_hop
&&
650 tor_addr_eq(&circ
->n_hop
->addr
, &conn
->addr
) &&
651 circ
->n_hop
->port
== conn
->port
&&
652 conn
->type
== CONN_TYPE_OR
&&
653 !memcmp(TO_OR_CONN(conn
)->identity_digest
,
654 circ
->n_hop
->identity_digest
, DIGEST_LEN
)) {
655 circuit_dump_details(severity
, circ
, conn
->conn_array_index
,
656 (circ
->state
== CIRCUIT_STATE_OPEN
&&
657 !CIRCUIT_IS_ORIGIN(circ
)) ?
658 "Endpoint" : "Pending",
659 n_circ_id
, p_circ_id
);
664 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
665 * such circuit exists. */
667 circuit_get_by_global_id(uint32_t id
)
670 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
671 if (CIRCUIT_IS_ORIGIN(circ
) &&
672 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
== id
) {
673 if (circ
->marked_for_close
)
676 return TO_ORIGIN_CIRCUIT(circ
);
682 /** Return a circ such that:
683 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
684 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
685 * Return NULL if no such circuit exists.
687 static INLINE circuit_t
*
688 circuit_get_by_circid_orconn_impl(circid_t circ_id
, or_connection_t
*conn
)
690 orconn_circid_circuit_map_t search
;
691 orconn_circid_circuit_map_t
*found
;
693 if (_last_circid_orconn_ent
&&
694 circ_id
== _last_circid_orconn_ent
->circ_id
&&
695 conn
== _last_circid_orconn_ent
->or_conn
) {
696 found
= _last_circid_orconn_ent
;
698 search
.circ_id
= circ_id
;
699 search
.or_conn
= conn
;
700 found
= HT_FIND(orconn_circid_map
, &orconn_circid_circuit_map
, &search
);
701 _last_circid_orconn_ent
= found
;
703 if (found
&& found
->circuit
)
704 return found
->circuit
;
708 /* The rest of this checks for bugs. Disabled by default. */
711 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
712 if (! CIRCUIT_IS_ORIGIN(circ
)) {
713 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
714 if (or_circ
->p_conn
== conn
&& or_circ
->p_circ_id
== circ_id
) {
716 "circuit matches p_conn, but not in hash table (Bug!)");
720 if (circ
->n_conn
== conn
&& circ
->n_circ_id
== circ_id
) {
722 "circuit matches n_conn, but not in hash table (Bug!)");
730 /** Return a circ such that:
731 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
732 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
733 * - circ is not marked for close.
734 * Return NULL if no such circuit exists.
737 circuit_get_by_circid_orconn(circid_t circ_id
, or_connection_t
*conn
)
739 circuit_t
*circ
= circuit_get_by_circid_orconn_impl(circ_id
, conn
);
740 if (!circ
|| circ
->marked_for_close
)
746 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
747 * circuit, marked or not, on <b>conn</b>. */
749 circuit_id_in_use_on_orconn(circid_t circ_id
, or_connection_t
*conn
)
751 return circuit_get_by_circid_orconn_impl(circ_id
, conn
) != NULL
;
754 /** Return the circuit that a given edge connection is using. */
756 circuit_get_by_edge_conn(edge_connection_t
*conn
)
760 circ
= conn
->on_circuit
;
762 (CIRCUIT_IS_ORIGIN(circ
) ? circ
->magic
== ORIGIN_CIRCUIT_MAGIC
763 : circ
->magic
== OR_CIRCUIT_MAGIC
));
768 /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
769 * circuit from the orconn,circid map, and mark it for close if it hasn't
770 * been marked already.
773 circuit_unlink_all_from_or_conn(or_connection_t
*conn
, int reason
)
777 connection_or_unlink_all_active_circs(conn
);
779 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
781 if (circ
->n_conn
== conn
) {
782 circuit_set_n_circid_orconn(circ
, 0, NULL
);
785 if (! CIRCUIT_IS_ORIGIN(circ
)) {
786 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
787 if (or_circ
->p_conn
== conn
) {
788 circuit_set_p_circid_orconn(or_circ
, 0, NULL
);
792 if (mark
&& !circ
->marked_for_close
)
793 circuit_mark_for_close(circ
, reason
);
797 /** Return a circ such that:
798 * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
799 * - circ-\>purpose is equal to <b>purpose</b>.
801 * Return NULL if no such circuit exists.
804 circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
)
808 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
810 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
811 if (!circ
->marked_for_close
&&
812 circ
->purpose
== purpose
) {
813 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
814 if (ocirc
->rend_data
&&
815 !rend_cmp_service_ids(rend_query
,
816 ocirc
->rend_data
->onion_address
))
823 /** Return the first circuit originating here in global_circuitlist after
824 * <b>start</b> whose purpose is <b>purpose</b>, and where
825 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
826 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
829 circuit_get_next_by_pk_and_purpose(origin_circuit_t
*start
,
830 const char *digest
, uint8_t purpose
)
833 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose
));
835 circ
= global_circuitlist
;
837 circ
= TO_CIRCUIT(start
)->next
;
839 for ( ; circ
; circ
= circ
->next
) {
840 if (circ
->marked_for_close
)
842 if (circ
->purpose
!= purpose
)
845 return TO_ORIGIN_CIRCUIT(circ
);
846 else if (TO_ORIGIN_CIRCUIT(circ
)->rend_data
&&
847 !memcmp(TO_ORIGIN_CIRCUIT(circ
)->rend_data
->rend_pk_digest
,
849 return TO_ORIGIN_CIRCUIT(circ
);
854 /** Return the first OR circuit in the global list whose purpose is
855 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
857 static or_circuit_t
*
858 circuit_get_by_rend_token_and_purpose(uint8_t purpose
, const char *token
,
862 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
863 if (! circ
->marked_for_close
&&
864 circ
->purpose
== purpose
&&
865 ! memcmp(TO_OR_CIRCUIT(circ
)->rend_token
, token
, len
))
866 return TO_OR_CIRCUIT(circ
);
871 /** Return the circuit waiting for a rendezvous with the provided cookie.
872 * Return NULL if no such circuit is found.
875 circuit_get_rendezvous(const char *cookie
)
877 return circuit_get_by_rend_token_and_purpose(
878 CIRCUIT_PURPOSE_REND_POINT_WAITING
,
879 cookie
, REND_COOKIE_LEN
);
882 /** Return the circuit waiting for intro cells of the given digest.
883 * Return NULL if no such circuit is found.
886 circuit_get_intro_point(const char *digest
)
888 return circuit_get_by_rend_token_and_purpose(
889 CIRCUIT_PURPOSE_INTRO_POINT
, digest
,
893 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
894 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
895 * flags in <b>flags</b>, and if info is defined, does not already use info
896 * as any of its hops; or NULL if no circuit fits this description.
898 * The <b>purpose</b> argument (currently ignored) refers to the purpose of
899 * the circuit we want to create, not the purpose of the circuit we want to
902 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
905 circuit_find_to_cannibalize(uint8_t purpose
, extend_info_t
*info
,
909 origin_circuit_t
*best
=NULL
;
910 int need_uptime
= (flags
& CIRCLAUNCH_NEED_UPTIME
) != 0;
911 int need_capacity
= (flags
& CIRCLAUNCH_NEED_CAPACITY
) != 0;
912 int internal
= (flags
& CIRCLAUNCH_IS_INTERNAL
) != 0;
914 /* Make sure we're not trying to create a onehop circ by
915 * cannibalization. */
916 tor_assert(!(flags
& CIRCLAUNCH_ONEHOP_TUNNEL
));
919 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
920 "capacity %d, internal %d",
921 purpose
, need_uptime
, need_capacity
, internal
);
923 for (_circ
=global_circuitlist
; _circ
; _circ
= _circ
->next
) {
924 if (CIRCUIT_IS_ORIGIN(_circ
) &&
925 _circ
->state
== CIRCUIT_STATE_OPEN
&&
926 !_circ
->marked_for_close
&&
927 _circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
928 !_circ
->timestamp_dirty
) {
929 origin_circuit_t
*circ
= TO_ORIGIN_CIRCUIT(_circ
);
930 if ((!need_uptime
|| circ
->build_state
->need_uptime
) &&
931 (!need_capacity
|| circ
->build_state
->need_capacity
) &&
932 (internal
== circ
->build_state
->is_internal
) &&
933 circ
->remaining_relay_early_cells
&&
934 !circ
->build_state
->onehop_tunnel
) {
936 /* need to make sure we don't duplicate hops */
937 crypt_path_t
*hop
= circ
->cpath
;
938 routerinfo_t
*ri1
= router_get_by_digest(info
->identity_digest
);
941 if (!memcmp(hop
->extend_info
->identity_digest
,
942 info
->identity_digest
, DIGEST_LEN
))
945 (ri2
= router_get_by_digest(hop
->extend_info
->identity_digest
))
946 && routers_in_same_family(ri1
, ri2
))
949 } while (hop
!=circ
->cpath
);
951 if (!best
|| (best
->build_state
->need_uptime
&& !need_uptime
))
960 /** Return the number of hops in circuit's path. */
962 circuit_get_cpath_len(origin_circuit_t
*circ
)
965 if (circ
&& circ
->cpath
) {
966 crypt_path_t
*cpath
, *cpath_next
= NULL
;
967 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
968 cpath_next
= cpath
->next
;
975 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
976 * aren't that many hops in the list. */
978 circuit_get_cpath_hop(origin_circuit_t
*circ
, int hopnum
)
980 if (circ
&& circ
->cpath
&& hopnum
> 0) {
981 crypt_path_t
*cpath
, *cpath_next
= NULL
;
982 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
983 cpath_next
= cpath
->next
;
991 /** Go through the circuitlist; mark-for-close each circuit that starts
992 * at us but has not yet been used. */
994 circuit_mark_all_unused_circs(void)
998 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
999 if (CIRCUIT_IS_ORIGIN(circ
) &&
1000 !circ
->marked_for_close
&&
1001 !circ
->timestamp_dirty
)
1002 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
1006 /** Go through the circuitlist; for each circuit that starts at us
1007 * and is dirty, frob its timestamp_dirty so we won't use it for any
1010 * This is useful for letting the user change pseudonyms, so new
1011 * streams will not be linkable to old streams.
1014 circuit_expire_all_dirty_circs(void)
1017 or_options_t
*options
= get_options();
1019 for (circ
=global_circuitlist
; circ
; circ
= circ
->next
) {
1020 if (CIRCUIT_IS_ORIGIN(circ
) &&
1021 !circ
->marked_for_close
&&
1022 circ
->timestamp_dirty
)
1023 circ
->timestamp_dirty
-= options
->MaxCircuitDirtiness
;
1027 /** Mark <b>circ</b> to be closed next time we call
1028 * circuit_close_all_marked(). Do any cleanup needed:
1029 * - If state is onionskin_pending, remove circ from the onion_pending
1031 * - If circ isn't open yet: call circuit_build_failed() if we're
1032 * the origin, and in either case call circuit_rep_hist_note_result()
1034 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
1035 * just tried from our list of intro points for that service
1037 * - Send appropriate destroys and edge_destroys for conns and
1038 * streams attached to circ.
1039 * - If circ->rend_splice is set (we are the midpoint of a joined
1040 * rendezvous stream), then mark the other circuit to close as well.
1043 _circuit_mark_for_close(circuit_t
*circ
, int reason
, int line
,
1046 int orig_reason
= reason
; /* Passed to the controller */
1047 assert_circuit_ok(circ
);
1051 if (circ
->marked_for_close
) {
1052 log(LOG_WARN
,LD_BUG
,
1053 "Duplicate call to circuit_mark_for_close at %s:%d"
1054 " (first at %s:%d)", file
, line
,
1055 circ
->marked_for_close_file
, circ
->marked_for_close
);
1058 if (reason
== END_CIRC_AT_ORIGIN
) {
1059 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1060 log_warn(LD_BUG
, "Specified 'at-origin' non-reason for ending circuit, "
1061 "but circuit was not at origin. (called %s:%d, purpose=%d)",
1062 file
, line
, circ
->purpose
);
1064 reason
= END_CIRC_REASON_NONE
;
1066 if (CIRCUIT_IS_ORIGIN(circ
)) {
1067 /* We don't send reasons when closing circuits at the origin. */
1068 reason
= END_CIRC_REASON_NONE
;
1071 if (reason
& END_CIRC_REASON_FLAG_REMOTE
)
1072 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
1074 if (reason
< _END_CIRC_REASON_MIN
|| reason
> _END_CIRC_REASON_MAX
) {
1075 if (!(orig_reason
& END_CIRC_REASON_FLAG_REMOTE
))
1076 log_warn(LD_BUG
, "Reason %d out of range at %s:%d", reason
, file
, line
);
1077 reason
= END_CIRC_REASON_NONE
;
1080 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
1081 onion_pending_remove(TO_OR_CIRCUIT(circ
));
1083 /* If the circuit ever became OPEN, we sent it to the reputation history
1084 * module then. If it isn't OPEN, we send it there now to remember which
1085 * links worked and which didn't.
1087 if (circ
->state
!= CIRCUIT_STATE_OPEN
) {
1088 if (CIRCUIT_IS_ORIGIN(circ
)) {
1089 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1090 circuit_build_failed(ocirc
); /* take actions if necessary */
1091 circuit_rep_hist_note_result(ocirc
);
1094 if (circ
->state
== CIRCUIT_STATE_OR_WAIT
) {
1095 if (circuits_pending_or_conns
)
1096 smartlist_remove(circuits_pending_or_conns
, circ
);
1098 if (CIRCUIT_IS_ORIGIN(circ
)) {
1099 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ
),
1100 (circ
->state
== CIRCUIT_STATE_OPEN
)?CIRC_EVENT_CLOSED
:CIRC_EVENT_FAILED
,
1103 if (circ
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1104 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1105 tor_assert(circ
->state
== CIRCUIT_STATE_OPEN
);
1106 tor_assert(ocirc
->build_state
->chosen_exit
);
1107 tor_assert(ocirc
->rend_data
);
1108 /* treat this like getting a nack from it */
1109 log_info(LD_REND
, "Failed intro circ %s to %s (awaiting ack). "
1110 "Removing from descriptor.",
1111 safe_str_client(ocirc
->rend_data
->onion_address
),
1112 safe_str_client(build_state_get_exit_nickname(ocirc
->build_state
)));
1113 rend_client_remove_intro_point(ocirc
->build_state
->chosen_exit
,
1117 connection_or_send_destroy(circ
->n_circ_id
, circ
->n_conn
, reason
);
1119 if (! CIRCUIT_IS_ORIGIN(circ
)) {
1120 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1121 edge_connection_t
*conn
;
1122 for (conn
=or_circ
->n_streams
; conn
; conn
=conn
->next_stream
)
1123 connection_edge_destroy(or_circ
->p_circ_id
, conn
);
1124 or_circ
->n_streams
= NULL
;
1126 while (or_circ
->resolving_streams
) {
1127 conn
= or_circ
->resolving_streams
;
1128 or_circ
->resolving_streams
= conn
->next_stream
;
1129 if (!conn
->_base
.marked_for_close
) {
1130 /* The client will see a DESTROY, and infer that the connections
1131 * are closing because the circuit is getting torn down. No need
1132 * to send an end cell. */
1133 conn
->edge_has_sent_end
= 1;
1134 conn
->end_reason
= END_STREAM_REASON_DESTROY
;
1135 conn
->end_reason
|= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
;
1136 connection_mark_for_close(TO_CONN(conn
));
1138 conn
->on_circuit
= NULL
;
1141 if (or_circ
->p_conn
)
1142 connection_or_send_destroy(or_circ
->p_circ_id
, or_circ
->p_conn
, reason
);
1144 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1145 edge_connection_t
*conn
;
1146 for (conn
=ocirc
->p_streams
; conn
; conn
=conn
->next_stream
)
1147 connection_edge_destroy(circ
->n_circ_id
, conn
);
1148 ocirc
->p_streams
= NULL
;
1151 circ
->marked_for_close
= line
;
1152 circ
->marked_for_close_file
= file
;
1154 if (!CIRCUIT_IS_ORIGIN(circ
)) {
1155 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1156 if (or_circ
->rend_splice
) {
1157 if (!or_circ
->rend_splice
->_base
.marked_for_close
) {
1158 /* do this after marking this circuit, to avoid infinite recursion. */
1159 circuit_mark_for_close(TO_CIRCUIT(or_circ
->rend_splice
), reason
);
1161 or_circ
->rend_splice
= NULL
;
1166 /** Verify that cpath layer <b>cp</b> has all of its invariants
1167 * correct. Trigger an assert if anything is invalid.
1170 assert_cpath_layer_ok(const crypt_path_t
*cp
)
1172 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1173 // tor_assert(cp->port);
1175 tor_assert(cp
->magic
== CRYPT_PATH_MAGIC
);
1178 case CPATH_STATE_OPEN
:
1179 tor_assert(cp
->f_crypto
);
1180 tor_assert(cp
->b_crypto
);
1182 case CPATH_STATE_CLOSED
:
1183 tor_assert(!cp
->dh_handshake_state
);
1185 case CPATH_STATE_AWAITING_KEYS
:
1186 /* tor_assert(cp->dh_handshake_state); */
1189 log_fn(LOG_ERR
, LD_BUG
, "Unexpected state %d", cp
->state
);
1192 tor_assert(cp
->package_window
>= 0);
1193 tor_assert(cp
->deliver_window
>= 0);
1196 /** Verify that cpath <b>cp</b> has all of its invariants
1197 * correct. Trigger an assert if anything is invalid.
1200 assert_cpath_ok(const crypt_path_t
*cp
)
1202 const crypt_path_t
*start
= cp
;
1205 assert_cpath_layer_ok(cp
);
1206 /* layers must be in sequence of: "open* awaiting? closed*" */
1208 if (cp
->state
== CPATH_STATE_AWAITING_KEYS
) {
1209 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1210 } else if (cp
->state
== CPATH_STATE_OPEN
) {
1211 tor_assert(cp
->prev
->state
== CPATH_STATE_OPEN
);
1216 } while (cp
!= start
);
1219 /** Verify that circuit <b>c</b> has all of its invariants
1220 * correct. Trigger an assert if anything is invalid.
1223 assert_circuit_ok(const circuit_t
*c
)
1225 edge_connection_t
*conn
;
1226 const or_circuit_t
*or_circ
= NULL
;
1227 const origin_circuit_t
*origin_circ
= NULL
;
1230 tor_assert(c
->magic
== ORIGIN_CIRCUIT_MAGIC
|| c
->magic
== OR_CIRCUIT_MAGIC
);
1231 tor_assert(c
->purpose
>= _CIRCUIT_PURPOSE_MIN
&&
1232 c
->purpose
<= _CIRCUIT_PURPOSE_MAX
);
1235 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1236 * never understand. -NM. */
1237 circuit_t
*nonconst_circ
= (circuit_t
*) c
;
1238 if (CIRCUIT_IS_ORIGIN(c
))
1239 origin_circ
= TO_ORIGIN_CIRCUIT(nonconst_circ
);
1241 or_circ
= TO_OR_CIRCUIT(nonconst_circ
);
1245 tor_assert(!c
->n_hop
);
1248 /* We use the _impl variant here to make sure we don't fail on marked
1249 * circuits, which would not be returned by the regular function. */
1250 circuit_t
*c2
= circuit_get_by_circid_orconn_impl(c
->n_circ_id
,
1252 tor_assert(c
== c2
);
1255 if (or_circ
&& or_circ
->p_conn
) {
1256 if (or_circ
->p_circ_id
) {
1258 circuit_t
*c2
= circuit_get_by_circid_orconn_impl(or_circ
->p_circ_id
,
1260 tor_assert(c
== c2
);
1264 for (conn
= or_circ
->n_streams
; conn
; conn
= conn
->next_stream
)
1265 tor_assert(conn
->_base
.type
== CONN_TYPE_EXIT
);
1267 tor_assert(c
->deliver_window
>= 0);
1268 tor_assert(c
->package_window
>= 0);
1269 if (c
->state
== CIRCUIT_STATE_OPEN
) {
1270 tor_assert(!c
->n_conn_onionskin
);
1272 tor_assert(or_circ
->n_crypto
);
1273 tor_assert(or_circ
->p_crypto
);
1274 tor_assert(or_circ
->n_digest
);
1275 tor_assert(or_circ
->p_digest
);
1278 if (c
->state
== CIRCUIT_STATE_OR_WAIT
&& !c
->marked_for_close
) {
1279 tor_assert(circuits_pending_or_conns
&&
1280 smartlist_isin(circuits_pending_or_conns
, c
));
1282 tor_assert(!circuits_pending_or_conns
||
1283 !smartlist_isin(circuits_pending_or_conns
, c
));
1285 if (origin_circ
&& origin_circ
->cpath
) {
1286 assert_cpath_ok(origin_circ
->cpath
);
1288 if (c
->purpose
== CIRCUIT_PURPOSE_REND_ESTABLISHED
) {
1289 tor_assert(or_circ
);
1290 if (!c
->marked_for_close
) {
1291 tor_assert(or_circ
->rend_splice
);
1292 tor_assert(or_circ
->rend_splice
->rend_splice
== or_circ
);
1294 tor_assert(or_circ
->rend_splice
!= or_circ
);
1296 tor_assert(!or_circ
|| !or_circ
->rend_splice
);