Tor  0.4.7.0-alpha-dev
circuit_st.h
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file circuit_st.h
9  * @brief Base circuit structure.
10  **/
11 
12 #ifndef CIRCUIT_ST_H
13 #define CIRCUIT_ST_H
14 
15 #include "core/or/or.h"
16 
17 #include "lib/container/handles.h"
18 
19 #include "core/or/cell_queue_st.h"
20 #include "ext/ht.h"
21 
22 struct hs_token_t;
25 
26 /** Number of padding state machines on a circuit. */
27 #define CIRCPAD_MAX_MACHINES (2)
28 
29 /** "magic" value for an origin_circuit_t */
30 #define ORIGIN_CIRCUIT_MAGIC 0x35315243u
31 /** "magic" value for an or_circuit_t */
32 #define OR_CIRCUIT_MAGIC 0x98ABC04Fu
33 /** "magic" value for a circuit that would have been freed by circuit_free,
34  * but which we're keeping around until a cpuworker reply arrives. See
35  * circuit_free() for more documentation. */
36 #define DEAD_CIRCUIT_MAGIC 0xdeadc14c
37 
38 /**
39  * A circuit is a path over the onion routing
40  * network. Applications can connect to one end of the circuit, and can
41  * create exit connections at the other end of the circuit. AP and exit
42  * connections have only one circuit associated with them (and thus these
43  * connection types are closed when the circuit is closed), whereas
44  * OR connections multiplex many circuits at once, and stay standing even
45  * when there are no circuits running over them.
46  *
47  * A circuit_t structure can fill one of two roles. First, a or_circuit_t
48  * links two connections together: either an edge connection and an OR
49  * connection, or two OR connections. (When joined to an OR connection, a
50  * circuit_t affects only cells sent to a particular circID on that
51  * connection. When joined to an edge connection, a circuit_t affects all
52  * data.)
53 
54  * Second, an origin_circuit_t holds the cipher keys and state for sending data
55  * along a given circuit. At the OP, it has a sequence of ciphers, each
56  * of which is shared with a single OR along the circuit. Separate
57  * ciphers are used for data going "forward" (away from the OP) and
58  * "backward" (towards the OP). At the OR, a circuit has only two stream
59  * ciphers: one for data going forward, and one for data going backward.
60  */
61 struct circuit_t {
62  uint32_t magic; /**< For memory and type debugging: must equal
63  * ORIGIN_CIRCUIT_MAGIC or OR_CIRCUIT_MAGIC. */
64 
65  /** Handle entry for handle-based lookup */
67 
68  /** The channel that is next in this circuit. */
70 
71  /**
72  * The circuit_id used in the next (forward) hop of this circuit;
73  * this is unique to n_chan, but this ordered pair is globally
74  * unique:
75  *
76  * (n_chan->global_identifier, n_circ_id)
77  */
79 
80  /** Queue of cells waiting to be transmitted on n_chan */
82 
83  /**
84  * The hop to which we want to extend this circuit. Should be NULL if
85  * the circuit has attached to a channel.
86  */
88 
89  /** True iff we are waiting for n_chan_cells to become less full before
90  * allowing p_streams to add any more cells. (Origin circuit only.) */
91  unsigned int streams_blocked_on_n_chan : 1;
92  /** True iff we are waiting for p_chan_cells to become less full before
93  * allowing n_streams to add any more cells. (OR circuit only.) */
94  unsigned int streams_blocked_on_p_chan : 1;
95 
96  /** True iff we have queued a delete backwards on this circuit, but not put
97  * it on the output buffer. */
98  unsigned int p_delete_pending : 1;
99  /** True iff we have queued a delete forwards on this circuit, but not put
100  * it on the output buffer. */
101  unsigned int n_delete_pending : 1;
102 
103  /** True iff this circuit has received a DESTROY cell in either direction */
104  unsigned int received_destroy : 1;
105 
106  /** True iff we have sent a sufficiently random data cell since last
107  * we reset send_randomness_after_n_cells. */
109 
110  uint8_t state; /**< Current status of this circuit. */
111  uint8_t purpose; /**< Why are we creating this circuit? */
112 
113  /** How many relay data cells can we package (read from edge streams)
114  * on this circuit before we receive a circuit-level sendme cell asking
115  * for more? */
117  /** How many relay data cells will we deliver (write to edge streams)
118  * on this circuit? When deliver_window gets low, we send some
119  * circuit-level sendme cells to indicate that we're willing to accept
120  * more. */
122  /**
123  * How many cells do we have until we need to send one that contains
124  * sufficient randomness? Used to ensure that authenticated SENDME cells
125  * will reflect some unpredictable information.
126  **/
128 
129  /** FIFO containing the digest of the cells that are just before a SENDME is
130  * sent by the client. It is done at the last cell before our package_window
131  * goes down to 0 which is when we expect a SENDME.
132  *
133  * Our current circuit package window is capped to 1000
134  * (CIRCWINDOW_START_MAX) which is also the start value. The increment is
135  * set to 100 (CIRCWINDOW_INCREMENT) which means we don't allow more than
136  * 1000/100 = 10 outstanding SENDME cells worth of data. Meaning that this
137  * list can not contain more than 10 digests of DIGEST_LEN bytes (20).
138  *
139  * At position i in the list, the digest corresponds to the
140  * (CIRCWINDOW_INCREMENT * i)-nth cell received since we expect a SENDME to
141  * be received containing that cell digest.
142  *
143  * For example, position 2 (starting at 0) means that we've received 300
144  * cells so the 300th cell digest is kept at index 2.
145  *
146  * At maximum, this list contains 200 bytes plus the smartlist overhead. */
148 
149  /** Temporary field used during circuits_handle_oom. */
150  uint32_t age_tmp;
151 
152  /** For storage while n_chan is pending (state CIRCUIT_STATE_CHAN_WAIT). */
154 
155  /** When did circuit construction actually begin (ie send the
156  * CREATE cell or begin cannibalization).
157  *
158  * Note: This timer will get reset if we decide to cannibalize
159  * a circuit. It may also get reset during certain phases of hidden
160  * service circuit use.
161  *
162  * We keep this timestamp with a higher resolution than most so that the
163  * circuit-build-time tracking code can get millisecond resolution.
164  */
165  struct timeval timestamp_began;
166 
167  /** This timestamp marks when the init_circuit_base constructor ran. */
168  struct timeval timestamp_created;
169 
170  /** When the circuit was first used, or 0 if the circuit is clean.
171  *
172  * XXXX Note that some code will artificially adjust this value backward
173  * in time in order to indicate that a circuit shouldn't be used for new
174  * streams, but that it can stay alive as long as it has streams on it.
175  * That's a kludge we should fix.
176  *
177  * XXX The CBT code uses this field to record when HS-related
178  * circuits entered certain states. This usage probably won't
179  * interfere with this field's primary purpose, but we should
180  * document it more thoroughly to make sure of that.
181  *
182  * XXX The SocksPort option KeepaliveIsolateSOCKSAuth will artificially
183  * adjust this value forward each time a suitable stream is attached to an
184  * already constructed circuit, potentially keeping the circuit alive
185  * indefinitely.
186  */
188 
189  uint16_t marked_for_close; /**< Should we close this circuit at the end of
190  * the main loop? (If true, holds the line number
191  * where this circuit was marked.) */
192  const char *marked_for_close_file; /**< For debugging: in which file was this
193  * circuit marked for close? */
194  /** For what reason (See END_CIRC_REASON...) is this circuit being closed?
195  * This field is set in circuit_mark_for_close and used later in
196  * circuit_about_to_free. */
198  /** As marked_for_close_reason, but reflects the underlying reason for
199  * closing this circuit.
200  */
202 
203  /** Unique ID for measuring tunneled network status requests. */
204  uint64_t dirreq_id;
205 
206  /** Index in smartlist of all circuits (global_circuitlist). */
208 
209  /** Various statistics about cells being added to or removed from this
210  * circuit's queues; used only if CELL_STATS events are enabled and
211  * cleared after being sent to control port. */
213 
214  /** If set, points to an HS token that this circuit might be carrying.
215  * Used by the HS circuitmap. */
216  struct hs_token_t *hs_token;
217  /** Hashtable node: used to look up the circuit by its HS token using the HS
218  circuitmap. */
219  HT_ENTRY(circuit_t) hs_circuitmap_node;
220 
221  /** Adaptive Padding state machines: these are immutable. The state machines
222  * that come from the consensus are saved to a global structure, to avoid
223  * per-circuit allocations. This merely points to the global copy in
224  * origin_padding_machines or relay_padding_machines that should never
225  * change or get deallocated.
226  *
227  * Each element of this array corresponds to a different padding machine,
228  * and we can have up to CIRCPAD_MAX_MACHINES such machines. */
230 
231  /** Adaptive Padding machine runtime info for above machines. This is
232  * the per-circuit mutable information, such as the current state and
233  * histogram token counts. Some of it is optional (aka NULL).
234  * If a machine is being shut down, these indexes can be NULL
235  * without the corresponding padding_machine being NULL, while we
236  * wait for the other end to respond to our shutdown request.
237  *
238  * Each element of this array corresponds to a different padding machine,
239  * and we can have up to CIRCPAD_MAX_MACHINES such machines. */
241 
242  /** padding_machine_ctr increments each time a new padding machine
243  * is negotiated. It is used for shutdown conditions, to ensure
244  * that STOP commands actually correspond to the current machine,
245  * and not a previous one. */
247 };
248 
249 #endif /* !defined(CIRCUIT_ST_H) */
Cell queue structures.
#define CIRCPAD_MAX_MACHINES
Definition: circuit_st.h:27
Macros for C weak-handle implementation.
Master header file for Tor-specific functionality.
uint32_t circid_t
Definition: or.h:489
uint32_t age_tmp
Definition: circuit_st.h:150
unsigned int p_delete_pending
Definition: circuit_st.h:98
struct hs_token_t * hs_token
Definition: circuit_st.h:216
int marked_for_close_reason
Definition: circuit_st.h:197
uint8_t state
Definition: circuit_st.h:110
int global_circuitlist_idx
Definition: circuit_st.h:207
int marked_for_close_orig_reason
Definition: circuit_st.h:201
uint16_t send_randomness_after_n_cells
Definition: circuit_st.h:127
struct create_cell_t * n_chan_create_cell
Definition: circuit_st.h:153
unsigned int have_sent_sufficiently_random_cell
Definition: circuit_st.h:108
unsigned int streams_blocked_on_n_chan
Definition: circuit_st.h:91
uint64_t dirreq_id
Definition: circuit_st.h:204
time_t timestamp_dirty
Definition: circuit_st.h:187
cell_queue_t n_chan_cells
Definition: circuit_st.h:81
uint32_t magic
Definition: circuit_st.h:62
struct circpad_machine_runtime_t * padding_info[CIRCPAD_MAX_MACHINES]
Definition: circuit_st.h:240
smartlist_t * sendme_last_digests
Definition: circuit_st.h:147
unsigned int received_destroy
Definition: circuit_st.h:104
uint16_t marked_for_close
Definition: circuit_st.h:189
const struct circpad_machine_spec_t * padding_machine[CIRCPAD_MAX_MACHINES]
Definition: circuit_st.h:229
uint8_t purpose
Definition: circuit_st.h:111
const char * marked_for_close_file
Definition: circuit_st.h:192
uint32_t padding_machine_ctr
Definition: circuit_st.h:246
unsigned int n_delete_pending
Definition: circuit_st.h:101
HT_ENTRY(circuit_t) hs_circuitmap_node
struct timeval timestamp_began
Definition: circuit_st.h:165
int deliver_window
Definition: circuit_st.h:121
int package_window
Definition: circuit_st.h:116
HANDLE_ENTRY(circuit, circuit_t)
smartlist_t * testing_cell_stats
Definition: circuit_st.h:212
struct timeval timestamp_created
Definition: circuit_st.h:168
unsigned int streams_blocked_on_p_chan
Definition: circuit_st.h:94
channel_t * n_chan
Definition: circuit_st.h:69
extend_info_t * n_hop
Definition: circuit_st.h:87
circid_t n_circ_id
Definition: circuit_st.h:78