Drop ".i0", it is an undefined macro.
[netbsd-mini2440.git] / dist / tcpdump / print-mobility.c
blob850feacefaf26ac79ef78781ba9516b2c8efe187
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2002 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static const char rcsid[] _U_ =
40 "@(#) Header: /tcpdump/master/tcpdump/print-mobility.c,v 1.11.2.1 2005/04/20 22:21:16 guy Exp";
41 #else
42 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
43 #endif
44 #endif
46 #ifdef INET6
47 #include <tcpdump-stdinc.h>
49 #include <stdio.h>
51 #include "ip6.h"
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "extract.h" /* must come after interface.h */
57 /* Mobility header */
58 struct ip6_mobility {
59 u_int8_t ip6m_pproto; /* following payload protocol (for PG) */
60 u_int8_t ip6m_len; /* length in units of 8 octets */
61 u_int8_t ip6m_type; /* message type */
62 u_int8_t reserved; /* reserved */
63 u_int16_t ip6m_cksum; /* sum of IPv6 pseudo-header and MH */
64 union {
65 u_int16_t ip6m_un_data16[1]; /* type-specific field */
66 u_int8_t ip6m_un_data8[2]; /* type-specific fiedl */
67 } ip6m_dataun;
70 #define ip6m_data16 ip6m_dataun.ip6m_un_data16
71 #define ip6m_data8 ip6m_dataun.ip6m_un_data8
73 #define IP6M_MINLEN 8
75 /* message type */
76 #define IP6M_BINDING_REQUEST 0 /* Binding Refresh Request */
77 #define IP6M_HOME_TEST_INIT 1 /* Home Test Init */
78 #define IP6M_CAREOF_TEST_INIT 2 /* Care-of Test Init */
79 #define IP6M_HOME_TEST 3 /* Home Test */
80 #define IP6M_CAREOF_TEST 4 /* Care-of Test */
81 #define IP6M_BINDING_UPDATE 5 /* Binding Update */
82 #define IP6M_BINDING_ACK 6 /* Binding Acknowledgement */
83 #define IP6M_BINDING_ERROR 7 /* Binding Error */
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
98 static void
99 mobility_opt_print(const u_char *bp, int len)
101 int i;
102 int optlen;
104 for (i = 0; i < len; i += optlen) {
105 if (bp[i] == IP6MOPT_PAD1)
106 optlen = 1;
107 else {
108 if (i + 1 < len)
109 optlen = bp[i + 1] + 2;
110 else
111 goto trunc;
113 if (i + optlen > len)
114 goto trunc;
116 switch (bp[i]) {
117 case IP6MOPT_PAD1:
118 printf("(pad1)");
119 break;
120 case IP6MOPT_PADN:
121 if (len - i < IP6MOPT_MINLEN) {
122 printf("(padn: trunc)");
123 goto trunc;
125 printf("(padn)");
126 break;
127 case IP6MOPT_REFRESH:
128 if (len - i < IP6MOPT_REFRESH_MINLEN) {
129 printf("(refresh: trunc)");
130 goto trunc;
132 /* units of 4 secs */
133 printf("(refresh: %d)",
134 EXTRACT_16BITS(&bp[i+2]) << 2);
135 break;
136 case IP6MOPT_ALTCOA:
137 if (len - i < IP6MOPT_ALTCOA_MINLEN) {
138 printf("(altcoa: trunc)");
139 goto trunc;
141 printf("(alt-CoA: %s)", ip6addr_string(&bp[i+2]));
142 break;
143 case IP6MOPT_NONCEID:
144 if (len - i < IP6MOPT_NONCEID_MINLEN) {
145 printf("(ni: trunc)");
146 goto trunc;
148 printf("(ni: ho=0x%04x co=0x%04x)",
149 EXTRACT_16BITS(&bp[i+2]),
150 EXTRACT_16BITS(&bp[i+4]));
151 break;
152 case IP6MOPT_AUTH:
153 if (len - i < IP6MOPT_AUTH_MINLEN) {
154 printf("(auth: trunc)");
155 goto trunc;
157 printf("(auth)");
158 break;
159 default:
160 if (len - i < IP6MOPT_MINLEN) {
161 printf("(sopt_type %d: trunc)", bp[i]);
162 goto trunc;
164 printf("(type-0x%02x: len=%d)", bp[i], bp[i + 1]);
165 break;
168 return;
170 trunc:
171 printf("[trunc] ");
175 * Mobility Header
178 mobility_print(const u_char *bp, const u_char *bp2 _U_)
180 const struct ip6_mobility *mh;
181 const u_char *ep;
182 int mhlen, hlen, type;
184 mh = (struct ip6_mobility *)bp;
186 /* 'ep' points to the end of available data. */
187 ep = snapend;
189 if (!TTEST(mh->ip6m_len)) {
191 * There's not enough captured data to include the
192 * mobility header length.
194 * Our caller expects us to return the length, however,
195 * so return a value that will run to the end of the
196 * captured data.
198 * XXX - "ip6_print()" doesn't do anything with the
199 * returned length, however, as it breaks out of the
200 * header-processing loop.
202 mhlen = ep - bp;
203 goto trunc;
205 mhlen = (int)((mh->ip6m_len + 1) << 3);
207 /* XXX ip6m_cksum */
209 TCHECK(mh->ip6m_type);
210 type = mh->ip6m_type;
211 switch (type) {
212 case IP6M_BINDING_REQUEST:
213 printf("mobility: BRR");
214 hlen = IP6M_MINLEN;
215 break;
216 case IP6M_HOME_TEST_INIT:
217 case IP6M_CAREOF_TEST_INIT:
218 printf("mobility: %soTI",
219 type == IP6M_HOME_TEST_INIT ? "H" : "C");
220 hlen = IP6M_MINLEN;
221 if (vflag) {
222 TCHECK2(*mh, hlen + 8);
223 printf(" %s Init Cookie=%08x:%08x",
224 type == IP6M_HOME_TEST_INIT ? "Home" : "Care-of",
225 EXTRACT_32BITS(&bp[hlen]),
226 EXTRACT_32BITS(&bp[hlen + 4]));
228 hlen += 8;
229 break;
230 case IP6M_HOME_TEST:
231 case IP6M_CAREOF_TEST:
232 printf("mobility: %soT",
233 type == IP6M_HOME_TEST ? "H" : "C");
234 TCHECK(mh->ip6m_data16[0]);
235 printf(" nonce id=0x%x", EXTRACT_16BITS(&mh->ip6m_data16[0]));
236 hlen = IP6M_MINLEN;
237 if (vflag) {
238 TCHECK2(*mh, hlen + 8);
239 printf(" %s Init Cookie=%08x:%08x",
240 type == IP6M_HOME_TEST ? "Home" : "Care-of",
241 EXTRACT_32BITS(&bp[hlen]),
242 EXTRACT_32BITS(&bp[hlen + 4]));
244 hlen += 8;
245 if (vflag) {
246 TCHECK2(*mh, hlen + 8);
247 printf(" %s Keygen Token=%08x:%08x",
248 type == IP6M_HOME_TEST ? "Home" : "Care-of",
249 EXTRACT_32BITS(&bp[hlen]),
250 EXTRACT_32BITS(&bp[hlen + 4]));
252 hlen += 8;
253 break;
254 case IP6M_BINDING_UPDATE:
255 printf("mobility: BU");
256 TCHECK(mh->ip6m_data16[0]);
257 printf(" seq#=%d", EXTRACT_16BITS(&mh->ip6m_data16[0]));
258 hlen = IP6M_MINLEN;
259 TCHECK2(*mh, hlen + 1);
260 if (bp[hlen] & 0xf0)
261 printf(" ");
262 if (bp[hlen] & 0x80)
263 printf("A");
264 if (bp[hlen] & 0x40)
265 printf("H");
266 if (bp[hlen] & 0x20)
267 printf("L");
268 if (bp[hlen] & 0x10)
269 printf("K");
270 /* Reserved (4bits) */
271 hlen += 1;
272 /* Reserved (8bits) */
273 hlen += 1;
274 TCHECK2(*mh, hlen + 2);
275 /* units of 4 secs */
276 printf(" lifetime=%d", EXTRACT_16BITS(&bp[hlen]) << 2);
277 hlen += 2;
278 break;
279 case IP6M_BINDING_ACK:
280 printf("mobility: BA");
281 TCHECK(mh->ip6m_data8[0]);
282 printf(" status=%d", mh->ip6m_data8[0]);
283 if (mh->ip6m_data8[1] & 0x80)
284 printf(" K");
285 /* Reserved (7bits) */
286 hlen = IP6M_MINLEN;
287 TCHECK2(*mh, hlen + 2);
288 printf(" seq#=%d", EXTRACT_16BITS(&bp[hlen]));
289 hlen += 2;
290 TCHECK2(*mh, hlen + 2);
291 /* units of 4 secs */
292 printf(" lifetime=%d", EXTRACT_16BITS(&bp[hlen]) << 2);
293 hlen += 2;
294 break;
295 case IP6M_BINDING_ERROR:
296 printf("mobility: BE");
297 TCHECK(mh->ip6m_data8[0]);
298 printf(" status=%d", mh->ip6m_data8[0]);
299 /* Reserved */
300 hlen = IP6M_MINLEN;
301 TCHECK2(*mh, hlen + 16);
302 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
303 hlen += 16;
304 break;
305 default:
306 printf("mobility: type-#%d len=%d", type, mh->ip6m_len);
307 return(mhlen);
308 break;
310 if (vflag)
311 mobility_opt_print(&bp[hlen], mhlen - hlen);
313 return(mhlen);
315 trunc:
316 fputs("[|MOBILITY]", stdout);
317 return(mhlen);
319 #endif /* INET6 */