[PATCH 7/8] netns: Enable tagging for net_class directories in sysfs

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Eric W. Biederman
Date: Wednesday, August 20, 2008 - 11:39 pm

The problem.  Network devices show up in sysfs and with the network
namespace active multiple devices with the same name can show up in
the same directory, ouch!

To avoid that problem and allow existing applications in network namespaces
to see the same interface that is currently presented in sysfs, this
patch enables the tagging directory support in sysfs.

By using the network namespace pointers as tags to separate out the
the sysfs directory entries we ensure that we don't have conflicts
in the directories and applications only see a limited set of
the network devices.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
---
 include/linux/sysfs.h |    1 +
 net/Kconfig           |    2 +-
 net/core/net-sysfs.c  |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletions(-)

diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 4e1bfdb..6d7eb50 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -82,6 +82,7 @@ struct sysfs_dirent;
 
 enum sysfs_tag_type {
 	SYSFS_TAG_TYPE_NONE = 0,
+	SYSFS_TAG_TYPE_NETNS,
 	SYSFS_TAG_TYPES
 };
 
diff --git a/net/Kconfig b/net/Kconfig
index 7612cc8..29e7e09 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -27,7 +27,7 @@ menu "Networking options"
 config NET_NS
 	bool "Network namespace support"
 	default n
-	depends on EXPERIMENTAL && !SYSFS && NAMESPACES
+	depends on EXPERIMENTAL && NAMESPACES
 	help
 	  Allow user space to create what appear to be multiple instances
 	  of the network stack.
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index c9968f1..e4687af 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -13,7 +13,9 @@
 #include <linux/kernel.h>
 #include <linux/netdevice.h>
 #include <linux/if_arp.h>
+#include <linux/nsproxy.h>
 #include <net/sock.h>
+#include <net/net_namespace.h>
 #include <linux/rtnetlink.h>
 #include <linux/wireless.h>
 #include <net/iw_handler.h>
@@ -385,6 +387,24 @@ static struct attribute_group wireless_group = {
 };
 #endif
 
+static const void *net_sysfs_mount_tag(void)
+{
+	return current->nsproxy->net_ns;
+}
+
+static struct sysfs_tag_type_operations net_tag_type_operations = {
+	.mount_tag = net_sysfs_mount_tag,
+};
+
+static void net_sysfs_net_exit(struct net *net)
+{
+	sysfs_exit_tag(SYSFS_TAG_TYPE_NETNS, net);
+}
+
+static struct pernet_operations sysfs_net_ops = {
+	.exit = net_sysfs_net_exit,
+};
+
 #endif /* CONFIG_SYSFS */
 
 #ifdef CONFIG_HOTPLUG
@@ -421,6 +441,13 @@ static void netdev_release(struct device *d)
 	kfree((char *)dev - dev->padded);
 }
 
+static const void *net_sysfs_tag(struct device *d)
+{
+	struct net_device *dev;
+	dev = container_of(d, struct net_device, dev);
+	return dev_net(dev);
+}
+
 static struct class net_class = {
 	.name = "net",
 	.dev_release = netdev_release,
@@ -430,6 +457,8 @@ static struct class net_class = {
 #ifdef CONFIG_HOTPLUG
 	.dev_uevent = netdev_uevent,
 #endif
+	.tag_type = SYSFS_TAG_TYPE_NETNS,
+	.sysfs_tag = net_sysfs_tag,
 };
 
 /* Delete sysfs entries but hold kobject reference until after all
@@ -484,5 +513,9 @@ EXPORT_SYMBOL(netdev_class_remove_file);
 
 int netdev_kobject_init(void)
 {
+#ifdef CONFIG_SYSFS
+	sysfs_register_tag_type(SYSFS_TAG_TYPE_NETNS, &net_tag_type_operations);
+	register_pernet_subsys(&sysfs_net_ops);
+#endif
 	return class_register(&net_class);
 }
-- 
1.5.3.rc6.17.g1911

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jun 26, 1:21 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Mon Jun 30, 11:56 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 12:50 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 2:20 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 5:30 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 8:53 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Wed Jul 2, 9:49 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Wed Jul 2, 10:11 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jul 3, 5:27 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jul 3, 12:57 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jul 3, 1:08 pm)
[PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Thu Jul 3, 5:48 pm)
[PATCH 02/15] sysfs: Support for preventing unmounts., Eric W. Biederman, (Thu Jul 3, 6:07 pm)
[PATCH 03/15] sysfs: sysfs_get_dentry add a sb parameter, Eric W. Biederman, (Thu Jul 3, 6:08 pm)
[PATCH 04/15] sysfs: Implement __sysfs_get_dentry, Eric W. Biederman, (Thu Jul 3, 6:09 pm)
[PATCH 05/15] sysfs: Rename Support multiple superblocks, Eric W. Biederman, (Thu Jul 3, 6:10 pm)
[PATCH 06/15] Introduce sysfs_sd_setattr and fix sysfs_chmod, Eric W. Biederman, (Thu Jul 3, 6:11 pm)
[PATCH 08/15] sysfs: Make sysfs_mount static once again., Eric W. Biederman, (Thu Jul 3, 6:14 pm)
Re: [PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Thu Jul 3, 6:27 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 4, 6:31 am)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 4, 2:49 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 4, 3:00 pm)
Re: [PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Sat Jul 5, 9:42 pm)
Re: [PATCH 00/15] sysfs support for namespaces, Cornelia Huck, (Mon Jul 7, 4:41 am)
Re: [PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Mon Jul 7, 5:22 am)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Sun Jul 13, 6:54 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Tue Jul 15, 10:41 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Tue Jul 15, 11:32 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Wed Jul 16, 12:07 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Wed Jul 16, 2:09 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 18, 1:19 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Sat Aug 2, 11:59 pm)
Re: [PATCH 09/15] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Aug 19, 11:58 pm)
[PATCH 0/8] sysfs namespace support, Eric W. Biederman, (Wed Aug 20, 11:31 pm)
[PATCH 1/8] sysfs: Implement sysfs tagged directory support., Eric W. Biederman, (Wed Aug 20, 11:33 pm)
[PATCH 2/8] sysfs: Merge sysfs_rename_dir and sysfs_move_dir, Eric W. Biederman, (Wed Aug 20, 11:34 pm)
[PATCH 3/8] sysfs: Implement sysfs_delete_link and sysfs_r ..., Eric W. Biederman, (Wed Aug 20, 11:35 pm)
[PATCH 5/8] sysfs: Remove sysfs_create_link_nowarn, Eric W. Biederman, (Wed Aug 20, 11:36 pm)
Re: [PATCH 0/8] sysfs namespace support, David Miller, (Wed Aug 20, 11:37 pm)
[PATCH 4/8] driver core: Implement tagged directory suppor ..., Eric W. Biederman, (Wed Aug 20, 11:37 pm)
[PATCH 7/8] netns: Enable tagging for net_class directorie ..., Eric W. Biederman, (Wed Aug 20, 11:39 pm)
[PATCH 8/8] sysfs: user namespaces: fix bug with clone(CLO ..., Eric W. Biederman, (Wed Aug 20, 11:40 pm)
Re: [PATCH 1/8] sysfs: Implement sysfs tagged directory su ..., Eric W. Biederman, (Mon Oct 13, 8:20 pm)