ubus: fix invalid ipv6-prefix json
[project/odhcpd.git] / src / config.c
index e6f2382..c3e2437 100644 (file)
@@ -42,7 +42,9 @@ 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,
        IFACE_ATTR_RA_MANAGEMENT,
        IFACE_ATTR_RA_OFFLINK,
@@ -60,6 +62,7 @@ enum {
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
        IFACE_ATTR_NDPROXY_SLAVE,
+       IFACE_ATTR_PREFIX_FILTER,
        IFACE_ATTR_MAX
 };
 
@@ -82,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_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 },
        [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
@@ -100,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] = {
@@ -180,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;
 }
@@ -205,9 +211,10 @@ static struct interface* get_interface(const char *name)
 
 static void set_interface_defaults(struct interface *iface)
 {
-       iface->managed = 1;
        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;
@@ -231,13 +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->ia_addr);
+       free(iface->addr4);
+       free(iface->addr6);
        free(iface->ifname);
        free(iface);
 }
@@ -245,13 +255,13 @@ static void close_interface(struct interface *iface)
 static int parse_mode(const char *mode)
 {
        if (!strcmp(mode, "disabled"))
-               return RELAYD_DISABLED;
+               return MODE_DISABLED;
        else if (!strcmp(mode, "server"))
-               return RELAYD_SERVER;
+               return MODE_SERVER;
        else if (!strcmp(mode, "relay"))
-               return RELAYD_RELAY;
+               return MODE_RELAY;
        else if (!strcmp(mode, "hybrid"))
-               return RELAYD_HYBRID;
+               return MODE_HYBRID;
        else
                return -1;
 }
@@ -436,17 +446,23 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 
                if (!iface->ifname)
                        goto err;
-       }
 
-       if ((iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
-               goto err;
+               if (!iface->ifindex &&
+                   (iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
+                       goto err;
+       }
 
        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->addr6_len = len;
 
+               len = netlink_get_interface_addrs(iface->ifindex,
+                                               false, &iface->addr4);
                if (len > 0)
-                       iface->ia_addr_len = len;
+                       iface->addr4_len = len;
        }
 
        iface->inuse = true;
@@ -469,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 = RELAYD_SERVER;
+                       iface->dhcpv4 = MODE_SERVER;
        }
 
        if ((c = tb[IFACE_ATTR_LIMIT]))
@@ -611,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);
        }
 
+       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_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]))
-               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);
@@ -700,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:
@@ -766,13 +805,13 @@ void odhcpd_reload(void)
                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;
 
-               if (i->ra == RELAYD_HYBRID || i->ra == RELAYD_RELAY)
+               if (i->ra == MODE_HYBRID || i->ra == MODE_RELAY)
                        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;
        }
 
@@ -781,31 +820,31 @@ void odhcpd_reload(void)
                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))
-                       hybrid_mode = RELAYD_RELAY;
+                       hybrid_mode = MODE_RELAY;
 #endif
 
-               if (i->dhcpv6 == RELAYD_HYBRID)
+               if (i->dhcpv6 == MODE_HYBRID)
                        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;
 
-               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;
 
-               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;
        }
 
@@ -813,22 +852,24 @@ void odhcpd_reload(void)
        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);
        }
@@ -869,7 +910,7 @@ void odhcpd_run(void)
        signal(SIGHUP, handle_signal);
 
 #ifdef WITH_UBUS
-       while (init_ubus())
+       while (ubus_init())
                sleep(1);
 #endif