suricata
decode-chdlc.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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-unittest.h"
39 #include "util-debug.h"
40 
42  const uint8_t *pkt, uint32_t len)
43 {
45 
46  if (unlikely(len < CHDLC_HEADER_LEN)) {
48  return TM_ECODE_FAILED;
49  }
50 
51  if (unlikely(len > CHDLC_HEADER_LEN + USHRT_MAX)) {
52  return TM_ECODE_FAILED;
53  }
54 
55  CHDLCHdr *hdr = (CHDLCHdr *)pkt;
56  if (unlikely(hdr == NULL))
57  return TM_ECODE_FAILED;
58 
59  SCLogDebug("p %p pkt %p ether type %04x", p, pkt, SCNtohs(hdr->protocol));
60 
61  DecodeNetworkLayer(tv, dtv, SCNtohs(hdr->protocol), p,
63 
64  return TM_ECODE_OK;
65 }
66 
67 #ifdef UNITTESTS
68 static int DecodeCHDLCTest01 (void)
69 {
70  uint8_t raw[] = { 0x0f,0x00,0x08,0x00, // HDLC
71  0x45,0x00,0x00,0x30,0x15,0x5a,0x40,0x00,0x80,0x06,
72  0x6c,0xd0,0xc0,0xa8,0x02,0x07,0x41,0x37,0x74,0xb7,
73  0x13,0x4a,0x00,0x50,0x9c,0x34,0x09,0x6c,0x00,0x00,
74  0x00,0x00,0x70,0x02,0x40,0x00,0x11,0x47,0x00,0x00,
75  0x02,0x04,0x05,0xb4,0x01,0x01,0x04,0x02 };
76 
78  if (unlikely(p == NULL))
79  return 0;
80  ThreadVars tv;
82 
83  memset(&dtv, 0, sizeof(DecodeThreadVars));
84  memset(&tv, 0, sizeof(ThreadVars));
85  memset(p, 0, SIZE_OF_PACKET);
86 
87  DecodeCHDLC(&tv, &dtv, p, raw, sizeof(raw));
88 
91  FAIL_IF_NOT(p->dp == 80);
92 
93  SCFree(p);
94  PASS;
95 }
96 #endif /* UNITTESTS */
97 
98 
99 /**
100  * \brief Registers Ethernet unit tests
101  * \todo More Ethernet tests
102  */
104 {
105 #ifdef UNITTESTS
106  UtRegisterTest("DecodeCHDLCTest01", DecodeCHDLCTest01);
107 #endif /* UNITTESTS */
108 }
109 /**
110  * @}
111  */
len
uint8_t len
Definition: app-layer-dnp3.h:2
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:169
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:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DecodeCHDLC
int DecodeCHDLC(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-chdlc.c:41
CHDLC_PKT_TOO_SMALL
@ CHDLC_PKT_TOO_SMALL
Definition: decode-events.h:201
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:259
decode.h
util-debug.h
DecodeCHDLCRegisterTests
void DecodeCHDLCRegisterTests(void)
Registers Ethernet unit tests.
Definition: decode-chdlc.c:103
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:58
SIZE_OF_PACKET
#define SIZE_OF_PACKET
Definition: decode.h:627
Packet_
Definition: decode.h:414
decode-events.h
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:30
DecodeThreadVars_::counter_chdlc
uint16_t counter_chdlc
Definition: decode.h:646
CHDLC_HEADER_LEN
#define CHDLC_HEADER_LEN
Definition: decode-chdlc.h:27
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:398
suricata-common.h
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:631
CHDLCHdr_
Definition: decode-chdlc.h:29
decode-chdlc.h
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:1011
Packet_::dp
Port dp
Definition: decode.h:429
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:257