Coverage Report

Created: 2025-08-24 06:20

/src/tor/src/feature/client/entrynodes.c
Line
Count
Source (jump to first uncovered line)
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 entrynodes.c
9
 * \brief Code to manage our fixed first nodes for various functions.
10
 *
11
 * Entry nodes can be guards (for general use) or bridges (for censorship
12
 * circumvention).
13
 *
14
 * In general, we use entry guards to prevent traffic-sampling attacks:
15
 * if we chose every circuit independently, an adversary controlling
16
 * some fraction of paths on the network would observe a sample of every
17
 * user's traffic. Using guards gives users a chance of not being
18
 * profiled.
19
 *
20
 * The current entry guard selection code is designed to try to avoid
21
 * _ever_ trying every guard on the network, to try to stick to guards
22
 * that we've used before, to handle hostile/broken networks, and
23
 * to behave sanely when the network goes up and down.
24
 *
25
 * Our algorithm works as follows: First, we maintain a SAMPLE of guards
26
 * we've seen in the networkstatus consensus.  We maintain this sample
27
 * over time, and store it persistently; it is chosen without reference
28
 * to our configuration or firewall rules.  Guards remain in the sample
29
 * as they enter and leave the consensus.  We expand this sample as
30
 * needed, up to a maximum size.
31
 *
32
 * As a subset of the sample, we maintain a FILTERED SET of the guards
33
 * that we would be willing to use if we could connect to them.  The
34
 * filter removes all the guards that we're excluding because they're
35
 * bridges (or not bridges), because we have restrictive firewall rules,
36
 * because of ExcludeNodes, because we of path bias restrictions,
37
 * because they're absent from the network at present, and so on.
38
 *
39
 * As a subset of the filtered set, we keep a REACHABLE FILTERED SET
40
 * (also called a "usable filtered set") of those guards that we call
41
 * "reachable" or "maybe reachable".  A guard is reachable if we've
42
 * connected to it more recently than we've failed.  A guard is "maybe
43
 * reachable" if we have never tried to connect to it, or if we
44
 * failed to connect to it so long ago that we no longer think our
45
 * failure means it's down.
46
 *
47
 * As a persistent ordered list whose elements are taken from the
48
 * sampled set, we track a CONFIRMED GUARDS LIST.  A guard becomes
49
 * confirmed when we successfully build a circuit through it, and decide
50
 * to use that circuit.
51
 *
52
 * And as a final group, we have an ordered list of PRIMARY GUARDS,
53
 * whose elements are taken from the filtered set. We prefer
54
 * confirmed guards to non-confirmed guards for this list, and place
55
 * other restrictions on it.  The primary guards are the ones that we
56
 * connect to "when nothing is wrong" -- circuits through them can be used
57
 * immediately.
58
 *
59
 * To build circuits, we take a primary guard if possible -- or a
60
 * reachable filtered confirmed guard if no primary guard is possible --
61
 * or the first (by sampled order) filtered guard otherwise.  If the guard is
62
 * primary, we can use the circuit immediately on success.  Otherwise,
63
 * the guard is now "pending" -- we won't use its circuit unless all
64
 * of the circuits we're trying to build through better guards have
65
 * definitely failed.
66
 *
67
 * While we're building circuits, we track a little "guard state" for
68
 * each circuit. We use this to keep track of whether the circuit is
69
 * one that we can use as soon as it's done, or whether it's one that
70
 * we should keep around to see if we can do better.  In the latter case,
71
 * a periodic call to entry_guards_upgrade_waiting_circuits() will
72
 * eventually upgrade it.
73
 **/
74
/* DOCDOC -- expand this.
75
 *
76
 * Information invariants:
77
 *
78
 * [x] whenever a guard becomes unreachable, clear its usable_filtered flag.
79
 *
80
 * [x] Whenever a guard becomes reachable or maybe-reachable, if its filtered
81
 * flag is set, set its usable_filtered flag.
82
 *
83
 * [x] Whenever we get a new consensus, call update_from_consensus(). (LATER.)
84
 *
85
 * [x] Whenever the configuration changes in a relevant way, update the
86
 * filtered/usable flags. (LATER.)
87
 *
88
 * [x] Whenever we add a guard to the sample, make sure its filtered/usable
89
 * flags are set as possible.
90
 *
91
 * [x] Whenever we remove a guard from the sample, remove it from the primary
92
 * and confirmed lists.
93
 *
94
 * [x] When we make a guard confirmed, update the primary list, and sort them
95
 * by sampled order.
96
 *
97
 * [x] When we make a guard filtered or unfiltered, update the primary list.
98
 *
99
 * [x] When we are about to pick a guard, make sure that the primary list is
100
 * full.
101
 *
102
 * [x] When we update the confirmed list, or when we re-build the primary list
103
 * and detect a change, we sort those lists by sampled_idx
104
 *
105
 * [x] Before calling first_reachable_filtered_entry_guard(), make sure
106
 * that the filtered, primary, and confirmed flags are up-to-date.
107
 *
108
 * [x] Call entry_guard_consider_retry every time we are about to check
109
 * is_usable_filtered or is_reachable, and every time we set
110
 * is_filtered to 1.
111
 *
112
 * [x] Call entry_guards_changed_for_guard_selection() whenever we update
113
 * a persistent field.
114
 */
115
116
#define ENTRYNODES_PRIVATE
117
118
#include "core/or/or.h"
119
#include "app/config/config.h"
120
#include "lib/confmgt/confmgt.h"
121
#include "app/config/statefile.h"
122
#include "core/mainloop/connection.h"
123
#include "core/mainloop/mainloop.h"
124
#include "core/or/channel.h"
125
#include "core/or/circuitbuild.h"
126
#include "core/or/circuitlist.h"
127
#include "core/or/circuitstats.h"
128
#include "core/or/circuituse.h"
129
#include "core/or/conflux_pool.h"
130
#include "core/or/policies.h"
131
#include "feature/client/bridges.h"
132
#include "feature/client/circpathbias.h"
133
#include "feature/client/entrynodes.h"
134
#include "feature/client/transports.h"
135
#include "feature/control/control_events.h"
136
#include "feature/dirclient/dlstatus.h"
137
#include "feature/dircommon/directory.h"
138
#include "feature/nodelist/describe.h"
139
#include "feature/nodelist/microdesc.h"
140
#include "feature/nodelist/networkstatus.h"
141
#include "feature/nodelist/nickname.h"
142
#include "feature/nodelist/nodelist.h"
143
#include "feature/nodelist/node_select.h"
144
#include "feature/nodelist/routerset.h"
145
#include "feature/relay/router.h"
146
#include "lib/crypt_ops/crypto_rand.h"
147
#include "lib/crypt_ops/digestset.h"
148
#include "lib/encoding/confline.h"
149
#include "lib/math/fp.h"
150
151
#include "feature/nodelist/node_st.h"
152
#include "core/or/origin_circuit_st.h"
153
#include "app/config/or_state_st.h"
154
#include "src/feature/nodelist/routerstatus_st.h"
155
156
#include "core/or/conflux_util.h"
157
158
/** A list of existing guard selection contexts. */
159
static smartlist_t *guard_contexts = NULL;
160
/** The currently enabled guard selection context. */
161
static guard_selection_t *curr_guard_context = NULL;
162
163
/** A value of 1 means that at least one context has changed,
164
 * and those changes need to be flushed to disk. */
165
static int entry_guards_dirty = 0;
166
167
static void entry_guard_set_filtered_flags(const or_options_t *options,
168
                                           guard_selection_t *gs,
169
                                           entry_guard_t *guard);
170
static void pathbias_check_use_success_count(entry_guard_t *guard);
171
static void pathbias_check_close_success_count(entry_guard_t *guard);
172
static int node_is_possible_guard(const node_t *node);
173
static int node_passes_guard_filter(const or_options_t *options,
174
                                    const node_t *node);
175
static entry_guard_t *entry_guard_add_to_sample_impl(guard_selection_t *gs,
176
                               const uint8_t *rsa_id_digest,
177
                               const char *nickname,
178
                               const tor_addr_port_t *bridge_addrport);
179
static entry_guard_t *get_sampled_guard_by_bridge_addr(guard_selection_t *gs,
180
                                              const tor_addr_port_t *addrport);
181
static int entry_guard_obeys_restriction(const entry_guard_t *guard,
182
                                         const entry_guard_restriction_t *rst);
183
static int compare_guards_by_sampled_idx(const void **a_, const void **b_);
184
185
/** Return 0 if we should apply guardfraction information found in the
186
 *  consensus. A specific consensus can be specified with the
187
 *  <b>ns</b> argument, if NULL the most recent one will be picked.*/
188
int
189
should_apply_guardfraction(const networkstatus_t *ns)
190
859
{
191
  /* We need to check the corresponding torrc option and the consensus
192
   * parameter if we need to. */
193
859
  const or_options_t *options = get_options();
194
195
  /* If UseGuardFraction is 'auto' then check the same-named consensus
196
   * parameter. If the consensus parameter is not present, default to
197
   * "off". */
198
859
  if (options->UseGuardFraction == -1) {
199
0
    return networkstatus_get_param(ns, "UseGuardFraction",
200
0
                                   0, /* default to "off" */
201
0
                                   0, 1);
202
0
  }
203
204
859
  return options->UseGuardFraction;
205
859
}
206
207
/** Return true iff we know a preferred descriptor for <b>guard</b> */
208
static int
209
guard_has_descriptor(const entry_guard_t *guard)
210
0
{
211
0
  const node_t *node = node_get_by_id(guard->identity);
212
0
  if (!node)
213
0
    return 0;
214
0
  return node_has_preferred_descriptor(node, 1);
215
0
}
216
217
/**
218
 * Try to determine the correct type for a selection named "name",
219
 * if <b>type</b> is GS_TYPE_INFER.
220
 */
221
STATIC guard_selection_type_t
222
guard_selection_infer_type(guard_selection_type_t type,
223
                           const char *name)
224
0
{
225
0
  if (type == GS_TYPE_INFER) {
226
0
    if (!strcmp(name, "bridges"))
227
0
      type = GS_TYPE_BRIDGE;
228
0
    else if (!strcmp(name, "restricted"))
229
0
      type = GS_TYPE_RESTRICTED;
230
0
    else
231
0
      type = GS_TYPE_NORMAL;
232
0
  }
233
0
  return type;
234
0
}
235
236
/**
237
 * Allocate and return a new guard_selection_t, with the name <b>name</b>.
238
 */
239
STATIC guard_selection_t *
240
guard_selection_new(const char *name,
241
                    guard_selection_type_t type)
242
0
{
243
0
  guard_selection_t *gs;
244
245
0
  type = guard_selection_infer_type(type, name);
246
247
0
  gs = tor_malloc_zero(sizeof(*gs));
248
0
  gs->name = tor_strdup(name);
249
0
  gs->type = type;
250
0
  gs->sampled_entry_guards = smartlist_new();
251
0
  gs->confirmed_entry_guards = smartlist_new();
252
0
  gs->primary_entry_guards = smartlist_new();
253
254
0
  return gs;
255
0
}
256
257
/**
258
 * Return the guard selection called <b>name</b>. If there is none, and
259
 * <b>create_if_absent</b> is true, then create and return it.  If there
260
 * is none, and <b>create_if_absent</b> is false, then return NULL.
261
 */
262
STATIC guard_selection_t *
263
get_guard_selection_by_name(const char *name,
264
                            guard_selection_type_t type,
265
                            int create_if_absent)
266
0
{
267
0
  if (!guard_contexts) {
268
0
    guard_contexts = smartlist_new();
269
0
  }
270
0
  SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
271
0
    if (!strcmp(gs->name, name))
272
0
      return gs;
273
0
  } SMARTLIST_FOREACH_END(gs);
274
275
0
  if (! create_if_absent)
276
0
    return NULL;
277
278
0
  log_debug(LD_GUARD, "Creating a guard selection called %s", name);
279
0
  guard_selection_t *new_selection = guard_selection_new(name, type);
280
0
  smartlist_add(guard_contexts, new_selection);
281
282
0
  return new_selection;
283
0
}
284
285
/**
286
 * Allocate the first guard context that we're planning to use,
287
 * and make it the current context.
288
 */
289
static void
290
create_initial_guard_context(void)
291
0
{
292
0
  tor_assert(! curr_guard_context);
293
0
  if (!guard_contexts) {
294
0
    guard_contexts = smartlist_new();
295
0
  }
296
0
  guard_selection_type_t type = GS_TYPE_INFER;
297
0
  const char *name = choose_guard_selection(
298
0
                             get_options(),
299
0
                             networkstatus_get_reasonably_live_consensus(
300
0
                                                    approx_time(),
301
0
                                                    usable_consensus_flavor()),
302
0
                             NULL,
303
0
                             &type);
304
0
  tor_assert(name); // "name" can only be NULL if we had an old name.
305
0
  tor_assert(type != GS_TYPE_INFER);
306
0
  log_notice(LD_GUARD, "Starting with guard context \"%s\"", name);
307
0
  curr_guard_context = get_guard_selection_by_name(name, type, 1);
308
0
}
309
310
/** Get current default guard_selection_t, creating it if necessary */
311
guard_selection_t *
312
get_guard_selection_info(void)
313
0
{
314
0
  if (!curr_guard_context) {
315
0
    create_initial_guard_context();
316
0
  }
317
318
0
  return curr_guard_context;
319
0
}
320
321
/** Return a statically allocated human-readable description of <b>guard</b>
322
 */
323
const char *
324
entry_guard_describe(const entry_guard_t *guard)
325
0
{
326
0
  static char buf[256];
327
0
  tor_snprintf(buf, sizeof(buf),
328
0
               "%s ($%s)",
329
0
               strlen(guard->nickname) ? guard->nickname : "[bridge]",
330
0
               hex_str(guard->identity, DIGEST_LEN));
331
0
  return buf;
332
0
}
333
334
/** Return <b>guard</b>'s 20-byte RSA identity digest */
335
const char *
336
entry_guard_get_rsa_id_digest(const entry_guard_t *guard)
337
0
{
338
0
  return guard->identity;
339
0
}
340
341
/** Return the pathbias state associated with <b>guard</b>. */
342
guard_pathbias_t *
343
entry_guard_get_pathbias_state(entry_guard_t *guard)
344
0
{
345
0
  return &guard->pb;
346
0
}
347
348
HANDLE_IMPL(entry_guard, entry_guard_t, ATTR_UNUSED STATIC)
349
350
/** Return an interval between 'now' and 'max_backdate' seconds in the past,
351
 * chosen uniformly at random.  We use this before recording persistent
352
 * dates, so that we aren't leaking exactly when we recorded it.
353
 */
354
MOCK_IMPL(STATIC time_t,
355
randomize_time,(time_t now, time_t max_backdate))
356
0
{
357
0
  tor_assert(max_backdate > 0);
358
359
0
  time_t earliest = now - max_backdate;
360
0
  time_t latest = now;
361
0
  if (earliest <= 0)
362
0
    earliest = 1;
363
0
  if (latest <= earliest)
364
0
    latest = earliest + 1;
365
366
0
  return crypto_rand_time_range(earliest, latest);
367
0
}
368
369
/**
370
 * @name parameters for networkstatus algorithm
371
 *
372
 * These parameters are taken from the consensus; some are overrideable in
373
 * the torrc.
374
 */
375
/**@{*/
376
/**
377
 * We never let our sampled guard set grow larger than this fraction
378
 * of the guards on the network.
379
 */
380
STATIC double
381
get_max_sample_threshold(void)
382
0
{
383
0
  int32_t pct =
384
0
    networkstatus_get_param(NULL, "guard-max-sample-threshold-percent",
385
0
                            DFLT_MAX_SAMPLE_THRESHOLD_PERCENT,
386
0
                            1, 100);
387
0
  return pct / 100.0;
388
0
}
389
/**
390
 * We never let our sampled guard set grow larger than this number.
391
 */
392
STATIC int
393
get_max_sample_size_absolute(void)
394
0
{
395
0
  return (int) networkstatus_get_param(NULL, "guard-max-sample-size",
396
0
                                       DFLT_MAX_SAMPLE_SIZE,
397
0
                                       1, INT32_MAX);
398
0
}
399
/**
400
 * We always try to make our sample contain at least this many guards.
401
 */
402
STATIC int
403
get_min_filtered_sample_size(void)
404
0
{
405
0
  return networkstatus_get_param(NULL, "guard-min-filtered-sample-size",
406
0
                                 DFLT_MIN_FILTERED_SAMPLE_SIZE,
407
0
                                 1, INT32_MAX);
408
0
}
409
/**
410
 * If a guard is unlisted for this many days in a row, we remove it.
411
 */
412
STATIC int
413
get_remove_unlisted_guards_after_days(void)
414
0
{
415
0
  return networkstatus_get_param(NULL,
416
0
                                 "guard-remove-unlisted-guards-after-days",
417
0
                                 DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS,
418
0
                                 1, 365*10);
419
0
}
420
421
/**
422
 * Return number of seconds that will make a guard no longer eligible
423
 * for selection if unlisted for this long.
424
 */
425
static time_t
426
get_remove_unlisted_guards_after_seconds(void)
427
0
{
428
0
  return get_remove_unlisted_guards_after_days() * 24 * 60 * 60;
429
0
}
430
431
/**
432
 * We remove unconfirmed guards from the sample after this many days,
433
 * regardless of whether they are listed or unlisted.
434
 */
435
STATIC int
436
get_guard_lifetime(void)
437
0
{
438
0
  if (get_options()->GuardLifetime >= 86400)
439
0
    return get_options()->GuardLifetime;
440
0
  int32_t days;
441
0
  days = networkstatus_get_param(NULL,
442
0
                                 "guard-lifetime-days",
443
0
                                 DFLT_GUARD_LIFETIME_DAYS, 1, 365*10);
444
0
  return days * 86400;
445
0
}
446
/**
447
 * We remove confirmed guards from the sample if they were sampled
448
 * GUARD_LIFETIME_DAYS ago and confirmed this many days ago.
449
 */
450
STATIC int
451
get_guard_confirmed_min_lifetime(void)
452
0
{
453
0
  if (get_options()->GuardLifetime >= 86400)
454
0
    return get_options()->GuardLifetime;
455
0
  int32_t days;
456
0
  days = networkstatus_get_param(NULL, "guard-confirmed-min-lifetime-days",
457
0
                                 DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS,
458
0
                                 1, 365*10);
459
0
  return days * 86400;
460
0
}
461
/**
462
 * How many guards do we try to keep on our primary guard list?
463
 */
464
STATIC int
465
get_n_primary_guards(void)
466
0
{
467
  /* If the user has explicitly configured the number of primary guards, do
468
   * what the user wishes to do */
469
0
  const int configured_primaries = get_options()->NumPrimaryGuards;
470
0
  if (configured_primaries) {
471
0
    return configured_primaries;
472
0
  }
473
474
  /* otherwise check for consensus parameter and if that's not set either, just
475
   * use the default value. */
476
0
  return networkstatus_get_param(NULL,
477
0
                                 "guard-n-primary-guards",
478
0
                                 DFLT_N_PRIMARY_GUARDS, 1, INT32_MAX);
479
0
}
480
/**
481
 * Return the number of the live primary guards we should look at when
482
 * making a circuit.
483
 */
484
STATIC int
485
get_n_primary_guards_to_use(guard_usage_t usage)
486
0
{
487
0
  int configured;
488
0
  const char *param_name;
489
0
  int param_default;
490
491
  /* If the user has explicitly configured the amount of guards, use
492
     that. Otherwise, fall back to the default value. */
493
0
  if (usage == GUARD_USAGE_DIRGUARD) {
494
0
    configured = get_options()->NumDirectoryGuards;
495
0
    param_name = "guard-n-primary-dir-guards-to-use";
496
0
    param_default = DFLT_N_PRIMARY_DIR_GUARDS_TO_USE;
497
0
  } else {
498
0
    configured = get_options()->NumEntryGuards;
499
0
    param_name = "guard-n-primary-guards-to-use";
500
0
    param_default = DFLT_N_PRIMARY_GUARDS_TO_USE;
501
0
  }
502
0
  if (configured >= 1) {
503
0
    return configured;
504
0
  }
505
0
  return networkstatus_get_param(NULL,
506
0
                                 param_name, param_default, 1, INT32_MAX);
507
0
}
508
/**
509
 * If we haven't successfully built or used a circuit in this long, then
510
 * consider that the internet is probably down.
511
 */
512
STATIC int
513
get_internet_likely_down_interval(void)
514
0
{
515
0
  return networkstatus_get_param(NULL, "guard-internet-likely-down-interval",
516
0
                                 DFLT_INTERNET_LIKELY_DOWN_INTERVAL,
517
0
                                 1, INT32_MAX);
518
0
}
519
/**
520
 * If we're trying to connect to a nonprimary guard for at least this
521
 * many seconds, and we haven't gotten the connection to work, we will treat
522
 * lower-priority guards as usable.
523
 */
524
STATIC int
525
get_nonprimary_guard_connect_timeout(void)
526
0
{
527
0
  return networkstatus_get_param(NULL,
528
0
                                 "guard-nonprimary-guard-connect-timeout",
529
0
                                 DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT,
530
0
                                 1, INT32_MAX);
531
0
}
532
/**
533
 * If a circuit has been sitting around in 'waiting for better guard' state
534
 * for at least this long, we'll expire it.
535
 */
536
STATIC int
537
get_nonprimary_guard_idle_timeout(void)
538
0
{
539
0
  return networkstatus_get_param(NULL,
540
0
                                 "guard-nonprimary-guard-idle-timeout",
541
0
                                 DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT,
542
0
                                 1, INT32_MAX);
543
0
}
544
/**
545
 * If our configuration retains fewer than this fraction of guards from the
546
 * torrc, we are in a restricted setting.
547
 */
548
STATIC double
549
get_meaningful_restriction_threshold(void)
550
0
{
551
0
  int32_t pct = networkstatus_get_param(NULL,
552
0
                                        "guard-meaningful-restriction-percent",
553
0
                                        DFLT_MEANINGFUL_RESTRICTION_PERCENT,
554
0
                                        1, INT32_MAX);
555
0
  return pct / 100.0;
556
0
}
557
/**
558
 * If our configuration retains fewer than this fraction of guards from the
559
 * torrc, we are in an extremely restricted setting, and should warn.
560
 */
561
STATIC double
562
get_extreme_restriction_threshold(void)
563
0
{
564
0
  int32_t pct = networkstatus_get_param(NULL,
565
0
                                        "guard-extreme-restriction-percent",
566
0
                                        DFLT_EXTREME_RESTRICTION_PERCENT,
567
0
                                        1, 100);
568
0
  return pct / 100.0;
569
0
}
570
571
/* Mark <b>guard</b> as maybe reachable again. */
572
static void
573
mark_guard_maybe_reachable(entry_guard_t *guard)
574
0
{
575
0
  if (guard->is_reachable != GUARD_REACHABLE_NO) {
576
0
    return;
577
0
  }
578
579
  /* Note that we do not clear failing_since: this guard is now only
580
   * _maybe-reachable_. */
581
0
  guard->is_reachable = GUARD_REACHABLE_MAYBE;
582
0
  if (guard->is_filtered_guard)
583
0
    guard->is_usable_filtered_guard = 1;
584
585
  /* Check if it is a bridge and we don't have its descriptor yet */
586
0
  if (guard->bridge_addr && !guard_has_descriptor(guard)) {
587
    /* Reset the descriptor fetch retry schedule, so it gives it another
588
     * go soon. It's important to keep any "REACHABLE_MAYBE" bridges in
589
     * sync with the descriptor fetch schedule, since we will refuse to
590
     * use the network until our first primary bridges are either
591
     * known-usable or known-unusable. See bug 40396. */
592
0
    download_status_t *dl = get_bridge_dl_status_by_id(guard->identity);
593
0
    if (dl)
594
0
      download_status_reset(dl);
595
0
  }
596
0
}
597
598
/**
599
 * Called when the network comes up after having seemed to be down for
600
 * a while: Mark the primary guards as maybe-reachable so that we'll
601
 * try them again.
602
 */
