firewall3: add UBUS support for redirect sections
[project/firewall3.git] / redirects.c
index f1fa1f4..e651ddd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * firewall3 - 3rd OpenWrt UCI firewall implementation
  *
- *   Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
+ *   Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -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;
@@ -169,206 +166,210 @@ 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;
-       struct list_head *addrs;
-
        if (redir->target != FW3_FLAG_DNAT)
                return false;
 
        if (!redir->ip_redir.set)
                redir->local = true;
 
-       if (redir->local)
-               return true;
+       return redir->local;
+}
 
-       list_for_each_entry(zone, &state->zones, list)
+static bool
+check_redirect(struct fw3_state *state, struct fw3_redirect *redir, struct uci_element *e)
+{
+       bool valid;
+
+       if (!redir->enabled)
+               return false;
+
+       if (redir->src.invert)
        {
-               list_for_each_entry(net, &zone->networks, list)
-               {
-                       addrs = fw3_ubus_address(net->name);
+               warn_section("redirect", redir, e, "must not have an inverted source");
+               return false;
+       }
+       else if (redir->src.set && !redir->src.any &&
+                       !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
+       {
+               warn_section("redirect", redir, e, "refers to not existing zone '%s'",
+                               redir->src.name);
+               return false;
+       }
+       else if (redir->dest.set && !redir->dest.any &&
+                       !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
+       {
+               warn_section("redirect", redir, e, "refers to not existing zone '%s'",
+                               redir->dest.name);
+               return false;
+       }
+       else if (redir->ipset.set && state->disable_ipsets)
+       {
+               warn_section("redirect", redir, e, "skipped due to disabled ipset support",
+                               redir->name);
+               return false;
+       }
+       else if (redir->ipset.set &&
+                       !(redir->ipset.ptr = fw3_lookup_ipset(state, redir->ipset.name)))
+       {
+               warn_section("redirect", redir, e, "refers to unknown ipset '%s'", redir->name,
+                               redir->ipset.name);
+               return false;
+       }
 
-                       if (!addrs)
-                               continue;
+       if (!check_families(e, redir))
+               return false;
 
-                       list_for_each_entry(addr, addrs, list)
-                       {
-                               if (!compare_addr(&redir->ip_redir, addr))
-                                       continue;
+       if (redir->target == FW3_FLAG_UNSPEC)
+       {
+               warn_section("redirect", redir, e, "has no target specified, defaulting to DNAT");
+               redir->target = FW3_FLAG_DNAT;
+       }
+       else if (redir->target < FW3_FLAG_DNAT || redir->target > FW3_FLAG_SNAT)
+       {
+               warn_section("redirect", redir, e, "has invalid target specified, defaulting to DNAT");
+               redir->target = FW3_FLAG_DNAT;
+       }
 
-                               warn_elem(e, "refers to a destination address on this router, "
-                                            "assuming port redirection");
+       valid = false;
 
-                               redir->local = true;
-                               break;
-                       }
+       if (redir->target == FW3_FLAG_DNAT)
+       {
+               if (redir->src.any)
+                       warn_section("redirect", redir, e, "must not have source '*' for DNAT target");
+               else if (!redir->_src)
+                       warn_section("redirect", redir, e, "has no source specified");
+               else
+               {
+                       set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
+                       valid = true;
 
-                       fw3_free_list(addrs);
+                       if (!check_local(e, redir, state) && !redir->dest.set &&
+                                       resolve_dest(e, redir, state))
+                       {
+                               warn_section("redirect", redir, e,
+                                               "does not specify a destination, assuming '%s'",
+                                               redir->dest.name);
+                       }
 
-                       if (redir->local)
-                               return true;
+                       if (redir->reflection && redir->_dest && redir->_src->masq)
+                       {
+                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
+                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
+                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
+                       }
+               }
+       }
+       else
+       {
+               if (redir->dest.any)
+                       warn_section("redirect", redir, e,
+                                       "must not have destination '*' for SNAT target");
+               else if (!redir->_dest)
+                       warn_section("redirect", redir, e, "has no destination specified");
+               else if (!redir->ip_dest.set)
+                       warn_section("redirect", redir, e, "has no src_dip option specified");
+               else if (!list_empty(&redir->mac_src))
+                       warn_section("redirect", redir, e, "must not use 'src_mac' option for SNAT target");
+               else
+               {
+                       set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
+                       valid = true;
                }
        }
 
-       return false;
+       if (list_empty(&redir->proto))
+       {
+               warn_section("redirect", redir, e, "does not specify a protocol, assuming TCP+UDP");
+               fw3_parse_protocol(&redir->proto, "tcpudp", true);
+       }
+
+       if (!valid)
+               return false;
+
+       if (!redir->port_redir.set)
+               redir->port_redir = redir->port_dest;
+
+       return true;
 }
 
-void
-fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
+static struct fw3_redirect *
+fw3_alloc_redirect(struct fw3_state *state)
 {
-       struct uci_section *s;
-       struct uci_element *e;
        struct fw3_redirect *redir;
 
-       bool valid;
+       redir = calloc(1, sizeof(*redir));
+       if (!redir)
+               return NULL;
 
-       INIT_LIST_HEAD(&state->redirects);
+       INIT_LIST_HEAD(&redir->proto);
+       INIT_LIST_HEAD(&redir->mac_src);
 
-       uci_foreach_element(&p->sections, e)
-       {
-               s = uci_to_section(e);
+       redir->enabled = true;
+       redir->reflection = true;
 
-               if (strcmp(s->type, "redirect"))
-                       continue;
+       list_add_tail(&redir->list, &state->redirects);
 
-               redir = calloc(1, sizeof(*redir));
-               if (!redir)
-                       continue;
+       return redir;
+}
 
-               INIT_LIST_HEAD(&redir->proto);
-               INIT_LIST_HEAD(&redir->mac_src);
+void
+fw3_load_redirects(struct fw3_state *state, struct uci_package *p,
+               struct blob_attr *a)
+{
+       struct uci_section *s;
+       struct uci_element *e;
+       struct fw3_redirect *redir;
+       struct blob_attr *entry;
+       unsigned rem;
 
-               redir->enabled = true;
-               redir->reflection = true;
+       INIT_LIST_HEAD(&state->redirects);
 
-               valid = false;
+       blob_for_each_attr(entry, a, rem)
+       {
+               const char *type;
+               const char *name = "ubus redirect";
 
-               if (!fw3_parse_options(redir, fw3_redirect_opts, s))
-               {
-                       warn_elem(e, "skipped due to invalid options");
-                       fw3_free_redirect(redir);
+               if (!fw3_attr_parse_name_type(entry, &name, &type))
                        continue;
-               }
 
-               if (!redir->enabled)
-               {
-                       fw3_free_redirect(redir);
+               if (strcmp(type, "redirect"))
                        continue;
-               }
 
-               if (redir->src.invert)
-               {
-                       warn_elem(e, "must not have an inverted source");
-                       fw3_free_redirect(redir);
-                       continue;
-               }
-               else if (redir->src.set && !redir->src.any &&
-                        !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
-               {
-                       warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
-                       fw3_free_redirect(redir);
-                       continue;
-               }
-               else if (redir->dest.set && !redir->dest.any &&
-                        !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
-               {
-                       warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
-                       fw3_free_redirect(redir);
-                       continue;
-               }
-               else if (redir->ipset.set && state->disable_ipsets)
-               {
-                       warn_elem(e, "skipped due to disabled ipset support");
-                       fw3_free_redirect(redir);
-                       continue;
-               }
-               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);
+               redir = fw3_alloc_redirect(state);
+               if (!redir)
                        continue;
-               }
 
-               if (!check_families(e, redir))
+               if (!fw3_parse_blob_options(redir, fw3_redirect_opts, entry, name))
                {
+                       warn_section("redirect", redir, NULL, "skipped due to invalid options");
                        fw3_free_redirect(redir);
                        continue;
                }
 
-               if (redir->target == FW3_FLAG_UNSPEC)
-               {
-                       warn_elem(e, "has no target specified, defaulting to DNAT");
-                       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;
-               }
-
-               if (redir->target == FW3_FLAG_DNAT)
-               {
-                       if (redir->src.any)
-                               warn_elem(e, "must not have source '*' for DNAT target");
-                       else if (!redir->_src)
-                               warn_elem(e, "has no source specified");
-                       else
-                       {
-                               set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
-                               redir->_src->conntrack = true;
-                               valid = true;
-                       }
+               if (!check_redirect(state, redir, NULL))
+                       fw3_free_redirect(redir);
+       }
 
-                       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);
-                       }
+       uci_foreach_element(&p->sections, e)
+       {
+               s = uci_to_section(e);
 
-                       if (redir->reflection && redir->_dest && redir->_src->masq)
-                       {
-                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
-                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
-                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
-                       }
-               }
-               else
-               {
-                       if (redir->dest.any)
-                               warn_elem(e, "must not have destination '*' for SNAT target");
-                       else if (!redir->_dest)
-                               warn_elem(e, "has no destination specified");
-                       else if (!redir->ip_dest.set)
-                               warn_elem(e, "has no src_dip option specified");
-                       else if (!list_empty(&redir->mac_src))
-                               warn_elem(e, "must not use 'src_mac' option for SNAT target");
-                       else
-                       {
-                               set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
-                               redir->_dest->conntrack = true;
-                               valid = true;
-                       }
-               }
+               if (strcmp(s->type, "redirect"))
+                       continue;
 
-               if (list_empty(&redir->proto))
-               {
-                       warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
-                       fw3_parse_protocol(&redir->proto, "tcpudp", true);
-               }
+               redir = fw3_alloc_redirect(state);
+               if (!redir)
+                       continue;
 
-               if (!valid)
+               if (!fw3_parse_options(redir, fw3_redirect_opts, s))
                {
+                       warn_elem(e, "skipped due to invalid options");
                        fw3_free_redirect(redir);
                        continue;
                }
 
-               if (!redir->port_redir.set)
-                       redir->port_redir = redir->port_dest;
-
-               list_add_tail(&redir->list, &state->redirects);
+               if (!check_redirect(state, redir, e))
+                       fw3_free_redirect(redir);
        }
 }
 
@@ -382,6 +383,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)
 {
@@ -418,7 +437,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);
@@ -574,14 +595,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;
@@ -606,8 +627,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);