]> The Tcpdump Group git mirrors - tcpdump/blob - print-mobility.c
9decad75fe10484f326e04142cca5412e77c5b62
[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 /* \summary: IPv6 mobility printer */
31 /* RFC 3775 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "netdissect-stdinc.h"
38
39 #include "netdissect.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 #include "ip6.h"
44
45
46 /* Mobility header */
47 struct ip6_mobility {
48 nd_uint8_t ip6m_pproto; /* following payload protocol (for PG) */
49 nd_uint8_t ip6m_len; /* length in units of 8 octets */
50 nd_uint8_t ip6m_type; /* message type */
51 nd_uint8_t reserved; /* reserved */
52 nd_uint16_t ip6m_cksum; /* sum of IPv6 pseudo-header and MH */
53 union {
54 nd_uint16_t ip6m_un_data16[1]; /* type-specific field */
55 nd_uint8_t ip6m_un_data8[2]; /* type-specific field */
56 } ip6m_dataun;
57 };
58
59 #define ip6m_data16 ip6m_dataun.ip6m_un_data16
60 #define ip6m_data8 ip6m_dataun.ip6m_un_data8
61
62 #define IP6M_MINLEN 8
63
64 /* https://www.iana.org/assignments/mobility-parameters/mobility-parameters.xhtml */
65
66 /* message type */
67 #define IP6M_BINDING_REQUEST 0 /* Binding Refresh Request */
68 #define IP6M_HOME_TEST_INIT 1 /* Home Test Init */
69 #define IP6M_CAREOF_TEST_INIT 2 /* Care-of Test Init */
70 #define IP6M_HOME_TEST 3 /* Home Test */
71 #define IP6M_CAREOF_TEST 4 /* Care-of Test */
72 #define IP6M_BINDING_UPDATE 5 /* Binding Update */
73 #define IP6M_BINDING_ACK 6 /* Binding Acknowledgement */
74 #define IP6M_BINDING_ERROR 7 /* Binding Error */
75 #define IP6M_MAX 7
76
77 static const struct tok ip6m_str[] = {
78 { IP6M_BINDING_REQUEST, "BRR" },
79 { IP6M_HOME_TEST_INIT, "HoTI" },
80 { IP6M_CAREOF_TEST_INIT, "CoTI" },
81 { IP6M_HOME_TEST, "HoT" },
82 { IP6M_CAREOF_TEST, "CoT" },
83 { IP6M_BINDING_UPDATE, "BU" },
84 { IP6M_BINDING_ACK, "BA" },
85 { IP6M_BINDING_ERROR, "BE" },
86 { 0, NULL }
87 };
88
89 static const unsigned ip6m_hdrlen[IP6M_MAX + 1] = {
90 IP6M_MINLEN, /* IP6M_BINDING_REQUEST */
91 IP6M_MINLEN + 8, /* IP6M_HOME_TEST_INIT */
92 IP6M_MINLEN + 8, /* IP6M_CAREOF_TEST_INIT */
93 IP6M_MINLEN + 16, /* IP6M_HOME_TEST */
94 IP6M_MINLEN + 16, /* IP6M_CAREOF_TEST */
95 IP6M_MINLEN + 4, /* IP6M_BINDING_UPDATE */
96 IP6M_MINLEN + 4, /* IP6M_BINDING_ACK */
97 IP6M_MINLEN + 16, /* IP6M_BINDING_ERROR */
98 };
99
100 /* Mobility Header Options */
101 #define IP6MOPT_MINLEN 2
102 #define IP6MOPT_PAD1 0x0 /* Pad1 */
103 #define IP6MOPT_PADN 0x1 /* PadN */
104 #define IP6MOPT_REFRESH 0x2 /* Binding Refresh Advice */
105 #define IP6MOPT_REFRESH_MINLEN 4
106 #define IP6MOPT_ALTCOA 0x3 /* Alternate Care-of Address */
107 #define IP6MOPT_ALTCOA_MINLEN 18
108 #define IP6MOPT_NONCEID 0x4 /* Nonce Indices */
109 #define IP6MOPT_NONCEID_MINLEN 6
110 #define IP6MOPT_AUTH 0x5 /* Binding Authorization Data */
111 #define IP6MOPT_AUTH_MINLEN 12
112
113 static const struct tok ip6m_binding_update_bits [] = {
114 { 0x08, "A" },
115 { 0x04, "H" },
116 { 0x02, "L" },
117 { 0x01, "K" },
118 { 0, NULL }
119 };
120
121 static int
122 mobility_opt_print(netdissect_options *ndo,
123 const u_char *bp, const unsigned len)
124 {
125 unsigned i, optlen;
126
127 for (i = 0; i < len; i += optlen) {
128 if (GET_U_1(bp + i) == IP6MOPT_PAD1)
129 optlen = 1;
130 else {
131 if (i + 1 < len) {
132 optlen = GET_U_1(bp + i + 1) + 2;
133 } else
134 goto trunc;
135 }
136 if (i + optlen > len)
137 goto trunc;
138 ND_TCHECK_1(bp + i + optlen);
139
140 switch (GET_U_1(bp + i)) {
141 case IP6MOPT_PAD1:
142 ND_PRINT("(pad1)");
143 break;
144 case IP6MOPT_PADN:
145 if (len - i < IP6MOPT_MINLEN) {
146 ND_PRINT("(padn: trunc)");
147 goto trunc;
148 }
149 ND_PRINT("(padn)");
150 break;
151 case IP6MOPT_REFRESH:
152 if (len - i < IP6MOPT_REFRESH_MINLEN) {
153 ND_PRINT("(refresh: trunc)");
154 goto trunc;
155 }
156 /* units of 4 secs */
157 ND_PRINT("(refresh: %u)",
158 GET_BE_U_2(bp + i + 2) << 2);
159 break;
160 case IP6MOPT_ALTCOA:
161 if (len - i < IP6MOPT_ALTCOA_MINLEN) {
162 ND_PRINT("(altcoa: trunc)");
163 goto trunc;
164 }
165 ND_PRINT("(alt-CoA: %s)", GET_IP6ADDR_STRING(bp + i + 2));
166 break;
167 case IP6MOPT_NONCEID:
168 if (len - i < IP6MOPT_NONCEID_MINLEN) {
169 ND_PRINT("(ni: trunc)");
170 goto trunc;
171 }
172 ND_PRINT("(ni: ho=0x%04x co=0x%04x)",
173 GET_BE_U_2(bp + i + 2),
174 GET_BE_U_2(bp + i + 4));
175 break;
176 case IP6MOPT_AUTH:
177 if (len - i < IP6MOPT_AUTH_MINLEN) {
178 ND_PRINT("(auth: trunc)");
179 goto trunc;
180 }
181 ND_PRINT("(auth)");
182 break;
183 default:
184 if (len - i < IP6MOPT_MINLEN) {
185 ND_PRINT("(sopt_type %u: trunc)",
186 GET_U_1(bp + i));
187 goto trunc;
188 }
189 ND_PRINT("(type-0x%02x: len=%u)", GET_U_1(bp + i),
190 GET_U_1(bp + i + 1));
191 break;
192 }
193 }
194 return 0;
195
196 trunc:
197 return 1;
198 }
199
200 /*
201 * Mobility Header
202 */
203 int
204 mobility_print(netdissect_options *ndo,
205 const u_char *bp, const u_char *bp2 _U_)
206 {
207 const struct ip6_mobility *mh;
208 const u_char *ep;
209 unsigned mhlen, hlen;
210 uint8_t type;
211
212 ndo->ndo_protocol = "mobility";
213 mh = (const struct ip6_mobility *)bp;
214
215 /* 'ep' points to the end of available data. */
216 ep = ndo->ndo_snapend;
217
218 if (!ND_TTEST_1(mh->ip6m_len)) {
219 /*
220 * There's not enough captured data to include the
221 * mobility header length.
222 *
223 * Our caller expects us to return the length, however,
224 * so return a value that will run to the end of the
225 * captured data.
226 *
227 * XXX - "ip6_print()" doesn't do anything with the
228 * returned length, however, as it breaks out of the
229 * header-processing loop.
230 */
231 mhlen = (unsigned)(ep - bp);
232 goto trunc;
233 }
234 mhlen = (GET_U_1(mh->ip6m_len) + 1) << 3;
235
236 /* XXX ip6m_cksum */
237
238 type = GET_U_1(mh->ip6m_type);
239 if (type <= IP6M_MAX && mhlen < ip6m_hdrlen[type]) {
240 ND_PRINT("(header length %u is too small for type %u)", mhlen, type);
241 goto trunc;
242 }
243 ND_PRINT("mobility: %s", tok2str(ip6m_str, "type-#%u", type));
244 switch (type) {
245 case IP6M_BINDING_REQUEST:
246 hlen = IP6M_MINLEN;
247 break;
248 case IP6M_HOME_TEST_INIT:
249 case IP6M_CAREOF_TEST_INIT:
250 hlen = IP6M_MINLEN;
251 if (ndo->ndo_vflag) {
252 ND_PRINT(" %s Init Cookie=%08x:%08x",
253 type == IP6M_HOME_TEST_INIT ? "Home" : "Care-of",
254 GET_BE_U_4(bp + hlen),
255 GET_BE_U_4(bp + hlen + 4));
256 }
257 hlen += 8;
258 break;
259 case IP6M_HOME_TEST:
260 case IP6M_CAREOF_TEST:
261 ND_PRINT(" nonce id=0x%x", GET_BE_U_2(mh->ip6m_data16[0]));
262 hlen = IP6M_MINLEN;
263 if (ndo->ndo_vflag) {
264 ND_PRINT(" %s Init Cookie=%08x:%08x",
265 type == IP6M_HOME_TEST ? "Home" : "Care-of",
266 GET_BE_U_4(bp + hlen),
267 GET_BE_U_4(bp + hlen + 4));
268 }
269 hlen += 8;
270 if (ndo->ndo_vflag) {
271 ND_PRINT(" %s Keygen Token=%08x:%08x",
272 type == IP6M_HOME_TEST ? "Home" : "Care-of",
273 GET_BE_U_4(bp + hlen),
274 GET_BE_U_4(bp + hlen + 4));
275 }
276 hlen += 8;
277 break;
278 case IP6M_BINDING_UPDATE:
279 {
280 int bits;
281 ND_PRINT(" seq#=%u", GET_BE_U_2(mh->ip6m_data16[0]));
282 hlen = IP6M_MINLEN;
283 ND_TCHECK_2(bp + hlen);
284 bits = (GET_U_1(bp + hlen) & 0xf0) >> 4;
285 if (bits) {
286 ND_PRINT(" ");
287 ND_PRINT("%s",
288 bittok2str_nosep(ip6m_binding_update_bits,
289 "bits-#0x%x", bits));
290 }
291 /* Reserved (4bits) */
292 hlen += 1;
293 /* Reserved (8bits) */
294 hlen += 1;
295 /* units of 4 secs */
296 ND_PRINT(" lifetime=%u", GET_BE_U_2(bp + hlen) << 2);
297 hlen += 2;
298 break;
299 }
300 case IP6M_BINDING_ACK:
301 ND_PRINT(" status=%u", GET_U_1(mh->ip6m_data8[0]));
302 if (GET_U_1(mh->ip6m_data8[1]) & 0x80)
303 ND_PRINT(" K");
304 /* Reserved (7bits) */
305 hlen = IP6M_MINLEN;
306 ND_PRINT(" seq#=%u", GET_BE_U_2(bp + hlen));
307 hlen += 2;
308 /* units of 4 secs */
309 ND_PRINT(" lifetime=%u", GET_BE_U_2(bp + hlen) << 2);
310 hlen += 2;
311 break;
312 case IP6M_BINDING_ERROR:
313 ND_PRINT(" status=%u", GET_U_1(mh->ip6m_data8[0]));
314 /* Reserved */
315 hlen = IP6M_MINLEN;
316 ND_PRINT(" homeaddr %s", GET_IP6ADDR_STRING(bp + hlen));
317 hlen += 16;
318 break;
319 default:
320 ND_PRINT(" len=%u", GET_U_1(mh->ip6m_len));
321 return(mhlen);
322 }
323 if (ndo->ndo_vflag)
324 if (mobility_opt_print(ndo, bp + hlen, mhlen - hlen))
325 goto trunc;
326
327 return(mhlen);
328
329 trunc:
330 nd_print_trunc(ndo);
331 return(-1);
332 }