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.
|
2008-02-07 06:31:47 +01:00
|
|
|
* Copyright (c) 2007-2008, The Tor Project, Inc. */
|
2004-05-12 21:49:48 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 23:00:58 +01:00
|
|
|
const char relay_c_id[] =
|
|
|
|
"$Id$";
|
2004-05-12 21:49:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file relay.c
|
2004-05-13 09:24:49 +02:00
|
|
|
* \brief Handle relay cell encryption/decryption, plus packaging and
|
2007-03-26 16:07:59 +02:00
|
|
|
* receiving from circuits, plus queueing on circuits.
|
2004-05-12 21:49:48 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
2007-08-08 07:50:31 +02:00
|
|
|
#include "mempool.h"
|
2004-05-12 21:49:48 +02:00
|
|
|
|
|
|
|
static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
|
|
|
|
crypt_path_t **layer_hint, char *recognized);
|
2006-07-26 21:07:26 +02:00
|
|
|
static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
|
2007-07-12 19:09:19 +02:00
|
|
|
int cell_direction,
|
|
|
|
crypt_path_t *layer_hint);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn,
|
2004-05-13 09:24:49 +02:00
|
|
|
crypt_path_t *layer_hint);
|
|
|
|
static void
|
|
|
|
circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
|
|
|
|
static void
|
|
|
|
circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
|
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_resume_edge_reading_helper(edge_connection_t *conn,
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ,
|
|
|
|
crypt_path_t *layer_hint);
|
|
|
|
static int
|
|
|
|
circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
|
2004-05-12 21:49:48 +02:00
|
|
|
|
|
|
|
/** Stats: how many relay cells have originated at this hop, or have
|
|
|
|
* been relayed onward (not recognized at this hop)?
|
|
|
|
*/
|
2005-12-31 09:09:26 +01:00
|
|
|
uint64_t stats_n_relay_cells_relayed = 0;
|
2004-05-12 21:49:48 +02:00
|
|
|
/** Stats: how many relay cells have been delivered to streams at this
|
|
|
|
* hop?
|
|
|
|
*/
|
2005-12-31 09:09:26 +01:00
|
|
|
uint64_t stats_n_relay_cells_delivered = 0;
|
2004-05-12 21:49:48 +02:00
|
|
|
|
|
|
|
/** Update digest from the payload of cell. Assign integrity part to
|
|
|
|
* cell.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
relay_set_digest(crypto_digest_env_t *digest, cell_t *cell)
|
|
|
|
{
|
2004-05-12 21:49:48 +02:00
|
|
|
char integrity[4];
|
|
|
|
relay_header_t rh;
|
|
|
|
|
|
|
|
crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
|
|
|
|
crypto_digest_get_digest(digest, integrity, 4);
|
|
|
|
// log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
|
|
|
|
// integrity[0], integrity[1], integrity[2], integrity[3]);
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
memcpy(rh.integrity, integrity, 4);
|
|
|
|
relay_header_pack(cell->payload, &rh);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Does the digest for this circuit indicate that this cell is for us?
|
|
|
|
*
|
|
|
|
* Update digest from the payload of cell (with the integrity part set
|
|
|
|
* to 0). If the integrity part is valid, return 1, else restore digest
|
|
|
|
* and cell to their original state and return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell)
|
|
|
|
{
|
2004-05-12 21:49:48 +02:00
|
|
|
char received_integrity[4], calculated_integrity[4];
|
|
|
|
relay_header_t rh;
|
|
|
|
crypto_digest_env_t *backup_digest=NULL;
|
|
|
|
|
|
|
|
backup_digest = crypto_digest_dup(digest);
|
|
|
|
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
memcpy(received_integrity, rh.integrity, 4);
|
|
|
|
memset(rh.integrity, 0, 4);
|
|
|
|
relay_header_pack(cell->payload, &rh);
|
|
|
|
|
|
|
|
// log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
|
|
|
|
// received_integrity[0], received_integrity[1],
|
|
|
|
// received_integrity[2], received_integrity[3]);
|
|
|
|
|
|
|
|
crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
|
|
|
|
crypto_digest_get_digest(digest, calculated_integrity, 4);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (memcmp(received_integrity, calculated_integrity, 4)) {
|
2004-05-12 21:49:48 +02:00
|
|
|
// log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
|
|
|
|
// (%d vs %d).", received_integrity, calculated_integrity);
|
|
|
|
/* restore digest to its old form */
|
|
|
|
crypto_digest_assign(digest, backup_digest);
|
|
|
|
/* restore the relay header */
|
|
|
|
memcpy(rh.integrity, received_integrity, 4);
|
|
|
|
relay_header_pack(cell->payload, &rh);
|
|
|
|
crypto_free_digest_env(backup_digest);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
crypto_free_digest_env(backup_digest);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
|
|
|
|
* (in place).
|
|
|
|
*
|
|
|
|
* If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
|
|
|
|
*
|
|
|
|
* Return -1 if the crypto fails, else return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
|
|
|
|
int encrypt_mode)
|
|
|
|
{
|
2005-12-14 21:40:40 +01:00
|
|
|
int r;
|
2008-02-07 17:10:33 +01:00
|
|
|
(void)encrypt_mode;
|
|
|
|
r = crypto_cipher_crypt_inplace(cipher, in, CELL_PAYLOAD_SIZE);
|
2005-12-14 21:40:40 +01:00
|
|
|
|
|
|
|
if (r) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"Error during relay encryption");
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Receive a relay cell:
|
2007-03-26 16:08:35 +02:00
|
|
|
* - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
|
|
|
|
* origin; decrypt if we're headed toward the exit).
|
2004-05-12 21:49:48 +02:00
|
|
|
* - Check if recognized (if exitward).
|
2007-03-26 16:08:35 +02:00
|
|
|
* - If recognized and the digest checks out, then find if there's a stream
|
|
|
|
* that the cell is intended for, and deliver it to the right
|
2004-05-12 21:49:48 +02:00
|
|
|
* connection_edge.
|
2007-03-26 16:08:35 +02:00
|
|
|
* - If not recognized, then we need to relay it: append it to the appropriate
|
|
|
|
* cell_queue on <b>circ</b>.
|
2006-01-05 22:23:03 +01:00
|
|
|
*
|
2007-03-26 16:08:35 +02:00
|
|
|
* Return -<b>reason</b> on failure.
|
2004-05-12 21:49:48 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
circuit_receive_relay_cell(cell_t *cell, circuit_t *circ, int cell_direction)
|
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *or_conn=NULL;
|
2004-05-12 21:49:48 +02:00
|
|
|
crypt_path_t *layer_hint=NULL;
|
|
|
|
char recognized=0;
|
2006-01-05 22:23:03 +01:00
|
|
|
int reason;
|
2004-05-12 21:49:48 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(cell);
|
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(cell_direction == CELL_DIRECTION_OUT ||
|
|
|
|
cell_direction == CELL_DIRECTION_IN);
|
2004-05-12 21:49:48 +02:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
return 0;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"relay crypt failed. Dropping connection.");
|
2006-10-13 07:27:59 +02:00
|
|
|
return -END_CIRC_REASON_INTERNAL;
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (recognized) {
|
2007-07-12 19:09:19 +02:00
|
|
|
edge_connection_t *conn = relay_lookup_conn(circ, cell, cell_direction,
|
|
|
|
layer_hint);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cell_direction == CELL_DIRECTION_OUT) {
|
2004-05-12 21:49:48 +02:00
|
|
|
++stats_n_relay_cells_delivered;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_OR,"Sending away from origin.");
|
2006-01-05 22:23:03 +01:00
|
|
|
if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
|
|
|
|
< 0) {
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-12-14 21:40:40 +01:00
|
|
|
"connection_edge_process_relay_cell (away from origin) "
|
|
|
|
"failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return reason;
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cell_direction == CELL_DIRECTION_IN) {
|
2004-05-12 21:49:48 +02:00
|
|
|
++stats_n_relay_cells_delivered;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_OR,"Sending to origin.");
|
2006-01-05 22:23:03 +01:00
|
|
|
if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
|
|
|
|
layer_hint)) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_OR,
|
|
|
|
"connection_edge_process_relay_cell (at origin) failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return reason;
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not recognized. pass it on. */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cell_direction == CELL_DIRECTION_OUT) {
|
2004-05-12 21:49:48 +02:00
|
|
|
cell->circ_id = circ->n_circ_id; /* switch it */
|
2006-07-26 21:07:26 +02:00
|
|
|
or_conn = circ->n_conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
} else if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
|
2006-07-26 21:07:26 +02:00
|
|
|
or_conn = TO_OR_CIRCUIT(circ)->p_conn;
|
2004-05-12 21:49:48 +02:00
|
|
|
} else {
|
2006-12-29 03:47:51 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_OR,
|
|
|
|
"Dropping unrecognized inbound cell on origin circuit.");
|
2006-07-23 09:37:35 +02:00
|
|
|
return 0;
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!or_conn) {
|
2006-07-23 09:37:35 +02:00
|
|
|
// XXXX Can this splice stuff be done more cleanly?
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
TO_OR_CIRCUIT(circ)->rend_splice &&
|
|
|
|
cell_direction == CELL_DIRECTION_OUT) {
|
|
|
|
or_circuit_t *splice = TO_OR_CIRCUIT(circ)->rend_splice;
|
2004-05-12 21:49:48 +02:00
|
|
|
tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(splice->_base.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
|
|
|
|
cell->circ_id = splice->p_circ_id;
|
|
|
|
if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice),
|
2006-01-05 22:23:03 +01:00
|
|
|
CELL_DIRECTION_IN)) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
|
|
|
|
"circuits");
|
2005-12-14 21:40:40 +01:00
|
|
|
/* XXXX Do this here, or just return -1? */
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, -reason);
|
|
|
|
return reason;
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-12-10 10:36:26 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Didn't recognize cell, but circ stops here! Closing circ.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_OR,"Passing on unrecognized cell.");
|
2007-03-26 16:07:59 +02:00
|
|
|
|
|
|
|
++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
|
|
|
|
* we might kill the circ before we relay
|
|
|
|
* the cells. */
|
|
|
|
|
|
|
|
append_cell_to_circuit_queue(circ, or_conn, cell, cell_direction);
|
2004-05-12 21:49:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Do the appropriate en/decryptions for <b>cell</b> arriving on
|
|
|
|
* <b>circ</b> in direction <b>cell_direction</b>.
|
|
|
|
*
|
|
|
|
* If cell_direction == CELL_DIRECTION_IN:
|
|
|
|
* - If we're at the origin (we're the OP), for hops 1..N,
|
|
|
|
* decrypt cell. If recognized, stop.
|
|
|
|
* - Else (we're not the OP), encrypt one hop. Cell is not recognized.
|
|
|
|
*
|
|
|
|
* If cell_direction == CELL_DIRECTION_OUT:
|
|
|
|
* - decrypt one hop. Check if recognized.
|
|
|
|
*
|
|
|
|
* If cell is recognized, set *recognized to 1, and set
|
|
|
|
* *layer_hint to the hop that recognized it.
|
|
|
|
*
|
|
|
|
* Return -1 to indicate that we should mark the circuit for close,
|
|
|
|
* else return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
|
|
|
|
crypt_path_t **layer_hint, char *recognized)
|
|
|
|
{
|
2004-05-12 21:49:48 +02:00
|
|
|
relay_header_t rh;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(cell);
|
|
|
|
tor_assert(recognized);
|
|
|
|
tor_assert(cell_direction == CELL_DIRECTION_IN ||
|
|
|
|
cell_direction == CELL_DIRECTION_OUT);
|
2004-05-12 21:49:48 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cell_direction == CELL_DIRECTION_IN) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
|
2005-12-14 21:40:40 +01:00
|
|
|
* We'll want to do layered decrypts. */
|
2006-07-23 09:37:35 +02:00
|
|
|
crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
|
|
|
|
thishop = cpath;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (thishop->state != CPATH_STATE_OPEN) {
|
2005-12-10 10:36:26 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Relay cell before first created cell? Closing.");
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
do { /* Remember: cpath is in forward order, that is, first hop first. */
|
|
|
|
tor_assert(thishop);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (rh.recognized == 0) {
|
2004-05-12 21:49:48 +02:00
|
|
|
/* it's possibly recognized. have to check digest to be sure. */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (relay_digest_matches(thishop->b_digest, cell)) {
|
2004-05-12 21:49:48 +02:00
|
|
|
*recognized = 1;
|
|
|
|
*layer_hint = thishop;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thishop = thishop->next;
|
2006-07-23 09:37:35 +02:00
|
|
|
} while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_OR,
|
|
|
|
"Incoming cell at client not recognized. Closing.");
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
} else { /* we're in the middle. Just one crypt. */
|
2006-07-23 09:37:35 +02:00
|
|
|
if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto,
|
|
|
|
cell->payload, 1) < 0)
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
2005-12-14 21:40:40 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping recognized check, because we're not "
|
2006-09-07 03:00:37 +02:00
|
|
|
// "the client.");
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
} else /* cell_direction == CELL_DIRECTION_OUT */ {
|
|
|
|
/* we're in the middle. Just one crypt. */
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto,
|
|
|
|
cell->payload, 0) < 0)
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
if (rh.recognized == 0) {
|
|
|
|
/* it's possibly recognized. have to check digest to be sure. */
|
2006-07-23 09:37:35 +02:00
|
|
|
if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
|
2004-05-12 21:49:48 +02:00
|
|
|
*recognized = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** Package a relay cell from an edge:
|
2004-05-12 21:49:48 +02:00
|
|
|
* - Encrypt it to the right layer
|
2007-04-10 01:15:46 +02:00
|
|
|
* - Append it to the appropriate cell_queue on <b>circ</b>.
|
2004-05-12 21:49:48 +02:00
|
|
|
*/
|
2004-05-15 09:21:25 +02:00
|
|
|
static int
|
2004-05-12 21:49:48 +02:00
|
|
|
circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
|
|
|
|
int cell_direction,
|
|
|
|
crypt_path_t *layer_hint)
|
2004-07-23 01:21:12 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *conn; /* where to send the cell */
|
2004-05-12 21:49:48 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cell_direction == CELL_DIRECTION_OUT) {
|
2006-07-23 09:37:35 +02:00
|
|
|
crypt_path_t *thishop; /* counter for repeated crypts */
|
2004-05-12 21:49:48 +02:00
|
|
|
conn = circ->n_conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ) || !conn) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"outgoing relay cell has n_conn==NULL. Dropping.");
|
2004-05-12 21:49:48 +02:00
|
|
|
return 0; /* just drop it */
|
|
|
|
}
|
|
|
|
relay_set_digest(layer_hint->f_digest, cell);
|
|
|
|
|
|
|
|
thishop = layer_hint;
|
|
|
|
/* moving from farthest to nearest hop */
|
|
|
|
do {
|
|
|
|
tor_assert(thishop);
|
2005-10-25 09:04:36 +02:00
|
|
|
/* XXXX RD This is a bug, right? */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_OR,"crypting a layer of the relay cell.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
thishop = thishop->prev;
|
2006-07-23 09:37:35 +02:00
|
|
|
} while (thishop != TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
|
2004-05-12 21:49:48 +02:00
|
|
|
|
|
|
|
} else { /* incoming cell */
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *or_circ;
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2007-05-24 20:12:52 +02:00
|
|
|
/* We should never package an _incoming_ cell from the circuit
|
|
|
|
* origin; that means we messed up somewhere. */
|
2006-07-23 09:37:35 +02:00
|
|
|
log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
|
2005-11-30 04:01:16 +01:00
|
|
|
assert_circuit_ok(circ);
|
2004-05-12 21:49:48 +02:00
|
|
|
return 0; /* just drop it */
|
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
conn = or_circ->p_conn;
|
|
|
|
relay_set_digest(or_circ->p_digest, cell);
|
|
|
|
if (relay_crypt_one_payload(or_circ->p_crypto, cell->payload, 1) < 0)
|
2004-05-12 21:49:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
++stats_n_relay_cells_relayed;
|
2007-03-26 16:07:59 +02:00
|
|
|
|
|
|
|
append_cell_to_circuit_queue(circ, conn, cell, cell_direction);
|
2004-05-12 21:49:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If cell's stream_id matches the stream_id of any conn that's
|
|
|
|
* attached to circ, return that conn, else return NULL.
|
|
|
|
*/
|
2006-07-26 21:07:26 +02:00
|
|
|
static edge_connection_t *
|
2007-07-12 19:09:19 +02:00
|
|
|
relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction,
|
|
|
|
crypt_path_t *layer_hint)
|
2004-05-12 21:49:48 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *tmpconn;
|
2004-05-12 21:49:48 +02:00
|
|
|
relay_header_t rh;
|
|
|
|
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!rh.stream_id)
|
2004-05-12 21:49:48 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* IN or OUT cells could have come from either direction, now
|
|
|
|
* that we allow rendezvous *to* an OP.
|
|
|
|
*/
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (rh.stream_id == tmpconn->stream_id &&
|
2007-07-12 19:09:19 +02:00
|
|
|
!tmpconn->_base.marked_for_close &&
|
|
|
|
tmpconn->cpath_layer == layer_hint) {
|
2006-07-23 09:37:35 +02:00
|
|
|
log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
|
2004-05-12 21:49:48 +02:00
|
|
|
return tmpconn;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
} else {
|
|
|
|
for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:37 +02:00
|
|
|
if (rh.stream_id == tmpconn->stream_id &&
|
|
|
|
!tmpconn->_base.marked_for_close) {
|
2006-07-23 09:37:35 +02:00
|
|
|
log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
|
|
|
|
if (cell_direction == CELL_DIRECTION_OUT ||
|
|
|
|
connection_edge_is_rendezvous_stream(tmpconn))
|
|
|
|
return tmpconn;
|
|
|
|
}
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:37 +02:00
|
|
|
if (rh.stream_id == tmpconn->stream_id &&
|
|
|
|
!tmpconn->_base.marked_for_close) {
|
2006-07-23 09:37:35 +02:00
|
|
|
log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
|
|
|
|
return tmpconn;
|
|
|
|
}
|
2004-05-12 21:49:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL; /* probably a begin relay cell */
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Pack the relay_header_t host-order structure <b>src</b> into
|
|
|
|
* network-order in the buffer <b>dest</b>. See tor-spec.txt for details
|
|
|
|
* about the wire format.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
relay_header_pack(char *dest, const relay_header_t *src)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
*(uint8_t*)(dest) = src->command;
|
|
|
|
|
|
|
|
set_uint16(dest+1, htons(src->recognized));
|
|
|
|
set_uint16(dest+3, htons(src->stream_id));
|
|
|
|
memcpy(dest+5, src->integrity, 4);
|
|
|
|
set_uint16(dest+9, htons(src->length));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Unpack the network-order buffer <b>src</b> into a host-order
|
|
|
|
* relay_header_t structure <b>dest</b>.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
relay_header_unpack(relay_header_t *dest, const char *src)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
dest->command = *(uint8_t*)(src);
|
|
|
|
|
|
|
|
dest->recognized = ntohs(get_uint16(src+1));
|
|
|
|
dest->stream_id = ntohs(get_uint16(src+3));
|
|
|
|
memcpy(dest->integrity, src+5, 4);
|
|
|
|
dest->length = ntohs(get_uint16(src+9));
|
|
|
|
}
|
|
|
|
|
2006-10-20 16:57:46 +02:00
|
|
|
/** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
|
|
|
|
* it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
|
|
|
|
* <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
|
|
|
|
* control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
|
|
|
|
* destination hop for OP->OR cells.
|
2004-05-13 09:24:49 +02:00
|
|
|
*
|
2006-10-20 16:57:46 +02:00
|
|
|
* If you can't send the cell, mark the circuit for close and return -1. Else
|
|
|
|
* return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-10-20 16:57:46 +02:00
|
|
|
relay_send_command_from_edge(uint16_t stream_id, circuit_t *circ,
|
2005-06-11 20:52:12 +02:00
|
|
|
int relay_command, const char *payload,
|
|
|
|
size_t payload_len, crypt_path_t *cpath_layer)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
cell_t cell;
|
|
|
|
relay_header_t rh;
|
|
|
|
int cell_direction;
|
2006-07-23 09:37:35 +02:00
|
|
|
/* XXXX NM Split this function into a separate versions per circuit type? */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-10-20 16:57:46 +02:00
|
|
|
tor_assert(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
|
|
|
cell.command = CELL_RELAY;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cpath_layer) {
|
2004-05-13 09:24:49 +02:00
|
|
|
cell.circ_id = circ->n_circ_id;
|
|
|
|
cell_direction = CELL_DIRECTION_OUT;
|
2006-07-23 09:37:35 +02:00
|
|
|
} else if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
|
2004-05-13 09:24:49 +02:00
|
|
|
cell_direction = CELL_DIRECTION_IN;
|
2006-07-23 09:37:35 +02:00
|
|
|
} else {
|
|
|
|
return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&rh, 0, sizeof(rh));
|
|
|
|
rh.command = relay_command;
|
2006-10-20 16:57:46 +02:00
|
|
|
rh.stream_id = stream_id;
|
2004-05-13 09:24:49 +02:00
|
|
|
rh.length = payload_len;
|
|
|
|
relay_header_pack(cell.payload, &rh);
|
2005-02-24 11:22:00 +01:00
|
|
|
if (payload_len) {
|
|
|
|
tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
|
2004-05-13 09:24:49 +02:00
|
|
|
memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
|
2005-02-24 11:22:00 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_OR,"delivering %d cell %s.", relay_command,
|
|
|
|
cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-07-30 00:13:44 +02:00
|
|
|
if (cell_direction == CELL_DIRECTION_OUT && circ->n_conn) {
|
|
|
|
/* if we're using relaybandwidthrate, this conn wants priority */
|
2008-02-15 20:20:53 +01:00
|
|
|
/* XXXX021 the call to time() seems little too frequent */
|
2007-07-30 00:13:44 +02:00
|
|
|
circ->n_conn->client_used = time(NULL);
|
|
|
|
}
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer)
|
|
|
|
< 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-20 16:57:46 +02:00
|
|
|
/** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
|
|
|
|
* send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
|
|
|
|
* that's sending the relay cell, or NULL if it's a control cell.
|
|
|
|
* <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
|
|
|
|
* for OP->OR cells.
|
|
|
|
*
|
|
|
|
* If you can't send the cell, mark the circuit for close and
|
|
|
|
* return -1. Else return 0.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-24 16:58:11 +01:00
|
|
|
connection_edge_send_command(edge_connection_t *fromconn,
|
2006-10-20 16:57:46 +02:00
|
|
|
int relay_command, const char *payload,
|
2007-03-24 16:57:51 +01:00
|
|
|
size_t payload_len)
|
2006-10-20 16:57:46 +02:00
|
|
|
{
|
|
|
|
/* XXXX NM Split this function into a separate versions per circuit type? */
|
2007-03-24 16:58:11 +01:00
|
|
|
circuit_t *circ;
|
|
|
|
tor_assert(fromconn);
|
|
|
|
circ = fromconn->on_circuit;
|
2006-10-20 16:57:46 +02:00
|
|
|
|
2007-03-24 16:58:11 +01:00
|
|
|
if (fromconn->_base.marked_for_close) {
|
2006-10-20 16:57:46 +02:00
|
|
|
log_warn(LD_BUG,
|
2007-03-04 21:11:46 +01:00
|
|
|
"called on conn that's already marked for close at %s:%d.",
|
2006-10-20 16:57:46 +02:00
|
|
|
fromconn->_base.marked_for_close_file,
|
|
|
|
fromconn->_base.marked_for_close);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!circ) {
|
|
|
|
if (fromconn->_base.type == CONN_TYPE_AP) {
|
|
|
|
log_info(LD_APP,"no circ. Closing conn.");
|
|
|
|
connection_mark_unattached_ap(fromconn, END_STREAM_REASON_INTERNAL);
|
|
|
|
} else {
|
|
|
|
log_info(LD_EXIT,"no circ. Closing conn.");
|
|
|
|
fromconn->_base.edge_has_sent_end = 1; /* no circ to send to */
|
2006-10-20 19:54:48 +02:00
|
|
|
fromconn->end_reason = END_STREAM_REASON_INTERNAL;
|
2006-10-20 16:57:46 +02:00
|
|
|
connection_mark_for_close(TO_CONN(fromconn));
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-03-24 16:58:11 +01:00
|
|
|
return relay_send_command_from_edge(fromconn->stream_id, circ,
|
|
|
|
relay_command, payload,
|
|
|
|
payload_len, fromconn->cpath_layer);
|
2006-10-20 16:57:46 +02:00
|
|
|
}
|
|
|
|
|
2005-04-04 05:30:49 +02:00
|
|
|
/** Translate <b>reason</b>, which came from a relay 'end' cell,
|
|
|
|
* into a static const string describing why the stream is closing.
|
|
|
|
* <b>reason</b> is -1 if no reason was provided.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
static const char *
|
2005-06-11 20:52:12 +02:00
|
|
|
connection_edge_end_reason_str(int reason)
|
|
|
|
{
|
2005-04-04 05:30:49 +02:00
|
|
|
switch (reason) {
|
|
|
|
case -1:
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2006-10-29 08:41:25 +01:00
|
|
|
"End cell arrived with length 0. Should be at least 1.");
|
2005-04-04 05:30:49 +02:00
|
|
|
return "MALFORMED";
|
2004-05-13 09:24:49 +02:00
|
|
|
case END_STREAM_REASON_MISC: return "misc error";
|
|
|
|
case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
|
2005-03-01 23:16:15 +01:00
|
|
|
case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
|
2004-05-13 09:24:49 +02:00
|
|
|
case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
|
|
|
|
case END_STREAM_REASON_DESTROY: return "destroyed";
|
|
|
|
case END_STREAM_REASON_DONE: return "closed normally";
|
|
|
|
case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
|
2005-03-01 23:16:15 +01:00
|
|
|
case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
|
|
|
|
case END_STREAM_REASON_INTERNAL: return "internal error at server";
|
|
|
|
case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
|
|
|
|
case END_STREAM_REASON_CONNRESET: return "connection reset";
|
2005-03-01 23:42:31 +01:00
|
|
|
case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
|
2006-10-20 16:57:46 +02:00
|
|
|
case END_STREAM_REASON_NOTDIRECTORY: return "not a directory";
|
2005-03-01 23:16:15 +01:00
|
|
|
default:
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Reason for ending (%d) not recognized.",reason);
|
2005-03-01 23:16:15 +01:00
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-22 20:36:38 +01:00
|
|
|
/** Translate <b>reason</b> (as from a relay 'end' cell) into an
|
|
|
|
* appropriate SOCKS5 reply code.
|
2008-01-20 06:54:15 +01:00
|
|
|
*
|
|
|
|
* A reason of 0 means that we're not actually expecting to send
|
|
|
|
* this code back to the socks client; we just call it 'succeeded'
|
|
|
|
* to keep things simple.
|
2005-03-17 13:38:37 +01:00
|
|
|
*/
|
2005-03-22 20:36:38 +01:00
|
|
|
socks5_reply_status_t
|
|
|
|
connection_edge_end_reason_socks5_response(int reason)
|
|
|
|
{
|
2007-02-07 07:54:27 +01:00
|
|
|
switch (reason & END_STREAM_REASON_MASK) {
|
2006-10-20 19:54:43 +02:00
|
|
|
case 0:
|
|
|
|
return SOCKS5_SUCCEEDED;
|
2005-03-01 23:16:15 +01:00
|
|
|
case END_STREAM_REASON_MISC:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
case END_STREAM_REASON_RESOLVEFAILED:
|
|
|
|
return SOCKS5_HOST_UNREACHABLE;
|
|
|
|
case END_STREAM_REASON_CONNECTREFUSED:
|
|
|
|
return SOCKS5_CONNECTION_REFUSED;
|
2008-01-20 06:54:15 +01:00
|
|
|
case END_STREAM_REASON_ENTRYPOLICY:
|
|
|
|
return SOCKS5_NOT_ALLOWED;
|
2005-03-01 23:16:15 +01:00
|
|
|
case END_STREAM_REASON_EXITPOLICY:
|
2005-03-27 08:37:56 +02:00
|
|
|
return SOCKS5_NOT_ALLOWED;
|
2005-03-01 23:16:15 +01:00
|
|
|
case END_STREAM_REASON_DESTROY:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
case END_STREAM_REASON_DONE:
|
|
|
|
return SOCKS5_SUCCEEDED;
|
|
|
|
case END_STREAM_REASON_TIMEOUT:
|
2005-03-27 08:37:56 +02:00
|
|
|
return SOCKS5_TTL_EXPIRED;
|
2005-03-01 23:16:15 +01:00
|
|
|
case END_STREAM_REASON_RESOURCELIMIT:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
case END_STREAM_REASON_HIBERNATING:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
case END_STREAM_REASON_INTERNAL:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
case END_STREAM_REASON_CONNRESET:
|
|
|
|
return SOCKS5_CONNECTION_REFUSED;
|
2005-03-01 23:42:31 +01:00
|
|
|
case END_STREAM_REASON_TORPROTOCOL:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
2005-03-27 08:37:56 +02:00
|
|
|
|
|
|
|
case END_STREAM_REASON_CANT_ATTACH:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
case END_STREAM_REASON_NET_UNREACHABLE:
|
|
|
|
return SOCKS5_NET_UNREACHABLE;
|
2006-10-20 19:54:43 +02:00
|
|
|
case END_STREAM_REASON_SOCKSPROTOCOL:
|
|
|
|
return SOCKS5_GENERAL_ERROR;
|
2005-03-01 23:16:15 +01:00
|
|
|
default:
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Reason for ending (%d) not recognized; "
|
|
|
|
"sending generic socks error.", reason);
|
2005-03-01 23:16:15 +01:00
|
|
|
return SOCKS5_GENERAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-11 22:38:06 +01:00
|
|
|
/* We need to use a few macros to deal with the fact that Windows
|
|
|
|
* decided that their sockets interface should be a permakludge.
|
|
|
|
* E_CASE is for errors where windows has both a EFOO and a WSAEFOO
|
|
|
|
* version, and S_CASE is for errors where windows has only a WSAEFOO
|
|
|
|
* version. (The E is for 'error', the S is for 'socket'). */
|
2005-03-01 23:16:15 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#define E_CASE(s) case s: case WSA ## s
|
2005-03-10 19:48:13 +01:00
|
|
|
#define S_CASE(s) case WSA ## s
|
2005-03-01 23:16:15 +01:00
|
|
|
#else
|
|
|
|
#define E_CASE(s) case s
|
2005-03-10 19:48:13 +01:00
|
|
|
#define S_CASE(s) case s
|
2005-03-01 23:16:15 +01:00
|
|
|
#endif
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Given an errno from a failed exit connection, return a reason code
|
|
|
|
* appropriate for use in a RELAY END cell.
|
|
|
|
*/
|
2005-03-01 23:16:15 +01:00
|
|
|
int
|
2005-03-01 23:42:31 +01:00
|
|
|
errno_to_end_reason(int e)
|
2005-03-01 23:16:15 +01:00
|
|
|
{
|
|
|
|
switch (e) {
|
2005-03-10 19:48:13 +01:00
|
|
|
case EPIPE:
|
2005-03-01 23:16:15 +01:00
|
|
|
return END_STREAM_REASON_DONE;
|
|
|
|
E_CASE(EBADF):
|
|
|
|
E_CASE(EFAULT):
|
|
|
|
E_CASE(EINVAL):
|
2005-03-10 19:48:13 +01:00
|
|
|
S_CASE(EISCONN):
|
|
|
|
S_CASE(ENOTSOCK):
|
|
|
|
S_CASE(EPROTONOSUPPORT):
|
|
|
|
S_CASE(EAFNOSUPPORT):
|
2005-03-01 23:16:15 +01:00
|
|
|
E_CASE(EACCES):
|
2005-03-10 19:48:13 +01:00
|
|
|
S_CASE(ENOTCONN):
|
|
|
|
S_CASE(ENETUNREACH):
|
2005-03-01 23:16:15 +01:00
|
|
|
return END_STREAM_REASON_INTERNAL;
|
2005-03-10 19:48:13 +01:00
|
|
|
S_CASE(ECONNREFUSED):
|
2005-03-01 23:16:15 +01:00
|
|
|
return END_STREAM_REASON_CONNECTREFUSED;
|
2005-03-10 19:48:13 +01:00
|
|
|
S_CASE(ECONNRESET):
|
2005-03-01 23:16:15 +01:00
|
|
|
return END_STREAM_REASON_CONNRESET;
|
2005-03-10 19:48:13 +01:00
|
|
|
S_CASE(ETIMEDOUT):
|
2005-03-01 23:16:15 +01:00
|
|
|
return END_STREAM_REASON_TIMEOUT;
|
2005-03-10 19:48:13 +01:00
|
|
|
S_CASE(ENOBUFS):
|
|
|
|
case ENOMEM:
|
|
|
|
case ENFILE:
|
2005-03-01 23:16:15 +01:00
|
|
|
E_CASE(EMFILE):
|
|
|
|
return END_STREAM_REASON_RESOURCELIMIT;
|
|
|
|
default:
|
2006-09-07 03:00:37 +02:00
|
|
|
log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
|
|
|
|
"that we are ending a stream for 'misc' reason.",
|
2006-02-13 11:33:00 +01:00
|
|
|
e, tor_socket_strerror(e));
|
2005-03-01 23:16:15 +01:00
|
|
|
return END_STREAM_REASON_MISC;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** How many times will I retry a stream that fails due to DNS
|
2005-08-13 02:31:41 +02:00
|
|
|
* resolve failure or misc error?
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
#define MAX_RESOLVE_FAILURES 3
|
|
|
|
|
2005-04-04 05:30:49 +02:00
|
|
|
/** Return 1 if reason is something that you should retry if you
|
|
|
|
* get the end cell before you've connected; else return 0. */
|
2004-05-13 09:24:49 +02:00
|
|
|
static int
|
2005-06-11 20:52:12 +02:00
|
|
|
edge_reason_is_retriable(int reason)
|
|
|
|
{
|
2005-04-04 05:30:49 +02:00
|
|
|
return reason == END_STREAM_REASON_HIBERNATING ||
|
|
|
|
reason == END_STREAM_REASON_RESOURCELIMIT ||
|
|
|
|
reason == END_STREAM_REASON_EXITPOLICY ||
|
2005-08-13 02:31:41 +02:00
|
|
|
reason == END_STREAM_REASON_RESOLVEFAILED ||
|
|
|
|
reason == END_STREAM_REASON_MISC;
|
2005-04-04 05:30:49 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Called when we receive an END cell on a stream that isn't open yet.
|
|
|
|
* Arguments are as for connection_edge_process_relay_cell().
|
|
|
|
*/
|
2005-04-04 05:30:49 +02:00
|
|
|
static int
|
|
|
|
connection_edge_process_end_not_open(
|
2006-07-23 09:37:35 +02:00
|
|
|
relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn, crypt_path_t *layer_hint)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-03-13 06:53:17 +01:00
|
|
|
struct in_addr in;
|
2004-08-15 10:15:12 +02:00
|
|
|
routerinfo_t *exitrouter;
|
2005-04-04 05:30:49 +02:00
|
|
|
int reason = *(cell->payload+RELAY_HEADER_SIZE);
|
2006-10-20 19:54:43 +02:00
|
|
|
int control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
|
2006-06-05 00:42:13 +02:00
|
|
|
(void) layer_hint; /* unused */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-01-07 02:02:21 +01:00
|
|
|
if (rh->length > 0 && edge_reason_is_retriable(reason) &&
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.type == CONN_TYPE_AP) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
|
|
|
|
safe_str(conn->socks_request->address),
|
|
|
|
connection_edge_end_reason_str(reason));
|
2005-06-29 23:46:55 +02:00
|
|
|
exitrouter =
|
|
|
|
router_get_by_digest(circ->build_state->chosen_exit->identity_digest);
|
2005-04-04 05:30:49 +02:00
|
|
|
switch (reason) {
|
|
|
|
case END_STREAM_REASON_EXITPOLICY:
|
|
|
|
if (rh->length >= 5) {
|
|
|
|
uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
|
2005-09-02 20:53:31 +02:00
|
|
|
int ttl;
|
2005-04-04 05:30:49 +02:00
|
|
|
if (!addr) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
|
|
|
|
safe_str(conn->socks_request->address));
|
2005-04-04 05:30:49 +02:00
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-02 20:53:31 +02:00
|
|
|
if (rh->length >= 9)
|
|
|
|
ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
|
|
|
|
else
|
|
|
|
ttl = -1;
|
2005-04-04 05:30:49 +02:00
|
|
|
client_dns_set_addressmap(conn->socks_request->address, addr,
|
2005-09-02 20:53:31 +02:00
|
|
|
conn->chosen_exit_name, ttl);
|
2005-04-04 05:30:49 +02:00
|
|
|
}
|
|
|
|
/* check if he *ought* to have allowed it */
|
2005-06-29 23:46:55 +02:00
|
|
|
if (exitrouter &&
|
|
|
|
(rh->length < 5 ||
|
2005-09-09 08:11:37 +02:00
|
|
|
(tor_inet_aton(conn->socks_request->address, &in) &&
|
2005-06-29 23:46:55 +02:00
|
|
|
!conn->chosen_exit_name))) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_notice(LD_APP,
|
2005-12-10 09:27:01 +01:00
|
|
|
"Exitrouter '%s' seems to be more restrictive than its exit "
|
|
|
|
"policy. Not using this router as exit for now.",
|
|
|
|
exitrouter->nickname);
|
2008-01-02 05:43:44 +01:00
|
|
|
policies_set_router_exitpolicy_to_reject_all(exitrouter);
|
2005-04-04 05:30:49 +02:00
|
|
|
}
|
2005-09-09 08:11:37 +02:00
|
|
|
/* rewrite it to an IP if we learned one. */
|
2007-02-05 20:15:13 +01:00
|
|
|
if (addressmap_rewrite(conn->socks_request->address,
|
2007-07-10 19:14:51 +02:00
|
|
|
sizeof(conn->socks_request->address),
|
|
|
|
NULL)) {
|
2007-02-05 20:15:13 +01:00
|
|
|
control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
|
|
|
|
}
|
2006-07-27 07:03:57 +02:00
|
|
|
if (conn->_base.chosen_exit_optional) {
|
|
|
|
/* stop wanting a specific exit */
|
|
|
|
conn->_base.chosen_exit_optional = 0;
|
2006-10-01 08:41:13 +02:00
|
|
|
tor_free(conn->chosen_exit_name); /* clears it */
|
2006-07-18 02:59:46 +02:00
|
|
|
}
|
2006-10-20 19:54:43 +02:00
|
|
|
if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
|
2005-04-04 05:30:49 +02:00
|
|
|
return 0;
|
|
|
|
/* else, conn will get closed below */
|
|
|
|
break;
|
2006-07-18 02:59:46 +02:00
|
|
|
case END_STREAM_REASON_CONNECTREFUSED:
|
2006-07-27 07:03:57 +02:00
|
|
|
if (!conn->_base.chosen_exit_optional)
|
2006-07-18 02:59:46 +02:00
|
|
|
break; /* break means it'll close, below */
|
|
|
|
/* Else fall through: expire this circuit, clear the
|
|
|
|
* chosen_exit_name field, and try again. */
|
2005-04-04 05:30:49 +02:00
|
|
|
case END_STREAM_REASON_RESOLVEFAILED:
|
2006-07-18 02:59:46 +02:00
|
|
|
case END_STREAM_REASON_TIMEOUT:
|
2005-08-13 02:31:41 +02:00
|
|
|
case END_STREAM_REASON_MISC:
|
2005-04-04 05:30:49 +02:00
|
|
|
if (client_dns_incr_failures(conn->socks_request->address)
|
|
|
|
< MAX_RESOLVE_FAILURES) {
|
|
|
|
/* We haven't retried too many times; reattach the connection. */
|
2005-10-24 21:39:45 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_APP,circ);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->_base.timestamp_dirty);
|
|
|
|
circ->_base.timestamp_dirty -= get_options()->MaxCircuitDirtiness;
|
2005-04-04 05:30:49 +02:00
|
|
|
|
2006-07-27 07:03:57 +02:00
|
|
|
if (conn->_base.chosen_exit_optional) {
|
|
|
|
/* stop wanting a specific exit */
|
|
|
|
conn->_base.chosen_exit_optional = 0;
|
2006-10-01 08:41:13 +02:00
|
|
|
tor_free(conn->chosen_exit_name); /* clears it */
|
2006-07-18 02:59:46 +02:00
|
|
|
}
|
2006-10-20 19:54:43 +02:00
|
|
|
if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
|
2005-04-04 05:30:49 +02:00
|
|
|
return 0;
|
|
|
|
/* else, conn will get closed below */
|
|
|
|
} else {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_notice(LD_APP,
|
|
|
|
"Have tried resolving or connecting to address '%s' "
|
|
|
|
"at %d different places. Giving up.",
|
|
|
|
safe_str(conn->socks_request->address),
|
|
|
|
MAX_RESOLVE_FAILURES);
|
2005-08-15 05:35:15 +02:00
|
|
|
/* clear the failures, so it will have a full try next time */
|
|
|
|
client_dns_clear_failures(conn->socks_request->address);
|
2005-04-04 05:30:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case END_STREAM_REASON_HIBERNATING:
|
|
|
|
case END_STREAM_REASON_RESOURCELIMIT:
|
2005-06-29 23:46:55 +02:00
|
|
|
if (exitrouter) {
|
2008-01-02 05:43:44 +01:00
|
|
|
policies_set_router_exitpolicy_to_reject_all(exitrouter);
|
2005-06-29 23:46:55 +02:00
|
|
|
}
|
2006-07-27 07:03:57 +02:00
|
|
|
if (conn->_base.chosen_exit_optional) {
|
|
|
|
/* stop wanting a specific exit */
|
|
|
|
conn->_base.chosen_exit_optional = 0;
|
2006-10-01 08:41:13 +02:00
|
|
|
tor_free(conn->chosen_exit_name); /* clears it */
|
2006-07-18 02:59:46 +02:00
|
|
|
}
|
2006-10-20 19:54:43 +02:00
|
|
|
if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
2005-04-04 05:30:49 +02:00
|
|
|
/* else, will close below */
|
|
|
|
break;
|
|
|
|
} /* end switch */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
|
2005-04-04 05:30:49 +02:00
|
|
|
}
|
2005-03-14 04:12:59 +01:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,
|
|
|
|
"Edge got end (%s) before we're connected. Marking for close.",
|
2005-12-10 10:36:26 +01:00
|
|
|
connection_edge_end_reason_str(rh->length > 0 ? reason : -1));
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.type == CONN_TYPE_AP) {
|
2005-10-24 21:39:45 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_APP,circ);
|
2006-10-20 19:54:48 +02:00
|
|
|
connection_mark_unattached_ap(conn, control_reason);
|
2005-04-04 05:30:49 +02:00
|
|
|
} else {
|
2006-07-26 21:07:37 +02:00
|
|
|
/* we just got an 'end', don't need to send one */
|
|
|
|
conn->_base.edge_has_sent_end = 1;
|
2006-10-20 19:54:48 +02:00
|
|
|
conn->end_reason = control_reason;
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2005-04-04 05:30:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-26 06:36:02 +01:00
|
|
|
/** Helper: change the socks_request->address field on conn to the
|
|
|
|
* dotted-quad representation of <b>new_addr</b> (given in host order),
|
|
|
|
* and send an appropriate REMAP event. */
|
2007-02-23 21:13:02 +01:00
|
|
|
static void
|
|
|
|
remap_event_helper(edge_connection_t *conn, uint32_t new_addr)
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
in.s_addr = htonl(new_addr);
|
|
|
|
tor_inet_ntoa(&in, conn->socks_request->address,
|
|
|
|
sizeof(conn->socks_request->address));
|
|
|
|
control_event_stream_status(conn, STREAM_EVENT_REMAP,
|
|
|
|
REMAP_STREAM_SOURCE_EXIT);
|
|
|
|
}
|
|
|
|
|
2005-04-04 05:30:49 +02:00
|
|
|
/** An incoming relay cell has arrived from circuit <b>circ</b> to
|
|
|
|
* stream <b>conn</b>.
|
|
|
|
*
|
|
|
|
* The arguments here are the same as in
|
|
|
|
* connection_edge_process_relay_cell() below; this function is called
|
|
|
|
* from there when <b>conn</b> is defined and not in an open state.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
connection_edge_process_relay_cell_not_open(
|
|
|
|
relay_header_t *rh, cell_t *cell, circuit_t *circ,
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn, crypt_path_t *layer_hint)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
if (rh->command == RELAY_COMMAND_END) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
return connection_edge_process_end_not_open(rh, cell,
|
|
|
|
TO_ORIGIN_CIRCUIT(circ), conn,
|
|
|
|
layer_hint);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-26 21:07:37 +02:00
|
|
|
if (conn->_base.type == CONN_TYPE_AP &&
|
|
|
|
rh->command == RELAY_COMMAND_CONNECTED) {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.state != AP_CONN_STATE_CONNECT_WAIT) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_APP,
|
|
|
|
"Got 'connected' while not in state connect_wait. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.state = AP_CONN_STATE_OPEN;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,"'connected' received after %d seconds.",
|
2006-07-26 21:07:26 +02:00
|
|
|
(int)(time(NULL) - conn->_base.timestamp_lastread));
|
2004-05-13 09:24:49 +02:00
|
|
|
if (rh->length >= 4) {
|
2005-04-04 05:30:49 +02:00
|
|
|
uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
|
2005-09-02 20:53:31 +02:00
|
|
|
int ttl;
|
2007-08-27 17:33:58 +02:00
|
|
|
if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
|
|
|
|
is_internal_IP(addr, 0))) {
|
|
|
|
char buf[INET_NTOA_BUF_LEN];
|
|
|
|
struct in_addr a;
|
|
|
|
a.s_addr = htonl(addr);
|
|
|
|
tor_inet_ntoa(&a, buf, sizeof(buf));
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,
|
2007-08-27 17:33:58 +02:00
|
|
|
"...but it claims the IP address was %s. Closing.", buf);
|
2007-03-24 16:57:51 +01:00
|
|
|
connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
|
2005-04-03 00:11:24 +02:00
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
|
2004-08-15 22:05:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-09-02 20:53:31 +02:00
|
|
|
if (rh->length >= 8)
|
2005-09-03 04:12:53 +02:00
|
|
|
ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+4));
|
2005-09-02 20:53:31 +02:00
|
|
|
else
|
|
|
|
ttl = -1;
|
2005-02-24 12:44:08 +01:00
|
|
|
client_dns_set_addressmap(conn->socks_request->address, addr,
|
2005-09-02 20:53:31 +02:00
|
|
|
conn->chosen_exit_name, ttl);
|
2007-02-23 21:13:02 +01:00
|
|
|
|
|
|
|
remap_event_helper(conn, addr);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
|
2006-08-10 11:01:37 +02:00
|
|
|
/* don't send a socks reply to transparent conns */
|
|
|
|
if (!conn->socks_request->has_finished)
|
2006-10-20 19:54:43 +02:00
|
|
|
connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* handle anything that might have queued */
|
2004-11-21 08:43:12 +01:00
|
|
|
if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
|
2005-03-02 04:13:05 +01:00
|
|
|
/* (We already sent an end cell if possible) */
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-07-26 21:07:37 +02:00
|
|
|
if (conn->_base.type == CONN_TYPE_AP &&
|
|
|
|
rh->command == RELAY_COMMAND_RESOLVED) {
|
2005-09-02 20:53:31 +02:00
|
|
|
int ttl;
|
|
|
|
int answer_len;
|
2007-02-23 21:13:02 +01:00
|
|
|
uint8_t answer_type;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.state != AP_CONN_STATE_RESOLVE_WAIT) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
|
|
|
|
"not in state resolve_wait. Dropping.");
|
2004-06-17 20:13:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-12-13 01:28:56 +01:00
|
|
|
tor_assert(SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command));
|
2005-09-02 20:53:31 +02:00
|
|
|
answer_len = cell->payload[RELAY_HEADER_SIZE+1];
|
|
|
|
if (rh->length < 2 || answer_len+2>rh->length) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Dropping malformed 'resolved' cell");
|
2005-04-03 00:11:24 +02:00
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
|
2004-06-17 20:13:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-08-27 17:33:58 +02:00
|
|
|
answer_type = cell->payload[RELAY_HEADER_SIZE];
|
2005-09-02 20:53:31 +02:00
|
|
|
if (rh->length >= answer_len+6)
|
2005-12-14 21:40:40 +01:00
|
|
|
ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+
|
|
|
|
2+answer_len));
|
2005-09-02 20:53:31 +02:00
|
|
|
else
|
|
|
|
ttl = -1;
|
2007-08-27 17:33:58 +02:00
|
|
|
if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
|
|
|
|
uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
|
|
|
|
if (get_options()->ClientDNSRejectInternalAddresses &&
|
|
|
|
is_internal_IP(addr, 0)) {
|
|
|
|
char buf[INET_NTOA_BUF_LEN];
|
|
|
|
struct in_addr a;
|
|
|
|
a.s_addr = htonl(addr);
|
|
|
|
tor_inet_ntoa(&a, buf, sizeof(buf));
|
|
|
|
log_info(LD_APP,"Got a resolve with answer %s. Rejecting.", buf);
|
|
|
|
connection_ap_handshake_socks_resolved(conn,
|
|
|
|
RESOLVED_TYPE_ERROR_TRANSIENT,
|
|
|
|
0, NULL, 0, TIME_MAX);
|
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-06-17 20:13:09 +02:00
|
|
|
connection_ap_handshake_socks_resolved(conn,
|
2007-02-23 21:13:02 +01:00
|
|
|
answer_type,
|
2004-07-23 01:21:12 +02:00
|
|
|
cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
|
2005-09-03 04:14:31 +02:00
|
|
|
cell->payload+RELAY_HEADER_SIZE+2, /*answer*/
|
2007-07-10 19:14:51 +02:00
|
|
|
ttl,
|
|
|
|
-1);
|
2007-02-23 21:13:02 +01:00
|
|
|
if (answer_type == RESOLVED_TYPE_IPV4) {
|
|
|
|
uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
|
|
|
|
remap_event_helper(conn, addr);
|
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_mark_unattached_ap(conn,
|
2007-02-07 07:54:27 +01:00
|
|
|
END_STREAM_REASON_DONE |
|
|
|
|
END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
|
2004-06-17 20:13:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-11-15 21:40:32 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-12-10 10:36:26 +01:00
|
|
|
"Got an unexpected relay command %d, in state %d (%s). Dropping.",
|
2006-07-26 21:07:26 +02:00
|
|
|
rh->command, conn->_base.state,
|
|
|
|
conn_state_to_string(conn->_base.type, conn->_base.state));
|
2005-11-17 04:40:20 +01:00
|
|
|
return 0; /* for forward compatibility, don't kill the circuit */
|
2007-03-24 16:57:51 +01:00
|
|
|
// connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
|
2005-11-17 04:40:20 +01:00
|
|
|
// connection_mark_for_close(conn);
|
|
|
|
// return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** An incoming relay cell has arrived on circuit <b>circ</b>. If
|
|
|
|
* <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
|
|
|
|
* destined for <b>conn</b>.
|
|
|
|
*
|
|
|
|
* If <b>layer_hint</b> is defined, then we're the origin of the
|
|
|
|
* circuit, and it specifies the hop that packaged <b>cell</b>.
|
|
|
|
*
|
2006-01-05 22:23:03 +01:00
|
|
|
* Return -reason if you want to warn and tear down the circuit, else 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn,
|
2004-05-13 09:24:49 +02:00
|
|
|
crypt_path_t *layer_hint)
|
|
|
|
{
|
|
|
|
static int num_seen=0;
|
|
|
|
relay_header_t rh;
|
2005-10-25 09:04:36 +02:00
|
|
|
unsigned domain = layer_hint?LD_APP:LD_EXIT;
|
2006-01-05 22:23:03 +01:00
|
|
|
int reason;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(cell);
|
|
|
|
tor_assert(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
// log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
|
|
|
|
num_seen++;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain, "Now seen %d relay cells here.", num_seen);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-05-17 22:00:24 +02:00
|
|
|
if (rh.length > RELAY_PAYLOAD_SIZE) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Relay cell length field too long. Closing circuit.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return - END_CIRC_REASON_TORPROTOCOL;
|
2005-05-17 22:00:24 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/* either conn is NULL, in which case we've got a control cell, or else
|
|
|
|
* conn points to the recognized stream. */
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn && !connection_state_is_open(TO_CONN(conn)))
|
2004-05-13 09:24:49 +02:00
|
|
|
return connection_edge_process_relay_cell_not_open(
|
|
|
|
&rh, cell, circ, conn, layer_hint);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (rh.command) {
|
2004-05-13 09:24:49 +02:00
|
|
|
case RELAY_COMMAND_DROP:
|
2006-09-22 02:24:27 +02:00
|
|
|
// log_info(domain,"Got a relay-level padding cell. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_BEGIN:
|
2006-09-29 05:50:11 +02:00
|
|
|
case RELAY_COMMAND_BEGIN_DIR:
|
2004-05-13 09:24:49 +02:00
|
|
|
if (layer_hint &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_APP,
|
|
|
|
"Relay begin request unsupported at AP. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, domain,
|
|
|
|
"Begin cell for known stream. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-20 17:26:02 +02:00
|
|
|
return connection_exit_begin_conn(cell, circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
case RELAY_COMMAND_DATA:
|
|
|
|
++stats_n_data_cells_received;
|
2004-11-28 12:39:53 +01:00
|
|
|
if (( layer_hint && --layer_hint->deliver_window < 0) ||
|
|
|
|
(!layer_hint && --circ->deliver_window < 0)) {
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-10-17 03:29:28 +02:00
|
|
|
"(relay data) circ deliver_window below 0. Killing.");
|
2007-03-24 16:57:51 +01:00
|
|
|
connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"circ deliver_window now %d.", layer_hint ?
|
|
|
|
layer_hint->deliver_window : circ->deliver_window);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
circuit_consider_sending_sendme(circ, layer_hint);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!conn) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,"data cell dropped, unknown stream.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-10-17 03:29:28 +02:00
|
|
|
"(relay data) conn deliver_window below 0. Killing.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stats_n_data_bytes_received += rh.length;
|
|
|
|
connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
|
2006-07-26 21:07:26 +02:00
|
|
|
rh.length, TO_CONN(conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
connection_edge_consider_sending_sendme(conn);
|
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_END:
|
2006-10-20 19:54:48 +02:00
|
|
|
reason = rh.length > 0 ?
|
|
|
|
*(uint8_t *)(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!conn) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,"end cell (%s) dropped, unknown stream.",
|
2006-10-20 19:54:48 +02:00
|
|
|
connection_edge_end_reason_str(reason));
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* XXX add to this log_fn the exit node's nickname? */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,"%d: end cell (%s) for stream %d. Removing stream.",
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.s,
|
2006-10-20 19:54:48 +02:00
|
|
|
connection_edge_end_reason_str(reason),
|
2006-02-13 11:33:00 +01:00
|
|
|
conn->stream_id);
|
2005-04-04 05:30:49 +02:00
|
|
|
if (conn->socks_request && !conn->socks_request->has_finished)
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,
|
2007-03-04 21:11:46 +01:00
|
|
|
"open stream hasn't sent socks answer yet? Closing.");
|
2004-05-13 09:24:49 +02:00
|
|
|
/* We just *got* an end; no reason to send one. */
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.edge_has_sent_end = 1;
|
2006-10-20 19:54:48 +02:00
|
|
|
if (!conn->end_reason)
|
|
|
|
conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!conn->_base.marked_for_close) {
|
2004-08-07 04:19:49 +02:00
|
|
|
/* only mark it if not already marked. it's possible to
|
|
|
|
* get the 'end' right around when the client hangs up on us. */
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
|
|
|
conn->_base.hold_open_until_flushed = 1;
|
2004-08-07 04:19:49 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_EXTEND:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, domain,
|
|
|
|
"'extend' cell received for non-zero stream. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return circuit_extend(cell, circ);
|
|
|
|
case RELAY_COMMAND_EXTENDED:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!layer_hint) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"'extended' unsupported at non-origin. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"Got an extended cell! Yay.");
|
2006-07-23 09:37:35 +02:00
|
|
|
if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
|
|
|
|
CELL_CREATED,
|
2006-01-05 22:23:03 +01:00
|
|
|
cell->payload+RELAY_HEADER_SIZE)) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(domain,"circuit_finish_handshake failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return reason;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,"circuit_send_next_onion_skin() failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return reason;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_TRUNCATE:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (layer_hint) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_APP,
|
|
|
|
"'truncate' unsupported at origin. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn) {
|
2007-05-17 00:15:14 +02:00
|
|
|
uint8_t trunc_reason = *(uint8_t*)(cell->payload + RELAY_HEADER_SIZE);
|
|
|
|
connection_or_send_destroy(circ->n_circ_id, circ->n_conn,
|
|
|
|
trunc_reason);
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_n_circid_orconn(circ, 0, NULL);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_EXIT, "Processed 'truncate', replying.");
|
2006-01-05 22:23:03 +01:00
|
|
|
{
|
|
|
|
char payload[1];
|
|
|
|
payload[0] = (char)END_CIRC_REASON_REQUESTED;
|
2007-03-24 16:58:11 +01:00
|
|
|
relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
|
|
|
|
payload, sizeof(payload), NULL);
|
2006-01-05 22:23:03 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_TRUNCATED:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!layer_hint) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
|
|
|
|
"'truncated' unsupported at non-origin. Dropping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint);
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_CONNECTED:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn) {
|
2005-10-25 09:04:36 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-10-17 03:29:28 +02:00
|
|
|
"'connected' unsupported while open. Closing circ.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,
|
|
|
|
"'connected' received, no conn attached anymore. Ignoring.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_SENDME:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!conn) {
|
|
|
|
if (layer_hint) {
|
2004-05-13 09:24:49 +02:00
|
|
|
layer_hint->package_window += CIRCWINDOW_INCREMENT;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
|
|
|
|
layer_hint->package_window);
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_resume_edge_reading(circ, layer_hint);
|
|
|
|
} else {
|
|
|
|
circ->package_window += CIRCWINDOW_INCREMENT;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_APP,
|
|
|
|
"circ-level sendme at non-origin, packagewindow %d.",
|
|
|
|
circ->package_window);
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_resume_edge_reading(circ, layer_hint);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
conn->package_window += STREAMWINDOW_INCREMENT;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"stream-level sendme, packagewindow now %d.",
|
|
|
|
conn->package_window);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_start_reading(TO_CONN(conn));
|
2005-03-02 04:13:05 +01:00
|
|
|
/* handle whatever might still be on the inbuf */
|
|
|
|
if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
|
|
|
|
/* (We already sent an end cell if possible) */
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2005-03-02 04:13:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
2004-06-17 20:13:09 +02:00
|
|
|
case RELAY_COMMAND_RESOLVE:
|
|
|
|
if (layer_hint) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_APP,
|
|
|
|
"resolve request unsupported at AP; dropping.");
|
2004-07-23 01:21:12 +02:00
|
|
|
return 0;
|
2004-06-17 20:13:09 +02:00
|
|
|
} else if (conn) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, domain,
|
|
|
|
"resolve request for known stream; dropping.");
|
2004-07-23 01:21:12 +02:00
|
|
|
return 0;
|
2004-06-17 20:13:09 +02:00
|
|
|
} else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, domain,
|
|
|
|
"resolve request on circ with purpose %d; dropping",
|
|
|
|
circ->purpose);
|
2004-07-23 01:21:12 +02:00
|
|
|
return 0;
|
2004-06-17 20:13:09 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
|
2004-06-17 20:13:09 +02:00
|
|
|
return 0;
|
|
|
|
case RELAY_COMMAND_RESOLVED:
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn) {
|
2006-10-15 09:42:51 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, domain,
|
|
|
|
"'resolved' unsupported while open. Closing circ.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-06-17 20:13:09 +02:00
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,
|
|
|
|
"'resolved' received, no conn attached anymore. Ignoring.");
|
2004-06-17 20:13:09 +02:00
|
|
|
return 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
case RELAY_COMMAND_ESTABLISH_INTRO:
|
|
|
|
case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
|
|
|
|
case RELAY_COMMAND_INTRODUCE1:
|
|
|
|
case RELAY_COMMAND_INTRODUCE2:
|
|
|
|
case RELAY_COMMAND_INTRODUCE_ACK:
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS1:
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS2:
|
|
|
|
case RELAY_COMMAND_INTRO_ESTABLISHED:
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
|
|
|
|
rend_process_relay_cell(circ, rh.command, rh.length,
|
|
|
|
cell->payload+RELAY_HEADER_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-11-17 04:40:20 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-12-14 21:40:40 +01:00
|
|
|
"Received unknown relay command %d. Perhaps the other side is using "
|
|
|
|
"a newer version of Tor? Dropping.",
|
2005-11-17 04:40:20 +01:00
|
|
|
rh.command);
|
|
|
|
return 0; /* for forward compatibility, don't kill the circuit */
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t stats_n_data_cells_packaged = 0;
|
|
|
|
uint64_t stats_n_data_bytes_packaged = 0;
|
|
|
|
uint64_t stats_n_data_cells_received = 0;
|
|
|
|
uint64_t stats_n_data_bytes_received = 0;
|
|
|
|
|
|
|
|
/** While conn->inbuf has an entire relay payload of bytes on it,
|
|
|
|
* and the appropriate package windows aren't empty, grab a cell
|
|
|
|
* and send it down the circuit.
|
|
|
|
*
|
2005-03-02 04:13:05 +01:00
|
|
|
* Return -1 (and send a RELAY_END cell if necessary) if conn should
|
|
|
|
* be marked for close, else return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-14 04:47:09 +02:00
|
|
|
size_t amount_to_process, length;
|
2004-05-13 09:24:49 +02:00
|
|
|
char payload[CELL_PAYLOAD_SIZE];
|
|
|
|
circuit_t *circ;
|
2005-10-25 09:04:36 +02:00
|
|
|
unsigned domain = conn->cpath_layer ? LD_APP : LD_EXIT;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(conn);
|
2006-07-26 21:07:26 +02:00
|
|
|
|
|
|
|
if (conn->_base.marked_for_close) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,
|
2007-03-04 21:11:46 +01:00
|
|
|
"called on conn that's already marked for close at %s:%d.",
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.marked_for_close_file, conn->_base.marked_for_close);
|
2005-03-23 03:52:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
repeat_connection_edge_package_raw_inbuf:
|
|
|
|
|
2005-04-06 08:43:21 +02:00
|
|
|
circ = circuit_get_by_edge_conn(conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,"conn has no circuit! Closing.");
|
2006-10-20 19:54:48 +02:00
|
|
|
conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->package_window <= 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(domain,"called with package_window %d. Skipping.",
|
|
|
|
conn->package_window);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_stop_reading(TO_CONN(conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
amount_to_process = buf_datalen(conn->_base.inbuf);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-21 08:43:12 +01:00
|
|
|
if (!amount_to_process)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-11-21 08:43:12 +01:00
|
|
|
if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (amount_to_process > RELAY_PAYLOAD_SIZE) {
|
2004-05-13 09:24:49 +02:00
|
|
|
length = RELAY_PAYLOAD_SIZE;
|
|
|
|
} else {
|
|
|
|
length = amount_to_process;
|
|
|
|
}
|
|
|
|
stats_n_data_bytes_packaged += length;
|
|
|
|
stats_n_data_cells_packaged += 1;
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_fetch_from_buf(payload, length, TO_CONN(conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
log_debug(domain,"(%d) Packaging %d bytes (%d waiting).", conn->_base.s,
|
|
|
|
(int)length, (int)buf_datalen(conn->_base.inbuf));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-03-24 16:58:11 +01:00
|
|
|
if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
|
2007-03-24 16:57:51 +01:00
|
|
|
payload, length) < 0 )
|
2005-03-02 04:13:05 +01:00
|
|
|
/* circuit got marked for close, don't continue, don't need to mark conn */
|
|
|
|
return 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!conn->cpath_layer) { /* non-rendezvous exit */
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(circ->package_window > 0);
|
|
|
|
circ->package_window--;
|
|
|
|
} else { /* we're an AP, or an exit on a rendezvous circ */
|
|
|
|
tor_assert(conn->cpath_layer->package_window > 0);
|
|
|
|
conn->cpath_layer->package_window--;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (--conn->package_window <= 0) { /* is it 0 after decrement? */
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_stop_reading(TO_CONN(conn));
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"conn->package_window reached 0.");
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
|
|
|
|
return 0; /* don't process the inbuf any more */
|
|
|
|
}
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"conn->package_window is now %d",conn->package_window);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* handle more if there's more, or return 0 if there isn't */
|
|
|
|
goto repeat_connection_edge_package_raw_inbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when we've just received a relay data cell, or when
|
|
|
|
* we've just finished flushing all bytes to stream <b>conn</b>.
|
|
|
|
*
|
|
|
|
* If conn->outbuf is not too full, and our deliver window is
|
|
|
|
* low, send back a suitable number of stream-level sendme cells.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_edge_consider_sending_sendme(edge_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (connection_outbuf_too_full(TO_CONN(conn)))
|
2004-05-13 09:24:49 +02:00
|
|
|
return;
|
|
|
|
|
2005-04-06 08:43:21 +02:00
|
|
|
circ = circuit_get_by_edge_conn(conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* this can legitimately happen if the destroy has already
|
|
|
|
* arrived and torn down the circuit */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_APP,"No circuit associated with conn. Skipping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(conn->cpath_layer?LD_APP:LD_EXIT,
|
|
|
|
"Outbuf %d, Queueing stream sendme.",
|
2006-07-26 21:07:26 +02:00
|
|
|
(int)conn->_base.outbuf_flushlen);
|
2004-05-13 09:24:49 +02:00
|
|
|
conn->deliver_window += STREAMWINDOW_INCREMENT;
|
2007-03-24 16:58:11 +01:00
|
|
|
if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
|
2007-03-24 16:57:51 +01:00
|
|
|
NULL, 0) < 0) {
|
2008-02-06 13:46:46 +01:00
|
|
|
log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return; /* the circuit's closed, don't continue */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The circuit <b>circ</b> has received a circuit-level sendme
|
|
|
|
* (on hop <b>layer_hint</b>, if we're the OP). Go through all the
|
|
|
|
* attached streams and let them resume reading and packaging, if
|
|
|
|
* their stream windows allow it.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
|
|
|
|
{
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
|
|
|
|
circ, layer_hint);
|
|
|
|
else
|
|
|
|
circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
|
|
|
|
circ, layer_hint);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A helper function for circuit_resume_edge_reading() above.
|
|
|
|
* The arguments are the same, except that <b>conn</b> is the head
|
|
|
|
* of a linked list of edge streams that should each be considered.
|
|
|
|
*/
|
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_resume_edge_reading_helper(edge_connection_t *conn,
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ,
|
2005-06-11 20:52:12 +02:00
|
|
|
crypt_path_t *layer_hint)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
for ( ; conn; conn=conn->next_stream) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (conn->_base.marked_for_close)
|
2005-03-24 22:57:45 +01:00
|
|
|
continue;
|
2004-11-28 10:05:49 +01:00
|
|
|
if ((!layer_hint && conn->package_window > 0) ||
|
2005-12-14 21:40:40 +01:00
|
|
|
(layer_hint && conn->package_window > 0 &&
|
|
|
|
conn->cpath_layer == layer_hint)) {
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_start_reading(TO_CONN(conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
/* handle whatever might still be on the inbuf */
|
2005-03-02 04:13:05 +01:00
|
|
|
if (connection_edge_package_raw_inbuf(conn, 1)<0) {
|
|
|
|
/* (We already sent an end cell if possible) */
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2005-03-02 04:13:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* If the circuit won't accept any more data, return without looking
|
|
|
|
* at any more of the streams. Any connections that should be stopped
|
|
|
|
* have already been stopped by connection_edge_package_raw_inbuf. */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circuit_consider_stop_edge_reading(circ, layer_hint))
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if the package window for <b>circ</b> is empty (at
|
|
|
|
* hop <b>layer_hint</b> if it's defined).
|
|
|
|
*
|
2004-11-21 12:20:28 +01:00
|
|
|
* If yes, tell edge streams to stop reading and return 1.
|
2004-05-13 09:24:49 +02:00
|
|
|
* Else return 0.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
|
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn = NULL;
|
2005-10-25 17:30:02 +02:00
|
|
|
unsigned domain = layer_hint ? LD_APP : LD_EXIT;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-21 12:20:28 +01:00
|
|
|
if (!layer_hint) {
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"considering circ->package_window %d",
|
|
|
|
circ->package_window);
|
2004-11-21 12:20:28 +01:00
|
|
|
if (circ->package_window <= 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"yes, not-at-origin. stopped.");
|
2006-07-23 09:37:35 +02:00
|
|
|
for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_stop_reading(TO_CONN(conn));
|
2004-11-21 12:20:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* else, layer hint is defined, use it */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"considering layer_hint->package_window %d",
|
|
|
|
layer_hint->package_window);
|
2004-11-21 12:20:28 +01:00
|
|
|
if (layer_hint->package_window <= 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(domain,"yes, at-origin. stopped.");
|
2006-07-23 09:37:35 +02:00
|
|
|
for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
|
|
|
|
conn=conn->next_stream)
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->cpath_layer == layer_hint)
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_stop_reading(TO_CONN(conn));
|
2004-11-21 12:20:28 +01:00
|
|
|
return 1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if the deliver_window for circuit <b>circ</b> (at hop
|
|
|
|
* <b>layer_hint</b> if it's defined) is low enough that we should
|
|
|
|
* send a circuit-level sendme back down the circuit. If so, send
|
|
|
|
* enough sendmes that the window would be overfull if we sent any
|
|
|
|
* more.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
|
|
|
|
{
|
|
|
|
// log_fn(LOG_INFO,"Considering: layer_hint is %s",
|
|
|
|
// layer_hint ? "defined" : "null");
|
2004-11-28 10:05:49 +01:00
|
|
|
while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
|
2004-11-28 12:39:53 +01:00
|
|
|
CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_CIRC,"Queueing circuit sendme.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (layer_hint)
|
2004-05-13 09:24:49 +02:00
|
|
|
layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
|
|
|
|
else
|
|
|
|
circ->deliver_window += CIRCWINDOW_INCREMENT;
|
2007-03-24 16:57:51 +01:00
|
|
|
if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
|
2004-11-28 12:39:53 +01:00
|
|
|
NULL, 0, layer_hint) < 0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"connection_edge_send_command failed. Circuit's closed.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return; /* the circuit's closed, don't continue */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Stop reading on edge connections when we have this many cells
|
|
|
|
* waiting on the appropriate queue. */
|
2007-03-26 16:08:18 +02:00
|
|
|
#define CELL_QUEUE_HIGHWATER_SIZE 256
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Start reading from edge connections again when we get down to this many
|
|
|
|
* cells. */
|
2007-03-26 16:08:18 +02:00
|
|
|
#define CELL_QUEUE_LOWWATER_SIZE 64
|
|
|
|
|
2007-03-29 04:41:36 +02:00
|
|
|
#ifdef ACTIVE_CIRCUITS_PARANOIA
|
|
|
|
#define assert_active_circuits_ok_paranoid(conn) \
|
|
|
|
assert_active_circuits_ok(conn)
|
|
|
|
#else
|
|
|
|
#define assert_active_circuits_ok_paranoid(conn)
|
|
|
|
#endif
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** The total number of cells we have allocated from the memory pool. */
|
2007-04-19 21:52:30 +02:00
|
|
|
static int total_cells_allocated = 0;
|
|
|
|
|
2007-05-25 21:41:31 +02:00
|
|
|
#ifdef ENABLE_CELL_POOL /* Defined in ./configure. True by default. */
|
2008-02-15 20:20:53 +01:00
|
|
|
/* XXX021 make cell pools the only option once we know they work and improve
|
2008-02-05 22:39:46 +01:00
|
|
|
* matters? -RD */
|
2007-04-11 02:30:34 +02:00
|
|
|
static mp_pool_t *cell_pool = NULL;
|
|
|
|
/** Allocate structures to hold cells. */
|
2007-04-11 02:30:29 +02:00
|
|
|
void
|
|
|
|
init_cell_pool(void)
|
|
|
|
{
|
|
|
|
tor_assert(!cell_pool);
|
2008-02-12 21:20:52 +01:00
|
|
|
cell_pool = mp_pool_new(sizeof(packed_cell_t), 128*1024);
|
2007-04-11 02:30:29 +02:00
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Free all storage used to hold cells. */
|
2007-04-11 02:30:29 +02:00
|
|
|
void
|
|
|
|
free_cell_pool(void)
|
|
|
|
{
|
2007-04-30 03:09:03 +02:00
|
|
|
/* Maybe we haven't called init_cell_pool yet; need to check for it. */
|
|
|
|
if (cell_pool) {
|
|
|
|
mp_pool_destroy(cell_pool);
|
|
|
|
cell_pool = NULL;
|
|
|
|
}
|
2007-04-11 02:30:29 +02:00
|
|
|
}
|
|
|
|
|
2007-04-11 15:18:25 +02:00
|
|
|
/** Free excess storage in cell pool. */
|
|
|
|
void
|
|
|
|
clean_cell_pool(void)
|
|
|
|
{
|
|
|
|
tor_assert(cell_pool);
|
2008-02-12 21:20:52 +01:00
|
|
|
mp_pool_clean(cell_pool, 0, 1);
|
2007-04-11 15:18:25 +02:00
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Release storage held by <b>cell</b>. */
|
2007-04-11 02:30:29 +02:00
|
|
|
static INLINE void
|
|
|
|
packed_cell_free(packed_cell_t *cell)
|
|
|
|
{
|
2007-04-19 21:52:30 +02:00
|
|
|
--total_cells_allocated;
|
2007-04-11 02:30:29 +02:00
|
|
|
mp_pool_release(cell);
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Allocate and return a new packed_cell_t. */
|
|
|
|
static INLINE packed_cell_t *
|
2007-04-11 02:30:29 +02:00
|
|
|
packed_cell_alloc(void)
|
|
|
|
{
|
2007-04-19 21:52:30 +02:00
|
|
|
++total_cells_allocated;
|
2007-04-11 02:30:29 +02:00
|
|
|
return mp_pool_get(cell_pool);
|
|
|
|
}
|
2007-04-19 20:47:04 +02:00
|
|
|
void
|
|
|
|
dump_cell_pool_usage(int severity)
|
|
|
|
{
|
2007-04-19 21:52:30 +02:00
|
|
|
circuit_t *c;
|
|
|
|
int n_circs = 0;
|
|
|
|
int n_cells = 0;
|
|
|
|
for (c = _circuit_get_global_list(); c; c = c->next) {
|
|
|
|
n_cells += c->n_conn_cells.n;
|
|
|
|
if (!CIRCUIT_IS_ORIGIN(c))
|
|
|
|
n_cells += TO_OR_CIRCUIT(c)->p_conn_cells.n;
|
|
|
|
++n_circs;
|
|
|
|
}
|
|
|
|
log(severity, LD_MM, "%d cells allocated on %d circuits. %d cells leaked.",
|
|
|
|
n_cells, n_circs, total_cells_allocated - n_cells);
|
2007-04-19 20:47:04 +02:00
|
|
|
mp_pool_log_status(cell_pool, severity);
|
|
|
|
}
|
2007-04-11 02:30:29 +02:00
|
|
|
#else
|
2007-04-11 02:30:34 +02:00
|
|
|
/* ENABLE_CELL_POOL isn't defined: here are some stubs to use tor_malloc()
|
|
|
|
* and tor_free() instead. */
|
2007-04-11 02:30:29 +02:00
|
|
|
void
|
|
|
|
init_cell_pool(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_cell_pool(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-04-11 15:18:25 +02:00
|
|
|
void
|
|
|
|
clean_cell_pool(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
static INLINE void
|
2007-04-10 01:15:46 +02:00
|
|
|
packed_cell_free(packed_cell_t *cell)
|
2007-03-26 16:07:59 +02:00
|
|
|
{
|
2007-04-19 21:52:30 +02:00
|
|
|
--total_cells_allocated;
|
2007-03-26 16:07:59 +02:00
|
|
|
tor_free(cell);
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:29 +02:00
|
|
|
static INLINE packed_cell_t *
|
|
|
|
packed_cell_alloc(void)
|
|
|
|
{
|
2007-04-19 21:52:30 +02:00
|
|
|
++total_cells_allocated;
|
2007-04-11 02:30:29 +02:00
|
|
|
return tor_malloc(sizeof(packed_cell_t));
|
|
|
|
}
|
2007-04-19 20:47:04 +02:00
|
|
|
void
|
|
|
|
dump_cell_pool_usage(int severity)
|
|
|
|
{
|
|
|
|
(void) severity;
|
|
|
|
}
|
2007-04-11 02:30:29 +02:00
|
|
|
#endif
|
|
|
|
|
2007-04-10 01:15:46 +02:00
|
|
|
/** Allocate a new copy of packed <b>cell</b>. */
|
|
|
|
static INLINE packed_cell_t *
|
|
|
|
packed_cell_copy(const cell_t *cell)
|
2007-03-26 16:07:59 +02:00
|
|
|
{
|
2007-04-11 02:30:29 +02:00
|
|
|
packed_cell_t *c = packed_cell_alloc();
|
2007-04-10 01:15:46 +02:00
|
|
|
cell_pack(c, cell);
|
2007-03-26 16:07:59 +02:00
|
|
|
c->next = NULL;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Append <b>cell</b> to the end of <b>queue</b>. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
2007-04-10 01:15:46 +02:00
|
|
|
cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
|
2007-03-26 16:07:59 +02:00
|
|
|
{
|
|
|
|
if (queue->tail) {
|
|
|
|
tor_assert(!queue->tail->next);
|
|
|
|
queue->tail->next = cell;
|
|
|
|
} else {
|
|
|
|
queue->head = cell;
|
|
|
|
}
|
|
|
|
queue->tail = cell;
|
|
|
|
cell->next = NULL;
|
|
|
|
++queue->n;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
2007-04-10 01:15:46 +02:00
|
|
|
cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell)
|
2007-03-26 16:07:59 +02:00
|
|
|
{
|
2007-04-10 01:15:46 +02:00
|
|
|
cell_queue_append(queue, packed_cell_copy(cell));
|
2007-03-26 16:07:59 +02:00
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Remove and free every cell in <b>queue</b>. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
|
|
|
cell_queue_clear(cell_queue_t *queue)
|
|
|
|
{
|
2007-04-10 01:15:46 +02:00
|
|
|
packed_cell_t *cell, *next;
|
2007-03-26 16:07:59 +02:00
|
|
|
cell = queue->head;
|
|
|
|
while (cell) {
|
|
|
|
next = cell->next;
|
2007-04-10 01:15:46 +02:00
|
|
|
packed_cell_free(cell);
|
2007-03-26 16:07:59 +02:00
|
|
|
cell = next;
|
|
|
|
}
|
|
|
|
queue->head = queue->tail = NULL;
|
|
|
|
queue->n = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Extract and return the cell at the head of <b>queue</b>; return NULL if
|
|
|
|
* <b>queue</b> is empty. */
|
2007-04-10 01:15:46 +02:00
|
|
|
static INLINE packed_cell_t *
|
2007-03-26 16:07:59 +02:00
|
|
|
cell_queue_pop(cell_queue_t *queue)
|
|
|
|
{
|
2007-04-10 01:15:46 +02:00
|
|
|
packed_cell_t *cell = queue->head;
|
2007-03-26 16:07:59 +02:00
|
|
|
if (!cell)
|
|
|
|
return NULL;
|
|
|
|
queue->head = cell->next;
|
|
|
|
if (cell == queue->tail) {
|
|
|
|
tor_assert(!queue->head);
|
|
|
|
queue->tail = NULL;
|
|
|
|
}
|
|
|
|
--queue->n;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Return a pointer to the "next_active_on_{n,p}_conn" pointer of <b>circ</b>,
|
|
|
|
* depending on whether <b>conn</b> matches n_conn or p_conn. */
|
2007-03-26 16:07:59 +02:00
|
|
|
static INLINE circuit_t **
|
|
|
|
next_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
|
|
|
|
{
|
2007-03-29 04:41:36 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(conn);
|
2007-03-26 16:07:59 +02:00
|
|
|
if (conn == circ->n_conn) {
|
|
|
|
return &circ->next_active_on_n_conn;
|
|
|
|
} else {
|
|
|
|
or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
|
|
|
|
tor_assert(conn == orcirc->p_conn);
|
|
|
|
return &orcirc->next_active_on_p_conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Return a pointer to the "prev_active_on_{n,p}_conn" pointer of <b>circ</b>,
|
|
|
|
* depending on whether <b>conn</b> matches n_conn or p_conn. */
|
2007-03-26 16:07:59 +02:00
|
|
|
static INLINE circuit_t **
|
|
|
|
prev_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
|
|
|
|
{
|
2007-03-29 04:41:36 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(conn);
|
2007-03-26 16:07:59 +02:00
|
|
|
if (conn == circ->n_conn) {
|
|
|
|
return &circ->prev_active_on_n_conn;
|
|
|
|
} else {
|
|
|
|
or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
|
|
|
|
tor_assert(conn == orcirc->p_conn);
|
|
|
|
return &orcirc->prev_active_on_p_conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Add <b>circ</b> to the list of circuits with pending cells on
|
2007-05-25 21:41:31 +02:00
|
|
|
* <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
|
|
|
make_circuit_active_on_conn(circuit_t *circ, or_connection_t *conn)
|
|
|
|
{
|
2007-04-10 18:24:50 +02:00
|
|
|
circuit_t **nextp = next_circ_on_conn_p(circ, conn);
|
|
|
|
circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
|
|
|
|
|
|
|
|
if (*nextp && *prevp) {
|
|
|
|
/* Already active. */
|
|
|
|
return;
|
|
|
|
}
|
2007-03-26 16:08:35 +02:00
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
if (! conn->active_circuits) {
|
|
|
|
conn->active_circuits = circ;
|
2007-04-10 18:24:50 +02:00
|
|
|
*prevp = *nextp = circ;
|
2007-03-26 16:07:59 +02:00
|
|
|
} else {
|
|
|
|
circuit_t *head = conn->active_circuits;
|
|
|
|
circuit_t *old_tail = *prev_circ_on_conn_p(head, conn);
|
|
|
|
*next_circ_on_conn_p(old_tail, conn) = circ;
|
2007-04-10 18:24:50 +02:00
|
|
|
*nextp = head;
|
2007-03-26 16:07:59 +02:00
|
|
|
*prev_circ_on_conn_p(head, conn) = circ;
|
2007-04-10 18:24:50 +02:00
|
|
|
*prevp = old_tail;
|
2007-03-26 16:07:59 +02:00
|
|
|
}
|
2007-03-29 04:41:36 +02:00
|
|
|
assert_active_circuits_ok_paranoid(conn);
|
2007-03-26 16:07:59 +02:00
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Remove <b>circ</b> to the list of circuits with pending cells on
|
2007-04-10 18:24:50 +02:00
|
|
|
* <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
|
|
|
make_circuit_inactive_on_conn(circuit_t *circ, or_connection_t *conn)
|
|
|
|
{
|
2007-04-10 18:24:50 +02:00
|
|
|
circuit_t **nextp = next_circ_on_conn_p(circ, conn);
|
|
|
|
circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
|
|
|
|
circuit_t *next = *nextp, *prev = *prevp;
|
|
|
|
|
|
|
|
if (!next && !prev) {
|
|
|
|
/* Already inactive. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-29 04:41:36 +02:00
|
|
|
tor_assert(next && prev);
|
2007-03-26 16:08:35 +02:00
|
|
|
tor_assert(*prev_circ_on_conn_p(next, conn) == circ);
|
|
|
|
tor_assert(*next_circ_on_conn_p(prev, conn) == circ);
|
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
if (next == circ) {
|
|
|
|
conn->active_circuits = NULL;
|
|
|
|
} else {
|
|
|
|
*prev_circ_on_conn_p(next, conn) = prev;
|
|
|
|
*next_circ_on_conn_p(prev, conn) = next;
|
|
|
|
if (conn->active_circuits == circ)
|
|
|
|
conn->active_circuits = next;
|
|
|
|
}
|
2007-04-10 18:24:50 +02:00
|
|
|
*prevp = *nextp = NULL;
|
2007-03-29 04:41:36 +02:00
|
|
|
assert_active_circuits_ok_paranoid(conn);
|
2007-03-26 16:07:59 +02:00
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Remove all circuits from the list of circuits with pending cells on
|
|
|
|
* <b>conn</b>. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
|
|
|
connection_or_unlink_all_active_circs(or_connection_t *orconn)
|
|
|
|
{
|
|
|
|
circuit_t *head = orconn->active_circuits;
|
|
|
|
circuit_t *cur = head;
|
|
|
|
if (! head)
|
|
|
|
return;
|
|
|
|
do {
|
|
|
|
circuit_t *next = *next_circ_on_conn_p(cur, orconn);
|
|
|
|
*prev_circ_on_conn_p(cur, orconn) = NULL;
|
|
|
|
*next_circ_on_conn_p(cur, orconn) = NULL;
|
|
|
|
cur = next;
|
|
|
|
} while (cur != head);
|
|
|
|
orconn->active_circuits = NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
|
|
|
|
* every edge connection that is using <b>circ</b> to write to <b>orconn</b>,
|
|
|
|
* and start or stop reading as appropriate. */
|
2007-03-26 16:08:18 +02:00
|
|
|
static void
|
2007-03-26 16:08:35 +02:00
|
|
|
set_streams_blocked_on_circ(circuit_t *circ, or_connection_t *orconn,
|
|
|
|
int block)
|
2007-03-26 16:08:18 +02:00
|
|
|
{
|
|
|
|
edge_connection_t *edge = NULL;
|
|
|
|
if (circ->n_conn == orconn) {
|
|
|
|
circ->streams_blocked_on_n_conn = block;
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
|
|
|
|
} else {
|
|
|
|
circ->streams_blocked_on_p_conn = block;
|
|
|
|
tor_assert(!CIRCUIT_IS_ORIGIN(circ));
|
|
|
|
edge = TO_OR_CIRCUIT(circ)->n_streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; edge; edge = edge->next_stream) {
|
|
|
|
connection_t *conn = TO_CONN(edge);
|
|
|
|
conn->edge_blocked_on_circ = block;
|
|
|
|
|
|
|
|
if (block) {
|
|
|
|
if (connection_is_reading(conn))
|
|
|
|
connection_stop_reading(conn);
|
|
|
|
} else {
|
|
|
|
/* Is this right? */
|
|
|
|
if (!connection_is_reading(conn))
|
|
|
|
connection_start_reading(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Pull as many cells as possible (but no more than <b>max</b>) from the
|
|
|
|
* queue of the first active circuit on <b>conn</b>, and write then to
|
|
|
|
* <b>conn</b>->outbuf. Return the number of cells written. Advance
|
|
|
|
* the active circuit pointer to the next active circuit in the ring. */
|
2007-03-26 16:07:59 +02:00
|
|
|
int
|
|
|
|
connection_or_flush_from_first_active_circuit(or_connection_t *conn, int max)
|
|
|
|
{
|
|
|
|
int n_flushed;
|
|
|
|
cell_queue_t *queue;
|
|
|
|
circuit_t *circ;
|
2007-03-26 16:08:18 +02:00
|
|
|
int streams_blocked;
|
2007-03-26 16:07:59 +02:00
|
|
|
circ = conn->active_circuits;
|
|
|
|
if (!circ) return 0;
|
2007-03-29 04:41:36 +02:00
|
|
|
assert_active_circuits_ok_paranoid(conn);
|
2007-03-26 16:08:18 +02:00
|
|
|
if (circ->n_conn == conn) {
|
2007-03-26 16:07:59 +02:00
|
|
|
queue = &circ->n_conn_cells;
|
2007-03-26 16:08:18 +02:00
|
|
|
streams_blocked = circ->streams_blocked_on_n_conn;
|
|
|
|
} else {
|
2007-03-26 16:07:59 +02:00
|
|
|
queue = &TO_OR_CIRCUIT(circ)->p_conn_cells;
|
2007-03-26 16:08:18 +02:00
|
|
|
streams_blocked = circ->streams_blocked_on_p_conn;
|
|
|
|
}
|
2007-03-29 04:41:36 +02:00
|
|
|
tor_assert(*next_circ_on_conn_p(circ,conn));
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2007-05-17 00:15:48 +02:00
|
|
|
for (n_flushed = 0; n_flushed < max && queue->head; ) {
|
2007-04-10 01:15:46 +02:00
|
|
|
packed_cell_t *cell = cell_queue_pop(queue);
|
2007-03-29 04:41:36 +02:00
|
|
|
tor_assert(*next_circ_on_conn_p(circ,conn));
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2007-04-10 01:15:46 +02:00
|
|
|
connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
|
|
|
|
|
|
|
|
packed_cell_free(cell);
|
2007-03-26 16:07:59 +02:00
|
|
|
++n_flushed;
|
2007-03-29 04:41:36 +02:00
|
|
|
if (circ != conn->active_circuits) {
|
|
|
|
/* If this happens, the current circuit just got made inactive by
|
|
|
|
* a call in connection_write_to_buf(). That's nothing to worry about:
|
|
|
|
* circuit_make_inactive_on_conn() already advanced conn->active_circuits
|
|
|
|
* for us.
|
|
|
|
*/
|
|
|
|
assert_active_circuits_ok_paranoid(conn);
|
|
|
|
return n_flushed;
|
|
|
|
}
|
2007-03-26 16:07:59 +02:00
|
|
|
}
|
2007-03-29 04:41:36 +02:00
|
|
|
tor_assert(*next_circ_on_conn_p(circ,conn));
|
|
|
|
assert_active_circuits_ok_paranoid(conn);
|
2007-03-26 16:07:59 +02:00
|
|
|
conn->active_circuits = *next_circ_on_conn_p(circ, conn);
|
2007-03-26 16:08:18 +02:00
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/* Is the cell queue low enough to unblock all the streams that are waiting
|
|
|
|
* to write to this circuit? */
|
2007-03-26 16:08:18 +02:00
|
|
|
if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
|
2007-03-26 16:08:35 +02:00
|
|
|
set_streams_blocked_on_circ(circ, conn, 0); /* unblock streams */
|
2007-03-26 16:08:18 +02:00
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/* Did we just ran out of cells on this queue? */
|
2007-03-26 16:07:59 +02:00
|
|
|
if (queue->n == 0) {
|
2007-04-25 08:10:16 +02:00
|
|
|
log_debug(LD_GENERAL, "Made a circuit inactive.");
|
2007-03-26 16:07:59 +02:00
|
|
|
make_circuit_inactive_on_conn(circ, conn);
|
|
|
|
}
|
|
|
|
return n_flushed;
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>orconn</b>
|
|
|
|
* transmitting in <b>direction</b>. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
|
|
|
append_cell_to_circuit_queue(circuit_t *circ, or_connection_t *orconn,
|
|
|
|
cell_t *cell, int direction)
|
|
|
|
{
|
|
|
|
cell_queue_t *queue;
|
2007-03-26 16:08:18 +02:00
|
|
|
int streams_blocked;
|
2007-03-26 16:07:59 +02:00
|
|
|
if (direction == CELL_DIRECTION_OUT) {
|
|
|
|
queue = &circ->n_conn_cells;
|
2007-03-26 16:08:18 +02:00
|
|
|
streams_blocked = circ->streams_blocked_on_n_conn;
|
2007-03-26 16:07:59 +02:00
|
|
|
} else {
|
|
|
|
or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
|
|
|
|
queue = &orcirc->p_conn_cells;
|
2007-03-26 16:08:18 +02:00
|
|
|
streams_blocked = circ->streams_blocked_on_p_conn;
|
2007-03-26 16:07:59 +02:00
|
|
|
}
|
2007-11-14 21:01:15 +01:00
|
|
|
if (cell->command == CELL_RELAY_EARLY && orconn->link_proto < 2) {
|
|
|
|
/* V1 connections don't understand RELAY_EARLY. */
|
|
|
|
cell->command = CELL_RELAY;
|
|
|
|
}
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2007-04-10 01:15:46 +02:00
|
|
|
cell_queue_append_packed_copy(queue, cell);
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/* If we have too many cells on the circuit, we should stop reading from
|
|
|
|
* the edge streams for a while. */
|
2007-03-26 16:08:18 +02:00
|
|
|
if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
|
2007-03-26 16:08:35 +02:00
|
|
|
set_streams_blocked_on_circ(circ, orconn, 1); /* block streams */
|
2007-03-26 16:07:59 +02:00
|
|
|
|
|
|
|
if (queue->n == 1) {
|
|
|
|
/* This was the first cell added to the queue. We need to make this
|
|
|
|
* circuit active. */
|
2007-04-25 08:10:16 +02:00
|
|
|
log_debug(LD_GENERAL, "Made a circuit active.");
|
2007-03-26 16:07:59 +02:00
|
|
|
make_circuit_active_on_conn(circ, orconn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! buf_datalen(orconn->_base.outbuf)) {
|
|
|
|
/* There is no data at all waiting to be sent on the outbuf. Add a
|
|
|
|
* cell, so that we can notice when it gets flushed, flushed_some can
|
|
|
|
* get called, and we can start putting more data onto the buffer then.
|
|
|
|
*/
|
2007-04-25 08:10:16 +02:00
|
|
|
log_debug(LD_GENERAL, "Primed a buffer.");
|
2007-03-26 16:07:59 +02:00
|
|
|
connection_or_flush_from_first_active_circuit(orconn, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Fail with an assert if the active circuits ring on <b>orconn</b> is
|
|
|
|
* corrupt. */
|
2007-03-26 16:07:59 +02:00
|
|
|
void
|
|
|
|
assert_active_circuits_ok(or_connection_t *orconn)
|
|
|
|
{
|
|
|
|
circuit_t *head = orconn->active_circuits;
|
|
|
|
circuit_t *cur = head;
|
|
|
|
if (! head)
|
|
|
|
return;
|
|
|
|
do {
|
|
|
|
circuit_t *next = *next_circ_on_conn_p(cur, orconn);
|
|
|
|
circuit_t *prev = *prev_circ_on_conn_p(cur, orconn);
|
|
|
|
tor_assert(next);
|
|
|
|
tor_assert(prev);
|
|
|
|
tor_assert(*next_circ_on_conn_p(prev, orconn) == cur);
|
|
|
|
tor_assert(*prev_circ_on_conn_p(next, orconn) == cur);
|
|
|
|
cur = next;
|
|
|
|
} while (cur != head);
|
|
|
|
}
|
2007-03-26 16:08:18 +02:00
|
|
|
|