The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 30 | /* \summary: IPv6 header option printer */ |
| 31 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 33 | #include <config.h> |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 34 | #endif |
| 35 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 36 | #include "netdissect-stdinc.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 38 | #include "netdissect.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 39 | #include "addrtoname.h" |
| 40 | #include "extract.h" |
| 41 | |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 42 | #include "ip6.h" |
| 43 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 44 | static int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 45 | ip6_sopt_print(netdissect_options *ndo, const u_char *bp, int len) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 46 | { |
| 47 | int i; |
| 48 | int optlen; |
| 49 | |
| 50 | for (i = 0; i < len; i += optlen) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 51 | if (GET_U_1(bp + i) == IP6OPT_PAD1) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 52 | optlen = 1; |
| 53 | else { |
| 54 | if (i + 1 < len) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 55 | optlen = GET_U_1(bp + i + 1) + 2; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 56 | else |
| 57 | goto trunc; |
| 58 | } |
| 59 | if (i + optlen > len) |
| 60 | goto trunc; |
| 61 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 62 | switch (GET_U_1(bp + i)) { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 63 | case IP6OPT_PAD1: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 64 | ND_PRINT(", pad1"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 65 | break; |
| 66 | case IP6OPT_PADN: |
| 67 | if (len - i < IP6OPT_MINLEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 68 | ND_PRINT(", padn: trunc"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 69 | goto trunc; |
| 70 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 71 | ND_PRINT(", padn"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 72 | break; |
| 73 | default: |
| 74 | if (len - i < IP6OPT_MINLEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 75 | ND_PRINT(", sopt_type %u: trunc)", GET_U_1(bp + i)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 76 | goto trunc; |
| 77 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 78 | ND_PRINT(", sopt_type 0x%02x: len=%u", GET_U_1(bp + i), |
| 79 | GET_U_1(bp + i + 1)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 83 | return 0; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 84 | |
| 85 | trunc: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 86 | return -1; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 89 | static int |
| 90 | ip6_opt_process(netdissect_options *ndo, const u_char *bp, int len, |
| 91 | int *found_jumbop, uint32_t *payload_len) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 92 | { |
| 93 | int i; |
| 94 | int optlen = 0; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 95 | int found_jumbo = 0; |
| 96 | uint32_t jumbolen = 0; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 97 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 98 | if (len == 0) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 99 | return 0; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 100 | for (i = 0; i < len; i += optlen) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 101 | if (GET_U_1(bp + i) == IP6OPT_PAD1) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 102 | optlen = 1; |
| 103 | else { |
| 104 | if (i + 1 < len) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 105 | optlen = GET_U_1(bp + i + 1) + 2; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 106 | else |
| 107 | goto trunc; |
| 108 | } |
| 109 | if (i + optlen > len) |
| 110 | goto trunc; |
| 111 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 112 | switch (GET_U_1(bp + i)) { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 113 | case IP6OPT_PAD1: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 114 | if (ndo->ndo_vflag) |
| 115 | ND_PRINT("(pad1)"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 116 | break; |
| 117 | case IP6OPT_PADN: |
| 118 | if (len - i < IP6OPT_MINLEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 119 | ND_PRINT("(padn: trunc)"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 120 | goto trunc; |
| 121 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 122 | if (ndo->ndo_vflag) |
| 123 | ND_PRINT("(padn)"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 124 | break; |
| 125 | case IP6OPT_ROUTER_ALERT: |
| 126 | if (len - i < IP6OPT_RTALERT_LEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 127 | ND_PRINT("(rtalert: trunc)"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 128 | goto trunc; |
| 129 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 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)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 132 | goto trunc; |
| 133 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 134 | if (ndo->ndo_vflag) |
| 135 | ND_PRINT("(rtalert: 0x%04x) ", GET_BE_U_2(bp + i + 2)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 136 | break; |
| 137 | case IP6OPT_JUMBO: |
| 138 | if (len - i < IP6OPT_JUMBO_LEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 139 | ND_PRINT("(jumbo: trunc)"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 140 | goto trunc; |
| 141 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 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)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 144 | goto trunc; |
| 145 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 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 == 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 179 | break; |
| 180 | case IP6OPT_HOME_ADDRESS: |
| 181 | if (len - i < IP6OPT_HOMEADDR_MINLEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 182 | ND_PRINT("(homeaddr: trunc)"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 183 | goto trunc; |
| 184 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 185 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 187 | goto trunc; |
| 188 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 189 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 197 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 198 | break; |
| 199 | default: |
| 200 | if (len - i < IP6OPT_MINLEN) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 201 | ND_PRINT("(type %u: trunc)", GET_U_1(bp + i)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 202 | goto trunc; |
| 203 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 204 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 210 | if (ndo->ndo_vflag) |
| 211 | ND_PRINT(" "); |
| 212 | return 0; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 213 | |
| 214 | trunc: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 215 | return -1; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 219 | hbhopt_process(netdissect_options *ndo, const u_char *bp, int *found_jumbo, |
| 220 | uint32_t *jumbolen) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 221 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 222 | const struct ip6_hbh *dp = (const struct ip6_hbh *)bp; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 223 | u_int hbhlen = 0; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 224 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 225 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 233 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 234 | trunc: |
| 235 | nd_print_trunc(ndo); |
| 236 | return -1; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | int |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 240 | dstopt_process(netdissect_options *ndo, const u_char *bp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 241 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 242 | const struct ip6_dest *dp = (const struct ip6_dest *)bp; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 243 | u_int dstoptlen = 0; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 244 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 245 | 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 Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 249 | if (ndo->ndo_vflag) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 250 | /* |
| 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 260 | return dstoptlen; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 261 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 262 | trunc: |
| 263 | nd_print_trunc(ndo); |
| 264 | return -1; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 265 | } |