add a generic option for disabling the automatic default route on interfaces
authorFelix Fietkau <nbd@openwrt.org>
Mon, 23 Jan 2012 23:52:32 +0000 (00:52 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 23 Jan 2012 23:52:32 +0000 (00:52 +0100)
interface-ip.c
interface.c
interface.h
utils.h

index d22e3692289cc88b51e7059b28c14cfc7651ce23..9bdad4fd32ca9b31015b5d1c491acd0cfb6643c9 100644 (file)
@@ -155,6 +155,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 +192,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 +353,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;
        }
 }
 
index 257eaca5a705310e916b4941c43258c15d1b77ed..1db2efe4ae8e317933ae3e0d5a47daec5c081a60 100644 (file)
@@ -17,6 +17,7 @@ enum {
        IFACE_ATTR_IFNAME,
        IFACE_ATTR_PROTO,
        IFACE_ATTR_AUTO,
+       IFACE_ATTR_DEFAULTROUTE,
        IFACE_ATTR_MAX
 };
 
@@ -24,6 +25,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
+       [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
 };
 
 const struct config_param_list interface_attr_list = {
@@ -326,10 +328,10 @@ interface_init(struct interface *iface, const char *name,
 
        proto_attach_interface(iface, proto_name);
 
-       if ((cur = tb[IFACE_ATTR_AUTO]))
-               iface->autostart = blobmsg_get_bool(cur);
-       else
-               iface->autostart = true;
+       iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
+       iface->proto_ip.no_defaultroute =
+               !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
+
        iface->config_autostart = iface->autostart;
 }
 
@@ -499,6 +501,11 @@ interface_change_config(struct interface *if_old, struct interface *if_new)
                goto reload;
        }
 
+       if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
+               if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
+               interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
+       }
+
        goto out;
 
 reload:
index e50ccf69c12203c52820bae6d487c784fae9dc73..4580c5b9e848710ddfe25994a4b6bb1fd3cbfb1e 100644 (file)
@@ -42,6 +42,7 @@ struct interface_user {
 struct interface_ip_settings {
        struct interface *iface;
        bool enabled;
+       bool no_defaultroute;
 
        struct vlist_tree addr;
        struct vlist_tree route;
diff --git a/utils.h b/utils.h
index b5ae7e00002d137e29b76792a0d50a9b59b20476..2866280b6173e9135b11318594049cb1c0f75fe5 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -3,6 +3,15 @@
 
 #include <libubox/list.h>
 #include <libubox/avl.h>
+#include <libubox/blobmsg.h>
+
+static inline bool blobmsg_get_bool_default(struct blob_attr *attr, bool val)
+{
+       if (!attr)
+               return val;
+
+       return blobmsg_get_bool(attr);
+}
 
 #define __init __attribute__((constructor))