ubus: fix interface name and proto lookup
[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 struct list_head masq_src;
308 struct list_head masq_dest;
309
310 bool mtu_fix;
311
312 bool log;
313 struct fw3_limit log_limit;
314
315 bool custom_chains;
316
317 uint32_t flags[2];
318
319 struct list_head old_addrs;
320 };
321
322 struct fw3_rule
323 {
324 struct list_head list;
325
326 bool enabled;
327 const char *name;
328
329 enum fw3_family family;
330
331 struct fw3_zone *_src;
332 struct fw3_zone *_dest;
333
334 const char *device;
335 bool direction_out;
336
337 struct fw3_device src;
338 struct fw3_device dest;
339 struct fw3_setmatch ipset;
340
341 struct list_head proto;
342
343 struct list_head ip_src;
344 struct list_head mac_src;
345 struct list_head port_src;
346
347 struct list_head ip_dest;
348 struct list_head port_dest;
349
350 struct list_head icmp_type;
351
352 struct fw3_limit limit;
353 struct fw3_time time;
354 struct fw3_mark mark;
355
356 enum fw3_flag target;
357 struct fw3_mark set_mark;
358 struct fw3_mark set_xmark;
359
360 const char *extra;
361 };
362
363 struct fw3_redirect
364 {
365 struct list_head list;
366
367 bool enabled;
368 const char *name;
369
370 enum fw3_family family;
371
372 struct fw3_zone *_src;
373 struct fw3_zone *_dest;
374
375 struct fw3_device src;
376 struct fw3_device dest;
377 struct fw3_setmatch ipset;
378
379 struct list_head proto;
380
381 struct fw3_address ip_src;
382 struct list_head mac_src;
383 struct fw3_port port_src;
384
385 struct fw3_address ip_dest;
386 struct fw3_port port_dest;
387
388 struct fw3_address ip_redir;
389 struct fw3_port port_redir;
390
391 struct fw3_limit limit;
392 struct fw3_time time;
393 struct fw3_mark mark;
394
395 enum fw3_flag target;
396
397 const char *extra;
398
399 bool local;
400 bool reflection;
401 enum fw3_reflection_source reflection_src;
402 };
403
404 struct fw3_snat
405 {
406 struct list_head list;
407
408 bool enabled;
409 const char *name;
410
411 enum fw3_family family;
412
413 struct fw3_zone *_src;
414
415 struct fw3_device src;
416 struct fw3_setmatch ipset;
417 const char *device;
418
419 struct list_head proto;
420
421 struct fw3_address ip_src;
422 struct fw3_port port_src;
423
424 struct fw3_address ip_dest;
425 struct fw3_port port_dest;
426
427 struct fw3_address ip_snat;
428 struct fw3_port port_snat;
429
430 struct fw3_limit limit;
431 struct fw3_time time;
432 struct fw3_mark mark;
433 bool connlimit_ports;
434
435 enum fw3_flag target;
436
437 const char *extra;
438 };
439
440 struct fw3_forward
441 {
442 struct list_head list;
443
444 bool enabled;
445 const char *name;
446
447 enum fw3_family family;
448
449 struct fw3_zone *_src;
450 struct fw3_zone *_dest;
451
452 struct fw3_device src;
453 struct fw3_device dest;
454 };
455
456 struct fw3_ipset
457 {
458 struct list_head list;
459
460 bool enabled;
461 const char *name;
462 enum fw3_family family;
463
464 enum fw3_ipset_method method;
465 struct list_head datatypes;
466
467 struct fw3_address iprange;
468 struct fw3_port portrange;
469
470 int netmask;
471 int maxelem;
472 int hashsize;
473
474 int timeout;
475
476 const char *external;
477
478 uint32_t flags[2];
479 };
480
481 struct fw3_include
482 {
483 struct list_head list;
484
485 bool enabled;
486 const char *name;
487 enum fw3_family family;
488
489 const char *path;
490 enum fw3_include_type type;
491
492 bool reload;
493 };
494
495 struct fw3_state
496 {
497 struct uci_context *uci;
498 struct fw3_defaults defaults;
499 struct list_head zones;
500 struct list_head rules;
501 struct list_head redirects;
502 struct list_head snats;
503 struct list_head forwards;
504 struct list_head ipsets;
505 struct list_head includes;
506
507 bool disable_ipsets;
508 bool statefile;
509 };
510
511 struct fw3_chain_spec {
512 int family;
513 int table;
514 int flag;
515 const char *format;
516 };
517
518
519 struct fw3_option
520 {
521 const char *name;
522 bool (*parse)(void *, const char *, bool);
523 uintptr_t offset;
524 size_t elem_size;
525 };
526
527 #define FW3_OPT(name, parse, structure, member) \
528 { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
529
530 #define FW3_LIST(name, parse, structure, member) \
531 { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
532 sizeof(struct fw3_##structure) }
533
534 bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
535 bool fw3_parse_int(void *ptr, const char *val, bool is_list);
536 bool fw3_parse_string(void *ptr, const char *val, bool is_list);
537 bool fw3_parse_target(void *ptr, const char *val, bool is_list);
538 bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
539 bool fw3_parse_device(void *ptr, const char *val, bool is_list);
540 bool fw3_parse_address(void *ptr, const char *val, bool is_list);
541 bool fw3_parse_network(void *ptr, const char *val, bool is_list);
542 bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
543 bool fw3_parse_port(void *ptr, const char *val, bool is_list);
544 bool fw3_parse_family(void *ptr, const char *val, bool is_list);
545 bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
546 bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
547
548 bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
549 bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
550
551 bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
552 bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
553
554 bool fw3_parse_date(void *ptr, const char *val, bool is_list);
555 bool fw3_parse_time(void *ptr, const char *val, bool is_list);
556 bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
557 bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
558 bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
559 bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
560 bool fw3_parse_direction(void *ptr, const char *val, bool is_list);
561
562 bool fw3_parse_options(void *s, const struct fw3_option *opts,
563 struct uci_section *section);
564 bool fw3_parse_blob_options(void *s, const struct fw3_option *opts,
565 struct blob_attr *a, const char *name);
566
567 const char * fw3_address_to_string(struct fw3_address *address,
568 bool allow_invert, bool as_cidr);
569
570 #endif