blob: 3c1f84576d4a0049e22d15fb3ef843bd88e3d0f6 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * ipsec_if.c : IPSec interface support
3 *
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:
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/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
Pierre Pfister4c422f92018-12-10 11:19:08 +010021#include <vnet/fib/fib.h>
Kingwel Xie00bff192019-03-07 01:25:32 -050022#include <vnet/udp/udp.h>
Neale Ranns25edf142019-03-22 08:12:48 +000023#include <vnet/adj/adj_midchain.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
25#include <vnet/ipsec/ipsec.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000026#include <vnet/ipsec/esp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
Matthew Smith2838a232016-06-21 16:05:09 -050028void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
29
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070030static u8 *
31format_ipsec_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070032{
33 u32 dev_instance = va_arg (*args, u32);
Matthew Smith8e1039a2018-04-12 07:32:56 -050034 ipsec_main_t *im = &ipsec_main;
35 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
36
37 return format (s, "ipsec%d", t->show_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -070038}
39
Klement Sekera31da2e32018-06-24 22:49:55 +020040/* Statistics (not really errors) */
41#define foreach_ipsec_if_tx_error \
42_(TX, "good packets transmitted")
43
Neale Ranns25edf142019-03-22 08:12:48 +000044static void
45ipsec_if_tunnel_stack (adj_index_t ai)
Klement Sekeraa98346f2018-05-16 10:52:45 +020046{
Neale Ranns25edf142019-03-22 08:12:48 +000047 ipsec_main_t *ipm = &ipsec_main;
48 ipsec_tunnel_if_t *it;
49 ip_adjacency_t *adj;
50 u32 sw_if_index;
Klement Sekera31da2e32018-06-24 22:49:55 +020051
Neale Ranns25edf142019-03-22 08:12:48 +000052 adj = adj_get (ai);
53 sw_if_index = adj->rewrite_header.sw_if_index;
54
55 if ((vec_len (ipm->ipsec_if_by_sw_if_index) <= sw_if_index) ||
56 (~0 == ipm->ipsec_if_by_sw_if_index[sw_if_index]))
57 return;
58
59 it = pool_elt_at_index (ipm->tunnel_interfaces,
60 ipm->ipsec_if_by_sw_if_index[sw_if_index]);
61
62 if (!vnet_hw_interface_is_link_up (vnet_get_main (), it->hw_if_index))
63 {
64 adj_midchain_delegate_unstack (ai);
65 }
66 else
67 {
68 ipsec_sa_t *sa;
69
70 sa = ipsec_sa_get (it->output_sa_index);
71
Neale Ranns45d8f852019-03-28 08:59:12 +000072 /* *INDENT-OFF* */
Neale Ranns25edf142019-03-22 08:12:48 +000073 fib_prefix_t pfx = {
74 .fp_addr = sa->tunnel_dst_addr,
Neale Ranns45d8f852019-03-28 08:59:12 +000075 .fp_len = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ? 128 : 32),
76 .fp_proto = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ?
77 FIB_PROTOCOL_IP6 :
78 FIB_PROTOCOL_IP4),
Neale Ranns25edf142019-03-22 08:12:48 +000079 };
Neale Ranns45d8f852019-03-28 08:59:12 +000080 /* *INDENT-ON* */
Neale Ranns25edf142019-03-22 08:12:48 +000081
82 adj_midchain_delegate_stack (ai, sa->tx_fib_index, &pfx);
83 }
84}
85
86/**
87 * @brief Call back when restacking all adjacencies on a GRE interface
88 */
89static adj_walk_rc_t
90ipsec_if_adj_walk_cb (adj_index_t ai, void *ctx)
Klement Sekera31da2e32018-06-24 22:49:55 +020091{
Neale Ranns25edf142019-03-22 08:12:48 +000092 ipsec_if_tunnel_stack (ai);
Klement Sekera31da2e32018-06-24 22:49:55 +020093
Neale Ranns25edf142019-03-22 08:12:48 +000094 return (ADJ_WALK_RC_CONTINUE);
Klement Sekeraa98346f2018-05-16 10:52:45 +020095}
96
Neale Ranns77eb28f2019-03-04 14:13:14 +000097static void
Neale Ranns25edf142019-03-22 08:12:48 +000098ipsec_if_tunnel_restack (ipsec_tunnel_if_t * it)
Neale Rannsfe480f62019-02-28 12:03:58 +000099{
Neale Ranns25edf142019-03-22 08:12:48 +0000100 fib_protocol_t proto;
Neale Rannsfe480f62019-02-28 12:03:58 +0000101
Neale Ranns25edf142019-03-22 08:12:48 +0000102 /*
103 * walk all the adjacencies on th GRE interface and restack them
104 */
105 FOR_EACH_FIB_IP_PROTOCOL (proto)
106 {
107 adj_nbr_walk (it->sw_if_index, proto, ipsec_if_adj_walk_cb, NULL);
108 }
Neale Rannsfe480f62019-02-28 12:03:58 +0000109}
110
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000111static clib_error_t *
112ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
113{
114 ipsec_main_t *im = &ipsec_main;
115 clib_error_t *err = 0;
116 ipsec_tunnel_if_t *t;
117 vnet_hw_interface_t *hi;
118 ipsec_sa_t *sa;
119
120 hi = vnet_get_hw_interface (vnm, hw_if_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100121 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
Neale Rannsfe480f62019-02-28 12:03:58 +0000122 t->flags = flags;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100123
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000124 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
125 {
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000126 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100127
Klement Sekerab4d30532018-11-08 13:00:02 +0100128 err = ipsec_check_support_cb (im, sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000129 if (err)
130 return err;
131
Klement Sekerab4d30532018-11-08 13:00:02 +0100132 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
133 if (err)
134 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100135
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000136 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100137
Klement Sekerab4d30532018-11-08 13:00:02 +0100138 err = ipsec_check_support_cb (im, sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000139 if (err)
140 return err;
141
Klement Sekerab4d30532018-11-08 13:00:02 +0100142 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
143 if (err)
144 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100145
Radu Nicolau3f903392017-01-30 14:33:39 +0000146 vnet_hw_interface_set_flags (vnm, hw_if_index,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000147 VNET_HW_INTERFACE_FLAG_LINK_UP);
148 }
149 else
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100150 {
151 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100152 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100153 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
154 if (err)
155 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100156 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100157 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
158 if (err)
159 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100160 }
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000161
Neale Ranns25edf142019-03-22 08:12:48 +0000162 ipsec_if_tunnel_restack (t);
163
164 return (NULL);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000165}
166
Neale Ranns25edf142019-03-22 08:12:48 +0000167static u8 *
168ipsec_if_build_rewrite (vnet_main_t * vnm,
169 u32 sw_if_index,
170 vnet_link_t link_type, const void *dst_address)
171{
172 return (NULL);
173}
174
175static void
176ipsec_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
177{
178 adj_nbr_midchain_update_rewrite
179 (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK,
180 ipsec_if_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
181
182 ipsec_if_tunnel_stack (ai);
183}
Klement Sekera31da2e32018-06-24 22:49:55 +0200184
Neale Rannsb80c5362016-10-08 13:03:40 +0100185/* *INDENT-OFF* */
Neale Ranns77eb28f2019-03-04 14:13:14 +0000186VNET_DEVICE_CLASS (ipsec_device_class) =
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700187{
Neale Rannsb80c5362016-10-08 13:03:40 +0100188 .name = "IPSec",
189 .format_device_name = format_ipsec_name,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000190 .admin_up_down_function = ipsec_admin_up_down_function,
Neale Rannsb80c5362016-10-08 13:03:40 +0100191};
192/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Neale Rannsb80c5362016-10-08 13:03:40 +0100194/* *INDENT-OFF* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700195VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
196{
Neale Rannsb80c5362016-10-08 13:03:40 +0100197 .name = "IPSec",
198 .build_rewrite = default_build_rewrite,
Neale Ranns25edf142019-03-22 08:12:48 +0000199 .update_adjacency = ipsec_if_update_adj,
Matthew Smith922549a2017-05-24 16:18:27 -0500200 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Neale Rannsb80c5362016-10-08 13:03:40 +0100201};
202/* *INDENT-ON* */
Matthew Smith2838a232016-06-21 16:05:09 -0500203
204static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700205ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
Matthew Smith2838a232016-06-21 16:05:09 -0500206{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700207 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion586afd72017-04-05 19:18:20 +0200208 ASSERT (vlib_get_thread_index () == 0);
Matthew Smith2838a232016-06-21 16:05:09 -0500209
Matthew Smithe04d09d2017-05-14 21:47:18 -0500210 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
Matthew Smith2838a232016-06-21 16:05:09 -0500211}
212
213int
214ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
215{
216 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700217 (u8 *) args, sizeof (*args));
Matthew Smith2838a232016-06-21 16:05:09 -0500218 return 0;
219}
220
Neale Ranns8d7c5022019-02-06 01:41:05 -0800221static u32
222ipsec_tun_mk_input_sa_id (u32 ti)
223{
224 return (0x80000000 | ti);
225}
226
227static u32
228ipsec_tun_mk_output_sa_id (u32 ti)
229{
230 return (0xc0000000 | ti);
231}
232
Matthew Smith2838a232016-06-21 16:05:09 -0500233int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700234ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
Matthew Smithe04d09d2017-05-14 21:47:18 -0500235 ipsec_add_del_tunnel_args_t * args,
236 u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700238 ipsec_tunnel_if_t *t;
239 ipsec_main_t *im = &ipsec_main;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500240 vnet_hw_interface_t *hi = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 u32 hw_if_index = ~0;
242 uword *p;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500243 u32 dev_instance;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800244 ipsec_key_t crypto_key, integ_key;
245 ipsec_sa_flags_t flags;
246 int rv;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400247 int is_ip6 = args->is_ip6;
248 ipsec4_tunnel_key_t key4;
249 ipsec6_tunnel_key_t key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400251 if (!is_ip6)
252 {
253 key4.remote_ip = args->remote_ip.ip4.as_u32;
254 key4.spi = clib_host_to_net_u32 (args->remote_spi);
255 p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64);
256 }
257 else
258 {
259 key6.remote_ip = args->remote_ip.ip6;
260 key6.spi = clib_host_to_net_u32 (args->remote_spi);
261 p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6);
262 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
264 if (args->is_add)
265 {
266 /* check if same src/dst pair exists */
267 if (p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700268 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
Neale Rannsfe480f62019-02-28 12:03:58 +0000270 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Matthew Smith8e1039a2018-04-12 07:32:56 -0500272 dev_instance = t - im->tunnel_interfaces;
273 if (args->renumber)
274 t->show_instance = args->show_instance;
275 else
276 t->show_instance = dev_instance;
277
278 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
279 {
280 pool_put (im->tunnel_interfaces, t);
281 return VNET_API_ERROR_INSTANCE_IN_USE;
282 }
283
284 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
285 dev_instance);
286
Neale Ranns8d7c5022019-02-06 01:41:05 -0800287 flags = IPSEC_SA_FLAG_IS_TUNNEL;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400288 if (args->is_ip6)
289 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800290 if (args->udp_encap)
291 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
292 if (args->esn)
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100293 flags |= IPSEC_SA_FLAG_USE_ESN;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800294 if (args->anti_replay)
295 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Neale Ranns8d7c5022019-02-06 01:41:05 -0800297 ipsec_mk_key (&crypto_key,
298 args->remote_crypto_key, args->remote_crypto_key_len);
299 ipsec_mk_key (&integ_key,
300 args->remote_integ_key, args->remote_integ_key_len);
301
302 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
303 args->remote_spi,
304 IPSEC_PROTOCOL_ESP,
305 args->crypto_alg,
306 &crypto_key,
307 args->integ_alg,
308 &integ_key,
Neale Ranns2b5ba952019-04-02 10:15:40 +0000309 (flags | IPSEC_SA_FLAG_IS_INBOUND),
Neale Ranns8d7c5022019-02-06 01:41:05 -0800310 args->tx_table_id,
311 &args->remote_ip,
312 &args->local_ip, &t->input_sa_index);
313
314 if (rv)
Neale Rannsaf3f0782019-03-26 08:21:25 +0000315 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800316
317 ipsec_mk_key (&crypto_key,
318 args->local_crypto_key, args->local_crypto_key_len);
319 ipsec_mk_key (&integ_key,
320 args->local_integ_key, args->local_integ_key_len);
321
322 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
323 args->local_spi,
324 IPSEC_PROTOCOL_ESP,
325 args->crypto_alg,
326 &crypto_key,
327 args->integ_alg,
328 &integ_key,
329 flags,
330 args->tx_table_id,
331 &args->local_ip,
332 &args->remote_ip, &t->output_sa_index);
333
334 if (rv)
Neale Rannsaf3f0782019-03-26 08:21:25 +0000335 return VNET_API_ERROR_INVALID_DST_ADDRESS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400337 /* copy the key */
338 if (is_ip6)
339 hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6,
340 t - im->tunnel_interfaces);
341 else
342 hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64,
343 t - im->tunnel_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
Matthew Smith8e1039a2018-04-12 07:32:56 -0500345 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
346 t - im->tunnel_interfaces,
347 ipsec_hw_class.index,
348 t - im->tunnel_interfaces);
Matthew Smithe04d09d2017-05-14 21:47:18 -0500349
350 hi = vnet_get_hw_interface (vnm, hw_if_index);
Klement Sekera31da2e32018-06-24 22:49:55 +0200351
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 t->hw_if_index = hw_if_index;
Neale Rannsfe480f62019-02-28 12:03:58 +0000353 t->sw_if_index = hi->sw_if_index;
354
Neale Rannsd7603d92019-03-28 08:56:10 +0000355 /* Standard default jumbo MTU. */
356 vnet_sw_interface_set_mtu (vnm, t->sw_if_index, 9000);
357
Neale Ranns25edf142019-03-22 08:12:48 +0000358 /* Add the new tunnel to the DB of tunnels per sw_if_index ... */
359 vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index,
360 ~0);
361 im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance;
362
363 vnet_feature_enable_disable ("ip4-output",
364 "esp4-encrypt-tun",
365 t->sw_if_index, 1,
366 &t->output_sa_index,
367 sizeof (t->output_sa_index));
368 vnet_feature_enable_disable ("ip6-output",
369 "esp6-encrypt-tun",
370 t->sw_if_index, 1,
371 &t->output_sa_index,
372 sizeof (t->output_sa_index));
373
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 /*1st interface, register protocol */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700375 if (pool_elts (im->tunnel_interfaces) == 1)
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400376 {
377 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
378 ipsec4_if_input_node.index);
379 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
380 ipsec6_if_input_node.index);
381 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 }
384 else
385 {
Neale Rannsc80cc9a2019-03-20 14:10:23 +0000386 u32 ti;
387
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 /* check if exists */
389 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700390 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Neale Rannsc80cc9a2019-03-20 14:10:23 +0000392 ti = p[0];
393 t = pool_elt_at_index (im->tunnel_interfaces, ti);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700395 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
Matthew Smith537eeec2018-04-09 11:49:20 -0500396
Neale Ranns25edf142019-03-22 08:12:48 +0000397 vnet_feature_enable_disable ("ip4-output",
398 "esp4-encrypt-tun",
399 hi->sw_if_index, 0,
400 &t->output_sa_index,
401 sizeof (t->output_sa_index));
402 vnet_feature_enable_disable ("ip6-output",
403 "esp6-encrypt-tun",
404 hi->sw_if_index, 0,
405 &t->output_sa_index,
406 sizeof (t->output_sa_index));
Matthew Smith8e1039a2018-04-12 07:32:56 -0500407 vnet_delete_hw_interface (vnm, t->hw_if_index);
Matthew Smith01034be2017-05-16 11:51:18 -0500408
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400409 if (is_ip6)
410 hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6);
411 else
412 hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64);
413
Matthew Smith8e1039a2018-04-12 07:32:56 -0500414 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
Neale Ranns25edf142019-03-22 08:12:48 +0000415 im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 pool_put (im->tunnel_interfaces, t);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800418
419 /* delete input and output SA */
Neale Rannsc80cc9a2019-03-20 14:10:23 +0000420 ipsec_sa_del (ipsec_tun_mk_input_sa_id (ti));
421 ipsec_sa_del (ipsec_tun_mk_output_sa_id (ti));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 }
Matthew Smithe04d09d2017-05-14 21:47:18 -0500423
424 if (sw_if_index)
425 *sw_if_index = hi->sw_if_index;
426
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 return 0;
428}
429
430int
Matus Fabian694265d2016-08-10 01:55:36 -0700431ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
Neale Rannse524d452019-02-19 15:22:46 +0000432 const ipsec_gre_tunnel_add_del_args_t * args)
Matus Fabian694265d2016-08-10 01:55:36 -0700433{
434 ipsec_tunnel_if_t *t = 0;
435 ipsec_main_t *im = &ipsec_main;
436 uword *p;
437 ipsec_sa_t *sa;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400438 ipsec4_tunnel_key_t key;
Matus Fabian694265d2016-08-10 01:55:36 -0700439 u32 isa, osa;
440
441 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
442 if (!p)
443 return VNET_API_ERROR_INVALID_VALUE;
Neale Rannse524d452019-02-19 15:22:46 +0000444 osa = p[0];
445 sa = pool_elt_at_index (im->sad, p[0]);
446 ipsec_sa_set_IS_GRE (sa);
Matus Fabian694265d2016-08-10 01:55:36 -0700447
448 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
449 if (!p)
450 return VNET_API_ERROR_INVALID_VALUE;
Neale Rannse524d452019-02-19 15:22:46 +0000451 isa = p[0];
Matus Fabian694265d2016-08-10 01:55:36 -0700452 sa = pool_elt_at_index (im->sad, p[0]);
Neale Rannse524d452019-02-19 15:22:46 +0000453 ipsec_sa_set_IS_GRE (sa);
Matus Fabian694265d2016-08-10 01:55:36 -0700454
Neale Rannse524d452019-02-19 15:22:46 +0000455 /* we form the key from the input/remote SA whose tunnel is srouce
456 * at the remote end */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100457 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400458 {
Neale Rannse524d452019-02-19 15:22:46 +0000459 key.remote_ip = sa->tunnel_src_addr.ip4.as_u32;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400460 key.spi = clib_host_to_net_u32 (sa->spi);
461 }
Matus Fabian694265d2016-08-10 01:55:36 -0700462 else
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400463 {
Neale Rannse524d452019-02-19 15:22:46 +0000464 key.remote_ip = args->src.as_u32;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400465 key.spi = clib_host_to_net_u32 (sa->spi);
466 }
Matus Fabian694265d2016-08-10 01:55:36 -0700467
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400468 p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
Matus Fabian694265d2016-08-10 01:55:36 -0700469
470 if (args->is_add)
471 {
472 /* check if same src/dst pair exists */
473 if (p)
474 return VNET_API_ERROR_INVALID_VALUE;
475
476 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400477 clib_memset (t, 0, sizeof (*t));
Matus Fabian694265d2016-08-10 01:55:36 -0700478
479 t->input_sa_index = isa;
480 t->output_sa_index = osa;
481 t->hw_if_index = ~0;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400482 hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
Matus Fabian694265d2016-08-10 01:55:36 -0700483 t - im->tunnel_interfaces);
484
485 /*1st interface, register protocol */
486 if (pool_elts (im->tunnel_interfaces) == 1)
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400487 {
488 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
489 ipsec4_if_input_node.index);
490 /* TBD, GRE IPSec6
491 *
492 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
493 ipsec6_if_input_node.index);
494 */
495 }
Matus Fabian694265d2016-08-10 01:55:36 -0700496 }
497 else
498 {
499 /* check if exists */
500 if (!p)
501 return VNET_API_ERROR_INVALID_VALUE;
502
503 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400504 hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
Matus Fabian694265d2016-08-10 01:55:36 -0700505 pool_put (im->tunnel_interfaces, t);
506 }
507 return 0;
508}
509
510int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700511ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
512 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700514 ipsec_main_t *im = &ipsec_main;
515 vnet_hw_interface_t *hi;
516 ipsec_tunnel_if_t *t;
517 ipsec_sa_t *sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
519 hi = vnet_get_hw_interface (vnm, hw_if_index);
520 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
521
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100522 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
523 return VNET_API_ERROR_SYSCALL_ERROR_1;
524
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
526 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700527 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100528 ipsec_sa_set_crypto_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800529 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 }
531 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
532 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700533 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100534 ipsec_sa_set_integ_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800535 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 }
537 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
538 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700539 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100540 ipsec_sa_set_crypto_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800541 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 }
543 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
544 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700545 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100546 ipsec_sa_set_integ_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800547 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 }
549 else
550 return VNET_API_ERROR_INVALID_VALUE;
551
552 return 0;
553}
554
555
Matthew Smithca514fd2017-10-12 12:06:59 -0500556int
557ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
558 u8 is_outbound)
559{
560 ipsec_main_t *im = &ipsec_main;
561 vnet_hw_interface_t *hi;
562 ipsec_tunnel_if_t *t;
563 ipsec_sa_t *sa, *old_sa;
564 u32 sa_index, old_sa_index;
565 uword *p;
566
567 hi = vnet_get_hw_interface (vnm, hw_if_index);
568 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
569
570 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
571 if (sa_index == ~0)
572 {
573 clib_warning ("SA with ID %u not found", sa_id);
574 return VNET_API_ERROR_INVALID_VALUE;
575 }
576
577 if (ipsec_is_sa_used (sa_index))
578 {
579 clib_warning ("SA with ID %u is already in use", sa_id);
580 return VNET_API_ERROR_INVALID_VALUE;
581 }
582
583 sa = pool_elt_at_index (im->sad, sa_index);
Matthew Smithca514fd2017-10-12 12:06:59 -0500584
585 if (!is_outbound)
586 {
Matthew Smithca514fd2017-10-12 12:06:59 -0500587 old_sa_index = t->input_sa_index;
588 old_sa = pool_elt_at_index (im->sad, old_sa_index);
589
Damjan Mariond709cbc2019-03-26 13:16:42 +0100590 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
591 ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400592 {
593 clib_warning ("IPsec interface SA endpoints type can't be changed");
594 return VNET_API_ERROR_INVALID_VALUE;
595 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500596
Damjan Mariond709cbc2019-03-26 13:16:42 +0100597 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400598 {
599 ipsec6_tunnel_key_t key;
600
601 /* unset old inbound hash entry. packets should stop arriving */
602 key.remote_ip = old_sa->tunnel_src_addr.ip6;
603 key.spi = clib_host_to_net_u32 (old_sa->spi);
604
605 p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key);
606 if (p)
607 hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key);
608
609 /* set new inbound SA, then set new hash entry */
610 t->input_sa_index = sa_index;
611 key.remote_ip = sa->tunnel_src_addr.ip6;
612 key.spi = clib_host_to_net_u32 (sa->spi);
613
614 hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key,
615 hi->dev_instance);
616 }
617 else
618 {
619 ipsec4_tunnel_key_t key;
620
621 /* unset old inbound hash entry. packets should stop arriving */
622 key.remote_ip = old_sa->tunnel_src_addr.ip4.as_u32;
623 key.spi = clib_host_to_net_u32 (old_sa->spi);
624
625 p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
626 if (p)
627 hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
628
629 /* set new inbound SA, then set new hash entry */
630 t->input_sa_index = sa_index;
631 key.remote_ip = sa->tunnel_src_addr.ip4.as_u32;
632 key.spi = clib_host_to_net_u32 (sa->spi);
633
634 hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
635 hi->dev_instance);
636 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500637 }
638 else
639 {
640 old_sa_index = t->output_sa_index;
641 old_sa = pool_elt_at_index (im->sad, old_sa_index);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400642
Damjan Mariond709cbc2019-03-26 13:16:42 +0100643 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
644 ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400645 {
646 clib_warning ("IPsec interface SA endpoints type can't be changed");
647 return VNET_API_ERROR_INVALID_VALUE;
648 }
649
Matthew Smithca514fd2017-10-12 12:06:59 -0500650 t->output_sa_index = sa_index;
651 }
652
653 /* remove sa_id to sa_index mapping on old SA */
654 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
655 hash_unset (im->sa_index_by_sa_id, old_sa->id);
656
Matthew Smith41b25cf2018-12-05 14:41:21 -0600657 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
Matthew Smithca514fd2017-10-12 12:06:59 -0500658 {
Klement Sekerab4d30532018-11-08 13:00:02 +0100659 clib_warning ("IPsec backend add/del callback returned error");
660 return VNET_API_ERROR_SYSCALL_ERROR_1;
Matthew Smithca514fd2017-10-12 12:06:59 -0500661 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500662 pool_put (im->sad, old_sa);
663
664 return 0;
665}
666
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400667
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668clib_error_t *
669ipsec_tunnel_if_init (vlib_main_t * vm)
670{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700671 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400673 /* initialize the ipsec-if ip4 hash */
674 im->ipsec4_if_pool_index_by_key =
675 hash_create (0, sizeof (ipsec4_tunnel_key_t));
676 /* initialize the ipsec-if ip6 hash */
677 im->ipsec6_if_pool_index_by_key = hash_create_mem (0,
678 sizeof
679 (ipsec6_tunnel_key_t),
680 sizeof (uword));
Matthew Smith8e1039a2018-04-12 07:32:56 -0500681 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400683 udp_register_dst_port (vm, UDP_DST_PORT_ipsec, ipsec4_if_input_node.index,
Kingwel Xie00bff192019-03-07 01:25:32 -0500684 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685 return 0;
686}
687
688VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
689
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700690
691/*
692 * fd.io coding-style-patch-verification: ON
693 *
694 * Local Variables:
695 * eval: (c-set-style "gnu")
696 * End:
697 */