[LEDE-DEV] [PATCH] netifd: Set source address for static address routes

Kristian Evensen kristian.evensen at gmail.com
Thu Oct 6 01:52:06 PDT 2016


When using UCI to configure static addresses, netifd does not set the source
address of the gateway route. In order to be consistent, also set the source
address for subnet routes (this applies to all protocols).

Signed-off-by: Kristian Evensen <kristian.evensen at gmail.com>
---
 interface-ip.c |  4 ++++
 proto.c        | 32 ++++++++++++++++++++++----------
 2 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/interface-ip.c b/interface-ip.c
index 24ea054..7fce833 100644
--- a/interface-ip.c
+++ b/interface-ip.c
@@ -457,6 +457,7 @@ prefix_cmp(const void *k1, const void *k2, void *ptr)
 static void
 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
 {
+	bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6);
 	struct device *dev = iface->l3_dev.dev;
 	struct device_route *r = &addr->subnet;
 
@@ -484,6 +485,9 @@ interface_handle_subnet_route(struct interface *iface, struct device_addr *addr,
 	r->flags &= ~DEVADDR_KERNEL;
 	interface_set_route_info(iface, r);
 
+	r->sourcemask = v6 ? 128 : 32;
+	r->source = addr->addr;
+
 	system_add_route(dev, r);
 }
 
diff --git a/proto.c b/proto.c
index 23304f3..03973e5 100644
--- a/proto.c
+++ b/proto.c
@@ -113,7 +113,7 @@ alloc_device_addr(bool v6, bool ext)
 
 static bool
 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
-	   bool ext, uint32_t broadcast)
+	   bool ext, uint32_t broadcast, struct device_addr **addr_to_set)
 {
 	struct device_addr *addr;
 	int af = v6 ? AF_INET6 : AF_INET;
@@ -137,6 +137,10 @@ parse_addr(struct interface *iface, const char *str, bool v6, int mask,
 		addr->broadcast = broadcast;
 
 	vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
+
+	if (addr_to_set)
+		*addr_to_set = addr;
+
 	return true;
 
 error:
@@ -148,7 +152,8 @@ error:
 
 static int
 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
-			    bool v6, int netmask, bool ext, uint32_t broadcast)
+			    bool v6, int netmask, bool ext, uint32_t broadcast,
+				struct device_addr **addr_to_set)
 {
 	struct blob_attr *cur;
 	int n_addr = 0;
@@ -160,7 +165,7 @@ parse_static_address_option(struct interface *iface, struct blob_attr *attr,
 
 		n_addr++;
 		if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
-				broadcast))
+				broadcast, addr_to_set))
 			return -1;
 	}
 
@@ -268,7 +273,8 @@ parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
 }
 
 static bool
-parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
+parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6,
+					struct device_addr *addr_to_set)
 {
 	struct device_route *route;
 	const char *str = blobmsg_data(attr);
@@ -294,6 +300,11 @@ parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
 		route->flags |= DEVROUTE_SRCTABLE;
 	}
 
+	if (addr_to_set) {
+		route->sourcemask = v6 ? 128 : 32;
+		route->source = addr_to_set->addr;
+	}
+
 	vlist_add(&iface->proto_ip.route, &route->node, route);
 
 	return true;
@@ -402,6 +413,7 @@ proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
 	unsigned int netmask = 32;
 	int n_v4 = 0, n_v6 = 0;
 	struct in_addr bcast = {};
+	struct device_addr *addr_to_set;
 
 	blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
 
@@ -422,11 +434,11 @@ proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
 
 	if ((cur = tb[OPT_IPADDR]))
 		n_v4 = parse_static_address_option(iface, cur, false,
-			netmask, false, bcast.s_addr);
+			netmask, false, bcast.s_addr, &addr_to_set);
 
 	if ((cur = tb[OPT_IP6ADDR]))
 		n_v6 = parse_static_address_option(iface, cur, true,
-			128, false, 0);
+			128, false, 0, &addr_to_set);
 
 	if ((cur = tb[OPT_IP6PREFIX]))
 		if (parse_prefix_list(iface, cur) < 0)
@@ -436,12 +448,12 @@ proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
 		goto out;
 
 	if ((cur = tb[OPT_GATEWAY])) {
-		if (n_v4 && !parse_gateway_option(iface, cur, false))
+		if (n_v4 && !parse_gateway_option(iface, cur, false, addr_to_set))
 			goto out;
 	}
 
 	if ((cur = tb[OPT_IP6GW])) {
-		if (n_v6 && !parse_gateway_option(iface, cur, true))
+		if (n_v6 && !parse_gateway_option(iface, cur, true, addr_to_set))
 			goto out;
 	}
 
@@ -476,12 +488,12 @@ proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ex
 		goto out;
 
 	if ((cur = tb[OPT_GATEWAY])) {
-		if (n_v4 && !parse_gateway_option(iface, cur, false))
+		if (n_v4 && !parse_gateway_option(iface, cur, false, NULL))
 			goto out;
 	}
 
 	if ((cur = tb[OPT_IP6GW])) {
-		if (n_v6 && !parse_gateway_option(iface, cur, true))
+		if (n_v6 && !parse_gateway_option(iface, cur, true, NULL))
 			goto out;
 	}
 
-- 
2.5.0




More information about the Lede-dev mailing list