You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/src/decode-icmpv6.c

163 lines
5.2 KiB
C

/* Copyright (c) 2008 Victor Julien <victor@inliniac.net> */
#include "eidps-common.h"
#include "decode.h"
#include "decode-icmpv6.h"
#include "util-unittest.h"
/**
* \brief Calculates the checksum for the ICMPV6 packet
*
* \param shdr Pointer to source address field from the IPV6 packet. Used as a
* part of the psuedoheader for computing the checksum
* \param pkt Pointer to the start of the ICMPV6 packet
* \param tlen Total length of the ICMPV6 packet(header + payload)
*
* \retval csum Checksum for the ICMPV6 packet
*/
inline uint16_t ICMPV6CalculateChecksum(uint16_t *shdr, uint16_t *pkt,
uint16_t tlen)
{
uint16_t pad = 0;
uint32_t csum = shdr[0];
csum += shdr[1] + shdr[2] + shdr[3] + shdr[4] + shdr[5] + shdr[6] +
shdr[7] + shdr[8] + shdr[9] + shdr[10] + shdr[11] + shdr[12] +
shdr[13] + shdr[14] + shdr[15] + htons(58 + tlen);
csum += pkt[0];
tlen -= 4;
pkt += 2;
while (tlen >= 64) {
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
pkt[14] + pkt[15] + pkt[16] + pkt[17] + pkt[18] + pkt[19] +
pkt[20] + pkt[21] + pkt[22] + pkt[23] + pkt[24] + pkt[25] +
pkt[26] + pkt[27] + pkt[28] + pkt[29] + pkt[30] + pkt[31];
tlen -= 64;
pkt += 32;
}
while (tlen >= 32) {
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
pkt[14] + pkt[15];
tlen -= 32;
pkt += 16;
}
while(tlen >= 8) {
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
tlen -= 8;
pkt += 4;
}
while(tlen >= 4) {
csum += pkt[0] + pkt[1];
tlen -= 4;
pkt += 2;
}
while (tlen > 1) {
csum += pkt[0];
tlen -= 2;
pkt += 1;
}
if (tlen == 1) {
*(uint8_t *)(&pad) = (*(uint8_t *)pkt);
csum += pad;
}
csum = (csum >> 16) + (csum & 0x0000FFFF);
return (uint16_t) ~csum;
}
void DecodeICMPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, uint16_t len, PacketQueue *pq)
{
PerfCounterIncr(dtv->counter_icmpv6, tv->pca);
if (len < ICMPV6_HEADER_LEN) {
/** \todo decode event */
return;
}
p->icmpv6h = (ICMPV6Hdr *)pkt;
#ifdef DEBUG
printf("ICMPV6 TYPE %" PRIu32 " CODE %" PRIu32 "\n", p->icmpv6h->type, p->icmpv6h->code);
#endif
p->proto = IPPROTO_ICMPV6;
return;
}
static int ICMPV6CalculateValidChecksumtest01(void) {
uint16_t csum = 0;
uint8_t raw_ipv6[] = {
0x00, 0x00, 0x86, 0x05, 0x80, 0xda, 0x00, 0x60,
0x97, 0x07, 0x69, 0xea, 0x86, 0xdd, 0x60, 0x00,
0x00, 0x00, 0x00, 0x44, 0x3a, 0x40, 0x3f, 0xfe,
0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x60,
0x97, 0xff, 0xfe, 0x07, 0x69, 0xea, 0x3f, 0xfe,
0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x03, 0x00,
0xf7, 0x52, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00, 0x14, 0x11, 0x01, 0x3f, 0xfe,
0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0xa0, 0x75,
0x82, 0x9b, 0x00, 0x14, 0x82, 0x8b, 0x01, 0x01,
0x00, 0x00, 0xf9, 0xc8, 0xe7, 0x36, 0xf5, 0xed,
0x08, 0x00};
csum = *( ((uint16_t *)(raw_ipv6 + 56)));
return (csum == ICMPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8),
(uint16_t *)(raw_ipv6 + 54), 68));
}
static int ICMPV6CalculateInvalidChecksumtest02(void) {
uint16_t csum = 0;
uint8_t raw_ipv6[] = {
0x00, 0x00, 0x86, 0x05, 0x80, 0xda, 0x00, 0x60,
0x97, 0x07, 0x69, 0xea, 0x86, 0xdd, 0x60, 0x00,
0x00, 0x00, 0x00, 0x44, 0x3a, 0x40, 0x3f, 0xfe,
0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x60,
0x97, 0xff, 0xfe, 0x07, 0x69, 0xea, 0x3f, 0xfe,
0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x03, 0x00,
0xf7, 0x52, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00, 0x14, 0x11, 0x01, 0x3f, 0xfe,
0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0xa0, 0x75,
0x82, 0x9b, 0x00, 0x14, 0x82, 0x8b, 0x01, 0x01,
0x00, 0x00, 0xf9, 0xc8, 0xe7, 0x36, 0xf5, 0xed,
0x08, 0x01};
csum = *( ((uint16_t *)(raw_ipv6 + 56)));
return (csum == ICMPV6CalculateChecksum((uint16_t *)(raw_ipv6 + 14 + 8),
(uint16_t *)(raw_ipv6 + 54), 68));
}
/**
* \brief Registers ICMPV6 unit tests
* \todo More ICMPv6 tests
*/
void DecodeICMPV6RegisterTests(void)
{
UtRegisterTest("ICMPV6CalculateValidChecksumtest01",
ICMPV6CalculateValidChecksumtest01, 1);
UtRegisterTest("ICMPV6CalculateInValidChecksumtest02",
ICMPV6CalculateInvalidChecksumtest02, 0);
}