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