policies.c 109 KB
Newer Older
1
/* Copyright (c) 2001-2004, Roger Dingledine.
2
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
Nick Mathewson's avatar
Nick Mathewson committed
3
 * Copyright (c) 2007-2018, The Tor Project, Inc. */
4
5
6
7
8
/* See LICENSE for licensing information */

/**
 * \file policies.c
 * \brief Code to parse and use address policies and exit policies.
9
10
11
12
13
14
15
 *
 * We have two key kinds of address policy: full and compressed.  A full
 * policy is an array of accept/reject patterns, to be applied in order.
 * A short policy is simply a list of ports.  This module handles both
 * kinds, including generic functions to apply them to addresses, and
 * also including code to manage the global policies that we apply to
 * incoming and outgoing connections.
16
17
 **/

Nick Mathewson's avatar
Nick Mathewson committed
18
19
#define POLICIES_PRIVATE

20
21
22
23
24
25
26
27
28
29
30
#include "core/or/or.h"
#include "feature/client/bridges.h"
#include "app/config/config.h"
#include "feature/dircache/dirserv.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
#include "core/or/policies.h"
#include "feature/relay/router.h"
#include "feature/nodelist/routerparse.h"
#include "feature/stats/geoip.h"
31
#include "ht.h"
32
#include "lib/encoding/confline.h"
33

34
35
36
37
38
39
40
#include "core/or/addr_policy_st.h"
#include "feature/dirclient/dir_server_st.h"
#include "feature/nodelist/microdesc_st.h"
#include "feature/nodelist/node_st.h"
#include "core/or/port_cfg_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerstatus_st.h"
41

42
/** Policy that addresses for incoming SOCKS connections must match. */
43
static smartlist_t *socks_policy = NULL;
44
/** Policy that addresses for incoming directory connections must match. */
45
static smartlist_t *dir_policy = NULL;
46
47
/** Policy that addresses for incoming router descriptors must match in order
 * to be published by us. */
48
static smartlist_t *authdir_reject_policy = NULL;
49
50
/** Policy that addresses for incoming router descriptors must match in order
 * to be marked as valid in our networkstatus. */
51
static smartlist_t *authdir_invalid_policy = NULL;
52
53
/** Policy that addresses for incoming router descriptors must <b>not</b>
 * match in order to not be marked as BadExit. */
54
static smartlist_t *authdir_badexit_policy = NULL;
55
56
57

/** Parsed addr_policy_t describing which addresses we believe we can start
 * circuits at. */
58
static smartlist_t *reachable_or_addr_policy = NULL;
59
60
/** Parsed addr_policy_t describing which addresses we believe we can connect
 * to directories at. */
61
62
static smartlist_t *reachable_dir_addr_policy = NULL;

63
64
65
66
67
/** Element of an exit policy summary */
typedef struct policy_summary_item_t {
    uint16_t prt_min; /**< Lowest port number to accept/reject. */
    uint16_t prt_max; /**< Highest port number to accept/reject. */
    uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
Nick Mathewson's avatar
Nick Mathewson committed
68
                                this port range. */
69
    unsigned int accepted:1; /** Has this port already been accepted */
70
71
} policy_summary_item_t;

72
73
74
75
76
77
78
79
80
/** Private networks.  This list is used in two places, once to expand the
 *  "private" keyword when parsing our own exit policy, secondly to ignore
 *  just such networks when building exit policy summaries.  It is important
 *  that all authorities agree on that list when creating summaries, so don't
 *  just change this without a proper migration plan and a proposal and stuff.
 */
static const char *private_nets[] = {
  "0.0.0.0/8", "169.254.0.0/16",
  "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
81
82
83
84
  "[::]/8",
  "[fc00::]/7", "[fe80::]/10", "[fec0::]/10", "[ff00::]/8", "[::]/127",
  NULL
};
85

86
static int policies_parse_exit_policy_internal(
87
88
89
90
91
92
93
                                      config_line_t *cfg,
                                      smartlist_t **dest,
                                      int ipv6_exit,
                                      int rejectprivate,
                                      const smartlist_t *configured_addresses,
                                      int reject_interface_addresses,
                                      int reject_configured_port_addresses,
94
95
                                      int add_default_policy,
                                      int add_reduced_policy);
96

97
98
99
100
101
102
103
104
105
106
/** Replace all "private" entries in *<b>policy</b> with their expanded
 * equivalents. */
void
policy_expand_private(smartlist_t **policy)
{
  uint16_t port_min, port_max;

  int i;
  smartlist_t *tmp;

107
  if (!*policy) /*XXXX disallow NULL policies? */
108
109
    return;

110
  tmp = smartlist_new();
111

112
  SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
113
114
115
116
117
     if (! p->is_private) {
       smartlist_add(tmp, p);
       continue;
     }
     for (i = 0; private_nets[i]; ++i) {
118
119
120
121
       addr_policy_t newpolicy;
       memcpy(&newpolicy, p, sizeof(addr_policy_t));
       newpolicy.is_private = 0;
       newpolicy.is_canonical = 0;
122
123
       if (tor_addr_parse_mask_ports(private_nets[i], 0,
                               &newpolicy.addr,
124
                               &newpolicy.maskbits, &port_min, &port_max)<0) {
125
         tor_assert_unreached();
126
       }
127
       smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy));
