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.
|
2011-01-03 17:50:39 +01:00
|
|
|
* Copyright (c) 2007-2011, The Tor Project, Inc. */
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
/* See LICENSE for licensing information */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file connection_or.c
|
|
|
|
* \brief Functions to handle OR connections, TLS handshaking, and
|
|
|
|
* cells on the network.
|
|
|
|
**/
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
#include "or.h"
|
2010-07-22 00:46:18 +02:00
|
|
|
#include "buffers.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2011-06-22 19:57:19 +02:00
|
|
|
#include "circuitlist.h"
|
2010-07-22 10:08:32 +02:00
|
|
|
#include "command.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:50:34 +02:00
|
|
|
#include "connection_or.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-22 12:09:49 +02:00
|
|
|
#include "dirserv.h"
|
2010-07-21 14:38:52 +02:00
|
|
|
#include "geoip.h"
|
2010-07-23 19:58:06 +02:00
|
|
|
#include "main.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 21:08:30 +02:00
|
|
|
#include "reasons.h"
|
2010-07-23 21:53:11 +02:00
|
|
|
#include "relay.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"
|
2003-06-21 21:29:32 +02:00
|
|
|
|
2009-08-14 20:34:16 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
#include <event2/bufferevent_ssl.h>
|
|
|
|
#endif
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
static int connection_tls_finish_handshake(or_connection_t *conn);
|
2011-09-27 19:15:36 +02:00
|
|
|
static int connection_or_launch_v3_or_handshake(or_connection_t *conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
static int connection_or_process_cells_from_inbuf(or_connection_t *conn);
|
2007-12-01 09:47:13 +01:00
|
|
|
static int connection_or_check_valid_tls_handshake(or_connection_t *conn,
|
|
|
|
int started_here,
|
|
|
|
char *digest_rcvd_out);
|
2003-09-30 20:45:55 +02:00
|
|
|
|
2010-10-11 19:45:27 +02:00
|
|
|
static void connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn);
|
|
|
|
|
2009-08-14 20:34:16 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
static void connection_or_handle_event_cb(struct bufferevent *bufev,
|
|
|
|
short event, void *arg);
|
|
|
|
#include <event2/buffer.h>/*XXXX REMOVE */
|
|
|
|
#endif
|
|
|
|
|
2003-09-16 07:41:49 +02:00
|
|
|
/**************************************************************/
|
|
|
|
|
2005-11-30 04:01:16 +01:00
|
|
|
/** Map from identity digest of connected OR or desired OR to a connection_t
|
|
|
|
* with that identity digest. If there is more than one such connection_t,
|
2006-06-13 07:50:24 +02:00
|
|
|
* they form a linked list, with next_with_same_id as the next pointer. */
|
2005-11-30 04:01:16 +01:00
|
|
|
static digestmap_t *orconn_identity_map = NULL;
|
|
|
|
|
|
|
|
/** If conn is listed in orconn_identity_map, remove it, and clear
|
2007-02-12 22:39:33 +01:00
|
|
|
* conn->identity_digest. Otherwise do nothing. */
|
2005-11-30 04:01:16 +01:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_remove_from_identity_map(or_connection_t *conn)
|
2005-11-30 04:01:16 +01:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *tmp;
|
2005-11-30 04:01:16 +01:00
|
|
|
tor_assert(conn);
|
|
|
|
if (!orconn_identity_map)
|
|
|
|
return;
|
|
|
|
tmp = digestmap_get(orconn_identity_map, conn->identity_digest);
|
2007-02-12 22:39:33 +01:00
|
|
|
if (!tmp) {
|
|
|
|
if (!tor_digest_is_zero(conn->identity_digest)) {
|
2007-11-07 19:26:46 +01:00
|
|
|
log_warn(LD_BUG, "Didn't find connection '%s' on identity map when "
|
|
|
|
"trying to remove it.",
|
|
|
|
conn->nickname ? conn->nickname : "NULL");
|
2007-02-12 22:39:33 +01:00
|
|
|
}
|
2005-11-30 04:01:16 +01:00
|
|
|
return;
|
2007-02-12 22:39:33 +01:00
|
|
|
}
|
2005-11-30 04:01:16 +01:00
|
|
|
if (conn == tmp) {
|
|
|
|
if (conn->next_with_same_id)
|
|
|
|
digestmap_set(orconn_identity_map, conn->identity_digest,
|
|
|
|
conn->next_with_same_id);
|
|
|
|
else
|
|
|
|
digestmap_remove(orconn_identity_map, conn->identity_digest);
|
|
|
|
} else {
|
|
|
|
while (tmp->next_with_same_id) {
|
|
|
|
if (tmp->next_with_same_id == conn) {
|
|
|
|
tmp->next_with_same_id = conn->next_with_same_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = tmp->next_with_same_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(conn->identity_digest, 0, DIGEST_LEN);
|
|
|
|
conn->next_with_same_id = NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-05 20:15:27 +01:00
|
|
|
/** Remove all entries from the identity-to-orconn map, and clear
|
|
|
|
* all identities in OR conns.*/
|
|
|
|
void
|
|
|
|
connection_or_clear_identity_map(void)
|
|
|
|
{
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, conn,
|
|
|
|
{
|
2005-12-05 20:15:27 +01:00
|
|
|
if (conn->type == CONN_TYPE_OR) {
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *or_conn = TO_OR_CONN(conn);
|
|
|
|
memset(or_conn->identity_digest, 0, DIGEST_LEN);
|
|
|
|
or_conn->next_with_same_id = NULL;
|
2005-12-05 20:15:27 +01:00
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2005-12-05 20:15:27 +01:00
|
|
|
|
2009-12-12 08:07:59 +01:00
|
|
|
digestmap_free(orconn_identity_map, NULL);
|
|
|
|
orconn_identity_map = NULL;
|
2005-12-05 20:15:27 +01:00
|
|
|
}
|
|
|
|
|
2005-11-30 04:01:16 +01:00
|
|
|
/** Change conn->identity_digest to digest, and add conn into
|
2007-02-24 02:12:53 +01:00
|
|
|
* orconn_digest_map. */
|
2005-11-30 04:01:16 +01:00
|
|
|
static void
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_set_identity_digest(or_connection_t *conn, const char *digest)
|
2005-11-30 04:01:16 +01:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *tmp;
|
2005-11-30 04:01:16 +01:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(digest);
|
|
|
|
|
|
|
|
if (!orconn_identity_map)
|
|
|
|
orconn_identity_map = digestmap_new();
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(conn->identity_digest, digest, DIGEST_LEN))
|
2005-11-30 04:01:16 +01:00
|
|
|
return;
|
2007-02-12 22:39:33 +01:00
|
|
|
|
|
|
|
/* If the identity was set previously, remove the old mapping. */
|
|
|
|
if (! tor_digest_is_zero(conn->identity_digest))
|
2005-11-30 04:01:16 +01:00
|
|
|
connection_or_remove_from_identity_map(conn);
|
|
|
|
|
|
|
|
memcpy(conn->identity_digest, digest, DIGEST_LEN);
|
2007-02-12 22:39:33 +01:00
|
|
|
|
2007-02-24 02:12:53 +01:00
|
|
|
/* If we're setting the ID to zero, don't add a mapping. */
|
2007-02-12 22:39:33 +01:00
|
|
|
if (tor_digest_is_zero(digest))
|
|
|
|
return;
|
|
|
|
|
2005-11-30 04:01:16 +01:00
|
|
|
tmp = digestmap_set(orconn_identity_map, digest, conn);
|
|
|
|
conn->next_with_same_id = tmp;
|
|
|
|
|
2007-02-12 22:39:33 +01:00
|
|
|
#if 1
|
2006-12-29 03:47:51 +01:00
|
|
|
/* Testing code to check for bugs in representation. */
|
2005-11-30 04:01:16 +01:00
|
|
|
for (; tmp; tmp = tmp->next_with_same_id) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(tmp->identity_digest, digest, DIGEST_LEN));
|
2005-11-30 04:01:16 +01:00
|
|
|
tor_assert(tmp != conn);
|
|
|
|
}
|
2006-12-29 03:47:51 +01:00
|
|
|
#endif
|
2005-11-30 04:01:16 +01:00
|
|
|
}
|
|
|
|
|
2011-06-22 21:29:30 +02:00
|
|
|
/**************************************************************/
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Map from a string describing what a non-open OR connection was doing when
|
|
|
|
* failed, to an intptr_t describing the count of connections that failed that
|
|
|
|
* way. Note that the count is stored _as_ the pointer.
|
|
|
|
*/
|
2011-06-22 21:29:30 +02:00
|
|
|
static strmap_t *broken_connection_counts;
|
|
|
|
|
2011-07-11 22:10:24 +02:00
|
|
|
/** If true, do not record information in <b>broken_connection_counts</b>. */
|
|
|
|
static int disable_broken_connection_counts = 0;
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Record that an OR connection failed in <b>state</b>. */
|
2011-06-22 21:29:30 +02:00
|
|
|
static void
|
|
|
|
note_broken_connection(const char *state)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
intptr_t val;
|
2011-07-11 22:10:24 +02:00
|
|
|
if (disable_broken_connection_counts)
|
|
|
|
return;
|
|
|
|
|
2011-06-22 21:29:30 +02:00
|
|
|
if (!broken_connection_counts)
|
|
|
|
broken_connection_counts = strmap_new();
|
|
|
|
|
|
|
|
ptr = strmap_get(broken_connection_counts, state);
|
|
|
|
val = (intptr_t)ptr;
|
|
|
|
val++;
|
|
|
|
ptr = (void*)val;
|
|
|
|
strmap_set(broken_connection_counts, state, ptr);
|
|
|
|
}
|
|
|
|
|
2011-07-11 22:10:24 +02:00
|
|
|
/** Forget all recorded states for failed connections. If
|
|
|
|
* <b>stop_recording</b> is true, don't record any more. */
|
2011-06-22 21:29:30 +02:00
|
|
|
void
|
2011-07-11 22:10:24 +02:00
|
|
|
clear_broken_connection_map(int stop_recording)
|
2011-06-22 21:29:30 +02:00
|
|
|
{
|
|
|
|
if (broken_connection_counts)
|
|
|
|
strmap_free(broken_connection_counts, NULL);
|
|
|
|
broken_connection_counts = NULL;
|
2011-07-11 22:10:24 +02:00
|
|
|
if (stop_recording)
|
|
|
|
disable_broken_connection_counts = 1;
|
2011-06-22 21:29:30 +02:00
|
|
|
}
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Write a detailed description the state of <b>orconn</b> into the
|
|
|
|
* <b>buflen</b>-byte buffer at <b>buf</b>. This description includes not
|
|
|
|
* only the OR-conn level state but also the TLS state. It's useful for
|
|
|
|
* diagnosing broken handshakes. */
|
2011-06-22 21:29:30 +02:00
|
|
|
static void
|
|
|
|
connection_or_get_state_description(or_connection_t *orconn,
|
|
|
|
char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
connection_t *conn = TO_CONN(orconn);
|
|
|
|
const char *conn_state;
|
|
|
|
char tls_state[256];
|
|
|
|
|
|
|
|
tor_assert(conn->type == CONN_TYPE_OR);
|
|
|
|
|
|
|
|
conn_state = conn_state_to_string(conn->type, conn->state);
|
|
|
|
tor_tls_get_state_description(orconn->tls, tls_state, sizeof(tls_state));
|
|
|
|
|
|
|
|
tor_snprintf(buf, buflen, "%s with SSL state %s", conn_state, tls_state);
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Record the current state of <b>orconn</b> as the state of a broken
|
|
|
|
* connection. */
|
2011-06-22 21:29:30 +02:00
|
|
|
static void
|
|
|
|
connection_or_note_state_when_broken(or_connection_t *orconn)
|
|
|
|
{
|
|
|
|
char buf[256];
|
2011-07-11 22:10:24 +02:00
|
|
|
if (disable_broken_connection_counts)
|
|
|
|
return;
|
2011-06-22 21:29:30 +02:00
|
|
|
connection_or_get_state_description(orconn, buf, sizeof(buf));
|
|
|
|
log_info(LD_HANDSHAKE,"Connection died in state '%s'", buf);
|
|
|
|
note_broken_connection(buf);
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Helper type used to sort connection states and find the most frequent. */
|
2011-06-22 21:29:30 +02:00
|
|
|
typedef struct broken_state_count_t {
|
|
|
|
intptr_t count;
|
|
|
|
const char *state;
|
|
|
|
} broken_state_count_t;
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Helper function used to sort broken_state_count_t by frequency. */
|
2011-06-22 21:29:30 +02:00
|
|
|
static int
|
|
|
|
broken_state_count_compare(const void **a_ptr, const void **b_ptr)
|
|
|
|
{
|
|
|
|
const broken_state_count_t *a = *a_ptr, *b = *b_ptr;
|
2011-07-12 17:23:55 +02:00
|
|
|
if (b->count < a->count)
|
|
|
|
return -1;
|
|
|
|
else if (b->count == a->count)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
2011-06-22 21:29:30 +02:00
|
|
|
}
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Upper limit on the number of different states to report for connection
|
|
|
|
* failure. */
|
2011-06-28 20:23:28 +02:00
|
|
|
#define MAX_REASONS_TO_REPORT 10
|
|
|
|
|
2011-06-28 21:06:56 +02:00
|
|
|
/** Report a list of the top states for failed OR connections at log level
|
|
|
|
* <b>severity</b>, in log domain <b>domain</b>. */
|
2011-06-22 21:29:30 +02:00
|
|
|
void
|
|
|
|
connection_or_report_broken_states(int severity, int domain)
|
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
smartlist_t *items;
|
|
|
|
|
2011-07-11 22:10:24 +02:00
|
|
|
if (!broken_connection_counts || disable_broken_connection_counts)
|
2011-06-22 21:29:30 +02:00
|
|
|
return;
|
2011-06-28 21:06:56 +02:00
|
|
|
|
2011-06-22 21:29:30 +02:00
|
|
|
items = smartlist_create();
|
|
|
|
STRMAP_FOREACH(broken_connection_counts, state, void *, countptr) {
|
|
|
|
broken_state_count_t *c = tor_malloc(sizeof(broken_state_count_t));
|
2011-07-15 23:12:43 +02:00
|
|
|
c->count = (intptr_t)countptr;
|
|
|
|
total += (int)c->count;
|
2011-06-22 21:29:30 +02:00
|
|
|
c->state = state;
|
|
|
|
smartlist_add(items, c);
|
|
|
|
} STRMAP_FOREACH_END;
|
|
|
|
|
|
|
|
smartlist_sort(items, broken_state_count_compare);
|
|
|
|
|
2011-06-28 20:23:28 +02:00
|
|
|
log(severity, domain, "%d connections have failed%s", total,
|
|
|
|
smartlist_len(items) > MAX_REASONS_TO_REPORT ? ". Top reasons:" : ":");
|
2011-06-22 21:29:30 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(items, const broken_state_count_t *, c) {
|
2011-06-28 20:23:28 +02:00
|
|
|
if (c_sl_idx > MAX_REASONS_TO_REPORT)
|
|
|
|
break;
|
2011-06-22 21:29:30 +02:00
|
|
|
log(severity, domain,
|
|
|
|
" %d connections died in state %s", (int)c->count, c->state);
|
|
|
|
} SMARTLIST_FOREACH_END(c);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(items, broken_state_count_t *, c, tor_free(c));
|
|
|
|
smartlist_free(items);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************/
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Pack the cell_t host-order structure <b>src</b> into network-order
|
|
|
|
* in the buffer <b>dest</b>. See tor-spec.txt for details about the
|
2004-05-07 10:53:40 +02:00
|
|
|
* wire format.
|
2007-05-25 21:41:31 +02:00
|
|
|
*
|
|
|
|
* Note that this function doesn't touch <b>dst</b>-\>next: the caller
|
|
|
|
* should set it or clear it as appropriate.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2007-04-10 01:15:46 +02:00
|
|
|
void
|
|
|
|
cell_pack(packed_cell_t *dst, const cell_t *src)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-04-10 01:15:46 +02:00
|
|
|
char *dest = dst->body;
|
2010-09-21 00:40:32 +02:00
|
|
|
set_uint16(dest, htons(src->circ_id));
|
2005-08-23 11:50:51 +02:00
|
|
|
*(uint8_t*)(dest+2) = src->command;
|
2003-12-16 10:48:17 +01:00
|
|
|
memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
|
2003-09-16 07:41:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Unpack the network-order buffer <b>src</b> into a host-order
|
|
|
|
* cell_t structure <b>dest</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
cell_unpack(cell_t *dest, const char *src)
|
|
|
|
{
|
2010-09-21 00:40:32 +02:00
|
|
|
dest->circ_id = ntohs(get_uint16(src));
|
2003-09-16 07:41:49 +02:00
|
|
|
dest->command = *(uint8_t*)(src+2);
|
2003-12-16 10:48:17 +01:00
|
|
|
memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
|
2003-09-16 07:41:49 +02:00
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Write the header of <b>cell</b> into the first VAR_CELL_HEADER_SIZE
|
|
|
|
* bytes of <b>hdr_out</b>. */
|
2007-11-05 22:46:35 +01:00
|
|
|
void
|
2007-11-06 00:34:39 +01:00
|
|
|
var_cell_pack_header(const var_cell_t *cell, char *hdr_out)
|
2007-11-05 22:46:35 +01:00
|
|
|
{
|
2008-11-12 15:41:44 +01:00
|
|
|
set_uint16(hdr_out, htons(cell->circ_id));
|
|
|
|
set_uint8(hdr_out+2, cell->command);
|
2007-11-05 22:46:35 +01:00
|
|
|
set_uint16(hdr_out+3, htons(cell->payload_len));
|
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Allocate and return a new var_cell_t with <b>payload_len</b> bytes of
|
|
|
|
* payload space. */
|
2007-11-06 00:34:39 +01:00
|
|
|
var_cell_t *
|
|
|
|
var_cell_new(uint16_t payload_len)
|
|
|
|
{
|
2011-01-06 21:59:05 +01:00
|
|
|
size_t size = STRUCT_OFFSET(var_cell_t, payload) + payload_len;
|
|
|
|
var_cell_t *cell = tor_malloc(size);
|
2007-11-06 00:34:39 +01:00
|
|
|
cell->payload_len = payload_len;
|
|
|
|
cell->command = 0;
|
|
|
|
cell->circ_id = 0;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Release all space held by <b>cell</b>. */
|
2007-11-05 22:46:35 +01:00
|
|
|
void
|
|
|
|
var_cell_free(var_cell_t *cell)
|
|
|
|
{
|
|
|
|
tor_free(cell);
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** We've received an EOF from <b>conn</b>. Mark it for close and return. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_reached_eof(or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_OR,"OR connection reached EOF. Closing.");
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2004-11-21 11:14:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Handle any new bytes that have come in on connection <b>conn</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
* If conn is in 'open' state, hand it to
|
|
|
|
* connection_or_process_cells_from_inbuf()
|
|
|
|
* (else do nothing).
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_process_inbuf(or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2009-06-19 01:59:18 +02:00
|
|
|
int ret;
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
switch (conn->_base.state) {
|
2009-06-19 01:59:18 +02:00
|
|
|
case OR_CONN_STATE_PROXY_HANDSHAKING:
|
|
|
|
ret = connection_read_proxy_handshake(TO_CONN(conn));
|
|
|
|
|
|
|
|
/* start TLS after handshake completion, or deal with error */
|
|
|
|
if (ret == 1) {
|
|
|
|
tor_assert(TO_CONN(conn)->proxy_state == PROXY_CONNECTED);
|
|
|
|
if (connection_tls_start_handshake(conn, 0) < 0)
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-10-11 19:45:27 +02:00
|
|
|
case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
|
2011-09-27 20:04:21 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
2010-10-11 19:45:27 +02:00
|
|
|
if (tor_tls_server_got_renegotiate(conn->tls))
|
|
|
|
connection_or_tls_renegotiated_cb(conn->tls, conn);
|
|
|
|
if (conn->_base.marked_for_close)
|
|
|
|
return 0;
|
|
|
|
/* fall through. */
|
2010-11-30 23:55:27 +01:00
|
|
|
#endif
|
2005-02-24 11:56:55 +01:00
|
|
|
case OR_CONN_STATE_OPEN:
|
2011-09-13 16:03:09 +02:00
|
|
|
case OR_CONN_STATE_OR_HANDSHAKING_V2:
|
2011-09-27 19:15:36 +02:00
|
|
|
case OR_CONN_STATE_OR_HANDSHAKING_V3:
|
2005-02-24 11:56:55 +01:00
|
|
|
return connection_or_process_cells_from_inbuf(conn);
|
|
|
|
default:
|
|
|
|
return 0; /* don't do anything */
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** When adding cells to an OR connection's outbuf, keep adding until the
|
|
|
|
* outbuf is at least this long, or we run out of cells. */
|
2007-03-26 16:08:18 +02:00
|
|
|
#define OR_CONN_HIGHWATER (32*1024)
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Add cells to an OR connection's outbuf whenever the outbuf's data length
|
|
|
|
* drops below this size. */
|
2007-03-26 16:08:18 +02:00
|
|
|
#define OR_CONN_LOWWATER (16*1024)
|
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
/** Called whenever we have flushed some data on an or_conn: add more data
|
|
|
|
* from active circuits. */
|
2007-01-27 09:55:06 +01:00
|
|
|
int
|
|
|
|
connection_or_flushed_some(or_connection_t *conn)
|
|
|
|
{
|
2009-07-31 17:39:31 +02:00
|
|
|
size_t datalen = connection_get_outbuf_len(TO_CONN(conn));
|
2007-03-26 16:08:35 +02:00
|
|
|
/* If we're under the low water mark, add cells until we're just over the
|
|
|
|
* high water mark. */
|
2007-03-26 16:08:18 +02:00
|
|
|
if (datalen < OR_CONN_LOWWATER) {
|
2010-09-15 04:32:36 +02:00
|
|
|
ssize_t n = CEIL_DIV(OR_CONN_HIGHWATER - datalen, CELL_NETWORK_SIZE);
|
2008-12-19 19:51:35 +01:00
|
|
|
time_t now = approx_time();
|
2007-03-26 16:07:59 +02:00
|
|
|
while (conn->active_circuits && n > 0) {
|
2008-04-09 20:44:50 +02:00
|
|
|
int flushed;
|
|
|
|
flushed = connection_or_flush_from_first_active_circuit(conn, 1, now);
|
2007-03-26 16:07:59 +02:00
|
|
|
n -= flushed;
|
|
|
|
}
|
|
|
|
}
|
2007-01-27 09:55:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Connection <b>conn</b> has finished writing and has no bytes left on
|
2004-05-07 10:53:40 +02:00
|
|
|
* its outbuf.
|
|
|
|
*
|
2004-05-10 06:34:48 +02:00
|
|
|
* Otherwise it's in state "open": stop writing and return.
|
2004-05-10 05:54:33 +02:00
|
|
|
*
|
|
|
|
* If <b>conn</b> is broken, mark it for close and return -1, else
|
|
|
|
* return 0.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_finished_flushing(or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_ok(TO_CONN(conn),0);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
switch (conn->_base.state) {
|
2009-06-19 01:59:18 +02:00
|
|
|
case OR_CONN_STATE_PROXY_HANDSHAKING:
|
2005-02-24 11:56:55 +01:00
|
|
|
case OR_CONN_STATE_OPEN:
|
2011-09-13 16:03:09 +02:00
|
|
|
case OR_CONN_STATE_OR_HANDSHAKING_V2:
|
2011-09-27 19:40:39 +02:00
|
|
|
case OR_CONN_STATE_OR_HANDSHAKING_V3:
|
2005-02-24 11:56:55 +01:00
|
|
|
break;
|
|
|
|
default:
|
2007-03-04 21:11:46 +01:00
|
|
|
log_err(LD_BUG,"Called in unexpected state %d.", conn->_base.state);
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2005-02-24 11:56:55 +01:00
|
|
|
return -1;
|
2004-05-12 21:17:09 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Connected handler for OR connections: begin the TLS handshake.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_finished_connecting(or_connection_t *or_conn)
|
2004-05-12 21:17:09 +02:00
|
|
|
{
|
2011-07-03 06:13:41 +02:00
|
|
|
const int proxy_type = or_conn->proxy_type;
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_t *conn;
|
|
|
|
tor_assert(or_conn);
|
|
|
|
conn = TO_CONN(or_conn);
|
2004-05-12 21:17:09 +02:00
|
|
|
tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
|
|
|
|
|
2009-09-24 18:31:22 +02:00
|
|
|
log_debug(LD_HANDSHAKE,"OR connect() to router at %s:%u finished.",
|
2006-02-13 10:02:35 +01:00
|
|
|
conn->address,conn->port);
|
2008-06-07 07:27:34 +02:00
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_HANDSHAKE, 0);
|
2004-05-12 21:17:09 +02:00
|
|
|
|
2009-06-19 01:59:18 +02:00
|
|
|
if (proxy_type != PROXY_NONE) {
|
|
|
|
/* start proxy handshake */
|
|
|
|
if (connection_proxy_connect(conn, proxy_type) < 0) {
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
return -1;
|
2005-04-26 20:33:33 +02:00
|
|
|
}
|
2009-06-19 01:59:18 +02:00
|
|
|
|
|
|
|
connection_start_reading(conn);
|
|
|
|
conn->state = OR_CONN_STATE_PROXY_HANDSHAKING;
|
2005-02-24 11:56:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (connection_tls_start_handshake(or_conn, 0) < 0) {
|
2004-05-12 23:12:33 +02:00
|
|
|
/* TLS handshaking error of some kind. */
|
|
|
|
connection_mark_for_close(conn);
|
2004-05-12 21:17:09 +02:00
|
|
|
return -1;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2004-05-12 21:17:09 +02:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2011-06-22 19:57:19 +02:00
|
|
|
/* Called when we're about to finally unlink and free an OR connection:
|
|
|
|
* perform necessary accounting and cleanup */
|
|
|
|
void
|
|
|
|
connection_or_about_to_close(or_connection_t *or_conn)
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
connection_t *conn = TO_CONN(or_conn);
|
|
|
|
|
|
|
|
/* Remember why we're closing this connection. */
|
|
|
|
if (conn->state != OR_CONN_STATE_OPEN) {
|
|
|
|
/* Inform any pending (not attached) circs that they should
|
|
|
|
* give up. */
|
|
|
|
circuit_n_conn_done(TO_OR_CONN(conn), 0);
|
|
|
|
/* now mark things down as needed */
|
|
|
|
if (connection_or_nonopen_was_started_here(or_conn)) {
|
|
|
|
const or_options_t *options = get_options();
|
2011-06-22 21:29:30 +02:00
|
|
|
connection_or_note_state_when_broken(or_conn);
|
2011-06-22 19:57:19 +02:00
|
|
|
rep_hist_note_connect_failed(or_conn->identity_digest, now);
|
|
|
|
entry_guard_register_connect_status(or_conn->identity_digest,0,
|
|
|
|
!options->HTTPSProxy, now);
|
|
|
|
if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
|
|
|
|
int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
|
|
|
|
control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
|
|
|
|
reason);
|
|
|
|
if (!authdir_mode_tests_reachability(options))
|
|
|
|
control_event_bootstrap_problem(
|
|
|
|
orconn_end_reason_to_control_string(reason), reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (conn->hold_open_until_flushed) {
|
|
|
|
/* We only set hold_open_until_flushed when we're intentionally
|
|
|
|
* closing a connection. */
|
|
|
|
rep_hist_note_disconnect(or_conn->identity_digest, now);
|
|
|
|
control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
|
|
|
|
tls_error_to_orconn_end_reason(or_conn->tls_error));
|
|
|
|
} else if (!tor_digest_is_zero(or_conn->identity_digest)) {
|
|
|
|
rep_hist_note_connection_died(or_conn->identity_digest, now);
|
|
|
|
control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
|
|
|
|
tls_error_to_orconn_end_reason(or_conn->tls_error));
|
|
|
|
}
|
|
|
|
/* Now close all the attached circuits on it. */
|
|
|
|
circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
|
|
|
|
END_CIRC_REASON_OR_CONN_CLOSED);
|
|
|
|
}
|
|
|
|
|
2009-12-23 10:56:24 +01:00
|
|
|
/** Return 1 if identity digest <b>id_digest</b> is known to be a
|
|
|
|
* currently or recently running relay. Otherwise return 0. */
|
2010-03-11 04:43:23 +01:00
|
|
|
int
|
2009-12-23 10:56:24 +01:00
|
|
|
connection_or_digest_is_known_relay(const char *id_digest)
|
|
|
|
{
|
|
|
|
if (router_get_consensus_status_by_id(id_digest))
|
|
|
|
return 1; /* It's in the consensus: "yes" */
|
2010-10-14 17:49:51 +02:00
|
|
|
if (router_get_by_id_digest(id_digest))
|
2009-12-23 10:56:24 +01:00
|
|
|
return 1; /* Not in the consensus, but we have a descriptor for
|
|
|
|
* it. Probably it was in a recent consensus. "Yes". */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-15 08:27:07 +02:00
|
|
|
/** Set the per-conn read and write limits for <b>conn</b>. If it's a known
|
|
|
|
* relay, we will rely on the global read and write buckets, so give it
|
|
|
|
* per-conn limits that are big enough they'll never matter. But if it's
|
|
|
|
* not a known relay, first check if we set PerConnBwRate/Burst, then
|
|
|
|
* check if the consensus sets them, else default to 'big enough'.
|
2011-03-25 22:21:16 +01:00
|
|
|
*
|
|
|
|
* If <b>reset</b> is true, set the bucket to be full. Otherwise, just
|
|
|
|
* clip the bucket if it happens to be <em>too</em> full.
|
2010-08-15 08:27:07 +02:00
|
|
|
*/
|
2010-08-15 10:01:42 +02:00
|
|
|
static void
|
|
|
|
connection_or_update_token_buckets_helper(or_connection_t *conn, int reset,
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options)
|
2004-07-01 03:16:59 +02:00
|
|
|
{
|
2009-12-30 05:13:03 +01:00
|
|
|
int rate, burst; /* per-connection rate limiting params */
|
2010-08-15 08:27:07 +02:00
|
|
|
if (connection_or_digest_is_known_relay(conn->identity_digest)) {
|
2009-12-23 10:56:24 +01:00
|
|
|
/* It's in the consensus, or we have a descriptor for it meaning it
|
|
|
|
* was probably in a recent consensus. It's a recognized relay:
|
|
|
|
* give it full bandwidth. */
|
2009-12-30 05:13:03 +01:00
|
|
|
rate = (int)options->BandwidthRate;
|
|
|
|
burst = (int)options->BandwidthBurst;
|
|
|
|
} else {
|
|
|
|
/* Not a recognized relay. Squeeze it down based on the suggested
|
|
|
|
* bandwidth parameters in the consensus, but allow local config
|
|
|
|
* options to override. */
|
|
|
|
rate = options->PerConnBWRate ? (int)options->PerConnBWRate :
|
2010-12-30 19:54:13 +01:00
|
|
|
networkstatus_get_param(NULL, "perconnbwrate",
|
|
|
|
(int)options->BandwidthRate, 1, INT32_MAX);
|
2009-12-30 05:13:03 +01:00
|
|
|
burst = options->PerConnBWBurst ? (int)options->PerConnBWBurst :
|
2010-12-30 19:54:13 +01:00
|
|
|
networkstatus_get_param(NULL, "perconnbwburst",
|
|
|
|
(int)options->BandwidthBurst, 1, INT32_MAX);
|
2009-12-23 10:56:24 +01:00
|
|
|
}
|
|
|
|
|
2009-12-30 05:13:03 +01:00
|
|
|
conn->bandwidthrate = rate;
|
2010-08-15 10:01:42 +02:00
|
|
|
conn->bandwidthburst = burst;
|
2010-02-22 19:59:34 +01:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
{
|
|
|
|
const struct timeval *tick = tor_libevent_get_one_tick_timeout();
|
|
|
|
struct ev_token_bucket_cfg *cfg, *old_cfg;
|
2011-09-08 04:00:48 +02:00
|
|
|
int64_t rate64 = (((int64_t)rate) * options->TokenBucketRefillInterval)
|
|
|
|
/ 1000;
|
|
|
|
/* This can't overflow, since TokenBucketRefillInterval <= 1000,
|
|
|
|
* and rate started out less than INT_MAX. */
|
|
|
|
int rate_per_tick = (int) rate64;
|
|
|
|
|
2010-02-22 19:59:34 +01:00
|
|
|
cfg = ev_token_bucket_cfg_new(rate_per_tick, burst, rate_per_tick,
|
|
|
|
burst, tick);
|
|
|
|
old_cfg = conn->bucket_cfg;
|
|
|
|
if (conn->_base.bufev)
|
2011-08-24 23:09:56 +02:00
|
|
|
tor_set_bufferevent_rate_limit(conn->_base.bufev, cfg);
|
2010-02-22 19:59:34 +01:00
|
|
|
if (old_cfg)
|
|
|
|
ev_token_bucket_cfg_free(old_cfg);
|
|
|
|
conn->bucket_cfg = cfg;
|
2010-09-27 18:45:56 +02:00
|
|
|
(void) reset; /* No way to do this with libevent yet. */
|
2010-02-22 19:59:34 +01:00
|
|
|
}
|
|
|
|
#else
|
2010-08-15 10:01:42 +02:00
|
|
|
if (reset) { /* set up the token buckets to be full */
|
|
|
|
conn->read_bucket = conn->write_bucket = burst;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* If the new token bucket is smaller, take out the extra tokens.
|
|
|
|
* (If it's larger, don't -- the buckets can grow to reach the cap.) */
|
|
|
|
if (conn->read_bucket > burst)
|
|
|
|
conn->read_bucket = burst;
|
|
|
|
if (conn->write_bucket > burst)
|
|
|
|
conn->write_bucket = burst;
|
2010-02-22 19:59:34 +01:00
|
|
|
#endif
|
2010-08-15 10:01:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Either our set of relays or our per-conn rate limits have changed.
|
2011-03-25 22:21:16 +01:00
|
|
|
* Go through all the OR connections and update their token buckets to make
|
|
|
|
* sure they don't exceed their maximum values. */
|
2010-08-15 10:01:42 +02:00
|
|
|
void
|
2011-06-14 19:01:38 +02:00
|
|
|
connection_or_update_token_buckets(smartlist_t *conns,
|
|
|
|
const or_options_t *options)
|
2010-08-15 10:01:42 +02:00
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, conn,
|
|
|
|
{
|
|
|
|
if (connection_speaks_cells(conn))
|
|
|
|
connection_or_update_token_buckets_helper(TO_OR_CONN(conn), 0, options);
|
|
|
|
});
|
2010-08-15 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** If we don't necessarily know the router we're connecting to, but we
|
|
|
|
* have an addr/port/id_digest, then fill in as much as we can. Start
|
2011-11-24 18:39:27 +01:00
|
|
|
* by checking to see if this describes a router we know.
|
|
|
|
* <b>started_here</b> is 1 if we are the initiator of <b>conn</b> and
|
|
|
|
* 0 if it's an incoming connection. */
|
2011-09-27 19:15:36 +02:00
|
|
|
void
|
2010-08-15 08:27:07 +02:00
|
|
|
connection_or_init_conn_from_address(or_connection_t *conn,
|
|
|
|
const tor_addr_t *addr, uint16_t port,
|
|
|
|
const char *id_digest,
|
|
|
|
int started_here)
|
|
|
|
{
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *r = node_get_by_id(id_digest);
|
2010-08-15 08:27:07 +02:00
|
|
|
connection_or_set_identity_digest(conn, id_digest);
|
2010-08-15 10:01:42 +02:00
|
|
|
connection_or_update_token_buckets_helper(conn, 1, get_options());
|
2009-12-30 05:13:03 +01:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.port = port;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_copy(&conn->_base.addr, addr);
|
|
|
|
tor_addr_copy(&conn->real_addr, addr);
|
2004-07-01 03:16:59 +02:00
|
|
|
if (r) {
|
2011-11-28 12:15:58 +01:00
|
|
|
tor_addr_port_t node_ap;
|
|
|
|
node_get_pref_orport(r, &node_ap);
|
2011-11-24 18:59:24 +01:00
|
|
|
/* XXXX proposal 186 is making this more complex. For now, a conn
|
|
|
|
is canonical when it uses the _preferred_ address. */
|
2011-11-28 12:15:58 +01:00
|
|
|
if (tor_addr_eq(&conn->_base.addr, &node_ap.addr))
|
2007-10-30 19:31:30 +01:00
|
|
|
conn->is_canonical = 1;
|
2006-06-13 07:36:35 +02:00
|
|
|
if (!started_here) {
|
|
|
|
/* Override the addr/port, so our log messages will make sense.
|
|
|
|
* This is dangerous, since if we ever try looking up a conn by
|
|
|
|
* its actual addr/port, we won't remember. Careful! */
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX arma: this is stupid, and it's the reason we need real_addr
|
2008-02-12 21:20:52 +01:00
|
|
|
* to track is_canonical properly. What requires it? */
|
2008-02-12 23:21:20 +01:00
|
|
|
/* XXXX <arma> i believe the reason we did this, originally, is because
|
|
|
|
* we wanted to log what OR a connection was to, and if we logged the
|
|
|
|
* right IP address and port 56244, that wouldn't be as helpful. now we
|
|
|
|
* log the "right" port too, so we know if it's moria1 or moria2.
|
2008-08-05 22:08:19 +02:00
|
|
|
*/
|
2011-11-28 12:15:58 +01:00
|
|
|
tor_addr_copy(&conn->_base.addr, &node_ap.addr);
|
|
|
|
conn->_base.port = node_ap.port;
|
2006-06-13 07:36:35 +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
|
|
|
conn->nickname = tor_strdup(node_get_nickname(r));
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_free(conn->_base.address);
|
2011-11-28 12:15:58 +01:00
|
|
|
conn->_base.address = tor_dup_addr(&node_ap.addr);
|
2004-09-29 00:24:56 +02:00
|
|
|
} else {
|
2006-06-07 11:18:53 +02:00
|
|
|
const char *n;
|
|
|
|
/* If we're an authoritative directory server, we may know a
|
|
|
|
* nickname for this router. */
|
|
|
|
n = dirserv_get_nickname_by_digest(id_digest);
|
|
|
|
if (n) {
|
|
|
|
conn->nickname = tor_strdup(n);
|
|
|
|
} else {
|
|
|
|
conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
|
|
|
|
conn->nickname[0] = '$';
|
|
|
|
base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
|
|
|
|
conn->identity_digest, DIGEST_LEN);
|
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_free(conn->_base.address);
|
2008-08-05 22:08:19 +02:00
|
|
|
conn->_base.address = tor_dup_addr(addr);
|
2004-09-29 00:24:56 +02:00
|
|
|
}
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
2008-12-24 03:38:04 +01:00
|
|
|
/** Return true iff <b>a</b> is "better" than <b>b</b> for new circuits.
|
2005-11-30 04:01:16 +01:00
|
|
|
*
|
2008-12-24 03:38:04 +01:00
|
|
|
* A more canonical connection is always better than a less canonical
|
|
|
|
* connection. That aside, a connection is better if it has circuits and the
|
|
|
|
* other does not, or if it was created more recently.
|
|
|
|
*
|
|
|
|
* Requires that both input connections are open; not is_bad_for_new_circs,
|
|
|
|
* and not impossibly non-canonical.
|
2009-01-28 18:36:41 +01:00
|
|
|
*
|
2011-03-16 19:47:27 +01:00
|
|
|
* If <b>forgive_new_connections</b> is true, then we do not call
|
2009-01-28 18:36:41 +01:00
|
|
|
* <b>a</b>better than <b>b</b> simply because b has no circuits,
|
|
|
|
* unless b is also relatively old.
|
2005-11-30 04:01:16 +01:00
|
|
|
*/
|
2008-12-24 03:38:04 +01:00
|
|
|
static int
|
2009-01-28 18:36:41 +01:00
|
|
|
connection_or_is_better(time_t now,
|
|
|
|
const or_connection_t *a,
|
|
|
|
const or_connection_t *b,
|
|
|
|
int forgive_new_connections)
|
2005-11-30 04:01:16 +01:00
|
|
|
{
|
|
|
|
int newer;
|
2009-01-28 18:36:41 +01:00
|
|
|
/** Do not definitively deprecate a new connection with no circuits on it
|
|
|
|
* until this much time has passed. */
|
|
|
|
#define NEW_CONN_GRACE_PERIOD (15*60)
|
2008-12-24 03:38:04 +01:00
|
|
|
|
|
|
|
if (b->is_canonical && !a->is_canonical)
|
|
|
|
return 0; /* A canonical connection is better than a non-canonical
|
|
|
|
* one, no matter how new it is or which has circuits. */
|
|
|
|
|
|
|
|
newer = b->_base.timestamp_created < a->_base.timestamp_created;
|
|
|
|
|
2009-01-28 18:36:41 +01:00
|
|
|
if (
|
|
|
|
/* We prefer canonical connections regardless of newness. */
|
|
|
|
(!b->is_canonical && a->is_canonical) ||
|
|
|
|
/* If both have circuits we prefer the newer: */
|
|
|
|
(b->n_circuits && a->n_circuits && newer) ||
|
|
|
|
/* If neither has circuits we prefer the newer: */
|
|
|
|
(!b->n_circuits && !a->n_circuits && newer))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* If one has no circuits and the other does... */
|
|
|
|
if (!b->n_circuits && a->n_circuits) {
|
|
|
|
/* Then it's bad, unless it's in its grace period and we're forgiving. */
|
|
|
|
if (forgive_new_connections &&
|
|
|
|
now < b->_base.timestamp_created + NEW_CONN_GRACE_PERIOD)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-12-24 03:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the OR connection we should use to extend a circuit to the router
|
|
|
|
* whose identity is <b>digest</b>, and whose address we believe (or have been
|
|
|
|
* told in an extend cell) is <b>target_addr</b>. If there is no good
|
|
|
|
* connection, set *<b>msg_out</b> to a message describing the connection's
|
|
|
|
* state and our next action, and set <b>launch_out</b> to a boolean for
|
|
|
|
* whether we should launch a new connection or not.
|
|
|
|
*/
|
|
|
|
or_connection_t *
|
|
|
|
connection_or_get_for_extend(const char *digest,
|
|
|
|
const tor_addr_t *target_addr,
|
|
|
|
const char **msg_out,
|
|
|
|
int *launch_out)
|
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *conn, *best=NULL;
|
2008-12-24 03:38:04 +01:00
|
|
|
int n_inprogress_goodaddr = 0, n_old = 0, n_noncanonical = 0, n_possible = 0;
|
2009-01-28 18:36:41 +01:00
|
|
|
time_t now = approx_time();
|
2005-11-30 04:01:16 +01:00
|
|
|
|
2008-12-24 03:38:04 +01:00
|
|
|
tor_assert(msg_out);
|
|
|
|
tor_assert(launch_out);
|
|
|
|
|
|
|
|
if (!orconn_identity_map) {
|
|
|
|
*msg_out = "Router not connected (nothing is). Connecting.";
|
|
|
|
*launch_out = 1;
|
2005-11-30 04:01:16 +01:00
|
|
|
return NULL;
|
2008-12-24 03:38:04 +01:00
|
|
|
}
|
2005-11-30 04:01:16 +01:00
|
|
|
|
|
|
|
conn = digestmap_get(orconn_identity_map, digest);
|
|
|
|
|
|
|
|
for (; conn; conn = conn->next_with_same_id) {
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.magic == OR_CONNECTION_MAGIC);
|
|
|
|
tor_assert(conn->_base.type == CONN_TYPE_OR);
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(conn->identity_digest, digest, DIGEST_LEN));
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.marked_for_close)
|
2005-11-30 04:01:16 +01:00
|
|
|
continue;
|
2011-10-23 23:27:56 +02:00
|
|
|
/* Never return a connection on which the other end appears to be
|
|
|
|
* a client. */
|
|
|
|
if (conn->is_connection_with_client) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-24 03:38:04 +01:00
|
|
|
/* Never return a non-open connection. */
|
|
|
|
if (conn->_base.state != OR_CONN_STATE_OPEN) {
|
|
|
|
/* If the address matches, don't launch a new connection for this
|
|
|
|
* circuit. */
|
|
|
|
if (!tor_addr_compare(&conn->real_addr, target_addr, CMP_EXACT))
|
|
|
|
++n_inprogress_goodaddr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Never return a connection that shouldn't be used for circs. */
|
|
|
|
if (conn->is_bad_for_new_circs) {
|
|
|
|
++n_old;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Never return a non-canonical connection using a recent link protocol
|
|
|
|
* if the address is not what we wanted.
|
|
|
|
*
|
|
|
|
* (For old link protocols, we can't rely on is_canonical getting
|
|
|
|
* set properly if we're talking to the right address, since we might
|
|
|
|
* have an out-of-date descriptor, and we will get no NETINFO cell to
|
|
|
|
* tell us about the right address.) */
|
|
|
|
if (!conn->is_canonical && conn->link_proto >= 2 &&
|
|
|
|
tor_addr_compare(&conn->real_addr, target_addr, CMP_EXACT)) {
|
|
|
|
++n_noncanonical;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++n_possible;
|
|
|
|
|
2005-11-30 04:01:16 +01:00
|
|
|
if (!best) {
|
2008-12-24 03:38:04 +01:00
|
|
|
best = conn; /* If we have no 'best' so far, this one is good enough. */
|
2005-11-30 04:01:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-12-24 03:38:04 +01:00
|
|
|
|
2009-01-28 18:36:41 +01:00
|
|
|
if (connection_or_is_better(now, conn, best, 0))
|
2006-02-14 01:08:19 +01:00
|
|
|
best = conn;
|
2005-11-30 04:01:16 +01:00
|
|
|
}
|
2008-12-24 03:38:04 +01:00
|
|
|
|
|
|
|
if (best) {
|
|
|
|
*msg_out = "Connection is fine; using it.";
|
|
|
|
*launch_out = 0;
|
|
|
|
return best;
|
|
|
|
} else if (n_inprogress_goodaddr) {
|
|
|
|
*msg_out = "Connection in progress; waiting.";
|
|
|
|
*launch_out = 0;
|
|
|
|
return NULL;
|
|
|
|
} else if (n_old || n_noncanonical) {
|
|
|
|
*msg_out = "Connections all too old, or too non-canonical. "
|
|
|
|
" Launching a new one.";
|
|
|
|
*launch_out = 1;
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
*msg_out = "Not connected. Connecting.";
|
|
|
|
*launch_out = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** How old do we let a connection to an OR get before deciding it's
|
|
|
|
* too old for new circuits? */
|
|
|
|
#define TIME_BEFORE_OR_CONN_IS_TOO_OLD (60*60*24*7)
|
|
|
|
|
|
|
|
/** Given the head of the linked list for all the or_connections with a given
|
2010-09-29 05:27:00 +02:00
|
|
|
* identity, set elements of that list as is_bad_for_new_circs as
|
|
|
|
* appropriate. Helper for connection_or_set_bad_connections().
|
|
|
|
*
|
|
|
|
* Specifically, we set the is_bad_for_new_circs flag on:
|
|
|
|
* - all connections if <b>force</b> is true.
|
|
|
|
* - all connections that are too old.
|
|
|
|
* - all open non-canonical connections for which a canonical connection
|
|
|
|
* exists to the same router.
|
|
|
|
* - all open canonical connections for which a 'better' canonical
|
|
|
|
* connection exists to the same router.
|
|
|
|
* - all open non-canonical connections for which a 'better' non-canonical
|
|
|
|
* connection exists to the same router at the same address.
|
|
|
|
*
|
|
|
|
* See connection_or_is_better() for our idea of what makes one OR connection
|
|
|
|
* better than another.
|
2008-12-24 03:38:04 +01:00
|
|
|
*/
|
|
|
|
static void
|
2010-09-29 04:32:38 +02:00
|
|
|
connection_or_group_set_badness(or_connection_t *head, int force)
|
2008-12-24 03:38:04 +01:00
|
|
|
{
|
|
|
|
or_connection_t *or_conn = NULL, *best = NULL;
|
|
|
|
int n_old = 0, n_inprogress = 0, n_canonical = 0, n_other = 0;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
/* Pass 1: expire everything that's old, and see what the status of
|
|
|
|
* everything else is. */
|
|
|
|
for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
|
|
|
|
if (or_conn->_base.marked_for_close ||
|
|
|
|
or_conn->is_bad_for_new_circs)
|
|
|
|
continue;
|
2010-09-29 04:32:38 +02:00
|
|
|
if (force ||
|
|
|
|
or_conn->_base.timestamp_created + TIME_BEFORE_OR_CONN_IS_TOO_OLD
|
|
|
|
< now) {
|
2008-12-24 03:38:04 +01:00
|
|
|
log_info(LD_OR,
|
|
|
|
"Marking OR conn to %s:%d as too old for new circuits "
|
|
|
|
"(fd %d, %d secs old).",
|
|
|
|
or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
|
|
|
|
(int)(now - or_conn->_base.timestamp_created));
|
|
|
|
or_conn->is_bad_for_new_circs = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (or_conn->is_bad_for_new_circs) {
|
|
|
|
++n_old;
|
|
|
|
} else if (or_conn->_base.state != OR_CONN_STATE_OPEN) {
|
|
|
|
++n_inprogress;
|
|
|
|
} else if (or_conn->is_canonical) {
|
|
|
|
++n_canonical;
|
|
|
|
} else {
|
|
|
|
++n_other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pass 2: We know how about how good the best connection is.
|
|
|
|
* expire everything that's worse, and find the very best if we can. */
|
|
|
|
for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
|
|
|
|
if (or_conn->_base.marked_for_close ||
|
|
|
|
or_conn->is_bad_for_new_circs)
|
|
|
|
continue; /* This one doesn't need to be marked bad. */
|
|
|
|
if (or_conn->_base.state != OR_CONN_STATE_OPEN)
|
|
|
|
continue; /* Don't mark anything bad until we have seen what happens
|
|
|
|
* when the connection finishes. */
|
|
|
|
if (n_canonical && !or_conn->is_canonical) {
|
|
|
|
/* We have at least one open canonical connection to this router,
|
|
|
|
* and this one is open but not canonical. Mark it bad. */
|
|
|
|
log_info(LD_OR,
|
2010-05-12 20:15:39 +02:00
|
|
|
"Marking OR conn to %s:%d as unsuitable for new circuits: "
|
2008-12-24 03:38:04 +01:00
|
|
|
"(fd %d, %d secs old). It is not canonical, and we have "
|
|
|
|
"another connection to that OR that is.",
|
|
|
|
or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
|
|
|
|
(int)(now - or_conn->_base.timestamp_created));
|
|
|
|
or_conn->is_bad_for_new_circs = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-28 18:36:41 +01:00
|
|
|
if (!best || connection_or_is_better(now, or_conn, best, 0))
|
2008-12-24 03:38:04 +01:00
|
|
|
best = or_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!best)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Pass 3: One connection to OR is best. If it's canonical, mark as bad
|
|
|
|
* every other open connection. If it's non-canonical, mark as bad
|
|
|
|
* every other open connection to the same address.
|
|
|
|
*
|
2008-12-29 20:57:13 +01:00
|
|
|
* XXXX This isn't optimal; if we have connections to an OR at multiple
|
|
|
|
* addresses, we'd like to pick the best _for each address_, and mark as
|
|
|
|
* bad every open connection that isn't best for its address. But this
|
|
|
|
* can only occur in cases where the other OR is old (so we have no
|
|
|
|
* canonical connection to it), or where all the connections to the OR are
|
|
|
|
* at noncanonical addresses and we have no good direct connection (which
|
|
|
|
* means we aren't at risk of attaching circuits to it anyway). As
|
|
|
|
* 0.1.2.x dies out, the first case will go away, and the second one is
|
|
|
|
* "mostly harmless", so a fix can wait until somebody is bored.
|
2008-12-24 03:38:04 +01:00
|
|
|
*/
|
|
|
|
for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
|
|
|
|
if (or_conn->_base.marked_for_close ||
|
|
|
|
or_conn->is_bad_for_new_circs ||
|
|
|
|
or_conn->_base.state != OR_CONN_STATE_OPEN)
|
|
|
|
continue;
|
2009-01-28 18:36:41 +01:00
|
|
|
if (or_conn != best && connection_or_is_better(now, best, or_conn, 1)) {
|
|
|
|
/* This isn't the best conn, _and_ the best conn is better than it,
|
|
|
|
even when we're being forgiving. */
|
2008-12-24 03:38:04 +01:00
|
|
|
if (best->is_canonical) {
|
|
|
|
log_info(LD_OR,
|
2010-05-12 20:15:39 +02:00
|
|
|
"Marking OR conn to %s:%d as unsuitable for new circuits: "
|
2008-12-24 03:38:04 +01:00
|
|
|
"(fd %d, %d secs old). We have a better canonical one "
|
|
|
|
"(fd %d; %d secs old).",
|
|
|
|
or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
|
|
|
|
(int)(now - or_conn->_base.timestamp_created),
|
|
|
|
best->_base.s, (int)(now - best->_base.timestamp_created));
|
|
|
|
or_conn->is_bad_for_new_circs = 1;
|
|
|
|
} else if (!tor_addr_compare(&or_conn->real_addr,
|
|
|
|
&best->real_addr, CMP_EXACT)) {
|
|
|
|
log_info(LD_OR,
|
2010-05-12 20:15:39 +02:00
|
|
|
"Marking OR conn to %s:%d as unsuitable for new circuits: "
|
|
|
|
"(fd %d, %d secs old). We have a better one with the "
|
|
|
|
"same address (fd %d; %d secs old).",
|
2008-12-24 03:38:04 +01:00
|
|
|
or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
|
|
|
|
(int)(now - or_conn->_base.timestamp_created),
|
|
|
|
best->_base.s, (int)(now - best->_base.timestamp_created));
|
|
|
|
or_conn->is_bad_for_new_circs = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-29 04:32:38 +02:00
|
|
|
/** Go through all the OR connections (or if <b>digest</b> is non-NULL, just
|
|
|
|
* the OR connections with that digest), and set the is_bad_for_new_circs
|
2010-09-29 05:27:00 +02:00
|
|
|
* flag based on the rules in connection_or_group_set_badness() (or just
|
|
|
|
* always set it if <b>force</b> is true).
|
2008-12-24 03:38:04 +01:00
|
|
|
*/
|
|
|
|
void
|
2010-09-29 04:32:38 +02:00
|
|
|
connection_or_set_bad_connections(const char *digest, int force)
|
2008-12-24 03:38:04 +01:00
|
|
|
{
|
|
|
|
if (!orconn_identity_map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DIGESTMAP_FOREACH(orconn_identity_map, identity, or_connection_t *, conn) {
|
2011-05-11 22:27:27 +02:00
|
|
|
if (!digest || tor_memeq(digest, conn->identity_digest, DIGEST_LEN))
|
2010-09-29 04:32:38 +02:00
|
|
|
connection_or_group_set_badness(conn, force);
|
2008-12-24 03:38:04 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
2005-11-30 04:01:16 +01:00
|
|
|
}
|
|
|
|
|
2008-09-09 08:25:39 +02:00
|
|
|
/** <b>conn</b> is in the 'connecting' state, and it failed to complete
|
|
|
|
* a TCP connection. Send notifications appropriately.
|
|
|
|
*
|
|
|
|
* <b>reason</b> specifies the or_conn_end_reason for the failure;
|
|
|
|
* <b>msg</b> specifies the strerror-style error message.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
connection_or_connect_failed(or_connection_t *conn,
|
|
|
|
int reason, const char *msg)
|
|
|
|
{
|
|
|
|
control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED, reason);
|
|
|
|
if (!authdir_mode_tests_reachability(get_options()))
|
|
|
|
control_event_bootstrap_problem(msg, reason);
|
|
|
|
}
|
|
|
|
|
2004-07-02 11:29:01 +02:00
|
|
|
/** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
|
|
|
|
* handshake with an OR with identity digest <b>id_digest</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-07-02 11:29:01 +02:00
|
|
|
* If <b>id_digest</b> is me, do nothing. If we're already connected to it,
|
2004-07-21 02:12:42 +02:00
|
|
|
* return that connection. If the connect() is in progress, set the
|
|
|
|
* new conn's state to 'connecting' and return it. If connect() succeeds,
|
2005-08-23 11:50:51 +02:00
|
|
|
* call connection_tls_start_handshake() on it.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:33:04 +02:00
|
|
|
* This function is called from router_retry_connections(), for
|
2004-05-07 10:53:40 +02:00
|
|
|
* ORs connecting to ORs, and circuit_establish_circuit(), for
|
|
|
|
* OPs connecting to ORs.
|
|
|
|
*
|
|
|
|
* Return the launched conn, or NULL if it failed.
|
|
|
|
*/
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *
|
2008-08-05 22:08:19 +02:00
|
|
|
connection_or_connect(const tor_addr_t *_addr, uint16_t port,
|
|
|
|
const char *id_digest)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *conn;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2008-06-11 03:14:23 +02:00
|
|
|
int socket_error = 0;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t addr;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2011-06-14 04:28:36 +02:00
|
|
|
int r;
|
|
|
|
tor_addr_t proxy_addr;
|
|
|
|
uint16_t proxy_port;
|
2011-07-03 06:13:41 +02:00
|
|
|
int proxy_type;
|
2011-06-14 04:28:36 +02:00
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_assert(_addr);
|
2004-07-02 11:29:01 +02:00
|
|
|
tor_assert(id_digest);
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_copy(&addr, _addr);
|
2003-05-28 04:03:25 +02:00
|
|
|
|
2006-06-05 10:02:04 +02:00
|
|
|
if (server_mode(options) && router_digest_is_me(id_digest)) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
|
2003-05-28 04:03:25 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-24 19:01:56 +01:00
|
|
|
conn = or_connection_new(tor_addr_family(&addr));
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
/* set up conn so it's got all the data we need to remember */
|
2008-08-05 22:08:19 +02:00
|
|
|
connection_or_init_conn_from_address(conn, &addr, port, id_digest, 1);
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.state = OR_CONN_STATE_CONNECTING;
|
2007-01-15 22:13:37 +01:00
|
|
|
control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2011-10-23 23:58:00 +02:00
|
|
|
conn->is_outgoing = 1;
|
|
|
|
|
2011-06-14 16:00:55 +02:00
|
|
|
/* If we are using a proxy server, find it and use it. */
|
2011-07-03 06:13:41 +02:00
|
|
|
r = get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, TO_CONN(conn));
|
|
|
|
if (r == 0) {
|
|
|
|
conn->proxy_type = proxy_type;
|
|
|
|
if (proxy_type != PROXY_NONE) {
|
|
|
|
tor_addr_copy(&addr, &proxy_addr);
|
|
|
|
port = proxy_port;
|
|
|
|
conn->_base.proxy_state = PROXY_INFANT;
|
|
|
|
}
|
|
|
|
} else {
|
2011-06-22 23:28:11 +02:00
|
|
|
log_warn(LD_GENERAL, "Tried to connect through proxy, but proxy address "
|
|
|
|
"could not be found.");
|
|
|
|
connection_free(TO_CONN(conn));
|
|
|
|
return NULL;
|
2005-02-24 11:56:55 +01:00
|
|
|
}
|
|
|
|
|
2008-06-11 03:14:23 +02:00
|
|
|
switch (connection_connect(TO_CONN(conn), conn->_base.address,
|
2008-08-05 22:08:19 +02:00
|
|
|
&addr, port, &socket_error)) {
|
2003-09-16 03:58:46 +02:00
|
|
|
case -1:
|
2005-12-25 00:32:15 +01:00
|
|
|
/* If the connection failed immediately, and we're using
|
2009-06-19 01:59:18 +02:00
|
|
|
* a proxy, our proxy is down. Don't blame the Tor server. */
|
2011-06-14 02:51:59 +02:00
|
|
|
if (conn->_base.proxy_state == PROXY_INFANT)
|
2009-02-05 00:27:35 +01:00
|
|
|
entry_guard_register_connect_status(conn->identity_digest,
|
|
|
|
0, 1, time(NULL));
|
2008-09-09 08:25:39 +02:00
|
|
|
connection_or_connect_failed(conn,
|
|
|
|
errno_to_orconn_end_reason(socket_error),
|
|
|
|
tor_socket_strerror(socket_error));
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_free(TO_CONN(conn));
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return NULL;
|
2003-09-16 03:58:46 +02:00
|
|
|
case 0:
|
2009-06-04 20:49:16 +02:00
|
|
|
connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT);
|
2003-08-14 19:13:52 +02:00
|
|
|
/* writable indicates finish, readable indicates broken link,
|
|
|
|
error indicates broken link on windows */
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return conn;
|
2003-09-16 03:58:46 +02:00
|
|
|
/* case 1: fall through */
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
2005-02-24 11:56:55 +01:00
|
|
|
if (connection_or_finished_connecting(conn) < 0) {
|
|
|
|
/* already marked for close */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return conn;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
|
|
|
|
* we initiated the connection, else it's 1.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2005-12-25 00:32:15 +01:00
|
|
|
* Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and
|
|
|
|
* pass <b>conn</b> to connection_tls_continue_handshake().
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:47:25 +02:00
|
|
|
* Return -1 if <b>conn</b> is broken, else return 0.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_tls_start_handshake(or_connection_t *conn, int receiving)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-11-05 19:15:44 +01:00
|
|
|
conn->_base.state = OR_CONN_STATE_TLS_HANDSHAKING;
|
2009-08-14 20:34:16 +02:00
|
|
|
tor_assert(!conn->tls);
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->tls = tor_tls_new(conn->_base.s, receiving);
|
2011-12-07 01:49:19 +01:00
|
|
|
tor_tls_set_logged_address(conn->tls, // XXX client and relay?
|
|
|
|
escaped_safe_str(conn->_base.address));
|
2005-10-06 07:08:00 +02:00
|
|
|
if (!conn->tls) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_BUG,"tor_tls_new failed. Closing.");
|
2003-09-30 20:45:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-08-14 20:34:16 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
if (connection_type_uses_bufferevent(TO_CONN(conn))) {
|
2010-11-09 21:36:27 +01:00
|
|
|
const int filtering = get_options()->_UseFilteringSSLBufferevents;
|
2009-08-14 20:34:16 +02:00
|
|
|
struct bufferevent *b =
|
|
|
|
tor_tls_init_bufferevent(conn->tls, conn->_base.bufev, conn->_base.s,
|
2010-11-09 21:36:27 +01:00
|
|
|
receiving, filtering);
|
2009-08-14 20:34:16 +02:00
|
|
|
if (!b) {
|
|
|
|
log_warn(LD_BUG,"tor_tls_init_bufferevent failed. Closing.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
conn->_base.bufev = b;
|
2010-02-22 19:59:34 +01:00
|
|
|
if (conn->bucket_cfg)
|
2011-08-24 23:09:56 +02:00
|
|
|
tor_set_bufferevent_rate_limit(conn->_base.bufev, conn->bucket_cfg);
|
2010-02-22 19:59:34 +01:00
|
|
|
connection_enable_rate_limiting(TO_CONN(conn));
|
2010-10-12 21:48:17 +02:00
|
|
|
|
|
|
|
connection_configure_bufferevent_callbacks(TO_CONN(conn));
|
|
|
|
bufferevent_setcb(b,
|
|
|
|
connection_handle_read_cb,
|
2009-08-14 20:34:16 +02:00
|
|
|
connection_handle_write_cb,
|
2010-10-12 21:48:17 +02:00
|
|
|
connection_or_handle_event_cb,/* overriding this one*/
|
2009-08-14 20:34:16 +02:00
|
|
|
TO_CONN(conn));
|
|
|
|
}
|
|
|
|
#endif
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_start_reading(TO_CONN(conn));
|
2009-09-24 18:31:22 +02:00
|
|
|
log_debug(LD_HANDSHAKE,"starting TLS handshake on fd %d", conn->_base.s);
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(receiving ? TLS_HANDSHAKE_S : TLS_HANDSHAKE_C);
|
|
|
|
|
2009-08-14 20:34:16 +02:00
|
|
|
IF_HAS_BUFFEREVENT(TO_CONN(conn), {
|
|
|
|
/* ???? */;
|
|
|
|
}) ELSE_IF_NO_BUFFEREVENT {
|
|
|
|
if (connection_tls_continue_handshake(conn) < 0)
|
|
|
|
return -1;
|
2004-03-06 02:43:37 +01:00
|
|
|
}
|
2003-09-30 20:45:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Invoked on the server side from inside tor_tls_read() when the server
|
|
|
|
* gets a successful TLS renegotiation from the client. */
|
2007-12-01 09:47:13 +01:00
|
|
|
static void
|
|
|
|
connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
|
|
|
|
{
|
|
|
|
or_connection_t *conn = _conn;
|
2008-01-13 01:20:47 +01:00
|
|
|
(void)tls;
|
2007-12-01 09:47:13 +01:00
|
|
|
|
2008-01-13 01:20:47 +01:00
|
|
|
if (connection_tls_finish_handshake(conn) < 0) {
|
2008-02-05 22:39:46 +01:00
|
|
|
/* XXXX_TLS double-check that it's ok to do this from inside read. */
|
2008-02-06 22:53:13 +01:00
|
|
|
/* XXXX_TLS double-check that this verifies certificates. */
|
2008-01-13 01:20:47 +01:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
}
|
2007-12-01 09:47:13 +01:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Move forward with the tls handshake. If it finishes, hand
|
|
|
|
* <b>conn</b> to connection_tls_finish_handshake().
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:47:25 +02:00
|
|
|
* Return -1 if <b>conn</b> is broken, else return 0.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_tls_continue_handshake(or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-11-07 18:44:15 +01:00
|
|
|
int result;
|
2005-04-23 16:26:02 +02:00
|
|
|
check_no_tls_errors();
|
2007-12-01 09:09:48 +01:00
|
|
|
again:
|
2008-02-06 22:53:13 +01:00
|
|
|
if (conn->_base.state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
|
|
|
|
// log_notice(LD_OR, "Renegotiate with %p", conn->tls);
|
2007-12-01 09:09:48 +01:00
|
|
|
result = tor_tls_renegotiate(conn->tls);
|
2008-02-06 22:53:13 +01:00
|
|
|
// log_notice(LD_OR, "Result: %d", result);
|
|
|
|
} else {
|
2008-01-13 01:20:47 +01:00
|
|
|
tor_assert(conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING);
|
2008-02-06 22:53:13 +01:00
|
|
|
// log_notice(LD_OR, "Continue handshake with %p", conn->tls);
|
2007-12-01 09:09:48 +01:00
|
|
|
result = tor_tls_handshake(conn->tls);
|
2008-02-06 22:53:13 +01:00
|
|
|
// log_notice(LD_OR, "Result: %d", result);
|
2008-01-13 01:20:47 +01:00
|
|
|
}
|
2007-11-07 18:44:15 +01:00
|
|
|
switch (result) {
|
2007-01-15 22:21:05 +01:00
|
|
|
CASE_TOR_TLS_ERROR_ANY:
|
2007-11-07 18:44:15 +01:00
|
|
|
log_info(LD_OR,"tls error [%s]. breaking connection.",
|
|
|
|
tor_tls_err_to_string(result));
|
2003-09-30 20:45:55 +02:00
|
|
|
return -1;
|
|
|
|
case TOR_TLS_DONE:
|
2007-12-05 17:11:33 +01:00
|
|
|
if (! tor_tls_used_v1_handshake(conn->tls)) {
|
2007-12-01 09:47:13 +01:00
|
|
|
if (!tor_tls_is_server(conn->tls)) {
|
|
|
|
if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
|
2011-09-27 19:15:36 +02:00
|
|
|
if (tor_tls_received_v3_certificate(conn->tls)) {
|
2011-10-11 04:24:33 +02:00
|
|
|
log_info(LD_OR, "Client got a v3 cert! Moving on to v3 "
|
2011-09-27 19:15:36 +02:00
|
|
|
"handshake.");
|
|
|
|
return connection_or_launch_v3_or_handshake(conn);
|
|
|
|
} else {
|
2011-10-11 17:30:01 +02:00
|
|
|
log_debug(LD_OR, "Done with initial SSL handshake (client-side)."
|
|
|
|
" Requesting renegotiation.");
|
2011-09-27 19:15:36 +02:00
|
|
|
conn->_base.state = OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING;
|
|
|
|
goto again;
|
|
|
|
}
|
2007-12-01 09:47:13 +01:00
|
|
|
}
|
2008-02-06 22:53:13 +01:00
|
|
|
// log_notice(LD_OR,"Done. state was %d.", conn->_base.state);
|
2007-12-01 09:47:13 +01:00
|
|
|
} else {
|
2011-09-27 19:15:36 +02:00
|
|
|
/* v2/v3 handshake, but not a client. */
|
2011-06-17 09:31:59 +02:00
|
|
|
log_debug(LD_OR, "Done with initial SSL handshake (server-side). "
|
2011-09-27 19:15:36 +02:00
|
|
|
"Expecting renegotiation or VERSIONS cell");
|
2011-12-07 01:49:20 +01:00
|
|
|
tor_tls_set_renegotiate_callback(conn->tls,
|
2011-12-07 01:49:19 +01:00
|
|
|
connection_or_tls_renegotiated_cb,
|
|
|
|
conn);
|
2008-01-13 01:20:47 +01:00
|
|
|
conn->_base.state = OR_CONN_STATE_TLS_SERVER_RENEGOTIATING;
|
|
|
|
connection_stop_writing(TO_CONN(conn));
|
|
|
|
connection_start_reading(TO_CONN(conn));
|
|
|
|
return 0;
|
2007-12-01 09:47:13 +01:00
|
|
|
}
|
2007-12-01 09:09:48 +01:00
|
|
|
}
|
2007-01-15 22:21:05 +01:00
|
|
|
return connection_tls_finish_handshake(conn);
|
2003-09-30 20:45:55 +02:00
|
|
|
case TOR_TLS_WANTWRITE:
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_start_writing(TO_CONN(conn));
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"wanted write");
|
2003-09-30 20:45:55 +02:00
|
|
|
return 0;
|
|
|
|
case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"wanted read");
|
2003-09-30 20:45:55 +02:00
|
|
|
return 0;
|
2007-01-15 22:21:05 +01:00
|
|
|
case TOR_TLS_CLOSE:
|
|
|
|
log_info(LD_OR,"tls closed. breaking connection.");
|
|
|
|
return -1;
|
2003-09-30 20:45:55 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-14 20:34:16 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
static void
|
|
|
|
connection_or_handle_event_cb(struct bufferevent *bufev, short event,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct or_connection_t *conn = TO_OR_CONN(arg);
|
|
|
|
|
|
|
|
/* XXXX cut-and-paste code; should become a function. */
|
|
|
|
if (event & BEV_EVENT_CONNECTED) {
|
|
|
|
if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
|
|
|
|
if (tor_tls_finish_handshake(conn->tls) < 0) {
|
|
|
|
log_warn(LD_OR, "Problem finishing handshake");
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! tor_tls_used_v1_handshake(conn->tls)) {
|
|
|
|
if (!tor_tls_is_server(conn->tls)) {
|
|
|
|
if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
|
2011-09-27 19:15:36 +02:00
|
|
|
if (tor_tls_received_v3_certificate(conn->tls)) {
|
2011-10-11 04:24:33 +02:00
|
|
|
log_info(LD_OR, "Client got a v3 cert!");
|
2011-09-27 19:15:36 +02:00
|
|
|
if (connection_or_launch_v3_or_handshake(conn) < 0)
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
conn->_base.state = OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING;
|
|
|
|
tor_tls_unblock_renegotiation(conn->tls);
|
|
|
|
if (bufferevent_ssl_renegotiate(conn->_base.bufev)<0) {
|
|
|
|
log_warn(LD_OR, "Start_renegotiating went badly.");
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
}
|
|
|
|
tor_tls_unblock_renegotiation(conn->tls);
|
|
|
|
return; /* ???? */
|
2009-08-14 20:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-11 19:45:27 +02:00
|
|
|
} else if (tor_tls_get_num_server_handshakes(conn->tls) == 1) {
|
2011-09-27 19:15:36 +02:00
|
|
|
/* v2 or v3 handshake, as a server. Only got one handshake, so
|
2010-10-11 19:45:27 +02:00
|
|
|
* wait for the next one. */
|
2011-12-07 01:49:20 +01:00
|
|
|
tor_tls_set_renegotiate_callback(conn->tls,
|
2011-12-07 01:49:19 +01:00
|
|
|
connection_or_tls_renegotiated_cb,
|
|
|
|
conn);
|
2009-08-14 20:34:16 +02:00
|
|
|
conn->_base.state = OR_CONN_STATE_TLS_SERVER_RENEGOTIATING;
|
|
|
|
/* return 0; */
|
|
|
|
return; /* ???? */
|
2010-10-11 19:45:27 +02:00
|
|
|
} else {
|
|
|
|
const int handshakes = tor_tls_get_num_server_handshakes(conn->tls);
|
|
|
|
tor_assert(handshakes >= 2);
|
|
|
|
if (handshakes == 2) {
|
2011-09-27 19:15:36 +02:00
|
|
|
/* v2 handshake, as a server. Two handshakes happened already,
|
2010-10-11 19:45:27 +02:00
|
|
|
* so we treat renegotiation as done.
|
|
|
|
*/
|
|
|
|
connection_or_tls_renegotiated_cb(conn->tls, conn);
|
|
|
|
} else {
|
|
|
|
log_warn(LD_OR, "More than two handshakes done on connection. "
|
|
|
|
"Closing.");
|
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
}
|
|
|
|
return;
|
2009-08-14 20:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
connection_watch_events(TO_CONN(conn), READ_EVENT|WRITE_EVENT);
|
|
|
|
if (connection_tls_finish_handshake(conn) < 0)
|
|
|
|
connection_mark_for_close(TO_CONN(conn)); /* ???? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-11 19:25:41 +02:00
|
|
|
if (event & BEV_EVENT_ERROR) {
|
|
|
|
unsigned long err;
|
|
|
|
while ((err = bufferevent_get_openssl_error(bufev))) {
|
|
|
|
tor_tls_log_one_error(conn->tls, err, LOG_WARN, LD_OR,
|
|
|
|
"handshaking (with bufferevent)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-14 20:34:16 +02:00
|
|
|
connection_handle_event_cb(bufev, event, arg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Return 1 if we initiated this connection, or 0 if it started
|
|
|
|
* out as an incoming connection.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_nonopen_was_started_here(or_connection_t *conn)
|
2004-10-13 22:05:57 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.type == CONN_TYPE_OR);
|
2006-06-07 09:11:42 +02:00
|
|
|
if (!conn->tls)
|
|
|
|
return 1; /* it's still in proxy states or something */
|
2007-11-14 21:01:12 +01:00
|
|
|
if (conn->handshake_state)
|
|
|
|
return conn->handshake_state->started_here;
|
2006-06-07 09:11:42 +02:00
|
|
|
return !tor_tls_is_server(conn->tls);
|
2004-07-21 04:25:14 +02:00
|
|
|
}
|
|
|
|
|
2011-09-27 19:15:36 +02:00
|
|
|
/** Set the circid_type field of <b>conn</b> (which determines which part of
|
|
|
|
* the circuit ID space we're willing to use) based on comparing our ID to
|
|
|
|
* <b>identity_rcvd</b> */
|
|
|
|
void
|
|
|
|
connection_or_set_circid_type(or_connection_t *conn,
|
|
|
|
crypto_pk_env_t *identity_rcvd)
|
|
|
|
{
|
|
|
|
const int started_here = connection_or_nonopen_was_started_here(conn);
|
|
|
|
crypto_pk_env_t *our_identity =
|
|
|
|
started_here ? get_tlsclient_identity_key() :
|
|
|
|
get_server_identity_key();
|
|
|
|
|
|
|
|
if (identity_rcvd) {
|
|
|
|
if (crypto_pk_cmp_keys(our_identity, identity_rcvd)<0) {
|
|
|
|
conn->circ_id_type = CIRC_ID_TYPE_LOWER;
|
|
|
|
} else {
|
|
|
|
conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conn->circ_id_type = CIRC_ID_TYPE_NEITHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-12 22:39:33 +01:00
|
|
|
/** <b>Conn</b> just completed its handshake. Return 0 if all is well, and
|
2005-06-21 01:04:13 +02:00
|
|
|
* return -1 if he is lying, broken, or otherwise something is wrong.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2007-02-12 22:39:33 +01:00
|
|
|
* If we initiated this connection (<b>started_here</b> is true), make sure
|
2009-12-18 12:55:05 +01:00
|
|
|
* the other side sent a correctly formed certificate. If I initiated the
|
2007-02-12 22:39:33 +01:00
|
|
|
* connection, make sure it's the right guy.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2007-02-12 22:39:33 +01:00
|
|
|
* Otherwise (if we _didn't_ initiate this connection), it's okay for
|
|
|
|
* the certificate to be weird or absent.
|
|
|
|
*
|
|
|
|
* If we return 0, and the certificate is as expected, write a hash of the
|
2010-06-04 02:29:29 +02:00
|
|
|
* identity key into <b>digest_rcvd_out</b>, which must have DIGEST_LEN
|
|
|
|
* space in it.
|
|
|
|
* If the certificate is invalid or missing on an incoming connection,
|
|
|
|
* we return 0 and set <b>digest_rcvd_out</b> to DIGEST_LEN NUL bytes.
|
|
|
|
* (If we return -1, the contents of this buffer are undefined.)
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2005-06-21 01:04:13 +02:00
|
|
|
* As side effects,
|
2006-07-04 05:19:59 +02:00
|
|
|
* 1) Set conn->circ_id_type according to tor-spec.txt.
|
2005-06-21 01:04:13 +02:00
|
|
|
* 2) If we're an authdirserver and we initiated the connection: drop all
|
|
|
|
* descriptors that claim to be on that IP/port but that aren't
|
|
|
|
* this guy; and note that this guy is reachable.
|
2010-06-04 02:29:29 +02:00
|
|
|
* 3) If this is a bridge and we didn't configure its identity
|
|
|
|
* fingerprint, remember the keyid we just learned.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2005-06-21 03:08:01 +02:00
|
|
|
static int
|
2007-11-06 19:00:07 +01:00
|
|
|
connection_or_check_valid_tls_handshake(or_connection_t *conn,
|
|
|
|
int started_here,
|
|
|
|
char *digest_rcvd_out)
|
2005-06-21 03:08:01 +02:00
|
|
|
{
|
2004-07-21 02:44:04 +02:00
|
|
|
crypto_pk_env_t *identity_rcvd=NULL;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2005-10-17 03:29:28 +02:00
|
|
|
int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
|
2007-01-23 20:22:49 +01:00
|
|
|
const char *safe_address =
|
2009-09-28 15:08:32 +02:00
|
|
|
started_here ? conn->_base.address :
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(conn->_base.address);
|
2007-02-12 22:39:33 +01:00
|
|
|
const char *conn_type = started_here ? "outgoing" : "incoming";
|
2011-09-27 19:15:36 +02:00
|
|
|
int has_cert = 0;
|
2003-09-30 20:45:55 +02:00
|
|
|
|
2005-04-23 16:26:02 +02:00
|
|
|
check_no_tls_errors();
|
2007-02-12 22:39:33 +01:00
|
|
|
has_cert = tor_tls_peer_has_cert(conn->tls);
|
|
|
|
if (started_here && !has_cert) {
|
2009-09-24 18:31:22 +02:00
|
|
|
log_info(LD_HANDSHAKE,"Tried connecting to router at %s:%d, but it didn't "
|
2007-02-12 22:39:33 +01:00
|
|
|
"send a cert! Closing.",
|
|
|
|
safe_address, conn->_base.port);
|
2003-11-18 08:25:04 +01:00
|
|
|
return -1;
|
2007-02-12 22:39:33 +01:00
|
|
|
} else if (!has_cert) {
|
2009-09-24 18:31:22 +02:00
|
|
|
log_debug(LD_HANDSHAKE,"Got incoming connection with no certificate. "
|
2007-02-12 22:39:33 +01:00
|
|
|
"That's ok.");
|
2003-11-18 08:25:04 +01:00
|
|
|
}
|
2005-04-23 16:26:02 +02:00
|
|
|
check_no_tls_errors();
|
2004-07-21 04:25:14 +02:00
|
|
|
|
2007-12-01 09:09:48 +01:00
|
|
|
if (has_cert) {
|
2008-02-12 21:20:52 +01:00
|
|
|
int v = tor_tls_verify(started_here?severity:LOG_INFO,
|
|
|
|
conn->tls, &identity_rcvd);
|
2007-12-01 09:09:48 +01:00
|
|
|
if (started_here && v<0) {
|
2009-09-24 18:31:22 +02:00
|
|
|
log_fn(severity,LD_HANDSHAKE,"Tried connecting to router at %s:%d: It"
|
2007-12-01 09:09:48 +01:00
|
|
|
" has a cert but it's invalid. Closing.",
|
|
|
|
safe_address, conn->_base.port);
|
2007-11-14 21:01:12 +01:00
|
|
|
return -1;
|
2007-12-01 09:09:48 +01:00
|
|
|
} else if (v<0) {
|
2009-09-24 18:31:22 +02:00
|
|
|
log_info(LD_HANDSHAKE,"Incoming connection gave us an invalid cert "
|
2007-12-01 09:09:48 +01:00
|
|
|
"chain; ignoring.");
|
|
|
|
} else {
|
2009-09-24 18:31:22 +02:00
|
|
|
log_debug(LD_HANDSHAKE,
|
|
|
|
"The certificate seems to be valid on %s connection "
|
2007-12-01 09:09:48 +01:00
|
|
|
"with %s:%d", conn_type, safe_address, conn->_base.port);
|
2007-02-12 22:39:33 +01:00
|
|
|
}
|
2007-12-01 09:09:48 +01:00
|
|
|
check_no_tls_errors();
|
2003-11-18 08:25:04 +01:00
|
|
|
}
|
2004-07-21 04:25:14 +02:00
|
|
|
|
2007-02-12 22:39:33 +01:00
|
|
|
if (identity_rcvd) {
|
2007-11-06 19:00:07 +01:00
|
|
|
crypto_pk_get_digest(identity_rcvd, digest_rcvd_out);
|
2004-11-10 21:14:37 +01:00
|
|
|
} else {
|
2007-11-06 19:00:07 +01:00
|
|
|
memset(digest_rcvd_out, 0, DIGEST_LEN);
|
2004-07-21 02:44:04 +02:00
|
|
|
}
|
2004-04-25 00:17:50 +02:00
|
|
|
|
2011-09-27 19:15:36 +02:00
|
|
|
connection_or_set_circid_type(conn, identity_rcvd);
|
|
|
|
crypto_free_pk_env(identity_rcvd);
|
|
|
|
|
|
|
|
if (started_here)
|
|
|
|
return connection_or_client_learned_peer_id(conn,
|
|
|
|
(const uint8_t*)digest_rcvd_out);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when we (as a connection initiator) have definitively,
|
|
|
|
* authenticatedly, learned that ID of the Tor instance on the other
|
|
|
|
* side of <b>conn</b> is <b>peer_id</b>. For v1 and v2 handshakes,
|
|
|
|
* this is right after we get a certificate chain in a TLS handshake
|
|
|
|
* or renegotiation. For v3 handshakes, this is right after we get a
|
2011-10-31 09:33:38 +01:00
|
|
|
* certificate chain in a CERTS cell.
|
2011-09-27 19:15:36 +02:00
|
|
|
*
|
|
|
|
* If we want any particular ID before, record the one we got.
|
|
|
|
*
|
|
|
|
* If we wanted an ID, but we didn't get it, log a warning and return -1.
|
|
|
|
*
|
|
|
|
* If we're testing reachability, remember what we learned.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
connection_or_client_learned_peer_id(or_connection_t *conn,
|
|
|
|
const uint8_t *peer_id)
|
|
|
|
{
|
|
|
|
int as_expected = 1;
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
|
|
|
|
|
|
|
|
if (tor_digest_is_zero(conn->identity_digest)) {
|
|
|
|
connection_or_set_identity_digest(conn, (const char*)peer_id);
|
2007-11-03 15:44:53 +01:00
|
|
|
tor_free(conn->nickname);
|
2007-06-15 08:01:04 +02:00
|
|
|
conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
|
|
|
|
conn->nickname[0] = '$';
|
|
|
|
base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
|
|
|
|
conn->identity_digest, DIGEST_LEN);
|
2009-09-24 18:31:22 +02:00
|
|
|
log_info(LD_HANDSHAKE, "Connected to router %s at %s:%d without knowing "
|
2007-06-15 08:01:04 +02:00
|
|
|
"its key. Hoping for the best.",
|
|
|
|
conn->nickname, conn->_base.address, conn->_base.port);
|
2010-06-04 02:29:29 +02:00
|
|
|
/* if it's a bridge and we didn't know its identity fingerprint, now
|
|
|
|
* we do -- remember it for future attempts. */
|
|
|
|
learned_router_identity(&conn->_base.addr, conn->_base.port,
|
2011-09-27 19:15:36 +02:00
|
|
|
(const char*)peer_id);
|
2007-06-15 08:01:04 +02:00
|
|
|
}
|
|
|
|
|
2011-09-27 19:15:36 +02:00
|
|
|
if (tor_memneq(peer_id, conn->identity_digest, DIGEST_LEN)) {
|
|
|
|
/* I was aiming for a particular digest. I didn't get it! */
|
|
|
|
char seen[HEX_DIGEST_LEN+1];
|
|
|
|
char expected[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(seen, sizeof(seen), (const char*)peer_id, DIGEST_LEN);
|
|
|
|
base16_encode(expected, sizeof(expected), conn->identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
log_fn(severity, LD_HANDSHAKE,
|
|
|
|
"Tried connecting to router at %s:%d, but identity key was not "
|
|
|
|
"as expected: wanted %s but got %s.",
|
|
|
|
conn->_base.address, conn->_base.port, expected, seen);
|
|
|
|
entry_guard_register_connect_status(conn->identity_digest, 0, 1,
|
|
|
|
time(NULL));
|
|
|
|
control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED,
|
|
|
|
END_OR_CONN_REASON_OR_IDENTITY);
|
|
|
|
if (!authdir_mode_tests_reachability(options))
|
2011-12-02 22:27:33 +01:00
|
|
|
control_event_bootstrap_problem(
|
|
|
|
"Unexpected identity in router certificate",
|
|
|
|
END_OR_CONN_REASON_OR_IDENTITY);
|
2011-09-27 19:15:36 +02:00
|
|
|
as_expected = 0;
|
|
|
|
}
|
|
|
|
if (authdir_mode_tests_reachability(options)) {
|
|
|
|
dirserv_orconn_tls_done(conn->_base.address, conn->_base.port,
|
|
|
|
(const char*)peer_id, as_expected);
|
2005-06-21 01:04:13 +02:00
|
|
|
}
|
2011-09-27 19:15:36 +02:00
|
|
|
if (!as_expected)
|
|
|
|
return -1;
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:15:36 +02:00
|
|
|
/** The v1/v2 TLS handshake is finished.
|
2005-06-21 01:04:13 +02:00
|
|
|
*
|
|
|
|
* Make sure we are happy with the person we just handshaked with.
|
|
|
|
*
|
|
|
|
* If he initiated the connection, make sure he's not already connected,
|
|
|
|
* then initialize conn from the information in router.
|
|
|
|
*
|
|
|
|
* If all is successful, call circuit_n_conn_done() to handle events
|
2007-01-23 20:22:49 +01:00
|
|
|
* that have been pending on the <tls handshake completion. Also set the
|
2005-06-21 01:04:13 +02:00
|
|
|
* directory to be dirty (only matters if I'm an authdirserver).
|
2011-09-27 19:15:36 +02:00
|
|
|
*
|
|
|
|
* If this is a v2 TLS handshake, send a versions cell.
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_tls_finish_handshake(or_connection_t *conn)
|
2005-06-21 01:04:13 +02:00
|
|
|
{
|
|
|
|
char digest_rcvd[DIGEST_LEN];
|
2005-12-28 08:19:55 +01:00
|
|
|
int started_here = connection_or_nonopen_was_started_here(conn);
|
2005-06-21 01:04:13 +02:00
|
|
|
|
2010-10-11 19:45:27 +02:00
|
|
|
log_debug(LD_HANDSHAKE,"%s tls handshake on %p with %s done. verifying.",
|
|
|
|
started_here?"outgoing":"incoming",
|
|
|
|
conn,
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(conn->_base.address));
|
2004-04-25 00:17:50 +02:00
|
|
|
|
2004-05-07 10:53:40 +02:00
|
|
|
directory_set_dirty();
|
2007-10-30 22:46:02 +01:00
|
|
|
|
2008-01-13 01:20:47 +01:00
|
|
|
if (connection_or_check_valid_tls_handshake(conn, started_here,
|
|
|
|
digest_rcvd) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-09-01 03:10:27 +02:00
|
|
|
circuit_build_times_network_is_live(&circ_times);
|
|
|
|
|
2007-10-30 22:46:02 +01:00
|
|
|
if (tor_tls_used_v1_handshake(conn->tls)) {
|
|
|
|
conn->link_proto = 1;
|
2007-11-07 18:11:23 +01:00
|
|
|
if (!started_here) {
|
2008-08-05 22:08:19 +02:00
|
|
|
connection_or_init_conn_from_address(conn, &conn->_base.addr,
|
2007-11-07 18:11:23 +01:00
|
|
|
conn->_base.port, digest_rcvd, 0);
|
|
|
|
}
|
2007-10-30 22:46:02 +01:00
|
|
|
return connection_or_set_state_open(conn);
|
|
|
|
} else {
|
2011-09-13 16:03:09 +02:00
|
|
|
conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING_V2;
|
2007-11-05 19:15:50 +01:00
|
|
|
if (connection_init_or_handshake_state(conn, started_here) < 0)
|
2007-11-05 19:15:47 +01:00
|
|
|
return -1;
|
2008-02-09 00:41:29 +01:00
|
|
|
if (!started_here) {
|
2008-08-05 22:08:19 +02:00
|
|
|
connection_or_init_conn_from_address(conn, &conn->_base.addr,
|
2008-02-09 00:41:29 +01:00
|
|
|
conn->_base.port, digest_rcvd, 0);
|
|
|
|
}
|
2011-09-27 19:15:36 +02:00
|
|
|
return connection_or_send_versions(conn, 0);
|
2007-10-30 22:46:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:15:36 +02:00
|
|
|
/**
|
|
|
|
* Called as client when initial TLS handshake is done, and we notice
|
|
|
|
* that we got a v3-handshake signalling certificate from the server.
|
|
|
|
* Set up structures, do bookkeeping, and send the versions cell.
|
|
|
|
* Return 0 on success and -1 on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
connection_or_launch_v3_or_handshake(or_connection_t *conn)
|
|
|
|
{
|
|
|
|
tor_assert(connection_or_nonopen_was_started_here(conn));
|
|
|
|
tor_assert(tor_tls_received_v3_certificate(conn->tls));
|
|
|
|
|
|
|
|
circuit_build_times_network_is_live(&circ_times);
|
|
|
|
|
|
|
|
conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
|
|
|
|
if (connection_init_or_handshake_state(conn, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return connection_or_send_versions(conn, 1);
|
|
|
|
}
|
|
|
|
|
2008-02-11 02:09:24 +01:00
|
|
|
/** Allocate a new connection handshake state for the connection
|
|
|
|
* <b>conn</b>. Return 0 on success, -1 on failure. */
|
2011-09-27 19:15:36 +02:00
|
|
|
int
|
2007-11-05 19:15:50 +01:00
|
|
|
connection_init_or_handshake_state(or_connection_t *conn, int started_here)
|
|
|
|
{
|
|
|
|
or_handshake_state_t *s;
|
|
|
|
s = conn->handshake_state = tor_malloc_zero(sizeof(or_handshake_state_t));
|
|
|
|
s->started_here = started_here ? 1 : 0;
|
2011-09-28 16:31:56 +02:00
|
|
|
s->digest_sent_data = 1;
|
|
|
|
s->digest_received_data = 1;
|
2007-11-05 19:15:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-05 19:15:47 +01:00
|
|
|
|
2008-02-11 02:09:24 +01:00
|
|
|
/** Free all storage held by <b>state</b>. */
|
2007-11-05 19:15:44 +01:00
|
|
|
void
|
|
|
|
or_handshake_state_free(or_handshake_state_t *state)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!state)
|
|
|
|
return;
|
2011-09-17 00:32:11 +02:00
|
|
|
crypto_free_digest_env(state->digest_sent);
|
|
|
|
crypto_free_digest_env(state->digest_received);
|
|
|
|
tor_cert_free(state->auth_cert);
|
|
|
|
tor_cert_free(state->id_cert);
|
2007-11-05 19:15:47 +01:00
|
|
|
memset(state, 0xBE, sizeof(or_handshake_state_t));
|
2007-11-05 19:15:44 +01:00
|
|
|
tor_free(state);
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:32:11 +02:00
|
|
|
/**
|
|
|
|
* Remember that <b>cell</b> has been transmitted (if <b>incoming</b> is
|
|
|
|
* false) or received (if <b>incoming is true) during a V3 handshake using
|
|
|
|
* <b>state</b>.
|
|
|
|
*
|
|
|
|
* (We don't record the cell, but we keep a digest of everything sent or
|
|
|
|
* received during the v3 handshake, and the client signs it in an
|
|
|
|
* authenticate cell.)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
or_handshake_state_record_cell(or_handshake_state_t *state,
|
|
|
|
const cell_t *cell,
|
|
|
|
int incoming)
|
|
|
|
{
|
|
|
|
crypto_digest_env_t *d, **dptr;
|
|
|
|
packed_cell_t packed;
|
2011-09-28 16:31:56 +02:00
|
|
|
if (incoming) {
|
|
|
|
if (!state->digest_received_data)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!state->digest_sent_data)
|
|
|
|
return;
|
|
|
|
}
|
2011-09-17 00:32:11 +02:00
|
|
|
if (!incoming) {
|
|
|
|
log_warn(LD_BUG, "We shouldn't be sending any non-variable-length cells "
|
2011-09-27 20:39:57 +02:00
|
|
|
"while making a handshake digest. But we think we are sending "
|
|
|
|
"one with type %d.", (int)cell->command);
|
2011-09-17 00:32:11 +02:00
|
|
|
}
|
|
|
|
dptr = incoming ? &state->digest_received : &state->digest_sent;
|
|
|
|
if (! *dptr)
|
|
|
|
*dptr = crypto_new_digest256_env(DIGEST_SHA256);
|
|
|
|
|
|
|
|
d = *dptr;
|
|
|
|
/* Re-packing like this is a little inefficient, but we don't have to do
|
|
|
|
this very often at all. */
|
|
|
|
cell_pack(&packed, cell);
|
|
|
|
crypto_digest_add_bytes(d, packed.body, sizeof(packed.body));
|
|
|
|
memset(&packed, 0, sizeof(packed));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remember that a variable-length <b>cell</b> has been transmitted (if
|
|
|
|
* <b>incoming</b> is false) or received (if <b>incoming is true) during a V3
|
|
|
|
* handshake using <b>state</b>.
|
|
|
|
*
|
|
|
|
* (We don't record the cell, but we keep a digest of everything sent or
|
|
|
|
* received during the v3 handshake, and the client signs it in an
|
|
|
|
* authenticate cell.)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
or_handshake_state_record_var_cell(or_handshake_state_t *state,
|
|
|
|
const var_cell_t *cell,
|
|
|
|
int incoming)
|
|
|
|
{
|
|
|
|
crypto_digest_env_t *d, **dptr;
|
|
|
|
char buf[VAR_CELL_HEADER_SIZE];
|
2011-09-28 16:31:56 +02:00
|
|
|
if (incoming) {
|
|
|
|
if (!state->digest_received_data)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!state->digest_sent_data)
|
|
|
|
return;
|
|
|
|
}
|
2011-09-17 00:32:11 +02:00
|
|
|
dptr = incoming ? &state->digest_received : &state->digest_sent;
|
|
|
|
if (! *dptr)
|
|
|
|
*dptr = crypto_new_digest256_env(DIGEST_SHA256);
|
|
|
|
|
|
|
|
d = *dptr;
|
|
|
|
|
|
|
|
var_cell_pack_header(cell, buf);
|
|
|
|
crypto_digest_add_bytes(d, buf, sizeof(buf));
|
|
|
|
crypto_digest_add_bytes(d, (const char *)cell->payload, cell->payload_len);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2008-02-11 02:09:24 +01:00
|
|
|
/** Set <b>conn</b>'s state to OR_CONN_STATE_OPEN, and tell other subsystems
|
|
|
|
* as appropriate. Called when we are done with all TLS and OR handshaking.
|
|
|
|
*/
|
2007-10-30 22:46:02 +01:00
|
|
|
int
|
|
|
|
connection_or_set_state_open(or_connection_t *conn)
|
|
|
|
{
|
|
|
|
int started_here = connection_or_nonopen_was_started_here(conn);
|
2007-12-17 23:44:11 +01:00
|
|
|
time_t now = time(NULL);
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.state = OR_CONN_STATE_OPEN;
|
2007-01-15 22:13:37 +01:00
|
|
|
control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED, 0);
|
2007-10-30 22:46:02 +01:00
|
|
|
|
2005-12-28 08:19:55 +01:00
|
|
|
if (started_here) {
|
2009-09-01 03:10:27 +02:00
|
|
|
circuit_build_times_network_is_live(&circ_times);
|
2007-12-17 23:44:11 +01:00
|
|
|
rep_hist_note_connect_succeeded(conn->identity_digest, now);
|
|
|
|
if (entry_guard_register_connect_status(conn->identity_digest,
|
2009-02-05 00:27:35 +01:00
|
|
|
1, 0, now) < 0) {
|
2008-04-22 20:38:25 +02:00
|
|
|
/* Close any circuits pending on this conn. We leave it in state
|
|
|
|
* 'open' though, because it didn't actually *fail* -- we just
|
|
|
|
* chose not to use it. (Otherwise
|
|
|
|
* connection_about_to_close_connection() will call a big pile of
|
|
|
|
* functions to indicate we shouldn't try it again.) */
|
2008-06-11 01:00:11 +02:00
|
|
|
log_debug(LD_OR, "New entry guard was reachable, but closing this "
|
|
|
|
"connection so we can retry the earlier entry guards.");
|
2008-04-22 20:38:25 +02:00
|
|
|
circuit_n_conn_done(conn, 0);
|
2005-12-28 08:19:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2006-03-18 02:24:04 +01:00
|
|
|
router_set_status(conn->identity_digest, 1);
|
2007-12-17 23:44:11 +01:00
|
|
|
} else {
|
2007-12-25 01:07:13 +01:00
|
|
|
/* only report it to the geoip module if it's not a known router */
|
2010-10-14 17:49:51 +02:00
|
|
|
if (!router_get_by_id_digest(conn->identity_digest)) {
|
2008-08-05 22:08:19 +02:00
|
|
|
if (tor_addr_family(&TO_CONN(conn)->addr) == AF_INET) {
|
2008-12-18 17:11:24 +01:00
|
|
|
/*XXXX IP6 support ipv6 geoip.*/
|
2008-08-05 22:08:19 +02:00
|
|
|
uint32_t a = tor_addr_to_ipv4h(&TO_CONN(conn)->addr);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, a, now);
|
|
|
|
}
|
|
|
|
}
|
2005-12-28 08:19:55 +01:00
|
|
|
}
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
or_handshake_state_free(conn->handshake_state);
|
|
|
|
conn->handshake_state = NULL;
|
2009-08-14 20:34:16 +02:00
|
|
|
IF_HAS_BUFFEREVENT(TO_CONN(conn), {
|
|
|
|
connection_watch_events(TO_CONN(conn), READ_EVENT|WRITE_EVENT);
|
|
|
|
}) ELSE_IF_NO_BUFFEREVENT {
|
|
|
|
connection_start_reading(TO_CONN(conn));
|
|
|
|
}
|
2009-12-12 08:07:59 +01:00
|
|
|
|
2004-07-02 11:29:01 +02:00
|
|
|
circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
|
2007-10-30 19:31:30 +01:00
|
|
|
|
2003-09-30 20:45:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s outbuf.
|
|
|
|
* For cells that use or affect a circuit, this should only be called by
|
2008-01-19 21:00:53 +01:00
|
|
|
* connection_or_flush_from_first_active_circuit().
|
2004-05-13 00:56:26 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-04-10 01:15:46 +02:00
|
|
|
packed_cell_t networkcell;
|
2003-09-13 00:45:31 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(cell);
|
|
|
|
tor_assert(conn);
|
2003-10-09 20:45:14 +02:00
|
|
|
|
2007-04-10 01:15:46 +02:00
|
|
|
cell_pack(&networkcell, cell);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2007-04-10 01:15:46 +02:00
|
|
|
connection_write_to_buf(networkcell.body, CELL_NETWORK_SIZE, TO_CONN(conn));
|
2007-11-08 17:19:07 +01:00
|
|
|
|
2011-09-27 19:15:36 +02:00
|
|
|
if (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
|
|
|
|
or_handshake_state_record_cell(conn->handshake_state, cell, 0);
|
|
|
|
|
2007-11-08 17:19:07 +01:00
|
|
|
if (cell->command != CELL_PADDING)
|
2008-12-19 19:51:35 +01:00
|
|
|
conn->timestamp_last_added_nonpadding = approx_time();
|
2003-09-13 00:45:31 +02:00
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Pack a variable-length <b>cell</b> into wire-format, and write it onto
|
|
|
|
* <b>conn</b>'s outbuf. Right now, this <em>DOES NOT</em> support cells that
|
|
|
|
* affect a circuit.
|
|
|
|
*/
|
2007-11-06 00:34:39 +01:00
|
|
|
void
|
|
|
|
connection_or_write_var_cell_to_buf(const var_cell_t *cell,
|
|
|
|
or_connection_t *conn)
|
|
|
|
{
|
|
|
|
char hdr[VAR_CELL_HEADER_SIZE];
|
|
|
|
tor_assert(cell);
|
|
|
|
tor_assert(conn);
|
|
|
|
var_cell_pack_header(cell, hdr);
|
|
|
|
connection_write_to_buf(hdr, sizeof(hdr), TO_CONN(conn));
|
2010-12-14 01:34:01 +01:00
|
|
|
connection_write_to_buf((char*)cell->payload,
|
|
|
|
cell->payload_len, TO_CONN(conn));
|
2011-09-27 19:15:36 +02:00
|
|
|
if (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
|
|
|
|
or_handshake_state_record_var_cell(conn->handshake_state, cell, 0);
|
2007-11-08 17:19:07 +01:00
|
|
|
if (cell->command != CELL_PADDING)
|
2008-12-19 19:51:35 +01:00
|
|
|
conn->timestamp_last_added_nonpadding = approx_time();
|
2007-11-06 00:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-08-09 20:40:28 +02:00
|
|
|
/** See whether there's a variable-length cell waiting on <b>or_conn</b>'s
|
2008-02-08 22:13:15 +01:00
|
|
|
* inbuf. Return values as for fetch_var_cell_from_buf(). */
|
2007-11-05 22:46:35 +01:00
|
|
|
static int
|
2009-08-09 20:40:28 +02:00
|
|
|
connection_fetch_var_cell_from_buf(or_connection_t *or_conn, var_cell_t **out)
|
2007-11-05 22:46:35 +01:00
|
|
|
{
|
2009-08-09 20:40:28 +02:00
|
|
|
connection_t *conn = TO_CONN(or_conn);
|
|
|
|
IF_HAS_BUFFEREVENT(conn, {
|
2009-08-14 20:34:16 +02:00
|
|
|
struct evbuffer *input = bufferevent_get_input(conn->bufev);
|
|
|
|
return fetch_var_cell_from_evbuffer(input, out, or_conn->link_proto);
|
2009-08-09 20:40:28 +02:00
|
|
|
}) ELSE_IF_NO_BUFFEREVENT {
|
|
|
|
return fetch_var_cell_from_buf(conn->inbuf, out, or_conn->link_proto);
|
|
|
|
}
|
2007-11-05 22:46:35 +01:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Process cells from <b>conn</b>'s inbuf.
|
|
|
|
*
|
|
|
|
* Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
|
|
|
|
* and hand it to command_process_cell().
|
|
|
|
*
|
2004-05-07 10:53:40 +02:00
|
|
|
* Always return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_or_process_cells_from_inbuf(or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-11-05 22:46:35 +01:00
|
|
|
var_cell_t *var_cell;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
log_debug(LD_OR,
|
|
|
|
"%d: starting, inbuf_datalen %d (%d pending in tls object).",
|
2009-07-31 17:39:31 +02:00
|
|
|
conn->_base.s,(int)connection_get_inbuf_len(TO_CONN(conn)),
|
2007-11-05 22:46:35 +01:00
|
|
|
tor_tls_get_pending_bytes(conn->tls));
|
|
|
|
if (connection_fetch_var_cell_from_buf(conn, &var_cell)) {
|
|
|
|
if (!var_cell)
|
|
|
|
return 0; /* not yet. */
|
2009-09-01 03:10:27 +02:00
|
|
|
circuit_build_times_network_is_live(&circ_times);
|
2007-11-05 22:46:35 +01:00
|
|
|
command_process_var_cell(var_cell, conn);
|
|
|
|
var_cell_free(var_cell);
|
|
|
|
} else {
|
|
|
|
char buf[CELL_NETWORK_SIZE];
|
|
|
|
cell_t cell;
|
2009-07-31 17:39:31 +02:00
|
|
|
if (connection_get_inbuf_len(TO_CONN(conn))
|
|
|
|
< CELL_NETWORK_SIZE) /* whole response available? */
|
2007-11-05 22:46:35 +01:00
|
|
|
return 0; /* not yet */
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2009-09-01 03:10:27 +02:00
|
|
|
circuit_build_times_network_is_live(&circ_times);
|
2007-11-05 22:46:35 +01:00
|
|
|
connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, TO_CONN(conn));
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2007-11-05 22:46:35 +01:00
|
|
|
/* retrieve cell info from buf (create the host-order struct from the
|
|
|
|
* network-order string) */
|
|
|
|
cell_unpack(&cell, buf);
|
2003-10-09 20:45:14 +02:00
|
|
|
|
2007-11-05 22:46:35 +01:00
|
|
|
command_process_cell(&cell, conn);
|
|
|
|
}
|
|
|
|
}
|
2003-09-13 00:45:31 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2006-01-05 22:23:03 +01:00
|
|
|
/** Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
|
|
|
|
* onto OR connection <b>conn</b>. Don't perform range-checking on reason:
|
|
|
|
* we may want to propagate reasons from other cells.
|
|
|
|
*
|
|
|
|
* Return 0.
|
|
|
|
*/
|
|
|
|
int
|
2008-07-23 17:58:30 +02:00
|
|
|
connection_or_send_destroy(circid_t circ_id, or_connection_t *conn, int reason)
|
2006-01-05 22:23:03 +01:00
|
|
|
{
|
|
|
|
cell_t cell;
|
|
|
|
|
|
|
|
tor_assert(conn);
|
|
|
|
|
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
|
|
|
cell.circ_id = circ_id;
|
|
|
|
cell.command = CELL_DESTROY;
|
|
|
|
cell.payload[0] = (uint8_t) reason;
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"Sending destroy (circID %d).", circ_id);
|
2007-03-26 16:08:35 +02:00
|
|
|
|
2006-01-05 22:23:03 +01:00
|
|
|
connection_or_write_cell_to_buf(&cell, conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Array of recognized link protocol versions. */
|
2011-09-27 19:15:36 +02:00
|
|
|
static const uint16_t or_protocol_versions[] = { 1, 2, 3 };
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Number of versions in <b>or_protocol_versions</b>. */
|
2008-01-13 01:20:47 +01:00
|
|
|
static const int n_or_protocol_versions =
|
2008-02-22 20:09:45 +01:00
|
|
|
(int)( sizeof(or_protocol_versions)/sizeof(uint16_t) );
|
2008-01-13 01:20:47 +01:00
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Return true iff <b>v</b> is a link protocol version that this Tor
|
|
|
|
* implementation believes it can support. */
|
2008-01-13 01:20:47 +01:00
|
|
|
int
|
|
|
|
is_or_protocol_version_known(uint16_t v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n_or_protocol_versions; ++i) {
|
|
|
|
if (or_protocol_versions[i] == v)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Send a VERSIONS cell on <b>conn</b>, telling the other host about the
|
2011-09-27 19:15:36 +02:00
|
|
|
* link protocol versions that this Tor can support.
|
|
|
|
*
|
|
|
|
* If <b>v3_plus</b>, this is part of a V3 protocol handshake, so only
|
|
|
|
* allow protocol version v3 or later. If not <b>v3_plus</b>, this is
|
|
|
|
* not part of a v3 protocol handshake, so don't allow protocol v3 or
|
|
|
|
* later.
|
|
|
|
**/
|
|
|
|
int
|
|
|
|
connection_or_send_versions(or_connection_t *conn, int v3_plus)
|
2007-10-30 19:31:30 +01:00
|
|
|
{
|
2007-11-06 00:34:39 +01:00
|
|
|
var_cell_t *cell;
|
2007-10-30 19:31:30 +01:00
|
|
|
int i;
|
2011-09-27 19:15:36 +02:00
|
|
|
int n_versions = 0;
|
|
|
|
const int min_version = v3_plus ? 3 : 0;
|
|
|
|
const int max_version = v3_plus ? UINT16_MAX : 2;
|
2007-11-05 19:15:44 +01:00
|
|
|
tor_assert(conn->handshake_state &&
|
|
|
|
!conn->handshake_state->sent_versions_at);
|
2008-01-13 01:20:47 +01:00
|
|
|
cell = var_cell_new(n_or_protocol_versions * 2);
|
2007-11-06 00:34:39 +01:00
|
|
|
cell->command = CELL_VERSIONS;
|
2008-01-13 01:20:47 +01:00
|
|
|
for (i = 0; i < n_or_protocol_versions; ++i) {
|
|
|
|
uint16_t v = or_protocol_versions[i];
|
2011-09-27 19:15:36 +02:00
|
|
|
if (v < min_version || v > max_version)
|
|
|
|
continue;
|
|
|
|
set_uint16(cell->payload+(2*n_versions), htons(v));
|
|
|
|
++n_versions;
|
2007-10-30 19:31:30 +01:00
|
|
|
}
|
2011-09-27 19:15:36 +02:00
|
|
|
cell->payload_len = n_versions * 2;
|
2007-10-30 19:31:30 +01:00
|
|
|
|
2007-11-06 00:34:39 +01:00
|
|
|
connection_or_write_var_cell_to_buf(cell, conn);
|
2007-11-05 19:15:44 +01:00
|
|
|
conn->handshake_state->sent_versions_at = time(NULL);
|
2007-10-30 22:46:02 +01:00
|
|
|
|
2007-11-06 00:55:43 +01:00
|
|
|
var_cell_free(cell);
|
2007-10-30 22:46:02 +01:00
|
|
|
return 0;
|
2007-10-30 19:31:30 +01:00
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:15 +01:00
|
|
|
/** Send a NETINFO cell on <b>conn</b>, telling the other server what we know
|
|
|
|
* about their address, our address, and the current time. */
|
2007-10-30 22:46:02 +01:00
|
|
|
int
|
2007-10-30 19:31:30 +01:00
|
|
|
connection_or_send_netinfo(or_connection_t *conn)
|
|
|
|
{
|
|
|
|
cell_t cell;
|
|
|
|
time_t now = time(NULL);
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *me;
|
2008-08-05 22:08:19 +02:00
|
|
|
int len;
|
2010-12-14 01:34:01 +01:00
|
|
|
uint8_t *out;
|
2007-10-30 19:31:30 +01:00
|
|
|
|
2011-09-28 16:31:56 +02:00
|
|
|
tor_assert(conn->handshake_state);
|
|
|
|
|
2007-10-30 19:31:30 +01:00
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
|
|
|
cell.command = CELL_NETINFO;
|
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
/* Timestamp. */
|
2008-02-22 20:09:45 +01:00
|
|
|
set_uint32(cell.payload, htonl((uint32_t)now));
|
2008-08-05 22:08:19 +02:00
|
|
|
|
|
|
|
/* Their address. */
|
|
|
|
out = cell.payload + 4;
|
2011-11-16 14:07:10 +01:00
|
|
|
/* We use &conn->real_addr below, unless it hasn't yet been set. If it
|
|
|
|
* hasn't yet been set, we know that _base.addr hasn't been tampered with
|
|
|
|
* yet either. */
|
|
|
|
len = append_address_to_payload(out, !tor_addr_is_null(&conn->real_addr)
|
|
|
|
? &conn->real_addr : &conn->_base.addr);
|
2008-08-05 22:08:19 +02:00
|
|
|
if (len<0)
|
|
|
|
return -1;
|
|
|
|
out += len;
|
2007-10-30 19:31:30 +01:00
|
|
|
|
2011-10-30 02:43:23 +01:00
|
|
|
/* My address -- only include it if I'm a public relay, or if I'm a
|
|
|
|
* bridge and this is an incoming connection. If I'm a bridge and this
|
|
|
|
* is an outgoing connection, act like a normal client and omit it. */
|
|
|
|
if ((public_server_mode(get_options()) || !conn->is_outgoing) &&
|
|
|
|
(me = router_get_my_routerinfo())) {
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t my_addr;
|
|
|
|
*out++ = 1; /* only one address is supported. */
|
|
|
|
|
|
|
|
tor_addr_from_ipv4h(&my_addr, me->addr);
|
|
|
|
len = append_address_to_payload(out, &my_addr);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
2007-10-30 19:31:30 +01:00
|
|
|
} else {
|
2011-04-26 04:50:15 +02:00
|
|
|
*out = 0;
|
2007-10-30 19:31:30 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 16:31:56 +02:00
|
|
|
conn->handshake_state->digest_sent_data = 0;
|
2007-10-30 19:31:30 +01:00
|
|
|
connection_or_write_cell_to_buf(&cell, conn);
|
|
|
|
|
2007-10-30 22:46:02 +01:00
|
|
|
return 0;
|
2007-10-30 19:31:30 +01:00
|
|
|
}
|
2007-10-31 21:48:10 +01:00
|
|
|
|
2011-10-31 09:33:38 +01:00
|
|
|
/** Send a CERTS cell on the connection <b>conn</b>. Return 0 on success, -1
|
2011-09-13 17:38:38 +02:00
|
|
|
* on failure. */
|
|
|
|
int
|
2011-10-31 09:33:38 +01:00
|
|
|
connection_or_send_certs_cell(or_connection_t *conn)
|
2011-09-13 17:38:38 +02:00
|
|
|
{
|
|
|
|
const tor_cert_t *link_cert = NULL, *id_cert = NULL;
|
|
|
|
const uint8_t *link_encoded = NULL, *id_encoded = NULL;
|
|
|
|
size_t link_len, id_len;
|
|
|
|
var_cell_t *cell;
|
|
|
|
size_t cell_len;
|
2011-10-11 03:06:41 +02:00
|
|
|
ssize_t pos;
|
2011-09-14 20:44:42 +02:00
|
|
|
int server_mode;
|
2011-09-13 17:38:38 +02:00
|
|
|
|
|
|
|
tor_assert(conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3);
|
|
|
|
|
|
|
|
if (! conn->handshake_state)
|
|
|
|
return -1;
|
2011-09-14 20:44:42 +02:00
|
|
|
server_mode = ! conn->handshake_state->started_here;
|
|
|
|
if (tor_tls_get_my_certs(server_mode, &link_cert, &id_cert) < 0)
|
2011-09-13 17:38:38 +02:00
|
|
|
return -1;
|
|
|
|
tor_cert_get_der(link_cert, &link_encoded, &link_len);
|
|
|
|
tor_cert_get_der(id_cert, &id_encoded, &id_len);
|
|
|
|
|
2011-10-11 17:30:01 +02:00
|
|
|
cell_len = 1 /* 1 byte: num certs in cell */ +
|
|
|
|
2 * ( 1 + 2 ) /* For each cert: 1 byte for type, 2 for length */ +
|
2011-09-13 17:38:38 +02:00
|
|
|
link_len + id_len;
|
|
|
|
cell = var_cell_new(cell_len);
|
2011-10-31 09:33:38 +01:00
|
|
|
cell->command = CELL_CERTS;
|
2011-09-13 17:38:38 +02:00
|
|
|
cell->payload[0] = 2;
|
|
|
|
pos = 1;
|
|
|
|
|
2011-09-14 20:44:42 +02:00
|
|
|
if (server_mode)
|
|
|
|
cell->payload[pos] = OR_CERT_TYPE_TLS_LINK; /* Link cert */
|
|
|
|
else
|
|
|
|
cell->payload[pos] = OR_CERT_TYPE_AUTH_1024; /* client authentication */
|
2011-09-13 17:38:38 +02:00
|
|
|
set_uint16(&cell->payload[pos+1], htons(link_len));
|
|
|
|
memcpy(&cell->payload[pos+3], link_encoded, link_len);
|
|
|
|
pos += 3 + link_len;
|
|
|
|
|
|
|
|
cell->payload[pos] = OR_CERT_TYPE_ID_1024; /* ID cert */
|
|
|
|
set_uint16(&cell->payload[pos+1], htons(id_len));
|
|
|
|
memcpy(&cell->payload[pos+3], id_encoded, id_len);
|
|
|
|
pos += 3 + id_len;
|
|
|
|
|
|
|
|
tor_assert(pos == (int)cell_len); /* Otherwise we just smashed the heap */
|
|
|
|
|
|
|
|
connection_or_write_var_cell_to_buf(cell, conn);
|
|
|
|
var_cell_free(cell);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Send an AUTH_CHALLENGE cell on the connection <b>conn</b>. Return 0
|
|
|
|
* on success, -1 on failure. */
|
|
|
|
int
|
|
|
|
connection_or_send_auth_challenge_cell(or_connection_t *conn)
|
|
|
|
{
|
|
|
|
var_cell_t *cell;
|
|
|
|
uint8_t *cp;
|
|
|
|
uint8_t challenge[OR_AUTH_CHALLENGE_LEN];
|
|
|
|
tor_assert(conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING_V3);
|
|
|
|
|
|
|
|
if (! conn->handshake_state)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (crypto_rand((char*)challenge, OR_AUTH_CHALLENGE_LEN) < 0)
|
|
|
|
return -1;
|
|
|
|
cell = var_cell_new(OR_AUTH_CHALLENGE_LEN + 4);
|
|
|
|
cell->command = CELL_AUTH_CHALLENGE;
|
|
|
|
memcpy(cell->payload, challenge, OR_AUTH_CHALLENGE_LEN);
|
|
|
|
cp = cell->payload + OR_AUTH_CHALLENGE_LEN;
|
|
|
|
set_uint16(cp, htons(1)); /* We recognize one authentication type. */
|
|
|
|
set_uint16(cp+2, htons(AUTHTYPE_RSA_SHA256_TLSSECRET));
|
|
|
|
|
|
|
|
connection_or_write_var_cell_to_buf(cell, conn);
|
|
|
|
var_cell_free(cell);
|
|
|
|
memset(challenge, 0, sizeof(challenge));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-14 20:44:42 +02:00
|
|
|
/** Compute the main body of an AUTHENTICATE cell that a client can use
|
|
|
|
* to authenticate itself on a v3 handshake for <b>conn</b>. Write it to the
|
|
|
|
* <b>outlen</b>-byte buffer at <b>out</b>.
|
|
|
|
*
|
|
|
|
* If <b>server</b> is true, only calculate the first
|
2011-09-13 22:24:49 +02:00
|
|
|
* V3_AUTH_FIXED_PART_LEN bytes -- the part of the authenticator that's
|
2011-09-14 20:44:42 +02:00
|
|
|
* determined by the rest of the handshake, and which match the provided value
|
|
|
|
* exactly.
|
|
|
|
*
|
|
|
|
* If <b>server</b> is false and <b>signing_key</b> is NULL, calculate the
|
2011-09-13 22:24:49 +02:00
|
|
|
* first V3_AUTH_BODY_LEN bytes of the authenticator (that is, everything
|
2011-09-14 20:44:42 +02:00
|
|
|
* that should be signed), but don't actually sign it.
|
|
|
|
*
|
|
|
|
* If <b>server</b> is false and <b>signing_key</b> is provided, calculate the
|
|
|
|
* entire authenticator, signed with <b>signing_key</b>.
|
2011-10-11 03:06:41 +02:00
|
|
|
* DOCDOC return value
|
2011-09-14 20:44:42 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
connection_or_compute_authenticate_cell_body(or_connection_t *conn,
|
|
|
|
uint8_t *out, size_t outlen,
|
|
|
|
crypto_pk_env_t *signing_key,
|
|
|
|
int server)
|
|
|
|
{
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
|
|
|
/* assert state is reasonable XXXX */
|
|
|
|
|
2011-09-13 22:24:49 +02:00
|
|
|
if (outlen < V3_AUTH_FIXED_PART_LEN ||
|
|
|
|
(!server && outlen < V3_AUTH_BODY_LEN))
|
2011-09-14 20:44:42 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ptr = out;
|
|
|
|
|
|
|
|
/* Type: 8 bytes. */
|
|
|
|
memcpy(ptr, "AUTH0001", 8);
|
|
|
|
ptr += 8;
|
|
|
|
|
|
|
|
{
|
|
|
|
const tor_cert_t *id_cert=NULL, *link_cert=NULL;
|
2011-10-05 16:33:39 +02:00
|
|
|
const digests_t *my_digests, *their_digests;
|
2011-09-14 20:44:42 +02:00
|
|
|
const uint8_t *my_id, *their_id, *client_id, *server_id;
|
|
|
|
if (tor_tls_get_my_certs(0, &link_cert, &id_cert))
|
|
|
|
return -1;
|
2011-10-05 16:33:39 +02:00
|
|
|
my_digests = tor_cert_get_id_digests(id_cert);
|
|
|
|
their_digests = tor_cert_get_id_digests(conn->handshake_state->id_cert);
|
|
|
|
tor_assert(my_digests);
|
|
|
|
tor_assert(their_digests);
|
|
|
|
my_id = (uint8_t*)my_digests->d[DIGEST_SHA256];
|
|
|
|
their_id = (uint8_t*)their_digests->d[DIGEST_SHA256];
|
|
|
|
|
2011-09-14 20:44:42 +02:00
|
|
|
client_id = server ? their_id : my_id;
|
|
|
|
server_id = server ? my_id : their_id;
|
|
|
|
|
|
|
|
/* Client ID digest: 32 octets. */
|
|
|
|
memcpy(ptr, client_id, 32);
|
|
|
|
ptr += 32;
|
|
|
|
|
|
|
|
/* Server ID digest: 32 octets. */
|
|
|
|
memcpy(ptr, server_id, 32);
|
|
|
|
ptr += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
crypto_digest_env_t *server_d, *client_d;
|
|
|
|
if (server) {
|
|
|
|
server_d = conn->handshake_state->digest_sent;
|
|
|
|
client_d = conn->handshake_state->digest_received;
|
|
|
|
} else {
|
|
|
|
client_d = conn->handshake_state->digest_sent;
|
|
|
|
server_d = conn->handshake_state->digest_received;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Server log digest : 32 octets */
|
|
|
|
crypto_digest_get_digest(server_d, (char*)ptr, 32);
|
|
|
|
ptr += 32;
|
|
|
|
|
|
|
|
/* Client log digest : 32 octets */
|
|
|
|
crypto_digest_get_digest(client_d, (char*)ptr, 32);
|
|
|
|
ptr += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Digest of cert used on TLS link : 32 octets. */
|
|
|
|
const tor_cert_t *cert = NULL;
|
|
|
|
tor_cert_t *freecert = NULL;
|
|
|
|
if (server) {
|
|
|
|
tor_tls_get_my_certs(1, &cert, NULL);
|
|
|
|
} else {
|
|
|
|
freecert = tor_tls_get_peer_cert(conn->tls);
|
|
|
|
cert = freecert;
|
|
|
|
}
|
|
|
|
if (!cert)
|
|
|
|
return -1;
|
|
|
|
memcpy(ptr, tor_cert_get_cert_digests(cert)->d[DIGEST_SHA256], 32);
|
|
|
|
|
|
|
|
if (freecert)
|
|
|
|
tor_cert_free(freecert);
|
|
|
|
ptr += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HMAC of clientrandom and serverrandom using master key : 32 octets */
|
|
|
|
tor_tls_get_tlssecrets(conn->tls, ptr);
|
|
|
|
ptr += 32;
|
|
|
|
|
2011-09-13 22:24:49 +02:00
|
|
|
tor_assert(ptr - out == V3_AUTH_FIXED_PART_LEN);
|
2011-09-14 20:44:42 +02:00
|
|
|
|
|
|
|
if (server)
|
2011-10-11 03:06:41 +02:00
|
|
|
return V3_AUTH_FIXED_PART_LEN; // ptr-out
|
2011-09-14 20:44:42 +02:00
|
|
|
|
|
|
|
/* Time: 8 octets. */
|
|
|
|
{
|
|
|
|
uint64_t now = time(NULL);
|
|
|
|
if ((time_t)now < 0)
|
|
|
|
return -1;
|
|
|
|
set_uint32(ptr, htonl((uint32_t)(now>>32)));
|
|
|
|
set_uint32(ptr+4, htonl((uint32_t)now));
|
|
|
|
ptr += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nonce: 16 octets. */
|
|
|
|
crypto_rand((char*)ptr, 16);
|
|
|
|
ptr += 16;
|
|
|
|
|
2011-09-13 22:24:49 +02:00
|
|
|
tor_assert(ptr - out == V3_AUTH_BODY_LEN);
|
2011-09-14 20:44:42 +02:00
|
|
|
|
|
|
|
if (!signing_key)
|
2011-10-11 03:06:41 +02:00
|
|
|
return V3_AUTH_BODY_LEN; // ptr - out
|
2011-09-14 20:44:42 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
int siglen;
|
|
|
|
char d[32];
|
|
|
|
crypto_digest256(d, (char*)out, ptr-out, DIGEST_SHA256);
|
|
|
|
siglen = crypto_pk_private_sign(signing_key,
|
|
|
|
(char*)ptr, outlen - (ptr-out),
|
|
|
|
d, 32);
|
|
|
|
if (siglen < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ptr += siglen;
|
|
|
|
tor_assert(ptr <= out+outlen);
|
2011-10-11 03:06:41 +02:00
|
|
|
return (int)(ptr - out);
|
2011-09-14 20:44:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Send an AUTHENTICATE cell on the connection <b>conn</b>. Return 0 on
|
|
|
|
* success, -1 on failure */
|
|
|
|
int
|
2011-09-27 19:15:36 +02:00
|
|
|
connection_or_send_authenticate_cell(or_connection_t *conn, int authtype)
|
2011-09-14 20:44:42 +02:00
|
|
|
{
|
|
|
|
var_cell_t *cell;
|
|
|
|
crypto_pk_env_t *pk = tor_tls_get_my_client_auth_key();
|
|
|
|
int authlen;
|
2011-10-11 03:06:41 +02:00
|
|
|
size_t cell_maxlen;
|
2011-09-14 20:44:42 +02:00
|
|
|
/* XXXX make sure we're actually supposed to send this! */
|
|
|
|
|
2011-10-06 20:58:59 +02:00
|
|
|
if (!pk) {
|
2011-10-11 17:30:01 +02:00
|
|
|
log_warn(LD_BUG, "Can't compute authenticate cell: no client auth key");
|
2011-10-06 20:58:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (authtype != AUTHTYPE_RSA_SHA256_TLSSECRET) {
|
|
|
|
log_warn(LD_BUG, "Tried to send authenticate cell with unknown "
|
|
|
|
"authentication type %d", authtype);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-27 19:15:36 +02:00
|
|
|
|
2011-09-14 20:44:42 +02:00
|
|
|
cell_maxlen = 4 + /* overhead */
|
2011-09-13 22:24:49 +02:00
|
|
|
V3_AUTH_BODY_LEN + /* Authentication body */
|
2011-09-14 20:44:42 +02:00
|
|
|
crypto_pk_keysize(pk) + /* Max signature length */
|
2011-10-06 20:58:59 +02:00
|
|
|
16 /* add a few extra bytes just in case. */;
|
2011-09-14 20:44:42 +02:00
|
|
|
|
|
|
|
cell = var_cell_new(cell_maxlen);
|
2011-09-27 21:20:17 +02:00
|
|
|
cell->command = CELL_AUTHENTICATE;
|
2011-09-14 20:44:42 +02:00
|
|
|
set_uint16(cell->payload, htons(AUTHTYPE_RSA_SHA256_TLSSECRET));
|
|
|
|
/* skip over length ; we don't know that yet. */
|
|
|
|
|
|
|
|
authlen = connection_or_compute_authenticate_cell_body(conn,
|
|
|
|
cell->payload+4,
|
|
|
|
cell_maxlen-4,
|
|
|
|
pk,
|
|
|
|
0 /* not server */);
|
|
|
|
if (authlen < 0) {
|
2011-10-06 20:58:59 +02:00
|
|
|
log_warn(LD_BUG, "Unable to compute authenticate cell!");
|
2011-09-14 20:44:42 +02:00
|
|
|
var_cell_free(cell);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tor_assert(authlen + 4 <= cell->payload_len);
|
|
|
|
set_uint16(cell->payload+2, htons(authlen));
|
|
|
|
cell->payload_len = authlen + 4;
|
|
|
|
|
|
|
|
connection_or_write_var_cell_to_buf(cell, conn);
|
|
|
|
var_cell_free(cell);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-11 17:30:01 +02:00
|
|
|
|