Coverage Report

Created: 2025-12-31 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/util-checksum.c
Line
Count
Source
1
/* Copyright (C) 2011-2012 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
/**
19
 * \file
20
 *
21
 * \author Eric Leblond <eric@regit.org>
22
 *
23
 * Util functions for checksum.
24
 */
25
26
#include "suricata-common.h"
27
#include "util-checksum.h"
28
29
int ReCalculateChecksum(Packet *p)
30
0
{
31
0
    if (PKT_IS_IPV4(p)) {
32
0
        if (PKT_IS_TCP(p)) {
33
            /* TCP */
34
0
            p->tcph->th_sum = 0;
35
0
            p->tcph->th_sum = TCPChecksum(p->ip4h->s_ip_addrs,
36
0
                    (uint16_t *)p->tcph, (p->payload_len + TCP_GET_HLEN(p)), 0);
37
0
        } else if (PKT_IS_UDP(p)) {
38
0
            p->udph->uh_sum = 0;
39
0
            p->udph->uh_sum = UDPV4Checksum(p->ip4h->s_ip_addrs,
40
0
                    (uint16_t *)p->udph, (p->payload_len + UDP_HEADER_LEN), 0);
41
0
        }
42
        /* IPV4 */
43
0
        p->ip4h->ip_csum = 0;
44
0
        p->ip4h->ip_csum = IPV4Checksum((uint16_t *)p->ip4h,
45
0
                IPV4_GET_RAW_HLEN(p->ip4h), 0);
46
0
    } else if (PKT_IS_IPV6(p)) {
47
        /* just TCP for IPV6 */
48
0
        if (PKT_IS_TCP(p)) {
49
0
            p->tcph->th_sum = 0;
50
0
            p->tcph->th_sum = TCPV6Checksum(p->ip6h->s_ip6_addrs,
51
0
                    (uint16_t *)p->tcph, (p->payload_len + TCP_GET_HLEN(p)), 0);
52
0
        } else if (PKT_IS_UDP(p)) {
53
0
            p->udph->uh_sum = 0;
54
0
            p->udph->uh_sum = UDPV6Checksum(p->ip6h->s_ip6_addrs,
55
0
                    (uint16_t *)p->udph, (p->payload_len + UDP_HEADER_LEN), 0);
56
0
        }
57
0
    }
58
59
0
    return 0;
60
0
}
61
62
/**
63
 *  \brief Check if the number of invalid checksums indicate checksum
64
 *         offloading in place.
65
 *
66
 *  \retval 1 yes, offloading in place
67
 *  \retval 0 no, no offloading used
68
 */
69
int ChecksumAutoModeCheck(uint64_t thread_count,
70
        uint64_t iface_count, uint64_t iface_fail)
71
0
{
72
0
    if (thread_count == CHECKSUM_SAMPLE_COUNT) {
73
0
        if (iface_fail != 0) {
74
0
            if ((iface_count / iface_fail) < CHECKSUM_INVALID_RATIO) {
75
0
                SCLogInfo("More than 1/%dth of packets have an invalid "
76
0
                        "checksum, assuming checksum offloading is used "
77
0
                        "(%"PRIu64"/%"PRIu64")",
78
0
                        CHECKSUM_INVALID_RATIO, iface_fail, iface_count);
79
0
                return 1;
80
0
            } else {
81
0
                SCLogInfo("Less than 1/%dth of packets have an invalid "
82
0
                        "checksum, assuming checksum offloading is NOT used "
83
0
                        "(%"PRIu64"/%"PRIu64")", CHECKSUM_INVALID_RATIO,
84
0
                        iface_fail, iface_count);
85
0
            }
86
0
        } else {
87
0
            SCLogInfo("No packets with invalid checksum, assuming "
88
0
                    "checksum offloading is NOT used");
89
0
        }
90
0
    }
91
0
    return 0;
92
0
}