128
129
     }
     addr_policy_free(p);
130
  } SMARTLIST_FOREACH_END(p);
131
132
133
134

  smartlist_free(*policy);
  *policy = tmp;
}
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/** Expand each of the AF_UNSPEC elements in *<b>policy</b> (which indicate
 * protocol-neutral wildcards) into a pair of wildcard elements: one IPv4-
 * specific and one IPv6-specific. */
void
policy_expand_unspec(smartlist_t **policy)
{
  smartlist_t *tmp;
  if (!*policy)
    return;

  tmp = smartlist_new();
  SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
    sa_family_t family = tor_addr_family(&p->addr);
    if (family == AF_INET6 || family == AF_INET || p->is_private) {
      smartlist_add(tmp, p);
    } else if (family == AF_UNSPEC) {
      addr_policy_t newpolicy_ipv4;
      addr_policy_t newpolicy_ipv6;
      memcpy(&newpolicy_ipv4, p, sizeof(addr_policy_t));
      memcpy(&newpolicy_ipv6, p, sizeof(addr_policy_t));
      newpolicy_ipv4.is_canonical = 0;
      newpolicy_ipv6.is_canonical = 0;
      if (p->maskbits != 0) {
        log_warn(LD_BUG, "AF_UNSPEC policy with maskbits==%d", p->maskbits);
        newpolicy_ipv4.maskbits = 0;
        newpolicy_ipv6.maskbits = 0;
      }
      tor_addr_from_ipv4h(&newpolicy_ipv4.addr, 0);
      tor_addr_from_ipv6_bytes(&newpolicy_ipv6.addr,
                               "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
      smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv4));
      smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv6));
      addr_policy_free(p);
    } else {
      log_warn(LD_BUG, "Funny-looking address policy with family %d", family);
      smartlist_add(tmp, p);
    }
  } SMARTLIST_FOREACH_END(p);

  smartlist_free(*policy);
  *policy = tmp;
}

179
/**
180
 * Given a linked list of config lines containing "accept[6]" and "reject[6]"
181
 * tokens, parse them and append the result to <b>dest</b>. Return -1
182
 * if any tokens are malformed (and don't append any), else return 0.
183
184
185
186
 *
 * If <b>assume_action</b> is nonnegative, then insert its action
 * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
 * action.
187
188
 */
