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/ip_packet.h: packet format common between ip4 & ip6 |
| 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_ip_packet_h |
| 41 | #define included_ip_packet_h |
| 42 | |
| 43 | #include <vppinfra/byte_order.h> |
| 44 | #include <vppinfra/error.h> |
| 45 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 46 | typedef enum ip_protocol |
| 47 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | #define ip_protocol(n,s) IP_PROTOCOL_##s = n, |
| 49 | #include "protocols.def" |
| 50 | #undef ip_protocol |
| 51 | } ip_protocol_t; |
| 52 | |
| 53 | /* TCP/UDP ports. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 54 | typedef enum |
| 55 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | #define ip_port(s,n) IP_PORT_##s = n, |
| 57 | #include "ports.def" |
| 58 | #undef ip_port |
| 59 | } ip_port_t; |
| 60 | |
Kevin Paul Herbert | 8f9e7d4 | 2016-01-26 18:32:24 -0800 | [diff] [blame] | 61 | /* Classifies protocols into UDP, ICMP or other. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 62 | typedef enum |
| 63 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | IP_BUILTIN_PROTOCOL_UDP, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | IP_BUILTIN_PROTOCOL_ICMP, |
| 66 | IP_BUILTIN_PROTOCOL_UNKNOWN, |
| 67 | } ip_builtin_protocol_t; |
| 68 | |
| 69 | #define foreach_ip_builtin_multicast_group \ |
| 70 | _ (1, all_hosts_on_subnet) \ |
| 71 | _ (2, all_routers_on_subnet) \ |
| 72 | _ (4, dvmrp) \ |
| 73 | _ (5, ospf_all_routers) \ |
| 74 | _ (6, ospf_designated_routers) \ |
| 75 | _ (13, pim) \ |
| 76 | _ (18, vrrp) \ |
| 77 | _ (102, hsrp) \ |
| 78 | _ (22, igmp_v3) |
| 79 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 80 | typedef enum |
| 81 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | #define _(n,f) IP_MULTICAST_GROUP_##f = n, |
| 83 | foreach_ip_builtin_multicast_group |
| 84 | #undef _ |
| 85 | } ip_multicast_group_t; |
| 86 | |
| 87 | /* IP checksum support. */ |
| 88 | |
| 89 | /* Incremental checksum update. */ |
| 90 | typedef uword ip_csum_t; |
| 91 | |
| 92 | always_inline ip_csum_t |
| 93 | ip_csum_with_carry (ip_csum_t sum, ip_csum_t x) |
| 94 | { |
| 95 | ip_csum_t t = sum + x; |
| 96 | return t + (t < x); |
| 97 | } |
| 98 | |
| 99 | /* Update checksum changing field at even byte offset from x -> 0. */ |
| 100 | always_inline ip_csum_t |
| 101 | ip_csum_add_even (ip_csum_t c, ip_csum_t x) |
| 102 | { |
| 103 | ip_csum_t d; |
| 104 | |
| 105 | d = c - x; |
| 106 | |
| 107 | /* Fold in carry from high bit. */ |
| 108 | d -= d > c; |
| 109 | |
| 110 | ASSERT (ip_csum_with_carry (d, x) == c); |
| 111 | |
| 112 | return d; |
| 113 | } |
| 114 | |
| 115 | /* Update checksum changing field at even byte offset from 0 -> x. */ |
| 116 | always_inline ip_csum_t |
| 117 | ip_csum_sub_even (ip_csum_t c, ip_csum_t x) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 118 | { |
| 119 | return ip_csum_with_carry (c, x); |
| 120 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
| 122 | always_inline ip_csum_t |
| 123 | ip_csum_update_inline (ip_csum_t sum, ip_csum_t old, ip_csum_t new, |
| 124 | u32 field_byte_offset, u32 field_n_bytes) |
| 125 | { |
| 126 | /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian |
| 127 | we need to shift byte into place for checksum. */ |
| 128 | if ((field_n_bytes % 2) |
| 129 | && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN) |
| 130 | { |
| 131 | old = old << 8; |
| 132 | new = new << 8; |
| 133 | } |
| 134 | sum = ip_csum_sub_even (sum, old); |
| 135 | sum = ip_csum_add_even (sum, new); |
| 136 | return sum; |
| 137 | } |
| 138 | |
| 139 | #define ip_csum_update(sum,old,new,type,field) \ |
| 140 | ip_csum_update_inline ((sum), (old), (new), \ |
| 141 | STRUCT_OFFSET_OF (type, field), \ |
| 142 | STRUCT_SIZE_OF (type, field)) |
| 143 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 144 | always_inline u16 |
| 145 | ip_csum_fold (ip_csum_t c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | { |
| 147 | /* Reduce to 16 bits. */ |
| 148 | #if uword_bits == 64 |
| 149 | c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32); |
| 150 | c = (c & 0xffff) + (c >> 16); |
| 151 | #endif |
| 152 | |
| 153 | c = (c & 0xffff) + (c >> 16); |
| 154 | c = (c & 0xffff) + (c >> 16); |
| 155 | |
| 156 | return c; |
| 157 | } |
| 158 | |
| 159 | /* Copy data and checksum at the same time. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 160 | ip_csum_t ip_csum_and_memcpy (ip_csum_t sum, void *dst, void *src, |
| 161 | uword n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
| 163 | always_inline u16 |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 164 | ip_csum_and_memcpy_fold (ip_csum_t sum, void *dst) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | return ip_csum_fold (sum); |
| 167 | } |
| 168 | |
| 169 | /* Checksum routine. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 170 | ip_csum_t ip_incremental_checksum (ip_csum_t sum, void *data, uword n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
| 172 | #endif /* included_ip_packet_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * fd.io coding-style-patch-verification: ON |
| 176 | * |
| 177 | * Local Variables: |
| 178 | * eval: (c-set-style "gnu") |
| 179 | * End: |
| 180 | */ |