blob: 1d92676706acc658d2c3c43f4404e46c9efdf6cd [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
128ipsec_add_feature (const char *arc_name,
129 const char *node_name, u32 * out_feature_index)
130{
131 u8 arc;
132
133 arc = vnet_get_feature_arc_index (arc_name);
134 ASSERT (arc != (u8) ~ 0);
135 *out_feature_index = vnet_get_feature_index (arc, node_name);
136}
137
138void
139ipsec_unregister_udp_port (u16 port)
140{
141 ipsec_main_t *im = &ipsec_main;
142 u32 n_regs;
143 uword *p;
144
145 p = hash_get (im->udp_port_registrations, port);
146
147 ASSERT (p);
148
149 n_regs = p[0];
150
151 if (0 == --n_regs)
152 {
153 udp_unregister_dst_port (vlib_get_main (), port, 1);
154 hash_unset (im->udp_port_registrations, port);
155 }
156 else
157 {
158 hash_unset (im->udp_port_registrations, port);
159 hash_set (im->udp_port_registrations, port, n_regs);
160 }
161}
162
163void
164ipsec_register_udp_port (u16 port)
165{
166 ipsec_main_t *im = &ipsec_main;
167 u32 n_regs;
168 uword *p;
169
170 p = hash_get (im->udp_port_registrations, port);
171
172 n_regs = (p ? p[0] : 0);
173
174 if (0 == n_regs++)
175 udp_register_dst_port (vlib_get_main (), port,
176 ipsec4_tun_input_node.index, 1);
177
178 hash_unset (im->udp_port_registrations, port);
179 hash_set (im->udp_port_registrations, port, n_regs);
180}
181
Klement Sekerab4d30532018-11-08 13:00:02 +0100182u32
183ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
184 const char *name,
185 const char *ah4_encrypt_node_name,
186 const char *ah4_decrypt_node_name,
187 const char *ah6_encrypt_node_name,
188 const char *ah6_decrypt_node_name,
189 check_support_cb_t ah_check_support_cb,
190 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
191{
192 ipsec_ah_backend_t *b;
193 pool_get (im->ah_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500194 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100195
Pierre Pfister057b3562018-12-10 17:01:01 +0100196 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100197 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100198 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100199 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100200 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100201 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100202 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100203 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
204
205 b->check_support_cb = ah_check_support_cb;
206 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
207 return b - im->ah_backends;
208}
209
210u32
211ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
212 const char *name,
213 const char *esp4_encrypt_node_name,
Neale Rannse8915fc2019-04-23 20:57:55 -0400214 const char *esp4_encrypt_node_tun_name,
Klement Sekerab4d30532018-11-08 13:00:02 +0100215 const char *esp4_decrypt_node_name,
Neale Ranns7ec120e2020-01-21 04:58:02 +0000216 const char *esp4_decrypt_tun_node_name,
Klement Sekerab4d30532018-11-08 13:00:02 +0100217 const char *esp6_encrypt_node_name,
Neale Rannse8915fc2019-04-23 20:57:55 -0400218 const char *esp6_encrypt_node_tun_name,
Klement Sekerab4d30532018-11-08 13:00:02 +0100219 const char *esp6_decrypt_node_name,
Neale Ranns7ec120e2020-01-21 04:58:02 +0000220 const char *esp6_decrypt_tun_node_name,
Klement Sekerab4d30532018-11-08 13:00:02 +0100221 check_support_cb_t esp_check_support_cb,
Fan Zhangf5395782020-04-29 14:00:03 +0100222 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
223 enable_disable_cb_t enable_disable_cb)
Klement Sekerab4d30532018-11-08 13:00:02 +0100224{
225 ipsec_esp_backend_t *b;
Neale Rannse8915fc2019-04-23 20:57:55 -0400226
Klement Sekerab4d30532018-11-08 13:00:02 +0100227 pool_get (im->esp_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500228 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100229
Pierre Pfister057b3562018-12-10 17:01:01 +0100230 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100231 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100232 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100233 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100234 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100235 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100236 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100237 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
Neale Ranns7ec120e2020-01-21 04:58:02 +0000238 ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
239 &b->esp4_decrypt_tun_node_index,
240 &b->esp4_decrypt_tun_next_index);
241 ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
242 &b->esp6_decrypt_tun_node_index,
243 &b->esp6_decrypt_tun_next_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100244
Neale Ranns4ec36c52020-03-31 09:21:29 -0400245 b->esp6_encrypt_tun_node_index =
246 vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index;
247 b->esp4_encrypt_tun_node_index =
248 vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400249
Klement Sekerab4d30532018-11-08 13:00:02 +0100250 b->check_support_cb = esp_check_support_cb;
251 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
Fan Zhangf5395782020-04-29 14:00:03 +0100252 b->enable_disable_cb = enable_disable_cb;
253
Klement Sekerab4d30532018-11-08 13:00:02 +0100254 return b - im->esp_backends;
255}
256
Neale Rannse8915fc2019-04-23 20:57:55 -0400257clib_error_t *
258ipsec_rsc_in_use (ipsec_main_t * im)
Neale Rannsb4cfd552019-02-13 02:08:06 -0800259{
Neale Rannse8915fc2019-04-23 20:57:55 -0400260 /* return an error is crypto resource are in use */
261 if (pool_elts (im->sad) > 0)
262 return clib_error_return (0,
263 "%d SA entries configured",
264 pool_elts (im->sad));
Neale Rannsb4cfd552019-02-13 02:08:06 -0800265
Neale Rannse8915fc2019-04-23 20:57:55 -0400266 return (NULL);
Neale Rannsb4cfd552019-02-13 02:08:06 -0800267}
268
Klement Sekerab4d30532018-11-08 13:00:02 +0100269int
270ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
271{
Neale Rannse8915fc2019-04-23 20:57:55 -0400272 if (ipsec_rsc_in_use (im))
273 return VNET_API_ERROR_RSRC_IN_USE;
274
275 if (pool_is_free_index (im->ah_backends, backend_idx))
276 return VNET_API_ERROR_INVALID_VALUE;
277
Klement Sekerab4d30532018-11-08 13:00:02 +0100278 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
279 im->ah_current_backend = backend_idx;
280 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
281 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
282 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
283 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
284 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
285 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
286 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
287 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800288
Klement Sekerab4d30532018-11-08 13:00:02 +0100289 return 0;
290}
291
292int
293ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
294{
Neale Rannse8915fc2019-04-23 20:57:55 -0400295 if (ipsec_rsc_in_use (im))
296 return VNET_API_ERROR_RSRC_IN_USE;
297
298 if (pool_is_free_index (im->esp_backends, backend_idx))
299 return VNET_API_ERROR_INVALID_VALUE;
300
Fan Zhangf5395782020-04-29 14:00:03 +0100301 /* disable current backend */
302 if (im->esp_current_backend != ~0)
303 {
304 ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
305 im->esp_current_backend);
306 if (cb->enable_disable_cb)
307 {
308 if ((cb->enable_disable_cb) (0) != 0)
309 return -1;
310 }
311 }
312
Klement Sekerab4d30532018-11-08 13:00:02 +0100313 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
314 im->esp_current_backend = backend_idx;
315 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
316 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
317 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
318 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
319 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
320 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
321 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
322 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
Neale Ranns7ec120e2020-01-21 04:58:02 +0000323 im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
324 im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
325 im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
326 im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400327 im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index;
328 im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400329
Fan Zhangf5395782020-04-29 14:00:03 +0100330 if (b->enable_disable_cb)
331 {
332 if ((b->enable_disable_cb) (1) != 0)
333 return -1;
334 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100335 return 0;
336}
337
Fan Zhangf5395782020-04-29 14:00:03 +0100338void
339ipsec_set_async_mode (u32 is_enabled)
340{
341 ipsec_main_t *im = &ipsec_main;
342 ipsec_sa_t *sa;
343
344 /* lock all SAs before change im->async_mode */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100345 pool_foreach (sa, im->sad)
346 {
347 fib_node_lock (&sa->node);
348 }
Fan Zhangf5395782020-04-29 14:00:03 +0100349
350 im->async_mode = is_enabled;
351
352 /* change SA crypto op data before unlock them */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100353 pool_foreach (sa, im->sad)
354 {
355 sa->crypto_op_data = is_enabled ?
356 sa->async_op_data.data : sa->sync_op_data.data;
357 fib_node_unlock (&sa->node);
358 }
Fan Zhangf5395782020-04-29 14:00:03 +0100359}
360
361static void
362crypto_engine_backend_register_post_node (vlib_main_t * vm)
363{
364 esp_async_post_next_t *eit;
365 esp_async_post_next_t *dit;
366
367 eit = &esp_encrypt_async_next;
368 eit->esp4_post_next =
369 vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
370 eit->esp6_post_next =
371 vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
372 eit->esp4_tun_post_next =
373 vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
374 eit->esp6_tun_post_next =
375 vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
376
377 dit = &esp_decrypt_async_next;
378 dit->esp4_post_next =
379 vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
380 dit->esp6_post_next =
381 vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
382 dit->esp4_tun_post_next =
383 vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
384 dit->esp6_tun_post_next =
385 vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
386}
387
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000388static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389ipsec_init (vlib_main_t * vm)
390{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700391 clib_error_t *error;
392 ipsec_main_t *im = &ipsec_main;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100393 ipsec_main_crypto_alg_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Dave Barach0dd91652019-05-05 13:34:28 -0400395 /* Backend registration requires the feature arcs to be set up */
396 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
397 return (error);
398
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700399 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 im->vlib_main = vm;
401
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700402 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
403 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
405
Klement Sekerab4d30532018-11-08 13:00:02 +0100406 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700407 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 im->error_drop_node_index = node->index;
409
Fan Zhangf5395782020-04-29 14:00:03 +0100410 im->ah_current_backend = ~0;
411 im->esp_current_backend = ~0;
412
Neale Rannsd1a5b2d2019-05-16 17:09:28 +0000413 u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
Klement Sekerab4d30532018-11-08 13:00:02 +0100414 "ah4-encrypt",
415 "ah4-decrypt",
416 "ah6-encrypt",
417 "ah6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100418 ipsec_check_ah_support,
Klement Sekerab4d30532018-11-08 13:00:02 +0100419 NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
Klement Sekerab4d30532018-11-08 13:00:02 +0100421 im->ah_default_backend = idx;
422 int rv = ipsec_select_ah_backend (im, idx);
423 ASSERT (0 == rv);
424 (void) (rv); // avoid warning
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000425
Neale Rannsd1a5b2d2019-05-16 17:09:28 +0000426 idx = ipsec_register_esp_backend (vm, im, "crypto engine backend",
Klement Sekerab4d30532018-11-08 13:00:02 +0100427 "esp4-encrypt",
Neale Rannse8915fc2019-04-23 20:57:55 -0400428 "esp4-encrypt-tun",
Klement Sekerab4d30532018-11-08 13:00:02 +0100429 "esp4-decrypt",
Neale Ranns7ec120e2020-01-21 04:58:02 +0000430 "esp4-decrypt-tun",
Klement Sekerab4d30532018-11-08 13:00:02 +0100431 "esp6-encrypt",
Neale Rannse8915fc2019-04-23 20:57:55 -0400432 "esp6-encrypt-tun",
Klement Sekerab4d30532018-11-08 13:00:02 +0100433 "esp6-decrypt",
Neale Ranns7ec120e2020-01-21 04:58:02 +0000434 "esp6-decrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +0100435 ipsec_check_esp_support,
436 NULL, crypto_dispatch_enable_disable);
Klement Sekerab4d30532018-11-08 13:00:02 +0100437 im->esp_default_backend = idx;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800438
Klement Sekerab4d30532018-11-08 13:00:02 +0100439 rv = ipsec_select_esp_backend (im, idx);
440 ASSERT (0 == rv);
441 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
444 return error;
445
Damjan Marion91f17dc2019-03-18 18:59:25 +0100446 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
447
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000448 a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
449 a->enc_op_id = VNET_CRYPTO_OP_NONE;
450 a->dec_op_id = VNET_CRYPTO_OP_NONE;
451 a->alg = VNET_CRYPTO_ALG_NONE;
452 a->iv_size = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500453 a->block_align = 1;
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000454
Damjan Marion91f17dc2019-03-18 18:59:25 +0100455 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100456 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
457 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200458 a->alg = VNET_CRYPTO_ALG_DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500459 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100460
461 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100462 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
463 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200464 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500465 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100466
467 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
Damjan Marion060bfb92019-03-29 13:47:54 +0100468 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
469 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200470 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500471 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100472
473 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
Damjan Marion060bfb92019-03-29 13:47:54 +0100474 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
475 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200476 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500477 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100478
479 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
Damjan Marion060bfb92019-03-29 13:47:54 +0100480 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
481 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200482 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500483 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100484
Neale Ranns47feb112019-04-11 15:14:07 +0000485 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
486 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
487 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200488 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100489 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500490 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000491 a->icv_size = 16;
492
493 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
494 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
495 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200496 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100497 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500498 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000499 a->icv_size = 16;
500
501 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
502 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
503 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200504 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100505 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500506 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000507 a->icv_size = 16;
508
Damjan Marion91f17dc2019-03-18 18:59:25 +0100509 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
510 ipsec_main_integ_alg_t *i;
511
Dmitry Vakhrushev77cc14a2019-08-14 00:12:33 -0400512 i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
513 i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
514 i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
515 i->icv_size = 12;
516
Damjan Marion91f17dc2019-03-18 18:59:25 +0100517 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100518 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200519 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200520 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100521
522 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100523 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200524 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200525 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100526
527 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Damjan Marion060bfb92019-03-29 13:47:54 +0100528 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200529 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200530 i->icv_size = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100531
532 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Damjan Marion060bfb92019-03-29 13:47:54 +0100533 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
Damjan Marion4cb83812019-04-24 17:32:01 +0200534 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200535 i->icv_size = 24;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100536
537 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Damjan Marion060bfb92019-03-29 13:47:54 +0100538 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200539 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200540 i->icv_size = 32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Neale Ranns5ae793a2019-04-03 13:36:56 +0000542 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100543
Neale Rannsf62a8c02019-04-02 08:13:33 +0000544 im->ah4_enc_fq_index =
545 vlib_frame_queue_main_init (ah4_encrypt_node.index, 0);
546 im->ah4_dec_fq_index =
547 vlib_frame_queue_main_init (ah4_decrypt_node.index, 0);
548 im->ah6_enc_fq_index =
549 vlib_frame_queue_main_init (ah6_encrypt_node.index, 0);
550 im->ah6_dec_fq_index =
551 vlib_frame_queue_main_init (ah6_decrypt_node.index, 0);
552
553 im->esp4_enc_fq_index =
554 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
555 im->esp4_dec_fq_index =
556 vlib_frame_queue_main_init (esp4_decrypt_node.index, 0);
557 im->esp6_enc_fq_index =
558 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
559 im->esp6_dec_fq_index =
560 vlib_frame_queue_main_init (esp6_decrypt_node.index, 0);
561 im->esp4_enc_tun_fq_index =
562 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
563 im->esp6_enc_tun_fq_index =
564 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
565 im->esp4_dec_tun_fq_index =
566 vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0);
567 im->esp6_dec_tun_fq_index =
568 vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0);
569
Fan Zhangf5395782020-04-29 14:00:03 +0100570 im->async_mode = 0;
571 crypto_engine_backend_register_post_node (vm);
572
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 return 0;
574}
575
576VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700577
578/*
579 * fd.io coding-style-patch-verification: ON
580 *
581 * Local Variables:
582 * eval: (c-set-style "gnu")
583 * End:
584 */