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 ikev2_main_t ikev2_main;
34 static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
36 ikev2_child_sa_t * child);
38 #define ikev2_set_state(sa, v) do { \
40 ikev2_elog_sa_state("ispi %lx SA state changed to " #v, sa->ispi); \
50 format_ikev2_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
56 s = format (s, "ikev2: sw_if_index %d, next index %d",
57 t->sw_if_index, t->next_index);
61 static vlib_node_registration_t ikev2_node;
63 #define foreach_ikev2_error \
64 _(PROCESSED, "IKEv2 packets processed") \
65 _(IKE_SA_INIT_RETRANSMIT, "IKE_SA_INIT retransmit ") \
66 _(IKE_SA_INIT_IGNORE, "IKE_SA_INIT ignore (IKE SA already auth)") \
67 _(IKE_REQ_RETRANSMIT, "IKE request retransmit") \
68 _(IKE_REQ_IGNORE, "IKE request ignore (old msgid)") \
69 _(NOT_IKEV2, "Non IKEv2 packets received")
73 #define _(sym,str) IKEV2_ERROR_##sym,
79 static char *ikev2_error_strings[] = {
80 #define _(sym,string) string,
87 IKEV2_NEXT_IP4_LOOKUP,
88 IKEV2_NEXT_ERROR_DROP,
92 static ikev2_sa_transform_t *
93 ikev2_find_transform_data (ikev2_sa_transform_t * t)
95 ikev2_main_t *km = &ikev2_main;
96 ikev2_sa_transform_t *td;
98 vec_foreach (td, km->supported_transforms)
100 if (td->type != t->type)
103 if (td->transform_id != t->transform_id)
106 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
108 if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
109 || t->attrs[1] != 14)
112 if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
120 static ikev2_sa_proposal_t *
121 ikev2_select_proposal (ikev2_sa_proposal_t * proposals,
122 ikev2_protocol_id_t prot_id)
124 ikev2_sa_proposal_t *rv = 0;
125 ikev2_sa_proposal_t *proposal;
126 ikev2_sa_transform_t *transform, *new_t;
127 u8 mandatory_bitmap, optional_bitmap;
129 if (prot_id == IKEV2_PROTOCOL_IKE)
131 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
132 (1 << IKEV2_TRANSFORM_TYPE_PRF) |
133 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
134 optional_bitmap = mandatory_bitmap;
136 else if (prot_id == IKEV2_PROTOCOL_ESP)
138 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
139 (1 << IKEV2_TRANSFORM_TYPE_ESN);
140 optional_bitmap = mandatory_bitmap |
141 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
143 else if (prot_id == IKEV2_PROTOCOL_AH)
145 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
146 (1 << IKEV2_TRANSFORM_TYPE_ESN);
147 optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
152 vec_add2 (rv, proposal, 1);
154 vec_foreach (proposal, proposals)
157 if (proposal->protocol_id != prot_id)
160 vec_foreach (transform, proposal->transforms)
162 if ((1 << transform->type) & bitmap)
165 if (ikev2_find_transform_data (transform))
167 bitmap |= 1 << transform->type;
168 vec_add2 (rv->transforms, new_t, 1);
169 clib_memcpy_fast (new_t, transform, sizeof (*new_t));
170 new_t->attrs = vec_dup (transform->attrs);
174 if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
175 (bitmap & ~optional_bitmap) == 0)
177 rv->proposal_num = proposal->proposal_num;
178 rv->protocol_id = proposal->protocol_id;
179 RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
184 vec_free (rv->transforms);
193 ikev2_sa_transform_t *
194 ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
195 ikev2_transform_type_t type)
197 ikev2_sa_transform_t *t;
202 vec_foreach (t, p->transforms)
205 return ikev2_find_transform_data (t);
211 ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id,
215 vec_foreach (c, sa->childs)
217 ikev2_sa_proposal_t *proposal =
218 by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
219 if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
227 ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v)
229 ikev2_sa_proposal_t *p;
230 ikev2_sa_transform_t *t;
237 vec_foreach (t, p->transforms)
241 vec_free (p->transforms);
247 ikev2_sa_free_child_sa (ikev2_child_sa_t * c)
249 ikev2_sa_free_proposal_vector (&c->r_proposals);
250 ikev2_sa_free_proposal_vector (&c->i_proposals);
260 ikev2_sa_free_all_child_sa (ikev2_child_sa_t ** childs)
263 vec_foreach (c, *childs) ikev2_sa_free_child_sa (c);
269 ikev2_sa_del_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * child)
271 ikev2_sa_free_child_sa (child);
272 vec_del1 (sa->childs, child - sa->childs);
276 ikev2_sa_free_all_vec (ikev2_sa_t * sa)
278 vec_free (sa->i_nonce);
279 vec_free (sa->i_dh_data);
280 vec_free (sa->dh_shared_key);
281 vec_free (sa->dh_private_key);
283 ikev2_sa_free_proposal_vector (&sa->r_proposals);
284 ikev2_sa_free_proposal_vector (&sa->i_proposals);
287 vec_free (sa->sk_ai);
288 vec_free (sa->sk_ar);
289 vec_free (sa->sk_ei);
290 vec_free (sa->sk_er);
291 vec_free (sa->sk_pi);
292 vec_free (sa->sk_pr);
294 vec_free (sa->i_id.data);
295 vec_free (sa->i_auth.data);
296 vec_free (sa->r_id.data);
297 vec_free (sa->r_auth.data);
299 EVP_PKEY_free (sa->r_auth.key);
303 ikev2_sa_free_all_child_sa (&sa->childs);
307 ikev2_delete_sa (ikev2_sa_t * sa)
309 ikev2_main_t *km = &ikev2_main;
310 u32 thread_index = vlib_get_thread_index ();
313 ikev2_sa_free_all_vec (sa);
315 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
318 hash_unset (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
319 pool_put (km->per_thread_data[thread_index].sas, sa);
324 ikev2_generate_sa_init_data (ikev2_sa_t * sa)
326 ikev2_sa_transform_t *t = 0, *t2;
327 ikev2_main_t *km = &ikev2_main;
329 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
334 /* check if received DH group is on our list of supported groups */
335 vec_foreach (t2, km->supported_transforms)
337 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
346 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
350 if (sa->is_initiator)
353 RAND_bytes ((u8 *) & sa->ispi, 8);
356 sa->i_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
357 RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
362 RAND_bytes ((u8 *) & sa->rspi, 8);
365 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
366 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
369 /* generate dh keys */
370 ikev2_generate_dh (sa, t);
375 ikev2_complete_sa_data (ikev2_sa_t * sa, ikev2_sa_t * sai)
377 ikev2_sa_transform_t *t = 0, *t2;
378 ikev2_main_t *km = &ikev2_main;
381 /*move some data to the new SA */
382 #define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
383 sa->i_nonce = _(sai->i_nonce);
384 sa->i_dh_data = _(sai->i_dh_data);
385 sa->dh_private_key = _(sai->dh_private_key);
386 sa->iaddr.as_u32 = sai->iaddr.as_u32;
387 sa->raddr.as_u32 = sai->raddr.as_u32;
388 sa->is_initiator = sai->is_initiator;
389 sa->i_id.type = sai->i_id.type;
390 sa->profile_index = sai->profile_index;
391 sa->is_profile_index_set = sai->is_profile_index_set;
392 sa->tun_itf = sai->tun_itf;
393 sa->is_tun_itf_set = sai->is_tun_itf_set;
394 sa->i_id.data = _(sai->i_id.data);
395 sa->i_auth.method = sai->i_auth.method;
396 sa->i_auth.hex = sai->i_auth.hex;
397 sa->i_auth.data = _(sai->i_auth.data);
398 sa->i_auth.key = _(sai->i_auth.key);
399 sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data);
400 sa->childs = _(sai->childs);
401 sa->udp_encap = sai->udp_encap;
402 sa->dst_port = sai->dst_port;
406 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
411 /* check if received DH group is on our list of supported groups */
412 vec_foreach (t2, km->supported_transforms)
414 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
423 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
428 /* generate dh keys */
429 ikev2_complete_dh (sa, t);
434 ikev2_calc_keys (ikev2_sa_t * sa)
437 /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
440 u16 integ_key_len = 0;
441 ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
443 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
445 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
447 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
450 integ_key_len = tr_integ->key_len;
452 vec_append (s, sa->i_nonce);
453 vec_append (s, sa->r_nonce);
454 skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
456 /* Calculate S = Ni | Nr | SPIi | SPIr */
458 vec_add2 (s, tmp, 2 * sizeof (*spi));
460 spi[0] = clib_host_to_net_u64 (sa->ispi);
461 spi[1] = clib_host_to_net_u64 (sa->rspi);
463 /* calculate PRFplus */
465 int len = tr_prf->key_trunc + /* SK_d */
466 integ_key_len * 2 + /* SK_ai, SK_ar */
467 tr_encr->key_len * 2 + /* SK_ei, SK_er */
468 tr_prf->key_len * 2; /* SK_pi, SK_pr */
470 keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
477 sa->sk_d = vec_new (u8, tr_prf->key_trunc);
478 clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
479 pos += tr_prf->key_trunc;
484 sa->sk_ai = vec_new (u8, integ_key_len);
485 clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
486 pos += integ_key_len;
489 sa->sk_ar = vec_new (u8, integ_key_len);
490 clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
491 pos += integ_key_len;
495 sa->sk_ei = vec_new (u8, tr_encr->key_len);
496 clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len);
497 pos += tr_encr->key_len;
500 sa->sk_er = vec_new (u8, tr_encr->key_len);
501 clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len);
502 pos += tr_encr->key_len;
505 sa->sk_pi = vec_new (u8, tr_prf->key_len);
506 clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
507 pos += tr_prf->key_len;
510 sa->sk_pr = vec_new (u8, tr_prf->key_len);
511 clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
512 pos += tr_prf->key_len;
518 ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child)
521 u16 integ_key_len = 0;
524 ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
526 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
528 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
530 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
533 integ_key_len = ctr_integ->key_len;
535 salt_len = sizeof (u32);
537 vec_append (s, sa->i_nonce);
538 vec_append (s, sa->r_nonce);
539 /* calculate PRFplus */
541 int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
543 keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
548 child->sk_ei = vec_new (u8, ctr_encr->key_len);
549 clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
550 pos += ctr_encr->key_len;
555 child->sk_ai = vec_new (u8, ctr_integ->key_len);
556 clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
557 pos += ctr_integ->key_len;
561 clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
566 child->sk_er = vec_new (u8, ctr_encr->key_len);
567 clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
568 pos += ctr_encr->key_len;
573 child->sk_ar = vec_new (u8, integ_key_len);
574 clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
575 pos += integ_key_len;
579 clib_memcpy (&child->salt_er, keymat + pos, salt_len);
589 ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
593 u32 len = clib_net_to_host_u32 (ike->length);
594 u8 payload = ike->nextpayload;
596 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
598 clib_net_to_host_u64 (ike->ispi),
599 clib_net_to_host_u64 (ike->rspi), sa->iaddr.as_u32);
601 sa->ispi = clib_net_to_host_u64 (ike->ispi);
603 /* store whole IKE payload - needed for PSK auth */
604 vec_free (sa->last_sa_init_req_packet_data);
605 vec_add (sa->last_sa_init_req_packet_data, ike, len);
607 while (p < len && payload != IKEV2_PAYLOAD_NONE)
609 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
610 u32 plen = clib_net_to_host_u16 (ikep->length);
612 if (plen < sizeof (ike_payload_header_t))
615 if (payload == IKEV2_PAYLOAD_SA)
617 ikev2_sa_free_proposal_vector (&sa->i_proposals);
618 sa->i_proposals = ikev2_parse_sa_payload (ikep);
620 else if (payload == IKEV2_PAYLOAD_KE)
622 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
623 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
624 vec_free (sa->i_dh_data);
625 vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
627 else if (payload == IKEV2_PAYLOAD_NONCE)
629 vec_free (sa->i_nonce);
630 vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
632 else if (payload == IKEV2_PAYLOAD_NOTIFY)
634 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
637 else if (payload == IKEV2_PAYLOAD_VENDOR)
639 ikev2_parse_vendor_payload (ikep);
643 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
645 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
647 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
648 sa->unsupported_cp = payload;
653 payload = ikep->nextpayload;
657 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
661 ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa,
665 u32 len = clib_net_to_host_u32 (ike->length);
666 u8 payload = ike->nextpayload;
668 sa->ispi = clib_net_to_host_u64 (ike->ispi);
669 sa->rspi = clib_net_to_host_u64 (ike->rspi);
671 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
672 "from %d.%d.%d.%d", sa->ispi, sa->rspi,
675 /* store whole IKE payload - needed for PSK auth */
676 vec_free (sa->last_sa_init_res_packet_data);
677 vec_add (sa->last_sa_init_res_packet_data, ike, len);
679 while (p < len && payload != IKEV2_PAYLOAD_NONE)
681 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
682 u32 plen = clib_net_to_host_u16 (ikep->length);
684 if (plen < sizeof (ike_payload_header_t))
687 if (payload == IKEV2_PAYLOAD_SA)
689 ikev2_sa_free_proposal_vector (&sa->r_proposals);
690 sa->r_proposals = ikev2_parse_sa_payload (ikep);
693 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
695 clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
698 else if (payload == IKEV2_PAYLOAD_KE)
700 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
701 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
702 vec_free (sa->r_dh_data);
703 vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
705 else if (payload == IKEV2_PAYLOAD_NONCE)
707 vec_free (sa->r_nonce);
708 vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
710 else if (payload == IKEV2_PAYLOAD_NOTIFY)
712 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
715 else if (payload == IKEV2_PAYLOAD_VENDOR)
717 ikev2_parse_vendor_payload (ikep);
721 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
723 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
725 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
726 sa->unsupported_cp = payload;
731 payload = ikep->nextpayload;
737 ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
742 u32 len = clib_net_to_host_u32 (ike->length);
743 ike_payload_header_t *ikep = 0;
745 ikev2_sa_transform_t *tr_integ;
747 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
750 *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
752 ikep = (ike_payload_header_t *) & ike->payload[p];
753 plen = clib_net_to_host_u16 (ikep->length);
755 if (plen < sizeof (*ikep))
758 if (*payload == IKEV2_PAYLOAD_SK)
760 last_payload = *payload;
764 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
766 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
768 sa->unsupported_cp = *payload;
773 *payload = ikep->nextpayload;
777 if (last_payload != IKEV2_PAYLOAD_SK)
779 ikev2_elog_error ("Last payload must be SK");
784 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
785 (u8 *) ike, len - tr_integ->key_trunc);
787 plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
789 if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
791 ikev2_elog_error ("message integrity check failed");
797 return ikev2_decrypt_data (sa, ikep->payload, plen);
801 ikev2_initial_contact_cleanup (ikev2_sa_t * sa)
803 ikev2_main_t *km = &ikev2_main;
807 u32 thread_index = vlib_get_thread_index ();
809 if (!sa->initial_contact)
812 /* find old IKE SAs with the same authenticated identity */
814 pool_foreach (tmp, km->per_thread_data[thread_index].sas, ({
815 if (tmp->i_id.type != sa->i_id.type ||
816 vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
817 memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
820 if (sa->rspi != tmp->rspi)
821 vec_add1(delete, tmp - km->per_thread_data[thread_index].sas);
825 for (i = 0; i < vec_len (delete); i++)
828 pool_elt_at_index (km->per_thread_data[thread_index].sas, delete[i]);
830 tmp->childs) ikev2_delete_tunnel_interface (km->vnet_main,
832 ikev2_delete_sa (tmp);
836 sa->initial_contact = 0;
840 ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
842 ikev2_child_sa_t *first_child_sa;
844 u8 payload = ike->nextpayload;
846 ike_payload_header_t *ikep;
849 ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
850 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
851 clib_host_to_net_u64 (ike->rspi),
852 sa->is_initiator ? sa->raddr.as_u32 : sa->
855 ikev2_calc_keys (sa);
857 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
861 if (sa->unsupported_cp)
862 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
863 goto cleanup_and_exit;
866 /* select or create 1st child SA */
867 if (sa->is_initiator)
869 first_child_sa = &sa->childs[0];
873 ikev2_sa_free_all_child_sa (&sa->childs);
874 vec_add2 (sa->childs, first_child_sa, 1);
878 /* process encrypted payload */
880 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
882 ikep = (ike_payload_header_t *) & plaintext[p];
883 plen = clib_net_to_host_u16 (ikep->length);
885 if (plen < sizeof (ike_payload_header_t))
886 goto cleanup_and_exit;
888 if (payload == IKEV2_PAYLOAD_SA) /* 33 */
890 if (sa->is_initiator)
892 ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
893 first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
897 ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
898 first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
901 else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
903 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
905 sa->i_id.type = id->id_type;
906 vec_free (sa->i_id.data);
907 vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
909 else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
911 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
913 sa->r_id.type = id->id_type;
914 vec_free (sa->r_id.data);
915 vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
917 else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
919 ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
921 if (sa->is_initiator)
923 sa->r_auth.method = a->auth_method;
924 vec_free (sa->r_auth.data);
925 vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
929 sa->i_auth.method = a->auth_method;
930 vec_free (sa->i_auth.data);
931 vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
934 else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
936 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
937 if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
939 sa->initial_contact = 1;
943 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
945 ikev2_parse_vendor_payload (ikep);
947 else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
949 vec_free (first_child_sa->tsi);
950 first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
952 else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
954 vec_free (first_child_sa->tsr);
955 first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
959 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
962 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
964 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
965 sa->unsupported_cp = payload;
970 payload = ikep->nextpayload;
975 vec_free (plaintext);
979 ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa,
983 u8 payload = ike->nextpayload;
985 ike_payload_header_t *ikep;
988 ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
989 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
990 clib_host_to_net_u64 (ike->rspi), sa->iaddr.as_u32);
992 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
995 goto cleanup_and_exit;
997 /* process encrypted payload */
999 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1001 ikep = (ike_payload_header_t *) & plaintext[p];
1002 plen = clib_net_to_host_u16 (ikep->length);
1004 if (plen < sizeof (ike_payload_header_t))
1005 goto cleanup_and_exit;
1007 if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1009 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
1010 if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1011 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1014 else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1016 sa->del = ikev2_parse_delete_payload (ikep);
1018 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1020 ikev2_parse_vendor_payload (ikep);
1024 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1026 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1028 sa->unsupported_cp = payload;
1033 payload = ikep->nextpayload;
1038 vec_free (plaintext);
1042 ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
1046 u8 payload = ike->nextpayload;
1049 u8 nonce[IKEV2_NONCE_SIZE];
1051 ike_payload_header_t *ikep;
1053 ikev2_notify_t *n = 0;
1054 ikev2_ts_t *tsi = 0;
1055 ikev2_ts_t *tsr = 0;
1056 ikev2_sa_proposal_t *proposal = 0;
1057 ikev2_child_sa_t *child_sa;
1059 ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received "
1060 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
1061 clib_host_to_net_u64 (ike->rspi), sa->raddr.as_u32);
1063 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
1066 goto cleanup_and_exit;
1068 /* process encrypted payload */
1070 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1072 ikep = (ike_payload_header_t *) & plaintext[p];
1073 plen = clib_net_to_host_u16 (ikep->length);
1075 if (plen < sizeof (ike_payload_header_t))
1076 goto cleanup_and_exit;
1078 else if (payload == IKEV2_PAYLOAD_SA)
1080 proposal = ikev2_parse_sa_payload (ikep);
1082 else if (payload == IKEV2_PAYLOAD_NOTIFY)
1084 n = ikev2_parse_notify_payload (ikep);
1085 if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1090 else if (payload == IKEV2_PAYLOAD_DELETE)
1092 sa->del = ikev2_parse_delete_payload (ikep);
1094 else if (payload == IKEV2_PAYLOAD_VENDOR)
1096 ikev2_parse_vendor_payload (ikep);
1098 else if (payload == IKEV2_PAYLOAD_NONCE)
1100 clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
1102 else if (payload == IKEV2_PAYLOAD_TSI)
1104 tsi = ikev2_parse_ts_payload (ikep);
1106 else if (payload == IKEV2_PAYLOAD_TSR)
1108 tsr = ikev2_parse_ts_payload (ikep);
1112 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1114 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1116 sa->unsupported_cp = payload;
1121 payload = ikep->nextpayload;
1125 if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1127 ikev2_rekey_t *rekey = &sa->rekey[0];
1128 rekey->protocol_id = proposal->protocol_id;
1130 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1131 rekey->i_proposal->spi = rekey->spi;
1132 rekey->r_proposal = proposal;
1136 vec_free (sa->r_nonce);
1137 vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1138 child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1141 child_sa->rekey_retries = 0;
1146 ikev2_rekey_t *rekey;
1147 child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1150 ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1152 goto cleanup_and_exit;
1154 vec_add2 (sa->rekey, rekey, 1);
1155 rekey->protocol_id = n->protocol_id;
1156 rekey->spi = n->spi;
1157 rekey->i_proposal = proposal;
1159 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1163 vec_free (sa->i_nonce);
1164 vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1165 /* generate new Nr */
1166 vec_free (sa->r_nonce);
1167 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
1168 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1172 vec_free (plaintext);
1177 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1185 ikev2_sa_transform_t *tr_prf;
1188 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1194 nonce = sa->i_nonce;
1195 packet_data = sa->last_sa_init_res_packet_data;
1201 nonce = sa->r_nonce;
1202 packet_data = sa->last_sa_init_req_packet_data;
1205 data = vec_new (u8, 4);
1207 vec_append (data, id->data);
1209 u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1210 vec_append (authmsg, packet_data);
1211 vec_append (authmsg, nonce);
1212 vec_append (authmsg, id_hash);
1220 ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
1222 if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1223 ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1224 ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1225 ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1232 ikev2_sa_match_ts (ikev2_sa_t * sa)
1234 ikev2_main_t *km = &ikev2_main;
1236 ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1240 pool_foreach (p, km->profiles, ({
1242 if (sa->is_initiator)
1256 if (p->rem_id.type != id->type ||
1257 vec_len(p->rem_id.data) != vec_len(id->data) ||
1258 memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
1261 vec_foreach(ts, sa->childs[0].tsi)
1263 if (ikev2_ts_cmp(p_tsi, ts))
1265 vec_add1 (tsi, ts[0]);
1270 vec_foreach(ts, sa->childs[0].tsr)
1272 if (ikev2_ts_cmp(p_tsr, ts))
1274 vec_add1 (tsr, ts[0]);
1285 vec_free (sa->childs[0].tsi);
1286 vec_free (sa->childs[0].tsr);
1287 sa->childs[0].tsi = tsi;
1288 sa->childs[0].tsr = tsr;
1294 ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
1299 ikev2_sa_auth (ikev2_sa_t * sa)
1301 ikev2_main_t *km = &ikev2_main;
1302 ikev2_profile_t *p, *sel_p = 0;
1303 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1304 ikev2_sa_transform_t *tr_prf;
1307 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1309 /* only shared key and rsa signature */
1310 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1311 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1313 ikev2_elog_uint (IKEV2_LOG_ERROR,
1314 "unsupported authentication method %u",
1316 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1320 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1321 authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1324 ikev2_auth_t *sa_auth;
1326 if (sa->is_initiator)
1329 sa_auth = &sa->r_auth;
1334 sa_auth = &sa->i_auth;
1338 pool_foreach (p, km->profiles, ({
1341 if (p->rem_id.type != sa_id->type ||
1342 vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
1343 memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
1346 if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1348 if (!p->auth.data ||
1349 p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1352 psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1353 auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1355 if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
1357 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1364 else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1366 if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1369 if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
1371 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1384 sa->udp_encap = sel_p->udp_encap;
1385 sa->dst_port = sel_p->dst_port;
1389 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1391 if (!sa->is_initiator)
1393 vec_free (sa->r_id.data);
1394 sa->r_id.data = vec_dup (sel_p->loc_id.data);
1395 sa->r_id.type = sel_p->loc_id.type;
1397 /* generate our auth data */
1398 authmsg = ikev2_sa_generate_authmsg (sa, 1);
1399 if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1401 sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1402 sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1404 else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1406 sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1407 sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1411 /* select transforms for 1st child sa */
1412 ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1413 sa->childs[0].r_proposals =
1414 ikev2_select_proposal (sa->childs[0].i_proposals,
1415 IKEV2_PROTOCOL_ESP);
1417 if (~0 != sel_p->tun_itf)
1419 sa->is_tun_itf_set = 1;
1420 sa->tun_itf = sel_p->tun_itf;
1426 ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1427 "profile found! ispi %lx", sa->ispi);
1428 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1436 ikev2_sa_auth_init (ikev2_sa_t * sa)
1438 ikev2_main_t *km = &ikev2_main;
1439 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1440 ikev2_sa_transform_t *tr_prf;
1443 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1445 /* only shared key and rsa signature */
1446 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1447 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1449 ikev2_elog_uint (IKEV2_LOG_ERROR,
1450 "unsupported authentication method %u",
1452 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1456 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1457 authmsg = ikev2_sa_generate_authmsg (sa, 0);
1458 psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1459 auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1462 if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1464 sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1465 sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1467 else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1469 sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1470 sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1480 ikev2_mk_local_sa_id (u32 sai, u32 ci, u32 ti)
1482 return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1486 ikev2_mk_remote_sa_id (u32 sai, u32 ci, u32 ti)
1488 return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1498 ipsec_sa_flags_t flags;
1501 ipsec_crypto_alg_t encr_type;
1502 ipsec_integ_alg_t integ_type;
1503 ip46_address_t local_ip;
1504 ip46_address_t remote_ip;
1505 ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1507 u32 old_remote_sa_id;
1509 } ikev2_add_ipsec_tunnel_args_t;
1512 ikev2_add_tunnel_from_main (ikev2_add_ipsec_tunnel_args_t * a)
1514 ikev2_main_t *km = &ikev2_main;
1518 if (~0 == a->sw_if_index)
1520 /* no tunnel associated with the SA/profile - create a new one */
1521 rv = ipip_add_tunnel (IPIP_TRANSPORT_IP4, ~0,
1522 &a->local_ip, &a->remote_ip, 0,
1523 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
1524 TUNNEL_MODE_P2P, &sw_if_index);
1526 if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
1528 if (hash_get (km->sw_if_indices, sw_if_index))
1529 /* interface is managed by IKE; proceed with updating SAs */
1532 hash_set1 (km->sw_if_indices, sw_if_index);
1536 sw_if_index = a->sw_if_index;
1537 vnet_sw_interface_admin_up (vnet_get_main (), sw_if_index);
1542 ikev2_elog_peers (IKEV2_LOG_ERROR, "installing ipip tunnel failed! "
1543 "loc:%d.%d.%d.%d rem:%d.%d.%d.%d",
1544 a->local_ip.ip4.as_u32, a->remote_ip.ip4.as_u32);
1549 vec_add1 (sas_in, a->remote_sa_id);
1552 ipsec_tun_protect_del (sw_if_index, NULL);
1554 /* replace local SA immediately */
1555 ipsec_sa_unlock_id (a->local_sa_id);
1557 /* keep the old sa */
1558 vec_add1 (sas_in, a->old_remote_sa_id);
1561 rv |= ipsec_sa_add_and_lock (a->local_sa_id,
1563 IPSEC_PROTOCOL_ESP, a->encr_type,
1564 &a->loc_ckey, a->integ_type, &a->loc_ikey,
1565 a->flags, 0, a->salt_local, &a->local_ip,
1566 &a->remote_ip, NULL, a->dst_port);
1567 rv |= ipsec_sa_add_and_lock (a->remote_sa_id, a->remote_spi,
1568 IPSEC_PROTOCOL_ESP, a->encr_type, &a->rem_ckey,
1569 a->integ_type, &a->rem_ikey,
1570 (a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0,
1571 a->salt_remote, &a->remote_ip,
1572 &a->local_ip, NULL, a->dst_port);
1574 rv |= ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
1578 ikev2_create_tunnel_interface (vnet_main_t * vnm,
1581 ikev2_child_sa_t * child, u32 sa_index,
1582 u32 child_index, u8 is_rekey)
1584 ikev2_main_t *km = &ikev2_main;
1585 ipsec_crypto_alg_t encr_type;
1586 ipsec_integ_alg_t integ_type;
1587 ikev2_profile_t *p = 0;
1588 ikev2_sa_transform_t *tr;
1589 ikev2_sa_proposal_t *proposals;
1591 ikev2_add_ipsec_tunnel_args_t a;
1593 clib_memset (&a, 0, sizeof (a));
1595 if (!child->r_proposals)
1597 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1601 if (sa->is_initiator)
1603 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1604 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1605 proposals = child->i_proposals;
1606 a.local_spi = child->r_proposals[0].spi;
1607 a.remote_spi = child->i_proposals[0].spi;
1611 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1612 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1613 proposals = child->r_proposals;
1614 a.local_spi = child->i_proposals[0].spi;
1615 a.remote_spi = child->r_proposals[0].spi;
1618 a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
1621 a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
1622 a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
1624 a.is_rekey = is_rekey;
1626 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
1627 if (tr && tr->esn_type)
1628 a.flags |= IPSEC_SA_FLAG_USE_ESN;
1630 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1633 if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
1635 switch (tr->key_len)
1638 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1641 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1644 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1647 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1652 else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
1655 switch (tr->key_len)
1658 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
1661 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
1664 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
1667 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1675 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1681 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1684 a.encr_type = encr_type;
1688 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1691 switch (tr->integ_type)
1693 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
1694 integ_type = IPSEC_INTEG_ALG_SHA_256_128;
1696 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
1697 integ_type = IPSEC_INTEG_ALG_SHA_384_192;
1699 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
1700 integ_type = IPSEC_INTEG_ALG_SHA_512_256;
1702 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
1703 integ_type = IPSEC_INTEG_ALG_SHA1_96;
1706 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1712 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1718 integ_type = IPSEC_INTEG_ALG_NONE;
1721 a.integ_type = integ_type;
1722 ikev2_calc_child_keys (sa, child);
1724 if (sa->is_initiator)
1726 ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
1727 ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
1728 ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
1729 ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
1732 a.salt_remote = child->salt_er;
1733 a.salt_local = child->salt_ei;
1738 ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
1739 ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
1740 ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
1741 ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
1744 a.salt_remote = child->salt_ei;
1745 a.salt_local = child->salt_er;
1749 if (sa->is_profile_index_set)
1750 p = pool_elt_at_index (km->profiles, sa->profile_index);
1752 if (p && p->lifetime)
1754 child->time_to_expiration =
1755 vlib_time_now (vnm->vlib_main) + p->lifetime;
1756 if (p->lifetime_jitter)
1758 // This is not much better than rand(3), which Coverity warns
1759 // is unsuitable for security applications; random_u32 is
1760 // however fast. If this perturbance to the expiration time
1761 // needs to use a better RNG then we may need to use something
1762 // like /dev/urandom which has significant overhead.
1763 u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
1764 rnd = random_u32 (&rnd);
1766 child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
1770 if (thread_index & 0xffffffc0)
1771 ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
1773 if (child_index & 0xfffff000 || sa_index & 0xfffff000)
1774 ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
1776 child->local_sa_id =
1778 ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
1780 u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
1785 /* create a new remote SA ID to keep the old SA for a bit longer
1786 * so the peer has some time to swap their SAs */
1788 /* use most significat bit of child index part in id */
1790 if (sa->current_remote_id_mask)
1792 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
1793 sa->current_remote_id_mask = 0;
1797 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
1798 sa->current_remote_id_mask = mask;
1799 remote_sa_id |= mask;
1801 sa->old_id_expiration = 3.0;
1802 sa->old_remote_id_present = 1;
1805 child->remote_sa_id = a.remote_sa_id = remote_sa_id;
1807 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1808 a.dst_port = sa->dst_port;
1810 vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main,
1811 (u8 *) & a, sizeof (a));
1817 ip46_address_t local_ip;
1818 ip46_address_t remote_ip;
1822 } ikev2_del_ipsec_tunnel_args_t;
1824 static_always_inline u32
1825 ikev2_flip_alternate_sa_bit (u32 id)
1834 ikev2_del_tunnel_from_main (ikev2_del_ipsec_tunnel_args_t * a)
1836 ikev2_main_t *km = &ikev2_main;
1837 ipip_tunnel_t *ipip = NULL;
1840 if (~0 == a->sw_if_index)
1843 ipip_tunnel_key_t key = {
1845 .dst = a->remote_ip,
1846 .transport = IPIP_TRANSPORT_IP4,
1851 ipip = ipip_tunnel_db_find (&key);
1855 sw_if_index = ipip->sw_if_index;
1856 hash_unset (km->sw_if_indices, ipip->sw_if_index);
1863 sw_if_index = a->sw_if_index;
1864 vnet_sw_interface_admin_down (vnet_get_main (), sw_if_index);
1867 if (~0 != sw_if_index)
1868 ipsec_tun_protect_del (sw_if_index, NULL);
1870 ipsec_sa_unlock_id (a->remote_sa_id);
1871 ipsec_sa_unlock_id (a->local_sa_id);
1872 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (a->remote_sa_id));
1875 ipip_del_tunnel (ipip->sw_if_index);
1879 ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1880 ikev2_child_sa_t * child)
1882 ikev2_del_ipsec_tunnel_args_t a;
1884 clib_memset (&a, 0, sizeof (a));
1886 if (sa->is_initiator)
1888 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1889 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1893 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1894 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1897 a.remote_sa_id = child->remote_sa_id;
1898 a.local_sa_id = child->local_sa_id;
1899 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1901 vl_api_rpc_call_main_thread (ikev2_del_tunnel_from_main, (u8 *) & a,
1907 ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
1910 ike_payload_header_t *ph;
1914 ikev2_sa_transform_t *tr_encr, *tr_integ;
1916 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1918 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1920 ikev2_payload_chain_t *chain = 0;
1921 ikev2_payload_new_chain (chain);
1923 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1925 if (sa->r_proposals == 0)
1927 ikev2_payload_add_notify (chain,
1928 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1929 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1931 else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
1933 u8 *data = vec_new (u8, 2);
1934 ikev2_sa_transform_t *tr_dh;
1936 ikev2_sa_get_td_for_type (sa->r_proposals,
1937 IKEV2_TRANSFORM_TYPE_DH);
1938 ASSERT (tr_dh && tr_dh->dh_type);
1940 data[0] = (tr_dh->dh_type >> 8) & 0xff;
1941 data[1] = (tr_dh->dh_type) & 0xff;
1943 ikev2_payload_add_notify (chain,
1944 IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1947 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1949 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1951 u8 *data = vec_new (u8, 1);
1953 data[0] = sa->unsupported_cp;
1954 ikev2_payload_add_notify (chain,
1955 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1961 ike->rspi = clib_host_to_net_u64 (sa->rspi);
1962 ikev2_payload_add_sa (chain, sa->r_proposals);
1963 ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1964 ikev2_payload_add_nonce (chain, sa->r_nonce);
1967 else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1969 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1971 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1972 ikev2_payload_add_auth (chain, &sa->r_auth);
1973 ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1974 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1975 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1977 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
1979 ikev2_payload_add_notify (chain,
1980 IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1982 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1984 else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
1986 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1988 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1989 ikev2_payload_add_auth (chain, &sa->r_auth);
1991 else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
1993 ikev2_payload_add_notify (chain,
1994 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1995 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1996 ikev2_payload_add_auth (chain, &sa->r_auth);
1997 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1998 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2000 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2002 u8 *data = vec_new (u8, 1);
2004 data[0] = sa->unsupported_cp;
2005 ikev2_payload_add_notify (chain,
2006 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2010 else if (sa->state == IKEV2_STATE_SA_INIT)
2012 ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
2013 ikev2_payload_add_auth (chain, &sa->i_auth);
2014 ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2015 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2016 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2017 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2022 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2026 else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2028 /* if pending delete */
2031 if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2033 if (sa->is_initiator)
2034 ikev2_payload_add_delete (chain, sa->del);
2036 /* The response to a request that deletes the IKE SA is an empty
2037 INFORMATIONAL response. */
2038 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2040 /* The response to a request that deletes ESP or AH SAs will contain
2041 delete payloads for the paired SAs going in the other direction. */
2044 ikev2_payload_add_delete (chain, sa->del);
2049 /* received N(AUTHENTICATION_FAILED) */
2050 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2052 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2055 /* received unsupported critical payload */
2056 else if (sa->unsupported_cp)
2058 u8 *data = vec_new (u8, 1);
2060 data[0] = sa->unsupported_cp;
2061 ikev2_payload_add_notify (chain,
2062 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2065 sa->unsupported_cp = 0;
2067 /* else send empty response */
2069 else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2071 if (sa->is_initiator)
2074 ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2075 ikev2_notify_t notify;
2076 u8 *data = vec_new (u8, 4);
2077 clib_memset (¬ify, 0, sizeof (notify));
2078 notify.protocol_id = IKEV2_PROTOCOL_ESP;
2079 notify.spi = sa->childs[0].i_proposals->spi;
2080 *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2082 ikev2_payload_add_sa (chain, proposals);
2083 ikev2_payload_add_nonce (chain, sa->i_nonce);
2084 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2085 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2086 ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2095 ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
2096 ikev2_payload_add_nonce (chain, sa->r_nonce);
2097 ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
2099 ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
2101 vec_del1 (sa->rekey, 0);
2103 else if (sa->unsupported_cp)
2105 u8 *data = vec_new (u8, 1);
2107 data[0] = sa->unsupported_cp;
2108 ikev2_payload_add_notify (chain,
2109 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2112 sa->unsupported_cp = 0;
2116 ikev2_payload_add_notify (chain,
2117 IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2124 ike->version = IKE_VERSION_2;
2125 ike->nextpayload = IKEV2_PAYLOAD_SK;
2126 tlen = sizeof (*ike);
2127 if (sa->is_initiator)
2129 ike->flags = IKEV2_HDR_FLAG_INITIATOR;
2130 sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
2134 ike->flags = IKEV2_HDR_FLAG_RESPONSE;
2138 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2140 tlen += vec_len (chain->data);
2141 ike->nextpayload = chain->first_payload_type;
2142 ike->length = clib_host_to_net_u32 (tlen);
2143 clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2145 /* store whole IKE payload - needed for PSK auth */
2146 vec_free (sa->last_sa_init_res_packet_data);
2147 vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
2152 ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2155 plen = sizeof (*ph);
2156 ph = (ike_payload_header_t *) & ike->payload[0];
2157 ph->nextpayload = chain->first_payload_type;
2159 int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
2162 /* add space for hmac */
2163 plen += tr_integ->key_trunc;
2166 /* payload and total length */
2167 ph->length = clib_host_to_net_u16 (plen);
2168 ike->length = clib_host_to_net_u32 (tlen);
2170 /* calc integrity data for whole packet except hash itself */
2172 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2173 (u8 *) ike, tlen - tr_integ->key_trunc);
2175 clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2176 sizeof (*ike), integ, tr_integ->key_trunc);
2178 /* store whole IKE payload - needed for retransmit */
2179 vec_free (sa->last_res_packet_data);
2180 vec_add (sa->last_res_packet_data, ike, tlen);
2184 ikev2_payload_destroy_chain (chain);
2190 ikev2_retransmit_sa_init (ike_header_t * ike,
2191 ip4_address_t iaddr, ip4_address_t raddr)
2193 ikev2_main_t *km = &ikev2_main;
2195 u32 thread_index = vlib_get_thread_index ();
2198 pool_foreach (sa, km->per_thread_data[thread_index].sas, ({
2199 if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
2200 sa->iaddr.as_u32 == iaddr.as_u32 &&
2201 sa->raddr.as_u32 == raddr.as_u32)
2204 u32 len = clib_net_to_host_u32(ike->length);
2205 u8 payload = ike->nextpayload;
2207 while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
2208 ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
2209 u32 plen = clib_net_to_host_u16(ikep->length);
2211 if (plen < sizeof(ike_payload_header_t))
2214 if (payload == IKEV2_PAYLOAD_NONCE)
2216 if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
2218 /* req is retransmit */
2219 if (sa->state == IKEV2_STATE_SA_INIT)
2222 tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
2223 ike->ispi = tmp->ispi;
2224 ike->rspi = tmp->rspi;
2225 ike->nextpayload = tmp->nextpayload;
2226 ike->version = tmp->version;
2227 ike->exchange = tmp->exchange;
2228 ike->flags = tmp->flags;
2229 ike->msgid = tmp->msgid;
2230 ike->length = tmp->length;
2231 clib_memcpy_fast(ike->payload, tmp->payload,
2232 clib_net_to_host_u32(tmp->length) - sizeof(*ike));
2233 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2234 "ispi %lx IKE_SA_INIT retransmit "
2235 "from %d.%d.%d.%d to %d.%d.%d.%d",
2237 raddr.as_u32, iaddr.as_u32);
2240 /* else ignore req */
2243 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2244 "ispi %lx IKE_SA_INIT ignore "
2245 "from %d.%d.%d.%d to %d.%d.%d.%d",
2247 raddr.as_u32, iaddr.as_u32);
2252 payload = ikep->nextpayload;
2259 /* req is not retransmit */
2264 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2266 u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2269 if (msg_id > sa->last_msg_id)
2271 sa->last_msg_id = msg_id;
2274 /* retransmitted req */
2275 else if (msg_id == sa->last_msg_id)
2278 tmp = (ike_header_t *) sa->last_res_packet_data;
2279 ike->ispi = tmp->ispi;
2280 ike->rspi = tmp->rspi;
2281 ike->nextpayload = tmp->nextpayload;
2282 ike->version = tmp->version;
2283 ike->exchange = tmp->exchange;
2284 ike->flags = tmp->flags;
2285 ike->msgid = tmp->msgid;
2286 ike->length = tmp->length;
2287 clib_memcpy_fast (ike->payload, tmp->payload,
2288 clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
2289 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2290 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2293 /* old req ignore */
2296 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2297 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2304 ikev2_node_fn (vlib_main_t * vm,
2305 vlib_node_runtime_t * node, vlib_frame_t * frame)
2307 u32 n_left_from, *from, *to_next;
2308 ikev2_next_t next_index;
2309 ikev2_main_t *km = &ikev2_main;
2310 u32 thread_index = vlib_get_thread_index ();
2312 from = vlib_frame_vector_args (frame);
2313 n_left_from = frame->n_vectors;
2314 next_index = node->cached_next_index;
2316 while (n_left_from > 0)
2320 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2322 while (n_left_from > 0 && n_left_to_next > 0)
2326 u32 next0 = IKEV2_NEXT_ERROR_DROP;
2331 ikev2_sa_t *sa0 = 0;
2332 ikev2_sa_t sa; /* temporary store for SA */
2336 /* speculatively enqueue b0 to the current next frame */
2342 n_left_to_next -= 1;
2344 b0 = vlib_get_buffer (vm, bi0);
2345 ike0 = vlib_buffer_get_current (b0);
2346 vlib_buffer_advance (b0, -sizeof (*udp0));
2347 udp0 = vlib_buffer_get_current (b0);
2348 vlib_buffer_advance (b0, -sizeof (*ip40));
2349 ip40 = vlib_buffer_get_current (b0);
2351 if (ike0->version != IKE_VERSION_2)
2353 vlib_node_increment_counter (vm, ikev2_node.index,
2354 IKEV2_ERROR_NOT_IKEV2, 1);
2358 if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2361 clib_memset (sa0, 0, sizeof (*sa0));
2363 if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
2365 if (ike0->rspi == 0)
2367 sa0->raddr.as_u32 = ip40->dst_address.as_u32;
2368 sa0->iaddr.as_u32 = ip40->src_address.as_u32;
2370 r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2374 vlib_node_increment_counter (vm, ikev2_node.index,
2375 IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2377 len = clib_net_to_host_u32 (ike0->length);
2382 vlib_node_increment_counter (vm, ikev2_node.index,
2383 IKEV2_ERROR_IKE_SA_INIT_IGNORE,
2388 ikev2_process_sa_init_req (vm, sa0, ike0);
2390 if (sa0->state == IKEV2_STATE_SA_INIT)
2392 ikev2_sa_free_proposal_vector (&sa0->r_proposals);
2394 ikev2_select_proposal (sa0->i_proposals,
2395 IKEV2_PROTOCOL_IKE);
2396 ikev2_generate_sa_init_data (sa0);
2399 if (sa0->state == IKEV2_STATE_SA_INIT
2400 || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2402 len = ikev2_generate_message (sa0, ike0, 0);
2405 if (sa0->state == IKEV2_STATE_SA_INIT)
2407 /* add SA to the pool */
2408 pool_get (km->per_thread_data[thread_index].sas,
2410 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2412 per_thread_data[thread_index].sa_by_rspi,
2415 km->per_thread_data[thread_index].sas);
2419 ikev2_sa_free_all_vec (sa0);
2423 else //received sa_init without initiator flag
2425 ikev2_process_sa_init_resp (vm, sa0, ike0);
2427 if (sa0->state == IKEV2_STATE_SA_INIT)
2429 ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
2430 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2434 pool_elt_at_index (km->sais, p[0]);
2436 ikev2_complete_sa_data (sa0, sai);
2437 ikev2_calc_keys (sa0);
2438 ikev2_sa_auth_init (sa0);
2439 len = ikev2_generate_message (sa0, ike0, 0);
2443 if (sa0->state == IKEV2_STATE_SA_INIT)
2445 /* add SA to the pool */
2446 pool_get (km->per_thread_data[thread_index].sas, sa0);
2447 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2448 hash_set (km->per_thread_data[thread_index].sa_by_rspi,
2450 sa0 - km->per_thread_data[thread_index].sas);
2454 ikev2_sa_free_all_vec (sa0);
2458 else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2461 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2462 clib_net_to_host_u64 (ike0->rspi));
2466 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2469 r = ikev2_retransmit_resp (sa0, ike0);
2472 vlib_node_increment_counter (vm, ikev2_node.index,
2473 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2475 len = clib_net_to_host_u32 (ike0->length);
2480 vlib_node_increment_counter (vm, ikev2_node.index,
2481 IKEV2_ERROR_IKE_REQ_IGNORE,
2486 ikev2_process_auth_req (vm, sa0, ike0);
2487 ikev2_sa_auth (sa0);
2488 if (sa0->state == IKEV2_STATE_AUTHENTICATED)
2490 ikev2_initial_contact_cleanup (sa0);
2491 ikev2_sa_match_ts (sa0);
2492 if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
2493 ikev2_create_tunnel_interface (km->vnet_main,
2499 if (sa0->is_initiator)
2501 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2505 pool_elt_at_index (km->sais, p[0]);
2506 hash_unset (km->sa_by_ispi, sai->ispi);
2507 ikev2_sa_free_all_vec (sai);
2508 pool_put (km->sais, sai);
2513 len = ikev2_generate_message (sa0, ike0, 0);
2517 else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2520 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2521 clib_net_to_host_u64 (ike0->rspi));
2525 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2528 r = ikev2_retransmit_resp (sa0, ike0);
2531 vlib_node_increment_counter (vm, ikev2_node.index,
2532 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2534 len = clib_net_to_host_u32 (ike0->length);
2539 vlib_node_increment_counter (vm, ikev2_node.index,
2540 IKEV2_ERROR_IKE_REQ_IGNORE,
2545 ikev2_process_informational_req (vm, sa0, ike0);
2548 if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
2550 ikev2_delete_t *d, *tmp, *resp = 0;
2551 vec_foreach (d, sa0->del)
2553 ikev2_child_sa_t *ch_sa;
2554 ch_sa = ikev2_sa_get_child (sa0, d->spi,
2556 !sa0->is_initiator);
2559 ikev2_delete_tunnel_interface (km->vnet_main,
2561 if (!sa0->is_initiator)
2563 vec_add2 (resp, tmp, 1);
2564 tmp->protocol_id = d->protocol_id;
2565 tmp->spi = ch_sa->r_proposals[0].spi;
2567 ikev2_sa_del_child_sa (sa0, ch_sa);
2570 if (!sa0->is_initiator)
2572 vec_free (sa0->del);
2577 if (!sa0->is_initiator)
2579 len = ikev2_generate_message (sa0, ike0, 0);
2583 else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2586 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2587 clib_net_to_host_u64 (ike0->rspi));
2591 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2594 r = ikev2_retransmit_resp (sa0, ike0);
2597 vlib_node_increment_counter (vm, ikev2_node.index,
2598 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2600 len = clib_net_to_host_u32 (ike0->length);
2605 vlib_node_increment_counter (vm, ikev2_node.index,
2606 IKEV2_ERROR_IKE_REQ_IGNORE,
2611 ikev2_process_create_child_sa_req (vm, sa0, ike0);
2614 if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
2617 vec_free (sa0->childs);
2618 ikev2_child_sa_t *child;
2619 vec_add2 (sa0->childs, child, 1);
2620 child->r_proposals = sa0->rekey[0].r_proposal;
2621 child->i_proposals = sa0->rekey[0].i_proposal;
2622 child->tsi = sa0->rekey[0].tsi;
2623 child->tsr = sa0->rekey[0].tsr;
2624 ikev2_create_tunnel_interface (km->vnet_main,
2627 child - sa0->childs,
2630 if (sa0->is_initiator)
2632 vec_del1 (sa0->rekey, 0);
2636 len = ikev2_generate_message (sa0, ike0, 0);
2643 ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2644 "received from %d.%d.%d.%d to %d.%d.%d.%d",
2646 ip40->src_address.as_u32,
2647 ip40->dst_address.as_u32);
2651 /* if we are sending packet back, rewrite headers */
2654 next0 = IKEV2_NEXT_IP4_LOOKUP;
2655 if (sa0->is_initiator)
2657 ip40->dst_address.as_u32 = sa0->raddr.as_u32;
2658 ip40->src_address.as_u32 = sa0->iaddr.as_u32;
2662 ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
2663 ip40->src_address.as_u32 = sa0->raddr.as_u32;
2666 clib_host_to_net_u16 (len + sizeof (udp_header_t));
2668 b0->current_length =
2669 len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2670 ip40->length = clib_host_to_net_u16 (b0->current_length);
2671 ip40->checksum = ip4_header_checksum (ip40);
2674 if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
2675 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
2677 ikev2_child_sa_t *c;
2679 vec_foreach (c, sa0->childs)
2680 ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
2682 ikev2_delete_sa (sa0);
2684 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2686 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2687 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2689 ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
2690 t->sw_if_index = sw_if_index0;
2691 t->next_index = next0;
2694 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2695 n_left_to_next, bi0, next0);
2698 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2701 vlib_node_increment_counter (vm, ikev2_node.index,
2702 IKEV2_ERROR_PROCESSED, frame->n_vectors);
2703 return frame->n_vectors;
2707 VLIB_REGISTER_NODE (ikev2_node,static) = {
2708 .function = ikev2_node_fn,
2710 .vector_size = sizeof (u32),
2711 .format_trace = format_ikev2_trace,
2712 .type = VLIB_NODE_TYPE_INTERNAL,
2714 .n_errors = ARRAY_LEN(ikev2_error_strings),
2715 .error_strings = ikev2_error_strings,
2717 .n_next_nodes = IKEV2_N_NEXT,
2720 [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
2721 [IKEV2_NEXT_ERROR_DROP] = "error-drop",
2726 // set ikev2 proposals when vpp is used as initiator
2727 static clib_error_t *
2728 ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
2729 ikev2_transforms_set * ts,
2730 ikev2_sa_proposal_t ** proposals, int is_ike)
2733 ikev2_main_t *km = &ikev2_main;
2734 ikev2_sa_proposal_t *proposal;
2735 vec_add2 (*proposals, proposal, 1);
2736 ikev2_sa_transform_t *td;
2741 vec_foreach (td, km->supported_transforms)
2743 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
2744 && td->encr_type == ts->crypto_alg
2745 && td->key_len == ts->crypto_key_size / 8)
2748 attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
2749 attr[1] = clib_host_to_net_u16 (td->key_len << 3);
2750 vec_add (td->attrs, (u8 *) attr, 4);
2751 vec_add1 (proposal->transforms, *td);
2760 r = clib_error_return (0, "Unsupported algorithm");
2764 if (IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16 != ts->crypto_alg)
2768 vec_foreach (td, km->supported_transforms)
2770 if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
2771 && td->integ_type == ts->integ_alg)
2773 vec_add1 (proposal->transforms, *td);
2781 ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
2782 r = clib_error_return (0, "Unsupported algorithm");
2791 vec_foreach (td, km->supported_transforms)
2793 if (td->type == IKEV2_TRANSFORM_TYPE_PRF
2794 && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
2796 vec_add1 (proposal->transforms, *td);
2803 r = clib_error_return (0, "Unsupported algorithm");
2809 if (is_ike || ts->dh_type != IKEV2_TRANSFORM_DH_TYPE_NONE)
2812 vec_foreach (td, km->supported_transforms)
2814 if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
2816 vec_add1 (proposal->transforms, *td);
2819 sa->dh_group = td->dh_type;
2827 r = clib_error_return (0, "Unsupported algorithm");
2835 vec_foreach (td, km->supported_transforms)
2837 if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
2839 vec_add1 (proposal->transforms, *td);
2846 r = clib_error_return (0, "Unsupported algorithm");
2855 static ikev2_profile_t *
2856 ikev2_profile_index_by_name (u8 * name)
2858 ikev2_main_t *km = &ikev2_main;
2861 p = mhash_get (&km->profile_index_by_name, name);
2865 return pool_elt_at_index (km->profiles, p[0]);
2870 ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst,
2879 b0 = vlib_get_buffer (vm, bi0);
2880 vlib_buffer_advance (b0, -sizeof (udp_header_t));
2881 udp0 = vlib_buffer_get_current (b0);
2882 vlib_buffer_advance (b0, -sizeof (ip4_header_t));
2883 ip40 = vlib_buffer_get_current (b0);
2886 ip40->ip_version_and_header_length = 0x45;
2888 ip40->fragment_id = 0;
2889 ip40->flags_and_fragment_offset = 0;
2891 ip40->protocol = IP_PROTOCOL_UDP;
2892 ip40->dst_address.as_u32 = dst->as_u32;
2893 ip40->src_address.as_u32 = src->as_u32;
2894 udp0->dst_port = clib_host_to_net_u16 (500);
2895 udp0->src_port = clib_host_to_net_u16 (500);
2896 udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
2898 b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2899 ip40->length = clib_host_to_net_u16 (b0->current_length);
2900 ip40->checksum = ip4_header_checksum (ip40);
2903 /* send the request */
2904 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
2905 to_next = vlib_frame_vector_args (f);
2908 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
2913 ikev2_get_new_ike_header_buff (vlib_main_t * vm, ike_header_t ** ike)
2916 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2921 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
2922 *ike = vlib_buffer_get_current (b0);
2927 ikev2_set_local_key (vlib_main_t * vm, u8 * file)
2929 ikev2_main_t *km = &ikev2_main;
2931 km->pkey = ikev2_load_key_file (file);
2932 if (km->pkey == NULL)
2933 return clib_error_return (0, "load key '%s' failed", file);
2938 static_always_inline vnet_api_error_t
2939 ikev2_register_udp_port (ikev2_profile_t * p, u16 port)
2941 ikev2_main_t *km = &ikev2_main;
2942 udp_dst_port_info_t *pi;
2944 uword *v = hash_get (km->udp_ports, port);
2945 pi = udp_get_dst_port_info (&udp_main, port, UDP_IP4);
2949 /* IKE already uses this port, only increment reference counter */
2956 return VNET_API_ERROR_UDP_PORT_TAKEN;
2958 udp_register_dst_port (km->vlib_main, port,
2959 ipsec4_tun_input_node.index, 1);
2960 hash_set (km->udp_ports, port, 1);
2966 static_always_inline void
2967 ikev2_unregister_udp_port (ikev2_profile_t * p)
2969 ikev2_main_t *km = &ikev2_main;
2972 if (p->dst_port == IPSEC_UDP_PORT_NONE)
2975 v = hash_get (km->udp_ports, p->dst_port);
2983 udp_unregister_dst_port (km->vlib_main, p->dst_port, 1);
2984 hash_unset (km->udp_ports, p->dst_port);
2987 p->dst_port = IPSEC_UDP_PORT_NONE;
2991 ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
2993 ikev2_main_t *km = &ikev2_main;
2998 if (ikev2_profile_index_by_name (name))
2999 return clib_error_return (0, "policy %v already exists", name);
3001 pool_get (km->profiles, p);
3002 clib_memset (p, 0, sizeof (*p));
3003 p->name = vec_dup (name);
3004 p->dst_port = IPSEC_UDP_PORT_NONE;
3005 p->responder.sw_if_index = ~0;
3007 uword index = p - km->profiles;
3008 mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
3012 p = ikev2_profile_index_by_name (name);
3014 return clib_error_return (0, "policy %v does not exists", name);
3016 ikev2_unregister_udp_port (p);
3019 pool_put (km->profiles, p);
3020 mhash_unset (&km->profile_index_by_name, name, 0);
3026 ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
3027 u8 * auth_data, u8 data_hex_format)
3032 p = ikev2_profile_index_by_name (name);
3036 r = clib_error_return (0, "unknown profile %v", name);
3039 vec_free (p->auth.data);
3040 p->auth.method = auth_method;
3041 p->auth.data = vec_dup (auth_data);
3042 p->auth.hex = data_hex_format;
3044 if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
3046 vec_add1 (p->auth.data, 0);
3048 EVP_PKEY_free (p->auth.key);
3049 p->auth.key = ikev2_load_cert_file (auth_data);
3050 if (p->auth.key == NULL)
3051 return clib_error_return (0, "load cert '%s' failed", auth_data);
3058 ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
3064 if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
3065 && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
3067 r = clib_error_return (0, "unsupported identity type %U",
3068 format_ikev2_id_type, id_type);
3072 p = ikev2_profile_index_by_name (name);
3076 r = clib_error_return (0, "unknown profile %v", name);
3082 vec_free (p->loc_id.data);
3083 p->loc_id.type = id_type;
3084 p->loc_id.data = vec_dup (data);
3088 vec_free (p->rem_id.data);
3089 p->rem_id.type = id_type;
3090 p->rem_id.data = vec_dup (data);
3097 ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
3098 u16 start_port, u16 end_port, ip4_address_t start_addr,
3099 ip4_address_t end_addr, int is_local)
3104 p = ikev2_profile_index_by_name (name);
3108 r = clib_error_return (0, "unknown profile %v", name);
3114 p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
3115 p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
3116 p->loc_ts.start_port = start_port;
3117 p->loc_ts.end_port = end_port;
3118 p->loc_ts.protocol_id = protocol_id;
3119 p->loc_ts.ts_type = 7;
3123 p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
3124 p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
3125 p->rem_ts.start_port = start_port;
3126 p->rem_ts.end_port = end_port;
3127 p->rem_ts.protocol_id = protocol_id;
3128 p->rem_ts.ts_type = 7;
3136 ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
3137 u32 sw_if_index, ip4_address_t ip4)
3142 p = ikev2_profile_index_by_name (name);
3146 r = clib_error_return (0, "unknown profile %v", name);
3150 p->responder.sw_if_index = sw_if_index;
3151 p->responder.ip4 = ip4;
3157 ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
3158 ikev2_transform_encr_type_t crypto_alg,
3159 ikev2_transform_integ_type_t integ_alg,
3160 ikev2_transform_dh_type_t dh_type,
3161 u32 crypto_key_size)
3166 p = ikev2_profile_index_by_name (name);
3170 r = clib_error_return (0, "unknown profile %v", name);
3174 p->ike_ts.crypto_alg = crypto_alg;
3175 p->ike_ts.integ_alg = integ_alg;
3176 p->ike_ts.dh_type = dh_type;
3177 p->ike_ts.crypto_key_size = crypto_key_size;
3182 ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
3183 ikev2_transform_encr_type_t crypto_alg,
3184 ikev2_transform_integ_type_t integ_alg,
3185 ikev2_transform_dh_type_t dh_type,
3186 u32 crypto_key_size)
3191 p = ikev2_profile_index_by_name (name);
3195 r = clib_error_return (0, "unknown profile %v", name);
3199 p->esp_ts.crypto_alg = crypto_alg;
3200 p->esp_ts.integ_alg = integ_alg;
3201 p->esp_ts.dh_type = dh_type;
3202 p->esp_ts.crypto_key_size = crypto_key_size;
3207 ikev2_set_profile_tunnel_interface (vlib_main_t * vm,
3208 u8 * name, u32 sw_if_index)
3213 p = ikev2_profile_index_by_name (name);
3217 r = clib_error_return (0, "unknown profile %v", name);
3221 p->tun_itf = sw_if_index;
3227 ikev2_set_profile_ipsec_udp_port (vlib_main_t * vm, u8 * name, u16 port,
3230 ikev2_profile_t *p = ikev2_profile_index_by_name (name);
3231 ikev2_main_t *km = &ikev2_main;
3232 vnet_api_error_t rv = 0;
3236 return VNET_API_ERROR_INVALID_VALUE;
3240 if (p->dst_port != IPSEC_UDP_PORT_NONE)
3241 return VNET_API_ERROR_VALUE_EXIST;
3243 rv = ikev2_register_udp_port (p, port);
3247 v = hash_get (km->udp_ports, port);
3249 return VNET_API_ERROR_IKE_NO_PORT;
3251 if (p->dst_port == IPSEC_UDP_PORT_NONE)
3252 return VNET_API_ERROR_INVALID_VALUE;
3254 ikev2_unregister_udp_port (p);
3260 ikev2_set_profile_udp_encap (vlib_main_t * vm, u8 * name)
3262 ikev2_profile_t *p = ikev2_profile_index_by_name (name);
3267 r = clib_error_return (0, "unknown profile %v", name);
3276 ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
3277 u64 lifetime, u32 jitter, u32 handover,
3283 p = ikev2_profile_index_by_name (name);
3287 r = clib_error_return (0, "unknown profile %v", name);
3291 p->lifetime = lifetime;
3292 p->lifetime_jitter = jitter;
3293 p->handover = handover;
3294 p->lifetime_maxdata = maxdata;
3299 ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
3303 ip4_main_t *im = &ip4_main;
3304 ikev2_main_t *km = &ikev2_main;
3306 p = ikev2_profile_index_by_name (name);
3310 r = clib_error_return (0, "unknown profile %v", name);
3314 if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
3316 r = clib_error_return (0, "responder not set for profile %v", name);
3321 /* Create the Initiator Request */
3325 ip_lookup_main_t *lm = &im->lookup_main;
3327 int len = sizeof (ike_header_t);
3329 /* Get own iface IP */
3331 lm->if_address_pool_index_by_sw_if_index[p->responder.sw_if_index];
3332 ip_interface_address_t *if_add =
3333 pool_elt_at_index (lm->if_address_pool, if_add_index0);
3334 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
3336 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3338 /* Prepare the SA and the IKE payload */
3340 clib_memset (&sa, 0, sizeof (ikev2_sa_t));
3341 ikev2_payload_chain_t *chain = 0;
3342 ikev2_payload_new_chain (chain);
3344 /* Build the IKE proposal payload */
3345 ikev2_sa_proposal_t *proposals = 0;
3346 ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
3347 proposals[0].proposal_num = 1;
3348 proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
3350 /* Add and then cleanup proposal data */
3351 ikev2_payload_add_sa (chain, proposals);
3352 ikev2_sa_free_proposal_vector (&proposals);
3354 sa.is_initiator = 1;
3355 sa.profile_index = km->profiles - p;
3356 sa.is_profile_index_set = 1;
3357 sa.state = IKEV2_STATE_SA_INIT;
3358 sa.tun_itf = p->tun_itf;
3359 sa.udp_encap = p->udp_encap;
3360 sa.dst_port = p->dst_port;
3361 sa.is_tun_itf_set = 1;
3362 sa.initial_contact = 1;
3363 ikev2_generate_sa_init_data (&sa);
3364 ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
3365 ikev2_payload_add_nonce (chain, sa.i_nonce);
3367 /* Build the child SA proposal */
3368 vec_resize (sa.childs, 1);
3369 ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
3370 &sa.childs[0].i_proposals, 0);
3371 sa.childs[0].i_proposals[0].proposal_num = 1;
3372 sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
3373 RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
3374 sizeof (sa.childs[0].i_proposals[0].spi));
3378 /* Add NAT detection notification messages (mandatory) */
3379 u8 nat_detection_source[8 + 8 + 4 + 2];
3380 u8 *nat_detection_sha1 = vec_new (u8, 20);
3382 u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
3383 clib_memcpy_fast (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
3384 tmpspi = clib_host_to_net_u64 (sa.rspi);
3385 clib_memcpy_fast (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
3386 u16 tmpport = clib_host_to_net_u16 (500);
3387 clib_memcpy_fast (&nat_detection_source[8 + 8 + 4], &tmpport,
3389 u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
3390 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3391 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3392 nat_detection_sha1);
3393 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
3394 nat_detection_sha1);
3395 tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
3396 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3397 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3398 nat_detection_sha1);
3399 ikev2_payload_add_notify (chain,
3400 IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
3401 nat_detection_sha1);
3402 vec_free (nat_detection_sha1);
3404 u8 *sig_hash_algo = vec_new (u8, 8);
3405 u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
3406 clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
3407 ikev2_payload_add_notify (chain,
3408 IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
3410 vec_free (sig_hash_algo);
3413 /* Buffer update and boilerplate */
3414 len += vec_len (chain->data);
3415 ike0->nextpayload = chain->first_payload_type;
3416 ike0->length = clib_host_to_net_u32 (len);
3417 clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
3418 ikev2_payload_destroy_chain (chain);
3420 ike0->version = IKE_VERSION_2;
3421 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3422 ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
3423 ike0->ispi = sa.ispi;
3427 /* store whole IKE payload - needed for PSK auth */
3428 vec_free (sa.last_sa_init_req_packet_data);
3429 vec_add (sa.last_sa_init_req_packet_data, ike0, len);
3431 /* add data to the SA then add it to the pool */
3432 sa.iaddr.as_u32 = if_ip->as_u32;
3433 sa.raddr.as_u32 = p->responder.ip4.as_u32;
3434 sa.i_id.type = p->loc_id.type;
3435 sa.i_id.data = vec_dup (p->loc_id.data);
3436 sa.i_auth.method = p->auth.method;
3437 sa.i_auth.hex = p->auth.hex;
3438 sa.i_auth.data = vec_dup (p->auth.data);
3439 vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
3440 vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
3442 ikev2_initial_contact_cleanup (&sa);
3444 /* add SA to the pool */
3445 ikev2_sa_t *sa0 = 0;
3446 pool_get (km->sais, sa0);
3447 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3448 hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
3450 ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
3452 ikev2_elog_exchange ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to "
3453 "%d.%d.%d.%d", clib_host_to_net_u64 (sa0->ispi), 0,
3454 p->responder.ip4.as_u32);
3461 ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3462 ikev2_child_sa_t * csa)
3464 /* Create the Initiator notification for child SA removal */
3465 ikev2_main_t *km = &ikev2_main;
3470 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3473 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3474 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3475 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3476 vec_resize (sa->del, 1);
3477 sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
3478 sa->del->spi = csa->i_proposals->spi;
3479 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3480 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3481 len = ikev2_generate_message (sa, ike0, 0);
3483 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3485 /* delete local child SA */
3486 ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
3487 ikev2_sa_del_child_sa (sa, csa);
3491 ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
3494 ikev2_main_t *km = &ikev2_main;
3495 ikev2_main_per_thread_data_t *tkm;
3496 ikev2_sa_t *fsa = 0;
3497 ikev2_child_sa_t *fchild = 0;
3499 /* Search for the child SA */
3500 vec_foreach (tkm, km->per_thread_data)
3506 pool_foreach (sa, tkm->sas, ({
3507 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3517 if (!fchild || !fsa)
3519 r = clib_error_return (0, "Child SA not found");
3524 ikev2_delete_child_sa_internal (vm, fsa, fchild);
3531 ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
3534 ikev2_main_t *km = &ikev2_main;
3535 ikev2_main_per_thread_data_t *tkm;
3536 ikev2_sa_t *fsa = 0;
3537 ikev2_main_per_thread_data_t *ftkm = 0;
3539 /* Search for the IKE SA */
3540 vec_foreach (tkm, km->per_thread_data)
3546 pool_foreach (sa, tkm->sas, ({
3547 if (sa->ispi == ispi)
3559 r = clib_error_return (0, "IKE SA not found");
3564 /* Create the Initiator notification for IKE SA removal */
3570 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3573 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3574 ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
3575 ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
3576 vec_resize (fsa->del, 1);
3577 fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3578 fsa->del->spi = ispi;
3579 ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
3580 fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3581 len = ikev2_generate_message (fsa, ike0, 0);
3583 ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
3587 /* delete local SA */
3588 ikev2_child_sa_t *c;
3589 vec_foreach (c, fsa->childs)
3591 ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
3592 ikev2_sa_del_child_sa (fsa, c);
3594 ikev2_sa_free_all_vec (fsa);
3595 uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
3598 hash_unset (ftkm->sa_by_rspi, fsa->rspi);
3599 pool_put (ftkm->sas, fsa);
3607 ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3608 ikev2_child_sa_t * csa)
3610 /* Create the Initiator request for create child SA */
3616 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3619 ike0->version = IKE_VERSION_2;
3620 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3621 ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
3622 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3623 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3624 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3625 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3627 ikev2_rekey_t *rekey;
3628 vec_add2 (sa->rekey, rekey, 1);
3629 ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
3632 RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
3633 rekey->spi = proposals[0].spi;
3634 rekey->ispi = csa->i_proposals->spi;
3635 len = ikev2_generate_message (sa, ike0, proposals);
3636 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3637 vec_free (proposals);
3641 ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
3644 ikev2_main_t *km = &ikev2_main;
3645 ikev2_main_per_thread_data_t *tkm;
3646 ikev2_sa_t *fsa = 0;
3647 ikev2_child_sa_t *fchild = 0;
3649 /* Search for the child SA */
3650 vec_foreach (tkm, km->per_thread_data)
3656 pool_foreach (sa, tkm->sas, ({
3657 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3667 if (!fchild || !fsa)
3669 r = clib_error_return (0, "Child SA not found");
3674 ikev2_rekey_child_sa_internal (vm, fsa, fchild);
3681 ikev2_init (vlib_main_t * vm)
3683 ikev2_main_t *km = &ikev2_main;
3684 vlib_thread_main_t *tm = vlib_get_thread_main ();
3687 clib_memset (km, 0, sizeof (ikev2_main_t));
3688 km->vnet_main = vnet_get_main ();
3691 ikev2_crypto_init (km);
3693 mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
3695 vec_validate_aligned (km->per_thread_data, tm->n_vlib_mains - 1,
3696 CLIB_CACHE_LINE_BYTES);
3697 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
3699 km->per_thread_data[thread_id].sa_by_rspi =
3700 hash_create (0, sizeof (uword));
3703 km->sa_by_ispi = hash_create (0, sizeof (uword));
3704 km->sw_if_indices = hash_create (0, 0);
3705 km->udp_ports = hash_create (0, sizeof (uword));
3707 udp_register_dst_port (vm, 500, ikev2_node.index, 1);
3709 ikev2_cli_reference ();
3711 km->log_level = IKEV2_LOG_ERROR;
3712 km->log_class = vlib_log_register_class ("ikev2", 0);
3717 VLIB_INIT_FUNCTION (ikev2_init) =
3719 .runs_after = VLIB_INITS("ipsec_init"),
3724 ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa,
3727 ikev2_main_t *km = &ikev2_main;
3728 ikev2_profile_t *p = 0;
3729 vlib_main_t *vm = km->vlib_main;
3730 f64 now = vlib_time_now (vm);
3733 if (sa->is_profile_index_set)
3734 p = pool_elt_at_index (km->profiles, sa->profile_index);
3736 if (sa->is_initiator && p && csa->time_to_expiration
3737 && now > csa->time_to_expiration)
3739 if (!csa->is_expired || csa->rekey_retries > 0)
3741 ikev2_rekey_child_sa_internal (vm, sa, csa);
3742 csa->time_to_expiration = now + p->handover;
3743 csa->is_expired = 1;
3744 if (csa->rekey_retries == 0)
3746 csa->rekey_retries = 5;
3748 else if (csa->rekey_retries > 0)
3750 csa->rekey_retries--;
3751 ikev2_log_debug ("Rekeying Child SA 0x%x, retries left %d",
3752 csa->i_proposals->spi, csa->rekey_retries);
3753 if (csa->rekey_retries == 0)
3755 csa->rekey_retries = -1;
3762 csa->time_to_expiration = 0;
3763 ikev2_delete_child_sa_internal (vm, sa, csa);
3770 ipip_tunnel_t *ipip = NULL;
3771 u32 sw_if_index = sa->is_tun_itf_set ? sa->tun_itf : ~0;
3772 if (~0 == sw_if_index)
3774 ip46_address_t local_ip;
3775 ip46_address_t remote_ip;
3776 if (sa->is_initiator)
3778 ip46_address_set_ip4 (&local_ip, &sa->iaddr);
3779 ip46_address_set_ip4 (&remote_ip, &sa->raddr);
3783 ip46_address_set_ip4 (&local_ip, &sa->raddr);
3784 ip46_address_set_ip4 (&remote_ip, &sa->iaddr);
3788 ipip_tunnel_key_t key = {
3791 .transport = IPIP_TRANSPORT_IP4,
3796 ipip = ipip_tunnel_db_find (&key);
3799 sw_if_index = ipip->sw_if_index;
3805 vec_add1 (sas_in, csa->remote_sa_id);
3806 ipsec_tun_protect_update (sw_if_index, NULL, csa->local_sa_id, sas_in);
3807 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (csa->remote_sa_id));
3814 ikev2_set_log_level (ikev2_log_level_t log_level)
3816 ikev2_main_t *km = &ikev2_main;
3818 if (log_level >= IKEV2_LOG_MAX)
3820 ikev2_log_error ("unknown logging level %d", log_level);
3824 km->log_level = log_level;
3829 ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
3831 ikev2_main_t *km = &ikev2_main;
3832 vlib_main_t *vm = km->vlib_main;
3833 ikev2_main_per_thread_data_t *tkm;
3834 ikev2_sa_t *fsa = 0;
3835 ikev2_profile_t *p = 0;
3836 ikev2_child_sa_t *fchild = 0;
3837 f64 now = vlib_time_now (vm);
3838 vlib_counter_t counts;
3840 /* Search for the SA and child SA */
3841 vec_foreach (tkm, km->per_thread_data)
3847 pool_foreach (sa, tkm->sas, ({
3848 fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
3857 vlib_get_combined_counter (&ipsec_sa_counters,
3858 ipsec_sa->stat_index, &counts);
3860 if (fsa && fsa->is_profile_index_set)
3861 p = pool_elt_at_index (km->profiles, fsa->profile_index);
3863 if (fchild && p && p->lifetime_maxdata)
3865 if (!fchild->is_expired && counts.bytes > p->lifetime_maxdata)
3867 fchild->time_to_expiration = now;
3873 ikev2_process_pending_sa_init (ikev2_main_t * km)
3880 hash_foreach (ispi, sai, km->sa_by_ispi,
3882 sa = pool_elt_at_index (km->sais, sai);
3884 if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
3887 vlib_buffer_t * b = vlib_get_buffer (km->vlib_main, bi0);
3888 clib_memcpy_fast (vlib_buffer_get_current (b),
3889 sa->last_sa_init_req_packet_data,
3890 vec_len (sa->last_sa_init_req_packet_data));
3891 ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0,
3892 vec_len (sa->last_sa_init_req_packet_data));
3897 static vlib_node_registration_t ikev2_mngr_process_node;
3900 ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
3903 ikev2_main_t *km = &ikev2_main;
3904 ipsec_main_t *im = &ipsec_main;
3909 vlib_process_wait_for_event_or_clock (vm, 1);
3910 vlib_process_get_events (vm, NULL);
3912 /* process ike child sas */
3913 ikev2_main_per_thread_data_t *tkm;
3914 vec_foreach (tkm, km->per_thread_data)
3918 pool_foreach (sa, tkm->sas, ({
3919 ikev2_child_sa_t *c;
3921 if (sa->old_remote_id_present && 0 > sa->old_id_expiration)
3923 sa->old_remote_id_present = 0;
3927 sa->old_id_expiration -= 1;
3929 vec_foreach (c, sa->childs)
3931 req_sent |= ikev2_mngr_process_child_sa(sa, c, del_old_ids);
3937 /* process ipsec sas */
3940 pool_foreach (sa, im->sad, ({
3941 ikev2_mngr_process_ipsec_sa(sa);
3945 ikev2_process_pending_sa_init (km);
3949 vlib_process_wait_for_event_or_clock (vm, 5);
3950 vlib_process_get_events (vm, NULL);
3959 VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
3960 .function = ikev2_mngr_process_fn,
3961 .type = VLIB_NODE_TYPE_PROCESS,
3963 "ikev2-manager-process",
3966 VLIB_PLUGIN_REGISTER () = {
3967 .version = VPP_BUILD_VER,
3968 .description = "Internet Key Exchange (IKEv2) Protocol",
3973 * fd.io coding-style-patch-verification: ON
3976 * eval: (c-set-style "gnu")