blob: 357d638d38b48c846c4e3011de0488facce08b5a [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
Klement Sekera31da2e32018-06-24 22:49:55 +020073static uword
74ipsec_if_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
75 vlib_frame_t * from_frame)
76{
77 ipsec_main_t *im = &ipsec_main;
78 vnet_main_t *vnm = im->vnet_main;
79 vnet_interface_main_t *vim = &vnm->interface_main;
80 u32 *from, *to_next = 0, next_index;
81 u32 n_left_from, sw_if_index0, last_sw_if_index = ~0;
Damjan Marion067cd622018-07-11 12:47:43 +020082 u32 thread_index = vm->thread_index;
Klement Sekera31da2e32018-06-24 22:49:55 +020083 u32 n_bytes = 0, n_packets = 0;
84
85 from = vlib_frame_vector_args (from_frame);
86 n_left_from = from_frame->n_vectors;
87 next_index = node->cached_next_index;
88
89 while (n_left_from > 0)
90 {
91 u32 n_left_to_next;
92
93 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
94
95 while (n_left_from > 0 && n_left_to_next > 0)
96 {
97 u32 bi0, next0, len0;
98 vlib_buffer_t *b0;
99 ipsec_tunnel_if_t *t0;
100 vnet_hw_interface_t *hi0;
101
102 bi0 = to_next[0] = from[0];
103 from += 1;
104 n_left_from -= 1;
105 to_next += 1;
106 n_left_to_next -= 1;
107 b0 = vlib_get_buffer (vm, bi0);
108 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
109 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
110 t0 = pool_elt_at_index (im->tunnel_interfaces, hi0->dev_instance);
111 vnet_buffer (b0)->ipsec.sad_index = t0->output_sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200112 next0 = IPSEC_OUTPUT_NEXT_ESP4_ENCRYPT;
Klement Sekera31da2e32018-06-24 22:49:55 +0200113
114 len0 = vlib_buffer_length_in_chain (vm, b0);
115
116 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
117 {
118 n_packets++;
119 n_bytes += len0;
120 }
121 else
122 {
123 vlib_increment_combined_counter (vim->combined_sw_if_counters +
124 VNET_INTERFACE_COUNTER_TX,
125 thread_index, sw_if_index0,
126 n_packets, n_bytes);
127 last_sw_if_index = sw_if_index0;
128 n_packets = 1;
129 n_bytes = len0;
130 }
131
132 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
133 {
134 ipsec_if_tx_trace_t *tr =
135 vlib_add_trace (vm, node, b0, sizeof (*tr));
136 ipsec_sa_t *sa0 =
137 pool_elt_at_index (im->sad, t0->output_sa_index);
138 tr->spi = sa0->spi;
139 tr->seq = sa0->seq;
140 }
141
142 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
143 n_left_to_next, bi0, next0);
144 }
145 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
146 }
147
148 if (last_sw_if_index != ~0)
149 {
150 vlib_increment_combined_counter (vim->combined_sw_if_counters +
151 VNET_INTERFACE_COUNTER_TX,
152 thread_index,
153 last_sw_if_index, n_packets, n_bytes);
154 }
155
156 return from_frame->n_vectors;
157}
158
159
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000160static clib_error_t *
161ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
162{
163 ipsec_main_t *im = &ipsec_main;
164 clib_error_t *err = 0;
165 ipsec_tunnel_if_t *t;
166 vnet_hw_interface_t *hi;
167 ipsec_sa_t *sa;
168
169 hi = vnet_get_hw_interface (vnm, hw_if_index);
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100170 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
171
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* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215VNET_DEVICE_CLASS (ipsec_device_class, static) =
216{
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,
220 .tx_function = ipsec_if_tx_node_fn,
221 .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
222 .tx_function_error_strings = ipsec_if_tx_error_strings,
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000223 .admin_up_down_function = ipsec_admin_up_down_function,
Neale Rannsb80c5362016-10-08 13:03:40 +0100224};
225/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Neale Rannsb80c5362016-10-08 13:03:40 +0100227/* *INDENT-OFF* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700228VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
229{
Neale Rannsb80c5362016-10-08 13:03:40 +0100230 .name = "IPSec",
231 .build_rewrite = default_build_rewrite,
Matthew Smith922549a2017-05-24 16:18:27 -0500232 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Neale Rannsb80c5362016-10-08 13:03:40 +0100233};
234/* *INDENT-ON* */
Matthew Smith2838a232016-06-21 16:05:09 -0500235
236static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700237ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
Matthew Smith2838a232016-06-21 16:05:09 -0500238{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700239 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion586afd72017-04-05 19:18:20 +0200240 ASSERT (vlib_get_thread_index () == 0);
Matthew Smith2838a232016-06-21 16:05:09 -0500241
Matthew Smithe04d09d2017-05-14 21:47:18 -0500242 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
Matthew Smith2838a232016-06-21 16:05:09 -0500243}
244
245int
246ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
247{
248 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700249 (u8 *) args, sizeof (*args));
Matthew Smith2838a232016-06-21 16:05:09 -0500250 return 0;
251}
252
Neale Ranns8d7c5022019-02-06 01:41:05 -0800253static u32
254ipsec_tun_mk_input_sa_id (u32 ti)
255{
256 return (0x80000000 | ti);
257}
258
259static u32
260ipsec_tun_mk_output_sa_id (u32 ti)
261{
262 return (0xc0000000 | ti);
263}
264
Matthew Smith2838a232016-06-21 16:05:09 -0500265int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700266ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
Matthew Smithe04d09d2017-05-14 21:47:18 -0500267 ipsec_add_del_tunnel_args_t * args,
268 u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700270 ipsec_tunnel_if_t *t;
271 ipsec_main_t *im = &ipsec_main;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500272 vnet_hw_interface_t *hi = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 u32 hw_if_index = ~0;
274 uword *p;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500275 u32 dev_instance;
Klement Sekera31da2e32018-06-24 22:49:55 +0200276 u32 slot;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800277 ipsec_key_t crypto_key, integ_key;
278 ipsec_sa_flags_t flags;
279 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Neale Ranns8d7c5022019-02-06 01:41:05 -0800281 u64 key = (u64) args->remote_ip.ip4.as_u32 << 32 | (u64) 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
290 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400291 clib_memset (t, 0, sizeof (*t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Matthew Smith8e1039a2018-04-12 07:32:56 -0500293 dev_instance = t - im->tunnel_interfaces;
294 if (args->renumber)
295 t->show_instance = args->show_instance;
296 else
297 t->show_instance = dev_instance;
298
299 if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
300 {
301 pool_put (im->tunnel_interfaces, t);
302 return VNET_API_ERROR_INSTANCE_IN_USE;
303 }
304
305 hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
306 dev_instance);
307
Neale Ranns8d7c5022019-02-06 01:41:05 -0800308 flags = IPSEC_SA_FLAG_IS_TUNNEL;
309 if (args->udp_encap)
310 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
311 if (args->esn)
312 flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM;
313 if (args->anti_replay)
314 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Neale Ranns8d7c5022019-02-06 01:41:05 -0800316 ipsec_mk_key (&crypto_key,
317 args->remote_crypto_key, args->remote_crypto_key_len);
318 ipsec_mk_key (&integ_key,
319 args->remote_integ_key, args->remote_integ_key_len);
320
321 rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
322 args->remote_spi,
323 IPSEC_PROTOCOL_ESP,
324 args->crypto_alg,
325 &crypto_key,
326 args->integ_alg,
327 &integ_key,
328 flags,
329 args->tx_table_id,
330 &args->remote_ip,
331 &args->local_ip, &t->input_sa_index);
332
333 if (rv)
334 return VNET_API_ERROR_UNIMPLEMENTED;
335
336 ipsec_mk_key (&crypto_key,
337 args->local_crypto_key, args->local_crypto_key_len);
338 ipsec_mk_key (&integ_key,
339 args->local_integ_key, args->local_integ_key_len);
340
341 rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
342 args->local_spi,
343 IPSEC_PROTOCOL_ESP,
344 args->crypto_alg,
345 &crypto_key,
346 args->integ_alg,
347 &integ_key,
348 flags,
349 args->tx_table_id,
350 &args->local_ip,
351 &args->remote_ip, &t->output_sa_index);
352
353 if (rv)
354 return VNET_API_ERROR_UNIMPLEMENTED;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700356 hash_set (im->ipsec_if_pool_index_by_key, key,
357 t - im->tunnel_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Matthew Smith8e1039a2018-04-12 07:32:56 -0500359 hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
360 t - im->tunnel_interfaces,
361 ipsec_hw_class.index,
362 t - im->tunnel_interfaces);
Matthew Smithe04d09d2017-05-14 21:47:18 -0500363
364 hi = vnet_get_hw_interface (vnm, hw_if_index);
Klement Sekera31da2e32018-06-24 22:49:55 +0200365
Matthew Smith0e36bbf2018-07-05 14:45:58 -0500366 slot = vlib_node_add_next_with_slot
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200367 (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index,
368 IPSEC_OUTPUT_NEXT_ESP4_ENCRYPT);
Klement Sekera31da2e32018-06-24 22:49:55 +0200369
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200370 ASSERT (slot == IPSEC_OUTPUT_NEXT_ESP4_ENCRYPT);
Klement Sekera31da2e32018-06-24 22:49:55 +0200371
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 t->hw_if_index = hw_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)
437 key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
438 else
439 key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
440
441 p = hash_get (im->ipsec_if_pool_index_by_key, key);
442
443 if (args->is_add)
444 {
445 /* check if same src/dst pair exists */
446 if (p)
447 return VNET_API_ERROR_INVALID_VALUE;
448
449 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400450 clib_memset (t, 0, sizeof (*t));
Matus Fabian694265d2016-08-10 01:55:36 -0700451
452 t->input_sa_index = isa;
453 t->output_sa_index = osa;
454 t->hw_if_index = ~0;
455 hash_set (im->ipsec_if_pool_index_by_key, key,
456 t - im->tunnel_interfaces);
457
458 /*1st interface, register protocol */
459 if (pool_elts (im->tunnel_interfaces) == 1)
460 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
461 ipsec_if_input_node.index);
462 }
463 else
464 {
465 /* check if exists */
466 if (!p)
467 return VNET_API_ERROR_INVALID_VALUE;
468
469 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
470 hash_unset (im->ipsec_if_pool_index_by_key, key);
471 pool_put (im->tunnel_interfaces, t);
472 }
473 return 0;
474}
475
476int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700477ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
478 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700480 ipsec_main_t *im = &ipsec_main;
481 vnet_hw_interface_t *hi;
482 ipsec_tunnel_if_t *t;
483 ipsec_sa_t *sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
485 hi = vnet_get_hw_interface (vnm, hw_if_index);
486 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
487
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100488 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
489 return VNET_API_ERROR_SYSCALL_ERROR_1;
490
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
492 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700493 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 sa->crypto_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800495 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 }
497 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
498 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700499 sa = pool_elt_at_index (im->sad, t->output_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 sa->integ_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800501 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502 }
503 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
504 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700505 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 sa->crypto_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800507 ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 }
509 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
510 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700511 sa = pool_elt_at_index (im->sad, t->input_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 sa->integ_alg = alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800513 ipsec_mk_key (&sa->integ_key, key, vec_len (key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 }
515 else
516 return VNET_API_ERROR_INVALID_VALUE;
517
518 return 0;
519}
520
521
Matthew Smithca514fd2017-10-12 12:06:59 -0500522int
523ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
524 u8 is_outbound)
525{
526 ipsec_main_t *im = &ipsec_main;
527 vnet_hw_interface_t *hi;
528 ipsec_tunnel_if_t *t;
529 ipsec_sa_t *sa, *old_sa;
530 u32 sa_index, old_sa_index;
531 uword *p;
532
533 hi = vnet_get_hw_interface (vnm, hw_if_index);
534 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
535
536 sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
537 if (sa_index == ~0)
538 {
539 clib_warning ("SA with ID %u not found", sa_id);
540 return VNET_API_ERROR_INVALID_VALUE;
541 }
542
543 if (ipsec_is_sa_used (sa_index))
544 {
545 clib_warning ("SA with ID %u is already in use", sa_id);
546 return VNET_API_ERROR_INVALID_VALUE;
547 }
548
549 sa = pool_elt_at_index (im->sad, sa_index);
550 if (sa->is_tunnel_ip6)
551 {
552 clib_warning ("IPsec interface not supported with IPv6 endpoints");
553 return VNET_API_ERROR_UNIMPLEMENTED;
554 }
555
556 if (!is_outbound)
557 {
558 u64 key;
559
560 old_sa_index = t->input_sa_index;
561 old_sa = pool_elt_at_index (im->sad, old_sa_index);
562
563 /* unset old inbound hash entry. packets should stop arriving */
564 key =
Matthew Smith89095582017-11-06 12:12:13 -0600565 (u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) old_sa->spi;
Matthew Smithca514fd2017-10-12 12:06:59 -0500566 p = hash_get (im->ipsec_if_pool_index_by_key, key);
567 if (p)
568 hash_unset (im->ipsec_if_pool_index_by_key, key);
569
570 /* set new inbound SA, then set new hash entry */
571 t->input_sa_index = sa_index;
Matthew Smith89095582017-11-06 12:12:13 -0600572 key = (u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) sa->spi;
Matthew Smithca514fd2017-10-12 12:06:59 -0500573 hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
574 }
575 else
576 {
577 old_sa_index = t->output_sa_index;
578 old_sa = pool_elt_at_index (im->sad, old_sa_index);
579 t->output_sa_index = sa_index;
580 }
581
582 /* remove sa_id to sa_index mapping on old SA */
583 if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
584 hash_unset (im->sa_index_by_sa_id, old_sa->id);
585
Matthew Smith41b25cf2018-12-05 14:41:21 -0600586 if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
Matthew Smithca514fd2017-10-12 12:06:59 -0500587 {
Klement Sekerab4d30532018-11-08 13:00:02 +0100588 clib_warning ("IPsec backend add/del callback returned error");
589 return VNET_API_ERROR_SYSCALL_ERROR_1;
Matthew Smithca514fd2017-10-12 12:06:59 -0500590 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500591 pool_put (im->sad, old_sa);
592
593 return 0;
594}
595
596
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597clib_error_t *
598ipsec_tunnel_if_init (vlib_main_t * vm)
599{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700600 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
602 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
Matthew Smith8e1039a2018-04-12 07:32:56 -0500603 im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
605 return 0;
606}
607
608VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
609
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700610
611/*
612 * fd.io coding-style-patch-verification: ON
613 *
614 * Local Variables:
615 * eval: (c-set-style "gnu")
616 * End:
617 */