netdev
[Top] [All Lists]

[PATCH] rtnetlink: link attribute modification by interface name

To: "David S. Miller" <davem@xxxxxxxxxxxxx>
Subject: [PATCH] rtnetlink: link attribute modification by interface name
From: Thomas Graf <tgraf@xxxxxxx>
Date: Tue, 30 Nov 2004 23:06:59 +0100
Cc: netdev@xxxxxxxxxxx
Sender: netdev-bounce@xxxxxxxxxxx
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.

Signed-off-by: Thomas Graf <tgraf@xxxxxxx>

--- linux-2.6.10-rc2-bk13.orig/net/core/rtnetlink.c     2004-11-30 
14:01:12.000000000 +0100
+++ linux-2.6.10-rc2-bk13/net/core/rtnetlink.c  2004-11-30 14:23:23.000000000 
+0100
@@ -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));

<Prev in Thread] Current Thread [Next in Thread>