Add dissector for Generic Network Virtualization Encapsulation (Geneve).
[metze/wireshark/wip.git] / epan / dissectors / packet-geneve.c
1 /* packet-geneve.c
2  * Routines for Geneve - Generic Network Virtualization Encapsulation
3  * http://tools.ietf.org/html/draft-gross-geneve-00
4  *
5  * Copyright (c) 2014 VMware, Inc. All Rights Reserved.
6  * Author: Jesse Gross <jesse@nicira.com>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27
28 #include "config.h"
29
30 #include <epan/etypes.h>
31 #include <epan/expert.h>
32 #include <epan/packet.h>
33
34 #define UDP_PORT_GENEVE  6081
35
36 #define VER_SHIFT 6
37 #define HDR_OPTS_LEN_MASK 0x3F
38
39 #define FLAG_OAM (1 << 7)
40
41 #define OPT_TYPE_CRITICAL (1 << 7)
42 #define OPT_FLAGS_SHIFT 5
43 #define OPT_LEN_MASK 0x1F
44
45 static const range_string class_id_names[] = {
46     { 0, 0xFF, "Standard" },
47     { 0xFFFF, 0xFFFF, "Experimental" },
48     { 0, 0, NULL }
49 };
50
51 void proto_register_geneve(void);
52 void proto_reg_handoff_geneve(void);
53
54 static int proto_geneve = -1;
55
56 static int hf_geneve_version = -1;
57 static int hf_geneve_opt_len = -1;
58 static int hf_geneve_flags = -1;
59 static int hf_geneve_flag_oam = -1;
60 static int hf_geneve_flag_critical = -1;
61 static int hf_geneve_flag_reserved = -1;
62 static int hf_geneve_proto_type = -1;
63 static int hf_geneve_vni = -1;
64 static int hf_geneve_reserved = -1;
65 static int hf_geneve_options = -1;
66 static int hf_geneve_option_class = -1;
67 static int hf_geneve_option_type = -1;
68 static int hf_geneve_option_type_critical = -1;
69 static int hf_geneve_option_flags = -1;
70 static int hf_geneve_option_flags_reserved = -1;
71 static int hf_geneve_option_length = -1;
72 static int hf_geneve_opt_unknown = -1;
73 static int hf_geneve_opt_unknown_data = -1;
74
75 static int ett_geneve = -1;
76 static int ett_geneve_flags = -1;
77 static int ett_geneve_opt_flags = -1;
78 static int ett_geneve_options = -1;
79 static int ett_geneve_unknown_opt = -1;
80
81 static expert_field ei_geneve_opt_len_invalid = EI_INIT;
82
83 static dissector_table_t ethertype_dissector_table;
84 static dissector_handle_t data_handle;
85
86 static void
87 print_flags(guint8 flags, proto_item *flag_item)
88 {
89     static const char flag_names[][5] = {"OAM", "CRIT"};
90     unsigned int i;
91
92     if (!flags) {
93         return;
94     }
95
96     proto_item_append_text(flag_item, " (");
97
98     for (i = 0; i < array_length(flag_names); i++) {
99         guint8 bit = 1 << (7 - i);
100
101         if (flags & bit) {
102             proto_item_append_text(flag_item, "%s", flag_names[i]);
103             flags &= ~bit;
104
105             if (flags) {
106                 proto_item_append_text(flag_item, ", ");
107             }
108         }
109     }
110
111     if (flags) {
112         proto_item_append_text(flag_item, "RSVD");
113     }
114
115     proto_item_append_text(flag_item, ")");
116 }
117
118 static const char *
119 format_unknown_option_name(guint16 opt_class, guint8 opt_type)
120 {
121     const char *name;
122
123     name = wmem_strdup_printf(wmem_packet_scope(),
124                               "Unknown, Class: %s (0x%04x) Type: 0x%02x",
125                               rval_to_str_const(opt_class, class_id_names, "Unknown"),
126                               opt_class, opt_type);
127
128     return name;
129 }
130
131 static void
132 dissect_unknown_option(tvbuff_t *tvb, proto_tree *opts_tree, int offset,
133                        guint16 opt_class, guint8 opt_type, int len)
134 {
135     proto_item *opt_item, *type_item, *hidden_item, *flag_item;
136     proto_tree *opt_tree, *flag_tree;
137     const char *critical;
138     guint8 flags;
139
140     critical = opt_type & OPT_TYPE_CRITICAL ? "Critical" : "Non-critical";
141
142     opt_item = proto_tree_add_item(opts_tree, hf_geneve_opt_unknown,
143                                    tvb, offset, len, ENC_NA);
144     proto_item_set_text(opt_item, "%s (%s)",
145                         format_unknown_option_name(opt_class, opt_type),
146                         critical);
147
148     opt_tree = proto_item_add_subtree(opt_item, ett_geneve_unknown_opt);
149
150     proto_tree_add_item(opt_tree, hf_geneve_option_class, tvb,
151                         offset, 2, ENC_BIG_ENDIAN);
152     offset += 2;
153
154     type_item = proto_tree_add_item(opt_tree, hf_geneve_option_type, tvb,
155                                     offset, 1, ENC_BIG_ENDIAN);
156     proto_item_append_text(type_item, " (%s)", critical);
157     hidden_item = proto_tree_add_item(opt_tree, hf_geneve_option_type_critical,
158                                       tvb, offset, 1, ENC_BIG_ENDIAN);
159     PROTO_ITEM_SET_HIDDEN(hidden_item);
160     offset += 1;
161
162     flags = tvb_get_guint8(tvb, offset) >> OPT_FLAGS_SHIFT;
163     flag_item = proto_tree_add_uint(opt_tree, hf_geneve_option_flags, tvb,
164                                     offset, 1, flags);
165     flag_tree = proto_item_add_subtree(flag_item, ett_geneve_opt_flags);
166     proto_tree_add_item(flag_tree, hf_geneve_option_flags_reserved, tvb,
167                         offset, 1, ENC_BIG_ENDIAN);
168     if (flags) {
169         proto_item_append_text(flag_item, " (RSVD)");
170     } else {
171         PROTO_ITEM_SET_HIDDEN(flag_item);
172     }
173
174     proto_tree_add_uint_format_value(opt_tree, hf_geneve_option_length, tvb,
175                                      offset, 1, len, "%u bytes", len);
176     offset += 1;
177
178     proto_tree_add_item(opt_tree, hf_geneve_opt_unknown_data, tvb, offset,
179                         len - 4, ENC_NA);
180 }
181
182 static void
183 dissect_geneve_options(tvbuff_t *tvb, packet_info *pinfo,
184                        proto_tree *geneve_tree, int offset, int len)
185 {
186     proto_item *opts_item;
187     proto_tree *opts_tree;
188     guint16 opt_class;
189     guint8 opt_type;
190     guint8 opt_len;
191
192     opts_item = proto_tree_add_item(geneve_tree, hf_geneve_options, tvb,
193                                     offset, len, ENC_NA);
194     proto_item_set_text(opts_item, "Options: (%u bytes)", len);
195     opts_tree = proto_item_add_subtree(opts_item, ett_geneve_options);
196
197     while (len > 0) {
198         opt_class = tvb_get_ntohs(tvb, offset);
199         opt_type = tvb_get_guint8(tvb, offset + 2);
200         opt_len = 4 + ((tvb_get_guint8(tvb, offset + 3) & OPT_LEN_MASK) * 4);
201
202         if (opt_len > len) {
203             proto_tree_add_expert_format(opts_tree, pinfo,
204                                          &ei_geneve_opt_len_invalid, tvb,
205                                          offset + 3, 1,
206                                          "%s (length of %u is past end of options)",
207                                          format_unknown_option_name(opt_class,
208                                                                     opt_type),
209                                          opt_len);
210             return;
211         }
212
213         dissect_unknown_option(tvb, opts_tree, offset,
214                                opt_class, opt_type, opt_len);
215
216         offset += opt_len;
217         len -= opt_len;
218     };
219 }
220
221 static void
222 dissect_geneve(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
223 {
224     proto_item *ti, *flag_item, *rsvd_item;
225     proto_tree *geneve_tree, *flag_tree;
226     tvbuff_t *next_tvb;
227     int offset = 0;
228     guint8 ver_opt;
229     guint8 flags;
230     guint16 proto_type;
231     int opts_len;
232
233     col_set_str(pinfo->cinfo, COL_PROTOCOL, "Geneve");
234     col_clear(pinfo->cinfo, COL_INFO);
235
236     proto_type = tvb_get_ntohs(tvb, 2);
237     col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
238                  val_to_str(proto_type, etype_vals, "0x%04x (unknown)"));
239
240     flags = tvb_get_guint8(tvb, 1);
241     ti = proto_tree_add_protocol_format(tree, proto_geneve, tvb, 0, -1,
242                        "Generic Network Virtualization Encapsuation, VNI: 0x%06x"
243                        "%s",
244                        tvb_get_ntoh24(tvb, 4),
245                        flags & FLAG_OAM ? ", OAM" : "");
246
247     geneve_tree = proto_item_add_subtree(ti, ett_geneve);
248
249     /* Version and option length. */
250     ver_opt = tvb_get_guint8(tvb, offset);
251     proto_tree_add_uint(geneve_tree, hf_geneve_version, tvb,
252                         offset, 1, ver_opt >> VER_SHIFT);
253     opts_len = (ver_opt & HDR_OPTS_LEN_MASK) * 4;
254     proto_tree_add_uint_format_value(geneve_tree, hf_geneve_opt_len, tvb,
255                                      offset, 1, opts_len, "%u bytes", opts_len);
256     offset += 1;
257
258     /* Flags. */
259     if (tree) {
260         flag_item = proto_tree_add_item(geneve_tree, hf_geneve_flags, tvb,
261                                        offset, 1, ENC_BIG_ENDIAN);
262         print_flags(flags, flag_item);
263
264         flag_tree = proto_item_add_subtree(flag_item, ett_geneve_flags);
265
266         proto_tree_add_item(flag_tree, hf_geneve_flag_oam, tvb, offset,
267                             1, ENC_BIG_ENDIAN);
268         proto_tree_add_item(flag_tree, hf_geneve_flag_critical, tvb, offset,
269                             1, ENC_BIG_ENDIAN);
270         proto_tree_add_item(flag_tree, hf_geneve_flag_reserved, tvb, offset,
271                             1, ENC_BIG_ENDIAN);
272     }
273     offset += 1;
274
275     /* Protocol Type. */
276     proto_tree_add_item(geneve_tree, hf_geneve_proto_type, tvb,
277                         offset, 2, ENC_BIG_ENDIAN);
278     offset += 2;
279
280     /* VNI. */
281     proto_tree_add_item(geneve_tree, hf_geneve_vni, tvb, offset, 3, ENC_BIG_ENDIAN);
282     offset += 3;
283
284     /* Reserved. */
285     rsvd_item = proto_tree_add_item(geneve_tree, hf_geneve_reserved, tvb, offset,
286                                     1, ENC_BIG_ENDIAN);
287     if (!tvb_get_guint8(tvb, offset)) {
288         PROTO_ITEM_SET_HIDDEN(rsvd_item);
289     }
290     offset += 1;
291
292     /* Options. */
293     if (tree && opts_len) {
294         dissect_geneve_options(tvb, pinfo, geneve_tree, offset, opts_len);
295     }
296     offset += opts_len;
297
298     proto_item_set_len(ti, offset);
299
300     next_tvb = tvb_new_subset_remaining(tvb, offset);
301     if (!dissector_try_uint(ethertype_dissector_table, proto_type, next_tvb, pinfo, tree))
302         call_dissector(data_handle, next_tvb, pinfo, tree);
303 }
304
305 /* Register Geneve with Wireshark */
306 void
307 proto_register_geneve(void)
308 {
309     static hf_register_info hf[] = {
310         { &hf_geneve_version,
311           { "Version", "geneve.version",
312             FT_UINT8, BASE_DEC, NULL, 0x00,
313             NULL, HFILL }
314         },
315         { &hf_geneve_opt_len,
316           { "Options Length", "geneve.options_length",
317             FT_UINT8, BASE_DEC, NULL, 0x0,
318             NULL, HFILL }
319         },
320         { &hf_geneve_flags,
321           { "Flags", "geneve.flags",
322             FT_UINT8, BASE_HEX, NULL, 0x00,
323             NULL, HFILL }
324         },
325         { &hf_geneve_flag_oam,
326           { "Operations, Administration and Management Frame", "geneve.flags.oam",
327             FT_BOOLEAN, 8, NULL, 0x80,
328             NULL, HFILL }
329         },
330         { &hf_geneve_flag_critical,
331           { "Critical Options Present", "geneve.flags.critical",
332             FT_BOOLEAN, 8, NULL, 0x40,
333             NULL, HFILL }
334         },
335         { &hf_geneve_flag_reserved,
336           { "Reserved", "geneve.flags.reserved",
337             FT_BOOLEAN, 8, NULL, 0x3F,
338             NULL, HFILL }
339         },
340         { &hf_geneve_proto_type,
341           { "Protocol Type", "geneve.proto_type",
342             FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
343             NULL, HFILL }
344         },
345         { &hf_geneve_vni,
346           { "Virtual Network Identifier (VNI)", "geneve.vni",
347             FT_UINT24, BASE_HEX, NULL, 0x0,
348             NULL, HFILL }
349         },
350         { &hf_geneve_reserved,
351           { "Reserved", "geneve.reserved",
352             FT_UINT8, BASE_HEX, NULL, 0x00,
353             NULL, HFILL }
354         },
355         { &hf_geneve_options,
356           { "Geneve Options", "geneve.options",
357             FT_BYTES, BASE_NONE, NULL, 0x00,
358             NULL, HFILL }
359         },
360         { &hf_geneve_option_class,
361           { "Class", "geneve.option.class",
362             FT_UINT16, BASE_HEX | BASE_RANGE_STRING, RVALS(class_id_names), 0x00,
363             NULL, HFILL }
364         },
365         { &hf_geneve_option_type,
366           { "Type", "geneve.option.type",
367             FT_UINT8, BASE_HEX, NULL, 0x00,
368             NULL, HFILL }
369         },
370         { &hf_geneve_option_type_critical,
371           { "Critical Option", "geneve.option.type.critical",
372             FT_BOOLEAN, 8, NULL, 0x80,
373             NULL, HFILL }
374         },
375         { &hf_geneve_option_flags,
376           { "Flags", "geneve.option.flags",
377             FT_UINT8, BASE_HEX, NULL, 0x00,
378             NULL, HFILL }
379         },
380         { &hf_geneve_option_flags_reserved,
381           { "Reserved", "geneve.option.flags.reserved",
382             FT_BOOLEAN, 8, NULL, 0xE0,
383             NULL, HFILL }
384         },
385         { &hf_geneve_option_length,
386           { "Length", "geneve.option.length",
387             FT_UINT8, BASE_DEC, NULL, 0x00,
388             NULL, HFILL }
389         },
390         { &hf_geneve_opt_unknown,
391           { "Unknown Option", "geneve.option.unknown",
392             FT_BYTES, BASE_NONE, NULL, 0x00,
393             NULL, HFILL }
394         },
395         { &hf_geneve_opt_unknown_data,
396           { "Option Data", "geneve.option.unknown.data",
397             FT_BYTES, BASE_NONE, NULL, 0x00,
398             NULL, HFILL }
399         },
400     };
401
402     static gint *ett[] = {
403         &ett_geneve,
404         &ett_geneve_flags,
405         &ett_geneve_options,
406         &ett_geneve_opt_flags,
407         &ett_geneve_unknown_opt,
408     };
409
410     static ei_register_info ei[] = {
411        { &ei_geneve_opt_len_invalid, { "geneve.option.length.invalid",
412          PI_SEQUENCE, PI_NOTE, "Invalid length for option", EXPFILL }},
413     };
414
415     expert_module_t *expert_geneve;
416
417     /* Register the protocol name and description */
418     proto_geneve = proto_register_protocol("Generic Network Virtualization Encapsulation",
419                                           "Geneve", "geneve");
420
421     proto_register_field_array(proto_geneve, hf, array_length(hf));
422     proto_register_subtree_array(ett, array_length(ett));
423
424     expert_geneve = expert_register_protocol(proto_geneve);
425     expert_register_field_array(expert_geneve, ei, array_length(ei));
426 }
427
428 void
429 proto_reg_handoff_geneve(void)
430 {
431     dissector_handle_t geneve_handle;
432
433     geneve_handle = create_dissector_handle(dissect_geneve, proto_geneve);
434     dissector_add_uint("udp.port", UDP_PORT_GENEVE, geneve_handle);
435     dissector_add_handle("udp.port", geneve_handle);  /* For 'Decode As' */
436
437     ethertype_dissector_table = find_dissector_table("ethertype");
438     data_handle = find_dissector("data");
439 }
440
441 /*
442  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
443  *
444  * Local variables:
445  * c-basic-offset: 4
446  * tab-width: 8
447  * indent-tabs-mode: nil
448  * End:
449  *
450  * vi: set shiftwidth=4 tabstop=8 expandtab:
451  * :indentSize=4:tabSize=8:noTabs=true:
452  */