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 |
| 33 | #include "config.h" |
| 34 | #endif |
| 35 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -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 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 44 | static void |
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) { |
| 51 | if (bp[i] == IP6OPT_PAD1) |
| 52 | optlen = 1; |
| 53 | else { |
| 54 | if (i + 1 < len) |
| 55 | optlen = bp[i + 1] + 2; |
| 56 | else |
| 57 | goto trunc; |
| 58 | } |
| 59 | if (i + optlen > len) |
| 60 | goto trunc; |
| 61 | |
| 62 | switch (bp[i]) { |
| 63 | case IP6OPT_PAD1: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 64 | ND_PRINT((ndo, ", 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 | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 68 | ND_PRINT((ndo, ", padn: trunc")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 69 | goto trunc; |
| 70 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 71 | ND_PRINT((ndo, ", 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 | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 75 | ND_PRINT((ndo, ", sopt_type %d: trunc)", bp[i])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 76 | goto trunc; |
| 77 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 78 | ND_PRINT((ndo, ", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | } |
| 82 | return; |
| 83 | |
| 84 | trunc: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 85 | ND_PRINT((ndo, "[trunc] ")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 88 | static void |
| 89 | ip6_opt_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] | 90 | { |
| 91 | int i; |
| 92 | int optlen = 0; |
| 93 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 94 | if (len == 0) |
| 95 | return; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 96 | for (i = 0; i < len; i += optlen) { |
| 97 | if (bp[i] == IP6OPT_PAD1) |
| 98 | optlen = 1; |
| 99 | else { |
| 100 | if (i + 1 < len) |
| 101 | optlen = bp[i + 1] + 2; |
| 102 | else |
| 103 | goto trunc; |
| 104 | } |
| 105 | if (i + optlen > len) |
| 106 | goto trunc; |
| 107 | |
| 108 | switch (bp[i]) { |
| 109 | case IP6OPT_PAD1: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 110 | ND_PRINT((ndo, "(pad1)")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 111 | break; |
| 112 | case IP6OPT_PADN: |
| 113 | if (len - i < IP6OPT_MINLEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 114 | ND_PRINT((ndo, "(padn: trunc)")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 115 | goto trunc; |
| 116 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 117 | ND_PRINT((ndo, "(padn)")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 118 | break; |
| 119 | case IP6OPT_ROUTER_ALERT: |
| 120 | if (len - i < IP6OPT_RTALERT_LEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 121 | ND_PRINT((ndo, "(rtalert: trunc)")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 122 | goto trunc; |
| 123 | } |
| 124 | if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 125 | ND_PRINT((ndo, "(rtalert: invalid len %d)", bp[i + 1])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 126 | goto trunc; |
| 127 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 128 | ND_PRINT((ndo, "(rtalert: 0x%04x) ", EXTRACT_16BITS(&bp[i + 2]))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 129 | break; |
| 130 | case IP6OPT_JUMBO: |
| 131 | if (len - i < IP6OPT_JUMBO_LEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 132 | ND_PRINT((ndo, "(jumbo: trunc)")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 133 | goto trunc; |
| 134 | } |
| 135 | if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 136 | ND_PRINT((ndo, "(jumbo: invalid len %d)", bp[i + 1])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 137 | goto trunc; |
| 138 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 139 | ND_PRINT((ndo, "(jumbo: %u) ", EXTRACT_32BITS(&bp[i + 2]))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 140 | break; |
| 141 | case IP6OPT_HOME_ADDRESS: |
| 142 | if (len - i < IP6OPT_HOMEADDR_MINLEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 143 | ND_PRINT((ndo, "(homeaddr: trunc)")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 144 | goto trunc; |
| 145 | } |
| 146 | if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 147 | ND_PRINT((ndo, "(homeaddr: invalid len %d)", bp[i + 1])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 148 | goto trunc; |
| 149 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 150 | ND_PRINT((ndo, "(homeaddr: %s", ip6addr_string(ndo, &bp[i + 2]))); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 151 | if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 152 | ip6_sopt_print(ndo, &bp[i + IP6OPT_HOMEADDR_MINLEN], |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 153 | (optlen - IP6OPT_HOMEADDR_MINLEN)); |
| 154 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 155 | ND_PRINT((ndo, ")")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 156 | break; |
| 157 | default: |
| 158 | if (len - i < IP6OPT_MINLEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 159 | ND_PRINT((ndo, "(type %d: trunc)", bp[i])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 160 | goto trunc; |
| 161 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 162 | ND_PRINT((ndo, "(opt_type 0x%02x: len=%d)", bp[i], bp[i + 1])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 166 | ND_PRINT((ndo, " ")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 167 | return; |
| 168 | |
| 169 | trunc: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 170 | ND_PRINT((ndo, "[trunc] ")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 174 | hbhopt_print(netdissect_options *ndo, register const u_char *bp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 175 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 176 | const struct ip6_hbh *dp = (const struct ip6_hbh *)bp; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 177 | int hbhlen = 0; |
| 178 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 179 | ND_TCHECK(dp->ip6h_len); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 180 | hbhlen = (int)((dp->ip6h_len + 1) << 3); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 181 | ND_TCHECK2(*dp, hbhlen); |
| 182 | ND_PRINT((ndo, "HBH ")); |
| 183 | if (ndo->ndo_vflag) |
| 184 | ip6_opt_print(ndo, (const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 185 | |
| 186 | return(hbhlen); |
| 187 | |
| 188 | trunc: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 189 | ND_PRINT((ndo, "[|HBH]")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 190 | return(-1); |
| 191 | } |
| 192 | |
| 193 | int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 194 | dstopt_print(netdissect_options *ndo, register const u_char *bp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 195 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 196 | const struct ip6_dest *dp = (const struct ip6_dest *)bp; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 197 | int dstoptlen = 0; |
| 198 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 199 | ND_TCHECK(dp->ip6d_len); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 200 | dstoptlen = (int)((dp->ip6d_len + 1) << 3); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 201 | ND_TCHECK2(*dp, dstoptlen); |
| 202 | ND_PRINT((ndo, "DSTOPT ")); |
| 203 | if (ndo->ndo_vflag) { |
| 204 | ip6_opt_print(ndo, (const u_char *)dp + sizeof(*dp), |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 205 | dstoptlen - sizeof(*dp)); |
| 206 | } |
| 207 | |
| 208 | return(dstoptlen); |
| 209 | |
| 210 | trunc: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 211 | ND_PRINT((ndo, "[|DSTOPT]")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 212 | return(-1); |
| 213 | } |