Pārlūkot izejas kodu

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>
Michael S. Tsirkin 15 gadi atpakaļ
vecāks
revīzija
d9d52b5178
2 mainītis faili ar 30 papildinājumiem un 4 dzēšanām
  1. 28 4
      drivers/net/tun.c
  2. 2 0
      include/linux/if_tun.h

+ 28 - 4
drivers/net/tun.c

@@ -110,6 +110,9 @@ struct tun_struct {
 	struct tap_filter       txflt;
 	struct tap_filter       txflt;
 	struct socket		socket;
 	struct socket		socket;
 	struct socket_wq	wq;
 	struct socket_wq	wq;
+
+	int			vnet_hdr_sz;
+
 #ifdef TUN_DEBUG
 #ifdef TUN_DEBUG
 	int debug;
 	int debug;
 #endif
 #endif
@@ -563,7 +566,7 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
 	}
 	}
 
 
 	if (tun->flags & TUN_VNET_HDR) {
 	if (tun->flags & TUN_VNET_HDR) {
-		if ((len -= sizeof(gso)) > count)
+		if ((len -= tun->vnet_hdr_sz) > count)
 			return -EINVAL;
 			return -EINVAL;
 
 
 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
@@ -575,7 +578,7 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
 
 
 		if (gso.hdr_len > len)
 		if (gso.hdr_len > len)
 			return -EINVAL;
 			return -EINVAL;
-		offset += sizeof(gso);
+		offset += tun->vnet_hdr_sz;
 	}
 	}
 
 
 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
@@ -718,7 +721,7 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
 
 
 	if (tun->flags & TUN_VNET_HDR) {
 	if (tun->flags & TUN_VNET_HDR) {
 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
-		if ((len -= sizeof(gso)) < 0)
+		if ((len -= tun->vnet_hdr_sz) < 0)
 			return -EINVAL;
 			return -EINVAL;
 
 
 		if (skb_is_gso(skb)) {
 		if (skb_is_gso(skb)) {
@@ -749,7 +752,7 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
 					       sizeof(gso))))
 					       sizeof(gso))))
 			return -EFAULT;
 			return -EFAULT;
-		total += sizeof(gso);
+		total += tun->vnet_hdr_sz;
 	}
 	}
 
 
 	len = min_t(int, skb->len, len);
 	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)
 		tun->dev = dev;
 		tun->dev = dev;
 		tun->flags = flags;
 		tun->flags = flags;
 		tun->txflt.count = 0;
 		tun->txflt.count = 0;
+		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
 
 		err = -ENOMEM;
 		err = -ENOMEM;
 		sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
 		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,
 	struct sock_fprog fprog;
 	struct sock_fprog fprog;
 	struct ifreq ifr;
 	struct ifreq ifr;
 	int sndbuf;
 	int sndbuf;
+	int vnet_hdr_sz;
 	int ret;
 	int ret;
 
 
 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
@@ -1322,6 +1327,25 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		tun->socket.sk->sk_sndbuf = sndbuf;
 		tun->socket.sk->sk_sndbuf = sndbuf;
 		break;
 		break;
 
 
+	case TUNGETVNETHDRSZ:
+		vnet_hdr_sz = tun->vnet_hdr_sz;
+		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
+			ret = -EFAULT;
+		break;
+
+	case TUNSETVNETHDRSZ:
+		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
+			ret = -EFAULT;
+			break;
+		}
+		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
+			ret = -EINVAL;
+			break;
+		}
+
+		tun->vnet_hdr_sz = vnet_hdr_sz;
+		break;
+
 	case TUNATTACHFILTER:
 	case TUNATTACHFILTER:
 		/* Can be set only for TAPs */
 		/* Can be set only for TAPs */
 		ret = -EINVAL;
 		ret = -EINVAL;

+ 2 - 0
include/linux/if_tun.h

@@ -51,6 +51,8 @@
 #define TUNSETSNDBUF   _IOW('T', 212, int)
 #define TUNSETSNDBUF   _IOW('T', 212, int)
 #define TUNATTACHFILTER _IOW('T', 213, struct sock_fprog)
 #define TUNATTACHFILTER _IOW('T', 213, struct sock_fprog)
 #define TUNDETACHFILTER _IOW('T', 214, struct sock_fprog)
 #define TUNDETACHFILTER _IOW('T', 214, struct sock_fprog)
+#define TUNGETVNETHDRSZ _IOR('T', 215, int)
+#define TUNSETVNETHDRSZ _IOW('T', 216, int)
 
 
 /* TUNSETIFF ifr flags */
 /* TUNSETIFF ifr flags */
 #define IFF_TUN		0x0001
 #define IFF_TUN		0x0001