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> |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 22 | #include <vnet/udp/udp.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> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 27 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 28 | ipsec_main_t ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | |
| 30 | static clib_error_t * |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 31 | ipsec_check_ah_support (ipsec_sa_t * sa) |
| 32 | { |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 33 | ipsec_main_t *im = &ipsec_main; |
| 34 | |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 35 | if (sa->integ_alg == IPSEC_INTEG_ALG_NONE) |
| 36 | return clib_error_return (0, "unsupported none integ-alg"); |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 37 | |
| 38 | if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg)) |
| 39 | return clib_error_return (0, "No crypto engine support for %U", |
| 40 | format_ipsec_integ_alg, sa->integ_alg); |
| 41 | |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static clib_error_t * |
| 46 | ipsec_check_esp_support (ipsec_sa_t * sa) |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 47 | { |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 48 | ipsec_main_t *im = &ipsec_main; |
| 49 | |
| 50 | if (IPSEC_INTEG_ALG_NONE != sa->integ_alg) |
| 51 | { |
| 52 | if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg)) |
| 53 | return clib_error_return (0, "No crypto engine support for %U", |
| 54 | format_ipsec_integ_alg, sa->integ_alg); |
| 55 | } |
| 56 | if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg) |
| 57 | { |
| 58 | if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg)) |
| 59 | return clib_error_return (0, "No crypto engine support for %U", |
| 60 | format_ipsec_crypto_alg, sa->crypto_alg); |
| 61 | } |
| 62 | |
| 63 | return (0); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 66 | clib_error_t * |
| 67 | ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add) |
| 68 | { |
| 69 | ipsec_ah_backend_t *ah = |
| 70 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 71 | if (ah->add_del_sa_sess_cb) |
| 72 | { |
| 73 | clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add); |
| 74 | if (err) |
| 75 | return err; |
| 76 | } |
| 77 | ipsec_esp_backend_t *esp = |
| 78 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 79 | if (esp->add_del_sa_sess_cb) |
| 80 | { |
| 81 | clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add); |
| 82 | if (err) |
| 83 | return err; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | clib_error_t * |
| 89 | ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa) |
| 90 | { |
| 91 | clib_error_t *error = 0; |
Matthew Smith | 461caa5 | 2018-12-21 11:53:16 -0600 | [diff] [blame] | 92 | |
| 93 | if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH)) |
| 94 | { |
| 95 | ipsec_ah_backend_t *ah = |
| 96 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 97 | ASSERT (ah->check_support_cb); |
| 98 | error = ah->check_support_cb (sa); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | ipsec_esp_backend_t *esp = |
| 103 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 104 | ASSERT (esp->check_support_cb); |
| 105 | error = esp->check_support_cb (sa); |
| 106 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 107 | return error; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static void |
| 112 | ipsec_add_node (vlib_main_t * vm, const char *node_name, |
| 113 | const char *prev_node_name, u32 * out_node_index, |
| 114 | u32 * out_next_index) |
| 115 | { |
| 116 | vlib_node_t *prev_node, *node; |
| 117 | prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name); |
| 118 | ASSERT (prev_node); |
| 119 | node = vlib_get_node_by_name (vm, (u8 *) node_name); |
| 120 | ASSERT (node); |
| 121 | *out_node_index = node->index; |
| 122 | *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index); |
| 123 | } |
| 124 | |
Matthew Smith | 401aedf | 2019-07-08 14:45:04 -0500 | [diff] [blame] | 125 | void |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 126 | ipsec_add_feature (const char *arc_name, |
| 127 | const char *node_name, u32 * out_feature_index) |
| 128 | { |
| 129 | u8 arc; |
| 130 | |
| 131 | arc = vnet_get_feature_arc_index (arc_name); |
Neale Ranns | 0224514 | 2019-05-13 23:07:43 -0700 | [diff] [blame] | 132 | ASSERT (arc != (u8) ~ 0); |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 133 | *out_feature_index = vnet_get_feature_index (arc, node_name); |
| 134 | } |
| 135 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 136 | u32 |
| 137 | ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 138 | const char *name, |
| 139 | const char *ah4_encrypt_node_name, |
| 140 | const char *ah4_decrypt_node_name, |
| 141 | const char *ah6_encrypt_node_name, |
| 142 | const char *ah6_decrypt_node_name, |
| 143 | check_support_cb_t ah_check_support_cb, |
| 144 | add_del_sa_sess_cb_t ah_add_del_sa_sess_cb) |
| 145 | { |
| 146 | ipsec_ah_backend_t *b; |
| 147 | pool_get (im->ah_backends, b); |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 148 | b->name = format (0, "%s%c", name, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 149 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 150 | ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 151 | &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 152 | ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 153 | &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 154 | ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 155 | &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 156 | ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 157 | &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index); |
| 158 | |
| 159 | b->check_support_cb = ah_check_support_cb; |
| 160 | b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb; |
| 161 | return b - im->ah_backends; |
| 162 | } |
| 163 | |
| 164 | u32 |
| 165 | ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 166 | const char *name, |
| 167 | const char *esp4_encrypt_node_name, |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 168 | const char *esp4_encrypt_node_tun_name, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 169 | const char *esp4_decrypt_node_name, |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 170 | const char *esp4_decrypt_tun_node_name, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 171 | const char *esp6_encrypt_node_name, |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 172 | const char *esp6_encrypt_node_tun_name, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 173 | const char *esp6_decrypt_node_name, |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 174 | const char *esp6_decrypt_tun_node_name, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 175 | check_support_cb_t esp_check_support_cb, |
| 176 | add_del_sa_sess_cb_t esp_add_del_sa_sess_cb) |
| 177 | { |
| 178 | ipsec_esp_backend_t *b; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 179 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 180 | pool_get (im->esp_backends, b); |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 181 | b->name = format (0, "%s%c", name, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 182 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 183 | ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 184 | &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 185 | ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 186 | &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 187 | ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 188 | &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 189 | ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 190 | &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index); |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 191 | ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input", |
| 192 | &b->esp4_decrypt_tun_node_index, |
| 193 | &b->esp4_decrypt_tun_next_index); |
| 194 | ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input", |
| 195 | &b->esp6_decrypt_tun_node_index, |
| 196 | &b->esp6_decrypt_tun_next_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 197 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 198 | ipsec_add_feature ("ip4-output", esp4_encrypt_node_tun_name, |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 199 | &b->esp44_encrypt_tun_feature_index); |
| 200 | ipsec_add_feature ("ip4-output", esp6_encrypt_node_tun_name, |
| 201 | &b->esp46_encrypt_tun_feature_index); |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 202 | ipsec_add_feature ("ip6-output", esp6_encrypt_node_tun_name, |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 203 | &b->esp66_encrypt_tun_feature_index); |
| 204 | ipsec_add_feature ("ip6-output", esp4_encrypt_node_tun_name, |
| 205 | &b->esp64_encrypt_tun_feature_index); |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 206 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 207 | b->check_support_cb = esp_check_support_cb; |
| 208 | b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb; |
| 209 | return b - im->esp_backends; |
| 210 | } |
| 211 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 212 | clib_error_t * |
| 213 | ipsec_rsc_in_use (ipsec_main_t * im) |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 214 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 215 | /* return an error is crypto resource are in use */ |
| 216 | if (pool_elts (im->sad) > 0) |
| 217 | return clib_error_return (0, |
| 218 | "%d SA entries configured", |
| 219 | pool_elts (im->sad)); |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 220 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 221 | return (NULL); |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 224 | int |
| 225 | ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx) |
| 226 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 227 | if (ipsec_rsc_in_use (im)) |
| 228 | return VNET_API_ERROR_RSRC_IN_USE; |
| 229 | |
| 230 | if (pool_is_free_index (im->ah_backends, backend_idx)) |
| 231 | return VNET_API_ERROR_INVALID_VALUE; |
| 232 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 233 | ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx); |
| 234 | im->ah_current_backend = backend_idx; |
| 235 | im->ah4_encrypt_node_index = b->ah4_encrypt_node_index; |
| 236 | im->ah4_decrypt_node_index = b->ah4_decrypt_node_index; |
| 237 | im->ah4_encrypt_next_index = b->ah4_encrypt_next_index; |
| 238 | im->ah4_decrypt_next_index = b->ah4_decrypt_next_index; |
| 239 | im->ah6_encrypt_node_index = b->ah6_encrypt_node_index; |
| 240 | im->ah6_decrypt_node_index = b->ah6_decrypt_node_index; |
| 241 | im->ah6_encrypt_next_index = b->ah6_encrypt_next_index; |
| 242 | im->ah6_decrypt_next_index = b->ah6_decrypt_next_index; |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 243 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int |
| 248 | ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx) |
| 249 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 250 | if (ipsec_rsc_in_use (im)) |
| 251 | return VNET_API_ERROR_RSRC_IN_USE; |
| 252 | |
| 253 | if (pool_is_free_index (im->esp_backends, backend_idx)) |
| 254 | return VNET_API_ERROR_INVALID_VALUE; |
| 255 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 256 | ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx); |
| 257 | im->esp_current_backend = backend_idx; |
| 258 | im->esp4_encrypt_node_index = b->esp4_encrypt_node_index; |
| 259 | im->esp4_decrypt_node_index = b->esp4_decrypt_node_index; |
| 260 | im->esp4_encrypt_next_index = b->esp4_encrypt_next_index; |
| 261 | im->esp4_decrypt_next_index = b->esp4_decrypt_next_index; |
| 262 | im->esp6_encrypt_node_index = b->esp6_encrypt_node_index; |
| 263 | im->esp6_decrypt_node_index = b->esp6_decrypt_node_index; |
| 264 | im->esp6_encrypt_next_index = b->esp6_encrypt_next_index; |
| 265 | im->esp6_decrypt_next_index = b->esp6_decrypt_next_index; |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 266 | im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index; |
| 267 | im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index; |
| 268 | im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index; |
| 269 | im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index; |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 270 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 271 | im->esp44_encrypt_tun_feature_index = b->esp44_encrypt_tun_feature_index; |
| 272 | im->esp64_encrypt_tun_feature_index = b->esp64_encrypt_tun_feature_index; |
| 273 | im->esp46_encrypt_tun_feature_index = b->esp46_encrypt_tun_feature_index; |
| 274 | im->esp66_encrypt_tun_feature_index = b->esp66_encrypt_tun_feature_index; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 275 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 279 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | ipsec_init (vlib_main_t * vm) |
| 281 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 282 | clib_error_t *error; |
| 283 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 284 | ipsec_main_crypto_alg_t *a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
Dave Barach | 0dd9165 | 2019-05-05 13:34:28 -0400 | [diff] [blame] | 286 | /* Backend registration requires the feature arcs to be set up */ |
| 287 | if ((error = vlib_call_init_function (vm, vnet_feature_init))) |
| 288 | return (error); |
| 289 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 290 | im->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | im->vlib_main = vm; |
| 292 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 293 | im->spd_index_by_spd_id = hash_create (0, sizeof (uword)); |
| 294 | im->sa_index_by_sa_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
| 296 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 297 | 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] | 298 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | im->error_drop_node_index = node->index; |
| 300 | |
Neale Ranns | d1a5b2d | 2019-05-16 17:09:28 +0000 | [diff] [blame] | 301 | u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 302 | "ah4-encrypt", |
| 303 | "ah4-decrypt", |
| 304 | "ah6-encrypt", |
| 305 | "ah6-decrypt", |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 306 | ipsec_check_ah_support, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 307 | NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 309 | im->ah_default_backend = idx; |
| 310 | int rv = ipsec_select_ah_backend (im, idx); |
| 311 | ASSERT (0 == rv); |
| 312 | (void) (rv); // avoid warning |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 313 | |
Neale Ranns | d1a5b2d | 2019-05-16 17:09:28 +0000 | [diff] [blame] | 314 | idx = ipsec_register_esp_backend (vm, im, "crypto engine backend", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 315 | "esp4-encrypt", |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 316 | "esp4-encrypt-tun", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 317 | "esp4-decrypt", |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 318 | "esp4-decrypt-tun", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 319 | "esp6-encrypt", |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 320 | "esp6-encrypt-tun", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 321 | "esp6-decrypt", |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 322 | "esp6-decrypt-tun", |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 323 | ipsec_check_esp_support, NULL); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 324 | im->esp_default_backend = idx; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 325 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 326 | rv = ipsec_select_esp_backend (im, idx); |
| 327 | ASSERT (0 == rv); |
| 328 | (void) (rv); // avoid warning |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | if ((error = vlib_call_init_function (vm, ipsec_cli_init))) |
| 331 | return error; |
| 332 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 333 | vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1); |
| 334 | |
Neale Ranns | 2cdcd0c | 2019-08-27 12:26:14 +0000 | [diff] [blame] | 335 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE; |
| 336 | a->enc_op_id = VNET_CRYPTO_OP_NONE; |
| 337 | a->dec_op_id = VNET_CRYPTO_OP_NONE; |
| 338 | a->alg = VNET_CRYPTO_ALG_NONE; |
| 339 | a->iv_size = 0; |
| 340 | a->block_size = 1; |
| 341 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 342 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 343 | a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC; |
| 344 | a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 345 | a->alg = VNET_CRYPTO_ALG_DES_CBC; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 346 | a->iv_size = a->block_size = 8; |
| 347 | |
| 348 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 349 | a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC; |
| 350 | a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 351 | a->alg = VNET_CRYPTO_ALG_3DES_CBC; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 352 | a->iv_size = a->block_size = 8; |
| 353 | |
| 354 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 355 | a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC; |
| 356 | a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 357 | a->alg = VNET_CRYPTO_ALG_AES_128_CBC; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 358 | a->iv_size = a->block_size = 16; |
| 359 | |
| 360 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 361 | a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC; |
| 362 | a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 363 | a->alg = VNET_CRYPTO_ALG_AES_192_CBC; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 364 | a->iv_size = a->block_size = 16; |
| 365 | |
| 366 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 367 | a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC; |
| 368 | a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 369 | a->alg = VNET_CRYPTO_ALG_AES_256_CBC; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 370 | a->iv_size = a->block_size = 16; |
| 371 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 372 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128; |
| 373 | a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC; |
| 374 | a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 375 | a->alg = VNET_CRYPTO_ALG_AES_128_GCM; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 376 | a->iv_size = a->block_size = 8; |
| 377 | a->icv_size = 16; |
| 378 | |
| 379 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192; |
| 380 | a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC; |
| 381 | a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 382 | a->alg = VNET_CRYPTO_ALG_AES_192_GCM; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 383 | a->iv_size = a->block_size = 8; |
| 384 | a->icv_size = 16; |
| 385 | |
| 386 | a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256; |
| 387 | a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC; |
| 388 | a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 389 | a->alg = VNET_CRYPTO_ALG_AES_256_GCM; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 390 | a->iv_size = a->block_size = 8; |
| 391 | a->icv_size = 16; |
| 392 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 393 | vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1); |
| 394 | ipsec_main_integ_alg_t *i; |
| 395 | |
Dmitry Vakhrushev | 77cc14a | 2019-08-14 00:12:33 -0400 | [diff] [blame] | 396 | i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96]; |
| 397 | i->op_id = VNET_CRYPTO_OP_MD5_HMAC; |
| 398 | i->alg = VNET_CRYPTO_ALG_HMAC_MD5; |
| 399 | i->icv_size = 12; |
| 400 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 401 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 402 | i->op_id = VNET_CRYPTO_OP_SHA1_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 403 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA1; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 404 | i->icv_size = 12; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 405 | |
| 406 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 407 | i->op_id = VNET_CRYPTO_OP_SHA1_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 408 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA256; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 409 | i->icv_size = 12; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 410 | |
| 411 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 412 | i->op_id = VNET_CRYPTO_OP_SHA256_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 413 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA256; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 414 | i->icv_size = 16; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 415 | |
| 416 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 417 | i->op_id = VNET_CRYPTO_OP_SHA384_HMAC; |
Damjan Marion | 4cb8381 | 2019-04-24 17:32:01 +0200 | [diff] [blame] | 418 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA384; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 419 | i->icv_size = 24; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 420 | |
| 421 | i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 422 | i->op_id = VNET_CRYPTO_OP_SHA512_HMAC; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 423 | i->alg = VNET_CRYPTO_ALG_HMAC_SHA512; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 424 | i->icv_size = 32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | |
Neale Ranns | 5ae793a | 2019-04-03 13:36:56 +0000 | [diff] [blame] | 426 | vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 427 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 428 | im->ah4_enc_fq_index = |
| 429 | vlib_frame_queue_main_init (ah4_encrypt_node.index, 0); |
| 430 | im->ah4_dec_fq_index = |
| 431 | vlib_frame_queue_main_init (ah4_decrypt_node.index, 0); |
| 432 | im->ah6_enc_fq_index = |
| 433 | vlib_frame_queue_main_init (ah6_encrypt_node.index, 0); |
| 434 | im->ah6_dec_fq_index = |
| 435 | vlib_frame_queue_main_init (ah6_decrypt_node.index, 0); |
| 436 | |
| 437 | im->esp4_enc_fq_index = |
| 438 | vlib_frame_queue_main_init (esp4_encrypt_node.index, 0); |
| 439 | im->esp4_dec_fq_index = |
| 440 | vlib_frame_queue_main_init (esp4_decrypt_node.index, 0); |
| 441 | im->esp6_enc_fq_index = |
| 442 | vlib_frame_queue_main_init (esp6_encrypt_node.index, 0); |
| 443 | im->esp6_dec_fq_index = |
| 444 | vlib_frame_queue_main_init (esp6_decrypt_node.index, 0); |
| 445 | im->esp4_enc_tun_fq_index = |
| 446 | vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0); |
| 447 | im->esp6_enc_tun_fq_index = |
| 448 | vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0); |
| 449 | im->esp4_dec_tun_fq_index = |
| 450 | vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0); |
| 451 | im->esp6_dec_tun_fq_index = |
| 452 | vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0); |
| 453 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | VLIB_INIT_FUNCTION (ipsec_init); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * fd.io coding-style-patch-verification: ON |
| 461 | * |
| 462 | * Local Variables: |
| 463 | * eval: (c-set-style "gnu") |
| 464 | * End: |
| 465 | */ |