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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 <vnet/pg/pg.h>
22 #include <vppinfra/error.h>
23 #include <vppinfra/random.h>
24 #include <vnet/udp/udp.h>
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/ipsec_tun.h>
27 #include <vnet/ipip/ipip.h>
28 #include <plugins/ikev2/ikev2.h>
29 #include <plugins/ikev2/ikev2_priv.h>
30 #include <openssl/sha.h>
32 #define IKEV2_LIVENESS_RETRIES 3
33 #define IKEV2_LIVENESS_PERIOD_CHECK 30
35 ikev2_main_t ikev2_main;
37 static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
39 ikev2_child_sa_t * child);
41 #define ikev2_set_state(sa, v) do { \
43 ikev2_elog_sa_state("ispi %lx SA state changed to " #v, sa->ispi); \
53 format_ikev2_trace (u8 * s, va_list * args)
55 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
56 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
57 ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
59 s = format (s, "ikev2: sw_if_index %d, next index %d",
60 t->sw_if_index, t->next_index);
64 static vlib_node_registration_t ikev2_node;
66 #define foreach_ikev2_error \
67 _(PROCESSED, "IKEv2 packets processed") \
68 _(IKE_SA_INIT_RETRANSMIT, "IKE_SA_INIT retransmit ") \
69 _(IKE_SA_INIT_IGNORE, "IKE_SA_INIT ignore (IKE SA already auth)") \
70 _(IKE_REQ_RETRANSMIT, "IKE request retransmit") \
71 _(IKE_REQ_IGNORE, "IKE request ignore (old msgid)") \
72 _(NOT_IKEV2, "Non IKEv2 packets received")
76 #define _(sym,str) IKEV2_ERROR_##sym,
82 static char *ikev2_error_strings[] = {
83 #define _(sym,string) string,
90 IKEV2_NEXT_IP4_LOOKUP,
91 IKEV2_NEXT_ERROR_DROP,
95 static ikev2_sa_transform_t *
96 ikev2_find_transform_data (ikev2_sa_transform_t * t)
98 ikev2_main_t *km = &ikev2_main;
99 ikev2_sa_transform_t *td;
101 vec_foreach (td, km->supported_transforms)
103 if (td->type != t->type)
106 if (td->transform_id != t->transform_id)
109 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
111 if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
112 || t->attrs[1] != 14)
115 if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
123 static ikev2_sa_proposal_t *
124 ikev2_select_proposal (ikev2_sa_proposal_t * proposals,
125 ikev2_protocol_id_t prot_id)
127 ikev2_sa_proposal_t *rv = 0;
128 ikev2_sa_proposal_t *proposal;
129 ikev2_sa_transform_t *transform, *new_t;
130 u8 mandatory_bitmap, optional_bitmap;
132 if (prot_id == IKEV2_PROTOCOL_IKE)
134 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
135 (1 << IKEV2_TRANSFORM_TYPE_PRF) |
136 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
137 optional_bitmap = mandatory_bitmap;
139 else if (prot_id == IKEV2_PROTOCOL_ESP)
141 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
142 (1 << IKEV2_TRANSFORM_TYPE_ESN);
143 optional_bitmap = mandatory_bitmap |
144 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
146 else if (prot_id == IKEV2_PROTOCOL_AH)
148 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
149 (1 << IKEV2_TRANSFORM_TYPE_ESN);
150 optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
155 vec_add2 (rv, proposal, 1);
157 vec_foreach (proposal, proposals)
160 if (proposal->protocol_id != prot_id)
163 vec_foreach (transform, proposal->transforms)
165 if ((1 << transform->type) & bitmap)
168 if (ikev2_find_transform_data (transform))
170 bitmap |= 1 << transform->type;
171 vec_add2 (rv->transforms, new_t, 1);
172 clib_memcpy_fast (new_t, transform, sizeof (*new_t));
173 new_t->attrs = vec_dup (transform->attrs);
177 if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
178 (bitmap & ~optional_bitmap) == 0)
180 rv->proposal_num = proposal->proposal_num;
181 rv->protocol_id = proposal->protocol_id;
182 RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
187 vec_free (rv->transforms);
196 ikev2_sa_transform_t *
197 ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
198 ikev2_transform_type_t type)
200 ikev2_sa_transform_t *t;
205 vec_foreach (t, p->transforms)
208 return ikev2_find_transform_data (t);
214 ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id,
218 vec_foreach (c, sa->childs)
220 ikev2_sa_proposal_t *proposal =
221 by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
222 if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
230 ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v)
232 ikev2_sa_proposal_t *p;
233 ikev2_sa_transform_t *t;
240 vec_foreach (t, p->transforms)
244 vec_free (p->transforms);
250 ikev2_sa_free_child_sa (ikev2_child_sa_t * c)
252 ikev2_sa_free_proposal_vector (&c->r_proposals);
253 ikev2_sa_free_proposal_vector (&c->i_proposals);
263 ikev2_sa_free_all_child_sa (ikev2_child_sa_t ** childs)
266 vec_foreach (c, *childs) ikev2_sa_free_child_sa (c);
272 ikev2_sa_del_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * child)
274 ikev2_sa_free_child_sa (child);
275 vec_del1 (sa->childs, child - sa->childs);
279 ikev2_sa_free_all_vec (ikev2_sa_t * sa)
281 vec_free (sa->i_nonce);
282 vec_free (sa->i_dh_data);
283 vec_free (sa->dh_shared_key);
284 vec_free (sa->dh_private_key);
286 ikev2_sa_free_proposal_vector (&sa->r_proposals);
287 ikev2_sa_free_proposal_vector (&sa->i_proposals);
290 vec_free (sa->sk_ai);
291 vec_free (sa->sk_ar);
292 vec_free (sa->sk_ei);
293 vec_free (sa->sk_er);
294 vec_free (sa->sk_pi);
295 vec_free (sa->sk_pr);
297 vec_free (sa->i_id.data);
298 vec_free (sa->i_auth.data);
299 vec_free (sa->r_id.data);
300 vec_free (sa->r_auth.data);
302 EVP_PKEY_free (sa->r_auth.key);
306 ikev2_sa_free_all_child_sa (&sa->childs);
310 ikev2_delete_sa (ikev2_sa_t * sa)
312 ikev2_main_t *km = &ikev2_main;
313 u32 thread_index = vlib_get_thread_index ();
316 ikev2_sa_free_all_vec (sa);
318 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
321 hash_unset (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
322 pool_put (km->per_thread_data[thread_index].sas, sa);
327 ikev2_generate_sa_init_data (ikev2_sa_t * sa)
329 ikev2_sa_transform_t *t = 0, *t2;
330 ikev2_main_t *km = &ikev2_main;
332 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
337 /* check if received DH group is on our list of supported groups */
338 vec_foreach (t2, km->supported_transforms)
340 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
349 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
353 if (sa->is_initiator)
356 RAND_bytes ((u8 *) & sa->ispi, 8);
359 sa->i_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
360 RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
365 RAND_bytes ((u8 *) & sa->rspi, 8);
368 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
369 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
372 /* generate dh keys */
373 ikev2_generate_dh (sa, t);
378 ikev2_complete_sa_data (ikev2_sa_t * sa, ikev2_sa_t * sai)
380 ikev2_sa_transform_t *t = 0, *t2;
381 ikev2_main_t *km = &ikev2_main;
383 sai->init_response_received = 1;
385 /*move some data to the new SA */
386 #define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
387 sa->i_nonce = _(sai->i_nonce);
388 sa->i_dh_data = _(sai->i_dh_data);
389 sa->dh_private_key = _(sai->dh_private_key);
390 sa->iaddr.as_u32 = sai->iaddr.as_u32;
391 sa->raddr.as_u32 = sai->raddr.as_u32;
392 sa->is_initiator = sai->is_initiator;
393 sa->i_id.type = sai->i_id.type;
394 sa->profile_index = sai->profile_index;
395 sa->is_profile_index_set = sai->is_profile_index_set;
396 sa->tun_itf = sai->tun_itf;
397 sa->is_tun_itf_set = sai->is_tun_itf_set;
398 sa->i_id.data = _(sai->i_id.data);
399 sa->i_auth.method = sai->i_auth.method;
400 sa->i_auth.hex = sai->i_auth.hex;
401 sa->i_auth.data = _(sai->i_auth.data);
402 sa->i_auth.key = _(sai->i_auth.key);
403 sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data);
404 sa->childs = _(sai->childs);
405 sa->udp_encap = sai->udp_encap;
406 sa->dst_port = sai->dst_port;
410 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
415 /* check if received DH group is on our list of supported groups */
416 vec_foreach (t2, km->supported_transforms)
418 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
427 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
432 /* generate dh keys */
433 ikev2_complete_dh (sa, t);
438 ikev2_calc_keys (ikev2_sa_t * sa)
441 /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
444 u16 integ_key_len = 0;
445 ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
447 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
449 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
451 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
454 integ_key_len = tr_integ->key_len;
456 vec_append (s, sa->i_nonce);
457 vec_append (s, sa->r_nonce);
458 skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
460 /* Calculate S = Ni | Nr | SPIi | SPIr */
462 vec_add2 (s, tmp, 2 * sizeof (*spi));
464 spi[0] = clib_host_to_net_u64 (sa->ispi);
465 spi[1] = clib_host_to_net_u64 (sa->rspi);
467 /* calculate PRFplus */
469 int len = tr_prf->key_trunc + /* SK_d */
470 integ_key_len * 2 + /* SK_ai, SK_ar */
471 tr_encr->key_len * 2 + /* SK_ei, SK_er */
472 tr_prf->key_len * 2; /* SK_pi, SK_pr */
474 keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
481 sa->sk_d = vec_new (u8, tr_prf->key_trunc);
482 clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
483 pos += tr_prf->key_trunc;
488 sa->sk_ai = vec_new (u8, integ_key_len);
489 clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
490 pos += integ_key_len;
493 sa->sk_ar = vec_new (u8, integ_key_len);
494 clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
495 pos += integ_key_len;
499 sa->sk_ei = vec_new (u8, tr_encr->key_len);
500 clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len);
501 pos += tr_encr->key_len;
504 sa->sk_er = vec_new (u8, tr_encr->key_len);
505 clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len);
506 pos += tr_encr->key_len;
509 sa->sk_pi = vec_new (u8, tr_prf->key_len);
510 clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
511 pos += tr_prf->key_len;
514 sa->sk_pr = vec_new (u8, tr_prf->key_len);
515 clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
516 pos += tr_prf->key_len;
522 ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child)
525 u16 integ_key_len = 0;
528 ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
530 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
532 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
534 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
537 integ_key_len = ctr_integ->key_len;
539 salt_len = sizeof (u32);
541 vec_append (s, sa->i_nonce);
542 vec_append (s, sa->r_nonce);
543 /* calculate PRFplus */
545 int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
547 keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
552 child->sk_ei = vec_new (u8, ctr_encr->key_len);
553 clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
554 pos += ctr_encr->key_len;
559 child->sk_ai = vec_new (u8, ctr_integ->key_len);
560 clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
561 pos += ctr_integ->key_len;
565 clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
570 child->sk_er = vec_new (u8, ctr_encr->key_len);
571 clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
572 pos += ctr_encr->key_len;
577 child->sk_ar = vec_new (u8, integ_key_len);
578 clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
579 pos += integ_key_len;
583 clib_memcpy (&child->salt_er, keymat + pos, salt_len);
593 ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
597 u32 len = clib_net_to_host_u32 (ike->length);
598 u8 payload = ike->nextpayload;
600 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
602 clib_net_to_host_u64 (ike->ispi),
603 clib_net_to_host_u64 (ike->rspi), sa->iaddr.as_u32);
605 sa->ispi = clib_net_to_host_u64 (ike->ispi);
607 /* store whole IKE payload - needed for PSK auth */
608 vec_free (sa->last_sa_init_req_packet_data);
609 vec_add (sa->last_sa_init_req_packet_data, ike, len);
611 while (p < len && payload != IKEV2_PAYLOAD_NONE)
613 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
614 u32 plen = clib_net_to_host_u16 (ikep->length);
616 if (plen < sizeof (ike_payload_header_t))
619 if (payload == IKEV2_PAYLOAD_SA)
621 ikev2_sa_free_proposal_vector (&sa->i_proposals);
622 sa->i_proposals = ikev2_parse_sa_payload (ikep);
624 else if (payload == IKEV2_PAYLOAD_KE)
626 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
627 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
628 vec_free (sa->i_dh_data);
629 vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
631 else if (payload == IKEV2_PAYLOAD_NONCE)
633 vec_free (sa->i_nonce);
634 vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
636 else if (payload == IKEV2_PAYLOAD_NOTIFY)
638 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
641 else if (payload == IKEV2_PAYLOAD_VENDOR)
643 ikev2_parse_vendor_payload (ikep);
647 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
649 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
651 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
652 sa->unsupported_cp = payload;
657 payload = ikep->nextpayload;
661 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
665 ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa,
669 u32 len = clib_net_to_host_u32 (ike->length);
670 u8 payload = ike->nextpayload;
672 sa->ispi = clib_net_to_host_u64 (ike->ispi);
673 sa->rspi = clib_net_to_host_u64 (ike->rspi);
675 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
676 "from %d.%d.%d.%d", sa->ispi, sa->rspi,
679 /* store whole IKE payload - needed for PSK auth */
680 vec_free (sa->last_sa_init_res_packet_data);
681 vec_add (sa->last_sa_init_res_packet_data, ike, len);
683 while (p < len && payload != IKEV2_PAYLOAD_NONE)
685 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
686 u32 plen = clib_net_to_host_u16 (ikep->length);
688 if (plen < sizeof (ike_payload_header_t))
691 if (payload == IKEV2_PAYLOAD_SA)
693 ikev2_sa_free_proposal_vector (&sa->r_proposals);
694 sa->r_proposals = ikev2_parse_sa_payload (ikep);
697 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
699 clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
702 else if (payload == IKEV2_PAYLOAD_KE)
704 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
705 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
706 vec_free (sa->r_dh_data);
707 vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
709 else if (payload == IKEV2_PAYLOAD_NONCE)
711 vec_free (sa->r_nonce);
712 vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
714 else if (payload == IKEV2_PAYLOAD_NOTIFY)
716 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
719 else if (payload == IKEV2_PAYLOAD_VENDOR)
721 ikev2_parse_vendor_payload (ikep);
725 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
727 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
729 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
730 sa->unsupported_cp = payload;
735 payload = ikep->nextpayload;
741 ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
746 u32 len = clib_net_to_host_u32 (ike->length);
747 ike_payload_header_t *ikep = 0;
749 ikev2_sa_transform_t *tr_integ;
751 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
754 *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
756 ikep = (ike_payload_header_t *) & ike->payload[p];
757 plen = clib_net_to_host_u16 (ikep->length);
759 if (plen < sizeof (*ikep))
762 if (*payload == IKEV2_PAYLOAD_SK)
764 last_payload = *payload;
768 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
770 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
772 sa->unsupported_cp = *payload;
777 *payload = ikep->nextpayload;
781 if (last_payload != IKEV2_PAYLOAD_SK)
783 ikev2_elog_error ("Last payload must be SK");
788 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
789 (u8 *) ike, len - tr_integ->key_trunc);
791 plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
793 if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
795 ikev2_elog_error ("message integrity check failed");
801 return ikev2_decrypt_data (sa, ikep->payload, plen);
805 ikev2_initial_contact_cleanup (ikev2_sa_t * sa)
807 ikev2_main_t *km = &ikev2_main;
811 u32 thread_index = vlib_get_thread_index ();
813 if (!sa->initial_contact)
816 /* find old IKE SAs with the same authenticated identity */
818 pool_foreach (tmp, km->per_thread_data[thread_index].sas, ({
819 if (tmp->i_id.type != sa->i_id.type ||
820 vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
821 memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
824 if (sa->rspi != tmp->rspi)
825 vec_add1(delete, tmp - km->per_thread_data[thread_index].sas);
829 for (i = 0; i < vec_len (delete); i++)
832 pool_elt_at_index (km->per_thread_data[thread_index].sas, delete[i]);
834 tmp->childs) ikev2_delete_tunnel_interface (km->vnet_main,
836 ikev2_delete_sa (tmp);
840 sa->initial_contact = 0;
844 ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
846 ikev2_child_sa_t *first_child_sa;
848 u8 payload = ike->nextpayload;
850 ike_payload_header_t *ikep;
853 ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
854 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
855 clib_host_to_net_u64 (ike->rspi),
856 sa->is_initiator ? sa->raddr.as_u32 : sa->
859 ikev2_calc_keys (sa);
861 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
865 if (sa->unsupported_cp)
866 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
867 goto cleanup_and_exit;
870 /* select or create 1st child SA */
871 if (sa->is_initiator)
873 first_child_sa = &sa->childs[0];
877 ikev2_sa_free_all_child_sa (&sa->childs);
878 vec_add2 (sa->childs, first_child_sa, 1);
882 /* process encrypted payload */
884 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
886 ikep = (ike_payload_header_t *) & plaintext[p];
887 plen = clib_net_to_host_u16 (ikep->length);
889 if (plen < sizeof (ike_payload_header_t))
890 goto cleanup_and_exit;
892 if (payload == IKEV2_PAYLOAD_SA) /* 33 */
894 if (sa->is_initiator)
896 ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
897 first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
901 ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
902 first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
905 else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
907 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
909 sa->i_id.type = id->id_type;
910 vec_free (sa->i_id.data);
911 vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
913 else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
915 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
917 sa->r_id.type = id->id_type;
918 vec_free (sa->r_id.data);
919 vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
921 else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
923 ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
925 if (sa->is_initiator)
927 sa->r_auth.method = a->auth_method;
928 vec_free (sa->r_auth.data);
929 vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
933 sa->i_auth.method = a->auth_method;
934 vec_free (sa->i_auth.data);
935 vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
938 else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
940 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
941 if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
943 sa->initial_contact = 1;
947 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
949 ikev2_parse_vendor_payload (ikep);
951 else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
953 vec_free (first_child_sa->tsi);
954 first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
956 else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
958 vec_free (first_child_sa->tsr);
959 first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
963 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
966 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
968 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
969 sa->unsupported_cp = payload;
974 payload = ikep->nextpayload;
979 vec_free (plaintext);
983 ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa,
987 u8 payload = ike->nextpayload;
989 ike_payload_header_t *ikep;
992 sa->liveness_retries = 0;
993 ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
994 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
995 clib_host_to_net_u64 (ike->rspi), sa->iaddr.as_u32);
997 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
1000 goto cleanup_and_exit;
1002 /* process encrypted payload */
1004 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1006 ikep = (ike_payload_header_t *) & plaintext[p];
1007 plen = clib_net_to_host_u16 (ikep->length);
1009 if (plen < sizeof (ike_payload_header_t))
1010 goto cleanup_and_exit;
1012 if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1014 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
1015 if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1016 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1019 else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1021 sa->del = ikev2_parse_delete_payload (ikep);
1023 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1025 ikev2_parse_vendor_payload (ikep);
1029 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1031 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1033 sa->unsupported_cp = payload;
1038 payload = ikep->nextpayload;
1043 vec_free (plaintext);
1047 ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
1051 u8 payload = ike->nextpayload;
1054 u8 nonce[IKEV2_NONCE_SIZE];
1056 ike_payload_header_t *ikep;
1058 ikev2_notify_t *n = 0;
1059 ikev2_ts_t *tsi = 0;
1060 ikev2_ts_t *tsr = 0;
1061 ikev2_sa_proposal_t *proposal = 0;
1062 ikev2_child_sa_t *child_sa;
1064 ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received "
1065 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
1066 clib_host_to_net_u64 (ike->rspi), sa->raddr.as_u32);
1068 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
1071 goto cleanup_and_exit;
1073 /* process encrypted payload */
1075 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1077 ikep = (ike_payload_header_t *) & plaintext[p];
1078 plen = clib_net_to_host_u16 (ikep->length);
1080 if (plen < sizeof (ike_payload_header_t))
1081 goto cleanup_and_exit;
1083 else if (payload == IKEV2_PAYLOAD_SA)
1085 proposal = ikev2_parse_sa_payload (ikep);
1087 else if (payload == IKEV2_PAYLOAD_NOTIFY)
1089 n = ikev2_parse_notify_payload (ikep);
1090 if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1095 else if (payload == IKEV2_PAYLOAD_DELETE)
1097 sa->del = ikev2_parse_delete_payload (ikep);
1099 else if (payload == IKEV2_PAYLOAD_VENDOR)
1101 ikev2_parse_vendor_payload (ikep);
1103 else if (payload == IKEV2_PAYLOAD_NONCE)
1105 clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
1107 else if (payload == IKEV2_PAYLOAD_TSI)
1109 tsi = ikev2_parse_ts_payload (ikep);
1111 else if (payload == IKEV2_PAYLOAD_TSR)
1113 tsr = ikev2_parse_ts_payload (ikep);
1117 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1119 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1121 sa->unsupported_cp = payload;
1126 payload = ikep->nextpayload;
1130 if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1132 ikev2_rekey_t *rekey = &sa->rekey[0];
1133 rekey->protocol_id = proposal->protocol_id;
1135 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1136 rekey->i_proposal->spi = rekey->spi;
1137 rekey->r_proposal = proposal;
1141 vec_free (sa->r_nonce);
1142 vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1143 child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1146 child_sa->rekey_retries = 0;
1151 ikev2_rekey_t *rekey;
1152 child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1155 ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1157 goto cleanup_and_exit;
1159 vec_add2 (sa->rekey, rekey, 1);
1160 rekey->protocol_id = n->protocol_id;
1161 rekey->spi = n->spi;
1162 rekey->i_proposal = proposal;
1164 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1168 vec_free (sa->i_nonce);
1169 vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1170 /* generate new Nr */
1171 vec_free (sa->r_nonce);
1172 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
1173 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1177 vec_free (plaintext);
1182 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1190 ikev2_sa_transform_t *tr_prf;
1193 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1199 nonce = sa->i_nonce;
1200 packet_data = sa->last_sa_init_res_packet_data;
1206 nonce = sa->r_nonce;
1207 packet_data = sa->last_sa_init_req_packet_data;
1210 data = vec_new (u8, 4);
1212 vec_append (data, id->data);
1214 u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1215 vec_append (authmsg, packet_data);
1216 vec_append (authmsg, nonce);
1217 vec_append (authmsg, id_hash);
1225 ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
1227 if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1228 ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1229 ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1230 ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1237 ikev2_sa_match_ts (ikev2_sa_t * sa)
1239 ikev2_main_t *km = &ikev2_main;
1241 ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1245 pool_foreach (p, km->profiles, ({
1247 if (sa->is_initiator)
1261 if (p->rem_id.type != id->type ||
1262 vec_len(p->rem_id.data) != vec_len(id->data) ||
1263 memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
1266 vec_foreach(ts, sa->childs[0].tsi)
1268 if (ikev2_ts_cmp(p_tsi, ts))
1270 vec_add1 (tsi, ts[0]);
1275 vec_foreach(ts, sa->childs[0].tsr)
1277 if (ikev2_ts_cmp(p_tsr, ts))
1279 vec_add1 (tsr, ts[0]);
1290 vec_free (sa->childs[0].tsi);
1291 vec_free (sa->childs[0].tsr);
1292 sa->childs[0].tsi = tsi;
1293 sa->childs[0].tsr = tsr;
1299 ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
1304 ikev2_sa_auth (ikev2_sa_t * sa)
1306 ikev2_main_t *km = &ikev2_main;
1307 ikev2_profile_t *p, *sel_p = 0;
1308 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1309 ikev2_sa_transform_t *tr_prf;
1312 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1314 /* only shared key and rsa signature */
1315 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1316 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1318 ikev2_elog_uint (IKEV2_LOG_ERROR,
1319 "unsupported authentication method %u",
1321 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1325 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1326 authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1329 ikev2_auth_t *sa_auth;
1331 if (sa->is_initiator)
1334 sa_auth = &sa->r_auth;
1339 sa_auth = &sa->i_auth;
1343 pool_foreach (p, km->profiles, ({
1346 if (p->rem_id.type != sa_id->type ||
1347 vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
1348 memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
1351 if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1353 if (!p->auth.data ||
1354 p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1357 psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1358 auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1360 if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
1362 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1369 else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1371 if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1374 if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
1376 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1389 sa->udp_encap = sel_p->udp_encap;
1390 sa->dst_port = sel_p->dst_port;
1394 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1396 if (!sa->is_initiator)
1398 vec_free (sa->r_id.data);
1399 sa->r_id.data = vec_dup (sel_p->loc_id.data);
1400 sa->r_id.type = sel_p->loc_id.type;
1402 /* generate our auth data */
1403 authmsg = ikev2_sa_generate_authmsg (sa, 1);
1404 if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1406 sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1407 sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1409 else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1411 sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1412 sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1416 /* select transforms for 1st child sa */
1417 ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1418 sa->childs[0].r_proposals =
1419 ikev2_select_proposal (sa->childs[0].i_proposals,
1420 IKEV2_PROTOCOL_ESP);
1422 if (~0 != sel_p->tun_itf)
1424 sa->is_tun_itf_set = 1;
1425 sa->tun_itf = sel_p->tun_itf;
1431 ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1432 "profile found! ispi %lx", sa->ispi);
1433 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1441 ikev2_sa_auth_init (ikev2_sa_t * sa)
1443 ikev2_main_t *km = &ikev2_main;
1444 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1445 ikev2_sa_transform_t *tr_prf;
1448 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1450 /* only shared key and rsa signature */
1451 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1452 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1454 ikev2_elog_uint (IKEV2_LOG_ERROR,
1455 "unsupported authentication method %u",
1457 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1461 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1462 authmsg = ikev2_sa_generate_authmsg (sa, 0);
1463 psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1464 auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1467 if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1469 sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1470 sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1472 else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1474 sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1475 sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1485 ikev2_mk_local_sa_id (u32 sai, u32 ci, u32 ti)
1487 return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1491 ikev2_mk_remote_sa_id (u32 sai, u32 ci, u32 ti)
1493 return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1503 ipsec_sa_flags_t flags;
1506 ipsec_crypto_alg_t encr_type;
1507 ipsec_integ_alg_t integ_type;
1508 ip46_address_t local_ip;
1509 ip46_address_t remote_ip;
1510 ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1512 u32 old_remote_sa_id;
1514 } ikev2_add_ipsec_tunnel_args_t;
1517 ikev2_add_tunnel_from_main (ikev2_add_ipsec_tunnel_args_t * a)
1519 ikev2_main_t *km = &ikev2_main;
1523 if (~0 == a->sw_if_index)
1525 /* no tunnel associated with the SA/profile - create a new one */
1526 rv = ipip_add_tunnel (IPIP_TRANSPORT_IP4, ~0,
1527 &a->local_ip, &a->remote_ip, 0,
1528 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
1529 TUNNEL_MODE_P2P, &sw_if_index);
1531 if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
1533 if (hash_get (km->sw_if_indices, sw_if_index))
1534 /* interface is managed by IKE; proceed with updating SAs */
1537 hash_set1 (km->sw_if_indices, sw_if_index);
1541 sw_if_index = a->sw_if_index;
1542 vnet_sw_interface_admin_up (vnet_get_main (), sw_if_index);
1547 ikev2_elog_peers (IKEV2_LOG_ERROR, "installing ipip tunnel failed! "
1548 "loc:%d.%d.%d.%d rem:%d.%d.%d.%d",
1549 a->local_ip.ip4.as_u32, a->remote_ip.ip4.as_u32);
1554 vec_add1 (sas_in, a->remote_sa_id);
1557 ipsec_tun_protect_del (sw_if_index, NULL);
1559 /* replace local SA immediately */
1560 ipsec_sa_unlock_id (a->local_sa_id);
1562 /* keep the old sa */
1563 vec_add1 (sas_in, a->old_remote_sa_id);
1566 rv |= ipsec_sa_add_and_lock (a->local_sa_id,
1568 IPSEC_PROTOCOL_ESP, a->encr_type,
1569 &a->loc_ckey, a->integ_type, &a->loc_ikey,
1570 a->flags, 0, a->salt_local, &a->local_ip,
1571 &a->remote_ip, NULL, a->dst_port);
1572 rv |= ipsec_sa_add_and_lock (a->remote_sa_id, a->remote_spi,
1573 IPSEC_PROTOCOL_ESP, a->encr_type, &a->rem_ckey,
1574 a->integ_type, &a->rem_ikey,
1575 (a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0,
1576 a->salt_remote, &a->remote_ip,
1577 &a->local_ip, NULL, a->dst_port);
1579 rv |= ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
1583 ikev2_create_tunnel_interface (vnet_main_t * vnm,
1586 ikev2_child_sa_t * child, u32 sa_index,
1587 u32 child_index, u8 is_rekey)
1589 ikev2_main_t *km = &ikev2_main;
1590 ipsec_crypto_alg_t encr_type;
1591 ipsec_integ_alg_t integ_type;
1592 ikev2_profile_t *p = 0;
1593 ikev2_sa_transform_t *tr;
1594 ikev2_sa_proposal_t *proposals;
1596 ikev2_add_ipsec_tunnel_args_t a;
1598 clib_memset (&a, 0, sizeof (a));
1600 if (!child->r_proposals)
1602 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1606 if (sa->is_initiator)
1608 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1609 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1610 proposals = child->i_proposals;
1611 a.local_spi = child->r_proposals[0].spi;
1612 a.remote_spi = child->i_proposals[0].spi;
1616 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1617 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1618 proposals = child->r_proposals;
1619 a.local_spi = child->i_proposals[0].spi;
1620 a.remote_spi = child->r_proposals[0].spi;
1623 a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
1626 a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
1627 a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
1629 a.is_rekey = is_rekey;
1631 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
1632 if (tr && tr->esn_type)
1633 a.flags |= IPSEC_SA_FLAG_USE_ESN;
1635 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1638 if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
1640 switch (tr->key_len)
1643 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1646 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1649 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1652 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1657 else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
1660 switch (tr->key_len)
1663 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
1666 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
1669 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
1672 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1680 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1686 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1689 a.encr_type = encr_type;
1693 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1696 switch (tr->integ_type)
1698 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
1699 integ_type = IPSEC_INTEG_ALG_SHA_256_128;
1701 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
1702 integ_type = IPSEC_INTEG_ALG_SHA_384_192;
1704 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
1705 integ_type = IPSEC_INTEG_ALG_SHA_512_256;
1707 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
1708 integ_type = IPSEC_INTEG_ALG_SHA1_96;
1711 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1717 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1723 integ_type = IPSEC_INTEG_ALG_NONE;
1726 a.integ_type = integ_type;
1727 ikev2_calc_child_keys (sa, child);
1729 if (sa->is_initiator)
1731 ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
1732 ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
1733 ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
1734 ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
1737 a.salt_remote = child->salt_er;
1738 a.salt_local = child->salt_ei;
1743 ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
1744 ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
1745 ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
1746 ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
1749 a.salt_remote = child->salt_ei;
1750 a.salt_local = child->salt_er;
1754 if (sa->is_profile_index_set)
1755 p = pool_elt_at_index (km->profiles, sa->profile_index);
1757 if (p && p->lifetime)
1759 child->time_to_expiration =
1760 vlib_time_now (vnm->vlib_main) + p->lifetime;
1761 if (p->lifetime_jitter)
1763 // This is not much better than rand(3), which Coverity warns
1764 // is unsuitable for security applications; random_u32 is
1765 // however fast. If this perturbance to the expiration time
1766 // needs to use a better RNG then we may need to use something
1767 // like /dev/urandom which has significant overhead.
1768 u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
1769 rnd = random_u32 (&rnd);
1771 child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
1775 if (thread_index & 0xffffffc0)
1776 ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
1778 if (child_index & 0xfffff000 || sa_index & 0xfffff000)
1779 ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
1781 child->local_sa_id =
1783 ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
1785 u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
1790 /* create a new remote SA ID to keep the old SA for a bit longer
1791 * so the peer has some time to swap their SAs */
1793 /* use most significat bit of child index part in id */
1795 if (sa->current_remote_id_mask)
1797 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
1798 sa->current_remote_id_mask = 0;
1802 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
1803 sa->current_remote_id_mask = mask;
1804 remote_sa_id |= mask;
1806 sa->old_id_expiration = 3.0;
1807 sa->old_remote_id_present = 1;
1810 child->remote_sa_id = a.remote_sa_id = remote_sa_id;
1812 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1813 a.dst_port = sa->dst_port;
1815 vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main,
1816 (u8 *) & a, sizeof (a));
1822 ip46_address_t local_ip;
1823 ip46_address_t remote_ip;
1827 } ikev2_del_ipsec_tunnel_args_t;
1829 static_always_inline u32
1830 ikev2_flip_alternate_sa_bit (u32 id)
1839 ikev2_del_tunnel_from_main (ikev2_del_ipsec_tunnel_args_t * a)
1841 ikev2_main_t *km = &ikev2_main;
1842 ipip_tunnel_t *ipip = NULL;
1845 if (~0 == a->sw_if_index)
1848 ipip_tunnel_key_t key = {
1850 .dst = a->remote_ip,
1851 .transport = IPIP_TRANSPORT_IP4,
1856 ipip = ipip_tunnel_db_find (&key);
1860 sw_if_index = ipip->sw_if_index;
1861 hash_unset (km->sw_if_indices, ipip->sw_if_index);
1868 sw_if_index = a->sw_if_index;
1869 vnet_sw_interface_admin_down (vnet_get_main (), sw_if_index);
1872 if (~0 != sw_if_index)
1873 ipsec_tun_protect_del (sw_if_index, NULL);
1875 ipsec_sa_unlock_id (a->remote_sa_id);
1876 ipsec_sa_unlock_id (a->local_sa_id);
1877 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (a->remote_sa_id));
1880 ipip_del_tunnel (ipip->sw_if_index);
1884 ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1885 ikev2_child_sa_t * child)
1887 ikev2_del_ipsec_tunnel_args_t a;
1889 clib_memset (&a, 0, sizeof (a));
1891 if (sa->is_initiator)
1893 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1894 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1898 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1899 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1902 a.remote_sa_id = child->remote_sa_id;
1903 a.local_sa_id = child->local_sa_id;
1904 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1906 vl_api_rpc_call_main_thread (ikev2_del_tunnel_from_main, (u8 *) & a,
1912 ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
1915 ike_payload_header_t *ph;
1919 ikev2_sa_transform_t *tr_encr, *tr_integ;
1921 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1923 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1925 ikev2_payload_chain_t *chain = 0;
1926 ikev2_payload_new_chain (chain);
1928 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1930 if (sa->r_proposals == 0)
1932 ikev2_payload_add_notify (chain,
1933 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1934 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1936 else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
1938 u8 *data = vec_new (u8, 2);
1939 ikev2_sa_transform_t *tr_dh;
1941 ikev2_sa_get_td_for_type (sa->r_proposals,
1942 IKEV2_TRANSFORM_TYPE_DH);
1943 ASSERT (tr_dh && tr_dh->dh_type);
1945 data[0] = (tr_dh->dh_type >> 8) & 0xff;
1946 data[1] = (tr_dh->dh_type) & 0xff;
1948 ikev2_payload_add_notify (chain,
1949 IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1952 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1954 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1956 u8 *data = vec_new (u8, 1);
1958 data[0] = sa->unsupported_cp;
1959 ikev2_payload_add_notify (chain,
1960 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1966 ike->rspi = clib_host_to_net_u64 (sa->rspi);
1967 ikev2_payload_add_sa (chain, sa->r_proposals);
1968 ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1969 ikev2_payload_add_nonce (chain, sa->r_nonce);
1972 else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1974 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1976 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1977 ikev2_payload_add_auth (chain, &sa->r_auth);
1978 ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1979 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1980 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1982 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
1984 ikev2_payload_add_notify (chain,
1985 IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1987 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1989 else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
1991 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1993 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1994 ikev2_payload_add_auth (chain, &sa->r_auth);
1996 else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
1998 ikev2_payload_add_notify (chain,
1999 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2000 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
2001 ikev2_payload_add_auth (chain, &sa->r_auth);
2002 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2003 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2005 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2007 u8 *data = vec_new (u8, 1);
2009 data[0] = sa->unsupported_cp;
2010 ikev2_payload_add_notify (chain,
2011 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2015 else if (sa->state == IKEV2_STATE_SA_INIT)
2017 ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
2018 ikev2_payload_add_auth (chain, &sa->i_auth);
2019 ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2020 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2021 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2022 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2027 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2031 else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2033 /* if pending delete */
2036 if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2038 if (sa->is_initiator)
2039 ikev2_payload_add_delete (chain, sa->del);
2041 /* The response to a request that deletes the IKE SA is an empty
2042 INFORMATIONAL response. */
2043 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2045 /* The response to a request that deletes ESP or AH SAs will contain
2046 delete payloads for the paired SAs going in the other direction. */
2049 ikev2_payload_add_delete (chain, sa->del);
2054 /* received N(AUTHENTICATION_FAILED) */
2055 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2057 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2060 /* received unsupported critical payload */
2061 else if (sa->unsupported_cp)
2063 u8 *data = vec_new (u8, 1);
2065 data[0] = sa->unsupported_cp;
2066 ikev2_payload_add_notify (chain,
2067 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2070 sa->unsupported_cp = 0;
2072 /* else send empty response */
2074 else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2076 if (sa->is_initiator)
2079 ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2080 ikev2_notify_t notify;
2081 u8 *data = vec_new (u8, 4);
2082 clib_memset (¬ify, 0, sizeof (notify));
2083 notify.protocol_id = IKEV2_PROTOCOL_ESP;
2084 notify.spi = sa->childs[0].i_proposals->spi;
2085 *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2087 ikev2_payload_add_sa (chain, proposals);
2088 ikev2_payload_add_nonce (chain, sa->i_nonce);
2089 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2090 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2091 ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2100 ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
2101 ikev2_payload_add_nonce (chain, sa->r_nonce);
2102 ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
2104 ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
2106 vec_del1 (sa->rekey, 0);
2108 else if (sa->unsupported_cp)
2110 u8 *data = vec_new (u8, 1);
2112 data[0] = sa->unsupported_cp;
2113 ikev2_payload_add_notify (chain,
2114 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2117 sa->unsupported_cp = 0;
2121 ikev2_payload_add_notify (chain,
2122 IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2129 ike->version = IKE_VERSION_2;
2130 ike->nextpayload = IKEV2_PAYLOAD_SK;
2131 tlen = sizeof (*ike);
2132 if (sa->is_initiator)
2134 ike->flags = IKEV2_HDR_FLAG_INITIATOR;
2135 sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
2139 ike->flags = IKEV2_HDR_FLAG_RESPONSE;
2143 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2145 tlen += vec_len (chain->data);
2146 ike->nextpayload = chain->first_payload_type;
2147 ike->length = clib_host_to_net_u32 (tlen);
2148 clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2150 /* store whole IKE payload - needed for PSK auth */
2151 vec_free (sa->last_sa_init_res_packet_data);
2152 vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
2157 ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2160 plen = sizeof (*ph);
2161 ph = (ike_payload_header_t *) & ike->payload[0];
2162 ph->nextpayload = chain->first_payload_type;
2164 int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
2167 /* add space for hmac */
2168 plen += tr_integ->key_trunc;
2171 /* payload and total length */
2172 ph->length = clib_host_to_net_u16 (plen);
2173 ike->length = clib_host_to_net_u32 (tlen);
2175 /* calc integrity data for whole packet except hash itself */
2177 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2178 (u8 *) ike, tlen - tr_integ->key_trunc);
2180 clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2181 sizeof (*ike), integ, tr_integ->key_trunc);
2183 /* store whole IKE payload - needed for retransmit */
2184 vec_free (sa->last_res_packet_data);
2185 vec_add (sa->last_res_packet_data, ike, tlen);
2189 ikev2_payload_destroy_chain (chain);
2195 ikev2_retransmit_sa_init (ike_header_t * ike,
2196 ip4_address_t iaddr, ip4_address_t raddr)
2198 ikev2_main_t *km = &ikev2_main;
2200 u32 thread_index = vlib_get_thread_index ();
2203 pool_foreach (sa, km->per_thread_data[thread_index].sas, ({
2204 if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
2205 sa->iaddr.as_u32 == iaddr.as_u32 &&
2206 sa->raddr.as_u32 == raddr.as_u32)
2209 u32 len = clib_net_to_host_u32(ike->length);
2210 u8 payload = ike->nextpayload;
2212 while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
2213 ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
2214 u32 plen = clib_net_to_host_u16(ikep->length);
2216 if (plen < sizeof(ike_payload_header_t))
2219 if (payload == IKEV2_PAYLOAD_NONCE)
2221 if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
2223 /* req is retransmit */
2224 if (sa->state == IKEV2_STATE_SA_INIT)
2227 tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
2228 ike->ispi = tmp->ispi;
2229 ike->rspi = tmp->rspi;
2230 ike->nextpayload = tmp->nextpayload;
2231 ike->version = tmp->version;
2232 ike->exchange = tmp->exchange;
2233 ike->flags = tmp->flags;
2234 ike->msgid = tmp->msgid;
2235 ike->length = tmp->length;
2236 clib_memcpy_fast(ike->payload, tmp->payload,
2237 clib_net_to_host_u32(tmp->length) - sizeof(*ike));
2238 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2239 "ispi %lx IKE_SA_INIT retransmit "
2240 "from %d.%d.%d.%d to %d.%d.%d.%d",
2242 raddr.as_u32, iaddr.as_u32);
2245 /* else ignore req */
2248 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2249 "ispi %lx IKE_SA_INIT ignore "
2250 "from %d.%d.%d.%d to %d.%d.%d.%d",
2252 raddr.as_u32, iaddr.as_u32);
2257 payload = ikep->nextpayload;
2264 /* req is not retransmit */
2269 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2271 u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2274 if (msg_id > sa->last_msg_id)
2276 sa->last_msg_id = msg_id;
2279 /* retransmitted req */
2280 else if (msg_id == sa->last_msg_id)
2283 tmp = (ike_header_t *) sa->last_res_packet_data;
2284 ike->ispi = tmp->ispi;
2285 ike->rspi = tmp->rspi;
2286 ike->nextpayload = tmp->nextpayload;
2287 ike->version = tmp->version;
2288 ike->exchange = tmp->exchange;
2289 ike->flags = tmp->flags;
2290 ike->msgid = tmp->msgid;
2291 ike->length = tmp->length;
2292 clib_memcpy_fast (ike->payload, tmp->payload,
2293 clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
2294 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2295 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2298 /* old req ignore */
2301 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2302 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2308 ikev2_init_sa (ikev2_sa_t * sa)
2310 ikev2_main_t *km = &ikev2_main;
2311 sa->liveness_period_check =
2312 vlib_time_now (km->vlib_main) + km->liveness_period;
2316 ikev2_node_fn (vlib_main_t * vm,
2317 vlib_node_runtime_t * node, vlib_frame_t * frame)
2319 u32 n_left_from, *from, *to_next;
2320 ikev2_next_t next_index;
2321 ikev2_main_t *km = &ikev2_main;
2322 u32 thread_index = vlib_get_thread_index ();
2324 from = vlib_frame_vector_args (frame);
2325 n_left_from = frame->n_vectors;
2326 next_index = node->cached_next_index;
2328 while (n_left_from > 0)
2332 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2334 while (n_left_from > 0 && n_left_to_next > 0)
2338 u32 next0 = IKEV2_NEXT_ERROR_DROP;
2343 ikev2_sa_t *sa0 = 0;
2344 ikev2_sa_t sa; /* temporary store for SA */
2348 /* speculatively enqueue b0 to the current next frame */
2354 n_left_to_next -= 1;
2356 b0 = vlib_get_buffer (vm, bi0);
2357 ike0 = vlib_buffer_get_current (b0);
2358 vlib_buffer_advance (b0, -sizeof (*udp0));
2359 udp0 = vlib_buffer_get_current (b0);
2360 vlib_buffer_advance (b0, -sizeof (*ip40));
2361 ip40 = vlib_buffer_get_current (b0);
2363 if (ike0->version != IKE_VERSION_2)
2365 vlib_node_increment_counter (vm, ikev2_node.index,
2366 IKEV2_ERROR_NOT_IKEV2, 1);
2370 if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2373 clib_memset (sa0, 0, sizeof (*sa0));
2375 if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
2377 if (ike0->rspi == 0)
2379 sa0->raddr.as_u32 = ip40->dst_address.as_u32;
2380 sa0->iaddr.as_u32 = ip40->src_address.as_u32;
2382 r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2386 vlib_node_increment_counter (vm, ikev2_node.index,
2387 IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2389 len = clib_net_to_host_u32 (ike0->length);
2394 vlib_node_increment_counter (vm, ikev2_node.index,
2395 IKEV2_ERROR_IKE_SA_INIT_IGNORE,
2400 ikev2_process_sa_init_req (vm, sa0, ike0);
2402 if (sa0->state == IKEV2_STATE_SA_INIT)
2404 ikev2_sa_free_proposal_vector (&sa0->r_proposals);
2406 ikev2_select_proposal (sa0->i_proposals,
2407 IKEV2_PROTOCOL_IKE);
2408 ikev2_generate_sa_init_data (sa0);
2411 if (sa0->state == IKEV2_STATE_SA_INIT
2412 || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2414 len = ikev2_generate_message (sa0, ike0, 0);
2417 if (sa0->state == IKEV2_STATE_SA_INIT)
2419 /* add SA to the pool */
2420 pool_get (km->per_thread_data[thread_index].sas,
2422 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2423 ikev2_init_sa (sa0);
2425 per_thread_data[thread_index].sa_by_rspi,
2428 km->per_thread_data[thread_index].sas);
2432 ikev2_sa_free_all_vec (sa0);
2436 else //received sa_init without initiator flag
2438 ikev2_process_sa_init_resp (vm, sa0, ike0);
2440 if (sa0->state == IKEV2_STATE_SA_INIT)
2442 ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
2443 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2447 pool_elt_at_index (km->sais, p[0]);
2449 if (sai->init_response_received)
2451 /* we've already processed sa-init response */
2452 sa0->state = IKEV2_STATE_UNKNOWN;
2456 ikev2_complete_sa_data (sa0, sai);
2457 ikev2_calc_keys (sa0);
2458 ikev2_sa_auth_init (sa0);
2459 len = ikev2_generate_message (sa0, ike0, 0);
2464 if (sa0->state == IKEV2_STATE_SA_INIT)
2466 /* add SA to the pool */
2467 pool_get (km->per_thread_data[thread_index].sas, sa0);
2468 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2469 hash_set (km->per_thread_data[thread_index].sa_by_rspi,
2471 sa0 - km->per_thread_data[thread_index].sas);
2475 ikev2_sa_free_all_vec (sa0);
2479 else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2482 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2483 clib_net_to_host_u64 (ike0->rspi));
2487 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2490 r = ikev2_retransmit_resp (sa0, ike0);
2493 vlib_node_increment_counter (vm, ikev2_node.index,
2494 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2496 len = clib_net_to_host_u32 (ike0->length);
2501 vlib_node_increment_counter (vm, ikev2_node.index,
2502 IKEV2_ERROR_IKE_REQ_IGNORE,
2507 ikev2_process_auth_req (vm, sa0, ike0);
2508 ikev2_sa_auth (sa0);
2509 if (sa0->state == IKEV2_STATE_AUTHENTICATED)
2511 ikev2_initial_contact_cleanup (sa0);
2512 ikev2_sa_match_ts (sa0);
2513 if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
2514 ikev2_create_tunnel_interface (km->vnet_main,
2520 if (sa0->is_initiator)
2522 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2526 pool_elt_at_index (km->sais, p[0]);
2527 hash_unset (km->sa_by_ispi, sai->ispi);
2528 ikev2_sa_free_all_vec (sai);
2529 pool_put (km->sais, sai);
2534 len = ikev2_generate_message (sa0, ike0, 0);
2538 else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2541 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2542 clib_net_to_host_u64 (ike0->rspi));
2546 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2549 r = ikev2_retransmit_resp (sa0, ike0);
2552 vlib_node_increment_counter (vm, ikev2_node.index,
2553 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2555 len = clib_net_to_host_u32 (ike0->length);
2560 vlib_node_increment_counter (vm, ikev2_node.index,
2561 IKEV2_ERROR_IKE_REQ_IGNORE,
2566 ikev2_process_informational_req (vm, sa0, ike0);
2569 if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
2571 ikev2_delete_t *d, *tmp, *resp = 0;
2572 vec_foreach (d, sa0->del)
2574 ikev2_child_sa_t *ch_sa;
2575 ch_sa = ikev2_sa_get_child (sa0, d->spi,
2577 !sa0->is_initiator);
2580 ikev2_delete_tunnel_interface (km->vnet_main,
2582 if (!sa0->is_initiator)
2584 vec_add2 (resp, tmp, 1);
2585 tmp->protocol_id = d->protocol_id;
2586 tmp->spi = ch_sa->r_proposals[0].spi;
2588 ikev2_sa_del_child_sa (sa0, ch_sa);
2591 if (!sa0->is_initiator)
2593 vec_free (sa0->del);
2598 if (!(ike0->flags & IKEV2_HDR_FLAG_RESPONSE))
2600 ike0->flags |= IKEV2_HDR_FLAG_RESPONSE;
2601 len = ikev2_generate_message (sa0, ike0, 0);
2605 else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2608 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2609 clib_net_to_host_u64 (ike0->rspi));
2613 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2616 r = ikev2_retransmit_resp (sa0, ike0);
2619 vlib_node_increment_counter (vm, ikev2_node.index,
2620 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2622 len = clib_net_to_host_u32 (ike0->length);
2627 vlib_node_increment_counter (vm, ikev2_node.index,
2628 IKEV2_ERROR_IKE_REQ_IGNORE,
2633 ikev2_process_create_child_sa_req (vm, sa0, ike0);
2636 if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
2639 vec_free (sa0->childs);
2640 ikev2_child_sa_t *child;
2641 vec_add2 (sa0->childs, child, 1);
2642 child->r_proposals = sa0->rekey[0].r_proposal;
2643 child->i_proposals = sa0->rekey[0].i_proposal;
2644 child->tsi = sa0->rekey[0].tsi;
2645 child->tsr = sa0->rekey[0].tsr;
2646 ikev2_create_tunnel_interface (km->vnet_main,
2649 child - sa0->childs,
2652 if (sa0->is_initiator)
2654 vec_del1 (sa0->rekey, 0);
2658 len = ikev2_generate_message (sa0, ike0, 0);
2665 ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2666 "received from %d.%d.%d.%d to %d.%d.%d.%d",
2668 ip40->src_address.as_u32,
2669 ip40->dst_address.as_u32);
2673 /* if we are sending packet back, rewrite headers */
2676 next0 = IKEV2_NEXT_IP4_LOOKUP;
2677 if (sa0->is_initiator)
2679 ip40->dst_address.as_u32 = sa0->raddr.as_u32;
2680 ip40->src_address.as_u32 = sa0->iaddr.as_u32;
2684 ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
2685 ip40->src_address.as_u32 = sa0->raddr.as_u32;
2688 clib_host_to_net_u16 (len + sizeof (udp_header_t));
2690 b0->current_length =
2691 len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2692 ip40->length = clib_host_to_net_u16 (b0->current_length);
2693 ip40->checksum = ip4_header_checksum (ip40);
2696 if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
2697 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
2699 ikev2_child_sa_t *c;
2701 vec_foreach (c, sa0->childs)
2702 ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
2704 ikev2_delete_sa (sa0);
2706 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2708 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2709 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2711 ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
2712 t->sw_if_index = sw_if_index0;
2713 t->next_index = next0;
2716 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2717 n_left_to_next, bi0, next0);
2720 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2723 vlib_node_increment_counter (vm, ikev2_node.index,
2724 IKEV2_ERROR_PROCESSED, frame->n_vectors);
2725 return frame->n_vectors;
2729 VLIB_REGISTER_NODE (ikev2_node,static) = {
2730 .function = ikev2_node_fn,
2732 .vector_size = sizeof (u32),
2733 .format_trace = format_ikev2_trace,
2734 .type = VLIB_NODE_TYPE_INTERNAL,
2736 .n_errors = ARRAY_LEN(ikev2_error_strings),
2737 .error_strings = ikev2_error_strings,
2739 .n_next_nodes = IKEV2_N_NEXT,
2742 [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
2743 [IKEV2_NEXT_ERROR_DROP] = "error-drop",
2748 // set ikev2 proposals when vpp is used as initiator
2749 static clib_error_t *
2750 ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
2751 ikev2_transforms_set * ts,
2752 ikev2_sa_proposal_t ** proposals, int is_ike)
2755 ikev2_main_t *km = &ikev2_main;
2756 ikev2_sa_proposal_t *proposal;
2757 vec_add2 (*proposals, proposal, 1);
2758 ikev2_sa_transform_t *td;
2763 vec_foreach (td, km->supported_transforms)
2765 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
2766 && td->encr_type == ts->crypto_alg
2767 && td->key_len == ts->crypto_key_size / 8)
2770 attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
2771 attr[1] = clib_host_to_net_u16 (td->key_len << 3);
2772 vec_add (td->attrs, (u8 *) attr, 4);
2773 vec_add1 (proposal->transforms, *td);
2782 r = clib_error_return (0, "Unsupported algorithm");
2786 if (is_ike || IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16 != ts->crypto_alg)
2790 vec_foreach (td, km->supported_transforms)
2792 if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
2793 && td->integ_type == ts->integ_alg)
2795 vec_add1 (proposal->transforms, *td);
2803 ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
2804 r = clib_error_return (0, "Unsupported algorithm");
2813 vec_foreach (td, km->supported_transforms)
2815 if (td->type == IKEV2_TRANSFORM_TYPE_PRF
2816 && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
2818 vec_add1 (proposal->transforms, *td);
2825 r = clib_error_return (0, "Unsupported algorithm");
2831 if (is_ike || ts->dh_type != IKEV2_TRANSFORM_DH_TYPE_NONE)
2834 vec_foreach (td, km->supported_transforms)
2836 if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
2838 vec_add1 (proposal->transforms, *td);
2841 sa->dh_group = td->dh_type;
2849 r = clib_error_return (0, "Unsupported algorithm");
2857 vec_foreach (td, km->supported_transforms)
2859 if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
2861 vec_add1 (proposal->transforms, *td);
2868 r = clib_error_return (0, "Unsupported algorithm");
2877 static ikev2_profile_t *
2878 ikev2_profile_index_by_name (u8 * name)
2880 ikev2_main_t *km = &ikev2_main;
2883 p = mhash_get (&km->profile_index_by_name, name);
2887 return pool_elt_at_index (km->profiles, p[0]);
2892 ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst,
2901 b0 = vlib_get_buffer (vm, bi0);
2902 vlib_buffer_advance (b0, -sizeof (udp_header_t));
2903 udp0 = vlib_buffer_get_current (b0);
2904 vlib_buffer_advance (b0, -sizeof (ip4_header_t));
2905 ip40 = vlib_buffer_get_current (b0);
2908 ip40->ip_version_and_header_length = 0x45;
2910 ip40->fragment_id = 0;
2911 ip40->flags_and_fragment_offset = 0;
2913 ip40->protocol = IP_PROTOCOL_UDP;
2914 ip40->dst_address.as_u32 = dst->as_u32;
2915 ip40->src_address.as_u32 = src->as_u32;
2916 udp0->dst_port = clib_host_to_net_u16 (500);
2917 udp0->src_port = clib_host_to_net_u16 (500);
2918 udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
2920 b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2921 ip40->length = clib_host_to_net_u16 (b0->current_length);
2922 ip40->checksum = ip4_header_checksum (ip40);
2925 /* send the request */
2926 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
2927 to_next = vlib_frame_vector_args (f);
2930 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
2935 ikev2_get_new_ike_header_buff (vlib_main_t * vm, ike_header_t ** ike)
2938 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2943 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
2944 *ike = vlib_buffer_get_current (b0);
2949 ikev2_set_local_key (vlib_main_t * vm, u8 * file)
2951 ikev2_main_t *km = &ikev2_main;
2953 km->pkey = ikev2_load_key_file (file);
2954 if (km->pkey == NULL)
2955 return clib_error_return (0, "load key '%s' failed", file);
2960 static_always_inline vnet_api_error_t
2961 ikev2_register_udp_port (ikev2_profile_t * p, u16 port)
2963 ikev2_main_t *km = &ikev2_main;
2964 udp_dst_port_info_t *pi;
2966 uword *v = hash_get (km->udp_ports, port);
2967 pi = udp_get_dst_port_info (&udp_main, port, UDP_IP4);
2971 /* IKE already uses this port, only increment reference counter */
2978 return VNET_API_ERROR_UDP_PORT_TAKEN;
2980 udp_register_dst_port (km->vlib_main, port,
2981 ipsec4_tun_input_node.index, 1);
2982 hash_set (km->udp_ports, port, 1);
2988 static_always_inline void
2989 ikev2_unregister_udp_port (ikev2_profile_t * p)
2991 ikev2_main_t *km = &ikev2_main;
2994 if (p->dst_port == IPSEC_UDP_PORT_NONE)
2997 v = hash_get (km->udp_ports, p->dst_port);
3005 udp_unregister_dst_port (km->vlib_main, p->dst_port, 1);
3006 hash_unset (km->udp_ports, p->dst_port);
3009 p->dst_port = IPSEC_UDP_PORT_NONE;
3013 ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
3015 ikev2_main_t *km = &ikev2_main;
3020 if (ikev2_profile_index_by_name (name))
3021 return clib_error_return (0, "policy %v already exists", name);
3023 pool_get (km->profiles, p);
3024 clib_memset (p, 0, sizeof (*p));
3025 p->name = vec_dup (name);
3026 p->dst_port = IPSEC_UDP_PORT_NONE;
3027 p->responder.sw_if_index = ~0;
3029 uword index = p - km->profiles;
3030 mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
3034 p = ikev2_profile_index_by_name (name);
3036 return clib_error_return (0, "policy %v does not exists", name);
3038 ikev2_unregister_udp_port (p);
3041 pool_put (km->profiles, p);
3042 mhash_unset (&km->profile_index_by_name, name, 0);
3048 ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
3049 u8 * auth_data, u8 data_hex_format)
3054 p = ikev2_profile_index_by_name (name);
3058 r = clib_error_return (0, "unknown profile %v", name);
3061 vec_free (p->auth.data);
3062 p->auth.method = auth_method;
3063 p->auth.data = vec_dup (auth_data);
3064 p->auth.hex = data_hex_format;
3066 if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
3068 vec_add1 (p->auth.data, 0);
3070 EVP_PKEY_free (p->auth.key);
3071 p->auth.key = ikev2_load_cert_file (auth_data);
3072 if (p->auth.key == NULL)
3073 return clib_error_return (0, "load cert '%s' failed", auth_data);
3080 ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
3086 if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
3087 && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
3089 r = clib_error_return (0, "unsupported identity type %U",
3090 format_ikev2_id_type, id_type);
3094 p = ikev2_profile_index_by_name (name);
3098 r = clib_error_return (0, "unknown profile %v", name);
3104 vec_free (p->loc_id.data);
3105 p->loc_id.type = id_type;
3106 p->loc_id.data = vec_dup (data);
3110 vec_free (p->rem_id.data);
3111 p->rem_id.type = id_type;
3112 p->rem_id.data = vec_dup (data);
3119 ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
3120 u16 start_port, u16 end_port, ip4_address_t start_addr,
3121 ip4_address_t end_addr, int is_local)
3126 p = ikev2_profile_index_by_name (name);
3130 r = clib_error_return (0, "unknown profile %v", name);
3136 p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
3137 p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
3138 p->loc_ts.start_port = start_port;
3139 p->loc_ts.end_port = end_port;
3140 p->loc_ts.protocol_id = protocol_id;
3141 p->loc_ts.ts_type = 7;
3145 p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
3146 p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
3147 p->rem_ts.start_port = start_port;
3148 p->rem_ts.end_port = end_port;
3149 p->rem_ts.protocol_id = protocol_id;
3150 p->rem_ts.ts_type = 7;
3158 ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
3159 u32 sw_if_index, ip4_address_t ip4)
3164 p = ikev2_profile_index_by_name (name);
3168 r = clib_error_return (0, "unknown profile %v", name);
3172 p->responder.sw_if_index = sw_if_index;
3173 p->responder.ip4 = ip4;
3179 ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
3180 ikev2_transform_encr_type_t crypto_alg,
3181 ikev2_transform_integ_type_t integ_alg,
3182 ikev2_transform_dh_type_t dh_type,
3183 u32 crypto_key_size)
3188 p = ikev2_profile_index_by_name (name);
3192 r = clib_error_return (0, "unknown profile %v", name);
3196 p->ike_ts.crypto_alg = crypto_alg;
3197 p->ike_ts.integ_alg = integ_alg;
3198 p->ike_ts.dh_type = dh_type;
3199 p->ike_ts.crypto_key_size = crypto_key_size;
3204 ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
3205 ikev2_transform_encr_type_t crypto_alg,
3206 ikev2_transform_integ_type_t integ_alg,
3207 ikev2_transform_dh_type_t dh_type,
3208 u32 crypto_key_size)
3213 p = ikev2_profile_index_by_name (name);
3217 r = clib_error_return (0, "unknown profile %v", name);
3221 p->esp_ts.crypto_alg = crypto_alg;
3222 p->esp_ts.integ_alg = integ_alg;
3223 p->esp_ts.dh_type = dh_type;
3224 p->esp_ts.crypto_key_size = crypto_key_size;
3229 ikev2_set_profile_tunnel_interface (vlib_main_t * vm,
3230 u8 * name, u32 sw_if_index)
3235 p = ikev2_profile_index_by_name (name);
3239 r = clib_error_return (0, "unknown profile %v", name);
3243 p->tun_itf = sw_if_index;
3249 ikev2_set_profile_ipsec_udp_port (vlib_main_t * vm, u8 * name, u16 port,
3252 ikev2_profile_t *p = ikev2_profile_index_by_name (name);
3253 ikev2_main_t *km = &ikev2_main;
3254 vnet_api_error_t rv = 0;
3258 return VNET_API_ERROR_INVALID_VALUE;
3262 if (p->dst_port != IPSEC_UDP_PORT_NONE)
3263 return VNET_API_ERROR_VALUE_EXIST;
3265 rv = ikev2_register_udp_port (p, port);
3269 v = hash_get (km->udp_ports, port);
3271 return VNET_API_ERROR_IKE_NO_PORT;
3273 if (p->dst_port == IPSEC_UDP_PORT_NONE)
3274 return VNET_API_ERROR_INVALID_VALUE;
3276 ikev2_unregister_udp_port (p);
3282 ikev2_set_profile_udp_encap (vlib_main_t * vm, u8 * name)
3284 ikev2_profile_t *p = ikev2_profile_index_by_name (name);
3289 r = clib_error_return (0, "unknown profile %v", name);
3298 ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
3299 u64 lifetime, u32 jitter, u32 handover,
3305 p = ikev2_profile_index_by_name (name);
3309 r = clib_error_return (0, "unknown profile %v", name);
3313 p->lifetime = lifetime;
3314 p->lifetime_jitter = jitter;
3315 p->handover = handover;
3316 p->lifetime_maxdata = maxdata;
3321 ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
3325 ip4_main_t *im = &ip4_main;
3326 ikev2_main_t *km = &ikev2_main;
3328 p = ikev2_profile_index_by_name (name);
3332 r = clib_error_return (0, "unknown profile %v", name);
3336 if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
3338 r = clib_error_return (0, "responder not set for profile %v", name);
3343 /* Create the Initiator Request */
3347 ip_lookup_main_t *lm = &im->lookup_main;
3349 int len = sizeof (ike_header_t);
3351 /* Get own iface IP */
3353 lm->if_address_pool_index_by_sw_if_index[p->responder.sw_if_index];
3354 ip_interface_address_t *if_add =
3355 pool_elt_at_index (lm->if_address_pool, if_add_index0);
3356 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
3358 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3360 /* Prepare the SA and the IKE payload */
3362 clib_memset (&sa, 0, sizeof (ikev2_sa_t));
3363 ikev2_payload_chain_t *chain = 0;
3364 ikev2_payload_new_chain (chain);
3366 /* Build the IKE proposal payload */
3367 ikev2_sa_proposal_t *proposals = 0;
3368 ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
3369 proposals[0].proposal_num = 1;
3370 proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
3372 /* Add and then cleanup proposal data */
3373 ikev2_payload_add_sa (chain, proposals);
3374 ikev2_sa_free_proposal_vector (&proposals);
3376 sa.is_initiator = 1;
3377 sa.profile_index = p - km->profiles;
3378 sa.is_profile_index_set = 1;
3379 sa.state = IKEV2_STATE_SA_INIT;
3380 sa.tun_itf = p->tun_itf;
3381 sa.udp_encap = p->udp_encap;
3382 sa.dst_port = p->dst_port;
3383 sa.is_tun_itf_set = 1;
3384 sa.initial_contact = 1;
3385 ikev2_generate_sa_init_data (&sa);
3386 ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
3387 ikev2_payload_add_nonce (chain, sa.i_nonce);
3389 /* Build the child SA proposal */
3390 vec_resize (sa.childs, 1);
3391 ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
3392 &sa.childs[0].i_proposals, 0);
3393 sa.childs[0].i_proposals[0].proposal_num = 1;
3394 sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
3395 RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
3396 sizeof (sa.childs[0].i_proposals[0].spi));
3400 /* Add NAT detection notification messages (mandatory) */
3401 u8 nat_detection_source[8 + 8 + 4 + 2];
3402 u8 *nat_detection_sha1 = vec_new (u8, 20);
3404 u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
3405 clib_memcpy_fast (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
3406 tmpspi = clib_host_to_net_u64 (sa.rspi);
3407 clib_memcpy_fast (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
3408 u16 tmpport = clib_host_to_net_u16 (500);
3409 clib_memcpy_fast (&nat_detection_source[8 + 8 + 4], &tmpport,
3411 u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
3412 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3413 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3414 nat_detection_sha1);
3415 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
3416 nat_detection_sha1);
3417 tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
3418 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3419 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3420 nat_detection_sha1);
3421 ikev2_payload_add_notify (chain,
3422 IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
3423 nat_detection_sha1);
3424 vec_free (nat_detection_sha1);
3426 u8 *sig_hash_algo = vec_new (u8, 8);
3427 u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
3428 clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
3429 ikev2_payload_add_notify (chain,
3430 IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
3432 vec_free (sig_hash_algo);
3435 /* Buffer update and boilerplate */
3436 len += vec_len (chain->data);
3437 ike0->nextpayload = chain->first_payload_type;
3438 ike0->length = clib_host_to_net_u32 (len);
3439 clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
3440 ikev2_payload_destroy_chain (chain);
3442 ike0->version = IKE_VERSION_2;
3443 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3444 ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
3445 ike0->ispi = sa.ispi;
3449 /* store whole IKE payload - needed for PSK auth */
3450 vec_free (sa.last_sa_init_req_packet_data);
3451 vec_add (sa.last_sa_init_req_packet_data, ike0, len);
3453 /* add data to the SA then add it to the pool */
3454 sa.iaddr.as_u32 = if_ip->as_u32;
3455 sa.raddr.as_u32 = p->responder.ip4.as_u32;
3456 sa.i_id.type = p->loc_id.type;
3457 sa.i_id.data = vec_dup (p->loc_id.data);
3458 sa.i_auth.method = p->auth.method;
3459 sa.i_auth.hex = p->auth.hex;
3460 sa.i_auth.data = vec_dup (p->auth.data);
3461 vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
3462 vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
3464 ikev2_initial_contact_cleanup (&sa);
3466 /* add SA to the pool */
3467 ikev2_sa_t *sa0 = 0;
3468 pool_get (km->sais, sa0);
3469 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3470 hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
3472 ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
3474 ikev2_elog_exchange ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to "
3475 "%d.%d.%d.%d", clib_host_to_net_u64 (sa0->ispi), 0,
3476 p->responder.ip4.as_u32);
3483 ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3484 ikev2_child_sa_t * csa)
3486 /* Create the Initiator notification for child SA removal */
3487 ikev2_main_t *km = &ikev2_main;
3492 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3495 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3496 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3497 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3498 vec_resize (sa->del, 1);
3499 sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
3500 sa->del->spi = csa->i_proposals->spi;
3501 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3502 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3503 len = ikev2_generate_message (sa, ike0, 0);
3505 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3507 /* delete local child SA */
3508 ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
3509 ikev2_sa_del_child_sa (sa, csa);
3513 ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
3516 ikev2_main_t *km = &ikev2_main;
3517 ikev2_main_per_thread_data_t *tkm;
3518 ikev2_sa_t *fsa = 0;
3519 ikev2_child_sa_t *fchild = 0;
3521 /* Search for the child SA */
3522 vec_foreach (tkm, km->per_thread_data)
3528 pool_foreach (sa, tkm->sas, ({
3529 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3539 if (!fchild || !fsa)
3541 r = clib_error_return (0, "Child SA not found");
3546 ikev2_delete_child_sa_internal (vm, fsa, fchild);
3553 ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
3556 ikev2_main_t *km = &ikev2_main;
3557 ikev2_main_per_thread_data_t *tkm;
3558 ikev2_sa_t *fsa = 0;
3559 ikev2_main_per_thread_data_t *ftkm = 0;
3561 /* Search for the IKE SA */
3562 vec_foreach (tkm, km->per_thread_data)
3568 pool_foreach (sa, tkm->sas, ({
3569 if (sa->ispi == ispi)
3581 r = clib_error_return (0, "IKE SA not found");
3586 /* Create the Initiator notification for IKE SA removal */
3592 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3595 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3596 ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
3597 ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
3598 vec_resize (fsa->del, 1);
3599 fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3600 fsa->del->spi = ispi;
3601 ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
3602 fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3603 len = ikev2_generate_message (fsa, ike0, 0);
3605 ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
3609 /* delete local SA */
3610 ikev2_child_sa_t *c;
3611 vec_foreach (c, fsa->childs)
3613 ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
3614 ikev2_sa_del_child_sa (fsa, c);
3616 ikev2_sa_free_all_vec (fsa);
3617 uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
3620 hash_unset (ftkm->sa_by_rspi, fsa->rspi);
3621 pool_put (ftkm->sas, fsa);
3629 ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3630 ikev2_child_sa_t * csa)
3632 /* Create the Initiator request for create child SA */
3638 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3641 ike0->version = IKE_VERSION_2;
3642 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3643 ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
3644 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3645 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3646 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3647 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3649 ikev2_rekey_t *rekey;
3650 vec_add2 (sa->rekey, rekey, 1);
3651 ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
3654 RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
3655 rekey->spi = proposals[0].spi;
3656 rekey->ispi = csa->i_proposals->spi;
3657 len = ikev2_generate_message (sa, ike0, proposals);
3658 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3659 vec_free (proposals);
3663 ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
3666 ikev2_main_t *km = &ikev2_main;
3667 ikev2_main_per_thread_data_t *tkm;
3668 ikev2_sa_t *fsa = 0;
3669 ikev2_child_sa_t *fchild = 0;
3671 /* Search for the child SA */
3672 vec_foreach (tkm, km->per_thread_data)
3678 pool_foreach (sa, tkm->sas, ({
3679 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3689 if (!fchild || !fsa)
3691 r = clib_error_return (0, "Child SA not found");
3696 ikev2_rekey_child_sa_internal (vm, fsa, fchild);
3703 ikev2_init (vlib_main_t * vm)
3705 ikev2_main_t *km = &ikev2_main;
3706 vlib_thread_main_t *tm = vlib_get_thread_main ();
3709 clib_memset (km, 0, sizeof (ikev2_main_t));
3710 km->vnet_main = vnet_get_main ();
3713 km->liveness_period = IKEV2_LIVENESS_PERIOD_CHECK;
3714 km->liveness_max_retries = IKEV2_LIVENESS_RETRIES;
3715 ikev2_crypto_init (km);
3717 mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
3719 vec_validate_aligned (km->per_thread_data, tm->n_vlib_mains - 1,
3720 CLIB_CACHE_LINE_BYTES);
3721 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
3723 km->per_thread_data[thread_id].sa_by_rspi =
3724 hash_create (0, sizeof (uword));
3727 km->sa_by_ispi = hash_create (0, sizeof (uword));
3728 km->sw_if_indices = hash_create (0, 0);
3729 km->udp_ports = hash_create (0, sizeof (uword));
3731 udp_register_dst_port (vm, 500, ikev2_node.index, 1);
3733 ikev2_cli_reference ();
3735 km->log_level = IKEV2_LOG_ERROR;
3736 km->log_class = vlib_log_register_class ("ikev2", 0);
3741 VLIB_INIT_FUNCTION (ikev2_init) =
3743 .runs_after = VLIB_INITS("ipsec_init"),
3748 ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa,
3751 ikev2_main_t *km = &ikev2_main;
3752 ikev2_profile_t *p = 0;
3753 vlib_main_t *vm = km->vlib_main;
3754 f64 now = vlib_time_now (vm);
3757 if (sa->is_profile_index_set)
3758 p = pool_elt_at_index (km->profiles, sa->profile_index);
3760 if (sa->is_initiator && p && csa->time_to_expiration
3761 && now > csa->time_to_expiration)
3763 if (!csa->is_expired || csa->rekey_retries > 0)
3765 ikev2_rekey_child_sa_internal (vm, sa, csa);
3766 csa->time_to_expiration = now + p->handover;
3767 csa->is_expired = 1;
3768 if (csa->rekey_retries == 0)
3770 csa->rekey_retries = 5;
3772 else if (csa->rekey_retries > 0)
3774 csa->rekey_retries--;
3775 ikev2_log_debug ("Rekeying Child SA 0x%x, retries left %d",
3776 csa->i_proposals->spi, csa->rekey_retries);
3777 if (csa->rekey_retries == 0)
3779 csa->rekey_retries = -1;
3786 csa->time_to_expiration = 0;
3787 ikev2_delete_child_sa_internal (vm, sa, csa);
3794 ipip_tunnel_t *ipip = NULL;
3795 u32 sw_if_index = sa->is_tun_itf_set ? sa->tun_itf : ~0;
3796 if (~0 == sw_if_index)
3798 ip46_address_t local_ip;
3799 ip46_address_t remote_ip;
3800 if (sa->is_initiator)
3802 ip46_address_set_ip4 (&local_ip, &sa->iaddr);
3803 ip46_address_set_ip4 (&remote_ip, &sa->raddr);
3807 ip46_address_set_ip4 (&local_ip, &sa->raddr);
3808 ip46_address_set_ip4 (&remote_ip, &sa->iaddr);
3812 ipip_tunnel_key_t key = {
3815 .transport = IPIP_TRANSPORT_IP4,
3820 ipip = ipip_tunnel_db_find (&key);
3823 sw_if_index = ipip->sw_if_index;
3829 vec_add1 (sas_in, csa->remote_sa_id);
3830 ipsec_tun_protect_update (sw_if_index, NULL, csa->local_sa_id, sas_in);
3831 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (csa->remote_sa_id));
3838 ikev2_set_log_level (ikev2_log_level_t log_level)
3840 ikev2_main_t *km = &ikev2_main;
3842 if (log_level >= IKEV2_LOG_MAX)
3844 ikev2_log_error ("unknown logging level %d", log_level);
3848 km->log_level = log_level;
3853 ikev2_set_liveness_params (u32 period, u32 max_retries)
3855 ikev2_main_t *km = &ikev2_main;
3857 if (period == 0 || max_retries == 0)
3858 return clib_error_return (0, "invalid args");
3860 km->liveness_period = period;
3861 km->liveness_max_retries = max_retries;
3866 ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
3868 ikev2_main_t *km = &ikev2_main;
3869 vlib_main_t *vm = km->vlib_main;
3870 ikev2_main_per_thread_data_t *tkm;
3871 ikev2_sa_t *fsa = 0;
3872 ikev2_profile_t *p = 0;
3873 ikev2_child_sa_t *fchild = 0;
3874 f64 now = vlib_time_now (vm);
3875 vlib_counter_t counts;
3877 /* Search for the SA and child SA */
3878 vec_foreach (tkm, km->per_thread_data)
3884 pool_foreach (sa, tkm->sas, ({
3885 fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
3894 vlib_get_combined_counter (&ipsec_sa_counters,
3895 ipsec_sa->stat_index, &counts);
3897 if (fsa && fsa->is_profile_index_set)
3898 p = pool_elt_at_index (km->profiles, fsa->profile_index);
3900 if (fchild && p && p->lifetime_maxdata)
3902 if (!fchild->is_expired && counts.bytes > p->lifetime_maxdata)
3904 fchild->time_to_expiration = now;
3910 ikev2_process_pending_sa_init (ikev2_main_t * km)
3917 hash_foreach (ispi, sai, km->sa_by_ispi,
3919 sa = pool_elt_at_index (km->sais, sai);
3920 if (sa->init_response_received)
3924 if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
3927 vlib_buffer_t * b = vlib_get_buffer (km->vlib_main, bi0);
3928 clib_memcpy_fast (vlib_buffer_get_current (b),
3929 sa->last_sa_init_req_packet_data,
3930 vec_len (sa->last_sa_init_req_packet_data));
3931 ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0,
3932 vec_len (sa->last_sa_init_req_packet_data));
3937 static vlib_node_registration_t ikev2_mngr_process_node;
3940 ikev2_send_informational_request (ikev2_sa_t * sa)
3942 ikev2_main_t *km = &ikev2_main;
3943 ip4_address_t *src, *dst;
3948 bi0 = ikev2_get_new_ike_header_buff (km->vlib_main, &ike0);
3950 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3951 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3952 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3953 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3954 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3955 len = ikev2_generate_message (sa, ike0, 0);
3957 if (sa->is_initiator)
3969 ikev2_send_ike (km->vlib_main, src, dst, bi0, len);
3972 static_always_inline int
3973 ikev2_mngr_process_responder_sas (ikev2_sa_t * sa)
3975 ikev2_main_t *km = &ikev2_main;
3976 vlib_main_t *vm = km->vlib_main;
3978 if (!sa->sk_ai || !sa->sk_ar)
3981 if (sa->liveness_retries >= km->liveness_max_retries)
3984 f64 now = vlib_time_now (vm);
3986 if (sa->liveness_period_check < now)
3988 sa->liveness_retries++;
3989 sa->liveness_period_check = now + km->liveness_period;
3990 ikev2_send_informational_request (sa);
3996 ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
3999 ikev2_main_t *km = &ikev2_main;
4000 ipsec_main_t *im = &ipsec_main;
4002 ikev2_child_sa_t *c;
4008 vlib_process_wait_for_event_or_clock (vm, 1);
4009 vlib_process_get_events (vm, NULL);
4011 /* process ike child sas */
4012 ikev2_main_per_thread_data_t *tkm;
4013 vec_foreach (tkm, km->per_thread_data)
4016 u32 *to_be_deleted = 0;
4019 pool_foreach (sa, tkm->sas, ({
4020 ikev2_child_sa_t *c;
4022 if (sa->old_remote_id_present && 0 > sa->old_id_expiration)
4024 sa->old_remote_id_present = 0;
4028 sa->old_id_expiration -= 1;
4030 vec_foreach (c, sa->childs)
4032 req_sent |= ikev2_mngr_process_child_sa(sa, c, del_old_ids);
4035 if (ikev2_mngr_process_responder_sas (sa))
4036 vec_add1 (to_be_deleted, sa - tkm->sas);
4040 vec_foreach (sai, to_be_deleted)
4042 sa = pool_elt_at_index (tkm->sas, sai[0]);
4043 if (sa->is_initiator && sa->is_profile_index_set)
4045 p = pool_elt_at_index (km->profiles, sa->profile_index);
4048 ikev2_initiate_sa_init (vm, p->name);
4052 vec_foreach (c, sa->childs)
4053 ikev2_delete_tunnel_interface (km->vnet_main, sa, c);
4054 hash_unset (tkm->sa_by_rspi, sa->rspi);
4055 pool_put (tkm->sas, sa);
4059 /* process ipsec sas */
4062 pool_foreach (sa, im->sad, ({
4063 ikev2_mngr_process_ipsec_sa(sa);
4067 ikev2_process_pending_sa_init (km);
4071 vlib_process_wait_for_event_or_clock (vm, 5);
4072 vlib_process_get_events (vm, NULL);
4081 VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
4082 .function = ikev2_mngr_process_fn,
4083 .type = VLIB_NODE_TYPE_PROCESS,
4085 "ikev2-manager-process",
4088 VLIB_PLUGIN_REGISTER () = {
4089 .version = VPP_BUILD_VER,
4090 .description = "Internet Key Exchange (IKEv2) Protocol",
4095 * fd.io coding-style-patch-verification: ON
4098 * eval: (c-set-style "gnu")