aboutsummaryrefslogtreecommitdiff
path: root/src/core/or/dos.c
blob: b789f87aaea39ffeeeaade49a9ff037596db2ca8 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
/* Copyright (c) 2018-2021, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/*
 * \file dos.c
 * \brief Implement Denial of Service mitigation subsystem.
 */

#define DOS_PRIVATE

#include "core/or/or.h"
#include "app/config/config.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/mainloop.h"
#include "core/or/channel.h"
#include "core/or/connection_or.h"
#include "core/or/relay.h"
#include "feature/hs/hs_dos.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
#include "feature/relay/routermode.h"
#include "feature/stats/geoip_stats.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/time/compat_time.h"

#include "core/or/dos.h"
#include "core/or/dos_sys.h"

#include "core/or/dos_options_st.h"
#include "core/or/or_connection_st.h"

/*
 * Circuit creation denial of service mitigation.
 *
 * Namespace used for this mitigation framework is "dos_cc_" where "cc" is for
 * Circuit Creation.
 */

/* Is the circuit creation DoS mitigation enabled? */
static unsigned int dos_cc_enabled = 0;

/* Consensus parameters. They can be changed when a new consensus arrives.
 * They are initialized with the hardcoded default values. */
static uint32_t dos_cc_min_concurrent_conn;
static uint32_t dos_cc_circuit_rate;
static uint32_t dos_cc_circuit_burst;
static dos_cc_defense_type_t dos_cc_defense_type;
static int32_t dos_cc_defense_time_period;

/* Keep some stats for the heartbeat so we can report out. */
static uint64_t cc_num_rejected_cells;
static uint32_t cc_num_marked_addrs;
static uint32_t cc_num_marked_addrs_max_queue;

/*
 * Concurrent connection denial of service mitigation.
 *
 * Namespace used for this mitigation framework is "dos_conn_".
 */

/* Is the connection DoS mitigation enabled? */
static unsigned int dos_conn_enabled = 0;

/* Consensus parameters. They can be changed when a new consensus arrives.
 * They are initialized with the hardcoded default values. */
static uint32_t dos_conn_max_concurrent_count;
static dos_conn_defense_type_t dos_conn_defense_type;
static uint32_t dos_conn_connect_rate = DOS_CONN_CONNECT_RATE_DEFAULT;
static uint32_t dos_conn_connect_burst = DOS_CONN_CONNECT_BURST_DEFAULT;
static int32_t dos_conn_connect_defense_time_period =
  DOS_CONN_CONNECT_DEFENSE_TIME_PERIOD_DEFAULT;

/* Keep some stats for the heartbeat so we can report out. */
static uint64_t conn_num_addr_rejected;
static uint64_t conn_num_addr_connect_rejected;

/** Consensus parameter: How many times a client IP is allowed to hit the
 * circ_max_cell_queue_size_out limit before being marked. */
static uint32_t dos_num_circ_max_outq;

/*
 * Stream denial of service mitigation.
 *
 * Namespace used for this mitigation framework is "dos_stream_".
 */

/* Is the connection DoS mitigation enabled? */
static unsigned int dos_stream_enabled = 0;

/* Consensus parameters. They can be changed when a new consensus arrives.
 * They are initialized with the hardcoded default values. */
static dos_stream_defense_type_t dos_stream_defense_type;
static uint32_t dos_stream_rate = DOS_STREAM_RATE_DEFAULT;
static uint32_t dos_stream_burst = DOS_STREAM_BURST_DEFAULT;

/* Keep some stats for the heartbeat so we can report out. */
static uint64_t stream_num_rejected;

/*
 * General interface of the denial of service mitigation subsystem.
 */

/* Keep stats for the heartbeat. */
static uint64_t num_single_hop_client_refused;

/** Return the consensus parameter for the outbound circ_max_cell_queue_size
 * limit. */
static uint32_t
get_param_dos_num_circ_max_outq(const networkstatus_t *ns)
{
#define DOS_NUM_CIRC_MAX_OUTQ_DEFAULT 3
#define DOS_NUM_CIRC_MAX_OUTQ_MIN 0
#define DOS_NUM_CIRC_MAX_OUTQ_MAX INT32_MAX

  /* Update the circuit max cell queue size from the consensus. */
  return networkstatus_get_param(ns, "dos_num_circ_max_outq",
                                 DOS_NUM_CIRC_MAX_OUTQ_DEFAULT,
                                 DOS_NUM_CIRC_MAX_OUTQ_MIN,
                                 DOS_NUM_CIRC_MAX_OUTQ_MAX);
}

/* Return true iff the circuit creation mitigation is enabled. We look at the
 * consensus for this else a default value is returned. */
MOCK_IMPL(STATIC unsigned int,
get_param_cc_enabled, (const networkstatus_t *ns))
{
  if (dos_get_options()->DoSCircuitCreationEnabled != -1) {
    return dos_get_options()->DoSCircuitCreationEnabled;
  }

  return !!networkstatus_get_param(ns, "DoSCircuitCreationEnabled",
                                   DOS_CC_ENABLED_DEFAULT, 0, 1);
}

/* Return the parameter for the minimum concurrent connection at which we'll
 * start counting circuit for a specific client address. */
