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