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.
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] = {
87 static const char *limit_units[] = {
94 static const char *ipset_methods[] = {
100 static const char *ipset_types[] = {
108 static const char *weekdays[] = {
118 static const char *include_types[] = {
123 static const char *reflection_sources[] = {
130 fw3_parse_bool(void *ptr, const char *val, bool is_list)
132 if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
133 *((bool *)ptr) = true;
135 *((bool *)ptr) = false;
141 fw3_parse_int(void *ptr, const char *val, bool is_list)
143 int n = strtol(val, NULL, 10);
145 if (errno == ERANGE || errno == EINVAL)
154 fw3_parse_string(void *ptr, const char *val, bool is_list)
156 *((char **)ptr) = (char *)val;
161 fw3_parse_target(void *ptr, const char *val, bool is_list)
163 return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
164 FW3_FLAG_ACCEPT, FW3_FLAG_SNAT);
168 fw3_parse_limit(void *ptr, const char *val, bool is_list)
170 struct fw3_limit *limit = ptr;
171 enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
177 limit->invert = true;
178 while (isspace(*++val));
181 n = strtol(val, &e, 10);
183 if (errno == ERANGE || errno == EINVAL)
186 if (*e && *e++ != '/')
192 if (!parse_enum(&u, e, limit_units, 0, FW3_LIMIT_UNIT_DAY))
202 fw3_parse_device(void *ptr, const char *val, bool is_list)
204 struct fw3_device dev = { };
210 put_value(ptr, &dev, sizeof(dev), is_list);
217 while (isspace(*++val));
221 snprintf(dev.name, sizeof(dev.name), "%s", val);
226 put_value(ptr, &dev, sizeof(dev), is_list);
231 fw3_parse_address(void *ptr, const char *val, bool is_list)
233 struct fw3_address addr = { };
242 while (isspace(*++val));
250 if ((p = strchr(s, '/')) != NULL)
253 m = strtoul(p, &e, 10);
255 if ((e == p) || (*e != 0))
257 if (strchr(s, ':') || !inet_pton(AF_INET, p, &v4))
263 for (i = 0, m = 32; !(v4.s_addr & 1) && (i < 32); i++)
270 else if ((p = strchr(s, '-')) != NULL)
274 if (inet_pton(AF_INET6, p, &v6))
276 addr.family = FW3_FAMILY_V6;
277 addr.address2.v6 = v6;
280 else if (inet_pton(AF_INET, p, &v4))
282 addr.family = FW3_FAMILY_V4;
283 addr.address2.v4 = v4;
293 if (inet_pton(AF_INET6, s, &v6))
295 addr.family = FW3_FAMILY_V6;
296 addr.address.v6 = v6;
297 addr.mask = (m >= 0) ? m : 128;
299 else if (inet_pton(AF_INET, s, &v4))
301 addr.family = FW3_FAMILY_V4;
302 addr.address.v4 = v4;
303 addr.mask = (m >= 0) ? m : 32;
313 put_value(ptr, &addr, sizeof(addr), is_list);
318 fw3_parse_network(void *ptr, const char *val, bool is_list)
320 struct fw3_device dev = { };
321 struct fw3_address *addr;
322 struct list_head *addr_list;
324 if (!fw3_parse_address(ptr, val, is_list))
326 if (!fw3_parse_device(&dev, val, false))
329 addr_list = fw3_ubus_address(dev.name);
333 list_for_each_entry(addr, addr_list, list)
335 addr->invert = dev.invert;
337 if (!put_value(ptr, addr, sizeof(*addr), is_list))
341 fw3_ubus_address_free(addr_list);
349 fw3_parse_mac(void *ptr, const char *val, bool is_list)
351 struct fw3_mac addr = { };
352 struct ether_addr *mac;
357 while (isspace(*++val));
360 if ((mac = ether_aton(val)) != NULL)
365 put_value(ptr, &addr, sizeof(addr), is_list);
373 fw3_parse_port(void *ptr, const char *val, bool is_list)
375 struct fw3_port range = { };
383 while (isspace(*++val));
386 n = strtoul(val, &p, 10);
388 if (errno == ERANGE || errno == EINVAL)
391 if (*p && *p != '-' && *p != ':')
396 m = strtoul(++p, NULL, 10);
398 if (errno == ERANGE || errno == EINVAL || m < n)
411 put_value(ptr, &range, sizeof(range), is_list);
416 fw3_parse_family(void *ptr, const char *val, bool is_list)
418 if (!strcmp(val, "any"))
419 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
420 else if (!strcmp(val, "inet") || strrchr(val, '4'))
421 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
422 else if (!strcmp(val, "inet6") || strrchr(val, '6'))
423 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
431 fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
433 struct fw3_icmptype icmp = { };
439 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
441 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
443 icmp.type = fw3_icmptype_list_v4[i].type;
444 icmp.code_min = fw3_icmptype_list_v4[i].code_min;
445 icmp.code_max = fw3_icmptype_list_v4[i].code_max;
452 for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
454 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
456 icmp.type6 = fw3_icmptype_list_v6[i].type;
457 icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
458 icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
467 i = strtoul(val, &p, 10);
469 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
477 i = strtoul(val, &p, 10);
479 if ((p == val) || (*p != 0) || (i > 0xFF))
488 icmp.code_max = 0xFF;
491 icmp.type6 = icmp.type;
492 icmp.code6_min = icmp.code_max;
493 icmp.code6_max = icmp.code_max;
499 icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
500 : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
502 put_value(ptr, &icmp, sizeof(icmp), is_list);
507 fw3_parse_protocol(void *ptr, const char *val, bool is_list)
509 struct fw3_protocol proto = { };
510 struct protoent *ent;
515 while (isspace(*++val));
518 if (!strcmp(val, "all"))
521 put_value(ptr, &proto, sizeof(proto), is_list);
524 else if (!strcmp(val, "icmpv6"))
528 else if (!strcmp(val, "tcpudp"))
531 if (put_value(ptr, &proto, sizeof(proto), is_list))
534 put_value(ptr, &proto, sizeof(proto), is_list);
540 ent = getprotobyname(val);
544 proto.protocol = ent->p_proto;
545 put_value(ptr, &proto, sizeof(proto), is_list);
549 proto.protocol = strtoul(val, NULL, 10);
551 if (errno == ERANGE || errno == EINVAL)
554 put_value(ptr, &proto, sizeof(proto), is_list);
559 fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
561 return parse_enum(ptr, val, ipset_methods,
562 FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
566 fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
568 struct fw3_ipset_datatype *type = ptr;
570 if (!strncmp(val, "dest_", 5))
575 else if (!strncmp(val, "dst_", 4))
580 else if (!strncmp(val, "src_", 4))
586 return parse_enum(&type->type, val, ipset_types,
587 FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET);
591 fw3_parse_date(void *ptr, const char *val, bool is_list)
593 unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
594 struct tm tm = { 0 };
597 year = strtoul(val, &p, 10);
598 if ((*p != '-' && *p) || year < 1970 || year > 2038)
603 mon = strtoul(++p, &p, 10);
604 if ((*p != '-' && *p) || mon > 12)
609 day = strtoul(++p, &p, 10);
610 if ((*p != 'T' && *p) || day > 31)
615 hour = strtoul(++p, &p, 10);
616 if ((*p != ':' && *p) || hour > 23)
621 min = strtoul(++p, &p, 10);
622 if ((*p != ':' && *p) || min > 59)
627 sec = strtoul(++p, &p, 10);
632 tm.tm_year = year - 1900;
639 if (mktime(&tm) >= 0)
641 *((struct tm *)ptr) = tm;
650 fw3_parse_time(void *ptr, const char *val, bool is_list)
652 unsigned int hour = 0, min = 0, sec = 0;
655 hour = strtoul(val, &p, 10);
656 if (*p != ':' || hour > 23)
659 min = strtoul(++p, &p, 10);
660 if ((*p != ':' && *p) || min > 59)
665 sec = strtoul(++p, &p, 10);
670 *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
678 fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
685 setbit(*(uint8_t *)ptr, 0);
686 while (isspace(*++val));
689 if (!(s = strdup(val)))
692 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
694 if (!parse_enum(&w, p, weekdays, 1, 7))
696 w = strtoul(p, &p, 10);
698 if (*p || w < 1 || w > 7)
705 setbit(*(uint8_t *)ptr, w);
713 fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
720 setbit(*(uint32_t *)ptr, 0);
721 while (isspace(*++val));
724 if (!(s = strdup(val)))
727 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
729 d = strtoul(p, &p, 10);
731 if (*p || d < 1 || d > 31)
737 setbit(*(uint32_t *)ptr, d);
745 fw3_parse_include_type(void *ptr, const char *val, bool is_list)
747 return parse_enum(ptr, val, include_types,
748 FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
752 fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
754 return parse_enum(ptr, val, reflection_sources,
755 FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
759 fw3_parse_mark(void *ptr, const char *val, bool is_list)
763 struct fw3_mark *m = ptr;
768 while (isspace(*++val));
771 if ((s = strchr(val, '/')) != NULL)
774 n = strtoul(val, &e, 0);
780 m->mask = 0xFFFFFFFF;
784 n = strtoul(s, &e, 0);
798 fw3_parse_options(void *s, const struct fw3_option *opts,
799 struct uci_section *section)
803 struct uci_element *e, *l;
804 struct uci_option *o;
805 const struct fw3_option *opt;
806 struct list_head *dest;
808 uci_foreach_element(§ion->options, e)
810 o = uci_to_option(e);
813 for (opt = opts; opt->name; opt++)
818 if (strcmp(opt->name, e->name))
821 if (o->type == UCI_TYPE_LIST)
825 warn_elem(e, "must not be a list");
829 dest = (struct list_head *)((char *)s + opt->offset);
831 uci_foreach_element(&o->v.list, l)
836 if (!opt->parse(dest, l->name, true))
838 warn_elem(e, "has invalid value '%s'", l->name);
853 if (!opt->parse((char *)s + opt->offset, o->v.string, false))
854 warn_elem(e, "has invalid value '%s'", o->v.string);
858 dest = (struct list_head *)((char *)s + opt->offset);
860 for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
862 if (!opt->parse(dest, p, true))
864 warn_elem(e, "has invalid value '%s'", p);
876 warn_elem(e, "is unknown");
882 fw3_format_in_out(struct fw3_device *in, struct fw3_device *out)
885 fw3_pr(" %s-i %s", in->invert ? "! " : "", in->name);
887 if (out && !out->any)
888 fw3_pr(" %s-o %s", out->invert ? "! " : "", out->name);
892 fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
894 char s[INET6_ADDRSTRLEN];
896 if ((src && src->range) || (dest && dest->range))
897 fw3_pr(" -m iprange");
903 inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
904 &src->address.v4, s, sizeof(s));
906 fw3_pr(" %s--src-range %s", src->invert ? "! " : "", s);
908 inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
909 &src->address2.v4, s, sizeof(s));
915 inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
916 &src->address.v4, s, sizeof(s));
918 fw3_pr(" %s-s %s/%u", src->invert ? "! " : "", s, src->mask);
922 if (dest && dest->set)
926 inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
927 &dest->address.v4, s, sizeof(s));
929 fw3_pr(" %s--dst-range %s", dest->invert ? "! " : "", s);
931 inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
932 &dest->address2.v4, s, sizeof(s));
938 inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
939 &dest->address.v4, s, sizeof(s));
941 fw3_pr(" %s-d %s/%u", dest->invert ? "! " : "", s, dest->mask);
947 fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp)
951 if (sp->port_min == sp->port_max)
952 fw3_pr(" %s--sport %u", sp->invert ? "! " : "", sp->port_min);
954 fw3_pr(" %s--sport %u:%u",
955 sp->invert ? "! " : "", sp->port_min, sp->port_max);
960 if (dp->port_min == dp->port_max)
961 fw3_pr(" %s--dport %u", dp->invert ? "! " : "", dp->port_min);
963 fw3_pr(" %s--dport %u:%u",
964 dp->invert ? "! " : "", dp->port_min, dp->port_max);
969 fw3_format_mac(struct fw3_mac *mac)
974 fw3_pr(" -m mac %s--mac-source %s",
975 mac->invert ? "! " : "", ether_ntoa(&mac->mac));
979 fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family)
986 pr = proto->protocol;
988 if (pr == 1 && family == FW3_FAMILY_V6)
994 fw3_pr(" %s-p %u", proto->invert ? "! " : "", pr);
998 fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family)
1003 if (family != FW3_FAMILY_V6)
1005 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
1006 fw3_pr(" %s--icmp-type %u", icmp->invert ? "! " : "", icmp->type);
1008 fw3_pr(" %s--icmp-type %u/%u",
1009 icmp->invert ? "! " : "", icmp->type, icmp->code_min);
1013 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
1014 fw3_pr(" %s--icmpv6-type %u", icmp->invert ? "! " : "", icmp->type6);
1016 fw3_pr(" %s--icmpv6-type %u/%u",
1017 icmp->invert ? "! " : "", icmp->type6, icmp->code6_min);
1022 fw3_format_limit(struct fw3_limit *limit)
1027 if (limit->rate > 0)
1029 fw3_pr(" -m limit %s--limit %u/%s",
1030 limit->invert ? "! " : "",
1031 limit->rate, limit_units[limit->unit]);
1033 if (limit->burst > 0)
1034 fw3_pr(" --limit-burst %u", limit->burst);
1039 fw3_format_ipset(struct fw3_ipset *ipset, bool invert)
1042 const char *name = NULL;
1043 struct fw3_ipset_datatype *type;
1048 if (ipset->external && *ipset->external)
1049 name = ipset->external;
1053 fw3_pr(" -m set %s--match-set %s", invert ? "! " : "", name);
1055 list_for_each_entry(type, &ipset->datatypes, list)
1057 fw3_pr("%c%s", first ? ' ' : ',', type->dest ? "dst" : "src");
1063 fw3_format_time(struct fw3_time *time)
1066 struct tm empty = { 0 };
1067 char buf[sizeof("9999-99-99T23:59:59\0")];
1068 bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
1069 bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
1072 if (!d1 && !d2 && !time->timestart && !time->timestop &&
1073 !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
1085 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
1086 fw3_pr(" --datestart %s", buf);
1091 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
1092 fw3_pr(" --datestop %s", buf);
1095 if (time->timestart)
1097 fw3_pr(" --timestart %02d:%02d:%02d",
1098 time->timestart / 3600,
1099 time->timestart % 3600 / 60,
1100 time->timestart % 60);
1105 fw3_pr(" --timestop %02d:%02d:%02d",
1106 time->timestop / 3600,
1107 time->timestop % 3600 / 60,
1108 time->timestop % 60);
1111 if (time->monthdays & 0xFFFFFFFE)
1113 fw3_pr(" %s--monthdays", hasbit(time->monthdays, 0) ? "! " : "");
1115 for (i = 1, first = true; i < 32; i++)
1117 if (hasbit(time->monthdays, i))
1119 fw3_pr("%c%u", first ? ' ' : ',', i);
1125 if (time->weekdays & 0xFE)
1127 fw3_pr(" %s--weekdays", hasbit(time->weekdays, 0) ? "! " : "");
1129 for (i = 1, first = true; i < 8; i++)
1131 if (hasbit(time->weekdays, i))
1133 fw3_pr("%c%u", first ? ' ' : ',', i);
1141 fw3_format_mark(struct fw3_mark *mark)
1146 fw3_pr(" -m mark %s--mark 0x%x", mark->invert ? "! " : "", mark->mark);
1148 if (mark->mask < 0xFFFFFFFF)
1149 fw3_pr("/0x%x", mark->mask);
1153 __fw3_format_comment(const char *comment, ...)
1159 if (!comment || !*comment)
1162 fw3_pr(" -m comment --comment \"");
1166 va_start(ap, comment);
1192 c = va_arg(ap, const char *);
1202 fw3_format_extra(const char *extra)
1204 if (!extra || !*extra)
1207 fw3_pr(" %s", extra);