entrynodes.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file entrynodes.h
  8. * \brief Header file for circuitbuild.c.
  9. **/
  10. #ifndef TOR_ENTRYNODES_H
  11. #define TOR_ENTRYNODES_H
  12. #include "handles.h"
  13. /* Forward declare for guard_selection_t; entrynodes.c has the real struct */
  14. typedef struct guard_selection_s guard_selection_t;
  15. /* Forward declare for entry_guard_t; the real declaration is private. */
  16. typedef struct entry_guard_t entry_guard_t;
  17. /* Forward declaration for circuit_guard_state_t; the real declaration is
  18. private. */
  19. typedef struct circuit_guard_state_t circuit_guard_state_t;
  20. /* Forward declaration for entry_guard_restriction_t; the real declaration is
  21. private. */
  22. typedef struct entry_guard_restriction_t entry_guard_restriction_t;
  23. /*
  24. XXXX Prop271 undefine this in order to disable all legacy guard functions.
  25. */
  26. #define ENABLE_LEGACY_GUARD_ALGORITHM
  27. /* Information about a guard's pathbias status.
  28. * These fields are used in circpathbias.c to try to detect entry
  29. * nodes that are failing circuits at a suspicious frequency.
  30. */
  31. typedef struct guard_pathbias_t {
  32. unsigned int path_bias_noticed : 1; /**< Did we alert the user about path
  33. * bias for this node already? */
  34. unsigned int path_bias_warned : 1; /**< Did we alert the user about path bias
  35. * for this node already? */
  36. unsigned int path_bias_extreme : 1; /**< Did we alert the user about path
  37. * bias for this node already? */
  38. unsigned int path_bias_disabled : 1; /**< Have we disabled this node because
  39. * of path bias issues? */
  40. unsigned int path_bias_use_noticed : 1; /**< Did we alert the user about path
  41. * use bias for this node already? */
  42. unsigned int path_bias_use_extreme : 1; /**< Did we alert the user about path
  43. * use bias for this node already? */
  44. double circ_attempts; /**< Number of circuits this guard has "attempted" */
  45. double circ_successes; /**< Number of successfully built circuits using
  46. * this guard as first hop. */
  47. double successful_circuits_closed; /**< Number of circuits that carried
  48. * streams successfully. */
  49. double collapsed_circuits; /**< Number of fully built circuits that were
  50. * remotely closed before any streams were
  51. * attempted. */
  52. double unusable_circuits; /**< Number of circuits for which streams were
  53. * attempted, but none succeeded. */
  54. double timeouts; /**< Number of 'right-censored' circuit timeouts for this
  55. * guard. */
  56. double use_attempts; /**< Number of circuits we tried to use with streams */
  57. double use_successes; /**< Number of successfully used circuits using
  58. * this guard as first hop. */
  59. } guard_pathbias_t;
  60. #if defined(ENTRYNODES_PRIVATE)
  61. /**
  62. * @name values for entry_guard_t.is_reachable.
  63. *
  64. * See entry_guard_t.is_reachable for more information.
  65. */
  66. /**@{*/
  67. #define GUARD_REACHABLE_NO 0
  68. #define GUARD_REACHABLE_YES 1
  69. #define GUARD_REACHABLE_MAYBE 2
  70. /**@}*/
  71. /** An entry_guard_t represents our information about a chosen long-term
  72. * first hop, known as a "helper" node in the literature. We can't just
  73. * use a node_t, since we want to remember these even when we
  74. * don't have any directory info. */
  75. struct entry_guard_t {
  76. HANDLE_ENTRY(entry_guard, entry_guard_t);
  77. char nickname[MAX_HEX_NICKNAME_LEN+1];
  78. char identity[DIGEST_LEN];
  79. ed25519_public_key_t ed_id;
  80. /**
  81. * @name new guard selection algorithm fields.
  82. *
  83. * Only the new (prop271) algorithm uses these. For a more full
  84. * description of the algorithm, see the module documentation for
  85. * entrynodes.c
  86. */
  87. /**@{*/
  88. /* == Persistent fields, present for all sampled guards. */
  89. /** When was this guard added to the sample? */
  90. time_t sampled_on_date;
  91. /** Since what date has this guard been "unlisted"? A guard counts as
  92. * unlisted if we have a live consensus that does not include it, or
  93. * if we have a live consensus that does not include it as a usable
  94. * guard. This field is zero when the guard is listed. */
  95. time_t unlisted_since_date; // can be zero
  96. /** What version of Tor added this guard to the sample? */
  97. char *sampled_by_version;
  98. /** Is this guard listed right now? If this is set, then
  99. * unlisted_since_date should be set too. */
  100. unsigned currently_listed : 1;
  101. /* == Persistent fields, for confirmed guards only */
  102. /** When was this guard confirmed? (That is, when did we first use it
  103. * successfully and decide to keep it?) This field is zero if this is not a
  104. * confirmed guard. */
  105. time_t confirmed_on_date; /* 0 if not confirmed */
  106. /**
  107. * In what order was this guard confirmed? Guards with lower indices
  108. * appear earlier on the confirmed list. If the confirmed list is compacted,
  109. * this field corresponds to the index of this guard on the confirmed list.
  110. *
  111. * This field is set to -1 if this guard is not confirmed.
  112. */
  113. int confirmed_idx; /* -1 if not confirmed; otherwise the order that this
  114. * item should occur in the CONFIRMED_GUARDS ordered
  115. * list */
  116. /**
  117. * Which selection does this guard belong to?
  118. */
  119. char *selection_name;
  120. /** Bridges only: address of the bridge. */
  121. tor_addr_port_t *bridge_addr;
  122. /* ==== Non-persistent fields. */
  123. /* == These are used by sampled guards */
  124. /** When did we last decide to try using this guard for a circuit? 0 for
  125. * "not since we started up." */
  126. time_t last_tried_to_connect;
  127. /** How reachable do we consider this guard to be? One of
  128. * GUARD_REACHABLE_NO, GUARD_REACHABLE_YES, or GUARD_REACHABLE_MAYBE. */
  129. unsigned is_reachable : 2;
  130. /** Boolean: true iff this guard is pending. A pending guard is one
  131. * that we have an in-progress circuit through, and which we do not plan
  132. * to try again until it either succeeds or fails. Primary guards can
  133. * never be pending. */
  134. unsigned is_pending : 1;
  135. /** If true, don't write this guard to disk. (Used for bridges with unknown
  136. * identities) */
  137. unsigned is_persistent : 1;
  138. /** When did we get the earliest connection failure for this guard?
  139. * We clear this field on a successful connect. We do _not_ clear it
  140. * when we mark the guard as "MAYBE" reachable.
  141. */
  142. time_t failing_since;
  143. /* == Set inclusion flags. */
  144. /** If true, this guard is in the filtered set. The filtered set includes
  145. * all sampled guards that our configuration allows us to use. */
  146. unsigned is_filtered_guard : 1;
  147. /** If true, this guard is in the usable filtered set. The usable filtered
  148. * set includes all filtered guards that are not believed to be
  149. * unreachable. (That is, those for which is_reachable is not
  150. * GUARD_REACHABLE_NO) */
  151. unsigned is_usable_filtered_guard : 1;
  152. unsigned is_primary:1;
  153. /** This string holds any fields that we are maintaining because
  154. * we saw them in the state, even if we don't understand them. */
  155. char *extra_state_fields;
  156. /** Backpointer to the guard selection that this guard belongs to. */
  157. guard_selection_t *in_selection;
  158. /**@}*/
  159. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  160. /**
  161. * @name legacy guard selection algorithm fields
  162. *
  163. * These are used and maintained by the legacy (pre-prop271) entry guard
  164. * algorithm. Most of them we will remove as prop271 gets implemented.
  165. * The rest we'll migrate over, if they are 100% semantically identical to
  166. * their prop271 equivalents. XXXXprop271
  167. */
  168. /**@{*/
  169. time_t chosen_on_date; /**< Approximately when was this guard added?
  170. * "0" if we don't know. */
  171. char *chosen_by_version; /**< What tor version added this guard? NULL
  172. * if we don't know. */
  173. unsigned int made_contact : 1; /**< 0 if we have never connected to this
  174. * router, 1 if we have. */
  175. unsigned int can_retry : 1; /**< Should we retry connecting to this entry,
  176. * in spite of having it marked as unreachable?*/
  177. unsigned int is_dir_cache : 1; /**< Is this node a directory cache? */
  178. time_t bad_since; /**< 0 if this guard is currently usable, or the time at
  179. * which it was observed to become (according to the
  180. * directory or the user configuration) unusable. */
  181. time_t unreachable_since; /**< 0 if we can connect to this guard, or the
  182. * time at which we first noticed we couldn't
  183. * connect to it. */
  184. time_t last_attempted; /**< 0 if we can connect to this guard, or the time
  185. * at which we last failed to connect to it. */
  186. /**}@*/
  187. #endif
  188. /** Path bias information for this guard. */
  189. guard_pathbias_t pb;
  190. };
  191. /**
  192. * Possible rules for a guard selection to follow
  193. */
  194. typedef enum guard_selection_type_t {
  195. /** Infer the type of this selection from its name. */
  196. GS_TYPE_INFER=0,
  197. /** Use the normal guard selection algorithm, taking our sample from the
  198. * complete list of guards in the consensus. */
  199. GS_TYPE_NORMAL=1,
  200. /** Use the normal guard selection algorithm, taking our sample from the
  201. * configured bridges, and allowing it to grow as large as all the configured
  202. * bridges */
  203. GS_TYPE_BRIDGE,
  204. /** Use the normal guard selection algorithm, taking our sample from the
  205. * set of filtered nodes. */
  206. GS_TYPE_RESTRICTED,
  207. /** Use the legacy (pre-prop271) guard selection algorithm and fields */
  208. GS_TYPE_LEGACY,
  209. } guard_selection_type_t;
  210. /**
  211. * All of the the context for guard selection on a particular client.
  212. *
  213. * We maintain multiple guard selection contexts for a client, depending
  214. * aspects on its current configuration -- whether an extremely
  215. * restrictive EntryNodes is used, whether UseBridges is enabled, and so
  216. * on.)
  217. *
  218. * See the module documentation for entrynodes.c for more information
  219. * about guard selection algorithms.
  220. */
  221. struct guard_selection_s {
  222. /**
  223. * The name for this guard-selection object. (Must not contain spaces).
  224. */
  225. char *name;
  226. /**
  227. * What rules does this guard-selection object follow?
  228. */
  229. guard_selection_type_t type;
  230. /**
  231. * A value of 1 means that primary_entry_guards is up-to-date; 0
  232. * means we need to recalculate it before using primary_entry_guards
  233. * or the is_primary flag on any guard.
  234. */
  235. int primary_guards_up_to_date;
  236. /**
  237. * A list of the sampled entry guards, as entry_guard_t structures.
  238. * Not in any particular order. When we 'sample' a guard, we are
  239. * noting it as a possible guard to pick in the future. The use of
  240. * sampling here prevents us from being forced by an attacker to try
  241. * every guard on the network. This list is persistent.
  242. */
  243. smartlist_t *sampled_entry_guards;
  244. /**
  245. * Ordered list (from highest to lowest priority) of guards that we
  246. * have successfully contacted and decided to use. Every member of
  247. * this list is a member of sampled_entry_guards. Every member should
  248. * have confirmed_on_date set, and have confirmed_idx greater than
  249. * any earlier member of the list.
  250. *
  251. * This list is persistent. It is a subset of the elements in
  252. * sampled_entry_guards, and its pointers point to elements of
  253. * sampled_entry_guards.
  254. */
  255. smartlist_t *confirmed_entry_guards;
  256. /**
  257. * Ordered list (from highest to lowest priority) of guards that we
  258. * are willing to use the most happily. These guards may or may not
  259. * yet be confirmed yet. If we can use one of these guards, we are
  260. * probably not on a network that is trying to restrict our guard
  261. * choices.
  262. *
  263. * This list is a subset of the elements in
  264. * sampled_entry_guards, and its pointers point to elements of
  265. * sampled_entry_guards.
  266. */
  267. smartlist_t *primary_entry_guards;
  268. /** When did we last successfully build a circuit or use a circuit? */
  269. time_t last_time_on_internet;
  270. /** What confirmed_idx value should the next-added member of
  271. * confirmed_entry_guards receive? */
  272. int next_confirmed_idx;
  273. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  274. /**
  275. * A list of our chosen entry guards, as entry_guard_t structures; this
  276. * preserves the pre-Prop271 behavior.
  277. */
  278. smartlist_t *chosen_entry_guards;
  279. /**
  280. * When we try to choose an entry guard, should we parse and add
  281. * config's EntryNodes first? This was formerly a global. This
  282. * preserves the pre-Prop271 behavior.
  283. */
  284. int should_add_entry_nodes;
  285. #endif
  286. };
  287. struct entry_guard_handle_t;
  288. /**
  289. * A restriction to remember which entry guards are off-limits for a given
  290. * circuit.
  291. *
  292. * Right now, we only use restrictions to block a single guard from being
  293. * selected; this mechanism is designed to be more extensible in the future,
  294. * however.
  295. *
  296. * Note: This mechanism is NOT for recording which guards are never to be
  297. * used: only which guards cannot be used on <em>one particular circuit</em>.
  298. */
  299. struct entry_guard_restriction_t {
  300. /**
  301. * The guard's RSA identity digest must not equal this.
  302. */
  303. uint8_t exclude_id[DIGEST_LEN];
  304. };
  305. /**
  306. * Per-circuit state to track whether we'll be able to use the circuit.
  307. */
  308. struct circuit_guard_state_t {
  309. /** Handle to the entry guard object for this circuit. */
  310. struct entry_guard_handle_t *guard;
  311. /** The time at which <b>state</b> last changed. */
  312. time_t state_set_at;
  313. /** One of GUARD_CIRC_STATE_* */
  314. uint8_t state;
  315. /**
  316. * A set of restrictions that were placed on this guard when we selected it
  317. * for this particular circuit. We need to remember the restrictions here,
  318. * since any guard that breaks these restrictions will not block this
  319. * circuit from becoming COMPLETE.
  320. */
  321. entry_guard_restriction_t *restrictions;
  322. };
  323. #endif
  324. /* Common entry points for old and new guard code */
  325. int guards_update_all(void);
  326. const node_t *guards_choose_guard(cpath_build_state_t *state,
  327. circuit_guard_state_t **guard_state_out);
  328. const node_t *guards_choose_dirguard(dirinfo_type_t info,
  329. circuit_guard_state_t **guard_state_out);
  330. #if 1
  331. /* XXXX NM I would prefer that all of this stuff be private to
  332. * entrynodes.c. */
  333. entry_guard_t *entry_guard_get_by_id_digest_for_guard_selection(
  334. guard_selection_t *gs, const char *digest);
  335. entry_guard_t *entry_guard_get_by_id_digest(const char *digest);
  336. void entry_guards_changed_for_guard_selection(guard_selection_t *gs);
  337. void entry_guards_changed(void);
  338. guard_selection_t * get_guard_selection_info(void);
  339. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  340. const smartlist_t *get_entry_guards_for_guard_selection(
  341. guard_selection_t *gs);
  342. const smartlist_t *get_entry_guards(void);
  343. #endif
  344. int num_live_entry_guards_for_guard_selection(
  345. guard_selection_t *gs,
  346. int for_directory);
  347. int num_live_entry_guards(int for_directory);
  348. #endif
  349. const node_t *entry_guard_find_node(const entry_guard_t *guard);
  350. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  351. void entry_guard_mark_bad(entry_guard_t *guard);
  352. #endif
  353. const char *entry_guard_get_rsa_id_digest(const entry_guard_t *guard);
  354. const char *entry_guard_describe(const entry_guard_t *guard);
  355. guard_pathbias_t *entry_guard_get_pathbias_state(entry_guard_t *guard);
  356. void circuit_guard_state_free(circuit_guard_state_t *state);
  357. int entry_guard_pick_for_circuit(guard_selection_t *gs,
  358. entry_guard_restriction_t *rst,
  359. const node_t **chosen_node_out,
  360. circuit_guard_state_t **guard_state_out);
  361. typedef enum {
  362. GUARD_USABLE_NEVER = -1,
  363. GUARD_MAYBE_USABLE_LATER = 0,
  364. GUARD_USABLE_NOW = 1,
  365. } guard_usable_t;
  366. guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p);
  367. void entry_guard_failed(circuit_guard_state_t **guard_state_p);
  368. void entry_guard_cancel(circuit_guard_state_t **guard_state_p);
  369. void entry_guard_chan_failed(channel_t *chan);
  370. int entry_guards_update_all(guard_selection_t *gs);
  371. int entry_guards_upgrade_waiting_circuits(guard_selection_t *gs,
  372. const smartlist_t *all_circuits,
  373. smartlist_t *newly_complete_out);
  374. int entry_guard_state_should_expire(circuit_guard_state_t *guard_state);
  375. void entry_guards_note_internet_connectivity(guard_selection_t *gs);
  376. int update_guard_selection_choice(const or_options_t *options);
  377. /* Used by bridges.c only. */
  378. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  379. void add_bridge_as_entry_guard(guard_selection_t *gs,
  380. const node_t *chosen);
  381. #endif
  382. int num_bridges_usable(void);
  383. #ifdef ENTRYNODES_PRIVATE
  384. /**
  385. * @name Default values for the parameters for the new (prop271) entry guard
  386. * algorithm.
  387. */
  388. /**@{*/
  389. /**
  390. * We never let our sampled guard set grow larger than this percentage
  391. * of the guards on the network.
  392. */
  393. #define DFLT_MAX_SAMPLE_THRESHOLD_PERCENT 30
  394. /**
  395. * We always try to make our sample contain at least this many guards.
  396. *
  397. * XXXX prop271 There was a MIN_SAMPLE_THRESHOLD in the proposal, but I
  398. * removed it in favor of MIN_FILTERED_SAMPLE_SIZE. -NM
  399. */
  400. #define DFLT_MIN_FILTERED_SAMPLE_SIZE 20
  401. /**
  402. * If a guard is unlisted for this many days in a row, we remove it.
  403. */
  404. #define DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS 20
  405. /**
  406. * We remove unconfirmed guards from the sample after this many days,
  407. * regardless of whether they are listed or unlisted.
  408. */
  409. #define DFLT_GUARD_LIFETIME_DAYS 120
  410. /**
  411. * We remove confirmed guards from the sample if they were sampled
  412. * GUARD_LIFETIME_DAYS ago and confirmed this many days ago.
  413. */
  414. #define DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS 60
  415. /**
  416. * How many guards do we try to keep on our primary guard list?
  417. */
  418. #define DFLT_N_PRIMARY_GUARDS 3
  419. /**
  420. * If we haven't successfully built or used a circuit in this long, then
  421. * consider that the internet is probably down.
  422. */
  423. #define DFLT_INTERNET_LIKELY_DOWN_INTERVAL (10*60)
  424. /**
  425. * If we're trying to connect to a nonprimary guard for at least this
  426. * many seconds, and we haven't gotten the connection to work, we will treat
  427. * lower-priority guards as usable.
  428. */
  429. #define DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT 15
  430. /**
  431. * If a circuit has been sitting around in 'waiting for better guard' state
  432. * for at least this long, we'll expire it.
  433. */
  434. #define DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT (10*60)
  435. /**
  436. * If our configuration retains fewer than this fraction of guards from the
  437. * torrc, we are in a restricted setting.
  438. */
  439. #define DFLT_MEANINGFUL_RESTRICTION_PERCENT 20
  440. /**
  441. * If our configuration retains fewer than this fraction of guards from the
  442. * torrc, we are in an extremely restricted setting, and should warn.
  443. */
  444. #define DFLT_EXTREME_RESTRICTION_PERCENT 1
  445. /**@}*/
  446. STATIC double get_max_sample_threshold(void);
  447. STATIC int get_min_filtered_sample_size(void);
  448. STATIC int get_remove_unlisted_guards_after_days(void);
  449. STATIC int get_guard_lifetime_days(void);
  450. STATIC int get_guard_confirmed_min_lifetime_days(void);
  451. STATIC int get_n_primary_guards(void);
  452. STATIC int get_internet_likely_down_interval(void);
  453. STATIC int get_nonprimary_guard_connect_timeout(void);
  454. STATIC int get_nonprimary_guard_idle_timeout(void);
  455. STATIC double get_meaningful_restriction_threshold(void);
  456. STATIC double get_extreme_restriction_threshold(void);
  457. // ---------- XXXX these functions and definitions are post-prop271.
  458. HANDLE_DECL(entry_guard, entry_guard_t, STATIC)
  459. STATIC guard_selection_type_t guard_selection_infer_type(
  460. guard_selection_type_t type_in,
  461. const char *name);
  462. STATIC guard_selection_t *guard_selection_new(const char *name,
  463. guard_selection_type_t type);
  464. STATIC guard_selection_t *get_guard_selection_by_name(
  465. const char *name, guard_selection_type_t type, int create_if_absent);
  466. STATIC void guard_selection_free(guard_selection_t *gs);
  467. MOCK_DECL(STATIC int, entry_guard_is_listed,
  468. (guard_selection_t *gs, const entry_guard_t *guard));
  469. STATIC const char *choose_guard_selection(const or_options_t *options,
  470. const networkstatus_t *ns,
  471. const char *old_selection,
  472. guard_selection_type_t *type_out);
  473. STATIC entry_guard_t *get_sampled_guard_with_id(guard_selection_t *gs,
  474. const uint8_t *rsa_id);
  475. MOCK_DECL(STATIC time_t, randomize_time, (time_t now, time_t max_backdate));
  476. STATIC entry_guard_t *entry_guard_add_to_sample(guard_selection_t *gs,
  477. const node_t *node);
  478. STATIC entry_guard_t *entry_guards_expand_sample(guard_selection_t *gs);
  479. STATIC char *entry_guard_encode_for_state(entry_guard_t *guard);
  480. STATIC entry_guard_t *entry_guard_parse_from_state(const char *s);
  481. STATIC void entry_guard_free(entry_guard_t *e);
  482. STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs);
  483. STATIC int entry_guards_all_primary_guards_are_down(guard_selection_t *gs);
  484. /**
  485. * @name Flags for sample_reachable_filtered_entry_guards()
  486. */
  487. /**@{*/
  488. #define SAMPLE_EXCLUDE_CONFIRMED (1u<<0)
  489. #define SAMPLE_EXCLUDE_PRIMARY (1u<<1)
  490. #define SAMPLE_EXCLUDE_PENDING (1u<<2)
  491. #define SAMPLE_NO_UPDATE_PRIMARY (1u<<3)
  492. /**@}*/
  493. STATIC entry_guard_t *sample_reachable_filtered_entry_guards(
  494. guard_selection_t *gs,
  495. const entry_guard_restriction_t *rst,
  496. unsigned flags);
  497. STATIC void entry_guard_consider_retry(entry_guard_t *guard);
  498. STATIC void make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard);
  499. STATIC void entry_guards_update_confirmed(guard_selection_t *gs);
  500. STATIC void entry_guards_update_primary(guard_selection_t *gs);
  501. STATIC int num_reachable_filtered_guards(guard_selection_t *gs,
  502. const entry_guard_restriction_t *rst);
  503. STATIC void sampled_guards_update_from_consensus(guard_selection_t *gs);
  504. /**
  505. * @name Possible guard-states for a circuit.
  506. */
  507. /**@{*/
  508. /** State for a circuit that can (so far as the guard subsystem is
  509. * concerned) be used for actual traffic as soon as it is successfully
  510. * opened. */
  511. #define GUARD_CIRC_STATE_USABLE_ON_COMPLETION 1
  512. /** State for an non-open circuit that we shouldn't use for actual
  513. * traffic, when it completes, unless other circuits to preferable
  514. * guards fail. */
  515. #define GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD 2
  516. /** State for an open circuit that we shouldn't use for actual traffic
  517. * unless other circuits to preferable guards fail. */
  518. #define GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD 3
  519. /** State for a circuit that can (so far as the guard subsystem is
  520. * concerned) be used for actual traffic. */
  521. #define GUARD_CIRC_STATE_COMPLETE 4
  522. /** State for a circuit that is unusable, and will not become usable. */
  523. #define GUARD_CIRC_STATE_DEAD 5
  524. /**@}*/
  525. STATIC void entry_guards_note_guard_failure(guard_selection_t *gs,
  526. entry_guard_t *guard);
  527. STATIC entry_guard_t *select_entry_guard_for_circuit(guard_selection_t *gs,
  528. const entry_guard_restriction_t *rst,
  529. unsigned *state_out);
  530. STATIC void mark_primary_guards_maybe_reachable(guard_selection_t *gs);
  531. STATIC unsigned entry_guards_note_guard_success(guard_selection_t *gs,
  532. entry_guard_t *guard,
  533. unsigned old_state);
  534. STATIC int entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b);
  535. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  536. // ---------- XXXX this stuff is pre-prop271.
  537. STATIC const node_t *add_an_entry_guard(guard_selection_t *gs,
  538. const node_t *chosen,
  539. int reset_status, int prepend,
  540. int for_discovery, int for_directory);
  541. STATIC int populate_live_entry_guards(smartlist_t *live_entry_guards,
  542. const smartlist_t *all_entry_guards,
  543. const node_t *chosen_exit,
  544. dirinfo_type_t dirinfo_type,
  545. int for_directory,
  546. int need_uptime, int need_capacity);
  547. STATIC int decide_num_guards(const or_options_t *options, int for_directory);
  548. STATIC void entry_guards_set_from_config(guard_selection_t *gs,
  549. const or_options_t *options);
  550. /** Flags to be passed to entry_is_live() to indicate what kind of
  551. * entry nodes we are looking for. */
  552. typedef enum {
  553. ENTRY_NEED_UPTIME = 1<<0,
  554. ENTRY_NEED_CAPACITY = 1<<1,
  555. ENTRY_ASSUME_REACHABLE = 1<<2,
  556. ENTRY_NEED_DESCRIPTOR = 1<<3,
  557. } entry_is_live_flags_t;
  558. STATIC const node_t *entry_is_live(const entry_guard_t *e,
  559. entry_is_live_flags_t flags,
  560. const char **msg);
  561. STATIC int entry_is_time_to_retry(const entry_guard_t *e, time_t now);
  562. #endif
  563. #endif
  564. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  565. void remove_all_entry_guards_for_guard_selection(guard_selection_t *gs);
  566. void remove_all_entry_guards(void);
  567. #endif
  568. struct bridge_info_t;
  569. void entry_guard_learned_bridge_identity(const tor_addr_port_t *addrport,
  570. const uint8_t *rsa_id_digest);
  571. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  572. void entry_guards_compute_status_for_guard_selection(
  573. guard_selection_t *gs, const or_options_t *options, time_t now);
  574. void entry_guards_compute_status(const or_options_t *options, time_t now);
  575. int entry_guard_register_connect_status_for_guard_selection(
  576. guard_selection_t *gs, const char *digest, int succeeded,
  577. int mark_relay_status, time_t now);
  578. int entry_guard_register_connect_status(const char *digest, int succeeded,
  579. int mark_relay_status, time_t now);
  580. void entry_nodes_should_be_added_for_guard_selection(guard_selection_t *gs);
  581. void entry_nodes_should_be_added(void);
  582. #endif
  583. int entry_list_is_constrained(const or_options_t *options);
  584. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  585. const node_t *choose_random_entry(cpath_build_state_t *state);
  586. const node_t *choose_random_dirguard(dirinfo_type_t t);
  587. #endif
  588. int guards_retry_optimistic(const or_options_t *options);
  589. int entry_guards_parse_state_for_guard_selection(
  590. guard_selection_t *gs, or_state_t *state, int set, char **msg);
  591. int entry_guards_parse_state(or_state_t *state, int set, char **msg);
  592. void entry_guards_update_state(or_state_t *state);
  593. int getinfo_helper_entry_guards(control_connection_t *conn,
  594. const char *question, char **answer,
  595. const char **errmsg);
  596. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  597. int is_node_used_as_guard_for_guard_selection(guard_selection_t *gs,
  598. const node_t *node);
  599. MOCK_DECL(int, is_node_used_as_guard, (const node_t *node));
  600. #endif
  601. int entries_known_but_down(const or_options_t *options);
  602. void entries_retry_all(const or_options_t *options);
  603. void entry_guards_free_all(void);
  604. double pathbias_get_close_success_count(entry_guard_t *guard);
  605. double pathbias_get_use_success_count(entry_guard_t *guard);
  606. /** Contains the bandwidth of a relay as a guard and as a non-guard
  607. * after the guardfraction has been considered. */
  608. typedef struct guardfraction_bandwidth_t {
  609. /** Bandwidth as a guard after guardfraction has been considered. */
  610. int guard_bw;
  611. /** Bandwidth as a non-guard after guardfraction has been considered. */
  612. int non_guard_bw;
  613. } guardfraction_bandwidth_t;
  614. int should_apply_guardfraction(const networkstatus_t *ns);
  615. void
  616. guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
  617. int orig_bandwidth,
  618. uint32_t guardfraction_percentage);
  619. #endif