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 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
199 vec_sort_with_function (spd->ipv6_outbound_policies,
200 ipsec_spd_entry_sort);
204 vec_add1 (spd->ipv4_outbound_policies, policy_index);
205 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
206 vec_sort_with_function (spd->ipv4_outbound_policies,
207 ipsec_spd_entry_sort);
214 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
216 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
218 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
219 vec_sort_with_function
220 (spd->ipv6_inbound_protect_policy_indices,
221 ipsec_spd_entry_sort);
226 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
228 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
229 vec_sort_with_function
230 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
231 ipsec_spd_entry_sort);
236 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
238 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
240 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
241 vec_sort_with_function
242 (spd->ipv4_inbound_protect_policy_indices,
243 ipsec_spd_entry_sort);
248 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
250 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
251 vec_sort_with_function
252 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
253 ipsec_spd_entry_sort);
258 ipsec_main.spd_to_sort = NULL;
264 pool_foreach_index(i, spd->policies, ({
265 vp = pool_elt_at_index(spd->policies, i);
266 if (vp->priority != policy->priority)
268 if (vp->is_outbound != policy->is_outbound)
270 if (vp->policy != policy->policy)
272 if (vp->sa_id != policy->sa_id)
274 if (vp->protocol != policy->protocol)
276 if (vp->lport.start != policy->lport.start)
278 if (vp->lport.stop != policy->lport.stop)
280 if (vp->rport.start != policy->rport.start)
282 if (vp->rport.stop != policy->rport.stop)
284 if (vp->is_ipv6 != policy->is_ipv6)
288 if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
290 if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
292 if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
294 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
296 if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
298 if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
300 if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
302 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
304 if (policy->is_outbound)
306 vec_foreach_index(j, spd->ipv6_outbound_policies) {
307 if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
308 vec_del1 (spd->ipv6_outbound_policies, j);
315 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
317 vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
318 if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
319 vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
326 vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
327 if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
328 vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
337 if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
339 if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
341 if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
343 if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
345 if (policy->is_outbound)
347 vec_foreach_index(j, spd->ipv4_outbound_policies) {
348 if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
349 vec_del1 (spd->ipv4_outbound_policies, j);
356 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
358 vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
359 if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
360 vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
367 vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
368 if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
369 vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
376 pool_put (spd->policies, vp);
386 ipsec_is_sa_used (u32 sa_index)
388 ipsec_main_t *im = &ipsec_main;
391 ipsec_tunnel_if_t *t;
394 pool_foreach(spd, im->spds, ({
395 pool_foreach(p, spd->policies, ({
396 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
398 if (p->sa_index == sa_index)
404 pool_foreach(t, im->tunnel_interfaces, ({
405 if (t->input_sa_index == sa_index)
407 if (t->output_sa_index == sa_index)
416 ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa,
417 u32 sa_index, int is_add)
419 ipsec_ah_backend_t *ab;
420 ipsec_esp_backend_t *eb;
421 switch (sa->protocol)
423 case IPSEC_PROTOCOL_AH:
424 ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend);
425 if (ab->add_del_sa_sess_cb)
426 return ab->add_del_sa_sess_cb (sa_index, is_add);
428 case IPSEC_PROTOCOL_ESP:
429 eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend);
430 if (eb->add_del_sa_sess_cb)
431 return eb->add_del_sa_sess_cb (sa_index, is_add);
438 ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
440 ipsec_main_t *im = &ipsec_main;
446 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
448 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
450 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
452 return VNET_API_ERROR_SYSCALL_ERROR_1;
454 if (!is_add) /* delete */
457 sa = pool_elt_at_index (im->sad, sa_index);
458 if (ipsec_is_sa_used (sa_index))
460 clib_warning ("sa_id %u used in policy", sa->id);
461 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
463 hash_unset (im->sa_index_by_sa_id, sa->id);
464 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
466 return VNET_API_ERROR_SYSCALL_ERROR_1;
467 pool_put (im->sad, sa);
469 else /* create new SA */
471 pool_get (im->sad, sa);
472 clib_memcpy (sa, new_sa, sizeof (*sa));
473 sa_index = sa - im->sad;
474 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
475 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
477 return VNET_API_ERROR_SYSCALL_ERROR_1;
483 ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
485 ipsec_main_t *im = &ipsec_main;
491 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
493 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
496 sa = pool_elt_at_index (im->sad, sa_index);
499 if (0 < sa_update->crypto_key_len)
501 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
502 sa_update->crypto_key_len);
503 sa->crypto_key_len = sa_update->crypto_key_len;
507 if (0 < sa_update->integ_key_len)
509 clib_memcpy (sa->integ_key, sa_update->integ_key,
510 sa_update->integ_key_len);
511 sa->integ_key_len = sa_update->integ_key_len;
514 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
516 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
518 return VNET_API_ERROR_SYSCALL_ERROR_1;
525 ipsec_rand_seed (void)
534 seed_data.time = time (NULL);
535 seed_data.pid = getpid ();
536 seed_data.p = (void *) &seed_data;
538 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
541 static clib_error_t *
542 ipsec_check_support (ipsec_sa_t * sa)
544 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
545 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
546 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
547 return clib_error_return (0, "unsupported none integ-alg");
553 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
555 ipsec_ah_backend_t *ah =
556 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
557 if (ah->add_del_sa_sess_cb)
559 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
563 ipsec_esp_backend_t *esp =
564 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
565 if (esp->add_del_sa_sess_cb)
567 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
575 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
577 clib_error_t *error = 0;
578 ipsec_ah_backend_t *ah =
579 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
580 ASSERT (ah->check_support_cb);
581 error = ah->check_support_cb (sa);
584 ipsec_esp_backend_t *esp =
585 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
586 ASSERT (esp->check_support_cb);
587 error = esp->check_support_cb (sa);
593 ipsec_add_node (vlib_main_t * vm, const char *node_name,
594 const char *prev_node_name, u32 * out_node_index,
595 u32 * out_next_index)
597 vlib_node_t *prev_node, *node;
598 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
600 node = vlib_get_node_by_name (vm, (u8 *) node_name);
602 *out_node_index = node->index;
603 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
607 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
609 const char *ah4_encrypt_node_name,
610 const char *ah4_decrypt_node_name,
611 const char *ah6_encrypt_node_name,
612 const char *ah6_decrypt_node_name,
613 check_support_cb_t ah_check_support_cb,
614 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
616 ipsec_ah_backend_t *b;
617 pool_get (im->ah_backends, b);
618 b->name = format (NULL, "%s", name);
620 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output",
621 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
622 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input",
623 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
624 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output",
625 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
626 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input",
627 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
629 b->check_support_cb = ah_check_support_cb;
630 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
631 return b - im->ah_backends;
635 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
637 const char *esp4_encrypt_node_name,
638 const char *esp4_decrypt_node_name,
639 const char *esp6_encrypt_node_name,
640 const char *esp6_decrypt_node_name,
641 check_support_cb_t esp_check_support_cb,
642 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
644 ipsec_esp_backend_t *b;
645 pool_get (im->esp_backends, b);
646 b->name = format (NULL, "%s", name);
648 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output",
649 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
650 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input",
651 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
652 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output",
653 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
654 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input",
655 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
657 b->check_support_cb = esp_check_support_cb;
658 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
659 return b - im->esp_backends;
663 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
665 if (pool_elts (im->sad) > 0
666 || pool_is_free_index (im->ah_backends, backend_idx))
670 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
671 im->ah_current_backend = backend_idx;
672 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
673 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
674 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
675 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
676 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
677 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
678 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
679 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
684 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
686 if (pool_elts (im->sad) > 0
687 || pool_is_free_index (im->esp_backends, backend_idx))
691 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
692 im->esp_current_backend = backend_idx;
693 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
694 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
695 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
696 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
697 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
698 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
699 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
700 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
704 static clib_error_t *
705 ipsec_init (vlib_main_t * vm)
708 ipsec_main_t *im = &ipsec_main;
709 vlib_thread_main_t *tm = vlib_get_thread_main ();
713 clib_memset (im, 0, sizeof (im[0]));
715 im->vnet_main = vnet_get_main ();
718 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
719 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
720 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
722 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
723 CLIB_CACHE_LINE_BYTES);
725 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
727 im->error_drop_node_index = node->index;
729 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
737 im->ah_default_backend = idx;
738 int rv = ipsec_select_ah_backend (im, idx);
740 (void) (rv); // avoid warning
742 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
747 ipsec_check_support, NULL);
748 im->esp_default_backend = idx;
750 rv = ipsec_select_esp_backend (im, idx);
752 (void) (rv); // avoid warning
754 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
757 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
762 if ((error = ikev2_init (vm)))
768 VLIB_INIT_FUNCTION (ipsec_init);
771 * fd.io coding-style-patch-verification: ON
774 * eval: (c-set-style "gnu")