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
;
133 * signal to emit when transaction fails. As this transaction is used
134 * for CHILD_SA creation AND rekeying, we must emit different signals.
140 * Implementation of transaction_t.get_message_id.
142 static u_int32_t
get_message_id(private_create_child_sa_t
*this)
144 return this->message_id
;
148 * Implementation of transaction_t.requested.
150 static u_int32_t
requested(private_create_child_sa_t
*this)
152 return this->requested
++;
156 * Implementation of create_child_sa_t.set_policy.
158 static void set_policy(private_create_child_sa_t
*this, policy_t
*policy
)
160 this->policy
= policy
;
164 * Implementation of create_child_sa_t.set_reqid.
166 static void set_reqid(private_create_child_sa_t
*this, u_int32_t reqid
)
172 * Implementation of create_child_sa_t.rekeys_child.
174 static void rekeys_child(private_create_child_sa_t
*this, child_sa_t
*child_sa
)
176 this->rekeyed_sa
= child_sa
;
177 this->failsig
= CHILD_REKEY_FAILED
;
181 * Implementation of create_child_sa_t.cancel.
183 static void cancel(private_create_child_sa_t
*this)
185 this->rekeyed_sa
= NULL
;
190 * Implementation of transaction_t.get_request.
192 static status_t
get_request(private_create_child_sa_t
*this, message_t
**result
)
196 identification_t
*my_id
, *other_id
;
198 /* check if we already have built a message (retransmission) */
201 *result
= this->message
;
205 /* check if we are not already rekeying */
206 if (this->rekeyed_sa
)
208 SIG(CHILD_REKEY_START
, "rekeying CHILD_SA");
210 switch (this->rekeyed_sa
->get_state(this->rekeyed_sa
))
213 SIG(CHILD_REKEY_FAILED
,
214 "rekeying a CHILD_SA which is already rekeying, aborted");
217 SIG(CHILD_REKEY_FAILED
,
218 "rekeying a CHILD_SA which is deleting, aborted");
223 this->rekeyed_sa
->set_state(this->rekeyed_sa
, CHILD_REKEYING
);
227 SIG(CHILD_UP_START
, "creating CHILD_SA");
230 me
= this->ike_sa
->get_my_host(this->ike_sa
);
231 other
= this->ike_sa
->get_other_host(this->ike_sa
);
232 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
233 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
235 /* build the request */
236 request
= message_create();
237 request
->set_source(request
, me
->clone(me
));
238 request
->set_destination(request
, other
->clone(other
));
239 request
->set_exchange_type(request
, CREATE_CHILD_SA
);
240 request
->set_request(request
, TRUE
);
241 request
->set_ike_sa_id(request
, this->ike_sa
->get_id(this->ike_sa
));
243 this->message
= request
;
245 { /* build SA payload */
246 sa_payload_t
*sa_payload
;
247 linked_list_t
*proposals
;
250 /* get a policy, if we are rekeying */
251 if (this->rekeyed_sa
)
253 linked_list_t
*my_ts
, *other_ts
;
254 identification_t
*my_id
, *other_id
;
256 my_ts
= this->rekeyed_sa
->get_my_traffic_selectors(this->rekeyed_sa
);
257 other_ts
= this->rekeyed_sa
->get_other_traffic_selectors(this->rekeyed_sa
);
258 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
259 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
261 this->policy
= charon
->policies
->get_policy(charon
->policies
,
266 this->reqid
= this->rekeyed_sa
->get_reqid(this->rekeyed_sa
);
268 if (this->policy
== NULL
)
270 SIG(IKE_REKEY_FAILED
, "no policy found to rekey "
271 "CHILD_SA with reqid %d", this->reqid
);
276 proposals
= this->policy
->get_proposals(this->policy
);
277 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
278 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
279 this->policy
->get_soft_lifetime(this->policy
),
280 this->policy
->get_hard_lifetime(this->policy
),
281 this->policy
->get_updown(this->policy
),
282 this->policy
->get_hostaccess(this->policy
),
284 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
285 if (this->child_sa
->alloc(this->child_sa
, proposals
) != SUCCESS
)
287 SIG(this->failsig
, "could not install CHILD_SA, CHILD_SA creation failed");
290 sa_payload
= sa_payload_create_from_proposal_list(proposals
);
291 proposals
->destroy_offset(proposals
, offsetof(proposal_t
, destroy
));
292 request
->add_payload(request
, (payload_t
*)sa_payload
);
295 { /* build the NONCE payload for us (initiator) */
296 nonce_payload_t
*nonce_payload
;
298 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
,
299 NONCE_SIZE
, &this->nonce_i
) != SUCCESS
)
301 SIG(this->failsig
, "could not create nonce, CHILD_SA creation failed");
304 nonce_payload
= nonce_payload_create();
305 nonce_payload
->set_nonce(nonce_payload
, this->nonce_i
);
306 request
->add_payload(request
, (payload_t
*)nonce_payload
);
309 { /* build TSi payload */
310 linked_list_t
*ts_list
;
311 ts_payload_t
*ts_payload
;
313 ts_list
= this->policy
->get_my_traffic_selectors(this->policy
, me
);
314 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, ts_list
);
315 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
316 request
->add_payload(request
, (payload_t
*)ts_payload
);
319 { /* build TSr payload */
320 linked_list_t
*ts_list
;
321 ts_payload_t
*ts_payload
;
323 ts_list
= this->policy
->get_other_traffic_selectors(this->policy
, other
);
324 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, ts_list
);
325 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
326 request
->add_payload(request
, (payload_t
*)ts_payload
);
329 if (this->rekeyed_sa
)
330 { /* add REKEY_SA notify if we are rekeying */
331 notify_payload_t
*notify
;
332 protocol_id_t protocol
;
334 protocol
= this->rekeyed_sa
->get_protocol(this->rekeyed_sa
);
335 notify
= notify_payload_create_from_protocol_and_type(protocol
, REKEY_SA
);
336 notify
->set_spi(notify
, this->rekeyed_sa
->get_spi(this->rekeyed_sa
, TRUE
));
337 request
->add_payload(request
, (payload_t
*)notify
);
339 /* register us as rekeying to detect multiple rekeying */
340 this->rekeyed_sa
->set_rekeying_transaction(this->rekeyed_sa
, &this->public);
343 this->message_id
= this->ike_sa
->get_next_message_id(this->ike_sa
);
344 request
->set_message_id(request
, this->message_id
);
350 * Handle all kind of notifys
352 static status_t
process_notifys(private_create_child_sa_t
*this, notify_payload_t
*notify_payload
)
354 notify_type_t notify_type
= notify_payload
->get_notify_type(notify_payload
);
356 DBG2(DBG_IKE
, "process notify type %N", notify_type_names
, notify_type
);
360 case SINGLE_PAIR_REQUIRED
:
362 SIG(this->failsig
, "received a SINGLE_PAIR_REQUIRED notify");
365 case TS_UNACCEPTABLE
:
367 SIG(this->failsig
, "received TS_UNACCEPTABLE notify");
370 case NO_PROPOSAL_CHOSEN
:
372 SIG(this->failsig
, "received NO_PROPOSAL_CHOSEN notify");
378 protocol_id_t protocol
;
380 protocol
= notify_payload
->get_protocol_id(notify_payload
);
385 spi
= notify_payload
->get_spi(notify_payload
);
386 this->rekeyed_sa
= this->ike_sa
->get_child_sa(this->ike_sa
,
389 this->failsig
= CHILD_REKEY_FAILED
;
398 if (notify_type
< 16383)
400 SIG(this->failsig
, "received %N notify error, CHILD_SA "
401 "creation failed", notify_type_names
, notify_type
);
406 DBG1(DBG_IKE
, "received %N notify, ignored",
407 notify_type_names
, notify_type
);
415 * Build a notify message.
417 static void build_notify(notify_type_t type
, chunk_t data
, message_t
*message
, bool flush_message
)
419 notify_payload_t
*notify
;
424 iterator_t
*iterator
= message
->get_payload_iterator(message
);
425 while (iterator
->iterate(iterator
, (void**)&payload
))
427 payload
->destroy(payload
);
428 iterator
->remove(iterator
);
430 iterator
->destroy(iterator
);
433 notify
= notify_payload_create();
434 notify
->set_notify_type(notify
, type
);
435 notify
->set_notification_data(notify
, data
);
436 message
->add_payload(message
, (payload_t
*)notify
);
440 * Install a CHILD_SA for usage
442 static status_t
install_child_sa(private_create_child_sa_t
*this, bool initiator
)
444 prf_plus_t
*prf_plus
;
448 seed
= chunk_alloc(this->nonce_i
.len
+ this->nonce_r
.len
);
449 memcpy(seed
.ptr
, this->nonce_i
.ptr
, this->nonce_i
.len
);
450 memcpy(seed
.ptr
+ this->nonce_i
.len
, this->nonce_r
.ptr
, this->nonce_r
.len
);
451 prf_plus
= prf_plus_create(this->ike_sa
->get_child_prf(this->ike_sa
), seed
);
456 status
= this->child_sa
->update(this->child_sa
, this->proposal
, prf_plus
);
460 status
= this->child_sa
->add(this->child_sa
, this->proposal
, prf_plus
);
462 prf_plus
->destroy(prf_plus
);
463 if (status
!= SUCCESS
)
469 status
= this->child_sa
->add_policies(this->child_sa
, this->tsi
, this->tsr
);
473 status
= this->child_sa
->add_policies(this->child_sa
, this->tsr
, this->tsi
);
475 if (status
!= SUCCESS
)
480 /* add to IKE_SA, and remove from transaction */
481 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
482 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
483 this->child_sa
= NULL
;
488 * Implementation of transaction_t.get_response.
490 static status_t
get_response(private_create_child_sa_t
*this, message_t
*request
,
491 message_t
**result
, transaction_t
**next
)
494 identification_t
*my_id
, *other_id
;
497 iterator_t
*payloads
;
499 sa_payload_t
*sa_request
= NULL
;
500 nonce_payload_t
*nonce_request
= NULL
;
501 ts_payload_t
*tsi_request
= NULL
;
502 ts_payload_t
*tsr_request
= NULL
;
503 nonce_payload_t
*nonce_response
;
505 /* check if we already have built a response (retransmission) */
508 *result
= this->message
;
512 me
= this->ike_sa
->get_my_host(this->ike_sa
);
513 other
= this->ike_sa
->get_other_host(this->ike_sa
);
514 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
515 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
516 this->message_id
= request
->get_message_id(request
);
518 /* set up response */
519 response
= message_create();
520 response
->set_source(response
, me
->clone(me
));
521 response
->set_destination(response
, other
->clone(other
));
522 response
->set_exchange_type(response
, CREATE_CHILD_SA
);
523 response
->set_request(response
, FALSE
);
524 response
->set_message_id(response
, this->message_id
);
525 response
->set_ike_sa_id(response
, this->ike_sa
->get_id(this->ike_sa
));
526 this->message
= response
;
529 /* check message type */
530 if (request
->get_exchange_type(request
) != CREATE_CHILD_SA
)
532 DBG1(DBG_IKE
, "CREATE_CHILD_SA response of invalid type, aborted");
536 /* we do not allow the creation of new CHILDren/rekeying when IKE_SA is
538 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_REKEYING
||
539 this->ike_sa
->get_state(this->ike_sa
) == IKE_DELETING
)
541 build_notify(NO_ADDITIONAL_SAS
, CHUNK_INITIALIZER
, response
, TRUE
);
542 DBG1(DBG_IKE
, "unable to create new CHILD_SAs, as rekeying in progress");
546 /* Iterate over all payloads. */
547 payloads
= request
->get_payload_iterator(request
);
548 while (payloads
->iterate(payloads
, (void**)&payload
))
550 switch (payload
->get_type(payload
))
552 case SECURITY_ASSOCIATION
:
553 sa_request
= (sa_payload_t
*)payload
;
556 nonce_request
= (nonce_payload_t
*)payload
;
558 case TRAFFIC_SELECTOR_INITIATOR
:
559 tsi_request
= (ts_payload_t
*)payload
;
561 case TRAFFIC_SELECTOR_RESPONDER
:
562 tsr_request
= (ts_payload_t
*)payload
;
566 u_int8_t dh_buffer
[] = {0x00, 0x00}; /* MODP_NONE */
567 chunk_t group
= chunk_from_buf(dh_buffer
);
568 build_notify(INVALID_KE_PAYLOAD
, group
, response
, TRUE
);
569 DBG1(DBG_IKE
, "CREATE_CHILD_SA used PFS, sending INVALID_KE_PAYLOAD");
574 status
= process_notifys(this, (notify_payload_t
*)payload
);
575 if (status
!= SUCCESS
)
577 payloads
->destroy(payloads
);
584 DBG1(DBG_IKE
, "ignoring %N payload",
585 payload_type_names
, payload
->get_type(payload
));
590 payloads
->destroy(payloads
);
592 /* after processing the notify payloads, we know if this transaction is
593 * for rekeying or for a new CHILD_SA. We can emit the signals now. */
594 if (this->rekeyed_sa
)
596 SIG(CHILD_REKEY_START
, "rekeying CHILD_SA");
600 SIG(CHILD_UP_START
, "creating CHILD_SA");
603 /* check if we have all payloads */
604 if (!(sa_request
&& nonce_request
&& tsi_request
&& tsr_request
))
606 build_notify(INVALID_SYNTAX
, CHUNK_INITIALIZER
, response
, TRUE
);
607 SIG(this->failsig
, "request message incomplete, no CHILD_SA created");
611 { /* process nonce payload */
612 this->nonce_i
= nonce_request
->get_nonce(nonce_request
);
613 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
,
614 NONCE_SIZE
, &this->nonce_r
) != SUCCESS
)
616 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
617 SIG(this->failsig
, "nonce generation failed, no CHILD_SA created");
620 nonce_response
= nonce_payload_create();
621 nonce_response
->set_nonce(nonce_response
, this->nonce_r
);
624 { /* get a policy and process traffic selectors */
625 identification_t
*my_id
, *other_id
;
626 linked_list_t
*my_ts
, *other_ts
;
628 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
629 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
631 my_ts
= tsr_request
->get_traffic_selectors(tsr_request
);
632 other_ts
= tsi_request
->get_traffic_selectors(tsi_request
);
634 this->policy
= charon
->policies
->get_policy(charon
->policies
,
640 this->tsr
= this->policy
->select_my_traffic_selectors(this->policy
, my_ts
, me
);
641 this->tsi
= this->policy
->select_other_traffic_selectors(this->policy
, other_ts
, other
);
643 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
644 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
646 if (this->policy
== NULL
)
648 SIG(this->failsig
, "no acceptable policy found, sending TS_UNACCEPTABLE notify");
649 build_notify(TS_UNACCEPTABLE
, CHUNK_INITIALIZER
, response
, TRUE
);
654 { /* process SA payload */
655 linked_list_t
*proposal_list
;
656 sa_payload_t
*sa_response
;
657 ts_payload_t
*ts_response
;
659 u_int32_t soft_lifetime
, hard_lifetime
;
661 sa_response
= sa_payload_create();
662 /* get proposals from request, and select one with ours */
663 proposal_list
= sa_request
->get_proposals(sa_request
);
664 DBG2(DBG_IKE
, "selecting proposals:");
665 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
666 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
668 /* do we have a proposal? */
669 if (this->proposal
== NULL
)
671 SIG(this->failsig
, "CHILD_SA proposals unacceptable, sending NO_PROPOSAL_CHOSEN notify");
672 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
675 /* do we have traffic selectors? */
676 else if (this->tsi
->get_count(this->tsi
) == 0 || this->tsr
->get_count(this->tsr
) == 0)
678 SIG(this->failsig
, "CHILD_SA traffic selectors unacceptable, sending TS_UNACCEPTABLE notify");
679 build_notify(TS_UNACCEPTABLE
, CHUNK_INITIALIZER
, response
, TRUE
);
683 { /* create child sa */
684 if (this->rekeyed_sa
)
686 this->reqid
= this->rekeyed_sa
->get_reqid(this->rekeyed_sa
);
688 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
689 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
690 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
691 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
692 soft_lifetime
, hard_lifetime
,
693 this->policy
->get_updown(this->policy
),
694 this->policy
->get_hostaccess(this->policy
),
696 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
697 if (install_child_sa(this, FALSE
) != SUCCESS
)
699 SIG(this->failsig
, "installing CHILD_SA failed, sending NO_PROPOSAL_CHOSEN notify");
700 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
703 /* add proposal to sa payload */
704 sa_response
->add_proposal(sa_response
, this->proposal
);
706 response
->add_payload(response
, (payload_t
*)sa_response
);
708 /* add nonce/ts payload after sa payload */
709 response
->add_payload(response
, (payload_t
*)nonce_response
);
710 ts_response
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
711 response
->add_payload(response
, (payload_t
*)ts_response
);
712 ts_response
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
713 response
->add_payload(response
, (payload_t
*)ts_response
);
715 /* CHILD_SA successfully created. If another transaction is already rekeying
716 * this SA, our lower nonce must be registered for a later nonce compare. */
717 if (this->rekeyed_sa
)
719 private_create_child_sa_t
*other
;
721 other
= this->rekeyed_sa
->get_rekeying_transaction(this->rekeyed_sa
);
724 /* store our lower nonce in the simultaneus transaction, it
725 * will later compare it against his nonces when it calls conclude().
727 if (memcmp(this->nonce_i
.ptr
, this->nonce_r
.ptr
,
728 min(this->nonce_i
.len
, this->nonce_r
.len
)) < 0)
730 other
->nonce_s
= chunk_clone(this->nonce_i
);
734 other
->nonce_s
= chunk_clone(this->nonce_r
);
739 /* we only signal when no other transaction is rekeying */
740 SIG(CHILD_REKEY_SUCCESS
, "CHILD_SA rekeyed");
742 this->rekeyed_sa
->set_state(this->rekeyed_sa
, CHILD_REKEYING
);
746 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
752 * Implementation of transaction_t.conclude
754 static status_t
conclude(private_create_child_sa_t
*this, message_t
*response
,
755 transaction_t
**next
)
757 iterator_t
*payloads
;
760 sa_payload_t
*sa_payload
= NULL
;
761 nonce_payload_t
*nonce_payload
= NULL
;
762 ts_payload_t
*tsi_payload
= NULL
;
763 ts_payload_t
*tsr_payload
= NULL
;
765 child_sa_t
*new_child
= NULL
;
766 delete_child_sa_t
*delete_child_sa
;
768 /* check message type */
769 if (response
->get_exchange_type(response
) != CREATE_CHILD_SA
)
771 SIG(this->failsig
, "CREATE_CHILD_SA response of invalid type, aborting");
775 me
= this->ike_sa
->get_my_host(this->ike_sa
);
776 other
= this->ike_sa
->get_other_host(this->ike_sa
);
778 /* Iterate over all payloads to collect them */
779 payloads
= response
->get_payload_iterator(response
);
780 while (payloads
->iterate(payloads
, (void**)&payload
))
782 switch (payload
->get_type(payload
))
784 case SECURITY_ASSOCIATION
:
785 sa_payload
= (sa_payload_t
*)payload
;
788 nonce_payload
= (nonce_payload_t
*)payload
;
790 case TRAFFIC_SELECTOR_INITIATOR
:
791 tsi_payload
= (ts_payload_t
*)payload
;
793 case TRAFFIC_SELECTOR_RESPONDER
:
794 tsr_payload
= (ts_payload_t
*)payload
;
798 status
= process_notifys(this, (notify_payload_t
*)payload
);
799 if (status
!= SUCCESS
)
801 payloads
->destroy(payloads
);
808 DBG1(DBG_IKE
, "ignoring %N payload",
809 payload_type_names
, payload
->get_type(payload
));
814 payloads
->destroy(payloads
);
816 if (!(sa_payload
&& nonce_payload
&& tsi_payload
&& tsr_payload
))
818 SIG(this->failsig
, "response message incomplete, no CHILD_SA built");
822 { /* process NONCE payload */
823 this->nonce_r
= nonce_payload
->get_nonce(nonce_payload
);
826 { /* process traffic selectors for us */
827 linked_list_t
*ts_received
= tsi_payload
->get_traffic_selectors(tsi_payload
);
828 this->tsi
= this->policy
->select_my_traffic_selectors(this->policy
, ts_received
, me
);
829 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
832 { /* process traffic selectors for other */
833 linked_list_t
*ts_received
= tsr_payload
->get_traffic_selectors(tsr_payload
);
834 this->tsr
= this->policy
->select_other_traffic_selectors(this->policy
, ts_received
, other
);
835 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
838 { /* process sa payload */
839 linked_list_t
*proposal_list
;
841 proposal_list
= sa_payload
->get_proposals(sa_payload
);
842 /* we have to re-check here if other's selection is valid */
843 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
844 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
846 /* everything fine to create CHILD? */
847 if (this->proposal
== NULL
||
848 this->tsi
->get_count(this->tsi
) == 0 ||
849 this->tsr
->get_count(this->tsr
) == 0)
851 SIG(this->failsig
, "CHILD_SA negotiation failed, no CHILD_SA built");
854 new_child
= this->child_sa
;
855 if (install_child_sa(this, TRUE
) != SUCCESS
)
857 SIG(this->failsig
, "installing CHILD_SA failed, no CHILD_SA built");
861 /* CHILD_SA successfully created. If the other peer initiated rekeying
862 * in the meantime, we detect this by comparing the rekeying_transaction
863 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
864 * If no simultaneous rekeying is going on, we just initiate the delete of
865 * the superseded SA. */
866 if (this->rekeyed_sa
)
868 /* rekeying finished, update SA status */
869 this->rekeyed_sa
->set_rekeying_transaction(this->rekeyed_sa
, NULL
);
871 if (this->nonce_s
.ptr
)
872 { /* simlutaneous rekeying is going on, not so good */
875 /* first get our lowest nonce */
876 if (memcmp(this->nonce_i
.ptr
, this->nonce_r
.ptr
,
877 min(this->nonce_i
.len
, this->nonce_r
.len
)) < 0)
879 this_lowest
= this->nonce_i
;
883 this_lowest
= this->nonce_r
;
885 /* then compare against other lowest nonce */
886 if (memcmp(this_lowest
.ptr
, this->nonce_s
.ptr
,
887 min(this_lowest
.len
, this->nonce_s
.len
)) < 0)
889 DBG1(DBG_IKE
, "detected simultaneous CHILD_SA rekeying, deleting ours");
894 DBG1(DBG_IKE
, "detected simultaneous CHILD_SA rekeying, but ours is preferred");
897 /* delete the old SA if we have won the rekeying nonce compare*/
900 delete_child_sa
= delete_child_sa_create(this->ike_sa
);
901 delete_child_sa
->set_child_sa(delete_child_sa
, this->rekeyed_sa
);
902 *next
= (transaction_t
*)delete_child_sa
;
904 /* we send a rekey SUCCESS signal in any case. If the other transaction
905 * detected our transaction, it did not send a signal. We do it for it. */
906 SIG(CHILD_REKEY_SUCCESS
, "CHILD_SA rekeyed");
910 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
914 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
915 delete_child_sa
= delete_child_sa_create(this->ike_sa
);
916 delete_child_sa
->set_child_sa(delete_child_sa
, new_child
);
917 *next
= (transaction_t
*)delete_child_sa
;
923 * implements transaction_t.destroy
925 static void destroy(private_create_child_sa_t
*this)
927 DESTROY_IF(this->message
);
928 DESTROY_IF(this->proposal
);
929 DESTROY_IF(this->child_sa
);
930 DESTROY_IF(this->policy
);
931 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
932 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
933 chunk_free(&this->nonce_i
);
934 chunk_free(&this->nonce_r
);
935 chunk_free(&this->nonce_s
);
936 this->randomizer
->destroy(this->randomizer
);
941 * Described in header.
943 create_child_sa_t
*create_child_sa_create(ike_sa_t
*ike_sa
)
945 private_create_child_sa_t
*this = malloc_thing(private_create_child_sa_t
);
947 /* transaction interface functions */
948 this->public.transaction
.get_request
= (status_t(*)(transaction_t
*,message_t
**))get_request
;
949 this->public.transaction
.get_response
= (status_t(*)(transaction_t
*,message_t
*,message_t
**,transaction_t
**))get_response
;
950 this->public.transaction
.conclude
= (status_t(*)(transaction_t
*,message_t
*,transaction_t
**))conclude
;
951 this->public.transaction
.get_message_id
= (u_int32_t(*)(transaction_t
*))get_message_id
;
952 this->public.transaction
.requested
= (u_int32_t(*)(transaction_t
*))requested
;
953 this->public.transaction
.destroy
= (void(*)(transaction_t
*))destroy
;
955 /* public functions */
956 this->public.set_policy
= (void(*)(create_child_sa_t
*,policy_t
*))set_policy
;
957 this->public.set_reqid
= (void(*)(create_child_sa_t
*,u_int32_t
))set_reqid
;
958 this->public.rekeys_child
= (void(*)(create_child_sa_t
*,child_sa_t
*))rekeys_child
;
959 this->public.cancel
= (void(*)(create_child_sa_t
*))cancel
;
962 this->ike_sa
= ike_sa
;
963 this->message_id
= 0;
964 this->message
= NULL
;
968 this->nonce_i
= CHUNK_INITIALIZER
;
969 this->nonce_r
= CHUNK_INITIALIZER
;
970 this->nonce_s
= CHUNK_INITIALIZER
;
971 this->child_sa
= NULL
;
972 this->rekeyed_sa
= NULL
;
974 this->proposal
= NULL
;
978 this->randomizer
= randomizer_create();
979 this->failsig
= CHILD_UP_FAILED
;
981 return &this->public;