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;
405 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
410 /* check if received DH group is on our list of supported groups */
411 vec_foreach (t2, km->supported_transforms)
413 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
422 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
427 /* generate dh keys */
428 ikev2_complete_dh (sa, t);
433 ikev2_calc_keys (ikev2_sa_t * sa)
436 /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
439 u16 integ_key_len = 0;
440 ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
442 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
444 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
446 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
449 integ_key_len = tr_integ->key_len;
451 vec_append (s, sa->i_nonce);
452 vec_append (s, sa->r_nonce);
453 skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
455 /* Calculate S = Ni | Nr | SPIi | SPIr */
457 vec_add2 (s, tmp, 2 * sizeof (*spi));
459 spi[0] = clib_host_to_net_u64 (sa->ispi);
460 spi[1] = clib_host_to_net_u64 (sa->rspi);
462 /* calculate PRFplus */
464 int len = tr_prf->key_trunc + /* SK_d */
465 integ_key_len * 2 + /* SK_ai, SK_ar */
466 tr_encr->key_len * 2 + /* SK_ei, SK_er */
467 tr_prf->key_len * 2; /* SK_pi, SK_pr */
469 keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
476 sa->sk_d = vec_new (u8, tr_prf->key_trunc);
477 clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
478 pos += tr_prf->key_trunc;
483 sa->sk_ai = vec_new (u8, integ_key_len);
484 clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
485 pos += integ_key_len;
488 sa->sk_ar = vec_new (u8, integ_key_len);
489 clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
490 pos += integ_key_len;
494 sa->sk_ei = vec_new (u8, tr_encr->key_len);
495 clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len);
496 pos += tr_encr->key_len;
499 sa->sk_er = vec_new (u8, tr_encr->key_len);
500 clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len);
501 pos += tr_encr->key_len;
504 sa->sk_pi = vec_new (u8, tr_prf->key_len);
505 clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
506 pos += tr_prf->key_len;
509 sa->sk_pr = vec_new (u8, tr_prf->key_len);
510 clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
511 pos += tr_prf->key_len;
517 ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child)
520 u16 integ_key_len = 0;
523 ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
525 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
527 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
529 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
532 integ_key_len = ctr_integ->key_len;
534 salt_len = sizeof (u32);
536 vec_append (s, sa->i_nonce);
537 vec_append (s, sa->r_nonce);
538 /* calculate PRFplus */
540 int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
542 keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
547 child->sk_ei = vec_new (u8, ctr_encr->key_len);
548 clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
549 pos += ctr_encr->key_len;
554 child->sk_ai = vec_new (u8, ctr_integ->key_len);
555 clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
556 pos += ctr_integ->key_len;
560 clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
565 child->sk_er = vec_new (u8, ctr_encr->key_len);
566 clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
567 pos += ctr_encr->key_len;
572 child->sk_ar = vec_new (u8, integ_key_len);
573 clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
574 pos += integ_key_len;
578 clib_memcpy (&child->salt_er, keymat + pos, salt_len);
588 ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
592 u32 len = clib_net_to_host_u32 (ike->length);
593 u8 payload = ike->nextpayload;
595 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
597 clib_net_to_host_u64 (ike->ispi),
598 clib_net_to_host_u64 (ike->rspi), sa->iaddr.as_u32);
600 sa->ispi = clib_net_to_host_u64 (ike->ispi);
602 /* store whole IKE payload - needed for PSK auth */
603 vec_free (sa->last_sa_init_req_packet_data);
604 vec_add (sa->last_sa_init_req_packet_data, ike, len);
606 while (p < len && payload != IKEV2_PAYLOAD_NONE)
608 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
609 u32 plen = clib_net_to_host_u16 (ikep->length);
611 if (plen < sizeof (ike_payload_header_t))
614 if (payload == IKEV2_PAYLOAD_SA)
616 ikev2_sa_free_proposal_vector (&sa->i_proposals);
617 sa->i_proposals = ikev2_parse_sa_payload (ikep);
619 else if (payload == IKEV2_PAYLOAD_KE)
621 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
622 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
623 vec_free (sa->i_dh_data);
624 vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
626 else if (payload == IKEV2_PAYLOAD_NONCE)
628 vec_free (sa->i_nonce);
629 vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
631 else if (payload == IKEV2_PAYLOAD_NOTIFY)
633 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
636 else if (payload == IKEV2_PAYLOAD_VENDOR)
638 ikev2_parse_vendor_payload (ikep);
642 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
644 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
646 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
647 sa->unsupported_cp = payload;
652 payload = ikep->nextpayload;
656 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
660 ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa,
664 u32 len = clib_net_to_host_u32 (ike->length);
665 u8 payload = ike->nextpayload;
667 sa->ispi = clib_net_to_host_u64 (ike->ispi);
668 sa->rspi = clib_net_to_host_u64 (ike->rspi);
670 ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
671 "from %d.%d.%d.%d", sa->ispi, sa->rspi,
674 /* store whole IKE payload - needed for PSK auth */
675 vec_free (sa->last_sa_init_res_packet_data);
676 vec_add (sa->last_sa_init_res_packet_data, ike, len);
678 while (p < len && payload != IKEV2_PAYLOAD_NONE)
680 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
681 u32 plen = clib_net_to_host_u16 (ikep->length);
683 if (plen < sizeof (ike_payload_header_t))
686 if (payload == IKEV2_PAYLOAD_SA)
688 ikev2_sa_free_proposal_vector (&sa->r_proposals);
689 sa->r_proposals = ikev2_parse_sa_payload (ikep);
692 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
694 clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
697 else if (payload == IKEV2_PAYLOAD_KE)
699 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
700 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
701 vec_free (sa->r_dh_data);
702 vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
704 else if (payload == IKEV2_PAYLOAD_NONCE)
706 vec_free (sa->r_nonce);
707 vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
709 else if (payload == IKEV2_PAYLOAD_NOTIFY)
711 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
714 else if (payload == IKEV2_PAYLOAD_VENDOR)
716 ikev2_parse_vendor_payload (ikep);
720 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
722 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
724 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
725 sa->unsupported_cp = payload;
730 payload = ikep->nextpayload;
736 ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
741 u32 len = clib_net_to_host_u32 (ike->length);
742 ike_payload_header_t *ikep = 0;
744 ikev2_sa_transform_t *tr_integ;
746 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
749 *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
751 ikep = (ike_payload_header_t *) & ike->payload[p];
752 plen = clib_net_to_host_u16 (ikep->length);
754 if (plen < sizeof (*ikep))
757 if (*payload == IKEV2_PAYLOAD_SK)
759 last_payload = *payload;
763 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
765 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
767 sa->unsupported_cp = *payload;
772 *payload = ikep->nextpayload;
776 if (last_payload != IKEV2_PAYLOAD_SK)
778 ikev2_elog_error ("Last payload must be SK");
783 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
784 (u8 *) ike, len - tr_integ->key_trunc);
786 plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
788 if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
790 ikev2_elog_error ("message integrity check failed");
796 return ikev2_decrypt_data (sa, ikep->payload, plen);
800 ikev2_initial_contact_cleanup (ikev2_sa_t * sa)
802 ikev2_main_t *km = &ikev2_main;
806 u32 thread_index = vlib_get_thread_index ();
808 if (!sa->initial_contact)
811 /* find old IKE SAs with the same authenticated identity */
813 pool_foreach (tmp, km->per_thread_data[thread_index].sas, ({
814 if (tmp->i_id.type != sa->i_id.type ||
815 vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
816 memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
819 if (sa->rspi != tmp->rspi)
820 vec_add1(delete, tmp - km->per_thread_data[thread_index].sas);
824 for (i = 0; i < vec_len (delete); i++)
827 pool_elt_at_index (km->per_thread_data[thread_index].sas, delete[i]);
829 tmp->childs) ikev2_delete_tunnel_interface (km->vnet_main,
831 ikev2_delete_sa (tmp);
835 sa->initial_contact = 0;
839 ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
841 ikev2_child_sa_t *first_child_sa;
843 u8 payload = ike->nextpayload;
845 ike_payload_header_t *ikep;
848 ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
849 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
850 clib_host_to_net_u64 (ike->rspi),
851 sa->is_initiator ? sa->raddr.as_u32 : sa->
854 ikev2_calc_keys (sa);
856 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
860 if (sa->unsupported_cp)
861 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
862 goto cleanup_and_exit;
865 /* select or create 1st child SA */
866 if (sa->is_initiator)
868 first_child_sa = &sa->childs[0];
872 ikev2_sa_free_all_child_sa (&sa->childs);
873 vec_add2 (sa->childs, first_child_sa, 1);
877 /* process encrypted payload */
879 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
881 ikep = (ike_payload_header_t *) & plaintext[p];
882 plen = clib_net_to_host_u16 (ikep->length);
884 if (plen < sizeof (ike_payload_header_t))
885 goto cleanup_and_exit;
887 if (payload == IKEV2_PAYLOAD_SA) /* 33 */
889 if (sa->is_initiator)
891 ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
892 first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
896 ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
897 first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
900 else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
902 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
904 sa->i_id.type = id->id_type;
905 vec_free (sa->i_id.data);
906 vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
908 else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
910 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
912 sa->r_id.type = id->id_type;
913 vec_free (sa->r_id.data);
914 vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
916 else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
918 ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
920 if (sa->is_initiator)
922 sa->r_auth.method = a->auth_method;
923 vec_free (sa->r_auth.data);
924 vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
928 sa->i_auth.method = a->auth_method;
929 vec_free (sa->i_auth.data);
930 vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
933 else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
935 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
936 if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
938 sa->initial_contact = 1;
942 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
944 ikev2_parse_vendor_payload (ikep);
946 else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
948 vec_free (first_child_sa->tsi);
949 first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
951 else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
953 vec_free (first_child_sa->tsr);
954 first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
958 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
961 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
963 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
964 sa->unsupported_cp = payload;
969 payload = ikep->nextpayload;
974 vec_free (plaintext);
978 ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa,
982 u8 payload = ike->nextpayload;
984 ike_payload_header_t *ikep;
987 ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
988 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
989 clib_host_to_net_u64 (ike->rspi), sa->iaddr.as_u32);
991 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
994 goto cleanup_and_exit;
996 /* process encrypted payload */
998 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1000 ikep = (ike_payload_header_t *) & plaintext[p];
1001 plen = clib_net_to_host_u16 (ikep->length);
1003 if (plen < sizeof (ike_payload_header_t))
1004 goto cleanup_and_exit;
1006 if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1008 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
1009 if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1010 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1013 else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1015 sa->del = ikev2_parse_delete_payload (ikep);
1017 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1019 ikev2_parse_vendor_payload (ikep);
1023 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1025 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1027 sa->unsupported_cp = payload;
1032 payload = ikep->nextpayload;
1037 vec_free (plaintext);
1041 ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
1045 u8 payload = ike->nextpayload;
1048 u8 nonce[IKEV2_NONCE_SIZE];
1050 ike_payload_header_t *ikep;
1052 ikev2_notify_t *n = 0;
1053 ikev2_ts_t *tsi = 0;
1054 ikev2_ts_t *tsr = 0;
1055 ikev2_sa_proposal_t *proposal = 0;
1056 ikev2_child_sa_t *child_sa;
1058 ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received "
1059 "from %d.%d.%d.%d", clib_host_to_net_u64 (ike->ispi),
1060 clib_host_to_net_u64 (ike->rspi), sa->raddr.as_u32);
1062 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
1065 goto cleanup_and_exit;
1067 /* process encrypted payload */
1069 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1071 ikep = (ike_payload_header_t *) & plaintext[p];
1072 plen = clib_net_to_host_u16 (ikep->length);
1074 if (plen < sizeof (ike_payload_header_t))
1075 goto cleanup_and_exit;
1077 else if (payload == IKEV2_PAYLOAD_SA)
1079 proposal = ikev2_parse_sa_payload (ikep);
1081 else if (payload == IKEV2_PAYLOAD_NOTIFY)
1083 n = ikev2_parse_notify_payload (ikep);
1084 if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1089 else if (payload == IKEV2_PAYLOAD_DELETE)
1091 sa->del = ikev2_parse_delete_payload (ikep);
1093 else if (payload == IKEV2_PAYLOAD_VENDOR)
1095 ikev2_parse_vendor_payload (ikep);
1097 else if (payload == IKEV2_PAYLOAD_NONCE)
1099 clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
1101 else if (payload == IKEV2_PAYLOAD_TSI)
1103 tsi = ikev2_parse_ts_payload (ikep);
1105 else if (payload == IKEV2_PAYLOAD_TSR)
1107 tsr = ikev2_parse_ts_payload (ikep);
1111 ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1113 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1115 sa->unsupported_cp = payload;
1120 payload = ikep->nextpayload;
1124 if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1126 ikev2_rekey_t *rekey = &sa->rekey[0];
1127 rekey->protocol_id = proposal->protocol_id;
1129 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1130 rekey->i_proposal->spi = rekey->spi;
1131 rekey->r_proposal = proposal;
1135 vec_free (sa->r_nonce);
1136 vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1137 child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1140 child_sa->rekey_retries = 0;
1145 ikev2_rekey_t *rekey;
1146 child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1149 ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1151 goto cleanup_and_exit;
1153 vec_add2 (sa->rekey, rekey, 1);
1154 rekey->protocol_id = n->protocol_id;
1155 rekey->spi = n->spi;
1156 rekey->i_proposal = proposal;
1158 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1162 vec_free (sa->i_nonce);
1163 vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1164 /* generate new Nr */
1165 vec_free (sa->r_nonce);
1166 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
1167 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1171 vec_free (plaintext);
1176 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1184 ikev2_sa_transform_t *tr_prf;
1187 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1193 nonce = sa->i_nonce;
1194 packet_data = sa->last_sa_init_res_packet_data;
1200 nonce = sa->r_nonce;
1201 packet_data = sa->last_sa_init_req_packet_data;
1204 data = vec_new (u8, 4);
1206 vec_append (data, id->data);
1208 u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1209 vec_append (authmsg, packet_data);
1210 vec_append (authmsg, nonce);
1211 vec_append (authmsg, id_hash);
1219 ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
1221 if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1222 ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1223 ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1224 ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1231 ikev2_sa_match_ts (ikev2_sa_t * sa)
1233 ikev2_main_t *km = &ikev2_main;
1235 ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1239 pool_foreach (p, km->profiles, ({
1241 if (sa->is_initiator)
1255 if (p->rem_id.type != id->type ||
1256 vec_len(p->rem_id.data) != vec_len(id->data) ||
1257 memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
1260 vec_foreach(ts, sa->childs[0].tsi)
1262 if (ikev2_ts_cmp(p_tsi, ts))
1264 vec_add1 (tsi, ts[0]);
1269 vec_foreach(ts, sa->childs[0].tsr)
1271 if (ikev2_ts_cmp(p_tsr, ts))
1273 vec_add1 (tsr, ts[0]);
1284 vec_free (sa->childs[0].tsi);
1285 vec_free (sa->childs[0].tsr);
1286 sa->childs[0].tsi = tsi;
1287 sa->childs[0].tsr = tsr;
1293 ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
1298 ikev2_sa_auth (ikev2_sa_t * sa)
1300 ikev2_main_t *km = &ikev2_main;
1301 ikev2_profile_t *p, *sel_p = 0;
1302 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1303 ikev2_sa_transform_t *tr_prf;
1306 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1308 /* only shared key and rsa signature */
1309 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1310 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1312 ikev2_elog_uint (IKEV2_LOG_ERROR,
1313 "unsupported authentication method %u",
1315 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1319 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1320 authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1323 ikev2_auth_t *sa_auth;
1325 if (sa->is_initiator)
1328 sa_auth = &sa->r_auth;
1333 sa_auth = &sa->i_auth;
1337 pool_foreach (p, km->profiles, ({
1340 if (p->rem_id.type != sa_id->type ||
1341 vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
1342 memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
1345 if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1347 if (!p->auth.data ||
1348 p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1351 psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1352 auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1354 if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
1356 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1363 else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1365 if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1368 if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
1370 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1377 sa->udp_encap = sel_p->udp_encap;
1386 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1388 if (!sa->is_initiator)
1390 vec_free (sa->r_id.data);
1391 sa->r_id.data = vec_dup (sel_p->loc_id.data);
1392 sa->r_id.type = sel_p->loc_id.type;
1394 /* generate our auth data */
1395 authmsg = ikev2_sa_generate_authmsg (sa, 1);
1396 if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1398 sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1399 sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1401 else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1403 sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1404 sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1408 /* select transforms for 1st child sa */
1409 ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1410 sa->childs[0].r_proposals =
1411 ikev2_select_proposal (sa->childs[0].i_proposals,
1412 IKEV2_PROTOCOL_ESP);
1414 if (~0 != sel_p->tun_itf)
1416 sa->is_tun_itf_set = 1;
1417 sa->tun_itf = sel_p->tun_itf;
1423 ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1424 "profile found! ispi %lx", sa->ispi);
1425 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1433 ikev2_sa_auth_init (ikev2_sa_t * sa)
1435 ikev2_main_t *km = &ikev2_main;
1436 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1437 ikev2_sa_transform_t *tr_prf;
1440 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1442 /* only shared key and rsa signature */
1443 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1444 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1446 ikev2_elog_uint (IKEV2_LOG_ERROR,
1447 "unsupported authentication method %u",
1449 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1453 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1454 authmsg = ikev2_sa_generate_authmsg (sa, 0);
1455 psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1456 auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1459 if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1461 sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1462 sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1464 else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1466 sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1467 sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1477 ikev2_mk_local_sa_id (u32 sai, u32 ci, u32 ti)
1479 return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1483 ikev2_mk_remote_sa_id (u32 sai, u32 ci, u32 ti)
1485 return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1495 ipsec_sa_flags_t flags;
1498 ipsec_crypto_alg_t encr_type;
1499 ipsec_integ_alg_t integ_type;
1500 ip46_address_t local_ip;
1501 ip46_address_t remote_ip;
1502 ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1504 u32 old_remote_sa_id;
1505 } ikev2_add_ipsec_tunnel_args_t;
1508 ikev2_add_tunnel_from_main (ikev2_add_ipsec_tunnel_args_t * a)
1510 ikev2_main_t *km = &ikev2_main;
1514 if (~0 == a->sw_if_index)
1516 /* no tunnel associated with the SA/profile - create a new one */
1517 rv = ipip_add_tunnel (IPIP_TRANSPORT_IP4, ~0,
1518 &a->local_ip, &a->remote_ip, 0,
1519 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
1520 TUNNEL_MODE_P2P, &sw_if_index);
1522 if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
1524 if (hash_get (km->sw_if_indices, sw_if_index))
1525 /* interface is managed by IKE; proceed with updating SAs */
1528 hash_set1 (km->sw_if_indices, sw_if_index);
1532 sw_if_index = a->sw_if_index;
1533 vnet_sw_interface_admin_up (vnet_get_main (), sw_if_index);
1538 ikev2_elog_peers (IKEV2_LOG_ERROR, "installing ipip tunnel failed! "
1539 "loc:%d.%d.%d.%d rem:%d.%d.%d.%d",
1540 a->local_ip.ip4.as_u32, a->remote_ip.ip4.as_u32);
1545 vec_add1 (sas_in, a->remote_sa_id);
1548 ipsec_tun_protect_del (sw_if_index, NULL);
1550 /* replace local SA immediately */
1551 ipsec_sa_unlock_id (a->local_sa_id);
1553 /* keep the old sa */
1554 vec_add1 (sas_in, a->old_remote_sa_id);
1557 rv |= ipsec_sa_add_and_lock (a->local_sa_id,
1559 IPSEC_PROTOCOL_ESP, a->encr_type,
1560 &a->loc_ckey, a->integ_type, &a->loc_ikey,
1561 a->flags, 0, a->salt_local, &a->local_ip,
1562 &a->remote_ip, NULL);
1563 rv |= ipsec_sa_add_and_lock (a->remote_sa_id, a->remote_spi,
1564 IPSEC_PROTOCOL_ESP, a->encr_type, &a->rem_ckey,
1565 a->integ_type, &a->rem_ikey,
1566 (a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0,
1567 a->salt_remote, &a->remote_ip,
1568 &a->local_ip, NULL);
1570 rv |= ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
1574 ikev2_create_tunnel_interface (vnet_main_t * vnm,
1577 ikev2_child_sa_t * child, u32 sa_index,
1578 u32 child_index, u8 is_rekey)
1580 ikev2_main_t *km = &ikev2_main;
1581 ipsec_crypto_alg_t encr_type;
1582 ipsec_integ_alg_t integ_type;
1583 ikev2_profile_t *p = 0;
1584 ikev2_sa_transform_t *tr;
1585 ikev2_sa_proposal_t *proposals;
1587 ikev2_add_ipsec_tunnel_args_t a;
1589 clib_memset (&a, 0, sizeof (a));
1591 if (!child->r_proposals)
1593 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1597 if (sa->is_initiator)
1599 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1600 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1601 proposals = child->i_proposals;
1602 a.local_spi = child->r_proposals[0].spi;
1603 a.remote_spi = child->i_proposals[0].spi;
1607 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1608 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1609 proposals = child->r_proposals;
1610 a.local_spi = child->i_proposals[0].spi;
1611 a.remote_spi = child->r_proposals[0].spi;
1614 a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
1615 a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
1617 a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
1618 a.is_rekey = is_rekey;
1620 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
1621 if (tr && tr->esn_type)
1622 a.flags |= IPSEC_SA_FLAG_USE_ESN;
1624 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1627 if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
1629 switch (tr->key_len)
1632 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1635 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1638 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1641 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1646 else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
1649 switch (tr->key_len)
1652 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
1655 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
1658 encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
1661 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1669 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1675 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1678 a.encr_type = encr_type;
1682 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1685 switch (tr->integ_type)
1687 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
1688 integ_type = IPSEC_INTEG_ALG_SHA_256_128;
1690 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
1691 integ_type = IPSEC_INTEG_ALG_SHA_384_192;
1693 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
1694 integ_type = IPSEC_INTEG_ALG_SHA_512_256;
1696 case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
1697 integ_type = IPSEC_INTEG_ALG_SHA1_96;
1700 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1706 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1712 integ_type = IPSEC_INTEG_ALG_NONE;
1715 a.integ_type = integ_type;
1716 ikev2_calc_child_keys (sa, child);
1718 if (sa->is_initiator)
1720 ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
1721 ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
1722 ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
1723 ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
1726 a.salt_remote = child->salt_er;
1727 a.salt_local = child->salt_ei;
1732 ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
1733 ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
1734 ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
1735 ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
1738 a.salt_remote = child->salt_ei;
1739 a.salt_local = child->salt_er;
1743 if (sa->is_profile_index_set)
1744 p = pool_elt_at_index (km->profiles, sa->profile_index);
1746 if (p && p->lifetime)
1748 child->time_to_expiration =
1749 vlib_time_now (vnm->vlib_main) + p->lifetime;
1750 if (p->lifetime_jitter)
1752 // This is not much better than rand(3), which Coverity warns
1753 // is unsuitable for security applications; random_u32 is
1754 // however fast. If this perturbance to the expiration time
1755 // needs to use a better RNG then we may need to use something
1756 // like /dev/urandom which has significant overhead.
1757 u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
1758 rnd = random_u32 (&rnd);
1760 child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
1764 if (thread_index & 0xffffffc0)
1765 ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
1767 if (child_index & 0xfffff000 || sa_index & 0xfffff000)
1768 ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
1770 child->local_sa_id =
1772 ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
1774 u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
1779 /* create a new remote SA ID to keep the old SA for a bit longer
1780 * so the peer has some time to swap their SAs */
1782 /* use most significat bit of child index part in id */
1784 if (sa->current_remote_id_mask)
1786 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
1787 sa->current_remote_id_mask = 0;
1791 sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
1792 sa->current_remote_id_mask = mask;
1793 remote_sa_id |= mask;
1795 sa->old_id_expiration = 3.0;
1796 sa->old_remote_id_present = 1;
1799 child->remote_sa_id = a.remote_sa_id = remote_sa_id;
1801 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1803 vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main,
1804 (u8 *) & a, sizeof (a));
1810 ip46_address_t local_ip;
1811 ip46_address_t remote_ip;
1815 } ikev2_del_ipsec_tunnel_args_t;
1817 static_always_inline u32
1818 ikev2_flip_alternate_sa_bit (u32 id)
1827 ikev2_del_tunnel_from_main (ikev2_del_ipsec_tunnel_args_t * a)
1829 ikev2_main_t *km = &ikev2_main;
1830 ipip_tunnel_t *ipip = NULL;
1833 if (~0 == a->sw_if_index)
1836 ipip_tunnel_key_t key = {
1838 .dst = a->remote_ip,
1839 .transport = IPIP_TRANSPORT_IP4,
1844 ipip = ipip_tunnel_db_find (&key);
1848 sw_if_index = ipip->sw_if_index;
1849 hash_unset (km->sw_if_indices, ipip->sw_if_index);
1856 sw_if_index = a->sw_if_index;
1857 vnet_sw_interface_admin_down (vnet_get_main (), sw_if_index);
1860 if (~0 != sw_if_index)
1861 ipsec_tun_protect_del (sw_if_index, NULL);
1863 ipsec_sa_unlock_id (a->remote_sa_id);
1864 ipsec_sa_unlock_id (a->local_sa_id);
1865 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (a->remote_sa_id));
1868 ipip_del_tunnel (ipip->sw_if_index);
1872 ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1873 ikev2_child_sa_t * child)
1875 ikev2_del_ipsec_tunnel_args_t a;
1877 clib_memset (&a, 0, sizeof (a));
1879 if (sa->is_initiator)
1881 ip46_address_set_ip4 (&a.local_ip, &sa->iaddr);
1882 ip46_address_set_ip4 (&a.remote_ip, &sa->raddr);
1886 ip46_address_set_ip4 (&a.local_ip, &sa->raddr);
1887 ip46_address_set_ip4 (&a.remote_ip, &sa->iaddr);
1890 a.remote_sa_id = child->remote_sa_id;
1891 a.local_sa_id = child->local_sa_id;
1892 a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
1894 vl_api_rpc_call_main_thread (ikev2_del_tunnel_from_main, (u8 *) & a,
1900 ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
1903 ike_payload_header_t *ph;
1907 ikev2_sa_transform_t *tr_encr, *tr_integ;
1909 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1911 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1913 ikev2_payload_chain_t *chain = 0;
1914 ikev2_payload_new_chain (chain);
1916 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1918 if (sa->r_proposals == 0)
1920 ikev2_payload_add_notify (chain,
1921 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1922 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1924 else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
1926 u8 *data = vec_new (u8, 2);
1927 ikev2_sa_transform_t *tr_dh;
1929 ikev2_sa_get_td_for_type (sa->r_proposals,
1930 IKEV2_TRANSFORM_TYPE_DH);
1931 ASSERT (tr_dh && tr_dh->dh_type);
1933 data[0] = (tr_dh->dh_type >> 8) & 0xff;
1934 data[1] = (tr_dh->dh_type) & 0xff;
1936 ikev2_payload_add_notify (chain,
1937 IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1940 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1942 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1944 u8 *data = vec_new (u8, 1);
1946 data[0] = sa->unsupported_cp;
1947 ikev2_payload_add_notify (chain,
1948 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1954 ike->rspi = clib_host_to_net_u64 (sa->rspi);
1955 ikev2_payload_add_sa (chain, sa->r_proposals);
1956 ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1957 ikev2_payload_add_nonce (chain, sa->r_nonce);
1960 else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1962 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1964 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1965 ikev2_payload_add_auth (chain, &sa->r_auth);
1966 ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1967 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1968 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1970 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
1972 ikev2_payload_add_notify (chain,
1973 IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1975 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1977 else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
1979 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1981 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1982 ikev2_payload_add_auth (chain, &sa->r_auth);
1984 else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
1986 ikev2_payload_add_notify (chain,
1987 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1988 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1989 ikev2_payload_add_auth (chain, &sa->r_auth);
1990 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1991 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1993 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1995 u8 *data = vec_new (u8, 1);
1997 data[0] = sa->unsupported_cp;
1998 ikev2_payload_add_notify (chain,
1999 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2003 else if (sa->state == IKEV2_STATE_SA_INIT)
2005 ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
2006 ikev2_payload_add_auth (chain, &sa->i_auth);
2007 ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2008 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2009 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2010 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2015 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2019 else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2021 /* if pending delete */
2024 if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2026 if (sa->is_initiator)
2027 ikev2_payload_add_delete (chain, sa->del);
2029 /* The response to a request that deletes the IKE SA is an empty
2030 INFORMATIONAL response. */
2031 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
2033 /* The response to a request that deletes ESP or AH SAs will contain
2034 delete payloads for the paired SAs going in the other direction. */
2037 ikev2_payload_add_delete (chain, sa->del);
2042 /* received N(AUTHENTICATION_FAILED) */
2043 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2045 ikev2_set_state (sa, IKEV2_STATE_DELETED);
2048 /* received unsupported critical payload */
2049 else if (sa->unsupported_cp)
2051 u8 *data = vec_new (u8, 1);
2053 data[0] = sa->unsupported_cp;
2054 ikev2_payload_add_notify (chain,
2055 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2058 sa->unsupported_cp = 0;
2060 /* else send empty response */
2062 else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2064 if (sa->is_initiator)
2067 ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2068 ikev2_notify_t notify;
2069 u8 *data = vec_new (u8, 4);
2070 clib_memset (¬ify, 0, sizeof (notify));
2071 notify.protocol_id = IKEV2_PROTOCOL_ESP;
2072 notify.spi = sa->childs[0].i_proposals->spi;
2073 *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2075 ikev2_payload_add_sa (chain, proposals);
2076 ikev2_payload_add_nonce (chain, sa->i_nonce);
2077 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
2078 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
2079 ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2088 ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
2089 ikev2_payload_add_nonce (chain, sa->r_nonce);
2090 ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
2092 ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
2094 vec_del1 (sa->rekey, 0);
2096 else if (sa->unsupported_cp)
2098 u8 *data = vec_new (u8, 1);
2100 data[0] = sa->unsupported_cp;
2101 ikev2_payload_add_notify (chain,
2102 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2105 sa->unsupported_cp = 0;
2109 ikev2_payload_add_notify (chain,
2110 IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2117 ike->version = IKE_VERSION_2;
2118 ike->nextpayload = IKEV2_PAYLOAD_SK;
2119 tlen = sizeof (*ike);
2120 if (sa->is_initiator)
2122 ike->flags = IKEV2_HDR_FLAG_INITIATOR;
2123 sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
2127 ike->flags = IKEV2_HDR_FLAG_RESPONSE;
2131 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2133 tlen += vec_len (chain->data);
2134 ike->nextpayload = chain->first_payload_type;
2135 ike->length = clib_host_to_net_u32 (tlen);
2136 clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2138 /* store whole IKE payload - needed for PSK auth */
2139 vec_free (sa->last_sa_init_res_packet_data);
2140 vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
2145 ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2148 plen = sizeof (*ph);
2149 ph = (ike_payload_header_t *) & ike->payload[0];
2150 ph->nextpayload = chain->first_payload_type;
2152 int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
2155 /* add space for hmac */
2156 plen += tr_integ->key_trunc;
2159 /* payload and total length */
2160 ph->length = clib_host_to_net_u16 (plen);
2161 ike->length = clib_host_to_net_u32 (tlen);
2163 /* calc integrity data for whole packet except hash itself */
2165 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2166 (u8 *) ike, tlen - tr_integ->key_trunc);
2168 clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2169 sizeof (*ike), integ, tr_integ->key_trunc);
2171 /* store whole IKE payload - needed for retransmit */
2172 vec_free (sa->last_res_packet_data);
2173 vec_add (sa->last_res_packet_data, ike, tlen);
2177 ikev2_payload_destroy_chain (chain);
2183 ikev2_retransmit_sa_init (ike_header_t * ike,
2184 ip4_address_t iaddr, ip4_address_t raddr)
2186 ikev2_main_t *km = &ikev2_main;
2188 u32 thread_index = vlib_get_thread_index ();
2191 pool_foreach (sa, km->per_thread_data[thread_index].sas, ({
2192 if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
2193 sa->iaddr.as_u32 == iaddr.as_u32 &&
2194 sa->raddr.as_u32 == raddr.as_u32)
2197 u32 len = clib_net_to_host_u32(ike->length);
2198 u8 payload = ike->nextpayload;
2200 while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
2201 ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
2202 u32 plen = clib_net_to_host_u16(ikep->length);
2204 if (plen < sizeof(ike_payload_header_t))
2207 if (payload == IKEV2_PAYLOAD_NONCE)
2209 if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
2211 /* req is retransmit */
2212 if (sa->state == IKEV2_STATE_SA_INIT)
2215 tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
2216 ike->ispi = tmp->ispi;
2217 ike->rspi = tmp->rspi;
2218 ike->nextpayload = tmp->nextpayload;
2219 ike->version = tmp->version;
2220 ike->exchange = tmp->exchange;
2221 ike->flags = tmp->flags;
2222 ike->msgid = tmp->msgid;
2223 ike->length = tmp->length;
2224 clib_memcpy_fast(ike->payload, tmp->payload,
2225 clib_net_to_host_u32(tmp->length) - sizeof(*ike));
2226 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2227 "ispi %lx IKE_SA_INIT retransmit "
2228 "from %d.%d.%d.%d to %d.%d.%d.%d",
2230 raddr.as_u32, iaddr.as_u32);
2233 /* else ignore req */
2236 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2237 "ispi %lx IKE_SA_INIT ignore "
2238 "from %d.%d.%d.%d to %d.%d.%d.%d",
2240 raddr.as_u32, iaddr.as_u32);
2245 payload = ikep->nextpayload;
2252 /* req is not retransmit */
2257 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2259 u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2262 if (msg_id > sa->last_msg_id)
2264 sa->last_msg_id = msg_id;
2267 /* retransmitted req */
2268 else if (msg_id == sa->last_msg_id)
2271 tmp = (ike_header_t *) sa->last_res_packet_data;
2272 ike->ispi = tmp->ispi;
2273 ike->rspi = tmp->rspi;
2274 ike->nextpayload = tmp->nextpayload;
2275 ike->version = tmp->version;
2276 ike->exchange = tmp->exchange;
2277 ike->flags = tmp->flags;
2278 ike->msgid = tmp->msgid;
2279 ike->length = tmp->length;
2280 clib_memcpy_fast (ike->payload, tmp->payload,
2281 clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
2282 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2283 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2286 /* old req ignore */
2289 ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2290 msg_id, sa->raddr.as_u32, sa->iaddr.as_u32);
2297 ikev2_node_fn (vlib_main_t * vm,
2298 vlib_node_runtime_t * node, vlib_frame_t * frame)
2300 u32 n_left_from, *from, *to_next;
2301 ikev2_next_t next_index;
2302 ikev2_main_t *km = &ikev2_main;
2303 u32 thread_index = vlib_get_thread_index ();
2305 from = vlib_frame_vector_args (frame);
2306 n_left_from = frame->n_vectors;
2307 next_index = node->cached_next_index;
2309 while (n_left_from > 0)
2313 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2315 while (n_left_from > 0 && n_left_to_next > 0)
2319 u32 next0 = IKEV2_NEXT_ERROR_DROP;
2324 ikev2_sa_t *sa0 = 0;
2325 ikev2_sa_t sa; /* temporary store for SA */
2329 /* speculatively enqueue b0 to the current next frame */
2335 n_left_to_next -= 1;
2337 b0 = vlib_get_buffer (vm, bi0);
2338 ike0 = vlib_buffer_get_current (b0);
2339 vlib_buffer_advance (b0, -sizeof (*udp0));
2340 udp0 = vlib_buffer_get_current (b0);
2341 vlib_buffer_advance (b0, -sizeof (*ip40));
2342 ip40 = vlib_buffer_get_current (b0);
2344 if (ike0->version != IKE_VERSION_2)
2346 vlib_node_increment_counter (vm, ikev2_node.index,
2347 IKEV2_ERROR_NOT_IKEV2, 1);
2351 if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2354 clib_memset (sa0, 0, sizeof (*sa0));
2356 if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
2358 if (ike0->rspi == 0)
2360 sa0->raddr.as_u32 = ip40->dst_address.as_u32;
2361 sa0->iaddr.as_u32 = ip40->src_address.as_u32;
2363 r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2367 vlib_node_increment_counter (vm, ikev2_node.index,
2368 IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2370 len = clib_net_to_host_u32 (ike0->length);
2375 vlib_node_increment_counter (vm, ikev2_node.index,
2376 IKEV2_ERROR_IKE_SA_INIT_IGNORE,
2381 ikev2_process_sa_init_req (vm, sa0, ike0);
2383 if (sa0->state == IKEV2_STATE_SA_INIT)
2385 ikev2_sa_free_proposal_vector (&sa0->r_proposals);
2387 ikev2_select_proposal (sa0->i_proposals,
2388 IKEV2_PROTOCOL_IKE);
2389 ikev2_generate_sa_init_data (sa0);
2392 if (sa0->state == IKEV2_STATE_SA_INIT
2393 || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2395 len = ikev2_generate_message (sa0, ike0, 0);
2398 if (sa0->state == IKEV2_STATE_SA_INIT)
2400 /* add SA to the pool */
2401 pool_get (km->per_thread_data[thread_index].sas,
2403 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2405 per_thread_data[thread_index].sa_by_rspi,
2408 km->per_thread_data[thread_index].sas);
2412 ikev2_sa_free_all_vec (sa0);
2416 else //received sa_init without initiator flag
2418 ikev2_process_sa_init_resp (vm, sa0, ike0);
2420 if (sa0->state == IKEV2_STATE_SA_INIT)
2422 ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
2423 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2427 pool_elt_at_index (km->sais, p[0]);
2429 ikev2_complete_sa_data (sa0, sai);
2430 ikev2_calc_keys (sa0);
2431 ikev2_sa_auth_init (sa0);
2432 len = ikev2_generate_message (sa0, ike0, 0);
2436 if (sa0->state == IKEV2_STATE_SA_INIT)
2438 /* add SA to the pool */
2439 pool_get (km->per_thread_data[thread_index].sas, sa0);
2440 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2441 hash_set (km->per_thread_data[thread_index].sa_by_rspi,
2443 sa0 - km->per_thread_data[thread_index].sas);
2447 ikev2_sa_free_all_vec (sa0);
2451 else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2454 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2455 clib_net_to_host_u64 (ike0->rspi));
2459 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2462 r = ikev2_retransmit_resp (sa0, ike0);
2465 vlib_node_increment_counter (vm, ikev2_node.index,
2466 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2468 len = clib_net_to_host_u32 (ike0->length);
2473 vlib_node_increment_counter (vm, ikev2_node.index,
2474 IKEV2_ERROR_IKE_REQ_IGNORE,
2479 ikev2_process_auth_req (vm, sa0, ike0);
2480 ikev2_sa_auth (sa0);
2481 if (sa0->state == IKEV2_STATE_AUTHENTICATED)
2483 ikev2_initial_contact_cleanup (sa0);
2484 ikev2_sa_match_ts (sa0);
2485 if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
2486 ikev2_create_tunnel_interface (km->vnet_main,
2492 if (sa0->is_initiator)
2494 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2498 pool_elt_at_index (km->sais, p[0]);
2499 hash_unset (km->sa_by_ispi, sai->ispi);
2500 ikev2_sa_free_all_vec (sai);
2501 pool_put (km->sais, sai);
2506 len = ikev2_generate_message (sa0, ike0, 0);
2510 else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2513 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2514 clib_net_to_host_u64 (ike0->rspi));
2518 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2521 r = ikev2_retransmit_resp (sa0, ike0);
2524 vlib_node_increment_counter (vm, ikev2_node.index,
2525 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2527 len = clib_net_to_host_u32 (ike0->length);
2532 vlib_node_increment_counter (vm, ikev2_node.index,
2533 IKEV2_ERROR_IKE_REQ_IGNORE,
2538 ikev2_process_informational_req (vm, sa0, ike0);
2541 if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
2543 ikev2_delete_t *d, *tmp, *resp = 0;
2544 vec_foreach (d, sa0->del)
2546 ikev2_child_sa_t *ch_sa;
2547 ch_sa = ikev2_sa_get_child (sa0, d->spi,
2549 !sa0->is_initiator);
2552 ikev2_delete_tunnel_interface (km->vnet_main,
2554 if (!sa0->is_initiator)
2556 vec_add2 (resp, tmp, 1);
2557 tmp->protocol_id = d->protocol_id;
2558 tmp->spi = ch_sa->r_proposals[0].spi;
2560 ikev2_sa_del_child_sa (sa0, ch_sa);
2563 if (!sa0->is_initiator)
2565 vec_free (sa0->del);
2570 if (!sa0->is_initiator)
2572 len = ikev2_generate_message (sa0, ike0, 0);
2576 else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2579 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2580 clib_net_to_host_u64 (ike0->rspi));
2584 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2587 r = ikev2_retransmit_resp (sa0, ike0);
2590 vlib_node_increment_counter (vm, ikev2_node.index,
2591 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2593 len = clib_net_to_host_u32 (ike0->length);
2598 vlib_node_increment_counter (vm, ikev2_node.index,
2599 IKEV2_ERROR_IKE_REQ_IGNORE,
2604 ikev2_process_create_child_sa_req (vm, sa0, ike0);
2607 if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
2610 vec_free (sa0->childs);
2611 ikev2_child_sa_t *child;
2612 vec_add2 (sa0->childs, child, 1);
2613 child->r_proposals = sa0->rekey[0].r_proposal;
2614 child->i_proposals = sa0->rekey[0].i_proposal;
2615 child->tsi = sa0->rekey[0].tsi;
2616 child->tsr = sa0->rekey[0].tsr;
2617 ikev2_create_tunnel_interface (km->vnet_main,
2620 child - sa0->childs,
2623 if (sa0->is_initiator)
2625 vec_del1 (sa0->rekey, 0);
2629 len = ikev2_generate_message (sa0, ike0, 0);
2636 ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2637 "received from %d.%d.%d.%d to %d.%d.%d.%d",
2639 ip40->src_address.as_u32,
2640 ip40->dst_address.as_u32);
2644 /* if we are sending packet back, rewrite headers */
2647 next0 = IKEV2_NEXT_IP4_LOOKUP;
2648 if (sa0->is_initiator)
2650 ip40->dst_address.as_u32 = sa0->raddr.as_u32;
2651 ip40->src_address.as_u32 = sa0->iaddr.as_u32;
2655 ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
2656 ip40->src_address.as_u32 = sa0->raddr.as_u32;
2659 clib_host_to_net_u16 (len + sizeof (udp_header_t));
2661 b0->current_length =
2662 len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2663 ip40->length = clib_host_to_net_u16 (b0->current_length);
2664 ip40->checksum = ip4_header_checksum (ip40);
2667 if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
2668 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
2670 ikev2_child_sa_t *c;
2672 vec_foreach (c, sa0->childs)
2673 ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
2675 ikev2_delete_sa (sa0);
2677 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2679 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2680 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2682 ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
2683 t->sw_if_index = sw_if_index0;
2684 t->next_index = next0;
2687 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2688 n_left_to_next, bi0, next0);
2691 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2694 vlib_node_increment_counter (vm, ikev2_node.index,
2695 IKEV2_ERROR_PROCESSED, frame->n_vectors);
2696 return frame->n_vectors;
2700 VLIB_REGISTER_NODE (ikev2_node,static) = {
2701 .function = ikev2_node_fn,
2703 .vector_size = sizeof (u32),
2704 .format_trace = format_ikev2_trace,
2705 .type = VLIB_NODE_TYPE_INTERNAL,
2707 .n_errors = ARRAY_LEN(ikev2_error_strings),
2708 .error_strings = ikev2_error_strings,
2710 .n_next_nodes = IKEV2_N_NEXT,
2713 [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
2714 [IKEV2_NEXT_ERROR_DROP] = "error-drop",
2719 // set ikev2 proposals when vpp is used as initiator
2720 static clib_error_t *
2721 ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
2722 ikev2_transforms_set * ts,
2723 ikev2_sa_proposal_t ** proposals, int is_ike)
2726 ikev2_main_t *km = &ikev2_main;
2727 ikev2_sa_proposal_t *proposal;
2728 vec_add2 (*proposals, proposal, 1);
2729 ikev2_sa_transform_t *td;
2734 vec_foreach (td, km->supported_transforms)
2736 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
2737 && td->encr_type == ts->crypto_alg
2738 && td->key_len == ts->crypto_key_size / 8)
2741 attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
2742 attr[1] = clib_host_to_net_u16 (td->key_len << 3);
2743 vec_add (td->attrs, (u8 *) attr, 4);
2744 vec_add1 (proposal->transforms, *td);
2753 r = clib_error_return (0, "Unsupported algorithm");
2759 vec_foreach (td, km->supported_transforms)
2761 if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
2762 && td->integ_type == ts->integ_alg)
2764 vec_add1 (proposal->transforms, *td);
2772 ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
2773 r = clib_error_return (0, "Unsupported algorithm");
2781 vec_foreach (td, km->supported_transforms)
2783 if (td->type == IKEV2_TRANSFORM_TYPE_PRF
2784 && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
2786 vec_add1 (proposal->transforms, *td);
2793 r = clib_error_return (0, "Unsupported algorithm");
2799 if (is_ike || ts->dh_type != IKEV2_TRANSFORM_DH_TYPE_NONE)
2802 vec_foreach (td, km->supported_transforms)
2804 if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
2806 vec_add1 (proposal->transforms, *td);
2809 sa->dh_group = td->dh_type;
2817 r = clib_error_return (0, "Unsupported algorithm");
2825 vec_foreach (td, km->supported_transforms)
2827 if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
2829 vec_add1 (proposal->transforms, *td);
2836 r = clib_error_return (0, "Unsupported algorithm");
2845 static ikev2_profile_t *
2846 ikev2_profile_index_by_name (u8 * name)
2848 ikev2_main_t *km = &ikev2_main;
2851 p = mhash_get (&km->profile_index_by_name, name);
2855 return pool_elt_at_index (km->profiles, p[0]);
2860 ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst,
2869 b0 = vlib_get_buffer (vm, bi0);
2870 vlib_buffer_advance (b0, -sizeof (udp_header_t));
2871 udp0 = vlib_buffer_get_current (b0);
2872 vlib_buffer_advance (b0, -sizeof (ip4_header_t));
2873 ip40 = vlib_buffer_get_current (b0);
2876 ip40->ip_version_and_header_length = 0x45;
2878 ip40->fragment_id = 0;
2879 ip40->flags_and_fragment_offset = 0;
2881 ip40->protocol = IP_PROTOCOL_UDP;
2882 ip40->dst_address.as_u32 = dst->as_u32;
2883 ip40->src_address.as_u32 = src->as_u32;
2884 udp0->dst_port = clib_host_to_net_u16 (500);
2885 udp0->src_port = clib_host_to_net_u16 (500);
2886 udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
2888 b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2889 ip40->length = clib_host_to_net_u16 (b0->current_length);
2890 ip40->checksum = ip4_header_checksum (ip40);
2893 /* send the request */
2894 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
2895 to_next = vlib_frame_vector_args (f);
2898 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
2903 ikev2_get_new_ike_header_buff (vlib_main_t * vm, ike_header_t ** ike)
2906 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2911 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
2912 *ike = vlib_buffer_get_current (b0);
2917 ikev2_set_local_key (vlib_main_t * vm, u8 * file)
2919 ikev2_main_t *km = &ikev2_main;
2921 km->pkey = ikev2_load_key_file (file);
2922 if (km->pkey == NULL)
2923 return clib_error_return (0, "load key '%s' failed", file);
2929 ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
2931 ikev2_main_t *km = &ikev2_main;
2936 if (ikev2_profile_index_by_name (name))
2937 return clib_error_return (0, "policy %v already exists", name);
2939 pool_get (km->profiles, p);
2940 clib_memset (p, 0, sizeof (*p));
2941 p->name = vec_dup (name);
2942 p->responder.sw_if_index = ~0;
2944 uword index = p - km->profiles;
2945 mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
2949 p = ikev2_profile_index_by_name (name);
2951 return clib_error_return (0, "policy %v does not exists", name);
2954 pool_put (km->profiles, p);
2955 mhash_unset (&km->profile_index_by_name, name, 0);
2961 ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
2962 u8 * auth_data, u8 data_hex_format)
2967 p = ikev2_profile_index_by_name (name);
2971 r = clib_error_return (0, "unknown profile %v", name);
2974 vec_free (p->auth.data);
2975 p->auth.method = auth_method;
2976 p->auth.data = vec_dup (auth_data);
2977 p->auth.hex = data_hex_format;
2979 if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
2981 vec_add1 (p->auth.data, 0);
2983 EVP_PKEY_free (p->auth.key);
2984 p->auth.key = ikev2_load_cert_file (auth_data);
2985 if (p->auth.key == NULL)
2986 return clib_error_return (0, "load cert '%s' failed", auth_data);
2993 ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
2999 if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
3000 && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
3002 r = clib_error_return (0, "unsupported identity type %U",
3003 format_ikev2_id_type, id_type);
3007 p = ikev2_profile_index_by_name (name);
3011 r = clib_error_return (0, "unknown profile %v", name);
3017 vec_free (p->loc_id.data);
3018 p->loc_id.type = id_type;
3019 p->loc_id.data = vec_dup (data);
3023 vec_free (p->rem_id.data);
3024 p->rem_id.type = id_type;
3025 p->rem_id.data = vec_dup (data);
3032 ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
3033 u16 start_port, u16 end_port, ip4_address_t start_addr,
3034 ip4_address_t end_addr, int is_local)
3039 p = ikev2_profile_index_by_name (name);
3043 r = clib_error_return (0, "unknown profile %v", name);
3049 p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
3050 p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
3051 p->loc_ts.start_port = start_port;
3052 p->loc_ts.end_port = end_port;
3053 p->loc_ts.protocol_id = protocol_id;
3054 p->loc_ts.ts_type = 7;
3058 p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
3059 p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
3060 p->rem_ts.start_port = start_port;
3061 p->rem_ts.end_port = end_port;
3062 p->rem_ts.protocol_id = protocol_id;
3063 p->rem_ts.ts_type = 7;
3071 ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
3072 u32 sw_if_index, ip4_address_t ip4)
3077 p = ikev2_profile_index_by_name (name);
3081 r = clib_error_return (0, "unknown profile %v", name);
3085 p->responder.sw_if_index = sw_if_index;
3086 p->responder.ip4 = ip4;
3092 ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
3093 ikev2_transform_encr_type_t crypto_alg,
3094 ikev2_transform_integ_type_t integ_alg,
3095 ikev2_transform_dh_type_t dh_type,
3096 u32 crypto_key_size)
3101 p = ikev2_profile_index_by_name (name);
3105 r = clib_error_return (0, "unknown profile %v", name);
3109 p->ike_ts.crypto_alg = crypto_alg;
3110 p->ike_ts.integ_alg = integ_alg;
3111 p->ike_ts.dh_type = dh_type;
3112 p->ike_ts.crypto_key_size = crypto_key_size;
3117 ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
3118 ikev2_transform_encr_type_t crypto_alg,
3119 ikev2_transform_integ_type_t integ_alg,
3120 ikev2_transform_dh_type_t dh_type,
3121 u32 crypto_key_size)
3126 p = ikev2_profile_index_by_name (name);
3130 r = clib_error_return (0, "unknown profile %v", name);
3134 p->esp_ts.crypto_alg = crypto_alg;
3135 p->esp_ts.integ_alg = integ_alg;
3136 p->esp_ts.dh_type = dh_type;
3137 p->esp_ts.crypto_key_size = crypto_key_size;
3142 ikev2_set_profile_tunnel_interface (vlib_main_t * vm,
3143 u8 * name, u32 sw_if_index)
3148 p = ikev2_profile_index_by_name (name);
3152 r = clib_error_return (0, "unknown profile %v", name);
3156 p->tun_itf = sw_if_index;
3162 ikev2_set_profile_udp_encap (vlib_main_t * vm, u8 * name)
3164 ikev2_profile_t *p = ikev2_profile_index_by_name (name);
3169 r = clib_error_return (0, "unknown profile %v", name);
3178 ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
3179 u64 lifetime, u32 jitter, u32 handover,
3185 p = ikev2_profile_index_by_name (name);
3189 r = clib_error_return (0, "unknown profile %v", name);
3193 p->lifetime = lifetime;
3194 p->lifetime_jitter = jitter;
3195 p->handover = handover;
3196 p->lifetime_maxdata = maxdata;
3201 ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
3205 ip4_main_t *im = &ip4_main;
3206 ikev2_main_t *km = &ikev2_main;
3208 p = ikev2_profile_index_by_name (name);
3212 r = clib_error_return (0, "unknown profile %v", name);
3216 if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
3218 r = clib_error_return (0, "responder not set for profile %v", name);
3223 /* Create the Initiator Request */
3227 ip_lookup_main_t *lm = &im->lookup_main;
3229 int len = sizeof (ike_header_t);
3231 /* Get own iface IP */
3233 lm->if_address_pool_index_by_sw_if_index[p->responder.sw_if_index];
3234 ip_interface_address_t *if_add =
3235 pool_elt_at_index (lm->if_address_pool, if_add_index0);
3236 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
3238 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3240 /* Prepare the SA and the IKE payload */
3242 clib_memset (&sa, 0, sizeof (ikev2_sa_t));
3243 ikev2_payload_chain_t *chain = 0;
3244 ikev2_payload_new_chain (chain);
3246 /* Build the IKE proposal payload */
3247 ikev2_sa_proposal_t *proposals = 0;
3248 ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
3249 proposals[0].proposal_num = 1;
3250 proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
3252 /* Add and then cleanup proposal data */
3253 ikev2_payload_add_sa (chain, proposals);
3254 ikev2_sa_free_proposal_vector (&proposals);
3256 sa.is_initiator = 1;
3257 sa.profile_index = km->profiles - p;
3258 sa.is_profile_index_set = 1;
3259 sa.state = IKEV2_STATE_SA_INIT;
3260 sa.tun_itf = p->tun_itf;
3261 sa.udp_encap = p->udp_encap;
3262 sa.is_tun_itf_set = 1;
3263 sa.initial_contact = 1;
3264 ikev2_generate_sa_init_data (&sa);
3265 ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
3266 ikev2_payload_add_nonce (chain, sa.i_nonce);
3268 /* Build the child SA proposal */
3269 vec_resize (sa.childs, 1);
3270 ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
3271 &sa.childs[0].i_proposals, 0);
3272 sa.childs[0].i_proposals[0].proposal_num = 1;
3273 sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
3274 RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
3275 sizeof (sa.childs[0].i_proposals[0].spi));
3279 /* Add NAT detection notification messages (mandatory) */
3280 u8 nat_detection_source[8 + 8 + 4 + 2];
3281 u8 *nat_detection_sha1 = vec_new (u8, 20);
3283 u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
3284 clib_memcpy_fast (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
3285 tmpspi = clib_host_to_net_u64 (sa.rspi);
3286 clib_memcpy_fast (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
3287 u16 tmpport = clib_host_to_net_u16 (500);
3288 clib_memcpy_fast (&nat_detection_source[8 + 8 + 4], &tmpport,
3290 u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
3291 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3292 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3293 nat_detection_sha1);
3294 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
3295 nat_detection_sha1);
3296 tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
3297 clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3298 SHA1 (nat_detection_source, sizeof (nat_detection_source),
3299 nat_detection_sha1);
3300 ikev2_payload_add_notify (chain,
3301 IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
3302 nat_detection_sha1);
3303 vec_free (nat_detection_sha1);
3305 u8 *sig_hash_algo = vec_new (u8, 8);
3306 u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
3307 clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
3308 ikev2_payload_add_notify (chain,
3309 IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
3311 vec_free (sig_hash_algo);
3314 /* Buffer update and boilerplate */
3315 len += vec_len (chain->data);
3316 ike0->nextpayload = chain->first_payload_type;
3317 ike0->length = clib_host_to_net_u32 (len);
3318 clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
3319 ikev2_payload_destroy_chain (chain);
3321 ike0->version = IKE_VERSION_2;
3322 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3323 ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
3324 ike0->ispi = sa.ispi;
3328 /* store whole IKE payload - needed for PSK auth */
3329 vec_free (sa.last_sa_init_req_packet_data);
3330 vec_add (sa.last_sa_init_req_packet_data, ike0, len);
3332 /* add data to the SA then add it to the pool */
3333 sa.iaddr.as_u32 = if_ip->as_u32;
3334 sa.raddr.as_u32 = p->responder.ip4.as_u32;
3335 sa.i_id.type = p->loc_id.type;
3336 sa.i_id.data = vec_dup (p->loc_id.data);
3337 sa.i_auth.method = p->auth.method;
3338 sa.i_auth.hex = p->auth.hex;
3339 sa.i_auth.data = vec_dup (p->auth.data);
3340 vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
3341 vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
3343 ikev2_initial_contact_cleanup (&sa);
3345 /* add SA to the pool */
3346 ikev2_sa_t *sa0 = 0;
3347 pool_get (km->sais, sa0);
3348 clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3349 hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
3351 ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
3353 ikev2_elog_exchange ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to "
3354 "%d.%d.%d.%d", clib_host_to_net_u64 (sa0->ispi), 0,
3355 p->responder.ip4.as_u32);
3362 ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3363 ikev2_child_sa_t * csa)
3365 /* Create the Initiator notification for child SA removal */
3366 ikev2_main_t *km = &ikev2_main;
3371 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3374 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3375 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3376 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3377 vec_resize (sa->del, 1);
3378 sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
3379 sa->del->spi = csa->i_proposals->spi;
3380 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3381 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3382 len = ikev2_generate_message (sa, ike0, 0);
3384 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3386 /* delete local child SA */
3387 ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
3388 ikev2_sa_del_child_sa (sa, csa);
3392 ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
3395 ikev2_main_t *km = &ikev2_main;
3396 ikev2_main_per_thread_data_t *tkm;
3397 ikev2_sa_t *fsa = 0;
3398 ikev2_child_sa_t *fchild = 0;
3400 /* Search for the child SA */
3401 vec_foreach (tkm, km->per_thread_data)
3407 pool_foreach (sa, tkm->sas, ({
3408 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3418 if (!fchild || !fsa)
3420 r = clib_error_return (0, "Child SA not found");
3425 ikev2_delete_child_sa_internal (vm, fsa, fchild);
3432 ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
3435 ikev2_main_t *km = &ikev2_main;
3436 ikev2_main_per_thread_data_t *tkm;
3437 ikev2_sa_t *fsa = 0;
3438 ikev2_main_per_thread_data_t *ftkm = 0;
3440 /* Search for the IKE SA */
3441 vec_foreach (tkm, km->per_thread_data)
3447 pool_foreach (sa, tkm->sas, ({
3448 if (sa->ispi == ispi)
3460 r = clib_error_return (0, "IKE SA not found");
3465 /* Create the Initiator notification for IKE SA removal */
3471 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3474 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3475 ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
3476 ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
3477 vec_resize (fsa->del, 1);
3478 fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3479 fsa->del->spi = ispi;
3480 ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
3481 fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3482 len = ikev2_generate_message (fsa, ike0, 0);
3484 ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
3488 /* delete local SA */
3489 ikev2_child_sa_t *c;
3490 vec_foreach (c, fsa->childs)
3492 ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
3493 ikev2_sa_del_child_sa (fsa, c);
3495 ikev2_sa_free_all_vec (fsa);
3496 uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
3499 hash_unset (ftkm->sa_by_rspi, fsa->rspi);
3500 pool_put (ftkm->sas, fsa);
3508 ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3509 ikev2_child_sa_t * csa)
3511 /* Create the Initiator request for create child SA */
3517 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3520 ike0->version = IKE_VERSION_2;
3521 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3522 ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
3523 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3524 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3525 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3526 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3528 ikev2_rekey_t *rekey;
3529 vec_add2 (sa->rekey, rekey, 1);
3530 ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
3533 RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
3534 rekey->spi = proposals[0].spi;
3535 rekey->ispi = csa->i_proposals->spi;
3536 len = ikev2_generate_message (sa, ike0, proposals);
3537 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3538 vec_free (proposals);
3542 ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
3545 ikev2_main_t *km = &ikev2_main;
3546 ikev2_main_per_thread_data_t *tkm;
3547 ikev2_sa_t *fsa = 0;
3548 ikev2_child_sa_t *fchild = 0;
3550 /* Search for the child SA */
3551 vec_foreach (tkm, km->per_thread_data)
3557 pool_foreach (sa, tkm->sas, ({
3558 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3568 if (!fchild || !fsa)
3570 r = clib_error_return (0, "Child SA not found");
3575 ikev2_rekey_child_sa_internal (vm, fsa, fchild);
3582 ikev2_init (vlib_main_t * vm)
3584 ikev2_main_t *km = &ikev2_main;
3585 vlib_thread_main_t *tm = vlib_get_thread_main ();
3588 clib_memset (km, 0, sizeof (ikev2_main_t));
3589 km->vnet_main = vnet_get_main ();
3592 ikev2_crypto_init (km);
3594 mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
3596 vec_validate (km->per_thread_data, tm->n_vlib_mains - 1);
3597 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
3599 km->per_thread_data[thread_id].sa_by_rspi =
3600 hash_create (0, sizeof (uword));
3603 km->sa_by_ispi = hash_create (0, sizeof (uword));
3604 km->sw_if_indices = hash_create (0, 0);
3606 udp_register_dst_port (vm, 500, ikev2_node.index, 1);
3608 ikev2_cli_reference ();
3610 km->log_level = IKEV2_LOG_ERROR;
3611 km->log_class = vlib_log_register_class ("ikev2", 0);
3616 VLIB_INIT_FUNCTION (ikev2_init) =
3618 .runs_after = VLIB_INITS("ipsec_init"),
3623 ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa,
3626 ikev2_main_t *km = &ikev2_main;
3627 ikev2_profile_t *p = 0;
3628 vlib_main_t *vm = km->vlib_main;
3629 f64 now = vlib_time_now (vm);
3632 if (sa->is_profile_index_set)
3633 p = pool_elt_at_index (km->profiles, sa->profile_index);
3635 if (sa->is_initiator && p && csa->time_to_expiration
3636 && now > csa->time_to_expiration)
3638 if (!csa->is_expired || csa->rekey_retries > 0)
3640 ikev2_rekey_child_sa_internal (vm, sa, csa);
3641 csa->time_to_expiration = now + p->handover;
3642 csa->is_expired = 1;
3643 if (csa->rekey_retries == 0)
3645 csa->rekey_retries = 5;
3647 else if (csa->rekey_retries > 0)
3649 csa->rekey_retries--;
3650 ikev2_log_debug ("Rekeying Child SA 0x%x, retries left %d",
3651 csa->i_proposals->spi, csa->rekey_retries);
3652 if (csa->rekey_retries == 0)
3654 csa->rekey_retries = -1;
3661 csa->time_to_expiration = 0;
3662 ikev2_delete_child_sa_internal (vm, sa, csa);
3669 ipip_tunnel_t *ipip = NULL;
3670 u32 sw_if_index = sa->is_tun_itf_set ? sa->tun_itf : ~0;
3671 if (~0 == sw_if_index)
3673 ip46_address_t local_ip;
3674 ip46_address_t remote_ip;
3675 if (sa->is_initiator)
3677 ip46_address_set_ip4 (&local_ip, &sa->iaddr);
3678 ip46_address_set_ip4 (&remote_ip, &sa->raddr);
3682 ip46_address_set_ip4 (&local_ip, &sa->raddr);
3683 ip46_address_set_ip4 (&remote_ip, &sa->iaddr);
3687 ipip_tunnel_key_t key = {
3690 .transport = IPIP_TRANSPORT_IP4,
3695 ipip = ipip_tunnel_db_find (&key);
3698 sw_if_index = ipip->sw_if_index;
3704 vec_add1 (sas_in, csa->remote_sa_id);
3705 ipsec_tun_protect_update (sw_if_index, NULL, csa->local_sa_id, sas_in);
3706 ipsec_sa_unlock_id (ikev2_flip_alternate_sa_bit (csa->remote_sa_id));
3713 ikev2_set_log_level (ikev2_log_level_t log_level)
3715 ikev2_main_t *km = &ikev2_main;
3717 if (log_level >= IKEV2_LOG_MAX)
3719 ikev2_log_error ("unknown logging level %d", log_level);
3723 km->log_level = log_level;
3728 ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
3730 ikev2_main_t *km = &ikev2_main;
3731 vlib_main_t *vm = km->vlib_main;
3732 ikev2_main_per_thread_data_t *tkm;
3733 ikev2_sa_t *fsa = 0;
3734 ikev2_profile_t *p = 0;
3735 ikev2_child_sa_t *fchild = 0;
3736 f64 now = vlib_time_now (vm);
3737 vlib_counter_t counts;
3739 /* Search for the SA and child SA */
3740 vec_foreach (tkm, km->per_thread_data)
3746 pool_foreach (sa, tkm->sas, ({
3747 fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
3756 vlib_get_combined_counter (&ipsec_sa_counters,
3757 ipsec_sa->stat_index, &counts);
3759 if (fsa && fsa->is_profile_index_set)
3760 p = pool_elt_at_index (km->profiles, fsa->profile_index);
3762 if (fchild && p && p->lifetime_maxdata)
3764 if (!fchild->is_expired && counts.bytes > p->lifetime_maxdata)
3766 fchild->time_to_expiration = now;
3772 ikev2_process_pending_sa_init (ikev2_main_t * km)
3779 hash_foreach (ispi, sai, km->sa_by_ispi,
3781 sa = pool_elt_at_index (km->sais, sai);
3783 if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
3786 vlib_buffer_t * b = vlib_get_buffer (km->vlib_main, bi0);
3787 clib_memcpy_fast (vlib_buffer_get_current (b),
3788 sa->last_sa_init_req_packet_data,
3789 vec_len (sa->last_sa_init_req_packet_data));
3790 ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0,
3791 vec_len (sa->last_sa_init_req_packet_data));
3796 static vlib_node_registration_t ikev2_mngr_process_node;
3799 ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
3802 ikev2_main_t *km = &ikev2_main;
3803 ipsec_main_t *im = &ipsec_main;
3808 vlib_process_wait_for_event_or_clock (vm, 1);
3809 vlib_process_get_events (vm, NULL);
3811 /* process ike child sas */
3812 ikev2_main_per_thread_data_t *tkm;
3813 vec_foreach (tkm, km->per_thread_data)
3817 pool_foreach (sa, tkm->sas, ({
3818 ikev2_child_sa_t *c;
3820 if (sa->old_remote_id_present && 0 > sa->old_id_expiration)
3822 sa->old_remote_id_present = 0;
3826 sa->old_id_expiration -= 1;
3828 vec_foreach (c, sa->childs)
3830 req_sent |= ikev2_mngr_process_child_sa(sa, c, del_old_ids);
3836 /* process ipsec sas */
3839 pool_foreach (sa, im->sad, ({
3840 ikev2_mngr_process_ipsec_sa(sa);
3844 ikev2_process_pending_sa_init (km);
3848 vlib_process_wait_for_event_or_clock (vm, 5);
3849 vlib_process_get_events (vm, NULL);
3858 VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
3859 .function = ikev2_mngr_process_fn,
3860 .type = VLIB_NODE_TYPE_PROCESS,
3862 "ikev2-manager-process",
3865 VLIB_PLUGIN_REGISTER () = {
3866 .version = VPP_BUILD_VER,
3867 .description = "Internet Key Exchange (IKEv2) Protocol",
3872 * fd.io coding-style-patch-verification: ON
3875 * eval: (c-set-style "gnu")