Line data Source code
1 : /*
2 : * Copyright (c) 2015 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <vlib/vlib.h>
17 : #include <vlib/unix/plugin.h>
18 : #include <vlibmemory/api.h>
19 : #include <vpp/app/version.h>
20 : #include <vnet/vnet.h>
21 : #include <vppinfra/error.h>
22 : #include <vppinfra/random.h>
23 : #include <vnet/udp/udp.h>
24 : #include <vnet/ipsec/ipsec.h>
25 : #include <vnet/ipsec/ipsec_tun.h>
26 : #include <vnet/ipip/ipip.h>
27 : #include <plugins/ikev2/ikev2.h>
28 : #include <plugins/ikev2/ikev2_priv.h>
29 : #include <plugins/dns/dns.h>
30 : #include <openssl/sha.h>
31 : #include <vnet/ipsec/ipsec_punt.h>
32 : #include <plugins/ikev2/ikev2.api_enum.h>
33 :
34 : #define IKEV2_LIVENESS_RETRIES 3
35 : #define IKEV2_LIVENESS_PERIOD_CHECK 30
36 :
37 : ikev2_main_t ikev2_main;
38 :
39 : static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
40 : ikev2_sa_t * sa,
41 : ikev2_child_sa_t * child);
42 :
43 : #define ikev2_set_state(sa, v, ...) do { \
44 : (sa)->state = v; \
45 : ikev2_elog_sa_state("ispi %lx SA state changed to " #v __VA_ARGS__, sa->ispi); \
46 : } while(0);
47 :
48 : typedef struct
49 : {
50 : u32 next_index;
51 : u32 sw_if_index;
52 : } ikev2_trace_t;
53 :
54 : static u8 *
55 120 : format_ikev2_trace (u8 * s, va_list * args)
56 : {
57 120 : CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58 120 : CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59 120 : ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
60 :
61 120 : s = format (s, "ikev2: sw_if_index %d, next index %d",
62 : t->sw_if_index, t->next_index);
63 120 : return s;
64 : }
65 :
66 : #define IKEV2_GENERATE_SA_INIT_OK_str ""
67 : #define IKEV2_GENERATE_SA_INIT_OK_ERR_NO_DH_STR \
68 : "no DH group configured for IKE proposals!"
69 : #define IKEV2_GENERATE_SA_INIT_OK_ERR_UNSUPP_STR \
70 : "DH group not supported!"
71 :
72 : typedef enum
73 : {
74 : IKEV2_GENERATE_SA_INIT_OK,
75 : IKEV2_GENERATE_SA_INIT_ERR_NO_DH,
76 : IKEV2_GENERATE_SA_INIT_ERR_UNSUPPORTED_DH,
77 : } ikev2_generate_sa_error_t;
78 :
79 : static u8 *
80 0 : format_ikev2_gen_sa_error (u8 * s, va_list * args)
81 : {
82 0 : ikev2_generate_sa_error_t e = va_arg (*args, ikev2_generate_sa_error_t);
83 0 : switch (e)
84 : {
85 0 : case IKEV2_GENERATE_SA_INIT_OK:
86 0 : break;
87 0 : case IKEV2_GENERATE_SA_INIT_ERR_NO_DH:
88 0 : s = format (s, IKEV2_GENERATE_SA_INIT_OK_ERR_NO_DH_STR);
89 0 : break;
90 0 : case IKEV2_GENERATE_SA_INIT_ERR_UNSUPPORTED_DH:
91 0 : s = format (s, IKEV2_GENERATE_SA_INIT_OK_ERR_UNSUPP_STR);
92 0 : break;
93 : }
94 0 : return s;
95 : }
96 :
97 : typedef enum
98 : {
99 : IKEV2_NEXT_IP4_LOOKUP,
100 : IKEV2_NEXT_IP4_ERROR_DROP,
101 : IKEV2_IP4_N_NEXT,
102 : } ikev2_ip4_next_t;
103 :
104 : typedef enum
105 : {
106 : IKEV2_NEXT_IP6_LOOKUP,
107 : IKEV2_NEXT_IP6_ERROR_DROP,
108 : IKEV2_IP6_N_NEXT,
109 : } ikev2_ip6_next_t;
110 :
111 : typedef u32 ikev2_non_esp_marker;
112 :
113 : static u16
114 20 : ikev2_get_port (ikev2_sa_t *sa)
115 : {
116 20 : return ikev2_natt_active (sa) ? IKEV2_PORT_NATT : IKEV2_PORT;
117 : }
118 :
119 : static int
120 2 : ikev2_insert_non_esp_marker (ike_header_t *ike, int len)
121 : {
122 2 : memmove ((u8 *) ike + sizeof (ikev2_non_esp_marker), ike, len);
123 2 : clib_memset (ike, 0, sizeof (ikev2_non_esp_marker));
124 2 : return len + sizeof (ikev2_non_esp_marker);
125 : }
126 :
127 : static ikev2_sa_transform_t *
128 886 : ikev2_find_transform_data (ikev2_sa_transform_t * t)
129 : {
130 886 : ikev2_main_t *km = &ikev2_main;
131 : ikev2_sa_transform_t *td;
132 :
133 10757 : vec_foreach (td, km->supported_transforms)
134 : {
135 10707 : if (td->type != t->type)
136 7918 : continue;
137 :
138 2789 : if (td->transform_id != t->transform_id)
139 1931 : continue;
140 :
141 858 : if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
142 : {
143 314 : if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
144 314 : || t->attrs[1] != 14)
145 0 : continue;
146 :
147 314 : if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
148 22 : continue;
149 : }
150 836 : return td;
151 : }
152 50 : return 0;
153 : }
154 :
155 : static ikev2_sa_proposal_t *
156 34 : ikev2_select_proposal (ikev2_sa_proposal_t * proposals,
157 : ikev2_protocol_id_t prot_id)
158 : {
159 34 : ikev2_sa_proposal_t *rv = 0;
160 : ikev2_sa_proposal_t *proposal;
161 : ikev2_sa_transform_t *transform, *new_t;
162 : u8 mandatory_bitmap, optional_bitmap;
163 :
164 34 : if (prot_id == IKEV2_PROTOCOL_IKE)
165 : {
166 13 : mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
167 : (1 << IKEV2_TRANSFORM_TYPE_PRF) | (1 << IKEV2_TRANSFORM_TYPE_DH);
168 13 : optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_INTEG);
169 : }
170 21 : else if (prot_id == IKEV2_PROTOCOL_ESP)
171 : {
172 21 : mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
173 : (1 << IKEV2_TRANSFORM_TYPE_ESN);
174 21 : optional_bitmap = mandatory_bitmap |
175 : (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
176 : }
177 0 : else if (prot_id == IKEV2_PROTOCOL_AH)
178 : {
179 0 : mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
180 : (1 << IKEV2_TRANSFORM_TYPE_ESN);
181 0 : optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
182 : }
183 : else
184 0 : return 0;
185 :
186 34 : vec_add2 (rv, proposal, 1);
187 :
188 34 : vec_foreach (proposal, proposals)
189 : {
190 34 : u8 bitmap = 0;
191 34 : if (proposal->protocol_id != prot_id)
192 0 : continue;
193 :
194 173 : vec_foreach (transform, proposal->transforms)
195 : {
196 139 : if ((1 << transform->type) & bitmap)
197 21 : continue;
198 :
199 118 : if (ikev2_find_transform_data (transform))
200 : {
201 116 : bitmap |= 1 << transform->type;
202 116 : vec_add2 (rv->transforms, new_t, 1);
203 116 : clib_memcpy_fast (new_t, transform, sizeof (*new_t));
204 116 : new_t->attrs = vec_dup (transform->attrs);
205 : }
206 : }
207 :
208 34 : if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
209 34 : (bitmap & ~optional_bitmap) == 0)
210 : {
211 34 : rv->proposal_num = proposal->proposal_num;
212 34 : rv->protocol_id = proposal->protocol_id;
213 34 : RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
214 34 : goto done;
215 : }
216 : else
217 : {
218 0 : vec_free (rv->transforms);
219 : }
220 : }
221 :
222 0 : vec_free (rv);
223 34 : done:
224 34 : return rv;
225 : }
226 :
227 : ikev2_sa_transform_t *
228 777 : ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
229 : ikev2_transform_type_t type)
230 : {
231 : ikev2_sa_transform_t *t;
232 :
233 777 : if (!p)
234 0 : return 0;
235 :
236 1612 : vec_foreach (t, p->transforms)
237 : {
238 1603 : if (t->type == type)
239 768 : return ikev2_find_transform_data (t);
240 : }
241 9 : return 0;
242 : }
243 :
244 : ikev2_child_sa_t *
245 52 : ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id,
246 : int by_initiator)
247 : {
248 : ikev2_child_sa_t *c;
249 75 : vec_foreach (c, sa->childs)
250 : {
251 52 : ikev2_sa_proposal_t *proposal =
252 52 : by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
253 52 : if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
254 29 : return c;
255 : }
256 :
257 23 : return 0;
258 : }
259 :
260 : void
261 712 : ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v)
262 : {
263 : ikev2_sa_proposal_t *p;
264 : ikev2_sa_transform_t *t;
265 :
266 712 : if (!*v)
267 614 : return;
268 :
269 196 : vec_foreach (p, *v)
270 : {
271 449 : vec_foreach (t, p->transforms)
272 : {
273 351 : vec_free (t->attrs);
274 : }
275 98 : vec_free (p->transforms);
276 : }
277 98 : vec_free (*v);
278 : }
279 :
280 : static void
281 27 : ikev2_sa_free_child_sa (ikev2_child_sa_t * c)
282 : {
283 27 : ikev2_sa_free_proposal_vector (&c->r_proposals);
284 27 : ikev2_sa_free_proposal_vector (&c->i_proposals);
285 27 : vec_free (c->sk_ai);
286 27 : vec_free (c->sk_ar);
287 27 : vec_free (c->sk_ei);
288 27 : vec_free (c->sk_er);
289 27 : vec_free (c->tsi);
290 27 : vec_free (c->tsr);
291 27 : }
292 :
293 : static void
294 311 : ikev2_sa_free_all_child_sa (ikev2_child_sa_t ** childs)
295 : {
296 : ikev2_child_sa_t *c;
297 337 : vec_foreach (c, *childs) ikev2_sa_free_child_sa (c);
298 :
299 311 : vec_free (*childs);
300 311 : }
301 :
302 : static void
303 1 : ikev2_sa_del_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * child)
304 : {
305 1 : ikev2_sa_free_child_sa (child);
306 1 : vec_del1 (sa->childs, child - sa->childs);
307 1 : }
308 :
309 : static void
310 290 : ikev2_sa_free_all_vec (ikev2_sa_t * sa)
311 : {
312 290 : vec_free (sa->i_nonce);
313 290 : vec_free (sa->r_nonce);
314 :
315 290 : vec_free (sa->dh_shared_key);
316 290 : vec_free (sa->dh_private_key);
317 290 : vec_free (sa->i_dh_data);
318 290 : vec_free (sa->r_dh_data);
319 :
320 290 : ikev2_sa_free_proposal_vector (&sa->r_proposals);
321 290 : ikev2_sa_free_proposal_vector (&sa->i_proposals);
322 :
323 290 : vec_free (sa->sk_d);
324 290 : vec_free (sa->sk_ai);
325 290 : vec_free (sa->sk_ar);
326 290 : vec_free (sa->sk_ei);
327 290 : vec_free (sa->sk_er);
328 290 : vec_free (sa->sk_pi);
329 290 : vec_free (sa->sk_pr);
330 :
331 290 : vec_free (sa->i_id.data);
332 290 : vec_free (sa->r_id.data);
333 :
334 290 : vec_free (sa->i_auth.data);
335 290 : if (sa->i_auth.key)
336 0 : EVP_PKEY_free (sa->i_auth.key);
337 290 : vec_free (sa->r_auth.data);
338 290 : if (sa->r_auth.key)
339 0 : EVP_PKEY_free (sa->r_auth.key);
340 :
341 290 : vec_free (sa->del);
342 :
343 290 : vec_free (sa->rekey);
344 :
345 290 : vec_free (sa->last_sa_init_req_packet_data);
346 290 : vec_free (sa->last_sa_init_res_packet_data);
347 :
348 290 : vec_free (sa->last_res_packet_data);
349 :
350 290 : ikev2_sa_free_all_child_sa (&sa->childs);
351 290 : }
352 :
353 : static void
354 12 : ikev2_delete_sa (ikev2_main_per_thread_data_t * ptd, ikev2_sa_t * sa)
355 : {
356 : uword *p;
357 :
358 12 : ikev2_sa_free_all_vec (sa);
359 :
360 12 : p = hash_get (ptd->sa_by_rspi, sa->rspi);
361 12 : if (p)
362 : {
363 12 : hash_unset (ptd->sa_by_rspi, sa->rspi);
364 12 : pool_put (ptd->sas, sa);
365 : }
366 12 : }
367 :
368 : static ikev2_generate_sa_error_t
369 19 : ikev2_generate_sa_init_data (ikev2_sa_t * sa)
370 : {
371 19 : ikev2_sa_transform_t *t = 0, *t2;
372 19 : ikev2_main_t *km = &ikev2_main;
373 :
374 19 : if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
375 0 : return IKEV2_GENERATE_SA_INIT_ERR_NO_DH;
376 :
377 : /* check if received DH group is on our list of supported groups */
378 506 : vec_foreach (t2, km->supported_transforms)
379 : {
380 506 : if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
381 : {
382 19 : t = t2;
383 19 : break;
384 : }
385 : }
386 :
387 19 : if (!t)
388 : {
389 0 : sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
390 0 : return IKEV2_GENERATE_SA_INIT_ERR_UNSUPPORTED_DH;
391 : }
392 :
393 19 : if (sa->is_initiator)
394 : {
395 : /* generate rspi */
396 6 : RAND_bytes ((u8 *) & sa->ispi, 8);
397 :
398 : /* generate nonce */
399 6 : sa->i_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
400 6 : RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
401 : }
402 : else
403 : {
404 : /* generate rspi */
405 13 : RAND_bytes ((u8 *) & sa->rspi, 8);
406 :
407 : /* generate nonce */
408 13 : sa->r_nonce = vec_new (u8, vec_len (sa->i_nonce));
409 13 : RAND_bytes ((u8 *) sa->r_nonce, vec_len (sa->i_nonce));
410 : }
411 :
412 : /* generate dh keys */
413 19 : ikev2_generate_dh (sa, t);
414 :
415 19 : return IKEV2_GENERATE_SA_INIT_OK;
416 : }
417 :
418 : static void
419 6 : ikev2_complete_sa_data (ikev2_sa_t * sa, ikev2_sa_t * sai)
420 : {
421 6 : ikev2_sa_transform_t *t = 0, *t2;
422 6 : ikev2_main_t *km = &ikev2_main;
423 :
424 : /*move some data to the new SA */
425 : #define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
426 6 : sa->i_nonce = _(sai->i_nonce);
427 6 : sa->i_dh_data = _(sai->i_dh_data);
428 6 : sa->dh_private_key = _(sai->dh_private_key);
429 6 : ip_address_copy (&sa->iaddr, &sai->iaddr);
430 6 : ip_address_copy (&sa->raddr, &sai->raddr);
431 6 : sa->is_initiator = sai->is_initiator;
432 6 : sa->i_id.type = sai->i_id.type;
433 6 : sa->r_id.type = sai->r_id.type;
434 6 : sa->profile_index = sai->profile_index;
435 6 : sa->tun_itf = sai->tun_itf;
436 6 : sa->is_tun_itf_set = sai->is_tun_itf_set;
437 6 : if (sai->natt_state == IKEV2_NATT_DISABLED)
438 0 : sa->natt_state = IKEV2_NATT_DISABLED;
439 6 : sa->i_id.data = _(sai->i_id.data);
440 6 : sa->r_id.data = _(sai->r_id.data);
441 6 : sa->i_auth.method = sai->i_auth.method;
442 6 : sa->i_auth.hex = sai->i_auth.hex;
443 6 : sa->i_auth.data = _(sai->i_auth.data);
444 6 : sa->i_auth.key = _(sai->i_auth.key);
445 6 : sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data);
446 6 : sa->last_init_msg_id = sai->last_init_msg_id;
447 6 : sa->childs = _(sai->childs);
448 6 : sa->udp_encap = sai->udp_encap;
449 6 : sa->ipsec_over_udp_port = sai->ipsec_over_udp_port;
450 6 : sa->dst_port = sai->dst_port;
451 6 : sa->sw_if_index = sai->sw_if_index;
452 : #undef _
453 :
454 :
455 6 : if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
456 : {
457 0 : return;
458 : }
459 :
460 : /* check if received DH group is on our list of supported groups */
461 156 : vec_foreach (t2, km->supported_transforms)
462 : {
463 156 : if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
464 : {
465 6 : t = t2;
466 6 : break;
467 : }
468 : }
469 :
470 6 : if (!t)
471 : {
472 0 : sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
473 0 : return;
474 : }
475 :
476 :
477 : /* generate dh keys */
478 6 : ikev2_complete_dh (sa, t);
479 :
480 : }
481 :
482 : static void
483 25 : ikev2_calc_keys (ikev2_sa_t * sa)
484 : {
485 : u8 *tmp;
486 : /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
487 25 : u8 *skeyseed = 0;
488 25 : u8 *s = 0;
489 25 : u16 integ_key_len = 0, salt_len = 0;
490 : ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
491 : tr_encr =
492 25 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
493 : tr_prf =
494 25 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
495 : tr_integ =
496 25 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
497 :
498 25 : if (tr_integ)
499 12 : integ_key_len = tr_integ->key_len;
500 : else
501 13 : salt_len = sizeof (u32);
502 :
503 25 : vec_append (s, sa->i_nonce);
504 25 : vec_append (s, sa->r_nonce);
505 25 : skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
506 :
507 : /* Calculate S = Ni | Nr | SPIi | SPIr */
508 : u64 *spi;
509 25 : vec_add2 (s, tmp, 2 * sizeof (*spi));
510 25 : spi = (u64 *) tmp;
511 25 : spi[0] = clib_host_to_net_u64 (sa->ispi);
512 25 : spi[1] = clib_host_to_net_u64 (sa->rspi);
513 :
514 : /* calculate PRFplus */
515 : u8 *keymat;
516 25 : int len = tr_prf->key_trunc + /* SK_d */
517 25 : integ_key_len * 2 + /* SK_ai, SK_ar */
518 25 : tr_encr->key_len * 2 + /* SK_ei, SK_er */
519 25 : tr_prf->key_len * 2 + /* SK_pi, SK_pr */
520 25 : salt_len * 2;
521 :
522 25 : keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
523 25 : vec_free (skeyseed);
524 25 : vec_free (s);
525 :
526 25 : int pos = 0;
527 :
528 : /* SK_d */
529 25 : sa->sk_d = vec_new (u8, tr_prf->key_trunc);
530 25 : clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
531 25 : pos += tr_prf->key_trunc;
532 :
533 25 : if (integ_key_len)
534 : {
535 : /* SK_ai */
536 12 : sa->sk_ai = vec_new (u8, integ_key_len);
537 12 : clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
538 12 : pos += integ_key_len;
539 :
540 : /* SK_ar */
541 12 : sa->sk_ar = vec_new (u8, integ_key_len);
542 12 : clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
543 12 : pos += integ_key_len;
544 : }
545 :
546 : /* SK_ei */
547 25 : sa->sk_ei = vec_new (u8, tr_encr->key_len + salt_len);
548 25 : clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len + salt_len);
549 25 : pos += tr_encr->key_len + salt_len;
550 :
551 : /* SK_er */
552 25 : sa->sk_er = vec_new (u8, tr_encr->key_len + salt_len);
553 25 : clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len + salt_len);
554 25 : pos += tr_encr->key_len + salt_len;
555 :
556 : /* SK_pi */
557 25 : sa->sk_pi = vec_new (u8, tr_prf->key_len);
558 25 : clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
559 25 : pos += tr_prf->key_len;
560 :
561 : /* SK_pr */
562 25 : sa->sk_pr = vec_new (u8, tr_prf->key_len);
563 25 : clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
564 25 : pos += tr_prf->key_len;
565 :
566 25 : vec_free (keymat);
567 25 : sa->keys_generated = 1;
568 25 : }
569 :
570 : static void
571 27 : ikev2_calc_child_keys (ikev2_sa_t *sa, ikev2_child_sa_t *child, u8 kex)
572 : {
573 27 : u8 *s = 0;
574 27 : u16 integ_key_len = 0;
575 27 : u8 salt_len = 0;
576 :
577 : ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
578 : tr_prf =
579 27 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
580 : ctr_encr =
581 27 : ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
582 : ctr_integ =
583 27 : ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
584 :
585 27 : if (ctr_integ)
586 26 : integ_key_len = ctr_integ->key_len;
587 : else
588 1 : salt_len = sizeof (u32);
589 :
590 27 : if (kex)
591 3 : vec_append (s, sa->dh_shared_key);
592 27 : vec_append (s, sa->i_nonce);
593 27 : vec_append (s, sa->r_nonce);
594 : /* calculate PRFplus */
595 : u8 *keymat;
596 27 : int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
597 :
598 27 : keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
599 :
600 27 : int pos = 0;
601 :
602 : /* SK_ei */
603 27 : child->sk_ei = vec_new (u8, ctr_encr->key_len);
604 27 : clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
605 27 : pos += ctr_encr->key_len;
606 :
607 27 : if (ctr_integ)
608 : {
609 : /* SK_ai */
610 26 : child->sk_ai = vec_new (u8, ctr_integ->key_len);
611 26 : clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
612 26 : pos += ctr_integ->key_len;
613 : }
614 : else
615 : {
616 1 : clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
617 1 : pos += salt_len;
618 : }
619 :
620 : /* SK_er */
621 27 : child->sk_er = vec_new (u8, ctr_encr->key_len);
622 27 : clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
623 27 : pos += ctr_encr->key_len;
624 :
625 27 : if (ctr_integ)
626 : {
627 : /* SK_ar */
628 26 : child->sk_ar = vec_new (u8, integ_key_len);
629 26 : clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
630 26 : pos += integ_key_len;
631 : }
632 : else
633 : {
634 1 : clib_memcpy (&child->salt_er, keymat + pos, salt_len);
635 1 : pos += salt_len;
636 : }
637 :
638 27 : ASSERT (pos == len);
639 :
640 27 : vec_free (keymat);
641 27 : }
642 :
643 : static u8 *
644 74 : ikev2_compute_nat_sha1 (u64 ispi, u64 rspi, ip_address_t *ia, u16 port)
645 74 : {
646 74 : const u32 max_buf_size =
647 : sizeof (ispi) + sizeof (rspi) + sizeof (ip6_address_t) + sizeof (u16);
648 74 : u8 buf[max_buf_size];
649 74 : u8 *res = vec_new (u8, 20);
650 :
651 74 : clib_memcpy_fast (&buf[0], &ispi, sizeof (ispi));
652 74 : clib_memcpy_fast (&buf[8], &rspi, sizeof (rspi));
653 74 : clib_memcpy_fast (&buf[8 + 8], ip_addr_bytes (ia), ip_address_size (ia));
654 74 : clib_memcpy_fast (&buf[8 + 8 + ip_address_size (ia)], &port, sizeof (port));
655 74 : SHA1 (buf, 2 * sizeof (ispi) + sizeof (port) + ip_address_size (ia), res);
656 74 : return res;
657 : }
658 :
659 : static int
660 23 : ikev2_parse_ke_payload (const void *p, u32 rlen, ikev2_sa_t * sa,
661 : u8 ** ke_data)
662 : {
663 23 : const ike_ke_payload_header_t *ke = p;
664 23 : u16 plen = clib_net_to_host_u16 (ke->length);
665 23 : ASSERT (plen >= sizeof (*ke) && plen <= rlen);
666 23 : if (sizeof (*ke) > rlen)
667 : {
668 0 : ikev2_elog_error ("KE: packet too small");
669 0 : return 0;
670 : }
671 :
672 23 : sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
673 23 : vec_reset_length (ke_data[0]);
674 23 : vec_add (ke_data[0], ke->payload, plen - sizeof (*ke));
675 23 : return 1;
676 : }
677 :
678 : static int
679 30 : ikev2_parse_nonce_payload (const void *p, u32 rlen, const u8 **nonce)
680 : {
681 30 : const ike_payload_header_t *ikep = p;
682 30 : u16 plen = clib_net_to_host_u16 (ikep->length);
683 30 : ASSERT (plen >= sizeof (*ikep) && plen <= rlen);
684 30 : int len = plen - sizeof (*ikep);
685 30 : ASSERT (len >= 16 && len <= 256);
686 30 : if (PREDICT_FALSE (len < 16 || len > 256))
687 : {
688 0 : ikev2_elog_error ("NONCE: bad size");
689 0 : return 0;
690 : }
691 30 : *nonce = ikep->payload;
692 30 : return len;
693 : }
694 :
695 : static int
696 597 : ikev2_check_payload_length (const ike_payload_header_t * ikep, int rlen,
697 : u16 * plen)
698 : {
699 597 : if (sizeof (*ikep) > rlen)
700 : {
701 0 : ikev2_elog_error ("payload: packet too small");
702 0 : return 0;
703 : }
704 597 : *plen = clib_net_to_host_u16 (ikep->length);
705 597 : if (*plen < sizeof (*ikep) || *plen > rlen)
706 : {
707 254 : ikev2_elog_error ("payload: bad size");
708 254 : return 0;
709 : }
710 343 : return 1;
711 : }
712 :
713 : static int
714 267 : ikev2_process_sa_init_req (vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike,
715 : udp_header_t *udp, u32 len, u32 sw_if_index)
716 : {
717 267 : int p = 0;
718 267 : u8 payload = ike->nextpayload;
719 : ike_payload_header_t *ikep;
720 : u16 plen;
721 :
722 267 : ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
723 : "from ", clib_net_to_host_u64 (ike->ispi),
724 : clib_net_to_host_u64 (ike->rspi),
725 : ip_addr_v4 (&sa->iaddr).as_u32,
726 : ip_addr_version (&sa->iaddr) == AF_IP4);
727 :
728 267 : sa->ispi = clib_net_to_host_u64 (ike->ispi);
729 267 : sa->sw_if_index = sw_if_index;
730 :
731 : /* store whole IKE payload - needed for PSK auth */
732 267 : vec_reset_length (sa->last_sa_init_req_packet_data);
733 267 : vec_add (sa->last_sa_init_req_packet_data, ike, len);
734 :
735 267 : if (len < sizeof (*ike))
736 : {
737 0 : ikev2_elog_error ("IKE_INIT request too small");
738 0 : return 0;
739 : }
740 :
741 267 : len -= sizeof (*ike);
742 330 : while (p < len && payload != IKEV2_PAYLOAD_NONE)
743 : {
744 317 : ikep = (ike_payload_header_t *) & ike->payload[p];
745 317 : int current_length = len - p;
746 317 : if (!ikev2_check_payload_length (ikep, current_length, &plen))
747 254 : return 0;
748 :
749 63 : if (payload == IKEV2_PAYLOAD_SA)
750 : {
751 13 : ikev2_sa_free_proposal_vector (&sa->i_proposals);
752 13 : sa->i_proposals = ikev2_parse_sa_payload (ikep, current_length);
753 : }
754 50 : else if (payload == IKEV2_PAYLOAD_KE)
755 : {
756 13 : if (!ikev2_parse_ke_payload (ikep, current_length, sa,
757 : &sa->i_dh_data))
758 0 : return 0;
759 : }
760 37 : else if (payload == IKEV2_PAYLOAD_NONCE)
761 : {
762 : const u8 *nonce;
763 : int nonce_len;
764 13 : vec_reset_length (sa->i_nonce);
765 13 : if ((nonce_len = ikev2_parse_nonce_payload (ikep, current_length,
766 : &nonce)) <= 0)
767 0 : return 0;
768 13 : vec_add (sa->i_nonce, nonce, nonce_len);
769 : }
770 24 : else if (payload == IKEV2_PAYLOAD_NOTIFY)
771 : {
772 24 : ikev2_notify_t *n =
773 24 : ikev2_parse_notify_payload (ikep, current_length);
774 24 : if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP)
775 : {
776 24 : u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, 0, &sa->iaddr,
777 12 : udp->src_port);
778 12 : if (clib_memcmp (src_sha, n->data, vec_len (src_sha)))
779 : {
780 1 : if (sa->natt_state == IKEV2_NATT_ENABLED)
781 1 : sa->natt_state = IKEV2_NATT_ACTIVE;
782 1 : ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator"
783 : " behind NAT", sa->ispi);
784 : }
785 12 : vec_free (src_sha);
786 : }
787 12 : else if (n->msg_type ==
788 : IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP)
789 : {
790 24 : u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, 0, &sa->raddr,
791 12 : udp->dst_port);
792 12 : if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha)))
793 : {
794 1 : if (sa->natt_state == IKEV2_NATT_ENABLED)
795 1 : sa->natt_state = IKEV2_NATT_ACTIVE;
796 1 : ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder"
797 : " (self) behind NAT", sa->ispi);
798 : }
799 12 : vec_free (dst_sha);
800 : }
801 24 : vec_free (n);
802 : }
803 0 : else if (payload == IKEV2_PAYLOAD_VENDOR)
804 : {
805 0 : ikev2_parse_vendor_payload (ikep);
806 : }
807 : else
808 : {
809 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
810 : payload);
811 0 : if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
812 : {
813 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
814 0 : sa->unsupported_cp = payload;
815 0 : return 0;
816 : }
817 : }
818 :
819 63 : payload = ikep->nextpayload;
820 63 : p += plen;
821 : }
822 :
823 13 : ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
824 13 : return 1;
825 : }
826 :
827 : static void
828 6 : ikev2_process_sa_init_resp (vlib_main_t * vm,
829 : ikev2_sa_t * sa, ike_header_t * ike,
830 : udp_header_t * udp, u32 len)
831 : {
832 6 : int p = 0;
833 6 : u8 payload = ike->nextpayload;
834 : ike_payload_header_t *ikep;
835 : u16 plen;
836 :
837 6 : sa->ispi = clib_net_to_host_u64 (ike->ispi);
838 6 : sa->rspi = clib_net_to_host_u64 (ike->rspi);
839 :
840 6 : ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
841 : "from ", sa->ispi, sa->rspi,
842 : ip_addr_v4 (&sa->raddr).as_u32,
843 : ip_addr_version (&sa->raddr) == AF_IP4);
844 :
845 : /* store whole IKE payload - needed for PSK auth */
846 6 : vec_reset_length (sa->last_sa_init_res_packet_data);
847 6 : vec_add (sa->last_sa_init_res_packet_data, ike, len);
848 :
849 6 : if (sizeof (*ike) > len)
850 : {
851 0 : ikev2_elog_error ("IKE_INIT response too small");
852 0 : return;
853 : }
854 :
855 6 : len -= sizeof (*ike);
856 36 : while (p < len && payload != IKEV2_PAYLOAD_NONE)
857 : {
858 30 : int current_length = len - p;
859 30 : ikep = (ike_payload_header_t *) & ike->payload[p];
860 30 : if (!ikev2_check_payload_length (ikep, current_length, &plen))
861 0 : return;
862 :
863 30 : if (payload == IKEV2_PAYLOAD_SA)
864 : {
865 6 : ikev2_sa_free_proposal_vector (&sa->r_proposals);
866 6 : sa->r_proposals = ikev2_parse_sa_payload (ikep, current_length);
867 6 : if (sa->r_proposals)
868 : {
869 6 : ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
870 6 : ike->msgid =
871 6 : clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
872 : }
873 : }
874 24 : else if (payload == IKEV2_PAYLOAD_KE)
875 : {
876 6 : if (!ikev2_parse_ke_payload (ikep, current_length, sa,
877 : &sa->r_dh_data))
878 0 : return;
879 : }
880 18 : else if (payload == IKEV2_PAYLOAD_NONCE)
881 : {
882 : const u8 *nonce;
883 : int nonce_len;
884 6 : vec_reset_length (sa->r_nonce);
885 6 : if ((nonce_len = ikev2_parse_nonce_payload (ikep, current_length,
886 : &nonce)) <= 0)
887 0 : return;
888 6 : vec_add (sa->r_nonce, nonce, nonce_len);
889 : }
890 12 : else if (payload == IKEV2_PAYLOAD_NOTIFY)
891 : {
892 12 : ikev2_notify_t *n =
893 12 : ikev2_parse_notify_payload (ikep, current_length);
894 12 : if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP)
895 : {
896 12 : u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, ike->rspi,
897 : &sa->raddr,
898 6 : udp->src_port);
899 6 : if (clib_memcmp (src_sha, n->data, vec_len (src_sha)))
900 : {
901 0 : ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder"
902 : " behind NAT, unsupported", sa->ispi);
903 : }
904 6 : vec_free (src_sha);
905 : }
906 6 : else if (n->msg_type ==
907 : IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP)
908 : {
909 12 : u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, ike->rspi,
910 : &sa->iaddr,
911 6 : udp->dst_port);
912 6 : if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha)))
913 : {
914 1 : if (sa->natt_state == IKEV2_NATT_ENABLED)
915 1 : sa->natt_state = IKEV2_NATT_ACTIVE;
916 1 : ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator"
917 : " (self) behind NAT", sa->ispi);
918 : }
919 6 : vec_free (dst_sha);
920 : }
921 12 : vec_free (n);
922 : }
923 0 : else if (payload == IKEV2_PAYLOAD_VENDOR)
924 : {
925 0 : ikev2_parse_vendor_payload (ikep);
926 : }
927 : else
928 : {
929 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
930 : payload);
931 0 : if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
932 : {
933 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
934 0 : sa->unsupported_cp = payload;
935 0 : return;
936 : }
937 : }
938 :
939 30 : payload = ikep->nextpayload;
940 30 : p += plen;
941 : }
942 : }
943 :
944 : static u8 *
945 43 : ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike,
946 : u8 * payload, u32 rlen, u32 * out_len)
947 : {
948 43 : ikev2_main_per_thread_data_t *ptd = ikev2_get_per_thread_data ();
949 43 : int p = 0;
950 43 : u8 last_payload = 0, *hmac = 0, *plaintext = 0;
951 43 : ike_payload_header_t *ikep = 0;
952 43 : u16 plen = 0;
953 43 : u32 dlen = 0;
954 : ikev2_sa_transform_t *tr_integ;
955 : ikev2_sa_transform_t *tr_encr;
956 : tr_integ =
957 43 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
958 : tr_encr =
959 43 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
960 43 : int is_aead = tr_encr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16;
961 :
962 43 : if (((!sa->sk_ar || !sa->sk_ai) && !is_aead) || (!sa->sk_ei || !sa->sk_er))
963 0 : return 0;
964 :
965 43 : if (rlen <= sizeof (*ike))
966 0 : return 0;
967 :
968 43 : int len = rlen - sizeof (*ike);
969 86 : while (p < len &&
970 43 : *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
971 : {
972 43 : ikep = (ike_payload_header_t *) & ike->payload[p];
973 43 : int current_length = len - p;
974 43 : if (!ikev2_check_payload_length (ikep, current_length, &plen))
975 0 : return 0;
976 :
977 43 : if (*payload == IKEV2_PAYLOAD_SK)
978 : {
979 43 : last_payload = *payload;
980 : }
981 : else
982 : {
983 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
984 : *payload);
985 0 : if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
986 : {
987 0 : sa->unsupported_cp = *payload;
988 0 : return 0;
989 : }
990 : }
991 :
992 43 : *payload = ikep->nextpayload;
993 43 : p += plen;
994 : }
995 :
996 43 : if (last_payload != IKEV2_PAYLOAD_SK)
997 : {
998 0 : ikev2_elog_error ("Last payload must be SK");
999 0 : return 0;
1000 : }
1001 :
1002 43 : if (is_aead)
1003 : {
1004 12 : if (plen < sizeof (*ikep) + IKEV2_GCM_ICV_SIZE)
1005 0 : return 0;
1006 :
1007 12 : plen -= sizeof (*ikep) + IKEV2_GCM_ICV_SIZE;
1008 12 : u8 *aad = (u8 *) ike;
1009 12 : u32 aad_len = ikep->payload - aad;
1010 12 : u8 *tag = ikep->payload + plen;
1011 :
1012 12 : int rc = ikev2_decrypt_aead_data (ptd, sa, tr_encr, ikep->payload,
1013 : plen, aad, aad_len, tag, &dlen);
1014 12 : if (rc)
1015 : {
1016 12 : *out_len = dlen;
1017 12 : plaintext = ikep->payload + IKEV2_GCM_IV_SIZE;
1018 : }
1019 : }
1020 : else
1021 : {
1022 31 : if (rlen < tr_integ->key_trunc)
1023 0 : return 0;
1024 :
1025 31 : hmac =
1026 31 : ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
1027 31 : (u8 *) ike, rlen - tr_integ->key_trunc);
1028 :
1029 31 : if (plen < sizeof (*ikep) + tr_integ->key_trunc)
1030 0 : return 0;
1031 :
1032 31 : plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
1033 :
1034 31 : if (clib_memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
1035 : {
1036 0 : ikev2_elog_error ("message integrity check failed");
1037 0 : vec_free (hmac);
1038 0 : return 0;
1039 : }
1040 31 : vec_free (hmac);
1041 :
1042 31 : int rc = ikev2_decrypt_data (ptd, sa, tr_encr, ikep->payload, plen,
1043 : &dlen);
1044 31 : if (rc)
1045 : {
1046 31 : *out_len = dlen;
1047 31 : plaintext = ikep->payload + tr_encr->block_size;
1048 : }
1049 : }
1050 :
1051 43 : return plaintext;
1052 : }
1053 :
1054 : static int
1055 111 : ikev2_is_id_equal (const ikev2_id_t *i1, const ikev2_id_t *i2)
1056 : {
1057 111 : if (i1->type != i2->type)
1058 0 : return 0;
1059 :
1060 111 : if (vec_len (i1->data) != vec_len (i2->data))
1061 0 : return 0;
1062 :
1063 111 : if (clib_memcmp (i1->data, i2->data, vec_len (i1->data)))
1064 0 : return 0;
1065 :
1066 111 : return 1;
1067 : }
1068 :
1069 : static void
1070 25 : ikev2_initial_contact_cleanup_internal (ikev2_main_per_thread_data_t * ptd,
1071 : ikev2_sa_t * sa)
1072 : {
1073 25 : ikev2_main_t *km = &ikev2_main;
1074 : ikev2_sa_t *tmp;
1075 25 : u32 i, *delete = 0;
1076 : ikev2_child_sa_t *c;
1077 :
1078 : /* find old IKE SAs with the same authenticated identity */
1079 : /* *INDENT-OFF* */
1080 44 : pool_foreach (tmp, ptd->sas) {
1081 19 : if (!ikev2_is_id_equal (&tmp->i_id, &sa->i_id)
1082 19 : || !ikev2_is_id_equal(&tmp->r_id, &sa->r_id))
1083 0 : continue;
1084 :
1085 19 : if (sa->rspi != tmp->rspi)
1086 0 : vec_add1(delete, tmp - ptd->sas);
1087 : }
1088 : /* *INDENT-ON* */
1089 :
1090 25 : for (i = 0; i < vec_len (delete); i++)
1091 : {
1092 0 : tmp = pool_elt_at_index (ptd->sas, delete[i]);
1093 0 : vec_foreach (c, tmp->childs)
1094 : {
1095 0 : ikev2_delete_tunnel_interface (km->vnet_main, tmp, c);
1096 : }
1097 0 : ikev2_delete_sa (ptd, tmp);
1098 : }
1099 :
1100 25 : vec_free (delete);
1101 25 : sa->initial_contact = 0;
1102 25 : }
1103 :
1104 : static void
1105 25 : ikev2_initial_contact_cleanup (ikev2_main_per_thread_data_t * ptd,
1106 : ikev2_sa_t * sa)
1107 : {
1108 25 : ikev2_main_t *km = &ikev2_main;
1109 :
1110 25 : if (!sa->initial_contact)
1111 0 : return;
1112 :
1113 25 : if (ptd)
1114 : {
1115 19 : ikev2_initial_contact_cleanup_internal (ptd, sa);
1116 : }
1117 : else
1118 : {
1119 12 : vec_foreach (ptd, km->per_thread_data)
1120 6 : ikev2_initial_contact_cleanup_internal (ptd, sa);
1121 : }
1122 25 : sa->initial_contact = 0;
1123 : }
1124 :
1125 : static int
1126 37 : ikev2_parse_id_payload (const void *p, u16 rlen, ikev2_id_t * sa_id)
1127 : {
1128 37 : const ike_id_payload_header_t *id = p;
1129 37 : u16 plen = clib_net_to_host_u16 (id->length);
1130 37 : if (plen < sizeof (*id) || plen > rlen)
1131 0 : return 0;
1132 :
1133 37 : sa_id->type = id->id_type;
1134 37 : vec_reset_length (sa_id->data);
1135 37 : vec_add (sa_id->data, id->payload, plen - sizeof (*id));
1136 :
1137 37 : return 1;
1138 : }
1139 :
1140 : static int
1141 19 : ikev2_parse_auth_payload (const void *p, u32 rlen, ikev2_auth_t * a)
1142 : {
1143 19 : const ike_auth_payload_header_t *ah = p;
1144 19 : u16 plen = clib_net_to_host_u16 (ah->length);
1145 :
1146 19 : a->method = ah->auth_method;
1147 19 : vec_reset_length (a->data);
1148 19 : vec_add (a->data, ah->payload, plen - sizeof (*ah));
1149 19 : return 1;
1150 : }
1151 :
1152 : static int
1153 19 : ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa,
1154 : ike_header_t * ike, u32 len)
1155 : {
1156 19 : int p = 0;
1157 : ikev2_child_sa_t *first_child_sa;
1158 19 : u8 payload = ike->nextpayload;
1159 19 : u8 *plaintext = 0;
1160 : ike_payload_header_t *ikep;
1161 : u16 plen;
1162 19 : u32 dlen = 0;
1163 :
1164 19 : ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
1165 : "from ", clib_host_to_net_u64 (ike->ispi),
1166 : clib_host_to_net_u64 (ike->rspi),
1167 : sa->is_initiator ?
1168 : ip_addr_v4 (&sa->raddr).as_u32 :
1169 : ip_addr_v4 (&sa->iaddr).as_u32,
1170 : ip_addr_version (&sa->raddr) == AF_IP4);
1171 :
1172 19 : ikev2_calc_keys (sa);
1173 :
1174 19 : plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1175 :
1176 19 : if (!plaintext)
1177 : {
1178 0 : if (sa->unsupported_cp)
1179 : {
1180 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1181 0 : return 0;
1182 : }
1183 0 : goto malformed;
1184 : }
1185 :
1186 : /* select or create 1st child SA */
1187 19 : if (sa->is_initiator)
1188 : {
1189 6 : first_child_sa = &sa->childs[0];
1190 : }
1191 : else
1192 : {
1193 13 : ikev2_sa_free_all_child_sa (&sa->childs);
1194 13 : vec_add2 (sa->childs, first_child_sa, 1);
1195 : }
1196 :
1197 :
1198 : /* process encrypted payload */
1199 151 : while (p < dlen && payload != IKEV2_PAYLOAD_NONE)
1200 : {
1201 132 : ikep = (ike_payload_header_t *) & plaintext[p];
1202 132 : int current_length = dlen - p;
1203 132 : if (!ikev2_check_payload_length (ikep, current_length, &plen))
1204 0 : goto malformed;
1205 :
1206 132 : if (payload == IKEV2_PAYLOAD_SA) /* 33 */
1207 : {
1208 19 : if (sa->is_initiator)
1209 : {
1210 6 : ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
1211 6 : first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep,
1212 : current_length);
1213 : }
1214 : else
1215 : {
1216 13 : ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
1217 13 : first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep,
1218 : current_length);
1219 : }
1220 : }
1221 113 : else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
1222 : {
1223 19 : if (!ikev2_parse_id_payload (ikep, current_length, &sa->i_id))
1224 0 : goto malformed;
1225 : }
1226 94 : else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
1227 : {
1228 18 : if (!ikev2_parse_id_payload (ikep, current_length, &sa->r_id))
1229 0 : goto malformed;
1230 : }
1231 76 : else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
1232 : {
1233 19 : if (sa->is_initiator)
1234 : {
1235 6 : if (!ikev2_parse_auth_payload (ikep, current_length,
1236 : &sa->r_auth))
1237 0 : goto malformed;
1238 : }
1239 : else
1240 : {
1241 13 : if (!ikev2_parse_auth_payload (ikep, current_length,
1242 : &sa->i_auth))
1243 0 : goto malformed;
1244 : }
1245 : }
1246 57 : else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1247 : {
1248 19 : ikev2_notify_t *n =
1249 19 : ikev2_parse_notify_payload (ikep, current_length);
1250 19 : if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
1251 : {
1252 19 : sa->initial_contact = 1;
1253 : }
1254 19 : vec_free (n);
1255 : }
1256 38 : else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1257 : {
1258 0 : ikev2_parse_vendor_payload (ikep);
1259 : }
1260 38 : else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
1261 : {
1262 19 : vec_free (first_child_sa->tsi);
1263 19 : first_child_sa->tsi = ikev2_parse_ts_payload (ikep, current_length);
1264 : }
1265 19 : else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
1266 : {
1267 19 : vec_free (first_child_sa->tsr);
1268 19 : first_child_sa->tsr = ikev2_parse_ts_payload (ikep, current_length);
1269 : }
1270 : else
1271 : {
1272 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1273 : payload);
1274 :
1275 0 : if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1276 : {
1277 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1278 0 : sa->unsupported_cp = payload;
1279 0 : return 0;
1280 : }
1281 : }
1282 :
1283 132 : payload = ikep->nextpayload;
1284 132 : p += plen;
1285 : }
1286 :
1287 19 : return 1;
1288 :
1289 0 : malformed:
1290 0 : ikev2_set_state (sa, IKEV2_STATE_DELETED, ": malformed IKE_AUTH");
1291 0 : return 0;
1292 : }
1293 :
1294 : static int
1295 13 : ikev2_process_informational_req (vlib_main_t * vm,
1296 : ikev2_sa_t * sa, ike_header_t * ike, u32 len)
1297 : {
1298 13 : int p = 0;
1299 13 : u8 payload = ike->nextpayload;
1300 13 : u8 *plaintext = 0;
1301 : ike_payload_header_t *ikep;
1302 13 : u32 dlen = 0;
1303 13 : ikev2_notify_t *n = 0;
1304 :
1305 13 : sa->liveness_retries = 0;
1306 13 : ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
1307 : "from ", clib_host_to_net_u64 (ike->ispi),
1308 : clib_host_to_net_u64 (ike->rspi),
1309 : ip_addr_v4 (&sa->iaddr).as_u32,
1310 : ip_addr_version (&sa->iaddr) == AF_IP4);
1311 :
1312 13 : plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1313 :
1314 13 : if (!plaintext)
1315 0 : return 0;
1316 :
1317 : /* process encrypted payload */
1318 13 : p = 0;
1319 25 : while (p < dlen && payload != IKEV2_PAYLOAD_NONE)
1320 : {
1321 12 : u32 current_length = dlen - p;
1322 12 : if (p + sizeof (*ikep) > dlen)
1323 0 : return 0;
1324 :
1325 12 : ikep = (ike_payload_header_t *) & plaintext[p];
1326 12 : u16 plen = clib_net_to_host_u16 (ikep->length);
1327 :
1328 12 : if (plen < sizeof (*ikep) || plen > current_length)
1329 0 : return 0;
1330 :
1331 12 : if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1332 : {
1333 0 : n = ikev2_parse_notify_payload (ikep, current_length);
1334 0 : if (!n)
1335 0 : return 0;
1336 0 : if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1337 0 : ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1338 0 : vec_free (n);
1339 : }
1340 12 : else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1341 : {
1342 12 : sa->del = ikev2_parse_delete_payload (ikep, current_length);
1343 : }
1344 0 : else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1345 : {
1346 0 : ikev2_parse_vendor_payload (ikep);
1347 : }
1348 : else
1349 : {
1350 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1351 : payload);
1352 0 : if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1353 : {
1354 0 : sa->unsupported_cp = payload;
1355 0 : return 0;
1356 : }
1357 : }
1358 12 : payload = ikep->nextpayload;
1359 12 : p += plen;
1360 : }
1361 13 : return 1;
1362 : }
1363 :
1364 : static int
1365 6 : ikev2_process_create_child_sa_rekey (ikev2_sa_t *sa, ikev2_sa_t *sar,
1366 : ikev2_rekey_t *rekey,
1367 : ikev2_sa_proposal_t *proposal,
1368 : ikev2_ts_t *tsi, ikev2_ts_t *tsr,
1369 : const u8 *nonce, int nonce_len)
1370 : {
1371 : ikev2_sa_transform_t *tr;
1372 :
1373 6 : rekey->i_proposal = proposal;
1374 6 : rekey->r_proposal = ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1375 :
1376 6 : if (sar->dh_group)
1377 : {
1378 : tr =
1379 3 : ikev2_sa_get_td_for_type (rekey->r_proposal, IKEV2_TRANSFORM_TYPE_DH);
1380 :
1381 3 : if (!tr || tr->dh_type != sar->dh_group)
1382 : {
1383 0 : rekey->notify_type = IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD;
1384 0 : ikev2_sa_free_proposal_vector (&rekey->r_proposal);
1385 0 : return 0;
1386 : }
1387 :
1388 3 : vec_free (sa->dh_shared_key);
1389 3 : vec_free (sa->dh_private_key);
1390 3 : vec_free (sa->i_dh_data);
1391 3 : vec_free (sa->r_dh_data);
1392 :
1393 3 : sa->dh_group = sar->dh_group;
1394 3 : sa->i_dh_data = sar->i_dh_data;
1395 3 : sar->i_dh_data = 0;
1396 :
1397 3 : ikev2_generate_dh (sa, tr);
1398 3 : rekey->kex = 1;
1399 : }
1400 :
1401 6 : vec_reset_length (sa->i_nonce);
1402 6 : vec_add (sa->i_nonce, nonce, nonce_len);
1403 :
1404 6 : vec_validate (sa->r_nonce, nonce_len - 1);
1405 6 : RAND_bytes ((u8 *) sa->r_nonce, nonce_len);
1406 :
1407 6 : rekey->tsi = tsi;
1408 6 : rekey->tsr = tsr;
1409 :
1410 6 : return 1;
1411 : }
1412 :
1413 : static int
1414 11 : ikev2_process_create_child_sa_req (vlib_main_t * vm,
1415 : ikev2_sa_t * sa, ike_header_t * ike,
1416 : u32 len)
1417 : {
1418 11 : int p = 0;
1419 11 : u8 payload = ike->nextpayload;
1420 11 : u8 *plaintext = 0;
1421 : ikev2_rekey_t *rekey;
1422 : ike_payload_header_t *ikep;
1423 11 : ikev2_notify_t *n = 0;
1424 11 : ikev2_ts_t *tsi = 0;
1425 11 : ikev2_ts_t *tsr = 0;
1426 11 : ikev2_sa_proposal_t *proposal = 0;
1427 : ikev2_child_sa_t *child_sa;
1428 11 : u32 dlen = 0, src;
1429 : u16 plen;
1430 11 : const u8 *nonce = 0;
1431 11 : int nonce_len = 0;
1432 : ikev2_sa_t sar;
1433 :
1434 11 : clib_memset (&sar, 0, sizeof (sar));
1435 :
1436 11 : if (sa->is_initiator)
1437 3 : src = ip_addr_v4 (&sa->raddr).as_u32;
1438 : else
1439 8 : src = ip_addr_v4 (&sa->iaddr).as_u32;
1440 :
1441 11 : ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received from",
1442 : clib_host_to_net_u64 (ike->ispi),
1443 : clib_host_to_net_u64 (ike->rspi), src,
1444 : ip_addr_version (&sa->raddr) == AF_IP4);
1445 :
1446 11 : plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1447 :
1448 11 : if (!plaintext)
1449 0 : goto cleanup_and_exit;
1450 :
1451 : /* process encrypted payload */
1452 11 : p = 0;
1453 86 : while (payload != IKEV2_PAYLOAD_NONE)
1454 : {
1455 75 : ikep = (ike_payload_header_t *) & plaintext[p];
1456 75 : int current_length = dlen - p;
1457 75 : if (!ikev2_check_payload_length (ikep, current_length, &plen))
1458 0 : goto cleanup_and_exit;
1459 :
1460 75 : if (payload == IKEV2_PAYLOAD_SA)
1461 : {
1462 19 : proposal = ikev2_parse_sa_payload (ikep, current_length);
1463 : }
1464 56 : else if (payload == IKEV2_PAYLOAD_KE)
1465 : {
1466 4 : if (!ikev2_parse_ke_payload (ikep, current_length, &sar,
1467 : &sar.i_dh_data))
1468 0 : goto cleanup_and_exit;
1469 : }
1470 52 : else if (payload == IKEV2_PAYLOAD_NOTIFY)
1471 : {
1472 : ikev2_notify_t *n0;
1473 19 : n0 = ikev2_parse_notify_payload (ikep, current_length);
1474 19 : if (n0->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1475 : {
1476 11 : vec_free (n);
1477 11 : n = n0;
1478 : }
1479 : else
1480 8 : vec_free (n0);
1481 : }
1482 33 : else if (payload == IKEV2_PAYLOAD_DELETE)
1483 : {
1484 0 : sa->del = ikev2_parse_delete_payload (ikep, current_length);
1485 : }
1486 33 : else if (payload == IKEV2_PAYLOAD_VENDOR)
1487 : {
1488 0 : ikev2_parse_vendor_payload (ikep);
1489 : }
1490 33 : else if (payload == IKEV2_PAYLOAD_NONCE)
1491 : {
1492 11 : nonce_len = ikev2_parse_nonce_payload (ikep, current_length, &nonce);
1493 11 : if (nonce_len <= 0)
1494 0 : goto cleanup_and_exit;
1495 : }
1496 22 : else if (payload == IKEV2_PAYLOAD_TSI)
1497 : {
1498 11 : tsi = ikev2_parse_ts_payload (ikep, current_length);
1499 : }
1500 11 : else if (payload == IKEV2_PAYLOAD_TSR)
1501 : {
1502 11 : tsr = ikev2_parse_ts_payload (ikep, current_length);
1503 : }
1504 : else
1505 : {
1506 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1507 : payload);
1508 0 : if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1509 : {
1510 0 : sa->unsupported_cp = payload;
1511 0 : goto cleanup_and_exit;
1512 : }
1513 : }
1514 75 : payload = ikep->nextpayload;
1515 75 : p += plen;
1516 : }
1517 :
1518 11 : if (!proposal || proposal->protocol_id != IKEV2_PROTOCOL_ESP || !nonce)
1519 0 : goto cleanup_and_exit;
1520 :
1521 11 : if (sa->is_initiator)
1522 : {
1523 3 : rekey = sa->rekey;
1524 3 : if (vec_len (rekey) == 0)
1525 1 : goto cleanup_and_exit;
1526 2 : rekey->notify_type = 0;
1527 2 : rekey->protocol_id = proposal->protocol_id;
1528 4 : rekey->i_proposal =
1529 2 : ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1530 2 : rekey->i_proposal->spi = rekey->spi;
1531 2 : rekey->r_proposal = proposal;
1532 2 : rekey->tsi = tsi;
1533 2 : rekey->tsr = tsr;
1534 : /* update Nr */
1535 2 : vec_reset_length (sa->r_nonce);
1536 2 : vec_add (sa->r_nonce, nonce, nonce_len);
1537 2 : child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1538 2 : if (child_sa)
1539 : {
1540 2 : child_sa->rekey_retries = 0;
1541 : }
1542 : }
1543 : else
1544 : {
1545 8 : if (n)
1546 : {
1547 8 : child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1548 8 : if (!child_sa)
1549 : {
1550 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1551 : n->spi);
1552 0 : goto cleanup_and_exit;
1553 : }
1554 8 : vec_add2 (sa->rekey, rekey, 1);
1555 8 : rekey->notify_type = 0;
1556 8 : rekey->kex = 0;
1557 8 : rekey->protocol_id = n->protocol_id;
1558 8 : rekey->spi = n->spi;
1559 8 : if (sa->old_remote_id_present)
1560 : {
1561 2 : rekey->notify_type = IKEV2_NOTIFY_MSG_TEMPORARY_FAILURE;
1562 2 : vec_free (proposal);
1563 2 : vec_free (tsr);
1564 2 : vec_free (tsi);
1565 : }
1566 6 : else if (!ikev2_process_create_child_sa_rekey (
1567 : sa, &sar, rekey, proposal, tsi, tsr, nonce, nonce_len))
1568 : {
1569 0 : vec_free (proposal);
1570 0 : vec_free (tsr);
1571 0 : vec_free (tsi);
1572 : }
1573 : }
1574 : else
1575 : {
1576 : /* create new child SA */
1577 0 : vec_add2 (sa->new_child, rekey, 1);
1578 0 : rekey->notify_type = 0;
1579 0 : rekey->kex = 0;
1580 0 : if (!ikev2_process_create_child_sa_rekey (
1581 : sa, &sar, rekey, proposal, tsi, tsr, nonce, nonce_len))
1582 : {
1583 0 : vec_free (proposal);
1584 0 : vec_free (tsr);
1585 0 : vec_free (tsi);
1586 : }
1587 : }
1588 : }
1589 10 : vec_free (n);
1590 10 : ikev2_sa_free_all_vec (&sar);
1591 10 : return 1;
1592 :
1593 1 : cleanup_and_exit:
1594 1 : vec_free (n);
1595 1 : vec_free (proposal);
1596 1 : vec_free (tsr);
1597 1 : vec_free (tsi);
1598 1 : ikev2_sa_free_all_vec (&sar);
1599 1 : return 0;
1600 : }
1601 :
1602 : static u8 *
1603 38 : ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1604 : {
1605 38 : u8 *authmsg = 0;
1606 : u8 *data;
1607 : u8 *nonce;
1608 : ikev2_id_t *id;
1609 : u8 *key;
1610 : u8 *packet_data;
1611 : ikev2_sa_transform_t *tr_prf;
1612 :
1613 : tr_prf =
1614 38 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1615 :
1616 38 : if (is_responder)
1617 : {
1618 19 : id = &sa->r_id;
1619 19 : key = sa->sk_pr;
1620 19 : nonce = sa->i_nonce;
1621 19 : packet_data = sa->last_sa_init_res_packet_data;
1622 : }
1623 : else
1624 : {
1625 19 : id = &sa->i_id;
1626 19 : key = sa->sk_pi;
1627 19 : nonce = sa->r_nonce;
1628 19 : packet_data = sa->last_sa_init_req_packet_data;
1629 : }
1630 :
1631 38 : data = vec_new (u8, 4);
1632 38 : data[0] = id->type;
1633 38 : vec_append (data, id->data);
1634 :
1635 38 : u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1636 38 : vec_append (authmsg, packet_data);
1637 38 : vec_append (authmsg, nonce);
1638 38 : vec_append (authmsg, id_hash);
1639 38 : vec_free (id_hash);
1640 38 : vec_free (data);
1641 :
1642 38 : return authmsg;
1643 : }
1644 :
1645 : static int
1646 38 : ikev2_match_profile (const ikev2_profile_t *p, const ikev2_id_t *id_loc,
1647 : const ikev2_id_t *id_rem, int is_initiator)
1648 : {
1649 : /* on the initiator, IDi is always present and must match
1650 : * however on the responder, IDr (which is our local id) is optional */
1651 75 : if ((is_initiator || id_loc->type != 0) &&
1652 37 : !ikev2_is_id_equal (&p->loc_id, id_loc))
1653 0 : return 0;
1654 :
1655 : /* on the initiator, we might not have configured a specific remote id
1656 : * however on the responder, the remote id should always be configured */
1657 74 : if ((!is_initiator || p->rem_id.type != 0) &&
1658 36 : !ikev2_is_id_equal (&p->rem_id, id_rem))
1659 0 : return 0;
1660 :
1661 38 : return 1;
1662 : }
1663 :
1664 : static int
1665 38 : ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
1666 : {
1667 38 : if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1668 76 : ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1669 76 : !ip_address_cmp (&ts1->start_addr, &ts2->start_addr) &&
1670 38 : !ip_address_cmp (&ts1->end_addr, &ts2->end_addr))
1671 38 : return 1;
1672 :
1673 0 : return 0;
1674 : }
1675 :
1676 : static void
1677 19 : ikev2_sa_match_ts (ikev2_sa_t * sa)
1678 : {
1679 19 : ikev2_main_t *km = &ikev2_main;
1680 : ikev2_profile_t *p;
1681 19 : ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1682 : ikev2_id_t *id_rem, *id_loc;
1683 :
1684 : /* *INDENT-OFF* */
1685 19 : pool_foreach (p, km->profiles) {
1686 :
1687 19 : if (sa->is_initiator)
1688 : {
1689 6 : p_tsi = &p->loc_ts;
1690 6 : p_tsr = &p->rem_ts;
1691 6 : id_rem = &sa->r_id;
1692 6 : id_loc = &sa->i_id;
1693 : }
1694 : else
1695 : {
1696 13 : p_tsi = &p->rem_ts;
1697 13 : p_tsr = &p->loc_ts;
1698 13 : id_rem = &sa->i_id;
1699 13 : id_loc = &sa->r_id;
1700 : }
1701 :
1702 19 : if (!ikev2_match_profile (p, id_loc, id_rem, sa->is_initiator))
1703 0 : continue;
1704 :
1705 19 : sa->profile_index = p - km->profiles;
1706 :
1707 19 : vec_foreach(ts, sa->childs[0].tsi)
1708 : {
1709 19 : if (ikev2_ts_cmp(p_tsi, ts))
1710 : {
1711 19 : vec_add1 (tsi, ts[0]);
1712 19 : break;
1713 : }
1714 : }
1715 :
1716 19 : vec_foreach(ts, sa->childs[0].tsr)
1717 : {
1718 19 : if (ikev2_ts_cmp(p_tsr, ts))
1719 : {
1720 19 : vec_add1 (tsr, ts[0]);
1721 19 : break;
1722 : }
1723 : }
1724 :
1725 19 : break;
1726 : }
1727 : /* *INDENT-ON* */
1728 :
1729 19 : if (tsi && tsr)
1730 : {
1731 19 : vec_free (sa->childs[0].tsi);
1732 19 : vec_free (sa->childs[0].tsr);
1733 19 : sa->childs[0].tsi = tsi;
1734 19 : sa->childs[0].tsr = tsr;
1735 : }
1736 : else
1737 : {
1738 0 : vec_free (tsi);
1739 0 : vec_free (tsr);
1740 0 : ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
1741 : }
1742 19 : }
1743 :
1744 : static ikev2_profile_t *
1745 19 : ikev2_select_profile (ikev2_main_t *km, ikev2_sa_t *sa,
1746 : ikev2_sa_transform_t *tr_prf, u8 *key_pad)
1747 : {
1748 19 : ikev2_profile_t *ret = 0, *p;
1749 : ikev2_id_t *id_rem, *id_loc;
1750 : ikev2_auth_t *sa_auth;
1751 19 : u8 *authmsg, *psk = 0, *auth = 0;
1752 :
1753 19 : authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1754 :
1755 19 : if (sa->is_initiator)
1756 : {
1757 6 : id_rem = &sa->r_id;
1758 6 : id_loc = &sa->i_id;
1759 6 : sa_auth = &sa->r_auth;
1760 : }
1761 : else
1762 : {
1763 13 : id_rem = &sa->i_id;
1764 13 : id_loc = &sa->r_id;
1765 13 : sa_auth = &sa->i_auth;
1766 : }
1767 :
1768 19 : pool_foreach (p, km->profiles)
1769 : {
1770 19 : if (!ikev2_match_profile (p, id_loc, id_rem, sa->is_initiator))
1771 0 : continue;
1772 :
1773 19 : if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1774 : {
1775 18 : if (!p->auth.data ||
1776 18 : p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1777 0 : continue;
1778 :
1779 18 : psk = ikev2_calc_prf (tr_prf, p->auth.data, key_pad);
1780 18 : auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1781 :
1782 18 : if (!clib_memcmp (auth, sa_auth->data, vec_len (sa_auth->data)))
1783 : {
1784 18 : ikev2_set_state (sa, IKEV2_STATE_AUTHENTICATED);
1785 18 : vec_free (auth);
1786 18 : ret = p;
1787 18 : break;
1788 : }
1789 : else
1790 : {
1791 0 : ikev2_elog_uint (IKEV2_LOG_ERROR,
1792 : "shared key mismatch! ispi %lx", sa->ispi);
1793 : }
1794 : }
1795 1 : else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1796 : {
1797 1 : if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1798 0 : continue;
1799 :
1800 1 : if (ikev2_verify_sign (p->auth.key, sa_auth->data, authmsg) == 1)
1801 : {
1802 1 : ikev2_set_state (sa, IKEV2_STATE_AUTHENTICATED);
1803 1 : ret = p;
1804 1 : break;
1805 : }
1806 : else
1807 : {
1808 0 : ikev2_elog_uint (IKEV2_LOG_ERROR,
1809 : "cert verification failed! ispi %lx", sa->ispi);
1810 : }
1811 : }
1812 : }
1813 19 : vec_free (authmsg);
1814 19 : return ret;
1815 : }
1816 :
1817 : static void
1818 19 : ikev2_sa_auth (ikev2_sa_t *sa)
1819 : {
1820 19 : ikev2_main_t *km = &ikev2_main;
1821 19 : ikev2_profile_t *sel_p = 0;
1822 : ikev2_sa_transform_t *tr_prf;
1823 : u8 *psk, *authmsg, *key_pad;
1824 :
1825 : tr_prf =
1826 19 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1827 :
1828 : /* only shared key and rsa signature */
1829 19 : if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1830 1 : sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1831 : {
1832 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "unsupported authentication method %u",
1833 : sa->i_auth.method);
1834 0 : ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1835 0 : return;
1836 : }
1837 :
1838 19 : key_pad = format (0, "%s", IKEV2_KEY_PAD);
1839 19 : sel_p = ikev2_select_profile (km, sa, tr_prf, key_pad);
1840 :
1841 19 : if (sel_p)
1842 : {
1843 19 : ASSERT (sa->state == IKEV2_STATE_AUTHENTICATED);
1844 19 : sa->udp_encap = sel_p->udp_encap;
1845 19 : sa->ipsec_over_udp_port = sel_p->ipsec_over_udp_port;
1846 :
1847 19 : if (!sa->is_initiator)
1848 : {
1849 13 : vec_free (sa->r_id.data);
1850 13 : sa->r_id.data = vec_dup (sel_p->loc_id.data);
1851 13 : sa->r_id.type = sel_p->loc_id.type;
1852 13 : sa->i_id.data = vec_dup (sel_p->rem_id.data);
1853 13 : sa->i_id.type = sel_p->rem_id.type;
1854 :
1855 : /* generate our auth data */
1856 13 : authmsg = ikev2_sa_generate_authmsg (sa, 1);
1857 13 : if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1858 : {
1859 12 : vec_free (sa->r_auth.data);
1860 12 : psk = ikev2_calc_prf (tr_prf, sel_p->auth.data, key_pad);
1861 12 : sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1862 12 : sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1863 12 : vec_free (psk);
1864 : }
1865 1 : else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1866 : {
1867 1 : vec_free (sa->r_auth.data);
1868 1 : sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1869 1 : sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1870 : }
1871 13 : vec_free (authmsg);
1872 :
1873 : /* select transforms for 1st child sa */
1874 13 : ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1875 26 : sa->childs[0].r_proposals =
1876 13 : ikev2_select_proposal (sa->childs[0].i_proposals,
1877 : IKEV2_PROTOCOL_ESP);
1878 :
1879 13 : if (~0 != sel_p->tun_itf)
1880 : {
1881 0 : sa->is_tun_itf_set = 1;
1882 0 : sa->tun_itf = sel_p->tun_itf;
1883 : }
1884 : }
1885 : }
1886 : else
1887 : {
1888 0 : ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1889 : "profile found! ispi %lx", sa->ispi);
1890 0 : ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1891 : }
1892 19 : vec_free (key_pad);
1893 : }
1894 :
1895 : static void
1896 6 : ikev2_sa_auth_init (ikev2_sa_t * sa)
1897 : {
1898 6 : ikev2_main_t *km = &ikev2_main;
1899 6 : u8 *authmsg, *key_pad, *psk = 0;
1900 : ikev2_sa_transform_t *tr_prf;
1901 :
1902 : tr_prf =
1903 6 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1904 :
1905 : /* only shared key and rsa signature */
1906 6 : if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1907 0 : sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1908 : {
1909 0 : ikev2_elog_uint (IKEV2_LOG_ERROR,
1910 : "unsupported authentication method %u",
1911 : sa->i_auth.method);
1912 0 : ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1913 0 : return;
1914 : }
1915 :
1916 6 : authmsg = ikev2_sa_generate_authmsg (sa, 0);
1917 :
1918 6 : if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1919 : {
1920 6 : key_pad = format (0, "%s", IKEV2_KEY_PAD);
1921 6 : psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1922 6 : vec_free (sa->i_auth.data);
1923 6 : sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1924 6 : sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1925 6 : vec_free (psk);
1926 6 : vec_free (key_pad);
1927 : }
1928 0 : else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1929 : {
1930 0 : vec_free (sa->i_auth.data);
1931 0 : sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1932 0 : sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1933 : }
1934 6 : vec_free (authmsg);
1935 : }
1936 :
1937 : static u32
1938 27 : ikev2_mk_local_sa_id (u32 sai, u32 ci, u32 ti)
1939 : {
1940 27 : return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1941 : }
1942 :
1943 : static u32
1944 27 : ikev2_mk_remote_sa_id (u32 sai, u32 ci, u32 ti)
1945 : {
1946 27 : return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1947 : }
1948 :
1949 : typedef struct
1950 : {
1951 : u32 sw_if_index;
1952 : u32 salt_local;
1953 : u32 salt_remote;
1954 : u32 local_sa_id;
1955 : u32 remote_sa_id;
1956 : ipsec_sa_flags_t flags;
1957 : u32 local_spi;
1958 : u32 remote_spi;
1959 : ipsec_crypto_alg_t encr_type;
1960 : ipsec_integ_alg_t integ_type;
1961 : ip_address_t local_ip;
1962 : ip_address_t remote_ip;
1963 : ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1964 : u8 is_rekey;
1965 : u32 old_remote_sa_id;
1966 : u16 ipsec_over_udp_port;
1967 : u16 src_port;
1968 : u16 dst_port;
1969 : } ikev2_add_ipsec_tunnel_args_t;
1970 :
1971 : static void
1972 27 : ikev2_add_tunnel_from_main (ikev2_add_ipsec_tunnel_args_t * a)
1973 : {
1974 27 : ikev2_main_t *km = &ikev2_main;
1975 : u32 sw_if_index;
1976 27 : int rv = 0;
1977 27 : tunnel_t tun_in = {
1978 : .t_flags = TUNNEL_FLAG_NONE,
1979 : .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
1980 : .t_dscp = 0,
1981 : .t_mode = TUNNEL_MODE_P2P,
1982 : .t_table_id = 0,
1983 : .t_hop_limit = 255,
1984 : .t_src = a->remote_ip,
1985 : .t_dst = a->local_ip,
1986 : };
1987 27 : tunnel_t tun_out = {
1988 : .t_flags = TUNNEL_FLAG_NONE,
1989 : .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
1990 : .t_dscp = 0,
1991 : .t_mode = TUNNEL_MODE_P2P,
1992 : .t_table_id = 0,
1993 : .t_hop_limit = 255,
1994 : .t_src = a->local_ip,
1995 : .t_dst = a->remote_ip,
1996 : };
1997 :
1998 27 : if (~0 == a->sw_if_index)
1999 : {
2000 : /* no tunnel associated with the SA/profile - create a new one */
2001 27 : rv = ipip_add_tunnel (IPIP_TRANSPORT_IP4, ~0, &ip_addr_46 (&a->local_ip),
2002 : &ip_addr_46 (&a->remote_ip), 0,
2003 : TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
2004 : TUNNEL_MODE_P2P, &sw_if_index);
2005 :
2006 27 : if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
2007 : {
2008 8 : if (hash_get (km->sw_if_indices, sw_if_index))
2009 : /* interface is managed by IKE; proceed with updating SAs */
2010 8 : rv = 0;
2011 : }
2012 27 : hash_set1 (km->sw_if_indices, sw_if_index);
2013 : }
2014 : else
2015 : {
2016 0 : sw_if_index = a->sw_if_index;
2017 0 : vnet_sw_interface_admin_up (vnet_get_main (), sw_if_index);
2018 : }
2019 :
2020 27 : if (rv)
2021 : {
2022 0 : ikev2_elog_uint (IKEV2_LOG_ERROR,
2023 : "installing ipip tunnel failed! local spi: %x",
2024 : a->local_spi);
2025 27 : return;
2026 : }
2027 :
2028 27 : u32 *sas_in = NULL;
2029 27 : vec_add1 (sas_in, a->remote_sa_id);
2030 27 : if (a->is_rekey)
2031 : {
2032 8 : ipsec_tun_protect_del (sw_if_index, NULL);
2033 :
2034 : /* replace local SA immediately */
2035 8 : ipsec_sa_unlock_id (a->local_sa_id);
2036 :
2037 : /* keep the old sa */
2038 8 : vec_add1 (sas_in, a->old_remote_sa_id);
2039 : }
2040 :
2041 27 : rv = ipsec_sa_add_and_lock (a->local_sa_id, a->local_spi, IPSEC_PROTOCOL_ESP,
2042 27 : a->encr_type, &a->loc_ckey, a->integ_type,
2043 27 : &a->loc_ikey, a->flags, a->salt_local,
2044 27 : a->src_port, a->dst_port, &tun_out, NULL);
2045 27 : if (rv)
2046 0 : goto err0;
2047 :
2048 27 : rv = ipsec_sa_add_and_lock (
2049 27 : a->remote_sa_id, a->remote_spi, IPSEC_PROTOCOL_ESP, a->encr_type,
2050 27 : &a->rem_ckey, a->integ_type, &a->rem_ikey,
2051 27 : (a->flags | IPSEC_SA_FLAG_IS_INBOUND), a->salt_remote,
2052 27 : a->ipsec_over_udp_port, a->ipsec_over_udp_port, &tun_in, NULL);
2053 27 : if (rv)
2054 0 : goto err1;
2055 :
2056 27 : rv = ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
2057 27 : if (rv)
2058 0 : goto err2;
2059 :
2060 27 : return;
2061 :
2062 0 : err2:
2063 0 : ipsec_sa_unlock_id (a->remote_sa_id);
2064 0 : err1:
2065 0 : ipsec_sa_unlock_id (a->local_sa_id);
2066 0 : err0:
2067 0 : vec_free (sas_in);
2068 : }
2069 :
2070 : static int
2071 27 : ikev2_create_tunnel_interface (vlib_main_t *vm, ikev2_sa_t *sa,
2072 : ikev2_child_sa_t *child, u32 sa_index,
2073 : u32 child_index, u8 is_rekey, u8 kex)
2074 : {
2075 27 : u32 thread_index = vlib_get_thread_index ();
2076 27 : ikev2_main_t *km = &ikev2_main;
2077 : ipsec_crypto_alg_t encr_type;
2078 : ipsec_integ_alg_t integ_type;
2079 27 : ikev2_profile_t *p = 0;
2080 : ikev2_sa_transform_t *tr;
2081 : ikev2_sa_proposal_t *proposals;
2082 27 : u8 is_aead = 0;
2083 : ikev2_add_ipsec_tunnel_args_t a;
2084 :
2085 27 : clib_memset (&a, 0, sizeof (a));
2086 :
2087 27 : if (!child->r_proposals)
2088 : {
2089 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2090 0 : return 1;
2091 : }
2092 :
2093 27 : if (sa->is_initiator)
2094 : {
2095 8 : ip_address_copy (&a.local_ip, &sa->iaddr);
2096 8 : ip_address_copy (&a.remote_ip, &sa->raddr);
2097 8 : proposals = child->r_proposals;
2098 8 : a.local_spi = child->r_proposals[0].spi;
2099 8 : a.remote_spi = child->i_proposals[0].spi;
2100 : }
2101 : else
2102 : {
2103 19 : ip_address_copy (&a.local_ip, &sa->raddr);
2104 19 : ip_address_copy (&a.remote_ip, &sa->iaddr);
2105 19 : proposals = child->i_proposals;
2106 19 : a.local_spi = child->i_proposals[0].spi;
2107 19 : a.remote_spi = child->r_proposals[0].spi;
2108 : }
2109 :
2110 27 : a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
2111 27 : if (sa->udp_encap)
2112 : {
2113 1 : a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
2114 1 : a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
2115 : }
2116 27 : if (ikev2_natt_active (sa))
2117 3 : a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
2118 27 : a.is_rekey = is_rekey;
2119 :
2120 27 : tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
2121 27 : if (tr && tr->esn_type)
2122 2 : a.flags |= IPSEC_SA_FLAG_USE_ESN;
2123 :
2124 27 : tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
2125 27 : if (tr)
2126 : {
2127 27 : if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
2128 : {
2129 26 : switch (tr->key_len)
2130 : {
2131 0 : case 16:
2132 0 : encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
2133 0 : break;
2134 1 : case 24:
2135 1 : encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
2136 1 : break;
2137 25 : case 32:
2138 25 : encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
2139 25 : break;
2140 0 : default:
2141 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2142 0 : return 1;
2143 : break;
2144 : }
2145 : }
2146 1 : else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
2147 1 : && tr->key_len)
2148 : {
2149 1 : switch (tr->key_len)
2150 : {
2151 0 : case 16:
2152 0 : encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
2153 0 : break;
2154 0 : case 24:
2155 0 : encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
2156 0 : break;
2157 1 : case 32:
2158 1 : encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
2159 1 : break;
2160 0 : default:
2161 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2162 0 : return 1;
2163 : break;
2164 : }
2165 1 : is_aead = 1;
2166 : }
2167 : else
2168 : {
2169 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2170 0 : return 1;
2171 : }
2172 : }
2173 : else
2174 : {
2175 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2176 0 : return 1;
2177 : }
2178 27 : a.encr_type = encr_type;
2179 :
2180 27 : if (!is_aead)
2181 : {
2182 26 : tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
2183 26 : if (tr)
2184 : {
2185 26 : switch (tr->integ_type)
2186 : {
2187 8 : case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
2188 8 : integ_type = IPSEC_INTEG_ALG_SHA_256_128;
2189 8 : break;
2190 1 : case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
2191 1 : integ_type = IPSEC_INTEG_ALG_SHA_384_192;
2192 1 : break;
2193 0 : case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
2194 0 : integ_type = IPSEC_INTEG_ALG_SHA_512_256;
2195 0 : break;
2196 17 : case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
2197 17 : integ_type = IPSEC_INTEG_ALG_SHA1_96;
2198 17 : break;
2199 0 : default:
2200 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2201 0 : return 1;
2202 : }
2203 : }
2204 : else
2205 : {
2206 0 : ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
2207 0 : return 1;
2208 : }
2209 : }
2210 : else
2211 : {
2212 1 : integ_type = IPSEC_INTEG_ALG_NONE;
2213 : }
2214 :
2215 27 : a.integ_type = integ_type;
2216 27 : ikev2_calc_child_keys (sa, child, kex);
2217 :
2218 27 : if (sa->is_initiator)
2219 : {
2220 8 : ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
2221 8 : ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
2222 8 : ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
2223 8 : ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
2224 8 : if (is_aead)
2225 : {
2226 0 : a.salt_remote = child->salt_er;
2227 0 : a.salt_local = child->salt_ei;
2228 : }
2229 8 : a.dst_port = a.src_port = sa->ipsec_over_udp_port;
2230 : }
2231 : else
2232 : {
2233 19 : ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
2234 19 : ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
2235 19 : ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
2236 19 : ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
2237 19 : if (is_aead)
2238 : {
2239 1 : a.salt_remote = child->salt_ei;
2240 1 : a.salt_local = child->salt_er;
2241 : }
2242 19 : a.dst_port =
2243 19 : ikev2_natt_active (sa) ? sa->dst_port : sa->ipsec_over_udp_port;
2244 19 : a.src_port = sa->ipsec_over_udp_port;
2245 : }
2246 :
2247 27 : if (sa->is_initiator && sa->profile_index != ~0)
2248 8 : p = pool_elt_at_index (km->profiles, sa->profile_index);
2249 :
2250 27 : if (p && p->lifetime)
2251 : {
2252 0 : child->time_to_expiration = vlib_time_now (vm) + p->lifetime;
2253 0 : if (p->lifetime_jitter)
2254 : {
2255 : // This is not much better than rand(3), which Coverity warns
2256 : // is unsuitable for security applications; random_u32 is
2257 : // however fast. If this perturbance to the expiration time
2258 : // needs to use a better RNG then we may need to use something
2259 : // like /dev/urandom which has significant overhead.
2260 0 : u32 rnd = (u32) (vlib_time_now (vm) * 1e6);
2261 0 : rnd = random_u32 (&rnd);
2262 :
2263 0 : child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
2264 : }
2265 : }
2266 :
2267 27 : if (thread_index & 0xffffffc0)
2268 0 : ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
2269 :
2270 27 : if (child_index & 0xfffff000 || sa_index & 0xfffff000)
2271 0 : ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
2272 :
2273 27 : child->local_sa_id =
2274 27 : a.local_sa_id =
2275 27 : ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
2276 :
2277 27 : u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
2278 : thread_index);
2279 :
2280 27 : if (is_rekey)
2281 : {
2282 : /* create a new remote SA ID to keep the old SA for a bit longer
2283 : * so the peer has some time to swap their SAs */
2284 :
2285 : /* use most significat bit of child index part in id */
2286 8 : u32 mask = 0x800;
2287 8 : if (sa->current_remote_id_mask)
2288 : {
2289 2 : sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
2290 2 : sa->current_remote_id_mask = 0;
2291 : }
2292 : else
2293 : {
2294 6 : sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
2295 6 : sa->current_remote_id_mask = mask;
2296 6 : remote_sa_id |= mask;
2297 : }
2298 8 : sa->old_id_expiration = 3.0;
2299 8 : sa->old_remote_id_present = 1;
2300 : }
2301 :
2302 27 : child->remote_sa_id = a.remote_sa_id = remote_sa_id;
2303 :
2304 27 : a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
2305 27 : a.ipsec_over_udp_port = sa->ipsec_over_udp_port;
2306 :
2307 27 : vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main,
2308 : (u8 *) & a, sizeof (a));
2309 27 : return 0;
2310 : }
2311 :
2312 : typedef struct
2313 : {
2314 : ip46_address_t local_ip;
2315 : ip46_address_t remote_ip;
2316 : u32 remote_sa_id;
2317 : u32 local_sa_id;
2318 : u32 sw_if_index;
2319 : } ikev2_del_ipsec_tunnel_args_t;
2320 :
2321 : static u32
2322 21 : ikev2_flip_alternate_sa_bit (u32 id)
2323 : {
2324 21 : u32 mask = 0x800;
2325 21 : if (mask & id)
2326 6 : return id & ~mask;
2327 15 : return id | mask;
2328 : }
2329 :
2330 : static void
2331 19 : ikev2_del_tunnel_from_main (ikev2_del_ipsec_tunnel_args_t * a)
2332 : {
2333 19 : ikev2_main_t *km = &ikev2_main;
2334 19 : ipip_tunnel_t *ipip = NULL;
2335 : u32 sw_if_index;
2336 :
2337 19 : if (~0 == a->sw_if_index)
2338 : {
2339 : /* *INDENT-OFF* */
2340 19 : ipip_tunnel_key_t key = {
2341 : .src = a->local_ip,
2342 : .dst = a->remote_ip,
2343 : .transport = IPIP_TRANSPORT_IP4,
2344 : .fib_index = 0,
2345 : };
2346 : /* *INDENT-ON* */
2347 :
2348 19 : ipip = ipip_tunnel_db_find (&key);
2349 :
2350 19 : if (ipip)
2351 : {
2352 19 : sw_if_index = ipip->sw_if_index;
2353 19 : hash_unset (km->sw_if_indices, ipip->sw_if_index);
2354 : }
2355 : else
2356 0 : sw_if_index = ~0;
2357 : }
2358 : else
2359 : {
2360 0 : sw_if_index = a->sw_if_index;
2361 0 : vnet_sw_interface_admin_down (vnet_get_main (), sw_if_index);
2362 : }
2363 :
2364 19 : if (~0 != sw_if_index)
2365 19 : ipsec_tun_protect_del (sw_if_index, NULL);
2366 :
2367 19 : ipsec_sa_unlock_id (a->remote_sa_id);
2368 19 : ipsec_sa_unlock_id (a->local_sa_id);
2369 19 : ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (a->remote_sa_id));
2370 :
2371 19 : if (ipip)
2372 19 : ipip_del_tunnel (ipip->sw_if_index);
2373 19 : }
2374 :
2375 : static int
2376 19 : ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
2377 : ikev2_child_sa_t * child)
2378 : {
2379 : ikev2_del_ipsec_tunnel_args_t a;
2380 :
2381 19 : clib_memset (&a, 0, sizeof (a));
2382 :
2383 19 : if (sa->is_initiator)
2384 : {
2385 6 : ip_address_to_46 (&sa->iaddr, &a.local_ip);
2386 6 : ip_address_to_46 (&sa->raddr, &a.remote_ip);
2387 : }
2388 : else
2389 : {
2390 13 : ip_address_to_46 (&sa->raddr, &a.local_ip);
2391 13 : ip_address_to_46 (&sa->iaddr, &a.remote_ip);
2392 : }
2393 :
2394 19 : a.remote_sa_id = child->remote_sa_id;
2395 19 : a.local_sa_id = child->local_sa_id;
2396 19 : a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
2397 :
2398 19 : vl_api_rpc_call_main_thread (ikev2_del_tunnel_from_main, (u8 *) & a,
2399 : sizeof (a));
2400 19 : return 0;
2401 : }
2402 :
2403 : static void
2404 0 : ikev2_add_invalid_ke_payload (ikev2_sa_t *sa, ikev2_payload_chain_t *chain)
2405 : {
2406 0 : u8 *data = vec_new (u8, 2);
2407 : ikev2_sa_transform_t *tr_dh =
2408 0 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
2409 0 : ASSERT (tr_dh && tr_dh->dh_type);
2410 0 : data[0] = (tr_dh->dh_type >> 8) & 0xff;
2411 0 : data[1] = (tr_dh->dh_type) & 0xff;
2412 0 : ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD, data);
2413 0 : vec_free (data);
2414 0 : }
2415 :
2416 : static void
2417 8 : ikev2_add_create_child_resp (ikev2_sa_t *sa, ikev2_rekey_t *rekey,
2418 : ikev2_payload_chain_t *chain)
2419 : {
2420 8 : if (rekey->notify_type)
2421 : {
2422 2 : if (rekey->notify_type == IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD)
2423 0 : ikev2_add_invalid_ke_payload (sa, chain);
2424 : else
2425 2 : ikev2_payload_add_notify (chain, rekey->notify_type, 0);
2426 2 : return;
2427 : }
2428 :
2429 6 : ikev2_payload_add_sa (chain, rekey->r_proposal);
2430 6 : ikev2_payload_add_nonce (chain, sa->r_nonce);
2431 6 : if (rekey->kex)
2432 3 : ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
2433 6 : ikev2_payload_add_ts (chain, rekey->tsi, IKEV2_PAYLOAD_TSI);
2434 6 : ikev2_payload_add_ts (chain, rekey->tsr, IKEV2_PAYLOAD_TSR);
2435 : }
2436 :
2437 : static u32
2438 64 : ikev2_generate_message (vlib_buffer_t *b, ikev2_sa_t *sa, ike_header_t *ike,
2439 : void *user, udp_header_t *udp, ikev2_stats_t *stats)
2440 : {
2441 64 : ikev2_main_t *km = &ikev2_main;
2442 64 : u16 buffer_data_size = vlib_buffer_get_default_data_size (km->vlib_main);
2443 64 : v8 *integ = 0;
2444 : ike_payload_header_t *ph;
2445 : u16 plen;
2446 64 : u32 tlen = 0;
2447 :
2448 : ikev2_sa_transform_t *tr_encr, *tr_integ;
2449 : tr_encr =
2450 64 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
2451 : tr_integ =
2452 64 : ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
2453 :
2454 64 : ikev2_payload_chain_t *chain = 0;
2455 64 : ikev2_payload_new_chain (chain);
2456 :
2457 64 : if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2458 : {
2459 13 : if (sa->r_proposals == 0)
2460 : {
2461 0 : ikev2_payload_add_notify (chain,
2462 : IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2463 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2464 : }
2465 13 : else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
2466 : {
2467 0 : ikev2_add_invalid_ke_payload (sa, chain);
2468 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2469 : }
2470 13 : else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2471 : {
2472 0 : u8 *data = vec_new (u8, 1);
2473 :
2474 0 : data[0] = sa->unsupported_cp;
2475 0 : ikev2_payload_add_notify (chain,
2476 : IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2477 : data);
2478 0 : vec_free (data);
2479 : }
2480 : else
2481 : {
2482 13 : ASSERT (udp);
2483 :
2484 13 : ike->rspi = clib_host_to_net_u64 (sa->rspi);
2485 13 : ikev2_payload_add_sa (chain, sa->r_proposals);
2486 13 : ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
2487 13 : ikev2_payload_add_nonce (chain, sa->r_nonce);
2488 :
2489 13 : u8 *nat_detection_sha1 =
2490 13 : ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
2491 : clib_host_to_net_u64 (sa->rspi),
2492 13 : &sa->raddr, udp->dst_port);
2493 13 : ikev2_payload_add_notify (chain,
2494 : IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
2495 : nat_detection_sha1);
2496 13 : vec_free (nat_detection_sha1);
2497 13 : nat_detection_sha1 =
2498 13 : ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
2499 : clib_host_to_net_u64 (sa->rspi),
2500 13 : &sa->iaddr, udp->src_port);
2501 13 : ikev2_payload_add_notify (chain,
2502 : IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
2503 : nat_detection_sha1);
2504 13 : vec_free (nat_detection_sha1);
2505 : }
2506 : }
2507 51 : else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2508 : {
2509 19 : if (sa->state == IKEV2_STATE_AUTHENTICATED)
2510 : {
2511 13 : ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
2512 13 : ikev2_payload_add_auth (chain, &sa->r_auth);
2513 13 : ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
2514 13 : ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2515 13 : ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2516 : }
2517 6 : else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2518 : {
2519 0 : ikev2_payload_add_notify (chain,
2520 : IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
2521 : 0);
2522 0 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2523 : }
2524 6 : else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
2525 : {
2526 0 : ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
2527 : 0);
2528 0 : ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
2529 0 : ikev2_payload_add_auth (chain, &sa->r_auth);
2530 : }
2531 6 : else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
2532 : {
2533 0 : ikev2_payload_add_notify (chain,
2534 : IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2535 0 : ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
2536 0 : ikev2_payload_add_auth (chain, &sa->r_auth);
2537 0 : ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2538 0 : ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2539 : }
2540 6 : else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2541 : {
2542 0 : u8 *data = vec_new (u8, 1);
2543 :
2544 0 : data[0] = sa->unsupported_cp;
2545 0 : ikev2_payload_add_notify (chain,
2546 : IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2547 : data);
2548 0 : vec_free (data);
2549 : }
2550 6 : else if (sa->state == IKEV2_STATE_SA_INIT)
2551 : {
2552 6 : ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
2553 : /* IDr is optional when sending INIT from the initiator */
2554 6 : ASSERT (sa->r_id.type != 0 || sa->is_initiator);
2555 6 : if (sa->r_id.type != 0)
2556 5 : ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
2557 6 : ikev2_payload_add_auth (chain, &sa->i_auth);
2558 6 : ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2559 6 : ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2560 6 : ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2561 6 : ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2562 : 0);
2563 : }
2564 : else
2565 : {
2566 0 : ikev2_set_state (sa, IKEV2_STATE_DELETED, ": unexpected IKE_AUTH");
2567 0 : goto done;
2568 : }
2569 : }
2570 32 : else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2571 : {
2572 : /* if pending delete */
2573 21 : if (sa->del)
2574 : {
2575 18 : if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2576 : {
2577 18 : if (ike_hdr_is_request (ike))
2578 6 : ikev2_payload_add_delete (chain, sa->del);
2579 :
2580 : /* The response to a request that deletes the IKE SA is an empty
2581 : INFORMATIONAL response. */
2582 18 : ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2583 : }
2584 : /* The response to a request that deletes ESP or AH SAs will contain
2585 : delete payloads for the paired SAs going in the other direction. */
2586 : else
2587 : {
2588 0 : ikev2_payload_add_delete (chain, sa->del);
2589 : }
2590 18 : vec_free (sa->del);
2591 18 : sa->del = 0;
2592 : }
2593 : /* received N(AUTHENTICATION_FAILED) */
2594 3 : else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2595 : {
2596 0 : ikev2_set_state (sa, IKEV2_STATE_DELETED, ": auth failed");
2597 0 : goto done;
2598 : }
2599 : /* received unsupported critical payload */
2600 3 : else if (sa->unsupported_cp)
2601 : {
2602 0 : u8 *data = vec_new (u8, 1);
2603 :
2604 0 : data[0] = sa->unsupported_cp;
2605 0 : ikev2_payload_add_notify (chain,
2606 : IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2607 : data);
2608 0 : vec_free (data);
2609 0 : sa->unsupported_cp = 0;
2610 : }
2611 : else
2612 : /* else send empty response */
2613 : {
2614 3 : if (ike_hdr_is_response (ike))
2615 : {
2616 1 : ASSERT (stats != 0);
2617 1 : stats->n_keepalives++;
2618 1 : sa->stats.n_keepalives++;
2619 : }
2620 : }
2621 : }
2622 11 : else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2623 : {
2624 11 : if (sa->is_initiator)
2625 : {
2626 :
2627 3 : ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2628 : ikev2_notify_t notify;
2629 3 : u8 *data = vec_new (u8, 4);
2630 3 : clib_memset (¬ify, 0, sizeof (notify));
2631 3 : notify.protocol_id = IKEV2_PROTOCOL_ESP;
2632 3 : notify.spi = sa->childs[0].i_proposals->spi;
2633 3 : *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2634 :
2635 3 : ikev2_payload_add_sa (chain, proposals);
2636 3 : ikev2_payload_add_nonce (chain, sa->i_nonce);
2637 3 : ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2638 3 : ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2639 3 : ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2640 : ¬ify);
2641 :
2642 3 : vec_free (data);
2643 : }
2644 8 : else if (vec_len (sa->rekey) > 0)
2645 : {
2646 8 : ikev2_add_create_child_resp (sa, &sa->rekey[0], chain);
2647 8 : vec_del1 (sa->rekey, 0);
2648 : }
2649 0 : else if (vec_len (sa->new_child) > 0)
2650 : {
2651 0 : ikev2_add_create_child_resp (sa, &sa->new_child[0], chain);
2652 0 : vec_del1 (sa->new_child, 0);
2653 : }
2654 0 : else if (sa->unsupported_cp)
2655 : {
2656 0 : u8 *data = vec_new (u8, 1);
2657 :
2658 0 : data[0] = sa->unsupported_cp;
2659 0 : ikev2_payload_add_notify (
2660 : chain, IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD, data);
2661 0 : vec_free (data);
2662 0 : sa->unsupported_cp = 0;
2663 : }
2664 : else
2665 : {
2666 0 : ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2667 : 0);
2668 : }
2669 : }
2670 :
2671 : /* IKEv2 header */
2672 64 : ike->version = IKE_VERSION_2;
2673 64 : ike->nextpayload = IKEV2_PAYLOAD_SK;
2674 64 : tlen = sizeof (*ike);
2675 :
2676 64 : if (sa->is_initiator)
2677 16 : ike->flags |= IKEV2_HDR_FLAG_INITIATOR;
2678 :
2679 64 : if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2680 : {
2681 13 : tlen += vec_len (chain->data);
2682 13 : ike->nextpayload = chain->first_payload_type;
2683 13 : ike->length = clib_host_to_net_u32 (tlen);
2684 :
2685 13 : if (tlen + b->current_length + b->current_data > buffer_data_size)
2686 : {
2687 0 : tlen = ~0;
2688 0 : goto done;
2689 : }
2690 :
2691 13 : clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2692 :
2693 : /* store whole IKE payload - needed for PSK auth */
2694 13 : vec_reset_length (sa->last_sa_init_res_packet_data);
2695 13 : vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
2696 : }
2697 : else
2698 : {
2699 51 : ikev2_main_per_thread_data_t *ptd = ikev2_get_per_thread_data ();
2700 51 : ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2701 :
2702 : /* SK payload */
2703 51 : plen = sizeof (*ph);
2704 51 : ph = (ike_payload_header_t *) & ike->payload[0];
2705 51 : ph->nextpayload = chain->first_payload_type;
2706 51 : ph->flags = 0;
2707 51 : int is_aead =
2708 51 : tr_encr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16;
2709 51 : int iv_len = is_aead ? IKEV2_GCM_IV_SIZE : tr_encr->block_size;
2710 51 : plen += vec_len (chain->data) + iv_len;
2711 :
2712 : /* add space for hmac/tag */
2713 51 : if (tr_integ)
2714 33 : plen += tr_integ->key_trunc;
2715 : else
2716 18 : plen += IKEV2_GCM_ICV_SIZE;
2717 51 : tlen += plen;
2718 :
2719 51 : if (tlen + b->current_length + b->current_data > buffer_data_size)
2720 : {
2721 0 : tlen = ~0;
2722 0 : goto done;
2723 : }
2724 :
2725 : /* payload and total length */
2726 51 : ph->length = clib_host_to_net_u16 (plen);
2727 51 : ike->length = clib_host_to_net_u32 (tlen);
2728 :
2729 51 : if (is_aead)
2730 : {
2731 18 : if (!ikev2_encrypt_aead_data (ptd, sa, tr_encr, chain->data,
2732 18 : ph->payload, (u8 *) ike,
2733 : sizeof (*ike) + sizeof (*ph),
2734 18 : ph->payload + plen - sizeof (*ph) -
2735 : IKEV2_GCM_ICV_SIZE))
2736 : {
2737 0 : tlen = ~0;
2738 0 : goto done;
2739 : }
2740 : }
2741 : else
2742 : {
2743 33 : if (!ikev2_encrypt_data
2744 33 : (ptd, sa, tr_encr, chain->data, ph->payload))
2745 : {
2746 0 : tlen = ~0;
2747 0 : goto done;
2748 : }
2749 33 : integ =
2750 33 : ikev2_calc_integr (tr_integ,
2751 33 : sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2752 33 : (u8 *) ike, tlen - tr_integ->key_trunc);
2753 33 : clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2754 33 : sizeof (*ike), integ, tr_integ->key_trunc);
2755 : }
2756 :
2757 : /* store whole IKE payload - needed for retransmit */
2758 51 : vec_reset_length (sa->last_res_packet_data);
2759 51 : vec_add (sa->last_res_packet_data, ike, tlen);
2760 : }
2761 :
2762 64 : done:
2763 64 : ikev2_payload_destroy_chain (chain);
2764 64 : vec_free (integ);
2765 64 : return tlen;
2766 : }
2767 :
2768 : static u32
2769 0 : ikev2_retransmit_sa_init_one (ikev2_sa_t * sa, ike_header_t * ike,
2770 : ip_address_t iaddr, ip_address_t raddr,
2771 : u32 rlen)
2772 : {
2773 0 : int p = 0;
2774 : ike_header_t *tmp;
2775 0 : u8 payload = ike->nextpayload;
2776 :
2777 0 : if (sa->ispi != clib_net_to_host_u64 (ike->ispi) ||
2778 0 : ip_address_cmp (&sa->iaddr, &iaddr) ||
2779 0 : ip_address_cmp (&sa->raddr, &raddr))
2780 : {
2781 0 : return 0;
2782 : }
2783 :
2784 0 : while (p < rlen && payload != IKEV2_PAYLOAD_NONE)
2785 : {
2786 0 : ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
2787 0 : u32 plen = clib_net_to_host_u16 (ikep->length);
2788 :
2789 0 : if (plen < sizeof (ike_payload_header_t))
2790 0 : return ~0;
2791 :
2792 0 : if (payload == IKEV2_PAYLOAD_NONCE &&
2793 0 : !clib_memcmp (sa->i_nonce, ikep->payload, plen - sizeof (*ikep)))
2794 : {
2795 : /* req is retransmit */
2796 0 : if (sa->state == IKEV2_STATE_SA_INIT)
2797 : {
2798 0 : sa->stats.n_init_retransmit++;
2799 0 : tmp = (ike_header_t *) sa->last_sa_init_res_packet_data;
2800 0 : u32 slen = clib_net_to_host_u32 (tmp->length);
2801 0 : ike->ispi = tmp->ispi;
2802 0 : ike->rspi = tmp->rspi;
2803 0 : ike->nextpayload = tmp->nextpayload;
2804 0 : ike->version = tmp->version;
2805 0 : ike->exchange = tmp->exchange;
2806 0 : ike->flags = tmp->flags;
2807 0 : ike->msgid = tmp->msgid;
2808 0 : ike->length = tmp->length;
2809 0 : clib_memcpy_fast (ike->payload, tmp->payload,
2810 : slen - sizeof (*ike));
2811 0 : ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2812 : "ispi %lx IKE_SA_INIT retransmit "
2813 : "from %d.%d.%d.%d to %d.%d.%d.%d",
2814 : ike->ispi,
2815 : ip_addr_v4 (&raddr).as_u32,
2816 : ip_addr_v4 (&iaddr).as_u32);
2817 0 : return slen;
2818 : }
2819 : /* else ignore req */
2820 : else
2821 : {
2822 0 : ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2823 : "ispi %lx IKE_SA_INIT ignore "
2824 : "from %d.%d.%d.%d to %d.%d.%d.%d",
2825 : ike->ispi,
2826 : ip_addr_v4 (&raddr).as_u32,
2827 : ip_addr_v4 (&iaddr).as_u32);
2828 0 : return ~0;
2829 : }
2830 : }
2831 0 : payload = ikep->nextpayload;
2832 0 : p += plen;
2833 : }
2834 :
2835 0 : return 0;
2836 : }
2837 :
2838 : static u32
2839 267 : ikev2_retransmit_sa_init (ike_header_t * ike, ip_address_t iaddr,
2840 : ip_address_t raddr, u32 rlen)
2841 : {
2842 : ikev2_sa_t *sa;
2843 : u32 res;
2844 267 : ikev2_main_per_thread_data_t *ptd = ikev2_get_per_thread_data ();
2845 :
2846 : /* *INDENT-OFF* */
2847 267 : pool_foreach (sa, ptd->sas) {
2848 0 : res = ikev2_retransmit_sa_init_one (sa, ike, iaddr, raddr, rlen);
2849 0 : if (res)
2850 0 : return res;
2851 : }
2852 : /* *INDENT-ON* */
2853 :
2854 : /* req is not retransmit */
2855 267 : return 0;
2856 : }
2857 :
2858 : static u32
2859 43 : ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2860 : {
2861 43 : if (ike_hdr_is_response (ike))
2862 9 : return 0;
2863 :
2864 34 : u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2865 :
2866 : /* new req */
2867 34 : if (msg_id > sa->last_msg_id || sa->last_msg_id == ~0)
2868 : {
2869 34 : sa->last_msg_id = msg_id;
2870 34 : return 0;
2871 : }
2872 :
2873 : /* retransmitted req */
2874 0 : if (msg_id == sa->last_msg_id)
2875 : {
2876 0 : sa->stats.n_retransmit++;
2877 0 : ike_header_t *tmp = (ike_header_t *) sa->last_res_packet_data;
2878 0 : u32 slen = clib_net_to_host_u32 (tmp->length);
2879 0 : ike->ispi = tmp->ispi;
2880 0 : ike->rspi = tmp->rspi;
2881 0 : ike->nextpayload = tmp->nextpayload;
2882 0 : ike->version = tmp->version;
2883 0 : ike->exchange = tmp->exchange;
2884 0 : ike->flags = tmp->flags;
2885 0 : ike->msgid = tmp->msgid;
2886 0 : ike->length = tmp->length;
2887 0 : clib_memcpy_fast (ike->payload, tmp->payload, slen - sizeof (*ike));
2888 0 : ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2889 : msg_id, ip_addr_v4 (&sa->raddr).as_u32,
2890 : ip_addr_v4 (&sa->iaddr).as_u32);
2891 0 : return slen;
2892 : }
2893 :
2894 : /* old req ignore */
2895 0 : ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2896 : msg_id, ip_addr_v4 (&sa->raddr).as_u32,
2897 : ip_addr_v4 (&sa->iaddr).as_u32);
2898 0 : return ~0;
2899 : }
2900 :
2901 : static void
2902 13 : ikev2_init_sa (vlib_main_t * vm, ikev2_sa_t * sa)
2903 : {
2904 13 : ikev2_main_t *km = &ikev2_main;
2905 13 : sa->liveness_period_check = vlib_time_now (vm) + km->liveness_period;
2906 13 : sa->profile_index = ~0;
2907 13 : }
2908 :
2909 : static void
2910 6 : ikev2_del_sa_init_from_main (u64 * ispi)
2911 : {
2912 6 : ikev2_main_t *km = &ikev2_main;
2913 6 : uword *p = hash_get (km->sa_by_ispi, *ispi);
2914 6 : if (p)
2915 : {
2916 6 : ikev2_sa_t *sai = pool_elt_at_index (km->sais, p[0]);
2917 6 : hash_unset (km->sa_by_ispi, sai->ispi);
2918 6 : ikev2_sa_free_all_vec (sai);
2919 6 : pool_put (km->sais, sai);
2920 : }
2921 6 : }
2922 :
2923 : static void
2924 6 : ikev2_del_sa_init (u64 ispi)
2925 : {
2926 6 : vl_api_rpc_call_main_thread (ikev2_del_sa_init_from_main, (u8 *) & ispi,
2927 : sizeof (ispi));
2928 6 : }
2929 :
2930 : static void
2931 2 : ikev2_rewrite_v6_addrs (ikev2_sa_t *sa, ip6_header_t *ih)
2932 : {
2933 2 : if (sa->is_initiator)
2934 : {
2935 0 : ip_address_copy_addr (&ih->dst_address, &sa->raddr);
2936 0 : ip_address_copy_addr (&ih->src_address, &sa->iaddr);
2937 : }
2938 : else
2939 : {
2940 2 : ip_address_copy_addr (&ih->dst_address, &sa->iaddr);
2941 2 : ip_address_copy_addr (&ih->src_address, &sa->raddr);
2942 : }
2943 2 : }
2944 :
2945 : static void
2946 51 : ikev2_rewrite_v4_addrs (ikev2_sa_t *sa, ip4_header_t *ih)
2947 : {
2948 51 : if (sa->is_initiator)
2949 : {
2950 8 : ip_address_copy_addr (&ih->dst_address, &sa->raddr);
2951 8 : ip_address_copy_addr (&ih->src_address, &sa->iaddr);
2952 : }
2953 : else
2954 : {
2955 43 : ip_address_copy_addr (&ih->dst_address, &sa->iaddr);
2956 43 : ip_address_copy_addr (&ih->src_address, &sa->raddr);
2957 : }
2958 51 : }
2959 :
2960 : static void
2961 273 : ikev2_set_ip_address (ikev2_sa_t *sa, const void *iaddr, const void *raddr,
2962 : const ip_address_family_t af)
2963 : {
2964 273 : ip_address_set (&sa->raddr, raddr, af);
2965 273 : ip_address_set (&sa->iaddr, iaddr, af);
2966 273 : }
2967 :
2968 : static void
2969 0 : ikev2_elog_uint_peers_addr (u32 exchange, ip4_header_t * ip4,
2970 : ip6_header_t * ip6, u8 is_ip4)
2971 : {
2972 : u32 src, dst;
2973 0 : if (is_ip4)
2974 : {
2975 0 : src = ip4->src_address.as_u32;
2976 0 : dst = ip4->dst_address.as_u32;
2977 : }
2978 : else
2979 : {
2980 0 : src = ip6->src_address.as_u32[3];
2981 0 : dst = ip6->dst_address.as_u32[3];
2982 : }
2983 0 : ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2984 : "received from %d.%d.%d.%d to %d.%d.%d.%d",
2985 : exchange, src, dst);
2986 0 : }
2987 :
2988 : static void
2989 13 : ikev2_generate_sa_init_data_and_log (ikev2_sa_t * sa)
2990 : {
2991 13 : ikev2_generate_sa_error_t rc = ikev2_generate_sa_init_data (sa);
2992 :
2993 13 : if (PREDICT_TRUE (rc == IKEV2_GENERATE_SA_INIT_OK))
2994 13 : return;
2995 :
2996 0 : if (rc == IKEV2_GENERATE_SA_INIT_ERR_NO_DH)
2997 0 : ikev2_elog_error (IKEV2_GENERATE_SA_INIT_OK_ERR_NO_DH_STR);
2998 0 : else if (rc == IKEV2_GENERATE_SA_INIT_ERR_UNSUPPORTED_DH)
2999 0 : ikev2_elog_error (IKEV2_GENERATE_SA_INIT_OK_ERR_UNSUPP_STR);
3000 : }
3001 :
3002 : static void
3003 64 : ikev2_update_stats (vlib_main_t *vm, u32 node_index, ikev2_stats_t *s)
3004 : {
3005 64 : vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_KEEPALIVE,
3006 64 : s->n_keepalives);
3007 64 : vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_REKEY_REQ,
3008 64 : s->n_rekey_req);
3009 64 : vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_INIT_SA_REQ,
3010 64 : s->n_sa_init_req);
3011 64 : vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_IKE_AUTH_REQ,
3012 64 : s->n_sa_auth_req);
3013 64 : }
3014 :
3015 : static uword
3016 64 : ikev2_node_internal (vlib_main_t *vm, vlib_node_runtime_t *node,
3017 : vlib_frame_t *frame, u8 is_ip4, u8 natt)
3018 : {
3019 64 : u32 n_left = frame->n_vectors, *from;
3020 64 : ikev2_main_t *km = &ikev2_main;
3021 : vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
3022 64 : u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
3023 64 : ikev2_main_per_thread_data_t *ptd = ikev2_get_per_thread_data ();
3024 64 : ikev2_stats_t _stats, *stats = &_stats;
3025 : int res;
3026 :
3027 : /* no NAT traversal for ip6 */
3028 64 : ASSERT (!natt || is_ip4);
3029 :
3030 64 : clib_memset_u16 (stats, 0, sizeof (stats[0]) / sizeof (u16));
3031 64 : from = vlib_frame_vector_args (frame);
3032 64 : vlib_get_buffers (vm, from, bufs, n_left);
3033 64 : b = bufs;
3034 :
3035 634 : while (n_left > 0)
3036 : {
3037 570 : vlib_buffer_t *b0 = b[0];
3038 570 : next[0] = is_ip4 ? IKEV2_NEXT_IP4_ERROR_DROP
3039 : : IKEV2_NEXT_IP6_ERROR_DROP;
3040 570 : ip4_header_t *ip40 = 0;
3041 570 : ip6_header_t *ip60 = 0;
3042 : udp_header_t *udp0;
3043 : ike_header_t *ike0;
3044 570 : ikev2_sa_t *sa0 = 0;
3045 : ikev2_sa_t sa; /* temporary store for SA */
3046 570 : u32 rlen, slen = 0;
3047 570 : int ip_hdr_sz = 0;
3048 570 : int is_req = 0;
3049 :
3050 570 : if (natt)
3051 : {
3052 7 : u8 *ptr = vlib_buffer_get_current (b0);
3053 7 : ip40 = (ip4_header_t *) ptr;
3054 7 : ptr += sizeof (*ip40);
3055 7 : udp0 = (udp_header_t *) ptr;
3056 7 : ptr += sizeof (*udp0);
3057 7 : ike0 = (ike_header_t *) ptr;
3058 7 : ip_hdr_sz = sizeof (*ip40);
3059 : }
3060 : else
3061 : {
3062 563 : u8 *ipx_hdr = b0->data + vnet_buffer (b0)->l3_hdr_offset;
3063 563 : ike0 = vlib_buffer_get_current (b0);
3064 563 : vlib_buffer_advance (b0, -sizeof (*udp0));
3065 563 : udp0 = vlib_buffer_get_current (b0);
3066 :
3067 563 : if (is_ip4)
3068 : {
3069 561 : ip40 = (ip4_header_t *) ipx_hdr;
3070 561 : ip_hdr_sz = sizeof (*ip40);
3071 : }
3072 : else
3073 : {
3074 2 : ip60 = (ip6_header_t *) ipx_hdr;
3075 2 : ip_hdr_sz = sizeof (*ip60);
3076 : }
3077 563 : vlib_buffer_advance (b0, -ip_hdr_sz);
3078 : }
3079 :
3080 570 : rlen = b0->current_length - ip_hdr_sz - sizeof (*udp0);
3081 :
3082 : /* check for non-esp marker */
3083 570 : if (natt)
3084 : {
3085 7 : ASSERT (*((u32 *) ike0) == 0);
3086 7 : ike0 =
3087 : (ike_header_t *) ((u8 *) ike0 + sizeof (ikev2_non_esp_marker));
3088 7 : rlen -= sizeof (ikev2_non_esp_marker);
3089 : }
3090 :
3091 570 : if (clib_net_to_host_u32 (ike0->length) != rlen)
3092 : {
3093 254 : vlib_node_increment_counter (vm, node->node_index,
3094 : IKEV2_ERROR_BAD_LENGTH, 1);
3095 254 : goto dispatch0;
3096 : }
3097 :
3098 316 : if (ike0->version != IKE_VERSION_2)
3099 : {
3100 0 : vlib_node_increment_counter (vm, node->node_index,
3101 : IKEV2_ERROR_NOT_IKEV2, 1);
3102 0 : goto dispatch0;
3103 : }
3104 :
3105 316 : if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
3106 : {
3107 273 : sa0 = &sa;
3108 273 : clib_memset (sa0, 0, sizeof (*sa0));
3109 :
3110 273 : if (ike_hdr_is_initiator (ike0))
3111 : {
3112 267 : sa0->stats.n_sa_init_req++;
3113 267 : stats->n_sa_init_req++;
3114 267 : if (ike0->rspi == 0)
3115 : {
3116 267 : if (is_ip4)
3117 266 : ikev2_set_ip_address (sa0, &ip40->src_address,
3118 266 : &ip40->dst_address, AF_IP4);
3119 : else
3120 1 : ikev2_set_ip_address (sa0, &ip60->src_address,
3121 1 : &ip60->dst_address, AF_IP6);
3122 :
3123 267 : sa0->dst_port = clib_net_to_host_u16 (udp0->src_port);
3124 :
3125 : slen =
3126 267 : ikev2_retransmit_sa_init (ike0, sa0->iaddr,
3127 267 : sa0->raddr, rlen);
3128 267 : if (slen)
3129 : {
3130 0 : vlib_node_increment_counter (vm, node->node_index,
3131 : ~0 ==
3132 : slen ?
3133 : IKEV2_ERROR_IKE_SA_INIT_IGNORE
3134 : :
3135 : IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
3136 : 1);
3137 0 : goto dispatch0;
3138 : }
3139 :
3140 267 : res = ikev2_process_sa_init_req (
3141 : vm, sa0, ike0, udp0, rlen,
3142 267 : vnet_buffer (b0)->sw_if_index[VLIB_RX]);
3143 267 : if (!res)
3144 254 : vlib_node_increment_counter (vm, node->node_index,
3145 : IKEV2_ERROR_MALFORMED_PACKET,
3146 : 1);
3147 :
3148 267 : if (sa0->state == IKEV2_STATE_SA_INIT)
3149 : {
3150 13 : ikev2_sa_free_proposal_vector (&sa0->r_proposals);
3151 26 : sa0->r_proposals =
3152 13 : ikev2_select_proposal (sa0->i_proposals,
3153 : IKEV2_PROTOCOL_IKE);
3154 13 : ikev2_generate_sa_init_data_and_log (sa0);
3155 : }
3156 :
3157 267 : if (sa0->state == IKEV2_STATE_SA_INIT
3158 254 : || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
3159 : {
3160 13 : ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3161 : slen =
3162 13 : ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3163 13 : if (~0 == slen)
3164 0 : vlib_node_increment_counter (vm, node->node_index,
3165 : IKEV2_ERROR_NO_BUFF_SPACE,
3166 : 1);
3167 : }
3168 :
3169 267 : if (sa0->state == IKEV2_STATE_SA_INIT)
3170 : {
3171 : /* add SA to the pool */
3172 13 : pool_get (ptd->sas, sa0);
3173 13 : clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3174 13 : ikev2_init_sa (vm, sa0);
3175 13 : hash_set (ptd->sa_by_rspi, sa0->rspi, sa0 - ptd->sas);
3176 : }
3177 : else
3178 : {
3179 254 : ikev2_sa_free_all_vec (sa0);
3180 : }
3181 : }
3182 : }
3183 : else //received sa_init without initiator flag
3184 : {
3185 6 : if (is_ip4)
3186 6 : ikev2_set_ip_address (sa0, &ip40->dst_address,
3187 6 : &ip40->src_address, AF_IP4);
3188 : else
3189 0 : ikev2_set_ip_address (sa0, &ip60->dst_address,
3190 0 : &ip60->src_address, AF_IP6);
3191 :
3192 6 : ikev2_process_sa_init_resp (vm, sa0, ike0, udp0, rlen);
3193 :
3194 6 : if (sa0->state == IKEV2_STATE_SA_INIT)
3195 : {
3196 6 : is_req = 1;
3197 6 : ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
3198 6 : uword *p = hash_get (km->sa_by_ispi, sa0->ispi);
3199 6 : if (p)
3200 : {
3201 6 : ikev2_sa_t *sai = pool_elt_at_index (km->sais, p[0]);
3202 :
3203 6 : if (clib_atomic_bool_cmp_and_swap
3204 : (&sai->init_response_received, 0, 1))
3205 : {
3206 6 : ikev2_complete_sa_data (sa0, sai);
3207 6 : ikev2_calc_keys (sa0);
3208 6 : ikev2_sa_auth_init (sa0);
3209 6 : ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3210 6 : ike0->msgid =
3211 6 : clib_net_to_host_u32 (sai->last_init_msg_id);
3212 6 : sa0->last_init_msg_id = sai->last_init_msg_id + 1;
3213 6 : slen = ikev2_generate_message (b0, sa0, ike0, 0,
3214 : udp0, stats);
3215 6 : if (~0 == slen)
3216 0 : vlib_node_increment_counter (vm,
3217 : node->node_index,
3218 : IKEV2_ERROR_NO_BUFF_SPACE,
3219 : 1);
3220 : }
3221 : else
3222 : {
3223 : /* we've already processed sa-init response */
3224 0 : sa0->state = IKEV2_STATE_UNKNOWN;
3225 : }
3226 : }
3227 : }
3228 :
3229 6 : if (sa0->state == IKEV2_STATE_SA_INIT)
3230 : {
3231 : /* add SA to the pool */
3232 6 : pool_get (ptd->sas, sa0);
3233 6 : clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3234 6 : hash_set (ptd->sa_by_rspi, sa0->rspi, sa0 - ptd->sas);
3235 : }
3236 : else
3237 : {
3238 0 : ikev2_sa_free_all_vec (sa0);
3239 : }
3240 : }
3241 : }
3242 43 : else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
3243 : {
3244 : uword *p;
3245 19 : p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3246 19 : if (p)
3247 : {
3248 19 : sa0 = pool_elt_at_index (ptd->sas, p[0]);
3249 19 : slen = ikev2_retransmit_resp (sa0, ike0);
3250 19 : if (slen)
3251 : {
3252 0 : vlib_node_increment_counter (vm, node->node_index,
3253 : ~0 ==
3254 : slen ?
3255 : IKEV2_ERROR_IKE_REQ_IGNORE
3256 : :
3257 : IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3258 : 1);
3259 0 : goto dispatch0;
3260 : }
3261 :
3262 19 : sa0->dst_port = clib_net_to_host_u16 (udp0->src_port);
3263 19 : res = ikev2_process_auth_req (vm, sa0, ike0, rlen);
3264 19 : if (res)
3265 19 : ikev2_sa_auth (sa0);
3266 : else
3267 0 : vlib_node_increment_counter (vm, node->node_index,
3268 : IKEV2_ERROR_MALFORMED_PACKET, 1);
3269 19 : if (sa0->state == IKEV2_STATE_AUTHENTICATED)
3270 : {
3271 19 : ikev2_initial_contact_cleanup (ptd, sa0);
3272 19 : p = hash_get (ptd->sa_by_rspi,
3273 : clib_net_to_host_u64 (ike0->rspi));
3274 19 : ikev2_sa_match_ts (sa0);
3275 19 : if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
3276 19 : ikev2_create_tunnel_interface (vm, sa0, &sa0->childs[0],
3277 19 : p[0], 0, 0, 0);
3278 : }
3279 :
3280 19 : if (sa0->is_initiator)
3281 : {
3282 6 : sa0->last_msg_id = ~0;
3283 6 : ikev2_del_sa_init (sa0->ispi);
3284 : }
3285 : else
3286 : {
3287 13 : sa0->stats.n_sa_auth_req++;
3288 13 : stats->n_sa_auth_req++;
3289 13 : ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3290 : slen =
3291 13 : ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3292 13 : if (~0 == slen)
3293 0 : vlib_node_increment_counter (vm, node->node_index,
3294 : IKEV2_ERROR_NO_BUFF_SPACE,
3295 : 1);
3296 : }
3297 : }
3298 : }
3299 24 : else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
3300 : {
3301 : uword *p;
3302 13 : p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3303 13 : if (p)
3304 : {
3305 13 : sa0 = pool_elt_at_index (ptd->sas, p[0]);
3306 13 : slen = ikev2_retransmit_resp (sa0, ike0);
3307 13 : if (slen)
3308 : {
3309 0 : vlib_node_increment_counter (vm, node->node_index,
3310 : ~0 ==
3311 : slen ?
3312 : IKEV2_ERROR_IKE_REQ_IGNORE
3313 : :
3314 : IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3315 : 1);
3316 0 : goto dispatch0;
3317 : }
3318 :
3319 13 : res = ikev2_process_informational_req (vm, sa0, ike0, rlen);
3320 13 : if (!res)
3321 : {
3322 0 : vlib_node_increment_counter (vm, node->node_index,
3323 : IKEV2_ERROR_MALFORMED_PACKET,
3324 : 1);
3325 0 : slen = ~0;
3326 0 : goto dispatch0;
3327 : }
3328 :
3329 13 : if (sa0->del)
3330 : {
3331 12 : if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
3332 : {
3333 0 : ikev2_delete_t *d, *tmp, *resp = 0;
3334 0 : vec_foreach (d, sa0->del)
3335 : {
3336 : ikev2_child_sa_t *ch_sa;
3337 0 : ch_sa = ikev2_sa_get_child (sa0, d->spi,
3338 0 : d->protocol_id,
3339 0 : !sa0->is_initiator);
3340 0 : if (ch_sa)
3341 : {
3342 0 : ikev2_delete_tunnel_interface (km->vnet_main,
3343 : sa0, ch_sa);
3344 0 : if (!sa0->is_initiator)
3345 : {
3346 0 : vec_add2 (resp, tmp, 1);
3347 0 : tmp->protocol_id = d->protocol_id;
3348 0 : tmp->spi = ch_sa->r_proposals[0].spi;
3349 : }
3350 0 : ikev2_sa_del_child_sa (sa0, ch_sa);
3351 : }
3352 : }
3353 0 : if (!sa0->is_initiator)
3354 : {
3355 0 : vec_free (sa0->del);
3356 0 : sa0->del = resp;
3357 : }
3358 : }
3359 : }
3360 13 : if (ike_hdr_is_request (ike0))
3361 : {
3362 13 : ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3363 : slen =
3364 13 : ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3365 13 : if (~0 == slen)
3366 0 : vlib_node_increment_counter (vm, node->node_index,
3367 : IKEV2_ERROR_NO_BUFF_SPACE,
3368 : 1);
3369 : }
3370 : }
3371 : }
3372 11 : else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
3373 : {
3374 : uword *p;
3375 11 : p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3376 11 : if (p)
3377 : {
3378 11 : sa0 = pool_elt_at_index (ptd->sas, p[0]);
3379 11 : slen = ikev2_retransmit_resp (sa0, ike0);
3380 11 : if (slen)
3381 : {
3382 0 : vlib_node_increment_counter (vm, node->node_index,
3383 : ~0 ==
3384 : slen ?
3385 : IKEV2_ERROR_IKE_REQ_IGNORE
3386 : :
3387 : IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3388 : 1);
3389 0 : goto dispatch0;
3390 : }
3391 :
3392 11 : res = ikev2_process_create_child_sa_req (vm, sa0, ike0, rlen);
3393 11 : if (!res)
3394 : {
3395 1 : vlib_node_increment_counter (vm, node->node_index,
3396 : IKEV2_ERROR_MALFORMED_PACKET,
3397 : 1);
3398 1 : slen = ~0;
3399 1 : goto dispatch0;
3400 : }
3401 :
3402 10 : if (vec_len (sa0->rekey) > 0)
3403 : {
3404 10 : if (!sa0->rekey[0].notify_type &&
3405 8 : sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
3406 : {
3407 8 : if (vec_len (sa0->childs) > 0)
3408 8 : ikev2_sa_free_all_child_sa (&sa0->childs);
3409 : ikev2_child_sa_t *child;
3410 8 : vec_add2 (sa0->childs, child, 1);
3411 8 : clib_memset (child, 0, sizeof (*child));
3412 8 : child->r_proposals = sa0->rekey[0].r_proposal;
3413 8 : child->i_proposals = sa0->rekey[0].i_proposal;
3414 8 : child->tsi = sa0->rekey[0].tsi;
3415 8 : child->tsr = sa0->rekey[0].tsr;
3416 8 : ikev2_create_tunnel_interface (vm, sa0, child, p[0],
3417 8 : child - sa0->childs, 1,
3418 8 : sa0->rekey[0].kex);
3419 : }
3420 10 : if (ike_hdr_is_response (ike0))
3421 : {
3422 2 : vec_free (sa0->rekey);
3423 : }
3424 : else
3425 : {
3426 8 : stats->n_rekey_req++;
3427 8 : sa0->stats.n_rekey_req++;
3428 8 : ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3429 : slen =
3430 8 : ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3431 8 : if (~0 == slen)
3432 0 : vlib_node_increment_counter (vm, node->node_index,
3433 : IKEV2_ERROR_NO_BUFF_SPACE,
3434 : 1);
3435 : }
3436 : }
3437 0 : else if (vec_len (sa0->new_child) > 0)
3438 : {
3439 : ikev2_child_sa_t *c;
3440 0 : vec_add2 (sa0->childs, c, 1);
3441 0 : memset (c, 0, sizeof (*c));
3442 0 : c->r_proposals = sa0->new_child[0].r_proposal;
3443 0 : c->i_proposals = sa0->new_child[0].i_proposal;
3444 0 : c->tsi = sa0->new_child[0].tsi;
3445 0 : c->tsr = sa0->new_child[0].tsr;
3446 0 : ikev2_create_tunnel_interface (vm, sa0, c, p[0],
3447 0 : c - sa0->childs, 0,
3448 0 : sa0->new_child[0].kex);
3449 0 : if (ike_hdr_is_request (ike0))
3450 : {
3451 0 : ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3452 : slen =
3453 0 : ikev2_generate_message (b0, sa0, ike0, 0, udp0, 0);
3454 0 : if (~0 == slen)
3455 0 : vlib_node_increment_counter (
3456 : vm, node->node_index, IKEV2_ERROR_NO_BUFF_SPACE, 1);
3457 : }
3458 : }
3459 : }
3460 : }
3461 : else
3462 : {
3463 0 : ikev2_elog_uint_peers_addr (ike0->exchange, ip40, ip60, is_ip4);
3464 : }
3465 :
3466 570 : dispatch0:
3467 : /* if we are sending packet back, rewrite headers */
3468 570 : if (slen && ~0 != slen)
3469 : {
3470 53 : if (is_ip4)
3471 : {
3472 51 : next[0] = IKEV2_NEXT_IP4_LOOKUP;
3473 51 : ikev2_rewrite_v4_addrs (sa0, ip40);
3474 : }
3475 : else
3476 : {
3477 2 : next[0] = IKEV2_NEXT_IP6_LOOKUP;
3478 2 : ikev2_rewrite_v6_addrs (sa0, ip60);
3479 : }
3480 :
3481 53 : if (is_req)
3482 : {
3483 6 : udp0->dst_port = udp0->src_port =
3484 6 : clib_net_to_host_u16 (ikev2_get_port (sa0));
3485 :
3486 6 : if (udp0->dst_port == clib_net_to_host_u16 (IKEV2_PORT_NATT)
3487 1 : && ikev2_natt_active (sa0))
3488 : {
3489 1 : if (!natt)
3490 1 : slen = ikev2_insert_non_esp_marker (ike0, slen);
3491 : }
3492 : }
3493 : else
3494 : {
3495 47 : if (natt)
3496 6 : slen += sizeof (ikev2_non_esp_marker);
3497 :
3498 47 : u16 tp = udp0->dst_port;
3499 47 : udp0->dst_port = udp0->src_port;
3500 47 : udp0->src_port = tp;
3501 : }
3502 :
3503 53 : udp0->length = clib_host_to_net_u16 (slen + sizeof (udp_header_t));
3504 53 : udp0->checksum = 0;
3505 53 : b0->current_length = slen + ip_hdr_sz + sizeof (udp_header_t);
3506 53 : if (is_ip4)
3507 : {
3508 51 : ip40->length = clib_host_to_net_u16 (b0->current_length);
3509 51 : ip40->checksum = ip4_header_checksum (ip40);
3510 : }
3511 : else
3512 : {
3513 2 : ip60->payload_length =
3514 2 : clib_host_to_net_u16 (b0->current_length - sizeof (*ip60));
3515 : }
3516 : }
3517 : /* delete sa */
3518 570 : if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
3519 316 : sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
3520 : {
3521 : ikev2_child_sa_t *c;
3522 :
3523 24 : vec_foreach (c, sa0->childs)
3524 12 : ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
3525 :
3526 12 : ikev2_delete_sa (ptd, sa0);
3527 : }
3528 570 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
3529 : && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3530 : {
3531 :
3532 570 : ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
3533 570 : t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3534 570 : t->next_index = next[0];
3535 : }
3536 570 : n_left -= 1;
3537 570 : next += 1;
3538 570 : b += 1;
3539 : }
3540 :
3541 64 : ikev2_update_stats (vm, node->node_index, stats);
3542 64 : vlib_node_increment_counter (vm, node->node_index,
3543 64 : IKEV2_ERROR_PROCESSED, frame->n_vectors);
3544 64 : vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
3545 64 : return frame->n_vectors;
3546 : }
3547 :
3548 : static uword
3549 55 : ikev2_ip4 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3550 : {
3551 55 : return ikev2_node_internal (vm, node, frame, 1 /* is_ip4 */, 0);
3552 : }
3553 :
3554 : static uword
3555 7 : ikev2_ip4_natt (vlib_main_t *vm, vlib_node_runtime_t *node,
3556 : vlib_frame_t *frame)
3557 : {
3558 7 : return ikev2_node_internal (vm, node, frame, 1 /* is_ip4 */, 1 /* natt */);
3559 : }
3560 :
3561 : static uword
3562 2 : ikev2_ip6 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3563 : {
3564 2 : return ikev2_node_internal (vm, node, frame, 0 /* is_ip4 */, 0);
3565 : }
3566 :
3567 : /* *INDENT-OFF* */
3568 126188 : VLIB_REGISTER_NODE (ikev2_node_ip4,static) = {
3569 : .function = ikev2_ip4,
3570 : .name = "ikev2-ip4",
3571 : .vector_size = sizeof (u32),
3572 : .format_trace = format_ikev2_trace,
3573 : .type = VLIB_NODE_TYPE_INTERNAL,
3574 :
3575 : .n_errors = IKEV2_N_ERROR,
3576 : .error_counters = ikev2_error_counters,
3577 :
3578 : .n_next_nodes = IKEV2_IP4_N_NEXT,
3579 : .next_nodes = {
3580 : [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
3581 : [IKEV2_NEXT_IP4_ERROR_DROP] = "error-drop",
3582 : },
3583 : };
3584 :
3585 126188 : VLIB_REGISTER_NODE (ikev2_node_ip4_natt,static) = {
3586 : .function = ikev2_ip4_natt,
3587 : .name = "ikev2-ip4-natt",
3588 : .vector_size = sizeof (u32),
3589 : .format_trace = format_ikev2_trace,
3590 : .type = VLIB_NODE_TYPE_INTERNAL,
3591 :
3592 : .n_errors = IKEV2_N_ERROR,
3593 : .error_counters = ikev2_error_counters,
3594 :
3595 : .n_next_nodes = IKEV2_IP4_N_NEXT,
3596 : .next_nodes = {
3597 : [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
3598 : [IKEV2_NEXT_IP4_ERROR_DROP] = "error-drop",
3599 : },
3600 : };
3601 :
3602 126188 : VLIB_REGISTER_NODE (ikev2_node_ip6,static) = {
3603 : .function = ikev2_ip6,
3604 : .name = "ikev2-ip6",
3605 : .vector_size = sizeof (u32),
3606 : .format_trace = format_ikev2_trace,
3607 : .type = VLIB_NODE_TYPE_INTERNAL,
3608 :
3609 : .n_errors = IKEV2_N_ERROR,
3610 : .error_counters = ikev2_error_counters,
3611 :
3612 : .n_next_nodes = IKEV2_IP6_N_NEXT,
3613 : .next_nodes = {
3614 : [IKEV2_NEXT_IP6_LOOKUP] = "ip6-lookup",
3615 : [IKEV2_NEXT_IP6_ERROR_DROP] = "error-drop",
3616 : },
3617 : };
3618 : /* *INDENT-ON* */
3619 :
3620 : // set ikev2 proposals when vpp is used as initiator
3621 : static clib_error_t *
3622 12 : ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
3623 : ikev2_transforms_set * ts,
3624 : ikev2_sa_proposal_t ** proposals, int is_ike)
3625 : {
3626 : clib_error_t *r;
3627 12 : ikev2_main_t *km = &ikev2_main;
3628 : ikev2_sa_proposal_t *proposal;
3629 12 : vec_add2 (*proposals, proposal, 1);
3630 : ikev2_sa_transform_t *td;
3631 : int error;
3632 :
3633 : /* Encryption */
3634 12 : error = 1;
3635 30 : vec_foreach (td, km->supported_transforms)
3636 : {
3637 30 : if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
3638 30 : && td->encr_type == ts->crypto_alg
3639 12 : && td->key_len == ts->crypto_key_size / 8)
3640 : {
3641 : u16 attr[2];
3642 12 : attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
3643 12 : attr[1] = clib_host_to_net_u16 (td->key_len << 3);
3644 12 : vec_add (td->attrs, (u8 *) attr, 4);
3645 12 : vec_add1 (proposal->transforms, *td);
3646 12 : td->attrs = 0;
3647 :
3648 12 : error = 0;
3649 12 : break;
3650 : }
3651 : }
3652 12 : if (error)
3653 : {
3654 0 : r = clib_error_return (0, "Unsupported algorithm");
3655 0 : return r;
3656 : }
3657 :
3658 12 : if (IKEV2_TRANSFORM_INTEG_TYPE_NONE != ts->integ_alg)
3659 : {
3660 : /* Integrity */
3661 6 : error = 1;
3662 66 : vec_foreach (td, km->supported_transforms)
3663 : {
3664 66 : if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
3665 6 : && td->integ_type == ts->integ_alg)
3666 : {
3667 6 : vec_add1 (proposal->transforms, *td);
3668 6 : error = 0;
3669 6 : break;
3670 : }
3671 : }
3672 6 : if (error)
3673 : {
3674 0 : ikev2_elog_error
3675 : ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
3676 0 : r = clib_error_return (0, "Unsupported algorithm");
3677 0 : return r;
3678 : }
3679 : }
3680 :
3681 : /* PRF */
3682 12 : if (is_ike)
3683 : {
3684 6 : error = 1;
3685 42 : vec_foreach (td, km->supported_transforms)
3686 : {
3687 42 : if (td->type == IKEV2_TRANSFORM_TYPE_PRF
3688 6 : && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
3689 : {
3690 6 : vec_add1 (proposal->transforms, *td);
3691 6 : error = 0;
3692 6 : break;
3693 : }
3694 : }
3695 6 : if (error)
3696 : {
3697 0 : r = clib_error_return (0, "Unsupported algorithm");
3698 0 : return r;
3699 : }
3700 : }
3701 :
3702 : /* DH */
3703 12 : if (is_ike)
3704 : {
3705 6 : error = 1;
3706 156 : vec_foreach (td, km->supported_transforms)
3707 : {
3708 156 : if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
3709 : {
3710 6 : vec_add1 (proposal->transforms, *td);
3711 6 : if (is_ike)
3712 : {
3713 6 : sa->dh_group = td->dh_type;
3714 : }
3715 6 : error = 0;
3716 6 : break;
3717 : }
3718 : }
3719 6 : if (error)
3720 : {
3721 0 : r = clib_error_return (0, "Unsupported algorithm");
3722 0 : return r;
3723 : }
3724 : }
3725 :
3726 12 : if (!is_ike)
3727 : {
3728 6 : error = 1;
3729 198 : vec_foreach (td, km->supported_transforms)
3730 : {
3731 192 : if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
3732 : {
3733 12 : vec_add1 (proposal->transforms, *td);
3734 12 : error = 0;
3735 : }
3736 : }
3737 6 : if (error)
3738 : {
3739 0 : r = clib_error_return (0, "Unsupported algorithm");
3740 0 : return r;
3741 : }
3742 : }
3743 :
3744 :
3745 12 : return 0;
3746 : }
3747 :
3748 : static ikev2_profile_t *
3749 188 : ikev2_profile_index_by_name (u8 * name)
3750 : {
3751 188 : ikev2_main_t *km = &ikev2_main;
3752 : uword *p;
3753 :
3754 188 : p = mhash_get (&km->profile_index_by_name, name);
3755 188 : if (!p)
3756 22 : return 0;
3757 :
3758 166 : return pool_elt_at_index (km->profiles, p[0]);
3759 : }
3760 :
3761 :
3762 : static void
3763 17 : ikev2_send_ike (vlib_main_t * vm, ip_address_t * src, ip_address_t * dst,
3764 : u32 bi0, u32 len, u16 src_port, u16 dst_port, u32 sw_if_index)
3765 : {
3766 : ip4_header_t *ip40;
3767 : ip6_header_t *ip60;
3768 : udp_header_t *udp0;
3769 : vlib_buffer_t *b0;
3770 : vlib_frame_t *f;
3771 : u32 *to_next;
3772 :
3773 17 : b0 = vlib_get_buffer (vm, bi0);
3774 17 : vlib_buffer_advance (b0, -sizeof (udp_header_t));
3775 17 : udp0 = vlib_buffer_get_current (b0);
3776 17 : udp0->dst_port = clib_host_to_net_u16 (dst_port);
3777 17 : udp0->src_port = clib_host_to_net_u16 (src_port);
3778 17 : udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
3779 17 : udp0->checksum = 0;
3780 :
3781 17 : if (ip_addr_version (dst) == AF_IP4)
3782 : {
3783 16 : vlib_buffer_advance (b0, -sizeof (ip4_header_t));
3784 16 : ip40 = vlib_buffer_get_current (b0);
3785 16 : ip40->ip_version_and_header_length = 0x45;
3786 16 : ip40->tos = 0;
3787 16 : ip40->fragment_id = 0;
3788 16 : ip40->flags_and_fragment_offset = 0;
3789 16 : ip40->ttl = 0xff;
3790 16 : ip40->protocol = IP_PROTOCOL_UDP;
3791 16 : ip40->dst_address.as_u32 = ip_addr_v4 (dst).as_u32;
3792 16 : ip40->src_address.as_u32 = ip_addr_v4 (src).as_u32;
3793 16 : b0->current_length =
3794 16 : len + sizeof (ip4_header_t) + sizeof (udp_header_t);
3795 16 : ip40->length = clib_host_to_net_u16 (b0->current_length);
3796 16 : ip40->checksum = ip4_header_checksum (ip40);
3797 : }
3798 : else
3799 : {
3800 1 : vlib_buffer_advance (b0, -sizeof (ip6_header_t));
3801 1 : ip60 = vlib_buffer_get_current (b0);
3802 :
3803 1 : b0->current_length = len + sizeof (*ip60) + sizeof (udp_header_t);
3804 1 : ip60->ip_version_traffic_class_and_flow_label =
3805 1 : clib_host_to_net_u32 (0x6 << 28);
3806 1 : ip60->payload_length =
3807 1 : clib_host_to_net_u16 (b0->current_length - sizeof (*ip60));
3808 1 : ip60->protocol = IP_PROTOCOL_UDP;
3809 1 : ip60->hop_limit = 0xff;
3810 1 : clib_memcpy_fast (ip60->src_address.as_u8, ip_addr_v6 (src).as_u8,
3811 : sizeof (ip60->src_address));
3812 1 : clib_memcpy_fast (ip60->dst_address.as_u8, ip_addr_v6 (dst).as_u8,
3813 : sizeof (ip60->src_address));
3814 : }
3815 :
3816 17 : b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3817 17 : vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
3818 17 : vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
3819 :
3820 34 : u32 next_index = (ip_addr_version (dst) == AF_IP4) ?
3821 17 : ip4_lookup_node.index : ip6_lookup_node.index;
3822 :
3823 : /* send the request */
3824 17 : f = vlib_get_frame_to_node (vm, next_index);
3825 17 : to_next = vlib_frame_vector_args (f);
3826 17 : to_next[0] = bi0;
3827 17 : f->n_vectors = 1;
3828 17 : vlib_put_frame_to_node (vm, next_index, f);
3829 :
3830 17 : }
3831 :
3832 : static u32
3833 17 : ikev2_get_new_ike_header_buff (vlib_main_t * vm, vlib_buffer_t ** b)
3834 : {
3835 : u32 bi0;
3836 17 : if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
3837 : {
3838 0 : *b = 0;
3839 0 : return 0;
3840 : }
3841 17 : *b = vlib_get_buffer (vm, bi0);
3842 17 : return bi0;
3843 : }
3844 :
3845 : clib_error_t *
3846 1 : ikev2_set_local_key (vlib_main_t * vm, u8 * file)
3847 : {
3848 1 : ikev2_main_t *km = &ikev2_main;
3849 :
3850 1 : if (km->pkey)
3851 0 : EVP_PKEY_free (km->pkey);
3852 1 : km->pkey = ikev2_load_key_file (file);
3853 1 : if (km->pkey == NULL)
3854 0 : return clib_error_return (0, "load key '%s' failed", file);
3855 :
3856 1 : return 0;
3857 : }
3858 :
3859 : static vnet_api_error_t
3860 2 : ikev2_register_udp_port (ikev2_profile_t *p, u16 port)
3861 : {
3862 2 : ipsec_register_udp_port (port, 0 /* is_ip4 */);
3863 2 : ipsec_register_udp_port (port, 1 /* is_ip4 */);
3864 2 : p->ipsec_over_udp_port = port;
3865 2 : return 0;
3866 : }
3867 :
3868 : static void
3869 20 : ikev2_unregister_udp_port (ikev2_profile_t *p)
3870 : {
3871 20 : if (p->ipsec_over_udp_port == IPSEC_UDP_PORT_NONE)
3872 18 : return;
3873 :
3874 2 : ipsec_unregister_udp_port (p->ipsec_over_udp_port, 0 /* is_ip4 */);
3875 2 : ipsec_unregister_udp_port (p->ipsec_over_udp_port, 1 /* is_ip4 */);
3876 2 : p->ipsec_over_udp_port = IPSEC_UDP_PORT_NONE;
3877 : }
3878 :
3879 : static void
3880 6 : ikev2_initiate_delete_ike_sa_internal (vlib_main_t * vm,
3881 : ikev2_main_per_thread_data_t * tkm,
3882 : ikev2_sa_t * sa, u8 send_notification)
3883 : {
3884 6 : ikev2_main_t *km = &ikev2_main;
3885 : ip_address_t *src, *dst;
3886 : vlib_buffer_t *b0;
3887 : ikev2_child_sa_t *c;
3888 :
3889 : /* Create the Initiator notification for IKE SA removal */
3890 : ike_header_t *ike0;
3891 6 : u32 bi0 = 0;
3892 : int len;
3893 :
3894 6 : vec_resize (sa->del, 1);
3895 6 : sa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3896 6 : sa->del->spi = sa->ispi;
3897 :
3898 6 : if (send_notification)
3899 : {
3900 6 : bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
3901 6 : if (!bi0)
3902 : {
3903 0 : ikev2_log_error ("buffer alloc failure");
3904 0 : goto delete_sa;
3905 : }
3906 :
3907 6 : ike0 = vlib_buffer_get_current (b0);
3908 6 : ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3909 6 : ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3910 6 : ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3911 6 : ike0->flags = 0;
3912 6 : ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
3913 6 : sa->last_init_msg_id += 1;
3914 6 : len = ikev2_generate_message (b0, sa, ike0, 0, 0, 0);
3915 6 : if (~0 == len)
3916 0 : return;
3917 :
3918 6 : if (ikev2_natt_active (sa))
3919 1 : len = ikev2_insert_non_esp_marker (ike0, len);
3920 :
3921 6 : if (sa->is_initiator)
3922 : {
3923 5 : src = &sa->iaddr;
3924 5 : dst = &sa->raddr;
3925 : }
3926 : else
3927 : {
3928 1 : dst = &sa->iaddr;
3929 1 : src = &sa->raddr;
3930 : }
3931 :
3932 6 : ikev2_send_ike (vm, src, dst, bi0, len,
3933 6 : ikev2_get_port (sa), sa->dst_port, 0);
3934 : }
3935 :
3936 0 : delete_sa:
3937 : /* delete local SA */
3938 12 : vec_foreach (c, sa->childs)
3939 6 : ikev2_delete_tunnel_interface (km->vnet_main, sa, c);
3940 :
3941 6 : u64 rspi = sa->rspi;
3942 6 : ikev2_sa_free_all_vec (sa);
3943 6 : uword *p = hash_get (tkm->sa_by_rspi, rspi);
3944 6 : if (p)
3945 : {
3946 6 : hash_unset (tkm->sa_by_rspi, rspi);
3947 6 : pool_put (tkm->sas, sa);
3948 : }
3949 : }
3950 :
3951 : static void
3952 20 : ikev2_cleanup_profile_sessions (ikev2_main_t * km, ikev2_profile_t * p)
3953 : {
3954 : ikev2_main_per_thread_data_t *tkm;
3955 : ikev2_sa_t *sa;
3956 20 : u32 pi = p - km->profiles;
3957 : u32 *sai;
3958 20 : u32 *del_sai = 0;
3959 :
3960 : /* *INDENT-OFF* */
3961 20 : pool_foreach (sa, km->sais) {
3962 0 : if (pi == sa->profile_index)
3963 0 : vec_add1 (del_sai, sa - km->sais);
3964 : }
3965 : /* *INDENT-ON* */
3966 :
3967 20 : vec_foreach (sai, del_sai)
3968 : {
3969 0 : sa = pool_elt_at_index (km->sais, sai[0]);
3970 0 : ikev2_sa_free_all_vec (sa);
3971 0 : hash_unset (km->sa_by_ispi, sa->ispi);
3972 0 : pool_put (km->sais, sa);
3973 : }
3974 20 : vec_reset_length (del_sai);
3975 :
3976 40 : vec_foreach (tkm, km->per_thread_data)
3977 : {
3978 : /* *INDENT-OFF* */
3979 20 : pool_foreach (sa, tkm->sas) {
3980 0 : if (sa->profile_index != ~0 && pi == sa->profile_index)
3981 0 : vec_add1 (del_sai, sa - tkm->sas);
3982 : }
3983 : /* *INDENT-ON* */
3984 :
3985 20 : vec_foreach (sai, del_sai)
3986 : {
3987 0 : sa = pool_elt_at_index (tkm->sas, sai[0]);
3988 0 : ikev2_initiate_delete_ike_sa_internal (km->vlib_main, tkm, sa, 1);
3989 : }
3990 :
3991 20 : vec_reset_length (del_sai);
3992 : }
3993 :
3994 20 : vec_free (del_sai);
3995 20 : }
3996 :
3997 : static void
3998 20 : ikev2_profile_responder_free (ikev2_responder_t *r)
3999 : {
4000 20 : vec_free (r->hostname);
4001 20 : }
4002 :
4003 : static void
4004 20 : ikev2_profile_free (ikev2_profile_t * p)
4005 : {
4006 20 : vec_free (p->name);
4007 :
4008 20 : vec_free (p->auth.data);
4009 20 : if (p->auth.key)
4010 1 : EVP_PKEY_free (p->auth.key);
4011 :
4012 20 : ikev2_profile_responder_free (&p->responder);
4013 :
4014 20 : vec_free (p->loc_id.data);
4015 20 : vec_free (p->rem_id.data);
4016 20 : }
4017 :
4018 : static void
4019 22 : ikev2_bind (vlib_main_t *vm, ikev2_main_t *km)
4020 : {
4021 22 : if (0 == km->bind_refcount)
4022 : {
4023 21 : udp_register_dst_port (vm, IKEV2_PORT, ikev2_node_ip4.index, 1);
4024 21 : udp_register_dst_port (vm, IKEV2_PORT, ikev2_node_ip6.index, 0);
4025 21 : udp_register_dst_port (vm, IKEV2_PORT_NATT, ikev2_node_ip4.index, 1);
4026 21 : udp_register_dst_port (vm, IKEV2_PORT_NATT, ikev2_node_ip6.index, 0);
4027 :
4028 21 : vlib_punt_register (km->punt_hdl,
4029 : ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0],
4030 : "ikev2-ip4-natt");
4031 : }
4032 :
4033 22 : km->bind_refcount++;
4034 22 : }
4035 :
4036 : static void
4037 20 : ikev2_unbind (vlib_main_t *vm, ikev2_main_t *km)
4038 : {
4039 20 : km->bind_refcount--;
4040 20 : if (0 == km->bind_refcount)
4041 : {
4042 19 : vlib_punt_unregister (km->punt_hdl,
4043 : ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0],
4044 : "ikev2-ip4-natt");
4045 :
4046 19 : udp_unregister_dst_port (vm, IKEV2_PORT_NATT, 0);
4047 19 : udp_unregister_dst_port (vm, IKEV2_PORT_NATT, 1);
4048 19 : udp_unregister_dst_port (vm, IKEV2_PORT, 0);
4049 19 : udp_unregister_dst_port (vm, IKEV2_PORT, 1);
4050 : }
4051 20 : }
4052 :
4053 : static void ikev2_lazy_init (ikev2_main_t *km);
4054 :
4055 : clib_error_t *
4056 42 : ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
4057 : {
4058 42 : ikev2_main_t *km = &ikev2_main;
4059 : ikev2_profile_t *p;
4060 :
4061 42 : ikev2_lazy_init (km);
4062 :
4063 42 : if (is_add)
4064 : {
4065 22 : if (ikev2_profile_index_by_name (name))
4066 0 : return clib_error_return (0, "policy %v already exists", name);
4067 :
4068 22 : pool_get (km->profiles, p);
4069 22 : clib_memset (p, 0, sizeof (*p));
4070 22 : p->name = vec_dup (name);
4071 22 : p->ipsec_over_udp_port = IPSEC_UDP_PORT_NONE;
4072 22 : p->responder.sw_if_index = ~0;
4073 22 : p->tun_itf = ~0;
4074 22 : uword index = p - km->profiles;
4075 22 : mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
4076 :
4077 22 : ikev2_bind (vm, km);
4078 : }
4079 : else
4080 : {
4081 20 : p = ikev2_profile_index_by_name (name);
4082 20 : if (!p)
4083 0 : return clib_error_return (0, "policy %v does not exists", name);
4084 :
4085 20 : ikev2_unbind (vm, km);
4086 :
4087 20 : ikev2_unregister_udp_port (p);
4088 20 : ikev2_cleanup_profile_sessions (km, p);
4089 :
4090 20 : ikev2_profile_free (p);
4091 20 : pool_put (km->profiles, p);
4092 20 : mhash_unset (&km->profile_index_by_name, name, 0);
4093 : }
4094 42 : return 0;
4095 : }
4096 :
4097 : clib_error_t *
4098 22 : ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
4099 : u8 * auth_data, u8 data_hex_format)
4100 : {
4101 : ikev2_profile_t *p;
4102 : clib_error_t *r;
4103 :
4104 22 : p = ikev2_profile_index_by_name (name);
4105 :
4106 22 : if (!p)
4107 : {
4108 0 : r = clib_error_return (0, "unknown profile %v", name);
4109 0 : return r;
4110 : }
4111 :
4112 22 : if (p->auth.key)
4113 0 : EVP_PKEY_free (p->auth.key);
4114 22 : vec_free (p->auth.data);
4115 :
4116 22 : p->auth.method = auth_method;
4117 22 : p->auth.data = vec_dup (auth_data);
4118 22 : p->auth.hex = data_hex_format;
4119 :
4120 22 : if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
4121 : {
4122 1 : vec_add1 (p->auth.data, 0);
4123 1 : p->auth.key = ikev2_load_cert_file (p->auth.data);
4124 1 : if (p->auth.key == NULL)
4125 0 : return clib_error_return (0, "load cert '%s' failed", p->auth.data);
4126 : }
4127 :
4128 22 : return 0;
4129 : }
4130 :
4131 : static int
4132 43 : ikev2_is_id_supported (u8 id_type)
4133 : {
4134 42 : return (id_type == IKEV2_ID_TYPE_ID_IPV4_ADDR ||
4135 41 : id_type == IKEV2_ID_TYPE_ID_IPV6_ADDR ||
4136 85 : id_type == IKEV2_ID_TYPE_ID_RFC822_ADDR ||
4137 : id_type == IKEV2_ID_TYPE_ID_FQDN);
4138 : }
4139 :
4140 : clib_error_t *
4141 43 : ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
4142 : int is_local)
4143 : {
4144 : ikev2_profile_t *p;
4145 : clib_error_t *r;
4146 :
4147 43 : if (!ikev2_is_id_supported (id_type))
4148 : {
4149 0 : r = clib_error_return (0, "unsupported identity type %U",
4150 : format_ikev2_id_type, id_type);
4151 0 : return r;
4152 : }
4153 :
4154 43 : p = ikev2_profile_index_by_name (name);
4155 :
4156 43 : if (!p)
4157 : {
4158 0 : r = clib_error_return (0, "unknown profile %v", name);
4159 0 : return r;
4160 : }
4161 :
4162 43 : if (is_local)
4163 : {
4164 22 : vec_free (p->loc_id.data);
4165 22 : p->loc_id.type = id_type;
4166 22 : p->loc_id.data = vec_dup (data);
4167 : }
4168 : else
4169 : {
4170 21 : vec_free (p->rem_id.data);
4171 21 : p->rem_id.type = id_type;
4172 21 : p->rem_id.data = vec_dup (data);
4173 : }
4174 :
4175 43 : return 0;
4176 : }
4177 :
4178 : static void
4179 44 : ikev2_set_ts_type (ikev2_ts_t *ts, const ip_address_t *addr)
4180 : {
4181 44 : if (ip_addr_version (addr) == AF_IP4)
4182 40 : ts->ts_type = TS_IPV4_ADDR_RANGE;
4183 : else
4184 4 : ts->ts_type = TS_IPV6_ADDR_RANGE;
4185 44 : }
4186 :
4187 : static void
4188 44 : ikev2_set_ts_addrs (ikev2_ts_t *ts, const ip_address_t *start,
4189 : const ip_address_t *end)
4190 : {
4191 44 : ip_address_copy (&ts->start_addr, start);
4192 44 : ip_address_copy (&ts->end_addr, end);
4193 44 : }
4194 :
4195 : clib_error_t *
4196 44 : ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
4197 : u16 start_port, u16 end_port, ip_address_t start_addr,
4198 : ip_address_t end_addr, int is_local)
4199 : {
4200 : ikev2_profile_t *p;
4201 : clib_error_t *r;
4202 :
4203 44 : p = ikev2_profile_index_by_name (name);
4204 :
4205 44 : if (!p)
4206 : {
4207 0 : r = clib_error_return (0, "unknown profile %v", name);
4208 0 : return r;
4209 : }
4210 :
4211 44 : if (ip_addr_version (&start_addr) != ip_addr_version (&end_addr))
4212 0 : return clib_error_return (0, "IP address version mismatch!");
4213 :
4214 44 : if (is_local)
4215 : {
4216 22 : ikev2_set_ts_addrs (&p->loc_ts, &start_addr, &end_addr);
4217 22 : p->loc_ts.start_port = start_port;
4218 22 : p->loc_ts.end_port = end_port;
4219 22 : p->loc_ts.protocol_id = protocol_id;
4220 22 : ikev2_set_ts_type (&p->loc_ts, &start_addr);
4221 : }
4222 : else
4223 : {
4224 22 : ikev2_set_ts_addrs (&p->rem_ts, &start_addr, &end_addr);
4225 22 : p->rem_ts.start_port = start_port;
4226 22 : p->rem_ts.end_port = end_port;
4227 22 : p->rem_ts.protocol_id = protocol_id;
4228 22 : ikev2_set_ts_type (&p->rem_ts, &start_addr);
4229 : }
4230 :
4231 44 : return 0;
4232 : }
4233 :
4234 : clib_error_t *
4235 4 : ikev2_set_profile_responder_hostname (vlib_main_t *vm, u8 *name, u8 *hostname,
4236 : u32 sw_if_index)
4237 : {
4238 : ikev2_profile_t *p;
4239 : clib_error_t *r;
4240 :
4241 4 : p = ikev2_profile_index_by_name (name);
4242 :
4243 4 : if (!p)
4244 : {
4245 0 : r = clib_error_return (0, "unknown profile %v", name);
4246 0 : return r;
4247 : }
4248 :
4249 4 : p->responder.is_resolved = 0;
4250 4 : p->responder.sw_if_index = sw_if_index;
4251 4 : p->responder.hostname = vec_dup (hostname);
4252 :
4253 4 : return 0;
4254 : }
4255 :
4256 : clib_error_t *
4257 4 : ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
4258 : u32 sw_if_index, ip_address_t addr)
4259 : {
4260 : ikev2_profile_t *p;
4261 : clib_error_t *r;
4262 :
4263 4 : p = ikev2_profile_index_by_name (name);
4264 :
4265 4 : if (!p)
4266 : {
4267 0 : r = clib_error_return (0, "unknown profile %v", name);
4268 0 : return r;
4269 : }
4270 :
4271 4 : p->responder.is_resolved = 1;
4272 4 : p->responder.sw_if_index = sw_if_index;
4273 4 : ip_address_copy (&p->responder.addr, &addr);
4274 :
4275 4 : return 0;
4276 : }
4277 :
4278 : clib_error_t *
4279 8 : ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
4280 : ikev2_transform_encr_type_t crypto_alg,
4281 : ikev2_transform_integ_type_t integ_alg,
4282 : ikev2_transform_dh_type_t dh_type,
4283 : u32 crypto_key_size)
4284 : {
4285 : ikev2_profile_t *p;
4286 :
4287 8 : p = ikev2_profile_index_by_name (name);
4288 8 : if (!p)
4289 0 : return clib_error_return (0, "unknown profile %v", name);
4290 :
4291 8 : if ((IKEV2_TRANSFORM_INTEG_TYPE_NONE != integ_alg) +
4292 8 : (IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16 == crypto_alg) !=
4293 : 1)
4294 0 : return clib_error_return (0, "invalid cipher + integrity algorithm");
4295 :
4296 8 : p->ike_ts.crypto_alg = crypto_alg;
4297 8 : p->ike_ts.integ_alg = integ_alg;
4298 8 : p->ike_ts.dh_type = dh_type;
4299 8 : p->ike_ts.crypto_key_size = crypto_key_size;
4300 8 : return 0;
4301 : }
4302 :
4303 : clib_error_t *
4304 8 : ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
4305 : ikev2_transform_encr_type_t crypto_alg,
4306 : ikev2_transform_integ_type_t integ_alg,
4307 : u32 crypto_key_size)
4308 : {
4309 : ikev2_profile_t *p;
4310 : clib_error_t *r;
4311 :
4312 8 : p = ikev2_profile_index_by_name (name);
4313 :
4314 8 : if (!p)
4315 : {
4316 0 : r = clib_error_return (0, "unknown profile %v", name);
4317 0 : return r;
4318 : }
4319 :
4320 8 : p->esp_ts.crypto_alg = crypto_alg;
4321 8 : p->esp_ts.integ_alg = integ_alg;
4322 8 : p->esp_ts.crypto_key_size = crypto_key_size;
4323 8 : return 0;
4324 : }
4325 :
4326 : clib_error_t *
4327 1 : ikev2_set_profile_tunnel_interface (vlib_main_t * vm,
4328 : u8 * name, u32 sw_if_index)
4329 : {
4330 : ikev2_profile_t *p;
4331 : clib_error_t *r;
4332 :
4333 1 : p = ikev2_profile_index_by_name (name);
4334 :
4335 1 : if (!p)
4336 : {
4337 0 : r = clib_error_return (0, "unknown profile %v", name);
4338 0 : return r;
4339 : }
4340 :
4341 1 : p->tun_itf = sw_if_index;
4342 :
4343 1 : return 0;
4344 : }
4345 :
4346 : vnet_api_error_t
4347 2 : ikev2_set_profile_ipsec_udp_port (vlib_main_t * vm, u8 * name, u16 port,
4348 : u8 is_set)
4349 : {
4350 2 : ikev2_profile_t *p = ikev2_profile_index_by_name (name);
4351 2 : vnet_api_error_t rv = 0;
4352 :
4353 2 : if (!p)
4354 0 : return VNET_API_ERROR_INVALID_VALUE;
4355 :
4356 2 : if (is_set)
4357 : {
4358 2 : if (p->ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
4359 0 : return VNET_API_ERROR_VALUE_EXIST;
4360 :
4361 2 : rv = ikev2_register_udp_port (p, port);
4362 : }
4363 : else
4364 : {
4365 0 : if (p->ipsec_over_udp_port == IPSEC_UDP_PORT_NONE)
4366 0 : return VNET_API_ERROR_INVALID_VALUE;
4367 :
4368 0 : ikev2_unregister_udp_port (p);
4369 : }
4370 2 : return rv;
4371 : }
4372 :
4373 : clib_error_t *
4374 2 : ikev2_set_profile_udp_encap (vlib_main_t * vm, u8 * name)
4375 : {
4376 2 : ikev2_profile_t *p = ikev2_profile_index_by_name (name);
4377 : clib_error_t *r;
4378 :
4379 2 : if (!p)
4380 : {
4381 0 : r = clib_error_return (0, "unknown profile %v", name);
4382 0 : return r;
4383 : }
4384 :
4385 2 : p->udp_encap = 1;
4386 2 : return 0;
4387 : }
4388 :
4389 : clib_error_t *
4390 1 : ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
4391 : u64 lifetime, u32 jitter, u32 handover,
4392 : u64 maxdata)
4393 : {
4394 : ikev2_profile_t *p;
4395 : clib_error_t *r;
4396 :
4397 1 : p = ikev2_profile_index_by_name (name);
4398 :
4399 1 : if (!p)
4400 : {
4401 0 : r = clib_error_return (0, "unknown profile %v", name);
4402 0 : return r;
4403 : }
4404 :
4405 1 : p->lifetime = lifetime;
4406 1 : p->lifetime_jitter = jitter;
4407 1 : p->handover = handover;
4408 1 : p->lifetime_maxdata = maxdata;
4409 1 : return 0;
4410 : }
4411 :
4412 : static int
4413 6 : ikev2_get_if_address (u32 sw_if_index, ip_address_family_t af,
4414 : ip_address_t * out_addr)
4415 : {
4416 : ip4_address_t *if_ip4;
4417 : ip6_address_t *if_ip6;
4418 :
4419 6 : if (af == AF_IP4)
4420 : {
4421 6 : if_ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
4422 6 : if (if_ip4)
4423 : {
4424 6 : ip_address_set (out_addr, if_ip4, AF_IP4);
4425 6 : return 1;
4426 : }
4427 : }
4428 : else
4429 : {
4430 0 : if_ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
4431 0 : if (if_ip6)
4432 : {
4433 0 : ip_address_set (out_addr, if_ip6, AF_IP6);
4434 0 : return 1;
4435 : }
4436 : }
4437 0 : return 0;
4438 : }
4439 :
4440 : static clib_error_t *
4441 4 : ikev2_resolve_responder_hostname (vlib_main_t *vm, ikev2_responder_t *r)
4442 : {
4443 4 : ikev2_main_t *km = &ikev2_main;
4444 4 : dns_cache_entry_t *ep = 0;
4445 4 : dns_pending_request_t _t0, *t0 = &_t0;
4446 4 : dns_resolve_name_t _rn, *rn = &_rn;
4447 : u8 *name;
4448 : int rv;
4449 :
4450 4 : if (!km->dns_resolve_name_ptr)
4451 0 : return clib_error_return (0, "cannot load symbols from dns plugin");
4452 :
4453 4 : t0->request_type = DNS_API_PENDING_NAME_TO_IP;
4454 : /* VPP main curse: IKEv2 uses only non-NULL terminated vectors internally
4455 : * whereas DNS resolver expects a NULL-terminated C-string */
4456 4 : name = vec_dup (r->hostname);
4457 4 : vec_terminate_c_string (name);
4458 4 : rv = ((__typeof__ (dns_resolve_name) *) km->dns_resolve_name_ptr) (name, &ep,
4459 : t0, rn);
4460 4 : vec_free (name);
4461 4 : if (rv < 0)
4462 0 : return clib_error_return (0, "dns lookup failure");
4463 :
4464 4 : if (ep == 0)
4465 0 : return 0;
4466 :
4467 4 : ip_address_copy (&r->addr, &rn->address);
4468 4 : r->is_resolved = 1;
4469 4 : return 0;
4470 : }
4471 :
4472 : clib_error_t *
4473 6 : ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
4474 : {
4475 : ikev2_profile_t *p;
4476 : clib_error_t *r;
4477 6 : ikev2_main_t *km = &ikev2_main;
4478 : vlib_buffer_t *b0;
4479 : ike_header_t *ike0;
4480 6 : u32 bi0 = 0;
4481 6 : int len = sizeof (ike_header_t), valid_ip = 0;
4482 6 : ip_address_t src_if_ip = ip_address_initializer;
4483 :
4484 6 : p = ikev2_profile_index_by_name (name);
4485 :
4486 6 : if (!p)
4487 : {
4488 0 : r = clib_error_return (0, "unknown profile %v", name);
4489 0 : return r;
4490 : }
4491 :
4492 12 : if (p->responder.sw_if_index == ~0 ||
4493 6 : (ip_address_is_zero (&p->responder.addr) &&
4494 4 : vec_len (p->responder.hostname) == 0))
4495 : {
4496 0 : r = clib_error_return (0, "responder not set for profile %v", name);
4497 0 : return r;
4498 : }
4499 :
4500 6 : if (!p->responder.is_resolved)
4501 : {
4502 : /* try to resolve using dns plugin
4503 : * success does not mean we have resolved the name */
4504 4 : r = ikev2_resolve_responder_hostname (vm, &p->responder);
4505 4 : if (r)
4506 0 : return r;
4507 : }
4508 :
4509 12 : if (p->responder.is_resolved &&
4510 6 : ikev2_get_if_address (p->responder.sw_if_index,
4511 6 : ip_addr_version (&p->responder.addr), &src_if_ip))
4512 : {
4513 6 : valid_ip = 1;
4514 : }
4515 :
4516 : /* Prepare the SA and the IKE payload */
4517 : ikev2_sa_t sa;
4518 6 : clib_memset (&sa, 0, sizeof (ikev2_sa_t));
4519 6 : ikev2_payload_chain_t *chain = 0;
4520 6 : ikev2_payload_new_chain (chain);
4521 :
4522 : /* Build the IKE proposal payload */
4523 6 : ikev2_sa_proposal_t *proposals = 0;
4524 6 : ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
4525 6 : proposals[0].proposal_num = 1;
4526 6 : proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
4527 :
4528 : /* Add and then cleanup proposal data */
4529 6 : ikev2_payload_add_sa (chain, proposals);
4530 6 : ikev2_sa_free_proposal_vector (&proposals);
4531 :
4532 6 : sa.is_initiator = 1;
4533 6 : sa.profile_index = p - km->profiles;
4534 6 : sa.state = IKEV2_STATE_SA_INIT;
4535 6 : sa.tun_itf = p->tun_itf;
4536 6 : sa.udp_encap = p->udp_encap;
4537 6 : if (p->natt_disabled)
4538 0 : sa.natt_state = IKEV2_NATT_DISABLED;
4539 6 : sa.ipsec_over_udp_port = p->ipsec_over_udp_port;
4540 6 : sa.is_tun_itf_set = 1;
4541 6 : sa.initial_contact = 1;
4542 6 : sa.dst_port = IKEV2_PORT;
4543 :
4544 6 : ikev2_generate_sa_error_t rc = ikev2_generate_sa_init_data (&sa);
4545 6 : if (rc != IKEV2_GENERATE_SA_INIT_OK)
4546 : {
4547 0 : ikev2_sa_free_all_vec (&sa);
4548 0 : ikev2_payload_destroy_chain (chain);
4549 0 : return clib_error_return (0, "%U", format_ikev2_gen_sa_error, rc);
4550 : }
4551 :
4552 6 : ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
4553 6 : ikev2_payload_add_nonce (chain, sa.i_nonce);
4554 :
4555 : /* Build the child SA proposal */
4556 6 : vec_resize (sa.childs, 1);
4557 6 : ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
4558 6 : &sa.childs[0].i_proposals, 0);
4559 6 : sa.childs[0].i_proposals[0].proposal_num = 1;
4560 6 : sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
4561 6 : RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
4562 : sizeof (sa.childs[0].i_proposals[0].spi));
4563 :
4564 : /* Add NAT detection notification messages (mandatory) */
4565 6 : u8 *nat_detection_sha1 = ikev2_compute_nat_sha1 (
4566 : clib_host_to_net_u64 (sa.ispi), clib_host_to_net_u64 (sa.rspi), &src_if_ip,
4567 6 : clib_host_to_net_u16 (IKEV2_PORT));
4568 :
4569 6 : ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
4570 : nat_detection_sha1);
4571 6 : vec_free (nat_detection_sha1);
4572 6 : nat_detection_sha1 =
4573 6 : ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa.ispi),
4574 : clib_host_to_net_u64 (sa.rspi),
4575 : &p->responder.addr,
4576 6 : clib_host_to_net_u16 (sa.dst_port));
4577 6 : ikev2_payload_add_notify (chain,
4578 : IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
4579 : nat_detection_sha1);
4580 6 : vec_free (nat_detection_sha1);
4581 :
4582 6 : u8 *sig_hash_algo = vec_new (u8, 8);
4583 6 : u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
4584 6 : clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
4585 6 : ikev2_payload_add_notify (chain,
4586 : IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
4587 : sig_hash_algo);
4588 6 : vec_free (sig_hash_algo);
4589 :
4590 6 : bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4591 6 : if (!bi0)
4592 : {
4593 0 : ikev2_sa_free_all_vec (&sa);
4594 0 : ikev2_payload_destroy_chain (chain);
4595 0 : char *errmsg = "buffer alloc failure";
4596 0 : ikev2_log_error (errmsg);
4597 0 : return clib_error_return (0, errmsg);
4598 : }
4599 6 : ike0 = vlib_buffer_get_current (b0);
4600 :
4601 : /* Buffer update and boilerplate */
4602 6 : len += vec_len (chain->data);
4603 6 : ike0->nextpayload = chain->first_payload_type;
4604 6 : ike0->length = clib_host_to_net_u32 (len);
4605 6 : clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
4606 6 : ikev2_payload_destroy_chain (chain);
4607 :
4608 6 : ike0->version = IKE_VERSION_2;
4609 6 : ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
4610 6 : ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
4611 6 : ike0->ispi = clib_host_to_net_u64 (sa.ispi);
4612 6 : ike0->rspi = 0;
4613 6 : ike0->msgid = 0;
4614 6 : sa.last_init_msg_id += 1;
4615 :
4616 : /* store whole IKE payload - needed for PSK auth */
4617 6 : vec_reset_length (sa.last_sa_init_req_packet_data);
4618 6 : vec_add (sa.last_sa_init_req_packet_data, ike0, len);
4619 :
4620 : /* add data to the SA then add it to the pool */
4621 6 : ip_address_copy (&sa.iaddr, &src_if_ip);
4622 6 : ip_address_copy (&sa.raddr, &p->responder.addr);
4623 6 : sa.i_id.type = p->loc_id.type;
4624 6 : sa.i_id.data = vec_dup (p->loc_id.data);
4625 6 : sa.r_id.type = p->rem_id.type;
4626 6 : sa.r_id.data = vec_dup (p->rem_id.data);
4627 6 : sa.i_auth.method = p->auth.method;
4628 6 : sa.i_auth.hex = p->auth.hex;
4629 6 : sa.i_auth.data = vec_dup (p->auth.data);
4630 6 : sa.sw_if_index = p->responder.sw_if_index;
4631 6 : vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
4632 6 : vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
4633 :
4634 6 : ikev2_initial_contact_cleanup (0, &sa);
4635 :
4636 : /* add SA to the pool */
4637 6 : ikev2_sa_t *sa0 = 0;
4638 6 : pool_get (km->sais, sa0);
4639 6 : clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
4640 6 : hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
4641 :
4642 6 : if (valid_ip)
4643 : {
4644 6 : ikev2_send_ike (vm, &src_if_ip, &p->responder.addr, bi0, len, IKEV2_PORT,
4645 6 : sa.dst_port, sa.sw_if_index);
4646 :
4647 6 : ikev2_elog_exchange
4648 : ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to ",
4649 : clib_host_to_net_u64 (sa0->ispi), 0,
4650 : ip_addr_v4 (&p->responder.addr).as_u32,
4651 : ip_addr_version (&p->responder.addr) == AF_IP4);
4652 : }
4653 :
4654 6 : return 0;
4655 : }
4656 :
4657 : static void
4658 0 : ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
4659 : ikev2_child_sa_t * csa)
4660 : {
4661 : /* Create the Initiator notification for child SA removal */
4662 0 : ikev2_main_t *km = &ikev2_main;
4663 : ike_header_t *ike0;
4664 0 : u32 bi0 = 0;
4665 : vlib_buffer_t *b0;
4666 : int len;
4667 :
4668 0 : bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4669 0 : if (!bi0)
4670 : {
4671 0 : ikev2_log_error ("buffer alloc failure");
4672 0 : return;
4673 : }
4674 :
4675 0 : ike0 = vlib_buffer_get_current (b0);
4676 0 : ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
4677 0 : ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4678 0 : ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4679 0 : ike0->flags = 0;
4680 0 : vec_resize (sa->del, 1);
4681 0 : sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
4682 0 : sa->del->spi = csa->i_proposals->spi;
4683 0 : ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4684 0 : sa->last_init_msg_id += 1;
4685 0 : len = ikev2_generate_message (b0, sa, ike0, 0, 0, 0);
4686 0 : if (~0 == len)
4687 0 : return;
4688 :
4689 0 : if (ikev2_natt_active (sa))
4690 0 : len = ikev2_insert_non_esp_marker (ike0, len);
4691 0 : ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len,
4692 0 : ikev2_get_port (sa), sa->dst_port, sa->sw_if_index);
4693 :
4694 : /* delete local child SA */
4695 0 : ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
4696 0 : ikev2_sa_del_child_sa (sa, csa);
4697 : }
4698 :
4699 : clib_error_t *
4700 0 : ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
4701 : {
4702 : clib_error_t *r;
4703 0 : ikev2_main_t *km = &ikev2_main;
4704 : ikev2_main_per_thread_data_t *tkm;
4705 0 : ikev2_sa_t *fsa = 0;
4706 0 : ikev2_child_sa_t *fchild = 0;
4707 :
4708 : /* Search for the child SA */
4709 0 : vec_foreach (tkm, km->per_thread_data)
4710 : {
4711 : ikev2_sa_t *sa;
4712 0 : if (fchild)
4713 0 : break;
4714 : /* *INDENT-OFF* */
4715 0 : pool_foreach (sa, tkm->sas) {
4716 0 : fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
4717 0 : if (fchild)
4718 : {
4719 0 : fsa = sa;
4720 0 : break;
4721 : }
4722 : }
4723 : /* *INDENT-ON* */
4724 : }
4725 :
4726 0 : if (!fchild || !fsa)
4727 : {
4728 0 : r = clib_error_return (0, "Child SA not found");
4729 0 : return r;
4730 : }
4731 : else
4732 : {
4733 0 : ikev2_delete_child_sa_internal (vm, fsa, fchild);
4734 : }
4735 :
4736 0 : return 0;
4737 : }
4738 :
4739 : clib_error_t *
4740 6 : ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
4741 : {
4742 : clib_error_t *r;
4743 6 : ikev2_main_t *km = &ikev2_main;
4744 : ikev2_main_per_thread_data_t *tkm;
4745 6 : ikev2_sa_t *fsa = 0;
4746 6 : ikev2_main_per_thread_data_t *ftkm = 0;
4747 :
4748 : /* Search for the IKE SA */
4749 12 : vec_foreach (tkm, km->per_thread_data)
4750 : {
4751 : ikev2_sa_t *sa;
4752 6 : if (fsa)
4753 0 : break;
4754 : /* *INDENT-OFF* */
4755 6 : pool_foreach (sa, tkm->sas) {
4756 6 : if (sa->ispi == ispi)
4757 : {
4758 6 : fsa = sa;
4759 6 : ftkm = tkm;
4760 6 : break;
4761 : }
4762 : }
4763 : /* *INDENT-ON* */
4764 : }
4765 :
4766 6 : if (!fsa)
4767 : {
4768 0 : r = clib_error_return (0, "IKE SA not found");
4769 0 : return r;
4770 : }
4771 :
4772 6 : ikev2_initiate_delete_ike_sa_internal (vm, ftkm, fsa, 1);
4773 6 : return 0;
4774 : }
4775 :
4776 : static void
4777 3 : ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
4778 : ikev2_child_sa_t * csa)
4779 : {
4780 : /* Create the Initiator request for create child SA */
4781 : ike_header_t *ike0;
4782 : vlib_buffer_t *b0;
4783 3 : u32 bi0 = 0;
4784 : int len;
4785 :
4786 3 : bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4787 3 : if (!bi0)
4788 : {
4789 0 : ikev2_log_error ("buffer alloc failure");
4790 0 : return;
4791 : }
4792 :
4793 3 : ike0 = vlib_buffer_get_current (b0);
4794 3 : ike0->version = IKE_VERSION_2;
4795 3 : ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
4796 3 : ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
4797 3 : ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4798 3 : ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4799 3 : ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4800 3 : sa->last_init_msg_id += 1;
4801 :
4802 : ikev2_rekey_t *rekey;
4803 3 : vec_reset_length (sa->rekey);
4804 3 : vec_add2 (sa->rekey, rekey, 1);
4805 3 : rekey->kex = 0;
4806 3 : ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
4807 :
4808 : /*need new ispi */
4809 3 : RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
4810 3 : rekey->spi = proposals[0].spi;
4811 3 : rekey->ispi = csa->i_proposals->spi;
4812 3 : len = ikev2_generate_message (b0, sa, ike0, proposals, 0, 0);
4813 3 : if (~0 == len)
4814 0 : return;
4815 :
4816 3 : if (ikev2_natt_active (sa))
4817 0 : len = ikev2_insert_non_esp_marker (ike0, len);
4818 3 : ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len,
4819 3 : ikev2_get_port (sa), ikev2_get_port (sa), sa->sw_if_index);
4820 3 : vec_free (proposals);
4821 : }
4822 :
4823 : clib_error_t *
4824 3 : ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
4825 : {
4826 : clib_error_t *r;
4827 3 : ikev2_main_t *km = &ikev2_main;
4828 : ikev2_main_per_thread_data_t *tkm;
4829 3 : ikev2_sa_t *fsa = 0;
4830 3 : ikev2_child_sa_t *fchild = 0;
4831 :
4832 : /* Search for the child SA */
4833 6 : vec_foreach (tkm, km->per_thread_data)
4834 : {
4835 : ikev2_sa_t *sa;
4836 3 : if (fchild)
4837 0 : break;
4838 : /* *INDENT-OFF* */
4839 3 : pool_foreach (sa, tkm->sas) {
4840 3 : fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
4841 3 : if (fchild)
4842 : {
4843 3 : fsa = sa;
4844 3 : break;
4845 : }
4846 : }
4847 : /* *INDENT-ON* */
4848 : }
4849 :
4850 3 : if (!fchild || !fsa)
4851 : {
4852 0 : r = clib_error_return (0, "Child SA not found");
4853 0 : return r;
4854 : }
4855 : else
4856 : {
4857 3 : ikev2_rekey_child_sa_internal (vm, fsa, fchild);
4858 : }
4859 :
4860 3 : return 0;
4861 : }
4862 :
4863 : static int
4864 0 : ikev2_sa_sw_if_match (ikev2_sa_t * sa, u32 sw_if_index)
4865 : {
4866 0 : return (sa->sw_if_index == sw_if_index) && sa->is_initiator;
4867 : }
4868 :
4869 : static void
4870 0 : ikev2_sa_del (ikev2_profile_t * p, u32 sw_if_index)
4871 : {
4872 0 : u64 *ispi, *ispi_vec = 0;
4873 0 : ikev2_sa_t *sa, **sap, **sa_vec = 0;
4874 0 : ikev2_main_t *km = &ikev2_main;
4875 : ikev2_main_per_thread_data_t *tkm;
4876 0 : p->responder.sw_if_index = ~0;
4877 :
4878 0 : vec_foreach (tkm, km->per_thread_data)
4879 : {
4880 : /* *INDENT-OFF* */
4881 0 : pool_foreach (sa, tkm->sas) {
4882 0 : if (ikev2_sa_sw_if_match (sa, sw_if_index))
4883 0 : vec_add1 (sa_vec, sa);
4884 : }
4885 : /* *INDENT-ON* */
4886 :
4887 0 : vec_foreach (sap, sa_vec)
4888 : {
4889 0 : ikev2_initiate_delete_ike_sa_internal (km->vlib_main, tkm, *sap, 0);
4890 : }
4891 0 : vec_reset_length (sa_vec);
4892 : }
4893 0 : vec_free (sa_vec);
4894 :
4895 : /* *INDENT-OFF* */
4896 0 : pool_foreach (sa, km->sais) {
4897 0 : if (ikev2_sa_sw_if_match (sa, sw_if_index))
4898 0 : vec_add1 (ispi_vec, sa->ispi);
4899 : }
4900 : /* *INDENT-ON* */
4901 :
4902 0 : vec_foreach (ispi, ispi_vec)
4903 : {
4904 0 : ikev2_del_sa_init_from_main (ispi);
4905 : }
4906 :
4907 0 : vec_free (ispi_vec);
4908 0 : }
4909 :
4910 : static clib_error_t *
4911 11798 : ikev2_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
4912 : {
4913 11798 : ikev2_main_t *km = &ikev2_main;
4914 : ikev2_profile_t *p;
4915 :
4916 11798 : if (is_add)
4917 7547 : return 0;
4918 :
4919 : /* *INDENT-OFF* */
4920 4270 : pool_foreach (p, km->profiles) {
4921 19 : if (p->responder.sw_if_index == sw_if_index)
4922 0 : ikev2_sa_del (p, sw_if_index);
4923 : }
4924 : /* *INDENT-ON* */
4925 :
4926 4251 : return 0;
4927 : }
4928 :
4929 1731 : VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ikev2_sw_interface_add_del);
4930 :
4931 : clib_error_t *
4932 575 : ikev2_init (vlib_main_t * vm)
4933 : {
4934 575 : ikev2_main_t *km = &ikev2_main;
4935 :
4936 575 : clib_memset (km, 0, sizeof (ikev2_main_t));
4937 :
4938 575 : km->log_level = IKEV2_LOG_ERROR;
4939 575 : km->log_class = vlib_log_register_class ("ikev2", 0);
4940 :
4941 575 : km->vnet_main = vnet_get_main ();
4942 575 : km->vlib_main = vm;
4943 :
4944 575 : km->liveness_period = IKEV2_LIVENESS_PERIOD_CHECK;
4945 575 : km->liveness_max_retries = IKEV2_LIVENESS_RETRIES;
4946 :
4947 575 : return 0;
4948 : }
4949 :
4950 : /* *INDENT-OFF* */
4951 1151 : VLIB_INIT_FUNCTION (ikev2_init) = {
4952 : .runs_after = VLIB_INITS ("ipsec_init", "ipsec_punt_init"),
4953 : };
4954 : /* *INDENT-ON* */
4955 :
4956 : static u8
4957 16 : ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa,
4958 : u8 del_old_ids)
4959 : {
4960 16 : ikev2_main_t *km = &ikev2_main;
4961 16 : ikev2_profile_t *p = 0;
4962 16 : vlib_main_t *vm = km->vlib_main;
4963 16 : f64 now = vlib_time_now (vm);
4964 16 : u8 res = 0;
4965 :
4966 16 : if (sa->profile_index != ~0)
4967 16 : p = pool_elt_at_index (km->profiles, sa->profile_index);
4968 :
4969 16 : if (sa->is_initiator && p && csa->time_to_expiration
4970 0 : && now > csa->time_to_expiration)
4971 : {
4972 0 : if (!csa->is_expired || csa->rekey_retries > 0)
4973 : {
4974 0 : ikev2_rekey_child_sa_internal (vm, sa, csa);
4975 0 : csa->time_to_expiration = now + p->handover;
4976 0 : csa->is_expired = 1;
4977 0 : if (csa->rekey_retries == 0)
4978 : {
4979 0 : csa->rekey_retries = 5;
4980 : }
4981 0 : else if (csa->rekey_retries > 0)
4982 : {
4983 0 : csa->rekey_retries--;
4984 0 : ikev2_log_debug ("Rekeying Child SA 0x%x, retries left %d",
4985 : csa->i_proposals->spi, csa->rekey_retries);
4986 0 : if (csa->rekey_retries == 0)
4987 : {
4988 0 : csa->rekey_retries = -1;
4989 : }
4990 : }
4991 0 : res |= 1;
4992 : }
4993 : else
4994 : {
4995 0 : csa->time_to_expiration = 0;
4996 0 : ikev2_delete_child_sa_internal (vm, sa, csa);
4997 0 : res |= 1;
4998 0 : return res;
4999 : }
5000 : }
5001 :
5002 16 : if (del_old_ids)
5003 : {
5004 2 : ipip_tunnel_t *ipip = NULL;
5005 2 : u32 sw_if_index = sa->is_tun_itf_set ? sa->tun_itf : ~0;
5006 2 : if (~0 == sw_if_index)
5007 : {
5008 : ip46_address_t local_ip;
5009 : ip46_address_t remote_ip;
5010 2 : if (sa->is_initiator)
5011 : {
5012 0 : local_ip = to_ip46 (ip_addr_version (&sa->iaddr),
5013 : ip_addr_bytes (&sa->iaddr));
5014 0 : remote_ip = to_ip46 (ip_addr_version (&sa->raddr),
5015 : ip_addr_bytes (&sa->raddr));
5016 : }
5017 : else
5018 : {
5019 2 : local_ip = to_ip46 (ip_addr_version (&sa->raddr),
5020 : ip_addr_bytes (&sa->raddr));
5021 2 : remote_ip = to_ip46 (ip_addr_version (&sa->iaddr),
5022 : ip_addr_bytes (&sa->iaddr));
5023 : }
5024 :
5025 : /* *INDENT-OFF* */
5026 2 : ipip_tunnel_key_t key = {
5027 : .src = local_ip,
5028 : .dst = remote_ip,
5029 : .transport = IPIP_TRANSPORT_IP4,
5030 : .fib_index = 0,
5031 : };
5032 : /* *INDENT-ON* */
5033 :
5034 2 : ipip = ipip_tunnel_db_find (&key);
5035 :
5036 2 : if (ipip)
5037 2 : sw_if_index = ipip->sw_if_index;
5038 : else
5039 0 : return res;
5040 : }
5041 :
5042 2 : u32 *sas_in = NULL;
5043 2 : vec_add1 (sas_in, csa->remote_sa_id);
5044 2 : vlib_worker_thread_barrier_sync (vm);
5045 2 : int rv = ipsec_tun_protect_update (sw_if_index, NULL,
5046 : csa->local_sa_id, sas_in);
5047 2 : if (rv)
5048 0 : vec_free (sas_in);
5049 2 : ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (csa->remote_sa_id));
5050 2 : vlib_worker_thread_barrier_release (vm);
5051 : }
5052 :
5053 16 : return res;
5054 : }
5055 :
5056 : int
5057 20 : ikev2_set_log_level (ikev2_log_level_t log_level)
5058 : {
5059 20 : ikev2_main_t *km = &ikev2_main;
5060 :
5061 20 : if (log_level >= IKEV2_LOG_MAX)
5062 : {
5063 0 : ikev2_log_error ("unknown logging level %d", log_level);
5064 0 : return -1;
5065 : }
5066 :
5067 20 : km->log_level = log_level;
5068 20 : return 0;
5069 : }
5070 :
5071 : clib_error_t *
5072 2 : ikev2_set_liveness_params (u32 period, u32 max_retries)
5073 : {
5074 2 : ikev2_main_t *km = &ikev2_main;
5075 :
5076 2 : if (period == 0 || max_retries == 0)
5077 0 : return clib_error_return (0, "invalid args");
5078 :
5079 2 : km->liveness_period = period;
5080 2 : km->liveness_max_retries = max_retries;
5081 2 : return 0;
5082 : }
5083 :
5084 : clib_error_t *
5085 1 : ikev2_profile_natt_disable (u8 * name)
5086 : {
5087 1 : ikev2_profile_t *p = ikev2_profile_index_by_name (name);
5088 1 : if (!p)
5089 0 : return clib_error_return (0, "unknown profile %v", name);
5090 :
5091 1 : p->natt_disabled = 1;
5092 1 : return 0;
5093 : }
5094 :
5095 : static void
5096 39 : ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
5097 : {
5098 39 : ikev2_main_t *km = &ikev2_main;
5099 39 : vlib_main_t *vm = km->vlib_main;
5100 : ikev2_main_per_thread_data_t *tkm;
5101 39 : ikev2_sa_t *fsa = 0;
5102 39 : ikev2_profile_t *p = 0;
5103 39 : ikev2_child_sa_t *fchild = 0;
5104 39 : f64 now = vlib_time_now (vm);
5105 : vlib_counter_t counts;
5106 :
5107 : /* Search for the SA and child SA */
5108 78 : vec_foreach (tkm, km->per_thread_data)
5109 : {
5110 : ikev2_sa_t *sa;
5111 39 : if (fchild)
5112 0 : break;
5113 : /* *INDENT-OFF* */
5114 62 : pool_foreach (sa, tkm->sas) {
5115 39 : fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
5116 39 : if (fchild)
5117 : {
5118 16 : fsa = sa;
5119 16 : break;
5120 : }
5121 : }
5122 : /* *INDENT-ON* */
5123 : }
5124 39 : vlib_get_combined_counter (&ipsec_sa_counters,
5125 : ipsec_sa->stat_index, &counts);
5126 :
5127 39 : if (fsa && fsa->profile_index != ~0 && fsa->is_initiator)
5128 2 : p = pool_elt_at_index (km->profiles, fsa->profile_index);
5129 :
5130 39 : if (fchild && p && p->lifetime_maxdata)
5131 : {
5132 0 : if (!fchild->is_expired && counts.bytes > p->lifetime_maxdata)
5133 : {
5134 0 : fchild->time_to_expiration = now;
5135 : }
5136 : }
5137 39 : }
5138 :
5139 : static void
5140 0 : ikev2_process_pending_sa_init_one (vlib_main_t *vm, ikev2_main_t *km,
5141 : ikev2_sa_t *sa)
5142 : {
5143 : ikev2_profile_t *p;
5144 : u32 bi0;
5145 : u8 *nat_sha, *np;
5146 0 : p = pool_elt_at_index (km->profiles, sa->profile_index);
5147 :
5148 0 : if (!p->responder.is_resolved)
5149 : {
5150 0 : clib_error_t *r = ikev2_resolve_responder_hostname (vm, &p->responder);
5151 0 : if (r)
5152 : {
5153 0 : clib_error_free (r);
5154 0 : return;
5155 : }
5156 :
5157 0 : if (!p->responder.is_resolved)
5158 0 : return;
5159 :
5160 0 : ip_address_copy (&sa->raddr, &p->responder.addr);
5161 :
5162 : /* update nat detection destination hash */
5163 0 : np = ikev2_find_ike_notify_payload (
5164 0 : (ike_header_t *) sa->last_sa_init_req_packet_data,
5165 : IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP);
5166 0 : if (np)
5167 : {
5168 0 : nat_sha = ikev2_compute_nat_sha1 (
5169 : clib_host_to_net_u64 (sa->ispi), clib_host_to_net_u64 (sa->rspi),
5170 0 : &sa->raddr, clib_host_to_net_u16 (sa->dst_port));
5171 0 : clib_memcpy_fast (np, nat_sha, vec_len (nat_sha));
5172 0 : vec_free (nat_sha);
5173 : }
5174 : }
5175 :
5176 0 : if (ip_address_is_zero (&sa->iaddr))
5177 : {
5178 0 : if (!ikev2_get_if_address (p->responder.sw_if_index,
5179 0 : ip_addr_version (&p->responder.addr),
5180 : &sa->iaddr))
5181 0 : return;
5182 :
5183 : /* update NAT detection payload */
5184 : np =
5185 0 : ikev2_find_ike_notify_payload
5186 0 : ((ike_header_t *) sa->last_sa_init_req_packet_data,
5187 : IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP);
5188 0 : if (np)
5189 : {
5190 0 : nat_sha =
5191 0 : ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
5192 : clib_host_to_net_u64 (sa->rspi),
5193 : &sa->iaddr,
5194 0 : clib_host_to_net_u16 (IKEV2_PORT));
5195 0 : clib_memcpy_fast (np, nat_sha, vec_len (nat_sha));
5196 0 : vec_free (nat_sha);
5197 : }
5198 : }
5199 :
5200 0 : if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
5201 0 : return;
5202 :
5203 0 : vlib_buffer_t *b = vlib_get_buffer (km->vlib_main, bi0);
5204 0 : clib_memcpy_fast (vlib_buffer_get_current (b),
5205 0 : sa->last_sa_init_req_packet_data,
5206 0 : vec_len (sa->last_sa_init_req_packet_data));
5207 :
5208 0 : ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0,
5209 0 : vec_len (sa->last_sa_init_req_packet_data),
5210 0 : ikev2_get_port (sa), IKEV2_PORT, sa->sw_if_index);
5211 : }
5212 :
5213 : static void
5214 42 : ikev2_process_pending_sa_init (vlib_main_t *vm, ikev2_main_t *km)
5215 : {
5216 : u32 sai;
5217 : u64 ispi;
5218 : ikev2_sa_t *sa;
5219 :
5220 : /* *INDENT-OFF* */
5221 2730 : hash_foreach (ispi, sai, km->sa_by_ispi,
5222 : ({
5223 : sa = pool_elt_at_index (km->sais, sai);
5224 : if (sa->init_response_received)
5225 : continue;
5226 :
5227 : ikev2_process_pending_sa_init_one (vm, km, sa);
5228 : }));
5229 : /* *INDENT-ON* */
5230 42 : }
5231 :
5232 : static void
5233 2 : ikev2_send_informational_request (ikev2_sa_t * sa)
5234 : {
5235 2 : ikev2_main_t *km = &ikev2_main;
5236 : ip_address_t *src, *dst;
5237 : ike_header_t *ike0;
5238 : vlib_buffer_t *b0;
5239 2 : u32 bi0 = 0;
5240 : u16 dp;
5241 : int len;
5242 :
5243 2 : bi0 = ikev2_get_new_ike_header_buff (km->vlib_main, &b0);
5244 2 : if (!bi0)
5245 : {
5246 0 : ikev2_log_error ("buffer alloc failure");
5247 0 : return;
5248 : }
5249 :
5250 2 : ike0 = vlib_buffer_get_current (b0);
5251 2 : ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
5252 2 : ike0->ispi = clib_host_to_net_u64 (sa->ispi);
5253 2 : ike0->rspi = clib_host_to_net_u64 (sa->rspi);
5254 2 : ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
5255 2 : ike0->flags = 0;
5256 2 : sa->last_init_msg_id += 1;
5257 2 : len = ikev2_generate_message (b0, sa, ike0, 0, 0, 0);
5258 2 : if (~0 == len)
5259 0 : return;
5260 :
5261 2 : if (ikev2_natt_active (sa))
5262 0 : len = ikev2_insert_non_esp_marker (ike0, len);
5263 :
5264 2 : if (sa->is_initiator)
5265 : {
5266 0 : src = &sa->iaddr;
5267 0 : dst = &sa->raddr;
5268 : }
5269 : else
5270 : {
5271 2 : dst = &sa->iaddr;
5272 2 : src = &sa->raddr;
5273 : }
5274 :
5275 2 : dp = sa->dst_port ? sa->dst_port : ikev2_get_port (sa);
5276 2 : ikev2_send_ike (km->vlib_main, src, dst, bi0, len, ikev2_get_port (sa), dp,
5277 : sa->sw_if_index);
5278 : }
5279 :
5280 : void
5281 18 : ikev2_disable_dpd (void)
5282 : {
5283 18 : ikev2_main_t *km = &ikev2_main;
5284 18 : km->dpd_disabled = 1;
5285 18 : }
5286 :
5287 : static int
5288 5 : ikev2_mngr_process_responder_sas (ikev2_sa_t *sa)
5289 : {
5290 5 : ikev2_main_t *km = &ikev2_main;
5291 5 : vlib_main_t *vm = km->vlib_main;
5292 :
5293 5 : if (!sa->keys_generated)
5294 0 : return 0;
5295 :
5296 5 : if (sa->liveness_retries >= km->liveness_max_retries)
5297 1 : return 1;
5298 :
5299 4 : f64 now = vlib_time_now (vm);
5300 :
5301 4 : if (sa->liveness_period_check < now)
5302 : {
5303 2 : sa->liveness_retries++;
5304 2 : sa->liveness_period_check = now + km->liveness_period;
5305 2 : ikev2_send_informational_request (sa);
5306 : }
5307 4 : return 0;
5308 : }
5309 :
5310 : static uword
5311 575 : ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
5312 : vlib_frame_t * f)
5313 : {
5314 575 : ikev2_main_t *km = &ikev2_main;
5315 : ikev2_profile_t *p;
5316 : ikev2_child_sa_t *c;
5317 : u32 *sai;
5318 :
5319 : /* lazy init will wake it up */
5320 575 : vlib_process_wait_for_event (vm);
5321 :
5322 : while (1)
5323 42 : {
5324 63 : vlib_process_wait_for_event_or_clock (vm, 2);
5325 42 : vlib_process_get_events (vm, NULL);
5326 :
5327 : /* process ike child sas */
5328 : ikev2_main_per_thread_data_t *tkm;
5329 84 : vec_foreach (tkm, km->per_thread_data)
5330 : {
5331 : ikev2_sa_t *sa;
5332 42 : u32 *to_be_deleted = 0;
5333 :
5334 : /* *INDENT-OFF* */
5335 58 : pool_foreach (sa, tkm->sas) {
5336 : ikev2_child_sa_t *c;
5337 16 : u8 del_old_ids = 0;
5338 :
5339 16 : if (sa->state == IKEV2_STATE_SA_INIT)
5340 : {
5341 0 : if (vec_len (sa->childs) > 0)
5342 0 : vec_add1 (to_be_deleted, sa - tkm->sas);
5343 : }
5344 16 : else if (sa->state != IKEV2_STATE_AUTHENTICATED)
5345 0 : continue;
5346 :
5347 16 : if (sa->old_remote_id_present && 0 > sa->old_id_expiration)
5348 : {
5349 2 : sa->old_remote_id_present = 0;
5350 2 : del_old_ids = 1;
5351 : }
5352 : else
5353 14 : sa->old_id_expiration -= 1;
5354 :
5355 32 : vec_foreach (c, sa->childs)
5356 16 : ikev2_mngr_process_child_sa (sa, c, del_old_ids);
5357 :
5358 16 : if (!km->dpd_disabled && ikev2_mngr_process_responder_sas (sa))
5359 1 : vec_add1 (to_be_deleted, sa - tkm->sas);
5360 : }
5361 :
5362 43 : vec_foreach (sai, to_be_deleted)
5363 : {
5364 1 : sa = pool_elt_at_index (tkm->sas, sai[0]);
5365 1 : const u32 profile_index = sa->profile_index;
5366 1 : const int reinitiate = (sa->is_initiator && profile_index != ~0);
5367 2 : vec_foreach (c, sa->childs)
5368 : {
5369 1 : ikev2_delete_tunnel_interface (km->vnet_main, sa, c);
5370 1 : ikev2_sa_del_child_sa (sa, c);
5371 : }
5372 1 : ikev2_sa_free_all_vec (sa);
5373 1 : hash_unset (tkm->sa_by_rspi, sa->rspi);
5374 1 : pool_put (tkm->sas, sa);
5375 :
5376 1 : if (reinitiate)
5377 : {
5378 0 : p = pool_elt_at_index (km->profiles, profile_index);
5379 0 : if (p)
5380 : {
5381 0 : clib_error_t *e = ikev2_initiate_sa_init (vm, p->name);
5382 0 : if (e)
5383 : {
5384 0 : ikev2_log_error ("%U", format_clib_error, e);
5385 0 : clib_error_free (e);
5386 : }
5387 : }
5388 : }
5389 : }
5390 42 : vec_free (to_be_deleted);
5391 : }
5392 :
5393 : /* process ipsec sas */
5394 : ipsec_sa_t *sa;
5395 : /* *INDENT-OFF* */
5396 81 : pool_foreach (sa, ipsec_sa_pool)
5397 : {
5398 39 : ikev2_mngr_process_ipsec_sa (sa);
5399 : }
5400 : /* *INDENT-ON* */
5401 :
5402 42 : ikev2_process_pending_sa_init (vm, km);
5403 : }
5404 : return 0;
5405 : }
5406 :
5407 : /* *INDENT-OFF* */
5408 126188 : VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
5409 : .function = ikev2_mngr_process_fn,
5410 : .type = VLIB_NODE_TYPE_PROCESS,
5411 : .name =
5412 : "ikev2-manager-process",
5413 : };
5414 :
5415 : static void
5416 42 : ikev2_lazy_init (ikev2_main_t *km)
5417 : {
5418 42 : vlib_thread_main_t *tm = vlib_get_thread_main ();
5419 : int thread_id;
5420 :
5421 42 : if (km->lazy_init_done)
5422 21 : return;
5423 :
5424 21 : ikev2_crypto_init (km);
5425 :
5426 21 : mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
5427 :
5428 21 : vec_validate_aligned (km->per_thread_data, tm->n_vlib_mains - 1,
5429 : CLIB_CACHE_LINE_BYTES);
5430 42 : for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
5431 : {
5432 21 : ikev2_main_per_thread_data_t *ptd =
5433 21 : vec_elt_at_index (km->per_thread_data, thread_id);
5434 :
5435 21 : ptd->sa_by_rspi = hash_create (0, sizeof (uword));
5436 :
5437 : #if OPENSSL_VERSION_NUMBER >= 0x10100000L
5438 21 : ptd->evp_ctx = EVP_CIPHER_CTX_new ();
5439 21 : ptd->hmac_ctx = HMAC_CTX_new ();
5440 : #else
5441 : EVP_CIPHER_CTX_init (&ptd->_evp_ctx);
5442 : ptd->evp_ctx = &ptd->_evp_ctx;
5443 : HMAC_CTX_init (&(ptd->_hmac_ctx));
5444 : ptd->hmac_ctx = &ptd->_hmac_ctx;
5445 : #endif
5446 : }
5447 :
5448 21 : km->sa_by_ispi = hash_create (0, sizeof (uword));
5449 21 : km->sw_if_indices = hash_create (0, 0);
5450 :
5451 21 : km->punt_hdl = vlib_punt_client_register ("ikev2");
5452 :
5453 21 : km->dns_resolve_name_ptr =
5454 21 : vlib_get_plugin_symbol ("dns_plugin.so", "dns_resolve_name");
5455 21 : if (!km->dns_resolve_name_ptr)
5456 0 : ikev2_log_error ("cannot load symbols from dns plugin");
5457 :
5458 : /* wake up ikev2 process */
5459 21 : vlib_process_signal_event (vlib_get_first_main (),
5460 21 : ikev2_mngr_process_node.index, 0, 0);
5461 :
5462 21 : km->lazy_init_done = 1;
5463 : }
5464 :
5465 : VLIB_PLUGIN_REGISTER () = {
5466 : .version = VPP_BUILD_VER,
5467 : .description = "Internet Key Exchange (IKEv2) Protocol",
5468 : };
5469 : /* *INDENT-ON* */
5470 :
5471 : /*
5472 : * fd.io coding-style-patch-verification: ON
5473 : *
5474 : * Local Variables:
5475 : * eval: (c-set-style "gnu")
5476 : * End:
5477 : */
|