/
circuitpadding.c
2712 lines (2395 loc) · 89.3 KB
/
circuitpadding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2017 The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file circuitpadding.c
* \brief Circuit-level padding implementation
*
* \details
*
* This file implements Tor proposal 254 "Padding Negotiation" which is heavily
* inspired by the paper "Toward an Efficient Website Fingerprinting Defense"
* by M. Juarez, M. Imani, M. Perry, C. Diaz, M. Wright.
*
* In particular the code in this file describes mechanisms for clients to
* negotiate various types of circuit-level padding from relays.
*
* Each padding type is described by a state machine (circpad_machine_spec_t),
* which is also referred as a "padding machine" in this file. Currently,
* these state machines are hardcoded in the source code (e.g. see
* circpad_circ_client_machine_init()), but in the future we will be able to
* serialize them in the torrc or the consensus.
*
* As specified by prop#254, clients can negotiate padding with relays by using
* PADDING_NEGOTIATE cells. After successful padding negotiation, padding
* machines are assigned to the circuit in their mutable form as a
* circpad_machine_runtime_t.
*
* Each state of a padding state machine can be either:
* - A histogram that specifies inter-arrival padding delays.
* - Or a parametrized probability distribution that specifies inter-arrival
* delays (see circpad_distribution_type_t).
*
* Padding machines start from the START state and finish with the END
* state. They can transition between states using the events in
* circpad_event_t.
*
* When a padding machine reaches the END state, it gets wiped from the circuit
* so that other padding machines can take over if needed (see
* circpad_machine_spec_transitioned_to_end()).
**/
#define CIRCUITPADDING_PRIVATE
#include <math.h>
#include "lib/math/fp.h"
#include "lib/math/prob_distr.h"
#include "core/or/or.h"
#include "core/or/circuitpadding.h"
#include "core/or/circuitpadding_machines.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/mainloop/netstatus.h"
#include "core/or/relay.h"
#include "feature/stats/rephist.h"
#include "feature/nodelist/networkstatus.h"
#include "core/or/channel.h"
#include "lib/time/compat_time.h"
#include "lib/defs/time.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "core/or/crypt_path_st.h"
#include "core/or/circuit_st.h"
#include "core/or/origin_circuit_st.h"
#include "core/or/or_circuit_st.h"
#include "feature/nodelist/routerstatus_st.h"
#include "feature/nodelist/node_st.h"
#include "core/or/cell_st.h"
#include "core/or/extend_info_st.h"
#include "core/crypto/relay_crypto.h"
#include "feature/nodelist/nodelist.h"
#include "app/config/config.h"
static inline circpad_circuit_state_t circpad_circuit_state(
origin_circuit_t *circ);
static void circpad_setup_machine_on_circ(circuit_t *on_circ,
const circpad_machine_spec_t *machine);
static double circpad_distribution_sample(circpad_distribution_t dist);
/** Cached consensus params */
static uint8_t circpad_global_max_padding_percent;
static uint16_t circpad_global_allowed_cells;
static uint16_t circpad_max_circ_queued_cells;
/** Global cell counts, for rate limiting */
static uint64_t circpad_global_padding_sent;
static uint64_t circpad_global_nonpadding_sent;
/** This is the list of circpad_machine_spec_t's parsed from consensus and
* torrc that have origin_side == 1 (ie: are for client side).
*
* The machines in this smartlist are considered immutable and they are used
* as-is by circuits so they should not change or get deallocated in Tor's
* runtime and as long as circuits are alive. */
STATIC smartlist_t *origin_padding_machines = NULL;
/** This is the list of circpad_machine_spec_t's parsed from consensus and
* torrc that have origin_side == 0 (ie: are for relay side).
*
* The machines in this smartlist are considered immutable and they are used
* as-is by circuits so they should not change or get deallocated in Tor's
* runtime and as long as circuits are alive. */
STATIC smartlist_t *relay_padding_machines = NULL;
/** Loop over the current padding state machines using <b>loop_var</b> as the
* loop variable. */
#define FOR_EACH_CIRCUIT_MACHINE_BEGIN(loop_var) \
STMT_BEGIN \
for (int loop_var = 0; loop_var < CIRCPAD_MAX_MACHINES; loop_var++) {
#define FOR_EACH_CIRCUIT_MACHINE_END } STMT_END ;
/** Loop over the current active padding state machines using <b>loop_var</b>
* as the loop variable. If a machine is not active, skip it. */
#define FOR_EACH_ACTIVE_CIRCUIT_MACHINE_BEGIN(loop_var, circ) \
FOR_EACH_CIRCUIT_MACHINE_BEGIN(loop_var) \
if (!(circ)->padding_info[loop_var]) \
continue;
#define FOR_EACH_ACTIVE_CIRCUIT_MACHINE_END } STMT_END ;
/**
* Return a human-readable description for a circuit padding state.
*/
static const char *
circpad_state_to_string(circpad_statenum_t state)
{
const char *descr;
switch (state) {
case CIRCPAD_STATE_START:
descr = "START";
break;
case CIRCPAD_STATE_BURST:
descr = "BURST";
break;
case CIRCPAD_STATE_GAP:
descr = "GAP";
break;
case CIRCPAD_STATE_END:
descr = "END";
break;
default:
descr = "CUSTOM"; // XXX: Just return # in static char buf?
}
return descr;
}
/**
* Free the machineinfo at an index
*/
static void
circpad_circuit_machineinfo_free_idx(circuit_t *circ, int idx)
{
if (circ->padding_info[idx]) {
tor_free(circ->padding_info[idx]->histogram);
timer_free(circ->padding_info[idx]->padding_timer);
tor_free(circ->padding_info[idx]);
}
}
/** Free all the machineinfos in <b>circ</b> that match <b>machine_num</b>. */
static void
free_circ_machineinfos_with_machine_num(circuit_t *circ, int machine_num)
{
FOR_EACH_CIRCUIT_MACHINE_BEGIN(i) {
if (circ->padding_machine[i] &&
circ->padding_machine[i]->machine_num == machine_num) {
circpad_circuit_machineinfo_free_idx(circ, i);
circ->padding_machine[i] = NULL;
}
} FOR_EACH_CIRCUIT_MACHINE_END;
}
/**
* Free all padding machines and mutable info associated with circuit
*/
void
circpad_circuit_free_all_machineinfos(circuit_t *circ)
{
FOR_EACH_CIRCUIT_MACHINE_BEGIN(i) {
circpad_circuit_machineinfo_free_idx(circ, i);
} FOR_EACH_CIRCUIT_MACHINE_END;
}
/**
* Allocate a new mutable machineinfo structure.
*/
STATIC circpad_machine_runtime_t *
circpad_circuit_machineinfo_new(circuit_t *on_circ, int machine_index)
{
circpad_machine_runtime_t *mi =
tor_malloc_zero(sizeof(circpad_machine_runtime_t));
mi->machine_index = machine_index;
mi->on_circ = on_circ;
return mi;
}
/**
* Return the circpad_state_t for the current state based on the
* mutable info.
*
* This function returns NULL when the machine is in the end state or in an
* invalid state.
*/
STATIC const circpad_state_t *
circpad_machine_current_state(const circpad_machine_runtime_t *mi)
{
const circpad_machine_spec_t *machine = CIRCPAD_GET_MACHINE(mi);
if (mi->current_state == CIRCPAD_STATE_END) {
return NULL;
} else if (BUG(mi->current_state >= machine->num_states)) {
log_fn(LOG_WARN,LD_CIRC,
"Invalid circuit padding state %d",
mi->current_state);
return NULL;
}
return &machine->states[mi->current_state];
}
/**
* Get the lower bound of a histogram bin.
*
* You can obtain the upper bound using histogram_get_bin_upper_bound().
*
* This function can also be called with 'bin' set to a value equal or greater
* than histogram_len in which case the infinity bin is chosen and
* CIRCPAD_DELAY_INFINITE is returned.
*/
STATIC circpad_delay_t
circpad_histogram_bin_to_usec(const circpad_machine_runtime_t *mi,
circpad_hist_index_t bin)
{
const circpad_state_t *state = circpad_machine_current_state(mi);
circpad_delay_t rtt_add_usec = 0;
/* Our state should have been checked to be non-null by the caller
* (circpad_machine_remove_token()) */
if (BUG(state == NULL)) {
return CIRCPAD_DELAY_INFINITE;
}
/* The infinity bin has an upper bound of infinity, so make sure we return
* that if they ask for it. */
if (bin > CIRCPAD_INFINITY_BIN(mi)) {
return CIRCPAD_DELAY_INFINITE;
}
/* If we are using an RTT estimate, consider it as well. */
if (state->use_rtt_estimate) {
rtt_add_usec = mi->rtt_estimate_usec;
}
return state->histogram_edges[bin] + rtt_add_usec;
}
/**
* Like circpad_histogram_bin_to_usec() but return the upper bound of bin.
* (The upper bound is included in the bin.)
*/
STATIC circpad_delay_t
histogram_get_bin_upper_bound(const circpad_machine_runtime_t *mi,
circpad_hist_index_t bin)
{
return circpad_histogram_bin_to_usec(mi, bin+1) - 1;
}
/** Return the midpoint of the histogram bin <b>bin_index</b>. */
static circpad_delay_t
circpad_get_histogram_bin_midpoint(const circpad_machine_runtime_t *mi,
int bin_index)
{
circpad_delay_t left_bound = circpad_histogram_bin_to_usec(mi, bin_index);
circpad_delay_t right_bound = histogram_get_bin_upper_bound(mi, bin_index);
return left_bound + (right_bound - left_bound)/2;
}
/**
* Return the bin that contains the usec argument.
* "Contains" is defined as us in [lower, upper).
*
* This function will never return the infinity bin (histogram_len-1), in order
* to simplify the rest of the code, so if a usec is provided that falls above
* the highest non-infinity bin, that bin index will be returned.
*/
STATIC circpad_hist_index_t
circpad_histogram_usec_to_bin(const circpad_machine_runtime_t *mi,
circpad_delay_t usec)
{
const circpad_state_t *state = circpad_machine_current_state(mi);
circpad_delay_t rtt_add_usec = 0;
circpad_hist_index_t bin;
/* Our state should have been checked to be non-null by the caller
* (circpad_machine_remove_token()) */
if (BUG(state == NULL)) {
return 0;
}
/* If we are using an RTT estimate, consider it as well. */
if (state->use_rtt_estimate) {
rtt_add_usec = mi->rtt_estimate_usec;
}
/* Walk through the bins and check the upper bound of each bin, if 'usec' is
* less-or-equal to that, return that bin. If rtt_estimate is enabled then
* add that to the upper bound of each bin.
*
* We don't want to return the infinity bin here, so don't go there. */
for (bin = 0 ; bin < CIRCPAD_INFINITY_BIN(state) ; bin++) {
if (usec <= histogram_get_bin_upper_bound(mi, bin) + rtt_add_usec) {
return bin;
}
}
/* We don't want to return the infinity bin here, so if we still didn't find
* the right bin, return the highest non-infinity bin */
return CIRCPAD_INFINITY_BIN(state)-1;
}
/**
* This function frees any token bins allocated from a previous state
*
* Called after a state transition, or if the bins are empty.
*/
STATIC void
circpad_machine_setup_tokens(circpad_machine_runtime_t *mi)
{
const circpad_state_t *state = circpad_machine_current_state(mi);
/* If this state doesn't exist, or doesn't have token removal,
* free any previous state's histogram, and bail */
if (!state || state->token_removal == CIRCPAD_TOKEN_REMOVAL_NONE) {
if (mi->histogram) {
tor_free(mi->histogram);
mi->histogram = NULL;
mi->histogram_len = 0;
}
return;
}
/* Try to avoid re-mallocing if we don't really need to */
if (!mi->histogram || (mi->histogram
&& mi->histogram_len != state->histogram_len)) {
tor_free(mi->histogram); // null ok
mi->histogram = tor_malloc_zero(sizeof(circpad_hist_token_t)
*state->histogram_len);
}
mi->histogram_len = state->histogram_len;
memcpy(mi->histogram, state->histogram,
sizeof(circpad_hist_token_t)*state->histogram_len);
}
/**
* Choose a length for this state (in cells), if specified.
*/
static void
circpad_choose_state_length(circpad_machine_runtime_t *mi)
{
const circpad_state_t *state = circpad_machine_current_state(mi);
double length;
if (!state || state->length_dist.type == CIRCPAD_DIST_NONE) {
mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
return;
}
length = circpad_distribution_sample(state->length_dist);
length = MAX(0, length);
length += state->start_length;
if (state->max_length) {
length = MIN(length, state->max_length);
}
mi->state_length = clamp_double_to_int64(length);
log_info(LD_GENERAL, "State length sampled to %"PRIu64".", mi->state_length);
}
/**
* Sample a value from our iat_dist, and clamp it safely
* to circpad_delay_t.
*
* Before returning, add <b>delay_shift</b> (can be zero) to the sampled value.
*/
static circpad_delay_t
circpad_distribution_sample_iat_delay(const circpad_state_t *state,
circpad_delay_t delay_shift)
{
double val = circpad_distribution_sample(state->iat_dist);
/* These comparisons are safe, because the output is in the range
* [0, 2**32), and double has a precision of 53 bits. */
/* We want a positive sample value */
val = MAX(0, val);
/* Respect the maximum sample setting */
val = MIN(val, state->dist_max_sample_usec);
/* Now apply the shift:
* This addition is exact: val is at most 2**32-1, delay_shift is at most
* 2**32-1, and doubles have a precision of 53 bits. */
val += delay_shift;
/* Clamp the distribution at infinite delay val */
return (circpad_delay_t)MIN(tor_llround(val), CIRCPAD_DELAY_INFINITE);
}
/**
* Sample an expected time-until-next-packet delay from the histogram.
*
* The bin is chosen with probability proportional to the number
* of tokens in each bin, and then a time value is chosen uniformly from
* that bin's [start,end) time range.
*/
STATIC circpad_delay_t
circpad_machine_sample_delay(circpad_machine_runtime_t *mi)
{
const circpad_state_t *state = circpad_machine_current_state(mi);
const circpad_hist_token_t *histogram = NULL;
circpad_hist_index_t curr_bin = 0;
circpad_delay_t bin_start, bin_end;
/* These three must all be larger than circpad_hist_token_t, because
* we sum several circpad_hist_token_t values across the histogram */
uint64_t curr_weight = 0;
uint64_t histogram_total_tokens = 0;
uint64_t bin_choice;
tor_assert(state);
if (state->iat_dist.type != CIRCPAD_DIST_NONE) {
/* Sample from a fixed IAT distribution and return */
circpad_delay_t iat_delay_shift = state->use_rtt_estimate ?
mi->rtt_estimate_usec + state->dist_added_shift_usec :
state->dist_added_shift_usec;
return circpad_distribution_sample_iat_delay(state, iat_delay_shift);
} else if (state->token_removal != CIRCPAD_TOKEN_REMOVAL_NONE) {
/* We have a mutable histogram. Do basic sanity check and apply: */
if (BUG(!mi->histogram) ||
BUG(mi->histogram_len != state->histogram_len)) {
return CIRCPAD_DELAY_INFINITE;
}
histogram = mi->histogram;
for (circpad_hist_index_t b = 0; b < state->histogram_len; b++)
histogram_total_tokens += histogram[b];
} else {
/* We have a histogram, but it's immutable */
histogram = state->histogram;
histogram_total_tokens = state->histogram_total_tokens;
}
bin_choice = crypto_fast_rng_get_uint64(get_thread_fast_rng(),
histogram_total_tokens);
/* Skip all the initial zero bins */
while (!histogram[curr_bin]) {
curr_bin++;
}
curr_weight = histogram[curr_bin];
// TODO: This is not constant-time. Pretty sure we don't
// really need it to be, though.
while (curr_weight < bin_choice) {
curr_bin++;
/* It should be impossible to run past the end of the histogram */
if (BUG(curr_bin >= state->histogram_len)) {
return CIRCPAD_DELAY_INFINITE;
}
curr_weight += histogram[curr_bin];
}
/* Do some basic checking of the current bin we are in */
if (BUG(curr_bin >= state->histogram_len) ||
BUG(histogram[curr_bin] == 0)) {
return CIRCPAD_DELAY_INFINITE;
}
// Store this index to remove the token upon callback.
if (state->token_removal != CIRCPAD_TOKEN_REMOVAL_NONE) {
mi->chosen_bin = curr_bin;
}
if (curr_bin >= CIRCPAD_INFINITY_BIN(state)) {
if (state->token_removal != CIRCPAD_TOKEN_REMOVAL_NONE &&
mi->histogram[curr_bin] > 0) {
mi->histogram[curr_bin]--;
}
// Infinity: Don't send a padding packet. Wait for a real packet
// and then see if our bins are empty or what else we should do.
return CIRCPAD_DELAY_INFINITE;
}
tor_assert(curr_bin < CIRCPAD_INFINITY_BIN(state));
bin_start = circpad_histogram_bin_to_usec(mi, curr_bin);
/* We don't need to reduct 1 from the upper bound because the random range
* function below samples from [bin_start, bin_end) */
bin_end = circpad_histogram_bin_to_usec(mi, curr_bin+1);
/* Bin edges are monotonically increasing so this is a bug. Handle it. */
if (BUG(bin_start >= bin_end)) {
return bin_start;
}
return (circpad_delay_t)crypto_fast_rng_uint64_range(get_thread_fast_rng(),
bin_start, bin_end);
}
/**
* Sample a value from the specified probability distribution.
*
* This performs inverse transform sampling
* (https://en.wikipedia.org/wiki/Inverse_transform_sampling).
*
* XXX: These formulas were taken verbatim. Need a floating wizard
* to check them for catastropic cancellation and other issues (teor?).
* Also: is 32bits of double from [0.0,1.0) enough?
*/
static double
circpad_distribution_sample(circpad_distribution_t dist)
{
log_fn(LOG_DEBUG,LD_CIRC, "Sampling delay with distribution %d",
dist.type);
switch (dist.type) {
case CIRCPAD_DIST_NONE:
{
/* We should not get in here like this */
tor_assert_nonfatal_unreached();
return 0;
}
case CIRCPAD_DIST_UNIFORM:
{
// param2 is upper bound, param1 is lower
const struct uniform my_uniform = {
.base = UNIFORM(my_uniform),
.a = dist.param1,
.b = dist.param2,
};
return dist_sample(&my_uniform.base);
}
case CIRCPAD_DIST_LOGISTIC:
{
/* param1 is Mu, param2 is sigma. */
const struct logistic my_logistic = {
.base = LOGISTIC(my_logistic),
.mu = dist.param1,
.sigma = dist.param2,
};
return dist_sample(&my_logistic.base);
}
case CIRCPAD_DIST_LOG_LOGISTIC:
{
/* param1 is Alpha, param2 is 1.0/Beta */
const struct log_logistic my_log_logistic = {
.base = LOG_LOGISTIC(my_log_logistic),
.alpha = dist.param1,
.beta = dist.param2,
};
return dist_sample(&my_log_logistic.base);
}
case CIRCPAD_DIST_GEOMETRIC:
{
/* param1 is 'p' (success probability) */
const struct geometric my_geometric = {
.base = GEOMETRIC(my_geometric),
.p = dist.param1,
};
return dist_sample(&my_geometric.base);
}
case CIRCPAD_DIST_WEIBULL:
{
/* param1 is k, param2 is Lambda */
const struct weibull my_weibull = {
.base = WEIBULL(my_weibull),
.k = dist.param1,
.lambda = dist.param2,
};
return dist_sample(&my_weibull.base);
}
case CIRCPAD_DIST_PARETO:
{
/* param1 is sigma, param2 is xi, no more params for mu so we use 0 */
const struct genpareto my_genpareto = {
.base = GENPARETO(my_genpareto),
.mu = 0,
.sigma = dist.param1,
.xi = dist.param2,
};
return dist_sample(&my_genpareto.base);
}
}
tor_assert_nonfatal_unreached();
return 0;
}
/**
* Find the index of the first bin whose upper bound is
* greater than the target, and that has tokens remaining.
*/
static circpad_hist_index_t
circpad_machine_first_higher_index(const circpad_machine_runtime_t *mi,
circpad_delay_t target_bin_usec)
{
circpad_hist_index_t bin = circpad_histogram_usec_to_bin(mi,
target_bin_usec);
/* Don't remove from the infinity bin */
for (; bin < CIRCPAD_INFINITY_BIN(mi); bin++) {
if (mi->histogram[bin] &&
histogram_get_bin_upper_bound(mi, bin) >= target_bin_usec) {
return bin;
}
}
return mi->histogram_len;
}
/**
* Find the index of the first bin whose lower bound is lower or equal to
* <b>target_bin_usec</b>, and that still has tokens remaining.
*/
static circpad_hist_index_t
circpad_machine_first_lower_index(const circpad_machine_runtime_t *mi,
circpad_delay_t target_bin_usec)
{
circpad_hist_index_t bin = circpad_histogram_usec_to_bin(mi,
target_bin_usec);
for (; bin >= 0; bin--) {
if (mi->histogram[bin] &&
circpad_histogram_bin_to_usec(mi, bin) <= target_bin_usec) {
return bin;
}
}
return -1;
}
/**
* Remove a token from the first non-empty bin whose upper bound is
* greater than the target.
*/
STATIC void
circpad_machine_remove_higher_token(circpad_machine_runtime_t *mi,
circpad_delay_t target_bin_usec)
{
/* We need to remove the token from the first bin
* whose upper bound is greater than the target, and that
* has tokens remaining. */
circpad_hist_index_t bin = circpad_machine_first_higher_index(mi,
target_bin_usec);
if (bin >= 0 && bin < CIRCPAD_INFINITY_BIN(mi)) {
if (!BUG(mi->histogram[bin] == 0)) {
mi->histogram[bin]--;
}
}
}
/**
* Remove a token from the first non-empty bin whose upper bound is
* lower than the target.
*/
STATIC void
circpad_machine_remove_lower_token(circpad_machine_runtime_t *mi,
circpad_delay_t target_bin_usec)
{
circpad_hist_index_t bin = circpad_machine_first_lower_index(mi,
target_bin_usec);
if (bin >= 0 && bin < CIRCPAD_INFINITY_BIN(mi)) {
if (!BUG(mi->histogram[bin] == 0)) {
mi->histogram[bin]--;
}
}
}
/* Helper macro: Ensure that the bin has tokens available, and BUG out of the
* function if it's not the case. */
#define ENSURE_BIN_CAPACITY(bin_index) \
if (BUG(mi->histogram[bin_index] == 0)) { \
return; \
}
/**
* Remove a token from the closest non-empty bin to the target.
*
* If use_usec is true, measure "closest" in terms of the next closest bin
* midpoint.
*
* If it is false, use bin index distance only.
*/
STATIC void
circpad_machine_remove_closest_token(circpad_machine_runtime_t *mi,
circpad_delay_t target_bin_usec,
bool use_usec)
{
circpad_hist_index_t lower, higher, current;
circpad_hist_index_t bin_to_remove = -1;
lower = circpad_machine_first_lower_index(mi, target_bin_usec);
higher = circpad_machine_first_higher_index(mi, target_bin_usec);
current = circpad_histogram_usec_to_bin(mi, target_bin_usec);
/* Sanity check the results */
if (BUG(lower > current) || BUG(higher < current)) {
return;
}
/* Take care of edge cases first */
if (higher == mi->histogram_len && lower == -1) {
/* All bins are empty */
return;
} else if (higher == mi->histogram_len) {
/* All higher bins are empty */
ENSURE_BIN_CAPACITY(lower);
mi->histogram[lower]--;
return;
} else if (lower == -1) {
/* All lower bins are empty */
ENSURE_BIN_CAPACITY(higher);
mi->histogram[higher]--;
return;
}
/* Now handle the intermediate cases */
if (use_usec) {
/* Find the closest bin midpoint to the target */
circpad_delay_t lower_usec = circpad_get_histogram_bin_midpoint(mi, lower);
circpad_delay_t higher_usec =
circpad_get_histogram_bin_midpoint(mi, higher);
if (target_bin_usec < lower_usec) {
// Lower bin is closer
ENSURE_BIN_CAPACITY(lower);
bin_to_remove = lower;
} else if (target_bin_usec > higher_usec) {
// Higher bin is closer
ENSURE_BIN_CAPACITY(higher);
bin_to_remove = higher;
} else if (target_bin_usec-lower_usec > higher_usec-target_bin_usec) {
// Higher bin is closer
ENSURE_BIN_CAPACITY(higher);
bin_to_remove = higher;
} else {
// Lower bin is closer
ENSURE_BIN_CAPACITY(lower);
bin_to_remove = lower;
}
mi->histogram[bin_to_remove]--;
log_debug(LD_GENERAL, "Removing token from bin %d", bin_to_remove);
return;
} else {
if (current - lower > higher - current) {
// Higher bin is closer
ENSURE_BIN_CAPACITY(higher);
mi->histogram[higher]--;
return;
} else {
// Lower bin is closer
ENSURE_BIN_CAPACITY(lower);
mi->histogram[lower]--;
return;
}
}
}
#undef ENSURE_BIN_CAPACITY
/**
* Remove a token from the exact bin corresponding to the target.
*
* If it is empty, do nothing.
*/
static void
circpad_machine_remove_exact(circpad_machine_runtime_t *mi,
circpad_delay_t target_bin_usec)
{
circpad_hist_index_t bin = circpad_histogram_usec_to_bin(mi,
target_bin_usec);
if (mi->histogram[bin] > 0)
mi->histogram[bin]--;
}
/**
* Check our state's cell limit count and tokens.
*
* Returns 1 if either limits are hit and we decide to change states,
* otherwise returns 0.
*/
static circpad_decision_t
check_machine_token_supply(circpad_machine_runtime_t *mi)
{
uint32_t histogram_total_tokens = 0;
/* Check if bins empty. This requires summing up the current mutable
* machineinfo histogram token total and checking if it is zero.
* Machineinfo does not keep a running token count. We're assuming the
* extra space is not worth this short loop iteration.
*
* We also do not count infinity bin in histogram totals.
*/
if (mi->histogram_len && mi->histogram) {
for (circpad_hist_index_t b = 0; b < CIRCPAD_INFINITY_BIN(mi); b++)
histogram_total_tokens += mi->histogram[b];
/* If we change state, we're done */
if (histogram_total_tokens == 0) {
if (circpad_internal_event_bins_empty(mi) == CIRCPAD_STATE_CHANGED)
return CIRCPAD_STATE_CHANGED;
}
}
if (mi->state_length == 0) {
return circpad_internal_event_state_length_up(mi);
}
return CIRCPAD_STATE_UNCHANGED;
}
/**
* Remove a token from the bin corresponding to the delta since
* last packet. If that bin is empty, choose a token based on
* the specified removal strategy in the state machine.
*
* This function also updates and checks rate limit and state
* limit counters.
*
* Returns 1 if we transition states, 0 otherwise.
*/
STATIC circpad_decision_t
circpad_machine_remove_token(circpad_machine_runtime_t *mi)
{
const circpad_state_t *state = NULL;
circpad_time_t current_time;
circpad_delay_t target_bin_usec;
/* Update non-padding counts for rate limiting: We scale at UINT16_MAX
* because we only use this for a percentile limit of 2 sig figs, and
* space is scare in the machineinfo struct. */
mi->nonpadding_sent++;
if (mi->nonpadding_sent == UINT16_MAX) {
mi->padding_sent /= 2;
mi->nonpadding_sent /= 2;
}
/* Dont remove any tokens if there was no padding scheduled */
if (!mi->padding_scheduled_at_usec) {
return CIRCPAD_STATE_UNCHANGED;
}
state = circpad_machine_current_state(mi);
current_time = monotime_absolute_usec();
/* If we have scheduled padding some time in the future, we want to see what
bin we are in at the current time */
target_bin_usec = (circpad_delay_t)
MIN((current_time - mi->padding_scheduled_at_usec),
CIRCPAD_DELAY_INFINITE-1);
/* We are treating this non-padding cell as a padding cell, so we cancel
padding timer, if present. */
mi->padding_scheduled_at_usec = 0;
if (mi->is_padding_timer_scheduled) {
mi->is_padding_timer_scheduled = 0;
timer_disable(mi->padding_timer);
}
/* If we are not in a padding state (like start or end), we're done */
if (!state)
return CIRCPAD_STATE_UNCHANGED;
/* If we're enforcing a state length on non-padding packets,
* decrement it */
if (mi->state_length != CIRCPAD_STATE_LENGTH_INFINITE &&
state->length_includes_nonpadding &&
mi->state_length > 0) {
mi->state_length--;
}
/* Perform the specified token removal strategy */
switch (state->token_removal) {
case CIRCPAD_TOKEN_REMOVAL_NONE:
break;
case CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC:
circpad_machine_remove_closest_token(mi, target_bin_usec, 1);
break;
case CIRCPAD_TOKEN_REMOVAL_CLOSEST:
circpad_machine_remove_closest_token(mi, target_bin_usec, 0);
break;
case CIRCPAD_TOKEN_REMOVAL_LOWER:
circpad_machine_remove_lower_token(mi, target_bin_usec);
break;
case CIRCPAD_TOKEN_REMOVAL_HIGHER:
circpad_machine_remove_higher_token(mi, target_bin_usec);
break;
case CIRCPAD_TOKEN_REMOVAL_EXACT:
circpad_machine_remove_exact(mi, target_bin_usec);
break;
}
/* Check our token and state length limits */
return check_machine_token_supply(mi);
}
/**
* Send a relay command with a relay cell payload on a circuit to
* the particular hopnum.
*
* Hopnum starts at 1 (1=guard, 2=middle, 3=exit, etc).
*
* Payload may be null.
*
* Returns negative on error, 0 on success.
*/
MOCK_IMPL(STATIC signed_error_t,
circpad_send_command_to_hop,(origin_circuit_t *circ, uint8_t hopnum,
uint8_t relay_command, const uint8_t *payload,
ssize_t payload_len))
{
crypt_path_t *target_hop = circuit_get_cpath_hop(circ, hopnum);
signed_error_t ret;
/* Check that the cpath has the target hop */
if (!target_hop) {
log_fn(LOG_WARN, LD_BUG, "Padding circuit %u has %d hops, not %d",
circ->global_identifier, circuit_get_cpath_len(circ), hopnum);
return -1;
}
/* Check that the target hop is opened */
if (target_hop->state != CPATH_STATE_OPEN) {
log_fn(LOG_WARN,LD_CIRC,
"Padding circuit %u has %d hops, not %d",
circ->global_identifier,
circuit_get_cpath_opened_len(circ), hopnum);
return -1;
}
/* Send the drop command to the second hop */
ret = relay_send_command_from_edge(0, TO_CIRCUIT(circ), relay_command,
(const char*)payload, payload_len,
target_hop);
return ret;
}
/**
* Callback helper to send a padding cell.
*
* This helper is called after our histogram-sampled delay period passes
* without another packet being sent first. If a packet is sent before this
* callback happens, it is canceled. So when we're called here, send padding
* right away.
*
* If sending this padding cell forced us to transition states return
* CIRCPAD_STATE_CHANGED. Otherwise return CIRCPAD_STATE_UNCHANGED.
*/
circpad_decision_t
circpad_send_padding_cell_for_callback(circpad_machine_runtime_t *mi)
{
circuit_t *circ = mi->on_circ;
int machine_idx = mi->machine_index;
mi->padding_scheduled_at_usec = 0;
circpad_statenum_t state = mi->current_state;
/* Make sure circuit didn't close on us */
if (mi->on_circ->marked_for_close) {
log_fn(LOG_INFO,LD_CIRC,
"Padding callback on a circuit marked for close. Ignoring.");
return CIRCPAD_STATE_CHANGED;
}
/* If it's a histogram, reduce the token count */
if (mi->histogram && mi->histogram_len) {
/* Basic sanity check on the histogram before removing anything */
if (BUG(mi->chosen_bin >= mi->histogram_len) ||
BUG(mi->histogram[mi->chosen_bin] == 0)) {
return CIRCPAD_STATE_CHANGED;
}
mi->histogram[mi->chosen_bin]--;
log_debug(LD_GENERAL, "Now %u tokens on bin %u",
mi->histogram[mi->chosen_bin], mi->chosen_bin);
}
/* If we have a valid state length bound, consider it */
if (mi->state_length != CIRCPAD_STATE_LENGTH_INFINITE &&
!BUG(mi->state_length <= 0)) {
mi->state_length--;