Skip to content

Commit d9d52b5

Browse files
committed
tun: add ioctl to modify vnet header size
virtio added mergeable buffers mode where 2 bytes of extra info is put after vnet header but before actual data (tun does not need this data). In hindsight, it would have been better to add the new info *before* the packet: as it is, users need a lot of tricky code to skip the extra 2 bytes in the middle of the iovec, and in fact applications seem to get it wrong, and only work with specific iovec layout. The fact we might need to split iovec also means we might in theory overflow iovec max size. This patch adds a simpler way for applications to handle this, and future proofs the interface against further extensions, by making the size of the virtio net header configurable from userspace. As a result, tun driver will simply skip the extra 2 bytes on both input and output. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: David S. Miller <davem@davemloft.net>
1 parent 7ef5273 commit d9d52b5

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

‎drivers/net/tun.c‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ struct tun_struct {
110110
struct tap_filter txflt;
111111
struct socket socket;
112112
struct socket_wq wq;
113+
114+
int vnet_hdr_sz;
115+
113116
#ifdef TUN_DEBUG
114117
int debug;
115118
#endif
@@ -563,7 +566,7 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
563566
}
564567

565568
if (tun->flags & TUN_VNET_HDR) {
566-
if ((len -= sizeof(gso)) > count)
569+
if ((len -= tun->vnet_hdr_sz) > count)
567570
return -EINVAL;
568571

569572
if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
@@ -575,7 +578,7 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
575578

576579
if (gso.hdr_len > len)
577580
return -EINVAL;
578-
offset += sizeof(gso);
581+
offset += tun->vnet_hdr_sz;
579582
}
580583

581584
if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
@@ -718,7 +721,7 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
718721

719722
if (tun->flags & TUN_VNET_HDR) {
720723
struct virtio_net_hdr gso = { 0 }; /* no info leak */
721-
if ((len -= sizeof(gso)) < 0)
724+
if ((len -= tun->vnet_hdr_sz) < 0)
722725
return -EINVAL;
723726

724727
if (skb_is_gso(skb)) {
@@ -749,7 +752,7 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
749752
if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
750753
sizeof(gso))))
751754
return -EFAULT;
752-
total += sizeof(gso);
755+
total += tun->vnet_hdr_sz;
753756
}
754757

755758
len = min_t(int, skb->len, len);
@@ -1035,6 +1038,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
10351038
tun->dev = dev;
10361039
tun->flags = flags;
10371040
tun->txflt.count = 0;
1041+
tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
10381042

10391043
err = -ENOMEM;
10401044
sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
@@ -1177,6 +1181,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
11771181
struct sock_fprog fprog;
11781182
struct ifreq ifr;
11791183
int sndbuf;
1184+
int vnet_hdr_sz;
11801185
int ret;
11811186

11821187
if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
@@ -1322,6 +1327,25 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
13221327
tun->socket.sk->sk_sndbuf = sndbuf;
13231328
break;
13241329

1330+
case TUNGETVNETHDRSZ:
1331+
vnet_hdr_sz = tun->vnet_hdr_sz;
1332+
if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1333+
ret = -EFAULT;
1334+
break;
1335+
1336+
case TUNSETVNETHDRSZ:
1337+
if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1338+
ret = -EFAULT;
1339+
break;
1340+
}
1341+
if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1342+
ret = -EINVAL;
1343+
break;
1344+
}
1345+
1346+
tun->vnet_hdr_sz = vnet_hdr_sz;
1347+
break;
1348+
13251349
case TUNATTACHFILTER:
13261350
/* Can be set only for TAPs */
13271351
ret = -EINVAL;

‎include/linux/if_tun.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#define TUNSETSNDBUF _IOW('T', 212, int)
5252
#define TUNATTACHFILTER _IOW('T', 213, struct sock_fprog)
5353
#define TUNDETACHFILTER _IOW('T', 214, struct sock_fprog)
54+
#define TUNGETVNETHDRSZ _IOR('T', 215, int)
55+
#define TUNSETVNETHDRSZ _IOW('T', 216, int)
5456

5557
/* TUNSETIFF ifr flags */
5658
#define IFF_TUN 0x0001

0 commit comments

Comments
 (0)