2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
22 fw3_parse_bool(void *ptr, const char *val)
24 if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
25 *((bool *)ptr) = true;
27 *((bool *)ptr) = false;
33 fw3_parse_int(void *ptr, const char *val)
35 int n = strtol(val, NULL, 10);
37 if (errno == ERANGE || errno == EINVAL)
46 fw3_parse_string(void *ptr, const char *val)
48 *((char **)ptr) = (char *)val;
53 fw3_parse_target(void *ptr, const char *val)
55 if (!strcmp(val, "ACCEPT"))
57 *((enum fw3_target *)ptr) = FW3_TARGET_ACCEPT;
60 else if (!strcmp(val, "REJECT"))
62 *((enum fw3_target *)ptr) = FW3_TARGET_REJECT;
65 else if (!strcmp(val, "DROP"))
67 *((enum fw3_target *)ptr) = FW3_TARGET_DROP;
70 else if (!strcmp(val, "NOTRACK"))
72 *((enum fw3_target *)ptr) = FW3_TARGET_NOTRACK;
75 else if (!strcmp(val, "DNAT"))
77 *((enum fw3_target *)ptr) = FW3_TARGET_DNAT;
80 else if (!strcmp(val, "SNAT"))
82 *((enum fw3_target *)ptr) = FW3_TARGET_SNAT;
90 fw3_parse_limit(void *ptr, const char *val)
92 struct fw3_limit *limit = ptr;
93 enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
100 while (isspace(*++val));
103 n = strtol(val, &e, 10);
105 if (errno == ERANGE || errno == EINVAL)
108 if (*e && *e++ != '/')
114 if (!strncmp(e, "second", strlen(e)))
115 u = FW3_LIMIT_UNIT_SECOND;
116 else if (!strncmp(e, "minute", strlen(e)))
117 u = FW3_LIMIT_UNIT_MINUTE;
118 else if (!strncmp(e, "hour", strlen(e)))
119 u = FW3_LIMIT_UNIT_HOUR;
120 else if (!strncmp(e, "day", strlen(e)))
121 u = FW3_LIMIT_UNIT_DAY;
132 fw3_parse_device(void *ptr, const char *val)
134 struct fw3_device *dev = ptr;
146 while (isspace(*++val));
150 snprintf(dev->name, sizeof(dev->name), "%s", val);
159 fw3_parse_address(void *ptr, const char *val)
161 struct fw3_address *addr = ptr;
170 while (isspace(*++val));
178 if ((p = strchr(s, '/')) != NULL)
181 m = strtoul(p, &e, 10);
183 if ((e == p) || (*e != 0))
185 if (strchr(s, ':') || !inet_pton(AF_INET, p, &v4))
191 for (i = 0, m = 32; !(v4.s_addr & 1) && (i < 32); i++)
199 if (inet_pton(AF_INET6, s, &v6))
201 addr->family = FW3_FAMILY_V6;
202 addr->address.v6 = v6;
203 addr->mask = (m >= 0) ? m : 128;
205 else if (inet_pton(AF_INET, s, &v4))
207 addr->family = FW3_FAMILY_V4;
208 addr->address.v4 = v4;
209 addr->mask = (m >= 0) ? m : 32;
223 fw3_parse_mac(void *ptr, const char *val)
225 struct fw3_mac *addr = ptr;
226 struct ether_addr *mac;
231 while (isspace(*++val));
234 if ((mac = ether_aton(val)) != NULL)
245 fw3_parse_port(void *ptr, const char *val)
247 struct fw3_port *range = ptr;
254 range->invert = true;
255 while (isspace(*++val));
258 n = strtoul(val, &p, 10);
260 if (errno == ERANGE || errno == EINVAL)
263 if (*p && *p != '-' && *p != ':')
268 m = strtoul(++p, NULL, 10);
270 if (errno == ERANGE || errno == EINVAL || m < n)
287 fw3_parse_family(void *ptr, const char *val)
289 if (!strcmp(val, "any"))
290 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
291 else if (!strcmp(val, "inet") || strrchr(val, '4'))
292 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
293 else if (!strcmp(val, "inet6") || strrchr(val, '6'))
294 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
302 fw3_parse_icmptype(void *ptr, const char *val)
304 struct fw3_icmptype *icmp = ptr;
310 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
312 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
314 icmp->type = fw3_icmptype_list_v4[i].type;
315 icmp->code_min = fw3_icmptype_list_v4[i].code_min;
316 icmp->code_max = fw3_icmptype_list_v4[i].code_max;
323 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
325 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
327 icmp->type6 = fw3_icmptype_list_v6[i].type;
328 icmp->code6_min = fw3_icmptype_list_v6[i].code_min;
329 icmp->code6_max = fw3_icmptype_list_v6[i].code_max;
338 i = strtoul(val, &p, 10);
340 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
348 i = strtoul(val, &p, 10);
350 if ((p == val) || (*p != 0) || (i > 0xFF))
359 icmp->code_max = 0xFF;
362 icmp->type6 = icmp->type;
363 icmp->code6_min = icmp->code_max;
364 icmp->code6_max = icmp->code_max;
370 icmp->family = (v4 && v6) ? FW3_FAMILY_ANY
371 : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
377 fw3_parse_protocol(void *ptr, const char *val)
379 struct fw3_protocol *proto = ptr;
380 struct protoent *ent;
384 proto->invert = true;
385 while (isspace(*++val));
388 if (!strcmp(val, "all"))
393 else if (!strcmp(val, "icmpv6"))
398 ent = getprotobyname(val);
402 proto->protocol = ent->p_proto;
406 proto->protocol = strtoul(val, NULL, 10);
407 return (errno != ERANGE && errno != EINVAL);
411 fw3_parse_ipset_method(void *ptr, const char *val)
413 if (!strncmp(val, "bitmap", strlen(val)))
415 *((enum fw3_ipset_method *)ptr) = FW3_IPSET_METHOD_BITMAP;
418 else if (!strncmp(val, "hash", strlen(val)))
420 *((enum fw3_ipset_method *)ptr) = FW3_IPSET_METHOD_HASH;
423 else if (!strncmp(val, "list", strlen(val)))
425 *((enum fw3_ipset_method *)ptr) = FW3_IPSET_METHOD_LIST;
433 fw3_parse_ipset_datatype(void *ptr, const char *val)
435 struct fw3_ipset_datatype *type = ptr;
437 if (!strncmp(val, "dest_", 5))
442 else if (!strncmp(val, "dst_", 4))
447 else if (!strncmp(val, "src_", 4))
453 if (!strncmp(val, "ip", strlen(val)))
455 type->type = FW3_IPSET_TYPE_IP;
458 else if (!strncmp(val, "port", strlen(val)))
460 type->type = FW3_IPSET_TYPE_PORT;
463 else if (!strncmp(val, "mac", strlen(val)))
465 type->type = FW3_IPSET_TYPE_MAC;
468 else if (!strncmp(val, "net", strlen(val)))
470 type->type = FW3_IPSET_TYPE_NET;
473 else if (!strncmp(val, "set", strlen(val)))
475 type->type = FW3_IPSET_TYPE_SET;
484 fw3_parse_options(void *s,
485 struct fw3_option *opts, int n,
486 struct uci_section *section)
491 struct uci_element *e, *l;
492 struct uci_option *o;
493 struct fw3_option *opt;
494 struct list_head *item;
495 struct list_head *dest;
497 uci_foreach_element(§ion->options, e)
499 o = uci_to_option(e);
502 for (i = 0; i < n; i++)
506 if (!opt->parse || !opt->name)
509 if (strcmp(opt->name, e->name))
512 if (o->type == UCI_TYPE_LIST)
516 warn_elem(e, "must not be a list");
520 uci_foreach_element(&o->v.list, l)
525 item = malloc(opt->elem_size);
530 memset(item, 0, opt->elem_size);
532 if (!opt->parse(item, l->name))
534 warn_elem(e, "has invalid value '%s'", l->name);
539 dest = (struct list_head *)((char *)s + opt->offset);
540 list_add_tail(item, dest);
551 if (!opt->parse((char *)s + opt->offset, o->v.string))
552 warn_elem(e, "has invalid value '%s'", o->v.string);
556 for (p = strtok(o->v.string, " \t");
558 p = strtok(NULL, " \t"))
560 item = malloc(opt->elem_size);
565 memset(item, 0, opt->elem_size);
567 if (!opt->parse(item, p))
569 warn_elem(e, "has invalid value '%s'", p);
574 dest = (struct list_head *)((char *)s + opt->offset);
575 list_add_tail(item, dest);
585 warn_elem(e, "is unknown");
591 fw3_format_in_out(struct fw3_device *in, struct fw3_device *out)
594 fw3_pr(" %s-i %s", in->invert ? "! " : "", in->name);
596 if (out && !out->any)
597 fw3_pr(" %s-o %s", out->invert ? "! " : "", out->name);
601 fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
603 char s[INET6_ADDRSTRLEN];
607 inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
608 &src->address.v4, s, sizeof(s));
610 fw3_pr(" %s-s %s/%u", src->invert ? "! " : "", s, src->mask);
613 if (dest && dest->set)
615 inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
616 &dest->address.v4, s, sizeof(s));
618 fw3_pr(" %s-d %s/%u", dest->invert ? "! " : "", s, dest->mask);
623 fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp)
627 if (sp->port_min == sp->port_max)
628 fw3_pr(" %s--sport %u", sp->invert ? "! " : "", sp->port_min);
630 fw3_pr(" %s--sport %u:%u",
631 sp->invert ? "! " : "", sp->port_min, sp->port_max);
636 if (dp->port_min == dp->port_max)
637 fw3_pr(" %s--dport %u", dp->invert ? "! " : "", dp->port_min);
639 fw3_pr(" %s--dport %u:%u",
640 dp->invert ? "! " : "", dp->port_min, dp->port_max);
645 fw3_format_mac(struct fw3_mac *mac)
650 fw3_pr(" -m mac %s--mac-source %s",
651 mac->invert ? "! " : "", ether_ntoa(&mac->mac));
655 fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family)
662 pr = proto->protocol;
664 if (pr == 1 && family == FW3_FAMILY_V6)
670 fw3_pr(" %s-p %u", proto->invert ? "! " : "", pr);
674 fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family)
679 if (family != FW3_FAMILY_V6)
681 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
682 fw3_pr(" %s--icmp-type %u", icmp->invert ? "! " : "", icmp->type);
684 fw3_pr(" %s--icmp-type %u/%u",
685 icmp->invert ? "! " : "", icmp->type, icmp->code_min);
689 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
690 fw3_pr(" %s--icmpv6-type %u", icmp->invert ? "! " : "", icmp->type6);
692 fw3_pr(" %s--icmpv6-type %u/%u",
693 icmp->invert ? "! " : "", icmp->type6, icmp->code6_min);
698 fw3_format_limit(struct fw3_limit *limit)
703 const char *units[] = {
704 [FW3_LIMIT_UNIT_SECOND] = "second",
705 [FW3_LIMIT_UNIT_MINUTE] = "minute",
706 [FW3_LIMIT_UNIT_HOUR] = "hour",
707 [FW3_LIMIT_UNIT_DAY] = "day",
712 fw3_pr(" -m limit %s--limit %u/%s",
713 limit->invert ? "! " : "", limit->rate, units[limit->unit]);
715 if (limit->burst > 0)
716 fw3_pr(" --limit-burst %u", limit->burst);
721 fw3_format_ipset(struct fw3_ipset *ipset, bool invert)
724 const char *name = NULL;
725 struct fw3_ipset_datatype *type;
730 if (ipset->external && *ipset->external)
731 name = ipset->external;
735 fw3_pr(" -m set %s--match-set %s", invert ? "! " : "", name);
737 list_for_each_entry(type, &ipset->datatypes, list)
739 fw3_pr("%c%s", first ? ' ' : ',', type->dest ? "dst" : "src");
745 __fw3_format_comment(const char *comment, ...)
751 if (!comment || !*comment)
754 fw3_pr(" -m comment --comment \"");
758 va_start(ap, comment);
784 c = va_arg(ap, const char *);
794 fw3_format_extra(const char *extra)
796 if (!extra || !*extra)
799 fw3_pr(" %s", extra);