|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
#include "eidps-common.h"
|
|
|
|
|
#include "decode.h"
|
|
|
|
|
#include "decode-events.h"
|
|
|
|
|
#include "decode-icmpv4.h"
|
|
|
|
|
#include "util-unittest.h"
|
|
|
|
|
|
|
|
|
|
@ -57,6 +58,14 @@ inline uint16_t ICMPV4CalculateChecksum(uint16_t *pkt, uint16_t tlen)
|
|
|
|
|
return (uint16_t) ~csum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \todo
|
|
|
|
|
* Note, this is the IP header, plus a bit of the original packet, not the whole thing!
|
|
|
|
|
*/
|
|
|
|
|
void DecodePartialIPV4( uint8_t* partial_packet, uint16_t len )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** DecodeICMPV4
|
|
|
|
|
* \brief Main ICMPv4 decoding function
|
|
|
|
|
*/
|
|
|
|
|
@ -65,7 +74,7 @@ void DecodeICMPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt
|
|
|
|
|
PerfCounterIncr(dtv->counter_icmpv4, tv->pca);
|
|
|
|
|
|
|
|
|
|
if (len < ICMPV4_HEADER_LEN) {
|
|
|
|
|
/** \todo decode event */
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_PKT_TOO_SMALL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -77,6 +86,129 @@ void DecodeICMPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt
|
|
|
|
|
|
|
|
|
|
p->proto = IPPROTO_ICMP;
|
|
|
|
|
|
|
|
|
|
ICMPV4ExtHdr* icmp4eh = (ICMPV4ExtHdr*) p->icmpv4h;
|
|
|
|
|
|
|
|
|
|
switch (p->icmpv4h->type)
|
|
|
|
|
{
|
|
|
|
|
case ICMP_ECHOREPLY:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_DEST_UNREACH:
|
|
|
|
|
if (p->icmpv4h->code>ICMP_SR_FAILED) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
} else {
|
|
|
|
|
// parse IP header plus 64 bytes
|
|
|
|
|
if (len >= ICMPV4_HEADER_PKT_OFFSET)
|
|
|
|
|
DecodePartialIPV4( (uint8_t*) (p->icmpv4h + ICMPV4_HEADER_PKT_OFFSET), len - ICMPV4_HEADER_PKT_OFFSET );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_SOURCE_QUENCH:
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
} else {
|
|
|
|
|
// parse IP header plus 64 bytes
|
|
|
|
|
if (len >= ICMPV4_HEADER_PKT_OFFSET)
|
|
|
|
|
DecodePartialIPV4( (uint8_t*) (p->icmpv4h + ICMPV4_HEADER_PKT_OFFSET), len - ICMPV4_HEADER_PKT_OFFSET );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_REDIRECT:
|
|
|
|
|
if (p->icmpv4h->code>ICMP_REDIR_HOSTTOS) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
} else {
|
|
|
|
|
// parse IP header plus 64 bytes
|
|
|
|
|
if (len >= ICMPV4_HEADER_PKT_OFFSET)
|
|
|
|
|
DecodePartialIPV4( (uint8_t*) (p->icmpv4h + ICMPV4_HEADER_PKT_OFFSET), len - ICMPV4_HEADER_PKT_OFFSET );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_ECHO:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_TIME_EXCEEDED:
|
|
|
|
|
if (p->icmpv4h->code>ICMP_EXC_FRAGTIME) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
} else {
|
|
|
|
|
// parse IP header plus 64 bytes
|
|
|
|
|
if (len >= ICMPV4_HEADER_PKT_OFFSET)
|
|
|
|
|
DecodePartialIPV4( (uint8_t*) (p->icmpv4h + ICMPV4_HEADER_PKT_OFFSET), len - ICMPV4_HEADER_PKT_OFFSET );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_PARAMETERPROB:
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
} else {
|
|
|
|
|
// parse IP header plus 64 bytes
|
|
|
|
|
if (len >= ICMPV4_HEADER_PKT_OFFSET)
|
|
|
|
|
DecodePartialIPV4( (uint8_t*) (p->icmpv4h + ICMPV4_HEADER_PKT_OFFSET), len - ICMPV4_HEADER_PKT_OFFSET );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_TIMESTAMP:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_TIMESTAMPREPLY:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_INFO_REQUEST:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_INFO_REPLY:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_ADDRESS:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ICMP_ADDRESSREPLY:
|
|
|
|
|
p->icmpv4vars.id=icmp4eh->id;
|
|
|
|
|
p->icmpv4vars.seq=icmp4eh->seq;
|
|
|
|
|
if (p->icmpv4h->code!=0) {
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_CODE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
DECODER_SET_EVENT(p,ICMPV4_UNKNOWN_TYPE);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -84,7 +216,7 @@ void DecodeICMPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt
|
|
|
|
|
|
|
|
|
|
/** DecodeICMPV4test01
|
|
|
|
|
* \brief
|
|
|
|
|
* \retval 0 Expected test value
|
|
|
|
|
* \retval 1 Expected test value
|
|
|
|
|
*/
|
|
|
|
|
static int DecodeICMPV4test01(void) {
|
|
|
|
|
uint8_t raw_icmpv4[] = {
|
|
|
|
|
@ -104,12 +236,19 @@ static int DecodeICMPV4test01(void) {
|
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
|
|
|
|
|
|
DecodeICMPV4(&tv, &dtv, &p, raw_icmpv4, sizeof(raw_icmpv4), NULL);
|
|
|
|
|
|
|
|
|
|
if (NULL!=p.icmpv4h) {
|
|
|
|
|
if (p.icmpv4h->type==8 && p.icmpv4h->code==0) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** DecodeICMPV4test02
|
|
|
|
|
* \brief
|
|
|
|
|
* \retval 0 Expected test value
|
|
|
|
|
* \retval 1 Expected test value
|
|
|
|
|
*/
|
|
|
|
|
static int DecodeICMPV4test02(void) {
|
|
|
|
|
uint8_t raw_icmpv4[] = {
|
|
|
|
|
@ -129,12 +268,20 @@ static int DecodeICMPV4test02(void) {
|
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
|
|
|
|
|
|
DecodeICMPV4(&tv, &dtv, &p, raw_icmpv4, sizeof(raw_icmpv4), NULL);
|
|
|
|
|
|
|
|
|
|
if (NULL!=p.icmpv4h) {
|
|
|
|
|
if (p.icmpv4h->type==0 && p.icmpv4h->code==0) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** DecodeICMPV4test03
|
|
|
|
|
* \brief TTL exceeded
|
|
|
|
|
* \retval Expected test value: 0
|
|
|
|
|
* \retval Expected test value: 1
|
|
|
|
|
*/
|
|
|
|
|
static int DecodeICMPV4test03(void) {
|
|
|
|
|
uint8_t raw_icmpv4[] = {
|
|
|
|
|
@ -152,12 +299,19 @@ static int DecodeICMPV4test03(void) {
|
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
|
|
|
|
|
|
DecodeICMPV4(&tv, &dtv, &p, raw_icmpv4, sizeof(raw_icmpv4), NULL);
|
|
|
|
|
|
|
|
|
|
if (NULL!=p.icmpv4h) {
|
|
|
|
|
if (p.icmpv4h->type==11 && p.icmpv4h->code==0) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** DecodeICMPV4test04
|
|
|
|
|
* \brief dest. unreachable, administratively prohibited
|
|
|
|
|
* \retval 0 Expected test value
|
|
|
|
|
* \retval 1 Expected test value
|
|
|
|
|
*/
|
|
|
|
|
static int DecodeICMPV4test04(void) {
|
|
|
|
|
uint8_t raw_icmpv4[] = {
|
|
|
|
|
@ -177,6 +331,13 @@ static int DecodeICMPV4test04(void) {
|
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
|
|
|
|
|
|
DecodeICMPV4(&tv, &dtv, &p, raw_icmpv4, sizeof(raw_icmpv4), NULL);
|
|
|
|
|
|
|
|
|
|
if (NULL!=p.icmpv4h) {
|
|
|
|
|
if (p.icmpv4h->type==3 && p.icmpv4h->code==10) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -214,19 +375,77 @@ static int ICMPV4CalculateInvalidChecksumtest06(void) {
|
|
|
|
|
return (csum == ICMPV4CalculateChecksum((uint16_t *)raw_icmpv4, sizeof(raw_icmpv4)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int ICMPV4InvalidType07(void) {
|
|
|
|
|
|
|
|
|
|
uint8_t raw_icmpv4[] = {
|
|
|
|
|
0xff, 0x00, 0xab, 0x9b, 0x7f, 0x2b, 0x05, 0x2c,
|
|
|
|
|
0x3f, 0x72, 0x93, 0x4a, 0x00, 0x4d, 0x0a, 0x00,
|
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
|
|
|
|
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
|
|
|
|
|
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
|
|
|
|
|
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x38};
|
|
|
|
|
|
|
|
|
|
Packet p;
|
|
|
|
|
ThreadVars tv;
|
|
|
|
|
DecodeThreadVars dtv;
|
|
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(ThreadVars));
|
|
|
|
|
memset(&p, 0, sizeof(Packet));
|
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
|
|
|
|
|
|
DecodeICMPV4(&tv, &dtv, &p, raw_icmpv4, sizeof(raw_icmpv4), NULL);
|
|
|
|
|
|
|
|
|
|
if(DECODER_ISSET_EVENT(&p,ICMPV4_UNKNOWN_TYPE)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** DecodeICMPV4test08
|
|
|
|
|
* \brief
|
|
|
|
|
* \retval 1 Expected test value - what we really want is not to segfault
|
|
|
|
|
*/
|
|
|
|
|
static int DecodeICMPV4test08(void) {
|
|
|
|
|
uint8_t raw_icmpv4[] = {
|
|
|
|
|
0x08, 0x00, 0x78, 0x47, 0xfc, 0x55, 0x00, 0x00
|
|
|
|
|
};
|
|
|
|
|
Packet p;
|
|
|
|
|
ThreadVars tv;
|
|
|
|
|
DecodeThreadVars dtv;
|
|
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(ThreadVars));
|
|
|
|
|
memset(&p, 0, sizeof(Packet));
|
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
|
|
|
|
|
|
DecodeICMPV4(&tv, &dtv, &p, raw_icmpv4, sizeof(raw_icmpv4), NULL);
|
|
|
|
|
|
|
|
|
|
if (NULL!=p.icmpv4h) {
|
|
|
|
|
if (p.icmpv4h->type==8 && p.icmpv4h->code==0) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief Registers ICMPV4 unit test
|
|
|
|
|
* \todo More ICMPv4 tests
|
|
|
|
|
*/
|
|
|
|
|
void DecodeICMPV4RegisterTests(void) {
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest01", DecodeICMPV4test01, 0);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest02", DecodeICMPV4test02, 0);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest03", DecodeICMPV4test03, 0);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest04", DecodeICMPV4test04, 0);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest01", DecodeICMPV4test01, 1);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest02", DecodeICMPV4test02, 1);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest03", DecodeICMPV4test03, 1);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest04", DecodeICMPV4test04, 1);
|
|
|
|
|
UtRegisterTest("ICMPV4CalculateValidChecksumtest05",
|
|
|
|
|
ICMPV4CalculateValidChecksumtest05, 1);
|
|
|
|
|
UtRegisterTest("ICMPV4CalculateInvalidChecksumtest06",
|
|
|
|
|
ICMPV4CalculateInvalidChecksumtest06, 0);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4InvalidType", ICMPV4InvalidType07, 1);
|
|
|
|
|
UtRegisterTest("DecodeICMPV4ttest08", DecodeICMPV4test08, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* UNITTESTS */
|
|
|
|
|
|