1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2015, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /* TOR_CHANNEL_INTERNAL_ define needed for an O(1) implementation of
8 * channelpadding_channel_to_channelinfo() */
9 #define TOR_CHANNEL_INTERNAL_
13 #include "channelpadding.h"
14 #include "channeltls.h"
16 #include "networkstatus.h"
17 #include "connection.h"
18 #include "connection_or.h"
22 #include "compat_time.h"
23 #include <event2/event.h>
24 #include "rendservice.h"
26 STATIC
int32_t channelpadding_get_netflow_inactive_timeout_ms(
28 STATIC
int channelpadding_send_disable_command(channel_t
*);
29 STATIC
int64_t channelpadding_compute_time_until_pad_for_netflow(channel_t
*);
31 /** The total number of pending channelpadding timers */
32 static uint64_t total_timers_pending
;
34 /** These are cached consensus parameters for netflow */
35 /** The timeout lower bound that is allowed before sending padding */
36 static int consensus_nf_ito_low
;
37 /** The timeout upper bound that is allowed before sending padding */
38 static int consensus_nf_ito_high
;
39 /** The timeout lower bound that is allowed before sending reduced padding */
40 static int consensus_nf_ito_low_reduced
;
41 /** The timeout upper bound that is allowed before sending reduced padding */
42 static int consensus_nf_ito_high_reduced
;
43 /** The connection timeout between relays */
44 static int consensus_nf_conntimeout_relays
;
45 /** The connection timeout for client connections */
46 static int consensus_nf_conntimeout_clients
;
47 /** Should we pad before circuits are actually used for client data? */
48 static int consensus_nf_pad_before_usage
;
49 /** Should we pad relay-to-relay connections? */
50 static int consensus_nf_pad_relays
;
51 /** Should we pad tor2web connections? */
52 static int consensus_nf_pad_tor2web
;
53 /** Should we pad rosos connections? */
54 static int consensus_nf_pad_single_onion
;
56 #define TOR_MSEC_PER_SEC 1000
57 #define TOR_USEC_PER_MSEC 1000
60 * How often do we get called by the connection housekeeping (ie: once
62 #define TOR_HOUSEKEEPING_CALLBACK_MSEC 1000
64 * Additional extra time buffer on the housekeeping callback, since
65 * it can be delayed. This extra slack is used to decide if we should
66 * schedule a timer or wait for the next callback. */
67 #define TOR_HOUSEKEEPING_CALLBACK_SLACK_MSEC 100
70 * This macro tells us if either end of the channel is connected to a client.
71 * (If we're not a server, we're definitely a client. If the channel thinks
72 * it's a client, use that. Then finally verify in the consensus).
74 #define CHANNEL_IS_CLIENT(chan, options) \
75 (!public_server_mode((options)) || channel_is_client(chan) || \
76 !connection_or_digest_is_known_relay((chan)->identity_digest))
79 * This function is called to update cached consensus parameters every time
80 * there is a consensus update. This allows us to move the consensus param
81 * search off of the critical path, so it does not need to be evaluated
82 * for every single connection, every second.
85 channelpadding_new_consensus_params(networkstatus_t
*ns
)
87 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_LOW 1500
88 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH 9500
89 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_MIN 0
90 #define DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX 60000
91 consensus_nf_ito_low
= networkstatus_get_param(ns
, "nf_ito_low",
92 DFLT_NETFLOW_INACTIVE_KEEPALIVE_LOW
,
93 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MIN
,
94 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX
);
95 consensus_nf_ito_high
= networkstatus_get_param(ns
, "nf_ito_high",
96 DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH
,
98 DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX
);
100 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_LOW 9000
101 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_HIGH 14000
102 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_MIN 0
103 #define DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX 60000
104 consensus_nf_ito_low_reduced
=
105 networkstatus_get_param(ns
, "nf_ito_low_reduced",
106 DFLT_NETFLOW_REDUCED_KEEPALIVE_LOW
,
107 DFLT_NETFLOW_REDUCED_KEEPALIVE_MIN
,
108 DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX
);
110 consensus_nf_ito_high_reduced
=
111 networkstatus_get_param(ns
, "nf_ito_high_reduced",
112 DFLT_NETFLOW_REDUCED_KEEPALIVE_HIGH
,
113 consensus_nf_ito_low_reduced
,
114 DFLT_NETFLOW_REDUCED_KEEPALIVE_MAX
);
116 #define CONNTIMEOUT_RELAYS_DFLT (60*60) // 1 hour
117 #define CONNTIMEOUT_RELAYS_MIN 60
118 #define CONNTIMEOUT_RELAYS_MAX (7*24*60*60) // 1 week
119 consensus_nf_conntimeout_relays
=
120 networkstatus_get_param(ns
, "nf_conntimeout_relays",
121 CONNTIMEOUT_RELAYS_DFLT
,
122 CONNTIMEOUT_RELAYS_MIN
,
123 CONNTIMEOUT_RELAYS_MAX
);
125 #define CIRCTIMEOUT_CLIENTS_DFLT (30*60) // 30 minutes
126 #define CIRCTIMEOUT_CLIENTS_MIN 60
127 #define CIRCTIMEOUT_CLIENTS_MAX (24*60*60) // 24 hours
128 consensus_nf_conntimeout_clients
=
129 networkstatus_get_param(ns
, "nf_conntimeout_clients",
130 CIRCTIMEOUT_CLIENTS_DFLT
,
131 CIRCTIMEOUT_CLIENTS_MIN
,
132 CIRCTIMEOUT_CLIENTS_MAX
);
134 consensus_nf_pad_before_usage
=
135 networkstatus_get_param(ns
, "nf_pad_before_usage", 1, 0, 1);
137 consensus_nf_pad_relays
=
138 networkstatus_get_param(ns
, "nf_pad_relays", 0, 0, 1);
140 consensus_nf_pad_tor2web
=
141 networkstatus_get_param(ns
,
142 CHANNELPADDING_TOR2WEB_PARAM
,
143 CHANNELPADDING_TOR2WEB_DEFAULT
, 0, 1);
145 consensus_nf_pad_single_onion
=
146 networkstatus_get_param(ns
,
147 CHANNELPADDING_SOS_PARAM
,
148 CHANNELPADDING_SOS_DEFAULT
, 0, 1);
152 * Get a random netflow inactive timeout keepalive period in milliseconds,
153 * the range for which is determined by consensus parameters, negotiation,
154 * configuration, or default values. The consensus parameters enforce the
155 * minimum possible value, to avoid excessively frequent padding.
157 * The ranges for this value were chosen to be low enough to ensure that
158 * routers do not emit a new netflow record for a connection due to it
161 * Specific timeout values for major routers are listed in Proposal 251.
162 * No major router appeared capable of setting an inactive timeout below 10
163 * seconds, so we set the defaults below that value, since we can always
164 * scale back if it ends up being too much padding.
166 * Returns the next timeout period (in milliseconds) after which we should
167 * send a padding packet, or 0 if padding is disabled.
170 channelpadding_get_netflow_inactive_timeout_ms(const channel_t
*chan
)
172 int low_timeout
= consensus_nf_ito_low
;
173 int high_timeout
= consensus_nf_ito_high
;
176 if (low_timeout
== 0 && low_timeout
== high_timeout
)
177 return 0; // No padding
179 /* If we have negotiated different timeout values, use those, but
180 * don't allow them to be lower than the consensus ones */
181 if (chan
->padding_timeout_low_ms
&& chan
->padding_timeout_high_ms
) {
182 low_timeout
= MAX(low_timeout
, chan
->padding_timeout_low_ms
);
183 high_timeout
= MAX(high_timeout
, chan
->padding_timeout_high_ms
);
186 if (low_timeout
== high_timeout
)
187 return low_timeout
; // No randomization
190 * This MAX() hack is here because we apply the timeout on both the client
191 * and the server. This creates the situation where the total time before
192 * sending a packet in either direction is actually
193 * min(client_timeout,server_timeout).
195 * If X is a random variable uniform from 0..R-1 (where R=high-low),
196 * then Y=max(X,X) has Prob(Y == i) = (2.0*i + 1)/(R*R).
198 * If we create a third random variable Z=min(Y,Y), then it turns out that
199 * Exp[Z] ~= Exp[X]. Here's a table:
201 * R Exp[X] Exp[Z] Exp[min(X,X)] Exp[max(X,X)]
202 * 2000 999.5 1066 666.2 1332.8
203 * 3000 1499.5 1599.5 999.5 1999.5
204 * 5000 2499.5 2666 1666.2 3332.8
205 * 6000 2999.5 3199.5 1999.5 3999.5
206 * 7000 3499.5 3732.8 2332.8 4666.2
207 * 8000 3999.5 4266.2 2666.2 5332.8
208 * 10000 4999.5 5328 3332.8 6666.2
209 * 15000 7499.5 7995 4999.5 9999.5
210 * 20000 9900.5 10661 6666.2 13332.8
212 * In other words, this hack makes it so that when both the client and
213 * the guard are sending this padding, then the averages work out closer
214 * to the midpoint of the range, making the overhead easier to tune.
215 * If only one endpoint is padding (for example: if the relay does not
216 * support padding, but the client has set ConnectionPadding 1; or
217 * if the relay does support padding, but the client has set
218 * ReducedConnectionPadding 1), then the defense will still prevent
219 * record splitting, but with less overhead than the midpoint
220 * (as seen by the Exp[max(X,X)] column).
222 * To calculate average padding packet frequency (and thus overhead),
223 * index into the table by picking a row based on R = high-low. Then,
224 * use the appropriate column (Exp[Z] for two-sided padding, and
225 * Exp[max(X,X)] for one-sided padding). Finally, take this value
226 * and add it to the low timeout value. This value is the average
227 * frequency which padding packets will be sent.
230 X1
= crypto_rand_int(high_timeout
- low_timeout
);
231 X2
= crypto_rand_int(high_timeout
- low_timeout
);
232 return low_timeout
+ MAX(X1
, X2
);
236 * Update this channel's padding settings based on the PADDING_NEGOTIATE
239 * Returns -1 on error; 1 on success.
242 channelpadding_update_padding_for_channel(channel_t
*chan
,
243 const channelpadding_negotiate_t
*pad_vars
)
245 if (pad_vars
->version
!= 0) {
246 static ratelim_t version_limit
= RATELIM_INIT(600);
248 log_fn_ratelim(&version_limit
,LOG_PROTOCOL_WARN
,LD_PROTOCOL
,
249 "Got a PADDING_NEGOTIATE cell with an unknown version. Ignoring.");
253 // We should not allow malicious relays to disable or reduce padding for
254 // us as clients. In fact, we should only accept this cell at all if we're
255 // operating as a relay. Bridges should not accept it from relays, either
256 // (only from their clients).
257 if ((get_options()->BridgeRelay
&&
258 connection_or_digest_is_known_relay(chan
->identity_digest
)) ||
259 !get_options()->ORPort_set
) {
260 static ratelim_t relay_limit
= RATELIM_INIT(600);
262 log_fn_ratelim(&relay_limit
,LOG_PROTOCOL_WARN
,LD_PROTOCOL
,
263 "Got a PADDING_NEGOTIATE from relay at %s (%s). "
264 "This should not happen.",
265 chan
->get_remote_descr(chan
, 0),
266 hex_str(chan
->identity_digest
, DIGEST_LEN
));
270 chan
->padding_enabled
= (pad_vars
->command
== CHANNELPADDING_COMMAND_START
);
272 /* Min must not be lower than the current consensus parameter
274 chan
->padding_timeout_low_ms
= MAX(consensus_nf_ito_low
,
275 pad_vars
->ito_low_ms
);
277 /* Max must not be lower than ito_low_ms */
278 chan
->padding_timeout_high_ms
= MAX(chan
->padding_timeout_low_ms
,
279 pad_vars
->ito_high_ms
);
281 log_fn(LOG_INFO
,LD_OR
,
282 "Negotiated padding=%d, lo=%d, hi=%d on "U64_FORMAT
,
283 chan
->padding_enabled
, chan
->padding_timeout_low_ms
,
284 chan
->padding_timeout_high_ms
,
285 U64_PRINTF_ARG(chan
->global_identifier
));
291 * Sends a CELL_PADDING_NEGOTIATE on the channel to tell the other side not
294 * Returns -1 on error, 0 on success.
297 channelpadding_send_disable_command(channel_t
*chan
)
299 channelpadding_negotiate_t disable
;
302 tor_assert(BASE_CHAN_TO_TLS(chan
)->conn
->link_proto
>=
303 MIN_LINK_PROTO_FOR_CHANNEL_PADDING
);
305 memset(&cell
, 0, sizeof(cell_t
));
306 memset(&disable
, 0, sizeof(channelpadding_negotiate_t
));
307 cell
.command
= CELL_PADDING_NEGOTIATE
;
309 channelpadding_negotiate_set_command(&disable
, CHANNELPADDING_COMMAND_STOP
);
311 if (channelpadding_negotiate_encode(cell
.payload
, CELL_PAYLOAD_SIZE
,
315 if (chan
->write_cell(chan
, &cell
) == 1)
322 * Sends a CELL_PADDING_NEGOTIATE on the channel to tell the other side to
323 * resume sending padding at some rate.
325 * Returns -1 on error, 0 on success.
328 channelpadding_send_enable_command(channel_t
*chan
, uint16_t low_timeout
,
329 uint16_t high_timeout
)
331 channelpadding_negotiate_t enable
;
334 tor_assert(BASE_CHAN_TO_TLS(chan
)->conn
->link_proto
>=
335 MIN_LINK_PROTO_FOR_CHANNEL_PADDING
);
337 memset(&cell
, 0, sizeof(cell_t
));
338 memset(&enable
, 0, sizeof(channelpadding_negotiate_t
));
339 cell
.command
= CELL_PADDING_NEGOTIATE
;
341 channelpadding_negotiate_set_command(&enable
, CHANNELPADDING_COMMAND_START
);
342 channelpadding_negotiate_set_ito_low_ms(&enable
, low_timeout
);
343 channelpadding_negotiate_set_ito_high_ms(&enable
, high_timeout
);
345 if (channelpadding_negotiate_encode(cell
.payload
, CELL_PAYLOAD_SIZE
,
349 if (chan
->write_cell(chan
, &cell
) == 1)
356 * Sends a CELL_PADDING cell on a channel if it has been idle since
357 * our callback was scheduled.
359 * This function also clears the pending padding timer and the callback
363 channelpadding_send_padding_cell_for_callback(channel_t
*chan
)
367 /* Check that the channel is still valid and open */
368 if (!chan
|| chan
->state
!= CHANNEL_STATE_OPEN
) {
369 if (chan
) chan
->pending_padding_callback
= 0;
370 log_fn(LOG_INFO
,LD_OR
,
371 "Scheduled a netflow padding cell, but connection already closed.");
375 /* We should have a pending callback flag set. */
376 if (BUG(chan
->pending_padding_callback
== 0))
379 chan
->pending_padding_callback
= 0;
381 if (monotime_coarse_is_zero(&chan
->next_padding_time
) ||
382 chan
->has_queued_writes(chan
)) {
383 /* We must have been active before the timer fired */
384 monotime_coarse_zero(&chan
->next_padding_time
);
389 monotime_coarse_t now
;
390 monotime_coarse_get(&now
);
392 log_fn(LOG_INFO
,LD_OR
,
393 "Sending netflow keepalive on "U64_FORMAT
" to %s (%s) after "
394 I64_FORMAT
" ms. Delta "I64_FORMAT
"ms",
395 U64_PRINTF_ARG(chan
->global_identifier
),
396 safe_str_client(chan
->get_remote_descr(chan
, 0)),
397 safe_str_client(hex_str(chan
->identity_digest
, DIGEST_LEN
)),
398 I64_PRINTF_ARG(monotime_coarse_diff_msec(&chan
->timestamp_xfer
,&now
)),
400 monotime_coarse_diff_msec(&chan
->next_padding_time
,&now
)));
403 /* Clear the timer */
404 monotime_coarse_zero(&chan
->next_padding_time
);
406 /* Send the padding cell. This will cause the channel to get a
407 * fresh timestamp_active */
408 memset(&cell
, 0, sizeof(cell
));
409 cell
.command
= CELL_PADDING
;
410 chan
->write_cell(chan
, &cell
);
414 * tor_timer callback function for us to send padding on an idle channel.
416 * This function just obtains the channel from the callback handle, ensures
417 * it is still valid, and then hands it off to
418 * channelpadding_send_padding_cell_for_callback(), which checks if
419 * the channel is still idle before sending padding.
422 channelpadding_send_padding_callback(tor_timer_t
*timer
, void *args
,
423 const struct monotime_t
*when
)
425 channel_t
*chan
= channel_handle_get((struct channel_handle_t
*)args
);
426 (void)timer
; (void)when
;
428 if (chan
&& CHANNEL_CAN_HANDLE_CELLS(chan
)) {
429 /* Hrmm.. It might be nice to have an equivalent to assert_connection_ok
430 * for channels. Then we could get rid of the channeltls dependency */
431 tor_assert(TO_CONN(BASE_CHAN_TO_TLS(chan
)->conn
)->magic
==
432 OR_CONNECTION_MAGIC
);
433 assert_connection_ok(TO_CONN(BASE_CHAN_TO_TLS(chan
)->conn
), approx_time());
435 channelpadding_send_padding_cell_for_callback(chan
);
437 log_fn(LOG_INFO
,LD_OR
,
438 "Channel closed while waiting for timer.");
441 total_timers_pending
--;
445 * Schedules a callback to send padding on a channel in_ms milliseconds from
448 * Returns CHANNELPADDING_WONTPAD on error, CHANNELPADDING_PADDING_SENT if we
449 * sent the packet immediately without a timer, and
450 * CHANNELPADDING_PADDING_SCHEDULED if we decided to schedule a timer.
452 static channelpadding_decision_t
453 channelpadding_schedule_padding(channel_t
*chan
, int in_ms
)
455 struct timeval timeout
;
456 tor_assert(!chan
->pending_padding_callback
);
459 chan
->pending_padding_callback
= 1;
460 channelpadding_send_padding_cell_for_callback(chan
);
461 return CHANNELPADDING_PADDING_SENT
;
464 timeout
.tv_sec
= in_ms
/TOR_MSEC_PER_SEC
;
465 timeout
.tv_usec
= (in_ms
%TOR_USEC_PER_MSEC
)*TOR_USEC_PER_MSEC
;
467 if (!chan
->timer_handle
) {
468 chan
->timer_handle
= channel_handle_new(chan
);
471 if (chan
->padding_timer
) {
472 timer_set_cb(chan
->padding_timer
,
473 channelpadding_send_padding_callback
,
476 chan
->padding_timer
= timer_new(channelpadding_send_padding_callback
,
479 timer_schedule(chan
->padding_timer
, &timeout
);
481 rep_hist_padding_count_timers(++total_timers_pending
);
483 chan
->pending_padding_callback
= 1;
484 return CHANNELPADDING_PADDING_SCHEDULED
;
488 * Calculates the number of milliseconds from now to schedule a padding cell.
490 * Returns the number of milliseconds from now (relative) to schedule the
491 * padding callback. If the padding timer is more than 1.1 seconds in the
492 * future, we return -1, to avoid scheduling excessive callbacks. If padding
493 * is disabled in the consensus, we return -2.
495 * Side-effects: Updates chan->next_padding_time_ms, storing an (absolute, not
496 * relative) millisecond representation of when we should send padding, unless
497 * other activity happens first. This side-effect allows us to avoid
498 * scheduling a libevent callback until we're within 1.1 seconds of the padding
501 #define CHANNELPADDING_TIME_LATER -1
502 #define CHANNELPADDING_TIME_DISABLED -2
504 channelpadding_compute_time_until_pad_for_netflow(channel_t
*chan
)
506 monotime_coarse_t now
;
507 monotime_coarse_get(&now
);
509 if (monotime_coarse_is_zero(&chan
->next_padding_time
)) {
510 /* If the below line or crypto_rand_int() shows up on a profile,
511 * we can avoid getting a timeout until we're at least nf_ito_lo
512 * from a timeout window. That will prevent us from setting timers
513 * on connections that were active up to 1.5 seconds ago.
514 * Idle connections should only call this once every 5.5s on average
515 * though, so that might be a micro-optimization for little gain. */
516 int32_t padding_timeout
=
517 channelpadding_get_netflow_inactive_timeout_ms(chan
);
519 if (!padding_timeout
)
520 return CHANNELPADDING_TIME_DISABLED
;
522 monotime_coarse_add_msec(&chan
->next_padding_time
,
523 &chan
->timestamp_xfer
,
527 const int64_t ms_till_pad
=
528 monotime_coarse_diff_msec(&now
, &chan
->next_padding_time
);
530 /* If the next padding time is beyond the maximum possible consensus value,
531 * then this indicates a clock jump, so just send padding now. This is
532 * better than using monotonic time because we want to avoid the situation
533 * where we wait around forever for monotonic time to move forward after
534 * a clock jump far into the past.
536 if (ms_till_pad
> DFLT_NETFLOW_INACTIVE_KEEPALIVE_MAX
) {
537 tor_fragile_assert();
539 "Channel padding timeout scheduled "I64_FORMAT
"ms in the future. "
540 "Did the monotonic clock just jump?",
541 I64_PRINTF_ARG(ms_till_pad
));
542 return 0; /* Clock jumped: Send padding now */
545 /* If the timeout will expire before the next time we're called (1000ms
546 from now, plus some slack), then calculate the number of milliseconds
547 from now which we should send padding, so we can schedule a callback
550 if (ms_till_pad
< (TOR_HOUSEKEEPING_CALLBACK_MSEC
+
551 TOR_HOUSEKEEPING_CALLBACK_SLACK_MSEC
)) {
552 /* If the padding time is in the past, that means that libevent delayed
553 * calling the once-per-second callback due to other work taking too long.
554 * See https://bugs.torproject.org/22212 and
555 * https://bugs.torproject.org/16585. This is a systemic problem
556 * with being single-threaded, but let's emit a notice if this
557 * is long enough in the past that we might have missed a netflow window,
558 * and allowed a router to emit a netflow frame, just so we don't forget
559 * about it entirely.. */
560 #define NETFLOW_MISSED_WINDOW (150000 - DFLT_NETFLOW_INACTIVE_KEEPALIVE_HIGH)
561 if (ms_till_pad
< 0) {
562 int severity
= (ms_till_pad
< -NETFLOW_MISSED_WINDOW
)
563 ? LOG_NOTICE
: LOG_INFO
;
564 log_fn(severity
, LD_OR
,
565 "Channel padding timeout scheduled "I64_FORMAT
"ms in the past. ",
566 I64_PRINTF_ARG(-ms_till_pad
));
567 return 0; /* Clock jumped: Send padding now */
572 return CHANNELPADDING_TIME_LATER
;
576 * Returns a randomized value for channel idle timeout in seconds.
577 * The channel idle timeout governs how quickly we close a channel
578 * after its last circuit has disappeared.
580 * There are three classes of channels:
581 * 1. Client+non-canonical. These live for 3-4.5 minutes
582 * 2. relay to relay. These live for 45-75 min by default
583 * 3. Reduced padding clients. These live for 1.5-2.25 minutes.
585 * Also allows the default relay-to-relay value to be controlled by the
589 channelpadding_get_channel_idle_timeout(const channel_t
*chan
,
592 const or_options_t
*options
= get_options();
593 unsigned int timeout
;
595 /* Non-canonical and client channels only last for 3-4.5 min when idle */
596 if (!is_canonical
|| CHANNEL_IS_CLIENT(chan
, options
)) {
597 #define CONNTIMEOUT_CLIENTS_BASE 180 // 3 to 4.5 min
598 timeout
= CONNTIMEOUT_CLIENTS_BASE
599 + crypto_rand_int(CONNTIMEOUT_CLIENTS_BASE
/2);
600 } else { // Canonical relay-to-relay channels
601 // 45..75min or consensus +/- 25%
602 timeout
= consensus_nf_conntimeout_relays
;
603 timeout
= 3*timeout
/4 + crypto_rand_int(timeout
/2);
606 /* If ReducedConnectionPadding is set, we want to halve the duration of
607 * the channel idle timeout, since reducing the additional time that
608 * a channel stays open will reduce the total overhead for making
609 * new channels. This reduction in overhead/channel expense
610 * is important for mobile users. The option cannot be set by relays.
612 * We also don't reduce any values for timeout that the user explicitly
615 if (options
->ReducedConnectionPadding
616 && !options
->CircuitsAvailableTimeout
) {
624 * This function controls how long we keep idle circuits open,
625 * and how long we build predicted circuits. This behavior is under
626 * the control of channelpadding because circuit availability is the
627 * dominant factor in channel lifespan, which influences total padding
630 * Returns a randomized number of seconds in a range from
631 * CircuitsAvailableTimeout to 2*CircuitsAvailableTimeout. This value is halved
632 * if ReducedConnectionPadding is set. The default value of
633 * CircuitsAvailableTimeout can be controlled by the consensus.
636 channelpadding_get_circuits_available_timeout(void)
638 const or_options_t
*options
= get_options();
639 int timeout
= options
->CircuitsAvailableTimeout
;
642 timeout
= consensus_nf_conntimeout_clients
;
644 /* If ReducedConnectionPadding is set, we want to halve the duration of
645 * the channel idle timeout, since reducing the additional time that
646 * a channel stays open will reduce the total overhead for making
647 * new connections. This reduction in overhead/connection expense
648 * is important for mobile users. The option cannot be set by relays.
650 * We also don't reduce any values for timeout that the user explicitly
653 if (options
->ReducedConnectionPadding
) {
654 // half the value to 15..30min by default
659 // 30..60min by default
660 timeout
= timeout
+ crypto_rand_int(timeout
);
666 * Calling this function on a channel causes it to tell the other side
667 * not to send padding, and disables sending padding from this side as well.
670 channelpadding_disable_padding_on_channel(channel_t
*chan
)
672 chan
->padding_enabled
= 0;
674 // Send cell to disable padding on the other end
675 channelpadding_send_disable_command(chan
);
679 * Calling this function on a channel causes it to tell the other side
680 * not to send padding, and reduces the rate that padding is sent from
684 channelpadding_reduce_padding_on_channel(channel_t
*chan
)
686 /* Padding can be forced and reduced by clients, regardless of if
687 * the channel supports it. So we check for support here before
688 * sending any commands. */
689 if (chan
->padding_enabled
) {
690 channelpadding_send_disable_command(chan
);
693 chan
->padding_timeout_low_ms
= consensus_nf_ito_low_reduced
;
694 chan
->padding_timeout_high_ms
= consensus_nf_ito_high_reduced
;
696 log_fn(LOG_INFO
,LD_OR
,
697 "Reduced padding on channel "U64_FORMAT
": lo=%d, hi=%d",
698 U64_PRINTF_ARG(chan
->global_identifier
),
699 chan
->padding_timeout_low_ms
, chan
->padding_timeout_high_ms
);
703 * This function is called once per second by run_connection_housekeeping(),
704 * but only if the channel is still open, valid, and non-wedged.
706 * It decides if and when we should send a padding cell, and if needed,
707 * schedules a callback to send that cell at the appropriate time.
709 * Returns an enum that represents the current padding decision state.
710 * Return value is currently used only by unit tests.
712 channelpadding_decision_t
713 channelpadding_decide_to_pad_channel(channel_t
*chan
)
715 const or_options_t
*options
= get_options();
717 /* Only pad open channels */
718 if (chan
->state
!= CHANNEL_STATE_OPEN
)
719 return CHANNELPADDING_WONTPAD
;
721 if (chan
->channel_usage
== CHANNEL_USED_FOR_FULL_CIRCS
) {
722 if (!consensus_nf_pad_before_usage
)
723 return CHANNELPADDING_WONTPAD
;
724 } else if (chan
->channel_usage
!= CHANNEL_USED_FOR_USER_TRAFFIC
) {
725 return CHANNELPADDING_WONTPAD
;
728 if (chan
->pending_padding_callback
)
729 return CHANNELPADDING_PADDING_ALREADY_SCHEDULED
;
731 /* Don't pad the channel if we didn't negotiate it, but still
732 * allow clients to force padding if options->ChannelPadding is
733 * explicitly set to 1.
735 if (!chan
->padding_enabled
&& options
->ConnectionPadding
!= 1) {
736 return CHANNELPADDING_WONTPAD
;
739 if (options
->Tor2webMode
&& !consensus_nf_pad_tor2web
) {
740 /* If the consensus just changed values, this channel may still
741 * think padding is enabled. Negotiate it off. */
742 if (chan
->padding_enabled
)
743 channelpadding_disable_padding_on_channel(chan
);
745 return CHANNELPADDING_WONTPAD
;
748 if (rend_service_allow_non_anonymous_connection(options
) &&
749 !consensus_nf_pad_single_onion
) {
750 /* If the consensus just changed values, this channel may still
751 * think padding is enabled. Negotiate it off. */
752 if (chan
->padding_enabled
)
753 channelpadding_disable_padding_on_channel(chan
);
755 return CHANNELPADDING_WONTPAD
;
758 if (!chan
->has_queued_writes(chan
)) {
759 int is_client_channel
= 0;
761 if (CHANNEL_IS_CLIENT(chan
, options
)) {
762 is_client_channel
= 1;
765 /* If nf_pad_relays=1 is set in the consensus, we pad
766 * on *all* idle connections, relay-relay or relay-client.
767 * Otherwise pad only for client+bridge cons */
768 if (is_client_channel
|| consensus_nf_pad_relays
) {
769 int64_t pad_time_ms
=
770 channelpadding_compute_time_until_pad_for_netflow(chan
);
772 if (pad_time_ms
== CHANNELPADDING_TIME_DISABLED
) {
773 return CHANNELPADDING_WONTPAD
;
774 } else if (pad_time_ms
== CHANNELPADDING_TIME_LATER
) {
775 chan
->currently_padding
= 1;
776 return CHANNELPADDING_PADLATER
;
778 if (BUG(pad_time_ms
> INT_MAX
)) {
779 pad_time_ms
= INT_MAX
;
781 /* We have to schedule a callback because we're called exactly once per
782 * second, but we don't want padding packets to go out exactly on an
783 * integer multiple of seconds. This callback will only be scheduled
784 * if we're within 1.1 seconds of the padding time.
786 chan
->currently_padding
= 1;
787 return channelpadding_schedule_padding(chan
, (int)pad_time_ms
);
790 chan
->currently_padding
= 0;
791 return CHANNELPADDING_WONTPAD
;
794 return CHANNELPADDING_PADLATER
;