xref: /openbsd-src/usr.sbin/tcpdump/print-dhcp6.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: print-dhcp6.c,v 1.6 2007/10/07 16:41:05 deraadt Exp $	*/
2 
3 /*
4  * Copyright (C) 1998 and 1999 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char rcsid[] =
34     "@(#) $Id: print-dhcp6.c,v 1.6 2007/10/07 16:41:05 deraadt Exp $";
35 #endif
36 
37 #ifdef INET6
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/socket.h>
42 
43 struct mbuf;
44 struct rtentry;
45 #include <net/if.h>
46 
47 #include <netinet/in.h>
48 
49 #include <ctype.h>
50 #ifdef HAVE_MEMORY_H
51 #include <memory.h>
52 #endif
53 #include <stdio.h>
54 #include <string.h>
55 #include <arpa/inet.h>
56 
57 #include "interface.h"
58 #include "addrtoname.h"
59 #include "dhcp6.h"
60 #include "dhcp6opt.h"
61 
62 #if 0
63 static void dhcp6opttab_init(void);
64 static struct dhcp6_opt *dhcp6opttab_byname(char *);
65 #endif
66 static struct dhcp6_opt *dhcp6opttab_bycode(u_int);
67 
68 static char tstr[] = " [|dhcp6]";
69 
70 static struct dhcp6_opt dh6opttab[] = {
71 	/* IP Address Extension */
72 	{ 1, OL6_N,	"IP Address",			OT6_NONE, },
73 
74 	/* General Extension */
75 	{ 2, 4,		"Time Offset",			OT6_NUM, },
76 	{ 3, OL6_N,	"IEEE 1003.1 POSIX Timezone",	OT6_STR, },
77 	{ 6, OL6_16N,	"Domain Name Server",		OT6_V6, },
78 	{ 10, OL6_N,	"Domain Name",			OT6_STR, },
79 
80 	/* Application and Service Parameters */
81 	{ 16, OL6_N,	"Directory Agent",		OT6_NONE, },
82 	{ 17, OL6_N,	"Service Scope"	,		OT6_NONE, },
83 	{ 18, OL6_16N,	"Network Time Protocol Servers", OT6_V6, },
84 	{ 19, OL6_N,	"NIS Domain",			OT6_STR, },
85 	{ 20, OL6_16N,	"NIS Servers",			OT6_V6, },
86 	{ 21, OL6_N,	"NIS+ Domain",			OT6_STR, },
87 	{ 22, OL6_16N,	"NIS+ Servers",			OT6_V6, },
88 
89 	/* TCP Parameters */
90 	{ 32, 4,	"TCP Keepalive Interval",	OT6_NUM, },
91 
92 	/* DHCPv6 Extensions */
93 	{ 40, 4,	"Maximum DHCPv6 Message Size",	OT6_NUM, },
94 	{ 41, OL6_N,	"DHCP Retransmission and Configuration Parameter",
95 							OT6_NONE, },
96 	{ 48, OL6_N,	"Platform Specific Information", OT6_NONE, },
97 	{ 49, OL6_N,	"Platform Class Identifier",	OT6_STR, },
98 	{ 64, OL6_N,	"Class Identifier",		OT6_STR, },
99 	{ 66, 16,	"Reconfigure Multicast Address", OT6_V6, },
100 	{ 67, 16,	"Renumber DHCPv6 Server Address",
101 							OT6_V6, },
102 	{ 68, OL6_N,	"DHCP Relay ICMP Error Message", OT6_NONE, },
103 	{ 84, OL6_N,	"Client-Server Authentication",	OT6_NONE, },
104 	{ 85, 4,	"Client Key Selection",		OT6_NUM, },
105 
106 	/* End Extension */
107 	{ 65536, OL6_Z,	"End",				OT6_NONE, },
108 
109 	{ 0 },
110 };
111 
112 #if 0
113 static struct dhcp6_opt *dh6o_pad;
114 static struct dhcp6_opt *dh6o_end;
115 
116 static void
117 dhcp6opttab_init()
118 {
119 	dh6o_pad = dhcp6opttab_bycode(0);
120 	dh6o_end = dhcp6opttab_bycode(65536);
121 }
122 #endif
123 
124 #if 0
125 static struct dhcp6_opt *
126 dhcp6opttab_byname(name)
127 	char *name;
128 {
129 	struct dhcp6_opt *p;
130 
131 	for (p = dh6opttab; p->code; p++)
132 		if (strcmp(name, p->name) == 0)
133 			return p;
134 	return NULL;
135 }
136 #endif
137 
138 static struct dhcp6_opt *
139 dhcp6opttab_bycode(code)
140 	u_int code;
141 {
142 	struct dhcp6_opt *p;
143 
144 	for (p = dh6opttab; p->code; p++)
145 		if (p->code == code)
146 			return p;
147 	return NULL;
148 }
149 
150 static void
151 dhcp6ext_print(u_char *cp, u_char *ep)
152 {
153 	u_int16_t code, len;
154 	struct dhcp6_opt *p;
155 	char buf[BUFSIZ];
156 	int i;
157 
158 	if (cp == ep)
159 		return;
160 	printf(" ");
161 	while (cp < ep) {
162 		code = ntohs(*(u_int16_t *)&cp[0]);
163 		if (code != 65535)
164 			len = ntohs(*(u_int16_t *)&cp[2]);
165 		else
166 			len = 0;
167 		p = dhcp6opttab_bycode(code);
168 		if (p == NULL) {
169 			printf("(unknown, len=%d)", len);
170 			cp += len + 4;
171 			continue;
172 		}
173 
174 		/* sanity check on length */
175 		switch (p->len) {
176 		case OL6_N:
177 			break;
178 		case OL6_16N:
179 			if (len % 16 != 0)
180 				goto trunc;
181 			break;
182 		case OL6_Z:
183 			if (len != 0)
184 				goto trunc;
185 			break;
186 		default:
187 			if (len != p->len)
188 				goto trunc;
189 			break;
190 		}
191 		if (cp + 4 + len > ep) {
192 			printf("[|%s]", p->name);
193 			return;
194 		}
195 
196 		printf("(%s, ", p->name);
197 		switch (p->type) {
198 		case OT6_V6:
199 			for (i = 0; i < len; i += 16) {
200 				inet_ntop(AF_INET6, &cp[4 + i], buf,
201 					sizeof(buf));
202 				if (i != 0)
203 					printf(",");
204 				printf("%s", buf);
205 			}
206 			break;
207 		case OT6_STR:
208 			memset(&buf, 0, sizeof(buf));
209 			strlcpy(buf, &cp[4], len);
210 			printf("%s", buf);
211 			break;
212 		case OT6_NUM:
213 			printf("%d", (u_int32_t)ntohl(*(u_int32_t *)&cp[4]));
214 			break;
215 		default:
216 			for (i = 0; i < len; i++)
217 				printf("%02x", cp[4 + i] & 0xff);
218 		}
219 		printf(")");
220 		cp += len + 4;
221 	}
222 	return;
223 
224 trunc:
225 	printf("[|dhcp6ext]");
226 }
227 
228 /*
229  * Print dhcp6 requests
230  */
231 void
232 dhcp6_print(register const u_char *cp, u_int length,
233 	    u_short sport, u_short dport)
234 {
235 	union dhcp6 *dh6;
236 	u_char *ep;
237 	u_char *extp;
238 
239 	printf("dhcp6");
240 
241 	ep = (u_char *)snapend;
242 
243 	dh6 = (union dhcp6 *)cp;
244 	TCHECK(dh6->dh6_msgtype);
245 	switch (dh6->dh6_msgtype) {
246 	case DH6_SOLICIT:
247 		if (vflag && TTEST(dh6->dh6_sol.dh6sol_relayaddr)) {
248 			printf(" solicit(");
249 			if ((dh6->dh6_sol.dh6sol_flags & DH6SOL_CLOSE) != 0)
250 				printf("C");
251 			if (dh6->dh6_sol.dh6sol_flags != 0)
252 				printf(" ");
253 			printf("cliaddr=%s",
254 				ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
255 			printf(" relayaddr=%s",
256 				ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
257 			printf(")");
258 		} else
259 			printf(" solicit");
260 		break;
261 	case DH6_ADVERT:
262 		if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
263 			printf(" advert");
264 			break;
265 		}
266 		printf(" advert(");
267 		if ((dh6->dh6_adv.dh6adv_flags & DH6ADV_SERVPRESENT) != 0)
268 			printf("S");
269 		if (dh6->dh6_adv.dh6adv_flags != 0)
270 			printf(" ");
271 		printf("pref=%u", dh6->dh6_adv.dh6adv_pref);
272 		printf(" cliaddr=%s",
273 			ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
274 		printf(" relayaddr=%s",
275 			ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
276 		printf(" servaddr=%s",
277 			ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
278 		extp = (u_char *)((&dh6->dh6_adv) + 1);
279 		dhcp6ext_print(extp, ep);
280 		printf(")");
281 		break;
282 	case DH6_REQUEST:
283 		if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
284 			printf(" request");
285 			break;
286 		}
287 		printf(" request(");
288 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_CLOSE) != 0)
289 			printf("C");
290 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_SERVPRESENT) != 0)
291 			printf("S");
292 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_REBOOT) != 0)
293 			printf("R");
294 		if (dh6->dh6_req.dh6req_flags != 0)
295 			printf(" ");
296 		printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
297 		printf(" cliaddr=%s",
298 			ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
299 		printf(" relayaddr=%s",
300 			ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
301 		extp = (char *)((&dh6->dh6_req) + 1);
302 		if ((dh6->dh6_req.dh6req_flags & DH6REQ_SERVPRESENT) != 0) {
303 			printf(" servaddr=%s", ip6addr_string(extp));
304 			extp += 16;
305 		}
306 		dhcp6ext_print(extp, ep);
307 		printf(")");
308 		break;
309 	case DH6_REPLY:
310 		if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
311 			printf(" reply");
312 			break;
313 		}
314 		printf(" reply(");
315 		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_CLIPRESENT) != 0)
316 			printf("C");
317 		if (dh6->dh6_rep.dh6rep_flagandstat != 0)
318 			printf(" ");
319 		printf("stat=0x%02x",
320 			dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
321 		extp = (u_char *)((&dh6->dh6_rep) + 1);
322 		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_CLIPRESENT) != 0) {
323 			printf(" cliaddr=%s", ip6addr_string(extp));
324 			extp += 16;
325 		}
326 		dhcp6ext_print(extp, ep);
327 		printf(")");
328 		break;
329 	case DH6_RELEASE:
330 		printf(" release");
331 		break;
332 	case DH6_RECONFIG:
333 		printf(" reconfig");
334 		break;
335 	}
336 	return;
337 
338 trunc:
339 	printf("%s", tstr);
340 }
341 
342 #endif /* INET6 */
343