2 * Copyright (C) 2008-2018 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * HSR 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 <encoding/payloads/delete_payload.h>
30 #include <processing/jobs/delete_ike_sa_job.h>
31 #include <processing/jobs/inactivity_job.h>
32 #include <processing/jobs/initiate_tasks_job.h>
34 typedef struct private_child_create_t private_child_create_t
;
37 * Private members of a child_create_t task.
39 struct private_child_create_t
{
42 * Public methods and task_t interface.
44 child_create_t
public;
52 * Are we the initiator?
62 * nonce chosen by peer
72 * config to create the CHILD_SA from
77 * list of proposal candidates
79 linked_list_t
*proposals
;
82 * selected proposal to use for CHILD_SA
87 * traffic selectors for initiators side
92 * traffic selectors for responders side
97 * source of triggering packet
99 traffic_selector_t
*packet_tsi
;
102 * destination of triggering packet
104 traffic_selector_t
*packet_tsr
;
107 * optional diffie hellman exchange
109 diffie_hellman_t
*dh
;
112 * Applying DH public value failed?
117 * group used for DH exchange
119 diffie_hellman_group_t dh_group
;
127 * mode the new CHILD_SA uses (transport/tunnel/beet)
132 * peer accepts TFC padding for this SA
137 * IPComp transform to use
139 ipcomp_transform_t ipcomp
;
142 * IPComp transform proposed or accepted by the other peer
144 ipcomp_transform_t ipcomp_received
;
157 * SPI received in proposal
162 * Own allocated Compression Parameter Index (CPI)
167 * Other Compression Parameter Index (CPI), received via IPCOMP_SUPPORTED
172 * reqid to use if we are rekeying
177 * Explicit inbound mark value
182 * Explicit outbound mark value
187 * CHILD_SA which gets established
189 child_sa_t
*child_sa
;
192 * successfully established the CHILD?
197 * whether the CHILD_SA rekeys an existing one
202 * whether we are retrying with another DH group
208 * Schedule a retry if creating the CHILD_SA temporary failed
210 static void schedule_delayed_retry(private_child_create_t
*this)
212 child_create_t
*task
;
215 retry
= RETRY_INTERVAL
- (random() % RETRY_JITTER
);
217 task
= child_create_create(this->ike_sa
,
218 this->config
->get_ref(this->config
), FALSE
,
219 this->packet_tsi
, this->packet_tsr
);
220 task
->use_reqid(task
, this->reqid
);
221 DBG1(DBG_IKE
, "creating CHILD_SA failed, trying again in %d seconds",
223 this->ike_sa
->queue_task_delayed(this->ike_sa
, (task_t
*)task
, retry
);
227 * get the nonce from a message
229 static status_t
get_nonce(message_t
*message
, chunk_t
*nonce
)
231 nonce_payload_t
*payload
;
233 payload
= (nonce_payload_t
*)message
->get_payload(message
, PLV2_NONCE
);
238 *nonce
= payload
->get_nonce(payload
);
243 * generate a new nonce to include in a CREATE_CHILD_SA message
245 static bool generate_nonce(private_child_create_t
*this)
247 this->nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
250 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
253 if (!this->nonceg
->allocate_nonce(this->nonceg
, NONCE_SIZE
,
256 DBG1(DBG_IKE
, "nonce allocation failed");
263 * Check a list of traffic selectors if any selector belongs to host
265 static bool ts_list_is_host(linked_list_t
*list
, host_t
*host
)
267 traffic_selector_t
*ts
;
269 enumerator_t
*enumerator
= list
->create_enumerator(list
);
271 while (is_host
&& enumerator
->enumerate(enumerator
, (void**)&ts
))
273 is_host
= is_host
&& ts
->is_host(ts
, host
);
275 enumerator
->destroy(enumerator
);
282 static bool allocate_spi(private_child_create_t
*this)
284 proposal_t
*proposal
;
288 this->proto
= PROTO_ESP
;
289 /* we just get a SPI for the first protocol. TODO: If we ever support
290 * proposal lists with mixed protocols, we'd need multiple SPIs */
291 if (this->proposals
->get_first(this->proposals
,
292 (void**)&proposal
) == SUCCESS
)
294 this->proto
= proposal
->get_protocol(proposal
);
299 this->proto
= this->proposal
->get_protocol(this->proposal
);
301 this->my_spi
= this->child_sa
->alloc_spi(this->child_sa
, this->proto
);
302 return this->my_spi
!= 0;
306 * Update the proposals with the allocated SPIs as initiator and check the DH
307 * group and promote it if necessary
309 static bool update_and_check_proposals(private_child_create_t
*this)
311 enumerator_t
*enumerator
;
312 proposal_t
*proposal
;
313 linked_list_t
*other_dh_groups
;
316 other_dh_groups
= linked_list_create();
317 enumerator
= this->proposals
->create_enumerator(this->proposals
);
318 while (enumerator
->enumerate(enumerator
, &proposal
))
320 proposal
->set_spi(proposal
, this->my_spi
);
322 /* move the selected DH group to the front, if any */
323 if (this->dh_group
!= MODP_NONE
)
324 { /* proposals that don't contain the selected group are
325 * moved to the back */
326 if (!proposal
->promote_dh_group(proposal
, this->dh_group
))
328 this->proposals
->remove_at(this->proposals
, enumerator
);
329 other_dh_groups
->insert_last(other_dh_groups
, proposal
);
337 enumerator
->destroy(enumerator
);
338 enumerator
= other_dh_groups
->create_enumerator(other_dh_groups
);
339 while (enumerator
->enumerate(enumerator
, (void**)&proposal
))
340 { /* no need to remove from the list as we destroy it anyway*/
341 this->proposals
->insert_last(this->proposals
, proposal
);
343 enumerator
->destroy(enumerator
);
344 other_dh_groups
->destroy(other_dh_groups
);
346 return this->dh_group
== MODP_NONE
|| found
;
350 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
352 static void schedule_inactivity_timeout(private_child_create_t
*this)
354 uint32_t timeout
, id
;
357 timeout
= this->config
->get_inactivity(this->config
);
360 close_ike
= lib
->settings
->get_bool(lib
->settings
,
361 "%s.inactivity_close_ike", FALSE
, lib
->ns
);
362 id
= this->child_sa
->get_unique_id(this->child_sa
);
363 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
364 inactivity_job_create(id
, timeout
, close_ike
), timeout
);
369 * Check if we have a an address pool configured
371 static bool have_pool(ike_sa_t
*ike_sa
)
373 enumerator_t
*enumerator
;
374 peer_cfg_t
*peer_cfg
;
378 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
381 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
382 if (enumerator
->enumerate(enumerator
, &pool
))
386 enumerator
->destroy(enumerator
);
392 * Get hosts to use for dynamic traffic selectors
394 static linked_list_t
*get_dynamic_hosts(ike_sa_t
*ike_sa
, bool local
)
396 enumerator_t
*enumerator
;
400 list
= linked_list_create();
401 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
402 while (enumerator
->enumerate(enumerator
, &host
))
404 list
->insert_last(list
, host
);
406 enumerator
->destroy(enumerator
);
408 if (list
->get_count(list
) == 0)
409 { /* no virtual IPs assigned */
412 host
= ike_sa
->get_my_host(ike_sa
);
413 list
->insert_last(list
, host
);
415 else if (!have_pool(ike_sa
))
416 { /* use host only if we don't have a pool configured */
417 host
= ike_sa
->get_other_host(ike_sa
);
418 list
->insert_last(list
, host
);
425 * Substitute any host address with NATed address in traffic selector
427 static linked_list_t
* get_transport_nat_ts(private_child_create_t
*this,
428 bool local
, linked_list_t
*in
)
430 enumerator_t
*enumerator
;
432 traffic_selector_t
*ts
;
433 host_t
*ike
, *first
= NULL
;
438 ike
= this->ike_sa
->get_my_host(this->ike_sa
);
442 ike
= this->ike_sa
->get_other_host(this->ike_sa
);
445 out
= linked_list_create();
447 enumerator
= in
->create_enumerator(in
);
448 while (enumerator
->enumerate(enumerator
, &ts
))
450 /* require that all selectors match the first "host" selector */
451 if (ts
->is_host(ts
, first
))
455 ts
->to_subnet(ts
, &first
, &mask
);
458 ts
->set_address(ts
, ike
);
459 out
->insert_last(out
, ts
);
462 enumerator
->destroy(enumerator
);
469 * Narrow received traffic selectors with configuration
471 static linked_list_t
* narrow_ts(private_child_create_t
*this, bool local
,
474 linked_list_t
*hosts
, *nat
, *ts
;
475 ike_condition_t cond
;
477 cond
= local ? COND_NAT_HERE
: COND_NAT_THERE
;
478 hosts
= get_dynamic_hosts(this->ike_sa
, local
);
480 if (this->mode
== MODE_TRANSPORT
&&
481 this->ike_sa
->has_condition(this->ike_sa
, cond
))
483 nat
= get_transport_nat_ts(this, local
, in
);
484 ts
= this->config
->get_traffic_selectors(this->config
, local
, nat
,
486 nat
->destroy_offset(nat
, offsetof(traffic_selector_t
, destroy
));
490 ts
= this->config
->get_traffic_selectors(this->config
, local
, in
,
494 hosts
->destroy(hosts
);
500 * Install a CHILD_SA for usage, return value:
501 * - FAILED: no acceptable proposal
502 * - INVALID_ARG: diffie hellman group unacceptable
503 * - NOT_FOUND: TS unacceptable
505 static status_t
select_and_install(private_child_create_t
*this,
506 bool no_dh
, bool ike_auth
)
508 status_t status
, status_i
, status_o
;
509 child_sa_outbound_state_t out_state
;
510 chunk_t nonce_i
, nonce_r
;
511 chunk_t encr_i
= chunk_empty
, encr_r
= chunk_empty
;
512 chunk_t integ_i
= chunk_empty
, integ_r
= chunk_empty
;
513 linked_list_t
*my_ts
, *other_ts
;
515 bool private, prefer_configured
;
517 if (this->proposals
== NULL
)
519 DBG1(DBG_IKE
, "SA payload missing in message");
522 if (this->tsi
== NULL
|| this->tsr
== NULL
)
524 DBG1(DBG_IKE
, "TS payloads missing in message");
528 me
= this->ike_sa
->get_my_host(this->ike_sa
);
529 other
= this->ike_sa
->get_other_host(this->ike_sa
);
531 private = this->ike_sa
->supports_extension(this->ike_sa
, EXT_STRONGSWAN
);
532 prefer_configured
= lib
->settings
->get_bool(lib
->settings
,
533 "%s.prefer_configured_proposals", TRUE
, lib
->ns
);
534 this->proposal
= this->config
->select_proposal(this->config
,
535 this->proposals
, no_dh
, private, prefer_configured
);
536 if (this->proposal
== NULL
)
538 DBG1(DBG_IKE
, "no acceptable proposal found");
539 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_CHILD
,
543 this->other_spi
= this->proposal
->get_spi(this->proposal
);
545 if (!this->initiator
)
547 if (!allocate_spi(this))
549 /* responder has no SPI allocated yet */
550 DBG1(DBG_IKE
, "allocating SPI failed");
553 this->proposal
->set_spi(this->proposal
, this->my_spi
);
555 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
557 if (!this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
561 if (this->proposal
->get_algorithm(this->proposal
, DIFFIE_HELLMAN_GROUP
,
564 DBG1(DBG_IKE
, "DH group %N unacceptable, requesting %N",
565 diffie_hellman_group_names
, this->dh_group
,
566 diffie_hellman_group_names
, group
);
567 this->dh_group
= group
;
570 /* the selected proposal does not use a DH group */
571 DBG1(DBG_IKE
, "ignoring KE exchange, agreed on a non-PFS proposal");
572 DESTROY_IF(this->dh
);
574 this->dh_group
= MODP_NONE
;
579 nonce_i
= this->my_nonce
;
580 nonce_r
= this->other_nonce
;
581 my_ts
= narrow_ts(this, TRUE
, this->tsi
);
582 other_ts
= narrow_ts(this, FALSE
, this->tsr
);
586 nonce_r
= this->my_nonce
;
587 nonce_i
= this->other_nonce
;
588 my_ts
= narrow_ts(this, TRUE
, this->tsr
);
589 other_ts
= narrow_ts(this, FALSE
, this->tsi
);
596 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
597 NARROW_INITIATOR_POST_NOAUTH
, my_ts
, other_ts
);
601 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
602 NARROW_INITIATOR_POST_AUTH
, my_ts
, other_ts
);
607 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
608 NARROW_RESPONDER
, my_ts
, other_ts
);
611 if (my_ts
->get_count(my_ts
) == 0 || other_ts
->get_count(other_ts
) == 0)
613 charon
->bus
->alert(charon
->bus
, ALERT_TS_MISMATCH
, this->tsi
, this->tsr
);
614 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
615 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
616 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
620 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
621 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
625 this->tsr
= other_ts
;
630 this->tsi
= other_ts
;
633 if (!this->initiator
)
635 /* check if requested mode is acceptable, downgrade if required */
639 if (!this->config
->has_option(this->config
, OPT_PROXY_MODE
) &&
640 (!ts_list_is_host(this->tsi
, other
) ||
641 !ts_list_is_host(this->tsr
, me
))
644 this->mode
= MODE_TUNNEL
;
645 DBG1(DBG_IKE
, "not using transport mode, not host-to-host");
647 if (this->config
->get_mode(this->config
) != MODE_TRANSPORT
)
649 this->mode
= MODE_TUNNEL
;
653 if (!ts_list_is_host(this->tsi
, NULL
) ||
654 !ts_list_is_host(this->tsr
, NULL
))
656 this->mode
= MODE_TUNNEL
;
657 DBG1(DBG_IKE
, "not using BEET mode, not host-to-host");
659 if (this->config
->get_mode(this->config
) != MODE_BEET
)
661 this->mode
= MODE_TUNNEL
;
667 /* use a copy of the traffic selectors, as the POST hook should not
669 my_ts
= this->tsr
->clone_offset(this->tsr
,
670 offsetof(traffic_selector_t
, clone
));
671 other_ts
= this->tsi
->clone_offset(this->tsi
,
672 offsetof(traffic_selector_t
, clone
));
673 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
674 NARROW_RESPONDER_POST
, my_ts
, other_ts
);
676 if (my_ts
->get_count(my_ts
) == 0 || other_ts
->get_count(other_ts
) == 0)
678 my_ts
->destroy_offset(my_ts
,
679 offsetof(traffic_selector_t
, destroy
));
680 other_ts
->destroy_offset(other_ts
,
681 offsetof(traffic_selector_t
, destroy
));
686 this->child_sa
->set_policies(this->child_sa
, my_ts
, other_ts
);
687 if (!this->initiator
)
689 my_ts
->destroy_offset(my_ts
,
690 offsetof(traffic_selector_t
, destroy
));
691 other_ts
->destroy_offset(other_ts
,
692 offsetof(traffic_selector_t
, destroy
));
695 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
696 this->child_sa
->set_ipcomp(this->child_sa
, this->ipcomp
);
697 this->child_sa
->set_mode(this->child_sa
, this->mode
);
698 this->child_sa
->set_protocol(this->child_sa
,
699 this->proposal
->get_protocol(this->proposal
));
701 if (this->my_cpi
== 0 || this->other_cpi
== 0 || this->ipcomp
== IPCOMP_NONE
)
703 this->my_cpi
= this->other_cpi
= 0;
704 this->ipcomp
= IPCOMP_NONE
;
706 status_i
= status_o
= FAILED
;
707 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
,
708 this->dh
, nonce_i
, nonce_r
, &encr_i
, &integ_i
, &encr_r
, &integ_r
))
712 status_i
= this->child_sa
->install(this->child_sa
, encr_r
, integ_r
,
713 this->my_spi
, this->my_cpi
, this->initiator
,
718 status_i
= this->child_sa
->install(this->child_sa
, encr_i
, integ_i
,
719 this->my_spi
, this->my_cpi
, this->initiator
,
723 { /* during rekeyings we install the outbound SA and/or policies
724 * separately: as responder when we receive the delete for the old
725 * SA, as initiator pretty much immediately in the ike-rekey task,
726 * unless there was a rekey collision that we lost */
729 status_o
= this->child_sa
->register_outbound(this->child_sa
,
730 encr_i
, integ_i
, this->other_spi
, this->other_cpi
,
735 status_o
= this->child_sa
->register_outbound(this->child_sa
,
736 encr_r
, integ_r
, this->other_spi
, this->other_cpi
,
740 else if (this->initiator
)
742 status_o
= this->child_sa
->install(this->child_sa
, encr_i
, integ_i
,
743 this->other_spi
, this->other_cpi
, this->initiator
,
748 status_o
= this->child_sa
->install(this->child_sa
, encr_r
, integ_r
,
749 this->other_spi
, this->other_cpi
, this->initiator
,
754 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
756 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
757 (status_i
!= SUCCESS
) ?
"inbound " : "",
758 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ?
"and ": "",
759 (status_o
!= SUCCESS
) ?
"outbound " : "");
760 charon
->bus
->alert(charon
->bus
, ALERT_INSTALL_CHILD_SA_FAILED
,
766 status
= this->child_sa
->install_policies(this->child_sa
);
768 if (status
!= SUCCESS
)
770 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
771 charon
->bus
->alert(charon
->bus
, ALERT_INSTALL_CHILD_POLICY_FAILED
,
777 charon
->bus
->child_derived_keys(charon
->bus
, this->child_sa
,
778 this->initiator
, encr_i
, encr_r
,
782 chunk_clear(&integ_i
);
783 chunk_clear(&integ_r
);
784 chunk_clear(&encr_i
);
785 chunk_clear(&encr_r
);
787 if (status
!= SUCCESS
)
792 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
793 this->dh
, nonce_i
, nonce_r
);
795 my_ts
= linked_list_create_from_enumerator(
796 this->child_sa
->create_ts_enumerator(this->child_sa
, TRUE
));
797 other_ts
= linked_list_create_from_enumerator(
798 this->child_sa
->create_ts_enumerator(this->child_sa
, FALSE
));
799 out_state
= this->child_sa
->get_outbound_state(this->child_sa
);
801 DBG0(DBG_IKE
, "%sCHILD_SA %s{%d} established "
802 "with SPIs %.8x_i %.8x_o and TS %#R === %#R",
803 (out_state
== CHILD_OUTBOUND_INSTALLED
) ?
"" : "inbound ",
804 this->child_sa
->get_name(this->child_sa
),
805 this->child_sa
->get_unique_id(this->child_sa
),
806 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
807 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)),
810 my_ts
->destroy(my_ts
);
811 other_ts
->destroy(other_ts
);
813 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
814 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
815 this->established
= TRUE
;
817 schedule_inactivity_timeout(this);
822 * build the payloads for the message
824 static bool build_payloads(private_child_create_t
*this, message_t
*message
)
826 sa_payload_t
*sa_payload
;
827 nonce_payload_t
*nonce_payload
;
828 ke_payload_t
*ke_payload
;
829 ts_payload_t
*ts_payload
;
830 kernel_feature_t features
;
835 sa_payload
= sa_payload_create_from_proposals_v2(this->proposals
);
839 sa_payload
= sa_payload_create_from_proposal_v2(this->proposal
);
841 message
->add_payload(message
, (payload_t
*)sa_payload
);
843 /* add nonce payload if not in IKE_AUTH */
844 if (message
->get_exchange_type(message
) == CREATE_CHILD_SA
)
846 nonce_payload
= nonce_payload_create(PLV2_NONCE
);
847 nonce_payload
->set_nonce(nonce_payload
, this->my_nonce
);
848 message
->add_payload(message
, (payload_t
*)nonce_payload
);
851 /* diffie hellman exchange, if PFS enabled */
854 ke_payload
= ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE
,
858 DBG1(DBG_IKE
, "creating KE payload failed");
861 message
->add_payload(message
, (payload_t
*)ke_payload
);
864 /* add TSi/TSr payloads */
865 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
866 message
->add_payload(message
, (payload_t
*)ts_payload
);
867 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
868 message
->add_payload(message
, (payload_t
*)ts_payload
);
870 /* add a notify if we are not in tunnel mode */
874 message
->add_notify(message
, FALSE
, USE_TRANSPORT_MODE
, chunk_empty
);
877 message
->add_notify(message
, FALSE
, USE_BEET_MODE
, chunk_empty
);
883 features
= charon
->kernel
->get_features(charon
->kernel
);
884 if (!(features
& KERNEL_ESP_V3_TFC
))
886 message
->add_notify(message
, FALSE
, ESP_TFC_PADDING_NOT_SUPPORTED
,
893 * Adds an IPCOMP_SUPPORTED notify to the message, allocating a CPI
895 static void add_ipcomp_notify(private_child_create_t
*this,
896 message_t
*message
, uint8_t ipcomp
)
898 this->my_cpi
= this->child_sa
->alloc_cpi(this->child_sa
);
901 this->ipcomp
= ipcomp
;
902 message
->add_notify(message
, FALSE
, IPCOMP_SUPPORTED
,
903 chunk_cata("cc", chunk_from_thing(this->my_cpi
),
904 chunk_from_thing(ipcomp
)));
908 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, IPComp disabled");
913 * handle a received notify payload
915 static void handle_notify(private_child_create_t
*this, notify_payload_t
*notify
)
917 switch (notify
->get_notify_type(notify
))
919 case USE_TRANSPORT_MODE
:
920 this->mode
= MODE_TRANSPORT
;
923 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_STRONGSWAN
))
924 { /* handle private use notify only if we know its meaning */
925 this->mode
= MODE_BEET
;
929 DBG1(DBG_IKE
, "received a notify strongSwan uses for BEET "
930 "mode, but peer implementation unknown, skipped");
933 case IPCOMP_SUPPORTED
:
935 ipcomp_transform_t ipcomp
;
939 data
= notify
->get_notification_data(notify
);
940 cpi
= *(uint16_t*)data
.ptr
;
941 ipcomp
= (ipcomp_transform_t
)(*(data
.ptr
+ 2));
945 this->other_cpi
= cpi
;
946 this->ipcomp_received
= ipcomp
;
951 DBG1(DBG_IKE
, "received IPCOMP_SUPPORTED notify with a "
952 "transform ID we don't support %N",
953 ipcomp_transform_names
, ipcomp
);
958 case ESP_TFC_PADDING_NOT_SUPPORTED
:
959 DBG1(DBG_IKE
, "received %N, not using ESPv3 TFC padding",
960 notify_type_names
, notify
->get_notify_type(notify
));
969 * Read payloads from message
971 static void process_payloads(private_child_create_t
*this, message_t
*message
)
973 enumerator_t
*enumerator
;
975 sa_payload_t
*sa_payload
;
976 ke_payload_t
*ke_payload
;
977 ts_payload_t
*ts_payload
;
979 /* defaults to TUNNEL mode */
980 this->mode
= MODE_TUNNEL
;
982 enumerator
= message
->create_payload_enumerator(message
);
983 while (enumerator
->enumerate(enumerator
, &payload
))
985 switch (payload
->get_type(payload
))
987 case PLV2_SECURITY_ASSOCIATION
:
988 sa_payload
= (sa_payload_t
*)payload
;
989 this->proposals
= sa_payload
->get_proposals(sa_payload
);
991 case PLV2_KEY_EXCHANGE
:
992 ke_payload
= (ke_payload_t
*)payload
;
993 if (!this->initiator
)
995 this->dh_group
= ke_payload
->get_dh_group_number(ke_payload
);
996 this->dh
= this->keymat
->keymat
.create_dh(
997 &this->keymat
->keymat
, this->dh_group
);
1001 this->dh_failed
= this->dh
->get_dh_group(this->dh
) !=
1002 ke_payload
->get_dh_group_number(ke_payload
);
1004 if (this->dh
&& !this->dh_failed
)
1006 this->dh_failed
= !this->dh
->set_other_public_value(this->dh
,
1007 ke_payload
->get_key_exchange_data(ke_payload
));
1010 case PLV2_TS_INITIATOR
:
1011 ts_payload
= (ts_payload_t
*)payload
;
1012 this->tsi
= ts_payload
->get_traffic_selectors(ts_payload
);
1014 case PLV2_TS_RESPONDER
:
1015 ts_payload
= (ts_payload_t
*)payload
;
1016 this->tsr
= ts_payload
->get_traffic_selectors(ts_payload
);
1019 handle_notify(this, (notify_payload_t
*)payload
);
1025 enumerator
->destroy(enumerator
);
1028 METHOD(task_t
, build_i
, status_t
,
1029 private_child_create_t
*this, message_t
*message
)
1031 enumerator_t
*enumerator
;
1033 peer_cfg_t
*peer_cfg
;
1034 linked_list_t
*list
;
1036 switch (message
->get_exchange_type(message
))
1039 return get_nonce(message
, &this->my_nonce
);
1040 case CREATE_CHILD_SA
:
1041 if (!generate_nonce(this))
1043 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
,
1047 if (!this->retry
&& this->dh_group
== MODP_NONE
)
1048 { /* during a rekeying the group might already be set */
1049 this->dh_group
= this->config
->get_dh_group(this->config
);
1053 if (message
->get_message_id(message
) != 1)
1055 /* send only in the first request, not in subsequent rounds */
1063 /* check if we want a virtual IP, but don't have one */
1064 list
= linked_list_create();
1065 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1068 enumerator
= peer_cfg
->create_virtual_ip_enumerator(peer_cfg
);
1069 while (enumerator
->enumerate(enumerator
, &vip
))
1071 /* propose a 0.0.0.0/0 or ::/0 subnet when we use virtual ip */
1072 vip
= host_create_any(vip
->get_family(vip
));
1073 list
->insert_last(list
, vip
);
1075 enumerator
->destroy(enumerator
);
1077 if (list
->get_count(list
))
1079 this->tsi
= this->config
->get_traffic_selectors(this->config
,
1080 TRUE
, NULL
, list
, TRUE
);
1081 list
->destroy_offset(list
, offsetof(host_t
, destroy
));
1084 { /* no virtual IPs configured */
1085 list
->destroy(list
);
1086 list
= get_dynamic_hosts(this->ike_sa
, TRUE
);
1087 this->tsi
= this->config
->get_traffic_selectors(this->config
,
1088 TRUE
, NULL
, list
, TRUE
);
1089 list
->destroy(list
);
1091 list
= get_dynamic_hosts(this->ike_sa
, FALSE
);
1092 this->tsr
= this->config
->get_traffic_selectors(this->config
,
1093 FALSE
, NULL
, list
, TRUE
);
1094 list
->destroy(list
);
1096 if (this->packet_tsi
)
1098 this->tsi
->insert_first(this->tsi
,
1099 this->packet_tsi
->clone(this->packet_tsi
));
1101 if (this->packet_tsr
)
1103 this->tsr
->insert_first(this->tsr
,
1104 this->packet_tsr
->clone(this->packet_tsr
));
1106 this->proposals
= this->config
->get_proposals(this->config
,
1107 this->dh_group
== MODP_NONE
);
1108 this->mode
= this->config
->get_mode(this->config
);
1110 this->child_sa
= child_sa_create(this->ike_sa
->get_my_host(this->ike_sa
),
1111 this->ike_sa
->get_other_host(this->ike_sa
), this->config
, this->reqid
,
1112 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
),
1113 this->mark_in
, this->mark_out
);
1117 DBG0(DBG_IKE
, "establishing CHILD_SA %s{%d} reqid %d",
1118 this->child_sa
->get_name(this->child_sa
),
1119 this->child_sa
->get_unique_id(this->child_sa
), this->reqid
);
1123 DBG0(DBG_IKE
, "establishing CHILD_SA %s{%d}",
1124 this->child_sa
->get_name(this->child_sa
),
1125 this->child_sa
->get_unique_id(this->child_sa
));
1128 if (!allocate_spi(this))
1130 DBG1(DBG_IKE
, "unable to allocate SPIs from kernel");
1134 if (!update_and_check_proposals(this))
1136 DBG1(DBG_IKE
, "requested DH group %N not contained in any of our "
1138 diffie_hellman_group_names
, this->dh_group
);
1142 if (this->dh_group
!= MODP_NONE
)
1144 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
1148 if (this->config
->has_option(this->config
, OPT_IPCOMP
))
1150 /* IPCOMP_DEFLATE is the only transform we support at the moment */
1151 add_ipcomp_notify(this, message
, IPCOMP_DEFLATE
);
1154 if (message
->get_exchange_type(message
) == IKE_AUTH
)
1156 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
1157 NARROW_INITIATOR_PRE_NOAUTH
, this->tsi
, this->tsr
);
1161 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
1162 NARROW_INITIATOR_PRE_AUTH
, this->tsi
, this->tsr
);
1165 if (!build_payloads(this, message
))
1170 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1171 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1172 this->proposals
->destroy_offset(this->proposals
, offsetof(proposal_t
, destroy
));
1175 this->proposals
= NULL
;
1180 METHOD(task_t
, process_r
, status_t
,
1181 private_child_create_t
*this, message_t
*message
)
1183 switch (message
->get_exchange_type(message
))
1186 return get_nonce(message
, &this->other_nonce
);
1187 case CREATE_CHILD_SA
:
1188 get_nonce(message
, &this->other_nonce
);
1191 if (message
->get_message_id(message
) != 1)
1193 /* only handle first AUTH payload, not additional rounds */
1200 process_payloads(this, message
);
1206 * handle CHILD_SA setup failure
1208 static void handle_child_sa_failure(private_child_create_t
*this,
1213 is_first
= message
->get_exchange_type(message
) == IKE_AUTH
;
1215 lib
->settings
->get_bool(lib
->settings
,
1216 "%s.close_ike_on_child_failure", FALSE
, lib
->ns
))
1218 /* we delay the delete for 100ms, as the IKE_AUTH response must arrive
1220 DBG1(DBG_IKE
, "closing IKE_SA due CHILD_SA setup failure");
1221 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
1222 delete_ike_sa_job_create(this->ike_sa
->get_id(this->ike_sa
), TRUE
),
1227 DBG1(DBG_IKE
, "failed to establish CHILD_SA, keeping IKE_SA");
1228 charon
->bus
->alert(charon
->bus
, ALERT_KEEP_ON_CHILD_SA_FAILURE
,
1234 * Substitute transport mode NAT selectors, if applicable
1236 static linked_list_t
* get_ts_if_nat_transport(private_child_create_t
*this,
1237 bool local
, linked_list_t
*in
)
1239 linked_list_t
*out
= NULL
;
1240 ike_condition_t cond
;
1242 if (this->mode
== MODE_TRANSPORT
)
1244 cond
= local ? COND_NAT_HERE
: COND_NAT_THERE
;
1245 if (this->ike_sa
->has_condition(this->ike_sa
, cond
))
1247 out
= get_transport_nat_ts(this, local
, in
);
1248 if (out
->get_count(out
) == 0)
1259 * Select a matching CHILD config as responder
1261 static child_cfg_t
* select_child_cfg(private_child_create_t
*this)
1263 peer_cfg_t
*peer_cfg
;
1264 child_cfg_t
*child_cfg
= NULL
;;
1266 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1267 if (peer_cfg
&& this->tsi
&& this->tsr
)
1269 linked_list_t
*listr
, *listi
, *tsr
, *tsi
;
1271 tsr
= get_ts_if_nat_transport(this, TRUE
, this->tsr
);
1272 tsi
= get_ts_if_nat_transport(this, FALSE
, this->tsi
);
1274 listr
= get_dynamic_hosts(this->ike_sa
, TRUE
);
1275 listi
= get_dynamic_hosts(this->ike_sa
, FALSE
);
1276 child_cfg
= peer_cfg
->select_child_cfg(peer_cfg
,
1277 tsr ?
: this->tsr
, tsi ?
: this->tsi
,
1279 if ((tsi
|| tsr
) && child_cfg
&&
1280 child_cfg
->get_mode(child_cfg
) != MODE_TRANSPORT
)
1282 /* found a CHILD config, but it doesn't use transport mode */
1283 child_cfg
->destroy(child_cfg
);
1286 if (!child_cfg
&& (tsi
|| tsr
))
1288 /* no match for the substituted NAT selectors, try it without */
1289 child_cfg
= peer_cfg
->select_child_cfg(peer_cfg
,
1290 this->tsr
, this->tsi
, listr
, listi
);
1292 listr
->destroy(listr
);
1293 listi
->destroy(listi
);
1294 DESTROY_OFFSET_IF(tsi
, offsetof(traffic_selector_t
, destroy
));
1295 DESTROY_OFFSET_IF(tsr
, offsetof(traffic_selector_t
, destroy
));
1301 METHOD(task_t
, build_r
, status_t
,
1302 private_child_create_t
*this, message_t
*message
)
1305 enumerator_t
*enumerator
;
1306 bool no_dh
= TRUE
, ike_auth
= FALSE
;
1308 switch (message
->get_exchange_type(message
))
1311 return get_nonce(message
, &this->my_nonce
);
1312 case CREATE_CHILD_SA
:
1313 if (!generate_nonce(this))
1315 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
,
1319 if (this->dh_failed
)
1321 DBG1(DBG_IKE
, "applying DH public value failed");
1322 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
,
1329 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1330 { /* wait until all authentication round completed */
1333 if (this->ike_sa
->has_condition(this->ike_sa
, COND_REDIRECTED
))
1334 { /* no CHILD_SA is created for redirected SAs */
1342 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_REKEYING
)
1344 DBG1(DBG_IKE
, "unable to create CHILD_SA while rekeying IKE_SA");
1345 message
->add_notify(message
, TRUE
, TEMPORARY_FAILURE
, chunk_empty
);
1348 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_DELETING
)
1350 DBG1(DBG_IKE
, "unable to create CHILD_SA while deleting IKE_SA");
1351 message
->add_notify(message
, TRUE
, TEMPORARY_FAILURE
, chunk_empty
);
1355 if (this->config
== NULL
)
1357 this->config
= select_child_cfg(this);
1359 if (this->config
== NULL
)
1361 DBG1(DBG_IKE
, "traffic selectors %#R === %#R unacceptable",
1362 this->tsr
, this->tsi
);
1363 charon
->bus
->alert(charon
->bus
, ALERT_TS_MISMATCH
, this->tsi
, this->tsr
);
1364 message
->add_notify(message
, FALSE
, TS_UNACCEPTABLE
, chunk_empty
);
1365 handle_child_sa_failure(this, message
);
1369 /* check if ike_config_t included non-critical error notifies */
1370 enumerator
= message
->create_payload_enumerator(message
);
1371 while (enumerator
->enumerate(enumerator
, &payload
))
1373 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
1375 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
1377 switch (notify
->get_notify_type(notify
))
1379 case INTERNAL_ADDRESS_FAILURE
:
1380 case FAILED_CP_REQUIRED
:
1382 DBG1(DBG_IKE
,"configuration payload negotiation "
1383 "failed, no CHILD_SA built");
1384 enumerator
->destroy(enumerator
);
1385 handle_child_sa_failure(this, message
);
1393 enumerator
->destroy(enumerator
);
1395 this->child_sa
= child_sa_create(this->ike_sa
->get_my_host(this->ike_sa
),
1396 this->ike_sa
->get_other_host(this->ike_sa
), this->config
, this->reqid
,
1397 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
),
1398 this->mark_in
, this->mark_out
);
1400 if (this->ipcomp_received
!= IPCOMP_NONE
)
1402 if (this->config
->has_option(this->config
, OPT_IPCOMP
))
1404 add_ipcomp_notify(this, message
, this->ipcomp_received
);
1408 DBG1(DBG_IKE
, "received %N notify but IPComp is disabled, ignoring",
1409 notify_type_names
, IPCOMP_SUPPORTED
);
1413 switch (select_and_install(this, no_dh
, ike_auth
))
1418 message
->add_notify(message
, FALSE
, TS_UNACCEPTABLE
, chunk_empty
);
1419 handle_child_sa_failure(this, message
);
1423 uint16_t group
= htons(this->dh_group
);
1424 message
->add_notify(message
, FALSE
, INVALID_KE_PAYLOAD
,
1425 chunk_from_thing(group
));
1430 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
1431 handle_child_sa_failure(this, message
);
1435 if (!build_payloads(this, message
))
1437 message
->add_notify(message
, FALSE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
1438 handle_child_sa_failure(this, message
);
1443 { /* invoke the child_up() hook if we are not rekeying */
1444 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
1450 * Raise alerts for received notify errors
1452 static void raise_alerts(private_child_create_t
*this, notify_type_t type
)
1454 linked_list_t
*list
;
1458 case NO_PROPOSAL_CHOSEN
:
1459 list
= this->config
->get_proposals(this->config
, FALSE
);
1460 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_CHILD
, list
);
1461 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1468 METHOD(task_t
, build_i_delete
, status_t
,
1469 private_child_create_t
*this, message_t
*message
)
1471 message
->set_exchange_type(message
, INFORMATIONAL
);
1472 if (this->my_spi
&& this->proto
)
1474 delete_payload_t
*del
;
1476 del
= delete_payload_create(PLV2_DELETE
, this->proto
);
1477 del
->add_spi(del
, this->my_spi
);
1478 message
->add_payload(message
, (payload_t
*)del
);
1480 DBG1(DBG_IKE
, "sending DELETE for %N CHILD_SA with SPI %.8x",
1481 protocol_id_names
, this->proto
, ntohl(this->my_spi
));
1487 * Change task to delete the failed CHILD_SA as initiator
1489 static status_t
delete_failed_sa(private_child_create_t
*this)
1491 if (this->my_spi
&& this->proto
)
1493 this->public.task
.build
= _build_i_delete
;
1494 this->public.task
.process
= (void*)return_success
;
1500 METHOD(task_t
, process_i
, status_t
,
1501 private_child_create_t
*this, message_t
*message
)
1503 enumerator_t
*enumerator
;
1505 bool no_dh
= TRUE
, ike_auth
= FALSE
;
1507 switch (message
->get_exchange_type(message
))
1510 return get_nonce(message
, &this->other_nonce
);
1511 case CREATE_CHILD_SA
:
1512 get_nonce(message
, &this->other_nonce
);
1516 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1517 { /* wait until all authentication round completed */
1525 /* check for erroneous notifies */
1526 enumerator
= message
->create_payload_enumerator(message
);
1527 while (enumerator
->enumerate(enumerator
, &payload
))
1529 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
1531 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
1532 notify_type_t type
= notify
->get_notify_type(notify
);
1536 /* handle notify errors related to CHILD_SA only */
1537 case NO_PROPOSAL_CHOSEN
:
1538 case SINGLE_PAIR_REQUIRED
:
1539 case NO_ADDITIONAL_SAS
:
1540 case INTERNAL_ADDRESS_FAILURE
:
1541 case FAILED_CP_REQUIRED
:
1542 case TS_UNACCEPTABLE
:
1543 case INVALID_SELECTORS
:
1545 DBG1(DBG_IKE
, "received %N notify, no CHILD_SA built",
1546 notify_type_names
, type
);
1547 enumerator
->destroy(enumerator
);
1548 raise_alerts(this, type
);
1549 handle_child_sa_failure(this, message
);
1550 /* an error in CHILD_SA creation is not critical */
1553 case TEMPORARY_FAILURE
:
1555 DBG1(DBG_IKE
, "received %N notify, will retry later",
1556 notify_type_names
, type
);
1557 enumerator
->destroy(enumerator
);
1559 { /* the rekey task will retry itself if necessary */
1560 schedule_delayed_retry(this);
1564 case INVALID_KE_PAYLOAD
:
1567 uint16_t group
= MODP_NONE
;
1569 data
= notify
->get_notification_data(notify
);
1570 if (data
.len
== sizeof(group
))
1572 memcpy(&group
, data
.ptr
, data
.len
);
1573 group
= ntohs(group
);
1577 DBG1(DBG_IKE
, "already retried with DH group %N, ignore"
1578 "requested %N", diffie_hellman_group_names
,
1579 this->dh_group
, diffie_hellman_group_names
, group
);
1580 handle_child_sa_failure(this, message
);
1581 /* an error in CHILD_SA creation is not critical */
1584 DBG1(DBG_IKE
, "peer didn't accept DH group %N, "
1585 "it requested %N", diffie_hellman_group_names
,
1586 this->dh_group
, diffie_hellman_group_names
, group
);
1588 this->dh_group
= group
;
1589 this->child_sa
->set_state(this->child_sa
, CHILD_RETRYING
);
1590 this->public.task
.migrate(&this->public.task
, this->ike_sa
);
1591 enumerator
->destroy(enumerator
);
1596 if (message
->get_exchange_type(message
) == CREATE_CHILD_SA
)
1597 { /* handle notifies if not handled in IKE_AUTH */
1600 DBG1(DBG_IKE
, "received %N notify error",
1601 notify_type_names
, type
);
1602 enumerator
->destroy(enumerator
);
1605 DBG2(DBG_IKE
, "received %N notify",
1606 notify_type_names
, type
);
1613 enumerator
->destroy(enumerator
);
1615 process_payloads(this, message
);
1617 if (this->ipcomp
== IPCOMP_NONE
&& this->ipcomp_received
!= IPCOMP_NONE
)
1619 DBG1(DBG_IKE
, "received an IPCOMP_SUPPORTED notify without requesting"
1620 " one, no CHILD_SA built");
1621 handle_child_sa_failure(this, message
);
1622 return delete_failed_sa(this);
1624 else if (this->ipcomp
!= IPCOMP_NONE
&& this->ipcomp_received
== IPCOMP_NONE
)
1626 DBG1(DBG_IKE
, "peer didn't accept our proposed IPComp transforms, "
1627 "IPComp is disabled");
1628 this->ipcomp
= IPCOMP_NONE
;
1630 else if (this->ipcomp
!= IPCOMP_NONE
&& this->ipcomp
!= this->ipcomp_received
)
1632 DBG1(DBG_IKE
, "received an IPCOMP_SUPPORTED notify we didn't propose, "
1633 "no CHILD_SA built");
1634 handle_child_sa_failure(this, message
);
1635 return delete_failed_sa(this);
1638 if (this->dh_failed
)
1640 DBG1(DBG_IKE
, "applying DH public value failed");
1641 handle_child_sa_failure(this, message
);
1642 return delete_failed_sa(this);
1645 if (select_and_install(this, no_dh
, ike_auth
) == SUCCESS
)
1648 { /* invoke the child_up() hook if we are not rekeying */
1649 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
1654 handle_child_sa_failure(this, message
);
1655 return delete_failed_sa(this);
1660 METHOD(child_create_t
, use_reqid
, void,
1661 private_child_create_t
*this, uint32_t reqid
)
1663 this->reqid
= reqid
;
1666 METHOD(child_create_t
, use_marks
, void,
1667 private_child_create_t
*this, u_int in
, u_int out
)
1670 this->mark_out
= out
;
1673 METHOD(child_create_t
, use_dh_group
, void,
1674 private_child_create_t
*this, diffie_hellman_group_t dh_group
)
1676 this->dh_group
= dh_group
;
1679 METHOD(child_create_t
, get_child
, child_sa_t
*,
1680 private_child_create_t
*this)
1682 return this->child_sa
;
1685 METHOD(child_create_t
, set_config
, void,
1686 private_child_create_t
*this, child_cfg_t
*cfg
)
1688 DESTROY_IF(this->config
);
1692 METHOD(child_create_t
, get_lower_nonce
, chunk_t
,
1693 private_child_create_t
*this)
1695 if (memcmp(this->my_nonce
.ptr
, this->other_nonce
.ptr
,
1696 min(this->my_nonce
.len
, this->other_nonce
.len
)) < 0)
1698 return this->my_nonce
;
1702 return this->other_nonce
;
1706 METHOD(task_t
, get_type
, task_type_t
,
1707 private_child_create_t
*this)
1709 return TASK_CHILD_CREATE
;
1712 METHOD(task_t
, migrate
, void,
1713 private_child_create_t
*this, ike_sa_t
*ike_sa
)
1715 chunk_free(&this->my_nonce
);
1716 chunk_free(&this->other_nonce
);
1719 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1723 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1725 DESTROY_IF(this->child_sa
);
1726 DESTROY_IF(this->proposal
);
1727 DESTROY_IF(this->nonceg
);
1728 DESTROY_IF(this->dh
);
1729 this->dh_failed
= FALSE
;
1730 if (this->proposals
)
1732 this->proposals
->destroy_offset(this->proposals
, offsetof(proposal_t
, destroy
));
1735 this->ike_sa
= ike_sa
;
1736 this->keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
);
1737 this->proposal
= NULL
;
1738 this->proposals
= NULL
;
1742 this->nonceg
= NULL
;
1743 this->child_sa
= NULL
;
1744 this->mode
= MODE_TUNNEL
;
1745 this->ipcomp
= IPCOMP_NONE
;
1746 this->ipcomp_received
= IPCOMP_NONE
;
1747 this->other_cpi
= 0;
1751 this->established
= FALSE
;
1754 METHOD(task_t
, destroy
, void,
1755 private_child_create_t
*this)
1757 chunk_free(&this->my_nonce
);
1758 chunk_free(&this->other_nonce
);
1761 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1765 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1767 if (!this->established
)
1769 DESTROY_IF(this->child_sa
);
1771 DESTROY_IF(this->packet_tsi
);
1772 DESTROY_IF(this->packet_tsr
);
1773 DESTROY_IF(this->proposal
);
1774 DESTROY_IF(this->dh
);
1775 if (this->proposals
)
1777 this->proposals
->destroy_offset(this->proposals
, offsetof(proposal_t
, destroy
));
1779 DESTROY_IF(this->config
);
1780 DESTROY_IF(this->nonceg
);
1785 * Described in header.
1787 child_create_t
*child_create_create(ike_sa_t
*ike_sa
,
1788 child_cfg_t
*config
, bool rekey
,
1789 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1791 private_child_create_t
*this;
1795 .get_child
= _get_child
,
1796 .set_config
= _set_config
,
1797 .get_lower_nonce
= _get_lower_nonce
,
1798 .use_reqid
= _use_reqid
,
1799 .use_marks
= _use_marks
,
1800 .use_dh_group
= _use_dh_group
,
1802 .get_type
= _get_type
,
1803 .migrate
= _migrate
,
1804 .destroy
= _destroy
,
1809 .packet_tsi
= tsi ? tsi
->clone(tsi
) : NULL
,
1810 .packet_tsr
= tsr ? tsr
->clone(tsr
) : NULL
,
1811 .dh_group
= MODP_NONE
,
1812 .keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
),
1813 .mode
= MODE_TUNNEL
,
1815 .ipcomp
= IPCOMP_NONE
,
1816 .ipcomp_received
= IPCOMP_NONE
,
1823 this->public.task
.build
= _build_i
;
1824 this->public.task
.process
= _process_i
;
1825 this->initiator
= TRUE
;
1829 this->public.task
.build
= _build_r
;
1830 this->public.task
.process
= _process_r
;
1831 this->initiator
= FALSE
;
1833 return &this->public;