proto: move the dns search option handling to the core
[project/netifd.git] / interface-ip.c
index d22e369..93e8723 100644 (file)
@@ -18,7 +18,6 @@ enum {
        ROUTE_TARGET,
        ROUTE_MASK,
        ROUTE_GATEWAY,
-       ROUTE_DEVICE,
        ROUTE_METRIC,
        ROUTE_MTU,
        __ROUTE_MAX
@@ -29,7 +28,6 @@ static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
        [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
        [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
        [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
-       [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
        [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
        [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
 };
@@ -46,13 +44,9 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
        struct blob_attr *tb[__ROUTE_MAX], *cur;
        struct device_route *route;
        int af = v6 ? AF_INET6 : AF_INET;
-       bool config = false;
 
        blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
 
-       if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
-               return;
-
        if (!iface) {
                if ((cur = tb[ROUTE_INTERFACE]) == NULL)
                        return;
@@ -62,7 +56,6 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
                        return;
 
                ip = &iface->config_ip;
-               config = true;
        } else {
                ip = &iface->proto_ip;
        }
@@ -90,17 +83,18 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
                        DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
                        goto error;
                }
+       } else {
+               route->flags |= DEVADDR_DEVICE;
        }
 
        if ((cur = tb[ROUTE_METRIC]) != NULL)
                route->metric = blobmsg_get_u32(cur);
+       else
+               route->metric = -1;
 
        if ((cur = tb[ROUTE_MTU]) != NULL)
                route->mtu = blobmsg_get_u32(cur);
 
-       if (!config && (cur = tb[ROUTE_DEVICE]) != NULL)
-               route->device = device_get(blobmsg_data(cur), true);
-
        vlist_add(&ip->route, &route->node);
        return;
 
@@ -155,6 +149,15 @@ interface_update_proto_addr(struct vlist_tree *tree,
        }
 }
 
+static bool
+enable_route(struct interface_ip_settings *ip, struct device_route *route)
+{
+       if (ip->no_defaultroute && !route->mask)
+               return false;
+
+       return true;
+}
+
 static void
 interface_update_proto_route(struct vlist_tree *tree,
                             struct vlist_node *node_new,
@@ -183,9 +186,12 @@ interface_update_proto_route(struct vlist_tree *tree,
        }
 
        if (node_new) {
-               if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep)
+               bool _enabled = enable_route(ip, route_new);
+
+               if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
                        system_add_route(dev, route_new);
-               route_new->enabled = true;
+
+               route_new->enabled = _enabled;
        }
 }
 
@@ -341,14 +347,19 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
        }
 
        vlist_for_each_element(&ip->route, route, node) {
-               if (route->enabled == enabled)
+               bool _enabled = enabled;
+
+               if (!enable_route(ip, route))
+                       _enabled = false;
+
+               if (route->enabled == _enabled)
                        continue;
 
-               if (enabled)
+               if (_enabled)
                        system_add_route(dev, route);
                else
                        system_del_route(dev, route);
-               route->enabled = enabled;
+               route->enabled = _enabled;
        }
 }