Commit fb7589a1 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by David S. Miller

tun: Add ability to create tun device with given index

Tun devices cannot be created with ifidex user wants, but it's
required by checkpoint-restore project.

Long time ago such ability was implemented for rtnl_ops-based
interface for creating links (9c7dafbf net: Allow to create links
with given ifindex), but the only API for creating and managing
tuntap devices is ioctl-based and is evolving with adding new ones
(cde8b15f tuntap: add ioctl to attach or detach a file form tuntap
device).

Following that trend, here's how a new ioctl that sets the ifindex
for device, that _will_ be created by TUNSETIFF ioctl looks like.
So those who want a tuntap device with the ifindex N, should open
the tun device, call ioctl(fd, TUNSETIFINDEX, &N), then call TUNSETIFF.
If the index N is busy, then the register_netdev will find this out
and the ioctl would be failed with -EBUSY.

If setifindex is not called, then it will be generated as before.
Signed-off-by: default avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 5f1cd200
...@@ -138,7 +138,10 @@ struct tun_file { ...@@ -138,7 +138,10 @@ struct tun_file {
struct fasync_struct *fasync; struct fasync_struct *fasync;
/* only used for fasnyc */ /* only used for fasnyc */
unsigned int flags; unsigned int flags;
u16 queue_index; union {
u16 queue_index;
unsigned int ifindex;
};
struct list_head next; struct list_head next;
struct tun_struct *detached; struct tun_struct *detached;
}; };
...@@ -1601,6 +1604,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) ...@@ -1601,6 +1604,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
dev_net_set(dev, net); dev_net_set(dev, net);
dev->rtnl_link_ops = &tun_link_ops; dev->rtnl_link_ops = &tun_link_ops;
dev->ifindex = tfile->ifindex;
tun = netdev_priv(dev); tun = netdev_priv(dev);
tun->dev = dev; tun->dev = dev;
...@@ -1817,6 +1821,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, ...@@ -1817,6 +1821,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
kgid_t group; kgid_t group;
int sndbuf; int sndbuf;
int vnet_hdr_sz; int vnet_hdr_sz;
unsigned int ifindex;
int ret; int ret;
if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
...@@ -1851,6 +1856,19 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, ...@@ -1851,6 +1856,19 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
ret = -EFAULT; ret = -EFAULT;
goto unlock; goto unlock;
} }
if (cmd == TUNSETIFINDEX) {
ret = -EPERM;
if (tun)
goto unlock;
ret = -EFAULT;
if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
goto unlock;
ret = 0;
tfile->ifindex = ifindex;
goto unlock;
}
ret = -EBADFD; ret = -EBADFD;
if (!tun) if (!tun)
...@@ -2099,6 +2117,7 @@ static int tun_chr_open(struct inode *inode, struct file * file) ...@@ -2099,6 +2117,7 @@ static int tun_chr_open(struct inode *inode, struct file * file)
rcu_assign_pointer(tfile->tun, NULL); rcu_assign_pointer(tfile->tun, NULL);
tfile->net = get_net(current->nsproxy->net_ns); tfile->net = get_net(current->nsproxy->net_ns);
tfile->flags = 0; tfile->flags = 0;
tfile->ifindex = 0;
rcu_assign_pointer(tfile->socket.wq, &tfile->wq); rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
init_waitqueue_head(&tfile->wq.wait); init_waitqueue_head(&tfile->wq.wait);
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#define TUNGETVNETHDRSZ _IOR('T', 215, int) #define TUNGETVNETHDRSZ _IOR('T', 215, int)
#define TUNSETVNETHDRSZ _IOW('T', 216, int) #define TUNSETVNETHDRSZ _IOW('T', 216, int)
#define TUNSETQUEUE _IOW('T', 217, int) #define TUNSETQUEUE _IOW('T', 217, int)
#define TUNSETIFINDEX _IOW('T', 218, unsigned int)
/* TUNSETIFF ifr flags */ /* TUNSETIFF ifr flags */
#define IFF_TUN 0x0001 #define IFF_TUN 0x0001
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment