Tor
0.4.7.0-alpha-dev
|
Handle relay cell encryption/decryption, plus packaging and receiving from circuits, plus queuing on circuits. More...
#include "core/or/or.h"
#include "feature/client/addressmap.h"
#include "lib/err/backtrace.h"
#include "lib/buf/buffers.h"
#include "core/or/channel.h"
#include "feature/client/circpathbias.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/circuitpadding.h"
#include "core/or/extendinfo.h"
#include "lib/compress/compress.h"
#include "app/config/config.h"
#include "core/mainloop/connection.h"
#include "core/or/connection_edge.h"
#include "core/or/connection_or.h"
#include "feature/control/control_events.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_util.h"
#include "feature/dircommon/directory.h"
#include "feature/relay/dns.h"
#include "feature/relay/circuitbuild_relay.h"
#include "feature/stats/geoip_stats.h"
#include "feature/hs/hs_cache.h"
#include "core/mainloop/mainloop.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
#include "core/or/onion.h"
#include "core/or/policies.h"
#include "core/or/reasons.h"
#include "core/or/relay.h"
#include "core/crypto/relay_crypto.h"
#include "feature/rend/rendcommon.h"
#include "feature/nodelist/describe.h"
#include "feature/nodelist/routerlist.h"
#include "core/or/scheduler.h"
#include "feature/hs/hs_metrics.h"
#include "feature/stats/rephist.h"
#include "core/or/cell_st.h"
#include "core/or/cell_queue_st.h"
#include "core/or/cpath_build_state_st.h"
#include "feature/dircommon/dir_connection_st.h"
#include "core/or/destroy_cell_queue_st.h"
#include "core/or/entry_connection_st.h"
#include "core/or/extend_info_st.h"
#include "core/or/or_circuit_st.h"
#include "core/or/origin_circuit_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "core/or/socks_request_st.h"
#include "core/or/sendme.h"
Go to the source code of this file.
Macros | |
#define | RELAY_PRIVATE |
#define | CELL_QUEUE_HIGHWATER_SIZE 256 |
#define | CELL_QUEUE_LOWWATER_SIZE 64 |
#define | CELL_PADDING_GAP 4 |
#define | MAX_RESOLVE_FAILURES 3 |
#define | EARLY_WARNING_INTERVAL 3600 |
#define | RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES (RELAY_PAYLOAD_SIZE - CELL_PADDING_GAP - 16) |
#define | MEMORY_PRESSURE_INTERVAL (30*60) |
#define | RELAY_CIRC_CELL_QUEUE_SIZE_MIN CIRCWINDOW_START_MAX |
#define | RELAY_CIRC_CELL_QUEUE_SIZE_MAX INT32_MAX |
#define | RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT (50 * RELAY_CIRC_CELL_QUEUE_SIZE_MIN) |
Variables | |
uint64_t | stats_n_relay_cells_relayed = 0 |
uint64_t | stats_n_relay_cells_delivered = 0 |
uint64_t | stats_n_circ_max_cell_reached = 0 |
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 |
static size_t | total_cells_allocated = 0 |
static time_t | last_time_under_memory_pressure = 0 |
uint64_t | oom_stats_n_bytes_removed_dns = 0 |
uint64_t | oom_stats_n_bytes_removed_cell = 0 |
uint64_t | oom_stats_n_bytes_removed_geoip = 0 |
uint64_t | oom_stats_n_bytes_removed_hsdir = 0 |
static int32_t | max_circuit_cell_queue_size |
Handle relay cell encryption/decryption, plus packaging and receiving from circuits, plus queuing on circuits.
This is a core modules that makes Tor work. It's responsible for dealing with RELAY cells (the ones that travel more than one hop along a circuit), by:
RELAY cells are generated throughout the code at the client or relay side, using relay_send_command_from_edge() or one of the functions like connection_edge_send_command() that calls it. Of particular interest is connection_edge_package_raw_inbuf(), which takes information that has arrived on an edge connection socket, and packages it as a RELAY_DATA cell – this is how information is actually sent across the Tor network. The cryptography for these functions is handled deep in circuit_package_relay_cell(), which either adds a single layer of encryption (if we're an exit), or multiple layers (if we're the origin of the circuit). After construction and encryption, the RELAY cells are passed to append_cell_to_circuit_queue(), which queues them for transmission and tells the circuitmux (see circuitmux.c) that the circuit is waiting to send something.
Incoming RELAY cells arrive at circuit_receive_relay_cell(), called from command.c. There they are decrypted and, if they are for us, are passed to connection_edge_process_relay_cell(). If they're not for us, they're re-queued for retransmission again with append_cell_to_circuit_queue().
The connection_edge_process_relay_cell() function handles all the different types of relay cells, launching requests or transmitting data as needed.
Definition in file relay.c.
#define CELL_PADDING_GAP 4 |
#define CELL_QUEUE_HIGHWATER_SIZE 256 |
#define CELL_QUEUE_LOWWATER_SIZE 64 |
#define MAX_RESOLVE_FAILURES 3 |
#define MEMORY_PRESSURE_INTERVAL (30*60) |
#define RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES (RELAY_PAYLOAD_SIZE - CELL_PADDING_GAP - 16) |
STATIC void address_ttl_free_ | ( | address_ttl_t * | addr | ) |
|
static |
Called when we have gotten an END_REASON_EXITPOLICY failure on circ for conn, while attempting to connect via node. If the node told us which address it rejected, then addr is that address; otherwise it is AF_UNSPEC.
If we are sure the node should have allowed this address, mark the node as having a reject *:* exit policy. Otherwise, mark the circuit as unusable for this particular address.
int append_address_to_payload | ( | uint8_t * | payload_out, |
const tor_addr_t * | addr | ||
) |
Append an encoded value of addr to payload_out, which must have at least 18 bytes of free space. The encoding is, as specified in tor-spec.txt: RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte] LENGTH [1 byte] ADDRESS [length bytes] Return the number of bytes added, or -1 on error
void append_cell_to_circuit_queue | ( | circuit_t * | circ, |
channel_t * | chan, | ||
cell_t * | cell, | ||
cell_direction_t | direction, | ||
streamid_t | fromstream | ||
) |
void cell_queue_append | ( | cell_queue_t * | queue, |
packed_cell_t * | cell | ||
) |
Append cell to the end of queue.
Definition at line 2558 of file relay.c.
Referenced by cell_queue_append_packed_copy().
void cell_queue_append_packed_copy | ( | circuit_t * | circ, |
cell_queue_t * | queue, | ||
int | exitward, | ||
const cell_t * | cell, | ||
int | wide_circ_ids, | ||
int | use_stats | ||
) |
void cell_queue_clear | ( | cell_queue_t * | queue | ) |
Remove and free every cell in queue.
Definition at line 2593 of file relay.c.
Referenced by circuit_clear_cell_queue().
void cell_queue_init | ( | cell_queue_t * | queue | ) |
Initialize queue as an empty cell queue.
Definition at line 2585 of file relay.c.
Referenced by init_circuit_base().
STATIC packed_cell_t* cell_queue_pop | ( | cell_queue_t * | queue | ) |
STATIC int cell_queues_check_size | ( | void | ) |
int channel_flush_from_first_active_circuit | ( | channel_t * | chan, |
int | max | ||
) |
void channel_unlink_all_circuits | ( | channel_t * | chan, |
smartlist_t * | circuits_out | ||
) |
Remove all circuits from the cmux on chan.
If circuits_out is non-NULL, add all detached circuits to circuits_out.
Definition at line 2826 of file relay.c.
Referenced by circuit_unlink_all_from_channel().
Remove all the cells queued on circ for chan.
Definition at line 3257 of file relay.c.
Referenced by circuit_about_to_free_atexit().
|
static |
int circuit_package_relay_cell | ( | cell_t * | cell, |
circuit_t * | circ, | ||
cell_direction_t | cell_direction, | ||
crypt_path_t * | layer_hint, | ||
streamid_t | on_stream, | ||
const char * | filename, | ||
int | lineno | ||
) |
|
static |
Return 1 if we shouldn't restart reading on this circuit, even if we get a SENDME. Else return 0.
Definition at line 3284 of file relay.c.
Referenced by circuit_resume_edge_reading().
int circuit_receive_relay_cell | ( | cell_t * | cell, |
circuit_t * | circ, | ||
cell_direction_t | cell_direction | ||
) |
Receive a relay cell:
Return -reason on failure.
void circuit_reset_sendme_randomness | ( | circuit_t * | circ | ) |
Called when initializing a circuit, or when we have reached the end of the window in which we need to send some randomness so that incoming sendme cells will be unpredictable. Resets the flags and picks a new window.
Definition at line 2070 of file relay.c.
Referenced by init_circuit_base().
|
static |
|
static |
A helper function for circuit_resume_edge_reading() above. The arguments are the same, except that conn is the head of a linked list of edge streams that should each be considered.
Update channel usage state based on the type of relay cell and circuit properties.
This is needed to determine if a client channel is being used for application traffic, and if a relay channel is being used for multihop circuits and application traffic. The decision to pad in channelpadding.c depends upon this info (as well as consensus parameters) to decide what channels to pad.
STATIC int connected_cell_parse | ( | const relay_header_t * | rh, |
const cell_t * | cell, | ||
tor_addr_t * | addr_out, | ||
int * | ttl_out | ||
) |
Extract the contents of a connected cell in cell, whose relay header has already been parsed into rh. On success, set addr_out to the address we're connected to, and ttl_out to the ttl of that address, in seconds, and return 0. On failure, return -1.
Note that the resulting address can be UNSPEC if the connected cell had no address (as for a stream to an union service or a tunneled directory connection), and that the ttl can be absent (in which case ttl_out is set to -1).
|
static |
|
static |
Called when we receive an END cell on a stream that isn't open yet, from the client side. Arguments are as for connection_edge_process_relay_cell().
int connection_edge_package_raw_inbuf | ( | edge_connection_t * | conn, |
int | package_partial, | ||
int * | max_cells | ||
) |
If conn has an entire relay payload of bytes on its inbuf (or package_partial is true), and the appropriate package windows aren't empty, grab a cell and send it down the circuit.
If *max_cells is given, package no more than max_cells. Decrement *max_cells by the number of cells packaged.
Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should be marked for close, else return 0.
STATIC int connection_edge_process_relay_cell | ( | cell_t * | cell, |
circuit_t * | circ, | ||
edge_connection_t * | conn, | ||
crypt_path_t * | layer_hint | ||
) |
An incoming relay cell has arrived on circuit circ. If conn is NULL this is a control cell, else cell is destined for conn.
If layer_hint is defined, then we're the origin of the circuit, and it specifies the hop that packaged cell.
Return -reason if you want to warn and tear down the circuit, else 0.
|
static |
An incoming relay cell has arrived from circuit circ to stream conn.
The arguments here are the same as in connection_edge_process_relay_cell() below; this function is called from there when conn is defined and not in an open state.
STATIC int connection_edge_process_resolved_cell | ( | edge_connection_t * | conn, |
const cell_t * | cell, | ||
const relay_header_t * | rh | ||
) |
int connection_edge_send_command | ( | edge_connection_t * | fromconn, |
uint8_t | relay_command, | ||
const char * | payload, | ||
size_t | payload_len | ||
) |
Make a relay cell out of relay_command and payload, and send it onto the open circuit circ. fromconn is the stream that's sending the relay cell, or NULL if it's a control cell. cpath_layer 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.
const uint8_t* decode_address_from_payload | ( | tor_addr_t * | addr_out, |
const uint8_t * | payload, | ||
int | payload_len | ||
) |
Given payload_len bytes at payload, starting with an address encoded as by append_address_to_payload(), try to decode the address into *addr_out. Return the next byte in the payload after the address on success, or NULL on failure.
void destroy_cell_queue_append | ( | destroy_cell_queue_t * | queue, |
circid_t | circid, | ||
uint8_t | reason | ||
) |
void destroy_cell_queue_clear | ( | destroy_cell_queue_t * | queue | ) |
void destroy_cell_queue_init | ( | destroy_cell_queue_t * | queue | ) |
STATIC destroy_cell_t* destroy_cell_queue_pop | ( | destroy_cell_queue_t * | queue | ) |
Extract and return the cell at the head of queue; return NULL if queue is empty.
Definition at line 2641 of file relay.c.
Referenced by channel_flush_from_first_active_circuit().
|
static |
Convert a destroy_cell_t to a newly allocated cell_t. Frees its input.
Definition at line 2669 of file relay.c.
Referenced by channel_flush_from_first_active_circuit().
void dump_cell_pool_usage | ( | int | severity | ) |
Log current statistics for cell pool allocation at log level severity.
Definition at line 2531 of file relay.c.
Referenced by dumpmemusage().
|
static |
STATIC size_t get_pad_cell_offset | ( | size_t | data_len | ) |
Return the offset where the padding should start. The data_len is the relay payload length expected to be put in the cell. It can not be bigger than RELAY_PAYLOAD_SIZE else this function assert().
Value will always be smaller than CELL_PAYLOAD_SIZE because this offset is for the entire cell length not just the data payload length. Zero is returned if there is no room for padding.
This function always skips the first 4 bytes after the payload because having some unused zero bytes has saved us a lot of times in the past.
STATIC int handle_relay_cell_command | ( | cell_t * | cell, |
circuit_t * | circ, | ||
edge_connection_t * | conn, | ||
crypt_path_t * | layer_hint, | ||
relay_header_t * | rh, | ||
int | optimistic_data | ||
) |
A helper for connection_edge_process_relay_cell(): Actually handles the cell that we received on the connection.
The arguments are the same as in the parent function connection_edge_process_relay_cell(), plus the relay header rh as unpacked by the parent function, and optimistic_data as set by the parent function.
int have_been_under_memory_pressure | ( | void | ) |
|
inlinestatic |
Allocate a new copy of packed cell.
Definition at line 2549 of file relay.c.
Referenced by cell_queue_append_packed_copy().
void packed_cell_free_ | ( | packed_cell_t * | cell | ) |
|
inlinestatic |
Release storage held by cell.
Definition at line 2505 of file relay.c.
Referenced by cell_queue_clear(), and packed_cell_free_().
circid_t packed_cell_get_circid | ( | const packed_cell_t * | cell, |
int | wide_circ_ids | ||
) |
uint8_t packed_cell_get_command | ( | const packed_cell_t * | cell, |
int | wide_circ_ids | ||
) |
Extract the command from a packed cell.
Definition at line 2892 of file relay.c.
Referenced by write_packed_cell().
size_t packed_cell_mem_cost | ( | void | ) |
STATIC packed_cell_t* packed_cell_new | ( | void | ) |
Allocate and return a new packed_cell_t.
Definition at line 2513 of file relay.c.
Referenced by destroy_cell_to_packed_cell(), and packed_cell_copy().
|
static |
Process a SENDME cell that arrived on circ. If it is a stream level cell, it is destined for the given conn. If it is a circuit level cell, it is destined for the layer_hint. The domain is the logging domain that should be used.
Return 0 if everything went well or a negative value representing a circuit end reason on error for which the caller is responsible for closing it.
|
static |
void relay_header_pack | ( | uint8_t * | dest, |
const relay_header_t * | src | ||
) |
Pack the relay_header_t host-order structure src into network-order in the buffer dest. See tor-spec.txt for details about the wire format.
Definition at line 478 of file relay.c.
Referenced by relay_set_digest().
void relay_header_unpack | ( | relay_header_t * | dest, |
const uint8_t * | src | ||
) |
Unpack the network-order buffer src into a host-order relay_header_t structure dest.
Definition at line 491 of file relay.c.
Referenced by connection_exit_begin_conn(), connection_exit_begin_resolve(), pathbias_check_probe_response(), pathbias_count_valid_cells(), relay_lookup_conn(), and relay_set_digest().
|
static |
int relay_send_command_from_edge_ | ( | streamid_t | stream_id, |
circuit_t * | circ, | ||
uint8_t | relay_command, | ||
const char * | payload, | ||
size_t | payload_len, | ||
crypt_path_t * | cpath_layer, | ||
const char * | filename, | ||
int | lineno | ||
) |
Make a relay cell out of relay_command and payload, and send it onto the open circuit circ. stream_id is the ID on circ for the stream that's sending the relay cell, or 0 if it's a control cell. cpath_layer 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.
|
static |
STATIC int resolved_cell_parse | ( | const cell_t * | cell, |
const relay_header_t * | rh, | ||
smartlist_t * | addresses_out, | ||
int * | errcode_out | ||
) |
|
static |
Block (if block is true) or unblock (if block is false) every edge connection that is using circ to write to chan, and start or stop reading as appropriate.
If stream_id is nonzero, block only the edge connection whose stream_id matches it.
Returns the number of streams whose status we changed.
void update_circuit_on_cmux_ | ( | circuit_t * | circ, |
cell_direction_t | direction, | ||
const char * | file, | ||
int | lineno | ||
) |
|
static |
The time at which we were last low on memory.
Definition at line 2702 of file relay.c.
Referenced by have_been_under_memory_pressure().
|
static |
uint64_t oom_stats_n_bytes_removed_dns = 0 |
uint64_t stats_n_circ_max_cell_reached = 0 |
Stats: how many circuits have we closed due to the cell queue limit being reached (see append_cell_to_circuit_queue())
uint64_t stats_n_data_bytes_packaged = 0 |
uint64_t stats_n_data_bytes_received = 0 |
uint64_t stats_n_data_cells_packaged = 0 |
uint64_t stats_n_data_cells_received = 0 |
uint64_t stats_n_relay_cells_delivered = 0 |
uint64_t stats_n_relay_cells_relayed = 0 |
|
static |
The total number of cells we have allocated.
Definition at line 2501 of file relay.c.
Referenced by packed_cell_free_unchecked(), and packed_cell_new().