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 | * ip4/packet.h: ip4 packet format |
| 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 | #ifndef included_ip4_packet_h |
| 41 | #define included_ip4_packet_h |
| 42 | |
| 43 | #include <vnet/ip/ip_packet.h> /* for ip_csum_t */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 44 | #include <vnet/tcp/tcp_packet.h> /* for tcp_header_t */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #include <vppinfra/byte_order.h> /* for clib_net_to_host_u16 */ |
| 46 | |
| 47 | /* IP4 address which can be accessed either as 4 bytes |
| 48 | or as a 32-bit number. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 49 | typedef union |
| 50 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | u8 data[4]; |
| 52 | u32 data_u32; |
| 53 | /* Aliases. */ |
| 54 | u8 as_u8[4]; |
| 55 | u32 as_u32; |
| 56 | } ip4_address_t; |
| 57 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 58 | typedef struct |
| 59 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | /* IP address must be first for ip_interface_address_get_address() to work */ |
| 61 | ip4_address_t ip4_addr; |
| 62 | u32 fib_index; |
| 63 | } ip4_address_fib_t; |
| 64 | |
| 65 | always_inline void |
| 66 | ip4_addr_fib_init (ip4_address_fib_t * addr_fib, ip4_address_t * address, |
| 67 | u32 fib_index) |
| 68 | { |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 69 | clib_memcpy (&addr_fib->ip4_addr, address, sizeof (addr_fib->ip4_addr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | addr_fib->fib_index = fib_index; |
| 71 | } |
| 72 | |
| 73 | /* (src,dst) pair of addresses as found in packet header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 74 | typedef struct |
| 75 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | ip4_address_t src, dst; |
| 77 | } ip4_address_pair_t; |
| 78 | |
| 79 | /* If address is a valid netmask, return length of mask. */ |
| 80 | always_inline uword |
| 81 | ip4_address_netmask_length (ip4_address_t * a) |
| 82 | { |
| 83 | uword result = 0; |
| 84 | uword i; |
| 85 | for (i = 0; i < ARRAY_LEN (a->as_u8); i++) |
| 86 | { |
| 87 | switch (a->as_u8[i]) |
| 88 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 89 | case 0xff: |
| 90 | result += 8; |
| 91 | break; |
| 92 | case 0xfe: |
| 93 | result += 7; |
| 94 | goto done; |
| 95 | case 0xfc: |
| 96 | result += 6; |
| 97 | goto done; |
| 98 | case 0xf8: |
| 99 | result += 5; |
| 100 | goto done; |
| 101 | case 0xf0: |
| 102 | result += 4; |
| 103 | goto done; |
| 104 | case 0xe0: |
| 105 | result += 3; |
| 106 | goto done; |
| 107 | case 0xc0: |
| 108 | result += 2; |
| 109 | goto done; |
| 110 | case 0x80: |
| 111 | result += 1; |
| 112 | goto done; |
| 113 | case 0x00: |
| 114 | result += 0; |
| 115 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | default: |
| 117 | /* Not a valid netmask mask. */ |
| 118 | return ~0; |
| 119 | } |
| 120 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 121 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | return result; |
| 123 | } |
| 124 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 125 | typedef union |
| 126 | { |
| 127 | struct |
| 128 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | /* 4 bit packet length (in 32bit units) and version VVVVLLLL. |
| 130 | e.g. for packets w/ no options ip_version_and_header_length == 0x45. */ |
| 131 | u8 ip_version_and_header_length; |
| 132 | |
| 133 | /* Type of service. */ |
| 134 | u8 tos; |
| 135 | |
| 136 | /* Total layer 3 packet length including this header. */ |
| 137 | u16 length; |
| 138 | |
| 139 | /* Fragmentation ID. */ |
| 140 | u16 fragment_id; |
| 141 | |
| 142 | /* 3 bits of flags and 13 bits of fragment offset (in units |
| 143 | of 8 byte quantities). */ |
| 144 | u16 flags_and_fragment_offset; |
| 145 | #define IP4_HEADER_FLAG_MORE_FRAGMENTS (1 << 13) |
| 146 | #define IP4_HEADER_FLAG_DONT_FRAGMENT (1 << 14) |
| 147 | #define IP4_HEADER_FLAG_CONGESTION (1 << 15) |
| 148 | |
| 149 | /* Time to live decremented by router at each hop. */ |
| 150 | u8 ttl; |
| 151 | |
| 152 | /* Next level protocol packet. */ |
| 153 | u8 protocol; |
| 154 | |
| 155 | /* Checksum. */ |
| 156 | u16 checksum; |
| 157 | |
| 158 | /* Source and destination address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | union |
| 160 | { |
| 161 | struct |
| 162 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | ip4_address_t src_address, dst_address; |
| 164 | }; |
| 165 | ip4_address_pair_t address_pair; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 166 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /* For checksumming we'll want to access IP header in word sized chunks. */ |
| 170 | /* For 64 bit machines. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 171 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | CLIB_PACKED (struct { |
| 173 | u64 checksum_data_64[2]; |
| 174 | u32 checksum_data_64_32[1]; |
| 175 | }); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 176 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | |
| 178 | /* For 32 bit machines. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 179 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | CLIB_PACKED (struct { |
| 181 | u32 checksum_data_32[5]; |
| 182 | }); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 183 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | } ip4_header_t; |
| 185 | |
| 186 | /* Value of ip_version_and_header_length for packets w/o options. */ |
| 187 | #define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS \ |
| 188 | ((4 << 4) | (sizeof (ip4_header_t) / sizeof (u32))) |
| 189 | |
| 190 | always_inline int |
| 191 | ip4_get_fragment_offset (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 192 | { |
| 193 | return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff; |
| 194 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
| 196 | always_inline int |
| 197 | ip4_get_fragment_more (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 198 | { |
| 199 | return clib_net_to_host_u16 (i->flags_and_fragment_offset) & |
| 200 | IP4_HEADER_FLAG_MORE_FRAGMENTS; |
| 201 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
| 203 | always_inline int |
| 204 | ip4_is_fragment (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 205 | { |
| 206 | return (i->flags_and_fragment_offset & |
| 207 | clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)); |
| 208 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
| 210 | always_inline int |
| 211 | ip4_is_first_fragment (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 212 | { |
| 213 | return (i->flags_and_fragment_offset & |
| 214 | clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) == |
| 215 | clib_net_to_host_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); |
| 216 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
| 218 | /* Fragment offset in bytes. */ |
| 219 | always_inline int |
| 220 | ip4_get_fragment_offset_bytes (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 221 | { |
| 222 | return 8 * ip4_get_fragment_offset (i); |
| 223 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
| 225 | always_inline int |
| 226 | ip4_header_bytes (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 227 | { |
| 228 | return sizeof (u32) * (i->ip_version_and_header_length & 0xf); |
| 229 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | |
| 231 | always_inline void * |
| 232 | ip4_next_header (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 233 | { |
| 234 | return (void *) i + ip4_header_bytes (i); |
| 235 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
| 237 | always_inline u16 |
| 238 | ip4_header_checksum (ip4_header_t * i) |
| 239 | { |
| 240 | u16 save, csum; |
| 241 | ip_csum_t sum; |
| 242 | |
| 243 | save = i->checksum; |
| 244 | i->checksum = 0; |
| 245 | sum = ip_incremental_checksum (0, i, ip4_header_bytes (i)); |
| 246 | csum = ~ip_csum_fold (sum); |
| 247 | |
| 248 | i->checksum = save; |
| 249 | |
| 250 | /* Make checksum agree for special case where either |
| 251 | 0 or 0xffff would give same 1s complement sum. */ |
| 252 | if (csum == 0 && save == 0xffff) |
| 253 | csum = save; |
| 254 | |
| 255 | return csum; |
| 256 | } |
| 257 | |
| 258 | static inline uword |
| 259 | ip4_header_checksum_is_valid (ip4_header_t * i) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 260 | { |
| 261 | return i->checksum == ip4_header_checksum (i); |
| 262 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
| 264 | #define ip4_partial_header_checksum_x1(ip0,sum0) \ |
| 265 | do { \ |
| 266 | if (BITS (ip_csum_t) > 32) \ |
| 267 | { \ |
| 268 | sum0 = ip0->checksum_data_64[0]; \ |
| 269 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \ |
| 270 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \ |
| 271 | } \ |
| 272 | else \ |
| 273 | { \ |
| 274 | sum0 = ip0->checksum_data_32[0]; \ |
| 275 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \ |
| 276 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \ |
| 277 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \ |
| 278 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \ |
| 279 | } \ |
| 280 | } while (0) |
| 281 | |
| 282 | #define ip4_partial_header_checksum_x2(ip0,ip1,sum0,sum1) \ |
| 283 | do { \ |
| 284 | if (BITS (ip_csum_t) > 32) \ |
| 285 | { \ |
| 286 | sum0 = ip0->checksum_data_64[0]; \ |
| 287 | sum1 = ip1->checksum_data_64[0]; \ |
| 288 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64[1]); \ |
| 289 | sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64[1]); \ |
| 290 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_64_32[0]); \ |
| 291 | sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_64_32[0]); \ |
| 292 | } \ |
| 293 | else \ |
| 294 | { \ |
| 295 | sum0 = ip0->checksum_data_32[0]; \ |
| 296 | sum1 = ip1->checksum_data_32[0]; \ |
| 297 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[1]); \ |
| 298 | sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[1]); \ |
| 299 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[2]); \ |
| 300 | sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[2]); \ |
| 301 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[3]); \ |
| 302 | sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[3]); \ |
| 303 | sum0 = ip_csum_with_carry (sum0, ip0->checksum_data_32[4]); \ |
| 304 | sum1 = ip_csum_with_carry (sum1, ip1->checksum_data_32[4]); \ |
| 305 | } \ |
| 306 | } while (0) |
| 307 | |
| 308 | always_inline uword |
| 309 | ip4_address_is_multicast (ip4_address_t * a) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 310 | { |
| 311 | return (a->data[0] & 0xf0) == 0xe0; |
| 312 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | |
| 314 | always_inline void |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 315 | ip4_multicast_address_set_for_group (ip4_address_t * a, |
| 316 | ip_multicast_group_t g) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | { |
Damjan Marion | 2c29d75 | 2015-12-18 10:26:56 +0100 | [diff] [blame] | 318 | ASSERT ((u32) g < (1 << 28)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | a->as_u32 = clib_host_to_net_u32 ((0xe << 28) + g); |
| 320 | } |
| 321 | |
| 322 | always_inline void |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 323 | ip4_multicast_ethernet_address (u8 * ethernet_address, ip4_address_t * a) |
| 324 | { |
| 325 | u8 *d = a->as_u8; |
| 326 | |
| 327 | ethernet_address[0] = 0x01; |
| 328 | ethernet_address[1] = 0x00; |
| 329 | ethernet_address[2] = 0x5e; |
| 330 | ethernet_address[3] = d[1] & 0x7f; |
| 331 | ethernet_address[4] = d[2]; |
| 332 | ethernet_address[5] = d[3]; |
| 333 | } |
| 334 | |
| 335 | always_inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0) |
| 337 | { |
| 338 | u32 src0, dst0; |
| 339 | |
| 340 | src0 = ip0->src_address.data_u32; |
| 341 | dst0 = ip0->dst_address.data_u32; |
| 342 | ip0->src_address.data_u32 = dst0; |
| 343 | ip0->dst_address.data_u32 = src0; |
| 344 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | src0 = tcp0->src; |
| 346 | dst0 = tcp0->dst; |
| 347 | tcp0->src = dst0; |
| 348 | tcp0->dst = src0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | always_inline void |
| 352 | ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1, |
| 353 | tcp_header_t * tcp0, tcp_header_t * tcp1) |
| 354 | { |
| 355 | u32 src0, dst0, src1, dst1; |
| 356 | |
| 357 | src0 = ip0->src_address.data_u32; |
| 358 | src1 = ip1->src_address.data_u32; |
| 359 | dst0 = ip0->dst_address.data_u32; |
| 360 | dst1 = ip1->dst_address.data_u32; |
| 361 | ip0->src_address.data_u32 = dst0; |
| 362 | ip1->src_address.data_u32 = dst1; |
| 363 | ip0->dst_address.data_u32 = src0; |
| 364 | ip1->dst_address.data_u32 = src1; |
| 365 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 366 | src0 = tcp0->src; |
| 367 | src1 = tcp1->src; |
| 368 | dst0 = tcp0->dst; |
| 369 | dst1 = tcp1->dst; |
| 370 | tcp0->src = dst0; |
| 371 | tcp1->src = dst1; |
| 372 | tcp0->dst = src0; |
| 373 | tcp1->dst = src1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | #endif /* included_ip4_packet_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * fd.io coding-style-patch-verification: ON |
| 380 | * |
| 381 | * Local Variables: |
| 382 | * eval: (c-set-style "gnu") |
| 383 | * End: |
| 384 | */ |