Properly dereference struct ether_addr
[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 <time.h>
39
40 #include <uci.h>
41
42 #include <libubox/list.h>
43 #include <libubox/utils.h>
44
45 #include "icmp_codes.h"
46 #include "utils.h"
47
48
49 enum fw3_table
50 {
51         FW3_TABLE_FILTER = 0,
52         FW3_TABLE_NAT    = 1,
53         FW3_TABLE_MANGLE = 2,
54         FW3_TABLE_RAW    = 3,
55 };
56
57 enum fw3_family
58 {
59         FW3_FAMILY_ANY = 0,
60         FW3_FAMILY_V4  = 4,
61         FW3_FAMILY_V6  = 5,
62 };
63
64 enum fw3_flag
65 {
66         FW3_FLAG_UNSPEC        = 0,
67         FW3_FLAG_ACCEPT        = 6,
68         FW3_FLAG_REJECT        = 7,
69         FW3_FLAG_DROP          = 8,
70         FW3_FLAG_NOTRACK       = 9,
71         FW3_FLAG_MARK          = 10,
72         FW3_FLAG_DNAT          = 11,
73         FW3_FLAG_SNAT          = 12,
74         FW3_FLAG_SRC_ACCEPT    = 13,
75         FW3_FLAG_SRC_REJECT    = 14,
76         FW3_FLAG_SRC_DROP      = 15,
77         FW3_FLAG_CUSTOM_CHAINS = 16,
78         FW3_FLAG_SYN_FLOOD     = 17,
79         FW3_FLAG_MTU_FIX       = 18,
80         FW3_FLAG_DROP_INVALID  = 19,
81         FW3_FLAG_HOTPLUG       = 20,
82
83         __FW3_FLAG_MAX
84 };
85
86 extern const char *fw3_flag_names[__FW3_FLAG_MAX];
87
88
89 enum fw3_limit_unit
90 {
91         FW3_LIMIT_UNIT_SECOND = 0,
92         FW3_LIMIT_UNIT_MINUTE = 1,
93         FW3_LIMIT_UNIT_HOUR   = 2,
94         FW3_LIMIT_UNIT_DAY    = 3,
95
96         __FW3_LIMIT_UNIT_MAX
97 };
98
99 extern const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX];
100
101
102 enum fw3_ipset_method
103 {
104         FW3_IPSET_METHOD_UNSPEC = 0,
105         FW3_IPSET_METHOD_BITMAP = 1,
106         FW3_IPSET_METHOD_HASH   = 2,
107         FW3_IPSET_METHOD_LIST   = 3,
108
109         __FW3_IPSET_METHOD_MAX
110 };
111
112 enum fw3_ipset_type
113 {
114         FW3_IPSET_TYPE_UNSPEC = 0,
115         FW3_IPSET_TYPE_IP     = 1,
116         FW3_IPSET_TYPE_PORT   = 2,
117         FW3_IPSET_TYPE_MAC    = 3,
118         FW3_IPSET_TYPE_NET    = 4,
119         FW3_IPSET_TYPE_SET    = 5,
120
121         __FW3_IPSET_TYPE_MAX
122 };
123
124 extern const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX];
125 extern const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX];
126
127
128 enum fw3_include_type
129 {
130         FW3_INC_TYPE_SCRIPT   = 0,
131         FW3_INC_TYPE_RESTORE  = 1,
132 };
133
134 enum fw3_reflection_source
135 {
136         FW3_REFLECTION_INTERNAL = 0,
137         FW3_REFLECTION_EXTERNAL = 1,
138 };
139
140 struct fw3_ipset_datatype
141 {
142         struct list_head list;
143         enum fw3_ipset_type type;
144         const char *dir;
145 };
146
147 struct fw3_setmatch
148 {
149         bool set;
150         bool invert;
151         char name[32];
152         const char *dir[3];
153         struct fw3_ipset *ptr;
154 };
155
156 struct fw3_device
157 {
158         struct list_head list;
159
160         bool set;
161         bool any;
162         bool invert;
163         char name[32];
164         char network[32];
165 };
166
167 struct fw3_address
168 {
169         struct list_head list;
170
171         bool set;
172         bool range;
173         bool invert;
174         bool resolved;
175         enum fw3_family family;
176         int mask;
177         union {
178                 struct in_addr v4;
179                 struct in6_addr v6;
180                 struct ether_addr mac;
181         } address;
182         union {
183                 struct in_addr v4;
184                 struct in6_addr v6;
185                 struct ether_addr mac;
186         } address2;
187 };
188
189 struct fw3_mac
190 {
191         struct list_head list;
192
193         bool set;
194         bool invert;
195         struct ether_addr mac;
196 };
197
198 struct fw3_protocol
199 {
200         struct list_head list;
201
202         bool any;
203         bool invert;
204         uint32_t protocol;
205 };
206
207 struct fw3_port
208 {
209         struct list_head list;
210
211         bool set;
212         bool invert;
213         uint16_t port_min;
214         uint16_t port_max;
215 };
216
217 struct fw3_icmptype
218 {
219         struct list_head list;
220
221         bool invert;
222         enum fw3_family family;
223         uint8_t type;
224         uint8_t code_min;
225         uint8_t code_max;
226         uint8_t type6;
227         uint8_t code6_min;
228         uint8_t code6_max;
229 };
230
231 struct fw3_limit
232 {
233         bool invert;
234         int rate;
235         int burst;
236         enum fw3_limit_unit unit;
237 };
238
239 struct fw3_time
240 {
241         bool utc;
242         struct tm datestart;
243         struct tm datestop;
244         uint32_t timestart;
245         uint32_t timestop;
246         uint32_t monthdays; /* bit 0 is invert + 1 .. 31 */
247         uint8_t weekdays;   /* bit 0 is invert + 1 .. 7 */
248 };
249
250 struct fw3_mark
251 {
252         bool set;
253         bool invert;
254         uint32_t mark;
255         uint32_t mask;
256 };
257
258 struct fw3_defaults
259 {
260         enum fw3_flag policy_input;
261         enum fw3_flag policy_output;
262         enum fw3_flag policy_forward;
263
264         bool drop_invalid;
265
266         bool syn_flood;
267         struct fw3_limit syn_flood_rate;
268
269         bool tcp_syncookies;
270         bool tcp_ecn;
271         bool tcp_window_scaling;
272
273         bool accept_redirects;
274         bool accept_source_route;
275
276         bool custom_chains;
277
278         bool disable_ipv6;
279
280         uint32_t flags[2];
281 };
282
283 struct fw3_zone
284 {
285         struct list_head list;
286
287         bool enabled;
288         const char *name;
289
290         enum fw3_family family;
291
292         enum fw3_flag policy_input;
293         enum fw3_flag policy_output;
294         enum fw3_flag policy_forward;
295
296         struct list_head networks;
297         struct list_head devices;
298         struct list_head subnets;
299
300         const char *extra_src;
301         const char *extra_dest;
302
303         bool masq;
304         struct list_head masq_src;
305         struct list_head masq_dest;
306
307         bool conntrack;
308         bool mtu_fix;
309
310         bool log;
311         struct fw3_limit log_limit;
312
313         bool custom_chains;
314
315         uint32_t flags[2];
316 };
317
318 struct fw3_rule
319 {
320         struct list_head list;
321
322         bool enabled;
323         const char *name;
324
325         enum fw3_family family;
326
327         struct fw3_zone *_src;
328         struct fw3_zone *_dest;
329
330         struct fw3_device src;
331         struct fw3_device dest;
332         struct fw3_setmatch ipset;
333
334         struct list_head proto;
335
336         struct list_head ip_src;
337         struct list_head mac_src;
338         struct list_head port_src;
339
340         struct list_head ip_dest;
341         struct list_head port_dest;
342
343         struct list_head icmp_type;
344
345         struct fw3_limit limit;
346         struct fw3_time time;
347         struct fw3_mark mark;
348
349         enum fw3_flag target;
350         struct fw3_mark set_mark;
351         struct fw3_mark set_xmark;
352
353         const char *extra;
354 };
355
356 struct fw3_redirect
357 {
358         struct list_head list;
359
360         bool enabled;
361         const char *name;
362
363         enum fw3_family family;
364
365         struct fw3_zone *_src;
366         struct fw3_zone *_dest;
367
368         struct fw3_device src;
369         struct fw3_device dest;
370         struct fw3_setmatch ipset;
371
372         struct list_head proto;
373
374         struct fw3_address ip_src;
375         struct list_head mac_src;
376         struct fw3_port port_src;
377
378         struct fw3_address ip_dest;
379         struct fw3_port port_dest;
380
381         struct fw3_address ip_redir;
382         struct fw3_port port_redir;
383
384         struct fw3_limit limit;
385         struct fw3_time time;
386         struct fw3_mark mark;
387
388         enum fw3_flag target;
389
390         const char *extra;
391
392         bool reflection;
393         enum fw3_reflection_source reflection_src;
394 };
395
396 struct fw3_forward
397 {
398         struct list_head list;
399
400         bool enabled;
401         const char *name;
402
403         enum fw3_family family;
404
405         struct fw3_zone *_src;
406         struct fw3_zone *_dest;
407
408         struct fw3_device src;
409         struct fw3_device dest;
410 };
411
412 struct fw3_ipset
413 {
414         struct list_head list;
415
416         bool enabled;
417         const char *name;
418         enum fw3_family family;
419
420         enum fw3_ipset_method method;
421         struct list_head datatypes;
422
423         struct fw3_address iprange;
424         struct fw3_port portrange;
425
426         int netmask;
427         int maxelem;
428         int hashsize;
429
430         int timeout;
431
432         const char *external;
433
434         uint32_t flags[2];
435 };
436
437 struct fw3_include
438 {
439         struct list_head list;
440
441         bool enabled;
442         const char *name;
443         enum fw3_family family;
444
445         const char *path;
446         enum fw3_include_type type;
447
448         bool reload;
449 };
450
451 struct fw3_state
452 {
453         struct uci_context *uci;
454         struct fw3_defaults defaults;
455         struct list_head zones;
456         struct list_head rules;
457         struct list_head redirects;
458         struct list_head forwards;
459         struct list_head ipsets;
460         struct list_head includes;
461
462         bool disable_ipsets;
463         bool statefile;
464 };
465
466 struct fw3_chain_spec {
467         int family;
468         int table;
469         int flag;
470         const char *format;
471 };
472
473
474 struct fw3_option
475 {
476         const char *name;
477         bool (*parse)(void *, const char *, bool);
478         uintptr_t offset;
479         size_t elem_size;
480 };
481
482 #define FW3_OPT(name, parse, structure, member) \
483         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
484
485 #define FW3_LIST(name, parse, structure, member) \
486         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
487           sizeof(struct fw3_##structure) }
488
489 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
490 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
491 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
492 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
493 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
494 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
495 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
496 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
497 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
498 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
499 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
500 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
501 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
502
503 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
504 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
505
506 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
507 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
508
509 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
510 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
511 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
512 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
513 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
514 bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
515
516 void fw3_parse_options(void *s, const struct fw3_option *opts,
517                        struct uci_section *section);
518
519 const char * fw3_address_to_string(struct fw3_address *address,
520                                    bool allow_invert);
521
522 #endif