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>
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/ikev2.h>
25 #include <vnet/ipsec/esp.h>
27 ipsec_main_t ipsec_main;
30 ipsec_get_sa_index_by_sa_id (u32 sa_id)
32 ipsec_main_t *im = &ipsec_main;
33 uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
41 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
44 ipsec_main_t *im = &ipsec_main;
45 ip4_ipsec_config_t config;
50 p = hash_get (im->spd_index_by_spd_id, spd_id);
52 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
56 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
58 return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
62 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
66 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
69 clib_warning ("sw_if_index %u spd_id %u spd_index %u",
70 sw_if_index, spd_id, spd_index);
72 /* enable IPsec on TX */
73 vnet_feature_enable_disable ("ip4-output", "ipsec-output-ip4", sw_if_index,
75 vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index,
78 /* enable IPsec on RX */
79 vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index,
80 is_add, &config, sizeof (config));
81 vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index,
82 is_add, &config, sizeof (config));
88 ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
90 ipsec_main_t *im = &ipsec_main;
95 p = hash_get (im->spd_index_by_spd_id, spd_id);
97 return VNET_API_ERROR_INVALID_VALUE;
99 return VNET_API_ERROR_INVALID_VALUE;
101 if (!is_add) /* delete */
104 spd = pool_elt_at_index (im->spds, spd_index);
106 return VNET_API_ERROR_INVALID_VALUE;
108 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
110 ipsec_set_interface_spd(vm, k, spd_id, 0);
113 hash_unset (im->spd_index_by_spd_id, spd_id);
114 pool_free (spd->policies);
115 vec_free (spd->ipv4_outbound_policies);
116 vec_free (spd->ipv6_outbound_policies);
117 vec_free (spd->ipv4_inbound_protect_policy_indices);
118 vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices);
119 pool_put (im->spds, spd);
121 else /* create new SPD */
123 pool_get (im->spds, spd);
124 memset (spd, 0, sizeof (*spd));
125 spd_index = spd - im->spds;
127 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
133 ipsec_spd_entry_sort (void *a1, void *a2)
135 ipsec_main_t *im = &ipsec_main;
139 ipsec_policy_t *p1, *p2;
142 pool_foreach (spd, im->spds, ({
143 p1 = pool_elt_at_index(spd->policies, *id1);
144 p2 = pool_elt_at_index(spd->policies, *id2);
146 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 if (policy->is_outbound)
195 vec_add1 (spd->ipv6_outbound_policies, policy_index);
196 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
197 vec_sort_with_function (spd->ipv6_outbound_policies,
198 ipsec_spd_entry_sort);
202 vec_add1 (spd->ipv4_outbound_policies, policy_index);
203 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
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 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
217 vec_sort_with_function
218 (spd->ipv6_inbound_protect_policy_indices,
219 ipsec_spd_entry_sort);
224 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
226 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
227 vec_sort_with_function
228 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
229 ipsec_spd_entry_sort);
234 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
236 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
238 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
239 vec_sort_with_function
240 (spd->ipv4_inbound_protect_policy_indices,
241 ipsec_spd_entry_sort);
246 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
248 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
249 vec_sort_with_function
250 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
251 ipsec_spd_entry_sort);
261 pool_foreach_index(i, spd->policies, ({
262 vp = pool_elt_at_index(spd->policies, i);
263 if (vp->priority != policy->priority)
265 if (vp->is_outbound != policy->is_outbound)
267 if (vp->policy != policy->policy)
269 if (vp->sa_id != policy->sa_id)
271 if (vp->protocol != policy->protocol)
273 if (vp->lport.start != policy->lport.start)
275 if (vp->lport.stop != policy->lport.stop)
277 if (vp->rport.start != policy->rport.start)
279 if (vp->rport.stop != policy->rport.stop)
281 if (vp->is_ipv6 != policy->is_ipv6)
285 if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
287 if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
289 if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
291 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
293 if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
295 if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
297 if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
299 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
301 if (policy->is_outbound)
303 vec_foreach_index(j, spd->ipv6_outbound_policies) {
304 if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
305 vec_del1 (spd->ipv6_outbound_policies, j);
312 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
314 vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
315 if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
316 vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
323 vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
324 if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
325 vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
334 if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
336 if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
338 if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
340 if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
342 if (policy->is_outbound)
344 vec_foreach_index(j, spd->ipv4_outbound_policies) {
345 if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
346 vec_del1 (spd->ipv4_outbound_policies, j);
353 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
355 vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
356 if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
357 vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
364 vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
365 if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
366 vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
372 pool_put (spd->policies, vp);
383 ipsec_is_sa_used (u32 sa_index)
385 ipsec_main_t *im = &ipsec_main;
388 ipsec_tunnel_if_t *t;
391 pool_foreach(spd, im->spds, ({
392 pool_foreach(p, spd->policies, ({
393 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
395 if (p->sa_index == sa_index)
401 pool_foreach(t, im->tunnel_interfaces, ({
402 if (t->input_sa_index == sa_index)
404 if (t->output_sa_index == sa_index)
413 ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
415 ipsec_main_t *im = &ipsec_main;
421 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
423 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
425 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
427 return VNET_API_ERROR_SYSCALL_ERROR_1;
429 if (!is_add) /* delete */
432 sa = pool_elt_at_index (im->sad, sa_index);
433 if (ipsec_is_sa_used (sa_index))
435 clib_warning ("sa_id %u used in policy", sa->id);
436 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
438 hash_unset (im->sa_index_by_sa_id, sa->id);
439 if (im->cb.add_del_sa_sess_cb)
441 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
443 return VNET_API_ERROR_SYSCALL_ERROR_1;
445 pool_put (im->sad, sa);
447 else /* create new SA */
449 pool_get (im->sad, sa);
450 clib_memcpy (sa, new_sa, sizeof (*sa));
451 sa_index = sa - im->sad;
452 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
453 if (im->cb.add_del_sa_sess_cb)
455 err = im->cb.add_del_sa_sess_cb (sa_index, 1);
457 return VNET_API_ERROR_SYSCALL_ERROR_1;
464 ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
466 ipsec_main_t *im = &ipsec_main;
472 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
474 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
477 sa = pool_elt_at_index (im->sad, sa_index);
480 if (0 < sa_update->crypto_key_len)
482 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
483 sa_update->crypto_key_len);
484 sa->crypto_key_len = sa_update->crypto_key_len;
488 if (0 < sa_update->integ_key_len)
490 clib_memcpy (sa->integ_key, sa_update->integ_key,
491 sa_update->integ_key_len);
492 sa->integ_key_len = sa_update->integ_key_len;
495 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
497 if (im->cb.add_del_sa_sess_cb)
499 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
501 return VNET_API_ERROR_SYSCALL_ERROR_1;
509 ipsec_rand_seed (void)
518 seed_data.time = time (NULL);
519 seed_data.pid = getpid ();
520 seed_data.p = (void *) &seed_data;
522 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
525 static clib_error_t *
526 ipsec_check_support (ipsec_sa_t * sa)
528 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
529 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
530 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
531 return clib_error_return (0, "unsupported none integ-alg");
536 static clib_error_t *
537 ipsec_init (vlib_main_t * vm)
540 ipsec_main_t *im = &ipsec_main;
541 vlib_thread_main_t *tm = vlib_get_thread_main ();
546 memset (im, 0, sizeof (im[0]));
548 im->vnet_main = vnet_get_main ();
551 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
552 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
553 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
555 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
556 CLIB_CACHE_LINE_BYTES);
558 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
560 im->error_drop_node_index = node->index;
562 node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
564 im->esp_encrypt_node_index = node->index;
566 node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt");
568 im->esp_decrypt_node_index = node->index;
570 im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
571 im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT;
573 im->cb.check_support_cb = ipsec_check_support;
575 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
578 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
583 if ((error = ikev2_init (vm)))
589 VLIB_INIT_FUNCTION (ipsec_init);
592 * fd.io coding-style-patch-verification: ON
595 * eval: (c-set-style "gnu")