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 = { };
253 while (isspace(*++val));
261 if ((p = strchr(s, '/')) != NULL)
264 m = strtoul(p, &e, 10);
266 if ((e == p) || (*e != 0))
268 if (strchr(s, ':') || !inet_pton(AF_INET, p, &v4))
274 for (i = 0, m = 32; !(v4.s_addr & 1) && (i < 32); i++)
281 else if ((p = strchr(s, '-')) != NULL)
285 if (inet_pton(AF_INET6, p, &v6))
287 addr.family = FW3_FAMILY_V6;
288 addr.address2.v6 = v6;
291 else if (inet_pton(AF_INET, p, &v4))
293 addr.family = FW3_FAMILY_V4;
294 addr.address2.v4 = v4;
304 if (inet_pton(AF_INET6, s, &v6))
306 addr.family = FW3_FAMILY_V6;
307 addr.address.v6 = v6;
308 addr.mask = (m >= 0) ? m : 128;
310 else if (inet_pton(AF_INET, s, &v4))
312 addr.family = FW3_FAMILY_V4;
313 addr.address.v4 = v4;
314 addr.mask = (m >= 0) ? m : 32;
324 put_value(ptr, &addr, sizeof(addr), is_list);
329 fw3_parse_network(void *ptr, const char *val, bool is_list)
331 struct fw3_device dev = { };
332 struct fw3_address *addr, *tmp;
333 LIST_HEAD(addr_list);
335 if (!fw3_parse_address(ptr, val, is_list))
337 if (!fw3_parse_device(&dev, val, false))
340 fw3_ubus_address(&addr_list, dev.name);
341 list_for_each_entry(addr, &addr_list, list)
343 addr->invert = dev.invert;
344 addr->resolved = true;
349 list_splice_tail(&addr_list, ptr);
351 else if (!list_empty(&addr_list))
353 memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
356 list_for_each_entry_safe(addr, tmp, &addr_list, list)
365 fw3_parse_mac(void *ptr, const char *val, bool is_list)
367 struct fw3_mac addr = { };
368 struct ether_addr *mac;
373 while (isspace(*++val));
376 if ((mac = ether_aton(val)) != NULL)
381 put_value(ptr, &addr, sizeof(addr), is_list);
389 fw3_parse_port(void *ptr, const char *val, bool is_list)
391 struct fw3_port range = { };
399 while (isspace(*++val));
402 n = strtoul(val, &p, 10);
404 if (errno == ERANGE || errno == EINVAL)
407 if (*p && *p != '-' && *p != ':')
412 m = strtoul(++p, NULL, 10);
414 if (errno == ERANGE || errno == EINVAL || m < n)
427 put_value(ptr, &range, sizeof(range), is_list);
432 fw3_parse_family(void *ptr, const char *val, bool is_list)
434 if (!strcmp(val, "any"))
435 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
436 else if (!strcmp(val, "inet") || strrchr(val, '4'))
437 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
438 else if (!strcmp(val, "inet6") || strrchr(val, '6'))
439 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
447 fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
449 struct fw3_icmptype icmp = { };
455 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
457 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
459 icmp.type = fw3_icmptype_list_v4[i].type;
460 icmp.code_min = fw3_icmptype_list_v4[i].code_min;
461 icmp.code_max = fw3_icmptype_list_v4[i].code_max;
468 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
470 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
472 icmp.type6 = fw3_icmptype_list_v6[i].type;
473 icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
474 icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
483 i = strtoul(val, &p, 10);
485 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
493 i = strtoul(val, &p, 10);
495 if ((p == val) || (*p != 0) || (i > 0xFF))
504 icmp.code_max = 0xFF;
507 icmp.type6 = icmp.type;
508 icmp.code6_min = icmp.code_max;
509 icmp.code6_max = icmp.code_max;
515 icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
516 : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
518 put_value(ptr, &icmp, sizeof(icmp), is_list);
523 fw3_parse_protocol(void *ptr, const char *val, bool is_list)
525 struct fw3_protocol proto = { };
526 struct protoent *ent;
532 while (isspace(*++val));
535 if (!strcmp(val, "all"))
538 put_value(ptr, &proto, sizeof(proto), is_list);
541 else if (!strcmp(val, "icmpv6"))
545 else if (!strcmp(val, "tcpudp"))
548 if (put_value(ptr, &proto, sizeof(proto), is_list))
551 put_value(ptr, &proto, sizeof(proto), is_list);
557 ent = getprotobyname(val);
561 proto.protocol = ent->p_proto;
562 put_value(ptr, &proto, sizeof(proto), is_list);
566 proto.protocol = strtoul(val, &e, 10);
568 if ((e == val) || (*e != 0))
571 put_value(ptr, &proto, sizeof(proto), is_list);
576 fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
578 return parse_enum(ptr, val, &fw3_ipset_method_names[FW3_IPSET_METHOD_BITMAP],
579 FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
583 fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
585 struct fw3_ipset_datatype type = { };
589 if (!strncmp(val, "dest_", 5))
594 else if (!strncmp(val, "dst_", 4))
599 else if (!strncmp(val, "src_", 4))
605 if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
606 FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET))
608 put_value(ptr, &type, sizeof(type), is_list);
616 fw3_parse_date(void *ptr, const char *val, bool is_list)
618 unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
619 struct tm tm = { 0 };
622 year = strtoul(val, &p, 10);
623 if ((*p != '-' && *p) || year < 1970 || year > 2038)
628 mon = strtoul(++p, &p, 10);
629 if ((*p != '-' && *p) || mon > 12)
634 day = strtoul(++p, &p, 10);
635 if ((*p != 'T' && *p) || day > 31)
640 hour = strtoul(++p, &p, 10);
641 if ((*p != ':' && *p) || hour > 23)
646 min = strtoul(++p, &p, 10);
647 if ((*p != ':' && *p) || min > 59)
652 sec = strtoul(++p, &p, 10);
657 tm.tm_year = year - 1900;
664 if (mktime(&tm) >= 0)
666 *((struct tm *)ptr) = tm;
675 fw3_parse_time(void *ptr, const char *val, bool is_list)
677 unsigned int hour = 0, min = 0, sec = 0;
680 hour = strtoul(val, &p, 10);
681 if (*p != ':' || hour > 23)
684 min = strtoul(++p, &p, 10);
685 if ((*p != ':' && *p) || min > 59)
690 sec = strtoul(++p, &p, 10);
695 *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
703 fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
710 setbit(*(uint8_t *)ptr, 0);
711 while (isspace(*++val));
714 if (!(s = strdup(val)))
717 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
719 if (!parse_enum(&w, p, weekdays, 1, 7))
721 w = strtoul(p, &p, 10);
723 if (*p || w < 1 || w > 7)
730 setbit(*(uint8_t *)ptr, w);
738 fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
745 setbit(*(uint32_t *)ptr, 0);
746 while (isspace(*++val));
749 if (!(s = strdup(val)))
752 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
754 d = strtoul(p, &p, 10);
756 if (*p || d < 1 || d > 31)
762 setbit(*(uint32_t *)ptr, d);
770 fw3_parse_include_type(void *ptr, const char *val, bool is_list)
772 return parse_enum(ptr, val, include_types,
773 FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
777 fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
779 return parse_enum(ptr, val, reflection_sources,
780 FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
784 fw3_parse_mark(void *ptr, const char *val, bool is_list)
788 struct fw3_mark *m = ptr;
793 while (isspace(*++val));
796 if ((s = strchr(val, '/')) != NULL)
799 n = strtoul(val, &e, 0);
805 m->mask = 0xFFFFFFFF;
809 n = strtoul(s, &e, 0);
822 fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
824 struct fw3_setmatch *m = ptr;
831 while (isspace(*++val));
834 if (!(s = strdup(val)))
837 if (!(p = strtok(s, " \t")))
843 strncpy(m->name, p, sizeof(m->name));
845 for (i = 0, p = strtok(NULL, " \t,");
847 i++, p = strtok(NULL, " \t,"))
849 if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
851 else if (!strncmp(p, "src", 3))
862 fw3_parse_direction(void *ptr, const char *val, bool is_list)
867 if (!strcmp(val, "in") || !strcmp(val, "ingress"))
869 else if (!strcmp(val, "out") || !strcmp(val, "egress"))
879 fw3_parse_options(void *s, const struct fw3_option *opts,
880 struct uci_section *section)
884 struct uci_element *e, *l;
885 struct uci_option *o;
886 const struct fw3_option *opt;
887 struct list_head *dest;
890 uci_foreach_element(§ion->options, e)
892 o = uci_to_option(e);
895 for (opt = opts; opt->name; opt++)
900 if (strcmp(opt->name, e->name))
903 if (o->type == UCI_TYPE_LIST)
907 warn_elem(e, "must not be a list");
912 dest = (struct list_head *)((char *)s + opt->offset);
914 uci_foreach_element(&o->v.list, l)
919 if (!opt->parse(dest, l->name, true))
921 warn_elem(e, "has invalid value '%s'", l->name);
937 if (!opt->parse((char *)s + opt->offset, o->v.string, false))
939 warn_elem(e, "has invalid value '%s'", o->v.string);
945 dest = (struct list_head *)((char *)s + opt->offset);
947 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
949 if (!opt->parse(dest, p, true))
951 warn_elem(e, "has invalid value '%s'", p);
964 warn_elem(e, "is unknown");
972 fw3_parse_blob_options(void *s, const struct fw3_option *opts,
975 char *p, *v, buf[16];
978 struct blob_attr *o, *e;
979 const struct fw3_option *opt;
980 struct list_head *dest;
983 blobmsg_for_each_attr(o, a, rem)
987 for (opt = opts; opt->name; opt++)
992 if (strcmp(opt->name, blobmsg_name(o)))
995 if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
999 fprintf(stderr, "%s must not be a list\n", opt->name);
1004 dest = (struct list_head *)((char *)s + opt->offset);
1006 blobmsg_for_each_attr(e, o, erem)
1008 if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
1009 snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
1012 v = blobmsg_get_string(e);
1015 if (!opt->parse(dest, v, true))
1017 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1026 if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
1027 snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
1030 v = blobmsg_get_string(o);
1036 if (!opt->elem_size)
1038 if (!opt->parse((char *)s + opt->offset, v, false))
1040 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1046 dest = (struct list_head *)((char *)s + opt->offset);
1048 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
1050 if (!opt->parse(dest, p, true))
1052 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, p);
1065 fprintf(stderr, "%s is unknown\n", blobmsg_name(o));
1073 fw3_address_to_string(struct fw3_address *address, bool allow_invert)
1075 char *p, ip[INET6_ADDRSTRLEN];
1076 static char buf[INET6_ADDRSTRLEN * 2 + 2];
1080 if (address->invert && allow_invert)
1081 p += sprintf(p, "!");
1083 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1084 &address->address.v4, ip, sizeof(ip));
1086 p += sprintf(p, "%s", ip);
1090 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1091 &address->address2.v4, ip, sizeof(ip));
1093 p += sprintf(p, "-%s", ip);
1097 p += sprintf(p, "/%u", address->mask);