From d5f36f73be05ee425eca73b63b37d5a20b22837a Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Tue, 22 Jun 2021 12:34:58 +0800 Subject: [PATCH 2/2] tun: use vnet header only when IFF_VNET_HDR in tun_xdp_one() We should not try to read vnet header from the xdp buffer if IFF_VNET_HDR is not set, otherwise we break the semantic of IFF_VNET_HDR. Signed-off-by: Jason Wang --- drivers/net/tun.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 84f832806313..70c4bb22ef78 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -2334,18 +2334,22 @@ static int tun_xdp_one(struct tun_struct *tun, { unsigned int datasize = xdp->data_end - xdp->data; struct tun_xdp_hdr *hdr = xdp->data_hard_start; - struct virtio_net_hdr *gso = &hdr->gso; + struct virtio_net_hdr gso = { 0 }; struct bpf_prog *xdp_prog; struct sk_buff *skb = NULL; u32 rxhash = 0, act; int buflen = hdr->buflen; + bool vnet_hdr = tun->flags & IFF_VNET_HDR; int err = 0; bool skb_xdp = false; struct page *page; + if (vnet_hdr) + memcpy(&gso, &hdr->gso, sizeof(gso)); + xdp_prog = rcu_dereference(tun->xdp_prog); if (xdp_prog) { - if (gso->gso_type) { + if (gso.gso_type) { skb_xdp = true; goto build; } @@ -2391,7 +2395,7 @@ static int tun_xdp_one(struct tun_struct *tun, skb_reserve(skb, xdp->data - xdp->data_hard_start); skb_put(skb, xdp->data_end - xdp->data); - if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) { + if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { atomic_long_inc(&tun->rx_frame_errors); kfree_skb(skb); err = -EINVAL; -- 2.25.1