f0a759aaef8c872d11f1e91611cae80dcdf3ba82
[project/firewall3.git] / options.h
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013-2014 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_MASQUERADE    = 13,
75         FW3_FLAG_SRC_ACCEPT    = 14,
76         FW3_FLAG_SRC_REJECT    = 15,
77         FW3_FLAG_SRC_DROP      = 16,
78         FW3_FLAG_CUSTOM_CHAINS = 17,
79         FW3_FLAG_SYN_FLOOD     = 18,
80         FW3_FLAG_MTU_FIX       = 19,
81         FW3_FLAG_DROP_INVALID  = 20,
82         FW3_FLAG_HOTPLUG       = 21,
83
84         __FW3_FLAG_MAX
85 };
86
87 extern const char *fw3_flag_names[__FW3_FLAG_MAX];
88
89
90 enum fw3_limit_unit
91 {
92         FW3_LIMIT_UNIT_SECOND = 0,
93         FW3_LIMIT_UNIT_MINUTE = 1,
94         FW3_LIMIT_UNIT_HOUR   = 2,
95         FW3_LIMIT_UNIT_DAY    = 3,
96
97         __FW3_LIMIT_UNIT_MAX
98 };
99
100 extern const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX];
101
102
103 enum fw3_ipset_method
104 {
105         FW3_IPSET_METHOD_UNSPEC = 0,
106         FW3_IPSET_METHOD_BITMAP = 1,
107         FW3_IPSET_METHOD_HASH   = 2,
108         FW3_IPSET_METHOD_LIST   = 3,
109
110         __FW3_IPSET_METHOD_MAX
111 };
112
113 enum fw3_ipset_type
114 {
115         FW3_IPSET_TYPE_UNSPEC = 0,
116         FW3_IPSET_TYPE_IP     = 1,
117         FW3_IPSET_TYPE_PORT   = 2,
118         FW3_IPSET_TYPE_MAC    = 3,
119         FW3_IPSET_TYPE_NET    = 4,
120         FW3_IPSET_TYPE_SET    = 5,
121
122         __FW3_IPSET_TYPE_MAX
123 };
124
125 extern const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX];
126 extern const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX];
127
128
129 enum fw3_include_type
130 {
131         FW3_INC_TYPE_SCRIPT   = 0,
132         FW3_INC_TYPE_RESTORE  = 1,
133 };
134
135 enum fw3_reflection_source
136 {
137         FW3_REFLECTION_INTERNAL = 0,
138         FW3_REFLECTION_EXTERNAL = 1,
139 };
140
141 struct fw3_ipset_datatype
142 {
143         struct list_head list;
144         enum fw3_ipset_type type;
145         const char *dir;
146 };
147
148 struct fw3_setmatch
149 {
150         bool set;
151         bool invert;
152         char name[32];
153         const char *dir[3];
154         struct fw3_ipset *ptr;
155 };
156
157 struct fw3_device
158 {
159         struct list_head list;
160
161         bool set;
162         bool any;
163         bool invert;
164         char name[32];
165         char network[32];
166 };
167
168 struct fw3_address
169 {
170         struct list_head list;
171
172         bool set;
173         bool range;
174         bool invert;
175         bool resolved;
176         enum fw3_family family;
177         int mask;
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         } address2;
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
319 struct fw3_rule
320 {
321         struct list_head list;
322
323         bool enabled;
324         const char *name;
325
326         enum fw3_family family;
327
328         struct fw3_zone *_src;
329         struct fw3_zone *_dest;
330
331         struct fw3_device src;
332         struct fw3_device dest;
333         struct fw3_setmatch ipset;
334
335         struct list_head proto;
336
337         struct list_head ip_src;
338         struct list_head mac_src;
339         struct list_head port_src;
340
341         struct list_head ip_dest;
342         struct list_head port_dest;
343
344         struct list_head icmp_type;
345
346         struct fw3_limit limit;
347         struct fw3_time time;
348         struct fw3_mark mark;
349
350         enum fw3_flag target;
351         struct fw3_mark set_mark;
352         struct fw3_mark set_xmark;
353
354         const char *extra;
355 };
356
357 struct fw3_redirect
358 {
359         struct list_head list;
360
361         bool enabled;
362         const char *name;
363
364         enum fw3_family family;
365
366         struct fw3_zone *_src;
367         struct fw3_zone *_dest;
368
369         struct fw3_device src;
370         struct fw3_device dest;
371         struct fw3_setmatch ipset;
372
373         struct list_head proto;
374
375         struct fw3_address ip_src;
376         struct list_head mac_src;
377         struct fw3_port port_src;
378
379         struct fw3_address ip_dest;
380         struct fw3_port port_dest;
381
382         struct fw3_address ip_redir;
383         struct fw3_port port_redir;
384
385         struct fw3_limit limit;
386         struct fw3_time time;
387         struct fw3_mark mark;
388
389         enum fw3_flag target;
390
391         const char *extra;
392
393         bool local;
394         bool reflection;
395         enum fw3_reflection_source reflection_src;
396 };
397
398 struct fw3_snat
399 {
400         struct list_head list;
401
402         bool enabled;
403         const char *name;
404
405         enum fw3_family family;
406
407         struct fw3_zone *_src;
408
409         struct fw3_device src;
410         struct fw3_setmatch ipset;
411
412         struct list_head proto;
413
414         struct fw3_address ip_src;
415         struct fw3_port port_src;
416
417         struct fw3_address ip_dest;
418         struct fw3_port port_dest;
419
420         struct fw3_address ip_snat;
421         struct fw3_port port_snat;
422
423         struct fw3_limit limit;
424         struct fw3_time time;
425         struct fw3_mark mark;
426         bool connlimit_ports;
427
428         enum fw3_flag target;
429
430         const char *extra;
431 };
432
433 struct fw3_forward
434 {
435         struct list_head list;
436
437         bool enabled;
438         const char *name;
439
440         enum fw3_family family;
441
442         struct fw3_zone *_src;
443         struct fw3_zone *_dest;
444
445         struct fw3_device src;
446         struct fw3_device dest;
447 };
448
449 struct fw3_ipset
450 {
451         struct list_head list;
452
453         bool enabled;
454         const char *name;
455         enum fw3_family family;
456
457         enum fw3_ipset_method method;
458         struct list_head datatypes;
459
460         struct fw3_address iprange;
461         struct fw3_port portrange;
462
463         int netmask;
464         int maxelem;
465         int hashsize;
466
467         int timeout;
468
469         const char *external;
470
471         uint32_t flags[2];
472 };
473
474 struct fw3_include
475 {
476         struct list_head list;
477
478         bool enabled;
479         const char *name;
480         enum fw3_family family;
481
482         const char *path;
483         enum fw3_include_type type;
484
485         bool reload;
486 };
487
488 struct fw3_state
489 {
490         struct uci_context *uci;
491         struct fw3_defaults defaults;
492         struct list_head zones;
493         struct list_head rules;
494         struct list_head redirects;
495         struct list_head snats;
496         struct list_head forwards;
497         struct list_head ipsets;
498         struct list_head includes;
499
500         bool disable_ipsets;
501         bool statefile;
502 };
503
504 struct fw3_chain_spec {
505         int family;
506         int table;
507         int flag;
508         const char *format;
509 };
510
511
512 struct fw3_option
513 {
514         const char *name;
515         bool (*parse)(void *, const char *, bool);
516         uintptr_t offset;
517         size_t elem_size;
518 };
519
520 #define FW3_OPT(name, parse, structure, member) \
521         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
522
523 #define FW3_LIST(name, parse, structure, member) \
524         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
525           sizeof(struct fw3_##structure) }
526
527 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
528 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
529 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
530 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
531 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
532 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
533 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
534 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
535 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
536 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
537 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
538 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
539 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
540
541 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
542 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
543
544 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
545 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
546
547 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
548 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
549 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
550 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
551 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
552 bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
553
554 bool fw3_parse_options(void *s, const struct fw3_option *opts,
555                        struct uci_section *section);
556
557 const char * fw3_address_to_string(struct fw3_address *address,
558                                    bool allow_invert);
559
560 #endif