initial commit
[project/firewall3.git] / options.h
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef __FW3_OPTIONS_H
20 #define __FW3_OPTIONS_H
21
22
23 #include <errno.h>
24
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28
29 #include <ctype.h>
30 #include <string.h>
31
32 #include <netdb.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/ether.h>
37
38 #include <uci.h>
39
40 #include <libubox/list.h>
41 #include <libubox/utils.h>
42
43 #include "icmp_codes.h"
44 #include "utils.h"
45
46
47 enum fw3_table
48 {
49 FW3_TABLE_FILTER,
50 FW3_TABLE_NAT,
51 FW3_TABLE_MANGLE,
52 FW3_TABLE_RAW,
53 };
54
55 enum fw3_family
56 {
57 FW3_FAMILY_ANY = 0,
58 FW3_FAMILY_V4 = 1,
59 FW3_FAMILY_V6 = 2,
60 };
61
62 enum fw3_target
63 {
64 FW3_TARGET_UNSPEC = 0,
65 FW3_TARGET_ACCEPT = 1,
66 FW3_TARGET_REJECT = 2,
67 FW3_TARGET_DROP = 3,
68 FW3_TARGET_NOTRACK = 4,
69 FW3_TARGET_DNAT = 5,
70 FW3_TARGET_SNAT = 6,
71 };
72
73 enum fw3_limit_unit
74 {
75 FW3_LIMIT_UNIT_SECOND = 0,
76 FW3_LIMIT_UNIT_MINUTE = 1,
77 FW3_LIMIT_UNIT_HOUR = 2,
78 FW3_LIMIT_UNIT_DAY = 3,
79 };
80
81 enum fw3_ipset_method
82 {
83 FW3_IPSET_METHOD_UNSPEC = 0,
84 FW3_IPSET_METHOD_BITMAP = 1,
85 FW3_IPSET_METHOD_HASH = 2,
86 FW3_IPSET_METHOD_LIST = 3,
87 };
88
89 enum fw3_ipset_type
90 {
91 FW3_IPSET_TYPE_UNSPEC = 0,
92 FW3_IPSET_TYPE_IP = 1,
93 FW3_IPSET_TYPE_PORT = 2,
94 FW3_IPSET_TYPE_MAC = 3,
95 FW3_IPSET_TYPE_NET = 4,
96 FW3_IPSET_TYPE_SET = 5,
97 };
98
99 struct fw3_ipset_datatype
100 {
101 struct list_head list;
102 enum fw3_ipset_type type;
103 bool dest;
104 };
105
106 struct fw3_device
107 {
108 struct list_head list;
109
110 bool set;
111 bool any;
112 bool invert;
113 char name[32];
114 };
115
116 struct fw3_address
117 {
118 struct list_head list;
119
120 bool set;
121 bool invert;
122 enum fw3_family family;
123 int mask;
124 union {
125 struct in_addr v4;
126 struct in6_addr v6;
127 struct ether_addr mac;
128 } address;
129 };
130
131 struct fw3_mac
132 {
133 struct list_head list;
134
135 bool set;
136 bool invert;
137 struct ether_addr mac;
138 };
139
140 struct fw3_protocol
141 {
142 struct list_head list;
143
144 bool any;
145 bool invert;
146 uint16_t protocol;
147 };
148
149 struct fw3_port
150 {
151 struct list_head list;
152
153 bool set;
154 bool invert;
155 uint16_t port_min;
156 uint16_t port_max;
157 };
158
159 struct fw3_icmptype
160 {
161 struct list_head list;
162
163 bool invert;
164 enum fw3_family family;
165 uint8_t type;
166 uint8_t code_min;
167 uint8_t code_max;
168 uint8_t type6;
169 uint8_t code6_min;
170 uint8_t code6_max;
171 };
172
173 struct fw3_limit
174 {
175 bool invert;
176 int rate;
177 int burst;
178 enum fw3_limit_unit unit;
179 };
180
181 struct fw3_defaults
182 {
183 enum fw3_target policy_input;
184 enum fw3_target policy_output;
185 enum fw3_target policy_forward;
186
187 bool drop_invalid;
188
189 bool syn_flood;
190 struct fw3_limit syn_flood_rate;
191
192 bool tcp_syncookies;
193 bool tcp_ecn;
194 bool tcp_westwood;
195 bool tcp_window_scaling;
196
197 bool accept_redirects;
198 bool accept_source_route;
199
200 bool custom_chains;
201
202 bool disable_ipv6;
203 };
204
205 struct fw3_zone
206 {
207 struct list_head list;
208
209 const char *name;
210
211 enum fw3_family family;
212
213 enum fw3_target policy_input;
214 enum fw3_target policy_output;
215 enum fw3_target policy_forward;
216
217 struct list_head networks;
218 struct list_head devices;
219 struct list_head subnets;
220
221 const char *extra_src;
222 const char *extra_dest;
223
224 bool masq;
225 struct list_head masq_src;
226 struct list_head masq_dest;
227
228 bool conntrack;
229 bool mtu_fix;
230
231 bool log;
232 struct fw3_limit log_limit;
233
234 bool custom_chains;
235
236 bool has_src_target[FW3_TARGET_SNAT + 1];
237 bool has_dest_target[FW3_TARGET_SNAT + 1];
238 };
239
240 struct fw3_rule
241 {
242 struct list_head list;
243
244 const char *name;
245
246 enum fw3_family family;
247
248 struct fw3_zone *_src;
249 struct fw3_zone *_dest;
250
251 struct fw3_device src;
252 struct fw3_device dest;
253
254 struct fw3_ipset *_ipset;
255 struct fw3_device ipset;
256
257 struct list_head proto;
258
259 struct list_head ip_src;
260 struct list_head mac_src;
261 struct list_head port_src;
262
263 struct list_head ip_dest;
264 struct list_head port_dest;
265
266 struct list_head icmp_type;
267
268 enum fw3_target target;
269
270 struct fw3_limit limit;
271
272 const char *extra;
273 };
274
275 struct fw3_redirect
276 {
277 struct list_head list;
278
279 const char *name;
280
281 enum fw3_family family;
282
283 struct fw3_zone *_src;
284 struct fw3_zone *_dest;
285
286 struct fw3_device src;
287 struct fw3_device dest;
288
289 struct fw3_ipset *_ipset;
290 struct fw3_device ipset;
291
292 struct list_head proto;
293
294 struct fw3_address ip_src;
295 struct list_head mac_src;
296 struct fw3_port port_src;
297
298 struct fw3_address ip_dest;
299 struct fw3_port port_dest;
300
301 struct fw3_address ip_redir;
302 struct fw3_port port_redir;
303
304 enum fw3_target target;
305
306 const char *extra;
307
308 bool reflection;
309 };
310
311 struct fw3_forward
312 {
313 struct list_head list;
314
315 const char *name;
316
317 enum fw3_family family;
318
319 struct fw3_zone *_src;
320 struct fw3_zone *_dest;
321
322 struct fw3_device src;
323 struct fw3_device dest;
324 };
325
326 struct fw3_ipset
327 {
328 struct list_head list;
329
330 const char *name;
331 enum fw3_family family;
332
333 enum fw3_ipset_method method;
334 struct list_head datatypes;
335
336 struct list_head iprange;
337 struct fw3_port portrange;
338
339 int netmask;
340 int maxelem;
341 int hashsize;
342
343 int timeout;
344
345 const char *external;
346 };
347
348 struct fw3_state
349 {
350 struct uci_context *uci;
351 struct fw3_defaults defaults;
352 struct list_head zones;
353 struct list_head rules;
354 struct list_head redirects;
355 struct list_head forwards;
356 struct list_head ipsets;
357
358 bool disable_ipsets;
359 };
360
361
362 struct fw3_option
363 {
364 const char *name;
365 bool (*parse)(void *, const char *);
366 uintptr_t offset;
367 size_t elem_size;
368 };
369
370 #define FW3_OPT(name, parse, structure, member) \
371 { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
372
373 #define FW3_LIST(name, parse, structure, member) \
374 { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
375 sizeof(struct fw3_##structure) }
376
377
378 bool fw3_parse_bool(void *ptr, const char *val);
379 bool fw3_parse_int(void *ptr, const char *val);
380 bool fw3_parse_string(void *ptr, const char *val);
381 bool fw3_parse_target(void *ptr, const char *val);
382 bool fw3_parse_limit(void *ptr, const char *val);
383 bool fw3_parse_device(void *ptr, const char *val);
384 bool fw3_parse_address(void *ptr, const char *val);
385 bool fw3_parse_mac(void *ptr, const char *val);
386 bool fw3_parse_port(void *ptr, const char *val);
387 bool fw3_parse_family(void *ptr, const char *val);
388 bool fw3_parse_icmptype(void *ptr, const char *val);
389 bool fw3_parse_protocol(void *ptr, const char *val);
390 bool fw3_parse_ipset_method(void *ptr, const char *val);
391 bool fw3_parse_ipset_datatype(void *ptr, const char *val);
392
393 void fw3_parse_options(void *s, struct fw3_option *opts, int n,
394 struct uci_section *section);
395
396 void fw3_format_in_out(struct fw3_device *in, struct fw3_device *out);
397 void fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest);
398 void fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp);
399 void fw3_format_mac(struct fw3_mac *mac);
400 void fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family);
401 void fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family);
402 void fw3_format_limit(struct fw3_limit *limit);
403 void fw3_format_ipset(struct fw3_ipset *ipset, bool invert);
404
405 void __fw3_format_comment(const char *comment, ...);
406 #define fw3_format_comment(...) __fw3_format_comment(__VA_ARGS__, NULL)
407
408 void fw3_format_extra(const char *extra);
409
410 #endif