]> The Tcpdump Group git mirrors - tcpdump/blob - print-mobility.c
Fix a bunch of de-constifications.
[tcpdump] / print-mobility.c
1 /*
2 * Copyright (C) 2002 WIDE 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 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifdef INET6
35 #include <tcpdump-stdinc.h>
36
37 #include "ip6.h"
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "extract.h" /* must come after interface.h */
41
42 /* Mobility header */
43 struct ip6_mobility {
44 uint8_t ip6m_pproto; /* following payload protocol (for PG) */
45 uint8_t ip6m_len; /* length in units of 8 octets */
46 uint8_t ip6m_type; /* message type */
47 uint8_t reserved; /* reserved */
48 uint16_t ip6m_cksum; /* sum of IPv6 pseudo-header and MH */
49 union {
50 uint16_t ip6m_un_data16[1]; /* type-specific field */
51 uint8_t ip6m_un_data8[2]; /* type-specific field */
52 } ip6m_dataun;
53 };
54
55 #define ip6m_data16 ip6m_dataun.ip6m_un_data16
56 #define ip6m_data8 ip6m_dataun.ip6m_un_data8
57
58 #define IP6M_MINLEN 8
59
60 /* http://www.iana.org/assignments/mobility-parameters/mobility-parameters.xhtml */
61
62 /* message type */
63 #define IP6M_BINDING_REQUEST 0 /* Binding Refresh Request */
64 #define IP6M_HOME_TEST_INIT 1 /* Home Test Init */
65 #define IP6M_CAREOF_TEST_INIT 2 /* Care-of Test Init */
66 #define IP6M_HOME_TEST 3 /* Home Test */
67 #define IP6M_CAREOF_TEST 4 /* Care-of Test */
68 #define IP6M_BINDING_UPDATE 5 /* Binding Update */
69 #define IP6M_BINDING_ACK 6 /* Binding Acknowledgement */
70 #define IP6M_BINDING_ERROR 7 /* Binding Error */
71 #define IP6M_MAX 7
72
73 static const struct tok ip6m_str[] = {
74 { IP6M_BINDING_REQUEST, "BRR" },
75 { IP6M_HOME_TEST_INIT, "HoTI" },
76 { IP6M_CAREOF_TEST_INIT, "CoTI" },
77 { IP6M_HOME_TEST, "HoT" },
78 { IP6M_CAREOF_TEST, "CoT" },
79 { IP6M_BINDING_UPDATE, "BU" },
80 { IP6M_BINDING_ACK, "BA" },
81 { IP6M_BINDING_ERROR, "BE" },
82 { 0, NULL }
83 };
84
85 static const unsigned ip6m_hdrlen[IP6M_MAX + 1] = {
86 IP6M_MINLEN, /* IP6M_BINDING_REQUEST */
87 IP6M_MINLEN + 8, /* IP6M_HOME_TEST_INIT */
88 IP6M_MINLEN + 8, /* IP6M_CAREOF_TEST_INIT */
89 IP6M_MINLEN + 16, /* IP6M_HOME_TEST */
90 IP6M_MINLEN + 16, /* IP6M_CAREOF_TEST */
91 IP6M_MINLEN + 4, /* IP6M_BINDING_UPDATE */
92 IP6M_MINLEN + 4, /* IP6M_BINDING_ACK */
93 IP6M_MINLEN + 16, /* IP6M_BINDING_ERROR */
94 };
95
96 /* Mobility Header Options */
97 #define IP6MOPT_MINLEN 2
98 #define IP6MOPT_PAD1 0x0 /* Pad1 */
99 #define IP6MOPT_PADN 0x1 /* PadN */
100 #define IP6MOPT_REFRESH 0x2 /* Binding Refresh Advice */
101 #define IP6MOPT_REFRESH_MINLEN 4
102 #define IP6MOPT_ALTCOA 0x3 /* Alternate Care-of Address */
103 #define IP6MOPT_ALTCOA_MINLEN 18
104 #define IP6MOPT_NONCEID 0x4 /* Nonce Indices */
105 #define IP6MOPT_NONCEID_MINLEN 6
106 #define IP6MOPT_AUTH 0x5 /* Binding Authorization Data */
107 #define IP6MOPT_AUTH_MINLEN 12
108
109 static void
110 mobility_opt_print(netdissect_options *ndo,
111 const u_char *bp, const unsigned len)
112 {
113 unsigned i, optlen;
114
115 for (i = 0; i < len; i += optlen) {
116 ND_TCHECK(bp[i]);
117 if (bp[i] == IP6MOPT_PAD1)
118 optlen = 1;
119 else {
120 if (i + 1 < len) {
121 ND_TCHECK(bp[i + 1]);
122 optlen = bp[i + 1] + 2;
123 }
124 else
125 goto trunc;
126 }
127 if (i + optlen > len)
128 goto trunc;
129 ND_TCHECK(bp[i + optlen]);
130
131 switch (bp[i]) {
132 case IP6MOPT_PAD1:
133 ND_PRINT((ndo, "(pad1)"));
134 break;
135 case IP6MOPT_PADN:
136 if (len - i < IP6MOPT_MINLEN) {
137 ND_PRINT((ndo, "(padn: trunc)"));
138 goto trunc;
139 }
140 ND_PRINT((ndo, "(padn)"));
141 break;
142 case IP6MOPT_REFRESH:
143 if (len - i < IP6MOPT_REFRESH_MINLEN) {
144 ND_PRINT((ndo, "(refresh: trunc)"));
145 goto trunc;
146 }
147 /* units of 4 secs */
148 ND_PRINT((ndo, "(refresh: %u)",
149 EXTRACT_16BITS(&bp[i+2]) << 2));
150 break;
151 case IP6MOPT_ALTCOA:
152 if (len - i < IP6MOPT_ALTCOA_MINLEN) {
153 ND_PRINT((ndo, "(altcoa: trunc)"));
154 goto trunc;
155 }
156 ND_PRINT((ndo, "(alt-CoA: %s)", ip6addr_string(ndo, &bp[i+2])));
157 break;
158 case IP6MOPT_NONCEID:
159 if (len - i < IP6MOPT_NONCEID_MINLEN) {
160 ND_PRINT((ndo, "(ni: trunc)"));
161 goto trunc;
162 }
163 ND_PRINT((ndo, "(ni: ho=0x%04x co=0x%04x)",
164 EXTRACT_16BITS(&bp[i+2]),
165 EXTRACT_16BITS(&bp[i+4])));
166 break;
167 case IP6MOPT_AUTH:
168 if (len - i < IP6MOPT_AUTH_MINLEN) {
169 ND_PRINT((ndo, "(auth: trunc)"));
170 goto trunc;
171 }
172 ND_PRINT((ndo, "(auth)"));
173 break;
174 default:
175 if (len - i < IP6MOPT_MINLEN) {
176 ND_PRINT((ndo, "(sopt_type %u: trunc)", bp[i]));
177 goto trunc;
178 }
179 ND_PRINT((ndo, "(type-0x%02x: len=%u)", bp[i], bp[i + 1]));
180 break;
181 }
182 }
183 return;
184
185 trunc:
186 ND_PRINT((ndo, "[trunc] "));
187 }
188
189 /*
190 * Mobility Header
191 */
192 int
193 mobility_print(netdissect_options *ndo,
194 const u_char *bp, const u_char *bp2 _U_)
195 {
196 const struct ip6_mobility *mh;
197 const u_char *ep;
198 unsigned mhlen, hlen;
199 uint8_t type;
200
201 mh = (const struct ip6_mobility *)bp;
202
203 /* 'ep' points to the end of available data. */
204 ep = ndo->ndo_snapend;
205
206 if (!ND_TTEST(mh->ip6m_len)) {
207 /*
208 * There's not enough captured data to include the
209 * mobility header length.
210 *
211 * Our caller expects us to return the length, however,
212 * so return a value that will run to the end of the
213 * captured data.
214 *
215 * XXX - "ip6_print()" doesn't do anything with the
216 * returned length, however, as it breaks out of the
217 * header-processing loop.
218 */
219 mhlen = ep - bp;
220 goto trunc;
221 }
222 mhlen = (mh->ip6m_len + 1) << 3;
223
224 /* XXX ip6m_cksum */
225
226 ND_TCHECK(mh->ip6m_type);
227 type = mh->ip6m_type;
228 if (type <= IP6M_MAX && mhlen < ip6m_hdrlen[type]) {
229 ND_PRINT((ndo, "(header length %u is too small for type %u)", mhlen, type));
230 goto trunc;
231 }
232 ND_PRINT((ndo, "mobility: %s", tok2str(ip6m_str, "type-#%u", type)));
233 switch (type) {
234 case IP6M_BINDING_REQUEST:
235 hlen = IP6M_MINLEN;
236 break;
237 case IP6M_HOME_TEST_INIT:
238 case IP6M_CAREOF_TEST_INIT:
239 hlen = IP6M_MINLEN;
240 if (ndo->ndo_vflag) {
241 ND_TCHECK2(*mh, hlen + 8);
242 ND_PRINT((ndo, " %s Init Cookie=%08x:%08x",
243 type == IP6M_HOME_TEST_INIT ? "Home" : "Care-of",
244 EXTRACT_32BITS(&bp[hlen]),
245 EXTRACT_32BITS(&bp[hlen + 4])));
246 }
247 hlen += 8;
248 break;
249 case IP6M_HOME_TEST:
250 case IP6M_CAREOF_TEST:
251 ND_TCHECK(mh->ip6m_data16[0]);
252 ND_PRINT((ndo, " nonce id=0x%x", EXTRACT_16BITS(&mh->ip6m_data16[0])));
253 hlen = IP6M_MINLEN;
254 if (ndo->ndo_vflag) {
255 ND_TCHECK2(*mh, hlen + 8);
256 ND_PRINT((ndo, " %s Init Cookie=%08x:%08x",
257 type == IP6M_HOME_TEST ? "Home" : "Care-of",
258 EXTRACT_32BITS(&bp[hlen]),
259 EXTRACT_32BITS(&bp[hlen + 4])));
260 }
261 hlen += 8;
262 if (ndo->ndo_vflag) {
263 ND_TCHECK2(*mh, hlen + 8);
264 ND_PRINT((ndo, " %s Keygen Token=%08x:%08x",
265 type == IP6M_HOME_TEST ? "Home" : "Care-of",
266 EXTRACT_32BITS(&bp[hlen]),
267 EXTRACT_32BITS(&bp[hlen + 4])));
268 }
269 hlen += 8;
270 break;
271 case IP6M_BINDING_UPDATE:
272 ND_TCHECK(mh->ip6m_data16[0]);
273 ND_PRINT((ndo, " seq#=%u", EXTRACT_16BITS(&mh->ip6m_data16[0])));
274 hlen = IP6M_MINLEN;
275 ND_TCHECK2(*mh, hlen + 1);
276 if (bp[hlen] & 0xf0)
277 ND_PRINT((ndo, " "));
278 if (bp[hlen] & 0x80)
279 ND_PRINT((ndo, "A"));
280 if (bp[hlen] & 0x40)
281 ND_PRINT((ndo, "H"));
282 if (bp[hlen] & 0x20)
283 ND_PRINT((ndo, "L"));
284 if (bp[hlen] & 0x10)
285 ND_PRINT((ndo, "K"));
286 /* Reserved (4bits) */
287 hlen += 1;
288 /* Reserved (8bits) */
289 hlen += 1;
290 ND_TCHECK2(*mh, hlen + 2);
291 /* units of 4 secs */
292 ND_PRINT((ndo, " lifetime=%u", EXTRACT_16BITS(&bp[hlen]) << 2));
293 hlen += 2;
294 break;
295 case IP6M_BINDING_ACK:
296 ND_TCHECK(mh->ip6m_data8[0]);
297 ND_PRINT((ndo, " status=%u", mh->ip6m_data8[0]));
298 if (mh->ip6m_data8[1] & 0x80)
299 ND_PRINT((ndo, " K"));
300 /* Reserved (7bits) */
301 hlen = IP6M_MINLEN;
302 ND_TCHECK2(*mh, hlen + 2);
303 ND_PRINT((ndo, " seq#=%u", EXTRACT_16BITS(&bp[hlen])));
304 hlen += 2;
305 ND_TCHECK2(*mh, hlen + 2);
306 /* units of 4 secs */
307 ND_PRINT((ndo, " lifetime=%u", EXTRACT_16BITS(&bp[hlen]) << 2));
308 hlen += 2;
309 break;
310 case IP6M_BINDING_ERROR:
311 ND_TCHECK(mh->ip6m_data8[0]);
312 ND_PRINT((ndo, " status=%u", mh->ip6m_data8[0]));
313 /* Reserved */
314 hlen = IP6M_MINLEN;
315 ND_TCHECK2(*mh, hlen + 16);
316 ND_PRINT((ndo, " homeaddr %s", ip6addr_string(ndo, &bp[hlen])));
317 hlen += 16;
318 break;
319 default:
320 ND_PRINT((ndo, " len=%u", mh->ip6m_len));
321 return(mhlen);
322 break;
323 }
324 if (ndo->ndo_vflag)
325 mobility_opt_print(ndo, &bp[hlen], mhlen - hlen);
326
327 return(mhlen);
328
329 trunc:
330 ND_PRINT((ndo, "[|MOBILITY]"));
331 return(mhlen);
332 }
333 #endif /* INET6 */