xref: /freebsd/contrib/tcpdump/print-dhcp6.c (revision 685295f4d740a26b206b4cae691262196b887f34)
1b0453382SBill Fenner /*
2b0453382SBill Fenner  * Copyright (C) 1998 and 1999 WIDE Project.
3b0453382SBill Fenner  * All rights reserved.
4b0453382SBill Fenner  *
5b0453382SBill Fenner  * Redistribution and use in source and binary forms, with or without
6b0453382SBill Fenner  * modification, are permitted provided that the following conditions
7b0453382SBill Fenner  * are met:
8b0453382SBill Fenner  * 1. Redistributions of source code must retain the above copyright
9b0453382SBill Fenner  *    notice, this list of conditions and the following disclaimer.
10b0453382SBill Fenner  * 2. Redistributions in binary form must reproduce the above copyright
11b0453382SBill Fenner  *    notice, this list of conditions and the following disclaimer in the
12b0453382SBill Fenner  *    documentation and/or other materials provided with the distribution.
13b0453382SBill Fenner  * 3. Neither the name of the project nor the names of its contributors
14b0453382SBill Fenner  *    may be used to endorse or promote products derived from this software
15b0453382SBill Fenner  *    without specific prior written permission.
16b0453382SBill Fenner  *
17b0453382SBill Fenner  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18b0453382SBill Fenner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b0453382SBill Fenner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b0453382SBill Fenner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21b0453382SBill Fenner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b0453382SBill Fenner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b0453382SBill Fenner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b0453382SBill Fenner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b0453382SBill Fenner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b0453382SBill Fenner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b0453382SBill Fenner  * SUCH DAMAGE.
28b0453382SBill Fenner  */
29b0453382SBill Fenner 
30b0453382SBill Fenner #ifndef lint
31b0453382SBill Fenner static const char rcsid[] =
32685295f4SBill Fenner     "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.12 2000/10/24 00:56:50 fenner Exp $";
33b0453382SBill Fenner #endif
34b0453382SBill Fenner 
35b0453382SBill Fenner #ifdef HAVE_CONFIG_H
36b0453382SBill Fenner #include "config.h"
37b0453382SBill Fenner #endif
38b0453382SBill Fenner 
39b0453382SBill Fenner #include <sys/param.h>
40b0453382SBill Fenner #include <sys/time.h>
41b0453382SBill Fenner #include <sys/socket.h>
42b0453382SBill Fenner 
43b0453382SBill Fenner struct mbuf;
44b0453382SBill Fenner struct rtentry;
45b0453382SBill Fenner 
46b0453382SBill Fenner #include <netinet/in.h>
47b0453382SBill Fenner 
48b0453382SBill Fenner #include <ctype.h>
49b0453382SBill Fenner #include <stdio.h>
50b0453382SBill Fenner #include <string.h>
51b0453382SBill Fenner #include <arpa/inet.h>
52b0453382SBill Fenner 
53b0453382SBill Fenner #include "interface.h"
54b0453382SBill Fenner #include "addrtoname.h"
55b0453382SBill Fenner #include "dhcp6.h"
56b0453382SBill Fenner #include "dhcp6opt.h"
57b0453382SBill Fenner 
58b0453382SBill Fenner #if 0
59685295f4SBill Fenner static void dhcp6opttab_init (void);
60685295f4SBill Fenner static struct dhcp6_opt *dhcp6opttab_byname (char *);
61b0453382SBill Fenner #endif
62685295f4SBill Fenner static struct dhcp6_opt *dhcp6opttab_bycode (u_int);
63b0453382SBill Fenner 
64b0453382SBill Fenner static char tstr[] = " [|dhcp6]";
65b0453382SBill Fenner 
66b0453382SBill Fenner static struct dhcp6_opt dh6opttab[] = {
67b0453382SBill Fenner 	/* IP Address Extension */
68b0453382SBill Fenner 	{ 1, OL6_N,	"IP Address",			OT6_NONE, },
69b0453382SBill Fenner 
70b0453382SBill Fenner 	/* General Extension */
71685295f4SBill Fenner 	{ 8193, OL6_N,	"IEEE 1003.1 POSIX Timezone",	OT6_STR, },
72685295f4SBill Fenner 	{ 8194, OL6_16N, "Domain Name Server",		OT6_V6, },
73685295f4SBill Fenner 	{ 8195, OL6_N,	"Domain Name",			OT6_STR, },
74b0453382SBill Fenner 
75685295f4SBill Fenner 	{ 8196, OL6_N,	"SLP Agent",			OT6_NONE, },
76685295f4SBill Fenner 	{ 8197, OL6_N,	"SLP Scope"	,		OT6_NONE, },
77685295f4SBill Fenner 	{ 8198, OL6_16N, "Network Time Protocol Servers", OT6_V6, },
78685295f4SBill Fenner 	{ 8199, OL6_N,	"NIS Domain",			OT6_STR, },
79685295f4SBill Fenner 	{ 8200, OL6_16N, "NIS Servers",			OT6_V6, },
80685295f4SBill Fenner 	{ 8201, OL6_N,	"NIS+ Domain",			OT6_STR, },
81685295f4SBill Fenner 	{ 8202, OL6_16N, "NIS+ Servers",		OT6_V6, },
82b0453382SBill Fenner 
83b0453382SBill Fenner 	/* TCP Parameters */
84685295f4SBill Fenner 	{ 8203, 4,	"TCP Keepalive Interval",	OT6_NUM, },
85b0453382SBill Fenner 
86b0453382SBill Fenner 	/* DHCPv6 Extensions */
87685295f4SBill Fenner 	{ 8204, 4,	"Maximum DHCPv6 Message Size",	OT6_NUM, },
88685295f4SBill Fenner 	{ 8205, OL6_N,	"DHCP Retransmission and Configuration Parameter",
89b0453382SBill Fenner 							OT6_NONE, },
90685295f4SBill Fenner 	{ 8206, OL6_N,	"Extension Request",		OT6_NONE, },
91685295f4SBill Fenner 	{ 8207, OL6_N,	"Subnet Prefix",		OT6_NONE, },
92685295f4SBill Fenner 	{ 8208, OL6_N,	"Platform Specific Information", OT6_NONE, },
93685295f4SBill Fenner 	{ 8209, OL6_N,	"Platform Class Identifier",	OT6_STR, },
94685295f4SBill Fenner 	{ 8210, OL6_N,	"Class Identifier",		OT6_STR, },
95685295f4SBill Fenner 	{ 8211, 16,	"Reconfigure Multicast Address", OT6_V6, },
96685295f4SBill Fenner 	{ 8212, 16,	"Renumber DHCPv6 Server Address",
97b0453382SBill Fenner 							OT6_V6, },
98685295f4SBill Fenner 	{ 8213, OL6_N,	"Client-Server Authentication",	OT6_NONE, },
99685295f4SBill Fenner 	{ 8214, 4,	"Client Key Selection",		OT6_NUM, },
100b0453382SBill Fenner 
101b0453382SBill Fenner 	/* End Extension */
102b0453382SBill Fenner 	{ 65536, OL6_Z,	"End",				OT6_NONE, },
103b0453382SBill Fenner 
104b0453382SBill Fenner 	{ 0 },
105b0453382SBill Fenner };
106b0453382SBill Fenner 
107b0453382SBill Fenner #if 0
108b0453382SBill Fenner static struct dhcp6_opt *dh6o_pad;
109b0453382SBill Fenner static struct dhcp6_opt *dh6o_end;
110b0453382SBill Fenner 
111b0453382SBill Fenner static void
112b0453382SBill Fenner dhcp6opttab_init()
113b0453382SBill Fenner {
114b0453382SBill Fenner 	dh6o_pad = dhcp6opttab_bycode(0);
115b0453382SBill Fenner 	dh6o_end = dhcp6opttab_bycode(65536);
116b0453382SBill Fenner }
117b0453382SBill Fenner #endif
118b0453382SBill Fenner 
119b0453382SBill Fenner #if 0
120b0453382SBill Fenner static struct dhcp6_opt *
121b0453382SBill Fenner dhcp6opttab_byname(name)
122b0453382SBill Fenner 	char *name;
123b0453382SBill Fenner {
124b0453382SBill Fenner 	struct dhcp6_opt *p;
125b0453382SBill Fenner 
126b0453382SBill Fenner 	for (p = dh6opttab; p->code; p++)
127b0453382SBill Fenner 		if (strcmp(name, p->name) == 0)
128b0453382SBill Fenner 			return p;
129b0453382SBill Fenner 	return NULL;
130b0453382SBill Fenner }
131b0453382SBill Fenner #endif
132b0453382SBill Fenner 
133b0453382SBill Fenner static struct dhcp6_opt *
134b0453382SBill Fenner dhcp6opttab_bycode(code)
135b0453382SBill Fenner 	u_int code;
136b0453382SBill Fenner {
137b0453382SBill Fenner 	struct dhcp6_opt *p;
138b0453382SBill Fenner 
139b0453382SBill Fenner 	for (p = dh6opttab; p->code; p++)
140b0453382SBill Fenner 		if (p->code == code)
141b0453382SBill Fenner 			return p;
142b0453382SBill Fenner 	return NULL;
143b0453382SBill Fenner }
144b0453382SBill Fenner 
145b0453382SBill Fenner static void
146b0453382SBill Fenner dhcp6ext_print(u_char *cp, u_char *ep)
147b0453382SBill Fenner {
148b0453382SBill Fenner 	u_int16_t code, len;
149b0453382SBill Fenner 	struct dhcp6_opt *p;
150b0453382SBill Fenner 	char buf[BUFSIZ];
151b0453382SBill Fenner 	int i;
152b0453382SBill Fenner 
153b0453382SBill Fenner 	if (cp == ep)
154b0453382SBill Fenner 		return;
155b0453382SBill Fenner 	while (cp < ep) {
156685295f4SBill Fenner 		if (ep - cp < sizeof(u_int16_t))
157685295f4SBill Fenner 			break;
158b0453382SBill Fenner 		code = ntohs(*(u_int16_t *)&cp[0]);
159685295f4SBill Fenner 		if (ep - cp < sizeof(u_int16_t) * 2)
160685295f4SBill Fenner 			break;
161b0453382SBill Fenner 		if (code != 65535)
162b0453382SBill Fenner 			len = ntohs(*(u_int16_t *)&cp[2]);
163b0453382SBill Fenner 		else
164b0453382SBill Fenner 			len = 0;
165685295f4SBill Fenner 		if (ep - cp < len + 4)
166685295f4SBill Fenner 			break;
167b0453382SBill Fenner 		p = dhcp6opttab_bycode(code);
168b0453382SBill Fenner 		if (p == NULL) {
169b0453382SBill Fenner 			printf("(unknown, len=%d)", len);
170b0453382SBill Fenner 			cp += len + 4;
171b0453382SBill Fenner 			continue;
172b0453382SBill Fenner 		}
173b0453382SBill Fenner 
174b0453382SBill Fenner 		/* sanity check on length */
175b0453382SBill Fenner 		switch (p->len) {
176b0453382SBill Fenner 		case OL6_N:
177b0453382SBill Fenner 			break;
178b0453382SBill Fenner 		case OL6_16N:
179b0453382SBill Fenner 			if (len % 16 != 0)
180b0453382SBill Fenner 				goto trunc;
181b0453382SBill Fenner 			break;
182b0453382SBill Fenner 		case OL6_Z:
183b0453382SBill Fenner 			if (len != 0)
184b0453382SBill Fenner 				goto trunc;
185b0453382SBill Fenner 			break;
186b0453382SBill Fenner 		default:
187b0453382SBill Fenner 			if (len != p->len)
188b0453382SBill Fenner 				goto trunc;
189b0453382SBill Fenner 			break;
190b0453382SBill Fenner 		}
191b0453382SBill Fenner 		if (cp + 4 + len > ep) {
192b0453382SBill Fenner 			printf(" [|%s]", p->name);
193b0453382SBill Fenner 			return;
194b0453382SBill Fenner 		}
195b0453382SBill Fenner 
196b0453382SBill Fenner 		printf(" (%s, ", p->name);
197b0453382SBill Fenner 		switch (p->type) {
198b0453382SBill Fenner 		case OT6_V6:
199b0453382SBill Fenner 			for (i = 0; i < len; i += 16) {
200b0453382SBill Fenner 				inet_ntop(AF_INET6, &cp[4 + i], buf,
201b0453382SBill Fenner 					sizeof(buf));
202b0453382SBill Fenner 				if (i != 0)
203b0453382SBill Fenner 					printf(",");
204b0453382SBill Fenner 				printf("%s", buf);
205b0453382SBill Fenner 			}
206b0453382SBill Fenner 			break;
207b0453382SBill Fenner 		case OT6_STR:
208b0453382SBill Fenner 			memset(&buf, 0, sizeof(buf));
209b0453382SBill Fenner 			strncpy(buf, &cp[4], len);
210b0453382SBill Fenner 			printf("%s", buf);
211b0453382SBill Fenner 			break;
212b0453382SBill Fenner 		case OT6_NUM:
213b0453382SBill Fenner 			printf("%d", (u_int32_t)ntohl(*(u_int32_t *)&cp[4]));
214b0453382SBill Fenner 			break;
215b0453382SBill Fenner 		default:
216b0453382SBill Fenner 			for (i = 0; i < len; i++)
217b0453382SBill Fenner 				printf("%02x", cp[4 + i] & 0xff);
218b0453382SBill Fenner 		}
219b0453382SBill Fenner 		printf(")");
220b0453382SBill Fenner 		cp += len + 4;
221b0453382SBill Fenner 	}
222b0453382SBill Fenner 	return;
223b0453382SBill Fenner 
224b0453382SBill Fenner trunc:
225b0453382SBill Fenner 	printf("[|dhcp6ext]");
226b0453382SBill Fenner }
227b0453382SBill Fenner 
228b0453382SBill Fenner /*
229b0453382SBill Fenner  * Print dhcp6 requests
230b0453382SBill Fenner  */
231b0453382SBill Fenner void
232b0453382SBill Fenner dhcp6_print(register const u_char *cp, u_int length,
233685295f4SBill Fenner 	    u_int16_t sport, u_int16_t dport)
234b0453382SBill Fenner {
235b0453382SBill Fenner 	union dhcp6 *dh6;
236b0453382SBill Fenner 	u_char *ep;
237b0453382SBill Fenner 	u_char *extp;
238685295f4SBill Fenner 	u_int16_t field16;
239b0453382SBill Fenner 
240b0453382SBill Fenner 	printf("dhcp6");
241b0453382SBill Fenner 
242b0453382SBill Fenner 	ep = (u_char *)snapend;
243b0453382SBill Fenner 
244b0453382SBill Fenner 	dh6 = (union dhcp6 *)cp;
245b0453382SBill Fenner 	TCHECK(dh6->dh6_msgtype);
246b0453382SBill Fenner 	switch (dh6->dh6_msgtype) {
247b0453382SBill Fenner 	case DH6_SOLICIT:
248685295f4SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_sol.dh6sol_relayaddr))) {
249685295f4SBill Fenner 			printf(" solicit");
250685295f4SBill Fenner 			break;
251685295f4SBill Fenner 		}
252685295f4SBill Fenner 
253685295f4SBill Fenner 		printf(" solicit (");	/*)*/
254685295f4SBill Fenner 		if (dh6->dh6_sol.dh6sol_flags != 0) {
255685295f4SBill Fenner 			u_int8_t f = dh6->dh6_sol.dh6sol_flags;
256685295f4SBill Fenner 			printf("%s%s ",
257685295f4SBill Fenner 			   (f & DH6SOL_PREFIX) ? "P" : "",
258685295f4SBill Fenner 			   (f & DH6SOL_CLOSE) ? "C" : "");
259685295f4SBill Fenner 		}
260685295f4SBill Fenner 
261685295f4SBill Fenner 		memcpy(&field16, &dh6->dh6_sol.dh6sol_plen_id,
262685295f4SBill Fenner 		       sizeof(field16));
263685295f4SBill Fenner 		field16 = ntohs(field16);
264685295f4SBill Fenner 		if (field16 & ~DH6SOL_SOLICIT_PLEN_MASK)
265685295f4SBill Fenner 			printf("plen=%d ", DH6SOL_SOLICIT_PLEN(field16));
266685295f4SBill Fenner 		printf("solicit-ID=%d", DH6SOL_SOLICIT_ID(field16));
267685295f4SBill Fenner 
268b0453382SBill Fenner 		printf(" cliaddr=%s",
269b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
270b0453382SBill Fenner 		printf(" relayaddr=%s",
271b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
272685295f4SBill Fenner 		/*(*/
273b0453382SBill Fenner 		printf(")");
274b0453382SBill Fenner 		break;
275b0453382SBill Fenner 	case DH6_ADVERT:
276b0453382SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
277b0453382SBill Fenner 			printf(" advert");
278b0453382SBill Fenner 			break;
279b0453382SBill Fenner 		}
280685295f4SBill Fenner 		printf(" advert (");	/*)*/
281685295f4SBill Fenner 		memcpy(&field16, &dh6->dh6_adv.dh6adv_rsv_id, sizeof(field16));
282685295f4SBill Fenner 		printf("solicit-ID=%d",
283685295f4SBill Fenner 		       ntohs(field16) & DH6SOL_SOLICIT_ID_MASK);
284b0453382SBill Fenner 		printf(" pref=%u", dh6->dh6_adv.dh6adv_pref);
285b0453382SBill Fenner 		printf(" cliaddr=%s",
286b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
287b0453382SBill Fenner 		printf(" relayaddr=%s",
288b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
289b0453382SBill Fenner 		printf(" servaddr=%s",
290b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
291b0453382SBill Fenner 		extp = (u_char *)((&dh6->dh6_adv) + 1);
292b0453382SBill Fenner 		dhcp6ext_print(extp, ep);
293685295f4SBill Fenner 		/*(*/
294b0453382SBill Fenner 		printf(")");
295b0453382SBill Fenner 		break;
296b0453382SBill Fenner 	case DH6_REQUEST:
297b0453382SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
298b0453382SBill Fenner 			printf(" request");
299b0453382SBill Fenner 			break;
300b0453382SBill Fenner 		}
301685295f4SBill Fenner 		printf(" request (");	/*)*/
302685295f4SBill Fenner 		if (dh6->dh6_req.dh6req_flags != 0) {
303685295f4SBill Fenner 			u_int8_t f = dh6->dh6_req.dh6req_flags;
304685295f4SBill Fenner 			printf("%s%s ",
305685295f4SBill Fenner 			   (f & DH6REQ_CLOSE) ? "C" : "",
306685295f4SBill Fenner 			   (f & DH6REQ_REBOOT) ? "R" : "");
307685295f4SBill Fenner 		}
308b0453382SBill Fenner 		printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
309b0453382SBill Fenner 		printf(" cliaddr=%s",
310b0453382SBill Fenner 		       ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
311b0453382SBill Fenner 		printf(" relayaddr=%s",
312b0453382SBill Fenner 		       ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
313685295f4SBill Fenner 		printf(" servaddr=%s",
314685295f4SBill Fenner 		       ip6addr_string(&dh6->dh6_req.dh6req_serveraddr));
315685295f4SBill Fenner 		dhcp6ext_print((char *)(&dh6->dh6_req + 1), ep);
316685295f4SBill Fenner 		/*(*/
317b0453382SBill Fenner 		printf(")");
318b0453382SBill Fenner 		break;
319b0453382SBill Fenner 	case DH6_REPLY:
320b0453382SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
321b0453382SBill Fenner 			printf(" reply");
322b0453382SBill Fenner 			break;
323b0453382SBill Fenner 		}
324685295f4SBill Fenner 		printf(" reply (");	/*)*/
325685295f4SBill Fenner 		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) != 0)
326685295f4SBill Fenner 			printf("R ");
327b0453382SBill Fenner 		printf("stat=0x%02x",
328b0453382SBill Fenner 			dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
329685295f4SBill Fenner 		printf(" xid=0x%04x", dh6->dh6_rep.dh6rep_xid);
330685295f4SBill Fenner 		printf(" cliaddr=%s",
331685295f4SBill Fenner 		       ip6addr_string(&dh6->dh6_rep.dh6rep_cliaddr));
332b0453382SBill Fenner 		extp = (u_char *)((&dh6->dh6_rep) + 1);
333685295f4SBill Fenner 		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) !=
334685295f4SBill Fenner 		    0) {
335685295f4SBill Fenner 			printf(" relayaddr=%s", ip6addr_string(extp));
336685295f4SBill Fenner 			extp += sizeof(struct in6_addr);
337b0453382SBill Fenner 		}
338b0453382SBill Fenner 		dhcp6ext_print(extp, ep);
339685295f4SBill Fenner 		/*(*/
340b0453382SBill Fenner 		printf(")");
341b0453382SBill Fenner 		break;
342b0453382SBill Fenner 	case DH6_RELEASE:
343b0453382SBill Fenner 		printf(" release");
344b0453382SBill Fenner 		break;
345b0453382SBill Fenner 	case DH6_RECONFIG:
346b0453382SBill Fenner 		printf(" reconfig");
347b0453382SBill Fenner 		break;
348b0453382SBill Fenner 	}
349b0453382SBill Fenner 	return;
350b0453382SBill Fenner 
351b0453382SBill Fenner trunc:
352b0453382SBill Fenner 	printf("%s", tstr);
353b0453382SBill Fenner }
354