Use netmasks instead of prefix lengths internally
[project/firewall3.git] / redirects.c
index f1fa1f4..5dea21f 100644 (file)
@@ -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
@@ -171,8 +168,7 @@ check_local(struct uci_element *e, struct fw3_redirect *redir,
 {
        struct fw3_zone *zone;
        struct fw3_device *net;
-       struct fw3_address *addr;
-       struct list_head *addrs;
+       struct fw3_address *addr, *tmp;
 
        if (redir->target != FW3_FLAG_DNAT)
                return false;
@@ -187,25 +183,23 @@ check_local(struct uci_element *e, struct fw3_redirect *redir,
        {
                list_for_each_entry(net, &zone->networks, list)
                {
-                       addrs = fw3_ubus_address(net->name);
+                       LIST_HEAD(addrs);
 
-                       if (!addrs)
-                               continue;
-
-                       list_for_each_entry(addr, addrs, list)
+                       fw3_ubus_address(&addrs, net->name);
+                       list_for_each_entry_safe(addr, tmp, &addrs, list)
                        {
-                               if (!compare_addr(&redir->ip_redir, addr))
-                                       continue;
+                               if (compare_addr(&redir->ip_redir, addr)) {
+                                       warn_elem(e, "refers to a destination address on this router, "
+                                                    "assuming port redirection");
 
-                               warn_elem(e, "refers to a destination address on this router, "
-                                            "assuming port redirection");
+                                       redir->local = true;
+                                       continue;
+                               }
 
-                               redir->local = true;
-                               break;
+                               list_del(&addr->list);
+                               free(addr);
                        }
 
-                       fw3_free_list(addrs);
-
                        if (redir->local)
                                return true;
                }
@@ -606,8 +600,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);