selectively delete chains in filter and nat tables
[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 <uci.h>
39
40 #include <libubox/list.h>
41 #include <libubox/utils.h>
42
43 #include "icmp_codes.h"
44 #include "utils.h"
45
46
47 enum fw3_table
48 {
49         FW3_TABLE_FILTER = 0,
50         FW3_TABLE_NAT    = 1,
51         FW3_TABLE_MANGLE = 2,
52         FW3_TABLE_RAW    = 3,
53 };
54
55 enum fw3_family
56 {
57         FW3_FAMILY_ANY = 0,
58         FW3_FAMILY_V4  = 1,
59         FW3_FAMILY_V6  = 2,
60 };
61
62 enum fw3_target
63 {
64         FW3_TARGET_UNSPEC  = 0,
65         FW3_TARGET_ACCEPT  = 1,
66         FW3_TARGET_REJECT  = 2,
67         FW3_TARGET_DROP    = 3,
68         FW3_TARGET_NOTRACK = 4,
69         FW3_TARGET_DNAT    = 5,
70         FW3_TARGET_SNAT    = 6,
71 };
72
73 enum fw3_default
74 {
75         FW3_DEFAULT_UNSPEC        = 0,
76         FW3_DEFAULT_CUSTOM_CHAINS = 1,
77         FW3_DEFAULT_SYN_FLOOD     = 2,
78         FW3_DEFAULT_MTU_FIX       = 3,
79         FW3_DEFAULT_DROP_INVALID  = 4,
80 };
81
82 enum fw3_limit_unit
83 {
84         FW3_LIMIT_UNIT_SECOND = 0,
85         FW3_LIMIT_UNIT_MINUTE = 1,
86         FW3_LIMIT_UNIT_HOUR   = 2,
87         FW3_LIMIT_UNIT_DAY    = 3,
88 };
89
90 enum fw3_ipset_method
91 {
92         FW3_IPSET_METHOD_UNSPEC = 0,
93         FW3_IPSET_METHOD_BITMAP = 1,
94         FW3_IPSET_METHOD_HASH   = 2,
95         FW3_IPSET_METHOD_LIST   = 3,
96 };
97
98 enum fw3_ipset_type
99 {
100         FW3_IPSET_TYPE_UNSPEC = 0,
101         FW3_IPSET_TYPE_IP     = 1,
102         FW3_IPSET_TYPE_PORT   = 2,
103         FW3_IPSET_TYPE_MAC    = 3,
104         FW3_IPSET_TYPE_NET    = 4,
105         FW3_IPSET_TYPE_SET    = 5,
106 };
107
108 struct fw3_ipset_datatype
109 {
110         struct list_head list;
111         enum fw3_ipset_type type;
112         bool dest;
113 };
114
115 struct fw3_device
116 {
117         struct list_head list;
118
119         bool set;
120         bool any;
121         bool invert;
122         char name[32];
123 };
124
125 struct fw3_address
126 {
127         struct list_head list;
128
129         bool set;
130         bool invert;
131         enum fw3_family family;
132         int mask;
133         union {
134                 struct in_addr v4;
135                 struct in6_addr v6;
136                 struct ether_addr mac;
137         } address;
138 };
139
140 struct fw3_mac
141 {
142         struct list_head list;
143
144         bool set;
145         bool invert;
146         struct ether_addr mac;
147 };
148
149 struct fw3_protocol
150 {
151         struct list_head list;
152
153         bool any;
154         bool invert;
155         uint16_t protocol;
156 };
157
158 struct fw3_port
159 {
160         struct list_head list;
161
162         bool set;
163         bool invert;
164         uint16_t port_min;
165         uint16_t port_max;
166 };
167
168 struct fw3_icmptype
169 {
170         struct list_head list;
171
172         bool invert;
173         enum fw3_family family;
174         uint8_t type;
175         uint8_t code_min;
176         uint8_t code_max;
177         uint8_t type6;
178         uint8_t code6_min;
179         uint8_t code6_max;
180 };
181
182 struct fw3_limit
183 {
184         bool invert;
185         int rate;
186         int burst;
187         enum fw3_limit_unit unit;
188 };
189
190 struct fw3_defaults
191 {
192         enum fw3_target policy_input;
193         enum fw3_target policy_output;
194         enum fw3_target policy_forward;
195
196         bool drop_invalid;
197
198         bool syn_flood;
199         struct fw3_limit syn_flood_rate;
200
201         bool tcp_syncookies;
202         bool tcp_ecn;
203         bool tcp_westwood;
204         bool tcp_window_scaling;
205
206         bool accept_redirects;
207         bool accept_source_route;
208
209         bool custom_chains;
210
211         bool disable_ipv6;
212
213         uint8_t has_flag;
214 };
215
216 struct fw3_zone
217 {
218         struct list_head list;
219
220         const char *name;
221
222         enum fw3_family family;
223
224         enum fw3_target policy_input;
225         enum fw3_target policy_output;
226         enum fw3_target policy_forward;
227
228         struct list_head networks;
229         struct list_head devices;
230         struct list_head subnets;
231
232         const char *extra_src;
233         const char *extra_dest;
234
235         bool masq;
236         struct list_head masq_src;
237         struct list_head masq_dest;
238
239         bool conntrack;
240         bool mtu_fix;
241
242         bool log;
243         struct fw3_limit log_limit;
244
245         bool custom_chains;
246
247         uint8_t has_src_target;
248         uint8_t has_dest_target;
249 };
250
251 struct fw3_rule
252 {
253         struct list_head list;
254
255         const char *name;
256
257         enum fw3_family family;
258
259         struct fw3_zone *_src;
260         struct fw3_zone *_dest;
261
262         struct fw3_device src;
263         struct fw3_device dest;
264
265         struct fw3_ipset *_ipset;
266         struct fw3_device ipset;
267
268         struct list_head proto;
269
270         struct list_head ip_src;
271         struct list_head mac_src;
272         struct list_head port_src;
273
274         struct list_head ip_dest;
275         struct list_head port_dest;
276
277         struct list_head icmp_type;
278
279         enum fw3_target target;
280
281         struct fw3_limit limit;
282
283         const char *extra;
284 };
285
286 struct fw3_redirect
287 {
288         struct list_head list;
289
290         const char *name;
291
292         enum fw3_family family;
293
294         struct fw3_zone *_src;
295         struct fw3_zone *_dest;
296
297         struct fw3_device src;
298         struct fw3_device dest;
299
300         struct fw3_ipset *_ipset;
301         struct fw3_device ipset;
302
303         struct list_head proto;
304
305         struct fw3_address ip_src;
306         struct list_head mac_src;
307         struct fw3_port port_src;
308
309         struct fw3_address ip_dest;
310         struct fw3_port port_dest;
311
312         struct fw3_address ip_redir;
313         struct fw3_port port_redir;
314
315         enum fw3_target target;
316
317         const char *extra;
318
319         bool reflection;
320 };
321
322 struct fw3_forward
323 {
324         struct list_head list;
325
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
337 struct fw3_ipset
338 {
339         struct list_head list;
340
341         const char *name;
342         enum fw3_family family;
343
344         enum fw3_ipset_method method;
345         struct list_head datatypes;
346
347         struct list_head iprange;
348         struct fw3_port portrange;
349
350         int netmask;
351         int maxelem;
352         int hashsize;
353
354         int timeout;
355
356         const char *external;
357 };
358
359 struct fw3_state
360 {
361         struct uci_context *uci;
362         struct fw3_defaults defaults;
363         struct list_head zones;
364         struct list_head rules;
365         struct list_head redirects;
366         struct list_head forwards;
367         struct list_head ipsets;
368
369         bool disable_ipsets;
370 };
371
372
373 struct fw3_option
374 {
375         const char *name;
376         bool (*parse)(void *, const char *);
377         uintptr_t offset;
378         size_t elem_size;
379 };
380
381 #define FW3_OPT(name, parse, structure, member) \
382         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
383
384 #define FW3_LIST(name, parse, structure, member) \
385         { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
386           sizeof(struct fw3_##structure) }
387
388
389 bool fw3_parse_bool(void *ptr, const char *val);
390 bool fw3_parse_int(void *ptr, const char *val);
391 bool fw3_parse_string(void *ptr, const char *val);
392 bool fw3_parse_target(void *ptr, const char *val);
393 bool fw3_parse_limit(void *ptr, const char *val);
394 bool fw3_parse_device(void *ptr, const char *val);
395 bool fw3_parse_address(void *ptr, const char *val);
396 bool fw3_parse_mac(void *ptr, const char *val);
397 bool fw3_parse_port(void *ptr, const char *val);
398 bool fw3_parse_family(void *ptr, const char *val);
399 bool fw3_parse_icmptype(void *ptr, const char *val);
400 bool fw3_parse_protocol(void *ptr, const char *val);
401 bool fw3_parse_ipset_method(void *ptr, const char *val);
402 bool fw3_parse_ipset_datatype(void *ptr, const char *val);
403
404 void fw3_parse_options(void *s, struct fw3_option *opts, int n,
405                        struct uci_section *section);
406
407 void fw3_format_in_out(struct fw3_device *in, struct fw3_device *out);
408 void fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest);
409 void fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp);
410 void fw3_format_mac(struct fw3_mac *mac);
411 void fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family);
412 void fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family);
413 void fw3_format_limit(struct fw3_limit *limit);
414 void fw3_format_ipset(struct fw3_ipset *ipset, bool invert);
415
416 void __fw3_format_comment(const char *comment, ...);
417 #define fw3_format_comment(...) __fw3_format_comment(__VA_ARGS__, NULL)
418
419 void fw3_format_extra(const char *extra);
420
421 #endif