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