4 * @brief Implementation of ike_sa_t.
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
33 #include <definitions.h>
34 #include <utils/linked_list.h>
35 #include <crypto/diffie_hellman.h>
36 #include <crypto/prf_plus.h>
37 #include <crypto/crypters/crypter.h>
38 #include <crypto/hashers/hasher.h>
39 #include <encoding/payloads/sa_payload.h>
40 #include <encoding/payloads/nonce_payload.h>
41 #include <encoding/payloads/ke_payload.h>
42 #include <encoding/payloads/delete_payload.h>
43 #include <encoding/payloads/transform_substructure.h>
44 #include <encoding/payloads/transform_attribute.h>
45 #include <encoding/payloads/ts_payload.h>
46 #include <sa/transactions/transaction.h>
47 #include <sa/transactions/ike_sa_init.h>
48 #include <sa/transactions/delete_ike_sa.h>
49 #include <sa/transactions/create_child_sa.h>
50 #include <sa/transactions/delete_child_sa.h>
51 #include <sa/transactions/dead_peer_detection.h>
52 #include <sa/transactions/rekey_ike_sa.h>
53 #include <queues/jobs/retransmit_request_job.h>
54 #include <queues/jobs/delete_ike_sa_job.h>
55 #include <queues/jobs/send_dpd_job.h>
56 #include <queues/jobs/send_keepalive_job.h>
57 #include <queues/jobs/rekey_ike_sa_job.h>
58 #include <queues/jobs/route_job.h>
59 #include <queues/jobs/initiate_job.h>
61 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DELETING
,
69 typedef struct private_ike_sa_t private_ike_sa_t
;
72 * Private data of an ike_sa_t object.
74 struct private_ike_sa_t
{
82 * Identifier for the current IKE_SA.
84 ike_sa_id_t
*ike_sa_id
;
87 * Current state of the IKE_SA
92 * Name of the connection used by this IKE_SA
97 * Address of local host
102 * Address of remote host
107 * Identification used for us
109 identification_t
*my_id
;
112 * Identification used for other
114 identification_t
*other_id
;
117 * Linked List containing the child sa's of the current IKE_SA.
119 linked_list_t
*child_sas
;
122 * crypter for inbound traffic
124 crypter_t
*crypter_in
;
127 * crypter for outbound traffic
129 crypter_t
*crypter_out
;
132 * Signer for inbound traffic
137 * Signer for outbound traffic
139 signer_t
*signer_out
;
142 * Multi purpose prf, set key, use it, forget it
147 * Prf function for derivating keymat child SAs
152 * PRF, with key set to pi_key, used for authentication
157 * PRF, with key set to pr_key, used for authentication
164 hasher_t
*nat_hasher
;
167 * NAT status of local host.
172 * NAT status of remote host.
177 * message ID for next outgoung request
179 u_int32_t message_id_out
;
182 * Timestamps for this IKE_SA
185 /** last IKE message received */
187 /** last IKE message sent */
189 /** when IKE_SA became established */
190 u_int32_t established
;
191 /** when IKE_SA gets rekeyed */
193 /** when IKE_SA gets deleted */
198 * interval to send DPD liveness check
203 * number of retransmit sequences to go through before giving up (keyingtries)
205 u_int32_t retrans_sequences
;
208 * List of queued transactions to process
210 linked_list_t
*transaction_queue
;
213 * Transaction currently initiated
214 * (only one supported yet, window size = 1)
216 transaction_t
*transaction_out
;
219 * last transaction initiated by peer processed.
220 * (only one supported yet, window size = 1)
221 * Stored for retransmission.
223 transaction_t
*transaction_in
;
226 * Next incoming transaction expected. Used to
227 * do multi transaction operations.
229 transaction_t
*transaction_in_next
;
232 * Transaction which rekeys this IKE_SA, used do detect simultaneus rekeying
234 rekey_ike_sa_t
*rekeying_transaction
;
238 * get the time of the latest traffic processed by the kernel
240 static time_t get_kernel_time(private_ike_sa_t
* this, bool inbound
)
242 iterator_t
*iterator
;
243 child_sa_t
*child_sa
;
244 time_t latest
= 0, use_time
;
246 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
247 while (iterator
->iterate(iterator
, (void**)&child_sa
))
249 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
251 latest
= max(latest
, use_time
);
254 iterator
->destroy(iterator
);
260 * get the time of the latest received traffice
262 static time_t get_time_inbound(private_ike_sa_t
*this)
264 return max(this->time
.inbound
, get_kernel_time(this, TRUE
));
268 * get the time of the latest sent traffic
270 static time_t get_time_outbound(private_ike_sa_t
*this)
272 return max(this->time
.outbound
, get_kernel_time(this, FALSE
));
276 * Implementation of ike_sa_t.get_name.
278 static char *get_name(private_ike_sa_t
*this)
284 * Implementation of ike_sa_t.set_name.
286 static void set_name(private_ike_sa_t
*this, char* name
)
289 this->name
= strdup(name
);
293 * Implementation of ike_sa_t.apply_connection.
295 static void apply_connection(private_ike_sa_t
*this, connection_t
*connection
)
297 this->dpd_delay
= connection
->get_dpd_delay(connection
);
298 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
302 * Implementation of ike_sa_t.get_my_host.
304 static host_t
*get_my_host(private_ike_sa_t
*this)
306 return this->my_host
;
310 * Implementation of ike_sa_t.set_my_host.
312 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
314 DESTROY_IF(this->my_host
);
319 * Implementation of ike_sa_t.get_other_host.
321 static host_t
*get_other_host(private_ike_sa_t
*this)
323 return this->other_host
;
327 * Implementation of ike_sa_t.set_other_host.
329 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
331 DESTROY_IF(this->other_host
);
332 this->other_host
= other
;
336 * Update connection host, as addresses may change (NAT)
338 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
343 * 2.11. Address and Port Agility
345 * IKE runs over UDP ports 500 and 4500, and implicitly sets up ESP and
346 * AH associations for the same IP addresses it runs over. The IP
347 * addresses and ports in the outer header are, however, not themselves
348 * cryptographically protected, and IKE is designed to work even through
349 * Network Address Translation (NAT) boxes. An implementation MUST
350 * accept incoming requests even if the source port is not 500 or 4500,
351 * and MUST respond to the address and port from which the request was
352 * received. It MUST specify the address and port at which the request
353 * was received as the source address and port in the response. IKE
354 * functions identically over IPv4 or IPv6.
358 * There are cases where a NAT box decides to remove mappings that
359 * are still alive (for example, the keepalive interval is too long,
360 * or the NAT box is rebooted). To recover in these cases, hosts
361 * that are not behind a NAT SHOULD send all packets (including
362 * retransmission packets) to the IP address and port from the last
363 * valid authenticated packet from the other end (i.e., dynamically
364 * update the address). A host behind a NAT SHOULD NOT do this
365 * because it opens a DoS attack possibility. Any authenticated IKE
366 * packet or any authenticated UDP-encapsulated ESP packet can be
367 * used to detect that the IP address or the port has changed.
369 iterator_t
*iterator
= NULL
;
370 child_sa_t
*child_sa
= NULL
;
371 host_diff_t my_diff
, other_diff
;
373 if (this->my_host
->is_anyaddr(this->my_host
) ||
374 this->other_host
->is_anyaddr(this->other_host
))
376 /* on first received message */
377 this->my_host
->destroy(this->my_host
);
378 this->my_host
= me
->clone(me
);
379 this->other_host
->destroy(this->other_host
);
380 this->other_host
= other
->clone(other
);
384 my_diff
= me
->get_differences(me
, this->my_host
);
385 other_diff
= other
->get_differences(other
, this->other_host
);
387 if (!my_diff
&& !other_diff
)
394 this->my_host
->destroy(this->my_host
);
395 this->my_host
= me
->clone(me
);
400 /* update without restrictions if we are not NATted */
403 this->other_host
->destroy(this->other_host
);
404 this->other_host
= other
->clone(other
);
409 /* if we are natted, only port may change */
410 if (other_diff
& HOST_DIFF_ADDR
)
414 else if (other_diff
& HOST_DIFF_PORT
)
416 this->other_host
->set_port(this->other_host
, other
->get_port(other
));
419 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
420 while (iterator
->iterate(iterator
, (void**)&child_sa
))
422 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
423 my_diff
, other_diff
);
424 /* TODO: what to do if update fails? Delete CHILD_SA? */
426 iterator
->destroy(iterator
);
430 * called when the peer is not responding anymore
432 static void dpd_detected(private_ike_sa_t
*this)
434 /* check for childrens with dpdaction=hold */
435 connection_t
*connection
= NULL
;
437 linked_list_t
*my_ts
, *other_ts
;
438 child_sa_t
* child_sa
;
442 DBG2(SIG_DBG_IKE
, "dead peer detected, handling CHILD_SAs dpd action");
444 while(this->child_sas
->remove_first(this->child_sas
,
445 (void**)&child_sa
) == SUCCESS
)
447 /* get the policy which belongs to this CHILD */
448 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
449 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
450 policy
= charon
->policies
->get_policy(charon
->policies
,
451 this->my_id
, this->other_id
,
453 this->my_host
, this->other_host
);
456 SIG(SIG_CHILD_FAILED
, "no policy for CHILD to handle DPD");
460 action
= policy
->get_dpd_action(policy
);
461 /* get a connection for further actions */
462 if (connection
== NULL
&&
463 (action
== DPD_ROUTE
|| action
== DPD_RESTART
))
465 connection
= charon
->connections
->get_connection_by_hosts(
467 this->my_host
, this->other_host
);
468 if (connection
== NULL
)
470 SIG(SIG_IKE_FAILED
, "no connection found to handle DPD");
475 DBG1(SIG_DBG_IKE
, "dpd action for %s is %N",
476 policy
->get_name(policy
), dpd_action_names
, action
);
481 connection
->get_ref(connection
);
482 job
= (job_t
*)route_job_create(connection
, policy
, TRUE
);
483 charon
->job_queue
->add(charon
->job_queue
, job
);
486 connection
->get_ref(connection
);
487 job
= (job_t
*)initiate_job_create(connection
, policy
);
488 charon
->job_queue
->add(charon
->job_queue
, job
);
491 policy
->destroy(policy
);
494 child_sa
->destroy(child_sa
);
496 DESTROY_IF(connection
);
500 * send a request and schedule retransmission
502 static status_t
transmit_request(private_ike_sa_t
*this)
507 retransmit_request_job_t
*job
;
508 u_int32_t transmitted
;
510 transaction_t
*transaction
= this->transaction_out
;
511 u_int32_t message_id
;
513 transmitted
= transaction
->requested(transaction
);
514 timeout
= charon
->configuration
->get_retransmit_timeout(charon
->configuration
,
516 this->retrans_sequences
);
519 SIG(SIG_IKE_FAILED
, "giving up after %d retransmits, deleting IKE_SA",
525 status
= transaction
->get_request(transaction
, &request
);
526 if (status
!= SUCCESS
)
528 /* generating request failed */
531 message_id
= transaction
->get_message_id(transaction
);
532 /* if we retransmit, the request is already generated */
533 if (transmitted
== 0)
535 status
= request
->generate(request
, this->crypter_out
, this->signer_out
, &packet
);
536 if (status
!= SUCCESS
)
538 DBG1(SIG_DBG_IKE
, "request generation failed. transaction discarded");
544 DBG1(SIG_DBG_IKE
, "sending retransmit %d for %N request with messageID %d",
545 transmitted
, exchange_type_names
, request
->get_exchange_type(request
),
547 packet
= request
->get_packet(request
);
550 charon
->send_queue
->add(charon
->send_queue
, packet
);
551 this->time
.outbound
= time(NULL
);
553 /* schedule retransmission job */
554 job
= retransmit_request_job_create(message_id
, this->ike_sa_id
);
555 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
, timeout
);
560 * Implementation of ike_sa.retransmit_request.
562 static status_t
retransmit_request(private_ike_sa_t
*this, u_int32_t message_id
)
564 if (this->transaction_out
== NULL
||
565 this->transaction_out
->get_message_id(this->transaction_out
) != message_id
)
567 /* no retransmit necessary, transaction did already complete */
570 return transmit_request(this);
574 * Check for transactions in the queue and initiate the first transaction found.
576 static status_t
process_transaction_queue(private_ike_sa_t
*this)
578 if (this->transaction_out
)
580 /* already a transaction in progress */
586 if (this->transaction_queue
->remove_first(this->transaction_queue
,
587 (void**)&this->transaction_out
) != SUCCESS
)
589 /* transaction queue empty */
592 switch (transmit_request(this))
597 /* critical, IKE_SA unusable, destroy immediately */
600 /* discard transaction, process next one */
601 this->transaction_out
->destroy(this->transaction_out
);
602 this->transaction_out
= NULL
;
603 /* handle next transaction */
610 * Queue a new transaction and execute the next outstanding transaction
612 static status_t
queue_transaction(private_ike_sa_t
*this, transaction_t
*transaction
, bool prefer
)
614 /* inject next transaction */
619 this->transaction_queue
->insert_first(this->transaction_queue
, transaction
);
623 this->transaction_queue
->insert_last(this->transaction_queue
, transaction
);
626 /* process a transaction */
627 return process_transaction_queue(this);
631 * process an incoming request.
633 static status_t
process_request(private_ike_sa_t
*this, message_t
*request
)
635 transaction_t
*last
, *current
= NULL
;
638 u_int32_t request_mid
;
641 request_mid
= request
->get_message_id(request
);
642 last
= this->transaction_in
;
644 /* check if message ID is correct */
647 u_int32_t last_mid
= last
->get_message_id(last
);
649 if (last_mid
== request_mid
)
651 /* retransmit detected */
652 DBG1(SIG_DBG_IKE
, "received retransmitted request for message "
653 "ID %d, retransmitting response", request_mid
);
654 last
->get_response(last
, request
, &response
, &this->transaction_in_next
);
655 packet
= response
->get_packet(response
);
656 charon
->send_queue
->add(charon
->send_queue
, packet
);
657 this->time
.outbound
= time(NULL
);
661 if (last_mid
> request_mid
)
663 /* something seriously wrong here, message id may not decrease */
664 DBG1(SIG_DBG_IKE
, "received request with message ID %d, "
665 "excepted %d, ingored", request_mid
, last_mid
+ 1);
668 /* we allow jumps in message IDs, as long as they are incremental */
669 if (last_mid
+ 1 < request_mid
)
671 DBG1(SIG_DBG_IKE
, "received request with message ID %d, excepted %d",
672 request_mid
, last_mid
+ 1);
677 if (request_mid
!= 0)
679 /* warn, but allow it */
680 DBG1(SIG_DBG_IKE
, "first received request has message ID %d, "
681 "excepted 0", request_mid
);
685 /* check if we already have a pre-created transaction for this request */
686 if (this->transaction_in_next
)
688 current
= this->transaction_in_next
;
689 this->transaction_in_next
= NULL
;
693 current
= transaction_create(&this->public, request
);
696 DBG1(SIG_DBG_IKE
, "no idea how to handle received message (exchange"
697 " type %d), ignored", request
->get_exchange_type(request
));
702 /* send message. get_request() always gives a valid response */
703 status
= current
->get_response(current
, request
, &response
, &this->transaction_in_next
);
704 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
706 DBG1(SIG_DBG_IKE
, "response generation failed, discarding transaction");
707 current
->destroy(current
);
711 charon
->send_queue
->add(charon
->send_queue
, packet
);
712 this->time
.outbound
= time(NULL
);
713 /* act depending on transaction result */
717 /* transactions says we should destroy the IKE_SA, so do it */
718 current
->destroy(current
);
721 /* store for retransmission, destroy old transaction */
722 this->transaction_in
= current
;
732 * process an incoming response
734 static status_t
process_response(private_ike_sa_t
*this, message_t
*response
)
736 transaction_t
*current
, *new = NULL
;
738 current
= this->transaction_out
;
739 /* check if message ID is that of our currently active transaction */
740 if (current
== NULL
||
741 current
->get_message_id(current
) != response
->get_message_id(response
))
743 DBG1(SIG_DBG_IKE
, "received response with message ID %d "
744 "not requested, ignored", response
->get_message_id(response
));
748 switch (current
->conclude(current
, response
, &new))
751 /* state requested to destroy IKE_SA */
754 /* discard transaction, process next one */
757 /* transaction comleted, remove */
758 current
->destroy(current
);
759 this->transaction_out
= NULL
;
761 /* queue new transaction */
762 return queue_transaction(this, new, TRUE
);
766 * send a notify back to the sender
768 static void send_notify_response(private_ike_sa_t
*this,
772 notify_payload_t
*notify
;
777 response
= message_create();
778 dst
= request
->get_source(request
);
779 src
= request
->get_destination(request
);
780 response
->set_source(response
, src
->clone(src
));
781 response
->set_destination(response
, dst
->clone(dst
));
782 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
783 response
->set_request(response
, FALSE
);
784 response
->set_message_id(response
, request
->get_message_id(request
));
785 response
->set_ike_sa_id(response
, this->ike_sa_id
);
786 notify
= notify_payload_create_from_protocol_and_type(PROTO_NONE
, type
);
787 response
->add_payload(response
, (payload_t
*)notify
);
788 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
790 response
->destroy(response
);
793 charon
->send_queue
->add(charon
->send_queue
, packet
);
794 this->time
.outbound
= time(NULL
);
795 response
->destroy(response
);
801 * Implementation of ike_sa_t.process_message.
803 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
808 is_request
= message
->get_request(message
);
810 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
811 if (status
!= SUCCESS
)
819 DBG1(SIG_DBG_IKE
, "ciritcal unknown payloads found");
822 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
826 DBG1(SIG_DBG_IKE
, "message parsing failed");
829 send_notify_response(this, message
, INVALID_SYNTAX
);
833 DBG1(SIG_DBG_IKE
, "message verification failed");
836 send_notify_response(this, message
, INVALID_SYNTAX
);
840 DBG1(SIG_DBG_IKE
, "integrity check failed");
844 DBG1(SIG_DBG_IKE
, "found encrypted message, but no keys available");
847 send_notify_response(this, message
, INVALID_SYNTAX
);
853 DBG1(SIG_DBG_IKE
, "%N %s with message ID %d processing failed",
854 exchange_type_names
, message
->get_exchange_type(message
),
855 message
->get_request(message
) ?
"request" : "response",
856 message
->get_message_id(message
));
860 /* check if message is trustworthy, and update connection information */
861 if (this->state
== IKE_CREATED
||
862 message
->get_exchange_type(message
) != IKE_SA_INIT
)
864 update_hosts(this, message
->get_destination(message
),
865 message
->get_source(message
));
866 this->time
.inbound
= time(NULL
);
870 status
= process_request(this, message
);
874 status
= process_response(this, message
);
881 * Implementation of ike_sa_t.initiate.
883 static status_t
initiate(private_ike_sa_t
*this,
884 connection_t
*connection
, policy_t
*policy
)
890 /* in state CREATED, we must do the ike_sa_init
891 * and ike_auth transactions. Along with these,
892 * a CHILD_SA with the supplied policy is set up.
894 ike_sa_init_t
*ike_sa_init
;
896 SIG(SIG_INITIATE
, "initiating new IKE_SA for CHILD_SA");
897 DESTROY_IF(this->my_host
);
898 this->my_host
= connection
->get_my_host(connection
);
899 this->my_host
= this->my_host
->clone(this->my_host
);
900 DESTROY_IF(this->other_host
);
901 this->other_host
= connection
->get_other_host(connection
);
902 this->other_host
= this->other_host
->clone(this->other_host
);
903 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
904 this->dpd_delay
= connection
->get_dpd_delay(connection
);
906 if (this->other_host
->is_anyaddr(this->other_host
))
909 "can not initiate a connection to %%any, aborting");
910 SIG(SIG_CHILD_FAILED
,
911 "unable to create an IKE_SA to instantiate policy");
912 policy
->destroy(policy
);
913 connection
->destroy(connection
);
917 this->message_id_out
= 1;
918 ike_sa_init
= ike_sa_init_create(&this->public);
919 ike_sa_init
->set_config(ike_sa_init
, connection
, policy
);
920 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
925 /* if we are in DELETING/REKEYING, we deny set up of a policy.
926 * TODO: would it make sense to queue the transaction and adopt
927 * it all transactions to the new IKE_SA? */
928 SIG(SIG_CHILD_FAILED
,
929 "creating CHILD_SA discarded, as IKE_SA is in state %N",
930 ike_sa_state_names
, this->state
);
931 policy
->destroy(policy
);
932 connection
->destroy(connection
);
936 case IKE_ESTABLISHED
:
938 /* if we are ESTABLISHED or CONNECTING, we queue the
939 * transaction to create the CHILD_SA. It gets processed
940 * when the IKE_SA is ready to do so. We don't need the
941 * connection, as the IKE_SA is already established/establishing.
943 create_child_sa_t
*create_child
;
945 SIG(SIG_INITIATE
, "creating CHILD_SA in existing IKE_SA");
946 connection
->destroy(connection
);
947 create_child
= create_child_sa_create(&this->public);
948 create_child
->set_policy(create_child
, policy
);
949 return queue_transaction(this, (transaction_t
*)create_child
, FALSE
);
956 * Implementation of ike_sa_t.acquire.
958 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
960 connection_t
*connection
;
962 iterator_t
*iterator
;
963 child_sa_t
*current
, *child_sa
= NULL
;
964 linked_list_t
*my_ts
, *other_ts
;
966 if (this->state
== IKE_DELETING
)
968 SIG(SIG_CHILD_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
969 "IKE_SA is deleting", reqid
);
974 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
975 while (iterator
->iterate(iterator
, (void**)¤t
))
977 if (current
->get_reqid(current
) == reqid
)
983 iterator
->destroy(iterator
);
986 SIG(SIG_CHILD_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
987 "CHILD_SA not found", reqid
);
990 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
991 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
993 policy
= charon
->policies
->get_policy(charon
->policies
,
994 this->my_id
, this->other_id
,
996 this->my_host
, this->other_host
);
999 SIG(SIG_CHILD_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1000 "no policy found", reqid
);
1004 switch (this->state
)
1008 ike_sa_init_t
*ike_sa_init
;
1011 "acquiring CHILD_SA with reqid %d, IKE_SA setup needed", reqid
);
1013 connection
= charon
->connections
->get_connection_by_hosts(
1014 charon
->connections
, this->my_host
, this->other_host
);
1016 if (connection
== NULL
)
1018 SIG(SIG_CHILD_FAILED
, "acquiring CHILD_SA "
1019 "(reqid %d) failed: no connection found for IKE_SA", reqid
);
1020 policy
->destroy(policy
);
1024 this->message_id_out
= 1;
1025 ike_sa_init
= ike_sa_init_create(&this->public);
1026 ike_sa_init
->set_config(ike_sa_init
, connection
, policy
);
1027 /* reuse existing reqid */
1028 ike_sa_init
->set_reqid(ike_sa_init
, reqid
);
1029 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
1031 case IKE_CONNECTING
:
1032 case IKE_ESTABLISHED
:
1034 create_child_sa_t
*create_child
;
1036 DBG1(SIG_DBG_CHD
, "acquiring CHILD_SA with reqid %d", reqid
);
1038 create_child
= create_child_sa_create(&this->public);
1039 create_child
->set_policy(create_child
, policy
);
1040 /* reuse existing reqid */
1041 create_child
->set_reqid(create_child
, reqid
);
1042 return queue_transaction(this, (transaction_t
*)create_child
, FALSE
);
1051 * compare two lists of traffic selectors for equality
1053 static bool ts_list_equals(linked_list_t
*l1
, linked_list_t
*l2
)
1056 iterator_t
*i1
, *i2
;
1057 traffic_selector_t
*t1
, *t2
;
1059 if (l1
->get_count(l1
) != l2
->get_count(l2
))
1064 i1
= l1
->create_iterator(l1
, TRUE
);
1065 i2
= l2
->create_iterator(l2
, TRUE
);
1066 while (i1
->iterate(i1
, (void**)&t1
) && i2
->iterate(i2
, (void**)&t2
))
1068 if (!t1
->equals(t1
, t2
))
1080 * Implementation of ike_sa_t.route.
1082 static status_t
route(private_ike_sa_t
*this, connection_t
*connection
, policy_t
*policy
)
1084 child_sa_t
*child_sa
= NULL
;
1085 iterator_t
*iterator
;
1086 linked_list_t
*my_ts
, *other_ts
;
1089 /* check if not already routed*/
1090 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1091 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1093 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1095 linked_list_t
*my_ts_conf
, *other_ts_conf
;
1097 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1098 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1100 my_ts_conf
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1101 other_ts_conf
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1103 if (ts_list_equals(my_ts
, my_ts_conf
) &&
1104 ts_list_equals(other_ts
, other_ts_conf
))
1106 iterator
->destroy(iterator
);
1107 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1108 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1109 SIG(SIG_CHILD_FAILED
, "CHILD_SA with such a policy "
1113 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1114 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1117 iterator
->destroy(iterator
);
1119 switch (this->state
)
1122 case IKE_CONNECTING
:
1123 /* we update IKE_SA information as good as possible,
1124 * this allows us to set up the SA later when an acquire comes in. */
1125 if (this->my_id
->get_type(this->my_id
) == ID_ANY
)
1127 this->my_id
->destroy(this->my_id
);
1128 this->my_id
= policy
->get_my_id(policy
);
1129 this->my_id
= this->my_id
->clone(this->my_id
);
1131 if (this->other_id
->get_type(this->other_id
) == ID_ANY
)
1133 this->other_id
->destroy(this->other_id
);
1134 this->other_id
= policy
->get_other_id(policy
);
1135 this->other_id
= this->other_id
->clone(this->other_id
);
1137 if (this->my_host
->is_anyaddr(this->my_host
))
1139 this->my_host
->destroy(this->my_host
);
1140 this->my_host
= connection
->get_my_host(connection
);
1141 this->my_host
= this->my_host
->clone(this->my_host
);
1143 if (this->other_host
->is_anyaddr(this->other_host
))
1145 this->other_host
->destroy(this->other_host
);
1146 this->other_host
= connection
->get_other_host(connection
);
1147 this->other_host
= this->other_host
->clone(this->other_host
);
1149 set_name(this, connection
->get_name(connection
));
1150 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
1151 this->dpd_delay
= connection
->get_dpd_delay(connection
);
1153 case IKE_ESTABLISHED
:
1155 /* nothing to do. We allow it for rekeying, as it will be
1156 * adopted by the new IKE_SA */
1159 SIG(SIG_CHILD_FAILED
, "CHILD_SA with such a policy "
1164 child_sa
= child_sa_create(0, this->my_host
, this->other_host
,
1165 this->my_id
, this->other_id
,
1167 NULL
, policy
->get_hostaccess(policy
),
1169 child_sa
->set_name(child_sa
, policy
->get_name(policy
));
1170 my_ts
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1171 other_ts
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1172 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
);
1173 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1174 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1175 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1176 SIG(SIG_CHILD_ROUTE
,
1177 "CHILD_SA routed: %R...%R", my_ts
, other_ts
);
1182 * Implementation of ike_sa_t.unroute.
1184 static status_t
unroute(private_ike_sa_t
*this, policy_t
*policy
)
1186 iterator_t
*iterator
;
1187 child_sa_t
*child_sa
= NULL
;
1188 linked_list_t
*my_ts
, *other_ts
, *my_ts_conf
, *other_ts_conf
;
1190 /* find CHILD_SA in ROUTED state */
1191 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1192 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1194 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1196 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1197 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1199 my_ts_conf
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1200 other_ts_conf
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1202 if (ts_list_equals(my_ts
, my_ts_conf
) &&
1203 ts_list_equals(other_ts
, other_ts_conf
))
1205 iterator
->remove(iterator
);
1206 SIG(SIG_CHILD_UNROUTE
, "CHILD_SA unrouted");
1207 child_sa
->destroy(child_sa
);
1208 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1209 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1212 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1213 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1216 iterator
->destroy(iterator
);
1217 /* if we are not established, and we have no more routed childs, remove whole SA */
1218 if (this->state
== IKE_CREATED
&&
1219 this->child_sas
->get_count(this->child_sas
) == 0)
1227 * Implementation of ike_sa_t.send_dpd
1229 static status_t
send_dpd(private_ike_sa_t
*this)
1231 send_dpd_job_t
*job
;
1234 if (this->dpd_delay
== 0)
1240 if (this->transaction_out
)
1242 /* there is a transaction in progress. Come back later */
1247 /* check if there was any inbound traffic */
1248 time_t last_in
, now
;
1249 last_in
= get_time_inbound(this);
1251 diff
= now
- last_in
;
1252 if (diff
>= this->dpd_delay
)
1254 /* to long ago, initiate dead peer detection */
1255 dead_peer_detection_t
*dpd
;
1256 DBG1(SIG_DBG_IKE
, "sending DPD request");
1257 dpd
= dead_peer_detection_create(&this->public);
1258 queue_transaction(this, (transaction_t
*)dpd
, FALSE
);
1262 /* recheck in "interval" seconds */
1263 job
= send_dpd_job_create(this->ike_sa_id
);
1264 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
1265 (this->dpd_delay
- diff
) * 1000);
1270 * Implementation of ike_sa_t.send_keepalive
1272 static void send_keepalive(private_ike_sa_t
*this)
1274 send_keepalive_job_t
*job
;
1275 time_t last_out
, now
, diff
, interval
;
1277 last_out
= get_time_outbound(this);
1280 diff
= now
- last_out
;
1281 interval
= charon
->configuration
->get_keepalive_interval(charon
->configuration
);
1283 if (diff
>= interval
)
1288 packet
= packet_create();
1289 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
1290 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
1291 data
.ptr
= malloc(1);
1294 packet
->set_data(packet
, data
);
1295 charon
->send_queue
->add(charon
->send_queue
, packet
);
1296 DBG1(SIG_DBG_IKE
, "sending keep alive");
1299 job
= send_keepalive_job_create(this->ike_sa_id
);
1300 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
1301 (interval
- diff
) * 1000);
1305 * Implementation of ike_sa_t.get_state.
1307 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
1313 * Implementation of ike_sa_t.set_state.
1315 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
1317 DBG1(SIG_DBG_IKE
, "state change: %N => %N",
1318 ike_sa_state_names
, this->state
,
1319 ike_sa_state_names
, state
);
1321 if (state
== IKE_ESTABLISHED
)
1323 this->time
.established
= time(NULL
);
1324 /* start DPD checks */
1327 SIG(SIG_IKE_UP
, "IKE_SA established: %H[%D]...%H[%D]",
1328 this->my_host
, this->my_id
, this->other_host
, this->other_id
);
1331 this->state
= state
;
1335 * Implementation of ike_sa_t.get_prf.
1337 static prf_t
*get_prf(private_ike_sa_t
*this)
1343 * Implementation of ike_sa_t.get_prf.
1345 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1347 return this->child_prf
;
1351 * Implementation of ike_sa_t.get_prf_auth_i.
1353 static prf_t
*get_prf_auth_i(private_ike_sa_t
*this)
1355 return this->prf_auth_i
;
1359 * Implementation of ike_sa_t.get_prf_auth_r.
1361 static prf_t
*get_prf_auth_r(private_ike_sa_t
*this)
1363 return this->prf_auth_r
;
1367 * Implementation of ike_sa_t.get_id.
1369 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1371 return this->ike_sa_id
;
1375 * Implementation of ike_sa_t.get_my_id.
1377 static identification_t
* get_my_id(private_ike_sa_t
*this)
1383 * Implementation of ike_sa_t.set_my_id.
1385 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1387 DESTROY_IF(this->my_id
);
1392 * Implementation of ike_sa_t.get_other_id.
1394 static identification_t
* get_other_id(private_ike_sa_t
*this)
1396 return this->other_id
;
1400 * Implementation of ike_sa_t.set_other_id.
1402 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1404 DESTROY_IF(this->other_id
);
1405 this->other_id
= other
;
1409 * Implementation of ike_sa_t.derive_keys.
1411 static status_t
derive_keys(private_ike_sa_t
*this,
1412 proposal_t
*proposal
, diffie_hellman_t
*dh
,
1413 chunk_t nonce_i
, chunk_t nonce_r
,
1414 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1416 prf_plus_t
*prf_plus
;
1417 chunk_t skeyseed
, secret
, key
, nonces
, prf_plus_seed
;
1420 crypter_t
*crypter_i
, *crypter_r
;
1421 signer_t
*signer_i
, *signer_r
;
1422 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1423 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1424 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1426 /* Create SAs general purpose PRF first, we may use it here */
1427 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
1429 DBG1(SIG_DBG_IKE
, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1432 this->prf
= prf_create(algo
->algorithm
);
1433 if (this->prf
== NULL
)
1435 DBG1(SIG_DBG_IKE
, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1436 "%N not supported!", pseudo_random_function_names
, algo
->algorithm
);
1440 dh
->get_shared_secret(dh
, &secret
);
1441 DBG4(SIG_DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1442 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1443 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1444 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1445 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1447 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1449 * if we are rekeying, SKEYSEED built on another way
1451 if (child_prf
== NULL
) /* not rekeying */
1453 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1454 this->prf
->set_key(this->prf
, nonces
);
1455 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1456 DBG4(SIG_DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1457 this->prf
->set_key(this->prf
, skeyseed
);
1458 chunk_free(&skeyseed
);
1459 chunk_free(&secret
);
1460 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1464 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1465 * use OLD SAs PRF functions for both prf_plus and prf */
1466 secret
= chunk_cat("mc", secret
, nonces
);
1467 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1468 DBG4(SIG_DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1469 old_prf
->set_key(old_prf
, skeyseed
);
1470 chunk_free(&skeyseed
);
1471 chunk_free(&secret
);
1472 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1474 chunk_free(&nonces
);
1475 chunk_free(&prf_plus_seed
);
1477 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1479 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1480 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1481 this->child_prf
= prf_create(algo
->algorithm
);
1482 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1483 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1484 DBG4(SIG_DBG_IKE
, "Sk_d secret %B", &key
);
1485 this->child_prf
->set_key(this->child_prf
, key
);
1488 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1489 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1491 DBG1(SIG_DBG_IKE
, "key derivation failed: no INTEGRITY_ALGORITHM");
1494 signer_i
= signer_create(algo
->algorithm
);
1495 signer_r
= signer_create(algo
->algorithm
);
1496 if (signer_i
== NULL
|| signer_r
== NULL
)
1498 DBG1(SIG_DBG_IKE
, "key derivation failed: INTEGRITY_ALGORITHM "
1499 "%N not supported!", integrity_algorithm_names
,algo
->algorithm
);
1502 key_size
= signer_i
->get_key_size(signer_i
);
1504 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1505 DBG4(SIG_DBG_IKE
, "Sk_ai secret %B", &key
);
1506 signer_i
->set_key(signer_i
, key
);
1509 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1510 DBG4(SIG_DBG_IKE
, "Sk_ar secret %B", &key
);
1511 signer_r
->set_key(signer_r
, key
);
1516 this->signer_in
= signer_r
;
1517 this->signer_out
= signer_i
;
1521 this->signer_in
= signer_i
;
1522 this->signer_out
= signer_r
;
1525 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1526 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1528 DBG1(SIG_DBG_IKE
, "key derivation failed: no ENCRYPTION_ALGORITHM");
1531 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1532 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1533 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1535 DBG1(SIG_DBG_IKE
, "key derivation failed: ENCRYPTION_ALGORITHM "
1536 "%N (key size %d) not supported!",
1537 encryption_algorithm_names
, algo
->algorithm
, algo
->key_size
);
1540 key_size
= crypter_i
->get_key_size(crypter_i
);
1542 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1543 DBG4(SIG_DBG_IKE
, "Sk_ei secret %B", &key
);
1544 crypter_i
->set_key(crypter_i
, key
);
1547 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1548 DBG4(SIG_DBG_IKE
, "Sk_er secret %B", &key
);
1549 crypter_r
->set_key(crypter_r
, key
);
1554 this->crypter_in
= crypter_r
;
1555 this->crypter_out
= crypter_i
;
1559 this->crypter_in
= crypter_i
;
1560 this->crypter_out
= crypter_r
;
1563 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1564 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1565 this->prf_auth_i
= prf_create(algo
->algorithm
);
1566 this->prf_auth_r
= prf_create(algo
->algorithm
);
1568 key_size
= this->prf_auth_i
->get_key_size(this->prf_auth_i
);
1569 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1570 DBG4(SIG_DBG_IKE
, "Sk_pi secret %B", &key
);
1571 this->prf_auth_i
->set_key(this->prf_auth_i
, key
);
1574 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1575 DBG4(SIG_DBG_IKE
, "Sk_pr secret %B", &key
);
1576 this->prf_auth_r
->set_key(this->prf_auth_r
, key
);
1579 /* all done, prf_plus not needed anymore */
1580 prf_plus
->destroy(prf_plus
);
1587 * Implementation of ike_sa_t.add_child_sa.
1589 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1591 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1595 * Implementation of ike_sa_t.has_child_sa.
1597 static bool has_child_sa(private_ike_sa_t
*this, u_int32_t reqid
)
1599 iterator_t
*iterator
;
1600 child_sa_t
*current
;
1603 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1604 while (iterator
->iterate(iterator
, (void**)¤t
))
1606 if (current
->get_reqid(current
) == reqid
)
1612 iterator
->destroy(iterator
);
1617 * Implementation of ike_sa_t.get_child_sa.
1619 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1620 u_int32_t spi
, bool inbound
)
1622 iterator_t
*iterator
;
1623 child_sa_t
*current
, *found
= NULL
;
1625 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1626 while (iterator
->iterate(iterator
, (void**)¤t
))
1628 if (current
->get_spi(current
, inbound
) == spi
&&
1629 current
->get_protocol(current
) == protocol
)
1634 iterator
->destroy(iterator
);
1639 * Implementation of ike_sa_t.create_child_sa_iterator.
1641 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1643 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1647 * Implementation of ike_sa_t.rekey_child_sa.
1649 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1651 create_child_sa_t
*rekey
;
1652 child_sa_t
*child_sa
;
1654 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1655 if (child_sa
== NULL
)
1660 rekey
= create_child_sa_create(&this->public);
1661 rekey
->rekeys_child(rekey
, child_sa
);
1662 return queue_transaction(this, (transaction_t
*)rekey
, FALSE
);
1666 * Implementation of ike_sa_t.delete_child_sa.
1668 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1670 delete_child_sa_t
*del
;
1671 child_sa_t
*child_sa
;
1673 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1674 if (child_sa
== NULL
)
1679 del
= delete_child_sa_create(&this->public);
1680 del
->set_child_sa(del
, child_sa
);
1681 return queue_transaction(this, (transaction_t
*)del
, FALSE
);
1685 * Implementation of ike_sa_t.destroy_child_sa.
1687 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1689 iterator_t
*iterator
;
1690 child_sa_t
*child_sa
;
1691 status_t status
= NOT_FOUND
;
1693 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1694 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1696 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1697 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1699 child_sa
->destroy(child_sa
);
1700 iterator
->remove(iterator
);
1705 iterator
->destroy(iterator
);
1710 * Implementation of ike_sa_t.set_lifetimes.
1712 static void set_lifetimes(private_ike_sa_t
*this,
1713 u_int32_t soft_lifetime
, u_int32_t hard_lifetime
)
1719 this->time
.rekey
= this->time
.established
+ soft_lifetime
;
1720 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
);
1721 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
1722 soft_lifetime
* 1000);
1727 this->time
.delete = this->time
.established
+ hard_lifetime
;
1728 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
1729 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
1730 hard_lifetime
* 1000);
1735 * Implementation of ike_sa_t.rekey.
1737 static status_t
rekey(private_ike_sa_t
*this)
1739 rekey_ike_sa_t
*rekey_ike_sa
;
1741 DBG1(SIG_DBG_IKE
, "rekeying IKE_SA between %H[%D]..%H[%D]",
1742 this->my_host
, this->my_id
,
1743 this->other_host
, this->other_id
);
1745 if (this->state
!= IKE_ESTABLISHED
)
1747 SIG(SIG_IKE_FAILED
, "unable to rekey IKE_SA in state %N",
1748 ike_sa_state_names
, this->state
);
1752 rekey_ike_sa
= rekey_ike_sa_create(&this->public);
1753 return queue_transaction(this, (transaction_t
*)rekey_ike_sa
, FALSE
);
1757 * Implementation of ike_sa_t.get_rekeying_transaction.
1759 static rekey_ike_sa_t
* get_rekeying_transaction(private_ike_sa_t
*this)
1761 return this->rekeying_transaction
;
1765 * Implementation of ike_sa_t.set_rekeying_transaction.
1767 static void set_rekeying_transaction(private_ike_sa_t
*this, rekey_ike_sa_t
*rekey
)
1769 this->rekeying_transaction
= rekey
;
1773 * Implementation of ike_sa_t.adopt_children.
1775 static void adopt_children(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1777 child_sa_t
*child_sa
;
1779 while (other
->child_sas
->remove_last(other
->child_sas
,
1780 (void**)&child_sa
) == SUCCESS
)
1782 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1787 * Implementation of public_ike_sa_t.delete.
1789 static status_t
delete_(private_ike_sa_t
*this)
1791 switch (this->state
)
1793 case IKE_CONNECTING
:
1794 case IKE_ESTABLISHED
:
1796 delete_ike_sa_t
*delete_ike_sa
;
1797 if (this->transaction_out
)
1799 /* already a transaction in progress. As this may hang
1800 * around a while, we don't inform the other peer. */
1803 delete_ike_sa
= delete_ike_sa_create(&this->public);
1804 return queue_transaction(this, (transaction_t
*)delete_ike_sa
, FALSE
);
1816 * Implementation of ike_sa_t.get_next_message_id.
1818 static u_int32_t
get_next_message_id (private_ike_sa_t
*this)
1820 return this->message_id_out
++;
1824 * Implementation of ike_sa_t.is_natt_enabled.
1826 static bool is_natt_enabled (private_ike_sa_t
*this)
1828 return this->nat_here
|| this->nat_there
;
1832 * Implementation of ike_sa_t.enable_natt.
1834 static void enable_natt (private_ike_sa_t
*this, bool local
)
1838 DBG1(SIG_DBG_IKE
, "local host is behind NAT, using NAT-T, "
1839 "scheduled keep alives");
1840 this->nat_here
= TRUE
;
1841 send_keepalive(this);
1845 DBG1(SIG_DBG_IKE
, "remote host is behind NAT, using NAT-T");
1846 this->nat_there
= TRUE
;
1851 * output handler in printf()
1853 static int print(FILE *stream
, const struct printf_info
*info
,
1854 const void *const *args
)
1856 private_ike_sa_t
*this = *((private_ike_sa_t
**)(args
[0]));
1860 return fprintf(stream
, "(null)");
1863 return fprintf(stream
, "%10s: %N, %H[%D]...%H[%D] (%J)",
1864 this->name
, ike_sa_state_names
, this->state
,
1865 this->my_host
, this->my_id
, this->other_host
, this->other_id
,
1870 * arginfo handler in printf()
1872 static int print_arginfo(const struct printf_info
*info
, size_t n
, int *argtypes
)
1876 argtypes
[0] = PA_POINTER
;
1882 * register printf() handlers
1884 static void __attribute__ ((constructor
))print_register()
1886 register_printf_function(IKE_SA_PRINTF_SPEC
, print
, print_arginfo
);
1890 * Implementation of ike_sa_t.destroy.
1892 static void destroy(private_ike_sa_t
*this)
1894 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
1895 this->transaction_queue
->destroy_offset(this->transaction_queue
, offsetof(transaction_t
, destroy
));
1897 DESTROY_IF(this->transaction_in
);
1898 DESTROY_IF(this->transaction_in_next
);
1899 DESTROY_IF(this->transaction_out
);
1900 DESTROY_IF(this->crypter_in
);
1901 DESTROY_IF(this->crypter_out
);
1902 DESTROY_IF(this->signer_in
);
1903 DESTROY_IF(this->signer_out
);
1904 DESTROY_IF(this->prf
);
1905 DESTROY_IF(this->child_prf
);
1906 DESTROY_IF(this->prf_auth_i
);
1907 DESTROY_IF(this->prf_auth_r
);
1909 DBG1(SIG_DBG_IKE
, "IKE_SA deleted between %H[%D]...%H[%D]",
1910 this->my_host
, this->my_id
, this->other_host
, this->other_id
);
1912 DESTROY_IF(this->my_host
);
1913 DESTROY_IF(this->other_host
);
1914 DESTROY_IF(this->my_id
);
1915 DESTROY_IF(this->other_id
);
1918 this->ike_sa_id
->destroy(this->ike_sa_id
);
1923 * Described in header.
1925 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1927 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
1929 /* Public functions */
1930 this->public.get_state
= (ike_sa_state_t(*)(ike_sa_t
*)) get_state
;
1931 this->public.set_state
= (void(*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
1932 this->public.get_name
= (char*(*)(ike_sa_t
*))get_name
;
1933 this->public.set_name
= (void(*)(ike_sa_t
*,char*))set_name
;
1934 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
1935 this->public.initiate
= (status_t(*)(ike_sa_t
*,connection_t
*,policy_t
*)) initiate
;
1936 this->public.route
= (status_t(*)(ike_sa_t
*,connection_t
*,policy_t
*)) route
;
1937 this->public.unroute
= (status_t(*)(ike_sa_t
*,policy_t
*)) unroute
;
1938 this->public.acquire
= (status_t(*)(ike_sa_t
*,u_int32_t
)) acquire
;
1939 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
1940 this->public.get_my_host
= (host_t
*(*)(ike_sa_t
*)) get_my_host
;
1941 this->public.set_my_host
= (void(*)(ike_sa_t
*,host_t
*)) set_my_host
;
1942 this->public.get_other_host
= (host_t
*(*)(ike_sa_t
*)) get_other_host
;
1943 this->public.set_other_host
= (void(*)(ike_sa_t
*,host_t
*)) set_other_host
;
1944 this->public.get_my_id
= (identification_t
*(*)(ike_sa_t
*)) get_my_id
;
1945 this->public.set_my_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_my_id
;
1946 this->public.get_other_id
= (identification_t
*(*)(ike_sa_t
*)) get_other_id
;
1947 this->public.set_other_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_other_id
;
1948 this->public.get_next_message_id
= (u_int32_t(*)(ike_sa_t
*)) get_next_message_id
;
1949 this->public.retransmit_request
= (status_t (*) (ike_sa_t
*, u_int32_t
)) retransmit_request
;
1950 this->public.delete = (status_t(*)(ike_sa_t
*))delete_
;
1951 this->public.destroy
= (void(*)(ike_sa_t
*))destroy
;
1952 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
1953 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
1954 this->public.get_prf
= (prf_t
*(*) (ike_sa_t
*)) get_prf
;
1955 this->public.get_child_prf
= (prf_t
*(*) (ike_sa_t
*)) get_child_prf
;
1956 this->public.get_prf_auth_i
= (prf_t
*(*) (ike_sa_t
*)) get_prf_auth_i
;
1957 this->public.get_prf_auth_r
= (prf_t
*(*) (ike_sa_t
*)) get_prf_auth_r
;
1958 this->public.derive_keys
= (status_t (*) (ike_sa_t
*,proposal_t
*,diffie_hellman_t
*,chunk_t
,chunk_t
,bool,prf_t
*,prf_t
*)) derive_keys
;
1959 this->public.add_child_sa
= (void (*) (ike_sa_t
*,child_sa_t
*)) add_child_sa
;
1960 this->public.has_child_sa
= (bool(*)(ike_sa_t
*,u_int32_t
)) has_child_sa
;
1961 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
1962 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
1963 this->public.rekey_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
1964 this->public.delete_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
1965 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
1966 this->public.enable_natt
= (void(*)(ike_sa_t
*, bool)) enable_natt
;
1967 this->public.is_natt_enabled
= (bool(*)(ike_sa_t
*)) is_natt_enabled
;
1968 this->public.set_lifetimes
= (void(*)(ike_sa_t
*,u_int32_t
,u_int32_t
))set_lifetimes
;
1969 this->public.apply_connection
= (void(*)(ike_sa_t
*,connection_t
*))apply_connection
;
1970 this->public.rekey
= (status_t(*)(ike_sa_t
*))rekey
;
1971 this->public.get_rekeying_transaction
= (void*(*)(ike_sa_t
*))get_rekeying_transaction
;
1972 this->public.set_rekeying_transaction
= (void(*)(ike_sa_t
*,void*))set_rekeying_transaction
;
1973 this->public.adopt_children
= (void(*)(ike_sa_t
*,ike_sa_t
*))adopt_children
;
1975 /* initialize private fields */
1976 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1977 this->name
= strdup("(uninitialized)");
1978 this->child_sas
= linked_list_create();
1979 this->my_host
= host_create_from_string("0.0.0.0", 0);
1980 this->other_host
= host_create_from_string("0.0.0.0", 0);
1981 this->my_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
1982 this->other_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
1983 this->crypter_in
= NULL
;
1984 this->crypter_out
= NULL
;
1985 this->signer_in
= NULL
;
1986 this->signer_out
= NULL
;
1988 this->prf_auth_i
= NULL
;
1989 this->prf_auth_r
= NULL
;
1990 this->child_prf
= NULL
;
1991 this->nat_here
= FALSE
;
1992 this->nat_there
= FALSE
;
1993 this->transaction_queue
= linked_list_create();
1994 this->transaction_in
= NULL
;
1995 this->transaction_in_next
= NULL
;
1996 this->transaction_out
= NULL
;
1997 this->rekeying_transaction
= NULL
;
1998 this->state
= IKE_CREATED
;
1999 this->message_id_out
= 0;
2000 /* set to NOW, as when we rekey an existing IKE_SA no message is exchanged */
2001 this->time
.inbound
= this->time
.outbound
= time(NULL
);
2002 this->time
.established
= 0;
2003 this->time
.rekey
= 0;
2004 this->time
.delete = 0;
2005 this->dpd_delay
= 0;
2006 this->retrans_sequences
= 0;
2008 return &this->public;