redirects: respect src_dip option for reflection rules
[project/firewall3.git] / redirects.c
index 7bafec2..a30c540 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * firewall3 - 3rd OpenWrt UCI firewall implementation
  *
- *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *   Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -28,7 +28,7 @@ const struct fw3_option fw3_redirect_opts[] = {
        FW3_OPT("src",                 device,    redirect,     src),
        FW3_OPT("dest",                device,    redirect,     dest),
 
-       FW3_OPT("ipset",               device,    redirect,     ipset),
+       FW3_OPT("ipset",               setmatch,  redirect,     ipset),
 
        FW3_LIST("proto",              protocol,  redirect,     proto),
 
@@ -44,6 +44,9 @@ const struct fw3_option fw3_redirect_opts[] = {
 
        FW3_OPT("extra",               string,    redirect,     extra),
 
+       FW3_OPT("limit",               limit,     redirect,     limit),
+       FW3_OPT("limit_burst",         int,       redirect,     limit.burst),
+
        FW3_OPT("utc_time",            bool,      redirect,     time.utc),
        FW3_OPT("start_date",          date,      redirect,     time.datestart),
        FW3_OPT("stop_date",           date,      redirect,     time.datestop),
@@ -82,7 +85,8 @@ check_families(struct uci_element *e, struct fw3_redirect *r)
                return false;
        }
 
-       if (r->_ipset && r->_ipset->family && r->_ipset->family != r->family)
+       if (r->ipset.ptr && r->ipset.ptr->family &&
+           r->ipset.ptr->family != r->family)
        {
                warn_elem(e, "refers to ipset with different family");
                return false;
@@ -112,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)
+       if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4)
                return false;
 
-       mask = ~((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
@@ -135,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;
@@ -161,6 +162,51 @@ resolve_dest(struct uci_element *e, struct fw3_redirect *redir,
        return false;
 }
 
+static bool
+check_local(struct uci_element *e, struct fw3_redirect *redir,
+            struct fw3_state *state)
+{
+       struct fw3_zone *zone;
+       struct fw3_device *net;
+       struct fw3_address *addr, *tmp;
+
+       if (redir->target != FW3_FLAG_DNAT)
+               return false;
+
+       if (!redir->ip_redir.set)
+               redir->local = true;
+
+       if (redir->local)
+               return true;
+
+       list_for_each_entry(zone, &state->zones, list)
+       {
+               list_for_each_entry(net, &zone->networks, list)
+               {
+                       LIST_HEAD(addrs);
+
+                       fw3_ubus_address(&addrs, net->name);
+                       list_for_each_entry_safe(addr, tmp, &addrs, list)
+                       {
+                               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;
+                               }
+
+                               list_del(&addr->list);
+                               free(addr);
+                       }
+
+                       if (redir->local)
+                               return true;
+               }
+       }
+
+       return false;
+}
+
 void
 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
 {
@@ -179,13 +225,10 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                if (strcmp(s->type, "redirect"))
                        continue;
 
-               redir = malloc(sizeof(*redir));
-
+               redir = calloc(1, sizeof(*redir));
                if (!redir)
                        continue;
 
-               memset(redir, 0, sizeof(*redir));
-
                INIT_LIST_HEAD(&redir->proto);
                INIT_LIST_HEAD(&redir->mac_src);
 
@@ -194,7 +237,12 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
 
                valid = false;
 
-               fw3_parse_options(redir, fw3_redirect_opts, s);
+               if (!fw3_parse_options(redir, fw3_redirect_opts, s))
+               {
+                       warn_elem(e, "skipped due to invalid options");
+                       fw3_free_redirect(redir);
+                       continue;
+               }
 
                if (!redir->enabled)
                {
@@ -228,8 +276,8 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                        fw3_free_redirect(redir);
                        continue;
                }
-               else if (redir->ipset.set && !redir->ipset.any &&
-                        !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name)))
+               else if (redir->ipset.set &&
+                        !(redir->ipset.ptr = fw3_lookup_ipset(state, redir->ipset.name)))
                {
                        warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
                        fw3_free_redirect(redir);
@@ -247,7 +295,7 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                        warn_elem(e, "has no target specified, defaulting to DNAT");
                        redir->target = FW3_FLAG_DNAT;
                }
-               else if (redir->target < FW3_FLAG_DNAT)
+               else if (redir->target < FW3_FLAG_DNAT || redir->target > FW3_FLAG_SNAT)
                {
                        warn_elem(e, "has invalid target specified, defaulting to DNAT");
                        redir->target = FW3_FLAG_DNAT;
@@ -266,7 +314,8 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                                valid = true;
                        }
 
-                       if (!redir->dest.set && resolve_dest(e, redir, state))
+                       if (!check_local(e, redir, state) && !redir->dest.set &&
+                           resolve_dest(e, redir, state))
                        {
                                warn_elem(e, "does not specify a destination, assuming '%s'",
                                          redir->dest.name);
@@ -326,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)
 {
@@ -362,43 +429,15 @@ 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);
 }
 
 static void
