blob: a7372747797501b4eff8835c78c1459e4c3d75fb [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/**
Neale Rannsc87b66c2019-02-07 07:26:12 -080087 * @brief Call back when restacking all adjacencies on a IPSec interface
Neale Ranns25edf142019-03-22 08:12:48 +000088 */
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 /*
Neale Rannsc87b66c2019-02-07 07:26:12 -0800103 * walk all the adjacencies on the IPSec interface and restack them
Neale Ranns25edf142019-03-22 08:12:48 +0000104 */
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
Neale Rannsd6c9e822019-04-17 16:29:00 -0700233static void
Neale Rannse8915fc2019-04-23 20:57:55 -0400234ipsec_tunnel_feature_set (ipsec_main_t * im, ipsec_tunnel_if_t * t, u8 enable)
Neale Rannsd6c9e822019-04-17 16:29:00 -0700235{
Neale Rannse8915fc2019-04-23 20:57:55 -0400236 u8 arc;
Matthew Smith401aedf2019-07-08 14:45:04 -0500237 u32 esp4_feature_index, esp6_feature_index;
238 ipsec_sa_t *sa;
239
240 sa = ipsec_sa_get (t->output_sa_index);
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000241 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
242 sa->integ_alg == IPSEC_INTEG_ALG_NONE)
Matthew Smith401aedf2019-07-08 14:45:04 -0500243 {
244 esp4_feature_index = im->esp4_no_crypto_tun_feature_index;
245 esp6_feature_index = im->esp6_no_crypto_tun_feature_index;
246 }
247 else
248 {
249 esp4_feature_index = im->esp4_encrypt_tun_feature_index;
250 esp6_feature_index = im->esp6_encrypt_tun_feature_index;
251 }
Neale Rannse8915fc2019-04-23 20:57:55 -0400252
253 arc = vnet_get_feature_arc_index ("ip4-output");
254
Matthew Smith401aedf2019-07-08 14:45:04 -0500255 vnet_feature_enable_disable_with_index (arc, esp4_feature_index,
Neale Rannse8915fc2019-04-23 20:57:55 -0400256 t->sw_if_index, enable,
257 &t->output_sa_index,
258 sizeof (t->output_sa_index));
259
260 arc = vnet_get_feature_arc_index ("ip6-output");
261
Matthew Smith401aedf2019-07-08 14:45:04 -0500262 vnet_feature_enable_disable_with_index (arc, esp6_feature_index,
Neale Rannse8915fc2019-04-23 20:57:55 -0400263 t->sw_if_index, enable,
264 &t->output_sa_index,
265 sizeof (t->output_sa_index));
Neale Rannsd6c9e822019-04-17 16:29:00 -0700266}
267
Matthew Smith2838a232016-06-21 16:05:09 -0500268int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
Matthew Smithe04d09d2017-05-14 21:47:18 -0500270 ipsec_add_del_tunnel_args_t * args,
Benoît Ganne8133c782019-09-09 16:34:54 +0200271 u32 * sw_if_index_p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700273 ipsec_tunnel_if_t *t;
274 ipsec_main_t *im = &ipsec_main;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500275 vnet_hw_interface_t *hi = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 u32 hw_if_index = ~0;
Benoît Ganne8133c782019-09-09 16:34:54 +0200277 u32 sw_if_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 uword *p;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500279 u32 dev_instance;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800280 ipsec_key_t crypto_key, integ_key;
281 ipsec_sa_flags_t flags;
282 int rv;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400283 int is_ip6 = args->is_ip6;
284 ipsec4_tunnel_key_t key4;
285 ipsec6_tunnel_key_t key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400287 if (!is_ip6)
288 {
Neale Ranns41afb332019-07-16 06:19:35 -0700289 key4.remote_ip.as_u32 = args->remote_ip.ip4.as_u32;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400290 key4.spi = clib_host_to_net_u32 (args->remote_spi);
291 p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64);
292 }
293 else
294 {
295 key6.remote_ip = args->remote_ip.ip6;
296 key6.spi = clib_host_to_net_u32 (args->remote_spi);
297 p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6);
298 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
300 if (args->is_add)
301 {
302 /* check if same src/dst pair exists */
303 if (p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700304 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Neale Rannsfe480f62019-02-28 12:03:58 +0000306 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Matthew Smith8e1039a2018-04-12 07:32:56 -0500308 dev_instance = t - im->tunnel_interfaces;
309 if (args->renumber)
310 t->show_instance = args->show_instance;
311 else
312 t->show_instance = dev_instance;
313
314 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
315 {
316 pool_put (im->tunnel_interfaces, t);
317 return VNET_API_ERROR_INSTANCE_IN_USE;
318 }
319
320 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
321 dev_instance);
322
Neale Ranns8d7c5022019-02-06 01:41:05 -0800323 flags = IPSEC_SA_FLAG_IS_TUNNEL;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400324 if (args->is_ip6)
325 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800326 if (args->udp_encap)
327 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
328 if (args->esn)
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100329 flags |= IPSEC_SA_FLAG_USE_ESN;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800330 if (args->anti_replay)
331 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Neale Ranns8d7c5022019-02-06 01:41:05 -0800333 ipsec_mk_key (&crypto_key,
334 args->remote_crypto_key, args->remote_crypto_key_len);
335 ipsec_mk_key (&integ_key,
336 args->remote_integ_key, args->remote_integ_key_len);
337
Neale Ranns495d7ff2019-07-12 09:15:26 +0000338 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (dev_instance),
339 args->remote_spi,
340 IPSEC_PROTOCOL_ESP,
341 args->crypto_alg,
342 &crypto_key,
343 args->integ_alg,
344 &integ_key,
345 (flags | IPSEC_SA_FLAG_IS_INBOUND),
346 args->tx_table_id,
347 args->salt,
348 &args->remote_ip,
349 &args->local_ip, &t->input_sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800350
351 if (rv)
Neale Ranns2f052202019-06-14 10:31:46 +0000352 return rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800353
354 ipsec_mk_key (&crypto_key,
355 args->local_crypto_key, args->local_crypto_key_len);
356 ipsec_mk_key (&integ_key,
357 args->local_integ_key, args->local_integ_key_len);
358
Neale Ranns495d7ff2019-07-12 09:15:26 +0000359 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (dev_instance),
360 args->local_spi,
361 IPSEC_PROTOCOL_ESP,
362 args->crypto_alg,
363 &crypto_key,
364 args->integ_alg,
365 &integ_key,
366 flags,
367 args->tx_table_id,
368 args->salt,
369 &args->local_ip,
370 &args->remote_ip, &t->output_sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800371
372 if (rv)
Neale Ranns2f052202019-06-14 10:31:46 +0000373 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400375 /* copy the key */
376 if (is_ip6)
377 hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6,
378 t - im->tunnel_interfaces);
379 else
Neale Rannsf41110d2019-07-19 00:59:45 -0700380 hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64,
381 t - im->tunnel_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
Matthew Smith8e1039a2018-04-12 07:32:56 -0500383 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
384 t - im->tunnel_interfaces,
385 ipsec_hw_class.index,
386 t - im->tunnel_interfaces);
Matthew Smithe04d09d2017-05-14 21:47:18 -0500387
388 hi = vnet_get_hw_interface (vnm, hw_if_index);
Benoît Ganne8133c782019-09-09 16:34:54 +0200389 sw_if_index = hi->sw_if_index;
Klement Sekera31da2e32018-06-24 22:49:55 +0200390
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 t->hw_if_index = hw_if_index;
Neale Rannsfe480f62019-02-28 12:03:58 +0000392 t->sw_if_index = hi->sw_if_index;
393
Neale Rannsd7603d92019-03-28 08:56:10 +0000394 /* Standard default jumbo MTU. */
395 vnet_sw_interface_set_mtu (vnm, t->sw_if_index, 9000);
396
Neale Ranns25edf142019-03-22 08:12:48 +0000397 /* Add the new tunnel to the DB of tunnels per sw_if_index ... */
398 vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index,
399 ~0);
400 im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance;
401
Neale Rannse8915fc2019-04-23 20:57:55 -0400402 ipsec_tunnel_feature_set (im, t, 1);
Neale Ranns25edf142019-03-22 08:12:48 +0000403
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 /*1st interface, register protocol */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700405 if (pool_elts (im->tunnel_interfaces) == 1)
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400406 {
407 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
408 ipsec4_if_input_node.index);
409 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
410 ipsec6_if_input_node.index);
411 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 }
414 else
415 {
Neale Rannsc80cc9a2019-03-20 14:10:23 +0000416 u32 ti;
417
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 /* check if exists */
419 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
Neale Rannsc80cc9a2019-03-20 14:10:23 +0000422 ti = p[0];
423 t = pool_elt_at_index (im->tunnel_interfaces, ti);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
Benoît Ganne8133c782019-09-09 16:34:54 +0200425 sw_if_index = hi->sw_if_index;
426
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700427 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
Matthew Smith537eeec2018-04-09 11:49:20 -0500428
Neale Rannse8915fc2019-04-23 20:57:55 -0400429 ipsec_tunnel_feature_set (im, t, 0);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500430 vnet_delete_hw_interface (vnm, t->hw_if_index);
Matthew Smith01034be2017-05-16 11:51:18 -0500431
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400432 if (is_ip6)
433 hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6);
434 else
Neale Rannsf41110d2019-07-19 00:59:45 -0700435 hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500436 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
Neale Ranns41afb332019-07-16 06:19:35 -0700437
Neale Ranns25edf142019-03-22 08:12:48 +0000438 im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500439
Neale Ranns8d7c5022019-02-06 01:41:05 -0800440 /* delete input and output SA */
Neale Ranns495d7ff2019-07-12 09:15:26 +0000441 ipsec_sa_unlock (t->input_sa_index);
442 ipsec_sa_unlock (t->output_sa_index);
443
444 pool_put (im->tunnel_interfaces, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 }
Matthew Smithe04d09d2017-05-14 21:47:18 -0500446
Benoît Ganne8133c782019-09-09 16:34:54 +0200447 if (sw_if_index_p)
448 *sw_if_index_p = sw_if_index;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500449
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 return 0;
451}
452
453int
Matthew Smithca514fd2017-10-12 12:06:59 -0500454ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
455 u8 is_outbound)
456{
457 ipsec_main_t *im = &ipsec_main;
458 vnet_hw_interface_t *hi;
459 ipsec_tunnel_if_t *t;
460 ipsec_sa_t *sa, *old_sa;
461 u32 sa_index, old_sa_index;
462 uword *p;
463
464 hi = vnet_get_hw_interface (vnm, hw_if_index);
465 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
466
Neale Ranns495d7ff2019-07-12 09:15:26 +0000467 sa_index = ipsec_sa_find_and_lock (sa_id);
468
469 if (INDEX_INVALID == sa_index)
Matthew Smithca514fd2017-10-12 12:06:59 -0500470 {
471 clib_warning ("SA with ID %u not found", sa_id);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000472 return VNET_API_ERROR_NO_SUCH_ENTRY;
Matthew Smithca514fd2017-10-12 12:06:59 -0500473 }
474
475 sa = pool_elt_at_index (im->sad, sa_index);
Matthew Smithca514fd2017-10-12 12:06:59 -0500476
477 if (!is_outbound)
478 {
Matthew Smithca514fd2017-10-12 12:06:59 -0500479 old_sa_index = t->input_sa_index;
480 old_sa = pool_elt_at_index (im->sad, old_sa_index);
481
Damjan Mariond709cbc2019-03-26 13:16:42 +0100482 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
483 ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400484 {
485 clib_warning ("IPsec interface SA endpoints type can't be changed");
486 return VNET_API_ERROR_INVALID_VALUE;
487 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500488
Damjan Mariond709cbc2019-03-26 13:16:42 +0100489 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400490 {
491 ipsec6_tunnel_key_t key;
492
493 /* unset old inbound hash entry. packets should stop arriving */
494 key.remote_ip = old_sa->tunnel_src_addr.ip6;
495 key.spi = clib_host_to_net_u32 (old_sa->spi);
496
497 p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key);
498 if (p)
499 hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key);
500
501 /* set new inbound SA, then set new hash entry */
502 t->input_sa_index = sa_index;
503 key.remote_ip = sa->tunnel_src_addr.ip6;
504 key.spi = clib_host_to_net_u32 (sa->spi);
505
506 hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key,
507 hi->dev_instance);
508 }
509 else
510 {
511 ipsec4_tunnel_key_t key;
512
513 /* unset old inbound hash entry. packets should stop arriving */
Neale Ranns41afb332019-07-16 06:19:35 -0700514 key.remote_ip.as_u32 = old_sa->tunnel_src_addr.ip4.as_u32;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400515 key.spi = clib_host_to_net_u32 (old_sa->spi);
516
517 p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
518 if (p)
519 hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
520
521 /* set new inbound SA, then set new hash entry */
522 t->input_sa_index = sa_index;
Neale Ranns41afb332019-07-16 06:19:35 -0700523 key.remote_ip.as_u32 = sa->tunnel_src_addr.ip4.as_u32;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400524 key.spi = clib_host_to_net_u32 (sa->spi);
525
526 hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
527 hi->dev_instance);
528 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500529 }
530 else
531 {
532 old_sa_index = t->output_sa_index;
533 old_sa = pool_elt_at_index (im->sad, old_sa_index);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400534
Damjan Mariond709cbc2019-03-26 13:16:42 +0100535 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
536 ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400537 {
538 clib_warning ("IPsec interface SA endpoints type can't be changed");
539 return VNET_API_ERROR_INVALID_VALUE;
540 }
541
Neale Rannsd6c9e822019-04-17 16:29:00 -0700542 /*
543 * re-enable the feature to get the new SA in
544 * the workers are stopped so no packets are sent in the clear
545 */
Neale Rannse8915fc2019-04-23 20:57:55 -0400546 ipsec_tunnel_feature_set (im, t, 0);
Matthew Smithca514fd2017-10-12 12:06:59 -0500547 t->output_sa_index = sa_index;
Neale Rannse8915fc2019-04-23 20:57:55 -0400548 ipsec_tunnel_feature_set (im, t, 1);
Matthew Smithca514fd2017-10-12 12:06:59 -0500549 }
550
551 /* remove sa_id to sa_index mapping on old SA */
Neale Ranns495d7ff2019-07-12 09:15:26 +0000552 hash_unset (im->sa_index_by_sa_id, old_sa->id);
Matthew Smithca514fd2017-10-12 12:06:59 -0500553
Matthew Smith41b25cf2018-12-05 14:41:21 -0600554 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
Matthew Smithca514fd2017-10-12 12:06:59 -0500555 {
Klement Sekerab4d30532018-11-08 13:00:02 +0100556 clib_warning ("IPsec backend add/del callback returned error");
557 return VNET_API_ERROR_SYSCALL_ERROR_1;
Matthew Smithca514fd2017-10-12 12:06:59 -0500558 }
Neale Ranns495d7ff2019-07-12 09:15:26 +0000559
560 ipsec_sa_unlock (old_sa_index);
Matthew Smithca514fd2017-10-12 12:06:59 -0500561
562 return 0;
563}
564
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565clib_error_t *
566ipsec_tunnel_if_init (vlib_main_t * vm)
567{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700568 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400570 /* initialize the ipsec-if ip4 hash */
571 im->ipsec4_if_pool_index_by_key =
572 hash_create (0, sizeof (ipsec4_tunnel_key_t));
573 /* initialize the ipsec-if ip6 hash */
574 im->ipsec6_if_pool_index_by_key = hash_create_mem (0,
575 sizeof
576 (ipsec6_tunnel_key_t),
577 sizeof (uword));
Matthew Smith8e1039a2018-04-12 07:32:56 -0500578 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Matthew Smith401aedf2019-07-08 14:45:04 -0500580 /* set up feature nodes to drop outbound packets with no crypto alg set */
581 ipsec_add_feature ("ip4-output", "esp4-no-crypto",
582 &im->esp4_no_crypto_tun_feature_index);
583 ipsec_add_feature ("ip6-output", "esp6-no-crypto",
584 &im->esp6_no_crypto_tun_feature_index);
585
Neale Rannsf41110d2019-07-19 00:59:45 -0700586 udp_register_dst_port (vlib_get_main (),
587 UDP_DST_PORT_ipsec, ipsec4_if_input_node.index, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588 return 0;
589}
590
591VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
592
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700593
594/*
595 * fd.io coding-style-patch-verification: ON
596 *
597 * Local Variables:
598 * eval: (c-set-style "gnu")
599 * End:
600 */