Tor 0.4.9.0-alpha-dev
circuituse.c
Go to the documentation of this file.
1/* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file circuituse.c
9 * \brief Launch the right sort of circuits and attach the right streams to
10 * them.
11 *
12 * As distinct from circuitlist.c, which manages lookups to find circuits, and
13 * circuitbuild.c, which handles the logistics of circuit construction, this
14 * module keeps track of which streams can be attached to which circuits (in
15 * circuit_get_best()), and attaches streams to circuits (with
16 * circuit_try_attaching_streams(), connection_ap_handshake_attach_circuit(),
17 * and connection_ap_handshake_attach_chosen_circuit() ).
18 *
19 * This module also makes sure that we are building circuits for all of the
20 * predicted ports, using circuit_remove_handled_ports(),
21 * circuit_stream_is_being_handled(), and circuit_build_needed_cirs(). It
22 * handles launching circuits for specific targets using
23 * circuit_launch_by_extend_info().
24 *
25 * This is also where we handle expiring circuits that have been around for
26 * too long without actually completing, along with the circuit_build_timeout
27 * logic in circuitstats.c.
28 **/
29
30#include "core/or/or.h"
31#include "app/config/config.h"
33#include "core/or/channel.h"
35#include "core/or/circuitlist.h"
37#include "core/or/circuituse.h"
41#include "core/or/extendinfo.h"
42#include "core/or/policies.h"
43#include "core/or/trace_probes_circuit.h"
46#include "feature/client/circpathbias.h"
54#include "feature/hs/hs_ident.h"
56#include "feature/hs/hs_stats.h"
64#include "lib/math/fp.h"
65#include "lib/time/tvdiff.h"
66#include "lib/trace/events.h"
68#include "core/or/conflux.h"
69
74#include "core/or/or_circuit_st.h"
77
79
81static void circuit_increment_failure_count(void);
82
83/** Check whether the hidden service destination of the stream at
84 * <b>edge_conn</b> is the same as the destination of the circuit at
85 * <b>origin_circ</b>. */
86static int
88 const origin_circuit_t *origin_circ)
89{
90 /* Check if this is a v3 rendezvous circ/stream */
91 if ((edge_conn->hs_ident && !origin_circ->hs_ident) ||
92 (!edge_conn->hs_ident && origin_circ->hs_ident) ||
93 (edge_conn->hs_ident && origin_circ->hs_ident &&
94 !ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
95 &origin_circ->hs_ident->identity_pk))) {
96 /* this circ is not for this conn */
97 return 0;
98 }
99
100 return 1;
101}
102
103/** Return 1 if <b>circ</b> could be returned by circuit_get_best().
104 * Else return 0.
105 */
106int
108 const entry_connection_t *conn,
109 int must_be_open, uint8_t purpose,
110 int need_uptime, int need_internal,
111 time_t now)
112{
113 const circuit_t *circ = TO_CIRCUIT(origin_circ);
114 const node_t *exitnode;
115 cpath_build_state_t *build_state;
116 tor_assert(circ);
117 tor_assert(conn);
119
120 if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_chan))
121 return 0; /* ignore non-open circs */
122 if (circ->marked_for_close)
123 return 0;
124
125 /* if this circ isn't our purpose, skip. */
126 if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
131 return 0;
132 } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
133 !must_be_open) {
136 return 0;
137 } else {
138 if (purpose != circ->purpose)
139 return 0;
140 }
141
142 if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
143 purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
144 purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
145 purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
147 purpose == CIRCUIT_PURPOSE_CONFLUX_LINKED) {
148 if (circ->timestamp_dirty &&
149 circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
150 return 0;
151 }
152
153 if (origin_circ->unusable_for_new_conns)
154 return 0;
155
156 /* decide if this circ is suitable for this conn */
157
158 /* for rend circs, circ->cpath->prev is not the last router in the
159 * circuit, it's the magical extra service hop. so just check the nickname
160 * of the one we meant to finish at.
161 */
162 build_state = origin_circ->build_state;
163 exitnode = build_state_get_exit_node(build_state);
164
165 if (need_uptime && !build_state->need_uptime)
166 return 0;
167 if (need_internal != build_state->is_internal)
168 return 0;
169
170 if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
172 purpose == CIRCUIT_PURPOSE_CONFLUX_LINKED ||
173 purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
174 purpose == CIRCUIT_PURPOSE_C_HSDIR_GET) {
175 tor_addr_t addr;
176 if (!exitnode && !build_state->onehop_tunnel) {
177 log_debug(LD_CIRC,"Not considering circuit with unknown router.");
178 return 0; /* this circuit is screwed and doesn't know it yet,
179 * or is a rendezvous circuit. */
180 }
181 if (build_state->onehop_tunnel) {
182 if (!conn->want_onehop) {
183 log_debug(LD_CIRC,"Skipping one-hop circuit.");
184 return 0;
185 }
187 if (build_state->chosen_exit) {
188 char digest[DIGEST_LEN];
189 if (hexdigest_to_digest(conn->chosen_exit_name, digest) < 0)
190 return 0; /* broken digest, we don't want it */
191 if (tor_memneq(digest, build_state->chosen_exit->identity_digest,
192 DIGEST_LEN))
193 return 0; /* this is a circuit to somewhere else */
194 if (tor_digest_is_zero(digest)) {
195 /* we don't know the digest; have to compare addr:port */
196 const int family = tor_addr_parse(&addr,
197 conn->socks_request->address);
198 if (family < 0 ||
199 !extend_info_has_orport(build_state->chosen_exit, &addr,
200 conn->socks_request->port))
201 return 0;
202 }
203 }
204 } else {
205 if (conn->want_onehop) {
206 /* don't use three-hop circuits -- that could hurt our anonymity. */
207 return 0;
208 }
209 }
210 if (origin_circ->prepend_policy) {
211 if (tor_addr_parse(&addr, conn->socks_request->address) != -1) {
213 conn->socks_request->port,
214 origin_circ->prepend_policy);
215 if (r == ADDR_POLICY_REJECTED)
216 return 0;
217 }
218 }
219 if (exitnode && !connection_ap_can_use_exit(conn, exitnode)) {
220 /* can't exit from this router */
221 return 0;
222 }
223 } else { /* not general: this might be a rend circuit */
224 const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
225 if (!circuit_matches_with_rend_stream(edge_conn, origin_circ)) {
226 return 0;
227 }
228 }
229
230 if (!connection_edge_compatible_with_circuit(conn, origin_circ)) {
231 /* conn needs to be isolated from other conns that have already used
232 * origin_circ */
233 return 0;
234 }
235
236 return 1;
237}
238
239/** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
240 * <b>conn</b>, and return 0 otherwise. Used by circuit_get_best.
241 */
242static int
244 const entry_connection_t *conn)
245{
246 const circuit_t *a = TO_CIRCUIT(oa);
247 const circuit_t *b = TO_CIRCUIT(ob);
248 const uint8_t purpose = ENTRY_TO_CONN(conn)->purpose;
249 int a_bits, b_bits;
250
251 /* If one of the circuits was allowed to live due to relaxing its timeout,
252 * it is definitely worse (it's probably a much slower path). */
253 if (oa->relaxed_timeout && !ob->relaxed_timeout)
254 return 0; /* ob is better. It's not relaxed. */
255 if (!oa->relaxed_timeout && ob->relaxed_timeout)
256 return 1; /* oa is better. It's not relaxed. */
257
258 switch (purpose) {
262 /* if it's used but less dirty it's best;
263 * else if it's more recently created it's best
264 */
265 if (b->timestamp_dirty) {
266 if (a->timestamp_dirty &&
268 return 1;
269 } else {
270 if (a->timestamp_dirty ||
272 return 1;
273 if (ob->build_state->is_internal)
274 /* XXXX++ what the heck is this internal thing doing here. I
275 * think we can get rid of it. circuit_is_acceptable() already
276 * makes sure that is_internal is exactly what we need it to
277 * be. -RD */
278 return 1;
279 }
280 break;
282 /* the closer it is to ack_wait the better it is */
283 if (a->purpose > b->purpose)
284 return 1;
285 break;
287 /* the closer it is to rend_joined the better it is */
288 if (a->purpose > b->purpose)
289 return 1;
290 break;
291 }
292
293 /* XXXX Maybe this check should get a higher priority to avoid
294 * using up circuits too rapidly. */
295
297 (origin_circuit_t*)oa, 1);
299 (origin_circuit_t*)ob, 1);
300 /* if x_bits < 0, then we have not used x for anything; better not to dirty
301 * a connection if we can help it. */
302 if (a_bits < 0) {
303 return 0;
304 } else if (b_bits < 0) {
305 return 1;
306 }
307 a_bits &= ~ oa->isolation_flags_mixed;
308 a_bits &= ~ ob->isolation_flags_mixed;
309 if (n_bits_set_u8(a_bits) < n_bits_set_u8(b_bits)) {
310 /* The fewer new restrictions we need to make on a circuit for stream
311 * isolation, the better. */
312 return 1;
313 }
314
315 return 0;
316}
317
318/** Find the best circ that conn can use, preferably one which is
319 * dirty. Circ must not be too old.
320 *
321 * Conn must be defined.
322 *
323 * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
324 *
325 * circ_purpose specifies what sort of circuit we must have.
326 * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
327 *
328 * If it's REND_JOINED and must_be_open==0, then return the closest
329 * rendezvous-purposed circuit that you can find.
330 *
331 * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
332 * closest introduce-purposed circuit that you can find.
333 */
334static origin_circuit_t *
336 int must_be_open, uint8_t purpose,
337 int need_uptime, int need_internal)
338{
339 origin_circuit_t *best=NULL;
340 struct timeval now;
341 time_t now_sec;
342
343 tor_assert(conn);
344
346 purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
347 purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
348 purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
351
352 tor_gettimeofday(&now);
353 now_sec = now.tv_sec;
354
355 // Prefer pre-built conflux circuits here, if available but only for general
356 // purposes. We don't have onion service conflux support at the moment.
357 if (purpose == CIRCUIT_PURPOSE_C_GENERAL &&
358 (best = conflux_get_circ_for_conn(conn, now_sec))) {
359 return best;
360 }
361
363 origin_circuit_t *origin_circ;
364 if (!CIRCUIT_IS_ORIGIN(circ))
365 continue;
366 origin_circ = TO_ORIGIN_CIRCUIT(circ);
367
368 if (!circuit_is_acceptable(origin_circ,conn,must_be_open,purpose,
369 need_uptime,need_internal, now_sec))
370 continue;
371
372 /* now this is an acceptable circ to hand back. but that doesn't
373 * mean it's the *best* circ to hand back. try to decide.
374 */
375 if (!best || circuit_is_better(origin_circ,best,conn))
376 best = origin_circ;
377 }
378 SMARTLIST_FOREACH_END(circ);
379
380 return best;
381}
382
383/** Return the number of not-yet-open general-purpose origin circuits. */
384static int
386{
387 int count = 0;
388
390 if (circ->marked_for_close ||
391 circ->state == CIRCUIT_STATE_OPEN ||
393 !CIRCUIT_IS_ORIGIN(circ))
394 continue;
395
396 ++count;
397 }
398 SMARTLIST_FOREACH_END(circ);
399
400 return count;
401}
402
403#if 0
404/** Check whether, according to the policies in <b>options</b>, the
405 * circuit <b>circ</b> makes sense. */
406/* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
407 * Also, it doesn't have the right definition of an exit circuit. Also,
408 * it's never called. */
409int
410circuit_conforms_to_options(const origin_circuit_t *circ,
411 const or_options_t *options)
412{
413 const crypt_path_t *cpath, *cpath_next = NULL;
414
415 /* first check if it includes any excluded nodes */
416 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
417 cpath_next = cpath->next;
419 cpath->extend_info))
420 return 0;
421 }
422
423 /* then consider the final hop */
425 circ->cpath->prev->extend_info))
426 return 0;
427
428 return 1;
429}
430#endif /* 0 */
431
432/**
433 * Close all circuits that start at us, aren't open, and were born
434 * at least CircuitBuildTimeout seconds ago.
435 *
436 * TODO: This function is now partially redundant to
437 * circuit_build_times_handle_completed_hop(), but that function only
438 * covers circuits up to and including 3 hops that are still actually
439 * completing hops. However, circuit_expire_building() also handles longer
440 * circuits, as well as circuits that are completely stalled.
441 * In the future (after prop247/other path selection revamping), we probably
442 * want to eliminate this rats nest in favor of a simpler approach.
443 */
444void
446{
447 /* circ_times.timeout_ms and circ_times.close_ms are from
448 * circuit_build_times_get_initial_timeout() if we haven't computed
449 * custom timeouts yet */
450 struct timeval general_cutoff, begindir_cutoff, fourhop_cutoff,
451 close_cutoff, extremely_old_cutoff,
452 cannibalized_cutoff, c_intro_cutoff, s_intro_cutoff, stream_cutoff,
453 c_rend_ready_cutoff;
454 const or_options_t *options = get_options();
455 struct timeval now;
456 cpath_build_state_t *build_state;
457 int any_opened_circs = 0;
458
459 tor_gettimeofday(&now);
460
461 /* Check to see if we have any opened circuits. If we don't,
462 * we want to be more lenient with timeouts, in case the
463 * user has relocated and/or changed network connections.
464 * See bug #3443. */
465 any_opened_circs = circuit_any_opened_circuits();
466
467#define SET_CUTOFF(target, msec) do { \
468 long ms = tor_lround(msec); \
469 struct timeval diff; \
470 diff.tv_sec = ms / 1000; \
471 diff.tv_usec = (int)((ms % 1000) * 1000); \
472 timersub(&now, &diff, &target); \
473 } while (0)
474
475 /**
476 * Because circuit build timeout is calculated only based on 3 hop
477 * general purpose circuit construction, we need to scale the timeout
478 * to make it properly apply to longer circuits, and circuits of
479 * certain usage types. The following diagram illustrates how we
480 * derive the scaling below. In short, we calculate the number
481 * of times our telescoping-based circuit construction causes cells
482 * to traverse each link for the circuit purpose types in question,
483 * and then assume each link is equivalent.
484 *
485 * OP --a--> A --b--> B --c--> C
486 * OP --a--> A --b--> B --c--> C --d--> D
487 *
488 * Let h = a = b = c = d
489 *
490 * Three hops (general_cutoff)
491 * RTTs = 3a + 2b + c
492 * RTTs = 6h
493 * Cannibalized:
494 * RTTs = a+b+c+d
495 * RTTs = 4h
496 * Four hops:
497 * RTTs = 4a + 3b + 2c + d
498 * RTTs = 10h
499 * Client INTRODUCE1+ACK: // XXX: correct?
500 * RTTs = 5a + 4b + 3c + 2d
501 * RTTs = 14h
502 * Server intro:
503 * RTTs = 4a + 3b + 2c
504 * RTTs = 9h
505 */
506 SET_CUTOFF(general_cutoff, get_circuit_build_timeout_ms());
507 SET_CUTOFF(begindir_cutoff, get_circuit_build_timeout_ms());
508
509 // TODO: We should probably use route_len_for_purpose() here instead,
510 // except that does not count the extra round trip for things like server
511 // intros and rends.
512
513 /* > 3hop circs seem to have a 1.0 second delay on their cannibalized
514 * 4th hop. */
515 SET_CUTOFF(fourhop_cutoff, get_circuit_build_timeout_ms() * (10/6.0) + 1000);
516
517 /* CIRCUIT_PURPOSE_C_ESTABLISH_REND behaves more like a RELAY cell.
518 * Use the stream cutoff (more or less). */
519 SET_CUTOFF(stream_cutoff, MAX(options->CircuitStreamTimeout,15)*1000 + 1000);
520
521 /* Be lenient with cannibalized circs. They already survived the official
522 * CBT, and they're usually not performance-critical. */
523 SET_CUTOFF(cannibalized_cutoff,
525 options->CircuitStreamTimeout * 1000) + 1000);
526
527 /* Intro circs have an extra round trip (and are also 4 hops long) */
528 SET_CUTOFF(c_intro_cutoff, get_circuit_build_timeout_ms() * (14/6.0) + 1000);
529
530 /* Server intro circs have an extra round trip */
531 SET_CUTOFF(s_intro_cutoff, get_circuit_build_timeout_ms() * (9/6.0) + 1000);
532
533 /* For circuit purpose set to: CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED.
534 *
535 * The cutoff of such circuit is very high because we end up in this state if
536 * once the INTRODUCE_ACK is received which could be before the service
537 * receives the INTRODUCE2 cell. The worst case is a full 3-hop latency
538 * (intro -> service), 4-hop circuit creation latency (service -> RP), and
539 * finally a 7-hop latency for the RENDEZVOUS2 cell to arrive (service ->
540 * client). */
541 SET_CUTOFF(c_rend_ready_cutoff, get_circuit_build_timeout_ms() * 3 + 1000);
542
543 SET_CUTOFF(close_cutoff, get_circuit_build_close_time_ms());
544 SET_CUTOFF(extremely_old_cutoff, get_circuit_build_close_time_ms()*2 + 1000);
545
546 bool fixed_time = circuit_build_times_disabled(get_options());
547
549 struct timeval cutoff;
550
551 if (!CIRCUIT_IS_ORIGIN(victim) || /* didn't originate here */
552 victim->marked_for_close) /* don't mess with marked circs */
553 continue;
554
555 /* If we haven't yet started the first hop, it means we don't have
556 * any orconns available, and thus have not started counting time yet
557 * for this circuit. See circuit_deliver_create_cell() and uses of
558 * timestamp_began.
559 *
560 * Continue to wait in this case. The ORConn should timeout
561 * independently and kill us then.
562 */
563 if (TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_CLOSED) {
564 continue;
565 }
566
567 build_state = TO_ORIGIN_CIRCUIT(victim)->build_state;
568 if (build_state && build_state->onehop_tunnel)
569 cutoff = begindir_cutoff;
570 else if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
571 cutoff = close_cutoff;
572 else if (victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
573 cutoff = c_intro_cutoff;
574 else if (victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
575 cutoff = s_intro_cutoff;
576 else if (victim->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
577 /* Service connecting to a rendezvous point is a four hop circuit. We set
578 * it explicitly here because this function is a clusterf***. */
579 cutoff = fourhop_cutoff;
580 } else if (victim->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
581 cutoff = c_rend_ready_cutoff;
582 else if (victim->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
583 cutoff = close_cutoff;
584 else if (TO_ORIGIN_CIRCUIT(victim)->has_opened &&
585 victim->state != CIRCUIT_STATE_OPEN)
586 cutoff = cannibalized_cutoff;
587 else if (build_state && build_state->desired_path_len >= 4)
588 cutoff = fourhop_cutoff;
589 else
590 cutoff = general_cutoff;
591
592 if (timercmp(&victim->timestamp_began, &cutoff, OP_GT))
593 continue; /* it's still young, leave it alone */
594
595 /* We need to double-check the opened state here because
596 * we don't want to consider opened 1-hop dircon circuits for
597 * deciding when to relax the timeout, but we *do* want to relax
598 * those circuits too if nothing else is opened *and* they still
599 * aren't either. */
600 if (!any_opened_circs && victim->state != CIRCUIT_STATE_OPEN) {
601 /* It's still young enough that we wouldn't close it, right? */
602 if (timercmp(&victim->timestamp_began, &close_cutoff, OP_GT)) {
603 if (!TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout) {
604 int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath->state
605 == CPATH_STATE_OPEN;
606 if (!fixed_time) {
607 log_info(LD_CIRC,
608 "No circuits are opened. Relaxing timeout for circuit %d "
609 "(a %s %d-hop circuit in state %s with channel state %s).",
610 TO_ORIGIN_CIRCUIT(victim)->global_identifier,
611 circuit_purpose_to_string(victim->purpose),
612 TO_ORIGIN_CIRCUIT(victim)->build_state ?
613 TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
614 -1,
615 circuit_state_to_string(victim->state),
616 victim->n_chan ?
617 channel_state_to_string(victim->n_chan->state) : "none");
618 }
619
620 /* We count the timeout here for CBT, because technically this
621 * was a timeout, and the timeout value needs to reset if we
622 * see enough of them. Note this means we also need to avoid
623 * double-counting below, too. */
625 first_hop_succeeded);
627 }
628 continue;
629 } else {
630 static ratelim_t relax_timeout_limit = RATELIM_INIT(3600);
631 const double build_close_ms = get_circuit_build_close_time_ms();
632 if (!fixed_time) {
633 log_fn_ratelim(&relax_timeout_limit, LOG_NOTICE, LD_CIRC,
634 "No circuits are opened. Relaxed timeout for circuit %d "
635 "(a %s %d-hop circuit in state %s with channel state %s) to "
636 "%ldms. However, it appears the circuit has timed out "
637 "anyway.",
638 TO_ORIGIN_CIRCUIT(victim)->global_identifier,
639 circuit_purpose_to_string(victim->purpose),
640 TO_ORIGIN_CIRCUIT(victim)->build_state ?
641 TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
642 -1,
643 circuit_state_to_string(victim->state),
644 victim->n_chan ?
645 channel_state_to_string(victim->n_chan->state) : "none",
646 (long)build_close_ms);
647 }
648 }
649 }
650
651#if 0
652 /* some debug logs, to help track bugs */
653 if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
654 victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
655 if (!victim->timestamp_dirty)
656 log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d)."
657 "(clean).",
658 victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
659 victim->purpose, victim->build_state->chosen_exit_name,
660 victim->n_circ_id);
661 else
662 log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d). "
663 "%d secs since dirty.",
664 victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
665 victim->purpose, victim->build_state->chosen_exit_name,
666 victim->n_circ_id,
667 (int)(now - victim->timestamp_dirty));
668 }
669#endif /* 0 */
670
671 /* if circ is !open, or if it's open but purpose is a non-finished
672 * intro or rend, then mark it for close */
673 if (victim->state == CIRCUIT_STATE_OPEN) {
674 switch (victim->purpose) {
675 default: /* most open circuits can be left alone. */
676 continue; /* yes, continue inside a switch refers to the nearest
677 * enclosing loop. C is smart. */
679 break; /* too old, need to die */
681 /* it's a rend_ready circ -- has it already picked a query? */
682 /* c_rend_ready circs measure age since timestamp_dirty,
683 * because that's set when they switch purposes
684 */
685 if (TO_ORIGIN_CIRCUIT(victim)->hs_ident ||
686 victim->timestamp_dirty > cutoff.tv_sec)
687 continue;
688 break;
690 /* Open path bias testing circuits are given a long
691 * time to complete the test, but not forever */
693 break;
695 /* That purpose means that the intro point circuit has been opened
696 * successfully but the INTRODUCE1 cell hasn't been sent yet because
697 * the client is waiting for the rendezvous point circuit to open.
698 * Keep this circuit open while waiting for the rendezvous circuit.
699 * We let the circuit idle timeout take care of cleaning this
700 * circuit if it never used. */
701 continue;
705 /* rend and intro circs become dirty each time they
706 * make an introduction attempt. so timestamp_dirty
707 * will reflect the time since the last attempt.
708 */
709 if (victim->timestamp_dirty > cutoff.tv_sec)
710 continue;
711 break;
712 }
713 } else { /* circuit not open, consider recording failure as timeout */
714 int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath &&
715 TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_OPEN;
716 if (TO_ORIGIN_CIRCUIT(victim)->p_streams != NULL) {
717 log_warn(LD_BUG, "Circuit %d (purpose %d, %s) has timed out, "
718 "yet has attached streams!",
719 TO_ORIGIN_CIRCUIT(victim)->global_identifier,
720 victim->purpose,
721 circuit_purpose_to_string(victim->purpose));
723 continue;
724 }
725
728
729 log_info(LD_CIRC,
730 "Deciding to count the timeout for circuit %"PRIu32,
731 TO_ORIGIN_CIRCUIT(victim)->global_identifier);
732
733 /* Circuits are allowed to last longer for measurement.
734 * Switch their purpose and wait. */
735 if (victim->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
737 victim));
738 continue;
739 }
740
741 /*
742 * If the circuit build time is much greater than we would have cut
743 * it off at, we probably had a suspend event along this codepath,
744 * and we should discard the value.
745 */
746 if (timercmp(&victim->timestamp_began, &extremely_old_cutoff, OP_LT)) {
747 log_notice(LD_CIRC,
748 "Extremely large value for circuit build timeout: %lds. "
749 "Assuming clock jump. Purpose %d (%s)",
750 (long)(now.tv_sec - victim->timestamp_began.tv_sec),
751 victim->purpose,
752 circuit_purpose_to_string(victim->purpose));
755 first_hop_succeeded,
756 (time_t)victim->timestamp_created.tv_sec)) {
758 }
759 }
760 }
761
762 /* Special checks for onion service circuits. */
763 switch (victim->purpose) {
765 /* We only want to spare a rend circ iff it has been specified in an
766 * INTRODUCE1 cell sent to a hidden service. */
767 if (hs_circ_is_rend_sent_in_intro1(CONST_TO_ORIGIN_CIRCUIT(victim))) {
768 continue;
769 }
770 break;
772 /* The connection to the rendezvous point has timed out, close it and
773 * retry if possible. */
774 log_info(LD_CIRC,"Rendezvous circ %u (state %d:%s, purpose %d) "
775 "as timed-out, closing it. Relaunching rendezvous attempt.",
776 (unsigned)victim->n_circ_id,
777 victim->state, circuit_state_to_string(victim->state),
778 victim->purpose);
779 /* We'll close as a timeout the victim circuit. The rendezvous point
780 * won't keep both circuits, it only keeps the newest (for the same
781 * cookie). */
782 break;
783 default:
784 break;
785 }
786
787 if (victim->n_chan)
788 log_info(LD_CIRC,
789 "Abandoning circ %u %s:%u (state %d,%d:%s, purpose %d, "
790 "len %d)", TO_ORIGIN_CIRCUIT(victim)->global_identifier,
791 channel_describe_peer(victim->n_chan),
792 (unsigned)victim->n_circ_id,
793 TO_ORIGIN_CIRCUIT(victim)->has_opened,
794 victim->state, circuit_state_to_string(victim->state),
795 victim->purpose,
796 TO_ORIGIN_CIRCUIT(victim)->build_state ?
797 TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
798 -1);
799 else
800 log_info(LD_CIRC,
801 "Abandoning circ %u %u (state %d,%d:%s, purpose %d, len %d)",
802 TO_ORIGIN_CIRCUIT(victim)->global_identifier,
803 (unsigned)victim->n_circ_id,
804 TO_ORIGIN_CIRCUIT(victim)->has_opened,
805 victim->state,
806 circuit_state_to_string(victim->state), victim->purpose,
807 TO_ORIGIN_CIRCUIT(victim)->build_state ?
808 TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
809 -1);
810
812 tor_trace(TR_SUBSYS(circuit), TR_EV(timeout), TO_ORIGIN_CIRCUIT(victim));
813 if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
814 circuit_mark_for_close(victim, END_CIRC_REASON_MEASUREMENT_EXPIRED);
815 else
816 circuit_mark_for_close(victim, END_CIRC_REASON_TIMEOUT);
817
819 } SMARTLIST_FOREACH_END(victim);
820}
821
822/**
823 * Mark for close all circuits that start here, that were built through a
824 * guard we weren't sure if we wanted to use, and that have been waiting
825 * around for way too long.
826 */
827void
829{
831 origin_circuit_t *, circ) {
832 if (TO_CIRCUIT(circ)->marked_for_close)
833 continue;
834 if (circ->guard_state == NULL)
835 continue;
837 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NONE);
838 } SMARTLIST_FOREACH_END(circ);
839}
840
841/** For debugging #8387: track when we last called
842 * circuit_expire_old_circuits_clientside. */
844
845/**
846 * As a diagnostic for bug 8387, log information about how many one-hop
847 * circuits we have around that have been there for at least <b>age</b>
848 * seconds. Log a few of them. Ignores Single Onion Service intro, it is
849 * expected to be long-term one-hop circuits.
850 */
851void
853{
854#define MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG 10
855 time_t now = time(NULL);
856 time_t cutoff = now - age;
857 int n_found = 0;
858 smartlist_t *log_these = smartlist_new();
859 const or_options_t *options = get_options();
860
862 const origin_circuit_t *ocirc;
863 if (! CIRCUIT_IS_ORIGIN(circ))
864 continue;
865 if (circ->timestamp_created.tv_sec >= cutoff)
866 continue;
867 /* Single Onion Services deliberately make long term one-hop intro
868 * and rendezvous connections. Don't log the established ones. */
869 if (hs_service_allow_non_anonymous_connection(options) &&
870 (circ->purpose == CIRCUIT_PURPOSE_S_INTRO ||
871 circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED))
872 continue;
873 ocirc = CONST_TO_ORIGIN_CIRCUIT(circ);
874
875 if (ocirc->build_state && ocirc->build_state->onehop_tunnel) {
876 ++n_found;
877
878 if (smartlist_len(log_these) < MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG)
879 smartlist_add(log_these, (origin_circuit_t*) ocirc);
880 }
881 }
882 SMARTLIST_FOREACH_END(circ);
883
884 if (n_found == 0)
885 goto done;
886
887 log_notice(LD_HEARTBEAT,
888 "Diagnostic for issue 8387: Found %d one-hop circuits more "
889 "than %d seconds old! Logging %d...",
890 n_found, age, smartlist_len(log_these));
891
892 SMARTLIST_FOREACH_BEGIN(log_these, const origin_circuit_t *, ocirc) {
893 char created[ISO_TIME_LEN+1];
894 int stream_num;
895 const edge_connection_t *conn;
896 char *dirty = NULL;
897 const circuit_t *circ = TO_CIRCUIT(ocirc);
898
899 format_local_iso_time(created,
900 (time_t)circ->timestamp_created.tv_sec);
901
902 if (circ->timestamp_dirty) {
903 char dirty_since[ISO_TIME_LEN+1];
904 format_local_iso_time(dirty_since, circ->timestamp_dirty);
905
906 tor_asprintf(&dirty, "Dirty since %s (%ld seconds vs %ld-second cutoff)",
907 dirty_since, (long)(now - circ->timestamp_dirty),
908 (long) options->MaxCircuitDirtiness);
909 } else {
910 dirty = tor_strdup("Not marked dirty");
911 }
912
913 log_notice(LD_HEARTBEAT, " #%d created at %s. %s, %s. %s for close. "
914 "Package window: %d. "
915 "%s for new conns. %s.",
916 ocirc_sl_idx,
917 created,
920 circ->marked_for_close ? "Marked" : "Not marked",
921 circ->package_window,
922 ocirc->unusable_for_new_conns ? "Not usable" : "usable",
923 dirty);
924 tor_free(dirty);
925
926 stream_num = 0;
927 for (conn = ocirc->p_streams; conn; conn = conn->next_stream) {
928 const connection_t *c = TO_CONN(conn);
929 char stream_created[ISO_TIME_LEN+1];
930 if (++stream_num >= 5)
931 break;
932
933 format_local_iso_time(stream_created, c->timestamp_created);
934
935 log_notice(LD_HEARTBEAT, " Stream#%d created at %s. "
936 "%s conn in state %s. "
937 "It is %slinked and %sreading from a linked connection %p. "
938 "Package window %d. "
939 "%s for close (%s:%d). Hold-open is %sset. "
940 "Has %ssent RELAY_END. %s on circuit.",
941 stream_num,
942 stream_created,
945 c->linked ? "" : "not ",
946 c->reading_from_linked_conn ? "": "not",
947 c->linked_conn,
948 conn->package_window,
949 c->marked_for_close ? "Marked" : "Not marked",
952 c->hold_open_until_flushed ? "" : "not ",
953 conn->edge_has_sent_end ? "" : "not ",
954 connection_is_reading(c) ? "Not blocked" : "Blocked");
955 if (! c->linked_conn)
956 continue;
957
958 c = c->linked_conn;
959
960 log_notice(LD_HEARTBEAT, " Linked to %s connection in state %s "
961 "(Purpose %d). %s for close (%s:%d). Hold-open is %sset. ",
964 c->purpose,
965 c->marked_for_close ? "Marked" : "Not marked",
968 c->hold_open_until_flushed ? "" : "not ");
969 }
970 } SMARTLIST_FOREACH_END(ocirc);
971
972 log_notice(LD_HEARTBEAT, "It has been %ld seconds since I last called "
973 "circuit_expire_old_circuits_clientside().",
975
976 done:
977 smartlist_free(log_these);
978}
979
980/** Remove any elements in <b>needed_ports</b> that are handled by an
981 * open or in-progress circuit.
982 */
983void
985{
986 int i;
987 uint16_t *port;
988
989 for (i = 0; i < smartlist_len(needed_ports); ++i) {
990 port = smartlist_get(needed_ports, i);
991 tor_assert(*port);
992 if (circuit_stream_is_being_handled(NULL, *port,
994 log_debug(LD_CIRC,"Port %d is already being handled; removing.", *port);
995 smartlist_del(needed_ports, i--);
996 tor_free(port);
997 } else {
998 log_debug(LD_CIRC,"Port %d is not handled.", *port);
999 }
1000 }
1001}
1002
1003/** Return 1 if at least <b>min</b> general-purpose non-internal circuits
1004 * will have an acceptable exit node for exit stream <b>conn</b> if it
1005 * is defined, else for "*:port".
1006 * Else return 0.
1007 */
1008int
1010 uint16_t port, int min)
1011{
1012 const node_t *exitnode;
1013 int num=0;
1014 time_t now = time(NULL);
1015 int need_uptime = smartlist_contains_int_as_string(
1016 get_options()->LongLivedPorts,
1017 conn ? conn->socks_request->port : port);
1018
1020 if (CIRCUIT_IS_ORIGIN(circ) &&
1021 !circ->marked_for_close &&
1022 (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
1023 circ->purpose == CIRCUIT_PURPOSE_CONFLUX_LINKED) &&
1024 (!circ->timestamp_dirty ||
1025 circ->timestamp_dirty + get_options()->MaxCircuitDirtiness > now)) {
1026 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
1027 cpath_build_state_t *build_state = origin_circ->build_state;
1028 if (build_state->is_internal || build_state->onehop_tunnel)
1029 continue;
1030 if (origin_circ->unusable_for_new_conns)
1031 continue;
1032 if (origin_circ->isolation_values_set &&
1033 (conn == NULL ||
1034 !connection_edge_compatible_with_circuit(conn, origin_circ)))
1035 continue;
1036
1037 exitnode = build_state_get_exit_node(build_state);
1038 if (exitnode && (!need_uptime || build_state->need_uptime)) {
1039 int ok;
1040 if (conn) {
1041 ok = connection_ap_can_use_exit(conn, exitnode);
1042 } else {
1044 r = compare_tor_addr_to_node_policy(NULL, port, exitnode);
1046 }
1047 if (ok) {
1048 if (++num >= min)
1049 return 1;
1050 }
1051 }
1052 }
1053 }
1054 SMARTLIST_FOREACH_END(circ);
1055 return 0;
1056}
1057
1058/** Don't keep more than this many unused open circuits around. */
1059#define MAX_UNUSED_OPEN_CIRCUITS 14
1060
1061/* Return true if a circuit is available for use, meaning that it is open,
1062 * clean, usable for new multi-hop connections, and a general purpose origin
1063 * circuit.
1064 * Accept any kind of circuit, return false if the above conditions are not
1065 * met. */
1066STATIC int
1067circuit_is_available_for_use(const circuit_t *circ)
1068{
1069 const origin_circuit_t *origin_circ;
1070 cpath_build_state_t *build_state;
1071
1072 if (!CIRCUIT_IS_ORIGIN(circ))
1073 return 0; /* We first filter out only origin circuits before doing the
1074 following checks. */
1075 if (circ->marked_for_close)
1076 return 0; /* Don't mess with marked circs */
1077 if (circ->timestamp_dirty)
1078 return 0; /* Only count clean circs */
1079 if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL &&
1081 return 0; /* We only pay attention to general purpose circuits.
1082 General purpose circuits are always origin circuits. */
1083
1084 origin_circ = CONST_TO_ORIGIN_CIRCUIT(circ);
1085 if (origin_circ->unusable_for_new_conns)
1086 return 0;
1087
1088 build_state = origin_circ->build_state;
1089 if (build_state->onehop_tunnel)
1090 return 0;
1091
1092 return 1;
1093}
1094
1095/* Return true if we need any more exit circuits.
1096 * needs_uptime and needs_capacity are set only if we need more exit circuits.
1097 * Check if we know of a port that's been requested recently and no circuit
1098 * is currently available that can handle it. */
1099STATIC int
1100needs_exit_circuits(time_t now, int *needs_uptime, int *needs_capacity)
1101{
1102 return (!circuit_all_predicted_ports_handled(now, needs_uptime,
1103 needs_capacity) &&
1104 router_have_consensus_path() == CONSENSUS_PATH_EXIT);
1105}
1106
1107/* Hidden services need at least this many internal circuits */
1108#define SUFFICIENT_UPTIME_INTERNAL_HS_SERVERS 3
1109
1110/* Return true if we need any more hidden service server circuits.
1111 * HS servers only need an internal circuit. */
1112STATIC int
1113needs_hs_server_circuits(time_t now, int num_uptime_internal)
1114{
1116 /* No services, we don't need anything. */
1117 goto no_need;
1118 }
1119
1120 if (num_uptime_internal >= SUFFICIENT_UPTIME_INTERNAL_HS_SERVERS) {
1121 /* We have sufficient amount of internal circuit. */
1122 goto no_need;
1123 }
1124
1125 if (router_have_consensus_path() == CONSENSUS_PATH_UNKNOWN) {
1126 /* Consensus hasn't been checked or might be invalid so requesting
1127 * internal circuits is not wise. */
1128 goto no_need;
1129 }
1130
1131 /* At this point, we need a certain amount of circuits and we will most
1132 * likely use them for rendezvous so we note down the use of internal
1133 * circuit for our prediction for circuit needing uptime and capacity. */
1134 rep_hist_note_used_internal(now, 1, 1);
1135
1136 return 1;
1137 no_need:
1138 return 0;
1139}
1140
1141/* We need at least this many internal circuits for hidden service clients */
1142#define SUFFICIENT_INTERNAL_HS_CLIENTS 3
1143
1144/* We need at least this much uptime for internal circuits for hidden service
1145 * clients */
1146#define SUFFICIENT_UPTIME_INTERNAL_HS_CLIENTS 2
1147
1148/* Return true if we need any more hidden service client circuits.
1149 * HS clients only need an internal circuit. */
1150STATIC int
1151needs_hs_client_circuits(time_t now, int *needs_uptime, int *needs_capacity,
1152 int num_internal, int num_uptime_internal)
1153{
1154 int used_internal_recently = rep_hist_get_predicted_internal(now,
1155 needs_uptime,
1156 needs_capacity);
1157 int requires_uptime = num_uptime_internal <
1158 SUFFICIENT_UPTIME_INTERNAL_HS_CLIENTS &&
1159 needs_uptime;
1160
1161 return (used_internal_recently &&
1162 (requires_uptime || num_internal < SUFFICIENT_INTERNAL_HS_CLIENTS) &&
1163 router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN);
1164}
1165
1166/* This is how many circuits can be opened concurrently during the cbt learning
1167 * phase. This number cannot exceed the tor-wide MAX_UNUSED_OPEN_CIRCUITS. */
1168#define DFLT_CBT_UNUSED_OPEN_CIRCS (10)
1169#define MIN_CBT_UNUSED_OPEN_CIRCS 0
1170#define MAX_CBT_UNUSED_OPEN_CIRCS MAX_UNUSED_OPEN_CIRCUITS
1171
1172/* Return true if we need more circuits for a good build timeout.
1173 * XXXX make the assumption that build timeout streams should be
1174 * created whenever we can build internal circuits. */
1175STATIC int
1176needs_circuits_for_build(int num)
1177{
1178 if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN) {
1179 if (num < networkstatus_get_param(NULL, "cbtmaxopencircs",
1180 DFLT_CBT_UNUSED_OPEN_CIRCS,
1181 MIN_CBT_UNUSED_OPEN_CIRCS,
1182 MAX_CBT_UNUSED_OPEN_CIRCS) &&
1185 return 1;
1186 }
1187 }
1188 return 0;
1189}
1190
1191/** Determine how many circuits we have open that are clean,
1192 * Make sure it's enough for all the upcoming behaviors we predict we'll have.
1193 * But put an upper bound on the total number of circuits.
1194 */
1195static void
1197{
1198 int num=0, num_internal=0, num_uptime_internal=0;
1199 int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
1200 int port_needs_uptime=0, port_needs_capacity=1;
1201 time_t now = time(NULL);
1202 int flags = 0;
1203
1204 /* Attempt to launch predicted conflux circuits. This is outside the HS or
1205 * Exit preemptive circuit set. */
1207
1208 /* Count how many of each type of circuit we currently have. */
1210 if (!circuit_is_available_for_use(circ))
1211 continue;
1212
1213 num++;
1214
1216 if (build_state->is_internal)
1217 num_internal++;
1218 if (build_state->need_uptime && build_state->is_internal)
1219 num_uptime_internal++;
1220 }
1221 SMARTLIST_FOREACH_END(circ);
1222
1223 /* If that's enough, then stop now. */
1224 if (num >= MAX_UNUSED_OPEN_CIRCUITS)
1225 return;
1226
1227 if (needs_exit_circuits(now, &port_needs_uptime, &port_needs_capacity)) {
1228 if (port_needs_uptime)
1229 flags |= CIRCLAUNCH_NEED_UPTIME;
1230 if (port_needs_capacity)
1231 flags |= CIRCLAUNCH_NEED_CAPACITY;
1232
1233 log_info(LD_CIRC,
1234 "Have %d clean circs (%d internal), need another exit circ.",
1235 num, num_internal);
1236
1238 return;
1239 }
1240
1241 if (needs_hs_server_circuits(now, num_uptime_internal)) {
1244
1245 log_info(LD_CIRC,
1246 "Have %d clean circs (%d internal), need another internal "
1247 "circ for my hidden service.",
1248 num, num_internal);
1250 return;
1251 }
1252
1253 if (needs_hs_client_circuits(now, &hidserv_needs_uptime,
1254 &hidserv_needs_capacity,
1255 num_internal, num_uptime_internal))
1256 {
1257 if (hidserv_needs_uptime)
1258 flags |= CIRCLAUNCH_NEED_UPTIME;
1259 if (hidserv_needs_capacity)
1260 flags |= CIRCLAUNCH_NEED_CAPACITY;
1261 flags |= CIRCLAUNCH_IS_INTERNAL;
1262
1263 log_info(LD_CIRC,
1264 "Have %d clean circs (%d uptime-internal, %d internal), need"
1265 " another hidden service circ.",
1266 num, num_uptime_internal, num_internal);
1267
1268 /* Always launch vanguards purpose circuits for HS clients,
1269 * for vanguards-lite. This prevents us from cannibalizing
1270 * to build these circuits (and thus not use vanguards). */
1272 return;
1273 }
1274
1275 if (needs_circuits_for_build(num)) {
1277 /* if there are no exits in the consensus, make timeout
1278 * circuits internal */
1279 if (router_have_consensus_path() == CONSENSUS_PATH_INTERNAL)
1280 flags |= CIRCLAUNCH_IS_INTERNAL;
1281
1282 log_info(LD_CIRC,
1283 "Have %d clean circs need another buildtime test circ.", num);
1285 return;
1286 }
1287}
1288
1289/** Build a new test circuit every 5 minutes */
1290#define TESTING_CIRCUIT_INTERVAL 300
1291
1292/** This function is called once a second, if router_have_minimum_dir_info()
1293 * is true. Its job is to make sure all services we offer have enough circuits
1294 * available. Some services just want enough circuits for current tasks,
1295 * whereas others want a minimum set of idle circuits hanging around.
1296 */
1297void
1299{
1300 const or_options_t *options = get_options();
1301
1302 /* launch a new circ for any pending streams that need one
1303 * XXXX make the assumption that (some) AP streams (i.e. HS clients)
1304 * don't require an exit circuit, review in #13814.
1305 * This allows HSs to function in a consensus without exits. */
1306 if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN)
1308
1310
1311 if (!options->DisablePredictedCircuits)
1313}
1314
1315/**
1316 * Called once a second either directly or from
1317 * circuit_build_needed_circs(). As appropriate (once per NewCircuitPeriod)
1318 * resets failure counts and expires old circuits.
1319 */
1320void
1322{
1323 static time_t time_to_expire_and_reset = 0;
1324
1325 if (time_to_expire_and_reset < now) {
1327 time_to_expire_and_reset = now + get_options()->NewCircuitPeriod;
1328 if (proxy_mode(get_options()))
1329 addressmap_clean(now);
1331
1332#if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
1333
1334 /* If we ever re-enable, this has to move into
1335 * circuit_build_needed_circs */
1336
1337 circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
1338 if (get_options()->RunTesting &&
1339 circ &&
1340 circ->timestamp_began.tv_sec + TESTING_CIRCUIT_INTERVAL < now) {
1341 log_fn(LOG_INFO,"Creating a new testing circuit.");
1343 }
1344#endif /* 0 */
1345 }
1346}
1347
1348/** If the stream <b>conn</b> is a member of any of the linked
1349 * lists of <b>circ</b>, then remove it from the list.
1350 */
1351void
1353{
1354 edge_connection_t *prevconn;
1355
1356 tor_assert(circ);
1357 tor_assert(conn);
1358
1359 if (conn->base_.type == CONN_TYPE_AP) {
1360 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1361 entry_conn->may_use_optimistic_data = 0;
1362 }
1363 conn->cpath_layer = NULL; /* don't keep a stale pointer */
1364 conn->on_circuit = NULL;
1365
1366 if (CIRCUIT_IS_ORIGIN(circ)) {
1367 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
1368 int removed = 0;
1369 if (conn == origin_circ->p_streams) {
1370 origin_circ->p_streams = conn->next_stream;
1371 conflux_update_p_streams(origin_circ, conn->next_stream);
1372 removed = 1;
1373 } else {
1374 for (prevconn = origin_circ->p_streams;
1375 prevconn && prevconn->next_stream && prevconn->next_stream != conn;
1376 prevconn = prevconn->next_stream)
1377 ;
1378 if (prevconn && prevconn->next_stream) {
1379 prevconn->next_stream = conn->next_stream;
1380 removed = 1;
1381 }
1382 }
1383 if (removed) {
1384 log_debug(LD_APP, "Removing stream %d from circ %u",
1385 conn->stream_id, (unsigned)circ->n_circ_id);
1386
1387 /* If the stream was removed, and it was a rend stream, decrement the
1388 * number of streams on the circuit associated with the rend service.
1389 */
1391 hs_dec_rdv_stream_counter(origin_circ);
1392 }
1393
1394 /* If there are no more streams on this circ, tell circpad */
1395 if (!origin_circ->p_streams)
1397
1398 return;
1399 }
1400 } else {
1401 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1402 if (conn == or_circ->n_streams) {
1403 or_circ->n_streams = conn->next_stream;
1404 conflux_update_n_streams(or_circ, conn->next_stream);
1405 return;
1406 }
1407 if (conn == or_circ->resolving_streams) {
1408 or_circ->resolving_streams = conn->next_stream;
1410 return;
1411 }
1412
1413 for (prevconn = or_circ->n_streams;
1414 prevconn && prevconn->next_stream && prevconn->next_stream != conn;
1415 prevconn = prevconn->next_stream)
1416 ;
1417 if (prevconn && prevconn->next_stream) {
1418 prevconn->next_stream = conn->next_stream;
1419 return;
1420 }
1421
1422 for (prevconn = or_circ->resolving_streams;
1423 prevconn && prevconn->next_stream && prevconn->next_stream != conn;
1424 prevconn = prevconn->next_stream)
1425 ;
1426 if (prevconn && prevconn->next_stream) {
1427 prevconn->next_stream = conn->next_stream;
1428 return;
1429 }
1430 }
1431
1432 log_warn(LD_BUG,"Edge connection not in circuit's list.");
1433 /* Don't give an error here; it's harmless. */
1435}
1436
1437/** Find each circuit that has been unused for too long, or dirty
1438 * for too long and has no streams on it: mark it for close.
1439 */
1440STATIC void
1442{
1443 struct timeval cutoff, now;
1444
1445 tor_gettimeofday(&now);
1447
1449 if (circ->marked_for_close || !CIRCUIT_IS_ORIGIN(circ))
1450 continue;
1451
1452 cutoff = now;
1453 cutoff.tv_sec -= TO_ORIGIN_CIRCUIT(circ)->circuit_idle_timeout;
1454
1455 /* If the circuit has been dirty for too long, and there are no streams
1456 * on it, mark it for close.
1457 */
1458 if (circ->timestamp_dirty &&
1459 circ->timestamp_dirty + get_options()->MaxCircuitDirtiness <
1460 now.tv_sec &&
1462 !TO_ORIGIN_CIRCUIT(circ)->p_streams /* nothing attached */ ) {
1463 log_debug(LD_CIRC, "Closing n_circ_id %u (dirty %ld sec ago, "
1464 "purpose %d)",
1465 (unsigned)circ->n_circ_id,
1466 (long)(now.tv_sec - circ->timestamp_dirty),
1467 circ->purpose);
1468 /* Don't do this magic for testing circuits. Their death is governed
1469 * by circuit_expire_building */
1470 if (circ->purpose != CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
1471 tor_trace(TR_SUBSYS(circuit), TR_EV(idle_timeout),
1472 TO_ORIGIN_CIRCUIT(circ));
1473 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1474 }
1475 } else if (!circ->timestamp_dirty && circ->state == CIRCUIT_STATE_OPEN) {
1476 if (timercmp(&circ->timestamp_began, &cutoff, OP_LT)) {
1477 if (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
1478 circ->purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
1479 circ->purpose == CIRCUIT_PURPOSE_CONFLUX_UNLINKED ||
1480 circ->purpose == CIRCUIT_PURPOSE_CONFLUX_LINKED ||
1481 circ->purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
1482 circ->purpose == CIRCUIT_PURPOSE_HS_VANGUARDS ||
1483 circ->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT ||
1484 circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
1485 circ->purpose == CIRCUIT_PURPOSE_TESTING ||
1486 circ->purpose == CIRCUIT_PURPOSE_C_CIRCUIT_PADDING ||
1487 (circ->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
1488 circ->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) ||
1489 circ->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
1490 log_info(LD_CIRC,
1491 "Closing circuit %"PRIu32
1492 " that has been unused for %ld msec.",
1493 TO_ORIGIN_CIRCUIT(circ)->global_identifier,
1494 tv_mdiff(&circ->timestamp_began, &now));
1495 tor_trace(TR_SUBSYS(circuit), TR_EV(idle_timeout),
1496 TO_ORIGIN_CIRCUIT(circ));
1497 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1498 } else if (!TO_ORIGIN_CIRCUIT(circ)->is_ancient) {
1499 /* Server-side rend joined circuits can end up really old, because
1500 * they are reused by clients for longer than normal. The client
1501 * controls their lifespan. (They never become dirty, because
1502 * connection_exit_begin_conn() never marks anything as dirty.)
1503 * Similarly, server-side intro circuits last a long time. */
1504 if (circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED &&
1505 circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
1506 log_notice(LD_CIRC,
1507 "Ancient non-dirty circuit %d is still around after "
1508 "%ld milliseconds. Purpose: %d (%s)",
1509 TO_ORIGIN_CIRCUIT(circ)->global_identifier,
1510 tv_mdiff(&circ->timestamp_began, &now),
1511 circ->purpose,
1512 circuit_purpose_to_string(circ->purpose));
1513 TO_ORIGIN_CIRCUIT(circ)->is_ancient = 1;
1514 }
1515 }
1516 }
1517 }
1518 } SMARTLIST_FOREACH_END(circ);
1519}
1520
1521/** How long do we wait before killing circuits with the properties
1522 * described below?
1523 *
1524 * Probably we could choose a number here as low as 5 to 10 seconds,
1525 * since these circs are used for begindir, and a) generally you either
1526 * ask another begindir question right after or you don't for a long time,
1527 * b) clients at least through 0.2.1.x choose from the whole set of
1528 * directory mirrors at each choice, and c) re-establishing a one-hop
1529 * circuit via create-fast is a light operation assuming the TLS conn is
1530 * still there.
1531 *
1532 * I expect "b" to go away one day when we move to using directory
1533 * guards, but I think "a" and "c" are good enough reasons that a low
1534 * number is safe even then.
1535 */
1536#define IDLE_ONE_HOP_CIRC_TIMEOUT 60
1537
1538/** Find each non-origin circuit that has been unused for too long,
1539 * has no streams on it, came from a client, and ends here: mark it
1540 * for close.
1541 */
1542void
1544{
1545 or_circuit_t *or_circ;
1546 time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;
1547
1549 if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
1550 continue;
1551 or_circ = TO_OR_CIRCUIT(circ);
1552 /* If the circuit has been idle for too long, and there are no streams
1553 * on it, and it ends here, and it used a create_fast, mark it for close.
1554 *
1555 * Also if there is a rend_splice on it, it's a single onion service
1556 * circuit and we should not close it.
1557 */
1558 if (or_circ->p_chan && channel_is_client(or_circ->p_chan) &&
1559 !circ->n_chan &&
1560 !or_circ->n_streams && !or_circ->resolving_streams &&
1561 !or_circ->rend_splice &&
1562 channel_when_last_xmit(or_circ->p_chan) <= cutoff) {
1563 log_info(LD_CIRC, "Closing circ_id %u (empty %d secs ago)",
1564 (unsigned)or_circ->p_circ_id,
1565 (int)(now - channel_when_last_xmit(or_circ->p_chan)));
1566 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1567 }
1568 }
1569 SMARTLIST_FOREACH_END(circ);
1570}
1571
1572/** Number of testing circuits we want open before testing our bandwidth. */
1573#define NUM_PARALLEL_TESTING_CIRCS 4
1574
1575/** True iff we've ever had enough testing circuits open to test our
1576 * bandwidth. */
1578
1579/** Reset have_performed_bandwidth_test, so we'll start building
1580 * testing circuits again so we can exercise our bandwidth. */
1581void
1583{
1585}
1586
1587/** Return 1 if we've already exercised our bandwidth, or if we
1588 * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
1589 * established or on the way. Else return 0.
1590 */
1591int
1593{
1594 int num = 0;
1595
1597 return 1;
1598
1600 if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
1601 circ->purpose == CIRCUIT_PURPOSE_TESTING &&
1602 circ->state == CIRCUIT_STATE_OPEN)
1603 num++;
1604 }
1605 SMARTLIST_FOREACH_END(circ);
1606 return num >= NUM_PARALLEL_TESTING_CIRCS;
1607}
1608
1609/** A testing circuit has completed. Take whatever stats we want.
1610 * Noticing reachability is taken care of in onionskin_answer(),
1611 * so there's no need to record anything here. But if we still want
1612 * to do the bandwidth test, and we now have enough testing circuits
1613 * open, do it.
1614 */
1615static void
1617{
1620 /* either we've already done everything we want with testing circuits,
1621 * OR this IPv4 testing circuit became open due to a fluke, e.g. we picked
1622 * a last hop where we already had the connection open due to a
1623 * outgoing local circuit, OR this is an IPv6 self-test circuit, not
1624 * a bandwidth test circuit. */
1625 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_AT_ORIGIN);
1626 } else if (circuit_enough_testing_circs()) {
1629 } else {
1631 }
1632}
1633
1634/** A testing circuit has failed to build. Take whatever stats we want. */
1635static void
1637{
1638 const or_options_t *options = get_options();
1639 if (server_mode(options) &&
1640 router_all_orports_seem_reachable(options))
1641 return;
1642
1643 log_info(LD_GENERAL,
1644 "Our testing circuit (to see if your ORPort is reachable) "
1645 "has failed. I'll try again later.");
1646
1647 /* These aren't used yet. */
1648 (void)circ;
1649 (void)at_last_hop;
1650}
1651
1652/** The circuit <b>circ</b> has just become open. Take the next
1653 * step: for rendezvous circuits, we pass circ to the appropriate
1654 * function in rendclient or rendservice. For general circuits, we
1655 * call connection_ap_attach_pending, which looks for pending streams
1656 * that could use circ.
1657 */
1658void
1660{
1661 tor_trace(TR_SUBSYS(circuit), TR_EV(opened), circ);
1662 circuit_event_status(circ, CIRC_EVENT_BUILT, 0);
1663
1664 /* Remember that this circuit has finished building. Now if we start
1665 * it building again later (e.g. by extending it), we will know not
1666 * to consider its build time. */
1667 circ->has_opened = 1;
1668
1669 switch (TO_CIRCUIT(circ)->purpose) {
1672 /* Start building an intro circ if we don't have one yet. */
1674 /* This isn't a call to circuit_try_attaching_streams because a
1675 * circuit in _C_ESTABLISH_REND state isn't connected to its
1676 * hidden service yet, thus we can't attach streams to it yet,
1677 * thus circuit_try_attaching_streams would always clear the
1678 * circuit's isolation state. circuit_try_attaching_streams is
1679 * called later, when the rend circ enters _C_REND_JOINED
1680 * state. */
1681 break;
1684 break;
1687 break;
1690 break;
1693 /* Tell any AP connections that have been waiting for a new
1694 * circuit that one is ready. */
1696 break;
1698 /* at the service, waiting for introductions */
1700 break;
1702 /* at the service, connecting to rend point */
1704 break;
1707 break;
1708 /* default:
1709 * This won't happen in normal operation, but might happen if the
1710 * controller did it. Just let it slide. */
1711 }
1712}
1713
1714/** If the stream-isolation state of <b>circ</b> can be cleared, clear
1715 * it. Return non-zero iff <b>circ</b>'s isolation state was cleared. */
1716static int
1718{
1719 if (/* The circuit may have become non-open if it was cannibalized.*/
1720 circ->base_.state == CIRCUIT_STATE_OPEN &&
1721 /* If !isolation_values_set, there is nothing to clear. */
1722 circ->isolation_values_set &&
1723 /* It's not legal to clear a circuit's isolation info if it's ever had
1724 * streams attached */
1726 /* If we have any isolation information set on this circuit, and
1727 * we didn't manage to attach any streams to it, then we can
1728 * and should clear it and try again. */
1730 return 1;
1731 } else {
1732 return 0;
1733 }
1734}
1735
1736/** Called when a circuit becomes ready for streams to be attached to
1737 * it. */
1738void
1740{
1741 /* Attach streams to this circuit if we can. */
1743
1744 /* The call to circuit_try_clearing_isolation_state here will do
1745 * nothing and return 0 if we didn't attach any streams to circ
1746 * above. */
1748 /* Maybe *now* we can attach some streams to this circuit. */
1750 }
1751}
1752
1753/** Called whenever a circuit could not be successfully built.
1754 */
1755void
1757{
1758 channel_t *n_chan = NULL;
1759 /* we should examine circ and see if it failed because of
1760 * the last hop or an earlier hop. then use this info below.
1761 */
1762 int failed_at_last_hop = 0;
1763
1764 /* First, check to see if this was a path failure, rather than build
1765 * failure.
1766 *
1767 * Note that we deliberately use circuit_get_cpath_len() (and not
1768 * circuit_get_cpath_opened_len()) because we only want to ensure
1769 * that a full path is *chosen*. This is different than a full path
1770 * being *built*. We only want to count *build* failures below.
1771 *
1772 * Path selection failures can happen spuriously for a number
1773 * of reasons (such as aggressive/invalid user-specified path
1774 * restrictions in the torrc, insufficient microdescriptors, and
1775 * non-user reasons like exitpolicy issues), and so should not be
1776 * counted as failures below.
1777 */
1779 static ratelim_t pathfail_limit = RATELIM_INIT(3600);
1780 log_fn_ratelim(&pathfail_limit, LOG_NOTICE, LD_CIRC,
1781 "Our circuit %u (id: %" PRIu32 ") died due to an invalid "
1782 "selected path, purpose %s. This may be a torrc "
1783 "configuration issue, or a bug.",
1784 TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier,
1785 circuit_purpose_to_string(TO_CIRCUIT(circ)->purpose));
1786
1787 /* If the path failed on an RP, retry it. */
1788 if (TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
1790 HS_METRICS_ERR_RDV_PATH);
1792 }
1793
1794 /* In all other cases, just bail. The rest is just failure accounting
1795 * that we don't want to do */
1796 return;
1797 }
1798
1799 /* If the last hop isn't open, and the second-to-last is, we failed
1800 * at the last hop. */
1801 if (circ->cpath &&
1802 circ->cpath->prev->state != CPATH_STATE_OPEN &&
1803 circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
1804 failed_at_last_hop = 1;
1805 }
1806
1807 /* Check if we failed at first hop */
1808 if (circ->cpath &&
1809 circ->cpath->state != CPATH_STATE_OPEN &&
1810 ! circ->base_.received_destroy) {
1811 /* We failed at the first hop for some reason other than a DESTROY cell.
1812 * If there's an OR connection to blame, blame it. Also, avoid this relay
1813 * for a while, and fail any one-hop directory fetches destined for it. */
1814 const char *n_chan_ident = circ->cpath->extend_info->identity_digest;
1815 tor_assert(n_chan_ident);
1816 int already_marked = 0;
1817 if (circ->base_.n_chan) {
1818 n_chan = circ->base_.n_chan;
1819
1820 if (n_chan->is_bad_for_new_circs) {
1821 /* We only want to blame this router when a fresh healthy
1822 * connection fails. So don't mark this router as newly failed,
1823 * since maybe this was just an old circuit attempt that's
1824 * finally timing out now. Also, there's no need to blow away
1825 * circuits/streams/etc, since the failure of an unhealthy conn
1826 * doesn't tell us much about whether a healthy conn would
1827 * succeed. */
1828 already_marked = 1;
1829 }
1830 log_info(LD_OR,
1831 "Our circuit %u (id: %" PRIu32 ") failed to get a response "
1832 "from the first hop (%s). I'm going to try to rotate to a "
1833 "better connection.",
1834 TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier,
1835 channel_describe_peer(n_chan));
1836 n_chan->is_bad_for_new_circs = 1;
1837 } else {
1838 log_info(LD_OR,
1839 "Our circuit %u (id: %" PRIu32 ") died before the first hop "
1840 "with no connection",
1841 TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier);
1842 }
1843 if (!already_marked) {
1844 /*
1845 * If we have guard state (new guard API) and our path selection
1846 * code actually chose a full path, then blame the failure of this
1847 * circuit on the guard.
1848 */
1849 if (circ->guard_state)
1851 /* if there are any one-hop streams waiting on this circuit, fail
1852 * them now so they can retry elsewhere. */
1853 connection_ap_fail_onehop(n_chan_ident, circ->build_state);
1854 }
1855 }
1856
1857 switch (circ->base_.purpose) {
1861 /* If we never built the circuit, note it as a failure. */
1863 if (failed_at_last_hop) {
1864 /* Make sure any streams that demand our last hop as their exit
1865 * know that it's unlikely to happen. */
1867 }
1868 break;
1870 circuit_testing_failed(circ, failed_at_last_hop);
1871 break;
1873 /* at the service, waiting for introductions */
1874 if (circ->base_.state != CIRCUIT_STATE_OPEN) {
1876 }
1877 /* no need to care here, because the service will rebuild intro
1878 * points periodically. */
1879 break;
1881 /* at the client, connecting to intro point */
1882 /* Don't increment failure count, since the service may have picked
1883 * the introduction point maliciously */
1884 /* The client will pick a new intro point when this one dies, if
1885 * the stream in question still cares. No need to act here. */
1886 break;
1888 /* at the client, waiting for the service */
1890 /* the client will pick a new rend point when this one dies, if
1891 * the stream in question still cares. No need to act here. */
1892 break;
1894 /* at the service, connecting to rend point */
1895 /* Don't increment failure count, since the client may have picked
1896 * the rendezvous point maliciously */
1897 log_info(LD_REND,
1898 "Couldn't connect to the client's chosen rend point %s "
1899 "(%s hop failed).",
1901 failed_at_last_hop?"last":"non-last");
1902
1904 HS_METRICS_ERR_RDV_RP_CONN_FAILURE);
1906 break;
1907 /* default:
1908 * This won't happen in normal operation, but might happen if the
1909 * controller did it. Just let it slide. */
1910 }
1911}
1912
1913/** Number of consecutive failures so far; should only be touched by
1914 * circuit_launch_new and circuit_*_failure_count.
1915 */
1916static int n_circuit_failures = 0;
1917/** Before the last time we called circuit_reset_failure_count(), were
1918 * there a lot of failures? */
1920
1921/** Don't retry launching a new circuit if we try this many times with no
1922 * success. */
1923#define MAX_CIRCUIT_FAILURES 5
1924
1925/** Launch a new circuit; see circuit_launch_by_extend_info() for
1926 * details on arguments. */
1928circuit_launch(uint8_t purpose, int flags)
1929{
1930 return circuit_launch_by_extend_info(purpose, NULL, flags);
1931}
1932
1933/* Do we have enough descriptors to build paths?
1934 * If need_exit is true, return 1 if we can build exit paths.
1935 * (We need at least one Exit in the consensus to build exit paths.)
1936 * If need_exit is false, return 1 if we can build internal paths.
1937 */
1938static int
1939have_enough_path_info(int need_exit)
1940{
1941 if (need_exit)
1942 return router_have_consensus_path() == CONSENSUS_PATH_EXIT;
1943 else
1944 return router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN;
1945}
1946
1947/**
1948 * Tell us if a circuit is a hidden service circuit.
1949 */
1950int
1952{
1953 /* HS Vanguard purpose. */
1954 if (circuit_purpose_is_hs_vanguards(purpose)) {
1955 return 1;
1956 }
1957
1958 /* Client-side purpose */
1959 if (circuit_purpose_is_hs_client(purpose)) {
1960 return 1;
1961 }
1962
1963 /* Service-side purpose */
1964 if (circuit_purpose_is_hs_service(purpose)) {
1965 return 1;
1966 }
1967
1968 return 0;
1969}
1970
1971/** Return true iff the given circuit is an HS client circuit. */
1972bool
1973circuit_purpose_is_hs_client(const uint8_t purpose)
1974{
1975 return (purpose >= CIRCUIT_PURPOSE_C_HS_MIN_ &&
1976 purpose <= CIRCUIT_PURPOSE_C_HS_MAX_);
1977}
1978
1979/** Return true iff the given circuit is an HS service circuit. */
1980bool
1981circuit_purpose_is_hs_service(const uint8_t purpose)
1982{
1983 return (purpose >= CIRCUIT_PURPOSE_S_HS_MIN_ &&
1984 purpose <= CIRCUIT_PURPOSE_S_HS_MAX_);
1985}
1986
1987/** Return true iff the given circuit is an HS Vanguards circuit. */
1988bool
1990{
1991 return (purpose == CIRCUIT_PURPOSE_HS_VANGUARDS);
1992}
1993
1994/** Return true iff the given circuit is an HS v3 circuit. */
1995bool
1997{
1998 return (CIRCUIT_IS_ORIGIN(circ) &&
1999 (CONST_TO_ORIGIN_CIRCUIT(circ)->hs_ident != NULL));
2000}
2001
2002/**
2003 * Return true if this circuit purpose should use vanguards
2004 * or pinned Layer2 or Layer3 guards.
2005 *
2006 * This function takes both the circuit purpose and the
2007 * torrc options for pinned middles/vanguards into account
2008 * (ie: the circuit must be a hidden service circuit and
2009 * vanguards/pinned middles must be enabled for it to return
2010 * true).
2011 */
2012int
2014{
2015 /* All hidden service circuits use either vanguards or
2016 * vanguards-lite. */
2018 return 1;
2019
2020 /* Everything else is a normal circuit */
2021 return 0;
2022}
2023
2024/**
2025 * Return true for the set of conditions for which it is OK to use
2026 * a cannibalized circuit.
2027 *
2028 * Don't cannibalize for onehops, or certain purposes.
2029 */
2030static int
2032 int has_extend_info,
2033 int onehop_tunnel)
2034{
2035
2036 /* Do not try to cannibalize if this is a one hop circuit. */
2037 if (onehop_tunnel) {
2038 return 0;
2039 }
2040
2041 /* Don't try to cannibalize for general purpose circuits that do not
2042 * specify a custom exit. */
2043 if (purpose_to_build == CIRCUIT_PURPOSE_C_GENERAL && !has_extend_info) {
2044 return 0;
2045 }
2046
2047 /* Don't cannibalize for testing circuits. We want to see if they
2048 * complete normally. Also don't cannibalize for vanguard-purpose
2049 * circuits, since those are specially pre-built for later
2050 * cannibalization by the actual specific circuit types that need
2051 * vanguards.
2052 */
2053 if (purpose_to_build == CIRCUIT_PURPOSE_TESTING ||
2054 purpose_to_build == CIRCUIT_PURPOSE_HS_VANGUARDS) {
2055 return 0;
2056 }
2057
2058 /* The server-side intro circ is not cannibalized because it only
2059 * needs a 3 hop circuit. It is also long-lived, so it is more
2060 * important that it have lower latency than get built fast.
2061 */
2062 if (purpose_to_build == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
2063 return 0;
2064 }
2065
2066 /* Do not cannibalize for conflux circuits */
2067 if (purpose_to_build == CIRCUIT_PURPOSE_CONFLUX_UNLINKED) {
2068 return 0;
2069 }
2070
2071 return 1;
2072}
2073
2074/** Launch a new circuit with purpose <b>purpose</b> and exit node
2075 * <b>extend_info</b> (or NULL to select a random exit node).
2076 *
2077 * If flags contains:
2078 * - CIRCLAUNCH_ONEHOP_TUNNEL: the circuit will have only one hop;
2079 * - CIRCLAUNCH_NEED_UPTIME: choose routers with high uptime;
2080 * - CIRCLAUNCH_NEED_CAPACITY: choose routers with high bandwidth;
2081 * - CIRCLAUNCH_IS_IPV6_SELFTEST: the second-last hop must support IPv6
2082 * extends;
2083 * - CIRCLAUNCH_IS_INTERNAL: the last hop need not be an exit node;
2084 * - CIRCLAUNCH_IS_V3_RP: the last hop must support v3 onion service
2085 * rendezvous.
2086 *
2087 * Return the newly allocated circuit on success, or NULL on failure. */
2090 extend_info_t *extend_info,
2091 int flags)
2092{
2093 origin_circuit_t *circ;
2094 int onehop_tunnel = (flags & CIRCLAUNCH_ONEHOP_TUNNEL) != 0;
2095 int have_path = have_enough_path_info(! (flags & CIRCLAUNCH_IS_INTERNAL) );
2096
2097 /* Keep some stats about our attempts to launch HS rendezvous circuits */
2098 if (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
2100 }
2101
2102 if (!onehop_tunnel && (!router_have_minimum_dir_info() || !have_path)) {
2103 log_debug(LD_CIRC,"Haven't %s yet; canceling "
2104 "circuit launch.",
2106 "fetched enough directory info" :
2107 "received a consensus with exits");
2108 return NULL;
2109 }
2110
2111 /* If we can/should cannibalize another circuit to build this one,
2112 * then do so. */
2114 extend_info != NULL,
2115 onehop_tunnel)) {
2116 /* see if there are appropriate circs available to cannibalize. */
2117 /* XXX if we're planning to add a hop, perhaps we want to look for
2118 * internal circs rather than exit circs? -RD */
2119 circ = circuit_find_to_cannibalize(purpose, extend_info, flags);
2120 if (circ) {
2121 uint8_t old_purpose = circ->base_.purpose;
2122 struct timeval old_timestamp_began = circ->base_.timestamp_began;
2123
2124 log_info(LD_CIRC, "Cannibalizing circ %u (id: %" PRIu32 ") for "
2125 "purpose %d (%s)",
2126 TO_CIRCUIT(circ)->n_circ_id, circ->global_identifier, purpose,
2127 circuit_purpose_to_string(purpose));
2128
2129 if ((purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
2130 purpose == CIRCUIT_PURPOSE_C_INTRODUCING) &&
2132 /* Path bias: Cannibalized rends pre-emptively count as a
2133 * successfully built but unused closed circuit. We don't
2134 * wait until the extend (or the close) because the rend
2135 * point could be malicious.
2136 *
2137 * Same deal goes for client side introductions. Clients
2138 * can be manipulated to connect repeatedly to them
2139 * (especially web clients).
2140 *
2141 * If we decide to probe the initial portion of these circs,
2142 * (up to the adversary's final hop), we need to remove this,
2143 * or somehow mark the circuit with a special path state.
2144 */
2145
2146 /* This must be called before the purpose change */
2147 pathbias_check_close(circ, END_CIRC_REASON_FINISHED);
2148 }
2149
2150 circuit_change_purpose(TO_CIRCUIT(circ), purpose);
2151 /* Reset the start date of this circ, else expire_building
2152 * will see it and think it's been trying to build since it
2153 * began.
2154 *
2155 * Technically, the code should reset this when the
2156 * create cell is finally sent, but we're close enough
2157 * here. */
2158 tor_gettimeofday(&circ->base_.timestamp_began);
2159
2160 control_event_circuit_cannibalized(circ, old_purpose,
2161 &old_timestamp_began);
2162
2163 switch (purpose) {
2165 /* it's ready right now */
2166 break;
2173 /* need to add a new hop */
2174 tor_assert(extend_info);
2175 if (circuit_extend_to_new_exit(circ, extend_info) < 0)
2176 return NULL;
2177 break;
2178 default:
2179 log_warn(LD_BUG,
2180 "unexpected purpose %d when cannibalizing a circ.",
2181 purpose);
2183 return NULL;
2184 }
2185
2186 tor_trace(TR_SUBSYS(circuit), TR_EV(cannibalized), circ);
2187 return circ;
2188 }
2189 }
2190
2193 /* too many failed circs in a row. don't try. */
2194// log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
2195 return NULL;
2196 }
2197
2198 /* try a circ. if it fails, circuit_mark_for_close will increment
2199 * n_circuit_failures */
2200 return circuit_establish_circuit(purpose, extend_info, flags);
2201}
2202
2203/** Record another failure at opening a general circuit. When we have
2204 * too many, we'll stop trying for the remainder of this minute.
2205 */
2206static void
2208{
2210 log_debug(LD_CIRC,"n_circuit_failures now %d.",n_circuit_failures);
2211}
2212
2213/** Reset the failure count for opening general circuits. This means
2214 * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
2215 * stopping again.
2216 */
2217void
2219{
2222 else
2225}
2226
2227/** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
2228 * there isn't one, and there isn't one on the way, launch one and return
2229 * 0. If it will never work, return -1.
2230 *
2231 * Write the found or in-progress or launched circ into *circp.
2232 */
2233static int
2235 uint8_t desired_circuit_purpose,
2236 origin_circuit_t **circp)
2237{
2238 origin_circuit_t *circ;
2239 int check_exit_policy;
2240 int need_uptime, need_internal;
2241 int want_onehop;
2242 const or_options_t *options = get_options();
2243
2244 tor_assert(conn);
2245 tor_assert(circp);
2246 if (ENTRY_TO_CONN(conn)->state != AP_CONN_STATE_CIRCUIT_WAIT) {
2247 connection_t *c = ENTRY_TO_CONN(conn);
2248 log_err(LD_BUG, "Connection state mismatch: wanted "
2249 "AP_CONN_STATE_CIRCUIT_WAIT, but got %d (%s)",
2250 c->state, conn_state_to_string(c->type, c->state));
2251 }
2253
2254 /* Will the exit policy of the exit node apply to this stream? */
2255 check_exit_policy =
2257 !conn->use_begindir &&
2259
2260 /* Does this connection want a one-hop circuit? */
2261 want_onehop = conn->want_onehop;
2262
2263 /* Do we need a high-uptime circuit? */
2264 need_uptime = !conn->want_onehop && !conn->use_begindir &&
2266 conn->socks_request->port);
2267
2268 /* Do we need an "internal" circuit? */
2269 if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)
2270 need_internal = 1;
2271 else if (conn->use_begindir || conn->want_onehop)
2272 need_internal = 1;
2273 else
2274 need_internal = 0;
2275
2276 /* We now know what kind of circuit we need. See if there is an
2277 * open circuit that we can use for this stream */
2278 circ = circuit_get_best(conn, 1 /* Insist on open circuits */,
2279 desired_circuit_purpose,
2280 need_uptime, need_internal);
2281
2282 if (circ) {
2283 /* We got a circuit that will work for this stream! We can return it. */
2284 *circp = circ;
2285 return 1; /* we're happy */
2286 }
2287
2288 /* Okay, there's no circuit open that will work for this stream. Let's
2289 * see if there's an in-progress circuit or if we have to launch one */
2290
2291 /* Do we know enough directory info to build circuits at all? */
2292 int have_path = have_enough_path_info(!need_internal);
2293
2294 if (!want_onehop && (!router_have_minimum_dir_info() || !have_path)) {
2295 /* If we don't have enough directory information, we can't build
2296 * multihop circuits.
2297 */
2299 int severity = LOG_NOTICE;
2300 /* Retry some stuff that might help the connection work. */
2301 /* If we are configured with EntryNodes or UseBridges */
2302 if (entry_list_is_constrained(options)) {
2303 /* Retry all our guards / bridges.
2304 * guards_retry_optimistic() always returns true here. */
2305 int rv = guards_retry_optimistic(options);
2306 tor_assert_nonfatal_once(rv);
2307 log_fn(severity, LD_APP|LD_DIR,
2308 "Application request when we haven't %s. "
2309 "Optimistically trying known %s again.",
2311 "used client functionality lately" :
2312 "received a consensus with exits",
2313 options->UseBridges ? "bridges" : "entrynodes");
2314 } else {
2315 /* Getting directory documents doesn't help much if we have a limited
2316 * number of guards */
2317 tor_assert_nonfatal(!options->UseBridges);
2318 tor_assert_nonfatal(!options->EntryNodes);
2319 /* Retry our directory fetches, so we have a fresh set of guard info */
2320 log_fn(severity, LD_APP|LD_DIR,
2321 "Application request when we haven't %s. "
2322 "Optimistically trying directory fetches again.",
2324 "used client functionality lately" :
2325 "received a consensus with exits");
2327 }
2328 }
2329 /* Since we didn't have enough directory info, we can't attach now. The
2330 * stream will be dealt with when router_have_minimum_dir_info becomes 1,
2331 * or when all directory attempts fail and directory_all_unreachable()
2332 * kills it.
2333 */
2334 return 0;
2335 }
2336
2337 /* Check whether the exit policy of the chosen exit, or the exit policies
2338 * of _all_ nodes, would forbid this node. */
2339 if (check_exit_policy) {
2340 if (!conn->chosen_exit_name) {
2341 struct in_addr in;
2342 tor_addr_t addr, *addrp=NULL;
2343 if (tor_inet_aton(conn->socks_request->address, &in)) {
2344 tor_addr_from_in(&addr, &in);
2345 addrp = &addr;
2346 }
2348 conn->socks_request->port,
2349 need_uptime)) {
2350 log_notice(LD_APP,
2351 "No Tor server allows exit to %s:%d. Rejecting.",
2352 safe_str_client(conn->socks_request->address),
2353 conn->socks_request->port);
2354 return -1;
2355 }
2356 } else {
2357 /* XXXX Duplicates checks in connection_ap_handshake_attach_circuit:
2358 * refactor into a single function. */
2359 const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 0);
2360 int opt = conn->chosen_exit_optional;
2361 if (node && !connection_ap_can_use_exit(conn, node)) {
2362 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2363 "Requested exit point '%s' is excluded or "
2364 "would refuse request. %s.",
2365 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2366 if (opt) {
2367 conn->chosen_exit_optional = 0;
2369 /* Try again. */
2371 desired_circuit_purpose,
2372 circp);
2373 }
2374 return -1;
2375 }
2376 }
2377 }
2378
2379 /* Now, check whether there already a circuit on the way that could handle
2380 * this stream. This check matches the one above, but this time we
2381 * do not require that the circuit will work. */
2382 circ = circuit_get_best(conn, 0 /* don't insist on open circuits */,
2383 desired_circuit_purpose,
2384 need_uptime, need_internal);
2385 if (circ)
2386 log_debug(LD_CIRC, "one on the way!");
2387
2388 if (!circ) {
2389 /* No open or in-progress circuit could handle this stream! We
2390 * will have to launch one!
2391 */
2392
2393 /* The chosen exit node, if there is one. */
2394 extend_info_t *extend_info=NULL;
2395 const int n_pending = count_pending_general_client_circuits();
2396
2397 /* Do we have too many pending circuits? */
2398 if (n_pending >= options->MaxClientCircuitsPending) {
2399 static ratelim_t delay_limit = RATELIM_INIT(10*60);
2400 char *m;
2401 if ((m = rate_limit_log(&delay_limit, approx_time()))) {
2402 log_notice(LD_APP, "We'd like to launch a circuit to handle a "
2403 "connection, but we already have %d general-purpose client "
2404 "circuits pending. Waiting until some finish.%s",
2405 n_pending, m);
2406 tor_free(m);
2407 }
2408 return 0;
2409 }
2410
2411 /* If this is a hidden service trying to start an introduction point,
2412 * handle that case. */
2413 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
2414 const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
2415 /* need to pick an intro point */
2416 extend_info = hs_client_get_random_intro_from_edge(edge_conn);
2417 if (!extend_info) {
2418 log_info(LD_REND, "No intro points: re-fetching service descriptor.");
2419 hs_client_refetch_hsdesc(&edge_conn->hs_ident->identity_pk);
2421 return 0;
2422 }
2423 log_info(LD_REND,"Chose %s as intro point for service",
2424 extend_info_describe(extend_info));
2425 }
2426
2427 /* If we have specified a particular exit node for our
2428 * connection, then be sure to open a circuit to that exit node.
2429 */
2430 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2431 desired_circuit_purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
2432 desired_circuit_purpose == CIRCUIT_PURPOSE_C_HSDIR_GET) {
2433 if (conn->chosen_exit_name) {
2434 const node_t *r;
2435 int opt = conn->chosen_exit_optional;
2437 if (r && node_has_preferred_descriptor(r, conn->want_onehop ? 1 : 0)) {
2438 /* We might want to connect to an IPv6 bridge for loading
2439 descriptors so we use the preferred address rather than
2440 the primary. */
2441 extend_info = extend_info_from_node(r, conn->want_onehop ? 1 : 0,
2442 desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL);
2443 if (!extend_info) {
2444 log_warn(LD_CIRC,"Could not make a one-hop connection to %s. "
2445 "Discarding this circuit.", conn->chosen_exit_name);
2446 return -1;
2447 }
2448 } else { /* ! (r && node_has_preferred_descriptor(...)) */
2449 log_debug(LD_DIR, "considering %d, %s",
2450 want_onehop, conn->chosen_exit_name);
2451 if (want_onehop && conn->chosen_exit_name[0] == '$') {
2452 /* We're asking for a one-hop circuit to a router that
2453 * we don't have a routerinfo about. Make up an extend_info. */
2454 /* XXX prop220: we need to make chosen_exit_name able to
2455 * encode both key formats. This is not absolutely critical
2456 * since this is just for one-hop circuits, but we should
2457 * still get it done */
2458 char digest[DIGEST_LEN];
2459 char *hexdigest = conn->chosen_exit_name+1;
2460 tor_addr_t addr;
2461 if (strlen(hexdigest) < HEX_DIGEST_LEN ||
2463 hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN) {
2464 log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
2465 return -1;
2466 }
2467 if (tor_addr_parse(&addr, conn->socks_request->address) < 0) {
2468 log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
2470 return -1;
2471 }
2472 /* XXXX prop220 add a workaround for ed25519 ID below*/
2473 extend_info = extend_info_new(conn->chosen_exit_name+1,
2474 digest,
2475 NULL, /* Ed25519 ID */
2476 NULL, NULL, /* onion keys */
2477 &addr, conn->socks_request->port,
2478 NULL,
2479 false);
2480 } else { /* ! (want_onehop && conn->chosen_exit_name[0] == '$') */
2481 /* We will need an onion key for the router, and we
2482 * don't have one. Refuse or relax requirements. */
2483 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2484 "Requested exit point '%s' is not known. %s.",
2485 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2486 if (opt) {
2487 conn->chosen_exit_optional = 0;
2489 /* Try again with no requested exit */
2491 desired_circuit_purpose,
2492 circp);
2493 }
2494 return -1;
2495 }
2496 }
2497 }
2498 } /* Done checking for general circutis with chosen exits. */
2499
2500 /* What purpose do we need to launch this circuit with? */
2501 uint8_t new_circ_purpose;
2502 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
2503 new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
2504 else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
2505 new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
2506 else
2507 new_circ_purpose = desired_circuit_purpose;
2508
2509 /* Determine what kind of a circuit to launch, and actually launch it. */
2510 {
2511 int flags = CIRCLAUNCH_NEED_CAPACITY;
2512 if (want_onehop) flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
2513 if (need_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
2514 if (need_internal) flags |= CIRCLAUNCH_IS_INTERNAL;
2515
2516 /* If we are about to pick a v3 RP right now, make sure we pick a
2517 * rendezvous point that supports the v3 protocol! */
2518 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED &&
2519 new_circ_purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
2520 ENTRY_TO_EDGE_CONN(conn)->hs_ident) {
2521 flags |= CIRCLAUNCH_IS_V3_RP;
2522 log_info(LD_GENERAL, "Getting rendezvous circuit to v3 service!");
2523 }
2524
2525 circ = circuit_launch_by_extend_info(new_circ_purpose, extend_info,
2526 flags);
2527 }
2528
2529 extend_info_free(extend_info);
2530
2531 /* Now trigger things that need to happen when we launch circuits */
2532
2533 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2534 desired_circuit_purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
2535 desired_circuit_purpose == CIRCUIT_PURPOSE_S_HSDIR_POST) {
2536 /* We just caused a circuit to get built because of this stream.
2537 * If this stream has caused a _lot_ of circuits to be built, that's
2538 * a bad sign: we should tell the user. */
2539 if (conn->num_circuits_launched < NUM_CIRCUITS_LAUNCHED_THRESHOLD &&
2540 ++conn->num_circuits_launched == NUM_CIRCUITS_LAUNCHED_THRESHOLD)
2541 log_info(LD_CIRC, "The application request to %s:%d has launched "
2542 "%d circuits without finding one it likes.",
2544 conn->socks_request->port,
2545 conn->num_circuits_launched);
2546 } else {
2547 /* help predict this next time */
2548 rep_hist_note_used_internal(time(NULL), need_uptime, 1);
2549 if (circ) {
2550 const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
2551 if (edge_conn->hs_ident) {
2552 circ->hs_ident =
2553 hs_ident_circuit_new(&edge_conn->hs_ident->identity_pk);
2554 }
2555 if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
2557 return 0;
2558 }
2559 }
2560 if (circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
2561 circ->base_.state == CIRCUIT_STATE_OPEN)
2562 circuit_has_opened(circ);
2563 }
2564 }
2565 } /* endif (!circ) */
2566
2567 /* We either found a good circuit, or launched a new circuit, or failed to
2568 * do so. Report success, and delay. */
2569
2570 if (circ) {
2571 /* Mark the circuit with the isolation fields for this connection.
2572 * When the circuit arrives, we'll clear these flags: this is
2573 * just some internal bookkeeping to make sure that we have
2574 * launched enough circuits.
2575 */
2577 } else {
2578 log_info(LD_APP,
2579 "No safe circuit (purpose %d) ready for edge "
2580 "connection; delaying.",
2581 desired_circuit_purpose);
2582 }
2583 *circp = circ;
2584 return 0;
2585}
2586
2587/** Return true iff <b>crypt_path</b> is one of the crypt_paths for
2588 * <b>circ</b>.
2589 *
2590 * WARNING: This function only validates that the cpath is on the *current*
2591 * circuit, for internal consistency checking. For codepaths involving streams,
2592 * or cpaths or layer_hints that could be from a different circuit due to
2593 * conflux, use edge_uses_cpath() or conflux_validate_source_hop() instead.
2594 */
2595static int
2597{
2598 crypt_path_t *cpath, *cpath_next = NULL;
2599 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
2600 cpath_next = cpath->next;
2601 if (crypt_path == cpath)
2602 return 1;
2603 }
2604 return 0;
2605}
2606
2607/** Attach the AP stream <b>apconn</b> to circ's linked list of
2608 * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
2609 * hop in circ's cpath if <b>cpath</b> is NULL.
2610 */
2611static void
2613 crypt_path_t *cpath)
2614{
2615 const node_t *exitnode = NULL;
2616
2617 /* add it into the linked list of streams on this circuit */
2618 log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %u.",
2619 (unsigned)circ->base_.n_circ_id);
2620
2621 /* If this is the first stream on this circuit, tell circpad
2622 * that streams are attached */
2623 if (!circ->p_streams)
2625
2626 /* reset it, so we can measure circ timeouts */
2627 ENTRY_TO_CONN(apconn)->timestamp_last_read_allowed = time(NULL);
2628 ENTRY_TO_EDGE_CONN(apconn)->next_stream = circ->p_streams;
2629 ENTRY_TO_EDGE_CONN(apconn)->on_circuit = TO_CIRCUIT(circ);
2630 /* assert_connection_ok(conn, time(NULL)); */
2631 circ->p_streams = ENTRY_TO_EDGE_CONN(apconn);
2633
2635 /* We are attaching a stream to a rendezvous circuit. That means
2636 * that an attempt to connect to a hidden service just
2637 * succeeded. Tell rendclient.c. */
2639 }
2640
2641 if (cpath) { /* we were given one; use it */
2642 tor_assert(cpath_is_on_circuit(circ, cpath));
2643 } else {
2644 /* use the last hop in the circuit */
2645 tor_assert(circ->cpath);
2646 tor_assert(circ->cpath->prev);
2647 tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
2648 cpath = circ->cpath->prev;
2649 }
2650 ENTRY_TO_EDGE_CONN(apconn)->cpath_layer = cpath;
2651
2654
2655 /* Compute the exitnode if possible, for logging below */
2656 if (cpath->extend_info)
2657 exitnode = node_get_by_id(cpath->extend_info->identity_digest);
2658
2659 /* See if we can use optimistic data on this circuit */
2660 // TODO-329-PURPOSE: Can conflux use optimistic data? Does
2661 // anything use optimistic data? Does anything use this?
2662 if (circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2663 circ->base_.purpose == CIRCUIT_PURPOSE_C_HSDIR_GET ||
2664 circ->base_.purpose == CIRCUIT_PURPOSE_S_HSDIR_POST ||
2666 apconn->may_use_optimistic_data = 1;
2667 else
2668 apconn->may_use_optimistic_data = 0;
2669 log_info(LD_APP, "Looks like completed circuit to %s %s allow "
2670 "optimistic data for connection to %s",
2671 (circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL ||
2672 circ->base_.purpose == CIRCUIT_PURPOSE_CONTROLLER) ?
2673 /* node_describe() does the right thing if exitnode is NULL */
2674 safe_str_client(node_describe(exitnode)) :
2675 "hidden service",
2676 apconn->may_use_optimistic_data ? "does" : "doesn't",
2677 safe_str_client(apconn->socks_request->address));
2678}
2679
2680/** Return true iff <b>address</b> is matched by one of the entries in
2681 * TrackHostExits. */
2682int
2683hostname_in_track_host_exits(const or_options_t *options, const char *address)
2684{
2685 if (!options->TrackHostExits)
2686 return 0;
2687 SMARTLIST_FOREACH_BEGIN(options->TrackHostExits, const char *, cp) {
2688 if (cp[0] == '.') { /* match end */
2689 if (cp[1] == '\0' ||
2690 !strcasecmpend(address, cp) ||
2691 !strcasecmp(address, &cp[1]))
2692 return 1;
2693 } else if (strcasecmp(cp, address) == 0) {
2694 return 1;
2695 }
2696 } SMARTLIST_FOREACH_END(cp);
2697 return 0;
2698}
2699
2700/** If an exit wasn't explicitly specified for <b>conn</b>, consider saving
2701 * the exit that we *did* choose for use by future connections to
2702 * <b>conn</b>'s destination.
2703 */
2704static void
2706 const origin_circuit_t *circ)
2707{
2708 const or_options_t *options = get_options();
2709 char *new_address = NULL;
2710 char fp[HEX_DIGEST_LEN+1];
2711 uint64_t stream_id = 0;
2712
2713 if (BUG(!conn)) {
2714 return;
2715 }
2716
2717 stream_id = ENTRY_TO_CONN(conn)->global_identifier;
2718
2719 /* Search the addressmap for this conn's destination. */
2720 /* If they're not in the address map.. */
2721 if (!options->TrackHostExits ||
2723 options->TrackHostExitsExpire))
2724 return; /* nothing to track, or already mapped */
2725
2726 if (!hostname_in_track_host_exits(options, conn->socks_request->address) ||
2727 !circ->build_state->chosen_exit)
2728 return;
2729
2730 /* write down the fingerprint of the chosen exit, not the nickname,
2731 * because the chosen exit might not be named. */
2732 base16_encode(fp, sizeof(fp),
2734
2735 /* Add this exit/hostname pair to the addressmap. */
2736 tor_asprintf(&new_address, "%s.%s.exit",
2737 conn->socks_request->address, fp);
2738
2739 addressmap_register(conn->socks_request->address, new_address,
2740 time(NULL) + options->TrackHostExitsExpire,
2741 ADDRMAPSRC_TRACKEXIT, 0, 0, stream_id);
2742}
2743
2744/** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
2745 * begin or resolve cell as appropriate. Return values are as for
2746 * connection_ap_handshake_attach_circuit. The stream will exit from the hop
2747 * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
2748 * <b>cpath</b> is NULL. */
2749int
2751 origin_circuit_t *circ,
2752 crypt_path_t *cpath)
2753{
2754 connection_t *base_conn = ENTRY_TO_CONN(conn);
2755 tor_assert(conn);
2757 base_conn->state == AP_CONN_STATE_CONTROLLER_WAIT);
2759 tor_assert(circ);
2760 tor_assert(circ->base_.state == CIRCUIT_STATE_OPEN);
2761
2762 base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
2763
2764 if (!circ->base_.timestamp_dirty ||
2765 ((conn->entry_cfg.isolation_flags & ISO_SOCKSAUTH) &&
2766 (conn->entry_cfg.socks_iso_keep_alive) &&
2767 (conn->socks_request->usernamelen ||
2768 conn->socks_request->passwordlen))) {
2769 /* When stream isolation is in use and controlled by an application
2770 * we are willing to keep using the stream. */
2771 circ->base_.timestamp_dirty = approx_time();
2772 if (TO_CIRCUIT(circ)->conflux) {
2773 conflux_sync_circ_fields(TO_CIRCUIT(circ)->conflux, circ);
2774 }
2775 }
2776
2778
2779 /* Now, actually link the connection. */
2780 link_apconn_to_circ(conn, circ, cpath);
2781
2784 if (!conn->use_begindir) {
2785 consider_recording_trackhost(conn, circ);
2786 }
2788 return -1;
2789 } else {
2791 return -1;
2792 }
2793
2794 return 1;
2795}
2796
2797/**
2798 * Return an appropriate circuit purpose for non-rend streams.
2799 * We don't handle rends here because a rend stream triggers two
2800 * circuit builds with different purposes, so it is handled elsewhere.
2801 *
2802 * This function just figures out what type of hsdir activity this is,
2803 * and tells us. Everything else is general.
2804 */
2805static int
2807{
2808 const connection_t *base_conn = ENTRY_TO_CONN(conn);
2809 tor_assert_nonfatal(!connection_edge_is_rendezvous_stream(
2810 ENTRY_TO_EDGE_CONN(conn)));
2811
2812 if (base_conn->linked_conn &&
2813 base_conn->linked_conn->type == CONN_TYPE_DIR) {
2814 /* Set a custom purpose for hsdir activity */
2815 if (base_conn->linked_conn->purpose == DIR_PURPOSE_UPLOAD_HSDESC) {
2817 } else if (base_conn->linked_conn->purpose == DIR_PURPOSE_FETCH_HSDESC) {
2819 }
2820 }
2821
2822 /* All other purposes are general for now */
2824}
2825
2826/** Try to find a safe live circuit for stream <b>conn</b>. If we find one,
2827 * attach the stream, send appropriate cells, and return 1. Otherwise,
2828 * try to launch new circuit(s) for the stream. If we can launch
2829 * circuits, return 0. Otherwise, if we simply can't proceed with
2830 * this stream, return -1. (conn needs to die, and is maybe already marked).
2831 */
2832/* XXXX this function should mark for close whenever it returns -1;
2833 * its callers shouldn't have to worry about that. */
2834int
2836{
2837 connection_t *base_conn = ENTRY_TO_CONN(conn);
2838 int retval;
2839 int conn_age;
2840 int want_onehop;
2841
2842 tor_assert(conn);
2845 want_onehop = conn->want_onehop;
2846
2847 conn_age = (int)(time(NULL) - base_conn->timestamp_created);
2848
2849 /* Is this connection so old that we should give up on it? Don't timeout if
2850 * this is a connection to an HS with PoW enabled because it can take an
2851 * arbitrary amount of time. */
2852 if (conn_age >= get_options()->SocksTimeout && !conn->hs_with_pow_conn) {
2853 int severity = (tor_addr_is_null(&base_conn->addr) && !base_conn->port) ?
2855 log_fn(severity, LD_APP,
2856 "Tried for %d seconds to get a connection to %s:%d. Giving up.",
2857 conn_age, safe_str_client(conn->socks_request->address),
2858 conn->socks_request->port);
2859 return -1;
2860 }
2861
2862 /* We handle "general" (non-onion) connections much more straightforwardly.
2863 */
2865 /* we're a general conn */
2866 origin_circuit_t *circ=NULL;
2867
2868 /* Are we linked to a dir conn that aims to fetch a consensus?
2869 * We check here because the conn might no longer be needed. */
2870 if (base_conn->linked_conn &&
2871 base_conn->linked_conn->type == CONN_TYPE_DIR &&
2873
2874 /* Yes we are. Is there a consensus fetch farther along than us? */
2875 if (networkstatus_consensus_is_already_downloading(
2877 /* We're doing the "multiple consensus fetch attempts" game from
2878 * proposal 210, and we're late to the party. Just close this conn.
2879 * The circuit and TLS conn that we made will time out after a while
2880 * if nothing else wants to use them. */
2881 log_info(LD_DIR, "Closing extra consensus fetch (to %s) since one "
2882 "is already downloading.", base_conn->linked_conn->address);
2883 return -1;
2884 }
2885 }
2886
2887 /* If we have a chosen exit, we need to use a circuit that's
2888 * open to that exit. See what exit we meant, and whether we can use it.
2889 */
2890 if (conn->chosen_exit_name) {
2891 const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 0);
2892 int opt = conn->chosen_exit_optional;
2893 if (!node && !want_onehop) {
2894 /* We ran into this warning when trying to extend a circuit to a
2895 * hidden service directory for which we didn't have a router
2896 * descriptor. See flyspray task 767 for more details. We should
2897 * keep this in mind when deciding to use BEGIN_DIR cells for other
2898 * directory requests as well. -KL*/
2899 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2900 "Requested exit point '%s' is not known. %s.",
2901 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2902 if (opt) {
2903 /* If we are allowed to ignore the .exit request, do so */
2904 conn->chosen_exit_optional = 0;
2906 return 0;
2907 }
2908 return -1;
2909 }
2910 if (node && !connection_ap_can_use_exit(conn, node)) {
2911 log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
2912 "Requested exit point '%s' is excluded or "
2913 "would refuse request. %s.",
2914 conn->chosen_exit_name, opt ? "Trying others" : "Closing");
2915 if (opt) {
2916 /* If we are allowed to ignore the .exit request, do so */
2917 conn->chosen_exit_optional = 0;
2919 return 0;
2920 }
2921 return -1;
2922 }
2923 }
2924
2925 /* Find the circuit that we should use, if there is one. Otherwise
2926 * launch it
2927 */
2928 retval = circuit_get_open_circ_or_launch(conn,
2930 &circ);
2931
2932 if (retval < 1) {
2933 /* We were either told "-1" (complete failure) or 0 (circuit in
2934 * progress); we can't attach this stream yet. */
2935 return retval;
2936 }
2937
2938 log_debug(LD_APP|LD_CIRC,
2939 "Attaching apconn to circ %u (stream %d sec old).",
2940 (unsigned)circ->base_.n_circ_id, conn_age);
2941 /* print the circ's path, so clients can figure out which circs are
2942 * sucking. */
2944
2945 /* We have found a suitable circuit for our conn. Hurray. Do
2946 * the attachment. */
2947 return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
2948
2949 } else { /* we're a rendezvous conn */
2950 origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
2951
2952 tor_assert(!ENTRY_TO_EDGE_CONN(conn)->cpath_layer);
2953
2954 /* start by finding a rendezvous circuit for us */
2955
2957 conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
2958 if (retval < 0) return -1; /* failed */
2959
2960 if (retval > 0) {
2961 tor_assert(rendcirc);
2962 /* one is already established, attach */
2963 log_info(LD_REND,
2964 "rend joined circ %u (id: %" PRIu32 ") already here. "
2965 "Attaching. (stream %d sec old)",
2966 (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
2967 rendcirc->global_identifier, conn_age);
2968 /* Mark rendezvous circuits as 'newly dirty' every time you use
2969 * them, since the process of rebuilding a rendezvous circ is so
2970 * expensive. There is a tradeoff between linkability and
2971 * feasibility, at this point.
2972 */
2973 rendcirc->base_.timestamp_dirty = time(NULL);
2974
2975 /* We've also attempted to use them. If they fail, we need to
2976 * probe them for path bias */
2978
2979 link_apconn_to_circ(conn, rendcirc, NULL);
2981 return 0; /* already marked, let them fade away */
2982 return 1;
2983 }
2984
2985 /* At this point we need to re-check the state, since it's possible that
2986 * our call to circuit_get_open_circ_or_launch() changed the connection's
2987 * state from "CIRCUIT_WAIT" to "RENDDESC_WAIT" because we decided to
2988 * re-fetch the descriptor.
2989 */
2990 if (ENTRY_TO_CONN(conn)->state != AP_CONN_STATE_CIRCUIT_WAIT) {
2991 log_info(LD_REND, "This connection is no longer ready to attach; its "
2992 "state changed."
2993 "(We probably have to re-fetch its descriptor.)");
2994 return 0;
2995 }
2996
2997 if (rendcirc && (rendcirc->base_.purpose ==
2999 log_info(LD_REND,
3000 "pending-join circ %u (id: %" PRIu32 ") already here, with "
3001 "intro ack. Stalling. (stream %d sec old)",
3002 (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
3003 rendcirc->global_identifier, conn_age);
3004 return 0;
3005 }
3006
3007 /* it's on its way. find an intro circ. */
3009 conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
3010 if (retval < 0) return -1; /* failed */
3011
3012 if (rendcirc && introcirc) {
3013 /* Let's fill out the hs_ident fully as soon as possible, so that
3014 * unreachability counts can be updated properly even if circuits close
3015 * early. */
3016 tor_assert_nonfatal(!ed25519_public_key_is_zero(
3017 &introcirc->hs_ident->intro_auth_pk));
3019 &introcirc->hs_ident->intro_auth_pk);
3020 }
3021
3022 if (retval > 0) {
3023 /* one has already sent the intro. keep waiting. */
3024 tor_assert(introcirc);
3025 log_info(LD_REND, "Intro circ %u (id: %" PRIu32 ") present and "
3026 "awaiting ACK. Rend circuit %u (id: %" PRIu32 "). "
3027 "Stalling. (stream %d sec old)",
3028 (unsigned) TO_CIRCUIT(introcirc)->n_circ_id,
3029 introcirc->global_identifier,
3030 rendcirc ? (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id : 0,
3031 rendcirc ? rendcirc->global_identifier : 0,
3032 conn_age);
3033 return 0;
3034 }
3035
3036 /* now rendcirc and introcirc are each either undefined or not finished */
3037
3038 if (rendcirc && introcirc &&
3039 rendcirc->base_.purpose == CIRCUIT_PURPOSE_C_REND_READY) {
3040 log_info(LD_REND,
3041 "ready rend circ %u (id: %" PRIu32 ") already here. No"
3042 "intro-ack yet on intro %u (id: %" PRIu32 "). "
3043 "(stream %d sec old)",
3044 (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
3045 rendcirc->global_identifier,
3046 (unsigned) TO_CIRCUIT(introcirc)->n_circ_id,
3047 introcirc->global_identifier, conn_age);
3048
3049 tor_assert(introcirc->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
3050 if (introcirc->base_.state == CIRCUIT_STATE_OPEN) {
3051 int ret;
3052 log_info(LD_REND, "Found open intro circ %u (id: %" PRIu32 "). "
3053 "Rend circuit %u (id: %" PRIu32 "); Considering "
3054 "sending introduction. (stream %d sec old)",
3055 (unsigned) TO_CIRCUIT(introcirc)->n_circ_id,
3056 introcirc->global_identifier,
3057 (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id,
3058 rendcirc->global_identifier, conn_age);
3059 ret = hs_client_send_introduce1(introcirc, rendcirc);
3060 switch (ret) {
3061 case 0: /* success */
3062 rendcirc->base_.timestamp_dirty = time(NULL);
3063 introcirc->base_.timestamp_dirty = time(NULL);
3064
3065 pathbias_count_use_attempt(introcirc);
3067
3068 assert_circuit_ok(TO_CIRCUIT(rendcirc));
3069 assert_circuit_ok(TO_CIRCUIT(introcirc));
3070 return 0;
3071 case -1: /* transient error */
3072 return 0;
3073 case -2: /* permanent error */
3074 return -1;
3075 default: /* oops */
3077 return -1;
3078 }
3079 }
3080 }
3081
3082 log_info(LD_REND, "Intro %u (id: %" PRIu32 ") and rend circuit %u "
3083 "(id: %" PRIu32 ") circuits are not both ready. "
3084 "Stalling conn. (%d sec old)",
3085 introcirc ? (unsigned) TO_CIRCUIT(introcirc)->n_circ_id : 0,
3086 introcirc ? introcirc->global_identifier : 0,
3087 rendcirc ? (unsigned) TO_CIRCUIT(rendcirc)->n_circ_id : 0,
3088 rendcirc ? rendcirc->global_identifier : 0, conn_age);
3089 return 0;
3090 }
3091}
3092
3093/** Change <b>circ</b>'s purpose to <b>new_purpose</b>. */
3094void
3095circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
3096{
3097 uint8_t old_purpose;
3098 /* Don't allow an OR circ to become an origin circ or vice versa. */
3099 tor_assert(!!(CIRCUIT_IS_ORIGIN(circ)) ==
3100 !!(CIRCUIT_PURPOSE_IS_ORIGIN(new_purpose)));
3101
3102 if (circ->purpose == new_purpose) return;
3103
3104 /* If this is a conflux circuit, a purpose change means we have closed */
3105 if (CIRCUIT_IS_CONFLUX(circ)) {
3106 /* If we're not transitioning to the linked purpose, we're closed. */
3107 if (new_purpose != CIRCUIT_PURPOSE_CONFLUX_LINKED)
3109 }
3110
3111 if (CIRCUIT_IS_ORIGIN(circ)) {
3112 char old_purpose_desc[80] = "";
3113
3114 strncpy(old_purpose_desc, circuit_purpose_to_string(circ->purpose), 80-1);
3115 old_purpose_desc[80-1] = '\0';
3116
3117 log_debug(LD_CIRC,
3118 "changing purpose of origin circ %d "
3119 "from \"%s\" (%d) to \"%s\" (%d)",
3120 TO_ORIGIN_CIRCUIT(circ)->global_identifier,
3121 old_purpose_desc,
3122 circ->purpose,
3123 circuit_purpose_to_string(new_purpose),
3124 new_purpose);
3125
3126 /* Take specific actions if we are repurposing a hidden service circuit. */
3128 !circuit_purpose_is_hidden_service(new_purpose)) {
3130 }
3131 }
3132
3133 old_purpose = circ->purpose;
3134 circ->purpose = new_purpose;
3135 tor_trace(TR_SUBSYS(circuit), TR_EV(change_purpose), circ, old_purpose,
3136 new_purpose);
3137
3138 if (CIRCUIT_IS_ORIGIN(circ)) {
3140 old_purpose);
3141
3143 }
3144}
3145
3146/** Mark <b>circ</b> so that no more connections can be attached to it. */
3147void
3149{
3150 const or_options_t *options = get_options();
3151 tor_assert(circ);
3152
3153 /* XXXX This is a kludge; we're only keeping it around in case there's
3154 * something that doesn't check unusable_for_new_conns, and to avoid
3155 * deeper refactoring of our expiration logic. */
3156 if (! circ->base_.timestamp_dirty)
3157 circ->base_.timestamp_dirty = approx_time();
3158 if (options->MaxCircuitDirtiness >= circ->base_.timestamp_dirty)
3159 circ->base_.timestamp_dirty = 1; /* prevent underflow */
3160 else
3161 circ->base_.timestamp_dirty -= options->MaxCircuitDirtiness;
3162
3163 circ->unusable_for_new_conns = 1;
3164
3165 if (TO_CIRCUIT(circ)->conflux) {
3166 conflux_sync_circ_fields(TO_CIRCUIT(circ)->conflux, circ);
3167 }
3168}
3169
3170/**
3171 * Add relay_body_len and RELAY_PAYLOAD_SIZE-relay_body_len to
3172 * the valid delivered written fields and the overhead field,
3173 * respectively.
3174 */
3175void
3176circuit_sent_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
3177{
3178 if (!circ) return;
3179
3180 tor_assertf_nonfatal(relay_body_len <= RELAY_PAYLOAD_SIZE,
3181 "Wrong relay_body_len: %d (should be at most %d)",
3182 relay_body_len, RELAY_PAYLOAD_SIZE);
3183
3185 tor_add_u32_nowrap(circ->n_delivered_written_circ_bw, relay_body_len);
3187 tor_add_u32_nowrap(circ->n_overhead_written_circ_bw,
3188 RELAY_PAYLOAD_SIZE-relay_body_len);
3189}
3190
3191/**
3192 * Add relay_body_len and RELAY_PAYLOAD_SIZE-relay_body_len to
3193 * the valid delivered read field and the overhead field,
3194 * respectively.
3195 */
3196void
3197circuit_read_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
3198{
3199 if (!circ) return;
3200
3201 tor_assert_nonfatal(relay_body_len <= RELAY_PAYLOAD_SIZE);
3202
3204 tor_add_u32_nowrap(circ->n_delivered_read_circ_bw, relay_body_len);
3206 tor_add_u32_nowrap(circ->n_overhead_read_circ_bw,
3207 RELAY_PAYLOAD_SIZE-relay_body_len);
3208}
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1349
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
#define tor_addr_from_in(dest, in)
Definition: address.h:331
int addressmap_have_mapping(const char *address, int update_expiry)
Definition: addressmap.c:544
void addressmap_clean(time_t now)
Definition: addressmap.c:320
void addressmap_register(const char *address, char *new_address, time_t expires, addressmap_entry_source_t source, const int wildcard_addr, const int wildcard_new_addr, uint64_t stream_id)
Definition: addressmap.c:576
Header for addressmap.c.
time_t approx_time(void)
Definition: approx_time.c:32
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
int n_bits_set_u8(uint8_t v)
Definition: bits.c:72
Header file for circuitbuild.c.
time_t channel_when_last_xmit(channel_t *chan)
Definition: channel.c:3278
const char * channel_state_to_string(channel_state_t state)
Definition: channel.c:316
int channel_is_client(const channel_t *chan)
Definition: channel.c:2918
const char * channel_describe_peer(channel_t *chan)
Definition: channel.c:2840
Header file for channel.c.
void pathbias_count_use_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:626
int pathbias_check_close(origin_circuit_t *ocirc, int reason)
void pathbias_count_timeout(origin_circuit_t *circ)
int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
const char * build_state_get_exit_nickname(cpath_build_state_t *state)
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:357
int circuit_timeout_want_to_count_circ(const origin_circuit_t *circ)
Definition: circuitbuild.c:870
int circuit_all_predicted_ports_handled(time_t now, int *need_uptime, int *need_capacity)
origin_circuit_t * circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
Definition: circuitbuild.c:485
const node_t * build_state_get_exit_node(cpath_build_state_t *state)
Header file for circuitbuild.c.
int circuit_any_opened_circuits(void)
Definition: circuitlist.c:737
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:185
int circuit_get_cpath_len(origin_circuit_t *circ)
Definition: circuitlist.c:2035
void assert_circuit_ok(const circuit_t *c)
Definition: circuitlist.c:2809
const char * circuit_state_to_string(int state)
Definition: circuitlist.c:781
int circuit_event_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
Definition: circuitlist.c:518
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:173
origin_circuit_t * circuit_find_to_cannibalize(uint8_t purpose_to_produce, extend_info_t *info, int flags)
Definition: circuitlist.c:1893
const char * circuit_purpose_to_string(uint8_t purpose)
Definition: circuitlist.c:929
smartlist_t * circuit_get_global_origin_circuit_list(void)
Definition: circuitlist.c:722
smartlist_t * circuit_get_global_list(void)
Definition: circuitlist.c:713
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_S_CONNECT_REND
Definition: circuitlist.h:107
#define CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
Definition: circuitlist.h:93
#define CIRCUIT_PURPOSE_PATH_BIAS_TESTING
Definition: circuitlist.h:123
#define CIRCUIT_STATE_OPEN
Definition: circuitlist.h:32
#define CIRCUIT_PURPOSE_IS_ORIGIN(p)
Definition: circuitlist.h:147
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:88
#define CIRCUIT_PURPOSE_S_INTRO
Definition: circuitlist.h:104
#define CIRCUIT_PURPOSE_CONTROLLER
Definition: circuitlist.h:121
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:154
#define CIRCUIT_PURPOSE_C_CIRCUIT_PADDING
Definition: circuitlist.h:95
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:86
#define CIRCUIT_PURPOSE_TESTING
Definition: circuitlist.h:118
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
Definition: circuitlist.h:76
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:110
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:83
#define CIRCUIT_PURPOSE_S_HSDIR_POST
Definition: circuitlist.h:112
#define CIRCUIT_PURPOSE_C_HSDIR_GET
Definition: circuitlist.h:90
#define CIRCUIT_PURPOSE_C_INTRODUCING
Definition: circuitlist.h:73
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:101
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:81
#define CIRCUIT_PURPOSE_C_GENERAL
Definition: circuitlist.h:70
#define CIRCUIT_PURPOSE_COUNTS_TOWARDS_MAXPENDING(p)
Definition: circuitlist.h:167
#define CIRCUIT_PURPOSE_CONFLUX_UNLINKED
Definition: circuitlist.h:137
#define CIRCUIT_PURPOSE_HS_VANGUARDS
Definition: circuitlist.h:131
void circpad_machine_event_circ_purpose_changed(origin_circuit_t *circ)
void circpad_machine_event_circ_has_no_streams(origin_circuit_t *circ)
void circpad_machine_event_circ_has_streams(origin_circuit_t *circ)
Header file for circuitpadding.c.
int circuit_build_times_needs_circuits_now(const circuit_build_times_t *cbt)
circuit_build_times_t * get_circuit_build_times_mutable(void)
Definition: circuitstats.c:85
void circuit_build_times_set_timeout(circuit_build_times_t *cbt)
double get_circuit_build_timeout_ms(void)
Definition: circuitstats.c:101
void circuit_build_times_count_timeout(circuit_build_times_t *cbt, int did_onehop)
const circuit_build_times_t * get_circuit_build_times(void)
Definition: circuitstats.c:78
double get_circuit_build_close_time_ms(void)
Definition: circuitstats.c:93
void circuit_build_times_mark_circ_as_measurement_only(origin_circuit_t *circ)
Definition: circuitstats.c:637
int circuit_build_times_enough_to_compute(const circuit_build_times_t *cbt)
Definition: circuitstats.c:255
int circuit_build_times_count_close(circuit_build_times_t *cbt, int did_onehop, time_t start_time)
int circuit_build_times_disabled(const or_options_t *options)
Definition: circuitstats.c:117
Header file for circuitstats.c.
void circuit_build_failed(origin_circuit_t *circ)
Definition: circuituse.c:1756
void circuit_sent_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
Definition: circuituse.c:3176
void circuit_expire_old_circuits_serverside(time_t now)
Definition: circuituse.c:1543
bool circuit_is_hs_v3(const circuit_t *circ)
Definition: circuituse.c:1996
void circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
Definition: circuituse.c:1352
static int count_pending_general_client_circuits(void)
Definition: circuituse.c:385
static void circuit_testing_failed(origin_circuit_t *circ, int at_last_hop)
Definition: circuituse.c:1636
static void circuit_testing_opened(origin_circuit_t *circ)
Definition: circuituse.c:1616
static int circuit_matches_with_rend_stream(const edge_connection_t *edge_conn, const origin_circuit_t *origin_circ)
Definition: circuituse.c:87
static int circuit_try_clearing_isolation_state(origin_circuit_t *circ)
Definition: circuituse.c:1717
bool circuit_purpose_is_hs_service(const uint8_t purpose)
Definition: circuituse.c:1981
origin_circuit_t * circuit_launch(uint8_t purpose, int flags)
Definition: circuituse.c:1928
void circuit_read_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
Definition: circuituse.c:3197
void circuit_log_ancient_one_hop_circuits(int age)
Definition: circuituse.c:852
void circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
Definition: circuituse.c:3095
static int have_performed_bandwidth_test
Definition: circuituse.c:1577
void circuit_expire_waiting_for_better_guard(void)
Definition: circuituse.c:828
void circuit_expire_old_circs_as_needed(time_t now)
Definition: circuituse.c:1321
static void circuit_predict_and_launch_new(void)
Definition: circuituse.c:1196
int circuit_should_use_vanguards(uint8_t purpose)
Definition: circuituse.c:2013
void reset_bandwidth_test(void)
Definition: circuituse.c:1582
int hostname_in_track_host_exits(const or_options_t *options, const char *address)
Definition: circuituse.c:2683
int circuit_is_acceptable(const origin_circuit_t *origin_circ, const entry_connection_t *conn, int must_be_open, uint8_t purpose, int need_uptime, int need_internal, time_t now)
Definition: circuituse.c:107
static void consider_recording_trackhost(const entry_connection_t *conn, const origin_circuit_t *circ)
Definition: circuituse.c:2705
static int n_circuit_failures
Definition: circuituse.c:1916
#define MAX_CIRCUIT_FAILURES
Definition: circuituse.c:1923
void circuit_has_opened(origin_circuit_t *circ)
Definition: circuituse.c:1659
void circuit_expire_building(void)
Definition: circuituse.c:445
static int circuit_should_cannibalize_to_build(uint8_t purpose_to_build, int has_extend_info, int onehop_tunnel)
Definition: circuituse.c:2031
void circuit_build_needed_circs(time_t now)
Definition: circuituse.c:1298
static int did_circs_fail_last_period
Definition: circuituse.c:1919
bool circuit_purpose_is_hs_vanguards(const uint8_t purpose)
Definition: circuituse.c:1989
int connection_ap_handshake_attach_circuit(entry_connection_t *conn)
Definition: circuituse.c:2835
void circuit_reset_failure_count(int timeout)
Definition: circuituse.c:2218
#define IDLE_ONE_HOP_CIRC_TIMEOUT
Definition: circuituse.c:1536
STATIC void circuit_expire_old_circuits_clientside(void)
Definition: circuituse.c:1441
static int circuit_is_better(const origin_circuit_t *oa, const origin_circuit_t *ob, const entry_connection_t *conn)
Definition: circuituse.c:243
bool circuit_purpose_is_hs_client(const uint8_t purpose)
Definition: circuituse.c:1973
int connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
Definition: circuituse.c:2750
int circuit_stream_is_being_handled(entry_connection_t *conn, uint16_t port, int min)
Definition: circuituse.c:1009
static void circuit_increment_failure_count(void)
Definition: circuituse.c:2207
static int circuit_get_open_circ_or_launch(entry_connection_t *conn, uint8_t desired_circuit_purpose, origin_circuit_t **circp)
Definition: circuituse.c:2234
#define MAX_UNUSED_OPEN_CIRCUITS
Definition: circuituse.c:1059
void circuit_remove_handled_ports(smartlist_t *needed_ports)
Definition: circuituse.c:984
int circuit_enough_testing_circs(void)
Definition: circuituse.c:1592
static int connection_ap_get_nonrend_circ_purpose(const entry_connection_t *conn)
Definition: circuituse.c:2806
void mark_circuit_unusable_for_new_conns(origin_circuit_t *circ)
Definition: circuituse.c:3148
#define NUM_PARALLEL_TESTING_CIRCS
Definition: circuituse.c:1573
origin_circuit_t * circuit_launch_by_extend_info(uint8_t purpose, extend_info_t *extend_info, int flags)
Definition: circuituse.c:2089
#define TESTING_CIRCUIT_INTERVAL
Definition: circuituse.c:1290
static origin_circuit_t * circuit_get_best(const entry_connection_t *conn, int must_be_open, uint8_t purpose, int need_uptime, int need_internal)
Definition: circuituse.c:335
static time_t last_expired_clientside_circuits
Definition: circuituse.c:843
void circuit_try_attaching_streams(origin_circuit_t *circ)
Definition: circuituse.c:1739
static void link_apconn_to_circ(entry_connection_t *apconn, origin_circuit_t *circ, crypt_path_t *cpath)
Definition: circuituse.c:2612
static int cpath_is_on_circuit(origin_circuit_t *circ, crypt_path_t *crypt_path)
Definition: circuituse.c:2596
int circuit_purpose_is_hidden_service(uint8_t purpose)
Definition: circuituse.c:1951
Header file for circuituse.c.
#define CIRCLAUNCH_NEED_CAPACITY
Definition: circuituse.h:43
#define CIRCLAUNCH_ONEHOP_TUNNEL
Definition: circuituse.h:39
#define CIRCLAUNCH_IS_V3_RP
Definition: circuituse.h:49
#define CIRCLAUNCH_IS_INTERNAL
Definition: circuituse.h:46
#define CIRCLAUNCH_NEED_UPTIME
Definition: circuituse.h:41
#define MAX(a, b)
Definition: cmp.h:22
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1136
const or_options_t * get_options(void)
Definition: config.c:944
Header file for config.c.
Public APIs for conflux multipath support.
void conflux_circuit_has_opened(origin_circuit_t *orig_circ)
void conflux_circuit_has_closed(circuit_t *circ)
void conflux_predict_new(time_t now)
origin_circuit_t * conflux_get_circ_for_conn(const entry_connection_t *conn, time_t now)
Header file for conflux_pool.c.
void conflux_sync_circ_fields(conflux_t *cfx, origin_circuit_t *ref_circ)
Definition: conflux_util.c:295
void conflux_update_resolving_streams(or_circuit_t *circ, edge_connection_t *stream)
Definition: conflux_util.c:333
void conflux_update_p_streams(origin_circuit_t *circ, edge_connection_t *stream)
Definition: conflux_util.c:273
void conflux_update_n_streams(or_circuit_t *circ, edge_connection_t *stream)
Definition: conflux_util.c:317
Header file for conflux_util.c.
const char * conn_type_to_string(int type)
Definition: connection.c:270
connection_t * connection_get_by_type(int type)
Definition: connection.c:4920
const char * conn_state_to_string(int type, int state)
Definition: connection.c:304
Header file for connection.c.
#define CONN_TYPE_AP
Definition: connection.h:51
#define CONN_TYPE_DIR
Definition: connection.h:55
int connection_edge_compatible_with_circuit(const entry_connection_t *conn, const origin_circuit_t *circ)
void connection_ap_mark_as_waiting_for_renddesc(entry_connection_t *entry_conn)
void connection_ap_fail_onehop(const char *failed_digest, cpath_build_state_t *build_state)
void connection_ap_rescan_and_attach_pending(void)
int connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
bool connection_half_edges_waiting(const origin_circuit_t *circ)
void circuit_clear_isolation(origin_circuit_t *circ)
entry_connection_t * EDGE_TO_ENTRY_CONN(edge_connection_t *c)
int connection_ap_handshake_send_resolve(entry_connection_t *ap_conn)
int connection_edge_update_circuit_isolation(const entry_connection_t *conn, origin_circuit_t *circ, int dry_run)
int connection_ap_can_use_exit(const entry_connection_t *conn, const node_t *exit_node)
void connection_ap_attach_pending(int retry)
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
void circuit_discard_optional_exit_enclaves(extend_info_t *info)
Header file for connection_edge.c.
#define AP_CONN_STATE_CONTROLLER_WAIT
#define AP_CONN_STATE_CIRCUIT_WAIT
int control_event_circuit_purpose_changed(origin_circuit_t *circ, int old_purpose)
int control_event_circuit_cannibalized(origin_circuit_t *circ, int old_purpose, const struct timeval *old_tv_created)
Header file for control_events.c.
Circuit-build-stse structure.
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
void ed25519_pubkey_copy(ed25519_public_key_t *dest, const ed25519_public_key_t *src)
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
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 tor_memneq(a, b, sz)
Definition: di_ops.h:21
#define DIGEST_LEN
Definition: digest_sizes.h:20
Client/server directory connection structure.
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:88
Header file for directory.c.
#define DIR_PURPOSE_UPLOAD_HSDESC
Definition: directory.h:67
#define DIR_PURPOSE_FETCH_CONSENSUS
Definition: directory.h:54
#define DIR_PURPOSE_FETCH_HSDESC
Definition: directory.h:69
Entry connection structure.
#define ENTRY_TO_EDGE_CONN(c)
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2653
int entry_list_is_constrained(const or_options_t *options)
Definition: entrynodes.c:3511
int entry_guard_state_should_expire(circuit_guard_state_t *guard_state)
Definition: entrynodes.c:2921
int guards_retry_optimistic(const or_options_t *options)
Definition: entrynodes.c:3968
Header file for circuitbuild.c.
const char * escaped(const char *s)
Definition: escape.c:126
Header file for Tor tracing instrumentation definition.
#define TR_SUBSYS(name)
Definition: events.h:45
Extend-info structure.
extend_info_t * extend_info_new(const char *nickname, const char *rsa_id_digest, const ed25519_public_key_t *ed_id, crypto_pk_t *onion_key, const curve25519_public_key_t *ntor_key, const tor_addr_t *addr, uint16_t port, const protover_summary_flags_t *pv, bool for_exit_use)
Definition: extendinfo.c:33
extend_info_t * extend_info_from_node(const node_t *node, int for_direct_connect, bool for_exit)
Definition: extendinfo.c:100
bool extend_info_has_orport(const extend_info_t *ei, const tor_addr_t *addr, uint16_t port)
Definition: extendinfo.c:265
Header for core/or/extendinfo.c.
Header for fp.c.
void hs_circ_cleanup_on_repurpose(circuit_t *circ)
Definition: hs_circuit.c:1652
bool hs_circ_is_rend_sent_in_intro1(const origin_circuit_t *circ)
Definition: hs_circuit.c:1685
void hs_circ_retry_service_rendezvous_point(const origin_circuit_t *circ)
Definition: hs_circuit.c:999
Header file containing circuit data for the whole HS subsystem.
void hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
Definition: hs_client.c:2130
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:2228
int hs_client_send_introduce1(origin_circuit_t *intro_circ, origin_circuit_t *rend_circ)
Definition: hs_client.c:2259
void hs_client_circuit_has_opened(origin_circuit_t *circ)
Definition: hs_client.c:2269
extend_info_t * hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
Definition: hs_client.c:2591
int hs_client_setup_intro_circ_auth_key(origin_circuit_t *circ)
Definition: hs_client.c:847
Header file containing client data for the HS subsystem.
void hs_dec_rdv_stream_counter(origin_circuit_t *circ)
Definition: hs_common.c:1722
Header file containing common data for the whole HS subsystem.
hs_ident_circuit_t * hs_ident_circuit_new(const ed25519_public_key_t *identity_pk)
Definition: hs_ident.c:16
Header file containing circuit and connection identifier data for the whole HS subsystem.
Header for feature/hs/hs_metrics.c.
#define hs_metrics_failed_rdv(i, reason)
Definition: hs_metrics.h:63
void hs_service_circuit_has_opened(origin_circuit_t *circ)
Definition: hs_service.c:4438
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:4229
void hs_stats_note_service_rendezvous_launch(void)
Definition: hs_stats.c:34
Header file for hs_stats.c.
int tor_inet_aton(const char *str, struct in_addr *addr)
Definition: inaddr.c:40
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_REND
Definition: log.h:84
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:288
#define LD_APP
Definition: log.h:78
#define LOG_DEBUG
Definition: log.h:42
#define LD_OR
Definition: log.h:92
#define LD_HEARTBEAT
Definition: log.h:103
#define LD_BUG
Definition: log.h:86
#define LD_GENERAL
Definition: log.h:62
#define LD_DIR
Definition: log.h:88
#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
int connection_is_reading(const connection_t *conn)
Definition: mainloop.c:500
Header file for mainloop.c.
#define tor_free(p)
Definition: malloc.h:56
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:226
const node_t * node_get_by_nickname(const char *nickname, unsigned flags)
Definition: nodelist.c:1085
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1509
int router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port, int need_uptime)
Definition: nodelist.c:2359
consensus_path_type_t router_have_consensus_path(void)
Definition: nodelist.c:2469
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2436
Header file for nodelist.c.
Master header file for Tor-specific functionality.
#define MIN_CIRCUITS_HANDLING_STREAM
Definition: or.h:180
@ ADDRMAPSRC_TRACKEXIT
Definition: or.h:929
#define TO_CIRCUIT(x)
Definition: or.h:848
#define RELAY_PAYLOAD_SIZE
Definition: or.h:494
#define ISO_SOCKSAUTH
Definition: or.h:861
#define END_CIRC_REASON_MEASUREMENT_EXPIRED
Definition: or.h:313
#define TO_CONN(c)
Definition: or.h:612
#define END_CIRC_AT_ORIGIN
Definition: or.h:318
#define ENTRY_TO_CONN(c)
Definition: or.h:615
Origin circuit structure.
@ PATH_STATE_USE_FAILED
@ PATH_STATE_BUILD_SUCCEEDED
addr_policy_result_t compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port, const smartlist_t *policy)
Definition: policies.c:1536
addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port, const node_t *node)
Definition: policies.c:2907
Header file for policies.c.
addr_policy_result_t
Definition: policies.h:38
@ ADDR_POLICY_PROBABLY_REJECTED
Definition: policies.h:48
@ ADDR_POLICY_REJECTED
Definition: policies.h:42
void rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
int rep_hist_get_predicted_internal(time_t now, int *need_uptime, int *need_capacity)
Header file for predict_ports.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int proxy_mode(const or_options_t *options)
Definition: proxymode.c:21
Header file for proxymode.c.
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:42
void routerlist_retry_directory_downloads(time_t now)
Definition: routerlist.c:2347
int hexdigest_to_digest(const char *hexdigest, char *digest)
Definition: routerlist.c:752
Header file for routerlist.c.
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:308
int router_orport_seems_reachable(const or_options_t *options, int family)
Definition: selftest.c:101
void router_do_reachability_checks(void)
Definition: selftest.c:292
void router_perform_bandwidth_test(int num_circs, time_t now)
Definition: selftest.c:425
Header file for selftest.c.
int smartlist_contains_int_as_string(const smartlist_t *sl, int num)
Definition: smartlist.c:147
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_del(smartlist_t *sl, int idx)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Client request structure.
#define SOCKS_COMMAND_CONNECT
unsigned int is_bad_for_new_circs
Definition: channel.h:419
uint8_t state
Definition: circuit_st.h:111
time_t timestamp_dirty
Definition: circuit_st.h:188
unsigned int received_destroy
Definition: circuit_st.h:105
uint16_t marked_for_close
Definition: circuit_st.h:190
uint8_t purpose
Definition: circuit_st.h:112
struct timeval timestamp_began
Definition: circuit_st.h:166
int package_window
Definition: circuit_st.h:117
struct timeval timestamp_created
Definition: circuit_st.h:169
channel_t * n_chan
Definition: circuit_st.h:70
circid_t n_circ_id
Definition: circuit_st.h:79
uint8_t state
Definition: connection_st.h:49
struct connection_t * linked_conn
unsigned int hold_open_until_flushed
Definition: connection_st.h:61
unsigned int reading_from_linked_conn
Definition: connection_st.h:81
unsigned int type
Definition: connection_st.h:50
unsigned int linked
Definition: connection_st.h:78
uint16_t marked_for_close
uint16_t port
const char * marked_for_close_file
unsigned int purpose
Definition: connection_st.h:51
time_t timestamp_created
tor_addr_t addr
extend_info_t * chosen_exit
struct crypt_path_t * prev
Definition: crypt_path_st.h:80
uint8_t state
Definition: crypt_path_st.h:73
struct crypt_path_t * next
Definition: crypt_path_st.h:77
extend_info_t * extend_info
Definition: crypt_path_st.h:66
struct crypt_path_t * cpath_layer
struct edge_connection_t * next_stream
unsigned int edge_has_sent_end
struct circuit_t * on_circuit
socks_request_t * socks_request
unsigned int num_circuits_launched
unsigned int chosen_exit_optional
unsigned int may_use_optimistic_data
unsigned int hs_with_pow_conn
unsigned int socks_iso_keep_alive
char identity_digest[DIGEST_LEN]
ed25519_public_key_t intro_auth_pk
Definition: hs_ident.h:51
ed25519_public_key_t identity_pk
Definition: hs_ident.h:45
ed25519_public_key_t identity_pk
Definition: hs_ident.h:106
Definition: node_st.h:34
edge_connection_t * resolving_streams
Definition: or_circuit_st.h:50
channel_t * p_chan
Definition: or_circuit_st.h:37
circid_t p_circ_id
Definition: or_circuit_st.h:33
struct or_circuit_t * rend_splice
Definition: or_circuit_st.h:58
edge_connection_t * n_streams
Definition: or_circuit_st.h:43
int TrackHostExitsExpire
int MaxCircuitDirtiness
struct routerset_t * ExcludeExitNodes
struct smartlist_t * TrackHostExits
struct routerset_t * EntryNodes
struct routerset_t * ExcludeNodes
int MaxClientCircuitsPending
int DisablePredictedCircuits
struct smartlist_t * LongLivedPorts
unsigned int relaxed_timeout
struct hs_ident_circuit_t * hs_ident
edge_connection_t * p_streams
unsigned int isolation_values_set
uint32_t n_overhead_written_circ_bw
unsigned int has_opened
path_state_bitfield_t path_state
unsigned int is_ancient
uint32_t n_delivered_read_circ_bw
uint32_t n_delivered_written_circ_bw
uint32_t n_overhead_read_circ_bw
smartlist_t * prepend_policy
crypt_path_t * cpath
cpath_build_state_t * build_state
unsigned int isolation_any_streams_attached
struct circuit_guard_state_t * guard_state
char address[MAX_SOCKS_ADDR_LEN]
#define STATIC
Definition: testsupport.h:32
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:316
#define timercmp(tv1, tv2, op)
Definition: timeval.h:81
void tor_gettimeofday(struct timeval *timeval)
long tv_mdiff(const struct timeval *start, const struct timeval *end)
Definition: tvdiff.c:102
Header for tvdiff.c.
#define tor_assert(expr)
Definition: util_bug.h:103
#define tor_fragile_assert()
Definition: util_bug.h:278
int strcasecmpend(const char *s1, const char *s2)
Definition: util_string.c:266
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:98