Initial support for "config nat" rules - this allows configuring zone-independant...
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 6 Apr 2014 20:25:14 +0000 (22:25 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 6 Apr 2014 20:51:30 +0000 (22:51 +0200)
CMakeLists.txt
main.c
options.c
options.h
redirects.c
snats.c [new file with mode: 0644]
snats.h [new file with mode: 0644]

index 4a85e0741212a399b2ba623f3fecdc1ef5517c5f..a57cb7b18983a0a9ca4d0bc1ea42f261be54ea94 100644 (file)
@@ -37,7 +37,7 @@ ELSE()
   ADD_DEFINITIONS(-DDISABLE_IPV6)
 ENDIF()
 
-ADD_EXECUTABLE(firewall3 main.c options.c defaults.c zones.c forwards.c rules.c redirects.c utils.c ubus.c ipsets.c includes.c iptables.c)
+ADD_EXECUTABLE(firewall3 main.c options.c defaults.c zones.c forwards.c rules.c redirects.c snats.c utils.c ubus.c ipsets.c includes.c iptables.c)
 TARGET_LINK_LIBRARIES(firewall3 uci ubox ubus xtables m dl ${iptc_libs} ${ext_libs})
 
 SET(CMAKE_INSTALL_PREFIX /usr)
diff --git a/main.c b/main.c
index 32ff87dbf1d42d2a54b02b2f3b26f18e23f595f2..c0180b2e8ba9b79b69caacd7515071af617c5734 100644 (file)
--- a/main.c
+++ b/main.c
@@ -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
@@ -24,6 +24,7 @@
 #include "zones.h"
 #include "rules.h"
 #include "redirects.h"
+#include "snats.h"
 #include "forwards.h"
 #include "ipsets.h"
 #include "includes.h"
@@ -102,6 +103,7 @@ build_state(bool runtime)
        fw3_load_zones(state, p);
        fw3_load_rules(state, p);
        fw3_load_redirects(state, p);
+       fw3_load_snats(state, p);
        fw3_load_forwards(state, p);
        fw3_load_includes(state, p);
 
@@ -122,6 +124,9 @@ free_state(struct fw3_state *state)
        list_for_each_safe(cur, tmp, &state->redirects)
                fw3_free_redirect((struct fw3_redirect *)cur);
 
+       list_for_each_safe(cur, tmp, &state->snats)
+               fw3_free_snat((struct fw3_snat *)cur);
+
        list_for_each_safe(cur, tmp, &state->forwards)
                fw3_free_forward((struct fw3_forward *)cur);
 
@@ -275,6 +280,7 @@ start(void)
                        fw3_print_default_head_rules(handle, cfg_state, false);
                        fw3_print_rules(handle, cfg_state);
                        fw3_print_redirects(handle, cfg_state);
+                       fw3_print_snats(handle, cfg_state);
                        fw3_print_forwards(handle, cfg_state);
                        fw3_print_zone_rules(handle, cfg_state, false);
                        fw3_print_default_tail_rules(handle, cfg_state, false);
index 7e62ca694f0eb141cc7c43cc5c1e9fa1fa7db38d..4c42be023371585e410235eee41ea72c1ffac11b 100644 (file)
--- a/options.c
+++ b/options.c
@@ -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
@@ -78,6 +78,7 @@ const char *fw3_flag_names[__FW3_FLAG_MAX] = {
        "MARK",
        "DNAT",
        "SNAT",
+       "MASQUERADE",
 
        "ACCEPT",
        "REJECT",
@@ -164,7 +165,7 @@ bool
 fw3_parse_target(void *ptr, const char *val, bool is_list)
 {
        return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
-                         FW3_FLAG_ACCEPT, FW3_FLAG_SNAT);
+                         FW3_FLAG_ACCEPT, FW3_FLAG_MASQUERADE);
 }
 
 bool
