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