tor  0.4.1.0-alpha-dev
circuitbuild.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 
28 #define CIRCUITBUILD_PRIVATE
29 #define OCIRC_EVENT_PRIVATE
30 
31 #include "core/or/or.h"
32 #include "app/config/config.h"
33 #include "app/config/confparse.h"
34 #include "core/crypto/hs_ntor.h"
36 #include "core/crypto/onion_fast.h"
37 #include "core/crypto/onion_tap.h"
38 #include "core/crypto/relay_crypto.h"
40 #include "core/mainloop/mainloop.h"
41 #include "core/or/channel.h"
42 #include "core/or/circuitbuild.h"
43 #include "core/or/circuitlist.h"
44 #include "core/or/circuitstats.h"
45 #include "core/or/circuituse.h"
46 #include "core/or/circuitpadding.h"
47 #include "core/or/command.h"
49 #include "core/or/connection_or.h"
50 #include "core/or/onion.h"
51 #include "core/or/ocirc_event.h"
52 #include "core/or/policies.h"
53 #include "core/or/relay.h"
54 #include "feature/client/bridges.h"
55 #include "feature/client/circpathbias.h"
67 #include "feature/nodelist/routerset.h"
68 #include "feature/relay/router.h"
70 #include "feature/relay/selftest.h"
72 #include "feature/stats/predict_ports.h"
74 
75 #include "core/or/cell_st.h"
76 #include "core/or/cpath_build_state_st.h"
77 #include "core/or/entry_connection_st.h"
78 #include "core/or/extend_info_st.h"
79 #include "feature/nodelist/node_st.h"
80 #include "core/or/or_circuit_st.h"
81 #include "core/or/origin_circuit_st.h"
82 #include "feature/nodelist/microdesc_st.h"
83 #include "feature/nodelist/routerinfo_st.h"
84 #include "feature/nodelist/routerstatus_st.h"
85 
87  uint16_t port,
88  const char *id_digest,
89  const ed25519_public_key_t *ed_id);
90 static int circuit_deliver_create_cell(circuit_t *circ,
91  const create_cell_t *create_cell,
92  int relayed);
94 STATIC int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
98  crypt_path_t *hop);
99 static const node_t *choose_good_middle_server(uint8_t purpose,
100  cpath_build_state_t *state,
101  crypt_path_t *head,
102  int cur_len);
103 
108 static channel_t *
109 channel_connect_for_circuit(const tor_addr_t *addr, uint16_t port,
110  const char *id_digest,
111  const ed25519_public_key_t *ed_id)
112 {
113  channel_t *chan;
114 
115  chan = channel_connect(addr, port, id_digest, ed_id);
116  if (chan) command_setup_channel(chan);
117 
118  return chan;
119 }
120 
127 STATIC circid_t
129 {
130 /* This number is chosen somewhat arbitrarily; see comment below for more
131  * info. When the space is 80% full, it gives a one-in-a-million failure
132  * chance; when the space is 90% full, it gives a one-in-850 chance; and when
133  * the space is 95% full, it gives a one-in-26 failure chance. That seems
134  * okay, though you could make a case IMO for anything between N=32 and
135  * N=256. */
136 #define MAX_CIRCID_ATTEMPTS 64
137  int in_use;
138  unsigned n_with_circ = 0, n_pending_destroy = 0, n_weird_pending_destroy = 0;
139  circid_t test_circ_id;
140  circid_t attempts=0;
141  circid_t high_bit, max_range, mask;
142  int64_t pending_destroy_time_total = 0;
143  int64_t pending_destroy_time_max = 0;
144 
145  tor_assert(chan);
146 
147  if (chan->circ_id_type == CIRC_ID_TYPE_NEITHER) {
148  log_warn(LD_BUG,
149  "Trying to pick a circuit ID for a connection from "
150  "a client with no identity.");
151  return 0;
152  }
153  max_range = (chan->wide_circ_ids) ? (1u<<31) : (1u<<15);
154  mask = max_range - 1;
155  high_bit = (chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ? max_range : 0;
156  do {
157  if (++attempts > MAX_CIRCID_ATTEMPTS) {
158  /* Make sure we don't loop forever because all circuit IDs are used.
159  *
160  * Once, we would try until we had tried every possible circuit ID. But
161  * that's quite expensive. Instead, we try MAX_CIRCID_ATTEMPTS random
162  * circuit IDs, and then give up.
163  *
164  * This potentially causes us to give up early if our circuit ID space
165  * is nearly full. If we have N circuit IDs in use, then we will reject
166  * a new circuit with probability (N / max_range) ^ MAX_CIRCID_ATTEMPTS.
167  * This means that in practice, a few percent of our circuit ID capacity
168  * will go unused.
169  *
170  * The alternative here, though, is to do a linear search over the
171  * whole circuit ID space every time we extend a circuit, which is
172  * not so great either.
173  */
174  int64_t queued_destroys;
176  approx_time());
177  if (m == NULL)
178  return 0; /* This message has been rate-limited away. */
179  if (n_pending_destroy)
180  pending_destroy_time_total /= n_pending_destroy;
181  log_warn(LD_CIRC,"No unused circIDs found on channel %s wide "
182  "circID support, with %u inbound and %u outbound circuits. "
183  "Found %u circuit IDs in use by circuits, and %u with "
184  "pending destroy cells. (%u of those were marked bogusly.) "
185  "The ones with pending destroy cells "
186  "have been marked unusable for an average of %ld seconds "
187  "and a maximum of %ld seconds. This channel is %ld seconds "
188  "old. Failing a circuit.%s",
189  chan->wide_circ_ids ? "with" : "without",
190  chan->num_p_circuits, chan->num_n_circuits,
191  n_with_circ, n_pending_destroy, n_weird_pending_destroy,
192  (long)pending_destroy_time_total,
193  (long)pending_destroy_time_max,
194  (long)(approx_time() - chan->timestamp_created),
195  m);
196  tor_free(m);
197 
198  if (!chan->cmux) {
199  /* This warning should be impossible. */
200  log_warn(LD_BUG, " This channel somehow has no cmux on it!");
201  return 0;
202  }
203 
204  /* analysis so far on 12184 suggests that we're running out of circuit
205  IDs because it looks like we have too many pending destroy
206  cells. Let's see how many we really have pending.
207  */
208  queued_destroys = circuitmux_count_queued_destroy_cells(chan,
209  chan->cmux);
210 
211  log_warn(LD_CIRC, " Circuitmux on this channel has %u circuits, "
212  "of which %u are active. It says it has %"PRId64
213  " destroy cells queued.",
216  (queued_destroys));
217 
218  /* Change this into "if (1)" in order to get more information about
219  * possible failure modes here. You'll need to know how to use gdb with
220  * Tor: this will make Tor exit with an assertion failure if the cmux is
221  * corrupt. */
222  if (0)
223  circuitmux_assert_okay(chan->cmux);
224 
225  channel_dump_statistics(chan, LOG_WARN);
226 
227  return 0;
228  }
229 
230  do {
231  crypto_rand((char*) &test_circ_id, sizeof(test_circ_id));
232  test_circ_id &= mask;
233  } while (test_circ_id == 0);
234 
235  test_circ_id |= high_bit;
236 
237  in_use = circuit_id_in_use_on_channel(test_circ_id, chan);
238  if (in_use == 1)
239  ++n_with_circ;
240  else if (in_use == 2) {
241  time_t since_when;
242  ++n_pending_destroy;
243  since_when =
244  circuit_id_when_marked_unusable_on_channel(test_circ_id, chan);
245  if (since_when) {
246  time_t waiting = approx_time() - since_when;
247  pending_destroy_time_total += waiting;
248  if (waiting > pending_destroy_time_max)
249  pending_destroy_time_max = waiting;
250  } else {
251  ++n_weird_pending_destroy;
252  }
253  }
254  } while (in_use);
255  return test_circ_id;
256 }
257 
264 static char *
265 circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
266 {
267  crypt_path_t *hop;
268  smartlist_t *elements;
269  const char *states[] = {"closed", "waiting for keys", "open"};
270  char *s;
271 
272  elements = smartlist_new();
273 
274  if (verbose) {
275  const char *nickname = build_state_get_exit_nickname(circ->build_state);
276  smartlist_add_asprintf(elements, "%s%s circ (length %d%s%s):",
277  circ->build_state->is_internal ? "internal" : "exit",
278  circ->build_state->need_uptime ? " (high-uptime)" : "",
280  circ->base_.state == CIRCUIT_STATE_OPEN ? "" : ", last hop ",
281  circ->base_.state == CIRCUIT_STATE_OPEN ? "" :
282  (nickname?nickname:"*unnamed*"));
283  }
284 
285  hop = circ->cpath;
286  do {
287  char *elt;
288  const char *id;
289  const node_t *node;
290  if (!hop)
291  break;
292  if (!verbose && hop->state != CPATH_STATE_OPEN)
293  break;
294  if (!hop->extend_info)
295  break;
296  id = hop->extend_info->identity_digest;
297  if (verbose_names) {
298  elt = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
299  if ((node = node_get_by_id(id))) {
300  node_get_verbose_nickname(node, elt);
301  } else if (is_legal_nickname(hop->extend_info->nickname)) {
302  elt[0] = '$';
303  base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
304  elt[HEX_DIGEST_LEN+1]= '~';
305  strlcpy(elt+HEX_DIGEST_LEN+2,
307  } else {
308  elt[0] = '$';
309  base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
310  }
311  } else { /* ! verbose_names */
312  elt = tor_malloc(HEX_DIGEST_LEN+2);
313  elt[0] = '$';
314  base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
315  }
316  tor_assert(elt);
317  if (verbose) {
318  tor_assert(hop->state <= 2);
319  smartlist_add_asprintf(elements,"%s(%s)",elt,states[hop->state]);
320  tor_free(elt);
321  } else {
322  smartlist_add(elements, elt);
323  }
324  hop = hop->next;
325  } while (hop != circ->cpath);
326 
327  s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
328  SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
329  smartlist_free(elements);
330  return s;
331 }
332 
338 char *
340 {
341  return circuit_list_path_impl(circ, verbose, 0);
342 }
343 
347 char *
349 {
350  return circuit_list_path_impl(circ, 0, 1);
351 }
352 
357 void
358 circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
359 {
360  char *s = circuit_list_path(circ,1);
361  tor_log(severity,domain,"%s",s);
362  tor_free(s);
363 }
364 
366 static int
368 {
369  crypt_path_t *head, *cpath;
370 
371  cpath = head = circ->cpath;
372  do {
373  /* if the extend_info is missing, we can't tell if it supports ntor */
374  if (!cpath->extend_info) {
375  return 0;
376  }
377 
378  /* if the key is blank, it definitely doesn't support ntor */
379  if (!extend_info_supports_ntor(cpath->extend_info)) {
380  return 0;
381  }
382  cpath = cpath->next;
383  } while (cpath != head);
384 
385  return 1;
386 }
387 
390 static int
392 {
393  int r = 0;
394 
395  /* onion_extend_cpath assumes these are non-NULL */
396  tor_assert(circ);
397  tor_assert(circ->build_state);
398 
399  while (r == 0) {
400  r = onion_extend_cpath(circ);
401  if (r < 0) {
402  log_info(LD_CIRC,"Generating cpath hop failed.");
403  return -1;
404  }
405  }
406 
407  /* The path is complete */
408  tor_assert(r == 1);
409 
410  /* Does every node in this path support ntor? */
411  int path_supports_ntor = circuit_cpath_supports_ntor(circ);
412 
413  /* We would like every path to support ntor, but we have to allow for some
414  * edge cases. */
416  if (circuit_can_use_tap(circ)) {
417  /* Circuits from clients to intro points, and hidden services to rend
418  * points do not support ntor, because the hidden service protocol does
419  * not include ntor onion keys. This is also true for Single Onion
420  * Services. */
421  return 0;
422  }
423 
424  if (circuit_get_cpath_len(circ) == 1) {
425  /* Allow for bootstrapping: when we're fetching directly from a fallback,
426  * authority, or bridge, we have no way of knowing its ntor onion key
427  * before we connect to it. So instead, we try connecting, and end up using
428  * CREATE_FAST. */
429  tor_assert(circ->cpath);
430  tor_assert(circ->cpath->extend_info);
431  const node_t *node = node_get_by_id(
433  /* If we don't know the node and its descriptor, we must be bootstrapping.
434  */
435  if (!node || !node_has_preferred_descriptor(node, 1)) {
436  return 0;
437  }
438  }
439 
440  if (BUG(!path_supports_ntor)) {
441  /* If we're building a multi-hop path, and it's not one of the HS or
442  * bootstrapping exceptions, and it doesn't support ntor, something has
443  * gone wrong. */
444  return -1;
445  }
446 
447  return 0;
448 }
449 
454 origin_circuit_init(uint8_t purpose, int flags)
455 {
456  /* sets circ->p_circ_id and circ->p_chan */
459  circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
460  circ->build_state->onehop_tunnel =
461  ((flags & CIRCLAUNCH_ONEHOP_TUNNEL) ? 1 : 0);
462  circ->build_state->need_uptime =
463  ((flags & CIRCLAUNCH_NEED_UPTIME) ? 1 : 0);
464  circ->build_state->need_capacity =
465  ((flags & CIRCLAUNCH_NEED_CAPACITY) ? 1 : 0);
466  circ->build_state->is_internal =
467  ((flags & CIRCLAUNCH_IS_INTERNAL) ? 1 : 0);
468  circ->base_.purpose = purpose;
469  return circ;
470 }
471 
480 circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
481 {
482  origin_circuit_t *circ;
483  int err_reason = 0;
484  int is_hs_v3_rp_circuit = 0;
485 
486  if (flags & CIRCLAUNCH_IS_V3_RP) {
487  is_hs_v3_rp_circuit = 1;
488  }
489 
490  circ = origin_circuit_init(purpose, flags);
491 
492  if (onion_pick_cpath_exit(circ, exit_ei, is_hs_v3_rp_circuit) < 0 ||
493  onion_populate_cpath(circ) < 0) {
494  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
495  return NULL;
496  }
497 
498  circuit_event_status(circ, CIRC_EVENT_LAUNCHED, 0);
499 
500  if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
501  circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
502  return NULL;
503  }
504  return circ;
505 }
506 
508 circuit_guard_state_t *
510 {
511  return circ->guard_state;
512 }
513 
523 static void
525 {
526  ocirc_event_msg_t msg;
527 
528  msg.type = OCIRC_MSGTYPE_CHAN;
529  msg.u.chan.gid = circ->global_identifier;
530  msg.u.chan.chan = chan->global_identifier;
531  msg.u.chan.onehop = circ->build_state->onehop_tunnel;
532 
533  ocirc_event_publish(&msg);
534 }
535 
540 int
542 {
543  crypt_path_t *firsthop;
544  channel_t *n_chan;
545  int err_reason = 0;
546  const char *msg = NULL;
547  int should_launch = 0;
548  const or_options_t *options = get_options();
549 
550  firsthop = onion_next_hop_in_cpath(circ->cpath);
551  tor_assert(firsthop);
552  tor_assert(firsthop->extend_info);
553 
554  /* Some bridges are on private addresses. Others pass a dummy private
555  * address to the pluggable transport, which ignores it.
556  * Deny the connection if:
557  * - the address is internal, and
558  * - we're not connecting to a configured bridge, and
559  * - we're not configured to allow extends to private addresses. */
560  if (tor_addr_is_internal(&firsthop->extend_info->addr, 0) &&
562  !options->ExtendAllowPrivateAddresses) {
563  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
564  "Client asked me to connect directly to a private address");
565  return -END_CIRC_REASON_TORPROTOCOL;
566  }
567 
568  /* now see if we're already connected to the first OR in 'route' */
569  log_debug(LD_CIRC,"Looking for firsthop '%s'",
570  fmt_addrport(&firsthop->extend_info->addr,
571  firsthop->extend_info->port));
572 
574  &firsthop->extend_info->ed_identity,
575  &firsthop->extend_info->addr,
576  &msg,
577  &should_launch);
578 
579  if (!n_chan) {
580  /* not currently connected in a useful way. */
581  log_info(LD_CIRC, "Next router is %s: %s",
582  safe_str_client(extend_info_describe(firsthop->extend_info)),
583  msg?msg:"???");
584  circ->base_.n_hop = extend_info_dup(firsthop->extend_info);
585 
586  if (should_launch) {
588  &firsthop->extend_info->addr,
589  firsthop->extend_info->port,
590  firsthop->extend_info->identity_digest,
591  &firsthop->extend_info->ed_identity);
592  if (!n_chan) { /* connect failed, forget the whole thing */
593  log_info(LD_CIRC,"connect to firsthop failed. Closing.");
594  return -END_CIRC_REASON_CONNECTFAILED;
595  }
596  circuit_chan_publish(circ, n_chan);
597  }
598 
599  log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
600  /* return success. The onion/circuit/etc will be taken care of
601  * automatically (may already have been) whenever n_chan reaches
602  * OR_CONN_STATE_OPEN.
603  */
604  return 0;
605  } else { /* it's already open. use it. */
606  tor_assert(!circ->base_.n_hop);
607  circ->base_.n_chan = n_chan;
608  circuit_chan_publish(circ, n_chan);
609  log_debug(LD_CIRC,"Conn open. Delivering first onion skin.");
610  if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
611  log_info(LD_CIRC,"circuit_send_next_onion_skin failed.");
612  circ->base_.n_chan = NULL;
613  return err_reason;
614  }
615  }
616  return 0;
617 }
618 
628 void
629 circuit_n_chan_done(channel_t *chan, int status, int close_origin_circuits)
630 {
631  smartlist_t *pending_circs;
632  int err_reason = 0;
633 
634  tor_assert(chan);
635 
636  log_debug(LD_CIRC,"chan to %s, status=%d",
637  channel_get_canonical_remote_descr(chan), status);
638 
639  pending_circs = smartlist_new();
640  circuit_get_all_pending_on_channel(pending_circs, chan);
641 
642  SMARTLIST_FOREACH_BEGIN(pending_circs, circuit_t *, circ)
643  {
644  /* These checks are redundant wrt get_all_pending_on_or_conn, but I'm
645  * leaving them in in case it's possible for the status of a circuit to
646  * change as we're going down the list. */
647  if (circ->marked_for_close || circ->n_chan || !circ->n_hop ||
648  circ->state != CIRCUIT_STATE_CHAN_WAIT)
649  continue;
650 
651  if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
652  /* Look at addr/port. This is an unkeyed connection. */
653  if (!channel_matches_extend_info(chan, circ->n_hop))
654  continue;
655  } else {
656  /* We expected a key. See if it's the right one. */
657  if (tor_memneq(chan->identity_digest,
658  circ->n_hop->identity_digest, DIGEST_LEN))
659  continue;
660  }
661  if (!status) { /* chan failed; close circ */
662  log_info(LD_CIRC,"Channel failed; closing circ.");
663  circuit_mark_for_close(circ, END_CIRC_REASON_CHANNEL_CLOSED);
664  continue;
665  }
666  if (close_origin_circuits && CIRCUIT_IS_ORIGIN(circ)) {
667  log_info(LD_CIRC,"Channel deprecated for origin circs; closing circ.");
668  circuit_mark_for_close(circ, END_CIRC_REASON_CHANNEL_CLOSED);
669  continue;
670  }
671  log_debug(LD_CIRC, "Found circ, sending create cell.");
672  /* circuit_deliver_create_cell will set n_circ_id and add us to
673  * chan_circuid_circuit_map, so we don't need to call
674  * set_circid_chan here. */
675  circ->n_chan = chan;
676  extend_info_free(circ->n_hop);
677  circ->n_hop = NULL;
678 
679  if (CIRCUIT_IS_ORIGIN(circ)) {
680  if ((err_reason =
682  log_info(LD_CIRC,
683  "send_next_onion_skin failed; circuit marked for closing.");
684  circuit_mark_for_close(circ, -err_reason);
685  continue;
686  /* XXX could this be bad, eg if next_onion_skin failed because conn
687  * died? */
688  }
689  } else {
690  /* pull the create cell out of circ->n_chan_create_cell, and send it */
691  tor_assert(circ->n_chan_create_cell);
692  if (circuit_deliver_create_cell(circ, circ->n_chan_create_cell, 1)<0) {
693  circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
694  continue;
695  }
696  tor_free(circ->n_chan_create_cell);
698  }
699  }
700  SMARTLIST_FOREACH_END(circ);
701 
702  smartlist_free(pending_circs);
703 }
704 
712 static int
714  int relayed)
715 {
716  cell_t cell;
717  circid_t id;
718  int r;
719 
720  tor_assert(circ);
721  tor_assert(circ->n_chan);
722  tor_assert(create_cell);
723  tor_assert(create_cell->cell_type == CELL_CREATE ||
724  create_cell->cell_type == CELL_CREATE_FAST ||
725  create_cell->cell_type == CELL_CREATE2);
726 
728  if (!id) {
729  static ratelim_t circid_warning_limit = RATELIM_INIT(9600);
730  log_fn_ratelim(&circid_warning_limit, LOG_WARN, LD_CIRC,
731  "failed to get unique circID.");
732  goto error;
733  }
734 
735  memset(&cell, 0, sizeof(cell_t));
736  r = relayed ? create_cell_format_relayed(&cell, create_cell)
737  : create_cell_format(&cell, create_cell);
738  if (r < 0) {
739  log_warn(LD_CIRC,"Couldn't format create cell");
740  goto error;
741  }
742  log_debug(LD_CIRC,"Chosen circID %u.", (unsigned)id);
743  circuit_set_n_circid_chan(circ, id, circ->n_chan);
744  cell.circ_id = circ->n_circ_id;
745 
746  append_cell_to_circuit_queue(circ, circ->n_chan, &cell,
747  CELL_DIRECTION_OUT, 0);
748 
749  if (CIRCUIT_IS_ORIGIN(circ)) {
750  /* Update began timestamp for circuits starting their first hop */
751  if (TO_ORIGIN_CIRCUIT(circ)->cpath->state == CPATH_STATE_CLOSED) {
752  if (!CHANNEL_IS_OPEN(circ->n_chan)) {
753  log_warn(LD_CIRC,
754  "Got first hop for a circuit without an opened channel. "
755  "State: %s.", channel_state_to_string(circ->n_chan->state));
757  }
758 
759  tor_gettimeofday(&circ->timestamp_began);
760  }
761 
762  /* mark it so it gets better rate limiting treatment. */
764  }
765 
766  return 0;
767  error:
768  circ->n_chan = NULL;
769  return -1;
770 }
771 
775 int
777 {
778  char dirbuf[128];
779  char *address;
780  const routerinfo_t *me = router_get_my_routerinfo();
781  if (!me)
782  return 0;
783  address = tor_dup_ip(me->addr);
785  "CHECKING_REACHABILITY ORADDRESS=%s:%d",
786  address, me->or_port);
787  if (me->dir_port) {
788  tor_snprintf(dirbuf, sizeof(dirbuf), " and DirPort %s:%d",
789  address, me->dir_port);
791  "CHECKING_REACHABILITY DIRADDRESS=%s:%d",
792  address, me->dir_port);
793  }
794  log_notice(LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
795  "(this may take up to %d minutes -- look for log "
796  "messages indicating success)",
797  address, me->or_port,
798  me->dir_port ? dirbuf : "",
799  me->dir_port ? "are" : "is",
801 
802  tor_free(address);
803  return 1;
804 }
805 
808 static inline int
810 {
811  const or_options_t *options = get_options();
812  tor_assert(circ->cpath);
813  tor_assert(circ->cpath->extend_info);
814 
815  if (!circuit_has_usable_onion_key(circ)) {
816  /* We don't have ntor, and we don't have or can't use TAP,
817  * so our hand is forced: only a create_fast will work. */
818  return 1;
819  }
820  if (public_server_mode(options)) {
821  /* We're a server, and we have a usable onion key. We can choose.
822  * Prefer to blend our circuit into the other circuits we are
823  * creating on behalf of others. */
824  return 0;
825  }
826  return networkstatus_get_param(NULL, "usecreatefast", 0, 0, 1);
827 }
828 
842 int
844 {
845  return !circ->has_opened
848 }
849 
857 static void
858 circuit_pick_create_handshake(uint8_t *cell_type_out,
859  uint16_t *handshake_type_out,
860  const extend_info_t *ei)
861 {
862  /* torspec says: In general, clients SHOULD use CREATE whenever they are
863  * using the TAP handshake, and CREATE2 otherwise. */
864  if (extend_info_supports_ntor(ei)) {
865  *cell_type_out = CELL_CREATE2;
866  *handshake_type_out = ONION_HANDSHAKE_TYPE_NTOR;
867  } else {
868  /* XXXX030 Remove support for deciding to use TAP and EXTEND. */
869  *cell_type_out = CELL_CREATE;
870  *handshake_type_out = ONION_HANDSHAKE_TYPE_TAP;
871  }
872 }
873 
883 static void
884 circuit_pick_extend_handshake(uint8_t *cell_type_out,
885  uint8_t *create_cell_type_out,
886  uint16_t *handshake_type_out,
887  const extend_info_t *ei)
888 {
889  uint8_t t;
890  circuit_pick_create_handshake(&t, handshake_type_out, ei);
891 
892  /* torspec says: Clients SHOULD use the EXTEND format whenever sending a TAP
893  * handshake... In other cases, clients SHOULD use EXTEND2. */
894  if (*handshake_type_out != ONION_HANDSHAKE_TYPE_TAP) {
895  *cell_type_out = RELAY_COMMAND_EXTEND2;
896  *create_cell_type_out = CELL_CREATE2;
897  } else {
898  /* XXXX030 Remove support for deciding to use TAP and EXTEND. */
899  *cell_type_out = RELAY_COMMAND_EXTEND;
900  *create_cell_type_out = CELL_CREATE;
901  }
902 }
903 
909 static int
911 {
912  switch (purpose) {
915  /* Testing circuits may omit guards because they're measuring
916  * liveness or performance, and don't want guards to interfere. */
917  return 1;
918  default:
919  /* All other multihop circuits should use guards if guards are
920  * enabled. */
921  return 0;
922  }
923 }
924 
938 int
940 {
941  tor_assert(circ);
942 
943  if (circ->cpath->state == CPATH_STATE_CLOSED) {
944  /* Case one: we're on the first hop. */
945  return circuit_send_first_onion_skin(circ);
946  }
947 
948  tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
949  tor_assert(circ->base_.state == CIRCUIT_STATE_BUILDING);
950 
953 
955 
956  if (hop) {
957  /* Case two: we're on a hop after the first. */
958  return circuit_send_intermediate_onion_skin(circ, hop);
959  }
960 
961  /* Case three: the circuit is finished. Do housekeeping tasks on it. */
963  return circuit_build_no_more_hops(circ);
964 }
965 
972 static int
974 {
975  int fast;
976  int len;
977  const node_t *node;
978  create_cell_t cc;
979  memset(&cc, 0, sizeof(cc));
980 
981  log_debug(LD_CIRC,"First skin; sending create cell.");
982 
983  if (circ->build_state->onehop_tunnel) {
984  control_event_bootstrap(BOOTSTRAP_STATUS_ONEHOP_CREATE, 0);
985  } else {
986  control_event_bootstrap(BOOTSTRAP_STATUS_CIRCUIT_CREATE, 0);
987 
988  /* If this is not a one-hop tunnel, the channel is being used
989  * for traffic that wants anonymity and protection from traffic
990  * analysis (such as netflow record retention). That means we want
991  * to pad it.
992  */
993  if (circ->base_.n_chan->channel_usage < CHANNEL_USED_FOR_FULL_CIRCS)
994  circ->base_.n_chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
995  }
996 
997  node = node_get_by_id(circ->base_.n_chan->identity_digest);
999  if (!fast) {
1000  /* We know the right onion key: we should send a create cell. */
1002  circ->cpath->extend_info);
1003  } else {
1004  /* We don't know an onion key, so we need to fall back to CREATE_FAST. */
1005  cc.cell_type = CELL_CREATE_FAST;
1006  cc.handshake_type = ONION_HANDSHAKE_TYPE_FAST;
1007  }
1008 
1010  circ->cpath->extend_info,
1011  &circ->cpath->handshake_state,
1012  cc.onionskin);
1013  if (len < 0) {
1014  log_warn(LD_CIRC,"onion_skin_create (first hop) failed.");
1015  return - END_CIRC_REASON_INTERNAL;
1016  }
1017  cc.handshake_len = len;
1018 
1019  if (circuit_deliver_create_cell(TO_CIRCUIT(circ), &cc, 0) < 0)
1020  return - END_CIRC_REASON_RESOURCELIMIT;
1021 
1022  circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
1024  log_info(LD_CIRC,"First hop: finished sending %s cell to '%s'",
1025  fast ? "CREATE_FAST" : "CREATE",
1026  node ? node_describe(node) : "<unnamed>");
1027  return 0;
1028 }
1029 
1036 static int
1038 {
1039  guard_usable_t r;
1040  if (! circ->guard_state) {
1041  if (circuit_get_cpath_len(circ) != 1 &&
1042  ! circuit_purpose_may_omit_guard(circ->base_.purpose) &&
1043  get_options()->UseEntryGuards) {
1044  log_warn(LD_BUG, "%d-hop circuit %p with purpose %d has no "
1045  "guard state",
1046  circuit_get_cpath_len(circ), circ, circ->base_.purpose);
1047  }
1048  r = GUARD_USABLE_NOW;
1049  } else {
1050  r = entry_guard_succeeded(&circ->guard_state);
1051  }
1052  const int is_usable_for_streams = (r == GUARD_USABLE_NOW);
1053  if (r == GUARD_USABLE_NOW) {
1055  } else if (r == GUARD_MAYBE_USABLE_LATER) {
1056  // Wait till either a better guard succeeds, or till
1057  // all better guards fail.
1059  } else {
1060  tor_assert_nonfatal(r == GUARD_USABLE_NEVER);
1061  return - END_CIRC_REASON_INTERNAL;
1062  }
1063 
1064  /* XXXX #21422 -- the rest of this branch needs careful thought!
1065  * Some of the things here need to happen when a circuit becomes
1066  * mechanically open; some need to happen when it is actually usable.
1067  * I think I got them right, but more checking would be wise. -NM
1068  */
1069 
1070  log_info(LD_CIRC,"circuit built!");
1072 
1073  if (circ->build_state->onehop_tunnel || circ->has_opened) {
1074  control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_STATUS, 0);
1075  }
1076 
1078  if (is_usable_for_streams)
1079  circuit_has_opened(circ); /* do other actions as necessary */
1080 
1082  const or_options_t *options = get_options();
1084  /* FFFF Log a count of known routers here */
1085  log_info(LD_GENERAL,
1086  "Tor has successfully opened a circuit. "
1087  "Looks like client functionality is working.");
1088  control_event_bootstrap(BOOTSTRAP_STATUS_DONE, 0);
1089  control_event_client_status(LOG_NOTICE, "CIRCUIT_ESTABLISHED");
1091  if (server_mode(options) && !check_whether_orport_reachable(options)) {
1094  }
1095  }
1096 
1097  /* We're done with measurement circuits here. Just close them */
1098  if (circ->base_.purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1099  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
1100  }
1101  return 0;
1102 }
1103 
1110 static int
1112  crypt_path_t *hop)
1113 {
1114  int len;
1115  extend_cell_t ec;
1116  memset(&ec, 0, sizeof(ec));
1117 
1118  log_debug(LD_CIRC,"starting to send subsequent skin.");
1119 
1120  if (tor_addr_family(&hop->extend_info->addr) != AF_INET) {
1121  log_warn(LD_BUG, "Trying to extend to a non-IPv4 address.");
1122  return - END_CIRC_REASON_INTERNAL;
1123  }
1124 
1126  &ec.create_cell.cell_type,
1128  hop->extend_info);
1129 
1130  tor_addr_copy(&ec.orport_ipv4.addr, &hop->extend_info->addr);
1131  ec.orport_ipv4.port = hop->extend_info->port;
1133  memcpy(ec.node_id, hop->extend_info->identity_digest, DIGEST_LEN);
1134  /* Set the ED25519 identity too -- it will only get included
1135  * in the extend2 cell if we're configured to use it, though. */
1137 
1139  hop->extend_info,
1140  &hop->handshake_state,
1141  ec.create_cell.onionskin);
1142  if (len < 0) {
1143  log_warn(LD_CIRC,"onion_skin_create failed.");
1144  return - END_CIRC_REASON_INTERNAL;
1145  }
1146  ec.create_cell.handshake_len = len;
1147 
1148  log_info(LD_CIRC,"Sending extend relay cell.");
1149  {
1150  uint8_t command = 0;
1151  uint16_t payload_len=0;
1152  uint8_t payload[RELAY_PAYLOAD_SIZE];
1153  if (extend_cell_format(&command, &payload_len, payload, &ec)<0) {
1154  log_warn(LD_CIRC,"Couldn't format extend cell");
1155  return -END_CIRC_REASON_INTERNAL;
1156  }
1157 
1158  /* send it to hop->prev, because that relay will transfer
1159  * it to a create cell and then send to hop */
1160  if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
1161  command,
1162  (char*)payload, payload_len,
1163  hop->prev) < 0)
1164  return 0; /* circuit is closed */
1165  }
1166  hop->state = CPATH_STATE_AWAITING_KEYS;
1167  return 0;
1168 }
1169 
1174 void
1175 circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle)
1176 {
1177  int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
1178  if (was_idle) {
1179  tor_log(severity, LD_GENERAL, "Tor has been idle for %"PRId64
1180  " seconds; assuming established circuits no longer work.",
1181  (seconds_elapsed));
1182  } else {
1183  tor_log(severity, LD_GENERAL,
1184  "Your system clock just jumped %"PRId64" seconds %s; "
1185  "assuming established circuits no longer work.",
1186  (
1187  seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed),
1188  seconds_elapsed >=0 ? "forward" : "backward");
1189  }
1190  control_event_general_status(LOG_WARN, "CLOCK_JUMPED TIME=%"PRId64
1191  " IDLE=%d",
1192  (seconds_elapsed), was_idle?1:0);
1193  /* so we log when it works again */
1195  control_event_client_status(severity, "CIRCUIT_NOT_ESTABLISHED REASON=%s",
1196  "CLOCK_JUMPED");
1199  if (seconds_elapsed < 0) {
1200  /* Restart all the timers in case we jumped a long way into the past. */
1202  }
1203 }
1204 
1213 int
1215 {
1216  channel_t *n_chan;
1217  relay_header_t rh;
1218  extend_cell_t ec;
1219  const char *msg = NULL;
1220  int should_launch = 0;
1221 
1222  if (circ->n_chan) {
1223  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1224  "n_chan already set. Bug/attack. Closing.");
1225  return -1;
1226  }
1227  if (circ->n_hop) {
1228  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1229  "conn to next hop already launched. Bug/attack. Closing.");
1230  return -1;
1231  }
1232 
1233  if (!server_mode(get_options())) {
1234  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1235  "Got an extend cell, but running as a client. Closing.");
1236  return -1;
1237  }
1238 
1239  relay_header_unpack(&rh, cell->payload);
1240 
1241  if (extend_cell_parse(&ec, rh.command,
1242  cell->payload+RELAY_HEADER_SIZE,
1243  rh.length) < 0) {
1244  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1245  "Can't parse extend cell. Closing circuit.");
1246  return -1;
1247  }
1248 
1249  if (!ec.orport_ipv4.port || tor_addr_is_null(&ec.orport_ipv4.addr)) {
1250  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1251  "Client asked me to extend to zero destination port or addr.");
1252  return -1;
1253  }
1254 
1255  if (tor_addr_is_internal(&ec.orport_ipv4.addr, 0) &&
1256  !get_options()->ExtendAllowPrivateAddresses) {
1257  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1258  "Client asked me to extend to a private address");
1259  return -1;
1260  }
1261 
1262  /* Check if they asked us for 0000..0000. We support using
1263  * an empty fingerprint for the first hop (e.g. for a bridge relay),
1264  * but we don't want to let clients send us extend cells for empty
1265  * fingerprints -- a) because it opens the user up to a mitm attack,
1266  * and b) because it lets an attacker force the relay to hold open a
1267  * new TLS connection for each extend request. */
1268  if (tor_digest_is_zero((const char*)ec.node_id)) {
1269  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1270  "Client asked me to extend without specifying an id_digest.");
1271  return -1;
1272  }
1273 
1274  /* Fill in ed_pubkey if it was not provided and we can infer it from
1275  * our networkstatus */
1277  const node_t *node = node_get_by_id((const char*)ec.node_id);
1278  const ed25519_public_key_t *node_ed_id = NULL;
1279  if (node &&
1281  (node_ed_id = node_get_ed25519_id(node))) {
1282  ed25519_pubkey_copy(&ec.ed_pubkey, node_ed_id);
1283  }
1284  }
1285 
1286  /* Next, check if we're being asked to connect to the hop that the
1287  * extend cell came from. There isn't any reason for that, and it can
1288  * assist circular-path attacks. */
1289  if (tor_memeq(ec.node_id,
1290  TO_OR_CIRCUIT(circ)->p_chan->identity_digest,
1291  DIGEST_LEN)) {
1292  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1293  "Client asked me to extend back to the previous hop.");
1294  return -1;
1295  }
1296 
1297  /* Check the previous hop Ed25519 ID too */
1300  &TO_OR_CIRCUIT(circ)->p_chan->ed25519_identity)) {
1301  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1302  "Client asked me to extend back to the previous hop "
1303  "(by Ed25519 ID).");
1304  return -1;
1305  }
1306 
1307  n_chan = channel_get_for_extend((const char*)ec.node_id,
1308  &ec.ed_pubkey,
1309  &ec.orport_ipv4.addr,
1310  &msg,
1311  &should_launch);
1312 
1313  if (!n_chan) {
1314  log_debug(LD_CIRC|LD_OR,"Next router (%s): %s",
1315  fmt_addrport(&ec.orport_ipv4.addr,ec.orport_ipv4.port),
1316  msg?msg:"????");
1317 
1318  circ->n_hop = extend_info_new(NULL /*nickname*/,
1319  (const char*)ec.node_id,
1320  &ec.ed_pubkey,
1321  NULL, /*onion_key*/
1322  NULL, /*curve25519_key*/
1323  &ec.orport_ipv4.addr,
1324  ec.orport_ipv4.port);
1325 
1326  circ->n_chan_create_cell = tor_memdup(&ec.create_cell,
1327  sizeof(ec.create_cell));
1328 
1330 
1331  if (should_launch) {
1332  /* we should try to open a connection */
1333  n_chan = channel_connect_for_circuit(&ec.orport_ipv4.addr,
1334  ec.orport_ipv4.port,
1335  (const char*)ec.node_id,
1336  &ec.ed_pubkey);
1337  if (!n_chan) {
1338  log_info(LD_CIRC,"Launching n_chan failed. Closing circuit.");
1339  circuit_mark_for_close(circ, END_CIRC_REASON_CONNECTFAILED);
1340  return 0;
1341  }
1342  log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
1343  }
1344  /* return success. The onion/circuit/etc will be taken care of
1345  * automatically (may already have been) whenever n_chan reaches
1346  * OR_CONN_STATE_OPEN.
1347  */
1348  return 0;
1349  }
1350 
1351  tor_assert(!circ->n_hop); /* Connection is already established. */
1352  circ->n_chan = n_chan;
1353  log_debug(LD_CIRC,
1354  "n_chan is %s",
1356 
1357  if (circuit_deliver_create_cell(circ, &ec.create_cell, 1) < 0)
1358  return -1;
1359 
1360  return 0;
1361 }
1362 
1380 int
1382  const char *key_data, size_t key_data_len,
1383  int reverse, int is_hs_v3)
1384 {
1385 
1386  tor_assert(cpath);
1387  return relay_crypto_init(&cpath->crypto, key_data, key_data_len, reverse,
1388  is_hs_v3);
1389 }
1390 
1400 int
1402  const created_cell_t *reply)
1403 {
1404  char keys[CPATH_KEY_MATERIAL_LEN];
1405  crypt_path_t *hop;
1406  int rv;
1407 
1408  if ((rv = pathbias_count_build_attempt(circ)) < 0) {
1409  log_warn(LD_CIRC, "pathbias_count_build_attempt failed: %d", rv);
1410  return rv;
1411  }
1412 
1413  if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS) {
1414  hop = circ->cpath;
1415  } else {
1416  hop = onion_next_hop_in_cpath(circ->cpath);
1417  if (!hop) { /* got an extended when we're all done? */
1418  log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
1419  return - END_CIRC_REASON_TORPROTOCOL;
1420  }
1421  }
1422  tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
1423 
1424  {
1425  const char *msg = NULL;
1427  &hop->handshake_state,
1428  reply->reply, reply->handshake_len,
1429  (uint8_t*)keys, sizeof(keys),
1430  (uint8_t*)hop->rend_circ_nonce,
1431  &msg) < 0) {
1432  if (msg)
1433  log_warn(LD_CIRC,"onion_skin_client_handshake failed: %s", msg);
1434  return -END_CIRC_REASON_TORPROTOCOL;
1435  }
1436  }
1437 
1439 
1440  if (circuit_init_cpath_crypto(hop, keys, sizeof(keys), 0, 0)<0) {
1441  return -END_CIRC_REASON_TORPROTOCOL;
1442  }
1443 
1444  hop->state = CPATH_STATE_OPEN;
1445  log_info(LD_CIRC,"Finished building circuit hop:");
1447  circuit_event_status(circ, CIRC_EVENT_EXTENDED, 0);
1448 
1449  return 0;
1450 }
1451 
1458 int
1460 {
1461 // crypt_path_t *victim;
1462 // connection_t *stream;
1463 
1464  tor_assert(circ);
1465 
1466  /* XXX Since we don't send truncates currently, getting a truncated
1467  * means that a connection broke or an extend failed. For now,
1468  * just give up.
1469  */
1470  circuit_mark_for_close(TO_CIRCUIT(circ),
1472  return 0;
1473 
1474 #if 0
1475  while (layer->next != circ->cpath) {
1476  /* we need to clear out layer->next */
1477  victim = layer->next;
1478  log_debug(LD_CIRC, "Killing a layer of the cpath.");
1479 
1480  for (stream = circ->p_streams; stream; stream=stream->next_stream) {
1481  if (stream->cpath_layer == victim) {
1482  log_info(LD_APP, "Marking stream %d for close because of truncate.",
1483  stream->stream_id);
1484  /* no need to send 'end' relay cells,
1485  * because the other side's already dead
1486  */
1487  connection_mark_unattached_ap(stream, END_STREAM_REASON_DESTROY);
1488  }
1489  }
1490 
1491  layer->next = victim->next;
1492  circuit_free_cpath_node(victim);
1493  }
1494 
1495  log_info(LD_CIRC, "finished");
1496  return 0;
1497 #endif /* 0 */
1498 }
1499 
1503 int
1505  const created_cell_t *created_cell,
1506  const char *keys, size_t keys_len,
1507  const uint8_t *rend_circ_nonce)
1508 {
1509  cell_t cell;
1510 
1511  tor_assert(keys_len == CPATH_KEY_MATERIAL_LEN);
1512 
1513  if (created_cell_format(&cell, created_cell) < 0) {
1514  log_warn(LD_BUG,"couldn't format created cell (type=%d, len=%d)",
1515  (int)created_cell->cell_type, (int)created_cell->handshake_len);
1516  return -1;
1517  }
1518  cell.circ_id = circ->p_circ_id;
1519 
1521 
1522  log_debug(LD_CIRC,"init digest forward 0x%.8x, backward 0x%.8x.",
1523  (unsigned int)get_uint32(keys),
1524  (unsigned int)get_uint32(keys+20));
1525  if (relay_crypto_init(&circ->crypto, keys, keys_len, 0, 0)<0) {
1526  log_warn(LD_BUG,"Circuit initialization failed");
1527  return -1;
1528  }
1529 
1530  memcpy(circ->rend_circ_nonce, rend_circ_nonce, DIGEST_LEN);
1531 
1532  int used_create_fast = (created_cell->cell_type == CELL_CREATED_FAST);
1533 
1535  circ->p_chan, &cell, CELL_DIRECTION_IN, 0);
1536  log_debug(LD_CIRC,"Finished sending '%s' cell.",
1537  used_create_fast ? "created_fast" : "created");
1538 
1539  /* Ignore the local bit when ExtendAllowPrivateAddresses is set:
1540  * it violates the assumption that private addresses are local.
1541  * Also, many test networks run on local addresses, and
1542  * TestingTorNetwork sets ExtendAllowPrivateAddresses. */
1543  if ((!channel_is_local(circ->p_chan)
1544  || get_options()->ExtendAllowPrivateAddresses)
1545  && !channel_is_outgoing(circ->p_chan)) {
1546  /* record that we could process create cells from a non-local conn
1547  * that we didn't initiate; presumably this means that create cells
1548  * can reach us too. */
1550  }
1551 
1552  return 0;
1553 }
1554 
1595 int
1596 route_len_for_purpose(uint8_t purpose, extend_info_t *exit_ei)
1597 {
1598  int routelen = DEFAULT_ROUTE_LEN;
1599  int known_purpose = 0;
1600 
1601  if (circuit_should_use_vanguards(purpose)) {
1602  /* Clients want an extra hop for rends to avoid linkability.
1603  * Services want it for intro points to avoid publishing their
1604  * layer3 guards. They want it for hsdir posts to use
1605  * their full layer3 guard set for those connections.
1606  * Ex: C - G - L2 - L3 - R
1607  * S - G - L2 - L3 - HSDIR
1608  * S - G - L2 - L3 - I
1609  */
1610  if (purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
1611  purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
1612  purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
1614  return routelen+1;
1615 
1616  /* If we only have Layer2 vanguards, then we do not need
1617  * the extra hop for linkabilty reasons (see below).
1618  * This means all hops can be of the form:
1619  * S/C - G - L2 - M - R/HSDir/I
1620  */
1621  if (get_options()->HSLayer2Nodes && !get_options()->HSLayer3Nodes)
1622  return routelen+1;
1623 
1624  /* For connections to hsdirs, clients want two extra hops
1625  * when using layer3 guards, to avoid linkability.
1626  * Same goes for intro points. Note that the route len
1627  * includes the intro point or hsdir, hence the +2.
1628  * Ex: C - G - L2 - L3 - M - I
1629  * C - G - L2 - L3 - M - HSDIR
1630  * S - G - L2 - L3 - M - R
1631  */
1632  if (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
1633  purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
1634  purpose == CIRCUIT_PURPOSE_C_INTRODUCING)
1635  return routelen+2;
1636  }
1637 
1638  if (!exit_ei)
1639  return routelen;
1640 
1641  switch (purpose) {
1642  /* These two purposes connect to a router that we chose, so
1643  * DEFAULT_ROUTE_LEN is safe. */
1645  /* hidden service connecting to introduction point */
1647  /* router reachability testing */
1648  known_purpose = 1;
1649  break;
1650 
1651  /* These three purposes connect to a router that someone else
1652  * might have chosen, so add an extra hop to protect anonymity. */
1656  /* connecting to hidden service directory */
1658  /* client connecting to introduction point */
1660  /* hidden service connecting to rendezvous point */
1661  known_purpose = 1;
1662  routelen++;
1663  break;
1664 
1665  default:
1666  /* Got a purpose not listed above along with a chosen exit.
1667  * Increase the circuit length by one anyway for safety. */
1668  routelen++;
1669  break;
1670  }
1671 
1672  if (BUG(exit_ei && !known_purpose)) {
1673  log_warn(LD_BUG, "Unhandled purpose %d with a chosen exit; "
1674  "assuming routelen %d.", purpose, routelen);
1675  }
1676  return routelen;
1677 }
1678 
1685 STATIC int
1686 new_route_len(uint8_t purpose, extend_info_t *exit_ei, smartlist_t *nodes)
1687 {
1688  int routelen;
1689 
1690  tor_assert(nodes);
1691 
1692  routelen = route_len_for_purpose(purpose, exit_ei);
1693 
1694  int num_acceptable_direct = count_acceptable_nodes(nodes, 1);
1695  int num_acceptable_indirect = count_acceptable_nodes(nodes, 0);
1696 
1697  log_debug(LD_CIRC,"Chosen route length %d (%d direct and %d indirect "
1698  "routers suitable).", routelen, num_acceptable_direct,
1699  num_acceptable_indirect);
1700 
1701  if (num_acceptable_direct < 1 || num_acceptable_indirect < routelen - 1) {
1702  log_info(LD_CIRC,
1703  "Not enough acceptable routers (%d/%d direct and %d/%d "
1704  "indirect routers suitable). Discarding this circuit.",
1705  num_acceptable_direct, routelen,
1706  num_acceptable_indirect, routelen);
1707  return -1;
1708  }
1709 
1710  return routelen;
1711 }
1712 
1715 static smartlist_t *
1717 {
1720  return dest;
1721 }
1722 
1730 circuit_all_predicted_ports_handled, (time_t now, int *need_uptime,
1731  int *need_capacity))
1732 {
1733  int i, enough;
1734  uint16_t *port;
1736  smartlist_t *LongLivedServices = get_options()->LongLivedPorts;
1737  tor_assert(need_uptime);
1738  tor_assert(need_capacity);
1739  // Always predict need_capacity
1740  *need_capacity = 1;
1741  enough = (smartlist_len(sl) == 0);
1742  for (i = 0; i < smartlist_len(sl); ++i) {
1743  port = smartlist_get(sl, i);
1744  if (smartlist_contains_int_as_string(LongLivedServices, *port))
1745  *need_uptime = 1;
1746  tor_free(port);
1747  }
1748  smartlist_free(sl);
1749  return enough;
1750 }
1751 
1755 static int
1756 node_handles_some_port(const node_t *node, smartlist_t *needed_ports)
1757 { /* XXXX MOVE */
1758  int i;
1759  uint16_t port;
1760 
1761  for (i = 0; i < smartlist_len(needed_ports); ++i) {
1763  /* alignment issues aren't a worry for this dereference, since
1764  needed_ports is explicitly a smartlist of uint16_t's */
1765  port = *(uint16_t *)smartlist_get(needed_ports, i);
1766  tor_assert(port);
1767  if (node)
1768  r = compare_tor_addr_to_node_policy(NULL, port, node);
1769  else
1770  continue;
1772  return 1;
1773  }
1774  return 0;
1775 }
1776 
1779 static int
1781 {
1782  entry_connection_t *entry;
1783  if (conn->type != CONN_TYPE_AP)
1784  return 0;
1785  entry = TO_ENTRY_CONN(conn);
1786 
1787  if (conn->state == AP_CONN_STATE_CIRCUIT_WAIT &&
1788  !conn->marked_for_close &&
1789  !(entry->want_onehop) && /* ignore one-hop streams */
1790  !(entry->use_begindir) && /* ignore targeted dir fetches */
1791  !(entry->chosen_exit_name) && /* ignore defined streams */
1793  !circuit_stream_is_being_handled(TO_ENTRY_CONN(conn), 0,
1795  return 1;
1796  return 0;
1797 }
1798 
1807 static const node_t *
1809 {
1810  int *n_supported;
1811  int n_pending_connections = 0;
1812  smartlist_t *connections;
1813  int best_support = -1;
1814  int n_best_support=0;
1815  const or_options_t *options = get_options();
1816  const smartlist_t *the_nodes;
1817  const node_t *selected_node=NULL;
1818  const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
1819  const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
1820  const int direct_conn = (flags & CRN_DIRECT_CONN) != 0;
1821 
1822  connections = get_connection_array();
1823 
1824  /* Count how many connections are waiting for a circuit to be built.
1825  * We use this for log messages now, but in the future we may depend on it.
1826  */
1827  SMARTLIST_FOREACH(connections, connection_t *, conn,
1828  {
1830  ++n_pending_connections;
1831  });
1832 // log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
1833 // n_pending_connections);
1834  /* Now we count, for each of the routers in the directory, how many
1835  * of the pending connections could possibly exit from that
1836  * router (n_supported[i]). (We can't be sure about cases where we
1837  * don't know the IP address of the pending connection.)
1838  *
1839  * -1 means "Don't use this router at all."
1840  */
1841  the_nodes = nodelist_get_list();
1842  n_supported = tor_calloc(smartlist_len(the_nodes), sizeof(int));
1843  SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
1844  const int i = node_sl_idx;
1845  if (router_digest_is_me(node->identity)) {
1846  n_supported[i] = -1;
1847 // log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
1848  /* XXX there's probably a reverse predecessor attack here, but
1849  * it's slow. should we take this out? -RD
1850  */
1851  continue;
1852  }
1853  if (!node_has_preferred_descriptor(node, direct_conn)) {
1854  n_supported[i] = -1;
1855  continue;
1856  }
1857  if (!node->is_running || node->is_bad_exit) {
1858  n_supported[i] = -1;
1859  continue; /* skip routers that are known to be down or bad exits */
1860  }
1861  if (node_get_purpose(node) != ROUTER_PURPOSE_GENERAL) {
1862  /* never pick a non-general node as a random exit. */
1863  n_supported[i] = -1;
1864  continue;
1865  }
1866  if (routerset_contains_node(options->ExcludeExitNodesUnion_, node)) {
1867  n_supported[i] = -1;
1868  continue; /* user asked us not to use it, no matter what */
1869  }
1870  if (options->ExitNodes &&
1871  !routerset_contains_node(options->ExitNodes, node)) {
1872  n_supported[i] = -1;
1873  continue; /* not one of our chosen exit nodes */
1874  }
1875 
1876  if (node_is_unreliable(node, need_uptime, need_capacity, 0)) {
1877  n_supported[i] = -1;
1878  continue; /* skip routers that are not suitable. Don't worry if
1879  * this makes us reject all the possible routers: if so,
1880  * we'll retry later in this function with need_update and
1881  * need_capacity set to 0. */
1882  }
1883  if (!(node->is_valid)) {
1884  /* if it's invalid and we don't want it */
1885  n_supported[i] = -1;
1886 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- invalid router.",
1887 // router->nickname, i);
1888  continue; /* skip invalid routers */
1889  }
1890  /* We do not allow relays that allow single hop exits by default. Option
1891  * was deprecated in 0.2.9.2-alpha and removed in 0.3.1.0-alpha. */
1892  if (node_allows_single_hop_exits(node)) {
1893  n_supported[i] = -1;
1894  continue;
1895  }
1896  if (node_exit_policy_rejects_all(node)) {
1897  n_supported[i] = -1;
1898 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
1899 // router->nickname, i);
1900  continue; /* skip routers that reject all */
1901  }
1902  n_supported[i] = 0;
1903  /* iterate over connections */
1904  SMARTLIST_FOREACH_BEGIN(connections, connection_t *, conn) {
1905  if (!ap_stream_wants_exit_attention(conn))
1906  continue; /* Skip everything but APs in CIRCUIT_WAIT */
1907  if (connection_ap_can_use_exit(TO_ENTRY_CONN(conn), node)) {
1908  ++n_supported[i];
1909 // log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
1910 // router->nickname, i, n_supported[i]);
1911  } else {
1912 // log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
1913 // router->nickname, i);
1914  }
1915  } SMARTLIST_FOREACH_END(conn);
1916  if (n_pending_connections > 0 && n_supported[i] == 0) {
1917  /* Leave best_support at -1 if that's where it is, so we can
1918  * distinguish it later. */
1919  continue;
1920  }
1921  if (n_supported[i] > best_support) {
1922  /* If this router is better than previous ones, remember its index
1923  * and goodness, and start counting how many routers are this good. */
1924  best_support = n_supported[i]; n_best_support=1;
1925 // log_fn(LOG_DEBUG,"%s is new best supported option so far.",
1926 // router->nickname);
1927  } else if (n_supported[i] == best_support) {
1928  /* If this router is _as good_ as the best one, just increment the
1929  * count of equally good routers.*/
1930  ++n_best_support;
1931  }
1932  } SMARTLIST_FOREACH_END(node);
1933  log_info(LD_CIRC,
1934  "Found %d servers that might support %d/%d pending connections.",
1935  n_best_support, best_support >= 0 ? best_support : 0,
1936  n_pending_connections);
1937 
1938  /* If any routers definitely support any pending connections, choose one
1939  * at random. */
1940  if (best_support > 0) {
1941  smartlist_t *supporting = smartlist_new();
1942 
1943  SMARTLIST_FOREACH(the_nodes, const node_t *, node, {
1944  if (n_supported[node_sl_idx] == best_support)
1945  smartlist_add(supporting, (void*)node);
1946  });
1947 
1948  selected_node = node_sl_choose_by_bandwidth(supporting, WEIGHT_FOR_EXIT);
1949  smartlist_free(supporting);
1950  } else {
1951  /* Either there are no pending connections, or no routers even seem to
1952  * possibly support any of them. Choose a router at random that satisfies
1953  * at least one predicted exit port. */
1954 
1955  int attempt;
1956  smartlist_t *needed_ports, *supporting;
1957 
1958  if (best_support == -1) {
1959  if (need_uptime || need_capacity) {
1960  log_info(LD_CIRC,
1961  "We couldn't find any live%s%s routers; falling back "
1962  "to list of all routers.",
1963  need_capacity?", fast":"",
1964  need_uptime?", stable":"");
1965  tor_free(n_supported);
1966  flags &= ~(CRN_NEED_UPTIME|CRN_NEED_CAPACITY);
1967  return choose_good_exit_server_general(flags);
1968  }
1969  log_notice(LD_CIRC, "All routers are down or won't exit%s -- "
1970  "choosing a doomed exit at random.",
1971  options->ExcludeExitNodesUnion_ ? " or are Excluded" : "");
1972  }
1973  supporting = smartlist_new();
1974  needed_ports = circuit_get_unhandled_ports(time(NULL));
1975  for (attempt = 0; attempt < 2; attempt++) {
1976  /* try once to pick only from routers that satisfy a needed port,
1977  * then if there are none, pick from any that support exiting. */
1978  SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
1979  if (n_supported[node_sl_idx] != -1 &&
1980  (attempt || node_handles_some_port(node, needed_ports))) {
1981 // log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.",
1982 // try, router->nickname);
1983  smartlist_add(supporting, (void*)node);
1984  }
1985  } SMARTLIST_FOREACH_END(node);
1986 
1987  selected_node = node_sl_choose_by_bandwidth(supporting, WEIGHT_FOR_EXIT);
1988  if (selected_node)
1989  break;
1990  smartlist_clear(supporting);
1991  /* If we reach this point, we can't actually support any unhandled
1992  * predicted ports, so clear all the remaining ones. */
1993  if (smartlist_len(needed_ports))
1994  rep_hist_remove_predicted_ports(needed_ports);
1995  }
1996  SMARTLIST_FOREACH(needed_ports, uint16_t *, cp, tor_free(cp));
1997  smartlist_free(needed_ports);
1998  smartlist_free(supporting);
1999  }
2000 
2001  tor_free(n_supported);
2002  if (selected_node) {
2003  log_info(LD_CIRC, "Chose exit server '%s'", node_describe(selected_node));
2004  return selected_node;
2005  }
2006  if (options->ExitNodes) {
2007  log_warn(LD_CIRC,
2008  "No exits in ExitNodes%s seem to be running: "
2009  "can't choose an exit.",
2010  options->ExcludeExitNodesUnion_ ?
2011  ", except possibly those excluded by your configuration, " : "");
2012  }
2013  return NULL;
2014 }
2015 
2016 /* Pick a Rendezvous Point for our HS circuits according to <b>flags</b>. */
2017 static const node_t *
2018 pick_rendezvous_node(router_crn_flags_t flags)
2019 {
2020  const or_options_t *options = get_options();
2021  return router_choose_random_node(NULL, options->ExcludeNodes, flags);
2022 }
2023 
2024 /*
2025  * Helper function to pick a configured restricted middle node
2026  * (either HSLayer2Nodes or HSLayer3Nodes).
2027  *
2028  * Make sure that the node we chose is alive, and not excluded,
2029  * and return it.
2030  *
2031  * The exclude_set is a routerset of nodes that the selected node
2032  * must not match, and the exclude_list is a simple list of nodes
2033  * that the selected node must not be in. Either or both may be
2034  * NULL.
2035  *
2036  * Return NULL if no usable nodes could be found. */
2037 static const node_t *
2039  const routerset_t *pick_from,
2040  const routerset_t *exclude_set,
2041  const smartlist_t *exclude_list,
2042  int position_hint)
2043 {
2044  const node_t *middle_node = NULL;
2045 
2046  smartlist_t *whitelisted_live_middles = smartlist_new();
2047  smartlist_t *all_live_nodes = smartlist_new();
2048 
2049  tor_assert(pick_from);
2050 
2051  /* Add all running nodes to all_live_nodes */
2053  (flags & CRN_NEED_UPTIME) != 0,
2054  (flags & CRN_NEED_CAPACITY) != 0,
2055  (flags & CRN_NEED_GUARD) != 0,
2056  (flags & CRN_NEED_DESC) != 0,
2057  (flags & CRN_PREF_ADDR) != 0,
2058  (flags & CRN_DIRECT_CONN) != 0);
2059 
2060  /* Filter all_live_nodes to only add live *and* whitelisted middles
2061  * to the list whitelisted_live_middles. */
2062  SMARTLIST_FOREACH_BEGIN(all_live_nodes, node_t *, live_node) {
2063  if (routerset_contains_node(pick_from, live_node)) {
2064  smartlist_add(whitelisted_live_middles, live_node);
2065  }
2066  } SMARTLIST_FOREACH_END(live_node);
2067 
2068  /* Honor ExcludeNodes */
2069  if (exclude_set) {
2070  routerset_subtract_nodes(whitelisted_live_middles, exclude_set);
2071  }
2072 
2073  if (exclude_list) {
2074  smartlist_subtract(whitelisted_live_middles, exclude_list);
2075  }
2076 
2083 #define MAX_SANE_RESTRICTED_NODES 20
2084  /* If the user (or associated tor controller) selected only a few nodes,
2085  * assume they took load balancing into account and don't do it for them.
2086  *
2087  * If there are a lot of nodes in here, assume they did not load balance
2088  * and do it for them, but also warn them that they may be Doing It Wrong.
2089  */
2090  if (smartlist_len(whitelisted_live_middles) <=
2091  MAX_SANE_RESTRICTED_NODES) {
2092  middle_node = smartlist_choose(whitelisted_live_middles);
2093  } else {
2094  static ratelim_t pinned_notice_limit = RATELIM_INIT(24*3600);
2095  log_fn_ratelim(&pinned_notice_limit, LOG_NOTICE, LD_CIRC,
2096  "Your _HSLayer%dNodes setting has resulted "
2097  "in %d total nodes. This is a lot of nodes. "
2098  "You may want to consider using a Tor controller "
2099  "to select and update a smaller set of nodes instead.",
2100  position_hint, smartlist_len(whitelisted_live_middles));
2101 
2102  /* NO_WEIGHTING here just means don't take node flags into account
2103  * (ie: use consensus measurement only). This is done so that
2104  * we don't further surprise the user by not using Exits that they
2105  * specified at all */
2106  middle_node = node_sl_choose_by_bandwidth(whitelisted_live_middles,
2107  NO_WEIGHTING);
2108  }
2109 
2110  smartlist_free(whitelisted_live_middles);
2111  smartlist_free(all_live_nodes);
2112 
2113  return middle_node;
2114 }
2115 
2126 static const node_t *
2128  router_crn_flags_t flags, int is_internal)
2129 {
2130  const or_options_t *options = get_options();
2131  flags |= CRN_NEED_DESC;
2132 
2133  switch (TO_CIRCUIT(circ)->purpose) {
2137  /* For these three, we want to pick the exit like a middle hop,
2138  * since it should be random. */
2139  tor_assert_nonfatal(is_internal);
2140  /* Falls through */
2142  if (is_internal) /* pick it like a middle hop */
2143  return router_choose_random_node(NULL, options->ExcludeNodes, flags);
2144  else
2145  return choose_good_exit_server_general(flags);
2147  {
2148  /* Pick a new RP */
2149  const node_t *rendezvous_node = pick_rendezvous_node(flags);
2150  log_info(LD_REND, "Picked new RP: %s",
2151  safe_str_client(node_describe(rendezvous_node)));
2152  return rendezvous_node;
2153  }
2154  }
2155  log_warn(LD_BUG,"Unhandled purpose %d", TO_CIRCUIT(circ)->purpose);
2157  return NULL;
2158 }
2159 
2162 static void
2164  const extend_info_t *exit_ei)
2165 {
2166  const or_options_t *options = get_options();
2167  routerset_t *rs = options->ExcludeNodes;
2168  const char *description;
2169  uint8_t purpose = circ->base_.purpose;
2170 
2171  if (circ->build_state->onehop_tunnel)
2172  return;
2173 
2174  switch (purpose)
2175  {
2176  default:
2177  case CIRCUIT_PURPOSE_OR:
2181  log_warn(LD_BUG, "Called on non-origin circuit (purpose %d, %s)",
2182  (int)purpose,
2183  circuit_purpose_to_string(purpose));
2184  return;
2188  if (circ->build_state->is_internal)
2189  return;
2190  description = "requested exit node";
2191  rs = options->ExcludeExitNodesUnion_;
2192  break;
2200  return;
2205  description = "chosen rendezvous point";
2206  break;
2208  rs = options->ExcludeExitNodesUnion_;
2209  description = "controller-selected circuit target";
2210  break;
2211  }
2212 
2213  if (routerset_contains_extendinfo(rs, exit_ei)) {
2214  /* We should never get here if StrictNodes is set to 1. */
2215  if (options->StrictNodes) {
2216  log_warn(LD_BUG, "Using %s '%s' which is listed in ExcludeNodes%s, "
2217  "even though StrictNodes is set. Please report. "
2218  "(Circuit purpose: %s)",
2219  description, extend_info_describe(exit_ei),
2220  rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
2221  circuit_purpose_to_string(purpose));
2222  } else {
2223  log_warn(LD_CIRC, "Using %s '%s' which is listed in "
2224  "ExcludeNodes%s, because no better options were available. To "
2225  "prevent this (and possibly break your Tor functionality), "
2226  "set the StrictNodes configuration option. "
2227  "(Circuit purpose: %s)",
2228  description, extend_info_describe(exit_ei),
2229  rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
2230  circuit_purpose_to_string(purpose));
2231  }
2233  }
2234 
2235  return;
2236 }
2237 
2246 STATIC int
2248  int is_hs_v3_rp_circuit)
2249 {
2250  cpath_build_state_t *state = circ->build_state;
2251 
2252  if (state->onehop_tunnel) {
2253  log_debug(LD_CIRC, "Launching a one-hop circuit for dir tunnel%s.",
2254  (rend_allow_non_anonymous_connection(get_options()) ?
2255  ", or intro or rendezvous connection" : ""));
2256  state->desired_path_len = 1;
2257  } else {
2258  int r = new_route_len(circ->base_.purpose, exit_ei, nodelist_get_list());
2259  if (r < 1) /* must be at least 1 */
2260  return -1;
2261  state->desired_path_len = r;
2262  }
2263 
2264  if (exit_ei) { /* the circuit-builder pre-requested one */
2265  warn_if_last_router_excluded(circ, exit_ei);
2266  log_info(LD_CIRC,"Using requested exit node '%s'",
2267  extend_info_describe(exit_ei));
2268  exit_ei = extend_info_dup(exit_ei);
2269  } else { /* we have to decide one */
2270  router_crn_flags_t flags = CRN_NEED_DESC;
2271  if (state->need_uptime)
2272  flags |= CRN_NEED_UPTIME;
2273  if (state->need_capacity)
2274  flags |= CRN_NEED_CAPACITY;
2275  if (is_hs_v3_rp_circuit)
2276  flags |= CRN_RENDEZVOUS_V3;
2277  if (state->onehop_tunnel)
2278  flags |= CRN_DIRECT_CONN;
2279  const node_t *node =
2280  choose_good_exit_server(circ, flags, state->is_internal);
2281  if (!node) {
2282  log_warn(LD_CIRC,"Failed to choose an exit server");
2283  return -1;
2284  }
2285  exit_ei = extend_info_from_node(node, state->onehop_tunnel);
2286  if (BUG(exit_ei == NULL))
2287  return -1;
2288  }
2289  state->chosen_exit = exit_ei;
2290  return 0;
2291 }
2292 
2297 int
2299 {
2300  cpath_build_state_t *state;
2301  tor_assert(exit_ei);
2302  tor_assert(circ);
2303 
2304  state = circ->build_state;
2305  tor_assert(state);
2306  extend_info_free(state->chosen_exit);
2307  state->chosen_exit = extend_info_dup(exit_ei);
2308 
2309  ++circ->build_state->desired_path_len;
2310  onion_append_hop(&circ->cpath, exit_ei);
2311  return 0;
2312 }
2313 
2318 int
2320 {
2321  int err_reason = 0;
2322  warn_if_last_router_excluded(circ, exit_ei);
2323 
2324  tor_gettimeofday(&circ->base_.timestamp_began);
2325 
2326  circuit_append_new_exit(circ, exit_ei);
2328  if ((err_reason = circuit_send_next_onion_skin(circ))<0) {
2329  log_warn(LD_CIRC, "Couldn't extend circuit to new point %s.",
2330  extend_info_describe(exit_ei));
2331  circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
2332  return -1;
2333  }
2334 
2335  // XXX: Should cannibalized circuits be dirty or not? Not easy to say..
2336 
2337  return 0;
2338 }
2339 
2347 MOCK_IMPL(STATIC int,
2348 count_acceptable_nodes, (smartlist_t *nodes, int direct))
2349 {
2350  int num=0;
2351 
2352  SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
2353  // log_debug(LD_CIRC,
2354 // "Contemplating whether router %d (%s) is a new option.",
2355 // i, r->nickname);
2356  if (! node->is_running)
2357 // log_debug(LD_CIRC,"Nope, the directory says %d is not running.",i);
2358  continue;
2359  if (! node->is_valid)
2360 // log_debug(LD_CIRC,"Nope, the directory says %d is not valid.",i);
2361  continue;
2362  if (! node_has_preferred_descriptor(node, direct))
2363  continue;
2364  /* The node has a descriptor, so we can just check the ntor key directly */
2365  if (!node_has_curve25519_onion_key(node))
2366  continue;
2367  ++num;
2368  } SMARTLIST_FOREACH_END(node);
2369 
2370 // log_debug(LD_CIRC,"I like %d. num_acceptable_routers now %d.",i, num);
2371 
2372  return num;
2373 }
2374 
2378 void
2380 {
2381  if (*head_ptr) {
2382  new_hop->next = (*head_ptr);
2383  new_hop->prev = (*head_ptr)->prev;
2384  (*head_ptr)->prev->next = new_hop;
2385  (*head_ptr)->prev = new_hop;
2386  } else {
2387  *head_ptr = new_hop;
2388  new_hop->prev = new_hop->next = new_hop;
2389  }
2390 }
2391 
2392 #ifdef TOR_UNIT_TESTS
2393 
2395 unsigned int
2396 cpath_get_n_hops(crypt_path_t **head_ptr)
2397 {
2398  unsigned int n_hops = 0;
2399  crypt_path_t *tmp;
2400 
2401  if (!*head_ptr) {
2402  return 0;
2403  }
2404 
2405  tmp = *head_ptr;
2406  do {
2407  n_hops++;
2408  tmp = tmp->next;
2409  } while (tmp != *head_ptr);
2410 
2411  return n_hops;
2412 }
2413 
2414 #endif /* defined(TOR_UNIT_TESTS) */
2415 
2436 static smartlist_t *
2438  cpath_build_state_t *state,
2439  crypt_path_t *head,
2440  int cur_len)
2441 {
2442  smartlist_t *excluded;
2443  const node_t *r;
2444  crypt_path_t *cpath;
2445  int i;
2446 
2447  (void) purpose;
2448 
2449  excluded = smartlist_new();
2450 
2451  /* Add the exit to the exclude list (note that the exit/last hop is always
2452  * chosen first in circuit_establish_circuit()). */
2453  if ((r = build_state_get_exit_node(state))) {
2454  smartlist_add(excluded, (node_t*)r);
2455  }
2456 
2457  /* If we are picking the 4th hop, allow that node to be the guard too.
2458  * This prevents us from avoiding the Guard for those hops, which
2459  * gives the adversary information about our guard if they control
2460  * the RP, IP, or HSDIR. We don't do this check based on purpose
2461  * because we also want to allow HS_VANGUARDS pre-build circuits
2462  * to use the guard for that last hop.
2463  */
2464  if (cur_len == DEFAULT_ROUTE_LEN+1) {
2465  /* Skip the first hop for the exclude list below */
2466  head = head->next;
2467  cur_len--;
2468  }
2469 
2470  for (i = 0, cpath = head; cpath && i < cur_len; ++i, cpath=cpath->next) {
2471  if ((r = node_get_by_id(cpath->extend_info->identity_digest))) {
2472  smartlist_add(excluded, (node_t*)r);
2473  }
2474  }
2475 
2476  return excluded;
2477 }
2478 
2483 static smartlist_t *
2485  cpath_build_state_t *state,
2486  crypt_path_t *head,
2487  int cur_len)
2488 {
2489  smartlist_t *excluded;
2490  const node_t *r;
2491  crypt_path_t *cpath;
2492  int i;
2493 
2495  if (circuit_should_use_vanguards(purpose)) {
2496  return build_vanguard_middle_exclude_list(purpose, state, head, cur_len);
2497  }
2498 
2499  excluded = smartlist_new();
2500 
2501  /* For non-vanguard circuits, add the exit and its family to the exclude list
2502  * (note that the exit/last hop is always chosen first in
2503  * circuit_establish_circuit()). */
2504  if ((r = build_state_get_exit_node(state))) {
2505  nodelist_add_node_and_family(excluded, r);
2506  }
2507 
2508  /* also exclude all other already chosen nodes and their family */
2509  for (i = 0, cpath = head; cpath && i < cur_len; ++i, cpath=cpath->next) {
2510  if ((r = node_get_by_id(cpath->extend_info->identity_digest))) {
2511  nodelist_add_node_and_family(excluded, r);
2512  }
2513  }
2514 
2515  return excluded;
2516 }
2517 
2519 static int
2521  uint8_t purpose, int cur_len)
2522 {
2523  /* If this is not a hidden service circuit, don't use vanguards */
2524  if (!circuit_purpose_is_hidden_service(purpose)) {
2525  return 0;
2526  }
2527 
2528  /* If we have sticky L2 nodes, and this is an L2 pick, use vanguards */
2529  if (options->HSLayer2Nodes && cur_len == 1) {
2530  return 1;
2531  }
2532 
2533  /* If we have sticky L3 nodes, and this is an L3 pick, use vanguards */
2534  if (options->HSLayer3Nodes && cur_len == 2) {
2535  return 1;
2536  }
2537 
2538  return 0;
2539 }
2540 
2543 static const node_t *
2545  router_crn_flags_t flags, int cur_len,
2546  const smartlist_t *excluded)
2547 {
2548  const routerset_t *vanguard_routerset = NULL;
2549  const node_t *node = NULL;
2550 
2551  /* Pick the right routerset based on the current hop */
2552  if (cur_len == 1) {
2553  vanguard_routerset = options->HSLayer2Nodes;
2554  } else if (cur_len == 2) {
2555  vanguard_routerset = options->HSLayer3Nodes;
2556  } else {
2557  /* guaranteed by middle_node_should_be_vanguard() */
2558  tor_assert_nonfatal_unreached();
2559  return NULL;
2560  }
2561 
2562  node = pick_restricted_middle_node(flags, vanguard_routerset,
2563  options->ExcludeNodes, excluded,
2564  cur_len+1);
2565 
2566  if (!node) {
2567  static ratelim_t pinned_warning_limit = RATELIM_INIT(300);
2568  log_fn_ratelim(&pinned_warning_limit, LOG_WARN, LD_CIRC,
2569  "Could not find a node that matches the configured "
2570  "_HSLayer%dNodes set", cur_len+1);
2571  }
2572 
2573  return node;
2574 }
2575 
2582 static const node_t *
2584  cpath_build_state_t *state,
2585  crypt_path_t *head,
2586  int cur_len)
2587 {
2588  const node_t *choice;
2589  smartlist_t *excluded;
2590  const or_options_t *options = get_options();
2591  router_crn_flags_t flags = CRN_NEED_DESC;
2592  tor_assert(CIRCUIT_PURPOSE_MIN_ <= purpose &&
2593  purpose <= CIRCUIT_PURPOSE_MAX_);
2594 
2595  log_debug(LD_CIRC, "Contemplating intermediate hop #%d: random choice.",
2596  cur_len+1);
2597 
2598  excluded = build_middle_exclude_list(purpose, state, head, cur_len);
2599 
2600  if (state->need_uptime)
2601  flags |= CRN_NEED_UPTIME;
2602  if (state->need_capacity)
2603  flags |= CRN_NEED_CAPACITY;
2604 
2606  if (middle_node_must_be_vanguard(options, purpose, cur_len)) {
2607  log_debug(LD_GENERAL, "Picking a sticky node (cur_len = %d)", cur_len);
2608  choice = pick_vanguard_middle_node(options, flags, cur_len, excluded);
2609  smartlist_free(excluded);
2610  return choice;
2611  }
2612 
2613  if (options->MiddleNodes) {
2614  smartlist_t *sl = smartlist_new();
2615  routerset_get_all_nodes(sl, options->MiddleNodes,
2616  options->ExcludeNodes, 1);
2617 
2618  smartlist_subtract(sl, excluded);
2619 
2620  choice = node_sl_choose_by_bandwidth(sl, WEIGHT_FOR_MID);
2621  smartlist_free(sl);
2622  if (choice) {
2623  log_fn(LOG_INFO, LD_CIRC, "Chose fixed middle node: %s",
2624  hex_str(choice->identity, DIGEST_LEN));
2625  } else {
2626  log_fn(LOG_NOTICE, LD_CIRC, "Restricted middle not available");
2627  }
2628  } else {
2629  choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2630  }
2631  smartlist_free(excluded);
2632  return choice;
2633 }
2634 
2644 const node_t *
2646  circuit_guard_state_t **guard_state_out)
2647 {
2648  const node_t *choice;
2649  smartlist_t *excluded;
2650  const or_options_t *options = get_options();
2651  /* If possible, choose an entry server with a preferred address,
2652  * otherwise, choose one with an allowed address */
2653  router_crn_flags_t flags = (CRN_NEED_GUARD|CRN_NEED_DESC|CRN_PREF_ADDR|
2654  CRN_DIRECT_CONN);
2655  const node_t *node;
2656 
2657  /* Once we used this function to select a node to be a guard. We had
2658  * 'state == NULL' be the signal for that. But we don't do that any more.
2659  */
2660  tor_assert_nonfatal(state);
2661 
2662  if (state && options->UseEntryGuards &&
2663  (purpose != CIRCUIT_PURPOSE_TESTING || options->BridgeRelay)) {
2664  /* This request is for an entry server to use for a regular circuit,
2665  * and we use entry guard nodes. Just return one of the guard nodes. */
2666  tor_assert(guard_state_out);
2667  return guards_choose_guard(state, purpose, guard_state_out);
2668  }
2669 
2670  excluded = smartlist_new();
2671 
2672  if (state && (node = build_state_get_exit_node(state))) {
2673  /* Exclude the exit node from the state, if we have one. Also exclude its
2674  * family. */
2675  nodelist_add_node_and_family(excluded, node);
2676  }
2677 
2678  if (state) {
2679  if (state->need_uptime)
2680  flags |= CRN_NEED_UPTIME;
2681  if (state->need_capacity)
2682  flags |= CRN_NEED_CAPACITY;
2683  }
2684 
2685  choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2686  smartlist_free(excluded);
2687  return choice;
2688 }
2689 
2692 static crypt_path_t *
2694 {
2695  crypt_path_t *hop = cpath;
2696  do {
2697  if (hop->state != CPATH_STATE_OPEN)
2698  return hop;
2699  hop = hop->next;
2700  } while (hop != cpath);
2701  return NULL;
2702 }
2703 
2710 STATIC int
2712 {
2713  uint8_t purpose = circ->base_.purpose;
2714  cpath_build_state_t *state = circ->build_state;
2715  int cur_len = circuit_get_cpath_len(circ);
2716  extend_info_t *info = NULL;
2717 
2718  if (cur_len >= state->desired_path_len) {
2719  log_debug(LD_CIRC, "Path is complete: %d steps long",
2720  state->desired_path_len);
2721  return 1;
2722  }
2723 
2724  log_debug(LD_CIRC, "Path is %d long; we want %d", cur_len,
2725  state->desired_path_len);
2726 
2727  if (cur_len == state->desired_path_len - 1) { /* Picking last node */
2728  info = extend_info_dup(state->chosen_exit);
2729  } else if (cur_len == 0) { /* picking first node */
2730  const node_t *r = choose_good_entry_server(purpose, state,
2731  &circ->guard_state);
2732  if (r) {
2733  /* If we're a client, use the preferred address rather than the
2734  primary address, for potentially connecting to an IPv6 OR
2735  port. Servers always want the primary (IPv4) address. */
2736  int client = (server_mode(get_options()) == 0);
2737  info = extend_info_from_node(r, client);
2738  /* Clients can fail to find an allowed address */
2739  tor_assert_nonfatal(info || client);
2740  }
2741  } else {
2742  const node_t *r =
2743  choose_good_middle_server(purpose, state, circ->cpath, cur_len);
2744  if (r) {
2745  info = extend_info_from_node(r, 0);
2746  tor_assert_nonfatal(info);
2747  }
2748  }
2749 
2750  if (!info) {
2751  log_warn(LD_CIRC,"Failed to find node for hop #%d of our path. Discarding "
2752  "this circuit.", cur_len+1);
2753  return -1;
2754  }
2755 
2756  log_debug(LD_CIRC,"Chose router %s for hop #%d (exit is %s)",
2757  extend_info_describe(info),
2758  cur_len+1, build_state_get_exit_nickname(state));
2759 
2760  onion_append_hop(&circ->cpath, info);
2761  extend_info_free(info);
2762  return 0;
2763 }
2764 
2768 STATIC int
2770 {
2771  crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
2772 
2773  /* link hop into the cpath, at the end. */
2774  onion_append_to_cpath(head_ptr, hop);
2775 
2776  hop->magic = CRYPT_PATH_MAGIC;
2777  hop->state = CPATH_STATE_CLOSED;
2778 
2779  hop->extend_info = extend_info_dup(choice);
2780 
2783 
2784  return 0;
2785 }
2786 
2788 extend_info_t *
2789 extend_info_new(const char *nickname,
2790  const char *rsa_id_digest,
2791  const ed25519_public_key_t *ed_id,
2792  crypto_pk_t *onion_key,
2793  const curve25519_public_key_t *ntor_key,
2794  const tor_addr_t *addr, uint16_t port)
2795 {
2796  extend_info_t *info = tor_malloc_zero(sizeof(extend_info_t));
2797  memcpy(info->identity_digest, rsa_id_digest, DIGEST_LEN);
2798  if (ed_id && !ed25519_public_key_is_zero(ed_id))
2799  memcpy(&info->ed_identity, ed_id, sizeof(ed25519_public_key_t));
2800  if (nickname)
2801  strlcpy(info->nickname, nickname, sizeof(info->nickname));
2802  if (onion_key)
2803  info->onion_key = crypto_pk_dup_key(onion_key);
2804  if (ntor_key)
2805  memcpy(&info->curve25519_onion_key, ntor_key,
2806  sizeof(curve25519_public_key_t));
2807  tor_addr_copy(&info->addr, addr);
2808  info->port = port;
2809  return info;
2810 }
2811 
2822 extend_info_t *
2823 extend_info_from_node(const node_t *node, int for_direct_connect)
2824 {
2825  crypto_pk_t *rsa_pubkey = NULL;
2826  extend_info_t *info = NULL;
2827  tor_addr_port_t ap;
2828  int valid_addr = 0;
2829 
2830  if (!node_has_preferred_descriptor(node, for_direct_connect)) {
2831  return NULL;
2832  }
2833 
2834  /* Choose a preferred address first, but fall back to an allowed address. */
2835  if (for_direct_connect)
2836  fascist_firewall_choose_address_node(node, FIREWALL_OR_CONNECTION, 0, &ap);
2837  else {
2838  node_get_prim_orport(node, &ap);
2839  }
2840  valid_addr = tor_addr_port_is_valid_ap(&ap, 0);
2841 
2842  if (valid_addr)
2843  log_debug(LD_CIRC, "using %s for %s",
2844  fmt_addrport(&ap.addr, ap.port),
2845  node->ri ? node->ri->nickname : node->rs->nickname);
2846  else
2847  log_warn(LD_CIRC, "Could not choose valid address for %s",
2848  node->ri ? node->ri->nickname : node->rs->nickname);
2849 
2850  /* Every node we connect or extend to must support ntor */
2851  if (!node_has_curve25519_onion_key(node)) {
2852  log_fn(LOG_PROTOCOL_WARN, LD_CIRC,
2853  "Attempted to create extend_info for a node that does not support "
2854  "ntor: %s", node_describe(node));
2855  return NULL;
2856  }
2857 
2858  const ed25519_public_key_t *ed_pubkey = NULL;
2859 
2860  /* Don't send the ed25519 pubkey unless the target node actually supports
2861  * authenticating with it. */
2863  log_info(LD_CIRC, "Including Ed25519 ID for %s", node_describe(node));
2864  ed_pubkey = node_get_ed25519_id(node);
2865  } else if (node_get_ed25519_id(node)) {
2866  log_info(LD_CIRC, "Not including the ed25519 ID for %s, since it won't "
2867  "be able to authenticate it.",
2868  node_describe(node));
2869  }
2870 
2871  /* Retrieve the curve25519 pubkey. */
2872  const curve25519_public_key_t *curve_pubkey =
2874  rsa_pubkey = node_get_rsa_onion_key(node);
2875 
2876  if (valid_addr && node->ri) {
2877  info = extend_info_new(node->ri->nickname,
2878  node->identity,
2879  ed_pubkey,
2880  rsa_pubkey,
2881  curve_pubkey,
2882  &ap.addr,
2883  ap.port);
2884  } else if (valid_addr && node->rs && node->md) {
2885  info = extend_info_new(node->rs->nickname,
2886  node->identity,
2887  ed_pubkey,
2888  rsa_pubkey,
2889  curve_pubkey,
2890  &ap.addr,
2891  ap.port);
2892  }
2893 
2894  crypto_pk_free(rsa_pubkey);
2895  return info;
2896 }
2897 
2899 void
2901 {
2902  if (!info)
2903  return;
2904  crypto_pk_free(info->onion_key);
2905  tor_free(info);
2906 }
2907 
2910 extend_info_t *
2912 {
2913  extend_info_t *newinfo;
2914  tor_assert(info);
2915  newinfo = tor_malloc(sizeof(extend_info_t));
2916  memcpy(newinfo, info, sizeof(extend_info_t));
2917  if (info->onion_key)
2918  newinfo->onion_key = crypto_pk_dup_key(info->onion_key);
2919  else
2920  newinfo->onion_key = NULL;
2921  return newinfo;
2922 }
2923 
2928 const node_t *
2930 {
2931  if (!state || !state->chosen_exit)
2932  return NULL;
2933  return node_get_by_id(state->chosen_exit->identity_digest);
2934 }
2935 
2939 const uint8_t *
2941 {
2942  if (!state || !state->chosen_exit)
2943  return NULL;
2944  return (const uint8_t *) state->chosen_exit->identity_digest;
2945 }
2946 
2951 const char *
2953 {
2954  if (!state || !state->chosen_exit)
2955  return NULL;
2956  return state->chosen_exit->nickname;
2957 }
2958 
2960 int
2962 {
2963  tor_assert(addr);
2964 
2965  /* Check if we have a private address and if we can extend to it. */
2966  if ((tor_addr_is_internal(addr, 0) || tor_addr_is_multicast(addr)) &&
2967  !get_options()->ExtendAllowPrivateAddresses) {
2968  goto disallow;
2969  }
2970  /* Allowed! */
2971  return 1;
2972  disallow:
2973  return 0;
2974 }
2975 
2976 /* Does ei have a valid TAP key? */
2977 int
2978 extend_info_supports_tap(const extend_info_t* ei)
2979 {
2980  tor_assert(ei);
2981  /* Valid TAP keys are not NULL */
2982  return ei->onion_key != NULL;
2983 }
2984 
2985 /* Does ei have a valid ntor key? */
2986 int
2987 extend_info_supports_ntor(const extend_info_t* ei)
2988 {
2989  tor_assert(ei);
2990  /* Valid ntor keys have at least one non-zero byte */
2991  return !tor_mem_is_zero(
2992  (const char*)ei->curve25519_onion_key.public_key,
2994 }
2995 
2996 /* Is circuit purpose allowed to use the deprecated TAP encryption protocol?
2997  * The hidden service protocol still uses TAP for some connections, because
2998  * ntor onion keys aren't included in HS descriptors or INTRODUCE cells. */
2999 static int
3000 circuit_purpose_can_use_tap_impl(uint8_t purpose)
3001 {
3002  return (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
3003  purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
3004 }
3005 
3006 /* Is circ allowed to use the deprecated TAP encryption protocol?
3007  * The hidden service protocol still uses TAP for some connections, because
3008  * ntor onion keys aren't included in HS descriptors or INTRODUCE cells. */
3009 int
3010 circuit_can_use_tap(const origin_circuit_t *circ)
3011 {
3012  tor_assert(circ);
3013  tor_assert(circ->cpath);
3014  tor_assert(circ->cpath->extend_info);
3015  return (circuit_purpose_can_use_tap_impl(circ->base_.purpose) &&
3016  extend_info_supports_tap(circ->cpath->extend_info));
3017 }
3018 
3019 /* Does circ have an onion key which it's allowed to use? */
3020 int
3021 circuit_has_usable_onion_key(const origin_circuit_t *circ)
3022 {
3023  tor_assert(circ);
3024  tor_assert(circ->cpath);
3025  tor_assert(circ->cpath->extend_info);
3026  return (extend_info_supports_ntor(circ->cpath->extend_info) ||
3027  circuit_can_use_tap(circ));
3028 }
3029 
3030 /* Does ei have an onion key which it would prefer to use?
3031  * Currently, we prefer ntor keys*/
3032 int
3033 extend_info_has_preferred_onion_key(const extend_info_t* ei)
3034 {
3035  tor_assert(ei);
3036  return extend_info_supports_ntor(ei);
3037 }
3038 
3042 void
3044 {
3045  smartlist_t *to_upgrade =
3047 
3048  if (to_upgrade == NULL)
3049  return;
3050 
3051  log_info(LD_GUARD, "Upgrading %d circuits from 'waiting for better guard' "
3052  "to 'open'.", smartlist_len(to_upgrade));
3053 
3054  SMARTLIST_FOREACH_BEGIN(to_upgrade, origin_circuit_t *, circ) {
3056  circuit_has_opened(circ);
3057  } SMARTLIST_FOREACH_END(circ);
3058 
3059  smartlist_free(to_upgrade);
3060 }
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
#define CIRCUIT_PURPOSE_C_INTRODUCING
Definition: circuitlist.h:74
channel_state_t state
Definition: channel.h:192
Header file for circuitstats.c.
#define CIRCLAUNCH_ONEHOP_TUNNEL
Definition: circuituse.h:39
Header file for rendcommon.c.
void circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
Definition: circuitlist.c:574
#define CIRCLAUNCH_IS_INTERNAL
Definition: circuituse.h:46
static void warn_if_last_router_excluded(origin_circuit_t *circ, const extend_info_t *exit_ei)
uint8_t onionskin[CELL_PAYLOAD_SIZE - 4]
Definition: onion.h:32
struct circuit_guard_state_t * guard_state
circ_id_type_bitfield_t circ_id_type
Definition: channel.h:411
static smartlist_t * build_middle_exclude_list(uint8_t purpose, cpath_build_state_t *state, crypt_path_t *head, int cur_len)
const char * channel_get_canonical_remote_descr(channel_t *chan)
Definition: channel.c:2836
int circuit_init_cpath_crypto(crypt_path_t *cpath, const char *key_data, size_t key_data_len, int reverse, int is_hs_v3)
int node_allows_single_hop_exits(const node_t *node)
Definition: nodelist.c:1408
#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
static void circuit_chan_publish(const origin_circuit_t *circ, const channel_t *chan)
Definition: circuitbuild.c:524
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:87
int channel_is_outgoing(channel_t *chan)
Definition: channel.c:3060
void pathbias_count_build_success(origin_circuit_t *circ)
Definition: circpathbias.c:506
static smartlist_t * circuit_get_unhandled_ports(time_t now)
Header file for circuitbuild.c.
uint8_t payload[CELL_PAYLOAD_SIZE]
Definition: cell_st.h:16
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)
Common functions for using (pseudo-)random number generators.
int extend_cell_format(uint8_t *command_out, uint16_t *len_out, uint8_t *payload_out, const extend_cell_t *cell_in)
Definition: onion.c:591
Definition: node_st.h:28
uint8_t cell_type
Definition: onion.h:38
int node_supports_ed25519_link_authentication(const node_t *node, int compatible_with_us)
Definition: nodelist.c:1135
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
uint16_t handshake_len
Definition: onion.h:30
int routerset_contains_node(const routerset_t *set, const node_t *node)
Definition: routerset.c:334
Header file for command.c.
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
extend_info_t * extend_info_from_node(const node_t *node, int for_direct_connect)
channel_t * channel_get_for_extend(const char *rsa_id_digest, const ed25519_public_key_t *ed_id, const tor_addr_t *target_addr, const char **msg_out, int *launch_out)
Definition: channel.c:2383
int circuit_handle_first_hop(origin_circuit_t *circ)
Definition: circuitbuild.c:541
addr_policy_result_t
Definition: policies.h:38
struct crypt_path_t * next
Definition: crypt_path_st.h:58
Header file for node_select.c.
void circpad_machine_event_circ_added_hop(origin_circuit_t *on_circ)
const char * extend_info_describe(const extend_info_t *ei)
Definition: describe.c:154
Header file for connection.c.
void circuit_n_chan_done(channel_t *chan, int status, int close_origin_circuits)
Definition: circuitbuild.c:629
int tor_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
Definition: cell_st.h:12
#define LD_GENERAL
Definition: log.h:58
int route_len_for_purpose(uint8_t purpose, extend_info_t *exit_ei)
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:143
time_t timestamp_created
Definition: channel.h:295
#define CIRCLAUNCH_NEED_UPTIME
Definition: circuituse.h:41
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
int circuit_finish_handshake(origin_circuit_t *circ, const created_cell_t *reply)
origin_circuit_t * origin_circuit_init(uint8_t purpose, int flags)
Definition: circuitbuild.c:454
uint8_t state
Definition: connection_st.h:44
#define CIRCUIT_PURPOSE_REND_POINT_WAITING
Definition: circuitlist.h:44
const char * circuit_purpose_to_string(uint8_t purpose)
Definition: circuitlist.c:896
void rep_hist_remove_predicted_ports(const smartlist_t *rmv_ports)
#define LOG_INFO
Definition: log.h:41
Header file for describe.c.
#define DEFAULT_ROUTE_LEN
Definition: or.h:1003
Header file for nodelist.c.
#define CIRCWINDOW_START
Definition: or.h:501
crypt_path_t * cpath
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:631
const ed25519_public_key_t * node_get_ed25519_id(const node_t *node)
Definition: nodelist.c:1051
void node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1684
void relay_header_unpack(relay_header_t *dest, const uint8_t *src)
Definition: relay.c:484
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)
void note_that_we_maybe_cant_complete_circuits(void)
Definition: mainloop.c:237
Header file for onion_tap.c.
void note_that_we_completed_a_circuit(void)
Definition: mainloop.c:229
int circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1544
int circuit_extend(cell_t *cell, circuit_t *circ)
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:41
int circuit_truncated(origin_circuit_t *circ, int reason)
#define MAX_NICKNAME_LEN
Definition: or.h:113
int onion_skin_create(int type, const extend_info_t *node, onion_handshake_state_t *state_out, uint8_t *onion_skin_out)
Definition: onion_crypto.c:110
#define TO_CIRCUIT(x)
Definition: or.h:947
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:84
Header file for config.c.
static crypt_path_t * onion_next_hop_in_cpath(crypt_path_t *cpath)
Header file for nickname.c.
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
char * circuit_list_path_for_controller(origin_circuit_t *circ)
Definition: circuitbuild.c:348
Header file for microdesc.c.
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:100
Header file for onion.c.
void circuit_set_state(circuit_t *circ, uint8_t state)
Definition: circuitlist.c:542
const node_t * choose_good_entry_server(uint8_t purpose, cpath_build_state_t *state, circuit_guard_state_t **guard_state_out)
const char * channel_state_to_string(channel_state_t state)
Definition: channel.c:315
static int circuit_purpose_may_omit_guard(int purpose)
Definition: circuitbuild.c:910
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
unsigned int num_n_circuits
Definition: channel.h:416
channel_usage_info_t channel_usage
Definition: channel.h:228
#define tor_free(p)
Definition: malloc.h:52
int channel_is_local(channel_t *chan)
Definition: channel.c:3014
#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
STATIC int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
Header file for mainloop.c.
#define CIRCUIT_PURPOSE_REND_ESTABLISHED
Definition: circuitlist.h:46
circuitmux_t * cmux
Definition: channel.h:403
int circuit_get_cpath_len(origin_circuit_t *circ)
Definition: circuitlist.c:2039
uint8_t purpose
Definition: circuit_st.h:96
void append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan, cell_t *cell, cell_direction_t direction, streamid_t fromstream)
Definition: relay.c:3012
STATIC int onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit_ei, int is_hs_v3_rp_circuit)
char rend_circ_nonce[DIGEST_LEN]
Definition: or_circuit_st.h:51
int extend_cell_parse(extend_cell_t *cell_out, const uint8_t command, const uint8_t *payload, size_t payload_length)
Definition: onion.c:378
int node_has_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:1872
origin_circuit_t * circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
Definition: circuitbuild.c:480
circuit_guard_state_t * origin_circuit_get_guard_state(origin_circuit_t *circ)
Definition: circuitbuild.c:509
static const node_t * pick_restricted_middle_node(router_crn_flags_t flags, const routerset_t *pick_from, const routerset_t *exclude_set, const smartlist_t *exclude_list, int position_hint)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
struct ed25519_public_key_t ed_pubkey
Definition: onion.h:57
#define END_CIRC_REASON_FLAG_REMOTE
Definition: or.h:328
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:49
crypto_pk_t * onion_key
#define CIRCUIT_PURPOSE_C_HSDIR_GET
Definition: circuitlist.h:91
#define CIRCLAUNCH_IS_V3_RP
Definition: circuituse.h:49
extend_info_t * n_hop
Definition: circuit_st.h:76
int node_is_unreliable(const node_t *node, int need_uptime, int need_capacity, int need_guard)
Definition: nodelist.c:2195
void reset_all_main_loop_timers(void)
Definition: mainloop.c:1485
struct create_cell_t * n_chan_create_cell
Definition: circuit_st.h:112
int circuit_should_use_vanguards(uint8_t purpose)
Definition: circuituse.c:1993
static const node_t * choose_good_exit_server_general(router_crn_flags_t flags)
#define MIN_CIRCUITS_HANDLING_STREAM
Definition: or.h:180
Header file for policies.c.
channel_t * p_chan
Definition: or_circuit_st.h:35
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
#define LD_APP
Definition: log.h:74
#define CIRCUIT_PURPOSE_CONTROLLER
Definition: circuitlist.h:117
void nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
Definition: nodelist.c:2099
int control_event_general_status(int severity, const char *format,...)
Definition: control.c:6822
static void circuit_pick_extend_handshake(uint8_t *cell_type_out, uint8_t *create_cell_type_out, uint16_t *handshake_type_out, const extend_info_t *ei)
Definition: circuitbuild.c:884
int32_t circuit_initial_package_window(void)
Definition: circuitlist.c:965
relay_crypto_t crypto
Definition: crypt_path_st.h:34
const curve25519_public_key_t * node_get_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:1879
Header file for channel.c.
int ExtendAllowPrivateAddresses
tor_assert(buffer)
void channel_timestamp_client(channel_t *chan)
Definition: channel.c:3208
routerset_t * ExitNodes
Definition: or_options_st.h:72
#define MAX_VERBOSE_NICKNAME_LEN
Definition: or.h:119
guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2432
int node_get_purpose(const node_t *node)
Definition: nodelist.c:1361
#define LD_CIRC
Definition: log.h:78
uint8_t state
Definition: circuit_st.h:95
static void circuit_free_cpath_node(crypt_path_t *victim)
Definition: circuitlist.c:1331
Header file for routermode.c.
void ocirc_event_publish(const ocirc_event_msg_t *msg)
Definition: ocirc_event.c:70
int connection_ap_can_use_exit(const entry_connection_t *conn, const node_t *exit_node)
#define LD_GUARD
Definition: log.h:105
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
const node_t * guards_choose_guard(cpath_build_state_t *state, uint8_t purpose, circuit_guard_state_t **guard_state_out)
Definition: entrynodes.c:3613
static int circuit_build_no_more_hops(origin_circuit_t *circ)
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1168
void clear_broken_connection_map(int stop_recording)
routerset_t * ExcludeNodes
Definition: or_options_st.h:84
circid_t n_circ_id
Definition: circuit_st.h:67
static int circuit_deliver_create_cell(circuit_t *circ, const create_cell_t *create_cell, int relayed)
Definition: circuitbuild.c:713
time_t circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1557
Header file for circuitpadding.c.
char nickname[MAX_HEX_NICKNAME_LEN+1]
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
Definition: circuitlist.h:80
#define CIRCUIT_PURPOSE_TESTING
Definition: circuitlist.h:115
const char * node_describe(const node_t *node)
Definition: describe.c:101
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
char identity[DIGEST_LEN]
Definition: node_st.h:40
Header file for circuitbuild.c.
void ed25519_pubkey_copy(ed25519_public_key_t *dest, const ed25519_public_key_t *src)
static int circuit_cpath_supports_ntor(const origin_circuit_t *circ)
Definition: circuitbuild.c:367
Master header file for Tor-specific functionality.
#define CIRCUIT_STATE_OPEN
Definition: circuitlist.h:31
uint8_t reply[CELL_PAYLOAD_SIZE - 2]
Definition: onion.h:42
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
int extend_info_is_a_configured_bridge(const extend_info_t *ei)
Definition: bridges.c:264
Header file for circuitbuild.c.
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1373
uint16_t marked_for_close
void circuit_mark_all_unused_circs(void)
Definition: circuitlist.c:2091
#define AP_CONN_STATE_CIRCUIT_WAIT
STATIC circid_t get_unique_circ_id_by_chan(channel_t *chan)
Definition: circuitbuild.c:128
Header file for onion_fast.c.
uint8_t cell_type
Definition: onion.h:26
circid_t circ_id
Definition: cell_st.h:13
#define LOG_WARN
Definition: log.h:49
extend_info_t * extend_info_dup(extend_info_t *info)
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:769
unsigned int type
Definition: connection_st.h:45
const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
Definition: node_select.c:849
static int middle_node_must_be_vanguard(const or_options_t *options, uint8_t purpose, int cur_len)
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:260
char nickname[MAX_NICKNAME_LEN+1]
char rend_circ_nonce[DIGEST_LEN]
Definition: crypt_path_st.h:44
Header file for circuituse.c.
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:476
int onionskin_answer(or_circuit_t *circ, const created_cell_t *created_cell, const char *keys, size_t keys_len, const uint8_t *rend_circ_nonce)
static const node_t * pick_vanguard_middle_node(const or_options_t *options, router_crn_flags_t flags, int cur_len, const smartlist_t *excluded)
static int should_use_create_fast_for_circuit(origin_circuit_t *circ)
Definition: circuitbuild.c:809
static channel_t * channel_connect_for_circuit(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id)
Definition: circuitbuild.c:109
const node_t * build_state_get_exit_node(cpath_build_state_t *state)
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:82
static const node_t * choose_good_middle_server(uint8_t purpose, cpath_build_state_t *state, crypt_path_t *head, int cur_len)
static int ap_stream_wants_exit_attention(connection_t *conn)
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.
crypto_pk_t * crypto_pk_dup_key(crypto_pk_t *orig)
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:358
Headers for transports.c.
smartlist_t * rep_hist_get_predicted_ports(time_t now)
#define LD_OR
Definition: log.h:88
int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
Definition: channel.c:3302
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
#define CIRCLAUNCH_NEED_CAPACITY
Definition: circuituse.h:43
int tor_addr_is_multicast(const tor_addr_t *a)
Definition: address.c:1499
uint8_t cell_type
Definition: onion.h:48
router_crn_flags_t
Definition: node_select.h:16
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
int is_legal_nickname(const char *s)
Definition: nickname.c:19
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:264
#define CIRCUIT_PURPOSE_HS_VANGUARDS
Definition: circuitlist.h:127
#define END_CIRC_REASON_NOPATH
Definition: or.h:303
uint16_t length
Definition: or.h:642
uint8_t command
Definition: or.h:638
struct crypt_path_t * prev
Definition: crypt_path_st.h:61
cpath_build_state_t * build_state
Header file for connection_edge.c.
int control_event_server_status(int severity, const char *format,...)
Definition: control.c:6862
routerset_t * HSLayer3Nodes
void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime, int need_capacity, int need_guard, int need_desc, int pref_addr, int direct_conn)
Definition: routerlist.c:504
routerset_t * ExcludeExitNodesUnion_
Definition: or_options_st.h:93
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
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
int circuit_send_next_onion_skin(origin_circuit_t *circ)
Definition: circuitbuild.c:939
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2110
void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
#define CIRCUIT_PURPOSE_INTRO_POINT
Definition: circuitlist.h:41
Header file for relay.c.
edge_connection_t * p_streams
char identity_digest[DIGEST_LEN]
#define SMARTLIST_FOREACH(sl, type, var, cmd)
unsigned int has_opened
#define CIRCUIT_PURPOSE_S_HSDIR_POST
Definition: circuitlist.h:111
Header file for router.c.
void routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
Definition: routerset.c:394
origin_circuit_t * origin_circuit_new(void)
Definition: circuitlist.c:1007
int created_cell_format(cell_t *cell_out, const created_cell_t *cell_in)
Definition: onion.c:546
static char * circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
Definition: circuitbuild.c:265
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
Definition: circuitlist.h:77
int circuit_get_cpath_opened_len(const origin_circuit_t *circ)
Definition: circuitlist.c:2055
char * nickname
Definition: routerinfo_st.h:17
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:89
uint8_t state
Definition: crypt_path_st.h:54
void control_event_bootstrap(bootstrap_status_t status, int progress)
onion_handshake_state_t handshake_state
Definition: crypt_path_st.h:38
uint16_t handshake_type
Definition: onion.h:28
uint32_t circid_t
Definition: or.h:608
#define CIRCUIT_PURPOSE_OR
Definition: circuitlist.h:38
uint16_t dir_port
Definition: routerinfo_st.h:21
struct timeval timestamp_began
Definition: circuit_st.h:124
#define log_fn(severity, domain, args,...)
Definition: log.h:255
#define CIRCUIT_STATE_GUARD_WAIT
Definition: circuitlist.h:29
ratelim_t last_warned_circ_ids_exhausted
Definition: channel.h:444
static void circuit_pick_create_handshake(uint8_t *cell_type_out, uint16_t *handshake_type_out, const extend_info_t *ei)
Definition: circuitbuild.c:858
#define CIRCUIT_STATE_BUILDING
Definition: circuitlist.h:20
uint8_t node_id[DIGEST_LEN]
Definition: onion.h:55
int router_digest_is_me(const char *digest)
Definition: router.c:1589
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:151
unsigned int circuitmux_num_circuits(circuitmux_t *cmux)
Definition: circuitmux.c:763
Header file for control.c.
const node_t * node_sl_choose_by_bandwidth(const smartlist_t *sl, bandwidth_weight_rule_t rule)
Definition: node_select.c:801
static int node_handles_some_port(const node_t *node, smartlist_t *needed_ports)
void circpad_machine_event_circ_built(origin_circuit_t *circ)
void circuit_reset_failure_count(int timeout)
Definition: circuituse.c:2190
channel_t * channel_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id)
Definition: channel.c:2305
char * circuit_list_path(origin_circuit_t *circ, int verbose)
Definition: circuitbuild.c:339
#define CIRCUIT_PURPOSE_C_GENERAL
Definition: circuitlist.h:71
static const node_t * choose_good_exit_server(origin_circuit_t *circ, router_crn_flags_t flags, int is_internal)
time_t approx_time(void)
Definition: approx_time.c:32
void fascist_firewall_choose_address_node(const node_t *node, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:1022
int node_exit_policy_rejects_all(const node_t *node)
Definition: nodelist.c:1419
routerset_t * MiddleNodes
Definition: or_options_st.h:75
extend_info_t * extend_info
Definition: crypt_path_st.h:47
#define CIRCUIT_STATE_CHAN_WAIT
Definition: circuitlist.h:25
uint32_t addr
Definition: routerinfo_st.h:19
void circuit_set_n_circid_chan(circuit_t *circ, circid_t id, channel_t *chan)
Definition: circuitlist.c:471
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
Definition: or.h:549
void command_setup_channel(channel_t *chan)
Definition: command.c:683
tor_addr_port_t orport_ipv4
Definition: onion.h:50
Header file for selftest.c.
int pathbias_count_build_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:420
Header file for ocirc_event.c.
void circuit_build_times_handle_completed_hop(origin_circuit_t *circ)
Definition: circuitstats.c:682
Header file for connection_or.c.
int control_event_client_status(int severity, const char *format,...)
Definition: control.c:6842
int circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
void routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset, const routerset_t *excludeset, int running_only)
Definition: routerset.c:360
extend_info_t * chosen_exit
unsigned int circuitmux_num_active_circuits(circuitmux_t *cmux)
Definition: circuitmux.c:751
static smartlist_t * build_vanguard_middle_exclude_list(uint8_t purpose, cpath_build_state_t *state, crypt_path_t *head, int cur_len)
char identity_digest[DIGEST_LEN]
Definition: channel.h:384
uint16_t handshake_len
Definition: onion.h:40
relay_crypto_t crypto
Definition: or_circuit_st.h:44
struct edge_connection_t * next_stream
STATIC int new_route_len(uint8_t purpose, extend_info_t *exit_ei, smartlist_t *nodes)
tor_addr_t addr
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:903
void onion_handshake_state_release(onion_handshake_state_t *state)
Definition: onion_crypto.c:79
void extend_info_free_(extend_info_t *info)
STATIC int onion_extend_cpath(origin_circuit_t *circ)
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:109
#define CONN_TYPE_AP
Definition: connection.h:31
ed25519_public_key_t ed_identity
void smartlist_clear(smartlist_t *sl)
#define LD_PROTOCOL
Definition: log.h:68
static int onion_populate_cpath(origin_circuit_t *circ)
Definition: circuitbuild.c:391
smartlist_t * circuit_find_circuits_to_upgrade_from_guard_wait(void)
Definition: circuitlist.c:2011
void router_orport_found_reachable(void)
Definition: selftest.c:219
const char * build_state_get_exit_nickname(cpath_build_state_t *state)
static int circuit_send_intermediate_onion_skin(origin_circuit_t *circ, crypt_path_t *hop)
#define RELAY_HEADER_SIZE
Definition: or.h:603
int check_whether_orport_reachable(const or_options_t *options)
Definition: selftest.c:74
int extend_info_addr_is_allowed(const tor_addr_t *addr)
int onion_skin_client_handshake(int type, const onion_handshake_state_t *handshake_state, const uint8_t *reply, size_t reply_len, uint8_t *keys_out, size_t keys_out_len, uint8_t *rend_authenticator_out, const char **msg_out)
Definition: onion_crypto.c:247
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:82
void circuit_upgrade_circuits_from_guard_wait(void)
int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20
int inform_testing_reachability(void)
Definition: circuitbuild.c:776
MOCK_IMPL(int, circuit_all_predicted_ports_handled,(time_t now, int *need_uptime, int *need_capacity))
Header file for routerlist.c.
const uint8_t * build_state_get_exit_rsa_id(cpath_build_state_t *state)
uint16_t or_port
Definition: routerinfo_st.h:20
channel_t * n_chan
Definition: circuit_st.h:58
static int circuit_send_first_onion_skin(origin_circuit_t *circ)
Definition: circuitbuild.c:973
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:179
void * smartlist_choose(const smartlist_t *sl)
Definition: crypto_rand.c:589
create_cell_t create_cell
Definition: onion.h:61
Header for confparse.c.
void circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle)
int have_completed_a_circuit(void)
Definition: mainloop.c:221
tor_addr_port_t orport_ipv6
Definition: onion.h:53
uint64_t global_identifier
Definition: channel.h:197
Header file for onion_crypto.c.
char * tor_dup_ip(uint32_t addr)
Definition: address.c:1890