603
STATIC void
604
mark_primary_guards_maybe_reachable(guard_selection_t *gs)
605
0
{
606
0
  tor_assert(gs);
607
608
0
  if (!gs->primary_guards_up_to_date)
609
0
    entry_guards_update_primary(gs);
610
611
0
  SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
612
0
    mark_guard_maybe_reachable(guard);
613
0
  } SMARTLIST_FOREACH_END(guard);
614
0
}
615
616
/* Called when we exhaust all guards in our sampled set: Marks all guards as
617
   maybe-reachable so that we'll try them again. */
618
static void
619
mark_all_guards_maybe_reachable(guard_selection_t *gs)
620
0
{
621
0
  tor_assert(gs);
622
623
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
624
0
    mark_guard_maybe_reachable(guard);
625
0
  } SMARTLIST_FOREACH_END(guard);
626
0
}
627
628
/**@}*/
629
630
/**
631
 * Given our options and our list of nodes, return the name of the
632
 * guard selection that we should use.  Return NULL for "use the
633
 * same selection you were using before.
634
 */
635
STATIC const char *
636
choose_guard_selection(const or_options_t *options,
637
                       const networkstatus_t *live_ns,
638
                       const guard_selection_t *old_selection,
639
                       guard_selection_type_t *type_out)
640
0
{
641
0
  tor_assert(options);
642
0
  tor_assert(type_out);
643
644
0
  if (options->UseBridges) {
645
0
    *type_out = GS_TYPE_BRIDGE;
646
0
    return "bridges";
647
0
  }
648
649
0
  if (! live_ns) {
650
    /* without a networkstatus, we can't tell any more than that. */
651
0
    *type_out = GS_TYPE_NORMAL;
652
0
    return "default";
653
0
  }
654
655
0
  const smartlist_t *nodes = nodelist_get_list();
656
0
  int n_guards = 0, n_passing_filter = 0;
657
0
  SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
658
0
    if (node_is_possible_guard(node)) {
659
0
      ++n_guards;
660
0
      if (node_passes_guard_filter(options, node)) {
661
0
        ++n_passing_filter;
662
0
      }
663
0
    }
664
0
  } SMARTLIST_FOREACH_END(node);
665
666
  /* We use separate 'high' and 'low' thresholds here to prevent flapping
667
   * back and forth */
668
0
  const int meaningful_threshold_high =
669
0
    (int)(n_guards * get_meaningful_restriction_threshold() * 1.05);
670
0
  const int meaningful_threshold_mid =
671
0
    (int)(n_guards * get_meaningful_restriction_threshold());
672
0
  const int meaningful_threshold_low =
673
0
    (int)(n_guards * get_meaningful_restriction_threshold() * .95);
674
0
  const int extreme_threshold =
675
0
    (int)(n_guards * get_extreme_restriction_threshold());
676
677
  /*
678
    If we have no previous selection, then we're "restricted" iff we are
679
    below the meaningful restriction threshold.  That's easy enough.
680
681
    But if we _do_ have a previous selection, we make it a little
682
    "sticky": we only move from "restricted" to "default" when we find
683
    that we're above the threshold plus 5%, and we only move from
684
    "default" to "restricted" when we're below the threshold minus 5%.
685
    That should prevent us from flapping back and forth if we happen to
686
    be hovering very close to the default.
687
688
    The extreme threshold is for warning only.
689
  */
690
691
0
  static int have_warned_extreme_threshold = 0;
692
0
  if (n_guards &&
693
0
      n_passing_filter < extreme_threshold &&
694
0
      ! have_warned_extreme_threshold) {
695
0
    have_warned_extreme_threshold = 1;
696
0
    const double exclude_frac =
697
0
      (n_guards - n_passing_filter) / (double)n_guards;
698
0
    log_warn(LD_GUARD, "Your configuration excludes %d%% of all possible "
699
0
             "guards. That's likely to make you stand out from the "
700
0
             "rest of the world.", (int)(exclude_frac * 100));
701
0
  }
702
703
  /* Easy case: no previous selection. Just check if we are in restricted or
704
     normal guard selection. */
705
0
  if (old_selection == NULL) {
706
0
    if (n_passing_filter >= meaningful_threshold_mid) {
707
0
      *type_out = GS_TYPE_NORMAL;
708
0
      return "default";
709
0
    } else {
710
0
      *type_out = GS_TYPE_RESTRICTED;
711
0
      return "restricted";
712
0
    }
713
0
  }
714
715
  /* Trickier case: we do have a previous guard selection context. */
716
0
  tor_assert(old_selection);
717
718
  /* Use high and low thresholds to decide guard selection, and if we fall in
719
     the middle then keep the current guard selection context. */
720
0
  if (n_passing_filter >= meaningful_threshold_high) {
721
0
    *type_out = GS_TYPE_NORMAL;
722
0
    return "default";
723
0
  } else if (n_passing_filter < meaningful_threshold_low) {
724
0
    *type_out = GS_TYPE_RESTRICTED;
725
0
    return "restricted";
726
0
  } else {
727
    /* we are in the middle: maintain previous guard selection */
728
0
    *type_out = old_selection->type;
729
0
    return old_selection->name;
730
0
  }
731
0
}
732
733
/**
734
 * Check whether we should switch from our current guard selection to a
735
 * different one.  If so, switch and return 1.  Return 0 otherwise.
736
 *
737
 * On a 1 return, the caller should mark all currently live circuits unusable
738
 * for new streams, by calling circuit_mark_all_unused_circs() and
739
 * circuit_mark_all_dirty_circs_as_unusable().
740
 */
741
int
742
update_guard_selection_choice(const or_options_t *options)
743
0
{
744
0
  if (!curr_guard_context) {
745
0
    create_initial_guard_context();
746
0
    return 1;
747
0
  }
748
749
0
  guard_selection_type_t type = GS_TYPE_INFER;
750
0
  const char *new_name = choose_guard_selection(
751
0
                             options,
752
0
                             networkstatus_get_reasonably_live_consensus(
753
0
                                                    approx_time(),
754
0
                                                    usable_consensus_flavor()),
755
0
                             curr_guard_context,
756
0
                             &type);
757
0
  tor_assert(new_name);
758
0
  tor_assert(type != GS_TYPE_INFER);
759
760
0
  const char *cur_name = curr_guard_context->name;
761
0
  if (! strcmp(cur_name, new_name)) {
762
0
    log_debug(LD_GUARD,
763
0
              "Staying with guard context \"%s\" (no change)", new_name);
764
0
    return 0; // No change
765
0
  }
766
767
0
  log_notice(LD_GUARD, "Switching to guard context \"%s\" (was using \"%s\")",
768
0
             new_name, cur_name);
769
0
  guard_selection_t *new_guard_context;
770
0
  new_guard_context = get_guard_selection_by_name(new_name, type, 1);
771
0
  tor_assert(new_guard_context);
772
0
  tor_assert(new_guard_context != curr_guard_context);
773
0
  curr_guard_context = new_guard_context;
774
775
0
  return 1;
776
0
}
777
778
/**
779
 * Return true iff <b>node</b> has all the flags needed for us to consider it
780
 * a possible guard when sampling guards.
781
 */
782
static int
783
node_is_possible_guard(const node_t *node)
784
0
{
785
  /* The "GUARDS" set is all nodes in the nodelist for which this predicate
786
   * holds. */
787
788
0
  tor_assert(node);
789
0
  return (node->is_possible_guard &&
790
0
          node->is_stable &&
791
0
          node->is_fast &&
792
0
          node->is_valid &&
793
0
          node_is_dir(node) &&
794
0
          !router_digest_is_me(node->identity));
795
0
}
796
797
/**
798
 * Return the sampled guard with the RSA identity digest <b>rsa_id</b>, or
799
 * NULL if we don't have one. */
800
STATIC entry_guard_t *
801
get_sampled_guard_with_id(guard_selection_t *gs,
802
                          const uint8_t *rsa_id)
803
0
{
804
0
  tor_assert(gs);
805
0
  tor_assert(rsa_id);
806
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
807
0
    if (tor_memeq(guard->identity, rsa_id, DIGEST_LEN))
808
0
      return guard;
809
0
  } SMARTLIST_FOREACH_END(guard);
810
0
  return NULL;
811
0
}
812
813
/** If <b>gs</b> contains a sampled entry guard matching <b>bridge</b>,
814
 * return that guard. Otherwise return NULL. */
815
static entry_guard_t *
816
get_sampled_guard_for_bridge(guard_selection_t *gs,
817
                             const bridge_info_t *bridge)
818
0
{
819
0
  const uint8_t *id = bridge_get_rsa_id_digest(bridge);
820
0
  const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
821
0
  entry_guard_t *guard;
822
0
  if (BUG(!addrport))
823
0
    return NULL; // LCOV_EXCL_LINE
824
0
  guard = get_sampled_guard_by_bridge_addr(gs, addrport);
825
0
  if (! guard || (id && tor_memneq(id, guard->identity, DIGEST_LEN)))
826
0
    return NULL;
827
0
  else
828
0
    return guard;
829
0
}
830
831
/** If we know a bridge_info_t matching <b>guard</b>, return that
832
 * bridge.  Otherwise return NULL. */
833
static bridge_info_t *
834
get_bridge_info_for_guard(const entry_guard_t *guard)
835
0
{
836
0
  const uint8_t *identity = NULL;
837
0
  if (! tor_digest_is_zero(guard->identity)) {
838
0
    identity = (const uint8_t *)guard->identity;
839
0
  }
840
0
  if (BUG(guard->bridge_addr == NULL))
841
0
    return NULL;
842
843
0
  return get_configured_bridge_by_exact_addr_port_digest(
844
0
                                                 &guard->bridge_addr->addr,
845
0
                                                 guard->bridge_addr->port,
846
0
                                                 (const char*)identity);
847
0
}
848
849
/**
850
 * Return true iff we have a sampled guard with the RSA identity digest
851
 * <b>rsa_id</b>. */
852
static inline int
853
have_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
854
0
{
855
0
  return get_sampled_guard_with_id(gs, rsa_id) != NULL;
856
0
}
857
858
/**
859
 * Allocate a new entry_guard_t object for <b>node</b>, add it to the
860
 * sampled entry guards in <b>gs</b>, and return it. <b>node</b> must
861
 * not currently be a sampled guard in <b>gs</b>.
862
 */
863
STATIC entry_guard_t *
864
entry_guard_add_to_sample(guard_selection_t *gs,
865
                          const node_t *node)
866
0
{
867
0
  log_info(LD_GUARD, "Adding %s to the entry guard sample set.",
868
0
           node_describe(node));
869
870
  /* make sure that the guard is not already sampled. */
871
0
  if (BUG(have_sampled_guard_with_id(gs, (const uint8_t*)node->identity)))
872
0
    return NULL; // LCOV_EXCL_LINE
873
874
0
  return entry_guard_add_to_sample_impl(gs,
875
0
                                        (const uint8_t*)node->identity,
876
0
                                        node_get_nickname(node),
877
0
                                        NULL);
878
0
}
879
880
/**
881
 * Backend: adds a new sampled guard to <b>gs</b>, with given identity,
882
 * nickname, and ORPort.  rsa_id_digest and bridge_addrport are optional, but
883
 * we need one of them. nickname is optional. The caller is responsible for
884
 * maintaining the size limit of the SAMPLED_GUARDS set.
885
 */
886
static entry_guard_t *
887
entry_guard_add_to_sample_impl(guard_selection_t *gs,
888
                               const uint8_t *rsa_id_digest,
889
                               const char *nickname,
890
                               const tor_addr_port_t *bridge_addrport)
891
0
{
892
0
  const int GUARD_LIFETIME = get_guard_lifetime();
893
0
  tor_assert(gs);
894
895
  // XXXX #20827 take ed25519 identity here too.
896
897
  /* Make sure we can actually identify the guard. */
898
0
  if (BUG(!rsa_id_digest && !bridge_addrport))
899
0
    return NULL; // LCOV_EXCL_LINE
900
901
0
  entry_guard_t *guard = tor_malloc_zero(sizeof(entry_guard_t));
902
903
  /* persistent fields */
904
0
  guard->is_persistent = (rsa_id_digest != NULL);
905
0
  guard->selection_name = tor_strdup(gs->name);
906
0
  if (rsa_id_digest)
907
0
    memcpy(guard->identity, rsa_id_digest, DIGEST_LEN);
908
0
  if (nickname)
909
0
    strlcpy(guard->nickname, nickname, sizeof(guard->nickname));
910
0
  guard->sampled_on_date = randomize_time(approx_time(), GUARD_LIFETIME/10);
911
0
  tor_free(guard->sampled_by_version);
912
0
  guard->sampled_by_version = tor_strdup(VERSION);
913
0
  guard->currently_listed = 1;
914
0
  guard->sampled_idx = gs->next_sampled_idx++;
915
0
  guard->confirmed_idx = -1;
916
917
  /* non-persistent fields */
918
0
  guard->is_reachable = GUARD_REACHABLE_MAYBE;
919
0
  if (bridge_addrport)
920
0
    guard->bridge_addr = tor_memdup(bridge_addrport, sizeof(*bridge_addrport));
921
922
0
  smartlist_add(gs->sampled_entry_guards, guard);
923
0
  guard->in_selection = gs;
924
0
  entry_guard_set_filtered_flags(get_options(), gs, guard);
925
0
  entry_guards_changed_for_guard_selection(gs);
926
927
  /* Just added this guard to the sampled set and hence it might be used as a
928
   * guard in the future: send GUARD NEW control event. */
929
0
  control_event_guard(guard->nickname, guard->identity, "NEW");
930
931
0
  return guard;
932
0
}
933
934
/**
935
 * Add an entry guard to the "bridges" guard selection sample, with
936
 * information taken from <b>bridge</b>. Return that entry guard.
937
 */
938
static entry_guard_t *
939
entry_guard_add_bridge_to_sample(guard_selection_t *gs,
940
                                 const bridge_info_t *bridge)
941
0
{
942
0
  const uint8_t *id_digest = bridge_get_rsa_id_digest(bridge);
943
0
  const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
944
945
0
  tor_assert(addrport);
946
947
  /* make sure that the guard is not already sampled. */
948
0
  if (BUG(get_sampled_guard_for_bridge(gs, bridge)))
949
0
    return NULL; // LCOV_EXCL_LINE
950
951
0
  return entry_guard_add_to_sample_impl(gs, id_digest, NULL, addrport);
952
0
}
953
954
/**
955
 * Return the entry_guard_t in <b>gs</b> whose address is <b>addrport</b>,
956
 * or NULL if none exists.
957
*/
958
static entry_guard_t *
959
get_sampled_guard_by_bridge_addr(guard_selection_t *gs,
960
                                 const tor_addr_port_t *addrport)
961
0
{
962
0
  if (! gs)
963
0
    return NULL;
964
0
  if (BUG(!addrport))
965
0
    return NULL;
966
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
967
0
    if (g->bridge_addr && tor_addr_port_eq(addrport, g->bridge_addr))
968
0
      return g;
969
0
  } SMARTLIST_FOREACH_END(g);
970
0
  return NULL;
971
0
}
972
973
/** Update the guard subsystem's knowledge of the identity of the bridge
974
 * at <b>addrport</b>.  Idempotent.
975
 */
976
void
977
entry_guard_learned_bridge_identity(const tor_addr_port_t *addrport,
978
                                    const uint8_t *rsa_id_digest)
979
0
{
980
0
  guard_selection_t *gs = get_guard_selection_by_name("bridges",
981
0
                                                      GS_TYPE_BRIDGE,
982
0
                                                      0);
983
0
  if (!gs)
984
0
    return;
985
986
0
  entry_guard_t *g = get_sampled_guard_by_bridge_addr(gs, addrport);
987
0
  if (!g)
988
0
    return;
989
990
0
  int make_persistent = 0;
991
992
0
  if (tor_digest_is_zero(g->identity)) {
993
0
    memcpy(g->identity, rsa_id_digest, DIGEST_LEN);
994
0
    make_persistent = 1;
995
0
  } else if (tor_memeq(g->identity, rsa_id_digest, DIGEST_LEN)) {
996
    /* Nothing to see here; we learned something we already knew. */
997
0
    if (BUG(! g->is_persistent))
998
0
      make_persistent = 1;
999
0
  } else {
1000
0
    char old_id[HEX_DIGEST_LEN+1];
1001
0
    base16_encode(old_id, sizeof(old_id), g->identity, sizeof(g->identity));
1002
0
    log_warn(LD_BUG, "We 'learned' an identity %s for a bridge at %s:%d, but "
1003
0
             "we already knew a different one (%s). Ignoring the new info as "
1004
0
             "possibly bogus.",
1005
0
             hex_str((const char *)rsa_id_digest, DIGEST_LEN),
1006
0
             fmt_and_decorate_addr(&addrport->addr), addrport->port,
1007
0
             old_id);
1008
0
    return; // redundant, but let's be clear: we're not making this persistent.
1009
0
  }
1010
1011
0
  if (make_persistent) {
1012
0
    g->is_persistent = 1;
1013
0
    entry_guards_changed_for_guard_selection(gs);
1014
0
  }
1015
0
}
1016
1017
/**
1018
 * Return the number of sampled guards in <b>gs</b> that are "filtered"
1019
 * (that is, we're willing to connect to them) and that are "usable"
1020
 * (that is, either "reachable" or "maybe reachable").
1021
 *
1022
 * If a restriction is provided in <b>rst</b>, do not count any guards that
1023
 * violate it.
1024
 */
1025
STATIC int
1026
num_reachable_filtered_guards(const guard_selection_t *gs,
1027
                              const entry_guard_restriction_t *rst)
1028
0
{
1029
0
  int n_reachable_filtered_guards = 0;
1030
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1031
0
    entry_guard_consider_retry(guard);
1032
0
    if (! entry_guard_obeys_restriction(guard, rst))
1033
0
      continue;
1034
0
    if (guard->is_usable_filtered_guard)
1035
0
      ++n_reachable_filtered_guards;
1036
0
  } SMARTLIST_FOREACH_END(guard);
1037
0
  return n_reachable_filtered_guards;
1038
0
}
1039
1040
/** Return the actual maximum size for the sample in <b>gs</b>,
1041
 * given that we know about <b>n_guards</b> total. */
1042
static int
1043
get_max_sample_size(guard_selection_t *gs,
1044
                    int n_guards)
1045
0
{
1046
0
  const int using_bridges = (gs->type == GS_TYPE_BRIDGE);
1047
0
  const int min_sample = get_min_filtered_sample_size();
1048
1049
  /* If we are in bridge mode, expand our sample set as needed without worrying
1050
   * about max size. We should respect the user's wishes to use many bridges if
1051
   * that's what they have specified in their configuration file. */
1052
0
  if (using_bridges)
1053
0
    return INT_MAX;
1054
1055
0
  const int max_sample_by_pct = (int)(n_guards * get_max_sample_threshold());
1056
0
  const int max_sample_absolute = get_max_sample_size_absolute();
1057
0
  const int max_sample = MIN(max_sample_by_pct, max_sample_absolute);
1058
0
  if (max_sample < min_sample)
1059
0
    return min_sample;
1060
0
  else
1061
0
    return max_sample;
1062
0
}
1063
1064
/**
1065
 * Return a smartlist of all the guards that are not currently
1066
 * members of the sample (GUARDS - SAMPLED_GUARDS).  The elements of
1067
 * this list are node_t pointers in the non-bridge case, and
1068
 * bridge_info_t pointers in the bridge case.  Set *<b>n_guards_out</b>
1069
 * to the number of guards that we found in GUARDS, including those
1070
 * that were already sampled.
1071
 */
1072
static smartlist_t *
1073
get_eligible_guards(const or_options_t *options,
1074
                    guard_selection_t *gs,
1075
                    int *n_guards_out)
1076
0
{
1077
  /* Construct eligible_guards as GUARDS - SAMPLED_GUARDS */
1078
0
  smartlist_t *eligible_guards = smartlist_new();
1079
0
  int n_guards = 0; // total size of "GUARDS"
1080
1081
0
  if (gs->type == GS_TYPE_BRIDGE) {
1082
0
    const smartlist_t *bridges = bridge_list_get();
1083
0
    SMARTLIST_FOREACH_BEGIN(bridges, bridge_info_t *, bridge) {
1084
0
      ++n_guards;
1085
0
      if (NULL != get_sampled_guard_for_bridge(gs, bridge)) {
1086
0
        continue;
1087
0
      }
1088
0
      smartlist_add(eligible_guards, bridge);
1089
0
    } SMARTLIST_FOREACH_END(bridge);
1090
0
  } else {
1091
0
    const smartlist_t *nodes = nodelist_get_list();
1092
0
    const int n_sampled = smartlist_len(gs->sampled_entry_guards);
1093
1094
    /* Build a bloom filter of our current guards: let's keep this O(N). */
1095
0
    digestset_t *sampled_guard_ids = digestset_new(n_sampled);
1096
0
    SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, const entry_guard_t *,
1097
0
                            guard) {
1098
0
      digestset_add(sampled_guard_ids, guard->identity);
1099
0
    } SMARTLIST_FOREACH_END(guard);
1100
1101
0
    SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
1102
0
      if (! node_is_possible_guard(node))
1103
0
        continue;
1104
0
      if (gs->type == GS_TYPE_RESTRICTED) {
1105
        /* In restricted mode, we apply the filter BEFORE sampling, so
1106
         * that we are sampling from the nodes that we might actually
1107
         * select. If we sampled first, we might wind up with a sample
1108
         * that didn't include any EntryNodes at all. */
1109
0
        if (! node_passes_guard_filter(options, node))
1110
0
          continue;
1111
0
      }
1112
0
      ++n_guards;
1113
0
      if (digestset_probably_contains(sampled_guard_ids, node->identity))
1114
0
        continue;
1115
0
      smartlist_add(eligible_guards, (node_t*)node);
1116
0
    } SMARTLIST_FOREACH_END(node);
1117
1118
    /* Now we can free that bloom filter. */
1119
0
    digestset_free(sampled_guard_ids);
1120
0
  }
1121
1122
0
  *n_guards_out = n_guards;
1123
0
  return eligible_guards;
1124
0
}
1125
1126
/** Helper: given a smartlist of either bridge_info_t (if gs->type is
1127
 * GS_TYPE_BRIDGE) or node_t (otherwise), pick one that can be a guard,
1128
 * add it as a guard, remove it from the list, and return a new
1129
 * entry_guard_t.  Return NULL on failure. */
1130
static entry_guard_t *
1131
select_and_add_guard_item_for_sample(guard_selection_t *gs,
1132
                                     smartlist_t *eligible_guards)
