ubus: fix invalid ipv6-prefix json
[project/odhcpd.git] / src / config.c
index 7940095..c3e2437 100644 (file)
@@ -4,12 +4,14 @@
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <libgen.h>
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <libgen.h>
+#include <net/if.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <syslog.h>
 
 #include <uci.h>
 #include <uci_blob.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <syslog.h>
 
 #include <uci.h>
 #include <uci_blob.h>
+#include <libubox/utils.h>
 
 #include "odhcpd.h"
 
 
 #include "odhcpd.h"
 
@@ -40,7 +42,9 @@ enum {
        IFACE_ATTR_DNS,
        IFACE_ATTR_DOMAIN,
        IFACE_ATTR_FILTER_CLASS,
        IFACE_ATTR_DNS,
        IFACE_ATTR_DOMAIN,
        IFACE_ATTR_FILTER_CLASS,
+       IFACE_ATTR_DHCPV4_FORCERECONF,
        IFACE_ATTR_DHCPV6_RAW,
        IFACE_ATTR_DHCPV6_RAW,
+       IFACE_ATTR_DHCPV6_ASSIGNALL,
        IFACE_ATTR_RA_DEFAULT,
        IFACE_ATTR_RA_MANAGEMENT,
        IFACE_ATTR_RA_OFFLINK,
        IFACE_ATTR_RA_DEFAULT,
        IFACE_ATTR_RA_MANAGEMENT,
        IFACE_ATTR_RA_OFFLINK,
@@ -58,6 +62,7 @@ enum {
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
        IFACE_ATTR_NDPROXY_SLAVE,
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
        IFACE_ATTR_NDPROXY_SLAVE,
+       IFACE_ATTR_PREFIX_FILTER,
        IFACE_ATTR_MAX
 };
 
        IFACE_ATTR_MAX
 };
 
@@ -80,7 +85,9 @@ 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_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_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 },
        [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
@@ -98,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_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] = {
 };
 
 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
@@ -178,7 +186,7 @@ static int mkdir_p(char *dir, mode_t mask)
                return 0;
 
        if (ret)
                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;
 }
 
        return ret;
 }
@@ -203,9 +211,10 @@ static struct interface* get_interface(const char *name)
 
 static void set_interface_defaults(struct interface *iface)
 {
 
 static void set_interface_defaults(struct interface *iface)
 {
-       iface->managed = 1;
        iface->learn_routes = 1;
        iface->dhcpv4_leasetime = 43200;
        iface->learn_routes = 1;
        iface->dhcpv4_leasetime = 43200;
+       iface->dhcpv6_assignall = true;
+       iface->ra_managed = RA_MANAGED_MFLAG;
        iface->ra_maxinterval = 600;
        iface->ra_mininterval = iface->ra_maxinterval/3;
        iface->ra_lifetime = -1;
        iface->ra_maxinterval = 600;
        iface->ra_mininterval = iface->ra_maxinterval/3;
        iface->ra_lifetime = -1;
@@ -229,25 +238,30 @@ static void close_interface(struct interface *iface)
        if (iface->head.next)
                list_del(&iface->head);
 
        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);
 
        clean_interface(iface);
+       free(iface->addr4);
+       free(iface->addr6);
+       free(iface->ifname);
        free(iface);
 }
 
 static int parse_mode(const char *mode)
 {
        if (!strcmp(mode, "disabled"))
        free(iface);
 }
 
 static int parse_mode(const char *mode)
 {
        if (!strcmp(mode, "disabled"))
-               return RELAYD_DISABLED;
+               return MODE_DISABLED;
        else if (!strcmp(mode, "server"))
        else if (!strcmp(mode, "server"))
-               return RELAYD_SERVER;
+               return MODE_SERVER;
        else if (!strcmp(mode, "relay"))
        else if (!strcmp(mode, "relay"))
-               return RELAYD_RELAY;
+               return MODE_RELAY;
        else if (!strcmp(mode, "hybrid"))
        else if (!strcmp(mode, "hybrid"))
-               return RELAYD_HYBRID;
+               return MODE_HYBRID;
        else
                return -1;
 }
        else
                return -1;
 }
