1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2015, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Code to parse and use address policies and exit policies.
11 #define POLICIES_PRIVATE
16 #include "networkstatus.h"
20 #include "routerparse.h"
24 /** Policy that addresses for incoming SOCKS connections must match. */
25 static smartlist_t
*socks_policy
= NULL
;
26 /** Policy that addresses for incoming directory connections must match. */
27 static smartlist_t
*dir_policy
= NULL
;
28 /** Policy that addresses for incoming router descriptors must match in order
29 * to be published by us. */
30 static smartlist_t
*authdir_reject_policy
= NULL
;
31 /** Policy that addresses for incoming router descriptors must match in order
32 * to be marked as valid in our networkstatus. */
33 static smartlist_t
*authdir_invalid_policy
= NULL
;
34 /** Policy that addresses for incoming router descriptors must <b>not</b>
35 * match in order to not be marked as BadExit. */
36 static smartlist_t
*authdir_badexit_policy
= NULL
;
38 /** Parsed addr_policy_t describing which addresses we believe we can start
40 static smartlist_t
*reachable_or_addr_policy
= NULL
;
41 /** Parsed addr_policy_t describing which addresses we believe we can connect
42 * to directories at. */
43 static smartlist_t
*reachable_dir_addr_policy
= NULL
;
45 /** Element of an exit policy summary */
46 typedef struct policy_summary_item_t
{
47 uint16_t prt_min
; /**< Lowest port number to accept/reject. */
48 uint16_t prt_max
; /**< Highest port number to accept/reject. */
49 uint64_t reject_count
; /**< Number of IP-Addresses that are rejected to
51 unsigned int accepted
:1; /** Has this port already been accepted */
52 } policy_summary_item_t
;
54 /** Private networks. This list is used in two places, once to expand the
55 * "private" keyword when parsing our own exit policy, secondly to ignore
56 * just such networks when building exit policy summaries. It is important
57 * that all authorities agree on that list when creating summaries, so don't
58 * just change this without a proper migration plan and a proposal and stuff.
60 static const char *private_nets
[] = {
61 "0.0.0.0/8", "169.254.0.0/16",
62 "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
64 "[fc00::]/7", "[fe80::]/10", "[fec0::]/10", "[ff00::]/8", "[::]/127",
68 static int policies_parse_exit_policy_internal(
73 const smartlist_t
*configured_addresses
,
74 int reject_interface_addresses
,
75 int reject_configured_port_addresses
,
76 int add_default_policy
);
78 /** Replace all "private" entries in *<b>policy</b> with their expanded
81 policy_expand_private(smartlist_t
**policy
)
83 uint16_t port_min
, port_max
;
88 if (!*policy
) /*XXXX disallow NULL policies? */
91 tmp
= smartlist_new();
93 SMARTLIST_FOREACH_BEGIN(*policy
, addr_policy_t
*, p
) {
94 if (! p
->is_private
) {
95 smartlist_add(tmp
, p
);
98 for (i
= 0; private_nets
[i
]; ++i
) {
99 addr_policy_t newpolicy
;
100 memcpy(&newpolicy
, p
, sizeof(addr_policy_t
));
101 newpolicy
.is_private
= 0;
102 newpolicy
.is_canonical
= 0;
103 if (tor_addr_parse_mask_ports(private_nets
[i
], 0,
105 &newpolicy
.maskbits
, &port_min
, &port_max
)<0) {
108 smartlist_add(tmp
, addr_policy_get_canonical_entry(&newpolicy
));
111 } SMARTLIST_FOREACH_END(p
);
113 smartlist_free(*policy
);
117 /** Expand each of the AF_UNSPEC elements in *<b>policy</b> (which indicate
118 * protocol-neutral wildcards) into a pair of wildcard elements: one IPv4-
119 * specific and one IPv6-specific. */
121 policy_expand_unspec(smartlist_t
**policy
)
127 tmp
= smartlist_new();
128 SMARTLIST_FOREACH_BEGIN(*policy
, addr_policy_t
*, p
) {
129 sa_family_t family
= tor_addr_family(&p
->addr
);
130 if (family
== AF_INET6
|| family
== AF_INET
|| p
->is_private
) {
131 smartlist_add(tmp
, p
);
132 } else if (family
== AF_UNSPEC
) {
133 addr_policy_t newpolicy_ipv4
;
134 addr_policy_t newpolicy_ipv6
;
135 memcpy(&newpolicy_ipv4
, p
, sizeof(addr_policy_t
));
136 memcpy(&newpolicy_ipv6
, p
, sizeof(addr_policy_t
));
137 newpolicy_ipv4
.is_canonical
= 0;
138 newpolicy_ipv6
.is_canonical
= 0;
139 if (p
->maskbits
!= 0) {
140 log_warn(LD_BUG
, "AF_UNSPEC policy with maskbits==%d", p
->maskbits
);
141 newpolicy_ipv4
.maskbits
= 0;
142 newpolicy_ipv6
.maskbits
= 0;
144 tor_addr_from_ipv4h(&newpolicy_ipv4
.addr
, 0);
145 tor_addr_from_ipv6_bytes(&newpolicy_ipv6
.addr
,
146 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
147 smartlist_add(tmp
, addr_policy_get_canonical_entry(&newpolicy_ipv4
));
148 smartlist_add(tmp
, addr_policy_get_canonical_entry(&newpolicy_ipv6
));
151 log_warn(LD_BUG
, "Funny-looking address policy with family %d", family
);
152 smartlist_add(tmp
, p
);
154 } SMARTLIST_FOREACH_END(p
);
156 smartlist_free(*policy
);
161 * Given a linked list of config lines containing "accept[6]" and "reject[6]"
162 * tokens, parse them and append the result to <b>dest</b>. Return -1
163 * if any tokens are malformed (and don't append any), else return 0.
165 * If <b>assume_action</b> is nonnegative, then insert its action
166 * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
170 parse_addr_policy(config_line_t
*cfg
, smartlist_t
**dest
,
174 smartlist_t
*entries
;
182 result
= smartlist_new();
183 entries
= smartlist_new();
184 for (; cfg
; cfg
= cfg
->next
) {
185 smartlist_split_string(entries
, cfg
->value
, ",",
186 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
187 SMARTLIST_FOREACH_BEGIN(entries
, const char *, ent
) {
188 log_debug(LD_CONFIG
,"Adding new entry '%s'",ent
);
190 item
= router_parse_addr_policy_item_from_string(ent
, assume_action
,
193 smartlist_add(result
, item
);
194 } else if (malformed_list
) {
195 /* the error is so severe the entire list should be discarded */
196 log_warn(LD_CONFIG
, "Malformed policy '%s'. Discarding entire policy "
200 /* the error is minor: don't add the item, but keep processing the
201 * rest of the policies in the list */
202 log_debug(LD_CONFIG
, "Ignored policy '%s' due to non-fatal error. "
203 "The remainder of the policy list will be used.",
206 } SMARTLIST_FOREACH_END(ent
);
207 SMARTLIST_FOREACH(entries
, char *, ent
, tor_free(ent
));
208 smartlist_clear(entries
);
210 smartlist_free(entries
);
212 addr_policy_list_free(result
);
214 policy_expand_private(&result
);
215 policy_expand_unspec(&result
);
218 smartlist_add_all(*dest
, result
);
219 smartlist_free(result
);
228 /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
229 * reachable_(or|dir)_addr_policy. The options should already have
230 * been validated by validate_addr_policies.
233 parse_reachable_addresses(void)
235 const or_options_t
*options
= get_options();
238 if (options
->ReachableDirAddresses
&&
239 options
->ReachableORAddresses
&&
240 options
->ReachableAddresses
) {
242 "Both ReachableDirAddresses and ReachableORAddresses are set. "
243 "ReachableAddresses setting will be ignored.");
245 addr_policy_list_free(reachable_or_addr_policy
);
246 reachable_or_addr_policy
= NULL
;
247 if (!options
->ReachableORAddresses
&& options
->ReachableAddresses
)
249 "Using ReachableAddresses as ReachableORAddresses.");
250 if (parse_addr_policy(options
->ReachableORAddresses
?
251 options
->ReachableORAddresses
:
252 options
->ReachableAddresses
,
253 &reachable_or_addr_policy
, ADDR_POLICY_ACCEPT
)) {
255 "Error parsing Reachable%sAddresses entry; ignoring.",
256 options
->ReachableORAddresses
? "OR" : "");
260 addr_policy_list_free(reachable_dir_addr_policy
);
261 reachable_dir_addr_policy
= NULL
;
262 if (!options
->ReachableDirAddresses
&& options
->ReachableAddresses
)
264 "Using ReachableAddresses as ReachableDirAddresses");
265 if (parse_addr_policy(options
->ReachableDirAddresses
?
266 options
->ReachableDirAddresses
:
267 options
->ReachableAddresses
,
268 &reachable_dir_addr_policy
, ADDR_POLICY_ACCEPT
)) {
269 if (options
->ReachableDirAddresses
)
271 "Error parsing ReachableDirAddresses entry; ignoring.");
275 /* We ignore ReachableAddresses for relays */
276 if (!server_mode(options
)) {
277 if ((reachable_or_addr_policy
278 && policy_is_reject_star(reachable_or_addr_policy
, AF_UNSPEC
))
279 || (reachable_dir_addr_policy
280 && policy_is_reject_star(reachable_dir_addr_policy
, AF_UNSPEC
))) {
281 log_warn(LD_CONFIG
, "Tor cannot connect to the Internet if "
282 "ReachableAddresses, ReachableORAddresses, or "
283 "ReachableDirAddresses reject all addresses. Please accept "
284 "some addresses in these options.");
285 } else if (options
->ClientUseIPv4
== 1
286 && ((reachable_or_addr_policy
287 && policy_is_reject_star(reachable_or_addr_policy
, AF_INET
))
288 || (reachable_dir_addr_policy
289 && policy_is_reject_star(reachable_dir_addr_policy
, AF_INET
)))) {
290 log_warn(LD_CONFIG
, "You have set ClientUseIPv4 1, but "
291 "ReachableAddresses, ReachableORAddresses, or "
292 "ReachableDirAddresses reject all IPv4 addresses. "
293 "Tor will not connect using IPv4.");
294 } else if (fascist_firewall_use_ipv6(options
)
295 && ((reachable_or_addr_policy
296 && policy_is_reject_star(reachable_or_addr_policy
, AF_INET6
))
297 || (reachable_dir_addr_policy
298 && policy_is_reject_star(reachable_dir_addr_policy
, AF_INET6
)))) {
299 log_warn(LD_CONFIG
, "You have configured tor to use IPv6 "
300 "(ClientUseIPv6 1 or UseBridges 1), but "
301 "ReachableAddresses, ReachableORAddresses, or "
302 "ReachableDirAddresses reject all IPv6 addresses. "
303 "Tor will not connect using IPv6.");
310 /* Return true iff ClientUseIPv4 0 or ClientUseIPv6 0 might block any OR or Dir
311 * address:port combination. */
313 firewall_is_fascist_impl(void)
315 const or_options_t
*options
= get_options();
316 /* Assume every non-bridge relay has an IPv4 address.
317 * Clients which use bridges may only know the IPv6 address of their
319 return (options
->ClientUseIPv4
== 0
320 || (!fascist_firewall_use_ipv6(options
)
321 && options
->UseBridges
== 1));
324 /** Return true iff the firewall options, including ClientUseIPv4 0 and
325 * ClientUseIPv6 0, might block any OR address:port combination.
328 firewall_is_fascist_or(void)
330 return (reachable_or_addr_policy
!= NULL
|| firewall_is_fascist_impl());
333 /** Return true iff the firewall options, including ClientUseIPv4 0 and
334 * ClientUseIPv6 0, might block any Dir address:port combination.
337 firewall_is_fascist_dir(void)
339 return (reachable_dir_addr_policy
!= NULL
|| firewall_is_fascist_impl());
342 /** Return true iff <b>policy</b> (possibly NULL) will allow a
343 * connection to <b>addr</b>:<b>port</b>.
346 addr_policy_permits_tor_addr(const tor_addr_t
*addr
, uint16_t port
,
349 addr_policy_result_t p
;
350 p
= compare_tor_addr_to_addr_policy(addr
, port
, policy
);
352 case ADDR_POLICY_PROBABLY_ACCEPTED
:
353 case ADDR_POLICY_ACCEPTED
:
355 case ADDR_POLICY_PROBABLY_REJECTED
:
356 case ADDR_POLICY_REJECTED
:
359 log_warn(LD_BUG
, "Unexpected result: %d", (int)p
);
364 /** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
365 * <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
367 /* XXXX deprecate when possible. */
369 addr_policy_permits_address(uint32_t addr
, uint16_t port
,
373 tor_addr_from_ipv4h(&a
, addr
);
374 return addr_policy_permits_tor_addr(&a
, port
, policy
);
377 /** Return true iff we think our firewall will let us make a connection to
380 * If we are configured as a server, ignore any address family preference and
383 * - return false for all IPv4 addresses:
384 * - if ClientUseIPv4 is 0, or
385 * if pref_only and pref_ipv6 are both true;
386 * - return false for all IPv6 addresses:
387 * - if fascist_firewall_use_ipv6() is 0, or
388 * - if pref_only is true and pref_ipv6 is false.
390 * Return false if addr is NULL or tor_addr_is_null(), or if port is 0. */
392 fascist_firewall_allows_address(const tor_addr_t
*addr
,
394 smartlist_t
*firewall_policy
,
395 int pref_only
, int pref_ipv6
)
397 const or_options_t
*options
= get_options();
399 if (!addr
|| tor_addr_is_null(addr
) || !port
) {
403 if (!server_mode(options
)) {
404 if (tor_addr_family(addr
) == AF_INET
&&
405 (!options
->ClientUseIPv4
|| (pref_only
&& pref_ipv6
)))
408 /* Bridges can always use IPv6 */
409 if (tor_addr_family(addr
) == AF_INET6
&&
410 (!fascist_firewall_use_ipv6(options
) || (pref_only
&& !pref_ipv6
)))
414 return addr_policy_permits_tor_addr(addr
, port
,
418 /** Is this client configured to use IPv6?
419 * Clients use IPv6 if ClientUseIPv6 is 1, or UseBridges is 1.
421 int fascist_firewall_use_ipv6(const or_options_t
*options
)
423 return (options
->ClientUseIPv6
== 1 || options
->UseBridges
== 1);
426 /** Do we prefer to connect to IPv6, ignoring ClientPreferIPv6ORPort and
427 * ClientPreferIPv6DirPort?
428 * If we're unsure, return -1, otherwise, return 1 for IPv6 and 0 for IPv4.
431 fascist_firewall_prefer_ipv6_impl(const or_options_t
*options
)
434 Cheap implementation of config options ClientUseIPv4 & ClientUseIPv6 --
435 If we're a server or IPv6 is disabled, use IPv4.
436 If IPv4 is disabled, use IPv6.
439 if (server_mode(options
) || !fascist_firewall_use_ipv6(options
)) {
443 if (!options
->ClientUseIPv4
) {
450 /** Do we prefer to connect to IPv6 ORPorts?
453 fascist_firewall_prefer_ipv6_orport(const or_options_t
*options
)
455 int pref_ipv6
= fascist_firewall_prefer_ipv6_impl(options
);
457 if (pref_ipv6
>= 0) {
461 /* We can use both IPv4 and IPv6 - which do we prefer? */
462 if (options
->ClientPreferIPv6ORPort
== 1) {
466 /* For bridge clients, ClientPreferIPv6ORPort auto means "prefer IPv6". */
467 if (options
->UseBridges
&& options
->ClientPreferIPv6ORPort
!= 0) {
474 /** Do we prefer to connect to IPv6 DirPorts?
477 fascist_firewall_prefer_ipv6_dirport(const or_options_t
*options
)
479 int pref_ipv6
= fascist_firewall_prefer_ipv6_impl(options
);
481 if (pref_ipv6
>= 0) {
485 /* We can use both IPv4 and IPv6 - which do we prefer? */
486 if (options
->ClientPreferIPv6DirPort
== 1) {
490 /* For bridge clients, ClientPreferIPv6ORPort auto means "prefer IPv6".
491 * XX/teor - do bridge clients ever use a DirPort? */
492 if (options
->UseBridges
&& options
->ClientPreferIPv6DirPort
!= 0) {
499 /** Return true iff we think our firewall will let us make a connection to
500 * addr:port. Uses ReachableORAddresses or ReachableDirAddresses based on
502 * If pref_only, return false if addr is not in the client's preferred address
506 fascist_firewall_allows_address_addr(const tor_addr_t
*addr
, uint16_t port
,
507 firewall_connection_t fw_connection
,
510 const or_options_t
*options
= get_options();
512 if (fw_connection
== FIREWALL_OR_CONNECTION
) {
513 return fascist_firewall_allows_address(addr
, port
,
514 reachable_or_addr_policy
,
516 fascist_firewall_prefer_ipv6_orport(options
));
517 } else if (fw_connection
== FIREWALL_DIR_CONNECTION
) {
518 return fascist_firewall_allows_address(addr
, port
,
519 reachable_dir_addr_policy
,
521 fascist_firewall_prefer_ipv6_dirport(options
));
523 log_warn(LD_BUG
, "Bad firewall_connection_t value %d.",
529 /** Return true iff we think our firewall will let us make a connection to
530 * addr:port (ap). Uses ReachableORAddresses or ReachableDirAddresses based on
532 * If pref_only, return false if addr is not in the client's preferred address
536 fascist_firewall_allows_address_ap(const tor_addr_port_t
*ap
,
537 firewall_connection_t fw_connection
,
541 return fascist_firewall_allows_address_addr(&ap
->addr
, ap
->port
,
542 fw_connection
, pref_only
);
545 /* Return true iff we think our firewall will let us make a connection to
546 * ipv4h_or_addr:ipv4_or_port. ipv4h_or_addr is interpreted in host order.
547 * Uses ReachableORAddresses or ReachableDirAddresses based on
549 * If pref_only, return false if addr is not in the client's preferred address
552 fascist_firewall_allows_address_ipv4h(uint32_t ipv4h_or_addr
,
553 uint16_t ipv4_or_port
,
554 firewall_connection_t fw_connection
,
557 tor_addr_t ipv4_or_addr
;
558 tor_addr_from_ipv4h(&ipv4_or_addr
, ipv4h_or_addr
);
559 return fascist_firewall_allows_address_addr(&ipv4_or_addr
, ipv4_or_port
,
560 fw_connection
, pref_only
);
563 /** Return true iff we think our firewall will let us make a connection to
564 * ipv4h_addr/ipv6_addr. Uses ipv4_orport/ipv6_orport/ReachableORAddresses or
565 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
566 * <b>fw_connection</b>.
567 * If pref_only, return false if addr is not in the client's preferred address
570 fascist_firewall_allows_base(uint32_t ipv4h_addr
, uint16_t ipv4_orport
,
571 uint16_t ipv4_dirport
,
572 const tor_addr_t
*ipv6_addr
, uint16_t ipv6_orport
,
573 uint16_t ipv6_dirport
,
574 firewall_connection_t fw_connection
,
577 if (fascist_firewall_allows_address_ipv4h(ipv4h_addr
,
578 (fw_connection
== FIREWALL_OR_CONNECTION
586 if (fascist_firewall_allows_address_addr(ipv6_addr
,
587 (fw_connection
== FIREWALL_OR_CONNECTION
598 /** Like fascist_firewall_allows_ri, but doesn't consult the node. */
600 fascist_firewall_allows_ri_impl(const routerinfo_t
*ri
,
601 firewall_connection_t fw_connection
,
608 /* Assume IPv4 and IPv6 DirPorts are the same */
609 return fascist_firewall_allows_base(ri
->addr
, ri
->or_port
, ri
->dir_port
,
610 &ri
->ipv6_addr
, ri
->ipv6_orport
,
611 ri
->dir_port
, fw_connection
, pref_only
);
614 /** Like fascist_firewall_allows_rs, but doesn't consult the node. */
616 fascist_firewall_allows_rs_impl(const routerstatus_t
*rs
,
617 firewall_connection_t fw_connection
,
624 /* Assume IPv4 and IPv6 DirPorts are the same */
625 return fascist_firewall_allows_base(rs
->addr
, rs
->or_port
, rs
->dir_port
,
626 &rs
->ipv6_addr
, rs
->ipv6_orport
,
627 rs
->dir_port
, fw_connection
, pref_only
);
630 /** Return true iff we think our firewall will let us make a connection to
631 * <b>rs</b> on either its IPv4 or IPv6 address. Uses
632 * or_port/ipv6_orport/ReachableORAddresses or dir_port/ReachableDirAddresses
633 * based on IPv4/IPv6 and <b>fw_connection</b>.
634 * If pref_only, return false if addr is not in the client's preferred address
636 * Consults the corresponding node if the addresses in rs are not permitted. */
638 fascist_firewall_allows_rs(const routerstatus_t
*rs
,
639 firewall_connection_t fw_connection
, int pref_only
)
645 /* Assume IPv4 and IPv6 DirPorts are the same */
646 if (fascist_firewall_allows_rs_impl(rs
, fw_connection
, pref_only
)) {
649 const node_t
*node
= node_get_by_id(rs
->identity_digest
);
650 return fascist_firewall_allows_node(node
, fw_connection
, pref_only
);
654 /** Like fascist_firewall_allows_md, but doesn't consult the node. */
656 fascist_firewall_allows_md_impl(const microdesc_t
*md
,
657 firewall_connection_t fw_connection
,
664 /* Can't check dirport, it doesn't have one */
665 if (fw_connection
== FIREWALL_DIR_CONNECTION
) {
669 /* Also can't check IPv4, doesn't have that either */
670 return fascist_firewall_allows_address_addr(&md
->ipv6_addr
, md
->ipv6_orport
,
671 fw_connection
, pref_only
);
674 /** Return true iff we think our firewall will let us make a connection to
676 * - if <b>preferred</b> is true, on its preferred address,
677 * - if not, on either its IPv4 or IPv6 address.
678 * Uses or_port/ipv6_orport/ReachableORAddresses or
679 * dir_port/ReachableDirAddresses based on IPv4/IPv6 and <b>fw_connection</b>.
680 * If pref_only, return false if addr is not in the client's preferred address
683 fascist_firewall_allows_node(const node_t
*node
,
684 firewall_connection_t fw_connection
,
691 node_assert_ok(node
);
693 /* Sometimes, the rs is missing the IPv6 address info, and we need to go
694 * all the way to the md */
695 if (node
->ri
&& fascist_firewall_allows_ri_impl(node
->ri
, fw_connection
,
698 } else if (node
->rs
&& fascist_firewall_allows_rs_impl(node
->rs
,
702 } else if (node
->md
&& fascist_firewall_allows_md_impl(node
->md
,
707 /* If we know nothing, assume it's unreachable, we'll never get an address
713 /** Return true iff we think our firewall will let us make a connection to
714 * <b>ds</b> on either its IPv4 or IPv6 address. Uses ReachableORAddresses or
715 * ReachableDirAddresses based on <b>fw_connection</b> (some directory
716 * connections are tunneled over ORPorts).
717 * If pref_only, return false if addr is not in the client's preferred address
720 fascist_firewall_allows_dir_server(const dir_server_t
*ds
,
721 firewall_connection_t fw_connection
,
728 /* A dir_server_t always has a fake_status. As long as it has the same
729 * addresses/ports in both fake_status and dir_server_t, this works fine.
731 * This function relies on fascist_firewall_allows_rs looking up the node on
732 * failure, because it will get the latest info for the relay. */
733 return fascist_firewall_allows_rs(&ds
->fake_status
, fw_connection
,
737 /** If a and b are both valid and allowed by fw_connection,
738 * choose one based on want_a and return it.
739 * Otherwise, return whichever is allowed.
740 * Otherwise, return NULL.
741 * If pref_only, only return an address if it's in the client's preferred
743 static const tor_addr_port_t
*
744 fascist_firewall_choose_address_impl(const tor_addr_port_t
*a
,
745 const tor_addr_port_t
*b
,
747 firewall_connection_t fw_connection
,
750 const tor_addr_port_t
*use_a
= NULL
;
751 const tor_addr_port_t
*use_b
= NULL
;
753 if (fascist_firewall_allows_address_ap(a
, fw_connection
, pref_only
)) {
757 if (fascist_firewall_allows_address_ap(b
, fw_connection
, pref_only
)) {
761 /* If both are allowed */
762 if (use_a
&& use_b
) {
763 /* Choose a if we want it */
764 return (want_a
? use_a
: use_b
);
766 /* Choose a if we have it */
767 return (use_a
? use_a
: use_b
);
771 /** If a and b are both valid and preferred by fw_connection,
772 * choose one based on want_a and return it.
773 * Otherwise, return whichever is preferred.
774 * If neither are preferred, and pref_only is false:
775 * - If a and b are both allowed by fw_connection,
776 * choose one based on want_a and return it.
777 * - Otherwise, return whichever is preferred.
778 * Otherwise, return NULL. */
779 const tor_addr_port_t
*
780 fascist_firewall_choose_address(const tor_addr_port_t
*a
,
781 const tor_addr_port_t
*b
,
783 firewall_connection_t fw_connection
,
786 const tor_addr_port_t
*pref
= fascist_firewall_choose_address_impl(
790 if (pref_only
|| pref
) {
791 /* If there is a preferred address, use it. If we can only use preferred
792 * addresses, and neither address is preferred, pref will be NULL, and we
793 * want to return NULL, so return it. */
796 /* If there's no preferred address, and we can return addresses that are
797 * not preferred, use an address that's allowed */
798 return fascist_firewall_choose_address_impl(a
, b
, want_a
, fw_connection
,
803 /** Copy an address and port into <b>ap</b> that we think our firewall will
804 * let us connect to. Uses ipv4_addr/ipv6_addr and
805 * ipv4_orport/ipv6_orport/ReachableORAddresses or
806 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
807 * <b>fw_connection</b>.
808 * If pref_only, only choose preferred addresses. In either case, choose
809 * a preferred address before an address that's not preferred.
810 * If neither address is chosen, return 0, else return 1. */
812 fascist_firewall_choose_address_base(const tor_addr_t
*ipv4_addr
,
813 uint16_t ipv4_orport
,
814 uint16_t ipv4_dirport
,
815 const tor_addr_t
*ipv6_addr
,
816 uint16_t ipv6_orport
,
817 uint16_t ipv6_dirport
,
818 firewall_connection_t fw_connection
,
822 const tor_addr_port_t
*result
= NULL
;
823 /* This argument is ignored as long as the address pair is IPv4/IPv6,
824 * because we always have a preference in a client.
825 * For bridge clients, this selects the preferred address, which was
826 * previously IPv6 (if a bridge has both), so we keep that behaviour. */
827 const int bridge_client_prefer_ipv4
= 0;
829 tor_assert(ipv6_addr
);
832 tor_addr_port_t ipv4_ap
;
833 tor_addr_copy(&ipv4_ap
.addr
, ipv4_addr
);
834 ipv4_ap
.port
= (fw_connection
== FIREWALL_OR_CONNECTION
838 tor_addr_port_t ipv6_ap
;
839 tor_addr_copy(&ipv6_ap
.addr
, ipv6_addr
);
840 ipv6_ap
.port
= (fw_connection
== FIREWALL_OR_CONNECTION
844 result
= fascist_firewall_choose_address(&ipv4_ap
, &ipv6_ap
,
845 bridge_client_prefer_ipv4
,
846 fw_connection
, pref_only
);
849 tor_addr_copy(&ap
->addr
, &result
->addr
);
850 ap
->port
= result
->port
;
857 /** Like fascist_firewall_choose_address_base, but takes a host-order IPv4
858 * address as the first parameter. */
860 fascist_firewall_choose_address_ipv4h(uint32_t ipv4h_addr
,
861 uint16_t ipv4_orport
,
862 uint16_t ipv4_dirport
,
863 const tor_addr_t
*ipv6_addr
,
864 uint16_t ipv6_orport
,
865 uint16_t ipv6_dirport
,
866 firewall_connection_t fw_connection
,
870 tor_addr_t ipv4_addr
;
871 tor_addr_from_ipv4h(&ipv4_addr
, ipv4h_addr
);
872 return fascist_firewall_choose_address_base(&ipv4_addr
, ipv4_orport
,
873 ipv4_dirport
, ipv6_addr
,
874 ipv6_orport
, ipv6_dirport
,
875 fw_connection
, pref_only
, ap
);
878 #define IPV6_OR_LOOKUP(r, identity_digest, ipv6_or_ap) \
880 if (!(r)->ipv6_orport || tor_addr_is_null(&(r)->ipv6_addr)) { \
881 const node_t *node = node_get_by_id((identity_digest)); \
883 node_get_pref_ipv6_orport(node, &(ipv6_or_ap)); \
885 tor_addr_make_null(&(ipv6_or_ap).addr, AF_INET6); \
886 (ipv6_or_ap).port = 0; \
889 tor_addr_copy(&(ipv6_or_ap).addr, &(r)->ipv6_addr); \
890 (ipv6_or_ap).port = (r)->ipv6_orport; \
894 /** Copy an address and port from <b>rs</b> into <b>ap</b> that we think our
895 * firewall will let us connect to. Uses ipv4h_addr/ipv6_addr and
896 * ipv4_orport/ipv6_orport/ReachableORAddresses or
897 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
898 * <b>fw_connection</b>.
899 * If pref_only, only choose preferred addresses. In either case, choose
900 * a preferred address before an address that's not preferred.
901 * If neither address is chosen, return 0, else return 1.
902 * Consults the corresponding node if the addresses in rs are not valid. */
904 fascist_firewall_choose_address_rs(const routerstatus_t
*rs
,
905 firewall_connection_t fw_connection
,
906 int pref_only
, tor_addr_port_t
* ap
)
914 /* Don't do the lookup if the IPv6 address/port in rs is OK.
915 * If it's OK, assume the dir_port is also OK. */
916 tor_addr_port_t ipv6_or_ap
;
917 IPV6_OR_LOOKUP(rs
, rs
->identity_digest
, ipv6_or_ap
);
919 /* Assume IPv4 and IPv6 DirPorts are the same.
920 * Assume the IPv6 OR and Dir addresses are the same. */
921 return fascist_firewall_choose_address_ipv4h(rs
->addr
,
932 /** Copy an address and port from <b>node</b> into <b>ap</b> that we think our
933 * firewall will let us connect to. Uses ipv4h_addr/ipv6_addr and
934 * ipv4_orport/ipv6_orport/ReachableORAddresses or
935 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
936 * <b>fw_connection</b>.
937 * If pref_only, only choose preferred addresses. In either case, choose
938 * a preferred address before an address that's not preferred.
939 * If neither address is chosen, return 0, else return 1. */
941 fascist_firewall_choose_address_node(const node_t
*node
,
942 firewall_connection_t fw_connection
,
943 int pref_only
, tor_addr_port_t
*ap
)
949 node_assert_ok(node
);
951 tor_addr_port_t ipv4_or_ap
;
952 node_get_prim_orport(node
, &ipv4_or_ap
);
953 tor_addr_port_t ipv4_dir_ap
;
954 node_get_prim_dirport(node
, &ipv4_dir_ap
);
956 tor_addr_port_t ipv6_or_ap
;
957 node_get_pref_ipv6_orport(node
, &ipv6_or_ap
);
958 tor_addr_port_t ipv6_dir_ap
;
959 node_get_pref_ipv6_dirport(node
, &ipv6_dir_ap
);
961 /* Assume the IPv6 OR and Dir addresses are the same. */
962 return fascist_firewall_choose_address_base(&ipv4_or_ap
.addr
,
973 /** Copy an address and port from <b>ds</b> into <b>ap</b> that we think our
974 * firewall will let us connect to. Uses ipv4h_addr/ipv6_addr and
975 * ipv4_orport/ipv6_orport/ReachableORAddresses or
976 * ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
977 * <b>fw_connection</b>.
978 * If pref_only, only choose preferred addresses. In either case, choose
979 * a preferred address before an address that's not preferred.
980 * If neither address is chosen, return 0, else return 1. */
982 fascist_firewall_choose_address_dir_server(const dir_server_t
*ds
,
983 firewall_connection_t fw_connection
,
984 int pref_only
, tor_addr_port_t
*ap
)
990 /* A dir_server_t always has a fake_status. As long as it has the same
991 * addresses/ports in both fake_status and dir_server_t, this works fine.
993 * This function relies on fascist_firewall_choose_address_rs looking up the
994 * addresses from the node if it can, because that will get the latest info
996 return fascist_firewall_choose_address_rs(&ds
->fake_status
, fw_connection
,
1000 /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
1001 * based on <b>dir_policy</b>. Else return 0.
1004 dir_policy_permits_address(const tor_addr_t
*addr
)
1006 return addr_policy_permits_tor_addr(addr
, 1, dir_policy
);
1009 /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
1010 * based on <b>socks_policy</b>. Else return 0.
1013 socks_policy_permits_address(const tor_addr_t
*addr
)
1015 return addr_policy_permits_tor_addr(addr
, 1, socks_policy
);
1018 /** Return true iff the address <b>addr</b> is in a country listed in the
1019 * case-insensitive list of country codes <b>cc_list</b>. */
1021 addr_is_in_cc_list(uint32_t addr
, const smartlist_t
*cc_list
)
1030 tor_addr_from_ipv4h(&tar
, addr
);
1031 country
= geoip_get_country_by_addr(&tar
);
1032 name
= geoip_get_country_name(country
);
1033 return smartlist_contains_string_case(cc_list
, name
);
1036 /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
1037 * directory, based on <b>authdir_reject_policy</b>. Else return 0.
1040 authdir_policy_permits_address(uint32_t addr
, uint16_t port
)
1042 if (! addr_policy_permits_address(addr
, port
, authdir_reject_policy
))
1044 return !addr_is_in_cc_list(addr
, get_options()->AuthDirRejectCCs
);
1047 /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
1048 * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
1051 authdir_policy_valid_address(uint32_t addr
, uint16_t port
)
1053 if (! addr_policy_permits_address(addr
, port
, authdir_invalid_policy
))
1055 return !addr_is_in_cc_list(addr
, get_options()->AuthDirInvalidCCs
);
1058 /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
1059 * based on <b>authdir_badexit_policy</b>. Else return 0.
1062 authdir_policy_badexit_address(uint32_t addr
, uint16_t port
)
1064 if (! addr_policy_permits_address(addr
, port
, authdir_badexit_policy
))
1066 return addr_is_in_cc_list(addr
, get_options()->AuthDirBadExitCCs
);
1069 #define REJECT(arg) \
1070 STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
1072 /** Config helper: If there's any problem with the policy configuration
1073 * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
1074 * allocated description of the error. Else return 0. */
1076 validate_addr_policies(const or_options_t
*options
, char **msg
)
1078 /* XXXX Maybe merge this into parse_policies_from_options, to make sure
1079 * that the two can't go out of sync. */
1081 smartlist_t
*addr_policy
=NULL
;
1084 if (policies_parse_exit_policy_from_options(options
,0,NULL
,&addr_policy
)) {
1085 REJECT("Error in ExitPolicy entry.");
1088 static int warned_about_exitrelay
= 0;
1090 const int exitrelay_setting_is_auto
= options
->ExitRelay
== -1;
1091 const int policy_accepts_something
=
1092 ! (policy_is_reject_star(addr_policy
, AF_INET
) &&
1093 policy_is_reject_star(addr_policy
, AF_INET6
));
1095 if (server_mode(options
) &&
1096 ! warned_about_exitrelay
&&
1097 exitrelay_setting_is_auto
&&
1098 policy_accepts_something
) {
1099 /* Policy accepts something */
1100 warned_about_exitrelay
= 1;
1102 "Tor is running as an exit relay%s. If you did not want this "
1103 "behavior, please set the ExitRelay option to 0. If you do "
1104 "want to run an exit Relay, please set the ExitRelay option "
1105 "to 1 to disable this warning, and for forward compatibility.",
1106 options
->ExitPolicy
== NULL
?
1107 " with the default exit policy" : "");
1108 if (options
->ExitPolicy
== NULL
) {
1110 "In a future version of Tor, ExitRelay 0 may become the "
1111 "default when no ExitPolicy is given.");
1115 /* The rest of these calls *append* to addr_policy. So don't actually
1116 * use the results for anything other than checking if they parse! */
1117 if (parse_addr_policy(options
->DirPolicy
, &addr_policy
, -1))
1118 REJECT("Error in DirPolicy entry.");
1119 if (parse_addr_policy(options
->SocksPolicy
, &addr_policy
, -1))
1120 REJECT("Error in SocksPolicy entry.");
1121 if (parse_addr_policy(options
->AuthDirReject
, &addr_policy
,
1122 ADDR_POLICY_REJECT
))
1123 REJECT("Error in AuthDirReject entry.");
1124 if (parse_addr_policy(options
->AuthDirInvalid
, &addr_policy
,
1125 ADDR_POLICY_REJECT
))
1126 REJECT("Error in AuthDirInvalid entry.");
1127 if (parse_addr_policy(options
->AuthDirBadExit
, &addr_policy
,
1128 ADDR_POLICY_REJECT
))
1129 REJECT("Error in AuthDirBadExit entry.");
1131 if (parse_addr_policy(options
->ReachableAddresses
, &addr_policy
,
1132 ADDR_POLICY_ACCEPT
))
1133 REJECT("Error in ReachableAddresses entry.");
1134 if (parse_addr_policy(options
->ReachableORAddresses
, &addr_policy
,
1135 ADDR_POLICY_ACCEPT
))
1136 REJECT("Error in ReachableORAddresses entry.");
1137 if (parse_addr_policy(options
->ReachableDirAddresses
, &addr_policy
,
1138 ADDR_POLICY_ACCEPT
))
1139 REJECT("Error in ReachableDirAddresses entry.");
1142 addr_policy_list_free(addr_policy
);
1143 return *msg
? -1 : 0;
1147 /** Parse <b>string</b> in the same way that the exit policy
1148 * is parsed, and put the processed version in *<b>policy</b>.
1149 * Ignore port specifiers.
1152 load_policy_from_option(config_line_t
*config
, const char *option_name
,
1153 smartlist_t
**policy
,
1157 int killed_any_ports
= 0;
1158 addr_policy_list_free(*policy
);
1160 r
= parse_addr_policy(config
, policy
, assume_action
);
1165 SMARTLIST_FOREACH_BEGIN(*policy
, addr_policy_t
*, n
) {
1166 /* ports aren't used in these. */
1167 if (n
->prt_min
> 1 || n
->prt_max
!= 65535) {
1168 addr_policy_t newp
, *c
;
1169 memcpy(&newp
, n
, sizeof(newp
));
1171 newp
.prt_max
= 65535;
1172 newp
.is_canonical
= 0;
1173 c
= addr_policy_get_canonical_entry(&newp
);
1174 SMARTLIST_REPLACE_CURRENT(*policy
, n
, c
);
1175 addr_policy_free(n
);
1176 killed_any_ports
= 1;
1178 } SMARTLIST_FOREACH_END(n
);
1180 if (killed_any_ports
) {
1181 log_warn(LD_CONFIG
, "Ignoring ports in %s option.", option_name
);
1186 /** Set all policies based on <b>options</b>, which should have been validated
1187 * first by validate_addr_policies. */
1189 policies_parse_from_options(const or_options_t
*options
)
1192 if (load_policy_from_option(options
->SocksPolicy
, "SocksPolicy",
1193 &socks_policy
, -1) < 0)
1195 if (load_policy_from_option(options
->DirPolicy
, "DirPolicy",
1196 &dir_policy
, -1) < 0)
1198 if (load_policy_from_option(options
->AuthDirReject
, "AuthDirReject",
1199 &authdir_reject_policy
, ADDR_POLICY_REJECT
) < 0)
1201 if (load_policy_from_option(options
->AuthDirInvalid
, "AuthDirInvalid",
1202 &authdir_invalid_policy
, ADDR_POLICY_REJECT
) < 0)
1204 if (load_policy_from_option(options
->AuthDirBadExit
, "AuthDirBadExit",
1205 &authdir_badexit_policy
, ADDR_POLICY_REJECT
) < 0)
1207 if (parse_reachable_addresses() < 0)
1212 /** Compare two provided address policy items, and return -1, 0, or 1
1213 * if the first is less than, equal to, or greater than the second. */
1215 cmp_single_addr_policy(addr_policy_t
*a
, addr_policy_t
*b
)
1218 if ((r
=((int)a
->policy_type
- (int)b
->policy_type
)))
1220 if ((r
=((int)a
->is_private
- (int)b
->is_private
)))
1222 /* refcnt and is_canonical are irrelevant to equality,
1223 * they are hash table implementation details */
1224 if ((r
=tor_addr_compare(&a
->addr
, &b
->addr
, CMP_EXACT
)))
1226 if ((r
=((int)a
->maskbits
- (int)b
->maskbits
)))
1228 if ((r
=((int)a
->prt_min
- (int)b
->prt_min
)))
1230 if ((r
=((int)a
->prt_max
- (int)b
->prt_max
)))
1235 /** Like cmp_single_addr_policy() above, but looks at the
1236 * whole set of policies in each case. */
1238 cmp_addr_policies(smartlist_t
*a
, smartlist_t
*b
)
1241 int len_a
= a
? smartlist_len(a
) : 0;
1242 int len_b
= b
? smartlist_len(b
) : 0;
1244 for (i
= 0; i
< len_a
&& i
< len_b
; ++i
) {
1245 if ((r
= cmp_single_addr_policy(smartlist_get(a
, i
), smartlist_get(b
, i
))))
1248 if (i
== len_a
&& i
== len_b
)
1256 /** Node in hashtable used to store address policy entries. */
1257 typedef struct policy_map_ent_t
{
1258 HT_ENTRY(policy_map_ent_t
) node
;
1259 addr_policy_t
*policy
;
1262 /* DOCDOC policy_root */
1263 static HT_HEAD(policy_map
, policy_map_ent_t
) policy_root
= HT_INITIALIZER();
1265 /** Return true iff a and b are equal. */
1267 policy_eq(policy_map_ent_t
*a
, policy_map_ent_t
*b
)
1269 return cmp_single_addr_policy(a
->policy
, b
->policy
) == 0;
1272 /** Return a hashcode for <b>ent</b> */
1274 policy_hash(const policy_map_ent_t
*ent
)
1276 const addr_policy_t
*a
= ent
->policy
;
1278 memset(&aa
, 0, sizeof(aa
));
1280 aa
.prt_min
= a
->prt_min
;
1281 aa
.prt_max
= a
->prt_max
;
1282 aa
.maskbits
= a
->maskbits
;
1283 aa
.policy_type
= a
->policy_type
;
1284 aa
.is_private
= a
->is_private
;
1286 if (a
->is_private
) {
1289 tor_addr_copy_tight(&aa
.addr
, &a
->addr
);
1292 return (unsigned) siphash24g(&aa
, sizeof(aa
));
1295 HT_PROTOTYPE(policy_map
, policy_map_ent_t
, node
, policy_hash
,
1297 HT_GENERATE2(policy_map
, policy_map_ent_t
, node
, policy_hash
,
1298 policy_eq
, 0.6, tor_reallocarray_
, tor_free_
)
1300 /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
1301 * "canonical" copy of that addr_policy_t; the canonical copy is a single
1302 * reference-counted object. */
1304 addr_policy_get_canonical_entry(addr_policy_t
*e
)
1306 policy_map_ent_t search
, *found
;
1307 if (e
->is_canonical
)
1311 found
= HT_FIND(policy_map
, &policy_root
, &search
);
1313 found
= tor_malloc_zero(sizeof(policy_map_ent_t
));
1314 found
->policy
= tor_memdup(e
, sizeof(addr_policy_t
));
1315 found
->policy
->is_canonical
= 1;
1316 found
->policy
->refcnt
= 0;
1317 HT_INSERT(policy_map
, &policy_root
, found
);
1320 tor_assert(!cmp_single_addr_policy(found
->policy
, e
));
1321 ++found
->policy
->refcnt
;
1322 return found
->policy
;
1325 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
1326 * addr and port are both known. */
1327 static addr_policy_result_t
1328 compare_known_tor_addr_to_addr_policy(const tor_addr_t
*addr
, uint16_t port
,
1329 const smartlist_t
*policy
)
1331 /* We know the address and port, and we know the policy, so we can just
1332 * compute an exact match. */
1333 SMARTLIST_FOREACH_BEGIN(policy
, addr_policy_t
*, tmpe
) {
1334 if (tmpe
->addr
.family
== AF_UNSPEC
) {
1335 log_warn(LD_BUG
, "Policy contains an AF_UNSPEC address, which only "
1336 "matches other AF_UNSPEC addresses.");
1338 /* Address is known */
1339 if (!tor_addr_compare_masked(addr
, &tmpe
->addr
, tmpe
->maskbits
,
1341 if (port
>= tmpe
->prt_min
&& port
<= tmpe
->prt_max
) {
1342 /* Exact match for the policy */
1343 return tmpe
->policy_type
== ADDR_POLICY_ACCEPT
?
1344 ADDR_POLICY_ACCEPTED
: ADDR_POLICY_REJECTED
;
1347 } SMARTLIST_FOREACH_END(tmpe
);
1349 /* accept all by default. */
1350 return ADDR_POLICY_ACCEPTED
;
1353 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
1354 * addr is known but port is not. */
1355 static addr_policy_result_t
1356 compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t
*addr
,
1357 const smartlist_t
*policy
)
1359 /* We look to see if there's a definite match. If so, we return that
1360 match's value, unless there's an intervening possible match that says
1361 something different. */
1362 int maybe_accept
= 0, maybe_reject
= 0;
1364 SMARTLIST_FOREACH_BEGIN(policy
, addr_policy_t
*, tmpe
) {
1365 if (tmpe
->addr
.family
== AF_UNSPEC
) {
1366 log_warn(LD_BUG
, "Policy contains an AF_UNSPEC address, which only "
1367 "matches other AF_UNSPEC addresses.");
1369 if (!tor_addr_compare_masked(addr
, &tmpe
->addr
, tmpe
->maskbits
,
1371 if (tmpe
->prt_min
<= 1 && tmpe
->prt_max
>= 65535) {
1372 /* Definitely matches, since it covers all ports. */
1373 if (tmpe
->policy_type
== ADDR_POLICY_ACCEPT
) {
1374 /* If we already hit a clause that might trigger a 'reject', than we
1375 * can't be sure of this certain 'accept'.*/
1376 return maybe_reject
? ADDR_POLICY_PROBABLY_ACCEPTED
:
1377 ADDR_POLICY_ACCEPTED
;
1379 return maybe_accept
? ADDR_POLICY_PROBABLY_REJECTED
:
1380 ADDR_POLICY_REJECTED
;
1384 if (tmpe
->policy_type
== ADDR_POLICY_REJECT
)
1390 } SMARTLIST_FOREACH_END(tmpe
);
1392 /* accept all by default. */
1393 return maybe_reject
? ADDR_POLICY_PROBABLY_ACCEPTED
: ADDR_POLICY_ACCEPTED
;
1396 /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
1397 * port is known but address is not. */
1398 static addr_policy_result_t
1399 compare_unknown_tor_addr_to_addr_policy(uint16_t port
,
1400 const smartlist_t
*policy
)
1402 /* We look to see if there's a definite match. If so, we return that
1403 match's value, unless there's an intervening possible match that says
1404 something different. */
1405 int maybe_accept
= 0, maybe_reject
= 0;
1407 SMARTLIST_FOREACH_BEGIN(policy
, addr_policy_t
*, tmpe
) {
1408 if (tmpe
->addr
.family
== AF_UNSPEC
) {
1409 log_warn(LD_BUG
, "Policy contains an AF_UNSPEC address, which only "
1410 "matches other AF_UNSPEC addresses.");
1412 if (tmpe
->prt_min
<= port
&& port
<= tmpe
->prt_max
) {
1413 if (tmpe
->maskbits
== 0) {
1414 /* Definitely matches, since it covers all addresses. */
1415 if (tmpe
->policy_type
== ADDR_POLICY_ACCEPT
) {
1416 /* If we already hit a clause that might trigger a 'reject', than we
1417 * can't be sure of this certain 'accept'.*/
1418 return maybe_reject
? ADDR_POLICY_PROBABLY_ACCEPTED
:
1419 ADDR_POLICY_ACCEPTED
;
1421 return maybe_accept
? ADDR_POLICY_PROBABLY_REJECTED
:
1422 ADDR_POLICY_REJECTED
;
1426 if (tmpe
->policy_type
== ADDR_POLICY_REJECT
)
1432 } SMARTLIST_FOREACH_END(tmpe
);
1434 /* accept all by default. */
1435 return maybe_reject
? ADDR_POLICY_PROBABLY_ACCEPTED
: ADDR_POLICY_ACCEPTED
;
1438 /** Decide whether a given addr:port is definitely accepted,
1439 * definitely rejected, probably accepted, or probably rejected by a
1440 * given policy. If <b>addr</b> is 0, we don't know the IP of the
1441 * target address. If <b>port</b> is 0, we don't know the port of the
1442 * target address. (At least one of <b>addr</b> and <b>port</b> must be
1443 * provided. If you want to know whether a policy would definitely reject
1444 * an unknown address:port, use policy_is_reject_star().)
1446 * We could do better by assuming that some ranges never match typical
1447 * addresses (127.0.0.1, and so on). But we'll try this for now.
1449 MOCK_IMPL(addr_policy_result_t
,
1450 compare_tor_addr_to_addr_policy
,(const tor_addr_t
*addr
, uint16_t port
,
1451 const smartlist_t
*policy
))
1454 /* no policy? accept all. */
1455 return ADDR_POLICY_ACCEPTED
;
1456 } else if (addr
== NULL
|| tor_addr_is_null(addr
)) {
1458 log_info(LD_BUG
, "Rejecting null address with 0 port (family %d)",
1459 addr
? tor_addr_family(addr
) : -1);
1460 return ADDR_POLICY_REJECTED
;
1462 return compare_unknown_tor_addr_to_addr_policy(port
, policy
);
1463 } else if (port
== 0) {
1464 return compare_known_tor_addr_to_addr_policy_noport(addr
, policy
);
1466 return compare_known_tor_addr_to_addr_policy(addr
, port
, policy
);
1470 /** Return true iff the address policy <b>a</b> covers every case that
1471 * would be covered by <b>b</b>, so that a,b is redundant. */
1473 addr_policy_covers(addr_policy_t
*a
, addr_policy_t
*b
)
1475 if (tor_addr_family(&a
->addr
) != tor_addr_family(&b
->addr
)) {
1476 /* You can't cover a different family. */
1479 /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
1480 * to "accept *:80". */
1481 if (a
->maskbits
> b
->maskbits
) {
1482 /* a has more fixed bits than b; it can't possibly cover b. */
1485 if (tor_addr_compare_masked(&a
->addr
, &b
->addr
, a
->maskbits
, CMP_EXACT
)) {
1486 /* There's a fixed bit in a that's set differently in b. */
1489 return (a
->prt_min
<= b
->prt_min
&& a
->prt_max
>= b
->prt_max
);
1492 /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
1493 * that is, there exists an address/port that is covered by <b>a</b> that
1494 * is also covered by <b>b</b>.
1497 addr_policy_intersects(addr_policy_t
*a
, addr_policy_t
*b
)
1500 /* All the bits we care about are those that are set in both
1501 * netmasks. If they are equal in a and b's networkaddresses
1502 * then the networks intersect. If there is a difference,
1503 * then they do not. */
1504 if (a
->maskbits
< b
->maskbits
)
1505 minbits
= a
->maskbits
;
1507 minbits
= b
->maskbits
;
1508 if (tor_addr_compare_masked(&a
->addr
, &b
->addr
, minbits
, CMP_EXACT
))
1510 if (a
->prt_max
< b
->prt_min
|| b
->prt_max
< a
->prt_min
)
1515 /** Add the exit policy described by <b>more</b> to <b>policy</b>.
1518 append_exit_policy_string(smartlist_t
**policy
, const char *more
)
1523 tmp
.value
= (char*) more
;
1525 if (parse_addr_policy(&tmp
, policy
, -1)<0) {
1526 log_warn(LD_BUG
, "Unable to parse internally generated policy %s",more
);
1530 /** Add "reject <b>addr</b>:*" to <b>dest</b>, creating the list as needed. */
1532 addr_policy_append_reject_addr(smartlist_t
**dest
, const tor_addr_t
*addr
)
1537 addr_policy_t p
, *add
;
1538 memset(&p
, 0, sizeof(p
));
1539 p
.policy_type
= ADDR_POLICY_REJECT
;
1540 p
.maskbits
= tor_addr_family(addr
) == AF_INET6
? 128 : 32;
1541 tor_addr_copy(&p
.addr
, addr
);
1545 add
= addr_policy_get_canonical_entry(&p
);
1547 *dest
= smartlist_new();
1548 smartlist_add(*dest
, add
);
1549 log_debug(LD_CONFIG
, "Adding a reject ExitPolicy 'reject %s:*'",
1553 /* Is addr public for the purposes of rejection? */
1555 tor_addr_is_public_for_reject(const tor_addr_t
*addr
)
1557 return (!tor_addr_is_null(addr
) && !tor_addr_is_internal(addr
, 0)
1558 && !tor_addr_is_multicast(addr
));
1561 /* Add "reject <b>addr</b>:*" to <b>dest</b>, creating the list as needed.
1562 * Filter the address, only adding an IPv4 reject rule if ipv4_rules
1563 * is true, and similarly for ipv6_rules. Check each address returns true for
1564 * tor_addr_is_public_for_reject before adding it.
1567 addr_policy_append_reject_addr_filter(smartlist_t
**dest
,
1568 const tor_addr_t
*addr
,
1575 /* Only reject IP addresses which are public */
1576 if (tor_addr_is_public_for_reject(addr
)) {
1578 /* Reject IPv4 addresses and IPv6 addresses based on the filters */
1579 int is_ipv4
= tor_addr_is_v4(addr
);
1580 if ((is_ipv4
&& ipv4_rules
) || (!is_ipv4
&& ipv6_rules
)) {
1581 addr_policy_append_reject_addr(dest
, addr
);
1586 /** Add "reject addr:*" to <b>dest</b>, for each addr in addrs, creating the
1587 * list as needed. */
1589 addr_policy_append_reject_addr_list(smartlist_t
**dest
,
1590 const smartlist_t
*addrs
)
1595 SMARTLIST_FOREACH_BEGIN(addrs
, tor_addr_t
*, addr
) {
1596 addr_policy_append_reject_addr(dest
, addr
);
1597 } SMARTLIST_FOREACH_END(addr
);
1600 /** Add "reject addr:*" to <b>dest</b>, for each addr in addrs, creating the
1601 * list as needed. Filter using */
1603 addr_policy_append_reject_addr_list_filter(smartlist_t
**dest
,
1604 const smartlist_t
*addrs
,
1611 SMARTLIST_FOREACH_BEGIN(addrs
, tor_addr_t
*, addr
) {
1612 addr_policy_append_reject_addr_filter(dest
, addr
, ipv4_rules
, ipv6_rules
);
1613 } SMARTLIST_FOREACH_END(addr
);
1616 /** Detect and excise "dead code" from the policy *<b>dest</b>. */
1618 exit_policy_remove_redundancies(smartlist_t
*dest
)
1620 addr_policy_t
*ap
, *tmp
;
1623 /* Step one: kill every ipv4 thing after *4:*, every IPv6 thing after *6:*
1626 int kill_v4
=0, kill_v6
=0;
1627 for (i
= 0; i
< smartlist_len(dest
); ++i
) {
1629 ap
= smartlist_get(dest
, i
);
1630 family
= tor_addr_family(&ap
->addr
);
1631 if ((family
== AF_INET
&& kill_v4
) ||
1632 (family
== AF_INET6
&& kill_v6
)) {
1633 smartlist_del_keeporder(dest
, i
--);
1634 addr_policy_free(ap
);
1638 if (ap
->maskbits
== 0 && ap
->prt_min
<= 1 && ap
->prt_max
>= 65535) {
1639 /* This is a catch-all line -- later lines are unreachable. */
1640 if (family
== AF_INET
) {
1642 } else if (family
== AF_INET6
) {
1649 /* Step two: for every entry, see if there's a redundant entry
1650 * later on, and remove it. */
1651 for (i
= 0; i
< smartlist_len(dest
)-1; ++i
) {
1652 ap
= smartlist_get(dest
, i
);
1653 for (j
= i
+1; j
< smartlist_len(dest
); ++j
) {
1654 tmp
= smartlist_get(dest
, j
);
1656 if (addr_policy_covers(ap
, tmp
)) {
1657 char p1
[POLICY_BUF_LEN
], p2
[POLICY_BUF_LEN
];
1658 policy_write_item(p1
, sizeof(p1
), tmp
, 0);
1659 policy_write_item(p2
, sizeof(p2
), ap
, 0);
1660 log_debug(LD_CONFIG
, "Removing exit policy %s (%d). It is made "
1661 "redundant by %s (%d).", p1
, j
, p2
, i
);
1662 smartlist_del_keeporder(dest
, j
--);
1663 addr_policy_free(tmp
);
1668 /* Step three: for every entry A, see if there's an entry B making this one
1669 * redundant later on. This is the case if A and B are of the same type
1670 * (accept/reject), A is a subset of B, and there is no other entry of
1671 * different type in between those two that intersects with A.
1673 * Anybody want to double-check the logic here? XXX
1675 for (i
= 0; i
< smartlist_len(dest
)-1; ++i
) {
1676 ap
= smartlist_get(dest
, i
);
1677 for (j
= i
+1; j
< smartlist_len(dest
); ++j
) {
1678 // tor_assert(j > i); // j starts out at i+1; j only increases; i only
1680 tmp
= smartlist_get(dest
, j
);
1681 if (ap
->policy_type
!= tmp
->policy_type
) {
1682 if (addr_policy_intersects(ap
, tmp
))
1684 } else { /* policy_types are equal. */
1685 if (addr_policy_covers(tmp
, ap
)) {
1686 char p1
[POLICY_BUF_LEN
], p2
[POLICY_BUF_LEN
];
1687 policy_write_item(p1
, sizeof(p1
), ap
, 0);
1688 policy_write_item(p2
, sizeof(p2
), tmp
, 0);
1689 log_debug(LD_CONFIG
, "Removing exit policy %s. It is already "
1690 "covered by %s.", p1
, p2
);
1691 smartlist_del_keeporder(dest
, i
--);
1692 addr_policy_free(ap
);
1700 /** Reject private helper for policies_parse_exit_policy_internal: rejects
1701 * publicly routable addresses on this exit relay.
1703 * Add reject entries to the linked list *dest:
1704 * - if configured_addresses is non-NULL, add entries that reject each
1705 * tor_addr_t* in the list as a destination.
1706 * - if reject_interface_addresses is true, add entries that reject each
1707 * public IPv4 and IPv6 address of each interface on this machine.
1708 * - if reject_configured_port_addresses is true, add entries that reject
1709 * each IPv4 and IPv6 address configured for a port.
1711 * IPv6 entries are only added if ipv6_exit is true. (All IPv6 addresses are
1712 * already blocked by policies_parse_exit_policy_internal if ipv6_exit is
1715 * The list *dest is created as needed.
1718 policies_parse_exit_policy_reject_private(
1721 const smartlist_t
*configured_addresses
,
1722 int reject_interface_addresses
,
1723 int reject_configured_port_addresses
)
1727 /* Reject configured addresses, if they are from public netblocks. */
1728 if (configured_addresses
) {
1729 addr_policy_append_reject_addr_list_filter(dest
, configured_addresses
,
1733 /* Reject configured port addresses, if they are from public netblocks. */
1734 if (reject_configured_port_addresses
) {
1735 const smartlist_t
*port_addrs
= get_configured_ports();
1737 SMARTLIST_FOREACH_BEGIN(port_addrs
, port_cfg_t
*, port
) {
1739 /* Only reject port IP addresses, not port unix sockets */
1740 if (!port
->is_unix_addr
) {
1741 addr_policy_append_reject_addr_filter(dest
, &port
->addr
, 1, ipv6_exit
);
1743 } SMARTLIST_FOREACH_END(port
);
1746 /* Reject local addresses from public netblocks on any interface. */
1747 if (reject_interface_addresses
) {
1748 smartlist_t
*public_addresses
= NULL
;
1750 /* Reject public IPv4 addresses on any interface */
1751 public_addresses
= get_interface_address6_list(LOG_INFO
, AF_INET
, 0);
1752 addr_policy_append_reject_addr_list_filter(dest
, public_addresses
, 1, 0);
1753 free_interface_address6_list(public_addresses
);
1755 /* Don't look for IPv6 addresses if we're configured as IPv4-only */
1757 /* Reject public IPv6 addresses on any interface */
1758 public_addresses
= get_interface_address6_list(LOG_INFO
, AF_INET6
, 0);
1759 addr_policy_append_reject_addr_list_filter(dest
, public_addresses
, 0, 1);
1760 free_interface_address6_list(public_addresses
);
1764 /* If addresses were added multiple times, remove all but one of them. */
1766 exit_policy_remove_redundancies(*dest
);
1771 * Iterate through <b>policy</b> looking for redundant entries. Log a
1772 * warning message with the first redundant entry, if any is found.
1775 policies_log_first_redundant_entry(const smartlist_t
*policy
)
1777 int found_final_effective_entry
= 0;
1778 int first_redundant_entry
= 0;
1780 SMARTLIST_FOREACH_BEGIN(policy
, const addr_policy_t
*, p
) {
1782 int found_ipv4_wildcard
= 0, found_ipv6_wildcard
= 0;
1783 const int i
= p_sl_idx
;
1785 /* Look for accept/reject *[4|6|]:* entires */
1786 if (p
->prt_min
<= 1 && p
->prt_max
== 65535 && p
->maskbits
== 0) {
1787 family
= tor_addr_family(&p
->addr
);
1788 /* accept/reject *:* may have already been expanded into
1789 * accept/reject *4:*,accept/reject *6:*
1790 * But handle both forms.
1792 if (family
== AF_INET
|| family
== AF_UNSPEC
) {
1793 found_ipv4_wildcard
= 1;
1795 if (family
== AF_INET6
|| family
== AF_UNSPEC
) {
1796 found_ipv6_wildcard
= 1;
1800 /* We also find accept *4:*,reject *6:* ; and
1801 * accept *4:*,<other policies>,accept *6:* ; and similar.
1802 * That's ok, because they make any subsequent entries redundant. */
1803 if (found_ipv4_wildcard
&& found_ipv6_wildcard
) {
1804 found_final_effective_entry
= 1;
1805 /* if we're not on the final entry in the list */
1806 if (i
< smartlist_len(policy
) - 1) {
1807 first_redundant_entry
= i
+ 1;
1811 } SMARTLIST_FOREACH_END(p
);
1813 /* Work out if there are redundant trailing entries in the policy list */
1814 if (found_final_effective_entry
&& first_redundant_entry
> 0) {
1815 const addr_policy_t
*p
;
1816 /* Longest possible policy is
1817 * "accept6 ffff:ffff:..255/128:10000-65535",
1818 * which contains a max-length IPv6 address, plus 24 characters. */
1819 char line
[TOR_ADDR_BUF_LEN
+ 32];
1821 tor_assert(first_redundant_entry
< smartlist_len(policy
));
1822 p
= smartlist_get(policy
, first_redundant_entry
);
1823 /* since we've already parsed the policy into an addr_policy_t struct,
1824 * we might not log exactly what the user typed in */
1825 policy_write_item(line
, TOR_ADDR_BUF_LEN
+ 32, p
, 0);
1826 log_warn(LD_DIR
, "Exit policy '%s' and all following policies are "
1827 "redundant, as it follows accept/reject *:* rules for both "
1828 "IPv4 and IPv6. They will be removed from the exit policy. (Use "
1829 "accept/reject *:* as the last entry in any exit policy.)",
1834 #define DEFAULT_EXIT_POLICY \
1835 "reject *:25,reject *:119,reject *:135-139,reject *:445," \
1836 "reject *:563,reject *:1214,reject *:4661-4666," \
1837 "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
1839 /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>.
1841 * If <b>ipv6_exit</b> is false, prepend "reject *6:*" to the policy.
1843 * If <b>rejectprivate</b> is true:
1844 * - prepend "reject private:*" to the policy.
1845 * - prepend entries that reject publicly routable addresses on this exit
1846 * relay by calling policies_parse_exit_policy_reject_private
1848 * If cfg doesn't end in an absolute accept or reject and if
1849 * <b>add_default_policy</b> is true, add the default exit
1850 * policy afterwards.
1852 * Return -1 if we can't parse cfg, else return 0.
1854 * This function is used to parse the exit policy from our torrc. For
1855 * the functions used to parse the exit policy from a router descriptor,
1856 * see router_add_exit_policy.
1859 policies_parse_exit_policy_internal(config_line_t
*cfg
,
1863 const smartlist_t
*configured_addresses
,
1864 int reject_interface_addresses
,
1865 int reject_configured_port_addresses
,
1866 int add_default_policy
)
1869 append_exit_policy_string(dest
, "reject *6:*");
1871 if (rejectprivate
) {
1872 /* Reject IPv4 and IPv6 reserved private netblocks */
1873 append_exit_policy_string(dest
, "reject private:*");
1874 /* Reject IPv4 and IPv6 publicly routable addresses on this exit relay */
1875 policies_parse_exit_policy_reject_private(
1877 configured_addresses
,
1878 reject_interface_addresses
,
1879 reject_configured_port_addresses
);
1881 if (parse_addr_policy(cfg
, dest
, -1))
1884 /* Before we add the default policy and final rejects, check to see if
1885 * there are any lines after accept *:* or reject *:*. These lines have no
1886 * effect, and are most likely an error. */
1887 policies_log_first_redundant_entry(*dest
);
1889 if (add_default_policy
) {
1890 append_exit_policy_string(dest
, DEFAULT_EXIT_POLICY
);
1892 append_exit_policy_string(dest
, "reject *4:*");
1893 append_exit_policy_string(dest
, "reject *6:*");
1895 exit_policy_remove_redundancies(*dest
);
1900 /** Parse exit policy in <b>cfg</b> into <b>dest</b> smartlist.
1902 * Prepend an entry that rejects all IPv6 destinations unless
1903 * <b>EXIT_POLICY_IPV6_ENABLED</b> bit is set in <b>options</b> bitmask.
1905 * If <b>EXIT_POLICY_REJECT_PRIVATE</b> bit is set in <b>options</b>:
1906 * - prepend an entry that rejects all destinations in all netblocks
1907 * reserved for private use.
1908 * - prepend entries that reject publicly routable addresses on this exit
1909 * relay by calling policies_parse_exit_policy_internal
1911 * If <b>EXIT_POLICY_ADD_DEFAULT</b> bit is set in <b>options</b>, append
1912 * default exit policy entries to <b>result</b> smartlist.
1915 policies_parse_exit_policy(config_line_t
*cfg
, smartlist_t
**dest
,
1916 exit_policy_parser_cfg_t options
,
1917 const smartlist_t
*configured_addresses
)
1919 int ipv6_enabled
= (options
& EXIT_POLICY_IPV6_ENABLED
) ? 1 : 0;
1920 int reject_private
= (options
& EXIT_POLICY_REJECT_PRIVATE
) ? 1 : 0;
1921 int add_default
= (options
& EXIT_POLICY_ADD_DEFAULT
) ? 1 : 0;
1923 return policies_parse_exit_policy_internal(cfg
,dest
,ipv6_enabled
,
1925 configured_addresses
,
1931 /** Helper function that adds a copy of addr to a smartlist as long as it is
1932 * non-NULL and not tor_addr_is_null().
1934 * The caller is responsible for freeing all the tor_addr_t* in the smartlist.
1937 policies_copy_addr_to_smartlist(smartlist_t
*addr_list
, const tor_addr_t
*addr
)
1939 if (addr
&& !tor_addr_is_null(addr
)) {
1940 tor_addr_t
*addr_copy
= tor_malloc(sizeof(tor_addr_t
));
1941 tor_addr_copy(addr_copy
, addr
);
1942 smartlist_add(addr_list
, addr_copy
);
1946 /** Helper function that adds ipv4h_addr to a smartlist as a tor_addr_t *,
1947 * as long as it is not tor_addr_is_null(), by converting it to a tor_addr_t
1948 * and passing it to policies_add_addr_to_smartlist.
1950 * The caller is responsible for freeing all the tor_addr_t* in the smartlist.
1953 policies_copy_ipv4h_to_smartlist(smartlist_t
*addr_list
, uint32_t ipv4h_addr
)
1956 tor_addr_t ipv4_tor_addr
;
1957 tor_addr_from_ipv4h(&ipv4_tor_addr
, ipv4h_addr
);
1958 policies_copy_addr_to_smartlist(addr_list
, &ipv4_tor_addr
);
1962 /** Helper function that adds copies of
1963 * or_options->OutboundBindAddressIPv[4|6]_ to a smartlist as tor_addr_t *, as
1964 * long as or_options is non-NULL, and the addresses are not
1965 * tor_addr_is_null(), by passing them to policies_add_addr_to_smartlist.
1967 * The caller is responsible for freeing all the tor_addr_t* in the smartlist.
1970 policies_copy_outbound_addresses_to_smartlist(smartlist_t
*addr_list
,
1971 const or_options_t
*or_options
)
1974 policies_copy_addr_to_smartlist(addr_list
,
1975 &or_options
->OutboundBindAddressIPv4_
);
1976 policies_copy_addr_to_smartlist(addr_list
,
1977 &or_options
->OutboundBindAddressIPv6_
);
1981 /** Parse <b>ExitPolicy</b> member of <b>or_options</b> into <b>result</b>
1983 * If <b>or_options->IPv6Exit</b> is false, prepend an entry that
1984 * rejects all IPv6 destinations.
1986 * If <b>or_options->ExitPolicyRejectPrivate</b> is true:
1987 * - prepend an entry that rejects all destinations in all netblocks reserved
1989 * - if local_address is non-zero, treat it as a host-order IPv4 address, and
1990 * add it to the list of configured addresses.
1991 * - if ipv6_local_address is non-NULL, and not the null tor_addr_t, add it
1992 * to the list of configured addresses.
1993 * - if or_options->OutboundBindAddressIPv4_ is not the null tor_addr_t, add
1994 * it to the list of configured addresses.
1995 * - if or_options->OutboundBindAddressIPv6_ is not the null tor_addr_t, add
1996 * it to the list of configured addresses.
1998 * If <b>or_options->BridgeRelay</b> is false, append entries of default
1999 * Tor exit policy into <b>result</b> smartlist.
2001 * If or_options->ExitRelay is false, then make our exit policy into
2002 * "reject *:*" regardless.
2005 policies_parse_exit_policy_from_options(const or_options_t
*or_options
,
2006 uint32_t local_address
,
2007 const tor_addr_t
*ipv6_local_address
,
2008 smartlist_t
**result
)
2010 exit_policy_parser_cfg_t parser_cfg
= 0;
2011 smartlist_t
*configured_addresses
= NULL
;
2014 /* Short-circuit for non-exit relays */
2015 if (or_options
->ExitRelay
== 0) {
2016 append_exit_policy_string(result
, "reject *4:*");
2017 append_exit_policy_string(result
, "reject *6:*");
2021 configured_addresses
= smartlist_new();
2023 /* Configure the parser */
2024 if (or_options
->IPv6Exit
) {
2025 parser_cfg
|= EXIT_POLICY_IPV6_ENABLED
;
2028 if (or_options
->ExitPolicyRejectPrivate
) {
2029 parser_cfg
|= EXIT_POLICY_REJECT_PRIVATE
;
2032 if (!or_options
->BridgeRelay
) {
2033 parser_cfg
|= EXIT_POLICY_ADD_DEFAULT
;
2036 /* Copy the configured addresses into the tor_addr_t* list */
2037 policies_copy_ipv4h_to_smartlist(configured_addresses
, local_address
);
2038 policies_copy_addr_to_smartlist(configured_addresses
, ipv6_local_address
);
2039 policies_copy_outbound_addresses_to_smartlist(configured_addresses
,
2042 rv
= policies_parse_exit_policy(or_options
->ExitPolicy
, result
, parser_cfg
,
2043 configured_addresses
);
2045 SMARTLIST_FOREACH(configured_addresses
, tor_addr_t
*, a
, tor_free(a
));
2046 smartlist_free(configured_addresses
);
2051 /** Add "reject *:*" to the end of the policy in *<b>dest</b>, allocating
2052 * *<b>dest</b> as needed. */
2054 policies_exit_policy_append_reject_star(smartlist_t
**dest
)
2056 append_exit_policy_string(dest
, "reject *4:*");
2057 append_exit_policy_string(dest
, "reject *6:*");
2060 /** Replace the exit policy of <b>node</b> with reject *:* */
2062 policies_set_node_exitpolicy_to_reject_all(node_t
*node
)
2064 node
->rejects_all
= 1;
2067 /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
2068 * allows exiting to <b>port</b>. Otherwise, return 0. */
2070 exit_policy_is_general_exit_helper(smartlist_t
*policy
, int port
)
2072 uint32_t mask
, ip
, i
;
2073 /* Is this /8 rejected (1), or undecided (0)? */
2074 char subnet_status
[256];
2076 memset(subnet_status
, 0, sizeof(subnet_status
));
2077 SMARTLIST_FOREACH_BEGIN(policy
, addr_policy_t
*, p
) {
2078 if (tor_addr_family(&p
->addr
) != AF_INET
)
2079 continue; /* IPv4 only for now */
2080 if (p
->prt_min
> port
|| p
->prt_max
< port
)
2081 continue; /* Doesn't cover our port. */
2083 tor_assert(p
->maskbits
<= 32);
2086 mask
= UINT32_MAX
<<(32-p
->maskbits
);
2087 ip
= tor_addr_to_ipv4h(&p
->addr
);
2089 /* Calculate the first and last subnet that this exit policy touches
2090 * and set it as loop boundaries. */
2091 for (i
= ((mask
& ip
)>>24); i
<= (~((mask
& ip
) ^ mask
)>>24); ++i
) {
2093 if (subnet_status
[i
] != 0)
2094 continue; /* We already reject some part of this /8 */
2095 tor_addr_from_ipv4h(&addr
, i
<<24);
2096 if (tor_addr_is_internal(&addr
, 0))
2097 continue; /* Local or non-routable addresses */
2098 if (p
->policy_type
== ADDR_POLICY_ACCEPT
) {
2099 if (p
->maskbits
> 8)
2100 continue; /* Narrower than a /8. */
2101 /* We found an allowed subnet of at least size /8. Done
2104 } else if (p
->policy_type
== ADDR_POLICY_REJECT
) {
2105 subnet_status
[i
] = 1;
2108 } SMARTLIST_FOREACH_END(p
);
2112 /** Return true iff <b>ri</b> is "useful as an exit node", meaning
2113 * it allows exit to at least one /8 address space for at least
2114 * two of ports 80, 443, and 6667. */
2116 exit_policy_is_general_exit(smartlist_t
*policy
)
2118 static const int ports
[] = { 80, 443, 6667 };
2121 if (!policy
) /*XXXX disallow NULL policies? */
2124 for (i
= 0; i
< 3; ++i
) {
2125 n_allowed
+= exit_policy_is_general_exit_helper(policy
, ports
[i
]);
2127 return n_allowed
>= 2;
2130 /** Return false if <b>policy</b> might permit access to some addr:port;
2131 * otherwise if we are certain it rejects everything, return true. */
2133 policy_is_reject_star(const smartlist_t
*policy
, sa_family_t family
)
2135 if (!policy
) /*XXXX disallow NULL policies? */
2137 SMARTLIST_FOREACH_BEGIN(policy
, addr_policy_t
*, p
) {
2138 if (p
->policy_type
== ADDR_POLICY_ACCEPT
&&
2139 (tor_addr_family(&p
->addr
) == family
||
2140 tor_addr_family(&p
->addr
) == AF_UNSPEC
)) {
2142 } else if (p
->policy_type
== ADDR_POLICY_REJECT
&&
2143 p
->prt_min
<= 1 && p
->prt_max
== 65535 &&
2145 (tor_addr_family(&p
->addr
) == family
||
2146 tor_addr_family(&p
->addr
) == AF_UNSPEC
)) {
2149 } SMARTLIST_FOREACH_END(p
);
2153 /** Write a single address policy to the buf_len byte buffer at buf. Return
2154 * the number of characters written, or -1 on failure. */
2156 policy_write_item(char *buf
, size_t buflen
, const addr_policy_t
*policy
,
2157 int format_for_desc
)
2160 char addrbuf
[TOR_ADDR_BUF_LEN
];
2161 const char *addrpart
;
2163 const int is_accept
= policy
->policy_type
== ADDR_POLICY_ACCEPT
;
2164 const sa_family_t family
= tor_addr_family(&policy
->addr
);
2165 const int is_ip6
= (family
== AF_INET6
);
2167 tor_addr_to_str(addrbuf
, &policy
->addr
, sizeof(addrbuf
), 1);
2169 /* write accept/reject 1.2.3.4 */
2170 if (policy
->is_private
) {
2171 addrpart
= "private";
2172 } else if (policy
->maskbits
== 0) {
2173 if (format_for_desc
)
2175 else if (family
== AF_INET6
)
2177 else if (family
== AF_INET
)
2185 result
= tor_snprintf(buf
, buflen
, "%s%s %s",
2186 is_accept
? "accept" : "reject",
2187 (is_ip6
&&format_for_desc
)?"6":"",
2191 written
+= strlen(buf
);
2192 /* If the maskbits is 32 (IPv4) or 128 (IPv6) we don't need to give it. If
2193 the mask is 0, we already wrote "*". */
2194 if (policy
->maskbits
< (is_ip6
?128:32) && policy
->maskbits
> 0) {
2195 if (tor_snprintf(buf
+written
, buflen
-written
, "/%d", policy
->maskbits
)<0)
2197 written
+= strlen(buf
+written
);
2199 if (policy
->prt_min
<= 1 && policy
->prt_max
== 65535) {
2200 /* There is no port set; write ":*" */
2201 if (written
+4 > buflen
)
2203 strlcat(buf
+written
, ":*", buflen
-written
);
2205 } else if (policy
->prt_min
== policy
->prt_max
) {
2206 /* There is only one port; write ":80". */
2207 result
= tor_snprintf(buf
+written
, buflen
-written
, ":%d", policy
->prt_min
);
2212 /* There is a range of ports; write ":79-80". */
2213 result
= tor_snprintf(buf
+written
, buflen
-written
, ":%d-%d",
2214 policy
->prt_min
, policy
->prt_max
);
2219 if (written
< buflen
)
2220 buf
[written
] = '\0';
2224 return (int)written
;
2227 /** Create a new exit policy summary, initially only with a single
2228 * port 1-64k item */
2229 /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
2230 * RB-tree if that turns out to matter. */
2231 static smartlist_t
*
2232 policy_summary_create(void)
2234 smartlist_t
*summary
;
2235 policy_summary_item_t
* item
;
2237 item
= tor_malloc_zero(sizeof(policy_summary_item_t
));
2239 item
->prt_max
= 65535;
2240 item
->reject_count
= 0;
2243 summary
= smartlist_new();
2244 smartlist_add(summary
, item
);
2249 /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
2250 * The current item is changed to end at new-starts - 1, the new item
2251 * copies reject_count and accepted from the old item,
2252 * starts at new_starts and ends at the port where the original item
2255 static policy_summary_item_t
*
2256 policy_summary_item_split(policy_summary_item_t
* old
, uint16_t new_starts
)
2258 policy_summary_item_t
* new;
2260 new = tor_malloc_zero(sizeof(policy_summary_item_t
));
2261 new->prt_min
= new_starts
;
2262 new->prt_max
= old
->prt_max
;
2263 new->reject_count
= old
->reject_count
;
2264 new->accepted
= old
->accepted
;
2266 old
->prt_max
= new_starts
-1;
2268 tor_assert(old
->prt_min
<= old
->prt_max
);
2269 tor_assert(new->prt_min
<= new->prt_max
);
2273 /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
2274 * my immortal soul, he can clean it up himself. */
2275 #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
2277 #define REJECT_CUTOFF_COUNT (1<<25)
2278 /** Split an exit policy summary so that prt_min and prt_max
2279 * fall at exactly the start and end of an item respectively.
2282 policy_summary_split(smartlist_t
*summary
,
2283 uint16_t prt_min
, uint16_t prt_max
)
2289 while (AT(i
)->prt_max
< prt_min
)
2291 if (AT(i
)->prt_min
!= prt_min
) {
2292 policy_summary_item_t
* new_item
;
2293 new_item
= policy_summary_item_split(AT(i
), prt_min
);
2294 smartlist_insert(summary
, i
+1, new_item
);
2299 while (AT(i
)->prt_max
< prt_max
)
2301 if (AT(i
)->prt_max
!= prt_max
) {
2302 policy_summary_item_t
* new_item
;
2303 new_item
= policy_summary_item_split(AT(i
), prt_max
+1);
2304 smartlist_insert(summary
, i
+1, new_item
);
2307 return start_at_index
;
2310 /** Mark port ranges as accepted if they are below the reject_count */
2312 policy_summary_accept(smartlist_t
*summary
,
2313 uint16_t prt_min
, uint16_t prt_max
)
2315 int i
= policy_summary_split(summary
, prt_min
, prt_max
);
2316 while (i
< smartlist_len(summary
) &&
2317 AT(i
)->prt_max
<= prt_max
) {
2318 if (!AT(i
)->accepted
&&
2319 AT(i
)->reject_count
<= REJECT_CUTOFF_COUNT
)
2320 AT(i
)->accepted
= 1;
2323 tor_assert(i
< smartlist_len(summary
) || prt_max
==65535);
2326 /** Count the number of addresses in a network with prefixlen maskbits
2327 * against the given portrange. */
2329 policy_summary_reject(smartlist_t
*summary
,
2330 maskbits_t maskbits
,
2331 uint16_t prt_min
, uint16_t prt_max
)
2333 int i
= policy_summary_split(summary
, prt_min
, prt_max
);
2334 /* XXX: ipv4 specific */
2335 uint64_t count
= (U64_LITERAL(1) << (32-maskbits
));
2336 while (i
< smartlist_len(summary
) &&
2337 AT(i
)->prt_max
<= prt_max
) {
2338 AT(i
)->reject_count
+= count
;
2341 tor_assert(i
< smartlist_len(summary
) || prt_max
==65535);
2344 /** Add a single exit policy item to our summary:
2345 * If it is an accept ignore it unless it is for all IP addresses
2346 * ("*"), i.e. it's prefixlen/maskbits is 0, else call
2347 * policy_summary_accept().
2348 * If it's a reject ignore it if it is about one of the private
2349 * networks, else call policy_summary_reject().
2352 policy_summary_add_item(smartlist_t
*summary
, addr_policy_t
*p
)
2354 if (p
->policy_type
== ADDR_POLICY_ACCEPT
) {
2355 if (p
->maskbits
== 0) {
2356 policy_summary_accept(summary
, p
->prt_min
, p
->prt_max
);
2358 } else if (p
->policy_type
== ADDR_POLICY_REJECT
) {
2362 for (i
= 0; private_nets
[i
]; ++i
) {
2364 maskbits_t maskbits
;
2365 if (tor_addr_parse_mask_ports(private_nets
[i
], 0, &addr
,
2366 &maskbits
, NULL
, NULL
)<0) {
2369 if (tor_addr_compare(&p
->addr
, &addr
, CMP_EXACT
) == 0 &&
2370 p
->maskbits
== maskbits
) {
2377 policy_summary_reject(summary
, p
->maskbits
, p
->prt_min
, p
->prt_max
);
2383 /** Create a string representing a summary for an exit policy.
2384 * The summary will either be an "accept" plus a comma-separated list of port
2385 * ranges or a "reject" plus port-ranges, depending on which is shorter.
2387 * If no exits are allowed at all then "reject 1-65535" is returned. If no
2388 * ports are blocked instead of "reject " we return "accept 1-65535". (These
2389 * are an exception to the shorter-representation-wins rule).
2392 policy_summarize(smartlist_t
*policy
, sa_family_t family
)
2394 smartlist_t
*summary
= policy_summary_create();
2395 smartlist_t
*accepts
, *rejects
;
2396 int i
, last
, start_prt
;
2397 size_t accepts_len
, rejects_len
;
2398 char *accepts_str
= NULL
, *rejects_str
= NULL
, *shorter_str
, *result
;
2403 /* Create the summary list */
2404 SMARTLIST_FOREACH_BEGIN(policy
, addr_policy_t
*, p
) {
2405 sa_family_t f
= tor_addr_family(&p
->addr
);
2406 if (f
!= AF_INET
&& f
!= AF_INET6
) {
2407 log_warn(LD_BUG
, "Weird family when summarizing address policy");
2411 /* XXXX-ipv6 More family work is needed */
2412 policy_summary_add_item(summary
, p
);
2413 } SMARTLIST_FOREACH_END(p
);
2415 /* Now create two lists of strings, one for accepted and one
2416 * for rejected ports. We take care to merge ranges so that
2417 * we avoid getting stuff like "1-4,5-9,10", instead we want
2422 accepts
= smartlist_new();
2423 rejects
= smartlist_new();
2425 last
= i
== smartlist_len(summary
)-1;
2427 AT(i
)->accepted
!= AT(i
+1)->accepted
) {
2428 char buf
[POLICY_BUF_LEN
];
2430 if (start_prt
== AT(i
)->prt_max
)
2431 tor_snprintf(buf
, sizeof(buf
), "%d", start_prt
);
2433 tor_snprintf(buf
, sizeof(buf
), "%d-%d", start_prt
, AT(i
)->prt_max
);
2435 if (AT(i
)->accepted
)
2436 smartlist_add(accepts
, tor_strdup(buf
));
2438 smartlist_add(rejects
, tor_strdup(buf
));
2443 start_prt
= AT(i
+1)->prt_min
;
2448 /* Figure out which of the two stringlists will be shorter and use
2449 * that to build the result
2451 if (smartlist_len(accepts
) == 0) { /* no exits at all */
2452 result
= tor_strdup("reject 1-65535");
2455 if (smartlist_len(rejects
) == 0) { /* no rejects at all */
2456 result
= tor_strdup("accept 1-65535");
2460 accepts_str
= smartlist_join_strings(accepts
, ",", 0, &accepts_len
);
2461 rejects_str
= smartlist_join_strings(rejects
, ",", 0, &rejects_len
);
2463 if (rejects_len
> MAX_EXITPOLICY_SUMMARY_LEN
-strlen("reject")-1 &&
2464 accepts_len
> MAX_EXITPOLICY_SUMMARY_LEN
-strlen("accept")-1) {
2466 shorter_str
= accepts_str
;
2469 c
= shorter_str
+ (MAX_EXITPOLICY_SUMMARY_LEN
-strlen(prefix
)-1);
2470 while (*c
!= ',' && c
>= shorter_str
)
2472 tor_assert(c
>= shorter_str
);
2473 tor_assert(*c
== ',');
2476 } else if (rejects_len
< accepts_len
) {
2477 shorter_str
= rejects_str
;
2480 shorter_str
= accepts_str
;
2484 tor_asprintf(&result
, "%s %s", prefix
, shorter_str
);
2488 SMARTLIST_FOREACH(summary
, policy_summary_item_t
*, s
, tor_free(s
));
2489 smartlist_free(summary
);
2491 tor_free(accepts_str
);
2492 SMARTLIST_FOREACH(accepts
, char *, s
, tor_free(s
));
2493 smartlist_free(accepts
);
2495 tor_free(rejects_str
);
2496 SMARTLIST_FOREACH(rejects
, char *, s
, tor_free(s
));
2497 smartlist_free(rejects
);
2502 /** Convert a summarized policy string into a short_policy_t. Return NULL
2503 * if the string is not well-formed. */
2505 parse_short_policy(const char *summary
)
2507 const char *orig_summary
= summary
;
2508 short_policy_t
*result
;
2511 short_policy_entry_t entries
[MAX_EXITPOLICY_SUMMARY_LEN
]; /* overkill */
2514 if (!strcmpstart(summary
, "accept ")) {
2516 summary
+= strlen("accept ");
2517 } else if (!strcmpstart(summary
, "reject ")) {
2519 summary
+= strlen("reject ");
2521 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
, "Unrecognized policy summary keyword");
2526 for ( ; *summary
; summary
= next
) {
2527 const char *comma
= strchr(summary
, ',');
2533 next
= comma
? comma
+1 : strchr(summary
, '\0');
2534 len
= comma
? (size_t)(comma
- summary
) : strlen(summary
);
2536 if (n_entries
== MAX_EXITPOLICY_SUMMARY_LEN
) {
2537 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
, "Impossibly long policy summary %s",
2538 escaped(orig_summary
));
2542 if (! TOR_ISDIGIT(*summary
) || len
> (sizeof(ent_buf
)-1)) {
2543 /* unrecognized entry format. skip it. */
2547 /* empty; skip it. */
2548 /* XXX This happens to be unreachable, since if len==0, then *summary is
2549 * ',' or '\0', and the TOR_ISDIGIT test above would have failed. */
2553 memcpy(ent_buf
, summary
, len
);
2554 ent_buf
[len
] = '\0';
2556 if (tor_sscanf(ent_buf
, "%u-%u%c", &low
, &high
, &dummy
) == 2) {
2557 if (low
<1 || low
>65535 || high
<1 || high
>65535 || low
>high
) {
2558 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
,
2559 "Found bad entry in policy summary %s", escaped(orig_summary
));
2562 } else if (tor_sscanf(ent_buf
, "%u%c", &low
, &dummy
) == 1) {
2563 if (low
<1 || low
>65535) {
2564 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
,
2565 "Found bad entry in policy summary %s", escaped(orig_summary
));
2570 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
,"Found bad entry in policy summary %s",
2571 escaped(orig_summary
));
2575 entries
[n_entries
].min_port
= low
;
2576 entries
[n_entries
].max_port
= high
;
2580 if (n_entries
== 0) {
2581 log_fn(LOG_PROTOCOL_WARN
, LD_DIR
,
2582 "Found no port-range entries in summary %s", escaped(orig_summary
));
2587 size_t size
= STRUCT_OFFSET(short_policy_t
, entries
) +
2588 sizeof(short_policy_entry_t
)*(n_entries
);
2589 result
= tor_malloc_zero(size
);
2591 tor_assert( (char*)&result
->entries
[n_entries
-1] < ((char*)result
)+size
);
2594 result
->is_accept
= is_accept
;
2595 result
->n_entries
= n_entries
;
2596 memcpy(result
->entries
, entries
, sizeof(short_policy_entry_t
)*n_entries
);
2600 /** Write <b>policy</b> back out into a string. Used only for unit tests
2603 write_short_policy(const short_policy_t
*policy
)
2607 smartlist_t
*sl
= smartlist_new();
2609 smartlist_add_asprintf(sl
, "%s", policy
->is_accept
? "accept " : "reject ");
2611 for (i
=0; i
< policy
->n_entries
; i
++) {
2612 const short_policy_entry_t
*e
= &policy
->entries
[i
];
2613 if (e
->min_port
== e
->max_port
) {
2614 smartlist_add_asprintf(sl
, "%d", e
->min_port
);
2616 smartlist_add_asprintf(sl
, "%d-%d", e
->min_port
, e
->max_port
);
2618 if (i
< policy
->n_entries
-1)
2619 smartlist_add(sl
, tor_strdup(","));
2621 answer
= smartlist_join_strings(sl
, "", 0, NULL
);
2622 SMARTLIST_FOREACH(sl
, char *, a
, tor_free(a
));
2627 /** Release all storage held in <b>policy</b>. */
2629 short_policy_free(short_policy_t
*policy
)
2634 /** See whether the <b>addr</b>:<b>port</b> address is likely to be accepted
2635 * or rejected by the summarized policy <b>policy</b>. Return values are as
2636 * for compare_tor_addr_to_addr_policy. Unlike the regular addr_policy
2637 * functions, requires the <b>port</b> be specified. */
2638 addr_policy_result_t
2639 compare_tor_addr_to_short_policy(const tor_addr_t
*addr
, uint16_t port
,
2640 const short_policy_t
*policy
)
2643 int found_match
= 0;
2646 tor_assert(port
!= 0);
2648 if (addr
&& tor_addr_is_null(addr
))
2649 addr
= NULL
; /* Unspec means 'no address at all,' in this context. */
2651 if (addr
&& get_options()->ClientRejectInternalAddresses
&&
2652 (tor_addr_is_internal(addr
, 0) || tor_addr_is_loopback(addr
)))
2653 return ADDR_POLICY_REJECTED
;
2655 for (i
=0; i
< policy
->n_entries
; ++i
) {
2656 const short_policy_entry_t
*e
= &policy
->entries
[i
];
2657 if (e
->min_port
<= port
&& port
<= e
->max_port
) {
2664 accept
= policy
->is_accept
;
2666 accept
= ! policy
->is_accept
;
2668 /* ???? are these right? -NM */
2669 /* We should be sure not to return ADDR_POLICY_ACCEPTED in the accept
2670 * case here, because it would cause clients to believe that the node
2671 * allows exit enclaving. Trying it anyway would open up a cool attack
2672 * where the node refuses due to exitpolicy, the client reacts in
2673 * surprise by rewriting the node's exitpolicy to reject *:*, and then
2674 * a bad guy targets users by causing them to attempt such connections
2675 * to 98% of the exits.
2677 * Once microdescriptors can handle addresses in special cases (e.g. if
2678 * we ever solve ticket 1774), we can provide certainty here. -RD */
2680 return ADDR_POLICY_PROBABLY_ACCEPTED
;
2682 return ADDR_POLICY_REJECTED
;
2685 /** Return true iff <b>policy</b> seems reject all ports */
2687 short_policy_is_reject_star(const short_policy_t
*policy
)
2689 /* This doesn't need to be as much on the lookout as policy_is_reject_star,
2690 * since policy summaries are from the consensus or from consensus
2694 /* Check for an exact match of "reject 1-65535". */
2695 return (policy
->is_accept
== 0 && policy
->n_entries
== 1 &&
2696 policy
->entries
[0].min_port
== 1 &&
2697 policy
->entries
[0].max_port
== 65535);
2700 /** Decide whether addr:port is probably or definitely accepted or rejected by
2701 * <b>node</b>. See compare_tor_addr_to_addr_policy for details on addr/port
2702 * interpretation. */
2703 addr_policy_result_t
2704 compare_tor_addr_to_node_policy(const tor_addr_t
*addr
, uint16_t port
,
2707 if (node
->rejects_all
)
2708 return ADDR_POLICY_REJECTED
;
2710 if (addr
&& tor_addr_family(addr
) == AF_INET6
) {
2711 const short_policy_t
*p
= NULL
;
2713 p
= node
->ri
->ipv6_exit_policy
;
2715 p
= node
->md
->ipv6_exit_policy
;
2717 return compare_tor_addr_to_short_policy(addr
, port
, p
);
2719 return ADDR_POLICY_REJECTED
;
2723 return compare_tor_addr_to_addr_policy(addr
, port
, node
->ri
->exit_policy
);
2724 } else if (node
->md
) {
2725 if (node
->md
->exit_policy
== NULL
)
2726 return ADDR_POLICY_REJECTED
;
2728 return compare_tor_addr_to_short_policy(addr
, port
,
2729 node
->md
->exit_policy
);
2731 return ADDR_POLICY_PROBABLY_REJECTED
;
2736 * Given <b>policy_list</b>, a list of addr_policy_t, produce a string
2737 * representation of the list.
2738 * If <b>include_ipv4</b> is true, include IPv4 entries.
2739 * If <b>include_ipv6</b> is true, include IPv6 entries.
2742 policy_dump_to_string(const smartlist_t
*policy_list
,
2746 smartlist_t
*policy_string_list
;
2747 char *policy_string
= NULL
;
2749 policy_string_list
= smartlist_new();
2751 SMARTLIST_FOREACH_BEGIN(policy_list
, addr_policy_t
*, tmpe
) {
2753 int bytes_written_to_pbuf
;
2754 if ((tor_addr_family(&tmpe
->addr
) == AF_INET6
) && (!include_ipv6
)) {
2755 continue; /* Don't include IPv6 parts of address policy */
2757 if ((tor_addr_family(&tmpe
->addr
) == AF_INET
) && (!include_ipv4
)) {
2758 continue; /* Don't include IPv4 parts of address policy */
2761 pbuf
= tor_malloc(POLICY_BUF_LEN
);
2762 bytes_written_to_pbuf
= policy_write_item(pbuf
,POLICY_BUF_LEN
, tmpe
, 1);
2764 if (bytes_written_to_pbuf
< 0) {
2765 log_warn(LD_BUG
, "policy_dump_to_string ran out of room!");
2770 smartlist_add(policy_string_list
,pbuf
);
2771 } SMARTLIST_FOREACH_END(tmpe
);
2773 policy_string
= smartlist_join_strings(policy_string_list
, "\n", 0, NULL
);
2776 SMARTLIST_FOREACH(policy_string_list
, char *, str
, tor_free(str
));
2777 smartlist_free(policy_string_list
);
2779 return policy_string
;
2782 /** Implementation for GETINFO control command: knows the answer for questions
2783 * about "exit-policy/..." */
2785 getinfo_helper_policies(control_connection_t
*conn
,
2786 const char *question
, char **answer
,
2787 const char **errmsg
)
2791 if (!strcmp(question
, "exit-policy/default")) {
2792 *answer
= tor_strdup(DEFAULT_EXIT_POLICY
);
2793 } else if (!strcmp(question
, "exit-policy/reject-private/default")) {
2794 smartlist_t
*private_policy_strings
;
2795 const char **priv
= private_nets
;
2797 private_policy_strings
= smartlist_new();
2799 while (*priv
!= NULL
) {
2800 /* IPv6 addresses are in "[]" and contain ":",
2801 * IPv4 addresses are not in "[]" and contain "." */
2802 smartlist_add_asprintf(private_policy_strings
, "reject %s:*", *priv
);
2806 *answer
= smartlist_join_strings(private_policy_strings
,
2809 SMARTLIST_FOREACH(private_policy_strings
, char *, str
, tor_free(str
));
2810 smartlist_free(private_policy_strings
);
2811 } else if (!strcmp(question
, "exit-policy/reject-private/relay")) {
2812 const or_options_t
*options
= get_options();
2813 const routerinfo_t
*me
= router_get_my_routerinfo();
2816 *errmsg
= "router_get_my_routerinfo returned NULL";
2820 if (!options
->ExitPolicyRejectPrivate
) {
2821 *answer
= tor_strdup("");
2825 smartlist_t
*private_policy_list
= smartlist_new();
2826 smartlist_t
*configured_addresses
= smartlist_new();
2828 /* Copy the configured addresses into the tor_addr_t* list */
2829 policies_copy_ipv4h_to_smartlist(configured_addresses
, me
->addr
);
2830 policies_copy_addr_to_smartlist(configured_addresses
, &me
->ipv6_addr
);
2831 policies_copy_outbound_addresses_to_smartlist(configured_addresses
,
2834 policies_parse_exit_policy_reject_private(
2835 &private_policy_list
,
2837 configured_addresses
,
2839 *answer
= policy_dump_to_string(private_policy_list
, 1, 1);
2841 addr_policy_list_free(private_policy_list
);
2842 SMARTLIST_FOREACH(configured_addresses
, tor_addr_t
*, a
, tor_free(a
));
2843 smartlist_free(configured_addresses
);
2844 } else if (!strcmpstart(question
, "exit-policy/")) {
2845 const routerinfo_t
*me
= router_get_my_routerinfo();
2847 int include_ipv4
= 0;
2848 int include_ipv6
= 0;
2850 if (!strcmp(question
, "exit-policy/ipv4")) {
2852 } else if (!strcmp(question
, "exit-policy/ipv6")) {
2854 } else if (!strcmp(question
, "exit-policy/full")) {
2855 include_ipv4
= include_ipv6
= 1;
2857 return 0; /* No such key. */
2861 *errmsg
= "router_get_my_routerinfo returned NULL";
2865 *answer
= router_dump_exit_policy_to_string(me
,include_ipv4
,include_ipv6
);
2870 /** Release all storage held by <b>p</b>. */
2872 addr_policy_list_free(smartlist_t
*lst
)
2876 SMARTLIST_FOREACH(lst
, addr_policy_t
*, policy
, addr_policy_free(policy
));
2877 smartlist_free(lst
);
2880 /** Release all storage held by <b>p</b>. */
2882 addr_policy_free(addr_policy_t
*p
)
2887 if (--p
->refcnt
<= 0) {
2888 if (p
->is_canonical
) {
2889 policy_map_ent_t search
, *found
;
2891 found
= HT_REMOVE(policy_map
, &policy_root
, &search
);
2893 tor_assert(p
== found
->policy
);
2901 /** Release all storage held by policy variables. */
2903 policies_free_all(void)
2905 addr_policy_list_free(reachable_or_addr_policy
);
2906 reachable_or_addr_policy
= NULL
;
2907 addr_policy_list_free(reachable_dir_addr_policy
);
2908 reachable_dir_addr_policy
= NULL
;
2909 addr_policy_list_free(socks_policy
);
2910 socks_policy
= NULL
;
2911 addr_policy_list_free(dir_policy
);
2913 addr_policy_list_free(authdir_reject_policy
);
2914 authdir_reject_policy
= NULL
;
2915 addr_policy_list_free(authdir_invalid_policy
);
2916 authdir_invalid_policy
= NULL
;
2917 addr_policy_list_free(authdir_badexit_policy
);
2918 authdir_badexit_policy
= NULL
;
2920 if (!HT_EMPTY(&policy_root
)) {
2921 policy_map_ent_t
**ent
;
2923 char buf
[POLICY_BUF_LEN
];
2925 log_warn(LD_MM
, "Still had %d address policies cached at shutdown.",
2926 (int)HT_SIZE(&policy_root
));
2928 /* Note the first 10 cached policies to try to figure out where they
2929 * might be coming from. */
2930 HT_FOREACH(ent
, policy_map
, &policy_root
) {
2933 if (policy_write_item(buf
, sizeof(buf
), (*ent
)->policy
, 0) >= 0)
2934 log_warn(LD_MM
," %d [%d]: %s", n
, (*ent
)->policy
->refcnt
, buf
);
2937 HT_CLEAR(policy_map
, &policy_root
);