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