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