@@ -384,6 +398,8 @@ err:
 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
 {
        struct blob_attr *tb[IFACE_ATTR_MAX], *c;
 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
 {
        struct blob_attr *tb[IFACE_ATTR_MAX], *c;
+       bool get_addrs = false;
+
        blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
 
        if (tb[IFACE_ATTR_INTERFACE])
        blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
 
        if (tb[IFACE_ATTR_INTERFACE])
@@ -394,16 +410,18 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 
        struct interface *iface = get_interface(name);
        if (!iface) {
 
        struct interface *iface = get_interface(name);
        if (!iface) {
-               iface = calloc(1, sizeof(*iface));
+               char *iface_name;
+
+               iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
                if (!iface)
                        return -1;
 
                if (!iface)
                        return -1;
 
-               strncpy(iface->name, name, sizeof(iface->name) - 1);
+               iface->name = strcpy(iface_name, name);
 
                set_interface_defaults(iface);
 
                list_add(&iface->head, &interfaces);
 
                set_interface_defaults(iface);
 
                list_add(&iface->head, &interfaces);
-               overwrite = true;
+               get_addrs = overwrite = true;
        }
 
        const char *ifname = NULL;
        }
 
        const char *ifname = NULL;
@@ -415,18 +433,37 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        }
 
 #ifdef WITH_UBUS
        }
 
 #ifdef WITH_UBUS
-       if (overwrite || !iface->ifname[0])
+       if (overwrite || !iface->ifname)
                ifname = ubus_get_ifname(name);
 #endif
 
                ifname = ubus_get_ifname(name);
 #endif
 
-       if (!iface->ifname[0] && !ifname)
+       if (!iface->ifname && !ifname)
                goto err;
 
                goto err;
 
-       if (ifname)
-               strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
+       if (ifname) {
+               free(iface->ifname);
+               iface->ifname = strdup(ifname);
 
 
-       if ((iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
-               goto err;
+               if (!iface->ifname)
+                       goto err;
+
+               if (!iface->ifindex &&
+                   (iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
+                       goto err;
+       }
+
+       if (get_addrs) {
+               ssize_t len = netlink_get_interface_addrs(iface->ifindex,
+                                               true, &iface->addr6);
+
+               if (len > 0)
+                       iface->addr6_len = len;
+
+               len = netlink_get_interface_addrs(iface->ifindex,
+                                               false, &iface->addr4);
+               if (len > 0)
+                       iface->addr4_len = len;
+       }
 
        iface->inuse = true;
 
 
        iface->inuse = true;
 
@@ -448,7 +485,7 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
 
                if (config.main_dhcpv4 && config.legacy)
                iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
 
                if (config.main_dhcpv4 && config.legacy)
-                       iface->dhcpv4 = RELAYD_SERVER;
+                       iface->dhcpv4 = MODE_SERVER;
        }
 
        if ((c = tb[IFACE_ATTR_LIMIT]))
        }
 
        if ((c = tb[IFACE_ATTR_LIMIT]))
@@ -590,17 +627,23 @@ 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);
        }
 
                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);
                odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(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);
                odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
        }
 
+       if ((c = tb[IFACE_ATTR_DHCPV6_ASSIGNALL]))
+               iface->dhcpv6_assignall = blobmsg_get_bool(c);
+
        if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
                iface->default_router = blobmsg_get_u32(c);
 
        if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
        if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
                iface->default_router = blobmsg_get_u32(c);
 
        if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
-               iface->managed = blobmsg_get_u32(c);
+               iface->ra_managed = blobmsg_get_u32(c);
 
        if ((c = tb[IFACE_ATTR_RA_REACHABLETIME])) {
                uint32_t ra_reachabletime = blobmsg_get_u32(c);
 
        if ((c = tb[IFACE_ATTR_RA_REACHABLETIME])) {
                uint32_t ra_reachabletime = blobmsg_get_u32(c);
@@ -679,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_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:
        return 0;
 
 err:
@@ -745,13 +805,13 @@ void odhcpd_reload(void)
                if (i->master)
                        continue;
 
                if (i->master)
                        continue;
 
-               if (i->dhcpv6 == RELAYD_HYBRID || i->dhcpv6 == RELAYD_RELAY)
+               if (i->dhcpv6 == MODE_HYBRID || i->dhcpv6 == MODE_RELAY)
                        any_dhcpv6_slave = true;
 
                        any_dhcpv6_slave = true;
 
-               if (i->ra == RELAYD_HYBRID || i->ra == RELAYD_RELAY)
+               if (i->ra == MODE_HYBRID || i->ra == MODE_RELAY)
                        any_ra_slave = true;
 
                        any_ra_slave = true;
 
-               if (i->ndp == RELAYD_HYBRID || i->ndp == RELAYD_RELAY)
+               if (i->ndp == MODE_HYBRID || i->ndp == MODE_RELAY)
                        any_ndp_slave = true;
        }
 
                        any_ndp_slave = true;
        }
 
@@ -760,31 +820,31 @@ void odhcpd_reload(void)
                if (!i->master)
                        continue;
 
                if (!i->master)
                        continue;
 