1133
0
{
1134
0
  entry_guard_t *added_guard;
1135
0
  if (gs->type == GS_TYPE_BRIDGE) {
1136
0
    const bridge_info_t *bridge = smartlist_choose(eligible_guards);
1137
0
    if (BUG(!bridge))
1138
0
      return NULL; // LCOV_EXCL_LINE
1139
0
    smartlist_remove(eligible_guards, bridge);
1140
0
    added_guard = entry_guard_add_bridge_to_sample(gs, bridge);
1141
0
  } else {
1142
0
    const node_t *node =
1143
0
      node_sl_choose_by_bandwidth(eligible_guards, WEIGHT_FOR_GUARD);
1144
0
    if (BUG(!node))
1145
0
      return NULL; // LCOV_EXCL_LINE
1146
0
    smartlist_remove(eligible_guards, node);
1147
0
    added_guard = entry_guard_add_to_sample(gs, node);
1148
0
  }
1149
1150
0
  return added_guard;
1151
0
}
1152
1153
/**
1154
 * Return true iff we need a consensus to update our guards, but we don't
1155
 * have one. (We can return 0 here either if the consensus is _not_ missing,
1156
 * or if we don't need a consensus because we're using bridges.)
1157
 */
1158
static int
1159
reasonably_live_consensus_is_missing(const guard_selection_t *gs)
1160
0
{
1161
0
  tor_assert(gs);
1162
0
  if (gs->type == GS_TYPE_BRIDGE) {
1163
    /* We don't update bridges from the consensus; they aren't there. */
1164
0
    return 0;
1165
0
  }
1166
0
  return networkstatus_get_reasonably_live_consensus(
1167
0
                                            approx_time(),
1168
0
                                            usable_consensus_flavor()) == NULL;
1169
0
}
1170
1171
/**
1172
 * Add new guards to the sampled guards in <b>gs</b> until there are
1173
 * enough usable filtered guards, but never grow the sample beyond its
1174
 * maximum size.  Return the last guard added, or NULL if none were
1175
 * added.
1176
 */
1177
STATIC entry_guard_t *
1178
entry_guards_expand_sample(guard_selection_t *gs)
1179
0
{
1180
0
  tor_assert(gs);
1181
0
  const or_options_t *options = get_options();
1182
1183
0
  if (reasonably_live_consensus_is_missing(gs)) {
1184
0
    log_info(LD_GUARD, "Not expanding the sample guard set; we have "
1185
0
             "no reasonably live consensus.");
1186
0
    return NULL;
1187
0
  }
1188
1189
0
  int n_sampled = smartlist_len(gs->sampled_entry_guards);
1190
0
  entry_guard_t *added_guard = NULL;
1191
0
  int n_usable_filtered_guards = num_reachable_filtered_guards(gs, NULL);
1192
0
  int n_guards = 0;
1193
0
  smartlist_t *eligible_guards = get_eligible_guards(options, gs, &n_guards);
1194
1195
0
  const int max_sample = get_max_sample_size(gs, n_guards);
1196
0
  const int min_filtered_sample = get_min_filtered_sample_size();
1197
1198
0
  log_info(LD_GUARD, "Expanding the sample guard set. We have %d guards "
1199
0
           "in the sample, and %d eligible guards to extend it with.",
1200
0
           n_sampled, smartlist_len(eligible_guards));
1201
1202
0
  while (n_usable_filtered_guards < min_filtered_sample) {
1203
    /* Has our sample grown too large to expand? */
1204
0
    if (n_sampled >= max_sample) {
1205
0
      log_info(LD_GUARD, "Not expanding the guard sample any further; "
1206
0
               "just hit the maximum sample threshold of %d",
1207
0
               max_sample);
1208
0
      goto done;
1209
0
    }
1210
1211
    /* Did we run out of guards? */
1212
0
    if (smartlist_len(eligible_guards) == 0) {
1213
      /* LCOV_EXCL_START
1214
         As long as MAX_SAMPLE_THRESHOLD makes can't be adjusted to
1215
         allow all guards to be sampled, this can't be reached.
1216
       */
1217
0
      log_info(LD_GUARD, "Not expanding the guard sample any further; "
1218
0
               "just ran out of eligible guards");
1219
0
      goto done;
1220
      /* LCOV_EXCL_STOP */
1221
0
    }
1222
1223
    /* Otherwise we can add at least one new guard. */
1224
0
    added_guard = select_and_add_guard_item_for_sample(gs, eligible_guards);
1225
0
    if (!added_guard)
1226
0
      goto done; // LCOV_EXCL_LINE -- only fails on BUG.
1227
1228
0
    ++n_sampled;
1229
1230
0
    if (added_guard->is_usable_filtered_guard)
1231
0
      ++n_usable_filtered_guards;
1232
0
  }
1233
1234
0
 done:
1235
0
  smartlist_free(eligible_guards);
1236
0
  return added_guard;
1237
0
}
1238
1239
/**
1240
 * Helper: <b>guard</b> has just been removed from the sampled guards:
1241
 * also remove it from primary and confirmed. */
1242
static void
1243
remove_guard_from_confirmed_and_primary_lists(guard_selection_t *gs,
1244
                                              entry_guard_t *guard)
1245
0
{
1246
0
  if (guard->is_primary) {
1247
0
    guard->is_primary = 0;
1248
0
    smartlist_remove_keeporder(gs->primary_entry_guards, guard);
1249
0
  } else {
1250
0
    if (BUG(smartlist_contains(gs->primary_entry_guards, guard))) {
1251
0
      smartlist_remove_keeporder(gs->primary_entry_guards, guard);
1252
0
    }
1253
0
  }
1254
1255
0
  if (guard->confirmed_idx >= 0) {
1256
0
    smartlist_remove_keeporder(gs->confirmed_entry_guards, guard);
1257
0
    guard->confirmed_idx = -1;
1258
0
    guard->confirmed_on_date = 0;
1259
0
  } else {
1260
0
    if (BUG(smartlist_contains(gs->confirmed_entry_guards, guard))) {
1261
      // LCOV_EXCL_START
1262
0
      smartlist_remove_keeporder(gs->confirmed_entry_guards, guard);
1263
      // LCOV_EXCL_STOP
1264
0
    }
1265
0
  }
1266
0
}
1267
1268
/** Return true iff <b>guard</b> is currently "listed" -- that is, it
1269
 * appears in the consensus, or as a configured bridge (as
1270
 * appropriate) */
1271
MOCK_IMPL(STATIC int,
1272
entry_guard_is_listed,(guard_selection_t *gs, const entry_guard_t *guard))
1273
0
{
1274
0
  if (gs->type == GS_TYPE_BRIDGE) {
1275
0
    return NULL != get_bridge_info_for_guard(guard);
1276
0
  } else {
1277
0
    const node_t *node = node_get_by_id(guard->identity);
1278
1279
0
    return node && node_is_possible_guard(node);
1280
0
  }
1281
0
}
1282
1283
/**
1284
 * Enumerate <b>sampled_entry_guards</b> smartlist in <b>gs</b>.
1285
 * For each <b>entry_guard_t</b> object in smartlist, do the following:
1286
 *  * Update <b>currently_listed</b> field to reflect if guard is listed
1287
 *    in guard selection <b>gs</b>.
1288
 *  * Set <b>unlisted_since_date</b> to approximate UNIX time of
1289
 *    unlisting if guard is unlisted (randomize within 20% of
1290
 *    get_remove_unlisted_guards_after_seconds()). Otherwise,
1291
 *    set it to 0.
1292
 *
1293
 * Require <b>gs</b> to be non-null pointer.
1294
 * Return a number of entries updated.
1295
 */
1296
static size_t
1297
sampled_guards_update_consensus_presence(guard_selection_t *gs)
1298
0
{
1299
0
  size_t n_changes = 0;
1300
1301
0
  tor_assert(gs);
1302
1303
0
  const time_t unlisted_since_slop =
1304
0
    get_remove_unlisted_guards_after_seconds() /  5;
1305
1306
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1307
    /* XXXX #20827 check ed ID too */
1308
0
    const int is_listed = entry_guard_is_listed(gs, guard);
1309
1310
0
    if (is_listed && ! guard->currently_listed) {
1311
0
      ++n_changes;
1312
0
      guard->currently_listed = 1;
1313
0
      guard->unlisted_since_date = 0;
1314
0
      log_info(LD_GUARD, "Sampled guard %s is now listed again.",
1315
0
               entry_guard_describe(guard));
1316
0
    } else if (!is_listed && guard->currently_listed) {
1317
0
      ++n_changes;
1318
0
      guard->currently_listed = 0;
1319
0
      guard->unlisted_since_date = randomize_time(approx_time(),
1320
0
                                                  unlisted_since_slop);
1321
0
      log_info(LD_GUARD, "Sampled guard %s is now unlisted.",
1322
0
               entry_guard_describe(guard));
1323
0
    } else if (is_listed && guard->currently_listed) {
1324
0
      log_debug(LD_GUARD, "Sampled guard %s is still listed.",
1325
0
               entry_guard_describe(guard));
1326
0
    } else {
1327
0
      tor_assert(! is_listed && ! guard->currently_listed);
1328
0
      log_debug(LD_GUARD, "Sampled guard %s is still unlisted.",
1329
0
                entry_guard_describe(guard));
1330
0
    }
1331
1332
    /* Clean up unlisted_since_date, just in case. */
1333
0
    if (guard->currently_listed && guard->unlisted_since_date) {
1334
0
      ++n_changes;
1335
0
      guard->unlisted_since_date = 0;
1336
0
      log_warn(LD_BUG, "Sampled guard %s was listed, but with "
1337
0
               "unlisted_since_date set. Fixing.",
1338
0
               entry_guard_describe(guard));
1339
0
    } else if (!guard->currently_listed && ! guard->unlisted_since_date) {
1340
0
      ++n_changes;
1341
0
      guard->unlisted_since_date = randomize_time(approx_time(),
1342
0
                                                  unlisted_since_slop);
1343
0
      log_warn(LD_BUG, "Sampled guard %s was unlisted, but with "
1344
0
               "unlisted_since_date unset. Fixing.",
1345
0
               entry_guard_describe(guard));
1346
0
    }
1347
0
  } SMARTLIST_FOREACH_END(guard);
1348
1349
0
  return n_changes;
1350
0
}
1351
1352
/**
1353
 * Enumerate <b>sampled_entry_guards</b> smartlist in <b>gs</b>.
1354
 * For each <b>entry_guard_t</b> object in smartlist, do the following:
1355
 * * If <b>currently_listed</b> is false and <b>unlisted_since_date</b>
1356
 *   is earlier than <b>remove_if_unlisted_since</b> - remove it.
1357
 * * Otherwise, check if <b>sampled_on_date</b> is earlier than
1358
 *   <b>maybe_remove_if_sampled_before</b>.
1359
 *   * When above condition is correct, remove the guard if:
1360
 *     * It was never confirmed.
1361
 *     * It was confirmed before <b>remove_if_confirmed_before</b>.
1362
 *
1363
 * Require <b>gs</b> to be non-null pointer.
1364
 * Return number of entries deleted.
1365
 */
1366
static size_t
1367
sampled_guards_prune_obsolete_entries(guard_selection_t *gs,
1368
                                  const time_t remove_if_unlisted_since,
1369
                                  const time_t maybe_remove_if_sampled_before,
1370
                                  const time_t remove_if_confirmed_before)
1371
0
{
1372
0
  size_t n_changes = 0;
1373
1374
0
  tor_assert(gs);
1375
1376
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1377
0
    int rmv = 0;
1378
1379
0
    if (guard->currently_listed == 0 &&
1380
0
        guard->unlisted_since_date < remove_if_unlisted_since) {
1381
      /*
1382
        "We have a live consensus, and {IS_LISTED} is false, and
1383
         {FIRST_UNLISTED_AT} is over get_remove_unlisted_guards_after_days()
1384
         days in the past."
1385
      */
1386
0
      log_info(LD_GUARD, "Removing sampled guard %s: it has been unlisted "
1387
0
               "for over %d days", entry_guard_describe(guard),
1388
0
               get_remove_unlisted_guards_after_days());
1389
0
      rmv = 1;
1390
0
    } else if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
1391
      /* We have a live consensus, and {ADDED_ON_DATE} is over
1392
        {GUARD_LIFETIME} ago, *and* {CONFIRMED_ON_DATE} is either
1393
        "never", or over {GUARD_CONFIRMED_MIN_LIFETIME} ago.
1394
      */
1395
0
      if (guard->confirmed_on_date == 0) {
1396
0
        rmv = 1;
1397
0
        log_info(LD_GUARD, "Removing sampled guard %s: it was sampled "
1398
0
                 "over %d days ago, but never confirmed.",
1399
0
                 entry_guard_describe(guard),
1400
0
                 get_guard_lifetime() / 86400);
1401
0
      } else if (guard->confirmed_on_date < remove_if_confirmed_before) {
1402
0
        rmv = 1;
1403
0
        log_info(LD_GUARD, "Removing sampled guard %s: it was sampled "
1404
0
                 "over %d days ago, and confirmed over %d days ago.",
1405
0
                 entry_guard_describe(guard),
1406
0
                 get_guard_lifetime() / 86400,
1407
0
                 get_guard_confirmed_min_lifetime() / 86400);
1408
0
      }
1409
0
    }
1410
1411
0
    if (rmv) {
1412
0
      ++n_changes;
1413
0
      SMARTLIST_DEL_CURRENT_KEEPORDER(gs->sampled_entry_guards, guard);
1414
0
      remove_guard_from_confirmed_and_primary_lists(gs, guard);
1415
0
      entry_guard_free(guard);
1416
0
    }
1417
0
  } SMARTLIST_FOREACH_END(guard);
1418
1419
0
  return n_changes;
1420
0
}
1421
1422
/**
1423
 * Update the status of all sampled guards based on the arrival of a
1424
 * new consensus networkstatus document.  This will include marking
1425
 * some guards as listed or unlisted, and removing expired guards. */
1426
STATIC void
1427
sampled_guards_update_from_consensus(guard_selection_t *gs)
1428
0
{
1429
0
  tor_assert(gs);
1430
1431
  // It's important to use a reasonably live consensus here; we want clients
1432
  // to bootstrap even if their clock is skewed by more than 2-3 hours.
1433
  // But we don't want to make changes based on anything that's really old.
1434
0
  if (reasonably_live_consensus_is_missing(gs)) {
1435
0
    log_info(LD_GUARD, "Not updating the sample guard set; we have "
1436
0
             "no reasonably live consensus.");
1437
0
    return;
1438
0
  }
1439
0
  log_info(LD_GUARD, "Updating sampled guard status based on received "
1440
0
           "consensus.");
1441
1442
  /* First: Update listed/unlisted. */
1443
0
  size_t n_changes = sampled_guards_update_consensus_presence(gs);
1444
1445
0
  const time_t remove_if_unlisted_since =
1446
0
    approx_time() - get_remove_unlisted_guards_after_seconds();
1447
0
  const time_t maybe_remove_if_sampled_before =
1448
0
    approx_time() - get_guard_lifetime();
1449
0
  const time_t remove_if_confirmed_before =
1450
0
    approx_time() - get_guard_confirmed_min_lifetime();
1451
1452
  /* Then: remove the ones that have been junk for too long */
1453
0
  n_changes +=
1454
0
    sampled_guards_prune_obsolete_entries(gs,
1455
0
                                          remove_if_unlisted_since,
1456
0
                                          maybe_remove_if_sampled_before,
1457
0
                                          remove_if_confirmed_before);
1458
1459
0
  if (n_changes) {
1460
0
    gs->primary_guards_up_to_date = 0;
1461
0
    entry_guards_update_filtered_sets(gs);
1462
    /* We don't need to rebuild the confirmed list right here -- we may have
1463
     * removed confirmed guards above, but we can't have added any new
1464
     * confirmed guards.
1465
     */
1466
0
    entry_guards_changed_for_guard_selection(gs);
1467
0
  }
1468
0
}
1469
1470
/**
1471
 * Return true iff <b>node</b> is a Tor relay that we are configured to
1472
 * be able to connect to. */
1473
static int
1474
node_passes_guard_filter(const or_options_t *options,
1475
                         const node_t *node)
1476
0
{
1477
  /* NOTE: Make sure that this function stays in sync with
1478
   * options_transition_affects_entry_guards */
1479
0
  if (routerset_contains_node(options->ExcludeNodes, node))
1480
0
    return 0;
1481
1482
0
  if (options->EntryNodes &&
1483
0
      !routerset_contains_node(options->EntryNodes, node))
1484
0
    return 0;
1485
1486
0
  if (!reachable_addr_allows_node(node, FIREWALL_OR_CONNECTION, 0))
1487
0
    return 0;
1488
1489
0
  if (node_is_a_configured_bridge(node))
1490
0
    return 0;
1491
1492
0
  return 1;
1493
0
}
1494
1495
/** Helper: Return true iff <b>bridge</b> passes our configuration
1496
 * filter-- if it is a relay that we are configured to be able to
1497
 * connect to. */
1498
static int
1499
bridge_passes_guard_filter(const or_options_t *options,
1500
                           const bridge_info_t *bridge)
1501
0
{
1502
0
  tor_assert(bridge);
1503
0
  if (!bridge)
1504
0
    return 0;
1505
1506
0
  if (routerset_contains_bridge(options->ExcludeNodes, bridge))
1507
0
    return 0;
1508
1509
  /* Ignore entrynodes */
1510
0
  const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
1511
1512
0
  if (!reachable_addr_allows_addr(&addrport->addr,
1513
0
                                            addrport->port,
1514
0
                                            FIREWALL_OR_CONNECTION,
1515
0
                                            0, 0))
1516
0
    return 0;
1517
1518
0
  return 1;
1519
0
}
1520
1521
/**
1522
 * Return true iff <b>guard</b> is a Tor relay that we are configured to
1523
 * be able to connect to, and we haven't disabled it for omission from
1524
 * the consensus or path bias issues. */
1525
static int
1526
entry_guard_passes_filter(const or_options_t *options, guard_selection_t *gs,
1527
                          entry_guard_t *guard)
1528
0
{
1529
0
  if (guard->currently_listed == 0)
1530
0
    return 0;
1531
0
  if (guard->pb.path_bias_disabled)
1532
0
    return 0;
1533
1534
0
  if (gs->type == GS_TYPE_BRIDGE) {
1535
0
    const bridge_info_t *bridge = get_bridge_info_for_guard(guard);
1536
0
    if (bridge == NULL)
1537
0
      return 0;
1538
0
    return bridge_passes_guard_filter(options, bridge);
1539
0
  } else {
1540
0
    const node_t *node = node_get_by_id(guard->identity);
1541
0
    if (node == NULL) {
1542
      // This can happen when currently_listed is true, and we're not updating
1543
      // it because we don't have a live consensus.
1544
0
      return 0;
1545
0
    }
1546
1547
0
    return node_passes_guard_filter(options, node);
1548
0
  }
1549
0
}
1550
1551
/** Return true iff <b>guard</b> is in the same family as <b>node</b>.
1552
 */
1553
static int
1554
guard_in_node_family(const entry_guard_t *guard, const node_t *node)
1555
0
{
1556
0
  const node_t *guard_node = node_get_by_id(guard->identity);
1557
0
  if (guard_node) {
1558
0
    return nodes_in_same_family(guard_node, node);
1559
0
  } else {
1560
    /* If we don't have a node_t for the guard node, we might have
1561
     * a bridge_info_t for it. So let's check to see whether the bridge
1562
     * address matches has any family issues.
1563
     *
1564
     * (Strictly speaking, I believe this check is unnecessary, since we only
1565
     * use it to avoid the exit's family when building circuits, and we don't
1566
     * build multihop circuits until we have a routerinfo_t for the
1567
     * bridge... at which point, we'll also have a node_t for the
1568
     * bridge. Nonetheless, it seems wise to include it, in case our
1569
     * assumptions change down the road.  -nickm.)
1570
     */
1571
0
    if (get_options()->EnforceDistinctSubnets && guard->bridge_addr) {
1572
0
      tor_addr_t node_addr;
1573
0
      node_get_addr(node, &node_addr);
1574
0
      if (router_addrs_in_same_network(&node_addr,
1575
0
                                       &guard->bridge_addr->addr)) {
1576
0
        return 1;
1577
0
      }
1578
0
    }
1579
0
    return 0;
1580
0
  }
1581
0
}
1582
1583
/* Allocate and return a new exit guard restriction (where <b>exit_id</b> is of
1584
 * size DIGEST_LEN) */
1585
STATIC entry_guard_restriction_t *
1586
guard_create_exit_restriction(const uint8_t *exit_id)
1587
0
{
1588
0
  entry_guard_restriction_t *rst = NULL;
1589
0
  rst = tor_malloc_zero(sizeof(entry_guard_restriction_t));
1590
0
  rst->type = RST_EXIT_NODE;
1591
0
  memcpy(rst->exclude_id, exit_id, DIGEST_LEN);
1592
0
  return rst;
1593
0
}
1594
1595
/* Allocate and return a new exit guard restriction that excludes all current
1596
 * and pending conflux guards */
1597
STATIC entry_guard_restriction_t *
1598
guard_create_conflux_restriction(const origin_circuit_t *circ,
1599
                                 const uint8_t *exit_id)
1600
0
{
1601
0
  entry_guard_restriction_t *rst = NULL;
1602
0
  rst = tor_malloc_zero(sizeof(entry_guard_restriction_t));
1603
0
  rst->type = RST_EXCL_LIST;
1604
0
  rst->excluded = smartlist_new();
1605
0
  conflux_add_guards_to_exclude_list(circ, rst->excluded);
1606
0
  memcpy(rst->exclude_id, exit_id, DIGEST_LEN);
1607
0
  return rst;
1608
0
}
1609
1610
/** If we have fewer than this many possible usable guards, don't set
1611
 * MD-availability-based restrictions: we might denylist all of them. */
1612
0
#define MIN_GUARDS_FOR_MD_RESTRICTION 10
1613
1614
/** Return true if we should set md dirserver restrictions. We might not want
1615
 *  to set those if our guard options are too restricted, since we don't want
1616
 *  to denylist all of them. */
1617
static int
1618
should_set_md_dirserver_restriction(void)
1619
0
{
1620
0
  const guard_selection_t *gs = get_guard_selection_info();
1621
0
  int num_usable_guards = num_reachable_filtered_guards(gs, NULL);
1622
1623
  /* Don't set restriction if too few reachable filtered guards. */
1624
0
  if (num_usable_guards < MIN_GUARDS_FOR_MD_RESTRICTION) {
1625
0
    log_info(LD_GUARD, "Not setting md restriction: only %d"
1626
0
             " usable guards.", num_usable_guards);
1627
0
    return 0;
1628
0
  }
1629
1630
  /* We have enough usable guards: set MD restriction */
1631
0
  return 1;
1632
0
}
1633
1634
/** Allocate and return an outdated md guard restriction. Return NULL if no
1635
 *  such restriction is needed. */
