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
,
267 this->reqid
= this->rekeyed_sa
->get_reqid(this->rekeyed_sa
);
269 if (this->policy
== NULL
)
271 SIG(IKE_REKEY_FAILED
, "no policy found to rekey "
272 "CHILD_SA with reqid %d", this->reqid
);
277 proposals
= this->policy
->get_proposals(this->policy
);
278 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
279 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
280 this->policy
->get_soft_lifetime(this->policy
),
281 this->policy
->get_hard_lifetime(this->policy
),
282 this->policy
->get_updown(this->policy
),
283 this->policy
->get_hostaccess(this->policy
),
285 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
286 if (this->child_sa
->alloc(this->child_sa
, proposals
) != SUCCESS
)
288 SIG(this->failsig
, "could not install CHILD_SA, CHILD_SA creation failed");
291 sa_payload
= sa_payload_create_from_proposal_list(proposals
);
292 proposals
->destroy_offset(proposals
, offsetof(proposal_t
, destroy
));
293 request
->add_payload(request
, (payload_t
*)sa_payload
);
296 { /* build the NONCE payload for us (initiator) */
297 nonce_payload_t
*nonce_payload
;
299 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
,
300 NONCE_SIZE
, &this->nonce_i
) != SUCCESS
)
302 SIG(this->failsig
, "could not create nonce, CHILD_SA creation failed");
305 nonce_payload
= nonce_payload_create();
306 nonce_payload
->set_nonce(nonce_payload
, this->nonce_i
);
307 request
->add_payload(request
, (payload_t
*)nonce_payload
);
310 { /* build TSi payload */
311 linked_list_t
*ts_list
;
312 ts_payload_t
*ts_payload
;
314 ts_list
= this->policy
->get_my_traffic_selectors(this->policy
, me
);
315 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, ts_list
);
316 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
317 request
->add_payload(request
, (payload_t
*)ts_payload
);
320 { /* build TSr payload */
321 linked_list_t
*ts_list
;
322 ts_payload_t
*ts_payload
;
324 ts_list
= this->policy
->get_other_traffic_selectors(this->policy
, other
);
325 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, ts_list
);
326 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
327 request
->add_payload(request
, (payload_t
*)ts_payload
);
330 if (this->rekeyed_sa
)
331 { /* add REKEY_SA notify if we are rekeying */
332 notify_payload_t
*notify
;
333 protocol_id_t protocol
;
335 protocol
= this->rekeyed_sa
->get_protocol(this->rekeyed_sa
);
336 notify
= notify_payload_create_from_protocol_and_type(protocol
, REKEY_SA
);
337 notify
->set_spi(notify
, this->rekeyed_sa
->get_spi(this->rekeyed_sa
, TRUE
));
338 request
->add_payload(request
, (payload_t
*)notify
);
340 /* register us as rekeying to detect multiple rekeying */
341 this->rekeyed_sa
->set_rekeying_transaction(this->rekeyed_sa
,
342 &this->public.transaction
);
345 this->message_id
= this->ike_sa
->get_next_message_id(this->ike_sa
);
346 request
->set_message_id(request
, this->message_id
);
352 * Handle all kind of notifys
354 static status_t
process_notifys(private_create_child_sa_t
*this, notify_payload_t
*notify_payload
)
356 notify_type_t notify_type
= notify_payload
->get_notify_type(notify_payload
);
358 DBG2(DBG_IKE
, "process notify type %N", notify_type_names
, notify_type
);
362 case SINGLE_PAIR_REQUIRED
:
364 SIG(this->failsig
, "received a SINGLE_PAIR_REQUIRED notify");
367 case TS_UNACCEPTABLE
:
369 SIG(this->failsig
, "received TS_UNACCEPTABLE notify");
372 case NO_PROPOSAL_CHOSEN
:
374 SIG(this->failsig
, "received NO_PROPOSAL_CHOSEN notify");
380 protocol_id_t protocol
;
382 protocol
= notify_payload
->get_protocol_id(notify_payload
);
387 spi
= notify_payload
->get_spi(notify_payload
);
388 this->rekeyed_sa
= this->ike_sa
->get_child_sa(this->ike_sa
,
391 this->failsig
= CHILD_REKEY_FAILED
;
400 if (notify_type
< 16383)
402 SIG(this->failsig
, "received %N notify error, CHILD_SA "
403 "creation failed", notify_type_names
, notify_type
);
408 DBG1(DBG_IKE
, "received %N notify, ignored",
409 notify_type_names
, notify_type
);
417 * Build a notify message.
419 static void build_notify(notify_type_t type
, chunk_t data
, message_t
*message
, bool flush_message
)
421 notify_payload_t
*notify
;
426 iterator_t
*iterator
= message
->get_payload_iterator(message
);
427 while (iterator
->iterate(iterator
, (void**)&payload
))
429 payload
->destroy(payload
);
430 iterator
->remove(iterator
);
432 iterator
->destroy(iterator
);
435 notify
= notify_payload_create();
436 notify
->set_notify_type(notify
, type
);
437 notify
->set_notification_data(notify
, data
);
438 message
->add_payload(message
, (payload_t
*)notify
);
442 * Install a CHILD_SA for usage
444 static status_t
install_child_sa(private_create_child_sa_t
*this, bool initiator
)
446 prf_plus_t
*prf_plus
;
450 seed
= chunk_alloc(this->nonce_i
.len
+ this->nonce_r
.len
);
451 memcpy(seed
.ptr
, this->nonce_i
.ptr
, this->nonce_i
.len
);
452 memcpy(seed
.ptr
+ this->nonce_i
.len
, this->nonce_r
.ptr
, this->nonce_r
.len
);
453 prf_plus
= prf_plus_create(this->ike_sa
->get_child_prf(this->ike_sa
), seed
);
458 status
= this->child_sa
->update(this->child_sa
, this->proposal
, prf_plus
);
462 status
= this->child_sa
->add(this->child_sa
, this->proposal
, prf_plus
);
464 prf_plus
->destroy(prf_plus
);
465 if (status
!= SUCCESS
)
471 status
= this->child_sa
->add_policies(this->child_sa
, this->tsi
, this->tsr
);
475 status
= this->child_sa
->add_policies(this->child_sa
, this->tsr
, this->tsi
);
477 if (status
!= SUCCESS
)
482 /* add to IKE_SA, and remove from transaction */
483 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
484 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
485 this->child_sa
= NULL
;
490 * Implementation of transaction_t.get_response.
492 static status_t
get_response(private_create_child_sa_t
*this, message_t
*request
,
493 message_t
**result
, transaction_t
**next
)
496 identification_t
*my_id
, *other_id
;
499 iterator_t
*payloads
;
501 sa_payload_t
*sa_request
= NULL
;
502 nonce_payload_t
*nonce_request
= NULL
;
503 ts_payload_t
*tsi_request
= NULL
;
504 ts_payload_t
*tsr_request
= NULL
;
505 nonce_payload_t
*nonce_response
;
507 /* check if we already have built a response (retransmission) */
510 *result
= this->message
;
514 me
= this->ike_sa
->get_my_host(this->ike_sa
);
515 other
= this->ike_sa
->get_other_host(this->ike_sa
);
516 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
517 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
518 this->message_id
= request
->get_message_id(request
);
520 /* set up response */
521 response
= message_create();
522 response
->set_source(response
, me
->clone(me
));
523 response
->set_destination(response
, other
->clone(other
));
524 response
->set_exchange_type(response
, CREATE_CHILD_SA
);
525 response
->set_request(response
, FALSE
);
526 response
->set_message_id(response
, this->message_id
);
527 response
->set_ike_sa_id(response
, this->ike_sa
->get_id(this->ike_sa
));
528 this->message
= response
;
531 /* check message type */
532 if (request
->get_exchange_type(request
) != CREATE_CHILD_SA
)
534 DBG1(DBG_IKE
, "CREATE_CHILD_SA response of invalid type, aborted");
538 /* we do not allow the creation of new CHILDren/rekeying when IKE_SA is
540 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_REKEYING
||
541 this->ike_sa
->get_state(this->ike_sa
) == IKE_DELETING
)
543 build_notify(NO_ADDITIONAL_SAS
, CHUNK_INITIALIZER
, response
, TRUE
);
544 DBG1(DBG_IKE
, "unable to create new CHILD_SAs, as rekeying in progress");
548 /* Iterate over all payloads. */
549 payloads
= request
->get_payload_iterator(request
);
550 while (payloads
->iterate(payloads
, (void**)&payload
))
552 switch (payload
->get_type(payload
))
554 case SECURITY_ASSOCIATION
:
555 sa_request
= (sa_payload_t
*)payload
;
558 nonce_request
= (nonce_payload_t
*)payload
;
560 case TRAFFIC_SELECTOR_INITIATOR
:
561 tsi_request
= (ts_payload_t
*)payload
;
563 case TRAFFIC_SELECTOR_RESPONDER
:
564 tsr_request
= (ts_payload_t
*)payload
;
568 u_int8_t dh_buffer
[] = {0x00, 0x00}; /* MODP_NONE */
569 chunk_t group
= chunk_from_buf(dh_buffer
);
570 build_notify(INVALID_KE_PAYLOAD
, group
, response
, TRUE
);
571 DBG1(DBG_IKE
, "CREATE_CHILD_SA used PFS, sending INVALID_KE_PAYLOAD");
576 status
= process_notifys(this, (notify_payload_t
*)payload
);
577 if (status
!= SUCCESS
)
579 payloads
->destroy(payloads
);
586 DBG1(DBG_IKE
, "ignoring %N payload",
587 payload_type_names
, payload
->get_type(payload
));
592 payloads
->destroy(payloads
);
594 /* after processing the notify payloads, we know if this transaction is
595 * for rekeying or for a new CHILD_SA. We can emit the signals now. */
596 if (this->rekeyed_sa
)
598 SIG(CHILD_REKEY_START
, "rekeying CHILD_SA");
602 SIG(CHILD_UP_START
, "creating CHILD_SA");
605 /* check if we have all payloads */
606 if (!(sa_request
&& nonce_request
&& tsi_request
&& tsr_request
))
608 build_notify(INVALID_SYNTAX
, CHUNK_INITIALIZER
, response
, TRUE
);
609 SIG(this->failsig
, "request message incomplete, no CHILD_SA created");
613 { /* process nonce payload */
614 this->nonce_i
= nonce_request
->get_nonce(nonce_request
);
615 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
,
616 NONCE_SIZE
, &this->nonce_r
) != SUCCESS
)
618 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
619 SIG(this->failsig
, "nonce generation failed, no CHILD_SA created");
622 nonce_response
= nonce_payload_create();
623 nonce_response
->set_nonce(nonce_response
, this->nonce_r
);
626 { /* get a policy and process traffic selectors */
627 identification_t
*my_id
, *other_id
;
628 linked_list_t
*my_ts
, *other_ts
;
630 my_id
= this->ike_sa
->get_my_id(this->ike_sa
);
631 other_id
= this->ike_sa
->get_other_id(this->ike_sa
);
633 my_ts
= tsr_request
->get_traffic_selectors(tsr_request
);
634 other_ts
= tsi_request
->get_traffic_selectors(tsi_request
);
636 this->policy
= charon
->policies
->get_policy(charon
->policies
,
643 this->tsr
= this->policy
->select_my_traffic_selectors(this->policy
, my_ts
, me
);
644 this->tsi
= this->policy
->select_other_traffic_selectors(this->policy
, other_ts
, other
);
646 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
647 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
649 if (this->policy
== NULL
)
651 SIG(this->failsig
, "no acceptable policy found, sending TS_UNACCEPTABLE notify");
652 build_notify(TS_UNACCEPTABLE
, CHUNK_INITIALIZER
, response
, TRUE
);
657 { /* process SA payload */
658 linked_list_t
*proposal_list
;
659 sa_payload_t
*sa_response
;
660 ts_payload_t
*ts_response
;
662 u_int32_t soft_lifetime
, hard_lifetime
;
664 sa_response
= sa_payload_create();
665 /* get proposals from request, and select one with ours */
666 proposal_list
= sa_request
->get_proposals(sa_request
);
667 DBG2(DBG_IKE
, "selecting proposals:");
668 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
669 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
671 /* do we have a proposal? */
672 if (this->proposal
== NULL
)
674 SIG(this->failsig
, "CHILD_SA proposals unacceptable, sending NO_PROPOSAL_CHOSEN notify");
675 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
678 /* do we have traffic selectors? */
679 else if (this->tsi
->get_count(this->tsi
) == 0 || this->tsr
->get_count(this->tsr
) == 0)
681 SIG(this->failsig
, "CHILD_SA traffic selectors unacceptable, sending TS_UNACCEPTABLE notify");
682 build_notify(TS_UNACCEPTABLE
, CHUNK_INITIALIZER
, response
, TRUE
);
686 { /* create child sa */
687 if (this->rekeyed_sa
)
689 this->reqid
= this->rekeyed_sa
->get_reqid(this->rekeyed_sa
);
691 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
692 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
693 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
694 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
695 soft_lifetime
, hard_lifetime
,
696 this->policy
->get_updown(this->policy
),
697 this->policy
->get_hostaccess(this->policy
),
699 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
700 if (install_child_sa(this, FALSE
) != SUCCESS
)
702 SIG(this->failsig
, "installing CHILD_SA failed, sending NO_PROPOSAL_CHOSEN notify");
703 build_notify(NO_PROPOSAL_CHOSEN
, CHUNK_INITIALIZER
, response
, TRUE
);
706 /* add proposal to sa payload */
707 sa_response
->add_proposal(sa_response
, this->proposal
);
709 response
->add_payload(response
, (payload_t
*)sa_response
);
711 /* add nonce/ts payload after sa payload */
712 response
->add_payload(response
, (payload_t
*)nonce_response
);
713 ts_response
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
714 response
->add_payload(response
, (payload_t
*)ts_response
);
715 ts_response
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
716 response
->add_payload(response
, (payload_t
*)ts_response
);
718 /* CHILD_SA successfully created. If another transaction is already rekeying
719 * this SA, our lower nonce must be registered for a later nonce compare. */
720 if (this->rekeyed_sa
)
722 private_create_child_sa_t
*other
;
724 other
= (private_create_child_sa_t
*)
725 this->rekeyed_sa
->get_rekeying_transaction(this->rekeyed_sa
);
728 /* store our lower nonce in the simultaneus transaction, it
729 * will later compare it against his nonces when it calls conclude().
731 if (memcmp(this->nonce_i
.ptr
, this->nonce_r
.ptr
,
732 min(this->nonce_i
.len
, this->nonce_r
.len
)) < 0)
734 other
->nonce_s
= chunk_clone(this->nonce_i
);
738 other
->nonce_s
= chunk_clone(this->nonce_r
);
743 /* we only signal when no other transaction is rekeying */
744 SIG(CHILD_REKEY_SUCCESS
, "CHILD_SA rekeyed");
746 this->rekeyed_sa
->set_state(this->rekeyed_sa
, CHILD_REKEYING
);
750 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
756 * Implementation of transaction_t.conclude
758 static status_t
conclude(private_create_child_sa_t
*this, message_t
*response
,
759 transaction_t
**next
)
761 iterator_t
*payloads
;
764 sa_payload_t
*sa_payload
= NULL
;
765 nonce_payload_t
*nonce_payload
= NULL
;
766 ts_payload_t
*tsi_payload
= NULL
;
767 ts_payload_t
*tsr_payload
= NULL
;
769 child_sa_t
*new_child
= NULL
;
770 delete_child_sa_t
*delete_child_sa
;
772 /* check message type */
773 if (response
->get_exchange_type(response
) != CREATE_CHILD_SA
)
775 SIG(this->failsig
, "CREATE_CHILD_SA response of invalid type, aborting");
779 me
= this->ike_sa
->get_my_host(this->ike_sa
);
780 other
= this->ike_sa
->get_other_host(this->ike_sa
);
782 /* Iterate over all payloads to collect them */
783 payloads
= response
->get_payload_iterator(response
);
784 while (payloads
->iterate(payloads
, (void**)&payload
))
786 switch (payload
->get_type(payload
))
788 case SECURITY_ASSOCIATION
:
789 sa_payload
= (sa_payload_t
*)payload
;
792 nonce_payload
= (nonce_payload_t
*)payload
;
794 case TRAFFIC_SELECTOR_INITIATOR
:
795 tsi_payload
= (ts_payload_t
*)payload
;
797 case TRAFFIC_SELECTOR_RESPONDER
:
798 tsr_payload
= (ts_payload_t
*)payload
;
802 status
= process_notifys(this, (notify_payload_t
*)payload
);
803 if (status
!= SUCCESS
)
805 payloads
->destroy(payloads
);
812 DBG1(DBG_IKE
, "ignoring %N payload",
813 payload_type_names
, payload
->get_type(payload
));
818 payloads
->destroy(payloads
);
820 if (!(sa_payload
&& nonce_payload
&& tsi_payload
&& tsr_payload
))
822 SIG(this->failsig
, "response message incomplete, no CHILD_SA built");
826 { /* process NONCE payload */
827 this->nonce_r
= nonce_payload
->get_nonce(nonce_payload
);
830 { /* process traffic selectors for us */
831 linked_list_t
*ts_received
= tsi_payload
->get_traffic_selectors(tsi_payload
);
832 this->tsi
= this->policy
->select_my_traffic_selectors(this->policy
, ts_received
, me
);
833 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
836 { /* process traffic selectors for other */
837 linked_list_t
*ts_received
= tsr_payload
->get_traffic_selectors(tsr_payload
);
838 this->tsr
= this->policy
->select_other_traffic_selectors(this->policy
, ts_received
, other
);
839 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
842 { /* process sa payload */
843 linked_list_t
*proposal_list
;
845 proposal_list
= sa_payload
->get_proposals(sa_payload
);
846 /* we have to re-check here if other's selection is valid */
847 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
848 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
850 /* everything fine to create CHILD? */
851 if (this->proposal
== NULL
||
852 this->tsi
->get_count(this->tsi
) == 0 ||
853 this->tsr
->get_count(this->tsr
) == 0)
855 SIG(this->failsig
, "CHILD_SA negotiation failed, no CHILD_SA built");
858 new_child
= this->child_sa
;
859 if (install_child_sa(this, TRUE
) != SUCCESS
)
861 SIG(this->failsig
, "installing CHILD_SA failed, no CHILD_SA built");
865 /* CHILD_SA successfully created. If the other peer initiated rekeying
866 * in the meantime, we detect this by comparing the rekeying_transaction
867 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
868 * If no simultaneous rekeying is going on, we just initiate the delete of
869 * the superseded SA. */
870 if (this->rekeyed_sa
)
872 /* rekeying finished, update SA status */
873 this->rekeyed_sa
->set_rekeying_transaction(this->rekeyed_sa
, NULL
);
875 if (this->nonce_s
.ptr
)
876 { /* simlutaneous rekeying is going on, not so good */
879 /* first get our lowest nonce */
880 if (memcmp(this->nonce_i
.ptr
, this->nonce_r
.ptr
,
881 min(this->nonce_i
.len
, this->nonce_r
.len
)) < 0)
883 this_lowest
= this->nonce_i
;
887 this_lowest
= this->nonce_r
;
889 /* then compare against other lowest nonce */
890 if (memcmp(this_lowest
.ptr
, this->nonce_s
.ptr
,
891 min(this_lowest
.len
, this->nonce_s
.len
)) < 0)
893 DBG1(DBG_IKE
, "detected simultaneous CHILD_SA rekeying, deleting ours");
898 DBG1(DBG_IKE
, "detected simultaneous CHILD_SA rekeying, but ours is preferred");
901 /* delete the old SA if we have won the rekeying nonce compare*/
904 delete_child_sa
= delete_child_sa_create(this->ike_sa
);
905 delete_child_sa
->set_child_sa(delete_child_sa
, this->rekeyed_sa
);
906 *next
= (transaction_t
*)delete_child_sa
;
908 /* we send a rekey SUCCESS signal in any case. If the other transaction
909 * detected our transaction, it did not send a signal. We do it for it. */
910 SIG(CHILD_REKEY_SUCCESS
, "CHILD_SA rekeyed");
914 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
918 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
919 delete_child_sa
= delete_child_sa_create(this->ike_sa
);
920 delete_child_sa
->set_child_sa(delete_child_sa
, new_child
);
921 *next
= (transaction_t
*)delete_child_sa
;
927 * implements transaction_t.destroy
929 static void destroy(private_create_child_sa_t
*this)
931 DESTROY_IF(this->message
);
932 DESTROY_IF(this->proposal
);
933 DESTROY_IF(this->child_sa
);
934 DESTROY_IF(this->policy
);
937 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
941 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
943 chunk_free(&this->nonce_i
);
944 chunk_free(&this->nonce_r
);
945 chunk_free(&this->nonce_s
);
946 this->randomizer
->destroy(this->randomizer
);
951 * Described in header.
953 create_child_sa_t
*create_child_sa_create(ike_sa_t
*ike_sa
)
955 private_create_child_sa_t
*this = malloc_thing(private_create_child_sa_t
);
957 /* transaction interface functions */
958 this->public.transaction
.get_request
= (status_t(*)(transaction_t
*,message_t
**))get_request
;
959 this->public.transaction
.get_response
= (status_t(*)(transaction_t
*,message_t
*,message_t
**,transaction_t
**))get_response
;
960 this->public.transaction
.conclude
= (status_t(*)(transaction_t
*,message_t
*,transaction_t
**))conclude
;
961 this->public.transaction
.get_message_id
= (u_int32_t(*)(transaction_t
*))get_message_id
;
962 this->public.transaction
.requested
= (u_int32_t(*)(transaction_t
*))requested
;
963 this->public.transaction
.destroy
= (void(*)(transaction_t
*))destroy
;
965 /* public functions */
966 this->public.set_policy
= (void(*)(create_child_sa_t
*,policy_t
*))set_policy
;
967 this->public.set_reqid
= (void(*)(create_child_sa_t
*,u_int32_t
))set_reqid
;
968 this->public.rekeys_child
= (void(*)(create_child_sa_t
*,child_sa_t
*))rekeys_child
;
969 this->public.cancel
= (void(*)(create_child_sa_t
*))cancel
;
972 this->ike_sa
= ike_sa
;
973 this->message_id
= 0;
974 this->message
= NULL
;
978 this->nonce_i
= CHUNK_INITIALIZER
;
979 this->nonce_r
= CHUNK_INITIALIZER
;
980 this->nonce_s
= CHUNK_INITIALIZER
;
981 this->child_sa
= NULL
;
982 this->rekeyed_sa
= NULL
;
984 this->proposal
= NULL
;
988 this->randomizer
= randomizer_create();
989 this->failsig
= CHILD_UP_FAILED
;
991 return &this->public;