blob: 928cafd5e25a38dd00230f2d38bd432ffa2d904b [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 */
Matus Fabian08a6f012016-11-15 06:08:51 -080076 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 Warnickecb9cada2015-12-08 15:45:58 -070080
81 /* enable IPsec on RX */
Damjan Marion8b3191e2016-11-09 19:54:20 +010082 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 Warnickecb9cada2015-12-08 15:45:58 -070086
87 return 0;
88}
89
90int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070091ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092{
93 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070094 ipsec_spd_t *spd = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 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)166a9d42016-08-06 11:00:56 -0700104 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 {
106 spd_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 if (!spd)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700109 return VNET_API_ERROR_INVALID_VALUE;
110 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 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)166a9d42016-08-06 11:00:56 -0700115 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 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)166a9d42016-08-06 11:00:56 -0700124 else /* create new SPD */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 {
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
135static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700136ipsec_spd_entry_sort (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137{
138 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700139 u32 *id1 = a1;
140 u32 *id2 = a2;
141 ipsec_spd_t *spd;
142 ipsec_policy_t *p1, *p2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700144 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 pool_foreach (spd, im->spds, ({
146 p1 = pool_elt_at_index(spd->policies, *id1);
147 p2 = pool_elt_at_index(spd->policies, *id2);
148 if (p1 && p2)
149 return p2->priority - p1->priority;
150 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700151 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153 return 0;
154}
155
156int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158{
159 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 ipsec_spd_t *spd = 0;
161 ipsec_policy_t *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 uword *p;
163 u32 spd_index;
164
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700165 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
166 policy->priority, policy->is_outbound);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
168 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
169 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700170 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700172 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 policy->sa_index = p[0];
174 }
175
176 p = hash_get (im->spd_index_by_spd_id, policy->id);
177
178 if (!p)
179 return VNET_API_ERROR_SYSCALL_ERROR_1;
180
181 spd_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700182 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 if (!spd)
184 return VNET_API_ERROR_SYSCALL_ERROR_1;
185
186 if (is_add)
187 {
188 u32 policy_index;
189
190 pool_get (spd->policies, vp);
Damjan Marionf1213b82016-03-13 02:22:06 +0100191 clib_memcpy (vp, policy, sizeof (*vp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 policy_index = vp - spd->policies;
193
194 if (policy->is_outbound)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700195 {
196 if (policy->is_ipv6)
197 {
198 vec_add1 (spd->ipv6_outbound_policies, policy_index);
199 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
200 vec_sort_with_function (spd->ipv6_outbound_policies,
201 ipsec_spd_entry_sort);
202 }
203 else
204 {
205 vec_add1 (spd->ipv4_outbound_policies, policy_index);
206 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
207 vec_sort_with_function (spd->ipv4_outbound_policies,
208 ipsec_spd_entry_sort);
209 }
210 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700212 {
213 if (policy->is_ipv6)
214 {
215 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
216 {
217 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
218 policy_index);
219 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700220 vec_sort_with_function
221 (spd->ipv6_inbound_protect_policy_indices,
222 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700223 }
224 else
225 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700226 vec_add1
227 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
228 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700229 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700230 vec_sort_with_function
231 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
232 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 }
234 }
235 else
236 {
237 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
238 {
239 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
240 policy_index);
241 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700242 vec_sort_with_function
243 (spd->ipv4_inbound_protect_policy_indices,
244 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700245 }
246 else
247 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700248 vec_add1
249 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
250 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700252 vec_sort_with_function
253 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
254 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 }
256 }
257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
259 }
260 else
261 {
262 u32 i, j;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700263 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 pool_foreach_index(i, spd->policies, ({
Damjan Marion607de1a2016-08-16 22:53:54 +0200265 vp = pool_elt_at_index(spd->policies, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 if (vp->priority != policy->priority)
267 continue;
268 if (vp->is_outbound != policy->is_outbound)
269 continue;
270 if (vp->policy != policy->policy)
271 continue;
272 if (vp->sa_id != policy->sa_id)
273 continue;
274 if (vp->protocol != policy->protocol)
275 continue;
276 if (vp->lport.start != policy->lport.start)
277 continue;
278 if (vp->lport.stop != policy->lport.stop)
279 continue;
280 if (vp->rport.start != policy->rport.start)
281 continue;
282 if (vp->rport.stop != policy->rport.stop)
283 continue;
284 if (vp->is_ipv6 != policy->is_ipv6)
285 continue;
286 if (policy->is_ipv6)
287 {
288 if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
289 continue;
290 if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
291 continue;
292 if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
293 continue;
294 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
295 continue;
296 if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
297 continue;
298 if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
299 continue;
300 if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
301 continue;
302 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
303 continue;
304 if (policy->is_outbound)
305 {
306 vec_foreach_index(j, spd->ipv6_outbound_policies) {
307 if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
308 vec_del1 (spd->ipv6_outbound_policies, j);
309 break;
310 }
311 }
312 }
313 else
314 {
315 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
316 {
317 vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
318 if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
319 vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
320 break;
321 }
322 }
323 }
324 else
325 {
326 vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
327 if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
328 vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
329 break;
330 }
331 }
332 }
333 }
334 }
335 else
336 {
337 if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
338 continue;
339 if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
340 continue;
341 if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
342 continue;
343 if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
344 continue;
345 if (policy->is_outbound)
346 {
347 vec_foreach_index(j, spd->ipv4_outbound_policies) {
348 if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
349 vec_del1 (spd->ipv4_outbound_policies, j);
350 break;
351 }
Damjan Marion607de1a2016-08-16 22:53:54 +0200352 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 }
354 else
355 {
356 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
357 {
358 vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
359 if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
360 vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
361 break;
362 }
363 }
364 }
365 else
366 {
367 vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
368 if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
369 vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
370 break;
Matus Fabiance8805c2018-03-16 05:44:24 -0700371 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 }
373 }
374 }
375 }
376 pool_put (spd->policies, vp);
377 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700379 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 }
381
382 return 0;
383}
384
Matthew Smithca514fd2017-10-12 12:06:59 -0500385u8
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700386ipsec_is_sa_used (u32 sa_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700388 ipsec_main_t *im = &ipsec_main;
389 ipsec_spd_t *spd;
390 ipsec_policy_t *p;
Matus Fabian694265d2016-08-10 01:55:36 -0700391 ipsec_tunnel_if_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 pool_foreach(spd, im->spds, ({
395 pool_foreach(p, spd->policies, ({
396 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
397 {
398 if (p->sa_index == sa_index)
399 return 1;
400 }
401 }));
402 }));
Matus Fabian694265d2016-08-10 01:55:36 -0700403
404 pool_foreach(t, im->tunnel_interfaces, ({
405 if (t->input_sa_index == sa_index)
406 return 1;
407 if (t->output_sa_index == sa_index)
408 return 1;
409 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700410 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
412 return 0;
413}
414
415int
Klement Sekera4b089f22018-04-17 18:04:57 +0200416ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add,
417 u8 udp_encap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418{
419 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420 ipsec_sa_t *sa = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 uword *p;
422 u32 sa_index;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100423 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700425 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
427 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
428 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700429 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 if (!p && !is_add)
431 return VNET_API_ERROR_SYSCALL_ERROR_1;
432
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700433 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 {
435 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700436 sa = pool_elt_at_index (im->sad, sa_index);
437 if (ipsec_is_sa_used (sa_index))
438 {
439 clib_warning ("sa_id %u used in policy", sa->id);
440 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
441 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 hash_unset (im->sa_index_by_sa_id, sa->id);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100443 if (im->cb.add_del_sa_sess_cb)
444 {
445 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
446 if (err)
447 return VNET_API_ERROR_SYSCALL_ERROR_1;
448 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 pool_put (im->sad, sa);
450 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700451 else /* create new SA */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 {
453 pool_get (im->sad, sa);
Damjan Marionf1213b82016-03-13 02:22:06 +0100454 clib_memcpy (sa, new_sa, sizeof (*sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 sa_index = sa - im->sad;
Klement Sekera4b089f22018-04-17 18:04:57 +0200456 sa->udp_encap = udp_encap ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100458 if (im->cb.add_del_sa_sess_cb)
459 {
460 err = im->cb.add_del_sa_sess_cb (sa_index, 1);
461 if (err)
462 return VNET_API_ERROR_SYSCALL_ERROR_1;
463 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 }
465 return 0;
466}
467
468int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700469ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470{
471 ipsec_main_t *im = &ipsec_main;
472 uword *p;
473 u32 sa_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700474 ipsec_sa_t *sa = 0;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100475 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
477 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
478 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700479 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
481 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700482 sa = pool_elt_at_index (im->sad, sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
484 /* new crypto key */
485 if (0 < sa_update->crypto_key_len)
486 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700487 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
488 sa_update->crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 sa->crypto_key_len = sa_update->crypto_key_len;
490 }
491
492 /* new integ key */
493 if (0 < sa_update->integ_key_len)
494 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700495 clib_memcpy (sa->integ_key, sa_update->integ_key,
496 sa_update->integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 sa->integ_key_len = sa_update->integ_key_len;
498 }
499
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100500 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000501 {
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100502 if (im->cb.add_del_sa_sess_cb)
503 {
504 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
505 if (err)
506 return VNET_API_ERROR_SYSCALL_ERROR_1;
507 }
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000508 }
509
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 return 0;
511}
512
513static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700514ipsec_rand_seed (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700516 struct
517 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 time_t time;
519 pid_t pid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700520 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 } seed_data;
522
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700523 seed_data.time = time (NULL);
524 seed_data.pid = getpid ();
525 seed_data.p = (void *) &seed_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700527 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528}
529
530static clib_error_t *
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000531ipsec_check_support (ipsec_sa_t * sa)
532{
533 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
534 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
535 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
536 return clib_error_return (0, "unsupported none integ-alg");
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000537
538 return 0;
539}
540
541static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542ipsec_init (vlib_main_t * vm)
543{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700544 clib_error_t *error;
545 ipsec_main_t *im = &ipsec_main;
546 vlib_thread_main_t *tm = vlib_get_thread_main ();
547 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700549 ipsec_rand_seed ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551 memset (im, 0, sizeof (im[0]));
552
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700553 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 im->vlib_main = vm;
555
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700556 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
557 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
559
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700560 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
561 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500562
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700564 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 im->error_drop_node_index = node->index;
566
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000567 node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700568 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 im->esp_encrypt_node_index = node->index;
570
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000571 node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700572 ASSERT (node);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000573 im->esp_decrypt_node_index = node->index;
574
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800575 node = vlib_get_node_by_name (vm, (u8 *) "ah-encrypt");
576 ASSERT (node);
577 im->ah_encrypt_node_index = node->index;
578
579 node = vlib_get_node_by_name (vm, (u8 *) "ah-decrypt");
580 ASSERT (node);
581 im->ah_decrypt_node_index = node->index;
582
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000583 im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
584 im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800585 im->ah_encrypt_next_index = IPSEC_OUTPUT_NEXT_AH_ENCRYPT;
586 im->ah_decrypt_next_index = IPSEC_INPUT_NEXT_AH_DECRYPT;
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000587
588 im->cb.check_support_cb = ipsec_check_support;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
591 return error;
592
593 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
594 return error;
595
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800596 ipsec_proto_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
598 if ((error = ikev2_init (vm)))
599 return error;
600
601 return 0;
602}
603
604VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700605
606/*
607 * fd.io coding-style-patch-verification: ON
608 *
609 * Local Variables:
610 * eval: (c-set-style "gnu")
611 * End:
612 */