Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * ip/ip6_format.c: ip6 formatting |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/ip/ip.h> |
| 41 | |
| 42 | /* Format an IP6 address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 43 | u8 * |
| 44 | format_ip6_address (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 46 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | u32 max_zero_run = 0, this_zero_run = 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 48 | int max_zero_run_index = -1, this_zero_run_index = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | int in_zero_run = 0, i; |
| 50 | int last_double_colon = 0; |
| 51 | |
| 52 | /* Ugh, this is a pain. Scan forward looking for runs of 0's */ |
| 53 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 54 | { |
| 55 | if (a->as_u16[i] == 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 56 | { |
| 57 | if (in_zero_run) |
| 58 | this_zero_run++; |
| 59 | else |
| 60 | { |
| 61 | in_zero_run = 1; |
| 62 | this_zero_run = 1; |
| 63 | this_zero_run_index = i; |
| 64 | } |
| 65 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 67 | { |
| 68 | if (in_zero_run) |
| 69 | { |
| 70 | /* offer to compress the biggest run of > 1 zero */ |
| 71 | if (this_zero_run > max_zero_run && this_zero_run > 1) |
| 72 | { |
| 73 | max_zero_run_index = this_zero_run_index; |
| 74 | max_zero_run = this_zero_run; |
| 75 | } |
| 76 | } |
| 77 | in_zero_run = 0; |
| 78 | this_zero_run = 0; |
| 79 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (in_zero_run) |
| 83 | { |
| 84 | if (this_zero_run > max_zero_run && this_zero_run > 1) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 85 | { |
| 86 | max_zero_run_index = this_zero_run_index; |
| 87 | max_zero_run = this_zero_run; |
| 88 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 90 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 92 | { |
| 93 | if (i == max_zero_run_index) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 94 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | s = format (s, "::"); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 96 | i += max_zero_run - 1; |
| 97 | last_double_colon = 1; |
| 98 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | else |
| 100 | { |
| 101 | s = format (s, "%s%x", |
| 102 | (last_double_colon || i == 0) ? "" : ":", |
| 103 | clib_net_to_host_u16 (a->as_u16[i])); |
| 104 | last_double_colon = 0; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return s; |
| 109 | } |
| 110 | |
| 111 | /* Format an IP6 route destination and length. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 112 | u8 * |
| 113 | format_ip6_address_and_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 115 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | u8 l = va_arg (*args, u32); |
| 117 | return format (s, "%U/%d", format_ip6_address, a, l); |
| 118 | } |
| 119 | |
Damjan Marion | a35cc14 | 2018-03-16 01:25:27 +0100 | [diff] [blame] | 120 | u8 * |
| 121 | format_ip6_address_and_mask (u8 * s, va_list * args) |
| 122 | { |
| 123 | ip6_address_and_mask_t *am = va_arg (*args, ip6_address_and_mask_t *); |
| 124 | |
| 125 | if (am->addr.as_u64[0] == 0 && am->addr.as_u64[1] == 0 && |
| 126 | am->mask.as_u64[0] == 0 && am->mask.as_u64[1] == 0) |
| 127 | return format (s, "any"); |
| 128 | |
| 129 | if (am->mask.as_u64[0] == ~0 && am->mask.as_u64[1] == ~0) |
| 130 | return format (s, "%U", format_ip4_address, &am->addr); |
| 131 | |
| 132 | return format (s, "%U/%U", format_ip6_address, &am->addr, |
| 133 | format_ip4_address, &am->mask); |
| 134 | } |
| 135 | |
| 136 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | /* Parse an IP6 address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 138 | uword |
| 139 | unformat_ip6_address (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 141 | ip6_address_t *result = va_arg (*args, ip6_address_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | u16 hex_quads[8]; |
| 143 | uword hex_quad, n_hex_quads, hex_digit, n_hex_digits; |
| 144 | uword c, n_colon, double_colon_index; |
| 145 | |
| 146 | n_hex_quads = hex_quad = n_hex_digits = n_colon = 0; |
| 147 | double_colon_index = ARRAY_LEN (hex_quads); |
| 148 | while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT) |
| 149 | { |
| 150 | hex_digit = 16; |
| 151 | if (c >= '0' && c <= '9') |
| 152 | hex_digit = c - '0'; |
| 153 | else if (c >= 'a' && c <= 'f') |
| 154 | hex_digit = c + 10 - 'a'; |
| 155 | else if (c >= 'A' && c <= 'F') |
| 156 | hex_digit = c + 10 - 'A'; |
| 157 | else if (c == ':' && n_colon < 2) |
| 158 | n_colon++; |
| 159 | else |
| 160 | { |
| 161 | unformat_put_input (input); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | /* Too many hex quads. */ |
| 166 | if (n_hex_quads >= ARRAY_LEN (hex_quads)) |
| 167 | return 0; |
| 168 | |
| 169 | if (hex_digit < 16) |
| 170 | { |
| 171 | hex_quad = (hex_quad << 4) | hex_digit; |
| 172 | |
| 173 | /* Hex quad must fit in 16 bits. */ |
| 174 | if (n_hex_digits >= 4) |
| 175 | return 0; |
| 176 | |
| 177 | n_colon = 0; |
| 178 | n_hex_digits++; |
| 179 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 180 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | /* Save position of :: */ |
| 182 | if (n_colon == 2) |
| 183 | { |
| 184 | /* More than one :: ? */ |
| 185 | if (double_colon_index < ARRAY_LEN (hex_quads)) |
| 186 | return 0; |
| 187 | double_colon_index = n_hex_quads; |
| 188 | } |
| 189 | |
| 190 | if (n_colon > 0 && n_hex_digits > 0) |
| 191 | { |
| 192 | hex_quads[n_hex_quads++] = hex_quad; |
| 193 | hex_quad = 0; |
| 194 | n_hex_digits = 0; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (n_hex_digits > 0) |
| 199 | hex_quads[n_hex_quads++] = hex_quad; |
| 200 | |
| 201 | { |
| 202 | word i; |
| 203 | |
| 204 | /* Expand :: to appropriate number of zero hex quads. */ |
| 205 | if (double_colon_index < ARRAY_LEN (hex_quads)) |
| 206 | { |
| 207 | word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads; |
| 208 | |
| 209 | for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--) |
| 210 | hex_quads[n_zero + i] = hex_quads[i]; |
| 211 | |
| 212 | for (i = 0; i < n_zero; i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 213 | { |
| 214 | ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads)); |
| 215 | hex_quads[double_colon_index + i] = 0; |
| 216 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
| 218 | n_hex_quads = ARRAY_LEN (hex_quads); |
| 219 | } |
| 220 | |
| 221 | /* Too few hex quads given. */ |
| 222 | if (n_hex_quads < ARRAY_LEN (hex_quads)) |
| 223 | return 0; |
| 224 | |
| 225 | for (i = 0; i < ARRAY_LEN (hex_quads); i++) |
| 226 | result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]); |
| 227 | |
| 228 | return 1; |
| 229 | } |
| 230 | } |
| 231 | |
Damjan Marion | a35cc14 | 2018-03-16 01:25:27 +0100 | [diff] [blame] | 232 | uword |
| 233 | unformat_ip6_address_and_mask (unformat_input_t * input, va_list * args) |
| 234 | { |
| 235 | ip6_address_and_mask_t *am = va_arg (*args, ip6_address_and_mask_t *); |
| 236 | ip6_address_t addr, mask; |
| 237 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 238 | clib_memset (&addr, 0, sizeof (ip6_address_t)); |
| 239 | clib_memset (&mask, 0, sizeof (ip6_address_t)); |
Damjan Marion | a35cc14 | 2018-03-16 01:25:27 +0100 | [diff] [blame] | 240 | |
| 241 | if (unformat (input, "any")) |
| 242 | ; |
| 243 | else if (unformat (input, "%U/%U", unformat_ip6_address, &addr, |
| 244 | unformat_ip6_address, &mask)) |
| 245 | ; |
| 246 | else if (unformat (input, "%U", unformat_ip6_address, &addr)) |
| 247 | mask.as_u64[0] = mask.as_u64[1] = ~0; |
| 248 | else |
| 249 | return 0; |
| 250 | |
| 251 | am->addr.as_u64[0] = addr.as_u64[0]; |
| 252 | am->addr.as_u64[1] = addr.as_u64[1]; |
| 253 | am->mask.as_u64[0] = mask.as_u64[0]; |
| 254 | am->mask.as_u64[1] = mask.as_u64[1]; |
| 255 | return 1; |
| 256 | } |
| 257 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | /* Format an IP6 header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 259 | u8 * |
| 260 | format_ip6_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 262 | ip6_header_t *ip = va_arg (*args, ip6_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | u32 max_header_bytes = va_arg (*args, u32); |
| 264 | u32 i, ip_version, traffic_class, flow_label; |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 265 | u32 indent; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
| 267 | /* Nothing to do. */ |
| 268 | if (max_header_bytes < sizeof (ip[0])) |
| 269 | return format (s, "IP header truncated"); |
| 270 | |
| 271 | indent = format_get_indent (s); |
| 272 | indent += 2; |
| 273 | |
| 274 | s = format (s, "%U: %U -> %U", |
| 275 | format_ip_protocol, ip->protocol, |
| 276 | format_ip6_address, &ip->src_address, |
| 277 | format_ip6_address, &ip->dst_address); |
| 278 | |
| 279 | i = clib_net_to_host_u32 (ip->ip_version_traffic_class_and_flow_label); |
| 280 | ip_version = (i >> 28); |
| 281 | traffic_class = (i >> 20) & 0xff; |
| 282 | flow_label = i & pow2_mask (20); |
| 283 | |
| 284 | if (ip_version != 6) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 285 | s = format (s, "\n%Uversion %d", format_white_space, indent, ip_version); |
| 286 | |
| 287 | s = |
| 288 | format (s, |
| 289 | "\n%Utos 0x%02x, flow label 0x%x, hop limit %d, payload length %d", |
| 290 | format_white_space, indent, traffic_class, flow_label, |
| 291 | ip->hop_limit, clib_net_to_host_u16 (ip->payload_length)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
| 293 | /* Recurse into next protocol layer. */ |
| 294 | if (max_header_bytes != 0 && sizeof (ip[0]) < max_header_bytes) |
| 295 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 296 | ip_main_t *im = &ip_main; |
| 297 | ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
| 299 | if (pi && pi->format_header) |
| 300 | s = format (s, "\n%U%U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 301 | format_white_space, indent - 2, pi->format_header, |
| 302 | /* next protocol header */ (void *) (ip + 1), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | max_header_bytes - sizeof (ip[0])); |
| 304 | } |
| 305 | |
| 306 | return s; |
| 307 | } |
| 308 | |
| 309 | /* Parse an IP6 header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 310 | uword |
| 311 | unformat_ip6_header (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 313 | u8 **result = va_arg (*args, u8 **); |
| 314 | ip6_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | int old_length; |
| 316 | |
| 317 | /* Allocate space for IP header. */ |
| 318 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 319 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
| 321 | old_length = vec_len (*result); |
| 322 | vec_add2 (*result, p, sizeof (ip[0])); |
| 323 | ip = p; |
| 324 | } |
| 325 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 326 | clib_memset (ip, 0, sizeof (ip[0])); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 327 | ip->ip_version_traffic_class_and_flow_label = |
| 328 | clib_host_to_net_u32 (6 << 28); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 330 | if (!unformat (input, "%U: %U -> %U", |
| 331 | unformat_ip_protocol, &ip->protocol, |
| 332 | unformat_ip6_address, &ip->src_address, |
| 333 | unformat_ip6_address, &ip->dst_address)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | return 0; |
| 335 | |
| 336 | /* Parse options. */ |
| 337 | while (1) |
| 338 | { |
| 339 | int i; |
| 340 | |
| 341 | if (unformat (input, "tos %U", unformat_vlib_number, &i)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 342 | ip->ip_version_traffic_class_and_flow_label |= |
| 343 | clib_host_to_net_u32 ((i & 0xff) << 20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | |
| 345 | else if (unformat (input, "hop-limit %U", unformat_vlib_number, &i)) |
| 346 | ip->hop_limit = i; |
| 347 | |
| 348 | /* Can't parse input: try next protocol level. */ |
| 349 | else |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | /* Recurse into next protocol layer. */ |
| 354 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 355 | ip_main_t *im = &ip_main; |
| 356 | ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | |
| 358 | if (pi && pi->unformat_header) |
| 359 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 360 | if (!unformat_user (input, pi->unformat_header, result)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | return 0; |
| 362 | |
| 363 | /* Result may have moved. */ |
| 364 | ip = (void *) *result + old_length; |
| 365 | } |
| 366 | } |
| 367 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 368 | ip->payload_length = |
| 369 | clib_host_to_net_u16 (vec_len (*result) - (old_length + sizeof (ip[0]))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | |
| 371 | return 1; |
| 372 | } |
Pierre Pfister | 1dabaaf | 2016-04-25 14:15:15 +0100 | [diff] [blame] | 373 | |
| 374 | /* Parse an IP46 address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 375 | uword |
| 376 | unformat_ip46_address (unformat_input_t * input, va_list * args) |
Pierre Pfister | 1dabaaf | 2016-04-25 14:15:15 +0100 | [diff] [blame] | 377 | { |
| 378 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 379 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 380 | if ((type != IP46_TYPE_IP6) && |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 381 | unformat (input, "%U", unformat_ip4_address, &ip46->ip4)) |
| 382 | { |
| 383 | ip46_address_mask_ip4 (ip46); |
| 384 | return 1; |
| 385 | } |
| 386 | else if ((type != IP46_TYPE_IP4) && |
| 387 | unformat (input, "%U", unformat_ip6_address, &ip46->ip6)) |
| 388 | { |
| 389 | return 1; |
| 390 | } |
Pierre Pfister | 1dabaaf | 2016-04-25 14:15:15 +0100 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* Format an IP46 address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 395 | u8 * |
| 396 | format_ip46_address (u8 * s, va_list * args) |
Pierre Pfister | 1dabaaf | 2016-04-25 14:15:15 +0100 | [diff] [blame] | 397 | { |
| 398 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
Damjan Marion | 86be487 | 2016-05-24 23:19:11 +0200 | [diff] [blame] | 399 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 400 | int is_ip4 = 1; |
| 401 | |
| 402 | switch (type) |
| 403 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 404 | case IP46_TYPE_ANY: |
| 405 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 406 | break; |
| 407 | case IP46_TYPE_IP4: |
| 408 | is_ip4 = 1; |
| 409 | break; |
| 410 | case IP46_TYPE_IP6: |
| 411 | is_ip4 = 0; |
| 412 | break; |
Damjan Marion | 86be487 | 2016-05-24 23:19:11 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return is_ip4 ? |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 416 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 417 | format (s, "%U", format_ip6_address, &ip46->ip6); |
Pierre Pfister | 1dabaaf | 2016-04-25 14:15:15 +0100 | [diff] [blame] | 418 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 419 | |
Klement Sekera | 8563cb3 | 2019-10-10 17:03:57 +0000 | [diff] [blame^] | 420 | u8 * |
| 421 | format_ip6_frag_hdr (u8 * s, va_list * args) |
| 422 | { |
| 423 | ip6_frag_hdr_t *h = va_arg (*args, ip6_frag_hdr_t *); |
| 424 | u32 max_header_bytes = va_arg (*args, u32); |
| 425 | u32 header_bytes; |
| 426 | |
| 427 | header_bytes = sizeof (h[0]); |
| 428 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 429 | return format (s, "ipv6 frag header truncated"); |
| 430 | |
| 431 | s = |
| 432 | format (s, |
| 433 | "IPV6_FRAG_HDR: next_hdr: %u, rsv: %u, frag_offset_and_more: %u, id: %u", |
| 434 | h->next_hdr, h->rsv, h->fragment_offset_and_more, |
| 435 | clib_net_to_host_u32 (h->identification)); |
| 436 | return s; |
| 437 | } |
| 438 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 439 | /* |
| 440 | * fd.io coding-style-patch-verification: ON |
| 441 | * |
| 442 | * Local Variables: |
| 443 | * eval: (c-set-style "gnu") |
| 444 | * End: |
| 445 | */ |