]> The Tcpdump Group git mirrors - tcpdump/blob - print-dhcp6.c
repair ID payload protocol # decoding. we shouln't check DOI,
[tcpdump] / print-dhcp6.c
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.10 2000-07-31 02:47:27 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 struct mbuf;
44 struct rtentry;
45 #include <net/if.h>
46
47 #include <netinet/in.h>
48
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <arpa/inet.h>
53
54 #include "interface.h"
55 #include "addrtoname.h"
56 #include "dhcp6.h"
57 #include "dhcp6opt.h"
58
59 #if 0
60 static void dhcp6opttab_init __P((void));
61 static struct dhcp6_opt *dhcp6opttab_byname __P((char *));
62 #endif
63 static struct dhcp6_opt *dhcp6opttab_bycode __P((u_int));
64
65 static char tstr[] = " [|dhcp6]";
66
67 static struct dhcp6_opt dh6opttab[] = {
68 /* IP Address Extension */
69 { 1, OL6_N, "IP Address", OT6_NONE, },
70
71 /* General Extension */
72 { 8193, OL6_N, "IEEE 1003.1 POSIX Timezone", OT6_STR, },
73 { 8194, OL6_16N, "Domain Name Server", OT6_V6, },
74 { 8195, OL6_N, "Domain Name", OT6_STR, },
75
76 { 8196, OL6_N, "SLP Agent", OT6_NONE, },
77 { 8197, OL6_N, "SLP Scope" , OT6_NONE, },
78 { 8198, OL6_16N, "Network Time Protocol Servers", OT6_V6, },
79 { 8199, OL6_N, "NIS Domain", OT6_STR, },
80 { 8200, OL6_16N, "NIS Servers", OT6_V6, },
81 { 8201, OL6_N, "NIS+ Domain", OT6_STR, },
82 { 8202, OL6_16N, "NIS+ Servers", OT6_V6, },
83
84 /* TCP Parameters */
85 { 8203, 4, "TCP Keepalive Interval", OT6_NUM, },
86
87 /* DHCPv6 Extensions */
88 { 8204, 4, "Maximum DHCPv6 Message Size", OT6_NUM, },
89 { 8205, OL6_N, "DHCP Retransmission and Configuration Parameter",
90 OT6_NONE, },
91 { 8206, OL6_N, "Extension Request", OT6_NONE, },
92 { 8207, OL6_N, "Subnet Prefix", OT6_NONE, },
93 { 8208, OL6_N, "Platform Specific Information", OT6_NONE, },
94 { 8209, OL6_N, "Platform Class Identifier", OT6_STR, },
95 { 8210, OL6_N, "Class Identifier", OT6_STR, },
96 { 8211, 16, "Reconfigure Multicast Address", OT6_V6, },
97 { 8212, 16, "Renumber DHCPv6 Server Address",
98 OT6_V6, },
99 { 8213, OL6_N, "Client-Server Authentication", OT6_NONE, },
100 { 8214, 4, "Client Key Selection", OT6_NUM, },
101
102 /* End Extension */
103 { 65536, OL6_Z, "End", OT6_NONE, },
104
105 { 0 },
106 };
107
108 #if 0
109 static struct dhcp6_opt *dh6o_pad;
110 static struct dhcp6_opt *dh6o_end;
111
112 static void
113 dhcp6opttab_init()
114 {
115 dh6o_pad = dhcp6opttab_bycode(0);
116 dh6o_end = dhcp6opttab_bycode(65536);
117 }
118 #endif
119
120 #if 0
121 static struct dhcp6_opt *
122 dhcp6opttab_byname(name)
123 char *name;
124 {
125 struct dhcp6_opt *p;
126
127 for (p = dh6opttab; p->code; p++)
128 if (strcmp(name, p->name) == 0)
129 return p;
130 return NULL;
131 }
132 #endif
133
134 static struct dhcp6_opt *
135 dhcp6opttab_bycode(code)
136 u_int code;
137 {
138 struct dhcp6_opt *p;
139
140 for (p = dh6opttab; p->code; p++)
141 if (p->code == code)
142 return p;
143 return NULL;
144 }
145
146 static void
147 dhcp6ext_print(u_char *cp, u_char *ep)
148 {
149 u_int16_t code, len;
150 struct dhcp6_opt *p;
151 char buf[BUFSIZ];
152 int i;
153
154 if (cp == ep)
155 return;
156 while (cp < ep) {
157 if (ep - cp < sizeof(u_int16_t))
158 break;
159 code = ntohs(*(u_int16_t *)&cp[0]);
160 if (ep - cp < sizeof(u_int16_t) * 2)
161 break;
162 if (code != 65535)
163 len = ntohs(*(u_int16_t *)&cp[2]);
164 else
165 len = 0;
166 if (ep - cp < len + 4)
167 break;
168 p = dhcp6opttab_bycode(code);
169 if (p == NULL) {
170 printf("(unknown, len=%d)", len);
171 cp += len + 4;
172 continue;
173 }
174
175 /* sanity check on length */
176 switch (p->len) {
177 case OL6_N:
178 break;
179 case OL6_16N:
180 if (len % 16 != 0)
181 goto trunc;
182 break;
183 case OL6_Z:
184 if (len != 0)
185 goto trunc;
186 break;
187 default:
188 if (len != p->len)
189 goto trunc;
190 break;
191 }
192 if (cp + 4 + len > ep) {
193 printf(" [|%s]", p->name);
194 return;
195 }
196
197 printf(" (%s, ", p->name);
198 switch (p->type) {
199 case OT6_V6:
200 for (i = 0; i < len; i += 16) {
201 inet_ntop(AF_INET6, &cp[4 + i], buf,
202 sizeof(buf));
203 if (i != 0)
204 printf(",");
205 printf("%s", buf);
206 }
207 break;
208 case OT6_STR:
209 memset(&buf, 0, sizeof(buf));
210 strncpy(buf, &cp[4], len);
211 printf("%s", buf);
212 break;
213 case OT6_NUM:
214 printf("%d", (u_int32_t)ntohl(*(u_int32_t *)&cp[4]));
215 break;
216 default:
217 for (i = 0; i < len; i++)
218 printf("%02x", cp[4 + i] & 0xff);
219 }
220 printf(")");
221 cp += len + 4;
222 }
223 return;
224
225 trunc:
226 printf("[|dhcp6ext]");
227 }
228
229 /*
230 * Print dhcp6 requests
231 */
232 void
233 dhcp6_print(register const u_char *cp, u_int length,
234 u_int16_t sport, u_int16_t dport)
235 {
236 union dhcp6 *dh6;
237 u_char *ep;
238 u_char *extp;
239 u_int16_t field16;
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 break;
252 }
253
254 printf(" solicit ("); /*)*/
255 if (dh6->dh6_sol.dh6sol_flags != 0) {
256 u_int8_t f = dh6->dh6_sol.dh6sol_flags;
257 printf("%s%s ",
258 (f & DH6SOL_PREFIX) ? "P" : "",
259 (f & DH6SOL_CLOSE) ? "C" : "");
260 }
261
262 memcpy(&field16, &dh6->dh6_sol.dh6sol_plen_id,
263 sizeof(field16));
264 field16 = ntohs(field16);
265 if (field16 & ~DH6SOL_SOLICIT_PLEN_MASK)
266 printf("plen=%d ", DH6SOL_SOLICIT_PLEN(field16));
267 printf("solicit-ID=%d", DH6SOL_SOLICIT_ID(field16));
268
269 printf(" cliaddr=%s",
270 ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
271 printf(" relayaddr=%s",
272 ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
273 /*(*/
274 printf(")");
275 break;
276 case DH6_ADVERT:
277 if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
278 printf(" advert");
279 break;
280 }
281 printf(" advert ("); /*)*/
282 memcpy(&field16, &dh6->dh6_adv.dh6adv_rsv_id, sizeof(field16));
283 printf("solicit-ID=%d",
284 ntohs(field16) & DH6SOL_SOLICIT_ID_MASK);
285 printf(" pref=%u", dh6->dh6_adv.dh6adv_pref);
286 printf(" cliaddr=%s",
287 ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
288 printf(" relayaddr=%s",
289 ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
290 printf(" servaddr=%s",
291 ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
292 extp = (u_char *)((&dh6->dh6_adv) + 1);
293 dhcp6ext_print(extp, ep);
294 /*(*/
295 printf(")");
296 break;
297 case DH6_REQUEST:
298 if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
299 printf(" request");
300 break;
301 }
302 printf(" request ("); /*)*/
303 if (dh6->dh6_req.dh6req_flags != 0) {
304 u_int8_t f = dh6->dh6_req.dh6req_flags;
305 printf("%s%s ",
306 (f & DH6REQ_CLOSE) ? "C" : "",
307 (f & DH6REQ_REBOOT) ? "R" : "");
308 }
309 printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
310 printf(" cliaddr=%s",
311 ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
312 printf(" relayaddr=%s",
313 ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
314 printf(" servaddr=%s",
315 ip6addr_string(&dh6->dh6_req.dh6req_serveraddr));
316 dhcp6ext_print((char *)(&dh6->dh6_req + 1), ep);
317 /*(*/
318 printf(")");
319 break;
320 case DH6_REPLY:
321 if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
322 printf(" reply");
323 break;
324 }
325 printf(" reply ("); /*)*/
326 if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) != 0)
327 printf("R ");
328 printf("stat=0x%02x",
329 dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
330 printf(" xid=0x%04x", dh6->dh6_rep.dh6rep_xid);
331 printf(" cliaddr=%s",
332 ip6addr_string(&dh6->dh6_rep.dh6rep_cliaddr));
333 extp = (u_char *)((&dh6->dh6_rep) + 1);
334 if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) !=
335 0) {
336 printf(" relayaddr=%s", ip6addr_string(extp));
337 extp += sizeof(struct in6_addr);
338 }
339 dhcp6ext_print(extp, ep);
340 /*(*/
341 printf(")");
342 break;
343 case DH6_RELEASE:
344 printf(" release");
345 break;
346 case DH6_RECONFIG:
347 printf(" reconfig");
348 break;
349 }
350 return;
351
352 trunc:
353 printf("%s", tstr);
354 }