1636
STATIC entry_guard_restriction_t *
1637
guard_create_dirserver_md_restriction(void)
1638
0
{
1639
0
  entry_guard_restriction_t *rst = NULL;
1640
1641
0
  if (!should_set_md_dirserver_restriction()) {
1642
0
    log_debug(LD_GUARD, "Not setting md restriction: too few "
1643
0
              "filtered guards.");
1644
0
    return NULL;
1645
0
  }
1646
1647
0
  rst = tor_malloc_zero(sizeof(entry_guard_restriction_t));
1648
0
  rst->type = RST_OUTDATED_MD_DIRSERVER;
1649
1650
0
  return rst;
1651
0
}
1652
1653
/* Return True if <b>guard</b> obeys the exit restriction <b>rst</b>. */
1654
static int
1655
guard_obeys_exit_restriction(const entry_guard_t *guard,
1656
                             const entry_guard_restriction_t *rst)
1657
0
{
1658
0
  tor_assert(rst->type == RST_EXIT_NODE ||
1659
0
          rst->type == RST_EXCL_LIST);
1660
1661
  // Exclude the exit ID and all of its family.
1662
0
  const node_t *node = node_get_by_id((const char*)rst->exclude_id);
1663
0
  if (node && guard_in_node_family(guard, node))
1664
0
    return 0;
1665
1666
0
  return tor_memneq(guard->identity, rst->exclude_id, DIGEST_LEN);
1667
0
}
1668
1669
/** Return True if <b>guard</b> should be used as a dirserver for fetching
1670
 *  microdescriptors. */
1671
static int
1672
guard_obeys_md_dirserver_restriction(const entry_guard_t *guard)
1673
0
{
1674
  /* If this guard is an outdated dirserver, don't use it. */
1675
0
  if (microdesc_relay_is_outdated_dirserver(guard->identity)) {
1676
0
    log_info(LD_GENERAL, "Skipping %s dirserver: outdated",
1677
0
             hex_str(guard->identity, DIGEST_LEN));
1678
0
    return 0;
1679
0
  }
1680
1681
0
  log_debug(LD_GENERAL, "%s dirserver obeys md restrictions",
1682
0
            hex_str(guard->identity, DIGEST_LEN));
1683
1684
0
  return 1;
1685
0
}
1686
1687
/**
1688
 * Return true if a restriction is reachability related, such that it should
1689
 * cause us to consider additional primary guards when selecting one.
1690
 */
1691
static bool
1692
entry_guard_restriction_is_reachability(const entry_guard_restriction_t *rst)
1693
0
{
1694
0
  tor_assert(rst);
1695
0
  return (rst->type == RST_OUTDATED_MD_DIRSERVER);
1696
0
}
1697
1698
/**
1699
 * Return true iff <b>guard</b> obeys the restrictions defined in <b>rst</b>.
1700
 * (If <b>rst</b> is NULL, there are no restrictions.)
1701
 */
1702
static int
1703
entry_guard_obeys_restriction(const entry_guard_t *guard,
1704
                              const entry_guard_restriction_t *rst)
1705
0
{
1706
0
  tor_assert(guard);
1707
0
  if (! rst)
1708
0
    return 1; // No restriction?  No problem.
1709
1710
0
  if (rst->type == RST_EXIT_NODE) {
1711
0
    return guard_obeys_exit_restriction(guard, rst);
1712
0
  } else if (rst->type == RST_OUTDATED_MD_DIRSERVER) {
1713
0
    return guard_obeys_md_dirserver_restriction(guard);
1714
0
  } else if (rst->type == RST_EXCL_LIST) {
1715
0
    return guard_obeys_exit_restriction(guard, rst) &&
1716
0
        !smartlist_contains_digest(rst->excluded, guard->identity);
1717
0
  }
1718
1719
0
  tor_assert_nonfatal_unreached();
1720
0
  return 0;
1721
0
}
1722
1723
/**
1724
 * Update the <b>is_filtered_guard</b> and <b>is_usable_filtered_guard</b>
1725
 * flags on <b>guard</b>. */
1726
void
1727
entry_guard_set_filtered_flags(const or_options_t *options,
1728
                               guard_selection_t *gs,
1729
                               entry_guard_t *guard)
1730
0
{
1731
0
  unsigned was_filtered = guard->is_filtered_guard;
1732
0
  guard->is_filtered_guard = 0;
1733
0
  guard->is_usable_filtered_guard = 0;
1734
1735
0
  if (entry_guard_passes_filter(options, gs, guard)) {
1736
0
    guard->is_filtered_guard = 1;
1737
1738
0
    if (guard->is_reachable != GUARD_REACHABLE_NO)
1739
0
      guard->is_usable_filtered_guard = 1;
1740
1741
0
    entry_guard_consider_retry(guard);
1742
0
  }
1743
0
  log_debug(LD_GUARD, "Updated sampled guard %s: filtered=%d; "
1744
0
            "reachable_filtered=%d.", entry_guard_describe(guard),
1745
0
            guard->is_filtered_guard, guard->is_usable_filtered_guard);
1746
1747
0
  if (!bool_eq(was_filtered, guard->is_filtered_guard)) {
1748
    /* This guard might now be primary or nonprimary. */
1749
0
    gs->primary_guards_up_to_date = 0;
1750
0
  }
1751
0
}
1752
1753
/**
1754
 * Update the <b>is_filtered_guard</b> and <b>is_usable_filtered_guard</b>
1755
 * flag on every guard in <b>gs</b>. */
1756
STATIC void
1757
entry_guards_update_filtered_sets(guard_selection_t *gs)
1758
0
{
1759
0
  const or_options_t *options = get_options();
1760
1761
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1762
0
    entry_guard_set_filtered_flags(options, gs, guard);
1763
0
  } SMARTLIST_FOREACH_END(guard);
1764
0
}
1765
1766
/**
1767
 * Return the first sampled guard from the reachable filtered sample guards
1768
 * in <b>gs</b>, subject to the exclusion rules listed in <b>flags</b>.
1769
 * Return NULL if no such guard can be found.
1770
 *
1771
 * Make sure that the sample is big enough, and that all the filter flags
1772
 * are set correctly, before calling this function.
1773
 *
1774
 * If a restriction is provided in <b>rst</b>, do not return any guards that
1775
 * violate it.
1776
 **/
1777
STATIC entry_guard_t *
1778
first_reachable_filtered_entry_guard(guard_selection_t *gs,
1779
                                       const entry_guard_restriction_t *rst,
1780
                                       unsigned flags)
1781
0
{
1782
0
  tor_assert(gs);
1783
0
  entry_guard_t *result = NULL;
1784
0
  const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
1785
0
  const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
1786
0
  const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
1787
0
  const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
1788
0
  const unsigned need_descriptor = flags & SAMPLE_EXCLUDE_NO_DESCRIPTOR;
1789
1790
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1791
0
    entry_guard_consider_retry(guard);
1792
0
  } SMARTLIST_FOREACH_END(guard);
1793
1794
0
  const int n_reachable_filtered = num_reachable_filtered_guards(gs, rst);
1795
1796
0
  log_info(LD_GUARD, "Trying to sample a reachable guard: We know of %d "
1797
0
           "in the USABLE_FILTERED set.", n_reachable_filtered);
1798
1799
0
  const int min_filtered_sample = get_min_filtered_sample_size();
1800
0
  if (n_reachable_filtered < min_filtered_sample) {
1801
0
    log_info(LD_GUARD, "  (That isn't enough. Trying to expand the sample.)");
1802
0
    entry_guards_expand_sample(gs);
1803
0
  }
1804
1805
0
  if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
1806
0
    entry_guards_update_primary(gs);
1807
1808
  /* Build the set of reachable filtered guards. */
1809
0
  smartlist_t *reachable_filtered_sample = smartlist_new();
1810
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1811
0
    entry_guard_consider_retry(guard);// redundant, but cheap.
1812
0
    if (! entry_guard_obeys_restriction(guard, rst))
1813
0
      continue;
1814
0
    if (! guard->is_usable_filtered_guard)
1815
0
      continue;
1816
0
    if (exclude_confirmed && guard->confirmed_idx >= 0)
1817
0
      continue;
1818
0
    if (exclude_primary && guard->is_primary)
1819
0
      continue;
1820
0
    if (exclude_pending && guard->is_pending)
1821
0
      continue;
1822
0
    if (need_descriptor && !guard_has_descriptor(guard))
1823
0
      continue;
1824
0
    smartlist_add(reachable_filtered_sample, guard);
1825
0
  } SMARTLIST_FOREACH_END(guard);
1826
1827
0
  log_info(LD_GUARD, "  (After filters [%x], we have %d guards to consider.)",
1828
0
           flags, smartlist_len(reachable_filtered_sample));
1829
1830
0
  if (smartlist_len(reachable_filtered_sample)) {
1831
    /**
1832
     * Get the first guard of the filtered set builds from
1833
     * sampled_entry_guards. Proposal 310 suggests this design to overcome
1834
     * performance and security issues linked to the previous selection
1835
     * method. The guard selected here should be filtered out if this function
1836
     * is called again in the same context. I.e., if we filter guards to add
1837
     * them into some list X, then the guards from list X will be filtered out
1838
     * when this function is called again. Hence it requires setting exclude
1839
     * flags in a appropriate way (depending of the context of the caller).
1840
     */
1841
0
    result = smartlist_get(reachable_filtered_sample, 0);
1842
0
    log_info(LD_GUARD, "  (Selected %s.)",
1843
0
             result ? entry_guard_describe(result) : "<null>");
1844
0
  }
1845
0
  smartlist_free(reachable_filtered_sample);
1846
1847
0
  return result;
1848
0
}
1849
1850
static int
1851
compare_guards_by_confirmed_idx(const void **a_, const void **b_)
1852
0
{
1853
0
  const entry_guard_t *a = *a_, *b = *b_;
1854
0
  if (a->confirmed_idx < b->confirmed_idx)
1855
0
    return -1;
1856
0
  else if (a->confirmed_idx > b->confirmed_idx)
1857
0
    return 1;
1858
0
  else
1859
0
    return 0;
1860
0
}
1861
/**
1862
 * Helper: compare two entry_guard_t by their sampled_idx values.
1863
 * Used to sort the sampled list
1864
 */
1865
static int
1866
compare_guards_by_sampled_idx(const void **a_, const void **b_)
1867
0
{
1868
0
  const entry_guard_t *a = *a_, *b = *b_;
1869
0
  if (a->sampled_idx < b->sampled_idx)
1870
0
    return -1;
1871
0
  else if (a->sampled_idx > b->sampled_idx)
1872
0
    return 1;
1873
0
  else
1874
0
    return 0;
1875
0
}
1876
1877
/**
1878
 * Find the confirmed guards from among the sampled guards in <b>gs</b>,
1879
 * and put them in confirmed_entry_guards in the correct
1880
 * order. Recalculate their indices.
1881
 */
1882
STATIC void
1883
entry_guards_update_confirmed(guard_selection_t *gs)
1884
0
{
1885
0
  smartlist_clear(gs->confirmed_entry_guards);
1886
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
1887
0
    if (guard->confirmed_idx >= 0)
1888
0
      smartlist_add(gs->confirmed_entry_guards, guard);
1889
0
  } SMARTLIST_FOREACH_END(guard);
1890
1891
0
  smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
1892
  /** Needed to keep a dense array of confirmed_idx */
1893
0
  int any_changed = 0;
1894
0
  SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
1895
0
    if (guard->confirmed_idx != guard_sl_idx) {
1896
0
      any_changed = 1;
1897
0
      guard->confirmed_idx = guard_sl_idx;
1898
0
    }
1899
0
  } SMARTLIST_FOREACH_END(guard);
1900
1901
0
  gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
1902
  // We need the confirmed list to always be give guards in sampled order
1903
0
  smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_sampled_idx);
1904
1905
0
  if (any_changed) {
1906
0
    entry_guards_changed_for_guard_selection(gs);
1907
0
  }
1908
0
}
1909
1910
/**
1911
 * Mark <b>guard</b> as a confirmed guard -- that is, one that we have
1912
 * connected to, and intend to use again.
1913
 */
1914
STATIC void
1915
make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard)
1916
0
{
1917
0
  if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
1918
0
    return; // LCOV_EXCL_LINE
1919
1920
0
  if (BUG(smartlist_contains(gs->confirmed_entry_guards, guard)))
1921
0
    return; // LCOV_EXCL_LINE
1922
1923
0
  const int GUARD_LIFETIME = get_guard_lifetime();
1924
0
  guard->confirmed_on_date = randomize_time(approx_time(), GUARD_LIFETIME/10);
1925
1926
0
  log_info(LD_GUARD, "Marking %s as a confirmed guard (index %d)",
1927
0
           entry_guard_describe(guard),
1928
0
           gs->next_confirmed_idx);
1929
1930
0
  guard->confirmed_idx = gs->next_confirmed_idx++;
1931
0
  smartlist_add(gs->confirmed_entry_guards, guard);
1932
  /** The confirmation ordering might not be the sample ordering. We need to
1933
   * reorder */
1934
0
  smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_sampled_idx);
1935
1936
  // This confirmed guard might kick something else out of the primary
1937
  // guards.
1938
0
  gs->primary_guards_up_to_date = 0;
1939
1940
0
  entry_guards_changed_for_guard_selection(gs);
1941
0
}
1942
1943
/**
1944
 * Recalculate the list of primary guards (the ones we'd prefer to use) from
1945
 * the filtered sample and the confirmed list.
1946
 */
1947
STATIC void
1948
entry_guards_update_primary(guard_selection_t *gs)
1949
0
{
1950
0
  tor_assert(gs);
1951
1952
  // prevent recursion. Recursion is potentially very bad here.
1953
0
  static int running = 0;
1954
0
  tor_assert(!running);
1955
0
  running = 1;
1956
1957
0
  const int N_PRIMARY_GUARDS = get_n_primary_guards();
1958
1959
0
  smartlist_t *new_primary_guards = smartlist_new();
1960
0
  smartlist_t *old_primary_guards = smartlist_new();
1961
0
  smartlist_add_all(old_primary_guards, gs->primary_entry_guards);
1962
1963
  /* Set this flag now, to prevent the calls below from recursing. */
1964
0
  gs->primary_guards_up_to_date = 1;
1965
1966
  /* First, can we fill it up with confirmed guards? */
1967
0
  SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
1968
0
    if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
1969
0
      break;
1970
0
    if (! guard->is_filtered_guard)
1971
0
      continue;
1972
0
    guard->is_primary = 1;
1973
0
    smartlist_add(new_primary_guards, guard);
1974
0
  } SMARTLIST_FOREACH_END(guard);
1975
1976
0
  SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
1977
    /* Can we keep any older primary guards? First remove all the ones
1978
     * that we already kept. */
1979
0
    if (smartlist_contains(new_primary_guards, guard)) {
1980
0
      SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
1981
0
      continue;
1982
0
    }
1983
1984
    /* Now add any that are still good. */
1985
0
    if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
1986
0
        guard->is_filtered_guard) {
1987
0
      guard->is_primary = 1;
1988
0
      smartlist_add(new_primary_guards, guard);
1989
0
      SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
1990
0
    } else {
1991
      /* Mark the remaining previous primary guards as non-primary */
1992
0
      guard->is_primary = 0;
1993
0
    }
1994
0
  } SMARTLIST_FOREACH_END(guard);
1995
1996
  /* Finally, fill out the list with sampled guards. */
1997
0
  while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
1998
0
    entry_guard_t *guard = first_reachable_filtered_entry_guard(gs, NULL,
1999
0
                                            SAMPLE_EXCLUDE_CONFIRMED|
2000
0
                                            SAMPLE_EXCLUDE_PRIMARY|
2001
0
                                            SAMPLE_NO_UPDATE_PRIMARY);
2002
0
    if (!guard)
2003
0
      break;
2004
0
    guard->is_primary = 1;
2005
0
    smartlist_add(new_primary_guards, guard);
2006
0
  }
2007
2008
0
#if 1
2009
  /* Debugging. */
2010
0
  SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, guard, {
2011
0
    tor_assert_nonfatal(
2012
0
                   bool_eq(guard->is_primary,
2013
0
                           smartlist_contains(new_primary_guards, guard)));
2014
0
  });
2015
0
#endif /* 1 */
2016
2017
0
  const int any_change = !smartlist_ptrs_eq(gs->primary_entry_guards,
2018
0
                                            new_primary_guards);
2019
0
  if (any_change) {
2020
0
    log_info(LD_GUARD, "Primary entry guards have changed. "
2021
0
             "New primary guard list is: ");
2022
0
    int n = smartlist_len(new_primary_guards);
2023
0
    SMARTLIST_FOREACH_BEGIN(new_primary_guards, entry_guard_t *, g) {
2024
0
      log_info(LD_GUARD, "  %d/%d: %s%s%s",
2025
0
               g_sl_idx+1, n, entry_guard_describe(g),
2026
0
               g->confirmed_idx >= 0 ? " (confirmed)" : "",
2027
0
               g->is_filtered_guard ? "" : " (excluded by filter)");
2028
0
    } SMARTLIST_FOREACH_END(g);
2029
0
    smartlist_sort(new_primary_guards, compare_guards_by_sampled_idx);
2030
0
  }
2031
2032
0
  smartlist_free(old_primary_guards);
2033
0
  smartlist_free(gs->primary_entry_guards);
2034
0
  gs->primary_entry_guards = new_primary_guards;
2035
0
  gs->primary_guards_up_to_date = 1;
2036
0
  running = 0;
2037
0
}
2038
2039
/**
2040
 * Return the number of seconds after the last attempt at which we should
2041
 * retry a guard that has been failing since <b>failing_since</b>.
2042
 */
2043
static int
2044
get_retry_schedule(time_t failing_since, time_t now,
2045
                   int is_primary)
2046
0
{
2047
0
  const unsigned SIX_HOURS = 6 * 3600;
2048
0
  const unsigned FOUR_DAYS = 4 * 86400;
2049
0
  const unsigned SEVEN_DAYS = 7 * 86400;
2050
2051
0
  time_t tdiff;
2052
0
  if (now > failing_since) {
2053
0
    tdiff = now - failing_since;
2054
0
  } else {
2055
0
    tdiff = 0;
2056
0
  }
2057
2058
0
  const struct {
2059
0
    time_t maximum; int primary_delay; int nonprimary_delay;
2060
0
  } delays[] = {
2061
    // clang-format off
2062
0
    { SIX_HOURS,    10*60,  1*60*60 },
2063
0
    { FOUR_DAYS,    90*60,  4*60*60 },
2064
0
    { SEVEN_DAYS, 4*60*60, 18*60*60 },
2065
0
    { TIME_MAX,   9*60*60, 36*60*60 }
2066
    // clang-format on
2067
0
  };
2068
2069
0
  unsigned i;
2070
0
  for (i = 0; i < ARRAY_LENGTH(delays); ++i) {
2071
0
    if (tdiff <= delays[i].maximum) {
2072
0
      return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
2073
0
    }
2074
0
  }
2075
  /* LCOV_EXCL_START -- can't reach, since delays ends with TIME_MAX. */
2076
0
  tor_assert_nonfatal_unreached();
2077
0
  return 36*60*60;
2078
  /* LCOV_EXCL_STOP */
2079
0
}
2080
2081
/**
2082
 * If <b>guard</b> is unreachable, consider whether enough time has passed
2083
 * to consider it maybe-reachable again.
2084
 */
2085
STATIC void
2086
entry_guard_consider_retry(entry_guard_t *guard)
2087
0
{
2088
0
  if (guard->is_reachable != GUARD_REACHABLE_NO)
2089
0
    return; /* No retry needed. */
2090
2091
0
  const time_t now = approx_time();
2092
0
  const int delay =
2093
0
    get_retry_schedule(guard->failing_since, now, guard->is_primary);
2094
0
  const time_t last_attempt = guard->last_tried_to_connect;
2095
2096
  /* Check if it is a bridge and we don't have its descriptor yet */
2097
0
  if (guard->bridge_addr && !guard_has_descriptor(guard)) {
2098
    /* We want to leave the retry schedule to fetch_bridge_descriptors(),
2099
     * so we don't have two retry schedules clobbering each other. See
2100
     * bugs 40396 and 40497 for details of why we need this exception. */
2101
0
    return;
2102
0
  }
2103
2104
0
  if (BUG(last_attempt == 0) ||
2105
0
      now >= last_attempt + delay) {
2106
    /* We should mark this retriable. */
2107
0
    char tbuf[ISO_TIME_LEN+1];
2108
0
    format_local_iso_time(tbuf, last_attempt);
2109
0
    log_info(LD_GUARD, "Marked %s%sguard %s for possible retry, since we "
2110
0
             "haven't tried to use it since %s.",
2111
0
             guard->is_primary?"primary ":"",
2112
0
             guard->confirmed_idx>=0?"confirmed ":"",
2113
0
             entry_guard_describe(guard),
2114
0
             tbuf);
2115
2116
0
    guard->is_reachable = GUARD_REACHABLE_MAYBE;
2117
0
    if (guard->is_filtered_guard)
2118
0
      guard->is_usable_filtered_guard = 1;
2119
0
  }
2120
0
}
2121
2122
/** Tell the entry guards subsystem that we have confirmed that as of
2123
 * just now, we're on the internet. */
2124
void
2125
entry_guards_note_internet_connectivity(guard_selection_t *gs)
2126
0
{
2127
0
  gs->last_time_on_internet = approx_time();
2128
0
}
2129
2130
/**
2131
 * Pick a primary guard for use with a circuit, if available. Update the
2132
 * <b>last_tried_to_connect</b> time and the <b>is_pending</b> fields of the
2133
 * guard as appropriate.  Set <b>state_out</b> to the new guard-state
2134
 * of the circuit.
2135
 */
2136
static entry_guard_t *
2137
select_primary_guard_for_circuit(guard_selection_t *gs,
2138
                                 guard_usage_t usage,
2139
                                 const entry_guard_restriction_t *rst,
2140
                                 unsigned *state_out)
2141
0
{
2142
0
  const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2143
0
  entry_guard_t *chosen_guard = NULL;
2144
2145
0
  int num_entry_guards_to_consider = get_n_primary_guards_to_use(usage);
2146
0
  smartlist_t *usable_primary_guards = smartlist_new();
2147
0
  int num_entry_guards_considered = 0;
2148
2149
0
  SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
2150
0
    entry_guard_consider_retry(guard);
2151
0
    if (!entry_guard_obeys_restriction(guard, rst)) {
2152
0
      log_info(LD_GUARD, "Entry guard %s doesn't obey restriction, we test the"
2153
0
          " next one", entry_guard_describe(guard));
2154
0
      if (!entry_guard_restriction_is_reachability(rst)) {
2155
0
        log_info(LD_GUARD,
2156
0
                 "Skipping guard %s due to circuit path restriction. "
2157
0
                 "Have %d, considered: %d, to consider: %d",
2158
0
                 entry_guard_describe(guard),
2159
0
                 smartlist_len(usable_primary_guards),
2160
0
                 num_entry_guards_considered,
2161
0
                 num_entry_guards_to_consider);
2162
        /* If the restriction is a circuit path restriction (as opposed to a
2163
         * reachability restriction), count this as considered. */
2164
0
        num_entry_guards_considered++;
2165
2166
        /* If we have considered enough guards, *and* we actually have a guard,
2167
         * then proceed to select one from the list. */
2168
0
        if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2169
          /* This should not happen with 2-leg conflux unless there is a
2170
           * race between removing a failed leg and a retry, but check
2171
           * anyway and log. */
2172
0
          if (smartlist_len(usable_primary_guards) == 0) {
2173
0
            static ratelim_t guardlog = RATELIM_INIT(60);
2174
0
            log_fn_ratelim(&guardlog, LOG_NOTICE, LD_GUARD,
2175
0
                           "All current guards excluded by path restriction "
2176
0
                           "type %d; using an additional guard.",
2177
0
                           rst->type);
2178
0
          } else {
2179
0
            break;
2180
0
          }
2181
0
        }
2182
0
      }
2183
0
      continue;
2184
0
    }
