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_output_trace (vlib_main_t * vm,
75 vlib_node_runtime_t * node,
76 vlib_frame_t * frame, const ipsec_tunnel_if_t * t0)
78 ipsec_main_t *im = &ipsec_main;
81 n_left = frame->n_vectors;
82 from = vlib_frame_vector_args (frame);
88 b0 = vlib_get_buffer (vm, from[0]);
90 if (b0->flags & VLIB_BUFFER_IS_TRACED)
92 ipsec_if_tx_trace_t *tr =
93 vlib_add_trace (vm, node, b0, sizeof (*tr));
94 ipsec_sa_t *sa0 = pool_elt_at_index (im->sad, t0->output_sa_index);
104 VNET_DEVICE_CLASS_TX_FN (ipsec_device_class) (vlib_main_t * vm,
105 vlib_node_runtime_t * node,
106 vlib_frame_t * frame)
108 ipsec_main_t *im = &ipsec_main;
110 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
111 const ipsec_tunnel_if_t *t0;
112 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
113 u16 nexts[VLIB_FRAME_SIZE];
115 from = vlib_frame_vector_args (frame);
116 t0 = pool_elt_at_index (im->tunnel_interfaces, rd->dev_instance);
117 n_left = frame->n_vectors;
120 /* All going to encrypt */
121 clib_memset (nexts, 0, sizeof (nexts));
123 if (node->flags & VLIB_NODE_FLAG_TRACE)
124 ipsec_output_trace (vm, node, frame, t0);
126 vlib_get_buffers (vm, from, bufs, n_left);
130 /* Prefetch the buffer header for the N+2 loop iteration */
131 vlib_prefetch_buffer_header (b[4], STORE);
132 vlib_prefetch_buffer_header (b[5], STORE);
133 vlib_prefetch_buffer_header (b[6], STORE);
134 vlib_prefetch_buffer_header (b[7], STORE);
136 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
137 vnet_buffer (b[1])->ipsec.sad_index = t0->output_sa_index;
138 vnet_buffer (b[2])->ipsec.sad_index = t0->output_sa_index;
139 vnet_buffer (b[3])->ipsec.sad_index = t0->output_sa_index;
146 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
152 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
154 return frame->n_vectors;
158 static clib_error_t *
159 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
161 ipsec_main_t *im = &ipsec_main;
162 clib_error_t *err = 0;
163 ipsec_tunnel_if_t *t;
164 vnet_hw_interface_t *hi;
167 hi = vnet_get_hw_interface (vnm, hw_if_index);
168 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
171 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
173 sa = pool_elt_at_index (im->sad, t->input_sa_index);
175 err = ipsec_check_support_cb (im, sa);
179 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
183 sa = pool_elt_at_index (im->sad, t->output_sa_index);
185 err = ipsec_check_support_cb (im, sa);
189 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
193 vnet_hw_interface_set_flags (vnm, hw_if_index,
194 VNET_HW_INTERFACE_FLAG_LINK_UP);
198 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
199 sa = pool_elt_at_index (im->sad, t->input_sa_index);
200 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
203 sa = pool_elt_at_index (im->sad, t->output_sa_index);
204 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
209 return /* no error */ 0;
214 VNET_DEVICE_CLASS (ipsec_device_class) =
217 .format_device_name = format_ipsec_name,
218 .format_tx_trace = format_ipsec_if_tx_trace,
219 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
220 .tx_function_error_strings = ipsec_if_tx_error_strings,
221 .admin_up_down_function = ipsec_admin_up_down_function,
226 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
229 .build_rewrite = default_build_rewrite,
230 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
235 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
237 vnet_main_t *vnm = vnet_get_main ();
238 ASSERT (vlib_get_thread_index () == 0);
240 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
244 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
246 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
247 (u8 *) args, sizeof (*args));
252 ipsec_tun_mk_input_sa_id (u32 ti)
254 return (0x80000000 | ti);
258 ipsec_tun_mk_output_sa_id (u32 ti)
260 return (0xc0000000 | ti);
264 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
265 ipsec_add_del_tunnel_args_t * args,
268 ipsec_tunnel_if_t *t;
269 ipsec_main_t *im = &ipsec_main;
270 vnet_hw_interface_t *hi = NULL;
271 u32 hw_if_index = ~0;
275 ipsec_key_t crypto_key, integ_key;
276 ipsec_sa_flags_t flags;
279 u64 key = ((u64) args->remote_ip.ip4.as_u32 << 32 |
280 (u64) clib_host_to_net_u32 (args->remote_spi));
281 p = hash_get (im->ipsec_if_pool_index_by_key, key);
285 /* check if same src/dst pair exists */
287 return VNET_API_ERROR_INVALID_VALUE;
289 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
291 dev_instance = t - im->tunnel_interfaces;
293 t->show_instance = args->show_instance;
295 t->show_instance = dev_instance;
297 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
299 pool_put (im->tunnel_interfaces, t);
300 return VNET_API_ERROR_INSTANCE_IN_USE;
303 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
306 flags = IPSEC_SA_FLAG_IS_TUNNEL;
308 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
310 flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM;
311 if (args->anti_replay)
312 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
314 ipsec_mk_key (&crypto_key,
315 args->remote_crypto_key, args->remote_crypto_key_len);
316 ipsec_mk_key (&integ_key,
317 args->remote_integ_key, args->remote_integ_key_len);
319 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
329 &args->local_ip, &t->input_sa_index);
332 return VNET_API_ERROR_UNIMPLEMENTED;
334 ipsec_mk_key (&crypto_key,
335 args->local_crypto_key, args->local_crypto_key_len);
336 ipsec_mk_key (&integ_key,
337 args->local_integ_key, args->local_integ_key_len);
339 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
349 &args->remote_ip, &t->output_sa_index);
352 return VNET_API_ERROR_UNIMPLEMENTED;
354 hash_set (im->ipsec_if_pool_index_by_key, key,
355 t - im->tunnel_interfaces);
357 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
358 t - im->tunnel_interfaces,
359 ipsec_hw_class.index,
360 t - im->tunnel_interfaces);
362 hi = vnet_get_hw_interface (vnm, hw_if_index);
363 /* add esp4 as the next-node-index of this tx-node */
365 slot = vlib_node_add_next_with_slot
366 (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, 0);
370 t->hw_if_index = hw_if_index;
371 t->sw_if_index = hi->sw_if_index;
373 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
374 hi->sw_if_index, 1, 0, 0);
376 /*1st interface, register protocol */
377 if (pool_elts (im->tunnel_interfaces) == 1)
378 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
379 ipsec_if_input_node.index);
384 /* check if exists */
386 return VNET_API_ERROR_INVALID_VALUE;
388 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
389 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
390 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
392 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
393 hi->sw_if_index, 0, 0, 0);
395 vnet_delete_hw_interface (vnm, t->hw_if_index);
397 hash_unset (im->ipsec_if_pool_index_by_key, key);
398 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
400 pool_put (im->tunnel_interfaces, t);
402 /* delete input and output SA */
403 ipsec_sa_del (ipsec_tun_mk_input_sa_id (p[0]));
404 ipsec_sa_del (ipsec_tun_mk_output_sa_id (p[0]));
408 *sw_if_index = hi->sw_if_index;
414 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
415 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
417 ipsec_tunnel_if_t *t = 0;
418 ipsec_main_t *im = &ipsec_main;
424 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
426 return VNET_API_ERROR_INVALID_VALUE;
429 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
431 return VNET_API_ERROR_INVALID_VALUE;
433 sa = pool_elt_at_index (im->sad, p[0]);
436 key = ((u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 |
437 (u64) clib_host_to_net_u32 (sa->spi));
439 key = ((u64) args->remote_ip.as_u32 << 32 |
440 (u64) clib_host_to_net_u32 (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 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
498 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
500 sa = pool_elt_at_index (im->sad, t->output_sa_index);
502 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
504 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
506 sa = pool_elt_at_index (im->sad, t->input_sa_index);
507 sa->crypto_alg = alg;
508 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
510 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
512 sa = pool_elt_at_index (im->sad, t->input_sa_index);
514 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
517 return VNET_API_ERROR_INVALID_VALUE;
524 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
527 ipsec_main_t *im = &ipsec_main;
528 vnet_hw_interface_t *hi;
529 ipsec_tunnel_if_t *t;
530 ipsec_sa_t *sa, *old_sa;
531 u32 sa_index, old_sa_index;
534 hi = vnet_get_hw_interface (vnm, hw_if_index);
535 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
537 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
540 clib_warning ("SA with ID %u not found", sa_id);
541 return VNET_API_ERROR_INVALID_VALUE;
544 if (ipsec_is_sa_used (sa_index))
546 clib_warning ("SA with ID %u is already in use", sa_id);
547 return VNET_API_ERROR_INVALID_VALUE;
550 sa = pool_elt_at_index (im->sad, sa_index);
551 if (sa->is_tunnel_ip6)
553 clib_warning ("IPsec interface not supported with IPv6 endpoints");
554 return VNET_API_ERROR_UNIMPLEMENTED;
561 old_sa_index = t->input_sa_index;
562 old_sa = pool_elt_at_index (im->sad, old_sa_index);
564 /* unset old inbound hash entry. packets should stop arriving */
565 key = ((u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 |
566 (u64) clib_host_to_net_u32 (old_sa->spi));
567 p = hash_get (im->ipsec_if_pool_index_by_key, key);
569 hash_unset (im->ipsec_if_pool_index_by_key, key);
571 /* set new inbound SA, then set new hash entry */
572 t->input_sa_index = sa_index;
573 key = ((u64) sa->tunnel_src_addr.ip4.as_u32 << 32 |
574 (u64) clib_host_to_net_u32 (sa->spi));
575 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
579 old_sa_index = t->output_sa_index;
580 old_sa = pool_elt_at_index (im->sad, old_sa_index);
581 t->output_sa_index = sa_index;
584 /* remove sa_id to sa_index mapping on old SA */
585 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
586 hash_unset (im->sa_index_by_sa_id, old_sa->id);
588 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
590 clib_warning ("IPsec backend add/del callback returned error");
591 return VNET_API_ERROR_SYSCALL_ERROR_1;
593 pool_put (im->sad, old_sa);
599 ipsec_tunnel_if_init (vlib_main_t * vm)
601 ipsec_main_t *im = &ipsec_main;
603 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
604 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
609 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
613 * fd.io coding-style-patch-verification: ON
616 * eval: (c-set-style "gnu")