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>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
25 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
28 format_ipsec_name (u8 * s, va_list * args)
30 u32 dev_instance = va_arg (*args, u32);
31 ipsec_main_t *im = &ipsec_main;
32 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
34 return format (s, "ipsec%d", t->show_instance);
37 /* Statistics (not really errors) */
38 #define foreach_ipsec_if_tx_error \
39 _(TX, "good packets transmitted")
41 static char *ipsec_if_tx_error_strings[] = {
42 #define _(sym,string) string,
43 foreach_ipsec_if_tx_error
49 #define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
50 foreach_ipsec_if_tx_error
53 } ipsec_if_tx_error_t;
59 } ipsec_if_tx_trace_t;
62 format_ipsec_if_tx_trace (u8 * s, va_list * args)
64 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66 ipsec_if_tx_trace_t *t = va_arg (*args, ipsec_if_tx_trace_t *);
68 s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
73 ipsec_if_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
74 vlib_frame_t * from_frame)
76 ipsec_main_t *im = &ipsec_main;
77 vnet_main_t *vnm = im->vnet_main;
78 vnet_interface_main_t *vim = &vnm->interface_main;
79 u32 *from, *to_next = 0, next_index;
80 u32 n_left_from, sw_if_index0, last_sw_if_index = ~0;
81 u32 thread_index = vlib_get_thread_index ();
82 u32 n_bytes = 0, n_packets = 0;
84 from = vlib_frame_vector_args (from_frame);
85 n_left_from = from_frame->n_vectors;
86 next_index = node->cached_next_index;
88 while (n_left_from > 0)
92 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
94 while (n_left_from > 0 && n_left_to_next > 0)
98 ipsec_tunnel_if_t *t0;
99 vnet_hw_interface_t *hi0;
101 bi0 = to_next[0] = from[0];
106 b0 = vlib_get_buffer (vm, bi0);
107 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
108 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
109 t0 = pool_elt_at_index (im->tunnel_interfaces, hi0->dev_instance);
110 vnet_buffer (b0)->ipsec.sad_index = t0->output_sa_index;
111 next0 = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
113 len0 = vlib_buffer_length_in_chain (vm, b0);
115 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
122 vlib_increment_combined_counter (vim->combined_sw_if_counters +
123 VNET_INTERFACE_COUNTER_TX,
124 thread_index, sw_if_index0,
126 last_sw_if_index = sw_if_index0;
131 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
133 ipsec_if_tx_trace_t *tr =
134 vlib_add_trace (vm, node, b0, sizeof (*tr));
136 pool_elt_at_index (im->sad, t0->output_sa_index);
141 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
142 n_left_to_next, bi0, next0);
144 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
147 if (last_sw_if_index != ~0)
149 vlib_increment_combined_counter (vim->combined_sw_if_counters +
150 VNET_INTERFACE_COUNTER_TX,
152 last_sw_if_index, n_packets, n_bytes);
155 return from_frame->n_vectors;
159 static clib_error_t *
160 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
162 ipsec_main_t *im = &ipsec_main;
163 clib_error_t *err = 0;
164 ipsec_tunnel_if_t *t;
165 vnet_hw_interface_t *hi;
168 hi = vnet_get_hw_interface (vnm, hw_if_index);
169 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
171 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
173 ASSERT (im->cb.check_support_cb);
175 sa = pool_elt_at_index (im->sad, t->input_sa_index);
177 err = im->cb.check_support_cb (sa);
181 if (im->cb.add_del_sa_sess_cb)
183 err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 1);
188 sa = pool_elt_at_index (im->sad, t->output_sa_index);
190 err = im->cb.check_support_cb (sa);
194 if (im->cb.add_del_sa_sess_cb)
196 err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 1);
201 vnet_hw_interface_set_flags (vnm, hw_if_index,
202 VNET_HW_INTERFACE_FLAG_LINK_UP);
206 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
208 sa = pool_elt_at_index (im->sad, t->input_sa_index);
210 if (im->cb.add_del_sa_sess_cb)
212 err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 0);
217 sa = pool_elt_at_index (im->sad, t->output_sa_index);
219 if (im->cb.add_del_sa_sess_cb)
221 err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 0);
227 return /* no error */ 0;
232 VNET_DEVICE_CLASS (ipsec_device_class, static) =
235 .format_device_name = format_ipsec_name,
236 .format_tx_trace = format_ipsec_if_tx_trace,
237 .tx_function = ipsec_if_tx_node_fn,
238 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
239 .tx_function_error_strings = ipsec_if_tx_error_strings,
240 .admin_up_down_function = ipsec_admin_up_down_function,
245 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
248 .build_rewrite = default_build_rewrite,
249 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
254 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
256 vnet_main_t *vnm = vnet_get_main ();
257 ASSERT (vlib_get_thread_index () == 0);
259 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
263 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
265 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
266 (u8 *) args, sizeof (*args));
271 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
272 ipsec_add_del_tunnel_args_t * args,
275 ipsec_tunnel_if_t *t;
276 ipsec_main_t *im = &ipsec_main;
277 vnet_hw_interface_t *hi = NULL;
278 u32 hw_if_index = ~0;
284 u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
285 p = hash_get (im->ipsec_if_pool_index_by_key, key);
289 /* check if same src/dst pair exists */
291 return VNET_API_ERROR_INVALID_VALUE;
293 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
294 memset (t, 0, sizeof (*t));
296 dev_instance = t - im->tunnel_interfaces;
298 t->show_instance = args->show_instance;
300 t->show_instance = dev_instance;
302 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
304 pool_put (im->tunnel_interfaces, t);
305 return VNET_API_ERROR_INSTANCE_IN_USE;
308 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
311 pool_get (im->sad, sa);
312 memset (sa, 0, sizeof (*sa));
313 t->input_sa_index = sa - im->sad;
314 sa->spi = args->remote_spi;
315 sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
316 sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
318 sa->use_esn = args->esn;
319 sa->use_anti_replay = args->anti_replay;
320 sa->integ_alg = args->integ_alg;
321 if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
323 sa->integ_key_len = args->remote_integ_key_len;
324 clib_memcpy (sa->integ_key, args->remote_integ_key,
325 args->remote_integ_key_len);
327 sa->crypto_alg = args->crypto_alg;
328 if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
330 sa->crypto_key_len = args->remote_crypto_key_len;
331 clib_memcpy (sa->crypto_key, args->remote_crypto_key,
332 args->remote_crypto_key_len);
335 pool_get (im->sad, sa);
336 memset (sa, 0, sizeof (*sa));
337 t->output_sa_index = sa - im->sad;
338 sa->spi = args->local_spi;
339 sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
340 sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
342 sa->use_esn = args->esn;
343 sa->use_anti_replay = args->anti_replay;
344 sa->integ_alg = args->integ_alg;
345 if (args->local_integ_key_len <= sizeof (args->local_integ_key))
347 sa->integ_key_len = args->local_integ_key_len;
348 clib_memcpy (sa->integ_key, args->local_integ_key,
349 args->local_integ_key_len);
351 sa->crypto_alg = args->crypto_alg;
352 if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
354 sa->crypto_key_len = args->local_crypto_key_len;
355 clib_memcpy (sa->crypto_key, args->local_crypto_key,
356 args->local_crypto_key_len);
359 hash_set (im->ipsec_if_pool_index_by_key, key,
360 t - im->tunnel_interfaces);
362 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
363 t - im->tunnel_interfaces,
364 ipsec_hw_class.index,
365 t - im->tunnel_interfaces);
367 hi = vnet_get_hw_interface (vnm, hw_if_index);
369 slot = vlib_node_add_named_next_with_slot
370 (vnm->vlib_main, hi->tx_node_index, "esp-encrypt",
371 IPSEC_OUTPUT_NEXT_ESP_ENCRYPT);
373 ASSERT (slot == IPSEC_OUTPUT_NEXT_ESP_ENCRYPT);
375 t->hw_if_index = hw_if_index;
377 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
378 hi->sw_if_index, 1, 0, 0);
380 /*1st interface, register protocol */
381 if (pool_elts (im->tunnel_interfaces) == 1)
382 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
383 ipsec_if_input_node.index);
388 /* check if exists */
390 return VNET_API_ERROR_INVALID_VALUE;
392 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
393 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
394 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
396 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
397 hi->sw_if_index, 0, 0, 0);
399 vnet_delete_hw_interface (vnm, t->hw_if_index);
401 /* delete input and output SA */
403 sa = pool_elt_at_index (im->sad, t->input_sa_index);
404 pool_put (im->sad, sa);
406 sa = pool_elt_at_index (im->sad, t->output_sa_index);
407 pool_put (im->sad, sa);
409 hash_unset (im->ipsec_if_pool_index_by_key, key);
410 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
412 pool_put (im->tunnel_interfaces, t);
416 *sw_if_index = hi->sw_if_index;
422 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
423 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
425 ipsec_tunnel_if_t *t = 0;
426 ipsec_main_t *im = &ipsec_main;
432 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
434 return VNET_API_ERROR_INVALID_VALUE;
437 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
439 return VNET_API_ERROR_INVALID_VALUE;
441 sa = pool_elt_at_index (im->sad, p[0]);
444 key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
446 key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
448 p = hash_get (im->ipsec_if_pool_index_by_key, key);
452 /* check if same src/dst pair exists */
454 return VNET_API_ERROR_INVALID_VALUE;
456 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
457 memset (t, 0, sizeof (*t));
459 t->input_sa_index = isa;
460 t->output_sa_index = osa;
462 hash_set (im->ipsec_if_pool_index_by_key, key,
463 t - im->tunnel_interfaces);
465 /*1st interface, register protocol */
466 if (pool_elts (im->tunnel_interfaces) == 1)
467 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
468 ipsec_if_input_node.index);
472 /* check if exists */
474 return VNET_API_ERROR_INVALID_VALUE;
476 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
477 hash_unset (im->ipsec_if_pool_index_by_key, key);
478 pool_put (im->tunnel_interfaces, t);
484 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
485 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
487 ipsec_main_t *im = &ipsec_main;
488 vnet_hw_interface_t *hi;
489 ipsec_tunnel_if_t *t;
492 hi = vnet_get_hw_interface (vnm, hw_if_index);
493 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
495 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
496 return VNET_API_ERROR_SYSCALL_ERROR_1;
498 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
500 sa = pool_elt_at_index (im->sad, t->output_sa_index);
501 sa->crypto_alg = alg;
502 sa->crypto_key_len = vec_len (key);
503 clib_memcpy (sa->crypto_key, key, vec_len (key));
505 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
507 sa = pool_elt_at_index (im->sad, t->output_sa_index);
509 sa->integ_key_len = vec_len (key);
510 clib_memcpy (sa->integ_key, key, vec_len (key));
512 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
514 sa = pool_elt_at_index (im->sad, t->input_sa_index);
515 sa->crypto_alg = alg;
516 sa->crypto_key_len = vec_len (key);
517 clib_memcpy (sa->crypto_key, key, vec_len (key));
519 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
521 sa = pool_elt_at_index (im->sad, t->input_sa_index);
523 sa->integ_key_len = vec_len (key);
524 clib_memcpy (sa->integ_key, key, vec_len (key));
527 return VNET_API_ERROR_INVALID_VALUE;
534 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
537 ipsec_main_t *im = &ipsec_main;
538 vnet_hw_interface_t *hi;
539 ipsec_tunnel_if_t *t;
540 ipsec_sa_t *sa, *old_sa;
541 u32 sa_index, old_sa_index;
544 hi = vnet_get_hw_interface (vnm, hw_if_index);
545 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
547 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
550 clib_warning ("SA with ID %u not found", sa_id);
551 return VNET_API_ERROR_INVALID_VALUE;
554 if (ipsec_is_sa_used (sa_index))
556 clib_warning ("SA with ID %u is already in use", sa_id);
557 return VNET_API_ERROR_INVALID_VALUE;
560 sa = pool_elt_at_index (im->sad, sa_index);
561 if (sa->is_tunnel_ip6)
563 clib_warning ("IPsec interface not supported with IPv6 endpoints");
564 return VNET_API_ERROR_UNIMPLEMENTED;
571 old_sa_index = t->input_sa_index;
572 old_sa = pool_elt_at_index (im->sad, old_sa_index);
574 /* unset old inbound hash entry. packets should stop arriving */
576 (u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) old_sa->spi;
577 p = hash_get (im->ipsec_if_pool_index_by_key, key);
579 hash_unset (im->ipsec_if_pool_index_by_key, key);
581 /* set new inbound SA, then set new hash entry */
582 t->input_sa_index = sa_index;
583 key = (u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) sa->spi;
584 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
588 old_sa_index = t->output_sa_index;
589 old_sa = pool_elt_at_index (im->sad, old_sa_index);
590 t->output_sa_index = sa_index;
593 /* remove sa_id to sa_index mapping on old SA */
594 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
595 hash_unset (im->sa_index_by_sa_id, old_sa->id);
597 if (im->cb.add_del_sa_sess_cb)
601 err = im->cb.add_del_sa_sess_cb (old_sa_index, 0);
603 return VNET_API_ERROR_SYSCALL_ERROR_1;
606 pool_put (im->sad, old_sa);
613 ipsec_tunnel_if_init (vlib_main_t * vm)
615 ipsec_main_t *im = &ipsec_main;
617 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
618 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
623 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
627 * fd.io coding-style-patch-verification: ON
630 * eval: (c-set-style "gnu")