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> |
| 17 | |
| 18 | int |
| 19 | ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add) |
| 20 | { |
| 21 | ipsec_main_t *im = &ipsec_main; |
| 22 | ipsec_spd_t *spd = 0; |
| 23 | uword *p; |
| 24 | u32 spd_index, k, v; |
| 25 | |
| 26 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 27 | if (p && is_add) |
| 28 | return VNET_API_ERROR_ENTRY_ALREADY_EXISTS; |
| 29 | if (!p && !is_add) |
| 30 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 31 | |
| 32 | if (!is_add) /* delete */ |
| 33 | { |
| 34 | spd_index = p[0]; |
| 35 | spd = pool_elt_at_index (im->spds, spd_index); |
| 36 | if (!spd) |
| 37 | return VNET_API_ERROR_INVALID_VALUE; |
| 38 | /* *INDENT-OFF* */ |
| 39 | hash_foreach (k, v, im->spd_index_by_sw_if_index, ({ |
| 40 | if (v == spd_index) |
| 41 | ipsec_set_interface_spd(vm, k, spd_id, 0); |
| 42 | })); |
| 43 | /* *INDENT-ON* */ |
| 44 | hash_unset (im->spd_index_by_spd_id, spd_id); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 45 | #define _(s,v) vec_free(spd->policies[IPSEC_SPD_POLICY_##s]); |
| 46 | foreach_ipsec_spd_policy_type |
| 47 | #undef _ |
| 48 | pool_put (im->spds, spd); |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 49 | } |
| 50 | else /* create new SPD */ |
| 51 | { |
| 52 | pool_get (im->spds, spd); |
| 53 | clib_memset (spd, 0, sizeof (*spd)); |
| 54 | spd_index = spd - im->spds; |
| 55 | spd->id = spd_id; |
| 56 | hash_set (im->spd_index_by_spd_id, spd_id, spd_index); |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id, |
| 63 | int is_add) |
| 64 | { |
| 65 | ipsec_main_t *im = &ipsec_main; |
| 66 | ip4_ipsec_config_t config; |
| 67 | |
| 68 | u32 spd_index; |
| 69 | uword *p; |
| 70 | |
| 71 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 72 | if (!p) |
| 73 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */ |
| 74 | |
| 75 | spd_index = p[0]; |
| 76 | |
| 77 | p = hash_get (im->spd_index_by_sw_if_index, sw_if_index); |
| 78 | if (p && is_add) |
| 79 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */ |
| 80 | |
| 81 | if (is_add) |
| 82 | { |
| 83 | hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | hash_unset (im->spd_index_by_sw_if_index, sw_if_index); |
| 88 | } |
| 89 | |
| 90 | clib_warning ("sw_if_index %u spd_id %u spd_index %u", |
| 91 | sw_if_index, spd_id, spd_index); |
| 92 | |
| 93 | /* enable IPsec on TX */ |
| 94 | vnet_feature_enable_disable ("ip4-output", "ipsec4-output-feature", |
| 95 | sw_if_index, is_add, 0, 0); |
| 96 | vnet_feature_enable_disable ("ip6-output", "ipsec6-output-feature", |
| 97 | sw_if_index, is_add, 0, 0); |
| 98 | |
| 99 | config.spd_index = spd_index; |
| 100 | |
| 101 | /* enable IPsec on RX */ |
| 102 | vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input-feature", |
| 103 | sw_if_index, is_add, &config, sizeof (config)); |
| 104 | vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input-feature", |
| 105 | sw_if_index, is_add, &config, sizeof (config)); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * fd.io coding-style-patch-verification: ON |
| 112 | * |
| 113 | * Local Variables: |
| 114 | * eval: (c-set-style "gnu") |
| 115 | * End: |
| 116 | */ |