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,
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,
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 */
69int 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}
#define IPV4_GET_RAW_HLEN(ip4h)
Definition decode-ipv4.h:96
#define TCP_GET_RAW_HLEN(tcph)
Definition decode-tcp.h:72
#define UDP_HEADER_LEN
Definition decode-udp.h:27
uint16_t ip_csum
Definition decode-ipv4.h:80
union PacketL3::Hdrs hdrs
union PacketL4::L4Hdrs hdrs
struct PacketL4 l4
Definition decode.h:601
struct PacketL3 l3
Definition decode.h:600
uint16_t payload_len
Definition decode.h:606
uint16_t th_sum
Definition decode-tcp.h:157
uint16_t uh_sum
Definition decode-udp.h:46
IPV6Hdr * ip6h
Definition decode.h:440
IPV4Hdr * ip4h
Definition decode.h:439
UDPHdr * udph
Definition decode.h:470
TCPHdr * tcph
Definition decode.h:469
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.
int ReCalculateChecksum(Packet *p)
#define CHECKSUM_INVALID_RATIO
#define CHECKSUM_SAMPLE_COUNT
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition util-debug.h:225