]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6opts.c
Use EXTRACT_nBITS even when just testing against zero.
[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 #ifdef INET6
35 #include <tcpdump-stdinc.h>
36
37 #include <stdio.h>
38
39 #include "ip6.h"
40
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h"
44
45 static void
46 ip6_sopt_print(const u_char *bp, int len)
47 {
48 int i;
49 int optlen;
50
51 for (i = 0; i < len; i += optlen) {
52 if (bp[i] == IP6OPT_PAD1)
53 optlen = 1;
54 else {
55 if (i + 1 < len)
56 optlen = bp[i + 1] + 2;
57 else
58 goto trunc;
59 }
60 if (i + optlen > len)
61 goto trunc;
62
63 switch (bp[i]) {
64 case IP6OPT_PAD1:
65 printf(", pad1");
66 break;
67 case IP6OPT_PADN:
68 if (len - i < IP6OPT_MINLEN) {
69 printf(", padn: trunc");
70 goto trunc;
71 }
72 printf(", padn");
73 break;
74 default:
75 if (len - i < IP6OPT_MINLEN) {
76 printf(", sopt_type %d: trunc)", bp[i]);
77 goto trunc;
78 }
79 printf(", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1]);
80 break;
81 }
82 }
83 return;
84
85 trunc:
86 printf("[trunc] ");
87 }
88
89 void
90 ip6_opt_print(const u_char *bp, int len)
91 {
92 int i;
93 int optlen = 0;
94
95 if (len == 0)
96 return;
97 for (i = 0; i < len; i += optlen) {
98 if (bp[i] == IP6OPT_PAD1)
99 optlen = 1;
100 else {
101 if (i + 1 < len)
102 optlen = bp[i + 1] + 2;
103 else
104 goto trunc;
105 }
106 if (i + optlen > len)
107 goto trunc;
108
109 switch (bp[i]) {
110 case IP6OPT_PAD1:
111 printf("(pad1)");
112 break;
113 case IP6OPT_PADN:
114 if (len - i < IP6OPT_MINLEN) {
115 printf("(padn: trunc)");
116 goto trunc;
117 }
118 printf("(padn)");
119 break;
120 case IP6OPT_ROUTER_ALERT:
121 if (len - i < IP6OPT_RTALERT_LEN) {
122 printf("(rtalert: trunc)");
123 goto trunc;
124 }
125 if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
126 printf("(rtalert: invalid len %d)", bp[i + 1]);
127 goto trunc;
128 }
129 printf("(rtalert: 0x%04x) ", EXTRACT_16BITS(&bp[i + 2]));
130 break;
131 case IP6OPT_JUMBO:
132 if (len - i < IP6OPT_JUMBO_LEN) {
133 printf("(jumbo: trunc)");
134 goto trunc;
135 }
136 if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
137 printf("(jumbo: invalid len %d)", bp[i + 1]);
138 goto trunc;
139 }
140 printf("(jumbo: %u) ", EXTRACT_32BITS(&bp[i + 2]));
141 break;
142 case IP6OPT_HOME_ADDRESS:
143 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
144 printf("(homeaddr: trunc)");
145 goto trunc;
146 }
147 if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) {
148 printf("(homeaddr: invalid len %d)", bp[i + 1]);
149 goto trunc;
150 }
151 printf("(homeaddr: %s", ip6addr_string(&bp[i + 2]));
152 if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) {
153 ip6_sopt_print(&bp[i + IP6OPT_HOMEADDR_MINLEN],
154 (optlen - IP6OPT_HOMEADDR_MINLEN));
155 }
156 printf(")");
157 break;
158 default:
159 if (len - i < IP6OPT_MINLEN) {
160 printf("(type %d: trunc)", bp[i]);
161 goto trunc;
162 }
163 printf("(opt_type 0x%02x: len=%d)", bp[i], bp[i + 1]);
164 break;
165 }
166 }
167 printf(" ");
168 return;
169
170 trunc:
171 printf("[trunc] ");
172 }
173
174 int
175 hbhopt_print(register const u_char *bp)
176 {
177 const struct ip6_hbh *dp = (struct ip6_hbh *)bp;
178 int hbhlen = 0;
179
180 TCHECK(dp->ip6h_len);
181 hbhlen = (int)((dp->ip6h_len + 1) << 3);
182 TCHECK2(*dp, hbhlen);
183 printf("HBH ");
184 if (vflag)
185 ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
186
187 return(hbhlen);
188
189 trunc:
190 fputs("[|HBH]", stdout);
191 return(-1);
192 }
193
194 int
195 dstopt_print(register const u_char *bp)
196 {
197 const struct ip6_dest *dp = (struct ip6_dest *)bp;
198 int dstoptlen = 0;
199
200 TCHECK(dp->ip6d_len);
201 dstoptlen = (int)((dp->ip6d_len + 1) << 3);
202 TCHECK2(*dp, dstoptlen);
203 printf("DSTOPT ");
204 if (vflag) {
205 ip6_opt_print((const u_char *)dp + sizeof(*dp),
206 dstoptlen - sizeof(*dp));
207 }
208
209 return(dstoptlen);
210
211 trunc:
212 fputs("[|DSTOPT]", stdout);
213 return(-1);
214 }
215 #endif /* INET6 */