2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2001 Matej Pfajfar.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuitlist.c
|
2016-02-27 18:08:24 +01:00
|
|
|
*
|
2016-10-26 18:38:50 +02:00
|
|
|
* \brief Manage global structures that list and index circuits, and
|
|
|
|
* look up circuits within them.
|
|
|
|
*
|
|
|
|
* One of the most frequent operations in Tor occurs every time that
|
|
|
|
* a relay cell arrives on a channel. When that happens, we need to
|
|
|
|
* find which circuit it is associated with, based on the channel and the
|
|
|
|
* circuit ID in the relay cell.
|
|
|
|
*
|
|
|
|
* To handle that, we maintain a global list of circuits, and a hashtable
|
|
|
|
* mapping [channel,circID] pairs to circuits. Circuits are added to and
|
|
|
|
* removed from this mapping using circuit_set_p_circid_chan() and
|
|
|
|
* circuit_set_n_circid_chan(). To look up a circuit from this map, most
|
|
|
|
* callers should use circuit_get_by_circid_channel(), though
|
|
|
|
* circuit_get_by_circid_channel_even_if_marked() is appropriate under some
|
|
|
|
* circumstances.
|
|
|
|
*
|
|
|
|
* We also need to allow for the possibility that we have blocked use of a
|
|
|
|
* circuit ID (because we are waiting to send a DESTROY cell), but the
|
|
|
|
* circuit is not there any more. For that case, we allow placeholder
|
|
|
|
* entries in the table, using channel_mark_circid_unusable().
|
|
|
|
*
|
|
|
|
* To efficiently handle a channel that has just opened, we also maintain a
|
|
|
|
* list of the circuits waiting for channels, so we can attach them as
|
|
|
|
* needed without iterating through the whole list of circuits, using
|
|
|
|
* circuit_get_all_pending_on_channel().
|
|
|
|
*
|
|
|
|
* In this module, we also handle the list of circuits that have been
|
|
|
|
* marked for close elsewhere, and close them as needed. (We use this
|
|
|
|
* "mark now, close later" pattern here and elsewhere to avoid
|
|
|
|
* unpredictable recursion if we closed every circuit immediately upon
|
|
|
|
* realizing it needed to close.) See circuit_mark_for_close() for the
|
|
|
|
* mark function, and circuit_close_all_marked() for the close function.
|
|
|
|
*
|
|
|
|
* For hidden services, we need to be able to look up introduction point
|
|
|
|
* circuits and rendezvous circuits by cookie, key, etc. These are
|
|
|
|
* currently handled with linear searches in
|
|
|
|
* circuit_get_ready_rend_circuit_by_rend_data(),
|
|
|
|
* circuit_get_next_by_pk_and_purpose(), and with hash lookups in
|
|
|
|
* circuit_get_rendezvous() and circuit_get_intro_point().
|
|
|
|
*
|
|
|
|
* This module is also the entry point for our out-of-memory handler
|
|
|
|
* logic, which was originally circuit-focused.
|
2004-05-13 09:24:49 +02:00
|
|
|
**/
|
2013-07-10 21:07:32 +02:00
|
|
|
#define CIRCUITLIST_PRIVATE
|
2018-01-12 17:44:30 +01:00
|
|
|
#include "torint.h" /* TOR_PRIuSZ */
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
#include "or.h"
|
2012-08-08 15:02:47 +02:00
|
|
|
#include "channel.h"
|
2013-10-31 19:33:34 +01:00
|
|
|
#include "circpathbias.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2010-07-22 10:03:40 +02:00
|
|
|
#include "circuituse.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "circuitstats.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:43:02 +02:00
|
|
|
#include "connection_edge.h"
|
2010-07-22 10:50:34 +02:00
|
|
|
#include "connection_or.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2016-11-18 22:05:09 +01:00
|
|
|
#include "entrynodes.h"
|
2014-08-19 17:25:48 +02:00
|
|
|
#include "main.h"
|
2017-02-16 21:55:12 +01:00
|
|
|
#include "hs_circuit.h"
|
2016-12-16 17:20:59 +01:00
|
|
|
#include "hs_circuitmap.h"
|
2017-05-12 17:39:46 +02:00
|
|
|
#include "hs_ident.h"
|
2010-07-23 20:18:55 +02:00
|
|
|
#include "networkstatus.h"
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
#include "nodelist.h"
|
2010-07-23 20:38:25 +02:00
|
|
|
#include "onion.h"
|
2012-12-04 22:51:31 +01:00
|
|
|
#include "onion_fast.h"
|
2013-03-19 17:29:08 +01:00
|
|
|
#include "policies.h"
|
2010-07-23 21:53:11 +02:00
|
|
|
#include "relay.h"
|
2018-03-17 15:59:15 +01:00
|
|
|
#include "relay_crypto.h"
|
2010-07-22 00:13:51 +02:00
|
|
|
#include "rendclient.h"
|
2010-07-21 17:52:54 +02:00
|
|
|
#include "rendcommon.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2012-09-13 18:46:39 +02:00
|
|
|
#include "routerset.h"
|
2017-02-22 03:28:00 +01:00
|
|
|
#include "channelpadding.h"
|
2018-02-27 22:55:10 +01:00
|
|
|
#include "compress_lzma.h"
|
|
|
|
#include "compress_zlib.h"
|
|
|
|
#include "compress_zstd.h"
|
2013-09-27 17:54:36 +02:00
|
|
|
|
2007-08-08 07:50:31 +02:00
|
|
|
#include "ht.h"
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
/** A global list of all circuits at this hop. */
|
2014-08-15 21:42:14 +02:00
|
|
|
static smartlist_t *global_circuitlist = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2016-11-22 15:05:52 +01:00
|
|
|
/** A global list of all origin circuits. Every element of this is also
|
|
|
|
* an element of global_circuitlist. */
|
|
|
|
static smartlist_t *global_origin_circuit_list = NULL;
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
|
|
|
|
static smartlist_t *circuits_pending_chans = NULL;
|
2005-12-03 03:12:37 +01:00
|
|
|
|
2016-11-21 23:23:25 +01:00
|
|
|
/** List of all the (origin) circuits whose state is
|
|
|
|
* CIRCUIT_STATE_GUARD_WAIT. */
|
|
|
|
static smartlist_t *circuits_pending_other_guards = NULL;
|
|
|
|
|
2015-11-13 19:46:47 +01:00
|
|
|
/** A list of all the circuits that have been marked with
|
|
|
|
* circuit_mark_for_close and which are waiting for circuit_about_to_free. */
|
2015-10-02 17:55:25 +02:00
|
|
|
static smartlist_t *circuits_pending_close = NULL;
|
|
|
|
|
2004-11-10 15:28:04 +01:00
|
|
|
static void circuit_free_cpath_node(crypt_path_t *victim);
|
2012-01-07 07:44:20 +01:00
|
|
|
static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
|
2016-02-11 18:15:12 +01:00
|
|
|
static void circuit_about_to_free_atexit(circuit_t *circ);
|
2015-10-02 17:55:25 +02:00
|
|
|
static void circuit_about_to_free(circuit_t *circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2017-12-07 01:04:09 +01:00
|
|
|
/**
|
|
|
|
* A cached value of the current state of the origin circuit list. Has the
|
|
|
|
* value 1 if we saw any opened circuits recently (since the last call to
|
|
|
|
* circuit_any_opened_circuits(), which gets called around once a second by
|
|
|
|
* circuit_expire_building). 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int any_opened_circs_cached_val = 0;
|
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** A map from channel and circuit ID to circuit. (Lookup performance is
|
2005-06-29 23:46:55 +02:00
|
|
|
* very important here, since we need to do it every time a cell arrives.) */
|
2012-08-08 15:02:47 +02:00
|
|
|
typedef struct chan_circid_circuit_map_t {
|
|
|
|
HT_ENTRY(chan_circid_circuit_map_t) node;
|
|
|
|
channel_t *chan;
|
2008-07-23 17:58:30 +02:00
|
|
|
circid_t circ_id;
|
2005-04-06 07:33:32 +02:00
|
|
|
circuit_t *circuit;
|
2014-06-10 19:35:45 +02:00
|
|
|
/* For debugging 12184: when was this placeholder item added? */
|
|
|
|
time_t made_placeholder_at;
|
2012-08-08 15:02:47 +02:00
|
|
|
} chan_circid_circuit_map_t;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** Helper for hash tables: compare the channel and circuit ID for a and
|
2005-12-14 21:40:40 +01:00
|
|
|
* b, and return less than, equal to, or greater than zero appropriately.
|
2005-06-29 23:46:55 +02:00
|
|
|
*/
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2012-10-12 18:22:13 +02:00
|
|
|
chan_circid_entries_eq_(chan_circid_circuit_map_t *a,
|
2012-08-08 15:02:47 +02:00
|
|
|
chan_circid_circuit_map_t *b)
|
2005-11-23 05:18:45 +01:00
|
|
|
{
|
2012-08-08 15:02:47 +02:00
|
|
|
return a->chan == b->chan && a->circ_id == b->circ_id;
|
2005-11-23 05:18:45 +01:00
|
|
|
}
|
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Helper: return a hash based on circuit ID and the pointer value of
|
2012-08-08 15:02:47 +02:00
|
|
|
* chan in <b>a</b>. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline unsigned int
|
2012-10-12 18:22:13 +02:00
|
|
|
chan_circid_entry_hash_(chan_circid_circuit_map_t *a)
|
2005-04-06 07:33:32 +02:00
|
|
|
{
|
2014-05-06 18:27:18 +02:00
|
|
|
/* Try to squeze the siphash input into 8 bytes to save any extra siphash
|
|
|
|
* rounds. This hash function is in the critical path. */
|
|
|
|
uintptr_t chan = (uintptr_t) (void*) a->chan;
|
|
|
|
uint32_t array[2];
|
|
|
|
array[0] = a->circ_id;
|
|
|
|
/* The low bits of the channel pointer are uninteresting, since the channel
|
|
|
|
* is a pretty big structure. */
|
|
|
|
array[1] = (uint32_t) (chan >> 6);
|
|
|
|
return (unsigned) siphash24g(array, sizeof(array));
|
2005-11-23 05:18:45 +01:00
|
|
|
}
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** Map from [chan,circid] to circuit. */
|
|
|
|
static HT_HEAD(chan_circid_map, chan_circid_circuit_map_t)
|
|
|
|
chan_circid_map = HT_INITIALIZER();
|
|
|
|
HT_PROTOTYPE(chan_circid_map, chan_circid_circuit_map_t, node,
|
2012-10-12 18:22:13 +02:00
|
|
|
chan_circid_entry_hash_, chan_circid_entries_eq_)
|
2014-09-02 18:48:34 +02:00
|
|
|
HT_GENERATE2(chan_circid_map, chan_circid_circuit_map_t, node,
|
|
|
|
chan_circid_entry_hash_, chan_circid_entries_eq_, 0.6,
|
|
|
|
tor_reallocarray_, tor_free_)
|
2005-04-07 07:09:19 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** The most recently returned entry from circuit_get_by_circid_chan;
|
2005-07-14 10:43:19 +02:00
|
|
|
* used to improve performance when many cells arrive in a row from the
|
|
|
|
* same circuit.
|
2005-06-29 23:46:55 +02:00
|
|
|
*/
|
2016-06-02 15:46:12 +02:00
|
|
|
static chan_circid_circuit_map_t *_last_circid_chan_ent = NULL;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
|
|
|
|
* and/or channel for circ has just changed from <b>old_chan, old_id</b>
|
|
|
|
* to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
|
2010-10-15 19:18:24 +02:00
|
|
|
* the old entry (if any) and adding a new one. */
|
2006-07-23 09:37:35 +02:00
|
|
|
static void
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_set_circid_chan_helper(circuit_t *circ, int direction,
|
|
|
|
circid_t id,
|
|
|
|
channel_t *chan)
|
2005-04-06 07:33:32 +02:00
|
|
|
{
|
2012-08-08 15:02:47 +02:00
|
|
|
chan_circid_circuit_map_t search;
|
|
|
|
chan_circid_circuit_map_t *found;
|
|
|
|
channel_t *old_chan, **chan_ptr;
|
2008-07-23 17:58:30 +02:00
|
|
|
circid_t old_id, *circid_ptr;
|
2012-10-11 03:25:52 +02:00
|
|
|
int make_active, attached = 0;
|
2007-04-09 22:09:28 +02:00
|
|
|
|
|
|
|
if (direction == CELL_DIRECTION_OUT) {
|
2012-08-08 15:02:47 +02:00
|
|
|
chan_ptr = &circ->n_chan;
|
2007-04-09 22:09:28 +02:00
|
|
|
circid_ptr = &circ->n_circ_id;
|
2012-08-08 15:02:47 +02:00
|
|
|
make_active = circ->n_chan_cells.n > 0;
|
2007-04-09 22:09:28 +02:00
|
|
|
} else {
|
|
|
|
or_circuit_t *c = TO_OR_CIRCUIT(circ);
|
2012-08-08 15:02:47 +02:00
|
|
|
chan_ptr = &c->p_chan;
|
2007-04-09 22:09:28 +02:00
|
|
|
circid_ptr = &c->p_circ_id;
|
2012-08-08 15:02:47 +02:00
|
|
|
make_active = c->p_chan_cells.n > 0;
|
2007-04-09 22:09:28 +02:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
old_chan = *chan_ptr;
|
2007-04-09 22:09:28 +02:00
|
|
|
old_id = *circid_ptr;
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (id == old_id && chan == old_chan)
|
2007-04-09 22:09:28 +02:00
|
|
|
return;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (_last_circid_chan_ent &&
|
|
|
|
((old_id == _last_circid_chan_ent->circ_id &&
|
|
|
|
old_chan == _last_circid_chan_ent->chan) ||
|
|
|
|
(id == _last_circid_chan_ent->circ_id &&
|
|
|
|
chan == _last_circid_chan_ent->chan))) {
|
|
|
|
_last_circid_chan_ent = NULL;
|
2005-04-07 07:09:19 +02:00
|
|
|
}
|
|
|
|
|
2012-10-01 19:39:40 +02:00
|
|
|
if (old_chan) {
|
|
|
|
/*
|
|
|
|
* If we're changing channels or ID and had an old channel and a non
|
2012-10-01 22:06:10 +02:00
|
|
|
* zero old ID and weren't marked for close (i.e., we should have been
|
|
|
|
* attached), detach the circuit. ID changes require this because
|
|
|
|
* circuitmux hashes on (channel_id, circuit_id).
|
2012-10-01 19:39:40 +02:00
|
|
|
*/
|
2012-10-01 23:22:53 +02:00
|
|
|
if (old_id != 0 && (old_chan != chan || old_id != id) &&
|
2012-10-01 22:06:10 +02:00
|
|
|
!(circ->marked_for_close)) {
|
2012-10-01 19:39:40 +02:00
|
|
|
tor_assert(old_chan->cmux);
|
|
|
|
circuitmux_detach_circuit(old_chan->cmux, circ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we may need to remove it from the conn-circid map */
|
2005-04-06 07:33:32 +02:00
|
|
|
search.circ_id = old_id;
|
2012-08-08 15:02:47 +02:00
|
|
|
search.chan = old_chan;
|
|
|
|
found = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found) {
|
2005-11-23 05:18:45 +01:00
|
|
|
tor_free(found);
|
2012-09-21 23:46:22 +02:00
|
|
|
if (direction == CELL_DIRECTION_OUT) {
|
|
|
|
/* One fewer circuits use old_chan as n_chan */
|
|
|
|
--(old_chan->num_n_circuits);
|
|
|
|
} else {
|
|
|
|
/* One fewer circuits use old_chan as p_chan */
|
|
|
|
--(old_chan->num_p_circuits);
|
|
|
|
}
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
|
|
|
|
2007-04-09 22:09:28 +02:00
|
|
|
/* Change the values only after we have possibly made the circuit inactive
|
2012-08-08 15:02:47 +02:00
|
|
|
* on the previous chan. */
|
|
|
|
*chan_ptr = chan;
|
2007-04-09 22:09:28 +02:00
|
|
|
*circid_ptr = id;
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (chan == NULL)
|
2005-04-06 07:33:32 +02:00
|
|
|
return;
|
|
|
|
|
2005-11-11 20:25:30 +01:00
|
|
|
/* now add the new one to the conn-circid map */
|
2005-04-06 07:33:32 +02:00
|
|
|
search.circ_id = id;
|
2012-08-08 15:02:47 +02:00
|
|
|
search.chan = chan;
|
|
|
|
found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found) {
|
|
|
|
found->circuit = circ;
|
2014-06-10 19:35:45 +02:00
|
|
|
found->made_placeholder_at = 0;
|
2005-04-06 07:33:32 +02:00
|
|
|
} else {
|
2012-08-08 15:02:47 +02:00
|
|
|
found = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
|
2005-04-06 07:33:32 +02:00
|
|
|
found->circ_id = id;
|
2012-08-08 15:02:47 +02:00
|
|
|
found->chan = chan;
|
2005-04-06 07:33:32 +02:00
|
|
|
found->circuit = circ;
|
2012-08-08 15:02:47 +02:00
|
|
|
HT_INSERT(chan_circid_map, &chan_circid_map, found);
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
2012-09-21 23:46:22 +02:00
|
|
|
|
2012-10-01 19:39:40 +02:00
|
|
|
/*
|
|
|
|
* Attach to the circuitmux if we're changing channels or IDs and
|
2012-10-01 22:06:10 +02:00
|
|
|
* have a new channel and ID to use and the circuit is not marked for
|
|
|
|
* close.
|
2012-10-01 19:39:40 +02:00
|
|
|
*/
|
2012-10-01 22:06:10 +02:00
|
|
|
if (chan && id != 0 && (old_chan != chan || old_id != id) &&
|
|
|
|
!(circ->marked_for_close)) {
|
2012-09-21 23:46:22 +02:00
|
|
|
tor_assert(chan->cmux);
|
|
|
|
circuitmux_attach_circuit(chan->cmux, circ, direction);
|
2012-10-01 19:39:40 +02:00
|
|
|
attached = 1;
|
2012-09-21 23:46:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a no-op if we have no cells, but if we do it marks us active to
|
|
|
|
* the circuitmux
|
|
|
|
*/
|
2012-10-01 19:39:40 +02:00
|
|
|
if (make_active && attached)
|
2012-09-21 23:46:22 +02:00
|
|
|
update_circuit_on_cmux(circ, direction);
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2012-09-21 23:46:22 +02:00
|
|
|
/* Adjust circuit counts on new channel */
|
|
|
|
if (direction == CELL_DIRECTION_OUT) {
|
|
|
|
++chan->num_n_circuits;
|
|
|
|
} else {
|
|
|
|
++chan->num_p_circuits;
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
|
|
|
|
2013-03-14 17:13:45 +01:00
|
|
|
/** Mark that circuit id <b>id</b> shouldn't be used on channel <b>chan</b>,
|
|
|
|
* even if there is no circuit on the channel. We use this to keep the
|
|
|
|
* circuit id from getting re-used while we have queued but not yet sent
|
|
|
|
* a destroy cell. */
|
|
|
|
void
|
|
|
|
channel_mark_circid_unusable(channel_t *chan, circid_t id)
|
|
|
|
{
|
|
|
|
chan_circid_circuit_map_t search;
|
|
|
|
chan_circid_circuit_map_t *ent;
|
|
|
|
|
|
|
|
/* See if there's an entry there. That wouldn't be good. */
|
|
|
|
memset(&search, 0, sizeof(search));
|
|
|
|
search.chan = chan;
|
|
|
|
search.circ_id = id;
|
|
|
|
ent = HT_FIND(chan_circid_map, &chan_circid_map, &search);
|
|
|
|
|
|
|
|
if (ent && ent->circuit) {
|
|
|
|
/* we have a problem. */
|
|
|
|
log_warn(LD_BUG, "Tried to mark %u unusable on %p, but there was already "
|
|
|
|
"a circuit there.", (unsigned)id, chan);
|
|
|
|
} else if (ent) {
|
|
|
|
/* It's already marked. */
|
2014-06-10 19:35:45 +02:00
|
|
|
if (!ent->made_placeholder_at)
|
|
|
|
ent->made_placeholder_at = approx_time();
|
2013-03-14 17:13:45 +01:00
|
|
|
} else {
|
|
|
|
ent = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
|
|
|
|
ent->chan = chan;
|
|
|
|
ent->circ_id = id;
|
2014-06-10 19:35:45 +02:00
|
|
|
/* leave circuit at NULL. */
|
|
|
|
ent->made_placeholder_at = approx_time();
|
2013-03-14 17:13:45 +01:00
|
|
|
HT_INSERT(chan_circid_map, &chan_circid_map, ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Mark that a circuit id <b>id</b> can be used again on <b>chan</b>.
|
|
|
|
* We use this to re-enable the circuit ID after we've sent a destroy cell.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
channel_mark_circid_usable(channel_t *chan, circid_t id)
|
|
|
|
{
|
|
|
|
chan_circid_circuit_map_t search;
|
|
|
|
chan_circid_circuit_map_t *ent;
|
|
|
|
|
|
|
|
/* See if there's an entry there. That wouldn't be good. */
|
|
|
|
memset(&search, 0, sizeof(search));
|
|
|
|
search.chan = chan;
|
|
|
|
search.circ_id = id;
|
|
|
|
ent = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
|
|
|
|
if (ent && ent->circuit) {
|
|
|
|
log_warn(LD_BUG, "Tried to mark %u usable on %p, but there was already "
|
|
|
|
"a circuit there.", (unsigned)id, chan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_last_circid_chan_ent == ent)
|
|
|
|
_last_circid_chan_ent = NULL;
|
|
|
|
tor_free(ent);
|
|
|
|
}
|
|
|
|
|
2013-03-15 15:45:48 +01:00
|
|
|
/** Called to indicate that a DESTROY is pending on <b>chan</b> with
|
|
|
|
* circuit ID <b>id</b>, but hasn't been sent yet. */
|
|
|
|
void
|
|
|
|
channel_note_destroy_pending(channel_t *chan, circid_t id)
|
|
|
|
{
|
|
|
|
circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
|
|
|
|
if (circ) {
|
|
|
|
if (circ->n_chan == chan && circ->n_circ_id == id) {
|
|
|
|
circ->n_delete_pending = 1;
|
|
|
|
} else {
|
|
|
|
or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
|
|
|
|
if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
|
|
|
|
circ->p_delete_pending = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
channel_mark_circid_unusable(chan, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called to indicate that a DESTROY is no longer pending on <b>chan</b> with
|
|
|
|
* circuit ID <b>id</b> -- typically, because it has been sent. */
|
2017-05-26 20:05:50 +02:00
|
|
|
MOCK_IMPL(void,
|
|
|
|
channel_note_destroy_not_pending,(channel_t *chan, circid_t id))
|
2013-03-15 15:45:48 +01:00
|
|
|
{
|
|
|
|
circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
|
|
|
|
if (circ) {
|
|
|
|
if (circ->n_chan == chan && circ->n_circ_id == id) {
|
|
|
|
circ->n_delete_pending = 0;
|
|
|
|
} else {
|
|
|
|
or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
|
|
|
|
if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
|
|
|
|
circ->p_delete_pending = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* XXXX this shouldn't happen; log a bug here. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
channel_mark_circid_usable(chan, id);
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** Set the p_conn field of a circuit <b>circ</b>, along
|
|
|
|
* with the corresponding circuit ID, and add the circuit as appropriate
|
2012-08-08 15:02:47 +02:00
|
|
|
* to the (chan,id)-\>circuit map. */
|
2006-07-23 09:37:35 +02:00
|
|
|
void
|
2013-03-15 15:45:48 +01:00
|
|
|
circuit_set_p_circid_chan(or_circuit_t *or_circ, circid_t id,
|
2012-08-08 15:02:47 +02:00
|
|
|
channel_t *chan)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
2013-03-15 15:45:48 +01:00
|
|
|
circuit_t *circ = TO_CIRCUIT(or_circ);
|
|
|
|
channel_t *old_chan = or_circ->p_chan;
|
|
|
|
circid_t old_id = or_circ->p_circ_id;
|
|
|
|
|
|
|
|
circuit_set_circid_chan_helper(circ, CELL_DIRECTION_IN, id, chan);
|
2007-04-09 22:09:28 +02:00
|
|
|
|
2014-05-16 16:32:31 +02:00
|
|
|
if (chan) {
|
|
|
|
chan->timestamp_last_had_circuits = approx_time();
|
|
|
|
}
|
2014-06-11 17:57:56 +02:00
|
|
|
|
2013-03-15 15:45:48 +01:00
|
|
|
if (circ->p_delete_pending && old_chan) {
|
|
|
|
channel_mark_circid_unusable(old_chan, old_id);
|
|
|
|
circ->p_delete_pending = 0;
|
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the n_conn field of a circuit <b>circ</b>, along
|
|
|
|
* with the corresponding circuit ID, and add the circuit as appropriate
|
2012-08-08 15:02:47 +02:00
|
|
|
* to the (chan,id)-\>circuit map. */
|
2006-07-23 09:37:35 +02:00
|
|
|
void
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_set_n_circid_chan(circuit_t *circ, circid_t id,
|
|
|
|
channel_t *chan)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
2013-03-15 15:45:48 +01:00
|
|
|
channel_t *old_chan = circ->n_chan;
|
|
|
|
circid_t old_id = circ->n_circ_id;
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_set_circid_chan_helper(circ, CELL_DIRECTION_OUT, id, chan);
|
2007-04-09 22:09:28 +02:00
|
|
|
|
2014-05-16 16:32:31 +02:00
|
|
|
if (chan) {
|
|
|
|
chan->timestamp_last_had_circuits = approx_time();
|
|
|
|
}
|
2014-06-11 17:57:56 +02:00
|
|
|
|
2013-03-15 15:45:48 +01:00
|
|
|
if (circ->n_delete_pending && old_chan) {
|
|
|
|
channel_mark_circid_unusable(old_chan, old_id);
|
2013-03-21 19:51:27 +01:00
|
|
|
circ->n_delete_pending = 0;
|
2013-03-15 15:45:48 +01:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
|
2005-12-03 17:32:29 +01:00
|
|
|
/** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
|
|
|
|
* it from lists as appropriate. */
|
2005-12-03 03:12:37 +01:00
|
|
|
void
|
2008-02-20 02:01:09 +01:00
|
|
|
circuit_set_state(circuit_t *circ, uint8_t state)
|
2005-12-03 03:12:37 +01:00
|
|
|
{
|
|
|
|
tor_assert(circ);
|
|
|
|
if (state == circ->state)
|
|
|
|
return;
|
2016-11-21 23:23:25 +01:00
|
|
|
if (PREDICT_UNLIKELY(!circuits_pending_chans))
|
2012-08-08 15:02:47 +02:00
|
|
|
circuits_pending_chans = smartlist_new();
|
2016-11-21 23:23:25 +01:00
|
|
|
if (PREDICT_UNLIKELY(!circuits_pending_other_guards))
|
|
|
|
circuits_pending_other_guards = smartlist_new();
|
2012-08-08 15:02:47 +02:00
|
|
|
if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
|
2005-12-03 03:12:37 +01:00
|
|
|
/* remove from waiting-circuit list. */
|
2012-08-08 15:02:47 +02:00
|
|
|
smartlist_remove(circuits_pending_chans, circ);
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (state == CIRCUIT_STATE_CHAN_WAIT) {
|
2005-12-03 03:12:37 +01:00
|
|
|
/* add to waiting-circuit list. */
|
2012-08-08 15:02:47 +02:00
|
|
|
smartlist_add(circuits_pending_chans, circ);
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
2016-11-21 22:59:00 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_GUARD_WAIT) {
|
|
|
|
smartlist_remove(circuits_pending_other_guards, circ);
|
|
|
|
}
|
|
|
|
if (state == CIRCUIT_STATE_GUARD_WAIT) {
|
|
|
|
smartlist_add(circuits_pending_other_guards, circ);
|
|
|
|
}
|
|
|
|
if (state == CIRCUIT_STATE_GUARD_WAIT || state == CIRCUIT_STATE_OPEN)
|
2012-12-06 05:07:49 +01:00
|
|
|
tor_assert(!circ->n_chan_create_cell);
|
2011-07-19 19:51:43 +02:00
|
|
|
circ->state = state;
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
|
2007-02-05 18:42:40 +01:00
|
|
|
* the given connection. */
|
|
|
|
void
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
|
2007-02-05 18:42:40 +01:00
|
|
|
{
|
|
|
|
tor_assert(out);
|
2012-08-08 15:02:47 +02:00
|
|
|
tor_assert(chan);
|
2007-02-05 18:42:40 +01:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (!circuits_pending_chans)
|
2007-02-05 18:42:40 +01:00
|
|
|
return;
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuits_pending_chans, circuit_t *, circ) {
|
2007-02-05 18:42:40 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
2008-07-30 15:04:32 +02:00
|
|
|
if (!circ->n_hop)
|
|
|
|
continue;
|
2012-08-08 15:02:47 +02:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_CHAN_WAIT);
|
2008-07-30 15:04:32 +02:00
|
|
|
if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
|
2007-06-15 08:01:04 +02:00
|
|
|
/* Look at addr/port. This is an unkeyed connection. */
|
2012-08-08 15:02:47 +02:00
|
|
|
if (!channel_matches_extend_info(chan, circ->n_hop))
|
2007-06-15 08:01:04 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* We expected a key. See if it's the right one. */
|
2012-10-09 09:51:33 +02:00
|
|
|
if (tor_memneq(chan->identity_digest,
|
|
|
|
circ->n_hop->identity_digest, DIGEST_LEN))
|
2007-06-15 08:01:04 +02:00
|
|
|
continue;
|
2007-02-05 18:42:40 +01:00
|
|
|
}
|
2007-06-15 08:01:04 +02:00
|
|
|
smartlist_add(out, circ);
|
2008-08-05 22:08:19 +02:00
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
2007-02-05 18:42:40 +01:00
|
|
|
}
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** Return the number of circuits in state CHAN_WAIT, waiting for the given
|
|
|
|
* channel. */
|
2007-02-05 18:42:40 +01:00
|
|
|
int
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_count_pending_on_channel(channel_t *chan)
|
2007-02-05 18:42:40 +01:00
|
|
|
{
|
|
|
|
int cnt;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *sl = smartlist_new();
|
2012-08-08 15:02:47 +02:00
|
|
|
|
|
|
|
tor_assert(chan);
|
|
|
|
|
|
|
|
circuit_get_all_pending_on_channel(sl, chan);
|
2007-02-05 18:42:40 +01:00
|
|
|
cnt = smartlist_len(sl);
|
|
|
|
smartlist_free(sl);
|
2017-11-21 21:13:20 +01:00
|
|
|
log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
|
2012-08-08 15:02:47 +02:00
|
|
|
channel_get_canonical_remote_descr(chan),
|
2011-05-16 03:58:46 +02:00
|
|
|
cnt);
|
2007-02-05 18:42:40 +01:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:06:47 +01:00
|
|
|
/** Remove <b>origin_circ</b> from the global list of origin circuits.
|
|
|
|
* Called when we are freeing a circuit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_remove_from_origin_circuit_list(origin_circuit_t *origin_circ)
|
|
|
|
{
|
|
|
|
int origin_idx = origin_circ->global_origin_circuit_list_idx;
|
|
|
|
if (origin_idx < 0)
|
|
|
|
return;
|
|
|
|
origin_circuit_t *c2;
|
|
|
|
tor_assert(origin_idx <= smartlist_len(global_origin_circuit_list));
|
|
|
|
c2 = smartlist_get(global_origin_circuit_list, origin_idx);
|
|
|
|
tor_assert(origin_circ == c2);
|
|
|
|
smartlist_del(global_origin_circuit_list, origin_idx);
|
|
|
|
if (origin_idx < smartlist_len(global_origin_circuit_list)) {
|
|
|
|
origin_circuit_t *replacement =
|
|
|
|
smartlist_get(global_origin_circuit_list, origin_idx);
|
|
|
|
replacement->global_origin_circuit_list_idx = origin_idx;
|
|
|
|
}
|
|
|
|
origin_circ->global_origin_circuit_list_idx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add <b>origin_circ</b> to the global list of origin circuits. Called
|
|
|
|
* when creating the circuit. */
|
|
|
|
static void
|
|
|
|
circuit_add_to_origin_circuit_list(origin_circuit_t *origin_circ)
|
|
|
|
{
|
|
|
|
tor_assert(origin_circ->global_origin_circuit_list_idx == -1);
|
|
|
|
smartlist_t *lst = circuit_get_global_origin_circuit_list();
|
|
|
|
smartlist_add(lst, origin_circ);
|
|
|
|
origin_circ->global_origin_circuit_list_idx = smartlist_len(lst) - 1;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Detach from the global circuit list, and deallocate, all
|
|
|
|
* circuits that have been marked for close.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_close_all_marked(void)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2015-10-02 17:55:25 +02:00
|
|
|
if (circuits_pending_close == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
smartlist_t *lst = circuit_get_global_list();
|
2015-10-02 17:55:25 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuits_pending_close, circuit_t *, circ) {
|
|
|
|
tor_assert(circ->marked_for_close);
|
|
|
|
|
|
|
|
/* Remove it from the circuit list. */
|
|
|
|
int idx = circ->global_circuitlist_idx;
|
|
|
|
smartlist_del(lst, idx);
|
|
|
|
if (idx < smartlist_len(lst)) {
|
|
|
|
circuit_t *replacement = smartlist_get(lst, idx);
|
|
|
|
replacement->global_circuitlist_idx = idx;
|
2014-08-15 23:02:28 +02:00
|
|
|
}
|
2015-10-02 17:55:25 +02:00
|
|
|
circ->global_circuitlist_idx = -1;
|
|
|
|
|
2016-11-22 15:05:52 +01:00
|
|
|
/* Remove it from the origin circuit list, if appropriate. */
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2017-01-02 19:06:47 +01:00
|
|
|
circuit_remove_from_origin_circuit_list(TO_ORIGIN_CIRCUIT(circ));
|
2016-11-22 15:05:52 +01:00
|
|
|
}
|
|
|
|
|
2015-10-02 17:55:25 +02:00
|
|
|
circuit_about_to_free(circ);
|
|
|
|
circuit_free(circ);
|
2014-08-15 23:02:28 +02:00
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
2015-10-02 17:55:25 +02:00
|
|
|
|
|
|
|
smartlist_clear(circuits_pending_close);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 18:53:00 +01:00
|
|
|
/** Return a pointer to the global list of circuits. */
|
2014-08-15 21:42:14 +02:00
|
|
|
MOCK_IMPL(smartlist_t *,
|
2014-04-15 14:20:34 +02:00
|
|
|
circuit_get_global_list,(void))
|
2005-06-19 22:40:41 +02:00
|
|
|
{
|
2014-08-15 21:42:14 +02:00
|
|
|
if (NULL == global_circuitlist)
|
|
|
|
global_circuitlist = smartlist_new();
|
|
|
|
return global_circuitlist;
|
2005-06-19 22:40:41 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 18:53:00 +01:00
|
|
|
/** Return a pointer to the global list of origin circuits. */
|
|
|
|
smartlist_t *
|
|
|
|
circuit_get_global_origin_circuit_list(void)
|
|
|
|
{
|
|
|
|
if (NULL == global_origin_circuit_list)
|
|
|
|
global_origin_circuit_list = smartlist_new();
|
2017-01-12 19:18:09 +01:00
|
|
|
return global_origin_circuit_list;
|
2016-11-25 18:53:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 01:04:09 +01:00
|
|
|
/**
|
|
|
|
* Return true if we have any opened general-purpose 3 hop
|
|
|
|
* origin circuits.
|
|
|
|
*
|
|
|
|
* The result from this function is cached for use by
|
|
|
|
* circuit_any_opened_circuits_cached().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
circuit_any_opened_circuits(void)
|
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_origin_circuit_list(),
|
|
|
|
const origin_circuit_t *, next_circ) {
|
|
|
|
if (!TO_CIRCUIT(next_circ)->marked_for_close &&
|
|
|
|
next_circ->has_opened &&
|
|
|
|
TO_CIRCUIT(next_circ)->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
TO_CIRCUIT(next_circ)->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT &&
|
|
|
|
next_circ->build_state &&
|
|
|
|
next_circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN) {
|
|
|
|
circuit_cache_opened_circuit_state(1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(next_circ);
|
|
|
|
|
|
|
|
circuit_cache_opened_circuit_state(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache the "any circuits opened" state, as specified in param
|
|
|
|
* circuits_are_opened. This is a helper function to update
|
|
|
|
* the circuit opened status whenever we happen to look at the
|
|
|
|
* circuit list.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circuit_cache_opened_circuit_state(int circuits_are_opened)
|
|
|
|
{
|
|
|
|
any_opened_circs_cached_val = circuits_are_opened;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if there were any opened circuits since the last call to
|
|
|
|
* circuit_any_opened_circuits(), or since circuit_expire_building() last
|
|
|
|
* ran (it runs roughly once per second).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
circuit_any_opened_circuits_cached(void)
|
|
|
|
{
|
|
|
|
return any_opened_circs_cached_val;
|
|
|
|
}
|
|
|
|
|
2005-04-07 23:07:19 +02:00
|
|
|
/** Function to make circ-\>state human-readable */
|
|
|
|
const char *
|
2005-09-30 03:09:52 +02:00
|
|
|
circuit_state_to_string(int state)
|
|
|
|
{
|
2005-04-08 05:07:07 +02:00
|
|
|
static char buf[64];
|
2005-04-07 23:07:19 +02:00
|
|
|
switch (state) {
|
|
|
|
case CIRCUIT_STATE_BUILDING: return "doing handshakes";
|
|
|
|
case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
|
2012-08-08 15:02:47 +02:00
|
|
|
case CIRCUIT_STATE_CHAN_WAIT: return "connecting to server";
|
2016-11-21 22:59:00 +01:00
|
|
|
case CIRCUIT_STATE_GUARD_WAIT: return "waiting to see how other "
|
|
|
|
"guards perform";
|
2005-04-07 23:07:19 +02:00
|
|
|
case CIRCUIT_STATE_OPEN: return "open";
|
|
|
|
default:
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "Unknown circuit state %d", state);
|
2005-04-08 05:26:23 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
|
2005-04-07 23:07:19 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 00:34:22 +02:00
|
|
|
/** Map a circuit purpose to a string suitable to be displayed to a
|
|
|
|
* controller. */
|
|
|
|
const char *
|
|
|
|
circuit_purpose_to_controller_string(uint8_t purpose)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
switch (purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_OR:
|
|
|
|
case CIRCUIT_PURPOSE_INTRO_POINT:
|
|
|
|
case CIRCUIT_PURPOSE_REND_POINT_WAITING:
|
|
|
|
case CIRCUIT_PURPOSE_REND_ESTABLISHED:
|
|
|
|
return "SERVER"; /* A controller should never see these, actually. */
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
return "GENERAL";
|
2017-12-02 03:28:16 +01:00
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_C_HSDIR_GET:
|
|
|
|
return "HS_CLIENT_HSDIR";
|
|
|
|
|
2008-11-10 00:56:29 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
2008-09-30 00:34:22 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
|
|
|
|
return "HS_CLIENT_INTRO";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
return "HS_CLIENT_REND";
|
|
|
|
|
2017-12-02 03:28:16 +01:00
|
|
|
case CIRCUIT_PURPOSE_S_HSDIR_POST:
|
|
|
|
return "HS_SERVICE_HSDIR";
|
|
|
|
|
2008-09-30 00:34:22 +02:00
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
case CIRCUIT_PURPOSE_S_INTRO:
|
|
|
|
return "HS_SERVICE_INTRO";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
case CIRCUIT_PURPOSE_S_REND_JOINED:
|
|
|
|
return "HS_SERVICE_REND";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
return "TESTING";
|
2010-07-15 03:06:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
|
2010-07-24 18:19:20 +02:00
|
|
|
return "MEASURE_TIMEOUT";
|
2008-09-30 00:34:22 +02:00
|
|
|
case CIRCUIT_PURPOSE_CONTROLLER:
|
|
|
|
return "CONTROLLER";
|
2012-12-12 20:53:18 +01:00
|
|
|
case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
|
|
|
|
return "PATH_BIAS_TESTING";
|
2017-12-22 07:35:29 +01:00
|
|
|
case CIRCUIT_PURPOSE_HS_VANGUARDS:
|
|
|
|
return "HS_VANGUARDS";
|
2008-09-30 00:34:22 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-24 02:28:59 +02:00
|
|
|
/** Return a string specifying the state of the hidden-service circuit
|
|
|
|
* purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
|
|
|
|
* hidden-service-related circuit purpose. */
|
|
|
|
const char *
|
|
|
|
circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
|
|
|
|
{
|
|
|
|
switch (purpose)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
log_fn(LOG_WARN, LD_BUG,
|
|
|
|
"Unrecognized circuit purpose: %d",
|
|
|
|
(int)purpose);
|
|
|
|
tor_fragile_assert();
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_OR:
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
|
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
case CIRCUIT_PURPOSE_CONTROLLER:
|
2012-12-12 20:53:18 +01:00
|
|
|
case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
|
2017-12-22 07:35:29 +01:00
|
|
|
case CIRCUIT_PURPOSE_HS_VANGUARDS:
|
2011-06-24 02:28:59 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_INTRO_POINT:
|
|
|
|
return "OR_HSSI_ESTABLISHED";
|
|
|
|
case CIRCUIT_PURPOSE_REND_POINT_WAITING:
|
|
|
|
return "OR_HSCR_ESTABLISHED";
|
|
|
|
case CIRCUIT_PURPOSE_REND_ESTABLISHED:
|
|
|
|
return "OR_HS_R_JOINED";
|
|
|
|
|
2017-12-02 03:28:16 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_HSDIR_GET:
|
2011-06-24 02:28:59 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
return "HSCI_CONNECTING";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
return "HSCI_INTRO_SENT";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
|
|
|
|
return "HSCI_DONE";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
return "HSCR_CONNECTING";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
return "HSCR_ESTABLISHED_IDLE";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
return "HSCR_ESTABLISHED_WAITING";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
return "HSCR_JOINED";
|
|
|
|
|
2017-12-02 03:28:16 +01:00
|
|
|
case CIRCUIT_PURPOSE_S_HSDIR_POST:
|
2011-06-24 02:28:59 +02:00
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
return "HSSI_CONNECTING";
|
|
|
|
case CIRCUIT_PURPOSE_S_INTRO:
|
|
|
|
return "HSSI_ESTABLISHED";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
return "HSSR_CONNECTING";
|
|
|
|
case CIRCUIT_PURPOSE_S_REND_JOINED:
|
|
|
|
return "HSSR_JOINED";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 19:27:25 +02:00
|
|
|
/** Return a human-readable string for the circuit purpose <b>purpose</b>. */
|
|
|
|
const char *
|
|
|
|
circuit_purpose_to_string(uint8_t purpose)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
|
|
|
|
switch (purpose)
|
|
|
|
{
|
|
|
|
case CIRCUIT_PURPOSE_OR:
|
|
|
|
return "Circuit at relay";
|
|
|
|
case CIRCUIT_PURPOSE_INTRO_POINT:
|
|
|
|
return "Acting as intro point";
|
|
|
|
case CIRCUIT_PURPOSE_REND_POINT_WAITING:
|
2018-01-24 09:55:15 +01:00
|
|
|
return "Acting as rendezvous (pending)";
|
2011-04-08 19:27:25 +02:00
|
|
|
case CIRCUIT_PURPOSE_REND_ESTABLISHED:
|
2018-01-24 09:55:15 +01:00
|
|
|
return "Acting as rendezvous (established)";
|
2011-04-08 19:27:25 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
return "General-purpose client";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
return "Hidden service client: Connecting to intro point";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
return "Hidden service client: Waiting for ack from intro point";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
|
|
|
|
return "Hidden service client: Received ack from intro point";
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
return "Hidden service client: Establishing rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
return "Hidden service client: Pending rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
return "Hidden service client: Pending rendezvous point (ack received)";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
return "Hidden service client: Active rendezvous point";
|
2017-12-02 03:28:16 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_HSDIR_GET:
|
|
|
|
return "Hidden service client: Fetching HS descriptor";
|
|
|
|
|
2011-04-08 19:27:25 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
|
|
|
|
return "Measuring circuit timeout";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
return "Hidden service: Establishing introduction point";
|
|
|
|
case CIRCUIT_PURPOSE_S_INTRO:
|
|
|
|
return "Hidden service: Introduction point";
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
return "Hidden service: Connecting to rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_S_REND_JOINED:
|
|
|
|
return "Hidden service: Active rendezvous point";
|
2017-12-02 03:28:16 +01:00
|
|
|
case CIRCUIT_PURPOSE_S_HSDIR_POST:
|
|
|
|
return "Hidden service: Uploading HS descriptor";
|
2011-04-08 19:27:25 +02:00
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
return "Testing circuit";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_CONTROLLER:
|
|
|
|
return "Circuit made by controller";
|
|
|
|
|
2013-03-14 17:06:03 +01:00
|
|
|
case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
|
|
|
|
return "Path-bias testing circuit";
|
|
|
|
|
2017-12-22 07:35:29 +01:00
|
|
|
case CIRCUIT_PURPOSE_HS_VANGUARDS:
|
|
|
|
return "Hidden service: Pre-built vanguard circuit";
|
|
|
|
|
2011-04-08 19:27:25 +02:00
|
|
|
default:
|
|
|
|
tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-15 12:33:33 +02:00
|
|
|
/** Pick a reasonable package_window to start out for our circuits.
|
|
|
|
* Originally this was hard-coded at 1000, but now the consensus votes
|
|
|
|
* on the answer. See proposal 168. */
|
|
|
|
int32_t
|
|
|
|
circuit_initial_package_window(void)
|
|
|
|
{
|
2010-12-30 19:54:13 +01:00
|
|
|
int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
|
|
|
|
CIRCWINDOW_START_MIN,
|
|
|
|
CIRCWINDOW_START_MAX);
|
2009-09-23 04:09:33 +02:00
|
|
|
/* If the consensus tells us a negative number, we'd assert. */
|
|
|
|
if (num < 0)
|
|
|
|
num = CIRCWINDOW_START;
|
|
|
|
return num;
|
2009-09-15 12:33:33 +02:00
|
|
|
}
|
|
|
|
|
2007-01-24 02:30:07 +01:00
|
|
|
/** Initialize the common elements in a circuit_t, and add it to the global
|
|
|
|
* list. */
|
2006-07-23 09:37:35 +02:00
|
|
|
static void
|
|
|
|
init_circuit_base(circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2010-10-15 18:35:55 +02:00
|
|
|
tor_gettimeofday(&circ->timestamp_created);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-10-26 02:43:10 +02:00
|
|
|
// Gets reset when we send CREATE_FAST.
|
|
|
|
// circuit_expire_building() expects these to be equal
|
|
|
|
// until the orconn is built.
|
|
|
|
circ->timestamp_began = circ->timestamp_created;
|
|
|
|
|
2009-09-15 12:33:33 +02:00
|
|
|
circ->package_window = circuit_initial_package_window();
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->deliver_window = CIRCWINDOW_START;
|
2013-03-22 19:25:34 +01:00
|
|
|
cell_queue_init(&circ->n_chan_cells);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
smartlist_add(circuit_get_global_list(), circ);
|
|
|
|
circ->global_circuitlist_idx = smartlist_len(circuit_get_global_list()) - 1;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 03:28:00 +01:00
|
|
|
/** If we haven't yet decided on a good timeout value for circuit
|
|
|
|
* building, we close idle circuits aggressively so we can get more
|
2018-01-31 17:27:50 +01:00
|
|
|
* data points. These are the default, min, and max consensus values */
|
|
|
|
#define DFLT_IDLE_TIMEOUT_WHILE_LEARNING (3*60)
|
|
|
|
#define MIN_IDLE_TIMEOUT_WHILE_LEARNING (10)
|
|
|
|
#define MAX_IDLE_TIMEOUT_WHILE_LEARNING (1000*60)
|
2017-02-22 03:28:00 +01:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
|
|
|
|
* and <b>p_conn</b>. Add it to the global circuit list.
|
|
|
|
*/
|
|
|
|
origin_circuit_t *
|
|
|
|
origin_circuit_new(void)
|
|
|
|
{
|
|
|
|
origin_circuit_t *circ;
|
2006-07-28 17:11:11 +02:00
|
|
|
/* never zero, since a global ID of 0 is treated specially by the
|
|
|
|
* controller */
|
|
|
|
static uint32_t n_circuits_allocated = 1;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
circ = tor_malloc_zero(sizeof(origin_circuit_t));
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
circ->next_stream_id = crypto_rand_int(1<<16);
|
2006-07-28 17:11:11 +02:00
|
|
|
circ->global_identifier = n_circuits_allocated++;
|
2008-07-23 17:58:38 +02:00
|
|
|
circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
|
|
|
|
circ->remaining_relay_early_cells -= crypto_rand_int(2);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
init_circuit_base(TO_CIRCUIT(circ));
|
|
|
|
|
2016-11-22 15:05:52 +01:00
|
|
|
/* Add to origin-list. */
|
2017-01-02 19:06:47 +01:00
|
|
|
circ->global_origin_circuit_list_idx = -1;
|
|
|
|
circuit_add_to_origin_circuit_list(circ);
|
2016-11-22 15:05:52 +01:00
|
|
|
|
2013-08-21 18:37:35 +02:00
|
|
|
circuit_build_times_update_last_circ(get_circuit_build_times_mutable());
|
2009-09-01 08:09:54 +02:00
|
|
|
|
2017-02-22 03:28:00 +01:00
|
|
|
if (! circuit_build_times_disabled(get_options()) &&
|
|
|
|
circuit_build_times_needs_circuits(get_circuit_build_times())) {
|
|
|
|
/* Circuits should be shorter lived if we need more of them
|
|
|
|
* for learning a good build timeout */
|
2018-01-31 17:27:50 +01:00
|
|
|
circ->circuit_idle_timeout =
|
|
|
|
networkstatus_get_param(NULL, "cbtlearntimeout",
|
|
|
|
DFLT_IDLE_TIMEOUT_WHILE_LEARNING,
|
|
|
|
MIN_IDLE_TIMEOUT_WHILE_LEARNING,
|
|
|
|
MAX_IDLE_TIMEOUT_WHILE_LEARNING);
|
2017-02-22 03:28:00 +01:00
|
|
|
} else {
|
|
|
|
// This should always be larger than the current port prediction time
|
|
|
|
// remaining, or else we'll end up with the case where a circuit times out
|
|
|
|
// and another one is built, effectively doubling the timeout window.
|
|
|
|
//
|
|
|
|
// We also randomize it by up to 5% more (ie 5% of 0 to 3600 seconds,
|
|
|
|
// depending on how much circuit prediction time is remaining) so that
|
|
|
|
// we don't close a bunch of unused circuits all at the same time.
|
|
|
|
int prediction_time_remaining =
|
|
|
|
predicted_ports_prediction_time_remaining(time(NULL));
|
|
|
|
circ->circuit_idle_timeout = prediction_time_remaining+1+
|
|
|
|
crypto_rand_int(1+prediction_time_remaining/20);
|
|
|
|
|
|
|
|
if (circ->circuit_idle_timeout <= 0) {
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Circuit chose a negative idle timeout of %d based on "
|
|
|
|
"%d seconds of predictive building remaining.",
|
|
|
|
circ->circuit_idle_timeout,
|
|
|
|
prediction_time_remaining);
|
2018-01-31 17:27:50 +01:00
|
|
|
circ->circuit_idle_timeout =
|
|
|
|
networkstatus_get_param(NULL, "cbtlearntimeout",
|
|
|
|
DFLT_IDLE_TIMEOUT_WHILE_LEARNING,
|
|
|
|
MIN_IDLE_TIMEOUT_WHILE_LEARNING,
|
|
|
|
MAX_IDLE_TIMEOUT_WHILE_LEARNING);
|
2017-02-22 03:28:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Circuit " U64_FORMAT " chose an idle timeout of %d based on "
|
|
|
|
"%d seconds of predictive building remaining.",
|
|
|
|
U64_PRINTF_ARG(circ->global_identifier),
|
|
|
|
circ->circuit_idle_timeout,
|
|
|
|
prediction_time_remaining);
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
2015-08-06 08:05:07 +02:00
|
|
|
/** Allocate a new or_circuit_t, connected to <b>p_chan</b> as
|
|
|
|
* <b>p_circ_id</b>. If <b>p_chan</b> is NULL, the circuit is unattached. */
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *
|
2012-08-08 15:02:47 +02:00
|
|
|
or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
|
|
|
/* CircIDs */
|
|
|
|
or_circuit_t *circ;
|
|
|
|
|
|
|
|
circ = tor_malloc_zero(sizeof(or_circuit_t));
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.magic = OR_CIRCUIT_MAGIC;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (p_chan)
|
|
|
|
circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2008-07-23 17:58:38 +02:00
|
|
|
circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
|
2013-03-22 19:25:34 +01:00
|
|
|
cell_queue_init(&circ->p_chan_cells);
|
2008-07-23 17:58:38 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
init_circuit_base(TO_CIRCUIT(circ));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
2016-03-28 16:37:22 +02:00
|
|
|
/** Free all storage held in circ->testing_cell_stats */
|
|
|
|
void
|
|
|
|
circuit_clear_testing_cell_stats(circuit_t *circ)
|
|
|
|
{
|
2016-03-29 02:09:09 +02:00
|
|
|
if (!circ || !circ->testing_cell_stats)
|
2016-03-28 16:37:22 +02:00
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(circ->testing_cell_stats, testing_cell_stats_entry_t *,
|
|
|
|
ent, tor_free(ent));
|
|
|
|
smartlist_free(circ->testing_cell_stats);
|
|
|
|
circ->testing_cell_stats = NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Deallocate space associated with circ.
|
|
|
|
*/
|
2013-07-10 21:07:32 +02:00
|
|
|
STATIC void
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2017-09-25 22:04:30 +02:00
|
|
|
circid_t n_circ_id = 0;
|
2006-07-23 09:37:35 +02:00
|
|
|
void *mem;
|
2008-02-07 17:10:33 +01:00
|
|
|
size_t memlen;
|
2013-10-02 21:11:34 +02:00
|
|
|
int should_free = 1;
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!circ)
|
|
|
|
return;
|
|
|
|
|
2017-09-25 22:04:30 +02:00
|
|
|
/* We keep a copy of this so we can log its value before it gets unset. */
|
|
|
|
n_circ_id = circ->n_circ_id;
|
|
|
|
|
2016-03-28 16:37:22 +02:00
|
|
|
circuit_clear_testing_cell_stats(circ);
|
|
|
|
|
2017-12-04 17:49:48 +01:00
|
|
|
/* Cleanup circuit from anything HS v3 related. We also do this when the
|
|
|
|
* circuit is closed. This is to avoid any code path that free registered
|
|
|
|
* circuits without closing them before. This needs to be done before the
|
|
|
|
* hs identifier is freed. */
|
|
|
|
hs_circ_cleanup(circ);
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
mem = ocirc;
|
2008-02-07 17:10:33 +01:00
|
|
|
memlen = sizeof(origin_circuit_t);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
|
2016-11-22 15:05:52 +01:00
|
|
|
|
2017-01-02 19:06:47 +01:00
|
|
|
circuit_remove_from_origin_circuit_list(ocirc);
|
2016-11-22 15:05:52 +01:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (ocirc->build_state) {
|
|
|
|
extend_info_free(ocirc->build_state->chosen_exit);
|
|
|
|
circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
|
2012-01-07 07:44:20 +01:00
|
|
|
cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
tor_free(ocirc->build_state);
|
|
|
|
|
2016-11-23 15:15:51 +01:00
|
|
|
/* Cancel before freeing, if we haven't already succeeded or failed. */
|
|
|
|
if (ocirc->guard_state) {
|
2016-11-28 17:04:28 +01:00
|
|
|
entry_guard_cancel(ô->guard_state);
|
2016-11-23 15:15:51 +01:00
|
|
|
}
|
2016-11-18 22:05:09 +01:00
|
|
|
circuit_guard_state_free(ocirc->guard_state);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2014-02-07 16:50:06 +01:00
|
|
|
circuit_clear_cpath(ocirc);
|
2009-12-12 08:07:59 +01:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(ocirc->intro_key);
|
2009-12-12 08:07:59 +01:00
|
|
|
rend_data_free(ocirc->rend_data);
|
2017-12-04 17:49:48 +01:00
|
|
|
|
|
|
|
/* Finally, free the identifier of the circuit and nullify it so multiple
|
|
|
|
* cleanup will work. */
|
2017-05-12 17:39:46 +02:00
|
|
|
hs_ident_circuit_free(ocirc->hs_ident);
|
2017-12-04 17:49:48 +01:00
|
|
|
ocirc->hs_ident = NULL;
|
2011-07-06 22:03:47 +02:00
|
|
|
|
|
|
|
tor_free(ocirc->dest_address);
|
2011-08-06 01:07:33 +02:00
|
|
|
if (ocirc->socks_username) {
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(ocirc->socks_username, 0x12, ocirc->socks_username_len);
|
2011-08-06 01:07:33 +02:00
|
|
|
tor_free(ocirc->socks_username);
|
|
|
|
}
|
|
|
|
if (ocirc->socks_password) {
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(ocirc->socks_password, 0x06, ocirc->socks_password_len);
|
2011-08-06 01:07:33 +02:00
|
|
|
tor_free(ocirc->socks_password);
|
|
|
|
}
|
2013-03-19 17:29:08 +01:00
|
|
|
addr_policy_list_free(ocirc->prepend_policy);
|
2006-07-23 09:37:35 +02:00
|
|
|
} else {
|
|
|
|
or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
|
2009-07-05 19:53:25 +02:00
|
|
|
/* Remember cell statistics for this circuit before deallocating. */
|
|
|
|
if (get_options()->CellStatistics)
|
2009-08-19 15:41:12 +02:00
|
|
|
rep_hist_buffer_stats_add_circ(circ, time(NULL));
|
2006-07-23 09:37:35 +02:00
|
|
|
mem = ocirc;
|
2008-02-07 17:10:33 +01:00
|
|
|
memlen = sizeof(or_circuit_t);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
|
|
|
|
|
2013-10-02 21:11:34 +02:00
|
|
|
should_free = (ocirc->workqueue_entry == NULL);
|
|
|
|
|
2018-03-17 15:59:15 +01:00
|
|
|
relay_crypto_clear(ô->crypto);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
if (ocirc->rend_splice) {
|
|
|
|
or_circuit_t *other = ocirc->rend_splice;
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
|
2006-07-23 09:37:35 +02:00
|
|
|
other->rend_splice = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove from map. */
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_set_p_circid_chan(ocirc, 0, NULL);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2007-04-09 22:09:26 +02:00
|
|
|
/* Clear cell queue _after_ removing it from the map. Otherwise our
|
|
|
|
* "active" checks will be violated. */
|
2012-08-08 15:02:47 +02:00
|
|
|
cell_queue_clear(ô->p_chan_cells);
|
2007-04-09 22:09:26 +02:00
|
|
|
}
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2009-12-12 08:07:59 +01:00
|
|
|
extend_info_free(circ->n_hop);
|
2012-12-06 05:07:49 +01:00
|
|
|
tor_free(circ->n_chan_create_cell);
|
2008-02-06 00:20:49 +01:00
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
if (circ->global_circuitlist_idx != -1) {
|
|
|
|
int idx = circ->global_circuitlist_idx;
|
|
|
|
circuit_t *c2 = smartlist_get(global_circuitlist, idx);
|
|
|
|
tor_assert(c2 == circ);
|
|
|
|
smartlist_del(global_circuitlist, idx);
|
|
|
|
if (idx < smartlist_len(global_circuitlist)) {
|
|
|
|
c2 = smartlist_get(global_circuitlist, idx);
|
|
|
|
c2->global_circuitlist_idx = idx;
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 17:56:54 +02:00
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
/* Remove from map. */
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_set_n_circid_chan(circ, 0, NULL);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-04-09 22:09:26 +02:00
|
|
|
/* Clear cell queue _after_ removing it from the map. Otherwise our
|
|
|
|
* "active" checks will be violated. */
|
2012-08-08 15:02:47 +02:00
|
|
|
cell_queue_clear(&circ->n_chan_cells);
|
2007-04-09 22:09:26 +02:00
|
|
|
|
2017-09-20 17:56:44 +02:00
|
|
|
log_info(LD_CIRC, "Circuit %u (id: %" PRIu32 ") has been freed.",
|
2017-09-25 22:04:30 +02:00
|
|
|
n_circ_id,
|
2017-09-20 17:56:44 +02:00
|
|
|
CIRCUIT_IS_ORIGIN(circ) ?
|
|
|
|
TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0);
|
|
|
|
|
2013-10-02 21:11:34 +02:00
|
|
|
if (should_free) {
|
|
|
|
memwipe(mem, 0xAA, memlen); /* poison memory */
|
|
|
|
tor_free(mem);
|
|
|
|
} else {
|
|
|
|
/* If we made it here, this is an or_circuit_t that still has a pending
|
|
|
|
* cpuworker request which we weren't able to cancel. Instead, set up
|
|
|
|
* the magic value so that when the reply comes back, we'll know to discard
|
|
|
|
* the reply and free this structure.
|
|
|
|
*/
|
|
|
|
memwipe(mem, 0xAA, memlen);
|
|
|
|
circ->magic = DEAD_CIRCUIT_MAGIC;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:50:06 +01:00
|
|
|
/** Deallocate the linked list circ-><b>cpath</b>, and remove the cpath from
|
|
|
|
* <b>circ</b>. */
|
|
|
|
void
|
|
|
|
circuit_clear_cpath(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2014-02-07 16:50:06 +01:00
|
|
|
crypt_path_t *victim, *head, *cpath;
|
|
|
|
|
|
|
|
head = cpath = circ->cpath;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!cpath)
|
2004-05-13 09:24:49 +02:00
|
|
|
return;
|
|
|
|
|
2014-02-06 23:08:50 +01:00
|
|
|
/* it's a circular list, so we have to notice when we've
|
2004-05-13 09:24:49 +02:00
|
|
|
* gone through it once. */
|
2004-11-28 10:05:49 +01:00
|
|
|
while (cpath->next && cpath->next != head) {
|
2004-05-13 09:24:49 +02:00
|
|
|
victim = cpath;
|
|
|
|
cpath = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
|
|
|
|
|
|
|
circuit_free_cpath_node(cpath);
|
|
|
|
|
2014-02-06 23:08:50 +01:00
|
|
|
circ->cpath = NULL;
|
|
|
|
}
|
|
|
|
|
2005-02-11 02:26:47 +01:00
|
|
|
/** Release all storage held by circuits. */
|
|
|
|
void
|
|
|
|
circuit_free_all(void)
|
|
|
|
{
|
2014-08-15 23:02:28 +02:00
|
|
|
smartlist_t *lst = circuit_get_global_list();
|
2013-06-20 17:56:54 +02:00
|
|
|
|
2014-08-15 23:02:28 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(lst, circuit_t *, tmp) {
|
2013-06-20 17:56:54 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(tmp)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(tmp);
|
2006-07-23 09:37:35 +02:00
|
|
|
while (or_circ->resolving_streams) {
|
2007-05-17 00:15:14 +02:00
|
|
|
edge_connection_t *next_conn;
|
|
|
|
next_conn = or_circ->resolving_streams->next_stream;
|
2017-11-21 14:39:07 +01:00
|
|
|
connection_free_(TO_CONN(or_circ->resolving_streams));
|
2007-05-17 00:15:14 +02:00
|
|
|
or_circ->resolving_streams = next_conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
2005-02-11 02:26:47 +01:00
|
|
|
}
|
2014-08-15 23:02:28 +02:00
|
|
|
tmp->global_circuitlist_idx = -1;
|
2016-02-11 18:15:12 +01:00
|
|
|
circuit_about_to_free_atexit(tmp);
|
2013-06-20 17:56:54 +02:00
|
|
|
circuit_free(tmp);
|
2014-08-15 23:02:28 +02:00
|
|
|
SMARTLIST_DEL_CURRENT(lst, tmp);
|
|
|
|
} SMARTLIST_FOREACH_END(tmp);
|
|
|
|
|
|
|
|
smartlist_free(lst);
|
|
|
|
global_circuitlist = NULL;
|
2009-12-12 08:07:59 +01:00
|
|
|
|
2016-11-22 15:05:52 +01:00
|
|
|
smartlist_free(global_origin_circuit_list);
|
|
|
|
global_origin_circuit_list = NULL;
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
smartlist_free(circuits_pending_chans);
|
|
|
|
circuits_pending_chans = NULL;
|
2009-12-12 08:07:59 +01:00
|
|
|
|
2015-12-01 13:34:31 +01:00
|
|
|
smartlist_free(circuits_pending_close);
|
|
|
|
circuits_pending_close = NULL;
|
|
|
|
|
2016-11-21 23:23:25 +01:00
|
|
|
smartlist_free(circuits_pending_other_guards);
|
|
|
|
circuits_pending_other_guards = NULL;
|
|
|
|
|
2014-03-25 15:14:26 +01:00
|
|
|
{
|
|
|
|
chan_circid_circuit_map_t **elt, **next, *c;
|
|
|
|
for (elt = HT_START(chan_circid_map, &chan_circid_map);
|
|
|
|
elt;
|
|
|
|
elt = next) {
|
|
|
|
c = *elt;
|
|
|
|
next = HT_NEXT_RMV(chan_circid_map, &chan_circid_map, elt);
|
|
|
|
|
|
|
|
tor_assert(c->circuit == NULL);
|
|
|
|
tor_free(c);
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
HT_CLEAR(chan_circid_map, &chan_circid_map);
|
2005-02-11 02:26:47 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Deallocate space associated with the cpath node <b>victim</b>. */
|
2004-11-10 15:28:04 +01:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_free_cpath_node(crypt_path_t *victim)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!victim)
|
|
|
|
return;
|
|
|
|
|
2018-03-17 15:59:15 +01:00
|
|
|
relay_crypto_clear(&victim->crypto);
|
2012-12-05 03:27:07 +01:00
|
|
|
onion_handshake_state_release(&victim->handshake_state);
|
|
|
|
crypto_dh_free(victim->rend_dh_handshake_state);
|
2009-12-12 08:07:59 +01:00
|
|
|
extend_info_free(victim->extend_info);
|
2005-06-29 23:46:55 +02:00
|
|
|
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(victim);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-01-07 07:44:20 +01:00
|
|
|
/** Release a crypt_path_reference_t*, which may be NULL. */
|
|
|
|
static void
|
|
|
|
cpath_ref_decref(crypt_path_reference_t *cpath_ref)
|
|
|
|
{
|
|
|
|
if (cpath_ref != NULL) {
|
|
|
|
if (--(cpath_ref->refcount) == 0) {
|
|
|
|
circuit_free_cpath_node(cpath_ref->cpath);
|
|
|
|
tor_free(cpath_ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** A helper function for circuit_dump_by_conn() below. Log a bunch
|
|
|
|
* of information about circuit <b>circ</b>.
|
|
|
|
*/
|
|
|
|
static void
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_dump_conn_details(int severity,
|
|
|
|
circuit_t *circ,
|
|
|
|
int conn_array_index,
|
|
|
|
const char *type,
|
2013-03-10 13:32:58 +01:00
|
|
|
circid_t this_circid,
|
|
|
|
circid_t other_circid)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
2013-03-10 13:32:58 +01:00
|
|
|
tor_log(severity, LD_CIRC, "Conn %d has %s circuit: circID %u "
|
|
|
|
"(other side %u), state %d (%s), born %ld:",
|
|
|
|
conn_array_index, type, (unsigned)this_circid, (unsigned)other_circid,
|
|
|
|
circ->state, circuit_state_to_string(circ->state),
|
2012-10-26 02:43:10 +02:00
|
|
|
(long)circ->timestamp_began.tv_sec);
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
|
|
|
|
circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Log, at severity <b>severity</b>, information about each circuit
|
|
|
|
* that is connected to <b>conn</b>.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circuit_dump_by_conn(connection_t *conn, int severity)
|
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *tmpconn;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
2006-07-23 09:37:35 +02:00
|
|
|
circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
|
2012-08-08 15:02:47 +02:00
|
|
|
|
|
|
|
if (circ->marked_for_close) {
|
2006-07-23 09:37:35 +02:00
|
|
|
continue;
|
2012-08-08 15:02:47 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
|
2012-08-08 15:02:47 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (TO_CONN(tmpconn) == conn) {
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_dump_conn_details(severity, circ, conn->conn_array_index,
|
|
|
|
"App-ward", p_circ_id, n_circ_id);
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (TO_CONN(tmpconn) == conn) {
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_dump_conn_details(severity, circ, conn->conn_array_index,
|
|
|
|
"Exit-ward", n_circ_id, p_circ_id);
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2012-08-08 15:02:47 +02:00
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Return the circuit whose global ID is <b>id</b>, or NULL if no
|
|
|
|
* such circuit exists. */
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *
|
2005-03-12 05:22:01 +01:00
|
|
|
circuit_get_by_global_id(uint32_t id)
|
|
|
|
{
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
2006-07-28 17:11:11 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
|
2005-03-12 05:22:01 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
return NULL;
|
|
|
|
else
|
2006-07-28 17:11:11 +02:00
|
|
|
return TO_ORIGIN_CIRCUIT(circ);
|
2005-03-12 05:22:01 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2005-03-12 05:22:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Return a circ such that:
|
|
|
|
* - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
|
2012-08-08 15:02:47 +02:00
|
|
|
* - circ is attached to <b>chan</b>, either as p_chan or n_chan.
|
2004-05-13 09:24:49 +02:00
|
|
|
* Return NULL if no such circuit exists.
|
2013-03-14 17:13:45 +01:00
|
|
|
*
|
|
|
|
* If <b>found_entry_out</b> is provided, set it to true if we have a
|
|
|
|
* placeholder entry for circid/chan, and leave it unset otherwise.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline circuit_t *
|
2013-03-14 17:13:45 +01:00
|
|
|
circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan,
|
|
|
|
int *found_entry_out)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2012-08-08 15:02:47 +02:00
|
|
|
chan_circid_circuit_map_t search;
|
|
|
|
chan_circid_circuit_map_t *found;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (_last_circid_chan_ent &&
|
|
|
|
circ_id == _last_circid_chan_ent->circ_id &&
|
|
|
|
chan == _last_circid_chan_ent->chan) {
|
|
|
|
found = _last_circid_chan_ent;
|
2005-04-07 07:09:19 +02:00
|
|
|
} else {
|
|
|
|
search.circ_id = circ_id;
|
2012-08-08 15:02:47 +02:00
|
|
|
search.chan = chan;
|
|
|
|
found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
|
|
|
|
_last_circid_chan_ent = found;
|
2005-04-07 07:09:19 +02:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (found && found->circuit) {
|
|
|
|
log_debug(LD_CIRC,
|
|
|
|
"circuit_get_by_circid_channel_impl() returning circuit %p for"
|
2013-03-10 13:32:58 +01:00
|
|
|
" circ_id %u, channel ID " U64_FORMAT " (%p)",
|
|
|
|
found->circuit, (unsigned)circ_id,
|
2012-10-09 04:48:06 +02:00
|
|
|
U64_PRINTF_ARG(chan->global_identifier), chan);
|
2013-03-14 17:13:45 +01:00
|
|
|
if (found_entry_out)
|
|
|
|
*found_entry_out = 1;
|
2005-04-06 07:33:32 +02:00
|
|
|
return found->circuit;
|
2012-08-08 15:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log_debug(LD_CIRC,
|
2013-03-14 17:13:45 +01:00
|
|
|
"circuit_get_by_circid_channel_impl() found %s for"
|
2013-03-10 13:32:58 +01:00
|
|
|
" circ_id %u, channel ID " U64_FORMAT " (%p)",
|
2013-03-14 17:13:45 +01:00
|
|
|
found ? "placeholder" : "nothing",
|
2013-03-10 13:32:58 +01:00
|
|
|
(unsigned)circ_id,
|
2012-10-09 04:48:06 +02:00
|
|
|
U64_PRINTF_ARG(chan->global_identifier), chan);
|
2005-04-06 08:13:49 +02:00
|
|
|
|
2013-03-14 17:13:45 +01:00
|
|
|
if (found_entry_out)
|
|
|
|
*found_entry_out = found ? 1 : 0;
|
|
|
|
|
2005-11-11 20:28:32 +01:00
|
|
|
return NULL;
|
|
|
|
/* The rest of this checks for bugs. Disabled by default. */
|
2011-06-08 21:23:05 +02:00
|
|
|
/* We comment it out because coverity complains otherwise.
|
2005-04-06 08:13:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2013-06-20 17:56:54 +02:00
|
|
|
TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
|
2006-07-23 09:37:35 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
2012-08-08 15:02:47 +02:00
|
|
|
if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
|
2006-07-23 09:37:35 +02:00
|
|
|
log_warn(LD_BUG,
|
2012-08-08 15:02:47 +02:00
|
|
|
"circuit matches p_chan, but not in hash table (Bug!)");
|
2006-07-23 09:37:35 +02:00
|
|
|
return circ;
|
|
|
|
}
|
2005-04-06 08:13:49 +02:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG,
|
2012-08-08 15:02:47 +02:00
|
|
|
"circuit matches n_chan, but not in hash table (Bug!)");
|
2005-04-06 08:13:49 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
return NULL;
|
2011-06-08 21:23:05 +02:00
|
|
|
} */
|
2005-10-24 21:37:45 +02:00
|
|
|
}
|
2005-04-06 08:13:49 +02:00
|
|
|
|
2005-10-24 21:37:45 +02:00
|
|
|
/** Return a circ such that:
|
|
|
|
* - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
|
2012-08-08 15:02:47 +02:00
|
|
|
* - circ is attached to <b>chan</b>, either as p_chan or n_chan.
|
2005-10-24 21:37:45 +02:00
|
|
|
* - circ is not marked for close.
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
|
|
|
circuit_t *
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
|
2005-10-24 21:37:45 +02:00
|
|
|
{
|
2013-03-14 17:13:45 +01:00
|
|
|
circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
|
2005-10-25 17:31:25 +02:00
|
|
|
if (!circ || circ->marked_for_close)
|
2005-10-24 21:37:45 +02:00
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
2012-10-01 23:22:53 +02:00
|
|
|
/** Return a circ such that:
|
|
|
|
* - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
|
|
|
|
* - circ is attached to <b>chan</b>, either as p_chan or n_chan.
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
|
|
|
circuit_t *
|
|
|
|
circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
|
|
|
|
channel_t *chan)
|
|
|
|
{
|
2013-03-14 17:13:45 +01:00
|
|
|
return circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
|
2012-10-01 23:22:53 +02:00
|
|
|
}
|
|
|
|
|
2008-07-23 14:55:55 +02:00
|
|
|
/** Return true iff the circuit ID <b>circ_id</b> is currently used by a
|
2014-04-18 19:04:37 +02:00
|
|
|
* circuit, marked or not, on <b>chan</b>, or if the circ ID is reserved until
|
|
|
|
* a queued destroy cell can be sent.
|
|
|
|
*
|
|
|
|
* (Return 1 if the circuit is present, marked or not; Return 2
|
|
|
|
* if the circuit ID is pending a destroy.)
|
|
|
|
**/
|
2008-07-23 14:55:55 +02:00
|
|
|
int
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
|
2008-07-23 14:55:55 +02:00
|
|
|
{
|
2013-03-14 17:13:45 +01:00
|
|
|
int found = 0;
|
2014-04-18 19:04:37 +02:00
|
|
|
if (circuit_get_by_circid_channel_impl(circ_id, chan, &found) != NULL)
|
|
|
|
return 1;
|
|
|
|
if (found)
|
|
|
|
return 2;
|
|
|
|
return 0;
|
2008-07-23 14:55:55 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 19:35:45 +02:00
|
|
|
/** Helper for debugging 12184. Returns the time since which 'circ_id' has
|
|
|
|
* been marked unusable on 'chan'. */
|
|
|
|
time_t
|
|
|
|
circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
|
|
|
|
{
|
|
|
|
chan_circid_circuit_map_t search;
|
|
|
|
chan_circid_circuit_map_t *found;
|
|
|
|
|
|
|
|
memset(&search, 0, sizeof(search));
|
|
|
|
search.circ_id = circ_id;
|
|
|
|
search.chan = chan;
|
|
|
|
|
|
|
|
found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
|
|
|
|
|
|
|
|
if (! found || found->circuit)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return found->made_placeholder_at;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** Return the circuit that a given edge connection is using. */
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_get_by_edge_conn(edge_connection_t *conn)
|
2005-04-06 08:13:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
circ = conn->on_circuit;
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(!circ ||
|
|
|
|
(CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
|
|
|
|
: circ->magic == OR_CIRCUIT_MAGIC));
|
2005-11-26 02:43:57 +01:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
return circ;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
|
|
|
|
* circuit from the chan,circid map, and mark it for close if it hasn't
|
2005-12-05 20:45:54 +01:00
|
|
|
* been marked already.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-12-05 20:45:54 +01:00
|
|
|
void
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_unlink_all_from_channel(channel_t *chan, int reason)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2013-09-09 19:48:44 +02:00
|
|
|
smartlist_t *detached = smartlist_new();
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2014-03-26 15:31:56 +01:00
|
|
|
/* #define DEBUG_CIRCUIT_UNLINK_ALL */
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2013-09-09 19:48:44 +02:00
|
|
|
channel_unlink_all_circuits(chan, detached);
|
|
|
|
|
|
|
|
#ifdef DEBUG_CIRCUIT_UNLINK_ALL
|
|
|
|
{
|
|
|
|
smartlist_t *detached_2 = smartlist_new();
|
|
|
|
int mismatch = 0, badlen = 0;
|
|
|
|
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
2013-09-09 19:48:44 +02:00
|
|
|
if (circ->n_chan == chan ||
|
|
|
|
(!CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
TO_OR_CIRCUIT(circ)->p_chan == chan)) {
|
|
|
|
smartlist_add(detached_2, circ);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2013-09-09 19:48:44 +02:00
|
|
|
|
|
|
|
if (smartlist_len(detached) != smartlist_len(detached_2)) {
|
|
|
|
log_warn(LD_BUG, "List of detached circuits had the wrong length! "
|
|
|
|
"(got %d, should have gotten %d)",
|
|
|
|
(int)smartlist_len(detached),
|
|
|
|
(int)smartlist_len(detached_2));
|
|
|
|
badlen = 1;
|
|
|
|
}
|
|
|
|
smartlist_sort_pointers(detached);
|
|
|
|
smartlist_sort_pointers(detached_2);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(detached, circuit_t *, c,
|
|
|
|
if (c != smartlist_get(detached_2, c_sl_idx))
|
|
|
|
mismatch = 1;
|
|
|
|
);
|
|
|
|
|
|
|
|
if (mismatch)
|
|
|
|
log_warn(LD_BUG, "Mismatch in list of detached circuits.");
|
|
|
|
|
|
|
|
if (badlen || mismatch) {
|
|
|
|
smartlist_free(detached);
|
|
|
|
detached = detached_2;
|
|
|
|
} else {
|
|
|
|
log_notice(LD_CIRC, "List of %d circuits was as expected.",
|
|
|
|
(int)smartlist_len(detached));
|
|
|
|
smartlist_free(detached_2);
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(DEBUG_CIRCUIT_UNLINK_ALL) */
|
2013-09-09 19:48:44 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(detached, circuit_t *, circ) {
|
2006-07-23 09:37:35 +02:00
|
|
|
int mark = 0;
|
2012-08-08 15:02:47 +02:00
|
|
|
if (circ->n_chan == chan) {
|
2013-09-09 19:48:44 +02:00
|
|
|
|
2012-11-22 01:32:38 +01:00
|
|
|
circuit_set_n_circid_chan(circ, 0, NULL);
|
|
|
|
mark = 1;
|
2012-12-10 08:47:04 +01:00
|
|
|
|
2012-11-22 01:32:38 +01:00
|
|
|
/* If we didn't request this closure, pass the remote
|
2012-12-10 08:47:04 +01:00
|
|
|
* bit to mark_for_close. */
|
2012-11-22 01:32:38 +01:00
|
|
|
if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
|
|
|
|
reason |= END_CIRC_REASON_FLAG_REMOTE;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
2012-08-08 15:02:47 +02:00
|
|
|
if (or_circ->p_chan == chan) {
|
|
|
|
circuit_set_p_circid_chan(or_circ, 0, NULL);
|
2006-07-23 09:37:35 +02:00
|
|
|
mark = 1;
|
|
|
|
}
|
2005-12-05 20:45:54 +01:00
|
|
|
}
|
2013-09-09 19:48:44 +02:00
|
|
|
if (!mark) {
|
|
|
|
log_warn(LD_BUG, "Circuit on detached list which I had no reason "
|
|
|
|
"to mark");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!circ->marked_for_close)
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_mark_for_close(circ, reason);
|
2013-09-09 19:48:44 +02:00
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
|
|
|
|
|
|
|
smartlist_free(detached);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:15:24 +01:00
|
|
|
/** Return a circ such that
|
|
|
|
* - circ-\>rend_data-\>onion_address is equal to
|
|
|
|
* <b>rend_data</b>-\>onion_address,
|
|
|
|
* - circ-\>rend_data-\>rend_cookie is equal to
|
|
|
|
* <b>rend_data</b>-\>rend_cookie, and
|
|
|
|
* - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
|
2004-05-13 09:24:49 +02:00
|
|
|
*
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
2006-07-26 21:05:34 +02:00
|
|
|
origin_circuit_t *
|
2011-12-22 16:15:24 +01:00
|
|
|
circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (!circ->marked_for_close &&
|
2011-12-22 16:15:24 +01:00
|
|
|
circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
|
2008-09-24 16:44:29 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2016-05-31 20:51:30 +02:00
|
|
|
if (ocirc->rend_data == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!rend_cmp_service_ids(rend_data_get_address(rend_data),
|
|
|
|
rend_data_get_address(ocirc->rend_data)) &&
|
2011-12-22 16:15:24 +01:00
|
|
|
tor_memeq(ocirc->rend_data->rend_cookie,
|
|
|
|
rend_data->rend_cookie,
|
|
|
|
REND_COOKIE_LEN))
|
2008-09-24 16:44:29 +02:00
|
|
|
return ocirc;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-21 19:34:45 +01:00
|
|
|
/** Return the first service introduction circuit originating from the global
|
|
|
|
* circuit list after <b>start</b> or at the start of the list if <b>start</b>
|
|
|
|
* is NULL. Return NULL if no circuit is found.
|
|
|
|
*
|
|
|
|
* A service introduction point circuit has a purpose of either
|
|
|
|
* CIRCUIT_PURPOSE_S_ESTABLISH_INTRO or CIRCUIT_PURPOSE_S_INTRO. This does not
|
|
|
|
* return a circuit marked for close and its state must be open. */
|
|
|
|
origin_circuit_t *
|
|
|
|
circuit_get_next_service_intro_circ(origin_circuit_t *start)
|
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
smartlist_t *lst = circuit_get_global_list();
|
|
|
|
|
|
|
|
if (start) {
|
|
|
|
idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; idx < smartlist_len(lst); ++idx) {
|
|
|
|
circuit_t *circ = smartlist_get(lst, idx);
|
|
|
|
|
|
|
|
/* Ignore a marked for close circuit or purpose not matching a service
|
|
|
|
* intro point or if the state is not open. */
|
|
|
|
if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
|
|
|
|
(circ->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_S_INTRO)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* The purposes we are looking for are only for origin circuits so the
|
|
|
|
* following is valid. */
|
|
|
|
return TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
}
|
|
|
|
/* Not found. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:30:46 +01:00
|
|
|
/** Return the first service rendezvous circuit originating from the global
|
|
|
|
* circuit list after <b>start</b> or at the start of the list if <b>start</b>
|
|
|
|
* is NULL. Return NULL if no circuit is found.
|
|
|
|
*
|
|
|
|
* A service rendezvous point circuit has a purpose of either
|
|
|
|
* CIRCUIT_PURPOSE_S_CONNECT_REND or CIRCUIT_PURPOSE_S_REND_JOINED. This does
|
|
|
|
* not return a circuit marked for close and its state must be open. */
|
|
|
|
origin_circuit_t *
|
|
|
|
circuit_get_next_service_rp_circ(origin_circuit_t *start)
|
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
smartlist_t *lst = circuit_get_global_list();
|
|
|
|
|
|
|
|
if (start) {
|
|
|
|
idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; idx < smartlist_len(lst); ++idx) {
|
|
|
|
circuit_t *circ = smartlist_get(lst, idx);
|
|
|
|
|
|
|
|
/* Ignore a marked for close circuit or purpose not matching a service
|
|
|
|
* intro point or if the state is not open. */
|
|
|
|
if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
|
|
|
|
(circ->purpose != CIRCUIT_PURPOSE_S_CONNECT_REND &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* The purposes we are looking for are only for origin circuits so the
|
|
|
|
* following is valid. */
|
|
|
|
return TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
}
|
|
|
|
/* Not found. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Return the first circuit originating here in global_circuitlist after
|
2016-05-31 20:51:30 +02:00
|
|
|
* <b>start</b> whose purpose is <b>purpose</b>, and where <b>digest</b> (if
|
|
|
|
* set) matches the private key digest of the rend data associated with the
|
|
|
|
* circuit. Return NULL if no circuit is found. If <b>start</b> is NULL,
|
|
|
|
* begin at the start of the list.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2006-07-26 21:05:41 +02:00
|
|
|
origin_circuit_t *
|
|
|
|
circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
|
2016-05-31 20:51:30 +02:00
|
|
|
const uint8_t *digest, uint8_t purpose)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2014-08-15 23:02:28 +02:00
|
|
|
int idx;
|
|
|
|
smartlist_t *lst = circuit_get_global_list();
|
2006-07-26 21:05:41 +02:00
|
|
|
tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
|
2004-05-13 09:24:49 +02:00
|
|
|
if (start == NULL)
|
2014-08-15 23:02:28 +02:00
|
|
|
idx = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
else
|
2014-08-15 23:02:28 +02:00
|
|
|
idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
|
|
|
|
|
|
|
|
for ( ; idx < smartlist_len(lst); ++idx) {
|
|
|
|
circuit_t *circ = smartlist_get(lst, idx);
|
2016-05-31 20:51:30 +02:00
|
|
|
origin_circuit_t *ocirc;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
if (circ->purpose != purpose)
|
|
|
|
continue;
|
2016-05-31 20:51:30 +02:00
|
|
|
/* At this point we should be able to get a valid origin circuit because
|
|
|
|
* the origin purpose we are looking for matches this circuit. */
|
|
|
|
if (BUG(!CIRCUIT_PURPOSE_IS_ORIGIN(circ->purpose))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2006-09-15 07:53:00 +02:00
|
|
|
if (!digest)
|
2016-05-31 20:51:30 +02:00
|
|
|
return ocirc;
|
|
|
|
if (rend_circuit_pk_digest_eq(ocirc, digest)) {
|
|
|
|
return ocirc;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:13:22 +02:00
|
|
|
/** We might cannibalize this circuit: Return true if its last hop can be used
|
|
|
|
* as a v3 rendezvous point. */
|
|
|
|
static int
|
|
|
|
circuit_can_be_cannibalized_for_v3_rp(const origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
if (!circ->build_state) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extend_info_t *chosen_exit = circ->build_state->chosen_exit;
|
|
|
|
if (BUG(!chosen_exit)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const node_t *rp_node = node_get_by_id(chosen_exit->identity_digest);
|
|
|
|
if (rp_node) {
|
|
|
|
if (node_supports_v3_rendezvous_point(rp_node)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-22 07:35:29 +01:00
|
|
|
/** We are trying to create a circuit of purpose <b>purpose</b> and we are
|
|
|
|
* looking for cannibalizable circuits. Return the circuit purpose we would be
|
|
|
|
* willing to cannibalize. */
|
|
|
|
static uint8_t
|
|
|
|
get_circuit_purpose_needed_to_cannibalize(uint8_t purpose)
|
|
|
|
{
|
|
|
|
if (circuit_should_use_vanguards(purpose)) {
|
|
|
|
/* If we are using vanguards, then we should only cannibalize vanguard
|
|
|
|
* circuits so that we get the same path construction logic. */
|
|
|
|
return CIRCUIT_PURPOSE_HS_VANGUARDS;
|
|
|
|
} else {
|
|
|
|
/* If no vanguards are used just get a general circuit! */
|
|
|
|
return CIRCUIT_PURPOSE_C_GENERAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-19 09:04:24 +02:00
|
|
|
/** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
|
2007-12-23 20:15:22 +01:00
|
|
|
* has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
|
|
|
|
* flags in <b>flags</b>, and if info is defined, does not already use info
|
2005-11-11 20:25:30 +01:00
|
|
|
* as any of its hops; or NULL if no circuit fits this description.
|
2005-01-20 00:13:20 +01:00
|
|
|
*
|
2017-12-22 07:35:29 +01:00
|
|
|
* The <b>purpose</b> argument refers to the purpose of the circuit we want to
|
|
|
|
* create, not the purpose of the circuit we want to cannibalize.
|
2008-12-18 18:18:14 +01:00
|
|
|
*
|
2007-12-23 20:15:22 +01:00
|
|
|
* If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
|
2016-10-26 18:38:50 +02:00
|
|
|
*
|
|
|
|
* To "cannibalize" a circuit means to extend it an extra hop, and use it
|
|
|
|
* for some other purpose than we had originally intended. We do this when
|
|
|
|
* we want to perform some low-bandwidth task at a specific relay, and we
|
|
|
|
* would like the circuit to complete as soon as possible. (If we were going
|
|
|
|
* to use a lot of bandwidth, we wouldn't want a circuit with an extra hop.
|
|
|
|
* If we didn't care about circuit completion latency, we would just build
|
|
|
|
* a new circuit.)
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *
|
2017-12-22 07:35:29 +01:00
|
|
|
circuit_find_to_cannibalize(uint8_t purpose_to_produce, extend_info_t *info,
|
2007-12-23 20:15:22 +01:00
|
|
|
int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *best=NULL;
|
2008-03-24 19:43:01 +01:00
|
|
|
int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
|
|
|
|
int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
|
|
|
|
int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2017-12-22 07:35:29 +01:00
|
|
|
/* We want the circuit we are trying to cannibalize to have this purpose */
|
|
|
|
int purpose_to_search_for;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2010-03-01 22:26:44 +01:00
|
|
|
/* Make sure we're not trying to create a onehop circ by
|
|
|
|
* cannibalization. */
|
|
|
|
tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
|
|
|
|
|
2017-12-22 07:35:29 +01:00
|
|
|
purpose_to_search_for = get_circuit_purpose_needed_to_cannibalize(
|
|
|
|
purpose_to_produce);
|
|
|
|
|
|
|
|
tor_assert_nonfatal(purpose_to_search_for == CIRCUIT_PURPOSE_C_GENERAL ||
|
|
|
|
purpose_to_search_for == CIRCUIT_PURPOSE_HS_VANGUARDS);
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,
|
|
|
|
"Hunting for a circ to cannibalize: purpose %d, uptime %d, "
|
|
|
|
"capacity %d, internal %d",
|
2017-12-22 07:35:29 +01:00
|
|
|
purpose_to_produce, need_uptime, need_capacity, internal);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ_) {
|
2012-10-12 18:22:13 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ_) &&
|
|
|
|
circ_->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
!circ_->marked_for_close &&
|
2017-12-22 07:35:29 +01:00
|
|
|
circ_->purpose == purpose_to_search_for &&
|
2012-10-12 18:22:13 +02:00
|
|
|
!circ_->timestamp_dirty) {
|
|
|
|
origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
|
2017-12-22 07:35:29 +01:00
|
|
|
|
|
|
|
/* Only cannibalize from reasonable length circuits. If we
|
|
|
|
* want C_GENERAL, then only choose 3 hop circs. If we want
|
|
|
|
* HS_VANGUARDS, only choose 4 hop circs.
|
|
|
|
*/
|
|
|
|
if (circ->build_state->desired_path_len !=
|
|
|
|
route_len_for_purpose(purpose_to_search_for, NULL)) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:23:45 +01:00
|
|
|
/* Ignore any circuits for which we can't use the Guard. It is possible
|
|
|
|
* that the Guard was removed from the samepled set after the circuit
|
|
|
|
* was created so avoid using it. */
|
|
|
|
if (!entry_guard_could_succeed(circ->guard_state)) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if ((!need_uptime || circ->build_state->need_uptime) &&
|
|
|
|
(!need_capacity || circ->build_state->need_capacity) &&
|
2008-12-29 20:55:13 +01:00
|
|
|
(internal == circ->build_state->is_internal) &&
|
2013-02-20 00:29:17 +01:00
|
|
|
!circ->unusable_for_new_conns &&
|
2010-03-01 22:26:44 +01:00
|
|
|
circ->remaining_relay_early_cells &&
|
2011-07-20 20:40:47 +02:00
|
|
|
!circ->build_state->onehop_tunnel &&
|
|
|
|
!circ->isolation_values_set) {
|
2006-07-23 09:37:35 +02:00
|
|
|
if (info) {
|
|
|
|
/* need to make sure we don't duplicate hops */
|
|
|
|
crypt_path_t *hop = circ->cpath;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *ri1 = node_get_by_id(info->identity_digest);
|
2006-07-23 09:37:35 +02:00
|
|
|
do {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *ri2;
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(hop->extend_info->identity_digest,
|
2015-02-17 12:01:33 +01:00
|
|
|
info->identity_digest, DIGEST_LEN))
|
2006-07-23 09:37:35 +02:00
|
|
|
goto next;
|
2007-10-11 23:40:35 +02:00
|
|
|
if (ri1 &&
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
(ri2 = node_get_by_id(hop->extend_info->identity_digest))
|
|
|
|
&& nodes_in_same_family(ri1, ri2))
|
2007-10-11 23:40:35 +02:00
|
|
|
goto next;
|
2006-07-23 09:37:35 +02:00
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
|
|
|
}
|
2011-04-04 01:58:28 +02:00
|
|
|
if (options->ExcludeNodes) {
|
|
|
|
/* Make sure no existing nodes in the circuit are excluded for
|
|
|
|
* general use. (This may be possible if StrictNodes is 0, and we
|
|
|
|
* thought we needed to use an otherwise excluded node for, say, a
|
|
|
|
* directory operation.) */
|
|
|
|
crypt_path_t *hop = circ->cpath;
|
|
|
|
do {
|
|
|
|
if (routerset_contains_extendinfo(options->ExcludeNodes,
|
|
|
|
hop->extend_info))
|
|
|
|
goto next;
|
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != circ->cpath);
|
|
|
|
}
|
2017-08-30 16:13:22 +02:00
|
|
|
|
|
|
|
if ((flags & CIRCLAUNCH_IS_V3_RP) &&
|
|
|
|
!circuit_can_be_cannibalized_for_v3_rp(circ)) {
|
|
|
|
log_debug(LD_GENERAL, "Skipping uncannibalizable circuit for v3 "
|
|
|
|
"rendezvous point.");
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (!best || (best->build_state->need_uptime && !need_uptime))
|
|
|
|
best = circ;
|
2005-11-11 20:28:32 +01:00
|
|
|
next: ;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
2005-01-20 00:13:20 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ_);
|
2005-01-20 00:13:20 +01:00
|
|
|
return best;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-21 23:23:25 +01:00
|
|
|
/**
|
|
|
|
* Check whether any of the origin circuits that are waiting to see if
|
|
|
|
* their guard is good enough to use can be upgraded to "ready". If so,
|
|
|
|
* return a new smartlist containing them. Otherwise return NULL.
|
|
|
|
*/
|
|
|
|
smartlist_t *
|
|
|
|
circuit_find_circuits_to_upgrade_from_guard_wait(void)
|
|
|
|
{
|
|
|
|
/* Only if some circuit is actually waiting on an upgrade should we
|
|
|
|
* run the algorithm. */
|
|
|
|
if (! circuits_pending_other_guards ||
|
|
|
|
smartlist_len(circuits_pending_other_guards)==0)
|
|
|
|
return NULL;
|
2016-12-06 20:34:48 +01:00
|
|
|
/* Only if we have some origin circuits should we run the algorithm. */
|
2016-11-21 23:23:25 +01:00
|
|
|
if (!global_origin_circuit_list)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Okay; we can pass our circuit list to entrynodes.c.*/
|
|
|
|
smartlist_t *result = smartlist_new();
|
2016-12-06 20:34:48 +01:00
|
|
|
int circuits_upgraded = entry_guards_upgrade_waiting_circuits(
|
|
|
|
get_guard_selection_info(),
|
|
|
|
global_origin_circuit_list,
|
|
|
|
result);
|
|
|
|
if (circuits_upgraded && smartlist_len(result)) {
|
2016-11-21 23:23:25 +01:00
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
smartlist_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 04:30:20 +02:00
|
|
|
/** Return the number of hops in circuit's path. If circ has no entries,
|
|
|
|
* or is NULL, returns 0. */
|
2006-10-01 22:50:11 +02:00
|
|
|
int
|
|
|
|
circuit_get_cpath_len(origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
if (circ && circ->cpath) {
|
|
|
|
crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
|
|
|
|
cpath_next = cpath->next;
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-08-04 23:16:38 +02:00
|
|
|
/** Return the number of opened hops in circuit's path.
|
|
|
|
* If circ has no entries, or is NULL, returns 0. */
|
|
|
|
int
|
2017-12-07 01:04:09 +01:00
|
|
|
circuit_get_cpath_opened_len(const origin_circuit_t *circ)
|
2017-08-04 23:16:38 +02:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
if (circ && circ->cpath) {
|
|
|
|
crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
for (cpath = circ->cpath;
|
|
|
|
cpath->state == CPATH_STATE_OPEN
|
|
|
|
&& cpath_next != circ->cpath;
|
|
|
|
cpath = cpath_next) {
|
|
|
|
cpath_next = cpath->next;
|
2006-10-01 22:50:11 +02:00
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-04-30 13:10:45 +02:00
|
|
|
/** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
|
2016-07-08 04:30:20 +02:00
|
|
|
* aren't that many hops in the list. <b>hopnum</b> starts at 1.
|
|
|
|
* Returns NULL if <b>hopnum</b> is 0 or negative. */
|
2007-04-30 13:10:45 +02:00
|
|
|
crypt_path_t *
|
|
|
|
circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
|
|
|
|
{
|
2007-04-30 16:09:11 +02:00
|
|
|
if (circ && circ->cpath && hopnum > 0) {
|
2007-04-30 13:10:45 +02:00
|
|
|
crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
|
|
|
|
cpath_next = cpath->next;
|
|
|
|
if (--hopnum <= 0)
|
|
|
|
return cpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-20 00:58:42 +01:00
|
|
|
/** Go through the circuitlist; mark-for-close each circuit that starts
|
|
|
|
* at us but has not yet been used. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_mark_all_unused_circs(void)
|
|
|
|
{
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
2005-03-20 00:58:42 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
!circ->timestamp_dirty)
|
2006-10-17 17:20:00 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
|
2005-03-20 00:58:42 +01:00
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2005-03-20 00:58:42 +01:00
|
|
|
}
|
|
|
|
|
2005-10-06 00:31:21 +02:00
|
|
|
/** Go through the circuitlist; for each circuit that starts at us
|
|
|
|
* and is dirty, frob its timestamp_dirty so we won't use it for any
|
|
|
|
* new streams.
|
|
|
|
*
|
|
|
|
* This is useful for letting the user change pseudonyms, so new
|
|
|
|
* streams will not be linkable to old streams.
|
|
|
|
*/
|
|
|
|
void
|
2013-02-20 00:37:03 +01:00
|
|
|
circuit_mark_all_dirty_circs_as_unusable(void)
|
2005-10-06 00:31:21 +02:00
|
|
|
{
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
|
2005-10-06 00:31:21 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
2013-02-20 00:29:17 +01:00
|
|
|
circ->timestamp_dirty) {
|
|
|
|
mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
|
|
|
|
}
|
2005-10-06 00:31:21 +02:00
|
|
|
}
|
2014-08-15 22:23:22 +02:00
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2005-10-06 00:31:21 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Mark <b>circ</b> to be closed next time we call
|
|
|
|
* circuit_close_all_marked(). Do any cleanup needed:
|
|
|
|
* - If state is onionskin_pending, remove circ from the onion_pending
|
|
|
|
* list.
|
2004-10-24 19:11:44 +02:00
|
|
|
* - If circ isn't open yet: call circuit_build_failed() if we're
|
2018-02-06 18:43:55 +01:00
|
|
|
* the origin.
|
2011-09-17 12:19:29 +02:00
|
|
|
* - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
|
|
|
|
* failure we just had to the hidden service client module.
|
2011-09-17 12:53:07 +02:00
|
|
|
* - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
|
|
|
|
* report to the hidden service client module that the intro point
|
|
|
|
* we just tried may be unreachable.
|
2004-05-13 09:24:49 +02:00
|
|
|
* - Send appropriate destroys and edge_destroys for conns and
|
|
|
|
* streams attached to circ.
|
|
|
|
* - If circ->rend_splice is set (we are the midpoint of a joined
|
|
|
|
* rendezvous stream), then mark the other circuit to close as well.
|
|
|
|
*/
|
2014-01-13 15:52:07 +01:00
|
|
|
MOCK_IMPL(void,
|
|
|
|
circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
|
|
|
|
const char *file))
|
2005-04-03 07:22:33 +02:00
|
|
|
{
|
2006-10-09 17:47:50 +02:00
|
|
|
int orig_reason = reason; /* Passed to the controller */
|
2004-05-13 09:24:49 +02:00
|
|
|
assert_circuit_ok(circ);
|
2005-04-03 07:22:33 +02:00
|
|
|
tor_assert(line);
|
|
|
|
tor_assert(file);
|
|
|
|
|
|
|
|
if (circ->marked_for_close) {
|
2013-02-01 21:43:37 +01:00
|
|
|
log_warn(LD_BUG,
|
2005-10-19 00:56:40 +02:00
|
|
|
"Duplicate call to circuit_mark_for_close at %s:%d"
|
2005-04-03 07:31:41 +02:00
|
|
|
" (first at %s:%d)", file, line,
|
2005-04-03 07:22:33 +02:00
|
|
|
circ->marked_for_close_file, circ->marked_for_close);
|
|
|
|
return;
|
|
|
|
}
|
2006-01-05 22:23:03 +01:00
|
|
|
if (reason == END_CIRC_AT_ORIGIN) {
|
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ)) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
|
|
|
|
"but circuit was not at origin. (called %s:%d, purpose=%d)",
|
|
|
|
file, line, circ->purpose);
|
2006-01-05 22:23:03 +01:00
|
|
|
}
|
|
|
|
reason = END_CIRC_REASON_NONE;
|
2007-07-11 05:37:45 +02:00
|
|
|
}
|
2012-12-10 08:47:04 +01:00
|
|
|
|
2007-07-11 05:37:45 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2012-12-12 20:53:18 +01:00
|
|
|
if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
|
|
|
|
/* Don't close it yet, we need to test it first */
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 01:30:50 +01:00
|
|
|
|
2007-07-11 05:37:45 +02:00
|
|
|
/* We don't send reasons when closing circuits at the origin. */
|
2006-01-05 22:23:03 +01:00
|
|
|
reason = END_CIRC_REASON_NONE;
|
|
|
|
}
|
2006-10-20 01:04:49 +02:00
|
|
|
|
2006-11-01 02:30:25 +01:00
|
|
|
if (reason & END_CIRC_REASON_FLAG_REMOTE)
|
2006-10-20 01:04:49 +02:00
|
|
|
reason &= ~END_CIRC_REASON_FLAG_REMOTE;
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
|
2006-11-01 00:35:50 +01:00
|
|
|
if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
|
|
|
|
log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
|
|
|
|
reason = END_CIRC_REASON_NONE;
|
2006-01-05 22:23:03 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2015-10-02 17:55:25 +02:00
|
|
|
circ->marked_for_close = line;
|
|
|
|
circ->marked_for_close_file = file;
|
|
|
|
circ->marked_for_close_reason = reason;
|
|
|
|
circ->marked_for_close_orig_reason = orig_reason;
|
|
|
|
|
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
if (or_circ->rend_splice) {
|
|
|
|
if (!or_circ->rend_splice->base_.marked_for_close) {
|
|
|
|
/* do this after marking this circuit, to avoid infinite recursion. */
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
|
|
|
|
}
|
|
|
|
or_circ->rend_splice = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 17:49:48 +01:00
|
|
|
/* Notify the HS subsystem that this circuit is closing. */
|
|
|
|
hs_circ_cleanup(circ);
|
|
|
|
|
2015-10-02 17:55:25 +02:00
|
|
|
if (circuits_pending_close == NULL)
|
|
|
|
circuits_pending_close = smartlist_new();
|
|
|
|
|
|
|
|
smartlist_add(circuits_pending_close, circ);
|
2017-09-11 22:10:18 +02:00
|
|
|
|
2017-09-20 17:56:44 +02:00
|
|
|
log_info(LD_GENERAL, "Circuit %u (id: %" PRIu32 ") marked for close at "
|
2017-09-25 22:01:12 +02:00
|
|
|
"%s:%d (orig reason: %d, new reason: %d)",
|
2017-09-20 17:56:44 +02:00
|
|
|
circ->n_circ_id,
|
|
|
|
CIRCUIT_IS_ORIGIN(circ) ?
|
|
|
|
TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0,
|
|
|
|
file, line, orig_reason, reason);
|
2015-10-02 17:55:25 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 06:35:03 +01:00
|
|
|
/** Called immediately before freeing a marked circuit <b>circ</b> from
|
|
|
|
* circuit_free_all() while shutting down Tor; this is a safe-at-shutdown
|
|
|
|
* version of circuit_about_to_free(). It's important that it at least
|
|
|
|
* do circuitmux_detach_circuit() when appropriate.
|
|
|
|
*/
|
|
|
|
static void
|
2016-02-11 18:15:12 +01:00
|
|
|
circuit_about_to_free_atexit(circuit_t *circ)
|
2016-02-10 06:35:03 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
if (circ->n_chan) {
|
|
|
|
circuit_clear_cell_queue(circ, circ->n_chan);
|
|
|
|
circuitmux_detach_circuit(circ->n_chan->cmux, circ);
|
|
|
|
circuit_set_n_circid_chan(circ, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
|
|
|
|
if (or_circ->p_chan) {
|
|
|
|
circuit_clear_cell_queue(circ, or_circ->p_chan);
|
|
|
|
circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
|
|
|
|
circuit_set_p_circid_chan(or_circ, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 17:55:25 +02:00
|
|
|
/** Called immediately before freeing a marked circuit <b>circ</b>.
|
|
|
|
* Disconnects the circuit from other data structures, launches events
|
|
|
|
* as appropriate, and performs other housekeeping.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_about_to_free(circuit_t *circ)
|
|
|
|
{
|
|
|
|
|
|
|
|
int reason = circ->marked_for_close_reason;
|
|
|
|
int orig_reason = circ->marked_for_close_orig_reason;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
2006-07-23 09:37:35 +02:00
|
|
|
onion_pending_remove(TO_OR_CIRCUIT(circ));
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
/* If the circuit ever became OPEN, we sent it to the reputation history
|
|
|
|
* module then. If it isn't OPEN, we send it there now to remember which
|
|
|
|
* links worked and which didn't.
|
|
|
|
*/
|
2016-11-21 22:59:00 +01:00
|
|
|
if (circ->state != CIRCUIT_STATE_OPEN &&
|
|
|
|
circ->state != CIRCUIT_STATE_GUARD_WAIT) {
|
2004-11-28 10:05:49 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
circuit_build_failed(ocirc); /* take actions if necessary */
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
|
|
|
|
if (circuits_pending_chans)
|
|
|
|
smartlist_remove(circuits_pending_chans, circ);
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
2017-01-17 12:26:59 +01:00
|
|
|
if (circuits_pending_other_guards) {
|
|
|
|
smartlist_remove(circuits_pending_other_guards, circ);
|
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
|
2016-11-21 22:59:00 +01:00
|
|
|
(circ->state == CIRCUIT_STATE_OPEN ||
|
|
|
|
circ->state == CIRCUIT_STATE_GUARD_WAIT) ?
|
|
|
|
CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
|
2006-10-09 17:47:27 +02:00
|
|
|
orig_reason);
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2015-10-02 17:55:25 +02:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2011-10-12 15:41:33 +02:00
|
|
|
int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OPEN);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(ocirc->build_state->chosen_exit);
|
2017-07-25 17:37:55 +02:00
|
|
|
if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
|
|
|
|
ocirc->rend_data) {
|
2014-11-10 20:38:53 +01:00
|
|
|
/* treat this like getting a nack from it */
|
|
|
|
log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
|
2016-05-31 20:51:30 +02:00
|
|
|
safe_str_client(rend_data_get_address(ocirc->rend_data)),
|
2014-11-10 20:38:53 +01:00
|
|
|
safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
|
|
|
|
timed_out ? "Recording timeout." : "Removing from descriptor.");
|
|
|
|
rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
|
|
|
|
ocirc->rend_data,
|
|
|
|
timed_out ?
|
|
|
|
INTRO_POINT_FAILURE_TIMEOUT :
|
|
|
|
INTRO_POINT_FAILURE_GENERIC);
|
|
|
|
}
|
2011-09-17 12:53:07 +02:00
|
|
|
} else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
|
2011-10-12 15:41:33 +02:00
|
|
|
reason != END_CIRC_REASON_TIMEOUT) {
|
2011-09-17 12:53:07 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2011-12-28 18:02:14 +01:00
|
|
|
if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
|
2017-07-25 17:37:55 +02:00
|
|
|
if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
|
|
|
|
ocirc->rend_data) {
|
2014-11-10 20:38:53 +01:00
|
|
|
log_info(LD_REND, "Failed intro circ %s to %s "
|
|
|
|
"(building circuit to intro point). "
|
|
|
|
"Marking intro point as possibly unreachable.",
|
2016-05-31 20:51:30 +02:00
|
|
|
safe_str_client(rend_data_get_address(ocirc->rend_data)),
|
2014-11-12 19:15:10 +01:00
|
|
|
safe_str_client(build_state_get_exit_nickname(
|
|
|
|
ocirc->build_state)));
|
2014-11-10 20:38:53 +01:00
|
|
|
rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
|
2014-11-12 19:15:10 +01:00
|
|
|
ocirc->rend_data,
|
|
|
|
INTRO_POINT_FAILURE_UNREACHABLE);
|
2014-11-10 20:38:53 +01:00
|
|
|
}
|
2011-12-28 18:02:14 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2014-11-10 20:38:53 +01:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (circ->n_chan) {
|
|
|
|
circuit_clear_cell_queue(circ, circ->n_chan);
|
2012-11-07 02:58:59 +01:00
|
|
|
/* Only send destroy if the channel isn't closing anyway */
|
2014-11-23 20:27:15 +01:00
|
|
|
if (!CHANNEL_CONDEMNED(circ->n_chan)) {
|
2012-11-07 02:58:59 +01:00
|
|
|
channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
|
|
|
|
}
|
2012-10-01 22:06:10 +02:00
|
|
|
circuitmux_detach_circuit(circ->n_chan->cmux, circ);
|
2014-03-12 15:57:08 +01:00
|
|
|
circuit_set_n_circid_chan(circ, 0, NULL);
|
2010-07-31 00:55:24 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
|
|
|
|
connection_edge_destroy(or_circ->p_circ_id, conn);
|
2009-11-23 16:13:50 +01:00
|
|
|
or_circ->n_streams = NULL;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
while (or_circ->resolving_streams) {
|
|
|
|
conn = or_circ->resolving_streams;
|
|
|
|
or_circ->resolving_streams = conn->next_stream;
|
2012-10-12 18:22:13 +02:00
|
|
|
if (!conn->base_.marked_for_close) {
|
2007-02-07 19:25:04 +01:00
|
|
|
/* The client will see a DESTROY, and infer that the connections
|
2006-07-23 09:37:35 +02:00
|
|
|
* are closing because the circuit is getting torn down. No need
|
|
|
|
* to send an end cell. */
|
2008-12-17 15:59:28 +01:00
|
|
|
conn->edge_has_sent_end = 1;
|
2006-10-20 19:54:48 +02:00
|
|
|
conn->end_reason = END_STREAM_REASON_DESTROY;
|
2007-02-07 07:54:22 +01:00
|
|
|
conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
conn->on_circuit = NULL;
|
2005-03-02 04:13:05 +01:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (or_circ->p_chan) {
|
|
|
|
circuit_clear_cell_queue(circ, or_circ->p_chan);
|
2012-11-07 02:58:59 +01:00
|
|
|
/* Only send destroy if the channel isn't closing anyway */
|
2014-11-23 20:27:15 +01:00
|
|
|
if (!CHANNEL_CONDEMNED(or_circ->p_chan)) {
|
2012-11-07 02:58:59 +01:00
|
|
|
channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
|
|
|
|
}
|
2012-10-01 22:06:10 +02:00
|
|
|
circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
|
2014-03-12 15:57:08 +01:00
|
|
|
circuit_set_p_circid_chan(or_circ, 0, NULL);
|
2010-07-31 00:55:24 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
} else {
|
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
|
|
|
|
connection_edge_destroy(circ->n_circ_id, conn);
|
2009-11-23 16:13:50 +01:00
|
|
|
ocirc->p_streams = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-16 15:55:44 +02:00
|
|
|
/** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
|
|
|
|
* recover memory. */
|
|
|
|
static void
|
|
|
|
marked_circuit_free_cells(circuit_t *circ)
|
|
|
|
{
|
|
|
|
if (!circ->marked_for_close) {
|
|
|
|
log_warn(LD_BUG, "Called on non-marked circuit");
|
|
|
|
return;
|
|
|
|
}
|
2013-06-18 16:25:10 +02:00
|
|
|
cell_queue_clear(&circ->n_chan_cells);
|
2016-09-22 01:01:12 +02:00
|
|
|
if (circ->n_mux)
|
|
|
|
circuitmux_clear_num_cells(circ->n_mux, circ);
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
|
|
|
|
cell_queue_clear(&orcirc->p_chan_cells);
|
|
|
|
if (orcirc->p_mux)
|
|
|
|
circuitmux_clear_num_cells(orcirc->p_mux, circ);
|
|
|
|
}
|
2013-06-16 15:55:44 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:21:50 +02:00
|
|
|
static size_t
|
2014-08-19 17:25:48 +02:00
|
|
|
single_conn_free_bytes(connection_t *conn)
|
2014-08-18 21:21:50 +02:00
|
|
|
{
|
|
|
|
size_t result = 0;
|
|
|
|
if (conn->inbuf) {
|
|
|
|
result += buf_allocation(conn->inbuf);
|
|
|
|
buf_clear(conn->inbuf);
|
|
|
|
}
|
|
|
|
if (conn->outbuf) {
|
|
|
|
result += buf_allocation(conn->outbuf);
|
|
|
|
buf_clear(conn->outbuf);
|
2017-09-29 16:00:14 +02:00
|
|
|
conn->outbuf_flushlen = 0;
|
2014-08-18 21:21:50 +02:00
|
|
|
}
|
2014-08-19 16:59:15 +02:00
|
|
|
if (conn->type == CONN_TYPE_DIR) {
|
|
|
|
dir_connection_t *dir_conn = TO_DIR_CONN(conn);
|
2017-04-18 01:10:25 +02:00
|
|
|
if (dir_conn->compress_state) {
|
|
|
|
result += tor_compress_state_size(dir_conn->compress_state);
|
|
|
|
tor_compress_free(dir_conn->compress_state);
|
|
|
|
dir_conn->compress_state = NULL;
|
2014-08-19 16:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-18 21:21:50 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-20 18:01:21 +01:00
|
|
|
/** Aggressively free buffer contents on all the buffers of all streams in the
|
|
|
|
* list starting at <b>stream</b>. Return the number of bytes recovered. */
|
2013-11-20 17:56:35 +01:00
|
|
|
static size_t
|
|
|
|
marked_circuit_streams_free_bytes(edge_connection_t *stream)
|
|
|
|
{
|
|
|
|
size_t result = 0;
|
|
|
|
for ( ; stream; stream = stream->next_stream) {
|
|
|
|
connection_t *conn = TO_CONN(stream);
|
2014-08-19 17:25:48 +02:00
|
|
|
result += single_conn_free_bytes(conn);
|
2014-08-18 21:21:50 +02:00
|
|
|
if (conn->linked_conn) {
|
2014-08-19 17:25:48 +02:00
|
|
|
result += single_conn_free_bytes(conn->linked_conn);
|
2013-11-20 17:56:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-20 18:01:21 +01:00
|
|
|
/** Aggressively free buffer contents on all the buffers of all streams on
|
|
|
|
* circuit <b>c</b>. Return the number of bytes recovered. */
|
2013-11-20 17:56:35 +01:00
|
|
|
static size_t
|
|
|
|
marked_circuit_free_stream_bytes(circuit_t *c)
|
|
|
|
{
|
|
|
|
if (CIRCUIT_IS_ORIGIN(c)) {
|
|
|
|
return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
|
|
|
|
} else {
|
|
|
|
return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-16 15:55:44 +02:00
|
|
|
/** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
|
2013-07-18 17:21:27 +02:00
|
|
|
STATIC size_t
|
2013-06-16 15:55:44 +02:00
|
|
|
n_cells_in_circ_queues(const circuit_t *c)
|
|
|
|
{
|
2013-06-18 16:25:10 +02:00
|
|
|
size_t n = c->n_chan_cells.n;
|
2013-07-16 20:48:12 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(c)) {
|
|
|
|
circuit_t *cc = (circuit_t *) c;
|
|
|
|
n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
|
|
|
|
}
|
2013-06-16 15:55:44 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:15:30 +01:00
|
|
|
/**
|
2017-11-21 18:28:16 +01:00
|
|
|
* Return the age of the oldest cell queued on <b>c</b>, in timestamp units.
|
2013-11-07 18:15:30 +01:00
|
|
|
* Return 0 if there are no cells queued on c. Requires that <b>now</b> be
|
2017-11-21 18:28:16 +01:00
|
|
|
* the current coarse timestamp.
|
2013-11-07 18:15:30 +01:00
|
|
|
*
|
|
|
|
* This function will return incorrect results if the oldest cell queued on
|
2017-11-21 18:28:16 +01:00
|
|
|
* the circuit is older than about 2**32 msec (about 49 days) old.
|
2013-11-07 18:15:30 +01:00
|
|
|
*/
|
2014-01-13 21:25:28 +01:00
|
|
|
STATIC uint32_t
|
2013-11-07 18:15:30 +01:00
|
|
|
circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
|
|
|
|
{
|
|
|
|
uint32_t age = 0;
|
2013-11-15 21:35:00 +01:00
|
|
|
packed_cell_t *cell;
|
|
|
|
|
|
|
|
if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
|
2017-11-21 18:28:16 +01:00
|
|
|
age = now - cell->inserted_timestamp;
|
2013-11-07 18:15:30 +01:00
|
|
|
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(c)) {
|
2014-04-29 03:02:59 +02:00
|
|
|
const or_circuit_t *orcirc = CONST_TO_OR_CIRCUIT(c);
|
2013-11-15 21:35:00 +01:00
|
|
|
if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
|
2017-11-21 18:28:16 +01:00
|
|
|
uint32_t age2 = now - cell->inserted_timestamp;
|
2013-11-07 18:15:30 +01:00
|
|
|
if (age2 > age)
|
|
|
|
return age2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return age;
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:28:16 +01:00
|
|
|
/** Return the age of the oldest buffer chunk on <b>conn</b>, where age is
|
|
|
|
* taken in timestamp units before the time <b>now</b>. If the connection has
|
|
|
|
* no data, treat it as having age zero.
|
2014-08-19 17:25:48 +02:00
|
|
|
**/
|
|
|
|
static uint32_t
|
2017-11-21 18:28:16 +01:00
|
|
|
conn_get_buffer_age(const connection_t *conn, uint32_t now_ts)
|
2014-08-19 17:25:48 +02:00
|
|
|
{
|
|
|
|
uint32_t age = 0, age2;
|
|
|
|
if (conn->outbuf) {
|
2017-11-21 18:28:16 +01:00
|
|
|
age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now_ts);
|
2014-08-19 17:25:48 +02:00
|
|
|
if (age2 > age)
|
|
|
|
age = age2;
|
|
|
|
}
|
|
|
|
if (conn->inbuf) {
|
2017-11-21 18:28:16 +01:00
|
|
|
age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now_ts);
|
2014-08-19 17:25:48 +02:00
|
|
|
if (age2 > age)
|
|
|
|
age = age2;
|
|
|
|
}
|
|
|
|
return age;
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:28:16 +01:00
|
|
|
/** Return the age in timestamp units of the oldest buffer chunk on any stream
|
|
|
|
* in the linked list <b>stream</b>, where age is taken in timestamp units
|
|
|
|
* before the timestamp <b>now</b>. */
|
2013-11-16 00:38:52 +01:00
|
|
|
static uint32_t
|
|
|
|
circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
|
|
|
|
{
|
|
|
|
uint32_t age = 0, age2;
|
|
|
|
for (; stream; stream = stream->next_stream) {
|
|
|
|
const connection_t *conn = TO_CONN(stream);
|
2014-08-19 17:25:48 +02:00
|
|
|
age2 = conn_get_buffer_age(conn, now);
|
|
|
|
if (age2 > age)
|
|
|
|
age = age2;
|
2014-08-19 17:27:52 +02:00
|
|
|
if (conn->linked_conn) {
|
|
|
|
age2 = conn_get_buffer_age(conn->linked_conn, now);
|
2013-11-16 00:38:52 +01:00
|
|
|
if (age2 > age)
|
|
|
|
age = age2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return age;
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:28:16 +01:00
|
|
|
/** Return the age in timestamp units of the oldest buffer chunk on any stream
|
|
|
|
* attached to the circuit <b>c</b>, where age is taken before the timestamp
|
|
|
|
* <b>now</b>. */
|
2014-01-13 21:25:28 +01:00
|
|
|
STATIC uint32_t
|
2013-11-16 00:38:52 +01:00
|
|
|
circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
|
|
|
|
{
|
|
|
|
if (CIRCUIT_IS_ORIGIN(c)) {
|
|
|
|
return circuit_get_streams_max_data_age(
|
2014-04-29 03:02:59 +02:00
|
|
|
CONST_TO_ORIGIN_CIRCUIT(c)->p_streams, now);
|
2013-11-16 00:38:52 +01:00
|
|
|
} else {
|
|
|
|
return circuit_get_streams_max_data_age(
|
2014-04-29 03:02:59 +02:00
|
|
|
CONST_TO_OR_CIRCUIT(c)->n_streams, now);
|
2013-11-16 00:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-07 18:15:30 +01:00
|
|
|
|
2013-11-20 18:01:21 +01:00
|
|
|
/** Return the age of the oldest cell or stream buffer chunk on the circuit
|
2017-11-21 18:28:16 +01:00
|
|
|
* <b>c</b>, where age is taken in timestamp units before the timestamp
|
|
|
|
* <b>now</b> */
|
2014-01-13 21:25:28 +01:00
|
|
|
STATIC uint32_t
|
2013-11-16 00:38:52 +01:00
|
|
|
circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
|
|
|
|
{
|
|
|
|
uint32_t cell_age = circuit_max_queued_cell_age(c, now);
|
|
|
|
uint32_t data_age = circuit_max_queued_data_age(c, now);
|
|
|
|
if (cell_age > data_age)
|
|
|
|
return cell_age;
|
|
|
|
else
|
|
|
|
return data_age;
|
|
|
|
}
|
2013-11-07 18:15:30 +01:00
|
|
|
|
2013-11-16 00:38:52 +01:00
|
|
|
/** Helper to sort a list of circuit_t by age of oldest item, in descending
|
|
|
|
* order. */
|
2013-06-16 15:55:44 +02:00
|
|
|
static int
|
2013-11-16 00:38:52 +01:00
|
|
|
circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
|
2013-06-16 15:55:44 +02:00
|
|
|
{
|
|
|
|
const circuit_t *a = *a_;
|
|
|
|
const circuit_t *b = *b_;
|
2013-11-16 00:38:52 +01:00
|
|
|
uint32_t age_a = a->age_tmp;
|
|
|
|
uint32_t age_b = b->age_tmp;
|
2013-06-16 15:55:44 +02:00
|
|
|
|
2013-11-07 18:15:30 +01:00
|
|
|
if (age_a < age_b)
|
2013-06-16 15:55:44 +02:00
|
|
|
return 1;
|
2013-11-07 18:15:30 +01:00
|
|
|
else if (age_a == age_b)
|
2013-06-16 15:55:44 +02:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:28:16 +01:00
|
|
|
static uint32_t now_ts_for_buf_cmp;
|
2014-08-19 17:25:48 +02:00
|
|
|
|
|
|
|
/** Helper to sort a list of circuit_t by age of oldest item, in descending
|
|
|
|
* order. */
|
|
|
|
static int
|
|
|
|
conns_compare_by_buffer_age_(const void **a_, const void **b_)
|
|
|
|
{
|
|
|
|
const connection_t *a = *a_;
|
|
|
|
const connection_t *b = *b_;
|
2017-11-21 18:28:16 +01:00
|
|
|
time_t age_a = conn_get_buffer_age(a, now_ts_for_buf_cmp);
|
|
|
|
time_t age_b = conn_get_buffer_age(b, now_ts_for_buf_cmp);
|
2014-08-19 17:25:48 +02:00
|
|
|
|
|
|
|
if (age_a < age_b)
|
|
|
|
return 1;
|
|
|
|
else if (age_a == age_b)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-20 18:08:14 +01:00
|
|
|
#define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
|
2013-06-16 15:55:44 +02:00
|
|
|
|
|
|
|
/** We're out of memory for cells, having allocated <b>current_allocation</b>
|
|
|
|
* bytes' worth. Kill the 'worst' circuits until we're under
|
2013-11-20 18:08:14 +01:00
|
|
|
* FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
|
2013-06-16 15:55:44 +02:00
|
|
|
void
|
|
|
|
circuits_handle_oom(size_t current_allocation)
|
|
|
|
{
|
2014-08-15 23:37:39 +02:00
|
|
|
smartlist_t *circlist;
|
2014-08-19 17:25:48 +02:00
|
|
|
smartlist_t *connection_array = get_connection_array();
|
|
|
|
int conn_idx;
|
2013-11-20 17:56:35 +01:00
|
|
|
size_t mem_to_recover;
|
|
|
|
size_t mem_recovered=0;
|
2013-06-16 15:55:44 +02:00
|
|
|
int n_circuits_killed=0;
|
2014-08-19 17:25:48 +02:00
|
|
|
int n_dirconns_killed=0;
|
2017-11-21 18:28:16 +01:00
|
|
|
uint32_t now_ts;
|
2018-01-12 17:44:30 +01:00
|
|
|
log_notice(LD_GENERAL, "We're low on memory (cell queues total alloc:"
|
|
|
|
" %"TOR_PRIuSZ" buffer total alloc: %" TOR_PRIuSZ ","
|
|
|
|
" tor compress total alloc: %" TOR_PRIuSZ
|
2018-02-27 22:55:10 +01:00
|
|
|
" (zlib: %" TOR_PRIuSZ ", zstd: %" TOR_PRIuSZ ","
|
|
|
|
" lzma: %" TOR_PRIuSZ "),"
|
2018-01-12 17:44:30 +01:00
|
|
|
" rendezvous cache total alloc: %" TOR_PRIuSZ "). Killing"
|
|
|
|
" circuits withover-long queues. (This behavior is controlled by"
|
2017-12-20 18:31:30 +01:00
|
|
|
" MaxMemInQueues.)",
|
|
|
|
cell_queues_get_total_allocation(),
|
|
|
|
buf_get_total_allocation(),
|
|
|
|
tor_compress_get_total_allocation(),
|
2018-02-27 22:55:10 +01:00
|
|
|
tor_zlib_get_total_allocation(),
|
|
|
|
tor_zstd_get_total_allocation(),
|
|
|
|
tor_lzma_get_total_allocation(),
|
2017-12-20 18:31:30 +01:00
|
|
|
rend_cache_get_total_allocation());
|
2013-06-16 15:55:44 +02:00
|
|
|
|
|
|
|
{
|
2013-11-20 18:08:14 +01:00
|
|
|
size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
|
|
|
|
FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
|
2013-06-16 15:55:44 +02:00
|
|
|
if (current_allocation <= mem_target)
|
|
|
|
return;
|
|
|
|
mem_to_recover = current_allocation - mem_target;
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:28:16 +01:00
|
|
|
now_ts = monotime_coarse_get_stamp();
|
2013-11-16 00:38:52 +01:00
|
|
|
|
2014-08-15 23:37:39 +02:00
|
|
|
circlist = circuit_get_global_list();
|
|
|
|
SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
|
2017-11-21 18:28:16 +01:00
|
|
|
circ->age_tmp = circuit_max_queued_item_age(circ, now_ts);
|
2014-08-15 23:37:39 +02:00
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
2013-11-07 18:15:30 +01:00
|
|
|
|
2013-06-16 15:55:44 +02:00
|
|
|
/* This is O(n log n); there are faster algorithms we could use instead.
|
|
|
|
* Let's hope this doesn't happen enough to be in the critical path. */
|
2013-11-16 00:38:52 +01:00
|
|
|
smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
|
2013-06-16 15:55:44 +02:00
|
|
|
|
2014-08-15 23:37:39 +02:00
|
|
|
/* Fix up the indices before we run into trouble */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
|
|
|
|
circ->global_circuitlist_idx = circ_sl_idx;
|
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
|
|
|
|
2014-08-24 19:09:08 +02:00
|
|
|
/* Now sort the connection array ... */
|
2017-11-21 18:28:16 +01:00
|
|
|
now_ts_for_buf_cmp = now_ts;
|
2014-08-19 17:25:48 +02:00
|
|
|
smartlist_sort(connection_array, conns_compare_by_buffer_age_);
|
2017-11-21 18:28:16 +01:00
|
|
|
now_ts_for_buf_cmp = 0;
|
2014-08-19 17:25:48 +02:00
|
|
|
|
|
|
|
/* Fix up the connection array to its new order. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
|
|
|
|
conn->conn_array_index = conn_sl_idx;
|
|
|
|
} SMARTLIST_FOREACH_END(conn);
|
|
|
|
|
|
|
|
/* Okay, now the worst circuits and connections are at the front of their
|
|
|
|
* respective lists. Let's mark them, and reclaim their storage
|
|
|
|
* aggressively. */
|
|
|
|
conn_idx = 0;
|
2013-06-16 15:55:44 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
|
2014-08-19 17:25:48 +02:00
|
|
|
size_t n;
|
2013-11-20 17:56:35 +01:00
|
|
|
size_t freed;
|
2014-08-19 17:25:48 +02:00
|
|
|
|
|
|
|
/* Free storage in any non-linked directory connections that have buffered
|
|
|
|
* data older than this circuit. */
|
|
|
|
while (conn_idx < smartlist_len(connection_array)) {
|
|
|
|
connection_t *conn = smartlist_get(connection_array, conn_idx);
|
2017-11-21 18:28:16 +01:00
|
|
|
uint32_t conn_age = conn_get_buffer_age(conn, now_ts);
|
2014-08-19 17:25:48 +02:00
|
|
|
if (conn_age < circ->age_tmp) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (conn->type == CONN_TYPE_DIR && conn->linked_conn == NULL) {
|
|
|
|
if (!conn->marked_for_close)
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
mem_recovered += single_conn_free_bytes(conn);
|
|
|
|
|
|
|
|
++n_dirconns_killed;
|
|
|
|
|
|
|
|
if (mem_recovered >= mem_to_recover)
|
|
|
|
goto done_recovering_mem;
|
|
|
|
}
|
|
|
|
++conn_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, kill the circuit. */
|
|
|
|
n = n_cells_in_circ_queues(circ);
|
2013-06-16 15:55:44 +02:00
|
|
|
if (! circ->marked_for_close) {
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
|
|
|
|
}
|
|
|
|
marked_circuit_free_cells(circ);
|
2013-11-20 17:56:35 +01:00
|
|
|
freed = marked_circuit_free_stream_bytes(circ);
|
2013-06-16 15:55:44 +02:00
|
|
|
|
|
|
|
++n_circuits_killed;
|
2013-11-20 17:56:35 +01:00
|
|
|
|
|
|
|
mem_recovered += n * packed_cell_mem_cost();
|
|
|
|
mem_recovered += freed;
|
|
|
|
|
|
|
|
if (mem_recovered >= mem_to_recover)
|
2014-08-19 17:25:48 +02:00
|
|
|
goto done_recovering_mem;
|
2013-06-16 15:55:44 +02:00
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
|
|
|
|
2014-08-19 17:25:48 +02:00
|
|
|
done_recovering_mem:
|
|
|
|
|
2014-02-12 19:09:02 +01:00
|
|
|
log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits; "
|
2014-08-19 17:25:48 +02:00
|
|
|
"%d circuits remain alive. Also killed %d non-linked directory "
|
|
|
|
"connections.",
|
2013-11-20 17:56:35 +01:00
|
|
|
U64_PRINTF_ARG(mem_recovered),
|
2014-02-12 19:09:02 +01:00
|
|
|
n_circuits_killed,
|
2014-08-19 17:25:48 +02:00
|
|
|
smartlist_len(circlist) - n_circuits_killed,
|
|
|
|
n_dirconns_killed);
|
2013-06-16 15:55:44 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Verify that cpath layer <b>cp</b> has all of its invariants
|
|
|
|
* correct. Trigger an assert if anything is invalid.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
assert_cpath_layer_ok(const crypt_path_t *cp)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
// tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
|
|
|
|
// tor_assert(cp->port);
|
2005-03-23 07:21:48 +01:00
|
|
|
tor_assert(cp);
|
|
|
|
tor_assert(cp->magic == CRYPT_PATH_MAGIC);
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (cp->state)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
case CPATH_STATE_OPEN:
|
2018-03-17 15:59:15 +01:00
|
|
|
relay_crypto_assert_ok(&cp->crypto);
|
2004-05-20 01:51:39 +02:00
|
|
|
/* fall through */
|
|
|
|
case CPATH_STATE_CLOSED:
|
2012-12-05 03:27:07 +01:00
|
|
|
/*XXXX Assert that there's no handshake_state either. */
|
|
|
|
tor_assert(!cp->rend_dh_handshake_state);
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
case CPATH_STATE_AWAITING_KEYS:
|
2005-05-03 00:35:18 +02:00
|
|
|
/* tor_assert(cp->dh_handshake_state); */
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
default:
|
2005-10-25 09:05:03 +02:00
|
|
|
log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
tor_assert(cp->package_window >= 0);
|
|
|
|
tor_assert(cp->deliver_window >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Verify that cpath <b>cp</b> has all of its invariants
|
|
|
|
* correct. Trigger an assert if anything is invalid.
|
|
|
|
*/
|
|
|
|
static void
|
2004-05-18 17:35:21 +02:00
|
|
|
assert_cpath_ok(const crypt_path_t *cp)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2004-05-18 17:35:21 +02:00
|
|
|
const crypt_path_t *start = cp;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-05-18 17:35:21 +02:00
|
|
|
do {
|
2004-05-13 09:24:49 +02:00
|
|
|
assert_cpath_layer_ok(cp);
|
|
|
|
/* layers must be in sequence of: "open* awaiting? closed*" */
|
2004-05-18 17:35:21 +02:00
|
|
|
if (cp != start) {
|
|
|
|
if (cp->state == CPATH_STATE_AWAITING_KEYS) {
|
|
|
|
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
|
|
|
|
} else if (cp->state == CPATH_STATE_OPEN) {
|
|
|
|
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cp = cp->next;
|
2004-05-18 17:35:21 +02:00
|
|
|
tor_assert(cp);
|
|
|
|
} while (cp != start);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Verify that circuit <b>c</b> has all of its invariants
|
|
|
|
* correct. Trigger an assert if anything is invalid.
|
|
|
|
*/
|
2017-12-04 17:45:41 +01:00
|
|
|
MOCK_IMPL(void,
|
|
|
|
assert_circuit_ok,(const circuit_t *c))
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
const or_circuit_t *or_circ = NULL;
|
|
|
|
const origin_circuit_t *origin_circ = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(c);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
|
|
|
|
c->purpose <= CIRCUIT_PURPOSE_MAX_);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2014-04-29 03:02:59 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(c))
|
|
|
|
origin_circ = CONST_TO_ORIGIN_CIRCUIT(c);
|
|
|
|
else
|
|
|
|
or_circ = CONST_TO_OR_CIRCUIT(c);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (c->n_chan) {
|
2008-07-30 15:04:32 +02:00
|
|
|
tor_assert(!c->n_hop);
|
|
|
|
|
2008-11-01 21:27:41 +01:00
|
|
|
if (c->n_circ_id) {
|
|
|
|
/* We use the _impl variant here to make sure we don't fail on marked
|
|
|
|
* circuits, which would not be returned by the regular function. */
|
2012-08-08 15:02:47 +02:00
|
|
|
circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
|
2013-03-14 17:13:45 +01:00
|
|
|
c->n_chan, NULL);
|
2008-11-01 21:27:41 +01:00
|
|
|
tor_assert(c == c2);
|
|
|
|
}
|
2004-07-03 03:45:13 +02:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (or_circ && or_circ->p_chan) {
|
2008-11-01 21:27:41 +01:00
|
|
|
if (or_circ->p_circ_id) {
|
|
|
|
/* ibid */
|
2013-06-13 07:22:21 +02:00
|
|
|
circuit_t *c2 =
|
|
|
|
circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
|
|
|
|
or_circ->p_chan, NULL);
|
2008-11-01 21:27:41 +01:00
|
|
|
tor_assert(c == c2);
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if (or_circ)
|
|
|
|
for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(conn->base_.type == CONN_TYPE_EXIT);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(c->deliver_window >= 0);
|
|
|
|
tor_assert(c->package_window >= 0);
|
2016-11-21 22:59:00 +01:00
|
|
|
if (c->state == CIRCUIT_STATE_OPEN ||
|
|
|
|
c->state == CIRCUIT_STATE_GUARD_WAIT) {
|
2012-12-06 05:07:49 +01:00
|
|
|
tor_assert(!c->n_chan_create_cell);
|
2006-07-23 09:37:35 +02:00
|
|
|
if (or_circ) {
|
2018-03-17 15:59:15 +01:00
|
|
|
relay_crypto_assert_ok(&or_circ->crypto);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
|
|
|
|
tor_assert(circuits_pending_chans &&
|
2012-04-11 18:50:50 +02:00
|
|
|
smartlist_contains(circuits_pending_chans, c));
|
2005-12-03 03:12:37 +01:00
|
|
|
} else {
|
2012-08-08 15:02:47 +02:00
|
|
|
tor_assert(!circuits_pending_chans ||
|
2012-04-11 18:50:50 +02:00
|
|
|
!smartlist_contains(circuits_pending_chans, c));
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
2006-07-23 10:13:45 +02:00
|
|
|
if (origin_circ && origin_circ->cpath) {
|
2006-07-23 09:37:35 +02:00
|
|
|
assert_cpath_ok(origin_circ->cpath);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(or_circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
if (!c->marked_for_close) {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(or_circ->rend_splice);
|
|
|
|
tor_assert(or_circ->rend_splice->rend_splice == or_circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(or_circ->rend_splice != or_circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(!or_circ || !or_circ->rend_splice);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|