]> The Tcpdump Group git mirrors - tcpdump/blob - print-mobility.c
Put variables in quotes when testing them in "test" commands, so that
[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.1 2002-06-27 08:22:41 guy Exp $";
37 #endif
38
39 #ifdef INET6
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44
45 #include <netinet/in.h>
46 #include <stdio.h>
47
48 #include "ip6.h"
49
50 #include "interface.h"
51 #include "addrtoname.h"
52 #include "extract.h" /* must come after interface.h */
53
54 /* Mobility header */
55 struct ip6_mobility {
56 u_int8_t ip6m_pproto; /* following payload protocol (for PG) */
57 u_int8_t ip6m_len; /* length in units of 8 octets */
58 u_int16_t ip6m_type; /* message type */
59 u_int16_t ip6m_cksum; /* sum of IPv6 pseudo-header and MH */
60 union {
61 u_int16_t ip6m_un_data16[1]; /* type-specific field */
62 u_int8_t ip6m_un_data8[2]; /* type-specific fiedl */
63 } ip6m_dataun;
64 };
65
66 #define ip6m_data16 ip6m_dataun.ip6m_un_data16
67 #define ip6m_data8 ip6m_dataun.ip6m_un_data8
68
69 #define IP6M_MINLEN 8
70
71 /* message type */
72 #if BYTE_ORDER == BIG_ENDIAN
73 #define IP6M_BINDING_REQUEST 0x0000 /* Binding Refresh Request */
74 #define IP6M_HOME_TEST_INIT 0x0001 /* Home Test Init */
75 #define IP6M_CAREOF_TEST_INIT 0x0002 /* Care-of Test Init */
76 #define IP6M_HOME_TEST 0x0003 /* Home Test */
77 #define IP6M_CAREOF_TEST 0x0004 /* Care-of Test */
78 #define IP6M_BINDING_UPDATE 0x0005 /* Binding Update */
79 #define IP6M_BINDING_ACK 0x0006 /* Binding Acknowledgement */
80 #define IP6M_BINDING_ERROR 0x0007 /* Binding Error */
81 #else /* BYTE_ORDER == LITTLE_ENDIAN */
82 #define IP6M_BINDING_REQUEST 0x0000 /* Binding Refresh Request */
83 #define IP6M_HOME_TEST_INIT 0x0100 /* Home Test Init */
84 #define IP6M_CAREOF_TEST_INIT 0x0200 /* Care-of Test Init */
85 #define IP6M_HOME_TEST 0x0300 /* Home Test */
86 #define IP6M_CAREOF_TEST 0x0400 /* Care-of Test */
87 #define IP6M_BINDING_UPDATE 0x0500 /* Binding Update */
88 #define IP6M_BINDING_ACK 0x0600 /* Binding Acknowledgement */
89 #define IP6M_BINDING_ERROR 0x0700 /* Binding Error */
90 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
91
92 /* Mobility Header Options */
93 #define IP6MOPT_MINLEN 2
94 #define IP6MOPT_PAD1 0x0
95 #define IP6MOPT_PADN 0x1
96 #define IP6MOPT_UI 0x2
97 #define IP6MOPT_UI_MINLEN 4
98 #define IP6MOPT_ALTCOA 0x3
99 #define IP6MOPT_ALTCOA_MINLEN 18
100 #define IP6MOPT_NONCEID 0x4
101 #define IP6MOPT_NONCEID_MINLEN 6
102 #define IP6MOPT_AUTH 0x5
103 #define IP6MOPT_AUTH_MINLEN 2 /* 2+len */
104
105 void
106 mobility_opt_print(const u_char *bp, int len)
107 {
108 int i;
109 int optlen;
110
111 for (i = 0; i < len; i += optlen) {
112 if (bp[i] == IP6MOPT_PAD1)
113 optlen = 1;
114 else {
115 if (i + 1 < len)
116 optlen = bp[i + 1];
117 else
118 goto trunc;
119 if (optlen < IP6MOPT_MINLEN)
120 optlen = IP6MOPT_MINLEN; /* XXX */
121 }
122 if (i + optlen > len)
123 goto trunc;
124
125 switch (bp[i]) {
126 case IP6MOPT_PAD1:
127 printf("(pad1)");
128 break;
129 case IP6MOPT_PADN:
130 if (len - i < IP6MOPT_MINLEN) {
131 printf("(padn: trunc)");
132 goto trunc;
133 }
134 printf("(padn)");
135 break;
136 case IP6MOPT_UI:
137 if (len - i < IP6MOPT_UI_MINLEN) {
138 printf("(ui: trunc)");
139 goto trunc;
140 }
141 printf("(ui: 0x%04x)", ntohs(*(u_int16_t *)&bp[i + 2]));
142 break;
143 case IP6MOPT_ALTCOA:
144 if (len - i < IP6MOPT_ALTCOA_MINLEN) {
145 printf("(altcoa: trunc)");
146 goto trunc;
147 }
148 printf("(alt-CoA: %s)", ip6addr_string(&bp[i+2]));
149 break;
150 case IP6MOPT_NONCEID:
151 if (len - i < IP6MOPT_NONCEID_MINLEN) {
152 printf("(ni: trunc)");
153 goto trunc;
154 }
155 printf("(ni: ho=0x%04x ci=0x%04x)",
156 ntohs(*(u_int16_t *)&bp[i + 2]),
157 ntohs(*(u_int16_t *)&bp[i + 4]));
158 break;
159 case IP6MOPT_AUTH:
160 if (len - i < IP6MOPT_AUTH_MINLEN) {
161 printf("(auth: trunc)");
162 goto trunc;
163 }
164 printf("(auth spi: 0x%08x)",
165 (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 2]));
166 break;
167 default:
168 if (len - i < IP6MOPT_MINLEN) {
169 printf("(sopt_type %d: trunc)", bp[i]);
170 goto trunc;
171 }
172 printf("(type-0x%02x: len=%d)", bp[i], bp[i + 1]);
173 break;
174 }
175 }
176 return;
177
178 trunc:
179 printf("[trunc] ");
180 }
181
182 /*
183 * Mobility Header
184 */
185 int
186 mobility_print(const u_char *bp, const u_char *bp2)
187 {
188 const struct ip6_mobility *mh;
189 const struct ip6_hdr *ip6;
190 const u_char *ep;
191 int mhlen, hlen;
192
193 mh = (struct ip6_mobility *)bp;
194 ip6 = (struct ip6_hdr *)bp2;
195
196 /* 'ep' points to the end of available data. */
197 ep = snapend;
198
199 TCHECK(mh->ip6m_len);
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 switch (mh->ip6m_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 mh->ip6m_type == IP6M_HOME_TEST_INIT ? "H" : "C");
216 hlen = IP6M_MINLEN;
217 TCHECK2(*mh, hlen + 4);
218 printf(" cookie=0x%x", ntohl(*(u_int32_t *)&bp[hlen]));
219 hlen += 4;
220 break;
221 case IP6M_HOME_TEST:
222 case IP6M_CAREOF_TEST:
223 printf("mobility: %soT",
224 mh->ip6m_type == IP6M_HOME_TEST ? "H" : "C");
225 hlen = IP6M_MINLEN;
226 TCHECK2(*mh, hlen + 2);
227 printf(" nonce id=0x%x", ntohs(*(u_int16_t *)&bp[hlen]));
228 hlen += 2;
229 /* Reserved (16bits) */
230 hlen += 2;
231 TCHECK2(*mh, hlen + 4);
232 printf(" mobile cookie=0x%x", ntohl(*(u_int32_t *)&bp[hlen]));
233 hlen += 4;
234 /* Home(Care-of) Cookie (128 bits) */
235 hlen += 16;
236 break;
237 case IP6M_BINDING_UPDATE:
238 printf("mobility: BU");
239 TCHECK(mh->ip6m_data8[0]);
240 if (mh->ip6m_data8[0] & 0xf0)
241 printf(" ");
242 if (mh->ip6m_data8[0] & 0x80)
243 printf("A");
244 if (mh->ip6m_data8[0] & 0x40)
245 printf("H");
246 if (mh->ip6m_data8[0] & 0x20)
247 printf("S");
248 if (mh->ip6m_data8[0] & 0x10)
249 printf("D");
250 hlen = IP6M_MINLEN;
251 TCHECK2(*mh, hlen + 2);
252 printf(" seq#=%d", ntohs(*(u_int16_t *)&bp[hlen]));
253 hlen += 2;
254 /* Reserved (16bits) */
255 hlen += 2;
256 TCHECK2(*mh, hlen + 4);
257 printf(" lifetime=%d", ntohl(*(u_int32_t *)&bp[hlen]));
258 hlen += 4;
259 TCHECK2(*mh, hlen + 16);
260 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
261 hlen += 16;
262 break;
263 case IP6M_BINDING_ACK:
264 printf("mobility: BA");
265 TCHECK(mh->ip6m_data8[0]);
266 printf(" status=%d", mh->ip6m_data8[0]);
267 hlen = IP6M_MINLEN;
268 TCHECK2(*mh, hlen + 2);
269 printf(" seq#=%d", ntohs(*(u_int16_t *)&bp[hlen]));
270 hlen += 2;
271 /* Reserved (16bits) */
272 hlen += 2;
273 TCHECK2(*mh, hlen + 4);
274 printf(" lifetime=%d", ntohl(*(u_int32_t *)&bp[hlen]));
275 hlen += 4;
276 TCHECK2(*mh, hlen + 4);
277 printf(" refresh=%d", ntohl(*(u_int32_t *)&bp[hlen]));
278 hlen += 4;
279 break;
280 case IP6M_BINDING_ERROR:
281 printf("mobility: BE");
282 TCHECK(mh->ip6m_data8[0]);
283 printf(" status=%d", mh->ip6m_data8[0]);
284 hlen = IP6M_MINLEN;
285 TCHECK2(*mh, hlen + 16);
286 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
287 hlen += 16;
288 break;
289 default:
290 printf("mobility: type-#%d len=%d", ntohs(mh->ip6m_type),
291 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 */