Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * decap.c : IPSec tunnel 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> |
| 21 | #include <vnet/interface.h> |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #include <vnet/ipsec/ikev2.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 26 | #include <vnet/ipsec/esp.h> |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 27 | #include <vnet/ipsec/ah.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 28 | |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 29 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 30 | ipsec_main_t ipsec_main; |
| 31 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 32 | u32 |
| 33 | ipsec_get_sa_index_by_sa_id (u32 sa_id) |
| 34 | { |
| 35 | ipsec_main_t *im = &ipsec_main; |
| 36 | uword *p = hash_get (im->sa_index_by_sa_id, sa_id); |
| 37 | if (!p) |
| 38 | return ~0; |
| 39 | |
| 40 | return p[0]; |
| 41 | } |
| 42 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 44 | ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id, |
| 45 | int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | { |
| 47 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | ip4_ipsec_config_t config; |
| 49 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 50 | u32 spd_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | uword *p; |
| 52 | |
| 53 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 54 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 55 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
| 57 | spd_index = p[0]; |
| 58 | |
| 59 | p = hash_get (im->spd_index_by_sw_if_index, sw_if_index); |
| 60 | if (p && is_add) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 61 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | |
| 63 | if (is_add) |
| 64 | { |
| 65 | hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | hash_unset (im->spd_index_by_sw_if_index, sw_if_index); |
| 70 | } |
| 71 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 72 | clib_warning ("sw_if_index %u spd_id %u spd_index %u", |
| 73 | sw_if_index, spd_id, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
| 75 | /* enable IPsec on TX */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 76 | vnet_feature_enable_disable ("ip4-output", "ipsec4-output", sw_if_index, |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 77 | is_add, 0, 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 78 | vnet_feature_enable_disable ("ip6-output", "ipsec6-output", sw_if_index, |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 79 | is_add, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Kingwel Xie | fc91a17 | 2018-09-26 05:07:09 -0400 | [diff] [blame] | 81 | config.spd_index = spd_index; |
| 82 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | /* enable IPsec on RX */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 84 | vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input", sw_if_index, |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 85 | is_add, &config, sizeof (config)); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 86 | vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input", sw_if_index, |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 87 | is_add, &config, sizeof (config)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 93 | ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | { |
| 95 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 96 | ipsec_spd_t *spd = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | uword *p; |
| 98 | u32 spd_index, k, v; |
| 99 | |
| 100 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 101 | if (p && is_add) |
| 102 | return VNET_API_ERROR_INVALID_VALUE; |
| 103 | if (!p && !is_add) |
| 104 | return VNET_API_ERROR_INVALID_VALUE; |
| 105 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 106 | if (!is_add) /* delete */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | { |
| 108 | spd_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 109 | spd = pool_elt_at_index (im->spds, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | if (!spd) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 111 | return VNET_API_ERROR_INVALID_VALUE; |
| 112 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | hash_foreach (k, v, im->spd_index_by_sw_if_index, ({ |
| 114 | if (v == spd_index) |
| 115 | ipsec_set_interface_spd(vm, k, spd_id, 0); |
| 116 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 117 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | hash_unset (im->spd_index_by_spd_id, spd_id); |
| 119 | pool_free (spd->policies); |
| 120 | vec_free (spd->ipv4_outbound_policies); |
| 121 | vec_free (spd->ipv6_outbound_policies); |
| 122 | vec_free (spd->ipv4_inbound_protect_policy_indices); |
| 123 | vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices); |
| 124 | pool_put (im->spds, spd); |
| 125 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 126 | else /* create new SPD */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | { |
| 128 | pool_get (im->spds, spd); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 129 | clib_memset (spd, 0, sizeof (*spd)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | spd_index = spd - im->spds; |
| 131 | spd->id = spd_id; |
| 132 | hash_set (im->spd_index_by_spd_id, spd_id, spd_index); |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 138 | ipsec_spd_entry_sort (void *a1, void *a2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 140 | u32 *id1 = a1; |
| 141 | u32 *id2 = a2; |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 142 | ipsec_spd_t *spd = ipsec_main.spd_to_sort; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 143 | ipsec_policy_t *p1, *p2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 145 | p1 = pool_elt_at_index (spd->policies, *id1); |
| 146 | p2 = pool_elt_at_index (spd->policies, *id2); |
| 147 | if (p1 && p2) |
| 148 | return p2->priority - p1->priority; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 154 | ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | { |
| 156 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 157 | ipsec_spd_t *spd = 0; |
| 158 | ipsec_policy_t *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | uword *p; |
| 160 | u32 spd_index; |
| 161 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 162 | clib_warning ("policy-id %u priority %d is_outbound %u", policy->id, |
| 163 | policy->priority, policy->is_outbound); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
| 165 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 166 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 167 | p = hash_get (im->sa_index_by_sa_id, policy->sa_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 169 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | policy->sa_index = p[0]; |
| 171 | } |
| 172 | |
| 173 | p = hash_get (im->spd_index_by_spd_id, policy->id); |
| 174 | |
| 175 | if (!p) |
| 176 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 177 | |
| 178 | spd_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 179 | spd = pool_elt_at_index (im->spds, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | if (!spd) |
| 181 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 182 | |
| 183 | if (is_add) |
| 184 | { |
| 185 | u32 policy_index; |
| 186 | |
| 187 | pool_get (spd->policies, vp); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 188 | clib_memcpy (vp, policy, sizeof (*vp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | policy_index = vp - spd->policies; |
| 190 | |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 191 | ipsec_main.spd_to_sort = spd; |
| 192 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | if (policy->is_outbound) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 194 | { |
| 195 | if (policy->is_ipv6) |
| 196 | { |
| 197 | vec_add1 (spd->ipv6_outbound_policies, policy_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 198 | vec_sort_with_function (spd->ipv6_outbound_policies, |
| 199 | ipsec_spd_entry_sort); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | vec_add1 (spd->ipv4_outbound_policies, policy_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 204 | vec_sort_with_function (spd->ipv4_outbound_policies, |
| 205 | ipsec_spd_entry_sort); |
| 206 | } |
| 207 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 209 | { |
| 210 | if (policy->is_ipv6) |
| 211 | { |
| 212 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 213 | { |
| 214 | vec_add1 (spd->ipv6_inbound_protect_policy_indices, |
| 215 | policy_index); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 216 | vec_sort_with_function |
| 217 | (spd->ipv6_inbound_protect_policy_indices, |
| 218 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 219 | } |
| 220 | else |
| 221 | { |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 222 | vec_add1 |
| 223 | (spd->ipv6_inbound_policy_discard_and_bypass_indices, |
| 224 | policy_index); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 225 | vec_sort_with_function |
| 226 | (spd->ipv6_inbound_policy_discard_and_bypass_indices, |
| 227 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 233 | { |
| 234 | vec_add1 (spd->ipv4_inbound_protect_policy_indices, |
| 235 | policy_index); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 236 | vec_sort_with_function |
| 237 | (spd->ipv4_inbound_protect_policy_indices, |
| 238 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 242 | vec_add1 |
| 243 | (spd->ipv4_inbound_policy_discard_and_bypass_indices, |
| 244 | policy_index); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 245 | vec_sort_with_function |
| 246 | (spd->ipv4_inbound_policy_discard_and_bypass_indices, |
| 247 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 252 | ipsec_main.spd_to_sort = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | } |
| 254 | else |
| 255 | { |
| 256 | u32 i, j; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 257 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | pool_foreach_index(i, spd->policies, ({ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 259 | vp = pool_elt_at_index(spd->policies, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | if (vp->priority != policy->priority) |
| 261 | continue; |
| 262 | if (vp->is_outbound != policy->is_outbound) |
| 263 | continue; |
| 264 | if (vp->policy != policy->policy) |
| 265 | continue; |
| 266 | if (vp->sa_id != policy->sa_id) |
| 267 | continue; |
| 268 | if (vp->protocol != policy->protocol) |
| 269 | continue; |
| 270 | if (vp->lport.start != policy->lport.start) |
| 271 | continue; |
| 272 | if (vp->lport.stop != policy->lport.stop) |
| 273 | continue; |
| 274 | if (vp->rport.start != policy->rport.start) |
| 275 | continue; |
| 276 | if (vp->rport.stop != policy->rport.stop) |
| 277 | continue; |
| 278 | if (vp->is_ipv6 != policy->is_ipv6) |
| 279 | continue; |
| 280 | if (policy->is_ipv6) |
| 281 | { |
| 282 | if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0]) |
| 283 | continue; |
| 284 | if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1]) |
| 285 | continue; |
| 286 | if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0]) |
| 287 | continue; |
| 288 | if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1]) |
| 289 | continue; |
| 290 | if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0]) |
| 291 | continue; |
| 292 | if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1]) |
| 293 | continue; |
| 294 | if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0]) |
| 295 | continue; |
| 296 | if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1]) |
| 297 | continue; |
| 298 | if (policy->is_outbound) |
| 299 | { |
| 300 | vec_foreach_index(j, spd->ipv6_outbound_policies) { |
| 301 | if (vec_elt(spd->ipv6_outbound_policies, j) == i) { |
| 302 | vec_del1 (spd->ipv6_outbound_policies, j); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 310 | { |
| 311 | vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) { |
| 312 | if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) { |
| 313 | vec_del1 (spd->ipv6_inbound_protect_policy_indices, j); |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) { |
| 321 | if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) { |
| 322 | vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j); |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32) |
| 332 | continue; |
| 333 | if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32) |
| 334 | continue; |
| 335 | if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32) |
| 336 | continue; |
| 337 | if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32) |
| 338 | continue; |
| 339 | if (policy->is_outbound) |
| 340 | { |
| 341 | vec_foreach_index(j, spd->ipv4_outbound_policies) { |
| 342 | if (vec_elt(spd->ipv4_outbound_policies, j) == i) { |
| 343 | vec_del1 (spd->ipv4_outbound_policies, j); |
| 344 | break; |
| 345 | } |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 346 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | } |
| 348 | else |
| 349 | { |
| 350 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 351 | { |
| 352 | vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) { |
| 353 | if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) { |
| 354 | vec_del1 (spd->ipv4_inbound_protect_policy_indices, j); |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) { |
| 362 | if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) { |
| 363 | vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j); |
| 364 | break; |
Matus Fabian | ce8805c | 2018-03-16 05:44:24 -0700 | [diff] [blame] | 365 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | pool_put (spd->policies, vp); |
| 371 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 373 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 379 | u8 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 380 | ipsec_is_sa_used (u32 sa_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 382 | ipsec_main_t *im = &ipsec_main; |
| 383 | ipsec_spd_t *spd; |
| 384 | ipsec_policy_t *p; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 385 | ipsec_tunnel_if_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 387 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | pool_foreach(spd, im->spds, ({ |
| 389 | pool_foreach(p, spd->policies, ({ |
| 390 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 391 | { |
| 392 | if (p->sa_index == sa_index) |
| 393 | return 1; |
| 394 | } |
| 395 | })); |
| 396 | })); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 397 | |
| 398 | pool_foreach(t, im->tunnel_interfaces, ({ |
| 399 | if (t->input_sa_index == sa_index) |
| 400 | return 1; |
| 401 | if (t->output_sa_index == sa_index) |
| 402 | return 1; |
| 403 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 404 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 409 | clib_error_t * |
| 410 | ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa, |
| 411 | u32 sa_index, int is_add) |
| 412 | { |
| 413 | ipsec_ah_backend_t *ab; |
| 414 | ipsec_esp_backend_t *eb; |
| 415 | switch (sa->protocol) |
| 416 | { |
| 417 | case IPSEC_PROTOCOL_AH: |
| 418 | ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 419 | if (ab->add_del_sa_sess_cb) |
| 420 | return ab->add_del_sa_sess_cb (sa_index, is_add); |
| 421 | break; |
| 422 | case IPSEC_PROTOCOL_ESP: |
| 423 | eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 424 | if (eb->add_del_sa_sess_cb) |
| 425 | return eb->add_del_sa_sess_cb (sa_index, is_add); |
| 426 | break; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | int |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 432 | ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | { |
| 434 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 435 | ipsec_sa_t *sa = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | uword *p; |
| 437 | u32 sa_index; |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 438 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 440 | clib_warning ("id %u spi %u", new_sa->id, new_sa->spi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | |
| 442 | p = hash_get (im->sa_index_by_sa_id, new_sa->id); |
| 443 | if (p && is_add) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 444 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | if (!p && !is_add) |
| 446 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 447 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 448 | if (!is_add) /* delete */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | { |
| 450 | sa_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 451 | sa = pool_elt_at_index (im->sad, sa_index); |
| 452 | if (ipsec_is_sa_used (sa_index)) |
| 453 | { |
| 454 | clib_warning ("sa_id %u used in policy", sa->id); |
| 455 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */ |
| 456 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | hash_unset (im->sa_index_by_sa_id, sa->id); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 458 | err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0); |
| 459 | if (err) |
| 460 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | pool_put (im->sad, sa); |
| 462 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 463 | else /* create new SA */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | { |
| 465 | pool_get (im->sad, sa); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 466 | clib_memcpy (sa, new_sa, sizeof (*sa)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | sa_index = sa - im->sad; |
| 468 | hash_set (im->sa_index_by_sa_id, sa->id, sa_index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 469 | err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1); |
| 470 | if (err) |
| 471 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 477 | ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | { |
| 479 | ipsec_main_t *im = &ipsec_main; |
| 480 | uword *p; |
| 481 | u32 sa_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 482 | ipsec_sa_t *sa = 0; |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 483 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | |
| 485 | p = hash_get (im->sa_index_by_sa_id, sa_update->id); |
| 486 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 487 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | |
| 489 | sa_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 490 | sa = pool_elt_at_index (im->sad, sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 491 | |
| 492 | /* new crypto key */ |
| 493 | if (0 < sa_update->crypto_key_len) |
| 494 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 495 | clib_memcpy (sa->crypto_key, sa_update->crypto_key, |
| 496 | sa_update->crypto_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | sa->crypto_key_len = sa_update->crypto_key_len; |
| 498 | } |
| 499 | |
| 500 | /* new integ key */ |
| 501 | if (0 < sa_update->integ_key_len) |
| 502 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 503 | clib_memcpy (sa->integ_key, sa_update->integ_key, |
| 504 | sa_update->integ_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | sa->integ_key_len = sa_update->integ_key_len; |
| 506 | } |
| 507 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 508 | if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 509 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 510 | err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0); |
| 511 | if (err) |
| 512 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 519 | ipsec_rand_seed (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 521 | struct |
| 522 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | time_t time; |
| 524 | pid_t pid; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 525 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | } seed_data; |
| 527 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 528 | seed_data.time = time (NULL); |
| 529 | seed_data.pid = getpid (); |
| 530 | seed_data.p = (void *) &seed_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 532 | RAND_seed ((const void *) &seed_data, sizeof (seed_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static clib_error_t * |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 536 | ipsec_check_support (ipsec_sa_t * sa) |
| 537 | { |
| 538 | if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) |
| 539 | return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg"); |
| 540 | if (sa->integ_alg == IPSEC_INTEG_ALG_NONE) |
| 541 | return clib_error_return (0, "unsupported none integ-alg"); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 546 | clib_error_t * |
| 547 | ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add) |
| 548 | { |
| 549 | ipsec_ah_backend_t *ah = |
| 550 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 551 | if (ah->add_del_sa_sess_cb) |
| 552 | { |
| 553 | clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add); |
| 554 | if (err) |
| 555 | return err; |
| 556 | } |
| 557 | ipsec_esp_backend_t *esp = |
| 558 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 559 | if (esp->add_del_sa_sess_cb) |
| 560 | { |
| 561 | clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add); |
| 562 | if (err) |
| 563 | return err; |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | clib_error_t * |
| 569 | ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa) |
| 570 | { |
| 571 | clib_error_t *error = 0; |
| 572 | ipsec_ah_backend_t *ah = |
| 573 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 574 | ASSERT (ah->check_support_cb); |
| 575 | error = ah->check_support_cb (sa); |
| 576 | if (error) |
| 577 | return error; |
| 578 | ipsec_esp_backend_t *esp = |
| 579 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 580 | ASSERT (esp->check_support_cb); |
| 581 | error = esp->check_support_cb (sa); |
| 582 | return error; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | static void |
| 587 | ipsec_add_node (vlib_main_t * vm, const char *node_name, |
| 588 | const char *prev_node_name, u32 * out_node_index, |
| 589 | u32 * out_next_index) |
| 590 | { |
| 591 | vlib_node_t *prev_node, *node; |
| 592 | prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name); |
| 593 | ASSERT (prev_node); |
| 594 | node = vlib_get_node_by_name (vm, (u8 *) node_name); |
| 595 | ASSERT (node); |
| 596 | *out_node_index = node->index; |
| 597 | *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index); |
| 598 | } |
| 599 | |
| 600 | u32 |
| 601 | ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 602 | const char *name, |
| 603 | const char *ah4_encrypt_node_name, |
| 604 | const char *ah4_decrypt_node_name, |
| 605 | const char *ah6_encrypt_node_name, |
| 606 | const char *ah6_decrypt_node_name, |
| 607 | check_support_cb_t ah_check_support_cb, |
| 608 | add_del_sa_sess_cb_t ah_add_del_sa_sess_cb) |
| 609 | { |
| 610 | ipsec_ah_backend_t *b; |
| 611 | pool_get (im->ah_backends, b); |
| 612 | b->name = format (NULL, "%s", name); |
| 613 | |
| 614 | ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output", |
| 615 | &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index); |
| 616 | ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input", |
| 617 | &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index); |
| 618 | ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output", |
| 619 | &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index); |
| 620 | ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input", |
| 621 | &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index); |
| 622 | |
| 623 | b->check_support_cb = ah_check_support_cb; |
| 624 | b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb; |
| 625 | return b - im->ah_backends; |
| 626 | } |
| 627 | |
| 628 | u32 |
| 629 | ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 630 | const char *name, |
| 631 | const char *esp4_encrypt_node_name, |
| 632 | const char *esp4_decrypt_node_name, |
| 633 | const char *esp6_encrypt_node_name, |
| 634 | const char *esp6_decrypt_node_name, |
| 635 | check_support_cb_t esp_check_support_cb, |
| 636 | add_del_sa_sess_cb_t esp_add_del_sa_sess_cb) |
| 637 | { |
| 638 | ipsec_esp_backend_t *b; |
| 639 | pool_get (im->esp_backends, b); |
| 640 | b->name = format (NULL, "%s", name); |
| 641 | |
| 642 | ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output", |
| 643 | &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index); |
| 644 | ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input", |
| 645 | &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index); |
| 646 | ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output", |
| 647 | &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index); |
| 648 | ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input", |
| 649 | &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index); |
| 650 | |
| 651 | b->check_support_cb = esp_check_support_cb; |
| 652 | b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb; |
| 653 | return b - im->esp_backends; |
| 654 | } |
| 655 | |
| 656 | int |
| 657 | ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx) |
| 658 | { |
| 659 | if (pool_elts (im->sad) > 0 |
| 660 | || pool_is_free_index (im->ah_backends, backend_idx)) |
| 661 | { |
| 662 | return -1; |
| 663 | } |
| 664 | ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx); |
| 665 | im->ah_current_backend = backend_idx; |
| 666 | im->ah4_encrypt_node_index = b->ah4_encrypt_node_index; |
| 667 | im->ah4_decrypt_node_index = b->ah4_decrypt_node_index; |
| 668 | im->ah4_encrypt_next_index = b->ah4_encrypt_next_index; |
| 669 | im->ah4_decrypt_next_index = b->ah4_decrypt_next_index; |
| 670 | im->ah6_encrypt_node_index = b->ah6_encrypt_node_index; |
| 671 | im->ah6_decrypt_node_index = b->ah6_decrypt_node_index; |
| 672 | im->ah6_encrypt_next_index = b->ah6_encrypt_next_index; |
| 673 | im->ah6_decrypt_next_index = b->ah6_decrypt_next_index; |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int |
| 678 | ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx) |
| 679 | { |
| 680 | if (pool_elts (im->sad) > 0 |
| 681 | || pool_is_free_index (im->esp_backends, backend_idx)) |
| 682 | { |
| 683 | return -1; |
| 684 | } |
| 685 | ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx); |
| 686 | im->esp_current_backend = backend_idx; |
| 687 | im->esp4_encrypt_node_index = b->esp4_encrypt_node_index; |
| 688 | im->esp4_decrypt_node_index = b->esp4_decrypt_node_index; |
| 689 | im->esp4_encrypt_next_index = b->esp4_encrypt_next_index; |
| 690 | im->esp4_decrypt_next_index = b->esp4_decrypt_next_index; |
| 691 | im->esp6_encrypt_node_index = b->esp6_encrypt_node_index; |
| 692 | im->esp6_decrypt_node_index = b->esp6_decrypt_node_index; |
| 693 | im->esp6_encrypt_next_index = b->esp6_encrypt_next_index; |
| 694 | im->esp6_decrypt_next_index = b->esp6_decrypt_next_index; |
| 695 | return 0; |
| 696 | } |
| 697 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 698 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | ipsec_init (vlib_main_t * vm) |
| 700 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 701 | clib_error_t *error; |
| 702 | ipsec_main_t *im = &ipsec_main; |
| 703 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 705 | ipsec_rand_seed (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 707 | clib_memset (im, 0, sizeof (im[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 708 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 709 | im->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | im->vlib_main = vm; |
| 711 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 712 | im->spd_index_by_spd_id = hash_create (0, sizeof (uword)); |
| 713 | im->sa_index_by_sa_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
| 715 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 716 | vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1, |
| 717 | CLIB_CACHE_LINE_BYTES); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 718 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 719 | vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 720 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | im->error_drop_node_index = node->index; |
| 722 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 723 | u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend", |
| 724 | "ah4-encrypt", |
| 725 | "ah4-decrypt", |
| 726 | "ah6-encrypt", |
| 727 | "ah6-decrypt", |
| 728 | ipsec_check_support, |
| 729 | NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 731 | im->ah_default_backend = idx; |
| 732 | int rv = ipsec_select_ah_backend (im, idx); |
| 733 | ASSERT (0 == rv); |
| 734 | (void) (rv); // avoid warning |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 735 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 736 | idx = ipsec_register_esp_backend (vm, im, "default openssl backend", |
| 737 | "esp4-encrypt", |
| 738 | "esp4-decrypt", |
| 739 | "esp6-encrypt", |
| 740 | "esp6-decrypt", |
| 741 | ipsec_check_support, NULL); |
| 742 | im->esp_default_backend = idx; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 743 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 744 | rv = ipsec_select_esp_backend (im, idx); |
| 745 | ASSERT (0 == rv); |
| 746 | (void) (rv); // avoid warning |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 747 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 748 | if ((error = vlib_call_init_function (vm, ipsec_cli_init))) |
| 749 | return error; |
| 750 | |
| 751 | if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init))) |
| 752 | return error; |
| 753 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 754 | ipsec_proto_init (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | |
| 756 | if ((error = ikev2_init (vm))) |
| 757 | return error; |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | VLIB_INIT_FUNCTION (ipsec_init); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 763 | |
| 764 | /* |
| 765 | * fd.io coding-style-patch-verification: ON |
| 766 | * |
| 767 | * Local Variables: |
| 768 | * eval: (c-set-style "gnu") |
| 769 | * End: |
| 770 | */ |