options: fix logic flaw when parsing ipaddr/mask notation
[project/firewall3.git] / zones.c
diff --git a/zones.c b/zones.c
index 9f1a68d..6ab3d12 100644 (file)
--- a/zones.c
+++ b/zones.c
@@ -39,6 +39,8 @@ static const struct fw3_chain_spec zone_chains[] = {
        C(V4,  NAT,    SNAT,          "zone_%s_postrouting"),
        C(V4,  NAT,    DNAT,          "zone_%s_prerouting"),
 
+       C(ANY, RAW,    NOTRACK,       "zone_%s_notrack"),
+
        C(ANY, FILTER, CUSTOM_CHAINS, "input_%s_rule"),
        C(ANY, FILTER, CUSTOM_CHAINS, "output_%s_rule"),
        C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_%s_rule"),
@@ -126,13 +128,10 @@ fw3_alloc_zone(void)
 {
        struct fw3_zone *zone;
 
-       zone = malloc(sizeof(*zone));
-
+       zone = calloc(1, sizeof(*zone));
        if (!zone)
                return NULL;
 
-       memset(zone, 0, sizeof(*zone));
-
        INIT_LIST_HEAD(&zone->networks);
        INIT_LIST_HEAD(&zone->devices);
        INIT_LIST_HEAD(&zone->subnets);
@@ -197,6 +196,8 @@ fw3_load_zones(struct fw3_state *state, struct uci_package *p)
                        continue;
                }
 
+               fw3_ubus_zone_devices(zone);
+
                if (list_empty(&zone->networks) && list_empty(&zone->devices) &&
                    list_empty(&zone->subnets) && !zone->extra_src)
                {
@@ -317,7 +318,6 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                                         bool reload, struct fw3_zone *zone,
                      struct fw3_device *dev, struct fw3_address *sub)
 {
-       bool disable_notrack = state->defaults.drop_invalid;
        struct fw3_protocol tcp = { .protocol = 6 };
        struct fw3_ipt_rule *r;
        enum fw3_flag t;
@@ -344,8 +344,8 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                                r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
                                fw3_ipt_rule_target(r, jump_target(t));
                                fw3_ipt_rule_extra(r, zone->extra_src);
-                               fw3_ipt_rule_append(r, "zone_%s_src_%s", zone->name,
-                                                   fw3_flag_names[t]);
+                               fw3_ipt_rule_replace(r, "zone_%s_src_%s", zone->name,
+                                                    fw3_flag_names[t]);
                        }
 
                        if (has(zone->flags, handle->family, t))
@@ -353,8 +353,8 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                                r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
                                fw3_ipt_rule_target(r, jump_target(t));
                                fw3_ipt_rule_extra(r, zone->extra_dest);
-                               fw3_ipt_rule_append(r, "zone_%s_dest_%s", zone->name,
-                                                   fw3_flag_names[t]);
+                               fw3_ipt_rule_replace(r, "zone_%s_dest_%s", zone->name,
+                                                    fw3_flag_names[t]);
                        }
                }
 
@@ -372,7 +372,7 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                        else
                                fw3_ipt_rule_extra(r, zone->extra_src);
 
-                       fw3_ipt_rule_append(r, "delegate_%s", chains[i]);
+                       fw3_ipt_rule_replace(r, "delegate_%s", chains[i]);
                }
        }
        else if (handle->table == FW3_TABLE_NAT)
@@ -382,7 +382,7 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                        r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
                        fw3_ipt_rule_target(r, "zone_%s_prerouting", zone->name);
                        fw3_ipt_rule_extra(r, zone->extra_src);
-                       fw3_ipt_rule_append(r, "delegate_prerouting");
+                       fw3_ipt_rule_replace(r, "delegate_prerouting");
                }
 
                if (has(zone->flags, handle->family, FW3_FLAG_SNAT))
@@ -390,7 +390,7 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                        r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
                        fw3_ipt_rule_target(r, "zone_%s_postrouting", zone->name);
                        fw3_ipt_rule_extra(r, zone->extra_dest);
-                       fw3_ipt_rule_append(r, "delegate_postrouting");
+                       fw3_ipt_rule_replace(r, "delegate_postrouting");
                }
        }
        else if (handle->table == FW3_TABLE_MANGLE)
@@ -408,7 +408,7 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                                fw3_ipt_rule_comment(r, "%s (mtu_fix logging)", zone->name);
                                fw3_ipt_rule_target(r, "LOG");
                                fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