static int
189
parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
190
191
                  int assume_action)
{
192
  smartlist_t *result;
193
  smartlist_t *entries;
194
  addr_policy_t *item;
195
  int malformed_list;
196
197
198
199
200
  int r = 0;

  if (!cfg)
    return 0;

201
202
  result = smartlist_new();
  entries = smartlist_new();
203
204
205
  for (; cfg; cfg = cfg->next) {
    smartlist_split_string(entries, cfg->value, ",",
                           SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
206
    SMARTLIST_FOREACH_BEGIN(entries, const char *, ent) {
207
      log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
208
209
210
      malformed_list = 0;
      item = router_parse_addr_policy_item_from_string(ent, assume_action,
                                                       &malformed_list);
211
212
      if (item) {
        smartlist_add(result, item);
213
214
215
216
      } else if (malformed_list) {
        /* the error is so severe the entire list should be discarded */
        log_warn(LD_CONFIG, "Malformed policy '%s'. Discarding entire policy "
                 "list.", ent);
217
        r = -1;
218
219
220
221
222
223
      } else {
        /* the error is minor: don't add the item, but keep processing the
         * rest of the policies in the list */
        log_debug(LD_CONFIG, "Ignored policy '%s' due to non-fatal error. "
                  "The remainder of the policy list will be used.",
                  ent);
224
      }
225
    } SMARTLIST_FOREACH_END(ent);
226
227
228
229
    SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
    smartlist_clear(entries);
  }
  smartlist_free(entries);
230
231
232
233
  if (r == -1) {
    addr_policy_list_free(result);
  } else {
    policy_expand_private(&result);
234
    policy_expand_unspec(&result);
235
236
237
238
239
240
241
242
243

    if (*dest) {
      smartlist_add_all(*dest, result);
      smartlist_free(result);
    } else {
      *dest = result;
    }
  }

244
245
246
247
  return r;
}

/** Helper: parse the Reachable(Dir|OR)?Addresses fields into
248
249
250
251
 * reachable_(or|dir)_addr_policy.  The options should already have
 * been validated by validate_addr_policies.
 */
static int
252
253
parse_reachable_addresses(void)
{
254
  const or_options_t *options = get_options();
255
  int ret = 0;
256
257
258
259
260
261
262
263

  if (options->ReachableDirAddresses &&
      options->ReachableORAddresses &&
      options->ReachableAddresses) {
    log_warn(LD_CONFIG,
             "Both ReachableDirAddresses and ReachableORAddresses are set. "
             "ReachableAddresses setting will be ignored.");
  }
264
  addr_policy_list_free(reachable_or_addr_policy);
265
266
267
268
269
270
271
272
273
274
275
  reachable_or_addr_policy = NULL;
  if (!options->ReachableORAddresses && options->ReachableAddresses)
    log_info(LD_CONFIG,
             "Using ReachableAddresses as ReachableORAddresses.");
  if (parse_addr_policy(options->ReachableORAddresses ?
                          options->ReachableORAddresses :
                          options->ReachableAddresses,
                        &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
    log_warn(LD_CONFIG,
             "Error parsing Reachable%sAddresses entry; ignoring.",
             options->ReachableORAddresses ? "OR" : "");
276
    ret = -1;
277
278
  }

279
  addr_policy_list_free(reachable_dir_addr_policy);
280
281
282
283
284
285
286
287
288
289
290
  reachable_dir_addr_policy = NULL;
  if (!options->ReachableDirAddresses && options->ReachableAddresses)
    log_info(LD_CONFIG,
             "Using ReachableAddresses as ReachableDirAddresses");
  if (parse_addr_policy(options->ReachableDirAddresses ?
                          options->ReachableDirAddresses :
                          options->ReachableAddresses,
                        &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
    if (options->ReachableDirAddresses)
      log_warn(LD_CONFIG,
               "Error parsing ReachableDirAddresses entry; ignoring.");
291
    ret = -1;
292
293
  }

294
295
  /* We ignore ReachableAddresses for relays */
  if (!server_mode(options)) {
Nick Mathewson's avatar
Nick Mathewson committed
296
297
    if (policy_is_reject_star(reachable_or_addr_policy, AF_UNSPEC, 0)
        || policy_is_reject_star(reachable_dir_addr_policy, AF_UNSPEC,0)) {
298
299
300
301
      log_warn(LD_CONFIG, "Tor cannot connect to the Internet if "
               "ReachableAddresses, ReachableORAddresses, or "
               "ReachableDirAddresses reject all addresses. Please accept "
               "some addresses in these options.");
302
    } else if (options->ClientUseIPv4 == 1
Nick Mathewson's avatar
Nick Mathewson committed
303
304
       && (policy_is_reject_star(reachable_or_addr_policy, AF_INET, 0)
           || policy_is_reject_star(reachable_dir_addr_policy, AF_INET, 0))) {
305
306
307
308
309
          log_warn(LD_CONFIG, "You have set ClientUseIPv4 1, but "
                   "ReachableAddresses, ReachableORAddresses, or "
                   "ReachableDirAddresses reject all IPv4 addresses. "
                   "Tor will not connect using IPv4.");
    } else if (fascist_firewall_use_ipv6(options)
Nick Mathewson's avatar
Nick Mathewson committed
310
311
       && (policy_is_reject_star(reachable_or_addr_policy, AF_INET6, 0)
         || policy_is_reject_star(reachable_dir_addr_policy, AF_INET6, 0))) {
312
313
          log_warn(LD_CONFIG, "You have configured tor to use or prefer IPv6 "
                   "(or UseBridges 1), but "
314
315
316
                   "ReachableAddresses, ReachableORAddresses, or "
                   "ReachableDirAddresses reject all IPv6 addresses. "
                   "Tor will not connect using IPv6.");
317
318
319
    }
  }

Roger Dingledine's avatar
Roger Dingledine committed
320
  /* Append a reject *:* to reachable_(or|dir)_addr_policy */
321
322
323
324
325
326
327
  if (!ret && (options->ReachableDirAddresses ||
               options->ReachableORAddresses ||
               options->ReachableAddresses)) {
    append_exit_policy_string(&reachable_or_addr_policy, "reject *:*");
    append_exit_policy_string(&reachable_dir_addr_policy, "reject *:*");
  }

328
  return ret;
329
330
}

331
332
333
334
/* Return true iff ClientUseIPv4 0 or ClientUseIPv6 0 might block any OR or Dir
 * address:port combination. */
static int
firewall_is_fascist_impl(void)
335
336
337
338
{
  const or_options_t *options = get_options();
  /* Assume every non-bridge relay has an IPv4 address.
   * Clients which use bridges may only know the IPv6 address of their
339
340
   * bridge, but they will connect regardless of the ClientUseIPv6 setting. */
  return options->ClientUseIPv4 == 0;
341
342
343
344
}

/** Return true iff the firewall options, including ClientUseIPv4 0 and
 * ClientUseIPv6 0, might block any OR address:port combination.
345
346
 * Address preferences may still change which address is selected even if
 * this function returns false.
347
348
349
350
 */
int
firewall_is_fascist_or(void)
{
351
352
353
354
355
  return (reachable_or_addr_policy != NULL || firewall_is_fascist_impl());
}

/** Return true iff the firewall options, including ClientUseIPv4 0 and
 * ClientUseIPv6 0, might block any Dir address:port combination.
356
357
 * Address preferences may still change which address is selected even if
 * this function returns false.
358
359
360
361
362
 */
int
firewall_is_fascist_dir(void)
{
  return (reachable_dir_addr_policy != NULL || firewall_is_fascist_impl());
363
364
365
366
367
368
}

/** Return true iff <b>policy</b> (possibly NULL) will allow a
 * connection to <b>addr</b>:<b>port</b>.
 */
static int
369
addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
370
                            smartlist_t *policy)
371
372
{
  addr_policy_result_t p;
373
  p = compare_tor_addr_to_addr_policy(addr, port, policy);
374
375
376
377
378
379
380
381
382
383
384
385
386
  switch (p) {
    case ADDR_POLICY_PROBABLY_ACCEPTED:
    case ADDR_POLICY_ACCEPTED:
      return 1;
    case ADDR_POLICY_PROBABLY_REJECTED:
    case ADDR_POLICY_REJECTED:
      return 0;
    default:
      log_warn(LD_BUG, "Unexpected result: %d", (int)p);
      return 0;
  }
}

387
388
389
390
/** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
 * <b>addr</b>:<b>port</b>.  <b>addr</b> is an IPv4 address given in host
 * order. */
/* XXXX deprecate when possible. */
391
392
393
394
395
396
397
398
399
static int
addr_policy_permits_address(uint32_t addr, uint16_t port,
                            smartlist_t *policy)
{
  tor_addr_t a;
  tor_addr_from_ipv4h(&a, addr);
  return addr_policy_permits_tor_addr(&a, port, policy);
}

400
401
402
/** Return true iff we think our firewall will let us make a connection to
 * addr:port.
 *
403
404
 * If we are configured as a server, ignore any address family preference and
 * just use IPv4.
405
406
407
408
409
 * Otherwise:
 *  - return false for all IPv4 addresses:
 *    - if ClientUseIPv4 is 0, or
 *      if pref_only and pref_ipv6 are both true;
 *  - return false for all IPv6 addresses:
410
 *    - if fascist_firewall_use_ipv6() is 0, or
411
412
413
414
415
416
417
418
 *    - if pref_only is true and pref_ipv6 is false.
 *
 * Return false if addr is NULL or tor_addr_is_null(), or if port is 0. */
STATIC int
fascist_firewall_allows_address(const tor_addr_t *addr,
                                uint16_t port,
                                smartlist_t *firewall_policy,
                                int pref_only, int pref_ipv6)
419
{
420
  const or_options_t *options = get_options();
421
  const int client_mode = !server_mode(options);
422
423
424
425
426

  if (!addr || tor_addr_is_null(addr) || !port) {
    return 0;
  }

427
428
429
430
431
432
  /* Clients stop using IPv4 if it's disabled. In most cases, clients also
   * stop using IPv4 if it's not preferred.
   * Servers must have IPv4 enabled and preferred. */
  if (tor_addr_family(addr) == AF_INET && client_mode &&
      (!options->ClientUseIPv4 || (pref_only && pref_ipv6))) {
    return 0;
433
434
  }

435
436
  /* Clients and Servers won't use IPv6 unless it's enabled (and in most
   * cases, IPv6 must also be preferred before it will be used). */
437
  if (tor_addr_family(addr) == AF_INET6 &&
438
      (!fascist_firewall_use_ipv6(options) || (pref_only && !pref_ipv6))) {
439
    return 0;
440
  }
441

442
  return addr_policy_permits_tor_addr(addr, port,
443
444
445
                                      firewall_policy);
}

446
/** Is this client configured to use IPv6?
447
448
449
 * Returns true if the client might use IPv6 for some of its connections
 * (including dual-stack and IPv6-only clients), and false if it will never
 * use IPv6 for any connections.
450
451
 * Use node_ipv6_or/dir_preferred() when checking a specific node and OR/Dir
 * port: it supports bridge client per-node IPv6 preferences.
452
 */
Nick Mathewson's avatar
Nick Mathewson committed
453
454
int
fascist_firewall_use_ipv6(const or_options_t *options)
455
{
456
  /* Clients use IPv6 if it's set, or they use bridges, or they don't use
457
458
459
460
461
   * IPv4, or they prefer it.
   * ClientPreferIPv6DirPort is deprecated, but check it anyway. */
  return (options->ClientUseIPv6 == 1 || options->ClientUseIPv4 == 0 ||
          options->ClientPreferIPv6ORPort == 1 ||
          options->ClientPreferIPv6DirPort == 1 || options->UseBridges == 1);
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
}

/** Do we prefer to connect to IPv6, ignoring ClientPreferIPv6ORPort and
 * ClientPreferIPv6DirPort?
 * If we're unsure, return -1, otherwise, return 1 for IPv6 and 0 for IPv4.
 */
static int
fascist_firewall_prefer_ipv6_impl(const or_options_t *options)
{
  /*
   Cheap implementation of config options ClientUseIPv4 & ClientUseIPv6 --
   If we're a server or IPv6 is disabled, use IPv4.
   If IPv4 is disabled, use IPv6.
   */

  if (server_mode(options) || !fascist_firewall_use_ipv6(options)) {
    return 0;
  }

  if (!options->ClientUseIPv4) {
    return 1;
  }

  return -1;
}

/** Do we prefer to connect to IPv6 ORPorts?
489
490
 * Use node_ipv6_or_preferred() whenever possible: it supports bridge client
 * per-node IPv6 preferences.
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
 */
int
fascist_firewall_prefer_ipv6_orport(const or_options_t *options)
{
  int pref_ipv6 = fascist_firewall_prefer_ipv6_impl(options);

  if (pref_ipv6 >= 0) {
    return pref_ipv6;
  }

  /* We can use both IPv4 and IPv6 - which do we prefer? */
  if (options->ClientPreferIPv6ORPort == 1) {
    return 1;
  }

  return 0;
507
508
}

509
/** Do we prefer to connect to IPv6 DirPorts?
510
511
512
 *
 * (node_ipv6_dir_preferred() doesn't support bridge client per-node IPv6
 * preferences. There's no reason to use it instead of this function.)
513
 */
514
int
515
fascist_firewall_prefer_ipv6_dirport(const or_options_t *options)
516
{
517
518
519
520
521
522
523
524
525
526
527
528
  int pref_ipv6 = fascist_firewall_prefer_ipv6_impl(options);

  if (pref_ipv6 >= 0) {
    return pref_ipv6;
  }

  /* We can use both IPv4 and IPv6 - which do we prefer? */
  if (options->ClientPreferIPv6DirPort == 1) {
    return 1;
  }

  return 0;
529
530
}

531
532
533
/** Return true iff we think our firewall will let us make a connection to
 * addr:port. Uses ReachableORAddresses or ReachableDirAddresses based on
 * fw_connection.
534
535
536
 * If pref_only is true, return true if addr is in the client's preferred
 * address family, which is IPv6 if pref_ipv6 is true, and IPv4 otherwise.
 * If pref_only is false, ignore pref_ipv6, and return true if addr is allowed.
537
 */
538
int
539
540
fascist_firewall_allows_address_addr(const tor_addr_t *addr, uint16_t port,
                                     firewall_connection_t fw_connection,
541
                                     int pref_only, int pref_ipv6)
542
543
544
{
  if (fw_connection == FIREWALL_OR_CONNECTION) {
    return fascist_firewall_allows_address(addr, port,
Nick Mathewson's avatar
Nick Mathewson committed
545
                               reachable_or_addr_policy,
546
                               pref_only, pref_ipv6);
547
548
  } else if (fw_connection == FIREWALL_DIR_CONNECTION) {
    return fascist_firewall_allows_address(addr, port,
Nick Mathewson's avatar
Nick Mathewson committed
549
                               reachable_dir_addr_policy,
550
                               pref_only, pref_ipv6);
551
  } else {
552
553
554
555
556
557
558
559
560
    log_warn(LD_BUG, "Bad firewall_connection_t value %d.",
             fw_connection);
    return 0;
  }
}

/** Return true iff we think our firewall will let us make a connection to
 * addr:port (ap). Uses ReachableORAddresses or ReachableDirAddresses based on
 * fw_connection.
561
 * pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
562
 */
563
static int
564
565
fascist_firewall_allows_address_ap(const tor_addr_port_t *ap,
                                   firewall_connection_t fw_connection,
566
                                   int pref_only, int pref_ipv6)
567
568
569
{
  tor_assert(ap);
  return fascist_firewall_allows_address_addr(&ap->addr, ap->port,
570
571
                                              fw_connection, pref_only,
                                              pref_ipv6);
572
573
574
575
576
577
}

/* Return true iff we think our firewall will let us make a connection to
 * ipv4h_or_addr:ipv4_or_port. ipv4h_or_addr is interpreted in host order.
 * Uses ReachableORAddresses or ReachableDirAddresses based on
 * fw_connection.
578
579
 * pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
 */
580
static int
581
582
583
fascist_firewall_allows_address_ipv4h(uint32_t ipv4h_or_addr,
                                          uint16_t ipv4_or_port,
                                          firewall_connection_t fw_connection,
584
                                          int pref_only, int pref_ipv6)
585
586
587
588
{
  tor_addr_t ipv4_or_addr;
  tor_addr_from_ipv4h(&ipv4_or_addr, ipv4h_or_addr);
  return fascist_firewall_allows_address_addr(&ipv4_or_addr, ipv4_or_port,
589
590
                                              fw_connection, pref_only,
                                              pref_ipv6);
591
592
593
594
595
596
}

/** Return true iff we think our firewall will let us make a connection to
 * ipv4h_addr/ipv6_addr. Uses ipv4_orport/ipv6_orport/ReachableORAddresses or
 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
 * <b>fw_connection</b>.
597
598
 * pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
 */
599
600
601
602
603
604
static int
fascist_firewall_allows_base(uint32_t ipv4h_addr, uint16_t ipv4_orport,
                             uint16_t ipv4_dirport,
                             const tor_addr_t *ipv6_addr, uint16_t ipv6_orport,
                             uint16_t ipv6_dirport,
                             firewall_connection_t fw_connection,
605
                             int pref_only, int pref_ipv6)
606
607
608
609
610
611
{
  if (fascist_firewall_allows_address_ipv4h(ipv4h_addr,
                                      (fw_connection == FIREWALL_OR_CONNECTION
                                       ? ipv4_orport
                                       : ipv4_dirport),
                                      fw_connection,
612
                                      pref_only, pref_ipv6)) {
613
614
    return 1;
  }
615
616
617
618
619
620

  if (fascist_firewall_allows_address_addr(ipv6_addr,
                                      (fw_connection == FIREWALL_OR_CONNECTION
                                       ? ipv6_orport
                                       : ipv6_dirport),
                                      fw_connection,
621
                                      pref_only, pref_ipv6)) {
622
623
624
625
626
627
    return 1;
  }

  return 0;
}

628
/** Like fascist_firewall_allows_base(), but takes ri. */
629
630
631
static int
fascist_firewall_allows_ri_impl(const routerinfo_t *ri,
                                firewall_connection_t fw_connection,
632
                                int pref_only, int pref_ipv6)
633
634
635
636
637
638
639
640
{
  if (!ri) {
    return 0;
  }

  /* Assume IPv4 and IPv6 DirPorts are the same */
  return fascist_firewall_allows_base(ri->addr, ri->or_port, ri->dir_port,
                                      &ri->ipv6_addr, ri->ipv6_orport,
641
642
                                      ri->dir_port, fw_connection, pref_only,
                                      pref_ipv6);
643
644
}

645
/** Like fascist_firewall_allows_rs, but takes pref_ipv6. */
646
647
648
static int
fascist_firewall_allows_rs_impl(const routerstatus_t *rs,
                                firewall_connection_t fw_connection,
649
                                int pref_only, int pref_ipv6)
650
651
652
653
654
655
656
657
{
  if (!rs) {
    return 0;
  }

  /* Assume IPv4 and IPv6 DirPorts are the same */
  return fascist_firewall_allows_base(rs->addr, rs->or_port, rs->dir_port,
                                      &rs->ipv6_addr, rs->ipv6_orport,
658
659
                                      rs->dir_port, fw_connection, pref_only,
                                      pref_ipv6);
660
661
}

662
/** Like fascist_firewall_allows_base(), but takes rs.
663
664
665
666
667
 * When rs is a fake_status from a dir_server_t, it can have a reachable
 * address, even when the corresponding node does not.
 * nodes can be missing addresses when there's no consensus (IPv4 and IPv6),
 * or when there is a microdescriptor consensus, but no microdescriptors
 * (microdescriptors have IPv6, the microdesc consensus does not). */
668
int
669
670
fascist_firewall_allows_rs(const routerstatus_t *rs,
                           firewall_connection_t fw_connection, int pref_only)
671
{
672
673
674
675
  if (!rs) {
    return 0;
  }

676
677
678
679
680
681
  /* We don't have access to the node-specific IPv6 preference, so use the
   * generic IPv6 preference instead. */
  const or_options_t *options = get_options();
  int pref_ipv6 = (fw_connection == FIREWALL_OR_CONNECTION
                   ? fascist_firewall_prefer_ipv6_orport(options)
                   : fascist_firewall_prefer_ipv6_dirport(options));
682

683
684
  return fascist_firewall_allows_rs_impl(rs, fw_connection, pref_only,
                                         pref_ipv6);
685
686
}

687
688
689
690
691
/** Return true iff we think our firewall will let us make a connection to
 * ipv6_addr:ipv6_orport based on ReachableORAddresses.
 * If <b>fw_connection</b> is FIREWALL_DIR_CONNECTION, returns 0.
 * pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
 */
692
693
694
static int
fascist_firewall_allows_md_impl(const microdesc_t *md,
                                firewall_connection_t fw_connection,
695
                                int pref_only, int pref_ipv6)
696
697
698
699
700
701
702
703
704
705
706
707
{
  if (!md) {
    return 0;
  }

  /* Can't check dirport, it doesn't have one */
  if (fw_connection == FIREWALL_DIR_CONNECTION) {
    return 0;
  }

  /* Also can't check IPv4, doesn't have that either */
  return fascist_firewall_allows_address_addr(&md->ipv6_addr, md->ipv6_orport,
708
709
                                              fw_connection, pref_only,
                                              pref_ipv6);
710
711
}

712
713
/** Like fascist_firewall_allows_base(), but takes node, and looks up pref_ipv6
 * from node_ipv6_or/dir_preferred(). */
714
int
715
716
717
fascist_firewall_allows_node(const node_t *node,
                             firewall_connection_t fw_connection,
                             int pref_only)
718
{
719
720
721
722
723
724
  if (!node) {
    return 0;
  }

  node_assert_ok(node);

725
726
727
728
  const int pref_ipv6 = (fw_connection == FIREWALL_OR_CONNECTION
                         ? node_ipv6_or_preferred(node)
                         : node_ipv6_dir_preferred(node));

729
730
731
  /* Sometimes, the rs is missing the IPv6 address info, and we need to go
   * all the way to the md */
  if (node->ri && fascist_firewall_allows_ri_impl(node->ri, fw_connection,
732
                                                  pref_only, pref_ipv6)) {
733
734
735
    return 1;
  } else if (node->rs && fascist_firewall_allows_rs_impl(node->rs,
                                                         fw_connection,
736
737
                                                         pref_only,
                                                         pref_ipv6)) {
738
739
740
    return 1;
  } else if (node->md && fascist_firewall_allows_md_impl(node->md,
                                                         fw_connection,
741
742
                                                         pref_only,
                                                         pref_ipv6)) {
743
744
745
746
747
748
749
750
    return 1;
  } else {
    /* If we know nothing, assume it's unreachable, we'll never get an address
     * to connect to. */
    return 0;
  }
}

751
/** Like fascist_firewall_allows_rs(), but takes ds. */
752
753
754
755
756
757
758
759
760
761
762
763
int
fascist_firewall_allows_dir_server(const dir_server_t *ds,
                                   firewall_connection_t fw_connection,
                                   int pref_only)
{
  if (!ds) {
    return 0;
  }

  /* A dir_server_t always has a fake_status. As long as it has the same
   * addresses/ports in both fake_status and dir_server_t, this works fine.
   * (See #17867.)
764
   * fascist_firewall_allows_rs only checks the addresses in fake_status. */
765
766
767
768
769
770
771
772
  return fascist_firewall_allows_rs(&ds->fake_status, fw_connection,
                                    pref_only);
}

/** If a and b are both valid and allowed by fw_connection,
 * choose one based on want_a and return it.
 * Otherwise, return whichever is allowed.
 * Otherwise, return NULL.
773
774
 * pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
 */
775
776
777
778
779
static const tor_addr_port_t *
fascist_firewall_choose_address_impl(const tor_addr_port_t *a,
                                     const tor_addr_port_t *b,
                                     int want_a,
                                     firewall_connection_t fw_connection,
780
                                     int pref_only, int pref_ipv6)
781
782
783
784
{
  const tor_addr_port_t *use_a = NULL;
  const tor_addr_port_t *use_b = NULL;

785
786
  if (fascist_firewall_allows_address_ap(a, fw_connection, pref_only,
                                         pref_ipv6)) {
787
788
789
    use_a = a;
  }

790
791
  if (fascist_firewall_allows_address_ap(b, fw_connection, pref_only,
                                         pref_ipv6)) {
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
    use_b = b;
  }

  /* If both are allowed */
  if (use_a && use_b) {
    /* Choose a if we want it */
    return (want_a ? use_a : use_b);
  } else {
    /* Choose a if we have it */
    return (use_a ? use_a : use_b);
  }
}

/** If a and b are both valid and preferred by fw_connection,
 * choose one based on want_a and return it.
 * Otherwise, return whichever is preferred.
 * If neither are preferred, and pref_only is false:
 *  - If a and b are both allowed by fw_connection,
 *    choose one based on want_a and return it.
 *  - Otherwise, return whichever is preferred.
 * Otherwise, return NULL. */
813
STATIC const tor_addr_port_t *
814
815
816
817
fascist_firewall_choose_address(const tor_addr_port_t *a,
                                const tor_addr_port_t *b,
                                int want_a,
                                firewall_connection_t fw_connection,
818
                                int pref_only, int pref_ipv6)
819
820
821
822
{
  const tor_addr_port_t *pref = fascist_firewall_choose_address_impl(
                                                                a, b, want_a,
                                                                fw_connection,
823
                                                                1, pref_ipv6);
824
825
826
827
828
829
830
831
832
  if (pref_only || pref) {
    /* If there is a preferred address, use it. If we can only use preferred
     * addresses, and neither address is preferred, pref will be NULL, and we
     * want to return NULL, so return it. */
    return pref;
  } else {
    /* If there's no preferred address, and we can return addresses that are
     * not preferred, use an address that's allowed */
    return fascist_firewall_choose_address_impl(a, b, want_a, fw_connection,
833
                                                0, pref_ipv6);
834
835
836
837
838
839
840
841
842
843
  }
}

/** Copy an address and port into <b>ap</b> that we think our firewall will
 * let us connect to. Uses ipv4_addr/ipv6_addr and
 * ipv4_orport/ipv6_orport/ReachableORAddresses or
 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
 * <b>fw_connection</b>.
 * If pref_only, only choose preferred addresses. In either case, choose
 * a preferred address before an address that's not preferred.
844
 * If both addresses could be chosen (they are both preferred or both allowed)
845
846
 * choose IPv6 if pref_ipv6 is true, otherwise choose IPv4. */
static void
847
848
849
850
851
852
853
854
fascist_firewall_choose_address_base(const tor_addr_t *ipv4_addr,
                                     uint16_t ipv4_orport,
                                     uint16_t ipv4_dirport,
                                     const tor_addr_t *ipv6_addr,
                                     uint16_t ipv6_orport,
                                     uint16_t ipv6_dirport,
                                     firewall_connection_t fw_connection,
                                     int pref_only,
855
                                     int pref_ipv6,
856
857
858
                                     tor_addr_port_t* ap)
{
  const tor_addr_port_t *result = NULL;
859
  const int want_ipv4 = !pref_ipv6;
860
861
862
863

  tor_assert(ipv6_addr);
  tor_assert(ap);

864
865
866
  tor_addr_make_null(&ap->addr, AF_UNSPEC);
  ap->port = 0;

867
868
869
870
871
872
873
874
875
876
877
878
879
  tor_addr_port_t ipv4_ap;
  tor_addr_copy(&ipv4_ap.addr, ipv4_addr);
  ipv4_ap.port = (fw_connection == FIREWALL_OR_CONNECTION
                  ? ipv4_orport
                  : ipv4_dirport);

  tor_addr_port_t ipv6_ap;
  tor_addr_copy(&ipv6_ap.addr, ipv6_addr);
  ipv6_ap.port = (fw_connection == FIREWALL_OR_CONNECTION
                  ? ipv6_orport
                  : ipv6_dirport);

  result = fascist_firewall_choose_address(&ipv4_ap, &ipv6_ap,
880
881
882
                                           want_ipv4,
                                           fw_connection, pref_only,
                                           pref_ipv6);
883
884
885
886
887
888
889

  if (result) {
    tor_addr_copy(&ap->addr, &result->addr);
    ap->port = result->port;
  }
}

890
/** Like fascist_firewall_choose_address_base(), but takes a host-order IPv4
891
 * address as the first parameter. */
892
static void
893
894
895
896
897
898
899
900
fascist_firewall_choose_address_ipv4h(uint32_t ipv4h_addr,
                                      uint16_t ipv4_orport,
                                      uint16_t ipv4_dirport,
                                      const tor_addr_t *ipv6_addr,
                                      uint16_t ipv6_orport,
                                      uint16_t ipv6_dirport,
                                      firewall_connection_t fw_connection,
                                      int pref_only,
901
                                      int pref_ipv6,
902
903
904
905
                                      tor_addr_port_t* ap)
{
  tor_addr_t ipv4_addr;
  tor_addr_from_ipv4h(&ipv4_addr, ipv4h_addr);
906
907
908
909
910
  tor_assert(ap);

  tor_addr_make_null(&ap->addr, AF_UNSPEC);
  ap->port = 0;

911
912
913
914
915
  fascist_firewall_choose_address_base(&ipv4_addr, ipv4_orport,
                                       ipv4_dirport, ipv6_addr,
                                       ipv6_orport, ipv6_dirport,
                                       fw_connection, pref_only,
                                       pref_ipv6, ap);
916
917
}

918
919
920
921
/* Some microdescriptor consensus methods have no IPv6 addresses in rs: they
 * are in the microdescriptors. For these consensus methods, we can't rely on
 * the node's IPv6 address until its microdescriptor is available (when using
 * microdescs).
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
 * But for bridges, rewrite_node_address_for_bridge() updates node->ri with
 * the configured address, so we can trust bridge addresses.
 * (Bridges could gain an IPv6 address if their microdescriptor arrives, but
 * this will never be their preferred address: that is in the config.)
 * Returns true if the node needs a microdescriptor for its IPv6 address, and
 * false if the addresses in the node are already up-to-date.
 */
static int
node_awaiting_ipv6(const or_options_t* options, const node_t *node)
{
  tor_assert(node);

  /* There's no point waiting for an IPv6 address if we'd never use it */
  if (!fascist_firewall_use_ipv6(options)) {
    return 0;
  }

939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
  /* If the node has an IPv6 address, we're not waiting */
  if (node_has_ipv6_addr(node)) {
    return 0;
  }

  /* If the current consensus method and flavour has IPv6 addresses, we're not
   * waiting */
  if (networkstatus_consensus_has_ipv6(options)) {
    return 0;
  }

  /* Bridge clients never use the address from a bridge's md, so there's no
   * need to wait for it. */
  if (node_is_a_configured_bridge(node)) {
    return 0;
  }

956
  /* We are waiting if we_use_microdescriptors_for_circuits() and we have no
957
958
   * md. */
  return (!node->md && we_use_microdescriptors_for_circuits(options));
959
960
}

961
962
963
964
965
/** Like fascist_firewall_choose_address_base(), but takes <b>rs</b>.
 * Consults the corresponding node, then falls back to rs if node is NULL.
 * This should only happen when there's no valid consensus, and rs doesn't
 * correspond to a bridge client's bridge.
 */
966
void
967
968
969
970
971
972
fascist_firewall_choose_address_rs(const routerstatus_t *rs,
                                   firewall_connection_t fw_connection,
                                   int pref_only, tor_addr_port_t* ap)
{
  tor_assert(ap);

973
974
975
  tor_addr_make_null(&ap->addr, AF_UNSPEC);
  ap->port = 0;

976
977
978
979
  if (!rs) {
    return;
  }

980
  const or_options_t *options = get_options();
981
982
  const node_t *node = node_get_by_id(rs->identity_digest);

983
  if (node && !node_awaiting_ipv6(options, node)) {
984
    fascist_firewall_choose_address_node(node, fw_connection, pref_only, ap);
985
  } else {
986
987
988
989
990
991
992
993
    /* There's no node-specific IPv6 preference, so use the generic IPv6
     * preference instead. */
    int pref_ipv6 = (fw_connection == FIREWALL_OR_CONNECTION
                     ? fascist_firewall_prefer_ipv6_orport(options)
                     : fascist_firewall_prefer_ipv6_dirport(options));

    /* Assume IPv4 and IPv6 DirPorts are the same.
     * Assume the IPv6 OR and Dir addresses are the same. */
994
995
996
997
    fascist_firewall_choose_address_ipv4h(rs->addr, rs->or_port, rs->dir_port,
                                          &rs->ipv6_addr, rs->ipv6_orport,
                                          rs->dir_port, fw_connection,
                                          pref_only, pref_ipv6, ap);
998
  }
999
1000
}

For faster browsing, not all history is shown. View entire blame