1996-05-13 00:06:06 +00:00
|
|
|
/* packet.c
|
1996-04-11 06:42:28 +00:00
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
Packet assembly code, originally contributed by Archie Cobbs. */
|
1996-04-11 06:42:28 +00:00
|
|
|
|
|
|
|
/*
|
2014-09-08 09:31:32 -04:00
|
|
|
* Copyright (c) 2009,2012,2014 by Internet Systems Consortium, Inc. ("ISC")
|
2012-02-16 21:05:28 +00:00
|
|
|
* Copyright (c) 2004,2005,2007 by Internet Systems Consortium, Inc. ("ISC")
|
2005-03-17 20:15:29 +00:00
|
|
|
* Copyright (c) 1996-2003 by Internet Software Consortium
|
1996-04-11 06:42:28 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
1996-04-11 06:42:28 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1996-04-11 06:42:28 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* Internet Systems Consortium, Inc.
|
|
|
|
* 950 Charter Street
|
|
|
|
* Redwood City, CA 94063
|
|
|
|
* <info@isc.org>
|
2009-07-23 18:52:21 +00:00
|
|
|
* https://www.isc.org/
|
2000-03-17 04:00:32 +00:00
|
|
|
*
|
|
|
|
* This code was originally contributed by Archie Cobbs, and is still
|
|
|
|
* very similar to that contribution, although the packet checksum code
|
|
|
|
* has been hacked significantly with the help of quite a few ISC DHCP
|
|
|
|
* users, without whose gracious and thorough help the checksum code would
|
|
|
|
* still be disabled.
|
1996-04-11 06:42:28 +00:00
|
|
|
*/
|
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
#include "dhcpd.h"
|
1997-03-29 03:18:28 +00:00
|
|
|
|
1996-05-20 00:25:35 +00:00
|
|
|
#if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)
|
|
|
|
#include "includes/netinet/ip.h"
|
|
|
|
#include "includes/netinet/udp.h"
|
|
|
|
#include "includes/netinet/if_ether.h"
|
1997-03-29 03:18:28 +00:00
|
|
|
#endif /* PACKET_ASSEMBLY || PACKET_DECODING */
|
1996-04-11 06:42:28 +00:00
|
|
|
|
1996-05-20 00:25:35 +00:00
|
|
|
/* Compute the easy part of the checksum on a range of bytes. */
|
|
|
|
|
1997-03-06 18:27:55 +00:00
|
|
|
u_int32_t checksum (buf, nbytes, sum)
|
1996-05-20 00:25:35 +00:00
|
|
|
unsigned char *buf;
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned nbytes;
|
1996-05-20 00:25:35 +00:00
|
|
|
u_int32_t sum;
|
|
|
|
{
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned i;
|
1996-05-20 00:25:35 +00:00
|
|
|
|
1996-05-22 11:23:46 +00:00
|
|
|
#ifdef DEBUG_CHECKSUM
|
1999-02-24 17:56:53 +00:00
|
|
|
log_debug ("checksum (%x %d %x)", buf, nbytes, sum);
|
1996-05-22 11:23:46 +00:00
|
|
|
#endif
|
|
|
|
|
1996-05-20 00:25:35 +00:00
|
|
|
/* Checksum all the pairs of bytes first... */
|
1999-10-07 06:36:35 +00:00
|
|
|
for (i = 0; i < (nbytes & ~1U); i += 2) {
|
1996-05-22 11:23:46 +00:00
|
|
|
#ifdef DEBUG_CHECKSUM_VERBOSE
|
1999-02-24 17:56:53 +00:00
|
|
|
log_debug ("sum = %x", sum);
|
1996-05-22 11:23:46 +00:00
|
|
|
#endif
|
1997-05-09 08:07:33 +00:00
|
|
|
sum += (u_int16_t) ntohs(*((u_int16_t *)(buf + i)));
|
1999-04-23 15:38:57 +00:00
|
|
|
/* Add carry. */
|
|
|
|
if (sum > 0xFFFF)
|
|
|
|
sum -= 0xFFFF;
|
1996-05-20 00:25:35 +00:00
|
|
|
}
|
1997-03-05 20:06:03 +00:00
|
|
|
|
1996-05-20 00:25:35 +00:00
|
|
|
/* If there's a single byte left over, checksum it, too. Network
|
|
|
|
byte order is big-endian, so the remaining byte is the high byte. */
|
|
|
|
if (i < nbytes) {
|
1996-05-22 11:23:46 +00:00
|
|
|
#ifdef DEBUG_CHECKSUM_VERBOSE
|
1999-02-24 17:56:53 +00:00
|
|
|
log_debug ("sum = %x", sum);
|
1996-05-22 11:23:46 +00:00
|
|
|
#endif
|
1997-05-09 08:07:33 +00:00
|
|
|
sum += buf [i] << 8;
|
1999-04-23 15:38:57 +00:00
|
|
|
/* Add carry. */
|
|
|
|
if (sum > 0xFFFF)
|
|
|
|
sum -= 0xFFFF;
|
1996-05-20 00:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
1999-04-23 15:38:57 +00:00
|
|
|
/* Finish computing the checksum, and then put it into network byte order. */
|
1996-05-20 00:25:35 +00:00
|
|
|
|
1997-03-06 18:27:55 +00:00
|
|
|
u_int32_t wrapsum (sum)
|
1996-05-20 00:25:35 +00:00
|
|
|
u_int32_t sum;
|
|
|
|
{
|
1996-05-22 11:23:46 +00:00
|
|
|
#ifdef DEBUG_CHECKSUM
|
1999-02-24 17:56:53 +00:00
|
|
|
log_debug ("wrapsum (%x)", sum);
|
1996-05-22 11:23:46 +00:00
|
|
|
#endif
|
1997-03-05 20:06:03 +00:00
|
|
|
|
1999-04-23 15:38:57 +00:00
|
|
|
sum = ~sum & 0xFFFF;
|
1996-05-22 11:23:46 +00:00
|
|
|
#ifdef DEBUG_CHECKSUM_VERBOSE
|
1999-02-24 17:56:53 +00:00
|
|
|
log_debug ("sum = %x", sum);
|
1996-05-22 11:23:46 +00:00
|
|
|
#endif
|
1996-05-20 00:25:35 +00:00
|
|
|
|
1996-05-22 11:23:46 +00:00
|
|
|
#ifdef DEBUG_CHECKSUM
|
1999-02-24 17:56:53 +00:00
|
|
|
log_debug ("wrapsum returns %x", htons (sum));
|
1996-05-22 11:23:46 +00:00
|
|
|
#endif
|
1997-03-05 20:06:03 +00:00
|
|
|
return htons(sum);
|
1996-05-20 00:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PACKET_ASSEMBLY
|
1996-05-13 00:06:06 +00:00
|
|
|
void assemble_hw_header (interface, buf, bufix, to)
|
|
|
|
struct interface_info *interface;
|
|
|
|
unsigned char *buf;
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned *bufix;
|
1996-05-13 00:06:06 +00:00
|
|
|
struct hardware *to;
|
|
|
|
{
|
2012-02-16 21:05:28 +00:00
|
|
|
switch (interface->hw_address.hbuf[0]) {
|
|
|
|
#if defined(HAVE_TR_SUPPORT)
|
|
|
|
case HTYPE_IEEE802:
|
|
|
|
assemble_tr_header(interface, buf, bufix, to);
|
|
|
|
break;
|
2000-03-24 00:22:41 +00:00
|
|
|
#endif
|
|
|
|
#if defined (DEC_FDDI)
|
2012-02-16 21:05:28 +00:00
|
|
|
case HTYPE_FDDI:
|
|
|
|
assemble_fddi_header(interface, buf, bufix, to);
|
|
|
|
break;
|
1996-06-11 08:30:41 +00:00
|
|
|
#endif
|
2012-02-16 21:05:28 +00:00
|
|
|
case HTYPE_INFINIBAND:
|
|
|
|
log_error("Attempt to assemble hw header for infiniband");
|
|
|
|
break;
|
|
|
|
case HTYPE_ETHER:
|
|
|
|
default:
|
|
|
|
assemble_ethernet_header(interface, buf, bufix, to);
|
|
|
|
break;
|
|
|
|
}
|
1996-05-13 00:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* UDP header and IP header assembled together for convenience. */
|
1996-04-11 06:42:28 +00:00
|
|
|
|
1996-05-22 07:28:23 +00:00
|
|
|
void assemble_udp_ip_header (interface, buf, bufix,
|
|
|
|
from, to, port, data, len)
|
1996-05-13 00:06:06 +00:00
|
|
|
struct interface_info *interface;
|
|
|
|
unsigned char *buf;
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned *bufix;
|
1996-05-22 07:28:23 +00:00
|
|
|
u_int32_t from;
|
|
|
|
u_int32_t to;
|
1998-03-16 06:16:54 +00:00
|
|
|
u_int32_t port;
|
1996-05-13 00:06:06 +00:00
|
|
|
unsigned char *data;
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned len;
|
1996-05-13 00:06:06 +00:00
|
|
|
{
|
|
|
|
struct ip ip;
|
|
|
|
struct udphdr udp;
|
|
|
|
|
2009-02-20 00:09:24 +00:00
|
|
|
memset (&ip, 0, sizeof ip);
|
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
/* Fill out the IP header */
|
2000-02-01 03:19:56 +00:00
|
|
|
IP_V_SET (&ip, 4);
|
|
|
|
IP_HL_SET (&ip, 20);
|
1996-05-13 00:06:06 +00:00
|
|
|
ip.ip_tos = IPTOS_LOWDELAY;
|
|
|
|
ip.ip_len = htons(sizeof(ip) + sizeof(udp) + len);
|
|
|
|
ip.ip_id = 0;
|
|
|
|
ip.ip_off = 0;
|
2006-07-19 18:00:36 +00:00
|
|
|
ip.ip_ttl = 128;
|
1996-05-13 00:06:06 +00:00
|
|
|
ip.ip_p = IPPROTO_UDP;
|
|
|
|
ip.ip_sum = 0;
|
1996-05-22 07:28:23 +00:00
|
|
|
ip.ip_src.s_addr = from;
|
|
|
|
ip.ip_dst.s_addr = to;
|
1996-05-13 00:06:06 +00:00
|
|
|
|
|
|
|
/* Checksum the IP header... */
|
|
|
|
ip.ip_sum = wrapsum (checksum ((unsigned char *)&ip, sizeof ip, 0));
|
|
|
|
|
|
|
|
/* Copy the ip header into the buffer... */
|
|
|
|
memcpy (&buf [*bufix], &ip, sizeof ip);
|
|
|
|
*bufix += sizeof ip;
|
|
|
|
|
|
|
|
/* Fill out the UDP header */
|
1997-02-18 14:32:51 +00:00
|
|
|
udp.uh_sport = local_port; /* XXX */
|
1996-05-13 00:06:06 +00:00
|
|
|
udp.uh_dport = port; /* XXX */
|
|
|
|
udp.uh_ulen = htons(sizeof(udp) + len);
|
|
|
|
memset (&udp.uh_sum, 0, sizeof udp.uh_sum);
|
|
|
|
|
|
|
|
/* Compute UDP checksums, including the ``pseudo-header'', the UDP
|
|
|
|
header and the data. */
|
|
|
|
|
|
|
|
udp.uh_sum =
|
|
|
|
wrapsum (checksum ((unsigned char *)&udp, sizeof udp,
|
|
|
|
checksum (data, len,
|
|
|
|
checksum ((unsigned char *)
|
|
|
|
&ip.ip_src,
|
1999-03-26 19:19:46 +00:00
|
|
|
2 * sizeof ip.ip_src,
|
1996-05-13 00:06:06 +00:00
|
|
|
IPPROTO_UDP +
|
|
|
|
(u_int32_t)
|
|
|
|
ntohs (udp.uh_ulen)))));
|
|
|
|
|
|
|
|
/* Copy the udp header into the buffer... */
|
|
|
|
memcpy (&buf [*bufix], &udp, sizeof udp);
|
|
|
|
*bufix += sizeof udp;
|
|
|
|
}
|
1996-05-20 00:25:35 +00:00
|
|
|
#endif /* PACKET_ASSEMBLY */
|
1996-04-11 06:42:28 +00:00
|
|
|
|
1996-05-20 00:25:35 +00:00
|
|
|
#ifdef PACKET_DECODING
|
1996-05-13 00:06:06 +00:00
|
|
|
/* Decode a hardware header... */
|
2012-02-16 21:05:28 +00:00
|
|
|
/* Support for ethernet, TR and FDDI
|
|
|
|
* Doesn't support infiniband yet as the supported oses shouldn't get here
|
|
|
|
*/
|
1996-04-11 06:42:28 +00:00
|
|
|
|
1997-06-08 04:08:01 +00:00
|
|
|
ssize_t decode_hw_header (interface, buf, bufix, from)
|
1996-05-13 00:06:06 +00:00
|
|
|
struct interface_info *interface;
|
|
|
|
unsigned char *buf;
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned bufix;
|
1996-05-13 00:06:06 +00:00
|
|
|
struct hardware *from;
|
|
|
|
{
|
2012-02-16 21:05:28 +00:00
|
|
|
switch(interface->hw_address.hbuf[0]) {
|
1999-05-27 17:43:28 +00:00
|
|
|
#if defined (HAVE_TR_SUPPORT)
|
2012-02-16 21:05:28 +00:00
|
|
|
case HTYPE_IEEE802:
|
|
|
|
return (decode_tr_header(interface, buf, bufix, from));
|
2000-03-24 00:22:41 +00:00
|
|
|
#endif
|
|
|
|
#if defined (DEC_FDDI)
|
2012-02-16 21:05:28 +00:00
|
|
|
case HTYPE_FDDI:
|
|
|
|
return (decode_fddi_header(interface, buf, bufix, from));
|
1996-05-16 07:26:52 +00:00
|
|
|
#endif
|
2012-02-16 21:05:28 +00:00
|
|
|
case HTYPE_INFINIBAND:
|
|
|
|
log_error("Attempt to decode hw header for infiniband");
|
|
|
|
return (0);
|
|
|
|
case HTYPE_ETHER:
|
|
|
|
default:
|
|
|
|
return (decode_ethernet_header(interface, buf, bufix, from));
|
|
|
|
}
|
1996-04-11 06:42:28 +00:00
|
|
|
}
|
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
/* UDP header and IP header decoded together for convenience. */
|
1996-04-11 06:42:28 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
ssize_t
|
|
|
|
decode_udp_ip_header(struct interface_info *interface,
|
|
|
|
unsigned char *buf, unsigned bufix,
|
|
|
|
struct sockaddr_in *from, unsigned buflen,
|
|
|
|
unsigned *rbuflen)
|
1996-04-11 06:42:28 +00:00
|
|
|
{
|
2005-03-17 20:15:29 +00:00
|
|
|
unsigned char *data;
|
|
|
|
struct ip ip;
|
2007-04-27 23:54:06 +00:00
|
|
|
struct udphdr udp;
|
|
|
|
unsigned char *upp, *endbuf;
|
|
|
|
u_int32_t ip_len, ulen, pkt_len;
|
1996-05-13 00:06:06 +00:00
|
|
|
u_int32_t sum, usum;
|
2014-09-08 09:31:32 -04:00
|
|
|
static unsigned int ip_packets_seen = 0;
|
|
|
|
static unsigned int ip_packets_bad_checksum = 0;
|
|
|
|
static unsigned int udp_packets_seen = 0;
|
|
|
|
static unsigned int udp_packets_bad_checksum = 0;
|
|
|
|
static unsigned int udp_packets_length_checked = 0;
|
|
|
|
static unsigned int udp_packets_length_overflow = 0;
|
1999-10-07 06:36:35 +00:00
|
|
|
unsigned len;
|
1996-05-16 07:26:52 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
/* Designate the end of the input buffer for bounds checks. */
|
|
|
|
endbuf = buf + bufix + buflen;
|
|
|
|
|
|
|
|
/* Assure there is at least an IP header there. */
|
|
|
|
if ((buf + bufix + sizeof(ip)) > endbuf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Copy the IP header into a stack aligned structure for inspection.
|
|
|
|
* There may be bits in the IP header that we're not decoding, so we
|
|
|
|
* copy out the bits we grok and skip ahead by ip.ip_hl * 4.
|
|
|
|
*/
|
|
|
|
upp = buf + bufix;
|
|
|
|
memcpy(&ip, upp, sizeof(ip));
|
|
|
|
ip_len = (*upp & 0x0f) << 2;
|
|
|
|
upp += ip_len;
|
|
|
|
|
|
|
|
/* Check the IP packet length. */
|
|
|
|
pkt_len = ntohs(ip.ip_len);
|
|
|
|
if (pkt_len > buflen)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Assure after ip_len bytes that there is enough room for a UDP header. */
|
|
|
|
if ((upp + sizeof(udp)) > endbuf)
|
|
|
|
return -1;
|
|
|
|
|
2007-11-30 21:51:43 +00:00
|
|
|
/* Copy the UDP header into a stack aligned structure for inspection. */
|
2007-04-27 23:54:06 +00:00
|
|
|
memcpy(&udp, upp, sizeof(udp));
|
1996-05-16 07:26:52 +00:00
|
|
|
|
|
|
|
#ifdef USERLAND_FILTER
|
|
|
|
/* Is it a UDP packet? */
|
2005-03-17 20:15:29 +00:00
|
|
|
if (ip.ip_p != IPPROTO_UDP)
|
1996-05-16 07:26:52 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Is it to the port we're serving? */
|
2007-04-27 23:54:06 +00:00
|
|
|
if (udp.uh_dport != local_port)
|
1996-05-16 07:26:52 +00:00
|
|
|
return -1;
|
|
|
|
#endif /* USERLAND_FILTER */
|
1996-05-13 00:06:06 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
ulen = ntohs(udp.uh_ulen);
|
|
|
|
if (ulen < sizeof(udp))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
udp_packets_length_checked++;
|
|
|
|
if ((upp + ulen) > endbuf) {
|
|
|
|
udp_packets_length_overflow++;
|
2014-09-08 09:31:32 -04:00
|
|
|
if (((udp_packets_length_checked > 4) &&
|
|
|
|
(udp_packets_length_overflow != 0)) &&
|
|
|
|
((udp_packets_length_checked / udp_packets_length_overflow) < 2)) {
|
|
|
|
log_info("%u udp packets in %u too long - dropped",
|
2007-04-27 23:54:06 +00:00
|
|
|
udp_packets_length_overflow,
|
|
|
|
udp_packets_length_checked);
|
|
|
|
udp_packets_length_overflow = 0;
|
|
|
|
udp_packets_length_checked = 0;
|
|
|
|
}
|
|
|
|
return -1;
|
2000-10-13 18:54:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 09:31:32 -04:00
|
|
|
/* If at least 5 with less than 50% bad, start over */
|
|
|
|
if (udp_packets_length_checked > 4) {
|
|
|
|
udp_packets_length_overflow = 0;
|
|
|
|
udp_packets_length_checked = 0;
|
|
|
|
}
|
2007-04-27 23:54:06 +00:00
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
/* Check the IP header checksum - it should be zero. */
|
2014-09-08 09:31:32 -04:00
|
|
|
ip_packets_seen++;
|
1996-05-13 00:06:06 +00:00
|
|
|
if (wrapsum (checksum (buf + bufix, ip_len, 0))) {
|
1999-04-12 21:37:03 +00:00
|
|
|
++ip_packets_bad_checksum;
|
2014-09-08 09:31:32 -04:00
|
|
|
if (((ip_packets_seen > 4) && (ip_packets_bad_checksum != 0)) &&
|
|
|
|
((ip_packets_seen / ip_packets_bad_checksum) < 2)) {
|
|
|
|
log_info ("%u bad IP checksums seen in %u packets",
|
1999-04-12 21:37:03 +00:00
|
|
|
ip_packets_bad_checksum, ip_packets_seen);
|
|
|
|
ip_packets_seen = ip_packets_bad_checksum = 0;
|
|
|
|
}
|
1996-05-16 07:26:52 +00:00
|
|
|
return -1;
|
1996-05-13 00:06:06 +00:00
|
|
|
}
|
1996-04-11 06:42:28 +00:00
|
|
|
|
2014-09-08 09:31:32 -04:00
|
|
|
/* If at least 5 with less than 50% bad, start over */
|
|
|
|
if (ip_packets_seen > 4) {
|
|
|
|
ip_packets_bad_checksum = 0;
|
|
|
|
ip_packets_seen = 0;
|
|
|
|
}
|
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
/* Copy out the IP source address... */
|
2007-04-27 23:54:06 +00:00
|
|
|
memcpy(&from->sin_addr, &ip.ip_src, 4);
|
1996-04-11 06:42:28 +00:00
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
/* Compute UDP checksums, including the ``pseudo-header'', the UDP
|
|
|
|
header and the data. If the UDP checksum field is zero, we're
|
|
|
|
not supposed to do a checksum. */
|
1996-04-11 06:42:28 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
data = upp + sizeof(udp);
|
|
|
|
len = ulen - sizeof(udp);
|
1996-04-11 06:42:28 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
usum = udp.uh_sum;
|
|
|
|
udp.uh_sum = 0;
|
1996-05-16 07:26:52 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
/* XXX: We have to pass &udp, because we have to zero the checksum
|
|
|
|
* field before calculating the sum...'upp' isn't zeroed.
|
|
|
|
*/
|
|
|
|
sum = wrapsum(checksum((unsigned char *)&udp, sizeof(udp),
|
|
|
|
checksum(data, len,
|
|
|
|
checksum((unsigned char *)&ip.ip_src,
|
|
|
|
8, IPPROTO_UDP + ulen))));
|
1996-05-13 00:06:06 +00:00
|
|
|
|
1999-04-23 22:15:43 +00:00
|
|
|
udp_packets_seen++;
|
1996-05-13 00:06:06 +00:00
|
|
|
if (usum && usum != sum) {
|
1999-04-23 15:38:57 +00:00
|
|
|
udp_packets_bad_checksum++;
|
2014-09-08 09:31:32 -04:00
|
|
|
if (((udp_packets_seen > 4) && (udp_packets_bad_checksum != 0)) &&
|
|
|
|
((udp_packets_seen / udp_packets_bad_checksum) < 2)) {
|
|
|
|
log_info ("%u bad udp checksums in %u packets",
|
1999-04-12 21:37:03 +00:00
|
|
|
udp_packets_bad_checksum, udp_packets_seen);
|
|
|
|
udp_packets_seen = udp_packets_bad_checksum = 0;
|
|
|
|
}
|
1996-05-13 00:06:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1996-04-11 06:42:28 +00:00
|
|
|
|
2014-09-08 09:31:32 -04:00
|
|
|
/* If at least 5 with less than 50% bad, start over */
|
|
|
|
if (udp_packets_seen > 4) {
|
|
|
|
udp_packets_bad_checksum = 0;
|
|
|
|
udp_packets_seen = 0;
|
|
|
|
}
|
|
|
|
|
1996-05-13 00:06:06 +00:00
|
|
|
/* Copy out the port... */
|
2007-04-27 23:54:06 +00:00
|
|
|
memcpy (&from -> sin_port, &udp.uh_sport, sizeof udp.uh_sport);
|
|
|
|
|
|
|
|
/* Save the length of the UDP payload. */
|
|
|
|
if (rbuflen != NULL)
|
|
|
|
*rbuflen = len;
|
1996-05-13 00:06:06 +00:00
|
|
|
|
2007-04-27 23:54:06 +00:00
|
|
|
/* Return the index to the UDP payload. */
|
|
|
|
return ip_len + sizeof udp;
|
1996-04-11 06:42:28 +00:00
|
|
|
}
|
1996-05-20 00:25:35 +00:00
|
|
|
#endif /* PACKET_DECODING */
|