zones: add interface/subnet bound LOG rules
[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         bool flow_offloading_hw;
294
295         bool disable_ipv6;
296
297         uint32_t flags[2];
298 };
299
300 struct fw3_zone
301 {
302         struct list_head list;
303
304         bool enabled;
305         const char *name;
306
307         enum fw3_family family;
308
309         enum fw3_flag policy_input;
310         enum fw3_flag policy_output;
311         enum fw3_flag policy_forward;
312
313         struct list_head networks;
314         struct list_head devices;
315         struct list_head subnets;
316
317         const char *extra_src;
318         const char *extra_dest;
319
320         bool masq;
321         bool masq_allow_invalid;
322         struct list_head masq_src;
323         struct list_head masq_dest;
324
325         bool mtu_fix;
326
327         struct list_head cthelpers;
328
329         int log;
330         struct fw3_limit log_limit;
331
332         bool custom_chains;
333         bool auto_helper;
334
335         uint32_t flags[2];
336
337         struct list_head old_addrs;
338 };
339
340 struct fw3_rule
341 {
342         struct list_head list;
343
344         bool enabled;
345         const char *name;
346
347         enum fw3_family family;
348
349         struct fw3_zone *_src;
350         struct fw3_zone *_dest;
351
352         const char *device;
353         bool direction_out;
354
355         struct fw3_device src;
356         struct fw3_device dest;
357         struct fw3_setmatch ipset;
358         struct fw3_cthelpermatch helper;
359
360         struct list_head proto;
361
362         struct list_head ip_src;
363         struct list_head mac_src;
364         struct list_head port_src;
365
366         struct list_head ip_dest;
367         struct list_head port_dest;
368
369         struct list_head icmp_type;
370
371         struct fw3_limit limit;
372         struct fw3_time time;
373         struct fw3_mark mark;
374
375         enum fw3_flag target;
376         struct fw3_mark set_mark;
377         struct fw3_mark set_xmark;
378         struct fw3_cthelpermatch set_helper;
379
380         const char *extra;
381 };
382
383 struct fw3_redirect
384 {
385         struct list_head list;
386
387         bool enabled;
388         const char *name;
389
390         enum fw3_family family;
391
392         struct fw3_zone *_src;
393         struct fw3_zone *_dest;
394
395         struct fw3_device src;
396         struct fw3_device dest;
397         struct fw3_setmatch ipset;
398         struct fw3_cthelpermatch helper;
399
400         struct list_head proto;
401
402         struct fw3_address ip_src;
403         struct list_head mac_src;
404         struct fw3_port port_src;
405
406         struct fw3_address ip_dest;
407         struct fw3_port port_dest;
408
409         struct fw3_address ip_redir;
410         struct fw3_port port_redir;
411
412         struct fw3_limit limit;
413         struct fw3_time time;
414         struct fw3_mark mark;
415
416         enum fw3_flag target;
417
418         const char *extra;
419
420         bool local;
421         bool reflection;
422         enum fw3_reflection_source reflection_src;
423 };
424
425 struct fw3_snat
426 {
427         struct list_head list;
428
429         bool enabled;
430         const char *name;
431
432         enum fw3_family family;
433
434         struct fw3_zone *_src;
435
436         struct fw3_device src;
437         struct fw3_setmatch ipset;
438         struct fw3_cthelpermatch helper;
439         const char *device;
440
441         struct list_head proto;
442
443         struct fw3_address ip_src;
444         struct fw3_port port_src;
445
446         struct fw3_address ip_dest;
447         struct fw3_port port_dest;
448
449         struct fw3_address ip_snat;
450         struct fw3_port port_snat;
451
452         struct fw3_limit limit;
453         struct fw3_time time;
454         struct fw3_mark mark;
455         bool connlimit_ports;
456
457         enum fw3_flag target;
458
459         const char *extra;
460 };
461
462 struct fw3_forward
463 {
464         struct list_head list;
465
466         bool enabled;
467         const char *name;
468
469         enum fw3_family family;
470
471         struct fw3_zone *_src;
472         struct fw3_zone *_dest;
473
474         struct fw3_device src;
475         struct fw3_device dest;
476 };
477
478 struct fw3_ipset
479 {
480         struct list_head list;
481
482         bool enabled;
483         const char *name;
484         enum fw3_family family;
485
486         enum fw3_ipset_method method;
487         struct list_head datatypes;
488
489         struct fw3_address iprange;
490         struct fw3_port portrange;
491
492         int netmask;
493         int maxelem;
494         int hashsize;
495
496         int timeout;
497
498         const char *external;
499
500         struct list_head entries;
501         const char *loadfile;
502
503         uint32_t flags[2];
504 };
505
506 struct fw3_include
507 {
508         struct list_head list;
509
510         bool enabled;
511         const char *name;
512         enum fw3_family family;
513
514         const char *path;
515         enum fw3_include_type type;
516
517         bool reload;
518 };
519
520 struct fw3_cthelper
521 {
522         struct list_head list;
523
524         bool enabled;
525         const char *name;
526         const char *module;
527         const char *description;
528         enum fw3_family family;
529         struct list_head proto;
530         struct fw3_port port;
531 };
532
533 struct fw3_setentry
534 {
535         struct list_head list;
536         const char *value;
537 };
538
539 struct fw3_state
540 {
541         struct uci_context *uci;
542         struct fw3_defaults defaults;
543         struct list_head zones;
544         struct list_head rules;
545         struct list_head redirects;
546         struct list_head snats;
547         struct list_head forwards;
548         struct list_head ipsets;
549         struct list_head includes;
550         struct list_head cthelpers;
551
552         bool disable_ipsets;
553         bool statefile;
554 };
555
556 struct fw3_chain_spec {
557         int family;
558         int table;
559         int flag;
560         const char *format;
561 };
562
563
564 struct fw3_option
565 {
566         const char *name;
567         bool (*parse)(void *, const char *, bool);
568         uintptr_t offset;
569         size_t elem_size;
570 };
571
572 #define FW3_OPT(name, parse, structure, member) \
573         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
574
575 #define FW3_LIST(name, parse, structure, member) \
576         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
577           sizeof(struct fw3_##structure) }
578
579 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
580 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
581 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
582 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
583 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
584 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
585 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
586 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
587 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
588 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
589 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
590 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
591 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
592
593 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
594 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
595
596 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
597 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
598
599 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
600 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
601 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
602 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
603 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
604 bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
605 bool fw3_parse_direction(void *ptr, const char *val, bool is_list);
606 bool fw3_parse_cthelper(void *ptr, const char *val, bool is_list);
607 bool fw3_parse_setentry(void *ptr, const char *val, bool is_list);
608
609 bool fw3_parse_options(void *s, const struct fw3_option *opts,
610                        struct uci_section *section);
611 bool fw3_parse_blob_options(void *s, const struct fw3_option *opts,
612                             struct blob_attr *a, const char *name);
613
614 const char * fw3_address_to_string(struct fw3_address *address,
615                                    bool allow_invert, bool as_cidr);
616
617 #endif