]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6opts.c
s/u_short/u_int16_t/ for KAME-origin source codes
[tcpdump] / print-ip6opts.c
1 /*
2 * Copyright (C) 1998 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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifndef lint
35 static const char rcsid[] =
36 "@(#) $Header: /tcpdump/master/tcpdump/print-ip6opts.c,v 1.5 2000-04-28 11:14:48 itojun Exp $";
37 #endif
38
39 #ifdef INET6
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44
45 #include <netinet/in.h>
46 #include <netinet/ip6.h>
47
48 #include <stdio.h>
49
50 #include "interface.h"
51 #include "addrtoname.h"
52
53 void
54 ip6_opt_print(const u_char *bp, int len)
55 {
56 int i;
57 int optlen;
58
59 for (i = 0; i < len; i += optlen) {
60 switch (bp[i]) {
61 #ifndef IP6OPT_PAD1
62 #define IP6OPT_PAD1 0x00
63 #endif
64 case IP6OPT_PAD1:
65 optlen = 1;
66 break;
67 #ifndef IP6OPT_PADN
68 #define IP6OPT_PADN 0x01
69 #endif
70 case IP6OPT_PADN:
71 #ifndef IP6OPT_MINLEN
72 #define IP6OPT_MINLEN 2
73 #endif
74 if (len - i < IP6OPT_MINLEN) {
75 printf("(padn: trunc)");
76 goto trunc;
77 }
78 optlen = bp[i + 1] + 2;
79 break;
80 #ifndef IP6OPT_RTALERT
81 #define IP6OPT_RTALERT 0x05
82 #endif
83 case IP6OPT_RTALERT:
84 #ifndef IP6OPT_RTALERT_LEN
85 #define IP6OPT_RTALERT_LEN 4
86 #endif
87 if (len - i < IP6OPT_RTALERT_LEN) {
88 printf("(rtalert: trunc)");
89 goto trunc;
90 }
91 if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
92 printf("(rtalert: invalid len %d)", bp[i + 1]);
93 goto trunc;
94 }
95 printf("(rtalert: 0x%04x) ", ntohs(*(u_int16_t *)&bp[i + 2]));
96 optlen = IP6OPT_RTALERT_LEN;
97 break;
98 #ifndef IP6OPT_JUMBO
99 #define IP6OPT_JUMBO 0xC2
100 #endif
101 case IP6OPT_JUMBO:
102 #ifndef IP6OPT_JUMBO_LEN
103 #define IP6OPT_JUMBO_LEN 6
104 #endif
105 if (len - i < IP6OPT_JUMBO_LEN) {
106 printf("(jumbo: trunc)");
107 goto trunc;
108 }
109 if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
110 printf("(jumbo: invalid len %d)", bp[i + 1]);
111 goto trunc;
112 }
113 printf("(jumbo: %u) ", (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 2]));
114 optlen = IP6OPT_JUMBO_LEN;
115 break;
116 default:
117 if (len - i < IP6OPT_MINLEN) {
118 printf("(type %d: trunc)", bp[i]);
119 goto trunc;
120 }
121 printf("(type 0x%02x: len=%d) ", bp[i], bp[i + 1]);
122 optlen = bp[i + 1] + 2;
123 break;
124 }
125 }
126
127 #if 0
128 end:
129 #endif
130 return;
131
132 trunc:
133 printf("[trunc] ");
134 }
135
136 int
137 hbhopt_print(register const u_char *bp)
138 {
139 const struct ip6_hbh *dp = (struct ip6_hbh *)bp;
140 register const u_char *ep;
141 int hbhlen = 0;
142
143 /* 'ep' points to the end of avaible data. */
144 ep = snapend;
145 TCHECK(dp->ip6h_len);
146 hbhlen = (int)((dp->ip6h_len + 1) << 3);
147 TCHECK2(dp, hbhlen);
148 printf("HBH ");
149 if (vflag)
150 ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
151
152 return(hbhlen);
153
154 trunc:
155 fputs("[|HBH]", stdout);
156 return(hbhlen);
157 }
158
159 int
160 dstopt_print(register const u_char *bp)
161 {
162 const struct ip6_dest *dp = (struct ip6_dest *)bp;
163 register const u_char *ep;
164 int dstoptlen = 0;
165
166 /* 'ep' points to the end of avaible data. */
167 ep = snapend;
168 TCHECK(dp->ip6d_len);
169 dstoptlen = (int)((dp->ip6d_len + 1) << 3);
170 TCHECK2(dp, dstoptlen);
171 printf("DSTOPT ");
172 if (vflag) {
173 ip6_opt_print((const u_char *)dp + sizeof(*dp),
174 dstoptlen - sizeof(*dp));
175 }
176
177 return(dstoptlen);
178
179 trunc:
180 fputs("[|DSTOPT]", stdout);
181 return(dstoptlen);
182 }
183 #endif /* INET6 */