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-engine-port.h"
35 
36 #include "flow.h"
37 
38 #include "util-validate.h"
39 #include "util-unittest.h"
40 #include "util-debug.h"
41 
42 #include "pkt-var.h"
43 #include "util-profiling.h"
44 #include "host.h"
45 
46 #define VXLAN_HEADER_LEN sizeof(VXLANHeader)
47 
48 #define VXLAN_MAX_PORTS 4
49 #define VXLAN_UNSET_PORT -1
50 #define VXLAN_DEFAULT_PORT 4789
51 #define VXLAN_DEFAULT_PORT_S "4789"
52 
53 static bool g_vxlan_enabled = true;
54 static int g_vxlan_ports_idx = 0;
55 static int g_vxlan_ports[VXLAN_MAX_PORTS] = { VXLAN_DEFAULT_PORT, VXLAN_UNSET_PORT,
57 
58 typedef struct VXLANHeader_ {
59  uint8_t flags[2];
60  uint16_t gdp;
61  uint8_t vni[3];
62  uint8_t res;
64 
65 bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
66 {
67  SCLogDebug("ports %u->%u ports %d %d %d %d", sp, dp, g_vxlan_ports[0], g_vxlan_ports[1],
68  g_vxlan_ports[2], g_vxlan_ports[3]);
69 
70  if (g_vxlan_enabled) {
71  for (int i = 0; i < g_vxlan_ports_idx; i++) {
72  if (g_vxlan_ports[i] == VXLAN_UNSET_PORT)
73  return false;
74  const int port = g_vxlan_ports[i];
75  if (port == (const int)sp || port == (const int)dp)
76  return true;
77  }
78  }
79  return false;
80 }
81 
82 static void DecodeVXLANConfigPorts(const char *pstr)
83 {
84  SCLogDebug("parsing \'%s\'", pstr);
85 
86  DetectPort *head = NULL;
87  DetectPortParse(NULL, &head, pstr);
88 
89  g_vxlan_ports_idx = 0;
90  for (DetectPort *p = head; p != NULL; p = p->next) {
91  if (g_vxlan_ports_idx >= VXLAN_MAX_PORTS) {
92  SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "more than %d VXLAN ports defined",
94  break;
95  }
96  g_vxlan_ports[g_vxlan_ports_idx++] = (int)p->port;
97  }
98 
100 }
101 
103 {
104  int enabled = 0;
105  if (ConfGetBool("decoder.vxlan.enabled", &enabled) == 1) {
106  if (enabled) {
107  g_vxlan_enabled = true;
108  } else {
109  g_vxlan_enabled = false;
110  }
111  }
112 
113  if (g_vxlan_enabled) {
114  ConfNode *node = ConfGetNode("decoder.vxlan.ports");
115  if (node && node->val) {
116  DecodeVXLANConfigPorts(node->val);
117  } else {
118  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
119  }
120  }
121 }
122 
123 /** \param pkt payload data directly above UDP header
124  * \param len length in bytes of pkt
125  */
127  const uint8_t *pkt, uint32_t len)
128 {
129  DEBUG_VALIDATE_BUG_ON(pkt == NULL);
130 
131  /* Initial packet validation */
132  if (unlikely(!g_vxlan_enabled))
133  return TM_ECODE_FAILED;
134 
135  if (len < (VXLAN_HEADER_LEN + sizeof(EthernetHdr)))
136  return TM_ECODE_FAILED;
137  if (!PacketIncreaseCheckLayers(p)) {
138  return TM_ECODE_FAILED;
139  }
140 
141  const VXLANHeader *vxlanh = (const VXLANHeader *)pkt;
142  if ((vxlanh->flags[0] & 0x08) == 0 || vxlanh->res != 0)
143  return TM_ECODE_FAILED;
144 
145 #if DEBUG
146  uint32_t vni = (vxlanh->vni[0] << 16) + (vxlanh->vni[1] << 8) + (vxlanh->vni[2]);
147  SCLogDebug("VXLAN vni %u", vni);
148 #endif
149 
150  /* Increment stats counter for VXLAN packets */
152 
153  EthernetHdr *ethh = (EthernetHdr *)(pkt + VXLAN_HEADER_LEN);
154  int decode_tunnel_proto = DECODE_TUNNEL_UNSET;
155 
156  /* Look at encapsulated Ethernet frame to get next protocol */
157  uint16_t eth_type = SCNtohs(ethh->eth_type);
158  SCLogDebug("VXLAN ethertype 0x%04x", eth_type);
159 
160  switch (eth_type) {
161  case ETHERNET_TYPE_ARP:
162  SCLogDebug("VXLAN found ARP");
163  break;
164  case ETHERNET_TYPE_IP:
165  SCLogDebug("VXLAN found IPv4");
166  decode_tunnel_proto = DECODE_TUNNEL_IPV4;
167  break;
168  case ETHERNET_TYPE_IPV6:
169  SCLogDebug("VXLAN found IPv6");
170  decode_tunnel_proto = DECODE_TUNNEL_IPV6;
171  break;
172  case ETHERNET_TYPE_VLAN:
175  SCLogDebug("VXLAN found VLAN");
176  decode_tunnel_proto = DECODE_TUNNEL_VLAN;
177  break;
178  default:
179  SCLogDebug("VXLAN found unsupported Ethertype - expected IPv4, IPv6, VLAN, or ARP");
181  }
182 
183  /* Set-up and process inner packet if it is a supported ethertype */
184  if (decode_tunnel_proto != DECODE_TUNNEL_UNSET) {
186  len - (VXLAN_HEADER_LEN + ETHERNET_HEADER_LEN), decode_tunnel_proto);
187  if (tp != NULL) {
190  }
191  }
192 
193  return TM_ECODE_OK;
194 }
195 
196 #ifdef UNITTESTS
197 
198 /**
199  * \test DecodeVXLANTest01 test a good vxlan header.
200  * Contains a DNS request packet.
201  */
202 static int DecodeVXLANtest01 (void)
203 {
204  uint8_t raw_vxlan[] = {
205  0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
206  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
207  0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
208  0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
209  0x08, 0x00, /* another IPv4 0x0800 */
210  0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
211  0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
212  0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
213  };
214  Packet *p = PacketGetFromAlloc();
215  FAIL_IF_NULL(p);
216  ThreadVars tv;
218 
219  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
220 
221  memset(&tv, 0, sizeof(ThreadVars));
222  memset(p, 0, SIZE_OF_PACKET);
223  memset(&dtv, 0, sizeof(DecodeThreadVars));
224 
226  DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
227 
228  FAIL_IF(p->udph == NULL);
229  FAIL_IF(tv.decode_pq.top == NULL);
230 
232  FAIL_IF(tp->udph == NULL);
233  FAIL_IF_NOT(tp->sp == 53);
234 
235  FlowShutdown();
236  PacketFree(p);
237  PacketFree(tp);
238  PASS;
239 }
240 
241 /**
242  * \test DecodeVXLANtest02 tests default port disabled by the config.
243  */
244 static int DecodeVXLANtest02 (void)
245 {
246  uint8_t raw_vxlan[] = {
247  0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
248  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
249  0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
250  0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
251  0x08, 0x00, /* another IPv4 0x0800 */
252  0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
253  0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
254  0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
255  };
256  Packet *p = PacketGetFromAlloc();
257  FAIL_IF_NULL(p);
258  ThreadVars tv;
260 
261  DecodeVXLANConfigPorts("1");
262 
263  memset(&tv, 0, sizeof(ThreadVars));
264  memset(p, 0, SIZE_OF_PACKET);
265  memset(&dtv, 0, sizeof(DecodeThreadVars));
266 
268  DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
269 
270  FAIL_IF(p->udph == NULL);
271  FAIL_IF(tv.decode_pq.top != NULL);
272 
273  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S); /* reset */
274  FlowShutdown();
275  PacketFree(p);
276  PASS;
277 }
278 #endif /* UNITTESTS */
279 
281 {
282 #ifdef UNITTESTS
283  UtRegisterTest("DecodeVXLANtest01",
284  DecodeVXLANtest01);
285  UtRegisterTest("DecodeVXLANtest02",
286  DecodeVXLANtest02);
287 #endif /* UNITTESTS */
288 }
VXLANHeader_
Definition: decode-vxlan.c:58
host.h
VXLAN_UNSET_PORT
#define VXLAN_UNSET_PORT
Definition: decode-vxlan.c:49
len
uint8_t len
Definition: app-layer-dnp3.h:2
DECODE_TUNNEL_IPV6
@ DECODE_TUNNEL_IPV6
Definition: decode.h:925
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
decode-vxlan.h
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:169
DecodeUDP
int DecodeUDP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-udp.c:75
DecodeThreadVars_::counter_vxlan
uint16_t counter_vxlan
Definition: decode.h:673
ConfNode_::val
char * val
Definition: conf.h:34
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:516
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
ETHERNET_TYPE_IPV6
#define ETHERNET_TYPE_IPV6
Definition: decode-ethernet.h:39
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
VXLAN_HEADER_LEN
#define VXLAN_HEADER_LEN
Definition: decode-vxlan.c:46
DecodeVXLANEnabledForPort
bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
Definition: decode-vxlan.c:65
VXLANHeader_::flags
uint8_t flags[2]
Definition: decode-vxlan.c:59
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
DecodeVXLANConfig
void DecodeVXLANConfig(void)
Definition: decode-vxlan.c:102
util-unittest.h
VXLAN_MAX_PORTS
#define VXLAN_MAX_PORTS
Definition: decode-vxlan.c:48
ETHERNET_TYPE_8021QINQ
#define ETHERNET_TYPE_8021QINQ
Definition: decode-ethernet.h:47
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_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1170
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:516
DetectPortParse
int DetectPortParse(const DetectEngineCtx *de_ctx, DetectPort **head, const char *str)
Function for parsing port strings.
Definition: detect-engine-port.c:1243
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
ETHERNET_TYPE_8021AD
#define ETHERNET_TYPE_8021AD
Definition: decode-ethernet.h:43
PacketDequeueNoLock
Packet * PacketDequeueNoLock(PacketQueueNoLock *qnl)
Definition: packet-queue.c:206
VXLAN_DEFAULT_PORT
#define VXLAN_DEFAULT_PORT
Definition: decode-vxlan.c:50
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
Packet_::sp
Port sp
Definition: decode.h:423
eth_type
uint16_t eth_type
Definition: decode-ethernet.h:2
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:111
detect-engine-port.h
SIZE_OF_PACKET
#define SIZE_OF_PACKET
Definition: decode.h:636
DetectPort_
Port structure for detection engine.
Definition: detect.h:191
util-profiling.h
DECODE_TUNNEL_VLAN
@ DECODE_TUNNEL_VLAN
Definition: decode.h:923
Packet_
Definition: decode.h:416
VXLANHeader
struct VXLANHeader_ VXLANHeader
DECODE_TUNNEL_IPV4
@ DECODE_TUNNEL_IPV4
Definition: decode.h:924
ETHERNET_HEADER_LEN
#define ETHERNET_HEADER_LEN
Definition: decode-ethernet.h:27
DecodeVXLAN
int DecodeVXLAN(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-vxlan.c:126
SC_ERR_INVALID_YAML_CONF_ENTRY
@ SC_ERR_INVALID_YAML_CONF_ENTRY
Definition: util-error.h:169
decode-events.h
Flow_::next
struct Flow_ * next
Definition: flow.h:401
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:30
PacketEnqueueNoLock
void PacketEnqueueNoLock(PacketQueueNoLock *qnl, Packet *p)
Definition: packet-queue.c:167
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:395
suricata-common.h
PacketQueueNoLock_::top
struct Packet_ * top
Definition: packet-queue.h:33
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:644
VXLANHeader_::vni
uint8_t vni[3]
Definition: decode-vxlan.c:61
VXLANHeader_::res
uint8_t res
Definition: decode-vxlan.c:62
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
VXLANHeader_::gdp
uint16_t gdp
Definition: decode-vxlan.c:60
util-validate.h
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:150
PKT_SRC_DECODER_VXLAN
@ PKT_SRC_DECODER_VXLAN
Definition: decode.h:59
DecodeVXLANRegisterTests
void DecodeVXLANRegisterTests(void)
Definition: decode-vxlan.c:280
head
Flow * head
Definition: flow-hash.h:1
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:640
ConfNode_
Definition: conf.h:32
VXLAN_UNKNOWN_PAYLOAD_TYPE
@ VXLAN_UNKNOWN_PAYLOAD_TYPE
Definition: decode-events.h:194
ETHERNET_TYPE_ARP
#define ETHERNET_TYPE_ARP
Definition: decode-ethernet.h:35
ThreadVars_::decode_pq
PacketQueueNoLock decode_pq
Definition: threadvars.h:110
Packet_::udph
UDPHdr * udph
Definition: decode.h:535
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:1031
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:42
ETHERNET_TYPE_VLAN
#define ETHERNET_TYPE_VLAN
Definition: decode-vlan.h:31
PacketTunnelPktSetup
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:285
VXLAN_DEFAULT_PORT_S
#define VXLAN_DEFAULT_PORT_S
Definition: decode-vxlan.c:51
flow.h
ETHERNET_TYPE_IP
#define ETHERNET_TYPE_IP
Definition: decode-ethernet.h:34
DetectPortCleanupList
void DetectPortCleanupList(const DetectEngineCtx *de_ctx, DetectPort *head)
Free a DetectPort list and each of its members.
Definition: detect-engine-port.c:121
DECODE_TUNNEL_UNSET
@ DECODE_TUNNEL_UNSET
Definition: decode.h:929
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111