]> Git Repo - linux.git/blame - include/linux/virtio_net.h
net: Don't set transport offset to invalid value
[linux.git] / include / linux / virtio_net.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
fd2a0437
MR
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
9d2f67e4
JT
8static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,
9 const struct virtio_net_hdr *hdr)
10{
11 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
12 case VIRTIO_NET_HDR_GSO_TCPV4:
13 case VIRTIO_NET_HDR_GSO_UDP:
14 skb->protocol = cpu_to_be16(ETH_P_IP);
15 break;
16 case VIRTIO_NET_HDR_GSO_TCPV6:
17 skb->protocol = cpu_to_be16(ETH_P_IPV6);
18 break;
19 default:
20 return -EINVAL;
21 }
22
23 return 0;
24}
25
fd2a0437
MR
26static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
27 const struct virtio_net_hdr *hdr,
28 bool little_endian)
29{
0c19f846 30 unsigned int gso_type = 0;
fd2a0437
MR
31
32 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
33 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
34 case VIRTIO_NET_HDR_GSO_TCPV4:
35 gso_type = SKB_GSO_TCPV4;
36 break;
37 case VIRTIO_NET_HDR_GSO_TCPV6:
38 gso_type = SKB_GSO_TCPV6;
39 break;
0c19f846
WB
40 case VIRTIO_NET_HDR_GSO_UDP:
41 gso_type = SKB_GSO_UDP;
42 break;
fd2a0437
MR
43 default:
44 return -EINVAL;
45 }
46
47 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
48 gso_type |= SKB_GSO_TCP_ECN;
49
50 if (hdr->gso_size == 0)
51 return -EINVAL;
52 }
53
54 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
55 u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
56 u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
57
58 if (!skb_partial_csum_set(skb, start, off))
59 return -EINVAL;
d5be7f63
WB
60 } else {
61 /* gso packets without NEEDS_CSUM do not set transport_offset.
62 * probe and drop if does not match one of the above types.
63 */
64 if (gso_type) {
d2aa125d 65 skb_probe_transport_header(skb);
d5be7f63
WB
66 if (!skb_transport_header_was_set(skb))
67 return -EINVAL;
68 }
fd2a0437
MR
69 }
70
71 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
72 u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
73
74 skb_shinfo(skb)->gso_size = gso_size;
75 skb_shinfo(skb)->gso_type = gso_type;
76
77 /* Header must be checked, and gso_segs computed. */
78 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
79 skb_shinfo(skb)->gso_segs = 0;
80 }
81
82 return 0;
83}
84
85static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
86 struct virtio_net_hdr *hdr,
6391a448 87 bool little_endian,
fd3a8862
WB
88 bool has_data_valid,
89 int vlan_hlen)
fd2a0437 90{
9403cd7c 91 memset(hdr, 0, sizeof(*hdr)); /* no info leak */
fd2a0437
MR
92
93 if (skb_is_gso(skb)) {
94 struct skb_shared_info *sinfo = skb_shinfo(skb);
95
96 /* This is a hint as to how much should be linear. */
97 hdr->hdr_len = __cpu_to_virtio16(little_endian,
98 skb_headlen(skb));
99 hdr->gso_size = __cpu_to_virtio16(little_endian,
100 sinfo->gso_size);
101 if (sinfo->gso_type & SKB_GSO_TCPV4)
102 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
103 else if (sinfo->gso_type & SKB_GSO_TCPV6)
104 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
fd2a0437
MR
105 else
106 return -EINVAL;
107 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
108 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
109 } else
110 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
111
112 if (skb->ip_summed == CHECKSUM_PARTIAL) {
113 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
fd3a8862
WB
114 hdr->csum_start = __cpu_to_virtio16(little_endian,
115 skb_checksum_start_offset(skb) + vlan_hlen);
fd2a0437
MR
116 hdr->csum_offset = __cpu_to_virtio16(little_endian,
117 skb->csum_offset);
6391a448
JW
118 } else if (has_data_valid &&
119 skb->ip_summed == CHECKSUM_UNNECESSARY) {
120 hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
fd2a0437
MR
121 } /* else everything is zero */
122
123 return 0;
124}
125
d66016a7 126#endif /* _LINUX_VIRTIO_NET_H */
This page took 0.842126 seconds and 4 git commands to generate.