2 * ipsec_itf.c: IPSec dedicated interface type
4 * Copyright (c) 2020 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/ip/ip.h>
19 #include <vnet/ipsec/ipsec_itf.h>
20 #include <vnet/ipsec/ipsec_tun.h>
21 #include <vnet/ipsec/ipsec.h>
22 #include <vnet/adj/adj_midchain.h>
23 #include <vnet/ethernet/mac_address.h>
25 /* bitmap of Allocated IPSEC_ITF instances */
26 static uword *ipsec_itf_instances;
28 /* pool of interfaces */
29 static ipsec_itf_t *ipsec_itf_pool;
31 static u32 *ipsec_itf_index_by_sw_if_index;
34 ipsec_itf_get (index_t ii)
36 return (pool_elt_at_index (ipsec_itf_pool, ii));
40 ipsec_itf_find_by_sw_if_index (u32 sw_if_index)
42 if (vec_len (ipsec_itf_index_by_sw_if_index) <= sw_if_index)
44 u32 ti = ipsec_itf_index_by_sw_if_index[sw_if_index];
47 return pool_elt_at_index (ipsec_itf_pool, ti);
51 format_ipsec_itf_name (u8 * s, va_list * args)
53 u32 dev_instance = va_arg (*args, u32);
54 return format (s, "ipsec%d", dev_instance);
58 ipsec_itf_adj_unstack (adj_index_t ai)
60 adj_midchain_delegate_unstack (ai);
64 ipsec_itf_adj_stack (adj_index_t ai, u32 sai)
66 const vnet_hw_interface_t *hw;
68 hw = vnet_get_sup_hw_interface (vnet_get_main (), adj_get_sw_if_index (ai));
70 if (hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
75 sa = ipsec_sa_get (sai);
76 ip_address_to_fib_prefix (&sa->tunnel.t_dst, &dst);
77 adj_midchain_delegate_stack (ai, sa->tunnel.t_fib_index, &dst);
80 adj_midchain_delegate_unstack (ai);
84 ipsec_itf_adj_stack_cb (adj_index_t ai, void *arg)
86 ipsec_tun_protect_t *itp = arg;
88 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
90 return (ADJ_WALK_RC_CONTINUE);
94 ipsec_itf_restack (index_t itpi, const ipsec_itf_t * itf)
96 ipsec_tun_protect_t *itp;
99 itp = ipsec_tun_protect_get (itpi);
102 * walk all the adjacencies on the interface and restack them
104 FOR_EACH_FIB_IP_PROTOCOL (proto)
106 adj_nbr_walk (itf->ii_sw_if_index, proto, ipsec_itf_adj_stack_cb, itp);
111 ipsec_tun_protect_walk_state_change (index_t itpi, void *arg)
113 const ipsec_itf_t *itf = arg;
115 ipsec_itf_restack (itpi, itf);
117 return (WALK_CONTINUE);
120 static clib_error_t *
121 ipsec_itf_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
123 vnet_hw_interface_t *hi;
127 hi = vnet_get_hw_interface (vnm, hw_if_index);
128 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
129 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
130 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
132 itf = ipsec_itf_find_by_sw_if_index (hi->sw_if_index);
135 ipsec_tun_protect_walk_itf (itf->ii_sw_if_index,
136 ipsec_tun_protect_walk_state_change, itf);
142 ipsec_itf_tunnel_desc (u32 sw_if_index,
143 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
145 ip46_address_reset (src);
146 ip46_address_reset (dst);
153 ipsec_itf_build_rewrite (void)
156 * passing the adj code a NULL rewrite means 'i don't have one cos
157 * t'other end is unresolved'. That's not the case here. For the ipsec
158 * tunnel there are just no bytes of encap to apply in the adj.
159 * So return a zero length rewrite. Encap will be added by a tunnel mode SA.
163 vec_validate (rewrite, 0);
164 vec_reset_length (rewrite);
170 ipsec_itf_build_rewrite_i (vnet_main_t * vnm,
172 vnet_link_t link_type, const void *dst_address)
174 return (ipsec_itf_build_rewrite ());
178 ipsec_itf_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
180 adj_nbr_midchain_update_rewrite
181 (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK, ipsec_itf_build_rewrite ());
185 VNET_DEVICE_CLASS (ipsec_itf_device_class) = {
186 .name = "IPSEC Tunnel",
187 .format_device_name = format_ipsec_itf_name,
188 .admin_up_down_function = ipsec_itf_admin_up_down,
189 .ip_tun_desc = ipsec_itf_tunnel_desc,
192 VNET_HW_INTERFACE_CLASS(ipsec_hw_interface_class) = {
194 .build_rewrite = ipsec_itf_build_rewrite_i,
195 .update_adjacency = ipsec_itf_update_adj,
196 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
198 VNET_HW_INTERFACE_CLASS(ipsec_p2mp_hw_interface_class) = {
200 .build_rewrite = ipsec_itf_build_rewrite_i,
201 .update_adjacency = ipsec_itf_update_adj,
202 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
207 * Maintain a bitmap of allocated ipsec_itf instance numbers.
209 #define IPSEC_ITF_MAX_INSTANCE (16 * 1024)
212 ipsec_itf_instance_alloc (u32 want)
215 * Check for dynamically allocated instance number.
221 bit = clib_bitmap_first_clear (ipsec_itf_instances);
222 if (bit >= IPSEC_ITF_MAX_INSTANCE)
226 ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, bit, 1);
233 if (want >= IPSEC_ITF_MAX_INSTANCE)
241 if (clib_bitmap_get (ipsec_itf_instances, want))
247 * Grant allocation request.
249 ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, want, 1);
255 ipsec_itf_instance_free (u32 instance)
257 if (instance >= IPSEC_ITF_MAX_INSTANCE)
262 if (clib_bitmap_get (ipsec_itf_instances, instance) == 0)
267 ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, instance, 0);
272 ipsec_itf_create (u32 user_instance, tunnel_mode_t mode, u32 * sw_if_indexp)
274 vnet_main_t *vnm = vnet_get_main ();
275 u32 instance, hw_if_index;
276 vnet_hw_interface_t *hi;
277 ipsec_itf_t *ipsec_itf;
279 ASSERT (sw_if_indexp);
281 *sw_if_indexp = (u32) ~ 0;
284 * Allocate a ipsec_itf instance. Either select on dynamically
285 * or try to use the desired user_instance number.
287 instance = ipsec_itf_instance_alloc (user_instance);
289 return VNET_API_ERROR_INVALID_REGISTRATION;
291 pool_get (ipsec_itf_pool, ipsec_itf);
293 /* tunnel index (or instance) */
294 u32 t_idx = ipsec_itf - ipsec_itf_pool;
296 ipsec_itf->ii_mode = mode;
297 ipsec_itf->ii_user_instance = instance;
299 hw_if_index = vnet_register_interface (vnm,
300 ipsec_itf_device_class.index,
301 ipsec_itf->ii_user_instance,
302 (mode == TUNNEL_MODE_P2P ?
303 ipsec_hw_interface_class.index :
304 ipsec_p2mp_hw_interface_class.index),
307 hi = vnet_get_hw_interface (vnm, hw_if_index);
308 vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, 9000);
310 vec_validate_init_empty (ipsec_itf_index_by_sw_if_index, hi->sw_if_index,
312 ipsec_itf_index_by_sw_if_index[hi->sw_if_index] = t_idx;
314 ipsec_itf->ii_sw_if_index = *sw_if_indexp = hi->sw_if_index;
320 ipsec_itf_delete (u32 sw_if_index)
322 vnet_main_t *vnm = vnet_get_main ();
324 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
325 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
327 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
328 if (hw == 0 || hw->dev_class_index != ipsec_itf_device_class.index)
329 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
331 ipsec_itf_t *ipsec_itf;
332 ipsec_itf = ipsec_itf_find_by_sw_if_index (sw_if_index);
333 if (NULL == ipsec_itf)
334 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
336 if (ipsec_itf_instance_free (hw->dev_instance) < 0)
337 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
339 vnet_delete_hw_interface (vnm, hw->hw_if_index);
340 pool_put (ipsec_itf_pool, ipsec_itf);
346 ipsec_itf_walk (ipsec_itf_walk_cb_t cb, void *ctx)
350 pool_foreach (itf, ipsec_itf_pool)
352 if (WALK_CONTINUE != cb (itf, ctx))
357 static clib_error_t *
358 ipsec_itf_create_cli (vlib_main_t * vm,
359 unformat_input_t * input, vlib_cli_command_t * cmd)
361 unformat_input_t _line_input, *line_input = &_line_input;
362 u32 instance, sw_if_index;
368 instance = sw_if_index = ~0;
369 mac_address_set_zero (&mac);
371 if (unformat_user (input, unformat_line_input, line_input))
373 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
375 if (unformat (line_input, "instance %d", &instance))
379 error = clib_error_return (0, "unknown input: %U",
380 format_unformat_error, line_input);
385 unformat_free (line_input);
391 rv = ipsec_itf_create (instance, TUNNEL_MODE_P2P, &sw_if_index);
394 return clib_error_return (0, "iPSec interface create failed");
396 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
402 * Create a IPSec interface.
405 * The following two command syntaxes are equivalent:
406 * @cliexcmd{ipsec itf create [instance <instance>]}
407 * Example of how to create a ipsec interface:
408 * @cliexcmd{ipsec itf create}
411 VLIB_CLI_COMMAND (ipsec_itf_create_command, static) = {
412 .path = "ipsec itf create",
413 .short_help = "ipsec itf create [instance <instance>]",
414 .function = ipsec_itf_create_cli,
418 static clib_error_t *
419 ipsec_itf_delete_cli (vlib_main_t * vm,
420 unformat_input_t * input, vlib_cli_command_t * cmd)
426 vnm = vnet_get_main ();
429 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
432 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
438 if (~0 != sw_if_index)
440 rv = ipsec_itf_delete (sw_if_index);
443 return clib_error_return (0, "ipsec interface delete failed");
446 return clib_error_return (0, "no such interface: %U",
447 format_unformat_error, input);
453 * Delete a IPSEC_ITF interface.
456 * The following two command syntaxes are equivalent:
457 * @cliexcmd{ipsec itf delete <interface>}
458 * Example of how to create a ipsec_itf interface:
459 * @cliexcmd{ipsec itf delete ipsec0}
462 VLIB_CLI_COMMAND (ipsec_itf_delete_command, static) = {
463 .path = "ipsec itf delete",
464 .short_help = "ipsec itf delete <interface>",
465 .function = ipsec_itf_delete_cli,
469 static clib_error_t *
470 ipsec_interface_show (vlib_main_t * vm,
471 unformat_input_t * input, vlib_cli_command_t * cmd)
476 pool_foreach_index (ii, ipsec_itf_pool)
478 vlib_cli_output (vm, "%U", format_ipsec_itf, ii);
486 * show IPSEC tunnel protection hash tables
489 VLIB_CLI_COMMAND (ipsec_interface_show_node, static) =
491 .path = "show ipsec interface",
492 .function = ipsec_interface_show,
493 .short_help = "show ipsec interface",
498 * fd.io coding-style-patch-verification: ON
501 * eval: (c-set-style "gnu")