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 | |
| 49 | #include <vnet/ip/format.h> |
| 50 | #include <vnet/ip/ip_packet.h> |
| 51 | #include <vnet/ip/lookup.h> |
| 52 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 53 | #include <vnet/tcp/tcp_packet.h> |
| 54 | #include <vnet/udp/udp_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | #include <vnet/ip/icmp46_packet.h> |
| 56 | |
| 57 | #include <vnet/ip/ip4.h> |
| 58 | #include <vnet/ip/ip4_error.h> |
| 59 | #include <vnet/ip/ip4_packet.h> |
Ole Troan | 92eade1 | 2016-01-13 20:17:08 +0100 | [diff] [blame] | 60 | #include <vnet/ip/icmp4.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | |
| 62 | #include <vnet/ip/ip6.h> |
| 63 | #include <vnet/ip/ip6_packet.h> |
| 64 | #include <vnet/ip/ip6_error.h> |
| 65 | #include <vnet/ip/icmp6.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | #include <vnet/classify/vnet_classify.h> |
| 67 | |
Neale Ranns | 039cbfe | 2018-02-27 03:45:38 -0800 | [diff] [blame] | 68 | #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index) |
| 69 | #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val)) |
| 70 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | /* Per protocol info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 72 | typedef struct |
| 73 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | /* Protocol name (also used as hash key). */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 75 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | |
| 77 | /* Protocol number. */ |
| 78 | ip_protocol_t protocol; |
| 79 | |
| 80 | /* Format function for this IP protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 81 | format_function_t *format_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
| 83 | /* Parser for header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 84 | unformat_function_t *unformat_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | /* Parser for per-protocol matches. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 87 | unformat_function_t *unformat_match; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
| 89 | /* Parser for packet generator edits for this protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 90 | unformat_function_t *unformat_pg_edit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | } ip_protocol_info_t; |
| 92 | |
| 93 | /* Per TCP/UDP port info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 94 | typedef struct |
| 95 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | /* Port name (used as hash key). */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 97 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | /* UDP/TCP port number in network byte order. */ |
| 100 | u16 port; |
| 101 | |
| 102 | /* Port specific format function. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 103 | format_function_t *format_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | /* Parser for packet generator edits for this protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 106 | unformat_function_t *unformat_pg_edit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | } tcp_udp_port_info_t; |
| 108 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 109 | typedef struct |
| 110 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | /* Per IP protocol info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 112 | ip_protocol_info_t *protocol_infos; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
| 114 | /* Protocol info index hashed by 8 bit IP protocol. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 115 | uword *protocol_info_by_protocol; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
| 117 | /* Hash table mapping IP protocol name (see protocols.def) |
| 118 | to protocol number. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | uword *protocol_info_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
| 121 | /* Per TCP/UDP port info. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 122 | tcp_udp_port_info_t *port_infos; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | /* Hash table from network-byte-order port to port info index. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 125 | uword *port_info_by_port; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
| 127 | /* Hash table mapping TCP/UDP name to port info index. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 128 | uword *port_info_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | } ip_main_t; |
| 130 | |
| 131 | extern ip_main_t ip_main; |
| 132 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 133 | clib_error_t *ip_main_init (vlib_main_t * vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
| 135 | static inline ip_protocol_info_t * |
| 136 | ip_get_protocol_info (ip_main_t * im, u32 protocol) |
| 137 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 138 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
| 140 | p = hash_get (im->protocol_info_by_protocol, protocol); |
| 141 | return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0; |
| 142 | } |
| 143 | |
| 144 | static inline tcp_udp_port_info_t * |
| 145 | ip_get_tcp_udp_port_info (ip_main_t * im, u32 port) |
| 146 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 147 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
| 149 | p = hash_get (im->port_info_by_port, port); |
| 150 | return p ? vec_elt_at_index (im->port_infos, p[0]) : 0; |
| 151 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 152 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | always_inline ip_csum_t |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 154 | ip_incremental_checksum_buffer (vlib_main_t * vm, |
| 155 | vlib_buffer_t * first_buffer, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | u32 first_buffer_offset, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 157 | u32 n_bytes_to_checksum, ip_csum_t sum) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | vlib_buffer_t *b = first_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | u32 n_bytes_left = n_bytes_to_checksum; |
| 161 | ASSERT (b->current_length >= first_buffer_offset); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 162 | void *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | u32 n; |
| 164 | |
| 165 | n = clib_min (n_bytes_left, b->current_length); |
| 166 | h = vlib_buffer_get_current (b) + first_buffer_offset; |
| 167 | sum = ip_incremental_checksum (sum, h, n); |
| 168 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 169 | { |
| 170 | while (1) |
| 171 | { |
| 172 | n_bytes_left -= n; |
| 173 | if (n_bytes_left == 0) |
| 174 | break; |
| 175 | b = vlib_get_buffer (vm, b->next_buffer); |
| 176 | n = clib_min (n_bytes_left, b->current_length); |
| 177 | h = vlib_buffer_get_current (b); |
| 178 | sum = ip_incremental_checksum (sum, h, n); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return sum; |
| 183 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 185 | 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] | 186 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 187 | extern vlib_node_registration_t ip4_inacl_node; |
| 188 | extern vlib_node_registration_t ip6_inacl_node; |
| 189 | |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 190 | void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api, |
| 191 | const u8 * name); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 192 | |
Neale Ranns | 6b3a8ef | 2017-09-11 10:34:33 -0700 | [diff] [blame] | 193 | 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] | 194 | |
Neale Ranns | 6b3a8ef | 2017-09-11 10:34:33 -0700 | [diff] [blame] | 195 | int ip_table_bind (fib_protocol_t fproto, u32 sw_if_index, |
| 196 | u32 table_id, u8 is_api); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 197 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 198 | u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4); |
| 199 | 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] | 200 | u8 ip4_is_local_host (ip4_address_t * ip4_address); |
| 201 | u8 ip6_is_local_host (ip6_address_t * ip6_address); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 202 | u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4); |
| 203 | u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4); |
| 204 | void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4); |
| 205 | void ip_set (ip46_address_t * dst, void *src, u8 is_ip4); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 206 | void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 207 | void ip4_address_normalize (ip4_address_t * ip4, u8 preflen); |
| 208 | void ip6_address_normalize (ip6_address_t * ip6, u8 preflen); |
| 209 | void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip); |
| 210 | u32 ip4_mask_to_preflen (ip4_address_t * mask); |
| 211 | void ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen, |
| 212 | ip4_address_t * res); |
| 213 | void ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen, |
| 214 | ip6_address_t * res); |
| 215 | void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask); |
| 216 | u32 ip6_mask_to_preflen (ip6_address_t * mask); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 217 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | #endif /* included_ip_main_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * fd.io coding-style-patch-verification: ON |
| 222 | * |
| 223 | * Local Variables: |
| 224 | * eval: (c-set-style "gnu") |
| 225 | * End: |
| 226 | */ |