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