Macros | Functions | Variables
channelpadding.c File Reference

Link-level padding code. More...

#include "core/or/or.h"
#include "core/or/channel.h"
#include "core/or/channelpadding.h"
#include "core/or/channeltls.h"
#include "app/config/config.h"
#include "feature/nodelist/networkstatus.h"
#include "core/mainloop/connection.h"
#include "core/or/connection_or.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "core/mainloop/mainloop.h"
#include "feature/stats/rephist.h"
#include "feature/relay/router.h"
#include "feature/relay/routermode.h"
#include "lib/time/compat_time.h"
#include "lib/evloop/timers.h"
#include "feature/hs/hs_service.h"
#include "core/or/cell_st.h"
#include "core/or/or_connection_st.h"

Go to the source code of this file.


#define TOR_MSEC_PER_SEC   1000
#define TOR_USEC_PER_MSEC   1000
#define CHANNEL_IS_CLIENT(chan, options)
#define CONNTIMEOUT_RELAYS_MAX   (7*24*60*60)
#define CIRCTIMEOUT_CLIENTS_MAX   (24*60*60)


STATIC int32_t channelpadding_get_netflow_inactive_timeout_ms (const channel_t *)
STATIC int channelpadding_send_disable_command (channel_t *)
STATIC int64_t channelpadding_compute_time_until_pad_for_netflow (channel_t *)
void channelpadding_new_consensus_params (const networkstatus_t *ns)
int channelpadding_update_padding_for_channel (channel_t *chan, const channelpadding_negotiate_t *pad_vars)
int channelpadding_send_enable_command (channel_t *chan, uint16_t low_timeout, uint16_t high_timeout)
static void channelpadding_send_padding_cell_for_callback (channel_t *chan)
static void channelpadding_send_padding_callback (tor_timer_t *timer, void *args, const struct monotime_t *when)
static channelpadding_decision_t channelpadding_schedule_padding (channel_t *chan, int in_ms)
unsigned int channelpadding_get_channel_idle_timeout (const channel_t *chan, int is_canonical)
int channelpadding_get_circuits_available_timeout (void)
void channelpadding_disable_padding_on_channel (channel_t *chan)
void channelpadding_reduce_padding_on_channel (channel_t *chan)
channelpadding_decision_t channelpadding_decide_to_pad_channel (channel_t *chan)


static uint64_t total_timers_pending
static int consensus_nf_ito_low
static int consensus_nf_ito_high
static int consensus_nf_ito_low_reduced
static int consensus_nf_ito_high_reduced
static int consensus_nf_conntimeout_relays
static int consensus_nf_conntimeout_clients
static int consensus_nf_pad_before_usage
static int consensus_nf_pad_relays
static int consensus_nf_pad_single_onion

Detailed Description

Link-level padding code.

Definition in file channelpadding.c.

Macro Definition Documentation


