suricata
decode-vntag.c
Go to the documentation of this file.
1 /* Copyright (C) 2021 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  * \ingroup decode
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Jeff Lucovsky <jeff@lucovsky.org>
28  *
29  * Decode VNTag 802.1Qbh
30  */
31 
32 #include "suricata-common.h"
33 #include "decode-vntag.h"
34 #include "decode.h"
35 #include "decode-events.h"
36 
37 #include "util-validate.h"
38 #include "util-debug.h"
39 
40 /**
41  * \internal
42  * \brief this function is used to decode 802.1Qbh packets
43  *
44  * \param tv pointer to the thread vars
45  * \param dtv pointer code thread vars
46  * \param p pointer to the packet struct
47  * \param pkt pointer to the raw packet
48  * \param len packet len
49  *
50  */
51 int DecodeVNTag(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
52 {
53  DEBUG_VALIDATE_BUG_ON(pkt == NULL);
54 
56 
57  if (len < VNTAG_HEADER_LEN) {
59  return TM_ECODE_FAILED;
60  }
61 
62  if (!PacketIncreaseCheckLayers(p)) {
63  return TM_ECODE_FAILED;
64  }
65 
66  VNTagHdr *vntag_hdr = (VNTagHdr *)pkt;
67 
68  uint16_t proto = GET_VNTAG_PROTO(vntag_hdr);
69 
70  SCLogDebug("p %p pkt %p protocol %04x DIR %d PTR %d DEST %d LOOPED: %d VERSION: %d SRC: %d "
71  "Len: %" PRIu32 "",
72  p, pkt, proto, GET_VNTAG_DIR(vntag_hdr), GET_VNTAG_PTR(vntag_hdr),
73  GET_VNTAG_DEST(vntag_hdr), GET_VNTAG_LOOPED(vntag_hdr), GET_VNTAG_VERSION(vntag_hdr),
74  GET_VNTAG_SRC(vntag_hdr), len);
75 
76  if (DecodeNetworkLayer(tv, dtv, proto, p, pkt + VNTAG_HEADER_LEN, len - VNTAG_HEADER_LEN) ==
77  false) {
79  return TM_ECODE_FAILED;
80  }
81  return TM_ECODE_OK;
82 }
83 
84 #ifdef UNITTESTS
85 #include "util-unittest-helper.h"
86 #include "packet.h"
87 
88 /**
89  * \test DecodeVNTagTest01 test if vntag header is too small.
90  *
91  */
92 static int DecodeVNTagtest01(void)
93 {
94  uint8_t raw_vntag[] = { 0x00, 0x20, 0x08 };
96  FAIL_IF_NULL(p);
97 
98  ThreadVars tv;
100 
101  memset(&tv, 0, sizeof(ThreadVars));
102  memset(&dtv, 0, sizeof(DecodeThreadVars));
103 
104  FAIL_IF(TM_ECODE_OK == DecodeVNTag(&tv, &dtv, p, raw_vntag, sizeof(raw_vntag)));
105 
107  PacketFree(p);
108  PASS;
109 }
110 
111 /**
112  * \test DecodeVNTagt02 test if vntag header has unknown type.
113  *
114  */
115 static int DecodeVNTagtest02(void)
116 {
117  uint8_t raw_vntag[] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x0b, 0x08, 0x00, 0x45, 0x00,
118  0x00, 0x64, 0xac, 0xe6, 0x00, 0x00, 0xff, 0xfd, 0x08, 0xb3, 0x01, 0x01, 0x01, 0x01, 0x01,
119  0x01, 0x02, 0x01, 0xe5, 0xa3, 0x95, 0x5c, 0x5d, 0x82, 0x50, 0x24, 0x6f, 0x56, 0xac, 0xf4,
120  0xf9, 0x9b, 0x28, 0x6a, 0x03, 0xb5, 0xab, 0x15, 0xfe, 0x6c, 0xab, 0x98, 0x0c, 0x4e, 0xcc,
121  0xf4, 0xd1, 0x5b, 0x22, 0x0b, 0x81, 0x39, 0x08, 0xb3, 0xcf, 0xc2, 0x6b, 0x90, 0xe1, 0xcc,
122  0xe6, 0x4f, 0x5f, 0xa0, 0xb6, 0xa8, 0x93, 0x38, 0x8a, 0x17, 0xac, 0x6e, 0x3b, 0xbc, 0xad,
123  0x67, 0xad, 0xfc, 0x91, 0xf0, 0x16, 0x9d, 0xe2, 0xe1, 0xdf, 0x4f, 0x8c, 0xcb, 0xd3, 0xdc,
124  0xd9, 0xed, 0x3c, 0x0c, 0x92, 0xad, 0x8b, 0xf0, 0x2c, 0x2d, 0x55, 0x41 };
125 
127  FAIL_IF_NULL(p);
128  ThreadVars tv;
130 
131  memset(&tv, 0, sizeof(ThreadVars));
132  memset(&dtv, 0, sizeof(DecodeThreadVars));
133 
134  FAIL_IF_NOT(TM_ECODE_OK != DecodeVNTag(&tv, &dtv, p, raw_vntag, sizeof(raw_vntag)));
135  PacketFree(p);
136  PASS;
137 }
138 
139 /**
140  * \test DecodeVNTagTest03 test a good vntag header.
141  *
142  */
143 static int DecodeVNTagtest03(void)
144 {
145  uint8_t raw_vntag[] = { 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x0b, 0x08, 0x00, 0x45, 0x00,
146  0x00, 0x64, 0xac, 0xe6, 0x00, 0x00, 0xff, 0xfd, 0x08, 0xb3, 0x01, 0x01, 0x01, 0x01, 0x01,
147  0x01, 0x02, 0x01, 0xe5, 0xa3, 0x95, 0x5c, 0x5d, 0x82, 0x50, 0x24, 0x6f, 0x56, 0xac, 0xf4,
148  0xf9, 0x9b, 0x28, 0x6a, 0x03, 0xb5, 0xab, 0x15, 0xfe, 0x6c, 0xab, 0x98, 0x0c, 0x4e, 0xcc,
149  0xf4, 0xd1, 0x5b, 0x22, 0x0b, 0x81, 0x39, 0x08, 0xb3, 0xcf, 0xc2, 0x6b, 0x90, 0xe1, 0xcc,
150  0xe6, 0x4f, 0x5f, 0xa0, 0xb6, 0xa8, 0x93, 0x38, 0x8a, 0x17, 0xac, 0x6e, 0x3b, 0xbc, 0xad,
151  0x67, 0xad, 0xfc, 0x91, 0xf0, 0x16, 0x9d, 0xe2, 0xe1, 0xdf, 0x4f, 0x8c, 0xcb, 0xd3, 0xdc,
152  0xd9, 0xed, 0x3c, 0x0c, 0x92, 0xad, 0x8b, 0xf0, 0x2c, 0x2d, 0x55, 0x41 };
153 
155  FAIL_IF_NULL(p);
156 
157  ThreadVars tv = { 0 };
158  DecodeThreadVars dtv = { 0 };
159 
161 
162  FAIL_IF(TM_ECODE_OK != DecodeVNTag(&tv, &dtv, p, raw_vntag, sizeof(raw_vntag)));
163 
164  PacketRecycle(p);
165  FlowShutdown();
166  PacketFree(p);
167  PASS;
168 }
169 #endif /* UNITTESTS */
170 
172 {
173 #ifdef UNITTESTS
174  UtRegisterTest("DecodeVNTagtest01", DecodeVNTagtest01);
175  UtRegisterTest("DecodeVNTagtest02", DecodeVNTagtest02);
176  UtRegisterTest("DecodeVNTagtest03", DecodeVNTagtest03);
177 #endif /* UNITTESTS */
178 }
179 
180 /**
181  * @}
182  */
len
uint8_t len
Definition: app-layer-dnp3.h:2
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
GET_VNTAG_VERSION
#define GET_VNTAG_VERSION(vntagh)
Definition: decode-vntag.h:33
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
ENGINE_ISSET_EVENT
#define ENGINE_ISSET_EVENT(p, e)
Definition: decode.h:1244
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
VNTAG_HEADER_TOO_SMALL
@ VNTAG_HEADER_TOO_SMALL
Definition: decode-events.h:160
PacketRecycle
void PacketRecycle(Packet *p)
Definition: packet.c:166
VNTAG_UNKNOWN_TYPE
@ VNTAG_UNKNOWN_TYPE
Definition: decode-events.h:161
p
Packet * p
Definition: fuzz_dataset.c:30
proto
uint8_t proto
Definition: decode-template.h:0
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
GET_VNTAG_SRC
#define GET_VNTAG_SRC(vntagh)
Definition: decode-vntag.h:34
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:574
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
GET_VNTAG_PROTO
#define GET_VNTAG_PROTO(vntagh)
Definition: decode-vntag.h:35
DecodeVNTag
int DecodeVNTag(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-vntag.c:51
DecodeVNTagRegisterTests
void DecodeVNTagRegisterTests(void)
Definition: decode-vntag.c:171
DecodeThreadVars_::counter_vntag
StatsCounterId counter_vntag
Definition: decode.h:1043
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
StatsCounterIncr
void StatsCounterIncr(StatsThreadContext *stats, StatsCounterId id)
Increments the local counter.
Definition: counters.c:163
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:221
Packet_
Definition: decode.h:516
GET_VNTAG_DEST
#define GET_VNTAG_DEST(vntagh)
Definition: decode-vntag.h:31
GET_VNTAG_LOOPED
#define GET_VNTAG_LOOPED(vntagh)
Definition: decode-vntag.h:32
decode-events.h
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:718
packet.h
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
util-validate.h
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:260
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:995
GET_VNTAG_DIR
#define GET_VNTAG_DIR(vntagh)
Definition: decode-vntag.h:29
VNTAG_HEADER_LEN
#define VNTAG_HEADER_LEN
Definition: decode-vntag.h:44
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:1239
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:43
decode-vntag.h
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
GET_VNTAG_PTR
#define GET_VNTAG_PTR(vntagh)
Definition: decode-vntag.h:30