Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ipsec_itf.c: IPSec dedicated interface type |
| 3 | * |
| 4 | * Copyright (c) 2020 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/ip/ip.h> |
| 19 | #include <vnet/ipsec/ipsec_itf.h> |
| 20 | #include <vnet/ipsec/ipsec_tun.h> |
| 21 | #include <vnet/ipsec/ipsec.h> |
| 22 | #include <vnet/adj/adj_midchain.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 23 | #include <vnet/ethernet/mac_address.h> |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 24 | |
| 25 | /* bitmap of Allocated IPSEC_ITF instances */ |
| 26 | static uword *ipsec_itf_instances; |
| 27 | |
| 28 | /* pool of interfaces */ |
| 29 | static ipsec_itf_t *ipsec_itf_pool; |
| 30 | |
| 31 | static u32 *ipsec_itf_index_by_sw_if_index; |
| 32 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 33 | ipsec_itf_t * |
| 34 | ipsec_itf_get (index_t ii) |
| 35 | { |
| 36 | return (pool_elt_at_index (ipsec_itf_pool, ii)); |
| 37 | } |
| 38 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 39 | static ipsec_itf_t * |
| 40 | ipsec_itf_find_by_sw_if_index (u32 sw_if_index) |
| 41 | { |
| 42 | if (vec_len (ipsec_itf_index_by_sw_if_index) <= sw_if_index) |
| 43 | return NULL; |
| 44 | u32 ti = ipsec_itf_index_by_sw_if_index[sw_if_index]; |
| 45 | if (ti == ~0) |
| 46 | return NULL; |
| 47 | return pool_elt_at_index (ipsec_itf_pool, ti); |
| 48 | } |
| 49 | |
| 50 | static u8 * |
| 51 | format_ipsec_itf_name (u8 * s, va_list * args) |
| 52 | { |
| 53 | u32 dev_instance = va_arg (*args, u32); |
| 54 | return format (s, "ipsec%d", dev_instance); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | ipsec_itf_adj_unstack (adj_index_t ai) |
| 59 | { |
| 60 | adj_midchain_delegate_unstack (ai); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | ipsec_itf_adj_stack (adj_index_t ai, u32 sai) |
| 65 | { |
| 66 | const vnet_hw_interface_t *hw; |
| 67 | |
| 68 | hw = vnet_get_sup_hw_interface (vnet_get_main (), adj_get_sw_if_index (ai)); |
| 69 | |
| 70 | if (hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 71 | { |
| 72 | const ipsec_sa_t *sa; |
| 73 | |
| 74 | sa = ipsec_sa_get (sai); |
| 75 | |
| 76 | /* *INDENT-OFF* */ |
| 77 | const fib_prefix_t dst = { |
| 78 | .fp_len = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ? 128 : 32), |
| 79 | .fp_proto = (ipsec_sa_is_set_IS_TUNNEL_V6(sa)? |
| 80 | FIB_PROTOCOL_IP6 : |
| 81 | FIB_PROTOCOL_IP4), |
| 82 | .fp_addr = sa->tunnel_dst_addr, |
| 83 | }; |
| 84 | /* *INDENT-ON* */ |
| 85 | |
Neale Ranns | 637c51f | 2020-10-20 08:49:31 +0000 | [diff] [blame] | 86 | adj_midchain_delegate_stack (ai, sa->tx_fib_index, &dst); |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | adj_midchain_delegate_unstack (ai); |
| 90 | } |
| 91 | |
| 92 | static adj_walk_rc_t |
| 93 | ipsec_itf_adj_stack_cb (adj_index_t ai, void *arg) |
| 94 | { |
| 95 | ipsec_tun_protect_t *itp = arg; |
| 96 | |
| 97 | ipsec_itf_adj_stack (ai, itp->itp_out_sa); |
| 98 | |
| 99 | return (ADJ_WALK_RC_CONTINUE); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | ipsec_itf_restack (index_t itpi, const ipsec_itf_t * itf) |
| 104 | { |
| 105 | ipsec_tun_protect_t *itp; |
| 106 | fib_protocol_t proto; |
| 107 | |
| 108 | itp = ipsec_tun_protect_get (itpi); |
| 109 | |
| 110 | /* |
| 111 | * walk all the adjacencies on the interface and restack them |
| 112 | */ |
| 113 | FOR_EACH_FIB_IP_PROTOCOL (proto) |
| 114 | { |
| 115 | adj_nbr_walk (itf->ii_sw_if_index, proto, ipsec_itf_adj_stack_cb, itp); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static walk_rc_t |
| 120 | ipsec_tun_protect_walk_state_change (index_t itpi, void *arg) |
| 121 | { |
| 122 | const ipsec_itf_t *itf = arg; |
| 123 | |
| 124 | ipsec_itf_restack (itpi, itf); |
| 125 | |
| 126 | return (WALK_CONTINUE); |
| 127 | } |
| 128 | |
| 129 | static clib_error_t * |
| 130 | ipsec_itf_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 131 | { |
| 132 | vnet_hw_interface_t *hi; |
| 133 | ipsec_itf_t *itf; |
| 134 | u32 hw_flags; |
| 135 | |
| 136 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 137 | hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ? |
| 138 | VNET_HW_INTERFACE_FLAG_LINK_UP : 0); |
| 139 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
| 140 | |
| 141 | itf = ipsec_itf_find_by_sw_if_index (hi->sw_if_index); |
| 142 | |
| 143 | if (itf) |
| 144 | ipsec_tun_protect_walk_itf (itf->ii_sw_if_index, |
| 145 | ipsec_tun_protect_walk_state_change, itf); |
| 146 | |
| 147 | return (NULL); |
| 148 | } |
| 149 | |
| 150 | static int |
| 151 | ipsec_itf_tunnel_desc (u32 sw_if_index, |
| 152 | ip46_address_t * src, ip46_address_t * dst, u8 * is_l2) |
| 153 | { |
| 154 | ip46_address_reset (src); |
| 155 | ip46_address_reset (dst); |
| 156 | *is_l2 = 0; |
| 157 | |
| 158 | return (0); |
| 159 | } |
| 160 | |
| 161 | static u8 * |
| 162 | ipsec_itf_build_rewrite (void) |
| 163 | { |
| 164 | /* |
| 165 | * passing the adj code a NULL rewrite means 'i don't have one cos |
| 166 | * t'other end is unresolved'. That's not the case here. For the ipsec |
| 167 | * tunnel there are just no bytes of encap to apply in the adj. |
| 168 | * So return a zero length rewrite. Encap will be added by a tunnel mode SA. |
| 169 | */ |
| 170 | u8 *rewrite = NULL; |
| 171 | |
| 172 | vec_validate (rewrite, 0); |
| 173 | vec_reset_length (rewrite); |
| 174 | |
| 175 | return (rewrite); |
| 176 | } |
| 177 | |
| 178 | static u8 * |
| 179 | ipsec_itf_build_rewrite_i (vnet_main_t * vnm, |
| 180 | u32 sw_if_index, |
| 181 | vnet_link_t link_type, const void *dst_address) |
| 182 | { |
| 183 | return (ipsec_itf_build_rewrite ()); |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | ipsec_itf_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 188 | { |
| 189 | adj_nbr_midchain_update_rewrite |
| 190 | (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK, ipsec_itf_build_rewrite ()); |
| 191 | } |
| 192 | |
| 193 | /* *INDENT-OFF* */ |
| 194 | VNET_DEVICE_CLASS (ipsec_itf_device_class) = { |
| 195 | .name = "IPSEC Tunnel", |
| 196 | .format_device_name = format_ipsec_itf_name, |
| 197 | .admin_up_down_function = ipsec_itf_admin_up_down, |
| 198 | .ip_tun_desc = ipsec_itf_tunnel_desc, |
| 199 | }; |
| 200 | |
| 201 | VNET_HW_INTERFACE_CLASS(ipsec_hw_interface_class) = { |
| 202 | .name = "IPSec", |
| 203 | .build_rewrite = ipsec_itf_build_rewrite_i, |
| 204 | .update_adjacency = ipsec_itf_update_adj, |
| 205 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
| 206 | }; |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 207 | VNET_HW_INTERFACE_CLASS(ipsec_p2mp_hw_interface_class) = { |
| 208 | .name = "IPSec", |
| 209 | .build_rewrite = ipsec_itf_build_rewrite_i, |
| 210 | .update_adjacency = ipsec_itf_update_adj, |
| 211 | }; |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 212 | /* *INDENT-ON* */ |
| 213 | |
| 214 | /* |
| 215 | * Maintain a bitmap of allocated ipsec_itf instance numbers. |
| 216 | */ |
| 217 | #define IPSEC_ITF_MAX_INSTANCE (16 * 1024) |
| 218 | |
| 219 | static u32 |
| 220 | ipsec_itf_instance_alloc (u32 want) |
| 221 | { |
| 222 | /* |
| 223 | * Check for dynamically allocated instance number. |
| 224 | */ |
| 225 | if (~0 == want) |
| 226 | { |
| 227 | u32 bit; |
| 228 | |
| 229 | bit = clib_bitmap_first_clear (ipsec_itf_instances); |
| 230 | if (bit >= IPSEC_ITF_MAX_INSTANCE) |
| 231 | { |
| 232 | return ~0; |
| 233 | } |
| 234 | ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, bit, 1); |
| 235 | return bit; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * In range? |
| 240 | */ |
| 241 | if (want >= IPSEC_ITF_MAX_INSTANCE) |
| 242 | { |
| 243 | return ~0; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Already in use? |
| 248 | */ |
| 249 | if (clib_bitmap_get (ipsec_itf_instances, want)) |
| 250 | { |
| 251 | return ~0; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Grant allocation request. |
| 256 | */ |
| 257 | ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, want, 1); |
| 258 | |
| 259 | return want; |
| 260 | } |
| 261 | |
| 262 | static int |
| 263 | ipsec_itf_instance_free (u32 instance) |
| 264 | { |
| 265 | if (instance >= IPSEC_ITF_MAX_INSTANCE) |
| 266 | { |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | if (clib_bitmap_get (ipsec_itf_instances, instance) == 0) |
| 271 | { |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | ipsec_itf_instances = clib_bitmap_set (ipsec_itf_instances, instance, 0); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int |
| 280 | ipsec_itf_create (u32 user_instance, tunnel_mode_t mode, u32 * sw_if_indexp) |
| 281 | { |
| 282 | vnet_main_t *vnm = vnet_get_main (); |
| 283 | u32 instance, hw_if_index; |
| 284 | vnet_hw_interface_t *hi; |
| 285 | ipsec_itf_t *ipsec_itf; |
| 286 | |
| 287 | ASSERT (sw_if_indexp); |
| 288 | |
| 289 | *sw_if_indexp = (u32) ~ 0; |
| 290 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 291 | /* |
| 292 | * Allocate a ipsec_itf instance. Either select on dynamically |
| 293 | * or try to use the desired user_instance number. |
| 294 | */ |
| 295 | instance = ipsec_itf_instance_alloc (user_instance); |
| 296 | if (instance == ~0) |
| 297 | return VNET_API_ERROR_INVALID_REGISTRATION; |
| 298 | |
| 299 | pool_get (ipsec_itf_pool, ipsec_itf); |
| 300 | |
| 301 | /* tunnel index (or instance) */ |
| 302 | u32 t_idx = ipsec_itf - ipsec_itf_pool; |
| 303 | |
| 304 | ipsec_itf->ii_mode = mode; |
| 305 | ipsec_itf->ii_user_instance = instance; |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 306 | |
| 307 | hw_if_index = vnet_register_interface (vnm, |
| 308 | ipsec_itf_device_class.index, |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 309 | ipsec_itf->ii_user_instance, |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 310 | (mode == TUNNEL_MODE_P2P ? |
| 311 | ipsec_hw_interface_class.index : |
| 312 | ipsec_p2mp_hw_interface_class.index), |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 313 | t_idx); |
| 314 | |
| 315 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 316 | |
| 317 | vec_validate_init_empty (ipsec_itf_index_by_sw_if_index, hi->sw_if_index, |
| 318 | INDEX_INVALID); |
| 319 | ipsec_itf_index_by_sw_if_index[hi->sw_if_index] = t_idx; |
| 320 | |
| 321 | ipsec_itf->ii_sw_if_index = *sw_if_indexp = hi->sw_if_index; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | int |
| 327 | ipsec_itf_delete (u32 sw_if_index) |
| 328 | { |
| 329 | vnet_main_t *vnm = vnet_get_main (); |
| 330 | |
| 331 | if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) |
| 332 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 333 | |
| 334 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 335 | if (hw == 0 || hw->dev_class_index != ipsec_itf_device_class.index) |
| 336 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 337 | |
| 338 | ipsec_itf_t *ipsec_itf; |
| 339 | ipsec_itf = ipsec_itf_find_by_sw_if_index (sw_if_index); |
| 340 | if (NULL == ipsec_itf) |
| 341 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 342 | |
| 343 | if (ipsec_itf_instance_free (hw->dev_instance) < 0) |
| 344 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 345 | |
| 346 | vnet_delete_hw_interface (vnm, hw->hw_if_index); |
| 347 | pool_put (ipsec_itf_pool, ipsec_itf); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static clib_error_t * |
| 353 | ipsec_itf_create_cli (vlib_main_t * vm, |
| 354 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 355 | { |
| 356 | unformat_input_t _line_input, *line_input = &_line_input; |
| 357 | u32 instance, sw_if_index; |
| 358 | clib_error_t *error; |
| 359 | mac_address_t mac; |
| 360 | int rv; |
| 361 | |
| 362 | error = NULL; |
| 363 | instance = sw_if_index = ~0; |
| 364 | mac_address_set_zero (&mac); |
| 365 | |
| 366 | if (unformat_user (input, unformat_line_input, line_input)) |
| 367 | { |
| 368 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 369 | { |
| 370 | if (unformat (line_input, "instance %d", &instance)) |
| 371 | ; |
| 372 | else |
| 373 | { |
| 374 | error = clib_error_return (0, "unknown input: %U", |
| 375 | format_unformat_error, line_input); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | unformat_free (line_input); |
| 381 | |
| 382 | if (error) |
| 383 | return error; |
| 384 | } |
| 385 | |
| 386 | rv = ipsec_itf_create (instance, TUNNEL_MODE_P2P, &sw_if_index); |
| 387 | |
| 388 | if (rv) |
| 389 | return clib_error_return (0, "iPSec interface create failed"); |
| 390 | |
| 391 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), |
| 392 | sw_if_index); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /*? |
| 397 | * Create a IPSec interface. |
| 398 | * |
| 399 | * @cliexpar |
| 400 | * The following two command syntaxes are equivalent: |
| 401 | * @cliexcmd{ipsec itf create [instance <instance>]} |
| 402 | * Example of how to create a ipsec interface: |
| 403 | * @cliexcmd{ipsec itf create} |
| 404 | ?*/ |
| 405 | /* *INDENT-OFF* */ |
| 406 | VLIB_CLI_COMMAND (ipsec_itf_create_command, static) = { |
| 407 | .path = "ipsec itf create", |
| 408 | .short_help = "ipsec itf create [instance <instance>]", |
| 409 | .function = ipsec_itf_create_cli, |
| 410 | }; |
| 411 | /* *INDENT-ON* */ |
| 412 | |
| 413 | static clib_error_t * |
| 414 | ipsec_itf_delete_cli (vlib_main_t * vm, |
| 415 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 416 | { |
| 417 | vnet_main_t *vnm; |
| 418 | u32 sw_if_index; |
| 419 | int rv; |
| 420 | |
| 421 | vnm = vnet_get_main (); |
| 422 | sw_if_index = ~0; |
| 423 | |
| 424 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 425 | { |
| 426 | if (unformat |
| 427 | (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 428 | ; |
| 429 | else |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | if (~0 != sw_if_index) |
| 434 | { |
| 435 | rv = ipsec_itf_delete (sw_if_index); |
| 436 | |
| 437 | if (rv) |
| 438 | return clib_error_return (0, "ipsec interface delete failed"); |
| 439 | } |
| 440 | else |
| 441 | return clib_error_return (0, "no such interface: %U", |
| 442 | format_unformat_error, input); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /*? |
| 448 | * Delete a IPSEC_ITF interface. |
| 449 | * |
| 450 | * @cliexpar |
| 451 | * The following two command syntaxes are equivalent: |
| 452 | * @cliexcmd{ipsec itf delete <interface>} |
| 453 | * Example of how to create a ipsec_itf interface: |
| 454 | * @cliexcmd{ipsec itf delete ipsec0} |
| 455 | ?*/ |
| 456 | /* *INDENT-OFF* */ |
| 457 | VLIB_CLI_COMMAND (ipsec_itf_delete_command, static) = { |
| 458 | .path = "ipsec itf delete", |
| 459 | .short_help = "ipsec itf delete <interface>", |
| 460 | .function = ipsec_itf_delete_cli, |
| 461 | }; |
| 462 | /* *INDENT-ON* */ |
| 463 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 464 | static clib_error_t * |
| 465 | ipsec_interface_show (vlib_main_t * vm, |
| 466 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 467 | { |
| 468 | index_t ii; |
| 469 | |
| 470 | /* *INDENT-OFF* */ |
| 471 | pool_foreach_index (ii, ipsec_itf_pool, |
| 472 | ({ |
| 473 | vlib_cli_output (vm, "%U", format_ipsec_itf, ii); |
| 474 | })); |
| 475 | /* *INDENT-ON* */ |
| 476 | |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * show IPSEC tunnel protection hash tables |
| 482 | */ |
| 483 | /* *INDENT-OFF* */ |
| 484 | VLIB_CLI_COMMAND (ipsec_interface_show_node, static) = |
| 485 | { |
| 486 | .path = "show ipsec interface", |
| 487 | .function = ipsec_interface_show, |
| 488 | .short_help = "show ipsec interface", |
| 489 | }; |
| 490 | /* *INDENT-ON* */ |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * fd.io coding-style-patch-verification: ON |
| 494 | * |
| 495 | * Local Variables: |
| 496 | * eval: (c-set-style "gnu") |
| 497 | * End: |
| 498 | */ |