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