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-unittest.h"
40#include "util-debug.h"
41
43 const uint8_t *pkt, uint32_t len)
44{
45 DEBUG_VALIDATE_BUG_ON(pkt == NULL);
46
48
51 return TM_ECODE_FAILED;
52 }
53
54 if (unlikely(len > CHDLC_HEADER_LEN + USHRT_MAX)) {
55 return TM_ECODE_FAILED;
56 }
57 if (!PacketIncreaseCheckLayers(p)) {
58 return TM_ECODE_FAILED;
59 }
60
61 CHDLCHdr *hdr = (CHDLCHdr *)pkt;
62
63 SCLogDebug("p %p pkt %p ether type %04x", p, pkt, SCNtohs(hdr->protocol));
64
65 DecodeNetworkLayer(tv, dtv, SCNtohs(hdr->protocol), p,
67
68 return TM_ECODE_OK;
69}
70
71#ifdef UNITTESTS
72static int DecodeCHDLCTest01 (void)
73{
74 uint8_t raw[] = { 0x0f,0x00,0x08,0x00, // HDLC
75 0x45,0x00,0x00,0x30,0x15,0x5a,0x40,0x00,0x80,0x06,
76 0x6c,0xd0,0xc0,0xa8,0x02,0x07,0x41,0x37,0x74,0xb7,
77 0x13,0x4a,0x00,0x50,0x9c,0x34,0x09,0x6c,0x00,0x00,
78 0x00,0x00,0x70,0x02,0x40,0x00,0x11,0x47,0x00,0x00,
79 0x02,0x04,0x05,0xb4,0x01,0x01,0x04,0x02 };
80
82 FAIL_IF_NULL(p);
85
86 memset(&dtv, 0, sizeof(DecodeThreadVars));
87 memset(&tv, 0, sizeof(ThreadVars));
88
89 DecodeCHDLC(&tv, &dtv, p, raw, sizeof(raw));
90
91 FAIL_IF_NOT(PacketIsIPv4(p));
92 FAIL_IF_NOT(PacketIsTCP(p));
93 FAIL_IF_NOT(p->dp == 80);
94
95 SCFree(p);
96 PASS;
97}
98#endif /* UNITTESTS */
99
100
101/**
102 * \brief Registers Ethernet unit tests
103 * \todo More Ethernet tests
104 */
106{
107#ifdef UNITTESTS
108 UtRegisterTest("DecodeCHDLCTest01", DecodeCHDLCTest01);
109#endif /* UNITTESTS */
110}
111/**
112 * @}
113 */
uint8_t len
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition counters.c:166
void DecodeCHDLCRegisterTests(void)
Registers Ethernet unit tests.
int DecodeCHDLC(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
#define CHDLC_HEADER_LEN
@ CHDLC_PKT_TOO_SMALL
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition decode.h:1194
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.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition decode.c:258
uint16_t protocol
Structure to hold thread specific data for all decode modules.
Definition decode.h:963
uint16_t counter_chdlc
Definition decode.h:978
Port dp
Definition decode.h:516
Per thread variable structure.
Definition threadvars.h:58
#define SCNtohs(x)
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCFree(p)
Definition util-mem.h:61
#define unlikely(expr)
#define DEBUG_VALIDATE_BUG_ON(exp)