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