Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 16 | #include <sys/random.h> |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 17 | #include <vnet/ipsec/ipsec.h> |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 18 | #include <vnet/ipsec/esp.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 19 | #include <vnet/udp/udp_local.h> |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 20 | #include <vnet/fib/fib_table.h> |
Neale Ranns | 1f50bf8 | 2019-07-16 15:28:52 +0000 | [diff] [blame] | 21 | #include <vnet/fib/fib_entry_track.h> |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 22 | #include <vnet/ipsec/ipsec_tun.h> |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 23 | #include <vnet/ipsec/ipsec.api_enum.h> |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 24 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 25 | /** |
| 26 | * @brief |
| 27 | * SA packet & bytes counters |
| 28 | */ |
| 29 | vlib_combined_counter_main_t ipsec_sa_counters = { |
| 30 | .name = "SA", |
| 31 | .stat_segment_name = "/net/ipsec/sa", |
| 32 | }; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 33 | /* Per-SA error counters */ |
| 34 | vlib_simple_counter_main_t ipsec_sa_err_counters[IPSEC_SA_N_ERRORS]; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 35 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 36 | ipsec_sa_t *ipsec_sa_pool; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 37 | |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 38 | static clib_error_t * |
| 39 | ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa, |
| 40 | u32 sa_index, int is_add) |
| 41 | { |
| 42 | ipsec_ah_backend_t *ab; |
| 43 | ipsec_esp_backend_t *eb; |
| 44 | switch (sa->protocol) |
| 45 | { |
| 46 | case IPSEC_PROTOCOL_AH: |
| 47 | ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 48 | if (ab->add_del_sa_sess_cb) |
| 49 | return ab->add_del_sa_sess_cb (sa_index, is_add); |
| 50 | break; |
| 51 | case IPSEC_PROTOCOL_ESP: |
| 52 | eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 53 | if (eb->add_del_sa_sess_cb) |
| 54 | return eb->add_del_sa_sess_cb (sa_index, is_add); |
| 55 | break; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 60 | void |
| 61 | ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len) |
| 62 | { |
| 63 | memset (key, 0, sizeof (*key)); |
| 64 | |
| 65 | if (len > sizeof (key->data)) |
| 66 | key->len = sizeof (key->data); |
| 67 | else |
| 68 | key->len = len; |
| 69 | |
| 70 | memcpy (key->data, data, key->len); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * 'stack' (resolve the recursion for) the SA tunnel destination |
| 75 | */ |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 76 | static void |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 77 | ipsec_sa_stack (ipsec_sa_t * sa) |
| 78 | { |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 79 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 80 | dpo_id_t tmp = DPO_INVALID; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 81 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 82 | tunnel_contribute_forwarding (&sa->tunnel, &tmp); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 83 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 84 | if (IPSEC_PROTOCOL_AH == sa->protocol) |
| 85 | dpo_stack_from_node ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? |
| 86 | im->ah6_encrypt_node_index : |
| 87 | im->ah4_encrypt_node_index), &sa->dpo, &tmp); |
| 88 | else |
| 89 | dpo_stack_from_node ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? |
| 90 | im->esp6_encrypt_node_index : |
| 91 | im->esp4_encrypt_node_index), &sa->dpo, &tmp); |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 92 | dpo_reset (&tmp); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 95 | void |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 96 | ipsec_sa_set_async_mode (ipsec_sa_t *sa, int is_enabled) |
| 97 | { |
| 98 | if (is_enabled) |
| 99 | { |
| 100 | sa->crypto_key_index = sa->crypto_async_key_index; |
| 101 | sa->crypto_enc_op_id = sa->crypto_async_enc_op_id; |
| 102 | sa->crypto_dec_op_id = sa->crypto_async_dec_op_id; |
| 103 | sa->integ_key_index = ~0; |
| 104 | sa->integ_op_id = ~0; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | sa->crypto_key_index = sa->crypto_sync_key_index; |
| 109 | sa->crypto_enc_op_id = sa->crypto_sync_enc_op_id; |
| 110 | sa->crypto_dec_op_id = sa->crypto_sync_dec_op_id; |
| 111 | sa->integ_key_index = sa->integ_sync_key_index; |
| 112 | sa->integ_op_id = sa->integ_sync_op_id; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 117 | ipsec_sa_set_crypto_alg (ipsec_sa_t * sa, ipsec_crypto_alg_t crypto_alg) |
| 118 | { |
| 119 | ipsec_main_t *im = &ipsec_main; |
| 120 | sa->crypto_alg = crypto_alg; |
| 121 | sa->crypto_iv_size = im->crypto_algs[crypto_alg].iv_size; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 122 | sa->esp_block_align = clib_max (4, im->crypto_algs[crypto_alg].block_align); |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 123 | sa->crypto_sync_enc_op_id = im->crypto_algs[crypto_alg].enc_op_id; |
| 124 | sa->crypto_sync_dec_op_id = im->crypto_algs[crypto_alg].dec_op_id; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 125 | sa->crypto_calg = im->crypto_algs[crypto_alg].alg; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 126 | ASSERT (sa->crypto_iv_size <= ESP_MAX_IV_SIZE); |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 127 | ASSERT (sa->esp_block_align <= ESP_MAX_BLOCK_SIZE); |
Vladimir Ratnikov | d7c030d | 2022-09-13 13:09:53 +0000 | [diff] [blame] | 128 | if (IPSEC_CRYPTO_ALG_IS_GCM (crypto_alg) || |
| 129 | IPSEC_CRYPTO_ALG_CTR_AEAD_OTHERS (crypto_alg)) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 130 | { |
| 131 | sa->integ_icv_size = im->crypto_algs[crypto_alg].icv_size; |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 132 | ipsec_sa_set_IS_CTR (sa); |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 133 | ipsec_sa_set_IS_AEAD (sa); |
| 134 | } |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 135 | else if (IPSEC_CRYPTO_ALG_IS_CTR (crypto_alg)) |
| 136 | { |
| 137 | ipsec_sa_set_IS_CTR (sa); |
| 138 | } |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | ipsec_sa_set_integ_alg (ipsec_sa_t * sa, ipsec_integ_alg_t integ_alg) |
| 143 | { |
| 144 | ipsec_main_t *im = &ipsec_main; |
| 145 | sa->integ_alg = integ_alg; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 146 | sa->integ_icv_size = im->integ_algs[integ_alg].icv_size; |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 147 | sa->integ_sync_op_id = im->integ_algs[integ_alg].op_id; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 148 | sa->integ_calg = im->integ_algs[integ_alg].alg; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 149 | ASSERT (sa->integ_icv_size <= ESP_MAX_ICV_SIZE); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 152 | void |
| 153 | ipsec_sa_set_async_op_ids (ipsec_sa_t * sa) |
| 154 | { |
| 155 | /* *INDENT-OFF* */ |
| 156 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 157 | { |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 158 | #define _(n, s, k) \ |
| 159 | if (sa->crypto_sync_enc_op_id == VNET_CRYPTO_OP_##n##_ENC) \ |
| 160 | sa->crypto_async_enc_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD12_ENC; \ |
| 161 | if (sa->crypto_sync_dec_op_id == VNET_CRYPTO_OP_##n##_DEC) \ |
| 162 | sa->crypto_async_dec_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD12_DEC; |
| 163 | foreach_crypto_aead_alg |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 164 | #undef _ |
| 165 | } |
| 166 | else |
| 167 | { |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 168 | #define _(n, s, k) \ |
| 169 | if (sa->crypto_sync_enc_op_id == VNET_CRYPTO_OP_##n##_ENC) \ |
| 170 | sa->crypto_async_enc_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD8_ENC; \ |
| 171 | if (sa->crypto_sync_dec_op_id == VNET_CRYPTO_OP_##n##_DEC) \ |
| 172 | sa->crypto_async_dec_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD8_DEC; |
| 173 | foreach_crypto_aead_alg |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 174 | #undef _ |
| 175 | } |
| 176 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 177 | #define _(c, h, s, k, d) \ |
| 178 | if (sa->crypto_sync_enc_op_id == VNET_CRYPTO_OP_##c##_ENC && \ |
| 179 | sa->integ_sync_op_id == VNET_CRYPTO_OP_##h##_HMAC) \ |
| 180 | sa->crypto_async_enc_op_id = VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC; \ |
| 181 | if (sa->crypto_sync_dec_op_id == VNET_CRYPTO_OP_##c##_DEC && \ |
| 182 | sa->integ_sync_op_id == VNET_CRYPTO_OP_##h##_HMAC) \ |
| 183 | sa->crypto_async_dec_op_id = VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 184 | foreach_crypto_link_async_alg |
| 185 | #undef _ |
| 186 | /* *INDENT-ON* */ |
| 187 | } |
| 188 | |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 189 | int |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 190 | ipsec_sa_update (u32 id, u16 src_port, u16 dst_port, const tunnel_t *tun, |
| 191 | bool is_tun) |
| 192 | { |
| 193 | ipsec_main_t *im = &ipsec_main; |
| 194 | ipsec_sa_t *sa; |
| 195 | u32 sa_index; |
| 196 | uword *p; |
| 197 | int rv; |
| 198 | |
| 199 | p = hash_get (im->sa_index_by_sa_id, id); |
| 200 | if (!p) |
| 201 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 202 | |
| 203 | sa = ipsec_sa_get (p[0]); |
| 204 | sa_index = sa - ipsec_sa_pool; |
| 205 | |
| 206 | if (is_tun && ipsec_sa_is_set_IS_TUNNEL (sa) && |
| 207 | (ip_address_cmp (&tun->t_src, &sa->tunnel.t_src) != 0 || |
| 208 | ip_address_cmp (&tun->t_dst, &sa->tunnel.t_dst) != 0)) |
| 209 | { |
| 210 | /* if the source IP is updated for an inbound SA under a tunnel protect, |
| 211 | we need to update the tun_protect DB with the new src IP */ |
| 212 | if (ipsec_sa_is_set_IS_INBOUND (sa) && |
| 213 | ip_address_cmp (&tun->t_src, &sa->tunnel.t_src) != 0 && |
| 214 | !ip46_address_is_zero (&tun->t_src.ip)) |
| 215 | { |
| 216 | if (ip46_address_is_ip4 (&sa->tunnel.t_src.ip)) |
| 217 | { |
| 218 | ipsec4_tunnel_kv_t old_key, new_key; |
| 219 | clib_bihash_kv_8_16_t res, |
| 220 | *bkey = (clib_bihash_kv_8_16_t *) &old_key; |
| 221 | |
| 222 | ipsec4_tunnel_mk_key (&old_key, &sa->tunnel.t_src.ip.ip4, |
| 223 | clib_host_to_net_u32 (sa->spi)); |
| 224 | ipsec4_tunnel_mk_key (&new_key, &tun->t_src.ip.ip4, |
| 225 | clib_host_to_net_u32 (sa->spi)); |
| 226 | |
| 227 | if (!clib_bihash_search_8_16 (&im->tun4_protect_by_key, bkey, |
| 228 | &res)) |
| 229 | { |
| 230 | clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, &res, 0); |
| 231 | res.key = new_key.key; |
| 232 | clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, &res, 1); |
| 233 | } |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | ipsec6_tunnel_kv_t old_key = { |
| 238 | .key = { |
| 239 | .remote_ip = sa->tunnel.t_src.ip.ip6, |
| 240 | .spi = clib_host_to_net_u32 (sa->spi), |
| 241 | }, |
| 242 | }, new_key = { |
| 243 | .key = { |
| 244 | .remote_ip = tun->t_src.ip.ip6, |
| 245 | .spi = clib_host_to_net_u32 (sa->spi), |
| 246 | }}; |
| 247 | clib_bihash_kv_24_16_t res, |
| 248 | *bkey = (clib_bihash_kv_24_16_t *) &old_key; |
| 249 | |
| 250 | if (!clib_bihash_search_24_16 (&im->tun6_protect_by_key, bkey, |
| 251 | &res)) |
| 252 | { |
| 253 | clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, &res, |
| 254 | 0); |
| 255 | clib_memcpy (&res.key, &new_key.key, 3); |
| 256 | clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, &res, |
| 257 | 1); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | tunnel_unresolve (&sa->tunnel); |
| 262 | tunnel_copy (tun, &sa->tunnel); |
| 263 | if (!ipsec_sa_is_set_IS_INBOUND (sa)) |
| 264 | { |
| 265 | dpo_reset (&sa->dpo); |
| 266 | |
| 267 | sa->tunnel_flags = sa->tunnel.t_encap_decap_flags; |
| 268 | |
| 269 | rv = tunnel_resolve (&sa->tunnel, FIB_NODE_TYPE_IPSEC_SA, sa_index); |
| 270 | |
| 271 | if (rv) |
| 272 | { |
| 273 | hash_unset (im->sa_index_by_sa_id, sa->id); |
| 274 | pool_put (ipsec_sa_pool, sa); |
| 275 | return rv; |
| 276 | } |
| 277 | ipsec_sa_stack (sa); |
| 278 | /* generate header templates */ |
| 279 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa)) |
| 280 | { |
| 281 | tunnel_build_v6_hdr (&sa->tunnel, |
| 282 | (ipsec_sa_is_set_UDP_ENCAP (sa) ? |
| 283 | IP_PROTOCOL_UDP : |
| 284 | IP_PROTOCOL_IPSEC_ESP), |
| 285 | &sa->ip6_hdr); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | tunnel_build_v4_hdr (&sa->tunnel, |
| 290 | (ipsec_sa_is_set_UDP_ENCAP (sa) ? |
| 291 | IP_PROTOCOL_UDP : |
| 292 | IP_PROTOCOL_IPSEC_ESP), |
| 293 | &sa->ip4_hdr); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (ipsec_sa_is_set_UDP_ENCAP (sa)) |
| 299 | { |
| 300 | if (dst_port != IPSEC_UDP_PORT_NONE && |
| 301 | dst_port != clib_net_to_host_u16 (sa->udp_hdr.dst_port)) |
| 302 | { |
| 303 | if (ipsec_sa_is_set_IS_INBOUND (sa)) |
| 304 | { |
| 305 | ipsec_unregister_udp_port ( |
| 306 | clib_net_to_host_u16 (sa->udp_hdr.dst_port), |
| 307 | !ipsec_sa_is_set_IS_TUNNEL_V6 (sa)); |
| 308 | ipsec_register_udp_port (dst_port, |
| 309 | !ipsec_sa_is_set_IS_TUNNEL_V6 (sa)); |
| 310 | } |
| 311 | sa->udp_hdr.dst_port = clib_host_to_net_u16 (dst_port); |
| 312 | } |
| 313 | if (src_port != IPSEC_UDP_PORT_NONE && |
| 314 | src_port != clib_net_to_host_u16 (sa->udp_hdr.src_port)) |
| 315 | sa->udp_hdr.src_port = clib_host_to_net_u16 (src_port); |
| 316 | } |
| 317 | return (0); |
| 318 | } |
| 319 | |
| 320 | int |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 321 | ipsec_sa_add_and_lock (u32 id, u32 spi, ipsec_protocol_t proto, |
| 322 | ipsec_crypto_alg_t crypto_alg, const ipsec_key_t *ck, |
| 323 | ipsec_integ_alg_t integ_alg, const ipsec_key_t *ik, |
| 324 | ipsec_sa_flags_t flags, u32 salt, u16 src_port, |
| 325 | u16 dst_port, const tunnel_t *tun, u32 *sa_out_index) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 326 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 327 | vlib_main_t *vm = vlib_get_main (); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 328 | ipsec_main_t *im = &ipsec_main; |
| 329 | clib_error_t *err; |
| 330 | ipsec_sa_t *sa; |
| 331 | u32 sa_index; |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 332 | u64 rand[2]; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 333 | uword *p; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 334 | int rv; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 335 | |
| 336 | p = hash_get (im->sa_index_by_sa_id, id); |
| 337 | if (p) |
| 338 | return VNET_API_ERROR_ENTRY_ALREADY_EXISTS; |
| 339 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 340 | if (getrandom (rand, sizeof (rand), 0) != sizeof (rand)) |
| 341 | return VNET_API_ERROR_INIT_FAILED; |
| 342 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 343 | pool_get_aligned_zero (ipsec_sa_pool, sa, CLIB_CACHE_LINE_BYTES); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 344 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 345 | clib_pcg64i_srandom_r (&sa->iv_prng, rand[0], rand[1]); |
| 346 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 347 | fib_node_init (&sa->node, FIB_NODE_TYPE_IPSEC_SA); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 348 | fib_node_lock (&sa->node); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 349 | sa_index = sa - ipsec_sa_pool; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 350 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 351 | vlib_validate_combined_counter (&ipsec_sa_counters, sa_index); |
| 352 | vlib_zero_combined_counter (&ipsec_sa_counters, sa_index); |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 353 | for (int i = 0; i < IPSEC_SA_N_ERRORS; i++) |
| 354 | { |
| 355 | vlib_validate_simple_counter (&ipsec_sa_err_counters[i], sa_index); |
| 356 | vlib_zero_simple_counter (&ipsec_sa_err_counters[i], sa_index); |
| 357 | } |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 358 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 359 | tunnel_copy (tun, &sa->tunnel); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 360 | sa->id = id; |
| 361 | sa->spi = spi; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 362 | sa->stat_index = sa_index; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 363 | sa->protocol = proto; |
Neale Ranns | 2b5ba95 | 2019-04-02 10:15:40 +0000 | [diff] [blame] | 364 | sa->flags = flags; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 365 | sa->salt = salt; |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 366 | sa->thread_index = (vlib_num_workers ()) ? ~0 : 0; |
Filip Tehlar | c41217a | 2019-10-18 17:51:06 +0000 | [diff] [blame] | 367 | if (integ_alg != IPSEC_INTEG_ALG_NONE) |
| 368 | { |
| 369 | ipsec_sa_set_integ_alg (sa, integ_alg); |
| 370 | clib_memcpy (&sa->integ_key, ik, sizeof (sa->integ_key)); |
| 371 | } |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 372 | ipsec_sa_set_crypto_alg (sa, crypto_alg); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 373 | ipsec_sa_set_async_op_ids (sa); |
| 374 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 375 | clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key)); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 376 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 377 | sa->crypto_sync_key_index = vnet_crypto_key_add ( |
| 378 | vm, im->crypto_algs[crypto_alg].alg, (u8 *) ck->data, ck->len); |
| 379 | if (~0 == sa->crypto_sync_key_index) |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 380 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 381 | pool_put (ipsec_sa_pool, sa); |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 382 | return VNET_API_ERROR_KEY_LENGTH; |
| 383 | } |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 384 | |
Filip Tehlar | c41217a | 2019-10-18 17:51:06 +0000 | [diff] [blame] | 385 | if (integ_alg != IPSEC_INTEG_ALG_NONE) |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 386 | { |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 387 | sa->integ_sync_key_index = vnet_crypto_key_add ( |
| 388 | vm, im->integ_algs[integ_alg].alg, (u8 *) ik->data, ik->len); |
| 389 | if (~0 == sa->integ_sync_key_index) |
Filip Tehlar | c41217a | 2019-10-18 17:51:06 +0000 | [diff] [blame] | 390 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 391 | pool_put (ipsec_sa_pool, sa); |
Filip Tehlar | c41217a | 2019-10-18 17:51:06 +0000 | [diff] [blame] | 392 | return VNET_API_ERROR_KEY_LENGTH; |
| 393 | } |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 394 | } |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 395 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 396 | if (sa->crypto_async_enc_op_id && !ipsec_sa_is_set_IS_AEAD (sa)) |
| 397 | sa->crypto_async_key_index = |
| 398 | vnet_crypto_key_add_linked (vm, sa->crypto_sync_key_index, |
| 399 | sa->integ_sync_key_index); // AES-CBC & HMAC |
| 400 | else |
| 401 | sa->crypto_async_key_index = sa->crypto_sync_key_index; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 402 | |
| 403 | if (im->async_mode) |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 404 | { |
| 405 | ipsec_sa_set_async_mode (sa, 1); |
| 406 | } |
| 407 | else if (ipsec_sa_is_set_IS_ASYNC (sa)) |
| 408 | { |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 409 | ipsec_sa_set_async_mode (sa, 1 /* is_enabled */); |
| 410 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 411 | else |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 412 | { |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 413 | ipsec_sa_set_async_mode (sa, 0 /* is_enabled */); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 414 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 415 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 416 | err = ipsec_check_support_cb (im, sa); |
| 417 | if (err) |
| 418 | { |
| 419 | clib_warning ("%s", err->what); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 420 | pool_put (ipsec_sa_pool, sa); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 421 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 422 | } |
| 423 | |
| 424 | err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1); |
| 425 | if (err) |
| 426 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 427 | pool_put (ipsec_sa_pool, sa); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 428 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 429 | } |
| 430 | |
Neale Ranns | 53dd08c | 2021-06-24 15:41:03 +0000 | [diff] [blame] | 431 | if (ipsec_sa_is_set_IS_TUNNEL (sa) && |
| 432 | AF_IP6 == ip_addr_version (&tun->t_src)) |
| 433 | ipsec_sa_set_IS_TUNNEL_V6 (sa); |
| 434 | |
Neale Ranns | 2b5ba95 | 2019-04-02 10:15:40 +0000 | [diff] [blame] | 435 | if (ipsec_sa_is_set_IS_TUNNEL (sa) && !ipsec_sa_is_set_IS_INBOUND (sa)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 436 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 437 | sa->tunnel_flags = sa->tunnel.t_encap_decap_flags; |
| 438 | |
| 439 | rv = tunnel_resolve (&sa->tunnel, FIB_NODE_TYPE_IPSEC_SA, sa_index); |
| 440 | |
| 441 | if (rv) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 442 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 443 | pool_put (ipsec_sa_pool, sa); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 444 | return rv; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 445 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 446 | ipsec_sa_stack (sa); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 447 | |
| 448 | /* generate header templates */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 449 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 450 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 451 | tunnel_build_v6_hdr (&sa->tunnel, |
| 452 | (ipsec_sa_is_set_UDP_ENCAP (sa) ? |
| 453 | IP_PROTOCOL_UDP : |
| 454 | IP_PROTOCOL_IPSEC_ESP), |
| 455 | &sa->ip6_hdr); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 456 | } |
| 457 | else |
| 458 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 459 | tunnel_build_v4_hdr (&sa->tunnel, |
| 460 | (ipsec_sa_is_set_UDP_ENCAP (sa) ? |
| 461 | IP_PROTOCOL_UDP : |
| 462 | IP_PROTOCOL_IPSEC_ESP), |
| 463 | &sa->ip4_hdr); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 464 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 465 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 466 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 467 | if (ipsec_sa_is_set_UDP_ENCAP (sa)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 468 | { |
Filip Tehlar | e5d3491 | 2020-03-02 15:17:37 +0000 | [diff] [blame] | 469 | if (dst_port == IPSEC_UDP_PORT_NONE) |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 470 | sa->udp_hdr.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec); |
Filip Tehlar | e5d3491 | 2020-03-02 15:17:37 +0000 | [diff] [blame] | 471 | else |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 472 | sa->udp_hdr.dst_port = clib_host_to_net_u16 (dst_port); |
| 473 | |
| 474 | if (src_port == IPSEC_UDP_PORT_NONE) |
| 475 | sa->udp_hdr.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec); |
| 476 | else |
| 477 | sa->udp_hdr.src_port = clib_host_to_net_u16 (src_port); |
| 478 | |
| 479 | if (ipsec_sa_is_set_IS_INBOUND (sa)) |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 480 | ipsec_register_udp_port (clib_host_to_net_u16 (sa->udp_hdr.dst_port), |
| 481 | !ipsec_sa_is_set_IS_TUNNEL_V6 (sa)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 484 | hash_set (im->sa_index_by_sa_id, sa->id, sa_index); |
| 485 | |
| 486 | if (sa_out_index) |
| 487 | *sa_out_index = sa_index; |
| 488 | |
| 489 | return (0); |
| 490 | } |
| 491 | |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 492 | static void |
| 493 | ipsec_sa_del (ipsec_sa_t * sa) |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 494 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 495 | vlib_main_t *vm = vlib_get_main (); |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 496 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 497 | u32 sa_index; |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 498 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 499 | sa_index = sa - ipsec_sa_pool; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 500 | hash_unset (im->sa_index_by_sa_id, sa->id); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 501 | tunnel_unresolve (&sa->tunnel); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 502 | |
| 503 | /* no recovery possible when deleting an SA */ |
| 504 | (void) ipsec_call_add_del_callbacks (im, sa, sa_index, 0); |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 505 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 506 | if (ipsec_sa_is_set_IS_ASYNC (sa)) |
Benoît Ganne | ab412cd | 2023-01-03 18:35:04 +0100 | [diff] [blame] | 507 | { |
Benoît Ganne | ab412cd | 2023-01-03 18:35:04 +0100 | [diff] [blame] | 508 | if (!ipsec_sa_is_set_IS_AEAD (sa)) |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 509 | vnet_crypto_key_del (vm, sa->crypto_async_key_index); |
Benoît Ganne | ab412cd | 2023-01-03 18:35:04 +0100 | [diff] [blame] | 510 | } |
| 511 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 512 | if (ipsec_sa_is_set_UDP_ENCAP (sa) && ipsec_sa_is_set_IS_INBOUND (sa)) |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 513 | ipsec_unregister_udp_port (clib_net_to_host_u16 (sa->udp_hdr.dst_port), |
| 514 | !ipsec_sa_is_set_IS_TUNNEL_V6 (sa)); |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 515 | |
Neale Ranns | 2b5ba95 | 2019-04-02 10:15:40 +0000 | [diff] [blame] | 516 | if (ipsec_sa_is_set_IS_TUNNEL (sa) && !ipsec_sa_is_set_IS_INBOUND (sa)) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 517 | dpo_reset (&sa->dpo); |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 518 | vnet_crypto_key_del (vm, sa->crypto_sync_key_index); |
Filip Tehlar | c41217a | 2019-10-18 17:51:06 +0000 | [diff] [blame] | 519 | if (sa->integ_alg != IPSEC_INTEG_ALG_NONE) |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 520 | vnet_crypto_key_del (vm, sa->integ_sync_key_index); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 521 | pool_put (ipsec_sa_pool, sa); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 524 | int |
| 525 | ipsec_sa_bind (u32 id, u32 worker, bool bind) |
| 526 | { |
| 527 | ipsec_main_t *im = &ipsec_main; |
| 528 | uword *p; |
| 529 | ipsec_sa_t *sa; |
| 530 | |
| 531 | p = hash_get (im->sa_index_by_sa_id, id); |
| 532 | if (!p) |
| 533 | return VNET_API_ERROR_INVALID_VALUE; |
| 534 | |
| 535 | sa = ipsec_sa_get (p[0]); |
| 536 | |
| 537 | if (!bind) |
| 538 | { |
| 539 | sa->thread_index = ~0; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | if (worker >= vlib_num_workers ()) |
| 544 | return VNET_API_ERROR_INVALID_WORKER; |
| 545 | |
| 546 | sa->thread_index = vlib_get_worker_thread_index (worker); |
| 547 | return 0; |
| 548 | } |
| 549 | |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 550 | void |
| 551 | ipsec_sa_unlock (index_t sai) |
| 552 | { |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 553 | ipsec_sa_t *sa; |
| 554 | |
| 555 | if (INDEX_INVALID == sai) |
| 556 | return; |
| 557 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 558 | sa = ipsec_sa_get (sai); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 559 | |
| 560 | fib_node_unlock (&sa->node); |
| 561 | } |
| 562 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 563 | void |
| 564 | ipsec_sa_lock (index_t sai) |
| 565 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 566 | ipsec_sa_t *sa; |
| 567 | |
| 568 | if (INDEX_INVALID == sai) |
| 569 | return; |
| 570 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 571 | sa = ipsec_sa_get (sai); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 572 | |
| 573 | fib_node_lock (&sa->node); |
| 574 | } |
| 575 | |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 576 | index_t |
| 577 | ipsec_sa_find_and_lock (u32 id) |
| 578 | { |
| 579 | ipsec_main_t *im = &ipsec_main; |
| 580 | ipsec_sa_t *sa; |
| 581 | uword *p; |
| 582 | |
| 583 | p = hash_get (im->sa_index_by_sa_id, id); |
| 584 | |
| 585 | if (!p) |
| 586 | return INDEX_INVALID; |
| 587 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 588 | sa = ipsec_sa_get (p[0]); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 589 | |
| 590 | fib_node_lock (&sa->node); |
| 591 | |
| 592 | return (p[0]); |
| 593 | } |
| 594 | |
| 595 | int |
| 596 | ipsec_sa_unlock_id (u32 id) |
| 597 | { |
| 598 | ipsec_main_t *im = &ipsec_main; |
| 599 | uword *p; |
| 600 | |
| 601 | p = hash_get (im->sa_index_by_sa_id, id); |
| 602 | |
| 603 | if (!p) |
| 604 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 605 | |
| 606 | ipsec_sa_unlock (p[0]); |
| 607 | |
| 608 | return (0); |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 611 | void |
| 612 | ipsec_sa_clear (index_t sai) |
| 613 | { |
| 614 | vlib_zero_combined_counter (&ipsec_sa_counters, sai); |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 615 | for (int i = 0; i < IPSEC_SA_N_ERRORS; i++) |
| 616 | vlib_zero_simple_counter (&ipsec_sa_err_counters[i], sai); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 619 | void |
| 620 | ipsec_sa_walk (ipsec_sa_walk_cb_t cb, void *ctx) |
| 621 | { |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 622 | ipsec_sa_t *sa; |
| 623 | |
| 624 | /* *INDENT-OFF* */ |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 625 | pool_foreach (sa, ipsec_sa_pool) |
| 626 | { |
| 627 | if (WALK_CONTINUE != cb (sa, ctx)) |
| 628 | break; |
| 629 | } |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 630 | /* *INDENT-ON* */ |
| 631 | } |
| 632 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 633 | /** |
| 634 | * Function definition to get a FIB node from its index |
| 635 | */ |
| 636 | static fib_node_t * |
| 637 | ipsec_sa_fib_node_get (fib_node_index_t index) |
| 638 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 639 | ipsec_sa_t *sa; |
| 640 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 641 | sa = ipsec_sa_get (index); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 642 | |
| 643 | return (&sa->node); |
| 644 | } |
| 645 | |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 646 | static ipsec_sa_t * |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 647 | ipsec_sa_from_fib_node (fib_node_t *node) |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 648 | { |
| 649 | ASSERT (FIB_NODE_TYPE_IPSEC_SA == node->fn_type); |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 650 | return ( |
| 651 | (ipsec_sa_t *) (((char *) node) - STRUCT_OFFSET_OF (ipsec_sa_t, node))); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 654 | /** |
| 655 | * Function definition to inform the FIB node that its last lock has gone. |
| 656 | */ |
| 657 | static void |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 658 | ipsec_sa_last_lock_gone (fib_node_t *node) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 659 | { |
| 660 | /* |
| 661 | * The ipsec SA is a root of the graph. As such |
| 662 | * it never has children and thus is never locked. |
| 663 | */ |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 664 | ipsec_sa_del (ipsec_sa_from_fib_node (node)); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Function definition to backwalk a FIB node |
| 669 | */ |
| 670 | static fib_node_back_walk_rc_t |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 671 | ipsec_sa_back_walk (fib_node_t *node, fib_node_back_walk_ctx_t *ctx) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 672 | { |
| 673 | ipsec_sa_stack (ipsec_sa_from_fib_node (node)); |
| 674 | |
| 675 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 676 | } |
| 677 | |
| 678 | /* |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 679 | * Virtual function table registered by SAs |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 680 | * for participation in the FIB object graph. |
| 681 | */ |
| 682 | const static fib_node_vft_t ipsec_sa_vft = { |
| 683 | .fnv_get = ipsec_sa_fib_node_get, |
| 684 | .fnv_last_lock = ipsec_sa_last_lock_gone, |
| 685 | .fnv_back_walk = ipsec_sa_back_walk, |
| 686 | }; |
| 687 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 688 | /* Init per-SA error counters and node type */ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 689 | clib_error_t * |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 690 | ipsec_sa_init (vlib_main_t *vm) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 691 | { |
| 692 | fib_node_register_type (FIB_NODE_TYPE_IPSEC_SA, &ipsec_sa_vft); |
| 693 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 694 | #define _(index, val, err, desc) \ |
| 695 | ipsec_sa_err_counters[index].name = \ |
| 696 | (char *) format (0, "SA-" #err "%c", 0); \ |
| 697 | ipsec_sa_err_counters[index].stat_segment_name = \ |
| 698 | (char *) format (0, "/net/ipsec/sa/err/" #err "%c", 0); \ |
| 699 | ipsec_sa_err_counters[index].counters = 0; |
| 700 | foreach_ipsec_sa_err |
| 701 | #undef _ |
| 702 | return 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 705 | VLIB_INIT_FUNCTION (ipsec_sa_init); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 706 | |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 707 | /* |
| 708 | * fd.io coding-style-patch-verification: ON |
| 709 | * |
| 710 | * Local Variables: |
| 711 | * eval: (c-set-style "gnu") |
| 712 | * End: |
| 713 | */ |