ubus: add support for fetching firewall rules from procd
[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 #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         int mask;
179         union {
180                 struct in_addr v4;
181                 struct in6_addr v6;
182                 struct ether_addr mac;
183         } address;
184         union {
185                 struct in_addr v4;
186                 struct in6_addr v6;
187                 struct ether_addr mac;
188         } address2;
189 };
190
191 struct fw3_mac
192 {
193         struct list_head list;
194
195         bool set;
196         bool invert;
197         struct ether_addr mac;
198 };
199
200 struct fw3_protocol
201 {
202         struct list_head list;
203
204         bool any;
205         bool invert;
206         uint32_t protocol;
207 };
208
209 struct fw3_port
210 {
211         struct list_head list;
212
213         bool set;
214         bool invert;
215         uint16_t port_min;
216         uint16_t port_max;
217 };
218
219 struct fw3_icmptype
220 {
221         struct list_head list;
222
223         bool invert;
224         enum fw3_family family;
225         uint8_t type;
226         uint8_t code_min;
227         uint8_t code_max;
228         uint8_t type6;
229         uint8_t code6_min;
230         uint8_t code6_max;
231 };
232
233 struct fw3_limit
234 {
235         bool invert;
236         int rate;
237         int burst;
238         enum fw3_limit_unit unit;
239 };
240
241 struct fw3_time
242 {
243         bool utc;
244         struct tm datestart;
245         struct tm datestop;
246         uint32_t timestart;
247         uint32_t timestop;
248         uint32_t monthdays; /* bit 0 is invert + 1 .. 31 */
249         uint8_t weekdays;   /* bit 0 is invert + 1 .. 7 */
250 };
251
252 struct fw3_mark
253 {
254         bool set;
255         bool invert;
256         uint32_t mark;
257         uint32_t mask;
258 };
259
260 struct fw3_defaults
261 {
262         enum fw3_flag policy_input;
263         enum fw3_flag policy_output;
264         enum fw3_flag policy_forward;
265
266         bool drop_invalid;
267
268         bool syn_flood;
269         struct fw3_limit syn_flood_rate;
270
271         bool tcp_syncookies;
272         int tcp_ecn;
273         bool tcp_window_scaling;
274
275         bool accept_redirects;
276         bool accept_source_route;
277
278         bool custom_chains;
279
280         bool disable_ipv6;
281
282         uint32_t flags[2];
283 };
284
285 struct fw3_zone
286 {
287         struct list_head list;
288
289         bool enabled;
290         const char *name;
291
292         enum fw3_family family;
293
294         enum fw3_flag policy_input;
295         enum fw3_flag policy_output;
296         enum fw3_flag policy_forward;
297
298         struct list_head networks;
299         struct list_head devices;
300         struct list_head subnets;
301
302         const char *extra_src;
303         const char *extra_dest;
304
305         bool masq;
306         struct list_head masq_src;
307         struct list_head masq_dest;
308
309         bool conntrack;
310         bool mtu_fix;
311
312         bool log;
313         struct fw3_limit log_limit;
314
315         bool custom_chains;
316
317         uint32_t flags[2];
318 };
319
320 struct fw3_rule
321 {
322         struct list_head list;
323
324         bool enabled;
325         const char *name;
326
327         enum fw3_family family;
328
329         struct fw3_zone *_src;
330         struct fw3_zone *_dest;
331
332         const char *device;
333         bool direction_out;
334
335         struct fw3_device src;
336         struct fw3_device dest;
337         struct fw3_setmatch ipset;
338
339         struct list_head proto;
340
341         struct list_head ip_src;
342         struct list_head mac_src;
343         struct list_head port_src;
344
345         struct list_head ip_dest;
346         struct list_head port_dest;
347
348         struct list_head icmp_type;
349
350         struct fw3_limit limit;
351         struct fw3_time time;
352         struct fw3_mark mark;
353
354         enum fw3_flag target;
355         struct fw3_mark set_mark;
356         struct fw3_mark set_xmark;
357
358         const char *extra;
359 };
360
361 struct fw3_redirect
362 {
363         struct list_head list;
364
365         bool enabled;
366         const char *name;
367
368         enum fw3_family family;
369
370         struct fw3_zone *_src;
371         struct fw3_zone *_dest;
372
373         struct fw3_device src;
374         struct fw3_device dest;
375         struct fw3_setmatch ipset;
376
377         struct list_head proto;
378
379         struct fw3_address ip_src;
380         struct list_head mac_src;
381         struct fw3_port port_src;
382
383         struct fw3_address ip_dest;
384         struct fw3_port port_dest;
385
386         struct fw3_address ip_redir;
387         struct fw3_port port_redir;
388
389         struct fw3_limit limit;
390         struct fw3_time time;
391         struct fw3_mark mark;
392
393         enum fw3_flag target;
394
395         const char *extra;
396
397         bool local;
398         bool reflection;
399         enum fw3_reflection_source reflection_src;
400 };
401
402 struct fw3_snat
403 {
404         struct list_head list;
405
406         bool enabled;
407         const char *name;
408
409         enum fw3_family family;
410
411         struct fw3_zone *_src;
412
413         struct fw3_device src;
414         struct fw3_setmatch ipset;
415         const char *device;
416
417         struct list_head proto;
418
419         struct fw3_address ip_src;
420         struct fw3_port port_src;
421
422         struct fw3_address ip_dest;
423         struct fw3_port port_dest;
424
425         struct fw3_address ip_snat;
426         struct fw3_port port_snat;
427
428         struct fw3_limit limit;
429         struct fw3_time time;
430         struct fw3_mark mark;
431         bool connlimit_ports;
432
433         enum fw3_flag target;
434
435         const char *extra;
436 };
437
438 struct fw3_forward
439 {
440         struct list_head list;
441
442         bool enabled;
443         const char *name;
444
445         enum fw3_family family;
446
447         struct fw3_zone *_src;
448         struct fw3_zone *_dest;
449
450         struct fw3_device src;
451         struct fw3_device dest;
452 };
453
454 struct fw3_ipset
455 {
456         struct list_head list;
457
458         bool enabled;
459         const char *name;
460         enum fw3_family family;
461
462         enum fw3_ipset_method method;
463         struct list_head datatypes;
464
465         struct fw3_address iprange;
466         struct fw3_port portrange;
467
468         int netmask;
469         int maxelem;
470         int hashsize;
471
472         int timeout;
473
474         const char *external;
475
476         uint32_t flags[2];
477 };
478
479 struct fw3_include
480 {
481         struct list_head list;
482
483         bool enabled;
484         const char *name;
485         enum fw3_family family;
486
487         const char *path;
488         enum fw3_include_type type;
489
490         bool reload;
491 };
492
493 struct fw3_state
494 {
495         struct uci_context *uci;
496         struct fw3_defaults defaults;
497         struct list_head zones;
498         struct list_head rules;
499         struct list_head redirects;
500         struct list_head snats;
501         struct list_head forwards;
502         struct list_head ipsets;
503         struct list_head includes;
504
505         bool disable_ipsets;
506         bool statefile;
507 };
508
509 struct fw3_chain_spec {
510         int family;
511         int table;
512         int flag;
513         const char *format;
514 };
515
516
517 struct fw3_option
518 {
519         const char *name;
520         bool (*parse)(void *, const char *, bool);
521         uintptr_t offset;
522         size_t elem_size;
523 };
524
525 #define FW3_OPT(name, parse, structure, member) \
526         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
527
528 #define FW3_LIST(name, parse, structure, member) \
529         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
530           sizeof(struct fw3_##structure) }
531
532 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
533 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
534 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
535 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
536 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
537 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
538 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
539 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
540 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
541 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
542 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
543 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
544 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
545
546 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
547 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
548
549 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
550 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
551
552 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
553 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
554 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
555 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
556 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
557 bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
558 bool fw3_parse_direction(void *ptr, const char *val, bool is_list);
559
560 bool fw3_parse_options(void *s, const struct fw3_option *opts,
561                        struct uci_section *section);
562 bool fw3_parse_blob_options(void *s, const struct fw3_option *opts,
563                        struct blob_attr *a);
564
565 const char * fw3_address_to_string(struct fw3_address *address,
566                                    bool allow_invert);
567
568 #endif