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> |
| 22 | |
| 23 | #include <vnet/ipsec/ipsec.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | #include <vnet/ipsec/ikev2.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 25 | #include <vnet/ipsec/esp.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 26 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 27 | ipsec_main_t ipsec_main; |
| 28 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 29 | u32 |
| 30 | ipsec_get_sa_index_by_sa_id (u32 sa_id) |
| 31 | { |
| 32 | ipsec_main_t *im = &ipsec_main; |
| 33 | uword *p = hash_get (im->sa_index_by_sa_id, sa_id); |
| 34 | if (!p) |
| 35 | return ~0; |
| 36 | |
| 37 | return p[0]; |
| 38 | } |
| 39 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 41 | ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id, |
| 42 | int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | { |
| 44 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | ip4_ipsec_config_t config; |
| 46 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 47 | u32 spd_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | uword *p; |
| 49 | |
| 50 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 51 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 52 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
| 54 | spd_index = p[0]; |
| 55 | |
| 56 | p = hash_get (im->spd_index_by_sw_if_index, sw_if_index); |
| 57 | if (p && is_add) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 58 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
| 60 | if (is_add) |
| 61 | { |
| 62 | hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | hash_unset (im->spd_index_by_sw_if_index, sw_if_index); |
| 67 | } |
| 68 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 69 | clib_warning ("sw_if_index %u spd_id %u spd_index %u", |
| 70 | sw_if_index, spd_id, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
| 72 | /* enable IPsec on TX */ |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 73 | vnet_feature_enable_disable ("ip4-output", "ipsec-output-ip4", sw_if_index, |
| 74 | is_add, 0, 0); |
| 75 | vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index, |
| 76 | is_add, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | /* enable IPsec on RX */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 79 | vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index, |
| 80 | is_add, &config, sizeof (config)); |
| 81 | vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index, |
| 82 | is_add, &config, sizeof (config)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 88 | 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] | 89 | { |
| 90 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 91 | ipsec_spd_t *spd = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | uword *p; |
| 93 | u32 spd_index, k, v; |
| 94 | |
| 95 | p = hash_get (im->spd_index_by_spd_id, spd_id); |
| 96 | if (p && is_add) |
| 97 | return VNET_API_ERROR_INVALID_VALUE; |
| 98 | if (!p && !is_add) |
| 99 | return VNET_API_ERROR_INVALID_VALUE; |
| 100 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 101 | if (!is_add) /* delete */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | { |
| 103 | spd_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 104 | spd = pool_elt_at_index (im->spds, spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | if (!spd) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 106 | return VNET_API_ERROR_INVALID_VALUE; |
| 107 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | hash_foreach (k, v, im->spd_index_by_sw_if_index, ({ |
| 109 | if (v == spd_index) |
| 110 | ipsec_set_interface_spd(vm, k, spd_id, 0); |
| 111 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 112 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | hash_unset (im->spd_index_by_spd_id, spd_id); |
| 114 | pool_free (spd->policies); |
| 115 | vec_free (spd->ipv4_outbound_policies); |
| 116 | vec_free (spd->ipv6_outbound_policies); |
| 117 | vec_free (spd->ipv4_inbound_protect_policy_indices); |
| 118 | vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices); |
| 119 | pool_put (im->spds, spd); |
| 120 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 121 | else /* create new SPD */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | { |
| 123 | pool_get (im->spds, spd); |
| 124 | memset (spd, 0, sizeof (*spd)); |
| 125 | spd_index = spd - im->spds; |
| 126 | spd->id = spd_id; |
| 127 | hash_set (im->spd_index_by_spd_id, spd_id, spd_index); |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 133 | ipsec_spd_entry_sort (void *a1, void *a2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | { |
| 135 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 136 | u32 *id1 = a1; |
| 137 | u32 *id2 = a2; |
| 138 | ipsec_spd_t *spd; |
| 139 | ipsec_policy_t *p1, *p2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 141 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | pool_foreach (spd, im->spds, ({ |
| 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; |
| 147 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 148 | /* *INDENT-ON* */ |
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 | |
| 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 | |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | u32 i, j; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 260 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | pool_foreach_index(i, spd->policies, ({ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 262 | vp = pool_elt_at_index(spd->policies, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | if (vp->priority != policy->priority) |
| 264 | continue; |
| 265 | if (vp->is_outbound != policy->is_outbound) |
| 266 | continue; |
| 267 | if (vp->policy != policy->policy) |
| 268 | continue; |
| 269 | if (vp->sa_id != policy->sa_id) |
| 270 | continue; |
| 271 | if (vp->protocol != policy->protocol) |
| 272 | continue; |
| 273 | if (vp->lport.start != policy->lport.start) |
| 274 | continue; |
| 275 | if (vp->lport.stop != policy->lport.stop) |
| 276 | continue; |
| 277 | if (vp->rport.start != policy->rport.start) |
| 278 | continue; |
| 279 | if (vp->rport.stop != policy->rport.stop) |
| 280 | continue; |
| 281 | if (vp->is_ipv6 != policy->is_ipv6) |
| 282 | continue; |
| 283 | if (policy->is_ipv6) |
| 284 | { |
| 285 | if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0]) |
| 286 | continue; |
| 287 | if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1]) |
| 288 | continue; |
| 289 | if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0]) |
| 290 | continue; |
| 291 | if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1]) |
| 292 | continue; |
| 293 | if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0]) |
| 294 | continue; |
| 295 | if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1]) |
| 296 | continue; |
| 297 | if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0]) |
| 298 | continue; |
| 299 | if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1]) |
| 300 | continue; |
| 301 | if (policy->is_outbound) |
| 302 | { |
| 303 | vec_foreach_index(j, spd->ipv6_outbound_policies) { |
| 304 | if (vec_elt(spd->ipv6_outbound_policies, j) == i) { |
| 305 | vec_del1 (spd->ipv6_outbound_policies, j); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 313 | { |
| 314 | vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) { |
| 315 | if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) { |
| 316 | vec_del1 (spd->ipv6_inbound_protect_policy_indices, j); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) { |
| 324 | if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) { |
| 325 | vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j); |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32) |
| 335 | continue; |
| 336 | if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32) |
| 337 | continue; |
| 338 | if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32) |
| 339 | continue; |
| 340 | if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32) |
| 341 | continue; |
| 342 | if (policy->is_outbound) |
| 343 | { |
| 344 | vec_foreach_index(j, spd->ipv4_outbound_policies) { |
| 345 | if (vec_elt(spd->ipv4_outbound_policies, j) == i) { |
| 346 | vec_del1 (spd->ipv4_outbound_policies, j); |
| 347 | break; |
| 348 | } |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 349 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | } |
| 351 | else |
| 352 | { |
| 353 | if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 354 | { |
| 355 | vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) { |
| 356 | if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) { |
| 357 | vec_del1 (spd->ipv4_inbound_protect_policy_indices, j); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) { |
| 365 | if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) { |
| 366 | vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | pool_put (spd->policies, vp); |
| 373 | break; |
| 374 | } |
| 375 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 376 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 382 | u8 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 383 | ipsec_is_sa_used (u32 sa_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 385 | ipsec_main_t *im = &ipsec_main; |
| 386 | ipsec_spd_t *spd; |
| 387 | ipsec_policy_t *p; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 388 | ipsec_tunnel_if_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 390 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | pool_foreach(spd, im->spds, ({ |
| 392 | pool_foreach(p, spd->policies, ({ |
| 393 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) |
| 394 | { |
| 395 | if (p->sa_index == sa_index) |
| 396 | return 1; |
| 397 | } |
| 398 | })); |
| 399 | })); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 400 | |
| 401 | pool_foreach(t, im->tunnel_interfaces, ({ |
| 402 | if (t->input_sa_index == sa_index) |
| 403 | return 1; |
| 404 | if (t->output_sa_index == sa_index) |
| 405 | return 1; |
| 406 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 407 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 413 | 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] | 414 | { |
| 415 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 416 | ipsec_sa_t *sa = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | uword *p; |
| 418 | u32 sa_index; |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 419 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 421 | clib_warning ("id %u spi %u", new_sa->id, new_sa->spi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | |
| 423 | p = hash_get (im->sa_index_by_sa_id, new_sa->id); |
| 424 | if (p && is_add) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 425 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | if (!p && !is_add) |
| 427 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 428 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 429 | if (!is_add) /* delete */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | { |
| 431 | sa_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 432 | sa = pool_elt_at_index (im->sad, sa_index); |
| 433 | if (ipsec_is_sa_used (sa_index)) |
| 434 | { |
| 435 | clib_warning ("sa_id %u used in policy", sa->id); |
| 436 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */ |
| 437 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | hash_unset (im->sa_index_by_sa_id, sa->id); |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 439 | if (im->cb.add_del_sa_sess_cb) |
| 440 | { |
| 441 | err = im->cb.add_del_sa_sess_cb (sa_index, 0); |
| 442 | if (err) |
| 443 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 444 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | pool_put (im->sad, sa); |
| 446 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 447 | else /* create new SA */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | { |
| 449 | pool_get (im->sad, sa); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 450 | clib_memcpy (sa, new_sa, sizeof (*sa)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | sa_index = sa - im->sad; |
| 452 | 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] | 453 | if (im->cb.add_del_sa_sess_cb) |
| 454 | { |
| 455 | err = im->cb.add_del_sa_sess_cb (sa_index, 1); |
| 456 | if (err) |
| 457 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 458 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 464 | 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] | 465 | { |
| 466 | ipsec_main_t *im = &ipsec_main; |
| 467 | uword *p; |
| 468 | u32 sa_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 469 | ipsec_sa_t *sa = 0; |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 470 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | |
| 472 | p = hash_get (im->sa_index_by_sa_id, sa_update->id); |
| 473 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 474 | return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | |
| 476 | sa_index = p[0]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 477 | sa = pool_elt_at_index (im->sad, sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | |
| 479 | /* new crypto key */ |
| 480 | if (0 < sa_update->crypto_key_len) |
| 481 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 482 | clib_memcpy (sa->crypto_key, sa_update->crypto_key, |
| 483 | sa_update->crypto_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | sa->crypto_key_len = sa_update->crypto_key_len; |
| 485 | } |
| 486 | |
| 487 | /* new integ key */ |
| 488 | if (0 < sa_update->integ_key_len) |
| 489 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 490 | clib_memcpy (sa->integ_key, sa_update->integ_key, |
| 491 | sa_update->integ_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | sa->integ_key_len = sa_update->integ_key_len; |
| 493 | } |
| 494 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 495 | 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] | 496 | { |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 497 | if (im->cb.add_del_sa_sess_cb) |
| 498 | { |
| 499 | err = im->cb.add_del_sa_sess_cb (sa_index, 0); |
| 500 | if (err) |
| 501 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 502 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 509 | ipsec_rand_seed (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 511 | struct |
| 512 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | time_t time; |
| 514 | pid_t pid; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 515 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | } seed_data; |
| 517 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 518 | seed_data.time = time (NULL); |
| 519 | seed_data.pid = getpid (); |
| 520 | seed_data.p = (void *) &seed_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 522 | RAND_seed ((const void *) &seed_data, sizeof (seed_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static clib_error_t * |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 526 | ipsec_check_support (ipsec_sa_t * sa) |
| 527 | { |
| 528 | if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) |
| 529 | return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg"); |
| 530 | if (sa->integ_alg == IPSEC_INTEG_ALG_NONE) |
| 531 | return clib_error_return (0, "unsupported none integ-alg"); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | ipsec_init (vlib_main_t * vm) |
| 538 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 539 | clib_error_t *error; |
| 540 | ipsec_main_t *im = &ipsec_main; |
| 541 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 542 | vlib_node_t *node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 544 | ipsec_rand_seed (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 545 | |
| 546 | memset (im, 0, sizeof (im[0])); |
| 547 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 548 | im->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | im->vlib_main = vm; |
| 550 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 551 | im->spd_index_by_spd_id = hash_create (0, sizeof (uword)); |
| 552 | im->sa_index_by_sa_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
| 554 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 555 | vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1, |
| 556 | CLIB_CACHE_LINE_BYTES); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 557 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 559 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | im->error_drop_node_index = node->index; |
| 561 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 562 | node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 563 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | im->esp_encrypt_node_index = node->index; |
| 565 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 566 | node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 567 | ASSERT (node); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 568 | im->esp_decrypt_node_index = node->index; |
| 569 | |
| 570 | im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT; |
| 571 | im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT; |
| 572 | |
| 573 | im->cb.check_support_cb = ipsec_check_support; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 574 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | if ((error = vlib_call_init_function (vm, ipsec_cli_init))) |
| 576 | return error; |
| 577 | |
| 578 | if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init))) |
| 579 | return error; |
| 580 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 581 | esp_init (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | |
| 583 | if ((error = ikev2_init (vm))) |
| 584 | return error; |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | VLIB_INIT_FUNCTION (ipsec_init); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * fd.io coding-style-patch-verification: ON |
| 593 | * |
| 594 | * Local Variables: |
| 595 | * eval: (c-set-style "gnu") |
| 596 | * End: |
| 597 | */ |