]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6opts.c
remove blank lines at EOF
[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.12 2002-08-01 08:53:11 risso Exp $";
37 #endif
38
39 #ifdef INET6
40 #include <tcpdump-stdinc.h>
41
42 #include <stdio.h>
43
44 #include "ip6.h"
45
46 #include "interface.h"
47 #include "addrtoname.h"
48
49 /* items outside of rfc2292bis */
50 #ifndef IP6OPT_MINLEN
51 #define IP6OPT_MINLEN 2
52 #endif
53 #ifndef IP6OPT_RTALERT_LEN
54 #define IP6OPT_RTALERT_LEN 4
55 #endif
56 #ifndef IP6OPT_JUMBO_LEN
57 #define IP6OPT_JUMBO_LEN 6
58 #endif
59 #define IP6OPT_HOMEADDR_MINLEN 18
60 #define IP6OPT_BU_MINLEN 10
61 #define IP6OPT_BA_MINLEN 13
62 #define IP6OPT_BR_MINLEN 2
63 #define IP6SOPT_UI 0x2
64 #define IP6SOPT_UI_MINLEN 4
65 #define IP6SOPT_ALTCOA 0x3
66 #define IP6SOPT_ALTCOA_MINLEN 18
67 #define IP6SOPT_AUTH 0x4
68 #define IP6SOPT_AUTH_MINLEN 6
69
70 static void ip6_sopt_print(const u_char *, int);
71
72 static void
73 ip6_sopt_print(const u_char *bp, int len)
74 {
75 int i;
76 int optlen;
77
78 for (i = 0; i < len; i += optlen) {
79 if (bp[i] == IP6OPT_PAD1)
80 optlen = 1;
81 else {
82 if (i + 1 < len)
83 optlen = bp[i + 1] + 2;
84 else
85 goto trunc;
86 }
87 if (i + optlen > len)
88 goto trunc;
89
90 switch (bp[i]) {
91 case IP6OPT_PAD1:
92 printf(", pad1");
93 break;
94 case IP6OPT_PADN:
95 if (len - i < IP6OPT_MINLEN) {
96 printf(", padn: trunc");
97 goto trunc;
98 }
99 printf(", padn");
100 break;
101 case IP6SOPT_UI:
102 if (len - i < IP6SOPT_UI_MINLEN) {
103 printf(", ui: trunc");
104 goto trunc;
105 }
106 printf(", ui: 0x%04x ", ntohs(*(u_int16_t *)&bp[i + 2]));
107 break;
108 case IP6SOPT_ALTCOA:
109 if (len - i < IP6SOPT_ALTCOA_MINLEN) {
110 printf(", altcoa: trunc");
111 goto trunc;
112 }
113 printf(", alt-CoA: %s", ip6addr_string(&bp[i+2]));
114 break;
115 case IP6SOPT_AUTH:
116 if (len - i < IP6SOPT_AUTH_MINLEN) {
117 printf(", auth: trunc");
118 goto trunc;
119 }
120 printf(", auth spi: 0x%08x",
121 (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 2]));
122 break;
123 default:
124 if (len - i < IP6OPT_MINLEN) {
125 printf(", sopt_type %d: trunc)", bp[i]);
126 goto trunc;
127 }
128 printf(", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1]);
129 break;
130 }
131 }
132 return;
133
134 trunc:
135 printf("[trunc] ");
136 }
137
138 void
139 ip6_opt_print(const u_char *bp, int len)
140 {
141 int i;
142 int optlen;
143
144 for (i = 0; i < len; i += optlen) {
145 if (bp[i] == IP6OPT_PAD1)
146 optlen = 1;
147 else {
148 if (i + 1 < len)
149 optlen = bp[i + 1] + 2;
150 else
151 goto trunc;
152 }
153 if (i + optlen > len)
154 goto trunc;
155
156 switch (bp[i]) {
157 case IP6OPT_PAD1:
158 printf("(pad1)");
159 break;
160 case IP6OPT_PADN:
161 if (len - i < IP6OPT_MINLEN) {
162 printf("(padn: trunc)");
163 goto trunc;
164 }
165 printf("(padn)");
166 break;
167 case IP6OPT_ROUTER_ALERT:
168 if (len - i < IP6OPT_RTALERT_LEN) {
169 printf("(rtalert: trunc)");
170 goto trunc;
171 }
172 if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
173 printf("(rtalert: invalid len %d)", bp[i + 1]);
174 goto trunc;
175 }
176 printf("(rtalert: 0x%04x) ", ntohs(*(u_int16_t *)&bp[i + 2]));
177 break;
178 case IP6OPT_JUMBO:
179 if (len - i < IP6OPT_JUMBO_LEN) {
180 printf("(jumbo: trunc)");
181 goto trunc;
182 }
183 if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
184 printf("(jumbo: invalid len %d)", bp[i + 1]);
185 goto trunc;
186 }
187 printf("(jumbo: %u) ", (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 2]));
188 break;
189 case IP6OPT_HOME_ADDRESS:
190 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
191 printf("(homeaddr: trunc)");
192 goto trunc;
193 }
194 if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) {
195 printf("(homeaddr: invalid len %d)", bp[i + 1]);
196 goto trunc;
197 }
198 printf("(homeaddr: %s", ip6addr_string(&bp[i + 2]));
199 if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) {
200 ip6_sopt_print(&bp[i + IP6OPT_HOMEADDR_MINLEN],
201 (optlen - IP6OPT_HOMEADDR_MINLEN));
202 }
203 printf(")");
204 break;
205 case IP6OPT_BINDING_UPDATE:
206 if (len - i < IP6OPT_BU_MINLEN) {
207 printf("(bu: trunc)");
208 goto trunc;
209 }
210 if (bp[i + 1] < IP6OPT_BU_MINLEN - 2) {
211 printf("(bu: invalid len %d)", bp[i + 1]);
212 goto trunc;
213 }
214 printf("(bu: ");
215 if (bp[i + 2] & 0x80)
216 printf("A");
217 if (bp[i + 2] & 0x40)
218 printf("H");
219 if (bp[i + 2] & 0x20)
220 printf("S");
221 if (bp[i + 2] & 0x10)
222 printf("D");
223 if ((bp[i + 2] & 0x0f) || bp[i + 3] || bp[i + 4])
224 printf("res");
225 printf(", sequence: %u", bp[i + 5]);
226 printf(", lifetime: %u",
227 (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 6]));
228
229 if (bp[i + 1] > IP6OPT_BU_MINLEN - 2) {
230 ip6_sopt_print(&bp[i + IP6OPT_BU_MINLEN],
231 (optlen - IP6OPT_BU_MINLEN));
232 }
233 printf(")");
234 break;
235 case IP6OPT_BINDING_ACK:
236 if (len - i < IP6OPT_BA_MINLEN) {
237 printf("(ba: trunc)");
238 goto trunc;
239 }
240 if (bp[i + 1] < IP6OPT_BA_MINLEN - 2) {
241 printf("(ba: invalid len %d)", bp[i + 1]);
242 goto trunc;
243 }
244 printf("(ba: ");
245 printf("status: %u", bp[i + 2]);
246 if (bp[i + 3])
247 printf("res");
248 printf(", sequence: %u", bp[i + 4]);
249 printf(", lifetime: %u",
250 (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 5]));
251 printf(", refresh: %u",
252 (u_int32_t)ntohl(*(u_int32_t *)&bp[i + 9]));
253
254 if (bp[i + 1] > IP6OPT_BA_MINLEN - 2) {
255 ip6_sopt_print(&bp[i + IP6OPT_BA_MINLEN],
256 (optlen - IP6OPT_BA_MINLEN));
257 }
258 printf(")");
259 break;
260 case IP6OPT_BINDING_REQ:
261 if (len - i < IP6OPT_BR_MINLEN) {
262 printf("(br: trunc)");
263 goto trunc;
264 }
265 printf("(br");
266 if (bp[i + 1] > IP6OPT_BR_MINLEN - 2) {
267 ip6_sopt_print(&bp[i + IP6OPT_BR_MINLEN],
268 (optlen - IP6OPT_BR_MINLEN));
269 }
270 printf(")");
271 break;
272 default:
273 if (len - i < IP6OPT_MINLEN) {
274 printf("(type %d: trunc)", bp[i]);
275 goto trunc;
276 }
277 printf("(opt_type 0x%02x: len=%d) ", bp[i], bp[i + 1]);
278 break;
279 }
280 }
281
282 #if 0
283 end:
284 #endif
285 return;
286
287 trunc:
288 printf("[trunc] ");
289 }
290
291 int
292 hbhopt_print(register const u_char *bp)
293 {
294 const struct ip6_hbh *dp = (struct ip6_hbh *)bp;
295 register const u_char *ep;
296 int hbhlen = 0;
297
298 /* 'ep' points to the end of available data. */
299 ep = snapend;
300 TCHECK(dp->ip6h_len);
301 hbhlen = (int)((dp->ip6h_len + 1) << 3);
302 TCHECK2(*dp, hbhlen);
303 printf("HBH ");
304 if (vflag)
305 ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
306
307 return(hbhlen);
308
309 trunc:
310 fputs("[|HBH]", stdout);
311 return(hbhlen);
312 }
313
314 int
315 dstopt_print(register const u_char *bp)
316 {
317 const struct ip6_dest *dp = (struct ip6_dest *)bp;
318 register const u_char *ep;
319 int dstoptlen = 0;
320
321 /* 'ep' points to the end of available data. */
322 ep = snapend;
323 TCHECK(dp->ip6d_len);
324 dstoptlen = (int)((dp->ip6d_len + 1) << 3);
325 TCHECK2(*dp, dstoptlen);
326 printf("DSTOPT ");
327 if (vflag) {
328 ip6_opt_print((const u_char *)dp + sizeof(*dp),
329 dstoptlen - sizeof(*dp));
330 }
331
332 return(dstoptlen);
333
334 trunc:
335 fputs("[|DSTOPT]", stdout);
336 return(dstoptlen);
337 }
338 #endif /* INET6 */