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 == 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 } 179 break; 180 case IP6OPT_HOME_ADDRESS: 181 if (len - i < IP6OPT_HOMEADDR_MINLEN) { 182 ND_PRINT("(homeaddr: trunc)"); 183 goto trunc; 184 } 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)); 187 goto trunc; 188 } 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(")"); 197 } 198 break; 199 default: 200 if (len - i < IP6OPT_MINLEN) { 201 ND_PRINT("(type %u: trunc)", GET_U_1(bp + i)); 202 goto trunc; 203 } 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)); 207 break; 208 } 209 } 210 if (ndo->ndo_vflag) 211 ND_PRINT(" "); 212 return 0; 213 214 trunc: 215 return -1; 216 } 217 218 int 219 hbhopt_process(netdissect_options *ndo, const u_char *bp, int *found_jumbo, 220 uint32_t *jumbolen) 221 { 222 const struct ip6_hbh *dp = (const struct ip6_hbh *)bp; 223 u_int hbhlen = 0; 224 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; 233 234 trunc: 235 nd_print_trunc(ndo); 236 return -1; 237 } 238 239 int 240 dstopt_process(netdissect_options *ndo, const u_char *bp) 241 { 242 const struct ip6_dest *dp = (const struct ip6_dest *)bp; 243 u_int dstoptlen = 0; 244 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 "); 249 if (ndo->ndo_vflag) { 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; 258 } 259 260 return dstoptlen; 261 262 trunc: 263 nd_print_trunc(ndo); 264 return -1; 265 } 266