xref: /freebsd/contrib/tcpdump/print-dhcp6.c (revision b0453382235492c8e30b09659b52d784128ca7d0)
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[] =
32b0453382SBill Fenner     "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.3 1999/12/22 06:27:20 itojun 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 #if __STDC__
44b0453382SBill Fenner struct mbuf;
45b0453382SBill Fenner struct rtentry;
46b0453382SBill Fenner #endif
47b0453382SBill Fenner #include <net/if.h>
48b0453382SBill Fenner 
49b0453382SBill Fenner #include <netinet/in.h>
50b0453382SBill Fenner 
51b0453382SBill Fenner #include <ctype.h>
52b0453382SBill Fenner #ifdef HAVE_MEMORY_H
53b0453382SBill Fenner #include <memory.h>
54b0453382SBill Fenner #endif
55b0453382SBill Fenner #include <stdio.h>
56b0453382SBill Fenner #include <string.h>
57b0453382SBill Fenner #include <arpa/inet.h>
58b0453382SBill Fenner 
59b0453382SBill Fenner #include "interface.h"
60b0453382SBill Fenner #include "addrtoname.h"
61b0453382SBill Fenner #include "dhcp6.h"
62b0453382SBill Fenner #include "dhcp6opt.h"
63b0453382SBill Fenner 
64b0453382SBill Fenner #if 0
65b0453382SBill Fenner static void dhcp6opttab_init __P((void));
66b0453382SBill Fenner static struct dhcp6_opt *dhcp6opttab_byname __P((char *));
67b0453382SBill Fenner #endif
68b0453382SBill Fenner static struct dhcp6_opt *dhcp6opttab_bycode __P((u_int));
69b0453382SBill Fenner 
70b0453382SBill Fenner static char tstr[] = " [|dhcp6]";
71b0453382SBill Fenner 
72b0453382SBill Fenner static struct dhcp6_opt dh6opttab[] = {
73b0453382SBill Fenner 	/* IP Address Extension */
74b0453382SBill Fenner 	{ 1, OL6_N,	"IP Address",			OT6_NONE, },
75b0453382SBill Fenner 
76b0453382SBill Fenner 	/* General Extension */
77b0453382SBill Fenner 	{ 2, 4,		"Time Offset",			OT6_NUM, },
78b0453382SBill Fenner 	{ 3, OL6_N,	"IEEE 1003.1 POSIX Timezone",	OT6_STR, },
79b0453382SBill Fenner 	{ 6, OL6_16N,	"Domain Name Server",		OT6_V6, },
80b0453382SBill Fenner 	{ 10, OL6_N,	"Domain Name",			OT6_STR, },
81b0453382SBill Fenner 
82b0453382SBill Fenner 	/* Application and Service Parameters */
83b0453382SBill Fenner 	{ 16, OL6_N,	"Directory Agent",		OT6_NONE, },
84b0453382SBill Fenner 	{ 17, OL6_N,	"Service Scope"	,		OT6_NONE, },
85b0453382SBill Fenner 	{ 18, OL6_16N,	"Network Time Protocol Servers", OT6_V6, },
86b0453382SBill Fenner 	{ 19, OL6_N,	"NIS Domain",			OT6_STR, },
87b0453382SBill Fenner 	{ 20, OL6_16N,	"NIS Servers",			OT6_V6, },
88b0453382SBill Fenner 	{ 21, OL6_N,	"NIS+ Domain",			OT6_STR, },
89b0453382SBill Fenner 	{ 22, OL6_16N,	"NIS+ Servers",			OT6_V6, },
90b0453382SBill Fenner 
91b0453382SBill Fenner 	/* TCP Parameters */
92b0453382SBill Fenner 	{ 32, 4,	"TCP Keepalive Interval",	OT6_NUM, },
93b0453382SBill Fenner 
94b0453382SBill Fenner 	/* DHCPv6 Extensions */
95b0453382SBill Fenner 	{ 40, 4,	"Maximum DHCPv6 Message Size",	OT6_NUM, },
96b0453382SBill Fenner 	{ 41, OL6_N,	"DHCP Retransmission and Configuration Parameter",
97b0453382SBill Fenner 							OT6_NONE, },
98b0453382SBill Fenner 	{ 48, OL6_N,	"Platform Specific Information", OT6_NONE, },
99b0453382SBill Fenner 	{ 49, OL6_N,	"Platform Class Identifier",	OT6_STR, },
100b0453382SBill Fenner 	{ 64, OL6_N,	"Class Identifier",		OT6_STR, },
101b0453382SBill Fenner 	{ 66, 16,	"Reconfigure Multicast Address", OT6_V6, },
102b0453382SBill Fenner 	{ 67, 16,	"Renumber DHCPv6 Server Address",
103b0453382SBill Fenner 							OT6_V6, },
104b0453382SBill Fenner 	{ 68, OL6_N,	"DHCP Relay ICMP Error Message", OT6_NONE, },
105b0453382SBill Fenner 	{ 84, OL6_N,	"Client-Server Authentication",	OT6_NONE, },
106b0453382SBill Fenner 	{ 85, 4,	"Client Key Selection",		OT6_NUM, },
107b0453382SBill Fenner 
108b0453382SBill Fenner 	/* End Extension */
109b0453382SBill Fenner 	{ 65536, OL6_Z,	"End",				OT6_NONE, },
110b0453382SBill Fenner 
111b0453382SBill Fenner 	{ 0 },
112b0453382SBill Fenner };
113b0453382SBill Fenner 
114b0453382SBill Fenner #if 0
115b0453382SBill Fenner static struct dhcp6_opt *dh6o_pad;
116b0453382SBill Fenner static struct dhcp6_opt *dh6o_end;
117b0453382SBill Fenner 
118b0453382SBill Fenner static void
119b0453382SBill Fenner dhcp6opttab_init()
120b0453382SBill Fenner {
121b0453382SBill Fenner 	dh6o_pad = dhcp6opttab_bycode(0);
122b0453382SBill Fenner 	dh6o_end = dhcp6opttab_bycode(65536);
123b0453382SBill Fenner }
124b0453382SBill Fenner #endif
125b0453382SBill Fenner 
126b0453382SBill Fenner #if 0
127b0453382SBill Fenner static struct dhcp6_opt *
128b0453382SBill Fenner dhcp6opttab_byname(name)
129b0453382SBill Fenner 	char *name;
130b0453382SBill Fenner {
131b0453382SBill Fenner 	struct dhcp6_opt *p;
132b0453382SBill Fenner 
133b0453382SBill Fenner 	for (p = dh6opttab; p->code; p++)
134b0453382SBill Fenner 		if (strcmp(name, p->name) == 0)
135b0453382SBill Fenner 			return p;
136b0453382SBill Fenner 	return NULL;
137b0453382SBill Fenner }
138b0453382SBill Fenner #endif
139b0453382SBill Fenner 
140b0453382SBill Fenner static struct dhcp6_opt *
141b0453382SBill Fenner dhcp6opttab_bycode(code)
142b0453382SBill Fenner 	u_int code;
143b0453382SBill Fenner {
144b0453382SBill Fenner 	struct dhcp6_opt *p;
145b0453382SBill Fenner 
146b0453382SBill Fenner 	for (p = dh6opttab; p->code; p++)
147b0453382SBill Fenner 		if (p->code == code)
148b0453382SBill Fenner 			return p;
149b0453382SBill Fenner 	return NULL;
150b0453382SBill Fenner }
151b0453382SBill Fenner 
152b0453382SBill Fenner static void
153b0453382SBill Fenner dhcp6ext_print(u_char *cp, u_char *ep)
154b0453382SBill Fenner {
155b0453382SBill Fenner 	u_int16_t code, len;
156b0453382SBill Fenner 	struct dhcp6_opt *p;
157b0453382SBill Fenner 	char buf[BUFSIZ];
158b0453382SBill Fenner 	int i;
159b0453382SBill Fenner 
160b0453382SBill Fenner 	if (cp == ep)
161b0453382SBill Fenner 		return;
162b0453382SBill Fenner 	printf(" ");
163b0453382SBill Fenner 	while (cp < ep) {
164b0453382SBill Fenner 		code = ntohs(*(u_int16_t *)&cp[0]);
165b0453382SBill Fenner 		if (code != 65535)
166b0453382SBill Fenner 			len = ntohs(*(u_int16_t *)&cp[2]);
167b0453382SBill Fenner 		else
168b0453382SBill Fenner 			len = 0;
169b0453382SBill Fenner 		p = dhcp6opttab_bycode(code);
170b0453382SBill Fenner 		if (p == NULL) {
171b0453382SBill Fenner 			printf("(unknown, len=%d)", len);
172b0453382SBill Fenner 			cp += len + 4;
173b0453382SBill Fenner 			continue;
174b0453382SBill Fenner 		}
175b0453382SBill Fenner 
176b0453382SBill Fenner 		/* sanity check on length */
177b0453382SBill Fenner 		switch (p->len) {
178b0453382SBill Fenner 		case OL6_N:
179b0453382SBill Fenner 			break;
180b0453382SBill Fenner 		case OL6_16N:
181b0453382SBill Fenner 			if (len % 16 != 0)
182b0453382SBill Fenner 				goto trunc;
183b0453382SBill Fenner 			break;
184b0453382SBill Fenner 		case OL6_Z:
185b0453382SBill Fenner 			if (len != 0)
186b0453382SBill Fenner 				goto trunc;
187b0453382SBill Fenner 			break;
188b0453382SBill Fenner 		default:
189b0453382SBill Fenner 			if (len != p->len)
190b0453382SBill Fenner 				goto trunc;
191b0453382SBill Fenner 			break;
192b0453382SBill Fenner 		}
193b0453382SBill Fenner 		if (cp + 4 + len > ep) {
194b0453382SBill Fenner 			printf("[|%s]", p->name);
195b0453382SBill Fenner 			return;
196b0453382SBill Fenner 		}
197b0453382SBill Fenner 
198b0453382SBill Fenner 		printf("(%s, ", p->name);
199b0453382SBill Fenner 		switch (p->type) {
200b0453382SBill Fenner 		case OT6_V6:
201b0453382SBill Fenner 			for (i = 0; i < len; i += 16) {
202b0453382SBill Fenner 				inet_ntop(AF_INET6, &cp[4 + i], buf,
203b0453382SBill Fenner 					sizeof(buf));
204b0453382SBill Fenner 				if (i != 0)
205b0453382SBill Fenner 					printf(",");
206b0453382SBill Fenner 				printf("%s", buf);
207b0453382SBill Fenner 			}
208b0453382SBill Fenner 			break;
209b0453382SBill Fenner 		case OT6_STR:
210b0453382SBill Fenner 			memset(&buf, 0, sizeof(buf));
211b0453382SBill Fenner 			strncpy(buf, &cp[4], len);
212b0453382SBill Fenner 			printf("%s", buf);
213b0453382SBill Fenner 			break;
214b0453382SBill Fenner 		case OT6_NUM:
215b0453382SBill Fenner 			printf("%d", (u_int32_t)ntohl(*(u_int32_t *)&cp[4]));
216b0453382SBill Fenner 			break;
217b0453382SBill Fenner 		default:
218b0453382SBill Fenner 			for (i = 0; i < len; i++)
219b0453382SBill Fenner 				printf("%02x", cp[4 + i] & 0xff);
220b0453382SBill Fenner 		}
221b0453382SBill Fenner 		printf(")");
222b0453382SBill Fenner 		cp += len + 4;
223b0453382SBill Fenner 	}
224b0453382SBill Fenner 	return;
225b0453382SBill Fenner 
226b0453382SBill Fenner trunc:
227b0453382SBill Fenner 	printf("[|dhcp6ext]");
228b0453382SBill Fenner }
229b0453382SBill Fenner 
230b0453382SBill Fenner /*
231b0453382SBill Fenner  * Print dhcp6 requests
232b0453382SBill Fenner  */
233b0453382SBill Fenner void
234b0453382SBill Fenner dhcp6_print(register const u_char *cp, u_int length,
235b0453382SBill Fenner 	    u_short sport, u_short dport)
236b0453382SBill Fenner {
237b0453382SBill Fenner 	union dhcp6 *dh6;
238b0453382SBill Fenner 	u_char *ep;
239b0453382SBill Fenner 	u_char *extp;
240b0453382SBill Fenner 
241b0453382SBill Fenner 	printf("dhcp6");
242b0453382SBill Fenner 
243b0453382SBill Fenner 	ep = (u_char *)snapend;
244b0453382SBill Fenner 
245b0453382SBill Fenner 	dh6 = (union dhcp6 *)cp;
246b0453382SBill Fenner 	TCHECK(dh6->dh6_msgtype);
247b0453382SBill Fenner 	switch (dh6->dh6_msgtype) {
248b0453382SBill Fenner 	case DH6_SOLICIT:
249b0453382SBill Fenner 		if (vflag && TTEST(dh6->dh6_sol.dh6sol_relayaddr)) {
250b0453382SBill Fenner 			printf(" solicit(");
251b0453382SBill Fenner 			if ((dh6->dh6_sol.dh6sol_flags & DH6SOL_CLOSE) != 0)
252b0453382SBill Fenner 				printf("C");
253b0453382SBill Fenner 			if (dh6->dh6_sol.dh6sol_flags != 0)
254b0453382SBill Fenner 				printf(" ");
255b0453382SBill Fenner 			printf("cliaddr=%s",
256b0453382SBill Fenner 				ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
257b0453382SBill Fenner 			printf(" relayaddr=%s",
258b0453382SBill Fenner 				ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
259b0453382SBill Fenner 			printf(")");
260b0453382SBill Fenner 		} else
261b0453382SBill Fenner 			printf(" solicit");
262b0453382SBill Fenner 		break;
263b0453382SBill Fenner 	case DH6_ADVERT:
264b0453382SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
265b0453382SBill Fenner 			printf(" advert");
266b0453382SBill Fenner 			break;
267b0453382SBill Fenner 		}
268b0453382SBill Fenner 		printf(" advert(");
269b0453382SBill Fenner 		if ((dh6->dh6_adv.dh6adv_flags & DH6ADV_SERVPRESENT) != 0)
270b0453382SBill Fenner 			printf("S");
271b0453382SBill Fenner 		if (dh6->dh6_adv.dh6adv_flags != 0)
272b0453382SBill Fenner 			printf(" ");
273b0453382SBill Fenner 		printf("pref=%u", dh6->dh6_adv.dh6adv_pref);
274b0453382SBill Fenner 		printf(" cliaddr=%s",
275b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
276b0453382SBill Fenner 		printf(" relayaddr=%s",
277b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
278b0453382SBill Fenner 		printf(" servaddr=%s",
279b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
280b0453382SBill Fenner 		extp = (u_char *)((&dh6->dh6_adv) + 1);
281b0453382SBill Fenner 		dhcp6ext_print(extp, ep);
282b0453382SBill Fenner 		printf(")");
283b0453382SBill Fenner 		break;
284b0453382SBill Fenner 	case DH6_REQUEST:
285b0453382SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
286b0453382SBill Fenner 			printf(" request");
287b0453382SBill Fenner 			break;
288b0453382SBill Fenner 		}
289b0453382SBill Fenner 		printf(" request(");
290b0453382SBill Fenner 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_CLOSE) != 0)
291b0453382SBill Fenner 			printf("C");
292b0453382SBill Fenner 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_SERVPRESENT) != 0)
293b0453382SBill Fenner 			printf("S");
294b0453382SBill Fenner 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_REBOOT) != 0)
295b0453382SBill Fenner 			printf("R");
296b0453382SBill Fenner 		if (dh6->dh6_req.dh6req_flags != 0)
297b0453382SBill Fenner 			printf(" ");
298b0453382SBill Fenner 		printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
299b0453382SBill Fenner 		printf(" cliaddr=%s",
300b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
301b0453382SBill Fenner 		printf(" relayaddr=%s",
302b0453382SBill Fenner 			ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
303b0453382SBill Fenner 		extp = (char *)((&dh6->dh6_req) + 1);
304b0453382SBill Fenner 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_SERVPRESENT) != 0) {
305b0453382SBill Fenner 			printf(" servaddr=%s", ip6addr_string(extp));
306b0453382SBill Fenner 			extp += 16;
307b0453382SBill Fenner 		}
308b0453382SBill Fenner 		dhcp6ext_print(extp, ep);
309b0453382SBill Fenner 		printf(")");
310b0453382SBill Fenner 		break;
311b0453382SBill Fenner 	case DH6_REPLY:
312b0453382SBill Fenner 		if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
313b0453382SBill Fenner 			printf(" reply");
314b0453382SBill Fenner 			break;
315b0453382SBill Fenner 		}
316b0453382SBill Fenner 		printf(" reply(");
317b0453382SBill Fenner 		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_CLIPRESENT) != 0)
318b0453382SBill Fenner 			printf("C");
319b0453382SBill Fenner 		if (dh6->dh6_rep.dh6rep_flagandstat != 0)
320b0453382SBill Fenner 			printf(" ");
321b0453382SBill Fenner 		printf("stat=0x%02x",
322b0453382SBill Fenner 			dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
323b0453382SBill Fenner 		extp = (u_char *)((&dh6->dh6_rep) + 1);
324b0453382SBill Fenner 		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_CLIPRESENT) != 0) {
325b0453382SBill Fenner 			printf(" cliaddr=%s", ip6addr_string(extp));
326b0453382SBill Fenner 			extp += 16;
327b0453382SBill Fenner 		}
328b0453382SBill Fenner 		dhcp6ext_print(extp, ep);
329b0453382SBill Fenner 		printf(")");
330b0453382SBill Fenner 		break;
331b0453382SBill Fenner 	case DH6_RELEASE:
332b0453382SBill Fenner 		printf(" release");
333b0453382SBill Fenner 		break;
334b0453382SBill Fenner 	case DH6_RECONFIG:
335b0453382SBill Fenner 		printf(" reconfig");
336b0453382SBill Fenner 		break;
337b0453382SBill Fenner 	}
338b0453382SBill Fenner 	return;
339b0453382SBill Fenner 
340b0453382SBill Fenner trunc:
341b0453382SBill Fenner 	printf("%s", tstr);
342b0453382SBill Fenner }
343