]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6opts.c
Add more checks.
[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 /* \summary: IPv6 header option printer */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "netdissect-stdinc.h"
37
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 #include "extract.h"
41
42 #include "ip6.h"
43
44 static int
45 ip6_sopt_print(netdissect_options *ndo, const u_char *bp, int len)
46 {
47 int i;
48 int optlen;
49
50 for (i = 0; i < len; i += optlen) {
51 if (GET_U_1(bp + i) == IP6OPT_PAD1)
52 optlen = 1;
53 else {
54 if (i + 1 < len)
55 optlen = GET_U_1(bp + i + 1) + 2;
56 else
57 goto trunc;
58 }
59 if (i + optlen > len)
60 goto trunc;
61
62 switch (GET_U_1(bp + i)) {
63 case IP6OPT_PAD1:
64 ND_PRINT(", pad1");
65 break;
66 case IP6OPT_PADN:
67 if (len - i < IP6OPT_MINLEN) {
68 ND_PRINT(", padn: trunc");
69 goto trunc;
70 }
71 ND_PRINT(", padn");
72 break;
73 default:
74 if (len - i < IP6OPT_MINLEN) {
75 ND_PRINT(", sopt_type %u: trunc)", GET_U_1(bp + i));
76 goto trunc;
77 }
78 ND_PRINT(", sopt_type 0x%02x: len=%u", GET_U_1(bp + i),
79 GET_U_1(bp + i + 1));
80 break;
81 }
82 }
83 return 0;
84
85 trunc:
86 return -1;
87 }
88
89 static int
90 ip6_opt_process(netdissect_options *ndo, const u_char *bp, int len,
91 int *found_jumbop, uint32_t *payload_len)
92 {
93 int i;
94 int optlen = 0;
95 int found_jumbo = 0;
96 uint32_t jumbolen = 0;
97
98 if (len == 0)
99 return 0;
100 for (i = 0; i < len; i += optlen) {
101 if (GET_U_1(bp + i) == IP6OPT_PAD1)
102 optlen = 1;
103 else {
104 if (i + 1 < len)
105 optlen = GET_U_1(bp + i + 1) + 2;
106 else
107 goto trunc;
108 }
109 if (i + optlen > len)
110 goto trunc;
111
112 switch (GET_U_1(bp + i)) {
113 case IP6OPT_PAD1:
114 if (ndo->ndo_vflag)
115 ND_PRINT("(pad1)");
116 break;
117 case IP6OPT_PADN:
118 if (len - i < IP6OPT_MINLEN) {
119 ND_PRINT("(padn: trunc)");
120 goto trunc;
121 }
122 if (ndo->ndo_vflag)
123 ND_PRINT("(padn)");
124 break;
125 case IP6OPT_ROUTER_ALERT:
126 if (len - i < IP6OPT_RTALERT_LEN) {
127 ND_PRINT("(rtalert: trunc)");
128 goto trunc;
129 }
130 if (GET_U_1(bp + i + 1) != IP6OPT_RTALERT_LEN - 2) {
131 ND_PRINT("(rtalert: invalid len %u)", GET_U_1(bp + i + 1));
132 goto trunc;
133 }
134 if (ndo->ndo_vflag)
135 ND_PRINT("(rtalert: 0x%04x) ", GET_BE_U_2(bp + i + 2));
136 break;
137 case IP6OPT_JUMBO:
138 if (len - i < IP6OPT_JUMBO_LEN) {
139 ND_PRINT("(jumbo: trunc)");
140 goto trunc;
141 }
142 if (GET_U_1(bp + i + 1) != IP6OPT_JUMBO_LEN - 2) {
143 ND_PRINT("(jumbo: invalid len %u)", GET_U_1(bp + i + 1));
144 goto trunc;
145 }
146 jumbolen = GET_BE_U_4(bp + i + 2);
147 if (found_jumbo) {
148 /* More than one Jumbo Payload option */
149 if (ndo->ndo_vflag)
150 ND_PRINT("(jumbo: %u - already seen) ", jumbolen);
151 } else {
152 found_jumbo = 1;
153 if (*payload_len != 0) {
154 /* Payload length was non-zero - not valid */
155 if (ndo->ndo_vflag)
156 ND_PRINT("(jumbo: %u - payload len != 0) ", jumbolen);
157 } else {
158 /* Payload length was zero in the IPv6 header */
159 if (jumbolen < 65536) {
160 /* Too short */
161 if (ndo->ndo_vflag)
162 ND_PRINT("(jumbo: %u - < 65536) ", jumbolen);
163 } else {
164 /* OK, this is valid */
165 *found_jumbop = 1;
166 *payload_len = jumbolen;
167 if (ndo->ndo_vflag)
168 ND_PRINT("(jumbo: %u) ", jumbolen);
169 }
170 }
171 }
172 break;
173 case IP6OPT_HOME_ADDRESS:
174 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
175 ND_PRINT("(homeaddr: trunc)");
176 goto trunc;
177 }
178 if (GET_U_1(bp + i + 1) < IP6OPT_HOMEADDR_MINLEN - 2) {
179 ND_PRINT("(homeaddr: invalid len %u)", GET_U_1(bp + i + 1));
180 goto trunc;
181 }
182 if (ndo->ndo_vflag) {
183 ND_PRINT("(homeaddr: %s", ip6addr_string(ndo, bp + i + 2));
184 if (GET_U_1(bp + i + 1) > IP6OPT_HOMEADDR_MINLEN - 2) {
185 if (ip6_sopt_print(ndo, bp + i + IP6OPT_HOMEADDR_MINLEN,
186 (optlen - IP6OPT_HOMEADDR_MINLEN)) == -1)
187 goto trunc;
188 }
189 ND_PRINT(")");
190 }
191 break;
192 default:
193 if (len - i < IP6OPT_MINLEN) {
194 ND_PRINT("(type %u: trunc)", GET_U_1(bp + i));
195 goto trunc;
196 }
197 if (ndo->ndo_vflag)
198 ND_PRINT("(opt_type 0x%02x: len=%u)", GET_U_1(bp + i),
199 GET_U_1(bp + i + 1));
200 break;
201 }
202 }
203 if (ndo->ndo_vflag)
204 ND_PRINT(" ");
205 return 0;
206
207 trunc:
208 return -1;
209 }
210
211 int
212 hbhopt_process(netdissect_options *ndo, const u_char *bp, int *found_jumbo,
213 uint32_t *jumbolen)
214 {
215 const struct ip6_hbh *dp = (const struct ip6_hbh *)bp;
216 u_int hbhlen = 0;
217
218 ndo->ndo_protocol = "hbhopt";
219 ND_TCHECK_1(dp->ip6h_len);
220 hbhlen = (GET_U_1(dp->ip6h_len) + 1) << 3;
221 ND_TCHECK_LEN(dp, hbhlen);
222 ND_PRINT("HBH ");
223 if (ip6_opt_process(ndo, (const u_char *)dp + sizeof(*dp),
224 hbhlen - sizeof(*dp), found_jumbo, jumbolen) == -1)
225 goto trunc;
226 return hbhlen;
227
228 trunc:
229 nd_print_trunc(ndo);
230 return -1;
231 }
232
233 int
234 dstopt_process(netdissect_options *ndo, const u_char *bp)
235 {
236 const struct ip6_dest *dp = (const struct ip6_dest *)bp;
237 u_int dstoptlen = 0;
238 int found_jumbo;
239 uint32_t jumbolen;
240
241 ndo->ndo_protocol = "dstopt";
242 ND_TCHECK_1(dp->ip6d_len);
243 dstoptlen = (GET_U_1(dp->ip6d_len) + 1) << 3;
244 ND_TCHECK_LEN(dp, dstoptlen);
245 ND_PRINT("DSTOPT ");
246 if (ndo->ndo_vflag) {
247 /*
248 * The Jumbo Payload option is a hop-by-hop option; we print,
249 * but don't honor, Jumbo Payload destination options.
250 */
251 if (ip6_opt_process(ndo, (const u_char *)dp + sizeof(*dp),
252 dstoptlen - sizeof(*dp), &found_jumbo,
253 &jumbolen) == -1)
254 goto trunc;
255 }
256
257 return dstoptlen;
258
259 trunc:
260 nd_print_trunc(ndo);
261 return -1;
262 }