index 07d57a4bc835c9686686ea0340ec4e094a980fdb..b31f6b4979a3570eca36a97a4be3c5ee39ab6e1e 100644 (file)
--- a/options.h
+++ b/options.h
@@ -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
@@ -71,14 +71,15 @@ enum fw3_flag
        FW3_FLAG_MARK          = 10,
        FW3_FLAG_DNAT          = 11,
        FW3_FLAG_SNAT          = 12,
-       FW3_FLAG_SRC_ACCEPT    = 13,
-       FW3_FLAG_SRC_REJECT    = 14,
-       FW3_FLAG_SRC_DROP      = 15,
-       FW3_FLAG_CUSTOM_CHAINS = 16,
-       FW3_FLAG_SYN_FLOOD     = 17,
-       FW3_FLAG_MTU_FIX       = 18,
-       FW3_FLAG_DROP_INVALID  = 19,
-       FW3_FLAG_HOTPLUG       = 20,
+       FW3_FLAG_MASQUERADE    = 13,
+       FW3_FLAG_SRC_ACCEPT    = 14,
+       FW3_FLAG_SRC_REJECT    = 15,
+       FW3_FLAG_SRC_DROP      = 16,
+       FW3_FLAG_CUSTOM_CHAINS = 17,
+       FW3_FLAG_SYN_FLOOD     = 18,
+       FW3_FLAG_MTU_FIX       = 19,
+       FW3_FLAG_DROP_INVALID  = 20,
+       FW3_FLAG_HOTPLUG       = 21,
 
        __FW3_FLAG_MAX
 };
@@ -394,6 +395,40 @@ struct fw3_redirect
        enum fw3_reflection_source reflection_src;
 };
 
