Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 21 | #include <vnet/fib/fib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
| 23 | #include <vnet/ipsec/ipsec.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 24 | #include <vnet/ipsec/esp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 26 | void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); |
| 27 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 28 | static u8 * |
| 29 | format_ipsec_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | { |
| 31 | u32 dev_instance = va_arg (*args, u32); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 32 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 38 | /* Statistics (not really errors) */ |
| 39 | #define foreach_ipsec_if_tx_error \ |
| 40 | _(TX, "good packets transmitted") |
| 41 | |
| 42 | static char *ipsec_if_tx_error_strings[] = { |
| 43 | #define _(sym,string) string, |
| 44 | foreach_ipsec_if_tx_error |
| 45 | #undef _ |
| 46 | }; |
| 47 | |
| 48 | typedef enum |
Klement Sekera | a98346f | 2018-05-16 10:52:45 +0200 | [diff] [blame] | 49 | { |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 50 | #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 | |
| 56 | typedef struct |
| 57 | { |
| 58 | u32 spi; |
| 59 | u32 seq; |
| 60 | } ipsec_if_tx_trace_t; |
| 61 | |
| 62 | u8 * |
| 63 | format_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 Sekera | a98346f | 2018-05-16 10:52:45 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 73 | static uword |
| 74 | ipsec_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 Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 82 | u32 thread_index = vm->thread_index; |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 83 | 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; |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 112 | |
| 113 | /* 0, tx-node next[0] was added by vlib_node_add_next_with_slot */ |
| 114 | next0 = 0; |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 115 | |
| 116 | len0 = vlib_buffer_length_in_chain (vm, b0); |
| 117 | |
| 118 | if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index)) |
| 119 | { |
| 120 | n_packets++; |
| 121 | n_bytes += len0; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | vlib_increment_combined_counter (vim->combined_sw_if_counters + |
| 126 | VNET_INTERFACE_COUNTER_TX, |
| 127 | thread_index, sw_if_index0, |
| 128 | n_packets, n_bytes); |
| 129 | last_sw_if_index = sw_if_index0; |
| 130 | n_packets = 1; |
| 131 | n_bytes = len0; |
| 132 | } |
| 133 | |
| 134 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 135 | { |
| 136 | ipsec_if_tx_trace_t *tr = |
| 137 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 138 | ipsec_sa_t *sa0 = |
| 139 | pool_elt_at_index (im->sad, t0->output_sa_index); |
| 140 | tr->spi = sa0->spi; |
| 141 | tr->seq = sa0->seq; |
| 142 | } |
| 143 | |
| 144 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 145 | n_left_to_next, bi0, next0); |
| 146 | } |
| 147 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 148 | } |
| 149 | |
| 150 | if (last_sw_if_index != ~0) |
| 151 | { |
| 152 | vlib_increment_combined_counter (vim->combined_sw_if_counters + |
| 153 | VNET_INTERFACE_COUNTER_TX, |
| 154 | thread_index, |
| 155 | last_sw_if_index, n_packets, n_bytes); |
| 156 | } |
| 157 | |
| 158 | return from_frame->n_vectors; |
| 159 | } |
| 160 | |
| 161 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 162 | static clib_error_t * |
| 163 | ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 164 | { |
| 165 | ipsec_main_t *im = &ipsec_main; |
| 166 | clib_error_t *err = 0; |
| 167 | ipsec_tunnel_if_t *t; |
| 168 | vnet_hw_interface_t *hi; |
| 169 | ipsec_sa_t *sa; |
| 170 | |
| 171 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 172 | t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance); |
| 173 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 174 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 175 | { |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 176 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 177 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 178 | err = ipsec_check_support_cb (im, sa); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 179 | if (err) |
| 180 | return err; |
| 181 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 182 | err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1); |
| 183 | if (err) |
| 184 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 185 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 186 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 187 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 188 | err = ipsec_check_support_cb (im, sa); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 189 | if (err) |
| 190 | return err; |
| 191 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 192 | err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1); |
| 193 | if (err) |
| 194 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 195 | |
Radu Nicolau | 3f90339 | 2017-01-30 14:33:39 +0000 | [diff] [blame] | 196 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 197 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 198 | } |
| 199 | else |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 200 | { |
| 201 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 202 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 203 | err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0); |
| 204 | if (err) |
| 205 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 206 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 207 | err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0); |
| 208 | if (err) |
| 209 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 210 | } |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 211 | |
| 212 | return /* no error */ 0; |
| 213 | } |
| 214 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 215 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 216 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 217 | VNET_DEVICE_CLASS (ipsec_device_class, static) = |
| 218 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 219 | .name = "IPSec", |
| 220 | .format_device_name = format_ipsec_name, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 221 | .format_tx_trace = format_ipsec_if_tx_trace, |
| 222 | .tx_function = ipsec_if_tx_node_fn, |
| 223 | .tx_function_n_errors = IPSEC_IF_TX_N_ERROR, |
| 224 | .tx_function_error_strings = ipsec_if_tx_error_strings, |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 225 | .admin_up_down_function = ipsec_admin_up_down_function, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 226 | }; |
| 227 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 229 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 230 | VNET_HW_INTERFACE_CLASS (ipsec_hw_class) = |
| 231 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 232 | .name = "IPSec", |
| 233 | .build_rewrite = default_build_rewrite, |
Matthew Smith | 922549a | 2017-05-24 16:18:27 -0500 | [diff] [blame] | 234 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 235 | }; |
| 236 | /* *INDENT-ON* */ |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 237 | |
| 238 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 239 | ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a) |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 240 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 241 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 242 | ASSERT (vlib_get_thread_index () == 0); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 243 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 244 | return ipsec_add_del_tunnel_if_internal (vnm, a, NULL); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int |
| 248 | ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args) |
| 249 | { |
| 250 | vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 251 | (u8 *) args, sizeof (*args)); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 255 | static u32 |
| 256 | ipsec_tun_mk_input_sa_id (u32 ti) |
| 257 | { |
| 258 | return (0x80000000 | ti); |
| 259 | } |
| 260 | |
| 261 | static u32 |
| 262 | ipsec_tun_mk_output_sa_id (u32 ti) |
| 263 | { |
| 264 | return (0xc0000000 | ti); |
| 265 | } |
| 266 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 267 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 268 | ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 269 | ipsec_add_del_tunnel_args_t * args, |
| 270 | u32 * sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 272 | ipsec_tunnel_if_t *t; |
| 273 | ipsec_main_t *im = &ipsec_main; |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 274 | vnet_hw_interface_t *hi = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | u32 hw_if_index = ~0; |
| 276 | uword *p; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 277 | u32 dev_instance; |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 278 | u32 slot; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 279 | ipsec_key_t crypto_key, integ_key; |
| 280 | ipsec_sa_flags_t flags; |
| 281 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Neale Ranns | 7ba8fe2 | 2019-02-11 11:24:07 +0000 | [diff] [blame] | 283 | u64 key = ((u64) args->remote_ip.ip4.as_u32 << 32 | |
| 284 | (u64) clib_host_to_net_u32 (args->remote_spi)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | p = hash_get (im->ipsec_if_pool_index_by_key, key); |
| 286 | |
| 287 | if (args->is_add) |
| 288 | { |
| 289 | /* check if same src/dst pair exists */ |
| 290 | if (p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 291 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
| 293 | pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 294 | clib_memset (t, 0, sizeof (*t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 296 | dev_instance = t - im->tunnel_interfaces; |
| 297 | if (args->renumber) |
| 298 | t->show_instance = args->show_instance; |
| 299 | else |
| 300 | t->show_instance = dev_instance; |
| 301 | |
| 302 | if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance)) |
| 303 | { |
| 304 | pool_put (im->tunnel_interfaces, t); |
| 305 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 306 | } |
| 307 | |
| 308 | hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance, |
| 309 | dev_instance); |
| 310 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 311 | flags = IPSEC_SA_FLAG_IS_TUNNEL; |
| 312 | if (args->udp_encap) |
| 313 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
| 314 | if (args->esn) |
| 315 | flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM; |
| 316 | if (args->anti_replay) |
| 317 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 319 | ipsec_mk_key (&crypto_key, |
| 320 | args->remote_crypto_key, args->remote_crypto_key_len); |
| 321 | ipsec_mk_key (&integ_key, |
| 322 | args->remote_integ_key, args->remote_integ_key_len); |
| 323 | |
| 324 | rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance), |
| 325 | args->remote_spi, |
| 326 | IPSEC_PROTOCOL_ESP, |
| 327 | args->crypto_alg, |
| 328 | &crypto_key, |
| 329 | args->integ_alg, |
| 330 | &integ_key, |
| 331 | flags, |
| 332 | args->tx_table_id, |
| 333 | &args->remote_ip, |
| 334 | &args->local_ip, &t->input_sa_index); |
| 335 | |
| 336 | if (rv) |
| 337 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 338 | |
| 339 | ipsec_mk_key (&crypto_key, |
| 340 | args->local_crypto_key, args->local_crypto_key_len); |
| 341 | ipsec_mk_key (&integ_key, |
| 342 | args->local_integ_key, args->local_integ_key_len); |
| 343 | |
| 344 | rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance), |
| 345 | args->local_spi, |
| 346 | IPSEC_PROTOCOL_ESP, |
| 347 | args->crypto_alg, |
| 348 | &crypto_key, |
| 349 | args->integ_alg, |
| 350 | &integ_key, |
| 351 | flags, |
| 352 | args->tx_table_id, |
| 353 | &args->local_ip, |
| 354 | &args->remote_ip, &t->output_sa_index); |
| 355 | |
| 356 | if (rv) |
| 357 | return VNET_API_ERROR_UNIMPLEMENTED; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 359 | hash_set (im->ipsec_if_pool_index_by_key, key, |
| 360 | t - im->tunnel_interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 362 | hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index, |
| 363 | t - im->tunnel_interfaces, |
| 364 | ipsec_hw_class.index, |
| 365 | t - im->tunnel_interfaces); |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 366 | |
| 367 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 368 | /* add esp4 as the next-node-index of this tx-node */ |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 369 | |
Matthew Smith | 0e36bbf | 2018-07-05 14:45:58 -0500 | [diff] [blame] | 370 | slot = vlib_node_add_next_with_slot |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 371 | (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, 0); |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 372 | |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 373 | ASSERT (slot == 0); |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 374 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | t->hw_if_index = hw_if_index; |
| 376 | |
Matthew Smith | 537eeec | 2018-04-09 11:49:20 -0500 | [diff] [blame] | 377 | vnet_feature_enable_disable ("interface-output", "ipsec-if-output", |
| 378 | hi->sw_if_index, 1, 0, 0); |
| 379 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | /*1st interface, register protocol */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 381 | if (pool_elts (im->tunnel_interfaces) == 1) |
| 382 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 383 | ipsec_if_input_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | } |
| 386 | else |
| 387 | { |
| 388 | /* check if exists */ |
| 389 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 390 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 392 | t = pool_elt_at_index (im->tunnel_interfaces, p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | hi = vnet_get_hw_interface (vnm, t->hw_if_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 394 | vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */ |
Matthew Smith | 537eeec | 2018-04-09 11:49:20 -0500 | [diff] [blame] | 395 | |
| 396 | vnet_feature_enable_disable ("interface-output", "ipsec-if-output", |
| 397 | hi->sw_if_index, 0, 0, 0); |
| 398 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 399 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Matthew Smith | 01034be | 2017-05-16 11:51:18 -0500 | [diff] [blame] | 400 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | hash_unset (im->ipsec_if_pool_index_by_key, key); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 402 | hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance); |
| 403 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | pool_put (im->tunnel_interfaces, t); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 405 | |
| 406 | /* delete input and output SA */ |
| 407 | ipsec_sa_del (ipsec_tun_mk_input_sa_id (p[0])); |
| 408 | ipsec_sa_del (ipsec_tun_mk_output_sa_id (p[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | } |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 410 | |
| 411 | if (sw_if_index) |
| 412 | *sw_if_index = hi->sw_if_index; |
| 413 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 418 | ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm, |
| 419 | ipsec_add_del_ipsec_gre_tunnel_args_t * args) |
| 420 | { |
| 421 | ipsec_tunnel_if_t *t = 0; |
| 422 | ipsec_main_t *im = &ipsec_main; |
| 423 | uword *p; |
| 424 | ipsec_sa_t *sa; |
| 425 | u64 key; |
| 426 | u32 isa, osa; |
| 427 | |
| 428 | p = hash_get (im->sa_index_by_sa_id, args->local_sa_id); |
| 429 | if (!p) |
| 430 | return VNET_API_ERROR_INVALID_VALUE; |
| 431 | isa = p[0]; |
| 432 | |
| 433 | p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id); |
| 434 | if (!p) |
| 435 | return VNET_API_ERROR_INVALID_VALUE; |
| 436 | osa = p[0]; |
| 437 | sa = pool_elt_at_index (im->sad, p[0]); |
| 438 | |
| 439 | if (sa->is_tunnel) |
Neale Ranns | 7ba8fe2 | 2019-02-11 11:24:07 +0000 | [diff] [blame] | 440 | key = ((u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | |
| 441 | (u64) clib_host_to_net_u32 (sa->spi)); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 442 | else |
Neale Ranns | 7ba8fe2 | 2019-02-11 11:24:07 +0000 | [diff] [blame] | 443 | key = ((u64) args->remote_ip.as_u32 << 32 | |
| 444 | (u64) clib_host_to_net_u32 (sa->spi)); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 445 | |
| 446 | p = hash_get (im->ipsec_if_pool_index_by_key, key); |
| 447 | |
| 448 | if (args->is_add) |
| 449 | { |
| 450 | /* check if same src/dst pair exists */ |
| 451 | if (p) |
| 452 | return VNET_API_ERROR_INVALID_VALUE; |
| 453 | |
| 454 | pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 455 | clib_memset (t, 0, sizeof (*t)); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 456 | |
| 457 | t->input_sa_index = isa; |
| 458 | t->output_sa_index = osa; |
| 459 | t->hw_if_index = ~0; |
| 460 | hash_set (im->ipsec_if_pool_index_by_key, key, |
| 461 | t - im->tunnel_interfaces); |
| 462 | |
| 463 | /*1st interface, register protocol */ |
| 464 | if (pool_elts (im->tunnel_interfaces) == 1) |
| 465 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 466 | ipsec_if_input_node.index); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | /* check if exists */ |
| 471 | if (!p) |
| 472 | return VNET_API_ERROR_INVALID_VALUE; |
| 473 | |
| 474 | t = pool_elt_at_index (im->tunnel_interfaces, p[0]); |
| 475 | hash_unset (im->ipsec_if_pool_index_by_key, key); |
| 476 | pool_put (im->tunnel_interfaces, t); |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 482 | ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, |
| 483 | ipsec_if_set_key_type_t type, u8 alg, u8 * key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 485 | ipsec_main_t *im = &ipsec_main; |
| 486 | vnet_hw_interface_t *hi; |
| 487 | ipsec_tunnel_if_t *t; |
| 488 | ipsec_sa_t *sa; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | |
| 490 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 491 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 492 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 493 | if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 494 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 495 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO) |
| 497 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 498 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | sa->crypto_alg = alg; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 500 | ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | } |
| 502 | else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG) |
| 503 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 504 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | sa->integ_alg = alg; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 506 | ipsec_mk_key (&sa->integ_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | } |
| 508 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO) |
| 509 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 510 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | sa->crypto_alg = alg; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 512 | ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | } |
| 514 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG) |
| 515 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 516 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | sa->integ_alg = alg; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 518 | ipsec_mk_key (&sa->integ_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | return VNET_API_ERROR_INVALID_VALUE; |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 527 | int |
| 528 | ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id, |
| 529 | u8 is_outbound) |
| 530 | { |
| 531 | ipsec_main_t *im = &ipsec_main; |
| 532 | vnet_hw_interface_t *hi; |
| 533 | ipsec_tunnel_if_t *t; |
| 534 | ipsec_sa_t *sa, *old_sa; |
| 535 | u32 sa_index, old_sa_index; |
| 536 | uword *p; |
| 537 | |
| 538 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 539 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 540 | |
| 541 | sa_index = ipsec_get_sa_index_by_sa_id (sa_id); |
| 542 | if (sa_index == ~0) |
| 543 | { |
| 544 | clib_warning ("SA with ID %u not found", sa_id); |
| 545 | return VNET_API_ERROR_INVALID_VALUE; |
| 546 | } |
| 547 | |
| 548 | if (ipsec_is_sa_used (sa_index)) |
| 549 | { |
| 550 | clib_warning ("SA with ID %u is already in use", sa_id); |
| 551 | return VNET_API_ERROR_INVALID_VALUE; |
| 552 | } |
| 553 | |
| 554 | sa = pool_elt_at_index (im->sad, sa_index); |
| 555 | if (sa->is_tunnel_ip6) |
| 556 | { |
| 557 | clib_warning ("IPsec interface not supported with IPv6 endpoints"); |
| 558 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 559 | } |
| 560 | |
| 561 | if (!is_outbound) |
| 562 | { |
| 563 | u64 key; |
| 564 | |
| 565 | old_sa_index = t->input_sa_index; |
| 566 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
| 567 | |
| 568 | /* unset old inbound hash entry. packets should stop arriving */ |
Neale Ranns | 7ba8fe2 | 2019-02-11 11:24:07 +0000 | [diff] [blame] | 569 | key = ((u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | |
| 570 | (u64) clib_host_to_net_u32 (old_sa->spi)); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 571 | p = hash_get (im->ipsec_if_pool_index_by_key, key); |
| 572 | if (p) |
| 573 | hash_unset (im->ipsec_if_pool_index_by_key, key); |
| 574 | |
| 575 | /* set new inbound SA, then set new hash entry */ |
| 576 | t->input_sa_index = sa_index; |
Neale Ranns | 7ba8fe2 | 2019-02-11 11:24:07 +0000 | [diff] [blame] | 577 | key = ((u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | |
| 578 | (u64) clib_host_to_net_u32 (sa->spi)); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 579 | hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance); |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | old_sa_index = t->output_sa_index; |
| 584 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
| 585 | t->output_sa_index = sa_index; |
| 586 | } |
| 587 | |
| 588 | /* remove sa_id to sa_index mapping on old SA */ |
| 589 | if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index) |
| 590 | hash_unset (im->sa_index_by_sa_id, old_sa->id); |
| 591 | |
Matthew Smith | 41b25cf | 2018-12-05 14:41:21 -0600 | [diff] [blame] | 592 | if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0)) |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 593 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 594 | clib_warning ("IPsec backend add/del callback returned error"); |
| 595 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 596 | } |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 597 | pool_put (im->sad, old_sa); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | clib_error_t * |
| 604 | ipsec_tunnel_if_init (vlib_main_t * vm) |
| 605 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 606 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 607 | |
| 608 | im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword)); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 609 | im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | VLIB_INIT_FUNCTION (ipsec_tunnel_if_init); |
| 615 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * fd.io coding-style-patch-verification: ON |
| 619 | * |
| 620 | * Local Variables: |
| 621 | * eval: (c-set-style "gnu") |
| 622 | * End: |
| 623 | */ |