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