2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2016-02-27 18:48:19 +01:00
|
|
|
* Copyright (c) 2007-2016, The Tor Project, Inc. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuitbuild.c
|
2016-02-27 18:08:24 +01:00
|
|
|
*
|
|
|
|
* \brief Implements the details of building circuits (by chosing paths,
|
|
|
|
* constructing/sending create/extend cells, and so on).
|
2004-05-13 09:24:49 +02:00
|
|
|
**/
|
|
|
|
|
2014-05-07 08:57:50 +02:00
|
|
|
#define CIRCUITBUILD_PRIVATE
|
|
|
|
|
2009-09-01 08:09:54 +02:00
|
|
|
#include "or.h"
|
2012-08-08 15:02:47 +02:00
|
|
|
#include "channel.h"
|
2013-10-31 19:17:32 +01:00
|
|
|
#include "circpathbias.h"
|
2014-01-21 11:49:51 +01:00
|
|
|
#define CIRCUITBUILD_PRIVATE
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "circuitstats.h"
|
2010-07-22 10:03:40 +02:00
|
|
|
#include "circuituse.h"
|
2012-08-08 15:02:47 +02:00
|
|
|
#include "command.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2012-09-12 23:34:50 +02:00
|
|
|
#include "confparse.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.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-07-14 06:04:02 +02:00
|
|
|
#include "crypto.h"
|
2010-07-22 11:54:50 +02:00
|
|
|
#include "directory.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "entrynodes.h"
|
2010-07-23 19:58:06 +02:00
|
|
|
#include "main.h"
|
2012-01-26 02:18:51 +01:00
|
|
|
#include "microdesc.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:09:52 +01:00
|
|
|
#include "onion_tap.h"
|
|
|
|
#include "onion_fast.h"
|
2010-07-23 20:51:25 +02:00
|
|
|
#include "policies.h"
|
2010-07-23 21:53:11 +02:00
|
|
|
#include "relay.h"
|
2016-07-14 06:04:02 +02:00
|
|
|
#include "rendcommon.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2010-07-23 23:23:43 +02:00
|
|
|
#include "routerparse.h"
|
2012-09-13 18:46:39 +02:00
|
|
|
#include "routerset.h"
|
2016-07-14 06:04:02 +02:00
|
|
|
#include "transports.h"
|
2009-08-28 08:28:20 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
static channel_t * channel_connect_for_circuit(const tor_addr_t *addr,
|
|
|
|
uint16_t port,
|
|
|
|
const char *id_digest);
|
2005-05-03 00:35:18 +02:00
|
|
|
static int circuit_deliver_create_cell(circuit_t *circ,
|
2012-12-06 06:21:24 +01:00
|
|
|
const create_cell_t *create_cell,
|
|
|
|
int relayed);
|
2006-07-23 09:37:35 +02:00
|
|
|
static int onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit);
|
2005-03-22 01:42:38 +01:00
|
|
|
static crypt_path_t *onion_next_hop_in_cpath(crypt_path_t *cpath);
|
2006-10-07 08:28:50 +02:00
|
|
|
static int onion_extend_cpath(origin_circuit_t *circ);
|
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
|
|
|
static int count_acceptable_nodes(smartlist_t *routers);
|
2005-06-29 23:46:55 +02:00
|
|
|
static int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
|
2012-08-17 01:29:19 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
/** This function tries to get a channel to the specified endpoint,
|
|
|
|
* and then calls command_setup_channel() to give it the right
|
|
|
|
* callbacks.
|
|
|
|
*/
|
|
|
|
static channel_t *
|
|
|
|
channel_connect_for_circuit(const tor_addr_t *addr, uint16_t port,
|
|
|
|
const char *id_digest)
|
|
|
|
{
|
|
|
|
channel_t *chan;
|
|
|
|
|
|
|
|
chan = channel_connect(addr, port, id_digest);
|
|
|
|
if (chan) command_setup_channel(chan);
|
|
|
|
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:50:04 +02:00
|
|
|
/** Search for a value for circ_id that we can use on <b>chan</b> for an
|
|
|
|
* outbound circuit, until we get a circ_id that is not in use by any other
|
|
|
|
* circuit on that conn.
|
2011-01-17 07:07:31 +01:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* Return it, or 0 if can't get a unique circ_id.
|
2011-01-17 07:07:31 +01:00
|
|
|
*/
|
2014-05-07 08:57:50 +02:00
|
|
|
STATIC circid_t
|
2012-10-15 20:48:34 +02:00
|
|
|
get_unique_circ_id_by_chan(channel_t *chan)
|
2010-05-08 20:54:29 +02:00
|
|
|
{
|
2014-04-23 18:39:01 +02:00
|
|
|
/* This number is chosen somewhat arbitrarily; see comment below for more
|
|
|
|
* info. When the space is 80% full, it gives a one-in-a-million failure
|
|
|
|
* chance; when the space is 90% full, it gives a one-in-850 chance; and when
|
|
|
|
* the space is 95% full, it gives a one-in-26 failure chance. That seems
|
|
|
|
* okay, though you could make a case IMO for anything between N=32 and
|
|
|
|
* N=256. */
|
2014-04-18 18:50:04 +02:00
|
|
|
#define MAX_CIRCID_ATTEMPTS 64
|
2014-04-18 19:04:37 +02:00
|
|
|
int in_use;
|
2014-06-10 19:35:45 +02:00
|
|
|
unsigned n_with_circ = 0, n_pending_destroy = 0, n_weird_pending_destroy = 0;
|
2012-10-15 20:48:34 +02:00
|
|
|
circid_t test_circ_id;
|
|
|
|
circid_t attempts=0;
|
2014-04-18 18:50:04 +02:00
|
|
|
circid_t high_bit, max_range, mask;
|
2014-06-10 19:35:45 +02:00
|
|
|
int64_t pending_destroy_time_total = 0;
|
|
|
|
int64_t pending_destroy_time_max = 0;
|
2012-10-15 20:48:34 +02:00
|
|
|
|
|
|
|
tor_assert(chan);
|
|
|
|
|
|
|
|
if (chan->circ_id_type == CIRC_ID_TYPE_NEITHER) {
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Trying to pick a circuit ID for a connection from "
|
|
|
|
"a client with no identity.");
|
2010-05-08 20:54:29 +02:00
|
|
|
return 0;
|
2012-10-15 20:48:34 +02:00
|
|
|
}
|
2014-04-18 18:50:04 +02:00
|
|
|
max_range = (chan->wide_circ_ids) ? (1u<<31) : (1u<<15);
|
|
|
|
mask = max_range - 1;
|
2012-11-07 01:56:47 +01:00
|
|
|
high_bit = (chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ? max_range : 0;
|
2012-10-15 20:48:34 +02:00
|
|
|
do {
|
2014-04-18 18:50:04 +02:00
|
|
|
if (++attempts > MAX_CIRCID_ATTEMPTS) {
|
|
|
|
/* Make sure we don't loop forever because all circuit IDs are used.
|
|
|
|
*
|
|
|
|
* Once, we would try until we had tried every possible circuit ID. But
|
|
|
|
* that's quite expensive. Instead, we try MAX_CIRCID_ATTEMPTS random
|
|
|
|
* circuit IDs, and then give up.
|
|
|
|
*
|
|
|
|
* This potentially causes us to give up early if our circuit ID space
|
|
|
|
* is nearly full. If we have N circuit IDs in use, then we will reject
|
|
|
|
* a new circuit with probability (N / max_range) ^ MAX_CIRCID_ATTEMPTS.
|
|
|
|
* This means that in practice, a few percent of our circuit ID capacity
|
|
|
|
* will go unused.
|
|
|
|
*
|
|
|
|
* The alternative here, though, is to do a linear search over the
|
|
|
|
* whole circuit ID space every time we extend a circuit, which is
|
|
|
|
* not so great either.
|
2012-10-15 20:48:34 +02:00
|
|
|
*/
|
2014-06-10 19:35:45 +02:00
|
|
|
int64_t queued_destroys;
|
|
|
|
char *m = rate_limit_log(&chan->last_warned_circ_ids_exhausted,
|
|
|
|
approx_time());
|
|
|
|
if (m == NULL)
|
|
|
|
return 0; /* This message has been rate-limited away. */
|
|
|
|
if (n_pending_destroy)
|
|
|
|
pending_destroy_time_total /= n_pending_destroy;
|
|
|
|
log_warn(LD_CIRC,"No unused circIDs found on channel %s wide "
|
2014-04-18 18:28:30 +02:00
|
|
|
"circID support, with %u inbound and %u outbound circuits. "
|
2014-04-18 19:04:37 +02:00
|
|
|
"Found %u circuit IDs in use by circuits, and %u with "
|
2014-06-10 19:35:45 +02:00
|
|
|
"pending destroy cells. (%u of those were marked bogusly.) "
|
|
|
|
"The ones with pending destroy cells "
|
|
|
|
"have been marked unusable for an average of %ld seconds "
|
|
|
|
"and a maximum of %ld seconds. This channel is %ld seconds "
|
|
|
|
"old. Failing a circuit.%s",
|
2014-04-18 18:28:30 +02:00
|
|
|
chan->wide_circ_ids ? "with" : "without",
|
2014-04-18 19:04:37 +02:00
|
|
|
chan->num_p_circuits, chan->num_n_circuits,
|
2014-06-10 19:35:45 +02:00
|
|
|
n_with_circ, n_pending_destroy, n_weird_pending_destroy,
|
|
|
|
(long)pending_destroy_time_total,
|
|
|
|
(long)pending_destroy_time_max,
|
|
|
|
(long)(approx_time() - chan->timestamp_created),
|
|
|
|
m);
|
|
|
|
tor_free(m);
|
|
|
|
|
|
|
|
if (!chan->cmux) {
|
|
|
|
/* This warning should be impossible. */
|
|
|
|
log_warn(LD_BUG, " This channel somehow has no cmux on it!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* analysis so far on 12184 suggests that we're running out of circuit
|
|
|
|
IDs because it looks like we have too many pending destroy
|
|
|
|
cells. Let's see how many we really have pending.
|
|
|
|
*/
|
|
|
|
queued_destroys = circuitmux_count_queued_destroy_cells(chan,
|
|
|
|
chan->cmux);
|
|
|
|
|
|
|
|
log_warn(LD_CIRC, " Circuitmux on this channel has %u circuits, "
|
|
|
|
"of which %u are active. It says it has "I64_FORMAT
|
|
|
|
" destroy cells queued.",
|
|
|
|
circuitmux_num_circuits(chan->cmux),
|
|
|
|
circuitmux_num_active_circuits(chan->cmux),
|
|
|
|
I64_PRINTF_ARG(queued_destroys));
|
|
|
|
|
|
|
|
/* Change this into "if (1)" in order to get more information about
|
|
|
|
* possible failure modes here. You'll need to know how to use gdb with
|
|
|
|
* Tor: this will make Tor exit with an assertion failure if the cmux is
|
|
|
|
* corrupt. */
|
|
|
|
if (0)
|
|
|
|
circuitmux_assert_okay(chan->cmux);
|
|
|
|
|
2014-07-16 10:34:39 +02:00
|
|
|
channel_dump_statistics(chan, LOG_WARN);
|
|
|
|
|
2010-05-13 00:31:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-18 18:50:04 +02:00
|
|
|
|
2014-04-23 18:39:01 +02:00
|
|
|
do {
|
|
|
|
crypto_rand((char*) &test_circ_id, sizeof(test_circ_id));
|
|
|
|
test_circ_id &= mask;
|
|
|
|
} while (test_circ_id == 0);
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
test_circ_id |= high_bit;
|
2014-04-18 19:04:37 +02:00
|
|
|
|
|
|
|
in_use = circuit_id_in_use_on_channel(test_circ_id, chan);
|
|
|
|
if (in_use == 1)
|
|
|
|
++n_with_circ;
|
2014-06-10 19:35:45 +02:00
|
|
|
else if (in_use == 2) {
|
|
|
|
time_t since_when;
|
2014-04-18 19:04:37 +02:00
|
|
|
++n_pending_destroy;
|
2014-06-10 19:35:45 +02:00
|
|
|
since_when =
|
|
|
|
circuit_id_when_marked_unusable_on_channel(test_circ_id, chan);
|
|
|
|
if (since_when) {
|
|
|
|
time_t waiting = approx_time() - since_when;
|
|
|
|
pending_destroy_time_total += waiting;
|
|
|
|
if (waiting > pending_destroy_time_max)
|
|
|
|
pending_destroy_time_max = waiting;
|
|
|
|
} else {
|
|
|
|
++n_weird_pending_destroy;
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 19:04:37 +02:00
|
|
|
} while (in_use);
|
2012-10-15 20:48:34 +02:00
|
|
|
return test_circ_id;
|
2010-05-08 20:54:29 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** If <b>verbose</b> is false, allocate and return a comma-separated list of
|
|
|
|
* the currently built elements of <b>circ</b>. If <b>verbose</b> is true, also
|
|
|
|
* list information about link status in a more verbose format using spaces.
|
|
|
|
* If <b>verbose_names</b> is false, give nicknames for Named routers and hex
|
|
|
|
* digests for others; if <b>verbose_names</b> is true, use $DIGEST=Name style
|
|
|
|
* names.
|
2011-01-17 07:07:31 +01:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
static char *
|
|
|
|
circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
|
2010-01-22 01:10:02 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
smartlist_t *elements;
|
|
|
|
const char *states[] = {"closed", "waiting for keys", "open"};
|
|
|
|
char *s;
|
2012-06-12 05:36:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
elements = smartlist_new();
|
2010-01-22 01:10:02 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (verbose) {
|
|
|
|
const char *nickname = build_state_get_exit_nickname(circ->build_state);
|
|
|
|
smartlist_add_asprintf(elements, "%s%s circ (length %d%s%s):",
|
|
|
|
circ->build_state->is_internal ? "internal" : "exit",
|
|
|
|
circ->build_state->need_uptime ? " (high-uptime)" : "",
|
|
|
|
circ->build_state->desired_path_len,
|
|
|
|
circ->base_.state == CIRCUIT_STATE_OPEN ? "" : ", last hop ",
|
|
|
|
circ->base_.state == CIRCUIT_STATE_OPEN ? "" :
|
|
|
|
(nickname?nickname:"*unnamed*"));
|
2012-06-12 05:36:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
hop = circ->cpath;
|
|
|
|
do {
|
|
|
|
char *elt;
|
|
|
|
const char *id;
|
|
|
|
const node_t *node;
|
|
|
|
if (!hop)
|
|
|
|
break;
|
|
|
|
if (!verbose && hop->state != CPATH_STATE_OPEN)
|
|
|
|
break;
|
|
|
|
if (!hop->extend_info)
|
|
|
|
break;
|
|
|
|
id = hop->extend_info->identity_digest;
|
|
|
|
if (verbose_names) {
|
|
|
|
elt = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
|
|
|
|
if ((node = node_get_by_id(id))) {
|
|
|
|
node_get_verbose_nickname(node, elt);
|
|
|
|
} else if (is_legal_nickname(hop->extend_info->nickname)) {
|
|
|
|
elt[0] = '$';
|
|
|
|
base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
|
|
|
|
elt[HEX_DIGEST_LEN+1]= '~';
|
|
|
|
strlcpy(elt+HEX_DIGEST_LEN+2,
|
|
|
|
hop->extend_info->nickname, MAX_NICKNAME_LEN+1);
|
|
|
|
} else {
|
|
|
|
elt[0] = '$';
|
|
|
|
base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
} else { /* ! verbose_names */
|
|
|
|
node = node_get_by_id(id);
|
|
|
|
if (node && node_is_named(node)) {
|
|
|
|
elt = tor_strdup(node_get_nickname(node));
|
|
|
|
} else {
|
|
|
|
elt = tor_malloc(HEX_DIGEST_LEN+2);
|
|
|
|
elt[0] = '$';
|
|
|
|
base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(elt);
|
|
|
|
if (verbose) {
|
|
|
|
tor_assert(hop->state <= 2);
|
|
|
|
smartlist_add_asprintf(elements,"%s(%s)",elt,states[hop->state]);
|
|
|
|
tor_free(elt);
|
|
|
|
} else {
|
|
|
|
smartlist_add(elements, elt);
|
|
|
|
}
|
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != circ->cpath);
|
2012-06-12 05:36:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
|
|
|
return s;
|
2010-01-22 01:10:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** If <b>verbose</b> is false, allocate and return a comma-separated
|
|
|
|
* list of the currently built elements of <b>circ</b>. If
|
|
|
|
* <b>verbose</b> is true, also list information about link status in
|
|
|
|
* a more verbose format using spaces.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
circuit_list_path(origin_circuit_t *circ, int verbose)
|
2010-09-30 00:01:22 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
return circuit_list_path_impl(circ, verbose, 0);
|
2010-09-30 00:01:22 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Allocate and return a comma-separated list of the currently built elements
|
|
|
|
* of <b>circ</b>, giving each as a verbose nickname.
|
2011-01-17 07:07:31 +01:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
char *
|
|
|
|
circuit_list_path_for_controller(origin_circuit_t *circ)
|
2010-01-22 01:10:02 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
return circuit_list_path_impl(circ, 0, 1);
|
2010-01-22 01:10:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Log, at severity <b>severity</b>, the nicknames of each router in
|
|
|
|
* <b>circ</b>'s cpath. Also log the length of the cpath, and the intended
|
|
|
|
* exit point.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
|
2010-12-30 19:54:13 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
char *s = circuit_list_path(circ,1);
|
|
|
|
tor_log(severity,domain,"%s",s);
|
|
|
|
tor_free(s);
|
2010-12-30 19:54:13 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Tell the rep(utation)hist(ory) module about the status of the links
|
|
|
|
* in <b>circ</b>. Hops that have become OPEN are marked as successfully
|
|
|
|
* extended; the _first_ hop that isn't open (if any) is marked as
|
|
|
|
* unable to extend.
|
2011-01-17 07:07:31 +01:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
/* XXXX Someday we should learn from OR circuits too. */
|
|
|
|
void
|
|
|
|
circuit_rep_hist_note_result(origin_circuit_t *circ)
|
2010-06-08 10:01:47 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
const char *prev_digest = NULL;
|
|
|
|
hop = circ->cpath;
|
|
|
|
if (!hop) /* circuit hasn't started building yet. */
|
|
|
|
return;
|
|
|
|
if (server_mode(get_options())) {
|
|
|
|
const routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
if (!me)
|
|
|
|
return;
|
|
|
|
prev_digest = me->cache_info.identity_digest;
|
2011-01-15 19:31:23 +01:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
do {
|
|
|
|
const node_t *node = node_get_by_id(hop->extend_info->identity_digest);
|
|
|
|
if (node) { /* Why do we check this? We know the identity. -NM XXXX */
|
|
|
|
if (prev_digest) {
|
|
|
|
if (hop->state == CPATH_STATE_OPEN)
|
|
|
|
rep_hist_note_extend_succeeded(prev_digest, node->identity);
|
|
|
|
else {
|
|
|
|
rep_hist_note_extend_failed(prev_digest, node->identity);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_digest = node->identity;
|
|
|
|
} else {
|
|
|
|
prev_digest = NULL;
|
|
|
|
}
|
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
2010-06-08 10:01:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/** Return 1 iff every node in circ's cpath definitely supports ntor. */
|
2014-02-06 23:08:50 +01:00
|
|
|
static int
|
|
|
|
circuit_cpath_supports_ntor(const origin_circuit_t *circ)
|
|
|
|
{
|
2014-04-19 02:29:51 +02:00
|
|
|
crypt_path_t *head, *cpath;
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2014-04-19 02:29:51 +02:00
|
|
|
cpath = head = circ->cpath;
|
2014-02-06 23:08:50 +01:00
|
|
|
do {
|
2016-07-07 08:04:26 +02:00
|
|
|
/* if the extend_info is missing, we can't tell if it supports ntor */
|
|
|
|
if (!cpath->extend_info) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/* if the key is blank, it definitely doesn't support ntor */
|
|
|
|
if (!extend_info_supports_ntor(cpath->extend_info)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-06 23:08:50 +01:00
|
|
|
cpath = cpath->next;
|
|
|
|
} while (cpath != head);
|
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
return 1;
|
2014-02-06 23:08:50 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Pick all the entries in our cpath. Stop and return 0 when we're
|
|
|
|
* happy, or return -1 if an error occurs. */
|
|
|
|
static int
|
|
|
|
onion_populate_cpath(origin_circuit_t *circ)
|
2010-01-22 01:10:02 +01:00
|
|
|
{
|
2016-07-07 08:04:26 +02:00
|
|
|
int r = 0;
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/* onion_extend_cpath assumes these are non-NULL */
|
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->build_state);
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
while (r == 0) {
|
|
|
|
r = onion_extend_cpath(circ);
|
2014-02-06 23:08:50 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_info(LD_CIRC,"Generating cpath hop failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-07 08:04:26 +02:00
|
|
|
}
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/* The path is complete */
|
|
|
|
tor_assert(r == 1);
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/* Does every node in this path support ntor? */
|
|
|
|
int path_supports_ntor = circuit_cpath_supports_ntor(circ);
|
|
|
|
|
|
|
|
/* We would like every path to support ntor, but we have to allow for some
|
|
|
|
* edge cases. */
|
|
|
|
tor_assert(circuit_get_cpath_len(circ));
|
|
|
|
if (circuit_can_use_tap(circ)) {
|
|
|
|
/* Circuits from clients to intro points, and hidden services to
|
|
|
|
* rend points do not support ntor, because the hidden service protocol
|
|
|
|
* does not include ntor onion keys. This is also true for Tor2web clients
|
|
|
|
* and Single Onion Services. */
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
if (circuit_get_cpath_len(circ) == 1) {
|
|
|
|
/* Allow for bootstrapping: when we're fetching directly from a fallback,
|
|
|
|
* authority, or bridge, we have no way of knowing its ntor onion key
|
|
|
|
* before we connect to it. So instead, we try connecting, and end up using
|
|
|
|
* CREATE_FAST. */
|
|
|
|
tor_assert(circ->cpath);
|
|
|
|
tor_assert(circ->cpath->extend_info);
|
|
|
|
const node_t *node = node_get_by_id(
|
|
|
|
circ->cpath->extend_info->identity_digest);
|
|
|
|
/* If we don't know the node and its descriptor, we must be bootstrapping.
|
|
|
|
*/
|
|
|
|
if (!node || !node_has_descriptor(node)) {
|
|
|
|
return 0;
|
2014-02-06 23:08:50 +01:00
|
|
|
}
|
2012-06-12 05:36:38 +02:00
|
|
|
}
|
2014-02-06 23:08:50 +01:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
if (BUG(!path_supports_ntor)) {
|
|
|
|
/* If we're building a multi-hop path, and it's not one of the HS or
|
|
|
|
* bootstrapping exceptions, and it doesn't support ntor, something has
|
|
|
|
* gone wrong. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-01-22 01:10:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Create and return a new origin circuit. Initialize its purpose and
|
|
|
|
* build-state based on our arguments. The <b>flags</b> argument is a
|
|
|
|
* bitfield of CIRCLAUNCH_* flags. */
|
|
|
|
origin_circuit_t *
|
|
|
|
origin_circuit_init(uint8_t purpose, int flags)
|
2010-01-22 01:10:02 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
/* sets circ->p_circ_id and circ->p_chan */
|
|
|
|
origin_circuit_t *circ = origin_circuit_new();
|
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_CHAN_WAIT);
|
|
|
|
circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
|
|
|
|
circ->build_state->onehop_tunnel =
|
|
|
|
((flags & CIRCLAUNCH_ONEHOP_TUNNEL) ? 1 : 0);
|
|
|
|
circ->build_state->need_uptime =
|
|
|
|
((flags & CIRCLAUNCH_NEED_UPTIME) ? 1 : 0);
|
|
|
|
circ->build_state->need_capacity =
|
|
|
|
((flags & CIRCLAUNCH_NEED_CAPACITY) ? 1 : 0);
|
|
|
|
circ->build_state->is_internal =
|
|
|
|
((flags & CIRCLAUNCH_IS_INTERNAL) ? 1 : 0);
|
|
|
|
circ->base_.purpose = purpose;
|
|
|
|
return circ;
|
2010-01-22 01:10:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Build a new circuit for <b>purpose</b>. If <b>exit</b>
|
|
|
|
* is defined, then use that as your exit router, else choose a suitable
|
|
|
|
* exit node.
|
2011-01-17 07:07:31 +01:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* Also launch a connection to the first OR in the chosen path, if
|
|
|
|
* it's not open already.
|
2011-01-17 07:07:31 +01:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
origin_circuit_t *
|
2016-07-28 16:22:10 +02:00
|
|
|
circuit_establish_circuit(uint8_t purpose, extend_info_t *exit_ei, int flags)
|
2010-01-22 01:10:02 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
origin_circuit_t *circ;
|
|
|
|
int err_reason = 0;
|
2012-06-12 05:36:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
circ = origin_circuit_init(purpose, flags);
|
2010-01-22 01:10:02 +01:00
|
|
|
|
2016-07-28 16:22:10 +02:00
|
|
|
if (onion_pick_cpath_exit(circ, exit_ei) < 0 ||
|
2012-10-15 20:48:34 +02:00
|
|
|
onion_populate_cpath(circ) < 0) {
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
|
|
|
|
return NULL;
|
2012-06-12 05:36:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_LAUNCHED, 0);
|
|
|
|
|
|
|
|
if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return circ;
|
2010-01-22 01:10:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Start establishing the first hop of our circuit. Figure out what
|
|
|
|
* OR we should connect to, and if necessary start the connection to
|
|
|
|
* it. If we're already connected, then send the 'create' cell.
|
|
|
|
* Return 0 for ok, -reason if circ should be marked-for-close. */
|
|
|
|
int
|
|
|
|
circuit_handle_first_hop(origin_circuit_t *circ)
|
2010-01-22 01:10:02 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypt_path_t *firsthop;
|
|
|
|
channel_t *n_chan;
|
|
|
|
int err_reason = 0;
|
|
|
|
const char *msg = NULL;
|
|
|
|
int should_launch = 0;
|
2016-03-23 03:37:35 +01:00
|
|
|
const or_options_t *options = get_options();
|
2012-06-12 20:52:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
firsthop = onion_next_hop_in_cpath(circ->cpath);
|
|
|
|
tor_assert(firsthop);
|
|
|
|
tor_assert(firsthop->extend_info);
|
2012-06-12 20:52:38 +02:00
|
|
|
|
2016-03-23 03:37:35 +01:00
|
|
|
/* Some bridges are on private addresses. Others pass a dummy private
|
|
|
|
* address to the pluggable transport, which ignores it.
|
|
|
|
* Deny the connection if:
|
|
|
|
* - the address is internal, and
|
|
|
|
* - we're not connecting to a configured bridge, and
|
|
|
|
* - we're not configured to allow extends to private addresses. */
|
2015-11-24 17:11:15 +01:00
|
|
|
if (tor_addr_is_internal(&firsthop->extend_info->addr, 0) &&
|
2016-03-23 03:37:35 +01:00
|
|
|
!extend_info_is_a_configured_bridge(firsthop->extend_info) &&
|
|
|
|
!options->ExtendAllowPrivateAddresses) {
|
2015-11-24 17:11:15 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Client asked me to connect directly to a private address");
|
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* now see if we're already connected to the first OR in 'route' */
|
|
|
|
log_debug(LD_CIRC,"Looking for firsthop '%s'",
|
|
|
|
fmt_addrport(&firsthop->extend_info->addr,
|
|
|
|
firsthop->extend_info->port));
|
2012-06-12 20:52:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
n_chan = channel_get_for_extend(firsthop->extend_info->identity_digest,
|
|
|
|
&firsthop->extend_info->addr,
|
|
|
|
&msg,
|
|
|
|
&should_launch);
|
2012-06-12 20:52:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!n_chan) {
|
|
|
|
/* not currently connected in a useful way. */
|
|
|
|
log_info(LD_CIRC, "Next router is %s: %s",
|
|
|
|
safe_str_client(extend_info_describe(firsthop->extend_info)),
|
|
|
|
msg?msg:"???");
|
|
|
|
circ->base_.n_hop = extend_info_dup(firsthop->extend_info);
|
2012-06-12 20:52:38 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (should_launch) {
|
|
|
|
if (circ->build_state->onehop_tunnel)
|
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_CONN_DIR, 0);
|
|
|
|
n_chan = channel_connect_for_circuit(
|
|
|
|
&firsthop->extend_info->addr,
|
|
|
|
firsthop->extend_info->port,
|
|
|
|
firsthop->extend_info->identity_digest);
|
|
|
|
if (!n_chan) { /* connect failed, forget the whole thing */
|
|
|
|
log_info(LD_CIRC,"connect to firsthop failed. Closing.");
|
|
|
|
return -END_CIRC_REASON_CONNECTFAILED;
|
2012-06-12 20:52:38 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-12 21:11:32 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
|
|
|
|
/* return success. The onion/circuit/etc will be taken care of
|
|
|
|
* automatically (may already have been) whenever n_chan reaches
|
|
|
|
* OR_CONN_STATE_OPEN.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
} else { /* it's already open. use it. */
|
|
|
|
tor_assert(!circ->base_.n_hop);
|
|
|
|
circ->base_.n_chan = n_chan;
|
|
|
|
log_debug(LD_CIRC,"Conn open. Delivering first onion skin.");
|
|
|
|
if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
|
|
|
|
log_info(LD_CIRC,"circuit_send_next_onion_skin failed.");
|
2014-08-12 18:15:09 +02:00
|
|
|
circ->base_.n_chan = NULL;
|
2012-10-15 20:48:34 +02:00
|
|
|
return err_reason;
|
2009-09-18 11:01:39 +02:00
|
|
|
}
|
2012-06-12 21:11:32 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2009-09-02 05:27:43 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Find any circuits that are waiting on <b>or_conn</b> to become
|
|
|
|
* open and get them to send their create cells forward.
|
2009-09-08 10:31:29 +02:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* Status is 1 if connect succeeded, or 0 if connect failed.
|
2015-01-07 16:27:22 +01:00
|
|
|
*
|
2015-01-21 19:00:14 +01:00
|
|
|
* Close_origin_circuits is 1 if we should close all the origin circuits
|
|
|
|
* through this channel, or 0 otherwise. (This happens when we want to retry
|
|
|
|
* an older guard.)
|
2012-06-13 04:27:37 +02:00
|
|
|
*/
|
2012-06-13 04:38:28 +02:00
|
|
|
void
|
2015-01-07 16:27:22 +01:00
|
|
|
circuit_n_chan_done(channel_t *chan, int status, int close_origin_circuits)
|
2012-06-13 04:38:28 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
smartlist_t *pending_circs;
|
|
|
|
int err_reason = 0;
|
2012-06-13 04:27:37 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(chan);
|
2012-06-13 04:27:37 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC,"chan to %s/%s, status=%d",
|
|
|
|
chan->nickname ? chan->nickname : "NULL",
|
|
|
|
channel_get_canonical_remote_descr(chan), status);
|
2012-06-13 04:27:37 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
pending_circs = smartlist_new();
|
|
|
|
circuit_get_all_pending_on_channel(pending_circs, chan);
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(pending_circs, circuit_t *, circ)
|
|
|
|
{
|
|
|
|
/* These checks are redundant wrt get_all_pending_on_or_conn, but I'm
|
|
|
|
* leaving them in in case it's possible for the status of a circuit to
|
|
|
|
* change as we're going down the list. */
|
|
|
|
if (circ->marked_for_close || circ->n_chan || !circ->n_hop ||
|
|
|
|
circ->state != CIRCUIT_STATE_CHAN_WAIT)
|
|
|
|
continue;
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
|
|
|
|
/* Look at addr/port. This is an unkeyed connection. */
|
|
|
|
if (!channel_matches_extend_info(chan, circ->n_hop))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* We expected a key. See if it's the right one. */
|
|
|
|
if (tor_memneq(chan->identity_digest,
|
|
|
|
circ->n_hop->identity_digest, DIGEST_LEN))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!status) { /* chan failed; close circ */
|
|
|
|
log_info(LD_CIRC,"Channel failed; closing circ.");
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_CHANNEL_CLOSED);
|
2015-01-07 16:27:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (close_origin_circuits && CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
log_info(LD_CIRC,"Channel deprecated for origin circs; closing circ.");
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_CHANNEL_CLOSED);
|
2012-10-15 20:48:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
log_debug(LD_CIRC, "Found circ, sending create cell.");
|
|
|
|
/* circuit_deliver_create_cell will set n_circ_id and add us to
|
|
|
|
* chan_circuid_circuit_map, so we don't need to call
|
|
|
|
* set_circid_chan here. */
|
|
|
|
circ->n_chan = chan;
|
|
|
|
extend_info_free(circ->n_hop);
|
|
|
|
circ->n_hop = NULL;
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
if ((err_reason =
|
|
|
|
circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ))) < 0) {
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"send_next_onion_skin failed; circuit marked for closing.");
|
|
|
|
circuit_mark_for_close(circ, -err_reason);
|
|
|
|
continue;
|
|
|
|
/* XXX could this be bad, eg if next_onion_skin failed because conn
|
|
|
|
* died? */
|
|
|
|
}
|
|
|
|
} else {
|
2012-12-06 05:07:49 +01:00
|
|
|
/* pull the create cell out of circ->n_chan_create_cell, and send it */
|
|
|
|
tor_assert(circ->n_chan_create_cell);
|
2012-12-06 06:21:24 +01:00
|
|
|
if (circuit_deliver_create_cell(circ, circ->n_chan_create_cell, 1)<0) {
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-06 05:07:49 +01:00
|
|
|
tor_free(circ->n_chan_create_cell);
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_set_state(circ, CIRCUIT_STATE_OPEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH_END(circ);
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
smartlist_free(pending_circs);
|
2009-09-02 05:27:43 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Find a new circid that isn't currently in use on the circ->n_chan
|
|
|
|
* for the outgoing
|
2012-12-26 04:38:20 +01:00
|
|
|
* circuit <b>circ</b>, and deliver the cell <b>create_cell</b> to this
|
|
|
|
* circuit. If <b>relayed</b> is true, this is a create cell somebody
|
|
|
|
* gave us via an EXTEND cell, so we shouldn't worry if we don't understand
|
|
|
|
* it. Return -1 if we failed to find a suitable circid, else return 0.
|
2009-08-26 02:13:12 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
static int
|
2012-12-06 06:21:24 +01:00
|
|
|
circuit_deliver_create_cell(circuit_t *circ, const create_cell_t *create_cell,
|
|
|
|
int relayed)
|
2009-08-26 02:13:12 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
cell_t cell;
|
|
|
|
circid_t id;
|
2012-12-06 06:21:24 +01:00
|
|
|
int r;
|
2012-10-15 20:48:34 +02:00
|
|
|
|
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->n_chan);
|
2012-12-06 05:07:49 +01:00
|
|
|
tor_assert(create_cell);
|
|
|
|
tor_assert(create_cell->cell_type == CELL_CREATE ||
|
|
|
|
create_cell->cell_type == CELL_CREATE_FAST ||
|
|
|
|
create_cell->cell_type == CELL_CREATE2);
|
2012-10-15 20:48:34 +02:00
|
|
|
|
|
|
|
id = get_unique_circ_id_by_chan(circ->n_chan);
|
|
|
|
if (!id) {
|
2014-04-18 19:22:42 +02:00
|
|
|
static ratelim_t circid_warning_limit = RATELIM_INIT(9600);
|
|
|
|
log_fn_ratelim(&circid_warning_limit, LOG_WARN, LD_CIRC,
|
|
|
|
"failed to get unique circID.");
|
2014-08-12 18:10:57 +02:00
|
|
|
goto error;
|
2009-08-26 02:13:12 +02:00
|
|
|
}
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
2012-12-06 06:21:24 +01:00
|
|
|
r = relayed ? create_cell_format_relayed(&cell, create_cell)
|
|
|
|
: create_cell_format(&cell, create_cell);
|
|
|
|
if (r < 0) {
|
2012-12-06 05:07:49 +01:00
|
|
|
log_warn(LD_CIRC,"Couldn't format create cell");
|
2014-08-12 18:10:57 +02:00
|
|
|
goto error;
|
2012-12-06 05:07:49 +01:00
|
|
|
}
|
2014-08-12 18:10:57 +02:00
|
|
|
log_debug(LD_CIRC,"Chosen circID %u.", (unsigned)id);
|
|
|
|
circuit_set_n_circid_chan(circ, id, circ->n_chan);
|
2012-10-15 20:48:34 +02:00
|
|
|
cell.circ_id = circ->n_circ_id;
|
2009-09-16 13:55:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
append_cell_to_circuit_queue(circ, circ->n_chan, &cell,
|
|
|
|
CELL_DIRECTION_OUT, 0);
|
2009-08-26 02:13:12 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2012-10-26 02:43:10 +02:00
|
|
|
/* Update began timestamp for circuits starting their first hop */
|
|
|
|
if (TO_ORIGIN_CIRCUIT(circ)->cpath->state == CPATH_STATE_CLOSED) {
|
2014-11-23 20:17:15 +01:00
|
|
|
if (!CHANNEL_IS_OPEN(circ->n_chan)) {
|
2012-10-26 02:43:10 +02:00
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"Got first hop for a circuit without an opened channel. "
|
|
|
|
"State: %s.", channel_state_to_string(circ->n_chan->state));
|
|
|
|
tor_fragile_assert();
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_gettimeofday(&circ->timestamp_began);
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* mark it so it gets better rate limiting treatment. */
|
|
|
|
channel_timestamp_client(circ->n_chan);
|
2009-09-01 08:09:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-26 02:13:12 +02:00
|
|
|
return 0;
|
2014-08-12 18:10:57 +02:00
|
|
|
error:
|
|
|
|
circ->n_chan = NULL;
|
|
|
|
return -1;
|
2009-08-26 02:13:12 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** We've decided to start our reachability testing. If all
|
|
|
|
* is set, log this to the user. Return 1 if we did, or 0 if
|
|
|
|
* we chose not to log anything. */
|
|
|
|
int
|
|
|
|
inform_testing_reachability(void)
|
2009-08-26 02:13:12 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
char dirbuf[128];
|
2013-02-10 03:10:07 +01:00
|
|
|
char *address;
|
2012-10-15 20:48:34 +02:00
|
|
|
const routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
if (!me)
|
|
|
|
return 0;
|
2013-02-10 03:10:07 +01:00
|
|
|
address = tor_dup_ip(me->addr);
|
2012-10-15 20:48:34 +02:00
|
|
|
control_event_server_status(LOG_NOTICE,
|
|
|
|
"CHECKING_REACHABILITY ORADDRESS=%s:%d",
|
2013-02-10 03:10:07 +01:00
|
|
|
address, me->or_port);
|
2012-10-15 20:48:34 +02:00
|
|
|
if (me->dir_port) {
|
|
|
|
tor_snprintf(dirbuf, sizeof(dirbuf), " and DirPort %s:%d",
|
2013-02-10 03:10:07 +01:00
|
|
|
address, me->dir_port);
|
2012-10-15 20:48:34 +02:00
|
|
|
control_event_server_status(LOG_NOTICE,
|
|
|
|
"CHECKING_REACHABILITY DIRADDRESS=%s:%d",
|
2013-02-10 03:10:07 +01:00
|
|
|
address, me->dir_port);
|
2009-09-01 17:07:26 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
log_notice(LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
|
|
|
|
"(this may take up to %d minutes -- look for log "
|
|
|
|
"messages indicating success)",
|
2013-02-10 03:10:07 +01:00
|
|
|
address, me->or_port,
|
2012-10-15 20:48:34 +02:00
|
|
|
me->dir_port ? dirbuf : "",
|
|
|
|
me->dir_port ? "are" : "is",
|
|
|
|
TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT/60);
|
2009-09-01 17:07:26 +02:00
|
|
|
|
2013-02-10 03:10:07 +01:00
|
|
|
tor_free(address);
|
2012-10-15 20:48:34 +02:00
|
|
|
return 1;
|
2009-09-01 17:07:26 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return true iff we should send a create_fast cell to start building a given
|
|
|
|
* circuit */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2012-10-15 20:48:34 +02:00
|
|
|
should_use_create_fast_for_circuit(origin_circuit_t *circ)
|
2009-09-01 17:07:26 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
const or_options_t *options = get_options();
|
|
|
|
tor_assert(circ->cpath);
|
|
|
|
tor_assert(circ->cpath->extend_info);
|
2009-09-01 17:07:26 +02:00
|
|
|
|
2016-07-08 06:46:00 +02:00
|
|
|
if (!circuit_has_usable_onion_key(circ)) {
|
|
|
|
/* We don't have ntor, and we don't have or can't use TAP,
|
|
|
|
* so our hand is forced: only a create_fast will work. */
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
if (public_server_mode(options)) {
|
2016-07-08 06:46:00 +02:00
|
|
|
/* We're a server, and we have a usable onion key. We can choose.
|
2012-10-15 20:48:34 +02:00
|
|
|
* Prefer to blend our circuit into the other circuits we are
|
|
|
|
* creating on behalf of others. */
|
|
|
|
return 0;
|
2009-09-01 17:07:26 +02:00
|
|
|
}
|
2013-10-31 21:44:14 +01:00
|
|
|
if (options->FastFirstHopPK == -1) {
|
|
|
|
/* option is "auto", so look at the consensus. */
|
|
|
|
return networkstatus_get_param(NULL, "usecreatefast", 1, 0, 1);
|
|
|
|
}
|
2009-09-01 17:07:26 +02:00
|
|
|
|
2013-10-31 21:44:14 +01:00
|
|
|
return options->FastFirstHopPK;
|
2009-09-01 17:07:26 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return true if <b>circ</b> is the type of circuit we want to count
|
|
|
|
* timeouts from. In particular, we want it to have not completed yet
|
|
|
|
* (already completing indicates we cannibalized it), and we want it to
|
|
|
|
* have exactly three hops.
|
2009-08-26 02:13:12 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
|
|
|
circuit_timeout_want_to_count_circ(origin_circuit_t *circ)
|
2009-08-26 02:13:12 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
return !circ->has_opened
|
|
|
|
&& circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN;
|
2009-08-26 02:13:12 +02:00
|
|
|
}
|
|
|
|
|
2012-12-06 07:53:29 +01:00
|
|
|
/** Decide whether to use a TAP or ntor handshake for connecting to <b>ei</b>
|
|
|
|
* directly, and set *<b>cell_type_out</b> and *<b>handshake_type_out</b>
|
2016-07-08 06:46:00 +02:00
|
|
|
* accordingly.
|
2016-10-27 06:52:46 +02:00
|
|
|
* Note that TAP handshakes in CREATE cells are only used for direct
|
|
|
|
* connections:
|
2016-07-08 06:46:00 +02:00
|
|
|
* - from Tor2web to intro points not in the client's consensus, and
|
|
|
|
* - from Single Onions to rend points not in the service's consensus.
|
|
|
|
* This is checked in onion_populate_cpath. */
|
2012-12-06 07:53:29 +01:00
|
|
|
static void
|
|
|
|
circuit_pick_create_handshake(uint8_t *cell_type_out,
|
|
|
|
uint16_t *handshake_type_out,
|
|
|
|
const extend_info_t *ei)
|
|
|
|
{
|
2016-10-27 06:52:46 +02:00
|
|
|
/* torspec says: In general, clients SHOULD use CREATE whenever they are
|
|
|
|
* using the TAP handshake, and CREATE2 otherwise. */
|
2016-07-08 06:46:00 +02:00
|
|
|
if (extend_info_supports_ntor(ei)) {
|
2012-12-06 07:53:29 +01:00
|
|
|
*cell_type_out = CELL_CREATE2;
|
|
|
|
*handshake_type_out = ONION_HANDSHAKE_TYPE_NTOR;
|
2016-10-31 07:01:33 +01:00
|
|
|
} else {
|
|
|
|
/* XXXX030 Remove support for deciding to use TAP and EXTEND. */
|
|
|
|
*cell_type_out = CELL_CREATE;
|
|
|
|
*handshake_type_out = ONION_HANDSHAKE_TYPE_TAP;
|
2012-12-06 07:53:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 06:52:46 +02:00
|
|
|
/** Decide whether to use a TAP or ntor handshake for extending to <b>ei</b>
|
|
|
|
* and set *<b>handshake_type_out</b> accordingly. Decide whether we should
|
|
|
|
* use an EXTEND2 or an EXTEND cell to do so, and set *<b>cell_type_out</b>
|
|
|
|
* and *<b>create_cell_type_out</b> accordingly.
|
|
|
|
* Note that TAP handshakes in EXTEND cells are only used:
|
2016-07-08 06:46:00 +02:00
|
|
|
* - from clients to intro points, and
|
|
|
|
* - from hidden services to rend points.
|
2016-10-27 06:52:46 +02:00
|
|
|
* This is checked in onion_populate_cpath.
|
|
|
|
*/
|
2012-12-06 07:53:29 +01:00
|
|
|
static void
|
|
|
|
circuit_pick_extend_handshake(uint8_t *cell_type_out,
|
|
|
|
uint8_t *create_cell_type_out,
|
|
|
|
uint16_t *handshake_type_out,
|
|
|
|
const extend_info_t *ei)
|
|
|
|
{
|
|
|
|
uint8_t t;
|
|
|
|
circuit_pick_create_handshake(&t, handshake_type_out, ei);
|
2016-05-30 22:18:16 +02:00
|
|
|
|
2016-10-27 06:52:46 +02:00
|
|
|
/* torspec says: Clients SHOULD use the EXTEND format whenever sending a TAP
|
|
|
|
* handshake... In other cases, clients SHOULD use EXTEND2. */
|
|
|
|
if (*handshake_type_out != ONION_HANDSHAKE_TYPE_TAP) {
|
2016-08-31 16:32:10 +02:00
|
|
|
*cell_type_out = RELAY_COMMAND_EXTEND2;
|
|
|
|
*create_cell_type_out = CELL_CREATE2;
|
|
|
|
} else {
|
2016-10-27 06:52:46 +02:00
|
|
|
/* XXXX030 Remove support for deciding to use TAP and EXTEND. */
|
2016-08-31 16:32:10 +02:00
|
|
|
*cell_type_out = RELAY_COMMAND_EXTEND;
|
|
|
|
*create_cell_type_out = CELL_CREATE;
|
|
|
|
}
|
2012-12-06 07:53:29 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** This is the backbone function for building circuits.
|
2009-09-08 10:31:29 +02:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* If circ's first hop is closed, then we need to build a create
|
|
|
|
* cell and send it forward.
|
|
|
|
*
|
|
|
|
* Otherwise, we need to build a relay extend cell and send it
|
|
|
|
* forward.
|
2010-06-08 10:01:47 +02:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* Return -reason if we want to tear down circ, else return 0.
|
2010-06-07 11:36:43 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
|
|
|
circuit_send_next_onion_skin(origin_circuit_t *circ)
|
2010-06-07 11:36:43 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
const node_t *node;
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(circ);
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->cpath->state == CPATH_STATE_CLOSED) {
|
2012-12-06 05:07:49 +01:00
|
|
|
/* This is the first hop. */
|
|
|
|
create_cell_t cc;
|
2012-10-15 20:48:34 +02:00
|
|
|
int fast;
|
2012-12-06 05:07:49 +01:00
|
|
|
int len;
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC,"First skin; sending create cell.");
|
2012-12-06 05:07:49 +01:00
|
|
|
memset(&cc, 0, sizeof(cc));
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->build_state->onehop_tunnel)
|
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_ONEHOP_CREATE, 0);
|
|
|
|
else
|
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_CIRCUIT_CREATE, 0);
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
node = node_get_by_id(circ->base_.n_chan->identity_digest);
|
|
|
|
fast = should_use_create_fast_for_circuit(circ);
|
|
|
|
if (!fast) {
|
|
|
|
/* We are an OR and we know the right onion key: we should
|
2012-12-06 07:53:29 +01:00
|
|
|
* send a create cell.
|
2012-10-15 20:48:34 +02:00
|
|
|
*/
|
2012-12-06 07:53:29 +01:00
|
|
|
circuit_pick_create_handshake(&cc.cell_type, &cc.handshake_type,
|
|
|
|
circ->cpath->extend_info);
|
2012-10-15 20:48:34 +02:00
|
|
|
} else {
|
|
|
|
/* We are not an OR, and we're building the first hop of a circuit to a
|
|
|
|
* new OR: we can be speedy and use CREATE_FAST to save an RSA operation
|
|
|
|
* and a DH operation. */
|
2012-12-06 05:07:49 +01:00
|
|
|
cc.cell_type = CELL_CREATE_FAST;
|
|
|
|
cc.handshake_type = ONION_HANDSHAKE_TYPE_FAST;
|
2010-06-07 11:36:43 +02:00
|
|
|
}
|
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
len = onion_skin_create(cc.handshake_type,
|
|
|
|
circ->cpath->extend_info,
|
|
|
|
&circ->cpath->handshake_state,
|
|
|
|
cc.onionskin);
|
|
|
|
if (len < 0) {
|
2012-12-05 03:27:07 +01:00
|
|
|
log_warn(LD_CIRC,"onion_skin_create (first hop) failed.");
|
|
|
|
return - END_CIRC_REASON_INTERNAL;
|
|
|
|
}
|
2012-12-06 05:07:49 +01:00
|
|
|
cc.handshake_len = len;
|
2012-12-05 03:27:07 +01:00
|
|
|
|
2012-12-06 06:21:24 +01:00
|
|
|
if (circuit_deliver_create_cell(TO_CIRCUIT(circ), &cc, 0) < 0)
|
2012-10-15 20:48:34 +02:00
|
|
|
return - END_CIRC_REASON_RESOURCELIMIT;
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
|
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
|
|
|
|
log_info(LD_CIRC,"First hop: finished sending %s cell to '%s'",
|
|
|
|
fast ? "CREATE_FAST" : "CREATE",
|
|
|
|
node ? node_describe(node) : "<unnamed>");
|
|
|
|
} else {
|
2012-12-06 05:07:49 +01:00
|
|
|
extend_cell_t ec;
|
|
|
|
int len;
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
|
|
|
|
tor_assert(circ->base_.state == CIRCUIT_STATE_BUILDING);
|
|
|
|
log_debug(LD_CIRC,"starting to send subsequent skin.");
|
|
|
|
hop = onion_next_hop_in_cpath(circ->cpath);
|
2012-12-06 05:07:49 +01:00
|
|
|
memset(&ec, 0, sizeof(ec));
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!hop) {
|
|
|
|
/* done building the circuit. whew. */
|
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
|
|
|
|
if (circuit_timeout_want_to_count_circ(circ)) {
|
|
|
|
struct timeval end;
|
|
|
|
long timediff;
|
|
|
|
tor_gettimeofday(&end);
|
2012-10-26 02:43:10 +02:00
|
|
|
timediff = tv_mdiff(&circ->base_.timestamp_began, &end);
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/*
|
|
|
|
* If the circuit build time is much greater than we would have cut
|
|
|
|
* it off at, we probably had a suspend event along this codepath,
|
|
|
|
* and we should discard the value.
|
|
|
|
*/
|
2013-08-21 17:59:19 +02:00
|
|
|
if (timediff < 0 ||
|
|
|
|
timediff > 2*get_circuit_build_close_time_ms()+1000) {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_notice(LD_CIRC, "Strange value for circuit build time: %ldmsec. "
|
|
|
|
"Assuming clock jump. Purpose %d (%s)", timediff,
|
|
|
|
circ->base_.purpose,
|
|
|
|
circuit_purpose_to_string(circ->base_.purpose));
|
|
|
|
} else if (!circuit_build_times_disabled()) {
|
|
|
|
/* Only count circuit times if the network is live */
|
2013-08-21 17:44:28 +02:00
|
|
|
if (circuit_build_times_network_check_live(
|
|
|
|
get_circuit_build_times())) {
|
2013-08-21 18:10:05 +02:00
|
|
|
circuit_build_times_add_time(get_circuit_build_times_mutable(),
|
2013-08-21 17:44:28 +02:00
|
|
|
(build_time_t)timediff);
|
2013-08-21 18:10:05 +02:00
|
|
|
circuit_build_times_set_timeout(get_circuit_build_times_mutable());
|
2012-10-15 20:48:34 +02:00
|
|
|
}
|
2010-05-08 20:54:29 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
|
2013-08-21 17:44:28 +02:00
|
|
|
circuit_build_times_network_circ_success(
|
2013-08-21 18:10:05 +02:00
|
|
|
get_circuit_build_times_mutable());
|
2012-10-15 20:48:34 +02:00
|
|
|
}
|
|
|
|
}
|
2009-08-27 10:46:06 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
log_info(LD_CIRC,"circuit built!");
|
|
|
|
circuit_reset_failure_count(0);
|
2009-09-02 05:27:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->build_state->onehop_tunnel || circ->has_opened) {
|
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_STATUS, 0);
|
2009-09-29 12:41:23 +02:00
|
|
|
}
|
|
|
|
|
2013-03-26 19:39:13 +01:00
|
|
|
pathbias_count_build_success(circ);
|
|
|
|
circuit_rep_hist_note_result(circ);
|
|
|
|
circuit_has_opened(circ); /* do other actions as necessary */
|
|
|
|
|
2014-11-20 18:03:46 +01:00
|
|
|
if (!have_completed_a_circuit() && !circ->build_state->onehop_tunnel) {
|
2012-10-15 20:48:34 +02:00
|
|
|
const or_options_t *options = get_options();
|
2014-11-20 18:03:46 +01:00
|
|
|
note_that_we_completed_a_circuit();
|
2012-10-15 20:48:34 +02:00
|
|
|
/* FFFF Log a count of known routers here */
|
|
|
|
log_notice(LD_GENERAL,
|
|
|
|
"Tor has successfully opened a circuit. "
|
|
|
|
"Looks like client functionality is working.");
|
2015-02-20 08:03:42 +01:00
|
|
|
if (control_event_bootstrap(BOOTSTRAP_STATUS_DONE, 0) == 0) {
|
|
|
|
log_notice(LD_GENERAL,
|
|
|
|
"Tor has successfully opened a circuit. "
|
|
|
|
"Looks like client functionality is working.");
|
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
control_event_client_status(LOG_NOTICE, "CIRCUIT_ESTABLISHED");
|
|
|
|
clear_broken_connection_map(1);
|
2016-05-16 23:43:47 +02:00
|
|
|
if (server_mode(options) && !check_whether_orport_reachable(options)) {
|
2012-10-15 20:48:34 +02:00
|
|
|
inform_testing_reachability();
|
|
|
|
consider_testing_reachability(1, 1);
|
|
|
|
}
|
2009-08-26 02:13:12 +02:00
|
|
|
}
|
2009-09-04 22:42:58 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* We're done with measurement circuits here. Just close them */
|
2012-12-08 23:16:29 +01:00
|
|
|
if (circ->base_.purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
|
2012-12-08 23:16:29 +01:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tor_addr_family(&hop->extend_info->addr) != AF_INET) {
|
|
|
|
log_warn(LD_BUG, "Trying to extend to a non-IPv4 address.");
|
|
|
|
return - END_CIRC_REASON_INTERNAL;
|
|
|
|
}
|
2009-09-02 05:27:43 +02:00
|
|
|
|
2016-10-27 06:52:46 +02:00
|
|
|
circuit_pick_extend_handshake(&ec.cell_type,
|
|
|
|
&ec.create_cell.cell_type,
|
|
|
|
&ec.create_cell.handshake_type,
|
|
|
|
hop->extend_info);
|
2009-09-02 05:27:43 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
tor_addr_copy(&ec.orport_ipv4.addr, &hop->extend_info->addr);
|
|
|
|
ec.orport_ipv4.port = hop->extend_info->port;
|
|
|
|
tor_addr_make_unspec(&ec.orport_ipv6.addr);
|
|
|
|
memcpy(ec.node_id, hop->extend_info->identity_digest, DIGEST_LEN);
|
2010-07-06 21:08:13 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
len = onion_skin_create(ec.create_cell.handshake_type,
|
|
|
|
hop->extend_info,
|
|
|
|
&hop->handshake_state,
|
|
|
|
ec.create_cell.onionskin);
|
|
|
|
if (len < 0) {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_warn(LD_CIRC,"onion_skin_create failed.");
|
|
|
|
return - END_CIRC_REASON_INTERNAL;
|
|
|
|
}
|
2012-12-06 05:07:49 +01:00
|
|
|
ec.create_cell.handshake_len = len;
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_info(LD_CIRC,"Sending extend relay cell.");
|
2012-12-06 05:07:49 +01:00
|
|
|
{
|
|
|
|
uint8_t command = 0;
|
|
|
|
uint16_t payload_len=0;
|
|
|
|
uint8_t payload[RELAY_PAYLOAD_SIZE];
|
|
|
|
if (extend_cell_format(&command, &payload_len, payload, &ec)<0) {
|
|
|
|
log_warn(LD_CIRC,"Couldn't format extend cell");
|
|
|
|
return -END_CIRC_REASON_INTERNAL;
|
|
|
|
}
|
2010-06-07 11:36:43 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
/* send it to hop->prev, because it will transfer
|
|
|
|
* it to a create cell and then send to hop */
|
|
|
|
if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
|
|
|
|
command,
|
|
|
|
(char*)payload, payload_len,
|
|
|
|
hop->prev) < 0)
|
|
|
|
return 0; /* circuit is closed */
|
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
hop->state = CPATH_STATE_AWAITING_KEYS;
|
2010-06-07 11:36:43 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2009-08-26 02:13:12 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Our clock just jumped by <b>seconds_elapsed</b>. Assume
|
|
|
|
* something has also gone wrong with our network: notify the user,
|
|
|
|
* and abandon all not-yet-used circuits. */
|
2009-08-28 08:28:20 +02:00
|
|
|
void
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_note_clock_jumped(int seconds_elapsed)
|
2009-09-01 03:10:27 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
|
|
|
|
tor_log(severity, LD_GENERAL, "Your system clock just jumped %d seconds %s; "
|
|
|
|
"assuming established circuits no longer work.",
|
|
|
|
seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed,
|
|
|
|
seconds_elapsed >=0 ? "forward" : "backward");
|
|
|
|
control_event_general_status(LOG_WARN, "CLOCK_JUMPED TIME=%d",
|
|
|
|
seconds_elapsed);
|
2014-11-20 18:03:46 +01:00
|
|
|
/* so we log when it works again */
|
|
|
|
note_that_we_maybe_cant_complete_circuits();
|
2012-10-15 20:48:34 +02:00
|
|
|
control_event_client_status(severity, "CIRCUIT_NOT_ESTABLISHED REASON=%s",
|
|
|
|
"CLOCK_JUMPED");
|
|
|
|
circuit_mark_all_unused_circs();
|
2013-02-20 00:37:03 +01:00
|
|
|
circuit_mark_all_dirty_circs_as_unusable();
|
2015-02-09 07:05:31 +01:00
|
|
|
if (seconds_elapsed < 0) {
|
|
|
|
/* Restart all the timers in case we jumped a long way into the past. */
|
|
|
|
reset_all_main_loop_timers();
|
|
|
|
}
|
2009-09-01 03:10:27 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Take the 'extend' <b>cell</b>, pull out addr/port plus the onion
|
|
|
|
* skin and identity digest for the next hop. If we're already connected,
|
|
|
|
* pass the onion skin to the next hop using a create cell; otherwise
|
|
|
|
* launch a new OR connection, and <b>circ</b> will notice when the
|
|
|
|
* connection succeeds or fails.
|
|
|
|
*
|
|
|
|
* Return -1 if we want to warn and tear down the circuit, else return 0.
|
2009-09-08 10:31:29 +02:00
|
|
|
*/
|
2009-09-01 03:10:27 +02:00
|
|
|
int
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_extend(cell_t *cell, circuit_t *circ)
|
2009-09-01 03:10:27 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
channel_t *n_chan;
|
|
|
|
relay_header_t rh;
|
2012-12-06 05:07:49 +01:00
|
|
|
extend_cell_t ec;
|
2012-10-15 20:48:34 +02:00
|
|
|
const char *msg = NULL;
|
|
|
|
int should_launch = 0;
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->n_chan) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"n_chan already set. Bug/attack. Closing.");
|
|
|
|
return -1;
|
2010-09-29 17:55:11 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->n_hop) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"conn to next hop already launched. Bug/attack. Closing.");
|
|
|
|
return -1;
|
2012-06-12 20:52:38 +02:00
|
|
|
}
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!server_mode(get_options())) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Got an extend cell, but running as a client. Closing.");
|
|
|
|
return -1;
|
2010-06-15 10:13:49 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
if (extend_cell_parse(&ec, rh.command,
|
|
|
|
cell->payload+RELAY_HEADER_SIZE,
|
|
|
|
rh.length) < 0) {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2012-12-06 05:07:49 +01:00
|
|
|
"Can't parse extend cell. Closing circuit.");
|
2012-10-15 20:48:34 +02:00
|
|
|
return -1;
|
2009-09-02 00:40:54 +02:00
|
|
|
}
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
if (!ec.orport_ipv4.port || tor_addr_is_null(&ec.orport_ipv4.addr)) {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Client asked me to extend to zero destination port or addr.");
|
|
|
|
return -1;
|
2009-09-01 03:10:27 +02:00
|
|
|
}
|
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
if (tor_addr_is_internal(&ec.orport_ipv4.addr, 0) &&
|
2012-10-15 20:48:34 +02:00
|
|
|
!get_options()->ExtendAllowPrivateAddresses) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Client asked me to extend to a private address");
|
|
|
|
return -1;
|
2009-09-01 03:10:27 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* Check if they asked us for 0000..0000. We support using
|
|
|
|
* an empty fingerprint for the first hop (e.g. for a bridge relay),
|
|
|
|
* but we don't want to let people send us extend cells for empty
|
|
|
|
* fingerprints -- a) because it opens the user up to a mitm attack,
|
|
|
|
* and b) because it lets an attacker force the relay to hold open a
|
|
|
|
* new TLS connection for each extend request. */
|
2012-12-06 05:07:49 +01:00
|
|
|
if (tor_digest_is_zero((const char*)ec.node_id)) {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Client asked me to extend without specifying an id_digest.");
|
|
|
|
return -1;
|
2009-09-14 13:03:57 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* Next, check if we're being asked to connect to the hop that the
|
|
|
|
* extend cell came from. There isn't any reason for that, and it can
|
|
|
|
* assist circular-path attacks. */
|
2012-12-06 05:07:49 +01:00
|
|
|
if (tor_memeq(ec.node_id,
|
2012-10-15 20:48:34 +02:00
|
|
|
TO_OR_CIRCUIT(circ)->p_chan->identity_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Client asked me to extend back to the previous hop.");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
n_chan = channel_get_for_extend((const char*)ec.node_id,
|
|
|
|
&ec.orport_ipv4.addr,
|
2012-10-15 20:48:34 +02:00
|
|
|
&msg,
|
|
|
|
&should_launch);
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!n_chan) {
|
|
|
|
log_debug(LD_CIRC|LD_OR,"Next router (%s): %s",
|
2012-12-06 05:07:49 +01:00
|
|
|
fmt_addrport(&ec.orport_ipv4.addr,ec.orport_ipv4.port),
|
|
|
|
msg?msg:"????");
|
2010-05-07 23:43:56 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
circ->n_hop = extend_info_new(NULL /*nickname*/,
|
2012-12-06 05:07:49 +01:00
|
|
|
(const char*)ec.node_id,
|
|
|
|
NULL /*onion_key*/,
|
2012-12-06 07:53:29 +01:00
|
|
|
NULL /*curve25519_key*/,
|
2012-12-06 05:07:49 +01:00
|
|
|
&ec.orport_ipv4.addr,
|
|
|
|
ec.orport_ipv4.port);
|
2010-06-15 07:47:45 +02:00
|
|
|
|
2012-12-06 05:07:49 +01:00
|
|
|
circ->n_chan_create_cell = tor_memdup(&ec.create_cell,
|
|
|
|
sizeof(ec.create_cell));
|
2010-06-15 07:47:45 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_set_state(circ, CIRCUIT_STATE_CHAN_WAIT);
|
2010-05-07 23:43:56 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (should_launch) {
|
|
|
|
/* we should try to open a connection */
|
2012-12-06 05:07:49 +01:00
|
|
|
n_chan = channel_connect_for_circuit(&ec.orport_ipv4.addr,
|
|
|
|
ec.orport_ipv4.port,
|
|
|
|
(const char*)ec.node_id);
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!n_chan) {
|
|
|
|
log_info(LD_CIRC,"Launching n_chan failed. Closing circuit.");
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_CONNECTFAILED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
|
2010-06-16 03:02:19 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
/* return success. The onion/circuit/etc will be taken care of
|
|
|
|
* automatically (may already have been) whenever n_chan reaches
|
|
|
|
* OR_CONN_STATE_OPEN.
|
|
|
|
*/
|
|
|
|
return 0;
|
2010-06-16 03:02:19 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(!circ->n_hop); /* Connection is already established. */
|
|
|
|
circ->n_chan = n_chan;
|
|
|
|
log_debug(LD_CIRC,
|
|
|
|
"n_chan is %s",
|
|
|
|
channel_get_canonical_remote_descr(n_chan));
|
2010-06-16 03:02:19 +02:00
|
|
|
|
2012-12-06 06:21:24 +01:00
|
|
|
if (circuit_deliver_create_cell(circ, &ec.create_cell, 1) < 0)
|
2012-10-15 20:48:34 +02:00
|
|
|
return -1;
|
2012-12-06 07:53:29 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2010-06-16 03:02:19 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in
|
|
|
|
* key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
|
|
|
|
* used as follows:
|
|
|
|
* - 20 to initialize f_digest
|
|
|
|
* - 20 to initialize b_digest
|
|
|
|
* - 16 to key f_crypto
|
|
|
|
* - 16 to key b_crypto
|
2009-09-18 11:01:39 +02:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* (If 'reverse' is true, then f_XX and b_XX are swapped.)
|
2009-08-27 10:46:06 +02:00
|
|
|
*/
|
2009-09-18 11:01:39 +02:00
|
|
|
int
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_init_cpath_crypto(crypt_path_t *cpath, const char *key_data,
|
|
|
|
int reverse)
|
2009-08-27 10:46:06 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypto_digest_t *tmp_digest;
|
|
|
|
crypto_cipher_t *tmp_crypto;
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(cpath);
|
|
|
|
tor_assert(key_data);
|
|
|
|
tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
|
|
|
|
cpath->f_digest || cpath->b_digest));
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
cpath->f_digest = crypto_digest_new();
|
|
|
|
crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
|
|
|
|
cpath->b_digest = crypto_digest_new();
|
|
|
|
crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!(cpath->f_crypto =
|
|
|
|
crypto_cipher_new(key_data+(2*DIGEST_LEN)))) {
|
|
|
|
log_warn(LD_BUG,"Forward cipher initialization failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(cpath->b_crypto =
|
|
|
|
crypto_cipher_new(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN))) {
|
|
|
|
log_warn(LD_BUG,"Backward cipher initialization failed.");
|
|
|
|
return -1;
|
2009-08-27 10:46:06 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (reverse) {
|
|
|
|
tmp_digest = cpath->f_digest;
|
|
|
|
cpath->f_digest = cpath->b_digest;
|
|
|
|
cpath->b_digest = tmp_digest;
|
|
|
|
tmp_crypto = cpath->f_crypto;
|
|
|
|
cpath->f_crypto = cpath->b_crypto;
|
|
|
|
cpath->b_crypto = tmp_crypto;
|
|
|
|
}
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2009-08-27 10:46:06 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:40:17 +01:00
|
|
|
/** A "created" cell <b>reply</b> came back to us on circuit <b>circ</b>.
|
|
|
|
* (The body of <b>reply</b> varies depending on what sort of handshake
|
|
|
|
* this is.)
|
2012-10-15 20:48:34 +02:00
|
|
|
*
|
|
|
|
* Calculate the appropriate keys and digests, make sure KH is
|
|
|
|
* correct, and initialize this hop of the cpath.
|
|
|
|
*
|
|
|
|
* Return - reason if we want to mark circ for close, else return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
2012-12-06 04:34:49 +01:00
|
|
|
circuit_finish_handshake(origin_circuit_t *circ,
|
|
|
|
const created_cell_t *reply)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
char keys[CPATH_KEY_MATERIAL_LEN];
|
|
|
|
crypt_path_t *hop;
|
|
|
|
int rv;
|
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
|
|
|
|
2014-10-16 19:26:42 +02:00
|
|
|
if ((rv = pathbias_count_build_attempt(circ)) < 0) {
|
|
|
|
log_warn(LD_CIRC, "pathbias_count_build_attempt failed: %d", rv);
|
2012-10-15 20:48:34 +02:00
|
|
|
return rv;
|
2014-10-16 19:26:42 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS) {
|
|
|
|
hop = circ->cpath;
|
|
|
|
} else {
|
|
|
|
hop = onion_next_hop_in_cpath(circ->cpath);
|
|
|
|
if (!hop) { /* got an extended when we're all done? */
|
|
|
|
log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
|
|
|
|
return - END_CIRC_REASON_TORPROTOCOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-12-05 03:27:07 +01:00
|
|
|
{
|
2014-10-16 19:26:42 +02:00
|
|
|
const char *msg = NULL;
|
2012-12-06 04:34:49 +01:00
|
|
|
if (onion_skin_client_handshake(hop->handshake_state.tag,
|
2012-12-05 03:27:07 +01:00
|
|
|
&hop->handshake_state,
|
2012-12-06 04:34:49 +01:00
|
|
|
reply->reply, reply->handshake_len,
|
2012-12-05 03:27:07 +01:00
|
|
|
(uint8_t*)keys, sizeof(keys),
|
2014-10-16 19:26:42 +02:00
|
|
|
(uint8_t*)hop->rend_circ_nonce,
|
|
|
|
&msg) < 0) {
|
|
|
|
if (msg)
|
|
|
|
log_warn(LD_CIRC,"onion_skin_client_handshake failed: %s", msg);
|
2012-10-15 20:48:34 +02:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-12-05 03:27:07 +01:00
|
|
|
onion_handshake_state_release(&hop->handshake_state);
|
2012-10-15 20:48:34 +02:00
|
|
|
|
|
|
|
if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
|
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
2008-07-24 11:22:34 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
hop->state = CPATH_STATE_OPEN;
|
2012-12-06 04:34:49 +01:00
|
|
|
log_info(LD_CIRC,"Finished building circuit hop:");
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_CIRC,circ);
|
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_EXTENDED, 0);
|
|
|
|
|
|
|
|
return 0;
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** We received a relay truncated cell on circ.
|
2005-07-22 19:32:25 +02:00
|
|
|
*
|
2013-01-15 21:40:17 +01:00
|
|
|
* Since we don't send truncates currently, getting a truncated
|
2012-10-15 20:48:34 +02:00
|
|
|
* means that a connection broke or an extend failed. For now,
|
2013-01-15 21:40:17 +01:00
|
|
|
* just give up: force circ to close, and return 0.
|
2005-07-22 19:32:25 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
|
|
|
circuit_truncated(origin_circuit_t *circ, crypt_path_t *layer, int reason)
|
2004-08-15 10:15:12 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
// crypt_path_t *victim;
|
|
|
|
// connection_t *stream;
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(layer);
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2013-01-15 21:40:17 +01:00
|
|
|
/* XXX Since we don't send truncates currently, getting a truncated
|
2012-10-15 20:48:34 +02:00
|
|
|
* means that a connection broke or an extend failed. For now,
|
|
|
|
* just give up.
|
|
|
|
*/
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ),
|
2012-10-22 17:35:32 +02:00
|
|
|
END_CIRC_REASON_FLAG_REMOTE|reason);
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2006-01-11 05:14:10 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
#if 0
|
|
|
|
while (layer->next != circ->cpath) {
|
|
|
|
/* we need to clear out layer->next */
|
|
|
|
victim = layer->next;
|
|
|
|
log_debug(LD_CIRC, "Killing a layer of the cpath.");
|
2006-10-03 00:13:42 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
for (stream = circ->p_streams; stream; stream=stream->next_stream) {
|
|
|
|
if (stream->cpath_layer == victim) {
|
|
|
|
log_info(LD_APP, "Marking stream %d for close because of truncate.",
|
|
|
|
stream->stream_id);
|
|
|
|
/* no need to send 'end' relay cells,
|
|
|
|
* because the other side's already dead
|
|
|
|
*/
|
|
|
|
connection_mark_unattached_ap(stream, END_STREAM_REASON_DESTROY);
|
|
|
|
}
|
|
|
|
}
|
2008-07-24 11:22:34 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
layer->next = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_info(LD_CIRC, "finished");
|
|
|
|
return 0;
|
|
|
|
#endif
|
2005-03-22 01:42:38 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Given a response payload and keys, initialize, then send a created
|
|
|
|
* cell back.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
2012-12-06 05:44:27 +01:00
|
|
|
onionskin_answer(or_circuit_t *circ,
|
|
|
|
const created_cell_t *created_cell,
|
|
|
|
const char *keys,
|
2012-12-06 04:34:49 +01:00
|
|
|
const uint8_t *rend_circ_nonce)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
cell_t cell;
|
|
|
|
crypt_path_t *tmp_cpath;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-12-06 05:44:27 +01:00
|
|
|
if (created_cell_format(&cell, created_cell) < 0) {
|
2013-01-15 06:25:07 +01:00
|
|
|
log_warn(LD_BUG,"couldn't format created cell (type=%d, len=%d)",
|
|
|
|
(int)created_cell->cell_type, (int)created_cell->handshake_len);
|
2012-12-06 05:44:27 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cell.circ_id = circ->p_circ_id;
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tmp_cpath = tor_malloc_zero(sizeof(crypt_path_t));
|
|
|
|
tmp_cpath->magic = CRYPT_PATH_MAGIC;
|
2005-03-22 01:42:38 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC,"init digest forward 0x%.8x, backward 0x%.8x.",
|
|
|
|
(unsigned int)get_uint32(keys),
|
|
|
|
(unsigned int)get_uint32(keys+20));
|
|
|
|
if (circuit_init_cpath_crypto(tmp_cpath, keys, 0)<0) {
|
|
|
|
log_warn(LD_BUG,"Circuit initialization failed");
|
|
|
|
tor_free(tmp_cpath);
|
2004-08-15 10:15:12 +02:00
|
|
|
return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
circ->n_digest = tmp_cpath->f_digest;
|
|
|
|
circ->n_crypto = tmp_cpath->f_crypto;
|
|
|
|
circ->p_digest = tmp_cpath->b_digest;
|
|
|
|
circ->p_crypto = tmp_cpath->b_crypto;
|
|
|
|
tmp_cpath->magic = 0;
|
|
|
|
tor_free(tmp_cpath);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-12-06 04:34:49 +01:00
|
|
|
memcpy(circ->rend_circ_nonce, rend_circ_nonce, DIGEST_LEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-12-06 05:44:27 +01:00
|
|
|
circ->is_first_hop = (created_cell->cell_type == CELL_CREATED_FAST);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
append_cell_to_circuit_queue(TO_CIRCUIT(circ),
|
|
|
|
circ->p_chan, &cell, CELL_DIRECTION_IN, 0);
|
|
|
|
log_debug(LD_CIRC,"Finished sending '%s' cell.",
|
|
|
|
circ->is_first_hop ? "created_fast" : "created");
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2015-04-22 15:54:21 +02:00
|
|
|
/* Ignore the local bit when ExtendAllowPrivateAddresses is set:
|
|
|
|
* it violates the assumption that private addresses are local.
|
|
|
|
* Also, many test networks run on local addresses, and
|
|
|
|
* TestingTorNetwork sets ExtendAllowPrivateAddresses. */
|
|
|
|
if ((!channel_is_local(circ->p_chan)
|
|
|
|
|| get_options()->ExtendAllowPrivateAddresses)
|
2014-12-20 11:44:16 +01:00
|
|
|
&& !channel_is_outgoing(circ->p_chan)) {
|
2012-10-15 20:48:34 +02:00
|
|
|
/* record that we could process create cells from a non-local conn
|
|
|
|
* that we didn't initiate; presumably this means that create cells
|
|
|
|
* can reach us too. */
|
|
|
|
router_orport_found_reachable();
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2013-01-15 21:40:17 +01:00
|
|
|
/** Choose a length for a circuit of purpose <b>purpose</b>: three + the
|
|
|
|
* number of endpoints that would give something away about our destination.
|
|
|
|
*
|
|
|
|
* If the routerlist <b>nodes</b> doesn't have enough routers
|
2013-10-08 17:48:33 +02:00
|
|
|
* to handle the desired path length, return -1.
|
2012-10-15 20:48:34 +02:00
|
|
|
*/
|
|
|
|
static int
|
2016-07-28 16:22:10 +02:00
|
|
|
new_route_len(uint8_t purpose, extend_info_t *exit_ei, smartlist_t *nodes)
|
2007-06-12 11:17:23 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int num_acceptable_routers;
|
|
|
|
int routelen;
|
2007-06-12 11:17:23 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(nodes);
|
2012-08-31 23:02:19 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
routelen = DEFAULT_ROUTE_LEN;
|
2016-07-28 16:22:10 +02:00
|
|
|
if (exit_ei &&
|
2012-10-15 20:48:34 +02:00
|
|
|
purpose != CIRCUIT_PURPOSE_TESTING &&
|
|
|
|
purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
|
|
|
|
routelen++;
|
2012-08-31 23:02:19 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
num_acceptable_routers = count_acceptable_nodes(nodes);
|
2012-08-31 23:02:19 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC,"Chosen route length %d (%d/%d routers suitable).",
|
|
|
|
routelen, num_acceptable_routers, smartlist_len(nodes));
|
2012-08-31 23:02:19 +02:00
|
|
|
|
2013-10-08 17:48:33 +02:00
|
|
|
if (num_acceptable_routers < routelen) {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_info(LD_CIRC,
|
2013-10-08 17:48:33 +02:00
|
|
|
"Not enough acceptable routers (%d/%d). Discarding this circuit.",
|
|
|
|
num_acceptable_routers, routelen);
|
2012-10-15 20:48:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
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
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
return routelen;
|
2005-06-29 23:46:55 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return a newly allocated list of uint16_t * for each predicted port not
|
|
|
|
* handled by a current circuit. */
|
|
|
|
static smartlist_t *
|
|
|
|
circuit_get_unhandled_ports(time_t now)
|
2005-06-29 23:46:55 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
smartlist_t *dest = rep_hist_get_predicted_ports(now);
|
|
|
|
circuit_remove_handled_ports(dest);
|
|
|
|
return dest;
|
2005-06-29 23:46:55 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return 1 if we already have circuits present or on the way for
|
|
|
|
* all anticipated ports. Return 0 if we should make more.
|
|
|
|
*
|
|
|
|
* If we're returning 0, set need_uptime and need_capacity to
|
|
|
|
* indicate any requirements that the unhandled ports have.
|
2005-06-29 23:46:55 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
|
|
|
circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
|
|
|
|
int *need_capacity)
|
2005-06-29 23:46:55 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int i, enough;
|
|
|
|
uint16_t *port;
|
|
|
|
smartlist_t *sl = circuit_get_unhandled_ports(now);
|
|
|
|
smartlist_t *LongLivedServices = get_options()->LongLivedPorts;
|
|
|
|
tor_assert(need_uptime);
|
|
|
|
tor_assert(need_capacity);
|
|
|
|
// Always predict need_capacity
|
|
|
|
*need_capacity = 1;
|
|
|
|
enough = (smartlist_len(sl) == 0);
|
|
|
|
for (i = 0; i < smartlist_len(sl); ++i) {
|
|
|
|
port = smartlist_get(sl, i);
|
2012-04-11 18:50:50 +02:00
|
|
|
if (smartlist_contains_int_as_string(LongLivedServices, *port))
|
2012-10-15 20:48:34 +02:00
|
|
|
*need_uptime = 1;
|
|
|
|
tor_free(port);
|
|
|
|
}
|
|
|
|
smartlist_free(sl);
|
|
|
|
return enough;
|
2005-06-29 23:46:55 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return 1 if <b>node</b> can handle one or more of the ports in
|
|
|
|
* <b>needed_ports</b>, else return 0.
|
2007-11-26 03:18:57 +01:00
|
|
|
*/
|
2006-09-29 01:57:44 +02:00
|
|
|
static int
|
2012-10-15 20:48:34 +02:00
|
|
|
node_handles_some_port(const node_t *node, smartlist_t *needed_ports)
|
|
|
|
{ /* XXXX MOVE */
|
|
|
|
int i;
|
|
|
|
uint16_t port;
|
|
|
|
|
|
|
|
for (i = 0; i < smartlist_len(needed_ports); ++i) {
|
|
|
|
addr_policy_result_t r;
|
|
|
|
/* alignment issues aren't a worry for this dereference, since
|
|
|
|
needed_ports is explicitly a smartlist of uint16_t's */
|
|
|
|
port = *(uint16_t *)smartlist_get(needed_ports, i);
|
|
|
|
tor_assert(port);
|
|
|
|
if (node)
|
|
|
|
r = compare_tor_addr_to_node_policy(NULL, port, node);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
|
|
|
|
return 1;
|
2006-09-29 01:57:44 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2006-09-29 01:57:44 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return true iff <b>conn</b> needs another general circuit to be
|
|
|
|
* built. */
|
2006-09-29 01:57:44 +02:00
|
|
|
static int
|
2012-10-15 20:48:34 +02:00
|
|
|
ap_stream_wants_exit_attention(connection_t *conn)
|
2006-09-29 01:57:44 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
entry_connection_t *entry;
|
|
|
|
if (conn->type != CONN_TYPE_AP)
|
|
|
|
return 0;
|
|
|
|
entry = TO_ENTRY_CONN(conn);
|
|
|
|
|
|
|
|
if (conn->state == AP_CONN_STATE_CIRCUIT_WAIT &&
|
|
|
|
!conn->marked_for_close &&
|
|
|
|
!(entry->want_onehop) && /* ignore one-hop streams */
|
|
|
|
!(entry->use_begindir) && /* ignore targeted dir fetches */
|
|
|
|
!(entry->chosen_exit_name) && /* ignore defined streams */
|
|
|
|
!connection_edge_is_rendezvous_stream(TO_EDGE_CONN(conn)) &&
|
|
|
|
!circuit_stream_is_being_handled(TO_ENTRY_CONN(conn), 0,
|
|
|
|
MIN_CIRCUITS_HANDLING_STREAM))
|
2006-09-29 01:57:44 +02:00
|
|
|
return 1;
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2006-09-29 01:57:44 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return a pointer to a suitable router to be the exit node for the
|
|
|
|
* general-purpose circuit we're about to build.
|
|
|
|
*
|
|
|
|
* Look through the connection array, and choose a router that maximizes
|
|
|
|
* the number of pending streams that can exit from this router.
|
2009-09-20 04:59:14 +02:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* Return NULL if we can't find any suitable routers.
|
2005-12-27 08:23:52 +01:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
static const node_t *
|
|
|
|
choose_good_exit_server_general(int need_uptime, int need_capacity)
|
2005-12-27 08:23:52 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int *n_supported;
|
|
|
|
int n_pending_connections = 0;
|
|
|
|
smartlist_t *connections;
|
|
|
|
int best_support = -1;
|
|
|
|
int n_best_support=0;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2012-10-15 20:48:34 +02:00
|
|
|
const smartlist_t *the_nodes;
|
2016-07-05 20:19:31 +02:00
|
|
|
const node_t *selected_node=NULL;
|
2009-09-20 04:59:14 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
connections = get_connection_array();
|
|
|
|
|
|
|
|
/* Count how many connections are waiting for a circuit to be built.
|
|
|
|
* We use this for log messages now, but in the future we may depend on it.
|
|
|
|
*/
|
|
|
|
SMARTLIST_FOREACH(connections, connection_t *, conn,
|
|
|
|
{
|
|
|
|
if (ap_stream_wants_exit_attention(conn))
|
|
|
|
++n_pending_connections;
|
|
|
|
});
|
|
|
|
// log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
|
|
|
|
// n_pending_connections);
|
|
|
|
/* Now we count, for each of the routers in the directory, how many
|
|
|
|
* of the pending connections could possibly exit from that
|
|
|
|
* router (n_supported[i]). (We can't be sure about cases where we
|
|
|
|
* don't know the IP address of the pending connection.)
|
|
|
|
*
|
|
|
|
* -1 means "Don't use this router at all."
|
|
|
|
*/
|
|
|
|
the_nodes = nodelist_get_list();
|
2014-11-02 17:56:02 +01:00
|
|
|
n_supported = tor_calloc(smartlist_len(the_nodes), sizeof(int));
|
2012-10-15 20:48:34 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
|
|
|
|
const int i = node_sl_idx;
|
|
|
|
if (router_digest_is_me(node->identity)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
|
|
|
|
/* XXX there's probably a reverse predecessor attack here, but
|
|
|
|
* it's slow. should we take this out? -RD
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!node_has_descriptor(node)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue;
|
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
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!node->is_running || node->is_bad_exit) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue; /* skip routers that are known to be down or bad exits */
|
2011-06-01 02:43:55 +02:00
|
|
|
}
|
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
|
|
|
if (node_get_purpose(node) != ROUTER_PURPOSE_GENERAL) {
|
2012-10-15 20:48:34 +02:00
|
|
|
/* never pick a non-general node as a random exit. */
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue;
|
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
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
if (routerset_contains_node(options->ExcludeExitNodesUnion_, node)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue; /* user asked us not to use it, no matter what */
|
|
|
|
}
|
|
|
|
if (options->ExitNodes &&
|
|
|
|
!routerset_contains_node(options->ExitNodes, node)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue; /* not one of our chosen exit nodes */
|
2011-05-16 03:58:46 +02:00
|
|
|
}
|
2006-12-15 23:40:20 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (node_is_unreliable(node, need_uptime, need_capacity, 0)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue; /* skip routers that are not suitable. Don't worry if
|
|
|
|
* this makes us reject all the possible routers: if so,
|
|
|
|
* we'll retry later in this function with need_update and
|
|
|
|
* need_capacity set to 0. */
|
|
|
|
}
|
|
|
|
if (!(node->is_valid || options->AllowInvalid_ & ALLOW_INVALID_EXIT)) {
|
|
|
|
/* if it's invalid and we don't want it */
|
|
|
|
n_supported[i] = -1;
|
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- invalid router.",
|
|
|
|
// router->nickname, i);
|
|
|
|
continue; /* skip invalid routers */
|
|
|
|
}
|
|
|
|
if (options->ExcludeSingleHopRelays &&
|
|
|
|
node_allows_single_hop_exits(node)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (node_exit_policy_rejects_all(node)) {
|
|
|
|
n_supported[i] = -1;
|
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
|
|
|
|
// router->nickname, i);
|
|
|
|
continue; /* skip routers that reject all */
|
|
|
|
}
|
|
|
|
n_supported[i] = 0;
|
|
|
|
/* iterate over connections */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(connections, connection_t *, conn) {
|
|
|
|
if (!ap_stream_wants_exit_attention(conn))
|
|
|
|
continue; /* Skip everything but APs in CIRCUIT_WAIT */
|
|
|
|
if (connection_ap_can_use_exit(TO_ENTRY_CONN(conn), node)) {
|
|
|
|
++n_supported[i];
|
|
|
|
// log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
|
|
|
|
// router->nickname, i, n_supported[i]);
|
|
|
|
} else {
|
|
|
|
// log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
|
|
|
|
// router->nickname, i);
|
2007-12-24 11:31:39 +01:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
} SMARTLIST_FOREACH_END(conn);
|
|
|
|
if (n_pending_connections > 0 && n_supported[i] == 0) {
|
|
|
|
/* Leave best_support at -1 if that's where it is, so we can
|
|
|
|
* distinguish it later. */
|
|
|
|
continue;
|
2007-06-15 20:32:27 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
if (n_supported[i] > best_support) {
|
|
|
|
/* If this router is better than previous ones, remember its index
|
|
|
|
* and goodness, and start counting how many routers are this good. */
|
|
|
|
best_support = n_supported[i]; n_best_support=1;
|
|
|
|
// log_fn(LOG_DEBUG,"%s is new best supported option so far.",
|
|
|
|
// router->nickname);
|
|
|
|
} else if (n_supported[i] == best_support) {
|
|
|
|
/* If this router is _as good_ as the best one, just increment the
|
|
|
|
* count of equally good routers.*/
|
|
|
|
++n_best_support;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Found %d servers that might support %d/%d pending connections.",
|
|
|
|
n_best_support, best_support >= 0 ? best_support : 0,
|
|
|
|
n_pending_connections);
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* If any routers definitely support any pending connections, choose one
|
|
|
|
* at random. */
|
|
|
|
if (best_support > 0) {
|
|
|
|
smartlist_t *supporting = smartlist_new();
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
SMARTLIST_FOREACH(the_nodes, const node_t *, node, {
|
|
|
|
if (n_supported[node_sl_idx] == best_support)
|
|
|
|
smartlist_add(supporting, (void*)node);
|
|
|
|
});
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2016-07-05 20:19:31 +02:00
|
|
|
selected_node = node_sl_choose_by_bandwidth(supporting, WEIGHT_FOR_EXIT);
|
2012-10-15 20:48:34 +02:00
|
|
|
smartlist_free(supporting);
|
|
|
|
} else {
|
|
|
|
/* Either there are no pending connections, or no routers even seem to
|
|
|
|
* possibly support any of them. Choose a router at random that satisfies
|
|
|
|
* at least one predicted exit port. */
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
int attempt;
|
|
|
|
smartlist_t *needed_ports, *supporting;
|
2007-08-29 19:22:00 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (best_support == -1) {
|
|
|
|
if (need_uptime || need_capacity) {
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"We couldn't find any live%s%s routers; falling back "
|
|
|
|
"to list of all routers.",
|
|
|
|
need_capacity?", fast":"",
|
|
|
|
need_uptime?", stable":"");
|
|
|
|
tor_free(n_supported);
|
|
|
|
return choose_good_exit_server_general(0, 0);
|
2010-01-19 23:30:52 +01:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
log_notice(LD_CIRC, "All routers are down or won't exit%s -- "
|
|
|
|
"choosing a doomed exit at random.",
|
|
|
|
options->ExcludeExitNodesUnion_ ? " or are Excluded" : "");
|
2007-08-29 19:22:00 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
supporting = smartlist_new();
|
|
|
|
needed_ports = circuit_get_unhandled_ports(time(NULL));
|
|
|
|
for (attempt = 0; attempt < 2; attempt++) {
|
|
|
|
/* try once to pick only from routers that satisfy a needed port,
|
|
|
|
* then if there are none, pick from any that support exiting. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
|
|
|
|
if (n_supported[node_sl_idx] != -1 &&
|
|
|
|
(attempt || node_handles_some_port(node, needed_ports))) {
|
|
|
|
// log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.",
|
|
|
|
// try, router->nickname);
|
|
|
|
smartlist_add(supporting, (void*)node);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
2009-04-11 14:00:18 +02:00
|
|
|
|
2016-07-05 20:19:31 +02:00
|
|
|
selected_node = node_sl_choose_by_bandwidth(supporting, WEIGHT_FOR_EXIT);
|
|
|
|
if (selected_node)
|
2012-10-15 20:48:34 +02:00
|
|
|
break;
|
|
|
|
smartlist_clear(supporting);
|
|
|
|
/* If we reach this point, we can't actually support any unhandled
|
|
|
|
* predicted ports, so clear all the remaining ones. */
|
|
|
|
if (smartlist_len(needed_ports))
|
|
|
|
rep_hist_remove_predicted_ports(needed_ports);
|
2007-08-29 19:22:00 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
SMARTLIST_FOREACH(needed_ports, uint16_t *, cp, tor_free(cp));
|
|
|
|
smartlist_free(needed_ports);
|
|
|
|
smartlist_free(supporting);
|
2007-08-29 19:22:00 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_free(n_supported);
|
2016-07-05 20:19:31 +02:00
|
|
|
if (selected_node) {
|
|
|
|
log_info(LD_CIRC, "Chose exit server '%s'", node_describe(selected_node));
|
|
|
|
return selected_node;
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
if (options->ExitNodes) {
|
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"No specified %sexit routers seem to be running: "
|
|
|
|
"can't choose an exit.",
|
|
|
|
options->ExcludeExitNodesUnion_ ? "non-excluded " : "");
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
return NULL;
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2015-02-11 20:54:16 +01:00
|
|
|
#if defined(ENABLE_TOR2WEB_MODE) || defined(TOR_UNIT_TESTS)
|
2014-09-15 14:28:11 +02:00
|
|
|
/* The config option Tor2webRendezvousPoints has been set and we need
|
|
|
|
* to pick an RP out of that set. Make sure that the RP we choose is
|
|
|
|
* alive, and return it. Return NULL if no usable RP could be found in
|
|
|
|
* Tor2webRendezvousPoints. */
|
|
|
|
STATIC const node_t *
|
|
|
|
pick_tor2web_rendezvous_node(router_crn_flags_t flags,
|
|
|
|
const or_options_t *options)
|
|
|
|
{
|
|
|
|
const node_t *rp_node = NULL;
|
|
|
|
const int allow_invalid = (flags & CRN_ALLOW_INVALID) != 0;
|
|
|
|
const int need_desc = (flags & CRN_NEED_DESC) != 0;
|
2016-08-18 04:02:33 +02:00
|
|
|
const int pref_addr = (flags & CRN_PREF_ADDR) != 0;
|
|
|
|
const int direct_conn = (flags & CRN_DIRECT_CONN) != 0;
|
2014-09-15 14:28:11 +02:00
|
|
|
|
|
|
|
smartlist_t *whitelisted_live_rps = smartlist_new();
|
|
|
|
smartlist_t *all_live_nodes = smartlist_new();
|
|
|
|
|
|
|
|
tor_assert(options->Tor2webRendezvousPoints);
|
|
|
|
|
|
|
|
/* Add all running nodes to all_live_nodes */
|
|
|
|
router_add_running_nodes_to_smartlist(all_live_nodes,
|
|
|
|
allow_invalid,
|
|
|
|
0, 0, 0,
|
2016-08-18 04:02:33 +02:00
|
|
|
need_desc,
|
|
|
|
pref_addr,
|
|
|
|
direct_conn);
|
2014-09-15 14:28:11 +02:00
|
|
|
|
|
|
|
/* Filter all_live_nodes to only add live *and* whitelisted RPs to
|
|
|
|
* the list whitelisted_live_rps. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(all_live_nodes, node_t *, live_node) {
|
|
|
|
if (routerset_contains_node(options->Tor2webRendezvousPoints, live_node)) {
|
|
|
|
smartlist_add(whitelisted_live_rps, live_node);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(live_node);
|
|
|
|
|
|
|
|
/* Honor ExcludeNodes */
|
|
|
|
if (options->ExcludeNodes) {
|
|
|
|
routerset_subtract_nodes(whitelisted_live_rps, options->ExcludeNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now pick randomly amongst the whitelisted RPs. No need to waste time
|
|
|
|
doing bandwidth load balancing, for most use cases
|
|
|
|
'whitelisted_live_rps' contains a single OR anyway. */
|
|
|
|
rp_node = smartlist_choose(whitelisted_live_rps);
|
|
|
|
|
|
|
|
if (!rp_node) {
|
|
|
|
log_warn(LD_REND, "Could not find a Rendezvous Point that suits "
|
|
|
|
"the purposes of Tor2webRendezvousPoints. Choosing random one.");
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(whitelisted_live_rps);
|
|
|
|
smartlist_free(all_live_nodes);
|
|
|
|
|
|
|
|
return rp_node;
|
|
|
|
}
|
2015-02-11 20:54:16 +01:00
|
|
|
#endif
|
2014-09-15 14:28:11 +02:00
|
|
|
|
|
|
|
/* Pick a Rendezvous Point for our HS circuits according to <b>flags</b>. */
|
|
|
|
static const node_t *
|
|
|
|
pick_rendezvous_node(router_crn_flags_t flags)
|
|
|
|
{
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
|
|
|
|
if (options->AllowInvalid_ & ALLOW_INVALID_RENDEZVOUS)
|
|
|
|
flags |= CRN_ALLOW_INVALID;
|
|
|
|
|
2015-02-11 20:54:16 +01:00
|
|
|
#ifdef ENABLE_TOR2WEB_MODE
|
2016-08-18 05:15:48 +02:00
|
|
|
/* We want to connect directly to the node if we can */
|
|
|
|
router_crn_flags_t direct_flags = flags;
|
|
|
|
direct_flags |= CRN_PREF_ADDR;
|
|
|
|
direct_flags |= CRN_DIRECT_CONN;
|
|
|
|
|
2014-09-15 14:28:11 +02:00
|
|
|
/* The user wants us to pick specific RPs. */
|
|
|
|
if (options->Tor2webRendezvousPoints) {
|
2016-08-18 05:15:48 +02:00
|
|
|
const node_t *tor2web_rp = pick_tor2web_rendezvous_node(direct_flags,
|
|
|
|
options);
|
2014-09-15 14:28:11 +02:00
|
|
|
if (tor2web_rp) {
|
|
|
|
return tor2web_rp;
|
|
|
|
}
|
2016-08-18 05:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Else, if no direct, preferred tor2web RP was found, fall back to choosing
|
|
|
|
* a random direct node */
|
|
|
|
const node_t *node = router_choose_random_node(NULL, options->ExcludeNodes,
|
|
|
|
direct_flags);
|
|
|
|
/* Return the direct node (if found), or log a message and fall back to an
|
|
|
|
* indirect connection. */
|
|
|
|
if (node) {
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
log_info(LD_REND,
|
|
|
|
"Unable to find a random rendezvous point that is reachable via "
|
|
|
|
"a direct connection, falling back to a 3-hop path.");
|
2014-09-15 14:28:11 +02:00
|
|
|
}
|
2015-02-11 20:54:16 +01:00
|
|
|
#endif
|
2014-09-15 14:28:11 +02:00
|
|
|
|
|
|
|
return router_choose_random_node(NULL, options->ExcludeNodes, flags);
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return a pointer to a suitable router to be the exit node for the
|
|
|
|
* circuit of purpose <b>purpose</b> that we're about to build (or NULL
|
|
|
|
* if no router is suitable).
|
2009-02-05 00:27:35 +01:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* For general-purpose circuits, pass it off to
|
|
|
|
* choose_good_exit_server_general()
|
2009-02-05 00:27:35 +01:00
|
|
|
*
|
2012-10-15 20:48:34 +02:00
|
|
|
* For client-side rendezvous circuits, choose a random node, weighted
|
|
|
|
* toward the preferences in 'options'.
|
2005-07-23 06:12:35 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
static const node_t *
|
|
|
|
choose_good_exit_server(uint8_t purpose,
|
|
|
|
int need_uptime, int need_capacity, int is_internal)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
const or_options_t *options = get_options();
|
|
|
|
router_crn_flags_t flags = CRN_NEED_DESC;
|
|
|
|
if (need_uptime)
|
|
|
|
flags |= CRN_NEED_UPTIME;
|
|
|
|
if (need_capacity)
|
|
|
|
flags |= CRN_NEED_CAPACITY;
|
2006-09-29 01:57:44 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
switch (purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
if (options->AllowInvalid_ & ALLOW_INVALID_MIDDLE)
|
|
|
|
flags |= CRN_ALLOW_INVALID;
|
|
|
|
if (is_internal) /* pick it like a middle hop */
|
|
|
|
return router_choose_random_node(NULL, options->ExcludeNodes, flags);
|
|
|
|
else
|
|
|
|
return choose_good_exit_server_general(need_uptime,need_capacity);
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
2014-09-15 14:28:11 +02:00
|
|
|
{
|
|
|
|
/* Pick a new RP */
|
|
|
|
const node_t *rendezvous_node = pick_rendezvous_node(flags);
|
2014-09-22 20:14:50 +02:00
|
|
|
log_info(LD_REND, "Picked new RP: %s",
|
|
|
|
safe_str_client(node_describe(rendezvous_node)));
|
2014-09-15 14:28:11 +02:00
|
|
|
return rendezvous_node;
|
|
|
|
}
|
2006-09-29 01:57:44 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
log_warn(LD_BUG,"Unhandled purpose %d", purpose);
|
|
|
|
tor_fragile_assert();
|
|
|
|
return NULL;
|
2005-12-31 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Log a warning if the user specified an exit for the circuit that
|
|
|
|
* has been excluded from use by ExcludeNodes or ExcludeExitNodes. */
|
2007-02-28 01:23:05 +01:00
|
|
|
static void
|
2016-07-28 17:24:03 +02:00
|
|
|
warn_if_last_router_excluded(origin_circuit_t *circ,
|
|
|
|
const extend_info_t *exit_ei)
|
2005-12-31 07:32:57 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
const or_options_t *options = get_options();
|
|
|
|
routerset_t *rs = options->ExcludeNodes;
|
|
|
|
const char *description;
|
|
|
|
uint8_t purpose = circ->base_.purpose;
|
2007-02-28 01:23:05 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (circ->build_state->onehop_tunnel)
|
2007-02-28 01:23:05 +01:00
|
|
|
return;
|
2006-01-02 12:33:14 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
switch (purpose)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case CIRCUIT_PURPOSE_OR:
|
|
|
|
case CIRCUIT_PURPOSE_INTRO_POINT:
|
|
|
|
case CIRCUIT_PURPOSE_REND_POINT_WAITING:
|
|
|
|
case CIRCUIT_PURPOSE_REND_ESTABLISHED:
|
|
|
|
log_warn(LD_BUG, "Called on non-origin circuit (purpose %d, %s)",
|
|
|
|
(int)purpose,
|
|
|
|
circuit_purpose_to_string(purpose));
|
|
|
|
return;
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
if (circ->build_state->is_internal)
|
|
|
|
return;
|
|
|
|
description = "requested exit node";
|
|
|
|
rs = options->ExcludeExitNodesUnion_;
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
case CIRCUIT_PURPOSE_S_REND_JOINED:
|
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
return;
|
|
|
|
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:
|
|
|
|
description = "chosen rendezvous point";
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_CONTROLLER:
|
|
|
|
rs = options->ExcludeExitNodesUnion_;
|
|
|
|
description = "controller-selected circuit target";
|
Revise code for adding EntryNodes to guards.
Previously, we'd just take all the nodes in EntryNodes, see which
ones were already in the guard list, and add the ones that weren't.
There were some problems there, though:
* We'd add _every_ entry in EntryNodes, and add them in the order
they appeared in the routerlist. This wasn't a problem
until we added the ability to give country-code or IP-range
entries in the EntryNodes set, but now that we did, it is.
(Fix: We now shuffle the entry nodes before adding them; only
add up to 10*NumEntryGuards)
* We didn't screen EntryNodes for the Guard flag. That's okay
if the user has specified two or three entry nodes manually,
but if they have listed a whole subcontinent, we should
restrict ourselves to the entries that are currently guards.
(Fix: separate out the new guard from the new non-guard nodes,
and add the Guards first.)
* We'd prepend new EntryNodes _before_ the already configured
EntryNodes. This could lead to churn.
(Fix: don't prepend these.)
This patch also pre-screens EntryNodes entries for
reachableaddresses/excludenodes, even though we check for that
later. This is important now, since we cap the number of entries
we'll add.
2011-07-11 16:48:46 +02:00
|
|
|
break;
|
2007-11-16 08:37:49 +01:00
|
|
|
}
|
2012-05-04 05:15:34 +02:00
|
|
|
|
2016-07-28 17:24:03 +02:00
|
|
|
if (routerset_contains_extendinfo(rs, exit_ei)) {
|
2012-10-15 20:48:34 +02:00
|
|
|
/* We should never get here if StrictNodes is set to 1. */
|
|
|
|
if (options->StrictNodes) {
|
|
|
|
log_warn(LD_BUG, "Using %s '%s' which is listed in ExcludeNodes%s, "
|
|
|
|
"even though StrictNodes is set. Please report. "
|
|
|
|
"(Circuit purpose: %s)",
|
2016-07-28 17:24:03 +02:00
|
|
|
description, extend_info_describe(exit_ei),
|
2012-10-15 20:48:34 +02:00
|
|
|
rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
|
|
|
|
circuit_purpose_to_string(purpose));
|
2007-08-29 19:22:00 +02:00
|
|
|
} else {
|
2012-10-15 20:48:34 +02:00
|
|
|
log_warn(LD_CIRC, "Using %s '%s' which is listed in "
|
|
|
|
"ExcludeNodes%s, because no better options were available. To "
|
|
|
|
"prevent this (and possibly break your Tor functionality), "
|
|
|
|
"set the StrictNodes configuration option. "
|
|
|
|
"(Circuit purpose: %s)",
|
2016-07-28 17:24:03 +02:00
|
|
|
description, extend_info_describe(exit_ei),
|
2012-10-15 20:48:34 +02:00
|
|
|
rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
|
|
|
|
circuit_purpose_to_string(purpose));
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_log_path(LOG_WARN, LD_CIRC, circ);
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
return;
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Decide a suitable length for circ's cpath, and pick an exit
|
|
|
|
* router (or use <b>exit</b> if provided). Store these in the
|
|
|
|
* cpath. Return 0 if ok, -1 if circuit should be closed. */
|
|
|
|
static int
|
2016-07-28 16:22:10 +02:00
|
|
|
onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
|
2005-07-28 21:01:48 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
cpath_build_state_t *state = circ->build_state;
|
2005-07-28 21:01:48 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (state->onehop_tunnel) {
|
2016-07-14 06:04:02 +02:00
|
|
|
log_debug(LD_CIRC, "Launching a one-hop circuit for dir tunnel%s.",
|
|
|
|
(rend_allow_non_anonymous_connection(get_options()) ?
|
|
|
|
", or intro or rendezvous connection" : ""));
|
2012-10-15 20:48:34 +02:00
|
|
|
state->desired_path_len = 1;
|
|
|
|
} else {
|
2016-07-28 16:22:10 +02:00
|
|
|
int r = new_route_len(circ->base_.purpose, exit_ei, nodelist_get_list());
|
2012-10-15 20:48:34 +02:00
|
|
|
if (r < 1) /* must be at least 1 */
|
|
|
|
return -1;
|
|
|
|
state->desired_path_len = r;
|
|
|
|
}
|
|
|
|
|
2016-07-28 16:22:10 +02:00
|
|
|
if (exit_ei) { /* the circuit-builder pre-requested one */
|
|
|
|
warn_if_last_router_excluded(circ, exit_ei);
|
2012-10-15 20:48:34 +02:00
|
|
|
log_info(LD_CIRC,"Using requested exit node '%s'",
|
2016-07-28 16:22:10 +02:00
|
|
|
extend_info_describe(exit_ei));
|
|
|
|
exit_ei = extend_info_dup(exit_ei);
|
2012-10-15 20:48:34 +02:00
|
|
|
} else { /* we have to decide one */
|
|
|
|
const node_t *node =
|
|
|
|
choose_good_exit_server(circ->base_.purpose, state->need_uptime,
|
|
|
|
state->need_capacity, state->is_internal);
|
|
|
|
if (!node) {
|
2014-12-25 12:23:54 +01:00
|
|
|
log_warn(LD_CIRC,"Failed to choose an exit server");
|
2012-10-15 20:48:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-07-28 16:22:10 +02:00
|
|
|
exit_ei = extend_info_from_node(node, 0);
|
|
|
|
tor_assert(exit_ei);
|
2012-10-15 20:48:34 +02:00
|
|
|
}
|
2016-07-28 16:22:10 +02:00
|
|
|
state->chosen_exit = exit_ei;
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Give <b>circ</b> a new exit destination to <b>exit</b>, and add a
|
|
|
|
* hop to the cpath reflecting this. Don't send the next extend cell --
|
|
|
|
* the caller will do this if it wants to.
|
2005-12-24 21:07:19 +01:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
int
|
2016-07-28 16:22:10 +02:00
|
|
|
circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
|
2005-07-28 21:01:48 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
cpath_build_state_t *state;
|
2016-07-28 16:22:10 +02:00
|
|
|
tor_assert(exit_ei);
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_assert(circ);
|
2005-07-28 21:01:48 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
state = circ->build_state;
|
|
|
|
tor_assert(state);
|
|
|
|
extend_info_free(state->chosen_exit);
|
2016-07-28 16:22:10 +02:00
|
|
|
state->chosen_exit = extend_info_dup(exit_ei);
|
2012-05-04 05:15:34 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
++circ->build_state->desired_path_len;
|
2016-07-28 16:22:10 +02:00
|
|
|
onion_append_hop(&circ->cpath, exit_ei);
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2005-08-04 21:56:41 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Take an open <b>circ</b>, and add a new hop at the end, based on
|
|
|
|
* <b>info</b>. Set its state back to CIRCUIT_STATE_BUILDING, and then
|
|
|
|
* send the next extend cell to begin connecting to that hop.
|
|
|
|
*/
|
2005-08-04 21:56:41 +02:00
|
|
|
int
|
2016-07-28 16:22:10 +02:00
|
|
|
circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit_ei)
|
2005-08-04 21:56:41 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int err_reason = 0;
|
2016-07-28 16:22:10 +02:00
|
|
|
warn_if_last_router_excluded(circ, exit_ei);
|
2012-12-08 03:57:51 +01:00
|
|
|
|
|
|
|
tor_gettimeofday(&circ->base_.timestamp_began);
|
|
|
|
|
2016-07-28 16:22:10 +02:00
|
|
|
circuit_append_new_exit(circ, exit_ei);
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
|
|
|
|
if ((err_reason = circuit_send_next_onion_skin(circ))<0) {
|
|
|
|
log_warn(LD_CIRC, "Couldn't extend circuit to new point %s.",
|
2016-07-28 16:22:10 +02:00
|
|
|
extend_info_describe(exit_ei));
|
2012-10-15 20:48:34 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
|
|
|
|
return -1;
|
2005-08-04 21:56:41 +02:00
|
|
|
}
|
2012-12-08 02:47:23 +01:00
|
|
|
|
2013-01-19 04:34:01 +01:00
|
|
|
// XXX: Should cannibalized circuits be dirty or not? Not easy to say..
|
2012-12-08 02:47:23 +01:00
|
|
|
|
2005-08-04 21:56:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-08 23:58:48 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return the number of routers in <b>routers</b> that are currently up
|
|
|
|
* and available for building circuits through.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
count_acceptable_nodes(smartlist_t *nodes)
|
2007-06-15 08:01:04 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int num=0;
|
2007-07-18 12:06:03 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
|
|
|
|
// log_debug(LD_CIRC,
|
|
|
|
// "Contemplating whether router %d (%s) is a new option.",
|
|
|
|
// i, r->nickname);
|
|
|
|
if (! node->is_running)
|
|
|
|
// log_debug(LD_CIRC,"Nope, the directory says %d is not running.",i);
|
|
|
|
continue;
|
2016-07-07 08:04:26 +02:00
|
|
|
/* XXX This clause makes us count incorrectly: if AllowInvalidRouters
|
|
|
|
* allows this node in some places, then we're getting an inaccurate
|
|
|
|
* count. For now, be conservative and don't count it. But later we
|
|
|
|
* should try to be smarter. */
|
2012-10-15 20:48:34 +02:00
|
|
|
if (! node->is_valid)
|
|
|
|
// log_debug(LD_CIRC,"Nope, the directory says %d is not valid.",i);
|
|
|
|
continue;
|
|
|
|
if (! node_has_descriptor(node))
|
|
|
|
continue;
|
2016-07-07 08:04:26 +02:00
|
|
|
/* The node has a descriptor, so we can just check the ntor key directly */
|
|
|
|
if (!node_has_curve25519_onion_key(node))
|
|
|
|
continue;
|
2012-10-15 20:48:34 +02:00
|
|
|
++num;
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
2010-06-04 02:29:29 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
// log_debug(LD_CIRC,"I like %d. num_acceptable_routers now %d.",i, num);
|
2010-06-04 02:29:29 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
return num;
|
2011-06-02 19:05:00 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
|
|
|
|
* This function is used to extend cpath by another hop.
|
2010-06-04 02:29:29 +02:00
|
|
|
*/
|
|
|
|
void
|
2012-10-15 20:48:34 +02:00
|
|
|
onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
|
2010-06-04 02:29:29 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
if (*head_ptr) {
|
|
|
|
new_hop->next = (*head_ptr);
|
|
|
|
new_hop->prev = (*head_ptr)->prev;
|
|
|
|
(*head_ptr)->prev->next = new_hop;
|
|
|
|
(*head_ptr)->prev = new_hop;
|
|
|
|
} else {
|
|
|
|
*head_ptr = new_hop;
|
|
|
|
new_hop->prev = new_hop->next = new_hop;
|
2010-06-04 02:29:29 +02:00
|
|
|
}
|
2007-06-15 08:01:04 +02:00
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** A helper function used by onion_extend_cpath(). Use <b>purpose</b>
|
|
|
|
* and <b>state</b> and the cpath <b>head</b> (currently populated only
|
|
|
|
* to length <b>cur_len</b> to decide a suitable middle hop for a
|
|
|
|
* circuit. In particular, make sure we don't pick the exit node or its
|
|
|
|
* family, and make sure we don't duplicate any previous nodes or their
|
|
|
|
* families. */
|
|
|
|
static const node_t *
|
|
|
|
choose_good_middle_server(uint8_t purpose,
|
|
|
|
cpath_build_state_t *state,
|
|
|
|
crypt_path_t *head,
|
|
|
|
int cur_len)
|
2012-06-02 23:21:49 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
int i;
|
|
|
|
const node_t *r, *choice;
|
|
|
|
crypt_path_t *cpath;
|
|
|
|
smartlist_t *excluded;
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
router_crn_flags_t flags = CRN_NEED_DESC;
|
|
|
|
tor_assert(CIRCUIT_PURPOSE_MIN_ <= purpose &&
|
|
|
|
purpose <= CIRCUIT_PURPOSE_MAX_);
|
|
|
|
|
2014-12-25 12:23:54 +01:00
|
|
|
log_debug(LD_CIRC, "Contemplating intermediate hop %d: random choice.",
|
|
|
|
cur_len);
|
2012-10-15 20:48:34 +02:00
|
|
|
excluded = smartlist_new();
|
|
|
|
if ((r = build_state_get_exit_node(state))) {
|
|
|
|
nodelist_add_node_and_family(excluded, r);
|
|
|
|
}
|
|
|
|
for (i = 0, cpath = head; i < cur_len; ++i, cpath=cpath->next) {
|
|
|
|
if ((r = node_get_by_id(cpath->extend_info->identity_digest))) {
|
|
|
|
nodelist_add_node_and_family(excluded, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->need_uptime)
|
|
|
|
flags |= CRN_NEED_UPTIME;
|
|
|
|
if (state->need_capacity)
|
|
|
|
flags |= CRN_NEED_CAPACITY;
|
|
|
|
if (options->AllowInvalid_ & ALLOW_INVALID_MIDDLE)
|
|
|
|
flags |= CRN_ALLOW_INVALID;
|
|
|
|
choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
|
|
|
|
smartlist_free(excluded);
|
|
|
|
return choice;
|
2012-06-02 23:21:49 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Pick a good entry server for the circuit to be built according to
|
|
|
|
* <b>state</b>. Don't reuse a chosen exit (if any), don't use this
|
|
|
|
* router (if we're an OR), and respect firewall settings; if we're
|
|
|
|
* configured to use entry guards, return one.
|
|
|
|
*
|
|
|
|
* If <b>state</b> is NULL, we're choosing a router to serve as an entry
|
|
|
|
* guard, not for any particular circuit.
|
2012-06-02 23:21:49 +02:00
|
|
|
*/
|
2012-10-22 20:36:30 +02:00
|
|
|
const node_t *
|
2012-10-15 20:48:34 +02:00
|
|
|
choose_good_entry_server(uint8_t purpose, cpath_build_state_t *state)
|
2012-06-02 23:21:49 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
const node_t *choice;
|
|
|
|
smartlist_t *excluded;
|
|
|
|
const or_options_t *options = get_options();
|
2015-12-18 01:28:54 +01:00
|
|
|
/* If possible, choose an entry server with a preferred address,
|
|
|
|
* otherwise, choose one with an allowed address */
|
2016-08-18 04:02:33 +02:00
|
|
|
router_crn_flags_t flags = (CRN_NEED_GUARD|CRN_NEED_DESC|CRN_PREF_ADDR|
|
|
|
|
CRN_DIRECT_CONN);
|
2012-10-15 20:48:34 +02:00
|
|
|
const node_t *node;
|
2012-06-02 23:21:49 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (state && options->UseEntryGuards &&
|
|
|
|
(purpose != CIRCUIT_PURPOSE_TESTING || options->BridgeRelay)) {
|
|
|
|
/* This request is for an entry server to use for a regular circuit,
|
|
|
|
* and we use entry guard nodes. Just return one of the guard nodes. */
|
|
|
|
return choose_random_entry(state);
|
|
|
|
}
|
2012-06-02 23:21:49 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
excluded = smartlist_new();
|
2007-06-10 09:34:21 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (state && (node = build_state_get_exit_node(state))) {
|
|
|
|
/* Exclude the exit node from the state, if we have one. Also exclude its
|
|
|
|
* family. */
|
|
|
|
nodelist_add_node_and_family(excluded, node);
|
|
|
|
}
|
2014-12-25 13:42:38 +01:00
|
|
|
/* and exclude current entry guards and their families,
|
|
|
|
* unless we're in a test network, and excluding guards
|
|
|
|
* would exclude all nodes (i.e. we're in an incredibly small tor network,
|
|
|
|
* or we're using TestingAuthVoteGuard *).
|
|
|
|
* This is an incomplete fix, but is no worse than the previous behaviour,
|
|
|
|
* and only applies to minimal, testing tor networks
|
|
|
|
* (so it's no less secure) */
|
2016-05-30 22:18:16 +02:00
|
|
|
/*XXXX++ use the using_as_guard flag to accomplish this.*/
|
2014-12-25 13:42:38 +01:00
|
|
|
if (options->UseEntryGuards
|
|
|
|
&& (!options->TestingTorNetwork ||
|
2015-01-07 16:09:09 +01:00
|
|
|
smartlist_len(nodelist_get_list()) > smartlist_len(get_entry_guards())
|
2014-12-25 13:42:38 +01:00
|
|
|
)) {
|
2012-10-15 20:48:34 +02:00
|
|
|
SMARTLIST_FOREACH(get_entry_guards(), const entry_guard_t *, entry,
|
|
|
|
{
|
|
|
|
if ((node = node_get_by_id(entry->identity))) {
|
|
|
|
nodelist_add_node_and_family(excluded, node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-04-04 00:12:26 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (state) {
|
|
|
|
if (state->need_uptime)
|
|
|
|
flags |= CRN_NEED_UPTIME;
|
|
|
|
if (state->need_capacity)
|
|
|
|
flags |= CRN_NEED_CAPACITY;
|
|
|
|
}
|
|
|
|
if (options->AllowInvalid_ & ALLOW_INVALID_ENTRY)
|
|
|
|
flags |= CRN_ALLOW_INVALID;
|
2011-04-04 00:12:26 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
|
|
|
|
smartlist_free(excluded);
|
|
|
|
return choice;
|
2007-11-04 01:15:42 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return the first non-open hop in cpath, or return NULL if all
|
|
|
|
* hops are open. */
|
|
|
|
static crypt_path_t *
|
|
|
|
onion_next_hop_in_cpath(crypt_path_t *cpath)
|
2012-05-18 02:07:46 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypt_path_t *hop = cpath;
|
|
|
|
do {
|
|
|
|
if (hop->state != CPATH_STATE_OPEN)
|
|
|
|
return hop;
|
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != cpath);
|
2012-05-18 02:07:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Choose a suitable next hop in the cpath <b>head_ptr</b>,
|
|
|
|
* based on <b>state</b>. Append the hop info to head_ptr.
|
2014-02-06 23:08:50 +01:00
|
|
|
*
|
|
|
|
* Return 1 if the path is complete, 0 if we successfully added a hop,
|
|
|
|
* and -1 on error.
|
2011-06-22 23:28:11 +02:00
|
|
|
*/
|
2012-10-15 20:48:34 +02:00
|
|
|
static int
|
|
|
|
onion_extend_cpath(origin_circuit_t *circ)
|
2011-06-12 00:14:11 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
uint8_t purpose = circ->base_.purpose;
|
|
|
|
cpath_build_state_t *state = circ->build_state;
|
|
|
|
int cur_len = circuit_get_cpath_len(circ);
|
|
|
|
extend_info_t *info = NULL;
|
2011-06-12 00:14:11 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (cur_len >= state->desired_path_len) {
|
|
|
|
log_debug(LD_CIRC, "Path is complete: %d steps long",
|
|
|
|
state->desired_path_len);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-05 22:08:19 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC, "Path is %d long; we want %d", cur_len,
|
|
|
|
state->desired_path_len);
|
2008-08-05 22:08:19 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (cur_len == state->desired_path_len - 1) { /* Picking last node */
|
|
|
|
info = extend_info_dup(state->chosen_exit);
|
|
|
|
} else if (cur_len == 0) { /* picking first node */
|
|
|
|
const node_t *r = choose_good_entry_server(purpose, state);
|
|
|
|
if (r) {
|
|
|
|
/* If we're a client, use the preferred address rather than the
|
|
|
|
primary address, for potentially connecting to an IPv6 OR
|
2016-01-22 07:43:24 +01:00
|
|
|
port. Servers always want the primary (IPv4) address. */
|
|
|
|
int client = (server_mode(get_options()) == 0);
|
|
|
|
info = extend_info_from_node(r, client);
|
|
|
|
/* Clients can fail to find an allowed address */
|
|
|
|
tor_assert(info || client);
|
2012-10-15 20:48:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const node_t *r =
|
|
|
|
choose_good_middle_server(purpose, state, circ->cpath, cur_len);
|
|
|
|
if (r) {
|
|
|
|
info = extend_info_from_node(r, 0);
|
|
|
|
tor_assert(info);
|
|
|
|
}
|
2011-04-04 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!info) {
|
|
|
|
log_warn(LD_CIRC,"Failed to find node for hop %d of our path. Discarding "
|
|
|
|
"this circuit.", cur_len);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-11-04 01:15:42 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
log_debug(LD_CIRC,"Chose router %s for hop %d (exit is %s)",
|
|
|
|
extend_info_describe(info),
|
|
|
|
cur_len+1, build_state_get_exit_nickname(state));
|
2007-11-04 01:15:42 +01:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
onion_append_hop(&circ->cpath, info);
|
|
|
|
extend_info_free(info);
|
|
|
|
return 0;
|
2007-11-04 01:15:42 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Create a new hop, annotate it with information about its
|
|
|
|
* corresponding router <b>choice</b>, and append it to the
|
|
|
|
* end of the cpath <b>head_ptr</b>. */
|
|
|
|
static int
|
|
|
|
onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
|
2007-06-10 09:34:21 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
|
2007-06-10 09:34:21 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/* link hop into the cpath, at the end. */
|
|
|
|
onion_append_to_cpath(head_ptr, hop);
|
2007-06-10 09:34:21 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
hop->magic = CRYPT_PATH_MAGIC;
|
|
|
|
hop->state = CPATH_STATE_CLOSED;
|
2011-07-13 19:06:07 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
hop->extend_info = extend_info_dup(choice);
|
2007-07-18 12:06:03 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
hop->package_window = circuit_initial_package_window();
|
|
|
|
hop->deliver_window = CIRCWINDOW_START;
|
2007-07-18 09:13:15 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
return 0;
|
2007-06-10 09:34:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Allocate a new extend_info object based on the various arguments. */
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_new(const char *nickname, const char *digest,
|
2012-12-06 07:53:29 +01:00
|
|
|
crypto_pk_t *onion_key,
|
|
|
|
const curve25519_public_key_t *curve25519_key,
|
|
|
|
const tor_addr_t *addr, uint16_t port)
|
2011-02-08 09:11:18 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
extend_info_t *info = tor_malloc_zero(sizeof(extend_info_t));
|
|
|
|
memcpy(info->identity_digest, digest, DIGEST_LEN);
|
|
|
|
if (nickname)
|
|
|
|
strlcpy(info->nickname, nickname, sizeof(info->nickname));
|
|
|
|
if (onion_key)
|
|
|
|
info->onion_key = crypto_pk_dup_key(onion_key);
|
2012-12-06 07:53:29 +01:00
|
|
|
if (curve25519_key)
|
|
|
|
memcpy(&info->curve25519_onion_key, curve25519_key,
|
|
|
|
sizeof(curve25519_public_key_t));
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_addr_copy(&info->addr, addr);
|
|
|
|
info->port = port;
|
|
|
|
return info;
|
2011-02-08 09:11:18 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Allocate and return a new extend_info that can be used to build a
|
|
|
|
* circuit to or through the node <b>node</b>. Use the primary address
|
|
|
|
* of the node (i.e. its IPv4 address) unless
|
|
|
|
* <b>for_direct_connect</b> is true, in which case the preferred
|
|
|
|
* address is used instead. May return NULL if there is not enough
|
|
|
|
* info about <b>node</b> to extend to it--for example, if there is no
|
2016-01-22 07:43:24 +01:00
|
|
|
* routerinfo_t or microdesc_t, or if for_direct_connect is true and none of
|
|
|
|
* the node's addresses are allowed by tor's firewall and IP version config.
|
2012-10-15 20:48:34 +02:00
|
|
|
**/
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_from_node(const node_t *node, int for_direct_connect)
|
2007-06-12 11:17:23 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
tor_addr_port_t ap;
|
2016-01-22 07:43:24 +01:00
|
|
|
int valid_addr = 0;
|
2007-06-12 11:17:23 +02:00
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
if (node->ri == NULL && (node->rs == NULL || node->md == NULL))
|
|
|
|
return NULL;
|
2007-07-18 12:06:03 +02:00
|
|
|
|
2016-01-22 07:43:24 +01:00
|
|
|
/* Choose a preferred address first, but fall back to an allowed address.
|
|
|
|
* choose_address returns 1 on success, but get_prim_orport returns 0. */
|
2012-10-15 20:48:34 +02:00
|
|
|
if (for_direct_connect)
|
2016-01-22 07:43:24 +01:00
|
|
|
valid_addr = fascist_firewall_choose_address_node(node,
|
|
|
|
FIREWALL_OR_CONNECTION,
|
|
|
|
0, &ap);
|
2012-10-15 20:48:34 +02:00
|
|
|
else
|
2016-01-22 07:43:24 +01:00
|
|
|
valid_addr = !node_get_prim_orport(node, &ap);
|
2008-06-20 06:34:39 +02:00
|
|
|
|
2016-01-22 07:43:24 +01:00
|
|
|
if (valid_addr)
|
|
|
|
log_debug(LD_CIRC, "using %s for %s",
|
|
|
|
fmt_addrport(&ap.addr, ap.port),
|
|
|
|
node->ri ? node->ri->nickname : node->rs->nickname);
|
|
|
|
else
|
|
|
|
log_warn(LD_CIRC, "Could not choose valid address for %s",
|
|
|
|
node->ri ? node->ri->nickname : node->rs->nickname);
|
2012-10-15 20:48:34 +02:00
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/* Every node we connect or extend to must support ntor */
|
|
|
|
if (!node_has_curve25519_onion_key(node)) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_CIRC,
|
|
|
|
"Attempted to create extend_info for a node that does not support "
|
|
|
|
"ntor: %s", node_describe(node));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-22 07:43:24 +01:00
|
|
|
if (valid_addr && node->ri)
|
2012-10-15 20:48:34 +02:00
|
|
|
return extend_info_new(node->ri->nickname,
|
|
|
|
node->identity,
|
|
|
|
node->ri->onion_pkey,
|
2012-12-06 07:53:29 +01:00
|
|
|
node->ri->onion_curve25519_pkey,
|
2012-10-15 20:48:34 +02:00
|
|
|
&ap.addr,
|
|
|
|
ap.port);
|
2016-01-22 07:43:24 +01:00
|
|
|
else if (valid_addr && node->rs && node->md)
|
2012-10-15 20:48:34 +02:00
|
|
|
return extend_info_new(node->rs->nickname,
|
|
|
|
node->identity,
|
|
|
|
node->md->onion_pkey,
|
2012-12-06 07:53:29 +01:00
|
|
|
node->md->onion_curve25519_pkey,
|
2012-10-15 20:48:34 +02:00
|
|
|
&ap.addr,
|
|
|
|
ap.port);
|
|
|
|
else
|
|
|
|
return NULL;
|
2007-07-29 06:38:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Release storage held by an extend_info_t struct. */
|
|
|
|
void
|
|
|
|
extend_info_free(extend_info_t *info)
|
2007-07-29 06:38:21 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
crypto_pk_free(info->onion_key);
|
|
|
|
tor_free(info);
|
2007-07-29 06:38:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Allocate and return a new extend_info_t with the same contents as
|
|
|
|
* <b>info</b>. */
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_dup(extend_info_t *info)
|
2007-07-29 06:38:21 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
extend_info_t *newinfo;
|
|
|
|
tor_assert(info);
|
|
|
|
newinfo = tor_malloc(sizeof(extend_info_t));
|
|
|
|
memcpy(newinfo, info, sizeof(extend_info_t));
|
|
|
|
if (info->onion_key)
|
|
|
|
newinfo->onion_key = crypto_pk_dup_key(info->onion_key);
|
|
|
|
else
|
|
|
|
newinfo->onion_key = NULL;
|
|
|
|
return newinfo;
|
2007-06-12 11:17:23 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return the routerinfo_t for the chosen exit router in <b>state</b>.
|
|
|
|
* If there is no chosen exit, or if we don't know the routerinfo_t for
|
|
|
|
* the chosen exit, return NULL.
|
|
|
|
*/
|
|
|
|
const node_t *
|
|
|
|
build_state_get_exit_node(cpath_build_state_t *state)
|
2012-01-26 00:54:59 +01:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!state || !state->chosen_exit)
|
|
|
|
return NULL;
|
|
|
|
return node_get_by_id(state->chosen_exit->identity_digest);
|
2012-01-26 00:54:59 +01:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:34 +02:00
|
|
|
/** Return the nickname for the chosen exit router in <b>state</b>. If
|
|
|
|
* there is no chosen exit, or if we don't know the routerinfo_t for the
|
|
|
|
* chosen exit, return NULL.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
build_state_get_exit_nickname(cpath_build_state_t *state)
|
2007-07-17 13:33:38 +02:00
|
|
|
{
|
2012-10-15 20:48:34 +02:00
|
|
|
if (!state || !state->chosen_exit)
|
|
|
|
return NULL;
|
|
|
|
return state->chosen_exit->nickname;
|
2007-07-17 13:33:38 +02:00
|
|
|
}
|
|
|
|
|
2015-12-08 19:42:27 +01:00
|
|
|
/** Return true iff the given address can be used to extend to. */
|
2016-03-15 14:21:29 +01:00
|
|
|
int
|
|
|
|
extend_info_addr_is_allowed(const tor_addr_t *addr)
|
2015-12-08 19:42:27 +01:00
|
|
|
{
|
|
|
|
tor_assert(addr);
|
|
|
|
|
|
|
|
/* Check if we have a private address and if we can extend to it. */
|
2016-02-02 03:32:54 +01:00
|
|
|
if ((tor_addr_is_internal(addr, 0) || tor_addr_is_multicast(addr)) &&
|
2015-12-08 19:42:27 +01:00
|
|
|
!get_options()->ExtendAllowPrivateAddresses) {
|
|
|
|
goto disallow;
|
|
|
|
}
|
|
|
|
/* Allowed! */
|
|
|
|
return 1;
|
|
|
|
disallow:
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-15 14:21:29 +01:00
|
|
|
|
2016-07-08 06:46:00 +02:00
|
|
|
/* Does ei have a valid TAP key? */
|
|
|
|
int
|
|
|
|
extend_info_supports_tap(const extend_info_t* ei)
|
|
|
|
{
|
|
|
|
tor_assert(ei);
|
|
|
|
/* Valid TAP keys are not NULL */
|
|
|
|
return ei->onion_key != NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:04:26 +02:00
|
|
|
/* Does ei have a valid ntor key? */
|
|
|
|
int
|
|
|
|
extend_info_supports_ntor(const extend_info_t* ei)
|
|
|
|
{
|
|
|
|
tor_assert(ei);
|
|
|
|
/* Valid ntor keys have at least one non-zero byte */
|
|
|
|
return !tor_mem_is_zero(
|
|
|
|
(const char*)ei->curve25519_onion_key.public_key,
|
|
|
|
CURVE25519_PUBKEY_LEN);
|
|
|
|
}
|
2016-07-08 06:46:00 +02:00
|
|
|
|
|
|
|
/* Is circuit purpose allowed to use the deprecated TAP encryption protocol?
|
|
|
|
* The hidden service protocol still uses TAP for some connections, because
|
|
|
|
* ntor onion keys aren't included in HS descriptors or INTRODUCE cells. */
|
|
|
|
static int
|
|
|
|
circuit_purpose_can_use_tap_impl(uint8_t purpose)
|
|
|
|
{
|
|
|
|
return (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is circ allowed to use the deprecated TAP encryption protocol?
|
|
|
|
* The hidden service protocol still uses TAP for some connections, because
|
|
|
|
* ntor onion keys aren't included in HS descriptors or INTRODUCE cells. */
|
|
|
|
int
|
|
|
|
circuit_can_use_tap(const origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->cpath);
|
|
|
|
tor_assert(circ->cpath->extend_info);
|
|
|
|
return (circuit_purpose_can_use_tap_impl(circ->base_.purpose) &&
|
|
|
|
extend_info_supports_tap(circ->cpath->extend_info));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does circ have an onion key which it's allowed to use? */
|
|
|
|
int
|
|
|
|
circuit_has_usable_onion_key(const origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->cpath);
|
|
|
|
tor_assert(circ->cpath->extend_info);
|
|
|
|
return (extend_info_supports_ntor(circ->cpath->extend_info) ||
|
|
|
|
circuit_can_use_tap(circ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does ei have an onion key which it would prefer to use?
|
|
|
|
* Currently, we prefer ntor keys*/
|
|
|
|
int
|
|
|
|
extend_info_has_preferred_onion_key(const extend_info_t* ei)
|
|
|
|
{
|
|
|
|
tor_assert(ei);
|
|
|
|
return extend_info_supports_ntor(ei);
|
|
|
|
}
|
2016-09-05 19:47:39 +02:00
|
|
|
|