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