xref: /freebsd/contrib/tcpdump/print-dhcp6.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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  * draft-ietf-dhc-dhcpv6-22.txt
31  */
32 
33 #ifndef lint
34 static const char rcsid[] =
35     "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.14.4.2 2002/06/01 23:51:12 guy Exp $";
36 #endif
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/socket.h>
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 
56 /* Error Values */
57 #define DH6ERR_FAILURE		16
58 #define DH6ERR_AUTHFAIL		17
59 #define DH6ERR_POORLYFORMED	18
60 #define DH6ERR_UNAVAIL		19
61 #define DH6ERR_OPTUNAVAIL	20
62 
63 /* Message type */
64 #define DH6_REPLY	7
65 #define DH6_INFORM_REQ	11
66 
67 /* DHCP6 base packet format */
68 struct dhcp6 {
69 	union {
70 		u_int8_t m;
71 		u_int32_t x;
72 	} dh6_msgtypexid;
73 	struct in6_addr dh6_servaddr;
74 	/* options follow */
75 } __attribute__ ((__packed__));
76 #define dh6_msgtype	dh6_msgtypexid.m
77 #define dh6_xid		dh6_msgtypexid.x
78 #define DH6_XIDMASK	0x00ffffff
79 
80 /* option */
81 #define DH6OPT_DUID	1	/* TBD */
82 #define DH6OPT_DNS	11	/* TBD */
83 struct dhcp6opt {
84 	u_int16_t dh6opt_type;
85 	u_int16_t dh6opt_len;
86 	/* type-dependent data follows */
87 } __attribute__ ((__packed__));
88 
89 static void
90 dhcp6opt_print(u_char *cp, u_char *ep)
91 {
92 	struct dhcp6opt *dh6o;
93 	u_char *tp;
94 	int i;
95 	size_t optlen;
96 
97 	if (cp == ep)
98 		return;
99 	while (cp < ep) {
100 		if (ep - cp < sizeof(*dh6o))
101 			goto trunc;
102 		dh6o = (struct dhcp6opt *)cp;
103 		optlen = ntohs(dh6o->dh6opt_len);
104 		if (ep - cp < sizeof(*dh6o) + optlen)
105 			goto trunc;
106 		switch (ntohs(dh6o->dh6opt_type)) {
107 		case DH6OPT_DUID:
108 			printf(" (duid");	/*)*/
109 			if (optlen < 2) {
110 				/*(*/
111 				printf(" ??)");
112 				break;
113 			}
114 			tp = (u_char *)(dh6o + 1);
115 			switch (ntohs(*(u_int16_t *)tp)) {
116 			case 1:
117 				if (optlen >= 2 + 6) {
118 					printf(" hwaddr/time time %u type %u ",
119 					    ntohl(*(u_int32_t *)&tp[2]),
120 					    ntohs(*(u_int16_t *)&tp[6]));
121 					for (i = 8; i < optlen; i++)
122 						printf("%02x", tp[i]);
123 					/*(*/
124 					printf(")");
125 				} else {
126 					/*(*/
127 					printf(" ??)");
128 				}
129 				break;
130 			case 2:
131 				if (optlen >= 2 + 8) {
132 					printf(" vid ");
133 					for (i = 2; i < 2 + 8; i++)
134 						printf("%02x", tp[i]);
135 					/*(*/
136 					printf(")");
137 				} else {
138 					/*(*/
139 					printf(" ??)");
140 				}
141 				break;
142 			case 3:
143 				if (optlen >= 2 + 2) {
144 					printf(" hwaddr type %u ",
145 					    ntohs(*(u_int16_t *)&tp[2]));
146 					for (i = 4; i < optlen; i++)
147 						printf("%02x", tp[i]);
148 					/*(*/
149 					printf(")");
150 				} else {
151 					/*(*/
152 					printf(" ??)");
153 				}
154 			}
155 			break;
156 		case DH6OPT_DNS:
157 			printf(" (dnsserver");	/*)*/
158 			if (optlen % 16) {
159 				/*(*/
160 				printf(" ??)");
161 				break;
162 			}
163 			tp = (u_char *)(dh6o + 1);
164 			for (i = 0; i < optlen; i += 16)
165 				printf(" %s", ip6addr_string(&tp[i]));
166 			/*(*/
167 			printf(")");
168 		default:
169 			printf(" (opt-%u)", ntohs(dh6o->dh6opt_type));
170 			break;
171 		}
172 
173 		cp += sizeof(*dh6o) + optlen;
174 	}
175 	return;
176 
177 trunc:
178 	printf("[|dhcp6ext]");
179 }
180 
181 /*
182  * Print dhcp6 packets
183  */
184 void
185 dhcp6_print(register const u_char *cp, u_int length,
186 	    u_int16_t sport, u_int16_t dport)
187 {
188 	struct dhcp6 *dh6;
189 	u_char *ep;
190 	u_char *extp;
191 	const char *name;
192 
193 	printf("dhcp6");
194 
195 	ep = (u_char *)snapend;
196 
197 	dh6 = (struct dhcp6 *)cp;
198 	TCHECK(dh6->dh6_servaddr);
199 	switch (dh6->dh6_msgtype) {
200 	case DH6_REPLY:
201 		name = "reply";
202 		break;
203 	case DH6_INFORM_REQ:
204 		name= "inf-req";
205 		break;
206 	default:
207 		name = NULL;
208 		break;
209 	}
210 
211 	if (!vflag) {
212 		if (name)
213 			printf(" %s", name);
214 		else
215 			printf(" msgtype-%u", dh6->dh6_msgtype);
216 		return;
217 	}
218 
219 	/* XXX relay agent messages have to be handled differently */
220 
221 	if (name)
222 		printf(" %s (", name);	/*)*/
223 	else
224 		printf(" msgtype-%u (", dh6->dh6_msgtype);	/*)*/
225 	printf("xid=%x", ntohl(dh6->dh6_xid) & DH6_XIDMASK);
226 	printf(" server=%s", ip6addr_string(&dh6->dh6_servaddr));
227 	extp = (u_char *)(dh6 + 1);
228 	dhcp6opt_print(extp, ep);
229 	/*(*/
230 	printf(")");
231 	return;
232 
233 trunc:
234 	printf("[|dhcp6]");
235 }
236