-append_chain_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
-{
-       if (redir->target == FW3_FLAG_DNAT)
-       {
-               /* XXX: check for local ip */
-               if (!redir->ip_redir.set)
-                       fw3_ipt_rule_append(r, "zone_%s_input", redir->src.name);
-               else
-                       fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
-       }
-       else
-       {
-               if (redir->src.set && !redir->src.any)
-                       fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
-               else
-                       fw3_ipt_rule_append(r, "delegate_forward");
-       }
-}
-
-static void
-set_target_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
-{
-       /* XXX: check for local ip */
-       if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
-               fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
-
-       fw3_ipt_rule_target(r, "ACCEPT");
-}
-
-static void
 set_comment(struct fw3_ipt_rule *r, const char *name, int num, bool ref)
 {
        if (name)
@@ -443,7 +482,8 @@ print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
                r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
                fw3_ipt_rule_sport_dport(r, spt, dpt);
                fw3_ipt_rule_mac(r, mac);
-               fw3_ipt_rule_ipset(r, redir->_ipset, redir->ipset.invert);
+               fw3_ipt_rule_ipset(r, &redir->ipset);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                fw3_ipt_rule_mark(r, &redir->mark);
                set_target_nat(r, redir);
@@ -452,24 +492,6 @@ print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
                append_chain_nat(r, redir);
                break;
 
-       case FW3_TABLE_FILTER:
-               src = &redir->ip_src;
-               dst = &redir->ip_redir;
-               spt = &redir->port_src;
-               dpt = &redir->port_redir;
-
-               r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
-               fw3_ipt_rule_sport_dport(r, spt, dpt);
-               fw3_ipt_rule_mac(r, mac);
-               fw3_ipt_rule_ipset(r, redir->_ipset, redir->ipset.invert);
-               fw3_ipt_rule_time(r, &redir->time);
-               fw3_ipt_rule_mark(r, &redir->mark);
-               set_target_filter(r, redir);
-               fw3_ipt_rule_extra(r, redir->extra);
-               set_comment(r, redir->name, num, false);
-               append_chain_filter(r, redir);
-               break;
-
        default:
                break;
        }
@@ -488,26 +510,19 @@ print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
        case FW3_TABLE_NAT:
                r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
                fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                set_comment(r, redir->name, num, true);
                set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
-               fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->dest.name);
+               fw3_ipt_rule_replace(r, "zone_%s_prerouting", redir->dest.name);
 
                r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
                fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                set_comment(r, redir->name, num, true);
                set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
-               fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
-               break;
-
-       case FW3_TABLE_FILTER:
-               r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
-               fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
-               fw3_ipt_rule_time(r, &redir->time);
-               set_comment(r, redir->name, num, true);
-               fw3_ipt_rule_target(r, "zone_%s_dest_ACCEPT", redir->dest.name);
-               fw3_ipt_rule_append(r, "zone_%s_forward", redir->dest.name);
+               fw3_ipt_rule_replace(r, "zone_%s_postrouting", redir->dest.name);
                break;
 
        default:
@@ -540,27 +555,31 @@ expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
            !fw3_is_family(&redir->ip_dest, handle->family) ||
                !fw3_is_family(&redir->ip_redir, handle->family))
        {
-               info("     ! Skipping due to different family of ip address");
+               if (!redir->ip_src.resolved ||
+                   !redir->ip_dest.resolved ||
+                   !redir->ip_redir.resolved)
+                       info("     ! Skipping due to different family of ip address");
+
                return;
        }
 
-       if (redir->_ipset)
+       if (redir->ipset.ptr)
        {
-               if (!fw3_is_family(redir->_ipset, handle->family))
+               if (!fw3_is_family(redir->ipset.ptr, handle->family))
                {
                        info("     ! Skipping due to different family in ipset");
                        return;
                }
 
-               if (!fw3_check_ipset(redir->_ipset))
+               if (!fw3_check_ipset(redir->ipset.ptr))
                {
                        info("     ! Skipping due to missing ipset '%s'",
-                            redir->_ipset->external ?
-                                       redir->_ipset->external : redir->_ipset->name);
+                            redir->ipset.ptr->external ?
+                                       redir->ipset.ptr->external : redir->ipset.ptr->name);
                        return;
                }
 
-               set(redir->_ipset->flags, handle->family, handle->family);
+               set(redir->ipset.ptr->flags, handle->family, handle->family);
        }
 
        fw3_foreach(proto, &redir->proto)
@@ -568,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;
@@ -592,7 +611,7 @@ expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
 
                        fw3_foreach(proto, &redir->proto)
                        {
-                               if (!proto || (proto->protocol != 6 && proto->protocol != 17))
+                               if (!proto)
                                        continue;
 
                                if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
@@ -600,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);