introduce global string array for enum names, remove private arrays
[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,
502 struct fw3_option *opts, int n,
503 struct uci_section *section)
504 {
505 int i;
506 char *p;
507 bool known;
508 struct uci_element *e, *l;
509 struct uci_option *o;
510 struct fw3_option *opt;
511 struct list_head *item;
512 struct list_head *dest;
513
514 uci_foreach_element(&section->options, e)
515 {
516 o = uci_to_option(e);
517 known = false;
518
519 for (i = 0; i < n; i++)
520 {
521 opt = &opts[i];
522
523 if (!opt->parse || !opt->name)
524 continue;
525
526 if (strcmp(opt->name, e->name))
527 continue;
528
529 if (o->type == UCI_TYPE_LIST)
530 {
531 if (!opt->elem_size)
532 {
533 warn_elem(e, "must not be a list");
534 }
535 else
536 {
537 uci_foreach_element(&o->v.list, l)
538 {
539 if (!l->name)
540 continue;
541
542 item = malloc(opt->elem_size);
543
544 if (!item)
545 continue;
546
547 memset(item, 0, opt->elem_size);
548
549 if (!opt->parse(item, l->name))
550 {
551 warn_elem(e, "has invalid value '%s'", l->name);
552 free(item);
553 continue;
554 }
555
556 dest = (struct list_head *)((char *)s + opt->offset);
557 list_add_tail(item, dest);
558 }
559 }
560 }
561 else
562 {
563 if (!o->v.string)
564 continue;
565
566 if (!opt->elem_size)
567 {
568 if (!opt->parse((char *)s + opt->offset, o->v.string))
569 warn_elem(e, "has invalid value '%s'", o->v.string);
570 }
571 else
572 {
573 for (p = strtok(o->v.string, " \t");
574 p != NULL;
575 p = strtok(NULL, " \t"))
576 {
577 item = malloc(opt->elem_size);
578
579 if (!item)
580 continue;
581
582 memset(item, 0, opt->elem_size);
583
584 if (!opt->parse(item, p))
585 {
586 warn_elem(e, "has invalid value '%s'", p);
587 free(item);
588 continue;
589 }
590
591 dest = (struct list_head *)((char *)s + opt->offset);
592 list_add_tail(item, dest);
593 }
594 }
595 }
596
597 known = true;
598 break;
599 }
600
601 if (!known)
602 warn_elem(e, "is unknown");
603 }
604 }
605
606
607 void
608 fw3_format_in_out(struct fw3_device *in, struct fw3_device *out)
609 {
610 if (in && !in->any)
611 fw3_pr(" %s-i %s", in->invert ? "! " : "", in->name);
612
613 if (out && !out->any)
614 fw3_pr(" %s-o %s", out->invert ? "! " : "", out->name);
615 }
616
617 void
618 fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
619 {
620 char s[INET6_ADDRSTRLEN];
621
622 if (src && src->set)
623 {
624 inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
625 &src->address.v4, s, sizeof(s));
626
627 fw3_pr(" %s-s %s/%u", src->invert ? "! " : "", s, src->mask);
628 }
629
630 if (dest && dest->set)
631 {
632 inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
633 &dest->address.v4, s, sizeof(s));
634
635 fw3_pr(" %s-d %s/%u", dest->invert ? "! " : "", s, dest->mask);
636 }
637 }
638
639 void
640 fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp)
641 {
642 if (sp && sp->set)
643 {
644 if (sp->port_min == sp->port_max)
645 fw3_pr(" %s--sport %u", sp->invert ? "! " : "", sp->port_min);
646 else
647 fw3_pr(" %s--sport %u:%u",
648 sp->invert ? "! " : "", sp->port_min, sp->port_max);
649 }
650
651 if (dp && dp->set)
652 {
653 if (dp->port_min == dp->port_max)
654 fw3_pr(" %s--dport %u", dp->invert ? "! " : "", dp->port_min);
655 else
656 fw3_pr(" %s--dport %u:%u",
657 dp->invert ? "! " : "", dp->port_min, dp->port_max);
658 }
659 }
660
661 void
662 fw3_format_mac(struct fw3_mac *mac)
663 {
664 if (!mac)
665 return;
666
667 fw3_pr(" -m mac %s--mac-source %s",
668 mac->invert ? "! " : "", ether_ntoa(&mac->mac));
669 }
670
671 void
672 fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family)
673 {
674 uint16_t pr;
675
676 if (!proto)
677 return;
678
679 pr = proto->protocol;
680
681 if (pr == 1 && family == FW3_FAMILY_V6)
682 pr = 58;
683
684 if (proto->any)
685 fw3_pr(" -p all");
686 else
687 fw3_pr(" %s-p %u", proto->invert ? "! " : "", pr);
688 }
689
690 void
691 fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family)
692 {
693 if (!icmp)
694 return;
695
696 if (family != FW3_FAMILY_V6)
697 {
698 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
699 fw3_pr(" %s--icmp-type %u", icmp->invert ? "! " : "", icmp->type);
700 else
701 fw3_pr(" %s--icmp-type %u/%u",
702 icmp->invert ? "! " : "", icmp->type, icmp->code_min);
703 }
704 else
705 {
706 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
707 fw3_pr(" %s--icmpv6-type %u", icmp->invert ? "! " : "", icmp->type6);
708 else
709 fw3_pr(" %s--icmpv6-type %u/%u",
710 icmp->invert ? "! " : "", icmp->type6, icmp->code6_min);
711 }
712 }
713
714 void
715 fw3_format_limit(struct fw3_limit *limit)
716 {
717 if (!limit)
718 return;
719
720 const char *units[] = {
721 [FW3_LIMIT_UNIT_SECOND] = "second",
722 [FW3_LIMIT_UNIT_MINUTE] = "minute",
723 [FW3_LIMIT_UNIT_HOUR] = "hour",
724 [FW3_LIMIT_UNIT_DAY] = "day",
725 };
726
727 if (limit->rate > 0)
728 {
729 fw3_pr(" -m limit %s--limit %u/%s",
730 limit->invert ? "! " : "", limit->rate, units[limit->unit]);
731
732 if (limit->burst > 0)
733 fw3_pr(" --limit-burst %u", limit->burst);
734 }
735 }
736
737 void
738 fw3_format_ipset(struct fw3_ipset *ipset, bool invert)
739 {
740 bool first = true;
741 const char *name = NULL;
742 struct fw3_ipset_datatype *type;
743
744 if (!ipset)
745 return;
746
747 if (ipset->external && *ipset->external)
748 name = ipset->external;
749 else
750 name = ipset->name;
751
752 fw3_pr(" -m set %s--match-set %s", invert ? "! " : "", name);
753
754 list_for_each_entry(type, &ipset->datatypes, list)
755 {
756 fw3_pr("%c%s", first ? ' ' : ',', type->dest ? "dst" : "src");
757 first = false;
758 }
759 }
760
761 void
762 __fw3_format_comment(const char *comment, ...)
763 {
764 va_list ap;
765 int len = 0;
766 const char *c;
767
768 if (!comment || !*comment)
769 return;
770
771 fw3_pr(" -m comment --comment \"");
772
773 c = comment;
774
775 va_start(ap, comment);
776
777 do
778 {
779 while (*c)
780 {
781 switch (*c)
782 {
783 case '"':
784 case '$':
785 case '`':
786 case '\\':
787 fw3_pr("\\");
788 /* fall through */
789
790 default:
791 fw3_pr("%c", *c);
792 break;
793 }
794
795 c++;
796
797 if (len++ >= 255)
798 goto end;
799 }
800
801 c = va_arg(ap, const char *);
802 }
803 while (c);
804
805 end:
806 va_end(ap);
807 fw3_pr("\"");
808 }
809
810 void
811 fw3_format_extra(const char *extra)
812 {
813 if (!extra || !*extra)
814 return;
815
816 fw3_pr(" %s", extra);
817 }