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