-                               fw3_ipt_rule_append(r, "mssfix");
+                               fw3_ipt_rule_replace(r, "mssfix");
                        }
 
                        r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
@@ -417,18 +417,17 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                        fw3_ipt_rule_comment(r, "%s (mtu_fix)", zone->name);
                        fw3_ipt_rule_target(r, "TCPMSS");
                        fw3_ipt_rule_addarg(r, false, "--clamp-mss-to-pmtu", NULL);
-                       fw3_ipt_rule_append(r, "mssfix");
+                       fw3_ipt_rule_replace(r, "mssfix");
                }
        }
        else if (handle->table == FW3_TABLE_RAW)
        {
-               if (!zone->conntrack && !disable_notrack)
+               if (has(zone->flags, handle->family, FW3_FLAG_NOTRACK))
                {
                        r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
-                       fw3_ipt_rule_target(r, "CT");
-                       fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
+                       fw3_ipt_rule_target(r, "zone_%s_notrack", zone->name);
                        fw3_ipt_rule_extra(r, zone->extra_src);
-                       fw3_ipt_rule_append(r, "notrack");
+                       fw3_ipt_rule_replace(r, "delegate_notrack");
                }
        }
 }
@@ -457,6 +456,7 @@ static void
 print_zone_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                 bool reload, struct fw3_zone *zone)
 {
+       bool disable_notrack = state->defaults.drop_invalid;
        struct fw3_address *msrc;
        struct fw3_address *mdest;
        struct fw3_ipt_rule *r;
@@ -470,6 +470,21 @@ print_zone_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
        switch (handle->table)
        {
        case FW3_TABLE_FILTER:
+               if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
+               {
+                       r = fw3_ipt_rule_new(handle);
+                       fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
+                       fw3_ipt_rule_comment(r, "Accept port redirections");
+                       fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
+                       fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
+
+                       r = fw3_ipt_rule_new(handle);
+                       fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
+                       fw3_ipt_rule_comment(r, "Accept port forwards");
+                       fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
+                       fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
+               }
+
                r = fw3_ipt_rule_new(handle);
                fw3_ipt_rule_target(r, "zone_%s_src_%s", zone->name,
                                     fw3_flag_names[zone->policy_input]);
@@ -539,6 +554,15 @@ print_zone_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                break;
 
        case FW3_TABLE_RAW:
+               if (!zone->conntrack && !disable_notrack)
+               {
+                       r = fw3_ipt_rule_new(handle);
+                       fw3_ipt_rule_target(r, "CT");
+                       fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
+                       fw3_ipt_rule_append(r, "zone_%s_notrack", zone->name);
+               }
+               break;
+
        case FW3_TABLE_MANGLE:
                break;
        }
@@ -595,7 +619,13 @@ fw3_flush_zones(struct fw3_ipt_handle *handle, struct fw3_state *state,
                                continue;
 
                        snprintf(chain, sizeof(chain), c->format, z->name);
-                       fw3_ipt_delete_rules(handle, chain);
+                       fw3_ipt_flush_chain(handle, chain);
+
+                       /* keep certain basic chains that do not depend on any settings to
+                          avoid purging unrelated user rules pointing to them */
+                       if (reload && !c->flag)
+                               continue;
+
                        fw3_ipt_delete_chain(handle, chain);
                }
 
@@ -648,31 +678,16 @@ fw3_resolve_zone_addresses(struct fw3_zone *zone)
 {
        struct fw3_device *net;
        struct fw3_address *addr, *tmp;
-       struct list_head *addrs, *all;
-
-       all = malloc(sizeof(*all));
+       struct list_head *all;
 
+       all = calloc(1, sizeof(*all));
        if (!all)
                return NULL;
 
-       memset(all, 0, sizeof(*all));
        INIT_LIST_HEAD(all);
 
        list_for_each_entry(net, &zone->networks, list)
-       {
-               addrs = fw3_ubus_address(net->name);
-
-               if (!addrs)
-                       continue;
-
-               list_for_each_entry_safe(addr, tmp, addrs, list)
-               {
-                       list_del(&addr->list);
-                       list_add_tail(&addr->list, all);
-               }
-
-               free(addrs);
-       }
+               fw3_ubus_address(all, net->name);
 
        list_for_each_entry(addr, &zone->subnets, list)
        {