blob: 84d2293d4826b246a024b98312a18820a31a3b4f [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>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000027
Dave Wallace71612d62017-10-24 01:32:41 -040028ipsec_main_t ipsec_main;
Fan Zhangf5395782020-04-29 14:00:03 +010029esp_async_post_next_t esp_encrypt_async_next;
30esp_async_post_next_t esp_decrypt_async_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32static clib_error_t *
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010033ipsec_check_ah_support (ipsec_sa_t * sa)
34{
Neale Rannsece2ae02019-06-21 12:44:11 +000035 ipsec_main_t *im = &ipsec_main;
36
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010037 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
38 return clib_error_return (0, "unsupported none integ-alg");
Neale Rannsece2ae02019-06-21 12:44:11 +000039
40 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
41 return clib_error_return (0, "No crypto engine support for %U",
42 format_ipsec_integ_alg, sa->integ_alg);
43
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010044 return 0;
45}
46
47static clib_error_t *
48ipsec_check_esp_support (ipsec_sa_t * sa)
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000049{
Neale Rannsece2ae02019-06-21 12:44:11 +000050 ipsec_main_t *im = &ipsec_main;
51
52 if (IPSEC_INTEG_ALG_NONE != sa->integ_alg)
53 {
54 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
55 return clib_error_return (0, "No crypto engine support for %U",
56 format_ipsec_integ_alg, sa->integ_alg);
57 }
58 if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg)
59 {
60 if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg))
61 return clib_error_return (0, "No crypto engine support for %U",
62 format_ipsec_crypto_alg, sa->crypto_alg);
63 }
64
65 return (0);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000066}
67
Klement Sekerab4d30532018-11-08 13:00:02 +010068clib_error_t *
69ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
70{
71 ipsec_ah_backend_t *ah =
72 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
73 if (ah->add_del_sa_sess_cb)
74 {
75 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
76 if (err)
77 return err;
78 }
79 ipsec_esp_backend_t *esp =
80 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
81 if (esp->add_del_sa_sess_cb)
82 {
83 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
84 if (err)
85 return err;
86 }
87 return 0;
88}
89
90clib_error_t *
91ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
92{
93 clib_error_t *error = 0;
Matthew Smith461caa52018-12-21 11:53:16 -060094
95 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
96 {
97 ipsec_ah_backend_t *ah =
98 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
99 ASSERT (ah->check_support_cb);
100 error = ah->check_support_cb (sa);
101 }
102 else
103 {
104 ipsec_esp_backend_t *esp =
105 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
106 ASSERT (esp->check_support_cb);
107 error = esp->check_support_cb (sa);
108 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100109 return error;
110}
111
112
113static void
114ipsec_add_node (vlib_main_t * vm, const char *node_name,
115 const char *prev_node_name, u32 * out_node_index,
116 u32 * out_next_index)
117{
118 vlib_node_t *prev_node, *node;
119 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
120 ASSERT (prev_node);
121 node = vlib_get_node_by_name (vm, (u8 *) node_name);
122 ASSERT (node);
123 *out_node_index = node->index;
124 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
125}
126
Neale Rannsabc56602020-04-01 09:45:23 +0000127void
Neale Rannsabc56602020-04-01 09:45:23 +0000128ipsec_unregister_udp_port (u16 port)
129{
130 ipsec_main_t *im = &ipsec_main;
131 u32 n_regs;
132 uword *p;
133
134 p = hash_get (im->udp_port_registrations, port);
135
136 ASSERT (p);
137
138 n_regs = p[0];
139
140 if (0 == --n_regs)
141 {
142 udp_unregister_dst_port (vlib_get_main (), port, 1);
143 hash_unset (im->udp_port_registrations, port);
144 }
145 else
146 {
147 hash_unset (im->udp_port_registrations, port);
148 hash_set (im->udp_port_registrations, port, n_regs);
149 }
150}
151
152void
153ipsec_register_udp_port (u16 port)
154{
155 ipsec_main_t *im = &ipsec_main;
156 u32 n_regs;
157 uword *p;
158
159 p = hash_get (im->udp_port_registrations, port);
160
161 n_regs = (p ? p[0] : 0);
162
163 if (0 == n_regs++)
164 udp_register_dst_port (vlib_get_main (), port,
165 ipsec4_tun_input_node.index, 1);
166
167 hash_unset (im->udp_port_registrations, port);
168 hash_set (im->udp_port_registrations, port, n_regs);
169}
170
Klement Sekerab4d30532018-11-08 13:00:02 +0100171u32
172ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
173 const char *name,
174 const char *ah4_encrypt_node_name,
175 const char *ah4_decrypt_node_name,
176 const char *ah6_encrypt_node_name,
177 const char *ah6_decrypt_node_name,
178 check_support_cb_t ah_check_support_cb,
179 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
180{
181 ipsec_ah_backend_t *b;
182 pool_get (im->ah_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500183 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100184
Pierre Pfister057b3562018-12-10 17:01:01 +0100185 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100186 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100187 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100188 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100189 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100190 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100191 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100192 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
193
194 b->check_support_cb = ah_check_support_cb;
195 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
196 return b - im->ah_backends;
197}
198
199u32
Neale Ranns4a58e492020-12-21 13:19:10 +0000200ipsec_register_esp_backend (
201 vlib_main_t *vm, ipsec_main_t *im, const char *name,
202 const char *esp4_encrypt_node_name, const char *esp4_encrypt_node_tun_name,
203 const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name,
204 const char *esp6_encrypt_node_name, const char *esp6_encrypt_node_tun_name,
205 const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name,
206 const char *esp_mpls_encrypt_node_tun_name,
207 check_support_cb_t esp_check_support_cb,
208 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
209 enable_disable_cb_t enable_disable_cb)
Klement Sekerab4d30532018-11-08 13:00:02 +0100210{
211 ipsec_esp_backend_t *b;
Neale Rannse8915fc2019-04-23 20:57:55 -0400212
Klement Sekerab4d30532018-11-08 13:00:02 +0100213 pool_get (im->esp_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500214 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100215
Pierre Pfister057b3562018-12-10 17:01:01 +0100216 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100217 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100218 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100219 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100220 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100221 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100222 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100223 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
Neale Ranns7ec120e2020-01-21 04:58:02 +0000224 ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
225 &b->esp4_decrypt_tun_node_index,
226 &b->esp4_decrypt_tun_next_index);
227 ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
228 &b->esp6_decrypt_tun_node_index,
229 &b->esp6_decrypt_tun_next_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100230
Neale Ranns4ec36c52020-03-31 09:21:29 -0400231 b->esp6_encrypt_tun_node_index =
232 vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index;
Neale Ranns4a58e492020-12-21 13:19:10 +0000233 b->esp_mpls_encrypt_tun_node_index =
234 vlib_get_node_by_name (vm, (u8 *) esp_mpls_encrypt_node_tun_name)->index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400235 b->esp4_encrypt_tun_node_index =
236 vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400237
Klement Sekerab4d30532018-11-08 13:00:02 +0100238 b->check_support_cb = esp_check_support_cb;
239 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
Fan Zhangf5395782020-04-29 14:00:03 +0100240 b->enable_disable_cb = enable_disable_cb;
241
Klement Sekerab4d30532018-11-08 13:00:02 +0100242 return b - im->esp_backends;
243}
244
Neale Rannse8915fc2019-04-23 20:57:55 -0400245clib_error_t *
246ipsec_rsc_in_use (ipsec_main_t * im)
Neale Rannsb4cfd552019-02-13 02:08:06 -0800247{
Neale Rannse8915fc2019-04-23 20:57:55 -0400248 /* return an error is crypto resource are in use */
249 if (pool_elts (im->sad) > 0)
250 return clib_error_return (0,
251 "%d SA entries configured",
252 pool_elts (im->sad));
Neale Rannsb4cfd552019-02-13 02:08:06 -0800253
Neale Rannse8915fc2019-04-23 20:57:55 -0400254 return (NULL);
Neale Rannsb4cfd552019-02-13 02:08:06 -0800255}
256
Klement Sekerab4d30532018-11-08 13:00:02 +0100257int
258ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
259{
Neale Rannse8915fc2019-04-23 20:57:55 -0400260 if (ipsec_rsc_in_use (im))
261 return VNET_API_ERROR_RSRC_IN_USE;
262
263 if (pool_is_free_index (im->ah_backends, backend_idx))
264 return VNET_API_ERROR_INVALID_VALUE;
265
Klement Sekerab4d30532018-11-08 13:00:02 +0100266 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
267 im->ah_current_backend = backend_idx;
268 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
269 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
270 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
271 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
272 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
273 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
274 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
275 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800276
Klement Sekerab4d30532018-11-08 13:00:02 +0100277 return 0;
278}
279
280int
281ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
282{
Neale Rannse8915fc2019-04-23 20:57:55 -0400283 if (ipsec_rsc_in_use (im))
284 return VNET_API_ERROR_RSRC_IN_USE;
285
286 if (pool_is_free_index (im->esp_backends, backend_idx))
287 return VNET_API_ERROR_INVALID_VALUE;
288
Fan Zhangf5395782020-04-29 14:00:03 +0100289 /* disable current backend */
290 if (im->esp_current_backend != ~0)
291 {
292 ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
293 im->esp_current_backend);
294 if (cb->enable_disable_cb)
295 {
296 if ((cb->enable_disable_cb) (0) != 0)
297 return -1;
298 }
299 }
300
Klement Sekerab4d30532018-11-08 13:00:02 +0100301 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
302 im->esp_current_backend = backend_idx;
303 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
304 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
305 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
306 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
307 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
308 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
309 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
310 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
Neale Ranns7ec120e2020-01-21 04:58:02 +0000311 im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
312 im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
313 im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
314 im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400315 im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index;
316 im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index;
Neale Ranns4a58e492020-12-21 13:19:10 +0000317 im->esp_mpls_encrypt_tun_node_index = b->esp_mpls_encrypt_tun_node_index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400318
Fan Zhangf5395782020-04-29 14:00:03 +0100319 if (b->enable_disable_cb)
320 {
321 if ((b->enable_disable_cb) (1) != 0)
322 return -1;
323 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100324 return 0;
325}
326
Fan Zhangf5395782020-04-29 14:00:03 +0100327void
328ipsec_set_async_mode (u32 is_enabled)
329{
330 ipsec_main_t *im = &ipsec_main;
331 ipsec_sa_t *sa;
332
333 /* lock all SAs before change im->async_mode */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100334 pool_foreach (sa, im->sad)
335 {
336 fib_node_lock (&sa->node);
337 }
Fan Zhangf5395782020-04-29 14:00:03 +0100338
339 im->async_mode = is_enabled;
340
341 /* change SA crypto op data before unlock them */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100342 pool_foreach (sa, im->sad)
343 {
344 sa->crypto_op_data = is_enabled ?
345 sa->async_op_data.data : sa->sync_op_data.data;
346 fib_node_unlock (&sa->node);
347 }
Fan Zhangf5395782020-04-29 14:00:03 +0100348}
349
350static void
351crypto_engine_backend_register_post_node (vlib_main_t * vm)
352{
353 esp_async_post_next_t *eit;
354 esp_async_post_next_t *dit;
355
356 eit = &esp_encrypt_async_next;
357 eit->esp4_post_next =
358 vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
359 eit->esp6_post_next =
360 vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
361 eit->esp4_tun_post_next =
362 vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
363 eit->esp6_tun_post_next =
364 vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
Neale Ranns4a58e492020-12-21 13:19:10 +0000365 eit->esp_mpls_tun_post_next =
366 vnet_crypto_register_post_node (vm, "esp-mpls-encrypt-tun-post");
Fan Zhangf5395782020-04-29 14:00:03 +0100367
368 dit = &esp_decrypt_async_next;
369 dit->esp4_post_next =
370 vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
371 dit->esp6_post_next =
372 vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
373 dit->esp4_tun_post_next =
374 vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
375 dit->esp6_tun_post_next =
376 vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
377}
378
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000379static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380ipsec_init (vlib_main_t * vm)
381{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700382 clib_error_t *error;
383 ipsec_main_t *im = &ipsec_main;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100384 ipsec_main_crypto_alg_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
Dave Barach0dd91652019-05-05 13:34:28 -0400386 /* Backend registration requires the feature arcs to be set up */
387 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
388 return (error);
389
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700390 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 im->vlib_main = vm;
392
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
394 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
396
Klement Sekerab4d30532018-11-08 13:00:02 +0100397 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700398 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 im->error_drop_node_index = node->index;
400
Fan Zhangf5395782020-04-29 14:00:03 +0100401 im->ah_current_backend = ~0;
402 im->esp_current_backend = ~0;
403
Neale Rannsd1a5b2d2019-05-16 17:09:28 +0000404 u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
Klement Sekerab4d30532018-11-08 13:00:02 +0100405 "ah4-encrypt",
406 "ah4-decrypt",
407 "ah6-encrypt",
408 "ah6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100409 ipsec_check_ah_support,
Klement Sekerab4d30532018-11-08 13:00:02 +0100410 NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Klement Sekerab4d30532018-11-08 13:00:02 +0100412 im->ah_default_backend = idx;
413 int rv = ipsec_select_ah_backend (im, idx);
414 ASSERT (0 == rv);
415 (void) (rv); // avoid warning
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000416
Neale Ranns4a58e492020-12-21 13:19:10 +0000417 idx = ipsec_register_esp_backend (
418 vm, im, "crypto engine backend", "esp4-encrypt", "esp4-encrypt-tun",
419 "esp4-decrypt", "esp4-decrypt-tun", "esp6-encrypt", "esp6-encrypt-tun",
420 "esp6-decrypt", "esp6-decrypt-tun", "esp-mpls-encrypt-tun",
421 ipsec_check_esp_support, NULL, crypto_dispatch_enable_disable);
Klement Sekerab4d30532018-11-08 13:00:02 +0100422 im->esp_default_backend = idx;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800423
Klement Sekerab4d30532018-11-08 13:00:02 +0100424 rv = ipsec_select_esp_backend (im, idx);
425 ASSERT (0 == rv);
426 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
429 return error;
430
Damjan Marion91f17dc2019-03-18 18:59:25 +0100431 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
432
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000433 a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
434 a->enc_op_id = VNET_CRYPTO_OP_NONE;
435 a->dec_op_id = VNET_CRYPTO_OP_NONE;
436 a->alg = VNET_CRYPTO_ALG_NONE;
437 a->iv_size = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500438 a->block_align = 1;
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000439
Damjan Marion91f17dc2019-03-18 18:59:25 +0100440 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100441 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
442 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200443 a->alg = VNET_CRYPTO_ALG_DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500444 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100445
446 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100447 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
448 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200449 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500450 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100451
452 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
Damjan Marion060bfb92019-03-29 13:47:54 +0100453 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
454 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200455 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500456 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100457
458 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
Damjan Marion060bfb92019-03-29 13:47:54 +0100459 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
460 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200461 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500462 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100463
464 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
Damjan Marion060bfb92019-03-29 13:47:54 +0100465 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
466 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200467 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500468 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100469
Benoît Ganne490b9272021-01-22 18:03:09 +0100470 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128;
471 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
472 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC;
473 a->alg = VNET_CRYPTO_ALG_AES_128_CTR;
474 a->iv_size = 8;
475 a->block_align = 1;
476
477 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192;
478 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC;
479 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC;
480 a->alg = VNET_CRYPTO_ALG_AES_192_CTR;
481 a->iv_size = 8;
482 a->block_align = 1;
483
484 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256;
485 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
486 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC;
487 a->alg = VNET_CRYPTO_ALG_AES_256_CTR;
488 a->iv_size = 8;
489 a->block_align = 1;
490
Neale Ranns47feb112019-04-11 15:14:07 +0000491 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
492 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
493 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200494 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100495 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500496 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000497 a->icv_size = 16;
498
499 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
500 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
501 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200502 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100503 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500504 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000505 a->icv_size = 16;
506
507 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
508 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
509 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200510 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100511 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500512 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000513 a->icv_size = 16;
514
Damjan Marion91f17dc2019-03-18 18:59:25 +0100515 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
516 ipsec_main_integ_alg_t *i;
517
Dmitry Vakhrushev77cc14a2019-08-14 00:12:33 -0400518 i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
519 i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
520 i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
521 i->icv_size = 12;
522
Damjan Marion91f17dc2019-03-18 18:59:25 +0100523 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100524 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200525 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200526 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100527
528 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100529 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200530 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200531 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100532
533 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Damjan Marion060bfb92019-03-29 13:47:54 +0100534 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200535 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200536 i->icv_size = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100537
538 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Damjan Marion060bfb92019-03-29 13:47:54 +0100539 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
Damjan Marion4cb83812019-04-24 17:32:01 +0200540 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200541 i->icv_size = 24;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100542
543 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Damjan Marion060bfb92019-03-29 13:47:54 +0100544 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200545 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200546 i->icv_size = 32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Neale Ranns5ae793a2019-04-03 13:36:56 +0000548 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100549
Fan Zhangf5395782020-04-29 14:00:03 +0100550 im->async_mode = 0;
551 crypto_engine_backend_register_post_node (vm);
552
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 return 0;
554}
555
556VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700557
558/*
559 * fd.io coding-style-patch-verification: ON
560 *
561 * Local Variables:
562 * eval: (c-set-style "gnu")
563 * End:
564 */