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