1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 1998 WIDE Project. |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 3. Neither the name of the project nor the names of its contributors |
---|
16 | * may be used to endorse or promote products derived from this software |
---|
17 | * without specific prior written permission. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | #ifdef HAVE_CONFIG_H |
---|
33 | #include "config.h" |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifndef lint |
---|
37 | static const char rcsid[] _U_ = |
---|
38 | "@(#) $Header: /tcpdump/master/tcpdump/print-ip6opts.c,v 1.18 2005-04-20 22:18:50 guy Exp $"; |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifdef INET6 |
---|
42 | #include <tcpdump-stdinc.h> |
---|
43 | |
---|
44 | #include <stdio.h> |
---|
45 | |
---|
46 | #include "ip6.h" |
---|
47 | |
---|
48 | #include "interface.h" |
---|
49 | #include "addrtoname.h" |
---|
50 | #include "extract.h" |
---|
51 | |
---|
52 | /* items outside of rfc2292bis */ |
---|
53 | #ifndef IP6OPT_MINLEN |
---|
54 | #define IP6OPT_MINLEN 2 |
---|
55 | #endif |
---|
56 | #ifndef IP6OPT_RTALERT_LEN |
---|
57 | #define IP6OPT_RTALERT_LEN 4 |
---|
58 | #endif |
---|
59 | #ifndef IP6OPT_JUMBO_LEN |
---|
60 | #define IP6OPT_JUMBO_LEN 6 |
---|
61 | #endif |
---|
62 | #define IP6OPT_HOMEADDR_MINLEN 18 |
---|
63 | #define IP6OPT_BU_MINLEN 10 |
---|
64 | #define IP6OPT_BA_MINLEN 13 |
---|
65 | #define IP6OPT_BR_MINLEN 2 |
---|
66 | #define IP6SOPT_UI 0x2 |
---|
67 | #define IP6SOPT_UI_MINLEN 4 |
---|
68 | #define IP6SOPT_ALTCOA 0x3 |
---|
69 | #define IP6SOPT_ALTCOA_MINLEN 18 |
---|
70 | #define IP6SOPT_AUTH 0x4 |
---|
71 | #define IP6SOPT_AUTH_MINLEN 6 |
---|
72 | |
---|
73 | static void ip6_sopt_print(const u_char *, int); |
---|
74 | |
---|
75 | static void |
---|
76 | ip6_sopt_print(const u_char *bp, int len) |
---|
77 | { |
---|
78 | int i; |
---|
79 | int optlen; |
---|
80 | |
---|
81 | for (i = 0; i < len; i += optlen) { |
---|
82 | if (bp[i] == IP6OPT_PAD1) |
---|
83 | optlen = 1; |
---|
84 | else { |
---|
85 | if (i + 1 < len) |
---|
86 | optlen = bp[i + 1] + 2; |
---|
87 | else |
---|
88 | goto trunc; |
---|
89 | } |
---|
90 | if (i + optlen > len) |
---|
91 | goto trunc; |
---|
92 | |
---|
93 | switch (bp[i]) { |
---|
94 | case IP6OPT_PAD1: |
---|
95 | printf(", pad1"); |
---|
96 | break; |
---|
97 | case IP6OPT_PADN: |
---|
98 | if (len - i < IP6OPT_MINLEN) { |
---|
99 | printf(", padn: trunc"); |
---|
100 | goto trunc; |
---|
101 | } |
---|
102 | printf(", padn"); |
---|
103 | break; |
---|
104 | case IP6SOPT_UI: |
---|
105 | if (len - i < IP6SOPT_UI_MINLEN) { |
---|
106 | printf(", ui: trunc"); |
---|
107 | goto trunc; |
---|
108 | } |
---|
109 | printf(", ui: 0x%04x ", EXTRACT_16BITS(&bp[i + 2])); |
---|
110 | break; |
---|
111 | case IP6SOPT_ALTCOA: |
---|
112 | if (len - i < IP6SOPT_ALTCOA_MINLEN) { |
---|
113 | printf(", altcoa: trunc"); |
---|
114 | goto trunc; |
---|
115 | } |
---|
116 | printf(", alt-CoA: %s", ip6addr_string(&bp[i+2])); |
---|
117 | break; |
---|
118 | case IP6SOPT_AUTH: |
---|
119 | if (len - i < IP6SOPT_AUTH_MINLEN) { |
---|
120 | printf(", auth: trunc"); |
---|
121 | goto trunc; |
---|
122 | } |
---|
123 | printf(", auth spi: 0x%08x", EXTRACT_32BITS(&bp[i + 2])); |
---|
124 | break; |
---|
125 | default: |
---|
126 | if (len - i < IP6OPT_MINLEN) { |
---|
127 | printf(", sopt_type %d: trunc)", bp[i]); |
---|
128 | goto trunc; |
---|
129 | } |
---|
130 | printf(", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1]); |
---|
131 | break; |
---|
132 | } |
---|
133 | } |
---|
134 | return; |
---|
135 | |
---|
136 | trunc: |
---|
137 | printf("[trunc] "); |
---|
138 | } |
---|
139 | |
---|
140 | void |
---|
141 | ip6_opt_print(const u_char *bp, int len) |
---|
142 | { |
---|
143 | int i; |
---|
144 | int optlen = 0; |
---|
145 | |
---|
146 | if (len == 0) |
---|
147 | return; |
---|
148 | for (i = 0; i < len; i += optlen) { |
---|
149 | if (bp[i] == IP6OPT_PAD1) |
---|
150 | optlen = 1; |
---|
151 | else { |
---|
152 | if (i + 1 < len) |
---|
153 | optlen = bp[i + 1] + 2; |
---|
154 | else |
---|
155 | goto trunc; |
---|
156 | } |
---|
157 | if (i + optlen > len) |
---|
158 | goto trunc; |
---|
159 | |
---|
160 | switch (bp[i]) { |
---|
161 | case IP6OPT_PAD1: |
---|
162 | printf("(pad1)"); |
---|
163 | break; |
---|
164 | case IP6OPT_PADN: |
---|
165 | if (len - i < IP6OPT_MINLEN) { |
---|
166 | printf("(padn: trunc)"); |
---|
167 | goto trunc; |
---|
168 | } |
---|
169 | printf("(padn)"); |
---|
170 | break; |
---|
171 | case IP6OPT_ROUTER_ALERT: |
---|
172 | if (len - i < IP6OPT_RTALERT_LEN) { |
---|
173 | printf("(rtalert: trunc)"); |
---|
174 | goto trunc; |
---|
175 | } |
---|
176 | if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) { |
---|
177 | printf("(rtalert: invalid len %d)", bp[i + 1]); |
---|
178 | goto trunc; |
---|
179 | } |
---|
180 | printf("(rtalert: 0x%04x) ", EXTRACT_16BITS(&bp[i + 2])); |
---|
181 | break; |
---|
182 | case IP6OPT_JUMBO: |
---|
183 | if (len - i < IP6OPT_JUMBO_LEN) { |
---|
184 | printf("(jumbo: trunc)"); |
---|
185 | goto trunc; |
---|
186 | } |
---|
187 | if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) { |
---|
188 | printf("(jumbo: invalid len %d)", bp[i + 1]); |
---|
189 | goto trunc; |
---|
190 | } |
---|
191 | printf("(jumbo: %u) ", EXTRACT_32BITS(&bp[i + 2])); |
---|
192 | break; |
---|
193 | case IP6OPT_HOME_ADDRESS: |
---|
194 | if (len - i < IP6OPT_HOMEADDR_MINLEN) { |
---|
195 | printf("(homeaddr: trunc)"); |
---|
196 | goto trunc; |
---|
197 | } |
---|
198 | if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) { |
---|
199 | printf("(homeaddr: invalid len %d)", bp[i + 1]); |
---|
200 | goto trunc; |
---|
201 | } |
---|
202 | printf("(homeaddr: %s", ip6addr_string(&bp[i + 2])); |
---|
203 | if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) { |
---|
204 | ip6_sopt_print(&bp[i + IP6OPT_HOMEADDR_MINLEN], |
---|
205 | (optlen - IP6OPT_HOMEADDR_MINLEN)); |
---|
206 | } |
---|
207 | printf(")"); |
---|
208 | break; |
---|
209 | case IP6OPT_BINDING_UPDATE: |
---|
210 | if (len - i < IP6OPT_BU_MINLEN) { |
---|
211 | printf("(bu: trunc)"); |
---|
212 | goto trunc; |
---|
213 | } |
---|
214 | if (bp[i + 1] < IP6OPT_BU_MINLEN - 2) { |
---|
215 | printf("(bu: invalid len %d)", bp[i + 1]); |
---|
216 | goto trunc; |
---|
217 | } |
---|
218 | printf("(bu: "); |
---|
219 | if (bp[i + 2] & 0x80) |
---|
220 | printf("A"); |
---|
221 | if (bp[i + 2] & 0x40) |
---|
222 | printf("H"); |
---|
223 | if (bp[i + 2] & 0x20) |
---|
224 | printf("S"); |
---|
225 | if (bp[i + 2] & 0x10) |
---|
226 | printf("D"); |
---|
227 | if ((bp[i + 2] & 0x0f) || bp[i + 3] || bp[i + 4]) |
---|
228 | printf("res"); |
---|
229 | printf(", sequence: %u", bp[i + 5]); |
---|
230 | printf(", lifetime: %u", EXTRACT_32BITS(&bp[i + 6])); |
---|
231 | |
---|
232 | if (bp[i + 1] > IP6OPT_BU_MINLEN - 2) { |
---|
233 | ip6_sopt_print(&bp[i + IP6OPT_BU_MINLEN], |
---|
234 | (optlen - IP6OPT_BU_MINLEN)); |
---|
235 | } |
---|
236 | printf(")"); |
---|
237 | break; |
---|
238 | case IP6OPT_BINDING_ACK: |
---|
239 | if (len - i < IP6OPT_BA_MINLEN) { |
---|
240 | printf("(ba: trunc)"); |
---|
241 | goto trunc; |
---|
242 | } |
---|
243 | if (bp[i + 1] < IP6OPT_BA_MINLEN - 2) { |
---|
244 | printf("(ba: invalid len %d)", bp[i + 1]); |
---|
245 | goto trunc; |
---|
246 | } |
---|
247 | printf("(ba: "); |
---|
248 | printf("status: %u", bp[i + 2]); |
---|
249 | if (bp[i + 3]) |
---|
250 | printf("res"); |
---|
251 | printf(", sequence: %u", bp[i + 4]); |
---|
252 | printf(", lifetime: %u", EXTRACT_32BITS(&bp[i + 5])); |
---|
253 | printf(", refresh: %u", EXTRACT_32BITS(&bp[i + 9])); |
---|
254 | |
---|
255 | if (bp[i + 1] > IP6OPT_BA_MINLEN - 2) { |
---|
256 | ip6_sopt_print(&bp[i + IP6OPT_BA_MINLEN], |
---|
257 | (optlen - IP6OPT_BA_MINLEN)); |
---|
258 | } |
---|
259 | printf(")"); |
---|
260 | break; |
---|
261 | case IP6OPT_BINDING_REQ: |
---|
262 | if (len - i < IP6OPT_BR_MINLEN) { |
---|
263 | printf("(br: trunc)"); |
---|
264 | goto trunc; |
---|
265 | } |
---|
266 | printf("(br"); |
---|
267 | if (bp[i + 1] > IP6OPT_BR_MINLEN - 2) { |
---|
268 | ip6_sopt_print(&bp[i + IP6OPT_BR_MINLEN], |
---|
269 | (optlen - IP6OPT_BR_MINLEN)); |
---|
270 | } |
---|
271 | printf(")"); |
---|
272 | break; |
---|
273 | default: |
---|
274 | if (len - i < IP6OPT_MINLEN) { |
---|
275 | printf("(type %d: trunc)", bp[i]); |
---|
276 | goto trunc; |
---|
277 | } |
---|
278 | printf("(opt_type 0x%02x: len=%d)", bp[i], bp[i + 1]); |
---|
279 | break; |
---|
280 | } |
---|
281 | } |
---|
282 | printf(" "); |
---|
283 | |
---|
284 | #if 0 |
---|
285 | end: |
---|
286 | #endif |
---|
287 | return; |
---|
288 | |
---|
289 | trunc: |
---|
290 | printf("[trunc] "); |
---|
291 | } |
---|
292 | |
---|
293 | int |
---|
294 | hbhopt_print(register const u_char *bp) |
---|
295 | { |
---|
296 | const struct ip6_hbh *dp = (struct ip6_hbh *)bp; |
---|
297 | int hbhlen = 0; |
---|
298 | |
---|
299 | TCHECK(dp->ip6h_len); |
---|
300 | hbhlen = (int)((dp->ip6h_len + 1) << 3); |
---|
301 | TCHECK2(*dp, hbhlen); |
---|
302 | printf("HBH "); |
---|
303 | if (vflag) |
---|
304 | ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp)); |
---|
305 | |
---|
306 | return(hbhlen); |
---|
307 | |
---|
308 | trunc: |
---|
309 | fputs("[|HBH]", stdout); |
---|
310 | return(-1); |
---|
311 | } |
---|
312 | |
---|
313 | int |
---|
314 | dstopt_print(register const u_char *bp) |
---|
315 | { |
---|
316 | const struct ip6_dest *dp = (struct ip6_dest *)bp; |
---|
317 | int dstoptlen = 0; |
---|
318 | |
---|
319 | TCHECK(dp->ip6d_len); |
---|
320 | dstoptlen = (int)((dp->ip6d_len + 1) << 3); |
---|
321 | TCHECK2(*dp, dstoptlen); |
---|
322 | printf("DSTOPT "); |
---|
323 | if (vflag) { |
---|
324 | ip6_opt_print((const u_char *)dp + sizeof(*dp), |
---|
325 | dstoptlen - sizeof(*dp)); |
---|
326 | } |
---|
327 | |
---|
328 | return(dstoptlen); |
---|
329 | |
---|
330 | trunc: |
---|
331 | fputs("[|DSTOPT]", stdout); |
---|
332 | return(-1); |
---|
333 | } |
---|
334 | #endif /* INET6 */ |
---|