2185
0
    if (guard->is_reachable != GUARD_REACHABLE_NO) {
2186
0
      if (need_descriptor && !guard_has_descriptor(guard)) {
2187
0
        log_info(LD_GUARD, "Guard %s does not have a descriptor",
2188
0
            entry_guard_describe(guard));
2189
0
        continue;
2190
0
      }
2191
0
      *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
2192
0
      guard->last_tried_to_connect = approx_time();
2193
0
      smartlist_add(usable_primary_guards, guard);
2194
0
      num_entry_guards_considered++;
2195
2196
      /* If we have considered enough guards, then proceed to select
2197
       * one from the list. */
2198
0
      if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2199
0
        break;
2200
0
      }
2201
0
    } else {
2202
0
      log_info(LD_GUARD, "Guard %s is not reachable",
2203
0
          entry_guard_describe(guard));
2204
0
    }
2205
0
  } SMARTLIST_FOREACH_END(guard);
2206
2207
0
  if (smartlist_len(usable_primary_guards)) {
2208
0
    chosen_guard = smartlist_choose(usable_primary_guards);
2209
0
    log_info(LD_GUARD,
2210
0
        "Selected primary guard %s for circuit from a list size of %d.",
2211
0
        entry_guard_describe(chosen_guard),
2212
0
        smartlist_len(usable_primary_guards));
2213
    /* Describe each guard in the list: */
2214
0
    SMARTLIST_FOREACH_BEGIN(usable_primary_guards, entry_guard_t *, guard) {
2215
0
      log_info(LD_GUARD, "  %s", entry_guard_describe(guard));
2216
0
    } SMARTLIST_FOREACH_END(guard);
2217
0
    smartlist_free(usable_primary_guards);
2218
0
  }
2219
2220
0
  smartlist_free(usable_primary_guards);
2221
0
  return chosen_guard;
2222
0
}
2223
2224
/**
2225
 * For use with a circuit, pick a non-pending running filtered confirmed guard,
2226
 * if one is available. Update the <b>last_tried_to_connect</b> time and the
2227
 * <b>is_pending</b> fields of the guard as appropriate. Set <b>state_out</b>
2228
 * to the new guard-state of the circuit.
2229
 */
2230
static entry_guard_t *
2231
select_confirmed_guard_for_circuit(guard_selection_t *gs,
2232
                                  guard_usage_t usage,
2233
                                  const entry_guard_restriction_t *rst,
2234
                                  unsigned *state_out)
2235
0
{
2236
0
  const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2237
2238
0
  SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
2239
0
    if (guard->is_primary)
2240
0
      continue; /* we already considered this one. */
2241
0
    if (! entry_guard_obeys_restriction(guard, rst))
2242
0
      continue;
2243
0
    entry_guard_consider_retry(guard);
2244
0
    if (guard->is_usable_filtered_guard && ! guard->is_pending) {
2245
0
      if (need_descriptor && !guard_has_descriptor(guard))
2246
0
        continue; /* not a bug */
2247
0
      guard->is_pending = 1;
2248
0
      guard->last_tried_to_connect = approx_time();
2249
0
      *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2250
0
      log_info(LD_GUARD, "No primary guards available. Selected confirmed "
2251
0
               "guard %s for circuit. Will try other guards before using "
2252
0
               "this circuit.",
2253
0
               entry_guard_describe(guard));
2254
0
      return guard;
2255
0
    }
2256
0
  } SMARTLIST_FOREACH_END(guard);
2257
2258
0
  return NULL;
2259
0
}
2260
2261
/**
2262
 * For use with a circuit, pick a usable filtered guard. Update the
2263
 * <b>last_tried_to_connect</b> time and the <b>is_pending</b> fields of the
2264
 * guard as appropriate. Set <b>state_out</b> to the new guard-state of the
2265
 * circuit.
2266
 */
2267
static entry_guard_t *
2268
select_filtered_guard_for_circuit(guard_selection_t *gs,
2269
                                  guard_usage_t usage,
2270
                                  const entry_guard_restriction_t *rst,
2271
                                  unsigned *state_out)
2272
0
{
2273
0
  const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2274
0
  entry_guard_t *chosen_guard = NULL;
2275
0
  unsigned flags = 0;
2276
0
  if (need_descriptor)
2277
0
    flags |= SAMPLE_EXCLUDE_NO_DESCRIPTOR;
2278
0
  chosen_guard = first_reachable_filtered_entry_guard(gs,
2279
0
                                                 rst,
2280
0
                                                 SAMPLE_EXCLUDE_CONFIRMED |
2281
0
                                                 SAMPLE_EXCLUDE_PRIMARY |
2282
0
                                                 SAMPLE_EXCLUDE_PENDING |
2283
0
                                                 flags);
2284
0
  if (!chosen_guard) {
2285
0
    return NULL;
2286
0
  }
2287
2288
0
  chosen_guard->is_pending = 1;
2289
0
  chosen_guard->last_tried_to_connect = approx_time();
2290
0
  *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2291
0
  log_info(LD_GUARD, "No primary or confirmed guards available. Selected "
2292
0
           "guard %s for circuit. Will try other guards before "
2293
0
           "using this circuit.",
2294
0
           entry_guard_describe(chosen_guard));
2295
0
  return chosen_guard;
2296
0
}
2297
2298
/**
2299
 * Get a guard for use with a circuit.  Prefer to pick a running primary
2300
 * guard; then a non-pending running filtered confirmed guard; then a
2301
 * non-pending runnable filtered guard.  Update the
2302
 * <b>last_tried_to_connect</b> time and the <b>is_pending</b> fields of the
2303
 * guard as appropriate.  Set <b>state_out</b> to the new guard-state
2304
 * of the circuit.
2305
 */
2306
STATIC entry_guard_t *
2307
select_entry_guard_for_circuit(guard_selection_t *gs,
2308
                               guard_usage_t usage,
2309
                               const entry_guard_restriction_t *rst,
2310
                               unsigned *state_out)
2311
0
{
2312
0
  entry_guard_t *chosen_guard = NULL;
2313
0
  tor_assert(gs);
2314
0
  tor_assert(state_out);
2315
2316
0
  if (!gs->primary_guards_up_to_date)
2317
0
    entry_guards_update_primary(gs);
2318
2319
  /* "If any entry in PRIMARY_GUARDS has {is_reachable} status of
2320
      <maybe> or <yes>, return the first such guard." */
2321
0
  chosen_guard = select_primary_guard_for_circuit(gs, usage, rst, state_out);
2322
0
  if (chosen_guard) {
2323
0
    log_info(LD_GUARD, "Selected primary guard %s for circuit.",
2324
0
             entry_guard_describe(chosen_guard));
2325
0
    return chosen_guard;
2326
0
  }
2327
2328
  /* "Otherwise, if the ordered intersection of {CONFIRMED_GUARDS}
2329
      and {USABLE_FILTERED_GUARDS} is nonempty, return the first
2330
      entry in that intersection that has {is_pending} set to
2331
      false." */
2332
0
  chosen_guard = select_confirmed_guard_for_circuit(gs, usage, rst, state_out);
2333
0
  if (chosen_guard) {
2334
0
     log_info(LD_GUARD, "Selected confirmed guard %s for circuit.",
2335
0
             entry_guard_describe(chosen_guard));
2336
0
    return chosen_guard;
2337
0
  }
2338
2339
  /* "Otherwise, if there is no such entry, select a member
2340
   * {USABLE_FILTERED_GUARDS} following the sample ordering" */
2341
0
  chosen_guard = select_filtered_guard_for_circuit(gs, usage, rst, state_out);
2342
2343
0
  if (chosen_guard == NULL) {
2344
0
    log_info(LD_GUARD, "Absolutely no sampled guards were available. "
2345
0
             "Marking all guards for retry and starting from top again.");
2346
0
    mark_all_guards_maybe_reachable(gs);
2347
0
    return NULL;
2348
0
  }
2349
2350
0
  log_info(LD_GUARD, "Selected filtered guard %s for circuit.",
2351
0
             entry_guard_describe(chosen_guard));
2352
0
  return chosen_guard;
2353
0
}
2354
2355
/**
2356
 * Note that we failed to connect to or build circuits through <b>guard</b>.
2357
 * Use with a guard returned by select_entry_guard_for_circuit().
2358
 */
2359
STATIC void
2360
entry_guards_note_guard_failure(guard_selection_t *gs,
2361
                                entry_guard_t *guard)
2362
0
{
2363
0
  tor_assert(gs);
2364
2365
0
  guard->is_reachable = GUARD_REACHABLE_NO;
2366
0
  guard->is_usable_filtered_guard = 0;
2367
2368
0
  guard->is_pending = 0;
2369
0
  if (guard->failing_since == 0)
2370
0
    guard->failing_since = approx_time();
2371
2372
  /* This guard not reachable: send GUARD DOWN event */
2373
0
  control_event_guard(guard->nickname, guard->identity, "DOWN");
2374
2375
0
  log_info(LD_GUARD, "Recorded failure for %s%sguard %s",
2376
0
           guard->is_primary?"primary ":"",
2377
0
           guard->confirmed_idx>=0?"confirmed ":"",
2378
0
           entry_guard_describe(guard));
2379
2380
  /* Schedule a re-assessment of whether we have enough dir info to
2381
   * use the network. Counterintuitively, *losing* a bridge might actually
2382
   * be just what we need to *resume* using the network, if we had it in
2383
   * state GUARD_REACHABLE_MAYBE and we were stalling to learn this
2384
   * outcome. See bug 40396 for more details. */
2385
0
  router_dir_info_changed();
2386
0
}
2387
2388
/**
2389
 * Note that we successfully connected to, and built a circuit through
2390
 * <b>guard</b>. Given the old guard-state of the circuit in <b>old_state</b>,
2391
 * return the new guard-state of the circuit.
2392
 *
2393
 * Be aware: the circuit is only usable when its guard-state becomes
2394
 * GUARD_CIRC_STATE_COMPLETE.
2395
 **/
2396
STATIC unsigned
2397
entry_guards_note_guard_success(guard_selection_t *gs,
2398
                                entry_guard_t *guard,
2399
                                unsigned old_state)
2400
0
{
2401
0
  tor_assert(gs);
2402
2403
  /* Save this, since we're about to overwrite it. */
2404
0
  const time_t last_time_on_internet = gs->last_time_on_internet;
2405
0
  gs->last_time_on_internet = approx_time();
2406
2407
  /* If guard was not already marked as reachable, send a GUARD UP signal */
2408
0
  if (guard->is_reachable != GUARD_REACHABLE_YES) {
2409
0
    control_event_guard(guard->nickname, guard->identity, "UP");
2410
2411
    /* Schedule a re-assessment of whether we have enough dir info to
2412
     * use the network. One of our guards has just moved to
2413
     * GUARD_REACHABLE_YES, so maybe we can resume using the network
2414
     * now. */
2415
0
    router_dir_info_changed();
2416
0
  }
2417
2418
0
  guard->is_reachable = GUARD_REACHABLE_YES;
2419
0
  guard->failing_since = 0;
2420
0
  guard->is_pending = 0;
2421
0
  if (guard->is_filtered_guard)
2422
0
    guard->is_usable_filtered_guard = 1;
2423
2424
0
  if (guard->confirmed_idx < 0) {
2425
0
    make_guard_confirmed(gs, guard);
2426
0
    if (!gs->primary_guards_up_to_date)
2427
0
      entry_guards_update_primary(gs);
2428
0
  }
2429
2430
0
  unsigned new_state;
2431
0
  switch (old_state) {
2432
0
    case GUARD_CIRC_STATE_COMPLETE:
2433
0
    case GUARD_CIRC_STATE_USABLE_ON_COMPLETION:
2434
0
      new_state = GUARD_CIRC_STATE_COMPLETE;
2435
0
      break;
2436
0
    default:
2437
0
      tor_assert_nonfatal_unreached();
2438
0
      FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL;
2439
0
    case GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD:
2440
0
      if (guard->is_primary) {
2441
        /* XXXX #20832 -- I don't actually like this logic. It seems to make
2442
         * us a little more susceptible to evil-ISP attacks.  The mitigations
2443
         * I'm thinking of, however, aren't local to this point, so I'll leave
2444
         * it alone. */
2445
        /* This guard may have become primary by virtue of being confirmed.
2446
         * If so, the circuit for it is now complete.
2447
         */
2448
0
        new_state = GUARD_CIRC_STATE_COMPLETE;
2449
0
      } else {
2450
0
        new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2451
0
      }
2452
0
      break;
2453
0
  }
2454
2455
0
  if (! guard->is_primary) {
2456
0
    if (last_time_on_internet + get_internet_likely_down_interval()
2457
0
        < approx_time()) {
2458
0
      mark_primary_guards_maybe_reachable(gs);
2459
0
    }
2460
0
  }
2461
2462
0
  log_info(LD_GUARD, "Recorded success for %s%sguard %s",
2463
0
           guard->is_primary?"primary ":"",
2464
0
           guard->confirmed_idx>=0?"confirmed ":"",
2465
0
           entry_guard_describe(guard));
2466
2467
0
  return new_state;
2468
0
}
2469
2470
/**
2471
 * Helper: Return true iff <b>a</b> has higher priority than <b>b</b>.
2472
 */
2473
STATIC int
2474
entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b)
2475
0
{
2476
0
  tor_assert(a && b);
2477
0
  if (a == b)
2478
0
    return 0;
2479
2480
  /* Confirmed is always better than unconfirmed; lower index better
2481
     than higher */
2482
0
  if (a->confirmed_idx < 0) {
2483
0
    if (b->confirmed_idx >= 0)
2484
0
      return 0;
2485
0
  } else {
2486
0
    if (b->confirmed_idx < 0)
2487
0
      return 1;
2488
2489
    /* Lower confirmed_idx is better than higher. */
2490
0
    return (a->confirmed_idx < b->confirmed_idx);
2491
0
  }
2492
2493
  /* If we reach this point, both are unconfirmed. If one is pending, it
2494
   * has higher priority. */
2495
0
  if (a->is_pending) {
2496
0
    if (! b->is_pending)
2497
0
      return 1;
2498
2499
    /* Both are pending: earlier last_tried_connect wins. */
2500
0
    return a->last_tried_to_connect < b->last_tried_to_connect;
2501
0
  } else {
2502
0
    if (b->is_pending)
2503
0
      return 0;
2504
2505
    /* Neither is pending: priorities are equal. */
2506
0
    return 0;
2507
0
  }
2508
0
}
2509
2510
/** Release all storage held in <b>restriction</b> */
2511
STATIC void
2512
entry_guard_restriction_free_(entry_guard_restriction_t *rst)
2513
0
{
2514
0
  if (rst && rst->excluded) {
2515
0
    SMARTLIST_FOREACH(rst->excluded, void *, g,
2516
0
                      tor_free(g));
2517
0
    smartlist_free(rst->excluded);
2518
0
  }
2519
0
  tor_free(rst);
2520
0
}
2521
2522
/**
2523
 * Release all storage held in <b>state</b>.
2524
 */
2525
void
2526
circuit_guard_state_free_(circuit_guard_state_t *state)
2527
0
{
2528
0
  if (!state)
2529
0
    return;
2530
0
  entry_guard_restriction_free(state->restrictions);
2531
0
  entry_guard_handle_free(state->guard);
2532
0
  tor_free(state);
2533
0
}
2534
2535
/** Allocate and return a new circuit_guard_state_t to track the result
2536
 * of using <b>guard</b> for a given operation. */
2537
MOCK_IMPL(STATIC circuit_guard_state_t *,
2538
circuit_guard_state_new,(entry_guard_t *guard, unsigned state,
2539
                         entry_guard_restriction_t *rst))
2540
0
{
2541
0
  circuit_guard_state_t *result;
2542
2543
0
  result = tor_malloc_zero(sizeof(circuit_guard_state_t));
2544
0
  result->guard = entry_guard_handle_new(guard);
2545
0
  result->state = state;
2546
0
  result->state_set_at = approx_time();
2547
0
  result->restrictions = rst;
2548
2549
0
  return result;
2550
0
}
2551
2552
/**
2553
 * Pick a suitable entry guard for a circuit in, and place that guard
2554
 * in *<b>chosen_node_out</b>. Set *<b>guard_state_out</b> to an opaque
2555
 * state object that will record whether the circuit is ready to be used
2556
 * or not. Return 0 on success; on failure, return -1.
2557
 *
2558
 * If a restriction is provided in <b>rst</b>, do not return any guards that
2559
 * violate it, and remember that restriction in <b>guard_state_out</b> for
2560
 * later use. (Takes ownership of the <b>rst</b> object.)
2561
 */
2562
int
2563
entry_guard_pick_for_circuit(guard_selection_t *gs,
2564
                             guard_usage_t usage,
2565
                             entry_guard_restriction_t *rst,
2566
                             const node_t **chosen_node_out,
2567
                             circuit_guard_state_t **guard_state_out)
2568
0
{
2569
0
  tor_assert(gs);
2570
0
  tor_assert(chosen_node_out);
2571
0
  tor_assert(guard_state_out);
2572
0
  *chosen_node_out = NULL;
2573
0
  *guard_state_out = NULL;
2574
2575
0
  unsigned state = 0;
2576
0
  entry_guard_t *guard =
2577
0
    select_entry_guard_for_circuit(gs, usage, rst, &state);
2578
0
  if (! guard)
2579
0
    goto fail;
2580
0
  if (BUG(state == 0))
2581
0
    goto fail;
2582
0
  const node_t *node = node_get_by_id(guard->identity);
2583
  // XXXX #20827 check Ed ID.
2584
0
  if (! node)
2585
0
    goto fail;
2586
0
  if (BUG(usage != GUARD_USAGE_DIRGUARD &&
2587
0
          !node_has_preferred_descriptor(node, 1)))
2588
0
    goto fail;
2589
2590
0
  *chosen_node_out = node;
2591
0
  *guard_state_out = circuit_guard_state_new(guard, state, rst);
2592
2593
0
  return 0;
2594
0
 fail:
2595
0
  entry_guard_restriction_free(rst);
2596
0
  return -1;
2597
0
}
2598
2599
/**
2600
 * Called by the circuit building module when a circuit has succeeded: informs
2601
 * the guards code that the guard in *<b>guard_state_p</b> is working, and
2602
 * advances the state of the guard module.  On a GUARD_USABLE_NEVER return
2603
 * value, the circuit is broken and should not be used.  On a GUARD_USABLE_NOW
2604
 * return value, the circuit is ready to use.  On a GUARD_MAYBE_USABLE_LATER
2605
 * return value, the circuit should not be used until we find out whether
2606
 * preferred guards will work for us.
2607
 */
2608
guard_usable_t
2609
entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
2610
0
{
2611
0
  if (BUG(*guard_state_p == NULL))
2612
0
    return GUARD_USABLE_NEVER;
2613
2614
0
  entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2615
0
  if (! guard || BUG(guard->in_selection == NULL))
2616
0
    return GUARD_USABLE_NEVER;
2617
2618
0
  unsigned newstate =
2619
0
    entry_guards_note_guard_success(guard->in_selection, guard,
2620
0
                                    (*guard_state_p)->state);
2621
2622
0
  (*guard_state_p)->state = newstate;
2623
0
  (*guard_state_p)->state_set_at = approx_time();
2624
2625
0
  if (newstate == GUARD_CIRC_STATE_COMPLETE) {
2626
0
    return GUARD_USABLE_NOW;
2627
0
  } else {
2628
0
    return GUARD_MAYBE_USABLE_LATER;
2629
0
  }
2630
0
}
2631
2632
/** Cancel the selection of *<b>guard_state_p</b> without declaring
2633
 * success or failure. It is safe to call this function if success or
2634
 * failure _has_ already been declared. */
2635
void
2636
entry_guard_cancel(circuit_guard_state_t **guard_state_p)
2637
0
{
2638
0
  if (BUG(*guard_state_p == NULL))
2639
0
    return;
2640
0
  entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2641
0
  if (! guard)
2642
0
    return;
2643
2644
  /* XXXX prop271 -- last_tried_to_connect_at will be erroneous here, but this
2645
   * function will only get called in "bug" cases anyway. */
2646
0
  guard->is_pending = 0;
2647
0
  circuit_guard_state_free(*guard_state_p);
2648
0
  *guard_state_p = NULL;
2649
0
}
2650
2651
/**
2652
 * Called by the circuit building module when a circuit has failed:
2653
 * informs the guards code that the guard in *<b>guard_state_p</b> is
2654
 * not working, and advances the state of the guard module.
2655
 */
2656
void
2657
entry_guard_failed(circuit_guard_state_t **guard_state_p)
2658
0
{
2659
0
  if (BUG(*guard_state_p == NULL))
2660
0
    return;
2661
2662
0
  entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2663
0
  if (! guard || BUG(guard->in_selection == NULL))
2664
0
    return;
2665
2666
0
  entry_guards_note_guard_failure(guard->in_selection, guard);
2667
2668
0
  (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
2669
0
  (*guard_state_p)->state_set_at = approx_time();
2670
0
}
2671
2672
/**
2673
 * Run the entry_guard_failed() function on every circuit that is
2674
 * pending on <b>chan</b>.
2675
 */
2676
void
2677
entry_guard_chan_failed(channel_t *chan)
2678
0
{
2679
0
  if (!chan)
2680
0
    return;
2681
2682
0
  smartlist_t *pending = smartlist_new();
2683
0
  circuit_get_all_pending_on_channel(pending, chan);
2684
0
  SMARTLIST_FOREACH_BEGIN(pending, circuit_t *, circ) {
2685
0
    if (!CIRCUIT_IS_ORIGIN(circ))
2686
0
      continue;
2687
2688
0
    origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
2689
0
    if (origin_circ->guard_state) {
2690
      /* We might have no guard state if we didn't use a guard on this
2691
       * circuit (eg it's for a fallback directory). */
2692
0
      entry_guard_failed(&origin_circ->guard_state);
2693
0
    }
2694
0
  } SMARTLIST_FOREACH_END(circ);
2695
0
  smartlist_free(pending);
2696
0
}
2697
2698
/**
2699
 * Return true iff every primary guard in <b>gs</b> is believed to
2700
 * be unreachable.
2701
 */
2702
STATIC int
2703
entry_guards_all_primary_guards_are_down(guard_selection_t *gs)
2704
0
{
2705
0
  tor_assert(gs);
2706
0
  if (!gs->primary_guards_up_to_date)
2707
0
    entry_guards_update_primary(gs);
2708
0
  SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
2709
0
    entry_guard_consider_retry(guard);
2710
0
    if (guard->is_reachable != GUARD_REACHABLE_NO)
2711
0
      return 0;
2712
0
  } SMARTLIST_FOREACH_END(guard);
2713
0
  return 1;
2714
0
}
2715
2716
/** Wrapper for entry_guard_has_higher_priority that compares the
2717
 * guard-priorities of a pair of circuits. Return 1 if <b>a</b> has higher
2718
 * priority than <b>b</b>.
2719
 *
2720
 * If a restriction is provided in <b>rst</b>, then do not consider
2721
 * <b>a</b> to have higher priority if it violates the restriction.
2722
 */
