1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_NET_H
3 #define _LINUX_VIRTIO_NET_H
5 #include <linux/if_vlan.h>
6 #include <uapi/linux/tcp.h>
7 #include <uapi/linux/udp.h>
8 #include <uapi/linux/virtio_net.h>
10 static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,
11 const struct virtio_net_hdr *hdr)
13 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
14 case VIRTIO_NET_HDR_GSO_TCPV4:
15 case VIRTIO_NET_HDR_GSO_UDP:
16 skb->protocol = cpu_to_be16(ETH_P_IP);
18 case VIRTIO_NET_HDR_GSO_TCPV6:
19 skb->protocol = cpu_to_be16(ETH_P_IPV6);
28 static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
29 const struct virtio_net_hdr *hdr,
32 unsigned int gso_type = 0;
33 unsigned int thlen = 0;
34 unsigned int p_off = 0;
35 unsigned int ip_proto;
37 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
38 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
39 case VIRTIO_NET_HDR_GSO_TCPV4:
40 gso_type = SKB_GSO_TCPV4;
41 ip_proto = IPPROTO_TCP;
42 thlen = sizeof(struct tcphdr);
44 case VIRTIO_NET_HDR_GSO_TCPV6:
45 gso_type = SKB_GSO_TCPV6;
46 ip_proto = IPPROTO_TCP;
47 thlen = sizeof(struct tcphdr);
49 case VIRTIO_NET_HDR_GSO_UDP:
50 gso_type = SKB_GSO_UDP;
51 ip_proto = IPPROTO_UDP;
52 thlen = sizeof(struct udphdr);
58 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
59 gso_type |= SKB_GSO_TCP_ECN;
61 if (hdr->gso_size == 0)
65 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
66 u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
67 u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
69 if (!skb_partial_csum_set(skb, start, off))
72 p_off = skb_transport_offset(skb) + thlen;
73 if (p_off > skb_headlen(skb))
76 /* gso packets without NEEDS_CSUM do not set transport_offset.
77 * probe and drop if does not match one of the above types.
79 if (gso_type && skb->network_header) {
80 struct flow_keys_basic keys;
83 __be16 protocol = dev_parse_header_protocol(skb);
85 virtio_net_hdr_set_proto(skb, hdr);
86 if (protocol && protocol != skb->protocol)
90 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
93 /* UFO does not specify ipv4 or 6: try both */
94 if (gso_type & SKB_GSO_UDP &&
95 skb->protocol == htons(ETH_P_IP)) {
96 skb->protocol = htons(ETH_P_IPV6);
102 p_off = keys.control.thoff + thlen;
103 if (p_off > skb_headlen(skb) ||
104 keys.basic.ip_proto != ip_proto)
107 skb_set_transport_header(skb, keys.control.thoff);
108 } else if (gso_type) {
110 if (p_off > skb_headlen(skb))
115 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
116 u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
117 struct skb_shared_info *shinfo = skb_shinfo(skb);
119 /* Too small packets are not really GSO ones. */
120 if (skb->len - p_off > gso_size) {
121 shinfo->gso_size = gso_size;
122 shinfo->gso_type = gso_type;
124 /* Header must be checked, and gso_segs computed. */
125 shinfo->gso_type |= SKB_GSO_DODGY;
126 shinfo->gso_segs = 0;
133 static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
134 struct virtio_net_hdr *hdr,
139 memset(hdr, 0, sizeof(*hdr)); /* no info leak */
141 if (skb_is_gso(skb)) {
142 struct skb_shared_info *sinfo = skb_shinfo(skb);
144 /* This is a hint as to how much should be linear. */
145 hdr->hdr_len = __cpu_to_virtio16(little_endian,
147 hdr->gso_size = __cpu_to_virtio16(little_endian,
149 if (sinfo->gso_type & SKB_GSO_TCPV4)
150 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
151 else if (sinfo->gso_type & SKB_GSO_TCPV6)
152 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
155 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
156 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
158 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
160 if (skb->ip_summed == CHECKSUM_PARTIAL) {
161 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
162 hdr->csum_start = __cpu_to_virtio16(little_endian,
163 skb_checksum_start_offset(skb) + vlan_hlen);
164 hdr->csum_offset = __cpu_to_virtio16(little_endian,
166 } else if (has_data_valid &&
167 skb->ip_summed == CHECKSUM_UNNECESSARY) {
168 hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
169 } /* else everything is zero */
174 #endif /* _LINUX_VIRTIO_NET_H */