blob: 5cc8044e3d4d99cf0d184a310ce17e04c2a19669 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Neale Ranns999c8ee2019-02-01 03:31:24 -08002 * ipsec.c : IPSEC module functions
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 *
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 Corasb040f982020-10-20 14:59:43 -070022#include <vnet/udp/udp_local.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000025#include <vnet/ipsec/esp.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080026#include <vnet/ipsec/ah.h>
Zachary Leafb2d36782021-07-27 05:18:47 -050027#include <vnet/ipsec/ipsec_tun.h>
Neale Ranns6fdcc3d2021-10-08 07:30:47 +000028#include <vnet/ipsec/ipsec_itf.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000029
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +000030/* Flow cache is sized for 1 million flows with a load factor of .25.
31 */
32#define IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS (1 << 22)
33
Dave Wallace71612d62017-10-24 01:32:41 -040034ipsec_main_t ipsec_main;
Fan Zhangf5395782020-04-29 14:00:03 +010035esp_async_post_next_t esp_encrypt_async_next;
36esp_async_post_next_t esp_decrypt_async_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
38static clib_error_t *
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010039ipsec_check_ah_support (ipsec_sa_t * sa)
40{
Neale Rannsece2ae02019-06-21 12:44:11 +000041 ipsec_main_t *im = &ipsec_main;
42
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010043 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
44 return clib_error_return (0, "unsupported none integ-alg");
Neale Rannsece2ae02019-06-21 12:44:11 +000045
46 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
47 return clib_error_return (0, "No crypto engine support for %U",
48 format_ipsec_integ_alg, sa->integ_alg);
49
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010050 return 0;
51}
52
53static clib_error_t *
54ipsec_check_esp_support (ipsec_sa_t * sa)
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000055{
Neale Rannsece2ae02019-06-21 12:44:11 +000056 ipsec_main_t *im = &ipsec_main;
57
58 if (IPSEC_INTEG_ALG_NONE != sa->integ_alg)
59 {
60 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
61 return clib_error_return (0, "No crypto engine support for %U",
62 format_ipsec_integ_alg, sa->integ_alg);
63 }
64 if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg)
65 {
66 if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg))
67 return clib_error_return (0, "No crypto engine support for %U",
68 format_ipsec_crypto_alg, sa->crypto_alg);
69 }
70
71 return (0);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000072}
73
Klement Sekerab4d30532018-11-08 13:00:02 +010074clib_error_t *
75ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
76{
77 ipsec_ah_backend_t *ah =
78 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
79 if (ah->add_del_sa_sess_cb)
80 {
81 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
82 if (err)
83 return err;
84 }
85 ipsec_esp_backend_t *esp =
86 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
87 if (esp->add_del_sa_sess_cb)
88 {
89 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
90 if (err)
91 return err;
92 }
93 return 0;
94}
95
96clib_error_t *
97ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
98{
99 clib_error_t *error = 0;
Matthew Smith461caa52018-12-21 11:53:16 -0600100
101 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
102 {
103 ipsec_ah_backend_t *ah =
104 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
105 ASSERT (ah->check_support_cb);
106 error = ah->check_support_cb (sa);
107 }
108 else
109 {
110 ipsec_esp_backend_t *esp =
111 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
112 ASSERT (esp->check_support_cb);
113 error = esp->check_support_cb (sa);
114 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100115 return error;
116}
117
118
119static void
120ipsec_add_node (vlib_main_t * vm, const char *node_name,
121 const char *prev_node_name, u32 * out_node_index,
122 u32 * out_next_index)
123{
124 vlib_node_t *prev_node, *node;
125 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
126 ASSERT (prev_node);
127 node = vlib_get_node_by_name (vm, (u8 *) node_name);
128 ASSERT (node);
129 *out_node_index = node->index;
130 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
131}
132
Neale Rannsabc56602020-04-01 09:45:23 +0000133void
Neale Rannsabc56602020-04-01 09:45:23 +0000134ipsec_unregister_udp_port (u16 port)
135{
136 ipsec_main_t *im = &ipsec_main;
137 u32 n_regs;
138 uword *p;
139
140 p = hash_get (im->udp_port_registrations, port);
141
142 ASSERT (p);
143
144 n_regs = p[0];
145
146 if (0 == --n_regs)
147 {
148 udp_unregister_dst_port (vlib_get_main (), port, 1);
149 hash_unset (im->udp_port_registrations, port);
150 }
151 else
152 {
153 hash_unset (im->udp_port_registrations, port);
154 hash_set (im->udp_port_registrations, port, n_regs);
155 }
156}
157
158void
159ipsec_register_udp_port (u16 port)
160{
161 ipsec_main_t *im = &ipsec_main;
162 u32 n_regs;
163 uword *p;
164
165 p = hash_get (im->udp_port_registrations, port);
166
167 n_regs = (p ? p[0] : 0);
168
169 if (0 == n_regs++)
170 udp_register_dst_port (vlib_get_main (), port,
171 ipsec4_tun_input_node.index, 1);
172
173 hash_unset (im->udp_port_registrations, port);
174 hash_set (im->udp_port_registrations, port, n_regs);
175}
176
Klement Sekerab4d30532018-11-08 13:00:02 +0100177u32
178ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
179 const char *name,
180 const char *ah4_encrypt_node_name,
181 const char *ah4_decrypt_node_name,
182 const char *ah6_encrypt_node_name,
183 const char *ah6_decrypt_node_name,
184 check_support_cb_t ah_check_support_cb,
185 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
186{
187 ipsec_ah_backend_t *b;
188 pool_get (im->ah_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500189 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100190
Pierre Pfister057b3562018-12-10 17:01:01 +0100191 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100192 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100193 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100194 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100195 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100196 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100197 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100198 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
199
200 b->check_support_cb = ah_check_support_cb;
201 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
202 return b - im->ah_backends;
203}
204
205u32
Neale Ranns4a58e492020-12-21 13:19:10 +0000206ipsec_register_esp_backend (
207 vlib_main_t *vm, ipsec_main_t *im, const char *name,
208 const char *esp4_encrypt_node_name, const char *esp4_encrypt_node_tun_name,
209 const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name,
210 const char *esp6_encrypt_node_name, const char *esp6_encrypt_node_tun_name,
211 const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name,
212 const char *esp_mpls_encrypt_node_tun_name,
213 check_support_cb_t esp_check_support_cb,
214 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
215 enable_disable_cb_t enable_disable_cb)
Klement Sekerab4d30532018-11-08 13:00:02 +0100216{
217 ipsec_esp_backend_t *b;
Neale Rannse8915fc2019-04-23 20:57:55 -0400218
Klement Sekerab4d30532018-11-08 13:00:02 +0100219 pool_get (im->esp_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500220 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100221
Pierre Pfister057b3562018-12-10 17:01:01 +0100222 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100223 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100224 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100225 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100226 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100227 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100228 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100229 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
Neale Ranns7ec120e2020-01-21 04:58:02 +0000230 ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
231 &b->esp4_decrypt_tun_node_index,
232 &b->esp4_decrypt_tun_next_index);
233 ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
234 &b->esp6_decrypt_tun_node_index,
235 &b->esp6_decrypt_tun_next_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100236
Neale Ranns4ec36c52020-03-31 09:21:29 -0400237 b->esp6_encrypt_tun_node_index =
238 vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index;
Neale Ranns4a58e492020-12-21 13:19:10 +0000239 b->esp_mpls_encrypt_tun_node_index =
240 vlib_get_node_by_name (vm, (u8 *) esp_mpls_encrypt_node_tun_name)->index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400241 b->esp4_encrypt_tun_node_index =
242 vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400243
Klement Sekerab4d30532018-11-08 13:00:02 +0100244 b->check_support_cb = esp_check_support_cb;
245 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
Fan Zhangf5395782020-04-29 14:00:03 +0100246 b->enable_disable_cb = enable_disable_cb;
247
Klement Sekerab4d30532018-11-08 13:00:02 +0100248 return b - im->esp_backends;
249}
250
Neale Rannse8915fc2019-04-23 20:57:55 -0400251clib_error_t *
252ipsec_rsc_in_use (ipsec_main_t * im)
Neale Rannsb4cfd552019-02-13 02:08:06 -0800253{
Neale Rannse8915fc2019-04-23 20:57:55 -0400254 /* return an error is crypto resource are in use */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000255 if (pool_elts (ipsec_sa_pool) > 0)
256 return clib_error_return (0, "%d SA entries configured",
257 pool_elts (ipsec_sa_pool));
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000258 if (ipsec_itf_count () > 0)
259 return clib_error_return (0, "%d IPSec interface configured",
260 ipsec_itf_count ());
Neale Rannsb4cfd552019-02-13 02:08:06 -0800261
Neale Rannse8915fc2019-04-23 20:57:55 -0400262 return (NULL);
Neale Rannsb4cfd552019-02-13 02:08:06 -0800263}
264
Klement Sekerab4d30532018-11-08 13:00:02 +0100265int
266ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
267{
Neale Rannse8915fc2019-04-23 20:57:55 -0400268 if (ipsec_rsc_in_use (im))
269 return VNET_API_ERROR_RSRC_IN_USE;
270
271 if (pool_is_free_index (im->ah_backends, backend_idx))
272 return VNET_API_ERROR_INVALID_VALUE;
273
Klement Sekerab4d30532018-11-08 13:00:02 +0100274 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
275 im->ah_current_backend = backend_idx;
276 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
277 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
278 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
279 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
280 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
281 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
282 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
283 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800284
Klement Sekerab4d30532018-11-08 13:00:02 +0100285 return 0;
286}
287
288int
289ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
290{
Neale Rannse8915fc2019-04-23 20:57:55 -0400291 if (ipsec_rsc_in_use (im))
292 return VNET_API_ERROR_RSRC_IN_USE;
293
294 if (pool_is_free_index (im->esp_backends, backend_idx))
295 return VNET_API_ERROR_INVALID_VALUE;
296
Fan Zhangf5395782020-04-29 14:00:03 +0100297 /* disable current backend */
298 if (im->esp_current_backend != ~0)
299 {
300 ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
301 im->esp_current_backend);
302 if (cb->enable_disable_cb)
303 {
304 if ((cb->enable_disable_cb) (0) != 0)
305 return -1;
306 }
307 }
308
Klement Sekerab4d30532018-11-08 13:00:02 +0100309 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
310 im->esp_current_backend = backend_idx;
311 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
312 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
313 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
314 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
315 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
316 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
317 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
318 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
Neale Ranns7ec120e2020-01-21 04:58:02 +0000319 im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
320 im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
321 im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
322 im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400323 im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index;
324 im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index;
Neale Ranns4a58e492020-12-21 13:19:10 +0000325 im->esp_mpls_encrypt_tun_node_index = b->esp_mpls_encrypt_tun_node_index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400326
Fan Zhangf5395782020-04-29 14:00:03 +0100327 if (b->enable_disable_cb)
328 {
329 if ((b->enable_disable_cb) (1) != 0)
330 return -1;
331 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100332 return 0;
333}
334
Fan Zhangf5395782020-04-29 14:00:03 +0100335void
336ipsec_set_async_mode (u32 is_enabled)
337{
338 ipsec_main_t *im = &ipsec_main;
339 ipsec_sa_t *sa;
340
Neale Rannsf16e9a52021-02-25 19:09:24 +0000341 vnet_crypto_request_async_mode (is_enabled);
Fan Zhangf5395782020-04-29 14:00:03 +0100342
343 im->async_mode = is_enabled;
344
Neale Rannsf16e9a52021-02-25 19:09:24 +0000345 /* change SA crypto op data */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000346 pool_foreach (sa, ipsec_sa_pool)
347 {
348 sa->crypto_op_data =
Neale Rannsf16e9a52021-02-25 19:09:24 +0000349 (is_enabled ? sa->async_op_data.data : sa->sync_op_data.data);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000350 }
Fan Zhangf5395782020-04-29 14:00:03 +0100351}
352
353static void
354crypto_engine_backend_register_post_node (vlib_main_t * vm)
355{
356 esp_async_post_next_t *eit;
357 esp_async_post_next_t *dit;
358
359 eit = &esp_encrypt_async_next;
360 eit->esp4_post_next =
361 vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
362 eit->esp6_post_next =
363 vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
364 eit->esp4_tun_post_next =
365 vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
366 eit->esp6_tun_post_next =
367 vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
Neale Ranns4a58e492020-12-21 13:19:10 +0000368 eit->esp_mpls_tun_post_next =
369 vnet_crypto_register_post_node (vm, "esp-mpls-encrypt-tun-post");
Fan Zhangf5395782020-04-29 14:00:03 +0100370
371 dit = &esp_decrypt_async_next;
372 dit->esp4_post_next =
373 vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
374 dit->esp6_post_next =
375 vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
376 dit->esp4_tun_post_next =
377 vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
378 dit->esp6_tun_post_next =
379 vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
380}
381
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000382static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383ipsec_init (vlib_main_t * vm)
384{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700385 clib_error_t *error;
386 ipsec_main_t *im = &ipsec_main;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100387 ipsec_main_crypto_alg_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barach0dd91652019-05-05 13:34:28 -0400389 /* Backend registration requires the feature arcs to be set up */
390 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
391 return (error);
392
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 im->vlib_main = vm;
395
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700396 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
397 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
399
Klement Sekerab4d30532018-11-08 13:00:02 +0100400 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700401 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 im->error_drop_node_index = node->index;
403
Fan Zhangf5395782020-04-29 14:00:03 +0100404 im->ah_current_backend = ~0;
405 im->esp_current_backend = ~0;
406
Neale Rannsd1a5b2d2019-05-16 17:09:28 +0000407 u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
Klement Sekerab4d30532018-11-08 13:00:02 +0100408 "ah4-encrypt",
409 "ah4-decrypt",
410 "ah6-encrypt",
411 "ah6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100412 ipsec_check_ah_support,
Klement Sekerab4d30532018-11-08 13:00:02 +0100413 NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Klement Sekerab4d30532018-11-08 13:00:02 +0100415 im->ah_default_backend = idx;
416 int rv = ipsec_select_ah_backend (im, idx);
417 ASSERT (0 == rv);
418 (void) (rv); // avoid warning
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000419
Neale Ranns4a58e492020-12-21 13:19:10 +0000420 idx = ipsec_register_esp_backend (
421 vm, im, "crypto engine backend", "esp4-encrypt", "esp4-encrypt-tun",
422 "esp4-decrypt", "esp4-decrypt-tun", "esp6-encrypt", "esp6-encrypt-tun",
423 "esp6-decrypt", "esp6-decrypt-tun", "esp-mpls-encrypt-tun",
424 ipsec_check_esp_support, NULL, crypto_dispatch_enable_disable);
Klement Sekerab4d30532018-11-08 13:00:02 +0100425 im->esp_default_backend = idx;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800426
Klement Sekerab4d30532018-11-08 13:00:02 +0100427 rv = ipsec_select_esp_backend (im, idx);
428 ASSERT (0 == rv);
429 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
432 return error;
433
Damjan Marion91f17dc2019-03-18 18:59:25 +0100434 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
435
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000436 a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
437 a->enc_op_id = VNET_CRYPTO_OP_NONE;
438 a->dec_op_id = VNET_CRYPTO_OP_NONE;
439 a->alg = VNET_CRYPTO_ALG_NONE;
440 a->iv_size = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500441 a->block_align = 1;
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000442
Damjan Marion91f17dc2019-03-18 18:59:25 +0100443 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100444 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
445 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200446 a->alg = VNET_CRYPTO_ALG_DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500447 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100448
449 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100450 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
451 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200452 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500453 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100454
455 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
Damjan Marion060bfb92019-03-29 13:47:54 +0100456 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
457 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200458 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500459 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100460
461 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
Damjan Marion060bfb92019-03-29 13:47:54 +0100462 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
463 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200464 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500465 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100466
467 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
Damjan Marion060bfb92019-03-29 13:47:54 +0100468 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
469 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200470 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500471 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100472
Benoît Ganne490b9272021-01-22 18:03:09 +0100473 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128;
474 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
475 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC;
476 a->alg = VNET_CRYPTO_ALG_AES_128_CTR;
477 a->iv_size = 8;
478 a->block_align = 1;
479
480 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192;
481 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC;
482 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC;
483 a->alg = VNET_CRYPTO_ALG_AES_192_CTR;
484 a->iv_size = 8;
485 a->block_align = 1;
486
487 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256;
488 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
489 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC;
490 a->alg = VNET_CRYPTO_ALG_AES_256_CTR;
491 a->iv_size = 8;
492 a->block_align = 1;
493
Neale Ranns47feb112019-04-11 15:14:07 +0000494 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
495 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
496 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200497 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100498 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500499 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000500 a->icv_size = 16;
501
502 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
503 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
504 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200505 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100506 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500507 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000508 a->icv_size = 16;
509
510 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
511 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
512 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200513 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100514 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500515 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000516 a->icv_size = 16;
517
Damjan Marion91f17dc2019-03-18 18:59:25 +0100518 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
519 ipsec_main_integ_alg_t *i;
520
Dmitry Vakhrushev77cc14a2019-08-14 00:12:33 -0400521 i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
522 i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
523 i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
524 i->icv_size = 12;
525
Damjan Marion91f17dc2019-03-18 18:59:25 +0100526 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100527 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200528 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200529 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100530
531 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100532 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200533 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200534 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100535
536 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Damjan Marion060bfb92019-03-29 13:47:54 +0100537 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200538 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200539 i->icv_size = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100540
541 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Damjan Marion060bfb92019-03-29 13:47:54 +0100542 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
Damjan Marion4cb83812019-04-24 17:32:01 +0200543 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200544 i->icv_size = 24;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100545
546 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Damjan Marion060bfb92019-03-29 13:47:54 +0100547 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200548 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200549 i->icv_size = 32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
Neale Ranns5ae793a2019-04-03 13:36:56 +0000551 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100552
Fan Zhangf5395782020-04-29 14:00:03 +0100553 im->async_mode = 0;
554 crypto_engine_backend_register_post_node (vm);
555
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000556 im->ipsec4_out_spd_hash_tbl = NULL;
557 im->flow_cache_flag = 0;
558 im->ipsec4_out_spd_flow_cache_entries = 0;
559 im->epoch_count = 0;
560 im->ipsec4_out_spd_hash_num_buckets =
561 IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS;
562
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 return 0;
564}
565
566VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700567
Zachary Leafb2d36782021-07-27 05:18:47 -0500568static clib_error_t *
569ipsec_config (vlib_main_t *vm, unformat_input_t *input)
570{
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000571 ipsec_main_t *im = &ipsec_main;
Zachary Leafb2d36782021-07-27 05:18:47 -0500572 unformat_input_t sub_input;
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000573 u32 ipsec4_out_spd_hash_num_buckets;
Zachary Leafb2d36782021-07-27 05:18:47 -0500574
575 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
576 {
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000577 if (unformat (input, "ipv4-outbound-spd-flow-cache on"))
578 im->flow_cache_flag = 1;
579 else if (unformat (input, "ipv4-outbound-spd-flow-cache off"))
580 im->flow_cache_flag = 0;
581 else if (unformat (input, "ipv4-outbound-spd-hash-buckets %d",
582 &ipsec4_out_spd_hash_num_buckets))
583 {
584 /* Size of hash is power of 2 >= number of buckets */
585 im->ipsec4_out_spd_hash_num_buckets =
586 1ULL << max_log2 (ipsec4_out_spd_hash_num_buckets);
587 }
588 else if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input,
589 &sub_input))
Zachary Leafb2d36782021-07-27 05:18:47 -0500590 {
591 uword table_size = ~0;
592 u32 n_buckets = ~0;
593
594 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
595 {
596 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
597 ;
598 else
599 return clib_error_return (0, "unknown input `%U'",
600 format_unformat_error, &sub_input);
601 }
602
603 ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
604 }
605 else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
606 &sub_input))
607 {
608 uword table_size = ~0;
609 u32 n_buckets = ~0;
610
611 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
612 {
613 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
614 ;
615 else
616 return clib_error_return (0, "unknown input `%U'",
617 format_unformat_error, &sub_input);
618 }
619
620 ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
621 }
622 else
623 return clib_error_return (0, "unknown input `%U'",
624 format_unformat_error, input);
625 }
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000626 if (im->flow_cache_flag)
627 {
628 vec_add2 (im->ipsec4_out_spd_hash_tbl, im->ipsec4_out_spd_hash_tbl,
629 im->ipsec4_out_spd_hash_num_buckets);
630 }
Zachary Leafb2d36782021-07-27 05:18:47 -0500631
632 return 0;
633}
634
635VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
636
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700637/*
638 * fd.io coding-style-patch-verification: ON
639 *
640 * Local Variables:
641 * eval: (c-set-style "gnu")
642 * End:
643 */