STATIC uint32_t
get_param_cc_min_concurrent_connection(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSCircuitCreationMinConnections) {
    return dos_get_options()->DoSCircuitCreationMinConnections;
  }
  return networkstatus_get_param(ns, "DoSCircuitCreationMinConnections",
                                 DOS_CC_MIN_CONCURRENT_CONN_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the parameter for the time rate that is how many circuits over this
 * time span. */
static uint32_t
get_param_cc_circuit_rate(const networkstatus_t *ns)
{
  /* This is in seconds. */
  if (dos_get_options()->DoSCircuitCreationRate) {
    return dos_get_options()->DoSCircuitCreationRate;
  }
  return networkstatus_get_param(ns, "DoSCircuitCreationRate",
                                 DOS_CC_CIRCUIT_RATE_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the parameter for the maximum circuit count for the circuit time
 * rate. */
STATIC uint32_t
get_param_cc_circuit_burst(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSCircuitCreationBurst) {
    return dos_get_options()->DoSCircuitCreationBurst;
  }
  return networkstatus_get_param(ns, "DoSCircuitCreationBurst",
                                 DOS_CC_CIRCUIT_BURST_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the consensus parameter of the circuit creation defense type. */
static uint32_t
get_param_cc_defense_type(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSCircuitCreationDefenseType) {
    return dos_get_options()->DoSCircuitCreationDefenseType;
  }
  return networkstatus_get_param(ns, "DoSCircuitCreationDefenseType",
                                 DOS_CC_DEFENSE_TYPE_DEFAULT,
                                 DOS_CC_DEFENSE_NONE, DOS_CC_DEFENSE_MAX);
}

/* Return the consensus parameter of the defense time period which is how much
 * time should we defend against a malicious client address. */
static int32_t
get_param_cc_defense_time_period(const networkstatus_t *ns)
{
  /* Time in seconds. */
  if (dos_get_options()->DoSCircuitCreationDefenseTimePeriod) {
    return dos_get_options()->DoSCircuitCreationDefenseTimePeriod;
  }
  return networkstatus_get_param(ns, "DoSCircuitCreationDefenseTimePeriod",
                                 DOS_CC_DEFENSE_TIME_PERIOD_DEFAULT,
                                 0, INT32_MAX);
}

/* Return true iff connection mitigation is enabled. We look at the consensus
 * for this else a default value is returned. */
MOCK_IMPL(STATIC unsigned int,
get_param_conn_enabled, (const networkstatus_t *ns))
{
  if (dos_get_options()->DoSConnectionEnabled != -1) {
    return dos_get_options()->DoSConnectionEnabled;
  }
  return !!networkstatus_get_param(ns, "DoSConnectionEnabled",
                                   DOS_CONN_ENABLED_DEFAULT, 0, 1);
}

/* Return the consensus parameter for the maximum concurrent connection
 * allowed. */
STATIC uint32_t
get_param_conn_max_concurrent_count(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSConnectionMaxConcurrentCount) {
    return dos_get_options()->DoSConnectionMaxConcurrentCount;
  }
  return networkstatus_get_param(ns, "DoSConnectionMaxConcurrentCount",
                                 DOS_CONN_MAX_CONCURRENT_COUNT_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the consensus parameter of the connection defense type. */
static uint32_t
get_param_conn_defense_type(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSConnectionDefenseType) {
    return dos_get_options()->DoSConnectionDefenseType;
  }
  return networkstatus_get_param(ns, "DoSConnectionDefenseType",
                                 DOS_CONN_DEFENSE_TYPE_DEFAULT,
                                 DOS_CONN_DEFENSE_NONE, DOS_CONN_DEFENSE_MAX);
}

/* Return the connection connect rate parameters either from the configuration
 * file or, if not found, consensus parameter. */
static uint32_t
get_param_conn_connect_rate(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSConnectionConnectRate) {
    return dos_get_options()->DoSConnectionConnectRate;
  }
  return networkstatus_get_param(ns, "DoSConnectionConnectRate",
                                 DOS_CONN_CONNECT_RATE_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the connection connect burst parameters either from the
 * configuration file or, if not found, consensus parameter. */
STATIC uint32_t
get_param_conn_connect_burst(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSConnectionConnectBurst) {
    return dos_get_options()->DoSConnectionConnectBurst;
  }
  return networkstatus_get_param(ns, "DoSConnectionConnectBurst",
                                 DOS_CONN_CONNECT_BURST_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the connection connect defense time period from the configuration
 * file or, if not found, the consensus parameter. */
static int32_t
get_param_conn_connect_defense_time_period(const networkstatus_t *ns)
{
  /* Time in seconds. */
  if (dos_get_options()->DoSConnectionConnectDefenseTimePeriod) {
    return dos_get_options()->DoSConnectionConnectDefenseTimePeriod;
  }
  return networkstatus_get_param(ns, "DoSConnectionConnectDefenseTimePeriod",
                                 DOS_CONN_CONNECT_DEFENSE_TIME_PERIOD_DEFAULT,
                                 DOS_CONN_CONNECT_DEFENSE_TIME_PERIOD_MIN,
                                 INT32_MAX);
}

/* Return true iff the stream creation mitigation is enabled. We look at the
 * consensus for this else a default value is returned. */
MOCK_IMPL(STATIC unsigned int,
get_param_stream_enabled, (const networkstatus_t *ns))
{
  if (dos_get_options()->DoSStreamCreationEnabled != -1) {
    return dos_get_options()->DoSStreamCreationEnabled;
  }

  return !!networkstatus_get_param(ns, "DoSStreamCreationEnabled",
                                   DOS_STREAM_ENABLED_DEFAULT, 0, 1);
}

/* Return the parameter for the time rate that is how many stream per circuit
 * over this time span. */
static uint32_t
get_param_stream_rate(const networkstatus_t *ns)
{
  /* This is in seconds. */
  if (dos_get_options()->DoSStreamCreationRate) {
    return dos_get_options()->DoSStreamCreationRate;
  }
  return networkstatus_get_param(ns, "DoSStreamCreationRate",
                                 DOS_STREAM_RATE_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the parameter for the maximum circuit count for the circuit time
 * rate. */
static uint32_t
get_param_stream_burst(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSStreamCreationBurst) {
    return dos_get_options()->DoSStreamCreationBurst;
  }
  return networkstatus_get_param(ns, "DoSStreamCreationBurst",
                                 DOS_STREAM_BURST_DEFAULT,
                                 1, INT32_MAX);
}

/* Return the consensus parameter of the circuit creation defense type. */
static uint32_t
get_param_stream_defense_type(const networkstatus_t *ns)
{
  if (dos_get_options()->DoSStreamCreationDefenseType) {
    return dos_get_options()->DoSStreamCreationDefenseType;
  }
  return networkstatus_get_param(ns, "DoSStreamCreationDefenseType",
                                 DOS_STREAM_DEFENSE_TYPE_DEFAULT,
                                 DOS_STREAM_DEFENSE_NONE,
                                 DOS_STREAM_DEFENSE_MAX);
}

/* Set circuit creation parameters located in the consensus or their default
 * if none are present. Called at initialization or when the consensus
 * changes. */
static void
set_dos_parameters(const networkstatus_t *ns)
{
  /* Get the default consensus param values. */
  dos_cc_enabled = get_param_cc_enabled(ns);
  dos_cc_min_concurrent_conn = get_param_cc_min_concurrent_connection(ns);
  dos_cc_circuit_rate = get_param_cc_circuit_rate(ns);
  dos_cc_circuit_burst = get_param_cc_circuit_burst(ns);
  dos_cc_defense_time_period = get_param_cc_defense_time_period(ns);
  dos_cc_defense_type = get_param_cc_defense_type(ns);

  /* Connection detection. */
  dos_conn_enabled = get_param_conn_enabled(ns);
  dos_conn_max_concurrent_count = get_param_conn_max_concurrent_count(ns);
  dos_conn_defense_type = get_param_conn_defense_type(ns);
  dos_conn_connect_rate = get_param_conn_connect_rate(ns);
  dos_conn_connect_burst = get_param_conn_connect_burst(ns);
  dos_conn_connect_defense_time_period =
    get_param_conn_connect_defense_time_period(ns);

  /* Circuit. */
  dos_num_circ_max_outq = get_param_dos_num_circ_max_outq(ns);

  /* Stream. */
  dos_stream_enabled = get_param_stream_enabled(ns);
  dos_stream_defense_type = get_param_stream_defense_type(ns);
  dos_stream_rate = get_param_stream_rate(ns);
  dos_stream_burst = get_param_stream_burst(ns);
}

/* Free everything for the circuit creation DoS mitigation subsystem. */
static void
cc_free_all(void)
{
  /* If everything is freed, the circuit creation subsystem is not enabled. */
  dos_cc_enabled = 0;
}

/* Called when the consensus has changed. Do appropriate actions for the
 * circuit creation subsystem. */
static void
cc_consensus_has_changed(const networkstatus_t *ns)
{
  /* Looking at the consensus, is the circuit creation subsystem enabled? If
   * not and it was enabled before, clean it up. */
  if (dos_cc_enabled && !get_param_cc_enabled(ns)) {
    cc_free_all();
  }
}

/** Return the number of circuits we allow per second under the current
 *  configuration. */
STATIC uint64_t
get_circuit_rate_per_second(void)
{
  return dos_cc_circuit_rate;
}

/* Given the circuit creation client statistics object, refill the circuit
 * bucket if needed. This also works if the bucket was never filled in the
 * first place. The addr is only used for logging purposes. */
STATIC void
cc_stats_refill_bucket(cc_client_stats_t *stats, const tor_addr_t *addr)
{
  uint32_t new_circuit_bucket_count;
  uint64_t num_token, elapsed_time_last_refill = 0, circuit_rate = 0;
  time_t now;
  int64_t last_refill_ts;

  tor_assert(stats);
  tor_assert(addr);

  now = approx_time();
  last_refill_ts = (int64_t)stats->last_circ_bucket_refill_ts;

  /* If less than a second has elapsed, don't add any tokens.
   * Note: If a relay's clock is ever 0, any new clients won't get a refill
   * until the next second. But a relay that thinks it is 1970 will never
   * validate the public consensus. */
  if ((int64_t)now == last_refill_ts) {
    goto done;
  }

  /* At this point, we know we might need to add token to the bucket. We'll
   * first get the circuit rate that is how many circuit are we allowed to do
   * per second. */
  circuit_rate = get_circuit_rate_per_second();

  /* We've never filled the bucket so fill it with the maximum being the burst
   * and we are done.
   * Note: If a relay's clock is ever 0, all clients that were last refilled
   * in that zero second will get a full refill here. */
  if (last_refill_ts == 0) {
    num_token = dos_cc_circuit_burst;
    goto end;
  }

  /* Our clock jumped backward so fill it up to the maximum. Not filling it
   * could trigger a detection for a valid client. Also, if the clock jumped
   * negative but we didn't notice until the elapsed time became positive
   * again, then we potentially spent many seconds not refilling the bucket
   * when we should have been refilling it. But the fact that we didn't notice
   * until now means that no circuit creation requests came in during that
   * time, so the client doesn't end up punished that much from this hopefully
   * rare situation.*/
  if ((int64_t)now < last_refill_ts) {
    /* Use the maximum allowed value of token. */
    num_token = dos_cc_circuit_burst;
    goto end;
  }

  /* How many seconds have elapsed between now and the last refill?
   * This subtraction can't underflow, because now >= last_refill_ts.
   * And it can't overflow, because INT64_MAX - (-INT64_MIN) == UINT64_MAX. */
  elapsed_time_last_refill = (uint64_t)now - last_refill_ts;

  /* If the elapsed time is very large, it means our clock jumped forward.
   * If the multiplication would overflow, use the maximum allowed value. */
  if (elapsed_time_last_refill > UINT32_MAX) {
    num_token = dos_cc_circuit_burst;
    goto end;
  }

  /* Compute how many circuits we are allowed in that time frame which we'll
   * add to the bucket. This can't overflow, because both multiplicands
   * are less than or equal to UINT32_MAX, and num_token is uint64_t. */
  num_token = elapsed_time_last_refill * circuit_rate;

 end:
  /* If the sum would overflow, use the maximum allowed value. */
  if (num_token > UINT32_MAX - stats->circuit_bucket) {
    new_circuit_bucket_count = dos_cc_circuit_burst;
  } else {
    /* We cap the bucket to the burst value else this could overflow uint32_t
     * over time. */
    new_circuit_bucket_count = MIN(stats->circuit_bucket + (uint32_t)num_token,
                                   dos_cc_circuit_burst);
  }

  /* This function is not allowed to make the bucket count larger than the
   * burst value */
  tor_assert_nonfatal(new_circuit_bucket_count <= dos_cc_circuit_burst);
  /* This function is not allowed to make the bucket count smaller, unless it
   * is decreasing it to a newly configured, lower burst value. We allow the
   * bucket to stay the same size, in case the circuit rate is zero. */
  tor_assert_nonfatal(new_circuit_bucket_count >= stats->circuit_bucket ||
                      new_circuit_bucket_count == dos_cc_circuit_burst);

  log_debug(LD_DOS, "DoS address %s has its circuit bucket value: %" PRIu32
                    ". Filling it to %" PRIu32 ". Circuit rate is %" PRIu64
                    ". Elapsed time is %" PRIi64,
            fmt_addr(addr), stats->circuit_bucket, new_circuit_bucket_count,
            circuit_rate, (int64_t)elapsed_time_last_refill);

  stats->circuit_bucket = new_circuit_bucket_count;
  stats->last_circ_bucket_refill_ts = now;

 done:
  return;
}

/* Return true iff the circuit bucket is down to 0 and the number of
 * concurrent connections is greater or equal the minimum threshold set the
 * consensus parameter. */
static int
cc_has_exhausted_circuits(const dos_client_stats_t *stats)
{
  tor_assert(stats);
  return stats->cc_stats.circuit_bucket == 0 &&
         stats->conn_stats.concurrent_count >= dos_cc_min_concurrent_conn;
}

/* Mark client address by setting a timestamp in the stats object which tells
 * us until when it is marked as positively detected. */
static void
cc_mark_client(cc_client_stats_t *stats)
{
  tor_assert(stats);
  /* We add a random offset of a maximum of half the defense time so it is
   * less predictable. */
  stats->marked_until_ts =
    approx_time() + dos_cc_defense_time_period +
    crypto_rand_int_range(1, dos_cc_defense_time_period / 2);
}

/* Return true iff the given channel address is marked as malicious. This is
 * called a lot and part of the fast path of handling cells. It has to remain
 * as fast as we can. */
static int
cc_channel_addr_is_marked(channel_t *chan)
{
  time_t now;
  tor_addr_t addr;
  clientmap_entry_t *entry;
  cc_client_stats_t *stats = NULL;

  if (chan == NULL) {
    goto end;
  }
  /* Must be a client connection else we ignore. */
  if (!channel_is_client(chan)) {
    goto end;
  }
  /* Without an IP address, nothing can work. */
  if (!channel_get_addr_if_possible(chan, &addr)) {
    goto end;
  }

  /* We are only interested in client connection from the geoip cache. */
  entry = geoip_lookup_client(&addr, NULL, GEOIP_CLIENT_CONNECT);
  if (entry == NULL) {
    /* We can have a connection creating circuits but not tracked by the geoip
     * cache. Once this DoS subsystem is enabled, we can end up here with no
     * entry for the channel. */
    goto end;
  }
  now = approx_time();
  stats = &entry->dos_stats.cc_stats;

 end:
  return stats && stats->marked_until_ts >= now;
}

/* Concurrent connection private API. */

/* Mark client connection stats by setting a timestamp which tells us until
 * when it is marked as positively detected. */
static void
conn_mark_client(conn_client_stats_t *stats)
{
  tor_assert(stats);

  /* We add a random offset of a maximum of half the defense time so it is
   * less predictable and thus more difficult to game. */
  stats->marked_until_ts =
    approx_time() + dos_conn_connect_defense_time_period +
    crypto_rand_int_range(1, dos_conn_connect_defense_time_period / 2);
}

/* Free everything for the connection DoS mitigation subsystem. */
static void
conn_free_all(void)
{
  dos_conn_enabled = 0;
}

/* Called when the consensus has changed. Do appropriate actions for the
 * connection mitigation subsystem. */
static void
conn_consensus_has_changed(const networkstatus_t *ns)
{
  /* Looking at the consensus, is the connection mitigation subsystem enabled?
   * If not and it was enabled before, clean it up. */
  if (dos_conn_enabled && !get_param_conn_enabled(ns)) {
    conn_free_all();
  }
}

/** Called when a new client connection has arrived. The following will update
 * the client connection statistics.
 *
 * The addr is used for logging purposes only.
 *
 * If the connect counter reaches its limit, it is marked. */
static void
conn_update_on_connect(conn_client_stats_t *stats, const tor_addr_t *addr)
{
  tor_assert(stats);
  tor_assert(addr);

  /* Update concurrent count for this new connect. */
  stats->concurrent_count++;

  /* Refill connect connection count. */
  token_bucket_ctr_refill(&stats->connect_count,
                          (uint32_t) monotime_coarse_absolute_sec());

  /* Decrement counter for this new connection. */
  if (token_bucket_ctr_get(&stats->connect_count) > 0) {
    token_bucket_ctr_dec(&stats->connect_count, 1);
  }

  /* Assess connect counter. Mark it if counter is down to 0 and we haven't
   * marked it before or it was reset. This is to avoid to re-mark it over and
   * over again extending continuously the blocked time. */
  if (token_bucket_ctr_get(&stats->connect_count) == 0 &&
      stats->marked_until_ts == 0) {
    conn_mark_client(stats);
  }

  log_debug(LD_DOS, "Client address %s has now %u concurrent connections. "
                    "Remaining %" TOR_PRIuSZ "/sec connections are allowed.",
            fmt_addr(addr), stats->concurrent_count,
            token_bucket_ctr_get(&stats->connect_count));
}

/** Called when a client connection is closed. The following will update
 * the client connection statistics.
 *
 * The addr is used for logging purposes only. */
static void
conn_update_on_close(conn_client_stats_t *stats, const tor_addr_t *addr)
{
  /* Extra super duper safety. Going below 0 means an underflow which could
   * lead to most likely a false positive. In theory, this should never happen
   * but let's be extra safe. */
  if (BUG(stats->concurrent_count == 0)) {
    return;
  }

  stats->concurrent_count--;
  log_debug(LD_DOS, "Client address %s has lost a connection. Concurrent "
                    "connections are now at %u",
            fmt_addr(addr), stats->concurrent_count);
}

/* General private API */

/* Return true iff we have at least one DoS detection enabled. This is used to
 * decide if we need to allocate any kind of high level DoS object. */
static inline int
dos_is_enabled(void)
{
  return (dos_cc_enabled || dos_conn_enabled);
}

/* Circuit creation public API. */

/** Return the number of rejected circuits. */
uint64_t
dos_get_num_cc_rejected(void)
{
  return cc_num_rejected_cells;
}

/** Return the number of marked addresses. */
uint32_t
dos_get_num_cc_marked_addr(void)
{
  return cc_num_marked_addrs;
}

/** Return the number of marked addresses due to max queue limit reached. */
uint32_t
dos_get_num_cc_marked_addr_maxq(void)
{
  return cc_num_marked_addrs_max_queue;
}

/** Return number of concurrent connections rejected. */
uint64_t
dos_get_num_conn_addr_rejected(void)
{
  return conn_num_addr_rejected;
}

/** Return the number of connection rejected. */
uint64_t
dos_get_num_conn_addr_connect_rejected(void)
{
  return conn_num_addr_connect_rejected;
}

/** Return the number of single hop refused. */
uint64_t
dos_get_num_single_hop_refused(void)
{
  return num_single_hop_client_refused;
}

/* Called when a CREATE cell is received from the given channel. */
void
dos_cc_new_create_cell(channel_t *chan)
{
  tor_addr_t addr;
  clientmap_entry_t *entry;

  tor_assert(chan);

  /* Skip everything if not enabled. */
  if (!dos_cc_enabled) {
    goto end;
  }

  /* Must be a client connection else we ignore. */
  if (!channel_is_client(chan)) {
    goto end;
  }
  /* Without an IP address, nothing can work. */
  if (!channel_get_addr_if_possible(chan, &addr)) {
    goto end;
  }

  /* We are only interested in client connection from the geoip cache. */
  entry = geoip_lookup_client(&addr, NULL, GEOIP_CLIENT_CONNECT);
  if (entry == NULL) {
    /* We can have a connection creating circuits but not tracked by the geoip
     * cache. Once this DoS subsystem is enabled, we can end up here with no
     * entry for the channel. */
    goto end;
  }

  /* General comment. Even though the client can already be marked as
   * malicious, we continue to track statistics. If it keeps going above
   * threshold while marked, the defense period time will grow longer. There
   * is really no point at unmarking a client that keeps DoSing us. */

  /* First of all, we'll try to refill the circuit bucket opportunistically
   * before we assess. */
  cc_stats_refill_bucket(&entry->dos_stats.cc_stats, &addr);

  /* Take a token out of the circuit bucket if we are above 0 so we don't
   * underflow the bucket. */
  if (entry->dos_stats.cc_stats.circuit_bucket > 0) {
    entry->dos_stats.cc_stats.circuit_bucket--;
  }

  /* This is the detection. Assess at every CREATE cell if the client should
   * get marked as malicious. This should be kept as fast as possible. */
  if (cc_has_exhausted_circuits(&entry->dos_stats)) {
    /* If this is the first time we mark this entry, log it.
     * Under heavy DDoS, logging each time we mark would results in lots and
     * lots of logs. */
    if (entry->dos_stats.cc_stats.marked_until_ts == 0) {
      log_debug(LD_DOS, "Detected circuit creation DoS by address: %s",
                fmt_addr(&addr));
      cc_num_marked_addrs++;
    }
    cc_mark_client(&entry->dos_stats.cc_stats);
  }

 end:
  return;
}

/* Return the defense type that should be used for this circuit.
 *
 * This is part of the fast path and called a lot. */
dos_cc_defense_type_t
dos_cc_get_defense_type(channel_t *chan)
{
  tor_assert(chan);

  /* Skip everything if not enabled. */
  if (!dos_cc_enabled) {
    goto end;
  }

  /* On an OR circuit, we'll check if the previous channel is a marked client
   * connection detected by our DoS circuit creation mitigation subsystem. */
  if (cc_channel_addr_is_marked(chan)) {
    /* We've just assess that this circuit should trigger a defense for the
     * cell it just seen. Note it down. */
    cc_num_rejected_cells++;
    return dos_cc_defense_type;
  }

 end:
  return DOS_CC_DEFENSE_NONE;
}

/* Concurrent connection detection public API. */

/* Return true iff the given address is permitted to open another connection.
 * A defense value is returned for the caller to take appropriate actions. */
dos_conn_defense_type_t
dos_conn_addr_get_defense_type(const tor_addr_t *addr)
{
  clientmap_entry_t *entry;

  tor_assert(addr);

  /* Skip everything if not enabled. */
  if (!dos_conn_enabled) {
    goto end;
  }

  /* We are only interested in client connection from the geoip cache. */
  entry = geoip_lookup_client(addr, NULL, GEOIP_CLIENT_CONNECT);
  if (entry == NULL) {
    goto end;
  }

  /* Is this address marked as making too many client connections? */
  if (entry->dos_stats.conn_stats.marked_until_ts >= approx_time()) {
    conn_num_addr_connect_rejected++;
    return dos_conn_defense_type;
  }
  /* Reset it to 0 here so that if the marked timestamp has expired that is
   * we've gone beyond it, we have to reset it so the detection can mark it
   * again in the future. */
  entry->dos_stats.conn_stats.marked_until_ts = 0;

  /* Need to be above the maximum concurrent connection count to trigger a
   * defense. */
  if (entry->dos_stats.conn_stats.concurrent_count >
      dos_conn_max_concurrent_count) {
    conn_num_addr_rejected++;
    return dos_conn_defense_type;
  }

 end:
  return DOS_CONN_DEFENSE_NONE;
}

/* Stream creation public API. */

/** Return the number of rejected stream and resolve. */
uint64_t
dos_get_num_stream_rejected(void)
{
  return stream_num_rejected;
}

/* Return the action to take against a BEGIN or RESOLVE cell. Return
 *  DOS_STREAM_DEFENSE_NONE when no action should be taken.
 *  Increment the appropriate counter when the cell was found to go over a
 *  limit. */
dos_stream_defense_type_t
dos_stream_new_begin_or_resolve_cell(or_circuit_t *circ)
{
  if (!dos_stream_enabled || circ == NULL)
    return DOS_STREAM_DEFENSE_NONE;

  token_bucket_ctr_refill(&circ->stream_limiter,
                          (uint32_t) monotime_coarse_absolute_sec());

  if (token_bucket_ctr_get(&circ->stream_limiter) > 0) {
    token_bucket_ctr_dec(&circ->stream_limiter, 1);
    return DOS_STREAM_DEFENSE_NONE;
  }
  /* if defense type is DOS_STREAM_DEFENSE_NONE but DoSStreamEnabled is true,
   * we count offending cells as rejected, despite them being actually
   * accepted. */
  ++stream_num_rejected;
  return dos_stream_defense_type;
}

/* Initialize the token bucket for stream rate limit on a circuit. */
void
dos_stream_init_circ_tbf(or_circuit_t *circ)
{
  token_bucket_ctr_init(&circ->stream_limiter, dos_stream_rate,
                        dos_stream_burst,
                        (uint32_t) monotime_coarse_absolute_sec());
}

/* General API */

/* Take any appropriate actions for the given geoip entry that is about to get
 * freed. This is called for every entry that is being freed.
 *
 * This function will clear out the connection tracked flag if the concurrent
 * count of the entry is above 0 so if those connections end up being seen by
 * this subsystem, we won't try to decrement the counter for a new geoip entry
 * that might have been added after this call for the same address. */
void
dos_geoip_entry_about_to_free(const clientmap_entry_t *geoip_ent)
{
  tor_assert(geoip_ent);

  /* The count is down to 0 meaning no connections right now, we can safely
   * clear the geoip entry from the cache. */
  if (geoip_ent->dos_stats.conn_stats.concurrent_count == 0) {
    goto end;
  }

  /* For each connection matching the geoip entry address, we'll clear the
   * tracked flag because the entry is about to get removed from the geoip
   * cache. We do not try to decrement if the flag is not set. */
  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
    if (conn->type == CONN_TYPE_OR) {
      or_connection_t *or_conn = TO_OR_CONN(conn);
      if (!tor_addr_compare(&geoip_ent->addr, &TO_CONN(or_conn)->addr,
                            CMP_EXACT)) {
        or_conn->tracked_for_dos_mitigation = 0;
      }
    }
  } SMARTLIST_FOREACH_END(conn);

 end:
  return;
}

/** A new geoip client entry has been allocated, initialize its DoS object. */
void
dos_geoip_entry_init(clientmap_entry_t *geoip_ent)
{
  tor_assert(geoip_ent);

  /* Initialize the connection count counter with the rate and burst
   * parameters taken either from configuration or consensus.
   *
   * We do this even if the DoS connection detection is not enabled because it
   * can be enabled at runtime and these counters need to be valid. */
  token_bucket_ctr_init(&geoip_ent->dos_stats.conn_stats.connect_count,
                        dos_conn_connect_rate, dos_conn_connect_burst,
                        (uint32_t) monotime_coarse_absolute_sec());
}

/** Note that the given channel has sent outbound the maximum amount of cell
 * allowed on the next channel. */
void
dos_note_circ_max_outq(const channel_t *chan)
{
  tor_addr_t addr;
  clientmap_entry_t *entry;

  tor_assert(chan);

  /* Skip everything if circuit creation defense is disabled. */
  if (!dos_cc_enabled) {
    goto end;
  }

  /* Must be a client connection else we ignore. */
  if (!channel_is_client(chan)) {
    goto end;
  }
  /* Without an IP address, nothing can work. */
  if (!channel_get_addr_if_possible(chan, &addr)) {
    goto end;
  }

  /* We are only interested in client connection from the geoip cache. */
  entry = geoip_lookup_client(&addr, NULL, GEOIP_CLIENT_CONNECT);
  if (entry == NULL) {
    goto end;
  }

  /* Is the client marked? If yes, just ignore. */
  if (entry->dos_stats.cc_stats.marked_until_ts >= approx_time()) {
    goto end;
  }

  /* If max outq parameter is 0, it means disabled, just ignore. */
  if (dos_num_circ_max_outq == 0) {
    goto end;
  }

  entry->dos_stats.num_circ_max_cell_queue_size++;

  /* This is the detection. If we have reached the maximum amount of times a
   * client IP is allowed to reach this limit, mark client. */
  if (entry->dos_stats.num_circ_max_cell_queue_size >=
      dos_num_circ_max_outq) {
    /* Only account for this marked address if this is the first time we block
     * it else our counter is inflated with non unique entries. */
    if (entry->dos_stats.cc_stats.marked_until_ts == 0) {
      cc_num_marked_addrs_max_queue++;
    }
    log_info(LD_DOS, "Detected outbound max circuit queue from addr: %s",
             fmt_addr(&addr));
    cc_mark_client(&entry->dos_stats.cc_stats);

    /* Reset after being marked so once unmarked, we start back clean. */
    entry->dos_stats.num_circ_max_cell_queue_size = 0;
  }

 end:
  return;
}

/* Note down that we've just refused a single hop client. This increments a
 * counter later used for the heartbeat. */
void
dos_note_refuse_single_hop_client(void)
{
  num_single_hop_client_refused++;
}

/* Return true iff single hop client connection (ESTABLISH_RENDEZVOUS) should
 * be refused. */
int
dos_should_refuse_single_hop_client(void)
{
  /* If we aren't a public relay, this shouldn't apply to anything. */
  if (!public_server_mode(get_options())) {
    return 0;
  }

  if (dos_get_options()->DoSRefuseSingleHopClientRendezvous != -1) {
    return dos_get_options()->DoSRefuseSingleHopClientRendezvous;
  }

  return (int) networkstatus_get_param(NULL,
                                       "DoSRefuseSingleHopClientRendezvous",
                                       0 /* default */, 0, 1);
}

/* Log a heartbeat message with some statistics. */
void
dos_log_heartbeat(void)
{
  smartlist_t *elems = smartlist_new();

  /* Stats number coming from relay.c append_cell_to_circuit_queue(). */
  smartlist_add_asprintf(elems,
                         "%" PRIu64 " circuits killed with too many cells",
                         stats_n_circ_max_cell_reached);

  if (dos_cc_enabled) {
    smartlist_add_asprintf(elems,
                           "%" PRIu64 " circuits rejected, "
                           "%" PRIu32 " marked addresses, "
                           "%" PRIu32 " marked addresses for max queue",
                           cc_num_rejected_cells, cc_num_marked_addrs,
                           cc_num_marked_addrs_max_queue);
  } else {
    smartlist_add_asprintf(elems, "[DoSCircuitCreationEnabled disabled]");
  }

  if (dos_conn_enabled) {
    smartlist_add_asprintf(elems,
                           "%" PRIu64 " same address concurrent "
                           "connections rejected", conn_num_addr_rejected);
    smartlist_add_asprintf(elems,
                           "%" PRIu64 " connections rejected",
                           conn_num_addr_connect_rejected);
  } else {
    smartlist_add_asprintf(elems, "[DoSConnectionEnabled disabled]");
  }

  if (dos_should_refuse_single_hop_client()) {
    smartlist_add_asprintf(elems,
                           "%" PRIu64 " single hop clients refused",
                           num_single_hop_client_refused);
  } else {
    smartlist_add_asprintf(elems,
                           "[DoSRefuseSingleHopClientRendezvous disabled]");
  }

  if (dos_stream_enabled) {
    smartlist_add_asprintf(elems,
                           "%" PRIu64 " stream rejected",
                           stream_num_rejected);
  } else {
    smartlist_add_asprintf(elems, "[DoSStreamCreationEnabled disabled]");
  }

  /* HS DoS stats. */
  smartlist_add_asprintf(elems,
                         "%" PRIu64 " INTRODUCE2 rejected",
                         hs_dos_get_intro2_rejected_count());

  char *msg = smartlist_join_strings(elems, ", ", 0, NULL);

  log_notice(LD_HEARTBEAT,
             "Heartbeat: DoS mitigation since startup: %s.", msg);

  tor_free(msg);
  SMARTLIST_FOREACH(elems, char *, e, tor_free(e));
  smartlist_free(elems);
}

/* Called when a new client connection has been established on the given
 * address. */
void
dos_new_client_conn(or_connection_t *or_conn, const char *transport_name)
{
  clientmap_entry_t *entry;

  tor_assert(or_conn);
  tor_assert_nonfatal(!or_conn->tracked_for_dos_mitigation);

  /* Past that point, we know we have at least one DoS detection subsystem
   * enabled so we'll start allocating stuff. */
  if (!dos_is_enabled()) {
    goto end;
  }

  /* We are only interested in client connection from the geoip cache. */
  entry = geoip_lookup_client(&TO_CONN(or_conn)->addr, transport_name,
                              GEOIP_CLIENT_CONNECT);
  if (BUG(entry == NULL)) {
    /* Should never happen because we note down the address in the geoip
     * cache before this is called. */
    goto end;
  }

  /* Update stats from this new connect. */
  conn_update_on_connect(&entry->dos_stats.conn_stats,
                         &TO_CONN(or_conn)->addr);

  or_conn->tracked_for_dos_mitigation = 1;

 end:
  return;
}

/* Called when a client connection for the given IP address has been closed. */
void
dos_close_client_conn(const or_connection_t *or_conn)
{
  clientmap_entry_t *entry;

  tor_assert(or_conn);

  /* We have to decrement the count on tracked connection only even if the
   * subsystem has been disabled at runtime because it might be re-enabled
   * after and we need to keep a synchronized counter at all time. */
  if (!or_conn->tracked_for_dos_mitigation) {
    goto end;
  }

  /* We are only interested in client connection from the geoip cache. */
  entry = geoip_lookup_client(&TO_CONN(or_conn)->addr, NULL,
                              GEOIP_CLIENT_CONNECT);
  if (entry == NULL) {
    /* This can happen because we can close a connection before the channel
     * got to be noted down in the geoip cache. */
    goto end;
  }

  /* Update stats from this new close. */
  conn_update_on_close(&entry->dos_stats.conn_stats, &TO_CONN(or_conn)->addr);

 end:
  return;
}

/* Called when the consensus has changed. We might have new consensus
 * parameters to look at. */
void
dos_consensus_has_changed(const networkstatus_t *ns)
{
  /* There are two ways to configure this subsystem, one at startup through
   * dos_init() which is called when the options are parsed. And this one
   * through the consensus. We don't want to enable any DoS mitigation if we
   * aren't a public relay. */
  if (!public_server_mode(get_options())) {
    return;
  }

  cc_consensus_has_changed(ns);
  conn_consensus_has_changed(ns);

  /* We were already enabled or we just became enabled but either way, set the
   * consensus parameters for all subsystems. */
  set_dos_parameters(ns);
}

/* Return true iff the DoS mitigation subsystem is enabled. */
int
dos_enabled(void)
{
  return dos_is_enabled();
}

/* Free everything from the Denial of Service subsystem. */
void
dos_free_all(void)
{
  /* Free the circuit creation mitigation subsystem. It is safe to do this
   * even if it wasn't initialized. */
  cc_free_all();

  /* Free the connection mitigation subsystem. It is safe to do this even if
   * it wasn't initialized. */
  conn_free_all();
}

/* Initialize the Denial of Service subsystem. */
void
dos_init(void)
{
  /* To initialize, we only need to get the parameters. */
  set_dos_parameters(NULL);
}