Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ipsec_tun.h : IPSEC tunnel protection |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/ipsec/ipsec_tun.h> |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 19 | #include <vnet/ipsec/ipsec_itf.h> |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 20 | #include <vnet/ipsec/esp.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 21 | #include <vnet/udp/udp_local.h> |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 22 | #include <vnet/adj/adj_delegate.h> |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 23 | #include <vnet/adj/adj_midchain.h> |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 24 | #include <vnet/teib/teib.h> |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 25 | #include <vnet/mpls/mpls.h> |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 26 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 27 | /* instantiate the bihash functions */ |
| 28 | #include <vppinfra/bihash_8_16.h> |
| 29 | #include <vppinfra/bihash_template.c> |
| 30 | #include <vppinfra/bihash_24_16.h> |
| 31 | #include <vppinfra/bihash_template.c> |
| 32 | |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 33 | #define IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS (64 * 1024) |
| 34 | #define IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE 512 << 20 |
| 35 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 36 | /** |
| 37 | * The logger |
| 38 | */ |
| 39 | vlib_log_class_t ipsec_tun_protect_logger; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Pool of tunnel protection objects |
| 43 | */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 44 | ipsec_tun_protect_t *ipsec_tun_protect_pool; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 47 | * Adj delegate registered type |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 48 | */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 49 | static adj_delegate_type_t ipsec_tun_adj_delegate_type; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 50 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Adj index to TX SA mapping |
| 53 | */ |
| 54 | index_t *ipsec_tun_protect_sa_by_adj_index; |
| 55 | |
| 56 | const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S; |
| 57 | |
| 58 | /** |
| 59 | * The DB of all added per-nh tunnel protectiond |
| 60 | */ |
| 61 | typedef struct ipsec_tun_protect_itf_db_t_ |
| 62 | { |
| 63 | /** A hash table key'd on IP (4 or 6) address */ |
| 64 | uword *id_hash; |
| 65 | /** If the interface is P2P then there is only one protect |
| 66 | * object associated with the auto-adj for each NH proto */ |
| 67 | index_t id_itp; |
| 68 | } ipsec_tun_protect_itf_db_t; |
| 69 | |
| 70 | typedef struct ipsec_tun_protect_db_t_ |
| 71 | { |
| 72 | /** Per-interface vector */ |
| 73 | ipsec_tun_protect_itf_db_t *id_itf; |
| 74 | } ipsec_tun_protect_db_t; |
| 75 | |
| 76 | static ipsec_tun_protect_db_t itp_db; |
| 77 | |
| 78 | const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = { |
| 79 | .id_itp = INDEX_INVALID, |
| 80 | }; |
| 81 | |
| 82 | #define ITP_DBG(_itp, _fmt, _args...) \ |
| 83 | { \ |
| 84 | vlib_log_debug(ipsec_tun_protect_logger, \ |
| 85 | "[%U]: " _fmt, \ |
| 86 | format_ipsec_tun_protect, \ |
| 87 | _itp, ##_args); \ |
| 88 | } |
| 89 | |
| 90 | #define ITP_DBG2(_fmt, _args...) \ |
| 91 | { \ |
| 92 | vlib_log_debug(ipsec_tun_protect_logger, \ |
| 93 | _fmt, ##_args); \ |
| 94 | } |
| 95 | |
Neale Ranns | 8d6d74c | 2020-02-20 09:45:16 +0000 | [diff] [blame] | 96 | static u32 ipsec_tun_node_regs[N_AF]; |
| 97 | |
| 98 | void |
| 99 | ipsec_tun_register_nodes (ip_address_family_t af) |
| 100 | { |
| 101 | if (0 == ipsec_tun_node_regs[af]++) |
| 102 | { |
| 103 | if (AF_IP4 == af) |
| 104 | { |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 105 | ipsec_register_udp_port (UDP_DST_PORT_ipsec); |
Neale Ranns | 8d6d74c | 2020-02-20 09:45:16 +0000 | [diff] [blame] | 106 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 107 | ipsec4_tun_input_node.index); |
| 108 | } |
| 109 | else |
| 110 | ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 111 | ipsec6_tun_input_node.index); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | ipsec_tun_unregister_nodes (ip_address_family_t af) |
| 117 | { |
| 118 | ASSERT (0 != ipsec_tun_node_regs[af]); |
| 119 | if (0 == --ipsec_tun_node_regs[af]) |
| 120 | { |
| 121 | if (AF_IP4 == af) |
| 122 | { |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 123 | ipsec_unregister_udp_port (UDP_DST_PORT_ipsec); |
Neale Ranns | 8d6d74c | 2020-02-20 09:45:16 +0000 | [diff] [blame] | 124 | ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP); |
| 125 | } |
| 126 | else |
| 127 | ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP); |
| 128 | } |
| 129 | } |
| 130 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 131 | static inline const ipsec_tun_protect_t * |
| 132 | ipsec_tun_protect_from_const_base (const adj_delegate_t * ad) |
| 133 | { |
| 134 | if (ad == NULL) |
| 135 | return (NULL); |
| 136 | return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index)); |
| 137 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 138 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 139 | static u32 |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 140 | ipsec_tun_protect_get_adj_next (vnet_link_t linkt, |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 141 | const ipsec_tun_protect_t *itp) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 142 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 143 | ipsec_main_t *im; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 144 | u32 next; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 145 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 146 | im = &ipsec_main; |
| 147 | next = 0; |
| 148 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 149 | if (!(itp->itp_flags & IPSEC_PROTECT_ITF)) |
| 150 | { |
| 151 | if (ip46_address_is_ip4 (&itp->itp_tun.src)) |
| 152 | linkt = VNET_LINK_IP4; |
| 153 | else |
| 154 | linkt = VNET_LINK_IP6; |
| 155 | } |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 156 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 157 | switch (linkt) |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 158 | { |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 159 | case VNET_LINK_IP4: |
| 160 | next = im->esp4_encrypt_tun_node_index; |
| 161 | break; |
| 162 | case VNET_LINK_IP6: |
| 163 | next = im->esp6_encrypt_tun_node_index; |
| 164 | break; |
| 165 | case VNET_LINK_MPLS: |
| 166 | next = im->esp_mpls_encrypt_tun_node_index; |
| 167 | break; |
| 168 | case VNET_LINK_ARP: |
| 169 | case VNET_LINK_NSH: |
| 170 | case VNET_LINK_ETHERNET: |
| 171 | ASSERT (0); |
| 172 | break; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 173 | } |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 174 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 175 | return (next); |
| 176 | } |
| 177 | |
| 178 | static void |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 179 | ipsec_tun_reset_tx_nodes (u32 sw_if_index) |
| 180 | { |
| 181 | vnet_reset_interface_l3_output_node (vlib_get_main (), sw_if_index); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | ipsec_tun_setup_tx_nodes (u32 sw_if_index, const ipsec_tun_protect_t *itp) |
| 186 | { |
| 187 | vnet_feature_modify_end_node ( |
| 188 | ip4_main.lookup_main.output_feature_arc_index, sw_if_index, |
| 189 | ipsec_tun_protect_get_adj_next (VNET_LINK_IP4, itp)); |
| 190 | vnet_feature_modify_end_node ( |
| 191 | ip6_main.lookup_main.output_feature_arc_index, sw_if_index, |
| 192 | ipsec_tun_protect_get_adj_next (VNET_LINK_IP6, itp)); |
| 193 | vnet_feature_modify_end_node ( |
| 194 | mpls_main.output_feature_arc_index, sw_if_index, |
| 195 | ipsec_tun_protect_get_adj_next (VNET_LINK_MPLS, itp)); |
| 196 | } |
| 197 | |
| 198 | static void |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 199 | ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp) |
| 200 | { |
| 201 | vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai, |
| 202 | INDEX_INVALID); |
| 203 | |
| 204 | if (NULL == itp) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 205 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 206 | ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID; |
| 207 | adj_nbr_midchain_reset_next_node (ai); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 208 | } |
| 209 | else |
| 210 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 211 | ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa; |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 212 | adj_nbr_midchain_update_next_node ( |
| 213 | ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp)); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 214 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 217 | static index_t |
| 218 | ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh) |
| 219 | { |
| 220 | ipsec_tun_protect_itf_db_t *idi; |
| 221 | uword *p; |
| 222 | |
| 223 | if (vec_len (itp_db.id_itf) <= sw_if_index) |
| 224 | return INDEX_INVALID; |
| 225 | |
| 226 | if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index)) |
| 227 | return (itp_db.id_itf[sw_if_index].id_itp); |
| 228 | |
| 229 | idi = &itp_db.id_itf[sw_if_index]; |
| 230 | p = hash_get_mem (idi->id_hash, nh); |
| 231 | |
| 232 | if (NULL == p) |
| 233 | { |
| 234 | return INDEX_INVALID; |
| 235 | } |
| 236 | return (p[0]); |
| 237 | } |
| 238 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 239 | static void |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 240 | ipsec_tun_protect_rx_db_add (ipsec_main_t * im, |
| 241 | const ipsec_tun_protect_t * itp) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 242 | { |
| 243 | const ipsec_sa_t *sa; |
| 244 | u32 sai; |
| 245 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 246 | if (ip46_address_is_zero (&itp->itp_crypto.dst)) |
| 247 | return; |
| 248 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 249 | /* *INDENT-OFF* */ |
| 250 | FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai, |
| 251 | ({ |
| 252 | sa = ipsec_sa_get (sai); |
| 253 | |
| 254 | ipsec_tun_lkup_result_t res = { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 255 | .tun_index = itp - ipsec_tun_protect_pool, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 256 | .sa_index = sai, |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 257 | .flags = itp->itp_flags, |
| 258 | .sw_if_index = itp->itp_sw_if_index, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | /* |
| 262 | * The key is formed from the tunnel's destination |
| 263 | * as the packet lookup is done from the packet's source |
| 264 | */ |
| 265 | if (ip46_address_is_ip4 (&itp->itp_crypto.dst)) |
| 266 | { |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 267 | ipsec4_tunnel_kv_t key = { |
| 268 | .value = res, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 269 | }; |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 270 | clib_bihash_kv_8_16_t *bkey = (clib_bihash_kv_8_16_t*)&key; |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 271 | |
| 272 | ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4, |
| 273 | clib_host_to_net_u32 (sa->spi)); |
| 274 | |
Neale Ranns | 0ebbbfc | 2021-02-08 09:53:10 +0000 | [diff] [blame] | 275 | if (!clib_bihash_is_initialised_8_16 (&im->tun4_protect_by_key)) |
| 276 | clib_bihash_init_8_16 (&im->tun4_protect_by_key, |
| 277 | "IPSec IPv4 tunnels", |
| 278 | IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS, |
| 279 | IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE); |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 280 | |
Neale Ranns | 0ebbbfc | 2021-02-08 09:53:10 +0000 | [diff] [blame] | 281 | clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, bkey, 1); |
| 282 | ipsec_tun_register_nodes (AF_IP4); |
| 283 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 284 | else |
| 285 | { |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 286 | ipsec6_tunnel_kv_t key = { |
| 287 | .key = { |
| 288 | .remote_ip = itp->itp_crypto.dst.ip6, |
| 289 | .spi = clib_host_to_net_u32 (sa->spi), |
| 290 | }, |
| 291 | .value = res, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 292 | }; |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 293 | clib_bihash_kv_24_16_t *bkey = (clib_bihash_kv_24_16_t*)&key; |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 294 | |
Neale Ranns | 0ebbbfc | 2021-02-08 09:53:10 +0000 | [diff] [blame] | 295 | if (!clib_bihash_is_initialised_24_16 (&im->tun6_protect_by_key)) |
| 296 | clib_bihash_init_24_16 (&im->tun6_protect_by_key, |
| 297 | "IPSec IPv6 tunnels", |
| 298 | IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS, |
| 299 | IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE); |
| 300 | clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, bkey, 1); |
| 301 | ipsec_tun_register_nodes (AF_IP6); |
| 302 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 303 | })) |
| 304 | /* *INDENT-ON* */ |
| 305 | } |
| 306 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 307 | static adj_walk_rc_t |
| 308 | ipsec_tun_protect_adj_add (adj_index_t ai, void *arg) |
| 309 | { |
| 310 | ipsec_tun_protect_t *itp = arg; |
| 311 | adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type, |
| 312 | itp - ipsec_tun_protect_pool); |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 313 | ipsec_tun_protect_add_adj (ai, itp); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 314 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 315 | if (itp->itp_flags & IPSEC_PROTECT_ITF) |
| 316 | ipsec_itf_adj_stack (ai, itp->itp_out_sa); |
| 317 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 318 | return (ADJ_WALK_RC_CONTINUE); |
| 319 | } |
| 320 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 321 | static void |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 322 | ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp) |
| 323 | { |
| 324 | /* |
| 325 | * add the delegate to the adj |
| 326 | */ |
| 327 | ipsec_tun_protect_itf_db_t *idi; |
| 328 | fib_protocol_t nh_proto; |
| 329 | ip46_address_t nh; |
| 330 | |
| 331 | vec_validate_init_empty (itp_db.id_itf, |
| 332 | itp->itp_sw_if_index, |
| 333 | IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY); |
| 334 | |
| 335 | idi = &itp_db.id_itf[itp->itp_sw_if_index]; |
| 336 | |
| 337 | if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index)) |
| 338 | { |
| 339 | if (INDEX_INVALID == idi->id_itp) |
| 340 | { |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 341 | ipsec_tun_setup_tx_nodes (itp->itp_sw_if_index, itp); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 342 | } |
| 343 | idi->id_itp = itp - ipsec_tun_protect_pool; |
| 344 | |
| 345 | FOR_EACH_FIB_IP_PROTOCOL (nh_proto) |
| 346 | adj_nbr_walk (itp->itp_sw_if_index, |
| 347 | nh_proto, ipsec_tun_protect_adj_add, itp); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | if (NULL == idi->id_hash) |
| 352 | { |
| 353 | idi->id_hash = |
| 354 | hash_create_mem (0, sizeof (ip_address_t), sizeof (uword)); |
| 355 | /* |
| 356 | * enable the encrypt feature for egress if this is the first addition |
| 357 | * on this interface |
| 358 | */ |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 359 | ipsec_tun_setup_tx_nodes (itp->itp_sw_if_index, itp); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool); |
| 363 | |
| 364 | /* |
| 365 | * walk all the adjs with the same nh on this interface |
| 366 | * to associate them with this protection |
| 367 | */ |
| 368 | nh_proto = ip_address_to_46 (itp->itp_key, &nh); |
| 369 | |
| 370 | adj_nbr_walk_nh (itp->itp_sw_if_index, |
| 371 | nh_proto, &nh, ipsec_tun_protect_adj_add, itp); |
Neale Ranns | 8d6d74c | 2020-02-20 09:45:16 +0000 | [diff] [blame] | 372 | |
| 373 | ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ? |
| 374 | AF_IP6 : AF_IP4); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | ipsec_tun_protect_rx_db_remove (ipsec_main_t * im, |
| 380 | const ipsec_tun_protect_t * itp) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 381 | { |
| 382 | const ipsec_sa_t *sa; |
| 383 | |
| 384 | /* *INDENT-OFF* */ |
| 385 | FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa, |
| 386 | ({ |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 387 | if (ip46_address_is_ip4 (&itp->itp_crypto.dst)) |
| 388 | { |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 389 | ipsec4_tunnel_kv_t key; |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 390 | clib_bihash_kv_8_16_t res, *bkey = (clib_bihash_kv_8_16_t*)&key; |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 391 | |
| 392 | ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4, |
| 393 | clib_host_to_net_u32 (sa->spi)); |
| 394 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 395 | if (!clib_bihash_search_8_16 (&im->tun4_protect_by_key, bkey, &res)) |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 396 | { |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 397 | clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, bkey, 0); |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 398 | ipsec_tun_unregister_nodes(AF_IP4); |
| 399 | } |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 400 | } |
| 401 | else |
| 402 | { |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 403 | ipsec6_tunnel_kv_t key = { |
| 404 | .key = { |
| 405 | .remote_ip = itp->itp_crypto.dst.ip6, |
| 406 | .spi = clib_host_to_net_u32 (sa->spi), |
| 407 | }, |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 408 | }; |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 409 | clib_bihash_kv_24_16_t res, *bkey = (clib_bihash_kv_24_16_t*)&key; |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 410 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 411 | if (!clib_bihash_search_24_16 (&im->tun6_protect_by_key, bkey, &res)) |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 412 | { |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 413 | clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, bkey, 0); |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 414 | ipsec_tun_unregister_nodes(AF_IP6); |
| 415 | } |
| 416 | } |
| 417 | })); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 418 | /* *INDENT-ON* */ |
| 419 | } |
| 420 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 421 | static adj_walk_rc_t |
| 422 | ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg) |
| 423 | { |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 424 | ipsec_tun_protect_t *itp = arg; |
| 425 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 426 | adj_delegate_remove (ai, ipsec_tun_adj_delegate_type); |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 427 | ipsec_tun_protect_add_adj (ai, NULL); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 428 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 429 | if (itp->itp_flags & IPSEC_PROTECT_ITF) |
| 430 | ipsec_itf_adj_unstack (ai); |
| 431 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 432 | return (ADJ_WALK_RC_CONTINUE); |
| 433 | } |
| 434 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 435 | static void |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 436 | ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp) |
| 437 | { |
| 438 | ipsec_tun_protect_itf_db_t *idi; |
| 439 | fib_protocol_t nh_proto; |
| 440 | ip46_address_t nh; |
| 441 | |
| 442 | nh_proto = ip_address_to_46 (itp->itp_key, &nh); |
| 443 | idi = &itp_db.id_itf[itp->itp_sw_if_index]; |
| 444 | |
| 445 | if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index)) |
| 446 | { |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 447 | ipsec_tun_reset_tx_nodes (itp->itp_sw_if_index); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 448 | idi->id_itp = INDEX_INVALID; |
| 449 | |
| 450 | FOR_EACH_FIB_IP_PROTOCOL (nh_proto) |
| 451 | adj_nbr_walk (itp->itp_sw_if_index, |
| 452 | nh_proto, ipsec_tun_protect_adj_remove, itp); |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | adj_nbr_walk_nh (itp->itp_sw_if_index, |
| 457 | nh_proto, &nh, ipsec_tun_protect_adj_remove, itp); |
| 458 | |
| 459 | hash_unset_mem (idi->id_hash, itp->itp_key); |
| 460 | |
| 461 | if (0 == hash_elts (idi->id_hash)) |
| 462 | { |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 463 | ipsec_tun_reset_tx_nodes (itp->itp_sw_if_index); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 464 | hash_free (idi->id_hash); |
| 465 | idi->id_hash = NULL; |
| 466 | } |
Neale Ranns | 8d6d74c | 2020-02-20 09:45:16 +0000 | [diff] [blame] | 467 | ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ? |
| 468 | AF_IP6 : AF_IP4); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | static void |
| 473 | ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 474 | { |
| 475 | ipsec_sa_t *sa; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 476 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 477 | /* *INDENT-OFF* */ |
| 478 | FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa, |
| 479 | ({ |
| 480 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
| 481 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 482 | itp->itp_crypto.src = ip_addr_46 (&sa->tunnel.t_dst); |
| 483 | itp->itp_crypto.dst = ip_addr_46 (&sa->tunnel.t_src); |
| 484 | if (!(itp->itp_flags & IPSEC_PROTECT_ITF)) |
| 485 | { |
| 486 | ipsec_sa_set_IS_PROTECT (sa); |
| 487 | itp->itp_flags |= IPSEC_PROTECT_ENCAPED; |
| 488 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | { |
| 492 | itp->itp_crypto.src = itp->itp_tun.src; |
| 493 | itp->itp_crypto.dst = itp->itp_tun.dst; |
| 494 | itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED; |
| 495 | } |
| 496 | })); |
| 497 | /* *INDENT-ON* */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void |
| 501 | ipsec_tun_protect_config (ipsec_main_t * im, |
| 502 | ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in) |
| 503 | { |
| 504 | index_t sai; |
| 505 | u32 ii; |
| 506 | |
| 507 | itp->itp_n_sa_in = vec_len (sas_in); |
| 508 | for (ii = 0; ii < itp->itp_n_sa_in; ii++) |
| 509 | itp->itp_in_sas[ii] = sas_in[ii]; |
| 510 | itp->itp_out_sa = sa_out; |
| 511 | |
| 512 | ipsec_sa_lock (itp->itp_out_sa); |
| 513 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 514 | if (itp->itp_flags & IPSEC_PROTECT_ITF) |
| 515 | ipsec_sa_set_NO_ALGO_NO_DROP (ipsec_sa_get (itp->itp_out_sa)); |
| 516 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 517 | /* *INDENT-OFF* */ |
| 518 | FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai, |
| 519 | ({ |
| 520 | ipsec_sa_lock(sai); |
| 521 | })); |
| 522 | ipsec_tun_protect_set_crypto_addr(itp); |
| 523 | /* *INDENT-ON* */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | * add to the DB against each SA |
| 527 | */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 528 | ipsec_tun_protect_rx_db_add (im, itp); |
| 529 | ipsec_tun_protect_tx_db_add (itp); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 530 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 531 | ITP_DBG (itp, "configured"); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static void |
| 535 | ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp) |
| 536 | { |
| 537 | ipsec_sa_t *sa; |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 538 | index_t sai; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 539 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 540 | /* *INDENT-OFF* */ |
| 541 | FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa, |
| 542 | ({ |
| 543 | ipsec_sa_unset_IS_PROTECT (sa); |
| 544 | })); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 545 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 546 | ipsec_tun_protect_rx_db_remove (im, itp); |
| 547 | ipsec_tun_protect_tx_db_remove (itp); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 548 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 549 | ipsec_sa_unset_NO_ALGO_NO_DROP (ipsec_sa_get (itp->itp_out_sa)); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 550 | ipsec_sa_unlock(itp->itp_out_sa); |
| 551 | |
| 552 | FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai, |
| 553 | ({ |
| 554 | ipsec_sa_unlock(sai); |
| 555 | })); |
| 556 | /* *INDENT-ON* */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 557 | ITP_DBG (itp, "unconfigured"); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 560 | static void |
| 561 | ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp, |
| 562 | const teib_entry_t * ne) |
| 563 | { |
| 564 | if (NULL != ne) |
| 565 | { |
| 566 | const fib_prefix_t *pfx; |
| 567 | |
| 568 | pfx = teib_entry_get_nh (ne); |
| 569 | |
| 570 | ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr); |
| 571 | } |
| 572 | else |
| 573 | ip46_address_reset (&itp->itp_tun.dst); |
| 574 | } |
| 575 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 576 | int |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 577 | ipsec_tun_protect_update (u32 sw_if_index, |
| 578 | const ip_address_t * nh, u32 sa_out, u32 * sas_in) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 579 | { |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 580 | ipsec_tun_protect_t *itp; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 581 | u32 itpi, ii, *saip; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 582 | ipsec_main_t *im; |
| 583 | int rv; |
| 584 | |
Benoît Ganne | ac55a72 | 2021-10-07 17:37:07 +0200 | [diff] [blame] | 585 | if (NULL == nh) |
| 586 | nh = &IP_ADDR_ALL_0; |
| 587 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 588 | ITP_DBG2 ("update: %U/%U", |
| 589 | format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index, |
| 590 | format_ip_address, nh); |
| 591 | |
Matthew Smith | dc3e966 | 2020-04-10 20:27:33 -0500 | [diff] [blame] | 592 | if (vec_len (sas_in) > ITP_MAX_N_SA_IN) |
| 593 | { |
| 594 | rv = VNET_API_ERROR_LIMIT_EXCEEDED; |
| 595 | goto out; |
| 596 | } |
| 597 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 598 | rv = 0; |
| 599 | im = &ipsec_main; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 600 | itpi = ipsec_tun_protect_find (sw_if_index, nh); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 601 | |
| 602 | vec_foreach_index (ii, sas_in) |
| 603 | { |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 604 | sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 605 | if (~0 == sas_in[ii]) |
| 606 | { |
| 607 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 608 | goto out; |
| 609 | } |
| 610 | } |
| 611 | |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 612 | sa_out = ipsec_sa_find_and_lock (sa_out); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 613 | |
| 614 | if (~0 == sa_out) |
| 615 | { |
| 616 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 617 | goto out; |
| 618 | } |
| 619 | |
| 620 | if (INDEX_INVALID == itpi) |
| 621 | { |
| 622 | vnet_device_class_t *dev_class; |
| 623 | vnet_hw_interface_t *hi; |
| 624 | vnet_main_t *vnm; |
| 625 | u8 is_l2; |
| 626 | |
| 627 | vnm = vnet_get_main (); |
| 628 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 629 | dev_class = vnet_get_device_class (vnm, hi->dev_class_index); |
| 630 | |
| 631 | if (NULL == dev_class->ip_tun_desc) |
| 632 | { |
| 633 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 634 | goto out; |
| 635 | } |
| 636 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 637 | pool_get_zero (ipsec_tun_protect_pool, itp); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 638 | |
| 639 | itp->itp_sw_if_index = sw_if_index; |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 640 | itp->itp_ai = ADJ_INDEX_INVALID; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 641 | |
| 642 | itp->itp_n_sa_in = vec_len (sas_in); |
| 643 | for (ii = 0; ii < itp->itp_n_sa_in; ii++) |
| 644 | itp->itp_in_sas[ii] = sas_in[ii]; |
| 645 | itp->itp_out_sa = sa_out; |
| 646 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 647 | itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key)); |
| 648 | ip_address_copy (itp->itp_key, nh); |
| 649 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 650 | rv = dev_class->ip_tun_desc (sw_if_index, |
| 651 | &itp->itp_tun.src, |
| 652 | &itp->itp_tun.dst, &is_l2); |
| 653 | |
| 654 | if (rv) |
| 655 | goto out; |
| 656 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 657 | if (ip46_address_is_zero (&itp->itp_tun.src)) |
| 658 | { |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 659 | /* |
| 660 | * must be one of those pesky ipsec interfaces that has no encap. |
| 661 | * the encap then MUST come from the tunnel mode SA. |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 662 | */ |
| 663 | ipsec_sa_t *sa; |
| 664 | |
| 665 | sa = ipsec_sa_get (itp->itp_out_sa); |
| 666 | |
| 667 | if (!ipsec_sa_is_set_IS_TUNNEL (sa)) |
| 668 | { |
| 669 | rv = VNET_API_ERROR_INVALID_DST_ADDRESS; |
| 670 | goto out; |
| 671 | } |
| 672 | |
| 673 | itp->itp_flags |= IPSEC_PROTECT_ITF; |
| 674 | } |
| 675 | else if (ip46_address_is_zero (&itp->itp_tun.dst)) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 676 | { |
| 677 | /* tunnel has no destination address, presumably because it's p2mp |
| 678 | in which case we use the nh that this is protection for */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 679 | ipsec_tun_protect_update_from_teib |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 680 | (itp, teib_entry_find (sw_if_index, nh)); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 683 | if (is_l2) |
| 684 | itp->itp_flags |= IPSEC_PROTECT_L2; |
| 685 | |
| 686 | /* |
| 687 | * add to the tunnel DB for ingress |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 688 | * - if the SA is in trasnport mode, then the packates will arrive |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 689 | * with the IP src,dst of the protected tunnel, in which case we can |
| 690 | * simply strip the IP header and hand the payload to the protocol |
| 691 | * appropriate input handler |
| 692 | * - if the SA is in tunnel mode then there are two IP headers present |
| 693 | * one for the crytpo tunnel endpoints (described in the SA) and one |
| 694 | * for the tunnel endpoints. The outer IP headers in the srriving |
| 695 | * packets will have the crypto endpoints. So the DB needs to contain |
| 696 | * the crpto endpoint. Once the crypto header is stripped, revealing, |
| 697 | * the tunnel-IP we have 2 choices: |
| 698 | * 1) do a tunnel lookup based on the revealed header |
| 699 | * 2) skip the tunnel lookup and assume that the packet matches the |
| 700 | * one that is protected here. |
| 701 | * If we did 1) then we would allow our peer to use the SA for tunnel |
| 702 | * X to inject traffic onto tunnel Y, this is not good. If we do 2) |
| 703 | * then we don't verify that the peer is indeed using SA for tunnel |
| 704 | * X and addressing tunnel X. So we take a compromise, once the SA |
| 705 | * matches to tunnel X we veriy that the inner IP matches the value |
| 706 | * of the tunnel we are protecting, else it's dropped. |
| 707 | */ |
| 708 | ipsec_tun_protect_config (im, itp, sa_out, sas_in); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 709 | } |
| 710 | else |
| 711 | { |
| 712 | /* updating SAs only */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 713 | itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 714 | |
| 715 | ipsec_tun_protect_unconfig (im, itp); |
| 716 | ipsec_tun_protect_config (im, itp, sa_out, sas_in); |
| 717 | } |
| 718 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 719 | ipsec_sa_unlock (sa_out); |
| 720 | vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 721 | vec_free (sas_in); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 722 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 723 | out: |
| 724 | return (rv); |
| 725 | } |
| 726 | |
| 727 | int |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 728 | ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 729 | { |
| 730 | ipsec_tun_protect_t *itp; |
| 731 | ipsec_main_t *im; |
| 732 | index_t itpi; |
| 733 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 734 | ITP_DBG2 ("delete: %U/%U", |
| 735 | format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index, |
| 736 | format_ip_address, nh); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 737 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 738 | im = &ipsec_main; |
| 739 | if (NULL == nh) |
| 740 | nh = &IP_ADDR_ALL_0; |
| 741 | |
| 742 | itpi = ipsec_tun_protect_find (sw_if_index, nh); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 743 | |
| 744 | if (INDEX_INVALID == itpi) |
| 745 | return (VNET_API_ERROR_NO_SUCH_ENTRY); |
| 746 | |
| 747 | itp = ipsec_tun_protect_get (itpi); |
| 748 | ipsec_tun_protect_unconfig (im, itp); |
| 749 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 750 | if (ADJ_INDEX_INVALID != itp->itp_ai) |
| 751 | adj_unlock (itp->itp_ai); |
| 752 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 753 | clib_mem_free (itp->itp_key); |
| 754 | pool_put (ipsec_tun_protect_pool, itp); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 755 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 756 | return (0); |
| 757 | } |
| 758 | |
| 759 | void |
| 760 | ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx) |
| 761 | { |
| 762 | index_t itpi; |
| 763 | |
| 764 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 765 | pool_foreach_index (itpi, ipsec_tun_protect_pool) |
| 766 | { |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 767 | fn (itpi, ctx); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 768 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 769 | /* *INDENT-ON* */ |
| 770 | } |
| 771 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 772 | void |
| 773 | ipsec_tun_protect_walk_itf (u32 sw_if_index, |
| 774 | ipsec_tun_protect_walk_cb_t fn, void *ctx) |
| 775 | { |
| 776 | ipsec_tun_protect_itf_db_t *idi; |
| 777 | ip_address_t *key; |
| 778 | index_t itpi; |
| 779 | |
| 780 | if (vec_len (itp_db.id_itf) <= sw_if_index) |
| 781 | return; |
| 782 | |
| 783 | idi = &itp_db.id_itf[sw_if_index]; |
| 784 | |
| 785 | /* *INDENT-OFF* */ |
| 786 | hash_foreach(key, itpi, idi->id_hash, |
| 787 | ({ |
| 788 | fn (itpi, ctx); |
| 789 | })); |
| 790 | /* *INDENT-ON* */ |
| 791 | if (INDEX_INVALID != idi->id_itp) |
| 792 | fn (idi->id_itp, ctx); |
| 793 | } |
| 794 | |
| 795 | static void |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 796 | ipsec_tun_feature_update (u32 sw_if_index, u8 arc_index, u8 is_enable, |
| 797 | void *data) |
| 798 | { |
| 799 | ipsec_tun_protect_t *itp; |
| 800 | index_t itpi; |
| 801 | |
| 802 | if (arc_index != feature_main.device_input_feature_arc_index) |
| 803 | return; |
| 804 | |
| 805 | /* Only p2p tunnels supported */ |
| 806 | itpi = ipsec_tun_protect_find (sw_if_index, &IP_ADDR_ALL_0); |
| 807 | if (itpi == INDEX_INVALID) |
| 808 | return; |
| 809 | |
| 810 | itp = ipsec_tun_protect_get (itpi); |
| 811 | |
| 812 | if (is_enable) |
| 813 | { |
| 814 | u32 decrypt_tun = ip46_address_is_ip4 (&itp->itp_crypto.dst) ? |
| 815 | ipsec_main.esp4_decrypt_tun_node_index : |
| 816 | ipsec_main.esp6_decrypt_tun_node_index; |
| 817 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 818 | if (!(itp->itp_flags & IPSEC_PROTECT_FEAT)) |
| 819 | { |
| 820 | itp->itp_flags |= IPSEC_PROTECT_FEAT; |
| 821 | vnet_feature_modify_end_node ( |
| 822 | feature_main.device_input_feature_arc_index, sw_if_index, |
| 823 | decrypt_tun); |
| 824 | } |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 825 | } |
| 826 | else |
| 827 | { |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 828 | if (itp->itp_flags & IPSEC_PROTECT_FEAT) |
| 829 | { |
| 830 | itp->itp_flags &= ~IPSEC_PROTECT_FEAT; |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 831 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 832 | u32 eth_in = |
| 833 | vlib_get_node_by_name (vlib_get_main (), (u8 *) "ethernet-input") |
| 834 | ->index; |
| 835 | |
| 836 | vnet_feature_modify_end_node ( |
| 837 | feature_main.device_input_feature_arc_index, sw_if_index, eth_in); |
| 838 | } |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* Propagate flag change into lookup entries */ |
| 842 | ipsec_tun_protect_rx_db_remove (&ipsec_main, itp); |
| 843 | ipsec_tun_protect_rx_db_add (&ipsec_main, itp); |
| 844 | } |
| 845 | |
| 846 | static void |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 847 | ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad) |
| 848 | { |
| 849 | /* remove our delegate */ |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 850 | ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL); |
Benoît Ganne | ea9bc28 | 2020-04-16 12:40:04 +0200 | [diff] [blame] | 851 | adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static void |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 855 | ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad) |
| 856 | { |
| 857 | ipsec_tun_protect_add_adj (ad->ad_adj_index, |
| 858 | ipsec_tun_protect_get (ad->ad_index)); |
| 859 | } |
| 860 | |
| 861 | static void |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 862 | ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai) |
| 863 | { |
| 864 | /* add our delegate if there is protection for this neighbour */ |
| 865 | ip_address_t ip = IP_ADDRESS_V4_ALL_0S; |
| 866 | ip_adjacency_t *adj; |
| 867 | index_t itpi; |
| 868 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 869 | if (!adj_is_midchain (ai)) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 870 | return; |
| 871 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 872 | vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai, |
| 873 | INDEX_INVALID); |
| 874 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 875 | adj = adj_get (ai); |
| 876 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 877 | ip_address_from_46 (&adj->sub_type.midchain.next_hop, |
| 878 | adj->ia_nh_proto, &ip); |
| 879 | |
| 880 | itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip); |
| 881 | |
| 882 | if (INDEX_INVALID != itpi) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 883 | ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi)); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static u8 * |
| 887 | ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s) |
| 888 | { |
| 889 | const ipsec_tun_protect_t *itp; |
| 890 | |
| 891 | itp = ipsec_tun_protect_from_const_base (aed); |
| 892 | s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp); |
| 893 | |
| 894 | return (s); |
| 895 | } |
| 896 | |
| 897 | static void |
| 898 | ipsec_tun_teib_entry_added (const teib_entry_t * ne) |
| 899 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 900 | ipsec_tun_protect_t *itp; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 901 | index_t itpi; |
| 902 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 903 | itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), |
| 904 | teib_entry_get_peer (ne)); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 905 | |
| 906 | if (INDEX_INVALID == itpi) |
| 907 | return; |
| 908 | |
| 909 | itp = ipsec_tun_protect_get (itpi); |
| 910 | ipsec_tun_protect_rx_db_remove (&ipsec_main, itp); |
| 911 | ipsec_tun_protect_update_from_teib (itp, ne); |
| 912 | ipsec_tun_protect_set_crypto_addr (itp); |
| 913 | ipsec_tun_protect_rx_db_add (&ipsec_main, itp); |
| 914 | |
| 915 | ITP_DBG (itp, "teib-added"); |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | ipsec_tun_teib_entry_deleted (const teib_entry_t * ne) |
| 920 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 921 | ipsec_tun_protect_t *itp; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 922 | index_t itpi; |
| 923 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 924 | itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), |
| 925 | teib_entry_get_peer (ne)); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 926 | |
| 927 | if (INDEX_INVALID == itpi) |
| 928 | return; |
| 929 | |
| 930 | itp = ipsec_tun_protect_get (itpi); |
| 931 | ipsec_tun_protect_rx_db_remove (&ipsec_main, itp); |
| 932 | ipsec_tun_protect_update_from_teib (itp, NULL); |
| 933 | ipsec_tun_protect_set_crypto_addr (itp); |
| 934 | |
| 935 | ITP_DBG (itp, "teib-removed"); |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * VFT registered with the adjacency delegate |
| 940 | */ |
| 941 | const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = { |
| 942 | .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted, |
| 943 | .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created, |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 944 | .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified, |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 945 | .adv_format = ipsec_tun_protect_adj_delegate_format, |
| 946 | }; |
| 947 | |
| 948 | const static teib_vft_t ipsec_tun_teib_vft = { |
| 949 | .nv_added = ipsec_tun_teib_entry_added, |
| 950 | .nv_deleted = ipsec_tun_teib_entry_deleted, |
| 951 | }; |
| 952 | |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 953 | void |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 954 | ipsec_tun_table_init (ip_address_family_t af, uword table_size, u32 n_buckets) |
| 955 | { |
| 956 | ipsec_main_t *im; |
| 957 | |
| 958 | im = &ipsec_main; |
| 959 | |
| 960 | if (AF_IP4 == af) |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 961 | clib_bihash_init_8_16 (&im->tun4_protect_by_key, |
| 962 | "IPSec IPv4 tunnels", n_buckets, table_size); |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 963 | else |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 964 | clib_bihash_init_24_16 (&im->tun6_protect_by_key, |
| 965 | "IPSec IPv6 tunnels", n_buckets, table_size); |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 966 | } |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 967 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 968 | static clib_error_t * |
| 969 | ipsec_tunnel_protect_init (vlib_main_t *vm) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 970 | { |
| 971 | ipsec_main_t *im; |
| 972 | |
| 973 | im = &ipsec_main; |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 974 | clib_bihash_init_24_16 (&im->tun6_protect_by_key, |
| 975 | "IPSec IPv6 tunnels", |
| 976 | IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS, |
| 977 | IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE); |
| 978 | clib_bihash_init_8_16 (&im->tun4_protect_by_key, |
| 979 | "IPSec IPv4 tunnels", |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 980 | IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS, |
| 981 | IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 982 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 983 | ipsec_tun_adj_delegate_type = |
| 984 | adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft); |
| 985 | |
| 986 | ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun"); |
| 987 | |
| 988 | teib_register (&ipsec_tun_teib_vft); |
| 989 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 990 | vnet_feature_register (ipsec_tun_feature_update, NULL); |
| 991 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init); |
| 996 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 997 | /* |
| 998 | * fd.io coding-style-patch-verification: ON |
| 999 | * |
| 1000 | * Local Variables: |
| 1001 | * eval: (c-set-style "gnu") |
| 1002 | * End: |
| 1003 | */ |