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