2 * Copyright (C) 1998 and 1999 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 * supported DHCPv6 options:
35 * draft-ietf-dhc-dhcpv6-opt-timeconfig-03.txt,
36 * draft-ietf-dhc-lifetime-00.txt,
40 static const char rcsid[] _U_ =
41 "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.35 2004/07/06 22:16:03 guy Exp $";
48 #include <tcpdump-stdinc.h>
53 #include "interface.h"
54 #include "addrtoname.h"
58 #define DHCP6_DURATITION_INFINITE 0xffffffff
61 #define DH6ERR_FAILURE 16
62 #define DH6ERR_AUTHFAIL 17
63 #define DH6ERR_POORLYFORMED 18
64 #define DH6ERR_UNAVAIL 19
65 #define DH6ERR_OPTUNAVAIL 20
69 #define DH6_ADVERTISE 2
77 #define DH6_RECONFIGURE 10
78 #define DH6_INFORM_REQ 11
79 #define DH6_RELAY_FORW 12
80 #define DH6_RELAY_REPLY 13
82 /* DHCP6 base packet format */
90 #define dh6_msgtype dh6_msgtypexid.m
91 #define dh6_xid dh6_msgtypexid.x
92 #define DH6_XIDMASK 0x00ffffff
94 /* DHCPv6 relay messages */
96 u_int8_t dh6relay_msgtype;
97 u_int8_t dh6relay_hcnt;
98 u_int8_t dh6relay_linkaddr[16]; /* XXX: badly aligned */
99 u_int8_t dh6relay_peeraddr[16];
104 #define DH6OPT_CLIENTID 1
105 #define DH6OPT_SERVERID 2
106 #define DH6OPT_IA_NA 3
107 #define DH6OPT_IA_TMP 4
108 #define DH6OPT_IADDR 5
110 #define DH6OPT_PREFERENCE 7
111 # define DH6OPT_PREF_MAX 255
112 #define DH6OPT_ELAPSED_TIME 8
113 #define DH6OPT_RELAY_MSG 9
114 /*#define DH6OPT_SERVER_MSG 10 deprecated */
115 #define DH6OPT_AUTH 11
116 # define DH6OPT_AUTHPROTO_DELAYED 2
117 # define DH6OPT_AUTHPROTO_RECONFIG 3
118 # define DH6OPT_AUTHALG_HMACMD5 1
119 # define DH6OPT_AUTHRDM_MONOCOUNTER 0
120 # define DH6OPT_AUTHRECONFIG_KEY 1
121 # define DH6OPT_AUTHRECONFIG_HMACMD5 2
122 #define DH6OPT_UNICAST 12
123 #define DH6OPT_STATUS_CODE 13
124 # define DH6OPT_STCODE_SUCCESS 0
125 # define DH6OPT_STCODE_UNSPECFAIL 1
126 # define DH6OPT_STCODE_NOADDRAVAIL 2
127 # define DH6OPT_STCODE_NOBINDING 3
128 # define DH6OPT_STCODE_NOTONLINK 4
129 # define DH6OPT_STCODE_USEMULTICAST 5
130 # define DH6OPT_STCODE_NOPREFIXAVAIL 6
131 #define DH6OPT_RAPID_COMMIT 14
132 #define DH6OPT_USER_CLASS 15
133 #define DH6OPT_VENDOR_CLASS 16
134 #define DH6OPT_VENDOR_OPTS 17
135 #define DH6OPT_INTERFACE_ID 18
136 #define DH6OPT_RECONF_MSG 19
137 #define DH6OPT_RECONF_ACCEPT 20
138 #define DH6OPT_SIP_SERVER_D 21
139 #define DH6OPT_SIP_SERVER_A 22
140 #define DH6OPT_DNS 23
141 #define DH6OPT_DNSNAME 24
142 #define DH6OPT_IA_PD 25
143 #define DH6OPT_IA_PD_PREFIX 26
146 * The old prefix delegation option used in the service specification document
147 * (200206xx version) by NTT Communications.
149 #define DH6OPT_PREFIX_DELEGATION 30
150 #define DH6OPT_PREFIX_INFORMATION 31
151 #define DH6OPT_PREFIX_REQUEST 32
154 * The following one is an unassigned number.
155 * We temporarily use values as of KAME snap 20040322.
157 #define DH6OPT_NTP_SERVERS 35
158 #define DH6OPT_LIFETIME 36
161 u_int16_t dh6opt_type;
162 u_int16_t dh6opt_len;
163 /* type-dependent data follows */
167 u_int16_t dh6opt_ia_type;
168 u_int16_t dh6opt_ia_len;
169 u_int32_t dh6opt_ia_iaid;
170 u_int32_t dh6opt_ia_t1;
171 u_int32_t dh6opt_ia_t2;
174 struct dhcp6_ia_prefix {
175 u_int16_t dh6opt_ia_prefix_type;
176 u_int16_t dh6opt_ia_prefix_len;
177 u_int32_t dh6opt_ia_prefix_pltime;
178 u_int32_t dh6opt_ia_prefix_vltime;
179 u_int8_t dh6opt_ia_prefix_plen;
180 struct in6_addr dh6opt_ia_prefix_addr;
181 } __attribute__ ((__packed__));
184 u_int16_t dh6opt_auth_type;
185 u_int16_t dh6opt_auth_len;
186 u_int8_t dh6opt_auth_proto;
187 u_int8_t dh6opt_auth_alg;
188 u_int8_t dh6opt_auth_rdm;
189 u_int8_t dh6opt_auth_rdinfo[8];
190 /* authentication information follows */
191 } __attribute__ ((__packed__));
194 dhcp6opt_name(int type)
196 static char genstr[sizeof("opt_65535") + 1]; /* XXX thread unsafe */
199 return "INVALID option";
202 case DH6OPT_CLIENTID:
204 case DH6OPT_SERVERID:
209 return "option request";
210 case DH6OPT_PREFERENCE:
212 case DH6OPT_ELAPSED_TIME:
213 return "elapsed time";
214 case DH6OPT_RELAY_MSG:
215 return "relay message";
217 return "authentication";
219 return "server unicast";
220 case DH6OPT_STATUS_CODE:
221 return "status code";
222 case DH6OPT_RAPID_COMMIT:
223 return "rapid commit";
224 case DH6OPT_USER_CLASS:
226 case DH6OPT_VENDOR_CLASS:
227 return "vendor class";
228 case DH6OPT_VENDOR_OPTS:
229 return "vendor-specific info";
230 case DH6OPT_INTERFACE_ID:
231 return "interface ID";
232 case DH6OPT_RECONF_MSG:
233 return "reconfigure message";
234 case DH6OPT_RECONF_ACCEPT:
235 return "reconfigure accept";
236 case DH6OPT_SIP_SERVER_D:
237 return "SIP servers domain";
238 case DH6OPT_SIP_SERVER_A:
239 return "SIP servers address";
244 case DH6OPT_PREFIX_DELEGATION:
245 return "prefix delegation";
246 case DH6OPT_PREFIX_INFORMATION:
247 return "prefix information";
250 case DH6OPT_IA_PD_PREFIX:
251 return "IA_PD prefix";
252 case DH6OPT_NTP_SERVERS:
254 case DH6OPT_LIFETIME:
257 snprintf(genstr, sizeof(genstr), "opt_%d", type);
263 dhcp6stcode(int code)
265 static char genstr[sizeof("code255") + 1]; /* XXX thread unsafe */
268 return "INVALID code";
271 case DH6OPT_STCODE_SUCCESS:
273 case DH6OPT_STCODE_UNSPECFAIL:
274 return "unspec failure";
275 case DH6OPT_STCODE_NOADDRAVAIL:
276 return "no addresses";
277 case DH6OPT_STCODE_NOBINDING:
279 case DH6OPT_STCODE_NOTONLINK:
280 return "not on-link";
281 case DH6OPT_STCODE_USEMULTICAST:
282 return "use multicast";
283 case DH6OPT_STCODE_NOPREFIXAVAIL:
284 return "no prefixes";
286 snprintf(genstr, sizeof(genstr), "code%d", code);
292 dhcp6opt_print(const u_char *cp, const u_char *ep)
294 struct dhcp6opt *dh6o;
301 struct in6_addr addr6;
303 struct dhcp6_ia_prefix ia_prefix;
304 struct dhcp6_auth authopt;
305 u_int authinfolen, authrealmlen;
310 if (ep < cp + sizeof(*dh6o))
312 dh6o = (struct dhcp6opt *)cp;
313 optlen = EXTRACT_16BITS(&dh6o->dh6opt_len);
314 if (ep < cp + sizeof(*dh6o) + optlen)
316 opttype = EXTRACT_16BITS(&dh6o->dh6opt_type);
317 printf(" (%s", dhcp6opt_name(opttype));
319 case DH6OPT_CLIENTID:
320 case DH6OPT_SERVERID:
326 tp = (u_char *)(dh6o + 1);
327 switch (EXTRACT_16BITS(tp)) {
329 if (optlen >= 2 + 6) {
330 printf(" hwaddr/time type %u time %u ",
331 EXTRACT_16BITS(&tp[2]),
332 EXTRACT_32BITS(&tp[4]));
333 for (i = 8; i < optlen; i++)
334 printf("%02x", tp[i]);
343 if (optlen >= 2 + 8) {
345 for (i = 2; i < 2 + 8; i++)
346 printf("%02x", tp[i]);
355 if (optlen >= 2 + 2) {
356 printf(" hwaddr type %u ",
357 EXTRACT_16BITS(&tp[2]));
358 for (i = 4; i < optlen; i++)
359 printf("%02x", tp[i]);
368 printf(" type %d)", EXTRACT_16BITS(tp));
377 tp = (u_char *)(dh6o + 1);
378 for (i = 0; i < optlen; i += 2) {
381 memcpy(&opt, &tp[i], sizeof(opt));
382 printf(" %s", dhcp6opt_name(ntohs(opt)));
386 case DH6OPT_PREFERENCE:
391 printf(" %d)", *((u_char *)(dh6o + 1) + 1));
393 case DH6OPT_ELAPSED_TIME:
398 memcpy(&val16, dh6o + 1, sizeof(val16));
399 val16 = ntohs(val16);
400 printf(" %d)", (int)val16);
402 case DH6OPT_RELAY_MSG:
404 dhcp6_print((const u_char *)(dh6o + 1), optlen);
408 if (optlen < sizeof(authopt) - sizeof(*dh6o)) {
412 memcpy(&authopt, dh6o, sizeof(authopt));
413 switch (authopt.dh6opt_auth_proto) {
414 case DH6OPT_AUTHPROTO_DELAYED:
415 printf(" proto: delayed");
417 case DH6OPT_AUTHPROTO_RECONFIG:
418 printf(" proto: reconfigure");
422 authopt.dh6opt_auth_proto);
425 switch (authopt.dh6opt_auth_alg) {
426 case DH6OPT_AUTHALG_HMACMD5:
427 /* XXX: may depend on the protocol */
428 printf(", alg: HMAC-MD5");
431 printf(", alg: %d", authopt.dh6opt_auth_alg);
434 switch (authopt.dh6opt_auth_rdm) {
435 case DH6OPT_AUTHRDM_MONOCOUNTER:
436 printf(", RDM: mono");
439 printf(", RDM: %d", authopt.dh6opt_auth_rdm);
442 tp = (u_char *)&authopt.dh6opt_auth_rdinfo;
444 for (i = 0; i < 4; i++, tp += sizeof(val16))
445 printf(" %04x", EXTRACT_16BITS(tp));
447 /* protocol dependent part */
448 tp = (u_char *)dh6o + sizeof(authopt);
450 optlen + sizeof(*dh6o) - sizeof(authopt);
451 switch (authopt.dh6opt_auth_proto) {
452 case DH6OPT_AUTHPROTO_DELAYED:
453 if (authinfolen == 0)
455 if (authinfolen < 20) {
459 authrealmlen = authinfolen - 20;
460 if (authrealmlen > 0) {
463 for (i = 0; i < authrealmlen; i++, tp++)
465 printf(", key ID: %08x", EXTRACT_32BITS(tp));
467 printf(", HMAC-MD5:");
468 for (i = 0; i < 4; i++, tp+= 4)
469 printf(" %08x", EXTRACT_32BITS(tp));
471 case DH6OPT_AUTHPROTO_RECONFIG:
472 if (authinfolen != 17) {
477 case DH6OPT_AUTHRECONFIG_KEY:
478 printf(" reconfig-key");
480 case DH6OPT_AUTHRECONFIG_HMACMD5:
481 printf(" type: HMAC-MD5");
488 for (i = 0; i < 4; i++, tp+= 4)
489 printf(" %08x", EXTRACT_32BITS(tp));
498 case DH6OPT_RAPID_COMMIT: /* nothing todo */
501 case DH6OPT_INTERFACE_ID:
503 * Since we cannot predict the encoding, print hex dump
504 * at most 10 characters.
506 for (i = 0; i < optlen && i < 10; i++)
507 printf("%02x", ((u_char *)(dh6o + 1))[i]);
509 case DH6OPT_RECONF_MSG:
510 tp = (u_char *)(dh6o + 1);
513 printf(" for renew)");
516 printf(" for inf-req)");
519 printf(" for ?\?\?(%02x))", *tp);
523 case DH6OPT_RECONF_ACCEPT: /* nothing todo */
526 case DH6OPT_SIP_SERVER_A:
528 case DH6OPT_NTP_SERVERS:
533 tp = (u_char *)(dh6o + 1);
534 for (i = 0; i < optlen; i += 16)
535 printf(" %s", ip6addr_string(&tp[i]));
538 case DH6OPT_PREFIX_DELEGATION:
539 dhcp6opt_print((u_char *)(dh6o + 1),
540 (u_char *)(dh6o + 1) + optlen);
543 case DH6OPT_PREFIX_INFORMATION:
546 memcpy(&addr6, (u_char *)(dh6o + 1) + 5,
548 printf(" %s/%d", ip6addr_string(&addr6),
549 (int)*((u_char *)(dh6o + 1) + 4));
550 memcpy(&val32, dh6o + 1, sizeof(val32));
551 val32 = ntohl(val32);
552 if (val32 == DHCP6_DURATITION_INFINITE)
553 printf(" lease-duration: infinite)");
555 printf(" lease-duration: %u)", val32);
557 case DH6OPT_STATUS_CODE:
562 memcpy(&val16, (u_char *)(dh6o + 1), sizeof(val16));
563 val16 = ntohs(val16);
564 printf(" %s)", dhcp6stcode(val16));
568 if (optlen < sizeof(ia) - 4) {
572 memcpy(&ia, (u_char *)dh6o, sizeof(ia));
573 ia.dh6opt_ia_iaid = ntohl(ia.dh6opt_ia_iaid);
574 ia.dh6opt_ia_t1 = ntohl(ia.dh6opt_ia_t1);
575 ia.dh6opt_ia_t2 = ntohl(ia.dh6opt_ia_t2);
576 printf(" IAID:%lu T1:%lu T2:%lu",
577 (unsigned long)ia.dh6opt_ia_iaid,
578 (unsigned long)ia.dh6opt_ia_t1,
579 (unsigned long)ia.dh6opt_ia_t2);
580 if (optlen > sizeof(ia) - 4) {
581 /* there are sub-options */
582 dhcp6opt_print((u_char *)dh6o + sizeof(ia),
583 (u_char *)(dh6o + 1) + optlen);
587 case DH6OPT_IA_PD_PREFIX:
588 if (optlen < sizeof(ia_prefix) - 4) {
592 memcpy(&ia_prefix, (u_char *)dh6o, sizeof(ia_prefix));
594 ip6addr_string(&ia_prefix.dh6opt_ia_prefix_addr),
595 ia_prefix.dh6opt_ia_prefix_plen);
596 ia_prefix.dh6opt_ia_prefix_pltime =
597 ntohl(ia_prefix.dh6opt_ia_prefix_pltime);
598 ia_prefix.dh6opt_ia_prefix_vltime =
599 ntohl(ia_prefix.dh6opt_ia_prefix_vltime);
600 printf(" pltime:%lu vltime:%lu",
601 (unsigned long)ia_prefix.dh6opt_ia_prefix_pltime,
602 (unsigned long)ia_prefix.dh6opt_ia_prefix_vltime);
603 if (optlen > sizeof(ia_prefix) - 4) {
604 /* there are sub-options */
605 dhcp6opt_print((u_char *)dh6o +
607 (u_char *)(dh6o + 1) + optlen);
611 case DH6OPT_LIFETIME:
616 memcpy(&val32, dh6o + 1, sizeof(val32));
617 val32 = ntohl(val32);
618 printf(" %d)", (int)val32);
625 cp += sizeof(*dh6o) + optlen;
630 printf("[|dhcp6ext]");
634 * Print dhcp6 packets
637 dhcp6_print(const u_char *cp, u_int length)
640 struct dhcp6_relay *dh6relay;
647 ep = (u_char *)snapend;
648 if (cp + length < ep)
651 dh6 = (struct dhcp6 *)cp;
652 dh6relay = (struct dhcp6_relay *)cp;
653 TCHECK(dh6->dh6_xid);
654 switch (dh6->dh6_msgtype) {
682 case DH6_RECONFIGURE:
683 name = "reconfigure";
691 case DH6_RELAY_REPLY:
702 else if (dh6->dh6_msgtype != DH6_RELAY_FORW &&
703 dh6->dh6_msgtype != DH6_RELAY_REPLY) {
704 printf(" msgtype-%u", dh6->dh6_msgtype);
709 /* XXX relay agent messages have to be handled differently */
712 printf(" %s (", name); /*)*/
714 printf(" msgtype-%u (", dh6->dh6_msgtype); /*)*/
715 if (dh6->dh6_msgtype != DH6_RELAY_FORW &&
716 dh6->dh6_msgtype != DH6_RELAY_REPLY) {
717 printf("xid=%x", EXTRACT_32BITS(&dh6->dh6_xid) & DH6_XIDMASK);
718 extp = (u_char *)(dh6 + 1);
719 dhcp6opt_print(extp, ep);
720 } else { /* relay messages */
721 struct in6_addr addr6;
723 TCHECK(dh6relay->dh6relay_peeraddr);
725 memcpy(&addr6, dh6relay->dh6relay_linkaddr, sizeof (addr6));
726 printf("linkaddr=%s", ip6addr_string(&addr6));
728 memcpy(&addr6, dh6relay->dh6relay_peeraddr, sizeof (addr6));
729 printf(" peeraddr=%s", ip6addr_string(&addr6));
731 dhcp6opt_print((u_char *)(dh6relay + 1), ep);