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