blob: 5150e95d6de7ac749f26a8a8ad3dc91704cdae35 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
23#include <vnet/ipsec/ipsec.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000024#include <vnet/ipsec/esp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
Matthew Smith2838a232016-06-21 16:05:09 -050026void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
27
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070028static u8 *
29format_ipsec_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070030{
31 u32 dev_instance = va_arg (*args, u32);
Matthew Smith8e1039a2018-04-12 07:32:56 -050032 ipsec_main_t *im = &ipsec_main;
33 ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
34
35 return format (s, "ipsec%d", t->show_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -070036}
37
Klement Sekera31da2e32018-06-24 22:49:55 +020038/* Statistics (not really errors) */
39#define foreach_ipsec_if_tx_error \
40_(TX, "good packets transmitted")
41
42static char *ipsec_if_tx_error_strings[] = {
43#define _(sym,string) string,
44 foreach_ipsec_if_tx_error
45#undef _
46};
47
48typedef enum
Klement Sekeraa98346f2018-05-16 10:52:45 +020049{
Klement Sekera31da2e32018-06-24 22:49:55 +020050#define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
51 foreach_ipsec_if_tx_error
52#undef _
53 IPSEC_IF_TX_N_ERROR,
54} ipsec_if_tx_error_t;
55
56typedef struct
57{
58 u32 spi;
59 u32 seq;
60} ipsec_if_tx_trace_t;
61
62u8 *
63format_ipsec_if_tx_trace (u8 * s, va_list * args)
64{
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 ipsec_if_tx_trace_t *t = va_arg (*args, ipsec_if_tx_trace_t *);
68
69 s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
70 return s;
Klement Sekeraa98346f2018-05-16 10:52:45 +020071}
72
Neale Ranns77eb28f2019-03-04 14:13:14 +000073static void
74ipsec_output_trace (vlib_main_t * vm,
75 vlib_node_runtime_t * node,
76 vlib_frame_t * frame, const ipsec_tunnel_if_t * t0)
Neale Rannsfe480f62019-02-28 12:03:58 +000077{
78 ipsec_main_t *im = &ipsec_main;
Neale Ranns77eb28f2019-03-04 14:13:14 +000079 u32 *from, n_left;
Neale Rannsfe480f62019-02-28 12:03:58 +000080
Neale Ranns77eb28f2019-03-04 14:13:14 +000081 n_left = frame->n_vectors;
82 from = vlib_frame_vector_args (frame);
Neale Rannsfe480f62019-02-28 12:03:58 +000083
Neale Ranns77eb28f2019-03-04 14:13:14 +000084 while (n_left > 0)
85 {
86 vlib_buffer_t *b0;
87
88 b0 = vlib_get_buffer (vm, from[0]);
89
90 if (b0->flags & VLIB_BUFFER_IS_TRACED)
91 {
92 ipsec_if_tx_trace_t *tr =
93 vlib_add_trace (vm, node, b0, sizeof (*tr));
94 ipsec_sa_t *sa0 = pool_elt_at_index (im->sad, t0->output_sa_index);
95 tr->spi = sa0->spi;
96 tr->seq = sa0->seq;
97 }
98
99 from += 1;
100 n_left -= 1;
101 }
Neale Rannsfe480f62019-02-28 12:03:58 +0000102}
103
Neale Ranns77eb28f2019-03-04 14:13:14 +0000104VNET_DEVICE_CLASS_TX_FN (ipsec_device_class) (vlib_main_t * vm,
105 vlib_node_runtime_t * node,
106 vlib_frame_t * frame)
Klement Sekera31da2e32018-06-24 22:49:55 +0200107{
108 ipsec_main_t *im = &ipsec_main;
Neale Ranns77eb28f2019-03-04 14:13:14 +0000109 u32 *from, n_left;
110 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
111 const ipsec_tunnel_if_t *t0;
112 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
113 u16 nexts[VLIB_FRAME_SIZE];
Klement Sekera31da2e32018-06-24 22:49:55 +0200114
Neale Ranns77eb28f2019-03-04 14:13:14 +0000115 from = vlib_frame_vector_args (frame);
116 t0 = pool_elt_at_index (im->tunnel_interfaces, rd->dev_instance);
117 n_left = frame->n_vectors;
118 b = bufs;
Klement Sekera31da2e32018-06-24 22:49:55 +0200119
Neale Ranns77eb28f2019-03-04 14:13:14 +0000120 /* All going to encrypt */
121 clib_memset (nexts, 0, sizeof (nexts));
122
123 if (node->flags & VLIB_NODE_FLAG_TRACE)
124 ipsec_output_trace (vm, node, frame, t0);
125
126 vlib_get_buffers (vm, from, bufs, n_left);
127
128 while (n_left >= 8)
Klement Sekera31da2e32018-06-24 22:49:55 +0200129 {
Neale Ranns77eb28f2019-03-04 14:13:14 +0000130 /* Prefetch the buffer header for the N+2 loop iteration */
131 vlib_prefetch_buffer_header (b[4], STORE);
132 vlib_prefetch_buffer_header (b[5], STORE);
133 vlib_prefetch_buffer_header (b[6], STORE);
134 vlib_prefetch_buffer_header (b[7], STORE);
Klement Sekera31da2e32018-06-24 22:49:55 +0200135
Neale Ranns77eb28f2019-03-04 14:13:14 +0000136 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
137 vnet_buffer (b[1])->ipsec.sad_index = t0->output_sa_index;
138 vnet_buffer (b[2])->ipsec.sad_index = t0->output_sa_index;
139 vnet_buffer (b[3])->ipsec.sad_index = t0->output_sa_index;
Klement Sekera31da2e32018-06-24 22:49:55 +0200140
Neale Ranns77eb28f2019-03-04 14:13:14 +0000141 n_left -= 4;
142 b += 4;
143 }
144 while (n_left > 0)
145 {
146 vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
Klement Sekera31da2e32018-06-24 22:49:55 +0200147
Neale Ranns77eb28f2019-03-04 14:13:14 +0000148 n_left -= 1;
149 b += 1;
Klement Sekera31da2e32018-06-24 22:49:55 +0200150 }
151
Neale Ranns77eb28f2019-03-04 14:13:14 +0000152 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Klement Sekera31da2e32018-06-24 22:49:55 +0200153
Neale Ranns77eb28f2019-03-04 14:13:14 +0000154 return frame->n_vectors;
Klement Sekera31da2e32018-06-24 22:49:55 +0200155}
156
157
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000158static clib_error_t *
159ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
160{
161 ipsec_main_t *im = &ipsec_main;
162 clib_error_t *err = 0;
163 ipsec_tunnel_if_t *t;
164 vnet_hw_interface_t *hi;
165 ipsec_sa_t *sa;
166
167 hi = vnet_get_hw_interface (vnm, hw_if_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100168 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
Neale Rannsfe480f62019-02-28 12:03:58 +0000169 t->flags = flags;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100170
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000171 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
172 {
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000173 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100174
Klement Sekerab4d30532018-11-08 13:00:02 +0100175 err = ipsec_check_support_cb (im, sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000176 if (err)
177 return err;
178
Klement Sekerab4d30532018-11-08 13:00:02 +0100179 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
180 if (err)
181 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100182
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000183 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100184
Klement Sekerab4d30532018-11-08 13:00:02 +0100185 err = ipsec_check_support_cb (im, sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000186 if (err)
187 return err;
188
Klement Sekerab4d30532018-11-08 13:00:02 +0100189 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
190 if (err)
191 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100192
Radu Nicolau3f903392017-01-30 14:33:39 +0000193 vnet_hw_interface_set_flags (vnm, hw_if_index,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000194 VNET_HW_INTERFACE_FLAG_LINK_UP);
195 }
196 else
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100197 {
198 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100199 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100200 err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
201 if (err)
202 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100203 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100204 err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
205 if (err)
206 return err;
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100207 }
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000208
209 return /* no error */ 0;
210}
211
Klement Sekera31da2e32018-06-24 22:49:55 +0200212
Neale Rannsb80c5362016-10-08 13:03:40 +0100213/* *INDENT-OFF* */
Neale Ranns77eb28f2019-03-04 14:13:14 +0000214VNET_DEVICE_CLASS (ipsec_device_class) =
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215{
Neale Rannsb80c5362016-10-08 13:03:40 +0100216 .name = "IPSec",
217 .format_device_name = format_ipsec_name,
Klement Sekera31da2e32018-06-24 22:49:55 +0200218 .format_tx_trace = format_ipsec_if_tx_trace,
Klement Sekera31da2e32018-06-24 22:49:55 +0200219 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
220 .tx_function_error_strings = ipsec_if_tx_error_strings,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000221 .admin_up_down_function = ipsec_admin_up_down_function,
Neale Rannsb80c5362016-10-08 13:03:40 +0100222};
223/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
Neale Rannsb80c5362016-10-08 13:03:40 +0100225/* *INDENT-OFF* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700226VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
227{
Neale Rannsb80c5362016-10-08 13:03:40 +0100228 .name = "IPSec",
229 .build_rewrite = default_build_rewrite,
Matthew Smith922549a2017-05-24 16:18:27 -0500230 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Neale Rannsb80c5362016-10-08 13:03:40 +0100231};
232/* *INDENT-ON* */
Matthew Smith2838a232016-06-21 16:05:09 -0500233
234static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
Matthew Smith2838a232016-06-21 16:05:09 -0500236{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700237 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion586afd72017-04-05 19:18:20 +0200238 ASSERT (vlib_get_thread_index () == 0);
Matthew Smith2838a232016-06-21 16:05:09 -0500239
Matthew Smithe04d09d2017-05-14 21:47:18 -0500240 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
Matthew Smith2838a232016-06-21 16:05:09 -0500241}
242
243int
244ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
245{
246 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700247 (u8 *) args, sizeof (*args));
Matthew Smith2838a232016-06-21 16:05:09 -0500248 return 0;
249}
250
Neale Ranns8d7c5022019-02-06 01:41:05 -0800251static u32
252ipsec_tun_mk_input_sa_id (u32 ti)
253{
254 return (0x80000000 | ti);
255}
256
257static u32
258ipsec_tun_mk_output_sa_id (u32 ti)
259{
260 return (0xc0000000 | ti);
261}
262
Matthew Smith2838a232016-06-21 16:05:09 -0500263int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700264ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
Matthew Smithe04d09d2017-05-14 21:47:18 -0500265 ipsec_add_del_tunnel_args_t * args,
266 u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700268 ipsec_tunnel_if_t *t;
269 ipsec_main_t *im = &ipsec_main;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500270 vnet_hw_interface_t *hi = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 u32 hw_if_index = ~0;
272 uword *p;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500273 u32 dev_instance;
Klement Sekera31da2e32018-06-24 22:49:55 +0200274 u32 slot;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800275 ipsec_key_t crypto_key, integ_key;
276 ipsec_sa_flags_t flags;
277 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000279 u64 key = ((u64) args->remote_ip.ip4.as_u32 << 32 |
280 (u64) clib_host_to_net_u32 (args->remote_spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 p = hash_get (im->ipsec_if_pool_index_by_key, key);
282
283 if (args->is_add)
284 {
285 /* check if same src/dst pair exists */
286 if (p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700287 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Neale Rannsfe480f62019-02-28 12:03:58 +0000289 pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Matthew Smith8e1039a2018-04-12 07:32:56 -0500291 dev_instance = t - im->tunnel_interfaces;
292 if (args->renumber)
293 t->show_instance = args->show_instance;
294 else
295 t->show_instance = dev_instance;
296
297 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
298 {
299 pool_put (im->tunnel_interfaces, t);
300 return VNET_API_ERROR_INSTANCE_IN_USE;
301 }
302
303 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
304 dev_instance);
305
Neale Ranns8d7c5022019-02-06 01:41:05 -0800306 flags = IPSEC_SA_FLAG_IS_TUNNEL;
307 if (args->udp_encap)
308 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
309 if (args->esn)
310 flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM;
311 if (args->anti_replay)
312 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Neale Ranns8d7c5022019-02-06 01:41:05 -0800314 ipsec_mk_key (&crypto_key,
315 args->remote_crypto_key, args->remote_crypto_key_len);
316 ipsec_mk_key (&integ_key,
317 args->remote_integ_key, args->remote_integ_key_len);
318
319 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
320 args->remote_spi,
321 IPSEC_PROTOCOL_ESP,
322 args->crypto_alg,
323 &crypto_key,
324 args->integ_alg,
325 &integ_key,
326 flags,
327 args->tx_table_id,
328 &args->remote_ip,
329 &args->local_ip, &t->input_sa_index);
330
331 if (rv)
332 return VNET_API_ERROR_UNIMPLEMENTED;
333
334 ipsec_mk_key (&crypto_key,
335 args->local_crypto_key, args->local_crypto_key_len);
336 ipsec_mk_key (&integ_key,
337 args->local_integ_key, args->local_integ_key_len);
338
339 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
340 args->local_spi,
341 IPSEC_PROTOCOL_ESP,
342 args->crypto_alg,
343 &crypto_key,
344 args->integ_alg,
345 &integ_key,
346 flags,
347 args->tx_table_id,
348 &args->local_ip,
349 &args->remote_ip, &t->output_sa_index);
350
351 if (rv)
352 return VNET_API_ERROR_UNIMPLEMENTED;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700354 hash_set (im->ipsec_if_pool_index_by_key, key,
355 t - im->tunnel_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Matthew Smith8e1039a2018-04-12 07:32:56 -0500357 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
358 t - im->tunnel_interfaces,
359 ipsec_hw_class.index,
360 t - im->tunnel_interfaces);
Matthew Smithe04d09d2017-05-14 21:47:18 -0500361
362 hi = vnet_get_hw_interface (vnm, hw_if_index);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800363 /* add esp4 as the next-node-index of this tx-node */
Klement Sekera31da2e32018-06-24 22:49:55 +0200364
Matthew Smith0e36bbf2018-07-05 14:45:58 -0500365 slot = vlib_node_add_next_with_slot
Kingwel Xiec69ac312019-02-04 01:49:29 -0800366 (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, 0);
Klement Sekera31da2e32018-06-24 22:49:55 +0200367
Kingwel Xiec69ac312019-02-04 01:49:29 -0800368 ASSERT (slot == 0);
Klement Sekera31da2e32018-06-24 22:49:55 +0200369
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 t->hw_if_index = hw_if_index;
Neale Rannsfe480f62019-02-28 12:03:58 +0000371 t->sw_if_index = hi->sw_if_index;
372
Matthew Smith537eeec2018-04-09 11:49:20 -0500373 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
374 hi->sw_if_index, 1, 0, 0);
375
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 /*1st interface, register protocol */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700377 if (pool_elts (im->tunnel_interfaces) == 1)
378 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
379 ipsec_if_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 }
382 else
383 {
384 /* check if exists */
385 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700386 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700388 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700390 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
Matthew Smith537eeec2018-04-09 11:49:20 -0500391
392 vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
393 hi->sw_if_index, 0, 0, 0);
394
Matthew Smith8e1039a2018-04-12 07:32:56 -0500395 vnet_delete_hw_interface (vnm, t->hw_if_index);
Matthew Smith01034be2017-05-16 11:51:18 -0500396
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 hash_unset (im->ipsec_if_pool_index_by_key, key);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500398 hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
399
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 pool_put (im->tunnel_interfaces, t);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800401
402 /* delete input and output SA */
403 ipsec_sa_del (ipsec_tun_mk_input_sa_id (p[0]));
404 ipsec_sa_del (ipsec_tun_mk_output_sa_id (p[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 }
Matthew Smithe04d09d2017-05-14 21:47:18 -0500406
407 if (sw_if_index)
408 *sw_if_index = hi->sw_if_index;
409
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 return 0;
411}
412
413int
Matus Fabian694265d2016-08-10 01:55:36 -0700414ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
415 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
416{
417 ipsec_tunnel_if_t *t = 0;
418 ipsec_main_t *im = &ipsec_main;
419 uword *p;
420 ipsec_sa_t *sa;
421 u64 key;
422 u32 isa, osa;
423
424 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
425 if (!p)
426 return VNET_API_ERROR_INVALID_VALUE;
427 isa = p[0];
428
429 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
430 if (!p)
431 return VNET_API_ERROR_INVALID_VALUE;
432 osa = p[0];
433 sa = pool_elt_at_index (im->sad, p[0]);
434
435 if (sa->is_tunnel)
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000436 key = ((u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 |
437 (u64) clib_host_to_net_u32 (sa->spi));
Matus Fabian694265d2016-08-10 01:55:36 -0700438 else
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000439 key = ((u64) args->remote_ip.as_u32 << 32 |
440 (u64) clib_host_to_net_u32 (sa->spi));
Matus Fabian694265d2016-08-10 01:55:36 -0700441
442 p = hash_get (im->ipsec_if_pool_index_by_key, key);
443
444 if (args->is_add)
445 {
446 /* check if same src/dst pair exists */
447 if (p)
448 return VNET_API_ERROR_INVALID_VALUE;
449
450 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400451 clib_memset (t, 0, sizeof (*t));
Matus Fabian694265d2016-08-10 01:55:36 -0700452
453 t->input_sa_index = isa;
454 t->output_sa_index = osa;
455 t->hw_if_index = ~0;
456 hash_set (im->ipsec_if_pool_index_by_key, key,
457 t - im->tunnel_interfaces);
458
459 /*1st interface, register protocol */
460 if (pool_elts (im->tunnel_interfaces) == 1)
461 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
462 ipsec_if_input_node.index);
463 }
464 else
465 {
466 /* check if exists */
467 if (!p)
468 return VNET_API_ERROR_INVALID_VALUE;
469
470 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
471 hash_unset (im->ipsec_if_pool_index_by_key, key);
472 pool_put (im->tunnel_interfaces, t);
473 }
474 return 0;
475}
476
477int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700478ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
479 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700481 ipsec_main_t *im = &ipsec_main;
482 vnet_hw_interface_t *hi;
483 ipsec_tunnel_if_t *t;
484 ipsec_sa_t *sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
486 hi = vnet_get_hw_interface (vnm, hw_if_index);
487 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
488
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100489 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
490 return VNET_API_ERROR_SYSCALL_ERROR_1;
491
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
493 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700494 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 sa->crypto_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800496 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 }
498 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
499 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700500 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 sa->integ_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800502 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 }
504 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
505 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700506 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 sa->crypto_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800508 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 }
510 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
511 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700512 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 sa->integ_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800514 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 }
516 else
517 return VNET_API_ERROR_INVALID_VALUE;
518
519 return 0;
520}
521
522
Matthew Smithca514fd2017-10-12 12:06:59 -0500523int
524ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
525 u8 is_outbound)
526{
527 ipsec_main_t *im = &ipsec_main;
528 vnet_hw_interface_t *hi;
529 ipsec_tunnel_if_t *t;
530 ipsec_sa_t *sa, *old_sa;
531 u32 sa_index, old_sa_index;
532 uword *p;
533
534 hi = vnet_get_hw_interface (vnm, hw_if_index);
535 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
536
537 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
538 if (sa_index == ~0)
539 {
540 clib_warning ("SA with ID %u not found", sa_id);
541 return VNET_API_ERROR_INVALID_VALUE;
542 }
543
544 if (ipsec_is_sa_used (sa_index))
545 {
546 clib_warning ("SA with ID %u is already in use", sa_id);
547 return VNET_API_ERROR_INVALID_VALUE;
548 }
549
550 sa = pool_elt_at_index (im->sad, sa_index);
551 if (sa->is_tunnel_ip6)
552 {
553 clib_warning ("IPsec interface not supported with IPv6 endpoints");
554 return VNET_API_ERROR_UNIMPLEMENTED;
555 }
556
557 if (!is_outbound)
558 {
559 u64 key;
560
561 old_sa_index = t->input_sa_index;
562 old_sa = pool_elt_at_index (im->sad, old_sa_index);
563
564 /* unset old inbound hash entry. packets should stop arriving */
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000565 key = ((u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 |
566 (u64) clib_host_to_net_u32 (old_sa->spi));
Matthew Smithca514fd2017-10-12 12:06:59 -0500567 p = hash_get (im->ipsec_if_pool_index_by_key, key);
568 if (p)
569 hash_unset (im->ipsec_if_pool_index_by_key, key);
570
571 /* set new inbound SA, then set new hash entry */
572 t->input_sa_index = sa_index;
Neale Ranns7ba8fe22019-02-11 11:24:07 +0000573 key = ((u64) sa->tunnel_src_addr.ip4.as_u32 << 32 |
574 (u64) clib_host_to_net_u32 (sa->spi));
Matthew Smithca514fd2017-10-12 12:06:59 -0500575 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
576 }
577 else
578 {
579 old_sa_index = t->output_sa_index;
580 old_sa = pool_elt_at_index (im->sad, old_sa_index);
581 t->output_sa_index = sa_index;
582 }
583
584 /* remove sa_id to sa_index mapping on old SA */
585 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
586 hash_unset (im->sa_index_by_sa_id, old_sa->id);
587
Matthew Smith41b25cf2018-12-05 14:41:21 -0600588 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
Matthew Smithca514fd2017-10-12 12:06:59 -0500589 {
Klement Sekerab4d30532018-11-08 13:00:02 +0100590 clib_warning ("IPsec backend add/del callback returned error");
591 return VNET_API_ERROR_SYSCALL_ERROR_1;
Matthew Smithca514fd2017-10-12 12:06:59 -0500592 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500593 pool_put (im->sad, old_sa);
594
595 return 0;
596}
597
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598clib_error_t *
599ipsec_tunnel_if_init (vlib_main_t * vm)
600{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700601 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
603 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
Matthew Smith8e1039a2018-04-12 07:32:56 -0500604 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
606 return 0;
607}
608
609VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
610
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700611
612/*
613 * fd.io coding-style-patch-verification: ON
614 *
615 * Local Variables:
616 * eval: (c-set-style "gnu")
617 * End:
618 */