]> The Tcpdump Group git mirrors - tcpdump/blob - print-loopback.c
ICMP6 RPL: don't use inet_ntop()
[tcpdump] / print-loopback.c
1 /*
2 * This module implements decoding of the Loopback Protocol, originally
3 * defined as the Configuration Testing Protocol. It is based on the following
4 * specification:
5 * http://www.mit.edu/people/jhawk/ctp.pdf
6 *
7 * Copyright (c) 2014 The TCPDUMP project
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <netdissect-stdinc.h>
38
39 #include "netdissect.h"
40 #include "extract.h"
41 #include "ether.h"
42 #include "addrtoname.h"
43
44 static const char tstr[] = " [|loopback]";
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 = cp + len;
59 uint16_t function;
60
61 if (len < 2)
62 goto invalid;
63 /* function */
64 ND_TCHECK2(*cp, 2);
65 function = EXTRACT_LE_16BITS(cp);
66 cp += 2;
67 ND_PRINT((ndo, ", %s", tok2str(fcode_str, " invalid (%u)", function)));
68
69 switch (function) {
70 case LOOPBACK_REPLY:
71 if (len < 4)
72 goto invalid;
73 /* receipt number */
74 ND_TCHECK2(*cp, 2);
75 ND_PRINT((ndo, ", receipt number %u", EXTRACT_LE_16BITS(cp)));
76 cp += 2;
77 /* data */
78 ND_PRINT((ndo, ", data (%u octets)", len - 4));
79 ND_TCHECK2(*cp, len - 4);
80 break;
81 case LOOPBACK_FWDDATA:
82 if (len < 8)
83 goto invalid;
84 /* forwarding address */
85 ND_TCHECK2(*cp, ETHER_ADDR_LEN);
86 ND_PRINT((ndo, ", forwarding address %s", etheraddr_string(ndo, cp)));
87 cp += ETHER_ADDR_LEN;
88 /* data */
89 ND_PRINT((ndo, ", data (%u octets)", len - 8));
90 ND_TCHECK2(*cp, len - 8);
91 break;
92 default:
93 ND_TCHECK2(*cp, len - 2);
94 break;
95 }
96 return;
97
98 invalid:
99 ND_PRINT((ndo, "%s", istr));
100 ND_TCHECK2(*cp, ep - cp);
101 return;
102 trunc:
103 ND_PRINT((ndo, "%s", tstr));
104 }
105
106 void
107 loopback_print(netdissect_options *ndo, const u_char *cp, const u_int len)
108 {
109 const u_char *ep = cp + len;
110 uint16_t skipCount;
111
112 ND_PRINT((ndo, "Loopback"));
113 if (len < 2)
114 goto invalid;
115 /* skipCount */
116 ND_TCHECK2(*cp, 2);
117 skipCount = EXTRACT_LE_16BITS(cp);
118 cp += 2;
119 ND_PRINT((ndo, ", skipCount %u", skipCount));
120 if (skipCount % 8)
121 ND_PRINT((ndo, " (bogus)"));
122 if (skipCount > len - 2)
123 goto invalid;
124 loopback_message_print(ndo, cp + skipCount, len - 2 - skipCount);
125 return;
126
127 invalid:
128 ND_PRINT((ndo, "%s", istr));
129 ND_TCHECK2(*cp, ep - cp);
130 return;
131 trunc:
132 ND_PRINT((ndo, "%s", tstr));
133 }
134