2 * decap.c : IPSec tunnel support
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/ikev2.h>
26 #include <vnet/ipsec/esp.h>
27 #include <vnet/ipsec/ah.h>
30 ipsec_main_t ipsec_main;
33 ipsec_get_sa_index_by_sa_id (u32 sa_id)
35 ipsec_main_t *im = &ipsec_main;
36 uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
44 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
47 ipsec_main_t *im = &ipsec_main;
48 ip4_ipsec_config_t config;
53 p = hash_get (im->spd_index_by_spd_id, spd_id);
55 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
59 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
61 return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
65 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
69 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
72 clib_warning ("sw_if_index %u spd_id %u spd_index %u",
73 sw_if_index, spd_id, spd_index);
75 /* enable IPsec on TX */
76 vnet_feature_enable_disable ("ip4-output", "ipsec4-output", sw_if_index,
78 vnet_feature_enable_disable ("ip6-output", "ipsec6-output", sw_if_index,
81 config.spd_index = spd_index;
83 /* enable IPsec on RX */
84 vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input", sw_if_index,
85 is_add, &config, sizeof (config));
86 vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input", sw_if_index,
87 is_add, &config, sizeof (config));
93 ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
95 ipsec_main_t *im = &ipsec_main;
100 p = hash_get (im->spd_index_by_spd_id, spd_id);
102 return VNET_API_ERROR_INVALID_VALUE;
104 return VNET_API_ERROR_INVALID_VALUE;
106 if (!is_add) /* delete */
109 spd = pool_elt_at_index (im->spds, spd_index);
111 return VNET_API_ERROR_INVALID_VALUE;
113 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
115 ipsec_set_interface_spd(vm, k, spd_id, 0);
118 hash_unset (im->spd_index_by_spd_id, spd_id);
119 pool_free (spd->policies);
120 vec_free (spd->ipv4_outbound_policies);
121 vec_free (spd->ipv6_outbound_policies);
122 vec_free (spd->ipv4_inbound_protect_policy_indices);
123 vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices);
124 pool_put (im->spds, spd);
126 else /* create new SPD */
128 pool_get (im->spds, spd);
129 clib_memset (spd, 0, sizeof (*spd));
130 spd_index = spd - im->spds;
132 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
138 ipsec_spd_entry_sort (void *a1, void *a2)
142 ipsec_spd_t *spd = ipsec_main.spd_to_sort;
143 ipsec_policy_t *p1, *p2;
145 p1 = pool_elt_at_index (spd->policies, *id1);
146 p2 = pool_elt_at_index (spd->policies, *id2);
148 return p2->priority - p1->priority;
154 ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
156 ipsec_main_t *im = &ipsec_main;
157 ipsec_spd_t *spd = 0;
162 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
163 policy->priority, policy->is_outbound);
165 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
167 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
169 return VNET_API_ERROR_SYSCALL_ERROR_1;
170 policy->sa_index = p[0];
173 p = hash_get (im->spd_index_by_spd_id, policy->id);
176 return VNET_API_ERROR_SYSCALL_ERROR_1;
179 spd = pool_elt_at_index (im->spds, spd_index);
181 return VNET_API_ERROR_SYSCALL_ERROR_1;
187 pool_get (spd->policies, vp);
188 clib_memcpy (vp, policy, sizeof (*vp));
189 policy_index = vp - spd->policies;
191 ipsec_main.spd_to_sort = spd;
193 if (policy->is_outbound)
197 vec_add1 (spd->ipv6_outbound_policies, policy_index);
198 vec_sort_with_function (spd->ipv6_outbound_policies,
199 ipsec_spd_entry_sort);
203 vec_add1 (spd->ipv4_outbound_policies, policy_index);
204 vec_sort_with_function (spd->ipv4_outbound_policies,
205 ipsec_spd_entry_sort);
212 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
214 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
216 vec_sort_with_function
217 (spd->ipv6_inbound_protect_policy_indices,
218 ipsec_spd_entry_sort);
223 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
225 vec_sort_with_function
226 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
227 ipsec_spd_entry_sort);
232 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
234 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
236 vec_sort_with_function
237 (spd->ipv4_inbound_protect_policy_indices,
238 ipsec_spd_entry_sort);
243 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
245 vec_sort_with_function
246 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
247 ipsec_spd_entry_sort);
252 ipsec_main.spd_to_sort = NULL;
258 pool_foreach_index(i, spd->policies, ({
259 vp = pool_elt_at_index(spd->policies, i);
260 if (vp->priority != policy->priority)
262 if (vp->is_outbound != policy->is_outbound)
264 if (vp->policy != policy->policy)
266 if (vp->sa_id != policy->sa_id)
268 if (vp->protocol != policy->protocol)
270 if (vp->lport.start != policy->lport.start)
272 if (vp->lport.stop != policy->lport.stop)
274 if (vp->rport.start != policy->rport.start)
276 if (vp->rport.stop != policy->rport.stop)
278 if (vp->is_ipv6 != policy->is_ipv6)
282 if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
284 if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
286 if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
288 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
290 if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
292 if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
294 if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
296 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
298 if (policy->is_outbound)
300 vec_foreach_index(j, spd->ipv6_outbound_policies) {
301 if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
302 vec_del1 (spd->ipv6_outbound_policies, j);
309 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
311 vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
312 if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
313 vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
320 vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
321 if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
322 vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
331 if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
333 if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
335 if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
337 if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
339 if (policy->is_outbound)
341 vec_foreach_index(j, spd->ipv4_outbound_policies) {
342 if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
343 vec_del1 (spd->ipv4_outbound_policies, j);
350 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
352 vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
353 if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
354 vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
361 vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
362 if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
363 vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
370 pool_put (spd->policies, vp);
380 ipsec_is_sa_used (u32 sa_index)
382 ipsec_main_t *im = &ipsec_main;
385 ipsec_tunnel_if_t *t;
388 pool_foreach(spd, im->spds, ({
389 pool_foreach(p, spd->policies, ({
390 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
392 if (p->sa_index == sa_index)
398 pool_foreach(t, im->tunnel_interfaces, ({
399 if (t->input_sa_index == sa_index)
401 if (t->output_sa_index == sa_index)
410 ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa,
411 u32 sa_index, int is_add)
413 ipsec_ah_backend_t *ab;
414 ipsec_esp_backend_t *eb;
415 switch (sa->protocol)
417 case IPSEC_PROTOCOL_AH:
418 ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend);
419 if (ab->add_del_sa_sess_cb)
420 return ab->add_del_sa_sess_cb (sa_index, is_add);
422 case IPSEC_PROTOCOL_ESP:
423 eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend);
424 if (eb->add_del_sa_sess_cb)
425 return eb->add_del_sa_sess_cb (sa_index, is_add);
432 ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
434 ipsec_main_t *im = &ipsec_main;
440 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
442 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
444 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
446 return VNET_API_ERROR_SYSCALL_ERROR_1;
448 if (!is_add) /* delete */
451 sa = pool_elt_at_index (im->sad, sa_index);
452 if (ipsec_is_sa_used (sa_index))
454 clib_warning ("sa_id %u used in policy", sa->id);
455 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
457 hash_unset (im->sa_index_by_sa_id, sa->id);
458 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
460 return VNET_API_ERROR_SYSCALL_ERROR_1;
461 pool_put (im->sad, sa);
463 else /* create new SA */
465 pool_get (im->sad, sa);
466 clib_memcpy (sa, new_sa, sizeof (*sa));
467 sa_index = sa - im->sad;
468 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
469 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
471 return VNET_API_ERROR_SYSCALL_ERROR_1;
477 ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
479 ipsec_main_t *im = &ipsec_main;
485 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
487 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
490 sa = pool_elt_at_index (im->sad, sa_index);
493 if (0 < sa_update->crypto_key_len)
495 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
496 sa_update->crypto_key_len);
497 sa->crypto_key_len = sa_update->crypto_key_len;
501 if (0 < sa_update->integ_key_len)
503 clib_memcpy (sa->integ_key, sa_update->integ_key,
504 sa_update->integ_key_len);
505 sa->integ_key_len = sa_update->integ_key_len;
508 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
510 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
512 return VNET_API_ERROR_SYSCALL_ERROR_1;
519 ipsec_rand_seed (void)
528 seed_data.time = time (NULL);
529 seed_data.pid = getpid ();
530 seed_data.p = (void *) &seed_data;
532 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
535 static clib_error_t *
536 ipsec_check_support (ipsec_sa_t * sa)
538 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
539 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
540 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
541 return clib_error_return (0, "unsupported none integ-alg");
547 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
549 ipsec_ah_backend_t *ah =
550 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
551 if (ah->add_del_sa_sess_cb)
553 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
557 ipsec_esp_backend_t *esp =
558 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
559 if (esp->add_del_sa_sess_cb)
561 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
569 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
571 clib_error_t *error = 0;
572 ipsec_ah_backend_t *ah =
573 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
574 ASSERT (ah->check_support_cb);
575 error = ah->check_support_cb (sa);
578 ipsec_esp_backend_t *esp =
579 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
580 ASSERT (esp->check_support_cb);
581 error = esp->check_support_cb (sa);
587 ipsec_add_node (vlib_main_t * vm, const char *node_name,
588 const char *prev_node_name, u32 * out_node_index,
589 u32 * out_next_index)
591 vlib_node_t *prev_node, *node;
592 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
594 node = vlib_get_node_by_name (vm, (u8 *) node_name);
596 *out_node_index = node->index;
597 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
601 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
603 const char *ah4_encrypt_node_name,
604 const char *ah4_decrypt_node_name,
605 const char *ah6_encrypt_node_name,
606 const char *ah6_decrypt_node_name,
607 check_support_cb_t ah_check_support_cb,
608 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
610 ipsec_ah_backend_t *b;
611 pool_get (im->ah_backends, b);
612 b->name = format (NULL, "%s", name);
614 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output",
615 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
616 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input",
617 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
618 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output",
619 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
620 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input",
621 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
623 b->check_support_cb = ah_check_support_cb;
624 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
625 return b - im->ah_backends;
629 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
631 const char *esp4_encrypt_node_name,
632 const char *esp4_decrypt_node_name,
633 const char *esp6_encrypt_node_name,
634 const char *esp6_decrypt_node_name,
635 check_support_cb_t esp_check_support_cb,
636 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
638 ipsec_esp_backend_t *b;
639 pool_get (im->esp_backends, b);
640 b->name = format (NULL, "%s", name);
642 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output",
643 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
644 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input",
645 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
646 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output",
647 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
648 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input",
649 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
651 b->check_support_cb = esp_check_support_cb;
652 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
653 return b - im->esp_backends;
657 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
659 if (pool_elts (im->sad) > 0
660 || pool_is_free_index (im->ah_backends, backend_idx))
664 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
665 im->ah_current_backend = backend_idx;
666 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
667 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
668 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
669 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
670 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
671 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
672 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
673 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
678 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
680 if (pool_elts (im->sad) > 0
681 || pool_is_free_index (im->esp_backends, backend_idx))
685 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
686 im->esp_current_backend = backend_idx;
687 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
688 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
689 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
690 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
691 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
692 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
693 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
694 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
698 static clib_error_t *
699 ipsec_init (vlib_main_t * vm)
702 ipsec_main_t *im = &ipsec_main;
703 vlib_thread_main_t *tm = vlib_get_thread_main ();
707 clib_memset (im, 0, sizeof (im[0]));
709 im->vnet_main = vnet_get_main ();
712 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
713 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
714 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
716 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
717 CLIB_CACHE_LINE_BYTES);
719 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
721 im->error_drop_node_index = node->index;
723 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
731 im->ah_default_backend = idx;
732 int rv = ipsec_select_ah_backend (im, idx);
734 (void) (rv); // avoid warning
736 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
741 ipsec_check_support, NULL);
742 im->esp_default_backend = idx;
744 rv = ipsec_select_esp_backend (im, idx);
746 (void) (rv); // avoid warning
748 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
751 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
756 if ((error = ikev2_init (vm)))
762 VLIB_INIT_FUNCTION (ipsec_init);
765 * fd.io coding-style-patch-verification: ON
768 * eval: (c-set-style "gnu")