2 * ipsec.c : IPSEC module functions
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/interface.h>
22 #include <vnet/udp/udp.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
26 #include <vnet/ipsec/ah.h>
28 ipsec_main_t ipsec_main;
29 esp_async_post_next_t esp_encrypt_async_next;
30 esp_async_post_next_t esp_decrypt_async_next;
33 ipsec_check_ah_support (ipsec_sa_t * sa)
35 ipsec_main_t *im = &ipsec_main;
37 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
38 return clib_error_return (0, "unsupported none integ-alg");
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);
48 ipsec_check_esp_support (ipsec_sa_t * sa)
50 ipsec_main_t *im = &ipsec_main;
52 if (IPSEC_INTEG_ALG_NONE != sa->integ_alg)
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);
58 if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg)
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);
69 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
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)
75 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
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)
83 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
91 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
93 clib_error_t *error = 0;
95 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
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);
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);
114 ipsec_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)
118 vlib_node_t *prev_node, *node;
119 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
121 node = vlib_get_node_by_name (vm, (u8 *) node_name);
123 *out_node_index = node->index;
124 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
128 ipsec_add_feature (const char *arc_name,
129 const char *node_name, u32 * out_feature_index)
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);
139 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
141 const char *ah4_encrypt_node_name,
142 const char *ah4_decrypt_node_name,
143 const char *ah6_encrypt_node_name,
144 const char *ah6_decrypt_node_name,
145 check_support_cb_t ah_check_support_cb,
146 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
148 ipsec_ah_backend_t *b;
149 pool_get (im->ah_backends, b);
150 b->name = format (0, "%s%c", name, 0);
152 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
153 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
154 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
155 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
156 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
157 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
158 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
159 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
161 b->check_support_cb = ah_check_support_cb;
162 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
163 return b - im->ah_backends;
167 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
169 const char *esp4_encrypt_node_name,
170 const char *esp4_encrypt_node_tun_name,
171 const char *esp4_decrypt_node_name,
172 const char *esp4_decrypt_tun_node_name,
173 const char *esp6_encrypt_node_name,
174 const char *esp6_encrypt_node_tun_name,
175 const char *esp6_decrypt_node_name,
176 const char *esp6_decrypt_tun_node_name,
177 check_support_cb_t esp_check_support_cb,
178 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
179 enable_disable_cb_t enable_disable_cb)
181 ipsec_esp_backend_t *b;
183 pool_get (im->esp_backends, b);
184 b->name = format (0, "%s%c", name, 0);
186 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
187 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
188 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
189 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
190 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
191 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
192 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
193 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
194 ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
195 &b->esp4_decrypt_tun_node_index,
196 &b->esp4_decrypt_tun_next_index);
197 ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
198 &b->esp6_decrypt_tun_node_index,
199 &b->esp6_decrypt_tun_next_index);
201 ipsec_add_feature ("ip4-output", esp4_encrypt_node_tun_name,
202 &b->esp44_encrypt_tun_feature_index);
203 ipsec_add_feature ("ip4-output", esp6_encrypt_node_tun_name,
204 &b->esp46_encrypt_tun_feature_index);
205 ipsec_add_feature ("ip6-output", esp6_encrypt_node_tun_name,
206 &b->esp66_encrypt_tun_feature_index);
207 ipsec_add_feature ("ip6-output", esp4_encrypt_node_tun_name,
208 &b->esp64_encrypt_tun_feature_index);
210 b->check_support_cb = esp_check_support_cb;
211 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
212 b->enable_disable_cb = enable_disable_cb;
214 return b - im->esp_backends;
218 ipsec_rsc_in_use (ipsec_main_t * im)
220 /* return an error is crypto resource are in use */
221 if (pool_elts (im->sad) > 0)
222 return clib_error_return (0,
223 "%d SA entries configured",
224 pool_elts (im->sad));
230 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
232 if (ipsec_rsc_in_use (im))
233 return VNET_API_ERROR_RSRC_IN_USE;
235 if (pool_is_free_index (im->ah_backends, backend_idx))
236 return VNET_API_ERROR_INVALID_VALUE;
238 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
239 im->ah_current_backend = backend_idx;
240 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
241 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
242 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
243 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
244 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
245 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
246 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
247 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
253 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
255 if (ipsec_rsc_in_use (im))
256 return VNET_API_ERROR_RSRC_IN_USE;
258 if (pool_is_free_index (im->esp_backends, backend_idx))
259 return VNET_API_ERROR_INVALID_VALUE;
261 /* disable current backend */
262 if (im->esp_current_backend != ~0)
264 ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
265 im->esp_current_backend);
266 if (cb->enable_disable_cb)
268 if ((cb->enable_disable_cb) (0) != 0)
273 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
274 im->esp_current_backend = backend_idx;
275 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
276 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
277 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
278 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
279 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
280 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
281 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
282 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
283 im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
284 im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
285 im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
286 im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
288 im->esp44_encrypt_tun_feature_index = b->esp44_encrypt_tun_feature_index;
289 im->esp64_encrypt_tun_feature_index = b->esp64_encrypt_tun_feature_index;
290 im->esp46_encrypt_tun_feature_index = b->esp46_encrypt_tun_feature_index;
291 im->esp66_encrypt_tun_feature_index = b->esp66_encrypt_tun_feature_index;
293 if (b->enable_disable_cb)
295 if ((b->enable_disable_cb) (1) != 0)
302 ipsec_set_async_mode (u32 is_enabled)
304 ipsec_main_t *im = &ipsec_main;
307 /* lock all SAs before change im->async_mode */
308 pool_foreach (sa, im->sad, (
310 fib_node_lock (&sa->node);
313 im->async_mode = is_enabled;
315 /* change SA crypto op data before unlock them */
316 pool_foreach (sa, im->sad, (
318 sa->crypto_op_data = is_enabled ?
319 sa->async_op_data.data : sa->sync_op_data.data;
320 fib_node_unlock (&sa->node);
325 crypto_engine_backend_register_post_node (vlib_main_t * vm)
327 esp_async_post_next_t *eit;
328 esp_async_post_next_t *dit;
330 eit = &esp_encrypt_async_next;
331 eit->esp4_post_next =
332 vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
333 eit->esp6_post_next =
334 vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
335 eit->esp4_tun_post_next =
336 vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
337 eit->esp6_tun_post_next =
338 vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
340 dit = &esp_decrypt_async_next;
341 dit->esp4_post_next =
342 vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
343 dit->esp6_post_next =
344 vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
345 dit->esp4_tun_post_next =
346 vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
347 dit->esp6_tun_post_next =
348 vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
351 static clib_error_t *
352 ipsec_init (vlib_main_t * vm)
355 ipsec_main_t *im = &ipsec_main;
356 ipsec_main_crypto_alg_t *a;
358 /* Backend registration requires the feature arcs to be set up */
359 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
362 im->vnet_main = vnet_get_main ();
365 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
366 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
367 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
369 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
371 im->error_drop_node_index = node->index;
373 im->ah_current_backend = ~0;
374 im->esp_current_backend = ~0;
376 u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
381 ipsec_check_ah_support,
384 im->ah_default_backend = idx;
385 int rv = ipsec_select_ah_backend (im, idx);
387 (void) (rv); // avoid warning
389 idx = ipsec_register_esp_backend (vm, im, "crypto engine backend",
398 ipsec_check_esp_support,
399 NULL, crypto_dispatch_enable_disable);
400 im->esp_default_backend = idx;
402 rv = ipsec_select_esp_backend (im, idx);
404 (void) (rv); // avoid warning
406 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
409 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
411 a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
412 a->enc_op_id = VNET_CRYPTO_OP_NONE;
413 a->dec_op_id = VNET_CRYPTO_OP_NONE;
414 a->alg = VNET_CRYPTO_ALG_NONE;
418 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
419 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
420 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
421 a->alg = VNET_CRYPTO_ALG_DES_CBC;
422 a->iv_size = a->block_size = 8;
424 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
425 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
426 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
427 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
428 a->iv_size = a->block_size = 8;
430 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
431 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
432 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
433 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
434 a->iv_size = a->block_size = 16;
436 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
437 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
438 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
439 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
440 a->iv_size = a->block_size = 16;
442 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
443 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
444 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
445 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
446 a->iv_size = a->block_size = 16;
448 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
449 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
450 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
451 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
456 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
457 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
458 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
459 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
464 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
465 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
466 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
467 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
472 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
473 ipsec_main_integ_alg_t *i;
475 i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
476 i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
477 i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
480 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
481 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
482 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
485 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
486 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
487 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
490 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
491 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
492 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
495 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
496 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
497 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
500 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
501 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
502 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
505 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
507 im->ah4_enc_fq_index =
508 vlib_frame_queue_main_init (ah4_encrypt_node.index, 0);
509 im->ah4_dec_fq_index =
510 vlib_frame_queue_main_init (ah4_decrypt_node.index, 0);
511 im->ah6_enc_fq_index =
512 vlib_frame_queue_main_init (ah6_encrypt_node.index, 0);
513 im->ah6_dec_fq_index =
514 vlib_frame_queue_main_init (ah6_decrypt_node.index, 0);
516 im->esp4_enc_fq_index =
517 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
518 im->esp4_dec_fq_index =
519 vlib_frame_queue_main_init (esp4_decrypt_node.index, 0);
520 im->esp6_enc_fq_index =
521 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
522 im->esp6_dec_fq_index =
523 vlib_frame_queue_main_init (esp6_decrypt_node.index, 0);
524 im->esp4_enc_tun_fq_index =
525 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
526 im->esp6_enc_tun_fq_index =
527 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
528 im->esp4_dec_tun_fq_index =
529 vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0);
530 im->esp6_dec_tun_fq_index =
531 vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0);
534 crypto_engine_backend_register_post_node (vm);
539 VLIB_INIT_FUNCTION (ipsec_init);
542 * fd.io coding-style-patch-verification: ON
545 * eval: (c-set-style "gnu")