]> The Tcpdump Group git mirrors - tcpdump/blob - print-mobility.c
Make "mobility_opt_print()" static, as it's not used outside
[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.4 2002-08-02 04:10:14 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_int16_t ip6m_type; /* message type */
55 u_int16_t ip6m_cksum; /* sum of IPv6 pseudo-header and MH */
56 union {
57 u_int16_t ip6m_un_data16[1]; /* type-specific field */
58 u_int8_t ip6m_un_data8[2]; /* type-specific fiedl */
59 } ip6m_dataun;
60 };
61
62 #define ip6m_data16 ip6m_dataun.ip6m_un_data16
63 #define ip6m_data8 ip6m_dataun.ip6m_un_data8
64
65 #define IP6M_MINLEN 8
66
67 /* message type */
68 #define IP6M_BINDING_REQUEST 0x0000 /* Binding Refresh Request */
69 #define IP6M_HOME_TEST_INIT 0x0001 /* Home Test Init */
70 #define IP6M_CAREOF_TEST_INIT 0x0002 /* Care-of Test Init */
71 #define IP6M_HOME_TEST 0x0003 /* Home Test */
72 #define IP6M_CAREOF_TEST 0x0004 /* Care-of Test */
73 #define IP6M_BINDING_UPDATE 0x0005 /* Binding Update */
74 #define IP6M_BINDING_ACK 0x0006 /* Binding Acknowledgement */
75 #define IP6M_BINDING_ERROR 0x0007 /* Binding Error */
76
77 /* Mobility Header Options */
78 #define IP6MOPT_MINLEN 2
79 #define IP6MOPT_PAD1 0x0
80 #define IP6MOPT_PADN 0x1
81 #define IP6MOPT_UI 0x2
82 #define IP6MOPT_UI_MINLEN 4
83 #define IP6MOPT_ALTCOA 0x3
84 #define IP6MOPT_ALTCOA_MINLEN 18
85 #define IP6MOPT_NONCEID 0x4
86 #define IP6MOPT_NONCEID_MINLEN 6
87 #define IP6MOPT_AUTH 0x5
88 #define IP6MOPT_AUTH_MINLEN 2 /* 2+len */
89
90 static void
91 mobility_opt_print(const u_char *bp, int len)
92 {
93 int i;
94 int optlen;
95
96 for (i = 0; i < len; i += optlen) {
97 if (bp[i] == IP6MOPT_PAD1)
98 optlen = 1;
99 else {
100 if (i + 1 < len)
101 optlen = bp[i + 1];
102 else
103 goto trunc;
104 if (optlen < IP6MOPT_MINLEN)
105 optlen = IP6MOPT_MINLEN; /* XXX */
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_UI:
122 if (len - i < IP6MOPT_UI_MINLEN) {
123 printf("(ui: trunc)");
124 goto trunc;
125 }
126 printf("(ui: 0x%04x)", EXTRACT_16BITS(&bp[i+2]));
127 break;
128 case IP6MOPT_ALTCOA:
129 if (len - i < IP6MOPT_ALTCOA_MINLEN) {
130 printf("(altcoa: trunc)");
131 goto trunc;
132 }
133 printf("(alt-CoA: %s)", ip6addr_string(&bp[i+2]));
134 break;
135 case IP6MOPT_NONCEID:
136 if (len - i < IP6MOPT_NONCEID_MINLEN) {
137 printf("(ni: trunc)");
138 goto trunc;
139 }
140 printf("(ni: ho=0x%04x ci=0x%04x)",
141 EXTRACT_16BITS(&bp[i+2]),
142 EXTRACT_16BITS(&bp[i+4]));
143 break;
144 case IP6MOPT_AUTH:
145 if (len - i < IP6MOPT_AUTH_MINLEN) {
146 printf("(auth: trunc)");
147 goto trunc;
148 }
149 printf("(auth spi: 0x%08x)",
150 EXTRACT_32BITS(&bp[i+2]));
151 break;
152 default:
153 if (len - i < IP6MOPT_MINLEN) {
154 printf("(sopt_type %d: trunc)", bp[i]);
155 goto trunc;
156 }
157 printf("(type-0x%02x: len=%d)", bp[i], bp[i + 1]);
158 break;
159 }
160 }
161 return;
162
163 trunc:
164 printf("[trunc] ");
165 }
166
167 /*
168 * Mobility Header
169 */
170 int
171 mobility_print(const u_char *bp, const u_char *bp2)
172 {
173 const struct ip6_mobility *mh;
174 const struct ip6_hdr *ip6;
175 const u_char *ep;
176 int mhlen, hlen, type;
177
178 mh = (struct ip6_mobility *)bp;
179 ip6 = (struct ip6_hdr *)bp2;
180
181 /* 'ep' points to the end of available data. */
182 ep = snapend;
183
184 if (!TTEST(mh->ip6m_len)) {
185 /*
186 * There's not enough captured data to include the
187 * mobility header length.
188 *
189 * Our caller expects us to return the length, however,
190 * so return a value that will run to the end of the
191 * captured data.
192 *
193 * XXX - "ip6_print()" doesn't do anything with the
194 * returned length, however, as it breaks out of the
195 * header-processing loop.
196 */
197 mhlen = ep - bp;
198 goto trunc;
199 }
200 mhlen = (int)(mh->ip6m_len << 3);
201 if (mhlen < IP6M_MINLEN)
202 mhlen = IP6M_MINLEN; /* XXX */
203
204 /* XXX ip6m_cksum */
205
206 TCHECK(mh->ip6m_type);
207 type = ntohs(mh->ip6m_type);
208 switch (type) {
209 case IP6M_BINDING_REQUEST:
210 printf("mobility: BRR");
211 hlen = IP6M_MINLEN;
212 break;
213 case IP6M_HOME_TEST_INIT:
214 case IP6M_CAREOF_TEST_INIT:
215 printf("mobility: %soTI",
216 type == IP6M_HOME_TEST_INIT ? "H" : "C");
217 hlen = IP6M_MINLEN;
218 TCHECK2(*mh, hlen + 4);
219 printf(" cookie=0x%x", EXTRACT_32BITS(&bp[hlen]));
220 hlen += 4;
221 break;
222 case IP6M_HOME_TEST:
223 case IP6M_CAREOF_TEST:
224 printf("mobility: %soT",
225 type == IP6M_HOME_TEST ? "H" : "C");
226 hlen = IP6M_MINLEN;
227 TCHECK2(*mh, hlen + 2);
228 printf(" nonce id=0x%x", EXTRACT_16BITS(&bp[hlen]));
229 hlen += 2;
230 /* Reserved (16bits) */
231 hlen += 2;
232 TCHECK2(*mh, hlen + 4);
233 printf(" mobile cookie=0x%x", EXTRACT_32BITS(&bp[hlen]));
234 hlen += 4;
235 /* Home(Care-of) Cookie (128 bits) */
236 hlen += 16;
237 break;
238 case IP6M_BINDING_UPDATE:
239 printf("mobility: BU");
240 TCHECK(mh->ip6m_data8[0]);
241 if (mh->ip6m_data8[0] & 0xf0)
242 printf(" ");
243 if (mh->ip6m_data8[0] & 0x80)
244 printf("A");
245 if (mh->ip6m_data8[0] & 0x40)
246 printf("H");
247 if (mh->ip6m_data8[0] & 0x20)
248 printf("S");
249 if (mh->ip6m_data8[0] & 0x10)
250 printf("D");
251 hlen = IP6M_MINLEN;
252 TCHECK2(*mh, hlen + 2);
253 printf(" seq#=%d", EXTRACT_16BITS(&bp[hlen]));
254 hlen += 2;
255 /* Reserved (16bits) */
256 hlen += 2;
257 TCHECK2(*mh, hlen + 4);
258 printf(" lifetime=%d", EXTRACT_32BITS(&bp[hlen]));
259 hlen += 4;
260 TCHECK2(*mh, hlen + 16);
261 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
262 hlen += 16;
263 break;
264 case IP6M_BINDING_ACK:
265 printf("mobility: BA");
266 TCHECK(mh->ip6m_data8[0]);
267 printf(" status=%d", mh->ip6m_data8[0]);
268 hlen = IP6M_MINLEN;
269 TCHECK2(*mh, hlen + 2);
270 printf(" seq#=%d", EXTRACT_16BITS(&bp[hlen]));
271 hlen += 2;
272 /* Reserved (16bits) */
273 hlen += 2;
274 TCHECK2(*mh, hlen + 4);
275 printf(" lifetime=%d", EXTRACT_32BITS(&bp[hlen]));
276 hlen += 4;
277 TCHECK2(*mh, hlen + 4);
278 printf(" refresh=%d", ntohl(*(u_int32_t *)&bp[hlen]));
279 hlen += 4;
280 break;
281 case IP6M_BINDING_ERROR:
282 printf("mobility: BE");
283 TCHECK(mh->ip6m_data8[0]);
284 printf(" status=%d", mh->ip6m_data8[0]);
285 hlen = IP6M_MINLEN;
286 TCHECK2(*mh, hlen + 16);
287 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
288 hlen += 16;
289 break;
290 default:
291 printf("mobility: type-#%d len=%d", type, mh->ip6m_len);
292 return(mhlen);
293 break;
294 }
295 if (vflag)
296 mobility_opt_print(&bp[hlen], mhlen - hlen);
297
298 return(mhlen);
299
300 trunc:
301 fputs("[|MOBILITY]", stdout);
302 return(mhlen);
303 }
304 #endif /* INET6 */