blob: 828bfe81e435de8230c5abb7ec56f0b6031e6b60 [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>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000026
Matus Fabian694265d2016-08-10 01:55:36 -070027u32
28ipsec_get_sa_index_by_sa_id (u32 sa_id)
29{
30 ipsec_main_t *im = &ipsec_main;
31 uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
32 if (!p)
33 return ~0;
34
35 return p[0];
36}
37
Ed Warnickecb9cada2015-12-08 15:45:58 -070038int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
40 int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070041{
42 ipsec_main_t *im = &ipsec_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 ip4_ipsec_config_t config;
44
Damjan Marion8b3191e2016-11-09 19:54:20 +010045 u32 spd_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 uword *p;
47
48 p = hash_get (im->spd_index_by_spd_id, spd_id);
49 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070050 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
52 spd_index = p[0];
53
54 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
55 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070056 return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58 if (is_add)
59 {
60 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
61 }
62 else
63 {
64 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
65 }
66
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067 clib_warning ("sw_if_index %u spd_id %u spd_index %u",
68 sw_if_index, spd_id, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
70 /* enable IPsec on TX */
Matus Fabian08a6f012016-11-15 06:08:51 -080071 vnet_feature_enable_disable ("ip4-output", "ipsec-output-ip4", sw_if_index,
72 is_add, 0, 0);
73 vnet_feature_enable_disable ("ip6-output", "ipsec-output-ip6", sw_if_index,
74 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
76 /* enable IPsec on RX */
Damjan Marion8b3191e2016-11-09 19:54:20 +010077 vnet_feature_enable_disable ("ip4-unicast", "ipsec-input-ip4", sw_if_index,
78 is_add, &config, sizeof (config));
79 vnet_feature_enable_disable ("ip6-unicast", "ipsec-input-ip6", sw_if_index,
80 is_add, &config, sizeof (config));
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82 return 0;
83}
84
85int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070086ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
88 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070089 ipsec_spd_t *spd = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 uword *p;
91 u32 spd_index, k, v;
92
93 p = hash_get (im->spd_index_by_spd_id, spd_id);
94 if (p && is_add)
95 return VNET_API_ERROR_INVALID_VALUE;
96 if (!p && !is_add)
97 return VNET_API_ERROR_INVALID_VALUE;
98
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070099 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 {
101 spd_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 spd = pool_elt_at_index (im->spds, spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 if (!spd)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104 return VNET_API_ERROR_INVALID_VALUE;
105 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
107 if (v == spd_index)
108 ipsec_set_interface_spd(vm, k, spd_id, 0);
109 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 hash_unset (im->spd_index_by_spd_id, spd_id);
112 pool_free (spd->policies);
113 vec_free (spd->ipv4_outbound_policies);
114 vec_free (spd->ipv6_outbound_policies);
115 vec_free (spd->ipv4_inbound_protect_policy_indices);
116 vec_free (spd->ipv4_inbound_policy_discard_and_bypass_indices);
117 pool_put (im->spds, spd);
118 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700119 else /* create new SPD */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 {
121 pool_get (im->spds, spd);
122 memset (spd, 0, sizeof (*spd));
123 spd_index = spd - im->spds;
124 spd->id = spd_id;
125 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
126 }
127 return 0;
128}
129
130static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700131ipsec_spd_entry_sort (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132{
133 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134 u32 *id1 = a1;
135 u32 *id2 = a2;
136 ipsec_spd_t *spd;
137 ipsec_policy_t *p1, *p2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700139 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 pool_foreach (spd, im->spds, ({
141 p1 = pool_elt_at_index(spd->policies, *id1);
142 p2 = pool_elt_at_index(spd->policies, *id2);
143 if (p1 && p2)
144 return p2->priority - p1->priority;
145 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700146 /* *INDENT-ON* */
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
189 if (policy->is_outbound)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700190 {
191 if (policy->is_ipv6)
192 {
193 vec_add1 (spd->ipv6_outbound_policies, policy_index);
194 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
195 vec_sort_with_function (spd->ipv6_outbound_policies,
196 ipsec_spd_entry_sort);
197 }
198 else
199 {
200 vec_add1 (spd->ipv4_outbound_policies, policy_index);
201 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
202 vec_sort_with_function (spd->ipv4_outbound_policies,
203 ipsec_spd_entry_sort);
204 }
205 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700207 {
208 if (policy->is_ipv6)
209 {
210 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
211 {
212 vec_add1 (spd->ipv6_inbound_protect_policy_indices,
213 policy_index);
214 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700215 vec_sort_with_function
216 (spd->ipv6_inbound_protect_policy_indices,
217 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700218 }
219 else
220 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700221 vec_add1
222 (spd->ipv6_inbound_policy_discard_and_bypass_indices,
223 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
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);
236 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700237 vec_sort_with_function
238 (spd->ipv4_inbound_protect_policy_indices,
239 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700240 }
241 else
242 {
Ed Warnicke853e7202016-08-12 11:42:26 -0700243 vec_add1
244 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
245 policy_index);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 clib_memcpy (vp, policy, sizeof (ipsec_policy_t));
Ed Warnicke853e7202016-08-12 11:42:26 -0700247 vec_sort_with_function
248 (spd->ipv4_inbound_policy_discard_and_bypass_indices,
249 ipsec_spd_entry_sort);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700250 }
251 }
252 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
254 }
255 else
256 {
257 u32 i, j;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 pool_foreach_index(i, spd->policies, ({
Damjan Marion607de1a2016-08-16 22:53:54 +0200260 vp = pool_elt_at_index(spd->policies, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 if (vp->priority != policy->priority)
262 continue;
263 if (vp->is_outbound != policy->is_outbound)
264 continue;
265 if (vp->policy != policy->policy)
266 continue;
267 if (vp->sa_id != policy->sa_id)
268 continue;
269 if (vp->protocol != policy->protocol)
270 continue;
271 if (vp->lport.start != policy->lport.start)
272 continue;
273 if (vp->lport.stop != policy->lport.stop)
274 continue;
275 if (vp->rport.start != policy->rport.start)
276 continue;
277 if (vp->rport.stop != policy->rport.stop)
278 continue;
279 if (vp->is_ipv6 != policy->is_ipv6)
280 continue;
281 if (policy->is_ipv6)
282 {
283 if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
284 continue;
285 if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
286 continue;
287 if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
288 continue;
289 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
290 continue;
291 if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
292 continue;
293 if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
294 continue;
295 if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
296 continue;
297 if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
298 continue;
299 if (policy->is_outbound)
300 {
301 vec_foreach_index(j, spd->ipv6_outbound_policies) {
302 if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
303 vec_del1 (spd->ipv6_outbound_policies, j);
304 break;
305 }
306 }
307 }
308 else
309 {
310 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
311 {
312 vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
313 if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
314 vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
315 break;
316 }
317 }
318 }
319 else
320 {
321 vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
322 if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
323 vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
324 break;
325 }
326 }
327 }
328 }
329 }
330 else
331 {
332 if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
333 continue;
334 if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
335 continue;
336 if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
337 continue;
338 if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
339 continue;
340 if (policy->is_outbound)
341 {
342 vec_foreach_index(j, spd->ipv4_outbound_policies) {
343 if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
344 vec_del1 (spd->ipv4_outbound_policies, j);
345 break;
346 }
Damjan Marion607de1a2016-08-16 22:53:54 +0200347 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 }
349 else
350 {
351 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
352 {
353 vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
354 if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
355 vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
356 break;
357 }
358 }
359 }
360 else
361 {
362 vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
363 if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
364 vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
365 break;
366 }
367 }
368 }
369 }
370 pool_put (spd->policies, vp);
371 break;
372 }
373 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700374 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 }
376
377 return 0;
378}
379
380static u8
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700381ipsec_is_sa_used (u32 sa_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700383 ipsec_main_t *im = &ipsec_main;
384 ipsec_spd_t *spd;
385 ipsec_policy_t *p;
Matus Fabian694265d2016-08-10 01:55:36 -0700386 ipsec_tunnel_if_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700388 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 pool_foreach(spd, im->spds, ({
390 pool_foreach(p, spd->policies, ({
391 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
392 {
393 if (p->sa_index == sa_index)
394 return 1;
395 }
396 }));
397 }));
Matus Fabian694265d2016-08-10 01:55:36 -0700398
399 pool_foreach(t, im->tunnel_interfaces, ({
400 if (t->input_sa_index == sa_index)
401 return 1;
402 if (t->output_sa_index == sa_index)
403 return 1;
404 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700405 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
407 return 0;
408}
409
410int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700411ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412{
413 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700414 ipsec_sa_t *sa = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 uword *p;
416 u32 sa_index;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100417 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700419 clib_warning ("id %u spi %u", new_sa->id, new_sa->spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
421 p = hash_get (im->sa_index_by_sa_id, new_sa->id);
422 if (p && is_add)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700423 return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 if (!p && !is_add)
425 return VNET_API_ERROR_SYSCALL_ERROR_1;
426
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700427 if (!is_add) /* delete */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 {
429 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700430 sa = pool_elt_at_index (im->sad, sa_index);
431 if (ipsec_is_sa_used (sa_index))
432 {
433 clib_warning ("sa_id %u used in policy", sa->id);
434 return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
435 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 hash_unset (im->sa_index_by_sa_id, sa->id);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100437 if (im->cb.add_del_sa_sess_cb)
438 {
439 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
440 if (err)
441 return VNET_API_ERROR_SYSCALL_ERROR_1;
442 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 pool_put (im->sad, sa);
444 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700445 else /* create new SA */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 {
447 pool_get (im->sad, sa);
Damjan Marionf1213b82016-03-13 02:22:06 +0100448 clib_memcpy (sa, new_sa, sizeof (*sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 sa_index = sa - im->sad;
450 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100451 if (im->cb.add_del_sa_sess_cb)
452 {
453 err = im->cb.add_del_sa_sess_cb (sa_index, 1);
454 if (err)
455 return VNET_API_ERROR_SYSCALL_ERROR_1;
456 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 }
458 return 0;
459}
460
461int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700462ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463{
464 ipsec_main_t *im = &ipsec_main;
465 uword *p;
466 u32 sa_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700467 ipsec_sa_t *sa = 0;
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100468 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
470 p = hash_get (im->sa_index_by_sa_id, sa_update->id);
471 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700472 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
474 sa_index = p[0];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700475 sa = pool_elt_at_index (im->sad, sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
477 /* new crypto key */
478 if (0 < sa_update->crypto_key_len)
479 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700480 clib_memcpy (sa->crypto_key, sa_update->crypto_key,
481 sa_update->crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 sa->crypto_key_len = sa_update->crypto_key_len;
483 }
484
485 /* new integ key */
486 if (0 < sa_update->integ_key_len)
487 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700488 clib_memcpy (sa->integ_key, sa_update->integ_key,
489 sa_update->integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 sa->integ_key_len = sa_update->integ_key_len;
491 }
492
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +0100493 if (0 < sa_update->crypto_key_len || 0 < sa_update->integ_key_len)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000494 {
Sergio Gonzalez Monroy20960632017-10-12 11:43:41 +0100495 if (im->cb.add_del_sa_sess_cb)
496 {
497 err = im->cb.add_del_sa_sess_cb (sa_index, 0);
498 if (err)
499 return VNET_API_ERROR_SYSCALL_ERROR_1;
500 }
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000501 }
502
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 return 0;
504}
505
506static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700507ipsec_rand_seed (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700509 struct
510 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511 time_t time;
512 pid_t pid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700513 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 } seed_data;
515
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700516 seed_data.time = time (NULL);
517 seed_data.pid = getpid ();
518 seed_data.p = (void *) &seed_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700520 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521}
522
523static clib_error_t *
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000524ipsec_check_support (ipsec_sa_t * sa)
525{
526 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
527 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
528 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
529 return clib_error_return (0, "unsupported none integ-alg");
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000530
531 return 0;
532}
533
534static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535ipsec_init (vlib_main_t * vm)
536{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700537 clib_error_t *error;
538 ipsec_main_t *im = &ipsec_main;
539 vlib_thread_main_t *tm = vlib_get_thread_main ();
540 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700542 ipsec_rand_seed ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
544 memset (im, 0, sizeof (im[0]));
545
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700546 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 im->vlib_main = vm;
548
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700549 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
550 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
552
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700553 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
554 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500555
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700557 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 im->error_drop_node_index = node->index;
559
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000560 node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700561 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 im->esp_encrypt_node_index = node->index;
563
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000564 node = vlib_get_node_by_name (vm, (u8 *) "esp-decrypt");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700565 ASSERT (node);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000566 im->esp_decrypt_node_index = node->index;
567
568 im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT;
569 im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT;
570
571 im->cb.check_support_cb = ipsec_check_support;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
574 return error;
575
576 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
577 return error;
578
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700579 esp_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
581 if ((error = ikev2_init (vm)))
582 return error;
583
584 return 0;
585}
586
587VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700588
589/*
590 * fd.io coding-style-patch-verification: ON
591 *
592 * Local Variables:
593 * eval: (c-set-style "gnu")
594 * End:
595 */