Only process selected family for print
[project/firewall3.git] / ipsets.c
index d6ff9bd..955d434 100644 (file)
--- a/ipsets.c
+++ b/ipsets.c
@@ -91,15 +91,6 @@ check_types(struct uci_element *e, struct fw3_ipset *ipset)
        uint32_t typelist = 0;
        struct fw3_ipset_datatype *type;
 
-       const char *methods[] = {
-               "(bug)",
-               "bitmap",
-               "hash",
-               "list",
-       };
-
-       typelist = 0;
-
        list_for_each_entry(type, &ipset->datatypes, list)
        {
                if (i >= 3)
@@ -121,7 +112,7 @@ check_types(struct uci_element *e, struct fw3_ipset *ipset)
                                ipset->method = ipset_types[i].method;
 
                                warn_elem(e, "defines no storage method, assuming '%s'",
-                                         methods[ipset->method]);
+                                         fw3_ipset_method_names[ipset->method]);
 
                                break;
                        }
@@ -135,7 +126,7 @@ check_types(struct uci_element *e, struct fw3_ipset *ipset)
                if (ipset_types[i].method == ipset->method &&
                    ipset_types[i].types == typelist)
                {
-                       if (!ipset->external || !*ipset->external)
+                       if (!ipset->external)
                        {
                                if ((ipset_types[i].required & OPT_IPRANGE) &&
                                        !ipset->iprange.set)
@@ -187,10 +178,10 @@ check_types(struct uci_element *e, struct fw3_ipset *ipset)
                                }
 
                                if (!(ipset_types[i].optional & OPT_FAMILY) &&
-                                   ipset->family != FW3_FAMILY_ANY)
+                                   ipset->family != FW3_FAMILY_V4)
                                {
                                        warn_elem(e, "family ignored");
-                                       ipset->family = FW3_FAMILY_ANY;
+                                       ipset->family = FW3_FAMILY_V4;
                                }
                        }
 
@@ -216,6 +207,9 @@ fw3_alloc_ipset(void)
 
        INIT_LIST_HEAD(&ipset->datatypes);
 
+       ipset->enabled = true;
+       ipset->family  = FW3_FAMILY_V4;
+
        return ipset;
 }
 
@@ -245,6 +239,14 @@ fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
 
                fw3_parse_options(ipset, fw3_ipset_opts, s);
 
+               if (ipset->external)
+               {
+                       if (!*ipset->external)
+                               ipset->external = NULL;
+                       else if (!ipset->name)
+                               ipset->name = ipset->external;
+               }
+
                if (!ipset->name || !*ipset->name)
                {
                        warn_elem(e, "must have a name assigned");
@@ -253,6 +255,10 @@ fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
                //{
                //      warn_elem(e, "has duplicated set name '%s'", ipset->name);
                //}
+               else if (ipset->family == FW3_FAMILY_ANY)
+               {
+                       warn_elem(e, "must not have family 'any'");
+               }
                else if (list_empty(&ipset->datatypes))
                {
                        warn_elem(e, "has no datatypes assigned");
@@ -272,64 +278,26 @@ static void
 create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
 {
        bool first = true;
-       char s[INET6_ADDRSTRLEN];
 
        struct fw3_ipset_datatype *type;
-       struct fw3_address *a;
-
-       const char *methods[] = {
-               "(bug)",
-               "bitmap",
-               "hash",
-               "list",
-       };
-
-       const char *types[] = {
-               "(bug)",
-               "ip",
-               "port",
-               "mac",
-               "net",
-               "set",
-       };
-
-       if (ipset->external && *ipset->external)
+
+       if (ipset->external)
                return;
 
-       info("Creating ipset %s", ipset->name);
+       info(" * Creating ipset %s", ipset->name);
 
        first = true;
-       fw3_pr("create %s %s", ipset->name, methods[ipset->method]);
+       fw3_pr("create %s %s", ipset->name, fw3_ipset_method_names[ipset->method]);
 
        list_for_each_entry(type, &ipset->datatypes, list)
        {
-               fw3_pr("%c%s", first ? ':' : ',', types[type->type]);
+               fw3_pr("%c%s", first ? ':' : ',', fw3_ipset_type_names[type->type]);
                first = false;
        }
 
        if (ipset->iprange.set)
        {
-               a = &ipset->iprange;
-
-               if (!a->range)
-               {
-                       inet_ntop(a->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &a->address.v6, s, sizeof(s));
-
-                       fw3_pr(" range %s/%u", s, a->mask);
-               }
-               else
-               {
-                       inet_ntop(a->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &a->address.v6, s, sizeof(s));
-
-                       fw3_pr(" range %s", s);
-
-                       inet_ntop(a->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &a->address2.v6, s, sizeof(s));
-
-                       fw3_pr("-%s", s);
-               }
+               fw3_pr(" range %s", fw3_address_to_string(&ipset->iprange, false));
        }
        else if (ipset->portrange.set)
        {
@@ -337,8 +305,7 @@ create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
                       ipset->portrange.port_min, ipset->portrange.port_max);
        }
 
-       if (ipset->family != FW3_FAMILY_ANY)
-               fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
+       fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
 
        if (ipset->timeout > 0)
                fw3_pr(" timeout %u", ipset->timeout);
@@ -353,8 +320,6 @@ create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
                fw3_pr(" hashsize %u", ipset->hashsize);
 
        fw3_pr("\n");
-
-       fw3_set_running(ipset, &state->running_ipsets);
 }
 
 void
@@ -366,37 +331,29 @@ fw3_create_ipsets(struct fw3_state *state)
                return;
 
        list_for_each_entry(ipset, &state->ipsets, list)
-               if (!fw3_lookup_ipset(state, ipset->name, true))
-                       create_ipset(ipset, state);
+               create_ipset(ipset, state);
 
        fw3_pr("quit\n");
 }
 
 void
