]> The Tcpdump Group git mirrors - tcpdump/blob - print-dhcp6.c
remove extra prototype (now in interface.h)
[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.8 2000-05-17 05:00:12 itojun Exp $";
33 #endif
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/socket.h>
42
43 #if __STDC__
44 struct mbuf;
45 struct rtentry;
46 #endif
47 #include <net/if.h>
48
49 #include <netinet/in.h>
50
51 #include <ctype.h>
52 #ifdef HAVE_MEMORY_H
53 #include <memory.h>
54 #endif
55 #include <stdio.h>
56 #include <string.h>
57 #include <arpa/inet.h>
58
59 #include "interface.h"
60 #include "addrtoname.h"
61 #include "dhcp6.h"
62 #include "dhcp6opt.h"
63
64 #if 0
65 static void dhcp6opttab_init __P((void));
66 static struct dhcp6_opt *dhcp6opttab_byname __P((char *));
67 #endif
68 static struct dhcp6_opt *dhcp6opttab_bycode __P((u_int));
69
70 static char tstr[] = " [|dhcp6]";
71
72 static struct dhcp6_opt dh6opttab[] = {
73 /* IP Address Extension */
74 { 1, OL6_N, "IP Address", OT6_NONE, },
75
76 /* General Extension */
77 { 8193, OL6_N, "IEEE 1003.1 POSIX Timezone", OT6_STR, },
78 { 8194, OL6_16N, "Domain Name Server", OT6_V6, },
79 { 8195, OL6_N, "Domain Name", OT6_STR, },
80
81 { 8196, OL6_N, "SLP Agent", OT6_NONE, },
82 { 8197, OL6_N, "SLP Scope" , OT6_NONE, },
83 { 8198, OL6_16N, "Network Time Protocol Servers", OT6_V6, },
84 { 8199, OL6_N, "NIS Domain", OT6_STR, },
85 { 8200, OL6_16N, "NIS Servers", OT6_V6, },
86 { 8201, OL6_N, "NIS+ Domain", OT6_STR, },
87 { 8202, OL6_16N, "NIS+ Servers", OT6_V6, },
88
89 /* TCP Parameters */
90 { 8203, 4, "TCP Keepalive Interval", OT6_NUM, },
91
92 /* DHCPv6 Extensions */
93 { 8204, 4, "Maximum DHCPv6 Message Size", OT6_NUM, },
94 { 8205, OL6_N, "DHCP Retransmission and Configuration Parameter",
95 OT6_NONE, },
96 { 8206, OL6_N, "Extension Request", OT6_NONE, },
97 { 8207, OL6_N, "Subnet Prefix", OT6_NONE, },
98 { 8208, OL6_N, "Platform Specific Information", OT6_NONE, },
99 { 8209, OL6_N, "Platform Class Identifier", OT6_STR, },
100 { 8210, OL6_N, "Class Identifier", OT6_STR, },
101 { 8211, 16, "Reconfigure Multicast Address", OT6_V6, },
102 { 8212, 16, "Renumber DHCPv6 Server Address",
103 OT6_V6, },
104 { 8213, OL6_N, "Client-Server Authentication", OT6_NONE, },
105 { 8214, 4, "Client Key Selection", OT6_NUM, },
106
107 /* End Extension */
108 { 65536, OL6_Z, "End", OT6_NONE, },
109
110 { 0 },
111 };
112
113 #if 0
114 static struct dhcp6_opt *dh6o_pad;
115 static struct dhcp6_opt *dh6o_end;
116
117 static void
118 dhcp6opttab_init()
119 {
120 dh6o_pad = dhcp6opttab_bycode(0);
121 dh6o_end = dhcp6opttab_bycode(65536);
122 }
123 #endif
124
125 #if 0
126 static struct dhcp6_opt *
127 dhcp6opttab_byname(name)
128 char *name;
129 {
130 struct dhcp6_opt *p;
131
132 for (p = dh6opttab; p->code; p++)
133 if (strcmp(name, p->name) == 0)
134 return p;
135 return NULL;
136 }
137 #endif
138
139 static struct dhcp6_opt *
140 dhcp6opttab_bycode(code)
141 u_int code;
142 {
143 struct dhcp6_opt *p;
144
145 for (p = dh6opttab; p->code; p++)
146 if (p->code == code)
147 return p;
148 return NULL;
149 }
150
151 static void
152 dhcp6ext_print(u_char *cp, u_char *ep)
153 {
154 u_int16_t code, len;
155 struct dhcp6_opt *p;
156 char buf[BUFSIZ];
157 int i;
158
159 if (cp == ep)
160 return;
161 printf(" ");
162 while (cp < ep) {
163 if (ep - cp < sizeof(u_int16_t))
164 break;
165 code = ntohs(*(u_int16_t *)&cp[0]);
166 if (ep - cp < sizeof(u_int16_t) * 2)
167 break;
168 if (code != 65535)
169 len = ntohs(*(u_int16_t *)&cp[2]);
170 else
171 len = 0;
172 if (ep - cp < len + 4)
173 break;
174 p = dhcp6opttab_bycode(code);
175 if (p == NULL) {
176 printf("(unknown, len=%d)", len);
177 cp += len + 4;
178 continue;
179 }
180
181 /* sanity check on length */
182 switch (p->len) {
183 case OL6_N:
184 break;
185 case OL6_16N:
186 if (len % 16 != 0)
187 goto trunc;
188 break;
189 case OL6_Z:
190 if (len != 0)
191 goto trunc;
192 break;
193 default:
194 if (len != p->len)
195 goto trunc;
196 break;
197 }
198 if (cp + 4 + len > ep) {
199 printf("[|%s]", p->name);
200 return;
201 }
202
203 printf("(%s, ", p->name);
204 switch (p->type) {
205 case OT6_V6:
206 for (i = 0; i < len; i += 16) {
207 inet_ntop(AF_INET6, &cp[4 + i], buf,
208 sizeof(buf));
209 if (i != 0)
210 printf(",");
211 printf("%s", buf);
212 }
213 break;
214 case OT6_STR:
215 memset(&buf, 0, sizeof(buf));
216 strncpy(buf, &cp[4], len);
217 printf("%s", buf);
218 break;
219 case OT6_NUM:
220 printf("%d", (u_int32_t)ntohl(*(u_int32_t *)&cp[4]));
221 break;
222 default:
223 for (i = 0; i < len; i++)
224 printf("%02x", cp[4 + i] & 0xff);
225 }
226 printf(")");
227 cp += len + 4;
228 }
229 return;
230
231 trunc:
232 printf("[|dhcp6ext]");
233 }
234
235 /*
236 * Print dhcp6 requests
237 */
238 void
239 dhcp6_print(register const u_char *cp, u_int length,
240 u_int16_t sport, u_int16_t dport)
241 {
242 union dhcp6 *dh6;
243 u_char *ep;
244 u_char *extp;
245 u_int16_t field16;
246
247 printf("dhcp6");
248
249 ep = (u_char *)snapend;
250
251 dh6 = (union dhcp6 *)cp;
252 TCHECK(dh6->dh6_msgtype);
253 switch (dh6->dh6_msgtype) {
254 case DH6_SOLICIT:
255 if (!(vflag && TTEST(dh6->dh6_sol.dh6sol_relayaddr))) {
256 printf(" solicit");
257 break;
258 }
259
260 printf(" solicit("); /*)*/
261 if (dh6->dh6_sol.dh6sol_flags != 0) {
262 u_int8_t f = dh6->dh6_sol.dh6sol_flags;
263 printf("%s%s ",
264 (f & DH6SOL_PREFIX) ? "P" : "",
265 (f & DH6SOL_CLOSE) ? "C" : "");
266 }
267
268 memcpy(&field16, &dh6->dh6_sol.dh6sol_plen_id,
269 sizeof(field16));
270 field16 = ntohs(field16);
271 if (field16 & ~DH6SOL_SOLICIT_PLEN_MASK)
272 printf("plen=%d ", DH6SOL_SOLICIT_PLEN(field16));
273 printf("solicit-ID=%d", DH6SOL_SOLICIT_ID(field16));
274
275 printf(" cliaddr=%s",
276 ip6addr_string(&dh6->dh6_sol.dh6sol_cliaddr));
277 printf(" relayaddr=%s",
278 ip6addr_string(&dh6->dh6_sol.dh6sol_relayaddr));
279 /*(*/
280 printf(")");
281 break;
282 case DH6_ADVERT:
283 if (!(vflag && TTEST(dh6->dh6_adv.dh6adv_serveraddr))) {
284 printf(" advert");
285 break;
286 }
287 printf(" advert("); /*)*/
288 memcpy(&field16, &dh6->dh6_adv.dh6adv_rsv_id, sizeof(field16));
289 printf("solicit-ID=%d",
290 ntohs(field16) & DH6SOL_SOLICIT_ID_MASK);
291 printf(" pref=%u", dh6->dh6_adv.dh6adv_pref);
292 printf(" cliaddr=%s",
293 ip6addr_string(&dh6->dh6_adv.dh6adv_cliaddr));
294 printf(" relayaddr=%s",
295 ip6addr_string(&dh6->dh6_adv.dh6adv_relayaddr));
296 printf(" servaddr=%s",
297 ip6addr_string(&dh6->dh6_adv.dh6adv_serveraddr));
298 extp = (u_char *)((&dh6->dh6_adv) + 1);
299 dhcp6ext_print(extp, ep);
300 /*(*/
301 printf(")");
302 break;
303 case DH6_REQUEST:
304 if (!(vflag && TTEST(dh6->dh6_req.dh6req_relayaddr))) {
305 printf(" request");
306 break;
307 }
308 printf(" request("); /*)*/
309 if (dh6->dh6_req.dh6req_flags != 0) {
310 u_int8_t f = dh6->dh6_req.dh6req_flags;
311 printf("%s%s ",
312 (f & DH6REQ_CLOSE) ? "C" : "",
313 (f & DH6REQ_REBOOT) ? "R" : "");
314 }
315 printf("xid=0x%04x", dh6->dh6_req.dh6req_xid);
316 printf(" cliaddr=%s",
317 ip6addr_string(&dh6->dh6_req.dh6req_cliaddr));
318 printf(" relayaddr=%s",
319 ip6addr_string(&dh6->dh6_req.dh6req_relayaddr));
320 printf(" servaddr=%s",
321 ip6addr_string(&dh6->dh6_req.dh6req_serveraddr));
322 dhcp6ext_print((char *)(&dh6->dh6_req + 1), ep);
323 /*(*/
324 printf(")");
325 break;
326 case DH6_REPLY:
327 if (!(vflag && TTEST(dh6->dh6_rep.dh6rep_xid))) {
328 printf(" reply");
329 break;
330 }
331 printf(" reply("); /*)*/
332 if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) != 0)
333 printf("R ");
334 printf("stat=0x%02x",
335 dh6->dh6_rep.dh6rep_flagandstat & DH6REP_STATMASK);
336 printf(" xid=0x%04x", dh6->dh6_rep.dh6rep_xid);
337 printf(" cliaddr=%s",
338 ip6addr_string(&dh6->dh6_rep.dh6rep_cliaddr));
339 extp = (u_char *)((&dh6->dh6_rep) + 1);
340 if ((dh6->dh6_rep.dh6rep_flagandstat & DH6REP_RELAYPRESENT) !=
341 0) {
342 printf(" relayaddr=%s", ip6addr_string(extp));
343 extp += sizeof(struct in6_addr);
344 }
345 dhcp6ext_print(extp, ep);
346 /*(*/
347 printf(")");
348 break;
349 case DH6_RELEASE:
350 printf(" release");
351 break;
352 case DH6_RECONFIG:
353 printf(" reconfig");
354 break;
355 }
356 return;
357
358 trunc:
359 printf("%s", tstr);
360 }