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 ip_proto
;
36 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
37 switch (hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
38 case VIRTIO_NET_HDR_GSO_TCPV4
:
39 gso_type
= SKB_GSO_TCPV4
;
40 ip_proto
= IPPROTO_TCP
;
41 thlen
= sizeof(struct tcphdr
);
43 case VIRTIO_NET_HDR_GSO_TCPV6
:
44 gso_type
= SKB_GSO_TCPV6
;
45 ip_proto
= IPPROTO_TCP
;
46 thlen
= sizeof(struct tcphdr
);
48 case VIRTIO_NET_HDR_GSO_UDP
:
49 gso_type
= SKB_GSO_UDP
;
50 ip_proto
= IPPROTO_UDP
;
51 thlen
= sizeof(struct udphdr
);
57 if (hdr
->gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
58 gso_type
|= SKB_GSO_TCP_ECN
;
60 if (hdr
->gso_size
== 0)
64 if (hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
65 u16 start
= __virtio16_to_cpu(little_endian
, hdr
->csum_start
);
66 u16 off
= __virtio16_to_cpu(little_endian
, hdr
->csum_offset
);
68 if (!skb_partial_csum_set(skb
, start
, off
))
71 if (skb_transport_offset(skb
) + thlen
> skb_headlen(skb
))
74 /* gso packets without NEEDS_CSUM do not set transport_offset.
75 * probe and drop if does not match one of the above types.
77 if (gso_type
&& skb
->network_header
) {
78 struct flow_keys_basic keys
;
81 virtio_net_hdr_set_proto(skb
, hdr
);
83 if (!skb_flow_dissect_flow_keys_basic(NULL
, skb
, &keys
,
86 /* UFO does not specify ipv4 or 6: try both */
87 if (gso_type
& SKB_GSO_UDP
&&
88 skb
->protocol
== htons(ETH_P_IP
)) {
89 skb
->protocol
= htons(ETH_P_IPV6
);
95 if (keys
.control
.thoff
+ thlen
> skb_headlen(skb
) ||
96 keys
.basic
.ip_proto
!= ip_proto
)
99 skb_set_transport_header(skb
, keys
.control
.thoff
);
103 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
104 u16 gso_size
= __virtio16_to_cpu(little_endian
, hdr
->gso_size
);
106 skb_shinfo(skb
)->gso_size
= gso_size
;
107 skb_shinfo(skb
)->gso_type
= gso_type
;
109 /* Header must be checked, and gso_segs computed. */
110 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
111 skb_shinfo(skb
)->gso_segs
= 0;
117 static inline int virtio_net_hdr_from_skb(const struct sk_buff
*skb
,
118 struct virtio_net_hdr
*hdr
,
123 memset(hdr
, 0, sizeof(*hdr
)); /* no info leak */
125 if (skb_is_gso(skb
)) {
126 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
128 /* This is a hint as to how much should be linear. */
129 hdr
->hdr_len
= __cpu_to_virtio16(little_endian
,
131 hdr
->gso_size
= __cpu_to_virtio16(little_endian
,
133 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
134 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
135 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
136 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
139 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
140 hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
142 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
144 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
145 hdr
->flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
146 hdr
->csum_start
= __cpu_to_virtio16(little_endian
,
147 skb_checksum_start_offset(skb
) + vlan_hlen
);
148 hdr
->csum_offset
= __cpu_to_virtio16(little_endian
,
150 } else if (has_data_valid
&&
151 skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
152 hdr
->flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
153 } /* else everything is zero */
158 #endif /* _LINUX_VIRTIO_NET_H */