Merge remote branch 'origin/maint-0.2.1' into maint-0.2.2
[tor/rransom.git] / src / or / policies.c
blob62e048cfc2a4fae38614313d3962382584d3fa2b
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file policies.c
8 * \brief Code to parse and use address policies and exit policies.
9 **/
11 #include "or.h"
12 #include "config.h"
13 #include "dirserv.h"
14 #include "policies.h"
15 #include "routerparse.h"
16 #include "ht.h"
18 /** Policy that addresses for incoming SOCKS connections must match. */
19 static smartlist_t *socks_policy = NULL;
20 /** Policy that addresses for incoming directory connections must match. */
21 static smartlist_t *dir_policy = NULL;
22 /** Policy that addresses for incoming router descriptors must match in order
23 * to be published by us. */
24 static smartlist_t *authdir_reject_policy = NULL;
25 /** Policy that addresses for incoming router descriptors must match in order
26 * to be marked as valid in our networkstatus. */
27 static smartlist_t *authdir_invalid_policy = NULL;
28 /** Policy that addresses for incoming router descriptors must <b>not</b>
29 * match in order to not be marked as BadDirectory. */
30 static smartlist_t *authdir_baddir_policy = NULL;
31 /** Policy that addresses for incoming router descriptors must <b>not</b>
32 * match in order to not be marked as BadExit. */
33 static smartlist_t *authdir_badexit_policy = NULL;
35 /** Parsed addr_policy_t describing which addresses we believe we can start
36 * circuits at. */
37 static smartlist_t *reachable_or_addr_policy = NULL;
38 /** Parsed addr_policy_t describing which addresses we believe we can connect
39 * to directories at. */
40 static smartlist_t *reachable_dir_addr_policy = NULL;
42 /** Element of an exit policy summary */
43 typedef struct policy_summary_item_t {
44 uint16_t prt_min; /**< Lowest port number to accept/reject. */
45 uint16_t prt_max; /**< Highest port number to accept/reject. */
46 uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
47 this port range. */
48 int accepted:1; /** Has this port already been accepted */
49 } policy_summary_item_t;
51 /** Private networks. This list is used in two places, once to expand the
52 * "private" keyword when parsing our own exit policy, secondly to ignore
53 * just such networks when building exit policy summaries. It is important
54 * that all authorities agree on that list when creating summaries, so don't
55 * just change this without a proper migration plan and a proposal and stuff.
57 static const char *private_nets[] = {
58 "0.0.0.0/8", "169.254.0.0/16",
59 "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
60 // "fc00::/7", "fe80::/10", "fec0::/10", "::/127",
61 NULL };
63 /** Replace all "private" entries in *<b>policy</b> with their expanded
64 * equivalents. */
65 void
66 policy_expand_private(smartlist_t **policy)
68 uint16_t port_min, port_max;
70 int i;
71 smartlist_t *tmp;
73 if (!*policy) /*XXXX disallow NULL policies? */
74 return;
76 tmp = smartlist_create();
78 SMARTLIST_FOREACH(*policy, addr_policy_t *, p,
80 if (! p->is_private) {
81 smartlist_add(tmp, p);
82 continue;
84 for (i = 0; private_nets[i]; ++i) {
85 addr_policy_t policy;
86 memcpy(&policy, p, sizeof(addr_policy_t));
87 policy.is_private = 0;
88 policy.is_canonical = 0;
89 if (tor_addr_parse_mask_ports(private_nets[i], &policy.addr,
90 &policy.maskbits, &port_min, &port_max)<0) {
91 tor_assert(0);
93 smartlist_add(tmp, addr_policy_get_canonical_entry(&policy));
95 addr_policy_free(p);
96 });
98 smartlist_free(*policy);
99 *policy = tmp;
103 * Given a linked list of config lines containing "allow" and "deny"
104 * tokens, parse them and append the result to <b>dest</b>. Return -1
105 * if any tokens are malformed (and don't append any), else return 0.
107 * If <b>assume_action</b> is nonnegative, then insert its action
108 * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
109 * action.
111 static int
112 parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
113 int assume_action)
115 smartlist_t *result;
116 smartlist_t *entries;
117 addr_policy_t *item;
118 int r = 0;
120 if (!cfg)
121 return 0;
123 result = smartlist_create();
124 entries = smartlist_create();
125 for (; cfg; cfg = cfg->next) {
126 smartlist_split_string(entries, cfg->value, ",",
127 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
128 SMARTLIST_FOREACH(entries, const char *, ent,
130 log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
131 item = router_parse_addr_policy_item_from_string(ent, assume_action);
132 if (item) {
133 smartlist_add(result, item);
134 } else {
135 log_warn(LD_CONFIG,"Malformed policy '%s'.", ent);
136 r = -1;
139 SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
140 smartlist_clear(entries);
142 smartlist_free(entries);
143 if (r == -1) {
144 addr_policy_list_free(result);
145 } else {
146 policy_expand_private(&result);
148 if (*dest) {
149 smartlist_add_all(*dest, result);
150 smartlist_free(result);
151 } else {
152 *dest = result;
156 return r;
159 /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
160 * reachable_(or|dir)_addr_policy. The options should already have
161 * been validated by validate_addr_policies.
163 static int
164 parse_reachable_addresses(void)
166 or_options_t *options = get_options();
167 int ret = 0;
169 if (options->ReachableDirAddresses &&
170 options->ReachableORAddresses &&
171 options->ReachableAddresses) {
172 log_warn(LD_CONFIG,
173 "Both ReachableDirAddresses and ReachableORAddresses are set. "
174 "ReachableAddresses setting will be ignored.");
176 addr_policy_list_free(reachable_or_addr_policy);
177 reachable_or_addr_policy = NULL;
178 if (!options->ReachableORAddresses && options->ReachableAddresses)
179 log_info(LD_CONFIG,
180 "Using ReachableAddresses as ReachableORAddresses.");
181 if (parse_addr_policy(options->ReachableORAddresses ?
182 options->ReachableORAddresses :
183 options->ReachableAddresses,
184 &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
185 log_warn(LD_CONFIG,
186 "Error parsing Reachable%sAddresses entry; ignoring.",
187 options->ReachableORAddresses ? "OR" : "");
188 ret = -1;
191 addr_policy_list_free(reachable_dir_addr_policy);
192 reachable_dir_addr_policy = NULL;
193 if (!options->ReachableDirAddresses && options->ReachableAddresses)
194 log_info(LD_CONFIG,
195 "Using ReachableAddresses as ReachableDirAddresses");
196 if (parse_addr_policy(options->ReachableDirAddresses ?
197 options->ReachableDirAddresses :
198 options->ReachableAddresses,
199 &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
200 if (options->ReachableDirAddresses)
201 log_warn(LD_CONFIG,
202 "Error parsing ReachableDirAddresses entry; ignoring.");
203 ret = -1;
205 return ret;
208 /** Return true iff the firewall options might block any address:port
209 * combination.
212 firewall_is_fascist_or(void)
214 return reachable_or_addr_policy != NULL;
217 /** Return true iff <b>policy</b> (possibly NULL) will allow a
218 * connection to <b>addr</b>:<b>port</b>.
220 static int
221 addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
222 smartlist_t *policy)
224 addr_policy_result_t p;
225 p = compare_tor_addr_to_addr_policy(addr, port, policy);
226 switch (p) {
227 case ADDR_POLICY_PROBABLY_ACCEPTED:
228 case ADDR_POLICY_ACCEPTED:
229 return 1;
230 case ADDR_POLICY_PROBABLY_REJECTED:
231 case ADDR_POLICY_REJECTED:
232 return 0;
233 default:
234 log_warn(LD_BUG, "Unexpected result: %d", (int)p);
235 return 0;
239 /** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
240 * <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
241 * order. */
242 /* XXXX deprecate when possible. */
243 static int
244 addr_policy_permits_address(uint32_t addr, uint16_t port,
245 smartlist_t *policy)
247 tor_addr_t a;
248 tor_addr_from_ipv4h(&a, addr);
249 return addr_policy_permits_tor_addr(&a, port, policy);
252 /** Return true iff we think our firewall will let us make an OR connection to
253 * addr:port. */
255 fascist_firewall_allows_address_or(const tor_addr_t *addr, uint16_t port)
257 return addr_policy_permits_tor_addr(addr, port,
258 reachable_or_addr_policy);
261 /** Return true iff we think our firewall will let us make an OR connection to
262 * <b>ri</b>. */
264 fascist_firewall_allows_or(routerinfo_t *ri)
266 /* XXXX proposal 118 */
267 tor_addr_t addr;
268 tor_addr_from_ipv4h(&addr, ri->addr);
269 return fascist_firewall_allows_address_or(&addr, ri->or_port);
272 /** Return true iff we think our firewall will let us make a directory
273 * connection to addr:port. */
275 fascist_firewall_allows_address_dir(const tor_addr_t *addr, uint16_t port)
277 return addr_policy_permits_tor_addr(addr, port,
278 reachable_dir_addr_policy);
281 /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
282 * based on <b>dir_policy</b>. Else return 0.
285 dir_policy_permits_address(const tor_addr_t *addr)
287 return addr_policy_permits_tor_addr(addr, 1, dir_policy);
290 /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
291 * based on <b>socks_policy</b>. Else return 0.
294 socks_policy_permits_address(const tor_addr_t *addr)
296 return addr_policy_permits_tor_addr(addr, 1, socks_policy);
299 /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
300 * directory, based on <b>authdir_reject_policy</b>. Else return 0.
303 authdir_policy_permits_address(uint32_t addr, uint16_t port)
305 return addr_policy_permits_address(addr, port, authdir_reject_policy);
308 /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
309 * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
312 authdir_policy_valid_address(uint32_t addr, uint16_t port)
314 return addr_policy_permits_address(addr, port, authdir_invalid_policy);
317 /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad dir,
318 * based on <b>authdir_baddir_policy</b>. Else return 0.
321 authdir_policy_baddir_address(uint32_t addr, uint16_t port)
323 return ! addr_policy_permits_address(addr, port, authdir_baddir_policy);
326 /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
327 * based on <b>authdir_badexit_policy</b>. Else return 0.
330 authdir_policy_badexit_address(uint32_t addr, uint16_t port)
332 return ! addr_policy_permits_address(addr, port, authdir_badexit_policy);
335 #define REJECT(arg) \
336 STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
338 /** Config helper: If there's any problem with the policy configuration
339 * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
340 * allocated description of the error. Else return 0. */
342 validate_addr_policies(or_options_t *options, char **msg)
344 /* XXXX Maybe merge this into parse_policies_from_options, to make sure
345 * that the two can't go out of sync. */
347 smartlist_t *addr_policy=NULL;
348 *msg = NULL;
350 if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
351 options->ExitPolicyRejectPrivate, NULL,
352 !options->BridgeRelay))
353 REJECT("Error in ExitPolicy entry.");
355 /* The rest of these calls *append* to addr_policy. So don't actually
356 * use the results for anything other than checking if they parse! */
357 if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
358 REJECT("Error in DirPolicy entry.");
359 if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
360 REJECT("Error in SocksPolicy entry.");
361 if (parse_addr_policy(options->AuthDirReject, &addr_policy,
362 ADDR_POLICY_REJECT))
363 REJECT("Error in AuthDirReject entry.");
364 if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
365 ADDR_POLICY_REJECT))
366 REJECT("Error in AuthDirInvalid entry.");
367 if (parse_addr_policy(options->AuthDirBadDir, &addr_policy,
368 ADDR_POLICY_REJECT))
369 REJECT("Error in AuthDirBadDir entry.");
370 if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
371 ADDR_POLICY_REJECT))
372 REJECT("Error in AuthDirBadExit entry.");
374 if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
375 ADDR_POLICY_ACCEPT))
376 REJECT("Error in ReachableAddresses entry.");
377 if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
378 ADDR_POLICY_ACCEPT))
379 REJECT("Error in ReachableORAddresses entry.");
380 if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
381 ADDR_POLICY_ACCEPT))
382 REJECT("Error in ReachableDirAddresses entry.");
384 err:
385 addr_policy_list_free(addr_policy);
386 return *msg ? -1 : 0;
387 #undef REJECT
390 /** Parse <b>string</b> in the same way that the exit policy
391 * is parsed, and put the processed version in *<b>policy</b>.
392 * Ignore port specifiers.
394 static int
395 load_policy_from_option(config_line_t *config, smartlist_t **policy,
396 int assume_action)
398 int r;
399 addr_policy_list_free(*policy);
400 *policy = NULL;
401 r = parse_addr_policy(config, policy, assume_action);
402 if (r < 0) {
403 return -1;
405 if (*policy) {
406 SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, n) {
407 /* ports aren't used in these. */
408 if (n->prt_min > 1 || n->prt_max != 65535) {
409 addr_policy_t newp, *c;
410 memcpy(&newp, n, sizeof(newp));
411 newp.prt_min = 1;
412 newp.prt_max = 65535;
413 newp.is_canonical = 0;
414 c = addr_policy_get_canonical_entry(&newp);
415 SMARTLIST_REPLACE_CURRENT(*policy, n, c);
416 addr_policy_free(n);
418 } SMARTLIST_FOREACH_END(n);
420 return 0;
423 /** Set all policies based on <b>options</b>, which should have been validated
424 * first by validate_addr_policies. */
426 policies_parse_from_options(or_options_t *options)
428 int ret = 0;
429 if (load_policy_from_option(options->SocksPolicy, &socks_policy, -1) < 0)
430 ret = -1;
431 if (load_policy_from_option(options->DirPolicy, &dir_policy, -1) < 0)
432 ret = -1;
433 if (load_policy_from_option(options->AuthDirReject,
434 &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
435 ret = -1;
436 if (load_policy_from_option(options->AuthDirInvalid,
437 &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
438 ret = -1;
439 if (load_policy_from_option(options->AuthDirBadDir,
440 &authdir_baddir_policy, ADDR_POLICY_REJECT) < 0)
441 ret = -1;
442 if (load_policy_from_option(options->AuthDirBadExit,
443 &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
444 ret = -1;
445 if (parse_reachable_addresses() < 0)
446 ret = -1;
447 return ret;
450 /** Compare two provided address policy items, and return -1, 0, or 1
451 * if the first is less than, equal to, or greater than the second. */
452 static int
453 cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
455 int r;
456 if ((r=((int)a->policy_type - (int)b->policy_type)))
457 return r;
458 if ((r=((int)a->is_private - (int)b->is_private)))
459 return r;
460 if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
461 return r;
462 if ((r=((int)a->maskbits - (int)b->maskbits)))
463 return r;
464 if ((r=((int)a->prt_min - (int)b->prt_min)))
465 return r;
466 if ((r=((int)a->prt_max - (int)b->prt_max)))
467 return r;
468 return 0;
471 /** Like cmp_single_addr_policy() above, but looks at the
472 * whole set of policies in each case. */
474 cmp_addr_policies(smartlist_t *a, smartlist_t *b)
476 int r, i;
477 int len_a = a ? smartlist_len(a) : 0;
478 int len_b = b ? smartlist_len(b) : 0;
480 for (i = 0; i < len_a && i < len_b; ++i) {
481 if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
482 return r;
484 if (i == len_a && i == len_b)
485 return 0;
486 if (i < len_a)
487 return -1;
488 else
489 return 1;
492 /** Node in hashtable used to store address policy entries. */
493 typedef struct policy_map_ent_t {
494 HT_ENTRY(policy_map_ent_t) node;
495 addr_policy_t *policy;
496 } policy_map_ent_t;
498 static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
500 /** Return true iff a and b are equal. */
501 static INLINE int
502 policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
504 return cmp_single_addr_policy(a->policy, b->policy) == 0;
507 /** Return a hashcode for <b>ent</b> */
508 static unsigned int
509 policy_hash(policy_map_ent_t *ent)
511 addr_policy_t *a = ent->policy;
512 unsigned int r;
513 if (a->is_private)
514 r = 0x1234abcd;
515 else
516 r = tor_addr_hash(&a->addr);
517 r += a->prt_min << 8;
518 r += a->prt_max << 16;
519 r += a->maskbits;
520 if (a->policy_type == ADDR_POLICY_REJECT)
521 r ^= 0xffffffff;
523 return r;
526 HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
527 policy_eq)
528 HT_GENERATE(policy_map, policy_map_ent_t, node, policy_hash,
529 policy_eq, 0.6, malloc, realloc, free)
531 /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
532 * "canonical" copy of that addr_policy_t; the canonical copy is a single
533 * reference-counted object. */
534 addr_policy_t *
535 addr_policy_get_canonical_entry(addr_policy_t *e)
537 policy_map_ent_t search, *found;
538 if (e->is_canonical)
539 return e;
541 search.policy = e;
542 found = HT_FIND(policy_map, &policy_root, &search);
543 if (!found) {
544 found = tor_malloc_zero(sizeof(policy_map_ent_t));
545 found->policy = tor_memdup(e, sizeof(addr_policy_t));
546 found->policy->is_canonical = 1;
547 found->policy->refcnt = 0;
548 HT_INSERT(policy_map, &policy_root, found);
551 tor_assert(!cmp_single_addr_policy(found->policy, e));
552 ++found->policy->refcnt;
553 return found->policy;
556 /** As compare_tor_addr_to_addr_policy, but instead of a tor_addr_t, takes
557 * in host order. */
558 addr_policy_result_t
559 compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
560 const smartlist_t *policy)
562 /*XXXX deprecate this function when possible. */
563 tor_addr_t a;
564 tor_addr_from_ipv4h(&a, addr);
565 return compare_tor_addr_to_addr_policy(&a, port, policy);
568 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
569 * addr and port are both known. */
570 static addr_policy_result_t
571 compare_known_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
572 const smartlist_t *policy)
574 /* We know the address and port, and we know the policy, so we can just
575 * compute an exact match. */
576 SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
577 /* Address is known */
578 if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
579 CMP_EXACT)) {
580 if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
581 /* Exact match for the policy */
582 return tmpe->policy_type == ADDR_POLICY_ACCEPT ?
583 ADDR_POLICY_ACCEPTED : ADDR_POLICY_REJECTED;
586 } SMARTLIST_FOREACH_END(tmpe);
588 /* accept all by default. */
589 return ADDR_POLICY_ACCEPTED;
592 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
593 * addr is known but port is not. */
594 static addr_policy_result_t
595 compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t *addr,
596 const smartlist_t *policy)
598 /* We look to see if there's a definite match. If so, we return that
599 match's value, unless there's an intervening possible match that says
600 something different. */
601 int maybe_accept = 0, maybe_reject = 0;
603 SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
604 if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
605 CMP_EXACT)) {
606 if (tmpe->prt_min <= 1 && tmpe->prt_max >= 65535) {
607 /* Definitely matches, since it covers all ports. */
608 if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
609 /* If we already hit a clause that might trigger a 'reject', than we
610 * can't be sure of this certain 'accept'.*/
611 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
612 ADDR_POLICY_ACCEPTED;
613 } else {
614 return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
615 ADDR_POLICY_REJECTED;
617 } else {
618 /* Might match. */
619 if (tmpe->policy_type == ADDR_POLICY_REJECT)
620 maybe_reject = 1;
621 else
622 maybe_accept = 1;
625 } SMARTLIST_FOREACH_END(tmpe);
627 /* accept all by default. */
628 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
631 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
632 * port is known but address is not. */
633 static addr_policy_result_t
634 compare_unknown_tor_addr_to_addr_policy(uint16_t port,
635 const smartlist_t *policy)
637 /* We look to see if there's a definite match. If so, we return that
638 match's value, unless there's an intervening possible match that says
639 something different. */
640 int maybe_accept = 0, maybe_reject = 0;
642 SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
643 if (tmpe->prt_min <= port && port <= tmpe->prt_max) {
644 if (tmpe->maskbits == 0) {
645 /* Definitely matches, since it covers all addresses. */
646 if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
647 /* If we already hit a clause that might trigger a 'reject', than we
648 * can't be sure of this certain 'accept'.*/
649 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
650 ADDR_POLICY_ACCEPTED;
651 } else {
652 return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
653 ADDR_POLICY_REJECTED;
655 } else {
656 /* Might match. */
657 if (tmpe->policy_type == ADDR_POLICY_REJECT)
658 maybe_reject = 1;
659 else
660 maybe_accept = 1;
663 } SMARTLIST_FOREACH_END(tmpe);
665 /* accept all by default. */
666 return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
669 /** Decide whether a given addr:port is definitely accepted,
670 * definitely rejected, probably accepted, or probably rejected by a
671 * given policy. If <b>addr</b> is 0, we don't know the IP of the
672 * target address. If <b>port</b> is 0, we don't know the port of the
673 * target address. (At least one of <b>addr</b> and <b>port</b> must be
674 * provided. If you want to know whether a policy would definitely reject
675 * an unknown address:port, use policy_is_reject_star().)
677 * We could do better by assuming that some ranges never match typical
678 * addresses (127.0.0.1, and so on). But we'll try this for now.
680 addr_policy_result_t
681 compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
682 const smartlist_t *policy)
684 if (!policy) {
685 /* no policy? accept all. */
686 return ADDR_POLICY_ACCEPTED;
687 } else if (tor_addr_is_null(addr)) {
688 tor_assert(port != 0);
689 return compare_unknown_tor_addr_to_addr_policy(port, policy);
690 } else if (port == 0) {
691 return compare_known_tor_addr_to_addr_policy_noport(addr, policy);
692 } else {
693 return compare_known_tor_addr_to_addr_policy(addr, port, policy);
697 /** Return true iff the address policy <b>a</b> covers every case that
698 * would be covered by <b>b</b>, so that a,b is redundant. */
699 static int
700 addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
702 /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
703 * to "accept *:80". */
704 if (a->maskbits > b->maskbits) {
705 /* a has more fixed bits than b; it can't possibly cover b. */
706 return 0;
708 if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_EXACT)) {
709 /* There's a fixed bit in a that's set differently in b. */
710 return 0;
712 return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
715 /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
716 * that is, there exists an address/port that is covered by <b>a</b> that
717 * is also covered by <b>b</b>.
719 static int
720 addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
722 maskbits_t minbits;
723 /* All the bits we care about are those that are set in both
724 * netmasks. If they are equal in a and b's networkaddresses
725 * then the networks intersect. If there is a difference,
726 * then they do not. */
727 if (a->maskbits < b->maskbits)
728 minbits = a->maskbits;
729 else
730 minbits = b->maskbits;
731 if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_EXACT))
732 return 0;
733 if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
734 return 0;
735 return 1;
738 /** Add the exit policy described by <b>more</b> to <b>policy</b>.
740 static void
741 append_exit_policy_string(smartlist_t **policy, const char *more)
743 config_line_t tmp;
745 tmp.key = NULL;
746 tmp.value = (char*) more;
747 tmp.next = NULL;
748 if (parse_addr_policy(&tmp, policy, -1)<0) {
749 log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
753 /** Detect and excise "dead code" from the policy *<b>dest</b>. */
754 static void
755 exit_policy_remove_redundancies(smartlist_t *dest)
757 addr_policy_t *ap, *tmp, *victim;
758 int i, j;
760 /* Step one: find a *:* entry and cut off everything after it. */
761 for (i = 0; i < smartlist_len(dest); ++i) {
762 ap = smartlist_get(dest, i);
763 if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
764 /* This is a catch-all line -- later lines are unreachable. */
765 while (i+1 < smartlist_len(dest)) {
766 victim = smartlist_get(dest, i+1);
767 smartlist_del(dest, i+1);
768 addr_policy_free(victim);
770 break;
774 /* Step two: for every entry, see if there's a redundant entry
775 * later on, and remove it. */
776 for (i = 0; i < smartlist_len(dest)-1; ++i) {
777 ap = smartlist_get(dest, i);
778 for (j = i+1; j < smartlist_len(dest); ++j) {
779 tmp = smartlist_get(dest, j);
780 tor_assert(j > i);
781 if (addr_policy_covers(ap, tmp)) {
782 char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
783 policy_write_item(p1, sizeof(p1), tmp, 0);
784 policy_write_item(p2, sizeof(p2), ap, 0);
785 log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s (%d). It is made "
786 "redundant by %s (%d).", p1, j, p2, i);
787 smartlist_del_keeporder(dest, j--);
788 addr_policy_free(tmp);
793 /* Step three: for every entry A, see if there's an entry B making this one
794 * redundant later on. This is the case if A and B are of the same type
795 * (accept/reject), A is a subset of B, and there is no other entry of
796 * different type in between those two that intersects with A.
798 * Anybody want to double-check the logic here? XXX
800 for (i = 0; i < smartlist_len(dest)-1; ++i) {
801 ap = smartlist_get(dest, i);
802 for (j = i+1; j < smartlist_len(dest); ++j) {
803 // tor_assert(j > i); // j starts out at i+1; j only increases; i only
804 // // decreases.
805 tmp = smartlist_get(dest, j);
806 if (ap->policy_type != tmp->policy_type) {
807 if (addr_policy_intersects(ap, tmp))
808 break;
809 } else { /* policy_types are equal. */
810 if (addr_policy_covers(tmp, ap)) {
811 char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
812 policy_write_item(p1, sizeof(p1), ap, 0);
813 policy_write_item(p2, sizeof(p2), tmp, 0);
814 log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s. It is already "
815 "covered by %s.", p1, p2);
816 smartlist_del_keeporder(dest, i--);
817 addr_policy_free(ap);
818 break;
825 #define DEFAULT_EXIT_POLICY \
826 "reject *:25,reject *:119,reject *:135-139,reject *:445," \
827 "reject *:563,reject *:1214,reject *:4661-4666," \
828 "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
830 /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
831 * cfg doesn't end in an absolute accept or reject and if
832 * <b>add_default_policy</b> is true, add the default exit
833 * policy afterwards. If <b>rejectprivate</b> is true, prepend
834 * "reject private:*" to the policy. Return -1 if we can't parse cfg,
835 * else return 0.
838 policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
839 int rejectprivate, const char *local_address,
840 int add_default_policy)
842 if (rejectprivate) {
843 append_exit_policy_string(dest, "reject private:*");
844 if (local_address) {
845 char buf[POLICY_BUF_LEN];
846 tor_snprintf(buf, sizeof(buf), "reject %s:*", local_address);
847 append_exit_policy_string(dest, buf);
850 if (parse_addr_policy(cfg, dest, -1))
851 return -1;
852 if (add_default_policy)
853 append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
854 else
855 append_exit_policy_string(dest, "reject *:*");
856 exit_policy_remove_redundancies(*dest);
858 return 0;
861 /** Replace the exit policy of <b>r</b> with reject *:*. */
862 void
863 policies_set_router_exitpolicy_to_reject_all(routerinfo_t *r)
865 addr_policy_t *item;
866 addr_policy_list_free(r->exit_policy);
867 r->exit_policy = smartlist_create();
868 item = router_parse_addr_policy_item_from_string("reject *:*", -1);
869 smartlist_add(r->exit_policy, item);
872 /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
873 * allows exiting to <b>port</b>. Otherwise, return 0. */
874 static int
875 exit_policy_is_general_exit_helper(smartlist_t *policy, int port)
877 uint32_t mask, ip, i;
878 /* Is this /8 rejected (1), or undecided (0)? */
879 char subnet_status[256];
881 memset(subnet_status, 0, sizeof(subnet_status));
882 SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
883 if (p->prt_min > port || p->prt_max < port)
884 continue; /* Doesn't cover our port. */
885 mask = 0;
886 tor_assert(p->maskbits <= 32);
888 if (p->maskbits)
889 mask = UINT32_MAX<<(32-p->maskbits);
890 ip = tor_addr_to_ipv4h(&p->addr);
892 /* Calculate the first and last subnet that this exit policy touches
893 * and set it as loop boundaries. */
894 for (i = ((mask & ip)>>24); i <= (~((mask & ip) ^ mask)>>24); ++i) {
895 tor_addr_t addr;
896 if (subnet_status[i] != 0)
897 continue; /* We already reject some part of this /8 */
898 tor_addr_from_ipv4h(&addr, i<<24);
899 if (tor_addr_is_internal(&addr, 0))
900 continue; /* Local or non-routable addresses */
901 if (p->policy_type == ADDR_POLICY_ACCEPT) {
902 if (p->maskbits > 8)
903 continue; /* Narrower than a /8. */
904 /* We found an allowed subnet of at least size /8. Done
905 * for this port! */
906 return 1;
907 } else if (p->policy_type == ADDR_POLICY_REJECT) {
908 subnet_status[i] = 1;
912 return 0;
915 /** Return true iff <b>ri</b> is "useful as an exit node", meaning
916 * it allows exit to at least one /8 address space for at least
917 * two of ports 80, 443, and 6667. */
919 exit_policy_is_general_exit(smartlist_t *policy)
921 static const int ports[] = { 80, 443, 6667 };
922 int n_allowed = 0;
923 int i;
924 if (!policy) /*XXXX disallow NULL policies? */
925 return 0;
927 for (i = 0; i < 3; ++i) {
928 n_allowed += exit_policy_is_general_exit_helper(policy, ports[i]);
930 return n_allowed >= 2;
933 /** Return false if <b>policy</b> might permit access to some addr:port;
934 * otherwise if we are certain it rejects everything, return true. */
936 policy_is_reject_star(const smartlist_t *policy)
938 if (!policy) /*XXXX disallow NULL policies? */
939 return 1;
940 SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
941 if (p->policy_type == ADDR_POLICY_ACCEPT)
942 return 0;
943 else if (p->policy_type == ADDR_POLICY_REJECT &&
944 p->prt_min <= 1 && p->prt_max == 65535 &&
945 p->maskbits == 0)
946 return 1;
948 return 1;
951 /** Write a single address policy to the buf_len byte buffer at buf. Return
952 * the number of characters written, or -1 on failure. */
954 policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
955 int format_for_desc)
957 size_t written = 0;
958 char addrbuf[TOR_ADDR_BUF_LEN];
959 const char *addrpart;
960 int result;
961 const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
962 const int is_ip6 = tor_addr_family(&policy->addr) == AF_INET6;
964 tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
966 /* write accept/reject 1.2.3.4 */
967 if (policy->is_private)
968 addrpart = "private";
969 else if (policy->maskbits == 0)
970 addrpart = "*";
971 else
972 addrpart = addrbuf;
974 result = tor_snprintf(buf, buflen, "%s%s%s %s",
975 (is_ip6&&format_for_desc)?"opt ":"",
976 is_accept ? "accept" : "reject",
977 (is_ip6&&format_for_desc)?"6":"",
978 addrpart);
979 if (result < 0)
980 return -1;
981 written += strlen(buf);
982 /* If the maskbits is 32 we don't need to give it. If the mask is 0,
983 * we already wrote "*". */
984 if (policy->maskbits < 32 && policy->maskbits > 0) {
985 if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
986 return -1;
987 written += strlen(buf+written);
989 if (policy->prt_min <= 1 && policy->prt_max == 65535) {
990 /* There is no port set; write ":*" */
991 if (written+4 > buflen)
992 return -1;
993 strlcat(buf+written, ":*", buflen-written);
994 written += 2;
995 } else if (policy->prt_min == policy->prt_max) {
996 /* There is only one port; write ":80". */
997 result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
998 if (result<0)
999 return -1;
1000 written += result;
1001 } else {
1002 /* There is a range of ports; write ":79-80". */
1003 result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
1004 policy->prt_min, policy->prt_max);
1005 if (result<0)
1006 return -1;
1007 written += result;
1009 if (written < buflen)
1010 buf[written] = '\0';
1011 else
1012 return -1;
1014 return (int)written;
1017 /** Create a new exit policy summary, initially only with a single
1018 * port 1-64k item */
1019 /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
1020 * RB-tree if that turns out to matter. */
1021 static smartlist_t *
1022 policy_summary_create(void)
1024 smartlist_t *summary;
1025 policy_summary_item_t* item;
1027 item = tor_malloc_zero(sizeof(policy_summary_item_t));
1028 item->prt_min = 1;
1029 item->prt_max = 65535;
1030 item->reject_count = 0;
1031 item->accepted = 0;
1033 summary = smartlist_create();
1034 smartlist_add(summary, item);
1036 return summary;
1039 /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
1040 * The current item is changed to end at new-starts - 1, the new item
1041 * copies reject_count and accepted from the old item,
1042 * starts at new_starts and ends at the port where the original item
1043 * previously ended.
1045 static policy_summary_item_t*
1046 policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts)
1048 policy_summary_item_t* new;
1050 new = tor_malloc_zero(sizeof(policy_summary_item_t));
1051 new->prt_min = new_starts;
1052 new->prt_max = old->prt_max;
1053 new->reject_count = old->reject_count;
1054 new->accepted = old->accepted;
1056 old->prt_max = new_starts-1;
1058 tor_assert(old->prt_min <= old->prt_max);
1059 tor_assert(new->prt_min <= new->prt_max);
1060 return new;
1063 /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
1064 * my immortal soul, he can clean it up himself. */
1065 #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
1067 #define REJECT_CUTOFF_COUNT (1<<25)
1068 /** Split an exit policy summary so that prt_min and prt_max
1069 * fall at exactly the start and end of an item respectively.
1071 static int
1072 policy_summary_split(smartlist_t *summary,
1073 uint16_t prt_min, uint16_t prt_max)
1075 int start_at_index;
1077 int i = 0;
1078 /* XXXX Do a binary search if run time matters */
1079 while (AT(i)->prt_max < prt_min)
1080 i++;
1081 if (AT(i)->prt_min != prt_min) {
1082 policy_summary_item_t* new_item;
1083 new_item = policy_summary_item_split(AT(i), prt_min);
1084 smartlist_insert(summary, i+1, new_item);
1085 i++;
1087 start_at_index = i;
1089 while (AT(i)->prt_max < prt_max)
1090 i++;
1091 if (AT(i)->prt_max != prt_max) {
1092 policy_summary_item_t* new_item;
1093 new_item = policy_summary_item_split(AT(i), prt_max+1);
1094 smartlist_insert(summary, i+1, new_item);
1097 return start_at_index;
1100 /** Mark port ranges as accepted if they are below the reject_count */
1101 static void
1102 policy_summary_accept(smartlist_t *summary,
1103 uint16_t prt_min, uint16_t prt_max)
1105 int i = policy_summary_split(summary, prt_min, prt_max);
1106 while (i < smartlist_len(summary) &&
1107 AT(i)->prt_max <= prt_max) {
1108 if (!AT(i)->accepted &&
1109 AT(i)->reject_count <= REJECT_CUTOFF_COUNT)
1110 AT(i)->accepted = 1;
1111 i++;
1113 tor_assert(i < smartlist_len(summary) || prt_max==65535);
1116 /** Count the number of addresses in a network with prefixlen maskbits
1117 * against the given portrange. */
1118 static void
1119 policy_summary_reject(smartlist_t *summary,
1120 maskbits_t maskbits,
1121 uint16_t prt_min, uint16_t prt_max)
1123 int i = policy_summary_split(summary, prt_min, prt_max);
1124 /* XXX: ipv4 specific */
1125 uint64_t count = (U64_LITERAL(1) << (32-maskbits));
1126 while (i < smartlist_len(summary) &&
1127 AT(i)->prt_max <= prt_max) {
1128 AT(i)->reject_count += count;
1129 i++;
1131 tor_assert(i < smartlist_len(summary) || prt_max==65535);
1134 /** Add a single exit policy item to our summary:
1135 * If it is an accept ignore it unless it is for all IP addresses
1136 * ("*"), i.e. it's prefixlen/maskbits is 0, else call
1137 * policy_summary_accept().
1138 * If it's a reject ignore it if it is about one of the private
1139 * networks, else call policy_summary_reject().
1141 static void
1142 policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
1144 if (p->policy_type == ADDR_POLICY_ACCEPT) {
1145 if (p->maskbits == 0) {
1146 policy_summary_accept(summary, p->prt_min, p->prt_max);
1148 } else if (p->policy_type == ADDR_POLICY_REJECT) {
1150 int is_private = 0;
1151 int i;
1152 for (i = 0; private_nets[i]; ++i) {
1153 tor_addr_t addr;
1154 maskbits_t maskbits;
1155 if (tor_addr_parse_mask_ports(private_nets[i], &addr,
1156 &maskbits, NULL, NULL)<0) {
1157 tor_assert(0);
1159 if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
1160 p->maskbits == maskbits) {
1161 is_private = 1;
1162 break;
1166 if (!is_private) {
1167 policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
1169 } else
1170 tor_assert(0);
1173 /** Create a string representing a summary for an exit policy.
1174 * The summary will either be an "accept" plus a comma-separated list of port
1175 * ranges or a "reject" plus port-ranges, depending on which is shorter.
1177 * If no exits are allowed at all then NULL is returned, if no ports
1178 * are blocked instead of "reject " we return "accept 1-65535" (this
1179 * is an exception to the shorter-representation-wins rule).
1181 char *
1182 policy_summarize(smartlist_t *policy)
1184 smartlist_t *summary = policy_summary_create();
1185 smartlist_t *accepts, *rejects;
1186 int i, last, start_prt;
1187 size_t accepts_len, rejects_len, shorter_len, final_size;
1188 char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
1189 const char *prefix;
1191 tor_assert(policy);
1193 /* Create the summary list */
1194 SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
1195 policy_summary_add_item(summary, p);
1198 /* Now create two lists of strings, one for accepted and one
1199 * for rejected ports. We take care to merge ranges so that
1200 * we avoid getting stuff like "1-4,5-9,10", instead we want
1201 * "1-10"
1203 i = 0;
1204 start_prt = 1;
1205 accepts = smartlist_create();
1206 rejects = smartlist_create();
1207 while (1) {
1208 last = i == smartlist_len(summary)-1;
1209 if (last ||
1210 AT(i)->accepted != AT(i+1)->accepted) {
1211 char buf[POLICY_BUF_LEN];
1213 if (start_prt == AT(i)->prt_max)
1214 tor_snprintf(buf, sizeof(buf), "%d", start_prt);
1215 else
1216 tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
1218 if (AT(i)->accepted)
1219 smartlist_add(accepts, tor_strdup(buf));
1220 else
1221 smartlist_add(rejects, tor_strdup(buf));
1223 if (last)
1224 break;
1226 start_prt = AT(i+1)->prt_min;
1228 i++;
1231 /* Figure out which of the two stringlists will be shorter and use
1232 * that to build the result
1234 if (smartlist_len(accepts) == 0) { /* no exits at all */
1235 result = tor_strdup("reject 1-65535");
1236 goto cleanup;
1238 if (smartlist_len(rejects) == 0) { /* no rejects at all */
1239 result = tor_strdup("accept 1-65535");
1240 goto cleanup;
1243 accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
1244 rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
1246 if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
1247 accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
1248 char *c;
1249 shorter_str = accepts_str;
1250 prefix = "accept";
1252 c = shorter_str + (MAX_EXITPOLICY_SUMMARY_LEN-strlen(prefix)-1);
1253 while (*c != ',' && c >= shorter_str)
1254 c--;
1255 tor_assert(c >= shorter_str);
1256 tor_assert(*c == ',');
1257 *c = '\0';
1259 shorter_len = strlen(shorter_str);
1260 } else if (rejects_len < accepts_len) {
1261 shorter_str = rejects_str;
1262 shorter_len = rejects_len;
1263 prefix = "reject";
1264 } else {
1265 shorter_str = accepts_str;
1266 shorter_len = accepts_len;
1267 prefix = "accept";
1270 final_size = strlen(prefix)+1+shorter_len+1;
1271 tor_assert(final_size <= MAX_EXITPOLICY_SUMMARY_LEN+1);
1272 result = tor_malloc(final_size);
1273 tor_snprintf(result, final_size, "%s %s", prefix, shorter_str);
1275 cleanup:
1276 /* cleanup */
1277 SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
1278 smartlist_free(summary);
1280 tor_free(accepts_str);
1281 SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
1282 smartlist_free(accepts);
1284 tor_free(rejects_str);
1285 SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
1286 smartlist_free(rejects);
1288 return result;
1291 /** Implementation for GETINFO control command: knows the answer for questions
1292 * about "exit-policy/..." */
1294 getinfo_helper_policies(control_connection_t *conn,
1295 const char *question, char **answer,
1296 const char **errmsg)
1298 (void) conn;
1299 (void) errmsg;
1300 if (!strcmp(question, "exit-policy/default")) {
1301 *answer = tor_strdup(DEFAULT_EXIT_POLICY);
1303 return 0;
1306 /** Release all storage held by <b>p</b>. */
1307 void
1308 addr_policy_list_free(smartlist_t *lst)
1310 if (!lst)
1311 return;
1312 SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
1313 smartlist_free(lst);
1316 /** Release all storage held by <b>p</b>. */
1317 void
1318 addr_policy_free(addr_policy_t *p)
1320 if (!p)
1321 return;
1323 if (--p->refcnt <= 0) {
1324 if (p->is_canonical) {
1325 policy_map_ent_t search, *found;
1326 search.policy = p;
1327 found = HT_REMOVE(policy_map, &policy_root, &search);
1328 if (found) {
1329 tor_assert(p == found->policy);
1330 tor_free(found);
1333 tor_free(p);
1337 /** Release all storage held by policy variables. */
1338 void
1339 policies_free_all(void)
1341 addr_policy_list_free(reachable_or_addr_policy);
1342 reachable_or_addr_policy = NULL;
1343 addr_policy_list_free(reachable_dir_addr_policy);
1344 reachable_dir_addr_policy = NULL;
1345 addr_policy_list_free(socks_policy);
1346 socks_policy = NULL;
1347 addr_policy_list_free(dir_policy);
1348 dir_policy = NULL;
1349 addr_policy_list_free(authdir_reject_policy);
1350 authdir_reject_policy = NULL;
1351 addr_policy_list_free(authdir_invalid_policy);
1352 authdir_invalid_policy = NULL;
1353 addr_policy_list_free(authdir_baddir_policy);
1354 authdir_baddir_policy = NULL;
1355 addr_policy_list_free(authdir_badexit_policy);
1356 authdir_badexit_policy = NULL;
1358 if (!HT_EMPTY(&policy_root)) {
1359 policy_map_ent_t **ent;
1360 int n = 0;
1361 char buf[POLICY_BUF_LEN];
1363 log_warn(LD_MM, "Still had %d address policies cached at shutdown.",
1364 (int)HT_SIZE(&policy_root));
1366 /* Note the first 10 cached policies to try to figure out where they
1367 * might be coming from. */
1368 HT_FOREACH(ent, policy_map, &policy_root) {
1369 if (++n > 10)
1370 break;
1371 if (policy_write_item(buf, sizeof(buf), (*ent)->policy, 0) >= 0)
1372 log_warn(LD_MM," %d [%d]: %s", n, (*ent)->policy->refcnt, buf);
1375 HT_CLEAR(policy_map, &policy_root);