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