Add common fw3_address_to_string() helper function
[project/firewall3.git] / options.h
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 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_SRC_ACCEPT    = 13,
75         FW3_FLAG_SRC_REJECT    = 14,
76         FW3_FLAG_SRC_DROP      = 15,
77         FW3_FLAG_CUSTOM_CHAINS = 16,
78         FW3_FLAG_SYN_FLOOD     = 17,
79         FW3_FLAG_MTU_FIX       = 18,
80         FW3_FLAG_DROP_INVALID  = 19,
81         FW3_FLAG_HOTPLUG       = 20,
82
83         __FW3_FLAG_MAX
84 };
85
86 extern const char *fw3_flag_names[__FW3_FLAG_MAX];
87
88
89 enum fw3_limit_unit
90 {
91         FW3_LIMIT_UNIT_SECOND = 0,
92         FW3_LIMIT_UNIT_MINUTE = 1,
93         FW3_LIMIT_UNIT_HOUR   = 2,
94         FW3_LIMIT_UNIT_DAY    = 3,
95 };
96
97 enum fw3_ipset_method
98 {
99         FW3_IPSET_METHOD_UNSPEC = 0,
100         FW3_IPSET_METHOD_BITMAP = 1,
101         FW3_IPSET_METHOD_HASH   = 2,
102         FW3_IPSET_METHOD_LIST   = 3,
103 };
104
105 enum fw3_ipset_type
106 {
107         FW3_IPSET_TYPE_UNSPEC = 0,
108         FW3_IPSET_TYPE_IP     = 1,
109         FW3_IPSET_TYPE_PORT   = 2,
110         FW3_IPSET_TYPE_MAC    = 3,
111         FW3_IPSET_TYPE_NET    = 4,
112         FW3_IPSET_TYPE_SET    = 5,
113 };
114
115 enum fw3_include_type
116 {
117         FW3_INC_TYPE_SCRIPT   = 0,
118         FW3_INC_TYPE_RESTORE  = 1,
119 };
120
121 enum fw3_reflection_source
122 {
123         FW3_REFLECTION_INTERNAL = 0,
124         FW3_REFLECTION_EXTERNAL = 1,
125 };
126
127 struct fw3_ipset_datatype
128 {
129         struct list_head list;
130         enum fw3_ipset_type type;
131         bool dest;
132 };
133
134 struct fw3_device
135 {
136         struct list_head list;
137
138         bool set;
139         bool any;
140         bool invert;
141         char name[32];
142         struct fw3_device *network;
143 };
144
145 struct fw3_address
146 {
147         struct list_head list;
148
149         bool set;
150         bool range;
151         bool invert;
152         enum fw3_family family;
153         int mask;
154         union {
155                 struct in_addr v4;
156                 struct in6_addr v6;
157                 struct ether_addr mac;
158         } address;
159         union {
160                 struct in_addr v4;
161                 struct in6_addr v6;
162                 struct ether_addr mac;
163         } address2;
164 };
165
166 struct fw3_mac
167 {
168         struct list_head list;
169
170         bool set;
171         bool invert;
172         struct ether_addr mac;
173 };
174
175 struct fw3_protocol
176 {
177         struct list_head list;
178
179         bool any;
180         bool invert;
181         uint32_t protocol;
182 };
183
184 struct fw3_port
185 {
186         struct list_head list;
187
188         bool set;
189         bool invert;
190         uint16_t port_min;
191         uint16_t port_max;
192 };
193
194 struct fw3_icmptype
195 {
196         struct list_head list;
197
198         bool invert;
199         enum fw3_family family;
200         uint8_t type;
201         uint8_t code_min;
202         uint8_t code_max;
203         uint8_t type6;
204         uint8_t code6_min;
205         uint8_t code6_max;
206 };
207
208 struct fw3_limit
209 {
210         bool invert;
211         int rate;
212         int burst;
213         enum fw3_limit_unit unit;
214 };
215
216 struct fw3_time
217 {
218         bool utc;
219         struct tm datestart;
220         struct tm datestop;
221         uint32_t timestart;
222         uint32_t timestop;
223         uint32_t monthdays; /* bit 0 is invert + 1 .. 31 */
224         uint8_t weekdays;   /* bit 0 is invert + 1 .. 7 */
225 };
226
227 struct fw3_mark
228 {
229         bool set;
230         bool invert;
231         uint32_t mark;
232         uint32_t mask;
233 };
234
235 struct fw3_defaults
236 {
237         enum fw3_flag policy_input;
238         enum fw3_flag policy_output;
239         enum fw3_flag policy_forward;
240
241         bool drop_invalid;
242
243         bool syn_flood;
244         struct fw3_limit syn_flood_rate;
245
246         bool tcp_syncookies;
247         bool tcp_ecn;
248         bool tcp_window_scaling;
249
250         bool accept_redirects;
251         bool accept_source_route;
252
253         bool custom_chains;
254
255         bool disable_ipv6;
256
257         uint32_t flags[2];
258 };
259
260 struct fw3_zone
261 {
262         struct list_head list;
263
264         bool enabled;
265         const char *name;
266
267         enum fw3_family family;
268
269         enum fw3_flag policy_input;
270         enum fw3_flag policy_output;
271         enum fw3_flag policy_forward;
272
273         struct list_head networks;
274         struct list_head devices;
275         struct list_head subnets;
276
277         const char *extra_src;
278         const char *extra_dest;
279
280         bool masq;
281         struct list_head masq_src;
282         struct list_head masq_dest;
283
284         bool conntrack;
285         bool mtu_fix;
286
287         bool log;
288         struct fw3_limit log_limit;
289
290         bool custom_chains;
291
292         uint32_t flags[2];
293 };
294
295 struct fw3_rule
296 {
297         struct list_head list;
298
299         bool enabled;
300         const char *name;
301
302         enum fw3_family family;
303
304         struct fw3_zone *_src;
305         struct fw3_zone *_dest;
306
307         struct fw3_device src;
308         struct fw3_device dest;
309
310         struct fw3_ipset *_ipset;
311         struct fw3_device ipset;
312
313         struct list_head proto;
314
315         struct list_head ip_src;
316         struct list_head mac_src;
317         struct list_head port_src;
318
319         struct list_head ip_dest;
320         struct list_head port_dest;
321
322         struct list_head icmp_type;
323
324         struct fw3_limit limit;
325         struct fw3_time time;
326         struct fw3_mark mark;
327
328         enum fw3_flag target;
329         struct fw3_mark set_mark;
330         struct fw3_mark set_xmark;
331
332         const char *extra;
333 };
334
335 struct fw3_redirect
336 {
337         struct list_head list;
338
339         bool enabled;
340         const char *name;
341
342         enum fw3_family family;
343
344         struct fw3_zone *_src;
345         struct fw3_zone *_dest;
346
347         struct fw3_device src;
348         struct fw3_device dest;
349
350         struct fw3_ipset *_ipset;
351         struct fw3_device ipset;
352
353         struct list_head proto;
354
355         struct fw3_address ip_src;
356         struct list_head mac_src;
357         struct fw3_port port_src;
358
359         struct fw3_address ip_dest;
360         struct fw3_port port_dest;
361
362         struct fw3_address ip_redir;
363         struct fw3_port port_redir;
364
365         struct fw3_time time;
366         struct fw3_mark mark;
367
368         enum fw3_flag target;
369
370         const char *extra;
371
372         bool reflection;
373         enum fw3_reflection_source reflection_src;
374 };
375
376 struct fw3_forward
377 {
378         struct list_head list;
379
380         bool enabled;
381         const char *name;
382
383         enum fw3_family family;
384
385         struct fw3_zone *_src;
386         struct fw3_zone *_dest;
387
388         struct fw3_device src;
389         struct fw3_device dest;
390 };
391
392 struct fw3_ipset
393 {
394         struct list_head list;
395
396         bool enabled;
397         const char *name;
398         enum fw3_family family;
399
400         enum fw3_ipset_method method;
401         struct list_head datatypes;
402
403         struct fw3_address iprange;
404         struct fw3_port portrange;
405
406         int netmask;
407         int maxelem;
408         int hashsize;
409
410         int timeout;
411
412         const char *external;
413
414         uint32_t flags[2];
415 };
416
417 struct fw3_include
418 {
419         struct list_head list;
420
421         bool enabled;
422         const char *name;
423         enum fw3_family family;
424
425         const char *path;
426         enum fw3_include_type type;
427
428         bool reload;
429 };
430
431 struct fw3_state
432 {
433         struct uci_context *uci;
434         struct fw3_defaults defaults;
435         struct list_head zones;
436         struct list_head rules;
437         struct list_head redirects;
438         struct list_head forwards;
439         struct list_head ipsets;
440         struct list_head includes;
441
442         bool disable_ipsets;
443         bool statefile;
444 };
445
446
447 struct fw3_option
448 {
449         const char *name;
450         bool (*parse)(void *, const char *, bool);
451         uintptr_t offset;
452         size_t elem_size;
453 };
454
455 #define FW3_OPT(name, parse, structure, member) \
456         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
457
458 #define FW3_LIST(name, parse, structure, member) \
459         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
460           sizeof(struct fw3_##structure) }
461
462 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
463 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
464 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
465 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
466 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
467 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
468 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
469 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
470 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
471 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
472 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
473 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
474 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
475
476 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
477 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
478
479 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
480 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
481
482 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
483 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
484 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
485 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
486 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
487
488 void fw3_parse_options(void *s, const struct fw3_option *opts,
489                        struct uci_section *section);
490
491 const char * fw3_address_to_string(struct fw3_address *address,
492                                    bool allow_invert);
493
494 void fw3_format_in_out(struct fw3_device *in, struct fw3_device *out);
495 void fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest);
496 void fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp);
497 void fw3_format_mac(struct fw3_mac *mac);
498 void fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family);
499 void fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family);
500 void fw3_format_limit(struct fw3_limit *limit);
501 void fw3_format_ipset(struct fw3_ipset *ipset, bool invert);
502 void fw3_format_time(struct fw3_time *time);
503 void fw3_format_mark(struct fw3_mark *mark);
504
505 void __fw3_format_comment(const char *comment, ...);
506 #define fw3_format_comment(...) __fw3_format_comment(__VA_ARGS__, NULL)
507
508 void fw3_format_extra(const char *extra);
509
510 #endif