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>
22 #include <vnet/udp/udp.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
27 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
30 format_ipsec_name (u8 * s, va_list * args)
32 u32 dev_instance = va_arg (*args, u32);
33 ipsec_main_t *im = &ipsec_main;
34 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
36 return format (s, "ipsec%d", t->show_instance);
39 /* Statistics (not really errors) */
40 #define foreach_ipsec_if_tx_error \
41 _(TX, "good packets transmitted")
43 static char *ipsec_if_tx_error_strings[] = {
44 #define _(sym,string) string,
45 foreach_ipsec_if_tx_error
51 #define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
52 foreach_ipsec_if_tx_error
55 } ipsec_if_tx_error_t;
61 } ipsec_if_tx_trace_t;
64 format_ipsec_if_tx_trace (u8 * s, va_list * args)
66 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
67 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
68 ipsec_if_tx_trace_t *t = va_arg (*args, ipsec_if_tx_trace_t *);
70 s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
75 ipsec_output_trace (vlib_main_t * vm,
76 vlib_node_runtime_t * node,
77 vlib_frame_t * frame, const ipsec_tunnel_if_t * t0)
79 ipsec_main_t *im = &ipsec_main;
82 n_left = frame->n_vectors;
83 from = vlib_frame_vector_args (frame);
89 b0 = vlib_get_buffer (vm, from[0]);
91 if (b0->flags & VLIB_BUFFER_IS_TRACED)
93 ipsec_if_tx_trace_t *tr =
94 vlib_add_trace (vm, node, b0, sizeof (*tr));
95 ipsec_sa_t *sa0 = pool_elt_at_index (im->sad, t0->output_sa_index);
105 VNET_DEVICE_CLASS_TX_FN (ipsec_device_class) (vlib_main_t * vm,
106 vlib_node_runtime_t * node,
107 vlib_frame_t * frame)
109 ipsec_main_t *im = &ipsec_main;
111 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
112 const ipsec_tunnel_if_t *t0;
113 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
114 u16 nexts[VLIB_FRAME_SIZE];
116 from = vlib_frame_vector_args (frame);
117 t0 = pool_elt_at_index (im->tunnel_interfaces, rd->dev_instance);
118 n_left = frame->n_vectors;
121 /* All going to encrypt */
122 clib_memset (nexts, 0, sizeof (nexts));
124 if (node->flags & VLIB_NODE_FLAG_TRACE)
125 ipsec_output_trace (vm, node, frame, t0);
127 vlib_get_buffers (vm, from, bufs, n_left);
131 /* Prefetch the buffer header for the N+2 loop iteration */
132 vlib_prefetch_buffer_header (b[4], STORE);
133 vlib_prefetch_buffer_header (b[5], STORE);
134 vlib_prefetch_buffer_header (b[6], STORE);
135 vlib_prefetch_buffer_header (b[7], STORE);
137 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
138 vnet_buffer (b[1])->ipsec.sad_index = t0->output_sa_index;
139 vnet_buffer (b[2])->ipsec.sad_index = t0->output_sa_index;
140 vnet_buffer (b[3])->ipsec.sad_index = t0->output_sa_index;
147 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
153 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
155 return 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);
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) =
218 .format_device_name = format_ipsec_name,
219 .format_tx_trace = format_ipsec_if_tx_trace,
220 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
221 .tx_function_error_strings = ipsec_if_tx_error_strings,
222 .admin_up_down_function = ipsec_admin_up_down_function,
227 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
230 .build_rewrite = default_build_rewrite,
231 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
236 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
238 vnet_main_t *vnm = vnet_get_main ();
239 ASSERT (vlib_get_thread_index () == 0);
241 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
245 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
247 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
248 (u8 *) args, sizeof (*args));
253 ipsec_tun_mk_input_sa_id (u32 ti)
255 return (0x80000000 | ti);
259 ipsec_tun_mk_output_sa_id (u32 ti)
261 return (0xc0000000 | ti);
265 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
266 ipsec_add_del_tunnel_args_t * args,
269 ipsec_tunnel_if_t *t;
270 ipsec_main_t *im = &ipsec_main;
271 vnet_hw_interface_t *hi = NULL;
272 u32 hw_if_index = ~0;
276 ipsec_key_t crypto_key, integ_key;
277 ipsec_sa_flags_t flags;
280 u64 key = ((u64) args->remote_ip.ip4.as_u32 << 32 |
281 (u64) clib_host_to_net_u32 (args->remote_spi));
282 p = hash_get (im->ipsec_if_pool_index_by_key, key);
286 /* check if same src/dst pair exists */
288 return VNET_API_ERROR_INVALID_VALUE;
290 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
292 dev_instance = t - im->tunnel_interfaces;
294 t->show_instance = args->show_instance;
296 t->show_instance = dev_instance;
298 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
300 pool_put (im->tunnel_interfaces, t);
301 return VNET_API_ERROR_INSTANCE_IN_USE;
304 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
307 flags = IPSEC_SA_FLAG_IS_TUNNEL;
309 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
311 flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM;
312 if (args->anti_replay)
313 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
315 ipsec_mk_key (&crypto_key,
316 args->remote_crypto_key, args->remote_crypto_key_len);
317 ipsec_mk_key (&integ_key,
318 args->remote_integ_key, args->remote_integ_key_len);
320 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
330 &args->local_ip, &t->input_sa_index);
333 return VNET_API_ERROR_UNIMPLEMENTED;
335 ipsec_mk_key (&crypto_key,
336 args->local_crypto_key, args->local_crypto_key_len);
337 ipsec_mk_key (&integ_key,
338 args->local_integ_key, args->local_integ_key_len);
340 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
350 &args->remote_ip, &t->output_sa_index);
353 return VNET_API_ERROR_UNIMPLEMENTED;
355 hash_set (im->ipsec_if_pool_index_by_key, key,
356 t - im->tunnel_interfaces);
358 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
359 t - im->tunnel_interfaces,
360 ipsec_hw_class.index,
361 t - im->tunnel_interfaces);
363 hi = vnet_get_hw_interface (vnm, hw_if_index);
364 /* add esp4 as the next-node-index of this tx-node */
366 slot = vlib_node_add_next_with_slot
367 (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, 0);
371 t->hw_if_index = hw_if_index;
372 t->sw_if_index = hi->sw_if_index;
374 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
375 hi->sw_if_index, 1, 0, 0);
377 /*1st interface, register protocol */
378 if (pool_elts (im->tunnel_interfaces) == 1)
379 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
380 ipsec_if_input_node.index);
387 /* check if exists */
389 return VNET_API_ERROR_INVALID_VALUE;
392 t = pool_elt_at_index (im->tunnel_interfaces, ti);
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 hash_unset (im->ipsec_if_pool_index_by_key, key);
402 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
404 pool_put (im->tunnel_interfaces, t);
406 /* delete input and output SA */
407 ipsec_sa_del (ipsec_tun_mk_input_sa_id (ti));
408 ipsec_sa_del (ipsec_tun_mk_output_sa_id (ti));
412 *sw_if_index = hi->sw_if_index;
418 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
419 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
421 ipsec_tunnel_if_t *t = 0;
422 ipsec_main_t *im = &ipsec_main;
428 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
430 return VNET_API_ERROR_INVALID_VALUE;
433 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
435 return VNET_API_ERROR_INVALID_VALUE;
437 sa = pool_elt_at_index (im->sad, p[0]);
440 key = ((u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 |
441 (u64) clib_host_to_net_u32 (sa->spi));
443 key = ((u64) args->remote_ip.as_u32 << 32 |
444 (u64) clib_host_to_net_u32 (sa->spi));
446 p = hash_get (im->ipsec_if_pool_index_by_key, key);
450 /* check if same src/dst pair exists */
452 return VNET_API_ERROR_INVALID_VALUE;
454 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
455 clib_memset (t, 0, sizeof (*t));
457 t->input_sa_index = isa;
458 t->output_sa_index = osa;
460 hash_set (im->ipsec_if_pool_index_by_key, key,
461 t - im->tunnel_interfaces);
463 /*1st interface, register protocol */
464 if (pool_elts (im->tunnel_interfaces) == 1)
465 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
466 ipsec_if_input_node.index);
470 /* check if exists */
472 return VNET_API_ERROR_INVALID_VALUE;
474 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
475 hash_unset (im->ipsec_if_pool_index_by_key, key);
476 pool_put (im->tunnel_interfaces, t);
482 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
483 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
485 ipsec_main_t *im = &ipsec_main;
486 vnet_hw_interface_t *hi;
487 ipsec_tunnel_if_t *t;
490 hi = vnet_get_hw_interface (vnm, hw_if_index);
491 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
493 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
494 return VNET_API_ERROR_SYSCALL_ERROR_1;
496 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
498 sa = pool_elt_at_index (im->sad, t->output_sa_index);
499 ipsec_sa_set_crypto_alg (sa, alg);
500 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
502 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
504 sa = pool_elt_at_index (im->sad, t->output_sa_index);
505 ipsec_sa_set_integ_alg (sa, alg);
506 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
508 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
510 sa = pool_elt_at_index (im->sad, t->input_sa_index);
511 ipsec_sa_set_crypto_alg (sa, alg);
512 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
514 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
516 sa = pool_elt_at_index (im->sad, t->input_sa_index);
517 ipsec_sa_set_integ_alg (sa, alg);
518 ipsec_mk_key (&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 */
569 key = ((u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 |
570 (u64) clib_host_to_net_u32 (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 |
578 (u64) clib_host_to_net_u32 (sa->spi));
579 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
583 old_sa_index = t->output_sa_index;
584 old_sa = pool_elt_at_index (im->sad, old_sa_index);
585 t->output_sa_index = sa_index;
588 /* remove sa_id to sa_index mapping on old SA */
589 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
590 hash_unset (im->sa_index_by_sa_id, old_sa->id);
592 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
594 clib_warning ("IPsec backend add/del callback returned error");
595 return VNET_API_ERROR_SYSCALL_ERROR_1;
597 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));
610 udp_register_dst_port (vm, UDP_DST_PORT_ipsec, ipsec_if_input_node.index,
616 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
620 * fd.io coding-style-patch-verification: ON
623 * eval: (c-set-style "gnu")