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