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.
|
2012-06-05 02:58:17 +02:00
|
|
|
* Copyright (c) 2007-2012, 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 command.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Functions for processing incoming cells.
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
|
|
|
|
2004-05-11 05:21:18 +02:00
|
|
|
/* In-points to command.c:
|
|
|
|
*
|
|
|
|
* - command_process_cell(), called from
|
2012-08-24 04:30:49 +02:00
|
|
|
* incoming cell handlers of channel_t instances;
|
|
|
|
* callbacks registered in command_setup_channel(),
|
|
|
|
* called when channels are created in circuitbuild.c
|
2004-05-11 05:21:18 +02:00
|
|
|
*/
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
#include "or.h"
|
2012-08-24 04:30:49 +02:00
|
|
|
#include "channel.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2010-07-22 10:08:32 +02:00
|
|
|
#include "command.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 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-22 11:40:39 +02:00
|
|
|
#include "cpuworker.h"
|
2010-07-22 12:30:46 +02:00
|
|
|
#include "hibernate.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"
|
2012-12-06 04:34:49 +01:00
|
|
|
#include "onion.h"
|
2010-07-23 21:53:11 +02:00
|
|
|
#include "relay.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2008-12-23 18:56:31 +01:00
|
|
|
/** How many CELL_CREATE cells have we received, ever? */
|
2005-12-31 09:09:26 +01:00
|
|
|
uint64_t stats_n_create_cells_processed = 0;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** How many CELL_CREATED cells have we received, ever? */
|
2005-12-31 09:09:26 +01:00
|
|
|
uint64_t stats_n_created_cells_processed = 0;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** How many CELL_RELAY cells have we received, ever? */
|
2005-12-31 09:09:26 +01:00
|
|
|
uint64_t stats_n_relay_cells_processed = 0;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** How many CELL_DESTROY cells have we received, ever? */
|
2005-12-31 09:09:26 +01:00
|
|
|
uint64_t stats_n_destroy_cells_processed = 0;
|
2003-10-02 22:00:38 +02:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/* Handle an incoming channel */
|
2012-10-09 09:51:33 +02:00
|
|
|
static void command_handle_incoming_channel(channel_listener_t *listener,
|
2012-08-24 04:30:49 +02:00
|
|
|
channel_t *chan);
|
2011-09-27 19:15:36 +02:00
|
|
|
|
2007-10-30 19:31:30 +01:00
|
|
|
/* These are the main functions for processing cells */
|
2012-08-24 04:30:49 +02:00
|
|
|
static void command_process_create_cell(cell_t *cell, channel_t *chan);
|
|
|
|
static void command_process_created_cell(cell_t *cell, channel_t *chan);
|
|
|
|
static void command_process_relay_cell(cell_t *cell, channel_t *chan);
|
|
|
|
static void command_process_destroy_cell(cell_t *cell, channel_t *chan);
|
2003-09-16 07:41:49 +02:00
|
|
|
|
2005-01-03 21:03:49 +01:00
|
|
|
#ifdef KEEP_TIMING_STATS
|
2004-05-09 18:47:25 +02:00
|
|
|
/** This is a wrapper function around the actual function that processes the
|
|
|
|
* <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
|
|
|
|
* by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
|
2004-05-07 10:07:41 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_time_process_cell(cell_t *cell, channel_t *chan, int *time,
|
|
|
|
void (*func)(cell_t *, channel_t *))
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2002-11-24 09:45:54 +01:00
|
|
|
struct timeval start, end;
|
2003-12-17 22:09:31 +01:00
|
|
|
long time_passed;
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
tor_gettimeofday(&start);
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
(*func)(cell, chan);
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
tor_gettimeofday(&end);
|
2003-04-16 19:04:58 +02:00
|
|
|
time_passed = tv_udiff(&start, &end) ;
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2004-02-28 08:01:22 +01:00
|
|
|
if (time_passed > 10000) { /* more than 10ms */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
|
2002-11-24 09:45:54 +01:00
|
|
|
}
|
2004-11-23 01:08:26 +01:00
|
|
|
if (time_passed < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_GENERAL,"That call took us back in time!");
|
2004-11-23 01:08:26 +01:00
|
|
|
time_passed = 0;
|
|
|
|
}
|
2002-11-24 09:45:54 +01:00
|
|
|
*time += time_passed;
|
|
|
|
}
|
2005-01-03 21:03:49 +01:00
|
|
|
#endif
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
|
2004-05-07 10:07:41 +02:00
|
|
|
* statistics about how many of each cell we've processed so far
|
|
|
|
* this second, and the total number of microseconds it took to
|
|
|
|
* process each type of cell.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_process_cell(channel_t *chan, cell_t *cell)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-12-25 07:10:34 +01:00
|
|
|
#ifdef KEEP_TIMING_STATS
|
2004-05-07 10:07:41 +02:00
|
|
|
/* how many of each cell have we seen so far this second? needs better
|
|
|
|
* name. */
|
2003-05-20 08:41:23 +02:00
|
|
|
static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
|
2004-05-07 10:07:41 +02:00
|
|
|
/* how long has it taken to process each type of cell? */
|
2003-05-20 08:41:23 +02:00
|
|
|
static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
|
2003-10-04 05:29:09 +02:00
|
|
|
static time_t current_second = 0; /* from previous calls to time */
|
2004-05-07 10:07:41 +02:00
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
time_t now = time(NULL);
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (now > current_second) { /* the second has rolled over */
|
2002-11-24 09:45:54 +01:00
|
|
|
/* print stats */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_OR,
|
|
|
|
"At end of second: %d creates (%d ms), %d createds (%d ms), "
|
2005-12-14 21:40:40 +01:00
|
|
|
"%d relays (%d ms), %d destroys (%d ms)",
|
2005-12-10 10:36:26 +01:00
|
|
|
num_create, create_time/1000,
|
|
|
|
num_created, created_time/1000,
|
|
|
|
num_relay, relay_time/1000,
|
|
|
|
num_destroy, destroy_time/1000);
|
2002-11-24 09:45:54 +01:00
|
|
|
|
|
|
|
/* zero out stats */
|
2003-05-20 08:41:23 +02:00
|
|
|
num_create = num_created = num_relay = num_destroy = 0;
|
|
|
|
create_time = created_time = relay_time = destroy_time = 0;
|
2002-11-24 09:45:54 +01:00
|
|
|
|
|
|
|
/* remember which second it is, for next time */
|
2003-10-04 05:29:09 +02:00
|
|
|
current_second = now;
|
2002-11-24 09:45:54 +01:00
|
|
|
}
|
2004-12-25 07:10:34 +01:00
|
|
|
#endif
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2007-10-30 19:31:30 +01:00
|
|
|
#ifdef KEEP_TIMING_STATS
|
|
|
|
#define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
|
|
|
|
++num ## tp; \
|
|
|
|
command_time_process_cell(cl, cn, & tp ## time , \
|
|
|
|
command_process_ ## tp ## _cell); \
|
|
|
|
} STMT_END
|
|
|
|
#else
|
|
|
|
#define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
|
|
|
|
#endif
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (cell->command) {
|
2002-06-27 00:45:49 +02:00
|
|
|
case CELL_CREATE:
|
2005-05-03 00:35:18 +02:00
|
|
|
case CELL_CREATE_FAST:
|
2003-10-02 22:00:38 +02:00
|
|
|
++stats_n_create_cells_processed;
|
2012-08-24 04:30:49 +02:00
|
|
|
PROCESS_CELL(create, cell, chan);
|
2002-06-27 00:45:49 +02:00
|
|
|
break;
|
2003-05-06 01:24:46 +02:00
|
|
|
case CELL_CREATED:
|
2005-05-03 00:35:18 +02:00
|
|
|
case CELL_CREATED_FAST:
|
2003-10-02 22:00:38 +02:00
|
|
|
++stats_n_created_cells_processed;
|
2012-08-24 04:30:49 +02:00
|
|
|
PROCESS_CELL(created, cell, chan);
|
2003-05-06 01:24:46 +02:00
|
|
|
break;
|
2003-05-01 08:42:29 +02:00
|
|
|
case CELL_RELAY:
|
2007-11-14 21:01:15 +01:00
|
|
|
case CELL_RELAY_EARLY:
|
2003-10-02 22:00:38 +02:00
|
|
|
++stats_n_relay_cells_processed;
|
2012-08-24 04:30:49 +02:00
|
|
|
PROCESS_CELL(relay, cell, chan);
|
2002-06-27 00:45:49 +02:00
|
|
|
break;
|
|
|
|
case CELL_DESTROY:
|
2003-10-02 22:00:38 +02:00
|
|
|
++stats_n_destroy_cells_processed;
|
2012-08-24 04:30:49 +02:00
|
|
|
PROCESS_CELL(destroy, cell, chan);
|
2002-06-27 00:45:49 +02:00
|
|
|
break;
|
2002-07-18 08:37:58 +02:00
|
|
|
default:
|
2007-10-30 19:31:30 +01:00
|
|
|
log_fn(LOG_INFO, LD_PROTOCOL,
|
2012-08-24 04:30:49 +02:00
|
|
|
"Cell of unknown or unexpected type (%d) received. "
|
|
|
|
"Dropping.",
|
|
|
|
cell->command);
|
2002-07-18 08:37:58 +02:00
|
|
|
break;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Process an incoming var_cell from a channel; in the current protocol all
|
2012-10-14 00:34:24 +02:00
|
|
|
* the var_cells are handshake-related and handled below the channel layer,
|
2012-08-24 04:30:49 +02:00
|
|
|
* so this just logs a warning and drops the cell.
|
2007-11-05 22:46:35 +01:00
|
|
|
*/
|
2012-08-24 04:30:49 +02:00
|
|
|
|
2007-11-05 22:46:35 +01:00
|
|
|
void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_process_var_cell(channel_t *chan, var_cell_t *var_cell)
|
2007-11-05 22:46:35 +01:00
|
|
|
{
|
2012-08-24 04:30:49 +02:00
|
|
|
tor_assert(chan);
|
|
|
|
tor_assert(var_cell);
|
2007-11-05 22:46:35 +01:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
log_info(LD_PROTOCOL,
|
|
|
|
"Received unexpected var_cell above the channel layer of type %d"
|
|
|
|
"; dropping it.",
|
|
|
|
var_cell->command);
|
2007-11-05 22:46:35 +01:00
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
|
2005-12-14 21:40:40 +01:00
|
|
|
* new circuit with the p_circ_id specified in cell. Put the circuit in state
|
|
|
|
* onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
|
|
|
|
* picked up again when the cpuworker finishes decrypting it.
|
2004-05-07 10:07:41 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_process_create_cell(cell_t *cell, channel_t *chan)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *circ;
|
2011-10-27 00:15:25 +02:00
|
|
|
const or_options_t *options = get_options();
|
2005-12-14 21:40:40 +01:00
|
|
|
int id_is_high;
|
2012-12-06 04:34:49 +01:00
|
|
|
create_cell_t *create_cell;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
tor_assert(cell);
|
|
|
|
tor_assert(chan);
|
|
|
|
|
|
|
|
log_debug(LD_OR,
|
2012-10-09 04:48:06 +02:00
|
|
|
"Got a CREATE cell for circ_id %d on channel " U64_FORMAT
|
|
|
|
" (%p)",
|
|
|
|
cell->circ_id,
|
|
|
|
U64_PRINTF_ARG(chan->global_identifier), chan);
|
2012-08-24 04:30:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (we_are_hibernating()) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_OR,
|
|
|
|
"Received create cell but we're shutting down. Sending back "
|
|
|
|
"destroy.");
|
2012-08-24 04:30:49 +02:00
|
|
|
channel_send_destroy(cell->circ_id, chan,
|
2006-01-05 22:23:03 +01:00
|
|
|
END_CIRC_REASON_HIBERNATING);
|
2004-07-21 01:31:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-24 00:21:49 +02:00
|
|
|
if (!server_mode(options) ||
|
2012-08-24 04:30:49 +02:00
|
|
|
(!public_server_mode(options) && channel_is_outgoing(chan))) {
|
2006-07-30 06:32:58 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2012-08-24 04:30:49 +02:00
|
|
|
"Received create cell (type %d) from %s, but we're connected "
|
2011-10-24 00:21:49 +02:00
|
|
|
"to it as a client. "
|
2006-07-30 06:32:58 +02:00
|
|
|
"Sending back a destroy.",
|
2012-08-24 04:30:49 +02:00
|
|
|
(int)cell->command, channel_get_canonical_remote_descr(chan));
|
|
|
|
channel_send_destroy(cell->circ_id, chan,
|
|
|
|
END_CIRC_REASON_TORPROTOCOL);
|
2006-07-30 06:32:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-07-04 05:27:09 +02:00
|
|
|
/* If the high bit of the circuit ID is not as expected, close the
|
|
|
|
* circ. */
|
2005-12-14 21:40:40 +01:00
|
|
|
id_is_high = cell->circ_id & (1<<15);
|
2012-08-24 04:30:49 +02:00
|
|
|
if ((id_is_high &&
|
2012-10-09 09:51:33 +02:00
|
|
|
chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
|
2012-08-24 04:30:49 +02:00
|
|
|
(!id_is_high &&
|
2012-10-09 09:51:33 +02:00
|
|
|
chan->circ_id_type == CIRC_ID_TYPE_LOWER)) {
|
2006-07-04 05:27:09 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Received create cell with unexpected circ_id %d. Closing.",
|
|
|
|
cell->circ_id);
|
2012-08-24 04:30:49 +02:00
|
|
|
channel_send_destroy(cell->circ_id, chan,
|
|
|
|
END_CIRC_REASON_TORPROTOCOL);
|
2006-07-04 05:27:09 +02:00
|
|
|
return;
|
2004-11-10 21:14:37 +01:00
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
if (circuit_id_in_use_on_channel(cell->circ_id, chan)) {
|
2012-10-09 09:51:33 +02:00
|
|
|
const node_t *node = node_get_by_id(chan->identity_digest);
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2006-07-04 05:27:09 +02:00
|
|
|
"Received CREATE cell (circID %d) for known circ. "
|
2005-12-14 21:40:40 +01:00
|
|
|
"Dropping (age %d).",
|
2012-08-24 04:30:49 +02:00
|
|
|
cell->circ_id, (int)(time(NULL) - channel_when_created(chan)));
|
2010-10-01 00:37:53 +02:00
|
|
|
if (node) {
|
|
|
|
char *p = esc_for_log(node_get_platform(node));
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Details: router %s, platform %s.",
|
2011-05-30 21:41:46 +02:00
|
|
|
node_describe(node), p);
|
2010-10-01 00:37:53 +02:00
|
|
|
tor_free(p);
|
|
|
|
}
|
2004-12-01 05:55:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
circ = or_circuit_new(cell->circ_id, chan);
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.purpose = CIRCUIT_PURPOSE_OR;
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
|
2012-12-06 04:34:49 +01:00
|
|
|
create_cell = tor_malloc_zero(sizeof(create_cell_t));
|
|
|
|
if (create_cell_parse(create_cell, cell) < 0) {
|
|
|
|
tor_free(create_cell);
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_OR,
|
|
|
|
"Bogus/unrecognized create cell; closing.");
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
|
|
|
|
return;
|
|
|
|
}
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2012-12-06 04:34:49 +01:00
|
|
|
if (create_cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST) {
|
2006-07-04 05:27:09 +02:00
|
|
|
/* hand it off to the cpuworkers, and then return. */
|
2012-12-06 04:34:49 +01:00
|
|
|
if (assign_onionskin_to_cpuworker(NULL, circ, create_cell) < 0) {
|
2012-10-05 19:35:13 +02:00
|
|
|
log_debug(LD_GENERAL,"Failed to hand off onionskin. Closing.");
|
2012-10-04 02:17:37 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
|
2005-05-03 00:35:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,"success: handed off onionskin.");
|
2005-05-03 00:35:18 +02:00
|
|
|
} else {
|
2005-12-08 18:38:32 +01:00
|
|
|
/* This is a CREATE_FAST cell; we can handle it immediately without using
|
2006-07-04 05:27:09 +02:00
|
|
|
* a CPU worker. */
|
2012-12-06 04:34:49 +01:00
|
|
|
uint8_t keys[CPATH_KEY_MATERIAL_LEN];
|
|
|
|
uint8_t reply[MAX_ONIONSKIN_REPLY_LEN];
|
|
|
|
uint8_t rend_circ_nonce[DIGEST_LEN];
|
|
|
|
int len;
|
2011-10-23 23:27:56 +02:00
|
|
|
|
|
|
|
/* Make sure we never try to use the OR connection on which we
|
|
|
|
* received this cell to satisfy an EXTEND request, */
|
2012-08-24 04:30:49 +02:00
|
|
|
channel_mark_client(chan);
|
2011-10-23 23:27:56 +02:00
|
|
|
|
2012-12-06 04:34:49 +01:00
|
|
|
len = onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST,
|
|
|
|
create_cell->onionskin,
|
|
|
|
create_cell->handshake_len,
|
|
|
|
NULL,
|
|
|
|
reply, keys, CPATH_KEY_MATERIAL_LEN,
|
|
|
|
rend_circ_nonce);
|
|
|
|
tor_free(create_cell);
|
|
|
|
if (len < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_OR,"Failed to generate key material. Closing.");
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
|
2012-12-06 04:34:49 +01:00
|
|
|
tor_free(create_cell);
|
2005-05-03 00:35:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-12-06 04:34:49 +01:00
|
|
|
if (onionskin_answer(circ, CELL_CREATED_FAST, (const char *)reply, len,
|
|
|
|
(const char *)keys, rend_circ_nonce)<0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
|
2005-05-03 00:35:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-12-06 04:34:49 +01:00
|
|
|
memwipe(keys, 0, sizeof(keys));
|
2003-05-06 01:24:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
|
2005-11-19 02:56:58 +01:00
|
|
|
* Find the circuit
|
2004-05-09 18:47:25 +02:00
|
|
|
* that it's intended for. If we're not the origin of the circuit, package
|
|
|
|
* the 'created' cell in an 'extended' relay cell and pass it back. If we
|
2004-05-07 10:07:41 +02:00
|
|
|
* are the origin of the circuit, send it to circuit_finish_handshake() to
|
|
|
|
* finish processing keys, and then call circuit_send_next_onion_skin() to
|
|
|
|
* extend to the next hop in the circuit if necessary.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_process_created_cell(cell_t *cell, channel_t *chan)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2003-05-06 01:24:46 +02:00
|
|
|
circuit_t *circ;
|
2012-12-06 04:34:49 +01:00
|
|
|
extended_cell_t extended_cell;
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
circ = circuit_get_by_circid_channel(cell->circ_id, chan);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_OR,
|
|
|
|
"(circID %d) unknown circ (probably got a destroy earlier). "
|
|
|
|
"Dropping.", cell->circ_id);
|
2003-05-06 01:24:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_circ_id != cell->circ_id) {
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
|
2006-09-07 03:00:37 +02:00
|
|
|
"got created cell from Tor client? Closing.");
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
2003-05-06 01:24:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-06 04:34:49 +01:00
|
|
|
if (created_cell_parse(&extended_cell.created_cell, cell) < 0) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unparseable created cell.");
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,"at OP. Finishing handshake.");
|
2012-12-06 04:34:49 +01:00
|
|
|
if ((err_reason = circuit_finish_handshake(origin_circ,
|
|
|
|
&extended_cell.created_cell)) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_OR,"circuit_finish_handshake failed.");
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(circ, -err_reason);
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,"Moving to next skin.");
|
2006-10-09 17:47:27 +02:00
|
|
|
if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_OR,"circuit_send_next_onion_skin failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
/* XXX push this circuit_close lower */
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(circ, -err_reason);
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2003-05-06 01:24:46 +02:00
|
|
|
} else { /* pack it into an extended relay cell, and send it. */
|
2012-12-06 04:34:49 +01:00
|
|
|
uint8_t command=0;
|
|
|
|
uint16_t len=0;
|
|
|
|
uint8_t payload[RELAY_PAYLOAD_SIZE];
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,
|
|
|
|
"Converting created cell to extended relay cell, sending.");
|
2012-12-06 04:34:49 +01:00
|
|
|
memset(payload, 0, sizeof(payload));
|
|
|
|
if (extended_cell.created_cell.cell_type == CELL_CREATED2)
|
|
|
|
extended_cell.cell_type = RELAY_COMMAND_EXTENDED2;
|
|
|
|
else
|
|
|
|
extended_cell.cell_type = RELAY_COMMAND_EXTENDED;
|
|
|
|
if (extended_cell_format(&command, &len, payload, &extended_cell) < 0) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_OR, "Can't format extended cell.");
|
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
relay_send_command_from_edge(0, circ, command,
|
|
|
|
(const char*)payload, len, NULL);
|
2002-11-27 05:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2008-07-23 17:58:38 +02:00
|
|
|
/** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
|
|
|
|
* <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
|
2004-05-07 10:07:41 +02:00
|
|
|
* circuit_receive_relay_cell() for actual processing.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_process_relay_cell(cell_t *cell, channel_t *chan)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2002-07-18 08:37:58 +02:00
|
|
|
circuit_t *circ;
|
2007-03-26 16:07:59 +02:00
|
|
|
int reason, direction;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
circ = circuit_get_by_circid_channel(cell->circ_id, chan);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,
|
2012-08-24 04:30:49 +02:00
|
|
|
"unknown circuit %d on connection from %s. Dropping.",
|
|
|
|
cell->circ_id, channel_get_canonical_remote_descr(chan));
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
2002-11-27 05:08:20 +01:00
|
|
|
return;
|
|
|
|
}
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2007-07-30 00:13:44 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2007-12-04 19:35:03 +01:00
|
|
|
/* if we're a relay and treating connections with recent local
|
2007-07-30 00:13:44 +02:00
|
|
|
* traffic better, then this is one of them. */
|
2012-08-24 04:30:49 +02:00
|
|
|
channel_timestamp_client(chan);
|
2007-07-30 00:13:44 +02:00
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ) &&
|
2007-03-26 16:07:59 +02:00
|
|
|
cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
|
|
|
|
direction = CELL_DIRECTION_OUT;
|
|
|
|
else
|
|
|
|
direction = CELL_DIRECTION_IN;
|
|
|
|
|
2008-07-23 17:58:38 +02:00
|
|
|
/* If we have a relay_early cell, make sure that it's outbound, and we've
|
|
|
|
* gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
|
|
|
|
if (cell->command == CELL_RELAY_EARLY) {
|
|
|
|
if (direction == CELL_DIRECTION_IN) {
|
2009-10-26 06:32:27 +01:00
|
|
|
/* Allow an unlimited number of inbound relay_early cells,
|
|
|
|
* for hidden service compatibility. There isn't any way to make
|
|
|
|
* a long circuit through inbound relay_early cells anyway. See
|
|
|
|
* bug 1038. -RD */
|
2008-07-23 17:58:38 +02:00
|
|
|
} else {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
if (or_circ->remaining_relay_early_cells == 0) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_OR,
|
2012-08-24 04:30:49 +02:00
|
|
|
"Received too many RELAY_EARLY cells on circ %d from %s."
|
2008-07-23 17:58:38 +02:00
|
|
|
" Closing circuit.",
|
2012-08-24 04:30:49 +02:00
|
|
|
cell->circ_id,
|
|
|
|
safe_str(channel_get_canonical_remote_descr(chan)));
|
2008-07-23 17:58:38 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--or_circ->remaining_relay_early_cells;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
|
|
|
|
"(%s) failed. Closing.",
|
|
|
|
direction==CELL_DIRECTION_OUT?"forward":"backward");
|
|
|
|
circuit_mark_for_close(circ, -reason);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Process a 'destroy' <b>cell</b> that just arrived from
|
2012-08-24 04:30:49 +02:00
|
|
|
* <b>chan</b>. Find the circ that it refers to (if any).
|
2004-05-07 10:07:41 +02:00
|
|
|
*
|
|
|
|
* If the circ is in state
|
|
|
|
* onionskin_pending, then call onion_pending_remove() to remove it
|
|
|
|
* from the pending onion list (note that if it's already being
|
|
|
|
* processed by the cpuworker, it won't be in the list anymore; but
|
|
|
|
* when the cpuworker returns it, the circuit will be gone, and the
|
|
|
|
* cpuworker response will be dropped).
|
|
|
|
*
|
|
|
|
* Then mark the circuit for close (which marks all edges for close,
|
|
|
|
* and passes the destroy cell onward if necessary).
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2012-08-24 04:30:49 +02:00
|
|
|
command_process_destroy_cell(cell_t *cell, channel_t *chan)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_t *circ;
|
2006-10-20 01:04:49 +02:00
|
|
|
int reason;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
circ = circuit_get_by_circid_channel(cell->circ_id, chan);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2012-08-24 04:30:49 +02:00
|
|
|
log_info(LD_OR,"unknown circuit %d on connection from %s. Dropping.",
|
|
|
|
cell->circ_id, channel_get_canonical_remote_descr(chan));
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2012-05-18 14:22:03 +02:00
|
|
|
reason = (uint8_t)cell->payload[0];
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
|
2004-04-26 06:32:01 +02:00
|
|
|
/* the destroy came from behind */
|
2012-08-24 04:30:49 +02:00
|
|
|
circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ), 0, NULL);
|
2006-11-01 00:35:50 +01:00
|
|
|
circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
|
2003-06-13 11:20:23 +02:00
|
|
|
} else { /* the destroy came from ahead */
|
2012-08-24 04:30:49 +02:00
|
|
|
circuit_set_n_circid_chan(circ, 0, NULL);
|
2005-04-03 07:25:26 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2006-10-20 01:04:49 +02:00
|
|
|
circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
|
2005-04-03 07:25:26 +02:00
|
|
|
} else {
|
2006-01-05 22:23:03 +01:00
|
|
|
char payload[1];
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_OR, "Delivering 'truncated' back.");
|
2006-01-05 22:23:03 +01:00
|
|
|
payload[0] = (char)reason;
|
2007-03-24 16:58:11 +01:00
|
|
|
relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
|
|
|
|
payload, sizeof(payload), NULL);
|
2004-04-26 06:32:01 +02:00
|
|
|
}
|
2003-06-13 11:20:23 +02:00
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Callback to handle a new channel; call command_setup_channel() to give
|
|
|
|
* it the right cell handlers.
|
2011-10-27 02:19:29 +02:00
|
|
|
*/
|
|
|
|
|
2007-10-30 19:31:30 +01:00
|
|
|
static void
|
2012-10-09 09:51:33 +02:00
|
|
|
command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
|
2007-10-30 19:31:30 +01:00
|
|
|
{
|
2012-08-24 04:30:49 +02:00
|
|
|
tor_assert(listener);
|
|
|
|
tor_assert(chan);
|
2008-02-10 19:40:23 +01:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
command_setup_channel(chan);
|
2007-10-30 19:31:30 +01:00
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Given a channel, install the right handlers to process incoming
|
|
|
|
* cells on it.
|
2011-09-13 22:24:49 +02:00
|
|
|
*/
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
void
|
|
|
|
command_setup_channel(channel_t *chan)
|
2011-09-13 22:24:49 +02:00
|
|
|
{
|
2012-08-24 04:30:49 +02:00
|
|
|
tor_assert(chan);
|
2011-11-03 17:40:02 +01:00
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
channel_set_cell_handlers(chan,
|
|
|
|
command_process_cell,
|
|
|
|
command_process_var_cell);
|
2011-09-13 22:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
/** Given a listener, install the right handler to process incoming
|
|
|
|
* channels on it.
|
2011-09-13 22:24:49 +02:00
|
|
|
*/
|
|
|
|
|
2012-08-24 04:30:49 +02:00
|
|
|
void
|
2012-10-09 09:51:33 +02:00
|
|
|
command_setup_listener(channel_listener_t *listener)
|
2012-08-24 04:30:49 +02:00
|
|
|
{
|
|
|
|
tor_assert(listener);
|
2012-10-09 09:51:33 +02:00
|
|
|
tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
|
2011-09-27 19:15:36 +02:00
|
|
|
|
2012-10-09 09:51:33 +02:00
|
|
|
channel_listener_set_listener_fn(listener, command_handle_incoming_channel);
|
2011-09-13 22:24:49 +02:00
|
|
|
}
|
|
|
|
|