suricata
util-checksum.c
Go to the documentation of this file.
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
{
31
if
(
PKT_IS_IPV4
(p)) {
32
if
(
PKT_IS_TCP
(p)) {
33
/* TCP */
34
p->
tcph
->
th_sum
= 0;
35
p->
tcph
->
th_sum
= TCPChecksum(p->
ip4h
->s_ip_addrs,
36
(uint16_t *)p->
tcph
, (p->
payload_len
+
TCP_GET_HLEN
(p)), 0);
37
}
else
if
(
PKT_IS_UDP
(p)) {
38
p->
udph
->
uh_sum
= 0;
39
p->
udph
->
uh_sum
= UDPV4Checksum(p->
ip4h
->s_ip_addrs,
40
(uint16_t *)p->
udph
, (p->
payload_len
+
UDP_HEADER_LEN
), 0);
41
}
42
/* IPV4 */
43
p->
ip4h
->
ip_csum
= 0;
44
p->
ip4h
->
ip_csum
= IPV4Checksum((uint16_t *)p->
ip4h
,
45
IPV4_GET_RAW_HLEN
(p->
ip4h
), 0);
46
}
else
if
(
PKT_IS_IPV6
(p)) {
47
/* just TCP for IPV6 */
48
if
(
PKT_IS_TCP
(p)) {
49
p->
tcph
->
th_sum
= 0;
50
p->
tcph
->
th_sum
= TCPV6Checksum(p->
ip6h
->s_ip6_addrs,
51
(uint16_t *)p->
tcph
, (p->
payload_len
+
TCP_GET_HLEN
(p)), 0);
52
}
else
if
(
PKT_IS_UDP
(p)) {
53
p->
udph
->
uh_sum
= 0;
54
p->
udph
->
uh_sum
= UDPV6Checksum(p->
ip6h
->s_ip6_addrs,
55
(uint16_t *)p->
udph
, (p->
payload_len
+
UDP_HEADER_LEN
), 0);
56
}
57
}
58
59
return
0;
60
}
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
{
72
if
(thread_count ==
CHECKSUM_SAMPLE_COUNT
) {
73
if
(iface_fail != 0) {
74
if
((iface_count / iface_fail) <
CHECKSUM_INVALID_RATIO
) {
75
SCLogInfo
(
"More than 1/%dth of packets have an invalid "
76
"checksum, assuming checksum offloading is used "
77
"(%"
PRIu64
"/%"
PRIu64
")"
,
78
CHECKSUM_INVALID_RATIO
, iface_fail, iface_count);
79
return
1;
80
}
else
{
81
SCLogInfo
(
"Less than 1/%dth of packets have an invalid "
82
"checksum, assuming checksum offloading is NOT used "
83
"(%"
PRIu64
"/%"
PRIu64
")"
,
CHECKSUM_INVALID_RATIO
,
84
iface_fail, iface_count);
85
}
86
}
else
{
87
SCLogInfo
(
"No packets with invalid checksum, assuming "
88
"checksum offloading is NOT used"
);
89
}
90
}
91
return
0;
92
}
PKT_IS_UDP
#define PKT_IS_UDP(p)
Definition:
decode.h:249
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition:
decode.h:247
util-checksum.h
CHECKSUM_SAMPLE_COUNT
#define CHECKSUM_SAMPLE_COUNT
Definition:
util-checksum.h:35
Packet_::payload_len
uint16_t payload_len
Definition:
decode.h:587
ChecksumAutoModeCheck
int ChecksumAutoModeCheck(uint64_t thread_count, uint64_t iface_count, uint64_t iface_fail)
Check if the number of invalid checksums indicate checksum offloading in place.
Definition:
util-checksum.c:69
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition:
decode.h:248
TCPHdr_::th_sum
uint16_t th_sum
Definition:
decode-tcp.h:150
IPV4_GET_RAW_HLEN
#define IPV4_GET_RAW_HLEN(ip4h)
Definition:
decode-ipv4.h:96
Packet_
Definition:
decode.h:437
Packet_::ip4h
IPV4Hdr * ip4h
Definition:
decode.h:545
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition:
util-debug.h:224
CHECKSUM_INVALID_RATIO
#define CHECKSUM_INVALID_RATIO
Definition:
util-checksum.h:36
ReCalculateChecksum
int ReCalculateChecksum(Packet *p)
Definition:
util-checksum.c:29
suricata-common.h
Packet_::tcph
TCPHdr * tcph
Definition:
decode.h:567
IPV4Hdr_::ip_csum
uint16_t ip_csum
Definition:
decode-ipv4.h:80
TCP_GET_HLEN
#define TCP_GET_HLEN(p)
Definition:
decode-tcp.h:111
Packet_::udph
UDPHdr * udph
Definition:
decode.h:569
UDP_HEADER_LEN
#define UDP_HEADER_LEN
Definition:
decode-udp.h:27
Packet_::ip6h
IPV6Hdr * ip6h
Definition:
decode.h:547
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition:
decode.h:246
UDPHdr_::uh_sum
uint16_t uh_sum
Definition:
decode-udp.h:46
src
util-checksum.c
Generated on Thu Apr 25 2024 23:30:44 for suricata by
1.8.18