suricata
decode-vxlan.c
Go to the documentation of this file.
1/* Copyright (C) 2019-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 * \file
20 *
21 * \author Henrik Kramshoej <hlk@kramse.org>
22 *
23 * VXLAN tunneling scheme decoder.
24 *
25 * This implementation is based on the following specification doc:
26 * https://tools.ietf.org/html/draft-mahalingam-dutt-dcops-vxlan-00
27 */
28
29#include "suricata-common.h"
30#include "decode.h"
31#include "decode-vxlan.h"
32#include "decode-events.h"
33
34#include "detect.h"
35#include "detect-engine-port.h"
36
37#include "flow.h"
38
39#include "util-validate.h"
40#include "util-unittest.h"
41#include "util-debug.h"
42
43#define VXLAN_HEADER_LEN sizeof(VXLANHeader)
44
45#define VXLAN_MAX_PORTS 4
46#define VXLAN_UNSET_PORT -1
47#define VXLAN_DEFAULT_PORT 4789
48#define VXLAN_DEFAULT_PORT_S "4789"
49
50static bool g_vxlan_enabled = true;
51static int g_vxlan_ports_idx = 0;
52static int g_vxlan_ports[VXLAN_MAX_PORTS] = { VXLAN_DEFAULT_PORT, VXLAN_UNSET_PORT,
54
55typedef struct VXLANHeader_ {
56 uint8_t flags[2];
57 uint16_t gdp;
58 uint8_t vni[3];
59 uint8_t res;
61
62bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
63{
64 SCLogDebug("ports %u->%u ports %d %d %d %d", sp, dp, g_vxlan_ports[0], g_vxlan_ports[1],
65 g_vxlan_ports[2], g_vxlan_ports[3]);
66
67 if (g_vxlan_enabled) {
68 for (int i = 0; i < g_vxlan_ports_idx; i++) {
69 if (g_vxlan_ports[i] == VXLAN_UNSET_PORT)
70 return false;
71 const int port = g_vxlan_ports[i];
72 if (port == (const int)sp || port == (const int)dp)
73 return true;
74 }
75 }
76 return false;
77}
78
79static void DecodeVXLANConfigPorts(const char *pstr)
80{
81 SCLogDebug("parsing \'%s\'", pstr);
82
83 DetectPort *head = NULL;
84 DetectPortParse(NULL, &head, pstr);
85
86 g_vxlan_ports_idx = 0;
87 for (DetectPort *p = head; p != NULL; p = p->next) {
88 if (g_vxlan_ports_idx >= VXLAN_MAX_PORTS) {
89 SCLogWarning("more than %d VXLAN ports defined", VXLAN_MAX_PORTS);
90 break;
91 }
92 g_vxlan_ports[g_vxlan_ports_idx++] = (int)p->port;
93 }
94
96}
97
99{
100 int enabled = 0;
101 if (SCConfGetBool("decoder.vxlan.enabled", &enabled) == 1) {
102 if (enabled) {
103 g_vxlan_enabled = true;
104 } else {
105 g_vxlan_enabled = false;
106 }
107 }
108
109 if (g_vxlan_enabled) {
110 SCConfNode *node = SCConfGetNode("decoder.vxlan.ports");
111 if (node && node->val) {
112 DecodeVXLANConfigPorts(node->val);
113 } else {
114 DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
115 }
116 }
117}
118
119/** \param pkt payload data directly above UDP header
120 * \param len length in bytes of pkt
121 */
123 const uint8_t *pkt, uint32_t len)
124{
125 DEBUG_VALIDATE_BUG_ON(pkt == NULL);
126
127 /* Initial packet validation */
128 if (unlikely(!g_vxlan_enabled))
129 return TM_ECODE_FAILED;
130
131 if (len < (VXLAN_HEADER_LEN + sizeof(EthernetHdr)))
132 return TM_ECODE_FAILED;
133 if (!PacketIncreaseCheckLayers(p)) {
134 return TM_ECODE_FAILED;
135 }
136
137 const VXLANHeader *vxlanh = (const VXLANHeader *)pkt;
138 if ((vxlanh->flags[0] & 0x08) == 0 || vxlanh->res != 0)
139 return TM_ECODE_FAILED;
140
141#if DEBUG
142 uint32_t vni = (vxlanh->vni[0] << 16) + (vxlanh->vni[1] << 8) + (vxlanh->vni[2]);
143 SCLogDebug("VXLAN vni %u", vni);
144#endif
145
146 /* Increment stats counter for VXLAN packets */
148
149 EthernetHdr *ethh = (EthernetHdr *)(pkt + VXLAN_HEADER_LEN);
150 int decode_tunnel_proto = DECODE_TUNNEL_UNSET;
151
152 /* Look at encapsulated Ethernet frame to get next protocol */
153 uint16_t eth_type = SCNtohs(ethh->eth_type);
154 SCLogDebug("VXLAN ethertype 0x%04x", eth_type);
155
156 switch (eth_type) {
158 SCLogDebug("VXLAN found ARP");
159 break;
160 case ETHERNET_TYPE_IP:
161 SCLogDebug("VXLAN found IPv4");
162 decode_tunnel_proto = DECODE_TUNNEL_IPV4;
163 break;
165 SCLogDebug("VXLAN found IPv6");
166 decode_tunnel_proto = DECODE_TUNNEL_IPV6;
167 break;
171 SCLogDebug("VXLAN found VLAN");
172 decode_tunnel_proto = DECODE_TUNNEL_VLAN;
173 break;
174 default:
175 SCLogDebug("VXLAN found unsupported Ethertype - expected IPv4, IPv6, VLAN, or ARP");
177 }
178
179 /* Set-up and process inner packet if it is a supported ethertype */
180 if (decode_tunnel_proto != DECODE_TUNNEL_UNSET) {
182 len - (VXLAN_HEADER_LEN + ETHERNET_HEADER_LEN), decode_tunnel_proto);
183 if (tp != NULL) {
186 }
187 }
188
189 return TM_ECODE_OK;
190}
191
192#ifdef UNITTESTS
193
194/**
195 * \test DecodeVXLANTest01 test a good vxlan header.
196 * Contains a DNS request packet.
197 */
198static int DecodeVXLANtest01 (void)
199{
200 uint8_t raw_vxlan[] = {
201 0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
202 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
203 0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
204 0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
205 0x08, 0x00, /* another IPv4 0x0800 */
206 0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
207 0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
208 0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
209 };
211 FAIL_IF_NULL(p);
214 memset(&tv, 0, sizeof(ThreadVars));
215 memset(&dtv, 0, sizeof(DecodeThreadVars));
216
217 DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
219
220 DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
221 FAIL_IF_NOT(PacketIsUDP(p));
222 FAIL_IF(tv.decode_pq.top == NULL);
223
225 FAIL_IF_NOT(PacketIsUDP(tp));
226 FAIL_IF_NOT(tp->sp == 53);
227
228 FlowShutdown();
229 PacketFree(p);
231 PASS;
232}
233
234/**
235 * \test DecodeVXLANtest02 tests default port disabled by the config.
236 */
237static int DecodeVXLANtest02 (void)
238{
239 uint8_t raw_vxlan[] = {
240 0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
241 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
242 0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
243 0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
244 0x08, 0x00, /* another IPv4 0x0800 */
245 0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
246 0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
247 0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
248 };
250 FAIL_IF_NULL(p);
253 memset(&tv, 0, sizeof(ThreadVars));
254 memset(&dtv, 0, sizeof(DecodeThreadVars));
255
256 DecodeVXLANConfigPorts("1");
258
259 DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
260 FAIL_IF_NOT(PacketIsUDP(p));
261 FAIL_IF(tv.decode_pq.top != NULL);
262
263 DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S); /* reset */
264 FlowShutdown();
265 PacketFree(p);
266 PASS;
267}
268#endif /* UNITTESTS */
269
271{
272#ifdef UNITTESTS
273 UtRegisterTest("DecodeVXLANtest01",
274 DecodeVXLANtest01);
275 UtRegisterTest("DecodeVXLANtest02",
276 DecodeVXLANtest02);
277#endif /* UNITTESTS */
278}
uint8_t len
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition conf.c:181
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition conf.c:497
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition counters.c:166
uint16_t eth_type
#define ETHERNET_TYPE_IP
#define ETHERNET_TYPE_ARP
#define ETHERNET_TYPE_8021QINQ
#define ETHERNET_HEADER_LEN
#define ETHERNET_TYPE_IPV6
#define ETHERNET_TYPE_8021AD
@ VXLAN_UNKNOWN_PAYLOAD_TYPE
int DecodeUDP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition decode-udp.c:75
#define ETHERNET_TYPE_VLAN
Definition decode-vlan.h:31
#define VXLAN_HEADER_LEN
#define VXLAN_MAX_PORTS
#define VXLAN_UNSET_PORT
int DecodeVXLAN(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
struct VXLANHeader_ VXLANHeader
void DecodeVXLANConfig(void)
void DecodeVXLANRegisterTests(void)
bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
#define VXLAN_DEFAULT_PORT_S
#define VXLAN_DEFAULT_PORT
#define PKT_SET_SRC(p, src_val)
Definition decode.h:1325
@ DECODE_TUNNEL_IPV6
Definition decode.h:1107
@ DECODE_TUNNEL_UNSET
Definition decode.h:1112
@ DECODE_TUNNEL_VLAN
Definition decode.h:1105
@ DECODE_TUNNEL_IPV4
Definition decode.h:1106
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition decode.h:1194
@ PKT_SRC_DECODER_VXLAN
Definition decode.h:60
int DetectPortParse(const DetectEngineCtx *de_ctx, DetectPort **head, const char *str)
Function for parsing port strings.
void DetectPortCleanupList(const DetectEngineCtx *de_ctx, DetectPort *head)
Free a DetectPort list and each of its members.
Flow * head
Definition flow-hash.h:1
void FlowInitConfig(bool quiet)
initialize the configuration
Definition flow.c:547
void FlowShutdown(void)
shutdown the flow engine
Definition flow.c:691
#define FLOW_QUIET
Definition flow.h:43
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.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition decode.c:258
Packet * PacketTunnelPktSetup(ThreadVars *tv, DecodeThreadVars *dtv, Packet *parent, const uint8_t *pkt, uint32_t len, enum DecodeTunnelProto proto)
Setup a pseudo packet (tunnel)
Definition decode.c:393
void PacketFreeOrRelease(Packet *p)
Return a packet to where it was allocated.
Definition decode.c:276
void PacketFree(Packet *p)
Return a malloced packet.
Definition decode.c:219
void PacketEnqueueNoLock(PacketQueueNoLock *qnl, Packet *p)
Packet * PacketDequeueNoLock(PacketQueueNoLock *qnl)
Structure to hold thread specific data for all decode modules.
Definition decode.h:963
uint16_t counter_vxlan
Definition decode.h:1004
Port structure for detection engine.
Definition detect.h:220
struct Flow_ * next
Definition flow.h:396
struct Packet_ * top
Port sp
Definition decode.h:508
char * val
Definition conf.h:39
Per thread variable structure.
Definition threadvars.h:58
PacketQueueNoLock decode_pq
Definition threadvars.h:112
uint16_t gdp
uint8_t flags[2]
uint8_t vni[3]
#define SCNtohs(x)
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition util-debug.h:255
#define unlikely(expr)
#define DEBUG_VALIDATE_BUG_ON(exp)