]> git.proxmox.com Git - mirror_qemu.git/blame - hw/net/net_rx_pkt.c
Merge tag 'mem-2023-05-23' of https://github.com/davidhildenbrand/qemu into staging
[mirror_qemu.git] / hw / net / net_rx_pkt.c
CommitLineData
e263cd49 1/*
605d52e6 2 * QEMU RX packets abstractions
e263cd49
DF
3 *
4 * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
5 *
6 * Developed by Daynix Computing LTD (http://www.daynix.com)
7 *
8 * Authors:
9 * Dmitry Fleytman <dmitry@daynix.com>
10 * Tamir Shomer <tamirs@daynix.com>
11 * Yan Vugenfirer <yan@daynix.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2 or later.
14 * See the COPYING file in the top-level directory.
15 *
16 */
17
e8d40465 18#include "qemu/osdep.h"
907209e3 19#include "qemu/crc32c.h"
eb700029 20#include "trace.h"
605d52e6 21#include "net_rx_pkt.h"
e263cd49
DF
22#include "net/checksum.h"
23#include "net/tap.h"
24
605d52e6 25struct NetRxPkt {
e263cd49 26 struct virtio_net_hdr virt_hdr;
7edf2f1d
AO
27 struct {
28 struct eth_header eth;
29 struct vlan_header vlan;
30 } ehdr_buf;
eb700029
DF
31 struct iovec *vec;
32 uint16_t vec_len_total;
e263cd49
DF
33 uint16_t vec_len;
34 uint32_t tot_len;
35 uint16_t tci;
df8bf7a7 36 size_t ehdr_buf_len;
e263cd49
DF
37 eth_pkt_types_e packet_type;
38
39 /* Analysis results */
69ff5ef8
AO
40 bool hasip4;
41 bool hasip6;
eb700029
DF
42
43 size_t l3hdr_off;
44 size_t l4hdr_off;
45 size_t l5hdr_off;
46
47 eth_ip6_hdr_info ip6hdr_info;
48 eth_ip4_hdr_info ip4hdr_info;
49 eth_l4_hdr_info l4hdr_info;
e263cd49
DF
50};
51
aac8f89d 52void net_rx_pkt_init(struct NetRxPkt **pkt)
e263cd49 53{
605d52e6 54 struct NetRxPkt *p = g_malloc0(sizeof *p);
eb700029
DF
55 p->vec = NULL;
56 p->vec_len_total = 0;
e263cd49
DF
57 *pkt = p;
58}
59
605d52e6 60void net_rx_pkt_uninit(struct NetRxPkt *pkt)
e263cd49 61{
eb700029
DF
62 if (pkt->vec_len_total != 0) {
63 g_free(pkt->vec);
64 }
65
e263cd49
DF
66 g_free(pkt);
67}
68
605d52e6 69struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt)
e263cd49
DF
70{
71 assert(pkt);
72 return &pkt->virt_hdr;
73}
74
eb700029
DF
75static inline void
76net_rx_pkt_iovec_realloc(struct NetRxPkt *pkt,
77 int new_iov_len)
78{
79 if (pkt->vec_len_total < new_iov_len) {
80 g_free(pkt->vec);
81 pkt->vec = g_malloc(sizeof(*pkt->vec) * new_iov_len);
82 pkt->vec_len_total = new_iov_len;
83 }
84}
85
86static void
87net_rx_pkt_pull_data(struct NetRxPkt *pkt,
88 const struct iovec *iov, int iovcnt,
89 size_t ploff)
90{
002d394f
DF
91 uint32_t pllen = iov_size(iov, iovcnt) - ploff;
92
df8bf7a7 93 if (pkt->ehdr_buf_len) {
eb700029
DF
94 net_rx_pkt_iovec_realloc(pkt, iovcnt + 1);
95
7edf2f1d 96 pkt->vec[0].iov_base = &pkt->ehdr_buf;
df8bf7a7 97 pkt->vec[0].iov_len = pkt->ehdr_buf_len;
eb700029 98
002d394f 99 pkt->tot_len = pllen + pkt->ehdr_buf_len;
eb700029 100 pkt->vec_len = iov_copy(pkt->vec + 1, pkt->vec_len_total - 1,
002d394f 101 iov, iovcnt, ploff, pllen) + 1;
eb700029
DF
102 } else {
103 net_rx_pkt_iovec_realloc(pkt, iovcnt);
104
002d394f 105 pkt->tot_len = pllen;
eb700029
DF
106 pkt->vec_len = iov_copy(pkt->vec, pkt->vec_len_total,
107 iov, iovcnt, ploff, pkt->tot_len);
108 }
109
2f0fa232 110 eth_get_protocols(pkt->vec, pkt->vec_len, 0, &pkt->hasip4, &pkt->hasip6,
eb700029
DF
111 &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
112 &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
113
65f474bb 114 trace_net_rx_pkt_parsed(pkt->hasip4, pkt->hasip6, pkt->l4hdr_info.proto,
eb700029
DF
115 pkt->l3hdr_off, pkt->l4hdr_off, pkt->l5hdr_off);
116}
117
118void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
119 const struct iovec *iov, int iovcnt,
120 size_t iovoff, bool strip_vlan)
e263cd49
DF
121{
122 uint16_t tci = 0;
eb700029 123 uint16_t ploff = iovoff;
e263cd49 124 assert(pkt);
e263cd49
DF
125
126 if (strip_vlan) {
7edf2f1d 127 pkt->ehdr_buf_len = eth_strip_vlan(iov, iovcnt, iovoff, &pkt->ehdr_buf,
df8bf7a7
DF
128 &ploff, &tci);
129 } else {
130 pkt->ehdr_buf_len = 0;
e263cd49
DF
131 }
132
eb700029
DF
133 pkt->tci = tci;
134
135 net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
136}
137
138void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
139 const struct iovec *iov, int iovcnt,
7e64a9ca
AO
140 size_t iovoff, int strip_vlan_index,
141 uint16_t vet, uint16_t vet_ext)
eb700029
DF
142{
143 uint16_t tci = 0;
144 uint16_t ploff = iovoff;
145 assert(pkt);
eb700029 146
7e64a9ca
AO
147 pkt->ehdr_buf_len = eth_strip_vlan_ex(iov, iovcnt, iovoff,
148 strip_vlan_index, vet, vet_ext,
149 &pkt->ehdr_buf,
150 &ploff, &tci);
e263cd49
DF
151
152 pkt->tci = tci;
eb700029
DF
153
154 net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
e263cd49
DF
155}
156
605d52e6 157void net_rx_pkt_dump(struct NetRxPkt *pkt)
e263cd49 158{
605d52e6 159#ifdef NET_RX_PKT_DEBUG
e263cd49
DF
160 assert(pkt);
161
df8bf7a7
DF
162 printf("RX PKT: tot_len: %d, ehdr_buf_len: %lu, vlan_tag: %d\n",
163 pkt->tot_len, pkt->ehdr_buf_len, pkt->tci);
e263cd49
DF
164#endif
165}
166
605d52e6 167void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
e263cd49
DF
168 eth_pkt_types_e packet_type)
169{
170 assert(pkt);
171
172 pkt->packet_type = packet_type;
173
174}
175
605d52e6 176eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt)
e263cd49
DF
177{
178 assert(pkt);
179
180 return pkt->packet_type;
181}
182
605d52e6 183size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt)
e263cd49
DF
184{
185 assert(pkt);
186
187 return pkt->tot_len;
188}
189
2f0fa232
AO
190void net_rx_pkt_set_protocols(struct NetRxPkt *pkt,
191 const struct iovec *iov, size_t iovcnt,
192 size_t iovoff)
fcf0cdc3
SL
193{
194 assert(pkt);
195
2f0fa232 196 eth_get_protocols(iov, iovcnt, iovoff, &pkt->hasip4, &pkt->hasip6,
eb700029
DF
197 &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
198 &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
fcf0cdc3
SL
199}
200
605d52e6 201void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
69ff5ef8 202 bool *hasip4, bool *hasip6,
65f474bb 203 EthL4HdrProto *l4hdr_proto)
e263cd49
DF
204{
205 assert(pkt);
206
69ff5ef8
AO
207 *hasip4 = pkt->hasip4;
208 *hasip6 = pkt->hasip6;
65f474bb 209 *l4hdr_proto = pkt->l4hdr_info.proto;
e263cd49
DF
210}
211
eb700029
DF
212size_t net_rx_pkt_get_l3_hdr_offset(struct NetRxPkt *pkt)
213{
214 assert(pkt);
215 return pkt->l3hdr_off;
216}
217
218size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt)
219{
220 assert(pkt);
221 return pkt->l4hdr_off;
222}
223
224size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt)
225{
226 assert(pkt);
227 return pkt->l5hdr_off;
228}
229
230eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt)
231{
232 return &pkt->ip6hdr_info;
233}
234
235eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt)
236{
237 return &pkt->ip4hdr_info;
238}
239
eb700029
DF
240static inline void
241_net_rx_rss_add_chunk(uint8_t *rss_input, size_t *bytes_written,
242 void *ptr, size_t size)
243{
244 memcpy(&rss_input[*bytes_written], ptr, size);
245 trace_net_rx_pkt_rss_add_chunk(ptr, size, *bytes_written);
246 *bytes_written += size;
247}
248
249static inline void
250_net_rx_rss_prepare_ip4(uint8_t *rss_input,
251 struct NetRxPkt *pkt,
252 size_t *bytes_written)
253{
254 struct ip_header *ip4_hdr = &pkt->ip4hdr_info.ip4_hdr;
255
256 _net_rx_rss_add_chunk(rss_input, bytes_written,
257 &ip4_hdr->ip_src, sizeof(uint32_t));
258
259 _net_rx_rss_add_chunk(rss_input, bytes_written,
260 &ip4_hdr->ip_dst, sizeof(uint32_t));
261}
262
263static inline void
264_net_rx_rss_prepare_ip6(uint8_t *rss_input,
265 struct NetRxPkt *pkt,
266 bool ipv6ex, size_t *bytes_written)
267{
268 eth_ip6_hdr_info *ip6info = &pkt->ip6hdr_info;
269
270 _net_rx_rss_add_chunk(rss_input, bytes_written,
271 (ipv6ex && ip6info->rss_ex_src_valid) ? &ip6info->rss_ex_src
272 : &ip6info->ip6_hdr.ip6_src,
273 sizeof(struct in6_address));
274
275 _net_rx_rss_add_chunk(rss_input, bytes_written,
276 (ipv6ex && ip6info->rss_ex_dst_valid) ? &ip6info->rss_ex_dst
277 : &ip6info->ip6_hdr.ip6_dst,
278 sizeof(struct in6_address));
279}
280
281static inline void
282_net_rx_rss_prepare_tcp(uint8_t *rss_input,
283 struct NetRxPkt *pkt,
284 size_t *bytes_written)
285{
286 struct tcp_header *tcphdr = &pkt->l4hdr_info.hdr.tcp;
287
288 _net_rx_rss_add_chunk(rss_input, bytes_written,
289 &tcphdr->th_sport, sizeof(uint16_t));
290
291 _net_rx_rss_add_chunk(rss_input, bytes_written,
292 &tcphdr->th_dport, sizeof(uint16_t));
293}
294
33bbc05e
YB
295static inline void
296_net_rx_rss_prepare_udp(uint8_t *rss_input,
297 struct NetRxPkt *pkt,
298 size_t *bytes_written)
299{
300 struct udp_header *udphdr = &pkt->l4hdr_info.hdr.udp;
301
302 _net_rx_rss_add_chunk(rss_input, bytes_written,
303 &udphdr->uh_sport, sizeof(uint16_t));
304
305 _net_rx_rss_add_chunk(rss_input, bytes_written,
306 &udphdr->uh_dport, sizeof(uint16_t));
307}
308
eb700029
DF
309uint32_t
310net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
311 NetRxPktRssType type,
312 uint8_t *key)
313{
314 uint8_t rss_input[36];
315 size_t rss_length = 0;
316 uint32_t rss_hash = 0;
317 net_toeplitz_key key_data;
318
319 switch (type) {
320 case NetPktRssIpV4:
69ff5ef8 321 assert(pkt->hasip4);
eb700029
DF
322 trace_net_rx_pkt_rss_ip4();
323 _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
324 break;
325 case NetPktRssIpV4Tcp:
69ff5ef8 326 assert(pkt->hasip4);
65f474bb 327 assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP);
eb700029
DF
328 trace_net_rx_pkt_rss_ip4_tcp();
329 _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
330 _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
331 break;
332 case NetPktRssIpV6Tcp:
69ff5ef8 333 assert(pkt->hasip6);
65f474bb 334 assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP);
eb700029 335 trace_net_rx_pkt_rss_ip6_tcp();
2683a927 336 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
eb700029
DF
337 _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
338 break;
339 case NetPktRssIpV6:
69ff5ef8 340 assert(pkt->hasip6);
eb700029
DF
341 trace_net_rx_pkt_rss_ip6();
342 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
343 break;
344 case NetPktRssIpV6Ex:
69ff5ef8 345 assert(pkt->hasip6);
eb700029
DF
346 trace_net_rx_pkt_rss_ip6_ex();
347 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
348 break;
33bbc05e 349 case NetPktRssIpV6TcpEx:
69ff5ef8 350 assert(pkt->hasip6);
65f474bb 351 assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP);
33bbc05e
YB
352 trace_net_rx_pkt_rss_ip6_ex_tcp();
353 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
354 _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
355 break;
356 case NetPktRssIpV4Udp:
69ff5ef8 357 assert(pkt->hasip4);
65f474bb 358 assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP);
33bbc05e
YB
359 trace_net_rx_pkt_rss_ip4_udp();
360 _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
361 _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
362 break;
363 case NetPktRssIpV6Udp:
69ff5ef8 364 assert(pkt->hasip6);
65f474bb 365 assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP);
33bbc05e
YB
366 trace_net_rx_pkt_rss_ip6_udp();
367 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
368 _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
369 break;
370 case NetPktRssIpV6UdpEx:
69ff5ef8 371 assert(pkt->hasip6);
65f474bb 372 assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP);
33bbc05e
YB
373 trace_net_rx_pkt_rss_ip6_ex_udp();
374 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
375 _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
376 break;
eb700029
DF
377 default:
378 assert(false);
379 break;
380 }
381
382 net_toeplitz_key_init(&key_data, key);
383 net_toeplitz_add(&rss_hash, rss_input, rss_length, &key_data);
384
385 trace_net_rx_pkt_rss_hash(rss_length, rss_hash);
386
387 return rss_hash;
388}
389
390uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt)
391{
392 assert(pkt);
393
69ff5ef8 394 if (pkt->hasip4) {
eb700029
DF
395 return be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_id);
396 }
397
398 return 0;
399}
400
401bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt)
402{
403 assert(pkt);
404
65f474bb 405 if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP) {
eb700029
DF
406 return TCP_HEADER_FLAGS(&pkt->l4hdr_info.hdr.tcp) & TCP_FLAG_ACK;
407 }
408
409 return false;
410}
411
412bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt)
413{
414 assert(pkt);
415
65f474bb 416 if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP) {
eb700029
DF
417 return pkt->l4hdr_info.has_tcp_data;
418 }
419
420 return false;
421}
422
605d52e6 423struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt)
e263cd49
DF
424{
425 assert(pkt);
426
427 return pkt->vec;
428}
429
eb700029
DF
430uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt)
431{
432 assert(pkt);
433
434 return pkt->vec_len;
435}
436
605d52e6 437void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
e263cd49
DF
438 struct virtio_net_hdr *vhdr)
439{
440 assert(pkt);
441
442 memcpy(&pkt->virt_hdr, vhdr, sizeof pkt->virt_hdr);
443}
444
eb700029
DF
445void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
446 const struct iovec *iov, int iovcnt)
447{
448 assert(pkt);
449
450 iov_to_buf(iov, iovcnt, 0, &pkt->virt_hdr, sizeof pkt->virt_hdr);
451}
452
ffbd2dbd
AO
453void net_rx_pkt_unset_vhdr(struct NetRxPkt *pkt)
454{
455 assert(pkt);
456
457 memset(&pkt->virt_hdr, 0, sizeof(pkt->virt_hdr));
458}
459
605d52e6 460bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt)
e263cd49
DF
461{
462 assert(pkt);
463
df8bf7a7 464 return pkt->ehdr_buf_len ? true : false;
e263cd49
DF
465}
466
605d52e6 467uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt)
e263cd49
DF
468{
469 assert(pkt);
470
471 return pkt->tci;
472}
eb700029
DF
473
474bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid)
475{
476 uint32_t cntr;
477 uint16_t csum;
478 uint32_t csl;
479
480 trace_net_rx_pkt_l3_csum_validate_entry();
481
69ff5ef8 482 if (!pkt->hasip4) {
eb700029
DF
483 trace_net_rx_pkt_l3_csum_validate_not_ip4();
484 return false;
485 }
486
487 csl = pkt->l4hdr_off - pkt->l3hdr_off;
488
489 cntr = net_checksum_add_iov(pkt->vec, pkt->vec_len,
490 pkt->l3hdr_off,
491 csl, 0);
492
493 csum = net_checksum_finish(cntr);
494
495 *csum_valid = (csum == 0);
496
497 trace_net_rx_pkt_l3_csum_validate_csum(pkt->l3hdr_off, csl,
498 cntr, csum, *csum_valid);
499
500 return true;
501}
502
503static uint16_t
504_net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt)
505{
506 uint32_t cntr;
507 uint16_t csum;
508 uint16_t csl;
509 uint32_t cso;
510
511 trace_net_rx_pkt_l4_csum_calc_entry();
512
69ff5ef8 513 if (pkt->hasip4) {
65f474bb 514 if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP) {
eb700029
DF
515 csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
516 trace_net_rx_pkt_l4_csum_calc_ip4_udp();
517 } else {
518 csl = be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_len) -
519 IP_HDR_GET_LEN(&pkt->ip4hdr_info.ip4_hdr);
520 trace_net_rx_pkt_l4_csum_calc_ip4_tcp();
521 }
522
523 cntr = eth_calc_ip4_pseudo_hdr_csum(&pkt->ip4hdr_info.ip4_hdr,
524 csl, &cso);
525 trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
526 } else {
65f474bb 527 if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP) {
eb700029
DF
528 csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
529 trace_net_rx_pkt_l4_csum_calc_ip6_udp();
530 } else {
531 struct ip6_header *ip6hdr = &pkt->ip6hdr_info.ip6_hdr;
532 size_t full_ip6hdr_len = pkt->l4hdr_off - pkt->l3hdr_off;
533 size_t ip6opts_len = full_ip6hdr_len - sizeof(struct ip6_header);
534
535 csl = be16_to_cpu(ip6hdr->ip6_ctlun.ip6_un1.ip6_un1_plen) -
536 ip6opts_len;
537 trace_net_rx_pkt_l4_csum_calc_ip6_tcp();
538 }
539
540 cntr = eth_calc_ip6_pseudo_hdr_csum(&pkt->ip6hdr_info.ip6_hdr, csl,
541 pkt->ip6hdr_info.l4proto, &cso);
542 trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
543 }
544
545 cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len,
546 pkt->l4hdr_off, csl, cso);
547
0dacea92 548 csum = net_checksum_finish_nozero(cntr);
eb700029
DF
549
550 trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum);
551
552 return csum;
553}
554
907209e3
AO
555static bool
556_net_rx_pkt_validate_sctp_sum(struct NetRxPkt *pkt)
eb700029 557{
907209e3
AO
558 size_t csum_off;
559 size_t off = pkt->l4hdr_off;
560 size_t vec_len = pkt->vec_len;
561 struct iovec *vec;
562 uint32_t calculated = 0;
563 uint32_t original;
564 bool valid;
eb700029 565
907209e3
AO
566 for (vec = pkt->vec; vec->iov_len < off; vec++) {
567 off -= vec->iov_len;
568 vec_len--;
569 }
eb700029 570
907209e3
AO
571 csum_off = off + 8;
572
573 if (!iov_to_buf(vec, vec_len, csum_off, &original, sizeof(original))) {
eb700029
DF
574 return false;
575 }
576
907209e3
AO
577 if (!iov_from_buf(vec, vec_len, csum_off,
578 &calculated, sizeof(calculated))) {
eb700029
DF
579 return false;
580 }
581
907209e3
AO
582 calculated = crc32c(0xffffffff,
583 (uint8_t *)vec->iov_base + off, vec->iov_len - off);
584 calculated = iov_crc32c(calculated ^ 0xffffffff, vec + 1, vec_len - 1);
585 valid = calculated == le32_to_cpu(original);
586 iov_from_buf(vec, vec_len, csum_off, &original, sizeof(original));
587
588 return valid;
589}
590
591bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid)
592{
593 uint32_t csum;
594
595 trace_net_rx_pkt_l4_csum_validate_entry();
596
69ff5ef8 597 if (pkt->hasip4 && pkt->ip4hdr_info.fragment) {
eb700029
DF
598 trace_net_rx_pkt_l4_csum_validate_ip4_fragment();
599 return false;
600 }
601
907209e3
AO
602 switch (pkt->l4hdr_info.proto) {
603 case ETH_L4_HDR_PROTO_UDP:
604 if (pkt->l4hdr_info.hdr.udp.uh_sum == 0) {
605 trace_net_rx_pkt_l4_csum_validate_udp_with_no_checksum();
606 return false;
607 }
608 /* fall through */
609 case ETH_L4_HDR_PROTO_TCP:
610 csum = _net_rx_pkt_calc_l4_csum(pkt);
611 *csum_valid = ((csum == 0) || (csum == 0xFFFF));
612 break;
eb700029 613
907209e3
AO
614 case ETH_L4_HDR_PROTO_SCTP:
615 *csum_valid = _net_rx_pkt_validate_sctp_sum(pkt);
616 break;
617
618 default:
619 trace_net_rx_pkt_l4_csum_validate_not_xxp();
620 return false;
621 }
eb700029
DF
622
623 trace_net_rx_pkt_l4_csum_validate_csum(*csum_valid);
624
625 return true;
626}
627
628bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt)
629{
630 uint16_t csum = 0;
631 uint32_t l4_cso;
632
633 trace_net_rx_pkt_l4_csum_fix_entry();
634
65f474bb
AO
635 switch (pkt->l4hdr_info.proto) {
636 case ETH_L4_HDR_PROTO_TCP:
eb700029
DF
637 l4_cso = offsetof(struct tcp_header, th_sum);
638 trace_net_rx_pkt_l4_csum_fix_tcp(l4_cso);
65f474bb
AO
639 break;
640
641 case ETH_L4_HDR_PROTO_UDP:
eb700029
DF
642 if (pkt->l4hdr_info.hdr.udp.uh_sum == 0) {
643 trace_net_rx_pkt_l4_csum_fix_udp_with_no_checksum();
644 return false;
645 }
646 l4_cso = offsetof(struct udp_header, uh_sum);
647 trace_net_rx_pkt_l4_csum_fix_udp(l4_cso);
65f474bb
AO
648 break;
649
650 default:
eb700029
DF
651 trace_net_rx_pkt_l4_csum_fix_not_xxp();
652 return false;
653 }
654
69ff5ef8 655 if (pkt->hasip4 && pkt->ip4hdr_info.fragment) {
eb700029
DF
656 trace_net_rx_pkt_l4_csum_fix_ip4_fragment();
657 return false;
658 }
659
660 /* Set zero to checksum word */
661 iov_from_buf(pkt->vec, pkt->vec_len,
662 pkt->l4hdr_off + l4_cso,
663 &csum, sizeof(csum));
664
665 /* Calculate L4 checksum */
666 csum = cpu_to_be16(_net_rx_pkt_calc_l4_csum(pkt));
667
668 /* Set calculated checksum to checksum word */
669 iov_from_buf(pkt->vec, pkt->vec_len,
670 pkt->l4hdr_off + l4_cso,
671 &csum, sizeof(csum));
672
673 trace_net_rx_pkt_l4_csum_fix_csum(pkt->l4hdr_off + l4_cso, csum);
674
675 return true;
676}