entrynodes.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. /* Forward declare for guard_selection_t; entrynodes.c has the real struct */
  13. typedef struct guard_selection_s guard_selection_t;
  14. /* Forward declare for entry_guard_t; the real declaration is private. */
  15. typedef struct entry_guard_t entry_guard_t;
  16. /* Information about a guard's pathbias status.
  17. * These fields are used in circpathbias.c to try to detect entry
  18. * nodes that are failing circuits at a suspicious frequency.
  19. */
  20. typedef struct guard_pathbias_t {
  21. unsigned int path_bias_noticed : 1; /**< Did we alert the user about path
  22. * bias for this node already? */
  23. unsigned int path_bias_warned : 1; /**< Did we alert the user about path bias
  24. * for this node already? */
  25. unsigned int path_bias_extreme : 1; /**< Did we alert the user about path
  26. * bias for this node already? */
  27. unsigned int path_bias_disabled : 1; /**< Have we disabled this node because
  28. * of path bias issues? */
  29. unsigned int path_bias_use_noticed : 1; /**< Did we alert the user about path
  30. * use bias for this node already? */
  31. unsigned int path_bias_use_extreme : 1; /**< Did we alert the user about path
  32. * use bias for this node already? */
  33. double circ_attempts; /**< Number of circuits this guard has "attempted" */
  34. double circ_successes; /**< Number of successfully built circuits using
  35. * this guard as first hop. */
  36. double successful_circuits_closed; /**< Number of circuits that carried
  37. * streams successfully. */
  38. double collapsed_circuits; /**< Number of fully built circuits that were
  39. * remotely closed before any streams were
  40. * attempted. */
  41. double unusable_circuits; /**< Number of circuits for which streams were
  42. * attempted, but none succeeded. */
  43. double timeouts; /**< Number of 'right-censored' circuit timeouts for this
  44. * guard. */
  45. double use_attempts; /**< Number of circuits we tried to use with streams */
  46. double use_successes; /**< Number of successfully used circuits using
  47. * this guard as first hop. */
  48. } guard_pathbias_t;
  49. #if defined(ENTRYNODES_PRIVATE)
  50. /**
  51. * @name values for entry_guard_t.is_reachable.
  52. *
  53. * See entry_guard_t.is_reachable for more information.
  54. */
  55. /**@{*/
  56. #define GUARD_REACHABLE_NO 0
  57. #define GUARD_REACHABLE_YES 1
  58. #define GUARD_REACHABLE_MAYBE 2
  59. /**@}*/
  60. /** An entry_guard_t represents our information about a chosen long-term
  61. * first hop, known as a "helper" node in the literature. We can't just
  62. * use a node_t, since we want to remember these even when we
  63. * don't have any directory info. */
  64. struct entry_guard_t {
  65. char nickname[MAX_HEX_NICKNAME_LEN+1];
  66. char identity[DIGEST_LEN];
  67. ed25519_public_key_t ed_id;
  68. /**
  69. * @name new guard selection algorithm fields.
  70. *
  71. * Only the new (prop271) algorithm uses these. For a more full
  72. * description of the algorithm, see the module documentation for
  73. * entrynodes.c
  74. */
  75. /**@{*/
  76. /* == Persistent fields, present for all sampled guards. */
  77. /** When was this guard added to the sample? */
  78. time_t sampled_on_date;
  79. /** Since what date has this guard been "unlisted"? A guard counts as
  80. * unlisted if we have a live consensus that does not include it, or
  81. * if we have a live consensus that does not include it as a usable
  82. * guard. This field is zero when the guard is listed. */
  83. time_t unlisted_since_date; // can be zero
  84. /** What version of Tor added this guard to the sample? */
  85. char *sampled_by_version;
  86. /** Is this guard listed right now? If this is set, then
  87. * unlisted_since_date should be set too. */
  88. unsigned currently_listed : 1;
  89. /* == Persistent fields, for confirmed guards only */
  90. /** When was this guard confirmed? (That is, when did we first use it
  91. * successfully and decide to keep it?) This field is zero if this is not a
  92. * confirmed guard. */
  93. time_t confirmed_on_date; /* 0 if not confirmed */
  94. /**
  95. * In what order was this guard confirmed? Guards with lower indices
  96. * appear earlier on the confirmed list. If the confirmed list is compacted,
  97. * this field corresponds to the index of this guard on the confirmed list.
  98. *
  99. * This field is set to -1 if this guard is not confirmed.
  100. */
  101. int confirmed_idx; /* -1 if not confirmed; otherwise the order that this
  102. * item should occur in the CONFIRMED_GUARDS ordered
  103. * list */
  104. /* ==== Non-persistent fields. */
  105. /* == These are used by sampled guards */
  106. /** When did we last decide to try using this guard for a circuit? 0 for
  107. * "not since we started up." */
  108. time_t last_tried_to_connect;
  109. /** How reachable do we consider this guard to be? One of
  110. * GUARD_REACHABLE_NO, GUARD_REACHABLE_YES, or GUARD_REACHABLE_MAYBE. */
  111. unsigned is_reachable : 2;
  112. /** Boolean: true iff this guard is pending. A pending guard is one
  113. * that we have an in-progress circuit through, and which we do not plan
  114. * to try again until it either succeeds or fails. Primary guards can
  115. * never be pending. */
  116. unsigned is_pending : 1;
  117. /** When did we get the earliest connection failure for this guard?
  118. * We clear this field on a successful connect. We do _not_ clear it
  119. * when we mark the guard as "MAYBE" reachable.
  120. */
  121. time_t failing_since;
  122. /* == Set inclusion flags. */
  123. /** If true, this guard is in the filtered set. The filtered set includes
  124. * all sampled guards that our configuration allows us to use. */
  125. unsigned is_filtered_guard : 1;
  126. /** If true, this guard is in the usable filtered set. The usable filtered
  127. * set includes all filtered guards that are not believed to be
  128. * unreachable. (That is, those for which is_reachable is not
  129. * GUARD_REACHABLE_NO) */
  130. unsigned is_usable_filtered_guard : 1;
  131. unsigned is_primary:1;
  132. /** This string holds any fields that we are maintaining because
  133. * we saw them in the state, even if we don't understand them. */
  134. char *extra_state_fields;
  135. /**@}*/
  136. /**
  137. * @name legacy guard selection algorithm fields
  138. *
  139. * These are used and maintained by the legacy (pre-prop271) entry guard
  140. * algorithm. Most of them we will remove as prop271 gets implemented.
  141. * The rest we'll migrate over, if they are 100% semantically identical to
  142. * their prop271 equivalents. XXXXprop271
  143. */
  144. /**@{*/
  145. time_t chosen_on_date; /**< Approximately when was this guard added?
  146. * "0" if we don't know. */
  147. char *chosen_by_version; /**< What tor version added this guard? NULL
  148. * if we don't know. */
  149. unsigned int made_contact : 1; /**< 0 if we have never connected to this
  150. * router, 1 if we have. */
  151. unsigned int can_retry : 1; /**< Should we retry connecting to this entry,
  152. * in spite of having it marked as unreachable?*/
  153. unsigned int is_dir_cache : 1; /**< Is this node a directory cache? */
  154. time_t bad_since; /**< 0 if this guard is currently usable, or the time at
  155. * which it was observed to become (according to the
  156. * directory or the user configuration) unusable. */
  157. time_t unreachable_since; /**< 0 if we can connect to this guard, or the
  158. * time at which we first noticed we couldn't
  159. * connect to it. */
  160. time_t last_attempted; /**< 0 if we can connect to this guard, or the time
  161. * at which we last failed to connect to it. */
  162. /**}@*/
  163. /** Path bias information for this guard. */
  164. guard_pathbias_t pb;
  165. };
  166. /**
  167. * All of the the context for guard selection on a particular client.
  168. *
  169. * (XXXX prop271 this paragraph below is not actually implemented yet.)
  170. * We maintain multiple guard selection contexts for a client, depending
  171. * aspects on its current configuration -- whether an extremely
  172. * restrictive EntryNodes is used, whether UseBridges is enabled, and so
  173. * on.)
  174. *
  175. * See the module documentation for entrynodes.c for more information
  176. * about guard selection algorithms.
  177. */
  178. struct guard_selection_s {
  179. /**
  180. * A value of 1 means that guard_selection_t structures have changed
  181. * and those changes need to be flushed to disk.
  182. *
  183. * XXX prop271 we don't know how to flush multiple guard contexts to
  184. * disk yet; fix that as soon as any way to change the default exists,
  185. * or at least make sure this gets set on change.
  186. */
  187. int dirty;
  188. /**
  189. * A list of the sampled entry guards, as entry_guard_t structures.
  190. * Not in any particular order. When we 'sample' a guard, we are
  191. * noting it as a possible guard to pick in the future. The use of
  192. * sampling here prevents us from being forced by an attacker to try
  193. * every guard on the network. This list is persistent.
  194. */
  195. smartlist_t *sampled_entry_guards;
  196. /**
  197. * Ordered list (from highest to lowest priority) of guards that we
  198. * have successfully contacted and decided to use. Every member of
  199. * this list is a member of sampled_entry_guards. Every member should
  200. * have confirmed_on_date set, and have confirmed_idx greater than
  201. * any earlier member of the list.
  202. *
  203. * This list is persistent. It is a subset of the elements in
  204. * sampled_entry_guards, and its pointers point to elements of
  205. * sampled_entry_guards.
  206. */
  207. smartlist_t *confirmed_entry_guards;
  208. /**
  209. * Ordered list (from highest to lowest priority) of guards that we
  210. * are willing to use the most happily. These guards may or may not
  211. * yet be confirmed yet. If we can use one of these guards, we are
  212. * probably not on a network that is trying to restrict our guard
  213. * choices.
  214. *
  215. * This list is a subset of the elements in
  216. * sampled_entry_guards, and its pointers point to elements of
  217. * sampled_entry_guards.
  218. */
  219. smartlist_t *primary_entry_guards;
  220. /** When did we last successfully build a circuit or use a circuit? */
  221. time_t last_time_on_internet;
  222. /** What confirmed_idx value should the next-added member of
  223. * confirmed_entry_guards receive? */
  224. int next_confirmed_idx;
  225. /**
  226. * A list of our chosen entry guards, as entry_guard_t structures; this
  227. * preserves the pre-Prop271 behavior.
  228. */
  229. smartlist_t *chosen_entry_guards;
  230. /**
  231. * When we try to choose an entry guard, should we parse and add
  232. * config's EntryNodes first? This was formerly a global. This
  233. * preserves the pre-Prop271 behavior.
  234. */
  235. int should_add_entry_nodes;
  236. };
  237. #endif
  238. #if 1
  239. /* XXXX NM I would prefer that all of this stuff be private to
  240. * entrynodes.c. */
  241. entry_guard_t *entry_guard_get_by_id_digest_for_guard_selection(
  242. guard_selection_t *gs, const char *digest);
  243. entry_guard_t *entry_guard_get_by_id_digest(const char *digest);
  244. void entry_guards_changed_for_guard_selection(guard_selection_t *gs);
  245. void entry_guards_changed(void);
  246. guard_selection_t * get_guard_selection_info(void);
  247. const smartlist_t *get_entry_guards_for_guard_selection(
  248. guard_selection_t *gs);
  249. const smartlist_t *get_entry_guards(void);
  250. int num_live_entry_guards_for_guard_selection(
  251. guard_selection_t *gs,
  252. int for_directory);
  253. int num_live_entry_guards(int for_directory);
  254. #endif
  255. const node_t *entry_guard_find_node(const entry_guard_t *guard);
  256. void entry_guard_mark_bad(entry_guard_t *guard);
  257. const char *entry_guard_get_rsa_id_digest(const entry_guard_t *guard);
  258. const char *entry_guard_describe(const entry_guard_t *guard);
  259. guard_pathbias_t *entry_guard_get_pathbias_state(entry_guard_t *guard);
  260. /* Used by bridges.c only. */
  261. void add_bridge_as_entry_guard(guard_selection_t *gs,
  262. const node_t *chosen);
  263. int num_bridges_usable(void);
  264. #ifdef ENTRYNODES_PRIVATE
  265. /**
  266. * @name Parameters for the new (prop271) entry guard algorithm.
  267. */
  268. /* XXXX prop271 some of these should be networkstatus parameters */
  269. /**@{*/
  270. /**
  271. * We never let our sampled guard set grow larger than this fraction
  272. * of the guards on the network.
  273. */
  274. #define MAX_SAMPLE_THRESHOLD 0.30
  275. /**
  276. * We always try to make our sample contain at least this many guards.
  277. *
  278. * XXXX prop271 There was a MIN_SAMPLE_THRESHOLD in the proposal, but I
  279. * removed it in favor of MIN_FILTERED_SAMPLE_SIZE. -NM
  280. */
  281. #define MIN_FILTERED_SAMPLE_SIZE 20
  282. /**
  283. * If a guard is unlisted for this many days in a row, we remove it.
  284. */
  285. #define REMOVE_UNLISTED_GUARDS_AFTER_DAYS 20
  286. /**
  287. * We remove unconfirmed guards from the sample after this many days,
  288. * regardless of whether they are listed or unlisted.
  289. */
  290. #define GUARD_LIFETIME_DAYS 120
  291. /**
  292. * We remove confirmed guards from the sample if they were sampled
  293. * GUARD_LIFETIME_DAYS ago and confirmed this many days ago.
  294. */
  295. #define GUARD_CONFIRMED_MIN_LIFETIME_DAYS 60
  296. /**
  297. * How many guards do we try to keep on our primary guard list?
  298. */
  299. #define N_PRIMARY_GUARDS 3
  300. /**
  301. * If we haven't successfully built or used a circuit in this long, then
  302. * consider that the internet is probably down.
  303. */
  304. #define INTERNET_LIKELY_DOWN_INTERVAL (10*60)
  305. /**
  306. * DOCDOC. not yet used; see prop271.
  307. */
  308. #define NONPRIMARY_GUARD_CONNECT_TIMEOUT 15
  309. /**
  310. * DOCDOC. not yet used; see prop271.
  311. */
  312. #define NONPRIMARY_GUARD_IDLE_TIMEOUT (10*60)
  313. /**
  314. * DOCDOC. not yet used; see prop271.
  315. */
  316. #define MEANINGFUL_RESTRICTION_FRAC 0.2
  317. /**
  318. * DOCDOC. not yet used. see prop271.
  319. */
  320. #define EXTREME_RESTRICTION_FRAC 0.01
  321. /**@}*/
  322. // ---------- XXXX these functions and definitions are post-prop271.
  323. STATIC guard_selection_t *guard_selection_new(void);
  324. STATIC void guard_selection_free(guard_selection_t *gs);
  325. STATIC entry_guard_t *get_sampled_guard_with_id(guard_selection_t *gs,
  326. const uint8_t *rsa_id);
  327. MOCK_DECL(STATIC time_t, randomize_time, (time_t now, time_t max_backdate));
  328. STATIC entry_guard_t *entry_guard_add_to_sample(guard_selection_t *gs,
  329. const node_t *node);
  330. STATIC entry_guard_t *entry_guards_expand_sample(guard_selection_t *gs);
  331. STATIC char *entry_guard_encode_for_state(entry_guard_t *guard);
  332. STATIC entry_guard_t *entry_guard_parse_from_state(const char *s);
  333. STATIC void entry_guard_free(entry_guard_t *e);
  334. STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs);
  335. /**
  336. * @name Flags for sample_reachable_filtered_entry_guards()
  337. */
  338. /**@{*/
  339. #define SAMPLE_EXCLUDE_CONFIRMED (1u<<0)
  340. #define SAMPLE_EXCLUDE_PRIMARY (1u<<1)
  341. #define SAMPLE_EXCLUDE_PENDING (1u<<2)
  342. /**@}*/
  343. STATIC entry_guard_t *sample_reachable_filtered_entry_guards(
  344. guard_selection_t *gs,
  345. unsigned flags);
  346. STATIC void entry_guard_consider_retry(entry_guard_t *guard);
  347. STATIC void make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard);
  348. STATIC void entry_guards_update_confirmed(guard_selection_t *gs);
  349. STATIC void entry_guards_update_primary(guard_selection_t *gs);
  350. STATIC int num_reachable_filtered_guards(guard_selection_t *gs);
  351. STATIC void sampled_guards_update_from_consensus(guard_selection_t *gs);
  352. /**
  353. * @name Possible guard-states for a circuit.
  354. */
  355. /**@{*/
  356. /** State for a circuit that can (so far as the guard subsystem is
  357. * concerned) be used for actual traffic as soon as it is successfully
  358. * opened. */
  359. #define GUARD_CIRC_STATE_USABLE_ON_COMPLETION 1
  360. /** State for an non-open circuit that we shouldn't use for actual
  361. * traffic, when it completes, unless other circuits to preferable
  362. * guards fail. */
  363. #define GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD 2
  364. /** State for an open circuit that we shouldn't use for actual traffic
  365. * unless other circuits to preferable guards fail. */
  366. #define GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD 3
  367. /** State for a circuit that can (so far as the guard subsystem is
  368. * concerned) be used for actual traffic. */
  369. #define GUARD_CIRC_STATE_COMPLETE 4
  370. /**@}*/
  371. STATIC void entry_guards_note_guard_failure(guard_selection_t *gs,
  372. entry_guard_t *guard);
  373. STATIC entry_guard_t *select_entry_guard_for_circuit(guard_selection_t *gs,
  374. unsigned *state_out);
  375. STATIC void mark_primary_guards_maybe_reachable(guard_selection_t *gs);
  376. STATIC unsigned entry_guards_note_guard_success(guard_selection_t *gs,
  377. entry_guard_t *guard,
  378. unsigned old_state);
  379. STATIC int entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b);
  380. void entry_guards_DUMMY_ENTRY_POINT(void);
  381. // ---------- XXXX this stuff is pre-prop271.
  382. STATIC const node_t *add_an_entry_guard(guard_selection_t *gs,
  383. const node_t *chosen,
  384. int reset_status, int prepend,
  385. int for_discovery, int for_directory);
  386. STATIC int populate_live_entry_guards(smartlist_t *live_entry_guards,
  387. const smartlist_t *all_entry_guards,
  388. const node_t *chosen_exit,
  389. dirinfo_type_t dirinfo_type,
  390. int for_directory,
  391. int need_uptime, int need_capacity);
  392. STATIC int decide_num_guards(const or_options_t *options, int for_directory);
  393. STATIC void entry_guards_set_from_config(guard_selection_t *gs,
  394. const or_options_t *options);
  395. /** Flags to be passed to entry_is_live() to indicate what kind of
  396. * entry nodes we are looking for. */
  397. typedef enum {
  398. ENTRY_NEED_UPTIME = 1<<0,
  399. ENTRY_NEED_CAPACITY = 1<<1,
  400. ENTRY_ASSUME_REACHABLE = 1<<2,
  401. ENTRY_NEED_DESCRIPTOR = 1<<3,
  402. } entry_is_live_flags_t;
  403. STATIC const node_t *entry_is_live(const entry_guard_t *e,
  404. entry_is_live_flags_t flags,
  405. const char **msg);
  406. STATIC int entry_is_time_to_retry(const entry_guard_t *e, time_t now);
  407. #endif
  408. void remove_all_entry_guards_for_guard_selection(guard_selection_t *gs);
  409. void remove_all_entry_guards(void);
  410. void entry_guards_compute_status_for_guard_selection(
  411. guard_selection_t *gs, const or_options_t *options, time_t now);
  412. void entry_guards_compute_status(const or_options_t *options, time_t now);
  413. int entry_guard_register_connect_status_for_guard_selection(
  414. guard_selection_t *gs, const char *digest, int succeeded,
  415. int mark_relay_status, time_t now);
  416. int entry_guard_register_connect_status(const char *digest, int succeeded,
  417. int mark_relay_status, time_t now);
  418. void entry_nodes_should_be_added_for_guard_selection(guard_selection_t *gs);
  419. void entry_nodes_should_be_added(void);
  420. int entry_list_is_constrained(const or_options_t *options);
  421. const node_t *choose_random_entry(cpath_build_state_t *state);
  422. const node_t *choose_random_dirguard(dirinfo_type_t t);
  423. int entry_guards_parse_state_for_guard_selection(
  424. guard_selection_t *gs, or_state_t *state, int set, char **msg);
  425. int entry_guards_parse_state(or_state_t *state, int set, char **msg);
  426. void entry_guards_update_state(or_state_t *state);
  427. int getinfo_helper_entry_guards(control_connection_t *conn,
  428. const char *question, char **answer,
  429. const char **errmsg);
  430. int is_node_used_as_guard_for_guard_selection(guard_selection_t *gs,
  431. const node_t *node);
  432. MOCK_DECL(int, is_node_used_as_guard, (const node_t *node));
  433. int entries_known_but_down(const or_options_t *options);
  434. void entries_retry_all(const or_options_t *options);
  435. void entry_guards_free_all(void);
  436. double pathbias_get_close_success_count(entry_guard_t *guard);
  437. double pathbias_get_use_success_count(entry_guard_t *guard);
  438. /** Contains the bandwidth of a relay as a guard and as a non-guard
  439. * after the guardfraction has been considered. */
  440. typedef struct guardfraction_bandwidth_t {
  441. /** Bandwidth as a guard after guardfraction has been considered. */
  442. int guard_bw;
  443. /** Bandwidth as a non-guard after guardfraction has been considered. */
  444. int non_guard_bw;
  445. } guardfraction_bandwidth_t;
  446. int should_apply_guardfraction(const networkstatus_t *ns);
  447. void
  448. guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
  449. int orig_bandwidth,
  450. uint32_t guardfraction_percentage);
  451. #endif