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