2 * ipsec_if.c : IPSec interface 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/fib/fib.h>
22 #include <vnet/udp/udp.h>
23 #include <vnet/adj/adj_midchain.h>
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/esp.h>
28 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
31 format_ipsec_name (u8 * s, va_list * args)
33 u32 dev_instance = va_arg (*args, u32);
34 ipsec_main_t *im = &ipsec_main;
35 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
37 return format (s, "ipsec%d", t->show_instance);
40 /* Statistics (not really errors) */
41 #define foreach_ipsec_if_tx_error \
42 _(TX, "good packets transmitted")
45 ipsec_if_tunnel_stack (adj_index_t ai)
47 ipsec_main_t *ipm = &ipsec_main;
48 ipsec_tunnel_if_t *it;
53 sw_if_index = adj->rewrite_header.sw_if_index;
55 if ((vec_len (ipm->ipsec_if_by_sw_if_index) <= sw_if_index) ||
56 (~0 == ipm->ipsec_if_by_sw_if_index[sw_if_index]))
59 it = pool_elt_at_index (ipm->tunnel_interfaces,
60 ipm->ipsec_if_by_sw_if_index[sw_if_index]);
62 if (!vnet_hw_interface_is_link_up (vnet_get_main (), it->hw_if_index))
64 adj_midchain_delegate_unstack (ai);
70 sa = ipsec_sa_get (it->output_sa_index);
74 .fp_addr = sa->tunnel_dst_addr,
75 .fp_len = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ? 128 : 32),
76 .fp_proto = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ?
82 adj_midchain_delegate_stack (ai, sa->tx_fib_index, &pfx);
87 * @brief Call back when restacking all adjacencies on a GRE interface
90 ipsec_if_adj_walk_cb (adj_index_t ai, void *ctx)
92 ipsec_if_tunnel_stack (ai);
94 return (ADJ_WALK_RC_CONTINUE);
98 ipsec_if_tunnel_restack (ipsec_tunnel_if_t * it)
100 fib_protocol_t proto;
103 * walk all the adjacencies on th GRE interface and restack them
105 FOR_EACH_FIB_IP_PROTOCOL (proto)
107 adj_nbr_walk (it->sw_if_index, proto, ipsec_if_adj_walk_cb, NULL);
111 static clib_error_t *
112 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
114 ipsec_main_t *im = &ipsec_main;
115 clib_error_t *err = 0;
116 ipsec_tunnel_if_t *t;
117 vnet_hw_interface_t *hi;
120 hi = vnet_get_hw_interface (vnm, hw_if_index);
121 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
124 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
126 sa = pool_elt_at_index (im->sad, t->input_sa_index);
128 err = ipsec_check_support_cb (im, sa);
132 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
136 sa = pool_elt_at_index (im->sad, t->output_sa_index);
138 err = ipsec_check_support_cb (im, sa);
142 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
146 vnet_hw_interface_set_flags (vnm, hw_if_index,
147 VNET_HW_INTERFACE_FLAG_LINK_UP);
151 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
152 sa = pool_elt_at_index (im->sad, t->input_sa_index);
153 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
156 sa = pool_elt_at_index (im->sad, t->output_sa_index);
157 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
162 ipsec_if_tunnel_restack (t);
168 ipsec_if_build_rewrite (vnet_main_t * vnm,
170 vnet_link_t link_type, const void *dst_address)
176 ipsec_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
178 adj_nbr_midchain_update_rewrite
179 (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK,
180 ipsec_if_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
182 ipsec_if_tunnel_stack (ai);
186 VNET_DEVICE_CLASS (ipsec_device_class) =
189 .format_device_name = format_ipsec_name,
190 .admin_up_down_function = ipsec_admin_up_down_function,
195 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
198 .build_rewrite = default_build_rewrite,
199 .update_adjacency = ipsec_if_update_adj,
200 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
205 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
207 vnet_main_t *vnm = vnet_get_main ();
208 ASSERT (vlib_get_thread_index () == 0);
210 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
214 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
216 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
217 (u8 *) args, sizeof (*args));
222 ipsec_tun_mk_input_sa_id (u32 ti)
224 return (0x80000000 | ti);
228 ipsec_tun_mk_output_sa_id (u32 ti)
230 return (0xc0000000 | ti);
234 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
235 ipsec_add_del_tunnel_args_t * args,
238 ipsec_tunnel_if_t *t;
239 ipsec_main_t *im = &ipsec_main;
240 vnet_hw_interface_t *hi = NULL;
241 u32 hw_if_index = ~0;
244 ipsec_key_t crypto_key, integ_key;
245 ipsec_sa_flags_t flags;
247 int is_ip6 = args->is_ip6;
248 ipsec4_tunnel_key_t key4;
249 ipsec6_tunnel_key_t key6;
253 key4.remote_ip = args->remote_ip.ip4.as_u32;
254 key4.spi = clib_host_to_net_u32 (args->remote_spi);
255 p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64);
259 key6.remote_ip = args->remote_ip.ip6;
260 key6.spi = clib_host_to_net_u32 (args->remote_spi);
261 p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6);
266 /* check if same src/dst pair exists */
268 return VNET_API_ERROR_INVALID_VALUE;
270 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
272 dev_instance = t - im->tunnel_interfaces;
274 t->show_instance = args->show_instance;
276 t->show_instance = dev_instance;
278 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
280 pool_put (im->tunnel_interfaces, t);
281 return VNET_API_ERROR_INSTANCE_IN_USE;
284 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
287 flags = IPSEC_SA_FLAG_IS_TUNNEL;
289 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
291 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
293 flags |= IPSEC_SA_FLAG_USE_ESN;
294 if (args->anti_replay)
295 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
297 ipsec_mk_key (&crypto_key,
298 args->remote_crypto_key, args->remote_crypto_key_len);
299 ipsec_mk_key (&integ_key,
300 args->remote_integ_key, args->remote_integ_key_len);
302 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
309 (flags | IPSEC_SA_FLAG_IS_INBOUND),
313 &args->local_ip, &t->input_sa_index);
316 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
318 ipsec_mk_key (&crypto_key,
319 args->local_crypto_key, args->local_crypto_key_len);
320 ipsec_mk_key (&integ_key,
321 args->local_integ_key, args->local_integ_key_len);
323 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
334 &args->remote_ip, &t->output_sa_index);
337 return VNET_API_ERROR_INVALID_DST_ADDRESS;
341 hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6,
342 t - im->tunnel_interfaces);
344 hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64,
345 t - im->tunnel_interfaces);
347 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
348 t - im->tunnel_interfaces,
349 ipsec_hw_class.index,
350 t - im->tunnel_interfaces);
352 hi = vnet_get_hw_interface (vnm, hw_if_index);
354 t->hw_if_index = hw_if_index;
355 t->sw_if_index = hi->sw_if_index;
357 /* Standard default jumbo MTU. */
358 vnet_sw_interface_set_mtu (vnm, t->sw_if_index, 9000);
360 /* Add the new tunnel to the DB of tunnels per sw_if_index ... */
361 vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index,
363 im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance;
365 vnet_feature_enable_disable ("ip4-output",
369 sizeof (t->output_sa_index));
370 vnet_feature_enable_disable ("ip6-output",
374 sizeof (t->output_sa_index));
376 /*1st interface, register protocol */
377 if (pool_elts (im->tunnel_interfaces) == 1)
379 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
380 ipsec4_if_input_node.index);
381 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
382 ipsec6_if_input_node.index);
390 /* check if exists */
392 return VNET_API_ERROR_INVALID_VALUE;
395 t = pool_elt_at_index (im->tunnel_interfaces, ti);
396 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
397 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
399 vnet_feature_enable_disable ("ip4-output",
403 sizeof (t->output_sa_index));
404 vnet_feature_enable_disable ("ip6-output",
408 sizeof (t->output_sa_index));
409 vnet_delete_hw_interface (vnm, t->hw_if_index);
412 hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6);
414 hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64);
416 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
417 im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0;
419 pool_put (im->tunnel_interfaces, t);
421 /* delete input and output SA */
422 ipsec_sa_del (ipsec_tun_mk_input_sa_id (ti));
423 ipsec_sa_del (ipsec_tun_mk_output_sa_id (ti));
427 *sw_if_index = hi->sw_if_index;
433 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
434 const ipsec_gre_tunnel_add_del_args_t * args)
436 ipsec_tunnel_if_t *t = 0;
437 ipsec_main_t *im = &ipsec_main;
440 ipsec4_tunnel_key_t key;
443 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
445 return VNET_API_ERROR_INVALID_VALUE;
447 sa = pool_elt_at_index (im->sad, p[0]);
448 ipsec_sa_set_IS_GRE (sa);
450 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
452 return VNET_API_ERROR_INVALID_VALUE;
454 sa = pool_elt_at_index (im->sad, p[0]);
455 ipsec_sa_set_IS_GRE (sa);
457 /* we form the key from the input/remote SA whose tunnel is srouce
458 * at the remote end */
459 if (ipsec_sa_is_set_IS_TUNNEL (sa))
461 key.remote_ip = sa->tunnel_src_addr.ip4.as_u32;
462 key.spi = clib_host_to_net_u32 (sa->spi);
466 key.remote_ip = args->src.as_u32;
467 key.spi = clib_host_to_net_u32 (sa->spi);
470 p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
474 /* check if same src/dst pair exists */
476 return VNET_API_ERROR_INVALID_VALUE;
478 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
479 clib_memset (t, 0, sizeof (*t));
481 t->input_sa_index = isa;
482 t->output_sa_index = osa;
484 hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
485 t - im->tunnel_interfaces);
487 /*1st interface, register protocol */
488 if (pool_elts (im->tunnel_interfaces) == 1)
490 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
491 ipsec4_if_input_node.index);
494 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
495 ipsec6_if_input_node.index);
501 /* check if exists */
503 return VNET_API_ERROR_INVALID_VALUE;
505 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
506 hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
507 pool_put (im->tunnel_interfaces, t);
513 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
514 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
516 ipsec_main_t *im = &ipsec_main;
517 vnet_hw_interface_t *hi;
518 ipsec_tunnel_if_t *t;
521 hi = vnet_get_hw_interface (vnm, hw_if_index);
522 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
524 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
525 return VNET_API_ERROR_SYSCALL_ERROR_1;
527 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
529 sa = pool_elt_at_index (im->sad, t->output_sa_index);
530 ipsec_sa_set_crypto_alg (sa, alg);
531 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
533 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
535 sa = pool_elt_at_index (im->sad, t->output_sa_index);
536 ipsec_sa_set_integ_alg (sa, alg);
537 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
539 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
541 sa = pool_elt_at_index (im->sad, t->input_sa_index);
542 ipsec_sa_set_crypto_alg (sa, alg);
543 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
545 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
547 sa = pool_elt_at_index (im->sad, t->input_sa_index);
548 ipsec_sa_set_integ_alg (sa, alg);
549 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
552 return VNET_API_ERROR_INVALID_VALUE;
559 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
562 ipsec_main_t *im = &ipsec_main;
563 vnet_hw_interface_t *hi;
564 ipsec_tunnel_if_t *t;
565 ipsec_sa_t *sa, *old_sa;
566 u32 sa_index, old_sa_index;
569 hi = vnet_get_hw_interface (vnm, hw_if_index);
570 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
572 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
575 clib_warning ("SA with ID %u not found", sa_id);
576 return VNET_API_ERROR_INVALID_VALUE;
579 if (ipsec_is_sa_used (sa_index))
581 clib_warning ("SA with ID %u is already in use", sa_id);
582 return VNET_API_ERROR_INVALID_VALUE;
585 sa = pool_elt_at_index (im->sad, sa_index);
589 old_sa_index = t->input_sa_index;
590 old_sa = pool_elt_at_index (im->sad, old_sa_index);
592 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
593 ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
595 clib_warning ("IPsec interface SA endpoints type can't be changed");
596 return VNET_API_ERROR_INVALID_VALUE;
599 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
601 ipsec6_tunnel_key_t key;
603 /* unset old inbound hash entry. packets should stop arriving */
604 key.remote_ip = old_sa->tunnel_src_addr.ip6;
605 key.spi = clib_host_to_net_u32 (old_sa->spi);
607 p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key);
609 hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key);
611 /* set new inbound SA, then set new hash entry */
612 t->input_sa_index = sa_index;
613 key.remote_ip = sa->tunnel_src_addr.ip6;
614 key.spi = clib_host_to_net_u32 (sa->spi);
616 hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key,
621 ipsec4_tunnel_key_t key;
623 /* unset old inbound hash entry. packets should stop arriving */
624 key.remote_ip = old_sa->tunnel_src_addr.ip4.as_u32;
625 key.spi = clib_host_to_net_u32 (old_sa->spi);
627 p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
629 hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
631 /* set new inbound SA, then set new hash entry */
632 t->input_sa_index = sa_index;
633 key.remote_ip = sa->tunnel_src_addr.ip4.as_u32;
634 key.spi = clib_host_to_net_u32 (sa->spi);
636 hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
642 old_sa_index = t->output_sa_index;
643 old_sa = pool_elt_at_index (im->sad, old_sa_index);
645 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
646 ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
648 clib_warning ("IPsec interface SA endpoints type can't be changed");
649 return VNET_API_ERROR_INVALID_VALUE;
652 t->output_sa_index = sa_index;
655 /* remove sa_id to sa_index mapping on old SA */
656 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
657 hash_unset (im->sa_index_by_sa_id, old_sa->id);
659 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
661 clib_warning ("IPsec backend add/del callback returned error");
662 return VNET_API_ERROR_SYSCALL_ERROR_1;
664 pool_put (im->sad, old_sa);
671 ipsec_tunnel_if_init (vlib_main_t * vm)
673 ipsec_main_t *im = &ipsec_main;
675 /* initialize the ipsec-if ip4 hash */
676 im->ipsec4_if_pool_index_by_key =
677 hash_create (0, sizeof (ipsec4_tunnel_key_t));
678 /* initialize the ipsec-if ip6 hash */
679 im->ipsec6_if_pool_index_by_key = hash_create_mem (0,
681 (ipsec6_tunnel_key_t),
683 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
685 udp_register_dst_port (vm, UDP_DST_PORT_ipsec, ipsec4_if_input_node.index,
690 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
694 * fd.io coding-style-patch-verification: ON
697 * eval: (c-set-style "gnu")