[OpenWrt-Devel] [PATCH 1/4] netifd: Fix device ifindex overwrite when processing netlink event messages

Hans Dedecker dedeckeh at gmail.com
Thu Nov 13 08:35:47 EST 2014


When a device with the same name is deleted and created again in the kernel
the ifindex changes.
A race condition will occur when netlink event messages linked to the old device
are processed and will thus overwrite the correct ifindex of the new device.
Further make sure a valid ifindex is in place for both external and internal
devices when setting the state to enabled.

Signed-off-by: Hans Dedecker <dedeckeh at gmail.com>
---
 device.c       |    8 ++++++++
 system-dummy.c |    7 +++++++
 system-linux.c |    9 ++-------
 system.h       |    2 ++
 4 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/device.c b/device.c
index b29d73b..5d1dcac 100644
--- a/device.c
+++ b/device.c
@@ -62,6 +62,14 @@ void device_unlock(void)
 
 static int set_device_state(struct device *dev, bool state)
 {
+	if (state) {
+		/* Set ifindex for all devices being enabled so a valid  */
+		/* ifindex is in place avoiding possible race conditions */
+		device_set_ifindex(dev, system_if_resolve(dev));
+		if (!dev->ifindex)
+			return -1;
+	}
+
 	if (dev->external)
 		return 0;
 
diff --git a/system-dummy.c b/system-dummy.c
index 8e420e1..94b3308 100644
--- a/system-dummy.c
+++ b/system-dummy.c
@@ -96,6 +96,13 @@ int system_if_check(struct device *dev)
 	return 0;
 }
 
+int system_if_resolve(struct device *dev)
+{
+	dev->ifindex = 0;
+
+	return 0;
+}
+
 struct device *
 system_if_get_parent(struct device *dev)
 {
diff --git a/system-linux.c b/system-linux.c
index 02574fc..6a6028c 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -316,11 +316,7 @@ static int cb_rtnl_event(struct nl_msg *msg, void *arg)
 		goto out;
 
 	struct device *dev = device_get(nla_data(nla[IFLA_IFNAME]), false);
-	if (!dev)
-		goto out;
-
-	device_set_ifindex(dev, ifi->ifi_index);
-	if (dev->type->keep_link_status)
+	if (!dev || dev->type->keep_link_status)
 		goto out;
 
 	if (!system_get_dev_sysctl("/sys/class/net/%s/carrier", dev->ifname, buf, sizeof(buf)))
@@ -497,7 +493,7 @@ int system_bridge_delif(struct device *bridge, struct device *dev)
 	return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
 }
 
-static int system_if_resolve(struct device *dev)
+int system_if_resolve(struct device *dev)
 {
 	struct ifreq ifr;
 	strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
@@ -1006,7 +1002,6 @@ int system_if_up(struct device *dev)
 {
 	system_if_get_settings(dev, &dev->orig_settings);
 	system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
-	device_set_ifindex(dev, system_if_resolve(dev));
 	return system_if_flags(dev->ifname, IFF_UP, 0);
 }
 
diff --git a/system.h b/system.h
index b1215d1..3bf6f97 100644
--- a/system.h
+++ b/system.h
@@ -116,6 +116,8 @@ void system_if_clear_state(struct device *dev);
 int system_if_up(struct device *dev);
 int system_if_down(struct device *dev);
 int system_if_check(struct device *dev);
+int system_if_resolve(struct device *dev);
+
 int system_if_dump_info(struct device *dev, struct blob_buf *b);
 int system_if_dump_stats(struct device *dev, struct blob_buf *b);
 struct device *system_if_get_parent(struct device *dev);
-- 
1.7.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list