Tor  0.4.3.0-alpha-dev
policies.h
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file policies.h
9  * \brief Header file for policies.c.
10  **/
11 
12 #ifndef TOR_POLICIES_H
13 #define TOR_POLICIES_H
14 
15 /* (length of
16  * "accept6 [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/128:65535-65535\n"
17  * plus a terminating NUL, rounded up to a nice number.)
18  */
19 #define POLICY_BUF_LEN 72
20 
21 #define EXIT_POLICY_IPV6_ENABLED (1 << 0)
22 #define EXIT_POLICY_REJECT_PRIVATE (1 << 1)
23 #define EXIT_POLICY_ADD_DEFAULT (1 << 2)
24 #define EXIT_POLICY_REJECT_LOCAL_INTERFACES (1 << 3)
25 #define EXIT_POLICY_ADD_REDUCED (1 << 4)
26 #define EXIT_POLICY_OPTION_MAX EXIT_POLICY_ADD_REDUCED
27 /* All options set: used for unit testing */
28 #define EXIT_POLICY_OPTION_ALL ((EXIT_POLICY_OPTION_MAX << 1) - 1)
29 
30 typedef enum firewall_connection_t {
31  FIREWALL_OR_CONNECTION = 0,
32  FIREWALL_DIR_CONNECTION = 1
33 } firewall_connection_t;
34 
35 typedef int exit_policy_parser_cfg_t;
36 
37 /** Outcome of applying an address policy to an address. */
38 typedef enum {
39  /** The address was accepted */
41  /** The address was rejected */
43  /** Part of the address was unknown, but as far as we can tell, it was
44  * accepted. */
46  /** Part of the address was unknown, but as far as we can tell, it was
47  * rejected. */
50 
51 /** A single entry in a parsed policy summary, describing a range of ports. */
52 typedef struct short_policy_entry_t {
53  uint16_t min_port, max_port;
55 
56 /** A short_poliy_t is the parsed version of a policy summary. */
57 typedef struct short_policy_t {
58  /** True if the members of 'entries' are port ranges to accept; false if
59  * they are port ranges to reject */
60  unsigned int is_accept : 1;
61  /** The actual number of values in 'entries'. */
62  unsigned int n_entries : 31;
63  /** An array of 0 or more short_policy_entry_t values, each describing a
64  * range of ports that this policy accepts or rejects (depending on the
65  * value of is_accept).
66  */
67  short_policy_entry_t entries[FLEXIBLE_ARRAY_MEMBER];
69 
70 int firewall_is_fascist_or(void);
71 int firewall_is_fascist_dir(void);
72 int fascist_firewall_use_ipv6(const or_options_t *options);
73 MOCK_DECL(int, fascist_firewall_rand_prefer_ipv6_addr, (void));
76 
78  uint16_t port,
79  firewall_connection_t fw_connection,
80  int pref_only, int pref_ipv6);
81 
83  firewall_connection_t fw_connection,
84  int pref_only);
85 int fascist_firewall_allows_node(const node_t *node,
86  firewall_connection_t fw_connection,
87  int pref_only);
89  firewall_connection_t fw_connection,
90  int pref_only);
91 
93  firewall_connection_t fw_connection,
94  int pref_only, tor_addr_port_t* ap);
96  int pref_only, tor_addr_port_t* ap);
98  firewall_connection_t fw_connection,
99  int pref_only, tor_addr_port_t* ap);
101  firewall_connection_t fw_connection,
102  int pref_only, tor_addr_port_t* ap);
103 
104 int dir_policy_permits_address(const tor_addr_t *addr);
106 int authdir_policy_permits_address(uint32_t addr, uint16_t port);
107 int authdir_policy_valid_address(uint32_t addr, uint16_t port);
108 int authdir_policy_badexit_address(uint32_t addr, uint16_t port);
109 
110 int validate_addr_policies(const or_options_t *options, char **msg);
111 void policy_expand_private(smartlist_t **policy);
112 void policy_expand_unspec(smartlist_t **policy);
113 int policies_parse_from_options(const or_options_t *options);
114 
115 addr_policy_t *addr_policy_get_canonical_entry(addr_policy_t *ent);
116 int addr_policies_eq(const smartlist_t *a, const smartlist_t *b);
118  (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
120  uint16_t port, const node_t *node);
121 
123  const or_options_t *or_options,
124  uint32_t local_address,
125  const tor_addr_t *ipv6_local_address,
126  smartlist_t **result);
127 struct config_line_t;
129  exit_policy_parser_cfg_t options,
130  const smartlist_t *configured_addresses);
132  smartlist_t **dest,
133  int ipv6_exit,
134  const smartlist_t *configured_addresses,
135  int reject_interface_addresses,
136  int reject_configured_port_addresses);
139  const tor_addr_t *addr);
141  const smartlist_t *addrs);
144 int policy_is_reject_star(const smartlist_t *policy, sa_family_t family,
145  int reject_by_default);
146 char * policy_dump_to_string(const smartlist_t *policy_list,
147  int include_ipv4,
148  int include_ipv6);
150  const char *question, char **answer,
151  const char **errmsg);
152 int policy_write_item(char *buf, size_t buflen, const addr_policy_t *item,
153  int format_for_desc);
154 
156 #define addr_policy_list_free(lst) \
157  FREE_AND_NULL(smartlist_t, addr_policy_list_free_, (lst))
159 #define addr_policy_free(p) \
160  FREE_AND_NULL(addr_policy_t, addr_policy_free_, (p))
161 void policies_free_all(void);
162 
163 char *policy_summarize(smartlist_t *policy, sa_family_t family);
164 
165 short_policy_t *parse_short_policy(const char *summary);
166 char *write_short_policy(const short_policy_t *policy);
167 void short_policy_free_(short_policy_t *policy);
168 #define short_policy_free(p) \
169  FREE_AND_NULL(short_policy_t, short_policy_free_, (p))
172  const tor_addr_t *addr, uint16_t port,
173  const short_policy_t *policy);
174 
175 #ifdef POLICIES_PRIVATE
176 STATIC void append_exit_policy_string(smartlist_t **policy, const char *more);
178  uint16_t port,
179  smartlist_t *firewall_policy,
180  int pref_only, int pref_ipv6);
182  const tor_addr_port_t *a,
183  const tor_addr_port_t *b,
184  int want_a,
185  firewall_connection_t fw_connection,
186  int pref_only, int pref_ipv6);
187 
188 #endif /* defined(POLICIES_PRIVATE) */
189 
190 #endif /* !defined(TOR_POLICIES_H) */
int getinfo_helper_policies(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
Definition: policies.c:3085
int policies_parse_exit_policy_from_options(const or_options_t *or_options, uint32_t local_address, const tor_addr_t *ipv6_local_address, smartlist_t **result)
Definition: policies.c:2220
int firewall_is_fascist_dir(void)
Definition: policies.c:365
int fascist_firewall_prefer_ipv6_orport(const or_options_t *options)
Definition: policies.c:508
Definition: node_st.h:33
void addr_policy_free_(addr_policy_t *p)
Definition: policies.c:3193
int fascist_firewall_prefer_ipv6_dirport(const or_options_t *options)
Definition: policies.c:533
uint16_t sa_family_t
Definition: inaddr_st.h:77
Definition: policies.h:52
void fascist_firewall_choose_address_dir_server(const dir_server_t *ds, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:1150
char * policy_summarize(smartlist_t *policy, sa_family_t family)
Definition: policies.c:2690
addr_policy_result_t
Definition: policies.h:38
int authdir_policy_permits_address(uint32_t addr, uint16_t port)
Definition: policies.c:1213
char * policy_dump_to_string(const smartlist_t *policy_list, int include_ipv4, int include_ipv6)
Definition: policies.c:3042
int policies_parse_exit_policy(struct config_line_t *cfg, smartlist_t **dest, exit_policy_parser_cfg_t options, const smartlist_t *configured_addresses)
Definition: policies.c:2121
int authdir_policy_badexit_address(uint32_t addr, uint16_t port)
Definition: policies.c:1235
void addr_policy_list_free_(smartlist_t *p)
Definition: policies.c:3183
int fascist_firewall_allows_dir_server(const dir_server_t *ds, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:771
int fascist_firewall_use_ipv6(const or_options_t *options)
Definition: policies.c:459
int policy_write_item(char *buf, size_t buflen, const addr_policy_t *item, int format_for_desc)
Definition: policies.c:2385
int dir_policy_permits_address(const tor_addr_t *addr)
Definition: policies.c:1177
STATIC int fascist_firewall_allows_address(const tor_addr_t *addr, uint16_t port, smartlist_t *firewall_policy, int pref_only, int pref_ipv6)
Definition: policies.c:420
STATIC void append_exit_policy_string(smartlist_t **policy, const char *more)
Definition: policies.c:1684
void policy_expand_private(smartlist_t **policy)
Definition: policies.c:105
int policies_parse_from_options(const or_options_t *options)
Definition: policies.c:1355
addr_policy_result_t compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port, const smartlist_t *policy)
Definition: policies.c:1617
#define STATIC
Definition: testsupport.h:32
int fascist_firewall_allows_rs(const routerstatus_t *rs, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:687
int policy_is_reject_star(const smartlist_t *policy, sa_family_t family, int reject_by_default)
Definition: policies.c:2361
char * write_short_policy(const short_policy_t *policy)
Definition: policies.c:2903
short_policy_t * parse_short_policy(const char *summary)
Definition: policies.c:2802
int addr_policies_eq(const smartlist_t *a, const smartlist_t *b)
Definition: policies.c:1405
int short_policy_is_reject_star(const short_policy_t *policy)
Definition: policies.c:2987
void addr_policy_append_reject_addr(smartlist_t **dest, const tor_addr_t *addr)
Definition: policies.c:1698
void fascist_firewall_choose_address_rs(const routerstatus_t *rs, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:985
int exit_policy_is_general_exit(smartlist_t *policy)
Definition: policies.c:2347
addr_policy_result_t compare_tor_addr_to_short_policy(const tor_addr_t *addr, uint16_t port, const short_policy_t *policy)
Definition: policies.c:2939
void fascist_firewall_choose_address_ls(const smartlist_t *lspecs, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:1025
int socks_policy_permits_address(const tor_addr_t *addr)
Definition: policies.c:1186
unsigned int is_accept
Definition: policies.h:60
int validate_addr_policies(const or_options_t *options, char **msg)
Definition: policies.c:1258
void policies_free_all(void)
Definition: policies.c:3214
void fascist_firewall_choose_address_node(const node_t *node, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:1100
void policy_expand_unspec(smartlist_t **policy)
Definition: policies.c:145
short_policy_entry_t entries[FLEXIBLE_ARRAY_MEMBER]
Definition: policies.h:67
int fascist_firewall_allows_node(const node_t *node, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:733
int fascist_firewall_allows_address_addr(const tor_addr_t *addr, uint16_t port, firewall_connection_t fw_connection, int pref_only, int pref_ipv6)
Definition: policies.c:557
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
void addr_policy_append_reject_addr_list(smartlist_t **dest, const smartlist_t *addrs)
Definition: policies.c:1755
void policies_parse_exit_policy_reject_private(smartlist_t **dest, int ipv6_exit, const smartlist_t *configured_addresses, int reject_interface_addresses, int reject_configured_port_addresses)
Definition: policies.c:1886
int authdir_policy_valid_address(uint32_t addr, uint16_t port)
Definition: policies.c:1224
addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port, const node_t *node)
Definition: policies.c:3004
unsigned int n_entries
Definition: policies.h:62
void short_policy_free_(short_policy_t *policy)
Definition: policies.c:2929
int firewall_is_fascist_or(void)
Definition: policies.c:354
void policies_exit_policy_append_reject_star(smartlist_t **dest)
Definition: policies.c:2283
void policies_set_node_exitpolicy_to_reject_all(node_t *exitrouter)
Definition: policies.c:2291
STATIC const tor_addr_port_t * fascist_firewall_choose_address(const tor_addr_port_t *a, const tor_addr_port_t *b, int want_a, firewall_connection_t fw_connection, int pref_only, int pref_ipv6)
Definition: policies.c:832