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