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