blob: 62ab23976a848adb271ecb3190a201ec8a487561 [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
Zachary Leaf7cd35f52021-06-25 08:11:15 -050034/* Flow cache is sized for 1 million flows with a load factor of .25.
35 */
36#define IPSEC4_SPD_DEFAULT_HASH_NUM_BUCKETS (1 << 22)
37
Dave Wallace71612d62017-10-24 01:32:41 -040038ipsec_main_t ipsec_main;
Fan Zhangf5395782020-04-29 14:00:03 +010039esp_async_post_next_t esp_encrypt_async_next;
40esp_async_post_next_t esp_decrypt_async_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
Benoît Ganne98ca76a2022-04-11 18:51:25 +020042clib_error_t *
43ipsec_register_next_header (vlib_main_t *vm, u8 next_header,
44 const char *next_node)
45{
46 ipsec_main_t *im = &ipsec_main;
47 const vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) next_node);
48 /* -post nodes (eg. esp4-decrypt-post) are siblings of non-post nodes (eg.
49 * esp4-decrypt) and will therefore have the same next index */
50 const vlib_node_t *esp_decrypt_nodes[] = {
51 vlib_get_node (vm, im->esp4_decrypt_node_index),
52 vlib_get_node (vm, im->esp6_decrypt_node_index),
53 vlib_get_node (vm, im->esp4_decrypt_tun_node_index),
54 vlib_get_node (vm, im->esp6_decrypt_tun_node_index),
55 };
56 uword slot, max;
57 int i;
58
59 /* looks for a next_index value that we can use for all esp decrypt nodes to
60 * avoid maintaining different next index arrays... */
61
62 slot = vlib_node_get_next (vm, esp_decrypt_nodes[0]->index, node->index);
63 max = vec_len (esp_decrypt_nodes[0]->next_nodes);
64 for (i = 1; i < ARRAY_LEN (esp_decrypt_nodes); i++)
65 {
66 /* if next node already exists, check it shares the same next_index */
67 if (slot !=
68 vlib_node_get_next (vm, esp_decrypt_nodes[i]->index, node->index))
69 return clib_error_return (
70 0, "next node already exists with different next index");
71 /* compute a suitable slot from the max of all nodes next index */
72 max = clib_max (max, vec_len (esp_decrypt_nodes[i]->next_nodes));
73 }
74
75 if (~0 == slot)
76 {
77 /* next node not there yet, add it using the computed max */
78 slot = max;
79 for (i = 0; i < ARRAY_LEN (esp_decrypt_nodes); i++)
80 vlib_node_add_next_with_slot (vm, esp_decrypt_nodes[i]->index,
81 node->index, slot);
82 }
83
84 im->next_header_registrations[next_header] = slot;
85 return 0;
86}
87
Ed Warnickecb9cada2015-12-08 15:45:58 -070088static clib_error_t *
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010089ipsec_check_ah_support (ipsec_sa_t * sa)
90{
Neale Rannsece2ae02019-06-21 12:44:11 +000091 ipsec_main_t *im = &ipsec_main;
92
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010093 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
94 return clib_error_return (0, "unsupported none integ-alg");
Neale Rannsece2ae02019-06-21 12:44:11 +000095
96 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
97 return clib_error_return (0, "No crypto engine support for %U",
98 format_ipsec_integ_alg, sa->integ_alg);
99
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100100 return 0;
101}
102
103static clib_error_t *
104ipsec_check_esp_support (ipsec_sa_t * sa)
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000105{
Neale Rannsece2ae02019-06-21 12:44:11 +0000106 ipsec_main_t *im = &ipsec_main;
107
108 if (IPSEC_INTEG_ALG_NONE != sa->integ_alg)
109 {
110 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
111 return clib_error_return (0, "No crypto engine support for %U",
112 format_ipsec_integ_alg, sa->integ_alg);
113 }
114 if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg)
115 {
116 if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg))
117 return clib_error_return (0, "No crypto engine support for %U",
118 format_ipsec_crypto_alg, sa->crypto_alg);
119 }
120
121 return (0);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000122}
123
Klement Sekerab4d30532018-11-08 13:00:02 +0100124clib_error_t *
125ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
126{
127 ipsec_ah_backend_t *ah =
128 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
129 if (ah->add_del_sa_sess_cb)
130 {
131 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
132 if (err)
133 return err;
134 }
135 ipsec_esp_backend_t *esp =
136 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
137 if (esp->add_del_sa_sess_cb)
138 {
139 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
140 if (err)
141 return err;
142 }
143 return 0;
144}
145
146clib_error_t *
147ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
148{
149 clib_error_t *error = 0;
Matthew Smith461caa52018-12-21 11:53:16 -0600150
151 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
152 {
153 ipsec_ah_backend_t *ah =
154 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
155 ASSERT (ah->check_support_cb);
156 error = ah->check_support_cb (sa);
157 }
158 else
159 {
160 ipsec_esp_backend_t *esp =
161 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
162 ASSERT (esp->check_support_cb);
163 error = esp->check_support_cb (sa);
164 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100165 return error;
166}
167
168
169static void
170ipsec_add_node (vlib_main_t * vm, const char *node_name,
171 const char *prev_node_name, u32 * out_node_index,
172 u32 * out_next_index)
173{
174 vlib_node_t *prev_node, *node;
175 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
176 ASSERT (prev_node);
177 node = vlib_get_node_by_name (vm, (u8 *) node_name);
178 ASSERT (node);
179 *out_node_index = node->index;
180 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
181}
182
Neale Rannsabc56602020-04-01 09:45:23 +0000183void
Neale Rannsabc56602020-04-01 09:45:23 +0000184ipsec_unregister_udp_port (u16 port)
185{
186 ipsec_main_t *im = &ipsec_main;
187 u32 n_regs;
188 uword *p;
189
190 p = hash_get (im->udp_port_registrations, port);
191
192 ASSERT (p);
193
194 n_regs = p[0];
195
196 if (0 == --n_regs)
197 {
198 udp_unregister_dst_port (vlib_get_main (), port, 1);
199 hash_unset (im->udp_port_registrations, port);
200 }
201 else
202 {
203 hash_unset (im->udp_port_registrations, port);
204 hash_set (im->udp_port_registrations, port, n_regs);
205 }
206}
207
208void
209ipsec_register_udp_port (u16 port)
210{
211 ipsec_main_t *im = &ipsec_main;
212 u32 n_regs;
213 uword *p;
214
215 p = hash_get (im->udp_port_registrations, port);
216
217 n_regs = (p ? p[0] : 0);
218
219 if (0 == n_regs++)
220 udp_register_dst_port (vlib_get_main (), port,
221 ipsec4_tun_input_node.index, 1);
222
223 hash_unset (im->udp_port_registrations, port);
224 hash_set (im->udp_port_registrations, port, n_regs);
225}
226
Klement Sekerab4d30532018-11-08 13:00:02 +0100227u32
228ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
229 const char *name,
230 const char *ah4_encrypt_node_name,
231 const char *ah4_decrypt_node_name,
232 const char *ah6_encrypt_node_name,
233 const char *ah6_decrypt_node_name,
234 check_support_cb_t ah_check_support_cb,
235 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
236{
237 ipsec_ah_backend_t *b;
238 pool_get (im->ah_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500239 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100240
Pierre Pfister057b3562018-12-10 17:01:01 +0100241 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100242 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100243 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100244 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100245 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100246 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100247 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100248 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
249
250 b->check_support_cb = ah_check_support_cb;
251 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
252 return b - im->ah_backends;
253}
254
255u32
Neale Ranns4a58e492020-12-21 13:19:10 +0000256ipsec_register_esp_backend (
257 vlib_main_t *vm, ipsec_main_t *im, const char *name,
258 const char *esp4_encrypt_node_name, const char *esp4_encrypt_node_tun_name,
259 const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name,
260 const char *esp6_encrypt_node_name, const char *esp6_encrypt_node_tun_name,
261 const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name,
262 const char *esp_mpls_encrypt_node_tun_name,
263 check_support_cb_t esp_check_support_cb,
264 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
265 enable_disable_cb_t enable_disable_cb)
Klement Sekerab4d30532018-11-08 13:00:02 +0100266{
267 ipsec_esp_backend_t *b;
Neale Rannse8915fc2019-04-23 20:57:55 -0400268
Klement Sekerab4d30532018-11-08 13:00:02 +0100269 pool_get (im->esp_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500270 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100271
Pierre Pfister057b3562018-12-10 17:01:01 +0100272 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100273 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100274 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100275 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100276 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100277 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100278 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100279 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
Neale Ranns7ec120e2020-01-21 04:58:02 +0000280 ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
281 &b->esp4_decrypt_tun_node_index,
282 &b->esp4_decrypt_tun_next_index);
283 ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
284 &b->esp6_decrypt_tun_node_index,
285 &b->esp6_decrypt_tun_next_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100286
Neale Ranns4ec36c52020-03-31 09:21:29 -0400287 b->esp6_encrypt_tun_node_index =
288 vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index;
Neale Ranns4a58e492020-12-21 13:19:10 +0000289 b->esp_mpls_encrypt_tun_node_index =
290 vlib_get_node_by_name (vm, (u8 *) esp_mpls_encrypt_node_tun_name)->index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400291 b->esp4_encrypt_tun_node_index =
292 vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400293
Klement Sekerab4d30532018-11-08 13:00:02 +0100294 b->check_support_cb = esp_check_support_cb;
295 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
Fan Zhangf5395782020-04-29 14:00:03 +0100296 b->enable_disable_cb = enable_disable_cb;
297
Klement Sekerab4d30532018-11-08 13:00:02 +0100298 return b - im->esp_backends;
299}
300
Neale Rannse8915fc2019-04-23 20:57:55 -0400301clib_error_t *
302ipsec_rsc_in_use (ipsec_main_t * im)
Neale Rannsb4cfd552019-02-13 02:08:06 -0800303{
Neale Rannse8915fc2019-04-23 20:57:55 -0400304 /* return an error is crypto resource are in use */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000305 if (pool_elts (ipsec_sa_pool) > 0)
306 return clib_error_return (0, "%d SA entries configured",
307 pool_elts (ipsec_sa_pool));
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000308 if (ipsec_itf_count () > 0)
309 return clib_error_return (0, "%d IPSec interface configured",
310 ipsec_itf_count ());
Neale Rannsb4cfd552019-02-13 02:08:06 -0800311
Neale Rannse8915fc2019-04-23 20:57:55 -0400312 return (NULL);
Neale Rannsb4cfd552019-02-13 02:08:06 -0800313}
314
Klement Sekerab4d30532018-11-08 13:00:02 +0100315int
316ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
317{
Neale Rannse8915fc2019-04-23 20:57:55 -0400318 if (ipsec_rsc_in_use (im))
319 return VNET_API_ERROR_RSRC_IN_USE;
320
321 if (pool_is_free_index (im->ah_backends, backend_idx))
322 return VNET_API_ERROR_INVALID_VALUE;
323
Klement Sekerab4d30532018-11-08 13:00:02 +0100324 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
325 im->ah_current_backend = backend_idx;
326 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
327 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
328 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
329 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
330 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
331 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
332 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
333 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800334
Klement Sekerab4d30532018-11-08 13:00:02 +0100335 return 0;
336}
337
338int
339ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
340{
Neale Rannse8915fc2019-04-23 20:57:55 -0400341 if (ipsec_rsc_in_use (im))
342 return VNET_API_ERROR_RSRC_IN_USE;
343
344 if (pool_is_free_index (im->esp_backends, backend_idx))
345 return VNET_API_ERROR_INVALID_VALUE;
346
Fan Zhangf5395782020-04-29 14:00:03 +0100347 /* disable current backend */
348 if (im->esp_current_backend != ~0)
349 {
350 ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
351 im->esp_current_backend);
352 if (cb->enable_disable_cb)
353 {
354 if ((cb->enable_disable_cb) (0) != 0)
355 return -1;
356 }
357 }
358
Klement Sekerab4d30532018-11-08 13:00:02 +0100359 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
360 im->esp_current_backend = backend_idx;
361 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
362 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
363 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
364 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
365 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
366 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
367 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
368 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
Neale Ranns7ec120e2020-01-21 04:58:02 +0000369 im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
370 im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
371 im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
372 im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400373 im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index;
374 im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index;
Neale Ranns4a58e492020-12-21 13:19:10 +0000375 im->esp_mpls_encrypt_tun_node_index = b->esp_mpls_encrypt_tun_node_index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400376
Fan Zhangf5395782020-04-29 14:00:03 +0100377 if (b->enable_disable_cb)
378 {
379 if ((b->enable_disable_cb) (1) != 0)
380 return -1;
381 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100382 return 0;
383}
384
Fan Zhangf5395782020-04-29 14:00:03 +0100385void
386ipsec_set_async_mode (u32 is_enabled)
387{
388 ipsec_main_t *im = &ipsec_main;
389 ipsec_sa_t *sa;
390
Neale Rannsf16e9a52021-02-25 19:09:24 +0000391 vnet_crypto_request_async_mode (is_enabled);
Fan Zhangf5395782020-04-29 14:00:03 +0100392
393 im->async_mode = is_enabled;
394
Neale Rannsf16e9a52021-02-25 19:09:24 +0000395 /* change SA crypto op data */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000396 pool_foreach (sa, ipsec_sa_pool)
397 {
398 sa->crypto_op_data =
Neale Rannsf16e9a52021-02-25 19:09:24 +0000399 (is_enabled ? sa->async_op_data.data : sa->sync_op_data.data);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000400 }
Fan Zhangf5395782020-04-29 14:00:03 +0100401}
402
403static void
404crypto_engine_backend_register_post_node (vlib_main_t * vm)
405{
406 esp_async_post_next_t *eit;
407 esp_async_post_next_t *dit;
408
409 eit = &esp_encrypt_async_next;
410 eit->esp4_post_next =
411 vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
412 eit->esp6_post_next =
413 vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
414 eit->esp4_tun_post_next =
415 vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
416 eit->esp6_tun_post_next =
417 vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
Neale Ranns4a58e492020-12-21 13:19:10 +0000418 eit->esp_mpls_tun_post_next =
419 vnet_crypto_register_post_node (vm, "esp-mpls-encrypt-tun-post");
Fan Zhangf5395782020-04-29 14:00:03 +0100420
421 dit = &esp_decrypt_async_next;
422 dit->esp4_post_next =
423 vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
424 dit->esp6_post_next =
425 vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
426 dit->esp4_tun_post_next =
427 vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
428 dit->esp6_tun_post_next =
429 vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
430}
431
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000432static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433ipsec_init (vlib_main_t * vm)
434{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700435 clib_error_t *error;
436 ipsec_main_t *im = &ipsec_main;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100437 ipsec_main_crypto_alg_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
Dave Barach0dd91652019-05-05 13:34:28 -0400439 /* Backend registration requires the feature arcs to be set up */
440 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
441 return (error);
442
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700443 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 im->vlib_main = vm;
445
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700446 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
447 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
449
Klement Sekerab4d30532018-11-08 13:00:02 +0100450 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700451 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 im->error_drop_node_index = node->index;
453
Fan Zhangf5395782020-04-29 14:00:03 +0100454 im->ah_current_backend = ~0;
455 im->esp_current_backend = ~0;
456
Neale Rannsd1a5b2d2019-05-16 17:09:28 +0000457 u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
Klement Sekerab4d30532018-11-08 13:00:02 +0100458 "ah4-encrypt",
459 "ah4-decrypt",
460 "ah6-encrypt",
461 "ah6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100462 ipsec_check_ah_support,
Klement Sekerab4d30532018-11-08 13:00:02 +0100463 NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464
Klement Sekerab4d30532018-11-08 13:00:02 +0100465 im->ah_default_backend = idx;
466 int rv = ipsec_select_ah_backend (im, idx);
467 ASSERT (0 == rv);
468 (void) (rv); // avoid warning
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000469
Neale Ranns4a58e492020-12-21 13:19:10 +0000470 idx = ipsec_register_esp_backend (
471 vm, im, "crypto engine backend", "esp4-encrypt", "esp4-encrypt-tun",
472 "esp4-decrypt", "esp4-decrypt-tun", "esp6-encrypt", "esp6-encrypt-tun",
473 "esp6-decrypt", "esp6-decrypt-tun", "esp-mpls-encrypt-tun",
474 ipsec_check_esp_support, NULL, crypto_dispatch_enable_disable);
Klement Sekerab4d30532018-11-08 13:00:02 +0100475 im->esp_default_backend = idx;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800476
Klement Sekerab4d30532018-11-08 13:00:02 +0100477 rv = ipsec_select_esp_backend (im, idx);
478 ASSERT (0 == rv);
479 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
482 return error;
483
Damjan Marion91f17dc2019-03-18 18:59:25 +0100484 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
485
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000486 a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
487 a->enc_op_id = VNET_CRYPTO_OP_NONE;
488 a->dec_op_id = VNET_CRYPTO_OP_NONE;
489 a->alg = VNET_CRYPTO_ALG_NONE;
490 a->iv_size = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500491 a->block_align = 1;
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000492
Damjan Marion91f17dc2019-03-18 18:59:25 +0100493 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100494 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
495 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200496 a->alg = VNET_CRYPTO_ALG_DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500497 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100498
499 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100500 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
501 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200502 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500503 a->iv_size = a->block_align = 8;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100504
505 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
Damjan Marion060bfb92019-03-29 13:47:54 +0100506 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
507 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200508 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500509 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100510
511 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
Damjan Marion060bfb92019-03-29 13:47:54 +0100512 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
513 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200514 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500515 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100516
517 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
Damjan Marion060bfb92019-03-29 13:47:54 +0100518 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
519 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200520 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500521 a->iv_size = a->block_align = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100522
Benoît Ganne490b9272021-01-22 18:03:09 +0100523 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128;
524 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
525 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC;
526 a->alg = VNET_CRYPTO_ALG_AES_128_CTR;
527 a->iv_size = 8;
528 a->block_align = 1;
529
530 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192;
531 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC;
532 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC;
533 a->alg = VNET_CRYPTO_ALG_AES_192_CTR;
534 a->iv_size = 8;
535 a->block_align = 1;
536
537 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256;
538 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
539 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC;
540 a->alg = VNET_CRYPTO_ALG_AES_256_CTR;
541 a->iv_size = 8;
542 a->block_align = 1;
543
Neale Ranns47feb112019-04-11 15:14:07 +0000544 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
545 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
546 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200547 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100548 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500549 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000550 a->icv_size = 16;
551
552 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
553 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
554 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200555 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100556 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500557 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000558 a->icv_size = 16;
559
560 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
561 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
562 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200563 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
Damjan Marionf1ecb652020-02-10 19:21:14 +0100564 a->iv_size = 8;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500565 a->block_align = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000566 a->icv_size = 16;
567
Damjan Marion91f17dc2019-03-18 18:59:25 +0100568 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
569 ipsec_main_integ_alg_t *i;
570
Dmitry Vakhrushev77cc14a2019-08-14 00:12:33 -0400571 i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
572 i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
573 i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
574 i->icv_size = 12;
575
Damjan Marion91f17dc2019-03-18 18:59:25 +0100576 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100577 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200578 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200579 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100580
581 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100582 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200583 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200584 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100585
586 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Damjan Marion060bfb92019-03-29 13:47:54 +0100587 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200588 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200589 i->icv_size = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100590
591 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Damjan Marion060bfb92019-03-29 13:47:54 +0100592 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
Damjan Marion4cb83812019-04-24 17:32:01 +0200593 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200594 i->icv_size = 24;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100595
596 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Damjan Marion060bfb92019-03-29 13:47:54 +0100597 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
Damjan Mariond1bed682019-04-24 15:20:35 +0200598 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200599 i->icv_size = 32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Neale Ranns5ae793a2019-04-03 13:36:56 +0000601 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100602
Fan Zhangf5395782020-04-29 14:00:03 +0100603 im->async_mode = 0;
604 crypto_engine_backend_register_post_node (vm);
605
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000606 im->ipsec4_out_spd_hash_tbl = NULL;
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500607 im->output_flow_cache_flag = 0;
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000608 im->ipsec4_out_spd_flow_cache_entries = 0;
609 im->epoch_count = 0;
610 im->ipsec4_out_spd_hash_num_buckets =
611 IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS;
612
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500613 im->ipsec4_in_spd_hash_tbl = NULL;
614 im->input_flow_cache_flag = 0;
615 im->ipsec4_in_spd_flow_cache_entries = 0;
616 im->input_epoch_count = 0;
617 im->ipsec4_in_spd_hash_num_buckets = IPSEC4_SPD_DEFAULT_HASH_NUM_BUCKETS;
618
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200619 vec_validate_init_empty_aligned (im->next_header_registrations, 255, ~0,
620 CLIB_CACHE_LINE_BYTES);
621
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 return 0;
623}
624
625VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700626
Zachary Leafb2d36782021-07-27 05:18:47 -0500627static clib_error_t *
628ipsec_config (vlib_main_t *vm, unformat_input_t *input)
629{
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000630 ipsec_main_t *im = &ipsec_main;
Zachary Leafb2d36782021-07-27 05:18:47 -0500631 unformat_input_t sub_input;
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500632
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000633 u32 ipsec4_out_spd_hash_num_buckets;
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500634 u32 ipsec4_in_spd_hash_num_buckets;
Zachary Leafb2d36782021-07-27 05:18:47 -0500635
636 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
637 {
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000638 if (unformat (input, "ipv4-outbound-spd-flow-cache on"))
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500639 im->output_flow_cache_flag = 1;
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000640 else if (unformat (input, "ipv4-outbound-spd-flow-cache off"))
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500641 im->output_flow_cache_flag = 0;
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000642 else if (unformat (input, "ipv4-outbound-spd-hash-buckets %d",
643 &ipsec4_out_spd_hash_num_buckets))
644 {
645 /* Size of hash is power of 2 >= number of buckets */
646 im->ipsec4_out_spd_hash_num_buckets =
647 1ULL << max_log2 (ipsec4_out_spd_hash_num_buckets);
648 }
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500649 else if (unformat (input, "ipv4-inbound-spd-flow-cache on"))
650 im->input_flow_cache_flag = 1;
651 else if (unformat (input, "ipv4-inbound-spd-flow-cache off"))
652 im->input_flow_cache_flag = 0;
653 else if (unformat (input, "ipv4-inbound-spd-hash-buckets %d",
654 &ipsec4_in_spd_hash_num_buckets))
655 {
656 im->ipsec4_in_spd_hash_num_buckets =
657 1ULL << max_log2 (ipsec4_in_spd_hash_num_buckets);
658 }
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000659 else if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input,
660 &sub_input))
Zachary Leafb2d36782021-07-27 05:18:47 -0500661 {
662 uword table_size = ~0;
663 u32 n_buckets = ~0;
664
665 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
666 {
667 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
668 ;
669 else
670 return clib_error_return (0, "unknown input `%U'",
671 format_unformat_error, &sub_input);
672 }
673
674 ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
675 }
676 else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
677 &sub_input))
678 {
679 uword table_size = ~0;
680 u32 n_buckets = ~0;
681
682 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
683 {
684 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
685 ;
686 else
687 return clib_error_return (0, "unknown input `%U'",
688 format_unformat_error, &sub_input);
689 }
690
691 ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
692 }
693 else
694 return clib_error_return (0, "unknown input `%U'",
695 format_unformat_error, input);
696 }
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500697 if (im->output_flow_cache_flag)
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000698 {
699 vec_add2 (im->ipsec4_out_spd_hash_tbl, im->ipsec4_out_spd_hash_tbl,
700 im->ipsec4_out_spd_hash_num_buckets);
701 }
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500702 if (im->input_flow_cache_flag)
703 {
704 vec_add2 (im->ipsec4_in_spd_hash_tbl, im->ipsec4_in_spd_hash_tbl,
705 im->ipsec4_in_spd_hash_num_buckets);
706 }
Zachary Leafb2d36782021-07-27 05:18:47 -0500707
708 return 0;
709}
710
711VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
712
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700713/*
714 * fd.io coding-style-patch-verification: ON
715 *
716 * Local Variables:
717 * eval: (c-set-style "gnu")
718 * End:
719 */