Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74a3e5a7 authored by Eric W. Biederman's avatar Eric W. Biederman Committed by David S. Miller
Browse files

tun: Remove unnecessary tun_get_by_name



Currently the tun driver keeps a private list of tun devices for what
appears to be a small gain in performance when reconnecting a file
descriptor to an existing tun or tap device.  So simplify the code by
removing it.

Signed-off-by: default avatarEric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f5882c30
Loading
Loading
Loading
Loading
+19 −55
Original line number Original line Diff line number Diff line
@@ -88,7 +88,6 @@ struct tap_filter {
};
};


struct tun_struct {
struct tun_struct {
	struct list_head        list;
	unsigned int 		flags;
	unsigned int 		flags;
	int			attached;
	int			attached;
	uid_t			owner;
	uid_t			owner;
@@ -213,11 +212,6 @@ static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)


/* Network device part of the driver */
/* Network device part of the driver */


static int tun_net_id;
struct tun_net {
	struct list_head dev_list;
};

static const struct ethtool_ops tun_ethtool_ops;
static const struct ethtool_ops tun_ethtool_ops;


/* Net device open. */
/* Net device open. */
@@ -697,30 +691,22 @@ static void tun_setup(struct net_device *dev)
	dev->features |= NETIF_F_NETNS_LOCAL;
	dev->features |= NETIF_F_NETNS_LOCAL;
}
}


static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name)
{
	struct tun_struct *tun;

	ASSERT_RTNL();
	list_for_each_entry(tun, &tn->dev_list, list) {
		if (!strncmp(tun->dev->name, name, IFNAMSIZ))
		    return tun;
	}

	return NULL;
}

static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
{
{
	struct tun_net *tn;
	struct tun_struct *tun;
	struct tun_struct *tun;
	struct net_device *dev;
	struct net_device *dev;
	const struct cred *cred = current_cred();
	const struct cred *cred = current_cred();
	int err;
	int err;


	tn = net_generic(net, tun_net_id);
	dev = __dev_get_by_name(net, ifr->ifr_name);
	tun = tun_get_by_name(tn, ifr->ifr_name);
	if (dev) {
	if (tun) {
		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
			tun = netdev_priv(dev);
		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
			tun = netdev_priv(dev);
		else
			return -EINVAL;

		if (tun->attached)
		if (tun->attached)
			return -EBUSY;
			return -EBUSY;


@@ -733,8 +719,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
			return -EPERM;
			return -EPERM;
		}
		}
	}
	}
	else if (__dev_get_by_name(net, ifr->ifr_name))
		return -EINVAL;
	else {
	else {
		char *name;
		char *name;
		unsigned long flags = 0;
		unsigned long flags = 0;
@@ -782,8 +766,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
		err = register_netdevice(tun->dev);
		err = register_netdevice(tun->dev);
		if (err < 0)
		if (err < 0)
			goto err_free_dev;
			goto err_free_dev;

		list_add(&tun->list, &tn->dev_list);
	}
	}


	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
@@ -1095,10 +1077,8 @@ static int tun_chr_close(struct inode *inode, struct file *file)
	/* Drop read queue */
	/* Drop read queue */
	skb_queue_purge(&tun->readq);
	skb_queue_purge(&tun->readq);


	if (!(tun->flags & TUN_PERSIST)) {
	if (!(tun->flags & TUN_PERSIST))
		list_del(&tun->list);
		unregister_netdevice(tun->dev);
		unregister_netdevice(tun->dev);
	}


	rtnl_unlock();
	rtnl_unlock();


@@ -1212,37 +1192,21 @@ static const struct ethtool_ops tun_ethtool_ops = {


static int tun_init_net(struct net *net)
static int tun_init_net(struct net *net)
{
{
	struct tun_net *tn;

	tn = kmalloc(sizeof(*tn), GFP_KERNEL);
	if (tn == NULL)
		return -ENOMEM;

	INIT_LIST_HEAD(&tn->dev_list);

	if (net_assign_generic(net, tun_net_id, tn)) {
		kfree(tn);
		return -ENOMEM;
	}

	return 0;
	return 0;
}
}


static void tun_exit_net(struct net *net)
static void tun_exit_net(struct net *net)
{
{
	struct tun_net *tn;
	struct net_device *dev, *next;
	struct tun_struct *tun, *nxt;

	tn = net_generic(net, tun_net_id);


	rtnl_lock();
	rtnl_lock();
	list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
	for_each_netdev_safe(net, dev, next) {
		DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
		if (dev->ethtool_ops != &tun_ethtool_ops)
		unregister_netdevice(tun->dev);
			continue;
		DBG(KERN_INFO "%s cleaned up\n", dev->name);
		unregister_netdevice(dev);
	}
	}
	rtnl_unlock();
	rtnl_unlock();

	kfree(tn);
}
}


static struct pernet_operations tun_net_ops = {
static struct pernet_operations tun_net_ops = {
@@ -1257,7 +1221,7 @@ static int __init tun_init(void)
	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);


	ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
	ret = register_pernet_device(&tun_net_ops);
	if (ret) {
	if (ret) {
		printk(KERN_ERR "tun: Can't register pernet ops\n");
		printk(KERN_ERR "tun: Can't register pernet ops\n");
		goto err_pernet;
		goto err_pernet;
@@ -1271,7 +1235,7 @@ static int __init tun_init(void)
	return 0;
	return 0;


err_misc:
err_misc:
	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
	unregister_pernet_device(&tun_net_ops);
err_pernet:
err_pernet:
	return ret;
	return ret;
}
}
@@ -1279,7 +1243,7 @@ static int __init tun_init(void)
static void tun_cleanup(void)
static void tun_cleanup(void)
{
{
	misc_deregister(&tun_miscdev);
	misc_deregister(&tun_miscdev);
	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
	unregister_pernet_device(&tun_net_ops);
}
}


module_init(tun_init);
module_init(tun_init);