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>
27 #include <vnet/devices/dpdk/ipsec/esp.h>
28 #define ESP_NODE "dpdk-esp-encrypt"
30 #include <vnet/ipsec/esp.h>
31 #define ESP_NODE "esp-encrypt"
37 add_del_sa_sess (u32 sa_index, u8 is_add)
44 ipsec_get_sa_index_by_sa_id (u32 sa_id)
46 ipsec_main_t *im = &ipsec_main;
47 uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
55 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
58 ipsec_main_t *im = &ipsec_main;
59 ip4_ipsec_config_t config;
64 p = hash_get (im->spd_index_by_spd_id, spd_id);
66 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
70 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
72 return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
76 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
80 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
83 clib_warning ("sw_if_index %u spd_id %u spd_index %u",
84 sw_if_index, spd_id, spd_index);
86 /* enable IPsec on TX */
87 vnet_feature_enable_disable ("ip4-output", "ipsec-output-ip4", sw_if_index,
89 vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index,
92 /* enable IPsec on RX */
93 vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index,
94 is_add, &config, sizeof (config));
95 vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index,
96 is_add, &config, sizeof (config));
102 ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
104 ipsec_main_t *im = &ipsec_main;
105 ipsec_spd_t *spd = 0;
109 p = hash_get (im->spd_index_by_spd_id, spd_id);
111 return VNET_API_ERROR_INVALID_VALUE;
113 return VNET_API_ERROR_INVALID_VALUE;
115 if (!is_add) /* delete */
118 spd = pool_elt_at_index (im->spds, spd_index);
120 return VNET_API_ERROR_INVALID_VALUE;
122 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
124 ipsec_set_interface_spd(vm, k, spd_id, 0);
127 hash_unset (im->spd_index_by_spd_id, spd_id);
128 pool_free (spd->policies);
129 vec_free (spd->ipv4_outbound_policies);
130 vec_free (spd->ipv6_outbound_policies);
131 vec_free (spd->ipv4_inbound_protect_policy_indices);
132 vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices);
133 pool_put (im->spds, spd);
135 else /* create new SPD */
137 pool_get (im->spds, spd);
138 memset (spd, 0, sizeof (*spd));
139 spd_index = spd - im->spds;
141 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
147 ipsec_spd_entry_sort (void *a1, void *a2)
149 ipsec_main_t *im = &ipsec_main;
153 ipsec_policy_t *p1, *p2;
156 pool_foreach (spd, im->spds, ({
157 p1 = pool_elt_at_index(spd->policies, *id1);
158 p2 = pool_elt_at_index(spd->policies, *id2);
160 return p2->priority - p1->priority;
168 ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
170 ipsec_main_t *im = &ipsec_main;
171 ipsec_spd_t *spd = 0;
176 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
177 policy->priority, policy->is_outbound);
179 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
181 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
183 return VNET_API_ERROR_SYSCALL_ERROR_1;
184 policy->sa_index = p[0];
187 p = hash_get (im->spd_index_by_spd_id, policy->id);
190 return VNET_API_ERROR_SYSCALL_ERROR_1;
193 spd = pool_elt_at_index (im->spds, spd_index);
195 return VNET_API_ERROR_SYSCALL_ERROR_1;
201 pool_get (spd->policies, vp);
202 clib_memcpy (vp, policy, sizeof (*vp));
203 policy_index = vp - spd->policies;
205 if (policy->is_outbound)
209 vec_add1 (spd->ipv6_outbound_policies, policy_index);
210 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
211 vec_sort_with_function (spd->ipv6_outbound_policies,
212 ipsec_spd_entry_sort);
216 vec_add1 (spd->ipv4_outbound_policies, policy_index);
217 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
218 vec_sort_with_function (spd->ipv4_outbound_policies,
219 ipsec_spd_entry_sort);
226 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
228 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
230 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
231 vec_sort_with_function
232 (spd->ipv6_inbound_protect_policy_indices,
233 ipsec_spd_entry_sort);
238 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
240 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
241 vec_sort_with_function
242 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
243 ipsec_spd_entry_sort);
248 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
250 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
252 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
253 vec_sort_with_function
254 (spd->ipv4_inbound_protect_policy_indices,
255 ipsec_spd_entry_sort);
260 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
262 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
263 vec_sort_with_function
264 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
265 ipsec_spd_entry_sort);
275 pool_foreach_index(i, spd->policies, ({
276 vp = pool_elt_at_index(spd->policies, i);
277 if (vp->priority != policy->priority)
279 if (vp->is_outbound != policy->is_outbound)
281 if (vp->policy != policy->policy)
283 if (vp->sa_id != policy->sa_id)
285 if (vp->protocol != policy->protocol)
287 if (vp->lport.start != policy->lport.start)
289 if (vp->lport.stop != policy->lport.stop)
291 if (vp->rport.start != policy->rport.start)
293 if (vp->rport.stop != policy->rport.stop)
295 if (vp->is_ipv6 != policy->is_ipv6)
299 if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
301 if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
303 if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
305 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
307 if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
309 if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
311 if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
313 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
315 if (policy->is_outbound)
317 vec_foreach_index(j, spd->ipv6_outbound_policies) {
318 if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
319 vec_del1 (spd->ipv6_outbound_policies, j);
326 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
328 vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
329 if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
330 vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
337 vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
338 if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
339 vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
348 if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
350 if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
352 if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
354 if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
356 if (policy->is_outbound)
358 vec_foreach_index(j, spd->ipv4_outbound_policies) {
359 if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
360 vec_del1 (spd->ipv4_outbound_policies, j);
367 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
369 vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
370 if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
371 vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
378 vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
379 if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
380 vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
386 pool_put (spd->policies, vp);
397 ipsec_is_sa_used (u32 sa_index)
399 ipsec_main_t *im = &ipsec_main;
402 ipsec_tunnel_if_t *t;
405 pool_foreach(spd, im->spds, ({
406 pool_foreach(p, spd->policies, ({
407 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
409 if (p->sa_index == sa_index)
415 pool_foreach(t, im->tunnel_interfaces, ({
416 if (t->input_sa_index == sa_index)
418 if (t->output_sa_index == sa_index)
427 ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
429 ipsec_main_t *im = &ipsec_main;
434 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
436 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
438 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
440 return VNET_API_ERROR_SYSCALL_ERROR_1;
442 if (!is_add) /* delete */
445 sa = pool_elt_at_index (im->sad, sa_index);
446 if (ipsec_is_sa_used (sa_index))
448 clib_warning ("sa_id %u used in policy", sa->id);
449 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
451 hash_unset (im->sa_index_by_sa_id, sa->id);
452 add_del_sa_sess (sa_index, is_add);
453 pool_put (im->sad, sa);
455 else /* create new SA */
457 pool_get (im->sad, sa);
458 clib_memcpy (sa, new_sa, sizeof (*sa));
459 sa_index = sa - im->sad;
460 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
461 if (add_del_sa_sess (sa_index, is_add) < 0)
462 return VNET_API_ERROR_SYSCALL_ERROR_1;
468 ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
470 ipsec_main_t *im = &ipsec_main;
475 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
477 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
480 sa = pool_elt_at_index (im->sad, sa_index);
483 if (0 < sa_update->crypto_key_len)
485 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
486 sa_update->crypto_key_len);
487 sa->crypto_key_len = sa_update->crypto_key_len;
491 if (0 < sa_update->integ_key_len)
493 clib_memcpy (sa->integ_key, sa_update->integ_key,
494 sa_update->integ_key_len);
495 sa->integ_key_len = sa_update->integ_key_len;
498 if (sa->crypto_key_len + sa->integ_key_len > 0)
500 if (add_del_sa_sess (sa_index, 0) < 0)
501 return VNET_API_ERROR_SYSCALL_ERROR_1;
508 ipsec_rand_seed (void)
517 seed_data.time = time (NULL);
518 seed_data.pid = getpid ();
519 seed_data.p = (void *) &seed_data;
521 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
524 static clib_error_t *
525 ipsec_init (vlib_main_t * vm)
528 ipsec_main_t *im = &ipsec_main;
529 vlib_thread_main_t *tm = vlib_get_thread_main ();
534 memset (im, 0, sizeof (im[0]));
536 im->vnet_main = vnet_get_main ();
539 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
540 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
541 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
543 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
544 CLIB_CACHE_LINE_BYTES);
546 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
548 im->error_drop_node_index = node->index;
550 node = vlib_get_node_by_name (vm, (u8 *) ESP_NODE);
553 im->esp_encrypt_node_index = node->index;
555 node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
557 im->ip4_lookup_node_index = node->index;
559 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
562 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
567 if ((error = ikev2_init (vm)))
573 VLIB_INIT_FUNCTION (ipsec_init);
576 * fd.io coding-style-patch-verification: ON
579 * eval: (c-set-style "gnu")