From: Felix Fietkau Date: Mon, 23 Jan 2012 23:52:32 +0000 (+0100) Subject: add a generic option for disabling the automatic default route on interfaces X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c6b739e28f2b15789c95dd28af3f193ec8f67531;p=oweals%2Fnetifd.git add a generic option for disabling the automatic default route on interfaces --- diff --git a/interface-ip.c b/interface-ip.c index d22e369..9bdad4f 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -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; } } diff --git a/interface.c b/interface.c index 257eaca..1db2efe 100644 --- a/interface.c +++ b/interface.c @@ -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: diff --git a/interface.h b/interface.h index e50ccf6..4580c5b 100644 --- a/interface.h +++ b/interface.h @@ -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 b5ae7e0..2866280 100644 --- a/utils.h +++ b/utils.h @@ -3,6 +3,15 @@ #include #include +#include + +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))