]> The Tcpdump Group git mirrors - tcpdump/blob - print-geneve.c
59dca93e047823de2ef2906decc6e7a314880a56
[tcpdump] / print-geneve.c
1 /*
2 * Copyright (c) 2014 VMware, Inc. All Rights Reserved.
3 *
4 * Jesse Gross <jesse@nicira.com>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code
8 * distributions retain the above copyright notice and this paragraph
9 * in its entirety, and (2) distributions including binary code include
10 * the above copyright notice and this paragraph in its entirety in
11 * the documentation or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
13 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
14 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 * FOR A PARTICULAR PURPOSE.
16 */
17
18 /* \summary: Generic Network Virtualization Encapsulation (Geneve) printer */
19 /* specification: RFC 8926 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "netdissect-stdinc.h"
26
27 #define ND_LONGJMP_FROM_TCHECK
28 #include "netdissect.h"
29 #include "extract.h"
30 #include "ethertype.h"
31
32 /*
33 * Geneve header:
34 *
35 * 0 1 2 3
36 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * |Ver| Opt Len |O|C| Rsvd. | Protocol Type |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Virtual Network Identifier (VNI) | Reserved |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | |
43 * ~ Variable-Length Options ~
44 * | |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 *
47 * Options:
48 * 0 1 2 3
49 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Option Class | Type |R|R|R| Length |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | |
54 * ~ Variable-Length Option Data ~
55 * | |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 */
58
59 #define VER_SHIFT 6
60 #define HDR_OPTS_LEN_MASK 0x3F
61
62 #define FLAG_OAM (1 << 7)
63 #define FLAG_CRITICAL (1 << 6)
64 #define FLAG_R1 (1 << 5)
65 #define FLAG_R2 (1 << 4)
66 #define FLAG_R3 (1 << 3)
67 #define FLAG_R4 (1 << 2)
68 #define FLAG_R5 (1 << 1)
69 #define FLAG_R6 (1 << 0)
70
71 #define OPT_TYPE_CRITICAL (1 << 7)
72 #define OPT_LEN_MASK 0x1F
73
74 static const struct tok geneve_flag_values[] = {
75 { FLAG_OAM, "O" },
76 { FLAG_CRITICAL, "C" },
77 { FLAG_R1, "R1" },
78 { FLAG_R2, "R2" },
79 { FLAG_R3, "R3" },
80 { FLAG_R4, "R4" },
81 { FLAG_R5, "R5" },
82 { FLAG_R6, "R6" },
83 { 0, NULL }
84 };
85
86 static const char *
87 format_opt_class(const uint16_t opt_class)
88 {
89 switch (opt_class) {
90 case 0x0100:
91 return "Linux";
92 case 0x0101:
93 return "Open vSwitch";
94 case 0x0102:
95 return "Open Virtual Networking (OVN)";
96 case 0x0103:
97 return "In-band Network Telemetry (INT)";
98 case 0x0104:
99 return "VMware";
100 case 0x0105:
101 case 0x0108:
102 case 0x0109:
103 case 0x010A:
104 case 0x010B:
105 case 0x010C:
106 case 0x010D:
107 case 0x010E:
108 case 0x010F:
109 case 0x0110:
110 return "Amazon";
111 case 0x0106:
112 case 0x0130:
113 case 0x0131:
114 return "Cisco";
115 case 0x0107:
116 return "Oracle";
117 case 0x0111:
118 case 0x0112:
119 case 0x0113:
120 case 0x0114:
121 case 0x0115:
122 case 0x0116:
123 case 0x0117:
124 case 0x0118:
125 return "IBM";
126 case 0x0119:
127 case 0x011A:
128 case 0x011B:
129 case 0x011C:
130 case 0x011D:
131 case 0x011E:
132 case 0x011F:
133 case 0x0120:
134 case 0x0121:
135 case 0x0122:
136 case 0x0123:
137 case 0x0124:
138 case 0x0125:
139 case 0x0126:
140 case 0x0127:
141 case 0x0128:
142 return "Ericsson";
143 case 0x0129:
144 return "Oxide";
145 case 0x0132:
146 case 0x0133:
147 case 0x0134:
148 case 0x0135:
149 return "Google";
150 case 0x0136:
151 return "InfoQuick";
152 default:
153 if (opt_class <= 0x00ff)
154 return "Standard";
155 else if (opt_class >= 0xfff0)
156 return "Experimental";
157 }
158
159 return "Unknown";
160 }
161
162 static unsigned
163 geneve_opts_print(netdissect_options *ndo, const u_char *bp, u_int len)
164 {
165 const char *sep = "";
166
167 while (len > 0) {
168 uint16_t opt_class;
169 uint8_t opt_type;
170 uint8_t opt_len;
171
172 ND_ICHECKMSG_U("remaining options length", len, <, 4);
173 ND_PRINT("%s", sep);
174 sep = ", ";
175
176 opt_class = GET_BE_U_2(bp);
177 opt_type = GET_U_1(bp + 2);
178 opt_len = 4 + ((GET_U_1(bp + 3) & OPT_LEN_MASK) * 4);
179
180 ND_PRINT("class %s (0x%x) type 0x%x%s len %u",
181 format_opt_class(opt_class), opt_class, opt_type,
182 opt_type & OPT_TYPE_CRITICAL ? "(C)" : "", opt_len);
183
184 if (opt_len > len) {
185 ND_PRINT(" [bad length]");
186 goto invalid;
187 }
188
189 if (ndo->ndo_vflag > 1 && opt_len > 4) {
190 const uint32_t *data = (const uint32_t *)(bp + 4);
191 int i;
192
193 ND_PRINT(" data");
194
195 for (i = 4; i < opt_len; i += 4) {
196 ND_PRINT(" %08x", GET_BE_U_4(data));
197 data++;
198 }
199 } else
200 ND_TCHECK_LEN(bp, opt_len);
201
202 bp += opt_len;
203 len -= opt_len;
204 }
205 return 1;
206 invalid:
207 ND_TCHECK_LEN(bp, len);
208 return 0;
209 }
210
211 void
212 geneve_print(netdissect_options *ndo, const u_char *bp, u_int len)
213 {
214 uint8_t ver_opt;
215 u_int version;
216 uint8_t flags;
217 uint16_t prot;
218 uint32_t vni;
219 uint8_t reserved;
220 u_int opts_len;
221
222 ndo->ndo_protocol = "geneve";
223 ND_PRINT("Geneve");
224
225 ND_ICHECK_U(len, <, 8);
226
227 ver_opt = GET_U_1(bp);
228 bp += 1;
229 len -= 1;
230
231 version = ver_opt >> VER_SHIFT;
232 if (version != 0) {
233 ND_PRINT(" ERROR: unknown-version %u", version);
234 goto invalid;
235 }
236
237 flags = GET_U_1(bp);
238 bp += 1;
239 len -= 1;
240
241 prot = GET_BE_U_2(bp);
242 bp += 2;
243 len -= 2;
244
245 vni = GET_BE_U_3(bp);
246 bp += 3;
247 len -= 3;
248
249 reserved = GET_U_1(bp);
250 bp += 1;
251 len -= 1;
252
253 ND_PRINT(", Flags [%s]",
254 bittok2str_nosep(geneve_flag_values, "none", flags));
255 ND_PRINT(", vni 0x%x", vni);
256
257 if (reserved)
258 ND_PRINT(", rsvd 0x%x", reserved);
259
260 if (ndo->ndo_eflag)
261 ND_PRINT(", proto %s (0x%04x)",
262 tok2str(ethertype_values, "unknown", prot), prot);
263
264 opts_len = (ver_opt & HDR_OPTS_LEN_MASK) * 4;
265
266 if (len < opts_len) {
267 ND_PRINT(" (opts_len %u > %u", opts_len, len);
268 goto invalid;
269 }
270
271 if (opts_len > 0) {
272 ND_PRINT(", options [");
273
274 if (ndo->ndo_vflag) {
275 if (! geneve_opts_print(ndo, bp, opts_len))
276 goto invalid;
277 } else {
278 ND_TCHECK_LEN(bp, opts_len);
279 ND_PRINT("%u bytes", opts_len);
280 }
281
282 ND_PRINT("]");
283 }
284
285 bp += opts_len;
286 len -= opts_len;
287
288 if (ndo->ndo_vflag < 1)
289 ND_PRINT(": ");
290 else
291 ND_PRINT("\n\t");
292
293 if (ethertype_print(ndo, prot, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL) == 0) {
294 if (prot == ETHERTYPE_TEB)
295 ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
296 else {
297 ND_PRINT("geneve-proto-0x%x", prot);
298 ND_TCHECK_LEN(bp, len);
299 }
300 }
301
302 return;
303
304 invalid:
305 nd_print_invalid(ndo);
306 }