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> |
| 21 | |
| 22 | #include <vnet/ipsec/ipsec.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 23 | #include <vnet/ipsec/esp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 25 | void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); |
| 26 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 27 | static u8 * |
| 28 | format_ipsec_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | { |
| 30 | u32 dev_instance = va_arg (*args, u32); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 31 | ipsec_main_t *im = &ipsec_main; |
| 32 | ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance; |
| 33 | |
| 34 | return format (s, "ipsec%d", t->show_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 37 | static uword |
| 38 | dummy_interface_tx (vlib_main_t * vm, |
| 39 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | { |
| 41 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 42 | return frame->n_vectors; |
| 43 | } |
| 44 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 45 | static clib_error_t * |
| 46 | ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 47 | { |
| 48 | ipsec_main_t *im = &ipsec_main; |
| 49 | clib_error_t *err = 0; |
| 50 | ipsec_tunnel_if_t *t; |
| 51 | vnet_hw_interface_t *hi; |
| 52 | ipsec_sa_t *sa; |
| 53 | |
| 54 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 55 | t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance); |
| 56 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 57 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 58 | { |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 59 | ASSERT (im->cb.check_support_cb); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 60 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 61 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 62 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 63 | err = im->cb.check_support_cb (sa); |
| 64 | if (err) |
| 65 | return err; |
| 66 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 67 | if (im->cb.add_del_sa_sess_cb) |
| 68 | { |
| 69 | err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 1); |
| 70 | if (err) |
| 71 | return err; |
| 72 | } |
| 73 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 74 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 75 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 76 | err = im->cb.check_support_cb (sa); |
| 77 | if (err) |
| 78 | return err; |
| 79 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 80 | if (im->cb.add_del_sa_sess_cb) |
| 81 | { |
| 82 | err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 1); |
| 83 | if (err) |
| 84 | return err; |
| 85 | } |
| 86 | |
Radu Nicolau | 3f90339 | 2017-01-30 14:33:39 +0000 | [diff] [blame] | 87 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 88 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 89 | } |
| 90 | else |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 91 | { |
| 92 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
| 93 | |
| 94 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
| 95 | |
| 96 | if (im->cb.add_del_sa_sess_cb) |
| 97 | { |
| 98 | err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 0); |
| 99 | if (err) |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
| 104 | |
| 105 | if (im->cb.add_del_sa_sess_cb) |
| 106 | { |
| 107 | err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 0); |
| 108 | if (err) |
| 109 | return err; |
| 110 | } |
| 111 | } |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 112 | |
| 113 | return /* no error */ 0; |
| 114 | } |
| 115 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 116 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 117 | VNET_DEVICE_CLASS (ipsec_device_class, static) = |
| 118 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 119 | .name = "IPSec", |
| 120 | .format_device_name = format_ipsec_name, |
| 121 | .format_tx_trace = format_ipsec_if_output_trace, |
| 122 | .tx_function = dummy_interface_tx, |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 123 | .admin_up_down_function = ipsec_admin_up_down_function, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 124 | }; |
| 125 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 127 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 128 | VNET_HW_INTERFACE_CLASS (ipsec_hw_class) = |
| 129 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 130 | .name = "IPSec", |
| 131 | .build_rewrite = default_build_rewrite, |
Matthew Smith | 922549a | 2017-05-24 16:18:27 -0500 | [diff] [blame] | 132 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 133 | }; |
| 134 | /* *INDENT-ON* */ |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 135 | |
| 136 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 137 | 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] | 138 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 139 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 140 | ASSERT (vlib_get_thread_index () == 0); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 141 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 142 | return ipsec_add_del_tunnel_if_internal (vnm, a, NULL); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int |
| 146 | ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args) |
| 147 | { |
| 148 | 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] | 149 | (u8 *) args, sizeof (*args)); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 154 | ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 155 | ipsec_add_del_tunnel_args_t * args, |
| 156 | u32 * sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 158 | ipsec_tunnel_if_t *t; |
| 159 | ipsec_main_t *im = &ipsec_main; |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 160 | vnet_hw_interface_t *hi = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | u32 hw_if_index = ~0; |
| 162 | uword *p; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 163 | ipsec_sa_t *sa; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 164 | u32 dev_instance; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
| 166 | u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi; |
| 167 | p = hash_get (im->ipsec_if_pool_index_by_key, key); |
| 168 | |
| 169 | if (args->is_add) |
| 170 | { |
| 171 | /* check if same src/dst pair exists */ |
| 172 | if (p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 173 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
| 175 | pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
| 176 | memset (t, 0, sizeof (*t)); |
| 177 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 178 | dev_instance = t - im->tunnel_interfaces; |
| 179 | if (args->renumber) |
| 180 | t->show_instance = args->show_instance; |
| 181 | else |
| 182 | t->show_instance = dev_instance; |
| 183 | |
| 184 | if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance)) |
| 185 | { |
| 186 | pool_put (im->tunnel_interfaces, t); |
| 187 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 188 | } |
| 189 | |
| 190 | hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance, |
| 191 | dev_instance); |
| 192 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | pool_get (im->sad, sa); |
| 194 | memset (sa, 0, sizeof (*sa)); |
| 195 | t->input_sa_index = sa - im->sad; |
| 196 | sa->spi = args->remote_spi; |
| 197 | sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32; |
| 198 | sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32; |
| 199 | sa->is_tunnel = 1; |
| 200 | sa->use_esn = args->esn; |
| 201 | sa->use_anti_replay = args->anti_replay; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 202 | sa->integ_alg = args->integ_alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 203 | if (args->remote_integ_key_len <= sizeof (args->remote_integ_key)) |
| 204 | { |
| 205 | sa->integ_key_len = args->remote_integ_key_len; |
| 206 | clib_memcpy (sa->integ_key, args->remote_integ_key, |
| 207 | args->remote_integ_key_len); |
| 208 | } |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 209 | sa->crypto_alg = args->crypto_alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 210 | if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key)) |
| 211 | { |
| 212 | sa->crypto_key_len = args->remote_crypto_key_len; |
| 213 | clib_memcpy (sa->crypto_key, args->remote_crypto_key, |
| 214 | args->remote_crypto_key_len); |
| 215 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
| 217 | pool_get (im->sad, sa); |
| 218 | memset (sa, 0, sizeof (*sa)); |
| 219 | t->output_sa_index = sa - im->sad; |
| 220 | sa->spi = args->local_spi; |
| 221 | sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32; |
| 222 | sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32; |
| 223 | sa->is_tunnel = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | sa->use_esn = args->esn; |
| 225 | sa->use_anti_replay = args->anti_replay; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 226 | sa->integ_alg = args->integ_alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 227 | if (args->local_integ_key_len <= sizeof (args->local_integ_key)) |
| 228 | { |
| 229 | sa->integ_key_len = args->local_integ_key_len; |
| 230 | clib_memcpy (sa->integ_key, args->local_integ_key, |
| 231 | args->local_integ_key_len); |
| 232 | } |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 233 | sa->crypto_alg = args->crypto_alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 234 | if (args->local_crypto_key_len <= sizeof (args->local_crypto_key)) |
| 235 | { |
| 236 | sa->crypto_key_len = args->local_crypto_key_len; |
| 237 | clib_memcpy (sa->crypto_key, args->local_crypto_key, |
| 238 | args->local_crypto_key_len); |
| 239 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 241 | hash_set (im->ipsec_if_pool_index_by_key, key, |
| 242 | t - im->tunnel_interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 244 | hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index, |
| 245 | t - im->tunnel_interfaces, |
| 246 | ipsec_hw_class.index, |
| 247 | t - im->tunnel_interfaces); |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 248 | |
| 249 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 250 | hi->output_node_index = ipsec_if_output_node.index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | t->hw_if_index = hw_if_index; |
| 252 | |
Matthew Smith | 537eeec | 2018-04-09 11:49:20 -0500 | [diff] [blame] | 253 | vnet_feature_enable_disable ("interface-output", "ipsec-if-output", |
| 254 | hi->sw_if_index, 1, 0, 0); |
| 255 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | /*1st interface, register protocol */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 257 | if (pool_elts (im->tunnel_interfaces) == 1) |
| 258 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 259 | ipsec_if_input_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
| 262 | else |
| 263 | { |
| 264 | /* check if exists */ |
| 265 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 266 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 268 | t = pool_elt_at_index (im->tunnel_interfaces, p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | hi = vnet_get_hw_interface (vnm, t->hw_if_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 270 | 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] | 271 | |
| 272 | vnet_feature_enable_disable ("interface-output", "ipsec-if-output", |
| 273 | hi->sw_if_index, 0, 0, 0); |
| 274 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 275 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Matthew Smith | 01034be | 2017-05-16 11:51:18 -0500 | [diff] [blame] | 276 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | /* delete input and output SA */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 278 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 279 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | pool_put (im->sad, sa); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 281 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 282 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | pool_put (im->sad, sa); |
| 284 | |
| 285 | hash_unset (im->ipsec_if_pool_index_by_key, key); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 286 | hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance); |
| 287 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | pool_put (im->tunnel_interfaces, t); |
| 289 | } |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 290 | |
| 291 | if (sw_if_index) |
| 292 | *sw_if_index = hi->sw_if_index; |
| 293 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | int |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 298 | ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm, |
| 299 | ipsec_add_del_ipsec_gre_tunnel_args_t * args) |
| 300 | { |
| 301 | ipsec_tunnel_if_t *t = 0; |
| 302 | ipsec_main_t *im = &ipsec_main; |
| 303 | uword *p; |
| 304 | ipsec_sa_t *sa; |
| 305 | u64 key; |
| 306 | u32 isa, osa; |
| 307 | |
| 308 | p = hash_get (im->sa_index_by_sa_id, args->local_sa_id); |
| 309 | if (!p) |
| 310 | return VNET_API_ERROR_INVALID_VALUE; |
| 311 | isa = p[0]; |
| 312 | |
| 313 | p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id); |
| 314 | if (!p) |
| 315 | return VNET_API_ERROR_INVALID_VALUE; |
| 316 | osa = p[0]; |
| 317 | sa = pool_elt_at_index (im->sad, p[0]); |
| 318 | |
| 319 | if (sa->is_tunnel) |
| 320 | key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi; |
| 321 | else |
| 322 | key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi; |
| 323 | |
| 324 | p = hash_get (im->ipsec_if_pool_index_by_key, key); |
| 325 | |
| 326 | if (args->is_add) |
| 327 | { |
| 328 | /* check if same src/dst pair exists */ |
| 329 | if (p) |
| 330 | return VNET_API_ERROR_INVALID_VALUE; |
| 331 | |
| 332 | pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
| 333 | memset (t, 0, sizeof (*t)); |
| 334 | |
| 335 | t->input_sa_index = isa; |
| 336 | t->output_sa_index = osa; |
| 337 | t->hw_if_index = ~0; |
| 338 | hash_set (im->ipsec_if_pool_index_by_key, key, |
| 339 | t - im->tunnel_interfaces); |
| 340 | |
| 341 | /*1st interface, register protocol */ |
| 342 | if (pool_elts (im->tunnel_interfaces) == 1) |
| 343 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 344 | ipsec_if_input_node.index); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | /* check if exists */ |
| 349 | if (!p) |
| 350 | return VNET_API_ERROR_INVALID_VALUE; |
| 351 | |
| 352 | t = pool_elt_at_index (im->tunnel_interfaces, p[0]); |
| 353 | hash_unset (im->ipsec_if_pool_index_by_key, key); |
| 354 | pool_put (im->tunnel_interfaces, t); |
| 355 | } |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 360 | ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, |
| 361 | ipsec_if_set_key_type_t type, u8 alg, u8 * key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 363 | ipsec_main_t *im = &ipsec_main; |
| 364 | vnet_hw_interface_t *hi; |
| 365 | ipsec_tunnel_if_t *t; |
| 366 | ipsec_sa_t *sa; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | |
| 368 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 369 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 370 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 371 | if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 372 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 373 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO) |
| 375 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 376 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | sa->crypto_alg = alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 378 | sa->crypto_key_len = vec_len (key); |
| 379 | clib_memcpy (sa->crypto_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | } |
| 381 | else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG) |
| 382 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 383 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | sa->integ_alg = alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 385 | sa->integ_key_len = vec_len (key); |
| 386 | clib_memcpy (sa->integ_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | } |
| 388 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO) |
| 389 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 390 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | sa->crypto_alg = alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 392 | sa->crypto_key_len = vec_len (key); |
| 393 | clib_memcpy (sa->crypto_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | } |
| 395 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG) |
| 396 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 397 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | sa->integ_alg = alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 399 | sa->integ_key_len = vec_len (key); |
| 400 | clib_memcpy (sa->integ_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | } |
| 402 | else |
| 403 | return VNET_API_ERROR_INVALID_VALUE; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 409 | int |
| 410 | ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id, |
| 411 | u8 is_outbound) |
| 412 | { |
| 413 | ipsec_main_t *im = &ipsec_main; |
| 414 | vnet_hw_interface_t *hi; |
| 415 | ipsec_tunnel_if_t *t; |
| 416 | ipsec_sa_t *sa, *old_sa; |
| 417 | u32 sa_index, old_sa_index; |
| 418 | uword *p; |
| 419 | |
| 420 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 421 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 422 | |
| 423 | sa_index = ipsec_get_sa_index_by_sa_id (sa_id); |
| 424 | if (sa_index == ~0) |
| 425 | { |
| 426 | clib_warning ("SA with ID %u not found", sa_id); |
| 427 | return VNET_API_ERROR_INVALID_VALUE; |
| 428 | } |
| 429 | |
| 430 | if (ipsec_is_sa_used (sa_index)) |
| 431 | { |
| 432 | clib_warning ("SA with ID %u is already in use", sa_id); |
| 433 | return VNET_API_ERROR_INVALID_VALUE; |
| 434 | } |
| 435 | |
| 436 | sa = pool_elt_at_index (im->sad, sa_index); |
| 437 | if (sa->is_tunnel_ip6) |
| 438 | { |
| 439 | clib_warning ("IPsec interface not supported with IPv6 endpoints"); |
| 440 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 441 | } |
| 442 | |
| 443 | if (!is_outbound) |
| 444 | { |
| 445 | u64 key; |
| 446 | |
| 447 | old_sa_index = t->input_sa_index; |
| 448 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
| 449 | |
| 450 | /* unset old inbound hash entry. packets should stop arriving */ |
| 451 | key = |
Matthew Smith | 8909558 | 2017-11-06 12:12:13 -0600 | [diff] [blame] | 452 | (u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) old_sa->spi; |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 453 | p = hash_get (im->ipsec_if_pool_index_by_key, key); |
| 454 | if (p) |
| 455 | hash_unset (im->ipsec_if_pool_index_by_key, key); |
| 456 | |
| 457 | /* set new inbound SA, then set new hash entry */ |
| 458 | t->input_sa_index = sa_index; |
Matthew Smith | 8909558 | 2017-11-06 12:12:13 -0600 | [diff] [blame] | 459 | key = (u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) sa->spi; |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 460 | hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | old_sa_index = t->output_sa_index; |
| 465 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
| 466 | t->output_sa_index = sa_index; |
| 467 | } |
| 468 | |
| 469 | /* remove sa_id to sa_index mapping on old SA */ |
| 470 | if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index) |
| 471 | hash_unset (im->sa_index_by_sa_id, old_sa->id); |
| 472 | |
| 473 | if (im->cb.add_del_sa_sess_cb) |
| 474 | { |
| 475 | clib_error_t *err; |
| 476 | |
| 477 | err = im->cb.add_del_sa_sess_cb (old_sa_index, 0); |
| 478 | if (err) |
| 479 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 480 | } |
| 481 | |
| 482 | pool_put (im->sad, old_sa); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | clib_error_t * |
| 489 | ipsec_tunnel_if_init (vlib_main_t * vm) |
| 490 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 491 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | |
| 493 | im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword)); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 494 | 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] | 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | VLIB_INIT_FUNCTION (ipsec_tunnel_if_init); |
| 500 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * fd.io coding-style-patch-verification: ON |
| 504 | * |
| 505 | * Local Variables: |
| 506 | * eval: (c-set-style "gnu") |
| 507 | * End: |
| 508 | */ |