2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
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] = {
89 const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX] = {
96 const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX] = {
103 const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX] = {
112 static const char *weekdays[] = {
122 static const char *include_types[] = {
127 static const char *reflection_sources[] = {
134 fw3_parse_bool(void *ptr, const char *val, bool is_list)
136 if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
137 *((bool *)ptr) = true;
139 *((bool *)ptr) = false;
145 fw3_parse_int(void *ptr, const char *val, bool is_list)
148 int n = strtol(val, &e, 0);
159 fw3_parse_string(void *ptr, const char *val, bool is_list)
161 *((char **)ptr) = (char *)val;
166 fw3_parse_target(void *ptr, const char *val, bool is_list)
168 return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
169 FW3_FLAG_ACCEPT, FW3_FLAG_MASQUERADE);
173 fw3_parse_limit(void *ptr, const char *val, bool is_list)
175 struct fw3_limit *limit = ptr;
176 enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
182 limit->invert = true;
183 while (isspace(*++val));
186 n = strtol(val, &e, 10);
188 if (errno == ERANGE || errno == EINVAL)
191 if (*e && *e++ != '/')
197 if (!parse_enum(&u, e, fw3_limit_units, 0, FW3_LIMIT_UNIT_DAY))
207 fw3_parse_device(void *ptr, const char *val, bool is_list)
210 struct fw3_device dev = { };
216 put_value(ptr, &dev, sizeof(dev), is_list);
223 while (isspace(*++val));
226 if ((p = strchr(val, '@')) != NULL)
229 snprintf(dev.network, sizeof(dev.network), "%s", p);
233 snprintf(dev.name, sizeof(dev.name), "%s", val);
238 put_value(ptr, &dev, sizeof(dev), is_list);
243 fw3_parse_address(void *ptr, const char *val, bool is_list)
245 struct fw3_address addr = { };
248 char *p = NULL, *m = NULL, *s, *e;
254 while (isspace(*++val));
262 if ((m = strchr(s, '/')) != NULL)
264 else if ((p = strchr(s, '-')) != NULL)
267 if (inet_pton(AF_INET6, s, &v6))
269 addr.family = FW3_FAMILY_V6;
270 addr.address.v6 = v6;
274 if (!inet_pton(AF_INET6, m, &v6))
276 bits = strtol(m, &e, 10);
278 if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v6))
286 if (!inet_pton(AF_INET6, p, &addr.mask.v6))
293 memset(addr.mask.v6.s6_addr, 0xFF, 16);
296 else if (inet_pton(AF_INET, s, &v4))
298 addr.family = FW3_FAMILY_V4;
299 addr.address.v4 = v4;
303 if (!inet_pton(AF_INET, m, &v4))
305 bits = strtol(m, &e, 10);
307 if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v4))
315 if (!inet_pton(AF_INET, p, &addr.mask.v4))
322 addr.mask.v4.s_addr = 0xFFFFFFFF;
332 put_value(ptr, &addr, sizeof(addr), is_list);
341 fw3_parse_network(void *ptr, const char *val, bool is_list)
343 struct fw3_device dev = { };
344 struct fw3_address *addr, *tmp;
345 LIST_HEAD(addr_list);
348 if (!fw3_parse_address(ptr, val, is_list))
350 if (!fw3_parse_device(&dev, val, false))
353 n_addrs = fw3_ubus_address(&addr_list, dev.name);
355 list_for_each_entry(addr, &addr_list, list)
357 addr->invert = dev.invert;
358 addr->resolved = true;
361 /* add an empty address member with .set = false, .resolved = true
362 * to signal resolving failure to callers */
365 tmp = fw3_alloc(sizeof(*tmp));
366 tmp->resolved = true;
368 list_add_tail(&tmp->list, &addr_list);
373 list_splice_tail(&addr_list, ptr);
375 else if (!list_empty(&addr_list))
377 memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
380 list_for_each_entry_safe(addr, tmp, &addr_list, list)
389 fw3_parse_mac(void *ptr, const char *val, bool is_list)
391 struct fw3_mac addr = { };
392 struct ether_addr *mac;
397 while (isspace(*++val));
400 if ((mac = ether_aton(val)) != NULL)
405 put_value(ptr, &addr, sizeof(addr), is_list);
413 fw3_parse_port(void *ptr, const char *val, bool is_list)
415 struct fw3_port range = { };
423 while (isspace(*++val));
426 n = strtoul(val, &p, 10);
428 if (errno == ERANGE || errno == EINVAL)
431 if (*p && *p != '-' && *p != ':')
436 m = strtoul(++p, NULL, 10);
438 if (errno == ERANGE || errno == EINVAL || m < n)
451 put_value(ptr, &range, sizeof(range), is_list);
456 fw3_parse_family(void *ptr, const char *val, bool is_list)
458 if (!strcmp(val, "any") || !strcmp(val, "*"))
459 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
460 else if (!strcmp(val, "inet") || strrchr(val, '4'))
461 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
462 else if (!strcmp(val, "inet6") || strrchr(val, '6'))
463 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
471 fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
473 struct fw3_icmptype icmp = { };
479 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
481 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
483 icmp.type = fw3_icmptype_list_v4[i].type;
484 icmp.code_min = fw3_icmptype_list_v4[i].code_min;
485 icmp.code_max = fw3_icmptype_list_v4[i].code_max;
492 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
494 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
496 icmp.type6 = fw3_icmptype_list_v6[i].type;
497 icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
498 icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
507 i = strtoul(val, &p, 10);
509 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
517 i = strtoul(val, &p, 10);
519 if ((p == val) || (*p != 0) || (i > 0xFF))
528 icmp.code_max = 0xFF;
531 icmp.type6 = icmp.type;
532 icmp.code6_min = icmp.code_max;
533 icmp.code6_max = icmp.code_max;
539 icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
540 : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
542 put_value(ptr, &icmp, sizeof(icmp), is_list);
547 fw3_parse_protocol(void *ptr, const char *val, bool is_list)
549 struct fw3_protocol proto = { };
550 struct protoent *ent;
556 while (isspace(*++val));
559 if (!strcmp(val, "all") || !strcmp(val, "any") || !strcmp(val, "*"))
562 put_value(ptr, &proto, sizeof(proto), is_list);
565 else if (!strcmp(val, "icmpv6"))
569 else if (!strcmp(val, "tcpudp"))
572 if (put_value(ptr, &proto, sizeof(proto), is_list))
575 put_value(ptr, &proto, sizeof(proto), is_list);
581 ent = getprotobyname(val);
585 proto.protocol = ent->p_proto;
586 put_value(ptr, &proto, sizeof(proto), is_list);
590 proto.protocol = strtoul(val, &e, 10);
592 if ((e == val) || (*e != 0))
595 put_value(ptr, &proto, sizeof(proto), is_list);
600 fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
602 return parse_enum(ptr, val, &fw3_ipset_method_names[FW3_IPSET_METHOD_BITMAP],
603 FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
607 fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
609 struct fw3_ipset_datatype type = { };
613 if (!strncmp(val, "dest_", 5))
618 else if (!strncmp(val, "dst_", 4))
623 else if (!strncmp(val, "src_", 4))
629 if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
630 FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET))
632 put_value(ptr, &type, sizeof(type), is_list);
640 fw3_parse_date(void *ptr, const char *val, bool is_list)
642 unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
643 struct tm tm = { 0 };
646 year = strtoul(val, &p, 10);
647 if ((*p != '-' && *p) || year < 1970 || year > 2038)
652 mon = strtoul(++p, &p, 10);
653 if ((*p != '-' && *p) || mon > 12)
658 day = strtoul(++p, &p, 10);
659 if ((*p != 'T' && *p) || day > 31)
664 hour = strtoul(++p, &p, 10);
665 if ((*p != ':' && *p) || hour > 23)
670 min = strtoul(++p, &p, 10);
671 if ((*p != ':' && *p) || min > 59)
676 sec = strtoul(++p, &p, 10);
681 tm.tm_year = year - 1900;
688 if (mktime(&tm) >= 0)
690 *((struct tm *)ptr) = tm;
699 fw3_parse_time(void *ptr, const char *val, bool is_list)
701 unsigned int hour = 0, min = 0, sec = 0;
704 hour = strtoul(val, &p, 10);
705 if (*p != ':' || hour > 23)
708 min = strtoul(++p, &p, 10);
709 if ((*p != ':' && *p) || min > 59)
714 sec = strtoul(++p, &p, 10);
719 *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
727 fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
734 fw3_setbit(*(uint8_t *)ptr, 0);
735 while (isspace(*++val));
738 if (!(s = strdup(val)))
741 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
743 if (!parse_enum(&w, p, weekdays, 1, 7))
745 w = strtoul(p, &p, 10);
747 if (*p || w < 1 || w > 7)
754 fw3_setbit(*(uint8_t *)ptr, w);
762 fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
769 fw3_setbit(*(uint32_t *)ptr, 0);
770 while (isspace(*++val));
773 if (!(s = strdup(val)))
776 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
778 d = strtoul(p, &p, 10);
780 if (*p || d < 1 || d > 31)
786 fw3_setbit(*(uint32_t *)ptr, d);
794 fw3_parse_include_type(void *ptr, const char *val, bool is_list)
796 return parse_enum(ptr, val, include_types,
797 FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
801 fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
803 return parse_enum(ptr, val, reflection_sources,
804 FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
808 fw3_parse_mark(void *ptr, const char *val, bool is_list)
812 struct fw3_mark *m = ptr;
817 while (isspace(*++val));
820 if ((s = strchr(val, '/')) != NULL)
823 n = strtoul(val, &e, 0);
829 m->mask = 0xFFFFFFFF;
833 n = strtoul(s, &e, 0);
846 fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
848 struct fw3_setmatch *m = ptr;
855 while (isspace(*++val));
858 if (!(s = strdup(val)))
861 if (!(p = strtok(s, " \t")))
867 strncpy(m->name, p, sizeof(m->name));
869 for (i = 0, p = strtok(NULL, " \t,");
871 i++, p = strtok(NULL, " \t,"))
873 if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
875 else if (!strncmp(p, "src", 3))
886 fw3_parse_direction(void *ptr, const char *val, bool is_list)
891 if (!strcmp(val, "in") || !strcmp(val, "ingress"))
893 else if (!strcmp(val, "out") || !strcmp(val, "egress"))
902 fw3_parse_cthelper(void *ptr, const char *val, bool is_list)
904 struct fw3_cthelpermatch m = { };
909 while (isspace(*++val));
915 strncpy(m.name, val, sizeof(m.name) - 1);
916 put_value(ptr, &m, sizeof(m), is_list);
924 fw3_parse_setentry(void *ptr, const char *val, bool is_list)
926 struct fw3_setentry e = { };
929 put_value(ptr, &e, sizeof(e), is_list);
936 fw3_parse_options(void *s, const struct fw3_option *opts,
937 struct uci_section *section)
941 struct uci_element *e, *l;
942 struct uci_option *o;
943 const struct fw3_option *opt;
944 struct list_head *dest;
947 uci_foreach_element(§ion->options, e)
949 o = uci_to_option(e);
952 for (opt = opts; opt->name; opt++)
957 if (strcmp(opt->name, e->name))
960 if (o->type == UCI_TYPE_LIST)
964 warn_elem(e, "must not be a list");
969 dest = (struct list_head *)((char *)s + opt->offset);
971 uci_foreach_element(&o->v.list, l)
976 if (!opt->parse(dest, l->name, true))
978 warn_elem(e, "has invalid value '%s'", l->name);
994 if (!opt->parse((char *)s + opt->offset, o->v.string, false))
996 warn_elem(e, "has invalid value '%s'", o->v.string);
1003 dest = (struct list_head *)((char *)s + opt->offset);
1005 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
1007 /* If we encounter a sole "!" token, assume that it
1008 * is meant to be part of the next token, so silently
1009 * skip it and remember the state... */
1010 if (!strcmp(p, "!"))
1016 /* The previous token was a sole "!", rewind pointer
1017 * back by one byte to precede the value with an
1018 * exclamation mark which effectively turns
1019 * ("!", "foo") into ("!foo") */
1026 if (!opt->parse(dest, p, true))
1028 warn_elem(e, "has invalid value '%s'", p);
1034 /* The last token was a sole "!" without any subsequent
1035 * text, so pass it to the option parser as-is. */
1036 if (inv && !opt->parse(dest, "!", true))
1038 warn_elem(e, "has invalid value '%s'", p);
1049 warn_elem(e, "is unknown");
1057 fw3_parse_blob_options(void *s, const struct fw3_option *opts,
1058 struct blob_attr *a, const char *name)
1060 char *p, *v, buf[16];
1063 struct blob_attr *o, *e;
1064 const struct fw3_option *opt;
1065 struct list_head *dest;
1068 blobmsg_for_each_attr(o, a, rem)
1072 for (opt = opts; opt->name; opt++)
1077 if (strcmp(opt->name, blobmsg_name(o)))
1080 if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
1082 if (!opt->elem_size)
1084 fprintf(stderr, "%s: '%s' must not be a list\n",
1091 dest = (struct list_head *)((char *)s + opt->offset);
1093 blobmsg_for_each_attr(e, o, erem)
1095 if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
1096 snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
1099 v = blobmsg_get_string(e);
1102 if (!opt->parse(dest, v, true))
1104 fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
1105 name, opt->name, v);
1114 if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
1115 snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
1118 v = blobmsg_get_string(o);
1124 if (!opt->elem_size)
1126 if (!opt->parse((char *)s + opt->offset, v, false))
1128 fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
1129 name, opt->name, v);
1135 dest = (struct list_head *)((char *)s + opt->offset);
1137 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
1139 if (!opt->parse(dest, p, true))
1141 fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
1142 name, opt->name, p);
1154 if (!known && strcmp(blobmsg_name(o), "type"))
1155 fprintf(stderr, "%s: '%s' is unknown\n", name, blobmsg_name(o));
1163 fw3_address_to_string(struct fw3_address *address, bool allow_invert, bool as_cidr)
1165 char *p, ip[INET6_ADDRSTRLEN];
1166 static char buf[INET6_ADDRSTRLEN * 2 + 2];
1170 if (address->invert && allow_invert)
1171 p += sprintf(p, "!");
1173 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1174 &address->address.v4, ip, sizeof(ip));
1176 p += sprintf(p, "%s", ip);
1180 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1181 &address->mask.v4, ip, sizeof(ip));
1183 p += sprintf(p, "-%s", ip);
1187 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1188 &address->mask.v4, ip, sizeof(ip));
1190 p += sprintf(p, "/%s", ip);
1194 p += sprintf(p, "/%u", fw3_netmask2bitlen(address->family,
1195 &address->mask.v6));