circuitpadding.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. typedef enum {
  179. CIRCPAD_DIST_NONE = 0,
  180. CIRCPAD_DIST_UNIFORM = 1,
  181. CIRCPAD_DIST_LOGISTIC = 2,
  182. CIRCPAD_DIST_LOG_LOGISTIC = 3,
  183. CIRCPAD_DIST_GEOMETRIC = 4,
  184. CIRCPAD_DIST_WEIBULL = 5,
  185. CIRCPAD_DIST_PARETO = 6
  186. } circpad_distribution_type_t;
  187. /**
  188. * Distribution information.
  189. *
  190. * This type specifies a specific distribution above, as well as
  191. * up to two parameters for that distribution. The specific
  192. * per-distribution meaning of these parameters is specified
  193. * in circpad_distribution_sample().
  194. */
  195. typedef struct circpad_distribution_t {
  196. circpad_distribution_type_t type;
  197. double param1;
  198. double param2;
  199. } circpad_distribution_t;
  200. /** State number type. Represents current state of state machine. */
  201. typedef uint16_t circpad_statenum_t;
  202. #define CIRCPAD_STATENUM_MAX (UINT16_MAX)
  203. /** A histogram is used to sample padding delays given a machine state. This
  204. * constant defines the maximum histogram width (i.e. the max number of bins)
  205. *
  206. * Each histogram bin is twice as large as the previous. Two exceptions: The
  207. * first bin has zero width (which means that minimum delay is applied to the
  208. * next padding cell), and the last bin (infinity bin) has infinite width
  209. * (which means that the next padding cell will be delayed infinitely). */
  210. #define CIRCPAD_MAX_HISTOGRAM_LEN (sizeof(circpad_delay_t)*8 + 1)
  211. /**
  212. * A state of a padding state machine. The information here are immutable and
  213. * represent the initial form of the state; it does not get updated as things
  214. * happen. The mutable information that gets updated in runtime are carried in
  215. * a circpad_machine_state_t.
  216. *
  217. * This struct describes the histograms and parameters of a single
  218. * state in the adaptive padding machine. Instances of this struct
  219. * exist in global circpad machine definitions that come from torrc
  220. * or the consensus.
  221. */
  222. typedef struct circpad_state_t {
  223. /** If a histogram is used for this state, this specifies the number of bins
  224. * of this histogram. Histograms must have at least 2 bins.
  225. *
  226. * If a delay probability distribution is used for this state, this is set
  227. * to 0. */
  228. circpad_hist_index_t histogram_len;
  229. /** The histogram itself: an array of uint16s of tokens, whose
  230. * widths are exponentially spaced, in microseconds */
  231. circpad_hist_token_t histogram[CIRCPAD_MAX_HISTOGRAM_LEN];
  232. /** Total number of tokens in this histogram. This is a constant and is *not*
  233. * decremented every time we spend a token. It's used for initializing and
  234. * refilling the histogram. */
  235. uint32_t histogram_total_tokens;
  236. /** Minimum padding delay of this state in microseconds.
  237. *
  238. * If histograms are used, this is the left (and right) bound of the first
  239. * bin (since it has zero width).
  240. *
  241. * If a delay probability distribution is used, this represents the minimum
  242. * delay we can sample from the distribution.
  243. */
  244. circpad_delay_t start_usec;
  245. /** If histograms are used, this is the width of the whole histogram in
  246. * microseconds, and it's used to calculate individual bin width.
  247. *
  248. * If a delay probability distribution is used, this is used as the max
  249. * delay we can sample from the distribution.
  250. */
  251. circpad_delay_t range_usec;
  252. /**
  253. * Represents a delay probability distribution (aka IAT distribution). It's a
  254. * parametrized way of encoding inter-packet delay information in
  255. * microseconds. It can be used instead of histograms.
  256. *
  257. * If it is used, token_removal below must be set to
  258. * CIRCPAD_TOKEN_REMOVAL_NONE.
  259. *
  260. * Start_usec, range_sec, and rtt_estimates are still applied to the
  261. * results of sampling from this distribution (range_sec is used as a max).
  262. */
  263. circpad_distribution_t iat_dist;
  264. /**
  265. * The length dist is a parameterized way of encoding how long this
  266. * state machine runs in terms of sent padding cells or all
  267. * sent cells. Values are sampled from this distribution, clamped
  268. * to max_len, and then start_len is added to that value.
  269. *
  270. * It may be specified instead of or in addition to
  271. * the infinity bins and bins empty conditions. */
  272. circpad_distribution_t length_dist;
  273. /** A minimum length value, added to the output of length_dist */
  274. uint16_t start_length;
  275. /** A cap on the length value that can be sampled from the length_dist */
  276. uint64_t max_length;
  277. /** Should we decrement length when we see a nonpadding packet?
  278. * XXX: Are there any machines that actually want to set this to 0? There may
  279. * not be. OTOH, it's only a bit.. */
  280. unsigned length_includes_nonpadding : 1;
  281. /**
  282. * This is an array that specifies the next state to transition to upon
  283. * receipt an event matching the indicated array index.
  284. *
  285. * This aborts our scheduled packet and switches to the state
  286. * corresponding to the index of the array. Tokens are filled upon
  287. * this transition.
  288. *
  289. * States are allowed to transition to themselves, which means re-schedule
  290. * a new padding timer. They are also allowed to temporarily "transition"
  291. * to the "IGNORE" and "CANCEL" pseudo-states. See #defines below
  292. * for details on state behavior and meaning.
  293. */
  294. circpad_statenum_t next_state[CIRCPAD_NUM_EVENTS];
  295. /**
  296. * If true, estimate the RTT from this relay to the exit/website and add that
  297. * to start_usec for use as the histogram bin 0 start delay.
  298. *
  299. * Right now this is only supported for relay-side state machines.
  300. */
  301. unsigned use_rtt_estimate : 1;
  302. /** This specifies the token removal strategy to use upon padding and
  303. * non-padding activity. */
  304. circpad_removal_t token_removal;
  305. } circpad_state_t;
  306. /**
  307. * The start state for this machine.
  308. *
  309. * In the original WTF-PAD, this is only used for transition to/from
  310. * the burst state. All other fields are not used. But to simplify the
  311. * code we've made it a first-class state. This has no performance
  312. * consequences, but may make naive serialization of the state machine
  313. * large, if we're not careful about how we represent empty fields.
  314. */
  315. #define CIRCPAD_STATE_START 0
  316. /**
  317. * The burst state for this machine.
  318. *
  319. * In the original Adaptive Padding algorithm and in WTF-PAD
  320. * (https://www.freehaven.net/anonbib/cache/ShWa-Timing06.pdf and
  321. * https://www.cs.kau.se/pulls/hot/thebasketcase-wtfpad/), the burst
  322. * state serves to detect bursts in traffic. This is done by using longer
  323. * delays in its histogram, which represent the expected delays between
  324. * bursts of packets in the target stream. If this delay expires without a
  325. * real packet being sent, the burst state sends a padding packet and then
  326. * immediately transitions to the gap state, which is used to generate
  327. * a synthetic padding packet train. In this implementation, this transition
  328. * needs to be explicitly specified in the burst state's transition events.
  329. *
  330. * Because of this flexibility, other padding mechanisms can transition
  331. * between these two states arbitrarily, to encode other dynamics of
  332. * target traffic.
  333. */
  334. #define CIRCPAD_STATE_BURST 1
  335. /**
  336. * The gap state for this machine.
  337. *
  338. * In the original Adaptive Padding algorithm and in WTF-PAD, the gap
  339. * state serves to simulate an artificial packet train composed of padding
  340. * packets. It does this by specifying much lower inter-packet delays than
  341. * the burst state, and transitioning back to itself after padding is sent
  342. * if these timers expire before real traffic is sent. If real traffic is
  343. * sent, it transitions back to the burst state.
  344. *
  345. * Again, in this implementation, these transitions must be specified
  346. * explicitly, and other transitions are also permitted.
  347. */
  348. #define CIRCPAD_STATE_GAP 2
  349. /**
  350. * End is a pseudo-state that causes the machine to go completely
  351. * idle, and optionally get torn down (depending on the
  352. * value of circpad_machine_spec_t.should_negotiate_end)
  353. *
  354. * End MUST NOT occupy a slot in the machine state array.
  355. */
  356. #define CIRCPAD_STATE_END CIRCPAD_STATENUM_MAX
  357. /**
  358. * "Ignore" is a pseudo-state that means "do not react to this
  359. * event".
  360. *
  361. * "Ignore" MUST NOT occupy a slot in the machine state array.
  362. */
  363. #define CIRCPAD_STATE_IGNORE (CIRCPAD_STATENUM_MAX-1)
  364. /**
  365. * "Cancel" is a pseudo-state that means "cancel pending timers,
  366. * but remain in your current state".
  367. *
  368. * Cancel MUST NOT occupy a slot in the machine state array.
  369. */
  370. #define CIRCPAD_STATE_CANCEL (CIRCPAD_STATENUM_MAX-2)
  371. /**
  372. * Since we have 3 pseudo-states, the max state array length is
  373. * up to one less than cancel's statenum.
  374. */
  375. #define CIRCPAD_MAX_MACHINE_STATES (CIRCPAD_STATE_CANCEL-1)
  376. /**
  377. * Mutable padding machine info.
  378. *
  379. * This structure contains mutable information about a padding
  380. * machine. The mutable information must be kept separate because
  381. * it exists per-circuit, where as the machines themselves are global.
  382. * This separation is done to conserve space in the circuit structure.
  383. *
  384. * This is the per-circuit state that changes regarding the global state
  385. * machine. Some parts of it are optional (ie NULL).
  386. *
  387. * XXX: Play with layout to minimize space on x64 Linux (most common relay).
  388. */
  389. typedef struct circpad_machine_state_t {
  390. /** The callback pointer for the padding callbacks.
  391. *
  392. * These timers stick around the machineinfo until the machineinfo's circuit
  393. * is closed, at which point the timer is cancelled. For this reason it's
  394. * safe to assume that the machineinfo exists if this timer gets
  395. * triggered. */
  396. tor_timer_t *padding_timer;
  397. /** The circuit for this machine */
  398. struct circuit_t *on_circ;
  399. /** A mutable copy of the histogram for the current state.
  400. * NULL if remove_tokens is false for that state */
  401. circpad_hist_token_t *histogram;
  402. /** Length of the above histogram.
  403. * XXX: This field *could* be removed at the expense of added
  404. * complexity+overhead for reaching back into the immutable machine
  405. * state every time we need to inspect the histogram. It's only a byte,
  406. * though, so it seemed worth it.
  407. */
  408. circpad_hist_index_t histogram_len;
  409. /** Remove token from this index upon sending padding */
  410. circpad_hist_index_t chosen_bin;
  411. /** Stop padding/transition if this many cells sent */
  412. uint64_t state_length;
  413. #define CIRCPAD_STATE_LENGTH_INFINITE UINT64_MAX
  414. /** A scaled count of padding packets sent, used to limit padding overhead.
  415. * When this reaches UINT16_MAX, we cut it and nonpadding_sent in half. */
  416. uint16_t padding_sent;
  417. /** A scaled count of non-padding packets sent, used to limit padding
  418. * overhead. When this reaches UINT16_MAX, we cut it and padding_sent in
  419. * half. */
  420. uint16_t nonpadding_sent;
  421. /**
  422. * EWMA estimate of the RTT of the circuit from this hop
  423. * to the exit end, in microseconds. */
  424. circpad_delay_t rtt_estimate_usec;
  425. /**
  426. * The last time we got an event relevant to estimating
  427. * the RTT. Monotonic time in microseconds since system
  428. * start.
  429. */
  430. circpad_time_t last_received_time_usec;
  431. /**
  432. * The time at which we scheduled a non-padding packet,
  433. * or selected an infinite delay.
  434. *
  435. * Monotonic time in microseconds since system start.
  436. * This is 0 if we haven't chosen a padding delay.
  437. */
  438. circpad_time_t padding_scheduled_at_usec;
  439. /** What state is this machine in? */
  440. circpad_statenum_t current_state;
  441. /**
  442. * True if we have scheduled a timer for padding.
  443. *
  444. * This is 1 if a timer is pending. It is 0 if
  445. * no timer is scheduled. (It can be 0 even when
  446. * padding_was_scheduled_at_usec is non-zero).
  447. */
  448. unsigned is_padding_timer_scheduled : 1;
  449. /**
  450. * If this is true, we have seen full duplex behavior.
  451. * Stop updating the RTT.
  452. */
  453. unsigned stop_rtt_update : 1;
  454. /** Max number of padding machines on each circuit. If changed,
  455. * also ensure the machine_index bitwith supports the new size. */
  456. #define CIRCPAD_MAX_MACHINES (2)
  457. /** Which padding machine index was this for.
  458. * (make sure changes to the bitwidth can support the
  459. * CIRCPAD_MAX_MACHINES define). */
  460. unsigned machine_index : 1;
  461. } circpad_machine_state_t;
  462. /** Helper macro to get an actual state machine from a machineinfo */
  463. #define CIRCPAD_GET_MACHINE(machineinfo) \
  464. ((machineinfo)->on_circ->padding_machine[(machineinfo)->machine_index])
  465. /**
  466. * This specifies a particular padding machine to use after negotiation.
  467. *
  468. * The constants for machine_num_t are in trunnel.
  469. * We want to be able to define extra numbers in the consensus/torrc, though.
  470. */
  471. typedef uint8_t circpad_machine_num_t;
  472. /** Global state machine structure from the consensus */
  473. typedef struct circpad_machine_spec_t {
  474. /** Global machine number */
  475. circpad_machine_num_t machine_num;
  476. /** Which machine index slot should this machine go into in
  477. * the array on the circuit_t */
  478. unsigned machine_index : 1;
  479. /** Send a padding negotiate to shut down machine at end state? */
  480. unsigned should_negotiate_end : 1;
  481. // These next three fields are origin machine-only...
  482. /** Origin side or relay side */
  483. unsigned is_origin_side : 1;
  484. /** Which hop in the circuit should we send padding to/from?
  485. * 1-indexed (ie: hop #1 is guard, #2 middle, #3 exit). */
  486. unsigned target_hopnum : 3;
  487. /** This machine only kills fascists if the following conditions are met. */
  488. circpad_machine_conditions_t conditions;
  489. /** How many padding cells can be sent before we apply overhead limits?
  490. * XXX: Note that we can only allow up to 64k of padding cells on an
  491. * otherwise quiet circuit. Is this enough? It's 33MB. */
  492. uint16_t allowed_padding_count;
  493. /** Padding percent cap: Stop padding if we exceed this percent overhead.
  494. * 0 means no limit. Overhead is defined as percent of total traffic, so
  495. * that we can use 0..100 here. This is the same definition as used in
  496. * Prop#265. */
  497. uint8_t max_padding_percent;
  498. /** State array: indexed by circpad_statenum_t */
  499. circpad_state_t *states;
  500. /**
  501. * Number of states this machine has (ie: length of the states array).
  502. * XXX: This field is not needed other than for safety. */
  503. circpad_statenum_t num_states;
  504. } circpad_machine_spec_t;
  505. void circpad_new_consensus_params(const networkstatus_t *ns);
  506. /**
  507. * The following are event call-in points that are of interest to
  508. * the state machines. They are called during cell processing. */
  509. void circpad_deliver_unrecognized_cell_events(struct circuit_t *circ,
  510. cell_direction_t dir);
  511. void circpad_deliver_sent_relay_cell_events(struct circuit_t *circ,
  512. uint8_t relay_command);
  513. void circpad_deliver_recognized_relay_cell_events(struct circuit_t *circ,
  514. uint8_t relay_command,
  515. crypt_path_t *layer_hint);
  516. /** Cell events are delivered by the above delivery functions */
  517. void circpad_cell_event_nonpadding_sent(struct circuit_t *on_circ);
  518. void circpad_cell_event_nonpadding_received(struct circuit_t *on_circ);
  519. void circpad_cell_event_padding_sent(struct circuit_t *on_circ);
  520. void circpad_cell_event_padding_received(struct circuit_t *on_circ);
  521. /** Internal events are events the machines send to themselves */
  522. circpad_decision_t
  523. circpad_internal_event_infinity(circpad_machine_state_t *mi);
  524. circpad_decision_t
  525. circpad_internal_event_bins_empty(circpad_machine_state_t *);
  526. circpad_decision_t circpad_internal_event_state_length_up(
  527. circpad_machine_state_t *);
  528. /** Machine creation events are events that cause us to set up or
  529. * tear down padding state machines. */
  530. void circpad_machine_event_circ_added_hop(struct origin_circuit_t *on_circ);
  531. void circpad_machine_event_circ_built(struct origin_circuit_t *circ);
  532. void circpad_machine_event_circ_purpose_changed(struct origin_circuit_t *circ);
  533. void circpad_machine_event_circ_has_streams(struct origin_circuit_t *circ);
  534. void circpad_machine_event_circ_has_no_streams(struct origin_circuit_t *circ);
  535. void
  536. circpad_machine_event_circ_has_no_relay_early(struct origin_circuit_t *circ);
  537. void circpad_machines_init(void);
  538. void circpad_machines_free(void);
  539. void circpad_machine_states_init(circpad_machine_spec_t *machine,
  540. circpad_statenum_t num_states);
  541. void circpad_circuit_free_all_machineinfos(struct circuit_t *circ);
  542. bool circpad_padding_is_from_expected_hop(struct circuit_t *circ,
  543. crypt_path_t *from_hop);
  544. /** Serializaton functions for writing to/from torrc and consensus */
  545. char *circpad_machine_spec_to_string(const circpad_machine_spec_t *machine);
  546. const circpad_machine_spec_t *circpad_string_to_machine(const char *str);
  547. /* Padding negotiation between client and middle */
  548. signed_error_t circpad_handle_padding_negotiate(struct circuit_t *circ,
  549. struct cell_t *cell);
  550. signed_error_t circpad_handle_padding_negotiated(struct circuit_t *circ,
  551. struct cell_t *cell,
  552. crypt_path_t *layer_hint);
  553. signed_error_t circpad_negotiate_padding(struct origin_circuit_t *circ,
  554. circpad_machine_num_t machine,
  555. uint8_t target_hopnum,
  556. uint8_t command);
  557. bool circpad_padding_negotiated(struct circuit_t *circ,
  558. circpad_machine_num_t machine,
  559. uint8_t command,
  560. uint8_t response);
  561. MOCK_DECL(circpad_decision_t,
  562. circpad_machine_schedule_padding,(circpad_machine_state_t *));
  563. MOCK_DECL(circpad_decision_t,
  564. circpad_machine_spec_transition, (circpad_machine_state_t *mi,
  565. circpad_event_t event));
  566. circpad_decision_t circpad_send_padding_cell_for_callback(
  567. circpad_machine_state_t *mi);
  568. #ifdef CIRCUITPADDING_PRIVATE
  569. STATIC circpad_delay_t
  570. circpad_machine_sample_delay(circpad_machine_state_t *mi);
  571. STATIC bool
  572. circpad_machine_reached_padding_limit(circpad_machine_state_t *mi);
  573. STATIC
  574. circpad_decision_t circpad_machine_remove_token(circpad_machine_state_t *mi);
  575. STATIC circpad_delay_t
  576. circpad_histogram_bin_to_usec(const circpad_machine_state_t *mi,
  577. circpad_hist_index_t bin);
  578. STATIC const circpad_state_t *
  579. circpad_machine_current_state(const circpad_machine_state_t *mi);
  580. STATIC circpad_hist_index_t circpad_histogram_usec_to_bin(
  581. const circpad_machine_state_t *mi,
  582. circpad_delay_t us);
  583. STATIC circpad_machine_state_t *circpad_circuit_machineinfo_new(
  584. struct circuit_t *on_circ,
  585. int machine_index);
  586. STATIC void circpad_machine_remove_higher_token(circpad_machine_state_t *mi,
  587. circpad_delay_t target_bin_us);
  588. STATIC void circpad_machine_remove_lower_token(circpad_machine_state_t *mi,
  589. circpad_delay_t target_bin_us);
  590. STATIC void circpad_machine_remove_closest_token(circpad_machine_state_t *mi,
  591. circpad_delay_t target_bin_us,
  592. bool use_usec);
  593. STATIC void circpad_machine_setup_tokens(circpad_machine_state_t *mi);
  594. MOCK_DECL(STATIC signed_error_t,
  595. circpad_send_command_to_hop,(struct origin_circuit_t *circ, uint8_t hopnum,
  596. uint8_t relay_command, const uint8_t *payload,
  597. ssize_t payload_len));
  598. #ifdef TOR_UNIT_TESTS
  599. extern smartlist_t *origin_padding_machines;
  600. extern smartlist_t *relay_padding_machines;
  601. #endif
  602. #endif
  603. #endif