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