xref: /openbmc/linux/include/linux/virtio_net.h (revision 791d3ef2e11100449837dc0b6fe884e60ca3a484)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_NET_H
3 #define _LINUX_VIRTIO_NET_H
4 
5 #include <linux/if_vlan.h>
6 #include <uapi/linux/virtio_net.h>
7 
8 static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
9 					const struct virtio_net_hdr *hdr,
10 					bool little_endian)
11 {
12 	unsigned int gso_type = 0;
13 
14 	if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
15 		switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
16 		case VIRTIO_NET_HDR_GSO_TCPV4:
17 			gso_type = SKB_GSO_TCPV4;
18 			break;
19 		case VIRTIO_NET_HDR_GSO_TCPV6:
20 			gso_type = SKB_GSO_TCPV6;
21 			break;
22 		case VIRTIO_NET_HDR_GSO_UDP:
23 			gso_type = SKB_GSO_UDP;
24 			break;
25 		default:
26 			return -EINVAL;
27 		}
28 
29 		if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
30 			gso_type |= SKB_GSO_TCP_ECN;
31 
32 		if (hdr->gso_size == 0)
33 			return -EINVAL;
34 	}
35 
36 	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
37 		u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
38 		u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
39 
40 		if (!skb_partial_csum_set(skb, start, off))
41 			return -EINVAL;
42 	}
43 
44 	if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
45 		u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
46 
47 		skb_shinfo(skb)->gso_size = gso_size;
48 		skb_shinfo(skb)->gso_type = gso_type;
49 
50 		/* Header must be checked, and gso_segs computed. */
51 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
52 		skb_shinfo(skb)->gso_segs = 0;
53 	}
54 
55 	return 0;
56 }
57 
58 static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
59 					  struct virtio_net_hdr *hdr,
60 					  bool little_endian,
61 					  bool has_data_valid,
62 					  int vlan_hlen)
63 {
64 	memset(hdr, 0, sizeof(*hdr));   /* no info leak */
65 
66 	if (skb_is_gso(skb)) {
67 		struct skb_shared_info *sinfo = skb_shinfo(skb);
68 
69 		/* This is a hint as to how much should be linear. */
70 		hdr->hdr_len = __cpu_to_virtio16(little_endian,
71 						 skb_headlen(skb));
72 		hdr->gso_size = __cpu_to_virtio16(little_endian,
73 						  sinfo->gso_size);
74 		if (sinfo->gso_type & SKB_GSO_TCPV4)
75 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
76 		else if (sinfo->gso_type & SKB_GSO_TCPV6)
77 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
78 		else
79 			return -EINVAL;
80 		if (sinfo->gso_type & SKB_GSO_TCP_ECN)
81 			hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
82 	} else
83 		hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
84 
85 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
86 		hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
87 		hdr->csum_start = __cpu_to_virtio16(little_endian,
88 			skb_checksum_start_offset(skb) + vlan_hlen);
89 		hdr->csum_offset = __cpu_to_virtio16(little_endian,
90 				skb->csum_offset);
91 	} else if (has_data_valid &&
92 		   skb->ip_summed == CHECKSUM_UNNECESSARY) {
93 		hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
94 	} /* else everything is zero */
95 
96 	return 0;
97 }
98 
99 #endif /* _LINUX_VIRTIO_NET_H */
100