firewall3: make reject types selectable by user
[project/firewall3.git] / options.h
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
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 #define _LINUX_IN_H
36 #define _LINUX_IN6_H
37 #include <netinet/in.h>
38 #include <netinet/ether.h>
39
40 #include <time.h>
41
42 #include <uci.h>
43
44 #include <libubox/list.h>
45 #include <libubox/utils.h>
46 #include <libubox/blobmsg.h>
47
48 #include "icmp_codes.h"
49 #include "utils.h"
50
51
52 enum fw3_table
53 {
54 FW3_TABLE_FILTER = 0,
55 FW3_TABLE_NAT = 1,
56 FW3_TABLE_MANGLE = 2,
57 FW3_TABLE_RAW = 3,
58 };
59
60 enum fw3_family
61 {
62 FW3_FAMILY_ANY = 0,
63 FW3_FAMILY_V4 = 4,
64 FW3_FAMILY_V6 = 5,
65 };
66
67 enum fw3_flag
68 {
69 FW3_FLAG_UNSPEC = 0,
70 FW3_FLAG_ACCEPT = 6,
71 FW3_FLAG_REJECT = 7,
72 FW3_FLAG_DROP = 8,
73 FW3_FLAG_NOTRACK = 9,
74 FW3_FLAG_HELPER = 10,
75 FW3_FLAG_MARK = 11,
76 FW3_FLAG_DNAT = 12,
77 FW3_FLAG_SNAT = 13,
78 FW3_FLAG_MASQUERADE = 14,
79 FW3_FLAG_SRC_ACCEPT = 15,
80 FW3_FLAG_SRC_REJECT = 16,
81 FW3_FLAG_SRC_DROP = 17,
82 FW3_FLAG_CUSTOM_CHAINS = 18,
83 FW3_FLAG_SYN_FLOOD = 19,
84 FW3_FLAG_MTU_FIX = 20,
85 FW3_FLAG_DROP_INVALID = 21,
86 FW3_FLAG_HOTPLUG = 22,
87
88 __FW3_FLAG_MAX
89 };
90
91 enum fw3_reject_code
92 {
93 FW3_REJECT_CODE_TCP_RESET = 0,
94 FW3_REJECT_CODE_PORT_UNREACH = 1,
95 FW3_REJECT_CODE_ADM_PROHIBITED = 2,
96
97 __FW3_REJECT_CODE_MAX
98 };
99
100 extern const char *fw3_flag_names[__FW3_FLAG_MAX];
101
102
103 enum fw3_limit_unit
104 {
105 FW3_LIMIT_UNIT_SECOND = 0,
106 FW3_LIMIT_UNIT_MINUTE = 1,
107 FW3_LIMIT_UNIT_HOUR = 2,
108 FW3_LIMIT_UNIT_DAY = 3,
109
110 __FW3_LIMIT_UNIT_MAX
111 };
112
113 extern const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX];
114
115
116 enum fw3_ipset_method
117 {
118 FW3_IPSET_METHOD_UNSPEC = 0,
119 FW3_IPSET_METHOD_BITMAP = 1,
120 FW3_IPSET_METHOD_HASH = 2,
121 FW3_IPSET_METHOD_LIST = 3,
122
123 __FW3_IPSET_METHOD_MAX
124 };
125
126 enum fw3_ipset_type
127 {
128 FW3_IPSET_TYPE_UNSPEC = 0,
129 FW3_IPSET_TYPE_IP = 1,
130 FW3_IPSET_TYPE_PORT = 2,
131 FW3_IPSET_TYPE_MAC = 3,
132 FW3_IPSET_TYPE_NET = 4,
133 FW3_IPSET_TYPE_SET = 5,
134
135 __FW3_IPSET_TYPE_MAX
136 };
137
138 extern const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX];
139 extern const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX];
140
141
142 enum fw3_include_type
143 {
144 FW3_INC_TYPE_SCRIPT = 0,
145 FW3_INC_TYPE_RESTORE = 1,
146 };
147
148 enum fw3_reflection_source
149 {
150 FW3_REFLECTION_INTERNAL = 0,
151 FW3_REFLECTION_EXTERNAL = 1,
152 };
153
154 struct fw3_ipset_datatype
155 {
156 struct list_head list;
157 enum fw3_ipset_type type;
158 const char *dir;
159 };
160
161 struct fw3_setmatch
162 {
163 bool set;
164 bool invert;
165 char name[32];
166 const char *dir[3];
167 struct fw3_ipset *ptr;
168 };
169
170 struct fw3_device
171 {
172 struct list_head list;
173
174 bool set;
175 bool any;
176 bool invert;
177 char name[32];
178 char network[32];
179 };
180
181 struct fw3_address
182 {
183 struct list_head list;
184
185 bool set;
186 bool range;
187 bool invert;
188 bool resolved;
189 enum fw3_family family;
190 union {
191 struct in_addr v4;
192 struct in6_addr v6;
193 struct ether_addr mac;
194 } address;
195 union {
196 struct in_addr v4;
197 struct in6_addr v6;
198 struct ether_addr mac;
199 } mask;
200 };
201
202 struct fw3_mac
203 {
204 struct list_head list;
205
206 bool set;
207 bool invert;
208 struct ether_addr mac;
209 };
210
211 struct fw3_protocol
212 {
213 struct list_head list;
214
215 bool any;
216 bool invert;
217 uint32_t protocol;
218 };
219
220 struct fw3_port
221 {
222 struct list_head list;
223
224 bool set;
225 bool invert;
226 uint16_t port_min;
227 uint16_t port_max;
228 };
229
230 struct fw3_icmptype
231 {
232 struct list_head list;
233
234 bool invert;
235 enum fw3_family family;
236 uint8_t type;
237 uint8_t code_min;
238 uint8_t code_max;
239 uint8_t type6;
240 uint8_t code6_min;
241 uint8_t code6_max;
242 };
243
244 struct fw3_limit
245 {
246 bool invert;
247 int rate;
248 int burst;
249 enum fw3_limit_unit unit;
250 };
251
252 struct fw3_time
253 {
254 bool utc;
255 struct tm datestart;
256 struct tm datestop;
257 uint32_t timestart;
258 uint32_t timestop;
259 uint32_t monthdays; /* bit 0 is invert + 1 .. 31 */
260 uint8_t weekdays; /* bit 0 is invert + 1 .. 7 */
261 };
262
263 struct fw3_mark
264 {
265 bool set;
266 bool invert;
267 uint32_t mark;
268 uint32_t mask;
269 };
270
271 struct fw3_cthelpermatch
272 {
273 struct list_head list;
274
275 bool set;
276 bool invert;
277 char name[32];
278 struct fw3_cthelper *ptr;
279 };
280
281 struct fw3_defaults
282 {
283 enum fw3_flag policy_input;
284 enum fw3_flag policy_output;
285 enum fw3_flag policy_forward;
286
287 bool drop_invalid;
288 enum fw3_reject_code tcp_reject_code;
289 enum fw3_reject_code any_reject_code;
290
291 bool syn_flood;
292 struct fw3_limit syn_flood_rate;
293
294 bool tcp_syncookies;
295 int tcp_ecn;
296 bool tcp_window_scaling;
297
298 bool accept_redirects;
299 bool accept_source_route;
300
301 bool custom_chains;
302 bool auto_helper;
303 bool flow_offloading;
304 bool flow_offloading_hw;
305
306 bool disable_ipv6;
307
308 uint32_t flags[2];
309 };
310
311 struct fw3_zone
312 {
313 struct list_head list;
314
315 bool enabled;
316 const char *name;
317
318 enum fw3_family family;
319
320 enum fw3_flag policy_input;
321 enum fw3_flag policy_output;
322 enum fw3_flag policy_forward;
323
324 struct list_head networks;
325 struct list_head devices;
326 struct list_head subnets;
327
328 const char *extra_src;
329 const char *extra_dest;
330
331 bool masq;
332 bool masq_allow_invalid;
333 struct list_head masq_src;
334 struct list_head masq_dest;
335
336 bool mtu_fix;
337
338 struct list_head cthelpers;
339
340 int log;
341 struct fw3_limit log_limit;
342
343 bool custom_chains;
344 bool auto_helper;
345
346 uint32_t flags[2];
347
348 struct list_head old_addrs;
349 };
350
351 struct fw3_rule
352 {
353 struct list_head list;
354
355 bool enabled;
356 const char *name;
357
358 enum fw3_family family;
359
360 struct fw3_zone *_src;
361 struct fw3_zone *_dest;
362
363 const char *device;
364 bool direction_out;
365
366 struct fw3_device src;
367 struct fw3_device dest;
368 struct fw3_setmatch ipset;
369 struct fw3_cthelpermatch helper;
370
371 struct list_head proto;
372
373 struct list_head ip_src;
374 struct list_head mac_src;
375 struct list_head port_src;
376
377 struct list_head ip_dest;
378 struct list_head port_dest;
379
380 struct list_head icmp_type;
381
382 struct fw3_limit limit;
383 struct fw3_time time;
384 struct fw3_mark mark;
385
386 enum fw3_flag target;
387 struct fw3_mark set_mark;
388 struct fw3_mark set_xmark;
389 struct fw3_cthelpermatch set_helper;
390
391 const char *extra;
392 };
393
394 struct fw3_redirect
395 {
396 struct list_head list;
397
398 bool enabled;
399 const char *name;
400
401 enum fw3_family family;
402
403 struct fw3_zone *_src;
404 struct fw3_zone *_dest;
405
406 struct fw3_device src;
407 struct fw3_device dest;
408 struct fw3_setmatch ipset;
409 struct fw3_cthelpermatch helper;
410
411 struct list_head proto;
412
413 struct fw3_address ip_src;
414 struct list_head mac_src;
415 struct fw3_port port_src;
416
417 struct fw3_address ip_dest;
418 struct fw3_port port_dest;
419
420 struct fw3_address ip_redir;
421 struct fw3_port port_redir;
422
423 struct fw3_limit limit;
424 struct fw3_time time;
425 struct fw3_mark mark;
426
427 enum fw3_flag target;
428
429 const char *extra;
430
431 bool local;
432 bool reflection;
433 enum fw3_reflection_source reflection_src;
434 };
435
436 struct fw3_snat
437 {
438 struct list_head list;
439
440 bool enabled;
441 const char *name;
442
443 enum fw3_family family;
444
445 struct fw3_zone *_src;
446
447 struct fw3_device src;
448 struct fw3_setmatch ipset;
449 struct fw3_cthelpermatch helper;
450 const char *device;
451
452 struct list_head proto;
453
454 struct fw3_address ip_src;
455 struct fw3_port port_src;
456
457 struct fw3_address ip_dest;
458 struct fw3_port port_dest;
459
460 struct fw3_address ip_snat;
461 struct fw3_port port_snat;
462
463 struct fw3_limit limit;
464 struct fw3_time time;
465 struct fw3_mark mark;
466 bool connlimit_ports;
467
468 enum fw3_flag target;
469
470 const char *extra;
471 };
472
473 struct fw3_forward
474 {
475 struct list_head list;
476
477 bool enabled;
478 const char *name;
479
480 enum fw3_family family;
481
482 struct fw3_zone *_src;
483 struct fw3_zone *_dest;
484
485 struct fw3_device src;
486 struct fw3_device dest;
487 };
488
489 struct fw3_ipset
490 {
491 struct list_head list;
492
493 bool enabled;
494 const char *name;
495 enum fw3_family family;
496
497 enum fw3_ipset_method method;
498 struct list_head datatypes;
499
500 struct fw3_address iprange;
501 struct fw3_port portrange;
502
503 int netmask;
504 int maxelem;
505 int hashsize;
506
507 int timeout;
508
509 const char *external;
510
511 struct list_head entries;
512 const char *loadfile;
513
514 uint32_t flags[2];
515 };
516
517 struct fw3_include
518 {
519 struct list_head list;
520
521 bool enabled;
522 const char *name;
523 enum fw3_family family;
524
525 const char *path;
526 enum fw3_include_type type;
527
528 bool reload;
529 };
530
531 struct fw3_cthelper
532 {
533 struct list_head list;
534
535 bool enabled;
536 const char *name;
537 const char *module;
538 const char *description;
539 enum fw3_family family;
540 struct list_head proto;
541 struct fw3_port port;
542 };
543
544 struct fw3_setentry
545 {
546 struct list_head list;
547 const char *value;
548 };
549
550 struct fw3_state
551 {
552 struct uci_context *uci;
553 struct fw3_defaults defaults;
554 struct list_head zones;
555 struct list_head rules;
556 struct list_head redirects;
557 struct list_head snats;
558 struct list_head forwards;
559 struct list_head ipsets;
560 struct list_head includes;
561 struct list_head cthelpers;
562
563 bool disable_ipsets;
564 bool statefile;
565 };
566
567 struct fw3_chain_spec {
568 int family;
569 int table;
570 int flag;
571 const char *format;
572 };
573
574
575 struct fw3_option
576 {
577 const char *name;
578 bool (*parse)(void *, const char *, bool);
579 uintptr_t offset;
580 size_t elem_size;
581 };
582
583 #define FW3_OPT(name, parse, structure, member) \
584 { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
585
586 #define FW3_LIST(name, parse, structure, member) \
587 { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
588 sizeof(struct fw3_##structure) }
589
590 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
591 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
592 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
593 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
594 bool fw3_parse_reject_code(void *ptr, const char *val, bool is_list);
595 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
596 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
597 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
598 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
599 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
600 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
601 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
602 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
603 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
604
605 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
606 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
607
608 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
609 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
610
611 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
612 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
613 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
614 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
615 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
616 bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
617 bool fw3_parse_direction(void *ptr, const char *val, bool is_list);
618 bool fw3_parse_cthelper(void *ptr, const char *val, bool is_list);
619 bool fw3_parse_setentry(void *ptr, const char *val, bool is_list);
620
621 bool fw3_parse_options(void *s, const struct fw3_option *opts,
622 struct uci_section *section);
623 bool fw3_parse_blob_options(void *s, const struct fw3_option *opts,
624 struct blob_attr *a, const char *name);
625
626 const char * fw3_address_to_string(struct fw3_address *address,
627 bool allow_invert, bool as_cidr);
628
629 #endif