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 */
52int 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
87#include "packet.h"
88
89/**
90 * \test DecodeVNTagTest01 test if vntag header is too small.
91 *
92 */
93static int DecodeVNTagtest01(void)
94{
95 uint8_t raw_vntag[] = { 0x00, 0x20, 0x08 };
97 FAIL_IF_NULL(p);
98
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 */
116static 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
128 FAIL_IF_NULL(p);
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 */
144static 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
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 */
uint8_t len
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition counters.c:166
@ VNTAG_HEADER_TOO_SMALL
@ VNTAG_UNKNOWN_TYPE
uint8_t proto
void DecodeVNTagRegisterTests(void)
int DecodeVNTag(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
#define GET_VNTAG_DEST(vntagh)
#define GET_VNTAG_SRC(vntagh)
#define GET_VNTAG_PROTO(vntagh)
#define GET_VNTAG_LOOPED(vntagh)
#define GET_VNTAG_DIR(vntagh)
#define GET_VNTAG_PTR(vntagh)
#define GET_VNTAG_VERSION(vntagh)
#define VNTAG_HEADER_LEN
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition decode.h:1194
#define ENGINE_ISSET_EVENT(p, e)
Definition decode.h:1199
void FlowInitConfig(bool quiet)
initialize the configuration
Definition flow.c:547
void FlowShutdown(void)
shutdown the flow engine
Definition flow.c:691
#define FLOW_QUIET
Definition flow.h:43
DecodeThreadVars * dtv
ThreadVars * tv
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition decode.c:258
void PacketFree(Packet *p)
Return a malloced packet.
Definition decode.c:219
void PacketRecycle(Packet *p)
Definition packet.c:150
Structure to hold thread specific data for all decode modules.
Definition decode.h:963
uint16_t counter_vntag
Definition decode.h:1005
Per thread variable structure.
Definition threadvars.h:58
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCLogDebug(...)
Definition util-debug.h:275
#define DEBUG_VALIDATE_BUG_ON(exp)