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