blob: 4894022536ba8bf6d4db1c087415d7ad2b94b849 [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>
22
23#include <vnet/ipsec/ipsec.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vnet/ipsec/ikev2.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000025#include <vnet/ipsec/esp.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080026#include <vnet/ipsec/ah.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000027
Dave Wallace71612d62017-10-24 01:32:41 -040028ipsec_main_t ipsec_main;
29
Matus Fabian694265d2016-08-10 01:55:36 -070030u32
31ipsec_get_sa_index_by_sa_id (u32 sa_id)
32{
33 ipsec_main_t *im = &ipsec_main;
34 uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
35 if (!p)
36 return ~0;
37
38 return p[0];
39}
40
Ed Warnickecb9cada2015-12-08 15:45:58 -070041int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070042ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
43 int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070044{
45 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 ip4_ipsec_config_t config;
47
Damjan Marion8b3191e2016-11-09 19:54:20 +010048 u32 spd_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 uword *p;
50
51 p = hash_get (im->spd_index_by_spd_id, spd_id);
52 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070053 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 spd_index = p[0];
56
57 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
58 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070059 return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
61 if (is_add)
62 {
63 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
64 }
65 else
66 {
67 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
68 }
69
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070070 clib_warning ("sw_if_index %u spd_id %u spd_index %u",
71 sw_if_index, spd_id, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
73 /* enable IPsec on TX */
Matus Fabian08a6f012016-11-15 06:08:51 -080074 vnet_feature_enable_disable ("ip4-output", "ipsec-output-ip4", sw_if_index,
75 is_add, 0, 0);
76 vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index,
77 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 /* enable IPsec on RX */
Damjan Marion8b3191e2016-11-09 19:54:20 +010080 vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index,
81 is_add, &config, sizeof (config));
82 vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index,
83 is_add, &config, sizeof (config));
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 return 0;
86}
87
88int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070089ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
91 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070092 ipsec_spd_t *spd = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 uword *p;
94 u32 spd_index, k, v;
95
96 p = hash_get (im->spd_index_by_spd_id, spd_id);
97 if (p && is_add)
98 return VNET_API_ERROR_INVALID_VALUE;
99 if (!p && !is_add)
100 return VNET_API_ERROR_INVALID_VALUE;
101
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 {
104 spd_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 if (!spd)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 return VNET_API_ERROR_INVALID_VALUE;
108 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
110 if (v == spd_index)
111 ipsec_set_interface_spd(vm, k, spd_id, 0);
112 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 hash_unset (im->spd_index_by_spd_id, spd_id);
115 pool_free (spd->policies);
116 vec_free (spd->ipv4_outbound_policies);
117 vec_free (spd->ipv6_outbound_policies);
118 vec_free (spd->ipv4_inbound_protect_policy_indices);
119 vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices);
120 pool_put (im->spds, spd);
121 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700122 else /* create new SPD */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 {
124 pool_get (im->spds, spd);
125 memset (spd, 0, sizeof (*spd));
126 spd_index = spd - im->spds;
127 spd->id = spd_id;
128 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
129 }
130 return 0;
131}
132
133static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134ipsec_spd_entry_sort (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
136 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700137 u32 *id1 = a1;
138 u32 *id2 = a2;
139 ipsec_spd_t *spd;
140 ipsec_policy_t *p1, *p2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700142 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 pool_foreach (spd, im->spds, ({
144 p1 = pool_elt_at_index(spd->policies, *id1);
145 p2 = pool_elt_at_index(spd->policies, *id2);
146 if (p1 && p2)
147 return p2->priority - p1->priority;
148 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
151 return 0;
152}
153
154int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700155ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156{
157 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700158 ipsec_spd_t *spd = 0;
159 ipsec_policy_t *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 uword *p;
161 u32 spd_index;
162
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
164 policy->priority, policy->is_outbound);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
166 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
167 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700168 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700170 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 policy->sa_index = p[0];
172 }
173
174 p = hash_get (im->spd_index_by_spd_id, policy->id);
175
176 if (!p)
177 return VNET_API_ERROR_SYSCALL_ERROR_1;
178
179 spd_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700180 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 if (!spd)
182 return VNET_API_ERROR_SYSCALL_ERROR_1;
183
184 if (is_add)
185 {
186 u32 policy_index;
187
188 pool_get (spd->policies, vp);
Damjan Marionf1213b82016-03-13 02:22:06 +0100189 clib_memcpy (vp, policy, sizeof (*vp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 policy_index = vp - spd->policies;
191
192 if (policy->is_outbound)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700193 {
194 if (policy->is_ipv6)
195 {
196 vec_add1 (spd->ipv6_outbound_policies, policy_index);
197 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
198 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);
204 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
205 vec_sort_with_function (spd->ipv4_outbound_policies,
206 ipsec_spd_entry_sort);
207 }
208 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700210 {
211 if (policy->is_ipv6)
212 {
213 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
214 {
215 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
216 policy_index);
217 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700218 vec_sort_with_function
219 (spd->ipv6_inbound_protect_policy_indices,
220 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700221 }
222 else
223 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700224 vec_add1
225 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
226 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700228 vec_sort_with_function
229 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
230 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 }
232 }
233 else
234 {
235 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
236 {
237 vec_add1 (spd->ipv4_inbound_protect_policy_indices,
238 policy_index);
239 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700240 vec_sort_with_function
241 (spd->ipv4_inbound_protect_policy_indices,
242 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243 }
244 else
245 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700246 vec_add1
247 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
248 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700249 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700250 vec_sort_with_function
251 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
252 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700253 }
254 }
255 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
257 }
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
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700414ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415{
416 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700417 ipsec_sa_t *sa = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 uword *p;
419 u32 sa_index;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100420 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700422 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
424 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
425 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700426 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 if (!p && !is_add)
428 return VNET_API_ERROR_SYSCALL_ERROR_1;
429
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700430 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 {
432 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700433 sa = pool_elt_at_index (im->sad, sa_index);
434 if (ipsec_is_sa_used (sa_index))
435 {
436 clib_warning ("sa_id %u used in policy", sa->id);
437 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
438 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 hash_unset (im->sa_index_by_sa_id, sa->id);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100440 if (im->cb.add_del_sa_sess_cb)
441 {
442 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
443 if (err)
444 return VNET_API_ERROR_SYSCALL_ERROR_1;
445 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 pool_put (im->sad, sa);
447 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700448 else /* create new SA */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 {
450 pool_get (im->sad, sa);
Damjan Marionf1213b82016-03-13 02:22:06 +0100451 clib_memcpy (sa, new_sa, sizeof (*sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 sa_index = sa - im->sad;
453 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100454 if (im->cb.add_del_sa_sess_cb)
455 {
456 err = im->cb.add_del_sa_sess_cb (sa_index, 1);
457 if (err)
458 return VNET_API_ERROR_SYSCALL_ERROR_1;
459 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 }
461 return 0;
462}
463
464int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700465ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466{
467 ipsec_main_t *im = &ipsec_main;
468 uword *p;
469 u32 sa_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700470 ipsec_sa_t *sa = 0;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100471 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
473 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
474 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700475 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
477 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700478 sa = pool_elt_at_index (im->sad, sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
480 /* new crypto key */
481 if (0 < sa_update->crypto_key_len)
482 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700483 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
484 sa_update->crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 sa->crypto_key_len = sa_update->crypto_key_len;
486 }
487
488 /* new integ key */
489 if (0 < sa_update->integ_key_len)
490 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700491 clib_memcpy (sa->integ_key, sa_update->integ_key,
492 sa_update->integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 sa->integ_key_len = sa_update->integ_key_len;
494 }
495
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100496 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000497 {
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100498 if (im->cb.add_del_sa_sess_cb)
499 {
500 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
501 if (err)
502 return VNET_API_ERROR_SYSCALL_ERROR_1;
503 }
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000504 }
505
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 return 0;
507}
508
509static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700510ipsec_rand_seed (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700512 struct
513 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 time_t time;
515 pid_t pid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700516 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 } seed_data;
518
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700519 seed_data.time = time (NULL);
520 seed_data.pid = getpid ();
521 seed_data.p = (void *) &seed_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700523 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524}
525
526static clib_error_t *
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000527ipsec_check_support (ipsec_sa_t * sa)
528{
529 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
530 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
531 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
532 return clib_error_return (0, "unsupported none integ-alg");
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000533
534 return 0;
535}
536
537static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538ipsec_init (vlib_main_t * vm)
539{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700540 clib_error_t *error;
541 ipsec_main_t *im = &ipsec_main;
542 vlib_thread_main_t *tm = vlib_get_thread_main ();
543 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700545 ipsec_rand_seed ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
547 memset (im, 0, sizeof (im[0]));
548
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700549 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 im->vlib_main = vm;
551
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700552 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
553 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
555
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700556 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
557 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500558
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700560 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 im->error_drop_node_index = node->index;
562
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000563 node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700564 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 im->esp_encrypt_node_index = node->index;
566
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000567 node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700568 ASSERT (node);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000569 im->esp_decrypt_node_index = node->index;
570
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800571 node = vlib_get_node_by_name (vm, (u8 *) "ah-encrypt");
572 ASSERT (node);
573 im->ah_encrypt_node_index = node->index;
574
575 node = vlib_get_node_by_name (vm, (u8 *) "ah-decrypt");
576 ASSERT (node);
577 im->ah_decrypt_node_index = node->index;
578
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000579 im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
580 im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800581 im->ah_encrypt_next_index = IPSEC_OUTPUT_NEXT_AH_ENCRYPT;
582 im->ah_decrypt_next_index = IPSEC_INPUT_NEXT_AH_DECRYPT;
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000583
584 im->cb.check_support_cb = ipsec_check_support;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
587 return error;
588
589 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
590 return error;
591
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800592 ipsec_proto_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
594 if ((error = ikev2_init (vm)))
595 return error;
596
597 return 0;
598}
599
600VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700601
602/*
603 * fd.io coding-style-patch-verification: ON
604 *
605 * Local Variables:
606 * eval: (c-set-style "gnu")
607 * End:
608 */