netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Hemminger <shemminger@osdl.org>
To: jt@hpl.hp.com
Cc: jt@bougret.hpl.hp.com, Jeff Garzik <jgarzik@pobox.com>,
	"David S. Miller" <davem@redhat.com>,
	netdev@oss.sgi.com,
	Linux kernel mailing list <linux-kernel@vger.kernel.org>
Subject: [PATCH 2.6.X] SIOCSIFNAME wilcard suppor & name validation
Date: Tue, 13 Jan 2004 14:22:04 -0800	[thread overview]
Message-ID: <20040113142204.0b41403b.shemminger@osdl.org> (raw)
In-Reply-To: <20040112234332.GA1785@bougret.hpl.hp.com>

Here is an enhanced version of the previous patch.
It adds both the wildcard support that Jean did, and validation of network
device names.

It doesn't check the error return from class_device_rename because
that will not fail unless object or name are null.

diff -Nru a/net/core/dev.c b/net/core/dev.c
--- a/net/core/dev.c	Tue Jan 13 14:20:51 2004
+++ b/net/core/dev.c	Tue Jan 13 14:20:51 2004
@@ -621,6 +621,21 @@
 }
 
 /**
+ *	dev_valid_name - check if name is okay for network device
+ *	@name: name string
+ *
+ *	Network device names need to be valid file names to
+ *	to allow sysfs to work
+ */
+int dev_valid_name(const char *name)
+{
+	return !(*name == '\0' 
+		 || !strcmp(name, ".")
+		 || !strcmp(name, "..")
+		 || strchr(name, '/'));
+}
+
+/**
  *	dev_alloc_name - allocate a name for a device
  *	@dev: device
  *	@name: name format string
@@ -660,6 +675,41 @@
 	return -ENFILE;	/* Over 100 of the things .. bail out! */
 }
 
+
+/**
+ *	dev_change_name - change name of a device
+ *	@dev: device
+ *	@name: name (or format string) must be at least IFNAMSIZ
+ *
+ *	Change name of a device, can pass format strings "eth%d".
+ *	for wildcarding.
+ */
+int dev_change_name(struct net_device *dev, char *newname)
+{
+	ASSERT_RTNL();
+
+	if (dev->flags & IFF_UP)
+		return -EBUSY;
+
+	if (!dev_valid_name(newname))
+		return -EINVAL;
+
+	if (strchr(newname, '%')) {
+		int err = dev_alloc_name(dev, newname);
+		if (err)
+			return err;
+		strcpy(newname, dev->name);
+	}
+	else if (__dev_get_by_name(newname))
+		return -EEXIST;
+	else
+		strlcpy(dev->name, newname, IFNAMSIZ);
+
+	class_device_rename(&dev->class_dev, dev->name);
+	notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
+	return 0;
+}
+
 /**
  *	dev_alloc - allocate a network device and name
  *	@name: name format string
@@ -2359,20 +2409,8 @@
 			return 0;
 
 		case SIOCSIFNAME:
-			if (dev->flags & IFF_UP)
-				return -EBUSY;
 			ifr->ifr_newname[IFNAMSIZ-1] = '\0';
-			if (__dev_get_by_name(ifr->ifr_newname))
-				return -EEXIST;
-			err = class_device_rename(&dev->class_dev, 
-						  ifr->ifr_newname);
-			if (!err) {
-				strlcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);
-
-				notifier_call_chain(&netdev_chain,
-						    NETDEV_CHANGENAME, dev);
-			}
-			return err;
+			return dev_change_name(dev, ifr->ifr_newname);
 
 		/*
 		 *	Unknown or private ioctl
@@ -2505,6 +2543,7 @@
 		 */
 		case SIOCGMIIPHY:
 		case SIOCGMIIREG:
+		case SIOCSIFNAME:
 			if (!capable(CAP_NET_ADMIN))
 				return -EPERM;
 			dev_load(ifr.ifr_name);
@@ -2536,7 +2575,6 @@
 		case SIOCDELMULTI:
 		case SIOCSIFHWBROADCAST:
 		case SIOCSIFTXQLEN:
-		case SIOCSIFNAME:
 		case SIOCSMIIREG:
 		case SIOCBONDENSLAVE:
 		case SIOCBONDRELEASE:
@@ -2685,6 +2723,11 @@
 				ret = -EIO;
 			goto out_err;
 		}
+	}
+ 
+	if (!dev_valid_name(dev->name)) {
+		ret = -EINVAL;
+		goto out_err;
 	}
 
 	dev->ifindex = dev_new_index();

  reply	other threads:[~2004-01-13 22:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-12 23:43 [PATCH 2.6.X] SIOCSIFNAME wilcard support Jean Tourrilhes
2004-01-13 22:22 ` Stephen Hemminger [this message]
2004-01-13 22:27   ` [PATCH 2.6.X] SIOCSIFNAME wilcard suppor & name validation Jean Tourrilhes
2004-01-14  0:21   ` David S. Miller
2004-01-15  0:13     ` Stephen Hemminger
2004-01-15  0:17       ` Jean Tourrilhes
2004-01-15  8:49       ` David S. Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040113142204.0b41403b.shemminger@osdl.org \
    --to=shemminger@osdl.org \
    --cc=davem@redhat.com \
    --cc=jgarzik@pobox.com \
    --cc=jt@bougret.hpl.hp.com \
    --cc=jt@hpl.hp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@oss.sgi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).