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 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 233 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 234 | ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 235 | ipsec_add_del_tunnel_args_t * args, |
| 236 | u32 * sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 238 | ipsec_tunnel_if_t *t; |
| 239 | ipsec_main_t *im = &ipsec_main; |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 240 | vnet_hw_interface_t *hi = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | u32 hw_if_index = ~0; |
| 242 | uword *p; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 243 | u32 dev_instance; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 244 | ipsec_key_t crypto_key, integ_key; |
| 245 | ipsec_sa_flags_t flags; |
| 246 | int rv; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 247 | int is_ip6 = args->is_ip6; |
| 248 | ipsec4_tunnel_key_t key4; |
| 249 | ipsec6_tunnel_key_t key6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 251 | if (!is_ip6) |
| 252 | { |
| 253 | key4.remote_ip = args->remote_ip.ip4.as_u32; |
| 254 | key4.spi = clib_host_to_net_u32 (args->remote_spi); |
| 255 | p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64); |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | key6.remote_ip = args->remote_ip.ip6; |
| 260 | key6.spi = clib_host_to_net_u32 (args->remote_spi); |
| 261 | p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6); |
| 262 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
| 264 | if (args->is_add) |
| 265 | { |
| 266 | /* check if same src/dst pair exists */ |
| 267 | if (p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 268 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 270 | pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 272 | dev_instance = t - im->tunnel_interfaces; |
| 273 | if (args->renumber) |
| 274 | t->show_instance = args->show_instance; |
| 275 | else |
| 276 | t->show_instance = dev_instance; |
| 277 | |
| 278 | if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance)) |
| 279 | { |
| 280 | pool_put (im->tunnel_interfaces, t); |
| 281 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 282 | } |
| 283 | |
| 284 | hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance, |
| 285 | dev_instance); |
| 286 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 287 | flags = IPSEC_SA_FLAG_IS_TUNNEL; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 288 | if (args->is_ip6) |
| 289 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 290 | if (args->udp_encap) |
| 291 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
| 292 | if (args->esn) |
| 293 | flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM; |
| 294 | if (args->anti_replay) |
| 295 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 297 | ipsec_mk_key (&crypto_key, |
| 298 | args->remote_crypto_key, args->remote_crypto_key_len); |
| 299 | ipsec_mk_key (&integ_key, |
| 300 | args->remote_integ_key, args->remote_integ_key_len); |
| 301 | |
| 302 | rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance), |
| 303 | args->remote_spi, |
| 304 | IPSEC_PROTOCOL_ESP, |
| 305 | args->crypto_alg, |
| 306 | &crypto_key, |
| 307 | args->integ_alg, |
| 308 | &integ_key, |
| 309 | flags, |
| 310 | args->tx_table_id, |
| 311 | &args->remote_ip, |
| 312 | &args->local_ip, &t->input_sa_index); |
| 313 | |
| 314 | if (rv) |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 315 | return VNET_API_ERROR_INVALID_SRC_ADDRESS; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 316 | |
| 317 | ipsec_mk_key (&crypto_key, |
| 318 | args->local_crypto_key, args->local_crypto_key_len); |
| 319 | ipsec_mk_key (&integ_key, |
| 320 | args->local_integ_key, args->local_integ_key_len); |
| 321 | |
| 322 | rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance), |
| 323 | args->local_spi, |
| 324 | IPSEC_PROTOCOL_ESP, |
| 325 | args->crypto_alg, |
| 326 | &crypto_key, |
| 327 | args->integ_alg, |
| 328 | &integ_key, |
| 329 | flags, |
| 330 | args->tx_table_id, |
| 331 | &args->local_ip, |
| 332 | &args->remote_ip, &t->output_sa_index); |
| 333 | |
| 334 | if (rv) |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 335 | return VNET_API_ERROR_INVALID_DST_ADDRESS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 337 | /* copy the key */ |
| 338 | if (is_ip6) |
| 339 | hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6, |
| 340 | t - im->tunnel_interfaces); |
| 341 | else |
| 342 | hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64, |
| 343 | t - im->tunnel_interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 345 | hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index, |
| 346 | t - im->tunnel_interfaces, |
| 347 | ipsec_hw_class.index, |
| 348 | t - im->tunnel_interfaces); |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 349 | |
| 350 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 351 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | t->hw_if_index = hw_if_index; |
Neale Ranns | fe480f6 | 2019-02-28 12:03:58 +0000 | [diff] [blame] | 353 | t->sw_if_index = hi->sw_if_index; |
| 354 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 355 | /* Add the new tunnel to the DB of tunnels per sw_if_index ... */ |
| 356 | vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index, |
| 357 | ~0); |
| 358 | im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance; |
| 359 | |
| 360 | vnet_feature_enable_disable ("ip4-output", |
| 361 | "esp4-encrypt-tun", |
| 362 | t->sw_if_index, 1, |
| 363 | &t->output_sa_index, |
| 364 | sizeof (t->output_sa_index)); |
| 365 | vnet_feature_enable_disable ("ip6-output", |
| 366 | "esp6-encrypt-tun", |
| 367 | t->sw_if_index, 1, |
| 368 | &t->output_sa_index, |
| 369 | sizeof (t->output_sa_index)); |
| 370 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | /*1st interface, register protocol */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 372 | if (pool_elts (im->tunnel_interfaces) == 1) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 373 | { |
| 374 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 375 | ipsec4_if_input_node.index); |
| 376 | ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 377 | ipsec6_if_input_node.index); |
| 378 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | } |
| 381 | else |
| 382 | { |
Neale Ranns | c80cc9a | 2019-03-20 14:10:23 +0000 | [diff] [blame] | 383 | u32 ti; |
| 384 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | /* check if exists */ |
| 386 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 387 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | |
Neale Ranns | c80cc9a | 2019-03-20 14:10:23 +0000 | [diff] [blame] | 389 | ti = p[0]; |
| 390 | t = pool_elt_at_index (im->tunnel_interfaces, ti); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | hi = vnet_get_hw_interface (vnm, t->hw_if_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 392 | 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] | 393 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 394 | vnet_feature_enable_disable ("ip4-output", |
| 395 | "esp4-encrypt-tun", |
| 396 | hi->sw_if_index, 0, |
| 397 | &t->output_sa_index, |
| 398 | sizeof (t->output_sa_index)); |
| 399 | vnet_feature_enable_disable ("ip6-output", |
| 400 | "esp6-encrypt-tun", |
| 401 | hi->sw_if_index, 0, |
| 402 | &t->output_sa_index, |
| 403 | sizeof (t->output_sa_index)); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 404 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Matthew Smith | 01034be | 2017-05-16 11:51:18 -0500 | [diff] [blame] | 405 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 406 | if (is_ip6) |
| 407 | hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6); |
| 408 | else |
| 409 | hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64); |
| 410 | |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 411 | 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] | 412 | im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 413 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | pool_put (im->tunnel_interfaces, t); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 415 | |
| 416 | /* delete input and output SA */ |
Neale Ranns | c80cc9a | 2019-03-20 14:10:23 +0000 | [diff] [blame] | 417 | ipsec_sa_del (ipsec_tun_mk_input_sa_id (ti)); |
| 418 | ipsec_sa_del (ipsec_tun_mk_output_sa_id (ti)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 419 | } |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 420 | |
| 421 | if (sw_if_index) |
| 422 | *sw_if_index = hi->sw_if_index; |
| 423 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 428 | ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm, |
| 429 | ipsec_add_del_ipsec_gre_tunnel_args_t * args) |
| 430 | { |
| 431 | ipsec_tunnel_if_t *t = 0; |
| 432 | ipsec_main_t *im = &ipsec_main; |
| 433 | uword *p; |
| 434 | ipsec_sa_t *sa; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 435 | ipsec4_tunnel_key_t key; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 436 | u32 isa, osa; |
| 437 | |
| 438 | p = hash_get (im->sa_index_by_sa_id, args->local_sa_id); |
| 439 | if (!p) |
| 440 | return VNET_API_ERROR_INVALID_VALUE; |
| 441 | isa = p[0]; |
| 442 | |
| 443 | p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id); |
| 444 | if (!p) |
| 445 | return VNET_API_ERROR_INVALID_VALUE; |
| 446 | osa = p[0]; |
| 447 | sa = pool_elt_at_index (im->sad, p[0]); |
| 448 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 449 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 450 | { |
| 451 | key.remote_ip = sa->tunnel_dst_addr.ip4.as_u32; |
| 452 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 453 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 454 | else |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 455 | { |
| 456 | key.remote_ip = args->remote_ip.as_u32; |
| 457 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 458 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 459 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 460 | 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] | 461 | |
| 462 | if (args->is_add) |
| 463 | { |
| 464 | /* check if same src/dst pair exists */ |
| 465 | if (p) |
| 466 | return VNET_API_ERROR_INVALID_VALUE; |
| 467 | |
| 468 | pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 469 | clib_memset (t, 0, sizeof (*t)); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 470 | |
| 471 | t->input_sa_index = isa; |
| 472 | t->output_sa_index = osa; |
| 473 | t->hw_if_index = ~0; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 474 | hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64, |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 475 | t - im->tunnel_interfaces); |
| 476 | |
| 477 | /*1st interface, register protocol */ |
| 478 | if (pool_elts (im->tunnel_interfaces) == 1) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 479 | { |
| 480 | ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 481 | ipsec4_if_input_node.index); |
| 482 | /* TBD, GRE IPSec6 |
| 483 | * |
| 484 | ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP, |
| 485 | ipsec6_if_input_node.index); |
| 486 | */ |
| 487 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 488 | } |
| 489 | else |
| 490 | { |
| 491 | /* check if exists */ |
| 492 | if (!p) |
| 493 | return VNET_API_ERROR_INVALID_VALUE; |
| 494 | |
| 495 | t = pool_elt_at_index (im->tunnel_interfaces, p[0]); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 496 | hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 497 | pool_put (im->tunnel_interfaces, t); |
| 498 | } |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 503 | ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, |
| 504 | ipsec_if_set_key_type_t type, u8 alg, u8 * key) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 506 | ipsec_main_t *im = &ipsec_main; |
| 507 | vnet_hw_interface_t *hi; |
| 508 | ipsec_tunnel_if_t *t; |
| 509 | ipsec_sa_t *sa; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | |
| 511 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 512 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 513 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 514 | if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 515 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 516 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO) |
| 518 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 519 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 520 | ipsec_sa_set_crypto_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 521 | ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | } |
| 523 | else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG) |
| 524 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 525 | sa = pool_elt_at_index (im->sad, t->output_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 526 | ipsec_sa_set_integ_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 527 | ipsec_mk_key (&sa->integ_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 528 | } |
| 529 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO) |
| 530 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 531 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 532 | ipsec_sa_set_crypto_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 533 | ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | } |
| 535 | else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG) |
| 536 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 537 | sa = pool_elt_at_index (im->sad, t->input_sa_index); |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 538 | ipsec_sa_set_integ_alg (sa, alg); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 539 | ipsec_mk_key (&sa->integ_key, key, vec_len (key)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | } |
| 541 | else |
| 542 | return VNET_API_ERROR_INVALID_VALUE; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 548 | int |
| 549 | ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id, |
| 550 | u8 is_outbound) |
| 551 | { |
| 552 | ipsec_main_t *im = &ipsec_main; |
| 553 | vnet_hw_interface_t *hi; |
| 554 | ipsec_tunnel_if_t *t; |
| 555 | ipsec_sa_t *sa, *old_sa; |
| 556 | u32 sa_index, old_sa_index; |
| 557 | uword *p; |
| 558 | |
| 559 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 560 | t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance); |
| 561 | |
| 562 | sa_index = ipsec_get_sa_index_by_sa_id (sa_id); |
| 563 | if (sa_index == ~0) |
| 564 | { |
| 565 | clib_warning ("SA with ID %u not found", sa_id); |
| 566 | return VNET_API_ERROR_INVALID_VALUE; |
| 567 | } |
| 568 | |
| 569 | if (ipsec_is_sa_used (sa_index)) |
| 570 | { |
| 571 | clib_warning ("SA with ID %u is already in use", sa_id); |
| 572 | return VNET_API_ERROR_INVALID_VALUE; |
| 573 | } |
| 574 | |
| 575 | sa = pool_elt_at_index (im->sad, sa_index); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 576 | |
| 577 | if (!is_outbound) |
| 578 | { |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 579 | old_sa_index = t->input_sa_index; |
| 580 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
| 581 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 582 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^ |
| 583 | ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 584 | { |
| 585 | clib_warning ("IPsec interface SA endpoints type can't be changed"); |
| 586 | return VNET_API_ERROR_INVALID_VALUE; |
| 587 | } |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 588 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 589 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 590 | { |
| 591 | ipsec6_tunnel_key_t key; |
| 592 | |
| 593 | /* unset old inbound hash entry. packets should stop arriving */ |
| 594 | key.remote_ip = old_sa->tunnel_src_addr.ip6; |
| 595 | key.spi = clib_host_to_net_u32 (old_sa->spi); |
| 596 | |
| 597 | p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key); |
| 598 | if (p) |
| 599 | hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key); |
| 600 | |
| 601 | /* set new inbound SA, then set new hash entry */ |
| 602 | t->input_sa_index = sa_index; |
| 603 | key.remote_ip = sa->tunnel_src_addr.ip6; |
| 604 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 605 | |
| 606 | hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key, |
| 607 | hi->dev_instance); |
| 608 | } |
| 609 | else |
| 610 | { |
| 611 | ipsec4_tunnel_key_t key; |
| 612 | |
| 613 | /* unset old inbound hash entry. packets should stop arriving */ |
| 614 | key.remote_ip = old_sa->tunnel_src_addr.ip4.as_u32; |
| 615 | key.spi = clib_host_to_net_u32 (old_sa->spi); |
| 616 | |
| 617 | p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64); |
| 618 | if (p) |
| 619 | hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64); |
| 620 | |
| 621 | /* set new inbound SA, then set new hash entry */ |
| 622 | t->input_sa_index = sa_index; |
| 623 | key.remote_ip = sa->tunnel_src_addr.ip4.as_u32; |
| 624 | key.spi = clib_host_to_net_u32 (sa->spi); |
| 625 | |
| 626 | hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64, |
| 627 | hi->dev_instance); |
| 628 | } |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 629 | } |
| 630 | else |
| 631 | { |
| 632 | old_sa_index = t->output_sa_index; |
| 633 | old_sa = pool_elt_at_index (im->sad, old_sa_index); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 634 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 635 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^ |
| 636 | ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa)) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 637 | { |
| 638 | clib_warning ("IPsec interface SA endpoints type can't be changed"); |
| 639 | return VNET_API_ERROR_INVALID_VALUE; |
| 640 | } |
| 641 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 642 | t->output_sa_index = sa_index; |
| 643 | } |
| 644 | |
| 645 | /* remove sa_id to sa_index mapping on old SA */ |
| 646 | if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index) |
| 647 | hash_unset (im->sa_index_by_sa_id, old_sa->id); |
| 648 | |
Matthew Smith | 41b25cf | 2018-12-05 14:41:21 -0600 | [diff] [blame] | 649 | if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0)) |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 650 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 651 | clib_warning ("IPsec backend add/del callback returned error"); |
| 652 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 653 | } |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 654 | pool_put (im->sad, old_sa); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 659 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | clib_error_t * |
| 661 | ipsec_tunnel_if_init (vlib_main_t * vm) |
| 662 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 663 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 665 | /* initialize the ipsec-if ip4 hash */ |
| 666 | im->ipsec4_if_pool_index_by_key = |
| 667 | hash_create (0, sizeof (ipsec4_tunnel_key_t)); |
| 668 | /* initialize the ipsec-if ip6 hash */ |
| 669 | im->ipsec6_if_pool_index_by_key = hash_create_mem (0, |
| 670 | sizeof |
| 671 | (ipsec6_tunnel_key_t), |
| 672 | sizeof (uword)); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 673 | 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] | 674 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 675 | 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] | 676 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | VLIB_INIT_FUNCTION (ipsec_tunnel_if_init); |
| 681 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * fd.io coding-style-patch-verification: ON |
| 685 | * |
| 686 | * Local Variables: |
| 687 | * eval: (c-set-style "gnu") |
| 688 | * End: |
| 689 | */ |