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