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