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);
404 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
409 /* check if received DH group is on our list of supported groups */
410 vec_foreach (t2, km->supported_transforms)
412 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
421 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
426 /* generate dh keys */
427 ikev2_complete_dh (sa, t);
432 ikev2_calc_keys (ikev2_sa_t * sa)
435 /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
438 u16 integ_key_len = 0;
439 ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
441 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
443 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
445 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
448 integ_key_len = tr_integ->key_len;
450 vec_append (s, sa->i_nonce);
451 vec_append (s, sa->r_nonce);
452 skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
454 /* Calculate S = Ni | Nr | SPIi | SPIr */
456 vec_add2 (s, tmp, 2 * sizeof (*spi));
458 spi[0] = clib_host_to_net_u64 (sa->ispi);
459 spi[1] = clib_host_to_net_u64 (sa->rspi);
461 /* calculate PRFplus */
463 int len = tr_prf->key_trunc + /* SK_d */
464 integ_key_len * 2 + /* SK_ai, SK_ar */
465 tr_encr->key_len * 2 + /* SK_ei, SK_er */
466 tr_prf->key_len * 2; /* SK_pi, SK_pr */
468 keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
475 sa->sk_d = vec_new (u8, tr_prf->key_trunc);
476 clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
477 pos += tr_prf->key_trunc;
482 sa->sk_ai = vec_new (u8, integ_key_len);
483 clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
484 pos += integ_key_len;
487 sa->sk_ar = vec_new (u8, integ_key_len);
488 clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
489 pos += integ_key_len;
493 sa->sk_ei = vec_new (u8, tr_encr->key_len);
494 clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len);
495 pos += tr_encr->key_len;
498 sa->sk_er = vec_new (u8, tr_encr->key_len);
499 clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len);
500 pos += tr_encr->key_len;
503 sa->sk_pi = vec_new (u8, tr_prf->key_len);
504 clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
505 pos += tr_prf->key_len;
508 sa->sk_pr = vec_new (u8, tr_prf->key_len);
509 clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
510 pos += tr_prf->key_len;
516 ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child)
519 u16 integ_key_len = 0;
522 ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
524 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
526 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
528 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
531 integ_key_len = ctr_integ->key_len;
533 salt_len = sizeof (u32);
535 vec_append (s, sa->i_nonce);
536 vec_append (s, sa->r_nonce);
537 /* calculate PRFplus */
539 int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
541 keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
546 child->sk_ei = vec_new (u8, ctr_encr->key_len);
547 clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
548 pos += ctr_encr->key_len;
553 child->sk_ai = vec_new (u8, ctr_integ->key_len);
554 clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
555 pos += ctr_integ->key_len;
559 clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
564 child->sk_er = vec_new (u8, ctr_encr->key_len);
565 clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
566 pos += ctr_encr->key_len;
571 child->sk_ar = vec_new (u8, integ_key_len);
572 clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
573 pos += integ_key_len;
577 clib_memcpy (&child->salt_er, keymat + pos, salt_len);
587 ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
591 u32 len = clib_net_to_host_u32 (ike->length);
592 u8 payload = ike->nextpayload;
594 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
596 clib_net_to_host_u64 (ike->ispi),
597 clib_net_to_host_u64 (ike->rspi), sa->iaddr.as_u32);
599 sa->ispi = clib_net_to_host_u64 (ike->ispi);
601 /* store whole IKE payload - needed for PSK auth */
602 vec_free (sa->last_sa_init_req_packet_data);
603 vec_add (sa->last_sa_init_req_packet_data, ike, len);
605 while (p < len && payload != IKEV2_PAYLOAD_NONE)
607 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
608 u32 plen = clib_net_to_host_u16 (ikep->length);
610 if (plen < sizeof (ike_payload_header_t))
613 if (payload == IKEV2_PAYLOAD_SA)
615 ikev2_sa_free_proposal_vector (&sa->i_proposals);
616 sa->i_proposals = ikev2_parse_sa_payload (ikep);
618 else if (payload == IKEV2_PAYLOAD_KE)
620 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
621 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
622 vec_free (sa->i_dh_data);
623 vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
625 else if (payload == IKEV2_PAYLOAD_NONCE)
627 vec_free (sa->i_nonce);
628 vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
630 else if (payload == IKEV2_PAYLOAD_NOTIFY)
632 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
635 else if (payload == IKEV2_PAYLOAD_VENDOR)
637 ikev2_parse_vendor_payload (ikep);
641 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
643 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
645 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
646 sa->unsupported_cp = payload;
651 payload = ikep->nextpayload;
655 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
659 ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa,
663 u32 len = clib_net_to_host_u32 (ike->length);
664 u8 payload = ike->nextpayload;
666 sa->ispi = clib_net_to_host_u64 (ike->ispi);
667 sa->rspi = clib_net_to_host_u64 (ike->rspi);
669 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
670 "from %d.%d.%d.%d", sa->ispi, sa->rspi,
673 /* store whole IKE payload - needed for PSK auth */
674 vec_free (sa->last_sa_init_res_packet_data);
675 vec_add (sa->last_sa_init_res_packet_data, ike, len);
677 while (p < len && payload != IKEV2_PAYLOAD_NONE)
679 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
680 u32 plen = clib_net_to_host_u16 (ikep->length);
682 if (plen < sizeof (ike_payload_header_t))
685 if (payload == IKEV2_PAYLOAD_SA)
687 ikev2_sa_free_proposal_vector (&sa->r_proposals);
688 sa->r_proposals = ikev2_parse_sa_payload (ikep);
691 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
693 clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
696 else if (payload == IKEV2_PAYLOAD_KE)
698 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
699 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
700 vec_free (sa->r_dh_data);
701 vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
703 else if (payload == IKEV2_PAYLOAD_NONCE)
705 vec_free (sa->r_nonce);
706 vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
708 else if (payload == IKEV2_PAYLOAD_NOTIFY)
710 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
713 else if (payload == IKEV2_PAYLOAD_VENDOR)
715 ikev2_parse_vendor_payload (ikep);
719 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
721 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
723 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
724 sa->unsupported_cp = payload;
729 payload = ikep->nextpayload;
735 ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
740 u32 len = clib_net_to_host_u32 (ike->length);
741 ike_payload_header_t *ikep = 0;
743 ikev2_sa_transform_t *tr_integ;
745 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
748 *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
750 ikep = (ike_payload_header_t *) & ike->payload[p];
751 plen = clib_net_to_host_u16 (ikep->length);
753 if (plen < sizeof (*ikep))
756 if (*payload == IKEV2_PAYLOAD_SK)
758 last_payload = *payload;
762 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
764 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
766 sa->unsupported_cp = *payload;
771 *payload = ikep->nextpayload;
775 if (last_payload != IKEV2_PAYLOAD_SK)
777 ikev2_elog_error ("Last payload must be SK");
782 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
783 (u8 *) ike, len - tr_integ->key_trunc);
785 plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
787 if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
789 ikev2_elog_error ("message integrity check failed");
795 return ikev2_decrypt_data (sa, ikep->payload, plen);
799 ikev2_initial_contact_cleanup (ikev2_sa_t * sa)
801 ikev2_main_t *km = &ikev2_main;
805 u32 thread_index = vlib_get_thread_index ();
807 if (!sa->initial_contact)
810 /* find old IKE SAs with the same authenticated identity */
812 pool_foreach (tmp, km->per_thread_data[thread_index].sas, ({
813 if (tmp->i_id.type != sa->i_id.type ||
814 vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
815 memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
818 if (sa->rspi != tmp->rspi)
819 vec_add1(delete, tmp - km->per_thread_data[thread_index].sas);
823 for (i = 0; i < vec_len (delete); i++)
826 pool_elt_at_index (km->per_thread_data[thread_index].sas, delete[i]);
828 tmp->childs) ikev2_delete_tunnel_interface (km->vnet_main,
830 ikev2_delete_sa (tmp);
834 sa->initial_contact = 0;
838 ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
840 ikev2_child_sa_t *first_child_sa;
842 u8 payload = ike->nextpayload;
844 ike_payload_header_t *ikep;
847 ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
848 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
849 clib_host_to_net_u64 (ike->rspi),
850 sa->is_initiator ? sa->raddr.as_u32 : sa->
853 ikev2_calc_keys (sa);
855 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
859 if (sa->unsupported_cp)
860 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
861 goto cleanup_and_exit;
864 /* select or create 1st child SA */
865 if (sa->is_initiator)
867 first_child_sa = &sa->childs[0];
871 ikev2_sa_free_all_child_sa (&sa->childs);
872 vec_add2 (sa->childs, first_child_sa, 1);
876 /* process encrypted payload */
878 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
880 ikep = (ike_payload_header_t *) & plaintext[p];
881 plen = clib_net_to_host_u16 (ikep->length);
883 if (plen < sizeof (ike_payload_header_t))
884 goto cleanup_and_exit;
886 if (payload == IKEV2_PAYLOAD_SA) /* 33 */
888 if (sa->is_initiator)
890 ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
891 first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
895 ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
896 first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
899 else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
901 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
903 sa->i_id.type = id->id_type;
904 vec_free (sa->i_id.data);
905 vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
907 else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
909 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
911 sa->r_id.type = id->id_type;
912 vec_free (sa->r_id.data);
913 vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
915 else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
917 ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
919 if (sa->is_initiator)
921 sa->r_auth.method = a->auth_method;
922 vec_free (sa->r_auth.data);
923 vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
927 sa->i_auth.method = a->auth_method;
928 vec_free (sa->i_auth.data);
929 vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
932 else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
934 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
935 if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
937 sa->initial_contact = 1;
941 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
943 ikev2_parse_vendor_payload (ikep);
945 else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
947 vec_free (first_child_sa->tsi);
948 first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
950 else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
952 vec_free (first_child_sa->tsr);
953 first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
957 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
960 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
962 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
963 sa->unsupported_cp = payload;
968 payload = ikep->nextpayload;
973 vec_free (plaintext);
977 ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa,
981 u8 payload = ike->nextpayload;
983 ike_payload_header_t *ikep;
986 ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
987 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
988 clib_host_to_net_u64 (ike->rspi), sa->iaddr.as_u32);
990 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
993 goto cleanup_and_exit;
995 /* process encrypted payload */
997 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
999 ikep = (ike_payload_header_t *) & plaintext[p];
1000 plen = clib_net_to_host_u16 (ikep->length);
1002 if (plen < sizeof (ike_payload_header_t))
1003 goto cleanup_and_exit;
1005 if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1007 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
1008 if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1009 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1012 else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1014 sa->del = ikev2_parse_delete_payload (ikep);
1016 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1018 ikev2_parse_vendor_payload (ikep);
1022 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1024 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1026 sa->unsupported_cp = payload;
1031 payload = ikep->nextpayload;
1036 vec_free (plaintext);
1040 ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
1044 u8 payload = ike->nextpayload;
1047 u8 nonce[IKEV2_NONCE_SIZE];
1049 ike_payload_header_t *ikep;
1051 ikev2_notify_t *n = 0;
1052 ikev2_ts_t *tsi = 0;
1053 ikev2_ts_t *tsr = 0;
1054 ikev2_sa_proposal_t *proposal = 0;
1055 ikev2_child_sa_t *child_sa;
1057 ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received "
1058 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
1059 clib_host_to_net_u64 (ike->rspi), sa->raddr.as_u32);
1061 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
1064 goto cleanup_and_exit;
1066 /* process encrypted payload */
1068 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1070 ikep = (ike_payload_header_t *) & plaintext[p];
1071 plen = clib_net_to_host_u16 (ikep->length);
1073 if (plen < sizeof (ike_payload_header_t))
1074 goto cleanup_and_exit;
1076 else if (payload == IKEV2_PAYLOAD_SA)
1078 proposal = ikev2_parse_sa_payload (ikep);
1080 else if (payload == IKEV2_PAYLOAD_NOTIFY)
1082 n = ikev2_parse_notify_payload (ikep);
1083 if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1088 else if (payload == IKEV2_PAYLOAD_DELETE)
1090 sa->del = ikev2_parse_delete_payload (ikep);
1092 else if (payload == IKEV2_PAYLOAD_VENDOR)
1094 ikev2_parse_vendor_payload (ikep);
1096 else if (payload == IKEV2_PAYLOAD_NONCE)
1098 clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
1100 else if (payload == IKEV2_PAYLOAD_TSI)
1102 tsi = ikev2_parse_ts_payload (ikep);
1104 else if (payload == IKEV2_PAYLOAD_TSR)
1106 tsr = ikev2_parse_ts_payload (ikep);
1110 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1112 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1114 sa->unsupported_cp = payload;
1119 payload = ikep->nextpayload;
1123 if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1125 ikev2_rekey_t *rekey = &sa->rekey[0];
1126 rekey->protocol_id = proposal->protocol_id;
1128 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1129 rekey->i_proposal->spi = rekey->spi;
1130 rekey->r_proposal = proposal;
1134 vec_free (sa->r_nonce);
1135 vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1136 child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1139 child_sa->rekey_retries = 0;
1144 ikev2_rekey_t *rekey;
1145 child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1148 ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1150 goto cleanup_and_exit;
1152 vec_add2 (sa->rekey, rekey, 1);
1153 rekey->protocol_id = n->protocol_id;
1154 rekey->spi = n->spi;
1155 rekey->i_proposal = proposal;
1157 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1161 vec_free (sa->i_nonce);
1162 vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1163 /* generate new Nr */
1164 vec_free (sa->r_nonce);
1165 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
1166 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1170 vec_free (plaintext);
1175 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1183 ikev2_sa_transform_t *tr_prf;
1186 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1192 nonce = sa->i_nonce;
1193 packet_data = sa->last_sa_init_res_packet_data;
1199 nonce = sa->r_nonce;
1200 packet_data = sa->last_sa_init_req_packet_data;
1203 data = vec_new (u8, 4);
1205 vec_append (data, id->data);
1207 u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1208 vec_append (authmsg, packet_data);
1209 vec_append (authmsg, nonce);
1210 vec_append (authmsg, id_hash);
1218 ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
1220 if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1221 ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1222 ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1223 ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1230 ikev2_sa_match_ts (ikev2_sa_t * sa)
1232 ikev2_main_t *km = &ikev2_main;
1234 ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1238 pool_foreach (p, km->profiles, ({
1240 if (sa->is_initiator)
1254 if (p->rem_id.type != id->type ||
1255 vec_len(p->rem_id.data) != vec_len(id->data) ||
1256 memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
1259 vec_foreach(ts, sa->childs[0].tsi)
1261 if (ikev2_ts_cmp(p_tsi, ts))
1263 vec_add1 (tsi, ts[0]);
1268 vec_foreach(ts, sa->childs[0].tsr)
1270 if (ikev2_ts_cmp(p_tsr, ts))
1272 vec_add1 (tsr, ts[0]);
1283 vec_free (sa->childs[0].tsi);
1284 vec_free (sa->childs[0].tsr);
1285 sa->childs[0].tsi = tsi;
1286 sa->childs[0].tsr = tsr;
1292 ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
1297 ikev2_sa_auth (ikev2_sa_t * sa)
1299 ikev2_main_t *km = &ikev2_main;
1300 ikev2_profile_t *p, *sel_p = 0;
1301 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1302 ikev2_sa_transform_t *tr_prf;
1305 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1307 /* only shared key and rsa signature */
1308 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1309 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1311 ikev2_elog_uint (IKEV2_LOG_ERROR,
1312 "unsupported authentication method %u",
1314 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1318 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1319 authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1322 ikev2_auth_t *sa_auth;
1324 if (sa->is_initiator)
1327 sa_auth = &sa->r_auth;
1332 sa_auth = &sa->i_auth;
1336 pool_foreach (p, km->profiles, ({
1339 if (p->rem_id.type != sa_id->type ||
1340 vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
1341 memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
1344 if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1346 if (!p->auth.data ||
1347 p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1350 psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1351 auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1353 if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
1355 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1362 else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1364 if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1367 if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
1369 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1382 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1384 if (!sa->is_initiator)
1386 vec_free (sa->r_id.data);
1387 sa->r_id.data = vec_dup (sel_p->loc_id.data);
1388 sa->r_id.type = sel_p->loc_id.type;
1390 /* generate our auth data */
1391 authmsg = ikev2_sa_generate_authmsg (sa, 1);
1392 if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1394 sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1395 sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1397 else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1399 sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1400 sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1404 /* select transforms for 1st child sa */
1405 ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1406 sa->childs[0].r_proposals =
1407 ikev2_select_proposal (sa->childs[0].i_proposals,
1408 IKEV2_PROTOCOL_ESP);
1410 if (~0 != sel_p->tun_itf)
1412 sa->is_tun_itf_set = 1;
1413 sa->tun_itf = sel_p->tun_itf;
1419 ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1420 "profile found! ispi %lx", sa->ispi);
1421 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1429 ikev2_sa_auth_init (ikev2_sa_t * sa)
1431 ikev2_main_t *km = &ikev2_main;
1432 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1433 ikev2_sa_transform_t *tr_prf;
1436 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1438 /* only shared key and rsa signature */
1439 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1440 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1442 ikev2_elog_uint (IKEV2_LOG_ERROR,
1443 "unsupported authentication method %u",
1445 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1449 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1450 authmsg = ikev2_sa_generate_authmsg (sa, 0);
1451 psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1452 auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1455 if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1457 sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1458 sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1460 else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1462 sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1463 sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1473 ikev2_mk_local_sa_id (u32 sai, u32 ci, u32 ti)
1475 return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1479 ikev2_mk_remote_sa_id (u32 sai, u32 ci, u32 ti)
1481 return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1491 ipsec_sa_flags_t flags;
1494 ipsec_crypto_alg_t encr_type;
1495 ipsec_integ_alg_t integ_type;
1496 ip46_address_t local_ip;
1497 ip46_address_t remote_ip;
1498 ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1500 u32 old_remote_sa_id;
1501 } ikev2_add_ipsec_tunnel_args_t;
1504 ikev2_add_tunnel_from_main (ikev2_add_ipsec_tunnel_args_t * a)
1506 ikev2_main_t *km = &ikev2_main;
1510 if (~0 == a->sw_if_index)
1512 /* no tunnel associated with the SA/profile - create a new one */
1513 rv = ipip_add_tunnel (IPIP_TRANSPORT_IP4, ~0,
1514 &a->local_ip, &a->remote_ip, 0,
1515 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
1516 TUNNEL_MODE_P2P, &sw_if_index);
1518 if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
1520 if (hash_get (km->sw_if_indices, sw_if_index))
1521 /* interface is managed by IKE; proceed with updating SAs */
1524 hash_set1 (km->sw_if_indices, sw_if_index);
1528 sw_if_index = a->sw_if_index;
1529 vnet_sw_interface_admin_up (vnet_get_main (), sw_if_index);
1534 ikev2_elog_peers (IKEV2_LOG_ERROR, "installing ipip tunnel failed! "
1535 "loc:%d.%d.%d.%d rem:%d.%d.%d.%d",
1536 a->local_ip.ip4.as_u32, a->remote_ip.ip4.as_u32);
1541 vec_add1 (sas_in, a->remote_sa_id);
1544 ipsec_tun_protect_del (sw_if_index, NULL);
1546 /* replace local SA immediately */
1547 ipsec_sa_unlock_id (a->local_sa_id);
1549 /* keep the old sa */
1550 vec_add1 (sas_in, a->old_remote_sa_id);
1553 rv |= ipsec_sa_add_and_lock (a->local_sa_id,
1555 IPSEC_PROTOCOL_ESP, a->encr_type,
1556 &a->loc_ckey, a->integ_type, &a->loc_ikey,
1557 a->flags, 0, a->salt_local, &a->local_ip,
1558 &a->remote_ip, NULL);
1559 rv |= ipsec_sa_add_and_lock (a->remote_sa_id, a->remote_spi,
1560 IPSEC_PROTOCOL_ESP, a->encr_type, &a->rem_ckey,
1561 a->integ_type, &a->rem_ikey,
1562 (a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0,
1563 a->salt_remote, &a->remote_ip,
1564 &a->local_ip, NULL);
1566 rv |= ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
1570 ikev2_create_tunnel_interface (vnet_main_t * vnm,
1573 ikev2_child_sa_t * child, u32 sa_index,
1574 u32 child_index, u8 is_rekey)
1576 ikev2_main_t *km = &ikev2_main;
1577 ipsec_crypto_alg_t encr_type;
1578 ipsec_integ_alg_t integ_type;
1579 ikev2_profile_t *p = 0;
1580 ikev2_sa_transform_t *tr;
1581 ikev2_sa_proposal_t *proposals;
1583 ikev2_add_ipsec_tunnel_args_t a;
1585 clib_memset (&a, 0, sizeof (a));
1587 if (!child->r_proposals)
1589 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1593 if (sa->is_initiator)
1595 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1596 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1597 proposals = child->i_proposals;
1598 a.local_spi = child->r_proposals[0].spi;
1599 a.remote_spi = child->i_proposals[0].spi;
1603 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1604 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1605 proposals = child->r_proposals;
1606 a.local_spi = child->i_proposals[0].spi;
1607 a.remote_spi = child->r_proposals[0].spi;
1610 a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
1611 a.is_rekey = is_rekey;
1613 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
1614 if (tr && tr->esn_type)
1615 a.flags |= IPSEC_SA_FLAG_USE_ESN;
1617 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1620 if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
1622 switch (tr->key_len)
1625 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1628 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1631 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1634 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1639 else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
1642 switch (tr->key_len)
1645 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
1648 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
1651 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
1654 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1662 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1668 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1671 a.encr_type = encr_type;
1675 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1678 switch (tr->integ_type)
1680 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
1681 integ_type = IPSEC_INTEG_ALG_SHA_256_128;
1683 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
1684 integ_type = IPSEC_INTEG_ALG_SHA_384_192;
1686 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
1687 integ_type = IPSEC_INTEG_ALG_SHA_512_256;
1689 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
1690 integ_type = IPSEC_INTEG_ALG_SHA1_96;
1693 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1699 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1705 integ_type = IPSEC_INTEG_ALG_NONE;
1708 a.integ_type = integ_type;
1709 ikev2_calc_child_keys (sa, child);
1711 if (sa->is_initiator)
1713 ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
1714 ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
1715 ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
1716 ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
1719 a.salt_remote = child->salt_er;
1720 a.salt_local = child->salt_ei;
1725 ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
1726 ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
1727 ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
1728 ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
1731 a.salt_remote = child->salt_ei;
1732 a.salt_local = child->salt_er;
1736 if (sa->is_profile_index_set)
1737 p = pool_elt_at_index (km->profiles, sa->profile_index);
1739 if (p && p->lifetime)
1741 child->time_to_expiration =
1742 vlib_time_now (vnm->vlib_main) + p->lifetime;
1743 if (p->lifetime_jitter)
1745 // This is not much better than rand(3), which Coverity warns
1746 // is unsuitable for security applications; random_u32 is
1747 // however fast. If this perturbance to the expiration time
1748 // needs to use a better RNG then we may need to use something
1749 // like /dev/urandom which has significant overhead.
1750 u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
1751 rnd = random_u32 (&rnd);
1753 child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
1757 if (thread_index & 0xffffffc0)
1758 ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
1760 if (child_index & 0xfffff000 || sa_index & 0xfffff000)
1761 ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
1763 child->local_sa_id =
1765 ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
1767 u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
1772 /* create a new remote SA ID to keep the old SA for a bit longer
1773 * so the peer has some time to swap their SAs */
1775 /* use most significat bit of child index part in id */
1777 if (sa->current_remote_id_mask)
1779 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
1780 sa->current_remote_id_mask = 0;
1784 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
1785 sa->current_remote_id_mask = mask;
1786 remote_sa_id |= mask;
1788 sa->old_id_expiration = 3.0;
1789 sa->old_remote_id_present = 1;
1792 child->remote_sa_id = a.remote_sa_id = remote_sa_id;
1794 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1796 vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main,
1797 (u8 *) & a, sizeof (a));
1803 ip46_address_t local_ip;
1804 ip46_address_t remote_ip;
1808 } ikev2_del_ipsec_tunnel_args_t;
1810 static_always_inline u32
1811 ikev2_flip_alternate_sa_bit (u32 id)
1820 ikev2_del_tunnel_from_main (ikev2_del_ipsec_tunnel_args_t * a)
1822 ikev2_main_t *km = &ikev2_main;
1823 ipip_tunnel_t *ipip = NULL;
1826 if (~0 == a->sw_if_index)
1829 ipip_tunnel_key_t key = {
1831 .dst = a->remote_ip,
1832 .transport = IPIP_TRANSPORT_IP4,
1837 ipip = ipip_tunnel_db_find (&key);
1841 sw_if_index = ipip->sw_if_index;
1842 hash_unset (km->sw_if_indices, ipip->sw_if_index);
1849 sw_if_index = a->sw_if_index;
1850 vnet_sw_interface_admin_down (vnet_get_main (), sw_if_index);
1853 if (~0 != sw_if_index)
1854 ipsec_tun_protect_del (sw_if_index, NULL);
1856 ipsec_sa_unlock_id (a->remote_sa_id);
1857 ipsec_sa_unlock_id (a->local_sa_id);
1858 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (a->remote_sa_id));
1861 ipip_del_tunnel (ipip->sw_if_index);
1865 ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1866 ikev2_child_sa_t * child)
1868 ikev2_del_ipsec_tunnel_args_t a;
1870 clib_memset (&a, 0, sizeof (a));
1872 if (sa->is_initiator)
1874 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1875 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1879 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1880 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1883 a.remote_sa_id = child->remote_sa_id;
1884 a.local_sa_id = child->local_sa_id;
1885 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1887 vl_api_rpc_call_main_thread (ikev2_del_tunnel_from_main, (u8 *) & a,
1893 ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
1896 ike_payload_header_t *ph;
1900 ikev2_sa_transform_t *tr_encr, *tr_integ;
1902 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1904 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1906 ikev2_payload_chain_t *chain = 0;
1907 ikev2_payload_new_chain (chain);
1909 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1911 if (sa->r_proposals == 0)
1913 ikev2_payload_add_notify (chain,
1914 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1915 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1917 else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
1919 u8 *data = vec_new (u8, 2);
1920 ikev2_sa_transform_t *tr_dh;
1922 ikev2_sa_get_td_for_type (sa->r_proposals,
1923 IKEV2_TRANSFORM_TYPE_DH);
1924 ASSERT (tr_dh && tr_dh->dh_type);
1926 data[0] = (tr_dh->dh_type >> 8) & 0xff;
1927 data[1] = (tr_dh->dh_type) & 0xff;
1929 ikev2_payload_add_notify (chain,
1930 IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1933 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1935 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1937 u8 *data = vec_new (u8, 1);
1939 data[0] = sa->unsupported_cp;
1940 ikev2_payload_add_notify (chain,
1941 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1947 ike->rspi = clib_host_to_net_u64 (sa->rspi);
1948 ikev2_payload_add_sa (chain, sa->r_proposals);
1949 ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1950 ikev2_payload_add_nonce (chain, sa->r_nonce);
1953 else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1955 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1957 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1958 ikev2_payload_add_auth (chain, &sa->r_auth);
1959 ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1960 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1961 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1963 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
1965 ikev2_payload_add_notify (chain,
1966 IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1968 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1970 else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
1972 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1974 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1975 ikev2_payload_add_auth (chain, &sa->r_auth);
1977 else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
1979 ikev2_payload_add_notify (chain,
1980 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1981 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1982 ikev2_payload_add_auth (chain, &sa->r_auth);
1983 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1984 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1986 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1988 u8 *data = vec_new (u8, 1);
1990 data[0] = sa->unsupported_cp;
1991 ikev2_payload_add_notify (chain,
1992 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1996 else if (sa->state == IKEV2_STATE_SA_INIT)
1998 ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
1999 ikev2_payload_add_auth (chain, &sa->i_auth);
2000 ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2001 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2002 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2003 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2008 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2012 else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2014 /* if pending delete */
2017 if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2019 if (sa->is_initiator)
2020 ikev2_payload_add_delete (chain, sa->del);
2022 /* The response to a request that deletes the IKE SA is an empty
2023 INFORMATIONAL response. */
2024 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2026 /* The response to a request that deletes ESP or AH SAs will contain
2027 delete payloads for the paired SAs going in the other direction. */
2030 ikev2_payload_add_delete (chain, sa->del);
2035 /* received N(AUTHENTICATION_FAILED) */
2036 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2038 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2041 /* received unsupported critical payload */
2042 else if (sa->unsupported_cp)
2044 u8 *data = vec_new (u8, 1);
2046 data[0] = sa->unsupported_cp;
2047 ikev2_payload_add_notify (chain,
2048 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2051 sa->unsupported_cp = 0;
2053 /* else send empty response */
2055 else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2057 if (sa->is_initiator)
2060 ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2061 ikev2_notify_t notify;
2062 u8 *data = vec_new (u8, 4);
2063 clib_memset (¬ify, 0, sizeof (notify));
2064 notify.protocol_id = IKEV2_PROTOCOL_ESP;
2065 notify.spi = sa->childs[0].i_proposals->spi;
2066 *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2068 ikev2_payload_add_sa (chain, proposals);
2069 ikev2_payload_add_nonce (chain, sa->i_nonce);
2070 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2071 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2072 ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2081 ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
2082 ikev2_payload_add_nonce (chain, sa->r_nonce);
2083 ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
2085 ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
2087 vec_del1 (sa->rekey, 0);
2089 else if (sa->unsupported_cp)
2091 u8 *data = vec_new (u8, 1);
2093 data[0] = sa->unsupported_cp;
2094 ikev2_payload_add_notify (chain,
2095 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2098 sa->unsupported_cp = 0;
2102 ikev2_payload_add_notify (chain,
2103 IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2110 ike->version = IKE_VERSION_2;
2111 ike->nextpayload = IKEV2_PAYLOAD_SK;
2112 tlen = sizeof (*ike);
2113 if (sa->is_initiator)
2115 ike->flags = IKEV2_HDR_FLAG_INITIATOR;
2116 sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
2120 ike->flags = IKEV2_HDR_FLAG_RESPONSE;
2124 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2126 tlen += vec_len (chain->data);
2127 ike->nextpayload = chain->first_payload_type;
2128 ike->length = clib_host_to_net_u32 (tlen);
2129 clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2131 /* store whole IKE payload - needed for PSK auth */
2132 vec_free (sa->last_sa_init_res_packet_data);
2133 vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
2138 ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2141 plen = sizeof (*ph);
2142 ph = (ike_payload_header_t *) & ike->payload[0];
2143 ph->nextpayload = chain->first_payload_type;
2145 int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
2148 /* add space for hmac */
2149 plen += tr_integ->key_trunc;
2152 /* payload and total length */
2153 ph->length = clib_host_to_net_u16 (plen);
2154 ike->length = clib_host_to_net_u32 (tlen);
2156 /* calc integrity data for whole packet except hash itself */
2158 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2159 (u8 *) ike, tlen - tr_integ->key_trunc);
2161 clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2162 sizeof (*ike), integ, tr_integ->key_trunc);
2164 /* store whole IKE payload - needed for retransmit */
2165 vec_free (sa->last_res_packet_data);
2166 vec_add (sa->last_res_packet_data, ike, tlen);
2170 ikev2_payload_destroy_chain (chain);
2176 ikev2_retransmit_sa_init (ike_header_t * ike,
2177 ip4_address_t iaddr, ip4_address_t raddr)
2179 ikev2_main_t *km = &ikev2_main;
2181 u32 thread_index = vlib_get_thread_index ();
2184 pool_foreach (sa, km->per_thread_data[thread_index].sas, ({
2185 if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
2186 sa->iaddr.as_u32 == iaddr.as_u32 &&
2187 sa->raddr.as_u32 == raddr.as_u32)
2190 u32 len = clib_net_to_host_u32(ike->length);
2191 u8 payload = ike->nextpayload;
2193 while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
2194 ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
2195 u32 plen = clib_net_to_host_u16(ikep->length);
2197 if (plen < sizeof(ike_payload_header_t))
2200 if (payload == IKEV2_PAYLOAD_NONCE)
2202 if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
2204 /* req is retransmit */
2205 if (sa->state == IKEV2_STATE_SA_INIT)
2208 tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
2209 ike->ispi = tmp->ispi;
2210 ike->rspi = tmp->rspi;
2211 ike->nextpayload = tmp->nextpayload;
2212 ike->version = tmp->version;
2213 ike->exchange = tmp->exchange;
2214 ike->flags = tmp->flags;
2215 ike->msgid = tmp->msgid;
2216 ike->length = tmp->length;
2217 clib_memcpy_fast(ike->payload, tmp->payload,
2218 clib_net_to_host_u32(tmp->length) - sizeof(*ike));
2219 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2220 "ispi %lx IKE_SA_INIT retransmit "
2221 "from %d.%d.%d.%d to %d.%d.%d.%d",
2223 raddr.as_u32, iaddr.as_u32);
2226 /* else ignore req */
2229 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2230 "ispi %lx IKE_SA_INIT ignore "
2231 "from %d.%d.%d.%d to %d.%d.%d.%d",
2233 raddr.as_u32, iaddr.as_u32);
2238 payload = ikep->nextpayload;
2245 /* req is not retransmit */
2250 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2252 u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2255 if (msg_id > sa->last_msg_id)
2257 sa->last_msg_id = msg_id;
2260 /* retransmitted req */
2261 else if (msg_id == sa->last_msg_id)
2264 tmp = (ike_header_t *) sa->last_res_packet_data;
2265 ike->ispi = tmp->ispi;
2266 ike->rspi = tmp->rspi;
2267 ike->nextpayload = tmp->nextpayload;
2268 ike->version = tmp->version;
2269 ike->exchange = tmp->exchange;
2270 ike->flags = tmp->flags;
2271 ike->msgid = tmp->msgid;
2272 ike->length = tmp->length;
2273 clib_memcpy_fast (ike->payload, tmp->payload,
2274 clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
2275 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2276 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2279 /* old req ignore */
2282 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2283 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2290 ikev2_node_fn (vlib_main_t * vm,
2291 vlib_node_runtime_t * node, vlib_frame_t * frame)
2293 u32 n_left_from, *from, *to_next;
2294 ikev2_next_t next_index;
2295 ikev2_main_t *km = &ikev2_main;
2296 u32 thread_index = vlib_get_thread_index ();
2298 from = vlib_frame_vector_args (frame);
2299 n_left_from = frame->n_vectors;
2300 next_index = node->cached_next_index;
2302 while (n_left_from > 0)
2306 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2308 while (n_left_from > 0 && n_left_to_next > 0)
2312 u32 next0 = IKEV2_NEXT_ERROR_DROP;
2317 ikev2_sa_t *sa0 = 0;
2318 ikev2_sa_t sa; /* temporary store for SA */
2322 /* speculatively enqueue b0 to the current next frame */
2328 n_left_to_next -= 1;
2330 b0 = vlib_get_buffer (vm, bi0);
2331 ike0 = vlib_buffer_get_current (b0);
2332 vlib_buffer_advance (b0, -sizeof (*udp0));
2333 udp0 = vlib_buffer_get_current (b0);
2334 vlib_buffer_advance (b0, -sizeof (*ip40));
2335 ip40 = vlib_buffer_get_current (b0);
2337 if (ike0->version != IKE_VERSION_2)
2339 vlib_node_increment_counter (vm, ikev2_node.index,
2340 IKEV2_ERROR_NOT_IKEV2, 1);
2344 if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2347 clib_memset (sa0, 0, sizeof (*sa0));
2349 if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
2351 if (ike0->rspi == 0)
2353 sa0->raddr.as_u32 = ip40->dst_address.as_u32;
2354 sa0->iaddr.as_u32 = ip40->src_address.as_u32;
2356 r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2360 vlib_node_increment_counter (vm, ikev2_node.index,
2361 IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2363 len = clib_net_to_host_u32 (ike0->length);
2368 vlib_node_increment_counter (vm, ikev2_node.index,
2369 IKEV2_ERROR_IKE_SA_INIT_IGNORE,
2374 ikev2_process_sa_init_req (vm, sa0, ike0);
2376 if (sa0->state == IKEV2_STATE_SA_INIT)
2378 ikev2_sa_free_proposal_vector (&sa0->r_proposals);
2380 ikev2_select_proposal (sa0->i_proposals,
2381 IKEV2_PROTOCOL_IKE);
2382 ikev2_generate_sa_init_data (sa0);
2385 if (sa0->state == IKEV2_STATE_SA_INIT
2386 || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2388 len = ikev2_generate_message (sa0, ike0, 0);
2391 if (sa0->state == IKEV2_STATE_SA_INIT)
2393 /* add SA to the pool */
2394 pool_get (km->per_thread_data[thread_index].sas,
2396 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2398 per_thread_data[thread_index].sa_by_rspi,
2401 km->per_thread_data[thread_index].sas);
2405 ikev2_sa_free_all_vec (sa0);
2409 else //received sa_init without initiator flag
2411 ikev2_process_sa_init_resp (vm, sa0, ike0);
2413 if (sa0->state == IKEV2_STATE_SA_INIT)
2415 ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
2416 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2420 pool_elt_at_index (km->sais, p[0]);
2422 ikev2_complete_sa_data (sa0, sai);
2423 ikev2_calc_keys (sa0);
2424 ikev2_sa_auth_init (sa0);
2425 len = ikev2_generate_message (sa0, ike0, 0);
2429 if (sa0->state == IKEV2_STATE_SA_INIT)
2431 /* add SA to the pool */
2432 pool_get (km->per_thread_data[thread_index].sas, sa0);
2433 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2434 hash_set (km->per_thread_data[thread_index].sa_by_rspi,
2436 sa0 - km->per_thread_data[thread_index].sas);
2440 ikev2_sa_free_all_vec (sa0);
2444 else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2447 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2448 clib_net_to_host_u64 (ike0->rspi));
2452 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2455 r = ikev2_retransmit_resp (sa0, ike0);
2458 vlib_node_increment_counter (vm, ikev2_node.index,
2459 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2461 len = clib_net_to_host_u32 (ike0->length);
2466 vlib_node_increment_counter (vm, ikev2_node.index,
2467 IKEV2_ERROR_IKE_REQ_IGNORE,
2472 ikev2_process_auth_req (vm, sa0, ike0);
2473 ikev2_sa_auth (sa0);
2474 if (sa0->state == IKEV2_STATE_AUTHENTICATED)
2476 ikev2_initial_contact_cleanup (sa0);
2477 ikev2_sa_match_ts (sa0);
2478 if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
2479 ikev2_create_tunnel_interface (km->vnet_main,
2485 if (sa0->is_initiator)
2487 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2491 pool_elt_at_index (km->sais, p[0]);
2492 hash_unset (km->sa_by_ispi, sai->ispi);
2493 ikev2_sa_free_all_vec (sai);
2494 pool_put (km->sais, sai);
2499 len = ikev2_generate_message (sa0, ike0, 0);
2503 else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2506 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2507 clib_net_to_host_u64 (ike0->rspi));
2511 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2514 r = ikev2_retransmit_resp (sa0, ike0);
2517 vlib_node_increment_counter (vm, ikev2_node.index,
2518 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2520 len = clib_net_to_host_u32 (ike0->length);
2525 vlib_node_increment_counter (vm, ikev2_node.index,
2526 IKEV2_ERROR_IKE_REQ_IGNORE,
2531 ikev2_process_informational_req (vm, sa0, ike0);
2534 if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
2536 ikev2_delete_t *d, *tmp, *resp = 0;
2537 vec_foreach (d, sa0->del)
2539 ikev2_child_sa_t *ch_sa;
2540 ch_sa = ikev2_sa_get_child (sa0, d->spi,
2542 !sa0->is_initiator);
2545 ikev2_delete_tunnel_interface (km->vnet_main,
2547 if (!sa0->is_initiator)
2549 vec_add2 (resp, tmp, 1);
2550 tmp->protocol_id = d->protocol_id;
2551 tmp->spi = ch_sa->r_proposals[0].spi;
2553 ikev2_sa_del_child_sa (sa0, ch_sa);
2556 if (!sa0->is_initiator)
2558 vec_free (sa0->del);
2563 if (!sa0->is_initiator)
2565 len = ikev2_generate_message (sa0, ike0, 0);
2569 else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2572 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2573 clib_net_to_host_u64 (ike0->rspi));
2577 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2580 r = ikev2_retransmit_resp (sa0, ike0);
2583 vlib_node_increment_counter (vm, ikev2_node.index,
2584 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2586 len = clib_net_to_host_u32 (ike0->length);
2591 vlib_node_increment_counter (vm, ikev2_node.index,
2592 IKEV2_ERROR_IKE_REQ_IGNORE,
2597 ikev2_process_create_child_sa_req (vm, sa0, ike0);
2600 if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
2603 vec_free (sa0->childs);
2604 ikev2_child_sa_t *child;
2605 vec_add2 (sa0->childs, child, 1);
2606 child->r_proposals = sa0->rekey[0].r_proposal;
2607 child->i_proposals = sa0->rekey[0].i_proposal;
2608 child->tsi = sa0->rekey[0].tsi;
2609 child->tsr = sa0->rekey[0].tsr;
2610 ikev2_create_tunnel_interface (km->vnet_main,
2613 child - sa0->childs,
2616 if (sa0->is_initiator)
2618 vec_del1 (sa0->rekey, 0);
2622 len = ikev2_generate_message (sa0, ike0, 0);
2629 ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2630 "received from %d.%d.%d.%d to %d.%d.%d.%d",
2632 ip40->src_address.as_u32,
2633 ip40->dst_address.as_u32);
2637 /* if we are sending packet back, rewrite headers */
2640 next0 = IKEV2_NEXT_IP4_LOOKUP;
2641 if (sa0->is_initiator)
2643 ip40->dst_address.as_u32 = sa0->raddr.as_u32;
2644 ip40->src_address.as_u32 = sa0->iaddr.as_u32;
2648 ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
2649 ip40->src_address.as_u32 = sa0->raddr.as_u32;
2652 clib_host_to_net_u16 (len + sizeof (udp_header_t));
2654 b0->current_length =
2655 len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2656 ip40->length = clib_host_to_net_u16 (b0->current_length);
2657 ip40->checksum = ip4_header_checksum (ip40);
2660 if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
2661 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
2663 ikev2_child_sa_t *c;
2665 vec_foreach (c, sa0->childs)
2666 ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
2668 ikev2_delete_sa (sa0);
2670 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2672 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2673 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2675 ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
2676 t->sw_if_index = sw_if_index0;
2677 t->next_index = next0;
2680 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2681 n_left_to_next, bi0, next0);
2684 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2687 vlib_node_increment_counter (vm, ikev2_node.index,
2688 IKEV2_ERROR_PROCESSED, frame->n_vectors);
2689 return frame->n_vectors;
2693 VLIB_REGISTER_NODE (ikev2_node,static) = {
2694 .function = ikev2_node_fn,
2696 .vector_size = sizeof (u32),
2697 .format_trace = format_ikev2_trace,
2698 .type = VLIB_NODE_TYPE_INTERNAL,
2700 .n_errors = ARRAY_LEN(ikev2_error_strings),
2701 .error_strings = ikev2_error_strings,
2703 .n_next_nodes = IKEV2_N_NEXT,
2706 [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
2707 [IKEV2_NEXT_ERROR_DROP] = "error-drop",
2712 // set ikev2 proposals when vpp is used as initiator
2713 static clib_error_t *
2714 ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
2715 ikev2_transforms_set * ts,
2716 ikev2_sa_proposal_t ** proposals, int is_ike)
2719 ikev2_main_t *km = &ikev2_main;
2720 ikev2_sa_proposal_t *proposal;
2721 vec_add2 (*proposals, proposal, 1);
2722 ikev2_sa_transform_t *td;
2727 vec_foreach (td, km->supported_transforms)
2729 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
2730 && td->encr_type == ts->crypto_alg
2731 && td->key_len == ts->crypto_key_size / 8)
2734 attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
2735 attr[1] = clib_host_to_net_u16 (td->key_len << 3);
2736 vec_add (td->attrs, (u8 *) attr, 4);
2737 vec_add1 (proposal->transforms, *td);
2746 r = clib_error_return (0, "Unsupported algorithm");
2752 vec_foreach (td, km->supported_transforms)
2754 if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
2755 && td->integ_type == ts->integ_alg)
2757 vec_add1 (proposal->transforms, *td);
2765 ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
2766 r = clib_error_return (0, "Unsupported algorithm");
2774 vec_foreach (td, km->supported_transforms)
2776 if (td->type == IKEV2_TRANSFORM_TYPE_PRF
2777 && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
2779 vec_add1 (proposal->transforms, *td);
2786 r = clib_error_return (0, "Unsupported algorithm");
2792 if (is_ike || ts->dh_type != IKEV2_TRANSFORM_DH_TYPE_NONE)
2795 vec_foreach (td, km->supported_transforms)
2797 if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
2799 vec_add1 (proposal->transforms, *td);
2802 sa->dh_group = td->dh_type;
2810 r = clib_error_return (0, "Unsupported algorithm");
2818 vec_foreach (td, km->supported_transforms)
2820 if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
2822 vec_add1 (proposal->transforms, *td);
2829 r = clib_error_return (0, "Unsupported algorithm");
2838 static ikev2_profile_t *
2839 ikev2_profile_index_by_name (u8 * name)
2841 ikev2_main_t *km = &ikev2_main;
2844 p = mhash_get (&km->profile_index_by_name, name);
2848 return pool_elt_at_index (km->profiles, p[0]);
2853 ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst,
2862 b0 = vlib_get_buffer (vm, bi0);
2863 vlib_buffer_advance (b0, -sizeof (udp_header_t));
2864 udp0 = vlib_buffer_get_current (b0);
2865 vlib_buffer_advance (b0, -sizeof (ip4_header_t));
2866 ip40 = vlib_buffer_get_current (b0);
2869 ip40->ip_version_and_header_length = 0x45;
2871 ip40->fragment_id = 0;
2872 ip40->flags_and_fragment_offset = 0;
2874 ip40->protocol = IP_PROTOCOL_UDP;
2875 ip40->dst_address.as_u32 = dst->as_u32;
2876 ip40->src_address.as_u32 = src->as_u32;
2877 udp0->dst_port = clib_host_to_net_u16 (500);
2878 udp0->src_port = clib_host_to_net_u16 (500);
2879 udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
2881 b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2882 ip40->length = clib_host_to_net_u16 (b0->current_length);
2883 ip40->checksum = ip4_header_checksum (ip40);
2886 /* send the request */
2887 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
2888 to_next = vlib_frame_vector_args (f);
2891 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
2896 ikev2_get_new_ike_header_buff (vlib_main_t * vm, ike_header_t ** ike)
2899 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2904 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
2905 *ike = vlib_buffer_get_current (b0);
2910 ikev2_set_local_key (vlib_main_t * vm, u8 * file)
2912 ikev2_main_t *km = &ikev2_main;
2914 km->pkey = ikev2_load_key_file (file);
2915 if (km->pkey == NULL)
2916 return clib_error_return (0, "load key '%s' failed", file);
2922 ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
2924 ikev2_main_t *km = &ikev2_main;
2929 if (ikev2_profile_index_by_name (name))
2930 return clib_error_return (0, "policy %v already exists", name);
2932 pool_get (km->profiles, p);
2933 clib_memset (p, 0, sizeof (*p));
2934 p->name = vec_dup (name);
2935 p->responder.sw_if_index = ~0;
2937 uword index = p - km->profiles;
2938 mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
2942 p = ikev2_profile_index_by_name (name);
2944 return clib_error_return (0, "policy %v does not exists", name);
2947 pool_put (km->profiles, p);
2948 mhash_unset (&km->profile_index_by_name, name, 0);
2954 ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
2955 u8 * auth_data, u8 data_hex_format)
2960 p = ikev2_profile_index_by_name (name);
2964 r = clib_error_return (0, "unknown profile %v", name);
2967 vec_free (p->auth.data);
2968 p->auth.method = auth_method;
2969 p->auth.data = vec_dup (auth_data);
2970 p->auth.hex = data_hex_format;
2972 if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
2974 vec_add1 (p->auth.data, 0);
2976 EVP_PKEY_free (p->auth.key);
2977 p->auth.key = ikev2_load_cert_file (auth_data);
2978 if (p->auth.key == NULL)
2979 return clib_error_return (0, "load cert '%s' failed", auth_data);
2986 ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
2992 if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
2993 && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
2995 r = clib_error_return (0, "unsupported identity type %U",
2996 format_ikev2_id_type, id_type);
3000 p = ikev2_profile_index_by_name (name);
3004 r = clib_error_return (0, "unknown profile %v", name);
3010 vec_free (p->loc_id.data);
3011 p->loc_id.type = id_type;
3012 p->loc_id.data = vec_dup (data);
3016 vec_free (p->rem_id.data);
3017 p->rem_id.type = id_type;
3018 p->rem_id.data = vec_dup (data);
3025 ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
3026 u16 start_port, u16 end_port, ip4_address_t start_addr,
3027 ip4_address_t end_addr, int is_local)
3032 p = ikev2_profile_index_by_name (name);
3036 r = clib_error_return (0, "unknown profile %v", name);
3042 p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
3043 p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
3044 p->loc_ts.start_port = start_port;
3045 p->loc_ts.end_port = end_port;
3046 p->loc_ts.protocol_id = protocol_id;
3047 p->loc_ts.ts_type = 7;
3051 p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
3052 p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
3053 p->rem_ts.start_port = start_port;
3054 p->rem_ts.end_port = end_port;
3055 p->rem_ts.protocol_id = protocol_id;
3056 p->rem_ts.ts_type = 7;
3064 ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
3065 u32 sw_if_index, ip4_address_t ip4)
3070 p = ikev2_profile_index_by_name (name);
3074 r = clib_error_return (0, "unknown profile %v", name);
3078 p->responder.sw_if_index = sw_if_index;
3079 p->responder.ip4 = ip4;
3085 ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
3086 ikev2_transform_encr_type_t crypto_alg,
3087 ikev2_transform_integ_type_t integ_alg,
3088 ikev2_transform_dh_type_t dh_type,
3089 u32 crypto_key_size)
3094 p = ikev2_profile_index_by_name (name);
3098 r = clib_error_return (0, "unknown profile %v", name);
3102 p->ike_ts.crypto_alg = crypto_alg;
3103 p->ike_ts.integ_alg = integ_alg;
3104 p->ike_ts.dh_type = dh_type;
3105 p->ike_ts.crypto_key_size = crypto_key_size;
3110 ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
3111 ikev2_transform_encr_type_t crypto_alg,
3112 ikev2_transform_integ_type_t integ_alg,
3113 ikev2_transform_dh_type_t dh_type,
3114 u32 crypto_key_size)
3119 p = ikev2_profile_index_by_name (name);
3123 r = clib_error_return (0, "unknown profile %v", name);
3127 p->esp_ts.crypto_alg = crypto_alg;
3128 p->esp_ts.integ_alg = integ_alg;
3129 p->esp_ts.dh_type = dh_type;
3130 p->esp_ts.crypto_key_size = crypto_key_size;
3135 ikev2_set_profile_tunnel_interface (vlib_main_t * vm,
3136 u8 * name, u32 sw_if_index)
3141 p = ikev2_profile_index_by_name (name);
3145 r = clib_error_return (0, "unknown profile %v", name);
3149 p->tun_itf = sw_if_index;
3155 ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
3156 u64 lifetime, u32 jitter, u32 handover,
3162 p = ikev2_profile_index_by_name (name);
3166 r = clib_error_return (0, "unknown profile %v", name);
3170 p->lifetime = lifetime;
3171 p->lifetime_jitter = jitter;
3172 p->handover = handover;
3173 p->lifetime_maxdata = maxdata;
3178 ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
3182 ip4_main_t *im = &ip4_main;
3183 ikev2_main_t *km = &ikev2_main;
3185 p = ikev2_profile_index_by_name (name);
3189 r = clib_error_return (0, "unknown profile %v", name);
3193 if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
3195 r = clib_error_return (0, "responder not set for profile %v", name);
3200 /* Create the Initiator Request */
3204 ip_lookup_main_t *lm = &im->lookup_main;
3206 int len = sizeof (ike_header_t);
3208 /* Get own iface IP */
3210 lm->if_address_pool_index_by_sw_if_index[p->responder.sw_if_index];
3211 ip_interface_address_t *if_add =
3212 pool_elt_at_index (lm->if_address_pool, if_add_index0);
3213 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
3215 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3217 /* Prepare the SA and the IKE payload */
3219 clib_memset (&sa, 0, sizeof (ikev2_sa_t));
3220 ikev2_payload_chain_t *chain = 0;
3221 ikev2_payload_new_chain (chain);
3223 /* Build the IKE proposal payload */
3224 ikev2_sa_proposal_t *proposals = 0;
3225 ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
3226 proposals[0].proposal_num = 1;
3227 proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
3229 /* Add and then cleanup proposal data */
3230 ikev2_payload_add_sa (chain, proposals);
3231 ikev2_sa_free_proposal_vector (&proposals);
3233 sa.is_initiator = 1;
3234 sa.profile_index = km->profiles - p;
3235 sa.is_profile_index_set = 1;
3236 sa.state = IKEV2_STATE_SA_INIT;
3237 sa.tun_itf = p->tun_itf;
3238 sa.is_tun_itf_set = 1;
3239 sa.initial_contact = 1;
3240 ikev2_generate_sa_init_data (&sa);
3241 ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
3242 ikev2_payload_add_nonce (chain, sa.i_nonce);
3244 /* Build the child SA proposal */
3245 vec_resize (sa.childs, 1);
3246 ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
3247 &sa.childs[0].i_proposals, 0);
3248 sa.childs[0].i_proposals[0].proposal_num = 1;
3249 sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
3250 RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
3251 sizeof (sa.childs[0].i_proposals[0].spi));
3255 /* Add NAT detection notification messages (mandatory) */
3256 u8 nat_detection_source[8 + 8 + 4 + 2];
3257 u8 *nat_detection_sha1 = vec_new (u8, 20);
3259 u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
3260 clib_memcpy_fast (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
3261 tmpspi = clib_host_to_net_u64 (sa.rspi);
3262 clib_memcpy_fast (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
3263 u16 tmpport = clib_host_to_net_u16 (500);
3264 clib_memcpy_fast (&nat_detection_source[8 + 8 + 4], &tmpport,
3266 u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
3267 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3268 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3269 nat_detection_sha1);
3270 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
3271 nat_detection_sha1);
3272 tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
3273 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3274 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3275 nat_detection_sha1);
3276 ikev2_payload_add_notify (chain,
3277 IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
3278 nat_detection_sha1);
3279 vec_free (nat_detection_sha1);
3281 u8 *sig_hash_algo = vec_new (u8, 8);
3282 u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
3283 clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
3284 ikev2_payload_add_notify (chain,
3285 IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
3287 vec_free (sig_hash_algo);
3290 /* Buffer update and boilerplate */
3291 len += vec_len (chain->data);
3292 ike0->nextpayload = chain->first_payload_type;
3293 ike0->length = clib_host_to_net_u32 (len);
3294 clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
3295 ikev2_payload_destroy_chain (chain);
3297 ike0->version = IKE_VERSION_2;
3298 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3299 ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
3300 ike0->ispi = sa.ispi;
3304 /* store whole IKE payload - needed for PSK auth */
3305 vec_free (sa.last_sa_init_req_packet_data);
3306 vec_add (sa.last_sa_init_req_packet_data, ike0, len);
3308 /* add data to the SA then add it to the pool */
3309 sa.iaddr.as_u32 = if_ip->as_u32;
3310 sa.raddr.as_u32 = p->responder.ip4.as_u32;
3311 sa.i_id.type = p->loc_id.type;
3312 sa.i_id.data = vec_dup (p->loc_id.data);
3313 sa.i_auth.method = p->auth.method;
3314 sa.i_auth.hex = p->auth.hex;
3315 sa.i_auth.data = vec_dup (p->auth.data);
3316 vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
3317 vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
3319 ikev2_initial_contact_cleanup (&sa);
3321 /* add SA to the pool */
3322 ikev2_sa_t *sa0 = 0;
3323 pool_get (km->sais, sa0);
3324 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3325 hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
3327 ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
3329 ikev2_elog_exchange ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to "
3330 "%d.%d.%d.%d", clib_host_to_net_u64 (sa0->ispi), 0,
3331 p->responder.ip4.as_u32);
3338 ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3339 ikev2_child_sa_t * csa)
3341 /* Create the Initiator notification for child SA removal */
3342 ikev2_main_t *km = &ikev2_main;
3347 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3350 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3351 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3352 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3353 vec_resize (sa->del, 1);
3354 sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
3355 sa->del->spi = csa->i_proposals->spi;
3356 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3357 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3358 len = ikev2_generate_message (sa, ike0, 0);
3360 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3362 /* delete local child SA */
3363 ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
3364 ikev2_sa_del_child_sa (sa, csa);
3368 ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
3371 ikev2_main_t *km = &ikev2_main;
3372 ikev2_main_per_thread_data_t *tkm;
3373 ikev2_sa_t *fsa = 0;
3374 ikev2_child_sa_t *fchild = 0;
3376 /* Search for the child SA */
3377 vec_foreach (tkm, km->per_thread_data)
3383 pool_foreach (sa, tkm->sas, ({
3384 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3394 if (!fchild || !fsa)
3396 r = clib_error_return (0, "Child SA not found");
3401 ikev2_delete_child_sa_internal (vm, fsa, fchild);
3408 ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
3411 ikev2_main_t *km = &ikev2_main;
3412 ikev2_main_per_thread_data_t *tkm;
3413 ikev2_sa_t *fsa = 0;
3414 ikev2_main_per_thread_data_t *ftkm = 0;
3416 /* Search for the IKE SA */
3417 vec_foreach (tkm, km->per_thread_data)
3423 pool_foreach (sa, tkm->sas, ({
3424 if (sa->ispi == ispi)
3436 r = clib_error_return (0, "IKE SA not found");
3441 /* Create the Initiator notification for IKE SA removal */
3447 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3450 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3451 ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
3452 ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
3453 vec_resize (fsa->del, 1);
3454 fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3455 fsa->del->spi = ispi;
3456 ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
3457 fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3458 len = ikev2_generate_message (fsa, ike0, 0);
3460 ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
3464 /* delete local SA */
3465 ikev2_child_sa_t *c;
3466 vec_foreach (c, fsa->childs)
3468 ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
3469 ikev2_sa_del_child_sa (fsa, c);
3471 ikev2_sa_free_all_vec (fsa);
3472 uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
3475 hash_unset (ftkm->sa_by_rspi, fsa->rspi);
3476 pool_put (ftkm->sas, fsa);
3484 ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3485 ikev2_child_sa_t * csa)
3487 /* Create the Initiator request for create child SA */
3493 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3496 ike0->version = IKE_VERSION_2;
3497 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3498 ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
3499 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3500 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
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);
3504 ikev2_rekey_t *rekey;
3505 vec_add2 (sa->rekey, rekey, 1);
3506 ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
3509 RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
3510 rekey->spi = proposals[0].spi;
3511 rekey->ispi = csa->i_proposals->spi;
3512 len = ikev2_generate_message (sa, ike0, proposals);
3513 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3514 vec_free (proposals);
3518 ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
3521 ikev2_main_t *km = &ikev2_main;
3522 ikev2_main_per_thread_data_t *tkm;
3523 ikev2_sa_t *fsa = 0;
3524 ikev2_child_sa_t *fchild = 0;
3526 /* Search for the child SA */
3527 vec_foreach (tkm, km->per_thread_data)
3533 pool_foreach (sa, tkm->sas, ({
3534 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3544 if (!fchild || !fsa)
3546 r = clib_error_return (0, "Child SA not found");
3551 ikev2_rekey_child_sa_internal (vm, fsa, fchild);
3558 ikev2_init (vlib_main_t * vm)
3560 ikev2_main_t *km = &ikev2_main;
3561 vlib_thread_main_t *tm = vlib_get_thread_main ();
3564 clib_memset (km, 0, sizeof (ikev2_main_t));
3565 km->vnet_main = vnet_get_main ();
3568 ikev2_crypto_init (km);
3570 mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
3572 vec_validate (km->per_thread_data, tm->n_vlib_mains - 1);
3573 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
3575 km->per_thread_data[thread_id].sa_by_rspi =
3576 hash_create (0, sizeof (uword));
3579 km->sa_by_ispi = hash_create (0, sizeof (uword));
3580 km->sw_if_indices = hash_create (0, 0);
3582 udp_register_dst_port (vm, 500, ikev2_node.index, 1);
3584 ikev2_cli_reference ();
3586 km->log_level = IKEV2_LOG_ERROR;
3587 km->log_class = vlib_log_register_class ("ikev2", 0);
3592 VLIB_INIT_FUNCTION (ikev2_init) =
3594 .runs_after = VLIB_INITS("ipsec_init"),
3599 ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa,
3602 ikev2_main_t *km = &ikev2_main;
3603 ikev2_profile_t *p = 0;
3604 vlib_main_t *vm = km->vlib_main;
3605 f64 now = vlib_time_now (vm);
3608 if (sa->is_profile_index_set)
3609 p = pool_elt_at_index (km->profiles, sa->profile_index);
3611 if (sa->is_initiator && p && csa->time_to_expiration
3612 && now > csa->time_to_expiration)
3614 if (!csa->is_expired || csa->rekey_retries > 0)
3616 ikev2_rekey_child_sa_internal (vm, sa, csa);
3617 csa->time_to_expiration = now + p->handover;
3618 csa->is_expired = 1;
3619 if (csa->rekey_retries == 0)
3621 csa->rekey_retries = 5;
3623 else if (csa->rekey_retries > 0)
3625 csa->rekey_retries--;
3626 ikev2_log_debug ("Rekeying Child SA 0x%x, retries left %d",
3627 csa->i_proposals->spi, csa->rekey_retries);
3628 if (csa->rekey_retries == 0)
3630 csa->rekey_retries = -1;
3637 csa->time_to_expiration = 0;
3638 ikev2_delete_child_sa_internal (vm, sa, csa);
3645 ipip_tunnel_t *ipip = NULL;
3646 u32 sw_if_index = sa->is_tun_itf_set ? sa->tun_itf : ~0;
3647 if (~0 == sw_if_index)
3649 ip46_address_t local_ip;
3650 ip46_address_t remote_ip;
3651 if (sa->is_initiator)
3653 ip46_address_set_ip4 (&local_ip, &sa->iaddr);
3654 ip46_address_set_ip4 (&remote_ip, &sa->raddr);
3658 ip46_address_set_ip4 (&local_ip, &sa->raddr);
3659 ip46_address_set_ip4 (&remote_ip, &sa->iaddr);
3663 ipip_tunnel_key_t key = {
3666 .transport = IPIP_TRANSPORT_IP4,
3671 ipip = ipip_tunnel_db_find (&key);
3674 sw_if_index = ipip->sw_if_index;
3680 vec_add1 (sas_in, csa->remote_sa_id);
3681 ipsec_tun_protect_update (sw_if_index, NULL, csa->local_sa_id, sas_in);
3682 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (csa->remote_sa_id));
3689 ikev2_set_log_level (ikev2_log_level_t log_level)
3691 ikev2_main_t *km = &ikev2_main;
3693 if (log_level >= IKEV2_LOG_MAX)
3695 ikev2_log_error ("unknown logging level %d", log_level);
3699 km->log_level = log_level;
3704 ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
3706 ikev2_main_t *km = &ikev2_main;
3707 vlib_main_t *vm = km->vlib_main;
3708 ikev2_main_per_thread_data_t *tkm;
3709 ikev2_sa_t *fsa = 0;
3710 ikev2_profile_t *p = 0;
3711 ikev2_child_sa_t *fchild = 0;
3712 f64 now = vlib_time_now (vm);
3713 vlib_counter_t counts;
3715 /* Search for the SA and child SA */
3716 vec_foreach (tkm, km->per_thread_data)
3722 pool_foreach (sa, tkm->sas, ({
3723 fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
3732 vlib_get_combined_counter (&ipsec_sa_counters,
3733 ipsec_sa->stat_index, &counts);
3735 if (fsa && fsa->is_profile_index_set)
3736 p = pool_elt_at_index (km->profiles, fsa->profile_index);
3738 if (fchild && p && p->lifetime_maxdata)
3740 if (!fchild->is_expired && counts.bytes > p->lifetime_maxdata)
3742 fchild->time_to_expiration = now;
3748 ikev2_process_pending_sa_init (ikev2_main_t * km)
3755 hash_foreach (ispi, sai, km->sa_by_ispi,
3757 sa = pool_elt_at_index (km->sais, sai);
3759 if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
3762 vlib_buffer_t * b = vlib_get_buffer (km->vlib_main, bi0);
3763 clib_memcpy_fast (vlib_buffer_get_current (b),
3764 sa->last_sa_init_req_packet_data,
3765 vec_len (sa->last_sa_init_req_packet_data));
3766 ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0,
3767 vec_len (sa->last_sa_init_req_packet_data));
3772 static vlib_node_registration_t ikev2_mngr_process_node;
3775 ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
3778 ikev2_main_t *km = &ikev2_main;
3779 ipsec_main_t *im = &ipsec_main;
3784 vlib_process_wait_for_event_or_clock (vm, 1);
3785 vlib_process_get_events (vm, NULL);
3787 /* process ike child sas */
3788 ikev2_main_per_thread_data_t *tkm;
3789 vec_foreach (tkm, km->per_thread_data)
3793 pool_foreach (sa, tkm->sas, ({
3794 ikev2_child_sa_t *c;
3796 if (sa->old_remote_id_present && 0 > sa->old_id_expiration)
3798 sa->old_remote_id_present = 0;
3802 sa->old_id_expiration -= 1;
3804 vec_foreach (c, sa->childs)
3806 req_sent |= ikev2_mngr_process_child_sa(sa, c, del_old_ids);
3812 /* process ipsec sas */
3815 pool_foreach (sa, im->sad, ({
3816 ikev2_mngr_process_ipsec_sa(sa);
3820 ikev2_process_pending_sa_init (km);
3824 vlib_process_wait_for_event_or_clock (vm, 5);
3825 vlib_process_get_events (vm, NULL);
3834 VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
3835 .function = ikev2_mngr_process_fn,
3836 .type = VLIB_NODE_TYPE_PROCESS,
3838 "ikev2-manager-process",
3841 VLIB_PLUGIN_REGISTER () = {
3842 .version = VPP_BUILD_VER,
3843 .description = "Internet Key Exchange (IKEv2) Protocol",
3848 * fd.io coding-style-patch-verification: ON
3851 * eval: (c-set-style "gnu")