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", "ipsec-output-ip4", sw_if_index,
78 vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index,
81 /* enable IPsec on RX */
82 vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index,
83 is_add, &config, sizeof (config));
84 vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index,
85 is_add, &config, sizeof (config));
91 ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
93 ipsec_main_t *im = &ipsec_main;
98 p = hash_get (im->spd_index_by_spd_id, spd_id);
100 return VNET_API_ERROR_INVALID_VALUE;
102 return VNET_API_ERROR_INVALID_VALUE;
104 if (!is_add) /* delete */
107 spd = pool_elt_at_index (im->spds, spd_index);
109 return VNET_API_ERROR_INVALID_VALUE;
111 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
113 ipsec_set_interface_spd(vm, k, spd_id, 0);
116 hash_unset (im->spd_index_by_spd_id, spd_id);
117 pool_free (spd->policies);
118 vec_free (spd->ipv4_outbound_policies);
119 vec_free (spd->ipv6_outbound_policies);
120 vec_free (spd->ipv4_inbound_protect_policy_indices);
121 vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices);
122 pool_put (im->spds, spd);
124 else /* create new SPD */
126 pool_get (im->spds, spd);
127 memset (spd, 0, sizeof (*spd));
128 spd_index = spd - im->spds;
130 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
136 ipsec_spd_entry_sort (void *a1, void *a2)
138 ipsec_main_t *im = &ipsec_main;
142 ipsec_policy_t *p1, *p2;
145 pool_foreach (spd, im->spds, ({
146 p1 = pool_elt_at_index(spd->policies, *id1);
147 p2 = pool_elt_at_index(spd->policies, *id2);
149 return p2->priority - p1->priority;
157 ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
159 ipsec_main_t *im = &ipsec_main;
160 ipsec_spd_t *spd = 0;
165 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
166 policy->priority, policy->is_outbound);
168 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
170 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
172 return VNET_API_ERROR_SYSCALL_ERROR_1;
173 policy->sa_index = p[0];
176 p = hash_get (im->spd_index_by_spd_id, policy->id);
179 return VNET_API_ERROR_SYSCALL_ERROR_1;
182 spd = pool_elt_at_index (im->spds, spd_index);
184 return VNET_API_ERROR_SYSCALL_ERROR_1;
190 pool_get (spd->policies, vp);
191 clib_memcpy (vp, policy, sizeof (*vp));
192 policy_index = vp - spd->policies;
194 if (policy->is_outbound)
198 vec_add1 (spd->ipv6_outbound_policies, policy_index);
199 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
200 vec_sort_with_function (spd->ipv6_outbound_policies,
201 ipsec_spd_entry_sort);
205 vec_add1 (spd->ipv4_outbound_policies, policy_index);
206 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
207 vec_sort_with_function (spd->ipv4_outbound_policies,
208 ipsec_spd_entry_sort);
215 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
217 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
219 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
220 vec_sort_with_function
221 (spd->ipv6_inbound_protect_policy_indices,
222 ipsec_spd_entry_sort);
227 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
229 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
230 vec_sort_with_function
231 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
232 ipsec_spd_entry_sort);
237 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
239 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
241 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
242 vec_sort_with_function
243 (spd->ipv4_inbound_protect_policy_indices,
244 ipsec_spd_entry_sort);
249 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
251 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
252 vec_sort_with_function
253 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
254 ipsec_spd_entry_sort);
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_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add,
419 ipsec_main_t *im = &ipsec_main;
425 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
427 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
429 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
431 return VNET_API_ERROR_SYSCALL_ERROR_1;
433 if (!is_add) /* delete */
436 sa = pool_elt_at_index (im->sad, sa_index);
437 if (ipsec_is_sa_used (sa_index))
439 clib_warning ("sa_id %u used in policy", sa->id);
440 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
442 hash_unset (im->sa_index_by_sa_id, sa->id);
443 if (im->cb.add_del_sa_sess_cb)
445 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
447 return VNET_API_ERROR_SYSCALL_ERROR_1;
449 pool_put (im->sad, sa);
451 else /* create new SA */
453 pool_get (im->sad, sa);
454 clib_memcpy (sa, new_sa, sizeof (*sa));
455 sa_index = sa - im->sad;
456 sa->udp_encap = udp_encap ? 1 : 0;
457 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
458 if (im->cb.add_del_sa_sess_cb)
460 err = im->cb.add_del_sa_sess_cb (sa_index, 1);
462 return VNET_API_ERROR_SYSCALL_ERROR_1;
469 ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
471 ipsec_main_t *im = &ipsec_main;
477 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
479 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
482 sa = pool_elt_at_index (im->sad, sa_index);
485 if (0 < sa_update->crypto_key_len)
487 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
488 sa_update->crypto_key_len);
489 sa->crypto_key_len = sa_update->crypto_key_len;
493 if (0 < sa_update->integ_key_len)
495 clib_memcpy (sa->integ_key, sa_update->integ_key,
496 sa_update->integ_key_len);
497 sa->integ_key_len = sa_update->integ_key_len;
500 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
502 if (im->cb.add_del_sa_sess_cb)
504 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
506 return VNET_API_ERROR_SYSCALL_ERROR_1;
514 ipsec_rand_seed (void)
523 seed_data.time = time (NULL);
524 seed_data.pid = getpid ();
525 seed_data.p = (void *) &seed_data;
527 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
530 static clib_error_t *
531 ipsec_check_support (ipsec_sa_t * sa)
533 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
534 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
535 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
536 return clib_error_return (0, "unsupported none integ-alg");
541 static clib_error_t *
542 ipsec_init (vlib_main_t * vm)
545 ipsec_main_t *im = &ipsec_main;
546 vlib_thread_main_t *tm = vlib_get_thread_main ();
551 memset (im, 0, sizeof (im[0]));
553 im->vnet_main = vnet_get_main ();
556 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
557 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
558 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
560 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
561 CLIB_CACHE_LINE_BYTES);
563 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
565 im->error_drop_node_index = node->index;
567 node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
569 im->esp_encrypt_node_index = node->index;
571 node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt");
573 im->esp_decrypt_node_index = node->index;
575 node = vlib_get_node_by_name (vm, (u8 *) "ah-encrypt");
577 im->ah_encrypt_node_index = node->index;
579 node = vlib_get_node_by_name (vm, (u8 *) "ah-decrypt");
581 im->ah_decrypt_node_index = node->index;
583 im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
584 im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT;
585 im->ah_encrypt_next_index = IPSEC_OUTPUT_NEXT_AH_ENCRYPT;
586 im->ah_decrypt_next_index = IPSEC_INPUT_NEXT_AH_DECRYPT;
588 im->cb.check_support_cb = ipsec_check_support;
590 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
593 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
598 if ((error = ikev2_init (vm)))
604 VLIB_INIT_FUNCTION (ipsec_init);
607 * fd.io coding-style-patch-verification: ON
610 * eval: (c-set-style "gnu")