blob: 88997022a4b5ba7542f00e5a0706e48f5264281d [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>
Willem de Bruijn92741242020-05-04 12:48:54 -04006#include <uapi/linux/tcp.h>
7#include <uapi/linux/udp.h>
Mike Rapoportfd2a0432016-06-08 16:09:18 +03008#include <uapi/linux/virtio_net.h>
9
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +000010static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,
11 const struct virtio_net_hdr *hdr)
12{
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);
17 break;
18 case VIRTIO_NET_HDR_GSO_TCPV6:
19 skb->protocol = cpu_to_be16(ETH_P_IPV6);
20 break;
21 default:
22 return -EINVAL;
23 }
24
25 return 0;
26}
27
Mike Rapoportfd2a0432016-06-08 16:09:18 +030028static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
29 const struct virtio_net_hdr *hdr,
30 bool little_endian)
31{
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050032 unsigned int gso_type = 0;
Willem de Bruijn92741242020-05-04 12:48:54 -040033 unsigned int thlen = 0;
Willem de Bruijn6dd912f2020-05-25 15:07:40 -040034 unsigned int p_off = 0;
Willem de Bruijn92741242020-05-04 12:48:54 -040035 unsigned int ip_proto;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030036
37 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
38 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
39 case VIRTIO_NET_HDR_GSO_TCPV4:
40 gso_type = SKB_GSO_TCPV4;
Willem de Bruijn92741242020-05-04 12:48:54 -040041 ip_proto = IPPROTO_TCP;
42 thlen = sizeof(struct tcphdr);
Mike Rapoportfd2a0432016-06-08 16:09:18 +030043 break;
44 case VIRTIO_NET_HDR_GSO_TCPV6:
45 gso_type = SKB_GSO_TCPV6;
Willem de Bruijn92741242020-05-04 12:48:54 -040046 ip_proto = IPPROTO_TCP;
47 thlen = sizeof(struct tcphdr);
Mike Rapoportfd2a0432016-06-08 16:09:18 +030048 break;
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050049 case VIRTIO_NET_HDR_GSO_UDP:
50 gso_type = SKB_GSO_UDP;
Willem de Bruijn92741242020-05-04 12:48:54 -040051 ip_proto = IPPROTO_UDP;
52 thlen = sizeof(struct udphdr);
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050053 break;
Mike Rapoportfd2a0432016-06-08 16:09:18 +030054 default:
55 return -EINVAL;
56 }
57
58 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
59 gso_type |= SKB_GSO_TCP_ECN;
60
61 if (hdr->gso_size == 0)
62 return -EINVAL;
63 }
64
65 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
66 u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
67 u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
68
69 if (!skb_partial_csum_set(skb, start, off))
70 return -EINVAL;
Willem de Bruijn92741242020-05-04 12:48:54 -040071
Willem de Bruijn6dd912f2020-05-25 15:07:40 -040072 p_off = skb_transport_offset(skb) + thlen;
73 if (p_off > skb_headlen(skb))
Willem de Bruijn92741242020-05-04 12:48:54 -040074 return -EINVAL;
Willem de Bruijnd5be7f62019-02-15 12:15:47 -050075 } else {
76 /* gso packets without NEEDS_CSUM do not set transport_offset.
77 * probe and drop if does not match one of the above types.
78 */
Willem de Bruijn9e8db592019-02-18 23:37:12 -050079 if (gso_type && skb->network_header) {
Willem de Bruijn92741242020-05-04 12:48:54 -040080 struct flow_keys_basic keys;
81
Willem de Bruijn9e8db592019-02-18 23:37:12 -050082 if (!skb->protocol)
83 virtio_net_hdr_set_proto(skb, hdr);
84retry:
Willem de Bruijn92741242020-05-04 12:48:54 -040085 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
86 NULL, 0, 0, 0,
87 0)) {
Willem de Bruijn9e8db592019-02-18 23:37:12 -050088 /* UFO does not specify ipv4 or 6: try both */
89 if (gso_type & SKB_GSO_UDP &&
90 skb->protocol == htons(ETH_P_IP)) {
91 skb->protocol = htons(ETH_P_IPV6);
92 goto retry;
93 }
Willem de Bruijnd5be7f62019-02-15 12:15:47 -050094 return -EINVAL;
Willem de Bruijn9e8db592019-02-18 23:37:12 -050095 }
Willem de Bruijn92741242020-05-04 12:48:54 -040096
Willem de Bruijn6dd912f2020-05-25 15:07:40 -040097 p_off = keys.control.thoff + thlen;
98 if (p_off > skb_headlen(skb) ||
Willem de Bruijn92741242020-05-04 12:48:54 -040099 keys.basic.ip_proto != ip_proto)
100 return -EINVAL;
101
102 skb_set_transport_header(skb, keys.control.thoff);
Willem de Bruijn6dd912f2020-05-25 15:07:40 -0400103 } else if (gso_type) {
104 p_off = thlen;
105 if (p_off > skb_headlen(skb))
106 return -EINVAL;
Willem de Bruijnd5be7f62019-02-15 12:15:47 -0500107 }
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300108 }
109
110 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
111 u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
112
Willem de Bruijn6dd912f2020-05-25 15:07:40 -0400113 if (skb->len - p_off <= gso_size)
114 return -EINVAL;
115
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300116 skb_shinfo(skb)->gso_size = gso_size;
117 skb_shinfo(skb)->gso_type = gso_type;
118
119 /* Header must be checked, and gso_segs computed. */
120 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
121 skb_shinfo(skb)->gso_segs = 0;
122 }
123
124 return 0;
125}
126
127static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
128 struct virtio_net_hdr *hdr,
Jason Wang6391a442017-01-20 14:32:42 +0800129 bool little_endian,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -0400130 bool has_data_valid,
131 int vlan_hlen)
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300132{
Jarno Rajahalme9403cd72016-11-18 15:40:40 -0800133 memset(hdr, 0, sizeof(*hdr)); /* no info leak */
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300134
135 if (skb_is_gso(skb)) {
136 struct skb_shared_info *sinfo = skb_shinfo(skb);
137
138 /* This is a hint as to how much should be linear. */
139 hdr->hdr_len = __cpu_to_virtio16(little_endian,
140 skb_headlen(skb));
141 hdr->gso_size = __cpu_to_virtio16(little_endian,
142 sinfo->gso_size);
143 if (sinfo->gso_type & SKB_GSO_TCPV4)
144 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
145 else if (sinfo->gso_type & SKB_GSO_TCPV6)
146 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300147 else
148 return -EINVAL;
149 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
150 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
151 } else
152 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
153
154 if (skb->ip_summed == CHECKSUM_PARTIAL) {
155 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Willem de Bruijnfd3a8862018-06-06 11:23:01 -0400156 hdr->csum_start = __cpu_to_virtio16(little_endian,
157 skb_checksum_start_offset(skb) + vlan_hlen);
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300158 hdr->csum_offset = __cpu_to_virtio16(little_endian,
159 skb->csum_offset);
Jason Wang6391a442017-01-20 14:32:42 +0800160 } else if (has_data_valid &&
161 skb->ip_summed == CHECKSUM_UNNECESSARY) {
162 hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
Mike Rapoportfd2a0432016-06-08 16:09:18 +0300163 } /* else everything is zero */
164
165 return 0;
166}
167
Jarno Rajahalmed66016a2016-11-18 15:40:39 -0800168#endif /* _LINUX_VIRTIO_NET_H */