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