blob: 73b5101288045051db50256b56aeca30f688f91d [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{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700138 u32 *id1 = a1;
139 u32 *id2 = a2;
Klement Sekeraee52d872018-06-07 19:36:07 +0200140 ipsec_spd_t *spd = ipsec_main.spd_to_sort;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700141 ipsec_policy_t *p1, *p2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Klement Sekeraee52d872018-06-07 19:36:07 +0200143 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 Warnickecb9cada2015-12-08 15:45:58 -0700147
148 return 0;
149}
150
151int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700152ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153{
154 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700155 ipsec_spd_t *spd = 0;
156 ipsec_policy_t *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 uword *p;
158 u32 spd_index;
159
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
161 policy->priority, policy->is_outbound);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
163 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
164 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700165 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700167 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 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)166a9d42016-08-06 11:00:56 -0700177 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 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 Marionf1213b82016-03-13 02:22:06 +0100186 clib_memcpy (vp, policy, sizeof (*vp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 policy_index = vp - spd->policies;
188
Klement Sekeraee52d872018-06-07 19:36:07 +0200189 ipsec_main.spd_to_sort = spd;
190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191 if (policy->is_outbound)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192 {
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 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);
216 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700217 vec_sort_with_function
218 (spd->ipv6_inbound_protect_policy_indices,
219 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700220 }
221 else
222 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700223 vec_add1
224 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
225 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700226 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700227 vec_sort_with_function
228 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
229 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700230 }
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 Warnicke853e7202016-08-12 11:42:26 -0700239 vec_sort_with_function
240 (spd->ipv4_inbound_protect_policy_indices,
241 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 }
243 else
244 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700245 vec_add1
246 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
247 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700249 vec_sort_with_function
250 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
251 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700252 }
253 }
254 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Klement Sekeraee52d872018-06-07 19:36:07 +0200256 ipsec_main.spd_to_sort = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 }
258 else
259 {
260 u32 i, j;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700261 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 pool_foreach_index(i, spd->policies, ({
Damjan Marion607de1a2016-08-16 22:53:54 +0200263 vp = pool_elt_at_index(spd->policies, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 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 Marion607de1a2016-08-16 22:53:54 +0200350 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 }
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 Fabiance8805c2018-03-16 05:44:24 -0700369 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 }
371 }
372 }
373 }
374 pool_put (spd->policies, vp);
375 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700377 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 }
379
380 return 0;
381}
382
Matthew Smithca514fd2017-10-12 12:06:59 -0500383u8
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700384ipsec_is_sa_used (u32 sa_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700386 ipsec_main_t *im = &ipsec_main;
387 ipsec_spd_t *spd;
388 ipsec_policy_t *p;
Matus Fabian694265d2016-08-10 01:55:36 -0700389 ipsec_tunnel_if_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700391 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 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 Fabian694265d2016-08-10 01:55:36 -0700401
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)166a9d42016-08-06 11:00:56 -0700408 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
410 return 0;
411}
412
413int
Klement Sekera4b089f22018-04-17 18:04:57 +0200414ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add,
415 u8 udp_encap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416{
417 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700418 ipsec_sa_t *sa = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 uword *p;
420 u32 sa_index;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100421 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700423 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
425 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
426 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700427 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 if (!p && !is_add)
429 return VNET_API_ERROR_SYSCALL_ERROR_1;
430
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700431 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 {
433 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700434 sa = pool_elt_at_index (im->sad, sa_index);
435 if (ipsec_is_sa_used (sa_index))
436 {
437 clib_warning ("sa_id %u used in policy", sa->id);
438 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
439 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 hash_unset (im->sa_index_by_sa_id, sa->id);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100441 if (im->cb.add_del_sa_sess_cb)
442 {
443 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
444 if (err)
445 return VNET_API_ERROR_SYSCALL_ERROR_1;
446 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 pool_put (im->sad, sa);
448 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700449 else /* create new SA */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 {
451 pool_get (im->sad, sa);
Damjan Marionf1213b82016-03-13 02:22:06 +0100452 clib_memcpy (sa, new_sa, sizeof (*sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 sa_index = sa - im->sad;
Klement Sekera4b089f22018-04-17 18:04:57 +0200454 sa->udp_encap = udp_encap ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100456 if (im->cb.add_del_sa_sess_cb)
457 {
458 err = im->cb.add_del_sa_sess_cb (sa_index, 1);
459 if (err)
460 return VNET_API_ERROR_SYSCALL_ERROR_1;
461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 }
463 return 0;
464}
465
466int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700467ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468{
469 ipsec_main_t *im = &ipsec_main;
470 uword *p;
471 u32 sa_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700472 ipsec_sa_t *sa = 0;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100473 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474
475 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
476 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700477 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
479 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700480 sa = pool_elt_at_index (im->sad, sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
482 /* new crypto key */
483 if (0 < sa_update->crypto_key_len)
484 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700485 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
486 sa_update->crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 sa->crypto_key_len = sa_update->crypto_key_len;
488 }
489
490 /* new integ key */
491 if (0 < sa_update->integ_key_len)
492 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700493 clib_memcpy (sa->integ_key, sa_update->integ_key,
494 sa_update->integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 sa->integ_key_len = sa_update->integ_key_len;
496 }
497
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100498 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000499 {
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100500 if (im->cb.add_del_sa_sess_cb)
501 {
502 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
503 if (err)
504 return VNET_API_ERROR_SYSCALL_ERROR_1;
505 }
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000506 }
507
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 return 0;
509}
510
511static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700512ipsec_rand_seed (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700514 struct
515 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 time_t time;
517 pid_t pid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700518 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 } seed_data;
520
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700521 seed_data.time = time (NULL);
522 seed_data.pid = getpid ();
523 seed_data.p = (void *) &seed_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700525 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526}
527
528static clib_error_t *
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000529ipsec_check_support (ipsec_sa_t * sa)
530{
531 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
532 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
533 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
534 return clib_error_return (0, "unsupported none integ-alg");
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000535
536 return 0;
537}
538
539static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540ipsec_init (vlib_main_t * vm)
541{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700542 clib_error_t *error;
543 ipsec_main_t *im = &ipsec_main;
544 vlib_thread_main_t *tm = vlib_get_thread_main ();
545 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700547 ipsec_rand_seed ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
549 memset (im, 0, sizeof (im[0]));
550
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700551 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 im->vlib_main = vm;
553
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700554 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
555 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
557
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700558 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
559 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500560
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700562 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 im->error_drop_node_index = node->index;
564
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000565 node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700566 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567 im->esp_encrypt_node_index = node->index;
568
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000569 node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700570 ASSERT (node);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000571 im->esp_decrypt_node_index = node->index;
572
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800573 node = vlib_get_node_by_name (vm, (u8 *) "ah-encrypt");
574 ASSERT (node);
575 im->ah_encrypt_node_index = node->index;
576
577 node = vlib_get_node_by_name (vm, (u8 *) "ah-decrypt");
578 ASSERT (node);
579 im->ah_decrypt_node_index = node->index;
580
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000581 im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
582 im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800583 im->ah_encrypt_next_index = IPSEC_OUTPUT_NEXT_AH_ENCRYPT;
584 im->ah_decrypt_next_index = IPSEC_INPUT_NEXT_AH_DECRYPT;
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000585
586 im->cb.check_support_cb = ipsec_check_support;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
589 return error;
590
591 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
592 return error;
593
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800594 ipsec_proto_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
596 if ((error = ikev2_init (vm)))
597 return error;
598
599 return 0;
600}
601
602VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700603
604/*
605 * fd.io coding-style-patch-verification: ON
606 *
607 * Local Variables:
608 * eval: (c-set-style "gnu")
609 * End:
610 */