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