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