-               enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
+               enum odhcpd_mode hybrid_mode = MODE_DISABLED;
 #ifdef WITH_UBUS
                if (!ubus_has_prefix(i->name, i->ifname))
 #ifdef WITH_UBUS
                if (!ubus_has_prefix(i->name, i->ifname))
-                       hybrid_mode = RELAYD_RELAY;
+                       hybrid_mode = MODE_RELAY;
 #endif
 
 #endif
 
-               if (i->dhcpv6 == RELAYD_HYBRID)
+               if (i->dhcpv6 == MODE_HYBRID)
                        i->dhcpv6 = hybrid_mode;
 
                        i->dhcpv6 = hybrid_mode;
 
-               if (i->dhcpv6 == RELAYD_RELAY && !any_dhcpv6_slave)
-                       i->dhcpv6 = RELAYD_DISABLED;
+               if (i->dhcpv6 == MODE_RELAY && !any_dhcpv6_slave)
+                       i->dhcpv6 = MODE_DISABLED;
 
 
-               if (i->ra == RELAYD_HYBRID)
+               if (i->ra == MODE_HYBRID)
                        i->ra = hybrid_mode;
 
                        i->ra = hybrid_mode;
 
-               if (i->ra == RELAYD_RELAY && !any_ra_slave)
-                       i->ra = RELAYD_DISABLED;
+               if (i->ra == MODE_RELAY && !any_ra_slave)
+                       i->ra = MODE_DISABLED;
 
 
-               if (i->ndp == RELAYD_HYBRID)
+               if (i->ndp == MODE_HYBRID)
                        i->ndp = hybrid_mode;
 
                        i->ndp = hybrid_mode;
 
-               if (i->ndp == RELAYD_RELAY && !any_ndp_slave)
-                       i->ndp = RELAYD_DISABLED;
+               if (i->ndp == MODE_RELAY && !any_ndp_slave)
+                       i->ndp = MODE_DISABLED;
 
 
-               if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
+               if (i->dhcpv6 == MODE_RELAY || i->ra == MODE_RELAY || i->ndp == MODE_RELAY)
                        master = i;
        }
 
                        master = i;
        }
 
@@ -792,27 +852,28 @@ void odhcpd_reload(void)
        list_for_each_entry_safe(i, n, &interfaces, head) {
                if (i->inuse) {
                        /* Resolve hybrid mode */
        list_for_each_entry_safe(i, n, &interfaces, head) {
                if (i->inuse) {
                        /* Resolve hybrid mode */
-                       if (i->dhcpv6 == RELAYD_HYBRID)
-                               i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
-                                               RELAYD_RELAY : RELAYD_SERVER;
-
-                       if (i->ra == RELAYD_HYBRID)
-                               i->ra = (master && master->ra == RELAYD_RELAY) ?
-                                               RELAYD_RELAY : RELAYD_SERVER;
-
-                       if (i->ndp == RELAYD_HYBRID)
-                               i->ndp = (master && master->ndp == RELAYD_RELAY) ?
-                                               RELAYD_RELAY : RELAYD_DISABLED;
-
-                       setup_router_interface(i, !i->ignore || i->ra != RELAYD_DISABLED);
-                       setup_dhcpv6_interface(i, !i->ignore || i->dhcpv6 != RELAYD_DISABLED);
-                       setup_ndp_interface(i, !i->ignore || i->ndp != RELAYD_DISABLED);
-                       setup_dhcpv4_interface(i, !i->ignore || i->dhcpv4 != RELAYD_DISABLED);
+                       if (i->dhcpv6 == MODE_HYBRID)
+                               i->dhcpv6 = (master && master->dhcpv6 == MODE_RELAY) ?
+                                               MODE_RELAY : MODE_SERVER;
+
+                       if (i->ra == MODE_HYBRID)
+                               i->ra = (master && master->ra == MODE_RELAY) ?
+                                               MODE_RELAY : MODE_SERVER;
+
+                       if (i->ndp == MODE_HYBRID)
+                               i->ndp = (master && master->ndp == MODE_RELAY) ?
+                                               MODE_RELAY : 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);
        }
 
                } else
                        close_interface(i);
        }
 
-       ndp_handle_addr6_dump();
        uci_unload(uci, dhcp);
        uci_free_context(uci);
 }
        uci_unload(uci, dhcp);
        uci_free_context(uci);
 }
@@ -849,7 +910,7 @@ void odhcpd_run(void)
        signal(SIGHUP, handle_signal);
 
 #ifdef WITH_UBUS
        signal(SIGHUP, handle_signal);
 
 #ifdef WITH_UBUS
-       while (init_ubus())
+       while (ubus_init())
                sleep(1);
 #endif
 
                sleep(1);
 #endif