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 
30 {
31  if (PacketIsIPv4(p)) {
32  IPV4Hdr *ip4h = p->l3.hdrs.ip4h;
33  if (PacketIsTCP(p)) {
34  /* TCP */
35  p->l4.hdrs.tcph->th_sum = 0;
36  p->l4.hdrs.tcph->th_sum = TCPChecksum(ip4h->s_ip_addrs, (uint16_t *)p->l4.hdrs.tcph,
37  (p->payload_len + TCP_GET_RAW_HLEN(p->l4.hdrs.tcph)), 0);
38  } else if (PacketIsUDP(p)) {
39  p->l4.hdrs.udph->uh_sum = 0;
40  p->l4.hdrs.udph->uh_sum = UDPV4Checksum(ip4h->s_ip_addrs, (uint16_t *)p->l4.hdrs.udph,
41  (p->payload_len + UDP_HEADER_LEN), 0);
42  }
43  /* IPV4 */
44  ip4h->ip_csum = 0;
45  ip4h->ip_csum = IPV4Checksum((uint16_t *)ip4h, IPV4_GET_RAW_HLEN(ip4h), 0);
46  } else if (PacketIsIPv6(p)) {
47  IPV6Hdr *ip6h = p->l3.hdrs.ip6h;
48  if (PacketIsTCP(p)) {
49  p->l4.hdrs.tcph->th_sum = 0;
50  p->l4.hdrs.tcph->th_sum = TCPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)p->l4.hdrs.tcph,
51  (p->payload_len + TCP_GET_RAW_HLEN(p->l4.hdrs.tcph)), 0);
52  } else if (PacketIsUDP(p)) {
53  p->l4.hdrs.udph->uh_sum = 0;
54  p->l4.hdrs.udph->uh_sum = UDPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)p->l4.hdrs.udph,
55  (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 }
util-checksum.h
PacketL4::L4Hdrs::udph
UDPHdr * udph
Definition: decode.h:442
CHECKSUM_SAMPLE_COUNT
#define CHECKSUM_SAMPLE_COUNT
Definition: util-checksum.h:35
TCP_GET_RAW_HLEN
#define TCP_GET_RAW_HLEN(tcph)
Definition: decode-tcp.h:72
PacketL3::hdrs
union PacketL3::Hdrs hdrs
PacketL3::Hdrs::ip4h
IPV4Hdr * ip4h
Definition: decode.h:411
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:572
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
PacketL3::Hdrs::ip6h
IPV6Hdr * ip6h
Definition: decode.h:412
TCPHdr_::th_sum
uint16_t th_sum
Definition: decode-tcp.h:157
IPV4_GET_RAW_HLEN
#define IPV4_GET_RAW_HLEN(ip4h)
Definition: decode-ipv4.h:96
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:473
Packet_::l4
struct PacketL4 l4
Definition: decode.h:567
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
IPV4Hdr_
Definition: decode-ipv4.h:72
suricata-common.h
PacketL4::L4Hdrs::tcph
TCPHdr * tcph
Definition: decode.h:441
Packet_::l3
struct PacketL3 l3
Definition: decode.h:566
PacketL4::hdrs
union PacketL4::L4Hdrs hdrs
IPV4Hdr_::ip_csum
uint16_t ip_csum
Definition: decode-ipv4.h:80
UDP_HEADER_LEN
#define UDP_HEADER_LEN
Definition: decode-udp.h:27
UDPHdr_::uh_sum
uint16_t uh_sum
Definition: decode-udp.h:46