2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013-2014 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.
24 put_value(void *ptr, void *val, int elem_size, bool is_list)
30 copy = malloc(elem_size);
35 memcpy(copy, val, elem_size);
36 list_add_tail((struct list_head *)copy, (struct list_head *)ptr);
40 memcpy(ptr, val, elem_size);
45 parse_enum(void *ptr, const char *val, const char **values, int min, int max)
47 int i, l = strlen(val);
51 for (i = 0; i <= (max - min); i++)
53 if (!strncasecmp(val, values[i], l))
55 *((int *)ptr) = min + i;
65 const char *fw3_flag_names[__FW3_FLAG_MAX] = {
88 const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX] = {
95 const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX] = {
102 const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX] = {
111 static const char *weekdays[] = {
121 static const char *include_types[] = {
126 static const char *reflection_sources[] = {
133 fw3_parse_bool(void *ptr, const char *val, bool is_list)
135 if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
136 *((bool *)ptr) = true;
138 *((bool *)ptr) = false;
144 fw3_parse_int(void *ptr, const char *val, bool is_list)
147 int n = strtol(val, &e, 0);
158 fw3_parse_string(void *ptr, const char *val, bool is_list)
160 *((char **)ptr) = (char *)val;
165 fw3_parse_target(void *ptr, const char *val, bool is_list)
167 return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
168 FW3_FLAG_ACCEPT, FW3_FLAG_MASQUERADE);
172 fw3_parse_limit(void *ptr, const char *val, bool is_list)
174 struct fw3_limit *limit = ptr;
175 enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
181 limit->invert = true;
182 while (isspace(*++val));
185 n = strtol(val, &e, 10);
187 if (errno == ERANGE || errno == EINVAL)
190 if (*e && *e++ != '/')
196 if (!parse_enum(&u, e, fw3_limit_units, 0, FW3_LIMIT_UNIT_DAY))
206 fw3_parse_device(void *ptr, const char *val, bool is_list)
209 struct fw3_device dev = { };
215 put_value(ptr, &dev, sizeof(dev), is_list);
222 while (isspace(*++val));
225 if ((p = strchr(val, '@')) != NULL)
228 snprintf(dev.network, sizeof(dev.network), "%s", p);
232 snprintf(dev.name, sizeof(dev.name), "%s", val);
237 put_value(ptr, &dev, sizeof(dev), is_list);
242 fw3_parse_address(void *ptr, const char *val, bool is_list)
244 struct fw3_address addr = { };
247 char *p = NULL, *m = NULL, *s, *e;
253 while (isspace(*++val));
261 if ((m = strchr(s, '/')) != NULL)
263 else if ((p = strchr(s, '-')) != NULL)
266 if (inet_pton(AF_INET6, s, &v6))
268 addr.family = FW3_FAMILY_V6;
269 addr.address.v6 = v6;
273 if (!inet_pton(AF_INET6, m, &v6))
275 bits = strtol(m, &e, 10);
277 if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v6))
285 if (!inet_pton(AF_INET6, p, &addr.mask.v6))
292 memset(addr.mask.v6.s6_addr, 0xFF, 16);
295 else if (inet_pton(AF_INET, s, &v4))
297 addr.family = FW3_FAMILY_V4;
298 addr.address.v4 = v4;
302 if (!inet_pton(AF_INET, m, &v4))
304 bits = strtol(m, &e, 10);
306 if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v4))
314 if (!inet_pton(AF_INET, p, &addr.mask.v4))
321 addr.mask.v4.s_addr = 0xFFFFFFFF;
331 put_value(ptr, &addr, sizeof(addr), is_list);
340 fw3_parse_network(void *ptr, const char *val, bool is_list)
342 struct fw3_device dev = { };
343 struct fw3_address *addr, *tmp;
344 LIST_HEAD(addr_list);
346 if (!fw3_parse_address(ptr, val, is_list))
348 if (!fw3_parse_device(&dev, val, false))
351 fw3_ubus_address(&addr_list, dev.name);
352 list_for_each_entry(addr, &addr_list, list)
354 addr->invert = dev.invert;
355 addr->resolved = true;
360 list_splice_tail(&addr_list, ptr);
362 else if (!list_empty(&addr_list))
364 memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
367 list_for_each_entry_safe(addr, tmp, &addr_list, list)
376 fw3_parse_mac(void *ptr, const char *val, bool is_list)
378 struct fw3_mac addr = { };
379 struct ether_addr *mac;
384 while (isspace(*++val));
387 if ((mac = ether_aton(val)) != NULL)
392 put_value(ptr, &addr, sizeof(addr), is_list);
400 fw3_parse_port(void *ptr, const char *val, bool is_list)
402 struct fw3_port range = { };
410 while (isspace(*++val));
413 n = strtoul(val, &p, 10);
415 if (errno == ERANGE || errno == EINVAL)
418 if (*p && *p != '-' && *p != ':')
423 m = strtoul(++p, NULL, 10);
425 if (errno == ERANGE || errno == EINVAL || m < n)
438 put_value(ptr, &range, sizeof(range), is_list);
443 fw3_parse_family(void *ptr, const char *val, bool is_list)
445 if (!strcmp(val, "any") || !strcmp(val, "*"))
446 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
447 else if (!strcmp(val, "inet") || strrchr(val, '4'))
448 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
449 else if (!strcmp(val, "inet6") || strrchr(val, '6'))
450 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
458 fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
460 struct fw3_icmptype icmp = { };
466 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
468 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
470 icmp.type = fw3_icmptype_list_v4[i].type;
471 icmp.code_min = fw3_icmptype_list_v4[i].code_min;
472 icmp.code_max = fw3_icmptype_list_v4[i].code_max;
479 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
481 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
483 icmp.type6 = fw3_icmptype_list_v6[i].type;
484 icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
485 icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
494 i = strtoul(val, &p, 10);
496 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
504 i = strtoul(val, &p, 10);
506 if ((p == val) || (*p != 0) || (i > 0xFF))
515 icmp.code_max = 0xFF;
518 icmp.type6 = icmp.type;
519 icmp.code6_min = icmp.code_max;
520 icmp.code6_max = icmp.code_max;
526 icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
527 : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
529 put_value(ptr, &icmp, sizeof(icmp), is_list);
534 fw3_parse_protocol(void *ptr, const char *val, bool is_list)
536 struct fw3_protocol proto = { };
537 struct protoent *ent;
543 while (isspace(*++val));
546 if (!strcmp(val, "all") || !strcmp(val, "any") || !strcmp(val, "*"))
549 put_value(ptr, &proto, sizeof(proto), is_list);
552 else if (!strcmp(val, "icmpv6"))
556 else if (!strcmp(val, "tcpudp"))
559 if (put_value(ptr, &proto, sizeof(proto), is_list))
562 put_value(ptr, &proto, sizeof(proto), is_list);
568 ent = getprotobyname(val);
572 proto.protocol = ent->p_proto;
573 put_value(ptr, &proto, sizeof(proto), is_list);
577 proto.protocol = strtoul(val, &e, 10);
579 if ((e == val) || (*e != 0))
582 put_value(ptr, &proto, sizeof(proto), is_list);
587 fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
589 return parse_enum(ptr, val, &fw3_ipset_method_names[FW3_IPSET_METHOD_BITMAP],
590 FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
594 fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
596 struct fw3_ipset_datatype type = { };
600 if (!strncmp(val, "dest_", 5))
605 else if (!strncmp(val, "dst_", 4))
610 else if (!strncmp(val, "src_", 4))
616 if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
617 FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET))
619 put_value(ptr, &type, sizeof(type), is_list);
627 fw3_parse_date(void *ptr, const char *val, bool is_list)
629 unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
630 struct tm tm = { 0 };
633 year = strtoul(val, &p, 10);
634 if ((*p != '-' && *p) || year < 1970 || year > 2038)
639 mon = strtoul(++p, &p, 10);
640 if ((*p != '-' && *p) || mon > 12)
645 day = strtoul(++p, &p, 10);
646 if ((*p != 'T' && *p) || day > 31)
651 hour = strtoul(++p, &p, 10);
652 if ((*p != ':' && *p) || hour > 23)
657 min = strtoul(++p, &p, 10);
658 if ((*p != ':' && *p) || min > 59)
663 sec = strtoul(++p, &p, 10);
668 tm.tm_year = year - 1900;
675 if (mktime(&tm) >= 0)
677 *((struct tm *)ptr) = tm;
686 fw3_parse_time(void *ptr, const char *val, bool is_list)
688 unsigned int hour = 0, min = 0, sec = 0;
691 hour = strtoul(val, &p, 10);
692 if (*p != ':' || hour > 23)
695 min = strtoul(++p, &p, 10);
696 if ((*p != ':' && *p) || min > 59)
701 sec = strtoul(++p, &p, 10);
706 *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
714 fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
721 setbit(*(uint8_t *)ptr, 0);
722 while (isspace(*++val));
725 if (!(s = strdup(val)))
728 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
730 if (!parse_enum(&w, p, weekdays, 1, 7))
732 w = strtoul(p, &p, 10);
734 if (*p || w < 1 || w > 7)
741 setbit(*(uint8_t *)ptr, w);
749 fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
756 setbit(*(uint32_t *)ptr, 0);
757 while (isspace(*++val));
760 if (!(s = strdup(val)))
763 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
765 d = strtoul(p, &p, 10);
767 if (*p || d < 1 || d > 31)
773 setbit(*(uint32_t *)ptr, d);
781 fw3_parse_include_type(void *ptr, const char *val, bool is_list)
783 return parse_enum(ptr, val, include_types,
784 FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
788 fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
790 return parse_enum(ptr, val, reflection_sources,
791 FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
795 fw3_parse_mark(void *ptr, const char *val, bool is_list)
799 struct fw3_mark *m = ptr;
804 while (isspace(*++val));
807 if ((s = strchr(val, '/')) != NULL)
810 n = strtoul(val, &e, 0);
816 m->mask = 0xFFFFFFFF;
820 n = strtoul(s, &e, 0);
833 fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
835 struct fw3_setmatch *m = ptr;
842 while (isspace(*++val));
845 if (!(s = strdup(val)))
848 if (!(p = strtok(s, " \t")))
854 strncpy(m->name, p, sizeof(m->name));
856 for (i = 0, p = strtok(NULL, " \t,");
858 i++, p = strtok(NULL, " \t,"))
860 if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
862 else if (!strncmp(p, "src", 3))
873 fw3_parse_direction(void *ptr, const char *val, bool is_list)
878 if (!strcmp(val, "in") || !strcmp(val, "ingress"))
880 else if (!strcmp(val, "out") || !strcmp(val, "egress"))
890 fw3_parse_options(void *s, const struct fw3_option *opts,
891 struct uci_section *section)
895 struct uci_element *e, *l;
896 struct uci_option *o;
897 const struct fw3_option *opt;
898 struct list_head *dest;
901 uci_foreach_element(§ion->options, e)
903 o = uci_to_option(e);
906 for (opt = opts; opt->name; opt++)
911 if (strcmp(opt->name, e->name))
914 if (o->type == UCI_TYPE_LIST)
918 warn_elem(e, "must not be a list");
923 dest = (struct list_head *)((char *)s + opt->offset);
925 uci_foreach_element(&o->v.list, l)
930 if (!opt->parse(dest, l->name, true))
932 warn_elem(e, "has invalid value '%s'", l->name);
948 if (!opt->parse((char *)s + opt->offset, o->v.string, false))
950 warn_elem(e, "has invalid value '%s'", o->v.string);
956 dest = (struct list_head *)((char *)s + opt->offset);
958 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
960 if (!opt->parse(dest, p, true))
962 warn_elem(e, "has invalid value '%s'", p);
975 warn_elem(e, "is unknown");
983 fw3_parse_blob_options(void *s, const struct fw3_option *opts,
986 char *p, *v, buf[16];
989 struct blob_attr *o, *e;
990 const struct fw3_option *opt;
991 struct list_head *dest;
994 blobmsg_for_each_attr(o, a, rem)
998 for (opt = opts; opt->name; opt++)
1003 if (strcmp(opt->name, blobmsg_name(o)))
1006 if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
1008 if (!opt->elem_size)
1010 fprintf(stderr, "%s must not be a list\n", opt->name);
1015 dest = (struct list_head *)((char *)s + opt->offset);
1017 blobmsg_for_each_attr(e, o, erem)
1019 if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
1020 snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
1023 v = blobmsg_get_string(e);
1026 if (!opt->parse(dest, v, true))
1028 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1037 if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
1038 snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
1041 v = blobmsg_get_string(o);
1047 if (!opt->elem_size)
1049 if (!opt->parse((char *)s + opt->offset, v, false))
1051 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1057 dest = (struct list_head *)((char *)s + opt->offset);
1059 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
1061 if (!opt->parse(dest, p, true))
1063 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, p);
1076 fprintf(stderr, "%s is unknown\n", blobmsg_name(o));
1084 fw3_address_to_string(struct fw3_address *address, bool allow_invert, bool as_cidr)
1086 char *p, ip[INET6_ADDRSTRLEN];
1087 static char buf[INET6_ADDRSTRLEN * 2 + 2];
1091 if (address->invert && allow_invert)
1092 p += sprintf(p, "!");
1094 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1095 &address->address.v4, ip, sizeof(ip));
1097 p += sprintf(p, "%s", ip);
1101 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1102 &address->mask.v4, ip, sizeof(ip));
1104 p += sprintf(p, "-%s", ip);
1108 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1109 &address->mask.v4, ip, sizeof(ip));
1111 p += sprintf(p, "/%s", ip);
1115 p += sprintf(p, "/%u", fw3_netmask2bitlen(address->family,
1116 &address->mask.v6));