print-dhcp6.c revision 98524
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
31static const char rcsid[] =
32    "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.14 2001/09/17 21:57:59 fenner 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
43struct mbuf;
44struct rtentry;
45
46#include <netinet/in.h>
47
48#include <ctype.h>
49#include <stdio.h>
50#include <string.h>
51#include <arpa/inet.h>
52
53#include "interface.h"
54#include "addrtoname.h"
55#include "dhcp6.h"
56#include "dhcp6opt.h"
57
58#if 0
59static void dhcp6opttab_init(void);
60static struct dhcp6_opt *dhcp6opttab_byname(char *);
61#endif
62static struct dhcp6_opt *dhcp6opttab_bycode(u_int);
63
64static const char tstr[] = " [|dhcp6]";
65
66static struct dhcp6_opt dh6opttab[] = {
67	/* IP Address Extension */
68	{ 1, OL6_N,	"IP Address",			OT6_NONE, },
69
70	/* General Extension */
71	{ 8193, OL6_N,	"IEEE 1003.1 POSIX Timezone",	OT6_STR, },
72	{ 8194, OL6_16N, "Domain Name Server",		OT6_V6, },
73	{ 8195, OL6_N,	"Domain Name",			OT6_STR, },
74
75	{ 8196, OL6_N,	"SLP Agent",			OT6_NONE, },
76	{ 8197, OL6_N,	"SLP Scope"	,		OT6_NONE, },
77	{ 8198, OL6_16N, "Network Time Protocol Servers", OT6_V6, },
78	{ 8199, OL6_N,	"NIS Domain",			OT6_STR, },
79	{ 8200, OL6_16N, "NIS Servers",			OT6_V6, },
80	{ 8201, OL6_N,	"NIS+ Domain",			OT6_STR, },
81	{ 8202, OL6_16N, "NIS+ Servers",		OT6_V6, },
82
83	/* TCP Parameters */
84	{ 8203, 4,	"TCP Keepalive Interval",	OT6_NUM, },
85
86	/* DHCPv6 Extensions */
87	{ 8204, 4,	"Maximum DHCPv6 Message Size",	OT6_NUM, },
88	{ 8205, OL6_N,	"DHCP Retransmission and Configuration Parameter",
89							OT6_NONE, },
90	{ 8206, OL6_N,	"Extension Request",		OT6_NONE, },
91	{ 8207, OL6_N,	"Subnet Prefix",		OT6_NONE, },
92	{ 8208, OL6_N,	"Platform Specific Information", OT6_NONE, },
93	{ 8209, OL6_N,	"Platform Class Identifier",	OT6_STR, },
94	{ 8210, OL6_N,	"Class Identifier",		OT6_STR, },
95	{ 8211, 16,	"Reconfigure Multicast Address", OT6_V6, },
96	{ 8212, 16,	"Renumber DHCPv6 Server Address",
97							OT6_V6, },
98	{ 8213, OL6_N,	"Client-Server Authentication",	OT6_NONE, },
99	{ 8214, 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
108static struct dhcp6_opt *dh6o_pad;
109static struct dhcp6_opt *dh6o_end;
110
111static void
112dhcp6opttab_init()
113{
114	dh6o_pad = dhcp6opttab_bycode(0);
115	dh6o_end = dhcp6opttab_bycode(65536);
116}
117#endif
118
119#if 0
120static struct dhcp6_opt *
121dhcp6opttab_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
133static struct dhcp6_opt *
134dhcp6opttab_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
145static void
146dhcp6ext_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	while (cp < ep) {
156		if (ep - cp < sizeof(u_int16_t))
157			break;
158		code = ntohs(*(u_int16_t *)&cp[0]);
159		if (ep - cp < sizeof(u_int16_t) * 2)
160			break;
161		if (code != 65535)
162			len = ntohs(*(u_int16_t *)&cp[2]);
163		else
164			len = 0;
165		if (ep - cp < len + 4)
166			break;
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			strncpy(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
224trunc:
225	printf("[|dhcp6ext]");
226}
227
228/*
229 * Print dhcp6 requests
230 */
231void
232dhcp6_print(register const u_char *cp, u_int length,
233	    u_int16_t sport, u_int16_t dport)
234{
235	union dhcp6 *dh6;
236	u_char *ep;
237	u_char *extp;
238	u_int16_t field16;
239
240	printf("dhcp6");
241
242	ep = (u_char *)snapend;
243
244	dh6 = (union dhcp6 *)cp;
245	TCHECK(dh6->dh6_msgtype);
246	switch (dh6->dh6_msgtype) {
247	case DH6_SOLICIT:
248		if (!(vflag && TTEST(dh6->dh6_sol.dh6sol_relayaddr))) {
249			printf(" solicit");
250			break;
251		}
252
253		printf(" solicit (");	/*)*/
254		if (dh6->dh6_sol.dh6sol_flags != 0) {
255			u_int8_t f = dh6->dh6_sol.dh6sol_flags;
256			printf("%s%s ",
257			   (f & DH6SOL_PREFIX) ? "P" : "",
258			   (f & DH6SOL_CLOSE) ? "C" : "");
259		}
260
261		memcpy(&field16, &dh6->dh6_sol.dh6sol_plen_id,
262		       sizeof(field16));
263		field16 = ntohs(field16);
264		if (field16 & ~DH6SOL_SOLICIT_PLEN_MASK)
265			printf("plen=%d ", DH6SOL_SOLICIT_PLEN(field16));
266		printf("solicit-ID=%d", DH6SOL_SOLICIT_ID(field16));
267
268		printf(" cliaddr=%s",
269			ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
270		printf(" relayaddr=%s",
271			ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
272		/*(*/
273		printf(")");
274		break;
275	case DH6_ADVERT:
276		if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
277			printf(" advert");
278			break;
279		}
280		printf(" advert (");	/*)*/
281		memcpy(&field16, &dh6->dh6_adv.dh6adv_rsv_id, sizeof(field16));
282		printf("solicit-ID=%d",
283		       ntohs(field16) & DH6SOL_SOLICIT_ID_MASK);
284		printf(" pref=%u", dh6->dh6_adv.dh6adv_pref);
285		printf(" cliaddr=%s",
286			ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
287		printf(" relayaddr=%s",
288			ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
289		printf(" servaddr=%s",
290			ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
291		extp = (u_char *)((&dh6->dh6_adv) + 1);
292		dhcp6ext_print(extp, ep);
293		/*(*/
294		printf(")");
295		break;
296	case DH6_REQUEST:
297		if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
298			printf(" request");
299			break;
300		}
301		printf(" request (");	/*)*/
302		if (dh6->dh6_req.dh6req_flags != 0) {
303			u_int8_t f = dh6->dh6_req.dh6req_flags;
304			printf("%s%s ",
305			   (f & DH6REQ_CLOSE) ? "C" : "",
306			   (f & DH6REQ_REBOOT) ? "R" : "");
307		}
308		printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
309		printf(" cliaddr=%s",
310		       ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
311		printf(" relayaddr=%s",
312		       ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
313		printf(" servaddr=%s",
314		       ip6addr_string(&dh6->dh6_req.dh6req_serveraddr));
315		dhcp6ext_print((char *)(&dh6->dh6_req + 1), ep);
316		/*(*/
317		printf(")");
318		break;
319	case DH6_REPLY:
320		if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
321			printf(" reply");
322			break;
323		}
324		printf(" reply (");	/*)*/
325		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) != 0)
326			printf("R ");
327		printf("stat=0x%02x",
328			dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
329		printf(" xid=0x%04x", dh6->dh6_rep.dh6rep_xid);
330		printf(" cliaddr=%s",
331		       ip6addr_string(&dh6->dh6_rep.dh6rep_cliaddr));
332		extp = (u_char *)((&dh6->dh6_rep) + 1);
333		if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) !=
334		    0) {
335			printf(" relayaddr=%s", ip6addr_string(extp));
336			extp += sizeof(struct in6_addr);
337		}
338		dhcp6ext_print(extp, ep);
339		/*(*/
340		printf(")");
341		break;
342	case DH6_RELEASE:
343		printf(" release");
344		break;
345	case DH6_RECONFIG:
346		printf(" reconfig");
347		break;
348	}
349	return;
350
351trunc:
352	printf("%s", tstr);
353}
354