circuitpadding.h 29 KB

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