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 decoder.
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 #include "decode-vxlan.h"
29 #include "decode-events.h"
30 
31 #include "detect-engine-port.h"
32 
33 #include "flow.h"
34 
35 #include "util-unittest.h"
36 #include "util-debug.h"
37 
38 #include "pkt-var.h"
39 #include "util-profiling.h"
40 #include "host.h"
41 
42 #define VXLAN_HEADER_LEN 8
43 #define VXLAN_DEFAULT_PORT 4789
44 #define VXLAN_DEFAULT_PORT_S "4789"
45 
46 static bool g_vxlan_enabled = true;
47 static int g_vxlan_ports[4] = { VXLAN_DEFAULT_PORT, -1, -1, -1 };
48 static int g_vxlan_ports_idx = 0;
49 
50 bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
51 {
52  SCLogDebug("ports %u->%u ports %d %d %d %d", sp, dp,
53  g_vxlan_ports[0], g_vxlan_ports[1],
54  g_vxlan_ports[2], g_vxlan_ports[3]);
55 
56  if (g_vxlan_enabled) {
57  for (int i = 0; i < g_vxlan_ports_idx; i++) {
58  if (g_vxlan_ports[i] == -1)
59  return false;
60  const int port = g_vxlan_ports[i];
61  if (port == (const int)sp ||
62  port == (const int)dp)
63  return true;
64  }
65  }
66  return false;
67 }
68 
69 static void DecodeVXLANConfigPorts(const char *pstr)
70 {
71  SCLogDebug("parsing \'%s\'", pstr);
72 
73  DetectPort *head = NULL;
74  DetectPortParse(NULL, &head, pstr);
75 
76  g_vxlan_ports_idx = 0;
77  for (DetectPort *p = head; p != NULL; p = p->next) {
78  if (g_vxlan_ports_idx >= 4) {
80  "more than 4 VXLAN ports defined");
81  break;
82  }
83  g_vxlan_ports[g_vxlan_ports_idx++] = (int)p->port;
84  }
86 }
87 
89 {
90  int enabled = 0;
91  if (ConfGetBool("decoder.vxlan.enabled", &enabled) == 1) {
92  if (enabled) {
93  g_vxlan_enabled = true;
94  } else {
95  g_vxlan_enabled = false;
96  }
97  }
98 
99  if (g_vxlan_enabled) {
100  ConfNode *node = ConfGetNode("decoder.vxlan.ports");
101  if (node && node->val) {
102  DecodeVXLANConfigPorts(node->val);
103  } else {
104  DecodeVXLANConfigPorts(VXLAN_DEFAULT_PORT_S);
105  }
106  }
107 }
108 
109 typedef struct VXLANHeader_ {
110  uint8_t flags[2];
111  uint16_t gdp;
112  uint8_t vni[3];
113  uint8_t res;
115 
116 /** \param pkt payload data directly above UDP header
117  * \param len length in bytes of pkt
118  */
120  const uint8_t *pkt, uint32_t len)
121 {
122  if (unlikely(!g_vxlan_enabled))
123  return TM_ECODE_FAILED;
124 
125  if (len < (sizeof(VXLANHeader) + sizeof(EthernetHdr)))
126  return TM_ECODE_FAILED;
127 
128  const VXLANHeader *vxlanh = (const VXLANHeader *)pkt;
129  if ((vxlanh->flags[0] & 0x08) == 0 || vxlanh->res != 0) {
130  return TM_ECODE_FAILED;
131  }
132 
133 #if DEBUG
134  uint32_t vni = (vxlanh->vni[0] << 16) + (vxlanh->vni[1] << 8) + (vxlanh->vni[2]);
135  SCLogDebug("VXLAN vni %u", vni);
136 #endif
137 
139 
140  /* VXLAN encapsulate Layer 2 in UDP, most likely IPv4 and IPv6 */
141 
142  EthernetHdr *ethh = (EthernetHdr *)(pkt + VXLAN_HEADER_LEN);
143  SCLogDebug("VXLAN ethertype 0x%04x", SCNtohs(ethh->eth_type));
144 
145  /* Best guess at inner packet. */
146  switch (SCNtohs(ethh->eth_type)) {
147  case ETHERNET_TYPE_ARP:
148  SCLogDebug("VXLAN found ARP");
149  break;
150  case ETHERNET_TYPE_IP: {
151  SCLogDebug("VXLAN found IPv4");
154  if (tp != NULL) {
157  }
158  break;
159  }
160  case ETHERNET_TYPE_IPV6: {
161  SCLogDebug("VXLAN found IPv6");
164  if (tp != NULL) {
167  }
168  break;
169  }
170  default:
171  SCLogDebug("VXLAN found no known Ethertype - only checks for IPv4, IPv6, ARP");
172  /* ENGINE_SET_INVALID_EVENT(p, VXLAN_UNKNOWN_PAYLOAD_TYPE);*/
173  break;
174  }
175 
176  return TM_ECODE_OK;
177 }
178 
179 #ifdef UNITTESTS
180 
181 /**
182  * \test DecodeVXLANTest01 test a good vxlan header.
183  * Contains a DNS request packet
184  */
185 static int DecodeVXLANtest01 (void)
186 {
187  uint8_t raw_vxlan[] = {
188  0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
189  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
190  0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
191  0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
192  0x08, 0x00, /* another IPv4 0x0800 */
193  0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
194  0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
195  0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
196  };
197  Packet *p = PacketGetFromAlloc();
198  FAIL_IF_NULL(p);
199  ThreadVars tv;
201 
202  DecodeVXLANConfigPorts("4789");
203 
204  memset(&tv, 0, sizeof(ThreadVars));
205  memset(p, 0, SIZE_OF_PACKET);
206  memset(&dtv, 0, sizeof(DecodeThreadVars));
207 
209  DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
210 
211  FAIL_IF(p->udph == NULL);
212  FAIL_IF(tv.decode_pq.top == NULL);
214  FAIL_IF(tp->udph == NULL);
215  FAIL_IF_NOT(tp->sp == 53);
216 
217  FlowShutdown();
218  PacketFree(p);
219  PacketFree(tp);
220  PASS;
221 }
222 
223 /**
224  * \test test port disabled in config
225  */
226 static int DecodeVXLANtest02 (void)
227 {
228  uint8_t raw_vxlan[] = {
229  0x12, 0xb5, 0x12, 0xb5, 0x00, 0x3a, 0x87, 0x51, /* UDP header */
230  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, /* VXLAN header */
231  0x10, 0x00, 0x00, 0x0c, 0x01, 0x00, /* inner destination MAC */
232  0x00, 0x51, 0x52, 0xb3, 0x54, 0xe5, /* inner source MAC */
233  0x08, 0x00, /* another IPv4 0x0800 */
234  0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
235  0x44, 0x45, 0x0a, 0x60, 0x00, 0x0a, 0xb9, 0x1b, 0x73, 0x06, /* IPv4 hdr */
236  0x00, 0x35, 0x30, 0x39, 0x00, 0x08, 0x98, 0xe4 /* UDP probe src port 53 */
237  };
238  Packet *p = PacketGetFromAlloc();
239  FAIL_IF_NULL(p);
240  ThreadVars tv;
242 
243  DecodeVXLANConfigPorts("1");
244 
245  memset(&tv, 0, sizeof(ThreadVars));
246  memset(p, 0, SIZE_OF_PACKET);
247  memset(&dtv, 0, sizeof(DecodeThreadVars));
248 
250  DecodeUDP(&tv, &dtv, p, raw_vxlan, sizeof(raw_vxlan));
251 
252  FAIL_IF(p->udph == NULL);
253  FAIL_IF(tv.decode_pq.top != NULL);
254 
255  DecodeVXLANConfigPorts("4789"); /* reset */
256  FlowShutdown();
257  PacketFree(p);
258  PASS;
259 }
260 #endif /* UNITTESTS */
261 
263 {
264 #ifdef UNITTESTS
265  UtRegisterTest("DecodeVXLANtest01",
266  DecodeVXLANtest01);
267  UtRegisterTest("DecodeVXLANtest02",
268  DecodeVXLANtest02);
269 #endif /* UNITTESTS */
270 }
VXLANHeader_
Definition: decode-vxlan.c:109
host.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
DECODE_TUNNEL_IPV6
@ DECODE_TUNNEL_IPV6
Definition: decode.h:888
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:74
DecodeThreadVars_::counter_vxlan
uint16_t counter_vxlan
Definition: decode.h:654
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:42
FlowInitConfig
void FlowInitConfig(char quiet)
initialize the configuration
Definition: flow.c:531
DecodeVXLANEnabledForPort
bool DecodeVXLANEnabledForPort(const uint16_t sp, const uint16_t dp)
Definition: decode-vxlan.c:50
VXLANHeader_::flags
uint8_t flags[2]
Definition: decode-vxlan.c:110
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
DecodeVXLANConfig
void DecodeVXLANConfig(void)
Definition: decode-vxlan.c:88
util-unittest.h
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:78
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1128
DetectPortParse
int DetectPortParse(const DetectEngineCtx *de_ctx, DetectPort **head, const char *str)
Function for parsing port strings.
Definition: detect-engine-port.c:1241
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
PacketDequeueNoLock
Packet * PacketDequeueNoLock(PacketQueueNoLock *qnl)
Definition: packet-queue.c:206
VXLAN_DEFAULT_PORT
#define VXLAN_DEFAULT_PORT
Definition: decode-vxlan.c:43
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
Packet_::sp
Port sp
Definition: decode.h:418
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:105
detect-engine-port.h
SIZE_OF_PACKET
#define SIZE_OF_PACKET
Definition: decode.h:621
DetectPort_
Port structure for detection engine.
Definition: detect.h:191
util-profiling.h
Packet_
Definition: decode.h:411
VXLANHeader
struct VXLANHeader_ VXLANHeader
DECODE_TUNNEL_IPV4
@ DECODE_TUNNEL_IPV4
Definition: decode.h:887
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:119
SC_ERR_INVALID_YAML_CONF_ENTRY
@ SC_ERR_INVALID_YAML_CONF_ENTRY
Definition: util-error.h:169
decode-events.h
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:398
suricata-common.h
PacketQueueNoLock_::top
struct Packet_ * top
Definition: packet-queue.h:33
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:683
VXLANHeader_::vni
uint8_t vni[3]
Definition: decode-vxlan.c:112
VXLANHeader_::res
uint8_t res
Definition: decode-vxlan.c:113
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
VXLANHeader_::gdp
uint16_t gdp
Definition: decode-vxlan.c:111
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:144
PKT_SRC_DECODER_VXLAN
@ PKT_SRC_DECODER_VXLAN
Definition: decode.h:58
DecodeVXLANRegisterTests
void DecodeVXLANRegisterTests(void)
Definition: decode-vxlan.c:262
head
Flow * head
Definition: flow-hash.h:0
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:625
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:527
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:38
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:278
VXLAN_DEFAULT_PORT_S
#define VXLAN_DEFAULT_PORT_S
Definition: decode-vxlan.c:44
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