blob: 6e66d10660bc980eb015b2709a45fc5430e59ba0 [file] [log] [blame]
Neale Rannsdd4ccf22020-06-30 07:47:14 +00001/*
2 * ipsec_itf.c: IPSec dedicated interface type
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
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>
Neale Rannse4031132020-10-26 13:00:06 +000023#include <vnet/ethernet/mac_address.h>
Neale Ranns49378f22022-01-10 10:38:43 +000024#include <vnet/mpls/mpls.h>
Neale Rannsdd4ccf22020-06-30 07:47:14 +000025
26/* bitmap of Allocated IPSEC_ITF instances */
27static uword *ipsec_itf_instances;
28
29/* pool of interfaces */
30static ipsec_itf_t *ipsec_itf_pool;
31
32static u32 *ipsec_itf_index_by_sw_if_index;
33
Neale Ranns6ba4e412020-10-19 09:59:41 +000034ipsec_itf_t *
35ipsec_itf_get (index_t ii)
36{
37 return (pool_elt_at_index (ipsec_itf_pool, ii));
38}
39
Neale Ranns6fdcc3d2021-10-08 07:30:47 +000040u32
41ipsec_itf_count (void)
42{
43 return (pool_elts (ipsec_itf_pool));
44}
45
Neale Rannsdd4ccf22020-06-30 07:47:14 +000046static ipsec_itf_t *
47ipsec_itf_find_by_sw_if_index (u32 sw_if_index)
48{
49 if (vec_len (ipsec_itf_index_by_sw_if_index) <= sw_if_index)
50 return NULL;
51 u32 ti = ipsec_itf_index_by_sw_if_index[sw_if_index];
52 if (ti == ~0)
53 return NULL;
54 return pool_elt_at_index (ipsec_itf_pool, ti);
55}
56
57static u8 *
58format_ipsec_itf_name (u8 * s, va_list * args)
59{
60 u32 dev_instance = va_arg (*args, u32);
61 return format (s, "ipsec%d", dev_instance);
62}
63
64void
65ipsec_itf_adj_unstack (adj_index_t ai)
66{
67 adj_midchain_delegate_unstack (ai);
68}
69
70void
71ipsec_itf_adj_stack (adj_index_t ai, u32 sai)
72{
73 const vnet_hw_interface_t *hw;
74
75 hw = vnet_get_sup_hw_interface (vnet_get_main (), adj_get_sw_if_index (ai));
76
77 if (hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
78 {
79 const ipsec_sa_t *sa;
Neale Ranns9ec846c2021-02-09 14:04:02 +000080 fib_prefix_t dst;
Neale Rannsdd4ccf22020-06-30 07:47:14 +000081
82 sa = ipsec_sa_get (sai);
Neale Ranns9ec846c2021-02-09 14:04:02 +000083 ip_address_to_fib_prefix (&sa->tunnel.t_dst, &dst);
84 adj_midchain_delegate_stack (ai, sa->tunnel.t_fib_index, &dst);
Neale Rannsdd4ccf22020-06-30 07:47:14 +000085 }
86 else
87 adj_midchain_delegate_unstack (ai);
88}
89
90static adj_walk_rc_t
91ipsec_itf_adj_stack_cb (adj_index_t ai, void *arg)
92{
93 ipsec_tun_protect_t *itp = arg;
94
95 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
96
97 return (ADJ_WALK_RC_CONTINUE);
98}
99
100static void
101ipsec_itf_restack (index_t itpi, const ipsec_itf_t * itf)
102{
103 ipsec_tun_protect_t *itp;
104 fib_protocol_t proto;
105
106 itp = ipsec_tun_protect_get (itpi);
107
108 /*
109 * walk all the adjacencies on the interface and restack them
110 */
111 FOR_EACH_FIB_IP_PROTOCOL (proto)
112 {
113 adj_nbr_walk (itf->ii_sw_if_index, proto, ipsec_itf_adj_stack_cb, itp);
114 }
115}
116
117static walk_rc_t
118ipsec_tun_protect_walk_state_change (index_t itpi, void *arg)
119{
120 const ipsec_itf_t *itf = arg;
121
122 ipsec_itf_restack (itpi, itf);
123
124 return (WALK_CONTINUE);
125}
126
127static clib_error_t *
128ipsec_itf_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
129{
130 vnet_hw_interface_t *hi;
131 ipsec_itf_t *itf;
132 u32 hw_flags;
133
134 hi = vnet_get_hw_interface (vnm, hw_if_index);
135 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
136 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
137 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
138
139 itf = ipsec_itf_find_by_sw_if_index (hi->sw_if_index);
140
141 if (itf)
142 ipsec_tun_protect_walk_itf (itf->ii_sw_if_index,
143 ipsec_tun_protect_walk_state_change, itf);
144
145 return (NULL);
146}
147
148static int
149ipsec_itf_tunnel_desc (u32 sw_if_index,
150 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
151{
152 ip46_address_reset (src);
153 ip46_address_reset (dst);
154 *is_l2 = 0;
155
156 return (0);
157}
158
159static u8 *
160ipsec_itf_build_rewrite (void)
161{
162 /*
163 * passing the adj code a NULL rewrite means 'i don't have one cos
164 * t'other end is unresolved'. That's not the case here. For the ipsec
165 * tunnel there are just no bytes of encap to apply in the adj.
166 * So return a zero length rewrite. Encap will be added by a tunnel mode SA.
167 */
168 u8 *rewrite = NULL;
169
170 vec_validate (rewrite, 0);
171 vec_reset_length (rewrite);
172
173 return (rewrite);
174}
175
176static u8 *
177ipsec_itf_build_rewrite_i (vnet_main_t * vnm,
178 u32 sw_if_index,
179 vnet_link_t link_type, const void *dst_address)
180{
181 return (ipsec_itf_build_rewrite ());
182}
183
184void
185ipsec_itf_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
186{
187 adj_nbr_midchain_update_rewrite
188 (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK, ipsec_itf_build_rewrite ());
189}
190
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000191VNET_DEVICE_CLASS (ipsec_itf_device_class) = {
192 .name = "IPSEC Tunnel",
193 .format_device_name = format_ipsec_itf_name,
194 .admin_up_down_function = ipsec_itf_admin_up_down,
195 .ip_tun_desc = ipsec_itf_tunnel_desc,
196};
197
198VNET_HW_INTERFACE_CLASS(ipsec_hw_interface_class) = {
199 .name = "IPSec",
200 .build_rewrite = ipsec_itf_build_rewrite_i,
201 .update_adjacency = ipsec_itf_update_adj,
202 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
203};
Neale Ranns6ba4e412020-10-19 09:59:41 +0000204VNET_HW_INTERFACE_CLASS(ipsec_p2mp_hw_interface_class) = {
205 .name = "IPSec",
206 .build_rewrite = ipsec_itf_build_rewrite_i,
207 .update_adjacency = ipsec_itf_update_adj,
Neale Rannscfe949d2020-11-25 19:35:38 +0000208 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
Neale Ranns6ba4e412020-10-19 09:59:41 +0000209};
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000210
211/*
212 * Maintain a bitmap of allocated ipsec_itf instance numbers.
213 */
214#define IPSEC_ITF_MAX_INSTANCE (16 * 1024)
215
216static u32
217ipsec_itf_instance_alloc (u32 want)
218{
219 /*
220 * Check for dynamically allocated instance number.
221 */
222 if (~0 == want)
223 {
224 u32 bit;
225
226 bit = clib_bitmap_first_clear (ipsec_itf_instances);
227 if (bit >= IPSEC_ITF_MAX_INSTANCE)
228 {
229 return ~0;
230 }
231 ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, bit, 1);
232 return bit;
233 }
234
235 /*
236 * In range?
237 */
238 if (want >= IPSEC_ITF_MAX_INSTANCE)
239 {
240 return ~0;
241 }
242
243 /*
244 * Already in use?
245 */
246 if (clib_bitmap_get (ipsec_itf_instances, want))
247 {
248 return ~0;
249 }
250
251 /*
252 * Grant allocation request.
253 */
254 ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, want, 1);
255
256 return want;
257}
258
259static int
260ipsec_itf_instance_free (u32 instance)
261{
262 if (instance >= IPSEC_ITF_MAX_INSTANCE)
263 {
264 return -1;
265 }
266
267 if (clib_bitmap_get (ipsec_itf_instances, instance) == 0)
268 {
269 return -1;
270 }
271
272 ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, instance, 0);
273 return 0;
274}
275
Neale Ranns49378f22022-01-10 10:38:43 +0000276void
277ipsec_itf_reset_tx_nodes (u32 sw_if_index)
278{
279 vnet_feature_modify_end_node (
280 ip4_main.lookup_main.output_feature_arc_index, sw_if_index,
281 vlib_get_node_by_name (vlib_get_main (), (u8 *) "ip4-drop")->index);
282 vnet_feature_modify_end_node (
283 ip6_main.lookup_main.output_feature_arc_index, sw_if_index,
284 vlib_get_node_by_name (vlib_get_main (), (u8 *) "ip6-drop")->index);
285 vnet_feature_modify_end_node (
286 mpls_main.output_feature_arc_index, sw_if_index,
287 vlib_get_node_by_name (vlib_get_main (), (u8 *) "mpls-drop")->index);
288}
289
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000290int
291ipsec_itf_create (u32 user_instance, tunnel_mode_t mode, u32 * sw_if_indexp)
292{
293 vnet_main_t *vnm = vnet_get_main ();
294 u32 instance, hw_if_index;
295 vnet_hw_interface_t *hi;
296 ipsec_itf_t *ipsec_itf;
297
298 ASSERT (sw_if_indexp);
299
300 *sw_if_indexp = (u32) ~ 0;
301
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000302 /*
303 * Allocate a ipsec_itf instance. Either select on dynamically
304 * or try to use the desired user_instance number.
305 */
306 instance = ipsec_itf_instance_alloc (user_instance);
307 if (instance == ~0)
308 return VNET_API_ERROR_INVALID_REGISTRATION;
309
310 pool_get (ipsec_itf_pool, ipsec_itf);
311
312 /* tunnel index (or instance) */
313 u32 t_idx = ipsec_itf - ipsec_itf_pool;
314
315 ipsec_itf->ii_mode = mode;
316 ipsec_itf->ii_user_instance = instance;
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000317
318 hw_if_index = vnet_register_interface (vnm,
319 ipsec_itf_device_class.index,
Eric Kinzie609d5792020-10-13 20:02:11 -0400320 ipsec_itf->ii_user_instance,
Neale Ranns6ba4e412020-10-19 09:59:41 +0000321 (mode == TUNNEL_MODE_P2P ?
322 ipsec_hw_interface_class.index :
323 ipsec_p2mp_hw_interface_class.index),
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000324 t_idx);
325
326 hi = vnet_get_hw_interface (vnm, hw_if_index);
Neale Ranns5606c822021-05-20 16:03:59 +0000327 vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, 9000);
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000328
329 vec_validate_init_empty (ipsec_itf_index_by_sw_if_index, hi->sw_if_index,
330 INDEX_INVALID);
331 ipsec_itf_index_by_sw_if_index[hi->sw_if_index] = t_idx;
332
333 ipsec_itf->ii_sw_if_index = *sw_if_indexp = hi->sw_if_index;
Neale Ranns49378f22022-01-10 10:38:43 +0000334 ipsec_itf_reset_tx_nodes (hi->sw_if_index);
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000335
336 return 0;
337}
338
339int
340ipsec_itf_delete (u32 sw_if_index)
341{
342 vnet_main_t *vnm = vnet_get_main ();
343
344 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
345 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
346
347 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
348 if (hw == 0 || hw->dev_class_index != ipsec_itf_device_class.index)
349 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
350
351 ipsec_itf_t *ipsec_itf;
352 ipsec_itf = ipsec_itf_find_by_sw_if_index (sw_if_index);
353 if (NULL == ipsec_itf)
354 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
355
356 if (ipsec_itf_instance_free (hw->dev_instance) < 0)
357 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
358
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000359 vnet_reset_interface_l3_output_node (vnm->vlib_main, sw_if_index);
360
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000361 vnet_delete_hw_interface (vnm, hw->hw_if_index);
362 pool_put (ipsec_itf_pool, ipsec_itf);
363
364 return 0;
365}
366
Neale Ranns89d939e2021-06-07 09:34:07 +0000367void
368ipsec_itf_walk (ipsec_itf_walk_cb_t cb, void *ctx)
369{
370 ipsec_itf_t *itf;
371
372 pool_foreach (itf, ipsec_itf_pool)
373 {
374 if (WALK_CONTINUE != cb (itf, ctx))
375 break;
376 }
377}
378
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000379static clib_error_t *
380ipsec_itf_create_cli (vlib_main_t * vm,
381 unformat_input_t * input, vlib_cli_command_t * cmd)
382{
383 unformat_input_t _line_input, *line_input = &_line_input;
384 u32 instance, sw_if_index;
385 clib_error_t *error;
386 mac_address_t mac;
387 int rv;
388
389 error = NULL;
390 instance = sw_if_index = ~0;
391 mac_address_set_zero (&mac);
392
393 if (unformat_user (input, unformat_line_input, line_input))
394 {
395 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
396 {
397 if (unformat (line_input, "instance %d", &instance))
398 ;
399 else
400 {
401 error = clib_error_return (0, "unknown input: %U",
402 format_unformat_error, line_input);
403 break;
404 }
405 }
406
407 unformat_free (line_input);
408
409 if (error)
410 return error;
411 }
412
413 rv = ipsec_itf_create (instance, TUNNEL_MODE_P2P, &sw_if_index);
414
415 if (rv)
416 return clib_error_return (0, "iPSec interface create failed");
417
418 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
419 sw_if_index);
420 return 0;
421}
422
423/*?
424 * Create a IPSec interface.
425 *
426 * @cliexpar
427 * The following two command syntaxes are equivalent:
428 * @cliexcmd{ipsec itf create [instance <instance>]}
429 * Example of how to create a ipsec interface:
430 * @cliexcmd{ipsec itf create}
431?*/
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000432VLIB_CLI_COMMAND (ipsec_itf_create_command, static) = {
433 .path = "ipsec itf create",
434 .short_help = "ipsec itf create [instance <instance>]",
435 .function = ipsec_itf_create_cli,
436};
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000437
438static clib_error_t *
439ipsec_itf_delete_cli (vlib_main_t * vm,
440 unformat_input_t * input, vlib_cli_command_t * cmd)
441{
442 vnet_main_t *vnm;
443 u32 sw_if_index;
444 int rv;
445
446 vnm = vnet_get_main ();
447 sw_if_index = ~0;
448
449 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
450 {
451 if (unformat
452 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
453 ;
454 else
455 break;
456 }
457
458 if (~0 != sw_if_index)
459 {
460 rv = ipsec_itf_delete (sw_if_index);
461
462 if (rv)
463 return clib_error_return (0, "ipsec interface delete failed");
464 }
465 else
466 return clib_error_return (0, "no such interface: %U",
467 format_unformat_error, input);
468
469 return 0;
470}
471
472/*?
473 * Delete a IPSEC_ITF interface.
474 *
475 * @cliexpar
476 * The following two command syntaxes are equivalent:
477 * @cliexcmd{ipsec itf delete <interface>}
478 * Example of how to create a ipsec_itf interface:
479 * @cliexcmd{ipsec itf delete ipsec0}
480?*/
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000481VLIB_CLI_COMMAND (ipsec_itf_delete_command, static) = {
482 .path = "ipsec itf delete",
483 .short_help = "ipsec itf delete <interface>",
484 .function = ipsec_itf_delete_cli,
485};
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000486
Neale Ranns6ba4e412020-10-19 09:59:41 +0000487static clib_error_t *
488ipsec_interface_show (vlib_main_t * vm,
489 unformat_input_t * input, vlib_cli_command_t * cmd)
490{
491 index_t ii;
492
Damjan Marionb2c31b62020-12-13 21:47:40 +0100493 pool_foreach_index (ii, ipsec_itf_pool)
494 {
Neale Ranns6ba4e412020-10-19 09:59:41 +0000495 vlib_cli_output (vm, "%U", format_ipsec_itf, ii);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100496 }
Neale Ranns6ba4e412020-10-19 09:59:41 +0000497
498 return NULL;
499}
500
501/**
502 * show IPSEC tunnel protection hash tables
503 */
Neale Ranns6ba4e412020-10-19 09:59:41 +0000504VLIB_CLI_COMMAND (ipsec_interface_show_node, static) =
505{
506 .path = "show ipsec interface",
507 .function = ipsec_interface_show,
508 .short_help = "show ipsec interface",
509};
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000510
511/*
512 * fd.io coding-style-patch-verification: ON
513 *
514 * Local Variables:
515 * eval: (c-set-style "gnu")
516 * End:
517 */