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