]> The Tcpdump Group git mirrors - tcpdump/blob - print-mobility.c
Use EXTRACT_* to be a little more alignment-safe
[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.2 2002-07-08 08:58:37 fenner 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 #define IP6M_BINDING_REQUEST 0x0000 /* Binding Refresh Request */
73 #define IP6M_HOME_TEST_INIT 0x0001 /* Home Test Init */
74 #define IP6M_CAREOF_TEST_INIT 0x0002 /* Care-of Test Init */
75 #define IP6M_HOME_TEST 0x0003 /* Home Test */
76 #define IP6M_CAREOF_TEST 0x0004 /* Care-of Test */
77 #define IP6M_BINDING_UPDATE 0x0005 /* Binding Update */
78 #define IP6M_BINDING_ACK 0x0006 /* Binding Acknowledgement */
79 #define IP6M_BINDING_ERROR 0x0007 /* Binding Error */
80
81 /* Mobility Header Options */
82 #define IP6MOPT_MINLEN 2
83 #define IP6MOPT_PAD1 0x0
84 #define IP6MOPT_PADN 0x1
85 #define IP6MOPT_UI 0x2
86 #define IP6MOPT_UI_MINLEN 4
87 #define IP6MOPT_ALTCOA 0x3
88 #define IP6MOPT_ALTCOA_MINLEN 18
89 #define IP6MOPT_NONCEID 0x4
90 #define IP6MOPT_NONCEID_MINLEN 6
91 #define IP6MOPT_AUTH 0x5
92 #define IP6MOPT_AUTH_MINLEN 2 /* 2+len */
93
94 void
95 mobility_opt_print(const u_char *bp, int len)
96 {
97 int i;
98 int optlen;
99
100 for (i = 0; i < len; i += optlen) {
101 if (bp[i] == IP6MOPT_PAD1)
102 optlen = 1;
103 else {
104 if (i + 1 < len)
105 optlen = bp[i + 1];
106 else
107 goto trunc;
108 if (optlen < IP6MOPT_MINLEN)
109 optlen = IP6MOPT_MINLEN; /* XXX */
110 }
111 if (i + optlen > len)
112 goto trunc;
113
114 switch (bp[i]) {
115 case IP6MOPT_PAD1:
116 printf("(pad1)");
117 break;
118 case IP6MOPT_PADN:
119 if (len - i < IP6MOPT_MINLEN) {
120 printf("(padn: trunc)");
121 goto trunc;
122 }
123 printf("(padn)");
124 break;
125 case IP6MOPT_UI:
126 if (len - i < IP6MOPT_UI_MINLEN) {
127 printf("(ui: trunc)");
128 goto trunc;
129 }
130 printf("(ui: 0x%04x)", EXTRACT_16BITS(&bp[i+2]));
131 break;
132 case IP6MOPT_ALTCOA:
133 if (len - i < IP6MOPT_ALTCOA_MINLEN) {
134 printf("(altcoa: trunc)");
135 goto trunc;
136 }
137 printf("(alt-CoA: %s)", ip6addr_string(&bp[i+2]));
138 break;
139 case IP6MOPT_NONCEID:
140 if (len - i < IP6MOPT_NONCEID_MINLEN) {
141 printf("(ni: trunc)");
142 goto trunc;
143 }
144 printf("(ni: ho=0x%04x ci=0x%04x)",
145 EXTRACT_16BITS(&bp[i+2]),
146 EXTRACT_16BITS(&bp[i+4]));
147 break;
148 case IP6MOPT_AUTH:
149 if (len - i < IP6MOPT_AUTH_MINLEN) {
150 printf("(auth: trunc)");
151 goto trunc;
152 }
153 printf("(auth spi: 0x%08x)",
154 EXTRACT_32BITS(&bp[i+2]));
155 break;
156 default:
157 if (len - i < IP6MOPT_MINLEN) {
158 printf("(sopt_type %d: trunc)", bp[i]);
159 goto trunc;
160 }
161 printf("(type-0x%02x: len=%d)", bp[i], bp[i + 1]);
162 break;
163 }
164 }
165 return;
166
167 trunc:
168 printf("[trunc] ");
169 }
170
171 /*
172 * Mobility Header
173 */
174 int
175 mobility_print(const u_char *bp, const u_char *bp2)
176 {
177 const struct ip6_mobility *mh;
178 const struct ip6_hdr *ip6;
179 const u_char *ep;
180 int mhlen, hlen, type;
181
182 mh = (struct ip6_mobility *)bp;
183 ip6 = (struct ip6_hdr *)bp2;
184
185 /* 'ep' points to the end of available data. */
186 ep = snapend;
187
188 TCHECK(mh->ip6m_len);
189 mhlen = (int)(mh->ip6m_len << 3);
190 if (mhlen < IP6M_MINLEN)
191 mhlen = IP6M_MINLEN; /* XXX */
192
193 /* XXX ip6m_cksum */
194
195 TCHECK(mh->ip6m_type);
196 type = ntohs(mh->ip6m_type);
197 switch (type) {
198 case IP6M_BINDING_REQUEST:
199 printf("mobility: BRR");
200 hlen = IP6M_MINLEN;
201 break;
202 case IP6M_HOME_TEST_INIT:
203 case IP6M_CAREOF_TEST_INIT:
204 printf("mobility: %soTI",
205 type == IP6M_HOME_TEST_INIT ? "H" : "C");
206 hlen = IP6M_MINLEN;
207 TCHECK2(*mh, hlen + 4);
208 printf(" cookie=0x%x", EXTRACT_32BITS(&bp[hlen]));
209 hlen += 4;
210 break;
211 case IP6M_HOME_TEST:
212 case IP6M_CAREOF_TEST:
213 printf("mobility: %soT",
214 type == IP6M_HOME_TEST ? "H" : "C");
215 hlen = IP6M_MINLEN;
216 TCHECK2(*mh, hlen + 2);
217 printf(" nonce id=0x%x", EXTRACT_16BITS(&bp[hlen]));
218 hlen += 2;
219 /* Reserved (16bits) */
220 hlen += 2;
221 TCHECK2(*mh, hlen + 4);
222 printf(" mobile cookie=0x%x", EXTRACT_32BITS(&bp[hlen]));
223 hlen += 4;
224 /* Home(Care-of) Cookie (128 bits) */
225 hlen += 16;
226 break;
227 case IP6M_BINDING_UPDATE:
228 printf("mobility: BU");
229 TCHECK(mh->ip6m_data8[0]);
230 if (mh->ip6m_data8[0] & 0xf0)
231 printf(" ");
232 if (mh->ip6m_data8[0] & 0x80)
233 printf("A");
234 if (mh->ip6m_data8[0] & 0x40)
235 printf("H");
236 if (mh->ip6m_data8[0] & 0x20)
237 printf("S");
238 if (mh->ip6m_data8[0] & 0x10)
239 printf("D");
240 hlen = IP6M_MINLEN;
241 TCHECK2(*mh, hlen + 2);
242 printf(" seq#=%d", EXTRACT_16BITS(&bp[hlen]));
243 hlen += 2;
244 /* Reserved (16bits) */
245 hlen += 2;
246 TCHECK2(*mh, hlen + 4);
247 printf(" lifetime=%d", EXTRACT_32BITS(&bp[hlen]));
248 hlen += 4;
249 TCHECK2(*mh, hlen + 16);
250 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
251 hlen += 16;
252 break;
253 case IP6M_BINDING_ACK:
254 printf("mobility: BA");
255 TCHECK(mh->ip6m_data8[0]);
256 printf(" status=%d", mh->ip6m_data8[0]);
257 hlen = IP6M_MINLEN;
258 TCHECK2(*mh, hlen + 2);
259 printf(" seq#=%d", EXTRACT_16BITS(&bp[hlen]));
260 hlen += 2;
261 /* Reserved (16bits) */
262 hlen += 2;
263 TCHECK2(*mh, hlen + 4);
264 printf(" lifetime=%d", EXTRACT_32BITS(&bp[hlen]));
265 hlen += 4;
266 TCHECK2(*mh, hlen + 4);
267 printf(" refresh=%d", ntohl(*(u_int32_t *)&bp[hlen]));
268 hlen += 4;
269 break;
270 case IP6M_BINDING_ERROR:
271 printf("mobility: BE");
272 TCHECK(mh->ip6m_data8[0]);
273 printf(" status=%d", mh->ip6m_data8[0]);
274 hlen = IP6M_MINLEN;
275 TCHECK2(*mh, hlen + 16);
276 printf(" homeaddr %s", ip6addr_string(&bp[hlen]));
277 hlen += 16;
278 break;
279 default:
280 printf("mobility: type-#%d len=%d", type, mh->ip6m_len);
281 return(mhlen);
282 break;
283 }
284 if (vflag)
285 mobility_opt_print(&bp[hlen], mhlen - hlen);
286
287 return(mhlen);
288
289 trunc:
290 fputs("[|MOBILITY]", stdout);
291 return(mhlen);
292 }
293 #endif /* INET6 */