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