suricata
decode-vxlan.c
Go to the documentation of this file.
1 /* Copyright (C) 2019 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-unittest.h"
39 #include "util-debug.h"
40 
41 #include "pkt-var.h"
42 #include "util-profiling.h"
43 #include "host.h"
44 
45 #define VXLAN_HEADER_LEN sizeof(VXLANHeader)
46 
47 #define VXLAN_MAX_PORTS 4
48 #define VXLAN_UNSET_PORT -1
49 #define VXLAN_DEFAULT_PORT 4789
50 #define VXLAN_DEFAULT_PORT_S "4789"
51 
52 static bool g_vxlan_enabled = true;
53 static int g_vxlan_ports_idx = 0;
54 static int g_vxlan_ports[VXLAN_MAX_PORTS] = { VXLAN_DEFAULT_PORT, VXLAN_UNSET_PORT,
56 
57 typedef struct VXLANHeader_ {
58  uint8_t flags[2];
59  uint16_t gdp;
60  uint8_t vni[3];
61  uint8_t res;
63 
64 bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
65 {
66  SCLogDebug("ports %u->%u ports %d %d %d %d", sp, dp, g_vxlan_ports[0], g_vxlan_ports[1],
67  g_vxlan_ports[2], g_vxlan_ports[3]);
68 
69  if (g_vxlan_enabled) {
70  for (int i = 0; i < g_vxlan_ports_idx; i++) {
71  if (g_vxlan_ports[i] == VXLAN_UNSET_PORT)
72  return false;
73  const int port = g_vxlan_ports[i];
74  if (port == (const int)sp || port == (const int)dp)
75  return true;
76  }
77  }
78  return false;
79 }
80 
81 static void DecodeVXLANConfigPorts(const char *pstr)
82 {
83  SCLogDebug("parsing \'%s\'", pstr);
84 
85  DetectPort *head = NULL;
86  DetectPortParse(NULL, &head, pstr);
87 
88  g_vxlan_ports_idx = 0;
89  for (DetectPort *p = head; p != NULL; p = p->next) {
90  if (g_vxlan_ports_idx >= VXLAN_MAX_PORTS) {
91  SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "more than %d VXLAN ports defined",
93  break;
94  }
95  g_vxlan_ports[g_vxlan_ports_idx++] = (int)p->port;
96  }
97 
99 }
100 
102 {
103  int enabled = 0;
104  if (ConfGetBool("decoder.vxlan.enabled", &enabled) == 1) {
105  if (enabled) {
106  g_vxlan_enabled = true;
107  } else {
108  g_vxlan_enabled = false;
109  }
110  }
111 
112  if (g_vxlan_enabled) {
113  ConfNode *node = ConfGetNode("decoder.vxlan.ports");
114  if (node && node->val) {
115  DecodeVXLANConfigPorts(node->val);
116  } else {
117  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
118  }
119  }
120 }
121 
122 /** \param pkt payload data directly above UDP header
123  * \param len length in bytes of pkt
124  */
126  const uint8_t *pkt, uint32_t len)
127 {
128  EthernetHdr *ethh = (EthernetHdr *)(pkt + VXLAN_HEADER_LEN);
129 
130  uint16_t eth_type;
131  int decode_tunnel_proto = DECODE_TUNNEL_UNSET;
132 
133  /* Initial packet validation */
134  if (unlikely(!g_vxlan_enabled))
135  return TM_ECODE_FAILED;
136 
137  if (len < (VXLAN_HEADER_LEN + sizeof(EthernetHdr)))
138  return TM_ECODE_FAILED;
139  if (!PacketIncreaseCheckLayers(p)) {
140  return TM_ECODE_FAILED;
141  }
142 
143  const VXLANHeader *vxlanh = (const VXLANHeader *)pkt;
144  if ((vxlanh->flags[0] & 0x08) == 0 || vxlanh->res != 0)
145  return TM_ECODE_FAILED;
146 
147 #if DEBUG
148  uint32_t vni = (vxlanh->vni[0] << 16) + (vxlanh->vni[1] << 8) + (vxlanh->vni[2]);
149  SCLogDebug("VXLAN vni %u", vni);
150 #endif
151 
152  /* Increment stats counter for VXLAN packets */
154 
155  /* Look at encapsulated Ethernet frame to get next protocol */
156  eth_type = SCNtohs(ethh->eth_type);
157  SCLogDebug("VXLAN ethertype 0x%04x", eth_type);
158 
159  switch (eth_type) {
160  case ETHERNET_TYPE_ARP:
161  SCLogDebug("VXLAN found ARP");
162  break;
163  case ETHERNET_TYPE_IP:
164  SCLogDebug("VXLAN found IPv4");
165  decode_tunnel_proto = DECODE_TUNNEL_IPV4;
166  break;
167  case ETHERNET_TYPE_IPV6:
168  SCLogDebug("VXLAN found IPv6");
169  decode_tunnel_proto = DECODE_TUNNEL_IPV6;
170  break;
171  case ETHERNET_TYPE_VLAN:
174  SCLogDebug("VXLAN found VLAN");
175  decode_tunnel_proto = DECODE_TUNNEL_VLAN;
176  break;
177  default:
178  SCLogDebug("VXLAN found unsupported Ethertype - expected IPv4, IPv6, VLAN, or ARP");
180  }
181 
182  /* Set-up and process inner packet if it is a supported ethertype */
183  if (decode_tunnel_proto != DECODE_TUNNEL_UNSET) {
185  len - (VXLAN_HEADER_LEN + ETHERNET_HEADER_LEN), decode_tunnel_proto);
186  if (tp != NULL) {
189  }
190  }
191 
192  return TM_ECODE_OK;
193 }
194 
195 #ifdef UNITTESTS
196 
197 /**
198  * \test DecodeVXLANTest01 test a good vxlan header.
199  * Contains a DNS request packet.
200  */
201 static int DecodeVXLANtest01 (void)
202 {
203  uint8_t raw_vxlan[] = {
204  0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
205  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
206  0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
207  0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
208  0x08, 0x00, /* another IPv4 0x0800 */
209  0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
210  0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
211  0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
212  };
213  Packet *p = PacketGetFromAlloc();
214  FAIL_IF_NULL(p);
215  ThreadVars tv;
217 
218  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
219 
220  memset(&tv, 0, sizeof(ThreadVars));
221  memset(p, 0, SIZE_OF_PACKET);
222  memset(&dtv, 0, sizeof(DecodeThreadVars));
223 
225  DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
226 
227  FAIL_IF(p->udph == NULL);
228  FAIL_IF(tv.decode_pq.top == NULL);
229 
231  FAIL_IF(tp->udph == NULL);
232  FAIL_IF_NOT(tp->sp == 53);
233 
234  FlowShutdown();
235  PacketFree(p);
236  PacketFree(tp);
237  PASS;
238 }
239 
240 /**
241  * \test DecodeVXLANtest02 tests default port disabled by the config.
242  */
243 static int DecodeVXLANtest02 (void)
244 {
245  uint8_t raw_vxlan[] = {
246  0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
247  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
248  0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
249  0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
250  0x08, 0x00, /* another IPv4 0x0800 */
251  0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
252  0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
253  0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
254  };
255  Packet *p = PacketGetFromAlloc();
256  FAIL_IF_NULL(p);
257  ThreadVars tv;
259 
260  DecodeVXLANConfigPorts("1");
261 
262  memset(&tv, 0, sizeof(ThreadVars));
263  memset(p, 0, SIZE_OF_PACKET);
264  memset(&dtv, 0, sizeof(DecodeThreadVars));
265 
267  DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
268 
269  FAIL_IF(p->udph == NULL);
270  FAIL_IF(tv.decode_pq.top != NULL);
271 
272  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S); /* reset */
273  FlowShutdown();
274  PacketFree(p);
275  PASS;
276 }
277 #endif /* UNITTESTS */
278 
280 {
281 #ifdef UNITTESTS
282  UtRegisterTest("DecodeVXLANtest01",
283  DecodeVXLANtest01);
284  UtRegisterTest("DecodeVXLANtest02",
285  DecodeVXLANtest02);
286 #endif /* UNITTESTS */
287 }
VXLANHeader_
Definition: decode-vxlan.c:57
host.h
VXLAN_UNSET_PORT
#define VXLAN_UNSET_PORT
Definition: decode-vxlan.c:48
len
uint8_t len
Definition: app-layer-dnp3.h:2
DECODE_TUNNEL_IPV6
@ DECODE_TUNNEL_IPV6
Definition: decode.h:923
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:672
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:45
FlowInitConfig
void FlowInitConfig(char quiet)
initialize the configuration
Definition: flow.c:516
VXLAN_UNKNOWN_PAYLOAD_TYPE
@ VXLAN_UNKNOWN_PAYLOAD_TYPE
Definition: decode-events.h:190
DecodeVXLANEnabledForPort
bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
Definition: decode-vxlan.c:64
VXLANHeader_::flags
uint8_t flags[2]
Definition: decode-vxlan.c:58
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
DecodeVXLANConfig
void DecodeVXLANConfig(void)
Definition: decode-vxlan.c:101
util-unittest.h
VXLAN_MAX_PORTS
#define VXLAN_MAX_PORTS
Definition: decode-vxlan.c:47
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:1167
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:49
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
Packet_::sp
Port sp
Definition: decode.h:422
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:635
DetectPort_
Port structure for detection engine.
Definition: detect.h:191
util-profiling.h
DECODE_TUNNEL_VLAN
@ DECODE_TUNNEL_VLAN
Definition: decode.h:921
Packet_
Definition: decode.h:415
VXLANHeader
struct VXLANHeader_ VXLANHeader
DECODE_TUNNEL_IPV4
@ DECODE_TUNNEL_IPV4
Definition: decode.h:922
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:125
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:60
VXLANHeader_::res
uint8_t res
Definition: decode-vxlan.c:61
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
VXLANHeader_::gdp
uint16_t gdp
Definition: decode-vxlan.c:59
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:279
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:639
ConfNode_
Definition: conf.h:32
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:534
ENGINE_SET_INVALID_EVENT
#define ENGINE_SET_INVALID_EVENT(p, e)
Definition: decode.h:1028
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:50
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:927