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