2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "child_create.h"
21 #include <sa/ikev2/keymat_v2.h>
22 #include <crypto/diffie_hellman.h>
23 #include <credentials/certificates/x509.h>
24 #include <encoding/payloads/sa_payload.h>
25 #include <encoding/payloads/ke_payload.h>
26 #include <encoding/payloads/ts_payload.h>
27 #include <encoding/payloads/nonce_payload.h>
28 #include <encoding/payloads/notify_payload.h>
29 #include <processing/jobs/delete_ike_sa_job.h>
30 #include <processing/jobs/inactivity_job.h>
33 typedef struct private_child_create_t private_child_create_t
;
36 * Private members of a child_create_t task.
38 struct private_child_create_t
{
41 * Public methods and task_t interface.
43 child_create_t
public;
51 * Are we the initiator?
61 * nonce chosen by peer
66 * config to create the CHILD_SA from
71 * list of proposal candidates
73 linked_list_t
*proposals
;
76 * selected proposal to use for CHILD_SA
81 * traffic selectors for initiators side
86 * traffic selectors for responders side
91 * source of triggering packet
93 traffic_selector_t
*packet_tsi
;
96 * destination of triggering packet
98 traffic_selector_t
*packet_tsr
;
101 * optional diffie hellman exchange
103 diffie_hellman_t
*dh
;
106 * group used for DH exchange
108 diffie_hellman_group_t dh_group
;
116 * mode the new CHILD_SA uses (transport/tunnel/beet)
121 * peer accepts TFC padding for this SA
126 * IPComp transform to use
128 ipcomp_transform_t ipcomp
;
131 * IPComp transform proposed or accepted by the other peer
133 ipcomp_transform_t ipcomp_received
;
141 * SPI received in proposal
146 * Own allocated Compression Parameter Index (CPI)
151 * Other Compression Parameter Index (CPI), received via IPCOMP_SUPPORTED
156 * reqid to use if we are rekeying
161 * CHILD_SA which gets established
163 child_sa_t
*child_sa
;
166 * successfully established the CHILD?
171 * whether the CHILD_SA rekeys an existing one
176 * whether we are retrying with another DH group
182 * get the nonce from a message
184 static status_t
get_nonce(message_t
*message
, chunk_t
*nonce
)
186 nonce_payload_t
*payload
;
188 payload
= (nonce_payload_t
*)message
->get_payload(message
, NONCE
);
193 *nonce
= payload
->get_nonce(payload
);
198 * generate a new nonce to include in a CREATE_CHILD_SA message
200 static status_t
generate_nonce(private_child_create_t
*this)
204 nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
207 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
210 if (!nonceg
->allocate_nonce(nonceg
, NONCE_SIZE
, &this->my_nonce
))
212 DBG1(DBG_IKE
, "nonce allocation failed");
213 nonceg
->destroy(nonceg
);
216 nonceg
->destroy(nonceg
);
222 * Check a list of traffic selectors if any selector belongs to host
224 static bool ts_list_is_host(linked_list_t
*list
, host_t
*host
)
226 traffic_selector_t
*ts
;
228 enumerator_t
*enumerator
= list
->create_enumerator(list
);
230 while (is_host
&& enumerator
->enumerate(enumerator
, (void**)&ts
))
232 is_host
= is_host
&& ts
->is_host(ts
, host
);
234 enumerator
->destroy(enumerator
);
239 * Allocate SPIs and update proposals
241 static bool allocate_spi(private_child_create_t
*this)
243 enumerator_t
*enumerator
;
244 proposal_t
*proposal
;
246 /* TODO: allocate additional SPI for AH if we have such proposals */
247 this->my_spi
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
252 enumerator
= this->proposals
->create_enumerator(this->proposals
);
253 while (enumerator
->enumerate(enumerator
, &proposal
))
255 proposal
->set_spi(proposal
, this->my_spi
);
257 enumerator
->destroy(enumerator
);
261 this->proposal
->set_spi(this->proposal
, this->my_spi
);
269 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
271 static void schedule_inactivity_timeout(private_child_create_t
*this)
276 timeout
= this->config
->get_inactivity(this->config
);
279 close_ike
= lib
->settings
->get_bool(lib
->settings
,
280 "%s.inactivity_close_ike", FALSE
, charon
->name
);
281 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
282 inactivity_job_create(this->child_sa
->get_reqid(this->child_sa
),
283 timeout
, close_ike
), timeout
);
288 * Check if we have a an address pool configured
290 static bool have_pool(ike_sa_t
*ike_sa
)
292 enumerator_t
*enumerator
;
293 peer_cfg_t
*peer_cfg
;
297 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
300 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
301 if (enumerator
->enumerate(enumerator
, &pool
))
305 enumerator
->destroy(enumerator
);
311 * Get host to use for dynamic traffic selectors
313 static host_t
*get_dynamic_host(ike_sa_t
*ike_sa
, bool local
)
315 enumerator_t
*enumerator
;
318 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
319 if (!enumerator
->enumerate(enumerator
, &host
))
323 host
= ike_sa
->get_my_host(ike_sa
);
327 if (have_pool(ike_sa
))
329 /* we have an IP address pool, but didn't negotiate a
335 host
= ike_sa
->get_other_host(ike_sa
);
339 enumerator
->destroy(enumerator
);
344 * Install a CHILD_SA for usage, return value:
345 * - FAILED: no acceptable proposal
346 * - INVALID_ARG: diffie hellman group inacceptable
347 * - NOT_FOUND: TS inacceptable
349 static status_t
select_and_install(private_child_create_t
*this,
350 bool no_dh
, bool ike_auth
)
352 status_t status
, status_i
, status_o
;
353 chunk_t nonce_i
, nonce_r
;
354 chunk_t encr_i
= chunk_empty
, encr_r
= chunk_empty
;
355 chunk_t integ_i
= chunk_empty
, integ_r
= chunk_empty
;
356 linked_list_t
*my_ts
, *other_ts
;
360 if (this->proposals
== NULL
)
362 DBG1(DBG_IKE
, "SA payload missing in message");
365 if (this->tsi
== NULL
|| this->tsr
== NULL
)
367 DBG1(DBG_IKE
, "TS payloads missing in message");
371 me
= this->ike_sa
->get_my_host(this->ike_sa
);
372 other
= this->ike_sa
->get_other_host(this->ike_sa
);
374 private = this->ike_sa
->supports_extension(this->ike_sa
, EXT_STRONGSWAN
);
375 this->proposal
= this->config
->select_proposal(this->config
,
376 this->proposals
, no_dh
, private);
377 if (this->proposal
== NULL
)
379 DBG1(DBG_IKE
, "no acceptable proposal found");
382 this->other_spi
= this->proposal
->get_spi(this->proposal
);
384 if (!this->initiator
&& !allocate_spi(this))
385 { /* responder has no SPI allocated yet */
386 DBG1(DBG_IKE
, "allocating SPI failed");
389 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
391 if (!this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
395 if (this->proposal
->get_algorithm(this->proposal
, DIFFIE_HELLMAN_GROUP
,
398 DBG1(DBG_IKE
, "DH group %N inacceptable, requesting %N",
399 diffie_hellman_group_names
, this->dh_group
,
400 diffie_hellman_group_names
, group
);
401 this->dh_group
= group
;
404 /* the selected proposal does not use a DH group */
405 DBG1(DBG_IKE
, "ignoring KE exchange, agreed on a non-PFS proposal");
406 DESTROY_IF(this->dh
);
408 this->dh_group
= MODP_NONE
;
413 nonce_i
= this->my_nonce
;
414 nonce_r
= this->other_nonce
;
416 other_ts
= this->tsr
;
420 nonce_r
= this->my_nonce
;
421 nonce_i
= this->other_nonce
;
423 other_ts
= this->tsi
;
425 my_ts
= this->config
->get_traffic_selectors(this->config
, TRUE
, my_ts
,
426 get_dynamic_host(this->ike_sa
, TRUE
));
427 other_ts
= this->config
->get_traffic_selectors(this->config
, FALSE
, other_ts
,
428 get_dynamic_host(this->ike_sa
, FALSE
));
434 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
435 NARROW_INITIATOR_POST_NOAUTH
, my_ts
, other_ts
);
439 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
440 NARROW_INITIATOR_POST_AUTH
, my_ts
, other_ts
);
445 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
446 NARROW_RESPONDER
, my_ts
, other_ts
);
449 if (my_ts
->get_count(my_ts
) == 0 || other_ts
->get_count(other_ts
) == 0)
451 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
452 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
453 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
457 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
458 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
462 this->tsr
= other_ts
;
467 this->tsi
= other_ts
;
470 if (!this->initiator
)
472 /* check if requested mode is acceptable, downgrade if required */
476 if (!this->config
->use_proxy_mode(this->config
) &&
477 (!ts_list_is_host(this->tsi
, other
) ||
478 !ts_list_is_host(this->tsr
, me
))
481 this->mode
= MODE_TUNNEL
;
482 DBG1(DBG_IKE
, "not using transport mode, not host-to-host");
484 else if (this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
))
486 this->mode
= MODE_TUNNEL
;
487 DBG1(DBG_IKE
, "not using transport mode, connection NATed");
491 if (!ts_list_is_host(this->tsi
, NULL
) ||
492 !ts_list_is_host(this->tsr
, NULL
))
494 this->mode
= MODE_TUNNEL
;
495 DBG1(DBG_IKE
, "not using BEET mode, not host-to-host");
503 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
504 this->child_sa
->set_ipcomp(this->child_sa
, this->ipcomp
);
505 this->child_sa
->set_mode(this->child_sa
, this->mode
);
506 this->child_sa
->set_protocol(this->child_sa
,
507 this->proposal
->get_protocol(this->proposal
));
509 if (this->my_cpi
== 0 || this->other_cpi
== 0 || this->ipcomp
== IPCOMP_NONE
)
511 this->my_cpi
= this->other_cpi
= 0;
512 this->ipcomp
= IPCOMP_NONE
;
514 status_i
= status_o
= FAILED
;
515 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
,
516 this->dh
, nonce_i
, nonce_r
, &encr_i
, &integ_i
, &encr_r
, &integ_r
))
520 status_i
= this->child_sa
->install(this->child_sa
,
521 encr_r
, integ_r
, this->my_spi
, this->my_cpi
,
522 TRUE
, this->tfcv3
, my_ts
, other_ts
);
523 status_o
= this->child_sa
->install(this->child_sa
,
524 encr_i
, integ_i
, this->other_spi
, this->other_cpi
,
525 FALSE
, this->tfcv3
, my_ts
, other_ts
);
529 status_i
= this->child_sa
->install(this->child_sa
,
530 encr_i
, integ_i
, this->my_spi
, this->my_cpi
,
531 TRUE
, this->tfcv3
, my_ts
, other_ts
);
532 status_o
= this->child_sa
->install(this->child_sa
,
533 encr_r
, integ_r
, this->other_spi
, this->other_cpi
,
534 FALSE
, this->tfcv3
, my_ts
, other_ts
);
537 chunk_clear(&integ_i
);
538 chunk_clear(&integ_r
);
539 chunk_clear(&encr_i
);
540 chunk_clear(&encr_r
);
542 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
544 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
545 (status_i
!= SUCCESS
) ?
"inbound " : "",
546 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ?
"and ": "",
547 (status_o
!= SUCCESS
) ?
"outbound " : "");
553 status
= this->child_sa
->add_policies(this->child_sa
, my_ts
, other_ts
);
557 /* use a copy of the traffic selectors, as the POST hook should not
559 my_ts
= this->tsr
->clone_offset(this->tsr
,
560 offsetof(traffic_selector_t
, clone
));
561 other_ts
= this->tsi
->clone_offset(this->tsi
,
562 offsetof(traffic_selector_t
, clone
));
563 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
564 NARROW_RESPONDER_POST
, my_ts
, other_ts
);
565 if (my_ts
->get_count(my_ts
) == 0 || other_ts
->get_count(other_ts
) == 0)
571 status
= this->child_sa
->add_policies(this->child_sa
,
574 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
575 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
577 if (status
!= SUCCESS
)
579 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
583 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
584 this->dh
, nonce_i
, nonce_r
);
586 /* add to IKE_SA, and remove from task */
587 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
588 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
589 this->established
= TRUE
;
592 { /* a rekeyed SA uses the same reqid, no need for a new job */
593 schedule_inactivity_timeout(this);
599 * build the payloads for the message
601 static void build_payloads(private_child_create_t
*this, message_t
*message
)
603 sa_payload_t
*sa_payload
;
604 nonce_payload_t
*nonce_payload
;
605 ke_payload_t
*ke_payload
;
606 ts_payload_t
*ts_payload
;
611 sa_payload
= sa_payload_create_from_proposals_v2(this->proposals
);
615 sa_payload
= sa_payload_create_from_proposal_v2(this->proposal
);
617 message
->add_payload(message
, (payload_t
*)sa_payload
);
619 /* add nonce payload if not in IKE_AUTH */
620 if (message
->get_exchange_type(message
) == CREATE_CHILD_SA
)
622 nonce_payload
= nonce_payload_create(NONCE
);
623 nonce_payload
->set_nonce(nonce_payload
, this->my_nonce
);
624 message
->add_payload(message
, (payload_t
*)nonce_payload
);
627 /* diffie hellman exchange, if PFS enabled */
630 ke_payload
= ke_payload_create_from_diffie_hellman(KEY_EXCHANGE
,
632 message
->add_payload(message
, (payload_t
*)ke_payload
);
635 /* add TSi/TSr payloads */
636 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
637 message
->add_payload(message
, (payload_t
*)ts_payload
);
638 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
639 message
->add_payload(message
, (payload_t
*)ts_payload
);
641 /* add a notify if we are not in tunnel mode */
645 message
->add_notify(message
, FALSE
, USE_TRANSPORT_MODE
, chunk_empty
);
648 message
->add_notify(message
, FALSE
, USE_BEET_MODE
, chunk_empty
);
656 * Adds an IPCOMP_SUPPORTED notify to the message, allocating a CPI
658 static void add_ipcomp_notify(private_child_create_t
*this,
659 message_t
*message
, u_int8_t ipcomp
)
661 if (this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
))
663 DBG1(DBG_IKE
, "IPComp is not supported if either peer is natted, "
668 this->my_cpi
= this->child_sa
->alloc_cpi(this->child_sa
);
671 this->ipcomp
= ipcomp
;
672 message
->add_notify(message
, FALSE
, IPCOMP_SUPPORTED
,
673 chunk_cata("cc", chunk_from_thing(this->my_cpi
),
674 chunk_from_thing(ipcomp
)));
678 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, IPComp disabled");
683 * handle a received notify payload
685 static void handle_notify(private_child_create_t
*this, notify_payload_t
*notify
)
687 switch (notify
->get_notify_type(notify
))
689 case USE_TRANSPORT_MODE
:
690 this->mode
= MODE_TRANSPORT
;
693 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_STRONGSWAN
))
694 { /* handle private use notify only if we know its meaning */
695 this->mode
= MODE_BEET
;
699 DBG1(DBG_IKE
, "received a notify strongSwan uses for BEET "
700 "mode, but peer implementation unknown, skipped");
703 case IPCOMP_SUPPORTED
:
705 ipcomp_transform_t ipcomp
;
709 data
= notify
->get_notification_data(notify
);
710 cpi
= *(u_int16_t
*)data
.ptr
;
711 ipcomp
= (ipcomp_transform_t
)(*(data
.ptr
+ 2));
715 this->other_cpi
= cpi
;
716 this->ipcomp_received
= ipcomp
;
721 DBG1(DBG_IKE
, "received IPCOMP_SUPPORTED notify with a "
722 "transform ID we don't support %N",
723 ipcomp_transform_names
, ipcomp
);
728 case ESP_TFC_PADDING_NOT_SUPPORTED
:
729 DBG1(DBG_IKE
, "received %N, not using ESPv3 TFC padding",
730 notify_type_names
, notify
->get_notify_type(notify
));
739 * Read payloads from message
741 static void process_payloads(private_child_create_t
*this, message_t
*message
)
743 enumerator_t
*enumerator
;
745 sa_payload_t
*sa_payload
;
746 ke_payload_t
*ke_payload
;
747 ts_payload_t
*ts_payload
;
749 /* defaults to TUNNEL mode */
750 this->mode
= MODE_TUNNEL
;
752 enumerator
= message
->create_payload_enumerator(message
);
753 while (enumerator
->enumerate(enumerator
, &payload
))
755 switch (payload
->get_type(payload
))
757 case SECURITY_ASSOCIATION
:
758 sa_payload
= (sa_payload_t
*)payload
;
759 this->proposals
= sa_payload
->get_proposals(sa_payload
);
762 ke_payload
= (ke_payload_t
*)payload
;
763 if (!this->initiator
)
765 this->dh_group
= ke_payload
->get_dh_group_number(ke_payload
);
766 this->dh
= this->keymat
->keymat
.create_dh(
767 &this->keymat
->keymat
, this->dh_group
);
771 this->dh
->set_other_public_value(this->dh
,
772 ke_payload
->get_key_exchange_data(ke_payload
));
775 case TRAFFIC_SELECTOR_INITIATOR
:
776 ts_payload
= (ts_payload_t
*)payload
;
777 this->tsi
= ts_payload
->get_traffic_selectors(ts_payload
);
779 case TRAFFIC_SELECTOR_RESPONDER
:
780 ts_payload
= (ts_payload_t
*)payload
;
781 this->tsr
= ts_payload
->get_traffic_selectors(ts_payload
);
784 handle_notify(this, (notify_payload_t
*)payload
);
790 enumerator
->destroy(enumerator
);
793 METHOD(task_t
, build_i
, status_t
,
794 private_child_create_t
*this, message_t
*message
)
796 enumerator_t
*enumerator
;
798 peer_cfg_t
*peer_cfg
;
800 switch (message
->get_exchange_type(message
))
803 return get_nonce(message
, &this->my_nonce
);
804 case CREATE_CHILD_SA
:
805 if (generate_nonce(this) != SUCCESS
)
807 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
812 this->dh_group
= this->config
->get_dh_group(this->config
);
816 if (message
->get_message_id(message
) != 1)
818 /* send only in the first request, not in subsequent rounds */
828 DBG0(DBG_IKE
, "establishing CHILD_SA %s{%d}",
829 this->config
->get_name(this->config
), this->reqid
);
833 DBG0(DBG_IKE
, "establishing CHILD_SA %s",
834 this->config
->get_name(this->config
));
837 /* check if we want a virtual IP, but don't have one */
838 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
839 enumerator
= peer_cfg
->create_virtual_ip_enumerator(peer_cfg
);
840 if (!this->reqid
&& enumerator
->enumerate(enumerator
, &vip
))
842 /* propose a 0.0.0.0/0 or ::/0 subnet when we use virtual ip */
843 vip
= host_create_any(vip
->get_family(vip
));
844 this->tsi
= this->config
->get_traffic_selectors(this->config
, TRUE
,
849 { /* but narrow it for host2host / if we already have a vip */
850 this->tsi
= this->config
->get_traffic_selectors(this->config
, TRUE
, NULL
,
851 get_dynamic_host(this->ike_sa
, TRUE
));
853 enumerator
->destroy(enumerator
);
854 this->tsr
= this->config
->get_traffic_selectors(this->config
, FALSE
, NULL
,
855 get_dynamic_host(this->ike_sa
, FALSE
));
857 if (this->packet_tsi
)
859 this->tsi
->insert_first(this->tsi
,
860 this->packet_tsi
->clone(this->packet_tsi
));
862 if (this->packet_tsr
)
864 this->tsr
->insert_first(this->tsr
,
865 this->packet_tsr
->clone(this->packet_tsr
));
867 this->proposals
= this->config
->get_proposals(this->config
,
868 this->dh_group
== MODP_NONE
);
869 this->mode
= this->config
->get_mode(this->config
);
870 if (this->mode
== MODE_TRANSPORT
&&
871 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
))
873 this->mode
= MODE_TUNNEL
;
874 DBG1(DBG_IKE
, "not using transport mode, connection NATed");
877 this->child_sa
= child_sa_create(this->ike_sa
->get_my_host(this->ike_sa
),
878 this->ike_sa
->get_other_host(this->ike_sa
), this->config
, this->reqid
,
879 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
));
881 if (!allocate_spi(this))
883 DBG1(DBG_IKE
, "unable to allocate SPIs from kernel");
887 if (this->dh_group
!= MODP_NONE
)
889 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
893 if (this->config
->use_ipcomp(this->config
))
895 /* IPCOMP_DEFLATE is the only transform we support at the moment */
896 add_ipcomp_notify(this, message
, IPCOMP_DEFLATE
);
899 if (message
->get_exchange_type(message
) == IKE_AUTH
)
901 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
902 NARROW_INITIATOR_PRE_NOAUTH
, this->tsi
, this->tsr
);
906 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
907 NARROW_INITIATOR_PRE_AUTH
, this->tsi
, this->tsr
);
910 build_payloads(this, message
);
912 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
913 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
914 this->proposals
->destroy_offset(this->proposals
, offsetof(proposal_t
, destroy
));
917 this->proposals
= NULL
;
922 METHOD(task_t
, process_r
, status_t
,
923 private_child_create_t
*this, message_t
*message
)
925 switch (message
->get_exchange_type(message
))
928 return get_nonce(message
, &this->other_nonce
);
929 case CREATE_CHILD_SA
:
930 get_nonce(message
, &this->other_nonce
);
933 if (message
->get_message_id(message
) != 1)
935 /* only handle first AUTH payload, not additional rounds */
942 process_payloads(this, message
);
948 * handle CHILD_SA setup failure
950 static void handle_child_sa_failure(private_child_create_t
*this,
953 if (message
->get_exchange_type(message
) == IKE_AUTH
&&
954 lib
->settings
->get_bool(lib
->settings
,
955 "%s.close_ike_on_child_failure", FALSE
, charon
->name
))
957 /* we delay the delete for 100ms, as the IKE_AUTH response must arrive
959 DBG1(DBG_IKE
, "closing IKE_SA due CHILD_SA setup failure");
960 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
961 delete_ike_sa_job_create(this->ike_sa
->get_id(this->ike_sa
), TRUE
),
966 DBG1(DBG_IKE
, "failed to establish CHILD_SA, keeping IKE_SA");
970 METHOD(task_t
, build_r
, status_t
,
971 private_child_create_t
*this, message_t
*message
)
973 peer_cfg_t
*peer_cfg
;
975 enumerator_t
*enumerator
;
976 bool no_dh
= TRUE
, ike_auth
= FALSE
;
978 switch (message
->get_exchange_type(message
))
981 return get_nonce(message
, &this->my_nonce
);
982 case CREATE_CHILD_SA
:
983 if (generate_nonce(this) != SUCCESS
)
985 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
,
992 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
993 { /* wait until all authentication round completed */
1001 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_REKEYING
)
1003 DBG1(DBG_IKE
, "unable to create CHILD_SA while rekeying IKE_SA");
1004 message
->add_notify(message
, TRUE
, NO_ADDITIONAL_SAS
, chunk_empty
);
1008 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1009 if (!this->config
&& peer_cfg
&& this->tsi
&& this->tsr
)
1011 this->config
= peer_cfg
->select_child_cfg(peer_cfg
,
1012 this->tsr
, this->tsi
,
1013 get_dynamic_host(this->ike_sa
, TRUE
),
1014 get_dynamic_host(this->ike_sa
, FALSE
));
1017 if (this->config
== NULL
)
1019 DBG1(DBG_IKE
, "traffic selectors %#R=== %#R inacceptable",
1020 this->tsr
, this->tsi
);
1021 message
->add_notify(message
, FALSE
, TS_UNACCEPTABLE
, chunk_empty
);
1022 handle_child_sa_failure(this, message
);
1026 /* check if ike_config_t included non-critical error notifies */
1027 enumerator
= message
->create_payload_enumerator(message
);
1028 while (enumerator
->enumerate(enumerator
, &payload
))
1030 if (payload
->get_type(payload
) == NOTIFY
)
1032 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
1034 switch (notify
->get_notify_type(notify
))
1036 case INTERNAL_ADDRESS_FAILURE
:
1037 case FAILED_CP_REQUIRED
:
1039 DBG1(DBG_IKE
,"configuration payload negotiation "
1040 "failed, no CHILD_SA built");
1041 enumerator
->destroy(enumerator
);
1042 handle_child_sa_failure(this, message
);
1050 enumerator
->destroy(enumerator
);
1052 this->child_sa
= child_sa_create(this->ike_sa
->get_my_host(this->ike_sa
),
1053 this->ike_sa
->get_other_host(this->ike_sa
), this->config
, this->reqid
,
1054 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
));
1056 if (this->ipcomp_received
!= IPCOMP_NONE
)
1058 if (this->config
->use_ipcomp(this->config
))
1060 add_ipcomp_notify(this, message
, this->ipcomp_received
);
1064 DBG1(DBG_IKE
, "received %N notify but IPComp is disabled, ignoring",
1065 notify_type_names
, IPCOMP_SUPPORTED
);
1069 switch (select_and_install(this, no_dh
, ike_auth
))
1074 message
->add_notify(message
, FALSE
, TS_UNACCEPTABLE
, chunk_empty
);
1075 handle_child_sa_failure(this, message
);
1079 u_int16_t group
= htons(this->dh_group
);
1080 message
->add_notify(message
, FALSE
, INVALID_KE_PAYLOAD
,
1081 chunk_from_thing(group
));
1082 handle_child_sa_failure(this, message
);
1087 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
1088 handle_child_sa_failure(this, message
);
1092 build_payloads(this, message
);
1094 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
1095 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
1096 this->child_sa
->get_name(this->child_sa
),
1097 this->child_sa
->get_reqid(this->child_sa
),
1098 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
1099 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)),
1100 this->child_sa
->get_traffic_selectors(this->child_sa
, TRUE
),
1101 this->child_sa
->get_traffic_selectors(this->child_sa
, FALSE
));
1104 { /* invoke the child_up() hook if we are not rekeying */
1105 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
1110 METHOD(task_t
, process_i
, status_t
,
1111 private_child_create_t
*this, message_t
*message
)
1113 enumerator_t
*enumerator
;
1115 bool no_dh
= TRUE
, ike_auth
= FALSE
;
1117 switch (message
->get_exchange_type(message
))
1120 return get_nonce(message
, &this->other_nonce
);
1121 case CREATE_CHILD_SA
:
1122 get_nonce(message
, &this->other_nonce
);
1126 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1127 { /* wait until all authentication round completed */
1135 /* check for erronous notifies */
1136 enumerator
= message
->create_payload_enumerator(message
);
1137 while (enumerator
->enumerate(enumerator
, &payload
))
1139 if (payload
->get_type(payload
) == NOTIFY
)
1141 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
1142 notify_type_t type
= notify
->get_notify_type(notify
);
1146 /* handle notify errors related to CHILD_SA only */
1147 case NO_PROPOSAL_CHOSEN
:
1148 case SINGLE_PAIR_REQUIRED
:
1149 case NO_ADDITIONAL_SAS
:
1150 case INTERNAL_ADDRESS_FAILURE
:
1151 case FAILED_CP_REQUIRED
:
1152 case TS_UNACCEPTABLE
:
1153 case INVALID_SELECTORS
:
1155 DBG1(DBG_IKE
, "received %N notify, no CHILD_SA built",
1156 notify_type_names
, type
);
1157 enumerator
->destroy(enumerator
);
1158 handle_child_sa_failure(this, message
);
1159 /* an error in CHILD_SA creation is not critical */
1162 case INVALID_KE_PAYLOAD
:
1165 u_int16_t group
= MODP_NONE
;
1167 data
= notify
->get_notification_data(notify
);
1168 if (data
.len
== sizeof(group
))
1170 memcpy(&group
, data
.ptr
, data
.len
);
1171 group
= ntohs(group
);
1173 DBG1(DBG_IKE
, "peer didn't accept DH group %N, "
1174 "it requested %N", diffie_hellman_group_names
,
1175 this->dh_group
, diffie_hellman_group_names
, group
);
1177 this->dh_group
= group
;
1178 this->public.task
.migrate(&this->public.task
, this->ike_sa
);
1179 enumerator
->destroy(enumerator
);
1184 if (message
->get_exchange_type(message
) == CREATE_CHILD_SA
)
1185 { /* handle notifies if not handled in IKE_AUTH */
1188 DBG1(DBG_IKE
, "received %N notify error",
1189 notify_type_names
, type
);
1190 enumerator
->destroy(enumerator
);
1193 DBG2(DBG_IKE
, "received %N notify",
1194 notify_type_names
, type
);
1201 enumerator
->destroy(enumerator
);
1203 process_payloads(this, message
);
1205 if (this->ipcomp
== IPCOMP_NONE
&& this->ipcomp_received
!= IPCOMP_NONE
)
1207 DBG1(DBG_IKE
, "received an IPCOMP_SUPPORTED notify without requesting"
1208 " one, no CHILD_SA built");
1209 handle_child_sa_failure(this, message
);
1212 else if (this->ipcomp
!= IPCOMP_NONE
&& this->ipcomp_received
== IPCOMP_NONE
)
1214 DBG1(DBG_IKE
, "peer didn't accept our proposed IPComp transforms, "
1215 "IPComp is disabled");
1216 this->ipcomp
= IPCOMP_NONE
;
1218 else if (this->ipcomp
!= IPCOMP_NONE
&& this->ipcomp
!= this->ipcomp_received
)
1220 DBG1(DBG_IKE
, "received an IPCOMP_SUPPORTED notify we didn't propose, "
1221 "no CHILD_SA built");
1222 handle_child_sa_failure(this, message
);
1226 if (select_and_install(this, no_dh
, ike_auth
) == SUCCESS
)
1228 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
1229 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
1230 this->child_sa
->get_name(this->child_sa
),
1231 this->child_sa
->get_reqid(this->child_sa
),
1232 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
1233 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)),
1234 this->child_sa
->get_traffic_selectors(this->child_sa
, TRUE
),
1235 this->child_sa
->get_traffic_selectors(this->child_sa
, FALSE
));
1238 { /* invoke the child_up() hook if we are not rekeying */
1239 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
1244 handle_child_sa_failure(this, message
);
1249 METHOD(child_create_t
, use_reqid
, void,
1250 private_child_create_t
*this, u_int32_t reqid
)
1252 this->reqid
= reqid
;
1255 METHOD(child_create_t
, get_child
, child_sa_t
*,
1256 private_child_create_t
*this)
1258 return this->child_sa
;
1261 METHOD(child_create_t
, set_config
, void,
1262 private_child_create_t
*this, child_cfg_t
*cfg
)
1264 DESTROY_IF(this->config
);
1268 METHOD(child_create_t
, get_lower_nonce
, chunk_t
,
1269 private_child_create_t
*this)
1271 if (memcmp(this->my_nonce
.ptr
, this->other_nonce
.ptr
,
1272 min(this->my_nonce
.len
, this->other_nonce
.len
)) < 0)
1274 return this->my_nonce
;
1278 return this->other_nonce
;
1282 METHOD(task_t
, get_type
, task_type_t
,
1283 private_child_create_t
*this)
1285 return TASK_CHILD_CREATE
;
1288 METHOD(task_t
, migrate
, void,
1289 private_child_create_t
*this, ike_sa_t
*ike_sa
)
1291 chunk_free(&this->my_nonce
);
1292 chunk_free(&this->other_nonce
);
1295 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1299 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1301 DESTROY_IF(this->child_sa
);
1302 DESTROY_IF(this->proposal
);
1303 DESTROY_IF(this->dh
);
1304 if (this->proposals
)
1306 this->proposals
->destroy_offset(this->proposals
, offsetof(proposal_t
, destroy
));
1309 this->ike_sa
= ike_sa
;
1310 this->keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
);
1311 this->proposal
= NULL
;
1312 this->proposals
= NULL
;
1316 this->child_sa
= NULL
;
1317 this->mode
= MODE_TUNNEL
;
1318 this->ipcomp
= IPCOMP_NONE
;
1319 this->ipcomp_received
= IPCOMP_NONE
;
1320 this->other_cpi
= 0;
1322 this->established
= FALSE
;
1325 METHOD(task_t
, destroy
, void,
1326 private_child_create_t
*this)
1328 chunk_free(&this->my_nonce
);
1329 chunk_free(&this->other_nonce
);
1332 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1336 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1338 if (!this->established
)
1340 DESTROY_IF(this->child_sa
);
1342 DESTROY_IF(this->packet_tsi
);
1343 DESTROY_IF(this->packet_tsr
);
1344 DESTROY_IF(this->proposal
);
1345 DESTROY_IF(this->dh
);
1346 if (this->proposals
)
1348 this->proposals
->destroy_offset(this->proposals
, offsetof(proposal_t
, destroy
));
1351 DESTROY_IF(this->config
);
1356 * Described in header.
1358 child_create_t
*child_create_create(ike_sa_t
*ike_sa
,
1359 child_cfg_t
*config
, bool rekey
,
1360 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1362 private_child_create_t
*this;
1366 .get_child
= _get_child
,
1367 .set_config
= _set_config
,
1368 .get_lower_nonce
= _get_lower_nonce
,
1369 .use_reqid
= _use_reqid
,
1371 .get_type
= _get_type
,
1372 .migrate
= _migrate
,
1373 .destroy
= _destroy
,
1378 .packet_tsi
= tsi ? tsi
->clone(tsi
) : NULL
,
1379 .packet_tsr
= tsr ? tsr
->clone(tsr
) : NULL
,
1380 .dh_group
= MODP_NONE
,
1381 .keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
),
1382 .mode
= MODE_TUNNEL
,
1384 .ipcomp
= IPCOMP_NONE
,
1385 .ipcomp_received
= IPCOMP_NONE
,
1392 this->public.task
.build
= _build_i
;
1393 this->public.task
.process
= _process_i
;
1394 this->initiator
= TRUE
;
1398 this->public.task
.build
= _build_r
;
1399 this->public.task
.process
= _process_r
;
1400 this->initiator
= FALSE
;
1403 return &this->public;