add another heuristic for changes stanzas
[tor.git] / src / or / circuituse.c
blobfbe2e459a5f2b8eba11d2f2b734ee6b0287d9c29
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-2011, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file circuituse.c
9 * \brief Launch the right sort of circuits and attach streams to them.
10 **/
12 #include "or.h"
13 #include "circuitbuild.h"
14 #include "circuitlist.h"
15 #include "circuituse.h"
16 #include "config.h"
17 #include "connection.h"
18 #include "connection_edge.h"
19 #include "control.h"
20 #include "policies.h"
21 #include "rendclient.h"
22 #include "rendcommon.h"
23 #include "rendservice.h"
24 #include "rephist.h"
25 #include "router.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(void);
35 static void circuit_increment_failure_count(void);
37 /** Return 1 if <b>circ</b> could be returned by circuit_get_best().
38 * Else return 0.
40 static int
41 circuit_is_acceptable(circuit_t *circ, edge_connection_t *conn,
42 int must_be_open, uint8_t purpose,
43 int need_uptime, int need_internal,
44 time_t now)
46 routerinfo_t *exitrouter;
47 cpath_build_state_t *build_state;
48 tor_assert(circ);
49 tor_assert(conn);
50 tor_assert(conn->socks_request);
52 if (!CIRCUIT_IS_ORIGIN(circ))
53 return 0; /* this circ doesn't start at us */
54 if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
55 return 0; /* ignore non-open circs */
56 if (circ->marked_for_close)
57 return 0;
59 /* if this circ isn't our purpose, skip. */
60 if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
61 if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
62 circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
63 circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
64 circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
65 return 0;
66 } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
67 !must_be_open) {
68 if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
69 circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
70 return 0;
71 } else {
72 if (purpose != circ->purpose)
73 return 0;
76 if (purpose == CIRCUIT_PURPOSE_C_GENERAL)
77 if (circ->timestamp_dirty &&
78 circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
79 return 0;
81 /* decide if this circ is suitable for this conn */
83 /* for rend circs, circ->cpath->prev is not the last router in the
84 * circuit, it's the magical extra bob hop. so just check the nickname
85 * of the one we meant to finish at.
87 build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
88 exitrouter = build_state_get_exit_router(build_state);
90 if (need_uptime && !build_state->need_uptime)
91 return 0;
92 if (need_internal != build_state->is_internal)
93 return 0;
95 if (purpose == CIRCUIT_PURPOSE_C_GENERAL) {
96 if (!exitrouter && !build_state->onehop_tunnel) {
97 log_debug(LD_CIRC,"Not considering circuit with unknown router.");
98 return 0; /* this circuit is screwed and doesn't know it yet,
99 * or is a rendezvous circuit. */
101 if (build_state->onehop_tunnel) {
102 if (!conn->want_onehop) {
103 log_debug(LD_CIRC,"Skipping one-hop circuit.");
104 return 0;
106 tor_assert(conn->chosen_exit_name);
107 if (build_state->chosen_exit) {
108 char digest[DIGEST_LEN];
109 if (hexdigest_to_digest(conn->chosen_exit_name, digest) < 0)
110 return 0; /* broken digest, we don't want it */
111 if (memcmp(digest, build_state->chosen_exit->identity_digest,
112 DIGEST_LEN))
113 return 0; /* this is a circuit to somewhere else */
114 if (tor_digest_is_zero(digest)) {
115 /* we don't know the digest; have to compare addr:port */
116 tor_addr_t addr;
117 int r = tor_addr_from_str(&addr, conn->socks_request->address);
118 if (r < 0 ||
119 !tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
120 build_state->chosen_exit->port != conn->socks_request->port)
121 return 0;
124 } else {
125 if (conn->want_onehop) {
126 /* don't use three-hop circuits -- that could hurt our anonymity. */
127 return 0;
130 if (exitrouter && !connection_ap_can_use_exit(conn, exitrouter)) {
131 /* can't exit from this router */
132 return 0;
134 } else { /* not general */
135 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
136 if ((conn->rend_data && !ocirc->rend_data) ||
137 (!conn->rend_data && ocirc->rend_data) ||
138 (conn->rend_data && ocirc->rend_data &&
139 rend_cmp_service_ids(conn->rend_data->onion_address,
140 ocirc->rend_data->onion_address))) {
141 /* this circ is not for this conn */
142 return 0;
145 return 1;
148 /** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
149 * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
151 static int
152 circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
154 switch (purpose) {
155 case CIRCUIT_PURPOSE_C_GENERAL:
156 /* if it's used but less dirty it's best;
157 * else if it's more recently created it's best
159 if (b->timestamp_dirty) {
160 if (a->timestamp_dirty &&
161 a->timestamp_dirty > b->timestamp_dirty)
162 return 1;
163 } else {
164 if (a->timestamp_dirty ||
165 timercmp(&a->timestamp_created, &b->timestamp_created, >))
166 return 1;
167 if (CIRCUIT_IS_ORIGIN(b) &&
168 TO_ORIGIN_CIRCUIT(b)->build_state->is_internal)
169 /* XXX023 what the heck is this internal thing doing here. I
170 * think we can get rid of it. circuit_is_acceptable() already
171 * makes sure that is_internal is exactly what we need it to
172 * be. -RD */
173 return 1;
175 break;
176 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
177 /* the closer it is to ack_wait the better it is */
178 if (a->purpose > b->purpose)
179 return 1;
180 break;
181 case CIRCUIT_PURPOSE_C_REND_JOINED:
182 /* the closer it is to rend_joined the better it is */
183 if (a->purpose > b->purpose)
184 return 1;
185 break;
187 return 0;
190 /** Find the best circ that conn can use, preferably one which is
191 * dirty. Circ must not be too old.
193 * Conn must be defined.
195 * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
197 * circ_purpose specifies what sort of circuit we must have.
198 * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
200 * If it's REND_JOINED and must_be_open==0, then return the closest
201 * rendezvous-purposed circuit that you can find.
203 * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
204 * closest introduce-purposed circuit that you can find.
206 static origin_circuit_t *
207 circuit_get_best(edge_connection_t *conn, int must_be_open, uint8_t purpose,
208 int need_uptime, int need_internal)
210 circuit_t *circ, *best=NULL;
211 struct timeval now;
212 int intro_going_on_but_too_old = 0;
214 tor_assert(conn);
216 tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
217 purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
218 purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
220 tor_gettimeofday(&now);
222 for (circ=global_circuitlist;circ;circ = circ->next) {
223 if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,
224 need_uptime,need_internal,now.tv_sec))
225 continue;
227 if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
228 !must_be_open && circ->state != CIRCUIT_STATE_OPEN &&
229 tv_mdiff(&now, &circ->timestamp_created) > circ_times.timeout_ms) {
230 intro_going_on_but_too_old = 1;
231 continue;
234 /* now this is an acceptable circ to hand back. but that doesn't
235 * mean it's the *best* circ to hand back. try to decide.
237 if (!best || circuit_is_better(circ,best,purpose))
238 best = circ;
241 if (!best && intro_going_on_but_too_old)
242 log_info(LD_REND|LD_CIRC, "There is an intro circuit being created "
243 "right now, but it has already taken quite a while. Starting "
244 "one in parallel.");
246 return best ? TO_ORIGIN_CIRCUIT(best) : NULL;
249 #if 0
250 /** Check whether, according to the policies in <b>options</b>, the
251 * circuit <b>circ</b> makes sense. */
252 /* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
253 * Also, it doesn't have the right definition of an exit circuit. Also,
254 * it's never called. */
256 circuit_conforms_to_options(const origin_circuit_t *circ,
257 const or_options_t *options)
259 const crypt_path_t *cpath, *cpath_next = NULL;
261 /* first check if it includes any excluded nodes */
262 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
263 cpath_next = cpath->next;
264 if (routerset_contains_extendinfo(options->ExcludeNodes,
265 cpath->extend_info))
266 return 0;
269 /* then consider the final hop */
270 if (routerset_contains_extendinfo(options->ExcludeExitNodes,
271 circ->cpath->prev->extend_info))
272 return 0;
274 return 1;
276 #endif
278 /** Close all circuits that start at us, aren't open, and were born
279 * at least CircuitBuildTimeout seconds ago.
281 void
282 circuit_expire_building(void)
284 circuit_t *victim, *next_circ = global_circuitlist;
285 /* circ_times.timeout_ms and circ_times.close_ms are from
286 * circuit_build_times_get_initial_timeout() if we haven't computed
287 * custom timeouts yet */
288 struct timeval general_cutoff, begindir_cutoff, fourhop_cutoff,
289 cannibalize_cutoff, close_cutoff, extremely_old_cutoff;
290 struct timeval now;
291 struct timeval introcirc_cutoff;
292 cpath_build_state_t *build_state;
294 tor_gettimeofday(&now);
295 #define SET_CUTOFF(target, msec) do { \
296 long ms = tor_lround(msec); \
297 struct timeval diff; \
298 diff.tv_sec = ms / 1000; \
299 diff.tv_usec = (int)((ms % 1000) * 1000); \
300 timersub(&now, &diff, &target); \
301 } while (0)
303 SET_CUTOFF(general_cutoff, circ_times.timeout_ms);
304 SET_CUTOFF(begindir_cutoff, circ_times.timeout_ms / 2.0);
305 SET_CUTOFF(fourhop_cutoff, circ_times.timeout_ms * (4/3.0));
306 SET_CUTOFF(cannibalize_cutoff, circ_times.timeout_ms / 2.0);
307 SET_CUTOFF(close_cutoff, circ_times.close_ms);
308 SET_CUTOFF(extremely_old_cutoff, circ_times.close_ms*2 + 1000);
310 introcirc_cutoff = begindir_cutoff;
312 while (next_circ) {
313 struct timeval cutoff;
314 victim = next_circ;
315 next_circ = next_circ->next;
316 if (!CIRCUIT_IS_ORIGIN(victim) || /* didn't originate here */
317 victim->marked_for_close) /* don't mess with marked circs */
318 continue;
320 build_state = TO_ORIGIN_CIRCUIT(victim)->build_state;
321 if (build_state && build_state->onehop_tunnel)
322 cutoff = begindir_cutoff;
323 else if (build_state && build_state->desired_path_len == 4
324 && !TO_ORIGIN_CIRCUIT(victim)->has_opened)
325 cutoff = fourhop_cutoff;
326 else if (TO_ORIGIN_CIRCUIT(victim)->has_opened)
327 cutoff = cannibalize_cutoff;
328 else if (victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING)
329 cutoff = introcirc_cutoff;
330 else if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
331 cutoff = close_cutoff;
332 else
333 cutoff = general_cutoff;
335 if (timercmp(&victim->timestamp_created, &cutoff, >))
336 continue; /* it's still young, leave it alone */
338 #if 0
339 /* some debug logs, to help track bugs */
340 if (victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
341 victim->timestamp_created <= introcirc_cutoff &&
342 victim->timestamp_created > general_cutoff)
343 log_info(LD_REND|LD_CIRC, "Timing out introduction circuit which we "
344 "would not have done if it had been a general circuit.");
346 if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
347 victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
348 if (!victim->timestamp_dirty)
349 log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d)."
350 "(clean).",
351 victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
352 victim->purpose, victim->build_state->chosen_exit_name,
353 victim->n_circ_id);
354 else
355 log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d). "
356 "%d secs since dirty.",
357 victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
358 victim->purpose, victim->build_state->chosen_exit_name,
359 victim->n_circ_id,
360 (int)(now - victim->timestamp_dirty));
362 #endif
364 /* if circ is !open, or if it's open but purpose is a non-finished
365 * intro or rend, then mark it for close */
366 if (victim->state == CIRCUIT_STATE_OPEN) {
367 switch (victim->purpose) {
368 default: /* most open circuits can be left alone. */
369 continue; /* yes, continue inside a switch refers to the nearest
370 * enclosing loop. C is smart. */
371 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
372 case CIRCUIT_PURPOSE_C_INTRODUCING:
373 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
374 break; /* too old, need to die */
375 case CIRCUIT_PURPOSE_C_REND_READY:
376 /* it's a rend_ready circ -- has it already picked a query? */
377 /* c_rend_ready circs measure age since timestamp_dirty,
378 * because that's set when they switch purposes
380 if (TO_ORIGIN_CIRCUIT(victim)->rend_data ||
381 victim->timestamp_dirty > cutoff.tv_sec)
382 continue;
383 break;
384 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
385 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
386 /* rend and intro circs become dirty each time they
387 * make an introduction attempt. so timestamp_dirty
388 * will reflect the time since the last attempt.
390 if (victim->timestamp_dirty > cutoff.tv_sec)
391 continue;
392 break;
394 } else { /* circuit not open, consider recording failure as timeout */
395 int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath &&
396 TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_OPEN;
398 if (TO_ORIGIN_CIRCUIT(victim)->p_streams != NULL) {
399 log_warn(LD_BUG, "Circuit %d (purpose %d, %s) has timed out, "
400 "yet has attached streams!",
401 TO_ORIGIN_CIRCUIT(victim)->global_identifier,
402 victim->purpose,
403 circuit_purpose_to_string(victim->purpose));
404 tor_fragile_assert();
405 continue;
408 if (circuit_timeout_want_to_count_circ(TO_ORIGIN_CIRCUIT(victim)) &&
409 circuit_build_times_enough_to_compute(&circ_times)) {
410 /* Circuits are allowed to last longer for measurement.
411 * Switch their purpose and wait. */
412 if (victim->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
413 control_event_circuit_status(TO_ORIGIN_CIRCUIT(victim),
414 CIRC_EVENT_FAILED,
415 END_CIRC_REASON_TIMEOUT);
416 victim->purpose = CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT;
417 /* Record this failure to check for too many timeouts
418 * in a row. This function does not record a time value yet
419 * (we do that later); it only counts the fact that we did
420 * have a timeout. */
421 circuit_build_times_count_timeout(&circ_times,
422 first_hop_succeeded);
423 continue;
427 * If the circuit build time is much greater than we would have cut
428 * it off at, we probably had a suspend event along this codepath,
429 * and we should discard the value.
431 if (timercmp(&victim->timestamp_created, &extremely_old_cutoff, <)) {
432 log_notice(LD_CIRC,
433 "Extremely large value for circuit build timeout: %lds. "
434 "Assuming clock jump. Purpose %d (%s)",
435 (long)(now.tv_sec - victim->timestamp_created.tv_sec),
436 victim->purpose,
437 circuit_purpose_to_string(victim->purpose));
438 } else if (circuit_build_times_count_close(&circ_times,
439 first_hop_succeeded,
440 victim->timestamp_created.tv_sec)) {
441 circuit_build_times_set_timeout(&circ_times);
446 if (victim->n_conn)
447 log_info(LD_CIRC,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
448 victim->n_conn->_base.address, victim->n_conn->_base.port,
449 victim->n_circ_id,
450 victim->state, circuit_state_to_string(victim->state),
451 victim->purpose);
452 else
453 log_info(LD_CIRC,"Abandoning circ %d (state %d:%s, purpose %d)",
454 victim->n_circ_id, victim->state,
455 circuit_state_to_string(victim->state), victim->purpose);
457 circuit_log_path(LOG_INFO,LD_CIRC,TO_ORIGIN_CIRCUIT(victim));
458 if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
459 circuit_mark_for_close(victim, END_CIRC_REASON_MEASUREMENT_EXPIRED);
460 else
461 circuit_mark_for_close(victim, END_CIRC_REASON_TIMEOUT);
465 /** Remove any elements in <b>needed_ports</b> that are handled by an
466 * open or in-progress circuit.
468 void
469 circuit_remove_handled_ports(smartlist_t *needed_ports)
471 int i;
472 uint16_t *port;
474 for (i = 0; i < smartlist_len(needed_ports); ++i) {
475 port = smartlist_get(needed_ports, i);
476 tor_assert(*port);
477 if (circuit_stream_is_being_handled(NULL, *port,
478 MIN_CIRCUITS_HANDLING_STREAM)) {
479 // log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
480 smartlist_del(needed_ports, i--);
481 tor_free(port);
482 } else {
483 log_debug(LD_CIRC,"Port %d is not handled.", *port);
488 /** Return 1 if at least <b>min</b> general-purpose non-internal circuits
489 * will have an acceptable exit node for exit stream <b>conn</b> if it
490 * is defined, else for "*:port".
491 * Else return 0.
494 circuit_stream_is_being_handled(edge_connection_t *conn,
495 uint16_t port, int min)
497 circuit_t *circ;
498 routerinfo_t *exitrouter;
499 int num=0;
500 time_t now = time(NULL);
501 int need_uptime = smartlist_string_num_isin(get_options()->LongLivedPorts,
502 conn ? conn->socks_request->port : port);
504 for (circ=global_circuitlist;circ;circ = circ->next) {
505 if (CIRCUIT_IS_ORIGIN(circ) &&
506 !circ->marked_for_close &&
507 circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
508 (!circ->timestamp_dirty ||
509 circ->timestamp_dirty + get_options()->MaxCircuitDirtiness > now)) {
510 cpath_build_state_t *build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
511 if (build_state->is_internal || build_state->onehop_tunnel)
512 continue;
514 exitrouter = build_state_get_exit_router(build_state);
515 if (exitrouter && (!need_uptime || build_state->need_uptime)) {
516 int ok;
517 if (conn) {
518 ok = connection_ap_can_use_exit(conn, exitrouter);
519 } else {
520 addr_policy_result_t r = compare_addr_to_addr_policy(
521 0, port, exitrouter->exit_policy);
522 ok = r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED;
524 if (ok) {
525 if (++num >= min)
526 return 1;
531 return 0;
534 /** Don't keep more than this many unused open circuits around. */
535 #define MAX_UNUSED_OPEN_CIRCUITS 14
537 /** Figure out how many circuits we have open that are clean. Make
538 * sure it's enough for all the upcoming behaviors we predict we'll have.
539 * But put an upper bound on the total number of circuits.
541 static void
542 circuit_predict_and_launch_new(void)
544 circuit_t *circ;
545 int num=0, num_internal=0, num_uptime_internal=0;
546 int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
547 int port_needs_uptime=0, port_needs_capacity=1;
548 time_t now = time(NULL);
549 int flags = 0;
551 /* First, count how many of each type of circuit we have already. */
552 for (circ=global_circuitlist;circ;circ = circ->next) {
553 cpath_build_state_t *build_state;
554 if (!CIRCUIT_IS_ORIGIN(circ))
555 continue;
556 if (circ->marked_for_close)
557 continue; /* don't mess with marked circs */
558 if (circ->timestamp_dirty)
559 continue; /* only count clean circs */
560 if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL)
561 continue; /* only pay attention to general-purpose circs */
562 build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
563 if (build_state->onehop_tunnel)
564 continue;
565 num++;
566 if (build_state->is_internal)
567 num_internal++;
568 if (build_state->need_uptime && build_state->is_internal)
569 num_uptime_internal++;
572 /* If that's enough, then stop now. */
573 if (num >= MAX_UNUSED_OPEN_CIRCUITS)
574 return; /* we already have many, making more probably will hurt */
576 /* Second, see if we need any more exit circuits. */
577 /* check if we know of a port that's been requested recently
578 * and no circuit is currently available that can handle it. */
579 if (!circuit_all_predicted_ports_handled(now, &port_needs_uptime,
580 &port_needs_capacity)) {
581 if (port_needs_uptime)
582 flags |= CIRCLAUNCH_NEED_UPTIME;
583 if (port_needs_capacity)
584 flags |= CIRCLAUNCH_NEED_CAPACITY;
585 log_info(LD_CIRC,
586 "Have %d clean circs (%d internal), need another exit circ.",
587 num, num_internal);
588 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL, flags);
589 return;
592 /* Third, see if we need any more hidden service (server) circuits. */
593 if (num_rend_services() && num_uptime_internal < 3) {
594 flags = (CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_NEED_UPTIME |
595 CIRCLAUNCH_IS_INTERNAL);
596 log_info(LD_CIRC,
597 "Have %d clean circs (%d internal), need another internal "
598 "circ for my hidden service.",
599 num, num_internal);
600 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL, flags);
601 return;
604 /* Fourth, see if we need any more hidden service (client) circuits. */
605 if (rep_hist_get_predicted_internal(now, &hidserv_needs_uptime,
606 &hidserv_needs_capacity) &&
607 ((num_uptime_internal<2 && hidserv_needs_uptime) ||
608 num_internal<2)) {
609 if (hidserv_needs_uptime)
610 flags |= CIRCLAUNCH_NEED_UPTIME;
611 if (hidserv_needs_capacity)
612 flags |= CIRCLAUNCH_NEED_CAPACITY;
613 flags |= CIRCLAUNCH_IS_INTERNAL;
614 log_info(LD_CIRC,
615 "Have %d clean circs (%d uptime-internal, %d internal), need"
616 " another hidden service circ.",
617 num, num_uptime_internal, num_internal);
618 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL, flags);
619 return;
622 /* Finally, check to see if we still need more circuits to learn
623 * a good build timeout. But if we're close to our max number we
624 * want, don't do another -- we want to leave a few slots open so
625 * we can still build circuits preemptively as needed. */
626 if (num < MAX_UNUSED_OPEN_CIRCUITS-2 &&
627 circuit_build_times_needs_circuits_now(&circ_times)) {
628 flags = CIRCLAUNCH_NEED_CAPACITY;
629 log_info(LD_CIRC,
630 "Have %d clean circs need another buildtime test circ.", num);
631 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL, flags);
632 return;
636 /** Build a new test circuit every 5 minutes */
637 #define TESTING_CIRCUIT_INTERVAL 300
639 /** This function is called once a second, if router_have_min_dir_info() is
640 * true. Its job is to make sure all services we offer have enough circuits
641 * available. Some services just want enough circuits for current tasks,
642 * whereas others want a minimum set of idle circuits hanging around.
644 void
645 circuit_build_needed_circs(time_t now)
647 static time_t time_to_new_circuit = 0;
648 or_options_t *options = get_options();
650 /* launch a new circ for any pending streams that need one */
651 connection_ap_attach_pending();
653 /* make sure any hidden services have enough intro points */
654 rend_services_introduce();
656 if (time_to_new_circuit < now) {
657 circuit_reset_failure_count(1);
658 time_to_new_circuit = now + options->NewCircuitPeriod;
659 if (proxy_mode(get_options()))
660 addressmap_clean(now);
661 circuit_expire_old_circuits_clientside();
663 #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
664 circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
665 if (get_options()->RunTesting &&
666 circ &&
667 circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
668 log_fn(LOG_INFO,"Creating a new testing circuit.");
669 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL, 0);
671 #endif
673 if (!options->DisablePredictedCircuits)
674 circuit_predict_and_launch_new();
677 /** If the stream <b>conn</b> is a member of any of the linked
678 * lists of <b>circ</b>, then remove it from the list.
680 void
681 circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
683 edge_connection_t *prevconn;
685 tor_assert(circ);
686 tor_assert(conn);
688 conn->cpath_layer = NULL; /* make sure we don't keep a stale pointer */
689 conn->on_circuit = NULL;
691 if (CIRCUIT_IS_ORIGIN(circ)) {
692 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
693 if (conn == origin_circ->p_streams) {
694 origin_circ->p_streams = conn->next_stream;
695 return;
698 for (prevconn = origin_circ->p_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;
704 return;
706 } else {
707 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
708 if (conn == or_circ->n_streams) {
709 or_circ->n_streams = conn->next_stream;
710 return;
712 if (conn == or_circ->resolving_streams) {
713 or_circ->resolving_streams = conn->next_stream;
714 return;
717 for (prevconn = or_circ->n_streams;
718 prevconn && prevconn->next_stream && prevconn->next_stream != conn;
719 prevconn = prevconn->next_stream)
721 if (prevconn && prevconn->next_stream) {
722 prevconn->next_stream = conn->next_stream;
723 return;
726 for (prevconn = or_circ->resolving_streams;
727 prevconn && prevconn->next_stream && prevconn->next_stream != conn;
728 prevconn = prevconn->next_stream)
730 if (prevconn && prevconn->next_stream) {
731 prevconn->next_stream = conn->next_stream;
732 return;
736 log_warn(LD_BUG,"Edge connection not in circuit's list.");
737 /* Don't give an error here; it's harmless. */
738 tor_fragile_assert();
741 /** If we haven't yet decided on a good timeout value for circuit
742 * building, we close idles circuits aggressively so we can get more
743 * data points. */
744 #define IDLE_TIMEOUT_WHILE_LEARNING (10*60)
746 /** Find each circuit that has been unused for too long, or dirty
747 * for too long and has no streams on it: mark it for close.
749 static void
750 circuit_expire_old_circuits_clientside(void)
752 circuit_t *circ;
753 struct timeval cutoff, now;
755 tor_gettimeofday(&now);
756 cutoff = now;
758 if (circuit_build_times_needs_circuits(&circ_times)) {
759 /* Circuits should be shorter lived if we need more of them
760 * for learning a good build timeout */
761 cutoff.tv_sec -= IDLE_TIMEOUT_WHILE_LEARNING;
762 } else {
763 cutoff.tv_sec -= get_options()->CircuitIdleTimeout;
766 for (circ = global_circuitlist; circ; circ = circ->next) {
767 if (circ->marked_for_close || !CIRCUIT_IS_ORIGIN(circ))
768 continue;
769 /* If the circuit has been dirty for too long, and there are no streams
770 * on it, mark it for close.
772 if (circ->timestamp_dirty &&
773 circ->timestamp_dirty + get_options()->MaxCircuitDirtiness <
774 now.tv_sec &&
775 !TO_ORIGIN_CIRCUIT(circ)->p_streams /* nothing attached */ ) {
776 log_debug(LD_CIRC, "Closing n_circ_id %d (dirty %ld sec ago, "
777 "purpose %d)",
778 circ->n_circ_id, (long)(now.tv_sec - circ->timestamp_dirty),
779 circ->purpose);
780 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
781 } else if (!circ->timestamp_dirty && circ->state == CIRCUIT_STATE_OPEN) {
782 if (timercmp(&circ->timestamp_created, &cutoff, <)) {
783 if (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
784 circ->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT ||
785 circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
786 circ->purpose == CIRCUIT_PURPOSE_TESTING ||
787 (circ->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
788 circ->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) ||
789 circ->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
790 log_debug(LD_CIRC,
791 "Closing circuit that has been unused for %ld msec.",
792 tv_mdiff(&circ->timestamp_created, &now));
793 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
794 } else if (!TO_ORIGIN_CIRCUIT(circ)->is_ancient) {
795 /* Server-side rend joined circuits can end up really old, because
796 * they are reused by clients for longer than normal. The client
797 * controls their lifespan. (They never become dirty, because
798 * connection_exit_begin_conn() never marks anything as dirty.)
799 * Similarly, server-side intro circuits last a long time. */
800 if (circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED &&
801 circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
802 log_notice(LD_CIRC,
803 "Ancient non-dirty circuit %d is still around after "
804 "%ld milliseconds. Purpose: %d (%s)",
805 TO_ORIGIN_CIRCUIT(circ)->global_identifier,
806 tv_mdiff(&circ->timestamp_created, &now),
807 circ->purpose,
808 circuit_purpose_to_string(circ->purpose));
809 TO_ORIGIN_CIRCUIT(circ)->is_ancient = 1;
817 /** How long do we wait before killing circuits with the properties
818 * described below?
820 * Probably we could choose a number here as low as 5 to 10 seconds,
821 * since these circs are used for begindir, and a) generally you either
822 * ask another begindir question right after or you don't for a long time,
823 * b) clients at least through 0.2.1.x choose from the whole set of
824 * directory mirrors at each choice, and c) re-establishing a one-hop
825 * circuit via create-fast is a light operation assuming the TLS conn is
826 * still there.
828 * I expect "b" to go away one day when we move to using directory
829 * guards, but I think "a" and "c" are good enough reasons that a low
830 * number is safe even then.
832 #define IDLE_ONE_HOP_CIRC_TIMEOUT 60
834 /** Find each non-origin circuit that has been unused for too long,
835 * has no streams on it, used a create_fast, and ends here: mark it
836 * for close.
838 void
839 circuit_expire_old_circuits_serverside(time_t now)
841 circuit_t *circ;
842 or_circuit_t *or_circ;
843 time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;
845 for (circ = global_circuitlist; circ; circ = circ->next) {
846 if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
847 continue;
848 or_circ = TO_OR_CIRCUIT(circ);
849 /* If the circuit has been idle for too long, and there are no streams
850 * on it, and it ends here, and it used a create_fast, mark it for close.
852 if (or_circ->is_first_hop && !circ->n_conn &&
853 !or_circ->n_streams && !or_circ->resolving_streams &&
854 or_circ->p_conn &&
855 or_circ->p_conn->timestamp_last_added_nonpadding <= cutoff) {
856 log_info(LD_CIRC, "Closing circ_id %d (empty %d secs ago)",
857 or_circ->p_circ_id,
858 (int)(now - or_circ->p_conn->timestamp_last_added_nonpadding));
859 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
864 /** Number of testing circuits we want open before testing our bandwidth. */
865 #define NUM_PARALLEL_TESTING_CIRCS 4
867 /** True iff we've ever had enough testing circuits open to test our
868 * bandwidth. */
869 static int have_performed_bandwidth_test = 0;
871 /** Reset have_performed_bandwidth_test, so we'll start building
872 * testing circuits again so we can exercise our bandwidth. */
873 void
874 reset_bandwidth_test(void)
876 have_performed_bandwidth_test = 0;
879 /** Return 1 if we've already exercised our bandwidth, or if we
880 * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
881 * established or on the way. Else return 0.
884 circuit_enough_testing_circs(void)
886 circuit_t *circ;
887 int num = 0;
889 if (have_performed_bandwidth_test)
890 return 1;
892 for (circ = global_circuitlist; circ; circ = circ->next) {
893 if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
894 circ->purpose == CIRCUIT_PURPOSE_TESTING &&
895 circ->state == CIRCUIT_STATE_OPEN)
896 num++;
898 return num >= NUM_PARALLEL_TESTING_CIRCS;
901 /** A testing circuit has completed. Take whatever stats we want.
902 * Noticing reachability is taken care of in onionskin_answer(),
903 * so there's no need to record anything here. But if we still want
904 * to do the bandwidth test, and we now have enough testing circuits
905 * open, do it.
907 static void
908 circuit_testing_opened(origin_circuit_t *circ)
910 if (have_performed_bandwidth_test ||
911 !check_whether_orport_reachable()) {
912 /* either we've already done everything we want with testing circuits,
913 * or this testing circuit became open due to a fluke, e.g. we picked
914 * a last hop where we already had the connection open due to an
915 * outgoing local circuit. */
916 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_AT_ORIGIN);
917 } else if (circuit_enough_testing_circs()) {
918 router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS, time(NULL));
919 have_performed_bandwidth_test = 1;
920 } else
921 consider_testing_reachability(1, 0);
924 /** A testing circuit has failed to build. Take whatever stats we want. */
925 static void
926 circuit_testing_failed(origin_circuit_t *circ, int at_last_hop)
928 if (server_mode(get_options()) && check_whether_orport_reachable())
929 return;
931 log_info(LD_GENERAL,
932 "Our testing circuit (to see if your ORPort is reachable) "
933 "has failed. I'll try again later.");
935 /* These aren't used yet. */
936 (void)circ;
937 (void)at_last_hop;
940 /** The circuit <b>circ</b> has just become open. Take the next
941 * step: for rendezvous circuits, we pass circ to the appropriate
942 * function in rendclient or rendservice. For general circuits, we
943 * call connection_ap_attach_pending, which looks for pending streams
944 * that could use circ.
946 void
947 circuit_has_opened(origin_circuit_t *circ)
949 control_event_circuit_status(circ, CIRC_EVENT_BUILT, 0);
951 /* Remember that this circuit has finished building. Now if we start
952 * it building again later (e.g. by extending it), we will know not
953 * to consider its build time. */
954 circ->has_opened = 1;
956 switch (TO_CIRCUIT(circ)->purpose) {
957 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
958 rend_client_rendcirc_has_opened(circ);
959 connection_ap_attach_pending();
960 break;
961 case CIRCUIT_PURPOSE_C_INTRODUCING:
962 rend_client_introcirc_has_opened(circ);
963 break;
964 case CIRCUIT_PURPOSE_C_GENERAL:
965 /* Tell any AP connections that have been waiting for a new
966 * circuit that one is ready. */
967 connection_ap_attach_pending();
968 break;
969 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
970 /* at Bob, waiting for introductions */
971 rend_service_intro_has_opened(circ);
972 break;
973 case CIRCUIT_PURPOSE_S_CONNECT_REND:
974 /* at Bob, connecting to rend point */
975 rend_service_rendezvous_has_opened(circ);
976 break;
977 case CIRCUIT_PURPOSE_TESTING:
978 circuit_testing_opened(circ);
979 break;
980 /* default:
981 * This won't happen in normal operation, but might happen if the
982 * controller did it. Just let it slide. */
986 /** Called whenever a circuit could not be successfully built.
988 void
989 circuit_build_failed(origin_circuit_t *circ)
991 /* we should examine circ and see if it failed because of
992 * the last hop or an earlier hop. then use this info below.
994 int failed_at_last_hop = 0;
995 /* If the last hop isn't open, and the second-to-last is, we failed
996 * at the last hop. */
997 if (circ->cpath &&
998 circ->cpath->prev->state != CPATH_STATE_OPEN &&
999 circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
1000 failed_at_last_hop = 1;
1002 if (circ->cpath &&
1003 circ->cpath->state != CPATH_STATE_OPEN) {
1004 /* We failed at the first hop. If there's an OR connection
1005 * to blame, blame it. Also, avoid this relay for a while, and
1006 * fail any one-hop directory fetches destined for it. */
1007 const char *n_conn_id = circ->cpath->extend_info->identity_digest;
1008 int already_marked = 0;
1009 if (circ->_base.n_conn) {
1010 or_connection_t *n_conn = circ->_base.n_conn;
1011 if (n_conn->is_bad_for_new_circs) {
1012 /* We only want to blame this router when a fresh healthy
1013 * connection fails. So don't mark this router as newly failed,
1014 * since maybe this was just an old circuit attempt that's
1015 * finally timing out now. Also, there's no need to blow away
1016 * circuits/streams/etc, since the failure of an unhealthy conn
1017 * doesn't tell us much about whether a healthy conn would
1018 * succeed. */
1019 already_marked = 1;
1021 log_info(LD_OR,
1022 "Our circuit failed to get a response from the first hop "
1023 "(%s:%d). I'm going to try to rotate to a better connection.",
1024 n_conn->_base.address, n_conn->_base.port);
1025 n_conn->is_bad_for_new_circs = 1;
1026 } else {
1027 log_info(LD_OR,
1028 "Our circuit died before the first hop with no connection");
1030 if (n_conn_id && !already_marked) {
1031 entry_guard_register_connect_status(n_conn_id, 0, 1, time(NULL));
1032 /* if there are any one-hop streams waiting on this circuit, fail
1033 * them now so they can retry elsewhere. */
1034 connection_ap_fail_onehop(n_conn_id, circ->build_state);
1038 switch (circ->_base.purpose) {
1039 case CIRCUIT_PURPOSE_C_GENERAL:
1040 /* If we never built the circuit, note it as a failure. */
1041 circuit_increment_failure_count();
1042 if (failed_at_last_hop) {
1043 /* Make sure any streams that demand our last hop as their exit
1044 * know that it's unlikely to happen. */
1045 circuit_discard_optional_exit_enclaves(circ->cpath->prev->extend_info);
1047 break;
1048 case CIRCUIT_PURPOSE_TESTING:
1049 circuit_testing_failed(circ, failed_at_last_hop);
1050 break;
1051 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
1052 /* at Bob, waiting for introductions */
1053 if (circ->_base.state != CIRCUIT_STATE_OPEN) {
1054 circuit_increment_failure_count();
1056 /* no need to care here, because bob will rebuild intro
1057 * points periodically. */
1058 break;
1059 case CIRCUIT_PURPOSE_C_INTRODUCING:
1060 /* at Alice, connecting to intro point */
1061 /* Don't increment failure count, since Bob may have picked
1062 * the introduction point maliciously */
1063 /* Alice will pick a new intro point when this one dies, if
1064 * the stream in question still cares. No need to act here. */
1065 break;
1066 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
1067 /* at Alice, waiting for Bob */
1068 circuit_increment_failure_count();
1069 /* Alice will pick a new rend point when this one dies, if
1070 * the stream in question still cares. No need to act here. */
1071 break;
1072 case CIRCUIT_PURPOSE_S_CONNECT_REND:
1073 /* at Bob, connecting to rend point */
1074 /* Don't increment failure count, since Alice may have picked
1075 * the rendezvous point maliciously */
1076 log_info(LD_REND,
1077 "Couldn't connect to Alice's chosen rend point %s "
1078 "(%s hop failed).",
1079 escaped(build_state_get_exit_nickname(circ->build_state)),
1080 failed_at_last_hop?"last":"non-last");
1081 rend_service_relaunch_rendezvous(circ);
1082 break;
1083 /* default:
1084 * This won't happen in normal operation, but might happen if the
1085 * controller did it. Just let it slide. */
1089 /** Number of consecutive failures so far; should only be touched by
1090 * circuit_launch_new and circuit_*_failure_count.
1092 static int n_circuit_failures = 0;
1093 /** Before the last time we called circuit_reset_failure_count(), were
1094 * there a lot of failures? */
1095 static int did_circs_fail_last_period = 0;
1097 /** Don't retry launching a new circuit if we try this many times with no
1098 * success. */
1099 #define MAX_CIRCUIT_FAILURES 5
1101 /** Launch a new circuit; see circuit_launch_by_extend_info() for
1102 * details on arguments. */
1103 origin_circuit_t *
1104 circuit_launch_by_router(uint8_t purpose,
1105 routerinfo_t *exit, int flags)
1107 origin_circuit_t *circ;
1108 extend_info_t *info = NULL;
1109 if (exit)
1110 info = extend_info_from_router(exit);
1111 circ = circuit_launch_by_extend_info(purpose, info, flags);
1113 extend_info_free(info);
1114 return circ;
1117 /** Launch a new circuit with purpose <b>purpose</b> and exit node
1118 * <b>extend_info</b> (or NULL to select a random exit node). If flags
1119 * contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
1120 * CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
1121 * If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
1122 * If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
1123 * Return the newly allocated circuit on success, or NULL on failure. */
1124 origin_circuit_t *
1125 circuit_launch_by_extend_info(uint8_t purpose,
1126 extend_info_t *extend_info,
1127 int flags)
1129 origin_circuit_t *circ;
1130 int onehop_tunnel = (flags & CIRCLAUNCH_ONEHOP_TUNNEL) != 0;
1132 if (!onehop_tunnel && !router_have_minimum_dir_info()) {
1133 log_debug(LD_CIRC,"Haven't fetched enough directory info yet; canceling "
1134 "circuit launch.");
1135 return NULL;
1138 if ((extend_info || purpose != CIRCUIT_PURPOSE_C_GENERAL) &&
1139 purpose != CIRCUIT_PURPOSE_TESTING && !onehop_tunnel) {
1140 /* see if there are appropriate circs available to cannibalize. */
1141 /* XXX if we're planning to add a hop, perhaps we want to look for
1142 * internal circs rather than exit circs? -RD */
1143 circ = circuit_find_to_cannibalize(purpose, extend_info, flags);
1144 if (circ) {
1145 log_info(LD_CIRC,"Cannibalizing circ '%s' for purpose %d (%s)",
1146 build_state_get_exit_nickname(circ->build_state), purpose,
1147 circuit_purpose_to_string(purpose));
1148 circ->_base.purpose = purpose;
1149 /* reset the birth date of this circ, else expire_building
1150 * will see it and think it's been trying to build since it
1151 * began. */
1152 tor_gettimeofday(&circ->_base.timestamp_created);
1153 switch (purpose) {
1154 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
1155 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
1156 /* it's ready right now */
1157 break;
1158 case CIRCUIT_PURPOSE_C_INTRODUCING:
1159 case CIRCUIT_PURPOSE_S_CONNECT_REND:
1160 case CIRCUIT_PURPOSE_C_GENERAL:
1161 /* need to add a new hop */
1162 tor_assert(extend_info);
1163 if (circuit_extend_to_new_exit(circ, extend_info) < 0)
1164 return NULL;
1165 break;
1166 default:
1167 log_warn(LD_BUG,
1168 "unexpected purpose %d when cannibalizing a circ.",
1169 purpose);
1170 tor_fragile_assert();
1171 return NULL;
1173 return circ;
1177 if (did_circs_fail_last_period &&
1178 n_circuit_failures > MAX_CIRCUIT_FAILURES) {
1179 /* too many failed circs in a row. don't try. */
1180 // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
1181 return NULL;
1184 /* try a circ. if it fails, circuit_mark_for_close will increment
1185 * n_circuit_failures */
1186 return circuit_establish_circuit(purpose, extend_info, flags);
1189 /** Record another failure at opening a general circuit. When we have
1190 * too many, we'll stop trying for the remainder of this minute.
1192 static void
1193 circuit_increment_failure_count(void)
1195 ++n_circuit_failures;
1196 log_debug(LD_CIRC,"n_circuit_failures now %d.",n_circuit_failures);
1199 /** Reset the failure count for opening general circuits. This means
1200 * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
1201 * stopping again.
1203 void
1204 circuit_reset_failure_count(int timeout)
1206 if (timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
1207 did_circs_fail_last_period = 1;
1208 else
1209 did_circs_fail_last_period = 0;
1210 n_circuit_failures = 0;
1213 /** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
1214 * there isn't one, and there isn't one on the way, launch one and return
1215 * 0. If it will never work, return -1.
1217 * Write the found or in-progress or launched circ into *circp.
1219 static int
1220 circuit_get_open_circ_or_launch(edge_connection_t *conn,
1221 uint8_t desired_circuit_purpose,
1222 origin_circuit_t **circp)
1224 origin_circuit_t *circ;
1225 int check_exit_policy;
1226 int need_uptime, need_internal;
1227 int want_onehop;
1228 or_options_t *options = get_options();
1230 tor_assert(conn);
1231 tor_assert(circp);
1232 tor_assert(conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT);
1233 check_exit_policy =
1234 conn->socks_request->command == SOCKS_COMMAND_CONNECT &&
1235 !conn->use_begindir &&
1236 !connection_edge_is_rendezvous_stream(conn);
1237 want_onehop = conn->want_onehop;
1239 need_uptime = !conn->want_onehop && !conn->use_begindir &&
1240 smartlist_string_num_isin(options->LongLivedPorts,
1241 conn->socks_request->port);
1242 need_internal = desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL;
1244 circ = circuit_get_best(conn, 1, desired_circuit_purpose,
1245 need_uptime, need_internal);
1247 if (circ) {
1248 *circp = circ;
1249 return 1; /* we're happy */
1252 if (!want_onehop && !router_have_minimum_dir_info()) {
1253 if (!connection_get_by_type(CONN_TYPE_DIR)) {
1254 int severity = LOG_NOTICE;
1255 /* FFFF if this is a tunneled directory fetch, don't yell
1256 * as loudly. the user doesn't even know it's happening. */
1257 if (entry_list_is_constrained(options) &&
1258 entries_known_but_down(options)) {
1259 log_fn(severity, LD_APP|LD_DIR,
1260 "Application request when we haven't used client functionality "
1261 "lately. Optimistically trying known %s again.",
1262 options->UseBridges ? "bridges" : "entrynodes");
1263 entries_retry_all(options);
1264 } else if (!options->UseBridges || any_bridge_descriptors_known()) {
1265 log_fn(severity, LD_APP|LD_DIR,
1266 "Application request when we haven't used client functionality "
1267 "lately. Optimistically trying directory fetches again.");
1268 routerlist_retry_directory_downloads(time(NULL));
1271 /* the stream will be dealt with when router_have_minimum_dir_info becomes
1272 * 1, or when all directory attempts fail and directory_all_unreachable()
1273 * kills it.
1275 return 0;
1278 /* Do we need to check exit policy? */
1279 if (check_exit_policy) {
1280 if (!conn->chosen_exit_name) {
1281 struct in_addr in;
1282 uint32_t addr = 0;
1283 if (tor_inet_aton(conn->socks_request->address, &in))
1284 addr = ntohl(in.s_addr);
1285 if (router_exit_policy_all_routers_reject(addr,
1286 conn->socks_request->port,
1287 need_uptime)) {
1288 log_notice(LD_APP,
1289 "No Tor server allows exit to %s:%d. Rejecting.",
1290 safe_str_client(conn->socks_request->address),
1291 conn->socks_request->port);
1292 return -1;
1294 } else {
1295 /* XXXX023 Duplicates checks in connection_ap_handshake_attach_circuit:
1296 * refactor into a single function? */
1297 routerinfo_t *router = router_get_by_nickname(conn->chosen_exit_name, 1);
1298 int opt = conn->chosen_exit_optional;
1299 if (router && !connection_ap_can_use_exit(conn, router)) {
1300 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
1301 "Requested exit point '%s' is excluded or "
1302 "would refuse request. %s.",
1303 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
1304 if (opt) {
1305 conn->chosen_exit_optional = 0;
1306 tor_free(conn->chosen_exit_name);
1307 /* Try again. */
1308 return circuit_get_open_circ_or_launch(conn,
1309 desired_circuit_purpose,
1310 circp);
1312 return -1;
1317 /* is one already on the way? */
1318 circ = circuit_get_best(conn, 0, desired_circuit_purpose,
1319 need_uptime, need_internal);
1320 if (circ)
1321 log_debug(LD_CIRC, "one on the way!");
1322 if (!circ) {
1323 extend_info_t *extend_info=NULL;
1324 uint8_t new_circ_purpose;
1326 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
1327 /* need to pick an intro point */
1328 tor_assert(conn->rend_data);
1329 extend_info = rend_client_get_random_intro(conn->rend_data);
1330 if (!extend_info) {
1331 log_info(LD_REND,
1332 "No intro points for '%s': re-fetching service descriptor.",
1333 safe_str_client(conn->rend_data->onion_address));
1334 rend_client_refetch_v2_renddesc(conn->rend_data);
1335 conn->_base.state = AP_CONN_STATE_RENDDESC_WAIT;
1336 return 0;
1338 log_info(LD_REND,"Chose '%s' as intro point for '%s'.",
1339 extend_info->nickname,
1340 safe_str_client(conn->rend_data->onion_address));
1343 /* If we have specified a particular exit node for our
1344 * connection, then be sure to open a circuit to that exit node.
1346 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
1347 if (conn->chosen_exit_name) {
1348 routerinfo_t *r;
1349 int opt = conn->chosen_exit_optional;
1350 r = router_get_by_nickname(conn->chosen_exit_name, 1);
1351 if (r) {
1352 extend_info = extend_info_from_router(r);
1353 } else {
1354 log_debug(LD_DIR, "considering %d, %s",
1355 want_onehop, conn->chosen_exit_name);
1356 if (want_onehop && conn->chosen_exit_name[0] == '$') {
1357 /* We're asking for a one-hop circuit to a router that
1358 * we don't have a routerinfo about. Make up an extend_info. */
1359 char digest[DIGEST_LEN];
1360 char *hexdigest = conn->chosen_exit_name+1;
1361 tor_addr_t addr;
1362 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
1363 base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN)<0) {
1364 log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
1365 return -1;
1367 if (tor_addr_from_str(&addr, conn->socks_request->address) < 0) {
1368 log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
1369 escaped_safe_str_client(conn->socks_request->address));
1370 return -1;
1372 extend_info = extend_info_alloc(conn->chosen_exit_name+1,
1373 digest, NULL, &addr,
1374 conn->socks_request->port);
1375 } else {
1376 /* We will need an onion key for the router, and we
1377 * don't have one. Refuse or relax requirements. */
1378 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
1379 "Requested exit point '%s' is not known. %s.",
1380 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
1381 if (opt) {
1382 conn->chosen_exit_optional = 0;
1383 tor_free(conn->chosen_exit_name);
1384 /* Try again with no requested exit */
1385 return circuit_get_open_circ_or_launch(conn,
1386 desired_circuit_purpose,
1387 circp);
1389 return -1;
1395 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
1396 new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
1397 else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
1398 new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
1399 else
1400 new_circ_purpose = desired_circuit_purpose;
1403 int flags = CIRCLAUNCH_NEED_CAPACITY;
1404 if (want_onehop) flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
1405 if (need_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
1406 if (need_internal) flags |= CIRCLAUNCH_IS_INTERNAL;
1407 circ = circuit_launch_by_extend_info(new_circ_purpose, extend_info,
1408 flags);
1411 extend_info_free(extend_info);
1413 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
1414 /* We just caused a circuit to get built because of this stream.
1415 * If this stream has caused a _lot_ of circuits to be built, that's
1416 * a bad sign: we should tell the user. */
1417 if (conn->num_circuits_launched < NUM_CIRCUITS_LAUNCHED_THRESHOLD &&
1418 ++conn->num_circuits_launched == NUM_CIRCUITS_LAUNCHED_THRESHOLD)
1419 log_warn(LD_BUG, "The application request to %s:%d has launched "
1420 "%d circuits without finding one it likes.",
1421 escaped_safe_str_client(conn->socks_request->address),
1422 conn->socks_request->port,
1423 conn->num_circuits_launched);
1424 } else {
1425 /* help predict this next time */
1426 rep_hist_note_used_internal(time(NULL), need_uptime, 1);
1427 if (circ) {
1428 /* write the service_id into circ */
1429 circ->rend_data = rend_data_dup(conn->rend_data);
1430 if (circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
1431 circ->_base.state == CIRCUIT_STATE_OPEN)
1432 rend_client_rendcirc_has_opened(circ);
1436 if (!circ)
1437 log_info(LD_APP,
1438 "No safe circuit (purpose %d) ready for edge "
1439 "connection; delaying.",
1440 desired_circuit_purpose);
1441 *circp = circ;
1442 return 0;
1445 /** Return true iff <b>crypt_path</b> is one of the crypt_paths for
1446 * <b>circ</b>. */
1447 static int
1448 cpath_is_on_circuit(origin_circuit_t *circ, crypt_path_t *crypt_path)
1450 crypt_path_t *cpath, *cpath_next = NULL;
1451 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
1452 cpath_next = cpath->next;
1453 if (crypt_path == cpath)
1454 return 1;
1456 return 0;
1459 /** Attach the AP stream <b>apconn</b> to circ's linked list of
1460 * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
1461 * hop in circ's cpath if <b>cpath</b> is NULL.
1463 static void
1464 link_apconn_to_circ(edge_connection_t *apconn, origin_circuit_t *circ,
1465 crypt_path_t *cpath)
1467 /* add it into the linked list of streams on this circuit */
1468 log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %d.",
1469 circ->_base.n_circ_id);
1470 /* reset it, so we can measure circ timeouts */
1471 apconn->_base.timestamp_lastread = time(NULL);
1472 apconn->next_stream = circ->p_streams;
1473 apconn->on_circuit = TO_CIRCUIT(circ);
1474 /* assert_connection_ok(conn, time(NULL)); */
1475 circ->p_streams = apconn;
1477 if (cpath) { /* we were given one; use it */
1478 tor_assert(cpath_is_on_circuit(circ, cpath));
1479 apconn->cpath_layer = cpath;
1480 } else { /* use the last hop in the circuit */
1481 tor_assert(circ->cpath);
1482 tor_assert(circ->cpath->prev);
1483 tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
1484 apconn->cpath_layer = circ->cpath->prev;
1488 /** If an exit wasn't specifically chosen, save the history for future
1489 * use. */
1490 static void
1491 consider_recording_trackhost(edge_connection_t *conn, origin_circuit_t *circ)
1493 int found_needle = 0;
1494 or_options_t *options = get_options();
1495 size_t len;
1496 char *new_address;
1497 char fp[HEX_DIGEST_LEN+1];
1499 /* Search the addressmap for this conn's destination. */
1500 /* If he's not in the address map.. */
1501 if (!options->TrackHostExits ||
1502 addressmap_have_mapping(conn->socks_request->address,
1503 options->TrackHostExitsExpire))
1504 return; /* nothing to track, or already mapped */
1506 SMARTLIST_FOREACH(options->TrackHostExits, const char *, cp, {
1507 if (cp[0] == '.') { /* match end */
1508 if (cp[1] == '\0' ||
1509 !strcasecmpend(conn->socks_request->address, cp) ||
1510 !strcasecmp(conn->socks_request->address, &cp[1]))
1511 found_needle = 1;
1512 } else if (strcasecmp(cp, conn->socks_request->address) == 0) {
1513 found_needle = 1;
1517 if (!found_needle || !circ->build_state->chosen_exit)
1518 return;
1520 /* write down the fingerprint of the chosen exit, not the nickname,
1521 * because the chosen exit might not be named. */
1522 base16_encode(fp, sizeof(fp),
1523 circ->build_state->chosen_exit->identity_digest, DIGEST_LEN);
1525 /* Add this exit/hostname pair to the addressmap. */
1526 len = strlen(conn->socks_request->address) + 1 /* '.' */ +
1527 strlen(fp) + 1 /* '.' */ +
1528 strlen("exit") + 1 /* '\0' */;
1529 new_address = tor_malloc(len);
1531 tor_snprintf(new_address, len, "%s.%s.exit",
1532 conn->socks_request->address, fp);
1534 addressmap_register(conn->socks_request->address, new_address,
1535 time(NULL) + options->TrackHostExitsExpire,
1536 ADDRMAPSRC_TRACKEXIT);
1539 /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
1540 * begin or resolve cell as appropriate. Return values are as for
1541 * connection_ap_handshake_attach_circuit. The stream will exit from the hop
1542 * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
1543 * <b>cpath</b> is NULL. */
1545 connection_ap_handshake_attach_chosen_circuit(edge_connection_t *conn,
1546 origin_circuit_t *circ,
1547 crypt_path_t *cpath)
1549 tor_assert(conn);
1550 tor_assert(conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT ||
1551 conn->_base.state == AP_CONN_STATE_CONTROLLER_WAIT);
1552 tor_assert(conn->socks_request);
1553 tor_assert(circ);
1554 tor_assert(circ->_base.state == CIRCUIT_STATE_OPEN);
1556 conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
1558 if (!circ->_base.timestamp_dirty)
1559 circ->_base.timestamp_dirty = time(NULL);
1561 link_apconn_to_circ(conn, circ, cpath);
1562 tor_assert(conn->socks_request);
1563 if (conn->socks_request->command == SOCKS_COMMAND_CONNECT) {
1564 if (!conn->use_begindir)
1565 consider_recording_trackhost(conn, circ);
1566 if (connection_ap_handshake_send_begin(conn) < 0)
1567 return -1;
1568 } else {
1569 if (connection_ap_handshake_send_resolve(conn) < 0)
1570 return -1;
1573 return 1;
1576 /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
1577 * we don't find one: if conn cannot be handled by any known nodes,
1578 * warn and return -1 (conn needs to die, and is maybe already marked);
1579 * else launch new circuit (if necessary) and return 0.
1580 * Otherwise, associate conn with a safe live circuit, do the
1581 * right next step, and return 1.
1583 /* XXXX this function should mark for close whenever it returns -1;
1584 * its callers shouldn't have to worry about that. */
1586 connection_ap_handshake_attach_circuit(edge_connection_t *conn)
1588 int retval;
1589 int conn_age;
1590 int want_onehop;
1592 tor_assert(conn);
1593 tor_assert(conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT);
1594 tor_assert(conn->socks_request);
1595 want_onehop = conn->want_onehop;
1597 conn_age = (int)(time(NULL) - conn->_base.timestamp_created);
1599 if (conn_age >= get_options()->SocksTimeout) {
1600 int severity = (tor_addr_is_null(&conn->_base.addr) && !conn->_base.port) ?
1601 LOG_INFO : LOG_NOTICE;
1602 log_fn(severity, LD_APP,
1603 "Tried for %d seconds to get a connection to %s:%d. Giving up.",
1604 conn_age, safe_str_client(conn->socks_request->address),
1605 conn->socks_request->port);
1606 return -1;
1609 if (!connection_edge_is_rendezvous_stream(conn)) { /* we're a general conn */
1610 origin_circuit_t *circ=NULL;
1612 if (conn->chosen_exit_name) {
1613 routerinfo_t *router = router_get_by_nickname(conn->chosen_exit_name, 1);
1614 int opt = conn->chosen_exit_optional;
1615 if (!router && !want_onehop) {
1616 /* We ran into this warning when trying to extend a circuit to a
1617 * hidden service directory for which we didn't have a router
1618 * descriptor. See flyspray task 767 for more details. We should
1619 * keep this in mind when deciding to use BEGIN_DIR cells for other
1620 * directory requests as well. -KL*/
1621 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
1622 "Requested exit point '%s' is not known. %s.",
1623 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
1624 if (opt) {
1625 conn->chosen_exit_optional = 0;
1626 tor_free(conn->chosen_exit_name);
1627 return 0;
1629 return -1;
1631 if (router && !connection_ap_can_use_exit(conn, router)) {
1632 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
1633 "Requested exit point '%s' is excluded or "
1634 "would refuse request. %s.",
1635 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
1636 if (opt) {
1637 conn->chosen_exit_optional = 0;
1638 tor_free(conn->chosen_exit_name);
1639 return 0;
1641 return -1;
1645 /* find the circuit that we should use, if there is one. */
1646 retval = circuit_get_open_circ_or_launch(
1647 conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
1648 if (retval < 1) // XXX022 if we totally fail, this still returns 0 -RD
1649 return retval;
1651 log_debug(LD_APP|LD_CIRC,
1652 "Attaching apconn to circ %d (stream %d sec old).",
1653 circ->_base.n_circ_id, conn_age);
1654 /* print the circ's path, so people can figure out which circs are
1655 * sucking. */
1656 circuit_log_path(LOG_INFO,LD_APP|LD_CIRC,circ);
1658 /* We have found a suitable circuit for our conn. Hurray. */
1659 return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
1661 } else { /* we're a rendezvous conn */
1662 origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
1664 tor_assert(!conn->cpath_layer);
1666 /* start by finding a rendezvous circuit for us */
1668 retval = circuit_get_open_circ_or_launch(
1669 conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
1670 if (retval < 0) return -1; /* failed */
1672 if (retval > 0) {
1673 tor_assert(rendcirc);
1674 /* one is already established, attach */
1675 log_info(LD_REND,
1676 "rend joined circ %d already here. attaching. "
1677 "(stream %d sec old)",
1678 rendcirc->_base.n_circ_id, conn_age);
1679 /* Mark rendezvous circuits as 'newly dirty' every time you use
1680 * them, since the process of rebuilding a rendezvous circ is so
1681 * expensive. There is a tradeoff between linkability and
1682 * feasibility, at this point.
1684 rendcirc->_base.timestamp_dirty = time(NULL);
1685 link_apconn_to_circ(conn, rendcirc, NULL);
1686 if (connection_ap_handshake_send_begin(conn) < 0)
1687 return 0; /* already marked, let them fade away */
1688 return 1;
1691 if (rendcirc && (rendcirc->_base.purpose ==
1692 CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)) {
1693 log_info(LD_REND,
1694 "pending-join circ %d already here, with intro ack. "
1695 "Stalling. (stream %d sec old)",
1696 rendcirc->_base.n_circ_id, conn_age);
1697 return 0;
1700 /* it's on its way. find an intro circ. */
1701 retval = circuit_get_open_circ_or_launch(
1702 conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
1703 if (retval < 0) return -1; /* failed */
1705 if (retval > 0) {
1706 /* one has already sent the intro. keep waiting. */
1707 circuit_t *c = NULL;
1708 tor_assert(introcirc);
1709 log_info(LD_REND, "Intro circ %d present and awaiting ack (rend %d). "
1710 "Stalling. (stream %d sec old)",
1711 introcirc->_base.n_circ_id,
1712 rendcirc ? rendcirc->_base.n_circ_id : 0,
1713 conn_age);
1714 /* abort parallel intro circs, if any */
1715 for (c = global_circuitlist; c; c = c->next) {
1716 if (c->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
1717 !c->marked_for_close && CIRCUIT_IS_ORIGIN(c)) {
1718 origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(c);
1719 if (oc->rend_data &&
1720 !rend_cmp_service_ids(conn->rend_data->onion_address,
1721 oc->rend_data->onion_address)) {
1722 log_info(LD_REND|LD_CIRC, "Closing introduction circuit that we "
1723 "built in parallel.");
1724 circuit_mark_for_close(c, END_CIRC_REASON_TIMEOUT);
1728 return 0;
1731 /* now rendcirc and introcirc are each either undefined or not finished */
1733 if (rendcirc && introcirc &&
1734 rendcirc->_base.purpose == CIRCUIT_PURPOSE_C_REND_READY) {
1735 log_info(LD_REND,
1736 "ready rend circ %d already here (no intro-ack yet on "
1737 "intro %d). (stream %d sec old)",
1738 rendcirc->_base.n_circ_id,
1739 introcirc->_base.n_circ_id, conn_age);
1741 tor_assert(introcirc->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
1742 if (introcirc->_base.state == CIRCUIT_STATE_OPEN) {
1743 log_info(LD_REND,"found open intro circ %d (rend %d); sending "
1744 "introduction. (stream %d sec old)",
1745 introcirc->_base.n_circ_id, rendcirc->_base.n_circ_id,
1746 conn_age);
1747 switch (rend_client_send_introduction(introcirc, rendcirc)) {
1748 case 0: /* success */
1749 rendcirc->_base.timestamp_dirty = time(NULL);
1750 introcirc->_base.timestamp_dirty = time(NULL);
1751 assert_circuit_ok(TO_CIRCUIT(rendcirc));
1752 assert_circuit_ok(TO_CIRCUIT(introcirc));
1753 return 0;
1754 case -1: /* transient error */
1755 return 0;
1756 case -2: /* permanent error */
1757 return -1;
1758 default: /* oops */
1759 tor_fragile_assert();
1760 return -1;
1765 log_info(LD_REND, "Intro (%d) and rend (%d) circs are not both ready. "
1766 "Stalling conn. (%d sec old)",
1767 introcirc ? introcirc->_base.n_circ_id : 0,
1768 rendcirc ? rendcirc->_base.n_circ_id : 0, conn_age);
1769 return 0;