blob: bb62560cccf4d6d4607dbd052af307594b1bd697 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Sekera4b089f22018-04-17 18:04:57 +020022#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <vnet/ipsec/ikev2.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000026#include <vnet/ipsec/esp.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080027#include <vnet/ipsec/ah.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000028
Klement Sekera4b089f22018-04-17 18:04:57 +020029
Dave Wallace71612d62017-10-24 01:32:41 -040030ipsec_main_t ipsec_main;
31
Matus Fabian694265d2016-08-10 01:55:36 -070032u32
33ipsec_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 Warnickecb9cada2015-12-08 15:45:58 -070043int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
45 int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
47 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 ip4_ipsec_config_t config;
49
Damjan Marion8b3191e2016-11-09 19:54:20 +010050 u32 spd_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 uword *p;
52
53 p = hash_get (im->spd_index_by_spd_id, spd_id);
54 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070055 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
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)166a9d42016-08-06 11:00:56 -070061 return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
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)166a9d42016-08-06 11:00:56 -070072 clib_warning ("sw_if_index %u spd_id %u spd_index %u",
73 sw_if_index, spd_id, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75 /* enable IPsec on TX */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020076 vnet_feature_enable_disable ("ip4-output", "ipsec4-output", sw_if_index,
Matus Fabian08a6f012016-11-15 06:08:51 -080077 is_add, 0, 0);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020078 vnet_feature_enable_disable ("ip6-output", "ipsec6-output", sw_if_index,
Matus Fabian08a6f012016-11-15 06:08:51 -080079 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Kingwel Xiefc91a172018-09-26 05:07:09 -040081 config.spd_index = spd_index;
82
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 /* enable IPsec on RX */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020084 vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input", sw_if_index,
Damjan Marion8b3191e2016-11-09 19:54:20 +010085 is_add, &config, sizeof (config));
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020086 vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input", sw_if_index,
Damjan Marion8b3191e2016-11-09 19:54:20 +010087 is_add, &config, sizeof (config));
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89 return 0;
90}
91
92int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070093ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
95 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070096 ipsec_spd_t *spd = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 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)166a9d42016-08-06 11:00:56 -0700106 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 {
108 spd_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700109 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 if (!spd)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700111 return VNET_API_ERROR_INVALID_VALUE;
112 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 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)166a9d42016-08-06 11:00:56 -0700117 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 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)166a9d42016-08-06 11:00:56 -0700126 else /* create new SPD */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 {
128 pool_get (im->spds, spd);
Dave Barachb7b92992018-10-17 10:38:51 -0400129 clib_memset (spd, 0, sizeof (*spd));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 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
137static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700138ipsec_spd_entry_sort (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700140 u32 *id1 = a1;
141 u32 *id2 = a2;
Klement Sekeraee52d872018-06-07 19:36:07 +0200142 ipsec_spd_t *spd = ipsec_main.spd_to_sort;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700143 ipsec_policy_t *p1, *p2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Klement Sekeraee52d872018-06-07 19:36:07 +0200145 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 Warnickecb9cada2015-12-08 15:45:58 -0700149
150 return 0;
151}
152
153int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700154ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
156 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157 ipsec_spd_t *spd = 0;
158 ipsec_policy_t *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 uword *p;
160 u32 spd_index;
161
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700162 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
163 policy->priority, policy->is_outbound);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
165 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
166 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700167 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700169 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 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)166a9d42016-08-06 11:00:56 -0700179 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 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 Marionf1213b82016-03-13 02:22:06 +0100188 clib_memcpy (vp, policy, sizeof (*vp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 policy_index = vp - spd->policies;
190
Klement Sekeraee52d872018-06-07 19:36:07 +0200191 ipsec_main.spd_to_sort = spd;
192
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 if (policy->is_outbound)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700194 {
195 if (policy->is_ipv6)
196 {
197 vec_add1 (spd->ipv6_outbound_policies, policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700198 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)166a9d42016-08-06 11:00:56 -0700204 vec_sort_with_function (spd->ipv4_outbound_policies,
205 ipsec_spd_entry_sort);
206 }
207 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700209 {
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 Warnicke853e7202016-08-12 11:42:26 -0700216 vec_sort_with_function
217 (spd->ipv6_inbound_protect_policy_indices,
218 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700219 }
220 else
221 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700222 vec_add1
223 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
224 policy_index);
Ed Warnicke853e7202016-08-12 11:42:26 -0700225 vec_sort_with_function
226 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
227 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700228 }
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 Warnicke853e7202016-08-12 11:42:26 -0700236 vec_sort_with_function
237 (spd->ipv4_inbound_protect_policy_indices,
238 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700239 }
240 else
241 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700242 vec_add1
243 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
244 policy_index);
Ed Warnicke853e7202016-08-12 11:42:26 -0700245 vec_sort_with_function
246 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
247 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 }
249 }
250 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Klement Sekeraee52d872018-06-07 19:36:07 +0200252 ipsec_main.spd_to_sort = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 }
254 else
255 {
256 u32 i, j;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700257 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 pool_foreach_index(i, spd->policies, ({
Damjan Marion607de1a2016-08-16 22:53:54 +0200259 vp = pool_elt_at_index(spd->policies, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 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 Marion607de1a2016-08-16 22:53:54 +0200346 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 }
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 Fabiance8805c2018-03-16 05:44:24 -0700365 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 }
367 }
368 }
369 }
370 pool_put (spd->policies, vp);
371 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700373 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 }
375
376 return 0;
377}
378
Matthew Smithca514fd2017-10-12 12:06:59 -0500379u8
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700380ipsec_is_sa_used (u32 sa_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700382 ipsec_main_t *im = &ipsec_main;
383 ipsec_spd_t *spd;
384 ipsec_policy_t *p;
Matus Fabian694265d2016-08-10 01:55:36 -0700385 ipsec_tunnel_if_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700387 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 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 Fabian694265d2016-08-10 01:55:36 -0700397
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)166a9d42016-08-06 11:00:56 -0700404 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
406 return 0;
407}
408
Klement Sekerab4d30532018-11-08 13:00:02 +0100409clib_error_t *
410ipsec_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 Warnickecb9cada2015-12-08 15:45:58 -0700431int
Radu Nicolau717de092018-08-03 10:37:24 +0100432ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433{
434 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700435 ipsec_sa_t *sa = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 uword *p;
437 u32 sa_index;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100438 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700440 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
442 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
443 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700444 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 if (!p && !is_add)
446 return VNET_API_ERROR_SYSCALL_ERROR_1;
447
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700448 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 {
450 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700451 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 Warnickecb9cada2015-12-08 15:45:58 -0700457 hash_unset (im->sa_index_by_sa_id, sa->id);
Klement Sekerab4d30532018-11-08 13:00:02 +0100458 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
459 if (err)
460 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 pool_put (im->sad, sa);
462 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700463 else /* create new SA */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 {
465 pool_get (im->sad, sa);
Damjan Marionf1213b82016-03-13 02:22:06 +0100466 clib_memcpy (sa, new_sa, sizeof (*sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 sa_index = sa - im->sad;
468 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100469 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
470 if (err)
471 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 }
473 return 0;
474}
475
476int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700477ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478{
479 ipsec_main_t *im = &ipsec_main;
480 uword *p;
481 u32 sa_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700482 ipsec_sa_t *sa = 0;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100483 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
485 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
486 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700487 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
489 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700490 sa = pool_elt_at_index (im->sad, sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
492 /* new crypto key */
493 if (0 < sa_update->crypto_key_len)
494 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700495 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
496 sa_update->crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 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)166a9d42016-08-06 11:00:56 -0700503 clib_memcpy (sa->integ_key, sa_update->integ_key,
504 sa_update->integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 sa->integ_key_len = sa_update->integ_key_len;
506 }
507
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100508 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000509 {
Klement Sekerab4d30532018-11-08 13:00:02 +0100510 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
511 if (err)
512 return VNET_API_ERROR_SYSCALL_ERROR_1;
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000513 }
514
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 return 0;
516}
517
518static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700519ipsec_rand_seed (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700521 struct
522 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 time_t time;
524 pid_t pid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700525 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 } seed_data;
527
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700528 seed_data.time = time (NULL);
529 seed_data.pid = getpid ();
530 seed_data.p = (void *) &seed_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700532 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533}
534
535static clib_error_t *
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000536ipsec_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 Monroyd04b60b2017-01-20 15:35:23 +0000542
543 return 0;
544}
545
Klement Sekerab4d30532018-11-08 13:00:02 +0100546clib_error_t *
547ipsec_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
568clib_error_t *
569ipsec_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
586static void
587ipsec_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
600u32
601ipsec_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
628u32
629ipsec_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
656int
657ipsec_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
677int
678ipsec_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 Monroyd04b60b2017-01-20 15:35:23 +0000698static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699ipsec_init (vlib_main_t * vm)
700{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700701 clib_error_t *error;
702 ipsec_main_t *im = &ipsec_main;
703 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700705 ipsec_rand_seed ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
Dave Barachb7b92992018-10-17 10:38:51 -0400707 clib_memset (im, 0, sizeof (im[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700709 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 im->vlib_main = vm;
711
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700712 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
713 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
715
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700716 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
717 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500718
Klement Sekerab4d30532018-11-08 13:00:02 +0100719 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700720 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 im->error_drop_node_index = node->index;
722
Klement Sekerab4d30532018-11-08 13:00:02 +0100723 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 Warnickecb9cada2015-12-08 15:45:58 -0700730
Klement Sekerab4d30532018-11-08 13:00:02 +0100731 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 Monroyd04b60b2017-01-20 15:35:23 +0000735
Klement Sekerab4d30532018-11-08 13:00:02 +0100736 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”430ac932017-11-23 02:39:33 -0800743
Klement Sekerab4d30532018-11-08 13:00:02 +0100744 rv = ipsec_select_esp_backend (im, idx);
745 ASSERT (0 == rv);
746 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 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”430ac932017-11-23 02:39:33 -0800754 ipsec_proto_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
756 if ((error = ikev2_init (vm)))
757 return error;
758
759 return 0;
760}
761
762VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700763
764/*
765 * fd.io coding-style-patch-verification: ON
766 *
767 * Local Variables:
768 * eval: (c-set-style "gnu")
769 * End:
770 */