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:
33 * draft-ietf-dhc-dhcpv6-opt-dnsconfig-04.txt,
34 * draft-ietf-dhc-dhcpv6-opt-prefix-delegation-05.txt
35 * draft-ietf-dhc-dhcpv6-opt-timeconfig-02.txt,
39 static const char rcsid[] _U_ =
40 "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.27.2.4 2003/11/18 23:26:14 guy Exp $";
47 #include <tcpdump-stdinc.h>
52 #include "interface.h"
53 #include "addrtoname.h"
57 #define DHCP6_DURATITION_INFINITE 0xffffffff
60 #define DH6ERR_FAILURE 16
61 #define DH6ERR_AUTHFAIL 17
62 #define DH6ERR_POORLYFORMED 18
63 #define DH6ERR_UNAVAIL 19
64 #define DH6ERR_OPTUNAVAIL 20
68 #define DH6_ADVERTISE 2
76 #define DH6_RECONFIGURE 10
77 #define DH6_INFORM_REQ 11
78 #define DH6_RELAY_FORW 12
79 #define DH6_RELAY_REPLY 13
81 /* DHCP6 base packet format */
89 #define dh6_msgtype dh6_msgtypexid.m
90 #define dh6_xid dh6_msgtypexid.x
91 #define DH6_XIDMASK 0x00ffffff
93 /* DHCPv6 relay messages */
95 u_int8_t dh6relay_msgtype;
96 u_int8_t dh6relay_hcnt;
97 u_int8_t dh6relay_linkaddr[16]; /* XXX: badly aligned */
98 u_int8_t dh6relay_peeraddr[16];
103 #define DH6OPT_CLIENTID 1
104 #define DH6OPT_SERVERID 2
105 #define DH6OPT_IA_NA 3
106 #define DH6OPT_IA_TMP 4
107 #define DH6OPT_IADDR 5
109 #define DH6OPT_PREFERENCE 7
110 # define DH6OPT_PREF_UNDEF -1
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_UNICAST 12
117 #define DH6OPT_STATUS_CODE 13
118 # define DH6OPT_STCODE_SUCCESS 0
119 # define DH6OPT_STCODE_UNSPECFAIL 1
120 # define DH6OPT_STCODE_NOADDRAVAIL 2
121 # define DH6OPT_STCODE_NOBINDING 3
122 # define DH6OPT_STCODE_NOTONLINK 4
123 # define DH6OPT_STCODE_USEMULTICAST 5
124 # define DH6OPT_STCODE_NOPREFIXAVAIL 6
125 #define DH6OPT_RAPID_COMMIT 14
126 #define DH6OPT_USER_CLASS 15
127 #define DH6OPT_VENDOR_CLASS 16
128 #define DH6OPT_VENDOR_OPTS 17
129 #define DH6OPT_INTERFACE_ID 18
130 #define DH6OPT_RECONF_MSG 19
131 #define DH6OPT_RECONF_ACCEPT 20
132 #define DH6OPT_SIP_SERVER_D 21
133 #define DH6OPT_SIP_SERVER_A 22
134 #define DH6OPT_DNS 23
135 #define DH6OPT_DNSNAME 24
138 * The option type has not been assigned for the following options.
139 * We temporarily adopt values used in the service specification document
140 * (200206xx version) by NTT Communications.
141 * Note that we'll change the following definitions if different type values
142 * are officially assigned.
144 #define DH6OPT_PREFIX_DELEGATION 30
145 #define DH6OPT_PREFIX_INFORMATION 31
146 #define DH6OPT_PREFIX_REQUEST 32
149 * The followings are also unassigned numbers.
150 * We temporarily use values as of KAME snap 20031013.
152 #define DH6OPT_IA_PD 33
153 #define DH6OPT_IA_PD_PREFIX 34
154 #define DH6OPT_NTP_SERVERS 35
157 u_int16_t dh6opt_type;
158 u_int16_t dh6opt_len;
159 /* type-dependent data follows */
163 u_int16_t dh6opt_ia_type;
164 u_int16_t dh6opt_ia_len;
165 u_int32_t dh6opt_ia_iaid;
166 u_int32_t dh6opt_ia_t1;
167 u_int32_t dh6opt_ia_t2;
170 struct dhcp6_ia_prefix {
171 u_int16_t dh6opt_ia_prefix_type;
172 u_int16_t dh6opt_ia_prefix_len;
173 u_int32_t dh6opt_ia_prefix_pltime;
174 u_int32_t dh6opt_ia_prefix_vltime;
175 u_int8_t dh6opt_ia_prefix_plen;
176 struct in6_addr dh6opt_ia_prefix_addr;
177 } __attribute__ ((__packed__));
180 dhcp6opt_name(int type)
182 static char genstr[sizeof("opt_65535") + 1]; /* XXX thread unsafe */
185 return "INVALID option";
188 case DH6OPT_CLIENTID:
190 case DH6OPT_SERVERID:
195 return "option request";
196 case DH6OPT_PREFERENCE:
198 case DH6OPT_ELAPSED_TIME:
199 return "elapsed time";
200 case DH6OPT_RELAY_MSG:
201 return "relay message";
202 case DH6OPT_STATUS_CODE:
203 return "status code";
204 case DH6OPT_RAPID_COMMIT:
205 return "rapid commit";
206 case DH6OPT_INTERFACE_ID:
207 return "interface ID";
208 case DH6OPT_RECONF_MSG:
209 return "reconfigure message";
210 case DH6OPT_RECONF_ACCEPT:
211 return "reconfigure accept";
212 case DH6OPT_SIP_SERVER_D:
213 return "SIP Servers Domain";
214 case DH6OPT_SIP_SERVER_A:
215 return "SIP Servers Address";
218 case DH6OPT_PREFIX_DELEGATION:
219 return "prefix delegation";
220 case DH6OPT_PREFIX_INFORMATION:
221 return "prefix information";
224 case DH6OPT_IA_PD_PREFIX:
225 return "IA_PD prefix";
226 case DH6OPT_NTP_SERVERS:
229 snprintf(genstr, sizeof(genstr), "opt_%d", type);
235 dhcp6stcode(int code)
237 static char genstr[sizeof("code255") + 1]; /* XXX thread unsafe */
240 return "INVALID code";
243 case DH6OPT_STCODE_SUCCESS:
245 case DH6OPT_STCODE_UNSPECFAIL:
246 return "unspec failure";
247 case DH6OPT_STCODE_NOADDRAVAIL:
248 return "no addresses";
249 case DH6OPT_STCODE_NOBINDING:
251 case DH6OPT_STCODE_NOTONLINK:
252 return "not on-link";
253 case DH6OPT_STCODE_USEMULTICAST:
254 return "use multicast";
255 case DH6OPT_STCODE_NOPREFIXAVAIL:
256 return "no prefixes";
258 snprintf(genstr, sizeof(genstr), "code%d", code);
264 dhcp6opt_print(const u_char *cp, const u_char *ep)
266 struct dhcp6opt *dh6o;
273 struct in6_addr addr6;
275 struct dhcp6_ia_prefix ia_prefix;
280 if (ep < cp + sizeof(*dh6o))
282 dh6o = (struct dhcp6opt *)cp;
283 optlen = EXTRACT_16BITS(&dh6o->dh6opt_len);
284 if (ep < cp + sizeof(*dh6o) + optlen)
286 opttype = EXTRACT_16BITS(&dh6o->dh6opt_type);
287 printf(" (%s", dhcp6opt_name(opttype));
289 case DH6OPT_CLIENTID:
290 case DH6OPT_SERVERID:
296 tp = (u_char *)(dh6o + 1);
297 switch (EXTRACT_16BITS(tp)) {
299 if (optlen >= 2 + 6) {
300 printf(" hwaddr/time type %u time %u ",
301 EXTRACT_16BITS(&tp[2]),
302 EXTRACT_32BITS(&tp[4]));
303 for (i = 8; i < optlen; i++)
304 printf("%02x", tp[i]);
313 if (optlen >= 2 + 8) {
315 for (i = 2; i < 2 + 8; i++)
316 printf("%02x", tp[i]);
325 if (optlen >= 2 + 2) {
326 printf(" hwaddr type %u ",
327 EXTRACT_16BITS(&tp[2]));
328 for (i = 4; i < optlen; i++)
329 printf("%02x", tp[i]);
338 printf(" type %d)", EXTRACT_16BITS(tp));
347 tp = (u_char *)(dh6o + 1);
348 for (i = 0; i < optlen; i += 2) {
351 memcpy(&opt, &tp[i], sizeof(opt));
352 printf(" %s", dhcp6opt_name(ntohs(opt)));
356 case DH6OPT_PREFERENCE:
361 printf(" %d)", *((u_char *)(dh6o + 1) + 1));
363 case DH6OPT_ELAPSED_TIME:
368 memcpy(&val16, dh6o + 1, sizeof(val16));
369 val16 = ntohs(val16);
370 printf(" %d)", (int)val16);
372 case DH6OPT_RELAY_MSG:
374 dhcp6_print((const u_char *)(dh6o + 1), optlen);
377 case DH6OPT_RAPID_COMMIT: /* nothing todo */
380 case DH6OPT_INTERFACE_ID:
382 * Since we cannot predict the encoding, print hex dump
383 * at most 10 characters.
385 for (i = 0; i < optlen && i < 10; i++)
386 printf("%02x", ((u_char *)(dh6o + 1))[i]);
388 case DH6OPT_RECONF_MSG:
389 tp = (u_char *)(dh6o + 1);
392 printf(" for renew)");
395 printf(" for inf-req)");
398 printf(" for ?\?\?(%02x))", *tp);
402 case DH6OPT_RECONF_ACCEPT: /* nothing todo */
405 case DH6OPT_SIP_SERVER_A:
407 case DH6OPT_NTP_SERVERS:
412 tp = (u_char *)(dh6o + 1);
413 for (i = 0; i < optlen; i += 16)
414 printf(" %s", ip6addr_string(&tp[i]));
417 case DH6OPT_PREFIX_DELEGATION:
418 dhcp6opt_print((u_char *)(dh6o + 1),
419 (u_char *)(dh6o + 1) + optlen);
422 case DH6OPT_PREFIX_INFORMATION:
425 memcpy(&addr6, (u_char *)(dh6o + 1) + 5,
427 printf(" %s/%d", ip6addr_string(&addr6),
428 (int)*((u_char *)(dh6o + 1) + 4));
429 memcpy(&val32, dh6o + 1, sizeof(val32));
430 val32 = ntohl(val32);
431 if (val32 == DHCP6_DURATITION_INFINITE)
432 printf(" lease-duration: infinite)");
434 printf(" lease-duration: %u)", val32);
436 case DH6OPT_STATUS_CODE:
441 memcpy(&val16, (u_char *)(dh6o + 1), sizeof(val16));
442 val16 = ntohs(val16);
443 printf(" %s)", dhcp6stcode(val16));
447 if (optlen < sizeof(ia) - 4) {
451 memcpy(&ia, (u_char *)dh6o, sizeof(ia));
452 ia.dh6opt_ia_iaid = ntohl(ia.dh6opt_ia_iaid);
453 ia.dh6opt_ia_t1 = ntohl(ia.dh6opt_ia_t1);
454 ia.dh6opt_ia_t2 = ntohl(ia.dh6opt_ia_t2);
455 printf(" IAID:%lu T1:%lu T2:%lu",
456 (unsigned long)ia.dh6opt_ia_iaid,
457 (unsigned long)ia.dh6opt_ia_t1,
458 (unsigned long)ia.dh6opt_ia_t2);
459 if (optlen > sizeof(ia) - 4) {
460 /* there are sub-options */
461 dhcp6opt_print((u_char *)dh6o + sizeof(ia),
462 (u_char *)(dh6o + 1) + optlen);
466 case DH6OPT_IA_PD_PREFIX:
467 if (optlen < sizeof(ia_prefix) - 4) {
471 memcpy(&ia_prefix, (u_char *)dh6o, sizeof(ia_prefix));
473 ip6addr_string(&ia_prefix.dh6opt_ia_prefix_addr),
474 ia_prefix.dh6opt_ia_prefix_plen);
475 ia_prefix.dh6opt_ia_prefix_pltime =
476 ntohl(ia_prefix.dh6opt_ia_prefix_pltime);
477 ia_prefix.dh6opt_ia_prefix_vltime =
478 ntohl(ia_prefix.dh6opt_ia_prefix_vltime);
479 printf(" pltime:%lu vltime:%lu",
480 (unsigned long)ia_prefix.dh6opt_ia_prefix_pltime,
481 (unsigned long)ia_prefix.dh6opt_ia_prefix_vltime);
482 if (optlen > sizeof(ia_prefix) - 4) {
483 /* there are sub-options */
484 dhcp6opt_print((u_char *)dh6o +
486 (u_char *)(dh6o + 1) + optlen);
495 cp += sizeof(*dh6o) + optlen;
500 printf("[|dhcp6ext]");
504 * Print dhcp6 packets
507 dhcp6_print(const u_char *cp, u_int length)
510 struct dhcp6_relay *dh6relay;
517 ep = (u_char *)snapend;
518 if (cp + length < ep)
521 dh6 = (struct dhcp6 *)cp;
522 dh6relay = (struct dhcp6_relay *)cp;
523 TCHECK(dh6->dh6_xid);
524 switch (dh6->dh6_msgtype) {
552 case DH6_RECONFIGURE:
553 name = "reconfigure";
561 case DH6_RELAY_REPLY:
572 else if (dh6->dh6_msgtype != DH6_RELAY_FORW &&
573 dh6->dh6_msgtype != DH6_RELAY_REPLY) {
574 printf(" msgtype-%u", dh6->dh6_msgtype);
579 /* XXX relay agent messages have to be handled differently */
582 printf(" %s (", name); /*)*/
584 printf(" msgtype-%u (", dh6->dh6_msgtype); /*)*/
585 if (dh6->dh6_msgtype != DH6_RELAY_FORW &&
586 dh6->dh6_msgtype != DH6_RELAY_REPLY) {
587 printf("xid=%x", EXTRACT_32BITS(&dh6->dh6_xid) & DH6_XIDMASK);
588 extp = (u_char *)(dh6 + 1);
589 dhcp6opt_print(extp, ep);
590 } else { /* relay messages */
591 struct in6_addr addr6;
593 TCHECK(dh6relay->dh6relay_peeraddr);
595 memcpy(&addr6, dh6relay->dh6relay_linkaddr, sizeof (addr6));
596 printf("linkaddr=%s", ip6addr_string(&addr6));
598 memcpy(&addr6, dh6relay->dh6relay_peeraddr, sizeof (addr6));
599 printf(" peeraddr=%s", ip6addr_string(&addr6));
601 dhcp6opt_print((u_char *)(dh6relay + 1), ep);