-fw3_destroy_ipsets(struct fw3_state *state, enum fw3_family family)
+fw3_destroy_ipsets(struct fw3_state *state)
 {
-       struct fw3_ipset *s, *tmp;
-       uint32_t family_mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
+       struct fw3_ipset *s;
 
-       list_for_each_entry_safe(s, tmp, &state->running_ipsets, running_list)
+       list_for_each_entry(s, &state->ipsets, list)
        {
-               if (hasbit(s->running_flags, family))
-                       delbit(s->flags, family);
-
-               if (!(s->flags & family_mask))
-               {
-                       info("Deleting ipset %s", s->name);
+               info(" * Deleting ipset %s", s->name);
 
-                       fw3_pr("flush %s\n", s->name);
-                       fw3_pr("destroy %s\n", s->name);
-
-                       fw3_set_running(s, NULL);
-               }
+               fw3_pr("flush %s\n", s->name);
+               fw3_pr("destroy %s\n", s->name);
        }
+
+       fw3_pr("quit\n");
 }
 
 struct fw3_ipset *
-fw3_lookup_ipset(struct fw3_state *state, const char *name, bool running)
+fw3_lookup_ipset(struct fw3_state *state, const char *name)
 {
        struct fw3_ipset *s;
 
@@ -408,11 +365,45 @@ fw3_lookup_ipset(struct fw3_state *state, const char *name, bool running)
                if (strcmp(s->name, name))
                        continue;
 
-               if (!running || s->running_list.next)
-                       return s;
-
-               break;
+               return s;
        }
 
        return NULL;
 }
+
+bool
+fw3_check_ipset(struct fw3_ipset *set)
+{
+       bool rv = false;
+
+       socklen_t sz;
+       int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+       struct ip_set_req_version req_ver;
+       struct ip_set_req_get_set req_name;
+
+       if (s < 0 || fcntl(s, F_SETFD, FD_CLOEXEC))
+               goto out;
+
+       sz = sizeof(req_ver);
+       req_ver.op = IP_SET_OP_VERSION;
+
+       if (getsockopt(s, SOL_IP, SO_IP_SET, &req_ver, &sz))
+               goto out;
+
+       sz = sizeof(req_name);
+       req_name.op = IP_SET_OP_GET_BYNAME;
+       req_name.version = req_ver.version;
+       snprintf(req_name.set.name, IPSET_MAXNAMELEN - 1, "%s",
+                set->external ? set->external : set->name);
+
+       if (getsockopt(s, SOL_IP, SO_IP_SET, &req_name, &sz))
+               goto out;
+
+       rv = ((sz == sizeof(req_name)) && (req_name.set.index != IPSET_INVALID_ID));
+
+out:
+       if (s >= 0)
+               close(s);
+
+       return rv;
+}