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