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