ubus: fix invalid ipv6-prefix json
[project/odhcpd.git] / src / config.c
index e79f9ce..c3e2437 100644 (file)
@@ -42,6 +42,7 @@ enum {
        IFACE_ATTR_DNS,
        IFACE_ATTR_DOMAIN,
        IFACE_ATTR_FILTER_CLASS,
+       IFACE_ATTR_DHCPV4_FORCERECONF,
        IFACE_ATTR_DHCPV6_RAW,
        IFACE_ATTR_DHCPV6_ASSIGNALL,
        IFACE_ATTR_RA_DEFAULT,
@@ -61,6 +62,7 @@ enum {
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
        IFACE_ATTR_NDPROXY_SLAVE,
+       IFACE_ATTR_PREFIX_FILTER,
        IFACE_ATTR_MAX
 };
 
@@ -83,6 +85,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
        [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
        [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
+       [IFACE_ATTR_DHCPV4_FORCERECONF] = { .name = "dhcpv4_forcereconf", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_DHCPV6_ASSIGNALL] = { .name ="dhcpv6_assignall", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
@@ -102,6 +105,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_RA_MTU] = { .name = "ra_mtu", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
+       [IFACE_ATTR_PREFIX_FILTER] = { .name = "prefix_filter", .type = BLOBMSG_TYPE_STRING },
 };
 
 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
@@ -182,7 +186,7 @@ static int mkdir_p(char *dir, mode_t mask)
                return 0;
 
        if (ret)
-               syslog(LOG_ERR, "mkdir(%s, %d) failed: %s\n", dir, mask, strerror(errno));
+               syslog(LOG_ERR, "mkdir(%s, %d) failed: %m\n", dir, mask);
 
        return ret;
 }
@@ -234,14 +238,16 @@ static void close_interface(struct interface *iface)
        if (iface->head.next)
                list_del(&iface->head);
 
-       setup_router_interface(iface, false);
-       setup_dhcpv6_interface(iface, false);
-       setup_ndp_interface(iface, false);
-       setup_dhcpv4_interface(iface, false);
+       router_setup_interface(iface, false);
+       dhcpv6_setup_interface(iface, false);
+       ndp_setup_interface(iface, false);
+#ifdef DHCPV4_SUPPORT
+       dhcpv4_setup_interface(iface, false);
+#endif
 
        clean_interface(iface);
        free(iface->addr4);
-       free(iface->ia_addr);
+       free(iface->addr6);
        free(iface->ifname);
        free(iface);
 }
@@ -447,13 +453,13 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        }
 
        if (get_addrs) {
-               ssize_t len = odhcpd_get_interface_addresses(iface->ifindex,
-                                               true, &iface->ia_addr);
+               ssize_t len = netlink_get_interface_addrs(iface->ifindex,
+                                               true, &iface->addr6);
 
                if (len > 0)
-                       iface->ia_addr_len = len;
+                       iface->addr6_len = len;
 
-               len = odhcpd_get_interface_addresses(iface->ifindex,
+               len = netlink_get_interface_addrs(iface->ifindex,
                                                false, &iface->addr4);
                if (len > 0)
                        iface->addr4_len = len;
@@ -621,6 +627,9 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
        }
 
+       if ((c = tb[IFACE_ATTR_DHCPV4_FORCERECONF]))
+               iface->dhcpv4_forcereconf = blobmsg_get_bool(c);
+
        if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
                iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
                iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
@@ -713,6 +722,23 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
                iface->external = blobmsg_get_bool(c);
 
+       if ((c = tb[IFACE_ATTR_PREFIX_FILTER])) {
+               const char *str = blobmsg_get_string(c);
+               char *astr = malloc(strlen(str) + 1);
+               char *delim;
+               int l;
+               if (!astr || !strcpy(astr, str) ||
+                               (delim = strchr(astr, '/')) == NULL || (*(delim++) = 0) ||
+                               sscanf(delim, "%i", &l) == 0 || l > 128 ||
+                               inet_pton(AF_INET6, astr, &iface->pio_filter_addr) == 0) {
+                       iface->pio_filter_length = 0;
+               } else {
+                       iface->pio_filter_length = l;
+               }
+               if (astr)
+                       free(astr);
+       }
+
        return 0;
 
 err:
@@ -838,10 +864,12 @@ void odhcpd_reload(void)
                                i->ndp = (master && master->ndp == MODE_RELAY) ?
                                                MODE_RELAY : MODE_DISABLED;
 
-                       setup_router_interface(i, !i->ignore || i->ra != MODE_DISABLED);
-                       setup_dhcpv6_interface(i, !i->ignore || i->dhcpv6 != MODE_DISABLED);
-                       setup_ndp_interface(i, !i->ignore || i->ndp != MODE_DISABLED);
-                       setup_dhcpv4_interface(i, !i->ignore || i->dhcpv4 != MODE_DISABLED);
+                       router_setup_interface(i, !i->ignore || i->ra != MODE_DISABLED);
+                       dhcpv6_setup_interface(i, !i->ignore || i->dhcpv6 != MODE_DISABLED);
+                       ndp_setup_interface(i, !i->ignore || i->ndp != MODE_DISABLED);
+#ifdef DHCPV4_SUPPORT
+                       dhcpv4_setup_interface(i, !i->ignore || i->dhcpv4 != MODE_DISABLED);
+#endif
                } else
                        close_interface(i);
        }
@@ -882,7 +910,7 @@ void odhcpd_run(void)
        signal(SIGHUP, handle_signal);
 
 #ifdef WITH_UBUS
-       while (init_ubus())
+       while (ubus_init())
                sleep(1);
 #endif