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