return NULL;
}
+static int tun_skb_set_protocol(struct tun_struct *tun, struct sk_buff *skb,
+ __be16 pi_proto, bool no_pull_check)
+{
+ switch (tun->flags & TUN_TYPE_MASK) {
+ case IFF_TUN:
+ if (tun->flags & IFF_NO_PI) {
+ u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
+
+ switch (ip_version) {
+ case 4:
+ pi_proto = htons(ETH_P_IP);
+ break;
+ case 6:
+ pi_proto = htons(ETH_P_IPV6);
+ break;
+ default:
+ return -EINVAL;
+ }
+ }
+
+ skb_reset_mac_header(skb);
+ skb->protocol = pi_proto;
+ skb->dev = tun->dev;
+ break;
+ case IFF_TAP:
+ /* As an optimisation, no_pull_check can be set in the cases
+ * where the caller *knows* that eth_type_trans() will be OK
+ * because the Ethernet header is linearised at skb->data.
+ *
+ * XX: Or so I was reliably assured when I moved this code
+ * and didn't make it unconditional. dwmw2.
+ */
+ if (!no_pull_check && !pskb_may_pull(skb, ETH_HLEN))
+ return -ENOMEM;
+
+ skb->protocol = eth_type_trans(skb, tun->dev);
+ break;
+ }
+ return 0;
+}
+
/* Get packet from user space buffer */
static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
void *msg_control, struct iov_iter *from,
return -EINVAL;
}
- switch (tun->flags & TUN_TYPE_MASK) {
- case IFF_TUN:
- if (tun->flags & IFF_NO_PI) {
- u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
-
- switch (ip_version) {
- case 4:
- pi.proto = htons(ETH_P_IP);
- break;
- case 6:
- pi.proto = htons(ETH_P_IPV6);
- break;
- default:
- atomic_long_inc(&tun->dev->rx_dropped);
- kfree_skb(skb);
- return -EINVAL;
- }
- }
-
- skb_reset_mac_header(skb);
- skb->protocol = pi.proto;
- skb->dev = tun->dev;
- break;
- case IFF_TAP:
- if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
- err = -ENOMEM;
- goto drop;
- }
- skb->protocol = eth_type_trans(skb, tun->dev);
- break;
- }
+ err = tun_skb_set_protocol(tun, skb, pi.proto, !frags);
+ if (err)
+ goto drop;
/* copy skb_ubuf_info for callback when skb has no error */
if (zerocopy) {
struct virtio_net_hdr *gso = NULL;
struct bpf_prog *xdp_prog;
struct sk_buff *skb = NULL;
+ __be16 proto = 0;
u32 rxhash = 0, act;
int buflen = hdr->buflen;
int err = 0;
bool skb_xdp = false;
struct page *page;
+ if (!(tun->flags & IFF_NO_PI)) {
+ struct tun_pi *pi = tun_hdr;
+ tun_hdr += sizeof(*pi);
+
+ if (tun_hdr > xdp->data) {
+ atomic_long_inc(&tun->rx_frame_errors);
+ return -EINVAL;
+ }
+ proto = pi->proto;
+ }
+
if (tun->flags & IFF_VNET_HDR) {
gso = tun_hdr;
tun_hdr += sizeof(*gso);
goto out;
}
- skb->protocol = eth_type_trans(skb, tun->dev);
+ err = tun_skb_set_protocol(tun, skb, proto, true);
+ if (err) {
+ atomic_long_inc(&tun->dev->rx_dropped);
+ kfree_skb(skb);
+ goto out;
+ }
+
skb_reset_network_header(skb);
skb_probe_transport_header(skb);
skb_record_rx_queue(skb, tfile->queue_index);