2723
static int
2724
circ_state_has_higher_priority(origin_circuit_t *a,
2725
                               const entry_guard_restriction_t *rst,
2726
                               origin_circuit_t *b)
2727
0
{
2728
0
  circuit_guard_state_t *state_a = origin_circuit_get_guard_state(a);
2729
0
  circuit_guard_state_t *state_b = origin_circuit_get_guard_state(b);
2730
2731
0
  tor_assert(state_a);
2732
0
  tor_assert(state_b);
2733
2734
0
  entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
2735
0
  entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
2736
2737
0
  if (! guard_a) {
2738
    /* Unknown guard -- never higher priority. */
2739
0
    return 0;
2740
0
  } else if (! guard_b) {
2741
    /* Known guard -- higher priority than any unknown guard. */
2742
0
    return 1;
2743
0
  } else  if (! entry_guard_obeys_restriction(guard_a, rst)) {
2744
    /* Restriction violated; guard_a cannot have higher priority. */
2745
0
    return 0;
2746
0
  } else {
2747
    /* Both known -- compare.*/
2748
0
    return entry_guard_has_higher_priority(guard_a, guard_b);
2749
0
  }
2750
0
}
2751
2752
/**
2753
 * Look at all of the origin_circuit_t * objects in <b>all_circuits_in</b>,
2754
 * and see if any of them that were previously not ready to use for
2755
 * guard-related reasons are now ready to use. Place those circuits
2756
 * in <b>newly_complete_out</b>, and mark them COMPLETE.
2757
 *
2758
 * Return 1 if we upgraded any circuits, and 0 otherwise.
2759
 */
2760
int
2761
entry_guards_upgrade_waiting_circuits(guard_selection_t *gs,
2762
                                      const smartlist_t *all_circuits_in,
2763
                                      smartlist_t *newly_complete_out)
2764
0
{
2765
0
  tor_assert(gs);
2766
0
  tor_assert(all_circuits_in);
2767
0
  tor_assert(newly_complete_out);
2768
2769
0
  if (! entry_guards_all_primary_guards_are_down(gs)) {
2770
    /* We only upgrade a waiting circuit if the primary guards are all
2771
     * down. */
2772
0
    log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits, "
2773
0
              "but not all primary guards were definitely down.");
2774
0
    return 0;
2775
0
  }
2776
2777
0
  int n_waiting = 0;
2778
0
  int n_complete = 0;
2779
0
  int n_complete_blocking = 0;
2780
0
  origin_circuit_t *best_waiting_circuit = NULL;
2781
0
  smartlist_t *all_circuits = smartlist_new();
2782
0
  SMARTLIST_FOREACH_BEGIN(all_circuits_in, origin_circuit_t *, circ) {
2783
    // We filter out circuits that aren't ours, or which we can't
2784
    // reason about.
2785
0
    circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
2786
0
    if (state == NULL)
2787
0
      continue;
2788
0
    entry_guard_t *guard = entry_guard_handle_get(state->guard);
2789
0
    if (!guard || guard->in_selection != gs)
2790
0
      continue;
2791
0
    if (TO_CIRCUIT(circ)->marked_for_close) {
2792
      /* Don't consider any marked for close circuits. */
2793
0
      continue;
2794
0
    }
2795
2796
0
    smartlist_add(all_circuits, circ);
2797
0
  } SMARTLIST_FOREACH_END(circ);
2798
2799
0
  SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
2800
0
    circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
2801
0
    if (BUG(state == NULL))
2802
0
      continue;
2803
2804
0
    if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
2805
0
      ++n_waiting;
2806
0
      if (! best_waiting_circuit ||
2807
0
          circ_state_has_higher_priority(circ, NULL, best_waiting_circuit)) {
2808
0
        best_waiting_circuit = circ;
2809
0
      }
2810
0
    }
2811
0
  } SMARTLIST_FOREACH_END(circ);
2812
2813
0
  if (! best_waiting_circuit) {
2814
0
    log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits, "
2815
0
              "but didn't find any.");
2816
0
    goto no_change;
2817
0
  }
2818
2819
  /* We'll need to keep track of what restrictions were used when picking this
2820
   * circuit, so that we don't allow any circuit without those restrictions to
2821
   * block it. */
2822
0
  const entry_guard_restriction_t *rst_on_best_waiting =
2823
0
    origin_circuit_get_guard_state(best_waiting_circuit)->restrictions;
2824
2825
  /* First look at the complete circuits: Do any block this circuit? */
2826
0
  SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
2827
    /* "C2 "blocks" C1 if:
2828
        * C2 obeys all the restrictions that C1 had to obey, AND
2829
        * C2 has higher priority than C1, AND
2830
        * Either C2 is <complete>, or C2 is <waiting_for_better_guard>,
2831
          or C2 has been <usable_if_no_better_guard> for no more than
2832
          {NONPRIMARY_GUARD_CONNECT_TIMEOUT} seconds."
2833
    */
2834
0
    circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
2835
0
    if (BUG(state == NULL))
2836
0
      continue;
2837
0
    if (state->state != GUARD_CIRC_STATE_COMPLETE)
2838
0
      continue;
2839
0
    ++n_complete;
2840
0
    if (circ_state_has_higher_priority(circ, rst_on_best_waiting,
2841
0
                                       best_waiting_circuit))
2842
0
      ++n_complete_blocking;
2843
0
  } SMARTLIST_FOREACH_END(circ);
2844
2845
0
  if (n_complete_blocking) {
2846
0
    log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
2847
0
              "%d complete and %d guard-stalled. At least one complete "
2848
0
              "circuit had higher priority, so not upgrading.",
2849
0
              n_complete, n_waiting);
2850
0
    goto no_change;
2851
0
  }
2852
2853
  /* " * If any circuit C1 is <waiting_for_better_guard>, AND:
2854
          * All primary guards have reachable status of <no>.
2855
          * There is no circuit C2 that "blocks" C1.
2856
         Then, upgrade C1 to <complete>.""
2857
  */
2858
0
  int n_blockers_found = 0;
2859
0
  const time_t state_set_at_cutoff =
2860
0
    approx_time() - get_nonprimary_guard_connect_timeout();
2861
0
  SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
2862
0
    circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
2863
0
    if (BUG(state == NULL))
2864
0
      continue;
2865
0
    if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
2866
0
      continue;
2867
0
    if (state->state_set_at <= state_set_at_cutoff)
2868
0
      continue;
2869
0
    if (circ_state_has_higher_priority(circ, rst_on_best_waiting,
2870
0
                                       best_waiting_circuit))
2871
0
      ++n_blockers_found;
2872
0
  } SMARTLIST_FOREACH_END(circ);
2873
2874
0
  if (n_blockers_found) {
2875
0
    log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
2876
0
              "%d guard-stalled, but %d pending circuit(s) had higher "
2877
0
              "guard priority, so not upgrading.",
2878
0
              n_waiting, n_blockers_found);
2879
0
    goto no_change;
2880
0
  }
2881
2882
  /* Okay. We have a best waiting circuit, and we aren't waiting for
2883
     anything better.  Add all circuits with that priority to the
2884
     list, and call them COMPLETE. */
2885
0
  int n_succeeded = 0;
2886
0
  SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
2887
0
    circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
2888
0
    if (BUG(state == NULL))
2889
0
      continue;
2890
0
    if (circ != best_waiting_circuit && rst_on_best_waiting) {
2891
      /* Can't upgrade other circ with same priority as best; might
2892
         be blocked. */
2893
0
      continue;
2894
0
    }
2895
0
    if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
2896
0
      continue;
2897
0
    if (circ_state_has_higher_priority(best_waiting_circuit, NULL, circ))
2898
0
      continue;
2899
2900
0
    state->state = GUARD_CIRC_STATE_COMPLETE;
2901
0
    state->state_set_at = approx_time();
2902
0
    smartlist_add(newly_complete_out, circ);
2903
0
    ++n_succeeded;
2904
0
  } SMARTLIST_FOREACH_END(circ);
2905
2906
0
  log_info(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
2907
0
           "%d guard-stalled, %d complete. %d of the guard-stalled "
2908
0
           "circuit(s) had high enough priority to upgrade.",
2909
0
           n_waiting, n_complete, n_succeeded);
2910
2911
0
  tor_assert_nonfatal(n_succeeded >= 1);
2912
0
  smartlist_free(all_circuits);
2913
0
  return 1;
2914
2915
0
 no_change:
2916
0
  smartlist_free(all_circuits);
2917
0
  return 0;
2918
0
}
2919
2920
/**
2921
 * Return true iff the circuit whose state is <b>guard_state</b> should
2922
 * expire.
2923
 */
2924
int
2925
entry_guard_state_should_expire(circuit_guard_state_t *guard_state)
2926
0
{
2927
0
  if (guard_state == NULL)
2928
0
    return 0;
2929
0
  const time_t expire_if_waiting_since =
2930
0
    approx_time() - get_nonprimary_guard_idle_timeout();
2931
0
  return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
2932
0
          && guard_state->state_set_at < expire_if_waiting_since);
2933
0
}
2934
2935
/**
2936
 * Update all derived pieces of the guard selection state in <b>gs</b>.
2937
 * Return true iff we should stop using all previously generated circuits.
2938
 */
2939
int
2940
entry_guards_update_all(guard_selection_t *gs)
2941
0
{
2942
0
  sampled_guards_update_from_consensus(gs);
2943
0
  entry_guards_update_filtered_sets(gs);
2944
0
  entry_guards_update_confirmed(gs);
2945
0
  entry_guards_update_primary(gs);
2946
0
  return 0;
2947
0
}
2948
2949
/**
2950
 * Return a newly allocated string for encoding the persistent parts of
2951
 * <b>guard</b> to the state file. <b>dense_sampled_idx</b> refers to the
2952
 * sampled_idx made dense for this <b>guard</b>. Encoding all guards should
2953
 * lead to a dense array of sampled_idx in the state file.
2954
 */
2955
STATIC char *
2956
entry_guard_encode_for_state(entry_guard_t *guard, int dense_sampled_idx)
2957
0
{
2958
  /*
2959
   * The meta-format we use is K=V K=V K=V... where K can be any
2960
   * characters excepts space and =, and V can be any characters except
2961
   * space.  The order of entries is not allowed to matter.
2962
   * Unrecognized K=V entries are persisted; recognized but erroneous
2963
   * entries are corrected.
2964
   */
2965
2966
0
  smartlist_t *result = smartlist_new();
2967
0
  char tbuf[ISO_TIME_LEN+1];
2968
2969
0
  tor_assert(guard);
2970
2971
0
  smartlist_add_asprintf(result, "in=%s", guard->selection_name);
2972
0
  smartlist_add_asprintf(result, "rsa_id=%s",
2973
0
                         hex_str(guard->identity, DIGEST_LEN));
2974
0
  if (guard->bridge_addr) {
2975
0
    smartlist_add_asprintf(result, "bridge_addr=%s:%d",
2976
0
                           fmt_and_decorate_addr(&guard->bridge_addr->addr),
2977
0
                           guard->bridge_addr->port);
2978
0
  }
2979
0
  if (strlen(guard->nickname) && is_legal_nickname(guard->nickname)) {
2980
0
    smartlist_add_asprintf(result, "nickname=%s", guard->nickname);
2981
0
  }
2982
2983
0
  format_iso_time_nospace(tbuf, guard->sampled_on_date);
2984
0
  smartlist_add_asprintf(result, "sampled_on=%s", tbuf);
2985
  // Replacing the sampled_idx by dense array
2986
0
  smartlist_add_asprintf(result, "sampled_idx=%d", dense_sampled_idx);
2987
0
  if (guard->sampled_by_version) {
2988
0
    smartlist_add_asprintf(result, "sampled_by=%s",
2989
0
                           guard->sampled_by_version);
2990
0
  }
2991
2992
0
  if (guard->unlisted_since_date > 0) {
2993
0
    format_iso_time_nospace(tbuf, guard->unlisted_since_date);
2994
0
    smartlist_add_asprintf(result, "unlisted_since=%s", tbuf);
2995
0
  }
2996
2997
0
  smartlist_add_asprintf(result, "listed=%d",
2998
0
                         (int)guard->currently_listed);
2999
3000
0
  if (guard->confirmed_idx >= 0) {
3001
0
    format_iso_time_nospace(tbuf, guard->confirmed_on_date);
3002
0
    smartlist_add_asprintf(result, "confirmed_on=%s", tbuf);
3003
3004
0
    smartlist_add_asprintf(result, "confirmed_idx=%d", guard->confirmed_idx);
3005
0
  }
3006
3007
0
  const double EPSILON = 1.0e-6;
3008
3009
  /* Make a copy of the pathbias object, since we will want to update
3010
     some of them */
3011
0
  guard_pathbias_t *pb = tor_memdup(&guard->pb, sizeof(*pb));
3012
0
  pb->use_successes = pathbias_get_use_success_count(guard);
3013
0
  pb->successful_circuits_closed = pathbias_get_close_success_count(guard);
3014
3015
0
  #define PB_FIELD(field) do {                                          \
3016
0
      if (pb->field >= EPSILON) {                                       \
3017
0
        smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field);  \
3018
0
      }                                                                 \
3019
0
    } while (0)
3020
0
  PB_FIELD(use_attempts);
3021
0
  PB_FIELD(use_successes);
3022
0
  PB_FIELD(circ_attempts);
3023
0
  PB_FIELD(circ_successes);
3024
0
  PB_FIELD(successful_circuits_closed);
3025
0
  PB_FIELD(collapsed_circuits);
3026
0
  PB_FIELD(unusable_circuits);
3027
0
  PB_FIELD(timeouts);
3028
0
  tor_free(pb);
3029
0
#undef PB_FIELD
3030
3031
0
  if (guard->extra_state_fields)
3032
0
    smartlist_add_strdup(result, guard->extra_state_fields);
3033
3034
0
  char *joined = smartlist_join_strings(result, " ", 0, NULL);
3035
0
  SMARTLIST_FOREACH(result, char *, cp, tor_free(cp));
3036
0
  smartlist_free(result);
3037
3038
0
  return joined;
3039
0
}
3040
3041
/**
3042
 * Extract key=val from the state string <b>s</b> and duplicate the value to
3043
 * some string target declared in entry_guard_parse_from_state
3044
 */
3045
static void
3046
parse_from_state_set_vals(const char *s, smartlist_t *entries, smartlist_t
3047
    *extra, strmap_t *vals)
3048
0
{
3049
0
    smartlist_split_string(entries, s, " ",
3050
0
                           SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
3051
3052
0
    SMARTLIST_FOREACH_BEGIN(entries, char *, entry) {
3053
0
      const char *eq = strchr(entry, '=');
3054
0
      if (!eq) {
3055
0
        smartlist_add(extra, entry);
3056
0
        continue;
3057
0
      }
3058
0
      char *key = tor_strndup(entry, eq-entry);
3059
0
      char **target = strmap_get(vals, key);
3060
0
      if (target == NULL || *target != NULL) {
3061
        /* unrecognized or already set */
3062
0
        smartlist_add(extra, entry);
3063
0
        tor_free(key);
3064
0
        continue;
3065
0
      }
3066
3067
0
      *target = tor_strdup(eq+1);
3068
0
      tor_free(key);
3069
0
      tor_free(entry);
3070
0
    } SMARTLIST_FOREACH_END(entry);
3071
0
}
3072
3073
/**
3074
 * Handle part of the parsing state file logic, focused on time related things
3075
 */
3076
static void
3077
parse_from_state_handle_time(entry_guard_t *guard, char *sampled_on, char
3078
    *unlisted_since, char *confirmed_on)
3079
0
{
3080
0
#define HANDLE_TIME(field) do {                                 \
3081
0
    if (field) {                                                \
3082
0
      int r = parse_iso_time_nospace(field, &field ## _time);   \
3083
0
      if (r < 0) {                                              \
3084
0
        log_warn(LD_CIRC, "Unable to parse %s %s from guard",   \
3085
0
                 #field, escaped(field));                       \
3086
0
        field##_time = -1;                                      \
3087
0
      }                                                         \
3088
0
    }                                                           \
3089
0
  } while (0)
3090
3091
0
  time_t sampled_on_time = 0;
3092
0
  time_t unlisted_since_time = 0;
3093
0
  time_t confirmed_on_time = 0;
3094
3095
0
  HANDLE_TIME(sampled_on);
3096
0
  HANDLE_TIME(unlisted_since);
3097
0
  HANDLE_TIME(confirmed_on);
3098
3099
0
  if (sampled_on_time <= 0)
3100
0
    sampled_on_time = approx_time();
3101
0
  if (unlisted_since_time < 0)
3102
0
    unlisted_since_time = 0;
3103
0
  if (confirmed_on_time < 0)
3104
0
    confirmed_on_time = 0;
3105
3106
0
  #undef HANDLE_TIME
3107
3108
0
  guard->sampled_on_date = sampled_on_time;
3109
0
  guard->unlisted_since_date = unlisted_since_time;
3110
0
  guard->confirmed_on_date = confirmed_on_time;
3111
0
}
3112
3113
/**
3114
 * Given a string generated by entry_guard_encode_for_state(), parse it
3115
 * (if possible) and return an entry_guard_t object for it.  Return NULL
3116
 * on complete failure.
3117
 */
3118
STATIC entry_guard_t *
3119
entry_guard_parse_from_state(const char *s)
3120
0
{
3121
  /* Unrecognized entries get put in here. */
3122
0
  smartlist_t *extra = smartlist_new();
3123
3124
  /* These fields get parsed from the string. */
3125
0
  char *in = NULL;
3126
0
  char *rsa_id = NULL;
3127
0
  char *nickname = NULL;
3128
0
  char *sampled_on = NULL;
3129
0
  char *sampled_idx = NULL;
3130
0
  char *sampled_by = NULL;
3131
0
  char *unlisted_since = NULL;
3132
0
  char *listed  = NULL;
3133
0
  char *confirmed_on = NULL;
3134
0
  char *confirmed_idx = NULL;
3135
0
  char *bridge_addr = NULL;
3136
3137
  // pathbias
3138
0
  char *pb_use_attempts = NULL;
3139
0
  char *pb_use_successes = NULL;
3140
0
  char *pb_circ_attempts = NULL;
3141
0
  char *pb_circ_successes = NULL;
3142
0
  char *pb_successful_circuits_closed = NULL;
3143
0
  char *pb_collapsed_circuits = NULL;
3144
0
  char *pb_unusable_circuits = NULL;
3145
0
  char *pb_timeouts = NULL;
3146
0
  int invalid_sampled_idx = get_max_sample_size_absolute();
3147
3148
  /* Split up the entries.  Put the ones we know about in strings and the
3149
   * rest in "extra". */
3150
0
  {
3151
0
    smartlist_t *entries = smartlist_new();
3152
3153
0
    strmap_t *vals = strmap_new(); // Maps keyword to location
3154
0
#define FIELD(f) \
3155
0
    strmap_set(vals, #f, &f);
3156
0
    FIELD(in);
3157
0
    FIELD(rsa_id);
3158
0
    FIELD(nickname);
3159
0
    FIELD(sampled_on);
3160
0
    FIELD(sampled_idx);
3161
0
    FIELD(sampled_by);
3162
0
    FIELD(unlisted_since);
3163
0
    FIELD(listed);
3164
0
    FIELD(confirmed_on);
3165
0
    FIELD(confirmed_idx);
3166
0
    FIELD(bridge_addr);
3167
0
    FIELD(pb_use_attempts);
3168
0
    FIELD(pb_use_successes);
3169
0
    FIELD(pb_circ_attempts);
3170
0
    FIELD(pb_circ_successes);
3171
0
    FIELD(pb_successful_circuits_closed);
3172
0
    FIELD(pb_collapsed_circuits);
3173
0
    FIELD(pb_unusable_circuits);
3174
0
    FIELD(pb_timeouts);
3175
0
#undef FIELD
3176
    /* Extract from s the key=val that we recognize, put the others in extra*/
3177
0
    parse_from_state_set_vals(s, entries, extra, vals);
3178
3179
0
    smartlist_free(entries);
3180
0
    strmap_free(vals, NULL);
3181
0
  }
3182
3183
0
  entry_guard_t *guard = tor_malloc_zero(sizeof(entry_guard_t));
3184
0
  guard->is_persistent = 1;
3185
3186
0
  if (in == NULL) {
3187
0
    log_warn(LD_CIRC, "Guard missing 'in' field");
3188
0
    goto err;
3189
0
  }
3190
3191
0
  guard->selection_name = in;
3192
0
  in = NULL;
3193
3194
0
  if (rsa_id == NULL) {
3195
0
    log_warn(LD_CIRC, "Guard missing RSA ID field");
3196
0
    goto err;
3197
0
  }
3198
3199
  /* Process the identity and nickname. */
3200
0
  if (base16_decode(guard->identity, sizeof(guard->identity),
3201
0
                    rsa_id, strlen(rsa_id)) != DIGEST_LEN) {
3202
0
    log_warn(LD_CIRC, "Unable to decode guard identity %s", escaped(rsa_id));
3203
0
    goto err;
3204
0
  }
3205
3206
0
  if (nickname) {
3207
0
    strlcpy(guard->nickname, nickname, sizeof(guard->nickname));
3208
0
  } else {
3209
0
    guard->nickname[0]='$';
3210
0
    base16_encode(guard->nickname+1, sizeof(guard->nickname)-1,
3211
0
                  guard->identity, DIGEST_LEN);
3212
0
  }
3213
3214
0
  if (bridge_addr) {
3215
0
    tor_addr_port_t res;
3216
0
    memset(&res, 0, sizeof(res));
3217
0
    int r = tor_addr_port_parse(LOG_WARN, bridge_addr,
3218
0
                                &res.addr, &res.port, -1);
3219
0
    if (r == 0)
3220
0
      guard->bridge_addr = tor_memdup(&res, sizeof(res));
3221
    /* On error, we already warned. */
3222
0
  }
3223
3224
  /* Process the various time fields. */
3225
0
  parse_from_state_handle_time(guard, sampled_on, unlisted_since,
3226
0
      confirmed_on);
3227
3228
  /* Take sampled_by_version verbatim. */
3229
0
  guard->sampled_by_version = sampled_by;
3230
0
  sampled_by = NULL; /* prevent free */
3231
  /* Listed is a boolean */
3232
0
  if (listed && strcmp(listed, "0"))
3233
0
    guard->currently_listed = 1;
3234
3235
  /* The index is a nonnegative integer. */
3236
0
  guard->confirmed_idx = -1;
3237
0
  if (confirmed_idx) {
3238
0
    int ok=1;
3239
0
    long idx = tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
3240
0
    if (! ok) {
3241
0
      log_warn(LD_GUARD, "Guard has invalid confirmed_idx %s",
3242
0
               escaped(confirmed_idx));
3243
0
    } else {
3244
0
      guard->confirmed_idx = (int)idx;
3245
0
    }
3246
0
  }
3247
3248
0
  if (sampled_idx) {
3249
0
    int ok = 1;
3250
0
    long idx = tor_parse_long(sampled_idx, 10, 0, INT_MAX, &ok, NULL);
3251
0
    if (!ok) {
3252
0
      log_warn(LD_GUARD, "Guard has invalid sampled_idx %s",
3253
0
          escaped(sampled_idx));
3254
      /* set it to a idx higher than the max sample size */
3255
0
      guard->sampled_idx = invalid_sampled_idx++;
3256
0
    } else {
3257
0
      guard->sampled_idx = (int)idx;
3258
0
    }
3259
0
  } else if (confirmed_idx) {
3260
    /* This state has been written by an older Tor version which did not have
3261
     * sample ordering  */
3262
3263
0
    guard->sampled_idx = guard->confirmed_idx;
3264
0
  } else {
3265
0
    log_info(LD_GUARD, "The state file seems to be into a status that could"
3266
0
             " yield to weird entry node selection: we're missing both a"
3267
0
             " sampled_idx and a confirmed_idx.");
3268
0
    guard->sampled_idx = invalid_sampled_idx++;
3269
0
  }
3270
3271
  /* Anything we didn't recognize gets crammed together */
3272
0
  if (smartlist_len(extra) > 0) {
3273
0
    guard->extra_state_fields = smartlist_join_strings(extra, " ", 0, NULL);
3274
0
  }
3275
3276
  /* initialize non-persistent fields */
3277
0
  guard->is_reachable = GUARD_REACHABLE_MAYBE;
3278
3279
0
#define PB_FIELD(field)                                                 \
3280
0
  do {                                                                  \
3281
0
    if (pb_ ## field) {                                                 \
3282
0
      int ok = 1;                                                       \
3283
0
      double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL);   \
3284
0
      if (! ok) {                                                       \
3285
0
        log_warn(LD_CIRC, "Guard has invalid pb_%s %s",                 \
3286
0
                 #field, pb_ ## field);                                 \
3287
0
      } else {                                                          \
3288
0
        guard->pb.field = r;                                            \
3289
0
      }                                                                 \
3290
0
    }                                                                   \
3291
0
  } while (0)
3292
0
  PB_FIELD(use_attempts);
3293
0
  PB_FIELD(use_successes);
3294
0
  PB_FIELD(circ_attempts);
3295
0
  PB_FIELD(circ_successes);
3296
0
  PB_FIELD(successful_circuits_closed);
3297
0
  PB_FIELD(collapsed_circuits);
3298
0
  PB_FIELD(unusable_circuits);
3299
0
  PB_FIELD(timeouts);
3300
0
#undef PB_FIELD
3301
3302
0
  pathbias_check_use_success_count(guard);
3303
0
  pathbias_check_close_success_count(guard);
3304
3305
  /* We update everything on this guard later, after we've parsed
3306
   * everything.  */
3307
3308
0
  goto done;
3309
3310
0
 err:
3311
  // only consider it an error if the guard state was totally unparseable.
3312
0
  entry_guard_free(guard);
3313
0
  guard = NULL;
3314
3315
0
 done:
3316
0
  tor_free(in);
3317
0
  tor_free(rsa_id);
3318
0
  tor_free(nickname);
3319
0
  tor_free(sampled_on);
3320
0
  tor_free(sampled_by);
3321
0
  tor_free(unlisted_since);
3322
0
  tor_free(listed);
3323
0
  tor_free(confirmed_on);
3324
0
  tor_free(confirmed_idx);
3325
0
  tor_free(sampled_idx);
3326
0
  tor_free(bridge_addr);
3327
0
  tor_free(pb_use_attempts);
3328
0
  tor_free(pb_use_successes);
3329
0
  tor_free(pb_circ_attempts);
3330
0
  tor_free(pb_circ_successes);
3331
0
  tor_free(pb_successful_circuits_closed);
3332
0
  tor_free(pb_collapsed_circuits);
3333
0
  tor_free(pb_unusable_circuits);
3334
0
  tor_free(pb_timeouts);
3335
3336
0
  SMARTLIST_FOREACH(extra, char *, cp, tor_free(cp));
3337
0
  smartlist_free(extra);
3338
3339
0
  return guard;
3340
0
}
3341
3342
/**
3343
 * Replace the Guards entries in <b>state</b> with a list of all our sampled
3344
 * guards.
3345
 */
3346
static void
3347
entry_guards_update_guards_in_state(or_state_t *state)
3348
0
{
3349
0
  if (!guard_contexts)
3350
0
    return;
3351
0
  config_line_t *lines = NULL;
3352
0
  config_line_t **nextline = &lines;
3353
3354
0
  SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
3355
0
    int i = 0;
3356
0
    SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
3357
0
      if (guard->is_persistent == 0)
3358
0
        continue;
3359
0
      *nextline = tor_malloc_zero(sizeof(config_line_t));
3360
0
      (*nextline)->key = tor_strdup("Guard");
3361
0
      (*nextline)->value = entry_guard_encode_for_state(guard, i);
3362
0
      nextline = &(*nextline)->next;
3363
0
      i++;
3364
0
    } SMARTLIST_FOREACH_END(guard);
3365
0
  } SMARTLIST_FOREACH_END(gs);
3366
3367
0
  config_free_lines(state->Guard);
3368
0
  state->Guard = lines;
3369
0
}
3370
3371
/**
3372
 * Replace our sampled guards from the Guards entries in <b>state</b>. Return 0
3373
 * on success, -1 on failure. (If <b>set</b> is true, replace nothing -- only
3374
 * check whether replacing would work.)
3375
 */
3376
static int
3377
entry_guards_load_guards_from_state(or_state_t *state, int set)
3378
0
{
3379
0
  const config_line_t *line = state->Guard;
3380
0
  int n_errors = 0;
3381
3382
0
  if (!guard_contexts)
3383
0
    guard_contexts = smartlist_new();
3384
3385
  /* Wipe all our existing guard info. (we shouldn't have any, but
3386
   * let's be safe.) */
3387
0
  if (set) {
3388
0
    SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
3389
0
      guard_selection_free(gs);
3390
0
      if (curr_guard_context == gs)
3391
0
        curr_guard_context = NULL;
3392
0
      SMARTLIST_DEL_CURRENT(guard_contexts, gs);
3393
0
    } SMARTLIST_FOREACH_END(gs);
3394
0
  }
3395
3396
0
  for ( ; line != NULL; line = line->next) {
3397
0
    entry_guard_t *guard = entry_guard_parse_from_state(line->value);
3398
0
    if (guard == NULL) {
3399
0
      ++n_errors;
3400
0
      continue;
3401
0
    }
3402
0
    tor_assert(guard->selection_name);
3403
0
    if (!strcmp(guard->selection_name, "legacy")) {
3404
0
      ++n_errors;
3405
0
      entry_guard_free(guard);
3406
0
      continue;
3407
0
    }
3408
3409
0
    if (set) {
3410
0
      guard_selection_t *gs;
3411
0
      gs = get_guard_selection_by_name(guard->selection_name,
3412
0
                                       GS_TYPE_INFER, 1);
3413
0
      tor_assert(gs);
3414
0
      smartlist_add(gs->sampled_entry_guards, guard);
3415
0
      guard->in_selection = gs;
3416
      /* Recompute the next_sampled_id from the state. We do not assume that
3417
       * sampled guards appear in the correct order within the file, and we
3418
       * need to know what would be the next sampled idx to give to any
3419
       * new sampled guard (i.e., max of guard->sampled_idx + 1)*/
3420
0
      if (gs->next_sampled_idx <= guard->sampled_idx) {
3421
0
        gs->next_sampled_idx = guard->sampled_idx + 1;
3422
0
      }
3423
3424
0
    } else {
3425
0
      entry_guard_free(guard);
3426
0
    }
3427
0
  }
3428
3429
0
  if (set) {
3430
0
    SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
3431
      /** Guards should be in sample order within the file, but it is maybe
3432
       * better NOT to assume that. Let's order them before updating lists
3433
       */
3434
0
      smartlist_sort(gs->sampled_entry_guards, compare_guards_by_sampled_idx);
3435
0
      entry_guards_update_all(gs);
3436
0
    } SMARTLIST_FOREACH_END(gs);
3437
0
  }
