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.
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)
121 if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4)
124 mask = htonl(~((1 << (32 - a->mask)) - 1));
126 return ((a->address.v4.s_addr & mask) == (b->address.v4.s_addr & mask));
130 resolve_dest(struct uci_element *e, struct fw3_redirect *redir,
131 struct fw3_state *state)
133 struct fw3_zone *zone;
134 struct fw3_address *addr;
135 struct list_head *addrs;
137 if (!redir->ip_redir.set)
140 list_for_each_entry(zone, &state->zones, list)
142 addrs = fw3_resolve_zone_addresses(zone);
147 list_for_each_entry(addr, addrs, list)
149 if (!compare_addr(addr, &redir->ip_redir))
152 strncpy(redir->dest.name, zone->name, sizeof(redir->dest.name));
153 redir->dest.set = true;
159 fw3_free_list(addrs);
169 check_local(struct uci_element *e, struct fw3_redirect *redir,
170 struct fw3_state *state)
172 struct fw3_zone *zone;
173 struct fw3_device *net;
174 struct fw3_address *addr;
175 struct list_head *addrs;
177 if (redir->target != FW3_FLAG_DNAT)
180 if (!redir->ip_redir.set)
186 list_for_each_entry(zone, &state->zones, list)
188 list_for_each_entry(net, &zone->networks, list)
190 addrs = fw3_ubus_address(net->name);
195 list_for_each_entry(addr, addrs, list)
197 if (!compare_addr(&redir->ip_redir, addr))
200 warn_elem(e, "refers to a destination address on this router, "
201 "assuming port redirection");
207 fw3_free_list(addrs);
218 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
220 struct uci_section *s;
221 struct uci_element *e;
222 struct fw3_redirect *redir;
226 INIT_LIST_HEAD(&state->redirects);
228 uci_foreach_element(&p->sections, e)
230 s = uci_to_section(e);
232 if (strcmp(s->type, "redirect"))
235 redir = calloc(1, sizeof(*redir));
239 INIT_LIST_HEAD(&redir->proto);
240 INIT_LIST_HEAD(&redir->mac_src);
242 redir->enabled = true;
243 redir->reflection = true;
247 if (!fw3_parse_options(redir, fw3_redirect_opts, s))
249 warn_elem(e, "skipped due to invalid options");
250 fw3_free_redirect(redir);
256 fw3_free_redirect(redir);
260 if (redir->src.invert)
262 warn_elem(e, "must not have an inverted source");
263 fw3_free_redirect(redir);
266 else if (redir->src.set && !redir->src.any &&
267 !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
269 warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
270 fw3_free_redirect(redir);
273 else if (redir->dest.set && !redir->dest.any &&
274 !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
276 warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
277 fw3_free_redirect(redir);
280 else if (redir->ipset.set && state->disable_ipsets)
282 warn_elem(e, "skipped due to disabled ipset support");
283 fw3_free_redirect(redir);
286 else if (redir->ipset.set &&
287 !(redir->ipset.ptr = fw3_lookup_ipset(state, redir->ipset.name)))
289 warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
290 fw3_free_redirect(redir);
294 if (!check_families(e, redir))
296 fw3_free_redirect(redir);
300 if (redir->target == FW3_FLAG_UNSPEC)
302 warn_elem(e, "has no target specified, defaulting to DNAT");
303 redir->target = FW3_FLAG_DNAT;
305 else if (redir->target < FW3_FLAG_DNAT || redir->target > FW3_FLAG_SNAT)
307 warn_elem(e, "has invalid target specified, defaulting to DNAT");
308 redir->target = FW3_FLAG_DNAT;
311 if (redir->target == FW3_FLAG_DNAT)
314 warn_elem(e, "must not have source '*' for DNAT target");
315 else if (!redir->_src)
316 warn_elem(e, "has no source specified");
319 set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
320 redir->_src->conntrack = true;
324 if (!check_local(e, redir, state) && !redir->dest.set &&
325 resolve_dest(e, redir, state))
327 warn_elem(e, "does not specify a destination, assuming '%s'",
331 if (redir->reflection && redir->_dest && redir->_src->masq)
333 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
334 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
335 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
341 warn_elem(e, "must not have destination '*' for SNAT target");
342 else if (!redir->_dest)
343 warn_elem(e, "has no destination specified");
344 else if (!redir->ip_dest.set)
345 warn_elem(e, "has no src_dip option specified");
346 else if (!list_empty(&redir->mac_src))
347 warn_elem(e, "must not use 'src_mac' option for SNAT target");
350 set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
351 redir->_dest->conntrack = true;
356 if (list_empty(&redir->proto))
358 warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
359 fw3_parse_protocol(&redir->proto, "tcpudp", true);
364 fw3_free_redirect(redir);
368 if (!redir->port_redir.set)
369 redir->port_redir = redir->port_dest;
371 list_add_tail(&redir->list, &state->redirects);
376 append_chain_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
378 if (redir->target == FW3_FLAG_DNAT)
379 fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->src.name);
381 fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
385 set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target,
386 struct fw3_address *addr, struct fw3_port *port)
388 char buf[sizeof("255.255.255.255:65535-65535\0")];
392 if (addr && addr->set)
394 inet_ntop(AF_INET, &addr->address.v4, buf, sizeof(buf));
397 if (port && port->set)
399 if (port->port_min == port->port_max)
400 sprintf(buf + strlen(buf), ":%u", port->port_min);
402 sprintf(buf + strlen(buf), ":%u-%u",
403 port->port_min, port->port_max);
406 if (target == FW3_FLAG_DNAT)
408 fw3_ipt_rule_target(r, "DNAT");
409 fw3_ipt_rule_addarg(r, false, "--to-destination", buf);
413 fw3_ipt_rule_target(r, "SNAT");
414 fw3_ipt_rule_addarg(r, false, "--to-source", buf);
419 set_target_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
421 if (redir->target == FW3_FLAG_DNAT)
422 set_snat_dnat(r, redir->target, &redir->ip_redir, &redir->port_redir);
424 set_snat_dnat(r, redir->target, &redir->ip_dest, &redir->port_dest);
428 set_comment(struct fw3_ipt_rule *r, const char *name, int num, bool ref)
433 fw3_ipt_rule_comment(r, "%s (reflection)", name);
435 fw3_ipt_rule_comment(r, name);
440 fw3_ipt_rule_comment(r, "@redirect[%u] (reflection)", num);
442 fw3_ipt_rule_comment(r, "@redirect[%u]", num);
447 print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
448 struct fw3_redirect *redir, int num,
449 struct fw3_protocol *proto, struct fw3_mac *mac)
451 struct fw3_ipt_rule *r;
452 struct fw3_address *src, *dst;
453 struct fw3_port *spt, *dpt;
458 src = &redir->ip_src;
459 dst = &redir->ip_dest;
460 spt = &redir->port_src;
461 dpt = &redir->port_dest;
463 if (redir->target == FW3_FLAG_SNAT)
465 dst = &redir->ip_redir;
466 dpt = &redir->port_redir;
469 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
470 fw3_ipt_rule_sport_dport(r, spt, dpt);
471 fw3_ipt_rule_mac(r, mac);
472 fw3_ipt_rule_ipset(r, &redir->ipset);
473 fw3_ipt_rule_limit(r, &redir->limit);
474 fw3_ipt_rule_time(r, &redir->time);
475 fw3_ipt_rule_mark(r, &redir->mark);
476 set_target_nat(r, redir);
477 fw3_ipt_rule_extra(r, redir->extra);
478 set_comment(r, redir->name, num, false);
479 append_chain_nat(r, redir);
488 print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
489 struct fw3_redirect *redir, int num,
490 struct fw3_protocol *proto, struct fw3_address *ra,
491 struct fw3_address *ia, struct fw3_address *ea)
493 struct fw3_ipt_rule *r;
498 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
499 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
500 fw3_ipt_rule_limit(r, &redir->limit);
501 fw3_ipt_rule_time(r, &redir->time);
502 set_comment(r, redir->name, num, true);
503 set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
504 fw3_ipt_rule_replace(r, "zone_%s_prerouting", redir->dest.name);
506 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
507 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
508 fw3_ipt_rule_limit(r, &redir->limit);
509 fw3_ipt_rule_time(r, &redir->time);
510 set_comment(r, redir->name, num, true);
511 set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
512 fw3_ipt_rule_replace(r, "zone_%s_postrouting", redir->dest.name);
521 expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
522 struct fw3_redirect *redir, int num)
524 struct list_head *ext_addrs, *int_addrs;
525 struct fw3_address *ext_addr, *int_addr, ref_addr;
526 struct fw3_protocol *proto;
530 info(" * Redirect '%s'", redir->name);
532 info(" * Redirect #%u", num);
534 if (!fw3_is_family(redir->_src, handle->family) ||
535 !fw3_is_family(redir->_dest, handle->family))
537 info(" ! Skipping due to different family of zone");
541 if (!fw3_is_family(&redir->ip_src, handle->family) ||
542 !fw3_is_family(&redir->ip_dest, handle->family) ||
543 !fw3_is_family(&redir->ip_redir, handle->family))
545 if (!redir->ip_src.resolved ||
546 !redir->ip_dest.resolved ||
547 !redir->ip_redir.resolved)
548 info(" ! Skipping due to different family of ip address");
553 if (redir->ipset.ptr)
555 if (!fw3_is_family(redir->ipset.ptr, handle->family))
557 info(" ! Skipping due to different family in ipset");
561 if (!fw3_check_ipset(redir->ipset.ptr))
563 info(" ! Skipping due to missing ipset '%s'",
564 redir->ipset.ptr->external ?
565 redir->ipset.ptr->external : redir->ipset.ptr->name);
569 set(redir->ipset.ptr->flags, handle->family, handle->family);
572 fw3_foreach(proto, &redir->proto)
573 fw3_foreach(mac, &redir->mac_src)
574 print_redirect(handle, state, redir, num, proto, mac);
576 /* reflection rules */
577 if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
580 if (!redir->_dest || !redir->_src->masq)
583 ext_addrs = fw3_resolve_zone_addresses(redir->_src);
584 int_addrs = fw3_resolve_zone_addresses(redir->_dest);
586 if (!ext_addrs || !int_addrs)
589 list_for_each_entry(ext_addr, ext_addrs, list)
591 if (!fw3_is_family(ext_addr, handle->family))
594 list_for_each_entry(int_addr, int_addrs, list)
596 if (!fw3_is_family(int_addr, handle->family))
599 fw3_foreach(proto, &redir->proto)
604 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
605 ref_addr = *int_addr;
607 ref_addr = *ext_addr;
612 print_reflection(handle, state, redir, num, proto,
613 &ref_addr, int_addr, ext_addr);
619 fw3_free_list(ext_addrs);
620 fw3_free_list(int_addrs);
624 fw3_print_redirects(struct fw3_ipt_handle *handle, struct fw3_state *state)
627 struct fw3_redirect *redir;
629 if (handle->family == FW3_FAMILY_V6)
632 if (handle->table != FW3_TABLE_FILTER && handle->table != FW3_TABLE_NAT)
635 list_for_each_entry(redir, &state->redirects, list)
636 expand_redirect(handle, state, redir, num++);