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/ip4_format.c: ip4 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 IP4 address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 43 | u8 * |
| 44 | format_ip4_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 | u8 *a = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 48 | } |
| 49 | |
| 50 | /* Format an IP4 route destination and length. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 51 | u8 * |
| 52 | format_ip4_address_and_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 54 | u8 *a = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | u8 l = va_arg (*args, u32); |
| 56 | return format (s, "%U/%d", format_ip4_address, a, l); |
| 57 | } |
| 58 | |
| 59 | /* Parse an IP4 address %d.%d.%d.%d. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 60 | uword |
| 61 | unformat_ip4_address (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 63 | u8 *result = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | unsigned a[4]; |
| 65 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 66 | if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3])) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | |
| 69 | if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256) |
| 70 | return 0; |
| 71 | |
| 72 | result[0] = a[0]; |
| 73 | result[1] = a[1]; |
| 74 | result[2] = a[2]; |
| 75 | result[3] = a[3]; |
| 76 | |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | /* Format an IP4 header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 81 | u8 * |
| 82 | format_ip4_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 84 | ip4_header_t *ip = va_arg (*args, ip4_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | u32 max_header_bytes = va_arg (*args, u32); |
| 86 | u32 ip_version, header_bytes; |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 87 | u32 indent; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
| 89 | /* Nothing to do. */ |
| 90 | if (max_header_bytes < sizeof (ip[0])) |
| 91 | return format (s, "IP header truncated"); |
| 92 | |
| 93 | indent = format_get_indent (s); |
| 94 | indent += 2; |
| 95 | |
| 96 | ip_version = (ip->ip_version_and_header_length >> 4); |
| 97 | header_bytes = (ip->ip_version_and_header_length & 0xf) * sizeof (u32); |
| 98 | |
| 99 | s = format (s, "%U: %U -> %U", |
| 100 | format_ip_protocol, ip->protocol, |
| 101 | format_ip4_address, ip->src_address.data, |
| 102 | format_ip4_address, ip->dst_address.data); |
| 103 | |
| 104 | /* Show IP version and header length only with unexpected values. */ |
| 105 | if (ip_version != 4 || header_bytes != sizeof (ip4_header_t)) |
| 106 | s = format (s, "\n%Uversion %d, header length %d", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 107 | format_white_space, indent, ip_version, header_bytes); |
| 108 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | s = format (s, "\n%Utos 0x%02x, ttl %d, length %d, checksum 0x%04x", |
| 110 | format_white_space, indent, |
| 111 | ip->tos, ip->ttl, |
| 112 | clib_net_to_host_u16 (ip->length), |
| 113 | clib_net_to_host_u16 (ip->checksum)); |
| 114 | |
| 115 | /* Check and report invalid checksums. */ |
| 116 | { |
| 117 | u16 c = ip4_header_checksum (ip); |
| 118 | if (c != ip->checksum) |
| 119 | s = format (s, " (should be 0x%04x)", clib_net_to_host_u16 (c)); |
| 120 | } |
| 121 | |
| 122 | { |
| 123 | u32 f = clib_net_to_host_u16 (ip->flags_and_fragment_offset); |
| 124 | u32 o; |
| 125 | |
| 126 | s = format (s, "\n%Ufragment id 0x%04x", |
| 127 | format_white_space, indent, |
| 128 | clib_net_to_host_u16 (ip->fragment_id)); |
| 129 | |
| 130 | /* Fragment offset. */ |
| 131 | o = 8 * (f & 0x1fff); |
| 132 | f ^= o; |
| 133 | if (o != 0) |
| 134 | s = format (s, " offset %d", o); |
| 135 | |
| 136 | if (f != 0) |
| 137 | { |
| 138 | s = format (s, ", flags "); |
| 139 | #define _(l) if (f & IP4_HEADER_FLAG_##l) s = format (s, #l); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 140 | _(MORE_FRAGMENTS); |
| 141 | _(DONT_FRAGMENT); |
| 142 | _(CONGESTION); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | #undef _ |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Recurse into next protocol layer. */ |
| 148 | if (max_header_bytes != 0 && header_bytes < max_header_bytes) |
| 149 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 150 | ip_main_t *im = &ip_main; |
| 151 | ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | |
| 153 | if (pi && pi->format_header) |
| 154 | s = format (s, "\n%U%U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 155 | format_white_space, indent - 2, pi->format_header, |
| 156 | /* next protocol header */ (void *) ip + header_bytes, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | max_header_bytes - header_bytes); |
| 158 | } |
| 159 | |
| 160 | return s; |
| 161 | } |
| 162 | |
| 163 | /* Parse an IP4 header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 164 | uword |
| 165 | unformat_ip4_header (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 167 | u8 **result = va_arg (*args, u8 **); |
| 168 | ip4_header_t *ip; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | int old_length; |
| 170 | |
| 171 | /* Allocate space for IP header. */ |
| 172 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 173 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
| 175 | old_length = vec_len (*result); |
| 176 | vec_add2 (*result, p, sizeof (ip4_header_t)); |
| 177 | ip = p; |
| 178 | } |
| 179 | |
| 180 | memset (ip, 0, sizeof (ip[0])); |
| 181 | ip->ip_version_and_header_length = IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS; |
| 182 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 183 | if (!unformat (input, "%U: %U -> %U", |
| 184 | unformat_ip_protocol, &ip->protocol, |
| 185 | unformat_ip4_address, &ip->src_address, |
| 186 | unformat_ip4_address, &ip->dst_address)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | return 0; |
| 188 | |
| 189 | /* Parse options. */ |
| 190 | while (1) |
| 191 | { |
| 192 | int i, j; |
| 193 | |
| 194 | if (unformat (input, "tos %U", unformat_vlib_number, &i)) |
| 195 | ip->tos = i; |
| 196 | |
| 197 | else if (unformat (input, "ttl %U", unformat_vlib_number, &i)) |
| 198 | ip->ttl = i; |
| 199 | |
| 200 | else if (unformat (input, "fragment id %U offset %U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 201 | unformat_vlib_number, &i, unformat_vlib_number, &j)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | { |
| 203 | ip->fragment_id = clib_host_to_net_u16 (i); |
| 204 | ip->flags_and_fragment_offset |= |
| 205 | clib_host_to_net_u16 ((i / 8) & 0x1fff); |
| 206 | } |
| 207 | |
| 208 | /* Flags. */ |
| 209 | else if (unformat (input, "mf") || unformat (input, "MF")) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 210 | ip->flags_and_fragment_offset |= |
| 211 | clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
| 213 | else if (unformat (input, "df") || unformat (input, "DF")) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 214 | ip->flags_and_fragment_offset |= |
| 215 | clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
| 217 | else if (unformat (input, "ce") || unformat (input, "CE")) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 218 | ip->flags_and_fragment_offset |= |
| 219 | clib_host_to_net_u16 (IP4_HEADER_FLAG_CONGESTION); |
| 220 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | /* Can't parse input: try next protocol level. */ |
| 222 | else |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | /* Fill in checksum. */ |
| 227 | ip->checksum = ip4_header_checksum (ip); |
| 228 | |
| 229 | /* Recurse into next protocol layer. */ |
| 230 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 231 | ip_main_t *im = &ip_main; |
| 232 | ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
| 234 | if (pi && pi->unformat_header) |
| 235 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 236 | if (!unformat_user (input, pi->unformat_header, result)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | |
| 239 | /* Result may have moved. */ |
| 240 | ip = (void *) *result + old_length; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* Fill in IP length. */ |
| 245 | ip->length = clib_host_to_net_u16 (vec_len (*result) - old_length); |
| 246 | |
| 247 | return 1; |
| 248 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * fd.io coding-style-patch-verification: ON |
| 252 | * |
| 253 | * Local Variables: |
| 254 | * eval: (c-set-style "gnu") |
| 255 | * End: |
| 256 | */ |