+struct fw3_snat
+{
+       struct list_head list;
+
+       bool enabled;
+       const char *name;
+
+       enum fw3_family family;
+
+       struct fw3_zone *_src;
+
+       struct fw3_device src;
+       struct fw3_setmatch ipset;
+
+       struct list_head proto;
+
+       struct fw3_address ip_src;
+       struct fw3_port port_src;
+
+       struct fw3_address ip_dest;
+       struct fw3_port port_dest;
+
+       struct fw3_address ip_snat;
+       struct fw3_port port_snat;
+
+       struct fw3_limit limit;
+       struct fw3_time time;
+       struct fw3_mark mark;
+
+       enum fw3_flag target;
+
+       const char *extra;
+};
+
 struct fw3_forward
 {
        struct list_head list;
@@ -456,6 +491,7 @@ struct fw3_state
        struct list_head zones;
        struct list_head rules;
        struct list_head redirects;
+       struct list_head snats;
        struct list_head forwards;
        struct list_head ipsets;
        struct list_head includes;
index 18666a3c7dfd5940bbf66a94b1bd8accbb2d818e..a21998bafb3bedf32fb1924d08f37ffd24d1d72d 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
@@ -305,7 +305,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;
diff --git a/snats.c b/snats.c
new file mode 100644 (file)
index 0000000..00fd57c
--- /dev/null
+++ b/snats.c
@@ -0,0 +1,375 @@
+/*
+ * firewall3 - 3rd OpenWrt UCI firewall implementation
+ *
+ *   Copyright (C) 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
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "snats.h"
+
+
+const struct fw3_option fw3_snat_opts[] = {
+       FW3_OPT("enabled",             bool,      snat,     enabled),
+
+       FW3_OPT("name",                string,    snat,     name),
+       FW3_OPT("family",              family,    snat,     family),
+
+       FW3_OPT("src",                 device,    snat,     src),
+
+       FW3_OPT("ipset",               setmatch,  snat,     ipset),
+
+       FW3_LIST("proto",              protocol,  snat,     proto),
+
+       FW3_OPT("src_ip",              network,   snat,     ip_src),
+       FW3_OPT("src_port",            port,      snat,     port_src),
+
+       FW3_OPT("snat_ip",             network,   snat,     ip_snat),
+       FW3_OPT("snat_port",           port,      snat,     port_snat),
+
+       FW3_OPT("dest_ip",             network,   snat,     ip_dest),
+       FW3_OPT("dest_port",           port,      snat,     port_dest),
+
+       FW3_OPT("extra",               string,    snat,     extra),
+
+       FW3_OPT("limit",               limit,     snat,     limit),
+       FW3_OPT("limit_burst",         int,       snat,     limit.burst),
+
+       FW3_OPT("utc_time",            bool,      snat,     time.utc),
+       FW3_OPT("start_date",          date,      snat,     time.datestart),
+       FW3_OPT("stop_date",           date,      snat,     time.datestop),
+       FW3_OPT("start_time",          time,      snat,     time.timestart),
+       FW3_OPT("stop_time",           time,      snat,     time.timestop),
+       FW3_OPT("weekdays",            weekdays,  snat,     time.weekdays),
+       FW3_OPT("monthdays",           monthdays, snat,     time.monthdays),
+
+       FW3_OPT("mark",                mark,      snat,     mark),
+
+       FW3_OPT("target",              target,    snat,     target),
+
+       { }
+};
+
+
+static bool
+check_families(struct uci_element *e, struct fw3_snat *r)
+{
+       if (r->family == FW3_FAMILY_ANY)
+               return true;
+
+       if (r->_src && r->_src->family && r->_src->family != r->family)
+       {
+               warn_elem(e, "refers to source zone with different family");
+               return false;
+       }
+
+       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;
+       }
+
+       if (r->ip_src.family && r->ip_src.family != r->family)
+       {
+               warn_elem(e, "uses source ip with different family");
+               return false;
+       }
+
+       if (r->ip_dest.family && r->ip_dest.family != r->family)
+       {
+               warn_elem(e, "uses destination ip with different family");
+               return false;
+       }
+
+       if (r->ip_snat.family && r->ip_snat.family != r->family)
+       {
+               warn_elem(e, "uses snat ip with different family");
+               return false;
+       }
+
+       return true;
+}
+
+void
+fw3_load_snats(struct fw3_state *state, struct uci_package *p)
+{
+       struct uci_section *s;
+       struct uci_element *e;
+       struct fw3_snat *snat;
+
+       INIT_LIST_HEAD(&state->snats);
+
+       uci_foreach_element(&p->sections, e)
+       {
+               s = uci_to_section(e);
+
+               if (strcmp(s->type, "nat"))
+                       continue;
+
+               snat = malloc(sizeof(*snat));
+
+               if (!snat)
+                       continue;
+
+               memset(snat, 0, sizeof(*snat));
+
+               INIT_LIST_HEAD(&snat->proto);
+
+               snat->enabled = true;
+
+               if (!fw3_parse_options(snat, fw3_snat_opts, s))
+               {
+                       warn_elem(e, "skipped due to invalid options");
+                       fw3_free_snat(snat);
+                       continue;
+               }
+
+               if (!snat->enabled)
+               {
+                       fw3_free_snat(snat);
+                       continue;
+               }
+
+               if (snat->src.invert)
+               {
+                       warn_elem(e, "must not have an inverted source");
+                       fw3_free_snat(snat);
+                       continue;
+               }
+               else if (snat->src.set && !snat->src.any &&
+                        !(snat->_src = fw3_lookup_zone(state, snat->src.name)))
+               {
+                       warn_elem(e, "refers to not existing zone '%s'", snat->src.name);
+                       fw3_free_snat(snat);
+                       continue;
+               }
+               else if (snat->ipset.set && state->disable_ipsets)
+               {
+                       warn_elem(e, "skipped due to disabled ipset support");
+                       fw3_free_snat(snat);
+                       continue;
+               }
+               else if (snat->ipset.set &&
+                        !(snat->ipset.ptr = fw3_lookup_ipset(state, snat->ipset.name)))
+               {
+                       warn_elem(e, "refers to unknown ipset '%s'", snat->ipset.name);
+                       fw3_free_snat(snat);
+                       continue;
+               }
+
+               if (!check_families(e, snat))
+               {
+                       fw3_free_snat(snat);
+                       continue;
+               }
+
+               if (snat->target == FW3_FLAG_UNSPEC)
+               {
+                       warn_elem(e, "has no target specified, defaulting to MASQUERADE");
+                       snat->target = FW3_FLAG_MASQUERADE;
+               }
+               else if (snat->target < FW3_FLAG_SNAT || snat->target > FW3_FLAG_MASQUERADE)
+               {
+                       warn_elem(e, "has invalid target specified, defaulting to MASQUERADE");
+                       snat->target = FW3_FLAG_MASQUERADE;
+               }
+
+               if (snat->target == FW3_FLAG_SNAT &&
+                   !snat->ip_snat.set && !snat->port_snat.set)
+               {
+                       warn_elem(e, "needs either 'snat_ip' or 'snat_port' for SNAT");
+                       fw3_free_snat(snat);
+                       continue;
+               }
+               else if (snat->target == FW3_FLAG_MASQUERADE && snat->ip_snat.set)
+               {
+                       warn_elem(e, "must not use 'snat_ip' for MASQUERADE");
+                       fw3_free_snat(snat);
+                       continue;
+               }
+               else if (snat->target == FW3_FLAG_MASQUERADE && snat->port_snat.set)
+               {
+                       warn_elem(e, "must not use 'snat_port' for MASQUERADE");
+                       fw3_free_snat(snat);
+                       continue;
+               }
+
+               if (list_empty(&snat->proto))
+               {
+                       warn_elem(e, "does not specify a protocol, assuming all");
+                       fw3_parse_protocol(&snat->proto, "all", true);
+               }
+
+               if (snat->_src)
+               {
+                       set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
+                       snat->_src->conntrack = true;
+               }
+
+               list_add_tail(&snat->list, &state->snats);
+       }
+}
+
+static void
+append_chain(struct fw3_ipt_rule *r, struct fw3_snat *snat)
+{
+       if (snat->_src)
+               fw3_ipt_rule_append(r, "zone_%s_postrouting", snat->src.name);
+       else
+               fw3_ipt_rule_append(r, "delegate_postrouting");
+}
+
+static void
+set_target(struct fw3_ipt_rule *r, struct fw3_snat *snat,
+           struct fw3_protocol *proto)
+{
+       char buf[sizeof("255.255.255.255:65535-65535\0")];
+
+       if (snat->target == FW3_FLAG_SNAT)
+       {
+               buf[0] = '\0';
+
+               if (snat->ip_snat.set)
+               {
+                       inet_ntop(AF_INET, &snat->ip_snat.address.v4, buf, sizeof(buf));
+               }
+
+               if (snat->port_snat.set && proto && !proto->any &&
+                   (proto->protocol == 6 || proto->protocol == 17))
+               {
+                       if (snat->port_snat.port_min == snat->port_snat.port_max)
+                               sprintf(buf + strlen(buf), ":%u", snat->port_snat.port_min);
+                       else
+                               sprintf(buf + strlen(buf), ":%u-%u",
+                                               snat->port_snat.port_min, snat->port_snat.port_max);
+               }
+
+               fw3_ipt_rule_target(r, "SNAT");
+               fw3_ipt_rule_addarg(r, false, "--to-source", buf);
+       }
+       else
+       {
+               fw3_ipt_rule_target(r, "MASQUERADE");
+       }
+}
+
+static void
+set_comment(struct fw3_ipt_rule *r, const char *name, int num)
+{
+       if (name)
+               fw3_ipt_rule_comment(r, name);
+       else
+               fw3_ipt_rule_comment(r, "@nat[%u]", num);
+}
+
+static void
+print_snat(struct fw3_ipt_handle *h, struct fw3_state *state,
+           struct fw3_snat *snat, int num, struct fw3_protocol *proto)
+{
+       struct fw3_ipt_rule *r;
+       struct fw3_address *src, *dst;
+       struct fw3_port *spt, *dpt;
+
+       switch (h->table)
+       {
+       case FW3_TABLE_NAT:
+               src = &snat->ip_src;
+               dst = &snat->ip_dest;
+               spt = &snat->port_src;
+               dpt = &snat->port_dest;
+
+               r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
+               fw3_ipt_rule_sport_dport(r, spt, dpt);
+               fw3_ipt_rule_ipset(r, &snat->ipset);
+               fw3_ipt_rule_limit(r, &snat->limit);
+               fw3_ipt_rule_time(r, &snat->time);
+               fw3_ipt_rule_mark(r, &snat->mark);
+               set_target(r, snat, proto);
+               fw3_ipt_rule_extra(r, snat->extra);
+               set_comment(r, snat->name, num);
+               append_chain(r, snat);
+               break;
+
+       default:
+               break;
+       }
+}
+
+static void
+expand_snat(struct fw3_ipt_handle *handle, struct fw3_state *state,
+                struct fw3_snat *snat, int num)
+{
+       struct fw3_protocol *proto;
+
+       if (snat->name)
+               info("   * NAT '%s'", snat->name);
+       else
+               info("   * NAT #%u", num);
+
+       if (!fw3_is_family(snat->_src, handle->family))
+       {
+               info("     ! Skipping due to different family of zone");
+               return;
+       }
+
+       if (!fw3_is_family(&snat->ip_src, handle->family) ||
+           !fw3_is_family(&snat->ip_dest, handle->family) ||
+               !fw3_is_family(&snat->ip_snat, handle->family))
+       {
+               if (!snat->ip_src.resolved ||
+                   !snat->ip_dest.resolved ||
+                   !snat->ip_snat.resolved)
+                       info("     ! Skipping due to different family of ip address");
+
+               return;
+       }
+
+       if (snat->ipset.ptr)
+       {
+               if (!fw3_is_family(snat->ipset.ptr, handle->family))
+               {
+                       info("     ! Skipping due to different family in ipset");
+                       return;
+               }
+
+               if (!fw3_check_ipset(snat->ipset.ptr))
+               {
+                       info("     ! Skipping due to missing ipset '%s'",
+                            snat->ipset.ptr->external ?
+                                       snat->ipset.ptr->external : snat->ipset.ptr->name);
+                       return;
+               }
+
+               set(snat->ipset.ptr->flags, handle->family, handle->family);
+       }
+
+       fw3_foreach(proto, &snat->proto)
+               print_snat(handle, state, snat, num, proto);
+}
+
+void
+fw3_print_snats(struct fw3_ipt_handle *handle, struct fw3_state *state)
+{
+       int num = 0;
+       struct fw3_snat *snat;
+
+       if (handle->family == FW3_FAMILY_V6)
+               return;
+
+       if (handle->table != FW3_TABLE_NAT)
+               return;
+
+       list_for_each_entry(snat, &state->snats, list)
+               expand_snat(handle, state, snat, num++);
+}
diff --git a/snats.h b/snats.h
new file mode 100644 (file)
index 0000000..00d1406
--- /dev/null
+++ b/snats.h
@@ -0,0 +1,36 @@
+/*
+ * firewall3 - 3rd OpenWrt UCI firewall implementation
+ *
+ *   Copyright (C) 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
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __FW3_SNATS_H
+#define __FW3_SNATS_H
+
+#include "options.h"
+#include "zones.h"
+#include "ipsets.h"
+#include "ubus.h"
+#include "iptables.h"
+
+extern const struct fw3_option fw3_snat_opts[];
+
+void fw3_load_snats(struct fw3_state *state, struct uci_package *p);
+void fw3_print_snats(struct fw3_ipt_handle *handle, struct fw3_state *state);
+
+#define fw3_free_snat(redir) \
+       fw3_free_object(redir, fw3_snat_opts)
+
+#endif