tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

or.h (40570B)


      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 or.h
      9 * \brief Master header file for Tor-specific functionality.
     10 **/
     11 
     12 #ifndef TOR_OR_H
     13 #define TOR_OR_H
     14 
     15 #include "orconfig.h"
     16 #include "lib/cc/torint.h"
     17 
     18 #ifdef HAVE_SIGNAL_H
     19 #include <signal.h>
     20 #endif
     21 #ifdef HAVE_TIME_H
     22 #include <time.h>
     23 #endif
     24 
     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"
     67 
     68 #include "ht.h"
     69 
     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"
     73 
     74 struct ed25519_public_key_t;
     75 struct curve25519_public_key_t;
     76 
     77 /* These signals are defined to help handle_control_signal work.
     78 */
     79 #ifndef SIGHUP
     80 #define SIGHUP 1
     81 #endif
     82 #ifndef SIGINT
     83 #define SIGINT 2
     84 #endif
     85 #ifndef SIGUSR1
     86 #define SIGUSR1 10
     87 #endif
     88 #ifndef SIGUSR2
     89 #define SIGUSR2 12
     90 #endif
     91 #ifndef SIGTERM
     92 #define SIGTERM 15
     93 #endif
     94 /* Controller signals start at a high number so we don't
     95 * conflict with system-defined signals. */
     96 #define SIGNEWNYM 129
     97 #define SIGCLEARDNSCACHE 130
     98 #define SIGHEARTBEAT 131
     99 #define SIGACTIVE 132
    100 #define SIGDORMANT 133
    101 
    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
    106 #endif
    107 
    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_))
    110 
    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)
    119 
    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
    123 
    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
    128 
    129 /** Maximum size of a single extrainfo document, as above. */
    130 #define MAX_EXTRAINFO_UPLOAD_SIZE 50000
    131 
    132 /** Minimum lifetime for an onion key in days. */
    133 #define MIN_ONION_KEY_LIFETIME_DAYS (1)
    134 
    135 /** Maximum lifetime for an onion key in days. */
    136 #define MAX_ONION_KEY_LIFETIME_DAYS (90)
    137 
    138 /** Default lifetime for an onion key in days. */
    139 #define DEFAULT_ONION_KEY_LIFETIME_DAYS (28)
    140 
    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)
    145 
    146 /** Default grace period for acceptance of an onion key in days. */
    147 #define DEFAULT_ONION_KEY_GRACE_PERIOD_DAYS (7)
    148 
    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)
    152 
    153 /** How often do we rotate TLS contexts? */
    154 #define MAX_SSL_KEY_LIFETIME_INTERNAL (2*60*60)
    155 
    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)
    164 
    165 /* Proxy client types */
    166 #define PROXY_NONE 0
    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 */
    173 
    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
    177 
    178 /** How many circuits do we want simultaneously in-progress to handle
    179 * a given stream? */
    180 #define MIN_CIRCUITS_HANDLING_STREAM 2
    181 
    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
    188 
    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
    195 
    196 #define RELAY_COMMAND_RESOLVE 11
    197 #define RELAY_COMMAND_RESOLVED 12
    198 
    199 #define RELAY_COMMAND_BEGIN_DIR 13
    200 #define RELAY_COMMAND_EXTEND2 14
    201 #define RELAY_COMMAND_EXTENDED2 15
    202 
    203 /* Conflux */
    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
    208 
    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
    218 
    219 #define RELAY_COMMAND_PADDING_NEGOTIATE 41
    220 #define RELAY_COMMAND_PADDING_NEGOTIATED 42
    221 
    222 #define RELAY_COMMAND_XOFF 43
    223 #define RELAY_COMMAND_XON 44
    224 
    225 /* NOTE: Any new command from above MUST be added to this function. */
    226 /** Helper to learn if we know the relay command. Unfortuantely, they are not
    227 * contigous and so we need this kind of big switch. We could do better but for
    228 * now, we'll run with this. */
    229 static inline bool
    230 is_known_relay_command(const uint8_t cmd)
    231 {
    232  switch (cmd) {
    233  case RELAY_COMMAND_BEGIN:
    234  case RELAY_COMMAND_BEGIN_DIR:
    235  case RELAY_COMMAND_CONFLUX_LINK:
    236  case RELAY_COMMAND_CONFLUX_LINKED:
    237  case RELAY_COMMAND_CONFLUX_LINKED_ACK:
    238  case RELAY_COMMAND_CONFLUX_SWITCH:
    239  case RELAY_COMMAND_CONNECTED:
    240  case RELAY_COMMAND_DATA:
    241  case RELAY_COMMAND_DROP:
    242  case RELAY_COMMAND_END:
    243  case RELAY_COMMAND_ESTABLISH_INTRO:
    244  case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
    245  case RELAY_COMMAND_EXTEND2:
    246  case RELAY_COMMAND_EXTEND:
    247  case RELAY_COMMAND_EXTENDED2:
    248  case RELAY_COMMAND_EXTENDED:
    249  case RELAY_COMMAND_INTRODUCE1:
    250  case RELAY_COMMAND_INTRODUCE2:
    251  case RELAY_COMMAND_INTRODUCE_ACK:
    252  case RELAY_COMMAND_INTRO_ESTABLISHED:
    253  case RELAY_COMMAND_PADDING_NEGOTIATE:
    254  case RELAY_COMMAND_PADDING_NEGOTIATED:
    255  case RELAY_COMMAND_RENDEZVOUS1:
    256  case RELAY_COMMAND_RENDEZVOUS2:
    257  case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
    258  case RELAY_COMMAND_RESOLVE:
    259  case RELAY_COMMAND_RESOLVED:
    260  case RELAY_COMMAND_SENDME:
    261  case RELAY_COMMAND_TRUNCATE:
    262  case RELAY_COMMAND_TRUNCATED:
    263  case RELAY_COMMAND_XOFF:
    264  case RELAY_COMMAND_XON:
    265    return true;
    266  default:
    267    return false;
    268  }
    269 }
    270 
    271 /* Reasons why an OR connection is closed. */
    272 #define END_OR_CONN_REASON_DONE           1
    273 #define END_OR_CONN_REASON_REFUSED        2 /* connection refused */
    274 #define END_OR_CONN_REASON_OR_IDENTITY    3
    275 #define END_OR_CONN_REASON_CONNRESET      4 /* connection reset by peer */
    276 #define END_OR_CONN_REASON_TIMEOUT        5
    277 #define END_OR_CONN_REASON_NO_ROUTE       6 /* no route to host/net */
    278 #define END_OR_CONN_REASON_IO_ERROR       7 /* read/write error */
    279 #define END_OR_CONN_REASON_RESOURCE_LIMIT 8 /* sockets, buffers, etc */
    280 #define END_OR_CONN_REASON_PT_MISSING     9 /* PT failed or not available */
    281 #define END_OR_CONN_REASON_TLS_ERROR      10 /* Problem in TLS protocol */
    282 #define END_OR_CONN_REASON_MISC           11
    283 
    284 /* Reasons why we (or a remote OR) might close a stream. See tor-spec.txt for
    285 * documentation of these.  The values must match. */
    286 #define END_STREAM_REASON_MISC 1
    287 #define END_STREAM_REASON_RESOLVEFAILED 2
    288 #define END_STREAM_REASON_CONNECTREFUSED 3
    289 #define END_STREAM_REASON_EXITPOLICY 4
    290 #define END_STREAM_REASON_DESTROY 5
    291 #define END_STREAM_REASON_DONE 6
    292 #define END_STREAM_REASON_TIMEOUT 7
    293 #define END_STREAM_REASON_NOROUTE 8
    294 #define END_STREAM_REASON_HIBERNATING 9
    295 #define END_STREAM_REASON_INTERNAL 10
    296 #define END_STREAM_REASON_RESOURCELIMIT 11
    297 #define END_STREAM_REASON_CONNRESET 12
    298 #define END_STREAM_REASON_TORPROTOCOL 13
    299 #define END_STREAM_REASON_NOTDIRECTORY 14
    300 
    301 /* These high-numbered end reasons are not part of the official spec,
    302 * and are not intended to be put in relay end cells. They are here
    303 * to be more informative when sending back socks replies to the
    304 * application. */
    305 /* XXXX 256 is no longer used; feel free to reuse it. */
    306 /** We were unable to attach the connection to any circuit at all. */
    307 /* XXXX the ways we use this one don't make a lot of sense. */
    308 #define END_STREAM_REASON_CANT_ATTACH 257
    309 /** We can't connect to any directories at all, so we killed our streams
    310 * before they can time out. */
    311 #define END_STREAM_REASON_NET_UNREACHABLE 258
    312 /** This is a SOCKS connection, and the client used (or misused) the SOCKS
    313 * protocol in a way we couldn't handle. */
    314 #define END_STREAM_REASON_SOCKSPROTOCOL 259
    315 /** This is a transparent proxy connection, but we can't extract the original
    316 * target address:port. */
    317 #define END_STREAM_REASON_CANT_FETCH_ORIG_DEST 260
    318 /** This is a connection on the NATD port, and the destination IP:Port was
    319 * either ill-formed or out-of-range. */
    320 #define END_STREAM_REASON_INVALID_NATD_DEST 261
    321 /** The target address is in a private network (like 127.0.0.1 or 10.0.0.1);
    322 * you don't want to do that over a randomly chosen exit */
    323 #define END_STREAM_REASON_PRIVATE_ADDR 262
    324 /** This is an HTTP tunnel connection and the client used or misused HTTP in a
    325 * way we can't handle.
    326 */
    327 #define END_STREAM_REASON_HTTPPROTOCOL 263
    328 /**
    329 * The user has asked us to do something that we reject
    330 * (Like connecting to a plaintext port, or violating OnionTrafficOnly.)
    331 **/
    332 #define END_STREAM_REASON_ENTRYPOLICY 264
    333 
    334 /** Bitwise-and this value with endreason to mask out all flags. */
    335 #define END_STREAM_REASON_MASK 511
    336 
    337 /** Bitwise-or this with the argument to control_event_stream_status
    338 * to indicate that the reason came from an END cell. */
    339 #define END_STREAM_REASON_FLAG_REMOTE 512
    340 /** Bitwise-or this with the argument to control_event_stream_status
    341 * to indicate that we already sent a CLOSED stream event. */
    342 #define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED 1024
    343 /** Bitwise-or this with endreason to indicate that we already sent
    344 * a socks reply, and no further reply needs to be sent from
    345 * connection_mark_unattached_ap(). */
    346 #define END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED 2048
    347 
    348 /* 'type' values to use in RESOLVED cells.  Specified in tor-spec.txt. */
    349 #define RESOLVED_TYPE_HOSTNAME 0
    350 #define RESOLVED_TYPE_IPV4 4
    351 #define RESOLVED_TYPE_IPV6 6
    352 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
    353 #define RESOLVED_TYPE_ERROR 0xF1
    354 /* C Tor internal error code to handle empty dns reply */
    355 #define RESOLVED_TYPE_NOERROR 0x01F2
    356 
    357 /* Negative reasons are internal: we never send them in a DESTROY or TRUNCATE
    358 * call; they only go to the controller for tracking  */
    359 
    360 /* Closing introduction point that were opened in parallel. */
    361 #define END_CIRC_REASON_IP_NOW_REDUNDANT -4
    362 
    363 /** Our post-timeout circuit time measurement period expired.
    364 * We must give up now */
    365 #define END_CIRC_REASON_MEASUREMENT_EXPIRED -3
    366 
    367 /** We couldn't build a path for this circuit. */
    368 #define END_CIRC_REASON_NOPATH          -2
    369 /** Catch-all "other" reason for closing origin circuits. */
    370 #define END_CIRC_AT_ORIGIN              -1
    371 
    372 /* Reasons why we (or a remote OR) might close a circuit. See tor-spec.txt
    373 * section 5.4 for documentation of these. */
    374 #define END_CIRC_REASON_MIN_            0
    375 #define END_CIRC_REASON_NONE            0
    376 #define END_CIRC_REASON_TORPROTOCOL     1
    377 #define END_CIRC_REASON_INTERNAL        2
    378 #define END_CIRC_REASON_REQUESTED       3
    379 #define END_CIRC_REASON_HIBERNATING     4
    380 #define END_CIRC_REASON_RESOURCELIMIT   5
    381 #define END_CIRC_REASON_CONNECTFAILED   6
    382 #define END_CIRC_REASON_OR_IDENTITY     7
    383 #define END_CIRC_REASON_CHANNEL_CLOSED  8
    384 #define END_CIRC_REASON_FINISHED        9
    385 #define END_CIRC_REASON_TIMEOUT         10
    386 #define END_CIRC_REASON_DESTROYED       11
    387 #define END_CIRC_REASON_NOSUCHSERVICE   12
    388 #define END_CIRC_REASON_MAX_            12
    389 
    390 /** Bitwise-OR this with the argument to circuit_mark_for_close() or
    391 * control_event_circuit_status() to indicate that the reason was
    392 * passed through from a destroy or truncate cell. */
    393 #define END_CIRC_REASON_FLAG_REMOTE     512
    394 
    395 /** Length of v2 descriptor ID (32 base32 chars = 160 bits).
    396 *
    397 * XXX: It is still used by v3 code but should be renamed or maybe removed. */
    398 #define REND_DESC_ID_V2_LEN_BASE32 BASE32_DIGEST_LEN
    399 
    400 /** Maximum length of authorized client names for a hidden service. */
    401 #define REND_CLIENTNAME_MAX_LEN 16
    402 
    403 /** Length of the rendezvous cookie that is used to connect circuits at the
    404 * rendezvous point. */
    405 #define REND_COOKIE_LEN DIGEST_LEN
    406 
    407 /** Client authorization type that a hidden service performs. */
    408 typedef enum rend_auth_type_t {
    409  REND_NO_AUTH      = 0,
    410  REND_V3_AUTH      = 1, /* Dummy flag to allow adding v3 services on the
    411                          * control port */
    412 } rend_auth_type_t;
    413 
    414 /* Stub because we can't include hs_ident.h. */
    415 struct hs_ident_edge_conn_t;
    416 struct hs_ident_dir_conn_t;
    417 struct hs_ident_circuit_t;
    418 
    419 typedef struct hsdir_index_t hsdir_index_t;
    420 
    421 /** Time interval for tracking replays of DH public keys received in
    422 * INTRODUCE2 cells.  Used only to avoid launching multiple
    423 * simultaneous attempts to connect to the same rendezvous point. */
    424 #define REND_REPLAY_TIME_INTERVAL (5 * 60)
    425 
    426 /** Used to indicate which way a cell is going on a circuit. */
    427 typedef enum {
    428  CELL_DIRECTION_IN=1, /**< The cell is moving towards the origin. */
    429  CELL_DIRECTION_OUT=2, /**< The cell is moving away from the origin. */
    430 } cell_direction_t;
    431 
    432 /**
    433 * An enum to allow us to specify which channel in a circuit
    434 * we're interested in.
    435 *
    436 * This is needed because our data structures and other fields
    437 * for channel delivery are disassociated from the channel.
    438 */
    439 typedef enum {
    440  CIRCUIT_N_CHAN = 0,
    441  CIRCUIT_P_CHAN = 1
    442 } circuit_channel_direction_t;
    443 
    444 /** Initial value for both sides of a circuit transmission window when the
    445 * circuit is initialized.  Measured in cells. */
    446 #define CIRCWINDOW_START 1000
    447 #define CIRCWINDOW_START_MIN 100
    448 #define CIRCWINDOW_START_MAX 1000
    449 /** Amount to increment a circuit window when we get a circuit SENDME. */
    450 #define CIRCWINDOW_INCREMENT 100
    451 /** Initial value on both sides of a stream transmission window when the
    452 * stream is initialized.  Measured in cells. */
    453 #define STREAMWINDOW_START 500
    454 #define STREAMWINDOW_START_MAX 500
    455 /** Amount to increment a stream window when we get a stream SENDME. */
    456 #define STREAMWINDOW_INCREMENT 50
    457 
    458 /** Length for authenticated sendme tag with tor1 encryption. */
    459 #define SENDME_TAG_LEN_TOR1 20
    460 /** Length for authenticated sendme tag with cgo encryption. */
    461 #define SENDME_TAG_LEN_CGO 16
    462 
    463 /** Maximum number of queued cells on a circuit for which we are the
    464 * midpoint before we give up and kill it.  This must be >= circwindow
    465 * to avoid killing innocent circuits, and >= circwindow*2 to give
    466 * leaky-pipe a chance of working someday. The ORCIRC_MAX_MIDDLE_KILL_THRESH
    467 * ratio controls the margin of error between emitting a warning and
    468 * killing the circuit.
    469 */
    470 #define ORCIRC_MAX_MIDDLE_CELLS (CIRCWINDOW_START_MAX*2)
    471 /** Ratio of hard (circuit kill) to soft (warning) thresholds for the
    472 * ORCIRC_MAX_MIDDLE_CELLS tests.
    473 */
    474 #define ORCIRC_MAX_MIDDLE_KILL_THRESH (1.1f)
    475 
    476 /* Cell commands.  These values are defined in tor-spec.txt. */
    477 #define CELL_PADDING 0
    478 #define CELL_CREATE 1
    479 #define CELL_CREATED 2
    480 #define CELL_RELAY 3
    481 #define CELL_DESTROY 4
    482 #define CELL_CREATE_FAST 5
    483 #define CELL_CREATED_FAST 6
    484 #define CELL_VERSIONS 7
    485 #define CELL_NETINFO 8
    486 #define CELL_RELAY_EARLY 9
    487 #define CELL_CREATE2 10
    488 #define CELL_CREATED2 11
    489 #define CELL_PADDING_NEGOTIATE 12
    490 
    491 #define CELL_VPADDING 128
    492 #define CELL_CERTS 129
    493 #define CELL_AUTH_CHALLENGE 130
    494 #define CELL_AUTHENTICATE 131
    495 #define CELL_AUTHORIZE 132
    496 #define CELL_COMMAND_MAX_ 132
    497 
    498 /** How long to test reachability before complaining to the user. */
    499 #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
    500 
    501 /** Legal characters in a nickname. */
    502 #define LEGAL_NICKNAME_CHARACTERS \
    503  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    504 
    505 /** Name chosen by routers that don't configure nicknames */
    506 #define UNNAMED_ROUTER_NICKNAME "Unnamed"
    507 
    508 /** Number of bytes in a SOCKS4 header. */
    509 #define SOCKS4_NETWORK_LEN 8
    510 
    511 /*
    512 * Relay cell body (V0):
    513 *         Relay command           [1 byte]
    514 *         Recognized              [2 bytes]
    515 *         Stream ID               [2 bytes]
    516 *         Partial SHA-1           [4 bytes]
    517 *         Length                  [2 bytes]
    518 *         Relay payload           [498 bytes]
    519 *
    520 * Relay cell body (V1):
    521 *         Tag                     [16 bytes]
    522 *         Command                 [1 byte]
    523 *         Length                  [2 bytes]
    524 *         Stream ID               [2 bytes, Optional, depends on command]
    525 *         Relay payload           [488 bytes _or_ 490 bytes]
    526 */
    527 
    528 /** Number of bytes in a cell, minus cell header. */
    529 #define CELL_PAYLOAD_SIZE 509
    530 /** Number of bytes in a cell transmitted over the network, in the longest
    531 * form */
    532 #define CELL_MAX_NETWORK_SIZE 514
    533 
    534 /** Maximum length of a header on a variable-length cell. */
    535 #define VAR_CELL_MAX_HEADER_SIZE 7
    536 
    537 /** Which format should we use for relay cells? */
    538 typedef enum relay_cell_fmt_t {
    539  /** Our original format, with 2 byte recognized field and a 4-byte digest */
    540  RELAY_CELL_FORMAT_V0,
    541  /** New format introduced for CGO, with 16 byte tag. */
    542  RELAY_CELL_FORMAT_V1,
    543 } relay_cell_fmt_t;
    544 
    545 static int get_cell_network_size(int wide_circ_ids);
    546 static inline int get_cell_network_size(int wide_circ_ids)
    547 {
    548  return wide_circ_ids ? CELL_MAX_NETWORK_SIZE : CELL_MAX_NETWORK_SIZE - 2;
    549 }
    550 static int get_var_cell_header_size(int wide_circ_ids);
    551 static inline int get_var_cell_header_size(int wide_circ_ids)
    552 {
    553  return wide_circ_ids ? VAR_CELL_MAX_HEADER_SIZE :
    554    VAR_CELL_MAX_HEADER_SIZE - 2;
    555 }
    556 static int get_circ_id_size(int wide_circ_ids);
    557 static inline int get_circ_id_size(int wide_circ_ids)
    558 {
    559  return wide_circ_ids ? 4 : 2;
    560 }
    561 
    562 /** Number of bytes used for a relay cell's header, in the v0 format. */
    563 #define RELAY_HEADER_SIZE_V0 (1+2+2+4+2)
    564 /** Number of bytes used for a relay cell's header, in the v1 format,
    565 * if no StreamID is used. */
    566 #define RELAY_HEADER_SIZE_V1_NO_STREAM_ID (16+1+2)
    567 /** Number of bytes used for a relay cell's header, in the v1 format,
    568 * if a StreamID is used. */
    569 #define RELAY_HEADER_SIZE_V1_WITH_STREAM_ID (16+1+2+2)
    570 
    571 /** Largest number of bytes that can fit in any relay cell payload.
    572 *
    573 * Note that the actual maximum may be smaller if the V1 cell format
    574 * is in use; see relay_cell_max_payload_size() for the real maximum.
    575 */
    576 #define RELAY_PAYLOAD_SIZE_MAX (CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE_V0)
    577 
    578 /** Smallest capacity of any relay cell payload. */
    579 #define RELAY_PAYLOAD_SIZE_MIN \
    580  (CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE_V1_WITH_STREAM_ID)
    581 
    582 #ifdef TOR_UNIT_TESTS
    583 // This name is for testing only.
    584 #define RELAY_PAYLOAD_SIZE RELAY_PAYLOAD_SIZE_MAX
    585 #endif
    586 
    587 /** Identifies a circuit on an or_connection */
    588 typedef uint32_t circid_t;
    589 /** Identifies a stream on a circuit */
    590 typedef uint16_t streamid_t;
    591 
    592 /* channel_t typedef; struct channel_t is in channel.h */
    593 
    594 typedef struct channel_t channel_t;
    595 
    596 /* channel_listener_t typedef; struct channel_listener_t is in channel.h */
    597 
    598 typedef struct channel_listener_t channel_listener_t;
    599 
    600 /* TLS channel stuff */
    601 
    602 typedef struct channel_tls_t channel_tls_t;
    603 
    604 /* circuitmux_t typedef; struct circuitmux_t is in circuitmux.h */
    605 
    606 typedef struct circuitmux_t circuitmux_t;
    607 
    608 typedef struct cell_t cell_t;
    609 typedef struct var_cell_t var_cell_t;
    610 typedef struct packed_cell_t packed_cell_t;
    611 typedef struct cell_queue_t cell_queue_t;
    612 typedef struct destroy_cell_t destroy_cell_t;
    613 typedef struct destroy_cell_queue_t destroy_cell_queue_t;
    614 typedef struct ext_or_cmd_t ext_or_cmd_t;
    615 
    616 #ifdef TOR_UNIT_TESTS
    617 /* This is a vestigial type used only for testing.
    618 * All current code should instead use relay_msg_t and related accessors.
    619 */
    620 
    621 /** Beginning of a RELAY cell payload. */
    622 typedef struct {
    623  uint8_t command; /**< The end-to-end relay command. */
    624  uint16_t recognized; /**< Used to tell whether cell is for us. */
    625  streamid_t stream_id; /**< Which stream is this cell associated with? */
    626  char integrity[4]; /**< Used to tell whether cell is corrupted. */
    627  uint16_t length; /**< How long is the payload body? */
    628 } relay_header_t;
    629 #endif
    630 
    631 typedef struct socks_request_t socks_request_t;
    632 typedef struct entry_port_cfg_t entry_port_cfg_t;
    633 typedef struct server_port_cfg_t server_port_cfg_t;
    634 
    635 /** Minimum length of the random part of an AUTH_CHALLENGE cell. */
    636 #define OR_AUTH_CHALLENGE_LEN 32
    637 
    638 /**
    639 * @name Certificate types for CERTS cells.
    640 *
    641 * These values are defined by the protocol, and affect how an X509
    642 * certificate in a CERTS cell is interpreted and used.
    643 *
    644 * @{ */
    645 /** A certificate that authenticates a TLS link key.  The subject key
    646 * must match the key used in the TLS handshake; it must be signed by
    647 * the identity key. */
    648 #define OR_CERT_TYPE_TLS_LINK 1
    649 /** A self-signed identity certificate. The subject key must be a
    650 * 1024-bit RSA key. */
    651 #define OR_CERT_TYPE_ID_1024 2
    652 /** A certificate that authenticates a key used in an AUTHENTICATE cell
    653 * in the v3 handshake.  The subject key must be a 1024-bit RSA key; it
    654 * must be signed by the identity key */
    655 #define OR_CERT_TYPE_AUTH_1024 3
    656 /* DOCDOC */
    657 #define OR_CERT_TYPE_RSA_ED_CROSSCERT 7
    658 /**@}*/
    659 
    660 /** The first supported type of AUTHENTICATE cell.  It contains
    661 * a bunch of structures signed with an RSA1024 key.  The signed
    662 * structures include a HMAC using negotiated TLS secrets, and a digest
    663 * of all cells sent or received before the AUTHENTICATE cell (including
    664 * the random server-generated AUTH_CHALLENGE cell).
    665 */
    666 #define AUTHTYPE_RSA_SHA256_TLSSECRET 1
    667 /** As AUTHTYPE_RSA_SHA256_TLSSECRET, but instead of using the
    668 * negotiated TLS secrets, uses exported keying material from the TLS
    669 * session as described in RFC 5705.
    670 *
    671 * Not used by today's tors, since everything that supports this
    672 * also supports ED25519_SHA256_5705, which is better.
    673 **/
    674 #define AUTHTYPE_RSA_SHA256_RFC5705 2
    675 /** As AUTHTYPE_RSA_SHA256_RFC5705, but uses an Ed25519 identity key to
    676 * authenticate.  */
    677 #define AUTHTYPE_ED25519_SHA256_RFC5705 3
    678 /*
    679 * NOTE: authchallenge_type_is_better() relies on these AUTHTYPE codes
    680 * being sorted in order of preference.  If we someday add one with
    681 * a higher numerical value that we don't like as much, we should revise
    682 * authchallenge_type_is_better().
    683 */
    684 
    685 /** The length of the part of the AUTHENTICATE cell body that the client and
    686 * server can generate independently (when using RSA_SHA256_TLSSECRET). It
    687 * contains everything except the client's timestamp, the client's randomly
    688 * generated nonce, and the signature. */
    689 #define V3_AUTH_FIXED_PART_LEN (8+(32*6))
    690 /** The length of the part of the AUTHENTICATE cell body that the client
    691 * signs. */
    692 #define V3_AUTH_BODY_LEN (V3_AUTH_FIXED_PART_LEN + 8 + 16)
    693 
    694 typedef struct or_handshake_certs_t or_handshake_certs_t;
    695 typedef struct or_handshake_state_t or_handshake_state_t;
    696 
    697 /** Length of Extended ORPort connection identifier. */
    698 #define EXT_OR_CONN_ID_LEN DIGEST_LEN /* 20 */
    699 
    700 typedef struct connection_t connection_t;
    701 typedef struct control_connection_t control_connection_t;
    702 typedef struct dir_connection_t dir_connection_t;
    703 typedef struct edge_connection_t edge_connection_t;
    704 typedef struct entry_connection_t entry_connection_t;
    705 typedef struct listener_connection_t listener_connection_t;
    706 typedef struct or_connection_t or_connection_t;
    707 
    708 /** Cast a connection_t subtype pointer to a connection_t **/
    709 #define TO_CONN(c) (&(((c)->base_)))
    710 
    711 /** Cast a entry_connection_t subtype pointer to a connection_t **/
    712 #define ENTRY_TO_CONN(c) (TO_CONN(ENTRY_TO_EDGE_CONN(c)))
    713 
    714 typedef struct addr_policy_t addr_policy_t;
    715 
    716 typedef struct cached_dir_t cached_dir_t;
    717 
    718 /** Enum used to remember where a signed_descriptor_t is stored and how to
    719 * manage the memory for signed_descriptor_body.  */
    720 typedef enum {
    721  /** The descriptor isn't stored on disk at all: the copy in memory is
    722   * canonical; the saved_offset field is meaningless. */
    723  SAVED_NOWHERE=0,
    724  /** The descriptor is stored in the cached_routers file: the
    725   * signed_descriptor_body is meaningless; the signed_descriptor_len and
    726   * saved_offset are used to index into the mmaped cache file. */
    727  SAVED_IN_CACHE,
    728  /** The descriptor is stored in the cached_routers.new file: the
    729   * signed_descriptor_body and saved_offset fields are both set. */
    730  /* FFFF (We could also mmap the file and grow the mmap as needed, or
    731   * lazy-load the descriptor text by using seek and read.  We don't, for
    732   * now.)
    733   */
    734  SAVED_IN_JOURNAL
    735 } saved_location_t;
    736 #define saved_location_bitfield_t ENUM_BF(saved_location_t)
    737 
    738 /** Enumeration: what directory object is being downloaded?
    739 * This determines which schedule is selected to perform the download. */
    740 typedef enum {
    741  DL_SCHED_GENERIC = 0,
    742  DL_SCHED_CONSENSUS = 1,
    743  DL_SCHED_BRIDGE = 2,
    744 } download_schedule_t;
    745 #define download_schedule_bitfield_t ENUM_BF(download_schedule_t)
    746 
    747 /** Enumeration: is the download schedule for downloading from an authority,
    748 * or from any available directory mirror?
    749 * During bootstrap, "any" means a fallback (or an authority, if there
    750 * are no fallbacks).
    751 * When we have a valid consensus, "any" means any directory server. */
    752 typedef enum {
    753  DL_WANT_ANY_DIRSERVER = 0,
    754  DL_WANT_AUTHORITY = 1,
    755 } download_want_authority_t;
    756 #define download_want_authority_bitfield_t \
    757                                        ENUM_BF(download_want_authority_t)
    758 
    759 /** Enumeration: do we want to increment the schedule position each time a
    760 * connection is attempted (these attempts can be concurrent), or do we want
    761 * to increment the schedule position after a connection fails? */
    762 typedef enum {
    763  DL_SCHED_INCREMENT_FAILURE = 0,
    764  DL_SCHED_INCREMENT_ATTEMPT = 1,
    765 } download_schedule_increment_t;
    766 #define download_schedule_increment_bitfield_t \
    767                                        ENUM_BF(download_schedule_increment_t)
    768 
    769 typedef struct download_status_t download_status_t;
    770 
    771 /** If n_download_failures is this high, the download can never happen. */
    772 #define IMPOSSIBLE_TO_DOWNLOAD 255
    773 
    774 /** The max size we expect router descriptor annotations we create to
    775 * be. We'll accept larger ones if we see them on disk, but we won't
    776 * create any that are larger than this. */
    777 #define ROUTER_ANNOTATION_BUF_LEN 256
    778 
    779 typedef struct signed_descriptor_t signed_descriptor_t;
    780 
    781 /** Flags used to summarize the declared protocol versions of a relay,
    782 * so we don't need to parse them again and again. */
    783 typedef struct protover_summary_flags_t {
    784  /** True iff we have a proto line for this router, or a versions line
    785   * from which we could infer the protocols. */
    786  unsigned int protocols_known:1;
    787 
    788  /** True iff this router has a version or protocol list that allows it to
    789   * accept EXTEND2 cells. This requires Relay=2. */
    790  unsigned int supports_extend2_cells:1;
    791 
    792  /** True iff this router has a version or protocol list that allows it to
    793   * accept IPv6 connections. This requires Relay=2 or Relay=3. */
    794  unsigned int supports_accepting_ipv6_extends:1;
    795 
    796  /** True iff this router has a version or protocol list that allows it to
    797   * initiate IPv6 connections. This requires Relay=3. */
    798  unsigned int supports_initiating_ipv6_extends:1;
    799 
    800  /** True iff this router has a version or protocol list that allows it to
    801   * consider IPv6 connections canonical. This requires Relay=3. */
    802  unsigned int supports_canonical_ipv6_conns:1;
    803 
    804  /** True iff this router has a protocol list that allows it to negotiate
    805   * ed25519 identity keys on a link handshake with us. This
    806   * requires LinkAuth=3. */
    807  unsigned int supports_ed25519_link_handshake_compat:1;
    808 
    809  /** True iff this router has a protocol list that allows it to negotiate
    810   * ed25519 identity keys on a link handshake, at all. This requires some
    811   * LinkAuth=X for X >= 3. */
    812  unsigned int supports_ed25519_link_handshake_any:1;
    813 
    814  /** True iff this router has a protocol list that allows it to be an
    815   * introduction point supporting ed25519 authentication key which is part of
    816   * the v3 protocol detailed in proposal 224. This requires HSIntro=4. */
    817  unsigned int supports_ed25519_hs_intro : 1;
    818 
    819  /** True iff this router has a protocol list that allows it to support the
    820   * ESTABLISH_INTRO DoS cell extension. Requires HSIntro=5. */
    821  unsigned int supports_establish_intro_dos_extension : 1;
    822 
    823  /** True iff this router has a protocol list that allows it to be an hidden
    824   * service directory supporting version 3 as seen in proposal 224. This
    825   * requires HSDir=2. */
    826  unsigned int supports_v3_hsdir : 1;
    827 
    828  /** True iff this router has a protocol list that allows it to be an hidden
    829   * service rendezvous point supporting version 3 as seen in proposal 224.
    830   * This requires HSRend=2. */
    831  unsigned int supports_v3_rendezvous_point: 1;
    832 
    833  /** True iff this router has a protocol list that allows clients to
    834   * negotiate hs circuit setup padding. Requires Padding=2. */
    835  unsigned int supports_hs_setup_padding : 1;
    836 
    837  /** True iff this router supports congestion control.
    838   * Requires both FlowCtrl=2 *and* Relay=4 */
    839  unsigned int supports_congestion_control : 1;
    840 
    841  /** True iff this router supports conflux. */
    842  unsigned int supports_conflux : 1;
    843 
    844  /** True iff this router supports CGO. */
    845  unsigned int supports_cgo : 1;
    846 
    847  /** True iff this router supports ntorv3 */
    848  unsigned int supports_ntor_v3 : 1;
    849 } protover_summary_flags_t;
    850 
    851 typedef struct routerinfo_t routerinfo_t;
    852 typedef struct extrainfo_t extrainfo_t;
    853 typedef struct routerstatus_t routerstatus_t;
    854 
    855 typedef struct microdesc_t microdesc_t;
    856 typedef struct node_t node_t;
    857 typedef struct vote_microdesc_hash_t vote_microdesc_hash_t;
    858 typedef struct vote_routerstatus_t vote_routerstatus_t;
    859 typedef struct document_signature_t document_signature_t;
    860 typedef struct networkstatus_voter_info_t networkstatus_voter_info_t;
    861 typedef struct networkstatus_sr_info_t networkstatus_sr_info_t;
    862 
    863 /** Enumerates recognized flavors of a consensus networkstatus document.  All
    864 * flavors of a consensus are generated from the same set of votes, but they
    865 * present different types information to different versions of Tor. */
    866 typedef enum {
    867  FLAV_NS = 0,
    868  FLAV_MICRODESC = 1,
    869 } consensus_flavor_t;
    870 
    871 /** How many different consensus flavors are there? */
    872 #define N_CONSENSUS_FLAVORS ((int)(FLAV_MICRODESC)+1)
    873 
    874 typedef struct networkstatus_t networkstatus_t;
    875 typedef struct ns_detached_signatures_t ns_detached_signatures_t;
    876 typedef struct desc_store_t desc_store_t;
    877 typedef struct routerlist_t routerlist_t;
    878 typedef struct extend_info_t extend_info_t;
    879 typedef struct authority_cert_t authority_cert_t;
    880 
    881 /** Bitfield enum type listing types of information that directory authorities
    882 * can be authoritative about, and that directory caches may or may not cache.
    883 *
    884 * Note that the granularity here is based on authority granularity and on
    885 * cache capabilities.  Thus, one particular bit may correspond in practice to
    886 * a few types of directory info, so long as every authority that pronounces
    887 * officially about one of the types prounounces officially about all of them,
    888 * and so long as every cache that caches one of them caches all of them.
    889 */
    890 typedef enum {
    891  NO_DIRINFO      = 0,
    892  /** Serves/signs v3 directory information: votes, consensuses, certs */
    893  V3_DIRINFO      = 1 << 2,
    894  /** Serves bridge descriptors. */
    895  BRIDGE_DIRINFO  = 1 << 4,
    896  /** Serves extrainfo documents. */
    897  EXTRAINFO_DIRINFO=1 << 5,
    898  /** Serves microdescriptors. */
    899  MICRODESC_DIRINFO=1 << 6,
    900 } dirinfo_type_t;
    901 
    902 #define ALL_DIRINFO ((dirinfo_type_t)((1<<7)-1))
    903 
    904 #define ONION_HANDSHAKE_TYPE_TAP  0x0000
    905 #define ONION_HANDSHAKE_TYPE_FAST 0x0001
    906 #define ONION_HANDSHAKE_TYPE_NTOR 0x0002
    907 #define ONION_HANDSHAKE_TYPE_NTOR_V3 0x0003
    908 #define MAX_ONION_HANDSHAKE_TYPE 0x0003
    909 
    910 typedef struct onion_handshake_state_t onion_handshake_state_t;
    911 typedef struct relay_crypto_t relay_crypto_t;
    912 typedef struct crypt_path_t crypt_path_t;
    913 typedef struct crypt_path_reference_t crypt_path_reference_t;
    914 
    915 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
    916 
    917 typedef struct cpath_build_state_t cpath_build_state_t;
    918 
    919 struct create_cell_t;
    920 
    921 /** Entry in the cell stats list of a circuit; used only if CELL_STATS
    922 * events are enabled. */
    923 typedef struct testing_cell_stats_entry_t {
    924  uint8_t command; /**< cell command number. */
    925  /** Waiting time in centiseconds if this event is for a removed cell,
    926   * or 0 if this event is for adding a cell to the queue.  22 bits can
    927   * store more than 11 hours, enough to assume that a circuit with this
    928   * delay would long have been closed. */
    929  unsigned int waiting_time:22;
    930  unsigned int removed:1; /**< 0 for added to, 1 for removed from queue. */
    931  unsigned int exitward:1; /**< 0 for app-ward, 1 for exit-ward. */
    932 } testing_cell_stats_entry_t;
    933 
    934 typedef struct circuit_t circuit_t;
    935 typedef struct origin_circuit_t origin_circuit_t;
    936 typedef struct or_circuit_t or_circuit_t;
    937 
    938 /** Largest number of relay_early cells that we can send on a given
    939 * circuit. */
    940 #define MAX_RELAY_EARLY_CELLS_PER_CIRCUIT 8
    941 
    942 typedef enum path_state_t path_state_t;
    943 #define path_state_bitfield_t ENUM_BF(path_state_t)
    944 
    945 #if REND_COOKIE_LEN != DIGEST_LEN
    946 #error "The REND_TOKEN_LEN macro assumes REND_COOKIE_LEN == DIGEST_LEN"
    947 #endif
    948 #define REND_TOKEN_LEN DIGEST_LEN
    949 
    950 /** Convert a circuit subtype to a circuit_t. */
    951 #define TO_CIRCUIT(x)  (&((x)->base_))
    952 
    953 /** @name Isolation flags
    954 
    955    Ways to isolate client streams
    956 
    957    @{
    958 */
    959 /** Isolate based on destination port */
    960 #define ISO_DESTPORT    (1u<<0)
    961 /** Isolate based on destination address */
    962 #define ISO_DESTADDR    (1u<<1)
    963 /** Isolate based on SOCKS authentication */
    964 #define ISO_SOCKSAUTH   (1u<<2)
    965 /** Isolate based on client protocol choice */
    966 #define ISO_CLIENTPROTO (1u<<3)
    967 /** Isolate based on client address */
    968 #define ISO_CLIENTADDR  (1u<<4)
    969 /** Isolate based on session group (always on). */
    970 #define ISO_SESSIONGRP  (1u<<5)
    971 /** Isolate based on newnym epoch (always on). */
    972 #define ISO_NYM_EPOCH   (1u<<6)
    973 /** Isolate all streams (Internal only). */
    974 #define ISO_STREAM      (1u<<7)
    975 /**@}*/
    976 
    977 /** Default isolation level for ports. */
    978 #define ISO_DEFAULT (ISO_CLIENTADDR|ISO_SOCKSAUTH|ISO_SESSIONGRP|ISO_NYM_EPOCH)
    979 
    980 /** Indicates that we haven't yet set a session group on a port_cfg_t. */
    981 #define SESSION_GROUP_UNSET -1
    982 /** Session group reserved for directory connections */
    983 #define SESSION_GROUP_DIRCONN -2
    984 /** Session group reserved for resolve requests launched by a controller */
    985 #define SESSION_GROUP_CONTROL_RESOLVE -3
    986 /** First automatically allocated session group number */
    987 #define SESSION_GROUP_FIRST_AUTO -4
    988 
    989 typedef struct port_cfg_t port_cfg_t;
    990 typedef struct routerset_t routerset_t;
    991 
    992 /** A magic value for the (Socks|OR|...)Port options below, telling Tor
    993 * to pick its own port. */
    994 #define CFG_AUTO_PORT 0xc4005e
    995 
    996 typedef struct or_options_t or_options_t;
    997 
    998 typedef struct or_state_t or_state_t;
    999 
   1000 #define MAX_SOCKS_ADDR_LEN 256
   1001 
   1002 /********************************* circuitbuild.c **********************/
   1003 
   1004 /** How many hops does a general-purpose circuit have by default? */
   1005 #define DEFAULT_ROUTE_LEN 3
   1006 
   1007 /* Circuit Build Timeout "public" structures. */
   1008 
   1009 /** Precision multiplier for the Bw weights */
   1010 #define BW_WEIGHT_SCALE   10000
   1011 #define BW_MIN_WEIGHT_SCALE 1
   1012 #define BW_MAX_WEIGHT_SCALE INT32_MAX
   1013 
   1014 typedef struct circuit_build_times_t circuit_build_times_t;
   1015 
   1016 /********************************* config.c ***************************/
   1017 
   1018 /********************************* connection_edge.c *************************/
   1019 
   1020 /** Enumerates possible origins of a client-side address mapping. */
   1021 typedef enum {
   1022  /** We're remapping this address because the controller told us to. */
   1023  ADDRMAPSRC_CONTROLLER,
   1024  /** We're remapping this address because of an AutomapHostsOnResolve
   1025   * configuration. */
   1026  ADDRMAPSRC_AUTOMAP,
   1027  /** We're remapping this address because our configuration (via torrc, the
   1028   * command line, or a SETCONF command) told us to. */
   1029  ADDRMAPSRC_TORRC,
   1030  /** We're remapping this address because we have TrackHostExit configured,
   1031   * and we want to remember to use the same exit next time. */
   1032  ADDRMAPSRC_TRACKEXIT,
   1033  /** We're remapping this address because we got a DNS resolution from a
   1034   * Tor server that told us what its value was. */
   1035  ADDRMAPSRC_DNS,
   1036 
   1037  /** No remapping has occurred.  This isn't a possible value for an
   1038   * addrmap_entry_t; it's used as a null value when we need to answer "Why
   1039   * did this remapping happen." */
   1040  ADDRMAPSRC_NONE
   1041 } addressmap_entry_source_t;
   1042 #define addressmap_entry_source_bitfield_t ENUM_BF(addressmap_entry_source_t)
   1043 
   1044 #define WRITE_STATS_INTERVAL (24*60*60)
   1045 
   1046 /********************************* dirvote.c ************************/
   1047 
   1048 typedef struct vote_timing_t vote_timing_t;
   1049 
   1050 /********************************* microdesc.c *************************/
   1051 
   1052 typedef struct microdesc_cache_t microdesc_cache_t;
   1053 
   1054 /** The maximum number of non-circuit-build-timeout failures a hidden
   1055 * service client will tolerate while trying to build a circuit to an
   1056 * introduction point. */
   1057 #define MAX_INTRO_POINT_REACHABILITY_FAILURES 5
   1058 
   1059 /** The minimum and maximum number of distinct INTRODUCE2 cells which a
   1060 * hidden service's introduction point will receive before it begins to
   1061 * expire. */
   1062 #define INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS 16384
   1063 /* Double the minimum value so the interval is [min, min * 2]. */
   1064 #define INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS \
   1065  (INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS * 2)
   1066 
   1067 /** The minimum number of seconds that an introduction point will last
   1068 * before expiring due to old age.  (If it receives
   1069 * INTRO_POINT_LIFETIME_INTRODUCTIONS INTRODUCE2 cells, it may expire
   1070 * sooner.)
   1071 *
   1072 * XXX Should this be configurable? */
   1073 #define INTRO_POINT_LIFETIME_MIN_SECONDS (18*60*60)
   1074 /** The maximum number of seconds that an introduction point will last
   1075 * before expiring due to old age.
   1076 *
   1077 * XXX Should this be configurable? */
   1078 #define INTRO_POINT_LIFETIME_MAX_SECONDS (24*60*60)
   1079 
   1080 /** The maximum number of circuit creation retry we do to an intro point
   1081 * before giving up. We try to reuse intro point that fails during their
   1082 * lifetime so this is a hard limit on the amount of time we do that. */
   1083 #define MAX_INTRO_POINT_CIRCUIT_RETRIES 3
   1084 
   1085 /********************************* routerlist.c ***************************/
   1086 
   1087 typedef struct dir_server_t dir_server_t;
   1088 
   1089 #define RELAY_REQUIRED_MIN_BANDWIDTH (75*1024)
   1090 #define BRIDGE_REQUIRED_MIN_BANDWIDTH (50*1024)
   1091 
   1092 #define ROUTER_MAX_DECLARED_BANDWIDTH INT32_MAX
   1093 
   1094 typedef struct tor_version_t tor_version_t;
   1095 
   1096 #endif /* !defined(TOR_OR_H) */