mirror of https://github.com/OISF/suricata
VLAN Support
parent
29d51a6182
commit
b02bb6b6b4
@ -0,0 +1,190 @@
|
||||
/* Copyright (c) 2009 Open Information Security Foundation */
|
||||
|
||||
/** \file
|
||||
* \author Breno Silva <breno.silva@gmail.com>
|
||||
*/
|
||||
|
||||
#include "suricata-common.h"
|
||||
#include "decode.h"
|
||||
#include "decode-vlan.h"
|
||||
#include "decode-events.h"
|
||||
|
||||
#include "flow.h"
|
||||
|
||||
#include "util-unittest.h"
|
||||
#include "util-debug.h"
|
||||
|
||||
/**
|
||||
* \internal
|
||||
* \brief this function is used to decode IEEE802.1q packets
|
||||
*
|
||||
* \param tv pointer to the thread vars
|
||||
* \param dtv pointer code thread vars
|
||||
* \param p pointer to the packet struct
|
||||
* \param pkt pointer to the raw packet
|
||||
* \param len packet len
|
||||
* \param pq pointer to the packet queue
|
||||
*
|
||||
*/
|
||||
void DecodeVLAN(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, uint16_t len, PacketQueue *pq)
|
||||
{
|
||||
SCPerfCounterIncr(dtv->counter_vlan, tv->sc_perf_pca);
|
||||
|
||||
if(len < VLAN_HEADER_LEN) {
|
||||
DECODER_SET_EVENT(p,VLAN_HEADER_TOO_SMALL);
|
||||
return;
|
||||
}
|
||||
|
||||
p->vlanh = (VLANHdr *)pkt;
|
||||
if(p->vlanh == NULL)
|
||||
return;
|
||||
|
||||
SCLogDebug("p %p pkt %p VLAN protocol %04x VLAN PRI %d VLAN CFI %d VLAN ID %d Len: %" PRId32 "",
|
||||
p, pkt, GET_VLAN_PROTO(p->vlanh), GET_VLAN_PRIORITY(p->vlanh), GET_VLAN_CFI(p->vlanh), GET_VLAN_ID(p->vlanh), len);
|
||||
|
||||
switch (GET_VLAN_PROTO(p->vlanh)) {
|
||||
case ETHERNET_TYPE_IP:
|
||||
DecodeIPV4(tv, dtv, p, pkt + VLAN_HEADER_LEN,
|
||||
len - VLAN_HEADER_LEN, pq);
|
||||
break;
|
||||
case ETHERNET_TYPE_IPV6:
|
||||
DecodeIPV6(tv, dtv, p, pkt + VLAN_HEADER_LEN,
|
||||
len - VLAN_HEADER_LEN, pq);
|
||||
break;
|
||||
case ETHERNET_TYPE_PPPOE_SESS:
|
||||
DecodePPPOESession(tv, dtv, p, pkt + VLAN_HEADER_LEN,
|
||||
len - VLAN_HEADER_LEN, pq);
|
||||
break;
|
||||
case ETHERNET_TYPE_PPPOE_DISC:
|
||||
DecodePPPOEDiscovery(tv, dtv, p, pkt + VLAN_HEADER_LEN,
|
||||
len - VLAN_HEADER_LEN, pq);
|
||||
break;
|
||||
case ETHERNET_TYPE_VLAN:
|
||||
DecodeVLAN(tv, dtv, p, pkt + VLAN_HEADER_LEN,
|
||||
len - VLAN_HEADER_LEN, pq);
|
||||
break;
|
||||
default:
|
||||
SCLogDebug("unknown VLAN type: %" PRIx32 "",GET_VLAN_PROTO(p->vlanh));
|
||||
DECODER_SET_EVENT(p,VLAN_UNKNOWN_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef UNITTESTS
|
||||
/** \todo Must GRE+VLAN and Multi-Vlan packets to
|
||||
* create more tests
|
||||
*/
|
||||
|
||||
/**
|
||||
* \test DecodeVLANTest01 test if vlan header is too small.
|
||||
*
|
||||
* \retval 1 on success
|
||||
* \retval 0 on failure
|
||||
*/
|
||||
static int DecodeVLANtest01 (void) {
|
||||
uint8_t raw_vlan[] = { 0x00, 0x20, 0x08 };
|
||||
Packet p;
|
||||
ThreadVars tv;
|
||||
DecodeThreadVars dtv;
|
||||
|
||||
memset(&tv, 0, sizeof(ThreadVars));
|
||||
memset(&p, 0, sizeof(Packet));
|
||||
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
||||
|
||||
DecodeVLAN(&tv, &dtv, &p, raw_vlan, sizeof(raw_vlan), NULL);
|
||||
|
||||
if(DECODER_ISSET_EVENT(&p,VLAN_HEADER_TOO_SMALL)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DecodeVLANTest02 test if vlan header has unknown type.
|
||||
*
|
||||
* \retval 1 on success
|
||||
* \retval 0 on failure
|
||||
*/
|
||||
static int DecodeVLANtest02 (void) {
|
||||
uint8_t raw_vlan[] = {
|
||||
0x00, 0x20, 0x01, 0x00, 0x45, 0x00, 0x00, 0x34,
|
||||
0x3b, 0x36, 0x40, 0x00, 0x40, 0x06, 0xb7, 0xc9,
|
||||
0x83, 0x97, 0x20, 0x81, 0x83, 0x97, 0x20, 0x15,
|
||||
0x04, 0x8a, 0x17, 0x70, 0x4e, 0x14, 0xdf, 0x55,
|
||||
0x4d, 0x3d, 0x5a, 0x61, 0x80, 0x10, 0x6b, 0x50,
|
||||
0x3c, 0x4c, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a,
|
||||
0x00, 0x04, 0xf0, 0xc8, 0x01, 0x99, 0xa3, 0xf3};
|
||||
Packet p;
|
||||
ThreadVars tv;
|
||||
DecodeThreadVars dtv;
|
||||
|
||||
memset(&tv, 0, sizeof(ThreadVars));
|
||||
memset(&p, 0, sizeof(Packet));
|
||||
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
||||
|
||||
DecodeVLAN(&tv, &dtv, &p, raw_vlan, sizeof(raw_vlan), NULL);
|
||||
|
||||
|
||||
if(DECODER_ISSET_EVENT(&p,VLAN_UNKNOWN_TYPE)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DecodeVLANTest02 test a good vlan header.
|
||||
*
|
||||
* \retval 1 on success
|
||||
* \retval 0 on failure
|
||||
*/
|
||||
static int DecodeVLANtest03 (void) {
|
||||
uint8_t raw_vlan[] = {
|
||||
0x00, 0x20, 0x08, 0x00, 0x45, 0x00, 0x00, 0x34,
|
||||
0x3b, 0x36, 0x40, 0x00, 0x40, 0x06, 0xb7, 0xc9,
|
||||
0x83, 0x97, 0x20, 0x81, 0x83, 0x97, 0x20, 0x15,
|
||||
0x04, 0x8a, 0x17, 0x70, 0x4e, 0x14, 0xdf, 0x55,
|
||||
0x4d, 0x3d, 0x5a, 0x61, 0x80, 0x10, 0x6b, 0x50,
|
||||
0x3c, 0x4c, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a,
|
||||
0x00, 0x04, 0xf0, 0xc8, 0x01, 0x99, 0xa3, 0xf3};
|
||||
Packet p;
|
||||
ThreadVars tv;
|
||||
DecodeThreadVars dtv;
|
||||
|
||||
memset(&tv, 0, sizeof(ThreadVars));
|
||||
memset(&p, 0, sizeof(Packet));
|
||||
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
||||
|
||||
FlowInitConfig(FLOW_QUIET);
|
||||
|
||||
DecodeVLAN(&tv, &dtv, &p, raw_vlan, sizeof(raw_vlan), NULL);
|
||||
|
||||
FlowShutdown();
|
||||
|
||||
if(p.vlanh == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(DECODER_ISSET_EVENT(&p,VLAN_HEADER_TOO_SMALL)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(DECODER_ISSET_EVENT(&p,VLAN_UNKNOWN_TYPE)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif /* UNITTESTS */
|
||||
|
||||
void DecodeVLANRegisterTests(void) {
|
||||
#ifdef UNITTESTS
|
||||
UtRegisterTest("DecodeVLANtest01", DecodeVLANtest01, 1);
|
||||
UtRegisterTest("DecodeVLANtest02", DecodeVLANtest02, 1);
|
||||
UtRegisterTest("DecodeVLANtest03", DecodeVLANtest03, 1);
|
||||
#endif /* UNITTESTS */
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
/* Copyright (c) 2009 Open Information Security Foundation */
|
||||
|
||||
/** \file
|
||||
* \author Breno Silva <breno.silva@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __DECODE_VLAN_H__
|
||||
#define __DECODE_VLAN_H__
|
||||
|
||||
/** Vlan type */
|
||||
#define ETHERNET_TYPE_VLAN 0x8100
|
||||
|
||||
/** Vlan macros to access Vlan priority, Vlan CFI and VID */
|
||||
#define GET_VLAN_PRIORITY(vlanh) ((ntohs((vlanh)->vlan_cfi) & 0xe000) >> 13)
|
||||
#define GET_VLAN_CFI(vlanh) ((ntohs((vlanh)->vlan_cfi) & 0x0100) >> 12)
|
||||
#define GET_VLAN_ID(vlanh) ((unsigned short)(ntohs((vlanh)->vlan_cfi) & 0x0FFF))
|
||||
#define GET_VLAN_PROTO(vlanh) ((ntohs((vlanh)->protocol)))
|
||||
|
||||
/** Vlan header struct */
|
||||
typedef struct _VLANHdr {
|
||||
uint16_t vlan_cfi;
|
||||
uint16_t protocol; /** protocol field */
|
||||
}VLANHdr;
|
||||
|
||||
/** VLAN header length */
|
||||
#define VLAN_HEADER_LEN 4
|
||||
|
||||
void DecodeVLANRegisterTests(void);
|
||||
|
||||
#endif /* __DECODE_VLAN_H__ */
|
Loading…
Reference in New Issue