suricata
decode-chdlc.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-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 Victor Julien <victor@inliniac.net>
29  *
30  * Decode Cisco HDLC
31  */
32 
33 #include "suricata-common.h"
34 #include "decode.h"
35 #include "decode-chdlc.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 (unlikely(len < CHDLC_HEADER_LEN)) {
50  return TM_ECODE_FAILED;
51  }
52 
53  if (unlikely(len > CHDLC_HEADER_LEN + USHRT_MAX)) {
54  return TM_ECODE_FAILED;
55  }
56  if (!PacketIncreaseCheckLayers(p)) {
57  return TM_ECODE_FAILED;
58  }
59 
60  CHDLCHdr *hdr = (CHDLCHdr *)pkt;
61 
62  SCLogDebug("p %p pkt %p ether type %04x", p, pkt, SCNtohs(hdr->protocol));
63 
64  DecodeNetworkLayer(tv, dtv, SCNtohs(hdr->protocol), p,
66 
67  return TM_ECODE_OK;
68 }
69 
70 #ifdef UNITTESTS
71 static int DecodeCHDLCTest01 (void)
72 {
73  uint8_t raw[] = { 0x0f,0x00,0x08,0x00, // HDLC
74  0x45,0x00,0x00,0x30,0x15,0x5a,0x40,0x00,0x80,0x06,
75  0x6c,0xd0,0xc0,0xa8,0x02,0x07,0x41,0x37,0x74,0xb7,
76  0x13,0x4a,0x00,0x50,0x9c,0x34,0x09,0x6c,0x00,0x00,
77  0x00,0x00,0x70,0x02,0x40,0x00,0x11,0x47,0x00,0x00,
78  0x02,0x04,0x05,0xb4,0x01,0x01,0x04,0x02 };
79 
81  FAIL_IF_NULL(p);
82  ThreadVars tv;
84 
85  memset(&dtv, 0, sizeof(DecodeThreadVars));
86  memset(&tv, 0, sizeof(ThreadVars));
87 
88  DecodeCHDLC(&tv, &dtv, p, raw, sizeof(raw));
89 
90  FAIL_IF_NOT(PacketIsIPv4(p));
91  FAIL_IF_NOT(PacketIsTCP(p));
92  FAIL_IF_NOT(p->dp == 80);
93 
94  PacketFree(p);
95  PASS;
96 }
97 #endif /* UNITTESTS */
98 
99 
100 /**
101  * \brief Registers Ethernet unit tests
102  * \todo More Ethernet tests
103  */
105 {
106 #ifdef UNITTESTS
107  UtRegisterTest("DecodeCHDLCTest01", DecodeCHDLCTest01);
108 #endif /* UNITTESTS */
109 }
110 /**
111  * @}
112  */
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
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
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DecodeCHDLC
int DecodeCHDLC(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-chdlc.c:41
p
Packet * p
Definition: fuzz_dataset.c:30
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
DecodeThreadVars_::counter_chdlc
StatsCounterId counter_chdlc
Definition: decode.h:1010
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
decode.h
util-debug.h
DecodeCHDLCRegisterTests
void DecodeCHDLCRegisterTests(void)
Registers Ethernet unit tests.
Definition: decode-chdlc.c:104
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
CHDLCHdr_::protocol
uint16_t protocol
Definition: decode-chdlc.h:32
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
decode-events.h
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
CHDLC_HEADER_LEN
#define CHDLC_HEADER_LEN
Definition: decode-chdlc.h:27
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:436
suricata-common.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
CHDLCHdr_
Definition: decode-chdlc.h:29
decode-chdlc.h
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:1239
Packet_::dp
Port dp
Definition: decode.h:531
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
CHDLC_PKT_TOO_SMALL
@ CHDLC_PKT_TOO_SMALL
Definition: decode-events.h:229