2 * Copyright (C) 1998 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
35 static const char rcsid[] =
36 "@(#) $Header: /tcpdump/master/tcpdump/print-ip6opts.c,v 1.8 2000/12/13 07:57:05 itojun Exp $";
40 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
50 #include "interface.h"
51 #include "addrtoname.h"
53 /* items outside of rfc2292bis */
55 #define IP6OPT_MINLEN 2
57 #ifndef IP6OPT_RTALERT_LEN
58 #define IP6OPT_RTALERT_LEN 4
60 #ifndef IP6OPT_JUMBO_LEN
61 #define IP6OPT_JUMBO_LEN 6
63 #define IP6OPT_HOMEADDR_MINLEN 18
64 #define IP6OPT_BU_MINLEN 10
65 #define IP6OPT_BA_MINLEN 13
66 #define IP6OPT_BR_MINLEN 2
67 #define IP6SOPT_ALTCOA 0x4
68 #define IP6SOPT_ALTCOA_MINLEN 18
69 #define IP6SOPT_UI 0x2
70 #define IP6SOPT_UI_MINLEN 4
72 static void ip6_sopt_print(const u_char *, int);
75 ip6_sopt_print(const u_char *bp, int len)
80 for (i = 0; i < len; i += optlen) {
87 if (len - i < IP6OPT_MINLEN) {
88 printf(", padn: trunc");
92 optlen = bp[i + 1] + 2;
95 if (len - i < IP6SOPT_ALTCOA_MINLEN) {
96 printf(", altcoa: trunc");
99 printf(", alt-CoA: %s", ip6addr_string(&bp[i+2]));
100 optlen = bp[i + 1] + 2;
103 if (len - i < IP6SOPT_UI_MINLEN) {
104 printf(", ui: trunc");
107 printf("(ui: 0x%04x) ", ntohs(*(u_int16_t *)&bp[i + 2]));
108 optlen = bp[i + 1] + 2;
111 if (len - i < IP6OPT_MINLEN) {
112 printf(", sopt_type %d: trunc)", bp[i]);
115 printf(", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1]);
116 optlen = bp[i + 1] + 2;
127 ip6_opt_print(const u_char *bp, int len)
132 for (i = 0; i < len; i += optlen) {
139 if (len - i < IP6OPT_MINLEN) {
140 printf("(padn: trunc)");
144 optlen = bp[i + 1] + 2;
146 case IP6OPT_ROUTER_ALERT:
147 if (len - i < IP6OPT_RTALERT_LEN) {
148 printf("(rtalert: trunc)");
151 if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
152 printf("(rtalert: invalid len %d)", bp[i + 1]);
155 printf("(rtalert: 0x%04x) ", ntohs(*(u_int16_t *)&bp[i + 2]));
156 optlen = IP6OPT_RTALERT_LEN;
159 if (len - i < IP6OPT_JUMBO_LEN) {
160 printf("(jumbo: trunc)");
163 if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
164 printf("(jumbo: invalid len %d)", bp[i + 1]);
167 printf("(jumbo: %u) ", (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 2]));
168 optlen = IP6OPT_JUMBO_LEN;
170 case IP6OPT_HOME_ADDRESS:
171 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
172 printf("(homeaddr: trunc)");
175 if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) {
176 printf("(homeaddr: invalid len %d)", bp[i + 1]);
179 printf("(homeaddr: %s", ip6addr_string(&bp[i + 2]));
180 if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) {
181 ip6_sopt_print(&bp[i + IP6OPT_HOMEADDR_MINLEN],
182 (optlen-IP6OPT_HOMEADDR_MINLEN));
185 optlen = bp[i + 1] + 2;
187 case IP6OPT_BINDING_UPDATE:
188 if (len - i < IP6OPT_BU_MINLEN) {
189 printf("(bu: trunc)");
192 if (bp[i + 1] < IP6OPT_BU_MINLEN - 2) {
193 printf("(bu: invalid len %d)", bp[i + 1]);
197 if (bp[i + 2] & 0x80)
199 if (bp[i + 2] & 0x40)
201 if (bp[i + 2] & 0x20)
203 if (bp[i + 2] & 0x10)
205 if (bp[i + 2] & 0x0f)
207 printf(", prefixlen: %u", bp[i + 3]);
208 printf(", sequence: %u",
209 (u_int16_t)ntohs(*(u_int16_t *)&bp[i + 4]));
210 printf(", lifetime: %u",
211 (u_int32_t)ntohs(*(u_int32_t *)&bp[i + 8]));
213 optlen = bp[i + 1] + 2;
214 if (bp[i + 1] > IP6OPT_BU_MINLEN - 2) {
215 ip6_sopt_print(&bp[i + IP6OPT_BU_MINLEN],
216 (optlen - IP6OPT_BU_MINLEN));
220 case IP6OPT_BINDING_ACK:
221 if (len - i < IP6OPT_BA_MINLEN) {
222 printf("(ba: trunc)");
225 if (bp[i + 1] < IP6OPT_BA_MINLEN - 2) {
226 printf("(ba: invalid len %d)", bp[i + 1]);
230 printf("status: %u", bp[i + 2]);
231 printf(", sequence: %u",
232 (u_int16_t)ntohs(*(u_int16_t *)&bp[i + 3]));
233 printf(", lifetime: %u",
234 (u_int32_t)ntohs(*(u_int32_t *)&bp[i + 7]));
235 printf(", refresh: %u",
236 (u_int32_t)ntohs(*(u_int32_t *)&bp[i + 11]));
238 if (bp[i + 1] > IP6OPT_BA_MINLEN - 2) {
239 ip6_sopt_print(&bp[i + IP6OPT_BA_MINLEN],
240 (optlen-IP6OPT_BA_MINLEN));
243 optlen = bp[i + 1] + 2;
245 case IP6OPT_BINDING_REQ:
246 if (len - i < IP6OPT_BR_MINLEN) {
247 printf("(br: trunc)");
251 if (bp[i + 1] > IP6OPT_BR_MINLEN - 2) {
252 ip6_sopt_print(&bp[i + IP6OPT_BR_MINLEN],
253 (optlen-IP6OPT_BR_MINLEN));
256 optlen = bp[i + 1] + 2;
259 if (len - i < IP6OPT_MINLEN) {
260 printf("(type %d: trunc)", bp[i]);
263 printf("(opt_type 0x%02x: len=%d) ", bp[i], bp[i + 1]);
264 optlen = bp[i + 1] + 2;
279 hbhopt_print(register const u_char *bp)
281 const struct ip6_hbh *dp = (struct ip6_hbh *)bp;
282 register const u_char *ep;
285 /* 'ep' points to the end of available data. */
287 TCHECK(dp->ip6h_len);
288 hbhlen = (int)((dp->ip6h_len + 1) << 3);
289 TCHECK2(*dp, hbhlen);
292 ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
297 fputs("[|HBH]", stdout);
302 dstopt_print(register const u_char *bp)
304 const struct ip6_dest *dp = (struct ip6_dest *)bp;
305 register const u_char *ep;
308 /* 'ep' points to the end of available data. */
310 TCHECK(dp->ip6d_len);
311 dstoptlen = (int)((dp->ip6d_len + 1) << 3);
312 TCHECK2(*dp, dstoptlen);
315 ip6_opt_print((const u_char *)dp + sizeof(*dp),
316 dstoptlen - sizeof(*dp));
322 fputs("[|DSTOPT]", stdout);