#define CHANNEL_IS_CLIENT (   chan,
(!public_server_mode((options)) || channel_is_client(chan) || \
int channel_is_client(const channel_t *chan)
Definition: channel.c:2918
int connection_or_digest_is_known_relay(const char *id_digest)
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43

This macro tells us if either end of the channel is connected to a client. (If we're not a server, we're definitely a client. If the channel thinks it's a client, use that. Then finally verify in the consensus).

Definition at line 82 of file channelpadding.c.



Definition at line 14 of file channelpadding.c.



Definition at line 508 of file channelpadding.c.



Calculates the number of milliseconds from now to schedule a padding cell.

Returns the number of milliseconds from now (relative) to schedule the padding callback. If the padding timer is more than 1.1 seconds in the future, we return -1, to avoid scheduling excessive callbacks. If padding is disabled in the consensus, we return -2.

Side-effects: Updates chan->next_padding_time_ms, storing an (absolute, not relative) millisecond representation of when we should send padding, unless other activity happens first. This side-effect allows us to avoid scheduling a libevent callback until we're within 1.1 seconds of the padding time.

Definition at line 507 of file channelpadding.c.



How often do we get called by the connection housekeeping (ie: once per second)

Definition at line 70 of file channelpadding.c.



Additional extra time buffer on the housekeeping callback, since it can be delayed. This extra slack is used to decide if we should schedule a timer or wait for the next callback.

Definition at line 75 of file channelpadding.c.


#define TOR_MSEC_PER_SEC   1000

Definition at line 64 of file channelpadding.c.


#define TOR_USEC_PER_MSEC   1000

Definition at line 65 of file channelpadding.c.

Function Documentation

◆ channelpadding_compute_time_until_pad_for_netflow()

STATIC int64_t channelpadding_compute_time_until_pad_for_netflow ( channel_t chan)

Definition at line 510 of file channelpadding.c.

◆ channelpadding_decide_to_pad_channel()

channelpadding_decision_t channelpadding_decide_to_pad_channel ( channel_t chan)

This function is called once per second by run_connection_housekeeping(), but only if the channel is still open, valid, and non-wedged.

It decides if and when we should send a padding cell, and if needed, schedules a callback to send that cell at the appropriate time.

Returns an enum that represents the current padding decision state. Return value is currently used only by unit tests.

Definition at line 721 of file channelpadding.c.

◆ channelpadding_disable_padding_on_channel()

void channelpadding_disable_padding_on_channel ( channel_t chan)

Calling this function on a channel causes it to tell the other side not to send padding, and disables sending padding from this side as well.

Definition at line 678 of file channelpadding.c.

◆ channelpadding_get_channel_idle_timeout()

unsigned int channelpadding_get_channel_idle_timeout ( const channel_t chan,
int  is_canonical 

Returns a randomized value for channel idle timeout in seconds. The channel idle timeout governs how quickly we close a channel after its last circuit has disappeared.

There are three classes of channels:

  1. Client+non-canonical. These live for 3-4.5 minutes
  2. relay to relay. These live for 45-75 min by default
  3. Reduced padding clients. These live for 1.5-2.25 minutes.

Also allows the default relay-to-relay value to be controlled by the consensus.

Definition at line 595 of file channelpadding.c.

◆ channelpadding_get_circuits_available_timeout()

int channelpadding_get_circuits_available_timeout ( void  )

This function controls how long we keep idle circuits open, and how long we build predicted circuits. This behavior is under the control of channelpadding because circuit availability is the dominant factor in channel lifespan, which influences total padding overhead.

Returns a randomized number of seconds in a range from CircuitsAvailableTimeout to 2*CircuitsAvailableTimeout. This value is halved if ReducedConnectionPadding is set. The default value of CircuitsAvailableTimeout can be controlled by the consensus.

Definition at line 642 of file channelpadding.c.

Referenced by rep_hist_note_used_internal().

◆ channelpadding_get_netflow_inactive_timeout_ms()

STATIC int32_t channelpadding_get_netflow_inactive_timeout_ms ( const channel_t chan)

Get a random netflow inactive timeout keepalive period in milliseconds, the range for which is determined by consensus parameters, negotiation, configuration, or default values. The consensus parameters enforce the minimum possible value, to avoid excessively frequent padding.

The ranges for this value were chosen to be low enough to ensure that routers do not emit a new netflow record for a connection due to it being idle.

Specific timeout values for major routers are listed in Proposal 251. No major router appeared capable of setting an inactive timeout below 10 seconds, so we set the defaults below that value, since we can always scale back if it ends up being too much padding.

Returns the next timeout period (in milliseconds) after which we should send a padding packet, or 0 if padding is disabled.

Definition at line 173 of file channelpadding.c.

◆ channelpadding_new_consensus_params()

void channelpadding_new_consensus_params ( const networkstatus_t ns)

This function is called to update cached consensus parameters every time there is a consensus update. This allows us to move the consensus param search off of the critical path, so it does not need to be evaluated for every single connection, every second.

Definition at line 93 of file channelpadding.c.

◆ channelpadding_reduce_padding_on_channel()

void channelpadding_reduce_padding_on_channel ( channel_t chan)

Calling this function on a channel causes it to tell the other side not to send padding, and reduces the rate that padding is sent from this side.

Definition at line 692 of file channelpadding.c.

◆ channelpadding_schedule_padding()

static channelpadding_decision_t channelpadding_schedule_padding ( channel_t chan,
int  in_ms 

Schedules a callback to send padding on a channel in_ms milliseconds from now.

Returns CHANNELPADDING_WONTPAD on error, CHANNELPADDING_PADDING_SENT if we sent the packet immediately without a timer, and CHANNELPADDING_PADDING_SCHEDULED if we decided to schedule a timer.

Definition at line 459 of file channelpadding.c.

◆ channelpadding_send_disable_command()

STATIC int channelpadding_send_disable_command ( channel_t chan)

Sends a CELL_PADDING_NEGOTIATE on the channel to tell the other side not to send padding.

Returns -1 on error, 0 on success.

Definition at line 300 of file channelpadding.c.

Referenced by channelpadding_disable_padding_on_channel(), and channelpadding_reduce_padding_on_channel().

◆ channelpadding_send_enable_command()

int channelpadding_send_enable_command ( channel_t chan,
uint16_t  low_timeout,
uint16_t  high_timeout 

Sends a CELL_PADDING_NEGOTIATE on the channel to tell the other side to resume sending padding at some rate.

Returns -1 on error, 0 on success.

Definition at line 332 of file channelpadding.c.

◆ channelpadding_send_padding_callback()

static void channelpadding_send_padding_callback ( tor_timer_t *  timer,
void *  args,
const struct monotime_t when 

tor_timer callback function for us to send padding on an idle channel.

This function just obtains the channel from the callback handle, ensures it is still valid, and then hands it off to channelpadding_send_padding_cell_for_callback(), which checks if the channel is still idle before sending padding.

Definition at line 428 of file channelpadding.c.

◆ channelpadding_send_padding_cell_for_callback()

static void channelpadding_send_padding_cell_for_callback ( channel_t chan)

Sends a CELL_PADDING cell on a channel if it has been idle since our callback was scheduled.

This function also clears the pending padding timer and the callback flags.

Definition at line 368 of file channelpadding.c.

Referenced by channelpadding_schedule_padding().

◆ channelpadding_update_padding_for_channel()

int channelpadding_update_padding_for_channel ( channel_t chan,
const channelpadding_negotiate_t *  pad_vars 

Update this channel's padding settings based on the PADDING_NEGOTIATE contents.

Returns -1 on error; 1 on success.

Definition at line 245 of file channelpadding.c.

Variable Documentation

◆ consensus_nf_conntimeout_clients

int consensus_nf_conntimeout_clients

The connection timeout for client connections

Definition at line 56 of file channelpadding.c.

Referenced by channelpadding_get_circuits_available_timeout().

◆ consensus_nf_conntimeout_relays

int consensus_nf_conntimeout_relays

The connection timeout between relays

Definition at line 54 of file channelpadding.c.

◆ consensus_nf_ito_high

int consensus_nf_ito_high

The timeout upper bound that is allowed before sending padding

Definition at line 48 of file channelpadding.c.

Referenced by channelpadding_get_netflow_inactive_timeout_ms().

◆ consensus_nf_ito_high_reduced

int consensus_nf_ito_high_reduced

The timeout upper bound that is allowed before sending reduced padding

Definition at line 52 of file channelpadding.c.

Referenced by channelpadding_reduce_padding_on_channel().

◆ consensus_nf_ito_low

int consensus_nf_ito_low

These are cached consensus parameters for netflow The timeout lower bound that is allowed before sending padding

Definition at line 46 of file channelpadding.c.

Referenced by channelpadding_get_netflow_inactive_timeout_ms(), and channelpadding_new_consensus_params().

◆ consensus_nf_ito_low_reduced

int consensus_nf_ito_low_reduced

The timeout lower bound that is allowed before sending reduced padding

Definition at line 50 of file channelpadding.c.

Referenced by channelpadding_reduce_padding_on_channel().

◆ consensus_nf_pad_before_usage

int consensus_nf_pad_before_usage

Should we pad before circuits are actually used for client data?

Definition at line 58 of file channelpadding.c.

◆ consensus_nf_pad_relays

int consensus_nf_pad_relays

Should we pad relay-to-relay connections?

Definition at line 60 of file channelpadding.c.

◆ consensus_nf_pad_single_onion

int consensus_nf_pad_single_onion

Should we pad rosos connections?

Definition at line 62 of file channelpadding.c.

◆ total_timers_pending

uint64_t total_timers_pending

The total number of pending channelpadding timers

Definition at line 42 of file channelpadding.c.