]> The Tcpdump Group git mirrors - tcpdump/blob - print-dhcp6.c
use snprintf
[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 * draft-ietf-dhc-dhcpv6-28.txt
31 */
32
33 #ifndef lint
34 static const char rcsid[] =
35 "@(#) $Header: /tcpdump/master/tcpdump/print-dhcp6.c,v 1.26 2003-08-13 02:26:53 itojun Exp $";
36 #endif
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <tcpdump-stdinc.h>
43
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "extract.h"
50
51 /* lease duration */
52 #define DHCP6_DURATITION_INFINITE 0xffffffff
53
54 /* Error Values */
55 #define DH6ERR_FAILURE 16
56 #define DH6ERR_AUTHFAIL 17
57 #define DH6ERR_POORLYFORMED 18
58 #define DH6ERR_UNAVAIL 19
59 #define DH6ERR_OPTUNAVAIL 20
60
61 /* Message type */
62 #define DH6_SOLICIT 1
63 #define DH6_ADVERTISE 2
64 #define DH6_REQUEST 3
65 #define DH6_CONFIRM 4
66 #define DH6_RENEW 5
67 #define DH6_REBIND 6
68 #define DH6_REPLY 7
69 #define DH6_RELEASE 8
70 #define DH6_INFORM_REQ 11
71 #define DH6_RELAY_FORW 12
72 #define DH6_RELAY_REPLY 13
73
74 /* DHCP6 base packet format */
75 struct dhcp6 {
76 union {
77 u_int8_t m;
78 u_int32_t x;
79 } dh6_msgtypexid;
80 /* options follow */
81 };
82 #define dh6_msgtype dh6_msgtypexid.m
83 #define dh6_xid dh6_msgtypexid.x
84 #define DH6_XIDMASK 0x00ffffff
85
86 /* DHCPv6 relay messages */
87 struct dhcp6_relay {
88 u_int8_t dh6relay_msgtype;
89 u_int8_t dh6relay_hcnt;
90 u_int8_t dh6relay_linkaddr[16]; /* XXX: badly aligned */
91 u_int8_t dh6relay_peeraddr[16];
92 /* options follow */
93 };
94
95 /* options */
96 #define DH6OPT_CLIENTID 1
97 #define DH6OPT_SERVERID 2
98 #define DH6OPT_IA_NA 3
99 #define DH6OPT_IA_TMP 4
100 #define DH6OPT_IADDR 5
101 #define DH6OPT_ORO 6
102 #define DH6OPT_PREFERENCE 7
103 # define DH6OPT_PREF_UNDEF -1
104 # define DH6OPT_PREF_MAX 255
105 #define DH6OPT_ELAPSED_TIME 8
106 #define DH6OPT_RELAY_MSG 9
107 /*#define DH6OPT_SERVER_MSG 10 deprecated */
108 #define DH6OPT_AUTH 11
109 #define DH6OPT_UNICAST 12
110 #define DH6OPT_STATUS_CODE 13
111 # define DH6OPT_STCODE_SUCCESS 0
112 # define DH6OPT_STCODE_UNSPECFAIL 1
113 # define DH6OPT_STCODE_NOADDRAVAIL 2
114 # define DH6OPT_STCODE_NOBINDING 3
115 # define DH6OPT_STCODE_NOTONLINK 4
116 # define DH6OPT_STCODE_USEMULTICAST 5
117 # define DH6OPT_STCODE_NOPREFIXAVAIL 6
118 #define DH6OPT_RAPID_COMMIT 14
119 #define DH6OPT_USER_CLASS 15
120 #define DH6OPT_VENDOR_CLASS 16
121 #define DH6OPT_VENDOR_OPTS 17
122 #define DH6OPT_INTERFACE_ID 18
123 #define DH6OPT_RECONF_MSG 19
124
125 /*
126 * The option type has not been assigned for the following options.
127 * We temporarily adopt values used in the service specification document
128 * (200206xx version) by NTT Communications.
129 * Note that we'll change the following definitions if different type values
130 * are officially assigned.
131 */
132 #define DH6OPT_DNS 25
133 #define DH6OPT_PREFIX_DELEGATION 30
134 #define DH6OPT_PREFIX_INFORMATION 31
135 #define DH6OPT_PREFIX_REQUEST 32
136
137 /*
138 * The followings are also unassigned numbers.
139 * We temporarily use values as of KAME snap 20030106.
140 */
141 #define DH6OPT_IA_PD 33
142 #define DH6OPT_IA_PD_PREFIX 34
143
144 struct dhcp6opt {
145 u_int16_t dh6opt_type;
146 u_int16_t dh6opt_len;
147 /* type-dependent data follows */
148 };
149
150 struct dhcp6_ia {
151 u_int16_t dh6opt_ia_type;
152 u_int16_t dh6opt_ia_len;
153 u_int32_t dh6opt_ia_iaid;
154 u_int32_t dh6opt_ia_t1;
155 u_int32_t dh6opt_ia_t2;
156 };
157
158 struct dhcp6_ia_prefix {
159 u_int16_t dh6opt_ia_prefix_type;
160 u_int16_t dh6opt_ia_prefix_len;
161 u_int32_t dh6opt_ia_prefix_pltime;
162 u_int32_t dh6opt_ia_prefix_vltime;
163 u_int8_t dh6opt_ia_prefix_plen;
164 struct in6_addr dh6opt_ia_prefix_addr;
165 } __attribute__ ((__packed__));
166
167 static const char *
168 dhcp6opt_name(int type)
169 {
170 static char genstr[sizeof("opt_65535") + 1]; /* XXX thread unsafe */
171
172 if (type > 65535)
173 return "INVALID option";
174
175 switch(type) {
176 case DH6OPT_CLIENTID:
177 return "client ID";
178 case DH6OPT_SERVERID:
179 return "server ID";
180 case DH6OPT_IA_NA:
181 return "IA_NA";
182 case DH6OPT_ORO:
183 return "option request";
184 case DH6OPT_PREFERENCE:
185 return "preference";
186 case DH6OPT_ELAPSED_TIME:
187 return "elapsed time";
188 case DH6OPT_RELAY_MSG:
189 return "relay message";
190 case DH6OPT_STATUS_CODE:
191 return "status code";
192 case DH6OPT_RAPID_COMMIT:
193 return "rapid commit";
194 case DH6OPT_INTERFACE_ID:
195 return "interface ID";
196 case DH6OPT_DNS:
197 return "DNS";
198 case DH6OPT_PREFIX_DELEGATION:
199 return "prefix delegation";
200 case DH6OPT_PREFIX_INFORMATION:
201 return "prefix information";
202 case DH6OPT_IA_PD:
203 return "IA_PD";
204 case DH6OPT_IA_PD_PREFIX:
205 return "IA_PD prefix";
206 default:
207 snprintf(genstr, sizeof(genstr), "opt_%d", type);
208 return(genstr);
209 }
210 }
211
212 static const char *
213 dhcp6stcode(int code)
214 {
215 static char genstr[sizeof("code255") + 1]; /* XXX thread unsafe */
216
217 if (code > 255)
218 return "INVALID code";
219
220 switch(code) {
221 case DH6OPT_STCODE_SUCCESS:
222 return "success";
223 case DH6OPT_STCODE_UNSPECFAIL:
224 return "unspec failure";
225 case DH6OPT_STCODE_NOADDRAVAIL:
226 return "no addresses";
227 case DH6OPT_STCODE_NOBINDING:
228 return "no binding";
229 case DH6OPT_STCODE_NOTONLINK:
230 return "not on-link";
231 case DH6OPT_STCODE_USEMULTICAST:
232 return "use multicast";
233 case DH6OPT_STCODE_NOPREFIXAVAIL:
234 return "no prefixes";
235 default:
236 snprintf(genstr, sizeof(genstr), "code%d", code);
237 return(genstr);
238 }
239 }
240
241 static void
242 dhcp6opt_print(const u_char *cp, const u_char *ep)
243 {
244 struct dhcp6opt *dh6o;
245 u_char *tp;
246 size_t i;
247 u_int16_t opttype;
248 size_t optlen;
249 u_int16_t val16;
250 u_int32_t val32;
251 struct in6_addr addr6;
252 struct dhcp6_ia ia;
253 struct dhcp6_ia_prefix ia_prefix;
254
255 if (cp == ep)
256 return;
257 while (cp < ep) {
258 if (ep < cp + sizeof(*dh6o))
259 goto trunc;
260 dh6o = (struct dhcp6opt *)cp;
261 optlen = EXTRACT_16BITS(&dh6o->dh6opt_len);
262 if (ep < cp + sizeof(*dh6o) + optlen)
263 goto trunc;
264 opttype = EXTRACT_16BITS(&dh6o->dh6opt_type);
265 printf(" (%s", dhcp6opt_name(opttype));
266 switch (opttype) {
267 case DH6OPT_CLIENTID:
268 case DH6OPT_SERVERID:
269 if (optlen < 2) {
270 /*(*/
271 printf(" ?)");
272 break;
273 }
274 tp = (u_char *)(dh6o + 1);
275 switch (EXTRACT_16BITS(tp)) {
276 case 1:
277 if (optlen >= 2 + 6) {
278 printf(" hwaddr/time type %u time %u ",
279 EXTRACT_16BITS(&tp[2]),
280 EXTRACT_32BITS(&tp[4]));
281 for (i = 8; i < optlen; i++)
282 printf("%02x", tp[i]);
283 /*(*/
284 printf(")");
285 } else {
286 /*(*/
287 printf(" ?)");
288 }
289 break;
290 case 2:
291 if (optlen >= 2 + 8) {
292 printf(" vid ");
293 for (i = 2; i < 2 + 8; i++)
294 printf("%02x", tp[i]);
295 /*(*/
296 printf(")");
297 } else {
298 /*(*/
299 printf(" ?)");
300 }
301 break;
302 case 3:
303 if (optlen >= 2 + 2) {
304 printf(" hwaddr type %u ",
305 EXTRACT_16BITS(&tp[2]));
306 for (i = 4; i < optlen; i++)
307 printf("%02x", tp[i]);
308 /*(*/
309 printf(")");
310 } else {
311 /*(*/
312 printf(" ?)");
313 }
314 break;
315 default:
316 printf(" type %d)", EXTRACT_16BITS(tp));
317 break;
318 }
319 break;
320 case DH6OPT_ORO:
321 if (optlen % 2) {
322 printf(" ?)");
323 break;
324 }
325 tp = (u_char *)(dh6o + 1);
326 for (i = 0; i < optlen; i += 2) {
327 u_int16_t opt;
328
329 memcpy(&opt, &tp[i], sizeof(opt));
330 printf(" %s", dhcp6opt_name(ntohs(opt)));
331 }
332 printf(")");
333 break;
334 case DH6OPT_PREFERENCE:
335 if (optlen != 1) {
336 printf(" ?)");
337 break;
338 }
339 printf(" %d)", *((u_char *)(dh6o + 1) + 1));
340 break;
341 case DH6OPT_ELAPSED_TIME:
342 if (optlen != 2) {
343 printf(" ?)");
344 break;
345 }
346 memcpy(&val16, dh6o + 1, sizeof(val16));
347 val16 = ntohs(val16);
348 printf(" %d)", (int)val16);
349 break;
350 case DH6OPT_RELAY_MSG:
351 printf(" (");
352 dhcp6_print((const u_char *)(dh6o + 1), optlen, 0, 0);
353 printf(")");
354 break;
355 case DH6OPT_RAPID_COMMIT: /* nothing todo */
356 printf(")");
357 break;
358 case DH6OPT_INTERFACE_ID:
359 /*
360 * Since we cannot predict the encoding, print hex dump
361 * at most 10 characters.
362 */
363 for (i = 0; i < optlen && i < 10; i++)
364 printf("%02x", ((u_char *)(dh6o + 1))[i]);
365 break;
366 case DH6OPT_DNS:
367 if (optlen % 16) {
368 printf(" ?)");
369 break;
370 }
371 tp = (u_char *)(dh6o + 1);
372 for (i = 0; i < optlen; i += 16)
373 printf(" %s", ip6addr_string(&tp[i]));
374 printf(")");
375 break;
376 case DH6OPT_PREFIX_DELEGATION:
377 dhcp6opt_print((u_char *)(dh6o + 1),
378 (u_char *)(dh6o + 1) + optlen);
379 printf(")");
380 break;
381 case DH6OPT_PREFIX_INFORMATION:
382 if (optlen % 21)
383 printf(" ?)");
384 memcpy(&addr6, (u_char *)(dh6o + 1) + 5,
385 sizeof(addr6));
386 printf(" %s/%d", ip6addr_string(&addr6),
387 (int)*((u_char *)(dh6o + 1) + 4));
388 memcpy(&val32, dh6o + 1, sizeof(val32));
389 val32 = ntohl(val32);
390 if (val32 == DHCP6_DURATITION_INFINITE)
391 printf(" lease-duration: infinite)");
392 else
393 printf(" lease-duration: %u)", val32);
394 break;
395 case DH6OPT_STATUS_CODE:
396 if (optlen < 2) {
397 printf(" ?)");
398 break;
399 }
400 memcpy(&val16, (u_char *)(dh6o + 1), sizeof(val16));
401 val16 = ntohs(val16);
402 printf(" %s)", dhcp6stcode(val16));
403 break;
404 case DH6OPT_IA_NA:
405 case DH6OPT_IA_PD:
406 if (optlen < sizeof(ia) - 4) {
407 printf(" ?)");
408 break;
409 }
410 memcpy(&ia, (u_char *)dh6o, sizeof(ia));
411 ia.dh6opt_ia_iaid = ntohl(ia.dh6opt_ia_iaid);
412 ia.dh6opt_ia_t1 = ntohl(ia.dh6opt_ia_t1);
413 ia.dh6opt_ia_t2 = ntohl(ia.dh6opt_ia_t2);
414 printf(" IAID:%lu T1:%lu T2:%lu",
415 (unsigned long)ia.dh6opt_ia_iaid,
416 (unsigned long)ia.dh6opt_ia_t1,
417 (unsigned long)ia.dh6opt_ia_t2);
418 if (optlen > sizeof(ia) - 4) {
419 /* there are sub-options */
420 dhcp6opt_print((u_char *)dh6o + sizeof(ia),
421 (u_char *)(dh6o + 1) + optlen);
422 }
423 printf(")");
424 break;
425 case DH6OPT_IA_PD_PREFIX:
426 if (optlen < sizeof(ia_prefix) - 4) {
427 printf(" ?)");
428 break;
429 }
430 memcpy(&ia_prefix, (u_char *)dh6o, sizeof(ia_prefix));
431 printf(" %s/%d",
432 ip6addr_string(&ia_prefix.dh6opt_ia_prefix_addr),
433 ia_prefix.dh6opt_ia_prefix_plen);
434 ia_prefix.dh6opt_ia_prefix_pltime =
435 ntohl(ia_prefix.dh6opt_ia_prefix_pltime);
436 ia_prefix.dh6opt_ia_prefix_vltime =
437 ntohl(ia_prefix.dh6opt_ia_prefix_vltime);
438 printf(" pltime:%lu vltime:%lu",
439 (unsigned long)ia_prefix.dh6opt_ia_prefix_pltime,
440 (unsigned long)ia_prefix.dh6opt_ia_prefix_vltime);
441 if (optlen > sizeof(ia_prefix) - 4) {
442 /* there are sub-options */
443 dhcp6opt_print((u_char *)dh6o +
444 sizeof(ia_prefix),
445 (u_char *)(dh6o + 1) + optlen);
446 }
447 printf(")");
448 break;
449 default:
450 printf(")");
451 break;
452 }
453
454 cp += sizeof(*dh6o) + optlen;
455 }
456 return;
457
458 trunc:
459 printf("[|dhcp6ext]");
460 }
461
462 /*
463 * Print dhcp6 packets
464 */
465 void
466 dhcp6_print(const u_char *cp, u_int length,
467 u_int16_t sport, u_int16_t dport)
468 {
469 struct dhcp6 *dh6;
470 struct dhcp6_relay *dh6relay;
471 const u_char *ep;
472 u_char *extp;
473 const char *name;
474
475 printf("dhcp6");
476
477 ep = (u_char *)snapend;
478 if (cp + length < ep)
479 ep = cp + length;
480
481 dh6 = (struct dhcp6 *)cp;
482 dh6relay = (struct dhcp6_relay *)cp;
483 TCHECK(dh6->dh6_xid);
484 switch (dh6->dh6_msgtype) {
485 case DH6_SOLICIT:
486 name = "solicit";
487 break;
488 case DH6_ADVERTISE:
489 name = "advertise";
490 break;
491 case DH6_REQUEST:
492 name = "request";
493 break;
494 case DH6_CONFIRM:
495 name = "confirm";
496 break;
497 case DH6_RENEW:
498 name = "renew";
499 break;
500 case DH6_REBIND:
501 name = "rebind";
502 break;
503 case DH6_REPLY:
504 name = "reply";
505 break;
506 case DH6_RELEASE:
507 name = "release";
508 break;
509 case DH6_INFORM_REQ:
510 name= "inf-req";
511 break;
512 case DH6_RELAY_FORW:
513 name= "relay-fwd";
514 break;
515 case DH6_RELAY_REPLY:
516 name= "relay-reply";
517 break;
518 default:
519 name = NULL;
520 break;
521 }
522
523 if (!vflag) {
524 if (name)
525 printf(" %s", name);
526 else if (dh6->dh6_msgtype != DH6_RELAY_FORW &&
527 dh6->dh6_msgtype != DH6_RELAY_REPLY) {
528 printf(" msgtype-%u", dh6->dh6_msgtype);
529 }
530 return;
531 }
532
533 /* XXX relay agent messages have to be handled differently */
534
535 if (name)
536 printf(" %s (", name); /*)*/
537 else
538 printf(" msgtype-%u (", dh6->dh6_msgtype); /*)*/
539 if (dh6->dh6_msgtype != DH6_RELAY_FORW &&
540 dh6->dh6_msgtype != DH6_RELAY_REPLY) {
541 printf("xid=%x", EXTRACT_32BITS(&dh6->dh6_xid) & DH6_XIDMASK);
542 extp = (u_char *)(dh6 + 1);
543 dhcp6opt_print(extp, ep);
544 } else { /* relay messages */
545 struct in6_addr addr6;
546
547 TCHECK(dh6relay->dh6relay_peeraddr);
548
549 memcpy(&addr6, dh6relay->dh6relay_linkaddr, sizeof (addr6));
550 printf("linkaddr=%s", ip6addr_string(&addr6));
551
552 memcpy(&addr6, dh6relay->dh6relay_peeraddr, sizeof (addr6));
553 printf(" peeraddr=%s", ip6addr_string(&addr6));
554
555 dhcp6opt_print((u_char *)(dh6relay + 1), ep);
556 }
557 /*(*/
558 printf(")");
559 return;
560
561 trunc:
562 printf("[|dhcp6]");
563 }