14 #define CHANNEL_OBJECT_PRIVATE
18 #include "core/or/channelpadding.h"
39 STATIC int64_t channelpadding_compute_time_until_pad_for_netflow(
channel_t *);
64 #define TOR_MSEC_PER_SEC 1000
65 #define TOR_USEC_PER_MSEC 1000
70 #define TOR_HOUSEKEEPING_CALLBACK_MSEC 1000
75 #define TOR_HOUSEKEEPING_CALLBACK_SLACK_MSEC 100
82 #define CHANNEL_IS_CLIENT(chan, options) \
83 (!public_server_mode((options)) || channel_is_client(chan) || \
84 !connection_or_digest_is_known_relay((chan)->identity_digest))
95 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_LOW 1500
96 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH 9500
97 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_MIN 0
98 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX 60000
100 DFLT_NETFLOW_INACTIVE_KEEPALIVE_LOW,
101 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MIN,
102 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX);
104 DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH,
106 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX);
108 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_LOW 9000
109 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_HIGH 14000
110 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_MIN 0
111 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX 60000
114 DFLT_NETFLOW_REDUCED_KEEPALIVE_LOW,
115 DFLT_NETFLOW_REDUCED_KEEPALIVE_MIN,
116 DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX);
120 DFLT_NETFLOW_REDUCED_KEEPALIVE_HIGH,
122 DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX);
124 #define CONNTIMEOUT_RELAYS_DFLT (60*60)
125 #define CONNTIMEOUT_RELAYS_MIN 60
126 #define CONNTIMEOUT_RELAYS_MAX (7*24*60*60)
129 CONNTIMEOUT_RELAYS_DFLT,
130 CONNTIMEOUT_RELAYS_MIN,
131 CONNTIMEOUT_RELAYS_MAX);
133 #define CIRCTIMEOUT_CLIENTS_DFLT (30*60)
134 #define CIRCTIMEOUT_CLIENTS_MIN 60
135 #define CIRCTIMEOUT_CLIENTS_MAX (24*60*60)
138 CIRCTIMEOUT_CLIENTS_DFLT,
139 CIRCTIMEOUT_CLIENTS_MIN,
140 CIRCTIMEOUT_CLIENTS_MAX);
150 CHANNELPADDING_SOS_PARAM,
151 CHANNELPADDING_SOS_DEFAULT, 0, 1);
179 if (low_timeout == 0 && low_timeout == high_timeout)
186 high_timeout =
MAX(high_timeout, chan->padding_timeout_high_ms);
189 if (low_timeout == high_timeout)
235 return low_timeout +
MAX(X1, X2);
246 const channelpadding_negotiate_t *pad_vars)
248 if (pad_vars->version != 0) {
249 static ratelim_t version_limit = RATELIM_INIT(600);
252 "Got a PADDING_NEGOTIATE cell with an unknown version. Ignoring.");
263 static ratelim_t relay_limit = RATELIM_INIT(600);
266 "Got a PADDING_NEGOTIATE from relay at %s (%s). "
267 "This should not happen.",
273 chan->
padding_enabled = (pad_vars->command == CHANNELPADDING_COMMAND_START);
278 pad_vars->ito_low_ms);
282 pad_vars->ito_high_ms);
285 "Negotiated padding=%d, lo=%d, hi=%d on %"PRIu64,
287 chan->padding_timeout_high_ms,
302 channelpadding_negotiate_t disable;
306 tor_assert(BASE_CHAN_TO_TLS(chan)->conn->link_proto >=
307 MIN_LINK_PROTO_FOR_CHANNEL_PADDING);
309 memset(&cell, 0,
sizeof(
cell_t));
310 memset(&disable, 0,
sizeof(channelpadding_negotiate_t));
311 cell.
command = CELL_PADDING_NEGOTIATE;
313 channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
319 if (chan->write_cell(chan, &cell) == 1)
333 uint16_t high_timeout)
335 channelpadding_negotiate_t enable;
339 tor_assert(BASE_CHAN_TO_TLS(chan)->conn->link_proto >=
340 MIN_LINK_PROTO_FOR_CHANNEL_PADDING);
342 memset(&cell, 0,
sizeof(
cell_t));
343 memset(&enable, 0,
sizeof(channelpadding_negotiate_t));
344 cell.
command = CELL_PADDING_NEGOTIATE;
346 channelpadding_negotiate_set_command(&enable, CHANNELPADDING_COMMAND_START);
347 channelpadding_negotiate_set_ito_low_ms(&enable, low_timeout);
348 channelpadding_negotiate_set_ito_high_ms(&enable, high_timeout);
354 if (chan->write_cell(chan, &cell) == 1)
376 "Scheduled a netflow padding cell, but connection already closed.");
395 monotime_coarse_t now;
396 monotime_coarse_get(&now);
399 "Sending netflow keepalive on %"PRIu64
" to %s (%s) after "
400 "%"PRId64
" ms. Delta %"PRId64
"ms",
414 memset(&cell, 0,
sizeof(cell));
416 chan->write_cell(chan, &cell);
431 channel_t *chan = channel_handle_get((
struct channel_handle_t*)args);
432 (void)timer; (void)when;
434 if (chan && CHANNEL_CAN_HANDLE_CELLS(chan)) {
438 OR_CONNECTION_MAGIC);
444 "Channel closed while waiting for timer.");
458 static channelpadding_decision_t
467 return CHANNELPADDING_PADDING_SENT;
470 timeout.tv_sec = in_ms/TOR_MSEC_PER_SEC;
471 timeout.tv_usec = (in_ms%TOR_USEC_PER_MSEC)*TOR_USEC_PER_MSEC;
490 return CHANNELPADDING_PADDING_SCHEDULED;
507 #define CHANNELPADDING_TIME_LATER -1
508 #define CHANNELPADDING_TIME_DISABLED -2
510 channelpadding_compute_time_until_pad_for_netflow(
channel_t *chan)
512 monotime_coarse_t now;
513 monotime_coarse_get(&now);
522 int32_t padding_timeout =
525 if (!padding_timeout)
526 return CHANNELPADDING_TIME_DISABLED;
533 const int64_t ms_till_pad =
542 if (ms_till_pad > DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX) {
545 "Channel padding timeout scheduled %"PRId64
"ms in the future. "
546 "Did the monotonic clock just jump?",
566 #define NETFLOW_MISSED_WINDOW (150000 - DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH)
567 if (ms_till_pad < 0) {
568 int severity = (ms_till_pad < -NETFLOW_MISSED_WINDOW)
571 "Channel padding timeout scheduled %"PRId64
"ms in the past. ",
603 #define CONNTIMEOUT_CLIENTS_BASE 180
604 timeout = CONNTIMEOUT_CLIENTS_BASE
705 "Reduced padding on channel %"PRIu64
": lo=%d, hi=%d",
720 channelpadding_decision_t
727 return CHANNELPADDING_WONTPAD;
731 return CHANNELPADDING_WONTPAD;
732 }
else if (chan->
channel_usage != CHANNEL_USED_FOR_USER_TRAFFIC) {
733 return CHANNELPADDING_WONTPAD;
737 return CHANNELPADDING_PADDING_ALREADY_SCHEDULED;
744 return CHANNELPADDING_WONTPAD;
747 if (hs_service_allow_non_anonymous_connection(options) &&
754 return CHANNELPADDING_WONTPAD;
762 int is_client_channel = 0;
765 is_client_channel = 1;
772 int64_t pad_time_ms =
773 channelpadding_compute_time_until_pad_for_netflow(chan);
775 if (pad_time_ms == CHANNELPADDING_TIME_DISABLED) {
776 return CHANNELPADDING_WONTPAD;
779 return CHANNELPADDING_PADLATER;
781 if (BUG(pad_time_ms > INT_MAX)) {
782 pad_time_ms = INT_MAX;
794 return CHANNELPADDING_WONTPAD;
797 return CHANNELPADDING_PADLATER;
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
#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()