]> The Tcpdump Group git mirrors - tcpdump/blob - print-aoe.c
Printers must include 'netdissect.h', not 'interface.h'
[tcpdump] / print-aoe.c
1 /*
2 * This module implements decoding of the ATA over Ethernet (AoE) protocol
3 * according to the following specification:
4 * http://support.coraid.com/documents/AoEr11.txt
5 *
6 * Copyright (c) 2014 The TCPDUMP project
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <tcpdump-stdinc.h>
37
38 #include "netdissect.h"
39 #include "extract.h"
40 #include "addrtoname.h"
41 #include "ether.h"
42
43 static const char tstr[] = " [|aoe]";
44 static const char cstr[] = " (corrupt)";
45
46 #define AOE_V1 1
47 #define ATA_SECTOR_SIZE 512
48
49 #define AOEV1_CMD_ISSUE_ATA_COMMAND 0
50 #define AOEV1_CMD_QUERY_CONFIG_INFORMATION 1
51 #define AOEV1_CMD_MAC_MASK_LIST 2
52 #define AOEV1_CMD_RESERVE_RELEASE 3
53
54 static const struct tok cmdcode_str[] = {
55 { AOEV1_CMD_ISSUE_ATA_COMMAND, "Issue ATA Command" },
56 { AOEV1_CMD_QUERY_CONFIG_INFORMATION, "Query Config Information" },
57 { AOEV1_CMD_MAC_MASK_LIST, "MAC Mask List" },
58 { AOEV1_CMD_RESERVE_RELEASE, "Reserve/Release" },
59 { 0, NULL }
60 };
61
62 #define AOEV1_COMMON_HDR_LEN 10U /* up to but w/o Arg */
63 #define AOEV1_ISSUE_ARG_LEN 12U /* up to but w/o Data */
64 #define AOEV1_QUERY_ARG_LEN 8U /* up to but w/o Config String */
65 #define AOEV1_MAC_ARG_LEN 4U /* up to but w/o Directive 0 */
66 #define AOEV1_RESERVE_ARG_LEN 2U /* up to but w/o Ethernet address 0 */
67 #define AOEV1_MAX_CONFSTR_LEN 1024U
68
69 #define AOEV1_FLAG_R 0x08
70 #define AOEV1_FLAG_E 0x04
71
72 static const struct tok aoev1_flag_str[] = {
73 { AOEV1_FLAG_R, "Response" },
74 { AOEV1_FLAG_E, "Error" },
75 { 0x02, "MBZ-0x02" },
76 { 0x01, "MBZ-0x01" },
77 { 0, NULL }
78 };
79
80 static const struct tok aoev1_errcode_str[] = {
81 { 1, "Unrecognized command code" },
82 { 2, "Bad argument parameter" },
83 { 3, "Device unavailable" },
84 { 4, "Config string present" },
85 { 5, "Unsupported version" },
86 { 6, "Target is reserved" },
87 { 0, NULL }
88 };
89
90 #define AOEV1_AFLAG_E 0x40
91 #define AOEV1_AFLAG_D 0x10
92 #define AOEV1_AFLAG_A 0x02
93 #define AOEV1_AFLAG_W 0x01
94
95 static const struct tok aoev1_aflag_str[] = {
96 { 0x08, "MBZ-0x08" },
97 { AOEV1_AFLAG_E, "Ext48" },
98 { 0x06, "MBZ-0x06" },
99 { AOEV1_AFLAG_D, "Device" },
100 { 0x04, "MBZ-0x04" },
101 { 0x03, "MBZ-0x03" },
102 { AOEV1_AFLAG_A, "Async" },
103 { AOEV1_AFLAG_W, "Write" },
104 { 0, NULL }
105 };
106
107 static const struct tok aoev1_ccmd_str[] = {
108 { 0, "read config string" },
109 { 1, "test config string" },
110 { 2, "test config string prefix" },
111 { 3, "set config string" },
112 { 4, "force set config string" },
113 { 0, NULL }
114 };
115
116 static const struct tok aoev1_mcmd_str[] = {
117 { 0, "Read Mac Mask List" },
118 { 1, "Edit Mac Mask List" },
119 { 0, NULL }
120 };
121
122 static const struct tok aoev1_merror_str[] = {
123 { 1, "Unspecified Error" },
124 { 2, "Bad DCmd directive" },
125 { 3, "Mask list full" },
126 { 0, NULL }
127 };
128
129 static const struct tok aoev1_dcmd_str[] = {
130 { 0, "No Directive" },
131 { 1, "Add mac address to mask list" },
132 { 2, "Delete mac address from mask list" },
133 { 0, NULL }
134 };
135
136 static const struct tok aoev1_rcmd_str[] = {
137 { 0, "Read reserve list" },
138 { 1, "Set reserve list" },
139 { 2, "Force set reserve list" },
140 { 0, NULL }
141 };
142
143 static void
144 aoev1_issue_print(netdissect_options *ndo,
145 const u_char *cp, const u_int len)
146 {
147 const u_char *ep = cp + len;
148
149 if (len < AOEV1_ISSUE_ARG_LEN)
150 goto corrupt;
151 /* AFlags */
152 ND_TCHECK2(*cp, 1);
153 ND_PRINT((ndo, "\n\tAFlags: [%s]", bittok2str(aoev1_aflag_str, "none", *cp)));
154 cp += 1;
155 /* Err/Feature */
156 ND_TCHECK2(*cp, 1);
157 ND_PRINT((ndo, ", Err/Feature: %u", *cp));
158 cp += 1;
159 /* Sector Count (not correlated with the length) */
160 ND_TCHECK2(*cp, 1);
161 ND_PRINT((ndo, ", Sector Count: %u", *cp));
162 cp += 1;
163 /* Cmd/Status */
164 ND_TCHECK2(*cp, 1);
165 ND_PRINT((ndo, ", Cmd/Status: %u", *cp));
166 cp += 1;
167 /* lba0 */
168 ND_TCHECK2(*cp, 1);
169 ND_PRINT((ndo, "\n\tlba0: %u", *cp));
170 cp += 1;
171 /* lba1 */
172 ND_TCHECK2(*cp, 1);
173 ND_PRINT((ndo, ", lba1: %u", *cp));
174 cp += 1;
175 /* lba2 */
176 ND_TCHECK2(*cp, 1);
177 ND_PRINT((ndo, ", lba2: %u", *cp));
178 cp += 1;
179 /* lba3 */
180 ND_TCHECK2(*cp, 1);
181 ND_PRINT((ndo, ", lba3: %u", *cp));
182 cp += 1;
183 /* lba4 */
184 ND_TCHECK2(*cp, 1);
185 ND_PRINT((ndo, ", lba4: %u", *cp));
186 cp += 1;
187 /* lba5 */
188 ND_TCHECK2(*cp, 1);
189 ND_PRINT((ndo, ", lba5: %u", *cp));
190 cp += 1;
191 /* Reserved */
192 ND_TCHECK2(*cp, 2);
193 cp += 2;
194 /* Data */
195 if (len > AOEV1_ISSUE_ARG_LEN)
196 ND_PRINT((ndo, "\n\tData: %u bytes", len - AOEV1_ISSUE_ARG_LEN));
197 return;
198
199 corrupt:
200 ND_PRINT((ndo, "%s", cstr));
201 ND_TCHECK2(*cp, ep - cp);
202 return;
203 trunc:
204 ND_PRINT((ndo, "%s", tstr));
205 }
206
207 static void
208 aoev1_query_print(netdissect_options *ndo,
209 const u_char *cp, const u_int len)
210 {
211 const u_char *ep = cp + len;
212 uint16_t cslen;
213
214 if (len < AOEV1_QUERY_ARG_LEN)
215 goto corrupt;
216 /* Buffer Count */
217 ND_TCHECK2(*cp, 2);
218 ND_PRINT((ndo, "\n\tBuffer Count: %u", EXTRACT_16BITS(cp)));
219 cp += 2;
220 /* Firmware Version */
221 ND_TCHECK2(*cp, 2);
222 ND_PRINT((ndo, ", Firmware Version: %u", EXTRACT_16BITS(cp)));
223 cp += 2;
224 /* Sector Count */
225 ND_TCHECK2(*cp, 1);
226 ND_PRINT((ndo, ", Sector Count: %u", *cp));
227 cp += 1;
228 /* AoE/CCmd */
229 ND_TCHECK2(*cp, 1);
230 ND_PRINT((ndo, ", AoE: %u, CCmd: %s", (*cp & 0xF0) >> 4,
231 tok2str(aoev1_ccmd_str, "Unknown (0x02x)", *cp & 0x0F)));
232 cp += 1;
233 /* Config String Length */
234 ND_TCHECK2(*cp, 2);
235 cslen = EXTRACT_16BITS(cp);
236 cp += 2;
237 if (cslen > AOEV1_MAX_CONFSTR_LEN || AOEV1_QUERY_ARG_LEN + cslen > len)
238 goto corrupt;
239 /* Config String */
240 ND_TCHECK2(*cp, cslen);
241 if (cslen) {
242 ND_PRINT((ndo, "\n\tConfig String (length %u): ", cslen));
243 if (fn_printn(ndo, cp, cslen, ndo->ndo_snapend))
244 goto trunc;
245 }
246 return;
247
248 corrupt:
249 ND_PRINT((ndo, "%s", cstr));
250 ND_TCHECK2(*cp, ep - cp);
251 return;
252 trunc:
253 ND_PRINT((ndo, "%s", tstr));
254 }
255
256 static void
257 aoev1_mac_print(netdissect_options *ndo,
258 const u_char *cp, const u_int len)
259 {
260 const u_char *ep = cp + len;
261 uint8_t dircount, i;
262
263 if (len < AOEV1_MAC_ARG_LEN)
264 goto corrupt;
265 /* Reserved */
266 ND_TCHECK2(*cp, 1);
267 cp += 1;
268 /* MCmd */
269 ND_TCHECK2(*cp, 1);
270 ND_PRINT((ndo, "\n\tMCmd: %s", tok2str(aoev1_mcmd_str, "Unknown (0x%02x)", *cp)));
271 cp += 1;
272 /* MError */
273 ND_TCHECK2(*cp, 1);
274 ND_PRINT((ndo, ", MError: %s", tok2str(aoev1_merror_str, "Unknown (0x%02x)", *cp)));
275 cp += 1;
276 /* Dir Count */
277 ND_TCHECK2(*cp, 1);
278 dircount = *cp;
279 cp += 1;
280 ND_PRINT((ndo, ", Dir Count: %u", dircount));
281 if (AOEV1_MAC_ARG_LEN + dircount * 8 > len)
282 goto corrupt;
283 /* directives */
284 for (i = 0; i < dircount; i++) {
285 /* Reserved */
286 ND_TCHECK2(*cp, 1);
287 cp += 1;
288 /* DCmd */
289 ND_TCHECK2(*cp, 1);
290 ND_PRINT((ndo, "\n\t DCmd: %s", tok2str(aoev1_dcmd_str, "Unknown (0x%02x)", *cp)));
291 cp += 1;
292 /* Ethernet Address */
293 ND_TCHECK2(*cp, ETHER_ADDR_LEN);
294 ND_PRINT((ndo, ", Ethernet Address: %s", etheraddr_string(ndo, cp)));
295 cp += ETHER_ADDR_LEN;
296 }
297 return;
298
299 corrupt:
300 ND_PRINT((ndo, "%s", cstr));
301 ND_TCHECK2(*cp, ep - cp);
302 return;
303 trunc:
304 ND_PRINT((ndo, "%s", tstr));
305 }
306
307 static void
308 aoev1_reserve_print(netdissect_options *ndo,
309 const u_char *cp, const u_int len)
310 {
311 const u_char *ep = cp + len;
312 uint8_t nmacs, i;
313
314 if (len < AOEV1_RESERVE_ARG_LEN || (len - AOEV1_RESERVE_ARG_LEN) % ETHER_ADDR_LEN)
315 goto corrupt;
316 /* RCmd */
317 ND_TCHECK2(*cp, 1);
318 ND_PRINT((ndo, "\n\tRCmd: %s", tok2str(aoev1_rcmd_str, "Unknown (0x%02x)", *cp)));
319 cp += 1;
320 /* NMacs (correlated with the length) */
321 ND_TCHECK2(*cp, 1);
322 nmacs = *cp;
323 cp += 1;
324 ND_PRINT((ndo, ", NMacs: %u", nmacs));
325 if (AOEV1_RESERVE_ARG_LEN + nmacs * ETHER_ADDR_LEN != len)
326 goto corrupt;
327 /* addresses */
328 for (i = 0; i < nmacs; i++) {
329 ND_PRINT((ndo, "\n\tEthernet Address %u: %s", i, etheraddr_string(ndo, cp)));
330 cp += ETHER_ADDR_LEN;
331 }
332 return;
333
334 corrupt:
335 ND_PRINT((ndo, "%s", cstr));
336 ND_TCHECK2(*cp, ep - cp);
337 return;
338 trunc:
339 ND_PRINT((ndo, "%s", tstr));
340 }
341
342 /* cp points to the Ver/Flags octet */
343 static void
344 aoev1_print(netdissect_options *ndo,
345 const u_char *cp, const u_int len)
346 {
347 const u_char *ep = cp + len;
348 uint8_t flags, command;
349 void (*cmd_decoder)(netdissect_options *, const u_char *, const u_int);
350
351 if (len < AOEV1_COMMON_HDR_LEN)
352 goto corrupt;
353 /* Flags */
354 flags = *cp & 0x0F;
355 ND_PRINT((ndo, ", Flags: [%s]", bittok2str(aoev1_flag_str, "none", flags)));
356 cp += 1;
357 if (! ndo->ndo_vflag)
358 return;
359 /* Error */
360 ND_TCHECK2(*cp, 1);
361 if (flags & AOEV1_FLAG_E)
362 ND_PRINT((ndo, "\n\tError: %s", tok2str(aoev1_errcode_str, "Invalid (%u)", *cp)));
363 cp += 1;
364 /* Major */
365 ND_TCHECK2(*cp, 2);
366 ND_PRINT((ndo, "\n\tMajor: 0x%04x", EXTRACT_16BITS(cp)));
367 cp += 2;
368 /* Minor */
369 ND_TCHECK2(*cp, 1);
370 ND_PRINT((ndo, ", Minor: 0x%02x", *cp));
371 cp += 1;
372 /* Command */
373 ND_TCHECK2(*cp, 1);
374 command = *cp;
375 cp += 1;
376 ND_PRINT((ndo, ", Command: %s", tok2str(cmdcode_str, "Unknown (0x%02x)", command)));
377 /* Tag */
378 ND_TCHECK2(*cp, 4);
379 ND_PRINT((ndo, ", Tag: 0x%08x", EXTRACT_32BITS(cp)));
380 cp += 4;
381 /* Arg */
382 cmd_decoder =
383 command == AOEV1_CMD_ISSUE_ATA_COMMAND ? aoev1_issue_print :
384 command == AOEV1_CMD_QUERY_CONFIG_INFORMATION ? aoev1_query_print :
385 command == AOEV1_CMD_MAC_MASK_LIST ? aoev1_mac_print :
386 command == AOEV1_CMD_RESERVE_RELEASE ? aoev1_reserve_print :
387 NULL;
388 if (cmd_decoder != NULL)
389 cmd_decoder(ndo, cp, len - AOEV1_COMMON_HDR_LEN);
390 return;
391
392 corrupt:
393 ND_PRINT((ndo, "%s", cstr));
394 ND_TCHECK2(*cp, ep - cp);
395 return;
396 trunc:
397 ND_PRINT((ndo, "%s", tstr));
398 }
399
400 void
401 aoe_print(netdissect_options *ndo,
402 const u_char *cp, const u_int len)
403 {
404 const u_char *ep = cp + len;
405 uint8_t ver;
406
407 ND_PRINT((ndo, "AoE length %u", len));
408
409 if (len < 1)
410 goto corrupt;
411 /* Ver/Flags */
412 ND_TCHECK2(*cp, 1);
413 ver = (*cp & 0xF0) >> 4;
414 /* Don't advance cp yet: low order 4 bits are version-specific. */
415 ND_PRINT((ndo, ", Ver %u", ver));
416
417 switch (ver) {
418 case AOE_V1:
419 aoev1_print(ndo, cp, len);
420 break;
421 }
422 return;
423
424 corrupt:
425 ND_PRINT((ndo, "%s", cstr));
426 ND_TCHECK2(*cp, ep - cp);
427 return;
428 trunc:
429 ND_PRINT((ndo, "%s", tstr));
430 }
431