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>
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
26 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
29 format_ipsec_name (u8 * s, va_list * args)
31 u32 dev_instance = va_arg (*args, u32);
32 ipsec_main_t *im = &ipsec_main;
33 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
35 return format (s, "ipsec%d", t->show_instance);
38 /* Statistics (not really errors) */
39 #define foreach_ipsec_if_tx_error \
40 _(TX, "good packets transmitted")
42 static char *ipsec_if_tx_error_strings[] = {
43 #define _(sym,string) string,
44 foreach_ipsec_if_tx_error
50 #define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
51 foreach_ipsec_if_tx_error
54 } ipsec_if_tx_error_t;
60 } ipsec_if_tx_trace_t;
63 format_ipsec_if_tx_trace (u8 * s, va_list * args)
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 ipsec_if_tx_trace_t *t = va_arg (*args, ipsec_if_tx_trace_t *);
69 s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
74 ipsec_if_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
75 vlib_frame_t * from_frame)
77 ipsec_main_t *im = &ipsec_main;
78 vnet_main_t *vnm = im->vnet_main;
79 vnet_interface_main_t *vim = &vnm->interface_main;
80 u32 *from, *to_next = 0, next_index;
81 u32 n_left_from, sw_if_index0, last_sw_if_index = ~0;
82 u32 thread_index = vm->thread_index;
83 u32 n_bytes = 0, n_packets = 0;
85 from = vlib_frame_vector_args (from_frame);
86 n_left_from = from_frame->n_vectors;
87 next_index = node->cached_next_index;
89 while (n_left_from > 0)
93 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
95 while (n_left_from > 0 && n_left_to_next > 0)
99 ipsec_tunnel_if_t *t0;
100 vnet_hw_interface_t *hi0;
102 bi0 = to_next[0] = from[0];
107 b0 = vlib_get_buffer (vm, bi0);
108 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
109 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
110 t0 = pool_elt_at_index (im->tunnel_interfaces, hi0->dev_instance);
111 vnet_buffer (b0)->ipsec.sad_index = t0->output_sa_index;
112 next0 = IPSEC_OUTPUT_NEXT_ESP4_ENCRYPT;
114 len0 = vlib_buffer_length_in_chain (vm, b0);
116 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
123 vlib_increment_combined_counter (vim->combined_sw_if_counters +
124 VNET_INTERFACE_COUNTER_TX,
125 thread_index, sw_if_index0,
127 last_sw_if_index = sw_if_index0;
132 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
134 ipsec_if_tx_trace_t *tr =
135 vlib_add_trace (vm, node, b0, sizeof (*tr));
137 pool_elt_at_index (im->sad, t0->output_sa_index);
142 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
143 n_left_to_next, bi0, next0);
145 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
148 if (last_sw_if_index != ~0)
150 vlib_increment_combined_counter (vim->combined_sw_if_counters +
151 VNET_INTERFACE_COUNTER_TX,
153 last_sw_if_index, n_packets, n_bytes);
156 return from_frame->n_vectors;
160 static clib_error_t *
161 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
163 ipsec_main_t *im = &ipsec_main;
164 clib_error_t *err = 0;
165 ipsec_tunnel_if_t *t;
166 vnet_hw_interface_t *hi;
169 hi = vnet_get_hw_interface (vnm, hw_if_index);
170 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
172 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
174 sa = pool_elt_at_index (im->sad, t->input_sa_index);
176 err = ipsec_check_support_cb (im, sa);
180 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
184 sa = pool_elt_at_index (im->sad, t->output_sa_index);
186 err = ipsec_check_support_cb (im, sa);
190 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
194 vnet_hw_interface_set_flags (vnm, hw_if_index,
195 VNET_HW_INTERFACE_FLAG_LINK_UP);
199 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
200 sa = pool_elt_at_index (im->sad, t->input_sa_index);
201 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
204 sa = pool_elt_at_index (im->sad, t->output_sa_index);
205 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
210 return /* no error */ 0;
215 VNET_DEVICE_CLASS (ipsec_device_class, static) =
218 .format_device_name = format_ipsec_name,
219 .format_tx_trace = format_ipsec_if_tx_trace,
220 .tx_function = ipsec_if_tx_node_fn,
221 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
222 .tx_function_error_strings = ipsec_if_tx_error_strings,
223 .admin_up_down_function = ipsec_admin_up_down_function,
228 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
231 .build_rewrite = default_build_rewrite,
232 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
237 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
239 vnet_main_t *vnm = vnet_get_main ();
240 ASSERT (vlib_get_thread_index () == 0);
242 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
246 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
248 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
249 (u8 *) args, sizeof (*args));
254 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
255 ipsec_add_del_tunnel_args_t * args,
258 ipsec_tunnel_if_t *t;
259 ipsec_main_t *im = &ipsec_main;
260 vnet_hw_interface_t *hi = NULL;
261 u32 hw_if_index = ~0;
266 u32 tx_fib_index = ~0;
268 u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
269 p = hash_get (im->ipsec_if_pool_index_by_key, key);
273 /* check if same src/dst pair exists */
275 return VNET_API_ERROR_INVALID_VALUE;
277 tx_fib_index = fib_table_find (FIB_PROTOCOL_IP4, args->tx_table_id);
278 if (tx_fib_index == ~((u32) 0))
279 return VNET_API_ERROR_NO_SUCH_FIB;
281 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
282 clib_memset (t, 0, sizeof (*t));
284 dev_instance = t - im->tunnel_interfaces;
286 t->show_instance = args->show_instance;
288 t->show_instance = dev_instance;
290 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
292 pool_put (im->tunnel_interfaces, t);
293 return VNET_API_ERROR_INSTANCE_IN_USE;
296 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
299 pool_get (im->sad, sa);
300 clib_memset (sa, 0, sizeof (*sa));
301 t->input_sa_index = sa - im->sad;
302 sa->protocol = IPSEC_PROTOCOL_ESP;
303 sa->spi = args->remote_spi;
304 sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
305 sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
307 sa->use_esn = args->esn;
308 sa->use_anti_replay = args->anti_replay;
309 sa->integ_alg = args->integ_alg;
310 sa->udp_encap = args->udp_encap;
311 sa->tx_fib_index = ~((u32) 0); /* Not used, but set for troubleshooting */
312 if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
314 sa->integ_key_len = args->remote_integ_key_len;
315 clib_memcpy (sa->integ_key, args->remote_integ_key,
316 args->remote_integ_key_len);
318 sa->crypto_alg = args->crypto_alg;
319 if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
321 sa->crypto_key_len = args->remote_crypto_key_len;
322 clib_memcpy (sa->crypto_key, args->remote_crypto_key,
323 args->remote_crypto_key_len);
326 pool_get (im->sad, sa);
327 clib_memset (sa, 0, sizeof (*sa));
328 t->output_sa_index = sa - im->sad;
329 sa->protocol = IPSEC_PROTOCOL_ESP;
330 sa->spi = args->local_spi;
331 sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
332 sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
334 sa->use_esn = args->esn;
335 sa->use_anti_replay = args->anti_replay;
336 sa->integ_alg = args->integ_alg;
337 sa->udp_encap = args->udp_encap;
338 sa->tx_fib_index = tx_fib_index;
339 if (args->local_integ_key_len <= sizeof (args->local_integ_key))
341 sa->integ_key_len = args->local_integ_key_len;
342 clib_memcpy (sa->integ_key, args->local_integ_key,
343 args->local_integ_key_len);
345 sa->crypto_alg = args->crypto_alg;
346 if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
348 sa->crypto_key_len = args->local_crypto_key_len;
349 clib_memcpy (sa->crypto_key, args->local_crypto_key,
350 args->local_crypto_key_len);
353 hash_set (im->ipsec_if_pool_index_by_key, key,
354 t - im->tunnel_interfaces);
356 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
357 t - im->tunnel_interfaces,
358 ipsec_hw_class.index,
359 t - im->tunnel_interfaces);
361 hi = vnet_get_hw_interface (vnm, hw_if_index);
363 slot = vlib_node_add_next_with_slot
364 (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index,
365 IPSEC_OUTPUT_NEXT_ESP4_ENCRYPT);
367 ASSERT (slot == IPSEC_OUTPUT_NEXT_ESP4_ENCRYPT);
369 t->hw_if_index = hw_if_index;
371 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
372 hi->sw_if_index, 1, 0, 0);
374 /*1st interface, register protocol */
375 if (pool_elts (im->tunnel_interfaces) == 1)
376 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
377 ipsec_if_input_node.index);
382 /* check if exists */
384 return VNET_API_ERROR_INVALID_VALUE;
386 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
387 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
388 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
390 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
391 hi->sw_if_index, 0, 0, 0);
393 vnet_delete_hw_interface (vnm, t->hw_if_index);
395 /* delete input and output SA */
397 sa = pool_elt_at_index (im->sad, t->input_sa_index);
398 pool_put (im->sad, sa);
400 sa = pool_elt_at_index (im->sad, t->output_sa_index);
401 pool_put (im->sad, sa);
403 hash_unset (im->ipsec_if_pool_index_by_key, key);
404 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
406 pool_put (im->tunnel_interfaces, t);
410 *sw_if_index = hi->sw_if_index;
416 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
417 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
419 ipsec_tunnel_if_t *t = 0;
420 ipsec_main_t *im = &ipsec_main;
426 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
428 return VNET_API_ERROR_INVALID_VALUE;
431 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
433 return VNET_API_ERROR_INVALID_VALUE;
435 sa = pool_elt_at_index (im->sad, p[0]);
438 key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
440 key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
442 p = hash_get (im->ipsec_if_pool_index_by_key, key);
446 /* check if same src/dst pair exists */
448 return VNET_API_ERROR_INVALID_VALUE;
450 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
451 clib_memset (t, 0, sizeof (*t));
453 t->input_sa_index = isa;
454 t->output_sa_index = osa;
456 hash_set (im->ipsec_if_pool_index_by_key, key,
457 t - im->tunnel_interfaces);
459 /*1st interface, register protocol */
460 if (pool_elts (im->tunnel_interfaces) == 1)
461 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
462 ipsec_if_input_node.index);
466 /* check if exists */
468 return VNET_API_ERROR_INVALID_VALUE;
470 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
471 hash_unset (im->ipsec_if_pool_index_by_key, key);
472 pool_put (im->tunnel_interfaces, t);
478 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
479 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
481 ipsec_main_t *im = &ipsec_main;
482 vnet_hw_interface_t *hi;
483 ipsec_tunnel_if_t *t;
486 hi = vnet_get_hw_interface (vnm, hw_if_index);
487 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
489 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
490 return VNET_API_ERROR_SYSCALL_ERROR_1;
492 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
494 sa = pool_elt_at_index (im->sad, t->output_sa_index);
495 sa->crypto_alg = alg;
496 sa->crypto_key_len = vec_len (key);
497 clib_memcpy (sa->crypto_key, key, vec_len (key));
499 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
501 sa = pool_elt_at_index (im->sad, t->output_sa_index);
503 sa->integ_key_len = vec_len (key);
504 clib_memcpy (sa->integ_key, key, vec_len (key));
506 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
508 sa = pool_elt_at_index (im->sad, t->input_sa_index);
509 sa->crypto_alg = alg;
510 sa->crypto_key_len = vec_len (key);
511 clib_memcpy (sa->crypto_key, key, vec_len (key));
513 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
515 sa = pool_elt_at_index (im->sad, t->input_sa_index);
517 sa->integ_key_len = vec_len (key);
518 clib_memcpy (sa->integ_key, key, vec_len (key));
521 return VNET_API_ERROR_INVALID_VALUE;
528 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
531 ipsec_main_t *im = &ipsec_main;
532 vnet_hw_interface_t *hi;
533 ipsec_tunnel_if_t *t;
534 ipsec_sa_t *sa, *old_sa;
535 u32 sa_index, old_sa_index;
538 hi = vnet_get_hw_interface (vnm, hw_if_index);
539 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
541 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
544 clib_warning ("SA with ID %u not found", sa_id);
545 return VNET_API_ERROR_INVALID_VALUE;
548 if (ipsec_is_sa_used (sa_index))
550 clib_warning ("SA with ID %u is already in use", sa_id);
551 return VNET_API_ERROR_INVALID_VALUE;
554 sa = pool_elt_at_index (im->sad, sa_index);
555 if (sa->is_tunnel_ip6)
557 clib_warning ("IPsec interface not supported with IPv6 endpoints");
558 return VNET_API_ERROR_UNIMPLEMENTED;
565 old_sa_index = t->input_sa_index;
566 old_sa = pool_elt_at_index (im->sad, old_sa_index);
568 /* unset old inbound hash entry. packets should stop arriving */
570 (u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) old_sa->spi;
571 p = hash_get (im->ipsec_if_pool_index_by_key, key);
573 hash_unset (im->ipsec_if_pool_index_by_key, key);
575 /* set new inbound SA, then set new hash entry */
576 t->input_sa_index = sa_index;
577 key = (u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) sa->spi;
578 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
582 old_sa_index = t->output_sa_index;
583 old_sa = pool_elt_at_index (im->sad, old_sa_index);
584 t->output_sa_index = sa_index;
587 /* remove sa_id to sa_index mapping on old SA */
588 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
589 hash_unset (im->sa_index_by_sa_id, old_sa->id);
591 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
593 clib_warning ("IPsec backend add/del callback returned error");
594 return VNET_API_ERROR_SYSCALL_ERROR_1;
596 pool_put (im->sad, old_sa);
603 ipsec_tunnel_if_init (vlib_main_t * vm)
605 ipsec_main_t *im = &ipsec_main;
607 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
608 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
613 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
617 * fd.io coding-style-patch-verification: ON
620 * eval: (c-set-style "gnu")