Tor  0.4.8.0-alpha-dev
origin_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 origin_circuit_st.h
9  * @brief Origin circuit structure.
10  **/
11 
12 #ifndef ORIGIN_CIRCUIT_ST_H
13 #define ORIGIN_CIRCUIT_ST_H
14 
15 #include "core/or/or.h"
16 
17 #include "core/or/circuit_st.h"
18 
19 struct onion_queue_t;
20 
21 /**
22  * Describes the circuit building process in simplified terms based
23  * on the path bias accounting state for a circuit.
24  *
25  * NOTE: These state values are enumerated in the order for which we
26  * expect circuits to transition through them. If you add states,
27  * you need to preserve this overall ordering. The various pathbias
28  * state transition and accounting functions (pathbias_mark_* and
29  * pathbias_count_*) contain ordinal comparisons to enforce proper
30  * state transitions for corrections.
31  *
32  * This state machine and the associated logic was created to prevent
33  * miscounting due to unknown cases of circuit reuse. See also tickets
34  * #6475 and #7802.
35  */
37  /** This circuit is "new". It has not yet completed a first hop
38  * or been counted by the path bias code. */
40  /** This circuit has completed one/two hops, and has been counted by
41  * the path bias logic. */
43  /** This circuit has been completely built */
45  /** Did we try to attach any SOCKS streams or hidserv introductions to
46  * this circuit?
47  *
48  * Note: If we ever implement end-to-end stream timing through test
49  * stream probes (#5707), we must *not* set this for those probes
50  * (or any other automatic streams) because the adversary could
51  * just tag at a later point.
52  */
54  /** Did any SOCKS streams or hidserv introductions actually succeed on
55  * this circuit?
56  *
57  * If any streams detach/fail from this circuit, the code transitions
58  * the circuit back to PATH_STATE_USE_ATTEMPTED to ensure we probe. See
59  * pathbias_mark_use_rollback() for that.
60  */
62 
63  /**
64  * This is a special state to indicate that we got a corrupted
65  * relay cell on a circuit and we don't intend to probe it.
66  */
68 
69  /**
70  * This is a special state to indicate that we already counted
71  * the circuit. Used to guard against potential state machine
72  * violations.
73  */
75 };
76 
77 /** An origin_circuit_t holds data necessary to build and use a circuit.
78  */
80  circuit_t base_;
81 
82  /** Linked list of AP streams (or EXIT streams if hidden service)
83  * associated with this circuit. */
85 
86  /** Smartlist of half-closed streams (half_edge_t*) that still
87  * have pending activity */
89 
90  /** Bytes read on this circuit since last call to
91  * control_event_circ_bandwidth_used(). Only used if we're configured
92  * to emit CIRC_BW events. */
93  uint32_t n_read_circ_bw;
94 
95  /** Bytes written to on this circuit since last call to
96  * control_event_circ_bandwidth_used(). Only used if we're configured
97  * to emit CIRC_BW events. */
99 
100  /** Total known-valid relay cell bytes since last call to
101  * control_event_circ_bandwidth_used(). Only used if we're configured
102  * to emit CIRC_BW events. */
104 
105  /** Total written relay cell bytes since last call to
106  * control_event_circ_bandwidth_used(). Only used if we're configured
107  * to emit CIRC_BW events. */
109 
110  /** Total overhead data in all known-valid relay data cells since last
111  * call to control_event_circ_bandwidth_used(). Only used if we're
112  * configured to emit CIRC_BW events. */
114 
115  /** Total written overhead data in all relay data cells since last call to
116  * control_event_circ_bandwidth_used(). Only used if we're configured
117  * to emit CIRC_BW events. */
119 
120  /** Build state for this circuit. It includes the intended path
121  * length, the chosen exit router, rendezvous information, etc.
122  */
124  /** The doubly-linked list of crypt_path_t entries, one per hop,
125  * for this circuit. This includes ciphers for each hop,
126  * integrity-checking digests for each hop, and package/delivery
127  * windows for each hop.
128  */
130 
131  /** Holds hidden service identifier on either client or service side. This
132  * is for both introduction and rendezvous circuit. */
134 
135  /** Holds the data that the entry guard system uses to track the
136  * status of the guard this circuit is using, and thereby to determine
137  * whether this circuit can be used. */
138  struct circuit_guard_state_t *guard_state;
139 
140  /** Index into global_origin_circuit_list for this circuit. -1 if not
141  * present. */
143 
144  /** How many more relay_early cells can we send on this circuit, according
145  * to the specification? */
146  unsigned int remaining_relay_early_cells : 4;
147 
148  /** Set if this circuit is insanely old and we already informed the user */
149  unsigned int is_ancient : 1;
150 
151  /** Set if this circuit has already been opened. Used to detect
152  * cannibalized circuits. */
153  unsigned int has_opened : 1;
154 
155  /**
156  * Path bias state machine. Used to ensure integrity of our
157  * circuit building and usage accounting. See path_state_t
158  * for more details.
159  */
160  path_state_bitfield_t path_state : 3;
161 
162  /* If this flag is set, we should not consider attaching any more
163  * connections to this circuit. */
164  unsigned int unusable_for_new_conns : 1;
165 
166  /* If this flag is set (due to padding negotiation failure), we should
167  * not try to negotiate further circuit padding. */
168  unsigned padding_negotiation_failed : 1;
169 
170  /**
171  * If this flag is set, then a controller chose the first hop of this
172  * circuit's path, and it's okay to ignore checks that we'd usually do
173  * on this circuit's first hop.
174  *
175  * This flag is distinct from the CIRCUIT_PURPOSE_CONTROLLER purpose: the
176  * purpose indicates _what tor can use the circuit for_. Controller-created
177  * circuits can still have the CIRCUIT_PURPOSE_GENERAL purpose if Tor is
178  * allowed to attach streams to them.
179  */
181 
182  /**
183  * If true, this circuit's path has been chosen, in full or in part,
184  * by the controller API, and it's okay to ignore checks that we'd
185  * usually do on the path as whole. */
186  unsigned int any_hop_from_controller : 1;
187 
188  /**
189  * Tristate variable to guard against pathbias miscounting
190  * due to circuit purpose transitions changing the decision
191  * of pathbias_should_count(). This variable is informational
192  * only. The current results of pathbias_should_count() are
193  * the official decision for pathbias accounting.
194  */
196 #define PATHBIAS_SHOULDCOUNT_UNDECIDED 0
197 #define PATHBIAS_SHOULDCOUNT_IGNORED 1
198 #define PATHBIAS_SHOULDCOUNT_COUNTED 2
199 
200  /** For path probing. Store the temporary probe stream ID
201  * for response comparison */
203 
204  /** For path probing. Store the temporary probe address nonce
205  * (in host byte order) for response comparison. */
207 
208  /** Set iff this circuit has been given a relaxed timeout because
209  * no circuits have opened. Used to prevent spamming logs. */
210  unsigned int relaxed_timeout : 1;
211 
212  /** What commands were sent over this circuit that decremented the
213  * RELAY_EARLY counter? This is for debugging task 878. */
215 
216  /** How many RELAY_EARLY cells have been sent over this circuit? This is
217  * for debugging task 878, too. */
219 
220  /** The next stream_id that will be tried when we're attempting to
221  * construct a new AP stream originating at this circuit. */
223 
224  /* The intro key replaces the hidden service's public key if purpose is
225  * S_ESTABLISH_INTRO or S_INTRO, provided that no unversioned rendezvous
226  * descriptor is used. */
227  crypto_pk_t *intro_key;
228 
229  /** Quasi-global identifier for this circuit; used for control.c */
230  /* XXXX NM This can get re-used after 2**32 circuits. */
232 
233  /** True if we have associated one stream to this circuit, thereby setting
234  * the isolation parameters for this circuit. Note that this doesn't
235  * necessarily mean that we've <em>attached</em> any streams to the circuit:
236  * we may only have marked up this circuit during the launch process.
237  */
238  unsigned int isolation_values_set : 1;
239  /** True iff any stream has <em>ever</em> been attached to this circuit.
240  *
241  * In a better world we could use timestamp_dirty for this, but
242  * timestamp_dirty is far too overloaded at the moment.
243  */
245 
246  /** A bitfield of ISO_* flags for every isolation field such that this
247  * circuit has had streams with more than one value for that field
248  * attached to it. */
250 
251  /** @name Isolation parameters
252  *
253  * If any streams have been associated with this circ (isolation_values_set
254  * == 1), and all streams associated with the circuit have had the same
255  * value for some field ((isolation_flags_mixed & ISO_FOO) == 0), then these
256  * elements hold the value for that field.
257  *
258  * Note again that "associated" is not the same as "attached": we
259  * preliminarily associate streams with a circuit while the circuit is being
260  * launched, so that we can tell whether we need to launch more circuits.
261  *
262  * @{
263  */
264  uint8_t client_proto_type;
265  uint8_t client_proto_socksver;
266  uint16_t dest_port;
267  tor_addr_t client_addr;
268  char *dest_address;
269  int session_group;
270  unsigned nym_epoch;
271  size_t socks_username_len;
272  uint8_t socks_password_len;
273  /* Note that the next two values are NOT NUL-terminated; see
274  socks_username_len and socks_password_len for their lengths. */
275  char *socks_username;
276  char *socks_password;
277  /** Global identifier for the first stream attached here; used by
278  * ISO_STREAM. */
280  /**@}*/
281  /** A list of addr_policy_t for this circuit in particular. Used by
282  * adjust_exit_policy_from_exitpolicy_failure.
283  */
285 
286  /** How long do we wait before closing this circuit if it remains
287  * completely idle after it was built, in seconds? This value
288  * is randomized on a per-circuit basis from CircuitsAvailableTimoeut
289  * to 2*CircuitsAvailableTimoeut. */
291 
292 };
293 
294 #endif /* !defined(ORIGIN_CIRCUIT_ST_H) */
Base circuit structure.
Master header file for Tor-specific functionality.
#define MAX_RELAY_EARLY_CELLS_PER_CIRCUIT
Definition: or.h:825
uint16_t streamid_t
Definition: or.h:490
path_state_t
@ PATH_STATE_ALREADY_COUNTED
@ PATH_STATE_USE_FAILED
@ PATH_STATE_BUILD_ATTEMPTED
@ PATH_STATE_BUILD_SUCCEEDED
@ PATH_STATE_USE_SUCCEEDED
@ PATH_STATE_NEW_CIRC
@ PATH_STATE_USE_ATTEMPTED
uint64_t associated_isolated_stream_global_id
uint32_t n_written_circ_bw
unsigned int relaxed_timeout
struct hs_ident_circuit_t * hs_ident
edge_connection_t * p_streams
uint8_t relay_early_commands[MAX_RELAY_EARLY_CELLS_PER_CIRCUIT]
unsigned int isolation_values_set
unsigned int remaining_relay_early_cells
uint32_t n_overhead_written_circ_bw
unsigned int has_opened
path_state_bitfield_t path_state
unsigned int is_ancient
uint32_t pathbias_probe_nonce
unsigned int any_hop_from_controller
uint32_t n_delivered_read_circ_bw
uint32_t n_delivered_written_circ_bw
uint32_t n_overhead_read_circ_bw
smartlist_t * prepend_policy
crypt_path_t * cpath
streamid_t pathbias_probe_id
cpath_build_state_t * build_state
unsigned int isolation_any_streams_attached
struct circuit_guard_state_t * guard_state
streamid_t next_stream_id
smartlist_t * half_streams
unsigned first_hop_from_controller