suricata
decode-null.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-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 linkype null:
31  * http://www.tcpdump.org/linktypes.html
32  */
33 
34 #include "suricata-common.h"
35 #include "decode.h"
36 #include "decode-raw.h"
37 #include "decode-events.h"
38 
39 #include "util-validate.h"
40 #include "util-unittest.h"
41 #include "util-debug.h"
42 
43 #define HDR_SIZE 4
44 
45 #define AF_INET6_BSD 24
46 #define AF_INET6_FREEBSD 28
47 #define AF_INET6_DARWIN 30
48 #define AF_INET6_LINUX 10
49 #define AF_INET6_SOLARIS 26
50 #define AF_INET6_WINSOCK 23
51 
53  const uint8_t *pkt, uint32_t len)
54 {
55  DEBUG_VALIDATE_BUG_ON(pkt == NULL);
56 
58 
59  if (unlikely(len < HDR_SIZE)) {
61  return TM_ECODE_FAILED;
62  }
63 
64  if (unlikely(GET_PKT_LEN(p) > HDR_SIZE + USHRT_MAX)) {
65  return TM_ECODE_FAILED;
66  }
67 #if __BYTE_ORDER__ == __BIG_ENDIAN
68  uint32_t type = pkt[0] | pkt[1] << 8 | pkt[2] << 16 | pkt[3] << 24;
69 #else
70  uint32_t type = *((uint32_t *)pkt);
71 #endif
72  switch(type) {
73  case AF_INET:
74  SCLogDebug("IPV4 Packet");
75  if (GET_PKT_LEN(p) - HDR_SIZE > USHRT_MAX) {
76  return TM_ECODE_FAILED;
77  }
78  DecodeIPV4(
79  tv, dtv, p, GET_PKT_DATA(p) + HDR_SIZE, (uint16_t)(GET_PKT_LEN(p) - HDR_SIZE));
80  break;
81  case AF_INET6_BSD:
82  case AF_INET6_FREEBSD:
83  case AF_INET6_DARWIN:
84  case AF_INET6_LINUX:
85  case AF_INET6_SOLARIS:
86  case AF_INET6_WINSOCK:
87  SCLogDebug("IPV6 Packet");
88  if (GET_PKT_LEN(p) - HDR_SIZE > USHRT_MAX) {
89  return TM_ECODE_FAILED;
90  }
91  DecodeIPV6(
92  tv, dtv, p, GET_PKT_DATA(p) + HDR_SIZE, (uint16_t)(GET_PKT_LEN(p) - HDR_SIZE));
93  break;
94  default:
95  SCLogDebug("Unknown Null packet type version %" PRIu32 "", type);
97  break;
98  }
99  return TM_ECODE_OK;
100 }
101 
102 /**
103  * @}
104  */
ENGINE_SET_EVENT
#define ENGINE_SET_EVENT(p, e)
Definition: decode.h:887
decode-raw.h
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:167
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
HDR_SIZE
#define HDR_SIZE
Definition: decode-null.c:43
AF_INET6_BSD
#define AF_INET6_BSD
Definition: decode-null.c:45
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
DecodeNull
int DecodeNull(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-null.c:52
AF_INET6_WINSOCK
#define AF_INET6_WINSOCK
Definition: decode-null.c:50
decode.h
util-debug.h
type
uint8_t type
Definition: decode-icmpv4.h:0
AF_INET6_SOLARIS
#define AF_INET6_SOLARIS
Definition: decode-null.c:49
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:219
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
Packet_
Definition: decode.h:428
DecodeIPV6
int DecodeIPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv6.c:564
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:218
AF_INET6_LINUX
#define AF_INET6_LINUX
Definition: decode-null.c:48
decode-events.h
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
suricata-common.h
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
LTNULL_UNSUPPORTED_TYPE
@ LTNULL_UNSUPPORTED_TYPE
Definition: decode-events.h:160
util-validate.h
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:664
LTNULL_PKT_TOO_SMALL
@ LTNULL_PKT_TOO_SMALL
Definition: decode-events.h:159
AF_INET6_FREEBSD
#define AF_INET6_FREEBSD
Definition: decode-null.c:46
DecodeThreadVars_::counter_null
uint16_t counter_null
Definition: decode.h:691
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:895
DecodeIPV4
int DecodeIPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv4.c:520
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
AF_INET6_DARWIN
#define AF_INET6_DARWIN
Definition: decode-null.c:47