Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-ip6opts.c
blob1d2508cbacb1b3e4a13f68b8d0d0e14525ee0717
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 1998 WIDE Project.
5 * All rights reserved.
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.
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.
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static const char rcsid[] _U_ =
40 "@(#) Header: /tcpdump/master/tcpdump/print-ip6opts.c,v 1.17.2.1 2005/04/20 22:19:06 guy Exp";
41 #else
42 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
43 #endif
44 #endif
46 #ifdef INET6
47 #include <tcpdump-stdinc.h>
49 #include <stdio.h>
51 #include "ip6.h"
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "extract.h"
57 /* items outside of rfc2292bis */
58 #ifndef IP6OPT_MINLEN
59 #define IP6OPT_MINLEN 2
60 #endif
61 #ifndef IP6OPT_RTALERT_LEN
62 #define IP6OPT_RTALERT_LEN 4
63 #endif
64 #ifndef IP6OPT_JUMBO_LEN
65 #define IP6OPT_JUMBO_LEN 6
66 #endif
67 #define IP6OPT_HOMEADDR_MINLEN 18
68 #define IP6OPT_BU_MINLEN 10
69 #define IP6OPT_BA_MINLEN 13
70 #define IP6OPT_BR_MINLEN 2
71 #define IP6SOPT_UI 0x2
72 #define IP6SOPT_UI_MINLEN 4
73 #define IP6SOPT_ALTCOA 0x3
74 #define IP6SOPT_ALTCOA_MINLEN 18
75 #define IP6SOPT_AUTH 0x4
76 #define IP6SOPT_AUTH_MINLEN 6
78 static void ip6_sopt_print(const u_char *, int);
80 static void
81 ip6_sopt_print(const u_char *bp, int len)
83 int i;
84 int optlen;
86 for (i = 0; i < len; i += optlen) {
87 if (bp[i] == IP6OPT_PAD1)
88 optlen = 1;
89 else {
90 if (i + 1 < len)
91 optlen = bp[i + 1] + 2;
92 else
93 goto trunc;
95 if (i + optlen > len)
96 goto trunc;
98 switch (bp[i]) {
99 case IP6OPT_PAD1:
100 printf(", pad1");
101 break;
102 case IP6OPT_PADN:
103 if (len - i < IP6OPT_MINLEN) {
104 printf(", padn: trunc");
105 goto trunc;
107 printf(", padn");
108 break;
109 case IP6SOPT_UI:
110 if (len - i < IP6SOPT_UI_MINLEN) {
111 printf(", ui: trunc");
112 goto trunc;
114 printf(", ui: 0x%04x ", EXTRACT_16BITS(&bp[i + 2]));
115 break;
116 case IP6SOPT_ALTCOA:
117 if (len - i < IP6SOPT_ALTCOA_MINLEN) {
118 printf(", altcoa: trunc");
119 goto trunc;
121 printf(", alt-CoA: %s", ip6addr_string(&bp[i+2]));
122 break;
123 case IP6SOPT_AUTH:
124 if (len - i < IP6SOPT_AUTH_MINLEN) {
125 printf(", auth: trunc");
126 goto trunc;
128 printf(", auth spi: 0x%08x", EXTRACT_32BITS(&bp[i + 2]));
129 break;
130 default:
131 if (len - i < IP6OPT_MINLEN) {
132 printf(", sopt_type %d: trunc)", bp[i]);
133 goto trunc;
135 printf(", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1]);
136 break;
139 return;
141 trunc:
142 printf("[trunc] ");
145 void
146 ip6_opt_print(const u_char *bp, int len)
148 int i;
149 int optlen = 0;
151 for (i = 0; i < len; i += optlen) {
152 if (bp[i] == IP6OPT_PAD1)
153 optlen = 1;
154 else {
155 if (i + 1 < len)
156 optlen = bp[i + 1] + 2;
157 else
158 goto trunc;
160 if (i + optlen > len)
161 goto trunc;
163 switch (bp[i]) {
164 case IP6OPT_PAD1:
165 printf("(pad1)");
166 break;
167 case IP6OPT_PADN:
168 if (len - i < IP6OPT_MINLEN) {
169 printf("(padn: trunc)");
170 goto trunc;
172 printf("(padn)");
173 break;
174 case IP6OPT_ROUTER_ALERT:
175 if (len - i < IP6OPT_RTALERT_LEN) {
176 printf("(rtalert: trunc)");
177 goto trunc;
179 if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
180 printf("(rtalert: invalid len %d)", bp[i + 1]);
181 goto trunc;
183 printf("(rtalert: 0x%04x) ", EXTRACT_16BITS(&bp[i + 2]));
184 break;
185 case IP6OPT_JUMBO:
186 if (len - i < IP6OPT_JUMBO_LEN) {
187 printf("(jumbo: trunc)");
188 goto trunc;
190 if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
191 printf("(jumbo: invalid len %d)", bp[i + 1]);
192 goto trunc;
194 printf("(jumbo: %u) ", EXTRACT_32BITS(&bp[i + 2]));
195 break;
196 case IP6OPT_HOME_ADDRESS:
197 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
198 printf("(homeaddr: trunc)");
199 goto trunc;
201 if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) {
202 printf("(homeaddr: invalid len %d)", bp[i + 1]);
203 goto trunc;
205 printf("(homeaddr: %s", ip6addr_string(&bp[i + 2]));
206 if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) {
207 ip6_sopt_print(&bp[i + IP6OPT_HOMEADDR_MINLEN],
208 (optlen - IP6OPT_HOMEADDR_MINLEN));
210 printf(")");
211 break;
212 case IP6OPT_BINDING_UPDATE:
213 if (len - i < IP6OPT_BU_MINLEN) {
214 printf("(bu: trunc)");
215 goto trunc;
217 if (bp[i + 1] < IP6OPT_BU_MINLEN - 2) {
218 printf("(bu: invalid len %d)", bp[i + 1]);
219 goto trunc;
221 printf("(bu: ");
222 if (bp[i + 2] & 0x80)
223 printf("A");
224 if (bp[i + 2] & 0x40)
225 printf("H");
226 if (bp[i + 2] & 0x20)
227 printf("S");
228 if (bp[i + 2] & 0x10)
229 printf("D");
230 if ((bp[i + 2] & 0x0f) || bp[i + 3] || bp[i + 4])
231 printf("res");
232 printf(", sequence: %u", bp[i + 5]);
233 printf(", lifetime: %u", EXTRACT_32BITS(&bp[i + 6]));
235 if (bp[i + 1] > IP6OPT_BU_MINLEN - 2) {
236 ip6_sopt_print(&bp[i + IP6OPT_BU_MINLEN],
237 (optlen - IP6OPT_BU_MINLEN));
239 printf(")");
240 break;
241 case IP6OPT_BINDING_ACK:
242 if (len - i < IP6OPT_BA_MINLEN) {
243 printf("(ba: trunc)");
244 goto trunc;
246 if (bp[i + 1] < IP6OPT_BA_MINLEN - 2) {
247 printf("(ba: invalid len %d)", bp[i + 1]);
248 goto trunc;
250 printf("(ba: ");
251 printf("status: %u", bp[i + 2]);
252 if (bp[i + 3])
253 printf("res");
254 printf(", sequence: %u", bp[i + 4]);
255 printf(", lifetime: %u", EXTRACT_32BITS(&bp[i + 5]));
256 printf(", refresh: %u", EXTRACT_32BITS(&bp[i + 9]));
258 if (bp[i + 1] > IP6OPT_BA_MINLEN - 2) {
259 ip6_sopt_print(&bp[i + IP6OPT_BA_MINLEN],
260 (optlen - IP6OPT_BA_MINLEN));
262 printf(")");
263 break;
264 case IP6OPT_BINDING_REQ:
265 if (len - i < IP6OPT_BR_MINLEN) {
266 printf("(br: trunc)");
267 goto trunc;
269 printf("(br");
270 if (bp[i + 1] > IP6OPT_BR_MINLEN - 2) {
271 ip6_sopt_print(&bp[i + IP6OPT_BR_MINLEN],
272 (optlen - IP6OPT_BR_MINLEN));
274 printf(")");
275 break;
276 default:
277 if (len - i < IP6OPT_MINLEN) {
278 printf("(type %d: trunc)", bp[i]);
279 goto trunc;
281 printf("(opt_type 0x%02x: len=%d) ", bp[i], bp[i + 1]);
282 break;
286 #if 0
287 end:
288 #endif
289 return;
291 trunc:
292 printf("[trunc] ");
296 hbhopt_print(register const u_char *bp)
298 const struct ip6_hbh *dp = (struct ip6_hbh *)bp;
299 int hbhlen = 0;
301 TCHECK(dp->ip6h_len);
302 hbhlen = (int)((dp->ip6h_len + 1) << 3);
303 TCHECK2(*dp, hbhlen);
304 printf("HBH ");
305 if (vflag)
306 ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
308 return(hbhlen);
310 trunc:
311 fputs("[|HBH]", stdout);
312 return(-1);
316 dstopt_print(register const u_char *bp)
318 const struct ip6_dest *dp = (struct ip6_dest *)bp;
319 int dstoptlen = 0;
321 TCHECK(dp->ip6d_len);
322 dstoptlen = (int)((dp->ip6d_len + 1) << 3);
323 TCHECK2(*dp, dstoptlen);
324 printf("DSTOPT ");
325 if (vflag) {
326 ip6_opt_print((const u_char *)dp + sizeof(*dp),
327 dstoptlen - sizeof(*dp));
330 return(dstoptlen);
332 trunc:
333 fputs("[|DSTOPT]", stdout);
334 return(-1);
336 #endif /* INET6 */