unify object freeing
[project/firewall3.git] / options.c
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 #include "options.h"
20
21 const char *fw3_flag_names[FW3_DEFAULT_DROP_INVALID + 1] = {
22 "filter",
23 "nat",
24 "mangle",
25 "raw",
26
27 "IPv4",
28 "IPv6",
29
30 "ACCEPT",
31 "REJECT",
32 "DROP",
33 "NOTRACK",
34 "DNAT",
35 "SNAT",
36 };
37
38 bool
39 fw3_parse_bool(void *ptr, const char *val)
40 {
41 if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
42 *((bool *)ptr) = true;
43 else
44 *((bool *)ptr) = false;
45
46 return true;
47 }
48
49 bool
50 fw3_parse_int(void *ptr, const char *val)
51 {
52 int n = strtol(val, NULL, 10);
53
54 if (errno == ERANGE || errno == EINVAL)
55 return false;
56
57 *((int *)ptr) = n;
58
59 return true;
60 }
61
62 bool
63 fw3_parse_string(void *ptr, const char *val)
64 {
65 *((char **)ptr) = (char *)val;
66 return true;
67 }
68
69 bool
70 fw3_parse_target(void *ptr, const char *val)
71 {
72 if (!strcmp(val, "ACCEPT"))
73 {
74 *((enum fw3_target *)ptr) = FW3_TARGET_ACCEPT;
75 return true;
76 }
77 else if (!strcmp(val, "REJECT"))
78 {
79 *((enum fw3_target *)ptr) = FW3_TARGET_REJECT;
80 return true;
81 }
82 else if (!strcmp(val, "DROP"))
83 {
84 *((enum fw3_target *)ptr) = FW3_TARGET_DROP;
85 return true;
86 }
87 else if (!strcmp(val, "NOTRACK"))
88 {
89 *((enum fw3_target *)ptr) = FW3_TARGET_NOTRACK;
90 return true;
91 }
92 else if (!strcmp(val, "DNAT"))
93 {
94 *((enum fw3_target *)ptr) = FW3_TARGET_DNAT;
95 return true;
96 }
97 else if (!strcmp(val, "SNAT"))
98 {
99 *((enum fw3_target *)ptr) = FW3_TARGET_SNAT;
100 return true;
101 }
102
103 return false;
104 }
105
106 bool
107 fw3_parse_limit(void *ptr, const char *val)
108 {
109 struct fw3_limit *limit = ptr;
110 enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
111 char *e;
112 int n;
113
114 if (*val == '!')
115 {
116 limit->invert = true;
117 while (isspace(*++val));
118 }
119
120 n = strtol(val, &e, 10);
121
122 if (errno == ERANGE || errno == EINVAL)
123 return false;
124
125 if (*e && *e++ != '/')
126 return false;
127
128 if (!strlen(e))
129 return false;
130
131 if (!strncmp(e, "second", strlen(e)))
132 u = FW3_LIMIT_UNIT_SECOND;
133 else if (!strncmp(e, "minute", strlen(e)))
134 u = FW3_LIMIT_UNIT_MINUTE;
135 else if (!strncmp(e, "hour", strlen(e)))
136 u = FW3_LIMIT_UNIT_HOUR;
137 else if (!strncmp(e, "day", strlen(e)))
138 u = FW3_LIMIT_UNIT_DAY;
139 else
140 return false;
141
142 limit->rate = n;
143 limit->unit = u;
144
145 return true;
146 }
147
148 bool
149 fw3_parse_device(void *ptr, const char *val)
150 {
151 struct fw3_device *dev = ptr;
152
153 if (*val == '*')
154 {
155 dev->set = true;
156 dev->any = true;
157 return true;
158 }
159
160 if (*val == '!')
161 {
162 dev->invert = true;
163 while (isspace(*++val));
164 }
165
166 if (*val)
167 snprintf(dev->name, sizeof(dev->name), "%s", val);
168 else
169 return false;
170
171 dev->set = true;
172 return true;
173 }
174
175 bool
176 fw3_parse_address(void *ptr, const char *val)
177 {
178 struct fw3_address *addr = ptr;
179 struct in_addr v4;
180 struct in6_addr v6;
181 char *p, *s, *e;
182 int i, m = -1;
183
184 if (*val == '!')
185 {
186 addr->invert = true;
187 while (isspace(*++val));
188 }
189
190 s = strdup(val);
191
192 if (!s)
193 return false;
194
195 if ((p = strchr(s, '/')) != NULL)
196 {
197 *p++ = 0;
198 m = strtoul(p, &e, 10);
199
200 if ((e == p) || (*e != 0))
201 {
202 if (strchr(s, ':') || !inet_pton(AF_INET, p, &v4))
203 {
204 free(s);
205 return false;
206 }
207
208 for (i = 0, m = 32; !(v4.s_addr & 1) && (i < 32); i++)
209 {
210 m--;
211 v4.s_addr >>= 1;
212 }
213 }
214 }
215
216 if (inet_pton(AF_INET6, s, &v6))
217 {
218 addr->family = FW3_FAMILY_V6;
219 addr->address.v6 = v6;
220 addr->mask = (m >= 0) ? m : 128;
221 }
222 else if (inet_pton(AF_INET, s, &v4))
223 {
224 addr->family = FW3_FAMILY_V4;
225 addr->address.v4 = v4;
226 addr->mask = (m >= 0) ? m : 32;
227 }
228 else
229 {
230 free(s);
231 return false;
232 }
233
234 free(s);
235 addr->set = true;
236 return true;
237 }
238
239 bool
240 fw3_parse_mac(void *ptr, const char *val)
241 {
242 struct fw3_mac *addr = ptr;
243 struct ether_addr *mac;
244
245 if (*val == '!')
246 {
247 addr->invert = true;
248 while (isspace(*++val));
249 }
250
251 if ((mac = ether_aton(val)) != NULL)
252 {
253 addr->mac = *mac;
254 addr->set = true;
255 return true;
256 }
257
258 return false;
259 }
260
261 bool
262 fw3_parse_port(void *ptr, const char *val)
263 {
264 struct fw3_port *range = ptr;
265 uint16_t n;
266 uint16_t m;
267 char *p;
268
269 if (*val == '!')
270 {
271 range->invert = true;
272 while (isspace(*++val));
273 }
274
275 n = strtoul(val, &p, 10);
276
277 if (errno == ERANGE || errno == EINVAL)
278 return false;
279
280 if (*p && *p != '-' && *p != ':')
281 return false;
282
283 if (*p)
284 {
285 m = strtoul(++p, NULL, 10);
286
287 if (errno == ERANGE || errno == EINVAL || m < n)
288 return false;
289
290 range->port_min = n;
291 range->port_max = m;
292 }
293 else
294 {
295 range->port_min = n;
296 range->port_max = n;
297 }
298
299 range->set = true;
300 return true;
301 }
302
303 bool
304 fw3_parse_family(void *ptr, const char *val)
305 {
306 if (!strcmp(val, "any"))
307 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
308 else if (!strcmp(val, "inet") || strrchr(val, '4'))
309 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
310 else if (!strcmp(val, "inet6") || strrchr(val, '6'))
311 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
312 else
313 return false;
314
315 return true;
316 }
317
318 bool
319 fw3_parse_icmptype(void *ptr, const char *val)
320 {
321 struct fw3_icmptype *icmp = ptr;
322 bool v4 = false;
323 bool v6 = false;
324 char *p;
325 int i;
326
327 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
328 {
329 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
330 {
331 icmp->type = fw3_icmptype_list_v4[i].type;
332 icmp->code_min = fw3_icmptype_list_v4[i].code_min;
333 icmp->code_max = fw3_icmptype_list_v4[i].code_max;
334
335 v4 = true;
336 break;
337 }
338 }
339
340 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
341 {
342 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
343 {
344 icmp->type6 = fw3_icmptype_list_v6[i].type;
345 icmp->code6_min = fw3_icmptype_list_v6[i].code_min;
346 icmp->code6_max = fw3_icmptype_list_v6[i].code_max;
347
348 v6 = true;
349 break;
350 }
351 }
352
353 if (!v4 && !v6)
354 {
355 i = strtoul(val, &p, 10);
356
357 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
358 return false;
359
360 icmp->type = i;
361
362 if (*p == '/')
363 {
364 val = ++p;
365 i = strtoul(val, &p, 10);
366
367 if ((p == val) || (*p != 0) || (i > 0xFF))
368 return false;
369
370 icmp->code_min = i;
371 icmp->code_max = i;
372 }
373 else
374 {
375 icmp->code_min = 0;
376 icmp->code_max = 0xFF;
377 }
378
379 icmp->type6 = icmp->type;
380 icmp->code6_min = icmp->code_max;
381 icmp->code6_max = icmp->code_max;
382
383 v4 = true;
384 v6 = true;
385 }
386
387 icmp->family = (v4 && v6) ? FW3_FAMILY_ANY
388 : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
389
390 return true;
391 }
392
393 bool
394 fw3_parse_protocol(void *ptr, const char *val)
395 {
396 struct fw3_protocol *proto = ptr;
397 struct protoent *ent;
398
399 if (*val == '!')
400 {
401 proto->invert = true;
402 while (isspace(*++val));
403 }
404
405 if (!strcmp(val, "all"))
406 {
407 proto->any = true;
408 return true;
409 }
410 else if (!strcmp(val, "icmpv6"))
411 {
412 val = "ipv6-icmp";
413 }
414
415 ent = getprotobyname(val);
416
417 if (ent)
418 {
419 proto->protocol = ent->p_proto;
420 return true;
421 }
422
423 proto->protocol = strtoul(val, NULL, 10);
424 return (errno != ERANGE && errno != EINVAL);
425 }
426
427 bool
428 fw3_parse_ipset_method(void *ptr, const char *val)
429 {
430 if (!strncmp(val, "bitmap", strlen(val)))
431 {
432 *((enum fw3_ipset_method *)ptr) = FW3_IPSET_METHOD_BITMAP;
433 return true;
434 }
435 else if (!strncmp(val, "hash", strlen(val)))
436 {
437 *((enum fw3_ipset_method *)ptr) = FW3_IPSET_METHOD_HASH;
438 return true;
439 }
440 else if (!strncmp(val, "list", strlen(val)))
441 {
442 *((enum fw3_ipset_method *)ptr) = FW3_IPSET_METHOD_LIST;
443 return true;
444 }
445
446 return false;
447 }
448
449 bool
450 fw3_parse_ipset_datatype(void *ptr, const char *val)
451 {
452 struct fw3_ipset_datatype *type = ptr;
453
454 if (!strncmp(val, "dest_", 5))
455 {
456 val += 5;
457 type->dest = true;
458 }
459 else if (!strncmp(val, "dst_", 4))
460 {
461 val += 4;
462 type->dest = true;
463 }
464 else if (!strncmp(val, "src_", 4))
465 {
466 val += 4;
467 type->dest = false;
468 }
469
470 if (!strncmp(val, "ip", strlen(val)))
471 {
472 type->type = FW3_IPSET_TYPE_IP;
473 return true;
474 }
475 else if (!strncmp(val, "port", strlen(val)))
476 {
477 type->type = FW3_IPSET_TYPE_PORT;
478 return true;
479 }
480 else if (!strncmp(val, "mac", strlen(val)))
481 {
482 type->type = FW3_IPSET_TYPE_MAC;
483 return true;
484 }
485 else if (!strncmp(val, "net", strlen(val)))
486 {
487 type->type = FW3_IPSET_TYPE_NET;
488 return true;
489 }
490 else if (!strncmp(val, "set", strlen(val)))
491 {
492 type->type = FW3_IPSET_TYPE_SET;
493 return true;
494 }
495
496 return false;
497 }
498
499
500 void
501 fw3_parse_options(void *s, const struct fw3_option *opts,
502 struct uci_section *section)
503 {
504 char *p;
505 bool known;
506 struct uci_element *e, *l;
507 struct uci_option *o;
508 const struct fw3_option *opt;
509 struct list_head *item;
510 struct list_head *dest;
511
512 uci_foreach_element(&section->options, e)
513 {
514 o = uci_to_option(e);
515 known = false;
516
517 for (opt = opts; opt->name; opt++)
518 {
519 if (!opt->parse)
520 continue;
521
522 if (strcmp(opt->name, e->name))
523 continue;
524
525 if (o->type == UCI_TYPE_LIST)
526 {
527 if (!opt->elem_size)
528 {
529 warn_elem(e, "must not be a list");
530 }
531 else
532 {
533 uci_foreach_element(&o->v.list, l)
534 {
535 if (!l->name)
536 continue;
537
538 item = malloc(opt->elem_size);
539
540 if (!item)
541 continue;
542
543 memset(item, 0, opt->elem_size);
544
545 if (!opt->parse(item, l->name))
546 {
547 warn_elem(e, "has invalid value '%s'", l->name);
548 free(item);
549 continue;
550 }
551
552 dest = (struct list_head *)((char *)s + opt->offset);
553 list_add_tail(item, dest);
554 }
555 }
556 }
557 else
558 {
559 if (!o->v.string)
560 continue;
561
562 if (!opt->elem_size)
563 {
564 if (!opt->parse((char *)s + opt->offset, o->v.string))
565 warn_elem(e, "has invalid value '%s'", o->v.string);
566 }
567 else
568 {
569 for (p = strtok(o->v.string, " \t");
570 p != NULL;
571 p = strtok(NULL, " \t"))
572 {
573 item = malloc(opt->elem_size);
574
575 if (!item)
576 continue;
577
578 memset(item, 0, opt->elem_size);
579
580 if (!opt->parse(item, p))
581 {
582 warn_elem(e, "has invalid value '%s'", p);
583 free(item);
584 continue;
585 }
586
587 dest = (struct list_head *)((char *)s + opt->offset);
588 list_add_tail(item, dest);
589 }
590 }
591 }
592
593 known = true;
594 break;
595 }
596
597 if (!known)
598 warn_elem(e, "is unknown");
599 }
600 }
601
602
603 void
604 fw3_format_in_out(struct fw3_device *in, struct fw3_device *out)
605 {
606 if (in && !in->any)
607 fw3_pr(" %s-i %s", in->invert ? "! " : "", in->name);
608
609 if (out && !out->any)
610 fw3_pr(" %s-o %s", out->invert ? "! " : "", out->name);
611 }
612
613 void
614 fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
615 {
616 char s[INET6_ADDRSTRLEN];
617
618 if (src && src->set)
619 {
620 inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
621 &src->address.v4, s, sizeof(s));
622
623 fw3_pr(" %s-s %s/%u", src->invert ? "! " : "", s, src->mask);
624 }
625
626 if (dest && dest->set)
627 {
628 inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
629 &dest->address.v4, s, sizeof(s));
630
631 fw3_pr(" %s-d %s/%u", dest->invert ? "! " : "", s, dest->mask);
632 }
633 }
634
635 void
636 fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp)
637 {
638 if (sp && sp->set)
639 {
640 if (sp->port_min == sp->port_max)
641 fw3_pr(" %s--sport %u", sp->invert ? "! " : "", sp->port_min);
642 else
643 fw3_pr(" %s--sport %u:%u",
644 sp->invert ? "! " : "", sp->port_min, sp->port_max);
645 }
646
647 if (dp && dp->set)
648 {
649 if (dp->port_min == dp->port_max)
650 fw3_pr(" %s--dport %u", dp->invert ? "! " : "", dp->port_min);
651 else
652 fw3_pr(" %s--dport %u:%u",
653 dp->invert ? "! " : "", dp->port_min, dp->port_max);
654 }
655 }
656
657 void
658 fw3_format_mac(struct fw3_mac *mac)
659 {
660 if (!mac)
661 return;
662
663 fw3_pr(" -m mac %s--mac-source %s",
664 mac->invert ? "! " : "", ether_ntoa(&mac->mac));
665 }
666
667 void
668 fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family)
669 {
670 uint16_t pr;
671
672 if (!proto)
673 return;
674
675 pr = proto->protocol;
676
677 if (pr == 1 && family == FW3_FAMILY_V6)
678 pr = 58;
679
680 if (proto->any)
681 fw3_pr(" -p all");
682 else
683 fw3_pr(" %s-p %u", proto->invert ? "! " : "", pr);
684 }
685
686 void
687 fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family)
688 {
689 if (!icmp)
690 return;
691
692 if (family != FW3_FAMILY_V6)
693 {
694 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
695 fw3_pr(" %s--icmp-type %u", icmp->invert ? "! " : "", icmp->type);
696 else
697 fw3_pr(" %s--icmp-type %u/%u",
698 icmp->invert ? "! " : "", icmp->type, icmp->code_min);
699 }
700 else
701 {
702 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
703 fw3_pr(" %s--icmpv6-type %u", icmp->invert ? "! " : "", icmp->type6);
704 else
705 fw3_pr(" %s--icmpv6-type %u/%u",
706 icmp->invert ? "! " : "", icmp->type6, icmp->code6_min);
707 }
708 }
709
710 void
711 fw3_format_limit(struct fw3_limit *limit)
712 {
713 if (!limit)
714 return;
715
716 const char *units[] = {
717 [FW3_LIMIT_UNIT_SECOND] = "second",
718 [FW3_LIMIT_UNIT_MINUTE] = "minute",
719 [FW3_LIMIT_UNIT_HOUR] = "hour",
720 [FW3_LIMIT_UNIT_DAY] = "day",
721 };
722
723 if (limit->rate > 0)
724 {
725 fw3_pr(" -m limit %s--limit %u/%s",
726 limit->invert ? "! " : "", limit->rate, units[limit->unit]);
727
728 if (limit->burst > 0)
729 fw3_pr(" --limit-burst %u", limit->burst);
730 }
731 }
732
733 void
734 fw3_format_ipset(struct fw3_ipset *ipset, bool invert)
735 {
736 bool first = true;
737 const char *name = NULL;
738 struct fw3_ipset_datatype *type;
739
740 if (!ipset)
741 return;
742
743 if (ipset->external && *ipset->external)
744 name = ipset->external;
745 else
746 name = ipset->name;
747
748 fw3_pr(" -m set %s--match-set %s", invert ? "! " : "", name);
749
750 list_for_each_entry(type, &ipset->datatypes, list)
751 {
752 fw3_pr("%c%s", first ? ' ' : ',', type->dest ? "dst" : "src");
753 first = false;
754 }
755 }
756
757 void
758 __fw3_format_comment(const char *comment, ...)
759 {
760 va_list ap;
761 int len = 0;
762 const char *c;
763
764 if (!comment || !*comment)
765 return;
766
767 fw3_pr(" -m comment --comment \"");
768
769 c = comment;
770
771 va_start(ap, comment);
772
773 do
774 {
775 while (*c)
776 {
777 switch (*c)
778 {
779 case '"':
780 case '$':
781 case '`':
782 case '\\':
783 fw3_pr("\\");
784 /* fall through */
785
786 default:
787 fw3_pr("%c", *c);
788 break;
789 }
790
791 c++;
792
793 if (len++ >= 255)
794 goto end;
795 }
796
797 c = va_arg(ap, const char *);
798 }
799 while (c);
800
801 end:
802 va_end(ap);
803 fw3_pr("\"");
804 }
805
806 void
807 fw3_format_extra(const char *extra)
808 {
809 if (!extra || !*extra)
810 return;
811
812 fw3_pr(" %s", extra);
813 }