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