blob: 32fb046f2173347ad0b72628e84b575e8899f7b9 [file] [log] [blame]
Mike Rapoportfd2a0432016-06-08 16:09:18 +03001#ifndef _LINUX_VIRTIO_NET_H
2#define _LINUX_VIRTIO_NET_H
3
4#include <linux/if_vlan.h>
5#include <uapi/linux/virtio_net.h>
6
7static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
8 const struct virtio_net_hdr *hdr,
9 bool little_endian)
10{
11 unsigned short gso_type = 0;
12
13 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
14 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
15 case VIRTIO_NET_HDR_GSO_TCPV4:
16 gso_type = SKB_GSO_TCPV4;
17 break;
18 case VIRTIO_NET_HDR_GSO_TCPV6:
19 gso_type = SKB_GSO_TCPV6;
20 break;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030021 default:
22 return -EINVAL;
23 }
24
25 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
26 gso_type |= SKB_GSO_TCP_ECN;
27
28 if (hdr->gso_size == 0)
29 return -EINVAL;
30 }
31
32 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
33 u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
34 u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
35
36 if (!skb_partial_csum_set(skb, start, off))
37 return -EINVAL;
38 }
39
40 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
41 u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
42
43 skb_shinfo(skb)->gso_size = gso_size;
44 skb_shinfo(skb)->gso_type = gso_type;
45
46 /* Header must be checked, and gso_segs computed. */
47 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
48 skb_shinfo(skb)->gso_segs = 0;
49 }
50
51 return 0;
52}
53
54static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
55 struct virtio_net_hdr *hdr,
Jason Wang6391a442017-01-20 14:32:42 +080056 bool little_endian,
57 bool has_data_valid)
Mike Rapoportfd2a0432016-06-08 16:09:18 +030058{
Jarno Rajahalme9403cd72016-11-18 15:40:40 -080059 memset(hdr, 0, sizeof(*hdr)); /* no info leak */
Mike Rapoportfd2a0432016-06-08 16:09:18 +030060
61 if (skb_is_gso(skb)) {
62 struct skb_shared_info *sinfo = skb_shinfo(skb);
63
64 /* This is a hint as to how much should be linear. */
65 hdr->hdr_len = __cpu_to_virtio16(little_endian,
66 skb_headlen(skb));
67 hdr->gso_size = __cpu_to_virtio16(little_endian,
68 sinfo->gso_size);
69 if (sinfo->gso_type & SKB_GSO_TCPV4)
70 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
71 else if (sinfo->gso_type & SKB_GSO_TCPV6)
72 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030073 else
74 return -EINVAL;
75 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
76 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
77 } else
78 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
79
80 if (skb->ip_summed == CHECKSUM_PARTIAL) {
81 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
82 if (skb_vlan_tag_present(skb))
83 hdr->csum_start = __cpu_to_virtio16(little_endian,
84 skb_checksum_start_offset(skb) + VLAN_HLEN);
85 else
86 hdr->csum_start = __cpu_to_virtio16(little_endian,
87 skb_checksum_start_offset(skb));
88 hdr->csum_offset = __cpu_to_virtio16(little_endian,
89 skb->csum_offset);
Jason Wang6391a442017-01-20 14:32:42 +080090 } else if (has_data_valid &&
91 skb->ip_summed == CHECKSUM_UNNECESSARY) {
92 hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030093 } /* else everything is zero */
94
95 return 0;
96}
97
Jarno Rajahalmed66016a2016-11-18 15:40:39 -080098#endif /* _LINUX_VIRTIO_NET_H */