Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 2 | * ipsec.c : IPSEC module functions |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
| 21 | #include <vnet/interface.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 22 | #include <vnet/udp/udp_local.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include <vnet/ipsec/ipsec.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 25 | #include <vnet/ipsec/esp.h> |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 26 | #include <vnet/ipsec/ah.h> |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 27 | #include <vnet/ipsec/ipsec_tun.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 28 | |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 29 | /* Flow cache is sized for 1 million flows with a load factor of .25. |
| 30 | */ |
| 31 | #define IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS (1 << 22) |
| 32 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 33 | ipsec_main_t ipsec_main; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 34 | esp_async_post_next_t esp_encrypt_async_next; |
| 35 | esp_async_post_next_t esp_decrypt_async_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | |
| 37 | static clib_error_t * |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 38 | ipsec_check_ah_support (ipsec_sa_t * sa) |
| 39 | { |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 40 | ipsec_main_t *im = &ipsec_main; |
| 41 | |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 42 | if (sa->integ_alg == IPSEC_INTEG_ALG_NONE) |
| 43 | return clib_error_return (0, "unsupported none integ-alg"); |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 44 | |
| 45 | if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg)) |
| 46 | return clib_error_return (0, "No crypto engine support for %U", |
| 47 | format_ipsec_integ_alg, sa->integ_alg); |
| 48 | |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static clib_error_t * |
| 53 | ipsec_check_esp_support (ipsec_sa_t * sa) |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 54 | { |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 55 | ipsec_main_t *im = &ipsec_main; |
| 56 | |
| 57 | if (IPSEC_INTEG_ALG_NONE != sa->integ_alg) |
| 58 | { |
| 59 | if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg)) |
| 60 | return clib_error_return (0, "No crypto engine support for %U", |
| 61 | format_ipsec_integ_alg, sa->integ_alg); |
| 62 | } |
| 63 | if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg) |
| 64 | { |
| 65 | if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg)) |
| 66 | return clib_error_return (0, "No crypto engine support for %U", |
| 67 | format_ipsec_crypto_alg, sa->crypto_alg); |
| 68 | } |
| 69 | |
| 70 | return (0); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 73 | clib_error_t * |
| 74 | ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add) |
| 75 | { |
| 76 | ipsec_ah_backend_t *ah = |
| 77 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 78 | if (ah->add_del_sa_sess_cb) |
| 79 | { |
| 80 | clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add); |
| 81 | if (err) |
| 82 | return err; |
| 83 | } |
| 84 | ipsec_esp_backend_t *esp = |
| 85 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 86 | if (esp->add_del_sa_sess_cb) |
| 87 | { |
| 88 | clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add); |
| 89 | if (err) |
| 90 | return err; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | clib_error_t * |
| 96 | ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa) |
| 97 | { |
| 98 | clib_error_t *error = 0; |
Matthew Smith | 461caa5 | 2018-12-21 11:53:16 -0600 | [diff] [blame] | 99 | |
| 100 | if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH)) |
| 101 | { |
| 102 | ipsec_ah_backend_t *ah = |
| 103 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 104 | ASSERT (ah->check_support_cb); |
| 105 | error = ah->check_support_cb (sa); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | ipsec_esp_backend_t *esp = |
| 110 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 111 | ASSERT (esp->check_support_cb); |
| 112 | error = esp->check_support_cb (sa); |
| 113 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 114 | return error; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static void |
| 119 | ipsec_add_node (vlib_main_t * vm, const char *node_name, |
| 120 | const char *prev_node_name, u32 * out_node_index, |
| 121 | u32 * out_next_index) |
| 122 | { |
| 123 | vlib_node_t *prev_node, *node; |
| 124 | prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name); |
| 125 | ASSERT (prev_node); |
| 126 | node = vlib_get_node_by_name (vm, (u8 *) node_name); |
| 127 | ASSERT (node); |
| 128 | *out_node_index = node->index; |
| 129 | *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index); |
| 130 | } |
| 131 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 132 | void |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 133 | ipsec_unregister_udp_port (u16 port) |
| 134 | { |
| 135 | ipsec_main_t *im = &ipsec_main; |
| 136 | u32 n_regs; |
| 137 | uword *p; |
| 138 | |
| 139 | p = hash_get (im->udp_port_registrations, port); |
| 140 | |
| 141 | ASSERT (p); |
| 142 | |
| 143 | n_regs = p[0]; |
| 144 | |
| 145 | if (0 == --n_regs) |
| 146 | { |
| 147 | udp_unregister_dst_port (vlib_get_main (), port, 1); |
| 148 | hash_unset (im->udp_port_registrations, port); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | hash_unset (im->udp_port_registrations, port); |
| 153 | hash_set (im->udp_port_registrations, port, n_regs); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | ipsec_register_udp_port (u16 port) |
| 159 | { |
| 160 | ipsec_main_t *im = &ipsec_main; |
| 161 | u32 n_regs; |
| 162 | uword *p; |
| 163 | |
| 164 | p = hash_get (im->udp_port_registrations, port); |
| 165 | |
| 166 | n_regs = (p ? p[0] : 0); |
| 167 | |
| 168 | if (0 == n_regs++) |
| 169 | udp_register_dst_port (vlib_get_main (), port, |
| 170 | ipsec4_tun_input_node.index, 1); |
| 171 | |
| 172 | hash_unset (im->udp_port_registrations, port); |
| 173 | hash_set (im->udp_port_registrations, port, n_regs); |
| 174 | } |
| 175 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 176 | u32 |
| 177 | ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 178 | const char *name, |
| 179 | const char *ah4_encrypt_node_name, |
| 180 | const char *ah4_decrypt_node_name, |
| 181 | const char *ah6_encrypt_node_name, |
| 182 | const char *ah6_decrypt_node_name, |
| 183 | check_support_cb_t ah_check_support_cb, |
| 184 | add_del_sa_sess_cb_t ah_add_del_sa_sess_cb) |
| 185 | { |
| 186 | ipsec_ah_backend_t *b; |
| 187 | pool_get (im->ah_backends, b); |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 188 | b->name = format (0, "%s%c", name, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 189 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 190 | ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 191 | &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 192 | ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 193 | &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 194 | ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 195 | &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 196 | ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 197 | &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index); |
| 198 | |
| 199 | b->check_support_cb = ah_check_support_cb; |
| 200 | b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb; |
| 201 | return b - im->ah_backends; |
| 202 | } |
| 203 | |
| 204 | u32 |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 205 | ipsec_register_esp_backend ( |
| 206 | vlib_main_t *vm, ipsec_main_t *im, const char *name, |
| 207 | const char *esp4_encrypt_node_name, const char *esp4_encrypt_node_tun_name, |
| 208 | const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name, |
| 209 | const char *esp6_encrypt_node_name, const char *esp6_encrypt_node_tun_name, |
| 210 | const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name, |
| 211 | const char *esp_mpls_encrypt_node_tun_name, |
| 212 | check_support_cb_t esp_check_support_cb, |
| 213 | add_del_sa_sess_cb_t esp_add_del_sa_sess_cb, |
| 214 | enable_disable_cb_t enable_disable_cb) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 215 | { |
| 216 | ipsec_esp_backend_t *b; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 217 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 218 | pool_get (im->esp_backends, b); |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 219 | b->name = format (0, "%s%c", name, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 220 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 221 | ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 222 | &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 223 | ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 224 | &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 225 | ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 226 | &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 227 | ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 228 | &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index); |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 229 | ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input", |
| 230 | &b->esp4_decrypt_tun_node_index, |
| 231 | &b->esp4_decrypt_tun_next_index); |
| 232 | ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input", |
| 233 | &b->esp6_decrypt_tun_node_index, |
| 234 | &b->esp6_decrypt_tun_next_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 235 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 236 | b->esp6_encrypt_tun_node_index = |
| 237 | vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 238 | b->esp_mpls_encrypt_tun_node_index = |
| 239 | vlib_get_node_by_name (vm, (u8 *) esp_mpls_encrypt_node_tun_name)->index; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 240 | b->esp4_encrypt_tun_node_index = |
| 241 | vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 242 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 243 | b->check_support_cb = esp_check_support_cb; |
| 244 | b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 245 | b->enable_disable_cb = enable_disable_cb; |
| 246 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 247 | return b - im->esp_backends; |
| 248 | } |
| 249 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 250 | clib_error_t * |
| 251 | ipsec_rsc_in_use (ipsec_main_t * im) |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 252 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 253 | /* return an error is crypto resource are in use */ |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 254 | if (pool_elts (ipsec_sa_pool) > 0) |
| 255 | return clib_error_return (0, "%d SA entries configured", |
| 256 | pool_elts (ipsec_sa_pool)); |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 257 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 258 | return (NULL); |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 261 | int |
| 262 | ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx) |
| 263 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 264 | if (ipsec_rsc_in_use (im)) |
| 265 | return VNET_API_ERROR_RSRC_IN_USE; |
| 266 | |
| 267 | if (pool_is_free_index (im->ah_backends, backend_idx)) |
| 268 | return VNET_API_ERROR_INVALID_VALUE; |
| 269 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 270 | ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx); |
| 271 | im->ah_current_backend = backend_idx; |
| 272 | im->ah4_encrypt_node_index = b->ah4_encrypt_node_index; |
| 273 | im->ah4_decrypt_node_index = b->ah4_decrypt_node_index; |
| 274 | im->ah4_encrypt_next_index = b->ah4_encrypt_next_index; |
| 275 | im->ah4_decrypt_next_index = b->ah4_decrypt_next_index; |
| 276 | im->ah6_encrypt_node_index = b->ah6_encrypt_node_index; |
| 277 | im->ah6_decrypt_node_index = b->ah6_decrypt_node_index; |
| 278 | im->ah6_encrypt_next_index = b->ah6_encrypt_next_index; |
| 279 | im->ah6_decrypt_next_index = b->ah6_decrypt_next_index; |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 280 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | int |
| 285 | ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx) |
| 286 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 287 | if (ipsec_rsc_in_use (im)) |
| 288 | return VNET_API_ERROR_RSRC_IN_USE; |
| 289 | |
| 290 | if (pool_is_free_index (im->esp_backends, backend_idx)) |
| 291 | return VNET_API_ERROR_INVALID_VALUE; |
| 292 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 293 | /* disable current backend */ |
| 294 | if (im->esp_current_backend != ~0) |
| 295 | { |
| 296 | ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends, |
| 297 | im->esp_current_backend); |
| 298 | if (cb->enable_disable_cb) |
| 299 | { |
| 300 | if ((cb->enable_disable_cb) (0) != 0) |
| 301 | return -1; |
| 302 | } |
| 303 | } |
| 304 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 305 | ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx); |
| 306 | im->esp_current_backend = backend_idx; |
| 307 | im->esp4_encrypt_node_index = b->esp4_encrypt_node_index; |
| 308 | im->esp4_decrypt_node_index = b->esp4_decrypt_node_index; |
| 309 | im->esp4_encrypt_next_index = b->esp4_encrypt_next_index; |
| 310 | im->esp4_decrypt_next_index = b->esp4_decrypt_next_index; |
| 311 | im->esp6_encrypt_node_index = b->esp6_encrypt_node_index; |
| 312 | im->esp6_decrypt_node_index = b->esp6_decrypt_node_index; |
| 313 | im->esp6_encrypt_next_index = b->esp6_encrypt_next_index; |
| 314 | im->esp6_decrypt_next_index = b->esp6_decrypt_next_index; |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 315 | im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index; |
| 316 | im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index; |
| 317 | im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index; |
| 318 | im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 319 | im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index; |
| 320 | im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 321 | im->esp_mpls_encrypt_tun_node_index = b->esp_mpls_encrypt_tun_node_index; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 322 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 323 | if (b->enable_disable_cb) |
| 324 | { |
| 325 | if ((b->enable_disable_cb) (1) != 0) |
| 326 | return -1; |
| 327 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 331 | void |
| 332 | ipsec_set_async_mode (u32 is_enabled) |
| 333 | { |
| 334 | ipsec_main_t *im = &ipsec_main; |
| 335 | ipsec_sa_t *sa; |
| 336 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 337 | vnet_crypto_request_async_mode (is_enabled); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 338 | |
| 339 | im->async_mode = is_enabled; |
| 340 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 341 | /* change SA crypto op data */ |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 342 | pool_foreach (sa, ipsec_sa_pool) |
| 343 | { |
| 344 | sa->crypto_op_data = |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 345 | (is_enabled ? sa->async_op_data.data : sa->sync_op_data.data); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 346 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void |
| 350 | crypto_engine_backend_register_post_node (vlib_main_t * vm) |
| 351 | { |
| 352 | esp_async_post_next_t *eit; |
| 353 | esp_async_post_next_t *dit; |
| 354 | |
| 355 | eit = &esp_encrypt_async_next; |
| 356 | eit->esp4_post_next = |
| 357 | vnet_crypto_register_post_node (vm, "esp4-encrypt-post"); |
| 358 | eit->esp6_post_next = |
| 359 | vnet_crypto_register_post_node (vm, "esp6-encrypt-post"); |
| 360 | eit->esp4_tun_post_next = |
| 361 | vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post"); |
| 362 | eit->esp6_tun_post_next = |
| 363 | vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post"); |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 364 | eit->esp_mpls_tun_post_next = |
| 365 | vnet_crypto_register_post_node (vm, "esp-mpls-encrypt-tun-post"); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 366 | |
| 367 | dit = &esp_decrypt_async_next; |
| 368 | dit->esp4_post_next = |
| 369 | vnet_crypto_register_post_node (vm, "esp4-decrypt-post"); |
| 370 | dit->esp6_post_next = |
| 371 | vnet_crypto_register_post_node (vm, "esp6-decrypt-post"); |
| 372 | dit->esp4_tun_post_next = |
| 373 | vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post"); |
| 374 | dit->esp6_tun_post_next = |
| 375 | vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post"); |
| 376 | } |
| 377 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 378 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | ipsec_init (vlib_main_t * vm) |
| 380 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 381 | clib_error_t *error; |
| 382 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 383 | ipsec_main_crypto_alg_t *a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | |
Dave Barach | 0dd9165 | 2019-05-05 13:34:28 -0400 | [diff] [blame] | 385 | /* Backend registration requires the feature arcs to be set up */ |
| 386 | if ((error = vlib_call_init_function (vm, vnet_feature_init))) |
| 387 | return (error); |
| 388 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 389 | im->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | im->vlib_main = vm; |
| 391 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 392 | im->spd_index_by_spd_id = hash_create (0, sizeof (uword)); |
| 393 | im->sa_index_by_sa_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
| 395 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 396 | vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 397 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | im->error_drop_node_index = node->index; |
| 399 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 400 | im->ah_current_backend = ~0; |
| 401 | im->esp_current_backend = ~0; |
| 402 | |
Neale Ranns | d1a5b2d | 2019-05-16 17:09:28 +0000 | [diff] [blame] | 403 | u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 404 | "ah4-encrypt", |
| 405 | "ah4-decrypt", |
| 406 | "ah6-encrypt", |
| 407 | "ah6-decrypt", |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 408 | ipsec_check_ah_support, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 409 | NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 411 | im->ah_default_backend = idx; |
| 412 | int rv = ipsec_select_ah_backend (im, idx); |
| 413 | ASSERT (0 == rv); |
| 414 | (void) (rv); // avoid warning |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 415 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 416 | idx = ipsec_register_esp_backend ( |
| 417 | vm, im, "crypto engine backend", "esp4-encrypt", "esp4-encrypt-tun", |
| 418 | "esp4-decrypt", "esp4-decrypt-tun", "esp6-encrypt", "esp6-encrypt-tun", |
| 419 | "esp6-decrypt", "esp6-decrypt-tun", "esp-mpls-encrypt-tun", |
| 420 | ipsec_check_esp_support, NULL, crypto_dispatch_enable_disable); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 421 | im->esp_default_backend = idx; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 422 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 423 | rv = ipsec_select_esp_backend (im, idx); |
| 424 | ASSERT (0 == rv); |
| 425 | (void) (rv); // avoid warning |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | if ((error = vlib_call_init_function (vm, ipsec_cli_init))) |
| 428 | return error; |
| 429 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 430 | vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1); |
| 431 | |
Neale Ranns | 2cdcd0c | 2019-08-27 12:26:14 +0000 | [diff] [blame] | 432 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE; |
| 433 | a->enc_op_id = VNET_CRYPTO_OP_NONE; |
| 434 | a->dec_op_id = VNET_CRYPTO_OP_NONE; |
| 435 | a->alg = VNET_CRYPTO_ALG_NONE; |
| 436 | a->iv_size = 0; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 437 | a->block_align = 1; |
Neale Ranns | 2cdcd0c | 2019-08-27 12:26:14 +0000 | [diff] [blame] | 438 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 439 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 440 | a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC; |
| 441 | a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 442 | a->alg = VNET_CRYPTO_ALG_DES_CBC; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 443 | a->iv_size = a->block_align = 8; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 444 | |
| 445 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 446 | a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC; |
| 447 | a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 448 | a->alg = VNET_CRYPTO_ALG_3DES_CBC; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 449 | a->iv_size = a->block_align = 8; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 450 | |
| 451 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 452 | a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC; |
| 453 | a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 454 | a->alg = VNET_CRYPTO_ALG_AES_128_CBC; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 455 | a->iv_size = a->block_align = 16; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 456 | |
| 457 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 458 | a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC; |
| 459 | a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 460 | a->alg = VNET_CRYPTO_ALG_AES_192_CBC; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 461 | a->iv_size = a->block_align = 16; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 462 | |
| 463 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 464 | a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC; |
| 465 | a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 466 | a->alg = VNET_CRYPTO_ALG_AES_256_CBC; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 467 | a->iv_size = a->block_align = 16; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 468 | |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 469 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128; |
| 470 | a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC; |
| 471 | a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC; |
| 472 | a->alg = VNET_CRYPTO_ALG_AES_128_CTR; |
| 473 | a->iv_size = 8; |
| 474 | a->block_align = 1; |
| 475 | |
| 476 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192; |
| 477 | a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC; |
| 478 | a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC; |
| 479 | a->alg = VNET_CRYPTO_ALG_AES_192_CTR; |
| 480 | a->iv_size = 8; |
| 481 | a->block_align = 1; |
| 482 | |
| 483 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256; |
| 484 | a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC; |
| 485 | a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC; |
| 486 | a->alg = VNET_CRYPTO_ALG_AES_256_CTR; |
| 487 | a->iv_size = 8; |
| 488 | a->block_align = 1; |
| 489 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 490 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128; |
| 491 | a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC; |
| 492 | a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 493 | a->alg = VNET_CRYPTO_ALG_AES_128_GCM; |
Damjan Marion | f1ecb65 | 2020-02-10 19:21:14 +0100 | [diff] [blame] | 494 | a->iv_size = 8; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 495 | a->block_align = 1; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 496 | a->icv_size = 16; |
| 497 | |
| 498 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192; |
| 499 | a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC; |
| 500 | a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 501 | a->alg = VNET_CRYPTO_ALG_AES_192_GCM; |
Damjan Marion | f1ecb65 | 2020-02-10 19:21:14 +0100 | [diff] [blame] | 502 | a->iv_size = 8; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 503 | a->block_align = 1; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 504 | a->icv_size = 16; |
| 505 | |
| 506 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256; |
| 507 | a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC; |
| 508 | a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 509 | a->alg = VNET_CRYPTO_ALG_AES_256_GCM; |
Damjan Marion | f1ecb65 | 2020-02-10 19:21:14 +0100 | [diff] [blame] | 510 | a->iv_size = 8; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 511 | a->block_align = 1; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 512 | a->icv_size = 16; |
| 513 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 514 | vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1); |
| 515 | ipsec_main_integ_alg_t *i; |
| 516 | |
Dmitry Vakhrushev | 77cc14a | 2019-08-14 00:12:33 -0400 | [diff] [blame] | 517 | i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96]; |
| 518 | i->op_id = VNET_CRYPTO_OP_MD5_HMAC; |
| 519 | i->alg = VNET_CRYPTO_ALG_HMAC_MD5; |
| 520 | i->icv_size = 12; |
| 521 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 522 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 523 | i->op_id = VNET_CRYPTO_OP_SHA1_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 524 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA1; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 525 | i->icv_size = 12; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 526 | |
| 527 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 528 | i->op_id = VNET_CRYPTO_OP_SHA1_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 529 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA256; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 530 | i->icv_size = 12; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 531 | |
| 532 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 533 | i->op_id = VNET_CRYPTO_OP_SHA256_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 534 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA256; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 535 | i->icv_size = 16; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 536 | |
| 537 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 538 | i->op_id = VNET_CRYPTO_OP_SHA384_HMAC; |
Damjan Marion | 4cb8381 | 2019-04-24 17:32:01 +0200 | [diff] [blame] | 539 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA384; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 540 | i->icv_size = 24; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 541 | |
| 542 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 543 | i->op_id = VNET_CRYPTO_OP_SHA512_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 544 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA512; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 545 | i->icv_size = 32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
Neale Ranns | 5ae793a | 2019-04-03 13:36:56 +0000 | [diff] [blame] | 547 | vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 548 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 549 | im->async_mode = 0; |
| 550 | crypto_engine_backend_register_post_node (vm); |
| 551 | |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 552 | im->ipsec4_out_spd_hash_tbl = NULL; |
| 553 | im->flow_cache_flag = 0; |
| 554 | im->ipsec4_out_spd_flow_cache_entries = 0; |
| 555 | im->epoch_count = 0; |
| 556 | im->ipsec4_out_spd_hash_num_buckets = |
| 557 | IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS; |
| 558 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | VLIB_INIT_FUNCTION (ipsec_init); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 563 | |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 564 | static clib_error_t * |
| 565 | ipsec_config (vlib_main_t *vm, unformat_input_t *input) |
| 566 | { |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 567 | ipsec_main_t *im = &ipsec_main; |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 568 | unformat_input_t sub_input; |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 569 | u32 ipsec4_out_spd_hash_num_buckets; |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 570 | |
| 571 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 572 | { |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 573 | if (unformat (input, "ipv4-outbound-spd-flow-cache on")) |
| 574 | im->flow_cache_flag = 1; |
| 575 | else if (unformat (input, "ipv4-outbound-spd-flow-cache off")) |
| 576 | im->flow_cache_flag = 0; |
| 577 | else if (unformat (input, "ipv4-outbound-spd-hash-buckets %d", |
| 578 | &ipsec4_out_spd_hash_num_buckets)) |
| 579 | { |
| 580 | /* Size of hash is power of 2 >= number of buckets */ |
| 581 | im->ipsec4_out_spd_hash_num_buckets = |
| 582 | 1ULL << max_log2 (ipsec4_out_spd_hash_num_buckets); |
| 583 | } |
| 584 | else if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input, |
| 585 | &sub_input)) |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 586 | { |
| 587 | uword table_size = ~0; |
| 588 | u32 n_buckets = ~0; |
| 589 | |
| 590 | while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT) |
| 591 | { |
| 592 | if (unformat (&sub_input, "num-buckets %u", &n_buckets)) |
| 593 | ; |
| 594 | else |
| 595 | return clib_error_return (0, "unknown input `%U'", |
| 596 | format_unformat_error, &sub_input); |
| 597 | } |
| 598 | |
| 599 | ipsec_tun_table_init (AF_IP4, table_size, n_buckets); |
| 600 | } |
| 601 | else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input, |
| 602 | &sub_input)) |
| 603 | { |
| 604 | uword table_size = ~0; |
| 605 | u32 n_buckets = ~0; |
| 606 | |
| 607 | while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT) |
| 608 | { |
| 609 | if (unformat (&sub_input, "num-buckets %u", &n_buckets)) |
| 610 | ; |
| 611 | else |
| 612 | return clib_error_return (0, "unknown input `%U'", |
| 613 | format_unformat_error, &sub_input); |
| 614 | } |
| 615 | |
| 616 | ipsec_tun_table_init (AF_IP6, table_size, n_buckets); |
| 617 | } |
| 618 | else |
| 619 | return clib_error_return (0, "unknown input `%U'", |
| 620 | format_unformat_error, input); |
| 621 | } |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 622 | if (im->flow_cache_flag) |
| 623 | { |
| 624 | vec_add2 (im->ipsec4_out_spd_hash_tbl, im->ipsec4_out_spd_hash_tbl, |
| 625 | im->ipsec4_out_spd_hash_num_buckets); |
| 626 | } |
Zachary Leaf | b2d3678 | 2021-07-27 05:18:47 -0500 | [diff] [blame] | 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec"); |
| 632 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 633 | /* |
| 634 | * fd.io coding-style-patch-verification: ON |
| 635 | * |
| 636 | * Local Variables: |
| 637 | * eval: (c-set-style "gnu") |
| 638 | * End: |
| 639 | */ |