Tor 0.4.9.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-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file circuitbuild.c
9 *
10 * \brief Implements the details of building circuits (by choosing paths,
11 * constructing/sending create/extend cells, and so on).
12 *
13 * On the client side, this module handles launching circuits. Circuit
14 * launches are started from circuit_establish_circuit(), called from
15 * circuit_launch_by_extend_info()). To choose the path the circuit will
16 * take, onion_extend_cpath() calls into a maze of node selection functions.
17 *
18 * Once the circuit is ready to be launched, the first hop is treated as a
19 * special case with circuit_handle_first_hop(), since it might need to open a
20 * channel. As the channel opens, and later as CREATED and RELAY_EXTENDED
21 * cells arrive, the client will invoke circuit_send_next_onion_skin() to send
22 * CREATE or RELAY_EXTEND cells.
23 *
24 * The server side is handled in feature/relay/circuitbuild_relay.c.
25 **/
26
27#define CIRCUITBUILD_PRIVATE
28#define OCIRC_EVENT_PRIVATE
29
30#include "core/or/or.h"
31#include "app/config/config.h"
32#include "lib/confmgt/confmgt.h"
33#include "core/crypto/hs_ntor.h"
39#include "core/or/channel.h"
41#include "core/or/circuitlist.h"
43#include "core/or/circuituse.h"
45#include "core/or/command.h"
49#include "core/or/extendinfo.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 "core/or/trace_probes_circuit.h"
55#include "core/or/crypt_path.h"
57#include "feature/client/circpathbias.h"
75#include "lib/trace/events.h"
77
78#include "core/or/cell_st.h"
83#include "core/or/or_circuit_st.h"
85
86#include "trunnel/extension.h"
87#include "trunnel/congestion_control.h"
88
92 crypt_path_t *hop);
94 uint8_t purpose,
96 crypt_path_t *head,
97 int cur_len);
98
99/** This function tries to get a channel to the specified endpoint,
100 * and then calls command_setup_channel() to give it the right
101 * callbacks.
102 */
105{
106 channel_t *chan;
107
108 const tor_addr_port_t *orport = extend_info_pick_orport(ei);
109 if (!orport)
110 return NULL;
111 const char *id_digest = ei->identity_digest;
112 const ed25519_public_key_t *ed_id = &ei->ed_identity;
113
114 chan = channel_connect(&orport->addr, orport->port, id_digest, ed_id);
115 if (chan) command_setup_channel(chan);
116
117 return chan;
118}
119
120/** Search for a value for circ_id that we can use on <b>chan</b> for an
121 * outbound circuit, until we get a circ_id that is not in use by any other
122 * circuit on that conn.
123 *
124 * Return it, or 0 if can't get a unique circ_id.
125 */
128{
129/* This number is chosen somewhat arbitrarily; see comment below for more
130 * info. When the space is 80% full, it gives a one-in-a-million failure
131 * chance; when the space is 90% full, it gives a one-in-850 chance; and when
132 * the space is 95% full, it gives a one-in-26 failure chance. That seems
133 * okay, though you could make a case IMO for anything between N=32 and
134 * N=256. */
135#define MAX_CIRCID_ATTEMPTS 64
136 int in_use;
137 unsigned n_with_circ = 0, n_pending_destroy = 0, n_weird_pending_destroy = 0;
138 circid_t test_circ_id;
139 circid_t attempts=0;
140 circid_t high_bit, max_range, mask;
141 int64_t pending_destroy_time_total = 0;
142 int64_t pending_destroy_time_max = 0;
143
144 tor_assert(chan);
145
146 if (chan->circ_id_type == CIRC_ID_TYPE_NEITHER) {
147 log_warn(LD_BUG,
148 "Trying to pick a circuit ID for a connection from "
149 "a client with no identity.");
150 return 0;
151 }
152 max_range = (chan->wide_circ_ids) ? (1u<<31) : (1u<<15);
153 mask = max_range - 1;
154 high_bit = (chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ? max_range : 0;
155 do {
156 if (++attempts > MAX_CIRCID_ATTEMPTS) {
157 /* Make sure we don't loop forever because all circuit IDs are used.
158 *
159 * Once, we would try until we had tried every possible circuit ID. But
160 * that's quite expensive. Instead, we try MAX_CIRCID_ATTEMPTS random
161 * circuit IDs, and then give up.
162 *
163 * This potentially causes us to give up early if our circuit ID space
164 * is nearly full. If we have N circuit IDs in use, then we will reject
165 * a new circuit with probability (N / max_range) ^ MAX_CIRCID_ATTEMPTS.
166 * This means that in practice, a few percent of our circuit ID capacity
167 * will go unused.
168 *
169 * The alternative here, though, is to do a linear search over the
170 * whole circuit ID space every time we extend a circuit, which is
171 * not so great either.
172 */
173 int64_t queued_destroys;
175 approx_time());
176 if (m == NULL)
177 return 0; /* This message has been rate-limited away. */
178 if (n_pending_destroy)
179 pending_destroy_time_total /= n_pending_destroy;
180 log_warn(LD_CIRC,"No unused circIDs found on channel %s wide "
181 "circID support, with %u inbound and %u outbound circuits. "
182 "Found %u circuit IDs in use by circuits, and %u with "
183 "pending destroy cells. (%u of those were marked bogusly.) "
184 "The ones with pending destroy cells "
185 "have been marked unusable for an average of %ld seconds "
186 "and a maximum of %ld seconds. This channel is %ld seconds "
187 "old. Failing a circuit.%s",
188 chan->wide_circ_ids ? "with" : "without",
189 chan->num_p_circuits, chan->num_n_circuits,
190 n_with_circ, n_pending_destroy, n_weird_pending_destroy,
191 (long)pending_destroy_time_total,
192 (long)pending_destroy_time_max,
193 (long)(approx_time() - chan->timestamp_created),
194 m);
195 tor_free(m);
196
197 if (!chan->cmux) {
198 /* This warning should be impossible. */
199 log_warn(LD_BUG, " This channel somehow has no cmux on it!");
200 return 0;
201 }
202
203 /* analysis so far on 12184 suggests that we're running out of circuit
204 IDs because it looks like we have too many pending destroy
205 cells. Let's see how many we really have pending.
206 */
207 queued_destroys = circuitmux_count_queued_destroy_cells(chan,
208 chan->cmux);
209
210 log_warn(LD_CIRC, " Circuitmux on this channel has %u circuits, "
211 "of which %u are active. It says it has %"PRId64
212 " destroy cells queued.",
215 (queued_destroys));
216
217 /* Change this into "if (1)" in order to get more information about
218 * possible failure modes here. You'll need to know how to use gdb with
219 * Tor: this will make Tor exit with an assertion failure if the cmux is
220 * corrupt. */
221 if (0)
222 circuitmux_assert_okay(chan->cmux);
223
225
226 return 0;
227 }
228
229 do {
230 crypto_rand((char*) &test_circ_id, sizeof(test_circ_id));
231 test_circ_id &= mask;
232 } while (test_circ_id == 0);
233
234 test_circ_id |= high_bit;
235
236 in_use = circuit_id_in_use_on_channel(test_circ_id, chan);
237 if (in_use == 1)
238 ++n_with_circ;
239 else if (in_use == 2) {
240 time_t since_when;
241 ++n_pending_destroy;
242 since_when =
244 if (since_when) {
245 time_t waiting = approx_time() - since_when;
246 pending_destroy_time_total += waiting;
247 if (waiting > pending_destroy_time_max)
248 pending_destroy_time_max = waiting;
249 } else {
250 ++n_weird_pending_destroy;
251 }
252 }
253 } while (in_use);
254 return test_circ_id;
255}
256
257/** If <b>verbose</b> is false, allocate and return a comma-separated list of
258 * the currently built elements of <b>circ</b>. If <b>verbose</b> is true, also
259 * list information about link status in a more verbose format using spaces.
260 * If <b>verbose_names</b> is false, give hex digests; if <b>verbose_names</b>
261 * is true, use $DIGEST=Name style names.
262 */
263static char *
264circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
265{
266 crypt_path_t *hop;
267 smartlist_t *elements;
268 const char *states[] = {"closed", "waiting for keys", "open"};
269 char *s;
270
271 elements = smartlist_new();
272
273 if (verbose) {
274 const char *nickname = build_state_get_exit_nickname(circ->build_state);
275 smartlist_add_asprintf(elements, "%s%s circ (length %d%s%s):",
276 circ->build_state->is_internal ? "internal" : "exit",
277 circ->build_state->need_uptime ? " (high-uptime)" : "",
279 circ->base_.state == CIRCUIT_STATE_OPEN ? "" : ", last hop ",
280 circ->base_.state == CIRCUIT_STATE_OPEN ? "" :
281 (nickname?nickname:"*unnamed*"));
282 }
283
284 hop = circ->cpath;
285 do {
286 char *elt;
287 const char *id;
288 const node_t *node;
289 if (!hop)
290 break;
291 if (!verbose && hop->state != CPATH_STATE_OPEN)
292 break;
293 if (!hop->extend_info)
294 break;
295 id = hop->extend_info->identity_digest;
296 if (verbose_names) {
297 elt = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
298 if ((node = node_get_by_id(id))) {
299 node_get_verbose_nickname(node, elt);
300 } else if (is_legal_nickname(hop->extend_info->nickname)) {
301 elt[0] = '$';
303 elt[HEX_DIGEST_LEN+1]= '~';
304 strlcpy(elt+HEX_DIGEST_LEN+2,
306 } else {
307 elt[0] = '$';
309 }
310 } else { /* ! verbose_names */
311 elt = tor_malloc(HEX_DIGEST_LEN+2);
312 elt[0] = '$';
314 }
315 tor_assert(elt);
316 if (verbose) {
317 tor_assert(hop->state <= 2);
318 smartlist_add_asprintf(elements,"%s(%s)",elt,states[hop->state]);
319 tor_free(elt);
320 } else {
321 smartlist_add(elements, elt);
322 }
323 hop = hop->next;
324 } while (hop != circ->cpath);
325
326 s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
327 SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
328 smartlist_free(elements);
329 return s;
330}
331
332/** If <b>verbose</b> is false, allocate and return a comma-separated
333 * list of the currently built elements of <b>circ</b>. If
334 * <b>verbose</b> is true, also list information about link status in
335 * a more verbose format using spaces.
336 */
337char *
339{
340 return circuit_list_path_impl(circ, verbose, 0);
341}
342
343/** Allocate and return a comma-separated list of the currently built elements
344 * of <b>circ</b>, giving each as a verbose nickname.
345 */
346char *
348{
349 return circuit_list_path_impl(circ, 0, 1);
350}
351
352/** Log, at severity <b>severity</b>, the nicknames of each router in
353 * <b>circ</b>'s cpath. Also log the length of the cpath, and the intended
354 * exit point.
355 */
356void
357circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
358{
359 char *s = circuit_list_path(circ,1);
360 tor_log(severity,domain,"%s",s);
361 tor_free(s);
362}
363
364/** Return 1 iff every node in circ's cpath definitely supports ntor. */
365static int
367{
368 crypt_path_t *head, *cpath;
369
370 cpath = head = circ->cpath;
371 do {
372 /* if the extend_info is missing, we can't tell if it supports ntor */
373 if (!cpath->extend_info) {
374 return 0;
375 }
376
377 /* if the key is blank, it definitely doesn't support ntor */
378 if (!extend_info_supports_ntor(cpath->extend_info)) {
379 return 0;
380 }
381 cpath = cpath->next;
382 } while (cpath != head);
383
384 return 1;
385}
386
387/** Pick all the entries in our cpath. Stop and return 0 when we're
388 * happy, or return -1 if an error occurs. */
389static int
391{
392 int r = 0;
393
394 /* onion_extend_cpath assumes these are non-NULL */
395 tor_assert(circ);
396 tor_assert(circ->build_state);
397
398 while (r == 0) {
399 r = onion_extend_cpath(circ);
400 if (r < 0) {
401 log_info(LD_CIRC,"Generating cpath hop failed.");
402 return -1;
403 }
404 }
405
406 /* The path is complete */
407 tor_assert(r == 1);
408
409 /* Does every node in this path support ntor? */
410 int path_supports_ntor = circuit_cpath_supports_ntor(circ);
411
412 /* We would like every path to support ntor, but we have to allow for some
413 * edge cases. */
415 if (circuit_can_use_tap(circ)) {
416 /* Circuits from clients to intro points, and hidden services to rend
417 * points do not support ntor, because the hidden service protocol does
418 * not include ntor onion keys. This is also true for Single Onion
419 * Services. */
420 return 0;
421 }
422
423 if (circuit_get_cpath_len(circ) == 1) {
424 /* Allow for bootstrapping: when we're fetching directly from a fallback,
425 * authority, or bridge, we have no way of knowing its ntor onion key
426 * before we connect to it. So instead, we try connecting, and end up using
427 * CREATE_FAST. */
428 tor_assert(circ->cpath);
430 const node_t *node = node_get_by_id(
432 /* If we don't know the node and its descriptor, we must be bootstrapping.
433 */
434 if (!node || !node_has_preferred_descriptor(node, 1)) {
435 return 0;
436 }
437 }
438
439 if (BUG(!path_supports_ntor)) {
440 /* If we're building a multi-hop path, and it's not one of the HS or
441 * bootstrapping exceptions, and it doesn't support ntor, something has
442 * gone wrong. */
443 return -1;
444 }
445
446 return 0;
447}
448
449/** Create and return a new origin circuit. Initialize its purpose and
450 * build-state based on our arguments. The <b>flags</b> argument is a
451 * bitfield of CIRCLAUNCH_* flags, see circuit_launch_by_extend_info() for
452 * more details. */
454origin_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));
461 ((flags & CIRCLAUNCH_ONEHOP_TUNNEL) ? 1 : 0);
462 circ->build_state->need_uptime =
463 ((flags & CIRCLAUNCH_NEED_UPTIME) ? 1 : 0);
465 ((flags & CIRCLAUNCH_NEED_CAPACITY) ? 1 : 0);
466 circ->build_state->is_internal =
467 ((flags & CIRCLAUNCH_IS_INTERNAL) ? 1 : 0);
469 ((flags & CIRCLAUNCH_IS_IPV6_SELFTEST) ? 1 : 0);
471 ((flags & CIRCLAUNCH_NEED_CONFLUX) ? 1 : 0);
472 circ->base_.purpose = purpose;
473 return circ;
474}
475
476/** Build a new circuit for <b>purpose</b>. If <b>exit</b> is defined, then use
477 * that as your exit router, else choose a suitable exit node. The <b>flags</b>
478 * argument is a bitfield of CIRCLAUNCH_* flags, see
479 * circuit_launch_by_extend_info() for more details.
480 *
481 * Also launch a connection to the first OR in the chosen path, if
482 * it's not open already.
483 */
485circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
486{
487 origin_circuit_t *circ;
488 int err_reason = 0;
489 int is_hs_v3_rp_circuit = 0;
490
491 if (flags & CIRCLAUNCH_IS_V3_RP) {
492 is_hs_v3_rp_circuit = 1;
493 }
494
495 circ = origin_circuit_init(purpose, flags);
496
497 if (onion_pick_cpath_exit(circ, exit_ei, is_hs_v3_rp_circuit) < 0 ||
498 onion_populate_cpath(circ) < 0) {
499 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
500 return NULL;
501 }
502
503 circuit_event_status(circ, CIRC_EVENT_LAUNCHED, 0);
504
505 if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
506 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
507 return NULL;
508 }
509
510 tor_trace(TR_SUBSYS(circuit), TR_EV(establish), circ);
511 return circ;
512}
513
514/**
515 * Build a new conflux circuit for <b>purpose</b>. If <b>exit</b> is defined,
516 * then use that as your exit router, else choose a suitable exit node.
517 * The <b>flags</b> argument is a bitfield of CIRCLAUNCH_* flags, see
518 * circuit_launch_by_extend_info() for more details.
519 *
520 * Also launch a connection to the first OR in the chosen path, if
521 * it's not open already.
522 */
524circuit_establish_circuit_conflux,(const uint8_t *conflux_nonce,
525 uint8_t purpose, extend_info_t *exit_ei,
526 int flags))
527{
528 origin_circuit_t *circ;
529 int err_reason = 0;
530
531 /* Right now, only conflux client circuits use this function */
533
534 circ = origin_circuit_init(purpose, flags);
535 TO_CIRCUIT(circ)->conflux_pending_nonce =
536 tor_memdup(conflux_nonce, DIGEST256_LEN);
537
538 if (onion_pick_cpath_exit(circ, exit_ei, 0) < 0 ||
539 onion_populate_cpath(circ) < 0) {
540 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
541 return NULL;
542 }
543
544 circuit_event_status(circ, CIRC_EVENT_LAUNCHED, 0);
545
546 if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
547 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
548 return NULL;
549 }
550
551 tor_trace(TR_SUBSYS(circuit), TR_EV(establish), circ);
552 return circ;
553}
554
555/** Return the guard state associated with <b>circ</b>, which may be NULL. */
556circuit_guard_state_t *
558{
559 return circ->guard_state;
560}
561
562/**
563 * Helper function to publish a channel association message
564 *
565 * circuit_handle_first_hop() calls this to notify subscribers about a
566 * channel launch event, which associates a circuit with a channel.
567 * This doesn't always correspond to an assignment of the circuit's
568 * n_chan field, because that seems to be only for fully-open
569 * channels.
570 **/
571static void
573{
574 ocirc_chan_msg_t *msg = tor_malloc(sizeof(*msg));
575
576 msg->gid = circ->global_identifier;
577 msg->chan = chan->global_identifier;
578 msg->onehop = circ->build_state->onehop_tunnel;
579
580 ocirc_chan_publish(msg);
581}
582
583/** Start establishing the first hop of our circuit. Figure out what
584 * OR we should connect to, and if necessary start the connection to
585 * it. If we're already connected, then send the 'create' cell.
586 * Return 0 for ok, -reason if circ should be marked-for-close. */
587int
589{
590 crypt_path_t *firsthop;
591 channel_t *n_chan;
592 int err_reason = 0;
593 const char *msg = NULL;
594 int should_launch = 0;
595 const or_options_t *options = get_options();
596
597 firsthop = cpath_get_next_non_open_hop(circ->cpath);
598 tor_assert(firsthop);
599 tor_assert(firsthop->extend_info);
600
601 /* Some bridges are on private addresses. Others pass a dummy private
602 * address to the pluggable transport, which ignores it.
603 * Deny the connection if:
604 * - the address is internal, and
605 * - we're not connecting to a configured bridge, and
606 * - we're not configured to allow extends to private addresses. */
609 !options->ExtendAllowPrivateAddresses) {
610 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
611 "Client asked me to connect directly to a private address");
612 return -END_CIRC_REASON_TORPROTOCOL;
613 }
614
615 /* now see if we're already connected to the first OR in 'route' */
616 const tor_addr_port_t *orport4 =
617 extend_info_get_orport(firsthop->extend_info, AF_INET);
618 const tor_addr_port_t *orport6 =
619 extend_info_get_orport(firsthop->extend_info, AF_INET6);
620 n_chan = channel_get_for_extend(
621 firsthop->extend_info->identity_digest,
622 &firsthop->extend_info->ed_identity,
623 orport4 ? &orport4->addr : NULL,
624 orport6 ? &orport6->addr : NULL,
625 true,
626 &msg,
627 &should_launch);
628
629 if (!n_chan) {
630 /* not currently connected in a useful way. */
631 log_info(LD_CIRC, "Next router is %s: %s",
632 safe_str_client(extend_info_describe(firsthop->extend_info)),
633 msg?msg:"???");
634 circ->base_.n_hop = extend_info_dup(firsthop->extend_info);
635
636 if (should_launch) {
637 n_chan = channel_connect_for_circuit(firsthop->extend_info);
638 if (!n_chan) { /* connect failed, forget the whole thing */
639 log_info(LD_CIRC,"connect to firsthop failed. Closing.");
640 return -END_CIRC_REASON_CONNECTFAILED;
641 }
642 /* We didn't find a channel, but we're launching one for an origin
643 * circuit. (If we decided not to launch a channel, then we found at
644 * least one once good in-progress channel use for this circuit, and
645 * marked it in channel_get_for_extend().) */
647 circuit_chan_publish(circ, n_chan);
648 }
649
650 log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
651 /* return success. The onion/circuit/etc will be taken care of
652 * automatically (may already have been) whenever n_chan reaches
653 * OR_CONN_STATE_OPEN.
654 */
655 return 0;
656 } else { /* it's already open. use it. */
657 tor_assert(!circ->base_.n_hop);
658 circ->base_.n_chan = n_chan;
659 /* We found a channel, and we're using it for an origin circuit. */
661 circuit_chan_publish(circ, n_chan);
662 log_debug(LD_CIRC,"Conn open for %s. Delivering first onion skin.",
663 safe_str_client(extend_info_describe(firsthop->extend_info)));
664 if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
665 log_info(LD_CIRC,"circuit_send_next_onion_skin failed.");
666 circ->base_.n_chan = NULL;
667 return err_reason;
668 }
669 }
670 return 0;
671}
672
673/** Find any circuits that are waiting on <b>or_conn</b> to become
674 * open and get them to send their create cells forward.
675 *
676 * Status is 1 if connect succeeded, or 0 if connect failed.
677 *
678 * Close_origin_circuits is 1 if we should close all the origin circuits
679 * through this channel, or 0 otherwise. (This happens when we want to retry
680 * an older guard.)
681 */
682void
683circuit_n_chan_done(channel_t *chan, int status, int close_origin_circuits)
684{
685 smartlist_t *pending_circs;
686 int err_reason = 0;
687
688 tor_assert(chan);
689
690 log_debug(LD_CIRC,"chan to %s, status=%d",
691 channel_describe_peer(chan), status);
692
693 pending_circs = smartlist_new();
694 circuit_get_all_pending_on_channel(pending_circs, chan);
695
696 SMARTLIST_FOREACH_BEGIN(pending_circs, circuit_t *, circ)
697 {
698 /* These checks are redundant wrt get_all_pending_on_or_conn, but I'm
699 * leaving them in in case it's possible for the status of a circuit to
700 * change as we're going down the list. */
701 if (circ->marked_for_close || circ->n_chan || !circ->n_hop ||
702 circ->state != CIRCUIT_STATE_CHAN_WAIT)
703 continue;
704
705 const char *rsa_ident = NULL;
706 const ed25519_public_key_t *ed_ident = NULL;
707 if (! tor_digest_is_zero(circ->n_hop->identity_digest)) {
708 rsa_ident = circ->n_hop->identity_digest;
709 }
710 if (! ed25519_public_key_is_zero(&circ->n_hop->ed_identity)) {
711 ed_ident = &circ->n_hop->ed_identity;
712 }
713
714 if (rsa_ident == NULL && ed_ident == NULL) {
715 /* Look at addr/port. This is an unkeyed connection. */
716 if (!channel_matches_extend_info(chan, circ->n_hop))
717 continue;
718 } else {
719 /* We expected a key or keys. See if they matched. */
720 if (!channel_remote_identity_matches(chan, rsa_ident, ed_ident))
721 continue;
722
723 /* If the channel is canonical, great. If not, it needs to match
724 * the requested address exactly. */
725 if (! chan->is_canonical &&
726 ! channel_matches_extend_info(chan, circ->n_hop)) {
727 continue;
728 }
729 }
730 if (!status) { /* chan failed; close circ */
731 log_info(LD_CIRC,"Channel failed; closing circ.");
732 circuit_mark_for_close(circ, END_CIRC_REASON_CHANNEL_CLOSED);
733 continue;
734 }
735
736 if (close_origin_circuits && CIRCUIT_IS_ORIGIN(circ)) {
737 log_info(LD_CIRC,"Channel deprecated for origin circs; closing circ.");
738 circuit_mark_for_close(circ, END_CIRC_REASON_CHANNEL_CLOSED);
739 continue;
740 }
741 log_debug(LD_CIRC, "Found circ, sending create cell.");
742 /* circuit_deliver_create_cell will set n_circ_id and add us to
743 * chan_circuid_circuit_map, so we don't need to call
744 * set_circid_chan here. */
745 circ->n_chan = chan;
746 extend_info_free(circ->n_hop);
747 circ->n_hop = NULL;
748
749 if (CIRCUIT_IS_ORIGIN(circ)) {
750 if ((err_reason =
752 log_info(LD_CIRC,
753 "send_next_onion_skin failed; circuit marked for closing.");
754 circuit_mark_for_close(circ, -err_reason);
755 continue;
756 /* XXX could this be bad, eg if next_onion_skin failed because conn
757 * died? */
758 }
759 } else {
760 /* pull the create cell out of circ->n_chan_create_cell, and send it */
761 tor_assert(circ->n_chan_create_cell);
762 if (circuit_deliver_create_cell(circ, circ->n_chan_create_cell, 1)<0) {
763 circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
764 continue;
765 }
766 tor_free(circ->n_chan_create_cell);
768 }
769 }
770 SMARTLIST_FOREACH_END(circ);
771
772 smartlist_free(pending_circs);
773}
774
775/** Find a new circid that isn't currently in use on the circ->n_chan
776 * for the outgoing
777 * circuit <b>circ</b>, and deliver the cell <b>create_cell</b> to this
778 * circuit. If <b>relayed</b> is true, this is a create cell somebody
779 * gave us via an EXTEND cell, so we shouldn't worry if we don't understand
780 * it. Return -1 if we failed to find a suitable circid, else return 0.
781 */
782MOCK_IMPL(int,
784 const struct create_cell_t *create_cell,
785 int relayed))
786{
787 cell_t cell;
788 circid_t id;
789 int r;
790
791 tor_assert(circ);
792 tor_assert(circ->n_chan);
793 tor_assert(create_cell);
794 tor_assert(create_cell->cell_type == CELL_CREATE ||
795 create_cell->cell_type == CELL_CREATE_FAST ||
796 create_cell->cell_type == CELL_CREATE2);
797
799 if (!id) {
800 static ratelim_t circid_warning_limit = RATELIM_INIT(9600);
801 log_fn_ratelim(&circid_warning_limit, LOG_WARN, LD_CIRC,
802 "failed to get unique circID.");
803 goto error;
804 }
805
806 tor_assert_nonfatal_once(circ->n_chan->is_canonical);
807
808 memset(&cell, 0, sizeof(cell_t));
809 r = relayed ? create_cell_format_relayed(&cell, create_cell)
810 : create_cell_format(&cell, create_cell);
811 if (r < 0) {
812 log_warn(LD_CIRC,"Couldn't format create cell");
813 goto error;
814 }
815 log_debug(LD_CIRC,"Chosen circID %u.", (unsigned)id);
816 circuit_set_n_circid_chan(circ, id, circ->n_chan);
817 cell.circ_id = circ->n_circ_id;
818
819 if (append_cell_to_circuit_queue(circ, circ->n_chan, &cell,
820 CELL_DIRECTION_OUT, 0) < 0) {
821 return -1;
822 }
823
824 if (CIRCUIT_IS_ORIGIN(circ)) {
825 /* Update began timestamp for circuits starting their first hop */
826 if (TO_ORIGIN_CIRCUIT(circ)->cpath->state == CPATH_STATE_CLOSED) {
827 if (!CHANNEL_IS_OPEN(circ->n_chan)) {
828 log_warn(LD_CIRC,
829 "Got first hop for a circuit without an opened channel. "
830 "State: %s.", channel_state_to_string(circ->n_chan->state));
832 }
833
835 }
836
837 /* mark it so it gets better rate limiting treatment. */
839 }
840
841 return 0;
842 error:
843 circ->n_chan = NULL;
844 return -1;
845}
846
847/** Return true iff we should send a create_fast cell to start building a
848 * given circuit */
849static inline bool
851{
852 tor_assert(circ->cpath);
854
855 return ! circuit_has_usable_onion_key(circ);
856}
857
858/**
859 * Return true if <b>circ</b> is the type of circuit we want to count
860 * timeouts from.
861 *
862 * In particular, we want to consider any circuit that plans to build
863 * at least 3 hops (but maybe more), but has 3 or fewer hops built
864 * so far.
865 *
866 * We still want to consider circuits before 3 hops, because we need
867 * to decide if we should convert them to a measurement circuit in
868 * circuit_build_times_handle_completed_hop(), rather than letting
869 * slow circuits get killed right away.
870 */
871int
873{
874 return !circ->has_opened
877}
878
879/** Decide whether to use a TAP or ntor handshake for connecting to <b>ei</b>
880 * directly, and set *<b>cell_type_out</b> and *<b>handshake_type_out</b>
881 * accordingly.
882 * Note that TAP handshakes in CREATE cells are only used for direct
883 * connections:
884 * - from Single Onions to rend points not in the service's consensus.
885 * This is checked in onion_populate_cpath. */
886static void
887circuit_pick_create_handshake(uint8_t *cell_type_out,
888 uint16_t *handshake_type_out,
889 const extend_info_t *ei)
890{
891 /* torspec says: In general, clients SHOULD use CREATE whenever they are
892 * using the TAP handshake, and CREATE2 otherwise. */
893 if (extend_info_supports_ntor(ei)) {
894 *cell_type_out = CELL_CREATE2;
895 /* Only use ntor v3 with exits that support congestion control,
896 * and only when it is enabled. */
899 *handshake_type_out = ONION_HANDSHAKE_TYPE_NTOR_V3;
900 else
901 *handshake_type_out = ONION_HANDSHAKE_TYPE_NTOR;
902 } else {
903 /* XXXX030 Remove support for deciding to use TAP and EXTEND. */
904 *cell_type_out = CELL_CREATE;
905 *handshake_type_out = ONION_HANDSHAKE_TYPE_TAP;
906 }
907}
908
909/** Decide whether to use a TAP or ntor handshake for extending to <b>ei</b>
910 * and set *<b>handshake_type_out</b> accordingly. Decide whether we should
911 * use an EXTEND2 or an EXTEND cell to do so, and set *<b>cell_type_out</b>
912 * and *<b>create_cell_type_out</b> accordingly.
913 * Note that TAP handshakes in EXTEND cells are only used:
914 * - from clients to intro points, and
915 * - from hidden services to rend points.
916 * This is checked in onion_populate_cpath.
917 */
918static void
919circuit_pick_extend_handshake(uint8_t *cell_type_out,
920 uint8_t *create_cell_type_out,
921 uint16_t *handshake_type_out,
922 const extend_info_t *ei)
923{
924 uint8_t t;
925 circuit_pick_create_handshake(&t, handshake_type_out, ei);
926
927 /* torspec says: Clients SHOULD use the EXTEND format whenever sending a TAP
928 * handshake... In other cases, clients SHOULD use EXTEND2. */
929 if (*handshake_type_out != ONION_HANDSHAKE_TYPE_TAP) {
930 *cell_type_out = RELAY_COMMAND_EXTEND2;
931 *create_cell_type_out = CELL_CREATE2;
932 } else {
933 /* XXXX030 Remove support for deciding to use TAP and EXTEND. */
934 *cell_type_out = RELAY_COMMAND_EXTEND;
935 *create_cell_type_out = CELL_CREATE;
936 }
937}
938
939/**
940 * Return true iff <b>circ</b> is allowed
941 * to have no guard configured, even if the circuit is multihop
942 * and guards are enabled.
943 */
944static int
946{
947 if (BUG(!circ))
948 return 0;
949
950 if (circ->first_hop_from_controller) {
951 /* The controller picked the first hop: that bypasses the guard system. */
952 return 1;
953 }
954
955 switch (circ->base_.purpose) {
958 /* Testing circuits may omit guards because they're measuring
959 * liveness or performance, and don't want guards to interfere. */
960 return 1;
961 default:
962 /* All other multihop circuits should use guards if guards are
963 * enabled. */
964 return 0;
965 }
966}
967
968/** This is the backbone function for building circuits.
969 *
970 * If circ's first hop is closed, then we need to build a create
971 * cell and send it forward.
972 *
973 * Otherwise, if circ's cpath still has any non-open hops, we need to
974 * build a relay extend cell and send it forward to the next non-open hop.
975 *
976 * If all hops on the cpath are open, we're done building the circuit
977 * and we should do housekeeping for the newly opened circuit.
978 *
979 * Return -reason if we want to tear down circ, else return 0.
980 */
981int
983{
984 tor_assert(circ);
985
986 if (circ->cpath->state == CPATH_STATE_CLOSED) {
987 /* Case one: we're on the first hop. */
989 }
990
991 tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
993
996
998
999 if (hop) {
1000 /* Case two: we're on a hop after the first. */
1001 return circuit_send_intermediate_onion_skin(circ, hop);
1002 }
1003
1004 /* Case three: the circuit is finished. Do housekeeping tasks on it. */
1006 return circuit_build_no_more_hops(circ);
1007}
1008
1009/**
1010 * Called from circuit_send_next_onion_skin() when we find ourselves connected
1011 * to the first hop in <b>circ</b>: Send a CREATE or CREATE2 or CREATE_FAST
1012 * cell to that hop. Return 0 on success; -reason on failure (if the circuit
1013 * should be torn down).
1014 */
1015static int
1017{
1018 int fast;
1019 int len;
1020 const node_t *node;
1021 create_cell_t cc;
1022 memset(&cc, 0, sizeof(cc));
1023
1024 log_debug(LD_CIRC,"First skin; sending create cell.");
1025
1026 if (circ->build_state->onehop_tunnel) {
1027 control_event_bootstrap(BOOTSTRAP_STATUS_ONEHOP_CREATE, 0);
1028 } else {
1029 control_event_bootstrap(BOOTSTRAP_STATUS_CIRCUIT_CREATE, 0);
1030
1031 /* If this is not a one-hop tunnel, the channel is being used
1032 * for traffic that wants anonymity and protection from traffic
1033 * analysis (such as netflow record retention). That means we want
1034 * to pad it.
1035 */
1036 if (circ->base_.n_chan->channel_usage < CHANNEL_USED_FOR_FULL_CIRCS)
1037 circ->base_.n_chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
1038 }
1039
1040 node = node_get_by_id(circ->base_.n_chan->identity_digest);
1042 if (!fast) {
1043 /* We know the right onion key: we should send a create cell. */
1045 circ->cpath->extend_info);
1046 } else {
1047 /* We don't know an onion key, so we need to fall back to CREATE_FAST. */
1048 cc.cell_type = CELL_CREATE_FAST;
1049 cc.handshake_type = ONION_HANDSHAKE_TYPE_FAST;
1050 }
1051
1053 circ->cpath->extend_info,
1054 &circ->cpath->handshake_state,
1055 cc.onionskin,
1056 sizeof(cc.onionskin));
1057 if (len < 0) {
1058 log_warn(LD_CIRC,"onion_skin_create (first hop) failed.");
1059 return - END_CIRC_REASON_INTERNAL;
1060 }
1061 cc.handshake_len = len;
1062
1063 if (circuit_deliver_create_cell(TO_CIRCUIT(circ), &cc, 0) < 0)
1064 return - END_CIRC_REASON_RESOURCELIMIT;
1065 tor_trace(TR_SUBSYS(circuit), TR_EV(first_onion_skin), circ, circ->cpath);
1066
1067 circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
1069 log_info(LD_CIRC,"First hop: finished sending %s cell to '%s'",
1070 fast ? "CREATE_FAST" : "CREATE",
1071 node ? node_describe(node) : "<unnamed>");
1072 return 0;
1073}
1074
1075/**
1076 * Called from circuit_send_next_onion_skin() when we find that we have no
1077 * more hops: mark the circuit as finished, and perform the necessary
1078 * bookkeeping. Return 0 on success; -reason on failure (if the circuit
1079 * should be torn down).
1080 */
1081static int
1083{
1084 guard_usable_t r;
1085 if (! circ->guard_state) {
1086 if (circuit_get_cpath_len(circ) != 1 &&
1087 ! circuit_may_omit_guard(circ) &&
1088 get_options()->UseEntryGuards) {
1089 log_warn(LD_BUG, "%d-hop circuit %p with purpose %d has no "
1090 "guard state",
1091 circuit_get_cpath_len(circ), circ, circ->base_.purpose);
1092 }
1093 r = GUARD_USABLE_NOW;
1094 } else {
1096 }
1097 const int is_usable_for_streams = (r == GUARD_USABLE_NOW);
1098 if (r == GUARD_USABLE_NOW) {
1100 } else if (r == GUARD_MAYBE_USABLE_LATER) {
1101 // Wait till either a better guard succeeds, or till
1102 // all better guards fail.
1104 } else {
1105 tor_assert_nonfatal(r == GUARD_USABLE_NEVER);
1106 return - END_CIRC_REASON_INTERNAL;
1107 }
1108
1109 /* XXXX #21422 -- the rest of this branch needs careful thought!
1110 * Some of the things here need to happen when a circuit becomes
1111 * mechanically open; some need to happen when it is actually usable.
1112 * I think I got them right, but more checking would be wise. -NM
1113 */
1114
1115 log_info(LD_CIRC,"circuit built!");
1117
1118 if (circ->build_state->onehop_tunnel || circ->has_opened) {
1119 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_STATUS, 0);
1120 }
1121
1123 if (is_usable_for_streams)
1124 circuit_has_opened(circ); /* do other actions as necessary */
1125
1127 const or_options_t *options = get_options();
1129 /* FFFF Log a count of known routers here */
1130 log_info(LD_GENERAL,
1131 "Tor has successfully opened a circuit. "
1132 "Looks like client functionality is working.");
1133 control_event_bootstrap(BOOTSTRAP_STATUS_DONE, 0);
1134 control_event_client_status(LOG_NOTICE, "CIRCUIT_ESTABLISHED");
1136 if (server_mode(options) &&
1137 !router_all_orports_seem_reachable(options)) {
1139 }
1140 }
1141
1142 /* We're done with measurement circuits here. Just close them */
1143 if (circ->base_.purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1144 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
1145 }
1146 return 0;
1147}
1148
1149/**
1150 * Called from circuit_send_next_onion_skin() when we find that we have a hop
1151 * other than the first that we need to extend to: use <b>hop</b>'s
1152 * information to extend the circuit another step. Return 0 on success;
1153 * -reason on failure (if the circuit should be torn down).
1154 */
1155static int
1157 crypt_path_t *hop)
1158{
1159 int len;
1160 extend_cell_t ec;
1161 /* Relays and bridges can send IPv6 extends. But for clients, it's an
1162 * obvious version distinguisher. */
1163 const bool include_ipv6 = server_mode(get_options());
1164 memset(&ec, 0, sizeof(ec));
1167
1168 log_debug(LD_CIRC,"starting to send subsequent skin.");
1169
1173 hop->extend_info);
1174
1175 const tor_addr_port_t *orport4 =
1176 extend_info_get_orport(hop->extend_info, AF_INET);
1177 const tor_addr_port_t *orport6 =
1178 extend_info_get_orport(hop->extend_info, AF_INET6);
1179 int n_addrs_set = 0;
1180 if (orport4) {
1181 tor_addr_copy(&ec.orport_ipv4.addr, &orport4->addr);
1182 ec.orport_ipv4.port = orport4->port;
1183 ++n_addrs_set;
1184 }
1185 if (orport6 && include_ipv6) {
1186 tor_addr_copy(&ec.orport_ipv6.addr, &orport6->addr);
1187 ec.orport_ipv6.port = orport6->port;
1188 ++n_addrs_set;
1189 }
1190
1191 if (n_addrs_set == 0) {
1192 log_warn(LD_BUG, "No supported address family found in extend_info.");
1193 return - END_CIRC_REASON_INTERNAL;
1194 }
1195 memcpy(ec.node_id, hop->extend_info->identity_digest, DIGEST_LEN);
1196 /* Set the ED25519 identity too -- it will only get included
1197 * in the extend2 cell if we're configured to use it, though. */
1199
1201 hop->extend_info,
1202 &hop->handshake_state,
1204 sizeof(ec.create_cell.onionskin));
1205 if (len < 0) {
1206 log_warn(LD_CIRC,"onion_skin_create failed.");
1207 return - END_CIRC_REASON_INTERNAL;
1208 }
1209 ec.create_cell.handshake_len = len;
1210
1211 log_info(LD_CIRC,"Sending extend relay cell.");
1212 {
1213 uint8_t command = 0;
1214 uint16_t payload_len=0;
1215 uint8_t payload[RELAY_PAYLOAD_SIZE];
1216 if (extend_cell_format(&command, &payload_len, payload, &ec)<0) {
1217 log_warn(LD_CIRC,"Couldn't format extend cell");
1218 return -END_CIRC_REASON_INTERNAL;
1219 }
1220
1221 /* send it to hop->prev, because that relay will transfer
1222 * it to a create cell and then send to hop */
1223 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
1224 command,
1225 (char*)payload, payload_len,
1226 hop->prev) < 0)
1227 return 0; /* circuit is closed */
1228 }
1229 hop->state = CPATH_STATE_AWAITING_KEYS;
1230 tor_trace(TR_SUBSYS(circuit), TR_EV(intermediate_onion_skin), circ, hop);
1231 return 0;
1232}
1233
1234/** Our clock just jumped by <b>seconds_elapsed</b>. If <b>was_idle</b> is
1235 * true, then the monotonic time matches; otherwise it doesn't. Assume
1236 * something has also gone wrong with our network: notify the user, and
1237 * abandon all not-yet-used circuits. */
1238void
1239circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle)
1240{
1241 int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
1242 if (was_idle) {
1243 tor_log(severity, LD_GENERAL, "Tor has been idle for %"PRId64
1244 " seconds; assuming established circuits no longer work.",
1245 (seconds_elapsed));
1246 } else {
1247 tor_log(severity, LD_GENERAL,
1248 "Your system clock just jumped %"PRId64" seconds %s; "
1249 "assuming established circuits no longer work.",
1250 (
1251 seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed),
1252 seconds_elapsed >=0 ? "forward" : "backward");
1253 }
1254 control_event_general_status(LOG_WARN, "CLOCK_JUMPED TIME=%"PRId64
1255 " IDLE=%d",
1256 (seconds_elapsed), was_idle?1:0);
1257 /* so we log when it works again */
1259 control_event_client_status(severity, "CIRCUIT_NOT_ESTABLISHED REASON=%s",
1260 "CLOCK_JUMPED");
1263 if (seconds_elapsed < 0) {
1264 /* Restart all the timers in case we jumped a long way into the past. */
1266 }
1267}
1268
1269/** A "created" cell <b>reply</b> came back to us on circuit <b>circ</b>.
1270 * (The body of <b>reply</b> varies depending on what sort of handshake
1271 * this is.)
1272 *
1273 * Calculate the appropriate keys and digests, make sure KH is
1274 * correct, and initialize this hop of the cpath.
1275 *
1276 * Return - reason if we want to mark circ for close, else return 0.
1277 */
1278int
1280 const created_cell_t *reply)
1281{
1282 char keys[CPATH_KEY_MATERIAL_LEN];
1283 crypt_path_t *hop;
1284 int rv;
1285
1286 if ((rv = pathbias_count_build_attempt(circ)) < 0) {
1287 log_warn(LD_CIRC, "pathbias_count_build_attempt failed: %d", rv);
1288 return rv;
1289 }
1290
1291 if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS) {
1292 hop = circ->cpath;
1293 } else {
1295 if (!hop) { /* got an extended when we're all done? */
1296 log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
1297 return - END_CIRC_REASON_TORPROTOCOL;
1298 }
1299 }
1300 tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
1301
1302 circuit_params_t params;
1303 {
1304 const char *msg = NULL;
1306 &hop->handshake_state,
1307 reply->reply, reply->handshake_len,
1308 (uint8_t*)keys, sizeof(keys),
1309 (uint8_t*)hop->rend_circ_nonce,
1310 &params,
1311 &msg) < 0) {
1312 if (msg)
1313 log_warn(LD_CIRC,"onion_skin_client_handshake failed: %s", msg);
1314 return -END_CIRC_REASON_TORPROTOCOL;
1315 }
1316 }
1317
1319
1320 if (cpath_init_circuit_crypto(hop, keys, sizeof(keys), 0, 0)<0) {
1321 return -END_CIRC_REASON_TORPROTOCOL;
1322 }
1323
1324 if (params.cc_enabled) {
1325 int circ_len = circuit_get_cpath_len(circ);
1326
1327 if (circ_len == DEFAULT_ROUTE_LEN &&
1329 hop->ccontrol = congestion_control_new(&params, CC_PATH_EXIT);
1330 } else if (circ_len == SBWS_ROUTE_LEN &&
1331 circuit_get_cpath_hop(circ, SBWS_ROUTE_LEN) == hop) {
1332 hop->ccontrol = congestion_control_new(&params, CC_PATH_SBWS);
1333 } else {
1334 if (circ_len > DEFAULT_ROUTE_LEN) {
1335 /* This can happen for unknown reasons; cannibalization codepaths
1336 * don't seem able to do it, so there is some magic way that hops can
1337 * still get added. Perhaps some cases of circuit pre-build that change
1338 * purpose? */
1339 log_info(LD_CIRC,
1340 "Unexpected path length %d for exit circuit %d, purpose %d",
1341 circ_len, circ->global_identifier,
1342 TO_CIRCUIT(circ)->purpose);
1343 hop->ccontrol = congestion_control_new(&params, CC_PATH_EXIT);
1344 } else {
1345 /* This is likely directory requests, which should block on orconn
1346 * before congestion control, but lets give them the lower sbws
1347 * param set anyway just in case. */
1348 log_info(LD_CIRC,
1349 "Unexpected path length %d for exit circuit %d, purpose %d",
1350 circ_len, circ->global_identifier,
1351 TO_CIRCUIT(circ)->purpose);
1352
1353 hop->ccontrol = congestion_control_new(&params, CC_PATH_SBWS);
1354 }
1355 }
1356 }
1357
1358 hop->state = CPATH_STATE_OPEN;
1359 log_info(LD_CIRC,"Finished building circuit hop:");
1361 circuit_event_status(circ, CIRC_EVENT_EXTENDED, 0);
1362
1363 return 0;
1364}
1365
1366/** We received a relay truncated cell on circ.
1367 *
1368 * Since we don't send truncates currently, getting a truncated
1369 * means that a connection broke or an extend failed. For now,
1370 * just give up: force circ to close, and return 0.
1371 */
1372int
1374{
1375// crypt_path_t *victim;
1376// connection_t *stream;
1377
1378 tor_assert(circ);
1379
1380 /* XXX Since we don't send truncates currently, getting a truncated
1381 * means that a connection broke or an extend failed. For now,
1382 * just give up.
1383 */
1384 circuit_mark_for_close(TO_CIRCUIT(circ),
1386 return 0;
1387
1388#if 0
1389 while (layer->next != circ->cpath) {
1390 /* we need to clear out layer->next */
1391 victim = layer->next;
1392 log_debug(LD_CIRC, "Killing a layer of the cpath.");
1393
1394 for (stream = circ->p_streams; stream; stream=stream->next_stream) {
1395 if (stream->cpath_layer == victim) {
1396 log_info(LD_APP, "Marking stream %d for close because of truncate.",
1397 stream->stream_id);
1398 /* no need to send 'end' relay cells,
1399 * because the other side's already dead
1400 */
1401 connection_mark_unattached_ap(stream, END_STREAM_REASON_DESTROY);
1402 }
1403 }
1404
1405 layer->next = victim->next;
1406 cpath_free(victim);
1407 }
1408
1409 log_info(LD_CIRC, "finished");
1410 return 0;
1411#endif /* 0 */
1412}
1413
1414/** Helper for new_route_len(). Choose a circuit length for purpose
1415 * <b>purpose</b>: DEFAULT_ROUTE_LEN (+ 1 if someone else chose the
1416 * exit). If someone else chose the exit, they could be colluding
1417 * with the exit, so add a randomly selected node to preserve
1418 * anonymity.
1419 *
1420 * Here, "exit node" sometimes means an OR acting as an internal
1421 * endpoint, rather than as a relay to an external endpoint. This
1422 * means there need to be at least DEFAULT_ROUTE_LEN routers between
1423 * us and the internal endpoint to preserve the same anonymity
1424 * properties that we would get when connecting to an external
1425 * endpoint. These internal endpoints can include:
1426 *
1427 * - Connections to a directory of hidden services
1428 * (CIRCUIT_PURPOSE_C_GENERAL)
1429 *
1430 * - A client connecting to an introduction point, which the hidden
1431 * service picked (CIRCUIT_PURPOSE_C_INTRODUCING, via
1432 * circuit_get_open_circ_or_launch() which rewrites it from
1433 * CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
1434 *
1435 * - A hidden service connecting to a rendezvous point, which the
1436 * client picked (CIRCUIT_PURPOSE_S_CONNECT_REND.
1437 *
1438 * There are currently two situations where we picked the exit node
1439 * ourselves, making DEFAULT_ROUTE_LEN a safe circuit length:
1440 *
1441 * - We are a hidden service connecting to an introduction point
1442 * (CIRCUIT_PURPOSE_S_ESTABLISH_INTRO).
1443 *
1444 * - We are a router testing its own reachabiity
1445 * (CIRCUIT_PURPOSE_TESTING, via router_do_reachability_checks())
1446 *
1447 * onion_pick_cpath_exit() bypasses us (by not calling
1448 * new_route_len()) in the one-hop tunnel case, so we don't need to
1449 * handle that.
1450 */
1451int
1452route_len_for_purpose(uint8_t purpose, extend_info_t *exit_ei)
1453{
1454 int routelen = DEFAULT_ROUTE_LEN;
1455 int known_purpose = 0;
1456
1457 /* If we're using L3 vanguards, we need longer paths for onion services */
1458 if (circuit_purpose_is_hidden_service(purpose) &&
1459 get_options()->HSLayer3Nodes) {
1460 /* Clients want an extra hop for rends to avoid linkability.
1461 * Services want it for intro points to avoid publishing their
1462 * layer3 guards. They want it for hsdir posts to use
1463 * their full layer3 guard set for those connections.
1464 * Ex: C - G - L2 - L3 - R
1465 * S - G - L2 - L3 - HSDIR
1466 * S - G - L2 - L3 - I
1467 */
1468 if (purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
1469 purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
1470 purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
1472 return routelen+1;
1473
1474 /* For connections to hsdirs, clients want two extra hops
1475 * when using layer3 guards, to avoid linkability.
1476 * Same goes for intro points. Note that the route len
1477 * includes the intro point or hsdir, hence the +2.
1478 * Ex: C - G - L2 - L3 - M - I
1479 * C - G - L2 - L3 - M - HSDIR
1480 * S - G - L2 - L3 - M - R
1481 */
1482 if (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
1483 purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
1485 return routelen+2;
1486 }
1487
1488 if (!exit_ei)
1489 return routelen;
1490
1491 switch (purpose) {
1492 /* These purposes connect to a router that we chose, so DEFAULT_ROUTE_LEN
1493 * is safe: */
1496 /* router reachability testing */
1497 known_purpose = 1;
1498 break;
1499
1500 /* These purposes connect to a router that someone else
1501 * might have chosen, so add an extra hop to protect anonymity. */
1505 /* connecting to hidden service directory */
1507 /* client connecting to introduction point */
1509 /* hidden service connecting to rendezvous point */
1511 /* hidden service connecting to intro point. In this case we want an extra
1512 hop to avoid linkability attacks by the introduction point. */
1513 known_purpose = 1;
1514 routelen++;
1515 break;
1516
1517 default:
1518 /* Got a purpose not listed above along with a chosen exit.
1519 * Increase the circuit length by one anyway for safety. */
1520 routelen++;
1521 break;
1522 }
1523
1524 if (BUG(exit_ei && !known_purpose)) {
1525 log_warn(LD_BUG, "Unhandled purpose %d with a chosen exit; "
1526 "assuming routelen %d.", purpose, routelen);
1527 }
1528 return routelen;
1529}
1530
1531/** Choose a length for a circuit of purpose <b>purpose</b> and check
1532 * if enough routers are available.
1533 *
1534 * If the routerlist <b>nodes</b> doesn't have enough routers
1535 * to handle the desired path length, return -1.
1536 */
1537STATIC int
1538new_route_len(uint8_t purpose, extend_info_t *exit_ei,
1539 const smartlist_t *nodes)
1540{
1541 int routelen;
1542
1543 tor_assert(nodes);
1544
1545 routelen = route_len_for_purpose(purpose, exit_ei);
1546
1547 int num_acceptable_direct = count_acceptable_nodes(nodes, 1);
1548 int num_acceptable_indirect = count_acceptable_nodes(nodes, 0);
1549
1550 log_debug(LD_CIRC,"Chosen route length %d (%d direct and %d indirect "
1551 "routers suitable).", routelen, num_acceptable_direct,
1552 num_acceptable_indirect);
1553
1554 if (num_acceptable_direct < 1 || num_acceptable_indirect < routelen - 1) {
1555 log_info(LD_CIRC,
1556 "Not enough acceptable routers (%d/%d direct and %d/%d "
1557 "indirect routers suitable). Discarding this circuit.",
1558 num_acceptable_direct, routelen,
1559 num_acceptable_indirect, routelen);
1560 return -1;
1561 }
1562
1563 return routelen;
1564}
1565
1566/** Return a newly allocated list of uint16_t * for each predicted port not
1567 * handled by a current circuit. */
1568static smartlist_t *
1570{
1573 return dest;
1574}
1575
1576/** Return 1 if we already have circuits present or on the way for
1577 * all anticipated ports. Return 0 if we should make more.
1578 *
1579 * If we're returning 0, set need_uptime and need_capacity to
1580 * indicate any requirements that the unhandled ports have.
1581 */
1582MOCK_IMPL(int,
1583circuit_all_predicted_ports_handled, (time_t now, int *need_uptime,
1584 int *need_capacity))
1585{
1586 int i, enough;
1587 uint16_t *port;
1589 smartlist_t *LongLivedServices = get_options()->LongLivedPorts;
1590 tor_assert(need_uptime);
1591 tor_assert(need_capacity);
1592 // Always predict need_capacity
1593 *need_capacity = 1;
1594 enough = (smartlist_len(sl) == 0);
1595 for (i = 0; i < smartlist_len(sl); ++i) {
1596 port = smartlist_get(sl, i);
1597 if (smartlist_contains_int_as_string(LongLivedServices, *port))
1598 *need_uptime = 1;
1599 tor_free(port);
1600 }
1601 smartlist_free(sl);
1602 return enough;
1603}
1604
1605/** Return 1 if <b>node</b> can handle one or more of the ports in
1606 * <b>needed_ports</b>, else return 0.
1607 */
1608static int
1609node_handles_some_port(const node_t *node, smartlist_t *needed_ports)
1610{ /* XXXX MOVE */
1611 int i;
1612 uint16_t port;
1613
1614 for (i = 0; i < smartlist_len(needed_ports); ++i) {
1616 /* alignment issues aren't a worry for this dereference, since
1617 needed_ports is explicitly a smartlist of uint16_t's */
1618 port = *(uint16_t *)smartlist_get(needed_ports, i);
1619 tor_assert(port);
1620 if (node)
1621 r = compare_tor_addr_to_node_policy(NULL, port, node);
1622 else
1623 continue;
1625 return 1;
1626 }
1627 return 0;
1628}
1629
1630/** Return true iff <b>conn</b> needs another general circuit to be
1631 * built. */
1632static int
1634{
1635 entry_connection_t *entry;
1636 if (conn->type != CONN_TYPE_AP)
1637 return 0;
1638 entry = TO_ENTRY_CONN(conn);
1639
1640 if (conn->state == AP_CONN_STATE_CIRCUIT_WAIT &&
1641 !conn->marked_for_close &&
1642 !(entry->want_onehop) && /* ignore one-hop streams */
1643 !(entry->use_begindir) && /* ignore targeted dir fetches */
1644 !(entry->chosen_exit_name) && /* ignore defined streams */
1648 return 1;
1649 return 0;
1650}
1651
1652/** Return a pointer to a suitable router to be the exit node for the
1653 * general-purpose circuit we're about to build.
1654 *
1655 * Look through the connection array, and choose a router that maximizes
1656 * the number of pending streams that can exit from this router.
1657 *
1658 * Return NULL if we can't find any suitable routers.
1659 */
1660static const node_t *
1662{
1663 int *n_supported;
1664 int n_pending_connections = 0;
1665 smartlist_t *connections;
1666 int best_support = -1;
1667 int n_best_support=0;
1668 const or_options_t *options = get_options();
1669 const smartlist_t *the_nodes;
1670 const node_t *selected_node=NULL;
1671 const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
1672 const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
1673
1674 /* We should not require guard flags on exits. */
1675 IF_BUG_ONCE(flags & CRN_NEED_GUARD)
1676 return NULL;
1677
1678 /* We reject single-hop exits for all node positions. */
1679 IF_BUG_ONCE(flags & CRN_DIRECT_CONN)
1680 return NULL;
1681
1682 /* This isn't the function for picking rendezvous nodes. */
1683 IF_BUG_ONCE(flags & CRN_RENDEZVOUS_V3)
1684 return NULL;
1685
1686 /* We only want exits to extend if we cannibalize the circuit.
1687 * But we don't require IPv6 extends yet. */
1688 IF_BUG_ONCE(flags & CRN_INITIATE_IPV6_EXTEND)
1689 return NULL;
1690
1691 connections = get_connection_array();
1692
1693 /* Count how many connections are waiting for a circuit to be built.
1694 * We use this for log messages now, but in the future we may depend on it.
1695 */
1696 SMARTLIST_FOREACH(connections, connection_t *, conn,
1697 {
1699 ++n_pending_connections;
1700 });
1701// log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
1702// n_pending_connections);
1703 /* Now we count, for each of the routers in the directory, how many
1704 * of the pending connections could possibly exit from that
1705 * router (n_supported[i]). (We can't be sure about cases where we
1706 * don't know the IP address of the pending connection.)
1707 *
1708 * -1 means "Don't use this router at all."
1709 */
1710 the_nodes = nodelist_get_list();
1711 n_supported = tor_calloc(smartlist_len(the_nodes), sizeof(int));
1712 SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
1713 const int i = node_sl_idx;
1714 if (router_digest_is_me(node->identity)) {
1715 n_supported[i] = -1;
1716// log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
1717 /* XXX there's probably a reverse predecessor attack here, but
1718 * it's slow. should we take this out? -RD
1719 */
1720 continue;
1721 }
1722 if (!router_can_choose_node(node, flags)) {
1723 n_supported[i] = -1;
1724 continue;
1725 }
1726 if (node->is_bad_exit) {
1727 n_supported[i] = -1;
1728 continue; /* skip routers that are known to be down or bad exits */
1729 }
1730 if (routerset_contains_node(options->ExcludeExitNodesUnion_, node)) {
1731 n_supported[i] = -1;
1732 continue; /* user asked us not to use it, no matter what */
1733 }
1734 if (options->ExitNodes &&
1735 !routerset_contains_node(options->ExitNodes, node)) {
1736 n_supported[i] = -1;
1737 continue; /* not one of our chosen exit nodes */
1738 }
1739 if (node_exit_policy_rejects_all(node)) {
1740 n_supported[i] = -1;
1741// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
1742// router->nickname, i);
1743 continue; /* skip routers that reject all */
1744 }
1745 n_supported[i] = 0;
1746 /* iterate over connections */
1747 SMARTLIST_FOREACH_BEGIN(connections, connection_t *, conn) {
1749 continue; /* Skip everything but APs in CIRCUIT_WAIT */
1750 if (connection_ap_can_use_exit(TO_ENTRY_CONN(conn), node)) {
1751 ++n_supported[i];
1752// log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
1753// router->nickname, i, n_supported[i]);
1754 } else {
1755// log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
1756// router->nickname, i);
1757 }
1758 } SMARTLIST_FOREACH_END(conn);
1759 if (n_pending_connections > 0 && n_supported[i] == 0) {
1760 /* Leave best_support at -1 if that's where it is, so we can
1761 * distinguish it later. */
1762 continue;
1763 }
1764 if (n_supported[i] > best_support) {
1765 /* If this router is better than previous ones, remember its index
1766 * and goodness, and start counting how many routers are this good. */
1767 best_support = n_supported[i]; n_best_support=1;
1768// log_fn(LOG_DEBUG,"%s is new best supported option so far.",
1769// router->nickname);
1770 } else if (n_supported[i] == best_support) {
1771 /* If this router is _as good_ as the best one, just increment the
1772 * count of equally good routers.*/
1773 ++n_best_support;
1774 }
1775 } SMARTLIST_FOREACH_END(node);
1776 log_info(LD_CIRC,
1777 "Found %d servers that might support %d/%d pending connections.",
1778 n_best_support, best_support >= 0 ? best_support : 0,
1779 n_pending_connections);
1780
1781 /* If any routers definitely support any pending connections, choose one
1782 * at random. */
1783 if (best_support > 0) {
1784 smartlist_t *supporting = smartlist_new();
1785
1786 SMARTLIST_FOREACH(the_nodes, const node_t *, node, {
1787 if (n_supported[node_sl_idx] == best_support)
1788 smartlist_add(supporting, (void*)node);
1789 });
1790
1791 selected_node = node_sl_choose_by_bandwidth(supporting, WEIGHT_FOR_EXIT);
1792 smartlist_free(supporting);
1793 } else {
1794 /* Either there are no pending connections, or no routers even seem to
1795 * possibly support any of them. Choose a router at random that satisfies
1796 * at least one predicted exit port. */
1797
1798 int attempt;
1799 smartlist_t *needed_ports, *supporting;
1800
1801 if (best_support == -1) {
1802 if (need_uptime || need_capacity) {
1803 log_info(LD_CIRC,
1804 "We couldn't find any live%s%s routers; falling back "
1805 "to list of all routers.",
1806 need_capacity?", fast":"",
1807 need_uptime?", stable":"");
1808 tor_free(n_supported);
1809 flags &= ~(CRN_NEED_UPTIME|CRN_NEED_CAPACITY);
1810 return choose_good_exit_server_general(flags);
1811 }
1812 log_notice(LD_CIRC, "All routers are down or won't exit%s -- "
1813 "choosing a doomed exit at random.",
1814 options->ExcludeExitNodesUnion_ ? " or are Excluded" : "");
1815 }
1816 supporting = smartlist_new();
1817 needed_ports = circuit_get_unhandled_ports(time(NULL));
1818 for (attempt = 0; attempt < 2; attempt++) {
1819 /* try once to pick only from routers that satisfy a needed port,
1820 * then if there are none, pick from any that support exiting. */
1821 SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
1822 if (n_supported[node_sl_idx] != -1 &&
1823 (attempt || node_handles_some_port(node, needed_ports))) {
1824// log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.",
1825// try, router->nickname);
1826 smartlist_add(supporting, (void*)node);
1827 }
1828 } SMARTLIST_FOREACH_END(node);
1829
1830 selected_node = node_sl_choose_by_bandwidth(supporting, WEIGHT_FOR_EXIT);
1831 if (selected_node)
1832 break;
1833 smartlist_clear(supporting);
1834 /* If we reach this point, we can't actually support any unhandled
1835 * predicted ports, so clear all the remaining ones. */
1836 if (smartlist_len(needed_ports))
1837 rep_hist_remove_predicted_ports(needed_ports);
1838 }
1839 SMARTLIST_FOREACH(needed_ports, uint16_t *, cp, tor_free(cp));
1840 smartlist_free(needed_ports);
1841 smartlist_free(supporting);
1842 }
1843
1844 tor_free(n_supported);
1845 if (selected_node) {
1846 log_info(LD_CIRC, "Chose exit server '%s'", node_describe(selected_node));
1847 return selected_node;
1848 }
1849 if (options->ExitNodes) {
1850 log_warn(LD_CIRC,
1851 "No exits in ExitNodes%s seem to be running: "
1852 "can't choose an exit.",
1853 options->ExcludeExitNodesUnion_ ?
1854 ", except possibly those excluded by your configuration, " : "");
1855 }
1856 return NULL;
1857}
1858
1859/* Pick a Rendezvous Point for our HS circuits according to <b>flags</b>. */
1860static const node_t *
1861pick_rendezvous_node(router_crn_flags_t flags)
1862{
1863 const or_options_t *options = get_options();
1864 return router_choose_random_node(NULL, options->ExcludeNodes, flags);
1865}
1866
1867/*
1868 * Helper function to pick a configured restricted middle node
1869 * (either HSLayer2Nodes or HSLayer3Nodes).
1870 *
1871 * Make sure that the node we chose is alive, and not excluded,
1872 * and return it.
1873 *
1874 * The exclude_set is a routerset of nodes that the selected node
1875 * must not match, and the exclude_list is a simple list of nodes
1876 * that the selected node must not be in. Either or both may be
1877 * NULL.
1878 *
1879 * Return NULL if no usable nodes could be found. */
1880static const node_t *
1882 const routerset_t *pick_from,
1883 const routerset_t *exclude_set,
1884 const smartlist_t *exclude_list,
1885 int position_hint)
1886{
1887 const node_t *middle_node = NULL;
1888
1889 smartlist_t *allowlisted_live_middles = smartlist_new();
1890 smartlist_t *all_live_nodes = smartlist_new();
1891
1892 tor_assert(pick_from);
1893
1894 /* Add all running nodes to all_live_nodes */
1895 router_add_running_nodes_to_smartlist(all_live_nodes, flags);
1896
1897 /* Filter all_live_nodes to only add live *and* allowlisted middles
1898 * to the list allowlisted_live_middles. */
1899 SMARTLIST_FOREACH_BEGIN(all_live_nodes, node_t *, live_node) {
1900 if (routerset_contains_node(pick_from, live_node)) {
1901 smartlist_add(allowlisted_live_middles, live_node);
1902 }
1903 } SMARTLIST_FOREACH_END(live_node);
1904
1905 /* Honor ExcludeNodes */
1906 if (exclude_set) {
1907 routerset_subtract_nodes(allowlisted_live_middles, exclude_set);
1908 }
1909
1910 if (exclude_list) {
1911 smartlist_subtract(allowlisted_live_middles, exclude_list);
1912 }
1913
1914 /**
1915 * Max number of restricted nodes before we alert the user and try
1916 * to load balance for them.
1917 *
1918 * The most aggressive vanguard design had 16 nodes at layer3.
1919 * Let's give a small ceiling above that. */
1920#define MAX_SANE_RESTRICTED_NODES 20
1921 /* If the user (or associated tor controller) selected only a few nodes,
1922 * assume they took load balancing into account and don't do it for them.
1923 *
1924 * If there are a lot of nodes in here, assume they did not load balance
1925 * and do it for them, but also warn them that they may be Doing It Wrong.
1926 */
1927 if (smartlist_len(allowlisted_live_middles) <=
1928 MAX_SANE_RESTRICTED_NODES) {
1929 middle_node = smartlist_choose(allowlisted_live_middles);
1930 } else {
1931 static ratelim_t pinned_notice_limit = RATELIM_INIT(24*3600);
1932 log_fn_ratelim(&pinned_notice_limit, LOG_NOTICE, LD_CIRC,
1933 "Your _HSLayer%dNodes setting has resulted "
1934 "in %d total nodes. This is a lot of nodes. "
1935 "You may want to consider using a Tor controller "
1936 "to select and update a smaller set of nodes instead.",
1937 position_hint, smartlist_len(allowlisted_live_middles));
1938
1939 /* NO_WEIGHTING here just means don't take node flags into account
1940 * (ie: use consensus measurement only). This is done so that
1941 * we don't further surprise the user by not using Exits that they
1942 * specified at all */
1943 middle_node = node_sl_choose_by_bandwidth(allowlisted_live_middles,
1944 NO_WEIGHTING);
1945 }
1946
1947 smartlist_free(allowlisted_live_middles);
1948 smartlist_free(all_live_nodes);
1949
1950 return middle_node;
1951}
1952
1953/** Return a pointer to a suitable router to be the exit node for the
1954 * circuit of purpose <b>purpose</b> that we're about to build (or NULL
1955 * if no router is suitable).
1956 *
1957 * For general-purpose circuits, pass it off to
1958 * choose_good_exit_server_general()
1959 *
1960 * For client-side rendezvous circuits, choose a random node, weighted
1961 * toward the preferences in 'options'.
1962 */
1963static const node_t *
1965 router_crn_flags_t flags, int is_internal)
1966{
1967 const or_options_t *options = get_options();
1968 flags |= CRN_NEED_DESC;
1969
1970 switch (TO_CIRCUIT(circ)->purpose) {
1974 /* For these three, we want to pick the exit like a middle hop,
1975 * since it should be random. */
1976 tor_assert_nonfatal(is_internal);
1977 FALLTHROUGH;
1980 if (is_internal) /* pick it like a middle hop */
1981 return router_choose_random_node(NULL, options->ExcludeNodes, flags);
1982 else
1983 return choose_good_exit_server_general(flags);
1985 {
1986 /* Pick a new RP */
1987 const node_t *rendezvous_node = pick_rendezvous_node(flags);
1988 log_info(LD_REND, "Picked new RP: %s",
1989 safe_str_client(node_describe(rendezvous_node)));
1990 return rendezvous_node;
1991 }
1992 }
1993 log_warn(LD_BUG,"Unhandled purpose %d", TO_CIRCUIT(circ)->purpose);
1995 return NULL;
1996}
1997
1998/** Log a warning if the user specified an exit for the circuit that
1999 * has been excluded from use by ExcludeNodes or ExcludeExitNodes. */
2000static void
2002 const extend_info_t *exit_ei)
2003{
2004 const or_options_t *options = get_options();
2005 routerset_t *rs = options->ExcludeNodes;
2006 const char *description;
2007 uint8_t purpose = circ->base_.purpose;
2008
2009 if (circ->build_state->onehop_tunnel)
2010 return;
2011
2012 switch (purpose)
2013 {
2014 default:
2015 case CIRCUIT_PURPOSE_OR:
2019 log_warn(LD_BUG, "Called on non-origin circuit (purpose %d, %s)",
2020 (int)purpose,
2021 circuit_purpose_to_string(purpose));
2022 return;
2027 case CIRCUIT_PURPOSE_CONFLUX_LINKED:
2028 if (circ->build_state->is_internal)
2029 return;
2030 description = "requested exit node";
2031 rs = options->ExcludeExitNodesUnion_;
2032 break;
2040 return;
2045 description = "chosen rendezvous point";
2046 break;
2048 rs = options->ExcludeExitNodesUnion_;
2049 description = "controller-selected circuit target";
2050 break;
2051 }
2052
2053 if (routerset_contains_extendinfo(rs, exit_ei)) {
2054 /* We should never get here if StrictNodes is set to 1. */
2055 if (options->StrictNodes) {
2056 log_warn(LD_BUG, "Using %s '%s' which is listed in ExcludeNodes%s, "
2057 "even though StrictNodes is set. Please report. "
2058 "(Circuit purpose: %s)",
2059 description, extend_info_describe(exit_ei),
2060 rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
2061 circuit_purpose_to_string(purpose));
2062 } else {
2063 log_warn(LD_CIRC, "Using %s '%s' which is listed in "
2064 "ExcludeNodes%s, because no better options were available. To "
2065 "prevent this (and possibly break your Tor functionality), "
2066 "set the StrictNodes configuration option. "
2067 "(Circuit purpose: %s)",
2068 description, extend_info_describe(exit_ei),
2069 rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
2070 circuit_purpose_to_string(purpose));
2071 }
2073 }
2074
2075 return;
2076}
2077
2078/* Return a set of generic CRN_* flags based on <b>state</b>.
2079 *
2080 * Called for every position in the circuit. */
2081STATIC int
2082cpath_build_state_to_crn_flags(const cpath_build_state_t *state)
2083{
2084 router_crn_flags_t flags = 0;
2085 /* These flags apply to entry, middle, and exit nodes.
2086 * If a flag only applies to a specific position, it should be checked in
2087 * that function. */
2088 if (state->need_uptime)
2089 flags |= CRN_NEED_UPTIME;
2090 if (state->need_capacity)
2091 flags |= CRN_NEED_CAPACITY;
2092 return flags;
2093}
2094
2095/* Return the CRN_INITIATE_IPV6_EXTEND flag, based on <b>state</b> and
2096 * <b>cur_len</b>.
2097 *
2098 * Only called for middle nodes (for now). Must not be called on single-hop
2099 * circuits. */
2100STATIC int
2101cpath_build_state_to_crn_ipv6_extend_flag(const cpath_build_state_t *state,
2102 int cur_len)
2103{
2104 IF_BUG_ONCE(state->desired_path_len < 2)
2105 return 0;
2106
2107 /* The last node is the relay doing the self-test. So we want to extend over
2108 * IPv6 from the second-last node. */
2109 if (state->is_ipv6_selftest && cur_len == state->desired_path_len - 2)
2110 return CRN_INITIATE_IPV6_EXTEND;
2111 else
2112 return 0;
2113}
2114
2115/** Decide a suitable length for circ's cpath, and pick an exit
2116 * router (or use <b>exit_ei</b> if provided). Store these in the
2117 * cpath.
2118 *
2119 * If <b>is_hs_v3_rp_circuit</b> is set, then this exit should be suitable to
2120 * be used as an HS v3 rendezvous point.
2121 *
2122 * Return 0 if ok, -1 if circuit should be closed. */
2123STATIC int
2125 int is_hs_v3_rp_circuit)
2126{
2127 cpath_build_state_t *state = circ->build_state;
2128
2129 if (state->onehop_tunnel) {
2130 log_debug(LD_CIRC, "Launching a one-hop circuit for dir tunnel%s.",
2131 (hs_service_allow_non_anonymous_connection(get_options()) ?
2132 ", or intro or rendezvous connection" : ""));
2133 state->desired_path_len = 1;
2134 } else {
2135 int r = new_route_len(circ->base_.purpose, exit_ei, nodelist_get_list());
2136 if (r < 1) /* must be at least 1 */
2137 return -1;
2138 state->desired_path_len = r;
2139 }
2140
2141 if (exit_ei) { /* the circuit-builder pre-requested one */
2142 warn_if_last_router_excluded(circ, exit_ei);
2143 log_info(LD_CIRC,"Using requested exit node '%s'",
2144 extend_info_describe(exit_ei));
2145 exit_ei = extend_info_dup(exit_ei);
2146 } else { /* we have to decide one */
2147 router_crn_flags_t flags = CRN_NEED_DESC;
2148 flags |= cpath_build_state_to_crn_flags(state);
2149 /* Some internal exits are one hop, for example directory connections.
2150 * (Guards are always direct, middles are never direct.) */
2151 if (state->onehop_tunnel)
2152 flags |= CRN_DIRECT_CONN;
2153 if (is_hs_v3_rp_circuit)
2154 flags |= CRN_RENDEZVOUS_V3;
2155 if (state->need_conflux)
2156 flags |= CRN_CONFLUX;
2157 const node_t *node =
2158 choose_good_exit_server(circ, flags, state->is_internal);
2159 if (!node) {
2160 log_warn(LD_CIRC,"Failed to choose an exit server");
2161 return -1;
2162 }
2163 exit_ei = extend_info_from_node(node, state->onehop_tunnel,
2164 /* for_exit_use */
2165 !state->is_internal && (
2166 TO_CIRCUIT(circ)->purpose ==
2168 TO_CIRCUIT(circ)->purpose ==
2170 if (BUG(exit_ei == NULL))
2171 return -1;
2172 }
2173 state->chosen_exit = exit_ei;
2174 return 0;
2175}
2176
2177/** Give <b>circ</b> a new exit destination to <b>exit_ei</b>, and add a
2178 * hop to the cpath reflecting this. Don't send the next extend cell --
2179 * the caller will do this if it wants to.
2180 */
2181int
2183{
2184 cpath_build_state_t *state;
2185 tor_assert(exit_ei);
2186 tor_assert(circ);
2187
2188 state = circ->build_state;
2189 tor_assert(state);
2190 extend_info_free(state->chosen_exit);
2191 state->chosen_exit = extend_info_dup(exit_ei);
2192
2194 cpath_append_hop(&circ->cpath, exit_ei);
2195 return 0;
2196}
2197
2198/** Take an open <b>circ</b>, and add a new hop at the end, based on
2199 * <b>info</b>. Set its state back to CIRCUIT_STATE_BUILDING, and then
2200 * send the next extend cell to begin connecting to that hop.
2201 */
2202int
2204{
2205 int err_reason = 0;
2206 warn_if_last_router_excluded(circ, exit_ei);
2207
2208 tor_gettimeofday(&circ->base_.timestamp_began);
2209
2210 circuit_append_new_exit(circ, exit_ei);
2212 if ((err_reason = circuit_send_next_onion_skin(circ))<0) {
2213 log_warn(LD_CIRC, "Couldn't extend circuit to new point %s.",
2214 extend_info_describe(exit_ei));
2215 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
2216 return -1;
2217 }
2218
2219 return 0;
2220}
2221
2222/** Return the number of routers in <b>nodes</b> that are currently up and
2223 * available for building circuits through.
2224 *
2225 * If <b>direct</b> is true, only count nodes that are suitable for direct
2226 * connections. Counts nodes regardless of whether their addresses are
2227 * preferred.
2228 */
2229MOCK_IMPL(STATIC int,
2230count_acceptable_nodes, (const smartlist_t *nodes, int direct))
2231{
2232 int num=0;
2233 int flags = CRN_NEED_DESC;
2234
2235 if (direct)
2236 flags |= CRN_DIRECT_CONN;
2237
2238 SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
2239 // log_debug(LD_CIRC,
2240 // "Contemplating whether router %d (%s) is a new option.",
2241 // i, r->nickname);
2242 if (!router_can_choose_node(node, flags))
2243 continue;
2244 ++num;
2245 } SMARTLIST_FOREACH_END(node);
2246
2247// log_debug(LD_CIRC,"I like %d. num_acceptable_routers now %d.",i, num);
2248
2249 return num;
2250}
2251
2252/**
2253 * Build the exclude list for vanguard circuits.
2254 *
2255 * For vanguard circuits we exclude all the already chosen nodes (including the
2256 * exit) from being middle hops to prevent the creation of A - B - A subpaths.
2257 * We also allow the 4th hop to be the same as the guard node so as to not leak
2258 * guard information to RP/IP/HSDirs.
2259 *
2260 * For vanguard circuits, we don't apply any subnet or family restrictions.
2261 * This is to avoid impossible-to-build circuit paths, or just situations where
2262 * our earlier guards prevent us from using most of our later ones.
2263 *
2264 * The alternative is building the circuit in reverse. Reverse calls to
2265 * onion_extend_cpath() (ie: select outer hops first) would then have the
2266 * property that you don't gain information about inner hops by observing
2267 * outer ones. See https://bugs.torproject.org/tpo/core/tor/24487
2268 * for this.
2269 *
2270 * (Note further that we still exclude the exit to prevent A - B - A
2271 * at the end of the path. */
2272static smartlist_t *
2274 cpath_build_state_t *state,
2275 crypt_path_t *head,
2276 int cur_len)
2277{
2278 smartlist_t *excluded;
2279 const node_t *r;
2280 crypt_path_t *cpath;
2281 int i;
2282
2283 (void) purpose;
2284
2285 excluded = smartlist_new();
2286
2287 /* Add the exit to the exclude list (note that the exit/last hop is always
2288 * chosen first in circuit_establish_circuit()). */
2289 if ((r = build_state_get_exit_node(state))) {
2290 smartlist_add(excluded, (node_t*)r);
2291 }
2292
2293 /* If we are picking the 4th hop, allow that node to be the guard too.
2294 * This prevents us from avoiding the Guard for those hops, which
2295 * gives the adversary information about our guard if they control
2296 * the RP, IP, or HSDIR. We don't do this check based on purpose
2297 * because we also want to allow HS_VANGUARDS pre-build circuits
2298 * to use the guard for that last hop.
2299 */
2300 if (cur_len == DEFAULT_ROUTE_LEN+1) {
2301 /* Skip the first hop for the exclude list below */
2302 head = head->next;
2303 cur_len--;
2304 }
2305
2306 for (i = 0, cpath = head; cpath && i < cur_len; ++i, cpath=cpath->next) {
2307 if ((r = node_get_by_id(cpath->extend_info->identity_digest))) {
2308 smartlist_add(excluded, (node_t*)r);
2309 }
2310 }
2311
2312 return excluded;
2313}
2314
2315/**
2316 * Build a list of nodes to exclude from the choice of this middle
2317 * hop, based on already chosen nodes.
2318 */
2319static smartlist_t *
2321 uint8_t purpose,
2322 cpath_build_state_t *state,
2323 crypt_path_t *head,
2324 int cur_len)
2325{
2326 smartlist_t *excluded;
2327 const node_t *r;
2328 crypt_path_t *cpath;
2329 int i;
2330
2331 /** Vanguard circuits have their own path selection rules */
2332 if (circuit_should_use_vanguards(purpose)) {
2333 return build_vanguard_middle_exclude_list(purpose, state, head, cur_len);
2334 }
2335
2336 excluded = smartlist_new();
2337
2338 // Exclude other middles on pending and built conflux circs
2340
2341 /* For non-vanguard circuits, add the exit and its family to the exclude list
2342 * (note that the exit/last hop is always chosen first in
2343 * circuit_establish_circuit()). */
2344 if ((r = build_state_get_exit_node(state))) {
2345 nodelist_add_node_and_family(excluded, r);
2346 }
2347
2348 /* also exclude all other already chosen nodes and their family */
2349 for (i = 0, cpath = head; cpath && i < cur_len; ++i, cpath=cpath->next) {
2350 if ((r = node_get_by_id(cpath->extend_info->identity_digest))) {
2351 nodelist_add_node_and_family(excluded, r);
2352 }
2353 }
2354
2355 return excluded;
2356}
2357
2358/** Return true if we MUST use vanguards for picking this middle node. */
2359static int
2361 uint8_t purpose, int cur_len)
2362{
2363 /* If this is not a hidden service circuit, don't use vanguards */
2364 if (!circuit_purpose_is_hidden_service(purpose)) {
2365 return 0;
2366 }
2367
2368 /* Don't even bother if the feature is disabled */
2370 return 0;
2371 }
2372
2373 /* If we are a hidden service circuit, always use either vanguards-lite
2374 * or HSLayer2Nodes for 2nd hop. */
2375 if (cur_len == 1) {
2376 return 1;
2377 }
2378
2379 /* If we have sticky L3 nodes, and this is an L3 pick, use vanguards */
2380 if (options->HSLayer3Nodes && cur_len == 2) {
2381 return 1;
2382 }
2383
2384 return 0;
2385}
2386
2387/** Pick a sticky vanguard middle node or return NULL if not found.
2388 * See doc of pick_restricted_middle_node() for argument details. */
2389static const node_t *
2391 router_crn_flags_t flags, int cur_len,
2392 const smartlist_t *excluded)
2393{
2394 const routerset_t *vanguard_routerset = NULL;
2395 const node_t *node = NULL;
2396
2397 /* Pick the right routerset based on the current hop */
2398 if (cur_len == 1) {
2399 vanguard_routerset = options->HSLayer2Nodes ?
2400 options->HSLayer2Nodes : get_layer2_guards();
2401 } else if (cur_len == 2) {
2402 vanguard_routerset = options->HSLayer3Nodes;
2403 } else {
2404 /* guaranteed by middle_node_should_be_vanguard() */
2406 return NULL;
2407 }
2408
2409 if (BUG(!vanguard_routerset)) {
2410 return NULL;
2411 }
2412
2413 node = pick_restricted_middle_node(flags, vanguard_routerset,
2414 options->ExcludeNodes, excluded,
2415 cur_len+1);
2416
2417 if (!node) {
2418 static ratelim_t pinned_warning_limit = RATELIM_INIT(300);
2419 log_fn_ratelim(&pinned_warning_limit, LOG_WARN, LD_CIRC,
2420 "Could not find a node that matches the configured "
2421 "_HSLayer%dNodes set", cur_len+1);
2422 }
2423
2424 return node;
2425}
2426
2427/** A helper function used by onion_extend_cpath(). Use <b>purpose</b>
2428 * and <b>state</b> and the cpath <b>head</b> (currently populated only
2429 * to length <b>cur_len</b> to decide a suitable middle hop for a
2430 * circuit. In particular, make sure we don't pick the exit node or its
2431 * family, and make sure we don't duplicate any previous nodes or their
2432 * families. */
2433static const node_t *
2435 uint8_t purpose,
2436 cpath_build_state_t *state,
2437 crypt_path_t *head,
2438 int cur_len)
2439{
2440 const node_t *choice;
2441 smartlist_t *excluded;
2442 const or_options_t *options = get_options();
2443 router_crn_flags_t flags = CRN_NEED_DESC;
2444 tor_assert(CIRCUIT_PURPOSE_MIN_ <= purpose &&
2445 purpose <= CIRCUIT_PURPOSE_MAX_);
2446
2447 log_debug(LD_CIRC, "Contemplating intermediate hop #%d: random choice.",
2448 cur_len+1);
2449
2450 excluded = build_middle_exclude_list(circ, purpose, state, head, cur_len);
2451
2452 flags |= cpath_build_state_to_crn_flags(state);
2453 flags |= cpath_build_state_to_crn_ipv6_extend_flag(state, cur_len);
2454
2455 /** If a hidden service circuit wants a specific middle node, pin it. */
2456 if (middle_node_must_be_vanguard(options, purpose, cur_len)) {
2457 log_debug(LD_GENERAL, "Picking a sticky node (cur_len = %d)", cur_len);
2458 choice = pick_vanguard_middle_node(options, flags, cur_len, excluded);
2459 smartlist_free(excluded);
2460 return choice;
2461 }
2462
2463 if (options->MiddleNodes) {
2464 smartlist_t *sl = smartlist_new();
2466 options->ExcludeNodes, 1);
2467
2468 smartlist_subtract(sl, excluded);
2469
2470 choice = node_sl_choose_by_bandwidth(sl, WEIGHT_FOR_MID);
2471 smartlist_free(sl);
2472 if (choice) {
2473 log_fn(LOG_INFO, LD_CIRC, "Chose fixed middle node: %s",
2474 hex_str(choice->identity, DIGEST_LEN));
2475 } else {
2476 log_fn(LOG_NOTICE, LD_CIRC, "Restricted middle not available");
2477 }
2478 } else {
2479 choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2480 }
2481 smartlist_free(excluded);
2482 return choice;
2483}
2484
2485/** Pick a good entry server for the circuit to be built according to
2486 * <b>state</b>. Don't reuse a chosen exit (if any), don't use this
2487 * router (if we're an OR), and respect firewall settings; if we're
2488 * configured to use entry guards, return one.
2489 *
2490 * Set *<b>guard_state_out</b> to information about the guard that
2491 * we're selecting, which we'll use later to remember whether the
2492 * guard worked or not.
2493 */
2494const node_t *
2496 uint8_t purpose, cpath_build_state_t *state,
2497 circuit_guard_state_t **guard_state_out)
2498{
2499 const node_t *choice;
2500 smartlist_t *excluded;
2501 const or_options_t *options = get_options();
2502 /* If possible, choose an entry server with a preferred address,
2503 * otherwise, choose one with an allowed address */
2504 router_crn_flags_t flags = (CRN_NEED_GUARD|CRN_NEED_DESC|CRN_PREF_ADDR|
2505 CRN_DIRECT_CONN);
2506 const node_t *node;
2507
2508 /* Once we used this function to select a node to be a guard. We had
2509 * 'state == NULL' be the signal for that. But we don't do that any more.
2510 */
2511 tor_assert_nonfatal(state);
2512
2513 if (state && options->UseEntryGuards &&
2514 (purpose != CIRCUIT_PURPOSE_TESTING || options->BridgeRelay)) {
2515 /* This request is for an entry server to use for a regular circuit,
2516 * and we use entry guard nodes. Just return one of the guard nodes. */
2517 tor_assert(guard_state_out);
2518 return guards_choose_guard(circ, state, purpose, guard_state_out);
2519 }
2520
2521 excluded = smartlist_new();
2522
2523 if (state && (node = build_state_get_exit_node(state))) {
2524 /* Exclude the exit node from the state, if we have one. Also exclude its
2525 * family. */
2526 nodelist_add_node_and_family(excluded, node);
2527 }
2528
2529 if (state) {
2530 flags |= cpath_build_state_to_crn_flags(state);
2531 }
2532
2533 choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2534 smartlist_free(excluded);
2535 return choice;
2536}
2537
2538/** Choose a suitable next hop for the circuit <b>circ</b>.
2539 * Append the hop info to circ->cpath.
2540 *
2541 * Return 1 if the path is complete, 0 if we successfully added a hop,
2542 * and -1 on error.
2543 */
2544STATIC int
2546{
2547 uint8_t purpose = circ->base_.purpose;
2548 cpath_build_state_t *state = circ->build_state;
2549 int cur_len = circuit_get_cpath_len(circ);
2550 extend_info_t *info = NULL;
2551
2552 if (cur_len >= state->desired_path_len) {
2553 log_debug(LD_CIRC, "Path is complete: %d steps long",
2554 state->desired_path_len);
2555 return 1;
2556 }
2557
2558 log_debug(LD_CIRC, "Path is %d long; we want %d", cur_len,
2559 state->desired_path_len);
2560
2561 if (cur_len == state->desired_path_len - 1) { /* Picking last node */
2562 info = extend_info_dup(state->chosen_exit);
2563 } else if (cur_len == 0) { /* picking first node */
2564 const node_t *r = choose_good_entry_server(circ, purpose, state,
2565 &circ->guard_state);
2566 if (r) {
2567 /* If we're a client, use the preferred address rather than the
2568 primary address, for potentially connecting to an IPv6 OR
2569 port. Servers always want the primary (IPv4) address. */
2570 int client = (server_mode(get_options()) == 0);
2571 info = extend_info_from_node(r, client, false);
2572 /* Clients can fail to find an allowed address */
2573 tor_assert_nonfatal(info || client);
2574 }
2575 } else {
2576 const node_t *r =
2577 choose_good_middle_server(circ, purpose, state, circ->cpath, cur_len);
2578 if (r) {
2579 info = extend_info_from_node(r, 0, false);
2580 }
2581 }
2582
2583 if (!info) {
2584 /* This can happen on first startup, possibly due to insufficient relays
2585 * downloaded to pick vanguards-lite layer2 nodes, or other ephemeral
2586 * reasons. It only happens briefly, is hard to reproduce, and then goes
2587 * away for ever. :/ */
2589 log_info(LD_CIRC,
2590 "Failed to find node for hop #%d of our path. Discarding "
2591 "this circuit.", cur_len+1);
2592 } else {
2593 log_notice(LD_CIRC,
2594 "Failed to find node for hop #%d of our path. Discarding "
2595 "this circuit.", cur_len+1);
2596 }
2597 return -1;
2598 }
2599
2600 log_debug(LD_CIRC,"Chose router %s for hop #%d (exit is %s)",
2602 cur_len+1, build_state_get_exit_nickname(state));
2603
2604 cpath_append_hop(&circ->cpath, info);
2605 extend_info_free(info);
2606 return 0;
2607}
2608
2609/** Return the node_t for the chosen exit router in <b>state</b>.
2610 * If there is no chosen exit, or if we don't know the node_t for
2611 * the chosen exit, return NULL.
2612 */
2613MOCK_IMPL(const node_t *,
2615{
2616 if (!state || !state->chosen_exit)
2617 return NULL;
2619}
2620
2621/** Return the RSA ID digest for the chosen exit router in <b>state</b>.
2622 * If there is no chosen exit, return NULL.
2623 */
2624const uint8_t *
2626{
2627 if (!state || !state->chosen_exit)
2628 return NULL;
2629 return (const uint8_t *) state->chosen_exit->identity_digest;
2630}
2631
2632/** Return the nickname for the chosen exit router in <b>state</b>. If
2633 * there is no chosen exit, or if we don't know the routerinfo_t for the
2634 * chosen exit, return NULL.
2635 */
2636const char *
2638{
2639 if (!state || !state->chosen_exit)
2640 return NULL;
2641 return state->chosen_exit->nickname;
2642}
2643
2644/* Is circuit purpose allowed to use the deprecated TAP encryption protocol?
2645 * The hidden service protocol still uses TAP for some connections, because
2646 * ntor onion keys aren't included in HS descriptors or INTRODUCE cells. */
2647static int
2648circuit_purpose_can_use_tap_impl(uint8_t purpose)
2649{
2650 return (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
2652}
2653
2654/* Is circ allowed to use the deprecated TAP encryption protocol?
2655 * The hidden service protocol still uses TAP for some connections, because
2656 * ntor onion keys aren't included in HS descriptors or INTRODUCE cells. */
2657int
2658circuit_can_use_tap(const origin_circuit_t *circ)
2659{
2660 tor_assert(circ);
2661 tor_assert(circ->cpath);
2663 return (circuit_purpose_can_use_tap_impl(circ->base_.purpose) &&
2664 extend_info_supports_tap(circ->cpath->extend_info));
2665}
2666
2667/* Does circ have an onion key which it's allowed to use? */
2668int
2669circuit_has_usable_onion_key(const origin_circuit_t *circ)
2670{
2671 tor_assert(circ);
2672 tor_assert(circ->cpath);
2674 return (extend_info_supports_ntor(circ->cpath->extend_info) ||
2675 circuit_can_use_tap(circ));
2676}
2677
2678/** Find the circuits that are waiting to find out whether their guards are
2679 * usable, and if any are ready to become usable, mark them open and try
2680 * attaching streams as appropriate. */
2681void
2683{
2684 smartlist_t *to_upgrade =
2686
2687 if (to_upgrade == NULL)
2688 return;
2689
2690 log_info(LD_GUARD, "Upgrading %d circuits from 'waiting for better guard' "
2691 "to 'open'.", smartlist_len(to_upgrade));
2692
2693 SMARTLIST_FOREACH_BEGIN(to_upgrade, origin_circuit_t *, circ) {
2695 circuit_has_opened(circ);
2696 } SMARTLIST_FOREACH_END(circ);
2697
2698 smartlist_free(to_upgrade);
2699}
2700
2701/**
2702 * Try to generate a circuit-negotiation message for communication with a
2703 * given relay. Assumes we are using ntor v3, or some later version that
2704 * supports parameter negotiatoin.
2705 *
2706 * On success, return 0 and pass back a message in the `out` parameters.
2707 * Otherwise, return -1.
2708 **/
2709int
2711 uint8_t **msg_out,
2712 size_t *msg_len_out)
2713{
2714 tor_assert(ei && msg_out && msg_len_out);
2715
2717 return -1;
2718 }
2719
2720 return congestion_control_build_ext_request(msg_out, msg_len_out);
2721}
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
time_t approx_time(void)
Definition: approx_time.c:32
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
int extend_info_is_a_configured_bridge(const extend_info_t *ei)
Definition: bridges.c:325
Header file for circuitbuild.c.
Fixed-size cell structure.
void channel_timestamp_client(channel_t *chan)
Definition: channel.c:3198
channel_t * channel_get_for_extend(const char *rsa_id_digest, const ed25519_public_key_t *ed_id, const tor_addr_t *target_ipv4_addr, const tor_addr_t *target_ipv6_addr, bool for_origin_circ, const char **msg_out, int *launch_out)
Definition: channel.c:2416
const char * channel_state_to_string(channel_state_t state)
Definition: channel.c:316
int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
Definition: channel.c:3295
int channel_remote_identity_matches(const channel_t *chan, const char *rsa_id_digest, const ed25519_public_key_t *ed_id)
Definition: channel.c:668
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:2326
const char * channel_describe_peer(channel_t *chan)
Definition: channel.c:2840
void channel_dump_statistics(channel_t *chan, int severity)
Definition: channel.c:2544
Header file for channel.c.
void channel_mark_as_used_for_origin_circuit(channel_t *chan)
Definition: channeltls.c:391
@ CIRC_ID_TYPE_NEITHER
Definition: channel.h:44
@ CIRC_ID_TYPE_HIGHER
Definition: channel.h:41
int pathbias_count_build_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:446
void pathbias_count_build_success(origin_circuit_t *circ)
Definition: circpathbias.c:534
static int circuit_send_first_onion_skin(origin_circuit_t *circ)
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:919
static int circuit_build_no_more_hops(origin_circuit_t *circ)
int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
STATIC int count_acceptable_nodes(const smartlist_t *nodes, int direct)
STATIC int onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit_ei, int is_hs_v3_rp_circuit)
const char * build_state_get_exit_nickname(cpath_build_state_t *state)
int circuit_handle_first_hop(origin_circuit_t *circ)
Definition: circuitbuild.c:588
static int middle_node_must_be_vanguard(const or_options_t *options, uint8_t purpose, int cur_len)
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:357
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)
STATIC circid_t get_unique_circ_id_by_chan(channel_t *chan)
Definition: circuitbuild.c:127
static smartlist_t * build_middle_exclude_list(const origin_circuit_t *circ, uint8_t purpose, cpath_build_state_t *state, crypt_path_t *head, int cur_len)
int route_len_for_purpose(uint8_t purpose, extend_info_t *exit_ei)
STATIC int onion_extend_cpath(origin_circuit_t *circ)
int client_circ_negotiation_message(const extend_info_t *ei, uint8_t **msg_out, size_t *msg_len_out)
static void circuit_pick_create_handshake(uint8_t *cell_type_out, uint16_t *handshake_type_out, const extend_info_t *ei)
Definition: circuitbuild.c:887
char * circuit_list_path_for_controller(origin_circuit_t *circ)
Definition: circuitbuild.c:347
STATIC int new_route_len(uint8_t purpose, extend_info_t *exit_ei, const smartlist_t *nodes)
static const node_t * choose_good_middle_server(const origin_circuit_t *, uint8_t purpose, cpath_build_state_t *state, crypt_path_t *head, int cur_len)
circuit_guard_state_t * origin_circuit_get_guard_state(origin_circuit_t *circ)
Definition: circuitbuild.c:557
static int onion_populate_cpath(origin_circuit_t *circ)
Definition: circuitbuild.c:390
int circuit_deliver_create_cell(circuit_t *circ, const struct create_cell_t *create_cell, int relayed)
Definition: circuitbuild.c:785
static int circuit_may_omit_guard(const origin_circuit_t *circ)
Definition: circuitbuild.c:945
static int ap_stream_wants_exit_attention(connection_t *conn)
const uint8_t * build_state_get_exit_rsa_id(cpath_build_state_t *state)
void circuit_n_chan_done(channel_t *chan, int status, int close_origin_circuits)
Definition: circuitbuild.c:683
static smartlist_t * build_vanguard_middle_exclude_list(uint8_t purpose, cpath_build_state_t *state, crypt_path_t *head, int cur_len)
static int circuit_cpath_supports_ntor(const origin_circuit_t *circ)
Definition: circuitbuild.c:366
int circuit_timeout_want_to_count_circ(const origin_circuit_t *circ)
Definition: circuitbuild.c:872
static smartlist_t * circuit_get_unhandled_ports(time_t now)
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 const node_t * choose_good_exit_server(origin_circuit_t *circ, router_crn_flags_t flags, int is_internal)
static void warn_if_last_router_excluded(origin_circuit_t *circ, const extend_info_t *exit_ei)
origin_circuit_t * circuit_establish_circuit_conflux(const uint8_t *conflux_nonce, uint8_t purpose, extend_info_t *exit_ei, int flags)
Definition: circuitbuild.c:526
channel_t * channel_connect_for_circuit(const extend_info_t *ei)
Definition: circuitbuild.c:104
char * circuit_list_path(origin_circuit_t *circ, int verbose)
Definition: circuitbuild.c:338
int circuit_all_predicted_ports_handled(time_t now, int *need_uptime, int *need_capacity)
void circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle)
int circuit_send_next_onion_skin(origin_circuit_t *circ)
Definition: circuitbuild.c:982
int circuit_finish_handshake(origin_circuit_t *circ, const created_cell_t *reply)
static const node_t * choose_good_exit_server_general(router_crn_flags_t flags)
int circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
origin_circuit_t * circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
Definition: circuitbuild.c:485
int circuit_truncated(origin_circuit_t *circ, int reason)
const node_t * choose_good_entry_server(const origin_circuit_t *circ, uint8_t purpose, cpath_build_state_t *state, circuit_guard_state_t **guard_state_out)
static void circuit_chan_publish(const origin_circuit_t *circ, const channel_t *chan)
Definition: circuitbuild.c:572
static bool should_use_create_fast_for_circuit(origin_circuit_t *circ)
Definition: circuitbuild.c:850
static int circuit_send_intermediate_onion_skin(origin_circuit_t *circ, crypt_path_t *hop)
const node_t * build_state_get_exit_node(cpath_build_state_t *state)
static int node_handles_some_port(const node_t *node, smartlist_t *needed_ports)
static char * circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
Definition: circuitbuild.c:264
void circuit_upgrade_circuits_from_guard_wait(void)
origin_circuit_t * origin_circuit_init(uint8_t purpose, int flags)
Definition: circuitbuild.c:454
Header file for circuitbuild.c.
int circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1574
void circuit_set_n_circid_chan(circuit_t *circ, circid_t id, channel_t *chan)
Definition: circuitlist.c:493
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2106
void circuit_set_state(circuit_t *circ, uint8_t state)
Definition: circuitlist.c:562
smartlist_t * circuit_find_circuits_to_upgrade_from_guard_wait(void)
Definition: circuitlist.c:2007
origin_circuit_t * origin_circuit_new(void)
Definition: circuitlist.c:1050
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:185
int circuit_get_cpath_len(origin_circuit_t *circ)
Definition: circuitlist.c:2035
int circuit_get_cpath_opened_len(const origin_circuit_t *circ)
Definition: circuitlist.c:2051
void circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
Definition: circuitlist.c:597
int circuit_event_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
Definition: circuitlist.c:518
time_t circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1587
crypt_path_t * circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
Definition: circuitlist.c:2071
const char * circuit_purpose_to_string(uint8_t purpose)
Definition: circuitlist.c:929
void circuit_mark_all_unused_circs(void)
Definition: circuitlist.c:2087
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_S_CONNECT_REND
Definition: circuitlist.h:107
#define CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
Definition: circuitlist.h:93
#define CIRCUIT_PURPOSE_REND_POINT_WAITING
Definition: circuitlist.h:45
#define CIRCUIT_STATE_OPEN
Definition: circuitlist.h:32
#define CIRCUIT_STATE_BUILDING
Definition: circuitlist.h:21
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:88
#define CIRCUIT_PURPOSE_INTRO_POINT
Definition: circuitlist.h:42
#define CIRCUIT_PURPOSE_CONTROLLER
Definition: circuitlist.h:121
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:154
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:86
#define CIRCUIT_STATE_GUARD_WAIT
Definition: circuitlist.h:30
#define CIRCUIT_PURPOSE_TESTING
Definition: circuitlist.h:118
#define CIRCUIT_PURPOSE_OR
Definition: circuitlist.h:39
#define CIRCUIT_STATE_CHAN_WAIT
Definition: circuitlist.h:26
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
Definition: circuitlist.h:76
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:110
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:83
#define CIRCUIT_PURPOSE_S_HSDIR_POST
Definition: circuitlist.h:112
#define CIRCUIT_PURPOSE_C_HSDIR_GET
Definition: circuitlist.h:90
#define CIRCUIT_PURPOSE_REND_ESTABLISHED
Definition: circuitlist.h:47
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
Definition: circuitlist.h:79
#define CIRCUIT_PURPOSE_C_INTRODUCING
Definition: circuitlist.h:73
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:101
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:81
#define CIRCUIT_PURPOSE_C_GENERAL
Definition: circuitlist.h:70
#define CIRCUIT_PURPOSE_CONFLUX_UNLINKED
Definition: circuitlist.h:137
#define CIRCUIT_PURPOSE_HS_VANGUARDS
Definition: circuitlist.h:131
unsigned int circuitmux_num_active_circuits(circuitmux_t *cmux)
Definition: circuitmux.c:702
unsigned int circuitmux_num_circuits(circuitmux_t *cmux)
Definition: circuitmux.c:714
void circpad_machine_event_circ_added_hop(origin_circuit_t *on_circ)
void circpad_machine_event_circ_built(origin_circuit_t *circ)
Header file for circuitpadding.c.
void circuit_build_times_handle_completed_hop(origin_circuit_t *circ)
Definition: circuitstats.c:679
Header file for circuitstats.c.
int circuit_should_use_vanguards(uint8_t purpose)
Definition: circuituse.c:2013
void circuit_has_opened(origin_circuit_t *circ)
Definition: circuituse.c:1659
void circuit_reset_failure_count(int timeout)
Definition: circuituse.c:2218
int circuit_stream_is_being_handled(entry_connection_t *conn, uint16_t port, int min)
Definition: circuituse.c:1009
void circuit_remove_handled_ports(smartlist_t *needed_ports)
Definition: circuituse.c:984
int circuit_purpose_is_hidden_service(uint8_t purpose)
Definition: circuituse.c:1951
Header file for circuituse.c.
#define CIRCLAUNCH_NEED_CAPACITY
Definition: circuituse.h:43
#define CIRCLAUNCH_IS_IPV6_SELFTEST
Definition: circuituse.h:54
#define CIRCLAUNCH_ONEHOP_TUNNEL
Definition: circuituse.h:39
#define CIRCLAUNCH_IS_V3_RP
Definition: circuituse.h:49
#define CIRCLAUNCH_IS_INTERNAL
Definition: circuituse.h:46
#define CIRCLAUNCH_NEED_UPTIME
Definition: circuituse.h:41
#define CIRCLAUNCH_NEED_CONFLUX
Definition: circuituse.h:56
void command_setup_channel(channel_t *chan)
Definition: command.c:697
Header file for command.c.
const or_options_t * get_options(void)
Definition: config.c:944
tor_cmdline_mode_t command
Definition: config.c:2468
Header file for config.c.
void conflux_add_middles_to_exclude_list(const origin_circuit_t *orig_circ, smartlist_t *excluded)
Header file for conflux_pool.c.
Header for confmgt.c.
congestion_control_t * congestion_control_new(const circuit_params_t *params, cc_path_t path)
int congestion_control_build_ext_request(uint8_t **msg_out, size_t *msg_len_out)
bool congestion_control_enabled(void)
Public APIs for congestion control.
#define SBWS_ROUTE_LEN
Header file for connection.c.
#define CONN_TYPE_AP
Definition: connection.h:51
int connection_ap_can_use_exit(const entry_connection_t *conn, const node_t *exit_node)
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
entry_connection_t * TO_ENTRY_CONN(connection_t *c)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
Header file for connection_edge.c.
#define AP_CONN_STATE_CIRCUIT_WAIT
void clear_broken_connection_map(int stop_recording)
Header file for connection_or.c.
void control_event_bootstrap(bootstrap_status_t status, int progress)
int control_event_general_status(int severity, const char *format,...)
int control_event_client_status(int severity, const char *format,...)
Header file for control_events.c.
Circuit-build-stse structure.
crypt_path_t * cpath_get_next_non_open_hop(crypt_path_t *cpath)
Definition: crypt_path.c:227
int cpath_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
Definition: crypt_path.c:59
int cpath_init_circuit_crypto(crypt_path_t *cpath, const char *key_data, size_t key_data_len, int reverse, int is_hs_v3)
Definition: crypt_path.c:148
void cpath_free(crypt_path_t *victim)
Definition: crypt_path.c:160
Header file for crypt_path.c.
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
void ed25519_pubkey_copy(ed25519_public_key_t *dest, const ed25519_public_key_t *src)
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
void * smartlist_choose(const smartlist_t *sl)
Definition: crypto_rand.c:594
void crypto_rand(char *to, size_t n)
Definition: crypto_rand.c:479
Common functions for using (pseudo-)random number generators.
const char * extend_info_describe(const extend_info_t *ei)
Definition: describe.c:224
const char * node_describe(const node_t *node)
Definition: describe.c:160
Header file for describe.c.
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Header file for directory.c.
Entry connection structure.
const routerset_t * get_layer2_guards(void)
Definition: entrynodes.c:4310
guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2605
bool vanguards_lite_is_enabled(void)
Definition: entrynodes.c:4081
const node_t * guards_choose_guard(const origin_circuit_t *circ, cpath_build_state_t *state, uint8_t purpose, circuit_guard_state_t **guard_state_out)
Definition: entrynodes.c:3868
Header file for circuitbuild.c.
Header file for Tor tracing instrumentation definition.
#define TR_SUBSYS(name)
Definition: events.h:45
Extend-info structure.
extend_info_t * extend_info_dup(extend_info_t *info)
Definition: extendinfo.c:193
extend_info_t * extend_info_from_node(const node_t *node, int for_direct_connect, bool for_exit)
Definition: extendinfo.c:100
const tor_addr_port_t * extend_info_pick_orport(const extend_info_t *ei)
Definition: extendinfo.c:300
const tor_addr_port_t * extend_info_get_orport(const extend_info_t *ei, int family)
Definition: extendinfo.c:285
bool extend_info_any_orport_addr_is_internal(const extend_info_t *ei)
Definition: extendinfo.c:340
Header for core/or/extendinfo.c.
Header for hs_ntor.c.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:591
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_REND
Definition: log.h:84
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:288
#define LD_APP
Definition: log.h:78
#define LD_PROTOCOL
Definition: log.h:72
#define LD_BUG
Definition: log.h:86
#define LD_GUARD
Definition: log.h:109
#define LD_GENERAL
Definition: log.h:62
#define LOG_NOTICE
Definition: log.h:50
#define LD_CIRC
Definition: log.h:82
#define LOG_WARN
Definition: log.h:53
#define LOG_INFO
Definition: log.h:45
void note_that_we_maybe_cant_complete_circuits(void)
Definition: mainloop.c:234
int have_completed_a_circuit(void)
Definition: mainloop.c:218
void note_that_we_completed_a_circuit(void)
Definition: mainloop.c:226
smartlist_t * get_connection_array(void)
Definition: mainloop.c:443
void reset_all_main_loop_timers(void)
Definition: mainloop.c:1466
Header file for mainloop.c.
#define tor_free(p)
Definition: malloc.h:56
Header file for microdesc.c.
Header file for networkstatus.c.
int is_legal_nickname(const char *s)
Definition: nickname.c:19
Header file for nickname.c.
const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
Definition: node_select.c:979
const node_t * node_sl_choose_by_bandwidth(const smartlist_t *sl, bandwidth_weight_rule_t rule)
Definition: node_select.c:856
Header file for node_select.c.
router_crn_flags_t
Definition: node_select.h:16
Node information structure.
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:226
void nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
Definition: nodelist.c:2251
const smartlist_t * nodelist_get_list(void)
Definition: nodelist.c:1047
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1509
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1542
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2436
int node_exit_policy_rejects_all(const node_t *node)
Definition: nodelist.c:1588
Header file for nodelist.c.
Header file for ocirc_event.c.
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:617
Header file for onion.c.
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, circuit_params_t *params_out, const char **msg_out)
Definition: onion_crypto.c:462
int onion_skin_create(int type, const extend_info_t *node, onion_handshake_state_t *state_out, uint8_t *onion_skin_out, size_t onion_skin_out_maxlen)
Definition: onion_crypto.c:132
void onion_handshake_state_release(onion_handshake_state_t *state)
Definition: onion_crypto.c:97
Header file for onion_crypto.c.
Header file for onion_fast.c.
Header file for onion_tap.c.
Master header file for Tor-specific functionality.
#define MAX_NICKNAME_LEN
Definition: or.h:112
#define MIN_CIRCUITS_HANDLING_STREAM
Definition: or.h:180
#define DEFAULT_ROUTE_LEN
Definition: or.h:902
uint32_t circid_t
Definition: or.h:497
#define END_CIRC_REASON_NOPATH
Definition: or.h:316
#define TO_CIRCUIT(x)
Definition: or.h:848
#define MAX_VERBOSE_NICKNAME_LEN
Definition: or.h:118
#define RELAY_PAYLOAD_SIZE
Definition: or.h:494
#define END_CIRC_REASON_FLAG_REMOTE
Definition: or.h:341
@ CELL_DIRECTION_OUT
Definition: or.h:377
Origin circuit structure.
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:2907
Header file for policies.c.
addr_policy_result_t
Definition: policies.h:38
@ ADDR_POLICY_PROBABLY_REJECTED
Definition: policies.h:48
@ ADDR_POLICY_REJECTED
Definition: policies.h:42
void rep_hist_remove_predicted_ports(const smartlist_t *rmv_ports)
smartlist_t * rep_hist_get_predicted_ports(time_t now)
Header file for predict_ports.c.
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:42
int append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan, cell_t *cell, cell_direction_t direction, streamid_t fromstream)
Definition: relay.c:3406
Header file for relay.c.
int router_digest_is_me(const char *digest)
Definition: router.c:1744
Header file for router.c.
void router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags)
Definition: routerlist.c:618
Header file for routerlist.c.
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
int routerset_contains_node(const routerset_t *set, const node_t *node)
Definition: routerset.c:353
void routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset, const routerset_t *excludeset, int running_only)
Definition: routerset.c:379
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:308
void routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
Definition: routerset.c:413
Header file for routerset.c.
void router_do_reachability_checks(void)
Definition: selftest.c:292
Header file for selftest.c.
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
int smartlist_contains_int_as_string(const smartlist_t *sl, int num)
Definition: smartlist.c:147
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:264
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: cell_st.h:17
circid_t circ_id
Definition: cell_st.h:18
channel_state_t state
Definition: channel.h:192
int(* is_canonical)(channel_t *)
Definition: channel.h:353
unsigned int num_n_circuits
Definition: channel.h:410
circ_id_type_bitfield_t circ_id_type
Definition: channel.h:405
char identity_digest[DIGEST_LEN]
Definition: channel.h:378
uint64_t global_identifier
Definition: channel.h:197
channel_usage_info_t channel_usage
Definition: channel.h:228
time_t timestamp_created
Definition: channel.h:298
circuitmux_t * cmux
Definition: channel.h:397
ratelim_t last_warned_circ_ids_exhausted
Definition: channel.h:438
uint8_t state
Definition: circuit_st.h:111
uint8_t purpose
Definition: circuit_st.h:112
struct timeval timestamp_began
Definition: circuit_st.h:166
channel_t * n_chan
Definition: circuit_st.h:70
extend_info_t * n_hop
Definition: circuit_st.h:88
circid_t n_circ_id
Definition: circuit_st.h:79
uint8_t state
Definition: connection_st.h:49
unsigned int type
Definition: connection_st.h:50
uint16_t marked_for_close
extend_info_t * chosen_exit
uint16_t handshake_len
Definition: onion.h:30
uint16_t handshake_type
Definition: onion.h:28
uint8_t onionskin[CELL_PAYLOAD_SIZE - 4]
Definition: onion.h:32
uint8_t cell_type
Definition: onion.h:26
uint16_t handshake_len
Definition: onion.h:40
uint8_t reply[CELL_PAYLOAD_SIZE - 2]
Definition: onion.h:42
struct crypt_path_t * prev
Definition: crypt_path_st.h:80
uint8_t state
Definition: crypt_path_st.h:73
struct crypt_path_t * next
Definition: crypt_path_st.h:77
extend_info_t * extend_info
Definition: crypt_path_st.h:66
char rend_circ_nonce[DIGEST_LEN]
Definition: crypt_path_st.h:63
onion_handshake_state_t handshake_state
Definition: crypt_path_st.h:57
struct congestion_control_t * ccontrol
Definition: crypt_path_st.h:89
struct edge_connection_t * next_stream
tor_addr_port_t orport_ipv4
Definition: onion.h:50
create_cell_t create_cell
Definition: onion.h:60
struct ed25519_public_key_t ed_pubkey
Definition: onion.h:56
uint8_t node_id[DIGEST_LEN]
Definition: onion.h:54
tor_addr_port_t orport_ipv6
Definition: onion.h:52
uint8_t cell_type
Definition: onion.h:48
ed25519_public_key_t ed_identity
char identity_digest[DIGEST_LEN]
char nickname[MAX_HEX_NICKNAME_LEN+1]
bool exit_supports_congestion_control
Definition: node_st.h:34
char identity[DIGEST_LEN]
Definition: node_st.h:46
struct routerset_t * ExcludeExitNodesUnion_
struct routerset_t * ExcludeNodes
struct routerset_t * ExitNodes
struct routerset_t * HSLayer2Nodes
struct smartlist_t * LongLivedPorts
int ExtendAllowPrivateAddresses
struct routerset_t * MiddleNodes
struct routerset_t * HSLayer3Nodes
edge_connection_t * p_streams
unsigned int has_opened
crypt_path_t * cpath
cpath_build_state_t * build_state
struct circuit_guard_state_t * guard_state
unsigned first_hop_from_controller
#define STATIC
Definition: testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
void tor_gettimeofday(struct timeval *timeval)
Headers for transports.c.
#define tor_assert_nonfatal_unreached()
Definition: util_bug.h:177
#define tor_assert(expr)
Definition: util_bug.h:103
#define tor_fragile_assert()
Definition: util_bug.h:278
#define IF_BUG_ONCE(cond)
Definition: util_bug.h:254
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:98