]> The Tcpdump Group git mirrors - tcpdump/blob - print-geneve.c
Fix a bunch of de-constifications.
[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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <tcpdump-stdinc.h>
23
24 #include "interface.h"
25 #include "extract.h"
26 #include "ethertype.h"
27
28 /*
29 * Geneve header, draft-gross-geneve-02
30 *
31 * 0 1 2 3
32 * 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
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * |Ver| Opt Len |O|C| Rsvd. | Protocol Type |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | Virtual Network Identifier (VNI) | Reserved |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Variable Length Options |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 *
41 * Options:
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Option Class | Type |R|R|R| Length |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Variable Option Data |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 */
48
49 #define VER_SHIFT 6
50 #define HDR_OPTS_LEN_MASK 0x3F
51
52 #define FLAG_OAM (1 << 7)
53 #define FLAG_CRITICAL (1 << 6)
54 #define FLAG_R1 (1 << 5)
55 #define FLAG_R2 (1 << 4)
56 #define FLAG_R3 (1 << 3)
57 #define FLAG_R4 (1 << 2)
58 #define FLAG_R5 (1 << 1)
59 #define FLAG_R6 (1 << 0)
60
61 #define OPT_TYPE_CRITICAL (1 << 7)
62 #define OPT_LEN_MASK 0x1F
63
64 static const struct tok geneve_flag_values[] = {
65 { FLAG_OAM, "O" },
66 { FLAG_CRITICAL, "C" },
67 { FLAG_R1, "R1" },
68 { FLAG_R2, "R2" },
69 { FLAG_R3, "R3" },
70 { FLAG_R4, "R4" },
71 { FLAG_R5, "R5" },
72 { FLAG_R6, "R6" },
73 { 0, NULL }
74 };
75
76 static const char *
77 format_opt_class(uint16_t opt_class)
78 {
79 if (opt_class <= 0xff)
80 return "Standard";
81 else if (opt_class == 0xffff)
82 return "Experimental";
83 else
84 return "Unknown";
85 }
86
87 static void
88 geneve_opts_print(netdissect_options *ndo, const u_char *bp, u_int len)
89 {
90 const char *sep = "";
91
92 while (len > 0) {
93 uint16_t opt_class;
94 uint8_t opt_type;
95 uint8_t opt_len;
96
97 ND_PRINT((ndo, "%s", sep));
98 sep = ", ";
99
100 opt_class = EXTRACT_16BITS(bp);
101 opt_type = *(bp + 2);
102 opt_len = 4 + ((*(bp + 3) & OPT_LEN_MASK) * 4);
103
104 ND_PRINT((ndo, "class %s (0x%x) type 0x%x%s len %u",
105 format_opt_class(opt_class), opt_class, opt_type,
106 opt_type & OPT_TYPE_CRITICAL ? "(C)" : "", opt_len));
107
108 if (opt_len > len) {
109 ND_PRINT((ndo, " [bad length]"));
110 return;
111 }
112
113 if (ndo->ndo_vflag > 1 && opt_len > 4) {
114 const uint32_t *print_data = (const uint32_t *)(bp + 4);
115 int i;
116
117 ND_PRINT((ndo, " data"));
118
119 for (i = 4; i < opt_len; i += 4) {
120 ND_PRINT((ndo, " %08x", EXTRACT_32BITS(print_data)));
121 print_data++;
122 }
123 }
124
125 bp += opt_len;
126 len -= opt_len;
127 }
128 }
129
130 void
131 geneve_print(netdissect_options *ndo, const u_char *bp, u_int len)
132 {
133 uint8_t ver_opt;
134 uint version;
135 uint8_t flags;
136 uint16_t prot;
137 uint32_t vni;
138 uint8_t reserved;
139 u_int opts_len;
140
141 ND_PRINT((ndo, "Geneve"));
142
143 ND_TCHECK2(*bp, 8);
144
145 ver_opt = *bp;
146 bp += 1;
147 len -= 1;
148
149 version = ver_opt >> VER_SHIFT;
150 if (version != 0) {
151 ND_PRINT((ndo, " ERROR: unknown-version %u", version));
152 return;
153 }
154
155 flags = *bp;
156 bp += 1;
157 len -= 1;
158
159 prot = EXTRACT_16BITS(bp);
160 bp += 2;
161 len -= 2;
162
163 vni = EXTRACT_24BITS(bp);
164 bp += 3;
165 len -= 3;
166
167 reserved = *bp;
168 bp += 1;
169 len -= 1;
170
171 ND_PRINT((ndo, ", Flags [%s]",
172 bittok2str_nosep(geneve_flag_values, "none", flags)));
173 ND_PRINT((ndo, ", vni 0x%x", vni));
174
175 if (reserved)
176 ND_PRINT((ndo, ", rsvd 0x%x", reserved));
177
178 if (ndo->ndo_eflag)
179 ND_PRINT((ndo, ", proto %s (0x%04x)",
180 tok2str(ethertype_values, "unknown", prot), prot));
181
182 opts_len = (ver_opt & HDR_OPTS_LEN_MASK) * 4;
183
184 if (len < opts_len) {
185 ND_PRINT((ndo, " truncated-geneve - %u bytes missing",
186 len - opts_len));
187 return;
188 }
189
190 ND_TCHECK2(*bp, opts_len);
191
192 if (opts_len > 0) {
193 ND_PRINT((ndo, ", options ["));
194
195 if (ndo->ndo_vflag)
196 geneve_opts_print(ndo, bp, opts_len);
197 else
198 ND_PRINT((ndo, "%u bytes", opts_len));
199
200 ND_PRINT((ndo, "]"));
201 }
202
203 bp += opts_len;
204 len -= opts_len;
205
206 if (ndo->ndo_vflag < 1)
207 ND_PRINT((ndo, ": "));
208 else
209 ND_PRINT((ndo, "\n\t"));
210
211 if (ethertype_print(ndo, prot, bp, len, len) == 0) {
212 if (prot == ETHERTYPE_TEB)
213 ether_print(ndo, bp, len, len, NULL, NULL);
214 else
215 ND_PRINT((ndo, "geneve-proto-0x%x", prot));
216 }
217
218 return;
219
220 trunc:
221 ND_PRINT((ndo, " [|geneve]"));
222 }