circuitpadding.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * Copyright (c) 2017-2019, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file circuitpadding.h
  6. * \brief Header file for circuitpadding.c.
  7. **/
  8. #ifndef TOR_CIRCUITPADDING_H
  9. #define TOR_CIRCUITPADDING_H
  10. #include "trunnel/circpad_negotiation.h"
  11. #include "lib/evloop/timers.h"
  12. struct circuit_t;
  13. struct origin_circuit_t;
  14. struct cell_t;
  15. /**
  16. * Signed error return with the specific property that negative
  17. * values mean error codes of various semantics, 0 means success,
  18. * and positive values are unused.
  19. *
  20. * XXX: Tor uses this concept a lot but just calls it int. Should we move
  21. * this somewhere centralized? Where?
  22. */
  23. typedef int signed_error_t;
  24. /**
  25. * These constants specify the types of events that can cause
  26. * transitions between state machine states.
  27. *
  28. * Note that SENT and RECV are relative to this endpoint. For
  29. * relays, SENT means packets destined towards the client and
  30. * RECV means packets destined towards the relay. On the client,
  31. * SENT means packets destined towards the relay, where as RECV
  32. * means packets destined towards the client.
  33. */
  34. typedef enum {
  35. /* A non-padding cell was received. */
  36. CIRCPAD_EVENT_NONPADDING_RECV = 0,
  37. /* A non-padding cell was sent. */
  38. CIRCPAD_EVENT_NONPADDING_SENT = 1,
  39. /* A padding cell (RELAY_COMMAND_DROP) was sent. */
  40. CIRCPAD_EVENT_PADDING_SENT = 2,
  41. /* A padding cell was received. */
  42. CIRCPAD_EVENT_PADDING_RECV = 3,
  43. /* We tried to schedule padding but we ended up picking the infinity bin
  44. * which means that padding was delayed infinitely */
  45. CIRCPAD_EVENT_INFINITY = 4,
  46. /* All histogram bins are empty (we are out of tokens) */
  47. CIRCPAD_EVENT_BINS_EMPTY = 5,
  48. /* This state has used up its cell count */
  49. CIRCPAD_EVENT_LENGTH_COUNT = 6
  50. } circpad_event_t;
  51. #define CIRCPAD_NUM_EVENTS ((int)CIRCPAD_EVENT_LENGTH_COUNT+1)
  52. /** Boolean type that says if we decided to transition states or not */
  53. typedef enum {
  54. CIRCPAD_STATE_UNCHANGED = 0,
  55. CIRCPAD_STATE_CHANGED = 1
  56. } circpad_decision_t;
  57. /** The type for the things in histogram bins (aka tokens) */
  58. typedef uint32_t circpad_hist_token_t;
  59. /** The type for histogram indexes (needs to be negative for errors) */
  60. typedef int8_t circpad_hist_index_t;
  61. /** The type for absolute time, from monotime_absolute_usec() */
  62. typedef uint64_t circpad_time_t;
  63. /** The type for timer delays, in microseconds */
  64. typedef uint32_t circpad_delay_t;
  65. #define CIRCPAD_DELAY_UNITS_PER_SECOND (1000*1000)
  66. /**
  67. * An infinite padding cell delay means don't schedule any padding --
  68. * simply wait until a different event triggers a transition.
  69. *
  70. * This means that the maximum delay we can schedule is UINT32_MAX-1
  71. * microseconds, or about 4300 seconds (1.25 hours).
  72. * XXX: Is this enough if we want to simulate light, intermittent
  73. * activity on an onion service?
  74. */
  75. #define CIRCPAD_DELAY_INFINITE (UINT32_MAX)
  76. /**
  77. * This is the maximum delay that the circuit padding system can have, in
  78. * seconds.
  79. */
  80. #define CIRCPAD_DELAY_MAX_SECS \
  81. ((CIRCPAD_DELAY_INFINITE/CIRCPAD_DELAY_UNITS_PER_SECOND)+1)
  82. /**
  83. * Macro to clarify when we're checking the infinity bin.
  84. *
  85. * Works with either circpad_state_t or circpad_machine_runtime_t
  86. */
  87. #define CIRCPAD_INFINITY_BIN(mi) ((mi)->histogram_len-1)
  88. /**
  89. * These constants form a bitfield that specifies when a state machine
  90. * should be applied to a circuit.
  91. *
  92. * If any of these elements is set, then the circuit will be tested against
  93. * that specific condition. If an element is unset, then we don't test it.
  94. * (E.g., if neither NO_STREAMS or STREAMS are set, then we will not care
  95. * whether a circuit has streams attached when we apply a state machine.)
  96. *
  97. * The helper function circpad_circuit_state() converts circuit state
  98. * flags into this more compact representation.
  99. */
  100. typedef enum {
  101. /* Only apply machine if the circuit is still building */
  102. CIRCPAD_CIRC_BUILDING = 1<<0,
  103. /* Only apply machine if the circuit is open */
  104. CIRCPAD_CIRC_OPENED = 1<<1,
  105. /* Only apply machine if the circuit has no attached streams */
  106. CIRCPAD_CIRC_NO_STREAMS = 1<<2,
  107. /* Only apply machine if the circuit has attached streams */
  108. CIRCPAD_CIRC_STREAMS = 1<<3,
  109. /* Only apply machine if the circuit still allows RELAY_EARLY cells */
  110. CIRCPAD_CIRC_HAS_RELAY_EARLY = 1<<4,
  111. /* Only apply machine if the circuit has depleted its RELAY_EARLY cells
  112. * allowance. */
  113. CIRCPAD_CIRC_HAS_NO_RELAY_EARLY = 1<<5
  114. } circpad_circuit_state_t;
  115. /** Bitmask that says "apply this machine to all states" */
  116. #define CIRCPAD_STATE_ALL \
  117. (CIRCPAD_CIRC_BUILDING|CIRCPAD_CIRC_OPENED| \
  118. CIRCPAD_CIRC_STREAMS|CIRCPAD_CIRC_NO_STREAMS| \
  119. CIRCPAD_CIRC_HAS_RELAY_EARLY|CIRCPAD_CIRC_HAS_NO_RELAY_EARLY)
  120. /**
  121. * A compact circuit purpose bitfield mask that allows us to compactly
  122. * specify which circuit purposes a machine should apply to.
  123. *
  124. * The helper function circpad_circ_purpose_to_mask() converts circuit
  125. * purposes into bit positions in this bitmask.
  126. */
  127. typedef uint32_t circpad_purpose_mask_t;
  128. /** Bitmask that says "apply this machine to all purposes". */
  129. #define CIRCPAD_PURPOSE_ALL (0xFFFFFFFF)
  130. /**
  131. * This type specifies all of the conditions that must be met before
  132. * a client decides to initiate padding on a circuit.
  133. *
  134. * A circuit must satisfy every sub-field in this type in order
  135. * to be considered to match the conditions.
  136. */
  137. typedef struct circpad_machine_conditions_t {
  138. /** Only apply the machine *if* the circuit has at least this many hops */
  139. unsigned min_hops : 3;
  140. /** Only apply the machine *if* vanguards are enabled */
  141. unsigned requires_vanguards : 1;
  142. /**
  143. * This machine is ok to use if reduced padding is set in consensus
  144. * or torrc. This machine will still be applied even if reduced padding
  145. * is not set; this flag only acts to exclude machines that don't have
  146. * it set when reduced padding is requested. Therefore, reduced padding
  147. * machines should appear at the lowest priority in the padding machine
  148. * lists (aka first in the list), so that non-reduced padding machines
  149. * for the same purpose are given a chance to apply when reduced padding
  150. * is not requested. */
  151. unsigned reduced_padding_ok : 1;
  152. /** Only apply the machine *if* the circuit's state matches any of
  153. * the bits set in this bitmask. */
  154. circpad_circuit_state_t state_mask;
  155. /** Only apply a machine *if* the circuit's purpose matches one
  156. * of the bits set in this bitmask */
  157. circpad_purpose_mask_t purpose_mask;
  158. } circpad_machine_conditions_t;
  159. /**
  160. * Token removal strategy options.
  161. *
  162. * The WTF-PAD histograms are meant to specify a target distribution to shape
  163. * traffic towards. This is accomplished by removing tokens from the histogram
  164. * when either padding or non-padding cells are sent.
  165. *
  166. * When we see a non-padding cell at a particular time since the last cell, you
  167. * remove a token from the corresponding delay bin. These flags specify
  168. * which bin to choose if that bin is already empty.
  169. */
  170. typedef enum {
  171. /** Don't remove any tokens */
  172. CIRCPAD_TOKEN_REMOVAL_NONE = 0,
  173. /**
  174. * Remove from the first non-zero higher bin index when current is zero.
  175. * This is the recommended strategy from the Adaptive Padding paper. */
  176. CIRCPAD_TOKEN_REMOVAL_HIGHER = 1,
  177. /** Remove from the first non-zero lower bin index when current is empty. */
  178. CIRCPAD_TOKEN_REMOVAL_LOWER = 2,
  179. /** Remove from the closest non-zero bin index when current is empty. */
  180. CIRCPAD_TOKEN_REMOVAL_CLOSEST = 3,
  181. /** Remove from the closest bin by time value (since bins are
  182. * exponentially spaced). */
  183. CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC = 4,
  184. /** Only remove from the exact bin corresponding to this delay. If
  185. * the bin is 0, simply do nothing. Don't pick another bin. */
  186. CIRCPAD_TOKEN_REMOVAL_EXACT = 5
  187. } circpad_removal_t;
  188. /**
  189. * Distribution types supported by circpad_distribution_sample().
  190. *
  191. * These can be used instead of histograms for the inter-packet
  192. * timing distribution, or to specify a distribution on the number
  193. * of cells that can be sent while in a specific state of the state
  194. * machine.
  195. *
  196. * Each distribution takes up to two parameters which are described below. */
  197. typedef enum {
  198. /* No probability distribution is used */
  199. CIRCPAD_DIST_NONE = 0,
  200. /* Uniform distribution: param1 is lower bound and param2 is upper bound */
  201. CIRCPAD_DIST_UNIFORM = 1,
  202. /* Logistic distribution: param1 is Mu, param2 is sigma. */
  203. CIRCPAD_DIST_LOGISTIC = 2,
  204. /* Log-logistic distribution: param1 is Alpha, param2 is 1.0/Beta */
  205. CIRCPAD_DIST_LOG_LOGISTIC = 3,
  206. /* Geometric distribution: param1 is 'p' (success probability) */
  207. CIRCPAD_DIST_GEOMETRIC = 4,
  208. /* Weibull distribution: param1 is k, param2 is Lambda */
  209. CIRCPAD_DIST_WEIBULL = 5,
  210. /* Generalized Pareto distribution: param1 is sigma, param2 is xi */
  211. CIRCPAD_DIST_PARETO = 6
  212. } circpad_distribution_type_t;
  213. /**
  214. * Distribution information.
  215. *
  216. * This type specifies a specific distribution above, as well as
  217. * up to two parameters for that distribution. The specific
  218. * per-distribution meaning of these parameters is specified
  219. * in circpad_distribution_sample().
  220. */
  221. typedef struct circpad_distribution_t {
  222. circpad_distribution_type_t type;
  223. double param1;
  224. double param2;
  225. } circpad_distribution_t;
  226. /** State number type. Represents current state of state machine. */
  227. typedef uint16_t circpad_statenum_t;
  228. #define CIRCPAD_STATENUM_MAX (UINT16_MAX)
  229. /** A histogram can be used to sample padding delays given a machine state.
  230. * This constant defines the maximum histogram width (i.e. the max number of
  231. * bins).
  232. *
  233. * The current limit is arbitrary and could be raised if there is a need,
  234. * however too many bins will be hard to serialize in the future.
  235. *
  236. * Memory concerns are not so great here since the corresponding histogram and
  237. * histogram_edges arrays are global and not per-circuit.
  238. *
  239. * If we ever upgrade this to a value that can't be represented by 8-bits we
  240. * also need to upgrade circpad_hist_index_t.
  241. */
  242. #define CIRCPAD_MAX_HISTOGRAM_LEN (100)
  243. /**
  244. * A state of a padding state machine. The information here are immutable and
  245. * represent the initial form of the state; it does not get updated as things
  246. * happen. The mutable information that gets updated in runtime are carried in
  247. * a circpad_machine_runtime_t.
  248. *
  249. * This struct describes the histograms and/or probability distributions, as
  250. * well as parameters of a single state in the adaptive padding machine.
  251. * Instances of this struct exist in global circpad machine definitions that
  252. * come from torrc or the consensus.
  253. */
  254. typedef struct circpad_state_t {
  255. /**
  256. * If a histogram is used for this state, this specifies the number of bins
  257. * of this histogram. Histograms must have at least 2 bins.
  258. *
  259. * In particular, the following histogram:
  260. *
  261. * Tokens
  262. * +
  263. * 10 | +----+
  264. * 9 | | | +---------+
  265. * 8 | | | | |
  266. * 7 | | | +-----+ |
  267. * 6 +----+ Bin+-----+ | +---------------+
  268. * 5 | | #1 | | | | |
  269. * | Bin| | Bin | Bin | Bin #4 | Bin #5 |
  270. * | #0 | | #2 | #3 | | (infinity bin)|
  271. * | | | | | | |
  272. * | | | | | | |
  273. * 0 +----+----+-----+-----+---------+---------------+
  274. * 0 100 200 350 500 1000 ∞ microseconds
  275. *
  276. * would be specified the following way:
  277. * histogram_len = 6;
  278. * histogram[] = { 6, 10, 6, 7, 9, 6 }
  279. * histogram_edges[] = { 0, 100, 200, 350, 500, 1000 }
  280. *
  281. * The final bin is called the "infinity bin" and if it's chosen we don't
  282. * schedule any padding. The infinity bin is strange because its lower edge
  283. * is the max value of possible non-infinite delay allowed by this histogram,
  284. * and its upper edge is CIRCPAD_DELAY_INFINITE. You can tell if the infinity
  285. * bin is chosen by inspecting its bin index or inspecting its upper edge.
  286. *
  287. * If a delay probability distribution is used for this state, this is set
  288. * to 0. */
  289. circpad_hist_index_t histogram_len;
  290. /** The histogram itself: an array of uint16s of tokens, whose
  291. * widths are exponentially spaced, in microseconds.
  292. *
  293. * This array must have histogram_len elements that are strictly
  294. * monotonically increasing. */
  295. circpad_hist_token_t histogram[CIRCPAD_MAX_HISTOGRAM_LEN];
  296. /* The histogram bin edges in usec.
  297. *
  298. * Each element of this array specifies the left edge of the corresponding
  299. * bin. The rightmost edge is always infinity and is not specified in this
  300. * array.
  301. *
  302. * This array must have histogram_len elements. */
  303. circpad_delay_t histogram_edges[CIRCPAD_MAX_HISTOGRAM_LEN+1];
  304. /** Total number of tokens in this histogram. This is a constant and is *not*
  305. * decremented every time we spend a token. It's used for initializing and
  306. * refilling the histogram. */
  307. uint32_t histogram_total_tokens;
  308. /**
  309. * Represents a delay probability distribution (aka IAT distribution). It's a
  310. * parametrized way of encoding inter-packet delay information in
  311. * microseconds. It can be used instead of histograms.
  312. *
  313. * If it is used, token_removal below must be set to
  314. * CIRCPAD_TOKEN_REMOVAL_NONE.
  315. *
  316. * Start_usec, range_sec, and rtt_estimates are still applied to the
  317. * results of sampling from this distribution (range_sec is used as a max).
  318. */
  319. circpad_distribution_t iat_dist;
  320. /* If a delay probability distribution is used, this is used as the max
  321. * value we can sample from the distribution. However, RTT measurements and
  322. * dist_added_shift gets applied on top of this value to derive the final
  323. * padding delay. */
  324. circpad_delay_t dist_max_sample_usec;
  325. /* If a delay probability distribution is used and this is set, we will add
  326. * this value on top of the value sampled from the IAT distribution to
  327. * derive the final padding delay (We also add the RTT measurement if it's
  328. * enabled.). */
  329. circpad_delay_t dist_added_shift_usec;
  330. /**
  331. * The length dist is a parameterized way of encoding how long this
  332. * state machine runs in terms of sent padding cells or all
  333. * sent cells. Values are sampled from this distribution, clamped
  334. * to max_len, and then start_len is added to that value.
  335. *
  336. * It may be specified instead of or in addition to
  337. * the infinity bins and bins empty conditions. */
  338. circpad_distribution_t length_dist;
  339. /** A minimum length value, added to the output of length_dist */
  340. uint16_t start_length;
  341. /** A cap on the length value that can be sampled from the length_dist */
  342. uint64_t max_length;
  343. /** Should we decrement length when we see a nonpadding packet?
  344. * XXX: Are there any machines that actually want to set this to 0? There may
  345. * not be. OTOH, it's only a bit.. */
  346. unsigned length_includes_nonpadding : 1;
  347. /**
  348. * This is an array that specifies the next state to transition to upon
  349. * receipt an event matching the indicated array index.
  350. *
  351. * This aborts our scheduled packet and switches to the state
  352. * corresponding to the index of the array. Tokens are filled upon
  353. * this transition.
  354. *
  355. * States are allowed to transition to themselves, which means re-schedule
  356. * a new padding timer. They are also allowed to temporarily "transition"
  357. * to the "IGNORE" and "CANCEL" pseudo-states. See #defines below
  358. * for details on state behavior and meaning.
  359. */
  360. circpad_statenum_t next_state[CIRCPAD_NUM_EVENTS];
  361. /**
  362. * If true, estimate the RTT from this relay to the exit/website and add that
  363. * to start_usec for use as the histogram bin 0 start delay.
  364. *
  365. * Right now this is only supported for relay-side state machines.
  366. */
  367. unsigned use_rtt_estimate : 1;
  368. /** This specifies the token removal strategy to use upon padding and
  369. * non-padding activity. */
  370. circpad_removal_t token_removal;
  371. } circpad_state_t;
  372. /**
  373. * The start state for this machine.
  374. *
  375. * In the original WTF-PAD, this is only used for transition to/from
  376. * the burst state. All other fields are not used. But to simplify the
  377. * code we've made it a first-class state. This has no performance
  378. * consequences, but may make naive serialization of the state machine
  379. * large, if we're not careful about how we represent empty fields.
  380. */
  381. #define CIRCPAD_STATE_START 0
  382. /**
  383. * The burst state for this machine.
  384. *
  385. * In the original Adaptive Padding algorithm and in WTF-PAD
  386. * (https://www.freehaven.net/anonbib/cache/ShWa-Timing06.pdf and
  387. * https://www.cs.kau.se/pulls/hot/thebasketcase-wtfpad/), the burst
  388. * state serves to detect bursts in traffic. This is done by using longer
  389. * delays in its histogram, which represent the expected delays between
  390. * bursts of packets in the target stream. If this delay expires without a
  391. * real packet being sent, the burst state sends a padding packet and then
  392. * immediately transitions to the gap state, which is used to generate
  393. * a synthetic padding packet train. In this implementation, this transition
  394. * needs to be explicitly specified in the burst state's transition events.
  395. *
  396. * Because of this flexibility, other padding mechanisms can transition
  397. * between these two states arbitrarily, to encode other dynamics of
  398. * target traffic.
  399. */
  400. #define CIRCPAD_STATE_BURST 1
  401. /**
  402. * The gap state for this machine.
  403. *
  404. * In the original Adaptive Padding algorithm and in WTF-PAD, the gap
  405. * state serves to simulate an artificial packet train composed of padding
  406. * packets. It does this by specifying much lower inter-packet delays than
  407. * the burst state, and transitioning back to itself after padding is sent
  408. * if these timers expire before real traffic is sent. If real traffic is
  409. * sent, it transitions back to the burst state.
  410. *
  411. * Again, in this implementation, these transitions must be specified
  412. * explicitly, and other transitions are also permitted.
  413. */
  414. #define CIRCPAD_STATE_GAP 2
  415. /**
  416. * End is a pseudo-state that causes the machine to go completely
  417. * idle, and optionally get torn down (depending on the
  418. * value of circpad_machine_spec_t.should_negotiate_end)
  419. *
  420. * End MUST NOT occupy a slot in the machine state array.
  421. */
  422. #define CIRCPAD_STATE_END CIRCPAD_STATENUM_MAX
  423. /**
  424. * "Ignore" is a pseudo-state that means "do not react to this
  425. * event".
  426. *
  427. * "Ignore" MUST NOT occupy a slot in the machine state array.
  428. */
  429. #define CIRCPAD_STATE_IGNORE (CIRCPAD_STATENUM_MAX-1)
  430. /**
  431. * "Cancel" is a pseudo-state that means "cancel pending timers,
  432. * but remain in your current state".
  433. *
  434. * Cancel MUST NOT occupy a slot in the machine state array.
  435. */
  436. #define CIRCPAD_STATE_CANCEL (CIRCPAD_STATENUM_MAX-2)
  437. /**
  438. * Since we have 3 pseudo-states, the max state array length is
  439. * up to one less than cancel's statenum.
  440. */
  441. #define CIRCPAD_MAX_MACHINE_STATES (CIRCPAD_STATE_CANCEL-1)
  442. /**
  443. * Mutable padding machine info.
  444. *
  445. * This structure contains mutable information about a padding
  446. * machine. The mutable information must be kept separate because
  447. * it exists per-circuit, where as the machines themselves are global.
  448. * This separation is done to conserve space in the circuit structure.
  449. *
  450. * This is the per-circuit state that changes regarding the global state
  451. * machine. Some parts of it are optional (ie NULL).
  452. *
  453. * XXX: Play with layout to minimize space on x64 Linux (most common relay).
  454. */
  455. typedef struct circpad_machine_runtime_t {
  456. /** The callback pointer for the padding callbacks.
  457. *
  458. * These timers stick around the machineinfo until the machineinfo's circuit
  459. * is closed, at which point the timer is cancelled. For this reason it's
  460. * safe to assume that the machineinfo exists if this timer gets
  461. * triggered. */
  462. tor_timer_t *padding_timer;
  463. /** The circuit for this machine */
  464. struct circuit_t *on_circ;
  465. /** A mutable copy of the histogram for the current state.
  466. * NULL if remove_tokens is false for that state */
  467. circpad_hist_token_t *histogram;
  468. /** Length of the above histogram.
  469. * XXX: This field *could* be removed at the expense of added
  470. * complexity+overhead for reaching back into the immutable machine
  471. * state every time we need to inspect the histogram. It's only a byte,
  472. * though, so it seemed worth it.
  473. */
  474. circpad_hist_index_t histogram_len;
  475. /** Remove token from this index upon sending padding */
  476. circpad_hist_index_t chosen_bin;
  477. /** Stop padding/transition if this many cells sent */
  478. uint64_t state_length;
  479. #define CIRCPAD_STATE_LENGTH_INFINITE UINT64_MAX
  480. /** A scaled count of padding packets sent, used to limit padding overhead.
  481. * When this reaches UINT16_MAX, we cut it and nonpadding_sent in half. */
  482. uint16_t padding_sent;
  483. /** A scaled count of non-padding packets sent, used to limit padding
  484. * overhead. When this reaches UINT16_MAX, we cut it and padding_sent in
  485. * half. */
  486. uint16_t nonpadding_sent;
  487. /**
  488. * Timestamp of the most recent cell event (sent, received, padding,
  489. * non-padding), in seconds from approx_time().
  490. *
  491. * Used as an emergency break to stop holding padding circuits open.
  492. */
  493. time_t last_cell_time_sec;
  494. /**
  495. * EWMA estimate of the RTT of the circuit from this hop
  496. * to the exit end, in microseconds. */
  497. circpad_delay_t rtt_estimate_usec;
  498. /**
  499. * The last time we got an event relevant to estimating
  500. * the RTT. Monotonic time in microseconds since system
  501. * start.
  502. */
  503. circpad_time_t last_received_time_usec;
  504. /**
  505. * The time at which we scheduled a non-padding packet,
  506. * or selected an infinite delay.
  507. *
  508. * Monotonic time in microseconds since system start.
  509. * This is 0 if we haven't chosen a padding delay.
  510. */
  511. circpad_time_t padding_scheduled_at_usec;
  512. /** What state is this machine in? */
  513. circpad_statenum_t current_state;
  514. /**
  515. * True if we have scheduled a timer for padding.
  516. *
  517. * This is 1 if a timer is pending. It is 0 if
  518. * no timer is scheduled. (It can be 0 even when
  519. * padding_was_scheduled_at_usec is non-zero).
  520. */
  521. unsigned is_padding_timer_scheduled : 1;
  522. /**
  523. * If this is true, we have seen full duplex behavior.
  524. * Stop updating the RTT.
  525. */
  526. unsigned stop_rtt_update : 1;
  527. /** Max number of padding machines on each circuit. If changed,
  528. * also ensure the machine_index bitwith supports the new size. */
  529. #define CIRCPAD_MAX_MACHINES (2)
  530. /** Which padding machine index was this for.
  531. * (make sure changes to the bitwidth can support the
  532. * CIRCPAD_MAX_MACHINES define). */
  533. unsigned machine_index : 1;
  534. } circpad_machine_runtime_t;
  535. /** Helper macro to get an actual state machine from a machineinfo */
  536. #define CIRCPAD_GET_MACHINE(machineinfo) \
  537. ((machineinfo)->on_circ->padding_machine[(machineinfo)->machine_index])
  538. /**
  539. * This specifies a particular padding machine to use after negotiation.
  540. *
  541. * The constants for machine_num_t are in trunnel.
  542. * We want to be able to define extra numbers in the consensus/torrc, though.
  543. */
  544. typedef uint8_t circpad_machine_num_t;
  545. /** Global state machine structure from the consensus */
  546. typedef struct circpad_machine_spec_t {
  547. /* Just a user-friendly machine name for logs */
  548. const char *name;
  549. /** Global machine number */
  550. circpad_machine_num_t machine_num;
  551. /** Which machine index slot should this machine go into in
  552. * the array on the circuit_t */
  553. unsigned machine_index : 1;
  554. /** Send a padding negotiate to shut down machine at end state? */
  555. unsigned should_negotiate_end : 1;
  556. // These next three fields are origin machine-only...
  557. /** Origin side or relay side */
  558. unsigned is_origin_side : 1;
  559. /** Which hop in the circuit should we send padding to/from?
  560. * 1-indexed (ie: hop #1 is guard, #2 middle, #3 exit). */
  561. unsigned target_hopnum : 3;
  562. /** If this flag is enabled, don't close circuits that use this machine even
  563. * if another part of Tor wants to close this circuit.
  564. *
  565. * If this flag is set, the circuitpadding subsystem will close circuits the
  566. * moment the machine transitions to the END state, and only if the circuit
  567. * has already been asked to be closed by another part of Tor.
  568. *
  569. * Circuits that should have been closed but were kept open by a padding
  570. * machine are re-purposed to CIRCUIT_PURPOSE_C_CIRCUIT_PADDING, hence
  571. * machines should take that purpose into account if they are filtering
  572. * circuits by purpose. */
  573. unsigned manage_circ_lifetime : 1;
  574. /** This machine only kills fascists if the following conditions are met. */
  575. circpad_machine_conditions_t conditions;
  576. /** How many padding cells can be sent before we apply overhead limits?
  577. * XXX: Note that we can only allow up to 64k of padding cells on an
  578. * otherwise quiet circuit. Is this enough? It's 33MB. */
  579. uint16_t allowed_padding_count;
  580. /** Padding percent cap: Stop padding if we exceed this percent overhead.
  581. * 0 means no limit. Overhead is defined as percent of total traffic, so
  582. * that we can use 0..100 here. This is the same definition as used in
  583. * Prop#265. */
  584. uint8_t max_padding_percent;
  585. /** State array: indexed by circpad_statenum_t */
  586. circpad_state_t *states;
  587. /**
  588. * Number of states this machine has (ie: length of the states array).
  589. * XXX: This field is not needed other than for safety. */
  590. circpad_statenum_t num_states;
  591. } circpad_machine_spec_t;
  592. void circpad_new_consensus_params(const networkstatus_t *ns);
  593. int circpad_marked_circuit_for_padding(circuit_t *circ, int reason);
  594. /**
  595. * The following are event call-in points that are of interest to
  596. * the state machines. They are called during cell processing. */
  597. void circpad_deliver_unrecognized_cell_events(struct circuit_t *circ,
  598. cell_direction_t dir);
  599. void circpad_deliver_sent_relay_cell_events(struct circuit_t *circ,
  600. uint8_t relay_command);
  601. void circpad_deliver_recognized_relay_cell_events(struct circuit_t *circ,
  602. uint8_t relay_command,
  603. crypt_path_t *layer_hint);
  604. /** Cell events are delivered by the above delivery functions */
  605. void circpad_cell_event_nonpadding_sent(struct circuit_t *on_circ);
  606. void circpad_cell_event_nonpadding_received(struct circuit_t *on_circ);
  607. void circpad_cell_event_padding_sent(struct circuit_t *on_circ);
  608. void circpad_cell_event_padding_received(struct circuit_t *on_circ);
  609. /** Internal events are events the machines send to themselves */
  610. circpad_decision_t
  611. circpad_internal_event_infinity(circpad_machine_runtime_t *mi);
  612. circpad_decision_t
  613. circpad_internal_event_bins_empty(circpad_machine_runtime_t *);
  614. circpad_decision_t circpad_internal_event_state_length_up(
  615. circpad_machine_runtime_t *);
  616. /** Machine creation events are events that cause us to set up or
  617. * tear down padding state machines. */
  618. void circpad_machine_event_circ_added_hop(struct origin_circuit_t *on_circ);
  619. void circpad_machine_event_circ_built(struct origin_circuit_t *circ);
  620. void circpad_machine_event_circ_purpose_changed(struct origin_circuit_t *circ);
  621. void circpad_machine_event_circ_has_streams(struct origin_circuit_t *circ);
  622. void circpad_machine_event_circ_has_no_streams(struct origin_circuit_t *circ);
  623. void
  624. circpad_machine_event_circ_has_no_relay_early(struct origin_circuit_t *circ);
  625. void circpad_machines_init(void);
  626. void circpad_machines_free(void);
  627. void circpad_register_padding_machine(circpad_machine_spec_t *machine,
  628. smartlist_t *machine_list);
  629. void circpad_machine_states_init(circpad_machine_spec_t *machine,
  630. circpad_statenum_t num_states);
  631. void circpad_circuit_free_all_machineinfos(struct circuit_t *circ);
  632. bool circpad_padding_is_from_expected_hop(struct circuit_t *circ,
  633. crypt_path_t *from_hop);
  634. /** Serializaton functions for writing to/from torrc and consensus */
  635. char *circpad_machine_spec_to_string(const circpad_machine_spec_t *machine);
  636. const circpad_machine_spec_t *circpad_string_to_machine(const char *str);
  637. /* Padding negotiation between client and middle */
  638. signed_error_t circpad_handle_padding_negotiate(struct circuit_t *circ,
  639. struct cell_t *cell);
  640. signed_error_t circpad_handle_padding_negotiated(struct circuit_t *circ,
  641. struct cell_t *cell,
  642. crypt_path_t *layer_hint);
  643. signed_error_t circpad_negotiate_padding(struct origin_circuit_t *circ,
  644. circpad_machine_num_t machine,
  645. uint8_t target_hopnum,
  646. uint8_t command);
  647. bool circpad_padding_negotiated(struct circuit_t *circ,
  648. circpad_machine_num_t machine,
  649. uint8_t command,
  650. uint8_t response);
  651. circpad_purpose_mask_t circpad_circ_purpose_to_mask(uint8_t circ_purpose);
  652. int circpad_check_received_cell(cell_t *cell, circuit_t *circ,
  653. crypt_path_t *layer_hint,
  654. const relay_header_t *rh);
  655. MOCK_DECL(circpad_decision_t,
  656. circpad_machine_schedule_padding,(circpad_machine_runtime_t *));
  657. MOCK_DECL(circpad_decision_t,
  658. circpad_machine_spec_transition, (circpad_machine_runtime_t *mi,
  659. circpad_event_t event));
  660. circpad_decision_t circpad_send_padding_cell_for_callback(
  661. circpad_machine_runtime_t *mi);
  662. void circpad_free_all(void);
  663. #ifdef CIRCUITPADDING_PRIVATE
  664. STATIC void machine_spec_free_(circpad_machine_spec_t *m);
  665. #define machine_spec_free(chan) \
  666. FREE_AND_NULL(circpad_machine_spec_t,machine_spec_free_, (m))
  667. STATIC circpad_delay_t
  668. circpad_machine_sample_delay(circpad_machine_runtime_t *mi);
  669. STATIC bool
  670. circpad_machine_reached_padding_limit(circpad_machine_runtime_t *mi);
  671. STATIC circpad_delay_t
  672. circpad_histogram_bin_to_usec(const circpad_machine_runtime_t *mi,
  673. circpad_hist_index_t bin);
  674. STATIC const circpad_state_t *
  675. circpad_machine_current_state(const circpad_machine_runtime_t *mi);
  676. STATIC void circpad_machine_remove_token(circpad_machine_runtime_t *mi);
  677. STATIC circpad_hist_index_t circpad_histogram_usec_to_bin(
  678. const circpad_machine_runtime_t *mi,
  679. circpad_delay_t us);
  680. STATIC circpad_machine_runtime_t *circpad_circuit_machineinfo_new(
  681. struct circuit_t *on_circ,
  682. int machine_index);
  683. STATIC void circpad_machine_remove_higher_token(circpad_machine_runtime_t *mi,
  684. circpad_delay_t target_bin_us);
  685. STATIC void circpad_machine_remove_lower_token(circpad_machine_runtime_t *mi,
  686. circpad_delay_t target_bin_us);
  687. STATIC void circpad_machine_remove_closest_token(circpad_machine_runtime_t *mi,
  688. circpad_delay_t target_bin_us,
  689. bool use_usec);
  690. STATIC void circpad_machine_setup_tokens(circpad_machine_runtime_t *mi);
  691. MOCK_DECL(STATIC signed_error_t,
  692. circpad_send_command_to_hop,(struct origin_circuit_t *circ, uint8_t hopnum,
  693. uint8_t relay_command, const uint8_t *payload,
  694. ssize_t payload_len));
  695. MOCK_DECL(STATIC const node_t *,
  696. circuit_get_nth_node,(origin_circuit_t *circ, int hop));
  697. STATIC circpad_delay_t
  698. histogram_get_bin_upper_bound(const circpad_machine_runtime_t *mi,
  699. circpad_hist_index_t bin);
  700. STATIC void
  701. circpad_add_matching_machines(origin_circuit_t *on_circ,
  702. smartlist_t *machines_sl);
  703. #ifdef TOR_UNIT_TESTS
  704. extern smartlist_t *origin_padding_machines;
  705. extern smartlist_t *relay_padding_machines;
  706. #endif
  707. #endif /* defined(CIRCUITPADDING_PRIVATE) */
  708. #endif /* !defined(TOR_CIRCUITPADDING_H) */