suricata
decode-raw.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 /**
26  * \file
27  *
28  * \author William Metcalf <william.metcalf@gmail.com>
29  *
30  * Decode RAW
31  */
32 
33 #include "suricata-common.h"
34 #include "decode-raw.h"
35 #include "decode.h"
36 #include "decode-events.h"
37 
38 #include "util-validate.h"
39 #include "util-debug.h"
40 
42  const uint8_t *pkt, uint32_t len)
43 {
44  DEBUG_VALIDATE_BUG_ON(pkt == NULL);
45 
47 
48  /* If it is ipv4 or ipv6 it should at least be the size of ipv4 */
49  if (unlikely(len < IPV4_HEADER_LEN)) {
51  return TM_ECODE_FAILED;
52  }
53 
54 
55 
56  if (IP_GET_RAW_VER(pkt) == 4) {
57  if (unlikely(GET_PKT_LEN(p) > USHRT_MAX)) {
58  return TM_ECODE_FAILED;
59  }
60  SCLogDebug("IPV4 Packet");
61  DecodeIPV4(tv, dtv, p, GET_PKT_DATA(p), (uint16_t)(GET_PKT_LEN(p)));
62  } else if (IP_GET_RAW_VER(pkt) == 6) {
63  SCLogDebug("IPV6 Packet");
65  } else {
66  SCLogDebug("Unknown ip version %d", IP_GET_RAW_VER(pkt));
68  }
69  return TM_ECODE_OK;
70 }
71 
72 #ifdef UNITTESTS
73 #include "util-unittest-helper.h"
74 #include "packet.h"
75 
76 /** DecodeRawtest01
77  * \brief Valid Raw packet
78  * \retval 0 Expected test value
79  */
80 static int DecodeRawTest01 (void)
81 {
82  /* IPV6/TCP/no eth header */
83  uint8_t raw_ip[] = {
84  0x60, 0x00, 0x00, 0x00, 0x00, 0x28, 0x06, 0x40,
85  0x20, 0x01, 0x06, 0x18, 0x04, 0x00, 0x00, 0x00,
86  0x00, 0x00, 0x00, 0x00, 0x51, 0x99, 0xcc, 0x70,
87  0x20, 0x01, 0x06, 0x18, 0x00, 0x01, 0x80, 0x00,
88  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
89  0x8c, 0x9b, 0x00, 0x50, 0x6a, 0xe7, 0x07, 0x36,
90  0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0x30,
91  0x29, 0x9c, 0x00, 0x00, 0x02, 0x04, 0x05, 0x8c,
92  0x04, 0x02, 0x08, 0x0a, 0x00, 0xdd, 0x1a, 0x39,
93  0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x02 };
95  FAIL_IF_NULL(p);
96  ThreadVars tv;
98 
99  memset(&dtv, 0, sizeof(DecodeThreadVars));
100  memset(&tv, 0, sizeof(ThreadVars));
101 
102  FAIL_IF(PacketCopyData(p, raw_ip, sizeof(raw_ip)) == -1);
103 
105 
106  DecodeRaw(&tv, &dtv, p, raw_ip, GET_PKT_LEN(p));
107  FAIL_IF_NOT(PacketIsIPv6(p));
108 
109  PacketFree(p);
110  FlowShutdown();
111  PASS;
112 }
113 
114 /** DecodeRawtest02
115  * \brief Valid Raw packet
116  * \retval 0 Expected test value
117  */
118 static int DecodeRawTest02 (void)
119 {
120  /* IPV4/TCP/no eth header */
121  uint8_t raw_ip[] = {
122  0x45, 0x00, 0x00, 0x30, 0x00, 0xad, 0x40, 0x00,
123  0x7f, 0x06, 0xac, 0xc5, 0xc0, 0xa8, 0x67, 0x02,
124  0xc0, 0xa8, 0x66, 0x02, 0x0b, 0xc7, 0x00, 0x50,
125  0x1d, 0xb3, 0x12, 0x37, 0x00, 0x00, 0x00, 0x00,
126  0x70, 0x02, 0x40, 0x00, 0xb8, 0xc8, 0x00, 0x00,
127  0x02, 0x04, 0x05, 0xb4, 0x01, 0x01, 0x04, 0x02 };
128 
130  FAIL_IF_NULL(p);
131  ThreadVars tv;
133 
134  memset(&dtv, 0, sizeof(DecodeThreadVars));
135  memset(&tv, 0, sizeof(ThreadVars));
136 
137  FAIL_IF(PacketCopyData(p, raw_ip, sizeof(raw_ip)) == -1);
138 
140 
141  DecodeRaw(&tv, &dtv, p, raw_ip, GET_PKT_LEN(p));
142  FAIL_IF_NOT(PacketIsIPv4(p));
143 
144  PacketFree(p);
145  FlowShutdown();
146  PASS;
147 }
148 
149 /** DecodeRawtest03
150  * \brief Valid Raw packet
151  * \retval 0 Expected test value
152  */
153 static int DecodeRawTest03 (void)
154 {
155  /* IPV13 */
156  uint8_t raw_ip[] = { 0xdf, 0x00, 0x00, 0x3d, 0x49, 0x42, 0x40, 0x00, 0x40, 0x06, 0xcf, 0x8a,
157  0x0a, 0x1f, 0x03, 0xaf, 0x0a, 0x1f, 0x0a, 0x02, 0xa5, 0xe7, 0xde, 0xad, 0x00, 0x0c, 0xe2,
158  0x0e, 0x8b, 0xfe, 0x0c, 0xe7, 0x80, 0x18, 0x00, 0xb7, 0xaf, 0xeb, 0x00, 0x00, 0x01, 0x01,
159  0x08, 0x0a, 0x00, 0x08, 0xab, 0x4f, 0x34, 0x40, 0x67, 0x31, 0x3b, 0x63, 0x61, 0x74, 0x20,
160  0x6b, 0x65, 0x79, 0x3b };
162  FAIL_IF_NULL(p);
163  ThreadVars tv;
165 
166  memset(&dtv, 0, sizeof(DecodeThreadVars));
167  memset(&tv, 0, sizeof(ThreadVars));
168 
169  FAIL_IF(PacketCopyData(p, raw_ip, sizeof(raw_ip)) == -1);
170 
172 
173  DecodeRaw(&tv, &dtv, p, raw_ip, GET_PKT_LEN(p));
175  PacketFree(p);
176  FlowShutdown();
177  PASS;
178 }
179 
180 #endif /* UNITTESTS */
181 
182 /**
183  * \brief Registers Raw unit tests
184  * \todo More Raw tests
185  */
187 {
188 #ifdef UNITTESTS
189  UtRegisterTest("DecodeRawTest01", DecodeRawTest01);
190  UtRegisterTest("DecodeRawTest02", DecodeRawTest02);
191  UtRegisterTest("DecodeRawTest03", DecodeRawTest03);
192 #endif /* UNITTESTS */
193 }
194 /**
195  * @}
196  */
ENGINE_SET_EVENT
#define ENGINE_SET_EVENT(p, e)
Definition: decode.h:1231
decode-raw.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
IPV4_PKT_TOO_SMALL
@ IPV4_PKT_TOO_SMALL
Definition: decode-events.h:34
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
DecodeThreadVars_::counter_raw
StatsCounterId counter_raw
Definition: decode.h:1027
PacketCopyData
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
Definition: decode.c:386
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
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
p
Packet * p
Definition: fuzz_dataset.c:30
IP_GET_RAW_VER
#define IP_GET_RAW_VER(pkt)
Definition: decode.h:233
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
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_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:210
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
IPRAW_INVALID_IPV
@ IPRAW_INVALID_IPV
Definition: decode-events.h:168
Packet_
Definition: decode.h:516
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:209
DecodeIPV6
int DecodeIPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-ipv6.c:551
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
IPV4_HEADER_LEN
#define IPV4_HEADER_LEN
Definition: decode-ipv4.h:28
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
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:1239
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:43
DecodeRaw
int DecodeRaw(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-raw.c:41
DecodeIPV4
int DecodeIPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv4.c:515
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
DecodeRawRegisterTests
void DecodeRawRegisterTests(void)
Registers Raw unit tests.
Definition: decode-raw.c:186