1f10050bb14d1117c7c50ef15230c5a42157c141
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.
21 const char *fw3_flag_names
[FW3_DEFAULT_DROP_INVALID
+ 1] = {
39 fw3_parse_bool(void *ptr
, const char *val
)
41 if (!strcmp(val
, "true") || !strcmp(val
, "yes") || !strcmp(val
, "1"))
42 *((bool *)ptr
) = true;
44 *((bool *)ptr
) = false;
50 fw3_parse_int(void *ptr
, const char *val
)
52 int n
= strtol(val
, NULL
, 10);
54 if (errno
== ERANGE
|| errno
== EINVAL
)
63 fw3_parse_string(void *ptr
, const char *val
)
65 *((char **)ptr
) = (char *)val
;
70 fw3_parse_target(void *ptr
, const char *val
)
72 if (!strcmp(val
, "ACCEPT"))
74 *((enum fw3_target
*)ptr
) = FW3_TARGET_ACCEPT
;
77 else if (!strcmp(val
, "REJECT"))
79 *((enum fw3_target
*)ptr
) = FW3_TARGET_REJECT
;
82 else if (!strcmp(val
, "DROP"))
84 *((enum fw3_target
*)ptr
) = FW3_TARGET_DROP
;
87 else if (!strcmp(val
, "NOTRACK"))
89 *((enum fw3_target
*)ptr
) = FW3_TARGET_NOTRACK
;
92 else if (!strcmp(val
, "DNAT"))
94 *((enum fw3_target
*)ptr
) = FW3_TARGET_DNAT
;
97 else if (!strcmp(val
, "SNAT"))
99 *((enum fw3_target
*)ptr
) = FW3_TARGET_SNAT
;
107 fw3_parse_limit(void *ptr
, const char *val
)
109 struct fw3_limit
*limit
= ptr
;
110 enum fw3_limit_unit u
= FW3_LIMIT_UNIT_SECOND
;
116 limit
->invert
= true;
117 while (isspace(*++val
));
120 n
= strtol(val
, &e
, 10);
122 if (errno
== ERANGE
|| errno
== EINVAL
)
125 if (*e
&& *e
++ != '/')
131 if (!strncmp(e
, "second", strlen(e
)))
132 u
= FW3_LIMIT_UNIT_SECOND
;
133 else if (!strncmp(e
, "minute", strlen(e
)))
134 u
= FW3_LIMIT_UNIT_MINUTE
;
135 else if (!strncmp(e
, "hour", strlen(e
)))
136 u
= FW3_LIMIT_UNIT_HOUR
;
137 else if (!strncmp(e
, "day", strlen(e
)))
138 u
= FW3_LIMIT_UNIT_DAY
;
149 fw3_parse_device(void *ptr
, const char *val
)
151 struct fw3_device
*dev
= ptr
;
163 while (isspace(*++val
));
167 snprintf(dev
->name
, sizeof(dev
->name
), "%s", val
);
176 fw3_parse_address(void *ptr
, const char *val
)
178 struct fw3_address
*addr
= ptr
;
187 while (isspace(*++val
));
195 if ((p
= strchr(s
, '/')) != NULL
)
198 m
= strtoul(p
, &e
, 10);
200 if ((e
== p
) || (*e
!= 0))
202 if (strchr(s
, ':') || !inet_pton(AF_INET
, p
, &v4
))
208 for (i
= 0, m
= 32; !(v4
.s_addr
& 1) && (i
< 32); i
++)
216 if (inet_pton(AF_INET6
, s
, &v6
))
218 addr
->family
= FW3_FAMILY_V6
;
219 addr
->address
.v6
= v6
;
220 addr
->mask
= (m
>= 0) ? m
: 128;
222 else if (inet_pton(AF_INET
, s
, &v4
))
224 addr
->family
= FW3_FAMILY_V4
;
225 addr
->address
.v4
= v4
;
226 addr
->mask
= (m
>= 0) ? m
: 32;
240 fw3_parse_mac(void *ptr
, const char *val
)
242 struct fw3_mac
*addr
= ptr
;
243 struct ether_addr
*mac
;
248 while (isspace(*++val
));
251 if ((mac
= ether_aton(val
)) != NULL
)
262 fw3_parse_port(void *ptr
, const char *val
)
264 struct fw3_port
*range
= ptr
;
271 range
->invert
= true;
272 while (isspace(*++val
));
275 n
= strtoul(val
, &p
, 10);
277 if (errno
== ERANGE
|| errno
== EINVAL
)
280 if (*p
&& *p
!= '-' && *p
!= ':')
285 m
= strtoul(++p
, NULL
, 10);
287 if (errno
== ERANGE
|| errno
== EINVAL
|| m
< n
)
304 fw3_parse_family(void *ptr
, const char *val
)
306 if (!strcmp(val
, "any"))
307 *((enum fw3_family
*)ptr
) = FW3_FAMILY_ANY
;
308 else if (!strcmp(val
, "inet") || strrchr(val
, '4'))
309 *((enum fw3_family
*)ptr
) = FW3_FAMILY_V4
;
310 else if (!strcmp(val
, "inet6") || strrchr(val
, '6'))
311 *((enum fw3_family
*)ptr
) = FW3_FAMILY_V6
;
319 fw3_parse_icmptype(void *ptr
, const char *val
)
321 struct fw3_icmptype
*icmp
= ptr
;
327 for (i
= 0; i
< ARRAY_SIZE(fw3_icmptype_list_v4
); i
++)
329 if (!strcmp(val
, fw3_icmptype_list_v4
[i
].name
))
331 icmp
->type
= fw3_icmptype_list_v4
[i
].type
;
332 icmp
->code_min
= fw3_icmptype_list_v4
[i
].code_min
;
333 icmp
->code_max
= fw3_icmptype_list_v4
[i
].code_max
;
340 for (i
= 0; i
< ARRAY_SIZE(fw3_icmptype_list_v6
); i
++)
342 if (!strcmp(val
, fw3_icmptype_list_v6
[i
].name
))
344 icmp
->type6
= fw3_icmptype_list_v6
[i
].type
;
345 icmp
->code6_min
= fw3_icmptype_list_v6
[i
].code_min
;
346 icmp
->code6_max
= fw3_icmptype_list_v6
[i
].code_max
;
355 i
= strtoul(val
, &p
, 10);
357 if ((p
== val
) || (*p
!= '/' && *p
!= 0) || (i
> 0xFF))
365 i
= strtoul(val
, &p
, 10);
367 if ((p
== val
) || (*p
!= 0) || (i
> 0xFF))
376 icmp
->code_max
= 0xFF;
379 icmp
->type6
= icmp
->type
;
380 icmp
->code6_min
= icmp
->code_max
;
381 icmp
->code6_max
= icmp
->code_max
;
387 icmp
->family
= (v4
&& v6
) ? FW3_FAMILY_ANY
388 : (v6
? FW3_FAMILY_V6
: FW3_FAMILY_V4
);
394 fw3_parse_protocol(void *ptr
, const char *val
)
396 struct fw3_protocol
*proto
= ptr
;
397 struct protoent
*ent
;
401 proto
->invert
= true;
402 while (isspace(*++val
));
405 if (!strcmp(val
, "all"))
410 else if (!strcmp(val
, "icmpv6"))
415 ent
= getprotobyname(val
);
419 proto
->protocol
= ent
->p_proto
;
423 proto
->protocol
= strtoul(val
, NULL
, 10);
424 return (errno
!= ERANGE
&& errno
!= EINVAL
);
428 fw3_parse_ipset_method(void *ptr
, const char *val
)
430 if (!strncmp(val
, "bitmap", strlen(val
)))
432 *((enum fw3_ipset_method
*)ptr
) = FW3_IPSET_METHOD_BITMAP
;
435 else if (!strncmp(val
, "hash", strlen(val
)))
437 *((enum fw3_ipset_method
*)ptr
) = FW3_IPSET_METHOD_HASH
;
440 else if (!strncmp(val
, "list", strlen(val
)))
442 *((enum fw3_ipset_method
*)ptr
) = FW3_IPSET_METHOD_LIST
;
450 fw3_parse_ipset_datatype(void *ptr
, const char *val
)
452 struct fw3_ipset_datatype
*type
= ptr
;
454 if (!strncmp(val
, "dest_", 5))
459 else if (!strncmp(val
, "dst_", 4))
464 else if (!strncmp(val
, "src_", 4))
470 if (!strncmp(val
, "ip", strlen(val
)))
472 type
->type
= FW3_IPSET_TYPE_IP
;
475 else if (!strncmp(val
, "port", strlen(val
)))
477 type
->type
= FW3_IPSET_TYPE_PORT
;
480 else if (!strncmp(val
, "mac", strlen(val
)))
482 type
->type
= FW3_IPSET_TYPE_MAC
;
485 else if (!strncmp(val
, "net", strlen(val
)))
487 type
->type
= FW3_IPSET_TYPE_NET
;
490 else if (!strncmp(val
, "set", strlen(val
)))
492 type
->type
= FW3_IPSET_TYPE_SET
;
501 fw3_parse_options(void *s
,
502 struct fw3_option
*opts
, int n
,
503 struct uci_section
*section
)
508 struct uci_element
*e
, *l
;
509 struct uci_option
*o
;
510 struct fw3_option
*opt
;
511 struct list_head
*item
;
512 struct list_head
*dest
;
514 uci_foreach_element(§ion
->options
, e
)
516 o
= uci_to_option(e
);
519 for (i
= 0; i
< n
; i
++)
523 if (!opt
->parse
|| !opt
->name
)
526 if (strcmp(opt
->name
, e
->name
))
529 if (o
->type
== UCI_TYPE_LIST
)
533 warn_elem(e
, "must not be a list");
537 uci_foreach_element(&o
->v
.list
, l
)
542 item
= malloc(opt
->elem_size
);
547 memset(item
, 0, opt
->elem_size
);
549 if (!opt
->parse(item
, l
->name
))
551 warn_elem(e
, "has invalid value '%s'", l
->name
);
556 dest
= (struct list_head
*)((char *)s
+ opt
->offset
);
557 list_add_tail(item
, dest
);
568 if (!opt
->parse((char *)s
+ opt
->offset
, o
->v
.string
))
569 warn_elem(e
, "has invalid value '%s'", o
->v
.string
);
573 for (p
= strtok(o
->v
.string
, " \t");
575 p
= strtok(NULL
, " \t"))
577 item
= malloc(opt
->elem_size
);
582 memset(item
, 0, opt
->elem_size
);
584 if (!opt
->parse(item
, p
))
586 warn_elem(e
, "has invalid value '%s'", p
);
591 dest
= (struct list_head
*)((char *)s
+ opt
->offset
);
592 list_add_tail(item
, dest
);
602 warn_elem(e
, "is unknown");
608 fw3_format_in_out(struct fw3_device
*in
, struct fw3_device
*out
)
611 fw3_pr(" %s-i %s", in
->invert
? "! " : "", in
->name
);
613 if (out
&& !out
->any
)
614 fw3_pr(" %s-o %s", out
->invert
? "! " : "", out
->name
);
618 fw3_format_src_dest(struct fw3_address
*src
, struct fw3_address
*dest
)
620 char s
[INET6_ADDRSTRLEN
];
624 inet_ntop(src
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
625 &src
->address
.v4
, s
, sizeof(s
));
627 fw3_pr(" %s-s %s/%u", src
->invert
? "! " : "", s
, src
->mask
);
630 if (dest
&& dest
->set
)
632 inet_ntop(dest
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
633 &dest
->address
.v4
, s
, sizeof(s
));
635 fw3_pr(" %s-d %s/%u", dest
->invert
? "! " : "", s
, dest
->mask
);
640 fw3_format_sport_dport(struct fw3_port
*sp
, struct fw3_port
*dp
)
644 if (sp
->port_min
== sp
->port_max
)
645 fw3_pr(" %s--sport %u", sp
->invert
? "! " : "", sp
->port_min
);
647 fw3_pr(" %s--sport %u:%u",
648 sp
->invert
? "! " : "", sp
->port_min
, sp
->port_max
);
653 if (dp
->port_min
== dp
->port_max
)
654 fw3_pr(" %s--dport %u", dp
->invert
? "! " : "", dp
->port_min
);
656 fw3_pr(" %s--dport %u:%u",
657 dp
->invert
? "! " : "", dp
->port_min
, dp
->port_max
);
662 fw3_format_mac(struct fw3_mac
*mac
)
667 fw3_pr(" -m mac %s--mac-source %s",
668 mac
->invert
? "! " : "", ether_ntoa(&mac
->mac
));
672 fw3_format_protocol(struct fw3_protocol
*proto
, enum fw3_family family
)
679 pr
= proto
->protocol
;
681 if (pr
== 1 && family
== FW3_FAMILY_V6
)
687 fw3_pr(" %s-p %u", proto
->invert
? "! " : "", pr
);
691 fw3_format_icmptype(struct fw3_icmptype
*icmp
, enum fw3_family family
)
696 if (family
!= FW3_FAMILY_V6
)
698 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
699 fw3_pr(" %s--icmp-type %u", icmp
->invert
? "! " : "", icmp
->type
);
701 fw3_pr(" %s--icmp-type %u/%u",
702 icmp
->invert
? "! " : "", icmp
->type
, icmp
->code_min
);
706 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
707 fw3_pr(" %s--icmpv6-type %u", icmp
->invert
? "! " : "", icmp
->type6
);
709 fw3_pr(" %s--icmpv6-type %u/%u",
710 icmp
->invert
? "! " : "", icmp
->type6
, icmp
->code6_min
);
715 fw3_format_limit(struct fw3_limit
*limit
)
720 const char *units
[] = {
721 [FW3_LIMIT_UNIT_SECOND
] = "second",
722 [FW3_LIMIT_UNIT_MINUTE
] = "minute",
723 [FW3_LIMIT_UNIT_HOUR
] = "hour",
724 [FW3_LIMIT_UNIT_DAY
] = "day",
729 fw3_pr(" -m limit %s--limit %u/%s",
730 limit
->invert
? "! " : "", limit
->rate
, units
[limit
->unit
]);
732 if (limit
->burst
> 0)
733 fw3_pr(" --limit-burst %u", limit
->burst
);
738 fw3_format_ipset(struct fw3_ipset
*ipset
, bool invert
)
741 const char *name
= NULL
;
742 struct fw3_ipset_datatype
*type
;
747 if (ipset
->external
&& *ipset
->external
)
748 name
= ipset
->external
;
752 fw3_pr(" -m set %s--match-set %s", invert
? "! " : "", name
);
754 list_for_each_entry(type
, &ipset
->datatypes
, list
)
756 fw3_pr("%c%s", first
? ' ' : ',', type
->dest
? "dst" : "src");
762 __fw3_format_comment(const char *comment
, ...)
768 if (!comment
|| !*comment
)
771 fw3_pr(" -m comment --comment \"");
775 va_start(ap
, comment
);
801 c
= va_arg(ap
, const char *);
811 fw3_format_extra(const char *extra
)
813 if (!extra
|| !*extra
)
816 fw3_pr(" %s", extra
);