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