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-2010, 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 "circuitbuild.h"
14 #include "circuitlist.h"
15 #include "circuituse.h"
17 #include "connection.h"
18 #include "connection_edge.h"
21 #include "rendclient.h"
22 #include "rendcommon.h"
23 #include "rendservice.h"
26 #include "routerlist.h"
28 /********* START VARIABLES **********/
30 extern circuit_t
*global_circuitlist
; /* from circuitlist.c */
32 /********* END VARIABLES ************/
34 static void circuit_expire_old_circuits_clientside(time_t now
);
35 static void circuit_increment_failure_count(void);
37 long int lround(double x
);
39 /** Return 1 if <b>circ</b> could be returned by circuit_get_best().
43 circuit_is_acceptable(circuit_t
*circ
, edge_connection_t
*conn
,
44 int must_be_open
, uint8_t purpose
,
45 int need_uptime
, int need_internal
,
48 routerinfo_t
*exitrouter
;
49 cpath_build_state_t
*build_state
;
52 tor_assert(conn
->socks_request
);
54 if (!CIRCUIT_IS_ORIGIN(circ
))
55 return 0; /* this circ doesn't start at us */
56 if (must_be_open
&& (circ
->state
!= CIRCUIT_STATE_OPEN
|| !circ
->n_conn
))
57 return 0; /* ignore non-open circs */
58 if (circ
->marked_for_close
)
61 /* if this circ isn't our purpose, skip. */
62 if (purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
&& !must_be_open
) {
63 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_ESTABLISH_REND
&&
64 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_READY
&&
65 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
&&
66 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_JOINED
)
68 } else if (purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
&&
70 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_INTRODUCING
&&
71 circ
->purpose
!= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
74 if (purpose
!= circ
->purpose
)
78 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
)
79 if (circ
->timestamp_dirty
&&
80 circ
->timestamp_dirty
+get_options()->MaxCircuitDirtiness
<= now
)
83 /* decide if this circ is suitable for this conn */
85 /* for rend circs, circ->cpath->prev is not the last router in the
86 * circuit, it's the magical extra bob hop. so just check the nickname
87 * of the one we meant to finish at.
89 build_state
= TO_ORIGIN_CIRCUIT(circ
)->build_state
;
90 exitrouter
= build_state_get_exit_router(build_state
);
92 if (need_uptime
&& !build_state
->need_uptime
)
94 if (need_internal
!= build_state
->is_internal
)
97 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
98 if (!exitrouter
&& !build_state
->onehop_tunnel
) {
99 log_debug(LD_CIRC
,"Not considering circuit with unknown router.");
100 return 0; /* this circuit is screwed and doesn't know it yet,
101 * or is a rendezvous circuit. */
103 if (build_state
->onehop_tunnel
) {
104 if (!conn
->want_onehop
) {
105 log_debug(LD_CIRC
,"Skipping one-hop circuit.");
108 tor_assert(conn
->chosen_exit_name
);
109 if (build_state
->chosen_exit
) {
110 char digest
[DIGEST_LEN
];
111 if (hexdigest_to_digest(conn
->chosen_exit_name
, digest
) < 0)
112 return 0; /* broken digest, we don't want it */
113 if (memcmp(digest
, build_state
->chosen_exit
->identity_digest
,
115 return 0; /* this is a circuit to somewhere else */
116 if (tor_digest_is_zero(digest
)) {
117 /* we don't know the digest; have to compare addr:port */
119 int r
= tor_addr_from_str(&addr
, conn
->socks_request
->address
);
121 !tor_addr_eq(&build_state
->chosen_exit
->addr
, &addr
) ||
122 build_state
->chosen_exit
->port
!= conn
->socks_request
->port
)
127 if (conn
->want_onehop
) {
128 /* don't use three-hop circuits -- that could hurt our anonymity. */
132 if (exitrouter
&& !connection_ap_can_use_exit(conn
, exitrouter
, 0)) {
133 /* can't exit from this router */
136 } else { /* not general */
137 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
138 if ((conn
->rend_data
&& !ocirc
->rend_data
) ||
139 (!conn
->rend_data
&& ocirc
->rend_data
) ||
140 (conn
->rend_data
&& ocirc
->rend_data
&&
141 rend_cmp_service_ids(conn
->rend_data
->onion_address
,
142 ocirc
->rend_data
->onion_address
))) {
143 /* this circ is not for this conn */
150 /** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
151 * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
154 circuit_is_better(circuit_t
*a
, circuit_t
*b
, uint8_t purpose
)
157 case CIRCUIT_PURPOSE_C_GENERAL
:
158 /* if it's used but less dirty it's best;
159 * else if it's more recently created it's best
161 if (b
->timestamp_dirty
) {
162 if (a
->timestamp_dirty
&&
163 a
->timestamp_dirty
> b
->timestamp_dirty
)
166 if (a
->timestamp_dirty
||
167 a
->timestamp_created
> b
->timestamp_created
)
169 if (CIRCUIT_IS_ORIGIN(b
) &&
170 TO_ORIGIN_CIRCUIT(b
)->build_state
->is_internal
)
174 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
175 /* the closer it is to ack_wait the better it is */
176 if (a
->purpose
> b
->purpose
)
179 case CIRCUIT_PURPOSE_C_REND_JOINED
:
180 /* the closer it is to rend_joined the better it is */
181 if (a
->purpose
> b
->purpose
)
188 /** Find the best circ that conn can use, preferably one which is
189 * dirty. Circ must not be too old.
191 * Conn must be defined.
193 * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
195 * circ_purpose specifies what sort of circuit we must have.
196 * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
198 * If it's REND_JOINED and must_be_open==0, then return the closest
199 * rendezvous-purposed circuit that you can find.
201 * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
202 * closest introduce-purposed circuit that you can find.
204 static origin_circuit_t
*
205 circuit_get_best(edge_connection_t
*conn
, int must_be_open
, uint8_t purpose
,
206 int need_uptime
, int need_internal
)
208 circuit_t
*circ
, *best
=NULL
;
209 time_t now
= time(NULL
);
210 int intro_going_on_but_too_old
= 0;
214 tor_assert(purpose
== CIRCUIT_PURPOSE_C_GENERAL
||
215 purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
||
216 purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
);
218 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
219 if (!circuit_is_acceptable(circ
,conn
,must_be_open
,purpose
,
220 need_uptime
,need_internal
,now
))
223 /* XXX022 make this 15 be a function of circuit finishing times we've
224 * seen lately, a la Fallon Chen's GSoC work -RD */
225 #define REND_PARALLEL_INTRO_DELAY 15
226 if (purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
&&
227 !must_be_open
&& circ
->state
!= CIRCUIT_STATE_OPEN
&&
228 circ
->timestamp_created
+ REND_PARALLEL_INTRO_DELAY
< now
) {
229 intro_going_on_but_too_old
= 1;
233 /* now this is an acceptable circ to hand back. but that doesn't
234 * mean it's the *best* circ to hand back. try to decide.
236 if (!best
|| circuit_is_better(circ
,best
,purpose
))
240 if (!best
&& intro_going_on_but_too_old
)
241 log_info(LD_REND
|LD_CIRC
, "There is an intro circuit being created "
242 "right now, but it has already taken quite a while. Starting "
245 return best
? TO_ORIGIN_CIRCUIT(best
) : NULL
;
248 /** Check whether, according to the policies in <b>options</b>, the
249 * circuit <b>circ</b> makes sense. */
250 /* XXXX currently only checks Exclude{Exit}Nodes. It should check more. */
252 circuit_conforms_to_options(const origin_circuit_t
*circ
,
253 const or_options_t
*options
)
255 const crypt_path_t
*cpath
, *cpath_next
= NULL
;
257 for (cpath
= circ
->cpath
; cpath
&& cpath_next
!= circ
->cpath
;
258 cpath
= cpath_next
) {
259 cpath_next
= cpath
->next
;
261 if (routerset_contains_extendinfo(options
->ExcludeNodes
,
265 if (cpath
->next
== circ
->cpath
) {
266 /* This is apparently the exit node. */
268 if (routerset_contains_extendinfo(options
->ExcludeExitNodes
,
276 /** Close all circuits that start at us, aren't open, and were born
277 * at least CircuitBuildTimeout seconds ago.
280 circuit_expire_building(time_t now
)
282 circuit_t
*victim
, *next_circ
= global_circuitlist
;
283 /* circ_times.timeout is BUILD_TIMEOUT_INITIAL_VALUE if we haven't
284 * decided on a customized one yet */
285 time_t general_cutoff
= now
- lround(circ_times
.timeout_ms
/1000);
286 time_t begindir_cutoff
= now
- lround(circ_times
.timeout_ms
/2000);
287 time_t fourhop_cutoff
= now
- lround(4*circ_times
.timeout_ms
/3000);
288 time_t cannibalize_cutoff
= now
- lround(circ_times
.timeout_ms
/2000);
289 time_t close_cutoff
= now
- lround(circ_times
.close_ms
/1000);
290 time_t introcirc_cutoff
= begindir_cutoff
;
291 cpath_build_state_t
*build_state
;
296 next_circ
= next_circ
->next
;
297 if (!CIRCUIT_IS_ORIGIN(victim
) || /* didn't originate here */
298 victim
->marked_for_close
) /* don't mess with marked circs */
301 build_state
= TO_ORIGIN_CIRCUIT(victim
)->build_state
;
302 if (build_state
&& build_state
->onehop_tunnel
)
303 cutoff
= begindir_cutoff
;
304 else if (build_state
&& build_state
->desired_path_len
== 4
305 && !TO_ORIGIN_CIRCUIT(victim
)->has_opened
)
306 cutoff
= fourhop_cutoff
;
307 else if (TO_ORIGIN_CIRCUIT(victim
)->has_opened
)
308 cutoff
= cannibalize_cutoff
;
309 else if (victim
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
)
310 cutoff
= introcirc_cutoff
;
311 else if (victim
->purpose
== CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
)
312 cutoff
= close_cutoff
;
314 cutoff
= general_cutoff
;
316 if (victim
->timestamp_created
> cutoff
)
317 continue; /* it's still young, leave it alone */
320 /* some debug logs, to help track bugs */
321 if (victim
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
&&
322 victim
->timestamp_created
<= introcirc_cutoff
&&
323 victim
->timestamp_created
> general_cutoff
)
324 log_info(LD_REND
|LD_CIRC
, "Timing out introduction circuit which we "
325 "would not have done if it had been a general circuit.");
327 if (victim
->purpose
>= CIRCUIT_PURPOSE_C_INTRODUCING
&&
328 victim
->purpose
<= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
) {
329 if (!victim
->timestamp_dirty
)
330 log_fn(LOG_DEBUG
,"Considering %sopen purpose %d to %s (circid %d)."
332 victim
->state
== CIRCUIT_STATE_OPEN
? "" : "non",
333 victim
->purpose
, victim
->build_state
->chosen_exit_name
,
336 log_fn(LOG_DEBUG
,"Considering %sopen purpose %d to %s (circid %d). "
337 "%d secs since dirty.",
338 victim
->state
== CIRCUIT_STATE_OPEN
? "" : "non",
339 victim
->purpose
, victim
->build_state
->chosen_exit_name
,
341 (int)(now
- victim
->timestamp_dirty
));
345 /* if circ is !open, or if it's open but purpose is a non-finished
346 * intro or rend, then mark it for close */
347 if (victim
->state
== CIRCUIT_STATE_OPEN
) {
348 switch (victim
->purpose
) {
349 default: /* most open circuits can be left alone. */
350 continue; /* yes, continue inside a switch refers to the nearest
351 * enclosing loop. C is smart. */
352 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
353 case CIRCUIT_PURPOSE_C_INTRODUCING
:
354 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
355 break; /* too old, need to die */
356 case CIRCUIT_PURPOSE_C_REND_READY
:
357 /* it's a rend_ready circ -- has it already picked a query? */
358 /* c_rend_ready circs measure age since timestamp_dirty,
359 * because that's set when they switch purposes
361 if (TO_ORIGIN_CIRCUIT(victim
)->rend_data
||
362 victim
->timestamp_dirty
> cutoff
)
365 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
366 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
367 /* rend and intro circs become dirty each time they
368 * make an introduction attempt. so timestamp_dirty
369 * will reflect the time since the last attempt.
371 if (victim
->timestamp_dirty
> cutoff
)
375 } else { /* circuit not open, consider recording failure as timeout */
376 int first_hop_succeeded
= TO_ORIGIN_CIRCUIT(victim
)->cpath
&&
377 TO_ORIGIN_CIRCUIT(victim
)->cpath
->state
== CPATH_STATE_OPEN
;
379 if (TO_ORIGIN_CIRCUIT(victim
)->p_streams
!= NULL
) {
380 log_warn(LD_BUG
, "Circuit %d (purpose %d) has timed out, "
381 "yet has attached streams!",
382 TO_ORIGIN_CIRCUIT(victim
)->global_identifier
,
384 tor_fragile_assert();
388 if (circuit_timeout_want_to_count_circ(TO_ORIGIN_CIRCUIT(victim
))) {
389 /* Circuits are allowed to last longer for measurement.
390 * Switch their purpose and wait. */
391 if (victim
->purpose
!= CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
) {
392 victim
->purpose
= CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
;
393 /* Record this failure to check for too many timeouts
394 * in a row. This function does not record a time value yet
395 * (we do that later); it only counts the fact that we did
397 circuit_build_times_count_timeout(&circ_times
,
398 first_hop_succeeded
);
403 * If the circuit build time is much greater than we would have cut
404 * it off at, we probably had a suspend event along this codepath,
405 * and we should discard the value.
407 if (now
- victim
->timestamp_created
> 2*circ_times
.close_ms
/1000+1) {
409 "Extremely large value for circuit build timeout: %lds. "
410 "Assuming clock jump. Purpose %d",
411 (long)(now
- victim
->timestamp_created
),
413 } else if (circuit_build_times_count_close(&circ_times
,
415 victim
->timestamp_created
)) {
416 circuit_build_times_set_timeout(&circ_times
);
422 log_info(LD_CIRC
,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
423 victim
->n_conn
->_base
.address
, victim
->n_conn
->_base
.port
,
425 victim
->state
, circuit_state_to_string(victim
->state
),
428 log_info(LD_CIRC
,"Abandoning circ %d (state %d:%s, purpose %d)",
429 victim
->n_circ_id
, victim
->state
,
430 circuit_state_to_string(victim
->state
), victim
->purpose
);
432 circuit_log_path(LOG_INFO
,LD_CIRC
,TO_ORIGIN_CIRCUIT(victim
));
433 circuit_mark_for_close(victim
, END_CIRC_REASON_TIMEOUT
);
437 /** Remove any elements in <b>needed_ports</b> that are handled by an
438 * open or in-progress circuit.
441 circuit_remove_handled_ports(smartlist_t
*needed_ports
)
446 for (i
= 0; i
< smartlist_len(needed_ports
); ++i
) {
447 port
= smartlist_get(needed_ports
, i
);
449 if (circuit_stream_is_being_handled(NULL
, *port
,
450 MIN_CIRCUITS_HANDLING_STREAM
)) {
451 // log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
452 smartlist_del(needed_ports
, i
--);
455 log_debug(LD_CIRC
,"Port %d is not handled.", *port
);
460 /** Return 1 if at least <b>min</b> general-purpose non-internal circuits
461 * will have an acceptable exit node for exit stream <b>conn</b> if it
462 * is defined, else for "*:port".
466 circuit_stream_is_being_handled(edge_connection_t
*conn
,
467 uint16_t port
, int min
)
470 routerinfo_t
*exitrouter
;
472 time_t now
= time(NULL
);
473 int need_uptime
= smartlist_string_num_isin(get_options()->LongLivedPorts
,
474 conn
? conn
->socks_request
->port
: port
);
476 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
477 if (CIRCUIT_IS_ORIGIN(circ
) &&
478 !circ
->marked_for_close
&&
479 circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
480 (!circ
->timestamp_dirty
||
481 circ
->timestamp_dirty
+ get_options()->MaxCircuitDirtiness
> now
)) {
482 cpath_build_state_t
*build_state
= TO_ORIGIN_CIRCUIT(circ
)->build_state
;
483 if (build_state
->is_internal
|| build_state
->onehop_tunnel
)
486 exitrouter
= build_state_get_exit_router(build_state
);
487 if (exitrouter
&& (!need_uptime
|| build_state
->need_uptime
)) {
490 ok
= connection_ap_can_use_exit(conn
, exitrouter
, 0);
492 addr_policy_result_t r
= compare_addr_to_addr_policy(
493 0, port
, exitrouter
->exit_policy
);
494 ok
= r
!= ADDR_POLICY_REJECTED
&& r
!= ADDR_POLICY_PROBABLY_REJECTED
;
506 /** Don't keep more than this many unused open circuits around. */
507 #define MAX_UNUSED_OPEN_CIRCUITS 14
509 /** Figure out how many circuits we have open that are clean. Make
510 * sure it's enough for all the upcoming behaviors we predict we'll have.
511 * But put an upper bound on the total number of circuits.
514 circuit_predict_and_launch_new(void)
517 int num
=0, num_internal
=0, num_uptime_internal
=0;
518 int hidserv_needs_uptime
=0, hidserv_needs_capacity
=1;
519 int port_needs_uptime
=0, port_needs_capacity
=1;
520 time_t now
= time(NULL
);
523 /* First, count how many of each type of circuit we have already. */
524 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
525 cpath_build_state_t
*build_state
;
526 if (!CIRCUIT_IS_ORIGIN(circ
))
528 if (circ
->marked_for_close
)
529 continue; /* don't mess with marked circs */
530 if (circ
->timestamp_dirty
)
531 continue; /* only count clean circs */
532 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_GENERAL
)
533 continue; /* only pay attention to general-purpose circs */
534 build_state
= TO_ORIGIN_CIRCUIT(circ
)->build_state
;
535 if (build_state
->onehop_tunnel
)
538 if (build_state
->is_internal
)
540 if (build_state
->need_uptime
&& build_state
->is_internal
)
541 num_uptime_internal
++;
544 /* If that's enough, then stop now. */
545 if (num
>= MAX_UNUSED_OPEN_CIRCUITS
)
546 return; /* we already have many, making more probably will hurt */
548 /* Second, see if we need any more exit circuits. */
549 /* check if we know of a port that's been requested recently
550 * and no circuit is currently available that can handle it. */
551 if (!circuit_all_predicted_ports_handled(now
, &port_needs_uptime
,
552 &port_needs_capacity
)) {
553 if (port_needs_uptime
)
554 flags
|= CIRCLAUNCH_NEED_UPTIME
;
555 if (port_needs_capacity
)
556 flags
|= CIRCLAUNCH_NEED_CAPACITY
;
558 "Have %d clean circs (%d internal), need another exit circ.",
560 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
564 /* Third, see if we need any more hidden service (server) circuits. */
565 if (num_rend_services() && num_uptime_internal
< 3) {
566 flags
= (CIRCLAUNCH_NEED_CAPACITY
| CIRCLAUNCH_NEED_UPTIME
|
567 CIRCLAUNCH_IS_INTERNAL
);
569 "Have %d clean circs (%d internal), need another internal "
570 "circ for my hidden service.",
572 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
576 /* Fourth, see if we need any more hidden service (client) circuits. */
577 if (rep_hist_get_predicted_internal(now
, &hidserv_needs_uptime
,
578 &hidserv_needs_capacity
) &&
579 ((num_uptime_internal
<2 && hidserv_needs_uptime
) ||
581 if (hidserv_needs_uptime
)
582 flags
|= CIRCLAUNCH_NEED_UPTIME
;
583 if (hidserv_needs_capacity
)
584 flags
|= CIRCLAUNCH_NEED_CAPACITY
;
585 flags
|= CIRCLAUNCH_IS_INTERNAL
;
587 "Have %d clean circs (%d uptime-internal, %d internal), need"
588 " another hidden service circ.",
589 num
, num_uptime_internal
, num_internal
);
590 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
594 /* Finally, check to see if we still need more circuits to learn
595 * a good build timeout. But if we're close to our max number we
596 * want, don't do another -- we want to leave a few slots open so
597 * we can still build circuits preemptively as needed. */
598 if (num
< MAX_UNUSED_OPEN_CIRCUITS
-2 &&
599 circuit_build_times_needs_circuits_now(&circ_times
)) {
600 flags
= CIRCLAUNCH_NEED_CAPACITY
;
602 "Have %d clean circs need another buildtime test circ.", num
);
603 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
608 /** Build a new test circuit every 5 minutes */
609 #define TESTING_CIRCUIT_INTERVAL 300
611 /** This function is called once a second, if router_have_min_dir_info() is
612 * true. Its job is to make sure all services we offer have enough circuits
613 * available. Some services just want enough circuits for current tasks,
614 * whereas others want a minimum set of idle circuits hanging around.
617 circuit_build_needed_circs(time_t now
)
619 static time_t time_to_new_circuit
= 0;
620 or_options_t
*options
= get_options();
622 /* launch a new circ for any pending streams that need one */
623 connection_ap_attach_pending();
625 /* make sure any hidden services have enough intro points */
626 rend_services_introduce();
628 if (time_to_new_circuit
< now
) {
629 circuit_reset_failure_count(1);
630 time_to_new_circuit
= now
+ options
->NewCircuitPeriod
;
631 if (proxy_mode(get_options()))
632 addressmap_clean(now
);
633 circuit_expire_old_circuits_clientside(now
);
635 #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
636 circ
= circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL
);
637 if (get_options()->RunTesting
&&
639 circ
->timestamp_created
+ TESTING_CIRCUIT_INTERVAL
< now
) {
640 log_fn(LOG_INFO
,"Creating a new testing circuit.");
641 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, 0);
645 if (!options
->DisablePredictedCircuits
)
646 circuit_predict_and_launch_new();
649 /** If the stream <b>conn</b> is a member of any of the linked
650 * lists of <b>circ</b>, then remove it from the list.
653 circuit_detach_stream(circuit_t
*circ
, edge_connection_t
*conn
)
655 edge_connection_t
*prevconn
;
660 conn
->cpath_layer
= NULL
; /* make sure we don't keep a stale pointer */
661 conn
->on_circuit
= NULL
;
663 if (CIRCUIT_IS_ORIGIN(circ
)) {
664 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
665 if (conn
== origin_circ
->p_streams
) {
666 origin_circ
->p_streams
= conn
->next_stream
;
670 for (prevconn
= origin_circ
->p_streams
;
671 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
672 prevconn
= prevconn
->next_stream
)
674 if (prevconn
&& prevconn
->next_stream
) {
675 prevconn
->next_stream
= conn
->next_stream
;
679 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
680 if (conn
== or_circ
->n_streams
) {
681 or_circ
->n_streams
= conn
->next_stream
;
684 if (conn
== or_circ
->resolving_streams
) {
685 or_circ
->resolving_streams
= conn
->next_stream
;
689 for (prevconn
= or_circ
->n_streams
;
690 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
691 prevconn
= prevconn
->next_stream
)
693 if (prevconn
&& prevconn
->next_stream
) {
694 prevconn
->next_stream
= conn
->next_stream
;
698 for (prevconn
= or_circ
->resolving_streams
;
699 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
700 prevconn
= prevconn
->next_stream
)
702 if (prevconn
&& prevconn
->next_stream
) {
703 prevconn
->next_stream
= conn
->next_stream
;
708 log_warn(LD_BUG
,"Edge connection not in circuit's list.");
709 /* Don't give an error here; it's harmless. */
710 tor_fragile_assert();
713 /** If we haven't yet decided on a good timeout value for circuit
714 * building, we close idles circuits aggressively so we can get more
716 #define IDLE_TIMEOUT_WHILE_LEARNING (10*60)
718 /** Find each circuit that has been unused for too long, or dirty
719 * for too long and has no streams on it: mark it for close.
722 circuit_expire_old_circuits_clientside(time_t now
)
727 if (circuit_build_times_needs_circuits(&circ_times
)) {
728 /* Circuits should be shorter lived if we need more of them
729 * for learning a good build timeout */
730 cutoff
= now
- IDLE_TIMEOUT_WHILE_LEARNING
;
732 cutoff
= now
- get_options()->CircuitIdleTimeout
;
735 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
736 if (circ
->marked_for_close
|| !CIRCUIT_IS_ORIGIN(circ
))
738 /* If the circuit has been dirty for too long, and there are no streams
739 * on it, mark it for close.
741 if (circ
->timestamp_dirty
&&
742 circ
->timestamp_dirty
+ get_options()->MaxCircuitDirtiness
< now
&&
743 !TO_ORIGIN_CIRCUIT(circ
)->p_streams
/* nothing attached */ ) {
744 log_debug(LD_CIRC
, "Closing n_circ_id %d (dirty %d secs ago, "
746 circ
->n_circ_id
, (int)(now
- circ
->timestamp_dirty
),
748 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
749 } else if (!circ
->timestamp_dirty
&& circ
->state
== CIRCUIT_STATE_OPEN
) {
750 if (circ
->timestamp_created
< cutoff
) {
751 if (circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
||
752 circ
->purpose
== CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
||
753 circ
->purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
||
754 circ
->purpose
== CIRCUIT_PURPOSE_TESTING
||
755 (circ
->purpose
>= CIRCUIT_PURPOSE_C_INTRODUCING
&&
756 circ
->purpose
<= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
) ||
757 circ
->purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
) {
759 "Closing circuit that has been unused for %ld seconds.",
760 (long)(now
- circ
->timestamp_created
));
761 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
762 } else if (!TO_ORIGIN_CIRCUIT(circ
)->is_ancient
) {
763 /* Server-side rend joined circuits can end up really old, because
764 * they are reused by clients for longer than normal. The client
765 * controls their lifespan. (They never become dirty, because
766 * connection_exit_begin_conn() never marks anything as dirty.)
767 * Similarly, server-side intro circuits last a long time. */
768 if (circ
->purpose
!= CIRCUIT_PURPOSE_S_REND_JOINED
&&
769 circ
->purpose
!= CIRCUIT_PURPOSE_S_INTRO
) {
771 "Ancient non-dirty circuit %d is still around after "
772 "%ld seconds. Purpose: %d",
773 TO_ORIGIN_CIRCUIT(circ
)->global_identifier
,
774 (long)(now
- circ
->timestamp_created
),
776 /* FFFF implement a new circuit_purpose_to_string() so we don't
777 * just print out a number for circ->purpose */
778 TO_ORIGIN_CIRCUIT(circ
)->is_ancient
= 1;
786 /** How long do we wait before killing circuits with the properties
789 * Probably we could choose a number here as low as 5 to 10 seconds,
790 * since these circs are used for begindir, and a) generally you either
791 * ask another begindir question right after or you don't for a long time,
792 * b) clients at least through 0.2.1.x choose from the whole set of
793 * directory mirrors at each choice, and c) re-establishing a one-hop
794 * circuit via create-fast is a light operation assuming the TLS conn is
797 * I expect "b" to go away one day when we move to using directory
798 * guards, but I think "a" and "c" are good enough reasons that a low
799 * number is safe even then.
801 #define IDLE_ONE_HOP_CIRC_TIMEOUT 60
803 /** Find each non-origin circuit that has been unused for too long,
804 * has no streams on it, used a create_fast, and ends here: mark it
808 circuit_expire_old_circuits_serverside(time_t now
)
811 or_circuit_t
*or_circ
;
812 time_t cutoff
= now
- IDLE_ONE_HOP_CIRC_TIMEOUT
;
814 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
815 if (circ
->marked_for_close
|| CIRCUIT_IS_ORIGIN(circ
))
817 or_circ
= TO_OR_CIRCUIT(circ
);
818 /* If the circuit has been idle for too long, and there are no streams
819 * on it, and it ends here, and it used a create_fast, mark it for close.
821 if (or_circ
->is_first_hop
&& !circ
->n_conn
&&
822 !or_circ
->n_streams
&& !or_circ
->resolving_streams
&&
824 or_circ
->p_conn
->timestamp_last_added_nonpadding
<= cutoff
) {
825 log_info(LD_CIRC
, "Closing circ_id %d (empty %d secs ago)",
827 (int)(now
- or_circ
->p_conn
->timestamp_last_added_nonpadding
));
828 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
833 /** Number of testing circuits we want open before testing our bandwidth. */
834 #define NUM_PARALLEL_TESTING_CIRCS 4
836 /** True iff we've ever had enough testing circuits open to test our
838 static int have_performed_bandwidth_test
= 0;
840 /** Reset have_performed_bandwidth_test, so we'll start building
841 * testing circuits again so we can exercise our bandwidth. */
843 reset_bandwidth_test(void)
845 have_performed_bandwidth_test
= 0;
848 /** Return 1 if we've already exercised our bandwidth, or if we
849 * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
850 * established or on the way. Else return 0.
853 circuit_enough_testing_circs(void)
858 if (have_performed_bandwidth_test
)
861 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
862 if (!circ
->marked_for_close
&& CIRCUIT_IS_ORIGIN(circ
) &&
863 circ
->purpose
== CIRCUIT_PURPOSE_TESTING
&&
864 circ
->state
== CIRCUIT_STATE_OPEN
)
867 return num
>= NUM_PARALLEL_TESTING_CIRCS
;
870 /** A testing circuit has completed. Take whatever stats we want.
871 * Noticing reachability is taken care of in onionskin_answer(),
872 * so there's no need to record anything here. But if we still want
873 * to do the bandwidth test, and we now have enough testing circuits
877 circuit_testing_opened(origin_circuit_t
*circ
)
879 if (have_performed_bandwidth_test
||
880 !check_whether_orport_reachable()) {
881 /* either we've already done everything we want with testing circuits,
882 * or this testing circuit became open due to a fluke, e.g. we picked
883 * a last hop where we already had the connection open due to an
884 * outgoing local circuit. */
885 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_AT_ORIGIN
);
886 } else if (circuit_enough_testing_circs()) {
887 router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS
, time(NULL
));
888 have_performed_bandwidth_test
= 1;
890 consider_testing_reachability(1, 0);
893 /** A testing circuit has failed to build. Take whatever stats we want. */
895 circuit_testing_failed(origin_circuit_t
*circ
, int at_last_hop
)
897 if (server_mode(get_options()) && check_whether_orport_reachable())
901 "Our testing circuit (to see if your ORPort is reachable) "
902 "has failed. I'll try again later.");
904 /* These aren't used yet. */
909 /** The circuit <b>circ</b> has just become open. Take the next
910 * step: for rendezvous circuits, we pass circ to the appropriate
911 * function in rendclient or rendservice. For general circuits, we
912 * call connection_ap_attach_pending, which looks for pending streams
913 * that could use circ.
916 circuit_has_opened(origin_circuit_t
*circ
)
918 control_event_circuit_status(circ
, CIRC_EVENT_BUILT
, 0);
920 /* Remember that this circuit has finished building. Now if we start
921 * it building again later (e.g. by extending it), we will know not
922 * to consider its build time. */
923 circ
->has_opened
= 1;
925 switch (TO_CIRCUIT(circ
)->purpose
) {
926 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
927 rend_client_rendcirc_has_opened(circ
);
928 connection_ap_attach_pending();
930 case CIRCUIT_PURPOSE_C_INTRODUCING
:
931 rend_client_introcirc_has_opened(circ
);
933 case CIRCUIT_PURPOSE_C_GENERAL
:
934 /* Tell any AP connections that have been waiting for a new
935 * circuit that one is ready. */
936 connection_ap_attach_pending();
938 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
939 /* at Bob, waiting for introductions */
940 rend_service_intro_has_opened(circ
);
942 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
943 /* at Bob, connecting to rend point */
944 rend_service_rendezvous_has_opened(circ
);
946 case CIRCUIT_PURPOSE_TESTING
:
947 circuit_testing_opened(circ
);
950 * This won't happen in normal operation, but might happen if the
951 * controller did it. Just let it slide. */
955 /** Called whenever a circuit could not be successfully built.
958 circuit_build_failed(origin_circuit_t
*circ
)
960 /* we should examine circ and see if it failed because of
961 * the last hop or an earlier hop. then use this info below.
963 int failed_at_last_hop
= 0;
964 /* If the last hop isn't open, and the second-to-last is, we failed
965 * at the last hop. */
967 circ
->cpath
->prev
->state
!= CPATH_STATE_OPEN
&&
968 circ
->cpath
->prev
->prev
->state
== CPATH_STATE_OPEN
) {
969 failed_at_last_hop
= 1;
972 circ
->cpath
->state
!= CPATH_STATE_OPEN
) {
973 /* We failed at the first hop. If there's an OR connection
974 * to blame, blame it. Also, avoid this relay for a while, and
975 * fail any one-hop directory fetches destined for it. */
976 const char *n_conn_id
= circ
->cpath
->extend_info
->identity_digest
;
977 int already_marked
= 0;
978 if (circ
->_base
.n_conn
) {
979 or_connection_t
*n_conn
= circ
->_base
.n_conn
;
980 if (n_conn
->is_bad_for_new_circs
) {
981 /* We only want to blame this router when a fresh healthy
982 * connection fails. So don't mark this router as newly failed,
983 * since maybe this was just an old circuit attempt that's
984 * finally timing out now. Also, there's no need to blow away
985 * circuits/streams/etc, since the failure of an unhealthy conn
986 * doesn't tell us much about whether a healthy conn would
991 "Our circuit failed to get a response from the first hop "
992 "(%s:%d). I'm going to try to rotate to a better connection.",
993 n_conn
->_base
.address
, n_conn
->_base
.port
);
994 n_conn
->is_bad_for_new_circs
= 1;
997 "Our circuit died before the first hop with no connection");
999 if (n_conn_id
&& !already_marked
) {
1000 entry_guard_register_connect_status(n_conn_id
, 0, 1, time(NULL
));
1001 /* if there are any one-hop streams waiting on this circuit, fail
1002 * them now so they can retry elsewhere. */
1003 connection_ap_fail_onehop(n_conn_id
, circ
->build_state
);
1007 switch (circ
->_base
.purpose
) {
1008 case CIRCUIT_PURPOSE_C_GENERAL
:
1009 /* If we never built the circuit, note it as a failure. */
1010 circuit_increment_failure_count();
1011 if (failed_at_last_hop
) {
1012 /* Make sure any streams that demand our last hop as their exit
1013 * know that it's unlikely to happen. */
1014 circuit_discard_optional_exit_enclaves(circ
->cpath
->prev
->extend_info
);
1017 case CIRCUIT_PURPOSE_TESTING
:
1018 circuit_testing_failed(circ
, failed_at_last_hop
);
1020 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
1021 /* at Bob, waiting for introductions */
1022 if (circ
->_base
.state
!= CIRCUIT_STATE_OPEN
) {
1023 circuit_increment_failure_count();
1025 /* no need to care here, because bob will rebuild intro
1026 * points periodically. */
1028 case CIRCUIT_PURPOSE_C_INTRODUCING
:
1029 /* at Alice, connecting to intro point */
1030 /* Don't increment failure count, since Bob may have picked
1031 * the introduction point maliciously */
1032 /* Alice will pick a new intro point when this one dies, if
1033 * the stream in question still cares. No need to act here. */
1035 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
1036 /* at Alice, waiting for Bob */
1037 circuit_increment_failure_count();
1038 /* Alice will pick a new rend point when this one dies, if
1039 * the stream in question still cares. No need to act here. */
1041 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
1042 /* at Bob, connecting to rend point */
1043 /* Don't increment failure count, since Alice may have picked
1044 * the rendezvous point maliciously */
1046 "Couldn't connect to Alice's chosen rend point %s "
1048 escaped(build_state_get_exit_nickname(circ
->build_state
)),
1049 failed_at_last_hop
?"last":"non-last");
1050 rend_service_relaunch_rendezvous(circ
);
1053 * This won't happen in normal operation, but might happen if the
1054 * controller did it. Just let it slide. */
1058 /** Number of consecutive failures so far; should only be touched by
1059 * circuit_launch_new and circuit_*_failure_count.
1061 static int n_circuit_failures
= 0;
1062 /** Before the last time we called circuit_reset_failure_count(), were
1063 * there a lot of failures? */
1064 static int did_circs_fail_last_period
= 0;
1066 /** Don't retry launching a new circuit if we try this many times with no
1068 #define MAX_CIRCUIT_FAILURES 5
1070 /** Launch a new circuit; see circuit_launch_by_extend_info() for
1071 * details on arguments. */
1073 circuit_launch_by_router(uint8_t purpose
,
1074 routerinfo_t
*exit
, int flags
)
1076 origin_circuit_t
*circ
;
1077 extend_info_t
*info
= NULL
;
1079 info
= extend_info_from_router(exit
);
1080 circ
= circuit_launch_by_extend_info(purpose
, info
, flags
);
1082 extend_info_free(info
);
1086 /** Launch a new circuit with purpose <b>purpose</b> and exit node
1087 * <b>extend_info</b> (or NULL to select a random exit node). If flags
1088 * contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
1089 * CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
1090 * If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
1091 * If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
1092 * Return the newly allocated circuit on success, or NULL on failure. */
1094 circuit_launch_by_extend_info(uint8_t purpose
,
1095 extend_info_t
*extend_info
,
1098 origin_circuit_t
*circ
;
1099 int onehop_tunnel
= (flags
& CIRCLAUNCH_ONEHOP_TUNNEL
) != 0;
1101 if (!onehop_tunnel
&& !router_have_minimum_dir_info()) {
1102 log_debug(LD_CIRC
,"Haven't fetched enough directory info yet; canceling "
1107 if ((extend_info
|| purpose
!= CIRCUIT_PURPOSE_C_GENERAL
) &&
1108 purpose
!= CIRCUIT_PURPOSE_TESTING
&& !onehop_tunnel
) {
1109 /* see if there are appropriate circs available to cannibalize. */
1110 /* XXX if we're planning to add a hop, perhaps we want to look for
1111 * internal circs rather than exit circs? -RD */
1112 circ
= circuit_find_to_cannibalize(purpose
, extend_info
, flags
);
1114 log_info(LD_CIRC
,"Cannibalizing circ '%s' for purpose %d",
1115 build_state_get_exit_nickname(circ
->build_state
), purpose
);
1116 circ
->_base
.purpose
= purpose
;
1117 /* reset the birth date of this circ, else expire_building
1118 * will see it and think it's been trying to build since it
1120 circ
->_base
.timestamp_created
= time(NULL
);
1122 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
1123 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
1124 /* it's ready right now */
1126 case CIRCUIT_PURPOSE_C_INTRODUCING
:
1127 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
1128 case CIRCUIT_PURPOSE_C_GENERAL
:
1129 /* need to add a new hop */
1130 tor_assert(extend_info
);
1131 if (circuit_extend_to_new_exit(circ
, extend_info
) < 0)
1136 "unexpected purpose %d when cannibalizing a circ.",
1138 tor_fragile_assert();
1145 if (did_circs_fail_last_period
&&
1146 n_circuit_failures
> MAX_CIRCUIT_FAILURES
) {
1147 /* too many failed circs in a row. don't try. */
1148 // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
1152 /* try a circ. if it fails, circuit_mark_for_close will increment
1153 * n_circuit_failures */
1154 return circuit_establish_circuit(purpose
, extend_info
, flags
);
1157 /** Record another failure at opening a general circuit. When we have
1158 * too many, we'll stop trying for the remainder of this minute.
1161 circuit_increment_failure_count(void)
1163 ++n_circuit_failures
;
1164 log_debug(LD_CIRC
,"n_circuit_failures now %d.",n_circuit_failures
);
1167 /** Reset the failure count for opening general circuits. This means
1168 * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
1172 circuit_reset_failure_count(int timeout
)
1174 if (timeout
&& n_circuit_failures
> MAX_CIRCUIT_FAILURES
)
1175 did_circs_fail_last_period
= 1;
1177 did_circs_fail_last_period
= 0;
1178 n_circuit_failures
= 0;
1181 /** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
1182 * there isn't one, and there isn't one on the way, launch one and return
1183 * 0. If it will never work, return -1.
1185 * Write the found or in-progress or launched circ into *circp.
1188 circuit_get_open_circ_or_launch(edge_connection_t
*conn
,
1189 uint8_t desired_circuit_purpose
,
1190 origin_circuit_t
**circp
)
1192 origin_circuit_t
*circ
;
1193 int check_exit_policy
;
1194 int need_uptime
, need_internal
;
1196 or_options_t
*options
= get_options();
1200 tor_assert(conn
->_base
.state
== AP_CONN_STATE_CIRCUIT_WAIT
);
1202 conn
->socks_request
->command
== SOCKS_COMMAND_CONNECT
&&
1203 !conn
->use_begindir
&&
1204 !connection_edge_is_rendezvous_stream(conn
);
1205 want_onehop
= conn
->want_onehop
;
1207 need_uptime
= !conn
->want_onehop
&& !conn
->use_begindir
&&
1208 smartlist_string_num_isin(options
->LongLivedPorts
,
1209 conn
->socks_request
->port
);
1210 need_internal
= desired_circuit_purpose
!= CIRCUIT_PURPOSE_C_GENERAL
;
1212 circ
= circuit_get_best(conn
, 1, desired_circuit_purpose
,
1213 need_uptime
, need_internal
);
1217 return 1; /* we're happy */
1220 if (!want_onehop
&& !router_have_minimum_dir_info()) {
1221 if (!connection_get_by_type(CONN_TYPE_DIR
)) {
1222 int severity
= LOG_NOTICE
;
1223 /* FFFF if this is a tunneled directory fetch, don't yell
1224 * as loudly. the user doesn't even know it's happening. */
1225 if (entry_list_is_constrained(options
) &&
1226 entries_known_but_down(options
)) {
1227 log_fn(severity
, LD_APP
|LD_DIR
,
1228 "Application request when we haven't used client functionality "
1229 "lately. Optimistically trying known %s again.",
1230 options
->UseBridges
? "bridges" : "entrynodes");
1231 entries_retry_all(options
);
1232 } else if (!options
->UseBridges
|| any_bridge_descriptors_known()) {
1233 log_fn(severity
, LD_APP
|LD_DIR
,
1234 "Application request when we haven't used client functionality "
1235 "lately. Optimistically trying directory fetches again.");
1236 routerlist_retry_directory_downloads(time(NULL
));
1239 /* the stream will be dealt with when router_have_minimum_dir_info becomes
1240 * 1, or when all directory attempts fail and directory_all_unreachable()
1246 /* Do we need to check exit policy? */
1247 if (check_exit_policy
) {
1248 if (!conn
->chosen_exit_name
) {
1251 if (tor_inet_aton(conn
->socks_request
->address
, &in
))
1252 addr
= ntohl(in
.s_addr
);
1253 if (router_exit_policy_all_routers_reject(addr
,
1254 conn
->socks_request
->port
,
1257 "No Tor server allows exit to %s:%d. Rejecting.",
1258 safe_str_client(conn
->socks_request
->address
),
1259 conn
->socks_request
->port
);
1263 /* XXXX022 Duplicates checks in connection_ap_handshake_attach_circuit */
1264 routerinfo_t
*router
= router_get_by_nickname(conn
->chosen_exit_name
, 1);
1265 int opt
= conn
->chosen_exit_optional
;
1266 if (router
&& !connection_ap_can_use_exit(conn
, router
, 0)) {
1267 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1268 "Requested exit point '%s' would refuse request. %s.",
1269 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1271 conn
->chosen_exit_optional
= 0;
1272 tor_free(conn
->chosen_exit_name
);
1274 return circuit_get_open_circ_or_launch(conn
,
1275 desired_circuit_purpose
,
1283 /* is one already on the way? */
1284 circ
= circuit_get_best(conn
, 0, desired_circuit_purpose
,
1285 need_uptime
, need_internal
);
1287 log_debug(LD_CIRC
, "one on the way!");
1289 extend_info_t
*extend_info
=NULL
;
1290 uint8_t new_circ_purpose
;
1292 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1293 /* need to pick an intro point */
1294 tor_assert(conn
->rend_data
);
1295 extend_info
= rend_client_get_random_intro(conn
->rend_data
);
1298 "No intro points for '%s': re-fetching service descriptor.",
1299 safe_str_client(conn
->rend_data
->onion_address
));
1300 rend_client_refetch_v2_renddesc(conn
->rend_data
);
1301 conn
->_base
.state
= AP_CONN_STATE_RENDDESC_WAIT
;
1304 log_info(LD_REND
,"Chose '%s' as intro point for '%s'.",
1305 extend_info
->nickname
,
1306 safe_str_client(conn
->rend_data
->onion_address
));
1309 /* If we have specified a particular exit node for our
1310 * connection, then be sure to open a circuit to that exit node.
1312 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
1313 if (conn
->chosen_exit_name
) {
1315 int opt
= conn
->chosen_exit_optional
;
1316 r
= router_get_by_nickname(conn
->chosen_exit_name
, 1);
1318 extend_info
= extend_info_from_router(r
);
1320 log_debug(LD_DIR
, "considering %d, %s",
1321 want_onehop
, conn
->chosen_exit_name
);
1322 if (want_onehop
&& conn
->chosen_exit_name
[0] == '$') {
1323 /* We're asking for a one-hop circuit to a router that
1324 * we don't have a routerinfo about. Make up an extend_info. */
1325 char digest
[DIGEST_LEN
];
1326 char *hexdigest
= conn
->chosen_exit_name
+1;
1328 if (strlen(hexdigest
) < HEX_DIGEST_LEN
||
1329 base16_decode(digest
,DIGEST_LEN
,hexdigest
,HEX_DIGEST_LEN
)<0) {
1330 log_info(LD_DIR
, "Broken exit digest on tunnel conn. Closing.");
1333 if (tor_addr_from_str(&addr
, conn
->socks_request
->address
) < 0) {
1334 log_info(LD_DIR
, "Broken address %s on tunnel conn. Closing.",
1335 escaped_safe_str_client(conn
->socks_request
->address
));
1338 extend_info
= extend_info_alloc(conn
->chosen_exit_name
+1,
1339 digest
, NULL
, &addr
,
1340 conn
->socks_request
->port
);
1342 /* We will need an onion key for the router, and we
1343 * don't have one. Refuse or relax requirements. */
1344 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1345 "Requested exit point '%s' is not known. %s.",
1346 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1348 conn
->chosen_exit_optional
= 0;
1349 tor_free(conn
->chosen_exit_name
);
1350 /* Try again with no requested exit */
1351 return circuit_get_open_circ_or_launch(conn
,
1352 desired_circuit_purpose
,
1361 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
)
1362 new_circ_purpose
= CIRCUIT_PURPOSE_C_ESTABLISH_REND
;
1363 else if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
1364 new_circ_purpose
= CIRCUIT_PURPOSE_C_INTRODUCING
;
1366 new_circ_purpose
= desired_circuit_purpose
;
1369 int flags
= CIRCLAUNCH_NEED_CAPACITY
;
1370 if (want_onehop
) flags
|= CIRCLAUNCH_ONEHOP_TUNNEL
;
1371 if (need_uptime
) flags
|= CIRCLAUNCH_NEED_UPTIME
;
1372 if (need_internal
) flags
|= CIRCLAUNCH_IS_INTERNAL
;
1373 circ
= circuit_launch_by_extend_info(new_circ_purpose
, extend_info
,
1377 extend_info_free(extend_info
);
1379 if (desired_circuit_purpose
!= CIRCUIT_PURPOSE_C_GENERAL
) {
1380 /* help predict this next time */
1381 rep_hist_note_used_internal(time(NULL
), need_uptime
, 1);
1383 /* write the service_id into circ */
1384 circ
->rend_data
= rend_data_dup(conn
->rend_data
);
1385 if (circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
&&
1386 circ
->_base
.state
== CIRCUIT_STATE_OPEN
)
1387 rend_client_rendcirc_has_opened(circ
);
1393 "No safe circuit (purpose %d) ready for edge "
1394 "connection; delaying.",
1395 desired_circuit_purpose
);
1400 /** Return true iff <b>crypt_path</b> is one of the crypt_paths for
1403 cpath_is_on_circuit(origin_circuit_t
*circ
, crypt_path_t
*crypt_path
)
1405 crypt_path_t
*cpath
, *cpath_next
= NULL
;
1406 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
1407 cpath_next
= cpath
->next
;
1408 if (crypt_path
== cpath
)
1414 /** Attach the AP stream <b>apconn</b> to circ's linked list of
1415 * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
1416 * hop in circ's cpath if <b>cpath</b> is NULL.
1419 link_apconn_to_circ(edge_connection_t
*apconn
, origin_circuit_t
*circ
,
1420 crypt_path_t
*cpath
)
1422 /* add it into the linked list of streams on this circuit */
1423 log_debug(LD_APP
|LD_CIRC
, "attaching new conn to circ. n_circ_id %d.",
1424 circ
->_base
.n_circ_id
);
1425 /* reset it, so we can measure circ timeouts */
1426 apconn
->_base
.timestamp_lastread
= time(NULL
);
1427 apconn
->next_stream
= circ
->p_streams
;
1428 apconn
->on_circuit
= TO_CIRCUIT(circ
);
1429 /* assert_connection_ok(conn, time(NULL)); */
1430 circ
->p_streams
= apconn
;
1432 if (cpath
) { /* we were given one; use it */
1433 tor_assert(cpath_is_on_circuit(circ
, cpath
));
1434 apconn
->cpath_layer
= cpath
;
1435 } else { /* use the last hop in the circuit */
1436 tor_assert(circ
->cpath
);
1437 tor_assert(circ
->cpath
->prev
);
1438 tor_assert(circ
->cpath
->prev
->state
== CPATH_STATE_OPEN
);
1439 apconn
->cpath_layer
= circ
->cpath
->prev
;
1443 /** If an exit wasn't specifically chosen, save the history for future
1446 consider_recording_trackhost(edge_connection_t
*conn
, origin_circuit_t
*circ
)
1448 int found_needle
= 0;
1449 or_options_t
*options
= get_options();
1452 char fp
[HEX_DIGEST_LEN
+1];
1454 /* Search the addressmap for this conn's destination. */
1455 /* If he's not in the address map.. */
1456 if (!options
->TrackHostExits
||
1457 addressmap_have_mapping(conn
->socks_request
->address
,
1458 options
->TrackHostExitsExpire
))
1459 return; /* nothing to track, or already mapped */
1461 SMARTLIST_FOREACH(options
->TrackHostExits
, const char *, cp
, {
1462 if (cp
[0] == '.') { /* match end */
1463 if (cp
[1] == '\0' ||
1464 !strcasecmpend(conn
->socks_request
->address
, cp
) ||
1465 !strcasecmp(conn
->socks_request
->address
, &cp
[1]))
1467 } else if (strcasecmp(cp
, conn
->socks_request
->address
) == 0) {
1472 if (!found_needle
|| !circ
->build_state
->chosen_exit
)
1475 /* write down the fingerprint of the chosen exit, not the nickname,
1476 * because the chosen exit might not be named. */
1477 base16_encode(fp
, sizeof(fp
),
1478 circ
->build_state
->chosen_exit
->identity_digest
, DIGEST_LEN
);
1480 /* Add this exit/hostname pair to the addressmap. */
1481 len
= strlen(conn
->socks_request
->address
) + 1 /* '.' */ +
1482 strlen(fp
) + 1 /* '.' */ +
1483 strlen("exit") + 1 /* '\0' */;
1484 new_address
= tor_malloc(len
);
1486 tor_snprintf(new_address
, len
, "%s.%s.exit",
1487 conn
->socks_request
->address
, fp
);
1489 addressmap_register(conn
->socks_request
->address
, new_address
,
1490 time(NULL
) + options
->TrackHostExitsExpire
,
1491 ADDRMAPSRC_TRACKEXIT
);
1494 /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
1495 * begin or resolve cell as appropriate. Return values are as for
1496 * connection_ap_handshake_attach_circuit. The stream will exit from the hop
1497 * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
1498 * <b>cpath</b> is NULL. */
1500 connection_ap_handshake_attach_chosen_circuit(edge_connection_t
*conn
,
1501 origin_circuit_t
*circ
,
1502 crypt_path_t
*cpath
)
1505 tor_assert(conn
->_base
.state
== AP_CONN_STATE_CIRCUIT_WAIT
||
1506 conn
->_base
.state
== AP_CONN_STATE_CONTROLLER_WAIT
);
1507 tor_assert(conn
->socks_request
);
1509 tor_assert(circ
->_base
.state
== CIRCUIT_STATE_OPEN
);
1511 conn
->_base
.state
= AP_CONN_STATE_CIRCUIT_WAIT
;
1513 if (!circ
->_base
.timestamp_dirty
)
1514 circ
->_base
.timestamp_dirty
= time(NULL
);
1516 link_apconn_to_circ(conn
, circ
, cpath
);
1517 tor_assert(conn
->socks_request
);
1518 if (conn
->socks_request
->command
== SOCKS_COMMAND_CONNECT
) {
1519 if (!conn
->use_begindir
)
1520 consider_recording_trackhost(conn
, circ
);
1521 if (connection_ap_handshake_send_begin(conn
) < 0)
1524 if (connection_ap_handshake_send_resolve(conn
) < 0)
1531 /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
1532 * we don't find one: if conn cannot be handled by any known nodes,
1533 * warn and return -1 (conn needs to die, and is maybe already marked);
1534 * else launch new circuit (if necessary) and return 0.
1535 * Otherwise, associate conn with a safe live circuit, do the
1536 * right next step, and return 1.
1538 /* XXXX this function should mark for close whenever it returns -1;
1539 * its callers shouldn't have to worry about that. */
1541 connection_ap_handshake_attach_circuit(edge_connection_t
*conn
)
1548 tor_assert(conn
->_base
.state
== AP_CONN_STATE_CIRCUIT_WAIT
);
1549 tor_assert(conn
->socks_request
);
1550 want_onehop
= conn
->want_onehop
;
1552 conn_age
= (int)(time(NULL
) - conn
->_base
.timestamp_created
);
1554 if (conn_age
>= get_options()->SocksTimeout
) {
1555 int severity
= (tor_addr_is_null(&conn
->_base
.addr
) && !conn
->_base
.port
) ?
1556 LOG_INFO
: LOG_NOTICE
;
1557 log_fn(severity
, LD_APP
,
1558 "Tried for %d seconds to get a connection to %s:%d. Giving up.",
1559 conn_age
, safe_str_client(conn
->socks_request
->address
),
1560 conn
->socks_request
->port
);
1564 if (!connection_edge_is_rendezvous_stream(conn
)) { /* we're a general conn */
1565 origin_circuit_t
*circ
=NULL
;
1567 if (conn
->chosen_exit_name
) {
1568 routerinfo_t
*router
= router_get_by_nickname(conn
->chosen_exit_name
, 1);
1569 int opt
= conn
->chosen_exit_optional
;
1570 if (!router
&& !want_onehop
) {
1571 /* We ran into this warning when trying to extend a circuit to a
1572 * hidden service directory for which we didn't have a router
1573 * descriptor. See flyspray task 767 for more details. We should
1574 * keep this in mind when deciding to use BEGIN_DIR cells for other
1575 * directory requests as well. -KL*/
1576 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1577 "Requested exit point '%s' is not known. %s.",
1578 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1580 conn
->chosen_exit_optional
= 0;
1581 tor_free(conn
->chosen_exit_name
);
1586 if (router
&& !connection_ap_can_use_exit(conn
, router
, 0)) {
1587 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1588 "Requested exit point '%s' would refuse request. %s.",
1589 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1591 conn
->chosen_exit_optional
= 0;
1592 tor_free(conn
->chosen_exit_name
);
1599 /* find the circuit that we should use, if there is one. */
1600 retval
= circuit_get_open_circ_or_launch(
1601 conn
, CIRCUIT_PURPOSE_C_GENERAL
, &circ
);
1602 if (retval
< 1) // XXX021 if we totally fail, this still returns 0 -RD
1605 log_debug(LD_APP
|LD_CIRC
,
1606 "Attaching apconn to circ %d (stream %d sec old).",
1607 circ
->_base
.n_circ_id
, conn_age
);
1608 /* print the circ's path, so people can figure out which circs are
1610 circuit_log_path(LOG_INFO
,LD_APP
|LD_CIRC
,circ
);
1612 /* We have found a suitable circuit for our conn. Hurray. */
1613 return connection_ap_handshake_attach_chosen_circuit(conn
, circ
, NULL
);
1615 } else { /* we're a rendezvous conn */
1616 origin_circuit_t
*rendcirc
=NULL
, *introcirc
=NULL
;
1618 tor_assert(!conn
->cpath_layer
);
1620 /* start by finding a rendezvous circuit for us */
1622 retval
= circuit_get_open_circ_or_launch(
1623 conn
, CIRCUIT_PURPOSE_C_REND_JOINED
, &rendcirc
);
1624 if (retval
< 0) return -1; /* failed */
1627 tor_assert(rendcirc
);
1628 /* one is already established, attach */
1630 "rend joined circ %d already here. attaching. "
1631 "(stream %d sec old)",
1632 rendcirc
->_base
.n_circ_id
, conn_age
);
1633 /* Mark rendezvous circuits as 'newly dirty' every time you use
1634 * them, since the process of rebuilding a rendezvous circ is so
1635 * expensive. There is a tradeoff between linkability and
1636 * feasibility, at this point.
1638 rendcirc
->_base
.timestamp_dirty
= time(NULL
);
1639 link_apconn_to_circ(conn
, rendcirc
, NULL
);
1640 if (connection_ap_handshake_send_begin(conn
) < 0)
1641 return 0; /* already marked, let them fade away */
1645 if (rendcirc
&& (rendcirc
->_base
.purpose
==
1646 CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
)) {
1648 "pending-join circ %d already here, with intro ack. "
1649 "Stalling. (stream %d sec old)",
1650 rendcirc
->_base
.n_circ_id
, conn_age
);
1654 /* it's on its way. find an intro circ. */
1655 retval
= circuit_get_open_circ_or_launch(
1656 conn
, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
, &introcirc
);
1657 if (retval
< 0) return -1; /* failed */
1660 /* one has already sent the intro. keep waiting. */
1661 circuit_t
*c
= NULL
;
1662 tor_assert(introcirc
);
1663 log_info(LD_REND
, "Intro circ %d present and awaiting ack (rend %d). "
1664 "Stalling. (stream %d sec old)",
1665 introcirc
->_base
.n_circ_id
,
1666 rendcirc
? rendcirc
->_base
.n_circ_id
: 0,
1668 /* abort parallel intro circs, if any */
1669 for (c
= global_circuitlist
; c
; c
= c
->next
) {
1670 if (c
->purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
&&
1671 !c
->marked_for_close
&& CIRCUIT_IS_ORIGIN(c
)) {
1672 origin_circuit_t
*oc
= TO_ORIGIN_CIRCUIT(c
);
1673 if (oc
->rend_data
&&
1674 !rend_cmp_service_ids(conn
->rend_data
->onion_address
,
1675 oc
->rend_data
->onion_address
)) {
1676 log_info(LD_REND
|LD_CIRC
, "Closing introduction circuit that we "
1677 "built in parallel.");
1678 circuit_mark_for_close(c
, END_CIRC_REASON_TIMEOUT
);
1685 /* now rendcirc and introcirc are each either undefined or not finished */
1687 if (rendcirc
&& introcirc
&&
1688 rendcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_REND_READY
) {
1690 "ready rend circ %d already here (no intro-ack yet on "
1691 "intro %d). (stream %d sec old)",
1692 rendcirc
->_base
.n_circ_id
,
1693 introcirc
->_base
.n_circ_id
, conn_age
);
1695 tor_assert(introcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
1696 if (introcirc
->_base
.state
== CIRCUIT_STATE_OPEN
) {
1697 log_info(LD_REND
,"found open intro circ %d (rend %d); sending "
1698 "introduction. (stream %d sec old)",
1699 introcirc
->_base
.n_circ_id
, rendcirc
->_base
.n_circ_id
,
1701 if (rend_client_send_introduction(introcirc
, rendcirc
) < 0) {
1704 rendcirc
->_base
.timestamp_dirty
= time(NULL
);
1705 introcirc
->_base
.timestamp_dirty
= time(NULL
);
1706 assert_circuit_ok(TO_CIRCUIT(rendcirc
));
1707 assert_circuit_ok(TO_CIRCUIT(introcirc
));
1712 log_info(LD_REND
, "Intro (%d) and rend (%d) circs are not both ready. "
1713 "Stalling conn. (%d sec old)",
1714 introcirc
? introcirc
->_base
.n_circ_id
: 0,
1715 rendcirc
? rendcirc
->_base
.n_circ_id
: 0, conn_age
);