blob: a78c76daa27def5e848179534e9ba0b4bb883bea [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
Elliott Hughese2e3bd12017-05-15 10:59:29 -070030/* \summary: IPv6 header option printer */
31
The Android Open Source Project2949f582009-03-03 19:30:46 -080032#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070033#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080034#endif
35
Elliott Hughes820eced2021-08-20 18:00:50 -070036#include "netdissect-stdinc.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080037
Elliott Hughese2e3bd12017-05-15 10:59:29 -070038#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080039#include "addrtoname.h"
40#include "extract.h"
41
Elliott Hughescec480a2017-12-19 16:54:57 -080042#include "ip6.h"
43
Elliott Hughes820eced2021-08-20 18:00:50 -070044static int
Elliott Hughes892a68b2015-10-19 14:43:53 -070045ip6_sopt_print(netdissect_options *ndo, const u_char *bp, int len)
The Android Open Source Project2949f582009-03-03 19:30:46 -080046{
47 int i;
48 int optlen;
49
50 for (i = 0; i < len; i += optlen) {
Elliott Hughes820eced2021-08-20 18:00:50 -070051 if (GET_U_1(bp + i) == IP6OPT_PAD1)
The Android Open Source Project2949f582009-03-03 19:30:46 -080052 optlen = 1;
53 else {
54 if (i + 1 < len)
Elliott Hughes820eced2021-08-20 18:00:50 -070055 optlen = GET_U_1(bp + i + 1) + 2;
The Android Open Source Project2949f582009-03-03 19:30:46 -080056 else
57 goto trunc;
58 }
59 if (i + optlen > len)
60 goto trunc;
61
Elliott Hughes820eced2021-08-20 18:00:50 -070062 switch (GET_U_1(bp + i)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -080063 case IP6OPT_PAD1:
Elliott Hughes820eced2021-08-20 18:00:50 -070064 ND_PRINT(", pad1");
The Android Open Source Project2949f582009-03-03 19:30:46 -080065 break;
66 case IP6OPT_PADN:
67 if (len - i < IP6OPT_MINLEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -070068 ND_PRINT(", padn: trunc");
The Android Open Source Project2949f582009-03-03 19:30:46 -080069 goto trunc;
70 }
Elliott Hughes820eced2021-08-20 18:00:50 -070071 ND_PRINT(", padn");
The Android Open Source Project2949f582009-03-03 19:30:46 -080072 break;
73 default:
74 if (len - i < IP6OPT_MINLEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -070075 ND_PRINT(", sopt_type %u: trunc)", GET_U_1(bp + i));
The Android Open Source Project2949f582009-03-03 19:30:46 -080076 goto trunc;
77 }
Elliott Hughes820eced2021-08-20 18:00:50 -070078 ND_PRINT(", sopt_type 0x%02x: len=%u", GET_U_1(bp + i),
79 GET_U_1(bp + i + 1));
The Android Open Source Project2949f582009-03-03 19:30:46 -080080 break;
81 }
82 }
Elliott Hughes820eced2021-08-20 18:00:50 -070083 return 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -080084
85trunc:
Elliott Hughes820eced2021-08-20 18:00:50 -070086 return -1;
The Android Open Source Project2949f582009-03-03 19:30:46 -080087}
88
Elliott Hughes820eced2021-08-20 18:00:50 -070089static int
90ip6_opt_process(netdissect_options *ndo, const u_char *bp, int len,
91 int *found_jumbop, uint32_t *payload_len)
The Android Open Source Project2949f582009-03-03 19:30:46 -080092{
93 int i;
94 int optlen = 0;
Elliott Hughes820eced2021-08-20 18:00:50 -070095 int found_jumbo = 0;
96 uint32_t jumbolen = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -080097
JP Abgrall53f17a92014-02-12 14:02:41 -080098 if (len == 0)
Elliott Hughes820eced2021-08-20 18:00:50 -070099 return 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800100 for (i = 0; i < len; i += optlen) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700101 if (GET_U_1(bp + i) == IP6OPT_PAD1)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800102 optlen = 1;
103 else {
104 if (i + 1 < len)
Elliott Hughes820eced2021-08-20 18:00:50 -0700105 optlen = GET_U_1(bp + i + 1) + 2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800106 else
107 goto trunc;
108 }
109 if (i + optlen > len)
110 goto trunc;
111
Elliott Hughes820eced2021-08-20 18:00:50 -0700112 switch (GET_U_1(bp + i)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800113 case IP6OPT_PAD1:
Elliott Hughes820eced2021-08-20 18:00:50 -0700114 if (ndo->ndo_vflag)
115 ND_PRINT("(pad1)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800116 break;
117 case IP6OPT_PADN:
118 if (len - i < IP6OPT_MINLEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700119 ND_PRINT("(padn: trunc)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800120 goto trunc;
121 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700122 if (ndo->ndo_vflag)
123 ND_PRINT("(padn)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800124 break;
125 case IP6OPT_ROUTER_ALERT:
126 if (len - i < IP6OPT_RTALERT_LEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700127 ND_PRINT("(rtalert: trunc)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800128 goto trunc;
129 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700130 if (GET_U_1(bp + i + 1) != IP6OPT_RTALERT_LEN - 2) {
131 ND_PRINT("(rtalert: invalid len %u)", GET_U_1(bp + i + 1));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800132 goto trunc;
133 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700134 if (ndo->ndo_vflag)
135 ND_PRINT("(rtalert: 0x%04x) ", GET_BE_U_2(bp + i + 2));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800136 break;
137 case IP6OPT_JUMBO:
138 if (len - i < IP6OPT_JUMBO_LEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700139 ND_PRINT("(jumbo: trunc)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800140 goto trunc;
141 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700142 if (GET_U_1(bp + i + 1) != IP6OPT_JUMBO_LEN - 2) {
143 ND_PRINT("(jumbo: invalid len %u)", GET_U_1(bp + i + 1));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800144 goto trunc;
145 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700146 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 == NULL) {
154 /* Not a hop-by-hop option - not valid */
155 if (ndo->ndo_vflag)
156 ND_PRINT("(jumbo: %u - not a hop-by-hop option) ", jumbolen);
157 } else if (*payload_len != 0) {
158 /* Payload length was non-zero - not valid */
159 if (ndo->ndo_vflag)
160 ND_PRINT("(jumbo: %u - payload len != 0) ", jumbolen);
161 } else {
162 /*
163 * This is a hop-by-hop option, and Payload length
164 * was zero in the IPv6 header.
165 */
166 if (jumbolen < 65536) {
167 /* Too short */
168 if (ndo->ndo_vflag)
169 ND_PRINT("(jumbo: %u - < 65536) ", jumbolen);
170 } else {
171 /* OK, this is valid */
172 *found_jumbop = 1;
173 *payload_len = jumbolen;
174 if (ndo->ndo_vflag)
175 ND_PRINT("(jumbo: %u) ", jumbolen);
176 }
177 }
178 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800179 break;
180 case IP6OPT_HOME_ADDRESS:
181 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700182 ND_PRINT("(homeaddr: trunc)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800183 goto trunc;
184 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700185 if (GET_U_1(bp + i + 1) < IP6OPT_HOMEADDR_MINLEN - 2) {
186 ND_PRINT("(homeaddr: invalid len %u)", GET_U_1(bp + i + 1));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800187 goto trunc;
188 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700189 if (ndo->ndo_vflag) {
190 ND_PRINT("(homeaddr: %s", GET_IP6ADDR_STRING(bp + i + 2));
191 if (GET_U_1(bp + i + 1) > IP6OPT_HOMEADDR_MINLEN - 2) {
192 if (ip6_sopt_print(ndo, bp + i + IP6OPT_HOMEADDR_MINLEN,
193 (optlen - IP6OPT_HOMEADDR_MINLEN)) == -1)
194 goto trunc;
195 }
196 ND_PRINT(")");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800197 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800198 break;
199 default:
200 if (len - i < IP6OPT_MINLEN) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700201 ND_PRINT("(type %u: trunc)", GET_U_1(bp + i));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800202 goto trunc;
203 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700204 if (ndo->ndo_vflag)
205 ND_PRINT("(opt_type 0x%02x: len=%u)", GET_U_1(bp + i),
206 GET_U_1(bp + i + 1));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800207 break;
208 }
209 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700210 if (ndo->ndo_vflag)
211 ND_PRINT(" ");
212 return 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800213
214trunc:
Elliott Hughes820eced2021-08-20 18:00:50 -0700215 return -1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800216}
217
218int
Elliott Hughes820eced2021-08-20 18:00:50 -0700219hbhopt_process(netdissect_options *ndo, const u_char *bp, int *found_jumbo,
220 uint32_t *jumbolen)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800221{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700222 const struct ip6_hbh *dp = (const struct ip6_hbh *)bp;
Elliott Hughes820eced2021-08-20 18:00:50 -0700223 u_int hbhlen = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800224
Elliott Hughes820eced2021-08-20 18:00:50 -0700225 ndo->ndo_protocol = "hbhopt";
226 hbhlen = (GET_U_1(dp->ip6h_len) + 1) << 3;
227 ND_TCHECK_LEN(dp, hbhlen);
228 ND_PRINT("HBH ");
229 if (ip6_opt_process(ndo, (const u_char *)dp + sizeof(*dp),
230 hbhlen - sizeof(*dp), found_jumbo, jumbolen) == -1)
231 goto trunc;
232 return hbhlen;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800233
Elliott Hughes820eced2021-08-20 18:00:50 -0700234trunc:
235 nd_print_trunc(ndo);
236 return -1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800237}
238
239int
Elliott Hughes820eced2021-08-20 18:00:50 -0700240dstopt_process(netdissect_options *ndo, const u_char *bp)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800241{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700242 const struct ip6_dest *dp = (const struct ip6_dest *)bp;
Elliott Hughes820eced2021-08-20 18:00:50 -0700243 u_int dstoptlen = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800244
Elliott Hughes820eced2021-08-20 18:00:50 -0700245 ndo->ndo_protocol = "dstopt";
246 dstoptlen = (GET_U_1(dp->ip6d_len) + 1) << 3;
247 ND_TCHECK_LEN(dp, dstoptlen);
248 ND_PRINT("DSTOPT ");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700249 if (ndo->ndo_vflag) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700250 /*
251 * The Jumbo Payload option is a hop-by-hop option; we don't
252 * honor Jumbo Payload destination options, reporting them
253 * as invalid.
254 */
255 if (ip6_opt_process(ndo, (const u_char *)dp + sizeof(*dp),
256 dstoptlen - sizeof(*dp), NULL, NULL) == -1)
257 goto trunc;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800258 }
259
Elliott Hughes820eced2021-08-20 18:00:50 -0700260 return dstoptlen;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800261
Elliott Hughes820eced2021-08-20 18:00:50 -0700262trunc:
263 nd_print_trunc(ndo);
264 return -1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800265}