blob: 33cac4c3cbd1ea8955725fe3ad21b08f1920c2f5 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000025#include <vnet/ipsec/esp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Matthew Smith2838a232016-06-21 16:05:09 -050027void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
28
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070029static u8 *
30format_ipsec_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
32 u32 dev_instance = va_arg (*args, u32);
Matthew Smith8e1039a2018-04-12 07:32:56 -050033 ipsec_main_t *im = &ipsec_main;
34 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
35
36 return format (s, "ipsec%d", t->show_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -070037}
38
Klement Sekera31da2e32018-06-24 22:49:55 +020039/* Statistics (not really errors) */
40#define foreach_ipsec_if_tx_error \
41_(TX, "good packets transmitted")
42
43static char *ipsec_if_tx_error_strings[] = {
44#define _(sym,string) string,
45 foreach_ipsec_if_tx_error
46#undef _
47};
48
49typedef enum
Klement Sekeraa98346f2018-05-16 10:52:45 +020050{
Klement Sekera31da2e32018-06-24 22:49:55 +020051#define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
52 foreach_ipsec_if_tx_error
53#undef _
54 IPSEC_IF_TX_N_ERROR,
55} ipsec_if_tx_error_t;
56
57typedef struct
58{
59 u32 spi;
60 u32 seq;
61} ipsec_if_tx_trace_t;
62
63u8 *
64format_ipsec_if_tx_trace (u8 * s, va_list * args)
65{
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 *);
69
70 s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
71 return s;
Klement Sekeraa98346f2018-05-16 10:52:45 +020072}
73
Neale Ranns77eb28f2019-03-04 14:13:14 +000074static void
75ipsec_output_trace (vlib_main_t * vm,
76 vlib_node_runtime_t * node,
77 vlib_frame_t * frame, const ipsec_tunnel_if_t * t0)
Neale Rannsfe480f62019-02-28 12:03:58 +000078{
79 ipsec_main_t *im = &ipsec_main;
Neale Ranns77eb28f2019-03-04 14:13:14 +000080 u32 *from, n_left;
Neale Rannsfe480f62019-02-28 12:03:58 +000081
Neale Ranns77eb28f2019-03-04 14:13:14 +000082 n_left = frame->n_vectors;
83 from = vlib_frame_vector_args (frame);
Neale Rannsfe480f62019-02-28 12:03:58 +000084
Neale Ranns77eb28f2019-03-04 14:13:14 +000085 while (n_left > 0)
86 {
87 vlib_buffer_t *b0;
88
89 b0 = vlib_get_buffer (vm, from[0]);
90
91 if (b0->flags & VLIB_BUFFER_IS_TRACED)
92 {
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);
96 tr->spi = sa0->spi;
97 tr->seq = sa0->seq;
98 }
99
100 from += 1;
101 n_left -= 1;
102 }
Neale Rannsfe480f62019-02-28 12:03:58 +0000103}
104
Neale Ranns77eb28f2019-03-04 14:13:14 +0000105VNET_DEVICE_CLASS_TX_FN (ipsec_device_class) (vlib_main_t * vm,
106 vlib_node_runtime_t * node,
107 vlib_frame_t * frame)
Klement Sekera31da2e32018-06-24 22:49:55 +0200108{
109 ipsec_main_t *im = &ipsec_main;
Neale Ranns77eb28f2019-03-04 14:13:14 +0000110 u32 *from, n_left;
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];
Klement Sekera31da2e32018-06-24 22:49:55 +0200115
Neale Ranns77eb28f2019-03-04 14:13:14 +0000116 from = vlib_frame_vector_args (frame);
117 t0 = pool_elt_at_index (im->tunnel_interfaces, rd->dev_instance);
118 n_left = frame->n_vectors;
119 b = bufs;
Klement Sekera31da2e32018-06-24 22:49:55 +0200120
Neale Ranns77eb28f2019-03-04 14:13:14 +0000121 /* All going to encrypt */
122 clib_memset (nexts, 0, sizeof (nexts));
123
124 if (node->flags & VLIB_NODE_FLAG_TRACE)
125 ipsec_output_trace (vm, node, frame, t0);
126
127 vlib_get_buffers (vm, from, bufs, n_left);
128
129 while (n_left >= 8)
Klement Sekera31da2e32018-06-24 22:49:55 +0200130 {
Neale Ranns77eb28f2019-03-04 14:13:14 +0000131 /* 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);
Klement Sekera31da2e32018-06-24 22:49:55 +0200136
Neale Ranns77eb28f2019-03-04 14:13:14 +0000137 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;
Klement Sekera31da2e32018-06-24 22:49:55 +0200141
Neale Ranns77eb28f2019-03-04 14:13:14 +0000142 n_left -= 4;
143 b += 4;
144 }
145 while (n_left > 0)
146 {
147 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
Klement Sekera31da2e32018-06-24 22:49:55 +0200148
Neale Ranns77eb28f2019-03-04 14:13:14 +0000149 n_left -= 1;
150 b += 1;
Klement Sekera31da2e32018-06-24 22:49:55 +0200151 }
152
Neale Ranns77eb28f2019-03-04 14:13:14 +0000153 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Klement Sekera31da2e32018-06-24 22:49:55 +0200154
Neale Ranns77eb28f2019-03-04 14:13:14 +0000155 return frame->n_vectors;
Klement Sekera31da2e32018-06-24 22:49:55 +0200156}
157
158
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000159static clib_error_t *
160ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
161{
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;
166 ipsec_sa_t *sa;
167
168 hi = vnet_get_hw_interface (vnm, hw_if_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100169 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
Neale Rannsfe480f62019-02-28 12:03:58 +0000170 t->flags = flags;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100171
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000172 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
173 {
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000174 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100175
Klement Sekerab4d30532018-11-08 13:00:02 +0100176 err = ipsec_check_support_cb (im, sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000177 if (err)
178 return err;
179
Klement Sekerab4d30532018-11-08 13:00:02 +0100180 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
181 if (err)
182 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100183
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000184 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100185
Klement Sekerab4d30532018-11-08 13:00:02 +0100186 err = ipsec_check_support_cb (im, sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000187 if (err)
188 return err;
189
Klement Sekerab4d30532018-11-08 13:00:02 +0100190 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
191 if (err)
192 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100193
Radu Nicolau3f903392017-01-30 14:33:39 +0000194 vnet_hw_interface_set_flags (vnm, hw_if_index,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000195 VNET_HW_INTERFACE_FLAG_LINK_UP);
196 }
197 else
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100198 {
199 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100200 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100201 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
202 if (err)
203 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100204 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100205 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
206 if (err)
207 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100208 }
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000209
210 return /* no error */ 0;
211}
212
Klement Sekera31da2e32018-06-24 22:49:55 +0200213
Neale Rannsb80c5362016-10-08 13:03:40 +0100214/* *INDENT-OFF* */
Neale Ranns77eb28f2019-03-04 14:13:14 +0000215VNET_DEVICE_CLASS (ipsec_device_class) =
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700216{
Neale Rannsb80c5362016-10-08 13:03:40 +0100217 .name = "IPSec",
218 .format_device_name = format_ipsec_name,
Klement Sekera31da2e32018-06-24 22:49:55 +0200219 .format_tx_trace = format_ipsec_if_tx_trace,
Klement Sekera31da2e32018-06-24 22:49:55 +0200220 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
221 .tx_function_error_strings = ipsec_if_tx_error_strings,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000222 .admin_up_down_function = ipsec_admin_up_down_function,
Neale Rannsb80c5362016-10-08 13:03:40 +0100223};
224/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Neale Rannsb80c5362016-10-08 13:03:40 +0100226/* *INDENT-OFF* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
228{
Neale Rannsb80c5362016-10-08 13:03:40 +0100229 .name = "IPSec",
230 .build_rewrite = default_build_rewrite,
Matthew Smith922549a2017-05-24 16:18:27 -0500231 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Neale Rannsb80c5362016-10-08 13:03:40 +0100232};
233/* *INDENT-ON* */
Matthew Smith2838a232016-06-21 16:05:09 -0500234
235static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700236ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
Matthew Smith2838a232016-06-21 16:05:09 -0500237{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700238 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion586afd72017-04-05 19:18:20 +0200239 ASSERT (vlib_get_thread_index () == 0);
Matthew Smith2838a232016-06-21 16:05:09 -0500240
Matthew Smithe04d09d2017-05-14 21:47:18 -0500241 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
Matthew Smith2838a232016-06-21 16:05:09 -0500242}
243
244int
245ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
246{
247 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 (u8 *) args, sizeof (*args));
Matthew Smith2838a232016-06-21 16:05:09 -0500249 return 0;
250}
251
Neale Ranns8d7c5022019-02-06 01:41:05 -0800252static u32
253ipsec_tun_mk_input_sa_id (u32 ti)
254{
255 return (0x80000000 | ti);
256}
257
258static u32
259ipsec_tun_mk_output_sa_id (u32 ti)
260{
261 return (0xc0000000 | ti);
262}
263
Matthew Smith2838a232016-06-21 16:05:09 -0500264int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700265ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
Matthew Smithe04d09d2017-05-14 21:47:18 -0500266 ipsec_add_del_tunnel_args_t * args,
267 u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269 ipsec_tunnel_if_t *t;
270 ipsec_main_t *im = &ipsec_main;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500271 vnet_hw_interface_t *hi = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 u32 hw_if_index = ~0;
273 uword *p;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500274 u32 dev_instance;
Klement Sekera31da2e32018-06-24 22:49:55 +0200275 u32 slot;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800276 ipsec_key_t crypto_key, integ_key;
277 ipsec_sa_flags_t flags;
278 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000280 u64 key = ((u64) args->remote_ip.ip4.as_u32 << 32 |
281 (u64) clib_host_to_net_u32 (args->remote_spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 p = hash_get (im->ipsec_if_pool_index_by_key, key);
283
284 if (args->is_add)
285 {
286 /* check if same src/dst pair exists */
287 if (p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700288 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289
Neale Rannsfe480f62019-02-28 12:03:58 +0000290 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Matthew Smith8e1039a2018-04-12 07:32:56 -0500292 dev_instance = t - im->tunnel_interfaces;
293 if (args->renumber)
294 t->show_instance = args->show_instance;
295 else
296 t->show_instance = dev_instance;
297
298 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
299 {
300 pool_put (im->tunnel_interfaces, t);
301 return VNET_API_ERROR_INSTANCE_IN_USE;
302 }
303
304 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
305 dev_instance);
306
Neale Ranns8d7c5022019-02-06 01:41:05 -0800307 flags = IPSEC_SA_FLAG_IS_TUNNEL;
308 if (args->udp_encap)
309 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
310 if (args->esn)
311 flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM;
312 if (args->anti_replay)
313 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Neale Ranns8d7c5022019-02-06 01:41:05 -0800315 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);
319
320 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
321 args->remote_spi,
322 IPSEC_PROTOCOL_ESP,
323 args->crypto_alg,
324 &crypto_key,
325 args->integ_alg,
326 &integ_key,
327 flags,
328 args->tx_table_id,
329 &args->remote_ip,
330 &args->local_ip, &t->input_sa_index);
331
332 if (rv)
333 return VNET_API_ERROR_UNIMPLEMENTED;
334
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);
339
340 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
341 args->local_spi,
342 IPSEC_PROTOCOL_ESP,
343 args->crypto_alg,
344 &crypto_key,
345 args->integ_alg,
346 &integ_key,
347 flags,
348 args->tx_table_id,
349 &args->local_ip,
350 &args->remote_ip, &t->output_sa_index);
351
352 if (rv)
353 return VNET_API_ERROR_UNIMPLEMENTED;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700355 hash_set (im->ipsec_if_pool_index_by_key, key,
356 t - im->tunnel_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
Matthew Smith8e1039a2018-04-12 07:32:56 -0500358 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);
Matthew Smithe04d09d2017-05-14 21:47:18 -0500362
363 hi = vnet_get_hw_interface (vnm, hw_if_index);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800364 /* add esp4 as the next-node-index of this tx-node */
Klement Sekera31da2e32018-06-24 22:49:55 +0200365
Matthew Smith0e36bbf2018-07-05 14:45:58 -0500366 slot = vlib_node_add_next_with_slot
Kingwel Xiec69ac312019-02-04 01:49:29 -0800367 (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, 0);
Klement Sekera31da2e32018-06-24 22:49:55 +0200368
Kingwel Xiec69ac312019-02-04 01:49:29 -0800369 ASSERT (slot == 0);
Klement Sekera31da2e32018-06-24 22:49:55 +0200370
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 t->hw_if_index = hw_if_index;
Neale Rannsfe480f62019-02-28 12:03:58 +0000372 t->sw_if_index = hi->sw_if_index;
373
Matthew Smith537eeec2018-04-09 11:49:20 -0500374 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
375 hi->sw_if_index, 1, 0, 0);
376
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 /*1st interface, register protocol */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700378 if (pool_elts (im->tunnel_interfaces) == 1)
379 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
380 ipsec_if_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 }
383 else
384 {
385 /* check if exists */
386 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700387 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700389 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700391 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
Matthew Smith537eeec2018-04-09 11:49:20 -0500392
393 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
394 hi->sw_if_index, 0, 0, 0);
395
Matthew Smith8e1039a2018-04-12 07:32:56 -0500396 vnet_delete_hw_interface (vnm, t->hw_if_index);
Matthew Smith01034be2017-05-16 11:51:18 -0500397
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 hash_unset (im->ipsec_if_pool_index_by_key, key);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500399 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
400
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 pool_put (im->tunnel_interfaces, t);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800402
403 /* delete input and output SA */
404 ipsec_sa_del (ipsec_tun_mk_input_sa_id (p[0]));
405 ipsec_sa_del (ipsec_tun_mk_output_sa_id (p[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 }
Matthew Smithe04d09d2017-05-14 21:47:18 -0500407
408 if (sw_if_index)
409 *sw_if_index = hi->sw_if_index;
410
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 return 0;
412}
413
414int
Matus Fabian694265d2016-08-10 01:55:36 -0700415ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
416 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
417{
418 ipsec_tunnel_if_t *t = 0;
419 ipsec_main_t *im = &ipsec_main;
420 uword *p;
421 ipsec_sa_t *sa;
422 u64 key;
423 u32 isa, osa;
424
425 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
426 if (!p)
427 return VNET_API_ERROR_INVALID_VALUE;
428 isa = p[0];
429
430 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
431 if (!p)
432 return VNET_API_ERROR_INVALID_VALUE;
433 osa = p[0];
434 sa = pool_elt_at_index (im->sad, p[0]);
435
436 if (sa->is_tunnel)
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000437 key = ((u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 |
438 (u64) clib_host_to_net_u32 (sa->spi));
Matus Fabian694265d2016-08-10 01:55:36 -0700439 else
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000440 key = ((u64) args->remote_ip.as_u32 << 32 |
441 (u64) clib_host_to_net_u32 (sa->spi));
Matus Fabian694265d2016-08-10 01:55:36 -0700442
443 p = hash_get (im->ipsec_if_pool_index_by_key, key);
444
445 if (args->is_add)
446 {
447 /* check if same src/dst pair exists */
448 if (p)
449 return VNET_API_ERROR_INVALID_VALUE;
450
451 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400452 clib_memset (t, 0, sizeof (*t));
Matus Fabian694265d2016-08-10 01:55:36 -0700453
454 t->input_sa_index = isa;
455 t->output_sa_index = osa;
456 t->hw_if_index = ~0;
457 hash_set (im->ipsec_if_pool_index_by_key, key,
458 t - im->tunnel_interfaces);
459
460 /*1st interface, register protocol */
461 if (pool_elts (im->tunnel_interfaces) == 1)
462 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
463 ipsec_if_input_node.index);
464 }
465 else
466 {
467 /* check if exists */
468 if (!p)
469 return VNET_API_ERROR_INVALID_VALUE;
470
471 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
472 hash_unset (im->ipsec_if_pool_index_by_key, key);
473 pool_put (im->tunnel_interfaces, t);
474 }
475 return 0;
476}
477
478int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700479ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
480 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700482 ipsec_main_t *im = &ipsec_main;
483 vnet_hw_interface_t *hi;
484 ipsec_tunnel_if_t *t;
485 ipsec_sa_t *sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
487 hi = vnet_get_hw_interface (vnm, hw_if_index);
488 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
489
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100490 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
491 return VNET_API_ERROR_SYSCALL_ERROR_1;
492
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
494 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700495 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100496 ipsec_sa_set_crypto_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800497 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 }
499 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
500 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700501 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100502 ipsec_sa_set_integ_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800503 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 }
505 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
506 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700507 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100508 ipsec_sa_set_crypto_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800509 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 }
511 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
512 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700513 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100514 ipsec_sa_set_integ_alg (sa, alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800515 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 }
517 else
518 return VNET_API_ERROR_INVALID_VALUE;
519
520 return 0;
521}
522
523
Matthew Smithca514fd2017-10-12 12:06:59 -0500524int
525ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
526 u8 is_outbound)
527{
528 ipsec_main_t *im = &ipsec_main;
529 vnet_hw_interface_t *hi;
530 ipsec_tunnel_if_t *t;
531 ipsec_sa_t *sa, *old_sa;
532 u32 sa_index, old_sa_index;
533 uword *p;
534
535 hi = vnet_get_hw_interface (vnm, hw_if_index);
536 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
537
538 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
539 if (sa_index == ~0)
540 {
541 clib_warning ("SA with ID %u not found", sa_id);
542 return VNET_API_ERROR_INVALID_VALUE;
543 }
544
545 if (ipsec_is_sa_used (sa_index))
546 {
547 clib_warning ("SA with ID %u is already in use", sa_id);
548 return VNET_API_ERROR_INVALID_VALUE;
549 }
550
551 sa = pool_elt_at_index (im->sad, sa_index);
552 if (sa->is_tunnel_ip6)
553 {
554 clib_warning ("IPsec interface not supported with IPv6 endpoints");
555 return VNET_API_ERROR_UNIMPLEMENTED;
556 }
557
558 if (!is_outbound)
559 {
560 u64 key;
561
562 old_sa_index = t->input_sa_index;
563 old_sa = pool_elt_at_index (im->sad, old_sa_index);
564
565 /* unset old inbound hash entry. packets should stop arriving */
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000566 key = ((u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 |
567 (u64) clib_host_to_net_u32 (old_sa->spi));
Matthew Smithca514fd2017-10-12 12:06:59 -0500568 p = hash_get (im->ipsec_if_pool_index_by_key, key);
569 if (p)
570 hash_unset (im->ipsec_if_pool_index_by_key, key);
571
572 /* set new inbound SA, then set new hash entry */
573 t->input_sa_index = sa_index;
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000574 key = ((u64) sa->tunnel_src_addr.ip4.as_u32 << 32 |
575 (u64) clib_host_to_net_u32 (sa->spi));
Matthew Smithca514fd2017-10-12 12:06:59 -0500576 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
577 }
578 else
579 {
580 old_sa_index = t->output_sa_index;
581 old_sa = pool_elt_at_index (im->sad, old_sa_index);
582 t->output_sa_index = sa_index;
583 }
584
585 /* remove sa_id to sa_index mapping on old SA */
586 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
587 hash_unset (im->sa_index_by_sa_id, old_sa->id);
588
Matthew Smith41b25cf2018-12-05 14:41:21 -0600589 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
Matthew Smithca514fd2017-10-12 12:06:59 -0500590 {
Klement Sekerab4d30532018-11-08 13:00:02 +0100591 clib_warning ("IPsec backend add/del callback returned error");
592 return VNET_API_ERROR_SYSCALL_ERROR_1;
Matthew Smithca514fd2017-10-12 12:06:59 -0500593 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500594 pool_put (im->sad, old_sa);
595
596 return 0;
597}
598
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599clib_error_t *
600ipsec_tunnel_if_init (vlib_main_t * vm)
601{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700602 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
Matthew Smith8e1039a2018-04-12 07:32:56 -0500605 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Kingwel Xie00bff192019-03-07 01:25:32 -0500607 udp_register_dst_port (vm, UDP_DST_PORT_ipsec, ipsec_if_input_node.index,
608 1);
609
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 return 0;
611}
612
613VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
614
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700615
616/*
617 * fd.io coding-style-patch-verification: ON
618 *
619 * Local Variables:
620 * eval: (c-set-style "gnu")
621 * End:
622 */