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 checskum.
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "util-checksum.h"
29 
31 {
32  if (PKT_IS_IPV4(p)) {
33  if (PKT_IS_TCP(p)) {
34  /* TCP */
35  p->tcph->th_sum = 0;
36  p->tcph->th_sum = TCPChecksum(p->ip4h->s_ip_addrs,
37  (uint16_t *)p->tcph, (p->payload_len + TCP_GET_HLEN(p)), 0);
38  } else if (PKT_IS_UDP(p)) {
39  p->udph->uh_sum = 0;
40  p->udph->uh_sum = UDPV4Checksum(p->ip4h->s_ip_addrs,
41  (uint16_t *)p->udph, (p->payload_len + UDP_HEADER_LEN), 0);
42  }
43  /* IPV4 */
44  p->ip4h->ip_csum = 0;
45  p->ip4h->ip_csum = IPV4Checksum((uint16_t *)p->ip4h,
46  IPV4_GET_RAW_HLEN(p->ip4h), 0);
47  } else if (PKT_IS_IPV6(p)) {
48  /* just TCP for IPV6 */
49  if (PKT_IS_TCP(p)) {
50  p->tcph->th_sum = 0;
51  p->tcph->th_sum = TCPV6Checksum(p->ip6h->s_ip6_addrs,
52  (uint16_t *)p->tcph, (p->payload_len + TCP_GET_HLEN(p)), 0);
53  } else if (PKT_IS_UDP(p)) {
54  p->udph->uh_sum = 0;
55  p->udph->uh_sum = UDPV6Checksum(p->ip6h->s_ip6_addrs,
56  (uint16_t *)p->udph, (p->payload_len + UDP_HEADER_LEN), 0);
57  }
58  }
59 
60  return 0;
61 }
62 
63 /**
64  * \brief Check if the number of invalid checksums indicate checksum
65  * offloading in place.
66  *
67  * \retval 1 yes, offloading in place
68  * \retval 0 no, no offloading used
69  */
70 int ChecksumAutoModeCheck(uint64_t thread_count,
71  uint64_t iface_count, uint64_t iface_fail)
72 {
73  if (thread_count == CHECKSUM_SAMPLE_COUNT) {
74  if (iface_fail != 0) {
75  if ((iface_count / iface_fail) < CHECKSUM_INVALID_RATIO) {
76  SCLogInfo("More than 1/%dth of packets have an invalid "
77  "checksum, assuming checksum offloading is used "
78  "(%"PRIu64"/%"PRIu64")",
79  CHECKSUM_INVALID_RATIO, iface_fail, iface_count);
80  return 1;
81  } else {
82  SCLogInfo("Less than 1/%dth of packets have an invalid "
83  "checksum, assuming checksum offloading is NOT used "
84  "(%"PRIu64"/%"PRIu64")", CHECKSUM_INVALID_RATIO,
85  iface_fail, iface_count);
86  }
87  } else {
88  SCLogInfo("No packets with invalid checksum, assuming "
89  "checksum offloading is NOT used");
90  }
91  }
92  return 0;
93 }
UDPHdr * udph
Definition: decode.h:524
#define PKT_IS_IPV6(p)
Definition: decode.h:252
#define CHECKSUM_SAMPLE_COUNT
Definition: util-checksum.h:33
TCPHdr * tcph
Definition: decode.h:522
#define PKT_IS_IPV4(p)
Definition: decode.h:251
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:70
IPV6Hdr * ip6h
Definition: decode.h:502
int ReCalculateChecksum(Packet *p)
Definition: util-checksum.c:30
IPV4Hdr * ip4h
Definition: decode.h:500
#define IPV4_GET_RAW_HLEN(ip4h)
Definition: decode-ipv4.h:95
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
#define PKT_IS_TCP(p)
Definition: decode.h:253
#define CHECKSUM_INVALID_RATIO
Definition: util-checksum.h:34
uint16_t ip_csum
Definition: decode-ipv4.h:79
#define TCP_GET_HLEN(p)
Definition: decode-tcp.h:104
#define PKT_IS_UDP(p)
Definition: decode.h:254
uint16_t payload_len
Definition: decode.h:541
#define UDP_HEADER_LEN
Definition: decode-udp.h:27