]> The Tcpdump Group git mirrors - tcpdump/blob - print-aoe.c
Remove some now redundant ND_TCHECK_LEN(e, sizeof(nd_ipv6)) calls
[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:
31 * https://web.archive.org/web/20161025044402/http://brantleycoilecompany.com/AoEr11.pdf
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "netdissect-stdinc.h"
39
40 #include "netdissect.h"
41 #include "extract.h"
42 #include "addrtoname.h"
43
44
45 #define AOE_V1 1
46 #define ATA_SECTOR_SIZE 512
47
48 #define AOEV1_CMD_ISSUE_ATA_COMMAND 0
49 #define AOEV1_CMD_QUERY_CONFIG_INFORMATION 1
50 #define AOEV1_CMD_MAC_MASK_LIST 2
51 #define AOEV1_CMD_RESERVE_RELEASE 3
52
53 static const struct tok cmdcode_str[] = {
54 { AOEV1_CMD_ISSUE_ATA_COMMAND, "Issue ATA Command" },
55 { AOEV1_CMD_QUERY_CONFIG_INFORMATION, "Query Config Information" },
56 { AOEV1_CMD_MAC_MASK_LIST, "MAC Mask List" },
57 { AOEV1_CMD_RESERVE_RELEASE, "Reserve/Release" },
58 { 0, NULL }
59 };
60
61 #define AOEV1_COMMON_HDR_LEN 10U /* up to but w/o Arg */
62 #define AOEV1_ISSUE_ARG_LEN 12U /* up to but w/o Data */
63 #define AOEV1_QUERY_ARG_LEN 8U /* up to but w/o Config String */
64 #define AOEV1_MAC_ARG_LEN 4U /* up to but w/o Directive 0 */
65 #define AOEV1_RESERVE_ARG_LEN 2U /* up to but w/o Ethernet address 0 */
66 #define AOEV1_MAX_CONFSTR_LEN 1024U
67
68 #define AOEV1_FLAG_R 0x08
69 #define AOEV1_FLAG_E 0x04
70
71 static const struct tok aoev1_flag_str[] = {
72 { AOEV1_FLAG_R, "Response" },
73 { AOEV1_FLAG_E, "Error" },
74 { 0x02, "MBZ-0x02" },
75 { 0x01, "MBZ-0x01" },
76 { 0, NULL }
77 };
78
79 static const struct tok aoev1_errcode_str[] = {
80 { 1, "Unrecognized command code" },
81 { 2, "Bad argument parameter" },
82 { 3, "Device unavailable" },
83 { 4, "Config string present" },
84 { 5, "Unsupported version" },
85 { 6, "Target is reserved" },
86 { 0, NULL }
87 };
88
89 #define AOEV1_AFLAG_E 0x40
90 #define AOEV1_AFLAG_D 0x10
91 #define AOEV1_AFLAG_A 0x02
92 #define AOEV1_AFLAG_W 0x01
93
94 static const struct tok aoev1_aflag_str[] = {
95 { 0x08, "MBZ-0x08" },
96 { AOEV1_AFLAG_E, "Ext48" },
97 { 0x06, "MBZ-0x06" },
98 { AOEV1_AFLAG_D, "Device" },
99 { 0x04, "MBZ-0x04" },
100 { 0x03, "MBZ-0x03" },
101 { AOEV1_AFLAG_A, "Async" },
102 { AOEV1_AFLAG_W, "Write" },
103 { 0, NULL }
104 };
105
106 static const struct tok aoev1_ccmd_str[] = {
107 { 0, "read config string" },
108 { 1, "test config string" },
109 { 2, "test config string prefix" },
110 { 3, "set config string" },
111 { 4, "force set config string" },
112 { 0, NULL }
113 };
114
115 static const struct tok aoev1_mcmd_str[] = {
116 { 0, "Read Mac Mask List" },
117 { 1, "Edit Mac Mask List" },
118 { 0, NULL }
119 };
120
121 static const struct tok aoev1_merror_str[] = {
122 { 1, "Unspecified Error" },
123 { 2, "Bad DCmd directive" },
124 { 3, "Mask list full" },
125 { 0, NULL }
126 };
127
128 static const struct tok aoev1_dcmd_str[] = {
129 { 0, "No Directive" },
130 { 1, "Add mac address to mask list" },
131 { 2, "Delete mac address from mask list" },
132 { 0, NULL }
133 };
134
135 static const struct tok aoev1_rcmd_str[] = {
136 { 0, "Read reserve list" },
137 { 1, "Set reserve list" },
138 { 2, "Force set reserve list" },
139 { 0, NULL }
140 };
141
142 static void
143 aoev1_issue_print(netdissect_options *ndo,
144 const u_char *cp, const u_int len)
145 {
146 const u_char *ep = ndo->ndo_snapend;
147
148 if (len < AOEV1_ISSUE_ARG_LEN)
149 goto invalid;
150 /* AFlags */
151 ND_PRINT("\n\tAFlags: [%s]",
152 bittok2str(aoev1_aflag_str, "none", GET_U_1(cp)));
153 cp += 1;
154 /* Err/Feature */
155 ND_PRINT(", Err/Feature: %u", GET_U_1(cp));
156 cp += 1;
157 /* Sector Count (not correlated with the length) */
158 ND_PRINT(", Sector Count: %u", GET_U_1(cp));
159 cp += 1;
160 /* Cmd/Status */
161 ND_PRINT(", Cmd/Status: %u", GET_U_1(cp));
162 cp += 1;
163 /* lba0 */
164 ND_PRINT("\n\tlba0: %u", GET_U_1(cp));
165 cp += 1;
166 /* lba1 */
167 ND_PRINT(", lba1: %u", GET_U_1(cp));
168 cp += 1;
169 /* lba2 */
170 ND_PRINT(", lba2: %u", GET_U_1(cp));
171 cp += 1;
172 /* lba3 */
173 ND_PRINT(", lba3: %u", GET_U_1(cp));
174 cp += 1;
175 /* lba4 */
176 ND_PRINT(", lba4: %u", GET_U_1(cp));
177 cp += 1;
178 /* lba5 */
179 ND_PRINT(", lba5: %u", GET_U_1(cp));
180 cp += 1;
181 /* Reserved */
182 ND_TCHECK_2(cp);
183 cp += 2;
184 /* Data */
185 if (len > AOEV1_ISSUE_ARG_LEN)
186 ND_PRINT("\n\tData: %u bytes", len - AOEV1_ISSUE_ARG_LEN);
187 return;
188
189 invalid:
190 nd_print_invalid(ndo);
191 ND_TCHECK_LEN(cp, ep - cp);
192 return;
193 trunc:
194 nd_print_trunc(ndo);
195 }
196
197 static void
198 aoev1_query_print(netdissect_options *ndo,
199 const u_char *cp, const u_int len)
200 {
201 const u_char *ep = ndo->ndo_snapend;
202 uint16_t cslen;
203
204 if (len < AOEV1_QUERY_ARG_LEN)
205 goto invalid;
206 /* Buffer Count */
207 ND_PRINT("\n\tBuffer Count: %u", GET_BE_U_2(cp));
208 cp += 2;
209 /* Firmware Version */
210 ND_PRINT(", Firmware Version: %u", GET_BE_U_2(cp));
211 cp += 2;
212 /* Sector Count */
213 ND_PRINT(", Sector Count: %u", GET_U_1(cp));
214 cp += 1;
215 /* AoE/CCmd */
216 ND_PRINT(", AoE: %u, CCmd: %s", (GET_U_1(cp) & 0xF0) >> 4,
217 tok2str(aoev1_ccmd_str, "Unknown (0x02x)", GET_U_1(cp) & 0x0F));
218 cp += 1;
219 /* Config String Length */
220 cslen = GET_BE_U_2(cp);
221 cp += 2;
222 if (cslen > AOEV1_MAX_CONFSTR_LEN || AOEV1_QUERY_ARG_LEN + cslen > len)
223 goto invalid;
224 /* Config String */
225 if (cslen) {
226 ND_TCHECK_LEN(cp, cslen);
227 ND_PRINT("\n\tConfig String (length %u): ", cslen);
228 if (nd_printn(ndo, cp, cslen, ndo->ndo_snapend))
229 goto trunc;
230 }
231 return;
232
233 invalid:
234 nd_print_invalid(ndo);
235 ND_TCHECK_LEN(cp, ep - cp);
236 return;
237 trunc:
238 nd_print_trunc(ndo);
239 }
240
241 static void
242 aoev1_mac_print(netdissect_options *ndo,
243 const u_char *cp, const u_int len)
244 {
245 const u_char *ep = ndo->ndo_snapend;
246 uint8_t dircount, i;
247
248 if (len < AOEV1_MAC_ARG_LEN)
249 goto invalid;
250 /* Reserved */
251 ND_TCHECK_1(cp);
252 cp += 1;
253 /* MCmd */
254 ND_PRINT("\n\tMCmd: %s",
255 tok2str(aoev1_mcmd_str, "Unknown (0x%02x)", GET_U_1(cp)));
256 cp += 1;
257 /* MError */
258 ND_PRINT(", MError: %s",
259 tok2str(aoev1_merror_str, "Unknown (0x%02x)", GET_U_1(cp)));
260 cp += 1;
261 /* Dir Count */
262 dircount = GET_U_1(cp);
263 cp += 1;
264 ND_PRINT(", Dir Count: %u", dircount);
265 if (AOEV1_MAC_ARG_LEN + dircount * 8 > len)
266 goto invalid;
267 /* directives */
268 for (i = 0; i < dircount; i++) {
269 /* Reserved */
270 ND_TCHECK_1(cp);
271 cp += 1;
272 /* DCmd */
273 ND_PRINT("\n\t DCmd: %s",
274 tok2str(aoev1_dcmd_str, "Unknown (0x%02x)", GET_U_1(cp)));
275 cp += 1;
276 /* Ethernet Address */
277 ND_TCHECK_LEN(cp, MAC_ADDR_LEN);
278 ND_PRINT(", Ethernet Address: %s", GET_ETHERADDR_STRING(cp));
279 cp += MAC_ADDR_LEN;
280 }
281 return;
282
283 invalid:
284 nd_print_invalid(ndo);
285 ND_TCHECK_LEN(cp, ep - cp);
286 return;
287 trunc:
288 nd_print_trunc(ndo);
289 }
290
291 static void
292 aoev1_reserve_print(netdissect_options *ndo,
293 const u_char *cp, const u_int len)
294 {
295 const u_char *ep = ndo->ndo_snapend;
296 uint8_t nmacs, i;
297
298 if (len < AOEV1_RESERVE_ARG_LEN || (len - AOEV1_RESERVE_ARG_LEN) % MAC_ADDR_LEN)
299 goto invalid;
300 /* RCmd */
301 ND_PRINT("\n\tRCmd: %s",
302 tok2str(aoev1_rcmd_str, "Unknown (0x%02x)", GET_U_1(cp)));
303 cp += 1;
304 /* NMacs (correlated with the length) */
305 nmacs = GET_U_1(cp);
306 cp += 1;
307 ND_PRINT(", NMacs: %u", nmacs);
308 if (AOEV1_RESERVE_ARG_LEN + nmacs * MAC_ADDR_LEN != len)
309 goto invalid;
310 /* addresses */
311 for (i = 0; i < nmacs; i++) {
312 ND_TCHECK_LEN(cp, MAC_ADDR_LEN);
313 ND_PRINT("\n\tEthernet Address %u: %s", i, GET_ETHERADDR_STRING(cp));
314 cp += MAC_ADDR_LEN;
315 }
316 return;
317
318 invalid:
319 nd_print_invalid(ndo);
320 ND_TCHECK_LEN(cp, ep - cp);
321 return;
322 trunc:
323 nd_print_trunc(ndo);
324 }
325
326 /* cp points to the Ver/Flags octet */
327 static void
328 aoev1_print(netdissect_options *ndo,
329 const u_char *cp, const u_int len)
330 {
331 const u_char *ep = ndo->ndo_snapend;
332 uint8_t flags, command;
333 void (*cmd_decoder)(netdissect_options *, const u_char *, const u_int);
334
335 if (len < AOEV1_COMMON_HDR_LEN)
336 goto invalid;
337 /* Flags */
338 flags = GET_U_1(cp) & 0x0F;
339 ND_PRINT(", Flags: [%s]", bittok2str(aoev1_flag_str, "none", flags));
340 cp += 1;
341 if (! ndo->ndo_vflag)
342 return;
343 /* Error */
344 if (flags & AOEV1_FLAG_E)
345 ND_PRINT("\n\tError: %s",
346 tok2str(aoev1_errcode_str, "Invalid (%u)", GET_U_1(cp)));
347 cp += 1;
348 /* Major */
349 ND_PRINT("\n\tMajor: 0x%04x", GET_BE_U_2(cp));
350 cp += 2;
351 /* Minor */
352 ND_PRINT(", Minor: 0x%02x", GET_U_1(cp));
353 cp += 1;
354 /* Command */
355 command = GET_U_1(cp);
356 cp += 1;
357 ND_PRINT(", Command: %s", tok2str(cmdcode_str, "Unknown (0x%02x)", command));
358 /* Tag */
359 ND_PRINT(", Tag: 0x%08x", GET_BE_U_4(cp));
360 cp += 4;
361 /* Arg */
362 cmd_decoder =
363 command == AOEV1_CMD_ISSUE_ATA_COMMAND ? aoev1_issue_print :
364 command == AOEV1_CMD_QUERY_CONFIG_INFORMATION ? aoev1_query_print :
365 command == AOEV1_CMD_MAC_MASK_LIST ? aoev1_mac_print :
366 command == AOEV1_CMD_RESERVE_RELEASE ? aoev1_reserve_print :
367 NULL;
368 if (cmd_decoder != NULL)
369 cmd_decoder(ndo, cp, len - AOEV1_COMMON_HDR_LEN);
370 return;
371
372 invalid:
373 nd_print_invalid(ndo);
374 ND_TCHECK_LEN(cp, ep - cp);
375 return;
376 trunc:
377 nd_print_trunc(ndo);
378 }
379
380 void
381 aoe_print(netdissect_options *ndo,
382 const u_char *cp, const u_int len)
383 {
384 const u_char *ep = ndo->ndo_snapend;
385 uint8_t ver;
386
387 ndo->ndo_protocol = "aoe";
388 ND_PRINT("AoE length %u", len);
389
390 if (len < 1)
391 goto invalid;
392 /* Ver/Flags */
393 ver = (GET_U_1(cp) & 0xF0) >> 4;
394 /* Don't advance cp yet: low order 4 bits are version-specific. */
395 ND_PRINT(", Ver %u", ver);
396
397 switch (ver) {
398 case AOE_V1:
399 aoev1_print(ndo, cp, len);
400 break;
401 }
402 return;
403
404 invalid:
405 nd_print_invalid(ndo);
406 ND_TCHECK_LEN(cp, ep - cp);
407 return;
408 trunc:
409 nd_print_trunc(ndo);
410 }
411