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> |
Kingwel Xie | 00bff19 | 2019-03-07 01:25:32 -0500 | [diff] [blame] | 22 | #include <vnet/udp/udp.h> |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 23 | #include <vnet/adj/adj_midchain.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
| 25 | #include <vnet/ipsec/ipsec.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 26 | #include <vnet/ipsec/esp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 28 | void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); |
| 29 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 30 | static u8 * |
| 31 | format_ipsec_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | { |
| 33 | u32 dev_instance = va_arg (*args, u32); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 34 | ipsec_main_t *im = &ipsec_main; |
| 35 | ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance; |
| 36 | |
| 37 | return format (s, "ipsec%d", t->show_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 40 | /* Statistics (not really errors) */ |
| 41 | #define foreach_ipsec_if_tx_error \ |
| 42 | _(TX, "good packets transmitted") |
| 43 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 44 | static void |
| 45 | ipsec_if_tunnel_stack (adj_index_t ai) |
Klement Sekera | a98346f | 2018-05-16 10:52:45 +0200 | [diff] [blame] | 46 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 47 | ipsec_main_t *ipm = &ipsec_main; |
| 48 | ipsec_tunnel_if_t *it; |
| 49 | ip_adjacency_t *adj; |
| 50 | u32 sw_if_index; |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 51 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 52 | adj = adj_get (ai); |
| 53 | sw_if_index = adj->rewrite_header.sw_if_index; |
| 54 | |
| 55 | if ((vec_len (ipm->ipsec_if_by_sw_if_index) <= sw_if_index) || |
| 56 | (~0 == ipm->ipsec_if_by_sw_if_index[sw_if_index])) |
| 57 | return; |
| 58 | |
| 59 | it = pool_elt_at_index (ipm->tunnel_interfaces, |
| 60 | ipm->ipsec_if_by_sw_if_index[sw_if_index]); |
| 61 | |
| 62 | if (!vnet_hw_interface_is_link_up (vnet_get_main (), it->hw_if_index)) |
| 63 | { |
| 64 | adj_midchain_delegate_unstack (ai); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | ipsec_sa_t *sa; |
| 69 | |
| 70 | sa = ipsec_sa_get (it->output_sa_index); |
| 71 | |
Neale Ranns | 45d8f85 | 2019-03-28 08:59:12 +0000 | [diff] [blame] | 72 | /* *INDENT-OFF* */ |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 73 | fib_prefix_t pfx = { |
| 74 | .fp_addr = sa->tunnel_dst_addr, |
Neale Ranns | 45d8f85 | 2019-03-28 08:59:12 +0000 | [diff] [blame] | 75 | .fp_len = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ? 128 : 32), |
| 76 | .fp_proto = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ? |
| 77 | FIB_PROTOCOL_IP6 : |
| 78 | FIB_PROTOCOL_IP4), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 79 | }; |
Neale Ranns | 45d8f85 | 2019-03-28 08:59:12 +0000 | [diff] [blame] | 80 | /* *INDENT-ON* */ |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 81 | |
| 82 | adj_midchain_delegate_stack (ai, sa->tx_fib_index, &pfx); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @brief Call back when restacking all adjacencies on a GRE interface |
| 88 | */ |
| 89 | static adj_walk_rc_t |
| 90 | ipsec_if_adj_walk_cb (adj_index_t ai, void *ctx) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 91 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 92 | ipsec_if_tunnel_stack (ai); |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 93 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 94 | return (ADJ_WALK_RC_CONTINUE); |
Klement Sekera | a98346f | 2018-05-16 10:52:45 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Neale Ranns | 77eb28f | 2019-03-04 14:13:14 +0000 | [diff] [blame] | 97 | static void |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 98 | ipsec_if_tunnel_restack (ipsec_tunnel_if_t * it) |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 99 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 100 | fib_protocol_t proto; |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 101 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 102 | /* |
| 103 | * walk all the adjacencies on th GRE interface and restack them |
| 104 | */ |
| 105 | FOR_EACH_FIB_IP_PROTOCOL (proto) |
| 106 | { |
| 107 | adj_nbr_walk (it->sw_if_index, proto, ipsec_if_adj_walk_cb, NULL); |
| 108 | } |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 111 | static clib_error_t * |
| 112 | ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 113 | { |
| 114 | ipsec_main_t *im = &ipsec_main; |
| 115 | clib_error_t *err = 0; |
| 116 | ipsec_tunnel_if_t *t; |
| 117 | vnet_hw_interface_t *hi; |
| 118 | ipsec_sa_t *sa; |
| 119 | |
| 120 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 121 | t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance); |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 122 | t->flags = flags; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 123 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 124 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 125 | { |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 126 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 127 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 128 | err = ipsec_check_support_cb (im, sa); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 129 | if (err) |
| 130 | return err; |
| 131 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 132 | err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1); |
| 133 | if (err) |
| 134 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 135 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 136 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 137 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 138 | err = ipsec_check_support_cb (im, sa); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 139 | if (err) |
| 140 | return err; |
| 141 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 142 | err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1); |
| 143 | if (err) |
| 144 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 145 | |
Radu Nicolau | 3f90339 | 2017-01-30 14:33:39 +0000 | [diff] [blame] | 146 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 147 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 148 | } |
| 149 | else |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 150 | { |
| 151 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 152 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 153 | err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0); |
| 154 | if (err) |
| 155 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 156 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 157 | err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0); |
| 158 | if (err) |
| 159 | return err; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 160 | } |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 161 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 162 | ipsec_if_tunnel_restack (t); |
| 163 | |
| 164 | return (NULL); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 167 | static u8 * |
| 168 | ipsec_if_build_rewrite (vnet_main_t * vnm, |
| 169 | u32 sw_if_index, |
| 170 | vnet_link_t link_type, const void *dst_address) |
| 171 | { |
| 172 | return (NULL); |
| 173 | } |
| 174 | |
| 175 | static void |
| 176 | ipsec_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 177 | { |
| 178 | adj_nbr_midchain_update_rewrite |
| 179 | (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK, |
| 180 | ipsec_if_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL)); |
| 181 | |
| 182 | ipsec_if_tunnel_stack (ai); |
| 183 | } |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 184 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 185 | /* *INDENT-OFF* */ |
Neale Ranns | 77eb28f | 2019-03-04 14:13:14 +0000 | [diff] [blame] | 186 | VNET_DEVICE_CLASS (ipsec_device_class) = |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 187 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 188 | .name = "IPSec", |
| 189 | .format_device_name = format_ipsec_name, |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 190 | .admin_up_down_function = ipsec_admin_up_down_function, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 191 | }; |
| 192 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 194 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 195 | VNET_HW_INTERFACE_CLASS (ipsec_hw_class) = |
| 196 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 197 | .name = "IPSec", |
| 198 | .build_rewrite = default_build_rewrite, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 199 | .update_adjacency = ipsec_if_update_adj, |
Matthew Smith | 922549a | 2017-05-24 16:18:27 -0500 | [diff] [blame] | 200 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 201 | }; |
| 202 | /* *INDENT-ON* */ |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 203 | |
| 204 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 205 | 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] | 206 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 207 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 208 | ASSERT (vlib_get_thread_index () == 0); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 209 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 210 | return ipsec_add_del_tunnel_if_internal (vnm, a, NULL); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | int |
| 214 | ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args) |
| 215 | { |
| 216 | 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] | 217 | (u8 *) args, sizeof (*args)); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 221 | static u32 |
| 222 | ipsec_tun_mk_input_sa_id (u32 ti) |
| 223 | { |
| 224 | return (0x80000000 | ti); |
| 225 | } |
| 226 | |
| 227 | static u32 |
| 228 | ipsec_tun_mk_output_sa_id (u32 ti) |
| 229 | { |
| 230 | return (0xc0000000 | ti); |
| 231 | } |
| 232 | |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 233 | static void |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 234 | ipsec_tunnel_feature_set (ipsec_main_t * im, ipsec_tunnel_if_t * t, u8 enable) |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 235 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 236 | u8 arc; |
| 237 | |
| 238 | arc = vnet_get_feature_arc_index ("ip4-output"); |
| 239 | |
| 240 | vnet_feature_enable_disable_with_index (arc, |
| 241 | im->esp4_encrypt_tun_feature_index, |
| 242 | t->sw_if_index, enable, |
| 243 | &t->output_sa_index, |
| 244 | sizeof (t->output_sa_index)); |
| 245 | |
| 246 | arc = vnet_get_feature_arc_index ("ip6-output"); |
| 247 | |
| 248 | vnet_feature_enable_disable_with_index (arc, |
| 249 | im->esp6_encrypt_tun_feature_index, |
| 250 | t->sw_if_index, enable, |
| 251 | &t->output_sa_index, |
| 252 | sizeof (t->output_sa_index)); |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 255 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 256 | ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 257 | ipsec_add_del_tunnel_args_t * args, |
| 258 | u32 * sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 260 | ipsec_tunnel_if_t *t; |
| 261 | ipsec_main_t *im = &ipsec_main; |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 262 | vnet_hw_interface_t *hi = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | u32 hw_if_index = ~0; |
| 264 | uword *p; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 265 | u32 dev_instance; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 266 | ipsec_key_t crypto_key, integ_key; |
| 267 | ipsec_sa_flags_t flags; |
| 268 | int rv; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 269 | int is_ip6 = args->is_ip6; |
| 270 | ipsec4_tunnel_key_t key4; |
| 271 | ipsec6_tunnel_key_t key6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 272 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 273 | if (!is_ip6) |
| 274 | { |
| 275 | key4.remote_ip = args->remote_ip.ip4.as_u32; |
| 276 | key4.spi = clib_host_to_net_u32 (args->remote_spi); |
| 277 | p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | key6.remote_ip = args->remote_ip.ip6; |
| 282 | key6.spi = clib_host_to_net_u32 (args->remote_spi); |
| 283 | p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6); |
| 284 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
| 286 | if (args->is_add) |
| 287 | { |
| 288 | /* check if same src/dst pair exists */ |
| 289 | if (p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 290 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 292 | pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 294 | dev_instance = t - im->tunnel_interfaces; |
| 295 | if (args->renumber) |
| 296 | t->show_instance = args->show_instance; |
| 297 | else |
| 298 | t->show_instance = dev_instance; |
| 299 | |
| 300 | if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance)) |
| 301 | { |
| 302 | pool_put (im->tunnel_interfaces, t); |
| 303 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 304 | } |
| 305 | |
| 306 | hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance, |
| 307 | dev_instance); |
| 308 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 309 | flags = IPSEC_SA_FLAG_IS_TUNNEL; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 310 | if (args->is_ip6) |
| 311 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 312 | if (args->udp_encap) |
| 313 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
| 314 | if (args->esn) |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 315 | flags |= IPSEC_SA_FLAG_USE_ESN; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 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, |
Neale Ranns | 2b5ba95 | 2019-04-02 10:15:40 +0000 | [diff] [blame] | 331 | (flags | IPSEC_SA_FLAG_IS_INBOUND), |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 332 | args->tx_table_id, |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 333 | args->salt, |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 334 | &args->remote_ip, |
| 335 | &args->local_ip, &t->input_sa_index); |
| 336 | |
| 337 | if (rv) |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 338 | return VNET_API_ERROR_INVALID_SRC_ADDRESS; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 339 | |
| 340 | ipsec_mk_key (&crypto_key, |
| 341 | args->local_crypto_key, args->local_crypto_key_len); |
| 342 | ipsec_mk_key (&integ_key, |
| 343 | args->local_integ_key, args->local_integ_key_len); |
| 344 | |
| 345 | rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance), |
| 346 | args->local_spi, |
| 347 | IPSEC_PROTOCOL_ESP, |
| 348 | args->crypto_alg, |
| 349 | &crypto_key, |
| 350 | args->integ_alg, |
| 351 | &integ_key, |
| 352 | flags, |
| 353 | args->tx_table_id, |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 354 | args->salt, |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 355 | &args->local_ip, |
| 356 | &args->remote_ip, &t->output_sa_index); |
| 357 | |
| 358 | if (rv) |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 359 | return VNET_API_ERROR_INVALID_DST_ADDRESS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 361 | /* copy the key */ |
| 362 | if (is_ip6) |
| 363 | hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6, |
| 364 | t - im->tunnel_interfaces); |
| 365 | else |
| 366 | hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64, |
| 367 | t - im->tunnel_interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 369 | hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index, |
| 370 | t - im->tunnel_interfaces, |
| 371 | ipsec_hw_class.index, |
| 372 | t - im->tunnel_interfaces); |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 373 | |
| 374 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 375 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | t->hw_if_index = hw_if_index; |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 377 | t->sw_if_index = hi->sw_if_index; |
| 378 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 379 | /* Standard default jumbo MTU. */ |
| 380 | vnet_sw_interface_set_mtu (vnm, t->sw_if_index, 9000); |
| 381 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 382 | /* Add the new tunnel to the DB of tunnels per sw_if_index ... */ |
| 383 | vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index, |
| 384 | ~0); |
| 385 | im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance; |
| 386 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 387 | ipsec_tunnel_feature_set (im, t, 1); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 388 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | /*1st interface, register protocol */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 390 | if (pool_elts (im->tunnel_interfaces) == 1) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 391 | { |
| 392 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 393 | ipsec4_if_input_node.index); |
| 394 | ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 395 | ipsec6_if_input_node.index); |
| 396 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | } |
| 399 | else |
| 400 | { |
Neale Ranns | c80cc9a | 2019-03-20 14:10:23 +0000 | [diff] [blame] | 401 | u32 ti; |
| 402 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | /* check if exists */ |
| 404 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 405 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | |
Neale Ranns | c80cc9a | 2019-03-20 14:10:23 +0000 | [diff] [blame] | 407 | ti = p[0]; |
| 408 | t = pool_elt_at_index (im->tunnel_interfaces, ti); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | hi = vnet_get_hw_interface (vnm, t->hw_if_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 410 | 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] | 411 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 412 | ipsec_tunnel_feature_set (im, t, 0); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 413 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Matthew Smith | 01034be | 2017-05-16 11:51:18 -0500 | [diff] [blame] | 414 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 415 | if (is_ip6) |
| 416 | hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6); |
| 417 | else |
| 418 | hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64); |
| 419 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 420 | hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 421 | im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 422 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | pool_put (im->tunnel_interfaces, t); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 424 | |
| 425 | /* delete input and output SA */ |
Neale Ranns | c80cc9a | 2019-03-20 14:10:23 +0000 | [diff] [blame] | 426 | ipsec_sa_del (ipsec_tun_mk_input_sa_id (ti)); |
| 427 | ipsec_sa_del (ipsec_tun_mk_output_sa_id (ti)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | } |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 429 | |
| 430 | if (sw_if_index) |
| 431 | *sw_if_index = hi->sw_if_index; |
| 432 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | int |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 437 | ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm, |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 438 | const ipsec_gre_tunnel_add_del_args_t * args) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 439 | { |
| 440 | ipsec_tunnel_if_t *t = 0; |
| 441 | ipsec_main_t *im = &ipsec_main; |
| 442 | uword *p; |
| 443 | ipsec_sa_t *sa; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 444 | ipsec4_tunnel_key_t key; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 445 | u32 isa, osa; |
| 446 | |
| 447 | p = hash_get (im->sa_index_by_sa_id, args->local_sa_id); |
| 448 | if (!p) |
| 449 | return VNET_API_ERROR_INVALID_VALUE; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 450 | osa = p[0]; |
| 451 | sa = pool_elt_at_index (im->sad, p[0]); |
| 452 | ipsec_sa_set_IS_GRE (sa); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 453 | |
| 454 | p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id); |
| 455 | if (!p) |
| 456 | return VNET_API_ERROR_INVALID_VALUE; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 457 | isa = p[0]; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 458 | sa = pool_elt_at_index (im->sad, p[0]); |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 459 | ipsec_sa_set_IS_GRE (sa); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 460 | |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 461 | /* we form the key from the input/remote SA whose tunnel is srouce |
| 462 | * at the remote end */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 463 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 464 | { |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 465 | key.remote_ip = sa->tunnel_src_addr.ip4.as_u32; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 466 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 467 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 468 | else |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 469 | { |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 470 | key.remote_ip = args->src.as_u32; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 471 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 472 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 473 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 474 | p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 475 | |
| 476 | if (args->is_add) |
| 477 | { |
| 478 | /* check if same src/dst pair exists */ |
| 479 | if (p) |
| 480 | return VNET_API_ERROR_INVALID_VALUE; |
| 481 | |
| 482 | pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 483 | clib_memset (t, 0, sizeof (*t)); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 484 | |
| 485 | t->input_sa_index = isa; |
| 486 | t->output_sa_index = osa; |
| 487 | t->hw_if_index = ~0; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 488 | hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64, |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 489 | t - im->tunnel_interfaces); |
| 490 | |
| 491 | /*1st interface, register protocol */ |
| 492 | if (pool_elts (im->tunnel_interfaces) == 1) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 493 | { |
| 494 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 495 | ipsec4_if_input_node.index); |
| 496 | /* TBD, GRE IPSec6 |
| 497 | * |
| 498 | ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 499 | ipsec6_if_input_node.index); |
| 500 | */ |
| 501 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | { |
| 505 | /* check if exists */ |
| 506 | if (!p) |
| 507 | return VNET_API_ERROR_INVALID_VALUE; |
| 508 | |
| 509 | t = pool_elt_at_index (im->tunnel_interfaces, p[0]); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 510 | hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 511 | pool_put (im->tunnel_interfaces, t); |
| 512 | } |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 517 | ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, |
| 518 | ipsec_if_set_key_type_t type, u8 alg, u8 * key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 520 | vlib_main_t *vm = vlib_get_main (); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 521 | ipsec_main_t *im = &ipsec_main; |
| 522 | vnet_hw_interface_t *hi; |
| 523 | ipsec_tunnel_if_t *t; |
| 524 | ipsec_sa_t *sa; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | |
| 526 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 527 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 528 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 529 | if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 530 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 531 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO) |
| 533 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 534 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 535 | ipsec_sa_set_crypto_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 536 | ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 537 | sa->crypto_calg = im->crypto_algs[alg].alg; |
| 538 | vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg, |
| 539 | key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | } |
| 541 | else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG) |
| 542 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 543 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 544 | ipsec_sa_set_integ_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 545 | ipsec_mk_key (&sa->integ_key, key, vec_len (key)); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 546 | sa->integ_calg = im->integ_algs[alg].alg; |
| 547 | vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg, |
| 548 | key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | } |
| 550 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO) |
| 551 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 552 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 553 | ipsec_sa_set_crypto_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 554 | ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 555 | sa->crypto_calg = im->crypto_algs[alg].alg; |
| 556 | vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg, |
| 557 | key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | } |
| 559 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG) |
| 560 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 561 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 562 | ipsec_sa_set_integ_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 563 | ipsec_mk_key (&sa->integ_key, key, vec_len (key)); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 564 | sa->integ_calg = im->integ_algs[alg].alg; |
| 565 | vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg, |
| 566 | key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 567 | } |
| 568 | else |
| 569 | return VNET_API_ERROR_INVALID_VALUE; |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 575 | int |
| 576 | ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id, |
| 577 | u8 is_outbound) |
| 578 | { |
| 579 | ipsec_main_t *im = &ipsec_main; |
| 580 | vnet_hw_interface_t *hi; |
| 581 | ipsec_tunnel_if_t *t; |
| 582 | ipsec_sa_t *sa, *old_sa; |
| 583 | u32 sa_index, old_sa_index; |
| 584 | uword *p; |
| 585 | |
| 586 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 587 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 588 | |
| 589 | sa_index = ipsec_get_sa_index_by_sa_id (sa_id); |
| 590 | if (sa_index == ~0) |
| 591 | { |
| 592 | clib_warning ("SA with ID %u not found", sa_id); |
| 593 | return VNET_API_ERROR_INVALID_VALUE; |
| 594 | } |
| 595 | |
| 596 | if (ipsec_is_sa_used (sa_index)) |
| 597 | { |
| 598 | clib_warning ("SA with ID %u is already in use", sa_id); |
| 599 | return VNET_API_ERROR_INVALID_VALUE; |
| 600 | } |
| 601 | |
| 602 | sa = pool_elt_at_index (im->sad, sa_index); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 603 | |
| 604 | if (!is_outbound) |
| 605 | { |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 606 | old_sa_index = t->input_sa_index; |
| 607 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
| 608 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 609 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^ |
| 610 | ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 611 | { |
| 612 | clib_warning ("IPsec interface SA endpoints type can't be changed"); |
| 613 | return VNET_API_ERROR_INVALID_VALUE; |
| 614 | } |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 615 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 616 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 617 | { |
| 618 | ipsec6_tunnel_key_t key; |
| 619 | |
| 620 | /* unset old inbound hash entry. packets should stop arriving */ |
| 621 | key.remote_ip = old_sa->tunnel_src_addr.ip6; |
| 622 | key.spi = clib_host_to_net_u32 (old_sa->spi); |
| 623 | |
| 624 | p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key); |
| 625 | if (p) |
| 626 | hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key); |
| 627 | |
| 628 | /* set new inbound SA, then set new hash entry */ |
| 629 | t->input_sa_index = sa_index; |
| 630 | key.remote_ip = sa->tunnel_src_addr.ip6; |
| 631 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 632 | |
| 633 | hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key, |
| 634 | hi->dev_instance); |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | ipsec4_tunnel_key_t key; |
| 639 | |
| 640 | /* unset old inbound hash entry. packets should stop arriving */ |
| 641 | key.remote_ip = old_sa->tunnel_src_addr.ip4.as_u32; |
| 642 | key.spi = clib_host_to_net_u32 (old_sa->spi); |
| 643 | |
| 644 | p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64); |
| 645 | if (p) |
| 646 | hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64); |
| 647 | |
| 648 | /* set new inbound SA, then set new hash entry */ |
| 649 | t->input_sa_index = sa_index; |
| 650 | key.remote_ip = sa->tunnel_src_addr.ip4.as_u32; |
| 651 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 652 | |
| 653 | hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64, |
| 654 | hi->dev_instance); |
| 655 | } |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 656 | } |
| 657 | else |
| 658 | { |
| 659 | old_sa_index = t->output_sa_index; |
| 660 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 661 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 662 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^ |
| 663 | ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 664 | { |
| 665 | clib_warning ("IPsec interface SA endpoints type can't be changed"); |
| 666 | return VNET_API_ERROR_INVALID_VALUE; |
| 667 | } |
| 668 | |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 669 | /* |
| 670 | * re-enable the feature to get the new SA in |
| 671 | * the workers are stopped so no packets are sent in the clear |
| 672 | */ |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 673 | ipsec_tunnel_feature_set (im, t, 0); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 674 | t->output_sa_index = sa_index; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 675 | ipsec_tunnel_feature_set (im, t, 1); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* remove sa_id to sa_index mapping on old SA */ |
| 679 | if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index) |
| 680 | hash_unset (im->sa_index_by_sa_id, old_sa->id); |
| 681 | |
Matthew Smith | 41b25cf | 2018-12-05 14:41:21 -0600 | [diff] [blame] | 682 | if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0)) |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 683 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 684 | clib_warning ("IPsec backend add/del callback returned error"); |
| 685 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 686 | } |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 687 | ipsec_sa_del (old_sa->id); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | clib_error_t * |
| 693 | ipsec_tunnel_if_init (vlib_main_t * vm) |
| 694 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 695 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 697 | /* initialize the ipsec-if ip4 hash */ |
| 698 | im->ipsec4_if_pool_index_by_key = |
| 699 | hash_create (0, sizeof (ipsec4_tunnel_key_t)); |
| 700 | /* initialize the ipsec-if ip6 hash */ |
| 701 | im->ipsec6_if_pool_index_by_key = hash_create_mem (0, |
| 702 | sizeof |
| 703 | (ipsec6_tunnel_key_t), |
| 704 | sizeof (uword)); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 705 | 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] | 706 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 707 | udp_register_dst_port (vm, UDP_DST_PORT_ipsec, ipsec4_if_input_node.index, |
Kingwel Xie | 00bff19 | 2019-03-07 01:25:32 -0500 | [diff] [blame] | 708 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | VLIB_INIT_FUNCTION (ipsec_tunnel_if_init); |
| 713 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 714 | |
| 715 | /* |
| 716 | * fd.io coding-style-patch-verification: ON |
| 717 | * |
| 718 | * Local Variables: |
| 719 | * eval: (c-set-style "gnu") |
| 720 | * End: |
| 721 | */ |