1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2013, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Launch the right sort of circuits and attach streams to them.
13 #include "addressmap.h"
15 #include "circpathbias.h"
16 #include "circuitbuild.h"
17 #include "circuitlist.h"
18 #include "circuitstats.h"
19 #include "circuituse.h"
21 #include "connection.h"
22 #include "connection_edge.h"
24 #include "entrynodes.h"
26 #include "networkstatus.h"
28 #include "rendclient.h"
29 #include "rendcommon.h"
30 #include "rendservice.h"
33 #include "routerlist.h"
35 static void circuit_expire_old_circuits_clientside(void);
36 static void circuit_increment_failure_count(void);
38 /** Return 1 if <b>circ</b> could be returned by circuit_get_best().
42 circuit_is_acceptable(const origin_circuit_t
*origin_circ
,
43 const entry_connection_t
*conn
,
44 int must_be_open
, uint8_t purpose
,
45 int need_uptime
, int need_internal
,
48 const circuit_t
*circ
= TO_CIRCUIT(origin_circ
);
49 const node_t
*exitnode
;
50 cpath_build_state_t
*build_state
;
53 tor_assert(conn
->socks_request
);
55 if (must_be_open
&& (circ
->state
!= CIRCUIT_STATE_OPEN
|| !circ
->n_chan
))
56 return 0; /* ignore non-open circs */
57 if (circ
->marked_for_close
)
60 /* if this circ isn't our purpose, skip. */
61 if (purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
&& !must_be_open
) {
62 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_ESTABLISH_REND
&&
63 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_READY
&&
64 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
&&
65 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_JOINED
)
67 } else if (purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
&&
69 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_INTRODUCING
&&
70 circ
->purpose
!= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
73 if (purpose
!= circ
->purpose
)
77 /* If this is a timed-out hidden service circuit, skip it. */
78 if (origin_circ
->hs_circ_has_timed_out
) {
82 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
||
83 purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
) {
84 if (circ
->timestamp_dirty
&&
85 circ
->timestamp_dirty
+get_options()->MaxCircuitDirtiness
<= now
)
89 if (origin_circ
->unusable_for_new_conns
)
92 /* decide if this circ is suitable for this conn */
94 /* for rend circs, circ->cpath->prev is not the last router in the
95 * circuit, it's the magical extra bob hop. so just check the nickname
96 * of the one we meant to finish at.
98 build_state
= origin_circ
->build_state
;
99 exitnode
= build_state_get_exit_node(build_state
);
101 if (need_uptime
&& !build_state
->need_uptime
)
103 if (need_internal
!= build_state
->is_internal
)
106 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
108 const int family
= tor_addr_parse(&addr
, conn
->socks_request
->address
);
109 if (!exitnode
&& !build_state
->onehop_tunnel
) {
110 log_debug(LD_CIRC
,"Not considering circuit with unknown router.");
111 return 0; /* this circuit is screwed and doesn't know it yet,
112 * or is a rendezvous circuit. */
114 if (build_state
->onehop_tunnel
) {
115 if (!conn
->want_onehop
) {
116 log_debug(LD_CIRC
,"Skipping one-hop circuit.");
119 tor_assert(conn
->chosen_exit_name
);
120 if (build_state
->chosen_exit
) {
121 char digest
[DIGEST_LEN
];
122 if (hexdigest_to_digest(conn
->chosen_exit_name
, digest
) < 0)
123 return 0; /* broken digest, we don't want it */
124 if (tor_memneq(digest
, build_state
->chosen_exit
->identity_digest
,
126 return 0; /* this is a circuit to somewhere else */
127 if (tor_digest_is_zero(digest
)) {
128 /* we don't know the digest; have to compare addr:port */
130 !tor_addr_eq(&build_state
->chosen_exit
->addr
, &addr
) ||
131 build_state
->chosen_exit
->port
!= conn
->socks_request
->port
)
136 if (conn
->want_onehop
) {
137 /* don't use three-hop circuits -- that could hurt our anonymity. */
141 if (origin_circ
->prepend_policy
&& family
!= -1) {
142 int r
= compare_tor_addr_to_addr_policy(&addr
,
143 conn
->socks_request
->port
,
144 origin_circ
->prepend_policy
);
145 if (r
== ADDR_POLICY_REJECTED
)
148 if (exitnode
&& !connection_ap_can_use_exit(conn
, exitnode
)) {
149 /* can't exit from this router */
152 } else { /* not general */
153 const edge_connection_t
*edge_conn
= ENTRY_TO_EDGE_CONN(conn
);
154 if ((edge_conn
->rend_data
&& !origin_circ
->rend_data
) ||
155 (!edge_conn
->rend_data
&& origin_circ
->rend_data
) ||
156 (edge_conn
->rend_data
&& origin_circ
->rend_data
&&
157 rend_cmp_service_ids(edge_conn
->rend_data
->onion_address
,
158 origin_circ
->rend_data
->onion_address
))) {
159 /* this circ is not for this conn */
164 if (!connection_edge_compatible_with_circuit(conn
, origin_circ
)) {
165 /* conn needs to be isolated from other conns that have already used
173 /** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
174 * <b>conn</b>, and return 0 otherwise. Used by circuit_get_best.
177 circuit_is_better(const origin_circuit_t
*oa
, const origin_circuit_t
*ob
,
178 const entry_connection_t
*conn
)
180 const circuit_t
*a
= TO_CIRCUIT(oa
);
181 const circuit_t
*b
= TO_CIRCUIT(ob
);
182 const uint8_t purpose
= ENTRY_TO_CONN(conn
)->purpose
;
185 /* If one of the circuits was allowed to live due to relaxing its timeout,
186 * it is definitely worse (it's probably a much slower path). */
187 if (oa
->relaxed_timeout
&& !ob
->relaxed_timeout
)
188 return 0; /* ob is better. It's not relaxed. */
189 if (!oa
->relaxed_timeout
&& ob
->relaxed_timeout
)
190 return 1; /* oa is better. It's not relaxed. */
193 case CIRCUIT_PURPOSE_C_GENERAL
:
194 /* if it's used but less dirty it's best;
195 * else if it's more recently created it's best
197 if (b
->timestamp_dirty
) {
198 if (a
->timestamp_dirty
&&
199 a
->timestamp_dirty
> b
->timestamp_dirty
)
202 if (a
->timestamp_dirty
||
203 timercmp(&a
->timestamp_began
, &b
->timestamp_began
, >))
205 if (ob
->build_state
->is_internal
)
206 /* XXX023 what the heck is this internal thing doing here. I
207 * think we can get rid of it. circuit_is_acceptable() already
208 * makes sure that is_internal is exactly what we need it to
213 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
214 /* the closer it is to ack_wait the better it is */
215 if (a
->purpose
> b
->purpose
)
218 case CIRCUIT_PURPOSE_C_REND_JOINED
:
219 /* the closer it is to rend_joined the better it is */
220 if (a
->purpose
> b
->purpose
)
225 /* XXXX023 Maybe this check should get a higher priority to avoid
226 * using up circuits too rapidly. */
228 a_bits
= connection_edge_update_circuit_isolation(conn
,
229 (origin_circuit_t
*)oa
, 1);
230 b_bits
= connection_edge_update_circuit_isolation(conn
,
231 (origin_circuit_t
*)ob
, 1);
232 /* if x_bits < 0, then we have not used x for anything; better not to dirty
233 * a connection if we can help it. */
236 } else if (b_bits
< 0) {
239 a_bits
&= ~ oa
->isolation_flags_mixed
;
240 a_bits
&= ~ ob
->isolation_flags_mixed
;
241 if (n_bits_set_u8(a_bits
) < n_bits_set_u8(b_bits
)) {
242 /* The fewer new restrictions we need to make on a circuit for stream
243 * isolation, the better. */
250 /** Find the best circ that conn can use, preferably one which is
251 * dirty. Circ must not be too old.
253 * Conn must be defined.
255 * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
257 * circ_purpose specifies what sort of circuit we must have.
258 * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
260 * If it's REND_JOINED and must_be_open==0, then return the closest
261 * rendezvous-purposed circuit that you can find.
263 * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
264 * closest introduce-purposed circuit that you can find.
266 static origin_circuit_t
*
267 circuit_get_best(const entry_connection_t
*conn
,
268 int must_be_open
, uint8_t purpose
,
269 int need_uptime
, int need_internal
)
272 origin_circuit_t
*best
=NULL
;
274 int intro_going_on_but_too_old
= 0;
278 tor_assert(purpose
== CIRCUIT_PURPOSE_C_GENERAL
||
279 purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
||
280 purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
);
282 tor_gettimeofday(&now
);
284 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
285 origin_circuit_t
*origin_circ
;
286 if (!CIRCUIT_IS_ORIGIN(circ
))
288 origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
290 /* Log an info message if we're going to launch a new intro circ in
292 if (purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
&&
293 !must_be_open
&& origin_circ
->hs_circ_has_timed_out
) {
294 intro_going_on_but_too_old
= 1;
298 if (!circuit_is_acceptable(origin_circ
,conn
,must_be_open
,purpose
,
299 need_uptime
,need_internal
, (time_t)now
.tv_sec
))
302 /* now this is an acceptable circ to hand back. but that doesn't
303 * mean it's the *best* circ to hand back. try to decide.
305 if (!best
|| circuit_is_better(origin_circ
,best
,conn
))
309 if (!best
&& intro_going_on_but_too_old
)
310 log_info(LD_REND
|LD_CIRC
, "There is an intro circuit being created "
311 "right now, but it has already taken quite a while. Starting "
317 /** Return the number of not-yet-open general-purpose origin circuits. */
319 count_pending_general_client_circuits(void)
321 const circuit_t
*circ
;
325 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
326 if (circ
->marked_for_close
||
327 circ
->state
== CIRCUIT_STATE_OPEN
||
328 circ
->purpose
!= CIRCUIT_PURPOSE_C_GENERAL
||
329 !CIRCUIT_IS_ORIGIN(circ
))
339 /** Check whether, according to the policies in <b>options</b>, the
340 * circuit <b>circ</b> makes sense. */
341 /* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
342 * Also, it doesn't have the right definition of an exit circuit. Also,
343 * it's never called. */
345 circuit_conforms_to_options(const origin_circuit_t
*circ
,
346 const or_options_t
*options
)
348 const crypt_path_t
*cpath
, *cpath_next
= NULL
;
350 /* first check if it includes any excluded nodes */
351 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
352 cpath_next
= cpath
->next
;
353 if (routerset_contains_extendinfo(options
->ExcludeNodes
,
358 /* then consider the final hop */
359 if (routerset_contains_extendinfo(options
->ExcludeExitNodes
,
360 circ
->cpath
->prev
->extend_info
))
367 /** Close all circuits that start at us, aren't open, and were born
368 * at least CircuitBuildTimeout seconds ago.
371 circuit_expire_building(void)
373 circuit_t
*victim
, *next_circ
;
374 /* circ_times.timeout_ms and circ_times.close_ms are from
375 * circuit_build_times_get_initial_timeout() if we haven't computed
376 * custom timeouts yet */
377 struct timeval general_cutoff
, begindir_cutoff
, fourhop_cutoff
,
378 close_cutoff
, extremely_old_cutoff
, hs_extremely_old_cutoff
,
379 cannibalized_cutoff
, c_intro_cutoff
, s_intro_cutoff
, stream_cutoff
;
380 const or_options_t
*options
= get_options();
382 cpath_build_state_t
*build_state
;
383 int any_opened_circs
= 0;
385 tor_gettimeofday(&now
);
387 /* Check to see if we have any opened circuits. If we don't,
388 * we want to be more lenient with timeouts, in case the
389 * user has relocated and/or changed network connections.
391 TOR_LIST_FOREACH(next_circ
, circuit_get_global_list(), head
) {
392 if (!CIRCUIT_IS_ORIGIN(next_circ
) || /* didn't originate here */
393 next_circ
->marked_for_close
) { /* don't mess with marked circs */
397 if (TO_ORIGIN_CIRCUIT(next_circ
)->has_opened
&&
398 next_circ
->state
== CIRCUIT_STATE_OPEN
&&
399 TO_ORIGIN_CIRCUIT(next_circ
)->build_state
&&
400 TO_ORIGIN_CIRCUIT(next_circ
)->build_state
->desired_path_len
401 == DEFAULT_ROUTE_LEN
) {
402 any_opened_circs
= 1;
407 #define SET_CUTOFF(target, msec) do { \
408 long ms = tor_lround(msec); \
409 struct timeval diff; \
410 diff.tv_sec = ms / 1000; \
411 diff.tv_usec = (int)((ms % 1000) * 1000); \
412 timersub(&now, &diff, &target); \
416 * Because circuit build timeout is calculated only based on 3 hop
417 * general purpose circuit construction, we need to scale the timeout
418 * to make it properly apply to longer circuits, and circuits of
419 * certain usage types. The following diagram illustrates how we
420 * derive the scaling below. In short, we calculate the number
421 * of times our telescoping-based circuit construction causes cells
422 * to traverse each link for the circuit purpose types in question,
423 * and then assume each link is equivalent.
425 * OP --a--> A --b--> B --c--> C
426 * OP --a--> A --b--> B --c--> C --d--> D
428 * Let h = a = b = c = d
430 * Three hops (general_cutoff)
437 * RTTs = 4a + 3b + 2c + d
439 * Client INTRODUCE1+ACK: // XXX: correct?
440 * RTTs = 5a + 4b + 3c + 2d
443 * RTTs = 4a + 3b + 2c
446 SET_CUTOFF(general_cutoff
, get_circuit_build_timeout_ms());
447 SET_CUTOFF(begindir_cutoff
, get_circuit_build_timeout_ms());
449 /* > 3hop circs seem to have a 1.0 second delay on their cannibalized
451 SET_CUTOFF(fourhop_cutoff
, get_circuit_build_timeout_ms() * (10/6.0) + 1000);
453 /* CIRCUIT_PURPOSE_C_ESTABLISH_REND behaves more like a RELAY cell.
454 * Use the stream cutoff (more or less). */
455 SET_CUTOFF(stream_cutoff
, MAX(options
->CircuitStreamTimeout
,15)*1000 + 1000);
457 /* Be lenient with cannibalized circs. They already survived the official
458 * CBT, and they're usually not performance-critical. */
459 SET_CUTOFF(cannibalized_cutoff
,
460 MAX(get_circuit_build_close_time_ms()*(4/6.0),
461 options
->CircuitStreamTimeout
* 1000) + 1000);
463 /* Intro circs have an extra round trip (and are also 4 hops long) */
464 SET_CUTOFF(c_intro_cutoff
, get_circuit_build_timeout_ms() * (14/6.0) + 1000);
466 /* Server intro circs have an extra round trip */
467 SET_CUTOFF(s_intro_cutoff
, get_circuit_build_timeout_ms() * (9/6.0) + 1000);
469 SET_CUTOFF(close_cutoff
, get_circuit_build_close_time_ms());
470 SET_CUTOFF(extremely_old_cutoff
, get_circuit_build_close_time_ms()*2 + 1000);
472 SET_CUTOFF(hs_extremely_old_cutoff
,
473 MAX(get_circuit_build_close_time_ms()*2 + 1000,
474 options
->SocksTimeout
* 1000));
476 TOR_LIST_FOREACH(next_circ
, circuit_get_global_list(), head
) {
477 struct timeval cutoff
;
479 if (!CIRCUIT_IS_ORIGIN(victim
) || /* didn't originate here */
480 victim
->marked_for_close
) /* don't mess with marked circs */
483 /* If we haven't yet started the first hop, it means we don't have
484 * any orconns available, and thus have not started counting time yet
485 * for this circuit. See circuit_deliver_create_cell() and uses of
488 * Continue to wait in this case. The ORConn should timeout
489 * independently and kill us then.
491 if (TO_ORIGIN_CIRCUIT(victim
)->cpath
->state
== CPATH_STATE_CLOSED
) {
495 build_state
= TO_ORIGIN_CIRCUIT(victim
)->build_state
;
496 if (build_state
&& build_state
->onehop_tunnel
)
497 cutoff
= begindir_cutoff
;
498 else if (victim
->purpose
== CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
)
499 cutoff
= close_cutoff
;
500 else if (victim
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
||
501 victim
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
502 cutoff
= c_intro_cutoff
;
503 else if (victim
->purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
)
504 cutoff
= s_intro_cutoff
;
505 else if (victim
->purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
)
506 cutoff
= stream_cutoff
;
507 else if (victim
->purpose
== CIRCUIT_PURPOSE_PATH_BIAS_TESTING
)
508 cutoff
= close_cutoff
;
509 else if (TO_ORIGIN_CIRCUIT(victim
)->has_opened
&&
510 victim
->state
!= CIRCUIT_STATE_OPEN
)
511 cutoff
= cannibalized_cutoff
;
512 else if (build_state
&& build_state
->desired_path_len
>= 4)
513 cutoff
= fourhop_cutoff
;
515 cutoff
= general_cutoff
;
517 if (TO_ORIGIN_CIRCUIT(victim
)->hs_circ_has_timed_out
)
518 cutoff
= hs_extremely_old_cutoff
;
520 if (timercmp(&victim
->timestamp_began
, &cutoff
, >))
521 continue; /* it's still young, leave it alone */
523 /* We need to double-check the opened state here because
524 * we don't want to consider opened 1-hop dircon circuits for
525 * deciding when to relax the timeout, but we *do* want to relax
526 * those circuits too if nothing else is opened *and* they still
528 if (!any_opened_circs
&& victim
->state
!= CIRCUIT_STATE_OPEN
) {
529 /* It's still young enough that we wouldn't close it, right? */
530 if (timercmp(&victim
->timestamp_began
, &close_cutoff
, >)) {
531 if (!TO_ORIGIN_CIRCUIT(victim
)->relaxed_timeout
) {
532 int first_hop_succeeded
= TO_ORIGIN_CIRCUIT(victim
)->cpath
->state
535 "No circuits are opened. Relaxing timeout for circuit %d "
536 "(a %s %d-hop circuit in state %s with channel state %s). "
537 "%d guards are live.",
538 TO_ORIGIN_CIRCUIT(victim
)->global_identifier
,
539 circuit_purpose_to_string(victim
->purpose
),
540 TO_ORIGIN_CIRCUIT(victim
)->build_state
?
541 TO_ORIGIN_CIRCUIT(victim
)->build_state
->desired_path_len
:
543 circuit_state_to_string(victim
->state
),
544 channel_state_to_string(victim
->n_chan
->state
),
545 num_live_entry_guards(0));
547 /* We count the timeout here for CBT, because technically this
548 * was a timeout, and the timeout value needs to reset if we
549 * see enough of them. Note this means we also need to avoid
550 * double-counting below, too. */
551 circuit_build_times_count_timeout(get_circuit_build_times_mutable(),
552 first_hop_succeeded
);
553 TO_ORIGIN_CIRCUIT(victim
)->relaxed_timeout
= 1;
557 static ratelim_t relax_timeout_limit
= RATELIM_INIT(3600);
558 const double build_close_ms
= get_circuit_build_close_time_ms();
559 log_fn_ratelim(&relax_timeout_limit
, LOG_NOTICE
, LD_CIRC
,
560 "No circuits are opened. Relaxed timeout for circuit %d "
561 "(a %s %d-hop circuit in state %s with channel state %s) to "
562 "%ldms. However, it appears the circuit has timed out "
563 "anyway. %d guards are live.",
564 TO_ORIGIN_CIRCUIT(victim
)->global_identifier
,
565 circuit_purpose_to_string(victim
->purpose
),
566 TO_ORIGIN_CIRCUIT(victim
)->build_state
?
567 TO_ORIGIN_CIRCUIT(victim
)->build_state
->desired_path_len
:
569 circuit_state_to_string(victim
->state
),
570 channel_state_to_string(victim
->n_chan
->state
),
571 (long)build_close_ms
,
572 num_live_entry_guards(0));
577 /* some debug logs, to help track bugs */
578 if (victim
->purpose
>= CIRCUIT_PURPOSE_C_INTRODUCING
&&
579 victim
->purpose
<= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
) {
580 if (!victim
->timestamp_dirty
)
581 log_fn(LOG_DEBUG
,"Considering %sopen purpose %d to %s (circid %d)."
583 victim
->state
== CIRCUIT_STATE_OPEN
? "" : "non",
584 victim
->purpose
, victim
->build_state
->chosen_exit_name
,
587 log_fn(LOG_DEBUG
,"Considering %sopen purpose %d to %s (circid %d). "
588 "%d secs since dirty.",
589 victim
->state
== CIRCUIT_STATE_OPEN
? "" : "non",
590 victim
->purpose
, victim
->build_state
->chosen_exit_name
,
592 (int)(now
- victim
->timestamp_dirty
));
596 /* if circ is !open, or if it's open but purpose is a non-finished
597 * intro or rend, then mark it for close */
598 if (victim
->state
== CIRCUIT_STATE_OPEN
) {
599 switch (victim
->purpose
) {
600 default: /* most open circuits can be left alone. */
601 continue; /* yes, continue inside a switch refers to the nearest
602 * enclosing loop. C is smart. */
603 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
604 break; /* too old, need to die */
605 case CIRCUIT_PURPOSE_C_REND_READY
:
606 /* it's a rend_ready circ -- has it already picked a query? */
607 /* c_rend_ready circs measure age since timestamp_dirty,
608 * because that's set when they switch purposes
610 if (TO_ORIGIN_CIRCUIT(victim
)->rend_data
||
611 victim
->timestamp_dirty
> cutoff
.tv_sec
)
614 case CIRCUIT_PURPOSE_PATH_BIAS_TESTING
:
615 /* Open path bias testing circuits are given a long
616 * time to complete the test, but not forever */
617 TO_ORIGIN_CIRCUIT(victim
)->path_state
= PATH_STATE_USE_FAILED
;
619 case CIRCUIT_PURPOSE_C_INTRODUCING
:
620 /* We keep old introducing circuits around for
621 * a while in parallel, and they can end up "opened".
622 * We decide below if we're going to mark them timed
623 * out and eventually close them.
626 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
627 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
628 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
629 /* rend and intro circs become dirty each time they
630 * make an introduction attempt. so timestamp_dirty
631 * will reflect the time since the last attempt.
633 if (victim
->timestamp_dirty
> cutoff
.tv_sec
)
637 } else { /* circuit not open, consider recording failure as timeout */
638 int first_hop_succeeded
= TO_ORIGIN_CIRCUIT(victim
)->cpath
&&
639 TO_ORIGIN_CIRCUIT(victim
)->cpath
->state
== CPATH_STATE_OPEN
;
641 if (TO_ORIGIN_CIRCUIT(victim
)->p_streams
!= NULL
) {
642 log_warn(LD_BUG
, "Circuit %d (purpose %d, %s) has timed out, "
643 "yet has attached streams!",
644 TO_ORIGIN_CIRCUIT(victim
)->global_identifier
,
646 circuit_purpose_to_string(victim
->purpose
));
647 tor_fragile_assert();
651 if (circuit_timeout_want_to_count_circ(TO_ORIGIN_CIRCUIT(victim
)) &&
652 circuit_build_times_enough_to_compute(get_circuit_build_times())) {
653 /* Circuits are allowed to last longer for measurement.
654 * Switch their purpose and wait. */
655 if (victim
->purpose
!= CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
) {
656 control_event_circuit_status(TO_ORIGIN_CIRCUIT(victim
),
658 END_CIRC_REASON_TIMEOUT
);
659 circuit_change_purpose(victim
, CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
);
660 /* Record this failure to check for too many timeouts
661 * in a row. This function does not record a time value yet
662 * (we do that later); it only counts the fact that we did
663 * have a timeout. We also want to avoid double-counting
664 * already "relaxed" circuits, which are counted above. */
665 if (!TO_ORIGIN_CIRCUIT(victim
)->relaxed_timeout
) {
666 circuit_build_times_count_timeout(
667 get_circuit_build_times_mutable(),
668 first_hop_succeeded
);
674 * If the circuit build time is much greater than we would have cut
675 * it off at, we probably had a suspend event along this codepath,
676 * and we should discard the value.
678 if (timercmp(&victim
->timestamp_began
, &extremely_old_cutoff
, <)) {
680 "Extremely large value for circuit build timeout: %lds. "
681 "Assuming clock jump. Purpose %d (%s)",
682 (long)(now
.tv_sec
- victim
->timestamp_began
.tv_sec
),
684 circuit_purpose_to_string(victim
->purpose
));
685 } else if (circuit_build_times_count_close(
686 get_circuit_build_times_mutable(),
688 (time_t)victim
->timestamp_created
.tv_sec
)) {
689 circuit_build_times_set_timeout(get_circuit_build_times_mutable());
694 /* If this is a hidden service client circuit which is far enough
695 * along in connecting to its destination, and we haven't already
696 * flagged it as 'timed out', and the user has not told us to
697 * close such circs immediately on timeout, flag it as 'timed out'
698 * so we'll launch another intro or rend circ, but don't mark it
701 * (Circs flagged as 'timed out' are given a much longer timeout
702 * period above, so we won't close them in the next call to
703 * circuit_expire_building.) */
704 if (!(options
->CloseHSClientCircuitsImmediatelyOnTimeout
) &&
705 !(TO_ORIGIN_CIRCUIT(victim
)->hs_circ_has_timed_out
)) {
706 switch (victim
->purpose
) {
707 case CIRCUIT_PURPOSE_C_REND_READY
:
708 /* We only want to spare a rend circ if it has been specified in
709 * an INTRODUCE1 cell sent to a hidden service. A circ's
710 * pending_final_cpath field is non-NULL iff it is a rend circ
711 * and we have tried to send an INTRODUCE1 cell specifying it.
712 * Thus, if the pending_final_cpath field *is* NULL, then we
713 * want to not spare it. */
714 if (TO_ORIGIN_CIRCUIT(victim
)->build_state
&&
715 TO_ORIGIN_CIRCUIT(victim
)->build_state
->pending_final_cpath
==
719 case CIRCUIT_PURPOSE_C_INTRODUCING
:
720 /* connection_ap_handshake_attach_circuit() will relaunch for us */
721 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
722 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
723 /* If we have reached this line, we want to spare the circ for now. */
724 log_info(LD_CIRC
,"Marking circ %u (state %d:%s, purpose %d) "
725 "as timed-out HS circ",
726 (unsigned)victim
->n_circ_id
,
727 victim
->state
, circuit_state_to_string(victim
->state
),
729 TO_ORIGIN_CIRCUIT(victim
)->hs_circ_has_timed_out
= 1;
736 /* If this is a service-side rendezvous circuit which is far
737 * enough along in connecting to its destination, consider sparing
739 if (!(options
->CloseHSServiceRendCircuitsImmediatelyOnTimeout
) &&
740 !(TO_ORIGIN_CIRCUIT(victim
)->hs_circ_has_timed_out
) &&
741 victim
->purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
) {
742 log_info(LD_CIRC
,"Marking circ %u (state %d:%s, purpose %d) "
743 "as timed-out HS circ; relaunching rendezvous attempt.",
744 (unsigned)victim
->n_circ_id
,
745 victim
->state
, circuit_state_to_string(victim
->state
),
747 TO_ORIGIN_CIRCUIT(victim
)->hs_circ_has_timed_out
= 1;
748 rend_service_relaunch_rendezvous(TO_ORIGIN_CIRCUIT(victim
));
754 "Abandoning circ %u %s:%u (state %d,%d:%s, purpose %d, "
755 "len %d)", TO_ORIGIN_CIRCUIT(victim
)->global_identifier
,
756 channel_get_canonical_remote_descr(victim
->n_chan
),
757 (unsigned)victim
->n_circ_id
,
758 TO_ORIGIN_CIRCUIT(victim
)->has_opened
,
759 victim
->state
, circuit_state_to_string(victim
->state
),
761 TO_ORIGIN_CIRCUIT(victim
)->build_state
?
762 TO_ORIGIN_CIRCUIT(victim
)->build_state
->desired_path_len
:
766 "Abandoning circ %u %u (state %d,%d:%s, purpose %d, len %d)",
767 TO_ORIGIN_CIRCUIT(victim
)->global_identifier
,
768 (unsigned)victim
->n_circ_id
,
769 TO_ORIGIN_CIRCUIT(victim
)->has_opened
,
771 circuit_state_to_string(victim
->state
), victim
->purpose
,
772 TO_ORIGIN_CIRCUIT(victim
)->build_state
?
773 TO_ORIGIN_CIRCUIT(victim
)->build_state
->desired_path_len
:
776 circuit_log_path(LOG_INFO
,LD_CIRC
,TO_ORIGIN_CIRCUIT(victim
));
777 if (victim
->purpose
== CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
)
778 circuit_mark_for_close(victim
, END_CIRC_REASON_MEASUREMENT_EXPIRED
);
780 circuit_mark_for_close(victim
, END_CIRC_REASON_TIMEOUT
);
782 pathbias_count_timeout(TO_ORIGIN_CIRCUIT(victim
));
786 /** For debugging #8387: track when we last called
787 * circuit_expire_old_circuits_clientside. */
788 static time_t last_expired_clientside_circuits
= 0;
791 * As a diagnostic for bug 8387, log information about how many one-hop
792 * circuits we have around that have been there for at least <b>age</b>
793 * seconds. Log a few of them.
796 circuit_log_ancient_one_hop_circuits(int age
)
798 #define MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG 10
799 time_t now
= time(NULL
);
800 time_t cutoff
= now
- age
;
802 smartlist_t
*log_these
= smartlist_new();
803 const circuit_t
*circ
;
805 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
806 const origin_circuit_t
*ocirc
;
807 if (! CIRCUIT_IS_ORIGIN(circ
))
809 if (circ
->timestamp_created
.tv_sec
>= cutoff
)
811 ocirc
= CONST_TO_ORIGIN_CIRCUIT(circ
);
813 if (ocirc
->build_state
&& ocirc
->build_state
->onehop_tunnel
) {
816 if (smartlist_len(log_these
) < MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG
)
817 smartlist_add(log_these
, (origin_circuit_t
*) ocirc
);
824 log_notice(LD_HEARTBEAT
,
825 "Diagnostic for issue 8387: Found %d one-hop circuits more "
826 "than %d seconds old! Logging %d...",
827 n_found
, age
, smartlist_len(log_these
));
829 SMARTLIST_FOREACH_BEGIN(log_these
, const origin_circuit_t
*, ocirc
) {
830 char created
[ISO_TIME_LEN
+1];
832 const edge_connection_t
*conn
;
834 circ
= TO_CIRCUIT(ocirc
);
836 format_local_iso_time(created
,
837 (time_t)circ
->timestamp_created
.tv_sec
);
839 if (circ
->timestamp_dirty
) {
840 char dirty_since
[ISO_TIME_LEN
+1];
841 format_local_iso_time(dirty_since
, circ
->timestamp_dirty
);
843 tor_asprintf(&dirty
, "Dirty since %s (%ld seconds vs %ld-second cutoff)",
844 dirty_since
, (long)(now
- circ
->timestamp_dirty
),
845 (long) get_options()->MaxCircuitDirtiness
);
847 dirty
= tor_strdup("Not marked dirty");
850 log_notice(LD_HEARTBEAT
, " #%d created at %s. %s, %s. %s for close. "
851 "%s for new conns. %s.",
854 circuit_state_to_string(circ
->state
),
855 circuit_purpose_to_string(circ
->purpose
),
856 circ
->marked_for_close
? "Marked" : "Not marked",
857 ocirc
->unusable_for_new_conns
? "Not usable" : "usable",
862 for (conn
= ocirc
->p_streams
; conn
; conn
= conn
->next_stream
) {
863 const connection_t
*c
= TO_CONN(conn
);
864 char stream_created
[ISO_TIME_LEN
+1];
865 if (++stream_num
>= 5)
868 format_local_iso_time(stream_created
, c
->timestamp_created
);
870 log_notice(LD_HEARTBEAT
, " Stream#%d created at %s. "
871 "%s conn in state %s. "
872 "%s for close (%s:%d). Hold-open is %sset. "
873 "Has %ssent RELAY_END. %s on circuit.",
876 conn_type_to_string(c
->type
),
877 conn_state_to_string(c
->type
, c
->state
),
878 c
->marked_for_close
? "Marked" : "Not marked",
879 c
->marked_for_close_file
? c
->marked_for_close_file
: "--",
881 c
->hold_open_until_flushed
? "" : "not ",
882 conn
->edge_has_sent_end
? "" : "not ",
883 conn
->edge_blocked_on_circ
? "Blocked" : "Not blocked");
884 if (! c
->linked_conn
)
889 log_notice(LD_HEARTBEAT
, " Linked to %s connection in state %s "
890 "(Purpose %d). %s for close (%s:%d). Hold-open is %sset. ",
891 conn_type_to_string(c
->type
),
892 conn_state_to_string(c
->type
, c
->state
),
894 c
->marked_for_close
? "Marked" : "Not marked",
895 c
->marked_for_close_file
? c
->marked_for_close_file
: "--",
897 c
->hold_open_until_flushed
? "" : "not ");
899 } SMARTLIST_FOREACH_END(ocirc
);
901 log_notice(LD_HEARTBEAT
, "It has been %ld seconds since I last called "
902 "circuit_expire_old_circuits_clientside().",
903 (long)(now
- last_expired_clientside_circuits
));
906 smartlist_free(log_these
);
909 /** Remove any elements in <b>needed_ports</b> that are handled by an
910 * open or in-progress circuit.
913 circuit_remove_handled_ports(smartlist_t
*needed_ports
)
918 for (i
= 0; i
< smartlist_len(needed_ports
); ++i
) {
919 port
= smartlist_get(needed_ports
, i
);
921 if (circuit_stream_is_being_handled(NULL
, *port
,
922 MIN_CIRCUITS_HANDLING_STREAM
)) {
923 // log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
924 smartlist_del(needed_ports
, i
--);
927 log_debug(LD_CIRC
,"Port %d is not handled.", *port
);
932 /** Return 1 if at least <b>min</b> general-purpose non-internal circuits
933 * will have an acceptable exit node for exit stream <b>conn</b> if it
934 * is defined, else for "*:port".
938 circuit_stream_is_being_handled(entry_connection_t
*conn
,
939 uint16_t port
, int min
)
942 const node_t
*exitnode
;
944 time_t now
= time(NULL
);
945 int need_uptime
= smartlist_contains_int_as_string(
946 get_options()->LongLivedPorts
,
947 conn
? conn
->socks_request
->port
: port
);
949 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
950 if (CIRCUIT_IS_ORIGIN(circ
) &&
951 !circ
->marked_for_close
&&
952 circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
953 (!circ
->timestamp_dirty
||
954 circ
->timestamp_dirty
+ get_options()->MaxCircuitDirtiness
> now
)) {
955 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
956 cpath_build_state_t
*build_state
= origin_circ
->build_state
;
957 if (build_state
->is_internal
|| build_state
->onehop_tunnel
)
959 if (origin_circ
->unusable_for_new_conns
)
962 exitnode
= build_state_get_exit_node(build_state
);
963 if (exitnode
&& (!need_uptime
|| build_state
->need_uptime
)) {
966 ok
= connection_ap_can_use_exit(conn
, exitnode
);
968 addr_policy_result_t r
;
969 r
= compare_tor_addr_to_node_policy(NULL
, port
, exitnode
);
970 ok
= r
!= ADDR_POLICY_REJECTED
&& r
!= ADDR_POLICY_PROBABLY_REJECTED
;
982 /** Don't keep more than this many unused open circuits around. */
983 #define MAX_UNUSED_OPEN_CIRCUITS 14
985 /** Figure out how many circuits we have open that are clean. Make
986 * sure it's enough for all the upcoming behaviors we predict we'll have.
987 * But put an upper bound on the total number of circuits.
990 circuit_predict_and_launch_new(void)
993 int num
=0, num_internal
=0, num_uptime_internal
=0;
994 int hidserv_needs_uptime
=0, hidserv_needs_capacity
=1;
995 int port_needs_uptime
=0, port_needs_capacity
=1;
996 time_t now
= time(NULL
);
999 /* First, count how many of each type of circuit we have already. */
1000 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
1001 cpath_build_state_t
*build_state
;
1002 origin_circuit_t
*origin_circ
;
1003 if (!CIRCUIT_IS_ORIGIN(circ
))
1005 if (circ
->marked_for_close
)
1006 continue; /* don't mess with marked circs */
1007 if (circ
->timestamp_dirty
)
1008 continue; /* only count clean circs */
1009 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_GENERAL
)
1010 continue; /* only pay attention to general-purpose circs */
1011 origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
1012 if (origin_circ
->unusable_for_new_conns
)
1014 build_state
= origin_circ
->build_state
;
1015 if (build_state
->onehop_tunnel
)
1018 if (build_state
->is_internal
)
1020 if (build_state
->need_uptime
&& build_state
->is_internal
)
1021 num_uptime_internal
++;
1024 /* If that's enough, then stop now. */
1025 if (num
>= MAX_UNUSED_OPEN_CIRCUITS
)
1026 return; /* we already have many, making more probably will hurt */
1028 /* Second, see if we need any more exit circuits. */
1029 /* check if we know of a port that's been requested recently
1030 * and no circuit is currently available that can handle it. */
1031 if (!circuit_all_predicted_ports_handled(now
, &port_needs_uptime
,
1032 &port_needs_capacity
)) {
1033 if (port_needs_uptime
)
1034 flags
|= CIRCLAUNCH_NEED_UPTIME
;
1035 if (port_needs_capacity
)
1036 flags
|= CIRCLAUNCH_NEED_CAPACITY
;
1038 "Have %d clean circs (%d internal), need another exit circ.",
1040 circuit_launch(CIRCUIT_PURPOSE_C_GENERAL
, flags
);
1044 /* Third, see if we need any more hidden service (server) circuits. */
1045 if (num_rend_services() && num_uptime_internal
< 3) {
1046 flags
= (CIRCLAUNCH_NEED_CAPACITY
| CIRCLAUNCH_NEED_UPTIME
|
1047 CIRCLAUNCH_IS_INTERNAL
);
1049 "Have %d clean circs (%d internal), need another internal "
1050 "circ for my hidden service.",
1052 circuit_launch(CIRCUIT_PURPOSE_C_GENERAL
, flags
);
1056 /* Fourth, see if we need any more hidden service (client) circuits. */
1057 if (rep_hist_get_predicted_internal(now
, &hidserv_needs_uptime
,
1058 &hidserv_needs_capacity
) &&
1059 ((num_uptime_internal
<2 && hidserv_needs_uptime
) ||
1061 if (hidserv_needs_uptime
)
1062 flags
|= CIRCLAUNCH_NEED_UPTIME
;
1063 if (hidserv_needs_capacity
)
1064 flags
|= CIRCLAUNCH_NEED_CAPACITY
;
1065 flags
|= CIRCLAUNCH_IS_INTERNAL
;
1067 "Have %d clean circs (%d uptime-internal, %d internal), need"
1068 " another hidden service circ.",
1069 num
, num_uptime_internal
, num_internal
);
1070 circuit_launch(CIRCUIT_PURPOSE_C_GENERAL
, flags
);
1074 /* Finally, check to see if we still need more circuits to learn
1075 * a good build timeout. But if we're close to our max number we
1076 * want, don't do another -- we want to leave a few slots open so
1077 * we can still build circuits preemptively as needed. */
1078 if (num
< MAX_UNUSED_OPEN_CIRCUITS
-2 &&
1079 ! circuit_build_times_disabled() &&
1080 circuit_build_times_needs_circuits_now(get_circuit_build_times())) {
1081 flags
= CIRCLAUNCH_NEED_CAPACITY
;
1083 "Have %d clean circs need another buildtime test circ.", num
);
1084 circuit_launch(CIRCUIT_PURPOSE_C_GENERAL
, flags
);
1089 /** Build a new test circuit every 5 minutes */
1090 #define TESTING_CIRCUIT_INTERVAL 300
1092 /** This function is called once a second, if router_have_min_dir_info() is
1093 * true. Its job is to make sure all services we offer have enough circuits
1094 * available. Some services just want enough circuits for current tasks,
1095 * whereas others want a minimum set of idle circuits hanging around.
1098 circuit_build_needed_circs(time_t now
)
1100 const or_options_t
*options
= get_options();
1102 /* launch a new circ for any pending streams that need one */
1103 connection_ap_attach_pending();
1105 /* make sure any hidden services have enough intro points */
1106 rend_services_introduce();
1108 circuit_expire_old_circs_as_needed(now
);
1110 if (!options
->DisablePredictedCircuits
)
1111 circuit_predict_and_launch_new();
1115 * Called once a second either directly or from
1116 * circuit_build_needed_circs(). As appropriate (once per NewCircuitPeriod)
1117 * resets failure counts and expires old circuits.
1120 circuit_expire_old_circs_as_needed(time_t now
)
1122 static time_t time_to_expire_and_reset
= 0;
1124 if (time_to_expire_and_reset
< now
) {
1125 circuit_reset_failure_count(1);
1126 time_to_expire_and_reset
= now
+ get_options()->NewCircuitPeriod
;
1127 if (proxy_mode(get_options()))
1128 addressmap_clean(now
);
1129 circuit_expire_old_circuits_clientside();
1131 #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
1133 /* If we ever re-enable, this has to move into
1134 * circuit_build_needed_circs */
1136 circ
= circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL
);
1137 if (get_options()->RunTesting
&&
1139 circ
->timestamp_began
.tv_sec
+ TESTING_CIRCUIT_INTERVAL
< now
) {
1140 log_fn(LOG_INFO
,"Creating a new testing circuit.");
1141 circuit_launch(CIRCUIT_PURPOSE_C_GENERAL
, 0);
1147 /** If the stream <b>conn</b> is a member of any of the linked
1148 * lists of <b>circ</b>, then remove it from the list.
1151 circuit_detach_stream(circuit_t
*circ
, edge_connection_t
*conn
)
1153 edge_connection_t
*prevconn
;
1158 if (conn
->base_
.type
== CONN_TYPE_AP
) {
1159 entry_connection_t
*entry_conn
= EDGE_TO_ENTRY_CONN(conn
);
1160 entry_conn
->may_use_optimistic_data
= 0;
1162 conn
->cpath_layer
= NULL
; /* don't keep a stale pointer */
1163 conn
->on_circuit
= NULL
;
1165 if (CIRCUIT_IS_ORIGIN(circ
)) {
1166 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
1167 if (conn
== origin_circ
->p_streams
) {
1168 origin_circ
->p_streams
= conn
->next_stream
;
1172 for (prevconn
= origin_circ
->p_streams
;
1173 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
1174 prevconn
= prevconn
->next_stream
)
1176 if (prevconn
&& prevconn
->next_stream
) {
1177 prevconn
->next_stream
= conn
->next_stream
;
1181 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
1182 if (conn
== or_circ
->n_streams
) {
1183 or_circ
->n_streams
= conn
->next_stream
;
1186 if (conn
== or_circ
->resolving_streams
) {
1187 or_circ
->resolving_streams
= conn
->next_stream
;
1191 for (prevconn
= or_circ
->n_streams
;
1192 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
1193 prevconn
= prevconn
->next_stream
)
1195 if (prevconn
&& prevconn
->next_stream
) {
1196 prevconn
->next_stream
= conn
->next_stream
;
1200 for (prevconn
= or_circ
->resolving_streams
;
1201 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
1202 prevconn
= prevconn
->next_stream
)
1204 if (prevconn
&& prevconn
->next_stream
) {
1205 prevconn
->next_stream
= conn
->next_stream
;
1210 log_warn(LD_BUG
,"Edge connection not in circuit's list.");
1211 /* Don't give an error here; it's harmless. */
1212 tor_fragile_assert();
1215 /** If we haven't yet decided on a good timeout value for circuit
1216 * building, we close idles circuits aggressively so we can get more
1218 #define IDLE_TIMEOUT_WHILE_LEARNING (10*60)
1220 /** Find each circuit that has been unused for too long, or dirty
1221 * for too long and has no streams on it: mark it for close.
1224 circuit_expire_old_circuits_clientside(void)
1227 struct timeval cutoff
, now
;
1229 tor_gettimeofday(&now
);
1231 last_expired_clientside_circuits
= now
.tv_sec
;
1233 if (! circuit_build_times_disabled() &&
1234 circuit_build_times_needs_circuits(get_circuit_build_times())) {
1235 /* Circuits should be shorter lived if we need more of them
1236 * for learning a good build timeout */
1237 cutoff
.tv_sec
-= IDLE_TIMEOUT_WHILE_LEARNING
;
1239 cutoff
.tv_sec
-= get_options()->CircuitIdleTimeout
;
1242 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
1243 if (circ
->marked_for_close
|| !CIRCUIT_IS_ORIGIN(circ
))
1245 /* If the circuit has been dirty for too long, and there are no streams
1246 * on it, mark it for close.
1248 if (circ
->timestamp_dirty
&&
1249 circ
->timestamp_dirty
+ get_options()->MaxCircuitDirtiness
<
1251 !TO_ORIGIN_CIRCUIT(circ
)->p_streams
/* nothing attached */ ) {
1252 log_debug(LD_CIRC
, "Closing n_circ_id %u (dirty %ld sec ago, "
1254 (unsigned)circ
->n_circ_id
,
1255 (long)(now
.tv_sec
- circ
->timestamp_dirty
),
1257 /* Don't do this magic for testing circuits. Their death is governed
1258 * by circuit_expire_building */
1259 if (circ
->purpose
!= CIRCUIT_PURPOSE_PATH_BIAS_TESTING
)
1260 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
1261 } else if (!circ
->timestamp_dirty
&& circ
->state
== CIRCUIT_STATE_OPEN
) {
1262 if (timercmp(&circ
->timestamp_began
, &cutoff
, <)) {
1263 if (circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
||
1264 circ
->purpose
== CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
||
1265 circ
->purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
||
1266 circ
->purpose
== CIRCUIT_PURPOSE_TESTING
||
1267 (circ
->purpose
>= CIRCUIT_PURPOSE_C_INTRODUCING
&&
1268 circ
->purpose
<= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
) ||
1269 circ
->purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
) {
1271 "Closing circuit that has been unused for %ld msec.",
1272 tv_mdiff(&circ
->timestamp_began
, &now
));
1273 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
1274 } else if (!TO_ORIGIN_CIRCUIT(circ
)->is_ancient
) {
1275 /* Server-side rend joined circuits can end up really old, because
1276 * they are reused by clients for longer than normal. The client
1277 * controls their lifespan. (They never become dirty, because
1278 * connection_exit_begin_conn() never marks anything as dirty.)
1279 * Similarly, server-side intro circuits last a long time. */
1280 if (circ
->purpose
!= CIRCUIT_PURPOSE_S_REND_JOINED
&&
1281 circ
->purpose
!= CIRCUIT_PURPOSE_S_INTRO
) {
1283 "Ancient non-dirty circuit %d is still around after "
1284 "%ld milliseconds. Purpose: %d (%s)",
1285 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
,
1286 tv_mdiff(&circ
->timestamp_began
, &now
),
1288 circuit_purpose_to_string(circ
->purpose
));
1289 TO_ORIGIN_CIRCUIT(circ
)->is_ancient
= 1;
1297 /** How long do we wait before killing circuits with the properties
1300 * Probably we could choose a number here as low as 5 to 10 seconds,
1301 * since these circs are used for begindir, and a) generally you either
1302 * ask another begindir question right after or you don't for a long time,
1303 * b) clients at least through 0.2.1.x choose from the whole set of
1304 * directory mirrors at each choice, and c) re-establishing a one-hop
1305 * circuit via create-fast is a light operation assuming the TLS conn is
1308 * I expect "b" to go away one day when we move to using directory
1309 * guards, but I think "a" and "c" are good enough reasons that a low
1310 * number is safe even then.
1312 #define IDLE_ONE_HOP_CIRC_TIMEOUT 60
1314 /** Find each non-origin circuit that has been unused for too long,
1315 * has no streams on it, used a create_fast, and ends here: mark it
1319 circuit_expire_old_circuits_serverside(time_t now
)
1322 or_circuit_t
*or_circ
;
1323 time_t cutoff
= now
- IDLE_ONE_HOP_CIRC_TIMEOUT
;
1325 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
1326 if (circ
->marked_for_close
|| CIRCUIT_IS_ORIGIN(circ
))
1328 or_circ
= TO_OR_CIRCUIT(circ
);
1329 /* If the circuit has been idle for too long, and there are no streams
1330 * on it, and it ends here, and it used a create_fast, mark it for close.
1332 if (or_circ
->is_first_hop
&& !circ
->n_chan
&&
1333 !or_circ
->n_streams
&& !or_circ
->resolving_streams
&&
1335 channel_when_last_xmit(or_circ
->p_chan
) <= cutoff
) {
1336 log_info(LD_CIRC
, "Closing circ_id %u (empty %d secs ago)",
1337 (unsigned)or_circ
->p_circ_id
,
1338 (int)(now
- channel_when_last_xmit(or_circ
->p_chan
)));
1339 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
1344 /** Number of testing circuits we want open before testing our bandwidth. */
1345 #define NUM_PARALLEL_TESTING_CIRCS 4
1347 /** True iff we've ever had enough testing circuits open to test our
1349 static int have_performed_bandwidth_test
= 0;
1351 /** Reset have_performed_bandwidth_test, so we'll start building
1352 * testing circuits again so we can exercise our bandwidth. */
1354 reset_bandwidth_test(void)
1356 have_performed_bandwidth_test
= 0;
1359 /** Return 1 if we've already exercised our bandwidth, or if we
1360 * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
1361 * established or on the way. Else return 0.
1364 circuit_enough_testing_circs(void)
1369 if (have_performed_bandwidth_test
)
1372 TOR_LIST_FOREACH(circ
, circuit_get_global_list(), head
) {
1373 if (!circ
->marked_for_close
&& CIRCUIT_IS_ORIGIN(circ
) &&
1374 circ
->purpose
== CIRCUIT_PURPOSE_TESTING
&&
1375 circ
->state
== CIRCUIT_STATE_OPEN
)
1378 return num
>= NUM_PARALLEL_TESTING_CIRCS
;
1381 /** A testing circuit has completed. Take whatever stats we want.
1382 * Noticing reachability is taken care of in onionskin_answer(),
1383 * so there's no need to record anything here. But if we still want
1384 * to do the bandwidth test, and we now have enough testing circuits
1388 circuit_testing_opened(origin_circuit_t
*circ
)
1390 if (have_performed_bandwidth_test
||
1391 !check_whether_orport_reachable()) {
1392 /* either we've already done everything we want with testing circuits,
1393 * or this testing circuit became open due to a fluke, e.g. we picked
1394 * a last hop where we already had the connection open due to an
1395 * outgoing local circuit. */
1396 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_AT_ORIGIN
);
1397 } else if (circuit_enough_testing_circs()) {
1398 router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS
, time(NULL
));
1399 have_performed_bandwidth_test
= 1;
1401 consider_testing_reachability(1, 0);
1404 /** A testing circuit has failed to build. Take whatever stats we want. */
1406 circuit_testing_failed(origin_circuit_t
*circ
, int at_last_hop
)
1408 if (server_mode(get_options()) && check_whether_orport_reachable())
1411 log_info(LD_GENERAL
,
1412 "Our testing circuit (to see if your ORPort is reachable) "
1413 "has failed. I'll try again later.");
1415 /* These aren't used yet. */
1420 /** The circuit <b>circ</b> has just become open. Take the next
1421 * step: for rendezvous circuits, we pass circ to the appropriate
1422 * function in rendclient or rendservice. For general circuits, we
1423 * call connection_ap_attach_pending, which looks for pending streams
1424 * that could use circ.
1427 circuit_has_opened(origin_circuit_t
*circ
)
1429 control_event_circuit_status(circ
, CIRC_EVENT_BUILT
, 0);
1431 /* Remember that this circuit has finished building. Now if we start
1432 * it building again later (e.g. by extending it), we will know not
1433 * to consider its build time. */
1434 circ
->has_opened
= 1;
1436 switch (TO_CIRCUIT(circ
)->purpose
) {
1437 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
1438 rend_client_rendcirc_has_opened(circ
);
1439 /* Start building an intro circ if we don't have one yet. */
1440 connection_ap_attach_pending();
1441 /* This isn't a call to circuit_try_attaching_streams because a
1442 * circuit in _C_ESTABLISH_REND state isn't connected to its
1443 * hidden service yet, thus we can't attach streams to it yet,
1444 * thus circuit_try_attaching_streams would always clear the
1445 * circuit's isolation state. circuit_try_attaching_streams is
1446 * called later, when the rend circ enters _C_REND_JOINED
1449 case CIRCUIT_PURPOSE_C_INTRODUCING
:
1450 rend_client_introcirc_has_opened(circ
);
1452 case CIRCUIT_PURPOSE_C_GENERAL
:
1453 /* Tell any AP connections that have been waiting for a new
1454 * circuit that one is ready. */
1455 circuit_try_attaching_streams(circ
);
1457 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
1458 /* at Bob, waiting for introductions */
1459 rend_service_intro_has_opened(circ
);
1461 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
1462 /* at Bob, connecting to rend point */
1463 rend_service_rendezvous_has_opened(circ
);
1465 case CIRCUIT_PURPOSE_TESTING
:
1466 circuit_testing_opened(circ
);
1469 * This won't happen in normal operation, but might happen if the
1470 * controller did it. Just let it slide. */
1474 /** If the stream-isolation state of <b>circ</b> can be cleared, clear
1475 * it. Return non-zero iff <b>circ</b>'s isolation state was cleared. */
1477 circuit_try_clearing_isolation_state(origin_circuit_t
*circ
)
1479 if (/* The circuit may have become non-open if it was cannibalized.*/
1480 circ
->base_
.state
== CIRCUIT_STATE_OPEN
&&
1481 /* If !isolation_values_set, there is nothing to clear. */
1482 circ
->isolation_values_set
&&
1483 /* It's not legal to clear a circuit's isolation info if it's ever had
1484 * streams attached */
1485 !circ
->isolation_any_streams_attached
) {
1486 /* If we have any isolation information set on this circuit, and
1487 * we didn't manage to attach any streams to it, then we can
1488 * and should clear it and try again. */
1489 circuit_clear_isolation(circ
);
1496 /** Called when a circuit becomes ready for streams to be attached to
1499 circuit_try_attaching_streams(origin_circuit_t
*circ
)
1501 /* Attach streams to this circuit if we can. */
1502 connection_ap_attach_pending();
1504 /* The call to circuit_try_clearing_isolation_state here will do
1505 * nothing and return 0 if we didn't attach any streams to circ
1507 if (circuit_try_clearing_isolation_state(circ
)) {
1508 /* Maybe *now* we can attach some streams to this circuit. */
1509 connection_ap_attach_pending();
1513 /** Called whenever a circuit could not be successfully built.
1516 circuit_build_failed(origin_circuit_t
*circ
)
1518 channel_t
*n_chan
= NULL
;
1519 /* we should examine circ and see if it failed because of
1520 * the last hop or an earlier hop. then use this info below.
1522 int failed_at_last_hop
= 0;
1523 /* If the last hop isn't open, and the second-to-last is, we failed
1524 * at the last hop. */
1526 circ
->cpath
->prev
->state
!= CPATH_STATE_OPEN
&&
1527 circ
->cpath
->prev
->prev
->state
== CPATH_STATE_OPEN
) {
1528 failed_at_last_hop
= 1;
1531 circ
->cpath
->state
!= CPATH_STATE_OPEN
&&
1532 ! circ
->base_
.received_destroy
) {
1533 /* We failed at the first hop for some reason other than a DESTROY cell.
1534 * If there's an OR connection to blame, blame it. Also, avoid this relay
1535 * for a while, and fail any one-hop directory fetches destined for it. */
1536 const char *n_chan_id
= circ
->cpath
->extend_info
->identity_digest
;
1537 int already_marked
= 0;
1538 if (circ
->base_
.n_chan
) {
1539 n_chan
= circ
->base_
.n_chan
;
1541 if (n_chan
->is_bad_for_new_circs
) {
1542 /* We only want to blame this router when a fresh healthy
1543 * connection fails. So don't mark this router as newly failed,
1544 * since maybe this was just an old circuit attempt that's
1545 * finally timing out now. Also, there's no need to blow away
1546 * circuits/streams/etc, since the failure of an unhealthy conn
1547 * doesn't tell us much about whether a healthy conn would
1552 "Our circuit failed to get a response from the first hop "
1553 "(%s). I'm going to try to rotate to a better connection.",
1554 channel_get_canonical_remote_descr(n_chan
));
1555 n_chan
->is_bad_for_new_circs
= 1;
1558 "Our circuit died before the first hop with no connection");
1560 if (n_chan_id
&& !already_marked
) {
1561 entry_guard_register_connect_status(n_chan_id
, 0, 1, time(NULL
));
1562 /* if there are any one-hop streams waiting on this circuit, fail
1563 * them now so they can retry elsewhere. */
1564 connection_ap_fail_onehop(n_chan_id
, circ
->build_state
);
1568 switch (circ
->base_
.purpose
) {
1569 case CIRCUIT_PURPOSE_C_GENERAL
:
1570 /* If we never built the circuit, note it as a failure. */
1571 circuit_increment_failure_count();
1572 if (failed_at_last_hop
) {
1573 /* Make sure any streams that demand our last hop as their exit
1574 * know that it's unlikely to happen. */
1575 circuit_discard_optional_exit_enclaves(circ
->cpath
->prev
->extend_info
);
1578 case CIRCUIT_PURPOSE_TESTING
:
1579 circuit_testing_failed(circ
, failed_at_last_hop
);
1581 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
1582 /* at Bob, waiting for introductions */
1583 if (circ
->base_
.state
!= CIRCUIT_STATE_OPEN
) {
1584 circuit_increment_failure_count();
1586 /* no need to care here, because bob will rebuild intro
1587 * points periodically. */
1589 case CIRCUIT_PURPOSE_C_INTRODUCING
:
1590 /* at Alice, connecting to intro point */
1591 /* Don't increment failure count, since Bob may have picked
1592 * the introduction point maliciously */
1593 /* Alice will pick a new intro point when this one dies, if
1594 * the stream in question still cares. No need to act here. */
1596 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
1597 /* at Alice, waiting for Bob */
1598 circuit_increment_failure_count();
1599 /* Alice will pick a new rend point when this one dies, if
1600 * the stream in question still cares. No need to act here. */
1602 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
1603 /* at Bob, connecting to rend point */
1604 /* Don't increment failure count, since Alice may have picked
1605 * the rendezvous point maliciously */
1607 "Couldn't connect to Alice's chosen rend point %s "
1609 escaped(build_state_get_exit_nickname(circ
->build_state
)),
1610 failed_at_last_hop
?"last":"non-last");
1611 rend_service_relaunch_rendezvous(circ
);
1614 * This won't happen in normal operation, but might happen if the
1615 * controller did it. Just let it slide. */
1619 /** Number of consecutive failures so far; should only be touched by
1620 * circuit_launch_new and circuit_*_failure_count.
1622 static int n_circuit_failures
= 0;
1623 /** Before the last time we called circuit_reset_failure_count(), were
1624 * there a lot of failures? */
1625 static int did_circs_fail_last_period
= 0;
1627 /** Don't retry launching a new circuit if we try this many times with no
1629 #define MAX_CIRCUIT_FAILURES 5
1631 /** Launch a new circuit; see circuit_launch_by_extend_info() for
1632 * details on arguments. */
1634 circuit_launch(uint8_t purpose
, int flags
)
1636 return circuit_launch_by_extend_info(purpose
, NULL
, flags
);
1639 /** Launch a new circuit with purpose <b>purpose</b> and exit node
1640 * <b>extend_info</b> (or NULL to select a random exit node). If flags
1641 * contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
1642 * CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
1643 * If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
1644 * If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
1645 * Return the newly allocated circuit on success, or NULL on failure. */
1647 circuit_launch_by_extend_info(uint8_t purpose
,
1648 extend_info_t
*extend_info
,
1651 origin_circuit_t
*circ
;
1652 int onehop_tunnel
= (flags
& CIRCLAUNCH_ONEHOP_TUNNEL
) != 0;
1654 if (!onehop_tunnel
&& !router_have_minimum_dir_info()) {
1655 log_debug(LD_CIRC
,"Haven't fetched enough directory info yet; canceling "
1660 if ((extend_info
|| purpose
!= CIRCUIT_PURPOSE_C_GENERAL
) &&
1661 purpose
!= CIRCUIT_PURPOSE_TESTING
&& !onehop_tunnel
) {
1662 /* see if there are appropriate circs available to cannibalize. */
1663 /* XXX if we're planning to add a hop, perhaps we want to look for
1664 * internal circs rather than exit circs? -RD */
1665 circ
= circuit_find_to_cannibalize(purpose
, extend_info
, flags
);
1667 uint8_t old_purpose
= circ
->base_
.purpose
;
1668 struct timeval old_timestamp_began
= circ
->base_
.timestamp_began
;
1670 log_info(LD_CIRC
,"Cannibalizing circ '%s' for purpose %d (%s)",
1671 build_state_get_exit_nickname(circ
->build_state
), purpose
,
1672 circuit_purpose_to_string(purpose
));
1674 if ((purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
||
1675 purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
) &&
1676 circ
->path_state
== PATH_STATE_BUILD_SUCCEEDED
) {
1677 /* Path bias: Cannibalized rends pre-emptively count as a
1678 * successfully built but unused closed circuit. We don't
1679 * wait until the extend (or the close) because the rend
1680 * point could be malicious.
1682 * Same deal goes for client side introductions. Clients
1683 * can be manipulated to connect repeatedly to them
1684 * (especially web clients).
1686 * If we decide to probe the initial portion of these circs,
1687 * (up to the adversary's final hop), we need to remove this,
1688 * or somehow mark the circuit with a special path state.
1691 /* This must be called before the purpose change */
1692 pathbias_check_close(circ
, END_CIRC_REASON_FINISHED
);
1695 circuit_change_purpose(TO_CIRCUIT(circ
), purpose
);
1696 /* Reset the start date of this circ, else expire_building
1697 * will see it and think it's been trying to build since it
1700 * Technically, the code should reset this when the
1701 * create cell is finally sent, but we're close enough
1703 tor_gettimeofday(&circ
->base_
.timestamp_began
);
1705 control_event_circuit_cannibalized(circ
, old_purpose
,
1706 &old_timestamp_began
);
1709 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
1710 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
1711 /* it's ready right now */
1713 case CIRCUIT_PURPOSE_C_INTRODUCING
:
1714 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
1715 case CIRCUIT_PURPOSE_C_GENERAL
:
1716 /* need to add a new hop */
1717 tor_assert(extend_info
);
1718 if (circuit_extend_to_new_exit(circ
, extend_info
) < 0)
1723 "unexpected purpose %d when cannibalizing a circ.",
1725 tor_fragile_assert();
1732 if (did_circs_fail_last_period
&&
1733 n_circuit_failures
> MAX_CIRCUIT_FAILURES
) {
1734 /* too many failed circs in a row. don't try. */
1735 // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
1739 /* try a circ. if it fails, circuit_mark_for_close will increment
1740 * n_circuit_failures */
1741 return circuit_establish_circuit(purpose
, extend_info
, flags
);
1744 /** Record another failure at opening a general circuit. When we have
1745 * too many, we'll stop trying for the remainder of this minute.
1748 circuit_increment_failure_count(void)
1750 ++n_circuit_failures
;
1751 log_debug(LD_CIRC
,"n_circuit_failures now %d.",n_circuit_failures
);
1754 /** Reset the failure count for opening general circuits. This means
1755 * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
1759 circuit_reset_failure_count(int timeout
)
1761 if (timeout
&& n_circuit_failures
> MAX_CIRCUIT_FAILURES
)
1762 did_circs_fail_last_period
= 1;
1764 did_circs_fail_last_period
= 0;
1765 n_circuit_failures
= 0;
1768 /** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
1769 * there isn't one, and there isn't one on the way, launch one and return
1770 * 0. If it will never work, return -1.
1772 * Write the found or in-progress or launched circ into *circp.
1775 circuit_get_open_circ_or_launch(entry_connection_t
*conn
,
1776 uint8_t desired_circuit_purpose
,
1777 origin_circuit_t
**circp
)
1779 origin_circuit_t
*circ
;
1780 int check_exit_policy
;
1781 int need_uptime
, need_internal
;
1783 const or_options_t
*options
= get_options();
1787 tor_assert(ENTRY_TO_CONN(conn
)->state
== AP_CONN_STATE_CIRCUIT_WAIT
);
1789 conn
->socks_request
->command
== SOCKS_COMMAND_CONNECT
&&
1790 !conn
->use_begindir
&&
1791 !connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn
));
1792 want_onehop
= conn
->want_onehop
;
1794 need_uptime
= !conn
->want_onehop
&& !conn
->use_begindir
&&
1795 smartlist_contains_int_as_string(options
->LongLivedPorts
,
1796 conn
->socks_request
->port
);
1798 if (desired_circuit_purpose
!= CIRCUIT_PURPOSE_C_GENERAL
)
1800 else if (conn
->use_begindir
|| conn
->want_onehop
)
1805 circ
= circuit_get_best(conn
, 1, desired_circuit_purpose
,
1806 need_uptime
, need_internal
);
1810 return 1; /* we're happy */
1813 if (!want_onehop
&& !router_have_minimum_dir_info()) {
1814 if (!connection_get_by_type(CONN_TYPE_DIR
)) {
1815 int severity
= LOG_NOTICE
;
1816 /* FFFF if this is a tunneled directory fetch, don't yell
1817 * as loudly. the user doesn't even know it's happening. */
1818 if (entry_list_is_constrained(options
) &&
1819 entries_known_but_down(options
)) {
1820 log_fn(severity
, LD_APP
|LD_DIR
,
1821 "Application request when we haven't used client functionality "
1822 "lately. Optimistically trying known %s again.",
1823 options
->UseBridges
? "bridges" : "entrynodes");
1824 entries_retry_all(options
);
1825 } else if (!options
->UseBridges
|| any_bridge_descriptors_known()) {
1826 log_fn(severity
, LD_APP
|LD_DIR
,
1827 "Application request when we haven't used client functionality "
1828 "lately. Optimistically trying directory fetches again.");
1829 routerlist_retry_directory_downloads(time(NULL
));
1832 /* the stream will be dealt with when router_have_minimum_dir_info becomes
1833 * 1, or when all directory attempts fail and directory_all_unreachable()
1839 /* Do we need to check exit policy? */
1840 if (check_exit_policy
) {
1841 if (!conn
->chosen_exit_name
) {
1843 tor_addr_t addr
, *addrp
=NULL
;
1844 if (tor_inet_aton(conn
->socks_request
->address
, &in
)) {
1845 tor_addr_from_in(&addr
, &in
);
1848 if (router_exit_policy_all_nodes_reject(addrp
,
1849 conn
->socks_request
->port
,
1852 "No Tor server allows exit to %s:%d. Rejecting.",
1853 safe_str_client(conn
->socks_request
->address
),
1854 conn
->socks_request
->port
);
1858 /* XXXX024 Duplicates checks in connection_ap_handshake_attach_circuit:
1859 * refactor into a single function? */
1860 const node_t
*node
= node_get_by_nickname(conn
->chosen_exit_name
, 1);
1861 int opt
= conn
->chosen_exit_optional
;
1862 if (node
&& !connection_ap_can_use_exit(conn
, node
)) {
1863 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1864 "Requested exit point '%s' is excluded or "
1865 "would refuse request. %s.",
1866 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1868 conn
->chosen_exit_optional
= 0;
1869 tor_free(conn
->chosen_exit_name
);
1871 return circuit_get_open_circ_or_launch(conn
,
1872 desired_circuit_purpose
,
1880 /* is one already on the way? */
1881 circ
= circuit_get_best(conn
, 0, desired_circuit_purpose
,
1882 need_uptime
, need_internal
);
1884 log_debug(LD_CIRC
, "one on the way!");
1886 extend_info_t
*extend_info
=NULL
;
1887 uint8_t new_circ_purpose
;
1888 const int n_pending
= count_pending_general_client_circuits();
1890 if (n_pending
>= options
->MaxClientCircuitsPending
) {
1891 static ratelim_t delay_limit
= RATELIM_INIT(10*60);
1893 if ((m
= rate_limit_log(&delay_limit
, approx_time()))) {
1894 log_notice(LD_APP
, "We'd like to launch a circuit to handle a "
1895 "connection, but we already have %d general-purpose client "
1896 "circuits pending. Waiting until some finish.%s",
1903 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1904 /* need to pick an intro point */
1905 rend_data_t
*rend_data
= ENTRY_TO_EDGE_CONN(conn
)->rend_data
;
1906 tor_assert(rend_data
);
1907 extend_info
= rend_client_get_random_intro(rend_data
);
1910 "No intro points for '%s': re-fetching service descriptor.",
1911 safe_str_client(rend_data
->onion_address
));
1912 rend_client_refetch_v2_renddesc(rend_data
);
1913 ENTRY_TO_CONN(conn
)->state
= AP_CONN_STATE_RENDDESC_WAIT
;
1916 log_info(LD_REND
,"Chose %s as intro point for '%s'.",
1917 extend_info_describe(extend_info
),
1918 safe_str_client(rend_data
->onion_address
));
1921 /* If we have specified a particular exit node for our
1922 * connection, then be sure to open a circuit to that exit node.
1924 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
1925 if (conn
->chosen_exit_name
) {
1927 int opt
= conn
->chosen_exit_optional
;
1928 r
= node_get_by_nickname(conn
->chosen_exit_name
, 1);
1929 if (r
&& node_has_descriptor(r
)) {
1930 /* We might want to connect to an IPv6 bridge for loading
1931 descriptors so we use the preferred address rather than
1933 extend_info
= extend_info_from_node(r
, conn
->want_onehop
? 1 : 0);
1935 log_debug(LD_DIR
, "considering %d, %s",
1936 want_onehop
, conn
->chosen_exit_name
);
1937 if (want_onehop
&& conn
->chosen_exit_name
[0] == '$') {
1938 /* We're asking for a one-hop circuit to a router that
1939 * we don't have a routerinfo about. Make up an extend_info. */
1940 char digest
[DIGEST_LEN
];
1941 char *hexdigest
= conn
->chosen_exit_name
+1;
1943 if (strlen(hexdigest
) < HEX_DIGEST_LEN
||
1944 base16_decode(digest
,DIGEST_LEN
,hexdigest
,HEX_DIGEST_LEN
)<0) {
1945 log_info(LD_DIR
, "Broken exit digest on tunnel conn. Closing.");
1948 if (tor_addr_parse(&addr
, conn
->socks_request
->address
) < 0) {
1949 log_info(LD_DIR
, "Broken address %s on tunnel conn. Closing.",
1950 escaped_safe_str_client(conn
->socks_request
->address
));
1953 extend_info
= extend_info_new(conn
->chosen_exit_name
+1,
1954 digest
, NULL
, NULL
, &addr
,
1955 conn
->socks_request
->port
);
1957 /* We will need an onion key for the router, and we
1958 * don't have one. Refuse or relax requirements. */
1959 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1960 "Requested exit point '%s' is not known. %s.",
1961 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1963 conn
->chosen_exit_optional
= 0;
1964 tor_free(conn
->chosen_exit_name
);
1965 /* Try again with no requested exit */
1966 return circuit_get_open_circ_or_launch(conn
,
1967 desired_circuit_purpose
,
1976 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
)
1977 new_circ_purpose
= CIRCUIT_PURPOSE_C_ESTABLISH_REND
;
1978 else if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
1979 new_circ_purpose
= CIRCUIT_PURPOSE_C_INTRODUCING
;
1981 new_circ_purpose
= desired_circuit_purpose
;
1983 if (options
->Tor2webMode
&&
1984 (new_circ_purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
||
1985 new_circ_purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
)) {
1990 int flags
= CIRCLAUNCH_NEED_CAPACITY
;
1991 if (want_onehop
) flags
|= CIRCLAUNCH_ONEHOP_TUNNEL
;
1992 if (need_uptime
) flags
|= CIRCLAUNCH_NEED_UPTIME
;
1993 if (need_internal
) flags
|= CIRCLAUNCH_IS_INTERNAL
;
1994 circ
= circuit_launch_by_extend_info(new_circ_purpose
, extend_info
,
1998 extend_info_free(extend_info
);
2000 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
2001 /* We just caused a circuit to get built because of this stream.
2002 * If this stream has caused a _lot_ of circuits to be built, that's
2003 * a bad sign: we should tell the user. */
2004 if (conn
->num_circuits_launched
< NUM_CIRCUITS_LAUNCHED_THRESHOLD
&&
2005 ++conn
->num_circuits_launched
== NUM_CIRCUITS_LAUNCHED_THRESHOLD
)
2006 log_info(LD_CIRC
, "The application request to %s:%d has launched "
2007 "%d circuits without finding one it likes.",
2008 escaped_safe_str_client(conn
->socks_request
->address
),
2009 conn
->socks_request
->port
,
2010 conn
->num_circuits_launched
);
2012 /* help predict this next time */
2013 rep_hist_note_used_internal(time(NULL
), need_uptime
, 1);
2015 /* write the service_id into circ */
2016 circ
->rend_data
= rend_data_dup(ENTRY_TO_EDGE_CONN(conn
)->rend_data
);
2017 if (circ
->base_
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
&&
2018 circ
->base_
.state
== CIRCUIT_STATE_OPEN
)
2019 rend_client_rendcirc_has_opened(circ
);
2022 } /* endif (!circ) */
2024 /* Mark the circuit with the isolation fields for this connection.
2025 * When the circuit arrives, we'll clear these flags: this is
2026 * just some internal bookkeeping to make sure that we have
2027 * launched enough circuits.
2029 connection_edge_update_circuit_isolation(conn
, circ
, 0);
2032 "No safe circuit (purpose %d) ready for edge "
2033 "connection; delaying.",
2034 desired_circuit_purpose
);
2040 /** Return true iff <b>crypt_path</b> is one of the crypt_paths for
2043 cpath_is_on_circuit(origin_circuit_t
*circ
, crypt_path_t
*crypt_path
)
2045 crypt_path_t
*cpath
, *cpath_next
= NULL
;
2046 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
2047 cpath_next
= cpath
->next
;
2048 if (crypt_path
== cpath
)
2054 /** Return true iff client-side optimistic data is supported. */
2056 optimistic_data_enabled(void)
2058 const or_options_t
*options
= get_options();
2059 if (options
->OptimisticData
< 0) {
2060 /* XXX023 consider having auto default to 1 rather than 0 before
2061 * the 0.2.3 branch goes stable. See bug 3617. -RD */
2062 const int32_t enabled
=
2063 networkstatus_get_param(NULL
, "UseOptimisticData", 0, 0, 1);
2064 return (int)enabled
;
2066 return options
->OptimisticData
;
2069 /** Attach the AP stream <b>apconn</b> to circ's linked list of
2070 * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
2071 * hop in circ's cpath if <b>cpath</b> is NULL.
2074 link_apconn_to_circ(entry_connection_t
*apconn
, origin_circuit_t
*circ
,
2075 crypt_path_t
*cpath
)
2077 const node_t
*exitnode
;
2079 /* add it into the linked list of streams on this circuit */
2080 log_debug(LD_APP
|LD_CIRC
, "attaching new conn to circ. n_circ_id %u.",
2081 (unsigned)circ
->base_
.n_circ_id
);
2082 /* reset it, so we can measure circ timeouts */
2083 ENTRY_TO_CONN(apconn
)->timestamp_lastread
= time(NULL
);
2084 ENTRY_TO_EDGE_CONN(apconn
)->next_stream
= circ
->p_streams
;
2085 ENTRY_TO_EDGE_CONN(apconn
)->on_circuit
= TO_CIRCUIT(circ
);
2086 /* assert_connection_ok(conn, time(NULL)); */
2087 circ
->p_streams
= ENTRY_TO_EDGE_CONN(apconn
);
2089 if (connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(apconn
))) {
2090 /* We are attaching a stream to a rendezvous circuit. That means
2091 * that an attempt to connect to a hidden service just
2092 * succeeded. Tell rendclient.c. */
2093 rend_client_note_connection_attempt_ended(
2094 ENTRY_TO_EDGE_CONN(apconn
)->rend_data
->onion_address
);
2097 if (cpath
) { /* we were given one; use it */
2098 tor_assert(cpath_is_on_circuit(circ
, cpath
));
2100 /* use the last hop in the circuit */
2101 tor_assert(circ
->cpath
);
2102 tor_assert(circ
->cpath
->prev
);
2103 tor_assert(circ
->cpath
->prev
->state
== CPATH_STATE_OPEN
);
2104 cpath
= circ
->cpath
->prev
;
2106 ENTRY_TO_EDGE_CONN(apconn
)->cpath_layer
= cpath
;
2108 circ
->isolation_any_streams_attached
= 1;
2109 connection_edge_update_circuit_isolation(apconn
, circ
, 0);
2111 /* See if we can use optimistic data on this circuit */
2112 if (cpath
->extend_info
&&
2113 (exitnode
= node_get_by_id(cpath
->extend_info
->identity_digest
)) &&
2115 /* Okay; we know what exit node this is. */
2116 if (optimistic_data_enabled() &&
2117 circ
->base_
.purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
2118 exitnode
->rs
->version_supports_optimistic_data
)
2119 apconn
->may_use_optimistic_data
= 1;
2121 apconn
->may_use_optimistic_data
= 0;
2122 log_info(LD_APP
, "Looks like completed circuit to %s %s allow "
2123 "optimistic data for connection to %s",
2124 safe_str_client(node_describe(exitnode
)),
2125 apconn
->may_use_optimistic_data
? "does" : "doesn't",
2126 safe_str_client(apconn
->socks_request
->address
));
2130 /** Return true iff <b>address</b> is matched by one of the entries in
2131 * TrackHostExits. */
2133 hostname_in_track_host_exits(const or_options_t
*options
, const char *address
)
2135 if (!options
->TrackHostExits
)
2137 SMARTLIST_FOREACH_BEGIN(options
->TrackHostExits
, const char *, cp
) {
2138 if (cp
[0] == '.') { /* match end */
2139 if (cp
[1] == '\0' ||
2140 !strcasecmpend(address
, cp
) ||
2141 !strcasecmp(address
, &cp
[1]))
2143 } else if (strcasecmp(cp
, address
) == 0) {
2146 } SMARTLIST_FOREACH_END(cp
);
2150 /** If an exit wasn't explicitly specified for <b>conn</b>, consider saving
2151 * the exit that we *did* choose for use by future connections to
2152 * <b>conn</b>'s destination.
2155 consider_recording_trackhost(const entry_connection_t
*conn
,
2156 const origin_circuit_t
*circ
)
2158 const or_options_t
*options
= get_options();
2159 char *new_address
= NULL
;
2160 char fp
[HEX_DIGEST_LEN
+1];
2162 /* Search the addressmap for this conn's destination. */
2163 /* If he's not in the address map.. */
2164 if (!options
->TrackHostExits
||
2165 addressmap_have_mapping(conn
->socks_request
->address
,
2166 options
->TrackHostExitsExpire
))
2167 return; /* nothing to track, or already mapped */
2169 if (!hostname_in_track_host_exits(options
, conn
->socks_request
->address
) ||
2170 !circ
->build_state
->chosen_exit
)
2173 /* write down the fingerprint of the chosen exit, not the nickname,
2174 * because the chosen exit might not be named. */
2175 base16_encode(fp
, sizeof(fp
),
2176 circ
->build_state
->chosen_exit
->identity_digest
, DIGEST_LEN
);
2178 /* Add this exit/hostname pair to the addressmap. */
2179 tor_asprintf(&new_address
, "%s.%s.exit",
2180 conn
->socks_request
->address
, fp
);
2182 addressmap_register(conn
->socks_request
->address
, new_address
,
2183 time(NULL
) + options
->TrackHostExitsExpire
,
2184 ADDRMAPSRC_TRACKEXIT
, 0, 0);
2187 /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
2188 * begin or resolve cell as appropriate. Return values are as for
2189 * connection_ap_handshake_attach_circuit. The stream will exit from the hop
2190 * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
2191 * <b>cpath</b> is NULL. */
2193 connection_ap_handshake_attach_chosen_circuit(entry_connection_t
*conn
,
2194 origin_circuit_t
*circ
,
2195 crypt_path_t
*cpath
)
2197 connection_t
*base_conn
= ENTRY_TO_CONN(conn
);
2199 tor_assert(base_conn
->state
== AP_CONN_STATE_CIRCUIT_WAIT
||
2200 base_conn
->state
== AP_CONN_STATE_CONTROLLER_WAIT
);
2201 tor_assert(conn
->socks_request
);
2203 tor_assert(circ
->base_
.state
== CIRCUIT_STATE_OPEN
);
2205 base_conn
->state
= AP_CONN_STATE_CIRCUIT_WAIT
;
2207 if (!circ
->base_
.timestamp_dirty
)
2208 circ
->base_
.timestamp_dirty
= time(NULL
);
2210 pathbias_count_use_attempt(circ
);
2212 link_apconn_to_circ(conn
, circ
, cpath
);
2213 tor_assert(conn
->socks_request
);
2214 if (conn
->socks_request
->command
== SOCKS_COMMAND_CONNECT
) {
2215 if (!conn
->use_begindir
)
2216 consider_recording_trackhost(conn
, circ
);
2217 if (connection_ap_handshake_send_begin(conn
) < 0)
2220 if (connection_ap_handshake_send_resolve(conn
) < 0)
2227 /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
2228 * we don't find one: if conn cannot be handled by any known nodes,
2229 * warn and return -1 (conn needs to die, and is maybe already marked);
2230 * else launch new circuit (if necessary) and return 0.
2231 * Otherwise, associate conn with a safe live circuit, do the
2232 * right next step, and return 1.
2234 /* XXXX this function should mark for close whenever it returns -1;
2235 * its callers shouldn't have to worry about that. */
2237 connection_ap_handshake_attach_circuit(entry_connection_t
*conn
)
2239 connection_t
*base_conn
= ENTRY_TO_CONN(conn
);
2245 tor_assert(base_conn
->state
== AP_CONN_STATE_CIRCUIT_WAIT
);
2246 tor_assert(conn
->socks_request
);
2247 want_onehop
= conn
->want_onehop
;
2249 conn_age
= (int)(time(NULL
) - base_conn
->timestamp_created
);
2251 if (conn_age
>= get_options()->SocksTimeout
) {
2252 int severity
= (tor_addr_is_null(&base_conn
->addr
) && !base_conn
->port
) ?
2253 LOG_INFO
: LOG_NOTICE
;
2254 log_fn(severity
, LD_APP
,
2255 "Tried for %d seconds to get a connection to %s:%d. Giving up.",
2256 conn_age
, safe_str_client(conn
->socks_request
->address
),
2257 conn
->socks_request
->port
);
2261 if (!connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn
))) {
2262 /* we're a general conn */
2263 origin_circuit_t
*circ
=NULL
;
2265 if (conn
->chosen_exit_name
) {
2266 const node_t
*node
= node_get_by_nickname(conn
->chosen_exit_name
, 1);
2267 int opt
= conn
->chosen_exit_optional
;
2268 if (!node
&& !want_onehop
) {
2269 /* We ran into this warning when trying to extend a circuit to a
2270 * hidden service directory for which we didn't have a router
2271 * descriptor. See flyspray task 767 for more details. We should
2272 * keep this in mind when deciding to use BEGIN_DIR cells for other
2273 * directory requests as well. -KL*/
2274 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
2275 "Requested exit point '%s' is not known. %s.",
2276 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
2278 conn
->chosen_exit_optional
= 0;
2279 tor_free(conn
->chosen_exit_name
);
2284 if (node
&& !connection_ap_can_use_exit(conn
, node
)) {
2285 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
2286 "Requested exit point '%s' is excluded or "
2287 "would refuse request. %s.",
2288 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
2290 conn
->chosen_exit_optional
= 0;
2291 tor_free(conn
->chosen_exit_name
);
2298 /* find the circuit that we should use, if there is one. */
2299 retval
= circuit_get_open_circ_or_launch(
2300 conn
, CIRCUIT_PURPOSE_C_GENERAL
, &circ
);
2301 if (retval
< 1) // XXX023 if we totally fail, this still returns 0 -RD
2304 log_debug(LD_APP
|LD_CIRC
,
2305 "Attaching apconn to circ %u (stream %d sec old).",
2306 (unsigned)circ
->base_
.n_circ_id
, conn_age
);
2307 /* print the circ's path, so people can figure out which circs are
2309 circuit_log_path(LOG_INFO
,LD_APP
|LD_CIRC
,circ
);
2311 /* We have found a suitable circuit for our conn. Hurray. */
2312 return connection_ap_handshake_attach_chosen_circuit(conn
, circ
, NULL
);
2314 } else { /* we're a rendezvous conn */
2315 origin_circuit_t
*rendcirc
=NULL
, *introcirc
=NULL
;
2317 tor_assert(!ENTRY_TO_EDGE_CONN(conn
)->cpath_layer
);
2319 /* start by finding a rendezvous circuit for us */
2321 retval
= circuit_get_open_circ_or_launch(
2322 conn
, CIRCUIT_PURPOSE_C_REND_JOINED
, &rendcirc
);
2323 if (retval
< 0) return -1; /* failed */
2326 tor_assert(rendcirc
);
2327 /* one is already established, attach */
2329 "rend joined circ %d already here. attaching. "
2330 "(stream %d sec old)",
2331 (unsigned)rendcirc
->base_
.n_circ_id
, conn_age
);
2332 /* Mark rendezvous circuits as 'newly dirty' every time you use
2333 * them, since the process of rebuilding a rendezvous circ is so
2334 * expensive. There is a tradeoff between linkability and
2335 * feasibility, at this point.
2337 rendcirc
->base_
.timestamp_dirty
= time(NULL
);
2339 /* We've also attempted to use them. If they fail, we need to
2340 * probe them for path bias */
2341 pathbias_count_use_attempt(rendcirc
);
2343 link_apconn_to_circ(conn
, rendcirc
, NULL
);
2344 if (connection_ap_handshake_send_begin(conn
) < 0)
2345 return 0; /* already marked, let them fade away */
2349 if (rendcirc
&& (rendcirc
->base_
.purpose
==
2350 CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
)) {
2352 "pending-join circ %u already here, with intro ack. "
2353 "Stalling. (stream %d sec old)",
2354 (unsigned)rendcirc
->base_
.n_circ_id
, conn_age
);
2358 /* it's on its way. find an intro circ. */
2359 retval
= circuit_get_open_circ_or_launch(
2360 conn
, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
, &introcirc
);
2361 if (retval
< 0) return -1; /* failed */
2364 /* one has already sent the intro. keep waiting. */
2365 tor_assert(introcirc
);
2366 log_info(LD_REND
, "Intro circ %u present and awaiting ack (rend %u). "
2367 "Stalling. (stream %d sec old)",
2368 (unsigned)introcirc
->base_
.n_circ_id
,
2369 rendcirc
? (unsigned)rendcirc
->base_
.n_circ_id
: 0,
2374 /* now rendcirc and introcirc are each either undefined or not finished */
2376 if (rendcirc
&& introcirc
&&
2377 rendcirc
->base_
.purpose
== CIRCUIT_PURPOSE_C_REND_READY
) {
2379 "ready rend circ %u already here (no intro-ack yet on "
2380 "intro %u). (stream %d sec old)",
2381 (unsigned)rendcirc
->base_
.n_circ_id
,
2382 (unsigned)introcirc
->base_
.n_circ_id
, conn_age
);
2384 tor_assert(introcirc
->base_
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
2385 if (introcirc
->base_
.state
== CIRCUIT_STATE_OPEN
) {
2386 log_info(LD_REND
,"found open intro circ %u (rend %u); sending "
2387 "introduction. (stream %d sec old)",
2388 (unsigned)introcirc
->base_
.n_circ_id
,
2389 (unsigned)rendcirc
->base_
.n_circ_id
,
2391 switch (rend_client_send_introduction(introcirc
, rendcirc
)) {
2392 case 0: /* success */
2393 rendcirc
->base_
.timestamp_dirty
= time(NULL
);
2394 introcirc
->base_
.timestamp_dirty
= time(NULL
);
2396 pathbias_count_use_attempt(introcirc
);
2397 pathbias_count_use_attempt(rendcirc
);
2399 assert_circuit_ok(TO_CIRCUIT(rendcirc
));
2400 assert_circuit_ok(TO_CIRCUIT(introcirc
));
2402 case -1: /* transient error */
2404 case -2: /* permanent error */
2407 tor_fragile_assert();
2413 log_info(LD_REND
, "Intro (%u) and rend (%u) circs are not both ready. "
2414 "Stalling conn. (%d sec old)",
2415 introcirc
? (unsigned)introcirc
->base_
.n_circ_id
: 0,
2416 rendcirc
? (unsigned)rendcirc
->base_
.n_circ_id
: 0, conn_age
);
2421 /** Change <b>circ</b>'s purpose to <b>new_purpose</b>. */
2423 circuit_change_purpose(circuit_t
*circ
, uint8_t new_purpose
)
2425 uint8_t old_purpose
;
2426 /* Don't allow an OR circ to become an origin circ or vice versa. */
2427 tor_assert(!!(CIRCUIT_IS_ORIGIN(circ
)) ==
2428 !!(CIRCUIT_PURPOSE_IS_ORIGIN(new_purpose
)));
2430 if (circ
->purpose
== new_purpose
) return;
2432 if (CIRCUIT_IS_ORIGIN(circ
)) {
2433 char old_purpose_desc
[80] = "";
2435 strncpy(old_purpose_desc
, circuit_purpose_to_string(circ
->purpose
), 80-1);
2436 old_purpose_desc
[80-1] = '\0';
2439 "changing purpose of origin circ %d "
2440 "from \"%s\" (%d) to \"%s\" (%d)",
2441 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
,
2444 circuit_purpose_to_string(new_purpose
),
2448 old_purpose
= circ
->purpose
;
2449 circ
->purpose
= new_purpose
;
2451 if (CIRCUIT_IS_ORIGIN(circ
)) {
2452 control_event_circuit_purpose_changed(TO_ORIGIN_CIRCUIT(circ
),
2457 /** Mark <b>circ</b> so that no more connections can be attached to it. */
2459 mark_circuit_unusable_for_new_conns(origin_circuit_t
*circ
)
2461 const or_options_t
*options
= get_options();
2464 /* XXXX025 This is a kludge; we're only keeping it around in case there's
2465 * something that doesn't check unusable_for_new_conns, and to avoid
2466 * deeper refactoring of our expiration logic. */
2467 if (! circ
->base_
.timestamp_dirty
)
2468 circ
->base_
.timestamp_dirty
= approx_time();
2469 if (options
->MaxCircuitDirtiness
>= circ
->base_
.timestamp_dirty
)
2470 circ
->base_
.timestamp_dirty
= 1; /* prevent underflow */
2472 circ
->base_
.timestamp_dirty
-= options
->MaxCircuitDirtiness
;
2474 circ
->unusable_for_new_conns
= 1;