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.h: ip generic (4 or 6) main |
| 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_main_h |
| 41 | #define included_ip_main_h |
| 42 | |
| 43 | #include <vppinfra/hash.h> |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 44 | #include <vppinfra/heap.h> /* adjacency heap */ |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 45 | #include <vppinfra/ptclosure.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | #include <vnet/vnet.h> |
| 48 | |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 49 | #include <vnet/ip/ip_types.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | #include <vnet/ip/format.h> |
| 51 | #include <vnet/ip/ip_packet.h> |
| 52 | #include <vnet/ip/lookup.h> |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 53 | #include <vnet/ip/ip_interface.h> |
Neale Ranns | e22a704 | 2022-08-09 03:03:29 +0000 | [diff] [blame] | 54 | #include <vnet/ip/ip.api_enum.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 56 | #include <vnet/tcp/tcp_packet.h> |
| 57 | #include <vnet/udp/udp_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | #include <vnet/ip/icmp46_packet.h> |
| 59 | |
| 60 | #include <vnet/ip/ip4.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | #include <vnet/ip/ip4_packet.h> |
Ole Troan | 92eade1 | 2016-01-13 20:17:08 +0100 | [diff] [blame] | 62 | #include <vnet/ip/icmp4.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | |
| 64 | #include <vnet/ip/ip6.h> |
| 65 | #include <vnet/ip/ip6_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | #include <vnet/ip/icmp6.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | /* Per protocol info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 69 | typedef struct |
| 70 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | /* Protocol name (also used as hash key). */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 72 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
| 74 | /* Protocol number. */ |
| 75 | ip_protocol_t protocol; |
| 76 | |
| 77 | /* Format function for this IP protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 78 | format_function_t *format_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
| 80 | /* Parser for header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 81 | unformat_function_t *unformat_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
| 83 | /* Parser for per-protocol matches. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 84 | unformat_function_t *unformat_match; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | /* Parser for packet generator edits for this protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 87 | unformat_function_t *unformat_pg_edit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | } ip_protocol_info_t; |
| 89 | |
| 90 | /* Per TCP/UDP port info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 91 | typedef struct |
| 92 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | /* Port name (used as hash key). */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 94 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
| 96 | /* UDP/TCP port number in network byte order. */ |
| 97 | u16 port; |
| 98 | |
| 99 | /* Port specific format function. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 100 | format_function_t *format_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
| 102 | /* Parser for packet generator edits for this protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 103 | unformat_function_t *unformat_pg_edit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } tcp_udp_port_info_t; |
| 105 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 106 | typedef struct |
| 107 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | /* Per IP protocol info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 109 | ip_protocol_info_t *protocol_infos; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
| 111 | /* Protocol info index hashed by 8 bit IP protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 112 | uword *protocol_info_by_protocol; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
| 114 | /* Hash table mapping IP protocol name (see protocols.def) |
| 115 | to protocol number. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 116 | uword *protocol_info_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
| 118 | /* Per TCP/UDP port info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | tcp_udp_port_info_t *port_infos; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
| 121 | /* Hash table from network-byte-order port to port info index. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 122 | uword *port_info_by_port; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | /* Hash table mapping TCP/UDP name to port info index. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 125 | uword *port_info_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | } ip_main_t; |
| 127 | |
| 128 | extern ip_main_t ip_main; |
| 129 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 130 | clib_error_t *ip_main_init (vlib_main_t * vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | |
| 132 | static inline ip_protocol_info_t * |
| 133 | ip_get_protocol_info (ip_main_t * im, u32 protocol) |
| 134 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 135 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
| 137 | p = hash_get (im->protocol_info_by_protocol, protocol); |
| 138 | return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0; |
| 139 | } |
| 140 | |
| 141 | static inline tcp_udp_port_info_t * |
| 142 | ip_get_tcp_udp_port_info (ip_main_t * im, u32 port) |
| 143 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 144 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | |
| 146 | p = hash_get (im->port_info_by_port, port); |
| 147 | return p ? vec_elt_at_index (im->port_infos, p[0]) : 0; |
| 148 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | always_inline ip_csum_t |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 151 | ip_incremental_checksum_buffer (vlib_main_t * vm, |
| 152 | vlib_buffer_t * first_buffer, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | u32 first_buffer_offset, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 154 | u32 n_bytes_to_checksum, ip_csum_t sum) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 156 | vlib_buffer_t *b = first_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | u32 n_bytes_left = n_bytes_to_checksum; |
| 158 | ASSERT (b->current_length >= first_buffer_offset); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | void *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | u32 n; |
| 161 | |
Juraj Sloboda | 8f39d55 | 2018-09-26 14:25:32 +0200 | [diff] [blame] | 162 | n = clib_min (n_bytes_left, b->current_length - first_buffer_offset); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | h = vlib_buffer_get_current (b) + first_buffer_offset; |
| 164 | sum = ip_incremental_checksum (sum, h, n); |
| 165 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 166 | { |
| 167 | while (1) |
| 168 | { |
| 169 | n_bytes_left -= n; |
| 170 | if (n_bytes_left == 0) |
| 171 | break; |
| 172 | b = vlib_get_buffer (vm, b->next_buffer); |
| 173 | n = clib_min (n_bytes_left, b->current_length); |
| 174 | h = vlib_buffer_get_current (b); |
| 175 | sum = ip_incremental_checksum (sum, h, n); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return sum; |
| 180 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | |
Srikanth A | 02833ff | 2019-10-02 17:48:58 -0700 | [diff] [blame] | 182 | always_inline u16 |
| 183 | ip_calculate_l4_checksum (vlib_main_t * vm, vlib_buffer_t * p0, |
| 184 | ip_csum_t sum0, u32 payload_length, |
| 185 | u8 * iph, u32 ip_header_size, u8 * l4h) |
| 186 | { |
| 187 | u16 sum16; |
| 188 | u8 *data_this_buffer, length_odd; |
| 189 | u32 n_bytes_left, n_this_buffer, n_ip_bytes_this_buffer; |
| 190 | |
| 191 | n_bytes_left = payload_length; |
| 192 | |
| 193 | if (l4h) /* packet l4 header and no buffer chain involved */ |
| 194 | { |
| 195 | ASSERT (p0 == NULL); |
| 196 | n_this_buffer = payload_length; |
| 197 | data_this_buffer = l4h; |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | ASSERT (p0); |
| 202 | if (iph) /* ip header pointer set to packet in buffer */ |
| 203 | { |
| 204 | ASSERT (ip_header_size); |
| 205 | n_this_buffer = payload_length; |
| 206 | data_this_buffer = iph + ip_header_size; /* at l4 header */ |
| 207 | n_ip_bytes_this_buffer = |
| 208 | p0->current_length - (((u8 *) iph - p0->data) - p0->current_data); |
| 209 | if (PREDICT_FALSE (payload_length + ip_header_size > |
| 210 | n_ip_bytes_this_buffer)) |
| 211 | { |
| 212 | n_this_buffer = n_ip_bytes_this_buffer - ip_header_size; |
| 213 | if (PREDICT_FALSE (n_this_buffer >> 31)) |
| 214 | { /* error - ip header don't fit this buffer */ |
Srikanth A | 02833ff | 2019-10-02 17:48:58 -0700 | [diff] [blame] | 215 | return 0xfefe; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | else /* packet in buffer with no ip header */ |
| 220 | { /* buffer current pointer at l4 header */ |
| 221 | n_this_buffer = p0->current_length; |
| 222 | data_this_buffer = vlib_buffer_get_current (p0); |
| 223 | } |
| 224 | n_this_buffer = clib_min (n_this_buffer, n_bytes_left); |
| 225 | } |
| 226 | |
| 227 | while (1) |
| 228 | { |
| 229 | sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer); |
| 230 | n_bytes_left -= n_this_buffer; |
| 231 | if (n_bytes_left == 0) |
| 232 | break; |
| 233 | |
| 234 | if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 235 | { |
Srikanth A | 02833ff | 2019-10-02 17:48:58 -0700 | [diff] [blame] | 236 | return 0xfefe; |
| 237 | } |
| 238 | |
| 239 | length_odd = (n_this_buffer & 1); |
| 240 | |
| 241 | p0 = vlib_get_buffer (vm, p0->next_buffer); |
| 242 | data_this_buffer = vlib_buffer_get_current (p0); |
| 243 | n_this_buffer = clib_min (p0->current_length, n_bytes_left); |
| 244 | |
| 245 | if (PREDICT_FALSE (length_odd)) |
| 246 | { |
| 247 | /* Prepend a 0 byte to maintain 2-byte checksum alignment */ |
| 248 | data_this_buffer--; |
| 249 | n_this_buffer++; |
| 250 | n_bytes_left++; |
| 251 | data_this_buffer[0] = 0; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | sum16 = ~ip_csum_fold (sum0); |
| 256 | return sum16; |
| 257 | } |
| 258 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 259 | void ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 261 | extern vlib_node_registration_t ip4_inacl_node; |
| 262 | extern vlib_node_registration_t ip6_inacl_node; |
| 263 | |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 264 | void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api, |
| 265 | const u8 * name); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 266 | |
Neale Ranns | 6b3a8ef | 2017-09-11 10:34:33 -0700 | [diff] [blame] | 267 | void ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 268 | |
Nathan Skrzypczak | a424dd1 | 2021-08-20 15:53:43 +0200 | [diff] [blame] | 269 | void fib_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 fib_index); |
| 270 | void mfib_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 mfib_index); |
Nathan Skrzypczak | 275bd79 | 2021-09-17 17:29:14 +0200 | [diff] [blame] | 271 | int ip_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 table_id); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 272 | |
Aloys Augustin | 6e4cfb5 | 2021-09-16 20:53:14 +0200 | [diff] [blame] | 273 | u32 ip_table_get_unused_id (fib_protocol_t fproto); |
| 274 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 275 | u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4); |
| 276 | u8 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4); |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 277 | u8 ip4_is_local_host (ip4_address_t * ip4_address); |
| 278 | u8 ip6_is_local_host (ip6_address_t * ip6_address); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 279 | u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 280 | void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4); |
| 281 | void ip_set (ip46_address_t * dst, void *src, u8 is_ip4); |
| 282 | |
Neale Ranns | 6dc0c8d | 2020-11-26 14:15:33 +0000 | [diff] [blame] | 283 | void ip_feature_enable_disable (ip_address_family_t af, |
| 284 | ip_sub_address_family_t safi, |
| 285 | ip_feature_location_t loc, |
| 286 | const char *feature, |
| 287 | u32 sw_if_index, int enable_disable, |
| 288 | void *feature_config, |
| 289 | u32 n_feature_config_bytes); |
| 290 | |
Neale Ranns | b28652e | 2021-10-25 09:47:09 +0000 | [diff] [blame] | 291 | ethernet_type_t ip_address_family_to_ether_type (ip_address_family_t af); |
| 292 | |
Nick Zavaritsky | 27518c2 | 2020-02-27 15:54:58 +0000 | [diff] [blame] | 293 | always_inline u32 vlib_buffer_get_ip4_fib_index (vlib_buffer_t * b); |
| 294 | always_inline u32 vlib_buffer_get_ip6_fib_index (vlib_buffer_t * b); |
| 295 | always_inline u32 |
| 296 | vlib_buffer_get_ip_fib_index (vlib_buffer_t * b, u8 is_ip4) |
| 297 | { |
| 298 | return (is_ip4 ? vlib_buffer_get_ip4_fib_index |
| 299 | : vlib_buffer_get_ip6_fib_index) (b); |
| 300 | } |
| 301 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | #endif /* included_ip_main_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * fd.io coding-style-patch-verification: ON |
| 306 | * |
| 307 | * Local Variables: |
| 308 | * eval: (c-set-style "gnu") |
| 309 | * End: |
| 310 | */ |