]> The Tcpdump Group git mirrors - tcpdump/blob - print-loopback.c
6799bd3c05cfc1c2372a46b95d72768d0db4361d
[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: http://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 static const char tstr[] = " [|loopback]";
46
47 #define LOOPBACK_REPLY 1
48 #define LOOPBACK_FWDDATA 2
49
50 static const struct tok fcode_str[] = {
51 { LOOPBACK_REPLY, "Reply" },
52 { LOOPBACK_FWDDATA, "Forward Data" },
53 { 0, NULL }
54 };
55
56 static void
57 loopback_message_print(netdissect_options *ndo, const u_char *cp, const u_int len)
58 {
59 const u_char *ep = cp + len;
60 uint16_t function;
61
62 if (len < 2)
63 goto invalid;
64 /* function */
65 ND_TCHECK_2(cp);
66 function = EXTRACT_LE_U_2(cp);
67 cp += 2;
68 ND_PRINT(", %s", tok2str(fcode_str, " invalid (%u)", function));
69
70 switch (function) {
71 case LOOPBACK_REPLY:
72 if (len < 4)
73 goto invalid;
74 /* receipt number */
75 ND_TCHECK_2(cp);
76 ND_PRINT(", receipt number %u", EXTRACT_LE_U_2(cp));
77 cp += 2;
78 /* data */
79 ND_PRINT(", data (%u octets)", len - 4);
80 ND_TCHECK_LEN(cp, len - 4);
81 break;
82 case LOOPBACK_FWDDATA:
83 if (len < 8)
84 goto invalid;
85 /* forwarding address */
86 ND_TCHECK_LEN(cp, MAC_ADDR_LEN);
87 ND_PRINT(", forwarding address %s", etheraddr_string(ndo, cp));
88 cp += MAC_ADDR_LEN;
89 /* data */
90 ND_PRINT(", data (%u octets)", len - 8);
91 ND_TCHECK_LEN(cp, len - 8);
92 break;
93 default:
94 ND_TCHECK_LEN(cp, len - 2);
95 break;
96 }
97 return;
98
99 invalid:
100 ND_PRINT("%s", istr);
101 ND_TCHECK_LEN(cp, ep - cp);
102 return;
103 trunc:
104 ND_PRINT("%s", tstr);
105 }
106
107 void
108 loopback_print(netdissect_options *ndo, const u_char *cp, const u_int len)
109 {
110 const u_char *ep = cp + len;
111 uint16_t skipCount;
112
113 ND_PRINT("Loopback");
114 if (len < 2)
115 goto invalid;
116 /* skipCount */
117 ND_TCHECK_2(cp);
118 skipCount = EXTRACT_LE_U_2(cp);
119 cp += 2;
120 ND_PRINT(", skipCount %u", skipCount);
121 if (skipCount % 8)
122 ND_PRINT(" (bogus)");
123 if (skipCount > len - 2)
124 goto invalid;
125 loopback_message_print(ndo, cp + skipCount, len - 2 - skipCount);
126 return;
127
128 invalid:
129 ND_PRINT("%s", istr);
130 ND_TCHECK_LEN(cp, ep - cp);
131 return;
132 trunc:
133 ND_PRINT("%s", tstr);
134 }
135