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