3438
0
  return n_errors ? -1 : 0;
3439
0
}
3440
3441
/** If <b>digest</b> matches the identity of any node in the
3442
 * entry_guards list for the provided guard selection state,
3443
 return that node. Else return NULL. */
3444
entry_guard_t *
3445
entry_guard_get_by_id_digest_for_guard_selection(guard_selection_t *gs,
3446
                                                 const char *digest)
3447
0
{
3448
0
  return get_sampled_guard_with_id(gs, (const uint8_t*)digest);
3449
0
}
3450
3451
/** Return the node_t associated with a single entry_guard_t. May
3452
 * return NULL if the guard is not currently in the consensus. */
3453
const node_t *
3454
entry_guard_find_node(const entry_guard_t *guard)
3455
0
{
3456
0
  tor_assert(guard);
3457
0
  return node_get_by_id(guard->identity);
3458
0
}
3459
3460
/** If <b>digest</b> matches the identity of any node in the
3461
 * entry_guards list for the default guard selection state,
3462
 return that node. Else return NULL. */
3463
entry_guard_t *
3464
entry_guard_get_by_id_digest(const char *digest)
3465
0
{
3466
0
  return entry_guard_get_by_id_digest_for_guard_selection(
3467
0
      get_guard_selection_info(), digest);
3468
0
}
3469
3470
/** We are about to connect to bridge with identity <b>digest</b> to fetch its
3471
 *  descriptor. Create a new guard state for this connection and return it. */
3472
circuit_guard_state_t *
3473
get_guard_state_for_bridge_desc_fetch(const char *digest)
3474
0
{
3475
0
  circuit_guard_state_t *guard_state = NULL;
3476
0
  entry_guard_t *guard = NULL;
3477
3478
0
  guard = entry_guard_get_by_id_digest_for_guard_selection(
3479
0
                                    get_guard_selection_info(), digest);
3480
0
  if (!guard) {
3481
0
    return NULL;
3482
0
  }
3483
3484
  /* Update the guard last_tried_to_connect time since it's checked by the
3485
   * guard subsystem. */
3486
0
  guard->last_tried_to_connect = approx_time();
3487
3488
  /* Create the guard state */
3489
0
  guard_state = circuit_guard_state_new(guard,
3490
0
                                        GUARD_CIRC_STATE_USABLE_ON_COMPLETION,
3491
0
                                        NULL);
3492
3493
0
  return guard_state;
3494
0
}
3495
3496
/** Release all storage held by <b>e</b>. */
3497
STATIC void
3498
entry_guard_free_(entry_guard_t *e)
3499
0
{
3500
0
  if (!e)
3501
0
    return;
3502
0
  entry_guard_handles_clear(e);
3503
0
  tor_free(e->sampled_by_version);
3504
0
  tor_free(e->extra_state_fields);
3505
0
  tor_free(e->selection_name);
3506
0
  tor_free(e->bridge_addr);
3507
0
  tor_free(e);
3508
0
}
3509
3510
/** Return 0 if we're fine adding arbitrary routers out of the
3511
 * directory to our entry guard list, or return 1 if we have a
3512
 * list already and we must stick to it.
3513
 */
3514
int
3515
entry_list_is_constrained(const or_options_t *options)
3516
0
{
3517
  // XXXX #21425 look at the current selection.
3518
0
  if (options->EntryNodes)
3519
0
    return 1;
3520
0
  if (options->UseBridges)
3521
0
    return 1;
3522
0
  return 0;
3523
0
}
3524
3525
/** Return the number of bridges that have descriptors that are marked with
3526
 * purpose 'bridge' and are running. If use_maybe_reachable is
3527
 * true, include bridges that might be reachable in the count.
3528
 * Otherwise, if it is false, only include bridges that have recently been
3529
 * found running in the count.
3530
 *
3531
 * We use this function to decide if we're ready to start building
3532
 * circuits through our bridges, or if we need to wait until the
3533
 * directory "server/authority" requests finish. */
3534
MOCK_IMPL(int,
3535
num_bridges_usable,(int use_maybe_reachable))
3536
0
{
3537
0
  int n_options = 0;
3538
3539
0
  if (BUG(!get_options()->UseBridges)) {
3540
0
    return 0;
3541
0
  }
3542
0
  guard_selection_t *gs  = get_guard_selection_info();
3543
0
  if (BUG(gs->type != GS_TYPE_BRIDGE)) {
3544
0
    return 0;
3545
0
  }
3546
3547
0
  SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
3548
    /* Not a bridge, or not one we are configured to be able to use. */
3549
0
    if (! guard->is_filtered_guard)
3550
0
      continue;
3551
    /* Definitely not usable */
3552
0
    if (guard->is_reachable == GUARD_REACHABLE_NO)
3553
0
      continue;
3554
    /* If we want to be really sure the bridges will work, skip maybes */
3555
0
    if (!use_maybe_reachable && guard->is_reachable == GUARD_REACHABLE_MAYBE)
3556
0
      continue;
3557
0
    if (tor_digest_is_zero(guard->identity))
3558
0
      continue;
3559
0
    const node_t *node = node_get_by_id(guard->identity);
3560
0
    if (node && node->ri)
3561
0
      ++n_options;
3562
0
  } SMARTLIST_FOREACH_END(guard);
3563
3564
0
  return n_options;
3565
0
}
3566
3567
/** Check the pathbias use success count of <b>node</b> and disable it if it
3568
 *  goes over our thresholds. */
3569
static void
3570
pathbias_check_use_success_count(entry_guard_t *node)
3571
0
{
3572
0
  const or_options_t *options = get_options();
3573
0
  const double EPSILON = 1.0e-9;
3574
3575
  /* Note: We rely on the < comparison here to allow us to set a 0
3576
   * rate and disable the feature entirely. If refactoring, don't
3577
   * change to <= */
3578
0
  if (node->pb.use_attempts > EPSILON &&
3579
0
      pathbias_get_use_success_count(node)/node->pb.use_attempts
3580
0
      < pathbias_get_extreme_use_rate(options) &&
3581
0
      pathbias_get_dropguards(options)) {
3582
0
    node->pb.path_bias_disabled = 1;
3583
0
    log_info(LD_GENERAL,
3584
0
             "Path use bias is too high (%f/%f); disabling node %s",
3585
0
             node->pb.circ_successes, node->pb.circ_attempts,
3586
0
             node->nickname);
3587
0
  }
3588
0
}
3589
3590
/** Check the pathbias close count of <b>node</b> and disable it if it goes
3591
 *  over our thresholds. */
3592
static void
3593
pathbias_check_close_success_count(entry_guard_t *node)
3594
0
{
3595
0
  const or_options_t *options = get_options();
3596
0
  const double EPSILON = 1.0e-9;
3597
3598
  /* Note: We rely on the < comparison here to allow us to set a 0
3599
   * rate and disable the feature entirely. If refactoring, don't
3600
   * change to <= */
3601
0
  if (node->pb.circ_attempts > EPSILON &&
3602
0
      pathbias_get_close_success_count(node)/node->pb.circ_attempts
3603
0
      < pathbias_get_extreme_rate(options) &&
3604
0
      pathbias_get_dropguards(options)) {
3605
0
    node->pb.path_bias_disabled = 1;
3606
0
    log_info(LD_GENERAL,
3607
0
             "Path bias is too high (%f/%f); disabling node %s",
3608
0
             node->pb.circ_successes, node->pb.circ_attempts,
3609
0
             node->nickname);
3610
0
  }
3611
0
}
3612
3613
/** Parse <b>state</b> and learn about the entry guards it describes.
3614
 * If <b>set</b> is true, and there are no errors, replace the guard
3615
 * list in the default guard selection context with what we find.
3616
 * On success, return 0. On failure, alloc into *<b>msg</b> a string
3617
 * describing the error, and return -1.
3618
 */
3619
int
3620
entry_guards_parse_state(or_state_t *state, int set, char **msg)
3621
0
{
3622
0
  entry_guards_dirty = 0;
3623
0
  int r1 = entry_guards_load_guards_from_state(state, set);
3624
0
  entry_guards_dirty = 0;
3625
3626
0
  if (r1 < 0) {
3627
0
    if (msg && *msg == NULL) {
3628
0
      *msg = tor_strdup("parsing error");
3629
0
    }
3630
0
    return -1;
3631
0
  }
3632
0
  return 0;
3633
0
}
3634
3635
/** How long will we let a change in our guard nodes stay un-saved
3636
 * when we are trying to avoid disk writes? */
3637
0
#define SLOW_GUARD_STATE_FLUSH_TIME 600
3638
/** How long will we let a change in our guard nodes stay un-saved
3639
 * when we are not trying to avoid disk writes? */
3640
0
#define FAST_GUARD_STATE_FLUSH_TIME 30
3641
3642
/** Our list of entry guards has changed for a particular guard selection
3643
 * context, or some element of one of our entry guards has changed for one.
3644
 * Write the changes to disk within the next few minutes.
3645
 */
3646
void
3647
entry_guards_changed_for_guard_selection(guard_selection_t *gs)
3648
0
{
3649
0
  time_t when;
3650
3651
0
  tor_assert(gs != NULL);
3652
3653
0
  entry_guards_dirty = 1;
3654
3655
0
  if (get_options()->AvoidDiskWrites)
3656
0
    when = time(NULL) + SLOW_GUARD_STATE_FLUSH_TIME;
3657
0
  else
3658
0
    when = time(NULL) + FAST_GUARD_STATE_FLUSH_TIME;
3659
3660
  /* or_state_save() will call entry_guards_update_state() and
3661
     entry_guards_update_guards_in_state()
3662
  */
3663
0
  or_state_mark_dirty(get_or_state(), when);
3664
3665
  /* Schedule a re-assessment of whether we have enough dir info to
3666
   * use the network. When we add or remove or disable or enable a
3667
   * guard, the decision could shift. */
3668
0
  router_dir_info_changed();
3669
0
}
3670
3671
/** Our list of entry guards has changed for the default guard selection
3672
 * context, or some element of one of our entry guards has changed. Write
3673
 * the changes to disk within the next few minutes.
3674
 */
3675
void
3676
entry_guards_changed(void)
3677
0
{
3678
0
  entry_guards_changed_for_guard_selection(get_guard_selection_info());
3679
0
}
3680
3681
/** If the entry guard info has not changed, do nothing and return.
3682
 * Otherwise, free the EntryGuards piece of <b>state</b> and create
3683
 * a new one out of the global entry_guards list, and then mark
3684
 * <b>state</b> dirty so it will get saved to disk.
3685
 */
3686
void
3687
entry_guards_update_state(or_state_t *state)
3688
0
{
3689
0
  entry_guards_dirty = 0;
3690
3691
  // Handles all guard info.
3692
0
  entry_guards_update_guards_in_state(state);
3693
3694
0
  entry_guards_dirty = 0;
3695
3696
0
  if (!get_options()->AvoidDiskWrites)
3697
0
    or_state_mark_dirty(get_or_state(), 0);
3698
0
  entry_guards_dirty = 0;
3699
0
}
3700
3701
/** Return true iff the circuit's guard can succeed, that is, can be used. */
3702
int
3703
entry_guard_could_succeed(const circuit_guard_state_t *guard_state)
3704
0
{
3705
0
  if (get_options()->UseEntryGuards == 0) {
3706
    /* we're fine with this circuit's first hop, because we're not
3707
     * configured to use entry guards. */
3708
0
    return 1;
3709
0
  }
3710
3711
0
  if (!guard_state) {
3712
0
    return 0;
3713
0
  }
3714
3715
0
  entry_guard_t *guard = entry_guard_handle_get(guard_state->guard);
3716
0
  if (!guard || BUG(guard->in_selection == NULL)) {
3717
0
    return 0;
3718
0
  }
3719
3720
0
  return 1;
3721
0
}
3722
3723
/**
3724
 * Format a single entry guard in the format expected by the controller.
3725
 * Return a newly allocated string.
3726
 */
3727
STATIC char *
3728
getinfo_helper_format_single_entry_guard(const entry_guard_t *e)
3729
0
{
3730
0
  const char *status = NULL;
3731
0
  time_t when = 0;
3732
0
  const node_t *node;
3733
0
  char tbuf[ISO_TIME_LEN+1];
3734
0
  char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
3735
3736
  /* This is going to be a bit tricky, since the status
3737
   * codes weren't really intended for prop271 guards.
3738
   *
3739
   * XXXX use a more appropriate format for exporting this information
3740
   */
3741
0
  if (e->confirmed_idx < 0) {
3742
0
    status = "never-connected";
3743
0
  } else if (! e->currently_listed) {
3744
0
    when = e->unlisted_since_date;
3745
0
    status = "unusable";
3746
0
  } else if (! e->is_filtered_guard) {
3747
0
    status = "unusable";
3748
0
  } else if (e->is_reachable == GUARD_REACHABLE_NO) {
3749
0
    when = e->failing_since;
3750
0
    status = "down";
3751
0
  } else {
3752
0
    status = "up";
3753
0
  }
3754
3755
0
  node = entry_guard_find_node(e);
3756
0
  if (node) {
3757
0
    node_get_verbose_nickname(node, nbuf);
3758
0
  } else {
3759
0
    nbuf[0] = '$';
3760
0
    base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
3761
    /* e->nickname field is not very reliable if we don't know about
3762
     * this router any longer; don't include it. */
3763
0
  }
3764
3765
0
  char *result = NULL;
3766
0
  if (when) {
3767
0
    format_iso_time(tbuf, when);
3768
0
    tor_asprintf(&result, "%s %s %s\n", nbuf, status, tbuf);
3769
0
  } else {
3770
0
    tor_asprintf(&result, "%s %s\n", nbuf, status);
3771
0
  }
3772
0
  return result;
3773
0
}
3774
3775
/** If <b>question</b> is the string "entry-guards", then dump
3776
 * to *<b>answer</b> a newly allocated string describing all of
3777
 * the nodes in the global entry_guards list. See control-spec.txt
3778
 * for details.
3779
 * For backward compatibility, we also handle the string "helper-nodes".
3780
 *
3781
 * XXX this should be totally redesigned after prop 271 too, and that's
3782
 * going to take some control spec work.
3783
 * */
3784
int
3785
getinfo_helper_entry_guards(control_connection_t *conn,
3786
                            const char *question, char **answer,
3787
                            const char **errmsg)
3788
0
{
3789
0
  guard_selection_t *gs = get_guard_selection_info();
3790
3791
0
  tor_assert(gs != NULL);
3792
3793
0
  (void) conn;
3794
0
  (void) errmsg;
3795
3796
0
  if (!strcmp(question,"entry-guards") ||
3797
0
      !strcmp(question,"helper-nodes")) {
3798
0
    const smartlist_t *guards;
3799
0
    guards = gs->sampled_entry_guards;
3800
3801
0
    smartlist_t *sl = smartlist_new();
3802
3803
0
    SMARTLIST_FOREACH_BEGIN(guards, const entry_guard_t *, e) {
3804
0
      char *cp = getinfo_helper_format_single_entry_guard(e);
3805
0
      smartlist_add(sl, cp);
3806
0
    } SMARTLIST_FOREACH_END(e);
3807
0
    *answer = smartlist_join_strings(sl, "", 0, NULL);
3808
0
    SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
3809
0
    smartlist_free(sl);
3810
0
  }
3811
0
  return 0;
3812
0
}
3813
3814
/* Given the original bandwidth of a guard and its guardfraction,
3815
 * calculate how much bandwidth the guard should have as a guard and
3816
 * as a non-guard.
3817
 *
3818
 * Quoting from proposal236:
3819
 *
3820
 *   Let Wpf denote the weight from the 'bandwidth-weights' line a
3821
 *   client would apply to N for position p if it had the guard
3822
 *   flag, Wpn the weight if it did not have the guard flag, and B the
3823
 *   measured bandwidth of N in the consensus.  Then instead of choosing
3824
 *   N for position p proportionally to Wpf*B or Wpn*B, clients should
3825
 *   choose N proportionally to F*Wpf*B + (1-F)*Wpn*B.
3826
 *
3827
 * This function fills the <b>guardfraction_bw</b> structure. It sets
3828
 * <b>guard_bw</b> to F*B and <b>non_guard_bw</b> to (1-F)*B.
3829
 */
