Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/ipsec/ipsec.h> |
Neale Ranns | 918c161 | 2019-02-21 23:34:59 -0800 | [diff] [blame] | 17 | #include <vnet/ipsec/ipsec_io.h> |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 18 | |
| 19 | int |
| 20 | ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add) |
| 21 | { |
| 22 | ipsec_main_t *im = &ipsec_main; |
| 23 | ipsec_spd_t *spd = 0; |
| 24 | uword *p; |
| 25 | u32 spd_index, k, v; |
| 26 | |
| 27 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 28 | if (p && is_add) |
| 29 | return VNET_API_ERROR_ENTRY_ALREADY_EXISTS; |
| 30 | if (!p && !is_add) |
| 31 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 32 | |
| 33 | if (!is_add) /* delete */ |
| 34 | { |
| 35 | spd_index = p[0]; |
| 36 | spd = pool_elt_at_index (im->spds, spd_index); |
| 37 | if (!spd) |
| 38 | return VNET_API_ERROR_INVALID_VALUE; |
| 39 | /* *INDENT-OFF* */ |
| 40 | hash_foreach (k, v, im->spd_index_by_sw_if_index, ({ |
| 41 | if (v == spd_index) |
| 42 | ipsec_set_interface_spd(vm, k, spd_id, 0); |
| 43 | })); |
| 44 | /* *INDENT-ON* */ |
| 45 | hash_unset (im->spd_index_by_spd_id, spd_id); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 46 | #define _(s,v) vec_free(spd->policies[IPSEC_SPD_POLICY_##s]); |
| 47 | foreach_ipsec_spd_policy_type |
| 48 | #undef _ |
| 49 | pool_put (im->spds, spd); |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 50 | } |
| 51 | else /* create new SPD */ |
| 52 | { |
| 53 | pool_get (im->spds, spd); |
| 54 | clib_memset (spd, 0, sizeof (*spd)); |
| 55 | spd_index = spd - im->spds; |
| 56 | spd->id = spd_id; |
| 57 | hash_set (im->spd_index_by_spd_id, spd_id, spd_index); |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id, |
| 64 | int is_add) |
| 65 | { |
| 66 | ipsec_main_t *im = &ipsec_main; |
| 67 | ip4_ipsec_config_t config; |
| 68 | |
| 69 | u32 spd_index; |
| 70 | uword *p; |
| 71 | |
| 72 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 73 | if (!p) |
| 74 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */ |
| 75 | |
| 76 | spd_index = p[0]; |
| 77 | |
| 78 | p = hash_get (im->spd_index_by_sw_if_index, sw_if_index); |
| 79 | if (p && is_add) |
| 80 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */ |
| 81 | |
| 82 | if (is_add) |
| 83 | { |
| 84 | hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | hash_unset (im->spd_index_by_sw_if_index, sw_if_index); |
| 89 | } |
| 90 | |
| 91 | clib_warning ("sw_if_index %u spd_id %u spd_index %u", |
| 92 | sw_if_index, spd_id, spd_index); |
| 93 | |
| 94 | /* enable IPsec on TX */ |
| 95 | vnet_feature_enable_disable ("ip4-output", "ipsec4-output-feature", |
| 96 | sw_if_index, is_add, 0, 0); |
| 97 | vnet_feature_enable_disable ("ip6-output", "ipsec6-output-feature", |
| 98 | sw_if_index, is_add, 0, 0); |
| 99 | |
| 100 | config.spd_index = spd_index; |
| 101 | |
| 102 | /* enable IPsec on RX */ |
| 103 | vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input-feature", |
| 104 | sw_if_index, is_add, &config, sizeof (config)); |
| 105 | vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input-feature", |
| 106 | sw_if_index, is_add, &config, sizeof (config)); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * fd.io coding-style-patch-verification: ON |
| 113 | * |
| 114 | * Local Variables: |
| 115 | * eval: (c-set-style "gnu") |
| 116 | * End: |
| 117 | */ |