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 */
9 * \brief Master header file for Tor-specific functionality.
16 #include "lib/cc/torint.h"
25 #include "lib/arch/bytes.h"
26 #include "lib/cc/compat_compiler.h"
27 #include "lib/container/map.h"
28 #include "lib/buf/buffers.h"
29 #include "lib/container/smartlist.h"
30 #include "lib/crypt_ops/crypto_cipher.h"
31 #include "lib/crypt_ops/crypto_rsa.h"
32 #include "lib/ctime/di_ops.h"
33 #include "lib/defs/dh_sizes.h"
34 #include "lib/encoding/binascii.h"
35 #include "lib/encoding/cstring.h"
36 #include "lib/encoding/time_fmt.h"
37 #include "lib/err/torerr.h"
38 #include "lib/fs/dir.h"
39 #include "lib/fs/files.h"
40 #include "lib/fs/mmap.h"
41 #include "lib/fs/path.h"
42 #include "lib/fs/userdb.h"
43 #include "lib/geoip/country.h"
44 #include "lib/intmath/addsub.h"
45 #include "lib/intmath/bits.h"
46 #include "lib/intmath/cmp.h"
47 #include "lib/intmath/logic.h"
48 #include "lib/intmath/muldiv.h"
49 #include "lib/log/escape.h"
50 #include "lib/log/ratelim.h"
51 #include "lib/log/util_bug.h"
52 #include "lib/malloc/malloc.h"
53 #include "lib/net/address.h"
54 #include "lib/net/inaddr.h"
55 #include "lib/net/socket.h"
56 #include "lib/string/compat_ctype.h"
57 #include "lib/string/compat_string.h"
58 #include "lib/string/parse_int.h"
59 #include "lib/string/printf.h"
60 #include "lib/string/scanf.h"
61 #include "lib/string/util_string.h"
62 #include "lib/testsupport/testsupport.h"
63 #include "lib/thread/threads.h"
64 #include "lib/time/compat_time.h"
65 #include "lib/wallclock/approx_time.h"
66 #include "lib/wallclock/timeval.h"
70 // These, more than other includes, are for keeping the other struct
71 // definitions working. We should remove them when we minimize our includes.
72 #include "core/or/entry_port_cfg_st.h"
74 struct ed25519_public_key_t
;
75 struct curve25519_public_key_t
;
77 /* These signals are defined to help handle_control_signal work.
94 /* Controller signals start at a high number so we don't
95 * conflict with system-defined signals. */
97 #define SIGCLEARDNSCACHE 130
98 #define SIGHEARTBEAT 131
100 #define SIGDORMANT 133
102 #if (SIZEOF_CELL_T != 0)
103 /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
104 * that our stuff always calls cell_t something different. */
105 #define cell_t tor_cell_t
108 /** Helper macro: Given a pointer to to.base_, of type from*, return &to. */
109 #define DOWNCAST(to, ptr) ((to*)SUBTYPE_P(ptr, to, base_))
111 /** Length of longest allowable configured nickname. */
112 #define MAX_NICKNAME_LEN 19
113 /** Length of a router identity encoded as a hexadecimal digest, plus
114 * possible dollar sign. */
115 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
116 /** Maximum length of verbose router identifier: dollar sign, hex ID digest,
117 * equal sign or tilde, nickname. */
118 #define MAX_VERBOSE_NICKNAME_LEN (1+HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN)
120 /** For HTTP parsing: Maximum number of bytes we'll accept in the headers
121 * of an HTTP request or response. */
122 #define MAX_HEADERS_SIZE 50000
124 /** Maximum size, in bytes, of a single router descriptor uploaded to us
125 * as a directory authority. Caches and clients fetch whatever descriptors
126 * the authorities tell them to fetch, and don't care about size. */
127 #define MAX_DESCRIPTOR_UPLOAD_SIZE 20000
129 /** Maximum size of a single extrainfo document, as above. */
130 #define MAX_EXTRAINFO_UPLOAD_SIZE 50000
132 /** Minimum lifetime for an onion key in days. */
133 #define MIN_ONION_KEY_LIFETIME_DAYS (1)
135 /** Maximum lifetime for an onion key in days. */
136 #define MAX_ONION_KEY_LIFETIME_DAYS (90)
138 /** Default lifetime for an onion key in days. */
139 #define DEFAULT_ONION_KEY_LIFETIME_DAYS (28)
141 /** Minimum grace period for acceptance of an onion key in days.
142 * The maximum value is defined in proposal #274 as being the current network
143 * consensus parameter for "onion-key-rotation-days". */
144 #define MIN_ONION_KEY_GRACE_PERIOD_DAYS (1)
146 /** Default grace period for acceptance of an onion key in days. */
147 #define DEFAULT_ONION_KEY_GRACE_PERIOD_DAYS (7)
149 /** How often we should check the network consensus if it is time to rotate or
150 * expire onion keys. */
151 #define ONION_KEY_CONSENSUS_CHECK_INTERVAL (60*60)
153 /** How often do we rotate TLS contexts? */
154 #define MAX_SSL_KEY_LIFETIME_INTERNAL (2*60*60)
156 /** How old do we allow a router to get before removing it
157 * from the router list? In seconds. */
158 #define ROUTER_MAX_AGE (60*60*48)
159 /** How old can a router get before we (as a server) will no longer
160 * consider it live? In seconds. */
161 #define ROUTER_MAX_AGE_TO_PUBLISH (60*60*24)
162 /** How old do we let a saved descriptor get before force-removing it? */
163 #define OLD_ROUTER_DESC_MAX_AGE (60*60*24*5)
165 /* Proxy client types */
167 #define PROXY_CONNECT 1
168 #define PROXY_SOCKS4 2
169 #define PROXY_SOCKS5 3
170 #define PROXY_HAPROXY 4
171 /* !!!! If there is ever a PROXY_* type over 7, we must grow the proxy_type
172 * field in or_connection_t */
174 /* Pluggable transport proxy type. Don't use this in or_connection_t,
175 * instead use the actual underlying proxy type (see above). */
176 #define PROXY_PLUGGABLE 5
178 /** How many circuits do we want simultaneously in-progress to handle
180 #define MIN_CIRCUITS_HANDLING_STREAM 2
182 /* These RELAY_COMMAND constants define values for relay cell commands, and
183 * must match those defined in tor-spec.txt. */
184 #define RELAY_COMMAND_BEGIN 1
185 #define RELAY_COMMAND_DATA 2
186 #define RELAY_COMMAND_END 3
187 #define RELAY_COMMAND_CONNECTED 4
189 #define RELAY_COMMAND_SENDME 5
190 #define RELAY_COMMAND_EXTEND 6
191 #define RELAY_COMMAND_EXTENDED 7
192 #define RELAY_COMMAND_TRUNCATE 8
193 #define RELAY_COMMAND_TRUNCATED 9
194 #define RELAY_COMMAND_DROP 10
196 #define RELAY_COMMAND_RESOLVE 11
197 #define RELAY_COMMAND_RESOLVED 12
199 #define RELAY_COMMAND_BEGIN_DIR 13
200 #define RELAY_COMMAND_EXTEND2 14
201 #define RELAY_COMMAND_EXTENDED2 15
204 #define RELAY_COMMAND_CONFLUX_LINK 19
205 #define RELAY_COMMAND_CONFLUX_LINKED 20
206 #define RELAY_COMMAND_CONFLUX_LINKED_ACK 21
207 #define RELAY_COMMAND_CONFLUX_SWITCH 22
209 #define RELAY_COMMAND_ESTABLISH_INTRO 32
210 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
211 #define RELAY_COMMAND_INTRODUCE1 34
212 #define RELAY_COMMAND_INTRODUCE2 35
213 #define RELAY_COMMAND_RENDEZVOUS1 36
214 #define RELAY_COMMAND_RENDEZVOUS2 37
215 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
216 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
217 #define RELAY_COMMAND_INTRODUCE_ACK 40
219 #define RELAY_COMMAND_PADDING_NEGOTIATE 41
220 #define RELAY_COMMAND_PADDING_NEGOTIATED 42
222 #define RELAY_COMMAND_XOFF 43
223 #define RELAY_COMMAND_XON 44
225 /* Reasons why an OR connection is closed. */
226 #define END_OR_CONN_REASON_DONE 1
227 #define END_OR_CONN_REASON_REFUSED 2 /* connection refused */
228 #define END_OR_CONN_REASON_OR_IDENTITY 3
229 #define END_OR_CONN_REASON_CONNRESET 4 /* connection reset by peer */
230 #define END_OR_CONN_REASON_TIMEOUT 5
231 #define END_OR_CONN_REASON_NO_ROUTE 6 /* no route to host/net */
232 #define END_OR_CONN_REASON_IO_ERROR 7 /* read/write error */
233 #define END_OR_CONN_REASON_RESOURCE_LIMIT 8 /* sockets, buffers, etc */
234 #define END_OR_CONN_REASON_PT_MISSING 9 /* PT failed or not available */
235 #define END_OR_CONN_REASON_TLS_ERROR 10 /* Problem in TLS protocol */
236 #define END_OR_CONN_REASON_MISC 11
238 /* Reasons why we (or a remote OR) might close a stream. See tor-spec.txt for
239 * documentation of these. The values must match. */
240 #define END_STREAM_REASON_MISC 1
241 #define END_STREAM_REASON_RESOLVEFAILED 2
242 #define END_STREAM_REASON_CONNECTREFUSED 3
243 #define END_STREAM_REASON_EXITPOLICY 4
244 #define END_STREAM_REASON_DESTROY 5
245 #define END_STREAM_REASON_DONE 6
246 #define END_STREAM_REASON_TIMEOUT 7
247 #define END_STREAM_REASON_NOROUTE 8
248 #define END_STREAM_REASON_HIBERNATING 9
249 #define END_STREAM_REASON_INTERNAL 10
250 #define END_STREAM_REASON_RESOURCELIMIT 11
251 #define END_STREAM_REASON_CONNRESET 12
252 #define END_STREAM_REASON_TORPROTOCOL 13
253 #define END_STREAM_REASON_NOTDIRECTORY 14
254 #define END_STREAM_REASON_ENTRYPOLICY 15
256 /* These high-numbered end reasons are not part of the official spec,
257 * and are not intended to be put in relay end cells. They are here
258 * to be more informative when sending back socks replies to the
260 /* XXXX 256 is no longer used; feel free to reuse it. */
261 /** We were unable to attach the connection to any circuit at all. */
262 /* XXXX the ways we use this one don't make a lot of sense. */
263 #define END_STREAM_REASON_CANT_ATTACH 257
264 /** We can't connect to any directories at all, so we killed our streams
265 * before they can time out. */
266 #define END_STREAM_REASON_NET_UNREACHABLE 258
267 /** This is a SOCKS connection, and the client used (or misused) the SOCKS
268 * protocol in a way we couldn't handle. */
269 #define END_STREAM_REASON_SOCKSPROTOCOL 259
270 /** This is a transparent proxy connection, but we can't extract the original
271 * target address:port. */
272 #define END_STREAM_REASON_CANT_FETCH_ORIG_DEST 260
273 /** This is a connection on the NATD port, and the destination IP:Port was
274 * either ill-formed or out-of-range. */
275 #define END_STREAM_REASON_INVALID_NATD_DEST 261
276 /** The target address is in a private network (like 127.0.0.1 or 10.0.0.1);
277 * you don't want to do that over a randomly chosen exit */
278 #define END_STREAM_REASON_PRIVATE_ADDR 262
279 /** This is an HTTP tunnel connection and the client used or misused HTTP in a
280 * way we can't handle.
282 #define END_STREAM_REASON_HTTPPROTOCOL 263
284 /** Bitwise-and this value with endreason to mask out all flags. */
285 #define END_STREAM_REASON_MASK 511
287 /** Bitwise-or this with the argument to control_event_stream_status
288 * to indicate that the reason came from an END cell. */
289 #define END_STREAM_REASON_FLAG_REMOTE 512
290 /** Bitwise-or this with the argument to control_event_stream_status
291 * to indicate that we already sent a CLOSED stream event. */
292 #define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED 1024
293 /** Bitwise-or this with endreason to indicate that we already sent
294 * a socks reply, and no further reply needs to be sent from
295 * connection_mark_unattached_ap(). */
296 #define END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED 2048
298 /* 'type' values to use in RESOLVED cells. Specified in tor-spec.txt. */
299 #define RESOLVED_TYPE_HOSTNAME 0
300 #define RESOLVED_TYPE_IPV4 4
301 #define RESOLVED_TYPE_IPV6 6
302 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
303 #define RESOLVED_TYPE_ERROR 0xF1
305 /* Negative reasons are internal: we never send them in a DESTROY or TRUNCATE
306 * call; they only go to the controller for tracking */
308 /* Closing introduction point that were opened in parallel. */
309 #define END_CIRC_REASON_IP_NOW_REDUNDANT -4
311 /** Our post-timeout circuit time measurement period expired.
312 * We must give up now */
313 #define END_CIRC_REASON_MEASUREMENT_EXPIRED -3
315 /** We couldn't build a path for this circuit. */
316 #define END_CIRC_REASON_NOPATH -2
317 /** Catch-all "other" reason for closing origin circuits. */
318 #define END_CIRC_AT_ORIGIN -1
320 /* Reasons why we (or a remote OR) might close a circuit. See tor-spec.txt
321 * section 5.4 for documentation of these. */
322 #define END_CIRC_REASON_MIN_ 0
323 #define END_CIRC_REASON_NONE 0
324 #define END_CIRC_REASON_TORPROTOCOL 1
325 #define END_CIRC_REASON_INTERNAL 2
326 #define END_CIRC_REASON_REQUESTED 3
327 #define END_CIRC_REASON_HIBERNATING 4
328 #define END_CIRC_REASON_RESOURCELIMIT 5
329 #define END_CIRC_REASON_CONNECTFAILED 6
330 #define END_CIRC_REASON_OR_IDENTITY 7
331 #define END_CIRC_REASON_CHANNEL_CLOSED 8
332 #define END_CIRC_REASON_FINISHED 9
333 #define END_CIRC_REASON_TIMEOUT 10
334 #define END_CIRC_REASON_DESTROYED 11
335 #define END_CIRC_REASON_NOSUCHSERVICE 12
336 #define END_CIRC_REASON_MAX_ 12
338 /** Bitwise-OR this with the argument to circuit_mark_for_close() or
339 * control_event_circuit_status() to indicate that the reason was
340 * passed through from a destroy or truncate cell. */
341 #define END_CIRC_REASON_FLAG_REMOTE 512
343 /** Length of v2 descriptor ID (32 base32 chars = 160 bits).
345 * XXX: It is still used by v3 code but should be renamed or maybe removed. */
346 #define REND_DESC_ID_V2_LEN_BASE32 BASE32_DIGEST_LEN
348 /** Maximum length of authorized client names for a hidden service. */
349 #define REND_CLIENTNAME_MAX_LEN 16
351 /** Length of the rendezvous cookie that is used to connect circuits at the
352 * rendezvous point. */
353 #define REND_COOKIE_LEN DIGEST_LEN
355 /** Client authorization type that a hidden service performs. */
356 typedef enum rend_auth_type_t
{
358 REND_V3_AUTH
= 1, /* Dummy flag to allow adding v3 services on the
362 /* Stub because we can't include hs_ident.h. */
363 struct hs_ident_edge_conn_t
;
364 struct hs_ident_dir_conn_t
;
365 struct hs_ident_circuit_t
;
367 typedef struct hsdir_index_t hsdir_index_t
;
369 /** Time interval for tracking replays of DH public keys received in
370 * INTRODUCE2 cells. Used only to avoid launching multiple
371 * simultaneous attempts to connect to the same rendezvous point. */
372 #define REND_REPLAY_TIME_INTERVAL (5 * 60)
374 /** Used to indicate which way a cell is going on a circuit. */
376 CELL_DIRECTION_IN
=1, /**< The cell is moving towards the origin. */
377 CELL_DIRECTION_OUT
=2, /**< The cell is moving away from the origin. */
381 * An enum to allow us to specify which channel in a circuit
382 * we're interested in.
384 * This is needed because our data structures and other fields
385 * for channel delivery are disassociated from the channel.
390 } circuit_channel_direction_t
;
392 /** Initial value for both sides of a circuit transmission window when the
393 * circuit is initialized. Measured in cells. */
394 #define CIRCWINDOW_START 1000
395 #define CIRCWINDOW_START_MIN 100
396 #define CIRCWINDOW_START_MAX 1000
397 /** Amount to increment a circuit window when we get a circuit SENDME. */
398 #define CIRCWINDOW_INCREMENT 100
399 /** Initial value on both sides of a stream transmission window when the
400 * stream is initialized. Measured in cells. */
401 #define STREAMWINDOW_START 500
402 #define STREAMWINDOW_START_MAX 500
403 /** Amount to increment a stream window when we get a stream SENDME. */
404 #define STREAMWINDOW_INCREMENT 50
406 /** Maximum number of queued cells on a circuit for which we are the
407 * midpoint before we give up and kill it. This must be >= circwindow
408 * to avoid killing innocent circuits, and >= circwindow*2 to give
409 * leaky-pipe a chance of working someday. The ORCIRC_MAX_MIDDLE_KILL_THRESH
410 * ratio controls the margin of error between emitting a warning and
411 * killing the circuit.
413 #define ORCIRC_MAX_MIDDLE_CELLS (CIRCWINDOW_START_MAX*2)
414 /** Ratio of hard (circuit kill) to soft (warning) thresholds for the
415 * ORCIRC_MAX_MIDDLE_CELLS tests.
417 #define ORCIRC_MAX_MIDDLE_KILL_THRESH (1.1f)
419 /* Cell commands. These values are defined in tor-spec.txt. */
420 #define CELL_PADDING 0
421 #define CELL_CREATE 1
422 #define CELL_CREATED 2
424 #define CELL_DESTROY 4
425 #define CELL_CREATE_FAST 5
426 #define CELL_CREATED_FAST 6
427 #define CELL_VERSIONS 7
428 #define CELL_NETINFO 8
429 #define CELL_RELAY_EARLY 9
430 #define CELL_CREATE2 10
431 #define CELL_CREATED2 11
432 #define CELL_PADDING_NEGOTIATE 12
434 #define CELL_VPADDING 128
435 #define CELL_CERTS 129
436 #define CELL_AUTH_CHALLENGE 130
437 #define CELL_AUTHENTICATE 131
438 #define CELL_AUTHORIZE 132
439 #define CELL_COMMAND_MAX_ 132
441 /** How long to test reachability before complaining to the user. */
442 #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
444 /** Legal characters in a nickname. */
445 #define LEGAL_NICKNAME_CHARACTERS \
446 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
448 /** Name chosen by routers that don't configure nicknames */
449 #define UNNAMED_ROUTER_NICKNAME "Unnamed"
451 /** Number of bytes in a SOCKS4 header. */
452 #define SOCKS4_NETWORK_LEN 8
456 * Relay command [1 byte]
457 * Recognized [2 bytes]
458 * Stream ID [2 bytes]
459 * Partial SHA-1 [4 bytes]
461 * Relay payload [498 bytes]
464 /** Number of bytes in a cell, minus cell header. */
465 #define CELL_PAYLOAD_SIZE 509
466 /** Number of bytes in a cell transmitted over the network, in the longest
468 #define CELL_MAX_NETWORK_SIZE 514
470 /** Maximum length of a header on a variable-length cell. */
471 #define VAR_CELL_MAX_HEADER_SIZE 7
473 static int get_cell_network_size(int wide_circ_ids
);
474 static inline int get_cell_network_size(int wide_circ_ids
)
476 return wide_circ_ids
? CELL_MAX_NETWORK_SIZE
: CELL_MAX_NETWORK_SIZE
- 2;
478 static int get_var_cell_header_size(int wide_circ_ids
);
479 static inline int get_var_cell_header_size(int wide_circ_ids
)
481 return wide_circ_ids
? VAR_CELL_MAX_HEADER_SIZE
:
482 VAR_CELL_MAX_HEADER_SIZE
- 2;
484 static int get_circ_id_size(int wide_circ_ids
);
485 static inline int get_circ_id_size(int wide_circ_ids
)
487 return wide_circ_ids
? 4 : 2;
490 /** Number of bytes in a relay cell's header (not including general cell
492 #define RELAY_HEADER_SIZE (1+2+2+4+2)
493 /** Largest number of bytes that can fit in a relay cell payload. */
494 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
496 /** Identifies a circuit on an or_connection */
497 typedef uint32_t circid_t
;
498 /** Identifies a stream on a circuit */
499 typedef uint16_t streamid_t
;
501 /* channel_t typedef; struct channel_t is in channel.h */
503 typedef struct channel_t channel_t
;
505 /* channel_listener_t typedef; struct channel_listener_t is in channel.h */
507 typedef struct channel_listener_t channel_listener_t
;
509 /* TLS channel stuff */
511 typedef struct channel_tls_t channel_tls_t
;
513 /* circuitmux_t typedef; struct circuitmux_t is in circuitmux.h */
515 typedef struct circuitmux_t circuitmux_t
;
517 typedef struct cell_t cell_t
;
518 typedef struct var_cell_t var_cell_t
;
519 typedef struct packed_cell_t packed_cell_t
;
520 typedef struct cell_queue_t cell_queue_t
;
521 typedef struct destroy_cell_t destroy_cell_t
;
522 typedef struct destroy_cell_queue_t destroy_cell_queue_t
;
523 typedef struct ext_or_cmd_t ext_or_cmd_t
;
525 /** Beginning of a RELAY cell payload. */
527 uint8_t command
; /**< The end-to-end relay command. */
528 uint16_t recognized
; /**< Used to tell whether cell is for us. */
529 streamid_t stream_id
; /**< Which stream is this cell associated with? */
530 char integrity
[4]; /**< Used to tell whether cell is corrupted. */
531 uint16_t length
; /**< How long is the payload body? */
534 typedef struct socks_request_t socks_request_t
;
535 typedef struct entry_port_cfg_t entry_port_cfg_t
;
536 typedef struct server_port_cfg_t server_port_cfg_t
;
538 /** Minimum length of the random part of an AUTH_CHALLENGE cell. */
539 #define OR_AUTH_CHALLENGE_LEN 32
542 * @name Certificate types for CERTS cells.
544 * These values are defined by the protocol, and affect how an X509
545 * certificate in a CERTS cell is interpreted and used.
548 /** A certificate that authenticates a TLS link key. The subject key
549 * must match the key used in the TLS handshake; it must be signed by
550 * the identity key. */
551 #define OR_CERT_TYPE_TLS_LINK 1
552 /** A self-signed identity certificate. The subject key must be a
553 * 1024-bit RSA key. */
554 #define OR_CERT_TYPE_ID_1024 2
555 /** A certificate that authenticates a key used in an AUTHENTICATE cell
556 * in the v3 handshake. The subject key must be a 1024-bit RSA key; it
557 * must be signed by the identity key */
558 #define OR_CERT_TYPE_AUTH_1024 3
560 #define OR_CERT_TYPE_RSA_ED_CROSSCERT 7
563 /** The first supported type of AUTHENTICATE cell. It contains
564 * a bunch of structures signed with an RSA1024 key. The signed
565 * structures include a HMAC using negotiated TLS secrets, and a digest
566 * of all cells sent or received before the AUTHENTICATE cell (including
567 * the random server-generated AUTH_CHALLENGE cell).
569 #define AUTHTYPE_RSA_SHA256_TLSSECRET 1
570 /** As AUTHTYPE_RSA_SHA256_TLSSECRET, but instead of using the
571 * negotiated TLS secrets, uses exported keying material from the TLS
572 * session as described in RFC 5705.
574 * Not used by today's tors, since everything that supports this
575 * also supports ED25519_SHA256_5705, which is better.
577 #define AUTHTYPE_RSA_SHA256_RFC5705 2
578 /** As AUTHTYPE_RSA_SHA256_RFC5705, but uses an Ed25519 identity key to
580 #define AUTHTYPE_ED25519_SHA256_RFC5705 3
582 * NOTE: authchallenge_type_is_better() relies on these AUTHTYPE codes
583 * being sorted in order of preference. If we someday add one with
584 * a higher numerical value that we don't like as much, we should revise
585 * authchallenge_type_is_better().
588 /** The length of the part of the AUTHENTICATE cell body that the client and
589 * server can generate independently (when using RSA_SHA256_TLSSECRET). It
590 * contains everything except the client's timestamp, the client's randomly
591 * generated nonce, and the signature. */
592 #define V3_AUTH_FIXED_PART_LEN (8+(32*6))
593 /** The length of the part of the AUTHENTICATE cell body that the client
595 #define V3_AUTH_BODY_LEN (V3_AUTH_FIXED_PART_LEN + 8 + 16)
597 typedef struct or_handshake_certs_t or_handshake_certs_t
;
598 typedef struct or_handshake_state_t or_handshake_state_t
;
600 /** Length of Extended ORPort connection identifier. */
601 #define EXT_OR_CONN_ID_LEN DIGEST_LEN /* 20 */
603 typedef struct connection_t connection_t
;
604 typedef struct control_connection_t control_connection_t
;
605 typedef struct dir_connection_t dir_connection_t
;
606 typedef struct edge_connection_t edge_connection_t
;
607 typedef struct entry_connection_t entry_connection_t
;
608 typedef struct listener_connection_t listener_connection_t
;
609 typedef struct or_connection_t or_connection_t
;
611 /** Cast a connection_t subtype pointer to a connection_t **/
612 #define TO_CONN(c) (&(((c)->base_)))
614 /** Cast a entry_connection_t subtype pointer to a connection_t **/
615 #define ENTRY_TO_CONN(c) (TO_CONN(ENTRY_TO_EDGE_CONN(c)))
617 typedef struct addr_policy_t addr_policy_t
;
619 typedef struct cached_dir_t cached_dir_t
;
621 /** Enum used to remember where a signed_descriptor_t is stored and how to
622 * manage the memory for signed_descriptor_body. */
624 /** The descriptor isn't stored on disk at all: the copy in memory is
625 * canonical; the saved_offset field is meaningless. */
627 /** The descriptor is stored in the cached_routers file: the
628 * signed_descriptor_body is meaningless; the signed_descriptor_len and
629 * saved_offset are used to index into the mmaped cache file. */
631 /** The descriptor is stored in the cached_routers.new file: the
632 * signed_descriptor_body and saved_offset fields are both set. */
633 /* FFFF (We could also mmap the file and grow the mmap as needed, or
634 * lazy-load the descriptor text by using seek and read. We don't, for
639 #define saved_location_bitfield_t ENUM_BF(saved_location_t)
641 /** Enumeration: what directory object is being downloaded?
642 * This determines which schedule is selected to perform the download. */
644 DL_SCHED_GENERIC
= 0,
645 DL_SCHED_CONSENSUS
= 1,
647 } download_schedule_t
;
648 #define download_schedule_bitfield_t ENUM_BF(download_schedule_t)
650 /** Enumeration: is the download schedule for downloading from an authority,
651 * or from any available directory mirror?
652 * During bootstrap, "any" means a fallback (or an authority, if there
654 * When we have a valid consensus, "any" means any directory server. */
656 DL_WANT_ANY_DIRSERVER
= 0,
657 DL_WANT_AUTHORITY
= 1,
658 } download_want_authority_t
;
659 #define download_want_authority_bitfield_t \
660 ENUM_BF(download_want_authority_t)
662 /** Enumeration: do we want to increment the schedule position each time a
663 * connection is attempted (these attempts can be concurrent), or do we want
664 * to increment the schedule position after a connection fails? */
666 DL_SCHED_INCREMENT_FAILURE
= 0,
667 DL_SCHED_INCREMENT_ATTEMPT
= 1,
668 } download_schedule_increment_t
;
669 #define download_schedule_increment_bitfield_t \
670 ENUM_BF(download_schedule_increment_t)
672 typedef struct download_status_t download_status_t
;
674 /** If n_download_failures is this high, the download can never happen. */
675 #define IMPOSSIBLE_TO_DOWNLOAD 255
677 /** The max size we expect router descriptor annotations we create to
678 * be. We'll accept larger ones if we see them on disk, but we won't
679 * create any that are larger than this. */
680 #define ROUTER_ANNOTATION_BUF_LEN 256
682 typedef struct signed_descriptor_t signed_descriptor_t
;
684 /** Flags used to summarize the declared protocol versions of a relay,
685 * so we don't need to parse them again and again. */
686 typedef struct protover_summary_flags_t
{
687 /** True iff we have a proto line for this router, or a versions line
688 * from which we could infer the protocols. */
689 unsigned int protocols_known
:1;
691 /** True iff this router has a version or protocol list that allows it to
692 * accept EXTEND2 cells. This requires Relay=2. */
693 unsigned int supports_extend2_cells
:1;
695 /** True iff this router has a version or protocol list that allows it to
696 * accept IPv6 connections. This requires Relay=2 or Relay=3. */
697 unsigned int supports_accepting_ipv6_extends
:1;
699 /** True iff this router has a version or protocol list that allows it to
700 * initiate IPv6 connections. This requires Relay=3. */
701 unsigned int supports_initiating_ipv6_extends
:1;
703 /** True iff this router has a version or protocol list that allows it to
704 * consider IPv6 connections canonical. This requires Relay=3. */
705 unsigned int supports_canonical_ipv6_conns
:1;
707 /** True iff this router has a protocol list that allows it to negotiate
708 * ed25519 identity keys on a link handshake with us. This
709 * requires LinkAuth=3. */
710 unsigned int supports_ed25519_link_handshake_compat
:1;
712 /** True iff this router has a protocol list that allows it to negotiate
713 * ed25519 identity keys on a link handshake, at all. This requires some
714 * LinkAuth=X for X >= 3. */
715 unsigned int supports_ed25519_link_handshake_any
:1;
717 /** True iff this router has a protocol list that allows it to be an
718 * introduction point supporting ed25519 authentication key which is part of
719 * the v3 protocol detailed in proposal 224. This requires HSIntro=4. */
720 unsigned int supports_ed25519_hs_intro
: 1;
722 /** True iff this router has a protocol list that allows it to support the
723 * ESTABLISH_INTRO DoS cell extension. Requires HSIntro=5. */
724 unsigned int supports_establish_intro_dos_extension
: 1;
726 /** True iff this router has a protocol list that allows it to be an hidden
727 * service directory supporting version 3 as seen in proposal 224. This
728 * requires HSDir=2. */
729 unsigned int supports_v3_hsdir
: 1;
731 /** True iff this router has a protocol list that allows it to be an hidden
732 * service rendezvous point supporting version 3 as seen in proposal 224.
733 * This requires HSRend=2. */
734 unsigned int supports_v3_rendezvous_point
: 1;
736 /** True iff this router has a protocol list that allows clients to
737 * negotiate hs circuit setup padding. Requires Padding=2. */
738 unsigned int supports_hs_setup_padding
: 1;
740 /** True iff this router supports congestion control.
741 * Requires both FlowCtrl=2 *and* Relay=4 */
742 unsigned int supports_congestion_control
: 1;
744 /** True iff this router supports conflux. Requires Relay=5 */
745 unsigned int supports_conflux
: 1;
746 } protover_summary_flags_t
;
748 typedef struct routerinfo_t routerinfo_t
;
749 typedef struct extrainfo_t extrainfo_t
;
750 typedef struct routerstatus_t routerstatus_t
;
752 typedef struct microdesc_t microdesc_t
;
753 typedef struct node_t node_t
;
754 typedef struct vote_microdesc_hash_t vote_microdesc_hash_t
;
755 typedef struct vote_routerstatus_t vote_routerstatus_t
;
756 typedef struct document_signature_t document_signature_t
;
757 typedef struct networkstatus_voter_info_t networkstatus_voter_info_t
;
758 typedef struct networkstatus_sr_info_t networkstatus_sr_info_t
;
760 /** Enumerates recognized flavors of a consensus networkstatus document. All
761 * flavors of a consensus are generated from the same set of votes, but they
762 * present different types information to different versions of Tor. */
766 } consensus_flavor_t
;
768 /** How many different consensus flavors are there? */
769 #define N_CONSENSUS_FLAVORS ((int)(FLAV_MICRODESC)+1)
771 typedef struct networkstatus_t networkstatus_t
;
772 typedef struct ns_detached_signatures_t ns_detached_signatures_t
;
773 typedef struct desc_store_t desc_store_t
;
774 typedef struct routerlist_t routerlist_t
;
775 typedef struct extend_info_t extend_info_t
;
776 typedef struct authority_cert_t authority_cert_t
;
778 /** Bitfield enum type listing types of information that directory authorities
779 * can be authoritative about, and that directory caches may or may not cache.
781 * Note that the granularity here is based on authority granularity and on
782 * cache capabilities. Thus, one particular bit may correspond in practice to
783 * a few types of directory info, so long as every authority that pronounces
784 * officially about one of the types prounounces officially about all of them,
785 * and so long as every cache that caches one of them caches all of them.
789 /** Serves/signs v3 directory information: votes, consensuses, certs */
791 /** Serves bridge descriptors. */
792 BRIDGE_DIRINFO
= 1 << 4,
793 /** Serves extrainfo documents. */
794 EXTRAINFO_DIRINFO
=1 << 5,
795 /** Serves microdescriptors. */
796 MICRODESC_DIRINFO
=1 << 6,
799 #define ALL_DIRINFO ((dirinfo_type_t)((1<<7)-1))
801 #define ONION_HANDSHAKE_TYPE_TAP 0x0000
802 #define ONION_HANDSHAKE_TYPE_FAST 0x0001
803 #define ONION_HANDSHAKE_TYPE_NTOR 0x0002
804 #define ONION_HANDSHAKE_TYPE_NTOR_V3 0x0003
805 #define MAX_ONION_HANDSHAKE_TYPE 0x0003
807 typedef struct onion_handshake_state_t onion_handshake_state_t
;
808 typedef struct relay_crypto_t relay_crypto_t
;
809 typedef struct crypt_path_t crypt_path_t
;
810 typedef struct crypt_path_reference_t crypt_path_reference_t
;
812 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
814 typedef struct cpath_build_state_t cpath_build_state_t
;
816 struct create_cell_t
;
818 /** Entry in the cell stats list of a circuit; used only if CELL_STATS
819 * events are enabled. */
820 typedef struct testing_cell_stats_entry_t
{
821 uint8_t command
; /**< cell command number. */
822 /** Waiting time in centiseconds if this event is for a removed cell,
823 * or 0 if this event is for adding a cell to the queue. 22 bits can
824 * store more than 11 hours, enough to assume that a circuit with this
825 * delay would long have been closed. */
826 unsigned int waiting_time
:22;
827 unsigned int removed
:1; /**< 0 for added to, 1 for removed from queue. */
828 unsigned int exitward
:1; /**< 0 for app-ward, 1 for exit-ward. */
829 } testing_cell_stats_entry_t
;
831 typedef struct circuit_t circuit_t
;
832 typedef struct origin_circuit_t origin_circuit_t
;
833 typedef struct or_circuit_t or_circuit_t
;
835 /** Largest number of relay_early cells that we can send on a given
837 #define MAX_RELAY_EARLY_CELLS_PER_CIRCUIT 8
839 typedef enum path_state_t path_state_t
;
840 #define path_state_bitfield_t ENUM_BF(path_state_t)
842 #if REND_COOKIE_LEN != DIGEST_LEN
843 #error "The REND_TOKEN_LEN macro assumes REND_COOKIE_LEN == DIGEST_LEN"
845 #define REND_TOKEN_LEN DIGEST_LEN
847 /** Convert a circuit subtype to a circuit_t. */
848 #define TO_CIRCUIT(x) (&((x)->base_))
850 /** @name Isolation flags
852 Ways to isolate client streams
856 /** Isolate based on destination port */
857 #define ISO_DESTPORT (1u<<0)
858 /** Isolate based on destination address */
859 #define ISO_DESTADDR (1u<<1)
860 /** Isolate based on SOCKS authentication */
861 #define ISO_SOCKSAUTH (1u<<2)
862 /** Isolate based on client protocol choice */
863 #define ISO_CLIENTPROTO (1u<<3)
864 /** Isolate based on client address */
865 #define ISO_CLIENTADDR (1u<<4)
866 /** Isolate based on session group (always on). */
867 #define ISO_SESSIONGRP (1u<<5)
868 /** Isolate based on newnym epoch (always on). */
869 #define ISO_NYM_EPOCH (1u<<6)
870 /** Isolate all streams (Internal only). */
871 #define ISO_STREAM (1u<<7)
874 /** Default isolation level for ports. */
875 #define ISO_DEFAULT (ISO_CLIENTADDR|ISO_SOCKSAUTH|ISO_SESSIONGRP|ISO_NYM_EPOCH)
877 /** Indicates that we haven't yet set a session group on a port_cfg_t. */
878 #define SESSION_GROUP_UNSET -1
879 /** Session group reserved for directory connections */
880 #define SESSION_GROUP_DIRCONN -2
881 /** Session group reserved for resolve requests launched by a controller */
882 #define SESSION_GROUP_CONTROL_RESOLVE -3
883 /** First automatically allocated session group number */
884 #define SESSION_GROUP_FIRST_AUTO -4
886 typedef struct port_cfg_t port_cfg_t
;
887 typedef struct routerset_t routerset_t
;
889 /** A magic value for the (Socks|OR|...)Port options below, telling Tor
890 * to pick its own port. */
891 #define CFG_AUTO_PORT 0xc4005e
893 typedef struct or_options_t or_options_t
;
895 typedef struct or_state_t or_state_t
;
897 #define MAX_SOCKS_ADDR_LEN 256
899 /********************************* circuitbuild.c **********************/
901 /** How many hops does a general-purpose circuit have by default? */
902 #define DEFAULT_ROUTE_LEN 3
904 /* Circuit Build Timeout "public" structures. */
906 /** Precision multiplier for the Bw weights */
907 #define BW_WEIGHT_SCALE 10000
908 #define BW_MIN_WEIGHT_SCALE 1
909 #define BW_MAX_WEIGHT_SCALE INT32_MAX
911 typedef struct circuit_build_times_t circuit_build_times_t
;
913 /********************************* config.c ***************************/
915 /********************************* connection_edge.c *************************/
917 /** Enumerates possible origins of a client-side address mapping. */
919 /** We're remapping this address because the controller told us to. */
920 ADDRMAPSRC_CONTROLLER
,
921 /** We're remapping this address because of an AutomapHostsOnResolve
924 /** We're remapping this address because our configuration (via torrc, the
925 * command line, or a SETCONF command) told us to. */
927 /** We're remapping this address because we have TrackHostExit configured,
928 * and we want to remember to use the same exit next time. */
929 ADDRMAPSRC_TRACKEXIT
,
930 /** We're remapping this address because we got a DNS resolution from a
931 * Tor server that told us what its value was. */
934 /** No remapping has occurred. This isn't a possible value for an
935 * addrmap_entry_t; it's used as a null value when we need to answer "Why
936 * did this remapping happen." */
938 } addressmap_entry_source_t
;
939 #define addressmap_entry_source_bitfield_t ENUM_BF(addressmap_entry_source_t)
941 #define WRITE_STATS_INTERVAL (24*60*60)
943 /********************************* dirvote.c ************************/
945 typedef struct vote_timing_t vote_timing_t
;
947 /********************************* microdesc.c *************************/
949 typedef struct microdesc_cache_t microdesc_cache_t
;
951 /** The maximum number of non-circuit-build-timeout failures a hidden
952 * service client will tolerate while trying to build a circuit to an
953 * introduction point. */
954 #define MAX_INTRO_POINT_REACHABILITY_FAILURES 5
956 /** The minimum and maximum number of distinct INTRODUCE2 cells which a
957 * hidden service's introduction point will receive before it begins to
959 #define INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS 16384
960 /* Double the minimum value so the interval is [min, min * 2]. */
961 #define INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS \
962 (INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS * 2)
964 /** The minimum number of seconds that an introduction point will last
965 * before expiring due to old age. (If it receives
966 * INTRO_POINT_LIFETIME_INTRODUCTIONS INTRODUCE2 cells, it may expire
969 * XXX Should this be configurable? */
970 #define INTRO_POINT_LIFETIME_MIN_SECONDS (18*60*60)
971 /** The maximum number of seconds that an introduction point will last
972 * before expiring due to old age.
974 * XXX Should this be configurable? */
975 #define INTRO_POINT_LIFETIME_MAX_SECONDS (24*60*60)
977 /** The maximum number of circuit creation retry we do to an intro point
978 * before giving up. We try to reuse intro point that fails during their
979 * lifetime so this is a hard limit on the amount of time we do that. */
980 #define MAX_INTRO_POINT_CIRCUIT_RETRIES 3
982 /********************************* routerlist.c ***************************/
984 typedef struct dir_server_t dir_server_t
;
986 #define RELAY_REQUIRED_MIN_BANDWIDTH (75*1024)
987 #define BRIDGE_REQUIRED_MIN_BANDWIDTH (50*1024)
989 #define ROUTER_MAX_DECLARED_BANDWIDTH INT32_MAX
991 typedef struct tor_version_t tor_version_t
;
993 #endif /* !defined(TOR_OR_H) */