Skip to contentSkip to conversation

rtnetlink: link attribute modification by interface name

PATCH In review Thomas Graf 2 participants 2 messages Nov 30, 2004 – Dec 1, 2004

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtnetlink: link attribute modification by interface name

Dave,

This patch allows modification of link attributes by the interface name, avoids the requirement of translating a name to an ifindex first, and provides better atomicity to userspace. It works by setting the ifindex to a negative number and provide the interface name via the IFLA_IFNAME TLV and let the kernel lookup the device by name instead of ifindex. Changing the interface name will not work using this method because IFLA_IFNAME also transports the new interface name. The patch also fixes a possible source for bugs if IFNAMSIZ is ever changed to a number not aligned to RTA_ALIGNTO.

net/core/rtnetlink.c +18 -3
@@ -267,7 +267,22 @@
 	struct net_device *dev;
 	int err, send_addr_notify = 0;
 
-	dev = dev_get_by_index(ifm->ifi_index);
+	if (ifm->ifi_index >= 0)
+		dev = dev_get_by_index(ifm->ifi_index);
+	else if (ida[IFLA_IFNAME - 1]) {
+		char ifname[IFNAMSIZ];
+
+		if (RTA_PAYLOAD(ida[IFLA_IFNAME - 1]) > RTA_ALIGN(sizeof(ifname)))
+			return -EINVAL;
+
+		memset(ifname, 0, sizeof(ifname));
+		memcpy(ifname, RTA_DATA(ida[IFLA_IFNAME - 1]),
+			RTA_PAYLOAD(ida[IFLA_IFNAME - 1]));
+		ifname[IFNAMSIZ - 1] = '\0';
+		dev = dev_get_by_name(ifname);
+	} else
+		return -EINVAL;
+
 	if (!dev)
 		return -ENODEV;
 
@@ -358,10 +373,10 @@
 		dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
 	}
 
-	if (ida[IFLA_IFNAME - 1]) {
+	if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
 		char ifname[IFNAMSIZ];
 
-		if (ida[IFLA_IFNAME - 1]->rta_len > RTA_LENGTH(sizeof(ifname)))
+		if (RTA_PAYLOAD(ida[IFLA_IFNAME - 1]) > RTA_ALIGN(sizeof(ifname)))
 			goto out;
 
 		memset(ifname, 0, sizeof(ifname));
maintainer
#1 lore raw
Quoted 8 lines · On Tue, 30 Nov 2004 23:06:59 +0100 Thomas Graf <[email protected]> wrote:

This patch allows modification of link attributes by the interface name, avoids the requirement of translating a name to an ifindex first, and provides better atomicity to userspace. It works by setting the ifindex to a negative number and provide the interface name via the IFLA_IFNAME TLV and let the kernel lookup the device by name instead of ifindex. Changing the interface name will not work using this method because IFLA_IFNAME also transports the new interface name. The patch also fixes a possible source for bugs if IFNAMSIZ is ever changed to a number not aligned to RTA_ALIGNTO.

Looks good, applied to my 2.6.11 pending tree.

Thanks.