Tor  0.4.3.0-alpha-dev
circuitlist.c
Go to the documentation of this file.
1 /* Copyright 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file circuitlist.c
9  *
10  * \brief Manage global structures that list and index circuits, and
11  * look up circuits within them.
12  *
13  * One of the most frequent operations in Tor occurs every time that
14  * a relay cell arrives on a channel. When that happens, we need to
15  * find which circuit it is associated with, based on the channel and the
16  * circuit ID in the relay cell.
17  *
18  * To handle that, we maintain a global list of circuits, and a hashtable
19  * mapping [channel,circID] pairs to circuits. Circuits are added to and
20  * removed from this mapping using circuit_set_p_circid_chan() and
21  * circuit_set_n_circid_chan(). To look up a circuit from this map, most
22  * callers should use circuit_get_by_circid_channel(), though
23  * circuit_get_by_circid_channel_even_if_marked() is appropriate under some
24  * circumstances.
25  *
26  * We also need to allow for the possibility that we have blocked use of a
27  * circuit ID (because we are waiting to send a DESTROY cell), but the
28  * circuit is not there any more. For that case, we allow placeholder
29  * entries in the table, using channel_mark_circid_unusable().
30  *
31  * To efficiently handle a channel that has just opened, we also maintain a
32  * list of the circuits waiting for channels, so we can attach them as
33  * needed without iterating through the whole list of circuits, using
34  * circuit_get_all_pending_on_channel().
35  *
36  * In this module, we also handle the list of circuits that have been
37  * marked for close elsewhere, and close them as needed. (We use this
38  * "mark now, close later" pattern here and elsewhere to avoid
39  * unpredictable recursion if we closed every circuit immediately upon
40  * realizing it needed to close.) See circuit_mark_for_close() for the
41  * mark function, and circuit_close_all_marked() for the close function.
42  *
43  * For hidden services, we need to be able to look up introduction point
44  * circuits and rendezvous circuits by cookie, key, etc. These are
45  * currently handled with linear searches in
46  * circuit_get_ready_rend_circuit_by_rend_data(),
47  * circuit_get_next_by_pk_and_purpose(), and with hash lookups in
48  * circuit_get_rendezvous() and circuit_get_intro_point().
49  *
50  * This module is also the entry point for our out-of-memory handler
51  * logic, which was originally circuit-focused.
52  **/
53 #define CIRCUITLIST_PRIVATE
54 #define OCIRC_EVENT_PRIVATE
55 #include "lib/cc/torint.h" /* TOR_PRIuSZ */
56 
57 #include "core/or/or.h"
58 #include "core/or/channel.h"
59 #include "core/or/channeltls.h"
60 #include "feature/client/circpathbias.h"
61 #include "core/or/circuitbuild.h"
62 #include "core/or/circuitlist.h"
63 #include "core/or/circuituse.h"
64 #include "core/or/circuitstats.h"
65 #include "core/or/circuitpadding.h"
66 #include "core/or/crypt_path.h"
68 #include "app/config/config.h"
70 #include "core/or/connection_or.h"
77 #include "core/mainloop/mainloop.h"
78 #include "feature/hs/hs_circuit.h"
80 #include "feature/hs/hs_ident.h"
85 #include "core/crypto/onion_fast.h"
86 #include "core/or/policies.h"
87 #include "core/or/relay.h"
92 #include "feature/stats/rephist.h"
95 #include "core/or/channelpadding.h"
96 #include "lib/compress/compress.h"
100 #include "lib/buf/buffers.h"
101 
102 #define OCIRC_EVENT_PRIVATE
103 #include "core/or/ocirc_event.h"
104 
105 #include "ht.h"
106 
111 #include "core/or/half_edge_st.h"
112 #include "core/or/extend_info_st.h"
113 #include "core/or/or_circuit_st.h"
115 
116 /********* START VARIABLES **********/
117 
118 /** A global list of all circuits at this hop. */
120 
121 /** A global list of all origin circuits. Every element of this is also
122  * an element of global_circuitlist. */
124 
125 /** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
127 
128 /** List of all the (origin) circuits whose state is
129  * CIRCUIT_STATE_GUARD_WAIT. */
131 
132 /** A list of all the circuits that have been marked with
133  * circuit_mark_for_close and which are waiting for circuit_about_to_free. */
135 
136 static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
137 static void circuit_about_to_free_atexit(circuit_t *circ);
138 static void circuit_about_to_free(circuit_t *circ);
139 
140 /**
141  * A cached value of the current state of the origin circuit list. Has the
142  * value 1 if we saw any opened circuits recently (since the last call to
143  * circuit_any_opened_circuits(), which gets called around once a second by
144  * circuit_expire_building). 0 otherwise.
145  */
147 
148 /********* END VARIABLES ************/
149 
150 /* Implement circuit handle helpers. */
151 HANDLE_IMPL(circuit, circuit_t,)
152 
153 or_circuit_t *
155 {
156  tor_assert(x->magic == OR_CIRCUIT_MAGIC);
157  return DOWNCAST(or_circuit_t, x);
158 }
159 const or_circuit_t *
160 CONST_TO_OR_CIRCUIT(const circuit_t *x)
161 {
163  return DOWNCAST(or_circuit_t, x);
164 }
167 {
169  return DOWNCAST(origin_circuit_t, x);
170 }
171 const origin_circuit_t *
172 CONST_TO_ORIGIN_CIRCUIT(const circuit_t *x)
173 {
175  return DOWNCAST(origin_circuit_t, x);
176 }
177 
178 /** A map from channel and circuit ID to circuit. (Lookup performance is
179  * very important here, since we need to do it every time a cell arrives.) */
181  HT_ENTRY(chan_circid_circuit_map_t) node;
182  channel_t *chan;
183  circid_t circ_id;
184  circuit_t *circuit;
185  /* For debugging 12184: when was this placeholder item added? */
186  time_t made_placeholder_at;
188 
189 /** Helper for hash tables: compare the channel and circuit ID for a and
190  * b, and return less than, equal to, or greater than zero appropriately.
191  */
192 static inline int
195 {
196  return a->chan == b->chan && a->circ_id == b->circ_id;
197 }
198 
199 /** Helper: return a hash based on circuit ID and the pointer value of
200  * chan in <b>a</b>. */
201 static inline unsigned int
203 {
204  /* Try to squeze the siphash input into 8 bytes to save any extra siphash
205  * rounds. This hash function is in the critical path. */
206  uintptr_t chan = (uintptr_t) (void*) a->chan;
207  uint32_t array[2];
208  array[0] = a->circ_id;
209  /* The low bits of the channel pointer are uninteresting, since the channel
210  * is a pretty big structure. */
211  array[1] = (uint32_t) (chan >> 6);
212  return (unsigned) siphash24g(array, sizeof(array));
213 }
214 
215 /** Map from [chan,circid] to circuit. */
216 static HT_HEAD(chan_circid_map, chan_circid_circuit_map_t)
217  chan_circid_map = HT_INITIALIZER();
218 HT_PROTOTYPE(chan_circid_map, chan_circid_circuit_map_t, node,
220 HT_GENERATE2(chan_circid_map, chan_circid_circuit_map_t, node,
223 
224 /** The most recently returned entry from circuit_get_by_circid_chan;
225  * used to improve performance when many cells arrive in a row from the
226  * same circuit.
227  */
228 static chan_circid_circuit_map_t *_last_circid_chan_ent = NULL;
229 
230 /** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
231  * and/or channel for circ has just changed from <b>old_chan, old_id</b>
232  * to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
233  * the old entry (if any) and adding a new one. */
234 static void
235 circuit_set_circid_chan_helper(circuit_t *circ, int direction,
236  circid_t id,
237  channel_t *chan)
238 {
241  channel_t *old_chan, **chan_ptr;
242  circid_t old_id, *circid_ptr;
243  int make_active, attached = 0;
244 
245  if (direction == CELL_DIRECTION_OUT) {
246  chan_ptr = &circ->n_chan;
247  circid_ptr = &circ->n_circ_id;
248  make_active = circ->n_chan_cells.n > 0;
249  } else {
250  or_circuit_t *c = TO_OR_CIRCUIT(circ);
251  chan_ptr = &c->p_chan;
252  circid_ptr = &c->p_circ_id;
253  make_active = c->p_chan_cells.n > 0;
254  }
255  old_chan = *chan_ptr;
256  old_id = *circid_ptr;
257 
258  if (id == old_id && chan == old_chan)
259  return;
260 
261  if (_last_circid_chan_ent &&
262  ((old_id == _last_circid_chan_ent->circ_id &&
263  old_chan == _last_circid_chan_ent->chan) ||
264  (id == _last_circid_chan_ent->circ_id &&
265  chan == _last_circid_chan_ent->chan))) {
266  _last_circid_chan_ent = NULL;
267  }
268 
269  if (old_chan) {
270  /*
271  * If we're changing channels or ID and had an old channel and a non
272  * zero old ID and weren't marked for close (i.e., we should have been
273  * attached), detach the circuit. ID changes require this because
274  * circuitmux hashes on (channel_id, circuit_id).
275  */
276  if (old_id != 0 && (old_chan != chan || old_id != id) &&
277  !(circ->marked_for_close)) {
278  tor_assert(old_chan->cmux);
279  circuitmux_detach_circuit(old_chan->cmux, circ);
280  }
281 
282  /* we may need to remove it from the conn-circid map */
283  search.circ_id = old_id;
284  search.chan = old_chan;
285  found = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
286  if (found) {
287  tor_free(found);
288  if (direction == CELL_DIRECTION_OUT) {
289  /* One fewer circuits use old_chan as n_chan */
290  --(old_chan->num_n_circuits);
291  } else {
292  /* One fewer circuits use old_chan as p_chan */
293  --(old_chan->num_p_circuits);
294  }
295  }
296  }
297 
298  /* Change the values only after we have possibly made the circuit inactive
299  * on the previous chan. */
300  *chan_ptr = chan;
301  *circid_ptr = id;
302 
303  if (chan == NULL)
304  return;
305 
306  /* now add the new one to the conn-circid map */
307  search.circ_id = id;
308  search.chan = chan;
309  found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
310  if (found) {
311  found->circuit = circ;
312  found->made_placeholder_at = 0;
313  } else {
314  found = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
315  found->circ_id = id;
316  found->chan = chan;
317  found->circuit = circ;
318  HT_INSERT(chan_circid_map, &chan_circid_map, found);
319  }
320 
321  /*
322  * Attach to the circuitmux if we're changing channels or IDs and
323  * have a new channel and ID to use and the circuit is not marked for
324  * close.
325  */
326  if (chan && id != 0 && (old_chan != chan || old_id != id) &&
327  !(circ->marked_for_close)) {
328  tor_assert(chan->cmux);
329  circuitmux_attach_circuit(chan->cmux, circ, direction);
330  attached = 1;
331  }
332 
333  /*
334  * This is a no-op if we have no cells, but if we do it marks us active to
335  * the circuitmux
336  */
337  if (make_active && attached)
338  update_circuit_on_cmux(circ, direction);
339 
340  /* Adjust circuit counts on new channel */
341  if (direction == CELL_DIRECTION_OUT) {
342  ++chan->num_n_circuits;
343  } else {
344  ++chan->num_p_circuits;
345  }
346 }
347 
348 /** Mark that circuit id <b>id</b> shouldn't be used on channel <b>chan</b>,
349  * even if there is no circuit on the channel. We use this to keep the
350  * circuit id from getting re-used while we have queued but not yet sent
351  * a destroy cell. */
352 void
354 {
357 
358  /* See if there's an entry there. That wouldn't be good. */
359  memset(&search, 0, sizeof(search));
360  search.chan = chan;
361  search.circ_id = id;
362  ent = HT_FIND(chan_circid_map, &chan_circid_map, &search);
363 
364  if (ent && ent->circuit) {
365  /* we have a problem. */
366  log_warn(LD_BUG, "Tried to mark %u unusable on %p, but there was already "
367  "a circuit there.", (unsigned)id, chan);
368  } else if (ent) {
369  /* It's already marked. */
370  if (!ent->made_placeholder_at)
371  ent->made_placeholder_at = approx_time();
372  } else {
373  ent = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
374  ent->chan = chan;
375  ent->circ_id = id;
376  /* leave circuit at NULL. */
377  ent->made_placeholder_at = approx_time();
378  HT_INSERT(chan_circid_map, &chan_circid_map, ent);
379  }
380 }
381 
382 /** Mark that a circuit id <b>id</b> can be used again on <b>chan</b>.
383  * We use this to re-enable the circuit ID after we've sent a destroy cell.
384  */
385 void
387 {
390 
391  /* See if there's an entry there. That wouldn't be good. */
392  memset(&search, 0, sizeof(search));
393  search.chan = chan;
394  search.circ_id = id;
395  ent = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
396  if (ent && ent->circuit) {
397  log_warn(LD_BUG, "Tried to mark %u usable on %p, but there was already "
398  "a circuit there.", (unsigned)id, chan);
399  return;
400  }
401  if (_last_circid_chan_ent == ent)
402  _last_circid_chan_ent = NULL;
403  tor_free(ent);
404 }
405 
406 /** Called to indicate that a DESTROY is pending on <b>chan</b> with
407  * circuit ID <b>id</b>, but hasn't been sent yet. */
408 void
410 {
412  if (circ) {
413  if (circ->n_chan == chan && circ->n_circ_id == id) {
414  circ->n_delete_pending = 1;
415  } else {
416  or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
417  if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
418  circ->p_delete_pending = 1;
419  }
420  }
421  return;
422  }
424 }
425 
426 /** Called to indicate that a DESTROY is no longer pending on <b>chan</b> with
427  * circuit ID <b>id</b> -- typically, because it has been sent. */
428 MOCK_IMPL(void,
430 {
432  if (circ) {
433  if (circ->n_chan == chan && circ->n_circ_id == id) {
434  circ->n_delete_pending = 0;
435  } else {
436  or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
437  if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
438  circ->p_delete_pending = 0;
439  }
440  }
441  /* XXXX this shouldn't happen; log a bug here. */
442  return;
443  }
444  channel_mark_circid_usable(chan, id);
445 }
446 
447 /** Set the p_conn field of a circuit <b>circ</b>, along
448  * with the corresponding circuit ID, and add the circuit as appropriate
449  * to the (chan,id)->circuit map. */
450 void
452  channel_t *chan)
453 {
454  circuit_t *circ = TO_CIRCUIT(or_circ);
455  channel_t *old_chan = or_circ->p_chan;
456  circid_t old_id = or_circ->p_circ_id;
457 
458  circuit_set_circid_chan_helper(circ, CELL_DIRECTION_IN, id, chan);
459 
460  if (chan) {
461  chan->timestamp_last_had_circuits = approx_time();
462  }
463 
464  if (circ->p_delete_pending && old_chan) {
465  channel_mark_circid_unusable(old_chan, old_id);
466  circ->p_delete_pending = 0;
467  }
468 }
469 
470 /** Set the n_conn field of a circuit <b>circ</b>, along
471  * with the corresponding circuit ID, and add the circuit as appropriate
472  * to the (chan,id)->circuit map. */
473 void
475  channel_t *chan)
476 {
477  channel_t *old_chan = circ->n_chan;
478  circid_t old_id = circ->n_circ_id;
479 
480  circuit_set_circid_chan_helper(circ, CELL_DIRECTION_OUT, id, chan);
481 
482  if (chan) {
483  chan->timestamp_last_had_circuits = approx_time();
484  }
485 
486  if (circ->n_delete_pending && old_chan) {
487  channel_mark_circid_unusable(old_chan, old_id);
488  circ->n_delete_pending = 0;
489  }
490 }
491 
492 /**
493  * Helper function to publish a message about events on an origin circuit
494  *
495  * Publishes a message to subscribers of origin circuit events, and
496  * sends the control event.
497  **/
498 int
500  int reason_code)
501 {
502  ocirc_cevent_msg_t *msg = tor_malloc(sizeof(*msg));
503 
504  tor_assert(circ);
505 
506  msg->gid = circ->global_identifier;
507  msg->evtype = tp;
508  msg->reason = reason_code;
509  msg->onehop = circ->build_state->onehop_tunnel;
510 
511  ocirc_cevent_publish(msg);
512  return control_event_circuit_status(circ, tp, reason_code);
513 }
514 
515 /**
516  * Helper function to publish a state change message
517  *
518  * circuit_set_state() calls this to notify subscribers about a change
519  * of the state of an origin circuit. @a circ must be an origin
520  * circuit.
521  **/
522 static void
524 {
525  ocirc_state_msg_t *msg = tor_malloc(sizeof(*msg));
526  const origin_circuit_t *ocirc;
527 
529  ocirc = CONST_TO_ORIGIN_CIRCUIT(circ);
530  /* Only inbound OR circuits can be in this state, not origin circuits. */
532 
533  msg->gid = ocirc->global_identifier;
534  msg->state = circ->state;
535  msg->onehop = ocirc->build_state->onehop_tunnel;
536 
537  ocirc_state_publish(msg);
538 }
539 
540 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
541  * it from lists as appropriate. */
542 void
543 circuit_set_state(circuit_t *circ, uint8_t state)
544 {
545  tor_assert(circ);
546  if (state == circ->state)
547  return;
548  if (PREDICT_UNLIKELY(!circuits_pending_chans))
550  if (PREDICT_UNLIKELY(!circuits_pending_other_guards))
552  if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
553  /* remove from waiting-circuit list. */
555  }
556  if (state == CIRCUIT_STATE_CHAN_WAIT) {
557  /* add to waiting-circuit list. */
559  }
560  if (circ->state == CIRCUIT_STATE_GUARD_WAIT) {
562  }
563  if (state == CIRCUIT_STATE_GUARD_WAIT) {
565  }
566  if (state == CIRCUIT_STATE_GUARD_WAIT || state == CIRCUIT_STATE_OPEN)
568  circ->state = state;
569  if (CIRCUIT_IS_ORIGIN(circ))
570  circuit_state_publish(circ);
571 }
572 
573 /** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
574  * the given connection. */
575 void
577 {
578  tor_assert(out);
579  tor_assert(chan);
580 
582  return;
583 
585  if (circ->marked_for_close)
586  continue;
587  if (!circ->n_hop)
588  continue;
591  /* Look at addr/port. This is an unkeyed connection. */
592  if (!channel_matches_extend_info(chan, circ->n_hop))
593  continue;
594  } else {
595  /* We expected a key. See if it's the right one. */
596  if (tor_memneq(chan->identity_digest,
598  continue;
599  }
600  smartlist_add(out, circ);
601  } SMARTLIST_FOREACH_END(circ);
602 }
603 
604 /** Return the number of circuits in state CHAN_WAIT, waiting for the given
605  * channel. */
606 int
608 {
609  int cnt;
610  smartlist_t *sl = smartlist_new();
611 
612  tor_assert(chan);
613 
615  cnt = smartlist_len(sl);
616  smartlist_free(sl);
617  log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
619  cnt);
620  return cnt;
621 }
622 
623 /** Remove <b>origin_circ</b> from the global list of origin circuits.
624  * Called when we are freeing a circuit.
625  */
626 static void
628 {
629  int origin_idx = origin_circ->global_origin_circuit_list_idx;
630  if (origin_idx < 0)
631  return;
632  origin_circuit_t *c2;
633  tor_assert(origin_idx <= smartlist_len(global_origin_circuit_list));
634  c2 = smartlist_get(global_origin_circuit_list, origin_idx);
635  tor_assert(origin_circ == c2);
637  if (origin_idx < smartlist_len(global_origin_circuit_list)) {
638  origin_circuit_t *replacement =
639  smartlist_get(global_origin_circuit_list, origin_idx);
640  replacement->global_origin_circuit_list_idx = origin_idx;
641  }
642  origin_circ->global_origin_circuit_list_idx = -1;
643 }
644 
645 /** Add <b>origin_circ</b> to the global list of origin circuits. Called
646  * when creating the circuit. */
647 static void
649 {
650  tor_assert(origin_circ->global_origin_circuit_list_idx == -1);
652  smartlist_add(lst, origin_circ);
653  origin_circ->global_origin_circuit_list_idx = smartlist_len(lst) - 1;
654 }
655 
656 /** Detach from the global circuit list, and deallocate, all
657  * circuits that have been marked for close.
658  */
659 void
661 {
662  if (circuits_pending_close == NULL)
663  return;
664 
668 
669  /* Remove it from the circuit list. */
670  int idx = circ->global_circuitlist_idx;
671  smartlist_del(lst, idx);
672  if (idx < smartlist_len(lst)) {
673  circuit_t *replacement = smartlist_get(lst, idx);
674  replacement->global_circuitlist_idx = idx;
675  }
676  circ->global_circuitlist_idx = -1;
677 
678  /* Remove it from the origin circuit list, if appropriate. */
679  if (CIRCUIT_IS_ORIGIN(circ)) {
681  }
682 
683  circuit_about_to_free(circ);
684  circuit_free(circ);
685  } SMARTLIST_FOREACH_END(circ);
686 
688 }
689 
690 /** Return a pointer to the global list of circuits. */
693 {
694  if (NULL == global_circuitlist)
696  return global_circuitlist;
697 }
698 
699 /** Return a pointer to the global list of origin circuits. */
700 smartlist_t *
702 {
703  if (NULL == global_origin_circuit_list)
706 }
707 
708 /**
709  * Return true if we have any opened general-purpose 3 hop
710  * origin circuits.
711  *
712  * The result from this function is cached for use by
713  * circuit_any_opened_circuits_cached().
714  */
715 int
717 {
719  const origin_circuit_t *, next_circ) {
720  if (!TO_CIRCUIT(next_circ)->marked_for_close &&
721  next_circ->has_opened &&
722  TO_CIRCUIT(next_circ)->state == CIRCUIT_STATE_OPEN &&
723  TO_CIRCUIT(next_circ)->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT &&
724  next_circ->build_state &&
725  next_circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN) {
727  return 1;
728  }
729  } SMARTLIST_FOREACH_END(next_circ);
730 
732  return 0;
733 }
734 
735 /**
736  * Cache the "any circuits opened" state, as specified in param
737  * circuits_are_opened. This is a helper function to update
738  * the circuit opened status whenever we happen to look at the
739  * circuit list.
740  */
741 void
742 circuit_cache_opened_circuit_state(int circuits_are_opened)
743 {
744  any_opened_circs_cached_val = circuits_are_opened;
745 }
746 
747 /**
748  * Return true if there were any opened circuits since the last call to
749  * circuit_any_opened_circuits(), or since circuit_expire_building() last
750  * ran (it runs roughly once per second).
751  */
752 int
754 {
756 }
757 
758 /** Function to make circ->state human-readable */
759 const char *
761 {
762  static char buf[64];
763  switch (state) {
764  case CIRCUIT_STATE_BUILDING: return "doing handshakes";
765  case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
766  case CIRCUIT_STATE_CHAN_WAIT: return "connecting to server";
767  case CIRCUIT_STATE_GUARD_WAIT: return "waiting to see how other "
768  "guards perform";
769  case CIRCUIT_STATE_OPEN: return "open";
770  default:
771  log_warn(LD_BUG, "Unknown circuit state %d", state);
772  tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
773  return buf;
774  }
775 }
776 
777 /** Map a circuit purpose to a string suitable to be displayed to a
778  * controller. */
779 const char *
781 {
782  static char buf[32];
783  switch (purpose) {
784  case CIRCUIT_PURPOSE_OR:
788  return "SERVER"; /* A controller should never see these, actually. */
789 
791  return "GENERAL";
792 
794  return "HS_CLIENT_HSDIR";
795 
799  return "HS_CLIENT_INTRO";
800 
805  return "HS_CLIENT_REND";
806 
808  return "HS_SERVICE_HSDIR";
809 
812  return "HS_SERVICE_INTRO";
813 
816  return "HS_SERVICE_REND";
817 
819  return "TESTING";
821  return "MEASURE_TIMEOUT";
823  return "CONTROLLER";
825  return "PATH_BIAS_TESTING";
827  return "HS_VANGUARDS";
829  return "CIRCUIT_PADDING";
830 
831  default:
832  tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
833  return buf;
834  }
835 }
836 
837 /** Return a string specifying the state of the hidden-service circuit
838  * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
839  * hidden-service-related circuit purpose. */
840 const char *
842 {
843  switch (purpose)
844  {
845  default:
847  "Unrecognized circuit purpose: %d",
848  (int)purpose);
850  /* fall through */
851 
852  case CIRCUIT_PURPOSE_OR:
860  return NULL;
861 
863  return "OR_HSSI_ESTABLISHED";
865  return "OR_HSCR_ESTABLISHED";
867  return "OR_HS_R_JOINED";
868 
871  return "HSCI_CONNECTING";
873  return "HSCI_INTRO_SENT";
875  return "HSCI_DONE";
876 
878  return "HSCR_CONNECTING";
880  return "HSCR_ESTABLISHED_IDLE";
882  return "HSCR_ESTABLISHED_WAITING";
884  return "HSCR_JOINED";
885 
888  return "HSSI_CONNECTING";
890  return "HSSI_ESTABLISHED";
891 
893  return "HSSR_CONNECTING";
895  return "HSSR_JOINED";
896  }
897 }
898 
899 /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
900 const char *
902 {
903  static char buf[32];
904 
905  switch (purpose)
906  {
907  case CIRCUIT_PURPOSE_OR:
908  return "Circuit at relay";
910  return "Acting as intro point";
912  return "Acting as rendezvous (pending)";
914  return "Acting as rendezvous (established)";
916  return "General-purpose client";
918  return "Hidden service client: Connecting to intro point";
920  return "Hidden service client: Waiting for ack from intro point";
922  return "Hidden service client: Received ack from intro point";
924  return "Hidden service client: Establishing rendezvous point";
926  return "Hidden service client: Pending rendezvous point";
928  return "Hidden service client: Pending rendezvous point (ack received)";
930  return "Hidden service client: Active rendezvous point";
932  return "Hidden service client: Fetching HS descriptor";
933 
935  return "Measuring circuit timeout";
936 
938  return "Hidden service: Establishing introduction point";
940  return "Hidden service: Introduction point";
942  return "Hidden service: Connecting to rendezvous point";
944  return "Hidden service: Active rendezvous point";
946  return "Hidden service: Uploading HS descriptor";
947 
949  return "Testing circuit";
950 
952  return "Circuit made by controller";
953 
955  return "Path-bias testing circuit";
956 
958  return "Hidden service: Pre-built vanguard circuit";
959 
961  return "Circuit kept open for padding";
962 
963  default:
964  tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
965  return buf;
966  }
967 }
968 
969 /** Pick a reasonable package_window to start out for our circuits.
970  * Originally this was hard-coded at 1000, but now the consensus votes
971  * on the answer. See proposal 168. */
972 int32_t
974 {
975  int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
976  CIRCWINDOW_START_MIN,
977  CIRCWINDOW_START_MAX);
978  /* If the consensus tells us a negative number, we'd assert. */
979  if (num < 0)
980  num = CIRCWINDOW_START;
981  return num;
982 }
983 
984 /** Initialize the common elements in a circuit_t, and add it to the global
985  * list. */
986 static void
988 {
990 
991  // Gets reset when we send CREATE_FAST.
992  // circuit_expire_building() expects these to be equal
993  // until the orconn is built.
994  circ->timestamp_began = circ->timestamp_created;
995 
1000 
1002  circ->global_circuitlist_idx = smartlist_len(circuit_get_global_list()) - 1;
1003 }
1004 
1005 /** If we haven't yet decided on a good timeout value for circuit
1006  * building, we close idle circuits aggressively so we can get more
1007  * data points. These are the default, min, and max consensus values */
1008 #define DFLT_IDLE_TIMEOUT_WHILE_LEARNING (3*60)
1009 #define MIN_IDLE_TIMEOUT_WHILE_LEARNING (10)
1010 #define MAX_IDLE_TIMEOUT_WHILE_LEARNING (1000*60)
1011 
1012 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
1013  * and <b>p_conn</b>. Add it to the global circuit list.
1014  */
1017 {
1018  origin_circuit_t *circ;
1019  /* never zero, since a global ID of 0 is treated specially by the
1020  * controller */
1021  static uint32_t n_circuits_allocated = 1;
1022 
1023  circ = tor_malloc_zero(sizeof(origin_circuit_t));
1024  circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
1025 
1026  circ->next_stream_id = crypto_rand_int(1<<16);
1027  circ->global_identifier = n_circuits_allocated++;
1030 
1032 
1033  /* Add to origin-list. */
1034  circ->global_origin_circuit_list_idx = -1;
1036 
1037  circuit_build_times_update_last_circ(get_circuit_build_times_mutable());
1038 
1041  /* Circuits should be shorter lived if we need more of them
1042  * for learning a good build timeout */
1043  circ->circuit_idle_timeout =
1044  networkstatus_get_param(NULL, "cbtlearntimeout",
1046  MIN_IDLE_TIMEOUT_WHILE_LEARNING,
1047  MAX_IDLE_TIMEOUT_WHILE_LEARNING);
1048  } else {
1049  // This should always be larger than the current port prediction time
1050  // remaining, or else we'll end up with the case where a circuit times out
1051  // and another one is built, effectively doubling the timeout window.
1052  //
1053  // We also randomize it by up to 5% more (ie 5% of 0 to 3600 seconds,
1054  // depending on how much circuit prediction time is remaining) so that
1055  // we don't close a bunch of unused circuits all at the same time.
1056  int prediction_time_remaining =
1058  circ->circuit_idle_timeout = prediction_time_remaining+1+
1059  crypto_rand_int(1+prediction_time_remaining/20);
1060 
1061  if (circ->circuit_idle_timeout <= 0) {
1062  log_warn(LD_BUG,
1063  "Circuit chose a negative idle timeout of %d based on "
1064  "%d seconds of predictive building remaining.",
1065  circ->circuit_idle_timeout,
1066  prediction_time_remaining);
1067  circ->circuit_idle_timeout =
1068  networkstatus_get_param(NULL, "cbtlearntimeout",
1070  MIN_IDLE_TIMEOUT_WHILE_LEARNING,
1071  MAX_IDLE_TIMEOUT_WHILE_LEARNING);
1072  }
1073 
1074  log_info(LD_CIRC,
1075  "Circuit %"PRIu32" chose an idle timeout of %d based on "
1076  "%d seconds of predictive building remaining.",
1077  (circ->global_identifier),
1078  circ->circuit_idle_timeout,
1079  prediction_time_remaining);
1080  }
1081 
1082  return circ;
1083 }
1084 
1085 /** Allocate a new or_circuit_t, connected to <b>p_chan</b> as
1086  * <b>p_circ_id</b>. If <b>p_chan</b> is NULL, the circuit is unattached. */
1087 or_circuit_t *
1088 or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
1089 {
1090  /* CircIDs */
1091  or_circuit_t *circ;
1092 
1093  circ = tor_malloc_zero(sizeof(or_circuit_t));
1094  circ->base_.magic = OR_CIRCUIT_MAGIC;
1095 
1096  if (p_chan)
1097  circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
1098 
1100  cell_queue_init(&circ->p_chan_cells);
1101 
1103 
1104  return circ;
1105 }
1106 
1107 /** Free all storage held in circ->testing_cell_stats */
1108 void
1110 {
1111  if (!circ || !circ->testing_cell_stats)
1112  return;
1114  ent, tor_free(ent));
1115  smartlist_free(circ->testing_cell_stats);
1116  circ->testing_cell_stats = NULL;
1117 }
1118 
1119 /** Deallocate space associated with circ.
1120  */
1121 STATIC void
1123 {
1124  circid_t n_circ_id = 0;
1125  void *mem;
1126  size_t memlen;
1127  int should_free = 1;
1128  if (!circ)
1129  return;
1130 
1131  /* We keep a copy of this so we can log its value before it gets unset. */
1132  n_circ_id = circ->n_circ_id;
1133 
1135 
1136  /* Cleanup circuit from anything HS v3 related. We also do this when the
1137  * circuit is closed. This is to avoid any code path that free registered
1138  * circuits without closing them before. This needs to be done before the
1139  * hs identifier is freed. */
1140  hs_circ_cleanup(circ);
1141 
1142  if (CIRCUIT_IS_ORIGIN(circ)) {
1143  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1144  mem = ocirc;
1145  memlen = sizeof(origin_circuit_t);
1147 
1149 
1150  if (ocirc->half_streams) {
1152  half_conn) {
1153  half_edge_free(half_conn);
1154  } SMARTLIST_FOREACH_END(half_conn);
1155  smartlist_free(ocirc->half_streams);
1156  }
1157 
1158  if (ocirc->build_state) {
1159  extend_info_free(ocirc->build_state->chosen_exit);
1162  }
1163  tor_free(ocirc->build_state);
1164 
1165  /* Cancel before freeing, if we haven't already succeeded or failed. */
1166  if (ocirc->guard_state) {
1168  }
1169  circuit_guard_state_free(ocirc->guard_state);
1170 
1171  circuit_clear_cpath(ocirc);
1172 
1173  crypto_pk_free(ocirc->intro_key);
1174  rend_data_free(ocirc->rend_data);
1175 
1176  /* Finally, free the identifier of the circuit and nullify it so multiple
1177  * cleanup will work. */
1178  hs_ident_circuit_free(ocirc->hs_ident);
1179  ocirc->hs_ident = NULL;
1180 
1181  tor_free(ocirc->dest_address);
1182  if (ocirc->socks_username) {
1183  memwipe(ocirc->socks_username, 0x12, ocirc->socks_username_len);
1184  tor_free(ocirc->socks_username);
1185  }
1186  if (ocirc->socks_password) {
1187  memwipe(ocirc->socks_password, 0x06, ocirc->socks_password_len);
1188  tor_free(ocirc->socks_password);
1189  }
1190  addr_policy_list_free(ocirc->prepend_policy);
1191  } else {
1192  or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
1193  /* Remember cell statistics for this circuit before deallocating. */
1194  if (get_options()->CellStatistics)
1195  rep_hist_buffer_stats_add_circ(circ, time(NULL));
1196  mem = ocirc;
1197  memlen = sizeof(or_circuit_t);
1198  tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
1199 
1200  should_free = (ocirc->workqueue_entry == NULL);
1201 
1202  relay_crypto_clear(&ocirc->crypto);
1203 
1204  if (ocirc->rend_splice) {
1205  or_circuit_t *other = ocirc->rend_splice;
1206  tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
1207  other->rend_splice = NULL;
1208  }
1209 
1210  /* remove from map. */
1211  circuit_set_p_circid_chan(ocirc, 0, NULL);
1212 
1213  /* Clear cell queue _after_ removing it from the map. Otherwise our
1214  * "active" checks will be violated. */
1215  cell_queue_clear(&ocirc->p_chan_cells);
1216  }
1217 
1218  extend_info_free(circ->n_hop);
1220 
1221  if (circ->global_circuitlist_idx != -1) {
1222  int idx = circ->global_circuitlist_idx;
1223  circuit_t *c2 = smartlist_get(global_circuitlist, idx);
1224  tor_assert(c2 == circ);
1226  if (idx < smartlist_len(global_circuitlist)) {
1227  c2 = smartlist_get(global_circuitlist, idx);
1228  c2->global_circuitlist_idx = idx;
1229  }
1230  }
1231 
1232  /* Remove from map. */
1233  circuit_set_n_circid_chan(circ, 0, NULL);
1234 
1235  /* Clear cell queue _after_ removing it from the map. Otherwise our
1236  * "active" checks will be violated. */
1238 
1239  /* Cleanup possible SENDME state. */
1240  if (circ->sendme_last_digests) {
1241  SMARTLIST_FOREACH(circ->sendme_last_digests, uint8_t *, d, tor_free(d));
1242  smartlist_free(circ->sendme_last_digests);
1243  }
1244 
1245  log_info(LD_CIRC, "Circuit %u (id: %" PRIu32 ") has been freed.",
1246  n_circ_id,
1247  CIRCUIT_IS_ORIGIN(circ) ?
1248  TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0);
1249 
1250  /* Free any circuit padding structures */
1252 
1253  /* Clear all dangling handle references. */
1254  circuit_handles_clear(circ);
1255 
1256  if (should_free) {
1257  memwipe(mem, 0xAA, memlen); /* poison memory */
1258  tor_free(mem);
1259  } else {
1260  /* If we made it here, this is an or_circuit_t that still has a pending
1261  * cpuworker request which we weren't able to cancel. Instead, set up
1262  * the magic value so that when the reply comes back, we'll know to discard
1263  * the reply and free this structure.
1264  */
1265  memwipe(mem, 0xAA, memlen);
1266  circ->magic = DEAD_CIRCUIT_MAGIC;
1267  }
1268 }
1269 
1270 /** Deallocate the linked list circ-><b>cpath</b>, and remove the cpath from
1271  * <b>circ</b>. */
1272 void
1274 {
1275  crypt_path_t *victim, *head, *cpath;
1276 
1277  head = cpath = circ->cpath;
1278 
1279  if (!cpath)
1280  return;
1281 
1282  /* it's a circular list, so we have to notice when we've
1283  * gone through it once. */
1284  while (cpath->next && cpath->next != head) {
1285  victim = cpath;
1286  cpath = victim->next;
1287  cpath_free(victim);
1288  }
1289 
1290  cpath_free(cpath);
1291 
1292  circ->cpath = NULL;
1293 }
1294 
1295 /** Release all storage held by circuits. */
1296 void
1298 {
1300 
1301  SMARTLIST_FOREACH_BEGIN(lst, circuit_t *, tmp) {
1302  if (! CIRCUIT_IS_ORIGIN(tmp)) {
1303  or_circuit_t *or_circ = TO_OR_CIRCUIT(tmp);
1304  while (or_circ->resolving_streams) {
1305  edge_connection_t *next_conn;
1306  next_conn = or_circ->resolving_streams->next_stream;
1308  or_circ->resolving_streams = next_conn;
1309  }
1310  }
1311  tmp->global_circuitlist_idx = -1;
1313  circuit_free(tmp);
1314  SMARTLIST_DEL_CURRENT(lst, tmp);
1315  } SMARTLIST_FOREACH_END(tmp);
1316 
1317  smartlist_free(lst);
1318  global_circuitlist = NULL;
1319 
1320  smartlist_free(global_origin_circuit_list);
1322 
1323  smartlist_free(circuits_pending_chans);
1324  circuits_pending_chans = NULL;
1325 
1326  smartlist_free(circuits_pending_close);
1327  circuits_pending_close = NULL;
1328 
1329  smartlist_free(circuits_pending_other_guards);
1331 
1332  {
1333  chan_circid_circuit_map_t **elt, **next, *c;
1334  for (elt = HT_START(chan_circid_map, &chan_circid_map);
1335  elt;
1336  elt = next) {
1337  c = *elt;
1338  next = HT_NEXT_RMV(chan_circid_map, &chan_circid_map, elt);
1339 
1340  tor_assert(c->circuit == NULL);
1341  tor_free(c);
1342  }
1343  }
1344  HT_CLEAR(chan_circid_map, &chan_circid_map);
1345 }
1346 
1347 /** Release a crypt_path_reference_t*, which may be NULL. */
1348 static void
1350 {
1351  if (cpath_ref != NULL) {
1352  if (--(cpath_ref->refcount) == 0) {
1353  cpath_free(cpath_ref->cpath);
1354  tor_free(cpath_ref);
1355  }
1356  }
1357 }
1358 
1359 /** A helper function for circuit_dump_by_conn() below. Log a bunch
1360  * of information about circuit <b>circ</b>.
1361  */
1362 static void
1364  circuit_t *circ,
1365  int conn_array_index,
1366  const char *type,
1367  circid_t this_circid,
1368  circid_t other_circid)
1369 {
1370  tor_log(severity, LD_CIRC, "Conn %d has %s circuit: circID %u "
1371  "(other side %u), state %d (%s), born %ld:",
1372  conn_array_index, type, (unsigned)this_circid, (unsigned)other_circid,
1373  circ->state, circuit_state_to_string(circ->state),
1374  (long)circ->timestamp_began.tv_sec);
1375  if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
1376  circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
1377  }
1378 }
1379 
1380 /** Log, at severity <b>severity</b>, information about each circuit
1381  * that is connected to <b>conn</b>.
1382  */
1383 void
1385 {
1386  edge_connection_t *tmpconn;
1387 
1389  circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
1390 
1391  if (circ->marked_for_close) {
1392  continue;
1393  }
1394 
1395  if (!CIRCUIT_IS_ORIGIN(circ)) {
1396  p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
1397  }
1398 
1399  if (CIRCUIT_IS_ORIGIN(circ)) {
1400  for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
1401  tmpconn=tmpconn->next_stream) {
1402  if (TO_CONN(tmpconn) == conn) {
1403  circuit_dump_conn_details(severity, circ, conn->conn_array_index,
1404  "App-ward", p_circ_id, n_circ_id);
1405  }
1406  }
1407  }
1408 
1409  if (! CIRCUIT_IS_ORIGIN(circ)) {
1410  for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
1411  tmpconn=tmpconn->next_stream) {
1412  if (TO_CONN(tmpconn) == conn) {
1413  circuit_dump_conn_details(severity, circ, conn->conn_array_index,
1414  "Exit-ward", n_circ_id, p_circ_id);
1415  }
1416  }
1417  }
1418  }
1419  SMARTLIST_FOREACH_END(circ);
1420 }
1421 
1422 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
1423  * such circuit exists. */
1426 {
1428  if (CIRCUIT_IS_ORIGIN(circ) &&
1429  TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
1430  if (circ->marked_for_close)
1431  return NULL;
1432  else
1433  return TO_ORIGIN_CIRCUIT(circ);
1434  }
1435  }
1436  SMARTLIST_FOREACH_END(circ);
1437  return NULL;
1438 }
1439 
1440 /** Return a circ such that:
1441  * - circ->n_circ_id or circ->p_circ_id is equal to <b>circ_id</b>, and
1442  * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
1443  * Return NULL if no such circuit exists.
1444  *
1445  * If <b>found_entry_out</b> is provided, set it to true if we have a
1446  * placeholder entry for circid/chan, and leave it unset otherwise.
1447  */
1448 static inline circuit_t *
1450  int *found_entry_out)
1451 {
1454 
1455  if (_last_circid_chan_ent &&
1456  circ_id == _last_circid_chan_ent->circ_id &&
1457  chan == _last_circid_chan_ent->chan) {
1458  found = _last_circid_chan_ent;
1459  } else {
1460  search.circ_id = circ_id;
1461  search.chan = chan;
1462  found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
1463  _last_circid_chan_ent = found;
1464  }
1465  if (found && found->circuit) {
1466  log_debug(LD_CIRC,
1467  "circuit_get_by_circid_channel_impl() returning circuit %p for"
1468  " circ_id %u, channel ID %"PRIu64 " (%p)",
1469  found->circuit, (unsigned)circ_id,
1470  (chan->global_identifier), chan);
1471  if (found_entry_out)
1472  *found_entry_out = 1;
1473  return found->circuit;
1474  }
1475 
1476  log_debug(LD_CIRC,
1477  "circuit_get_by_circid_channel_impl() found %s for"
1478  " circ_id %u, channel ID %"PRIu64 " (%p)",
1479  found ? "placeholder" : "nothing",
1480  (unsigned)circ_id,
1481  (chan->global_identifier), chan);
1482 
1483  if (found_entry_out)
1484  *found_entry_out = found ? 1 : 0;
1485 
1486  return NULL;
1487  /* The rest of this checks for bugs. Disabled by default. */
1488  /* We comment it out because coverity complains otherwise.
1489  {
1490  circuit_t *circ;
1491  TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
1492  if (! CIRCUIT_IS_ORIGIN(circ)) {
1493  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1494  if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
1495  log_warn(LD_BUG,
1496  "circuit matches p_chan, but not in hash table (Bug!)");
1497  return circ;
1498  }
1499  }
1500  if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
1501  log_warn(LD_BUG,
1502  "circuit matches n_chan, but not in hash table (Bug!)");
1503  return circ;
1504  }
1505  }
1506  return NULL;
1507  } */
1508 }
1509 
1510 /** Return a circ such that:
1511  * - circ->n_circ_id or circ->p_circ_id is equal to <b>circ_id</b>, and
1512  * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
1513  * - circ is not marked for close.
1514  * Return NULL if no such circuit exists.
1515  */
1516 circuit_t *
1517 circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
1518 {
1519  circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
1520  if (!circ || circ->marked_for_close)
1521  return NULL;
1522  else
1523  return circ;
1524 }
1525 
1526 /** Return a circ such that:
1527  * - circ->n_circ_id or circ->p_circ_id is equal to <b>circ_id</b>, and
1528  * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
1529  * Return NULL if no such circuit exists.
1530  */
1531 circuit_t *
1533  channel_t *chan)
1534 {
1535  return circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
1536 }
1537 
1538 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
1539  * circuit, marked or not, on <b>chan</b>, or if the circ ID is reserved until
1540  * a queued destroy cell can be sent.
1541  *
1542  * (Return 1 if the circuit is present, marked or not; Return 2
1543  * if the circuit ID is pending a destroy.)
1544  **/
1545 int
1546 circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
1547 {
1548  int found = 0;
1549  if (circuit_get_by_circid_channel_impl(circ_id, chan, &found) != NULL)
1550  return 1;
1551  if (found)
1552  return 2;
1553  return 0;
1554 }
1555 
1556 /** Helper for debugging 12184. Returns the time since which 'circ_id' has
1557  * been marked unusable on 'chan'. */
1558 time_t
1560 {
1563 
1564  memset(&search, 0, sizeof(search));
1565  search.circ_id = circ_id;
1566  search.chan = chan;
1567 
1568  found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
1569 
1570  if (! found || found->circuit)
1571  return 0;
1572 
1573  return found->made_placeholder_at;
1574 }
1575 
1576 /** Return the circuit that a given edge connection is using. */
1577 circuit_t *
1579 {
1580  circuit_t *circ;
1581 
1582  circ = conn->on_circuit;
1583  tor_assert(!circ ||
1584  (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
1585  : circ->magic == OR_CIRCUIT_MAGIC));
1586 
1587  return circ;
1588 }
1589 
1590 /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
1591  * circuit from the chan,circid map, and mark it for close if it hasn't
1592  * been marked already.
1593  */
1594 void
1595 circuit_unlink_all_from_channel(channel_t *chan, int reason)
1596 {
1597  smartlist_t *detached = smartlist_new();
1598 
1599 /* #define DEBUG_CIRCUIT_UNLINK_ALL */
1600 
1601  channel_unlink_all_circuits(chan, detached);
1602 
1603 #ifdef DEBUG_CIRCUIT_UNLINK_ALL
1604  {
1605  smartlist_t *detached_2 = smartlist_new();
1606  int mismatch = 0, badlen = 0;
1607 
1609  if (circ->n_chan == chan ||
1610  (!CIRCUIT_IS_ORIGIN(circ) &&
1611  TO_OR_CIRCUIT(circ)->p_chan == chan)) {
1612  smartlist_add(detached_2, circ);
1613  }
1614  }
1615  SMARTLIST_FOREACH_END(circ);
1616 
1617  if (smartlist_len(detached) != smartlist_len(detached_2)) {
1618  log_warn(LD_BUG, "List of detached circuits had the wrong length! "
1619  "(got %d, should have gotten %d)",
1620  (int)smartlist_len(detached),
1621  (int)smartlist_len(detached_2));
1622  badlen = 1;
1623  }
1624  smartlist_sort_pointers(detached);
1625  smartlist_sort_pointers(detached_2);
1626 
1627  SMARTLIST_FOREACH(detached, circuit_t *, c,
1628  if (c != smartlist_get(detached_2, c_sl_idx))
1629  mismatch = 1;
1630  );
1631 
1632  if (mismatch)
1633  log_warn(LD_BUG, "Mismatch in list of detached circuits.");
1634 
1635  if (badlen || mismatch) {
1636  smartlist_free(detached);
1637  detached = detached_2;
1638  } else {
1639  log_notice(LD_CIRC, "List of %d circuits was as expected.",
1640  (int)smartlist_len(detached));
1641  smartlist_free(detached_2);
1642  }
1643  }
1644 #endif /* defined(DEBUG_CIRCUIT_UNLINK_ALL) */
1645 
1646  SMARTLIST_FOREACH_BEGIN(detached, circuit_t *, circ) {
1647  int mark = 0;
1648  if (circ->n_chan == chan) {
1649 
1650  circuit_set_n_circid_chan(circ, 0, NULL);
1651  mark = 1;
1652 
1653  /* If we didn't request this closure, pass the remote
1654  * bit to mark_for_close. */
1655  if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
1656  reason |= END_CIRC_REASON_FLAG_REMOTE;
1657  }
1658  if (! CIRCUIT_IS_ORIGIN(circ)) {
1659  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1660  if (or_circ->p_chan == chan) {
1661  circuit_set_p_circid_chan(or_circ, 0, NULL);
1662  mark = 1;
1663  }
1664  }
1665  if (!mark) {
1666  log_warn(LD_BUG, "Circuit on detached list which I had no reason "
1667  "to mark");
1668  continue;
1669  }
1670  if (!circ->marked_for_close)
1671  circuit_mark_for_close(circ, reason);
1672  } SMARTLIST_FOREACH_END(circ);
1673 
1674  smartlist_free(detached);
1675 }
1676 
1677 /** Return a circ such that
1678  * - circ->rend_data->onion_address is equal to
1679  * <b>rend_data</b>->onion_address,
1680  * - circ->rend_data->rend_cookie is equal to
1681  * <b>rend_data</b>->rend_cookie, and
1682  * - circ->purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
1683  *
1684  * Return NULL if no such circuit exists.
1685  */
1688 {
1690  if (!circ->marked_for_close &&
1692  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1693  if (ocirc->rend_data == NULL) {
1694  continue;
1695  }
1697  rend_data_get_address(ocirc->rend_data)) &&
1699  rend_data->rend_cookie,
1700  REND_COOKIE_LEN))
1701  return ocirc;
1702  }
1703  }
1704  SMARTLIST_FOREACH_END(circ);
1705  return NULL;
1706 }
1707 
1708 /** Return the first introduction circuit originating from the global circuit
1709  * list after <b>start</b> or at the start of the list if <b>start</b> is
1710  * NULL. Return NULL if no circuit is found.
1711  *
1712  * If <b>want_client_circ</b> is true, then we are looking for client-side
1713  * introduction circuits: A client introduction point circuit has a purpose of
1714  * either CIRCUIT_PURPOSE_C_INTRODUCING, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
1715  * or CIRCUIT_PURPOSE_C_INTRODUCE_ACKED. This does not return a circuit marked
1716  * for close, but it returns circuits regardless of their circuit state.
1717  *
1718  * If <b>want_client_circ</b> is false, then we are looking for service-side
1719  * introduction circuits: A service introduction point circuit has a purpose of
1720  * either CIRCUIT_PURPOSE_S_ESTABLISH_INTRO or CIRCUIT_PURPOSE_S_INTRO. This
1721  * does not return circuits marked for close, or in any state other than open.
1722  */
1725  bool want_client_circ)
1726 {
1727  int idx = 0;
1729 
1730  if (start) {
1731  idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
1732  }
1733 
1734  for ( ; idx < smartlist_len(lst); ++idx) {
1735  circuit_t *circ = smartlist_get(lst, idx);
1736 
1737  /* Ignore a marked for close circuit or if the state is not open. */
1738  if (circ->marked_for_close) {
1739  continue;
1740  }
1741 
1742  /* Depending on whether we are looking for client or service circs, skip
1743  * circuits with other purposes. */
1744  if (want_client_circ) {
1745  if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
1748  continue;
1749  }
1750  } else { /* we are looking for service-side circs */
1751  if (circ->state != CIRCUIT_STATE_OPEN) {
1752  continue;
1753  }
1755  circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
1756  continue;
1757  }
1758  }
1759 
1760  /* The purposes we are looking for are only for origin circuits so the
1761  * following is valid. */
1762  return TO_ORIGIN_CIRCUIT(circ);
1763  }
1764  /* Not found. */
1765  return NULL;
1766 }
1767 
1768 /** Return the first service rendezvous circuit originating from the global
1769  * circuit list after <b>start</b> or at the start of the list if <b>start</b>
1770  * is NULL. Return NULL if no circuit is found.
1771  *
1772  * A service rendezvous point circuit has a purpose of either
1773  * CIRCUIT_PURPOSE_S_CONNECT_REND or CIRCUIT_PURPOSE_S_REND_JOINED. This does
1774  * not return a circuit marked for close and its state must be open. */
1777 {
1778  int idx = 0;
1780 
1781  if (start) {
1782  idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
1783  }
1784 
1785  for ( ; idx < smartlist_len(lst); ++idx) {
1786  circuit_t *circ = smartlist_get(lst, idx);
1787 
1788  /* Ignore a marked for close circuit or purpose not matching a service
1789  * intro point or if the state is not open. */
1790  if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
1793  continue;
1794  }
1795  /* The purposes we are looking for are only for origin circuits so the
1796  * following is valid. */
1797  return TO_ORIGIN_CIRCUIT(circ);
1798  }
1799  /* Not found. */
1800  return NULL;
1801 }
1802 
1803 /** Return the first circuit originating here in global_circuitlist after
1804  * <b>start</b> whose purpose is <b>purpose</b>, and where <b>digest</b> (if
1805  * set) matches the private key digest of the rend data associated with the
1806  * circuit. Return NULL if no circuit is found. If <b>start</b> is NULL,
1807  * begin at the start of the list.
1808  */
1811  const uint8_t *digest, uint8_t purpose)
1812 {
1813  int idx;
1816  if (start == NULL)
1817  idx = 0;
1818  else
1819  idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
1820 
1821  for ( ; idx < smartlist_len(lst); ++idx) {
1822  circuit_t *circ = smartlist_get(lst, idx);
1823  origin_circuit_t *ocirc;
1824 
1825  if (circ->marked_for_close)
1826  continue;
1827  if (circ->purpose != purpose)
1828  continue;
1829  /* At this point we should be able to get a valid origin circuit because
1830  * the origin purpose we are looking for matches this circuit. */
1831  if (BUG(!CIRCUIT_PURPOSE_IS_ORIGIN(circ->purpose))) {
1832  break;
1833  }
1834  ocirc = TO_ORIGIN_CIRCUIT(circ);
1835  if (!digest)
1836  return ocirc;
1837  if (rend_circuit_pk_digest_eq(ocirc, digest)) {
1838  return ocirc;
1839  }
1840  }
1841  return NULL;
1842 }
1843 
1844 /** We might cannibalize this circuit: Return true if its last hop can be used
1845  * as a v3 rendezvous point. */
1846 static int
1848 {
1849  if (!circ->build_state) {
1850  return 0;
1851  }
1852 
1853  extend_info_t *chosen_exit = circ->build_state->chosen_exit;
1854  if (BUG(!chosen_exit)) {
1855  return 0;
1856  }
1857 
1858  const node_t *rp_node = node_get_by_id(chosen_exit->identity_digest);
1859  if (rp_node) {
1860  if (node_supports_v3_rendezvous_point(rp_node)) {
1861  return 1;
1862  }
1863  }
1864 
1865  return 0;
1866 }
1867 
1868 /** We are trying to create a circuit of purpose <b>purpose</b> and we are
1869  * looking for cannibalizable circuits. Return the circuit purpose we would be
1870  * willing to cannibalize. */
1871 static uint8_t
1873 {
1874  if (circuit_should_use_vanguards(purpose)) {
1875  /* If we are using vanguards, then we should only cannibalize vanguard
1876  * circuits so that we get the same path construction logic. */
1878  } else {
1879  /* If no vanguards are used just get a general circuit! */
1881  }
1882 }
1883 
1884 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
1885  * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
1886  * flags in <b>flags</b>, and if info is defined, does not already use info
1887  * as any of its hops; or NULL if no circuit fits this description.
1888  *
1889  * The <b>purpose</b> argument refers to the purpose of the circuit we want to
1890  * create, not the purpose of the circuit we want to cannibalize.
1891  *
1892  * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
1893  *
1894  * To "cannibalize" a circuit means to extend it an extra hop, and use it
1895  * for some other purpose than we had originally intended. We do this when
1896  * we want to perform some low-bandwidth task at a specific relay, and we
1897  * would like the circuit to complete as soon as possible. (If we were going
1898  * to use a lot of bandwidth, we wouldn't want a circuit with an extra hop.
1899  * If we didn't care about circuit completion latency, we would just build
1900  * a new circuit.)
1901  */
1903 circuit_find_to_cannibalize(uint8_t purpose_to_produce, extend_info_t *info,
1904  int flags)
1905 {
1906  origin_circuit_t *best=NULL;
1907  int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
1908  int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
1909  int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
1910  const or_options_t *options = get_options();
1911  /* We want the circuit we are trying to cannibalize to have this purpose */
1912  int purpose_to_search_for;
1913 
1914  /* Make sure we're not trying to create a onehop circ by
1915  * cannibalization. */
1917 
1918  purpose_to_search_for = get_circuit_purpose_needed_to_cannibalize(
1919  purpose_to_produce);
1920 
1921  tor_assert_nonfatal(purpose_to_search_for == CIRCUIT_PURPOSE_C_GENERAL ||
1922  purpose_to_search_for == CIRCUIT_PURPOSE_HS_VANGUARDS);
1923 
1924  log_debug(LD_CIRC,
1925  "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
1926  "capacity %d, internal %d",
1927  purpose_to_produce, need_uptime, need_capacity, internal);
1928 
1930  if (CIRCUIT_IS_ORIGIN(circ_) &&
1931  circ_->state == CIRCUIT_STATE_OPEN &&
1932  !circ_->marked_for_close &&
1933  circ_->purpose == purpose_to_search_for &&
1934  !circ_->timestamp_dirty) {
1935  origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
1936 
1937  /* Only cannibalize from reasonable length circuits. If we
1938  * want C_GENERAL, then only choose 3 hop circs. If we want
1939  * HS_VANGUARDS, only choose 4 hop circs.
1940  */
1941  if (circ->build_state->desired_path_len !=
1942  route_len_for_purpose(purpose_to_search_for, NULL)) {
1943  goto next;
1944  }
1945 
1946  /* Ignore any circuits for which we can't use the Guard. It is possible
1947  * that the Guard was removed from the samepled set after the circuit
1948  * was created so avoid using it. */
1949  if (!entry_guard_could_succeed(circ->guard_state)) {
1950  goto next;
1951  }
1952 
1953  if ((!need_uptime || circ->build_state->need_uptime) &&
1954  (!need_capacity || circ->build_state->need_capacity) &&
1955  (internal == circ->build_state->is_internal) &&
1956  !circ->unusable_for_new_conns &&
1958  !circ->build_state->onehop_tunnel &&
1959  !circ->isolation_values_set) {
1960  if (info) {
1961  /* need to make sure we don't duplicate hops */
1962  crypt_path_t *hop = circ->cpath;
1963  const node_t *ri1 = node_get_by_id(info->identity_digest);
1964  do {
1965  const node_t *ri2;
1967  info->identity_digest, DIGEST_LEN))
1968  goto next;
1969  if (ri1 &&
1971  && nodes_in_same_family(ri1, ri2))
1972  goto next;
1973  hop=hop->next;
1974  } while (hop!=circ->cpath);
1975  }
1976  if (options->ExcludeNodes) {
1977  /* Make sure no existing nodes in the circuit are excluded for
1978  * general use. (This may be possible if StrictNodes is 0, and we
1979  * thought we needed to use an otherwise excluded node for, say, a
1980  * directory operation.) */
1981  crypt_path_t *hop = circ->cpath;
1982  do {
1984  hop->extend_info))
1985  goto next;
1986  hop = hop->next;
1987  } while (hop != circ->cpath);
1988  }
1989 
1990  if ((flags & CIRCLAUNCH_IS_V3_RP) &&
1992  log_debug(LD_GENERAL, "Skipping uncannibalizable circuit for v3 "
1993  "rendezvous point.");
1994  goto next;
1995  }
1996 
1997  if (!best || (best->build_state->need_uptime && !need_uptime))
1998  best = circ;
1999  next: ;
2000  }
2001  }
2002  }
2003  SMARTLIST_FOREACH_END(circ_);
2004  return best;
2005 }
2006 
2007 /**
2008  * Check whether any of the origin circuits that are waiting to see if
2009  * their guard is good enough to use can be upgraded to "ready". If so,
2010  * return a new smartlist containing them. Otherwise return NULL.
2011  */
2012 smartlist_t *
2014 {
2015  /* Only if some circuit is actually waiting on an upgrade should we
2016  * run the algorithm. */
2018  smartlist_len(circuits_pending_other_guards)==0)
2019  return NULL;
2020  /* Only if we have some origin circuits should we run the algorithm. */
2022  return NULL;
2023 
2024  /* Okay; we can pass our circuit list to entrynodes.c.*/
2025  smartlist_t *result = smartlist_new();
2026  int circuits_upgraded = entry_guards_upgrade_waiting_circuits(
2029  result);
2030  if (circuits_upgraded && smartlist_len(result)) {
2031  return result;
2032  } else {
2033  smartlist_free(result);
2034  return NULL;
2035  }
2036 }
2037 
2038 /** Return the number of hops in circuit's path. If circ has no entries,
2039  * or is NULL, returns 0. */
2040 int
2042 {
2043  int n = 0;
2044  if (circ && circ->cpath) {
2045  crypt_path_t *cpath, *cpath_next = NULL;
2046  for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
2047  cpath_next = cpath->next;
2048  ++n;
2049  }
2050  }
2051  return n;
2052 }
2053 
2054 /** Return the number of opened hops in circuit's path.
2055  * If circ has no entries, or is NULL, returns 0. */
2056 int
2058 {
2059  int n = 0;
2060  if (circ && circ->cpath) {
2061  crypt_path_t *cpath, *cpath_next = NULL;
2062  for (cpath = circ->cpath;
2063  cpath->state == CPATH_STATE_OPEN
2064  && cpath_next != circ->cpath;
2065  cpath = cpath_next) {
2066  cpath_next = cpath->next;
2067  ++n;
2068  }
2069  }
2070  return n;
2071 }
2072 
2073 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
2074  * aren't that many hops in the list. <b>hopnum</b> starts at 1.
2075  * Returns NULL if <b>hopnum</b> is 0 or negative. */
2076 crypt_path_t *
2078 {
2079  if (circ && circ->cpath && hopnum > 0) {
2080  crypt_path_t *cpath, *cpath_next = NULL;
2081  for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
2082  cpath_next = cpath->next;
2083  if (--hopnum <= 0)
2084  return cpath;
2085  }
2086  }
2087  return NULL;
2088 }
2089 
2090 /** Go through the circuitlist; mark-for-close each circuit that starts
2091  * at us but has not yet been used. */
2092 void
2094 {
2096  if (CIRCUIT_IS_ORIGIN(circ) &&
2097  !circ->marked_for_close &&
2098  !circ->timestamp_dirty)
2099  circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
2100  }
2101  SMARTLIST_FOREACH_END(circ);
2102 }
2103 
2104 /** Go through the circuitlist; for each circuit that starts at us
2105  * and is dirty, frob its timestamp_dirty so we won't use it for any
2106  * new streams.
2107  *
2108  * This is useful for letting the user change pseudonyms, so new
2109  * streams will not be linkable to old streams.
2110  */
2111 void
2113 {
2115  if (CIRCUIT_IS_ORIGIN(circ) &&
2116  !circ->marked_for_close &&
2117  circ->timestamp_dirty) {
2119  }
2120  }
2121  SMARTLIST_FOREACH_END(circ);
2122 }
2123 
2124 /**
2125  * Report any queued cells on or_circuits as written in our bandwidth
2126  * totals, for the specified channel direction.
2127  *
2128  * When we close a circuit or clear its cell queues, we've read
2129  * data and recorded those bytes in our read statistics, but we're
2130  * not going to write it. This discrepancy can be used by an adversary
2131  * to infer information from our public relay statistics and perform
2132  * attacks such as guard discovery.
2133  *
2134  * This function is in the critical path of circuit_mark_for_close().
2135  * It must be (and is) O(1)!
2136  *
2137  * See https://trac.torproject.org/projects/tor/ticket/23512.
2138  */
2139 void
2142 {
2143  uint64_t cells;
2144  uint64_t cell_size;
2145  uint64_t written_sync;
2146  const channel_t *chan = NULL;
2147  const or_circuit_t *or_circ;
2148 
2149  if (!CIRCUIT_IS_ORCIRC(c))
2150  return;
2151 
2152  or_circ = CONST_TO_OR_CIRCUIT(c);
2153 
2154  if (dir == CIRCUIT_N_CHAN) {
2155  chan = c->n_chan;
2156  cells = c->n_chan_cells.n;
2157  } else {
2158  chan = or_circ->p_chan;
2159  cells = or_circ->p_chan_cells.n;
2160  }
2161 
2162  /* If we still know the chan, determine real cell size. Otherwise,
2163  * assume it's a wide circid channel */
2164  if (chan)
2165  cell_size = get_cell_network_size(chan->wide_circ_ids);
2166  else
2167  cell_size = CELL_MAX_NETWORK_SIZE;
2168 
2169  /* The missing written bytes are the cell counts times their cell
2170  * size plus TLS per cell overhead */
2171  written_sync = cells*(cell_size+TLS_PER_CELL_OVERHEAD);
2172 
2173  /* Report the missing bytes as written, to avoid asymmetry.
2174  * We must use time() for consistency with rephist, even though on
2175  * some very old rare platforms, approx_time() may be faster. */
2176  rep_hist_note_bytes_written(written_sync, time(NULL));
2177 }
2178 
2179 /** Mark <b>circ</b> to be closed next time we call
2180  * circuit_close_all_marked(). Do any cleanup needed:
2181  * - If state is onionskin_pending, remove circ from the onion_pending
2182  * list.
2183  * - If circ isn't open yet: call circuit_build_failed() if we're
2184  * the origin.
2185  * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
2186  * failure we just had to the hidden service client module.
2187  * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
2188  * report to the hidden service client module that the intro point
2189  * we just tried may be unreachable.
2190  * - Send appropriate destroys and edge_destroys for conns and
2191  * streams attached to circ.
2192  * - If circ->rend_splice is set (we are the midpoint of a joined
2193  * rendezvous stream), then mark the other circuit to close as well.
2194  */
2195 MOCK_IMPL(void,
2196 circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
2197  const char *file))
2198 {
2199  int orig_reason = reason; /* Passed to the controller */
2200  assert_circuit_ok(circ);
2201  tor_assert(line);
2202  tor_assert(file);
2203 
2204  /* Check whether the circuitpadding subsystem wants to block this close */
2205  if (circpad_marked_circuit_for_padding(circ, reason)) {
2206  return;
2207  }
2208 
2209  if (circ->marked_for_close) {
2210  log_warn(LD_BUG,
2211  "Duplicate call to circuit_mark_for_close at %s:%d"
2212  " (first at %s:%d)", file, line,
2214  return;
2215  }
2216  if (reason == END_CIRC_AT_ORIGIN) {
2217  if (!CIRCUIT_IS_ORIGIN(circ)) {
2218  log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
2219  "but circuit was not at origin. (called %s:%d, purpose=%d)",
2220  file, line, circ->purpose);
2221  }
2222  reason = END_CIRC_REASON_NONE;
2223  }
2224 
2225  if (CIRCUIT_IS_ORIGIN(circ)) {
2226  if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
2227  /* Don't close it yet, we need to test it first */
2228  return;
2229  }
2230 
2231  /* We don't send reasons when closing circuits at the origin. */
2232  reason = END_CIRC_REASON_NONE;
2233  }
2234 
2235  circuit_synchronize_written_or_bandwidth(circ, CIRCUIT_N_CHAN);
2236  circuit_synchronize_written_or_bandwidth(circ, CIRCUIT_P_CHAN);
2237 
2238  if (reason & END_CIRC_REASON_FLAG_REMOTE)
2239  reason &= ~END_CIRC_REASON_FLAG_REMOTE;
2240 
2241  if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
2242  if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
2243  log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
2244  reason = END_CIRC_REASON_NONE;
2245  }
2246 
2247  circ->marked_for_close = line;
2248  circ->marked_for_close_file = file;
2249  circ->marked_for_close_reason = reason;
2250  circ->marked_for_close_orig_reason = orig_reason;
2251 
2252  if (!CIRCUIT_IS_ORIGIN(circ)) {
2253  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2254  if (or_circ->rend_splice) {
2255  if (!or_circ->rend_splice->base_.marked_for_close) {
2256  /* do this after marking this circuit, to avoid infinite recursion. */
2257  circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
2258  }
2259  or_circ->rend_splice = NULL;
2260  }
2261  }
2262 
2263  /* Notify the HS subsystem that this circuit is closing. */
2264  hs_circ_cleanup(circ);
2265 
2266  if (circuits_pending_close == NULL)
2268 
2271 
2272  log_info(LD_GENERAL, "Circuit %u (id: %" PRIu32 ") marked for close at "
2273  "%s:%d (orig reason: %d, new reason: %d)",
2274  circ->n_circ_id,
2275  CIRCUIT_IS_ORIGIN(circ) ?
2276  TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0,
2277  file, line, orig_reason, reason);
2278 }
2279 
2280 /** Called immediately before freeing a marked circuit <b>circ</b> from
2281  * circuit_free_all() while shutting down Tor; this is a safe-at-shutdown
2282  * version of circuit_about_to_free(). It's important that it at least
2283  * do circuitmux_detach_circuit() when appropriate.
2284  */
2285 static void
2287 {
2288 
2289  if (circ->n_chan) {
2290  circuit_clear_cell_queue(circ, circ->n_chan);
2291  circuitmux_detach_circuit(circ->n_chan->cmux, circ);
2292  circuit_set_n_circid_chan(circ, 0, NULL);
2293  }
2294 
2295  if (! CIRCUIT_IS_ORIGIN(circ)) {
2296  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2297 
2298  if (or_circ->p_chan) {
2299  circuit_clear_cell_queue(circ, or_circ->p_chan);
2300  circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
2301  circuit_set_p_circid_chan(or_circ, 0, NULL);
2302  }
2303  }
2304 }
2305 
2306 /** Called immediately before freeing a marked circuit <b>circ</b>.
2307  * Disconnects the circuit from other data structures, launches events
2308  * as appropriate, and performs other housekeeping.
2309  */
2310 static void
2312 {
2313 
2314  int reason = circ->marked_for_close_reason;
2315  int orig_reason = circ->marked_for_close_orig_reason;
2316 
2317  if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
2319  }
2320  /* If the circuit ever became OPEN, we sent it to the reputation history
2321  * module then. If it isn't OPEN, we send it there now to remember which
2322  * links worked and which didn't.
2323  */
2324  if (circ->state != CIRCUIT_STATE_OPEN &&
2325  circ->state != CIRCUIT_STATE_GUARD_WAIT) {
2326  if (CIRCUIT_IS_ORIGIN(circ)) {
2327  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
2328  circuit_build_failed(ocirc); /* take actions if necessary */
2329  }
2330  }
2331  if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
2334  }
2337  }
2338  if (CIRCUIT_IS_ORIGIN(circ)) {
2340  (circ->state == CIRCUIT_STATE_OPEN ||
2341  circ->state == CIRCUIT_STATE_GUARD_WAIT) ?
2342  CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
2343  orig_reason);
2344  }
2345 
2347  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
2348  int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
2351  if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
2352  ocirc->rend_data) {
2353  /* treat this like getting a nack from it */
2354  log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
2355  safe_str_client(rend_data_get_address(ocirc->rend_data)),
2356  safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
2357  timed_out ? "Recording timeout." : "Removing from descriptor.");
2359  ocirc->rend_data,
2360  timed_out ?
2361  INTRO_POINT_FAILURE_TIMEOUT :
2362  INTRO_POINT_FAILURE_GENERIC);
2363  }
2364  } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
2365  reason != END_CIRC_REASON_TIMEOUT) {
2366  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
2367  if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
2368  if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
2369  ocirc->rend_data) {
2370  log_info(LD_REND, "Failed intro circ %s to %s "
2371  "(building circuit to intro point). "
2372  "Marking intro point as possibly unreachable.",
2373  safe_str_client(rend_data_get_address(ocirc->rend_data)),
2374  safe_str_client(build_state_get_exit_nickname(
2375  ocirc->build_state)));
2377  ocirc->rend_data,
2378  INTRO_POINT_FAILURE_UNREACHABLE);
2379  }
2380  }
2381  }
2382 
2383  if (circ->n_chan) {
2384  circuit_clear_cell_queue(circ, circ->n_chan);
2385  /* Only send destroy if the channel isn't closing anyway */
2386  if (!CHANNEL_CONDEMNED(circ->n_chan)) {
2387  channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
2388  }
2389  circuitmux_detach_circuit(circ->n_chan->cmux, circ);
2390  circuit_set_n_circid_chan(circ, 0, NULL);
2391  }
2392 
2393  if (! CIRCUIT_IS_ORIGIN(circ)) {
2394  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2395  edge_connection_t *conn;
2396  for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
2397  connection_edge_destroy(or_circ->p_circ_id, conn);
2398  or_circ->n_streams = NULL;
2399 
2400  while (or_circ->resolving_streams) {
2401  conn = or_circ->resolving_streams;
2402  or_circ->resolving_streams = conn->next_stream;
2403  if (!conn->base_.marked_for_close) {
2404  /* The client will see a DESTROY, and infer that the connections
2405  * are closing because the circuit is getting torn down. No need
2406  * to send an end cell. */
2407  conn->edge_has_sent_end = 1;
2408  conn->end_reason = END_STREAM_REASON_DESTROY;
2410  connection_mark_for_close(TO_CONN(conn));
2411  }
2412  conn->on_circuit = NULL;
2413  }
2414 
2415  if (or_circ->p_chan) {
2416  circuit_clear_cell_queue(circ, or_circ->p_chan);
2417  /* Only send destroy if the channel isn't closing anyway */
2418  if (!CHANNEL_CONDEMNED(or_circ->p_chan)) {
2419  channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
2420  }
2421  circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
2422  circuit_set_p_circid_chan(or_circ, 0, NULL);
2423  }
2424  } else {
2425  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
2426  edge_connection_t *conn;
2427  for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
2428  connection_edge_destroy(circ->n_circ_id, conn);
2429  ocirc->p_streams = NULL;
2430  }
2431 }
2432 
2433 /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
2434  * recover memory. */
2435 static void
2437 {
2438  if (!circ->marked_for_close) {
2439  log_warn(LD_BUG, "Called on non-marked circuit");
2440  return;
2441  }
2443  if (! CIRCUIT_IS_ORIGIN(circ)) {
2444  or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
2445  cell_queue_clear(&orcirc->p_chan_cells);
2446  }
2447 }
2448 
2449 static size_t
2450 single_conn_free_bytes(connection_t *conn)
2451 {
2452  size_t result = 0;
2453  if (conn->inbuf) {
2454  result += buf_allocation(conn->inbuf);
2455  buf_clear(conn->inbuf);
2456  }
2457  if (conn->outbuf) {
2458  result += buf_allocation(conn->outbuf);
2459  buf_clear(conn->outbuf);
2460  conn->outbuf_flushlen = 0;
2461  }
2462  if (conn->type == CONN_TYPE_DIR) {
2463  dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2464  if (dir_conn->compress_state) {
2465  result += tor_compress_state_size(dir_conn->compress_state);
2466  tor_compress_free(dir_conn->compress_state);
2467  dir_conn->compress_state = NULL;
2468  }
2469  }
2470  return result;
2471 }
2472 
2473 /** Aggressively free buffer contents on all the buffers of all streams in the
2474  * list starting at <b>stream</b>. Return the number of bytes recovered. */
2475 static size_t
2477 {
2478  size_t result = 0;
2479  for ( ; stream; stream = stream->next_stream) {
2480  connection_t *conn = TO_CONN(stream);
2481  result += single_conn_free_bytes(conn);
2482  if (conn->linked_conn) {
2483  result += single_conn_free_bytes(conn->linked_conn);
2484  }
2485  }
2486  return result;
2487 }
2488 
2489 /** Aggressively free buffer contents on all the buffers of all streams on
2490  * circuit <b>c</b>. Return the number of bytes recovered. */
2491 static size_t
2493 {
2494  if (CIRCUIT_IS_ORIGIN(c)) {
2496  } else {
2497  return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
2498  }
2499 }
2500 
2501 /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
2502 STATIC size_t
2504 {
2505  size_t n = c->n_chan_cells.n;
2506  if (! CIRCUIT_IS_ORIGIN(c)) {
2507  circuit_t *cc = (circuit_t *) c;
2508  n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
2509  }
2510  return n;
2511 }
2512 
2513 /** Return the number of bytes allocated for <b>c</b>'s half-open streams. */
2514 static size_t
2516 {
2517  if (! CIRCUIT_IS_ORIGIN(c)) {
2518  return 0;
2519  }
2520  const origin_circuit_t *ocirc = CONST_TO_ORIGIN_CIRCUIT(c);
2521  if (ocirc->half_streams)
2522  return smartlist_len(ocirc->half_streams) * sizeof(half_edge_t);
2523  else
2524  return 0;
2525 }
2526 
2527 /**
2528  * Return the age of the oldest cell queued on <b>c</b>, in timestamp units.
2529  * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
2530  * the current coarse timestamp.
2531  *
2532  * This function will return incorrect results if the oldest cell queued on
2533  * the circuit is older than about 2**32 msec (about 49 days) old.
2534  */
2535 STATIC uint32_t
2537 {
2538  uint32_t age = 0;
2539  packed_cell_t *cell;
2540 
2541  if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
2542  age = now - cell->inserted_timestamp;
2543 
2544  if (! CIRCUIT_IS_ORIGIN(c)) {
2545  const or_circuit_t *orcirc = CONST_TO_OR_CIRCUIT(c);
2546  if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
2547  uint32_t age2 = now - cell->inserted_timestamp;
2548  if (age2 > age)
2549  return age2;
2550  }
2551  }
2552  return age;
2553 }
2554 
2555 /** Return the age of the oldest buffer chunk on <b>conn</b>, where age is
2556  * taken in timestamp units before the time <b>now</b>. If the connection has
2557  * no data, treat it as having age zero.
2558  **/
2559 static uint32_t
2560 conn_get_buffer_age(const connection_t *conn, uint32_t now_ts)
2561 {
2562  uint32_t age = 0, age2;
2563  if (conn->outbuf) {
2564  age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now_ts);
2565  if (age2 > age)
2566  age = age2;
2567  }
2568  if (conn->inbuf) {
2569  age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now_ts);
2570  if (age2 > age)
2571  age = age2;
2572  }
2573  return age;
2574 }
2575 
2576 /** Return the age in timestamp units of the oldest buffer chunk on any stream
2577  * in the linked list <b>stream</b>, where age is taken in timestamp units
2578  * before the timestamp <b>now</b>. */
2579 static uint32_t
2581 {
2582  uint32_t age = 0, age2;
2583  for (; stream; stream = stream->next_stream) {
2584  const connection_t *conn = TO_CONN(stream);
2585  age2 = conn_get_buffer_age(conn, now);
2586  if (age2 > age)
2587  age = age2;
2588  if (conn->linked_conn) {
2589  age2 = conn_get_buffer_age(conn->linked_conn, now);
2590  if (age2 > age)
2591  age = age2;
2592  }
2593  }
2594  return age;
2595 }
2596 
2597 /** Return the age in timestamp units of the oldest buffer chunk on any stream
2598  * attached to the circuit <b>c</b>, where age is taken before the timestamp
2599  * <b>now</b>. */
2600 STATIC uint32_t
2602 {
2603  if (CIRCUIT_IS_ORIGIN(c)) {
2605  CONST_TO_ORIGIN_CIRCUIT(c)->p_streams, now);
2606  } else {
2608  CONST_TO_OR_CIRCUIT(c)->n_streams, now);
2609  }
2610 }
2611 
2612 /** Return the age of the oldest cell or stream buffer chunk on the circuit
2613  * <b>c</b>, where age is taken in timestamp units before the timestamp
2614  * <b>now</b> */
2615 STATIC uint32_t
2617 {
2618  uint32_t cell_age = circuit_max_queued_cell_age(c, now);
2619  uint32_t data_age = circuit_max_queued_data_age(c, now);
2620  if (cell_age > data_age)
2621  return cell_age;
2622  else
2623  return data_age;
2624 }
2625 
2626 /** Helper to sort a list of circuit_t by age of oldest item, in descending
2627  * order. */
2628 static int
2629 circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
2630 {
2631  const circuit_t *a = *a_;
2632  const circuit_t *b = *b_;
2633  uint32_t age_a = a->age_tmp;
2634  uint32_t age_b = b->age_tmp;
2635 
2636  if (age_a < age_b)
2637  return 1;
2638  else if (age_a == age_b)
2639  return 0;
2640  else
2641  return -1;
2642 }
2643 
2644 static uint32_t now_ts_for_buf_cmp;
2645 
2646 /** Helper to sort a list of circuit_t by age of oldest item, in descending
2647  * order. */
2648 static int
2649 conns_compare_by_buffer_age_(const void **a_, const void **b_)
2650 {
2651  const connection_t *a = *a_;
2652  const connection_t *b = *b_;
2653  time_t age_a = conn_get_buffer_age(a, now_ts_for_buf_cmp);
2654  time_t age_b = conn_get_buffer_age(b, now_ts_for_buf_cmp);
2655 
2656  if (age_a < age_b)
2657  return 1;
2658  else if (age_a == age_b)
2659  return 0;
2660  else
2661  return -1;
2662 }
2663 
2664 #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
2665 
2666 /** We're out of memory for cells, having allocated <b>current_allocation</b>
2667  * bytes' worth. Kill the 'worst' circuits until we're under
2668  * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
2669 void
2670 circuits_handle_oom(size_t current_allocation)
2671 {
2672  smartlist_t *circlist;
2674  int conn_idx;
2675  size_t mem_to_recover;
2676  size_t mem_recovered=0;
2677  int n_circuits_killed=0;
2678  int n_dirconns_killed=0;
2679  uint32_t now_ts;
2680  log_notice(LD_GENERAL, "We're low on memory (cell queues total alloc:"
2681  " %"TOR_PRIuSZ" buffer total alloc: %" TOR_PRIuSZ ","
2682  " tor compress total alloc: %" TOR_PRIuSZ
2683  " (zlib: %" TOR_PRIuSZ ", zstd: %" TOR_PRIuSZ ","
2684  " lzma: %" TOR_PRIuSZ "),"
2685  " rendezvous cache total alloc: %" TOR_PRIuSZ "). Killing"
2686  " circuits withover-long queues. (This behavior is controlled by"
2687  " MaxMemInQueues.)",
2688  cell_queues_get_total_allocation(),
2689  buf_get_total_allocation(),
2694  rend_cache_get_total_allocation());
2695 
2696  {
2697  size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
2698  FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
2699  if (current_allocation <= mem_target)
2700  return;
2701  mem_to_recover = current_allocation - mem_target;
2702  }
2703 
2704  now_ts = monotime_coarse_get_stamp();
2705 
2706  circlist = circuit_get_global_list();
2707  SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
2708  circ->age_tmp = circuit_max_queued_item_age(circ, now_ts);
2709  } SMARTLIST_FOREACH_END(circ);
2710 
2711  /* This is O(n log n); there are faster algorithms we could use instead.
2712  * Let's hope this doesn't happen enough to be in the critical path. */
2714 
2715  /* Fix up the indices before we run into trouble */
2716  SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
2717  circ->global_circuitlist_idx = circ_sl_idx;
2718  } SMARTLIST_FOREACH_END(circ);
2719 
2720  /* Now sort the connection array ... */
2721  now_ts_for_buf_cmp = now_ts;
2723  now_ts_for_buf_cmp = 0;
2724 
2725  /* Fix up the connection array to its new order. */
2727  conn->conn_array_index = conn_sl_idx;
2728  } SMARTLIST_FOREACH_END(conn);
2729 
2730  /* Okay, now the worst circuits and connections are at the front of their
2731  * respective lists. Let's mark them, and reclaim their storage
2732  * aggressively. */
2733  conn_idx = 0;
2734  SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
2735  size_t n;
2736  size_t freed;
2737 
2738  /* Free storage in any non-linked directory connections that have buffered
2739  * data older than this circuit. */
2740  while (conn_idx < smartlist_len(connection_array)) {
2741  connection_t *conn = smartlist_get(connection_array, conn_idx);
2742  uint32_t conn_age = conn_get_buffer_age(conn, now_ts);
2743  if (conn_age < circ->age_tmp) {
2744  break;
2745  }
2746  if (conn->type == CONN_TYPE_DIR && conn->linked_conn == NULL) {
2747  if (!conn->marked_for_close)
2748  connection_mark_for_close(conn);
2749  mem_recovered += single_conn_free_bytes(conn);
2750 
2751  ++n_dirconns_killed;
2752 
2753  if (mem_recovered >= mem_to_recover)
2754  goto done_recovering_mem;
2755  }
2756  ++conn_idx;
2757  }
2758 
2759  /* Now, kill the circuit. */
2760  n = n_cells_in_circ_queues(circ);
2761  const size_t half_stream_alloc = circuit_alloc_in_half_streams(circ);
2762  if (! circ->marked_for_close) {
2763  circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
2764  }
2766  freed = marked_circuit_free_stream_bytes(circ);
2767 
2768  ++n_circuits_killed;
2769 
2770  mem_recovered += n * packed_cell_mem_cost();
2771  mem_recovered += half_stream_alloc;
2772  mem_recovered += freed;
2773 
2774  if (mem_recovered >= mem_to_recover)
2775  goto done_recovering_mem;
2776  } SMARTLIST_FOREACH_END(circ);
2777 
2778  done_recovering_mem:
2779 
2780  log_notice(LD_GENERAL, "Removed %"TOR_PRIuSZ" bytes by killing %d circuits; "
2781  "%d circuits remain alive. Also killed %d non-linked directory "
2782  "connections.",
2783  mem_recovered,
2784  n_circuits_killed,
2785  smartlist_len(circlist) - n_circuits_killed,
2786  n_dirconns_killed);
2787 }
2788 
2789 /** Verify that circuit <b>c</b> has all of its invariants
2790  * correct. Trigger an assert if anything is invalid.
2791  */
2792 MOCK_IMPL(void,
2794 {
2795  edge_connection_t *conn;
2796  const or_circuit_t *or_circ = NULL;
2797  const origin_circuit_t *origin_circ = NULL;
2798 
2799  tor_assert(c);
2801  tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
2802  c->purpose <= CIRCUIT_PURPOSE_MAX_);
2803 
2804  if (CIRCUIT_IS_ORIGIN(c))
2805  origin_circ = CONST_TO_ORIGIN_CIRCUIT(c);
2806  else
2807  or_circ = CONST_TO_OR_CIRCUIT(c);
2808 
2809  if (c->n_chan) {
2810  tor_assert(!c->n_hop);
2811 
2812  if (c->n_circ_id) {
2813  /* We use the _impl variant here to make sure we don't fail on marked
2814  * circuits, which would not be returned by the regular function. */
2816  c->n_chan, NULL);
2817  tor_assert(c == c2);
2818  }
2819  }
2820  if (or_circ && or_circ->p_chan) {
2821  if (or_circ->p_circ_id) {
2822  /* ibid */
2823  circuit_t *c2 =
2825  or_circ->p_chan, NULL);
2826  tor_assert(c == c2);
2827  }
2828  }
2829  if (or_circ)
2830  for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
2831  tor_assert(conn->base_.type == CONN_TYPE_EXIT);
2832 
2833  tor_assert(c->deliver_window >= 0);
2834  tor_assert(c->package_window >= 0);
2835  if (c->state == CIRCUIT_STATE_OPEN ||
2838  if (or_circ) {
2839  relay_crypto_assert_ok(&or_circ->crypto);
2840  }
2841  }
2842  if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
2845  } else {
2848  }
2849  if (origin_circ && origin_circ->cpath) {
2850  cpath_assert_ok(origin_circ->cpath);
2851  }
2853  tor_assert(or_circ);
2854  if (!c->marked_for_close) {
2855  tor_assert(or_circ->rend_splice);
2856  tor_assert(or_circ->rend_splice->rend_splice == or_circ);
2857  }
2858  tor_assert(or_circ->rend_splice != or_circ);
2859  } else {
2860  tor_assert(!or_circ || !or_circ->rend_splice);
2861  }
2862 }
#define CIRCUIT_PURPOSE_C_INTRODUCING
Definition: circuitlist.h:75
Header file for circuitstats.c.
struct tor_compress_state_t * compress_state
#define CIRCLAUNCH_ONEHOP_TUNNEL
Definition: circuituse.h:39
static circuit_t * circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan, int *found_entry_out)
Definition: circuitlist.c:1449
smartlist_t * get_connection_array(void)
Definition: mainloop.c:452
Header file for rendcommon.c.
void circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
Definition: circuitlist.c:576
#define CIRCLAUNCH_IS_INTERNAL
Definition: circuituse.h:46
circuit_t * circuit_get_by_circid_channel_even_if_marked(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1532
HT_PROTOTYPE(HT_GENERATE2(channel_gid_map, HT_GENERATE2(channel_s, HT_GENERATE2(gidmap_node, HT_GENERATE2(channel_id_hash, HT_GENERATE2(channel_id_eq)
Definition: channel.c:121
const char * channel_get_canonical_remote_descr(channel_t *chan)
Definition: channel.c:2847
#define CIRCUIT_PURPOSE_IS_ORIGIN(p)
Definition: circuitlist.h:139
Header file for channeltls.c.
static int conns_compare_by_buffer_age_(const void **a_, const void **b_)
Definition: circuitlist.c:2649
#define CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
Definition: circuitlist.h:95
char identity_digest[DIGEST_LEN]
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:293
static void circuit_about_to_free(circuit_t *circ)
Definition: circuitlist.c:2311
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:88
channel_t * n_chan
Definition: circuit_st.h:68
Header file for circuitbuild.c.
smartlist_t * testing_cell_stats
Definition: circuit_st.h:211
Common functions for using (pseudo-)random number generators.
int rend_client_report_intro_point_failure(extend_info_t *failed_intro, rend_data_t *rend_data, unsigned int failure_type)
Definition: rendclient.c:794
Definition: node_st.h:33
Headers for crypto_dh.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define TO_CONN(c)
Definition: or.h:735
int marked_for_close_orig_reason
Definition: circuit_st.h:200
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)
void entry_guard_cancel(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2459
extend_info_t * extend_info
Definition: crypt_path_st.h:61
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
crypt_path_t * circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
Definition: circuitlist.c:2077
void circuits_handle_oom(size_t current_allocation)
Definition: circuitlist.c:2670
extend_info_t * chosen_exit
void circuitmux_attach_circuit(circuitmux_t *cmux, circuit_t *circ, cell_direction_t direction)
Definition: circuitmux.c:728
Header file for connection.c.
uint32_t magic
Definition: circuit_st.h:61
void rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
Definition: rephist.c:1857
extend_info_t * n_hop
Definition: circuit_st.h:86
#define LD_GENERAL
Definition: log.h:62
int deliver_window
Definition: circuit_st.h:120
static int chan_circid_entries_eq_(chan_circid_circuit_map_t *a, chan_circid_circuit_map_t *b)
Definition: circuitlist.c:193
int route_len_for_purpose(uint8_t purpose, extend_info_t *exit_ei)
void connection_free_(connection_t *conn)
Definition: connection.c:761
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:146
#define CIRCLAUNCH_NEED_UPTIME
Definition: circuituse.h:41
int rend_cmp_service_ids(const char *one, const char *two)
Definition: rendcommon.c:48
static size_t marked_circuit_free_stream_bytes(circuit_t *c)
Definition: circuitlist.c:2492
#define DOWNCAST(to, ptr)
Definition: or.h:110
int predicted_ports_prediction_time_remaining(time_t now)
Definition: predict_ports.c:54
void circuit_reset_sendme_randomness(circuit_t *circ)
Definition: relay.c:2060
#define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
Definition: or.h:279
static void init_circuit_base(circuit_t *circ)
Definition: circuitlist.c:987
#define CIRCUIT_PURPOSE_REND_POINT_WAITING
Definition: circuitlist.h:45
Headers for compress.c.
const char * circuit_purpose_to_string(uint8_t purpose)
Definition: circuitlist.c:901
int conn_array_index
Definition: connection_st.h:94
#define DEFAULT_ROUTE_LEN
Definition: or.h:1007
Header file for nodelist.c.
uint64_t MaxMemInQueues
#define CIRCWINDOW_START
Definition: or.h:501
struct edge_connection_t * next_stream
uint8_t state
Definition: circuit_st.h:109
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:628
uint8_t purpose
Definition: circuit_st.h:110
Header file for directory.c.
void smartlist_add(smartlist_t *sl, void *element)
static HT_HEAD(HT_PROTOTYPE(chan_circid_map, HT_PROTOTYPE(chan_circid_circuit_map_t)
Definition: circuitlist.c:216
static unsigned int chan_circid_entry_hash_(chan_circid_circuit_map_t *a)
Definition: circuitlist.c:202
const char * circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
Definition: circuitlist.c:841
void assert_circuit_ok(const circuit_t *c)
Definition: circuitlist.c:2793
int circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1546
void circuit_clear_testing_cell_stats(circuit_t *circ)
Definition: circuitlist.c:1109
void cell_queue_clear(cell_queue_t *queue)
Definition: relay.c:2583
int connection_edge_destroy(circid_t circ_id, edge_connection_t *conn)
int smartlist_contains(const smartlist_t *sl, const void *element)
#define TO_CIRCUIT(x)
Definition: or.h:951
int circuit_count_pending_on_channel(channel_t *chan)
Definition: circuitlist.c:607
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:85
struct timeval timestamp_began
Definition: circuit_st.h:164
smartlist_t * half_streams
Header file for config.c.
const or_options_t * get_options(void)
Definition: config.c:941
crypt_path_t * cpath
#define tor_assert(expr)
Definition: util_bug.h:102
origin_circuit_t * circuit_get_next_service_rp_circ(origin_circuit_t *start)
Definition: circuitlist.c:1776
guard_selection_t * get_guard_selection_info(void)
Definition: entrynodes.c:303
STATIC size_t n_cells_in_circ_queues(const circuit_t *c)
Definition: circuitlist.c:2503
origin_circuit_t * circuit_get_by_global_id(uint32_t id)
Definition: circuitlist.c:1425
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:103
void circuit_clear_cell_queue(circuit_t *circ, channel_t *chan)
Definition: relay.c:3230
static int circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
Definition: circuitlist.c:2629
void circuit_set_state(circuit_t *circ, uint8_t state)
Definition: circuitlist.c:543
void tor_gettimeofday(struct timeval *timeval)
relay_crypto_t crypto
Definition: or_circuit_st.h:46
struct circuit_t * on_circuit
int circuit_event_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
Definition: circuitlist.c:499
static smartlist_t * global_circuitlist
Definition: circuitlist.c:119
void mainloop_schedule_postloop_cleanup(void)
Definition: mainloop.c:1638
#define DEAD_CIRCUIT_MAGIC
Definition: circuit_st.h:35
#define tor_free(p)
Definition: malloc.h:52
uint16_t marked_for_close
Definition: circuit_st.h:188
#define tor_fragile_assert()
Definition: util_bug.h:246
#define SMARTLIST_DEL_CURRENT(sl, var)
Header file for mainloop.c.
struct timeval timestamp_created
Definition: circuit_st.h:167
#define CIRCUIT_PURPOSE_REND_ESTABLISHED
Definition: circuitlist.h:47
Integer definitions used throughout Tor.
int circuit_get_cpath_len(origin_circuit_t *circ)
Definition: circuitlist.c:2041
Header for compress_lzma.c.
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
size_t packed_cell_mem_cost(void)
Definition: relay.c:2676
smartlist_t * smartlist_new(void)
struct create_cell_t * n_chan_create_cell
Definition: circuit_st.h:152
void onion_pending_remove(or_circuit_t *circ)
Definition: onion_queue.c:308
void circuit_clear_cpath(origin_circuit_t *circ)
Definition: circuitlist.c:1273
void cpath_assert_ok(const crypt_path_t *cp)
Definition: crypt_path.c:83
Definition: or.h:923
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Definition: malloc.c:146
void smartlist_sort_pointers(smartlist_t *sl)
Definition: smartlist.c:594
struct connection_t * linked_conn
#define STATIC
Definition: testsupport.h:32
void circuitmux_detach_circuit(circuitmux_t *cmux, circuit_t *circ)
Definition: circuitmux.c:849
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
#define END_CIRC_REASON_FLAG_REMOTE
Definition: or.h:328
int entry_guards_upgrade_waiting_circuits(guard_selection_t *gs, const smartlist_t *all_circuits_in, smartlist_t *newly_complete_out)
Definition: entrynodes.c:2584
struct or_circuit_t * rend_splice
Definition: or_circuit_st.h:50
Circuit-build-stse structure.
#define CIRCUIT_PURPOSE_C_HSDIR_GET
Definition: circuitlist.h:92
Header file for routerset.c.
#define CIRCLAUNCH_IS_V3_RP
Definition: circuituse.h:49
void circpad_circuit_free_all_machineinfos(circuit_t *circ)
smartlist_t * circuit_get_global_list(void)
Definition: circuitlist.c:692
int marked_for_close_reason
Definition: circuit_st.h:196
int circuit_should_use_vanguards(uint8_t purpose)
Definition: circuituse.c:1998
streamid_t next_stream_id
unsigned int remaining_relay_early_cells
smartlist_t * circuit_get_global_origin_circuit_list(void)
Definition: circuitlist.c:701
#define MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
Definition: or.h:940
static smartlist_t * circuits_pending_chans
Definition: circuitlist.c:126
static int any_opened_circs_cached_val
Definition: circuitlist.c:146
Header file for policies.c.
#define CIRCUIT_STATE_ONIONSKIN_PENDING
Definition: circuitlist.h:23
void channel_note_destroy_pending(channel_t *chan, circid_t id)
Definition: circuitlist.c:409
#define CIRCUIT_PURPOSE_CONTROLLER
Definition: circuitlist.h:120
int circuit_build_times_needs_circuits(const circuit_build_times_t *cbt)
static void marked_circuit_free_cells(circuit_t *circ)
Definition: circuitlist.c:2436
unsigned int isolation_values_set
size_t tor_zlib_get_total_allocation(void)
int32_t circuit_initial_package_window(void)
Definition: circuitlist.c:973
Common functions for cryptographic routines.
Header file for channel.c.
origin_circuit_t * circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
Definition: circuitlist.c:1687
origin_circuit_t * circuit_find_to_cannibalize(uint8_t purpose_to_produce, extend_info_t *info, int flags)
Definition: circuitlist.c:1903
void relay_crypto_assert_ok(const relay_crypto_t *crypto)
Definition: relay_crypto.c:367
STATIC uint32_t circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
Definition: circuitlist.c:2616
#define LD_CIRC
Definition: log.h:82
circuit_t * circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1517
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
struct buf_t * outbuf
Definition: connection_st.h:99
cell_queue_t p_chan_cells
Definition: or_circuit_st.h:35
origin_circuit_t * circuit_get_next_intro_circ(const origin_circuit_t *start, bool want_client_circ)
Definition: circuitlist.c:1724
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:85
time_t circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1559
void hs_circ_cleanup(circuit_t *circ)
Definition: hs_circuit.c:1204
Header file for circuitpadding.c.
#define DIGEST_LEN
Definition: digest_sizes.h:20
void circuit_synchronize_written_or_bandwidth(const circuit_t *c, circuit_channel_direction_t dir)
Definition: circuitlist.c:2140
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED
Definition: circuitlist.h:81
Origin circuit structure.
#define CIRCUIT_PURPOSE_TESTING
Definition: circuitlist.h:118
static int circuit_can_be_cannibalized_for_v3_rp(const origin_circuit_t *circ)
Definition: circuitlist.c:1847
#define END_CIRC_AT_ORIGIN
Definition: or.h:305
Master header file for Tor-specific functionality.
uint32_t inserted_timestamp
Definition: cell_queue_st.h:22
#define CIRCUIT_STATE_OPEN
Definition: circuitlist.h:32
unsigned int n_delete_pending
Definition: circuit_st.h:100
int global_circuitlist_idx
Definition: circuit_st.h:206
HT_GENERATE2(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq, 0.6, tor_reallocarray, tor_free_) static void cdm_diff_free_(cdm_diff_t *diff)
Definition: consdiffmgr.c:222
struct buf_t * inbuf
Definition: connection_st.h:98
Header file for onion_queue.c.
Header file for circuitbuild.c.
int crypto_rand_int(unsigned int max)
void circuit_mark_all_unused_circs(void)
Definition: circuitlist.c:2093
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
Header file for onion_fast.c.
void cpath_free(crypt_path_t *victim)
Definition: crypt_path.c:162
void channel_mark_circid_usable(channel_t *chan, circid_t id)
Definition: circuitlist.c:386
circid_t p_circ_id
Definition: or_circuit_st.h:33
Header file for rephist.c.
static void circuit_about_to_free_atexit(circuit_t *circ)
Definition: circuitlist.c:2286
void smartlist_remove(smartlist_t *sl, const void *element)
Header file containing circuit and connection identifier data for the whole HS subsytem.
STATIC uint32_t circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
Definition: circuitlist.c:2601
#define LOG_WARN
Definition: log.h:53
static smartlist_t * global_origin_circuit_list
Definition: circuitlist.c:123
Header file for circuituse.c.
Extend-info structure.
void cell_queue_init(cell_queue_t *queue)
Definition: relay.c:2575
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:83
void tor_free_(void *mem)
Definition: malloc.c:227
#define CELL_MAX_NETWORK_SIZE
Definition: or.h:579
void smartlist_del(smartlist_t *sl, int idx)
static smartlist_t * circuits_pending_other_guards
Definition: circuitlist.c:130
Header file for hs_circuitmap.c.
#define CIRCUIT_PURPOSE_S_CONNECT_REND
Definition: circuitlist.h:109
#define LD_REND
Definition: log.h:84
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_PATH_BIAS_TESTING
Definition: circuitlist.h:122
int circuit_any_opened_circuits_cached(void)
Definition: circuitlist.c:753
uint16_t marked_for_close
static uint32_t conn_get_buffer_age(const connection_t *conn, uint32_t now_ts)
Definition: circuitlist.c:2560
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:357
size_t tor_compress_get_total_allocation(void)
Definition: compress.c:458
int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
Definition: channel.c:3313
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
STATIC void circuit_free_(circuit_t *circ)
Definition: circuitlist.c:1122
void circuit_dump_by_conn(connection_t *conn, int severity)
Definition: circuitlist.c:1384
unsigned int edge_has_sent_end
void circuit_close_all_marked(void)
Definition: circuitlist.c:660
#define CIRCLAUNCH_NEED_CAPACITY
Definition: circuituse.h:43
int pathbias_check_close(origin_circuit_t *ocirc, int reason)
Client/server directory connection structure.
Header for compress_zstd.c.
#define CIRCUIT_PURPOSE_S_INTRO
Definition: circuitlist.h:106
unsigned int type
Definition: connection_st.h:50
static void cpath_ref_decref(crypt_path_reference_t *cpath_ref)
Definition: circuitlist.c:1349
int circpad_marked_circuit_for_padding(circuit_t *circ, int reason)
unsigned int remaining_relay_early_cells
Definition: or_circuit_st.h:57
uint32_t age_tmp
Definition: circuit_st.h:149
#define CIRCUIT_PURPOSE_HS_VANGUARDS
Definition: circuitlist.h:130
void circuit_cache_opened_circuit_state(int circuits_are_opened)
Definition: circuitlist.c:742
Header file containing circuit data for the whole HS subsytem.
Half-open connection structure.
Header file for connection_edge.c.
STATIC uint32_t circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
Definition: circuitlist.c:2536
edge_connection_t * n_streams
Definition: or_circuit_st.h:39
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:166
static void circuit_remove_from_origin_circuit_list(origin_circuit_t *origin_circ)
Definition: circuitlist.c:627
Header file for crypt_path.c.
size_t tor_compress_state_size(const tor_compress_state_t *state)
Definition: compress.c:639
#define DFLT_IDLE_TIMEOUT_WHILE_LEARNING
Definition: circuitlist.c:1008
channel_t * p_chan
Definition: or_circuit_st.h:37
int package_window
Definition: circuit_st.h:115
const char * circuit_state_to_string(int state)
Definition: circuitlist.c:760
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
size_t buf_allocation(const buf_t *buf)
Definition: buffers.c:401
const char * rend_data_get_address(const rend_data_t *rend_data)
Definition: hs_common.c:527
char rend_cookie[REND_COOKIE_LEN]
Definition: or.h:427
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2112
int node_supports_v3_rendezvous_point(const node_t *node)
Definition: nodelist.c:1183
#define CIRCUIT_PURPOSE_INTRO_POINT
Definition: circuitlist.h:42
Header file for relay.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
crypt_path_t * pending_final_cpath
STATIC smartlist_t * connection_array
Definition: mainloop.c:165
#define CIRCUIT_PURPOSE_S_HSDIR_POST
Definition: circuitlist.h:114
Header for compress_zlib.c.
size_t outbuf_flushlen
smartlist_t * sendme_last_digests
Definition: circuit_st.h:146
origin_circuit_t * origin_circuit_new(void)
Definition: circuitlist.c:1016
static uint8_t get_circuit_purpose_needed_to_cannibalize(uint8_t purpose)
Definition: circuitlist.c:1872
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
Definition: circuitlist.h:78
int circuit_get_cpath_opened_len(const origin_circuit_t *circ)
Definition: circuitlist.c:2057
cpath_build_state_t * build_state
uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now)
Definition: buffers.c:506
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:90
void rep_hist_note_bytes_written(uint64_t num_bytes, time_t when)
Definition: rephist.c:1149
edge_connection_t * resolving_streams
Definition: or_circuit_st.h:42
rend_data_t * rend_data
void circuit_build_failed(origin_circuit_t *circ)
Definition: circuituse.c:1777
circuit_build_times_t * get_circuit_build_times_mutable(void)
Definition: circuitstats.c:89
uint32_t circid_t
Definition: or.h:608
#define CIRCUIT_PURPOSE_OR
Definition: circuitlist.h:39
#define log_fn(severity, domain, args,...)
Definition: log.h:287
#define CIRCUIT_STATE_GUARD_WAIT
Definition: circuitlist.h:30
#define CIRCUIT_STATE_BUILDING
Definition: circuitlist.h:21
#define CONN_TYPE_EXIT
Definition: connection.h:26
void mark_circuit_unusable_for_new_conns(origin_circuit_t *circ)
Definition: circuituse.c:3106
or_circuit_t * or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
Definition: circuitlist.c:1088
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:154
#define CIRCUIT_IS_ORCIRC(c)
Definition: circuitlist.h:153
edge_connection_t * p_streams
crypt_path_reference_t * service_pending_final_cpath_ref
void circuit_free_all(void)
Definition: circuitlist.c:1297
const circuit_build_times_t * get_circuit_build_times(void)
Definition: circuitstats.c:82
#define CIRCUIT_PURPOSE_C_GENERAL
Definition: circuitlist.h:72
struct circuit_guard_state_t * guard_state
time_t approx_time(void)
Definition: approx_time.c:32
static size_t circuit_alloc_in_half_streams(const circuit_t *c)
Definition: circuitlist.c:2515
time_t timestamp_dirty
Definition: circuit_st.h:186
Edge-connection structure.
const char * circuit_purpose_to_controller_string(uint8_t purpose)
Definition: circuitlist.c:780
size_t tor_lzma_get_total_allocation(void)
unsigned int p_delete_pending
Definition: circuit_st.h:97
#define CIRCUIT_STATE_CHAN_WAIT
Definition: circuitlist.h:26
circid_t n_circ_id
Definition: circuit_st.h:77
static uint32_t circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
Definition: circuitlist.c:2580
void circuit_set_n_circid_chan(circuit_t *circ, circid_t id, channel_t *chan)
Definition: circuitlist.c:474
reference-counting structure for crypt_path_t
static void circuit_dump_conn_details(int severity, circuit_t *circ, int conn_array_index, const char *type, circid_t this_circid, circid_t other_circid)
Definition: circuitlist.c:1363
static smartlist_t * circuits_pending_close
Definition: circuitlist.c:134
Header file for buffers.c.
#define CONN_TYPE_DIR
Definition: connection.h:35
Header file for ocirc_event.c.
circuit_t * circuit_get_by_edge_conn(edge_connection_t *conn)
Definition: circuitlist.c:1578
Header file for connection_or.c.
struct crypt_path_t * next
Definition: crypt_path_st.h:72
struct workqueue_entry_s * workqueue_entry
Definition: or_circuit_st.h:30
void channel_mark_circid_unusable(channel_t *chan, circid_t id)
Definition: circuitlist.c:353
void circuit_unlink_all_from_channel(channel_t *chan, int reason)
Definition: circuitlist.c:1595
void circuit_mark_for_close_(circuit_t *circ, int reason, int line, const char *file)
Definition: circuitlist.c:2197
int entry_guard_could_succeed(const circuit_guard_state_t *guard_state)
Definition: entrynodes.c:3457
#define REND_COOKIE_LEN
Definition: or.h:399
static void circuit_add_to_origin_circuit_list(origin_circuit_t *origin_circ)
Definition: circuitlist.c:648
static void circuit_state_publish(const circuit_t *circ)
Definition: circuitlist.c:523
int control_event_circuit_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
circuit_channel_direction_t
Definition: or.h:494
origin_circuit_t * circuit_get_next_by_pk_and_purpose(origin_circuit_t *start, const uint8_t *digest, uint8_t purpose)
Definition: circuitlist.c:1810
void channel_note_destroy_not_pending(channel_t *chan, circid_t id)
Definition: circuitlist.c:429
#define OR_CIRCUIT_MAGIC
Definition: circuit_st.h:31
cell_queue_t n_chan_cells
Definition: circuit_st.h:80
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:112
struct hs_ident_circuit_t * hs_ident
Header file for rendclient.c.
void buf_clear(buf_t *buf)
Definition: buffers.c:381
int circuit_any_opened_circuits(void)
Definition: circuitlist.c:716
Header file for control_events.c.
void smartlist_clear(smartlist_t *sl)
void circuit_set_p_circid_chan(or_circuit_t *or_circ, circid_t id, channel_t *chan)
Definition: circuitlist.c:451
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
smartlist_t * circuit_find_circuits_to_upgrade_from_guard_wait(void)
Definition: circuitlist.c:2013
int nodes_in_same_family(const node_t *node1, const node_t *node2)
Definition: nodelist.c:2062
const char * build_state_get_exit_nickname(cpath_build_state_t *state)
size_t tor_zstd_get_total_allocation(void)
const char * marked_for_close_file
Definition: circuit_st.h:191
smartlist_t * prepend_policy
static size_t marked_circuit_streams_free_bytes(edge_connection_t *stream)
Definition: circuitlist.c:2476
void relay_crypto_clear(relay_crypto_t *crypto)
Definition: relay_crypto.c:265
void channel_unlink_all_circuits(channel_t *chan, smartlist_t *circuits_out)
Definition: relay.c:2799
int channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
Definition: channel.c:2028
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:86
uint32_t monotime_coarse_get_stamp(void)
Definition: compat_time.c:844
Header file for predict_ports.c.
Header file for routerlist.c.
Header for relay_crypto.c.
int circuit_build_times_disabled(const or_options_t *options)
Definition: circuitstats.c:121
uint8_t state
Definition: crypt_path_st.h:68
#define CIRCUIT_PURPOSE_C_CIRCUIT_PADDING
Definition: circuitlist.h:97
#define ORIGIN_CIRCUIT_MAGIC
Definition: circuit_st.h:29
circuit_status_event_t
Definition: ocirc_event.h:19
routerset_t * ExcludeNodes
Definition: or_options_st.h:81
Header file for onion_crypto.c.