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>
7 #include <uapi/linux/tcp.h>
8 #include <uapi/linux/virtio_net.h>
10 static inline bool virtio_net_hdr_match_proto(__be16 protocol
, __u8 gso_type
)
12 switch (gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
13 case VIRTIO_NET_HDR_GSO_TCPV4
:
14 return protocol
== cpu_to_be16(ETH_P_IP
);
15 case VIRTIO_NET_HDR_GSO_TCPV6
:
16 return protocol
== cpu_to_be16(ETH_P_IPV6
);
17 case VIRTIO_NET_HDR_GSO_UDP
:
18 case VIRTIO_NET_HDR_GSO_UDP_L4
:
19 return protocol
== cpu_to_be16(ETH_P_IP
) ||
20 protocol
== cpu_to_be16(ETH_P_IPV6
);
26 static inline int virtio_net_hdr_set_proto(struct sk_buff
*skb
,
27 const struct virtio_net_hdr
*hdr
)
32 switch (hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
33 case VIRTIO_NET_HDR_GSO_TCPV4
:
34 case VIRTIO_NET_HDR_GSO_UDP
:
35 case VIRTIO_NET_HDR_GSO_UDP_L4
:
36 skb
->protocol
= cpu_to_be16(ETH_P_IP
);
38 case VIRTIO_NET_HDR_GSO_TCPV6
:
39 skb
->protocol
= cpu_to_be16(ETH_P_IPV6
);
48 static inline int virtio_net_hdr_to_skb(struct sk_buff
*skb
,
49 const struct virtio_net_hdr
*hdr
,
52 unsigned int gso_type
= 0;
53 unsigned int thlen
= 0;
54 unsigned int p_off
= 0;
55 unsigned int ip_proto
;
57 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
58 switch (hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
59 case VIRTIO_NET_HDR_GSO_TCPV4
:
60 gso_type
= SKB_GSO_TCPV4
;
61 ip_proto
= IPPROTO_TCP
;
62 thlen
= sizeof(struct tcphdr
);
64 case VIRTIO_NET_HDR_GSO_TCPV6
:
65 gso_type
= SKB_GSO_TCPV6
;
66 ip_proto
= IPPROTO_TCP
;
67 thlen
= sizeof(struct tcphdr
);
69 case VIRTIO_NET_HDR_GSO_UDP
:
70 gso_type
= SKB_GSO_UDP
;
71 ip_proto
= IPPROTO_UDP
;
72 thlen
= sizeof(struct udphdr
);
74 case VIRTIO_NET_HDR_GSO_UDP_L4
:
75 gso_type
= SKB_GSO_UDP_L4
;
76 ip_proto
= IPPROTO_UDP
;
77 thlen
= sizeof(struct udphdr
);
83 if (hdr
->gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
84 gso_type
|= SKB_GSO_TCP_ECN
;
86 if (hdr
->gso_size
== 0)
90 skb_reset_mac_header(skb
);
92 if (hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
93 u32 start
= __virtio16_to_cpu(little_endian
, hdr
->csum_start
);
94 u32 off
= __virtio16_to_cpu(little_endian
, hdr
->csum_offset
);
95 u32 needed
= start
+ max_t(u32
, thlen
, off
+ sizeof(__sum16
));
97 if (!pskb_may_pull(skb
, needed
))
100 if (!skb_partial_csum_set(skb
, start
, off
))
103 p_off
= skb_transport_offset(skb
) + thlen
;
104 if (!pskb_may_pull(skb
, p_off
))
107 /* gso packets without NEEDS_CSUM do not set transport_offset.
108 * probe and drop if does not match one of the above types.
110 if (gso_type
&& skb
->network_header
) {
111 struct flow_keys_basic keys
;
113 if (!skb
->protocol
) {
114 __be16 protocol
= dev_parse_header_protocol(skb
);
117 virtio_net_hdr_set_proto(skb
, hdr
);
118 else if (!virtio_net_hdr_match_proto(protocol
, hdr
->gso_type
))
121 skb
->protocol
= protocol
;
124 if (!skb_flow_dissect_flow_keys_basic(NULL
, skb
, &keys
,
127 /* UFO does not specify ipv4 or 6: try both */
128 if (gso_type
& SKB_GSO_UDP
&&
129 skb
->protocol
== htons(ETH_P_IP
)) {
130 skb
->protocol
= htons(ETH_P_IPV6
);
136 p_off
= keys
.control
.thoff
+ thlen
;
137 if (!pskb_may_pull(skb
, p_off
) ||
138 keys
.basic
.ip_proto
!= ip_proto
)
141 skb_set_transport_header(skb
, keys
.control
.thoff
);
142 } else if (gso_type
) {
144 if (!pskb_may_pull(skb
, p_off
))
149 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
150 u16 gso_size
= __virtio16_to_cpu(little_endian
, hdr
->gso_size
);
151 unsigned int nh_off
= p_off
;
152 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
154 switch (gso_type
& ~SKB_GSO_TCP_ECN
) {
156 /* UFO may not include transport header in gso_size. */
160 if (!(hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
))
162 if (skb
->csum_offset
!= offsetof(struct udphdr
, check
))
164 if (skb
->len
- p_off
> gso_size
* UDP_MAX_SEGMENTS
)
166 if (gso_type
!= SKB_GSO_UDP_L4
)
171 /* Kernel has a special handling for GSO_BY_FRAGS. */
172 if (gso_size
== GSO_BY_FRAGS
)
175 /* Too small packets are not really GSO ones. */
176 if (skb
->len
- nh_off
> gso_size
) {
177 shinfo
->gso_size
= gso_size
;
178 shinfo
->gso_type
= gso_type
;
180 /* Header must be checked, and gso_segs computed. */
181 shinfo
->gso_type
|= SKB_GSO_DODGY
;
182 shinfo
->gso_segs
= 0;
189 static inline int virtio_net_hdr_from_skb(const struct sk_buff
*skb
,
190 struct virtio_net_hdr
*hdr
,
195 memset(hdr
, 0, sizeof(*hdr
)); /* no info leak */
197 if (skb_is_gso(skb
)) {
198 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
200 /* This is a hint as to how much should be linear. */
201 hdr
->hdr_len
= __cpu_to_virtio16(little_endian
,
203 hdr
->gso_size
= __cpu_to_virtio16(little_endian
,
205 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
206 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
207 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
208 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
209 else if (sinfo
->gso_type
& SKB_GSO_UDP_L4
)
210 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_UDP_L4
;
213 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
214 hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
216 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
218 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
219 hdr
->flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
220 hdr
->csum_start
= __cpu_to_virtio16(little_endian
,
221 skb_checksum_start_offset(skb
) + vlan_hlen
);
222 hdr
->csum_offset
= __cpu_to_virtio16(little_endian
,
224 } else if (has_data_valid
&&
225 skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
226 hdr
->flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
227 } /* else everything is zero */
232 #endif /* _LINUX_VIRTIO_NET_H */