2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2001 Matej Pfajfar.
|
|
|
|
* Copyright 2001-2004 Roger Dingledine.
|
|
|
|
* Copyright 2004 Roger Dingledine, Nick Mathewson. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2004-11-29 23:25:31 +01:00
|
|
|
const char circuitbuild_c_id[] = "$Id$";
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuitbuild.c
|
|
|
|
* \brief The actual details of building circuits.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
|
|
|
/** A global list of all circuits at this hop. */
|
|
|
|
extern circuit_t *global_circuitlist;
|
|
|
|
|
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2004-07-03 01:08:59 +02:00
|
|
|
static int
|
|
|
|
circuit_deliver_create_cell(circuit_t *circ, char *payload);
|
2004-11-12 06:05:41 +01:00
|
|
|
static cpath_build_state_t *onion_new_cpath_build_state(uint8_t purpose,
|
|
|
|
const char *exit_digest);
|
|
|
|
static int onion_extend_cpath(crypt_path_t **head_ptr,
|
|
|
|
cpath_build_state_t *state, routerinfo_t **router_out);
|
2004-05-13 09:24:49 +02:00
|
|
|
static int count_acceptable_routers(smartlist_t *routers);
|
|
|
|
|
|
|
|
/** Iterate over values of circ_id, starting from conn-\>next_circ_id,
|
|
|
|
* and with the high bit specified by circ_id_type (see
|
|
|
|
* decide_circ_id_type()), until we get a circ_id that is not in use
|
|
|
|
* by any other circuit on that conn.
|
|
|
|
*
|
|
|
|
* Return it, or 0 if can't get a unique circ_id.
|
|
|
|
*/
|
2004-11-10 21:14:37 +01:00
|
|
|
static uint16_t get_unique_circ_id_by_conn(connection_t *conn) {
|
2004-05-13 09:24:49 +02:00
|
|
|
uint16_t test_circ_id;
|
|
|
|
int attempts=0;
|
|
|
|
uint16_t high_bit;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->type == CONN_TYPE_OR);
|
2004-11-10 21:14:37 +01:00
|
|
|
high_bit = (conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
do {
|
|
|
|
/* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
|
|
|
|
* circID such that (high_bit|test_circ_id) is not already used. */
|
|
|
|
test_circ_id = conn->next_circ_id++;
|
|
|
|
if (test_circ_id == 0 || test_circ_id >= 1<<15) {
|
|
|
|
test_circ_id = 1;
|
|
|
|
conn->next_circ_id = 2;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (++attempts > 1<<15) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* Make sure we don't loop forever if all circ_id's are used. This
|
|
|
|
* matters because it's an external DoS vulnerability.
|
|
|
|
*/
|
|
|
|
log_fn(LOG_WARN,"No unused circ IDs. Failing.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
test_circ_id |= high_bit;
|
2004-11-28 10:05:49 +01:00
|
|
|
} while (circuit_get_by_circ_id_conn(test_circ_id, conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
return test_circ_id;
|
|
|
|
}
|
|
|
|
|
2004-11-23 01:11:36 +01:00
|
|
|
/** If <b>verbose</b> is false, allocate and return a comma-separated
|
|
|
|
* list of the currently built elements of circuit_t. If
|
|
|
|
* <b>verbose</b> is true, also list information about link status in
|
|
|
|
* a more verbose format using spaces.
|
2004-11-03 19:33:07 +01:00
|
|
|
*/
|
2004-11-23 01:11:36 +01:00
|
|
|
char *
|
|
|
|
circuit_list_path(circuit_t *circ, int verbose)
|
2004-11-03 19:33:07 +01:00
|
|
|
{
|
|
|
|
struct crypt_path_t *hop;
|
|
|
|
smartlist_t *elements;
|
2004-11-23 01:11:36 +01:00
|
|
|
const char *states[] = {"closed", "waiting for keys", "open"};
|
|
|
|
char buf[128];
|
2004-11-03 19:33:07 +01:00
|
|
|
char *s;
|
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
|
|
|
tor_assert(circ->cpath);
|
|
|
|
|
|
|
|
elements = smartlist_create();
|
|
|
|
|
2004-11-23 01:11:36 +01:00
|
|
|
if (verbose) {
|
|
|
|
tor_snprintf(buf, sizeof(buf)-1, "circ (length %d, exit %s):",
|
|
|
|
circ->build_state->desired_path_len,
|
|
|
|
circ->build_state->chosen_exit_name);
|
|
|
|
smartlist_add(elements, tor_strdup(buf));
|
|
|
|
}
|
|
|
|
|
2004-11-23 07:08:08 +01:00
|
|
|
hop = circ->cpath;
|
|
|
|
do {
|
2004-11-23 01:11:36 +01:00
|
|
|
const char *elt;
|
|
|
|
routerinfo_t *r;
|
2004-11-23 07:08:08 +01:00
|
|
|
if (!hop)
|
|
|
|
break;
|
2004-11-23 01:11:36 +01:00
|
|
|
if (!verbose && hop->state != CPATH_STATE_OPEN)
|
2004-11-03 19:33:07 +01:00
|
|
|
break;
|
|
|
|
if ((r = router_get_by_digest(hop->identity_digest))) {
|
2004-11-23 01:11:36 +01:00
|
|
|
elt = r->nickname;
|
2004-11-03 19:33:07 +01:00
|
|
|
} else if (circ->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
|
2004-11-23 01:11:36 +01:00
|
|
|
elt = "<rendezvous splice>";
|
|
|
|
} else {
|
|
|
|
buf[0]='$';
|
|
|
|
base16_encode(buf+1,sizeof(buf)-1,hop->identity_digest,DIGEST_LEN);
|
|
|
|
elt = buf;
|
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
size_t len = strlen(elt)+2+strlen(states[hop->state])+1;
|
|
|
|
char *v = tor_malloc(len);
|
|
|
|
tor_assert(hop->state <= 2);
|
|
|
|
tor_snprintf(v,len,"%s(%s)",elt,states[hop->state]);
|
|
|
|
smartlist_add(elements, v);
|
2004-11-03 19:33:07 +01:00
|
|
|
} else {
|
2004-11-23 01:11:36 +01:00
|
|
|
smartlist_add(elements, tor_strdup(elt));
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2004-11-23 07:08:08 +01:00
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != circ->cpath);
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2004-11-23 01:11:36 +01:00
|
|
|
s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
|
2004-11-03 19:33:07 +01:00
|
|
|
SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Log, at severity <b>severity</b>, the nicknames of each router in
|
|
|
|
* circ's cpath. Also log the length of the cpath, and the intended
|
|
|
|
* exit point.
|
|
|
|
*/
|
|
|
|
void circuit_log_path(int severity, circuit_t *circ) {
|
2004-11-23 01:11:36 +01:00
|
|
|
char *s = circuit_list_path(circ,1);
|
|
|
|
log_fn(severity,"%s",s);
|
|
|
|
tor_free(s);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Tell the rep(utation)hist(ory) module about the status of the links
|
|
|
|
* in circ. 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.
|
|
|
|
*/
|
|
|
|
void circuit_rep_hist_note_result(circuit_t *circ) {
|
|
|
|
struct crypt_path_t *hop;
|
2004-07-13 20:23:40 +02:00
|
|
|
char *prev_digest = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
routerinfo_t *router;
|
|
|
|
hop = circ->cpath;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!hop) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* XXX
|
|
|
|
* if !hop, then we're not the beginning of this circuit.
|
|
|
|
* for now, just forget about it. later, we should remember when
|
|
|
|
* extends-through-us failed, too.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
if (server_mode(get_options())) {
|
2004-07-21 02:44:04 +02:00
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
tor_assert(me);
|
|
|
|
prev_digest = me->identity_digest;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
do {
|
2004-07-02 01:13:04 +02:00
|
|
|
router = router_get_by_digest(hop->identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
if (router) {
|
2004-07-13 20:23:40 +02:00
|
|
|
if (prev_digest) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (hop->state == CPATH_STATE_OPEN)
|
2004-07-13 20:23:40 +02:00
|
|
|
rep_hist_note_extend_succeeded(prev_digest, router->identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
else {
|
2004-07-13 20:23:40 +02:00
|
|
|
rep_hist_note_extend_failed(prev_digest, router->identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-07-13 20:23:40 +02:00
|
|
|
prev_digest = router->identity_digest;
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
2004-07-13 20:23:40 +02:00
|
|
|
prev_digest = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A helper function for circuit_dump_by_conn() below. Log a bunch
|
|
|
|
* of information about circuit <b>circ</b>.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_dump_details(int severity, circuit_t *circ, int poll_index,
|
2004-10-27 23:14:11 +02:00
|
|
|
const char *type, int this_circid, int other_circid) {
|
2004-12-04 03:51:11 +01:00
|
|
|
log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d:",
|
2004-05-13 09:24:49 +02:00
|
|
|
poll_index, type, this_circid, other_circid, circ->state,
|
|
|
|
circuit_state_to_string[circ->state], (int)circ->timestamp_created);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
|
2004-12-04 03:51:11 +01:00
|
|
|
circuit_log_path(severity, circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Log, at severity <b>severity</b>, information about each circuit
|
|
|
|
* that is connected to <b>conn</b>.
|
|
|
|
*/
|
|
|
|
void circuit_dump_by_conn(connection_t *conn, int severity) {
|
|
|
|
circuit_t *circ;
|
|
|
|
connection_t *tmpconn;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2004-12-04 03:51:11 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->p_conn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
|
|
|
|
circ->p_circ_id, circ->n_circ_id);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
|
|
|
if (tmpconn == conn) {
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
|
|
|
|
circ->p_circ_id, circ->n_circ_id);
|
|
|
|
}
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
|
|
|
|
circ->n_circ_id, circ->p_circ_id);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
|
|
|
if (tmpconn == conn) {
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
|
|
|
|
circ->n_circ_id, circ->p_circ_id);
|
|
|
|
}
|
|
|
|
}
|
2004-12-04 03:51:11 +01:00
|
|
|
if (!circ->n_conn &&
|
|
|
|
circ->n_addr == conn->addr &&
|
|
|
|
circ->n_port == conn->port &&
|
|
|
|
!memcmp(conn->identity_digest, circ->n_conn_id_digest, DIGEST_LEN)) {
|
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "Pending",
|
|
|
|
circ->n_circ_id, circ->p_circ_id);
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-03 01:40:03 +02:00
|
|
|
/** Build a new circuit for <b>purpose</b>. If <b>exit_digest</b>
|
2004-05-13 09:24:49 +02:00
|
|
|
* is defined, then use that as your exit router, else choose a suitable
|
|
|
|
* exit node.
|
|
|
|
*
|
|
|
|
* Also launch a connection to the first OR in the chosen path, if
|
|
|
|
* it's not open already.
|
|
|
|
*/
|
|
|
|
circuit_t *circuit_establish_circuit(uint8_t purpose,
|
2004-07-03 01:40:03 +02:00
|
|
|
const char *exit_digest) {
|
2004-05-13 09:24:49 +02:00
|
|
|
routerinfo_t *firsthop;
|
|
|
|
connection_t *n_conn;
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
|
|
|
|
circ->state = CIRCUIT_STATE_OR_WAIT;
|
2004-07-03 01:40:03 +02:00
|
|
|
circ->build_state = onion_new_cpath_build_state(purpose, exit_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->purpose = purpose;
|
|
|
|
|
|
|
|
if (! circ->build_state) {
|
|
|
|
log_fn(LOG_INFO,"Generating cpath failed.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop)<0 ||
|
2004-11-28 12:39:53 +01:00
|
|
|
!CIRCUIT_IS_ORIGIN(circ)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO,"Generating first cpath hop failed.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-03 19:33:07 +01:00
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_LAUNCHED);
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/* now see if we're already connected to the first OR in 'route' */
|
|
|
|
|
2004-08-17 14:09:37 +02:00
|
|
|
tor_assert(firsthop);
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
|
|
|
|
firsthop->address,firsthop->or_port);
|
2004-07-13 01:53:16 +02:00
|
|
|
/* imprint the circuit with its future n_conn->id */
|
|
|
|
memcpy(circ->n_conn_id_digest, firsthop->identity_digest, DIGEST_LEN);
|
2004-07-02 01:13:04 +02:00
|
|
|
n_conn = connection_get_by_identity_digest(firsthop->identity_digest,
|
|
|
|
CONN_TYPE_OR);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->n_addr = firsthop->addr;
|
|
|
|
circ->n_port = firsthop->or_port;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn) { /* launch the connection */
|
2004-07-02 11:29:01 +02:00
|
|
|
n_conn = connection_or_connect(firsthop->addr, firsthop->or_port,
|
|
|
|
firsthop->identity_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn) { /* connect failed, forget the whole thing */
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
|
|
|
|
/* return success. The onion/circuit/etc will be taken care of automatically
|
|
|
|
* (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
|
|
|
|
*/
|
|
|
|
return circ;
|
2004-08-18 22:35:11 +02:00
|
|
|
} else { /* it's already open. use it. */
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->n_addr = n_conn->addr;
|
|
|
|
circ->n_port = n_conn->port;
|
|
|
|
circ->n_conn = n_conn;
|
|
|
|
log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circuit_send_next_onion_skin(circ) < 0) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find circuits that are waiting on <b>or_conn</b> to become open,
|
|
|
|
* if any, and get them to send their create cells forward.
|
2004-11-21 08:43:12 +01:00
|
|
|
*
|
|
|
|
* Status is 1 if connect succeeded, or 0 if connect failed.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2004-11-21 08:43:12 +01:00
|
|
|
void circuit_n_conn_done(connection_t *or_conn, int status) {
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
|
2004-11-21 08:43:12 +01:00
|
|
|
log_fn(LOG_DEBUG,"or_conn to %s, status=%d", or_conn->nickname, status);
|
2004-07-13 01:53:16 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ->n_conn &&
|
2004-11-28 12:39:53 +01:00
|
|
|
circ->n_addr == or_conn->addr &&
|
|
|
|
circ->n_port == or_conn->port &&
|
|
|
|
!memcmp(or_conn->identity_digest, circ->n_conn_id_digest, DIGEST_LEN)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!status) { /* or_conn failed; close circ */
|
2004-07-02 11:29:01 +02:00
|
|
|
log_fn(LOG_INFO,"or_conn failed. Closing circ.");
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
continue;
|
2004-07-02 11:29:01 +02:00
|
|
|
}
|
|
|
|
log_fn(LOG_DEBUG,"Found circ %d, sending create cell.", circ->n_circ_id);
|
|
|
|
circ->n_conn = or_conn;
|
2004-07-03 03:37:59 +02:00
|
|
|
memcpy(circ->n_conn_id_digest, or_conn->identity_digest, DIGEST_LEN);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
if (circuit_send_next_onion_skin(circ) < 0) {
|
2004-07-02 11:29:01 +02:00
|
|
|
log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
continue;
|
|
|
|
/* XXX could this be bad, eg if next_onion_skin failed because conn died? */
|
|
|
|
}
|
|
|
|
} else {
|
2004-07-03 01:08:59 +02:00
|
|
|
/* pull the create cell out of circ->onionskin, and send it */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circuit_deliver_create_cell(circ, circ->onionskin) < 0) {
|
2004-07-03 01:08:59 +02:00
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
continue;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-03 01:08:59 +02:00
|
|
|
static int
|
|
|
|
circuit_deliver_create_cell(circuit_t *circ, char *payload) {
|
|
|
|
cell_t cell;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->n_conn);
|
|
|
|
tor_assert(circ->n_conn->type == CONN_TYPE_OR);
|
2004-07-03 01:08:59 +02:00
|
|
|
tor_assert(payload);
|
|
|
|
|
2004-11-10 21:14:37 +01:00
|
|
|
circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ->n_circ_id) {
|
2004-07-03 01:08:59 +02:00
|
|
|
log_fn(LOG_WARN,"failed to get unique circID.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
|
|
|
|
|
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
|
|
|
cell.command = CELL_CREATE;
|
|
|
|
cell.circ_id = circ->n_circ_id;
|
|
|
|
|
|
|
|
memcpy(cell.payload, payload, ONIONSKIN_CHALLENGE_LEN);
|
|
|
|
connection_or_write_cell_to_buf(&cell, circ->n_conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
extern int has_completed_circuit;
|
|
|
|
|
|
|
|
/** This is the backbone function for building circuits.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Return -1 if we want to tear down circ, else return 0.
|
|
|
|
*/
|
|
|
|
int circuit_send_next_onion_skin(circuit_t *circ) {
|
|
|
|
crypt_path_t *hop;
|
|
|
|
routerinfo_t *router;
|
|
|
|
int r;
|
2004-07-01 03:16:59 +02:00
|
|
|
char payload[2+4+DIGEST_LEN+ONIONSKIN_CHALLENGE_LEN];
|
|
|
|
char *onionskin;
|
2004-10-14 04:47:09 +02:00
|
|
|
size_t payload_len;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->cpath->state == CPATH_STATE_CLOSED) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_DEBUG,"First skin; sending create cell.");
|
|
|
|
|
2004-07-03 01:40:03 +02:00
|
|
|
router = router_get_by_digest(circ->n_conn->identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
if (!router) {
|
|
|
|
log_fn(LOG_WARN,"Couldn't find routerinfo for %s",
|
|
|
|
circ->n_conn->nickname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (onion_skin_create(router->onion_pkey,
|
2004-11-28 12:39:53 +01:00
|
|
|
&(circ->cpath->handshake_state),
|
|
|
|
payload) < 0) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circuit_deliver_create_cell(circ, payload) < 0)
|
2004-07-03 01:08:59 +02:00
|
|
|
return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
|
|
|
|
circ->state = CIRCUIT_STATE_BUILDING;
|
|
|
|
log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
|
|
|
|
} else {
|
|
|
|
tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
|
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_BUILDING);
|
|
|
|
log_fn(LOG_DEBUG,"starting to send subsequent skin.");
|
|
|
|
r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
|
|
|
|
if (r==1) {
|
|
|
|
/* done building the circuit. whew. */
|
|
|
|
circ->state = CIRCUIT_STATE_OPEN;
|
|
|
|
log_fn(LOG_INFO,"circuit built!");
|
2004-08-18 10:51:04 +02:00
|
|
|
circuit_reset_failure_count(0);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!has_completed_circuit) {
|
2004-05-13 09:24:49 +02:00
|
|
|
has_completed_circuit=1;
|
|
|
|
log_fn(LOG_NOTICE,"Tor has successfully opened a circuit. Looks like it's working.");
|
2004-11-16 04:32:01 +01:00
|
|
|
/* XXX009 Log a count of known routers here */
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
circuit_rep_hist_note_result(circ);
|
|
|
|
circuit_has_opened(circ); /* do other actions as necessary */
|
|
|
|
return 0;
|
|
|
|
} else if (r<0) {
|
|
|
|
log_fn(LOG_INFO,"Unable to extend circuit path.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
hop = circ->cpath->prev;
|
|
|
|
|
|
|
|
*(uint32_t*)payload = htonl(hop->addr);
|
|
|
|
*(uint16_t*)(payload+4) = htons(hop->port);
|
2004-07-22 05:47:20 +02:00
|
|
|
|
|
|
|
onionskin = payload+2+4;
|
|
|
|
memcpy(payload+2+4+ONIONSKIN_CHALLENGE_LEN, hop->identity_digest, DIGEST_LEN);
|
|
|
|
payload_len = 2+4+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN;
|
2004-07-01 03:16:59 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (onion_skin_create(router->onion_pkey, &(hop->handshake_state), onionskin) < 0) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_create failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG,"Sending extend relay cell.");
|
|
|
|
/* send it to hop->prev, because it will transfer
|
|
|
|
* it to a create cell and then send to hop */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTEND,
|
2004-11-28 12:39:53 +01:00
|
|
|
payload, payload_len, hop->prev) < 0)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0; /* circuit is closed */
|
|
|
|
|
|
|
|
hop->state = CPATH_STATE_AWAITING_KEYS;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Take the 'extend' cell, pull out addr/port plus the onion skin. Make
|
|
|
|
* sure we're connected to the next hop, and pass it the onion skin in
|
|
|
|
* a create cell.
|
|
|
|
*/
|
|
|
|
int circuit_extend(cell_t *cell, circuit_t *circ) {
|
|
|
|
connection_t *n_conn;
|
2004-07-01 03:16:59 +02:00
|
|
|
relay_header_t rh;
|
|
|
|
char *onionskin;
|
2004-07-03 01:08:59 +02:00
|
|
|
char *id_digest=NULL;
|
2004-08-18 13:20:15 +02:00
|
|
|
routerinfo_t *router;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
|
2004-10-17 23:51:20 +02:00
|
|
|
if (rh.length < 4+2+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN) {
|
2004-08-08 09:25:45 +02:00
|
|
|
log_fn(LOG_WARN, "Wrong length %d on extend cell. Closing circuit.", rh.length);
|
2004-07-03 01:08:59 +02:00
|
|
|
return -1;
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->n_addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
|
|
|
|
circ->n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
|
|
|
|
|
2004-10-17 23:51:20 +02:00
|
|
|
onionskin = cell->payload+RELAY_HEADER_SIZE+4+2;
|
|
|
|
id_digest = cell->payload+RELAY_HEADER_SIZE+4+2+ONIONSKIN_CHALLENGE_LEN;
|
|
|
|
n_conn = connection_get_by_identity_digest(id_digest, CONN_TYPE_OR);
|
2004-07-01 03:16:59 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn || n_conn->state != OR_CONN_STATE_OPEN) {
|
2004-07-02 11:29:01 +02:00
|
|
|
/* Note that this will close circuits where the onion has the same
|
2004-05-13 09:24:49 +02:00
|
|
|
* router twice in a row in the path. I think that's ok.
|
|
|
|
*/
|
|
|
|
struct in_addr in;
|
|
|
|
in.s_addr = htonl(circ->n_addr);
|
2004-07-02 11:29:01 +02:00
|
|
|
log_fn(LOG_INFO,"Next router (%s:%d) not connected. Connecting.",
|
|
|
|
inet_ntoa(in), circ->n_port);
|
|
|
|
|
2004-10-17 23:51:20 +02:00
|
|
|
router = router_get_by_digest(id_digest);
|
2004-07-02 11:29:01 +02:00
|
|
|
|
2004-07-03 01:08:59 +02:00
|
|
|
memcpy(circ->onionskin, onionskin, ONIONSKIN_CHALLENGE_LEN);
|
|
|
|
circ->state = CIRCUIT_STATE_OR_WAIT;
|
2004-07-13 01:53:16 +02:00
|
|
|
|
|
|
|
/* imprint the circuit with its future n_conn->id */
|
|
|
|
memcpy(circ->n_conn_id_digest, id_digest, DIGEST_LEN);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (n_conn) {
|
2004-08-08 12:32:36 +02:00
|
|
|
circ->n_addr = n_conn->addr;
|
|
|
|
circ->n_port = n_conn->port;
|
|
|
|
} else {
|
|
|
|
/* we should try to open a connection */
|
|
|
|
n_conn = connection_or_connect(circ->n_addr, circ->n_port, id_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn) {
|
2004-08-08 12:32:36 +02:00
|
|
|
log_fn(LOG_INFO,"Launching n_conn failed. Closing.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
|
2004-07-03 01:08:59 +02:00
|
|
|
}
|
|
|
|
/* return success. The onion/circuit/etc will be taken care of automatically
|
|
|
|
* (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
|
2004-07-02 11:29:01 +02:00
|
|
|
*/
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-08 12:32:36 +02:00
|
|
|
/* these may be different if the router connected to us from elsewhere */
|
|
|
|
circ->n_addr = n_conn->addr;
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->n_port = n_conn->port;
|
|
|
|
|
|
|
|
circ->n_conn = n_conn;
|
2004-07-03 03:37:59 +02:00
|
|
|
memcpy(circ->n_conn_id_digest, n_conn->identity_digest, DIGEST_LEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circuit_deliver_create_cell(circ, onionskin) < 0)
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
*
|
|
|
|
* (If 'reverse' is true, then f_XX and b_XX are swapped.)
|
|
|
|
*/
|
|
|
|
int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse)
|
|
|
|
{
|
|
|
|
crypto_digest_env_t *tmp_digest;
|
|
|
|
crypto_cipher_env_t *tmp_crypto;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(cpath);
|
|
|
|
tor_assert(key_data);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
|
|
|
|
cpath->f_digest || cpath->b_digest));
|
|
|
|
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"hop init digest forward 0x%.8x, backward 0x%.8x.",
|
|
|
|
// (unsigned int)*(uint32_t*)key_data, (unsigned int)*(uint32_t*)(key_data+20));
|
2004-05-13 09:24:49 +02:00
|
|
|
cpath->f_digest = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
|
|
|
|
cpath->b_digest = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
|
|
|
|
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"hop init cipher forward 0x%.8x, backward 0x%.8x.",
|
|
|
|
// (unsigned int)*(uint32_t*)(key_data+40), (unsigned int)*(uint32_t*)(key_data+40+16));
|
2004-05-13 09:24:49 +02:00
|
|
|
if (!(cpath->f_crypto =
|
|
|
|
crypto_create_init_cipher(key_data+(2*DIGEST_LEN),1))) {
|
|
|
|
log(LOG_WARN,"forward cipher initialization failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(cpath->b_crypto =
|
2004-11-28 12:39:53 +01:00
|
|
|
crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,0))) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log(LOG_WARN,"backward cipher initialization failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A created or extended cell came back to us on the circuit,
|
|
|
|
* and it included <b>reply</b> (the second DH key, plus KH).
|
|
|
|
*
|
|
|
|
* Calculate the appropriate keys and digests, make sure KH is
|
|
|
|
* correct, and initialize this hop of the cpath.
|
|
|
|
*
|
|
|
|
* Return -1 if we want to mark circ for close, else return 0.
|
|
|
|
*/
|
|
|
|
int circuit_finish_handshake(circuit_t *circ, char *reply) {
|
|
|
|
unsigned char keys[CPATH_KEY_MATERIAL_LEN];
|
|
|
|
crypt_path_t *hop;
|
|
|
|
|
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
|
2004-05-13 09:24:49 +02:00
|
|
|
hop = circ->cpath;
|
|
|
|
else {
|
2004-11-28 10:05:49 +01:00
|
|
|
for (hop=circ->cpath->next;
|
2004-05-13 09:24:49 +02:00
|
|
|
hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
|
|
|
|
hop=hop->next) ;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (hop == circ->cpath) { /* got an extended when we're all done? */
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_WARN,"got extended when circ already built? Closing.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (onion_skin_client_handshake(hop->handshake_state, reply, keys,
|
2004-11-28 12:39:53 +01:00
|
|
|
DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
crypto_dh_free(hop->handshake_state); /* don't need it anymore */
|
|
|
|
hop->handshake_state = NULL;
|
|
|
|
/* Remember hash of g^xy */
|
|
|
|
memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
|
|
|
|
|
|
|
|
if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hop->state = CPATH_STATE_OPEN;
|
2004-11-23 01:11:36 +01:00
|
|
|
log_fn(LOG_INFO,"Finished building circuit:");
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_log_path(LOG_INFO,circ);
|
2004-11-03 19:33:07 +01:00
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_EXTENDED);
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** We received a relay truncated cell on circ.
|
|
|
|
*
|
|
|
|
* Since we don't ask for truncates currently, getting a truncated
|
|
|
|
* means that a connection broke or an extend failed. For now,
|
|
|
|
* just give up: for circ to close, and return 0.
|
|
|
|
*/
|
|
|
|
int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
|
2004-10-11 03:17:42 +02:00
|
|
|
// crypt_path_t *victim;
|
|
|
|
// connection_t *stream;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(layer);
|
|
|
|
|
|
|
|
/* XXX Since we don't ask for truncates currently, getting a truncated
|
|
|
|
* means that a connection broke or an extend failed. For now,
|
|
|
|
* just give up.
|
|
|
|
*/
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return 0;
|
|
|
|
|
2004-10-11 03:17:42 +02:00
|
|
|
#if 0
|
2004-11-28 10:05:49 +01:00
|
|
|
while (layer->next != circ->cpath) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* we need to clear out layer->next */
|
|
|
|
victim = layer->next;
|
|
|
|
log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (stream = circ->p_streams; stream; stream=stream->next_stream) {
|
|
|
|
if (stream->cpath_layer == victim) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO, "Marking stream %d for close.", stream->stream_id);
|
|
|
|
/* no need to send 'end' relay cells,
|
|
|
|
* because the other side's already dead
|
|
|
|
*/
|
|
|
|
stream->has_sent_end = 1;
|
|
|
|
connection_mark_for_close(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layer->next = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
|
|
|
|
|
|
|
log_fn(LOG_INFO, "finished");
|
|
|
|
return 0;
|
2004-10-11 03:17:42 +02:00
|
|
|
#endif
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a response payload and keys, initialize, then send a created
|
|
|
|
* cell back.
|
|
|
|
*/
|
|
|
|
int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys) {
|
|
|
|
cell_t cell;
|
|
|
|
crypt_path_t *tmp_cpath;
|
|
|
|
|
|
|
|
tmp_cpath = tor_malloc_zero(sizeof(crypt_path_t));
|
|
|
|
|
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
|
|
|
cell.command = CELL_CREATED;
|
|
|
|
cell.circ_id = circ->p_circ_id;
|
|
|
|
|
|
|
|
circ->state = CIRCUIT_STATE_OPEN;
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG,"Entering.");
|
|
|
|
|
|
|
|
memcpy(cell.payload, payload, ONIONSKIN_REPLY_LEN);
|
|
|
|
|
|
|
|
log_fn(LOG_INFO,"init digest forward 0x%.8x, backward 0x%.8x.",
|
|
|
|
(unsigned int)*(uint32_t*)(keys), (unsigned int)*(uint32_t*)(keys+20));
|
|
|
|
if (circuit_init_cpath_crypto(tmp_cpath, keys, 0)<0) {
|
|
|
|
log_fn(LOG_WARN,"Circuit initialization failed");
|
|
|
|
tor_free(tmp_cpath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
tor_free(tmp_cpath);
|
|
|
|
|
|
|
|
memcpy(circ->handshake_digest, cell.payload+DH_KEY_LEN, DIGEST_LEN);
|
|
|
|
|
|
|
|
connection_or_write_cell_to_buf(&cell, circ->p_conn);
|
|
|
|
log_fn(LOG_DEBUG,"Finished sending 'created' cell.");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Choose a length for a circuit of purpose <b>purpose</b>.
|
|
|
|
* Default length is 3 + the number of endpoints that would give something
|
|
|
|
* away. If the routerlist <b>routers</b> doesn't have enough routers
|
|
|
|
* to handle the desired path length, return as large a path length as
|
|
|
|
* is feasible, except if it's less than 2, in which case return -1.
|
|
|
|
*/
|
|
|
|
static int new_route_len(double cw, uint8_t purpose, smartlist_t *routers) {
|
|
|
|
int num_acceptable_routers;
|
|
|
|
int routelen;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(cw >= 0.);
|
|
|
|
tor_assert(cw < 1.);
|
|
|
|
tor_assert(routers);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
#ifdef TOR_PERF
|
|
|
|
routelen = 2;
|
|
|
|
#else
|
2004-11-28 10:05:49 +01:00
|
|
|
if (purpose == CIRCUIT_PURPOSE_C_GENERAL)
|
2004-05-13 09:24:49 +02:00
|
|
|
routelen = 3;
|
2004-11-28 10:05:49 +01:00
|
|
|
else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCING)
|
2004-05-13 09:24:49 +02:00
|
|
|
routelen = 4;
|
2004-11-28 10:05:49 +01:00
|
|
|
else if (purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND)
|
2004-05-13 09:24:49 +02:00
|
|
|
routelen = 3;
|
2004-11-28 10:05:49 +01:00
|
|
|
else if (purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
|
2004-05-13 09:24:49 +02:00
|
|
|
routelen = 3;
|
2004-11-28 10:05:49 +01:00
|
|
|
else if (purpose == CIRCUIT_PURPOSE_S_CONNECT_REND)
|
2004-05-13 09:24:49 +02:00
|
|
|
routelen = 4;
|
|
|
|
else {
|
|
|
|
log_fn(LOG_WARN,"Unhandled purpose %d", purpose);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if 0
|
2004-11-28 10:05:49 +01:00
|
|
|
for (routelen = 3; ; routelen++) { /* 3, increment until coinflip says we're done */
|
2004-05-13 09:24:49 +02:00
|
|
|
if (crypto_pseudo_rand_int(255) >= cw*255) /* don't extend */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
log_fn(LOG_DEBUG,"Chosen route length %d (%d routers available).",routelen,
|
|
|
|
smartlist_len(routers));
|
|
|
|
|
|
|
|
num_acceptable_routers = count_acceptable_routers(routers);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_acceptable_routers < 2) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO,"Not enough acceptable routers (%d). Discarding this circuit.",
|
|
|
|
num_acceptable_routers);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_acceptable_routers < routelen) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO,"Not enough routers: cutting routelen from %d to %d.",
|
|
|
|
routelen, num_acceptable_routers);
|
|
|
|
routelen = num_acceptable_routers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return routelen;
|
|
|
|
}
|
|
|
|
|
2004-12-05 08:10:08 +01:00
|
|
|
/** Fetch the list of predicted ports, turn it into a smartlist of
|
|
|
|
* strings, remove the ones that are already handled by an
|
|
|
|
* existing circuit, and return it.
|
|
|
|
*/
|
|
|
|
static smartlist_t *
|
|
|
|
circuit_get_unhandled_ports(time_t now) {
|
|
|
|
char *pp = rep_hist_get_predicted_ports(now);
|
|
|
|
smartlist_t *needed_ports = smartlist_create();
|
|
|
|
smartlist_split_string(needed_ports, pp, " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
tor_free(pp);
|
|
|
|
|
|
|
|
circuit_remove_handled_ports(needed_ports);
|
|
|
|
return needed_ports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if we already have circuits present or on the way for
|
|
|
|
* all anticipated ports. Return 0 if we should make more.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
circuit_all_predicted_ports_handled(time_t now) {
|
|
|
|
int enough;
|
|
|
|
smartlist_t *sl = circuit_get_unhandled_ports(now);
|
|
|
|
enough = (smartlist_len(sl) == 0);
|
|
|
|
smartlist_free(sl);
|
|
|
|
return enough;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if <b>router</b> can handle one or more of the ports in
|
|
|
|
* <b>needed_ports</b>, else return 0.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
router_handles_some_port(routerinfo_t *router, smartlist_t *needed_ports) {
|
|
|
|
int i;
|
|
|
|
uint16_t port;
|
|
|
|
|
|
|
|
for (i = 0; i < smartlist_len(needed_ports); ++i) {
|
|
|
|
port = *(uint16_t *)smartlist_get(needed_ports, i);
|
|
|
|
tor_assert(port);
|
|
|
|
if (router_compare_addr_to_addr_policy(0, port, router->exit_policy) !=
|
|
|
|
ADDR_POLICY_REJECTED)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** How many circuits do we want simultaneously in-progress to handle
|
|
|
|
* a given stream?
|
|
|
|
*/
|
|
|
|
#define MIN_CIRCUITS_HANDLING_STREAM 2
|
|
|
|
|
2004-05-13 09:24:49 +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.
|
|
|
|
*
|
|
|
|
* Return NULL if we can't find any suitable routers.
|
|
|
|
*/
|
|
|
|
static routerinfo_t *choose_good_exit_server_general(routerlist_t *dir)
|
|
|
|
{
|
|
|
|
int *n_supported;
|
|
|
|
int i, j;
|
|
|
|
int n_pending_connections = 0;
|
|
|
|
connection_t **carray;
|
|
|
|
int n_connections;
|
|
|
|
int best_support = -1;
|
|
|
|
int n_best_support=0;
|
2004-08-17 09:09:02 +02:00
|
|
|
smartlist_t *sl, *preferredexits, *preferredentries, *excludedexits;
|
2004-05-13 09:24:49 +02:00
|
|
|
routerinfo_t *router;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-08-17 09:09:02 +02:00
|
|
|
preferredentries = smartlist_create();
|
2004-11-06 06:18:11 +01:00
|
|
|
add_nickname_list_to_smartlist(preferredentries,options->EntryNodes,1);
|
2004-08-17 09:09:02 +02:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
get_connection_array(&carray, &n_connections);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < n_connections; ++i) {
|
|
|
|
if (carray[i]->type == CONN_TYPE_AP &&
|
|
|
|
carray[i]->state == AP_CONN_STATE_CIRCUIT_WAIT &&
|
|
|
|
!carray[i]->marked_for_close &&
|
2004-12-05 08:10:08 +01:00
|
|
|
!circuit_stream_is_being_handled(carray[i], 0, MIN_CIRCUITS_HANDLING_STREAM))
|
2004-05-13 09:24:49 +02:00
|
|
|
++n_pending_connections;
|
|
|
|
}
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
|
|
|
|
// n_pending_connections);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* 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.)
|
|
|
|
*/
|
|
|
|
n_supported = tor_malloc(sizeof(int)*smartlist_len(dir->routers));
|
|
|
|
for (i = 0; i < smartlist_len(dir->routers); ++i) { /* iterate over routers */
|
|
|
|
router = smartlist_get(dir->routers, i);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_is_me(router)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* XXX there's probably a reverse predecessor attack here, but
|
|
|
|
* it's slow. should we take this out? -RD
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!router->is_running) {
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- directory says it's not running.",
|
|
|
|
// router->nickname, i);
|
2004-05-13 09:24:49 +02:00
|
|
|
continue; /* skip routers that are known to be down */
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!router->is_verified &&
|
2004-11-28 12:39:53 +01:00
|
|
|
(!(options->_AllowUnverified & ALLOW_UNVERIFIED_EXIT) ||
|
2004-08-20 23:34:36 +02:00
|
|
|
router_is_unreliable_router(router, 1, 1))) {
|
|
|
|
/* if it's unverified, and either we don't want it or it's unsuitable */
|
2004-07-21 11:35:24 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- unverified router.",
|
|
|
|
// router->nickname, i);
|
2004-07-21 11:35:24 +02:00
|
|
|
continue; /* skip unverified routers */
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_exit_policy_rejects_all(router)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
|
|
|
|
// router->nickname, i);
|
2004-05-13 09:24:49 +02:00
|
|
|
continue; /* skip routers that reject all */
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (smartlist_len(preferredentries)==1 &&
|
2004-11-28 12:39:53 +01:00
|
|
|
router == (routerinfo_t*)smartlist_get(preferredentries, 0)) {
|
2004-08-17 09:09:02 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it's our only preferred entry node.", router->nickname, i);
|
2004-08-17 09:09:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = 0;
|
|
|
|
for (j = 0; j < n_connections; ++j) { /* iterate over connections */
|
|
|
|
if (carray[j]->type != CONN_TYPE_AP ||
|
|
|
|
carray[j]->state != AP_CONN_STATE_CIRCUIT_WAIT ||
|
|
|
|
carray[j]->marked_for_close ||
|
2004-12-05 08:10:08 +01:00
|
|
|
circuit_stream_is_being_handled(carray[j], 0, MIN_CIRCUITS_HANDLING_STREAM))
|
2004-05-13 09:24:49 +02:00
|
|
|
continue; /* Skip everything but APs in CIRCUIT_WAIT */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (connection_ap_can_use_exit(carray[j], router)) {
|
2004-07-21 05:16:24 +02:00
|
|
|
++n_supported[i];
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
|
|
|
|
// router->nickname, i, n_supported[i]);
|
2004-07-21 05:16:24 +02:00
|
|
|
} else {
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
|
|
|
|
// router->nickname, i);
|
2004-07-21 05:16:24 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
} /* End looping over connections. */
|
|
|
|
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;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"%s is new best supported option so far.",
|
|
|
|
// router->nickname);
|
2004-05-13 09:24:49 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_fn(LOG_INFO, "Found %d servers that might support %d/%d pending connections.",
|
|
|
|
n_best_support, best_support, n_pending_connections);
|
|
|
|
|
|
|
|
preferredexits = smartlist_create();
|
2004-11-06 06:18:11 +01:00
|
|
|
add_nickname_list_to_smartlist(preferredexits,options->ExitNodes,1);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
excludedexits = smartlist_create();
|
2004-11-06 06:18:11 +01:00
|
|
|
add_nickname_list_to_smartlist(excludedexits,options->ExcludeNodes,0);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
sl = smartlist_create();
|
|
|
|
|
|
|
|
/* If any routers definitely support any pending connections, choose one
|
|
|
|
* at random. */
|
|
|
|
if (best_support > 0) {
|
|
|
|
for (i = 0; i < smartlist_len(dir->routers); i++)
|
|
|
|
if (n_supported[i] == best_support)
|
|
|
|
smartlist_add(sl, smartlist_get(dir->routers, i));
|
|
|
|
|
|
|
|
smartlist_subtract(sl,excludedexits);
|
2004-11-06 06:18:11 +01:00
|
|
|
if (options->StrictExitNodes || smartlist_overlap(sl,preferredexits))
|
2004-05-13 09:24:49 +02:00
|
|
|
smartlist_intersect(sl,preferredexits);
|
2004-08-18 08:10:12 +02:00
|
|
|
router = routerlist_sl_choose_by_bandwidth(sl);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
|
|
|
/* Either there are no pending connections, or no routers even seem to
|
2004-12-05 08:10:08 +01:00
|
|
|
* possibly support any of them. Choose a router at random that satisfies
|
|
|
|
* at least one predicted exit port. */
|
|
|
|
|
|
|
|
int try;
|
|
|
|
smartlist_t *needed_ports = circuit_get_unhandled_ports(time(NULL));
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
if (best_support == -1) {
|
|
|
|
log(LOG_WARN, "All routers are down or middleman -- choosing a doomed exit at random.");
|
|
|
|
}
|
2004-12-05 08:10:08 +01:00
|
|
|
for (try = 0; try < 2; try++) {
|
|
|
|
/* try once to pick only from routers that satisfy a needed port,
|
|
|
|
* then if there are none, pick from any that support exiting. */
|
|
|
|
for (i = 0; i < smartlist_len(dir->routers); i++) {
|
|
|
|
router = smartlist_get(dir->routers, i);
|
|
|
|
if (n_supported[i] != -1 &&
|
|
|
|
(try || router_handles_some_port(router, needed_ports))) {
|
|
|
|
log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.", try, router->nickname);
|
|
|
|
smartlist_add(sl, router);
|
|
|
|
}
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_subtract(sl,excludedexits);
|
|
|
|
if (options->StrictExitNodes || smartlist_overlap(sl,preferredexits))
|
|
|
|
smartlist_intersect(sl,preferredexits);
|
|
|
|
router = routerlist_sl_choose_by_bandwidth(sl);
|
|
|
|
if (router)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
smartlist_free(needed_ports);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(preferredexits);
|
2004-08-17 09:09:02 +02:00
|
|
|
smartlist_free(preferredentries);
|
2004-05-13 09:24:49 +02:00
|
|
|
smartlist_free(excludedexits);
|
|
|
|
smartlist_free(sl);
|
|
|
|
tor_free(n_supported);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO, "Chose exit server '%s'", router->nickname);
|
|
|
|
return router;
|
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
if (options->StrictExitNodes)
|
2004-08-15 22:14:44 +02:00
|
|
|
log_fn(LOG_WARN, "No exit routers seem to be running; can't choose an exit.");
|
2004-08-17 09:09:02 +02:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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).
|
|
|
|
*
|
|
|
|
* For general-purpose circuits, pass it off to
|
|
|
|
* choose_good_exit_server_general()
|
|
|
|
*
|
|
|
|
* For client-side rendezvous circuits, choose a random node, weighted
|
|
|
|
* toward the preferences in 'options'.
|
|
|
|
*/
|
|
|
|
static routerinfo_t *choose_good_exit_server(uint8_t purpose, routerlist_t *dir)
|
|
|
|
{
|
|
|
|
routerinfo_t *r;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (purpose) {
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
return choose_good_exit_server_general(dir);
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
2004-11-06 06:18:11 +01:00
|
|
|
r = router_choose_random_node(options->RendNodes, options->RendExcludeNodes,
|
|
|
|
NULL, 0, 1, options->_AllowUnverified & ALLOW_UNVERIFIED_RENDEZVOUS, 0);
|
2004-05-13 09:24:49 +02:00
|
|
|
return r;
|
|
|
|
}
|
2004-10-17 00:56:46 +02:00
|
|
|
log_fn(LOG_WARN,"Unhandled purpose %d", purpose);
|
|
|
|
return NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocate a cpath_build_state_t, populate it based on
|
2004-07-21 04:25:14 +02:00
|
|
|
* <b>purpose</b> and <b>exit_digest</b> (if specified), and
|
2004-05-13 09:24:49 +02:00
|
|
|
* return it.
|
|
|
|
*/
|
|
|
|
static cpath_build_state_t *
|
2004-07-03 01:40:03 +02:00
|
|
|
onion_new_cpath_build_state(uint8_t purpose, const char *exit_digest)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
routerlist_t *rl;
|
|
|
|
int r;
|
|
|
|
cpath_build_state_t *info;
|
|
|
|
routerinfo_t *exit;
|
2004-11-06 06:18:11 +01:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
router_get_routerlist(&rl);
|
2004-10-14 05:44:45 +02:00
|
|
|
if (!rl)
|
|
|
|
return NULL;
|
2004-11-06 06:18:11 +01:00
|
|
|
r = new_route_len(get_options()->PathlenCoinWeight, purpose, rl->routers);
|
2004-08-17 14:09:37 +02:00
|
|
|
if (r < 1) /* must be at least 1 */
|
2004-05-13 09:24:49 +02:00
|
|
|
return NULL;
|
|
|
|
info = tor_malloc_zero(sizeof(cpath_build_state_t));
|
|
|
|
info->desired_path_len = r;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (exit_digest) { /* the circuit-builder pre-requested one */
|
2004-07-03 01:40:03 +02:00
|
|
|
memcpy(info->chosen_exit_digest, exit_digest, DIGEST_LEN);
|
|
|
|
exit = router_get_by_digest(exit_digest);
|
|
|
|
if (exit) {
|
|
|
|
info->chosen_exit_name = tor_strdup(exit->nickname);
|
|
|
|
} else {
|
|
|
|
info->chosen_exit_name = tor_malloc(HEX_DIGEST_LEN+1);
|
|
|
|
base16_encode(info->chosen_exit_name, HEX_DIGEST_LEN+1,
|
|
|
|
exit_digest, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
log_fn(LOG_INFO,"Using requested exit node '%s'", info->chosen_exit_name);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else { /* we have to decide one */
|
|
|
|
exit = choose_good_exit_server(purpose, rl);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!exit) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_WARN,"failed to choose an exit server");
|
|
|
|
tor_free(info);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-07-03 01:40:03 +02:00
|
|
|
memcpy(info->chosen_exit_digest, exit->identity_digest, DIGEST_LEN);
|
|
|
|
info->chosen_exit_name = tor_strdup(exit->nickname);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the number of routers in <b>routers</b> that are currently up
|
2004-08-18 22:35:11 +02:00
|
|
|
* and available for building circuits through.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
static int count_acceptable_routers(smartlist_t *routers) {
|
2004-08-18 22:35:11 +02:00
|
|
|
int i, n;
|
2004-05-13 09:24:49 +02:00
|
|
|
int num=0;
|
2004-08-18 22:35:11 +02:00
|
|
|
routerinfo_t *r;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
n = smartlist_len(routers);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0;i<n;i++) {
|
2004-05-13 09:24:49 +02:00
|
|
|
r = smartlist_get(routers, i);
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Contemplating whether router %d (%s) is a new option...",
|
|
|
|
// i, r->nickname);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (r->is_running == 0) {
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Nope, the directory says %d is not running.",i);
|
2004-05-13 09:24:49 +02:00
|
|
|
goto next_i_loop;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (r->is_verified == 0) {
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Nope, the directory says %d is not verified.",i);
|
2004-11-10 15:27:26 +01:00
|
|
|
/* XXXX009 But unverified routers *are* sometimes acceptable. */
|
|
|
|
goto next_i_loop;
|
2004-07-21 11:35:24 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
num++;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"I like %d. num_acceptable_routers now %d.",i, num);
|
2004-05-13 09:24:49 +02:00
|
|
|
next_i_loop:
|
|
|
|
; /* C requires an explicit statement after the label */
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-15 10:15:12 +02:00
|
|
|
static routerinfo_t *choose_good_middle_server(cpath_build_state_t *state,
|
|
|
|
crypt_path_t *head,
|
|
|
|
int cur_len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
routerinfo_t *r, *choice;
|
|
|
|
crypt_path_t *cpath;
|
|
|
|
smartlist_t *excluded = smartlist_create();
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG, "Contemplating intermediate hop: random choice.");
|
|
|
|
excluded = smartlist_create();
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((r = router_get_by_digest(state->chosen_exit_digest))) {
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((r = routerlist_find_my_routerinfo())) {
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
for (i = 0, cpath = head; i < cur_len; ++i, cpath=cpath->next) {
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((r = router_get_by_digest(cpath->identity_digest))) {
|
2004-09-30 17:40:05 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-30 17:40:05 +02:00
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
choice = router_choose_random_node(NULL, get_options()->ExcludeNodes, excluded,
|
|
|
|
0, 1, get_options()->_AllowUnverified & ALLOW_UNVERIFIED_MIDDLE, 0);
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_free(excluded);
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
|
|
|
static routerinfo_t *choose_good_entry_server(cpath_build_state_t *state)
|
|
|
|
{
|
|
|
|
routerinfo_t *r, *choice;
|
|
|
|
smartlist_t *excluded = smartlist_create();
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-08-16 22:47:00 +02:00
|
|
|
char buf[16];
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((r = router_get_by_digest(state->chosen_exit_digest))) {
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((r = routerlist_find_my_routerinfo())) {
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (options->FascistFirewall) {
|
2004-08-15 10:15:12 +02:00
|
|
|
/* exclude all ORs that listen on the wrong port */
|
|
|
|
routerlist_t *rl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
router_get_routerlist(&rl);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!rl)
|
2004-08-15 10:15:12 +02:00
|
|
|
return NULL;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0; i < smartlist_len(rl->routers); i++) {
|
2004-08-15 10:15:12 +02:00
|
|
|
r = smartlist_get(rl->routers, i);
|
2004-10-27 08:37:34 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%d", r->or_port);
|
2004-11-06 06:18:11 +01:00
|
|
|
if (!smartlist_string_isin(options->FirewallPorts, buf))
|
2004-08-16 22:47:00 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
choice = router_choose_random_node(options->EntryNodes, options->ExcludeNodes,
|
|
|
|
excluded, 0, 1, options->_AllowUnverified & ALLOW_UNVERIFIED_ENTRY,
|
|
|
|
options->StrictEntryNodes);
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_free(excluded);
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Choose a suitable next hop in the cpath <b>head_ptr</b>,
|
|
|
|
* based on <b>state</b>. Add the hop info to head_ptr, and return a
|
|
|
|
* pointer to the chosen router in <b>router_out</b>.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t
|
|
|
|
*state, routerinfo_t **router_out)
|
|
|
|
{
|
|
|
|
int cur_len;
|
|
|
|
crypt_path_t *cpath, *hop;
|
|
|
|
routerinfo_t *choice;
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_t *excludednodes;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(head_ptr);
|
|
|
|
tor_assert(router_out);
|
|
|
|
|
|
|
|
if (!*head_ptr) {
|
|
|
|
cur_len = 0;
|
|
|
|
} else {
|
|
|
|
cur_len = 1;
|
|
|
|
for (cpath = *head_ptr; cpath->next != *head_ptr; cpath = cpath->next) {
|
|
|
|
++cur_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cur_len >= state->desired_path_len) {
|
|
|
|
log_fn(LOG_DEBUG, "Path is complete: %d steps long",
|
|
|
|
state->desired_path_len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
log_fn(LOG_DEBUG, "Path is %d long; we want %d", cur_len,
|
|
|
|
state->desired_path_len);
|
|
|
|
|
|
|
|
excludednodes = smartlist_create();
|
2004-11-06 06:18:11 +01:00
|
|
|
add_nickname_list_to_smartlist(excludednodes,get_options()->ExcludeNodes,0);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cur_len == state->desired_path_len - 1) { /* Picking last node */
|
2004-07-03 01:40:03 +02:00
|
|
|
choice = router_get_by_digest(state->chosen_exit_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
} else if (cur_len == 0) { /* picking first node */
|
2004-08-15 10:15:12 +02:00
|
|
|
choice = choose_good_entry_server(state);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
2004-08-15 10:15:12 +02:00
|
|
|
choice = choose_good_middle_server(state, *head_ptr, cur_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(excludednodes);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!choice) {
|
2004-08-15 10:15:12 +02:00
|
|
|
log_fn(LOG_WARN,"Failed to find node for hop %d of our path. Discarding this circuit.", cur_len);
|
|
|
|
return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG,"Chose router %s for hop %d (exit is %s)",
|
2004-10-30 07:04:52 +02:00
|
|
|
choice->nickname, cur_len+1, state->chosen_exit_name);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
hop = tor_malloc_zero(sizeof(crypt_path_t));
|
|
|
|
|
|
|
|
/* link hop into the cpath, at the end. */
|
|
|
|
onion_append_to_cpath(head_ptr, hop);
|
|
|
|
|
|
|
|
hop->state = CPATH_STATE_CLOSED;
|
|
|
|
|
|
|
|
hop->port = choice->or_port;
|
|
|
|
hop->addr = choice->addr;
|
2004-07-01 03:16:59 +02:00
|
|
|
memcpy(hop->identity_digest, choice->identity_digest, DIGEST_LEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
hop->package_window = CIRCWINDOW_START;
|
|
|
|
hop->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG, "Extended circuit path with %s for hop %d",
|
2004-10-30 07:04:52 +02:00
|
|
|
choice->nickname, cur_len+1);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
*router_out = choice;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|