Geneve: Update list of option classes.
[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-ietf-nvo3-geneve
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/packet.h>
31 #include <epan/etypes.h>
32 #include <epan/expert.h>
33
34 #define UDP_PORT_GENEVE  6081
35 #define GENEVE_VER 0
36
37 #define VER_SHIFT 6
38 #define HDR_OPTS_LEN_MASK 0x3F
39
40 #define FLAG_OAM (1 << 7)
41
42 #define OPT_TYPE_CRITICAL (1 << 7)
43 #define OPT_FLAGS_SHIFT 5
44 #define OPT_LEN_MASK 0x1F
45
46 static const range_string class_id_names[] = {
47     { 0, 0xFF, "Standard" },
48     { 0x0100, 0x0100, "Linux" },
49     { 0x0101, 0x0101, "Open vSwitch" },
50     { 0x0102, 0x0102, "Open Virtual Networking (OVN)" },
51     { 0x0103, 0x0103, "In-band Network Telemetry (INT)" },
52     { 0x0104, 0x0104, "VMware" },
53     { 0xFFF0, 0xFFFF, "Experimental" },
54     { 0, 0, NULL }
55 };
56
57 void proto_register_geneve(void);
58 void proto_reg_handoff_geneve(void);
59
60 static int proto_geneve = -1;
61
62 static int hf_geneve_version = -1;
63 static int hf_geneve_opt_len = -1;
64 static int hf_geneve_flags = -1;
65 static int hf_geneve_flag_oam = -1;
66 static int hf_geneve_flag_critical = -1;
67 static int hf_geneve_flag_reserved = -1;
68 static int hf_geneve_proto_type = -1;
69 static int hf_geneve_vni = -1;
70 static int hf_geneve_reserved = -1;
71 static int hf_geneve_options = -1;
72 static int hf_geneve_option_class = -1;
73 static int hf_geneve_option_type = -1;
74 static int hf_geneve_option_type_critical = -1;
75 static int hf_geneve_option_flags = -1;
76 static int hf_geneve_option_flags_reserved = -1;
77 static int hf_geneve_option_length = -1;
78 static int hf_geneve_opt_unknown = -1;
79 static int hf_geneve_opt_unknown_data = -1;
80
81 static int ett_geneve = -1;
82 static int ett_geneve_flags = -1;
83 static int ett_geneve_opt_flags = -1;
84 static int ett_geneve_options = -1;
85 static int ett_geneve_unknown_opt = -1;
86
87 static expert_field ei_geneve_ver_unknown = EI_INIT;
88 static expert_field ei_geneve_opt_len_invalid = EI_INIT;
89
90 static dissector_table_t ethertype_dissector_table;
91
92 static void
93 print_flags(guint8 flags, proto_item *flag_item)
94 {
95     static const char flag_names[][5] = {"OAM", "CRIT"};
96     unsigned int i;
97
98     if (!flags) {
99         return;
100     }
101
102     proto_item_append_text(flag_item, " (");
103
104     for (i = 0; i < array_length(flag_names); i++) {
105         guint8 bit = 1 << (7 - i);
106
107         if (flags & bit) {
108             proto_item_append_text(flag_item, "%s", flag_names[i]);
109             flags &= ~bit;
110
111             if (flags) {
112                 proto_item_append_text(flag_item, ", ");
113             }
114         }
115     }
116
117     if (flags) {
118         proto_item_append_text(flag_item, "RSVD");
119     }
120
121     proto_item_append_text(flag_item, ")");
122 }
123
124 static const char *
125 format_unknown_option_name(guint16 opt_class, guint8 opt_type)
126 {
127     const char *name;
128
129     name = wmem_strdup_printf(wmem_packet_scope(),
130                               "Unknown, Class: %s (0x%04x) Type: 0x%02x",
131                               rval_to_str_const(opt_class, class_id_names, "Unknown"),
132                               opt_class, opt_type);
133
134     return name;
135 }
136
137 static void
138 dissect_unknown_option(tvbuff_t *tvb, proto_tree *opts_tree, int offset,
139                        guint16 opt_class, guint8 opt_type, int len)
140 {
141     proto_item *opt_item, *type_item, *hidden_item, *flag_item;
142     proto_tree *opt_tree, *flag_tree;
143     const char *critical;
144     guint8 flags;
145
146     critical = opt_type & OPT_TYPE_CRITICAL ? "Critical" : "Non-critical";
147
148     opt_item = proto_tree_add_item(opts_tree, hf_geneve_opt_unknown,
149                                    tvb, offset, len, ENC_NA);
150     proto_item_set_text(opt_item, "%s (%s)",
151                         format_unknown_option_name(opt_class, opt_type),
152                         critical);
153
154     opt_tree = proto_item_add_subtree(opt_item, ett_geneve_unknown_opt);
155
156     proto_tree_add_item(opt_tree, hf_geneve_option_class, tvb,
157                         offset, 2, ENC_BIG_ENDIAN);
158     offset += 2;
159
160     type_item = proto_tree_add_item(opt_tree, hf_geneve_option_type, tvb,
161                                     offset, 1, ENC_BIG_ENDIAN);
162     proto_item_append_text(type_item, " (%s)", critical);
163     hidden_item = proto_tree_add_item(opt_tree, hf_geneve_option_type_critical,
164                                       tvb, offset, 1, ENC_BIG_ENDIAN);
165     PROTO_ITEM_SET_HIDDEN(hidden_item);
166     offset += 1;
167
168     flags = tvb_get_guint8(tvb, offset) >> OPT_FLAGS_SHIFT;
169     flag_item = proto_tree_add_uint(opt_tree, hf_geneve_option_flags, tvb,
170                                     offset, 1, flags);
171     flag_tree = proto_item_add_subtree(flag_item, ett_geneve_opt_flags);
172     proto_tree_add_item(flag_tree, hf_geneve_option_flags_reserved, tvb,
173                         offset, 1, ENC_BIG_ENDIAN);
174     if (flags) {
175         proto_item_append_text(flag_item, " (RSVD)");
176     } else {
177         PROTO_ITEM_SET_HIDDEN(flag_item);
178     }
179
180     proto_tree_add_uint_format_value(opt_tree, hf_geneve_option_length, tvb,
181                                      offset, 1, len, "%u bytes", len);
182     offset += 1;
183
184     proto_tree_add_item(opt_tree, hf_geneve_opt_unknown_data, tvb, offset,
185                         len - 4, ENC_NA);
186 }
187
188 static void
189 dissect_geneve_options(tvbuff_t *tvb, packet_info *pinfo,
190                        proto_tree *geneve_tree, int offset, int len)
191 {
192     proto_item *opts_item;
193     proto_tree *opts_tree;
194     guint16 opt_class;
195     guint8 opt_type;
196     guint8 opt_len;
197
198     opts_item = proto_tree_add_item(geneve_tree, hf_geneve_options, tvb,
199                                     offset, len, ENC_NA);
200     proto_item_set_text(opts_item, "Options: (%u bytes)", len);
201     opts_tree = proto_item_add_subtree(opts_item, ett_geneve_options);
202
203     while (len > 0) {
204         opt_class = tvb_get_ntohs(tvb, offset);
205         opt_type = tvb_get_guint8(tvb, offset + 2);
206         opt_len = 4 + ((tvb_get_guint8(tvb, offset + 3) & OPT_LEN_MASK) * 4);
207
208         if (opt_len > len) {
209             proto_tree_add_expert_format(opts_tree, pinfo,
210                                          &ei_geneve_opt_len_invalid, tvb,
211                                          offset + 3, 1,
212                                          "%s (length of %u is past end of options)",
213                                          format_unknown_option_name(opt_class,
214                                                                     opt_type),
215                                          opt_len);
216             return;
217         }
218
219         dissect_unknown_option(tvb, opts_tree, offset,
220                                opt_class, opt_type, opt_len);
221
222         offset += opt_len;
223         len -= opt_len;
224     };
225 }
226
227 static int
228 dissect_geneve(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
229 {
230     proto_item *ti, *flag_item, *rsvd_item;
231     proto_tree *geneve_tree, *flag_tree;
232     tvbuff_t *next_tvb;
233     int offset = 0;
234     guint8 ver_opt;
235     guint8 ver;
236     guint8 flags;
237     guint16 proto_type;
238     int opts_len;
239
240     col_set_str(pinfo->cinfo, COL_PROTOCOL, "Geneve");
241     col_clear(pinfo->cinfo, COL_INFO);
242
243     ti = proto_tree_add_item(tree, proto_geneve, tvb, offset, -1, ENC_NA);
244     geneve_tree = proto_item_add_subtree(ti, ett_geneve);
245
246     /* Version. */
247     ver_opt = tvb_get_guint8(tvb, offset);
248     ver = ver_opt >> VER_SHIFT;
249     proto_tree_add_uint(geneve_tree, hf_geneve_version, tvb,
250                         offset, 1, ver);
251
252     if (ver != GENEVE_VER) {
253         proto_tree_add_expert_format(geneve_tree, pinfo,
254                                      &ei_geneve_ver_unknown, tvb, offset, 1,
255                                      "Unknown version %u", ver);
256         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown Geneve version %u", ver);
257     }
258
259     /* Option length. */
260     opts_len = (ver_opt & HDR_OPTS_LEN_MASK) * 4;
261     proto_tree_add_uint_format_value(geneve_tree, hf_geneve_opt_len, tvb,
262                                      offset, 1, opts_len, "%u bytes", opts_len);
263     offset += 1;
264
265     /* Flags. */
266     flags = tvb_get_guint8(tvb, offset);
267     if (tree) {
268         flag_item = proto_tree_add_item(geneve_tree, hf_geneve_flags, tvb,
269                                        offset, 1, ENC_BIG_ENDIAN);
270         print_flags(flags, flag_item);
271
272         flag_tree = proto_item_add_subtree(flag_item, ett_geneve_flags);
273
274         proto_tree_add_item(flag_tree, hf_geneve_flag_oam, tvb, offset,
275                             1, ENC_BIG_ENDIAN);
276         proto_tree_add_item(flag_tree, hf_geneve_flag_critical, tvb, offset,
277                             1, ENC_BIG_ENDIAN);
278         proto_tree_add_item(flag_tree, hf_geneve_flag_reserved, tvb, offset,
279                             1, ENC_BIG_ENDIAN);
280     }
281     offset += 1;
282
283     /* Protocol Type. */
284     proto_tree_add_item(geneve_tree, hf_geneve_proto_type, tvb,
285                         offset, 2, ENC_BIG_ENDIAN);
286
287     proto_type = tvb_get_ntohs(tvb, offset);
288     col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
289                  val_to_str(proto_type, etype_vals, "0x%04x (unknown)"));
290
291     offset += 2;
292
293     /* VNI. */
294     proto_tree_add_item(geneve_tree, hf_geneve_vni, tvb, offset, 3,
295                         ENC_BIG_ENDIAN);
296     proto_item_append_text(ti, ", VNI: 0x%06x%s", tvb_get_ntoh24(tvb, offset),
297                            flags & FLAG_OAM ? ", OAM" : "");
298     offset += 3;
299
300     /* Reserved. */
301     rsvd_item = proto_tree_add_item(geneve_tree, hf_geneve_reserved, tvb,
302                                     offset, 1, ENC_BIG_ENDIAN);
303     if (!tvb_get_guint8(tvb, offset)) {
304         PROTO_ITEM_SET_HIDDEN(rsvd_item);
305     }
306     offset += 1;
307
308     /* Options. */
309     if (tree && opts_len) {
310         dissect_geneve_options(tvb, pinfo, geneve_tree, offset, opts_len);
311     }
312     offset += opts_len;
313
314     proto_item_set_len(ti, offset);
315
316     next_tvb = tvb_new_subset_remaining(tvb, offset);
317     if (!dissector_try_uint(ethertype_dissector_table, proto_type, next_tvb, pinfo, tree))
318         call_data_dissector(next_tvb, pinfo, tree);
319
320     return tvb_captured_length(tvb);
321 }
322
323 /* Register Geneve with Wireshark */
324 void
325 proto_register_geneve(void)
326 {
327     static hf_register_info hf[] = {
328         { &hf_geneve_version,
329           { "Version", "geneve.version",
330             FT_UINT8, BASE_DEC, NULL, 0x00,
331             NULL, HFILL }
332         },
333         { &hf_geneve_opt_len,
334           { "Options Length", "geneve.options_length",
335             FT_UINT8, BASE_DEC, NULL, 0x0,
336             NULL, HFILL }
337         },
338         { &hf_geneve_flags,
339           { "Flags", "geneve.flags",
340             FT_UINT8, BASE_HEX, NULL, 0x00,
341             NULL, HFILL }
342         },
343         { &hf_geneve_flag_oam,
344           { "Operations, Administration and Management Frame", "geneve.flags.oam",
345             FT_BOOLEAN, 8, NULL, 0x80,
346             NULL, HFILL }
347         },
348         { &hf_geneve_flag_critical,
349           { "Critical Options Present", "geneve.flags.critical",
350             FT_BOOLEAN, 8, NULL, 0x40,
351             NULL, HFILL }
352         },
353         { &hf_geneve_flag_reserved,
354           { "Reserved", "geneve.flags.reserved",
355             FT_BOOLEAN, 8, NULL, 0x3F,
356             NULL, HFILL }
357         },
358         { &hf_geneve_proto_type,
359           { "Protocol Type", "geneve.proto_type",
360             FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
361             NULL, HFILL }
362         },
363         { &hf_geneve_vni,
364           { "Virtual Network Identifier (VNI)", "geneve.vni",
365             FT_UINT24, BASE_HEX, NULL, 0x0,
366             NULL, HFILL }
367         },
368         { &hf_geneve_reserved,
369           { "Reserved", "geneve.reserved",
370             FT_UINT8, BASE_HEX, NULL, 0x00,
371             NULL, HFILL }
372         },
373         { &hf_geneve_options,
374           { "Geneve Options", "geneve.options",
375             FT_BYTES, BASE_NONE, NULL, 0x00,
376             NULL, HFILL }
377         },
378         { &hf_geneve_option_class,
379           { "Class", "geneve.option.class",
380             FT_UINT16, BASE_HEX | BASE_RANGE_STRING, RVALS(class_id_names), 0x00,
381             NULL, HFILL }
382         },
383         { &hf_geneve_option_type,
384           { "Type", "geneve.option.type",
385             FT_UINT8, BASE_HEX, NULL, 0x00,
386             NULL, HFILL }
387         },
388         { &hf_geneve_option_type_critical,
389           { "Critical Option", "geneve.option.type.critical",
390             FT_BOOLEAN, 8, NULL, 0x80,
391             NULL, HFILL }
392         },
393         { &hf_geneve_option_flags,
394           { "Flags", "geneve.option.flags",
395             FT_UINT8, BASE_HEX, NULL, 0x00,
396             NULL, HFILL }
397         },
398         { &hf_geneve_option_flags_reserved,
399           { "Reserved", "geneve.option.flags.reserved",
400             FT_BOOLEAN, 8, NULL, 0xE0,
401             NULL, HFILL }
402         },
403         { &hf_geneve_option_length,
404           { "Length", "geneve.option.length",
405             FT_UINT8, BASE_DEC, NULL, 0x00,
406             NULL, HFILL }
407         },
408         { &hf_geneve_opt_unknown,
409           { "Unknown Option", "geneve.option.unknown",
410             FT_BYTES, BASE_NONE, NULL, 0x00,
411             NULL, HFILL }
412         },
413         { &hf_geneve_opt_unknown_data,
414           { "Option Data", "geneve.option.unknown.data",
415             FT_BYTES, BASE_NONE, NULL, 0x00,
416             NULL, HFILL }
417         },
418     };
419
420     static gint *ett[] = {
421         &ett_geneve,
422         &ett_geneve_flags,
423         &ett_geneve_options,
424         &ett_geneve_opt_flags,
425         &ett_geneve_unknown_opt,
426     };
427
428     static ei_register_info ei[] = {
429        { &ei_geneve_ver_unknown, { "geneve.version.unknown",
430          PI_PROTOCOL, PI_WARN, "Unknown version", EXPFILL }},
431        { &ei_geneve_opt_len_invalid, { "geneve.option.length.invalid",
432          PI_PROTOCOL, PI_WARN, "Invalid length for option", EXPFILL }},
433     };
434
435     expert_module_t *expert_geneve;
436
437     /* Register the protocol name and description */
438     proto_geneve = proto_register_protocol("Generic Network Virtualization Encapsulation",
439                                           "Geneve", "geneve");
440
441     proto_register_field_array(proto_geneve, hf, array_length(hf));
442     proto_register_subtree_array(ett, array_length(ett));
443
444     expert_geneve = expert_register_protocol(proto_geneve);
445     expert_register_field_array(expert_geneve, ei, array_length(ei));
446 }
447
448 void
449 proto_reg_handoff_geneve(void)
450 {
451     dissector_handle_t geneve_handle;
452
453     geneve_handle = create_dissector_handle(dissect_geneve, proto_geneve);
454     dissector_add_uint("udp.port", UDP_PORT_GENEVE, geneve_handle);
455     dissector_add_for_decode_as("udp.port", geneve_handle);
456
457     ethertype_dissector_table = find_dissector_table("ethertype");
458 }
459
460 /*
461  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
462  *
463  * Local variables:
464  * c-basic-offset: 4
465  * tab-width: 8
466  * indent-tabs-mode: nil
467  * End:
468  *
469  * vi: set shiftwidth=4 tabstop=8 expandtab:
470  * :indentSize=4:tabSize=8:noTabs=true:
471  */