X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=redirects.c;h=a30c5406b9fd47f24a2e965c01ce63d2824804a1;hp=080e2c17d49fc1117d1467260ab81cda0eb9b8d6;hb=b59934331c4b9271ceb5e30b793a552618299d39;hpb=92281eb747b56e748b7c3d754055919c23befdd4 diff --git a/redirects.c b/redirects.c index 080e2c1..a30c540 100644 --- a/redirects.c +++ b/redirects.c @@ -116,14 +116,11 @@ check_families(struct uci_element *e, struct fw3_redirect *r) static bool compare_addr(struct fw3_address *a, struct fw3_address *b) { - uint32_t mask; - if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4) return false; - mask = htonl(~((1 << (32 - a->mask)) - 1)); - - return ((a->address.v4.s_addr & mask) == (b->address.v4.s_addr & mask)); + return ((a->address.v4.s_addr & a->mask.v4.s_addr) == + (b->address.v4.s_addr & a->mask.v4.s_addr)); } static bool @@ -139,7 +136,7 @@ resolve_dest(struct uci_element *e, struct fw3_redirect *redir, list_for_each_entry(zone, &state->zones, list) { - addrs = fw3_resolve_zone_addresses(zone); + addrs = fw3_resolve_zone_addresses(zone, NULL); if (!addrs) continue; @@ -191,12 +188,11 @@ check_local(struct uci_element *e, struct fw3_redirect *redir, fw3_ubus_address(&addrs, net->name); list_for_each_entry_safe(addr, tmp, &addrs, list) { - if (compare_addr(&redir->ip_redir, addr)) { + if (!redir->local && compare_addr(&redir->ip_redir, addr)) { warn_elem(e, "refers to a destination address on this router, " "assuming port redirection"); redir->local = true; - continue; } list_del(&addr->list); @@ -379,6 +375,24 @@ append_chain_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir) } static void +set_redirect(struct fw3_ipt_rule *r, struct fw3_port *port) +{ + char buf[sizeof("65535-65535\0")]; + + fw3_ipt_rule_target(r, "REDIRECT"); + + if (port && port->set) + { + if (port->port_min == port->port_max) + sprintf(buf, "%u", port->port_min); + else + sprintf(buf, "%u-%u", port->port_min, port->port_max); + + fw3_ipt_rule_addarg(r, false, "--to-ports", buf); + } +} + +static void set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target, struct fw3_address *addr, struct fw3_port *port) { @@ -415,7 +429,9 @@ set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target, static void set_target_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir) { - if (redir->target == FW3_FLAG_DNAT) + if (redir->local) + set_redirect(r, &redir->port_redir); + else if (redir->target == FW3_FLAG_DNAT) set_snat_dnat(r, redir->target, &redir->ip_redir, &redir->port_redir); else set_snat_dnat(r, redir->target, &redir->ip_dest, &redir->port_dest); @@ -571,14 +587,14 @@ expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state, print_redirect(handle, state, redir, num, proto, mac); /* reflection rules */ - if (redir->target != FW3_FLAG_DNAT || !redir->reflection) + if (redir->target != FW3_FLAG_DNAT || !redir->reflection || redir->local) return; if (!redir->_dest || !redir->_src->masq) return; - ext_addrs = fw3_resolve_zone_addresses(redir->_src); - int_addrs = fw3_resolve_zone_addresses(redir->_dest); + ext_addrs = fw3_resolve_zone_addresses(redir->_src, &redir->ip_dest); + int_addrs = fw3_resolve_zone_addresses(redir->_dest, NULL); if (!ext_addrs || !int_addrs) goto out; @@ -603,8 +619,8 @@ expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state, else ref_addr = *ext_addr; - ref_addr.mask = 32; - ext_addr->mask = 32; + ref_addr.mask.v4.s_addr = 0xFFFFFFFF; + ext_addr->mask.v4.s_addr = 0xFFFFFFFF; print_reflection(handle, state, redir, num, proto, &ref_addr, int_addr, ext_addr);