diff --git a/arch/um/drivers/vector_transports.c b/arch/um/drivers/vector_transports.c
index 8b7548458..2dc4cf992 100644
--- a/arch/um/drivers/vector_transports.c
+++ b/arch/um/drivers/vector_transports.c
@@ -210,7 +210,7 @@ static int raw_verify_header(
return 1;
skb_set_network_header(skb, ETH_HLEN);
- if (virtio_net_hdr_to_skb(skb, vheader,
+ if (virtio_net_hdr_to_skb(skb, vheader, true,
virtio_legacy_is_little_endian()) < 0)
return -1;
return 0;
diff --git a/drivers/net/tun_vnet.h b/drivers/net/tun_vnet.h
index 3364e08dd..5411258ee 100644
--- a/drivers/net/tun_vnet.h
+++ b/drivers/net/tun_vnet.h
@@ -180,7 +180,9 @@ static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
const struct virtio_net_hdr *hdr)
{
- return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
+ /* Only tap.c, where a frame always starts with an Ethernet header. */
+ return virtio_net_hdr_to_skb(skb, hdr, true,
+ tun_vnet_is_little_endian(flags));
}
/*diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index 3135dae03..e9057767b 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -190,6 +190,77 @@ virtio_net_l4_start(struct sk_buff *skb, unsigned int nhoff, __be16 proto,
return false;
}
+/* Does checksum metadata match a known TCP or UDP header? */
+static inline bool
+virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,
+ unsigned int nhoff, __be16 proto)
+{
+ unsigned int thoff;
+ int ip_proto;
+
+ if (!virtio_net_l4_start(skb, nhoff, proto, &thoff, &ip_proto))
+ return false;
+
+ if (skb_checksum_start_offset(skb) != thoff)
+ return false;
+
+ switch (ip_proto) {
+ case IPPROTO_TCP:
+ if (skb->csum_offset != offsetof(struct tcphdr, check) ||
+ !pskb_may_pull(skb, thoff + sizeof(struct tcphdr)))
+ return false;
+ break;
+ case IPPROTO_UDP:
+ if (skb->csum_offset != offsetof(struct udphdr, check) ||
+ !pskb_may_pull(skb, thoff + sizeof(struct udphdr)))
+ return false;
+ break;
+ default:
+ return false;
+ }
+
+ switch (hdr_gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
+ case VIRTIO_NET_HDR_GSO_NONE:
+ return true;
+ case VIRTIO_NET_HDR_GSO_TCPV4:
+ return proto == htons(ETH_P_IP) && ip_proto == IPPROTO_TCP;
+ case VIRTIO_NET_HDR_GSO_TCPV6:
+ return proto == htons(ETH_P_IPV6) && ip_proto == IPPROTO_TCP;
+ case VIRTIO_NET_HDR_GSO_UDP:
+ case VIRTIO_NET_HDR_GSO_UDP_L4:
+ return ip_proto == IPPROTO_UDP;
+ default:
+ return false;
+ }
+}
+
+static inline bool
+virtio_net_csum_partial_ok(struct sk_buff *skb, bool has_eth,
+ u8 hdr_gso_type)
+{
+ int nhoff;
+ __be16 proto;
+
+ if (!virtio_net_l3_start(skb, has_eth, &nhoff, &proto))
+ return false;
+
+ return virtio_net_csum_partial_ok_at_offset(skb, hdr_gso_type, nhoff,
+ proto);
+}
+
+/*
+ * Handle a packet whose checksum metadata does not match the packet:
+ * GSO packets are dropped, as segmentation needs one checksum per
+ * segment; others are completed in software.
+ */
+static inline int __virtio_net_resolve_csum_partial_mismatch(struct sk_buff *skb)
+{
+ if (skb_is_gso(skb))
+ return -EINVAL;
+
+ return skb_checksum_help(skb) ?: 1;
+}
+
static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb,
const struct virtio_net_hdr *hdr,
bool little_endian, u8 hdr_gso_type)@@ -336,11 +407,38 @@ static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb,
return 0;
}
+/*
+ * @has_eth: the frame starts with an Ethernet header, see
+ * virtio_net_l3_start().
+ *
+ * Return value:
+ * 0 - success
+ * >0 - checksum was completed in software (slow path)
+ * <0 - failure
+ */
static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
const struct virtio_net_hdr *hdr,
- bool little_endian)
+ bool has_eth, bool little_endian)
{
- return __virtio_net_hdr_to_skb(skb, hdr, little_endian, hdr->gso_type);
+ typeof(skb->transport_header) old_transport = skb->transport_header;
+ int ret = __virtio_net_hdr_to_skb(skb, hdr, little_endian,
+ hdr->gso_type);
+
+ if (ret < 0)
+ return ret;
+
+ if (likely(skb->ip_summed != CHECKSUM_PARTIAL ||
+ virtio_net_csum_partial_ok(skb, has_eth, hdr->gso_type)))
+ return 0;
+
+ /*
+ * Here we do not trust the checksum metadata.
+ * __virtio_net_hdr_to_skb set the transport header to csum_start
+ * from virtio hdr, restore it to a known good value.
+ */
+ skb->transport_header = old_transport;
+
+ return __virtio_net_resolve_csum_partial_mismatch(skb);
}
/* This function must be called after virtio_net_hdr_from_skb(). */@@ -447,6 +545,12 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool has_eth, bool outer_isv6,
return ip_proto == IPPROTO_UDP && outer_th == thoff;
}
+/*
+ * Return value:
+ * 0 - success
+ * >0 - checksum was completed in software (slow path)
+ * <0 - failure
+ */
static inline int
virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
const struct virtio_net_hdr_v1_hash_tunnel *vhdr,
@@ -459,16 +563,20 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
unsigned int inner_nh, outer_th, inner_th;
unsigned int inner_l3min, outer_l3min;
u8 gso_inner_type, gso_tunnel_type;
+ __be16 inner_proto;
bool outer_isv6, inner_isv6;
int ret;
gso_tunnel_type = hdr->gso_type & VIRTIO_NET_HDR_GSO_UDP_TUNNEL;
if (!gso_tunnel_type)
- return virtio_net_hdr_to_skb(skb, hdr, little_endian);
+ return virtio_net_hdr_to_skb(skb, hdr, has_eth, little_endian);
/* Tunnel not supported/negotiated, but the hdr asks for it. */
if (!tnl_hdr_negotiated)
return -EINVAL;
+ if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM &&
+ !tnl_csum_negotiated)
+ return -EINVAL;
/* Either ipv4 or ipv6. */
if (gso_tunnel_type == VIRTIO_NET_HDR_GSO_UDP_TUNNEL)
@@ -521,12 +629,20 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
return -EINVAL;
}
- skb_set_inner_protocol(skb, inner_isv6 ? htons(ETH_P_IPV6) :
- htons(ETH_P_IP));
- if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
- if (!tnl_csum_negotiated)
- return -EINVAL;
+ inner_proto = inner_isv6 ? htons(ETH_P_IPV6) : htons(ETH_P_IP);
+ /* NEEDS_CSUM is set, so this is CHECKSUM_PARTIAL. A packet whose
+ * inner metadata does not match cannot be segmented, and completing
+ * the inner checksum alone would leave the outer one to the seed
+ * the sender put there.
+ */
+ if (unlikely(!virtio_net_csum_partial_ok_at_offset(skb, gso_inner_type,
+ inner_nh,
+ inner_proto)))
+ return -EINVAL;
+
+ skb_set_inner_protocol(skb, inner_proto);
+ if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
} else {
skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 930e85e4b..c07a01969 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -2778,6 +2778,16 @@ static int tpacket_parse_header(struct packet_sock *po, void *frame,
return tp_len;
}
+/* The device says how its frames start. */
+static int packet_vnet_hdr_to_skb(struct sk_buff *skb,
+ const struct virtio_net_hdr *vnet_hdr)
+{
+ bool has_eth = skb->dev->type == ARPHRD_ETHER ||
+ skb->dev->type == ARPHRD_LOOPBACK;
+
+ return virtio_net_hdr_to_skb(skb, vnet_hdr, has_eth, vio_le());
+}
+
static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
{
struct sk_buff *skb = NULL;@@ -2941,7 +2951,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
}
if (has_vnet_hdr) {
- if (virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le()) < 0) {
+ if (packet_vnet_hdr_to_skb(skb, &vnet_hdr) < 0) {
tp_len = -EINVAL;
goto tpacket_error;
}@@ -3163,7 +3173,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
skb->no_fcs = 1;
if (vnet_hdr_sz) {
- err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
+ err = packet_vnet_hdr_to_skb(skb, &vnet_hdr);
if (err < 0)
goto out_free;
len += vnet_hdr_sz;--
2.54.0 (Apple Git-157)