14#define CHANNEL_OBJECT_PRIVATE
18#include "core/or/channelpadding.h"
39STATIC int64_t channelpadding_compute_time_until_pad_for_netflow(
channel_t *);
71#define TOR_MSEC_PER_SEC 1000
72#define TOR_USEC_PER_MSEC 1000
77#define TOR_HOUSEKEEPING_CALLBACK_MSEC 1000
82#define TOR_HOUSEKEEPING_CALLBACK_SLACK_MSEC 100
89#define CHANNEL_IS_CLIENT(chan, options) \
90 (!public_server_mode((options)) || channel_is_client(chan) || \
91 !connection_or_digest_is_known_relay((chan)->identity_digest))
89#define CHANNEL_IS_CLIENT(chan, options) \ …
102#define DFLT_NETFLOW_INACTIVE_KEEPALIVE_LOW 1500
103#define DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH 9500
104#define DFLT_NETFLOW_INACTIVE_KEEPALIVE_MIN 0
105#define DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX 60000
107 DFLT_NETFLOW_INACTIVE_KEEPALIVE_LOW,
108 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MIN,
109 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX);
111 DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH,
113 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX);
115#define DFLT_NETFLOW_REDUCED_KEEPALIVE_LOW 9000
116#define DFLT_NETFLOW_REDUCED_KEEPALIVE_HIGH 14000
117#define DFLT_NETFLOW_REDUCED_KEEPALIVE_MIN 0
118#define DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX 60000
121 DFLT_NETFLOW_REDUCED_KEEPALIVE_LOW,
122 DFLT_NETFLOW_REDUCED_KEEPALIVE_MIN,
123 DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX);
127 DFLT_NETFLOW_REDUCED_KEEPALIVE_HIGH,
129 DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX);
131#define CONNTIMEOUT_RELAYS_DFLT (60*60)
132#define CONNTIMEOUT_RELAYS_MIN 60
133#define CONNTIMEOUT_RELAYS_MAX (7*24*60*60)
136 CONNTIMEOUT_RELAYS_DFLT,
137 CONNTIMEOUT_RELAYS_MIN,
138 CONNTIMEOUT_RELAYS_MAX);
140#define CIRCTIMEOUT_CLIENTS_DFLT (30*60)
141#define CIRCTIMEOUT_CLIENTS_MIN 60
142#define CIRCTIMEOUT_CLIENTS_MAX (24*60*60)
145 CIRCTIMEOUT_CLIENTS_DFLT,
146 CIRCTIMEOUT_CLIENTS_MIN,
147 CIRCTIMEOUT_CLIENTS_MAX);
157 CHANNELPADDING_SOS_PARAM,
158 CHANNELPADDING_SOS_DEFAULT, 0, 1);
186 if (low_timeout == 0 && low_timeout == high_timeout)
193 high_timeout = MAX(high_timeout, chan->padding_timeout_high_ms);
196 if (low_timeout >= high_timeout)
242 return low_timeout + MAX(X1, X2);
253 const channelpadding_negotiate_t *pad_vars)
255 if (pad_vars->version != 0) {
256 static ratelim_t version_limit = RATELIM_INIT(600);
259 "Got a PADDING_NEGOTIATE cell with an unknown version. Ignoring.");
270 static ratelim_t relay_limit = RATELIM_INIT(600);
273 "Got a PADDING_NEGOTIATE from relay at %s (%s). "
274 "This should not happen.",
280 chan->
padding_enabled = (pad_vars->command == CHANNELPADDING_COMMAND_START);
285 pad_vars->ito_low_ms);
289 pad_vars->ito_high_ms);
292 "Negotiated padding=%d, lo=%d, hi=%d on %"PRIu64,
294 chan->padding_timeout_high_ms,
309 channelpadding_negotiate_t disable;
313 tor_assert(BASE_CHAN_TO_TLS(chan)->conn->link_proto >=
314 MIN_LINK_PROTO_FOR_CHANNEL_PADDING);
316 memset(&cell, 0,
sizeof(
cell_t));
317 memset(&disable, 0,
sizeof(channelpadding_negotiate_t));
318 cell.
command = CELL_PADDING_NEGOTIATE;
320 channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
326 if (chan->write_cell(chan, &cell) == 1)
340 uint16_t high_timeout)
342 channelpadding_negotiate_t enable;
346 tor_assert(BASE_CHAN_TO_TLS(chan)->conn->link_proto >=
347 MIN_LINK_PROTO_FOR_CHANNEL_PADDING);
349 memset(&cell, 0,
sizeof(
cell_t));
350 memset(&enable, 0,
sizeof(channelpadding_negotiate_t));
351 cell.
command = CELL_PADDING_NEGOTIATE;
353 channelpadding_negotiate_set_command(&enable, CHANNELPADDING_COMMAND_START);
354 channelpadding_negotiate_set_ito_low_ms(&enable, low_timeout);
355 channelpadding_negotiate_set_ito_high_ms(&enable, high_timeout);
361 if (chan->write_cell(chan, &cell) == 1)
383 "Scheduled a netflow padding cell, but connection already closed.");
402 monotime_coarse_t now;
403 monotime_coarse_get(&now);
406 "Sending netflow keepalive on %"PRIu64
" to %s (%s) after "
407 "%"PRId64
" ms. Delta %"PRId64
"ms",
421 memset(&cell, 0,
sizeof(cell));
423 chan->write_cell(chan, &cell);
438 channel_t *chan = channel_handle_get((
struct channel_handle_t*)args);
439 (void)timer; (void)when;
441 if (chan && CHANNEL_CAN_HANDLE_CELLS(chan)) {
445 OR_CONNECTION_MAGIC);
451 "Channel closed while waiting for timer.");
465static channelpadding_decision_t
474 return CHANNELPADDING_PADDING_SENT;
477 timeout.tv_sec = in_ms/TOR_MSEC_PER_SEC;
478 timeout.tv_usec = (in_ms%TOR_USEC_PER_MSEC)*TOR_USEC_PER_MSEC;
497 return CHANNELPADDING_PADDING_SCHEDULED;
514#define CHANNELPADDING_TIME_LATER -1
515#define CHANNELPADDING_TIME_DISABLED -2
517channelpadding_compute_time_until_pad_for_netflow(
channel_t *chan)
519 monotime_coarse_t now;
520 monotime_coarse_get(&now);
529 int32_t padding_timeout =
532 if (!padding_timeout)
533 return CHANNELPADDING_TIME_DISABLED;
540 const int64_t ms_till_pad =
549 if (ms_till_pad > DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX) {
552 "Channel padding timeout scheduled %"PRId64
"ms in the future. "
553 "Did the monotonic clock just jump?",
573#define NETFLOW_MISSED_WINDOW (150000 - DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH)
574 if (ms_till_pad < 0) {
575 if (ms_till_pad < -NETFLOW_MISSED_WINDOW) {
577 "Channel padding delay of %"PRIu64
"ms occurred in the past "
578 "that exceeded the allowed time window.",
613#define CONNTIMEOUT_CLIENTS_BASE 180
614 timeout = CONNTIMEOUT_CLIENTS_BASE
715 "Reduced padding on channel %"PRIu64
": lo=%d, hi=%d",
730channelpadding_decision_t
737 return CHANNELPADDING_WONTPAD;
741 return CHANNELPADDING_WONTPAD;
742 }
else if (chan->
channel_usage != CHANNEL_USED_FOR_USER_TRAFFIC) {
743 return CHANNELPADDING_WONTPAD;
747 return CHANNELPADDING_PADDING_ALREADY_SCHEDULED;
754 return CHANNELPADDING_WONTPAD;
757 if (hs_service_allow_non_anonymous_connection(options) &&
764 return CHANNELPADDING_WONTPAD;
772 int is_client_channel = 0;
775 is_client_channel = 1;
782 int64_t pad_time_ms =
783 channelpadding_compute_time_until_pad_for_netflow(chan);
785 if (pad_time_ms == CHANNELPADDING_TIME_DISABLED) {
786 return CHANNELPADDING_WONTPAD;
789 return CHANNELPADDING_PADLATER;
791 if (BUG(pad_time_ms > INT_MAX)) {
792 pad_time_ms = INT_MAX;
804 return CHANNELPADDING_WONTPAD;
807 return CHANNELPADDING_PADLATER;
816channelpadding_log_heartbeat(
void)
819 static uint8_t heartbeat = 0;
823 "Average channel padding delay of delays that exceeded "
824 "the allowed time window since %s: %"PRIu64
"ms "
825 "(Number of delays: %"PRIu64
")",
826 heartbeat ?
"previous heartbeat" :
"startup",
const char * hex_str(const char *from, size_t fromlen)
Fixed-size cell structure.
const char * channel_describe_peer(channel_t *chan)
Header file for channel.c.
static uint64_t total_timers_pending
void channelpadding_disable_padding_on_channel(channel_t *chan)
static int consensus_nf_ito_high
int channelpadding_get_circuits_available_timeout(void)
static int consensus_nf_pad_before_usage
unsigned int channelpadding_get_channel_idle_timeout(const channel_t *chan, int is_canonical)
static int consensus_nf_ito_low
int channelpadding_send_enable_command(channel_t *chan, uint16_t low_timeout, uint16_t high_timeout)
#define TOR_HOUSEKEEPING_CALLBACK_SLACK_MSEC
static void channelpadding_send_padding_cell_for_callback(channel_t *chan)
#define TOR_HOUSEKEEPING_CALLBACK_MSEC
static channelpadding_decision_t channelpadding_schedule_padding(channel_t *chan, int in_ms)
STATIC int channelpadding_send_disable_command(channel_t *)
int channelpadding_update_padding_for_channel(channel_t *chan, const channelpadding_negotiate_t *pad_vars)
channelpadding_decision_t channelpadding_decide_to_pad_channel(channel_t *chan)
static int consensus_nf_pad_single_onion
static int consensus_nf_conntimeout_clients
static void channelpadding_send_padding_callback(tor_timer_t *timer, void *args, const struct monotime_t *when)
void channelpadding_new_consensus_params(const networkstatus_t *ns)
void channelpadding_reduce_padding_on_channel(channel_t *chan)
static int consensus_nf_ito_low_reduced
static int consensus_nf_ito_high_reduced
static int consensus_nf_conntimeout_relays
static int consensus_nf_pad_relays
static uint64_t channel_padding_delayed_count
static uint64_t channel_padding_delayed_ms
#define CHANNELPADDING_TIME_LATER
#define CHANNEL_IS_CLIENT(chan, options)
STATIC int32_t channelpadding_get_netflow_inactive_timeout_ms(const channel_t *)
Header file for channeltls.c.
unsigned int circuitmux_num_cells(circuitmux_t *cmux)
Functions and types for monotonic times.
const or_options_t * get_options(void)
Header file for config.c.
void assert_connection_ok(connection_t *conn, time_t now)
Header file for connection.c.
int connection_or_digest_is_known_relay(const char *id_digest)
Header file for connection_or.c.
Common functions for using (pseudo-)random number generators.
int crypto_rand_int(unsigned int max)
Header file containing service data for the HS subsystem.
#define log_fn(severity, domain, args,...)
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Header file for mainloop.c.
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
Master header file for Tor-specific functionality.
#define CELL_PAYLOAD_SIZE
void rep_hist_padding_count_timers(uint64_t num_timers)
Header file for rephist.c.
Header file for router.c.
Header file for routermode.c.
uint8_t payload[CELL_PAYLOAD_SIZE]
uint16_t padding_timeout_low_ms
monotime_coarse_t next_padding_time
unsigned int padding_enabled
char identity_digest[DIGEST_LEN]
uint64_t global_identifier
channel_usage_info_t channel_usage
struct channel_handle_t * timer_handle
unsigned int pending_padding_callback
monotime_coarse_t timestamp_xfer
unsigned int currently_padding
struct tor_timer_t * padding_timer
int(* has_queued_writes)(channel_t *)
int ReducedConnectionPadding
int CircuitsAvailableTimeout
void timer_set_cb(tor_timer_t *t, timer_cb_fn_t cb, void *arg)
void timer_schedule(tor_timer_t *t, const struct timeval *tv)
tor_timer_t * timer_new(timer_cb_fn_t cb, void *arg)
#define tor_fragile_assert()