3830
void
3831
guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
3832
                                  int orig_bandwidth,
3833
                                  uint32_t guardfraction_percentage)
3834
0
{
3835
0
  double guardfraction_fraction;
3836
3837
  /* Turn the percentage into a fraction. */
3838
0
  tor_assert(guardfraction_percentage <= 100);
3839
0
  guardfraction_fraction = guardfraction_percentage / 100.0;
3840
3841
0
  long guard_bw = tor_lround(guardfraction_fraction * orig_bandwidth);
3842
0
  tor_assert(guard_bw <= INT_MAX);
3843
3844
0
  guardfraction_bw->guard_bw = (int) guard_bw;
3845
3846
0
  guardfraction_bw->non_guard_bw = orig_bandwidth - (int) guard_bw;
3847
0
}
3848
3849
/** Helper: Update the status of all entry guards, in whatever algorithm
3850
 * is used. Return true if we should stop using all previously generated
3851
 * circuits, by calling circuit_mark_all_unused_circs() and
3852
 * circuit_mark_all_dirty_circs_as_unusable().
3853
 */
3854
int
3855
guards_update_all(void)
3856
0
{
3857
0
  int mark_circuits = 0;
3858
0
  if (update_guard_selection_choice(get_options()))
3859
0
    mark_circuits = 1;
3860
3861
0
  tor_assert(curr_guard_context);
3862
3863
0
  if (entry_guards_update_all(curr_guard_context))
3864
0
    mark_circuits = 1;
3865
3866
0
  return mark_circuits;
3867
0
}
3868
3869
/** Helper: pick a guard for a circuit, with whatever algorithm is
3870
    used. */
3871
const node_t *
3872
guards_choose_guard(const origin_circuit_t *circ,
3873
                    cpath_build_state_t *state,
3874
                    uint8_t purpose,
3875
                    circuit_guard_state_t **guard_state_out)
3876
0
{
3877
0
  const node_t *r = NULL;
3878
0
  const uint8_t *exit_id = NULL;
3879
0
  entry_guard_restriction_t *rst = NULL;
3880
3881
  /* If we this is a conflux circuit, build an exclusion list for it. */
3882
0
  if (CIRCUIT_IS_CONFLUX(TO_CIRCUIT(circ)) && state
3883
0
          && (exit_id = build_state_get_exit_rsa_id(state))) {
3884
0
    rst = guard_create_conflux_restriction(circ, exit_id);
3885
    /* Don't allow connecting back to the exit if there is one */
3886
0
    if (state && (exit_id = build_state_get_exit_rsa_id(state))) {
3887
      /* add the exit_id to the excluded list */
3888
0
      smartlist_add(rst->excluded, tor_memdup(exit_id, DIGEST_LEN));
3889
0
    }
3890
0
  } else if (state && !circuit_should_use_vanguards(purpose) &&
3891
0
      (exit_id = build_state_get_exit_rsa_id(state))) {
3892
    /* We're building to a targeted exit node, so that node can't be
3893
     * chosen as our guard for this circuit, unless we're vanguards. */
3894
0
    rst = guard_create_exit_restriction(exit_id);
3895
0
    tor_assert(rst);
3896
0
  }
3897
0
  if (entry_guard_pick_for_circuit(get_guard_selection_info(),
3898
0
                                   GUARD_USAGE_TRAFFIC,
3899
0
                                   rst,
3900
0
                                   &r,
3901
0
                                   guard_state_out) < 0) {
3902
0
    tor_assert(r == NULL);
3903
0
  }
3904
0
  return r;
3905
0
}
3906
3907
/** Remove all currently listed entry guards for a given guard selection
3908
 * context.  This frees and replaces <b>gs</b>, so don't use <b>gs</b>
3909
 * after calling this function. */
3910
void
3911
remove_all_entry_guards_for_guard_selection(guard_selection_t *gs)
3912
0
{
3913
  // This function shouldn't exist. XXXX
3914
0
  tor_assert(gs != NULL);
3915
0
  char *old_name = tor_strdup(gs->name);
3916
0
  guard_selection_type_t old_type = gs->type;
3917
3918
0
  SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, entry, {
3919
0
    control_event_guard(entry->nickname, entry->identity, "DROPPED");
3920
0
  });
3921
3922
0
  if (gs == curr_guard_context) {
3923
0
    curr_guard_context = NULL;
3924
0
  }
3925
3926
0
  smartlist_remove(guard_contexts, gs);
3927
0
  guard_selection_free(gs);
3928
3929
0
  gs = get_guard_selection_by_name(old_name, old_type, 1);
3930
0
  entry_guards_changed_for_guard_selection(gs);
3931
0
  tor_free(old_name);
3932
0
}
3933
3934
/** Remove all currently listed entry guards, so new ones will be chosen.
3935
 *
3936
 * XXXX This function shouldn't exist -- it's meant to support the DROPGUARDS
3937
 * command, which is deprecated.
3938
 */
3939
void
3940
remove_all_entry_guards(void)
3941
0
{
3942
0
  remove_all_entry_guards_for_guard_selection(get_guard_selection_info());
3943
0
}
3944
3945
/** Helper: pick a directory guard, with whatever algorithm is used. */
3946
const node_t *
3947
guards_choose_dirguard(uint8_t dir_purpose,
3948
                       circuit_guard_state_t **guard_state_out)
3949
0
{
3950
0
  const node_t *r = NULL;
3951
0
  entry_guard_restriction_t *rst = NULL;
3952
3953
  /* If we are fetching microdescs, don't query outdated dirservers. */
3954
0
  if (dir_purpose == DIR_PURPOSE_FETCH_MICRODESC) {
3955
0
    rst = guard_create_dirserver_md_restriction();
3956
0
  }
3957
3958
0
  if (entry_guard_pick_for_circuit(get_guard_selection_info(),
3959
0
                                   GUARD_USAGE_DIRGUARD,
3960
0
                                   rst,
3961
0
                                   &r,
3962
0
                                   guard_state_out) < 0) {
3963
0
    tor_assert(r == NULL);
3964
0
  }
3965
0
  return r;
3966
0
}
3967
3968
/**
3969
 * If we're running with a constrained guard set, then maybe mark our guards
3970
 * usable.  Return 1 if we do; 0 if we don't.
3971
 */
3972
int
3973
guards_retry_optimistic(const or_options_t *options)
3974
0
{
3975
0
  if (! entry_list_is_constrained(options))
3976
0
    return 0;
3977
3978
0
  mark_primary_guards_maybe_reachable(get_guard_selection_info());
3979
3980
0
  return 1;
3981
0
}
3982
3983
/**
3984
 * Check if we are missing any crucial dirinfo for the guard subsystem to
3985
 * work. Return NULL if everything went well, otherwise return a newly
3986
 * allocated string with an informative error message. In the latter case, use
3987
 * the general descriptor information <b>using_mds</b>, <b>num_present</b> and
3988
 * <b>num_usable</b> to improve the error message. */
3989
char *
3990
guard_selection_get_err_str_if_dir_info_missing(guard_selection_t *gs,
3991
                                        int using_mds,
3992
                                        int num_present, int num_usable)
3993
0
{
3994
0
  if (!gs->primary_guards_up_to_date)
3995
0
    entry_guards_update_primary(gs);
3996
3997
0
  char *ret_str = NULL;
3998
0
  int n_missing_descriptors = 0;
3999
0
  int n_considered = 0;
4000
0
  int num_primary_to_check;
4001
4002
  /* We want to check for the descriptor of at least the first two primary
4003
   * guards in our list, since these are the guards that we typically use for
4004
   * circuits. */
4005
0
  num_primary_to_check = get_n_primary_guards_to_use(GUARD_USAGE_TRAFFIC);
4006
0
  num_primary_to_check++;
4007
4008
0
  SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
4009
0
    entry_guard_consider_retry(guard);
4010
0
    if (guard->is_reachable == GUARD_REACHABLE_NO)
4011
0
      continue;
4012
0
    n_considered++;
4013
0
    if (!guard_has_descriptor(guard))
4014
0
      n_missing_descriptors++;
4015
0
    if (n_considered >= num_primary_to_check)
4016
0
      break;
4017
0
  } SMARTLIST_FOREACH_END(guard);
4018
4019
  /* If we are not missing any descriptors, return NULL. */
4020
0
  if (!n_missing_descriptors) {
4021
0
    return NULL;
4022
0
  }
4023
4024
  /* otherwise return a helpful error string */
4025
0
  tor_asprintf(&ret_str, "We're missing descriptors for %d/%d of our "
4026
0
               "primary entry guards (total %sdescriptors: %d/%d). "
4027
0
               "That's ok. We will try to fetch missing descriptors soon.",
4028
0
               n_missing_descriptors, num_primary_to_check,
4029
0
               using_mds?"micro":"", num_present, num_usable);
4030
4031
0
  return ret_str;
4032
0
}
4033
4034
/** As guard_selection_have_enough_dir_info_to_build_circuits, but uses
4035
 * the default guard selection. */
4036
char *
4037
entry_guards_get_err_str_if_dir_info_missing(int using_mds,
4038
                                     int num_present, int num_usable)
4039
0
{
4040
0
  return guard_selection_get_err_str_if_dir_info_missing(
4041
0
                                                 get_guard_selection_info(),
4042
0
                                                 using_mds,
4043
0
                                                 num_present, num_usable);
4044
0
}
4045
4046
/** Free one guard selection context */
4047
STATIC void
4048
guard_selection_free_(guard_selection_t *gs)
4049
0
{
4050
0
  if (!gs) return;
4051
4052
0
  tor_free(gs->name);
4053
4054
0
  if (gs->sampled_entry_guards) {
4055
0
    SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, e,
4056
0
                      entry_guard_free(e));
4057
0
    smartlist_free(gs->sampled_entry_guards);
4058
0
    gs->sampled_entry_guards = NULL;
4059
0
  }
4060
4061
0
  smartlist_free(gs->confirmed_entry_guards);
4062
0
  smartlist_free(gs->primary_entry_guards);
4063
4064
0
  tor_free(gs);
4065
0
}
4066
4067
/**********************************************************************/
4068
4069
/** Layer2 guard subsystem (vanguards-lite) used for onion service circuits */
4070
4071
/** A simple representation of a layer2 guard. We just need its identity so
4072
 *  that we feed it into a routerset, and a sampled timestamp to do expiration
4073
 *  checks. */
4074
typedef struct layer2_guard_t {
4075
  /** Identity of the guard */
4076
  char identity[DIGEST_LEN];
4077
  /** When does this guard expire? (randomized timestamp) */
4078
  time_t expire_on_date;
4079
} layer2_guard_t;
4080
4081
#define layer2_guard_free(val) \
4082
0
  FREE_AND_NULL(layer2_guard_t, layer2_guard_free_, (val))
4083
4084
/** Return true if the vanguards-lite subsystem is enabled */
4085
bool
4086
vanguards_lite_is_enabled(void)
4087
0
{
4088
  /* First check torrc option and then maybe also the consensus parameter. */
4089
0
  const or_options_t *options = get_options();
4090
4091
  /* If the option is explicitly disabled, that's the final word here */
4092
0
  if (options->VanguardsLiteEnabled == 0) {
4093
0
    return false;
4094
0
  }
4095
4096
  /* If the option is set to auto, then check the consensus parameter */
4097
0
  if (options->VanguardsLiteEnabled == -1) {
4098
0
    return networkstatus_get_param(NULL, "vanguards-lite-enabled",
4099
0
                                   1, /* default to "on" */
4100
0
                                   0, 1);
4101
0
  }
4102
4103
  /* else it's enabled */
4104
0
  tor_assert_nonfatal(options->VanguardsLiteEnabled == 1);
4105
0
  return options->VanguardsLiteEnabled;
4106
0
}
4107
4108
static void
4109
layer2_guard_free_(layer2_guard_t *l2)
4110
0
{
4111
0
  if (!l2) {
4112
0
    return;
4113
0
  }
4114
4115
0
  tor_free(l2);
4116
0
}
4117
4118
/** Global list and routerset of L2 guards. They are both synced and they get
4119
 * updated periodically. We need both the list and the routerset: we use the
4120
 * smartlist to keep track of expiration times and the routerset is what we
4121
 * return to the users of this subsystem. */
4122
static smartlist_t *layer2_guards = NULL;
4123
static routerset_t *layer2_routerset = NULL;
4124
4125
/** Number of L2 guards */
4126
0
#define NUMBER_SECOND_GUARDS 4
4127
/** Make sure that the number of L2 guards is less than the number of
4128
 *  MAX_SANE_RESTRICTED_NODES */
4129
CTASSERT(NUMBER_SECOND_GUARDS < 20);
4130
4131
/** Lifetime of L2 guards:
4132
 *  1 to 12 days, for an average of a week using the max(x,x) distribution */
4133
0
#define MIN_SECOND_GUARD_LIFETIME (3600*24)
4134
0
#define MAX_SECOND_GUARD_LIFETIME (3600*24*12)
4135
4136
/** Return the number of guards our L2 guardset should have */
4137
static int
4138
get_number_of_layer2_hs_guards(void)
4139
0
{
4140
0
  return (int) networkstatus_get_param(NULL,
4141
0
                                        "guard-hs-l2-number",
4142
0
                                        NUMBER_SECOND_GUARDS,
4143
0
                                        1, 19);
4144
0
}
4145
4146
/** Return the minimum lifetime of L2 guards */
4147
static int
4148
get_min_lifetime_of_layer2_hs_guards(void)
4149
0
{
4150
0
  return (int) networkstatus_get_param(NULL,
4151
0
                                       "guard-hs-l2-lifetime-min",
4152
0
                                       MIN_SECOND_GUARD_LIFETIME,
4153
0
                                       1, INT32_MAX);
4154
0
}
4155
4156
/** Return the maximum lifetime of L2 guards */
4157
static int
4158
get_max_lifetime_of_layer2_hs_guards(void)
4159
0
{
4160
0
  return (int) networkstatus_get_param(NULL,
4161
0
                                        "guard-hs-l2-lifetime-max",
4162
0
                                       MAX_SECOND_GUARD_LIFETIME,
4163
0
                                       1, INT32_MAX);
4164
0
}
4165
4166
/**
4167
 * Sample and return a lifetime for an L2 guard.
4168
 *
4169
 * Lifetime randomized uniformly between min and max consensus params.
4170
 */
4171
static int
4172
get_layer2_hs_guard_lifetime(void)
4173
0
{
4174
0
  int min = get_min_lifetime_of_layer2_hs_guards();
4175
0
  int max = get_max_lifetime_of_layer2_hs_guards();
4176
4177
0
  if (BUG(min >= max)) {
4178
0
    return min;
4179
0
  }
4180
4181
0
  return crypto_rand_int_range(min, max);
4182
0
}
4183
4184
/** Maintain the L2 guard list. Make sure the list contains enough guards, do
4185
 *  expirations as necessary, and keep all the data structures of this
4186
 *  subsystem synchronized */
4187
void
4188
maintain_layer2_guards(void)
4189
0
{
4190
0
  if (!router_have_minimum_dir_info()) {
4191
0
    return;
4192
0
  }
4193
4194
  /* Create the list if it doesn't exist */
4195
0
  if (!layer2_guards) {
4196
0
    layer2_guards = smartlist_new();
4197
0
  }
4198
4199
  /* Go through the list and perform any needed expirations */
4200
0
  SMARTLIST_FOREACH_BEGIN(layer2_guards, layer2_guard_t *, g) {
4201
    /* Expire based on expiration date */
4202
0
    if (g->expire_on_date <= approx_time()) {
4203
0
      log_info(LD_GENERAL, "Removing expired Layer2 guard %s",
4204
0
               safe_str_client(hex_str(g->identity, DIGEST_LEN)));
4205
      // Nickname may be gone from consensus and doesn't matter anyway
4206
0
      control_event_guard("None", g->identity, "BAD_L2");
4207
0
      layer2_guard_free(g);
4208
0
      SMARTLIST_DEL_CURRENT_KEEPORDER(layer2_guards, g);
4209
0
      continue;
4210
0
    }
4211
4212
    /* Expire if relay has left consensus */
4213
0
    const routerstatus_t *rs = router_get_consensus_status_by_id(g->identity);
4214
0
    if (rs == NULL || !rs->is_stable || !rs->is_fast) {
4215
0
      log_info(LD_GENERAL, "Removing %s Layer2 guard %s",
4216
0
               rs ? "unsuitable" : "missing",
4217
0
               safe_str_client(hex_str(g->identity, DIGEST_LEN)));
4218
      // Nickname may be gone from consensus and doesn't matter anyway
4219
0
      control_event_guard("None", g->identity, "BAD_L2");
4220
0
      layer2_guard_free(g);
4221
0
      SMARTLIST_DEL_CURRENT_KEEPORDER(layer2_guards, g);
4222
0
      continue;
4223
0
    }
4224
0
  } SMARTLIST_FOREACH_END(g);
4225
4226
  /* Find out how many guards we need to add */
4227
0
  int new_guards_needed_n =
4228
0
    get_number_of_layer2_hs_guards() - smartlist_len(layer2_guards);
4229
0
  if (new_guards_needed_n <= 0) {
4230
0
    return;
4231
0
  }
4232
4233
0
  log_info(LD_GENERAL, "Adding %d guards to Layer2 routerset",
4234
0
           new_guards_needed_n);
4235
4236
  /* First gather the exclusions based on our current L2 guards */
4237
0
  smartlist_t *excluded = smartlist_new();
4238
0
  SMARTLIST_FOREACH_BEGIN(layer2_guards, layer2_guard_t *, g) {
4239
    /* Exclude existing L2 guard so that we don't double-pick it.
4240
     * But, it's ok if they come from the same family. */
4241
0
    const node_t *existing = node_get_by_id(g->identity);
4242
0
    if (existing)
4243
0
      smartlist_add(excluded, (node_t *)existing);
4244
0
  } SMARTLIST_FOREACH_END(g);
4245
4246
  /* Add required guards to the list */
4247
0
  for (int i = 0; i < new_guards_needed_n; i++) {
4248
0
    const node_t *choice = NULL;
4249
0
    const or_options_t *options = get_options();
4250
    /* Pick Stable nodes */
4251
0
    router_crn_flags_t flags = CRN_NEED_DESC|CRN_NEED_UPTIME;
4252
0
    choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
4253
0
    if (!choice) {
4254
0
      break;
4255
0
    }
4256
4257
    /* We found our node: create an L2 guard out of it */
4258
0
    layer2_guard_t *layer2_guard = tor_malloc_zero(sizeof(layer2_guard_t));
4259
0
    memcpy(layer2_guard->identity, choice->identity, DIGEST_LEN);
4260
0
    layer2_guard->expire_on_date = approx_time() +
4261
0
      get_layer2_hs_guard_lifetime();
4262
0
    smartlist_add(layer2_guards, layer2_guard);
4263
0
    log_info(LD_GENERAL, "Adding Layer2 guard %s",
4264
0
             safe_str_client(hex_str(layer2_guard->identity, DIGEST_LEN)));
4265
    // Nickname can also be None here because it is looked up later
4266
0
    control_event_guard("None", layer2_guard->identity,
4267
0
                        "GOOD_L2");
4268
    /* Exclude this node so that we don't double-pick it. (Again, coming
4269
     * from the same family is ok here.) */
4270
0
    smartlist_add(excluded, (node_t *)choice);
4271
0
  }
4272
4273
  /* Some cleanup */
4274
0
  smartlist_free(excluded);
4275
4276
  /* Now that the list is up to date, synchronize the routerset */
4277
0
  routerset_free(layer2_routerset);
4278
0
  layer2_routerset = routerset_new();
4279
4280
0
  SMARTLIST_FOREACH_BEGIN (layer2_guards, layer2_guard_t *, g) {
4281
0
    routerset_parse(layer2_routerset,
4282
0
                    hex_str(g->identity, DIGEST_LEN),
4283
0
                    "l2 guards");
4284
0
  } SMARTLIST_FOREACH_END(g);
4285
0
}
4286
4287
/**
4288
 * Reset vanguards-lite list(s).
4289
 *
4290
 * Used for SIGNAL NEWNYM.
4291
 */
4292
void
4293
purge_vanguards_lite(void)
4294
0
{
4295
0
  if (!layer2_guards)
4296
0
    return;
4297
4298
  /* Go through the list and perform any needed expirations */
4299
0
  SMARTLIST_FOREACH_BEGIN(layer2_guards, layer2_guard_t *, g) {
4300
0
    layer2_guard_free(g);
4301
0
  } SMARTLIST_FOREACH_END(g);
4302
4303
0
  smartlist_clear(layer2_guards);
4304
4305
  /* Pick new l2 guards */
4306
0
  maintain_layer2_guards();
4307
0
}
4308
4309
/** Return a routerset containing the L2 guards or NULL if it's not yet
4310
 *  initialized. Callers must not free the routerset. Designed for use in
4311
 *  pick_vanguard_middle_node() and should not be used anywhere else. Do not
4312
 *  store this pointer -- any future calls to maintain_layer2_guards() and
4313
 *  purge_vanguards_lite() can invalidate it. */
4314
const routerset_t *
4315
get_layer2_guards(void)
4316
0
{
4317
0
  if (!layer2_guards) {
4318
0
    maintain_layer2_guards();
4319
0
  }
4320
4321
0
  return layer2_routerset;
4322
0
}
4323
4324
/*****************************************************************************/
4325
4326
/** Release all storage held by the list of entry guards and related
4327
 * memory structs. */
4328
void
4329
entry_guards_free_all(void)
4330
0
{
4331
  /* Null out the default */
4332
0
  curr_guard_context = NULL;
4333
  /* Free all the guard contexts */
4334
0
  if (guard_contexts != NULL) {
4335
0
    SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
4336
0
      guard_selection_free(gs);
4337
0
    } SMARTLIST_FOREACH_END(gs);
4338
0
    smartlist_free(guard_contexts);
4339
0
    guard_contexts = NULL;
4340
0
  }
4341
0
  circuit_build_times_free_timeouts(get_circuit_build_times_mutable());
4342
4343
0
  if (!layer2_guards) {
4344
0
    return;
4345
0
  }
4346
4347
0
  SMARTLIST_FOREACH_BEGIN(layer2_guards, layer2_guard_t *, g) {
4348
0
    layer2_guard_free(g);
4349
0
  } SMARTLIST_FOREACH_END(g);
4350
4351
0
  smartlist_free(layer2_guards);
4352
0
  routerset_free(layer2_routerset);
4353
0
}