2 * @file create_child_sa.c
4 * @brief Implementation of create_child_sa_t transaction.
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #include "create_child_sa.h"
28 #include <encoding/payloads/sa_payload.h>
29 #include <encoding/payloads/nonce_payload.h>
30 #include <encoding/payloads/ts_payload.h>
31 #include <sa/transactions/delete_child_sa.h>
32 #include <utils/randomizer.h>
35 typedef struct private_create_child_sa_t private_create_child_sa_t
;
38 * Private members of a create_child_sa_t object..
40 struct private_create_child_sa_t
{
43 * Public methods and transaction_t interface.
45 create_child_sa_t
public;
53 * Message sent by our peer, if already generated
58 * Message ID this transaction uses
63 * Times we did send the request
68 * initiators inbound SPI of the CHILD_SA which gets rekeyed
73 * reqid to use for new CHILD_SA
78 * policy definition used
83 * Negotiated proposal used for CHILD_SA
88 * initiator chosen nonce
93 * responder chosen nonce
98 * lower of the nonces of a simultaneus rekeying request
103 * Negotiated traffic selectors for initiator
108 * Negotiated traffic selectors for responder
113 * CHILD_SA created by this transaction
115 child_sa_t
*child_sa
;
118 * CHILD_SA rekeyed if we are rekeying
120 child_sa_t
*rekeyed_sa
;
123 * Have we lost the simultaneous rekeying nonce compare?
128 * source of randomness
130 randomizer_t
*randomizer
;
134 * Implementation of transaction_t.get_message_id.
136 static u_int32_t
get_message_id(private_create_child_sa_t
*this)
138 return this->message_id
;
142 * Implementation of transaction_t.requested.
144 static u_int32_t
requested(private_create_child_sa_t
*this)
146 return this->requested
++;
150 * Implementation of create_child_sa_t.set_policy.
152 static void set_policy(private_create_child_sa_t
*this, policy_t
*policy
)
154 this->policy
= policy
;
158 * Implementation of create_child_sa_t.set_reqid.
160 static void set_reqid(private_create_child_sa_t
*this, u_int32_t reqid
)
166 * Implementation of create_child_sa_t.rekeys_child.
168 static void rekeys_child(private_create_child_sa_t
*this, child_sa_t
*child_sa
)
170 this->rekeyed_sa
= child_sa
;
174 * Implementation of create_child_sa_t.cancel.
176 static void cancel(private_create_child_sa_t
*this)
178 this->rekeyed_sa
= NULL
;
183 * Implementation of transaction_t.get_request.
185 static status_t
get_request(private_create_child_sa_t
*this, message_t
**result
)
189 identification_t
*my_id
, *other_id
;
191 /* check if we already have built a message (retransmission) */
194 *result
= this->message
;
198 /* check if we are not already rekeying */
199 if (this->rekeyed_sa
)
201 switch (this->rekeyed_sa
->get_state(this->rekeyed_sa
))
205 "rekeying a CHILD_SA which is already rekeying, aborted");
209 "rekeying a CHILD_SA which is deleting, aborted");
214 this->rekeyed_sa
->set_state(this->rekeyed_sa
, CHILD_REKEYING
);
217 me
= this->ike_sa
->get_my_host(this->ike_sa
);
218 other
= this->ike_sa
->get_other_host(this->ike_sa
);
219 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
220 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
222 /* build the request */
223 request
= message_create();
224 request
->set_source(request
, me
->clone(me
));
225 request
->set_destination(request
, other
->clone(other
));
226 request
->set_exchange_type(request
, CREATE_CHILD_SA
);
227 request
->set_request(request
, TRUE
);
228 request
->set_ike_sa_id(request
, this->ike_sa
->get_id(this->ike_sa
));
230 this->message
= request
;
232 { /* build SA payload */
233 sa_payload_t
*sa_payload
;
234 linked_list_t
*proposals
;
237 /* get a policy, if we are rekeying */
238 if (this->rekeyed_sa
)
240 linked_list_t
*my_ts
, *other_ts
;
241 identification_t
*my_id
, *other_id
;
243 my_ts
= this->rekeyed_sa
->get_my_traffic_selectors(this->rekeyed_sa
);
244 other_ts
= this->rekeyed_sa
->get_other_traffic_selectors(this->rekeyed_sa
);
245 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
246 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
248 this->policy
= charon
->policies
->get_policy(charon
->policies
,
253 this->reqid
= this->rekeyed_sa
->get_reqid(this->rekeyed_sa
);
255 if (this->policy
== NULL
)
257 DBG1(SIG_DBG_IKE
, "no policy found to rekey "
258 "CHILD_SA with reqid %d", this->reqid
);
263 proposals
= this->policy
->get_proposals(this->policy
);
264 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
265 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
266 this->policy
->get_soft_lifetime(this->policy
),
267 this->policy
->get_hard_lifetime(this->policy
),
268 this->policy
->get_updown(this->policy
),
269 this->policy
->get_hostaccess(this->policy
),
271 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
272 if (this->child_sa
->alloc(this->child_sa
, proposals
) != SUCCESS
)
274 SIG(SIG_CHILD_FAILED
, "could not install CHILD_SA, CHILD_SA creation aborted");
277 sa_payload
= sa_payload_create_from_proposal_list(proposals
);
278 proposals
->destroy_offset(proposals
, offsetof(proposal_t
, destroy
));
279 request
->add_payload(request
, (payload_t
*)sa_payload
);
282 { /* build the NONCE payload for us (initiator) */
283 nonce_payload_t
*nonce_payload
;
285 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
,
286 NONCE_SIZE
, &this->nonce_i
) != SUCCESS
)
288 SIG(SIG_CHILD_FAILED
, "could not create nonce");
291 nonce_payload
= nonce_payload_create();
292 nonce_payload
->set_nonce(nonce_payload
, this->nonce_i
);
293 request
->add_payload(request
, (payload_t
*)nonce_payload
);
296 { /* build TSi payload */
297 linked_list_t
*ts_list
;
298 ts_payload_t
*ts_payload
;
300 ts_list
= this->policy
->get_my_traffic_selectors(this->policy
, me
);
301 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, ts_list
);
302 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
303 request
->add_payload(request
, (payload_t
*)ts_payload
);
306 { /* build TSr payload */
307 linked_list_t
*ts_list
;
308 ts_payload_t
*ts_payload
;
310 ts_list
= this->policy
->get_other_traffic_selectors(this->policy
, other
);
311 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, ts_list
);
312 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
313 request
->add_payload(request
, (payload_t
*)ts_payload
);
316 if (this->rekeyed_sa
)
317 { /* add REKEY_SA notify if we are rekeying */
318 notify_payload_t
*notify
;
319 protocol_id_t protocol
;
321 protocol
= this->rekeyed_sa
->get_protocol(this->rekeyed_sa
);
322 notify
= notify_payload_create_from_protocol_and_type(protocol
, REKEY_SA
);
323 notify
->set_spi(notify
, this->rekeyed_sa
->get_spi(this->rekeyed_sa
, TRUE
));
324 request
->add_payload(request
, (payload_t
*)notify
);
326 /* register us as rekeying to detect multiple rekeying */
327 this->rekeyed_sa
->set_rekeying_transaction(this->rekeyed_sa
, &this->public);
330 this->message_id
= this->ike_sa
->get_next_message_id(this->ike_sa
);
331 request
->set_message_id(request
, this->message_id
);
337 * Handle all kind of notifys
339 static status_t
process_notifys(private_create_child_sa_t
*this, notify_payload_t
*notify_payload
)
341 notify_type_t notify_type
= notify_payload
->get_notify_type(notify_payload
);
343 DBG2(SIG_DBG_IKE
, "process notify type %N", notify_type_names
, notify_type
);
347 case SINGLE_PAIR_REQUIRED
:
349 SIG(SIG_CHILD_FAILED
, "received a SINGLE_PAIR_REQUIRED notify");
352 case TS_UNACCEPTABLE
:
354 SIG(SIG_CHILD_FAILED
, "received TS_UNACCEPTABLE notify");
357 case NO_PROPOSAL_CHOSEN
:
359 SIG(SIG_CHILD_FAILED
, "received NO_PROPOSAL_CHOSEN notify");
365 protocol_id_t protocol
;
367 protocol
= notify_payload
->get_protocol_id(notify_payload
);
372 spi
= notify_payload
->get_spi(notify_payload
);
373 this->rekeyed_sa
= this->ike_sa
->get_child_sa(this->ike_sa
,
384 if (notify_type
< 16383)
386 SIG(SIG_CHILD_FAILED
, "received %N notify error, CHILD_SA "
387 "creation failed", notify_type_names
, notify_type
);
392 DBG1(SIG_DBG_IKE
, "received %N notify, ignored",
393 notify_type_names
, notify_type
);
401 * Build a notify message.
403 static void build_notify(notify_type_t type
, chunk_t data
, message_t
*message
, bool flush_message
)
405 notify_payload_t
*notify
;
410 iterator_t
*iterator
= message
->get_payload_iterator(message
);
411 while (iterator
->iterate(iterator
, (void**)&payload
))
413 payload
->destroy(payload
);
414 iterator
->remove(iterator
);
416 iterator
->destroy(iterator
);
419 notify
= notify_payload_create();
420 notify
->set_notify_type(notify
, type
);
421 notify
->set_notification_data(notify
, data
);
422 message
->add_payload(message
, (payload_t
*)notify
);
426 * Install a CHILD_SA for usage
428 static status_t
install_child_sa(private_create_child_sa_t
*this, bool initiator
)
430 prf_plus_t
*prf_plus
;
434 seed
= chunk_alloc(this->nonce_i
.len
+ this->nonce_r
.len
);
435 memcpy(seed
.ptr
, this->nonce_i
.ptr
, this->nonce_i
.len
);
436 memcpy(seed
.ptr
+ this->nonce_i
.len
, this->nonce_r
.ptr
, this->nonce_r
.len
);
437 prf_plus
= prf_plus_create(this->ike_sa
->get_child_prf(this->ike_sa
), seed
);
442 status
= this->child_sa
->update(this->child_sa
, this->proposal
, prf_plus
);
446 status
= this->child_sa
->add(this->child_sa
, this->proposal
, prf_plus
);
448 prf_plus
->destroy(prf_plus
);
449 if (status
!= SUCCESS
)
455 status
= this->child_sa
->add_policies(this->child_sa
, this->tsi
, this->tsr
);
459 status
= this->child_sa
->add_policies(this->child_sa
, this->tsr
, this->tsi
);
461 if (status
!= SUCCESS
)
466 /* add to IKE_SA, and remove from transaction */
467 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
468 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
469 this->child_sa
= NULL
;
474 * Implementation of transaction_t.get_response.
476 static status_t
get_response(private_create_child_sa_t
*this, message_t
*request
,
477 message_t
**result
, transaction_t
**next
)
480 identification_t
*my_id
, *other_id
;
483 iterator_t
*payloads
;
485 sa_payload_t
*sa_request
= NULL
;
486 nonce_payload_t
*nonce_request
= NULL
;
487 ts_payload_t
*tsi_request
= NULL
;
488 ts_payload_t
*tsr_request
= NULL
;
489 nonce_payload_t
*nonce_response
;
491 /* check if we already have built a response (retransmission) */
494 *result
= this->message
;
498 me
= this->ike_sa
->get_my_host(this->ike_sa
);
499 other
= this->ike_sa
->get_other_host(this->ike_sa
);
500 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
501 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
502 this->message_id
= request
->get_message_id(request
);
504 /* set up response */
505 response
= message_create();
506 response
->set_source(response
, me
->clone(me
));
507 response
->set_destination(response
, other
->clone(other
));
508 response
->set_exchange_type(response
, CREATE_CHILD_SA
);
509 response
->set_request(response
, FALSE
);
510 response
->set_message_id(response
, this->message_id
);
511 response
->set_ike_sa_id(response
, this->ike_sa
->get_id(this->ike_sa
));
512 this->message
= response
;
515 /* check message type */
516 if (request
->get_exchange_type(request
) != CREATE_CHILD_SA
)
518 SIG(SIG_CHILD_FAILED
, "CREATE_CHILD_SA response of invalid type, aborted");
522 /* we do not allow the creation of new CHILDren/rekeying when IKE_SA is
524 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_REKEYING
||
525 this->ike_sa
->get_state(this->ike_sa
) == IKE_DELETING
)
527 build_notify(NO_ADDITIONAL_SAS
, CHUNK_INITIALIZER
, response
, TRUE
);
528 SIG(SIG_CHILD_FAILED
, "unable to create new CHILD_SAs, as rekeying in progress");
532 /* Iterate over all payloads. */
533 payloads
= request
->get_payload_iterator(request
);
534 while (payloads
->iterate(payloads
, (void**)&payload
))
536 switch (payload
->get_type(payload
))
538 case SECURITY_ASSOCIATION
:
539 sa_request
= (sa_payload_t
*)payload
;
542 nonce_request
= (nonce_payload_t
*)payload
;
544 case TRAFFIC_SELECTOR_INITIATOR
:
545 tsi_request
= (ts_payload_t
*)payload
;
547 case TRAFFIC_SELECTOR_RESPONDER
:
548 tsr_request
= (ts_payload_t
*)payload
;
552 u_int8_t dh_buffer
[] = {0x00, 0x00}; /* MODP_NONE */
553 chunk_t group
= chunk_from_buf(dh_buffer
);
554 build_notify(INVALID_KE_PAYLOAD
, group
, response
, TRUE
);
555 SIG(SIG_CHILD_FAILED
, "CREATE_CHILD_SA used PFS, sending INVALID_KE_PAYLOAD");
560 status
= process_notifys(this, (notify_payload_t
*)payload
);
561 if (status
!= SUCCESS
)
563 payloads
->destroy(payloads
);
570 DBG1(SIG_DBG_IKE
, "ignoring %N payload",
571 payload_type_names
, payload
->get_type(payload
));
576 payloads
->destroy(payloads
);
578 /* check if we have all payloads */
579 if (!(sa_request
&& nonce_request
&& tsi_request
&& tsr_request
))
581 build_notify(INVALID_SYNTAX
, CHUNK_INITIALIZER
, response
, TRUE
);
582 SIG(SIG_CHILD_FAILED
, "request message incomplete, no CHILD_SA created");
586 { /* process nonce payload */
587 this->nonce_i
= nonce_request
->get_nonce(nonce_request
);
588 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
,
589 NONCE_SIZE
, &this->nonce_r
) != SUCCESS
)
591 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
594 nonce_response
= nonce_payload_create();
595 nonce_response
->set_nonce(nonce_response
, this->nonce_r
);
598 { /* get a policy and process traffic selectors */
599 identification_t
*my_id
, *other_id
;
600 linked_list_t
*my_ts
, *other_ts
;
602 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
603 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
605 my_ts
= tsr_request
->get_traffic_selectors(tsr_request
);
606 other_ts
= tsi_request
->get_traffic_selectors(tsi_request
);
608 this->policy
= charon
->policies
->get_policy(charon
->policies
,
614 this->tsr
= this->policy
->select_my_traffic_selectors(this->policy
, my_ts
, me
);
615 this->tsi
= this->policy
->select_other_traffic_selectors(this->policy
, other_ts
, other
);
617 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
618 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
620 if (this->policy
== NULL
)
622 SIG(SIG_CHILD_FAILED
, "no acceptable policy found, sending TS_UNACCEPTABLE notify");
623 build_notify(TS_UNACCEPTABLE
, CHUNK_INITIALIZER
, response
, TRUE
);
628 { /* process SA payload */
629 linked_list_t
*proposal_list
;
630 sa_payload_t
*sa_response
;
631 ts_payload_t
*ts_response
;
633 u_int32_t soft_lifetime
, hard_lifetime
;
635 sa_response
= sa_payload_create();
636 /* get proposals from request, and select one with ours */
637 proposal_list
= sa_request
->get_proposals(sa_request
);
638 DBG2(SIG_DBG_IKE
, "selecting proposals:");
639 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
640 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
642 /* do we have a proposal? */
643 if (this->proposal
== NULL
)
645 SIG(SIG_CHILD_FAILED
, "CHILD_SA proposals unacceptable, sending NO_PROPOSAL_CHOSEN notify");
646 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
649 /* do we have traffic selectors? */
650 else if (this->tsi
->get_count(this->tsi
) == 0 || this->tsr
->get_count(this->tsr
) == 0)
652 SIG(SIG_CHILD_FAILED
, "CHILD_SA traffic selectors unacceptable, sending TS_UNACCEPTABLE notify");
653 build_notify(TS_UNACCEPTABLE
, CHUNK_INITIALIZER
, response
, TRUE
);
657 { /* create child sa */
658 if (this->rekeyed_sa
)
660 this->reqid
= this->rekeyed_sa
->get_reqid(this->rekeyed_sa
);
662 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
663 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
664 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
665 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
666 soft_lifetime
, hard_lifetime
,
667 this->policy
->get_updown(this->policy
),
668 this->policy
->get_hostaccess(this->policy
),
670 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
671 if (install_child_sa(this, FALSE
) != SUCCESS
)
673 SIG(SIG_CHILD_FAILED
, "installing CHILD_SA failed, sending NO_PROPOSAL_CHOSEN notify");
674 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
677 /* add proposal to sa payload */
678 sa_response
->add_proposal(sa_response
, this->proposal
);
680 response
->add_payload(response
, (payload_t
*)sa_response
);
682 /* add nonce/ts payload after sa payload */
683 response
->add_payload(response
, (payload_t
*)nonce_response
);
684 ts_response
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
685 response
->add_payload(response
, (payload_t
*)ts_response
);
686 ts_response
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
687 response
->add_payload(response
, (payload_t
*)ts_response
);
689 /* CHILD_SA successfully created. If another transaction is already rekeying
690 * this SA, our lower nonce must be registered for a later nonce compare. */
691 if (this->rekeyed_sa
)
693 private_create_child_sa_t
*other
;
695 other
= this->rekeyed_sa
->get_rekeying_transaction(this->rekeyed_sa
);
698 /* store our lower nonce in the simultaneus transaction, it
699 * will later compare it against his nonces when it calls conclude().
701 if (memcmp(this->nonce_i
.ptr
, this->nonce_r
.ptr
,
702 min(this->nonce_i
.len
, this->nonce_r
.len
)) < 0)
704 other
->nonce_s
= chunk_clone(this->nonce_i
);
708 other
->nonce_s
= chunk_clone(this->nonce_r
);
711 this->rekeyed_sa
->set_state(this->rekeyed_sa
, CHILD_REKEYING
);
715 SIG(SIG_CHILD_UP
, "CHILD_SA created");
721 * Implementation of transaction_t.conclude
723 static status_t
conclude(private_create_child_sa_t
*this, message_t
*response
,
724 transaction_t
**next
)
726 iterator_t
*payloads
;
729 sa_payload_t
*sa_payload
= NULL
;
730 nonce_payload_t
*nonce_payload
= NULL
;
731 ts_payload_t
*tsi_payload
= NULL
;
732 ts_payload_t
*tsr_payload
= NULL
;
734 child_sa_t
*new_child
= NULL
;
735 delete_child_sa_t
*delete_child_sa
;
737 /* check message type */
738 if (response
->get_exchange_type(response
) != CREATE_CHILD_SA
)
740 SIG(SIG_CHILD_FAILED
, "CREATE_CHILD_SA response of invalid type, aborting");
744 me
= this->ike_sa
->get_my_host(this->ike_sa
);
745 other
= this->ike_sa
->get_other_host(this->ike_sa
);
747 /* Iterate over all payloads to collect them */
748 payloads
= response
->get_payload_iterator(response
);
749 while (payloads
->iterate(payloads
, (void**)&payload
))
751 switch (payload
->get_type(payload
))
753 case SECURITY_ASSOCIATION
:
754 sa_payload
= (sa_payload_t
*)payload
;
757 nonce_payload
= (nonce_payload_t
*)payload
;
759 case TRAFFIC_SELECTOR_INITIATOR
:
760 tsi_payload
= (ts_payload_t
*)payload
;
762 case TRAFFIC_SELECTOR_RESPONDER
:
763 tsr_payload
= (ts_payload_t
*)payload
;
767 status
= process_notifys(this, (notify_payload_t
*)payload
);
768 if (status
!= SUCCESS
)
770 payloads
->destroy(payloads
);
777 DBG1(SIG_DBG_IKE
, "ignoring %N payload",
778 payload_type_names
, payload
->get_type(payload
));
783 payloads
->destroy(payloads
);
785 if (!(sa_payload
&& nonce_payload
&& tsi_payload
&& tsr_payload
))
787 SIG(SIG_CHILD_FAILED
, "response message incomplete, no CHILD_SA built");
791 { /* process NONCE payload */
792 this->nonce_r
= nonce_payload
->get_nonce(nonce_payload
);
795 { /* process traffic selectors for us */
796 linked_list_t
*ts_received
= tsi_payload
->get_traffic_selectors(tsi_payload
);
797 this->tsi
= this->policy
->select_my_traffic_selectors(this->policy
, ts_received
, me
);
798 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
801 { /* process traffic selectors for other */
802 linked_list_t
*ts_received
= tsr_payload
->get_traffic_selectors(tsr_payload
);
803 this->tsr
= this->policy
->select_other_traffic_selectors(this->policy
, ts_received
, other
);
804 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
807 { /* process sa payload */
808 linked_list_t
*proposal_list
;
810 proposal_list
= sa_payload
->get_proposals(sa_payload
);
811 /* we have to re-check here if other's selection is valid */
812 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
813 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
815 /* everything fine to create CHILD? */
816 if (this->proposal
== NULL
||
817 this->tsi
->get_count(this->tsi
) == 0 ||
818 this->tsr
->get_count(this->tsr
) == 0)
820 SIG(SIG_CHILD_FAILED
, "CHILD_SA negotiation failed, no CHILD_SA built");
823 new_child
= this->child_sa
;
824 if (install_child_sa(this, TRUE
) != SUCCESS
)
826 SIG(SIG_CHILD_FAILED
, "installing CHILD_SA failed, no CHILD_SA built");
829 SIG(SIG_CHILD_UP
, "CHILD_SA created");
831 /* CHILD_SA successfully created. If the other peer initiated rekeying
832 * in the meantime, we detect this by comparing the rekeying_transaction
833 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
834 * If no simultaneous rekeying is going on, we just initiate the delete of
835 * the superseded SA. */
836 if (this->rekeyed_sa
)
838 /* rekeying finished, update SA status */
839 this->rekeyed_sa
->set_rekeying_transaction(this->rekeyed_sa
, NULL
);
841 if (this->nonce_s
.ptr
)
842 { /* simlutaneous rekeying is going on, not so good */
845 /* first get our lowest nonce */
846 if (memcmp(this->nonce_i
.ptr
, this->nonce_r
.ptr
,
847 min(this->nonce_i
.len
, this->nonce_r
.len
)) < 0)
849 this_lowest
= this->nonce_i
;
853 this_lowest
= this->nonce_r
;
855 /* then compare against other lowest nonce */
856 if (memcmp(this_lowest
.ptr
, this->nonce_s
.ptr
,
857 min(this_lowest
.len
, this->nonce_s
.len
)) < 0)
859 DBG1(SIG_DBG_IKE
, "detected simultaneous CHILD_SA rekeying, deleting ours");
864 DBG1(SIG_DBG_IKE
, "detected simultaneous CHILD_SA rekeying, but ours is preferred");
867 /* delete the old SA if we have won the rekeying nonce compare*/
870 delete_child_sa
= delete_child_sa_create(this->ike_sa
);
871 delete_child_sa
->set_child_sa(delete_child_sa
, this->rekeyed_sa
);
872 *next
= (transaction_t
*)delete_child_sa
;
877 SIG(SIG_CHILD_UP
, "CHILD_SA created");
881 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
882 delete_child_sa
= delete_child_sa_create(this->ike_sa
);
883 delete_child_sa
->set_child_sa(delete_child_sa
, new_child
);
884 *next
= (transaction_t
*)delete_child_sa
;
890 * implements transaction_t.destroy
892 static void destroy(private_create_child_sa_t
*this)
894 DESTROY_IF(this->message
);
895 DESTROY_IF(this->proposal
);
896 DESTROY_IF(this->child_sa
);
897 DESTROY_IF(this->policy
);
898 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
899 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
900 chunk_free(&this->nonce_i
);
901 chunk_free(&this->nonce_r
);
902 chunk_free(&this->nonce_s
);
903 this->randomizer
->destroy(this->randomizer
);
908 * Described in header.
910 create_child_sa_t
*create_child_sa_create(ike_sa_t
*ike_sa
)
912 private_create_child_sa_t
*this = malloc_thing(private_create_child_sa_t
);
914 /* transaction interface functions */
915 this->public.transaction
.get_request
= (status_t(*)(transaction_t
*,message_t
**))get_request
;
916 this->public.transaction
.get_response
= (status_t(*)(transaction_t
*,message_t
*,message_t
**,transaction_t
**))get_response
;
917 this->public.transaction
.conclude
= (status_t(*)(transaction_t
*,message_t
*,transaction_t
**))conclude
;
918 this->public.transaction
.get_message_id
= (u_int32_t(*)(transaction_t
*))get_message_id
;
919 this->public.transaction
.requested
= (u_int32_t(*)(transaction_t
*))requested
;
920 this->public.transaction
.destroy
= (void(*)(transaction_t
*))destroy
;
922 /* public functions */
923 this->public.set_policy
= (void(*)(create_child_sa_t
*,policy_t
*))set_policy
;
924 this->public.set_reqid
= (void(*)(create_child_sa_t
*,u_int32_t
))set_reqid
;
925 this->public.rekeys_child
= (void(*)(create_child_sa_t
*,child_sa_t
*))rekeys_child
;
926 this->public.cancel
= (void(*)(create_child_sa_t
*))cancel
;
929 this->ike_sa
= ike_sa
;
930 this->message_id
= 0;
931 this->message
= NULL
;
935 this->nonce_i
= CHUNK_INITIALIZER
;
936 this->nonce_r
= CHUNK_INITIALIZER
;
937 this->nonce_s
= CHUNK_INITIALIZER
;
938 this->child_sa
= NULL
;
939 this->rekeyed_sa
= NULL
;
941 this->proposal
= NULL
;
945 this->randomizer
= randomizer_create();
947 return &this->public;