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 */ |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 76 | vnet_feature_enable_disable ("ip4-output", "ipsec-output-ip4", sw_if_index, |
| 77 | is_add, 0, 0); |
| 78 | vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index, |
| 79 | is_add, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
| 81 | /* enable IPsec on RX */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 82 | vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index, |
| 83 | is_add, &config, sizeof (config)); |
| 84 | vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index, |
| 85 | is_add, &config, sizeof (config)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 91 | 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] | 92 | { |
| 93 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 94 | ipsec_spd_t *spd = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | uword *p; |
| 96 | u32 spd_index, k, v; |
| 97 | |
| 98 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 99 | if (p && is_add) |
| 100 | return VNET_API_ERROR_INVALID_VALUE; |
| 101 | if (!p && !is_add) |
| 102 | return VNET_API_ERROR_INVALID_VALUE; |
| 103 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 104 | if (!is_add) /* delete */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | { |
| 106 | spd_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 107 | spd = pool_elt_at_index (im->spds, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | if (!spd) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 109 | return VNET_API_ERROR_INVALID_VALUE; |
| 110 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | hash_foreach (k, v, im->spd_index_by_sw_if_index, ({ |
| 112 | if (v == spd_index) |
| 113 | ipsec_set_interface_spd(vm, k, spd_id, 0); |
| 114 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 115 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | hash_unset (im->spd_index_by_spd_id, spd_id); |
| 117 | pool_free (spd->policies); |
| 118 | vec_free (spd->ipv4_outbound_policies); |
| 119 | vec_free (spd->ipv6_outbound_policies); |
| 120 | vec_free (spd->ipv4_inbound_protect_policy_indices); |
| 121 | vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices); |
| 122 | pool_put (im->spds, spd); |
| 123 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 124 | else /* create new SPD */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
| 126 | pool_get (im->spds, spd); |
| 127 | memset (spd, 0, sizeof (*spd)); |
| 128 | spd_index = spd - im->spds; |
| 129 | spd->id = spd_id; |
| 130 | hash_set (im->spd_index_by_spd_id, spd_id, spd_index); |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 136 | ipsec_spd_entry_sort (void *a1, void *a2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 138 | u32 *id1 = a1; |
| 139 | u32 *id2 = a2; |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 140 | ipsec_spd_t *spd = ipsec_main.spd_to_sort; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 141 | ipsec_policy_t *p1, *p2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 143 | p1 = pool_elt_at_index (spd->policies, *id1); |
| 144 | p2 = pool_elt_at_index (spd->policies, *id2); |
| 145 | if (p1 && p2) |
| 146 | return p2->priority - p1->priority; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 152 | 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] | 153 | { |
| 154 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 155 | ipsec_spd_t *spd = 0; |
| 156 | ipsec_policy_t *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | uword *p; |
| 158 | u32 spd_index; |
| 159 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 160 | clib_warning ("policy-id %u priority %d is_outbound %u", policy->id, |
| 161 | policy->priority, policy->is_outbound); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
| 163 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 164 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 165 | p = hash_get (im->sa_index_by_sa_id, policy->sa_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 167 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | policy->sa_index = p[0]; |
| 169 | } |
| 170 | |
| 171 | p = hash_get (im->spd_index_by_spd_id, policy->id); |
| 172 | |
| 173 | if (!p) |
| 174 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 175 | |
| 176 | spd_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 177 | spd = pool_elt_at_index (im->spds, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | if (!spd) |
| 179 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 180 | |
| 181 | if (is_add) |
| 182 | { |
| 183 | u32 policy_index; |
| 184 | |
| 185 | pool_get (spd->policies, vp); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 186 | clib_memcpy (vp, policy, sizeof (*vp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | policy_index = vp - spd->policies; |
| 188 | |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 189 | ipsec_main.spd_to_sort = spd; |
| 190 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | if (policy->is_outbound) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 192 | { |
| 193 | if (policy->is_ipv6) |
| 194 | { |
| 195 | vec_add1 (spd->ipv6_outbound_policies, policy_index); |
| 196 | clib_memcpy (vp, policy, sizeof (ipsec_policy_t)); |
| 197 | vec_sort_with_function (spd->ipv6_outbound_policies, |
| 198 | ipsec_spd_entry_sort); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | vec_add1 (spd->ipv4_outbound_policies, policy_index); |
| 203 | clib_memcpy (vp, policy, sizeof (ipsec_policy_t)); |
| 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); |
| 216 | clib_memcpy (vp, policy, sizeof (ipsec_policy_t)); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 217 | vec_sort_with_function |
| 218 | (spd->ipv6_inbound_protect_policy_indices, |
| 219 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 220 | } |
| 221 | else |
| 222 | { |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 223 | vec_add1 |
| 224 | (spd->ipv6_inbound_policy_discard_and_bypass_indices, |
| 225 | policy_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 226 | clib_memcpy (vp, policy, sizeof (ipsec_policy_t)); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 227 | vec_sort_with_function |
| 228 | (spd->ipv6_inbound_policy_discard_and_bypass_indices, |
| 229 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 235 | { |
| 236 | vec_add1 (spd->ipv4_inbound_protect_policy_indices, |
| 237 | policy_index); |
| 238 | clib_memcpy (vp, policy, sizeof (ipsec_policy_t)); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 239 | vec_sort_with_function |
| 240 | (spd->ipv4_inbound_protect_policy_indices, |
| 241 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 245 | vec_add1 |
| 246 | (spd->ipv4_inbound_policy_discard_and_bypass_indices, |
| 247 | policy_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 248 | clib_memcpy (vp, policy, sizeof (ipsec_policy_t)); |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 249 | vec_sort_with_function |
| 250 | (spd->ipv4_inbound_policy_discard_and_bypass_indices, |
| 251 | ipsec_spd_entry_sort); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 256 | ipsec_main.spd_to_sort = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | } |
| 258 | else |
| 259 | { |
| 260 | u32 i, j; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 261 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | pool_foreach_index(i, spd->policies, ({ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 263 | vp = pool_elt_at_index(spd->policies, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | if (vp->priority != policy->priority) |
| 265 | continue; |
| 266 | if (vp->is_outbound != policy->is_outbound) |
| 267 | continue; |
| 268 | if (vp->policy != policy->policy) |
| 269 | continue; |
| 270 | if (vp->sa_id != policy->sa_id) |
| 271 | continue; |
| 272 | if (vp->protocol != policy->protocol) |
| 273 | continue; |
| 274 | if (vp->lport.start != policy->lport.start) |
| 275 | continue; |
| 276 | if (vp->lport.stop != policy->lport.stop) |
| 277 | continue; |
| 278 | if (vp->rport.start != policy->rport.start) |
| 279 | continue; |
| 280 | if (vp->rport.stop != policy->rport.stop) |
| 281 | continue; |
| 282 | if (vp->is_ipv6 != policy->is_ipv6) |
| 283 | continue; |
| 284 | if (policy->is_ipv6) |
| 285 | { |
| 286 | if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0]) |
| 287 | continue; |
| 288 | if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1]) |
| 289 | continue; |
| 290 | if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0]) |
| 291 | continue; |
| 292 | if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1]) |
| 293 | continue; |
| 294 | if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0]) |
| 295 | continue; |
| 296 | if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1]) |
| 297 | continue; |
| 298 | if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0]) |
| 299 | continue; |
| 300 | if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1]) |
| 301 | continue; |
| 302 | if (policy->is_outbound) |
| 303 | { |
| 304 | vec_foreach_index(j, spd->ipv6_outbound_policies) { |
| 305 | if (vec_elt(spd->ipv6_outbound_policies, j) == i) { |
| 306 | vec_del1 (spd->ipv6_outbound_policies, j); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 314 | { |
| 315 | vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) { |
| 316 | if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) { |
| 317 | vec_del1 (spd->ipv6_inbound_protect_policy_indices, j); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) { |
| 325 | if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) { |
| 326 | vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j); |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32) |
| 336 | continue; |
| 337 | if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32) |
| 338 | continue; |
| 339 | if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32) |
| 340 | continue; |
| 341 | if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32) |
| 342 | continue; |
| 343 | if (policy->is_outbound) |
| 344 | { |
| 345 | vec_foreach_index(j, spd->ipv4_outbound_policies) { |
| 346 | if (vec_elt(spd->ipv4_outbound_policies, j) == i) { |
| 347 | vec_del1 (spd->ipv4_outbound_policies, j); |
| 348 | break; |
| 349 | } |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 350 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | } |
| 352 | else |
| 353 | { |
| 354 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 355 | { |
| 356 | vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) { |
| 357 | if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) { |
| 358 | vec_del1 (spd->ipv4_inbound_protect_policy_indices, j); |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) { |
| 366 | if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) { |
| 367 | vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j); |
| 368 | break; |
Matus Fabian | ce8805c | 2018-03-16 05:44:24 -0700 | [diff] [blame] | 369 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | pool_put (spd->policies, vp); |
| 375 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 377 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 383 | u8 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 384 | ipsec_is_sa_used (u32 sa_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 386 | ipsec_main_t *im = &ipsec_main; |
| 387 | ipsec_spd_t *spd; |
| 388 | ipsec_policy_t *p; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 389 | ipsec_tunnel_if_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 391 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | pool_foreach(spd, im->spds, ({ |
| 393 | pool_foreach(p, spd->policies, ({ |
| 394 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 395 | { |
| 396 | if (p->sa_index == sa_index) |
| 397 | return 1; |
| 398 | } |
| 399 | })); |
| 400 | })); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 401 | |
| 402 | pool_foreach(t, im->tunnel_interfaces, ({ |
| 403 | if (t->input_sa_index == sa_index) |
| 404 | return 1; |
| 405 | if (t->output_sa_index == sa_index) |
| 406 | return 1; |
| 407 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 408 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | int |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 414 | 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] | 415 | { |
| 416 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 417 | ipsec_sa_t *sa = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | uword *p; |
| 419 | u32 sa_index; |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 420 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 422 | clib_warning ("id %u spi %u", new_sa->id, new_sa->spi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | |
| 424 | p = hash_get (im->sa_index_by_sa_id, new_sa->id); |
| 425 | if (p && is_add) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 426 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | if (!p && !is_add) |
| 428 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 429 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 430 | if (!is_add) /* delete */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | { |
| 432 | sa_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 433 | sa = pool_elt_at_index (im->sad, sa_index); |
| 434 | if (ipsec_is_sa_used (sa_index)) |
| 435 | { |
| 436 | clib_warning ("sa_id %u used in policy", sa->id); |
| 437 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */ |
| 438 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | hash_unset (im->sa_index_by_sa_id, sa->id); |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 440 | if (im->cb.add_del_sa_sess_cb) |
| 441 | { |
| 442 | err = im->cb.add_del_sa_sess_cb (sa_index, 0); |
| 443 | if (err) |
| 444 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 445 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | pool_put (im->sad, sa); |
| 447 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 448 | else /* create new SA */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | { |
| 450 | pool_get (im->sad, sa); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 451 | clib_memcpy (sa, new_sa, sizeof (*sa)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | sa_index = sa - im->sad; |
| 453 | hash_set (im->sa_index_by_sa_id, sa->id, sa_index); |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 454 | if (im->cb.add_del_sa_sess_cb) |
| 455 | { |
| 456 | err = im->cb.add_del_sa_sess_cb (sa_index, 1); |
| 457 | if (err) |
| 458 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 459 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | } |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 465 | 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] | 466 | { |
| 467 | ipsec_main_t *im = &ipsec_main; |
| 468 | uword *p; |
| 469 | u32 sa_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 470 | ipsec_sa_t *sa = 0; |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 471 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
| 473 | p = hash_get (im->sa_index_by_sa_id, sa_update->id); |
| 474 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 475 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | |
| 477 | sa_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 478 | sa = pool_elt_at_index (im->sad, sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | |
| 480 | /* new crypto key */ |
| 481 | if (0 < sa_update->crypto_key_len) |
| 482 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 483 | clib_memcpy (sa->crypto_key, sa_update->crypto_key, |
| 484 | sa_update->crypto_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | sa->crypto_key_len = sa_update->crypto_key_len; |
| 486 | } |
| 487 | |
| 488 | /* new integ key */ |
| 489 | if (0 < sa_update->integ_key_len) |
| 490 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 491 | clib_memcpy (sa->integ_key, sa_update->integ_key, |
| 492 | sa_update->integ_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | sa->integ_key_len = sa_update->integ_key_len; |
| 494 | } |
| 495 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 496 | 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] | 497 | { |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 498 | if (im->cb.add_del_sa_sess_cb) |
| 499 | { |
| 500 | err = im->cb.add_del_sa_sess_cb (sa_index, 0); |
| 501 | if (err) |
| 502 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 503 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 510 | ipsec_rand_seed (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 512 | struct |
| 513 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | time_t time; |
| 515 | pid_t pid; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 516 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | } seed_data; |
| 518 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 519 | seed_data.time = time (NULL); |
| 520 | seed_data.pid = getpid (); |
| 521 | seed_data.p = (void *) &seed_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 523 | RAND_seed ((const void *) &seed_data, sizeof (seed_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static clib_error_t * |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 527 | ipsec_check_support (ipsec_sa_t * sa) |
| 528 | { |
| 529 | if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) |
| 530 | return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg"); |
| 531 | if (sa->integ_alg == IPSEC_INTEG_ALG_NONE) |
| 532 | return clib_error_return (0, "unsupported none integ-alg"); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | ipsec_init (vlib_main_t * vm) |
| 539 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 540 | clib_error_t *error; |
| 541 | ipsec_main_t *im = &ipsec_main; |
| 542 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 543 | vlib_node_t *node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 545 | ipsec_rand_seed (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
| 547 | memset (im, 0, sizeof (im[0])); |
| 548 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 549 | im->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | im->vlib_main = vm; |
| 551 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 552 | im->spd_index_by_spd_id = hash_create (0, sizeof (uword)); |
| 553 | im->sa_index_by_sa_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 554 | im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
| 555 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 556 | vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1, |
| 557 | CLIB_CACHE_LINE_BYTES); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 558 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 560 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | im->error_drop_node_index = node->index; |
| 562 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 563 | node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 564 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 565 | im->esp_encrypt_node_index = node->index; |
| 566 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 567 | node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 568 | ASSERT (node); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 569 | im->esp_decrypt_node_index = node->index; |
| 570 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 571 | node = vlib_get_node_by_name (vm, (u8 *) "ah-encrypt"); |
| 572 | ASSERT (node); |
| 573 | im->ah_encrypt_node_index = node->index; |
| 574 | |
| 575 | node = vlib_get_node_by_name (vm, (u8 *) "ah-decrypt"); |
| 576 | ASSERT (node); |
| 577 | im->ah_decrypt_node_index = node->index; |
| 578 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 579 | im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT; |
| 580 | im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 581 | im->ah_encrypt_next_index = IPSEC_OUTPUT_NEXT_AH_ENCRYPT; |
| 582 | im->ah_decrypt_next_index = IPSEC_INPUT_NEXT_AH_DECRYPT; |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 583 | |
| 584 | im->cb.check_support_cb = ipsec_check_support; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 585 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | if ((error = vlib_call_init_function (vm, ipsec_cli_init))) |
| 587 | return error; |
| 588 | |
| 589 | if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init))) |
| 590 | return error; |
| 591 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 592 | ipsec_proto_init (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 593 | |
| 594 | if ((error = ikev2_init (vm))) |
| 595 | return error; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | VLIB_INIT_FUNCTION (ipsec_init); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * fd.io coding-style-patch-verification: ON |
| 604 | * |
| 605 | * Local Variables: |
| 606 | * eval: (c-set-style "gnu") |
| 607 | * End: |
| 608 | */ |