dos.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /*
  4. * \file dos.h
  5. * \brief Header file for dos.c
  6. */
  7. #ifndef TOR_DOS_H
  8. #define TOR_DOS_H
  9. /* Structure that keeps stats of client connection per-IP. */
  10. typedef struct cc_client_stats_t {
  11. /* Number of allocated circuits remaining for this address. It is
  12. * decremented every time a new circuit is seen for this client address and
  13. * if the count goes to 0, we have a positive detection. */
  14. uint32_t circuit_bucket;
  15. /* When was the last time we've refilled the circuit bucket? This is used to
  16. * know if we need to refill the bucket when a new circuit is seen. It is
  17. * synchronized using approx_time(). */
  18. time_t last_circ_bucket_refill_ts;
  19. /* This client address was detected to be above the circuit creation rate
  20. * and this timestamp indicates until when it should remain marked as
  21. * detected so we can apply a defense for the address. It is synchronized
  22. * using the approx_time(). */
  23. time_t marked_until_ts;
  24. } cc_client_stats_t;
  25. /* This object is a top level object that contains everything related to the
  26. * per-IP client DoS mitigation. Because it is per-IP, it is used in the geoip
  27. * clientmap_entry_t object. */
  28. typedef struct dos_client_stats_t {
  29. /* Concurrent connection count from the specific address. 2^32 is most
  30. * likely way too big for the amount of allowed file descriptors. */
  31. uint32_t concurrent_count;
  32. /* Circuit creation statistics. This is only used if the circuit creation
  33. * subsystem has been enabled (dos_cc_enabled). */
  34. cc_client_stats_t cc_stats;
  35. } dos_client_stats_t;
  36. /* General API. */
  37. void dos_init(void);
  38. void dos_free_all(void);
  39. void dos_consensus_has_changed(const networkstatus_t *ns);
  40. int dos_enabled(void);
  41. void dos_new_client_conn(or_connection_t *or_conn);
  42. void dos_close_client_conn(const or_connection_t *or_conn);
  43. /*
  44. * Circuit creation DoS mitigation subsystemn interface.
  45. */
  46. /* DoSCircuitCreationEnabled default. Disabled by default. */
  47. #define DOS_CC_ENABLED_DEFAULT 0
  48. /* DoSCircuitCreationDefenseType maps to the dos_cc_defense_type_t enum. */
  49. #define DOS_CC_DEFENSE_TYPE_DEFAULT DOS_CC_DEFENSE_REFUSE_CELL
  50. /* DoSCircuitCreationMinConnections default */
  51. #define DOS_CC_MIN_CONCURRENT_CONN_DEFAULT 3
  52. /* DoSCircuitCreationRateTenths is 3 per seconds. */
  53. #define DOS_CC_CIRCUIT_RATE_TENTHS_DEFAULT (3 * 10)
  54. /* DoSCircuitCreationBurst default. */
  55. #define DOS_CC_CIRCUIT_BURST_DEFAULT 90
  56. /* DoSCircuitCreationDefenseTimePeriod in seconds. */
  57. #define DOS_CC_DEFENSE_TIME_PERIOD_DEFAULT (60 * 60)
  58. /* Type of defense that we can use for the circuit creation DoS mitigation. */
  59. typedef enum dos_cc_defense_type_t {
  60. /* No defense used. */
  61. DOS_CC_DEFENSE_NONE = 1,
  62. /* Refuse any cells which means a DESTROY cell will be sent back. */
  63. DOS_CC_DEFENSE_REFUSE_CELL = 2,
  64. /* Maximum value that can be used. Useful for the boundaries of the
  65. * consensus parameter. */
  66. DOS_CC_DEFENSE_MAX = 2,
  67. } dos_cc_defense_type_t;
  68. /*
  69. * Concurrent connection DoS mitigation interface.
  70. */
  71. /* DoSConnectionEnabled default. Disabled by default. */
  72. #define DOS_CONN_ENABLED_DEFAULT 0
  73. /* DoSConnectionMaxConcurrentCount default. */
  74. #define DOS_CONN_MAX_CONCURRENT_COUNT_DEFAULT 100
  75. /* DoSConnectionDefenseType maps to the dos_conn_defense_type_t enum. */
  76. #define DOS_CONN_DEFENSE_TYPE_DEFAULT DOS_CONN_DEFENSE_CLOSE
  77. /* Type of defense that we can use for the concurrent connection DoS
  78. * mitigation. */
  79. typedef enum dos_conn_defense_type_t {
  80. /* No defense used. */
  81. DOS_CONN_DEFENSE_NONE = 1,
  82. /* Close immediately the connection meaning refuse it. */
  83. DOS_CONN_DEFENSE_CLOSE = 2,
  84. /* Maximum value that can be used. Useful for the boundaries of the
  85. * consensus parameter. */
  86. DOS_CONN_DEFENSE_MAX = 2,
  87. } dos_conn_defense_type_t;
  88. #ifdef DOS_PRIVATE
  89. STATIC uint32_t get_param_conn_max_concurrent_count(
  90. const networkstatus_t *ns);
  91. STATIC uint32_t get_param_cc_circuit_burst(const networkstatus_t *ns);
  92. STATIC uint32_t get_param_cc_min_concurrent_connection(
  93. const networkstatus_t *ns);
  94. MOCK_DECL(STATIC unsigned int, get_param_cc_enabled,
  95. (const networkstatus_t *ns));
  96. MOCK_DECL(STATIC unsigned int, get_param_conn_enabled,
  97. (const networkstatus_t *ns));
  98. #endif /* TOR_DOS_PRIVATE */
  99. #endif /* TOR_DOS_H */