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