tor  0.4.1.0-alpha-dev
circuituse.c
Go to the documentation of this file.
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
30 #include "core/or/or.h"
31 #include "app/config/config.h"
33 #include "core/or/channel.h"
34 #include "core/or/circuitbuild.h"
35 #include "core/or/circuitlist.h"
36 #include "core/or/circuitstats.h"
37 #include "core/or/circuituse.h"
38 #include "core/or/circuitpadding.h"
40 #include "core/or/policies.h"
41 #include "feature/client/addressmap.h"
42 #include "feature/client/bridges.h"
43 #include "feature/client/circpathbias.h"
47 #include "feature/hs/hs_circuit.h"
48 #include "feature/hs/hs_client.h"
49 #include "feature/hs/hs_common.h"
50 #include "feature/hs/hs_ident.h"
51 #include "feature/hs/hs_stats.h"
57 #include "feature/relay/selftest.h"
61 #include "feature/stats/predict_ports.h"
62 #include "lib/math/fp.h"
63 #include "lib/time/tvdiff.h"
64 
65 #include "core/or/cpath_build_state_st.h"
66 #include "feature/dircommon/dir_connection_st.h"
67 #include "core/or/entry_connection_st.h"
68 #include "core/or/extend_info_st.h"
69 #include "core/or/or_circuit_st.h"
70 #include "core/or/origin_circuit_st.h"
71 #include "core/or/socks_request_st.h"
72 
74 static void circuit_increment_failure_count(void);
75 
79 static int
81  const origin_circuit_t *origin_circ)
82 {
83  /* Check if this is a v2 rendezvous circ/stream */
84  if ((edge_conn->rend_data && !origin_circ->rend_data) ||
85  (!edge_conn->rend_data && origin_circ->rend_data) ||
86  (edge_conn->rend_data && origin_circ->rend_data &&
87  rend_cmp_service_ids(rend_data_get_address(edge_conn->rend_data),
88  rend_data_get_address(origin_circ->rend_data)))) {
89  /* this circ is not for this conn */
90  return 0;
91  }
92 
93  /* Check if this is a v3 rendezvous circ/stream */
94  if ((edge_conn->hs_ident && !origin_circ->hs_ident) ||
95  (!edge_conn->hs_ident && origin_circ->hs_ident) ||
96  (edge_conn->hs_ident && origin_circ->hs_ident &&
97  !ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
98  &origin_circ->hs_ident->identity_pk))) {
99  /* this circ is not for this conn */
100  return 0;
101  }
102 
103  return 1;
104 }
105 
109 static int
111  const entry_connection_t *conn,
112  int must_be_open, uint8_t purpose,
113  int need_uptime, int need_internal,
114  time_t now)
115 {
116  const circuit_t *circ = TO_CIRCUIT(origin_circ);
117  const node_t *exitnode;
118  cpath_build_state_t *build_state;
119  tor_assert(circ);
120  tor_assert(conn);
121  tor_assert(conn->socks_request);
122 
123  if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_chan))
124  return 0; /* ignore non-open circs */
125  if (circ->marked_for_close)
126  return 0;
127 
128  /* if this circ isn't our purpose, skip. */
129  if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
134  return 0;
135  } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
136  !must_be_open) {
137  if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
139  return 0;
140  } else {
141  if (purpose != circ->purpose)
142  return 0;
143  }
144 
145  /* If this is a timed-out hidden service circuit, skip it. */
146  if (origin_circ->hs_circ_has_timed_out) {
147  return 0;
148  }
149 
150  if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
151  purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
152  purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
153  purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
154  purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
155  if (circ->timestamp_dirty &&
156  circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
157  return 0;
158  }
159 
160  if (origin_circ->unusable_for_new_conns)
161  return 0;
162 
163  /* decide if this circ is suitable for this conn */
164 
165  /* for rend circs, circ->cpath->prev is not the last router in the
166  * circuit, it's the magical extra service hop. so just check the nickname
167  * of the one we meant to finish at.
168  */
169  build_state = origin_circ->build_state;
170  exitnode = build_state_get_exit_node(build_state);
171 
172  if (need_uptime && !build_state->need_uptime)
173  return 0;
174  if (need_internal != build_state->is_internal)
175  return 0;
176 
177  if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
178  purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
179  purpose == CIRCUIT_PURPOSE_C_HSDIR_GET) {
180  tor_addr_t addr;
181  const int family = tor_addr_parse(&addr, conn->socks_request->address);
182  if (!exitnode && !build_state->onehop_tunnel) {
183  log_debug(LD_CIRC,"Not considering circuit with unknown router.");
184  return 0; /* this circuit is screwed and doesn't know it yet,
185  * or is a rendezvous circuit. */
186  }
187  if (build_state->onehop_tunnel) {
188  if (!conn->want_onehop) {
189  log_debug(LD_CIRC,"Skipping one-hop circuit.");
190  return 0;
191  }
193  if (build_state->chosen_exit) {
194  char digest[DIGEST_LEN];
195  if (hexdigest_to_digest(conn->chosen_exit_name, digest) < 0)
196  return 0; /* broken digest, we don't want it */
197  if (tor_memneq(digest, build_state->chosen_exit->identity_digest,
198  DIGEST_LEN))
199  return 0; /* this is a circuit to somewhere else */
200  if (tor_digest_is_zero(digest)) {
201  /* we don't know the digest; have to compare addr:port */
202  if (family < 0 ||
203  !tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
204  build_state->chosen_exit->port != conn->socks_request->port)
205  return 0;
206  }
207  }
208  } else {
209  if (conn->want_onehop) {
210  /* don't use three-hop circuits -- that could hurt our anonymity. */
211  return 0;
212  }
213  }
214  if (origin_circ->prepend_policy && family != -1) {
215  int r = compare_tor_addr_to_addr_policy(&addr,
216  conn->socks_request->port,
217  origin_circ->prepend_policy);
218  if (r == ADDR_POLICY_REJECTED)
219  return 0;
220  }
221  if (exitnode && !connection_ap_can_use_exit(conn, exitnode)) {
222  /* can't exit from this router */
223  return 0;
224  }
225  } else { /* not general: this might be a rend circuit */
226  const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
227  if (!circuit_matches_with_rend_stream(edge_conn, origin_circ)) {
228  return 0;
229  }
230  }
231 
232  if (!connection_edge_compatible_with_circuit(conn, origin_circ)) {
233  /* conn needs to be isolated from other conns that have already used
234  * origin_circ */
235  return 0;
236  }
237 
238  return 1;
239 }
240 
244 static int
246  const entry_connection_t *conn)
247 {
248  const circuit_t *a = TO_CIRCUIT(oa);
249  const circuit_t *b = TO_CIRCUIT(ob);
250  const uint8_t purpose = ENTRY_TO_CONN(conn)->purpose;
251  int a_bits, b_bits;
252 
253  /* If one of the circuits was allowed to live due to relaxing its timeout,
254  * it is definitely worse (it's probably a much slower path). */
255  if (oa->relaxed_timeout && !ob->relaxed_timeout)
256  return 0; /* ob is better. It's not relaxed. */
257  if (!oa->relaxed_timeout && ob->relaxed_timeout)
258  return 1; /* oa is better. It's not relaxed. */
259 
260  switch (purpose) {
264  /* if it's used but less dirty it's best;
265  * else if it's more recently created it's best
266  */
267  if (b->timestamp_dirty) {
268  if (a->timestamp_dirty &&
270  return 1;
271  } else {
272  if (a->timestamp_dirty ||
273  timercmp(&a->timestamp_began, &b->timestamp_began, OP_GT))
274  return 1;
275  if (ob->build_state->is_internal)
276  /* XXXX++ what the heck is this internal thing doing here. I
277  * think we can get rid of it. circuit_is_acceptable() already
278  * makes sure that is_internal is exactly what we need it to
279  * be. -RD */
280  return 1;
281  }
282  break;
284  /* the closer it is to ack_wait the better it is */
285  if (a->purpose > b->purpose)
286  return 1;
287  break;
289  /* the closer it is to rend_joined the better it is */
290  if (a->purpose > b->purpose)
291  return 1;
292  break;
293  }
294 
295  /* XXXX Maybe this check should get a higher priority to avoid
296  * using up circuits too rapidly. */
297 
299  (origin_circuit_t*)oa, 1);
301  (origin_circuit_t*)ob, 1);
302  /* if x_bits < 0, then we have not used x for anything; better not to dirty
303  * a connection if we can help it. */
304  if (a_bits < 0) {
305  return 0;
306  } else if (b_bits < 0) {
307  return 1;
308  }
309  a_bits &= ~ oa->isolation_flags_mixed;
310  a_bits &= ~ ob->isolation_flags_mixed;
311  if (n_bits_set_u8(a_bits) < n_bits_set_u8(b_bits)) {
312  /* The fewer new restrictions we need to make on a circuit for stream
313  * isolation, the better. */
314  return 1;
315  }
316 
317  return 0;
318 }
319 
336 static origin_circuit_t *
338  int must_be_open, uint8_t purpose,
339  int need_uptime, int need_internal)
340 {
341  origin_circuit_t *best=NULL;
342  struct timeval now;
343  int intro_going_on_but_too_old = 0;
344 
345  tor_assert(conn);
346 
348  purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
349  purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
350  purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
352  purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
353 
354  tor_gettimeofday(&now);
355 
356  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
357  origin_circuit_t *origin_circ;
358  if (!CIRCUIT_IS_ORIGIN(circ))
359  continue;
360  origin_circ = TO_ORIGIN_CIRCUIT(circ);
361 
362  /* Log an info message if we're going to launch a new intro circ in
363  * parallel */
364  if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
365  !must_be_open && origin_circ->hs_circ_has_timed_out &&
366  !circ->marked_for_close) {
367  intro_going_on_but_too_old = 1;
368  continue;
369  }
370 
371  if (!circuit_is_acceptable(origin_circ,conn,must_be_open,purpose,
372  need_uptime,need_internal, (time_t)now.tv_sec))
373  continue;
374 
375  /* now this is an acceptable circ to hand back. but that doesn't
376  * mean it's the *best* circ to hand back. try to decide.
377  */
378  if (!best || circuit_is_better(origin_circ,best,conn))
379  best = origin_circ;
380  }
381  SMARTLIST_FOREACH_END(circ);
382 
383  if (!best && intro_going_on_but_too_old)
384  log_info(LD_REND|LD_CIRC, "There is an intro circuit being created "
385  "right now, but it has already taken quite a while. Starting "
386  "one in parallel.");
387 
388  return best;
389 }
390 
392 static int
394 {
395  int count = 0;
396 
397  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
398  if (circ->marked_for_close ||
399  circ->state == CIRCUIT_STATE_OPEN ||
401  !CIRCUIT_IS_ORIGIN(circ))
402  continue;
403 
404  ++count;
405  }
406  SMARTLIST_FOREACH_END(circ);
407 
408  return count;
409 }
410 
411 #if 0
412 
414 /* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
415  * Also, it doesn't have the right definition of an exit circuit. Also,
416  * it's never called. */
417 int
418 circuit_conforms_to_options(const origin_circuit_t *circ,
419  const or_options_t *options)
420 {
421  const crypt_path_t *cpath, *cpath_next = NULL;
422 
423  /* first check if it includes any excluded nodes */
424  for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
425  cpath_next = cpath->next;
427  cpath->extend_info))
428  return 0;
429  }
430 
431  /* then consider the final hop */
433  circ->cpath->prev->extend_info))
434  return 0;
435 
436  return 1;
437 }
438 #endif /* 0 */
439 
452 void
454 {
455  /* circ_times.timeout_ms and circ_times.close_ms are from
456  * circuit_build_times_get_initial_timeout() if we haven't computed
457  * custom timeouts yet */
458  struct timeval general_cutoff, begindir_cutoff, fourhop_cutoff,
459  close_cutoff, extremely_old_cutoff, hs_extremely_old_cutoff,
460  cannibalized_cutoff, c_intro_cutoff, s_intro_cutoff, stream_cutoff;
461  const or_options_t *options = get_options();
462  struct timeval now;
463  cpath_build_state_t *build_state;
464  int any_opened_circs = 0;
465 
466  tor_gettimeofday(&now);
467 
468  /* Check to see if we have any opened circuits. If we don't,
469  * we want to be more lenient with timeouts, in case the
470  * user has relocated and/or changed network connections.
471  * See bug #3443. */
472  any_opened_circs = circuit_any_opened_circuits();
473 
474 #define SET_CUTOFF(target, msec) do { \
475  long ms = tor_lround(msec); \
476  struct timeval diff; \
477  diff.tv_sec = ms / 1000; \
478  diff.tv_usec = (int)((ms % 1000) * 1000); \
479  timersub(&now, &diff, &target); \
480  } while (0)
481 
513  SET_CUTOFF(general_cutoff, get_circuit_build_timeout_ms());
514  SET_CUTOFF(begindir_cutoff, get_circuit_build_timeout_ms());
515 
516  // TODO: We should probably use route_len_for_purpose() here instead,
517  // except that does not count the extra round trip for things like server
518  // intros and rends.
519 
520  /* > 3hop circs seem to have a 1.0 second delay on their cannibalized
521  * 4th hop. */
522  SET_CUTOFF(fourhop_cutoff, get_circuit_build_timeout_ms() * (10/6.0) + 1000);
523 
524  /* CIRCUIT_PURPOSE_C_ESTABLISH_REND behaves more like a RELAY cell.
525  * Use the stream cutoff (more or less). */
526  SET_CUTOFF(stream_cutoff, MAX(options->CircuitStreamTimeout,15)*1000 + 1000);
527 
528  /* Be lenient with cannibalized circs. They already survived the official
529  * CBT, and they're usually not performance-critical. */
530  SET_CUTOFF(cannibalized_cutoff,
532  options->CircuitStreamTimeout * 1000) + 1000);
533 
534  /* Intro circs have an extra round trip (and are also 4 hops long) */
535  SET_CUTOFF(c_intro_cutoff, get_circuit_build_timeout_ms() * (14/6.0) + 1000);
536 
537  /* Server intro circs have an extra round trip */
538  SET_CUTOFF(s_intro_cutoff, get_circuit_build_timeout_ms() * (9/6.0) + 1000);
539 
540  SET_CUTOFF(close_cutoff, get_circuit_build_close_time_ms());
541  SET_CUTOFF(extremely_old_cutoff, get_circuit_build_close_time_ms()*2 + 1000);
542 
543  SET_CUTOFF(hs_extremely_old_cutoff,
545  options->SocksTimeout * 1000));
546 
547  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *,victim) {
548  struct timeval cutoff;
549  bool fixed_time = circuit_build_times_disabled(get_options());
550 
551  if (!CIRCUIT_IS_ORIGIN(victim) || /* didn't originate here */
552  victim->marked_for_close) /* don't mess with marked circs */
553  continue;
554 
555  /* If we haven't yet started the first hop, it means we don't have
556  * any orconns available, and thus have not started counting time yet
557  * for this circuit. See circuit_deliver_create_cell() and uses of
558  * timestamp_began.
559  *
560  * Continue to wait in this case. The ORConn should timeout
561  * independently and kill us then.
562  */
563  if (TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_CLOSED) {
564  continue;
565  }
566 
567  build_state = TO_ORIGIN_CIRCUIT(victim)->build_state;
568  if (build_state && build_state->onehop_tunnel)
569  cutoff = begindir_cutoff;
570  else if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
571  cutoff = close_cutoff;
572  else if (victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
573  cutoff = c_intro_cutoff;
574  else if (victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
575  cutoff = s_intro_cutoff;
576  else if (victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND)
577  cutoff = stream_cutoff;
578  else if (victim->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
579  cutoff = close_cutoff;
580  else if (TO_ORIGIN_CIRCUIT(victim)->has_opened &&
581  victim->state != CIRCUIT_STATE_OPEN)
582  cutoff = cannibalized_cutoff;
583  else if (build_state && build_state->desired_path_len >= 4)
584  cutoff = fourhop_cutoff;
585  else
586  cutoff = general_cutoff;
587 
588  if (TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)
589  cutoff = hs_extremely_old_cutoff;
590 
591  if (timercmp(&victim->timestamp_began, &cutoff, OP_GT))
592  continue; /* it's still young, leave it alone */
593 
594  /* We need to double-check the opened state here because
595  * we don't want to consider opened 1-hop dircon circuits for
596  * deciding when to relax the timeout, but we *do* want to relax
597  * those circuits too if nothing else is opened *and* they still
598  * aren't either. */
599  if (!any_opened_circs && victim->state != CIRCUIT_STATE_OPEN) {
600  /* It's still young enough that we wouldn't close it, right? */
601  if (timercmp(&victim->timestamp_began, &close_cutoff, OP_GT)) {
602  if (!TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout) {
603  int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath->state
604  == CPATH_STATE_OPEN;
605  if (!fixed_time) {
606  log_info(LD_CIRC,
607  "No circuits are opened. Relaxing timeout for circuit %d "
608  "(a %s %d-hop circuit in state %s with channel state %s).",
609  TO_ORIGIN_CIRCUIT(victim)->global_identifier,
610  circuit_purpose_to_string(victim->purpose),
611  TO_ORIGIN_CIRCUIT(victim)->build_state ?
612  TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
613  -1,
614  circuit_state_to_string(victim->state),
615  victim->n_chan ?
616  channel_state_to_string(victim->n_chan->state) : "none");
617  }
618 
619  /* We count the timeout here for CBT, because technically this
620  * was a timeout, and the timeout value needs to reset if we
621  * see enough of them. Note this means we also need to avoid
622  * double-counting below, too. */
624  first_hop_succeeded);
625  TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout = 1;
626  }
627  continue;
628  } else {
629  static ratelim_t relax_timeout_limit = RATELIM_INIT(3600);
630  const double build_close_ms = get_circuit_build_close_time_ms();
631  if (!fixed_time) {
632  log_fn_ratelim(&relax_timeout_limit, LOG_NOTICE, LD_CIRC,
633  "No circuits are opened. Relaxed timeout for circuit %d "
634  "(a %s %d-hop circuit in state %s with channel state %s) to "
635  "%ldms. However, it appears the circuit has timed out "
636  "anyway.",
637  TO_ORIGIN_CIRCUIT(victim)->global_identifier,
638  circuit_purpose_to_string(victim->purpose),
639  TO_ORIGIN_CIRCUIT(victim)->build_state ?
640  TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
641  -1,
642  circuit_state_to_string(victim->state),
643  victim->n_chan ?
644  channel_state_to_string(victim->n_chan->state) : "none",
645  (long)build_close_ms);
646  }
647  }
648  }
649 
650 #if 0
651  /* some debug logs, to help track bugs */
652  if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
653  victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
654  if (!victim->timestamp_dirty)
655  log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d)."
656  "(clean).",
657  victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
658  victim->purpose, victim->build_state->chosen_exit_name,
659  victim->n_circ_id);
660  else
661  log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d). "
662  "%d secs since dirty.",
663  victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
664  victim->purpose, victim->build_state->chosen_exit_name,
665  victim->n_circ_id,
666  (int)(now - victim->timestamp_dirty));
667  }
668 #endif /* 0 */
669 
670  /* if circ is !open, or if it's open but purpose is a non-finished
671  * intro or rend, then mark it for close */
672  if (victim->state == CIRCUIT_STATE_OPEN) {
673  switch (victim->purpose) {
674  default: /* most open circuits can be left alone. */
675  continue; /* yes, continue inside a switch refers to the nearest
676  * enclosing loop. C is smart. */
678  break; /* too old, need to die */
680  /* it's a rend_ready circ -- has it already picked a query? */
681  /* c_rend_ready circs measure age since timestamp_dirty,
682  * because that's set when they switch purposes
683  */
684  if (TO_ORIGIN_CIRCUIT(victim)->rend_data ||
685  TO_ORIGIN_CIRCUIT(victim)->hs_ident ||
686  victim->timestamp_dirty > cutoff.tv_sec)
687  continue;
688  break;
690  /* Open path bias testing circuits are given a long
691  * time to complete the test, but not forever */
692  TO_ORIGIN_CIRCUIT(victim)->path_state = PATH_STATE_USE_FAILED;
693  break;
695  /* That purpose means that the intro point circuit has been opened
696  * successfully but the INTRODUCE1 cell hasn't been sent yet because
697  * the client is waiting for the rendezvous point circuit to open.
698  * Keep this circuit open while waiting for the rendezvous circuit.
699  * We let the circuit idle timeout take care of cleaning this
700  * circuit if it never used. */
701  continue;
705  /* rend and intro circs become dirty each time they
706  * make an introduction attempt. so timestamp_dirty
707  * will reflect the time since the last attempt.
708  */
709  if (victim->timestamp_dirty > cutoff.tv_sec)
710  continue;
711  break;
712  }
713  } else { /* circuit not open, consider recording failure as timeout */
714  int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath &&
715  TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_OPEN;
716 
717  if (TO_ORIGIN_CIRCUIT(victim)->p_streams != NULL) {
718  log_warn(LD_BUG, "Circuit %d (purpose %d, %s) has timed out, "
719  "yet has attached streams!",
720  TO_ORIGIN_CIRCUIT(victim)->global_identifier,
721  victim->purpose,
722  circuit_purpose_to_string(victim->purpose));
724  continue;
725  }
726 
729 
730  log_info(LD_CIRC,
731  "Deciding to count the timeout for circuit %"PRIu32"\n",
732  TO_ORIGIN_CIRCUIT(victim)->global_identifier);
733 
734  /* Circuits are allowed to last longer for measurement.
735  * Switch their purpose and wait. */
736  if (victim->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
738  victim));
739  continue;
740  }
741 
742  /*
743  * If the circuit build time is much greater than we would have cut
744  * it off at, we probably had a suspend event along this codepath,
745  * and we should discard the value.
746  */
747  if (timercmp(&victim->timestamp_began, &extremely_old_cutoff, OP_LT)) {
748  log_notice(LD_CIRC,
749  "Extremely large value for circuit build timeout: %lds. "
750  "Assuming clock jump. Purpose %d (%s)",
751  (long)(now.tv_sec - victim->timestamp_began.tv_sec),
752  victim->purpose,
753  circuit_purpose_to_string(victim->purpose));
756  first_hop_succeeded,
757  (time_t)victim->timestamp_created.tv_sec)) {
759  }
760  }
761  }
762 
763  /* If this is a hidden service client circuit which is far enough along in
764  * connecting to its destination, and we haven't already flagged it as
765  * 'timed out', flag it so we'll launch another intro or rend circ, but
766  * don't mark it for close yet.
767  *
768  * (Circs flagged as 'timed out' are given a much longer timeout
769  * period above, so we won't close them in the next call to
770  * circuit_expire_building.) */
771  if (!(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)) {
772  switch (victim->purpose) {
774  /* We only want to spare a rend circ if it has been specified in
775  * an INTRODUCE1 cell sent to a hidden service. A circ's
776  * pending_final_cpath field is non-NULL iff it is a rend circ
777  * and we have tried to send an INTRODUCE1 cell specifying it.
778  * Thus, if the pending_final_cpath field *is* NULL, then we
779  * want to not spare it. */
780  if (TO_ORIGIN_CIRCUIT(victim)->build_state &&
781  TO_ORIGIN_CIRCUIT(victim)->build_state->pending_final_cpath ==
782  NULL)
783  break;
784  /* fallthrough! */
787  /* If we have reached this line, we want to spare the circ for now. */
788  log_info(LD_CIRC,"Marking circ %u (state %d:%s, purpose %d) "
789  "as timed-out HS circ",
790  (unsigned)victim->n_circ_id,
791  victim->state, circuit_state_to_string(victim->state),
792  victim->purpose);
794  continue;
795  default:
796  break;
797  }
798  }
799 
800  /* If this is a service-side rendezvous circuit which is far
801  * enough along in connecting to its destination, consider sparing
802  * it. */
803  if (!(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out) &&
804  victim->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
805  log_info(LD_CIRC,"Marking circ %u (state %d:%s, purpose %d) "
806  "as timed-out HS circ; relaunching rendezvous attempt.",
807  (unsigned)victim->n_circ_id,
808  victim->state, circuit_state_to_string(victim->state),
809  victim->purpose);
811  hs_circ_retry_service_rendezvous_point(TO_ORIGIN_CIRCUIT(victim));
812  continue;
813  }
814 
815  if (victim->n_chan)
816  log_info(LD_CIRC,
817  "Abandoning circ %u %s:%u (state %d,%d:%s, purpose %d, "
818  "len %d)", TO_ORIGIN_CIRCUIT(victim)->global_identifier,
819  channel_get_canonical_remote_descr(victim->n_chan),
820  (unsigned)victim->n_circ_id,
821  TO_ORIGIN_CIRCUIT(victim)->has_opened,
822  victim->state, circuit_state_to_string(victim->state),
823  victim->purpose,
824  TO_ORIGIN_CIRCUIT(victim)->build_state ?
825  TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
826  -1);
827  else
828  log_info(LD_CIRC,
829  "Abandoning circ %u %u (state %d,%d:%s, purpose %d, len %d)",
830  TO_ORIGIN_CIRCUIT(victim)->global_identifier,
831  (unsigned)victim->n_circ_id,
832  TO_ORIGIN_CIRCUIT(victim)->has_opened,
833  victim->state,
834  circuit_state_to_string(victim->state), victim->purpose,
835  TO_ORIGIN_CIRCUIT(victim)->build_state ?
836  TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
837  -1);
838 
840  if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
841  circuit_mark_for_close(victim, END_CIRC_REASON_MEASUREMENT_EXPIRED);
842  else
843  circuit_mark_for_close(victim, END_CIRC_REASON_TIMEOUT);
844 
846  } SMARTLIST_FOREACH_END(victim);
847 }
848 
854 void
856 {
858  origin_circuit_t *, circ) {
859  if (TO_CIRCUIT(circ)->marked_for_close)
860  continue;
861  if (circ->guard_state == NULL)
862  continue;
864  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NONE);
865  } SMARTLIST_FOREACH_END(circ);
866 }
867 
871 
878 void
880 {
881 #define MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG 10
882  time_t now = time(NULL);
883  time_t cutoff = now - age;
884  int n_found = 0;
885  smartlist_t *log_these = smartlist_new();
886  const or_options_t *options = get_options();
887 
888  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
889  const origin_circuit_t *ocirc;
890  if (! CIRCUIT_IS_ORIGIN(circ))
891  continue;
892  if (circ->timestamp_created.tv_sec >= cutoff)
893  continue;
894  /* Single Onion Services deliberately make long term one-hop intro
895  * and rendezvous connections. Don't log the established ones. */
896  if (rend_service_allow_non_anonymous_connection(options) &&
897  (circ->purpose == CIRCUIT_PURPOSE_S_INTRO ||
898  circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED))
899  continue;
900  ocirc = CONST_TO_ORIGIN_CIRCUIT(circ);
901 
902  if (ocirc->build_state && ocirc->build_state->onehop_tunnel) {
903  ++n_found;
904 
905  if (smartlist_len(log_these) < MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG)
906  smartlist_add(log_these, (origin_circuit_t*) ocirc);
907  }
908  }
909  SMARTLIST_FOREACH_END(circ);
910 
911  if (n_found == 0)
912  goto done;
913 
914  log_notice(LD_HEARTBEAT,
915  "Diagnostic for issue 8387: Found %d one-hop circuits more "
916  "than %d seconds old! Logging %d...",
917  n_found, age, smartlist_len(log_these));
918 
919  SMARTLIST_FOREACH_BEGIN(log_these, const origin_circuit_t *, ocirc) {
920  char created[ISO_TIME_LEN+1];
921  int stream_num;
922  const edge_connection_t *conn;
923  char *dirty = NULL;
924  const circuit_t *circ = TO_CIRCUIT(ocirc);
925 
926  format_local_iso_time(created,
927  (time_t)circ->timestamp_created.tv_sec);
928 
929  if (circ->timestamp_dirty) {
930  char dirty_since[ISO_TIME_LEN+1];
931  format_local_iso_time(dirty_since, circ->timestamp_dirty);
932 
933  tor_asprintf(&dirty, "Dirty since %s (%ld seconds vs %ld-second cutoff)",
934  dirty_since, (long)(now - circ->timestamp_dirty),
935  (long) options->MaxCircuitDirtiness);
936  } else {
937  dirty = tor_strdup("Not marked dirty");
938  }
939 
940  log_notice(LD_HEARTBEAT, " #%d created at %s. %s, %s. %s for close. "
941  "Package window: %d. "
942  "%s for new conns. %s.",
943  ocirc_sl_idx,
944  created,
947  circ->marked_for_close ? "Marked" : "Not marked",
948  circ->package_window,
949  ocirc->unusable_for_new_conns ? "Not usable" : "usable",
950  dirty);
951  tor_free(dirty);
952 
953  stream_num = 0;
954  for (conn = ocirc->p_streams; conn; conn = conn->next_stream) {
955  const connection_t *c = TO_CONN(conn);
956  char stream_created[ISO_TIME_LEN+1];
957  if (++stream_num >= 5)
958  break;
959 
960  format_local_iso_time(stream_created, c->timestamp_created);
961 
962  log_notice(LD_HEARTBEAT, " Stream#%d created at %s. "
963  "%s conn in state %s. "
964  "It is %slinked and %sreading from a linked connection %p. "
965  "Package window %d. "
966  "%s for close (%s:%d). Hold-open is %sset. "
967  "Has %ssent RELAY_END. %s on circuit.",
968  stream_num,
969  stream_created,
972  c->linked ? "" : "not ",
973  c->reading_from_linked_conn ? "": "not",
974  c->linked_conn,
975  conn->package_window,
976  c->marked_for_close ? "Marked" : "Not marked",
978  c->marked_for_close,
979  c->hold_open_until_flushed ? "" : "not ",
980  conn->edge_has_sent_end ? "" : "not ",
981  conn->edge_blocked_on_circ ? "Blocked" : "Not blocked");
982  if (! c->linked_conn)
983  continue;
984 
985  c = c->linked_conn;
986 
987  log_notice(LD_HEARTBEAT, " Linked to %s connection in state %s "
988  "(Purpose %d). %s for close (%s:%d). Hold-open is %sset. ",
991  c->purpose,
992  c->marked_for_close ? "Marked" : "Not marked",
994  c->marked_for_close,
995  c->hold_open_until_flushed ? "" : "not ");
996  }
997  } SMARTLIST_FOREACH_END(ocirc);
998 
999  log_notice(LD_HEARTBEAT, "It has been %ld seconds since I last called "
1000  "circuit_expire_old_circuits_clientside().",
1001  (long)(now - last_expired_clientside_circuits));
1002 
1003  done:
1004  smartlist_free(log_these);
1005 }
1006 
1010 void
1012 {
1013  int i;
1014  uint16_t *port;
1015 
1016  for (i = 0; i < smartlist_len(needed_ports); ++i) {
1017  port = smartlist_get(needed_ports, i);
1018  tor_assert(*port);
1019  if (circuit_stream_is_being_handled(NULL, *port,
1021  log_debug(LD_CIRC,"Port %d is already being handled; removing.", *port);
1022  smartlist_del(needed_ports, i--);
1023  tor_free(port);
1024  } else {
1025  log_debug(LD_CIRC,"Port %d is not handled.", *port);
1026  }
1027  }
1028 }
1029 
1035 int
1037  uint16_t port, int min)
1038 {
1039  const node_t *exitnode;
1040  int num=0;
1041  time_t now = time(NULL);
1042  int need_uptime = smartlist_contains_int_as_string(
1043  get_options()->LongLivedPorts,
1044  conn ? conn->socks_request->port : port);
1045 
1046  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
1047  if (CIRCUIT_IS_ORIGIN(circ) &&
1048  !circ->marked_for_close &&
1049  circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
1050  (!circ->timestamp_dirty ||
1051  circ->timestamp_dirty + get_options()->MaxCircuitDirtiness > now)) {
1052  origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
1053  cpath_build_state_t *build_state = origin_circ->build_state;
1054  if (build_state->is_internal || build_state->onehop_tunnel)
1055  continue;
1056  if (origin_circ->unusable_for_new_conns)
1057  continue;
1058  if (origin_circ->isolation_values_set &&
1059  (conn == NULL ||
1060  !connection_edge_compatible_with_circuit(conn, origin_circ)))
1061  continue;
1062 
1063  exitnode = build_state_get_exit_node(build_state);
1064  if (exitnode && (!need_uptime || build_state->need_uptime)) {
1065  int ok;
1066  if (conn) {
1067  ok = connection_ap_can_use_exit(conn, exitnode);
1068  } else {
1070  r = compare_tor_addr_to_node_policy(NULL, port, exitnode);
1072  }
1073  if (ok) {
1074  if (++num >= min)
1075  return 1;
1076  }
1077  }
1078  }
1079  }
1080  SMARTLIST_FOREACH_END(circ);
1081  return 0;
1082 }
1083 
1085 #define MAX_UNUSED_OPEN_CIRCUITS 14
1086 
1087 /* Return true if a circuit is available for use, meaning that it is open,
1088  * clean, usable for new multi-hop connections, and a general purpose origin
1089  * circuit.
1090  * Accept any kind of circuit, return false if the above conditions are not
1091  * met. */
1092 STATIC int
1093 circuit_is_available_for_use(const circuit_t *circ)
1094 {
1095  const origin_circuit_t *origin_circ;
1096  cpath_build_state_t *build_state;
1097 
1098  if (!CIRCUIT_IS_ORIGIN(circ))
1099  return 0; /* We first filter out only origin circuits before doing the
1100  following checks. */
1101  if (circ->marked_for_close)
1102  return 0; /* Don't mess with marked circs */
1103  if (circ->timestamp_dirty)
1104  return 0; /* Only count clean circs */
1105  if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL &&
1107  return 0; /* We only pay attention to general purpose circuits.
1108  General purpose circuits are always origin circuits. */
1109 
1110  origin_circ = CONST_TO_ORIGIN_CIRCUIT(circ);
1111  if (origin_circ->unusable_for_new_conns)
1112  return 0;
1113 
1114  build_state = origin_circ->build_state;
1115  if (build_state->onehop_tunnel)
1116  return 0;
1117 
1118  return 1;
1119 }
1120 
1121 /* Return true if we need any more exit circuits.
1122  * needs_uptime and needs_capacity are set only if we need more exit circuits.
1123  * Check if we know of a port that's been requested recently and no circuit
1124  * is currently available that can handle it. */
1125 STATIC int
1126 needs_exit_circuits(time_t now, int *needs_uptime, int *needs_capacity)
1127 {
1128  return (!circuit_all_predicted_ports_handled(now, needs_uptime,
1129  needs_capacity) &&
1130  router_have_consensus_path() == CONSENSUS_PATH_EXIT);
1131 }
1132 
1133 /* Hidden services need at least this many internal circuits */
1134 #define SUFFICIENT_UPTIME_INTERNAL_HS_SERVERS 3
1135 
1136 /* Return true if we need any more hidden service server circuits.
1137  * HS servers only need an internal circuit. */
1138 STATIC int
1139 needs_hs_server_circuits(time_t now, int num_uptime_internal)
1140 {
1141  if (!rend_num_services() && !hs_service_get_num_services()) {
1142  /* No services, we don't need anything. */
1143  goto no_need;
1144  }
1145 
1146  if (num_uptime_internal >= SUFFICIENT_UPTIME_INTERNAL_HS_SERVERS) {
1147  /* We have sufficient amount of internal circuit. */
1148  goto no_need;
1149  }
1150 
1151  if (router_have_consensus_path() == CONSENSUS_PATH_UNKNOWN) {
1152  /* Consensus hasn't been checked or might be invalid so requesting
1153  * internal circuits is not wise. */
1154  goto no_need;
1155  }
1156 
1157  /* At this point, we need a certain amount of circuits and we will most
1158  * likely use them for rendezvous so we note down the use of internal
1159  * circuit for our prediction for circuit needing uptime and capacity. */
1160  rep_hist_note_used_internal(now, 1, 1);
1161 
1162  return 1;
1163  no_need:
1164  return 0;
1165 }
1166 
1167 /* We need at least this many internal circuits for hidden service clients */
1168 #define SUFFICIENT_INTERNAL_HS_CLIENTS 3
1169 
1170 /* We need at least this much uptime for internal circuits for hidden service
1171  * clients */
1172 #define SUFFICIENT_UPTIME_INTERNAL_HS_CLIENTS 2
1173 
1174 /* Return true if we need any more hidden service client circuits.
1175  * HS clients only need an internal circuit. */
1176 STATIC int
1177 needs_hs_client_circuits(time_t now, int *needs_uptime, int *needs_capacity,
1178  int num_internal, int num_uptime_internal)
1179 {
1180  int used_internal_recently = rep_hist_get_predicted_internal(now,
1181  needs_uptime,
1182  needs_capacity);
1183  int requires_uptime = num_uptime_internal <
1184  SUFFICIENT_UPTIME_INTERNAL_HS_CLIENTS &&
1185  needs_uptime;
1186 
1187  return (used_internal_recently &&
1188  (requires_uptime || num_internal < SUFFICIENT_INTERNAL_HS_CLIENTS) &&
1189  router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN);
1190 }
1191 
1192 /* This is how many circuits can be opened concurrently during the cbt learning
1193  * phase. This number cannot exceed the tor-wide MAX_UNUSED_OPEN_CIRCUITS. */
1194 #define DFLT_CBT_UNUSED_OPEN_CIRCS (10)
1195 #define MIN_CBT_UNUSED_OPEN_CIRCS 0
1196 #define MAX_CBT_UNUSED_OPEN_CIRCS MAX_UNUSED_OPEN_CIRCUITS
1197 
1198 /* Return true if we need more circuits for a good build timeout.
1199  * XXXX make the assumption that build timeout streams should be
1200  * created whenever we can build internal circuits. */
1201 STATIC int
1202 needs_circuits_for_build(int num)
1203 {
1204  if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN) {
1205  if (num < networkstatus_get_param(NULL, "cbtmaxopencircs",
1206  DFLT_CBT_UNUSED_OPEN_CIRCS,
1207  MIN_CBT_UNUSED_OPEN_CIRCS,
1208  MAX_CBT_UNUSED_OPEN_CIRCS) &&
1209  !circuit_build_times_disabled(get_options()) &&
1211  return 1;
1212  }
1213  }
1214  return 0;
1215 }
1216 
1221 static void
1223 {
1224  /* K.I.S.S. implementation of bug #23101: If we are using
1225  * vanguards or pinned middles, pre-build a specific purpose
1226  * for HS circs. */
1229  } else {
1230  /* If no vanguards, then no HS-specific prebuilt circuits are needed.
1231  * Normal GENERAL circs are fine */
1233  }
1234 }
1235 
1240 static void
1242 {
1243  int num=0, num_internal=0, num_uptime_internal=0;
1244  int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
1245  int port_needs_uptime=0, port_needs_capacity=1;
1246  time_t now = time(NULL);
1247  int flags = 0;
1248 
1249  /* Count how many of each type of circuit we currently have. */
1250  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
1251  if (!circuit_is_available_for_use(circ))
1252  continue;
1253 
1254  num++;
1255 
1256  cpath_build_state_t *build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
1257  if (build_state->is_internal)
1258  num_internal++;
1259  if (build_state->need_uptime && build_state->is_internal)
1260  num_uptime_internal++;
1261  }
1262  SMARTLIST_FOREACH_END(circ);
1263 
1264  /* If that's enough, then stop now. */
1265  if (num >= MAX_UNUSED_OPEN_CIRCUITS)
1266  return;
1267 
1268  if (needs_exit_circuits(now, &port_needs_uptime, &port_needs_capacity)) {
1269  if (port_needs_uptime)
1270  flags |= CIRCLAUNCH_NEED_UPTIME;
1271  if (port_needs_capacity)
1272  flags |= CIRCLAUNCH_NEED_CAPACITY;
1273 
1274  log_info(LD_CIRC,
1275  "Have %d clean circs (%d internal), need another exit circ.",
1276  num, num_internal);
1278  return;
1279  }
1280 
1281  if (needs_hs_server_circuits(now, num_uptime_internal)) {
1284 
1285  log_info(LD_CIRC,
1286  "Have %d clean circs (%d internal), need another internal "
1287  "circ for my hidden service.",
1288  num, num_internal);
1290  return;
1291  }
1292 
1293  if (needs_hs_client_circuits(now, &hidserv_needs_uptime,
1294  &hidserv_needs_capacity,
1295  num_internal, num_uptime_internal))
1296  {
1297  if (hidserv_needs_uptime)
1298  flags |= CIRCLAUNCH_NEED_UPTIME;
1299  if (hidserv_needs_capacity)
1300  flags |= CIRCLAUNCH_NEED_CAPACITY;
1301  flags |= CIRCLAUNCH_IS_INTERNAL;
1302 
1303  log_info(LD_CIRC,
1304  "Have %d clean circs (%d uptime-internal, %d internal), need"
1305  " another hidden service circ.",
1306  num, num_uptime_internal, num_internal);
1307 
1309  return;
1310  }
1311 
1312  if (needs_circuits_for_build(num)) {
1313  flags = CIRCLAUNCH_NEED_CAPACITY;
1314  /* if there are no exits in the consensus, make timeout
1315  * circuits internal */
1316  if (router_have_consensus_path() == CONSENSUS_PATH_INTERNAL)
1317  flags |= CIRCLAUNCH_IS_INTERNAL;
1318 
1319  log_info(LD_CIRC,
1320  "Have %d clean circs need another buildtime test circ.", num);
1322  return;
1323  }
1324 }
1325 
1327 #define TESTING_CIRCUIT_INTERVAL 300
1328 
1334 void
1336 {
1337  const or_options_t *options = get_options();
1338 
1339  /* launch a new circ for any pending streams that need one
1340  * XXXX make the assumption that (some) AP streams (i.e. HS clients)
1341  * don't require an exit circuit, review in #13814.
1342  * This allows HSs to function in a consensus without exits. */
1343  if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN)
1345 
1347 
1348  if (!options->DisablePredictedCircuits)
1350 }
1351 
1357 void
1359 {
1360  static time_t time_to_expire_and_reset = 0;
1361 
1362  if (time_to_expire_and_reset < now) {
1364  time_to_expire_and_reset = now + get_options()->NewCircuitPeriod;
1365  if (proxy_mode(get_options()))
1366  addressmap_clean(now);
1368 
1369 #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
1370 
1371  /* If we ever re-enable, this has to move into
1372  * circuit_build_needed_circs */
1373 
1374  circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
1375  if (get_options()->RunTesting &&
1376  circ &&
1377  circ->timestamp_began.tv_sec + TESTING_CIRCUIT_INTERVAL < now) {
1378  log_fn(LOG_INFO,"Creating a new testing circuit.");
1380  }
1381 #endif /* 0 */
1382  }
1383 }
1384 
1388 void
1390 {
1391  edge_connection_t *prevconn;
1392 
1393  tor_assert(circ);
1394  tor_assert(conn);
1395 
1396  if (conn->base_.type == CONN_TYPE_AP) {
1397  entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1398  entry_conn->may_use_optimistic_data = 0;
1399  }
1400  conn->cpath_layer = NULL; /* don't keep a stale pointer */
1401  conn->on_circuit = NULL;
1402 
1403  if (CIRCUIT_IS_ORIGIN(circ)) {
1404  origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
1405  int removed = 0;
1406  if (conn == origin_circ->p_streams) {
1407  origin_circ->p_streams = conn->next_stream;
1408  removed = 1;
1409  } else {
1410  for (prevconn = origin_circ->p_streams;
1411  prevconn && prevconn->next_stream && prevconn->next_stream != conn;
1412  prevconn = prevconn->next_stream)
1413  ;
1414  if (prevconn && prevconn->next_stream) {
1415  prevconn->next_stream = conn->next_stream;
1416  removed = 1;
1417  }
1418  }
1419  if (removed) {
1420  log_debug(LD_APP, "Removing stream %d from circ %u",
1421  conn->stream_id, (unsigned)circ->n_circ_id);
1422 
1423  /* If the stream was removed, and it was a rend stream, decrement the
1424  * number of streams on the circuit associated with the rend service.
1425  */
1426  if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED) {
1427  hs_dec_rdv_stream_counter(origin_circ);
1428  }
1429 
1430  /* If there are no more streams on this circ, tell circpad */
1431  if (!origin_circ->p_streams)
1433 
1434  return;
1435  }
1436  } else {
1437  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1438  if (conn == or_circ->n_streams) {
1439  or_circ->n_streams = conn->next_stream;
1440  return;
1441  }
1442  if (conn == or_circ->resolving_streams) {
1443  or_circ->resolving_streams = conn->next_stream;
1444  return;
1445  }
1446 
1447  for (prevconn = or_circ->n_streams;
1448  prevconn && prevconn->next_stream && prevconn->next_stream != conn;
1449  prevconn = prevconn->next_stream)
1450  ;
1451  if (prevconn && prevconn->next_stream) {
1452  prevconn->next_stream = conn->next_stream;
1453  return;
1454  }
1455 
1456  for (prevconn = or_circ->resolving_streams;
1457  prevconn && prevconn->next_stream && prevconn->next_stream != conn;
1458  prevconn = prevconn->next_stream)
1459  ;
1460  if (prevconn && prevconn->next_stream) {
1461  prevconn->next_stream = conn->next_stream;
1462  return;
1463  }
1464  }
1465 
1466  log_warn(LD_BUG,"Edge connection not in circuit's list.");
1467  /* Don't give an error here; it's harmless. */
1469 }
1470 
1474 static void
1476 {
1477  struct timeval cutoff, now;
1478 
1479  tor_gettimeofday(&now);
1480  last_expired_clientside_circuits = now.tv_sec;
1481 
1482  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
1483  if (circ->marked_for_close || !CIRCUIT_IS_ORIGIN(circ))
1484  continue;
1485 
1486  cutoff = now;
1487  cutoff.tv_sec -= TO_ORIGIN_CIRCUIT(circ)->circuit_idle_timeout;
1488 
1489  /* If the circuit has been dirty for too long, and there are no streams
1490  * on it, mark it for close.
1491  */
1492  if (circ->timestamp_dirty &&
1493  circ->timestamp_dirty + get_options()->MaxCircuitDirtiness <
1494  now.tv_sec &&
1495  !TO_ORIGIN_CIRCUIT(circ)->p_streams /* nothing attached */ ) {
1496  log_debug(LD_CIRC, "Closing n_circ_id %u (dirty %ld sec ago, "
1497  "purpose %d)",
1498  (unsigned)circ->n_circ_id,
1499  (long)(now.tv_sec - circ->timestamp_dirty),
1500  circ->purpose);
1501  /* Don't do this magic for testing circuits. Their death is governed
1502  * by circuit_expire_building */
1503  if (circ->purpose != CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
1504  circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1505  } else if (!circ->timestamp_dirty && circ->state == CIRCUIT_STATE_OPEN) {
1506  if (timercmp(&circ->timestamp_began, &cutoff, OP_LT)) {
1507  if (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
1508  circ->purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
1509  circ->purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
1510  circ->purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
1511  circ->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT ||
1512  circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
1513  circ->purpose == CIRCUIT_PURPOSE_TESTING ||
1514  (circ->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
1515  circ->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) ||
1516  circ->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
1517  log_info(LD_CIRC,
1518  "Closing circuit %"PRIu32
1519  " that has been unused for %ld msec.",
1520  TO_ORIGIN_CIRCUIT(circ)->global_identifier,
1521  tv_mdiff(&circ->timestamp_began, &now));
1522  circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1523  } else if (!TO_ORIGIN_CIRCUIT(circ)->is_ancient) {
1524  /* Server-side rend joined circuits can end up really old, because
1525  * they are reused by clients for longer than normal. The client
1526  * controls their lifespan. (They never become dirty, because
1527  * connection_exit_begin_conn() never marks anything as dirty.)
1528  * Similarly, server-side intro circuits last a long time. */
1529  if (circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED &&
1530  circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
1531  log_notice(LD_CIRC,
1532  "Ancient non-dirty circuit %d is still around after "
1533  "%ld milliseconds. Purpose: %d (%s)",
1534  TO_ORIGIN_CIRCUIT(circ)->global_identifier,
1535  tv_mdiff(&circ->timestamp_began, &now),
1536  circ->purpose,
1537  circuit_purpose_to_string(circ->purpose));
1538  TO_ORIGIN_CIRCUIT(circ)->is_ancient = 1;
1539  }
1540  }
1541  }
1542  }
1543  } SMARTLIST_FOREACH_END(circ);
1544 }
1545 
1561 #define IDLE_ONE_HOP_CIRC_TIMEOUT 60
1562 
1567 void
1569 {
1570  or_circuit_t *or_circ;
1571  time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;
1572 
1573  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
1574  if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
1575  continue;
1576  or_circ = TO_OR_CIRCUIT(circ);
1577  /* If the circuit has been idle for too long, and there are no streams
1578  * on it, and it ends here, and it used a create_fast, mark it for close.
1579  *
1580  * Also if there is a rend_splice on it, it's a single onion service
1581  * circuit and we should not close it.
1582  */
1583  if (or_circ->p_chan && channel_is_client(or_circ->p_chan) &&
1584  !circ->n_chan &&
1585  !or_circ->n_streams && !or_circ->resolving_streams &&
1586  !or_circ->rend_splice &&
1587  channel_when_last_xmit(or_circ->p_chan) <= cutoff) {
1588  log_info(LD_CIRC, "Closing circ_id %u (empty %d secs ago)",
1589  (unsigned)or_circ->p_circ_id,
1590  (int)(now - channel_when_last_xmit(or_circ->p_chan)));
1591  circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1592  }
1593  }
1594  SMARTLIST_FOREACH_END(circ);
1595 }
1596 
1598 #define NUM_PARALLEL_TESTING_CIRCS 4
1599 
1603 
1606 void
1608 {
1610 }
1611 
1616 int
1618 {
1619  int num = 0;
1620 
1622  return 1;
1623 
1624  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
1625  if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
1626  circ->purpose == CIRCUIT_PURPOSE_TESTING &&
1627  circ->state == CIRCUIT_STATE_OPEN)
1628  num++;
1629  }
1630  SMARTLIST_FOREACH_END(circ);
1631  return num >= NUM_PARALLEL_TESTING_CIRCS;
1632 }
1633 
1640 static void
1642 {
1644  !check_whether_orport_reachable(get_options())) {
1645  /* either we've already done everything we want with testing circuits,
1646  * or this testing circuit became open due to a fluke, e.g. we picked
1647  * a last hop where we already had the connection open due to an
1648  * outgoing local circuit. */
1649  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_AT_ORIGIN);
1650  } else if (circuit_enough_testing_circs()) {
1653  } else
1655 }
1656 
1658 static void
1660 {
1661  const or_options_t *options = get_options();
1662  if (server_mode(options) && check_whether_orport_reachable(options))
1663  return;
1664 
1665  log_info(LD_GENERAL,
1666  "Our testing circuit (to see if your ORPort is reachable) "
1667  "has failed. I'll try again later.");
1668 
1669  /* These aren't used yet. */
1670  (void)circ;
1671  (void)at_last_hop;
1672 }
1673 
1680 void
1682 {
1683  circuit_event_status(circ, CIRC_EVENT_BUILT, 0);
1684 
1685  /* Remember that this circuit has finished building. Now if we start
1686  * it building again later (e.g. by extending it), we will know not
1687  * to consider its build time. */
1688  circ->has_opened = 1;
1689 
1690  switch (TO_CIRCUIT(circ)->purpose) {
1692  hs_client_circuit_has_opened(circ);
1693  /* Start building an intro circ if we don't have one yet. */
1695  /* This isn't a call to circuit_try_attaching_streams because a
1696  * circuit in _C_ESTABLISH_REND state isn't connected to its
1697  * hidden service yet, thus we can't attach streams to it yet,
1698  * thus circuit_try_attaching_streams would always clear the
1699  * circuit's isolation state. circuit_try_attaching_streams is
1700  * called later, when the rend circ enters _C_REND_JOINED
1701  * state. */
1702  break;
1704  hs_client_circuit_has_opened(circ);
1705  break;
1709  /* Tell any AP connections that have been waiting for a new
1710  * circuit that one is ready. */
1712  break;
1714  /* at the service, waiting for introductions */
1715  hs_service_circuit_has_opened(circ);
1716  break;
1718  /* at the service, connecting to rend point */
1719  hs_service_circuit_has_opened(circ);
1720  break;
1722  circuit_testing_opened(circ);
1723  break;
1724  /* default:
1725  * This won't happen in normal operation, but might happen if the
1726  * controller did it. Just let it slide. */
1727  }
1728 }
1729 
1732 static int
1734 {
1735  if (/* The circuit may have become non-open if it was cannibalized.*/
1736  circ->base_.state == CIRCUIT_STATE_OPEN &&
1737  /* If !isolation_values_set, there is nothing to clear. */
1738  circ->isolation_values_set &&
1739  /* It's not legal to clear a circuit's isolation info if it's ever had
1740  * streams attached */
1742  /* If we have any isolation information set on this circuit, and
1743  * we didn't manage to attach any streams to it, then we can
1744  * and should clear it and try again. */
1746  return 1;
1747  } else {
1748  return 0;
1749  }
1750 }
1751 
1754 void
1756 {
1757  /* Attach streams to this circuit if we can. */
1759 
1760  /* The call to circuit_try_clearing_isolation_state here will do
1761  * nothing and return 0 if we didn't attach any streams to circ
1762  * above. */
1764  /* Maybe *now* we can attach some streams to this circuit. */
1766  }
1767 }
1768 
1771 void
1773 {
1774  channel_t *n_chan = NULL;
1775  /* we should examine circ and see if it failed because of
1776  * the last hop or an earlier hop. then use this info below.
1777  */
1778  int failed_at_last_hop = 0;
1779 
1780  /* First, check to see if this was a path failure, rather than build
1781  * failure.
1782  *
1783  * Note that we deliberately use circuit_get_cpath_len() (and not
1784  * circuit_get_cpath_opened_len()) because we only want to ensure
1785  * that a full path is *chosen*. This is different than a full path
1786  * being *built*. We only want to count *build* failures below.
1787  *
1788  * Path selection failures can happen spuriously for a number
1789  * of reasons (such as aggressive/invalid user-specified path
1790  * restrictions in the torrc, insufficient microdescriptors, and
1791  * non-user reasons like exitpolicy issues), and so should not be
1792  * counted as failures below.
1793  */
1794  if (circuit_get_cpath_len(circ) < circ->build_state->desired_path_len) {
1795  static ratelim_t pathfail_limit = RATELIM_INIT(3600);
1796  log_fn_ratelim(&pathfail_limit, LOG_NOTICE, LD_CIRC,
1797  "Our circuit %u (id: %" PRIu32 ") died due to an invalid "
1798  "selected path, purpose %s. This may be a torrc "
1799  "configuration issue, or a bug.",
1800  TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier,
1801  circuit_purpose_to_string(TO_CIRCUIT(circ)->purpose));
1802 
1803  /* If the path failed on an RP, retry it. */
1804  if (TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND)
1805  hs_circ_retry_service_rendezvous_point(circ);
1806 
1807  /* In all other cases, just bail. The rest is just failure accounting
1808  * that we don't want to do */
1809  return;
1810  }
1811 
1812  /* If the last hop isn't open, and the second-to-last is, we failed
1813  * at the last hop. */
1814  if (circ->cpath &&
1815  circ->cpath->prev->state != CPATH_STATE_OPEN &&
1816  circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
1817  failed_at_last_hop = 1;
1818  }
1819 
1820  /* Check if we failed at first hop */
1821  if (circ->cpath &&
1822  circ->cpath->state != CPATH_STATE_OPEN &&
1823  ! circ->base_.received_destroy) {
1824  /* We failed at the first hop for some reason other than a DESTROY cell.
1825  * If there's an OR connection to blame, blame it. Also, avoid this relay
1826  * for a while, and fail any one-hop directory fetches destined for it. */
1827  const char *n_chan_ident = circ->cpath->extend_info->identity_digest;
1828  tor_assert(n_chan_ident);
1829  int already_marked = 0;
1830  if (circ->base_.n_chan) {
1831  n_chan = circ->base_.n_chan;
1832 
1833  if (n_chan->is_bad_for_new_circs) {
1834  /* We only want to blame this router when a fresh healthy
1835  * connection fails. So don't mark this router as newly failed,
1836  * since maybe this was just an old circuit attempt that's
1837  * finally timing out now. Also, there's no need to blow away
1838  * circuits/streams/etc, since the failure of an unhealthy conn
1839  * doesn't tell us much about whether a healthy conn would
1840  * succeed. */
1841  already_marked = 1;
1842  }
1843  log_info(LD_OR,
1844  "Our circuit %u (id: %" PRIu32 ") failed to get a response "
1845  "from the first hop (%s). I'm going to try to rotate to a "
1846  "better connection.",
1847  TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier,
1849  n_chan->is_bad_for_new_circs = 1;
1850  } else {
1851  log_info(LD_OR,
1852  "Our circuit %u (id: %" PRIu32 ") died before the first hop "
1853  "with no connection",
1854  TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier);
1855  }
1856  if (!already_marked) {
1857  /*
1858  * If we have guard state (new guard API) and our path selection
1859  * code actually chose a full path, then blame the failure of this
1860  * circuit on the guard.
1861  */
1862  if (circ->guard_state)
1864  /* if there are any one-hop streams waiting on this circuit, fail
1865  * them now so they can retry elsewhere. */
1866  connection_ap_fail_onehop(n_chan_ident, circ->build_state);
1867  }
1868  }
1869 
1870  switch (circ->base_.purpose) {
1874  /* If we never built the circuit, note it as a failure. */
1876  if (failed_at_last_hop) {
1877  /* Make sure any streams that demand our last hop as their exit
1878  * know that it's unlikely to happen. */
1880  }
1881  break;
1883  circuit_testing_failed(circ, failed_at_last_hop);
1884  break;
1886  /* at the service, waiting for introductions */
1887  if (circ->base_.state != CIRCUIT_STATE_OPEN) {
1889  }
1890  /* no need to care here, because the service will rebuild intro
1891  * points periodically. */
1892  break;
1894  /* at the client, connecting to intro point */
1895  /* Don't increment failure count, since the service may have picked
1896  * the introduction point maliciously */
1897  /* The client will pick a new intro point when this one dies, if
1898  * the stream in question still cares. No need to act here. */
1899  break;
1901  /* at the client, waiting for the service */
1903  /* the client will pick a new rend point when this one dies, if
1904  * the stream in question still cares. No need to act here. */
1905  break;
1907  /* at the service, connecting to rend point */
1908  /* Don't increment failure count, since the client may have picked
1909  * the rendezvous point maliciously */
1910  log_info(LD_REND,
1911  "Couldn't connect to the client's chosen rend point %s "
1912  "(%s hop failed).",
1914  failed_at_last_hop?"last":"non-last");
1915  hs_circ_retry_service_rendezvous_point(circ);
1916  break;
1917  /* default:
1918  * This won't happen in normal operation, but might happen if the
1919  * controller did it. Just let it slide. */
1920  }
1921 }
1922 
1926 static int n_circuit_failures = 0;
1930 
1933 #define MAX_CIRCUIT_FAILURES 5
1934 
1938 circuit_launch(uint8_t purpose, int flags)
1939 {
1940  return circuit_launch_by_extend_info(purpose, NULL, flags);
1941 }
1942 
1943 /* Do we have enough descriptors to build paths?
1944  * If need_exit is true, return 1 if we can build exit paths.
1945  * (We need at least one Exit in the consensus to build exit paths.)
1946  * If need_exit is false, return 1 if we can build internal paths.
1947  */
1948 static int
1949 have_enough_path_info(int need_exit)
1950 {
1951  if (need_exit)
1952  return router_have_consensus_path() == CONSENSUS_PATH_EXIT;
1953  else
1954  return router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN;
1955 }
1956 
1960 int
1962 {
1963  if (purpose == CIRCUIT_PURPOSE_HS_VANGUARDS) {
1964  return 1;
1965  }
1966 
1967  /* Client-side purpose */
1968  if (purpose >= CIRCUIT_PURPOSE_C_HS_MIN_ &&
1969  purpose <= CIRCUIT_PURPOSE_C_HS_MAX_) {
1970  return 1;
1971  }
1972 
1973  /* Service-side purpose */
1974  if (purpose >= CIRCUIT_PURPOSE_S_HS_MIN_ &&
1975  purpose <= CIRCUIT_PURPOSE_S_HS_MAX_) {
1976  return 1;
1977  }
1978 
1979  return 0;
1980 }
1981 
1992 int
1994 {
1995  const or_options_t *options = get_options();
1996 
1997  /* Only hidden service circuits use vanguards */
1998  if (!circuit_purpose_is_hidden_service(purpose))
1999  return 0;
2000 
2001  /* Pinned middles are effectively vanguards */
2002  if (options->HSLayer2Nodes || options->HSLayer3Nodes)
2003  return 1;
2004 
2005  return 0;
2006 }
2007 
2014 static int
2015 circuit_should_cannibalize_to_build(uint8_t purpose_to_build,
2016  int has_extend_info,
2017  int onehop_tunnel)
2018 {
2019 
2020  /* Do not try to cannibalize if this is a one hop circuit. */
2021  if (onehop_tunnel) {
2022  return 0;
2023  }
2024 
2025  /* Don't try to cannibalize for general purpose circuits that do not
2026  * specify a custom exit. */
2027  if (purpose_to_build == CIRCUIT_PURPOSE_C_GENERAL && !has_extend_info) {
2028  return 0;
2029  }
2030 
2031  /* Don't cannibalize for testing circuits. We want to see if they
2032  * complete normally. Also don't cannibalize for vanguard-purpose
2033  * circuits, since those are specially pre-built for later
2034  * cannibalization by the actual specific circuit types that need
2035  * vanguards.
2036  */
2037  if (purpose_to_build == CIRCUIT_PURPOSE_TESTING ||
2038  purpose_to_build == CIRCUIT_PURPOSE_HS_VANGUARDS) {
2039  return 0;
2040  }
2041 
2042  /* For vanguards, the server-side intro circ is not cannibalized
2043  * because we pre-build 4 hop HS circuits, and it only needs a 3 hop
2044  * circuit. It is also long-lived, so it is more important that
2045  * it have lower latency than get built fast.
2046  */
2047  if (circuit_should_use_vanguards(purpose_to_build) &&
2048  purpose_to_build == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
2049  return 0;
2050  }
2051 
2052  return 1;
2053 }
2054 
2064  extend_info_t *extend_info,
2065  int flags)
2066 {
2067  origin_circuit_t *circ;
2068  int onehop_tunnel = (flags & CIRCLAUNCH_ONEHOP_TUNNEL) != 0;
2069  int have_path = have_enough_path_info(! (flags & CIRCLAUNCH_IS_INTERNAL) );
2070 
2071  /* Keep some stats about our attempts to launch HS rendezvous circuits */
2072  if (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
2074  }
2075 
2076  if (!onehop_tunnel && (!router_have_minimum_dir_info() || !have_path)) {
2077  log_debug(LD_CIRC,"Haven't %s yet; canceling "
2078  "circuit launch.",
2079  !router_have_minimum_dir_info() ?
2080  "fetched enough directory info" :
2081  "received a consensus with exits");
2082  return NULL;
2083  }
2084 
2085  /* If we can/should cannibalize another circuit to build this one,
2086  * then do so. */
2088  extend_info != NULL,
2089  onehop_tunnel)) {
2090  /* see if there are appropriate circs available to cannibalize. */
2091  /* XXX if we're planning to add a hop, perhaps we want to look for
2092  * internal circs rather than exit circs? -RD */
2093  circ = circuit_find_to_cannibalize(purpose, extend_info, flags);
2094  if (circ) {
2095  uint8_t old_purpose = circ->base_.purpose;
2096  struct timeval old_timestamp_began = circ->base_.timestamp_began;
2097 
2098  log_info(LD_CIRC, "Cannibalizing circ %u (id: %" PRIu32 ") for "
2099  "purpose %d (%s)",
2100  TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier, purpose,
2101  circuit_purpose_to_string(purpose));
2102 
2103  if ((purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
2104  purpose == CIRCUIT_PURPOSE_C_INTRODUCING) &&
2105  circ->path_state == PATH_STATE_BUILD_SUCCEEDED) {
2106  /* Path bias: Cannibalized rends pre-emptively count as a
2107  * successfully built but unused closed circuit. We don't
2108  * wait until the extend (or the close) because the rend
2109  * point could be malicious.
2110  *
2111  * Same deal goes for client side introductions. Clients
2112  * can be manipulated to connect repeatedly to them
2113  * (especially web clients).
2114  *
2115  * If we decide to probe the initial portion of these circs,
2116  * (up to the adversary's final hop), we need to remove this,
2117  * or somehow mark the circuit with a special path state.
2118  */
2119 
2120  /* This must be called before the purpose change */
2121  pathbias_check_close(circ, END_CIRC_REASON_FINISHED);
2122  }
2123 
2124  circuit_change_purpose(TO_CIRCUIT(circ), purpose);
2125  /* Reset the start date of this circ, else expire_building
2126  * will see it and think it's been trying to build since it
2127  * began.
2128  *
2129  * Technically, the code should reset this when the
2130  * create cell is finally sent, but we're close enough
2131  * here. */
2132  tor_gettimeofday(&circ->base_.timestamp_began);
2133 
2134  control_event_circuit_cannibalized(circ, old_purpose,
2135  &old_timestamp_began);
2136 
2137  switch (purpose) {
2139  /* it's ready right now */
2140  break;
2147  /* need to add a new hop */
2148  tor_assert(extend_info);
2149  if (circuit_extend_to_new_exit(circ, extend_info) < 0)
2150  return NULL;
2151  break;
2152  default:
2153  log_warn(LD_BUG,
2154  "unexpected purpose %d when cannibalizing a circ.",
2155  purpose);
2157  return NULL;
2158  }
2159  return circ;
2160  }
2161  }
2162 
2165  /* too many failed circs in a row. don't try. */
2166 // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
2167  return NULL;
2168  }
2169 
2170  /* try a circ. if it fails, circuit_mark_for_close will increment
2171  * n_circuit_failures */
2172  return circuit_establish_circuit(purpose, extend_info, flags);
2173 }
2174 
2178 static void
2180 {
2182  log_debug(LD_CIRC,"n_circuit_failures now %d.",n_circuit_failures);
2183 }
2184 
2189 void
2191 {
2192  if (timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
2194  else
2196  n_circuit_failures = 0;
2197 }
2198 
2205 static int
2207  uint8_t desired_circuit_purpose,
2208  origin_circuit_t **circp)
2209 {
2210  origin_circuit_t *circ;
2211  int check_exit_policy;
2212  int need_uptime, need_internal;
2213  int want_onehop;
2214  const or_options_t *options = get_options();
2215 
2216  tor_assert(conn);
2217  tor_assert(circp);
2218  if (ENTRY_TO_CONN(conn)->state != AP_CONN_STATE_CIRCUIT_WAIT) {
2219  connection_t *c = ENTRY_TO_CONN(conn);
2220  log_err(LD_BUG, "Connection state mismatch: wanted "
2221  "AP_CONN_STATE_CIRCUIT_WAIT, but got %d (%s)",
2222  c->state, conn_state_to_string(c->type, c->state));
2223  }
2225 
2226  /* Will the exit policy of the exit node apply to this stream? */
2227  check_exit_policy =
2228  conn->socks_request->command == SOCKS_COMMAND_CONNECT &&
2229  !conn->use_begindir &&
2230  !connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn));
2231 
2232  /* Does this connection want a one-hop circuit? */
2233  want_onehop = conn->want_onehop;
2234 
2235  /* Do we need a high-uptime circuit? */
2236  need_uptime = !conn->want_onehop && !conn->use_begindir &&
2238  conn->socks_request->port);
2239 
2240  /* Do we need an "internal" circuit? */
2241  if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)
2242  need_internal = 1;
2243  else if (conn->use_begindir || conn->want_onehop)
2244  need_internal = 1;
2245  else
2246  need_internal = 0;
2247 
2248  /* We now know what kind of circuit we need. See if there is an
2249  * open circuit that we can use for this stream */
2250  circ = circuit_get_best(conn, 1 /* Insist on open circuits */,
2251  desired_circuit_purpose,
2252  need_uptime, need_internal);
2253 
2254  if (circ) {
2255  /* We got a circuit that will work for this stream! We can return it. */
2256  *circp = circ;
2257  return 1; /* we're happy */
2258  }
2259 
2260  /* Okay, there's no circuit open that will work for this stream. Let's
2261  * see if there's an in-progress circuit or if we have to launch one */
2262 
2263  /* Do we know enough directory info to build circuits at all? */
2264  int have_path = have_enough_path_info(!need_internal);
2265 
2266  if (!want_onehop && (!router_have_minimum_dir_info() || !have_path)) {
2267  /* If we don't have enough directory information, we can't build
2268  * multihop circuits.
2269  */
2271  int severity = LOG_NOTICE;
2272  /* Retry some stuff that might help the connection work. */
2273  /* If we are configured with EntryNodes or UseBridges */
2274  if (entry_list_is_constrained(options)) {
2275  /* Retry all our guards / bridges.
2276  * guards_retry_optimistic() always returns true here. */
2277  int rv = guards_retry_optimistic(options);
2278  tor_assert_nonfatal_once(rv);
2279  log_fn(severity, LD_APP|LD_DIR,
2280  "Application request when we haven't %s. "
2281  "Optimistically trying known %s again.",
2282  !router_have_minimum_dir_info() ?
2283  "used client functionality lately" :
2284  "received a consensus with exits",
2285  options->UseBridges ? "bridges" : "entrynodes");
2286  } else {
2287  /* Getting directory documents doesn't help much if we have a limited
2288  * number of guards */
2289  tor_assert_nonfatal(!options->UseBridges);
2290  tor_assert_nonfatal(!options->EntryNodes);
2291  /* Retry our directory fetches, so we have a fresh set of guard info */
2292  log_fn(severity, LD_APP|LD_DIR,
2293  "Application request when we haven't %s. "
2294  "Optimistically trying directory fetches again.",
2295  !router_have_minimum_dir_info() ?
2296  "used client functionality lately" :
2297  "received a consensus with exits");
2299  }
2300  }
2301  /* Since we didn't have enough directory info, we can't attach now. The
2302  * stream will be dealt with when router_have_minimum_dir_info becomes 1,
2303  * or when all directory attempts fail and directory_all_unreachable()
2304  * kills it.
2305  */
2306  return 0;
2307  }
2308 
2309  /* Check whether the exit policy of the chosen exit, or the exit policies
2310  * of _all_ nodes, would forbid this node. */
2311  if (check_exit_policy) {
2312  if (!conn->chosen_exit_name) {
2313  struct in_addr in;
2314  tor_addr_t addr, *addrp=NULL;
2315  if (tor_inet_aton(conn->socks_request->address, &in)) {
2316  tor_addr_from_in(&addr, &in);
2317  addrp = &addr;
2318  }
2320  conn->socks_request->port,
2321  need_uptime)) {
2322  log_notice(LD_APP,
2323  "No Tor server allows exit to %s:%d. Rejecting.",
2324  safe_str_client(conn->socks_request->address),
2325  conn->socks_request->port);
2326  return -1;
2327  }
2328  } else {
2329  /* XXXX Duplicates checks in connection_ap_handshake_attach_circuit:
2330  * refactor into a single function. */
2331  const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 0);
2332  int opt = conn->chosen_exit_optional;
2333  if (node && !connection_ap_can_use_exit(conn, node)) {
2334  log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2335  "Requested exit point '%s' is excluded or "
2336  "would refuse request. %s.",
2337  conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2338  if (opt) {
2339  conn->chosen_exit_optional = 0;
2340  tor_free(conn->chosen_exit_name);
2341  /* Try again. */
2342  return circuit_get_open_circ_or_launch(conn,
2343  desired_circuit_purpose,
2344  circp);
2345  }
2346  return -1;
2347  }
2348  }
2349  }
2350 
2351  /* Now, check whether there already a circuit on the way that could handle
2352  * this stream. This check matches the one above, but this time we
2353  * do not require that the circuit will work. */
2354  circ = circuit_get_best(conn, 0 /* don't insist on open circuits */,
2355  desired_circuit_purpose,
2356  need_uptime, need_internal);
2357  if (circ)
2358  log_debug(LD_CIRC, "one on the way!");
2359 
2360  if (!circ) {
2361  /* No open or in-progress circuit could handle this stream! We
2362  * will have to launch one!
2363  */
2364 
2365  /* The chosen exit node, if there is one. */
2366  extend_info_t *extend_info=NULL;
2367  const int n_pending = count_pending_general_client_circuits();
2368 
2369  /* Do we have too many pending circuits? */
2370  if (n_pending >= options->MaxClientCircuitsPending) {
2371  static ratelim_t delay_limit = RATELIM_INIT(10*60);
2372  char *m;
2373  if ((m = rate_limit_log(&delay_limit, approx_time()))) {
2374  log_notice(LD_APP, "We'd like to launch a circuit to handle a "
2375  "connection, but we already have %d general-purpose client "
2376  "circuits pending. Waiting until some finish.%s",
2377  n_pending, m);
2378  tor_free(m);
2379  }
2380  return 0;
2381  }
2382 
2383  /* If this is a hidden service trying to start an introduction point,
2384  * handle that case. */
2385  if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
2386  const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
2387  /* need to pick an intro point */
2388  extend_info = hs_client_get_random_intro_from_edge(edge_conn);
2389  if (!extend_info) {
2390  log_info(LD_REND, "No intro points: re-fetching service descriptor.");
2391  if (edge_conn->rend_data) {
2393  } else {
2394  hs_client_refetch_hsdesc(&edge_conn->hs_ident->identity_pk);
2395  }
2397  return 0;
2398  }
2399  log_info(LD_REND,"Chose %s as intro point for '%s'.",
2400  extend_info_describe(extend_info),
2401  (edge_conn->rend_data) ?
2402  safe_str_client(rend_data_get_address(edge_conn->rend_data)) :
2403  "service");
2404  }
2405 
2406  /* If we have specified a particular exit node for our
2407  * connection, then be sure to open a circuit to that exit node.
2408  */
2409  if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2410  desired_circuit_purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
2411  desired_circuit_purpose == CIRCUIT_PURPOSE_C_HSDIR_GET) {
2412  if (conn->chosen_exit_name) {
2413  const node_t *r;
2414  int opt = conn->chosen_exit_optional;
2415  r = node_get_by_nickname(conn->chosen_exit_name, 0);
2416  if (r && node_has_preferred_descriptor(r, conn->want_onehop ? 1 : 0)) {
2417  /* We might want to connect to an IPv6 bridge for loading
2418  descriptors so we use the preferred address rather than
2419  the primary. */
2420  extend_info = extend_info_from_node(r, conn->want_onehop ? 1 : 0);
2421  if (!extend_info) {
2422  log_warn(LD_CIRC,"Could not make a one-hop connection to %s. "
2423  "Discarding this circuit.", conn->chosen_exit_name);
2424  return -1;
2425  }
2426  } else { /* ! (r && node_has_preferred_descriptor(...)) */
2427  log_debug(LD_DIR, "considering %d, %s",
2428  want_onehop, conn->chosen_exit_name);
2429  if (want_onehop && conn->chosen_exit_name[0] == '$') {
2430  /* We're asking for a one-hop circuit to a router that
2431  * we don't have a routerinfo about. Make up an extend_info. */
2432  /* XXX prop220: we need to make chosen_exit_name able to
2433  * encode both key formats. This is not absolutely critical
2434  * since this is just for one-hop circuits, but we should
2435  * still get it done */
2436  char digest[DIGEST_LEN];
2437  char *hexdigest = conn->chosen_exit_name+1;
2438  tor_addr_t addr;
2439  if (strlen(hexdigest) < HEX_DIGEST_LEN ||
2440  base16_decode(digest,DIGEST_LEN,
2441  hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN) {
2442  log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
2443  return -1;
2444  }
2445  if (tor_addr_parse(&addr, conn->socks_request->address) < 0) {
2446  log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
2448  return -1;
2449  }
2450  /* XXXX prop220 add a workaround for ed25519 ID below*/
2451  extend_info = extend_info_new(conn->chosen_exit_name+1,
2452  digest,
2453  NULL, /* Ed25519 ID */
2454  NULL, NULL, /* onion keys */
2455  &addr, conn->socks_request->port);
2456  } else { /* ! (want_onehop && conn->chosen_exit_name[0] == '$') */
2457  /* We will need an onion key for the router, and we
2458  * don't have one. Refuse or relax requirements. */
2459  log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2460  "Requested exit point '%s' is not known. %s.",
2461  conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2462  if (opt) {
2463  conn->chosen_exit_optional = 0;
2464  tor_free(conn->chosen_exit_name);
2465  /* Try again with no requested exit */
2466  return circuit_get_open_circ_or_launch(conn,
2467  desired_circuit_purpose,
2468  circp);
2469  }
2470  return -1;
2471  }
2472  }
2473  }
2474  } /* Done checking for general circutis with chosen exits. */
2475 
2476  /* What purpose do we need to launch this circuit with? */
2477  uint8_t new_circ_purpose;
2478  if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
2479  new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
2480  else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
2481  new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
2482  else
2483  new_circ_purpose = desired_circuit_purpose;
2484 
2485  /* Determine what kind of a circuit to launch, and actually launch it. */
2486  {
2487  int flags = CIRCLAUNCH_NEED_CAPACITY;
2488  if (want_onehop) flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
2489  if (need_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
2490  if (need_internal) flags |= CIRCLAUNCH_IS_INTERNAL;
2491 
2492  /* If we are about to pick a v3 RP right now, make sure we pick a
2493  * rendezvous point that supports the v3 protocol! */
2494  if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED &&
2495  new_circ_purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
2496  ENTRY_TO_EDGE_CONN(conn)->hs_ident) {
2497  flags |= CIRCLAUNCH_IS_V3_RP;
2498  log_info(LD_GENERAL, "Getting rendezvous circuit to v3 service!");
2499  }
2500 
2501  circ = circuit_launch_by_extend_info(new_circ_purpose, extend_info,
2502  flags);
2503  }
2504 
2505  extend_info_free(extend_info);
2506 
2507  /* Now trigger things that need to happen when we launch circuits */
2508 
2509  if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2510  desired_circuit_purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
2511  desired_circuit_purpose == CIRCUIT_PURPOSE_S_HSDIR_POST) {
2512  /* We just caused a circuit to get built because of this stream.
2513  * If this stream has caused a _lot_ of circuits to be built, that's
2514  * a bad sign: we should tell the user. */
2515  if (conn->num_circuits_launched < NUM_CIRCUITS_LAUNCHED_THRESHOLD &&
2516  ++conn->num_circuits_launched == NUM_CIRCUITS_LAUNCHED_THRESHOLD)
2517  log_info(LD_CIRC, "The application request to %s:%d has launched "
2518  "%d circuits without finding one it likes.",
2520  conn->socks_request->port,
2521  conn->num_circuits_launched);
2522  } else {
2523  /* help predict this next time */
2524  rep_hist_note_used_internal(time(NULL), need_uptime, 1);
2525  if (circ) {
2526  const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
2527  if (edge_conn->rend_data) {
2528  /* write the service_id into circ */
2529  circ->rend_data = rend_data_dup(edge_conn->rend_data);
2530  } else if (edge_conn->hs_ident) {
2531  circ->hs_ident =
2532  hs_ident_circuit_new(&edge_conn->hs_ident->identity_pk,
2533  HS_IDENT_CIRCUIT_INTRO);
2534  }
2535  if (circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
2536  circ->base_.state == CIRCUIT_STATE_OPEN)
2537  circuit_has_opened(circ);
2538  }
2539  }
2540  } /* endif (!circ) */
2541 
2542  /* We either found a good circuit, or launched a new circuit, or failed to
2543  * do so. Report success, and delay. */
2544 
2545  if (circ) {
2546  /* Mark the circuit with the isolation fields for this connection.
2547  * When the circuit arrives, we'll clear these flags: this is
2548  * just some internal bookkeeping to make sure that we have
2549  * launched enough circuits.
2550  */
2552  } else {
2553  log_info(LD_APP,
2554  "No safe circuit (purpose %d) ready for edge "
2555  "connection; delaying.",
2556  desired_circuit_purpose);
2557  }
2558  *circp = circ;
2559  return 0;
2560 }
2561 
2564 static int
2566 {
2567  crypt_path_t *cpath, *cpath_next = NULL;
2568  for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
2569  cpath_next = cpath->next;
2570  if (crypt_path == cpath)
2571  return 1;
2572  }
2573  return 0;
2574 }
2575 
2577 static int
2579 {
2580  const or_options_t *options = get_options();
2581  if (options->OptimisticData < 0) {
2582  /* Note: this default was 0 before #18815 was merged. We can't take the
2583  * parameter out of the consensus until versions before that are all
2584  * obsolete. */
2585  const int32_t enabled =
2586  networkstatus_get_param(NULL, "UseOptimisticData", /*default*/ 1, 0, 1);
2587  return (int)enabled;
2588  }
2589  return options->OptimisticData;
2590 }
2591 
2596 static void
2598  crypt_path_t *cpath)
2599 {
2600  const node_t *exitnode = NULL;
2601 
2602  /* add it into the linked list of streams on this circuit */
2603  log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %u.",
2604  (unsigned)circ->base_.n_circ_id);
2605 
2606  /* If this is the first stream on this circuit, tell circpad
2607  * that streams are attached */
2608  if (!circ->p_streams)
2610 
2611  /* reset it, so we can measure circ timeouts */
2612  ENTRY_TO_CONN(apconn)->timestamp_last_read_allowed = time(NULL);
2613  ENTRY_TO_EDGE_CONN(apconn)->next_stream = circ->p_streams;
2614  ENTRY_TO_EDGE_CONN(apconn)->on_circuit = TO_CIRCUIT(circ);
2615  /* assert_connection_ok(conn, time(NULL)); */
2616  circ->p_streams = ENTRY_TO_EDGE_CONN(apconn);
2617 
2618  if (connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(apconn))) {
2619  /* We are attaching a stream to a rendezvous circuit. That means
2620  * that an attempt to connect to a hidden service just
2621  * succeeded. Tell rendclient.c. */
2622  hs_client_note_connection_attempt_succeeded(ENTRY_TO_EDGE_CONN(apconn));
2623  }
2624 
2625  if (cpath) { /* we were given one; use it */
2626  tor_assert(cpath_is_on_circuit(circ, cpath));
2627  } else {
2628  /* use the last hop in the circuit */
2629  tor_assert(circ->cpath);
2630  tor_assert(circ->cpath->prev);
2631  tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
2632  cpath = circ->cpath->prev;
2633  }
2634  ENTRY_TO_EDGE_CONN(apconn)->cpath_layer = cpath;
2635 
2638 
2639  /* Compute the exitnode if possible, for logging below */
2640  if (cpath->extend_info)
2641  exitnode = node_get_by_id(cpath->extend_info->identity_digest);
2642 
2643  /* See if we can use optimistic data on this circuit */
2644  if (optimistic_data_enabled() &&
2645  (circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2646  circ->base_.purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
2647  circ->base_.purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
2648  circ->base_.purpose == CIRCUIT_PURPOSE_C_REND_JOINED))
2649  apconn->may_use_optimistic_data = 1;
2650  else
2651  apconn->may_use_optimistic_data = 0;
2652  log_info(LD_APP, "Looks like completed circuit to %s %s allow "
2653  "optimistic data for connection to %s",
2654  circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL ?
2655  /* node_describe() does the right thing if exitnode is NULL */
2656  safe_str_client(node_describe(exitnode)) :
2657  "hidden service",
2658  apconn->may_use_optimistic_data ? "does" : "doesn't",
2659  safe_str_client(apconn->socks_request->address));
2660 }
2661 
2664 int
2665 hostname_in_track_host_exits(const or_options_t *options, const char *address)
2666 {
2667  if (!options->TrackHostExits)
2668  return 0;
2669  SMARTLIST_FOREACH_BEGIN(options->TrackHostExits, const char *, cp) {
2670  if (cp[0] == '.') { /* match end */
2671  if (cp[1] == '\0' ||
2672  !strcasecmpend(address, cp) ||
2673  !strcasecmp(address, &cp[1]))
2674  return 1;
2675  } else if (strcasecmp(cp, address) == 0) {
2676  return 1;
2677  }
2678  } SMARTLIST_FOREACH_END(cp);
2679  return 0;
2680 }
2681 
2686 static void
2688  const origin_circuit_t *circ)
2689 {
2690  const or_options_t *options = get_options();
2691  char *new_address = NULL;
2692  char fp[HEX_DIGEST_LEN+1];
2693 
2694  /* Search the addressmap for this conn's destination. */
2695  /* If they're not in the address map.. */
2696  if (!options->TrackHostExits ||
2698  options->TrackHostExitsExpire))
2699  return; /* nothing to track, or already mapped */
2700 
2701  if (!hostname_in_track_host_exits(options, conn->socks_request->address) ||
2702  !circ->build_state->chosen_exit)
2703  return;
2704 
2705  /* write down the fingerprint of the chosen exit, not the nickname,
2706  * because the chosen exit might not be named. */
2707  base16_encode(fp, sizeof(fp),
2709 
2710  /* Add this exit/hostname pair to the addressmap. */
2711  tor_asprintf(&new_address, "%s.%s.exit",
2712  conn->socks_request->address, fp);
2713 
2714  addressmap_register(conn->socks_request->address, new_address,
2715  time(NULL) + options->TrackHostExitsExpire,
2716  ADDRMAPSRC_TRACKEXIT, 0, 0);
2717 }
2718 
2724 int
2726  origin_circuit_t *circ,
2727  crypt_path_t *cpath)
2728 {
2729  connection_t *base_conn = ENTRY_TO_CONN(conn);
2730  tor_assert(conn);
2732  base_conn->state == AP_CONN_STATE_CONTROLLER_WAIT);
2733  tor_assert(conn->socks_request);
2734  tor_assert(circ);
2735  tor_assert(circ->base_.state == CIRCUIT_STATE_OPEN);
2736 
2737  base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
2738 
2739  if (!circ->base_.timestamp_dirty ||
2740  ((conn->entry_cfg.isolation_flags & ISO_SOCKSAUTH) &&
2741  (conn->entry_cfg.socks_iso_keep_alive) &&
2742  (conn->socks_request->usernamelen ||
2743  conn->socks_request->passwordlen))) {
2744  /* When stream isolation is in use and controlled by an application
2745  * we are willing to keep using the stream. */
2746  circ->base_.timestamp_dirty = approx_time();
2747  }
2748 
2750 
2751  /* Now, actually link the connection. */
2752  link_apconn_to_circ(conn, circ, cpath);
2753 
2754  tor_assert(conn->socks_request);
2755  if (conn->socks_request->command == SOCKS_COMMAND_CONNECT) {
2756  if (!conn->use_begindir)
2757  consider_recording_trackhost(conn, circ);
2758  if (connection_ap_handshake_send_begin(conn) < 0)
2759  return -1;
2760  } else {
2762  return -1;
2763  }
2764 
2765  return 1;
2766 }
2767 
2776 static int
2778 {
2779  const connection_t *base_conn = ENTRY_TO_CONN(conn);
2780  tor_assert_nonfatal(!connection_edge_is_rendezvous_stream(
2781  ENTRY_TO_EDGE_CONN(conn)));
2782 
2783  if (base_conn->linked_conn &&
2784  base_conn->linked_conn->type == CONN_TYPE_DIR) {
2785  /* Set a custom purpose for hsdir activity */
2786  if (base_conn->linked_conn->purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2 ||
2789  } else if (base_conn->linked_conn->purpose
2791  base_conn->linked_conn->purpose
2794  }
2795  }
2796 
2797  /* All other purposes are general for now */
2799 }
2800 
2807 /* XXXX this function should mark for close whenever it returns -1;
2808  * its callers shouldn't have to worry about that. */
2809 int
2811 {
2812  connection_t *base_conn = ENTRY_TO_CONN(conn);
2813  int retval;
2814  int conn_age;
2815  int want_onehop;
2816 
2817  tor_assert(conn);
2819  tor_assert(conn->socks_request);
2820  want_onehop = conn->want_onehop;
2821 
2822  conn_age = (int)(time(NULL) - base_conn->timestamp_created);
2823 
2824  /* Is this connection so old that we should give up on it? */
2825  if (conn_age >= get_options()->SocksTimeout) {
2826  int severity = (tor_addr_is_null(&base_conn->addr) && !base_conn->port) ?
2827  LOG_INFO : LOG_NOTICE;
2828  log_fn(severity, LD_APP,
2829  "Tried for %d seconds to get a connection to %s:%d. Giving up.",
2830  conn_age, safe_str_client(conn->socks_request->address),
2831  conn->socks_request->port);
2832  return -1;
2833  }
2834 
2835  /* We handle "general" (non-onion) connections much more straightforwardly.
2836  */
2837  if (!connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn))) {
2838  /* we're a general conn */
2839  origin_circuit_t *circ=NULL;
2840 
2841  /* Are we linked to a dir conn that aims to fetch a consensus?
2842  * We check here because the conn might no longer be needed. */
2843  if (base_conn->linked_conn &&
2844  base_conn->linked_conn->type == CONN_TYPE_DIR &&
2846 
2847  /* Yes we are. Is there a consensus fetch farther along than us? */
2848  if (networkstatus_consensus_is_already_downloading(
2849  TO_DIR_CONN(base_conn->linked_conn)->requested_resource)) {
2850  /* We're doing the "multiple consensus fetch attempts" game from
2851  * proposal 210, and we're late to the party. Just close this conn.
2852  * The circuit and TLS conn that we made will time out after a while
2853  * if nothing else wants to use them. */
2854  log_info(LD_DIR, "Closing extra consensus fetch (to %s) since one "
2855  "is already downloading.", base_conn->linked_conn->address);
2856  return -1;
2857  }
2858  }
2859 
2860  /* If we have a chosen exit, we need to use a circuit that's
2861  * open to that exit. See what exit we meant, and whether we can use it.
2862  */
2863  if (conn->chosen_exit_name) {
2864  const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 0);
2865  int opt = conn->chosen_exit_optional;
2866  if (!node && !want_onehop) {
2867  /* We ran into this warning when trying to extend a circuit to a
2868  * hidden service directory for which we didn't have a router
2869  * descriptor. See flyspray task 767 for more details. We should
2870  * keep this in mind when deciding to use BEGIN_DIR cells for other
2871  * directory requests as well. -KL*/
2872  log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2873  "Requested exit point '%s' is not known. %s.",
2874  conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2875  if (opt) {
2876  /* If we are allowed to ignore the .exit request, do so */
2877  conn->chosen_exit_optional = 0;
2878  tor_free(conn->chosen_exit_name);
2879  return 0;
2880  }
2881  return -1;
2882  }
2883  if (node && !connection_ap_can_use_exit(conn, node)) {
2884  log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2885  "Requested exit point '%s' is excluded or "
2886  "would refuse request. %s.",
2887  conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2888  if (opt) {
2889  /* If we are allowed to ignore the .exit request, do so */
2890  conn->chosen_exit_optional = 0;
2891  tor_free(conn->chosen_exit_name);
2892  return 0;
2893  }
2894  return -1;
2895  }
2896  }
2897 
2898  /* Find the circuit that we should use, if there is one. Otherwise
2899  * launch it
2900  */
2901  retval = circuit_get_open_circ_or_launch(conn,
2903  &circ);
2904 
2905  if (retval < 1) {
2906  /* We were either told "-1" (complete failure) or 0 (circuit in
2907  * progress); we can't attach this stream yet. */
2908  return retval;
2909  }
2910 
2911  log_debug(LD_APP|LD_CIRC,
2912  "Attaching apconn to circ %u (stream %d sec old).",
2913  (unsigned)circ->base_.n_circ_id, conn_age);
2914  /* print the circ's path, so clients can figure out which circs are
2915  * sucking. */
2917 
2918  /* We have found a suitable circuit for our conn. Hurray. Do
2919  * the attachment. */
2920  return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
2921 
2922  } else { /* we're a rendezvous conn */
2923  origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
2924 
2925  tor_assert(!ENTRY_TO_EDGE_CONN(conn)->cpath_layer);
2926 
2927  /* start by finding a rendezvous circuit for us */
2928 
2930  conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
2931  if (retval < 0) return -1; /* failed */
2932 
2933  if (retval > 0) {
2934  tor_assert(rendcirc);
2935  /* one is already established, attach */
2936  log_info(LD_REND,
2937  "rend joined circ %u (id: %" PRIu32 ") already here. "
2938  "Attaching. (stream %d sec old)",
2939  (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
2940  rendcirc->global_identifier, conn_age);
2941  /* Mark rendezvous circuits as 'newly dirty' every time you use
2942  * them, since the process of rebuilding a rendezvous circ is so
2943  * expensive. There is a tradeoff between linkability and
2944  * feasibility, at this point.
2945  */
2946  rendcirc->base_.timestamp_dirty = time(NULL);
2947 
2948  /* We've also attempted to use them. If they fail, we need to
2949  * probe them for path bias */
2950  pathbias_count_use_attempt(rendcirc);
2951 
2952  link_apconn_to_circ(conn, rendcirc, NULL);
2953  if (connection_ap_handshake_send_begin(conn) < 0)
2954  return 0; /* already marked, let them fade away */
2955  return 1;
2956  }
2957 
2958  /* At this point we need to re-check the state, since it's possible that
2959  * our call to circuit_get_open_circ_or_launch() changed the connection's
2960  * state from "CIRCUIT_WAIT" to "RENDDESC_WAIT" because we decided to
2961  * re-fetch the descriptor.
2962  */
2963  if (ENTRY_TO_CONN(conn)->state != AP_CONN_STATE_CIRCUIT_WAIT) {
2964  log_info(LD_REND, "This connection is no longer ready to attach; its "
2965  "state changed."
2966  "(We probably have to re-fetch its descriptor.)");
2967  return 0;
2968  }
2969 
2970  if (rendcirc && (rendcirc->base_.purpose ==
2972  log_info(LD_REND,
2973  "pending-join circ %u (id: %" PRIu32 ") already here, with "
2974  "intro ack. Stalling. (stream %d sec old)",
2975  (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
2976  rendcirc->global_identifier, conn_age);
2977  return 0;
2978  }
2979 
2980  /* it's on its way. find an intro circ. */
2982  conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
2983  if (retval < 0) return -1; /* failed */
2984 
2985  if (retval > 0) {
2986  /* one has already sent the intro. keep waiting. */
2987  tor_assert(introcirc);
2988  log_info(LD_REND, "Intro circ %u (id: %" PRIu32 ") present and "
2989  "awaiting ACK. Rend circuit %u (id: %" PRIu32 "). "
2990  "Stalling. (stream %d sec old)",
2991  (unsigned) TO_CIRCUIT(introcirc)->n_circ_id,
2992  introcirc->global_identifier,
2993  rendcirc ? (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id : 0,
2994  rendcirc ? rendcirc->global_identifier : 0,
2995  conn_age);
2996  return 0;
2997  }
2998 
2999  /* now rendcirc and introcirc are each either undefined or not finished */
3000 
3001  if (rendcirc && introcirc &&
3002  rendcirc->base_.purpose == CIRCUIT_PURPOSE_C_REND_READY) {
3003  log_info(LD_REND,
3004  "ready rend circ %u (id: %" PRIu32 ") already here. No"
3005  "intro-ack yet on intro %u (id: %" PRIu32 "). "
3006  "(stream %d sec old)",
3007  (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
3008  rendcirc->global_identifier,
3009  (unsigned) TO_CIRCUIT(introcirc)->n_circ_id,
3010  introcirc->global_identifier, conn_age);
3011 
3012  tor_assert(introcirc->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
3013  if (introcirc->base_.state == CIRCUIT_STATE_OPEN) {
3014  int ret;
3015  log_info(LD_REND, "Found open intro circ %u (id: %" PRIu32 "). "
3016  "Rend circuit %u (id: %" PRIu32 "); Sending "
3017  "introduction. (stream %d sec old)",
3018  (unsigned) TO_CIRCUIT(introcirc)->n_circ_id,
3019  introcirc->global_identifier,
3020  (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
3021  rendcirc->global_identifier, conn_age);
3022  ret = hs_client_send_introduce1(introcirc, rendcirc);
3023  switch (ret) {
3024  case 0: /* success */
3025  rendcirc->base_.timestamp_dirty = time(NULL);
3026  introcirc->base_.timestamp_dirty = time(NULL);
3027 
3028  pathbias_count_use_attempt(introcirc);
3029  pathbias_count_use_attempt(rendcirc);
3030 
3031  assert_circuit_ok(TO_CIRCUIT(rendcirc));
3032  assert_circuit_ok(TO_CIRCUIT(introcirc));
3033  return 0;
3034  case -1: /* transient error */
3035  return 0;
3036  case -2: /* permanent error */
3037  return -1;
3038  default: /* oops */
3040  return -1;
3041  }
3042  }
3043  }
3044 
3045  log_info(LD_REND, "Intro %u (id: %" PRIu32 ") and rend circuit %u "
3046  "(id: %" PRIu32 ") circuits are not both ready. "
3047  "Stalling conn. (%d sec old)",
3048  introcirc ? (unsigned) TO_CIRCUIT(introcirc)->n_circ_id : 0,
3049  introcirc ? introcirc->global_identifier : 0,
3050  rendcirc ? (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id : 0,
3051  rendcirc ? rendcirc->global_identifier : 0, conn_age);
3052  return 0;
3053  }
3054 }
3055 
3057 void
3058 circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
3059 {
3060  uint8_t old_purpose;
3061  /* Don't allow an OR circ to become an origin circ or vice versa. */
3062  tor_assert(!!(CIRCUIT_IS_ORIGIN(circ)) ==
3063  !!(CIRCUIT_PURPOSE_IS_ORIGIN(new_purpose)));
3064 
3065  if (circ->purpose == new_purpose) return;
3066 
3067  if (CIRCUIT_IS_ORIGIN(circ)) {
3068  char old_purpose_desc[80] = "";
3069 
3070  strncpy(old_purpose_desc, circuit_purpose_to_string(circ->purpose), 80-1);
3071  old_purpose_desc[80-1] = '\0';
3072 
3073  log_debug(LD_CIRC,
3074  "changing purpose of origin circ %d "
3075  "from \"%s\" (%d) to \"%s\" (%d)",
3076  TO_ORIGIN_CIRCUIT(circ)->global_identifier,
3077  old_purpose_desc,
3078  circ->purpose,
3079  circuit_purpose_to_string(new_purpose),
3080  new_purpose);
3081  }
3082 
3083  old_purpose = circ->purpose;
3084  circ->purpose = new_purpose;
3085 
3086  if (CIRCUIT_IS_ORIGIN(circ)) {
3088  old_purpose);
3089 
3091  }
3092 }
3093 
3095 void
3097 {
3098  const or_options_t *options = get_options();
3099  tor_assert(circ);
3100 
3101  /* XXXX This is a kludge; we're only keeping it around in case there's
3102  * something that doesn't check unusable_for_new_conns, and to avoid
3103  * deeper refactoring of our expiration logic. */
3104  if (! circ->base_.timestamp_dirty)
3105  circ->base_.timestamp_dirty = approx_time();
3106  if (options->MaxCircuitDirtiness >= circ->base_.timestamp_dirty)
3107  circ->base_.timestamp_dirty = 1; /* prevent underflow */
3108  else
3109  circ->base_.timestamp_dirty -= options->MaxCircuitDirtiness;
3110 
3111  circ->unusable_for_new_conns = 1;
3112 }
3113 
3119 void
3120 circuit_sent_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
3121 {
3122  if (!circ) return;
3123 
3124  tor_assert_nonfatal(relay_body_len <= RELAY_PAYLOAD_SIZE);
3125 
3127  tor_add_u32_nowrap(circ->n_delivered_written_circ_bw, relay_body_len);
3129  tor_add_u32_nowrap(circ->n_overhead_written_circ_bw,
3130  RELAY_PAYLOAD_SIZE-relay_body_len);
3131 }
3132 
3138 void
3139 circuit_read_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
3140 {
3141  if (!circ) return;
3142 
3143  tor_assert_nonfatal(relay_body_len <= RELAY_PAYLOAD_SIZE);
3144 
3145  circ->n_delivered_read_circ_bw =
3146  tor_add_u32_nowrap(circ->n_delivered_read_circ_bw, relay_body_len);
3147  circ->n_overhead_read_circ_bw =
3148  tor_add_u32_nowrap(circ->n_overhead_read_circ_bw,
3149  RELAY_PAYLOAD_SIZE-relay_body_len);
3150 }
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
static void circuit_testing_failed(origin_circuit_t *circ, int at_last_hop)
Definition: circuituse.c:1659
#define CIRCUIT_PURPOSE_C_INTRODUCING
Definition: circuitlist.h:74
void rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
#define DIR_PURPOSE_UPLOAD_HSDESC
Definition: directory.h:72
Header file for circuitstats.c.
int channel_is_client(const channel_t *chan)
Definition: channel.c:2913
#define CIRCLAUNCH_ONEHOP_TUNNEL
Definition: circuituse.h:39
Header file for rendcommon.c.
#define CIRCLAUNCH_IS_INTERNAL
Definition: circuituse.h:46
rend_data_t * rend_data
origin_circuit_t * circuit_launch(uint8_t purpose, int flags)
Definition: circuituse.c:1938
struct circuit_guard_state_t * guard_state
const char * channel_get_canonical_remote_descr(channel_t *chan)
Definition: channel.c:2836
#define CIRCUIT_PURPOSE_IS_ORIGIN(p)
Definition: circuitlist.h:136
#define CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
Definition: circuitlist.h:94
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:289
int rend_num_services(void)
Definition: rendservice.c:183
int connection_edge_update_circuit_isolation(const entry_connection_t *conn, origin_circuit_t *circ, int dry_run)
Header file containing common data for the whole HS subsytem.
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:87
int package_window
Definition: circuit_st.h:101
unsigned int hs_circ_has_timed_out
Header file for circuitbuild.c.
extend_info_t * extend_info_new(const char *nickname, const char *rsa_id_digest, const ed25519_public_key_t *ed_id, crypto_pk_t *onion_key, const curve25519_public_key_t *ntor_key, const tor_addr_t *addr, uint16_t port)
uint32_t n_delivered_written_circ_bw
Definition: node_st.h:28
struct smartlist_t * LongLivedPorts
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define TO_CONN(c)
Definition: or.h:735
path_state_bitfield_t path_state
static void consider_recording_trackhost(const entry_connection_t *conn, const origin_circuit_t *circ)
Definition: circuituse.c:2687
extend_info_t * extend_info_from_node(const node_t *node, int for_direct_connect)
#define IDLE_ONE_HOP_CIRC_TIMEOUT
Definition: circuituse.c:1561
void circuit_try_attaching_streams(origin_circuit_t *circ)
Definition: circuituse.c:1755
addr_policy_result_t
Definition: policies.h:38
Header file containing client data for the HS subsytem.
tor_addr_t addr
struct crypt_path_t * next
Definition: crypt_path_st.h:58
void pathbias_count_timeout(origin_circuit_t *circ)
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
int MaxCircuitDirtiness
routerset_t * EntryNodes
Definition: or_options_st.h:78
const char * extend_info_describe(const extend_info_t *ei)
Definition: describe.c:154
#define MAX_UNUSED_OPEN_CIRCUITS
Definition: circuituse.c:1085
uint16_t marked_for_close
Definition: circuit_st.h:148
Header file for connection.c.
struct or_circuit_t * rend_splice
Definition: or_circuit_st.h:48
static int optimistic_data_enabled(void)
Definition: circuituse.c:2578
#define LD_GENERAL
Definition: log.h:58
int addressmap_have_mapping(const char *address, int update_expiry)
Definition: addressmap.c:545
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:143
#define CIRCLAUNCH_NEED_UPTIME
Definition: circuituse.h:41
time_t channel_when_last_xmit(channel_t *chan)
Definition: channel.c:3288
int rend_cmp_service_ids(const char *one, const char *two)
Definition: rendcommon.c:48
addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port, const node_t *node)
Definition: policies.c:2918
uint8_t state
Definition: connection_st.h:44
const char * circuit_purpose_to_string(uint8_t purpose)
Definition: circuitlist.c:896
#define LOG_INFO
Definition: log.h:41
Header file for describe.c.
int DisablePredictedCircuits
Header file for nodelist.c.
crypt_path_t * cpath
Header file for directory.c.
int circuit_purpose_is_hidden_service(uint8_t purpose)
Definition: circuituse.c:1961
void smartlist_add(smartlist_t *sl, void *element)
#define timercmp(tv1, tv2, op)
Definition: timeval.h:59
char address[MAX_SOCKS_ADDR_LEN]
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:41
void circuit_expire_building(void)
Definition: circuituse.c:453
void hs_stats_note_service_rendezvous_launch(void)
Definition: hs_stats.c:47
#define TO_CIRCUIT(x)
Definition: or.h:947
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:84
Header file for config.c.
unsigned int relaxed_timeout
void router_do_reachability_checks(int test_or, int test_dir)
Definition: selftest.c:171
int circuit_stream_is_being_handled(entry_connection_t *conn, uint16_t port, int min)
Definition: circuituse.c:1036
int circuit_build_times_needs_circuits_now(const circuit_build_times_t *cbt)
static void circuit_expire_old_circuits_clientside(void)
Definition: circuituse.c:1475
const char * conn_type_to_string(int type)
Definition: connection.c:241
struct connection_t * linked_conn
int tor_inet_aton(const char *str, struct in_addr *addr)
Definition: inaddr.c:38
long tv_mdiff(const struct timeval *start, const struct timeval *end)
Definition: tvdiff.c:102
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:100
unsigned int reading_from_linked_conn
Definition: connection_st.h:73
const char * channel_state_to_string(channel_state_t state)
Definition: channel.c:315
uint16_t port
int circuit_event_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
Definition: circuitlist.c:496
int circuit_timeout_want_to_count_circ(const origin_circuit_t *circ)
Definition: circuitbuild.c:843
void connection_ap_mark_as_waiting_for_renddesc(entry_connection_t *entry_conn)
void circuit_build_times_count_timeout(circuit_build_times_t *cbt, int did_onehop)
static int have_performed_bandwidth_test
Definition: circuituse.c:1602
unsigned int is_ancient
#define tor_free(p)
Definition: malloc.h:52
#define tor_fragile_assert()
Definition: util_bug.h:221
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1340
#define LOG_NOTICE
Definition: log.h:46
int smartlist_contains_int_as_string(const smartlist_t *sl, int num)
Definition: smartlist.c:147
void circuit_build_needed_circs(time_t now)
Definition: circuituse.c:1335
const char * conn_state_to_string(int type, int state)
Definition: connection.c:273
#define DIR_PURPOSE_FETCH_CONSENSUS
Definition: directory.h:56
Header for tvdiff.c.
int MaxClientCircuitsPending
int circuit_get_cpath_len(origin_circuit_t *circ)
Definition: circuitlist.c:2039
uint8_t purpose
Definition: circuit_st.h:96
static void circuit_testing_opened(origin_circuit_t *circ)
Definition: circuituse.c:1641
Header file for hs_stats.c.
#define TESTING_CIRCUIT_INTERVAL
Definition: circuituse.c:1327
struct smartlist_t * TrackHostExits
int connection_ap_handshake_attach_circuit(entry_connection_t *conn)
Definition: circuituse.c:2810
origin_circuit_t * circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
Definition: circuitbuild.c:480
unsigned int purpose
Definition: connection_st.h:46
uint32_t n_overhead_read_circ_bw
#define tor_addr_from_in(dest, in)
Definition: address.h:291
int entry_guard_state_should_expire(circuit_guard_state_t *guard_state)
Definition: entrynodes.c:2744
routerset_t * ExcludeExitNodes
Definition: or_options_st.h:88
#define ENTRY_TO_CONN(c)
Definition: or.h:738
void router_perform_bandwidth_test(int num_circs, time_t now)
Definition: selftest.c:276
static int n_circuit_failures
Definition: circuituse.c:1926
void connection_ap_attach_pending(int retry)
#define CIRCUIT_PURPOSE_C_HSDIR_GET
Definition: circuitlist.h:91
#define CIRCLAUNCH_IS_V3_RP
Definition: circuituse.h:49
unsigned int linked
Definition: connection_st.h:70
int circuit_should_use_vanguards(uint8_t purpose)
Definition: circuituse.c:1993
socks_request_t * socks_request
smartlist_t * circuit_get_global_origin_circuit_list(void)
Definition: circuitlist.c:699
unsigned int num_circuits_launched
#define MIN_CIRCUITS_HANDLING_STREAM
Definition: or.h:180
struct crypt_path_t * cpath_layer
Header file for policies.c.
int control_event_circuit_purpose_changed(origin_circuit_t *circ, int old_purpose)
Definition: control.c:5784
channel_t * p_chan
Definition: or_circuit_st.h:35
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2480
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
#define LD_APP
Definition: log.h:74
Header file for channel.c.
tor_assert(buffer)
void circuit_build_times_set_timeout(circuit_build_times_t *cbt)
edge_connection_t * n_streams
Definition: or_circuit_st.h:37
void circuit_expire_old_circuits_serverside(time_t now)
Definition: circuituse.c:1568
origin_circuit_t * circuit_find_to_cannibalize(uint8_t purpose_to_produce, extend_info_t *info, int flags)
Definition: circuitlist.c:1901
#define NUM_PARALLEL_TESTING_CIRCS
Definition: circuituse.c:1598
int strcasecmpend(const char *s1, const char *s2)
Definition: util_string.c:258
void circpad_machine_event_circ_has_streams(origin_circuit_t *circ)
Header for fp.c.
time_t timestamp_dirty
Definition: circuit_st.h:146
#define END_CIRC_REASON_MEASUREMENT_EXPIRED
Definition: or.h:300
#define LD_CIRC
Definition: log.h:78
uint8_t state
Definition: circuit_st.h:95
void circuit_sent_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
Definition: circuituse.c:3120
Header file for routermode.c.
int connection_ap_can_use_exit(const entry_connection_t *conn, const node_t *exit_node)
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
routerset_t * ExcludeNodes
Definition: or_options_st.h:84
circid_t n_circ_id
Definition: circuit_st.h:67
int connection_edge_compatible_with_circuit(const entry_connection_t *conn, const origin_circuit_t *circ)
unsigned int received_destroy
Definition: circuit_st.h:93
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:77
#define ISO_SOCKSAUTH
Definition: or.h:960
Header file for circuitpadding.c.
static time_t last_expired_clientside_circuits
Definition: circuituse.c:870
#define DIGEST_LEN
Definition: digest_sizes.h:20
double get_circuit_build_timeout_ms(void)
Definition: circuitstats.c:104
uint32_t n_delivered_read_circ_bw
#define CIRCUIT_PURPOSE_TESTING
Definition: circuitlist.h:115
const char * node_describe(const node_t *node)
Definition: describe.c:101
Header file for circuitbuild.c.
#define END_CIRC_AT_ORIGIN
Definition: or.h:305
Master header file for Tor-specific functionality.
#define CIRCUIT_STATE_OPEN
Definition: circuitlist.h:31
void circuit_discard_optional_exit_enclaves(extend_info_t *info)
#define DIR_PURPOSE_FETCH_HSDESC
Definition: directory.h:74
void circpad_machine_event_circ_has_no_streams(origin_circuit_t *circ)
Header file for circuitbuild.c.
connection_t * connection_get_by_type(int type)
Definition: connection.c:4461
static void circuit_increment_failure_count(void)
Definition: circuituse.c:2179
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1196
void circuit_build_times_mark_circ_as_measurement_only(origin_circuit_t *circ)
Definition: circuitstats.c:640
uint16_t marked_for_close
unsigned int chosen_exit_optional
#define AP_CONN_STATE_CIRCUIT_WAIT
unsigned int is_bad_for_new_circs
Definition: channel.h:425
int hexdigest_to_digest(const char *hexdigest, char *digest)
Definition: routerlist.c:663
int rep_hist_get_predicted_internal(time_t now, int *need_uptime, int *need_capacity)
Header file containing circuit and connection identifier data for the whole HS subsytem.
#define LOG_WARN
Definition: log.h:49
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:769
unsigned int type
Definition: connection_st.h:45
unsigned int edge_has_sent_end
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:260
static int count_pending_general_client_circuits(void)
Definition: circuituse.c:393
Header file for circuituse.c.
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:476
static int circuit_get_open_circ_or_launch(entry_connection_t *conn, uint8_t desired_circuit_purpose, origin_circuit_t **circp)
Definition: circuituse.c:2206
void circuit_clear_isolation(origin_circuit_t *circ)
const node_t * build_state_get_exit_node(cpath_build_state_t *state)
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:82
void smartlist_del(smartlist_t *sl, int idx)
void addressmap_clean(time_t now)
Definition: addressmap.c:321
circid_t p_circ_id
Definition: or_circuit_st.h:31
void circuit_remove_handled_ports(smartlist_t *needed_ports)
Definition: circuituse.c:1011
#define CIRCUIT_PURPOSE_S_CONNECT_REND
Definition: circuitlist.h:106
#define LD_REND
Definition: log.h:80
routerset_t * HSLayer2Nodes
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_PATH_BIAS_TESTING
Definition: circuitlist.h:119
void circuit_expire_old_circs_as_needed(time_t now)
Definition: circuituse.c:1358
Header file for rendservice.c.
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:358
static int connection_ap_get_nonrend_circ_purpose(const entry_connection_t *conn)
Definition: circuituse.c:2777
#define LD_OR
Definition: log.h:88
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
void circuit_expire_waiting_for_better_guard(void)
Definition: circuituse.c:855
int hostname_in_track_host_exits(const or_options_t *options, const char *address)
Definition: circuituse.c:2665
#define CIRCLAUNCH_NEED_CAPACITY
Definition: circuituse.h:43
int pathbias_check_close(origin_circuit_t *ocirc, int reason)
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
#define CIRCUIT_PURPOSE_S_INTRO
Definition: circuitlist.h:103
int control_event_circuit_cannibalized(origin_circuit_t *circ, int old_purpose, const struct timeval *old_tv_created)
Definition: control.c:5798
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
#define LD_DIR
Definition: log.h:84
#define CIRCUIT_PURPOSE_HS_VANGUARDS
Definition: circuitlist.h:127
void addressmap_register(const char *address, char *new_address, time_t expires, addressmap_entry_source_t source, const int wildcard_addr, const int wildcard_new_addr)
Definition: addressmap.c:577
Header file containing circuit data for the whole HS subsytem.
struct crypt_path_t * prev
Definition: crypt_path_st.h:61
cpath_build_state_t * build_state
int circuit_build_times_enough_to_compute(const circuit_build_times_t *cbt)
Definition: circuitstats.c:258
Header file for connection_edge.c.
void circuit_log_ancient_one_hop_circuits(int age)
Definition: circuituse.c:879
int TrackHostExitsExpire
routerset_t * HSLayer3Nodes
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:163
void circuit_has_opened(origin_circuit_t *circ)
Definition: circuituse.c:1681
const char * circuit_state_to_string(int state)
Definition: circuitlist.c:758
#define MAX_CIRCUIT_FAILURES
Definition: circuituse.c:1933
static int circuit_try_clearing_isolation_state(origin_circuit_t *circ)
Definition: circuituse.c:1733
struct hs_ident_circuit_t * hs_ident
static int cpath_is_on_circuit(origin_circuit_t *circ, crypt_path_t *crypt_path)
Definition: circuituse.c:2565
origin_circuit_t * circuit_launch_by_extend_info(uint8_t purpose, extend_info_t *extend_info, int flags)
Definition: circuituse.c:2063
edge_connection_t * p_streams
char identity_digest[DIGEST_LEN]
unsigned int has_opened
#define CIRCUIT_PURPOSE_S_HSDIR_POST
Definition: circuitlist.h:111
const char * escaped(const char *s)
Definition: escape.c:126
#define DIR_PURPOSE_UPLOAD_RENDDESC_V2
Definition: directory.h:65
edge_connection_t * resolving_streams
Definition: or_circuit_st.h:40
void hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
Definition: hs_client.c:1251
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
Definition: circuitlist.h:77
void routerlist_retry_directory_downloads(time_t now)
Definition: routerlist.c:2223
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:89
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:1354
uint8_t state
Definition: crypt_path_st.h:54
void circuit_build_failed(origin_circuit_t *circ)
Definition: circuituse.c:1772
circuit_build_times_t * get_circuit_build_times_mutable(void)
Definition: circuitstats.c:88
struct timeval timestamp_began
Definition: circuit_st.h:124
#define log_fn(severity, domain, args,...)
Definition: log.h:255
unsigned int isolation_values_set
#define AP_CONN_STATE_CONTROLLER_WAIT
int circuit_build_times_count_close(circuit_build_times_t *cbt, int did_onehop, time_t start_time)
static int circuit_should_cannibalize_to_build(uint8_t purpose_to_build, int has_extend_info, int onehop_tunnel)
Definition: circuituse.c:2015
void mark_circuit_unusable_for_new_conns(origin_circuit_t *circ)
Definition: circuituse.c:3096
static void circuit_launch_predicted_hs_circ(int flags)
Definition: circuituse.c:1222
int connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
Definition: circuituse.c:2725
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:151
Header file for control.c.
#define tor_addr_eq(a, b)
Definition: address.h:244
double get_circuit_build_close_time_ms(void)
Definition: circuitstats.c:96
void circuit_reset_failure_count(int timeout)
Definition: circuituse.c:2190
unsigned int hold_open_until_flushed
Definition: connection_st.h:56
unsigned int socks_iso_keep_alive
const circuit_build_times_t * get_circuit_build_times(void)
Definition: circuitstats.c:81
#define CIRCUIT_PURPOSE_C_GENERAL
Definition: circuitlist.h:71
time_t approx_time(void)
Definition: approx_time.c:32
void connection_ap_fail_onehop(const char *failed_digest, cpath_build_state_t *build_state)
unsigned int isolation_any_streams_attached
smartlist_t * prepend_policy
#define LOG_DEBUG
Definition: log.h:38
int router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port, int need_uptime)
Definition: nodelist.c:2210
void pathbias_count_use_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:596
static int circuit_is_better(const origin_circuit_t *oa, const origin_circuit_t *ob, const entry_connection_t *conn)
Definition: circuituse.c:245
const char * marked_for_close_file
time_t timestamp_created
extend_info_t * extend_info
Definition: crypt_path_st.h:47
int n_bits_set_u8(uint8_t v)
Definition: bits.c:72
void circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
Definition: circuituse.c:3058
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:504
Header file for selftest.c.
struct timeval timestamp_created
Definition: circuit_st.h:127
#define MAX(a, b)
Definition: cmp.h:22
#define CONN_TYPE_DIR
Definition: connection.h:35
struct circuit_t * on_circuit
int entry_list_is_constrained(const or_options_t *options)
Definition: entrynodes.c:3270
unsigned int edge_blocked_on_circ
void reset_bandwidth_test(void)
Definition: circuituse.c:1607
void circuit_read_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
Definition: circuituse.c:3139
void circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
Definition: circuituse.c:1389
int connection_ap_handshake_send_resolve(entry_connection_t *ap_conn)
uint32_t n_overhead_written_circ_bw
extend_info_t * chosen_exit
static origin_circuit_t * circuit_get_best(const entry_connection_t *conn, int must_be_open, uint8_t purpose, int need_uptime, int need_internal)
Definition: circuituse.c:337
struct edge_connection_t * next_stream
tor_addr_t addr
unsigned int may_use_optimistic_data
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:109
#define CONN_TYPE_AP
Definition: connection.h:31
Header file for rendclient.c.
int circuit_any_opened_circuits(void)
Definition: circuitlist.c:714
int circuit_enough_testing_circs(void)
Definition: circuituse.c:1617
static void link_apconn_to_circ(entry_connection_t *apconn, origin_circuit_t *circ, crypt_path_t *cpath)
Definition: circuituse.c:2597
rend_data_t * rend_data
int guards_retry_optimistic(const or_options_t *options)
Definition: entrynodes.c:3708
void rend_client_refetch_v2_renddesc(rend_data_t *rend_query)
Definition: rendclient.c:697
static void circuit_predict_and_launch_new(void)
Definition: circuituse.c:1241
const char * build_state_get_exit_nickname(cpath_build_state_t *state)
int proxy_mode(const or_options_t *options)
Definition: routermode.c:30
static int circuit_matches_with_rend_stream(const edge_connection_t *edge_conn, const origin_circuit_t *origin_circ)
Definition: circuituse.c:80
#define DIR_PURPOSE_FETCH_RENDDESC_V2
Definition: directory.h:68
int check_whether_orport_reachable(const or_options_t *options)
Definition: selftest.c:74
void connection_ap_rescan_and_attach_pending(void)
#define CIRCUIT_PURPOSE_COUNTS_TOWARDS_MAXPENDING(p)
Definition: circuitlist.h:156
#define LD_HEARTBEAT
Definition: log.h:99
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:82
int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
static int circuit_is_acceptable(const origin_circuit_t *origin_circ, const entry_connection_t *conn, int must_be_open, uint8_t purpose, int need_uptime, int need_internal, time_t now)
Definition: circuituse.c:110
Header file for routerlist.c.
static int did_circs_fail_last_period
Definition: circuituse.c:1929
int circuit_build_times_disabled(const or_options_t *options)
Definition: circuitstats.c:120
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1097
channel_t * n_chan
Definition: circuit_st.h:58
void circpad_machine_event_circ_purpose_changed(origin_circuit_t *circ)