blob: f144216febc642fd70512df9dddefe1a7f119478 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Mike Rapoportfd2a0432016-06-08 16:09:18 +03002#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
8static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
9 const struct virtio_net_hdr *hdr,
10 bool little_endian)
11{
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050012 unsigned int gso_type = 0;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030013
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;
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050022 case VIRTIO_NET_HDR_GSO_UDP:
23 gso_type = SKB_GSO_UDP;
24 break;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030025 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
58static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
59 struct virtio_net_hdr *hdr,
Jason Wang6391a442017-01-20 14:32:42 +080060 bool little_endian,
61 bool has_data_valid)
Mike Rapoportfd2a0432016-06-08 16:09:18 +030062{
Jarno Rajahalme9403cd72016-11-18 15:40:40 -080063 memset(hdr, 0, sizeof(*hdr)); /* no info leak */
Mike Rapoportfd2a0432016-06-08 16:09:18 +030064
65 if (skb_is_gso(skb)) {
66 struct skb_shared_info *sinfo = skb_shinfo(skb);
67
68 /* This is a hint as to how much should be linear. */
69 hdr->hdr_len = __cpu_to_virtio16(little_endian,
70 skb_headlen(skb));
71 hdr->gso_size = __cpu_to_virtio16(little_endian,
72 sinfo->gso_size);
73 if (sinfo->gso_type & SKB_GSO_TCPV4)
74 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
75 else if (sinfo->gso_type & SKB_GSO_TCPV6)
76 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030077 else
78 return -EINVAL;
79 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
80 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
81 } else
82 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
83
84 if (skb->ip_summed == CHECKSUM_PARTIAL) {
85 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
86 if (skb_vlan_tag_present(skb))
87 hdr->csum_start = __cpu_to_virtio16(little_endian,
88 skb_checksum_start_offset(skb) + VLAN_HLEN);
89 else
90 hdr->csum_start = __cpu_to_virtio16(little_endian,
91 skb_checksum_start_offset(skb));
92 hdr->csum_offset = __cpu_to_virtio16(little_endian,
93 skb->csum_offset);
Jason Wang6391a442017-01-20 14:32:42 +080094 } else if (has_data_valid &&
95 skb->ip_summed == CHECKSUM_UNNECESSARY) {
96 hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030097 } /* else everything is zero */
98
99 return 0;
100}
101
Jarno Rajahalmed66016a2016-11-18 15:40:39 -0800102#endif /* _LINUX_VIRTIO_NET_H */