Coverage Report

Created: 2026-10-31 08:12

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