]> Git Repo - J-linux.git/blob - include/linux/virtio_net.h
Merge tag 'apparmor-pr-2024-07-25' of git://git.kernel.org/pub/scm/linux/kernel/git...
[J-linux.git] / include / linux / virtio_net.h
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 <linux/ip.h>
7 #include <linux/ipv6.h>
8 #include <linux/udp.h>
9 #include <uapi/linux/tcp.h>
10 #include <uapi/linux/virtio_net.h>
11
12 static inline bool virtio_net_hdr_match_proto(__be16 protocol, __u8 gso_type)
13 {
14         switch (gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
15         case VIRTIO_NET_HDR_GSO_TCPV4:
16                 return protocol == cpu_to_be16(ETH_P_IP);
17         case VIRTIO_NET_HDR_GSO_TCPV6:
18                 return protocol == cpu_to_be16(ETH_P_IPV6);
19         case VIRTIO_NET_HDR_GSO_UDP:
20         case VIRTIO_NET_HDR_GSO_UDP_L4:
21                 return protocol == cpu_to_be16(ETH_P_IP) ||
22                        protocol == cpu_to_be16(ETH_P_IPV6);
23         default:
24                 return false;
25         }
26 }
27
28 static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,
29                                            const struct virtio_net_hdr *hdr)
30 {
31         if (skb->protocol)
32                 return 0;
33
34         switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
35         case VIRTIO_NET_HDR_GSO_TCPV4:
36         case VIRTIO_NET_HDR_GSO_UDP:
37         case VIRTIO_NET_HDR_GSO_UDP_L4:
38                 skb->protocol = cpu_to_be16(ETH_P_IP);
39                 break;
40         case VIRTIO_NET_HDR_GSO_TCPV6:
41                 skb->protocol = cpu_to_be16(ETH_P_IPV6);
42                 break;
43         default:
44                 return -EINVAL;
45         }
46
47         return 0;
48 }
49
50 static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
51                                         const struct virtio_net_hdr *hdr,
52                                         bool little_endian)
53 {
54         unsigned int nh_min_len = sizeof(struct iphdr);
55         unsigned int gso_type = 0;
56         unsigned int thlen = 0;
57         unsigned int p_off = 0;
58         unsigned int ip_proto;
59         u64 ret, remainder, gso_size;
60
61         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
62                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
63                 case VIRTIO_NET_HDR_GSO_TCPV4:
64                         gso_type = SKB_GSO_TCPV4;
65                         ip_proto = IPPROTO_TCP;
66                         thlen = sizeof(struct tcphdr);
67                         break;
68                 case VIRTIO_NET_HDR_GSO_TCPV6:
69                         gso_type = SKB_GSO_TCPV6;
70                         ip_proto = IPPROTO_TCP;
71                         thlen = sizeof(struct tcphdr);
72                         nh_min_len = sizeof(struct ipv6hdr);
73                         break;
74                 case VIRTIO_NET_HDR_GSO_UDP:
75                         gso_type = SKB_GSO_UDP;
76                         ip_proto = IPPROTO_UDP;
77                         thlen = sizeof(struct udphdr);
78                         break;
79                 case VIRTIO_NET_HDR_GSO_UDP_L4:
80                         gso_type = SKB_GSO_UDP_L4;
81                         ip_proto = IPPROTO_UDP;
82                         thlen = sizeof(struct udphdr);
83                         break;
84                 default:
85                         return -EINVAL;
86                 }
87
88                 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
89                         gso_type |= SKB_GSO_TCP_ECN;
90
91                 if (hdr->gso_size == 0)
92                         return -EINVAL;
93         }
94
95         skb_reset_mac_header(skb);
96
97         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
98                 u32 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
99                 u32 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
100                 u32 needed = start + max_t(u32, thlen, off + sizeof(__sum16));
101
102                 if (hdr->gso_size) {
103                         gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
104                         ret = div64_u64_rem(skb->len, gso_size, &remainder);
105                         if (!(ret && (hdr->gso_size > needed) &&
106                                                 ((remainder > needed) || (remainder == 0)))) {
107                                 return -EINVAL;
108                         }
109                         skb_shinfo(skb)->tx_flags |= SKBFL_SHARED_FRAG;
110                 }
111
112                 if (!pskb_may_pull(skb, needed))
113                         return -EINVAL;
114
115                 if (!skb_partial_csum_set(skb, start, off))
116                         return -EINVAL;
117
118                 nh_min_len = max_t(u32, nh_min_len, skb_transport_offset(skb));
119                 p_off = nh_min_len + thlen;
120                 if (!pskb_may_pull(skb, p_off))
121                         return -EINVAL;
122         } else {
123                 /* gso packets without NEEDS_CSUM do not set transport_offset.
124                  * probe and drop if does not match one of the above types.
125                  */
126                 if (gso_type && skb->network_header) {
127                         struct flow_keys_basic keys;
128
129                         if (!skb->protocol) {
130                                 __be16 protocol = dev_parse_header_protocol(skb);
131
132                                 if (!protocol)
133                                         virtio_net_hdr_set_proto(skb, hdr);
134                                 else if (!virtio_net_hdr_match_proto(protocol, hdr->gso_type))
135                                         return -EINVAL;
136                                 else
137                                         skb->protocol = protocol;
138                         }
139 retry:
140                         if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
141                                                               NULL, 0, 0, 0,
142                                                               0)) {
143                                 /* UFO does not specify ipv4 or 6: try both */
144                                 if (gso_type & SKB_GSO_UDP &&
145                                     skb->protocol == htons(ETH_P_IP)) {
146                                         skb->protocol = htons(ETH_P_IPV6);
147                                         goto retry;
148                                 }
149                                 return -EINVAL;
150                         }
151
152                         p_off = keys.control.thoff + thlen;
153                         if (!pskb_may_pull(skb, p_off) ||
154                             keys.basic.ip_proto != ip_proto)
155                                 return -EINVAL;
156
157                         skb_set_transport_header(skb, keys.control.thoff);
158                 } else if (gso_type) {
159                         p_off = nh_min_len + thlen;
160                         if (!pskb_may_pull(skb, p_off))
161                                 return -EINVAL;
162                 }
163         }
164
165         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
166                 u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
167                 unsigned int nh_off = p_off;
168                 struct skb_shared_info *shinfo = skb_shinfo(skb);
169
170                 switch (gso_type & ~SKB_GSO_TCP_ECN) {
171                 case SKB_GSO_UDP:
172                         /* UFO may not include transport header in gso_size. */
173                         nh_off -= thlen;
174                         break;
175                 case SKB_GSO_UDP_L4:
176                         if (!(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
177                                 return -EINVAL;
178                         if (skb->csum_offset != offsetof(struct udphdr, check))
179                                 return -EINVAL;
180                         if (skb->len - p_off > gso_size * UDP_MAX_SEGMENTS)
181                                 return -EINVAL;
182                         if (gso_type != SKB_GSO_UDP_L4)
183                                 return -EINVAL;
184                         break;
185                 }
186
187                 /* Kernel has a special handling for GSO_BY_FRAGS. */
188                 if (gso_size == GSO_BY_FRAGS)
189                         return -EINVAL;
190
191                 /* Too small packets are not really GSO ones. */
192                 if (skb->len - nh_off > gso_size) {
193                         shinfo->gso_size = gso_size;
194                         shinfo->gso_type = gso_type;
195
196                         /* Header must be checked, and gso_segs computed. */
197                         shinfo->gso_type |= SKB_GSO_DODGY;
198                         shinfo->gso_segs = 0;
199                 }
200         }
201
202         return 0;
203 }
204
205 static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
206                                           struct virtio_net_hdr *hdr,
207                                           bool little_endian,
208                                           bool has_data_valid,
209                                           int vlan_hlen)
210 {
211         memset(hdr, 0, sizeof(*hdr));   /* no info leak */
212
213         if (skb_is_gso(skb)) {
214                 struct skb_shared_info *sinfo = skb_shinfo(skb);
215
216                 /* This is a hint as to how much should be linear. */
217                 hdr->hdr_len = __cpu_to_virtio16(little_endian,
218                                                  skb_headlen(skb));
219                 hdr->gso_size = __cpu_to_virtio16(little_endian,
220                                                   sinfo->gso_size);
221                 if (sinfo->gso_type & SKB_GSO_TCPV4)
222                         hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
223                 else if (sinfo->gso_type & SKB_GSO_TCPV6)
224                         hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
225                 else if (sinfo->gso_type & SKB_GSO_UDP_L4)
226                         hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP_L4;
227                 else
228                         return -EINVAL;
229                 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
230                         hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
231         } else
232                 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
233
234         if (skb->ip_summed == CHECKSUM_PARTIAL) {
235                 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
236                 hdr->csum_start = __cpu_to_virtio16(little_endian,
237                         skb_checksum_start_offset(skb) + vlan_hlen);
238                 hdr->csum_offset = __cpu_to_virtio16(little_endian,
239                                 skb->csum_offset);
240         } else if (has_data_valid &&
241                    skb->ip_summed == CHECKSUM_UNNECESSARY) {
242                 hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
243         } /* else everything is zero */
244
245         return 0;
246 }
247
248 #endif /* _LINUX_VIRTIO_NET_H */
This page took 0.041177 seconds and 4 git commands to generate.