]> The Tcpdump Group git mirrors - tcpdump/blob - print-dhcp6.c
Bring in KAME IPv6 tcpdump. replaces esp/ah/isakmp decoder.
[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.1 1999-10-30 05:11:12 itojun Exp $";
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38
39 #if __STDC__
40 struct mbuf;
41 struct rtentry;
42 #endif
43 #include <net/if.h>
44
45 #include <netinet/in.h>
46
47 #include <ctype.h>
48 #ifdef HAVE_MEMORY_H
49 #include <memory.h>
50 #endif
51 #include <stdio.h>
52 #include <string.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 #endif
62 static struct dhcp6_opt *dhcp6opttab_byname __P((char *));
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 { 2, 4, "Time Offset", OT6_NUM, },
73 { 3, OL6_N, "IEEE 1003.1 POSIX Timezone", OT6_STR, },
74 { 6, OL6_16N, "Domain Name Server", OT6_V6, },
75 { 10, OL6_N, "Domain Name", OT6_STR, },
76
77 /* Application and Service Parameters */
78 { 16, OL6_N, "Directory Agent", OT6_NONE, },
79 { 17, OL6_N, "Service Scope" , OT6_NONE, },
80 { 18, OL6_16N, "Network Time Protocol Servers", OT6_V6, },
81 { 19, OL6_N, "NIS Domain", OT6_STR, },
82 { 20, OL6_16N, "NIS Servers", OT6_V6, },
83 { 21, OL6_N, "NIS+ Domain", OT6_STR, },
84 { 22, OL6_16N, "NIS+ Servers", OT6_V6, },
85
86 /* TCP Parameters */
87 { 32, 4, "TCP Keepalive Interval", OT6_NUM, },
88
89 /* DHCPv6 Extensions */
90 { 40, 4, "Maximum DHCPv6 Message Size", OT6_NUM, },
91 { 41, OL6_N, "DHCP Retransmission and Configuration Parameter",
92 OT6_NONE, },
93 { 48, OL6_N, "Platform Specific Information", OT6_NONE, },
94 { 49, OL6_N, "Platform Class Identifier", OT6_STR, },
95 { 64, OL6_N, "Class Identifier", OT6_STR, },
96 { 66, 16, "Reconfigure Multicast Address", OT6_V6, },
97 { 67, 16, "Renumber DHCPv6 Server Address",
98 OT6_V6, },
99 { 68, OL6_N, "DHCP Relay ICMP Error Message", OT6_NONE, },
100 { 84, OL6_N, "Client-Server Authentication", OT6_NONE, },
101 { 85, 4, "Client Key Selection", OT6_NUM, },
102
103 /* End Extension */
104 { 65536, OL6_Z, "End", OT6_NONE, },
105
106 { 0 },
107 };
108
109 #if 0
110 static struct dhcp6_opt *dh6o_pad;
111 static struct dhcp6_opt *dh6o_end;
112
113 static void
114 dhcp6opttab_init()
115 {
116 dh6o_pad = dhcp6opttab_bycode(0);
117 dh6o_end = dhcp6opttab_bycode(65536);
118 }
119 #endif
120
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
133 static struct dhcp6_opt *
134 dhcp6opttab_bycode(code)
135 u_int code;
136 {
137 struct dhcp6_opt *p;
138
139 for (p = dh6opttab; p->code; p++)
140 if (p->code == code)
141 return p;
142 return NULL;
143 }
144
145 static void
146 dhcp6ext_print(u_char *cp, u_char *ep)
147 {
148 u_int16_t code, len;
149 struct dhcp6_opt *p;
150 char *q;
151 char buf[BUFSIZ];
152 int i;
153
154 if (cp == ep)
155 return;
156 printf(" ");
157 while (cp < ep) {
158 code = ntohs(*(u_int16_t *)&cp[0]);
159 if (code != 65535)
160 len = ntohs(*(u_int16_t *)&cp[2]);
161 else
162 len = 0;
163 p = dhcp6opttab_bycode(code);
164 if (p == NULL) {
165 printf("(unknown, len=%d)", len);
166 cp += len + 4;
167 continue;
168 }
169
170 /* sanity check on length */
171 switch (p->len) {
172 case OL6_N:
173 break;
174 case OL6_16N:
175 if (len % 16 != 0)
176 goto trunc;
177 break;
178 case OL6_Z:
179 if (len != 0)
180 goto trunc;
181 break;
182 default:
183 if (len != p->len)
184 goto trunc;
185 break;
186 }
187 if (cp + 4 + len > ep) {
188 printf("[|%s]", p->name);
189 return;
190 }
191
192 printf("(%s, ", p->name);
193 switch (p->type) {
194 case OT6_V6:
195 for (i = 0; i < len; i += 16) {
196 inet_ntop(AF_INET6, &cp[4 + i], buf,
197 sizeof(buf));
198 if (i != 0)
199 printf(",");
200 printf("%s", buf);
201 }
202 break;
203 case OT6_STR:
204 memset(&buf, 0, sizeof(buf));
205 strncpy(buf, &cp[4], len);
206 printf("%s", buf);
207 break;
208 case OT6_NUM:
209 printf("%d", (u_int32_t)ntohl(*(u_int32_t *)&cp[4]));
210 break;
211 default:
212 for (i = 0; i < len; i++)
213 printf("%02x", cp[4 + i] & 0xff);
214 }
215 printf(")");
216 cp += len + 4;
217 }
218 return;
219
220 trunc:
221 printf("[|dhcp6ext]");
222 }
223
224 /*
225 * Print dhcp6 requests
226 */
227 void
228 dhcp6_print(register const u_char *cp, u_int length,
229 u_short sport, u_short dport)
230 {
231 union dhcp6 *dh6;
232 u_char *ep;
233 u_char *extp;
234
235 printf("dhcp6");
236
237 ep = (u_char *)snapend;
238
239 dh6 = (union dhcp6 *)cp;
240 TCHECK(dh6->dh6_msgtype);
241 switch (dh6->dh6_msgtype) {
242 case DH6_SOLICIT:
243 if (vflag && TTEST(dh6->dh6_sol.dh6sol_relayaddr)) {
244 printf(" solicit(");
245 if ((dh6->dh6_sol.dh6sol_flags & DH6SOL_CLOSE) != 0)
246 printf("C");
247 if (dh6->dh6_sol.dh6sol_flags != 0)
248 printf(" ");
249 printf("cliaddr=%s",
250 ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
251 printf(" relayaddr=%s",
252 ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
253 printf(")");
254 } else
255 printf(" solicit");
256 break;
257 case DH6_ADVERT:
258 if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
259 printf(" advert");
260 break;
261 }
262 printf(" advert(");
263 if ((dh6->dh6_adv.dh6adv_flags & DH6ADV_SERVPRESENT) != 0)
264 printf("S");
265 if (dh6->dh6_adv.dh6adv_flags != 0)
266 printf(" ");
267 printf("pref=%u", dh6->dh6_adv.dh6adv_pref);
268 printf(" cliaddr=%s",
269 ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
270 printf(" relayaddr=%s",
271 ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
272 printf(" servaddr=%s",
273 ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
274 extp = (u_char *)((&dh6->dh6_adv) + 1);
275 dhcp6ext_print(extp, ep);
276 printf(")");
277 break;
278 case DH6_REQUEST:
279 if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
280 printf(" request");
281 break;
282 }
283 printf(" request(");
284 if ((dh6->dh6_req.dh6req_flags & DH6REQ_CLOSE) != 0)
285 printf("C");
286 if ((dh6->dh6_req.dh6req_flags & DH6REQ_SERVPRESENT) != 0)
287 printf("S");
288 if ((dh6->dh6_req.dh6req_flags & DH6REQ_REBOOT) != 0)
289 printf("R");
290 if (dh6->dh6_req.dh6req_flags != 0)
291 printf(" ");
292 printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
293 printf(" cliaddr=%s",
294 ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
295 printf(" relayaddr=%s",
296 ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
297 extp = (char *)((&dh6->dh6_req) + 1);
298 if ((dh6->dh6_req.dh6req_flags & DH6REQ_SERVPRESENT) != 0) {
299 printf(" servaddr=%s", ip6addr_string(extp));
300 extp += 16;
301 }
302 dhcp6ext_print(extp, ep);
303 printf(")");
304 break;
305 case DH6_REPLY:
306 if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
307 printf(" reply");
308 break;
309 }
310 printf(" reply(");
311 if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_CLIPRESENT) != 0)
312 printf("C");
313 if (dh6->dh6_rep.dh6rep_flagandstat != 0)
314 printf(" ");
315 printf("stat=0x%02x",
316 dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
317 extp = (u_char *)((&dh6->dh6_rep) + 1);
318 if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_CLIPRESENT) != 0) {
319 printf(" cliaddr=%s", ip6addr_string(extp));
320 extp += 16;
321 }
322 dhcp6ext_print(extp, ep);
323 printf(")");
324 break;
325 case DH6_RELEASE:
326 printf(" release");
327 break;
328 case DH6_RECONFIG:
329 printf(" reconfig");
330 break;
331 }
332 return;
333
334 trunc:
335 printf("%s", tstr);
336 }