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.
19 #include "redirects.h"
22 const struct fw3_option fw3_redirect_opts
[] = {
23 FW3_OPT("enabled", bool, redirect
, enabled
),
25 FW3_OPT("name", string
, redirect
, name
),
26 FW3_OPT("family", family
, redirect
, family
),
28 FW3_OPT("src", device
, redirect
, src
),
29 FW3_OPT("dest", device
, redirect
, dest
),
31 FW3_OPT("ipset", setmatch
, redirect
, ipset
),
33 FW3_LIST("proto", protocol
, redirect
, proto
),
35 FW3_OPT("src_ip", network
, redirect
, ip_src
),
36 FW3_LIST("src_mac", mac
, redirect
, mac_src
),
37 FW3_OPT("src_port", port
, redirect
, port_src
),
39 FW3_OPT("src_dip", network
, redirect
, ip_dest
),
40 FW3_OPT("src_dport", port
, redirect
, port_dest
),
42 FW3_OPT("dest_ip", network
, redirect
, ip_redir
),
43 FW3_OPT("dest_port", port
, redirect
, port_redir
),
45 FW3_OPT("extra", string
, redirect
, extra
),
47 FW3_OPT("limit", limit
, redirect
, limit
),
48 FW3_OPT("limit_burst", int, redirect
, limit
.burst
),
50 FW3_OPT("utc_time", bool, redirect
, time
.utc
),
51 FW3_OPT("start_date", date
, redirect
, time
.datestart
),
52 FW3_OPT("stop_date", date
, redirect
, time
.datestop
),
53 FW3_OPT("start_time", time
, redirect
, time
.timestart
),
54 FW3_OPT("stop_time", time
, redirect
, time
.timestop
),
55 FW3_OPT("weekdays", weekdays
, redirect
, time
.weekdays
),
56 FW3_OPT("monthdays", monthdays
, redirect
, time
.monthdays
),
58 FW3_OPT("mark", mark
, redirect
, mark
),
60 FW3_OPT("reflection", bool, redirect
, reflection
),
61 FW3_OPT("reflection_src", reflection_source
,
62 redirect
, reflection_src
),
64 FW3_OPT("target", target
, redirect
, target
),
71 check_families(struct uci_element
*e
, struct fw3_redirect
*r
)
73 if (r
->family
== FW3_FAMILY_ANY
)
76 if (r
->_src
&& r
->_src
->family
&& r
->_src
->family
!= r
->family
)
78 warn_elem(e
, "refers to source zone with different family");
82 if (r
->_dest
&& r
->_dest
->family
&& r
->_dest
->family
!= r
->family
)
84 warn_elem(e
, "refers to destination zone with different family");
88 if (r
->ipset
.ptr
&& r
->ipset
.ptr
->family
&&
89 r
->ipset
.ptr
->family
!= r
->family
)
91 warn_elem(e
, "refers to ipset with different family");
95 if (r
->ip_src
.family
&& r
->ip_src
.family
!= r
->family
)
97 warn_elem(e
, "uses source ip with different family");
101 if (r
->ip_dest
.family
&& r
->ip_dest
.family
!= r
->family
)
103 warn_elem(e
, "uses destination ip with different family");
107 if (r
->ip_redir
.family
&& r
->ip_redir
.family
!= r
->family
)
109 warn_elem(e
, "uses redirect ip with different family");
117 compare_addr(struct fw3_address
*a
, struct fw3_address
*b
)
119 if (a
->family
!= FW3_FAMILY_V4
|| b
->family
!= FW3_FAMILY_V4
)
122 return ((a
->address
.v4
.s_addr
& a
->mask
.v4
.s_addr
) ==
123 (b
->address
.v4
.s_addr
& a
->mask
.v4
.s_addr
));
127 resolve_dest(struct uci_element
*e
, struct fw3_redirect
*redir
,
128 struct fw3_state
*state
)
130 struct fw3_zone
*zone
;
131 struct fw3_address
*addr
;
132 struct list_head
*addrs
;
134 if (!redir
->ip_redir
.set
)
137 list_for_each_entry(zone
, &state
->zones
, list
)
139 addrs
= fw3_resolve_zone_addresses(zone
, NULL
);
144 list_for_each_entry(addr
, addrs
, list
)
146 if (!compare_addr(addr
, &redir
->ip_redir
))
149 strncpy(redir
->dest
.name
, zone
->name
, sizeof(redir
->dest
.name
));
150 redir
->dest
.set
= true;
156 fw3_free_list(addrs
);
166 check_local(struct uci_element
*e
, struct fw3_redirect
*redir
,
167 struct fw3_state
*state
)
169 if (redir
->target
!= FW3_FLAG_DNAT
)
172 if (!redir
->ip_redir
.set
)
179 fw3_load_redirects(struct fw3_state
*state
, struct uci_package
*p
)
181 struct uci_section
*s
;
182 struct uci_element
*e
;
183 struct fw3_redirect
*redir
;
187 INIT_LIST_HEAD(&state
->redirects
);
189 uci_foreach_element(&p
->sections
, e
)
191 s
= uci_to_section(e
);
193 if (strcmp(s
->type
, "redirect"))
196 redir
= calloc(1, sizeof(*redir
));
200 INIT_LIST_HEAD(&redir
->proto
);
201 INIT_LIST_HEAD(&redir
->mac_src
);
203 redir
->enabled
= true;
204 redir
->reflection
= true;
208 if (!fw3_parse_options(redir
, fw3_redirect_opts
, s
))
210 warn_elem(e
, "skipped due to invalid options");
211 fw3_free_redirect(redir
);
217 fw3_free_redirect(redir
);
221 if (redir
->src
.invert
)
223 warn_elem(e
, "must not have an inverted source");
224 fw3_free_redirect(redir
);
227 else if (redir
->src
.set
&& !redir
->src
.any
&&
228 !(redir
->_src
= fw3_lookup_zone(state
, redir
->src
.name
)))
230 warn_elem(e
, "refers to not existing zone '%s'", redir
->src
.name
);
231 fw3_free_redirect(redir
);
234 else if (redir
->dest
.set
&& !redir
->dest
.any
&&
235 !(redir
->_dest
= fw3_lookup_zone(state
, redir
->dest
.name
)))
237 warn_elem(e
, "refers to not existing zone '%s'", redir
->dest
.name
);
238 fw3_free_redirect(redir
);
241 else if (redir
->ipset
.set
&& state
->disable_ipsets
)
243 warn_elem(e
, "skipped due to disabled ipset support");
244 fw3_free_redirect(redir
);
247 else if (redir
->ipset
.set
&&
248 !(redir
->ipset
.ptr
= fw3_lookup_ipset(state
, redir
->ipset
.name
)))
250 warn_elem(e
, "refers to unknown ipset '%s'", redir
->ipset
.name
);
251 fw3_free_redirect(redir
);
255 if (!check_families(e
, redir
))
257 fw3_free_redirect(redir
);
261 if (redir
->target
== FW3_FLAG_UNSPEC
)
263 warn_elem(e
, "has no target specified, defaulting to DNAT");
264 redir
->target
= FW3_FLAG_DNAT
;
266 else if (redir
->target
< FW3_FLAG_DNAT
|| redir
->target
> FW3_FLAG_SNAT
)
268 warn_elem(e
, "has invalid target specified, defaulting to DNAT");
269 redir
->target
= FW3_FLAG_DNAT
;
272 if (redir
->target
== FW3_FLAG_DNAT
)
275 warn_elem(e
, "must not have source '*' for DNAT target");
276 else if (!redir
->_src
)
277 warn_elem(e
, "has no source specified");
280 set(redir
->_src
->flags
, FW3_FAMILY_V4
, redir
->target
);
283 if (!check_local(e
, redir
, state
) && !redir
->dest
.set
&&
284 resolve_dest(e
, redir
, state
))
286 warn_elem(e
, "does not specify a destination, assuming '%s'",
290 if (redir
->reflection
&& redir
->_dest
&& redir
->_src
->masq
)
292 set(redir
->_dest
->flags
, FW3_FAMILY_V4
, FW3_FLAG_ACCEPT
);
293 set(redir
->_dest
->flags
, FW3_FAMILY_V4
, FW3_FLAG_DNAT
);
294 set(redir
->_dest
->flags
, FW3_FAMILY_V4
, FW3_FLAG_SNAT
);
301 warn_elem(e
, "must not have destination '*' for SNAT target");
302 else if (!redir
->_dest
)
303 warn_elem(e
, "has no destination specified");
304 else if (!redir
->ip_dest
.set
)
305 warn_elem(e
, "has no src_dip option specified");
306 else if (!list_empty(&redir
->mac_src
))
307 warn_elem(e
, "must not use 'src_mac' option for SNAT target");
310 set(redir
->_dest
->flags
, FW3_FAMILY_V4
, redir
->target
);
315 if (list_empty(&redir
->proto
))
317 warn_elem(e
, "does not specify a protocol, assuming TCP+UDP");
318 fw3_parse_protocol(&redir
->proto
, "tcpudp", true);
323 fw3_free_redirect(redir
);
327 if (!redir
->port_redir
.set
)
328 redir
->port_redir
= redir
->port_dest
;
330 list_add_tail(&redir
->list
, &state
->redirects
);
335 append_chain_nat(struct fw3_ipt_rule
*r
, struct fw3_redirect
*redir
)
337 if (redir
->target
== FW3_FLAG_DNAT
)
338 fw3_ipt_rule_append(r
, "zone_%s_prerouting", redir
->src
.name
);
340 fw3_ipt_rule_append(r
, "zone_%s_postrouting", redir
->dest
.name
);
344 set_redirect(struct fw3_ipt_rule
*r
, struct fw3_port
*port
)
346 char buf
[sizeof("65535-65535\0")];
348 fw3_ipt_rule_target(r
, "REDIRECT");
350 if (port
&& port
->set
)
352 if (port
->port_min
== port
->port_max
)
353 sprintf(buf
, "%u", port
->port_min
);
355 sprintf(buf
, "%u-%u", port
->port_min
, port
->port_max
);
357 fw3_ipt_rule_addarg(r
, false, "--to-ports", buf
);
362 set_snat_dnat(struct fw3_ipt_rule
*r
, enum fw3_flag target
,
363 struct fw3_address
*addr
, struct fw3_port
*port
)
365 char buf
[sizeof("255.255.255.255:65535-65535\0")];
369 if (addr
&& addr
->set
)
371 inet_ntop(AF_INET
, &addr
->address
.v4
, buf
, sizeof(buf
));
374 if (port
&& port
->set
)
376 if (port
->port_min
== port
->port_max
)
377 sprintf(buf
+ strlen(buf
), ":%u", port
->port_min
);
379 sprintf(buf
+ strlen(buf
), ":%u-%u",
380 port
->port_min
, port
->port_max
);
383 if (target
== FW3_FLAG_DNAT
)
385 fw3_ipt_rule_target(r
, "DNAT");
386 fw3_ipt_rule_addarg(r
, false, "--to-destination", buf
);
390 fw3_ipt_rule_target(r
, "SNAT");
391 fw3_ipt_rule_addarg(r
, false, "--to-source", buf
);
396 set_target_nat(struct fw3_ipt_rule
*r
, struct fw3_redirect
*redir
)
399 set_redirect(r
, &redir
->port_redir
);
400 else if (redir
->target
== FW3_FLAG_DNAT
)
401 set_snat_dnat(r
, redir
->target
, &redir
->ip_redir
, &redir
->port_redir
);
403 set_snat_dnat(r
, redir
->target
, &redir
->ip_dest
, &redir
->port_dest
);
407 set_comment(struct fw3_ipt_rule
*r
, const char *name
, int num
, bool ref
)
412 fw3_ipt_rule_comment(r
, "%s (reflection)", name
);
414 fw3_ipt_rule_comment(r
, name
);
419 fw3_ipt_rule_comment(r
, "@redirect[%u] (reflection)", num
);
421 fw3_ipt_rule_comment(r
, "@redirect[%u]", num
);
426 print_redirect(struct fw3_ipt_handle
*h
, struct fw3_state
*state
,
427 struct fw3_redirect
*redir
, int num
,
428 struct fw3_protocol
*proto
, struct fw3_mac
*mac
)
430 struct fw3_ipt_rule
*r
;
431 struct fw3_address
*src
, *dst
;
432 struct fw3_port
*spt
, *dpt
;
437 src
= &redir
->ip_src
;
438 dst
= &redir
->ip_dest
;
439 spt
= &redir
->port_src
;
440 dpt
= &redir
->port_dest
;
442 if (redir
->target
== FW3_FLAG_SNAT
)
444 dst
= &redir
->ip_redir
;
445 dpt
= &redir
->port_redir
;
448 r
= fw3_ipt_rule_create(h
, proto
, NULL
, NULL
, src
, dst
);
449 fw3_ipt_rule_sport_dport(r
, spt
, dpt
);
450 fw3_ipt_rule_mac(r
, mac
);
451 fw3_ipt_rule_ipset(r
, &redir
->ipset
);
452 fw3_ipt_rule_limit(r
, &redir
->limit
);
453 fw3_ipt_rule_time(r
, &redir
->time
);
454 fw3_ipt_rule_mark(r
, &redir
->mark
);
455 set_target_nat(r
, redir
);
456 fw3_ipt_rule_extra(r
, redir
->extra
);
457 set_comment(r
, redir
->name
, num
, false);
458 append_chain_nat(r
, redir
);
467 print_reflection(struct fw3_ipt_handle
*h
, struct fw3_state
*state
,
468 struct fw3_redirect
*redir
, int num
,
469 struct fw3_protocol
*proto
, struct fw3_address
*ra
,
470 struct fw3_address
*ia
, struct fw3_address
*ea
)
472 struct fw3_ipt_rule
*r
;
477 r
= fw3_ipt_rule_create(h
, proto
, NULL
, NULL
, ia
, ea
);
478 fw3_ipt_rule_sport_dport(r
, NULL
, &redir
->port_dest
);
479 fw3_ipt_rule_limit(r
, &redir
->limit
);
480 fw3_ipt_rule_time(r
, &redir
->time
);
481 set_comment(r
, redir
->name
, num
, true);
482 set_snat_dnat(r
, FW3_FLAG_DNAT
, &redir
->ip_redir
, &redir
->port_redir
);
483 fw3_ipt_rule_replace(r
, "zone_%s_prerouting", redir
->dest
.name
);
485 r
= fw3_ipt_rule_create(h
, proto
, NULL
, NULL
, ia
, &redir
->ip_redir
);
486 fw3_ipt_rule_sport_dport(r
, NULL
, &redir
->port_redir
);
487 fw3_ipt_rule_limit(r
, &redir
->limit
);
488 fw3_ipt_rule_time(r
, &redir
->time
);
489 set_comment(r
, redir
->name
, num
, true);
490 set_snat_dnat(r
, FW3_FLAG_SNAT
, ra
, NULL
);
491 fw3_ipt_rule_replace(r
, "zone_%s_postrouting", redir
->dest
.name
);
500 expand_redirect(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
501 struct fw3_redirect
*redir
, int num
)
503 struct list_head
*ext_addrs
, *int_addrs
;
504 struct fw3_address
*ext_addr
, *int_addr
, ref_addr
;
505 struct fw3_protocol
*proto
;
509 info(" * Redirect '%s'", redir
->name
);
511 info(" * Redirect #%u", num
);
513 if (!fw3_is_family(redir
->_src
, handle
->family
) ||
514 !fw3_is_family(redir
->_dest
, handle
->family
))
516 info(" ! Skipping due to different family of zone");
520 if (!fw3_is_family(&redir
->ip_src
, handle
->family
) ||
521 !fw3_is_family(&redir
->ip_dest
, handle
->family
) ||
522 !fw3_is_family(&redir
->ip_redir
, handle
->family
))
524 if (!redir
->ip_src
.resolved
||
525 !redir
->ip_dest
.resolved
||
526 !redir
->ip_redir
.resolved
)
527 info(" ! Skipping due to different family of ip address");
532 if (redir
->ipset
.ptr
)
534 if (!fw3_is_family(redir
->ipset
.ptr
, handle
->family
))
536 info(" ! Skipping due to different family in ipset");
540 if (!fw3_check_ipset(redir
->ipset
.ptr
))
542 info(" ! Skipping due to missing ipset '%s'",
543 redir
->ipset
.ptr
->external
?
544 redir
->ipset
.ptr
->external
: redir
->ipset
.ptr
->name
);
548 set(redir
->ipset
.ptr
->flags
, handle
->family
, handle
->family
);
551 fw3_foreach(proto
, &redir
->proto
)
552 fw3_foreach(mac
, &redir
->mac_src
)
553 print_redirect(handle
, state
, redir
, num
, proto
, mac
);
555 /* reflection rules */
556 if (redir
->target
!= FW3_FLAG_DNAT
|| !redir
->reflection
|| redir
->local
)
559 if (!redir
->_dest
|| !redir
->_src
->masq
)
562 ext_addrs
= fw3_resolve_zone_addresses(redir
->_src
, &redir
->ip_dest
);
563 int_addrs
= fw3_resolve_zone_addresses(redir
->_dest
, NULL
);
565 if (!ext_addrs
|| !int_addrs
)
568 list_for_each_entry(ext_addr
, ext_addrs
, list
)
570 if (!fw3_is_family(ext_addr
, handle
->family
))
573 list_for_each_entry(int_addr
, int_addrs
, list
)
575 if (!fw3_is_family(int_addr
, handle
->family
))
578 fw3_foreach(proto
, &redir
->proto
)
583 if (redir
->reflection_src
== FW3_REFLECTION_INTERNAL
)
584 ref_addr
= *int_addr
;
586 ref_addr
= *ext_addr
;
588 ref_addr
.mask
.v4
.s_addr
= 0xFFFFFFFF;
589 ext_addr
->mask
.v4
.s_addr
= 0xFFFFFFFF;
591 print_reflection(handle
, state
, redir
, num
, proto
,
592 &ref_addr
, int_addr
, ext_addr
);
598 fw3_free_list(ext_addrs
);
599 fw3_free_list(int_addrs
);
603 fw3_print_redirects(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
)
606 struct fw3_redirect
*redir
;
608 if (handle
->family
== FW3_FAMILY_V6
)
611 if (handle
->table
!= FW3_TABLE_FILTER
&& handle
->table
!= FW3_TABLE_NAT
)
614 list_for_each_entry(redir
, &state
->redirects
, list
)
615 expand_redirect(handle
, state
, redir
, num
++);