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