wireless: fix blob buf in put_container()
[project/netifd.git] / interface-ip.c
index bd6f164..62d15fd 100644 (file)
@@ -39,6 +39,7 @@ enum {
        ROUTE_TABLE,
        ROUTE_SOURCE,
        ROUTE_ONLINK,
+       ROUTE_TYPE,
        __ROUTE_MAX
 };
 
@@ -53,6 +54,7 @@ static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
        [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
        [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
        [ROUTE_ONLINK] = { .name = "onlink", .type = BLOBMSG_TYPE_BOOL },
+       [ROUTE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }
 };
 
 const struct uci_blob_param_list route_attr_list = {
@@ -73,8 +75,8 @@ clear_if_addr(union if_addr *a, int mask)
        uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
        uint8_t *p = (uint8_t *) a;
 
-       if (m_bytes < sizeof(a))
-               memset(p + m_bytes, 0, sizeof(a) - m_bytes);
+       if (m_bytes < sizeof(*a))
+               memset(p + m_bytes, 0, sizeof(*a) - m_bytes);
 
        p[m_bytes - 1] &= ~m_clear;
 }
@@ -363,6 +365,10 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
                        goto error;
                }
 
+               /* only set the table flag if not using the main (default) table */
+               if (system_is_default_rt_table(route->table))
+                       route->table = 0;
+
                if (route->table)
                        route->flags |= DEVROUTE_TABLE;
        }
@@ -374,6 +380,14 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
                        route->valid_until = valid_until;
        }
 
+       if ((cur = tb[ROUTE_TYPE]) != NULL) {
+               if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
+                       DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur));
+                       goto error;
+               }
+               route->flags |= DEVROUTE_TYPE;
+       }
+
        vlist_add(&ip->route, &route->node, route);
        return;
 
@@ -586,7 +600,8 @@ interface_update_proto_route(struct vlist_tree *tree,
 
        if (node_old && node_new)
                keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
-                       (route_old->mtu == route_new->mtu) && !route_old->failed;
+                       (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) &&
+                       (route_old->valid_until == route_new->valid_until) && !route_old->failed;
 
        if (node_old) {
                if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
@@ -654,13 +669,20 @@ interface_set_prefix_address(struct device_prefix_assignment *assignment,
        addr.preferred_until = prefix->preferred_until;
        addr.valid_until = prefix->valid_until;
 
+       if (addr.mask < 64) {
+               addr.mask = 64;
+               system_del_address(l3_downlink, &addr);
+               addr.mask = assignment->length;
+       }
+
        if (!add && assignment->enabled) {
                time_t now = system_get_rtime();
                addr.preferred_until = now;
                if (!addr.valid_until || addr.valid_until - now > 7200)
                        addr.valid_until = now + 7200;
+
                system_del_address(l3_downlink, &addr); // Work around dangling prefix routes
-               system_add_address(l3_downlink, &addr);
+
                if (prefix->iface) {
                        if (prefix->iface->ip6table)
                                set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
@@ -670,9 +692,17 @@ interface_set_prefix_address(struct device_prefix_assignment *assignment,
                                                        addr.mask, 0, iface, "unreachable");
                }
 
+               if (addr.mask < 64)
+                       addr.mask = 64;
+
+               interface_handle_subnet_route(iface, &addr, false);
+               system_add_address(l3_downlink, &addr);
+
                assignment->enabled = false;
        } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP) &&
                        !system_add_address(l3_downlink, &addr)) {
+               interface_handle_subnet_route(iface, &addr, false);
+
                if (prefix->iface && !assignment->enabled) {
                        set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
                                        addr.mask, 0, iface, "unreachable");
@@ -681,12 +711,19 @@ interface_set_prefix_address(struct device_prefix_assignment *assignment,
                                set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
                                                addr.mask, prefix->iface->ip6table, iface, NULL);
                }
+
+               if (addr.mask < 64)
+                       addr.mask = 64;
+
+               interface_handle_subnet_route(iface, &addr, true);
+
                if (uplink && uplink->l3_dev.dev) {
                        int mtu = system_update_ipv6_mtu(
                                        uplink->l3_dev.dev, 0);
                        if (mtu > 0)
                                system_update_ipv6_mtu(l3_downlink, mtu);
                }
+
                assignment->enabled = true;
        }
 }
@@ -1223,7 +1260,6 @@ interface_ip_init(struct interface *iface)
        __interface_ip_init(&iface->proto_ip, iface);
        __interface_ip_init(&iface->config_ip, iface);
        vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
-
 }
 
 static void