]> The Tcpdump Group git mirrors - tcpdump/blob - print-loopback.c
a90c293b015cd43e0950c92f475d3dda1c03024d
[tcpdump] / print-loopback.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: Loopback Protocol printer */
29
30 /*
31 * originally defined as the Ethernet Configuration Testing Protocol.
32 * specification: https://www.mit.edu/people/jhawk/ctp.pdf
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "netdissect-stdinc.h"
40
41 #include "netdissect.h"
42 #include "extract.h"
43 #include "addrtoname.h"
44
45
46 #define LOOPBACK_REPLY 1
47 #define LOOPBACK_FWDDATA 2
48
49 static const struct tok fcode_str[] = {
50 { LOOPBACK_REPLY, "Reply" },
51 { LOOPBACK_FWDDATA, "Forward Data" },
52 { 0, NULL }
53 };
54
55 static void
56 loopback_message_print(netdissect_options *ndo, const u_char *cp, const u_int len)
57 {
58 const u_char *ep = ndo->ndo_snapend;
59 uint16_t function;
60
61 if (len < 2)
62 goto invalid;
63 /* function */
64 function = GET_LE_U_2(cp);
65 cp += 2;
66 ND_PRINT(", %s", tok2str(fcode_str, " invalid (%u)", function));
67
68 switch (function) {
69 case LOOPBACK_REPLY:
70 if (len < 4)
71 goto invalid;
72 /* receipt number */
73 ND_PRINT(", receipt number %u", GET_LE_U_2(cp));
74 cp += 2;
75 /* data */
76 ND_PRINT(", data (%u octets)", len - 4);
77 ND_TCHECK_LEN(cp, len - 4);
78 break;
79 case LOOPBACK_FWDDATA:
80 if (len < 8)
81 goto invalid;
82 /* forwarding address */
83 ND_TCHECK_LEN(cp, MAC_ADDR_LEN);
84 ND_PRINT(", forwarding address %s", GET_ETHERADDR_STRING(cp));
85 cp += MAC_ADDR_LEN;
86 /* data */
87 ND_PRINT(", data (%u octets)", len - 8);
88 ND_TCHECK_LEN(cp, len - 8);
89 break;
90 default:
91 ND_TCHECK_LEN(cp, len - 2);
92 break;
93 }
94 return;
95
96 invalid:
97 nd_print_invalid(ndo);
98 ND_TCHECK_LEN(cp, ep - cp);
99 return;
100 trunc:
101 nd_print_trunc(ndo);
102 }
103
104 void
105 loopback_print(netdissect_options *ndo, const u_char *cp, const u_int len)
106 {
107 const u_char *ep = ndo->ndo_snapend;
108 uint16_t skipCount;
109
110 ndo->ndo_protocol = "loopback";
111 ND_PRINT("Loopback");
112 if (len < 2)
113 goto invalid;
114 /* skipCount */
115 skipCount = GET_LE_U_2(cp);
116 cp += 2;
117 ND_PRINT(", skipCount %u", skipCount);
118 if (skipCount % 8)
119 ND_PRINT(" (bogus)");
120 if (skipCount > len - 2)
121 goto invalid;
122 loopback_message_print(ndo, cp + skipCount, len - 2 - skipCount);
123 return;
124
125 invalid:
126 nd_print_invalid(ndo);
127 ND_TCHECK_LEN(cp, ep - cp);
128 return;
129 trunc:
130 nd_print_trunc(ndo);
131 }
132