]> The Tcpdump Group git mirrors - tcpdump/blob - print-geneve.c
b58666cc5b73df44d1c78cd31b1f6cab5305c597
[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 default:
101 if (opt_class <= 0x00ff)
102 return "Standard";
103 else if (opt_class >= 0xfff0)
104 return "Experimental";
105 }
106
107 return "Unknown";
108 }
109
110 static unsigned
111 geneve_opts_print(netdissect_options *ndo, const u_char *bp, u_int len)
112 {
113 const char *sep = "";
114
115 while (len > 0) {
116 uint16_t opt_class;
117 uint8_t opt_type;
118 uint8_t opt_len;
119
120 if (len < 4) {
121 ND_PRINT(" (remaining options length %u < 4)", len);
122 goto invalid;
123 }
124 ND_PRINT("%s", sep);
125 sep = ", ";
126
127 opt_class = GET_BE_U_2(bp);
128 opt_type = GET_U_1(bp + 2);
129 opt_len = 4 + ((GET_U_1(bp + 3) & OPT_LEN_MASK) * 4);
130
131 ND_PRINT("class %s (0x%x) type 0x%x%s len %u",
132 format_opt_class(opt_class), opt_class, opt_type,
133 opt_type & OPT_TYPE_CRITICAL ? "(C)" : "", opt_len);
134
135 if (opt_len > len) {
136 ND_PRINT(" [bad length]");
137 goto invalid;
138 }
139
140 if (ndo->ndo_vflag > 1 && opt_len > 4) {
141 const uint32_t *data = (const uint32_t *)(bp + 4);
142 int i;
143
144 ND_PRINT(" data");
145
146 for (i = 4; i < opt_len; i += 4) {
147 ND_PRINT(" %08x", GET_BE_U_4(data));
148 data++;
149 }
150 }
151
152 bp += opt_len;
153 len -= opt_len;
154 }
155 return 1;
156 invalid:
157 ND_TCHECK_LEN(bp, len);
158 return 0;
159 }
160
161 void
162 geneve_print(netdissect_options *ndo, const u_char *bp, u_int len)
163 {
164 uint8_t ver_opt;
165 u_int version;
166 uint8_t flags;
167 uint16_t prot;
168 uint32_t vni;
169 uint8_t reserved;
170 u_int opts_len;
171
172 ndo->ndo_protocol = "geneve";
173 ND_PRINT("Geneve");
174
175 if (len < 8) {
176 ND_PRINT(" [length %u < 8]", len);
177 goto invalid;
178 }
179
180 ver_opt = GET_U_1(bp);
181 bp += 1;
182 len -= 1;
183
184 version = ver_opt >> VER_SHIFT;
185 if (version != 0) {
186 ND_PRINT(" ERROR: unknown-version %u", version);
187 goto invalid;
188 }
189
190 flags = GET_U_1(bp);
191 bp += 1;
192 len -= 1;
193
194 prot = GET_BE_U_2(bp);
195 bp += 2;
196 len -= 2;
197
198 vni = GET_BE_U_3(bp);
199 bp += 3;
200 len -= 3;
201
202 reserved = GET_U_1(bp);
203 bp += 1;
204 len -= 1;
205
206 ND_PRINT(", Flags [%s]",
207 bittok2str_nosep(geneve_flag_values, "none", flags));
208 ND_PRINT(", vni 0x%x", vni);
209
210 if (reserved)
211 ND_PRINT(", rsvd 0x%x", reserved);
212
213 if (ndo->ndo_eflag)
214 ND_PRINT(", proto %s (0x%04x)",
215 tok2str(ethertype_values, "unknown", prot), prot);
216
217 opts_len = (ver_opt & HDR_OPTS_LEN_MASK) * 4;
218
219 if (len < opts_len) {
220 ND_PRINT(" (opts_len %u > %u", opts_len, len);
221 goto invalid;
222 }
223
224 if (opts_len > 0) {
225 ND_PRINT(", options [");
226
227 if (ndo->ndo_vflag) {
228 if (! geneve_opts_print(ndo, bp, opts_len))
229 goto invalid;
230 }
231 else {
232 ND_TCHECK_LEN(bp, opts_len);
233 ND_PRINT("%u bytes", opts_len);
234 }
235
236 ND_PRINT("]");
237 }
238
239 bp += opts_len;
240 len -= opts_len;
241
242 if (ndo->ndo_vflag < 1)
243 ND_PRINT(": ");
244 else
245 ND_PRINT("\n\t");
246
247 if (ethertype_print(ndo, prot, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL) == 0) {
248 if (prot == ETHERTYPE_TEB)
249 ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
250 else {
251 ND_PRINT("geneve-proto-0x%x", prot);
252 ND_TCHECK_LEN(bp, len);
253 }
254 }
255
256 return;
257
258 invalid:
259 nd_print_invalid(ndo);
260 }