Add common fw3_address_to_string() helper function
[project/firewall3.git] / zones.c
diff --git a/zones.c b/zones.c
index 576ad42..55f2e6c 100644 (file)
--- a/zones.c
+++ b/zones.c
 #include "ubus.h"
 
 
-#define C(f, tbl, tgt, name) \
-       { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, name }
-
-struct chain {
-       enum fw3_family family;
-       enum fw3_table table;
-       enum fw3_target target;
-       const char *name;
-};
+#define C(f, tbl, tgt, fmt) \
+       { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##tgt, fmt }
+
+static const struct fw3_rule_spec zone_chains[] = {
+       C(ANY, FILTER, UNSPEC,        "zone_%1$s_input"),
+       C(ANY, FILTER, UNSPEC,        "zone_%1$s_output"),
+       C(ANY, FILTER, UNSPEC,        "zone_%1$s_forward"),
+
+       C(ANY, FILTER, SRC_ACCEPT,    "zone_%1$s_src_ACCEPT"),
+       C(ANY, FILTER, SRC_REJECT,    "zone_%1$s_src_REJECT"),
+       C(ANY, FILTER, SRC_DROP,      "zone_%1$s_src_DROP"),
+
+       C(ANY, FILTER, ACCEPT,        "zone_%1$s_dest_ACCEPT"),
+       C(ANY, FILTER, REJECT,        "zone_%1$s_dest_REJECT"),
+       C(ANY, FILTER, DROP,          "zone_%1$s_dest_DROP"),
+
+       C(V4,  NAT,    SNAT,          "zone_%1$s_postrouting"),
+       C(V4,  NAT,    DNAT,          "zone_%1$s_prerouting"),
 
-static const struct chain src_chains[] = {
-       C(ANY, FILTER, UNSPEC,  "zone_%s_input"),
-       C(ANY, FILTER, UNSPEC,  "zone_%s_output"),
-       C(ANY, FILTER, UNSPEC,  "zone_%s_forward"),
+       C(ANY, FILTER, CUSTOM_CHAINS, "input_%1$s_rule"),
+       C(ANY, FILTER, CUSTOM_CHAINS, "output_%1$s_rule"),
+       C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_%1$s_rule"),
 
-       C(ANY, FILTER, ACCEPT,  "zone_%s_src_ACCEPT"),
-       C(ANY, FILTER, REJECT,  "zone_%s_src_REJECT"),
-       C(ANY, FILTER, DROP,    "zone_%s_src_DROP"),
+       C(V4,  NAT,    CUSTOM_CHAINS, "prerouting_%1$s_rule"),
+       C(V4,  NAT,    CUSTOM_CHAINS, "postrouting_%1$s_rule"),
+
+       { }
 };
 
-static const struct chain dst_chains[] = {
-       C(ANY, FILTER, ACCEPT,  "zone_%s_dest_ACCEPT"),
-       C(ANY, FILTER, REJECT,  "zone_%s_dest_REJECT"),
-       C(ANY, FILTER, DROP,    "zone_%s_dest_DROP"),
 
-       C(V4,  NAT,    SNAT,    "zone_%s_postrouting"),
-       C(V4,  NAT,    DNAT,    "zone_%s_prerouting"),
+#define R(dir1, dir2) \
+       "zone_%1$s_" #dir1 " -m comment --comment \"user chain for %1$s " \
+       #dir2 "\" -j " #dir2 "_%1$s_rule"
+
+static const struct fw3_rule_spec zone_rules[] = {
+       C(ANY, FILTER, CUSTOM_CHAINS, R(input, input)),
+       C(ANY, FILTER, CUSTOM_CHAINS, R(output, output)),
+       C(ANY, FILTER, CUSTOM_CHAINS, R(forward, forwarding)),
 
-       C(ANY, RAW,    NOTRACK, "zone_%s_notrack"),
+       C(V4,  NAT,    CUSTOM_CHAINS, R(prerouting, prerouting)),
+       C(V4,  NAT,    CUSTOM_CHAINS, R(postrouting, postrouting)),
+
+       { }
 };
 
 const struct fw3_option fw3_zone_opts[] = {
+       FW3_OPT("enabled",             bool,     zone,     enabled),
+
        FW3_OPT("name",                string,   zone,     name),
        FW3_OPT("family",              family,   zone,     family),
 
        FW3_LIST("network",            device,   zone,     networks),
        FW3_LIST("device",             device,   zone,     devices),
-       FW3_LIST("subnet",             address,  zone,     subnets),
+       FW3_LIST("subnet",             network,  zone,     subnets),
 
        FW3_OPT("input",               target,   zone,     policy_input),
        FW3_OPT("forward",             target,   zone,     policy_forward),
        FW3_OPT("output",              target,   zone,     policy_output),
 
        FW3_OPT("masq",                bool,     zone,     masq),
-       FW3_LIST("masq_src",           address,  zone,     masq_src),
-       FW3_LIST("masq_dest",          address,  zone,     masq_dest),
+       FW3_LIST("masq_src",           network,  zone,     masq_src),
+       FW3_LIST("masq_dest",          network,  zone,     masq_dest),
 
        FW3_OPT("extra",               string,   zone,     extra_src),
        FW3_OPT("extra_src",           string,   zone,     extra_src),
@@ -78,49 +94,23 @@ const struct fw3_option fw3_zone_opts[] = {
        FW3_OPT("log",                 bool,     zone,     log),
        FW3_OPT("log_limit",           limit,    zone,     log_limit),
 
+       FW3_OPT("__flags_v4",          int,      zone,     flags[0]),
+       FW3_OPT("__flags_v6",          int,      zone,     flags[1]),
+
        { }
 };
 
 
-static bool
-print_chains(enum fw3_table table, enum fw3_family family,
-             const char *fmt, const char *name, uint16_t targets,
-             const struct chain *chains, int n)
-{
-       bool rv = false;
-       char cn[128] = { 0 };
-       const struct chain *c;
-
-       for (c = chains; n > 0; c++, n--)
-       {
-               if (!fw3_is_family(c, family))
-                       continue;
-
-               if (c->table != table)
-                       continue;
-
-               if ((c->target != FW3_TARGET_UNSPEC) && !hasbit(targets, c->target))
-                       continue;
-
-               snprintf(cn, sizeof(cn), c->name, name);
-               fw3_pr(fmt, cn);
-
-               rv = true;
-       }
-
-       return rv;
-}
-
 static void
-check_policy(struct uci_element *e, enum fw3_target *pol, enum fw3_target def,
+check_policy(struct uci_element *e, enum fw3_flag *pol, enum fw3_flag def,
              const char *name)
 {
-       if (*pol == FW3_TARGET_UNSPEC)
+       if (*pol == FW3_FLAG_UNSPEC)
        {
                warn_elem(e, "has no %s policy specified, using default", name);
                *pol = def;
        }
-       else if (*pol > FW3_TARGET_DROP)
+       else if (*pol > FW3_FLAG_DROP)
        {
                warn_elem(e, "has invalid %s policy, using default", name);
                *pol = def;
@@ -142,6 +132,7 @@ resolve_networks(struct uci_element *e, struct fw3_zone *zone)
                        continue;
                }
 
+               tmp->network = net;
                list_add_tail(&tmp->list, &zone->devices);
        }
 }
@@ -164,6 +155,8 @@ fw3_alloc_zone(void)
        INIT_LIST_HEAD(&zone->masq_src);
        INIT_LIST_HEAD(&zone->masq_dest);
 
+       zone->enabled = true;
+       zone->custom_chains = true;
        zone->log_limit.rate = 10;
 
        return zone;
@@ -193,9 +186,18 @@ fw3_load_zones(struct fw3_state *state, struct uci_package *p)
 
                fw3_parse_options(zone, fw3_zone_opts, s);
 
+               if (!zone->enabled)
+               {
+                       fw3_free_zone(zone);
+                       continue;
+               }
+
                if (!zone->extra_dest)
                        zone->extra_dest = zone->extra_src;
 
+               if (!defs->custom_chains && zone->custom_chains)
+                       zone->custom_chains = false;
+
                if (!zone->name || !*zone->name)
                {
                        warn_elem(e, "has no name - ignoring");
@@ -217,13 +219,23 @@ fw3_load_zones(struct fw3_state *state, struct uci_package *p)
 
                if (zone->masq)
                {
-                       setbit(zone->dst_flags, FW3_TARGET_SNAT);
+                       setbit(zone->flags[0], FW3_FLAG_SNAT);
                        zone->conntrack = true;
                }
 
-               setbit(zone->src_flags, zone->policy_input);
-               setbit(zone->dst_flags, zone->policy_output);
-               setbit(zone->dst_flags, zone->policy_forward);
+               if (zone->custom_chains)
+               {
+                       setbit(zone->flags[0], FW3_FLAG_SNAT);
+                       setbit(zone->flags[0], FW3_FLAG_DNAT);
+               }
+
+               setbit(zone->flags[0], fw3_to_src_target(zone->policy_input));
+               setbit(zone->flags[0], zone->policy_output);
+               setbit(zone->flags[0], zone->policy_forward);
+
+               setbit(zone->flags[1], fw3_to_src_target(zone->policy_input));
+               setbit(zone->flags[1], zone->policy_output);
+               setbit(zone->flags[1], zone->policy_forward);
 
                list_add_tail(&zone->list, &state->zones);
        }
@@ -231,47 +243,58 @@ fw3_load_zones(struct fw3_state *state, struct uci_package *p)
 
 
 static void
-print_zone_chain(enum fw3_table table, enum fw3_family family,
-                 struct fw3_zone *zone, struct fw3_state *state)
+print_zone_chain(struct fw3_state *state, enum fw3_family family,
+                 enum fw3_table table, bool reload, struct fw3_zone *zone)
 {
-       bool s, d;
+       bool c, r;
+       uint32_t custom_mask = ~0;
 
        if (!fw3_is_family(zone, family))
                return;
 
-       setbit(zone->dst_flags, family);
+       set(zone->flags, family, table);
+
+       /* Don't touch user chains on reload */
+       if (reload)
+               delbit(custom_mask, FW3_FLAG_CUSTOM_CHAINS);
+
+       if (zone->custom_chains)
+               set(zone->flags, family, FW3_FLAG_CUSTOM_CHAINS);
 
        if (!zone->conntrack && !state->defaults.drop_invalid)
-               setbit(zone->dst_flags, FW3_TARGET_NOTRACK);
+               set(zone->flags, family, FW3_FLAG_NOTRACK);
 
-       s = print_chains(table, family, ":%s - [0:0]\n", zone->name,
-                        zone->src_flags, src_chains, ARRAY_SIZE(src_chains));
+       c = fw3_pr_rulespec(table, family, zone->flags, custom_mask, zone_chains,
+                           ":%s - [0:0]\n", zone->name);
 
-       d = print_chains(table, family, ":%s - [0:0]\n", zone->name,
-                        zone->dst_flags, dst_chains, ARRAY_SIZE(dst_chains));
+       r = fw3_pr_rulespec(table, family, zone->flags, 0, zone_rules,
+                           "-A %s\n", zone->name);
 
-       if (s || d)
+       if (c || r)
        {
                info("   * Zone '%s'", zone->name);
-               fw3_set_running(zone, &state->running_zones);
+
+               set(zone->flags, family, table);
        }
 }
 
 static void
-print_interface_rule(enum fw3_table table, enum fw3_family family,
-                     struct fw3_zone *zone, struct fw3_device *dev,
-                     struct fw3_address *sub, bool disable_notrack)
+print_interface_rule(struct fw3_state *state, enum fw3_family family,
+                     enum fw3_table table, bool reload, struct fw3_zone *zone,
+                     struct fw3_device *dev, struct fw3_address *sub)
 {
-       enum fw3_target t;
+       bool disable_notrack = state->defaults.drop_invalid;
+
+       enum fw3_flag t;
 
 #define jump_target(t) \
-       ((t == FW3_TARGET_REJECT) ? "reject" : fw3_flag_names[t])
+       ((t == FW3_FLAG_REJECT) ? "reject" : fw3_flag_names[t])
 
        if (table == FW3_TABLE_FILTER)
        {
-               for (t = FW3_TARGET_ACCEPT; t <= FW3_TARGET_DROP; t++)
+               for (t = FW3_FLAG_ACCEPT; t <= FW3_FLAG_DROP; t++)
                {
-                       if (hasbit(zone->src_flags, t))
+                       if (has(zone->flags, family, fw3_to_src_target(t)))
                        {
                                fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
                                fw3_format_in_out(dev, NULL);
@@ -280,7 +303,7 @@ print_interface_rule(enum fw3_table table, enum fw3_family family,
                                fw3_pr(" -j %s\n", jump_target(t));
                        }
 
-                       if (hasbit(zone->dst_flags, t))
+                       if (has(zone->flags, family, t))
                        {
                                fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
                                fw3_format_in_out(NULL, dev);
@@ -310,7 +333,7 @@ print_interface_rule(enum fw3_table table, enum fw3_family family,
        }
        else if (table == FW3_TABLE_NAT)
        {
-               if (hasbit(zone->dst_flags, FW3_TARGET_DNAT))
+               if (has(zone->flags, family, FW3_FLAG_DNAT))
                {
                        fw3_pr("-A delegate_prerouting");
                        fw3_format_in_out(dev, NULL);
@@ -319,7 +342,7 @@ print_interface_rule(enum fw3_table table, enum fw3_family family,
                        fw3_pr(" -j zone_%s_prerouting\n", zone->name);
                }
 
-               if (hasbit(zone->dst_flags, FW3_TARGET_SNAT))
+               if (has(zone->flags, family, FW3_FLAG_SNAT))
                {
                        fw3_pr("-A delegate_postrouting");
                        fw3_format_in_out(NULL, dev);
@@ -366,8 +389,8 @@ print_interface_rule(enum fw3_table table, enum fw3_family family,
 }
 
 static void
-print_interface_rules(enum fw3_table table, enum fw3_family family,
-                      struct fw3_zone *zone, bool disable_notrack)
+print_interface_rules(struct fw3_state *state, enum fw3_family family,
+                      enum fw3_table table, bool reload, struct fw3_zone *zone)
 {
        struct fw3_device *dev;
        struct fw3_address *sub;
@@ -381,18 +404,18 @@ print_interface_rules(enum fw3_table table, enum fw3_family family,
                if (!dev && !sub)
                        continue;
 
-               print_interface_rule(table, family, zone, dev, sub, disable_notrack);
+               print_interface_rule(state, family, table, reload, zone, dev, sub);
        }
 }
 
 static void
-print_zone_rule(enum fw3_table table, enum fw3_family family,
-                struct fw3_zone *zone, bool disable_notrack)
+print_zone_rule(struct fw3_state *state, enum fw3_family family,
+                enum fw3_table table, bool reload, struct fw3_zone *zone)
 {
        struct fw3_address *msrc;
        struct fw3_address *mdest;
 
-       enum fw3_target t;
+       enum fw3_flag t;
 
        if (!fw3_is_family(zone, family))
                return;
@@ -411,9 +434,9 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
 
                if (zone->log)
                {
-                       for (t = FW3_TARGET_REJECT; t <= FW3_TARGET_DROP; t++)
+                       for (t = FW3_FLAG_REJECT; t <= FW3_FLAG_DROP; t++)
                        {
-                               if (hasbit(zone->src_flags, t))
+                               if (has(zone->flags, family, fw3_to_src_target(t)))
                                {
                                        fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
                                        fw3_format_limit(&zone->log_limit);
@@ -421,7 +444,7 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
                                                   fw3_flag_names[t], zone->name);
                                }
 
-                               if (hasbit(zone->dst_flags, t))
+                               if (has(zone->flags, family, t))
                                {
                                        fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
                                        fw3_format_limit(&zone->log_limit);
@@ -438,9 +461,13 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
                        fw3_foreach(msrc, &zone->masq_src)
                        fw3_foreach(mdest, &zone->masq_dest)
                        {
-                               fw3_pr("-A zone_%s_postrouting ", zone->name);
+                               if (!fw3_is_family(msrc, family) ||
+                                   !fw3_is_family(mdest, family))
+                                       continue;
+
+                               fw3_pr("-A zone_%s_postrouting", zone->name);
                                fw3_format_src_dest(msrc, mdest);
-                               fw3_pr("-j MASQUERADE\n");
+                               fw3_pr(" -j MASQUERADE\n");
                        }
                }
                break;
@@ -450,59 +477,89 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
                break;
        }
 
-       print_interface_rules(table, family, zone, disable_notrack);
+       print_interface_rules(state, family, table, reload, zone);
 }
 
 void
-fw3_print_zone_chains(enum fw3_table table, enum fw3_family family,
-                      struct fw3_state *state)
+fw3_print_zone_chains(struct fw3_state *state, enum fw3_family family,
+                      enum fw3_table table, bool reload)
 {
        struct fw3_zone *zone;
 
        list_for_each_entry(zone, &state->zones, list)
-               print_zone_chain(table, family, zone, state);
+               print_zone_chain(state, family, table, reload, zone);
 }
 
 void
-fw3_print_zone_rules(enum fw3_table table, enum fw3_family family,
-                     struct fw3_state *state)
+fw3_print_zone_rules(struct fw3_state *state, enum fw3_family family,
+                     enum fw3_table table, bool reload)
 {
        struct fw3_zone *zone;
 
        list_for_each_entry(zone, &state->zones, list)
-               print_zone_rule(table, family, zone, state->defaults.drop_invalid);
+               print_zone_rule(state, family, table, reload, zone);
 }
 
 void
-fw3_flush_zones(enum fw3_table table, enum fw3_family family,
-                           bool pass2, struct fw3_state *state)
+fw3_flush_zones(struct fw3_state *state, enum fw3_family family,
+                enum fw3_table table, bool reload, bool pass2)
 {
        struct fw3_zone *z, *tmp;
-       int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
+       uint32_t custom_mask = ~0;
+
+       /* don't touch user chains on selective stop */
+       if (reload)
+               delbit(custom_mask, FW3_FLAG_CUSTOM_CHAINS);
 
-       list_for_each_entry_safe(z, tmp, &state->running_zones, running_list)
+       list_for_each_entry_safe(z, tmp, &state->zones, list)
        {
-               if (!hasbit(z->dst_flags, family))
+               if (!has(z->flags, family, table))
                        continue;
 
-               print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
-                            z->name, z->src_flags, src_chains, ARRAY_SIZE(src_chains));
-
-               print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
-                            z->name, z->dst_flags, dst_chains, ARRAY_SIZE(dst_chains));
+               fw3_pr_rulespec(table, family, z->flags, custom_mask, zone_chains,
+                               pass2 ? "-X %s\n" : "-F %s\n", z->name);
 
                if (pass2)
+                       del(z->flags, family, table);
+       }
+}
+
+void
+fw3_hotplug_zones(struct fw3_state *state, bool add)
+{
+       struct fw3_zone *z;
+       struct fw3_device *d;
+
+       if (add)
+       {
+               list_for_each_entry(z, &state->zones, list)
+               {
+                       if (!hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
+                       {
+                               list_for_each_entry(d, &z->devices, list)
+                                       fw3_hotplug(add, z, d);
+
+                               setbit(z->flags[0], FW3_FLAG_HOTPLUG);
+                       }
+               }
+       }
+       else
+       {
+               list_for_each_entry(z, &state->zones, list)
                {
-                       delbit(z->dst_flags, family);
+                       if (hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
+                       {
+                               list_for_each_entry(d, &z->devices, list)
+                                       fw3_hotplug(add, z, d);
 
-                       if (!(z->dst_flags & mask))
-                               fw3_set_running(z, NULL);
+                               delbit(z->flags[0], FW3_FLAG_HOTPLUG);
+                       }
                }
        }
 }
 
 struct fw3_zone *
-fw3_lookup_zone(struct fw3_state *state, const char *name, bool running)
+fw3_lookup_zone(struct fw3_state *state, const char *name)
 {
        struct fw3_zone *z;
 
@@ -514,11 +571,28 @@ fw3_lookup_zone(struct fw3_state *state, const char *name, bool running)
                if (strcmp(z->name, name))
                        continue;
 
-               if (!running || z->running_list.next)
-                       return z;
-
-               break;
+               return z;
        }
 
        return NULL;
 }
+
+void
+fw3_free_zone(struct fw3_zone *zone)
+{
+       struct fw3_device *dev, *tmp;
+
+       list_for_each_entry_safe(dev, tmp, &zone->devices, list)
+       {
+               list_del(&dev->list);
+               free(dev);
+       }
+
+       list_for_each_entry_safe(dev, tmp, &zone->networks, list)
+       {
+               list_del(&dev->list);
+               free(dev);
+       }
+
+       fw3_free_object(zone, fw3_zone_opts);
+}