a35247f331faa374fc1c595e34227f43173e54fc
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.
23 parse_enum(void *ptr
, const char *val
, const char **values
, int min
, int max
)
25 int i
, l
= strlen(val
);
29 for (i
= 0; i
<= (max
- min
); i
++)
31 if (!strncasecmp(val
, values
[i
], l
))
33 *((int *)ptr
) = min
+ i
;
43 const char *fw3_flag_names
[FW3_DEFAULT_DROP_INVALID
+ 1] = {
60 static const char *limit_units
[] = {
67 static const char *ipset_methods
[] = {
73 static const char *ipset_types
[] = {
81 static const char *weekdays
[] = {
91 static const char *include_types
[] = {
98 fw3_parse_bool(void *ptr
, const char *val
)
100 if (!strcmp(val
, "true") || !strcmp(val
, "yes") || !strcmp(val
, "1"))
101 *((bool *)ptr
) = true;
103 *((bool *)ptr
) = false;
109 fw3_parse_int(void *ptr
, const char *val
)
111 int n
= strtol(val
, NULL
, 10);
113 if (errno
== ERANGE
|| errno
== EINVAL
)
122 fw3_parse_string(void *ptr
, const char *val
)
124 *((char **)ptr
) = (char *)val
;
129 fw3_parse_target(void *ptr
, const char *val
)
131 return parse_enum(ptr
, val
, &fw3_flag_names
[FW3_TARGET_ACCEPT
],
132 FW3_TARGET_ACCEPT
, FW3_TARGET_SNAT
);
136 fw3_parse_limit(void *ptr
, const char *val
)
138 struct fw3_limit
*limit
= ptr
;
139 enum fw3_limit_unit u
= FW3_LIMIT_UNIT_SECOND
;
145 limit
->invert
= true;
146 while (isspace(*++val
));
149 n
= strtol(val
, &e
, 10);
151 if (errno
== ERANGE
|| errno
== EINVAL
)
154 if (*e
&& *e
++ != '/')
160 if (!parse_enum(&u
, e
, limit_units
, 0, FW3_LIMIT_UNIT_DAY
))
170 fw3_parse_device(void *ptr
, const char *val
)
172 struct fw3_device
*dev
= ptr
;
184 while (isspace(*++val
));
188 snprintf(dev
->name
, sizeof(dev
->name
), "%s", val
);
197 fw3_parse_address(void *ptr
, const char *val
)
199 struct fw3_address
*addr
= ptr
;
208 while (isspace(*++val
));
216 if ((p
= strchr(s
, '/')) != NULL
)
219 m
= strtoul(p
, &e
, 10);
221 if ((e
== p
) || (*e
!= 0))
223 if (strchr(s
, ':') || !inet_pton(AF_INET
, p
, &v4
))
229 for (i
= 0, m
= 32; !(v4
.s_addr
& 1) && (i
< 32); i
++)
236 else if ((p
= strchr(s
, '-')) != NULL
)
240 if (inet_pton(AF_INET6
, p
, &v6
))
242 addr
->family
= FW3_FAMILY_V6
;
243 addr
->address2
.v6
= v6
;
246 else if (inet_pton(AF_INET
, p
, &v4
))
248 addr
->family
= FW3_FAMILY_V4
;
249 addr
->address2
.v4
= v4
;
259 if (inet_pton(AF_INET6
, s
, &v6
))
261 addr
->family
= FW3_FAMILY_V6
;
262 addr
->address
.v6
= v6
;
263 addr
->mask
= (m
>= 0) ? m
: 128;
265 else if (inet_pton(AF_INET
, s
, &v4
))
267 addr
->family
= FW3_FAMILY_V4
;
268 addr
->address
.v4
= v4
;
269 addr
->mask
= (m
>= 0) ? m
: 32;
283 fw3_parse_mac(void *ptr
, const char *val
)
285 struct fw3_mac
*addr
= ptr
;
286 struct ether_addr
*mac
;
291 while (isspace(*++val
));
294 if ((mac
= ether_aton(val
)) != NULL
)
305 fw3_parse_port(void *ptr
, const char *val
)
307 struct fw3_port
*range
= ptr
;
314 range
->invert
= true;
315 while (isspace(*++val
));
318 n
= strtoul(val
, &p
, 10);
320 if (errno
== ERANGE
|| errno
== EINVAL
)
323 if (*p
&& *p
!= '-' && *p
!= ':')
328 m
= strtoul(++p
, NULL
, 10);
330 if (errno
== ERANGE
|| errno
== EINVAL
|| m
< n
)
347 fw3_parse_family(void *ptr
, const char *val
)
349 if (!strcmp(val
, "any"))
350 *((enum fw3_family
*)ptr
) = FW3_FAMILY_ANY
;
351 else if (!strcmp(val
, "inet") || strrchr(val
, '4'))
352 *((enum fw3_family
*)ptr
) = FW3_FAMILY_V4
;
353 else if (!strcmp(val
, "inet6") || strrchr(val
, '6'))
354 *((enum fw3_family
*)ptr
) = FW3_FAMILY_V6
;
362 fw3_parse_icmptype(void *ptr
, const char *val
)
364 struct fw3_icmptype
*icmp
= ptr
;
370 for (i
= 0; i
< ARRAY_SIZE(fw3_icmptype_list_v4
); i
++)
372 if (!strcmp(val
, fw3_icmptype_list_v4
[i
].name
))
374 icmp
->type
= fw3_icmptype_list_v4
[i
].type
;
375 icmp
->code_min
= fw3_icmptype_list_v4
[i
].code_min
;
376 icmp
->code_max
= fw3_icmptype_list_v4
[i
].code_max
;
383 for (i
= 0; i
< ARRAY_SIZE(fw3_icmptype_list_v6
); i
++)
385 if (!strcmp(val
, fw3_icmptype_list_v6
[i
].name
))
387 icmp
->type6
= fw3_icmptype_list_v6
[i
].type
;
388 icmp
->code6_min
= fw3_icmptype_list_v6
[i
].code_min
;
389 icmp
->code6_max
= fw3_icmptype_list_v6
[i
].code_max
;
398 i
= strtoul(val
, &p
, 10);
400 if ((p
== val
) || (*p
!= '/' && *p
!= 0) || (i
> 0xFF))
408 i
= strtoul(val
, &p
, 10);
410 if ((p
== val
) || (*p
!= 0) || (i
> 0xFF))
419 icmp
->code_max
= 0xFF;
422 icmp
->type6
= icmp
->type
;
423 icmp
->code6_min
= icmp
->code_max
;
424 icmp
->code6_max
= icmp
->code_max
;
430 icmp
->family
= (v4
&& v6
) ? FW3_FAMILY_ANY
431 : (v6
? FW3_FAMILY_V6
: FW3_FAMILY_V4
);
437 fw3_parse_protocol(void *ptr
, const char *val
)
439 struct fw3_protocol
*proto
= ptr
;
440 struct protoent
*ent
;
444 proto
->invert
= true;
445 while (isspace(*++val
));
448 if (!strcmp(val
, "all"))
453 else if (!strcmp(val
, "icmpv6"))
458 ent
= getprotobyname(val
);
462 proto
->protocol
= ent
->p_proto
;
466 proto
->protocol
= strtoul(val
, NULL
, 10);
467 return (errno
!= ERANGE
&& errno
!= EINVAL
);
471 fw3_parse_ipset_method(void *ptr
, const char *val
)
473 return parse_enum(ptr
, val
, ipset_methods
,
474 FW3_IPSET_METHOD_BITMAP
, FW3_IPSET_METHOD_LIST
);
478 fw3_parse_ipset_datatype(void *ptr
, const char *val
)
480 struct fw3_ipset_datatype
*type
= ptr
;
482 if (!strncmp(val
, "dest_", 5))
487 else if (!strncmp(val
, "dst_", 4))
492 else if (!strncmp(val
, "src_", 4))
498 return parse_enum(&type
->type
, val
, ipset_types
,
499 FW3_IPSET_TYPE_IP
, FW3_IPSET_TYPE_SET
);
503 fw3_parse_date(void *ptr
, const char *val
)
505 unsigned int year
= 1970, mon
= 1, day
= 1, hour
= 0, min
= 0, sec
= 0;
506 struct tm tm
= { 0 };
509 year
= strtoul(val
, &p
, 10);
510 if ((*p
!= '-' && *p
) || year
< 1970 || year
> 2038)
515 mon
= strtoul(++p
, &p
, 10);
516 if ((*p
!= '-' && *p
) || mon
> 12)
521 day
= strtoul(++p
, &p
, 10);
522 if ((*p
!= 'T' && *p
) || day
> 31)
527 hour
= strtoul(++p
, &p
, 10);
528 if ((*p
!= ':' && *p
) || hour
> 23)
533 min
= strtoul(++p
, &p
, 10);
534 if ((*p
!= ':' && *p
) || min
> 59)
539 sec
= strtoul(++p
, &p
, 10);
544 tm
.tm_year
= year
- 1900;
551 if (mktime(&tm
) >= 0)
553 *((struct tm
*)ptr
) = tm
;
562 fw3_parse_time(void *ptr
, const char *val
)
564 unsigned int hour
= 0, min
= 0, sec
= 0;
567 hour
= strtoul(val
, &p
, 10);
568 if (*p
!= ':' || hour
> 23)
571 min
= strtoul(++p
, &p
, 10);
572 if ((*p
!= ':' && *p
) || min
> 59)
577 sec
= strtoul(++p
, &p
, 10);
582 *((int *)ptr
) = 60 * 60 * hour
+ 60 * min
+ sec
;
590 fw3_parse_weekdays(void *ptr
, const char *val
)
597 setbit(*(uint8_t *)ptr
, 0);
598 while (isspace(*++val
));
601 if (!(s
= strdup(val
)))
604 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
606 if (!parse_enum(&w
, p
, weekdays
, 1, 7))
608 w
= strtoul(p
, &p
, 10);
610 if (*p
|| w
< 1 || w
> 7)
617 setbit(*(uint8_t *)ptr
, w
);
625 fw3_parse_monthdays(void *ptr
, const char *val
)
632 setbit(*(uint32_t *)ptr
, 0);
633 while (isspace(*++val
));
636 if (!(s
= strdup(val
)))
639 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
641 d
= strtoul(p
, &p
, 10);
643 if (*p
|| d
< 1 || d
> 31)
649 setbit(*(uint32_t *)ptr
, d
);
657 fw3_parse_include_type(void *ptr
, const char *val
)
659 return parse_enum(ptr
, val
, include_types
,
660 FW3_INC_TYPE_SCRIPT
, FW3_INC_TYPE_RESTORE
);
665 fw3_parse_options(void *s
, const struct fw3_option
*opts
,
666 struct uci_section
*section
)
670 struct uci_element
*e
, *l
;
671 struct uci_option
*o
;
672 const struct fw3_option
*opt
;
673 struct list_head
*item
;
674 struct list_head
*dest
;
676 uci_foreach_element(§ion
->options
, e
)
678 o
= uci_to_option(e
);
681 for (opt
= opts
; opt
->name
; opt
++)
686 if (strcmp(opt
->name
, e
->name
))
689 if (o
->type
== UCI_TYPE_LIST
)
693 warn_elem(e
, "must not be a list");
697 uci_foreach_element(&o
->v
.list
, l
)
702 item
= malloc(opt
->elem_size
);
707 memset(item
, 0, opt
->elem_size
);
709 if (!opt
->parse(item
, l
->name
))
711 warn_elem(e
, "has invalid value '%s'", l
->name
);
716 dest
= (struct list_head
*)((char *)s
+ opt
->offset
);
717 list_add_tail(item
, dest
);
728 if (!opt
->parse((char *)s
+ opt
->offset
, o
->v
.string
))
729 warn_elem(e
, "has invalid value '%s'", o
->v
.string
);
733 for (p
= strtok(o
->v
.string
, " \t");
735 p
= strtok(NULL
, " \t"))
737 item
= malloc(opt
->elem_size
);
742 memset(item
, 0, opt
->elem_size
);
744 if (!opt
->parse(item
, p
))
746 warn_elem(e
, "has invalid value '%s'", p
);
751 dest
= (struct list_head
*)((char *)s
+ opt
->offset
);
752 list_add_tail(item
, dest
);
762 warn_elem(e
, "is unknown");
768 fw3_format_in_out(struct fw3_device
*in
, struct fw3_device
*out
)
771 fw3_pr(" %s-i %s", in
->invert
? "! " : "", in
->name
);
773 if (out
&& !out
->any
)
774 fw3_pr(" %s-o %s", out
->invert
? "! " : "", out
->name
);
778 fw3_format_src_dest(struct fw3_address
*src
, struct fw3_address
*dest
)
780 char s
[INET6_ADDRSTRLEN
];
782 if ((src
&& src
->range
) || (dest
&& dest
->range
))
783 fw3_pr(" -m iprange");
789 inet_ntop(src
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
790 &src
->address
.v4
, s
, sizeof(s
));
792 fw3_pr(" %s--src-range %s", src
->invert
? "! " : "", s
);
794 inet_ntop(src
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
795 &src
->address2
.v4
, s
, sizeof(s
));
801 inet_ntop(src
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
802 &src
->address
.v4
, s
, sizeof(s
));
804 fw3_pr(" %s-s %s/%u", src
->invert
? "! " : "", s
, src
->mask
);
808 if (dest
&& dest
->set
)
812 inet_ntop(dest
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
813 &dest
->address
.v4
, s
, sizeof(s
));
815 fw3_pr(" %s--dst-range %s", dest
->invert
? "! " : "", s
);
817 inet_ntop(dest
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
818 &dest
->address2
.v4
, s
, sizeof(s
));
824 inet_ntop(dest
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
825 &dest
->address
.v4
, s
, sizeof(s
));
827 fw3_pr(" %s-d %s/%u", dest
->invert
? "! " : "", s
, dest
->mask
);
833 fw3_format_sport_dport(struct fw3_port
*sp
, struct fw3_port
*dp
)
837 if (sp
->port_min
== sp
->port_max
)
838 fw3_pr(" %s--sport %u", sp
->invert
? "! " : "", sp
->port_min
);
840 fw3_pr(" %s--sport %u:%u",
841 sp
->invert
? "! " : "", sp
->port_min
, sp
->port_max
);
846 if (dp
->port_min
== dp
->port_max
)
847 fw3_pr(" %s--dport %u", dp
->invert
? "! " : "", dp
->port_min
);
849 fw3_pr(" %s--dport %u:%u",
850 dp
->invert
? "! " : "", dp
->port_min
, dp
->port_max
);
855 fw3_format_mac(struct fw3_mac
*mac
)
860 fw3_pr(" -m mac %s--mac-source %s",
861 mac
->invert
? "! " : "", ether_ntoa(&mac
->mac
));
865 fw3_format_protocol(struct fw3_protocol
*proto
, enum fw3_family family
)
872 pr
= proto
->protocol
;
874 if (pr
== 1 && family
== FW3_FAMILY_V6
)
880 fw3_pr(" %s-p %u", proto
->invert
? "! " : "", pr
);
884 fw3_format_icmptype(struct fw3_icmptype
*icmp
, enum fw3_family family
)
889 if (family
!= FW3_FAMILY_V6
)
891 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
892 fw3_pr(" %s--icmp-type %u", icmp
->invert
? "! " : "", icmp
->type
);
894 fw3_pr(" %s--icmp-type %u/%u",
895 icmp
->invert
? "! " : "", icmp
->type
, icmp
->code_min
);
899 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
900 fw3_pr(" %s--icmpv6-type %u", icmp
->invert
? "! " : "", icmp
->type6
);
902 fw3_pr(" %s--icmpv6-type %u/%u",
903 icmp
->invert
? "! " : "", icmp
->type6
, icmp
->code6_min
);
908 fw3_format_limit(struct fw3_limit
*limit
)
915 fw3_pr(" -m limit %s--limit %u/%s",
916 limit
->invert
? "! " : "",
917 limit
->rate
, limit_units
[limit
->unit
]);
919 if (limit
->burst
> 0)
920 fw3_pr(" --limit-burst %u", limit
->burst
);
925 fw3_format_ipset(struct fw3_ipset
*ipset
, bool invert
)
928 const char *name
= NULL
;
929 struct fw3_ipset_datatype
*type
;
934 if (ipset
->external
&& *ipset
->external
)
935 name
= ipset
->external
;
939 fw3_pr(" -m set %s--match-set %s", invert
? "! " : "", name
);
941 list_for_each_entry(type
, &ipset
->datatypes
, list
)
943 fw3_pr("%c%s", first
? ' ' : ',', type
->dest
? "dst" : "src");
949 fw3_format_time(struct fw3_time
*time
)
952 struct tm empty
= { 0 };
953 char buf
[sizeof("9999-99-99T23:59:59\0")];
954 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
955 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
958 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
959 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
971 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
972 fw3_pr(" --datestart %s", buf
);
977 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
978 fw3_pr(" --datestop %s", buf
);
983 fw3_pr(" --timestart %02d:%02d:%02d",
984 time
->timestart
/ 3600,
985 time
->timestart
% 3600 / 60,
986 time
->timestart
% 60);
991 fw3_pr(" --timestop %02d:%02d:%02d",
992 time
->timestop
/ 3600,
993 time
->timestop
% 3600 / 60,
994 time
->timestop
% 60);
997 if (time
->monthdays
& 0xFFFFFFFE)
999 fw3_pr(" %s--monthdays", hasbit(time
->monthdays
, 0) ? "! " : "");
1001 for (i
= 1, first
= true; i
< 32; i
++)
1003 if (hasbit(time
->monthdays
, i
))
1005 fw3_pr("%c%u", first
? ' ' : ',', i
);
1011 if (time
->weekdays
& 0xFE)
1013 fw3_pr(" %s--weekdays", hasbit(time
->weekdays
, 0) ? "! " : "");
1015 for (i
= 1, first
= true; i
< 8; i
++)
1017 if (hasbit(time
->weekdays
, i
))
1019 fw3_pr("%c%u", first
? ' ' : ',', i
);
1027 __fw3_format_comment(const char *comment
, ...)
1033 if (!comment
|| !*comment
)
1036 fw3_pr(" -m comment --comment \"");
1040 va_start(ap
, comment
);
1066 c
= va_arg(ap
, const char *);
1076 fw3_format_extra(const char *extra
)
1078 if (!extra
|| !*extra
)
1081 fw3_pr(" %s", extra
);