wireshark  2.1.1
About: Wireshark is a network protocol analyzer. It lets you capture and interactively browse the traffic running on a computer network. Development release.
  Fossies Dox: wireshark-2.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

packet-vxlan.c
Go to the documentation of this file.
1 /* packet-vxlan.c
2  *
3  * Routines for Virtual eXtensible Local Area Network (VXLAN) packet dissection
4  * RFC 7348 plus draft-smith-vxlan-group-policy-01
5  *
6  * (c) Copyright 2016, Sumit Kumar Jha <sjha3@ncsu.edu>
7  * Support for VXLAN GPE (https://www.ietf.org/id/draft-ietf-nvo3-vxlan-gpe-02.txt)
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 
28 #include "config.h"
29 
30 #include <epan/packet.h>
31 #include <epan/tfs.h>
32 
33 #define UDP_PORT_VXLAN 4789
34 #define UDP_PORT_VXLAN_GPE 4790
35 #define VXLAN_IPV4 1
36 #define VXLAN_IPV6 2
37 #define VXLAN_ETHERNET 3
38 #define VXLAN_NSH 4
39 #define VXLAN_MPLS 5
40 
41 void proto_register_vxlan(void);
42 void proto_reg_handoff_vxlan(void);
43 
44 static int proto_vxlan = -1;
45 static int proto_vxlan_gpe = -1;
46 
47 static int hf_vxlan_flags = -1;
48 static int hf_vxlan_gpe_flags = -1;
49 static int hf_vxlan_flags_reserved = -1;
50 static int hf_vxlan_reserved_8 = -1;
51 static int hf_vxlan_flag_a = -1;
52 static int hf_vxlan_flag_d = -1;
53 static int hf_vxlan_flag_i = -1;
54 static int hf_vxlan_flag_g = -1;
55 static int hf_vxlan_gbp = -1;
56 static int hf_vxlan_vni = -1;
57 static int hf_vxlan_gpe_flag_i = -1;
58 static int hf_vxlan_gpe_flag_p = -1;
59 static int hf_vxlan_gpe_flag_o = -1;
60 static int hf_vxlan_gpe_flag_ver = -1;
61 static int hf_vxlan_gpe_flag_reserved = -1;
62 static int hf_vxlan_gpe_reserved_16 = -1;
63 static int hf_vxlan_next_proto = -1;
64 static int ett_vxlan = -1;
65 static int ett_vxlan_flags = -1;
66 
67 static const int *flags_fields[] = {
68  &hf_vxlan_flag_g,
69  &hf_vxlan_flag_d,
70  &hf_vxlan_flag_i,
71  &hf_vxlan_flag_a,
72  &hf_vxlan_flags_reserved,
73  NULL
74  };
75 
76 static const int *gpe_flags_fields[] = {
77  &hf_vxlan_gpe_flag_ver,
78  &hf_vxlan_gpe_flag_i,
79  &hf_vxlan_gpe_flag_p,
80  &hf_vxlan_gpe_flag_o,
81  &hf_vxlan_gpe_flag_reserved,
82  NULL
83 
84  };
85 
86 static const value_string vxlan_next_protocols[] = {
87  { VXLAN_IPV4, "IPv4" },
88  { VXLAN_IPV6, "IPv6" },
89  { VXLAN_ETHERNET, "Ethernet" },
90  { VXLAN_NSH, "Network Service Header" },
91  { VXLAN_MPLS, "MPLS"},
92  { 0, NULL }
93  };
94 
95 static dissector_handle_t eth_handle;
96 static dissector_handle_t ip_handle;
97 static dissector_handle_t ipv6_handle;
98 static dissector_handle_t nsh_handle;
99 static dissector_handle_t mpls_handle;
100 static dissector_table_t vxlan_dissector_table;
101 
102 static int
103 dissect_vxlan_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int is_gpe)
104 {
105  proto_tree *vxlan_tree;
106  proto_item *ti;
107  tvbuff_t *next_tvb;
108  int offset = 0;
109  int vxlan_next_proto=-1;
110 
111  col_set_str(pinfo->cinfo, COL_PROTOCOL, "VxLAN");
112  col_clear(pinfo->cinfo, COL_INFO);
113 
114 
115  ti = proto_tree_add_item(tree, proto_vxlan, tvb, offset, 8, ENC_NA);
116  vxlan_tree = proto_item_add_subtree(ti, ett_vxlan);
117 
118  if(is_gpe) {
119  proto_tree_add_bitmask(vxlan_tree, tvb, offset, hf_vxlan_gpe_flags, ett_vxlan_flags, gpe_flags_fields, ENC_BIG_ENDIAN);
120  offset += 1;
121 
122  proto_tree_add_item(vxlan_tree, hf_vxlan_gpe_reserved_16, tvb, offset, 2, ENC_BIG_ENDIAN);
123  offset += 2;
124 
125  proto_tree_add_item(vxlan_tree, hf_vxlan_next_proto, tvb, offset, 1, ENC_BIG_ENDIAN);
126  vxlan_next_proto = tvb_get_guint8(tvb, offset);
127  offset += 1;
128  } else {
129  proto_tree_add_bitmask(vxlan_tree, tvb, offset, hf_vxlan_flags, ett_vxlan_flags, flags_fields, ENC_BIG_ENDIAN);
130  offset += 2;
131 
132  proto_tree_add_item(vxlan_tree, hf_vxlan_gbp, tvb, offset, 2, ENC_BIG_ENDIAN);
133  offset += 2;
134  }
135 
136  proto_tree_add_item(vxlan_tree, hf_vxlan_vni, tvb, offset, 3, ENC_BIG_ENDIAN);
137  offset += 3;
138 
139  proto_tree_add_item(vxlan_tree, hf_vxlan_reserved_8, tvb, offset, 1, ENC_BIG_ENDIAN);
140  offset += 1;
141 
142  next_tvb = tvb_new_subset_remaining(tvb, offset);
143 
144  if(is_gpe){
145  switch(vxlan_next_proto){
146 
147  case VXLAN_IPV4 :
148  call_dissector(ip_handle, next_tvb, pinfo, tree);
149  break;
150 
151  case VXLAN_IPV6 :
152  call_dissector(ipv6_handle, next_tvb, pinfo, tree);
153  break;
154 
155  case VXLAN_ETHERNET :
156  call_dissector(eth_handle, next_tvb, pinfo, tree);
157  break;
158 
159  case VXLAN_NSH :
160  if(!dissector_try_uint(vxlan_dissector_table, VXLAN_NSH, next_tvb, pinfo, tree))
161  call_data_dissector(next_tvb, pinfo, vxlan_tree);
162  break;
163 
164  case VXLAN_MPLS :
165  call_dissector(mpls_handle, next_tvb, pinfo, tree);
166  break;
167 
168  default:
169  call_data_dissector(next_tvb, pinfo, vxlan_tree);
170  break;
171  }
172  } else {
173  call_dissector(eth_handle, next_tvb, pinfo, tree);
174  }
175 
176  return tvb_captured_length(tvb);
177 }
178 
179 static int
180 dissect_vxlan_gpe(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
181 {
182  return dissect_vxlan_common(tvb, pinfo, tree, TRUE);
183 }
184 
185 
186 static int
187 dissect_vxlan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
188 {
189  return dissect_vxlan_common(tvb, pinfo, tree, FALSE);
190 }
191 
192 
193 
194 
195 /* Register VxLAN with Wireshark */
196 void
198 {
199  static hf_register_info hf[] = {
200  { &hf_vxlan_flags,
201  { "Flags", "vxlan.flags",
202  FT_UINT16, BASE_HEX, NULL, 0x00,
203  NULL, HFILL
204  },
205  },
206  { &hf_vxlan_gpe_flags,
207  { "Flags", "vxlan.flags",
208  FT_UINT8, BASE_HEX, NULL, 0x00,
209  NULL, HFILL
210  },
211  },
212  { &hf_vxlan_flags_reserved,
213  { "Reserved(R)", "vxlan.flags_reserved",
214  FT_BOOLEAN, 16, NULL, 0x77b7,
215  NULL, HFILL,
216  },
217  },
218  { &hf_vxlan_gpe_flag_reserved,
219  { "Reserved(R)", "vxlan.flags_reserved",
220  FT_UINT8, BASE_DEC, NULL, 0xC2,
221  NULL, HFILL,
222  },
223  },
224  { &hf_vxlan_flag_g,
225  { "GBP Extension", "vxlan.flag_g",
226  FT_BOOLEAN, 16, TFS(&tfs_defined_not_defined), 0x8000,
227  NULL, HFILL,
228  },
229  },
230  { &hf_vxlan_flag_i,
231  { "VXLAN Network ID (VNI)", "vxlan.flag_i",
232  FT_BOOLEAN, 16, NULL, 0x0800,
233  NULL, HFILL,
234  },
235  },
236  { &hf_vxlan_flag_d,
237  { "Don't Learn", "vxlan.flag_d",
238  FT_BOOLEAN, 16, NULL, 0x0040,
239  NULL, HFILL,
240  },
241  },
242  { &hf_vxlan_flag_a,
243  { "Policy Applied", "vxlan.flag_a",
244  FT_BOOLEAN, 16, NULL, 0x0008,
245  NULL, HFILL,
246  },
247  },
248  { &hf_vxlan_gpe_flag_ver,
249  { "Version", "vxlan.ver",
250  FT_UINT8, BASE_DEC, NULL, 0x30,
251  NULL, HFILL,
252  },
253  },
254  { &hf_vxlan_gpe_flag_i,
255  { "Instance", "vxlan.i_bit",
256  FT_UINT8, BASE_DEC, NULL, 0x08,
257  NULL, HFILL,
258  },
259  },
260  { &hf_vxlan_gpe_flag_p,
261  { "Next Protocol Bit", "vxlan.p_bit",
262  FT_UINT8, BASE_DEC, NULL, 0x04,
263  NULL, HFILL,
264  },
265  },
266  { &hf_vxlan_gpe_flag_o,
267  { "OAM bit", "vxlan.o_bit",
268  FT_UINT8, BASE_DEC, NULL, 0x01,
269  NULL, HFILL,
270  },
271  },
272  { &hf_vxlan_gbp,
273  { "Group Policy ID", "vxlan.gbp",
274  FT_UINT16, BASE_DEC, NULL, 0x00,
275  NULL, HFILL
276  },
277  },
278  { &hf_vxlan_vni,
279  { "VXLAN Network Identifier (VNI)", "vxlan.vni",
280  FT_UINT24, BASE_DEC, NULL, 0x00,
281  NULL, HFILL
282  },
283  },
284  { &hf_vxlan_reserved_8,
285  { "Reserved", "vxlan.reserved8",
286  FT_UINT8, BASE_DEC, NULL, 0x00,
287  NULL, HFILL
288  },
289  },
290  { &hf_vxlan_gpe_reserved_16,
291  { "Reserved", "vxlan.reserved_16",
292  FT_UINT16, BASE_DEC, NULL, 0x0000,
293  NULL, HFILL
294  },
295  },
296  { &hf_vxlan_next_proto,
297  { "Next Protocol", "vxlan.next_proto",
298  FT_UINT8, BASE_DEC, VALS(vxlan_next_protocols), 0x00,
299  NULL, HFILL
300  },
301  },
302  };
303 
304  /* Setup protocol subtree array */
305  static gint *ett[] = {
306  &ett_vxlan,
307  &ett_vxlan_flags,
308  };
309 
310  /* Register the protocol name and description */
311  proto_vxlan = proto_register_protocol("Virtual eXtensible Local Area Network", "VXLAN", "vxlan");
312 
313  /* Protocol registered just for Decode As */
314  proto_vxlan_gpe = proto_register_protocol("Virtual eXtensible Local Area Network (GPE)", "VXLAN (GPE)", "vxlan_gpe");
315 
316  /* Required function calls to register the header fields and subtrees used */
317  proto_register_field_array(proto_vxlan, hf, array_length(hf));
319  vxlan_dissector_table = register_dissector_table("vxlan.next_proto", "VXLAN Next Protocol", proto_vxlan, FT_UINT8, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
320 
321 
322 }
323 
324 void
326 {
327  dissector_handle_t vxlan_handle;
328  dissector_handle_t vxlan_gpe_handle;
329 
330  /*
331  * RFC 7348 Figures 1 and 2, in the Payload section, say
332  *
333  * "(Note that the original Ethernet Frame's FCS is not included)"
334  *
335  * meaning that the inner Ethernet frame does *not* include an
336  * FCS.
337  */
338  eth_handle = find_dissector_add_dependency("eth_withoutfcs", proto_vxlan);
339  ip_handle = find_dissector_add_dependency("ip", proto_vxlan);
340  ipv6_handle = find_dissector_add_dependency("ipv6", proto_vxlan);
341  nsh_handle = find_dissector_add_dependency("nsh", proto_vxlan);
342  mpls_handle = find_dissector_add_dependency("mpls", proto_vxlan);
343 
344  vxlan_handle = create_dissector_handle(dissect_vxlan, proto_vxlan);
345  vxlan_gpe_handle = create_dissector_handle(dissect_vxlan_gpe, proto_vxlan_gpe);
346  dissector_add_uint("udp.port", UDP_PORT_VXLAN, vxlan_handle);
347  dissector_add_uint("udp.port", UDP_PORT_VXLAN_GPE, vxlan_gpe_handle);
348 }
349 
350 /*
351  * Editor modelines - http://www.wireshark.org/tools/modelines.html
352  *
353  * Local variables:
354  * c-basic-offset: 4
355  * tab-width: 8
356  * indent-tabs-mode: nil
357  * End:
358  *
359  * vi: set shiftwidth=4 tabstop=8 expandtab:
360  * :indentSize=4:tabSize=8:noTabs=true:
361  */
proto_item * proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb, const gint start, gint length, const guint encoding)
Definition: proto.c:2503
void col_set_str(column_info *cinfo, const gint el, const gchar *str)
Definition: column-utils.c:686
#define TFS(x)
Definition: proto.h:83
proto_item * proto_tree_add_bitmask(proto_tree *parent_tree, tvbuff_t *tvb, const guint offset, const int hf_hdr, const gint ett, const int **fields, const guint encoding)
Definition: proto.c:9048
#define UDP_PORT_VXLAN
Definition: packet-vxlan.c:33
void proto_register_subtree_array(gint *const *indices, const int num_indices)
Definition: proto.c:6868
void proto_register_field_array(const int parent, hf_register_info *hf, const int num_records)
Definition: proto.c:6109
const true_false_string tfs_defined_not_defined
Definition: tfs.c:91
dissector_handle_t find_dissector_add_dependency(const char *name, const int parent_proto)
Definition: packet.c:2661
tvbuff_t guint32 packet_info *pinfo _U_
Definition: packet-wsp.c:2843
#define NULL
Definition: resample.c:91
#define TRUE
void proto_reg_handoff_vxlan(void)
Definition: packet-vxlan.c:325
guint8 tvb_get_guint8(tvbuff_t *tvb, const gint offset)
Definition: tvbuff.c:843
#define UDP_PORT_VXLAN_GPE
Definition: packet-vxlan.c:34
dissector_table_t register_dissector_table(const char *name, const char *ui_name, const int proto, const ftenum_t type, const int param, dissector_table_allow_e allow_dup)
Definition: packet.c:2102
struct epan_column_info * cinfo
Definition: packet_info.h:56
#define VXLAN_IPV6
Definition: packet-vxlan.c:36
const char * data
Definition: packet-ntp.c:190
#define VXLAN_ETHERNET
Definition: packet-vxlan.c:37
proto_tree * proto_item_add_subtree(proto_item *pi, const gint idx)
Definition: proto.c:5480
#define VXLAN_MPLS
Definition: packet-vxlan.c:39
void dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
Definition: packet.c:926
dissector_handle_t create_dissector_handle(dissector_t dissector, const int proto)
Definition: packet.c:2684
#define FALSE
void proto_register_vxlan(void)
Definition: packet-vxlan.c:197
proto_item * ti
Definition: packet-wsp.c:2847
WS_DLL_PUBLIC tvbuff_t * tvb_new_subset_remaining(tvbuff_t *backing, const gint backing_offset)
#define VALS(x)
Definition: proto.h:74
#define VXLAN_IPV4
Definition: packet-vxlan.c:35
int call_data_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
Definition: packet.c:2825
void col_clear(column_info *cinfo, const gint el)
Definition: column-utils.c:270
#define ENC_NA
Definition: proto.h:450
#define array_length(x)
int dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
Definition: packet.c:1221
guint tvb_captured_length(const tvbuff_t *tvb)
Definition: tvbuff.c:421
#define HFILL
Definition: proto.h:591
int call_dissector(dissector_handle_t handle, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
Definition: packet.c:2818
tvbuff_t * tvb
Definition: packet-wsp.c:2842
#define VXLAN_NSH
Definition: packet-vxlan.c:38
int proto_register_protocol(const char *name, const char *short_name, const char *filter_name)
Definition: proto.c:5632
#define ENC_BIG_ENDIAN
Definition: proto.h:354