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 to build outging authentication data
157 * PRF to verify incoming authentication data
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 transaction_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 connection_t
*connection
= NULL
;
436 linked_list_t
*my_ts
, *other_ts
;
437 child_sa_t
* child_sa
;
441 DBG2(DBG_IKE
, "dead peer detected, handling CHILD_SAs dpd action");
443 /* check for childrens with dpdaction = hold */
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
,
457 DBG1(DBG_IKE
, "no policy for CHILD to handle DPD");
461 action
= policy
->get_dpd_action(policy
);
462 /* get a connection for further actions */
463 if (connection
== NULL
&&
464 (action
== DPD_ROUTE
|| action
== DPD_RESTART
))
466 connection
= charon
->connections
->get_connection_by_hosts(
468 this->my_host
, this->other_host
);
469 if (connection
== NULL
)
471 SIG(IKE_UP_FAILED
, "no connection found to handle DPD");
476 DBG1(DBG_IKE
, "dpd action for %s is %N",
477 policy
->get_name(policy
), dpd_action_names
, action
);
482 connection
->get_ref(connection
);
483 job
= (job_t
*)route_job_create(connection
, policy
, TRUE
);
484 charon
->job_queue
->add(charon
->job_queue
, job
);
487 connection
->get_ref(connection
);
488 job
= (job_t
*)initiate_job_create(connection
, policy
);
489 charon
->job_queue
->add(charon
->job_queue
, job
);
492 policy
->destroy(policy
);
495 child_sa
->destroy(child_sa
);
497 DESTROY_IF(connection
);
501 * send a request and schedule retransmission
503 static status_t
transmit_request(private_ike_sa_t
*this)
508 retransmit_request_job_t
*job
;
509 u_int32_t transmitted
;
511 transaction_t
*transaction
= this->transaction_out
;
512 u_int32_t message_id
;
514 transmitted
= transaction
->requested(transaction
);
515 timeout
= charon
->configuration
->get_retransmit_timeout(charon
->configuration
,
517 this->retrans_sequences
);
520 DBG1(DBG_IKE
, "giving up after %d retransmits, deleting IKE_SA",
526 status
= transaction
->get_request(transaction
, &request
);
527 if (status
!= SUCCESS
)
529 /* generating request failed */
532 message_id
= transaction
->get_message_id(transaction
);
533 /* if we retransmit, the request is already generated */
534 if (transmitted
== 0)
536 status
= request
->generate(request
, this->crypter_out
, this->signer_out
, &packet
);
537 if (status
!= SUCCESS
)
539 DBG1(DBG_IKE
, "request generation failed. transaction discarded");
545 DBG1(DBG_IKE
, "sending retransmit %d for %N request with messageID %d",
546 transmitted
, exchange_type_names
, request
->get_exchange_type(request
),
548 packet
= request
->get_packet(request
);
551 charon
->send_queue
->add(charon
->send_queue
, packet
);
552 this->time
.outbound
= time(NULL
);
554 /* schedule retransmission job */
555 job
= retransmit_request_job_create(message_id
, this->ike_sa_id
);
556 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
, timeout
);
561 * Implementation of ike_sa.retransmit_request.
563 static status_t
retransmit_request(private_ike_sa_t
*this, u_int32_t message_id
)
565 if (this->transaction_out
== NULL
||
566 this->transaction_out
->get_message_id(this->transaction_out
) != message_id
)
568 /* no retransmit necessary, transaction did already complete */
571 return transmit_request(this);
575 * Check for transactions in the queue and initiate the first transaction found.
577 static status_t
process_transaction_queue(private_ike_sa_t
*this)
579 if (this->transaction_out
)
581 /* already a transaction in progress */
587 if (this->transaction_queue
->remove_first(this->transaction_queue
,
588 (void**)&this->transaction_out
) != SUCCESS
)
590 /* transaction queue empty */
593 switch (transmit_request(this))
598 /* critical, IKE_SA unusable, destroy immediately */
601 /* discard transaction, process next one */
602 this->transaction_out
->destroy(this->transaction_out
);
603 this->transaction_out
= NULL
;
604 /* handle next transaction */
611 * Queue a new transaction and execute the next outstanding transaction
613 static status_t
queue_transaction(private_ike_sa_t
*this, transaction_t
*transaction
, bool prefer
)
615 /* inject next transaction */
620 this->transaction_queue
->insert_first(this->transaction_queue
, transaction
);
624 this->transaction_queue
->insert_last(this->transaction_queue
, transaction
);
627 /* process a transaction */
628 return process_transaction_queue(this);
632 * process an incoming request.
634 static status_t
process_request(private_ike_sa_t
*this, message_t
*request
)
636 transaction_t
*last
, *current
= NULL
;
639 u_int32_t request_mid
;
642 request_mid
= request
->get_message_id(request
);
643 last
= this->transaction_in
;
645 /* check if message ID is correct */
648 u_int32_t last_mid
= last
->get_message_id(last
);
650 if (last_mid
== request_mid
)
652 /* retransmit detected */
653 DBG1(DBG_IKE
, "received retransmitted request for message "
654 "ID %d, retransmitting response", request_mid
);
655 last
->get_response(last
, request
, &response
, &this->transaction_in_next
);
656 packet
= response
->get_packet(response
);
657 charon
->send_queue
->add(charon
->send_queue
, packet
);
658 this->time
.outbound
= time(NULL
);
662 if (last_mid
> request_mid
)
664 /* something seriously wrong here, message id may not decrease */
665 DBG1(DBG_IKE
, "received request with message ID %d, "
666 "excepted %d, ingored", request_mid
, last_mid
+ 1);
669 /* we allow jumps in message IDs, as long as they are incremental */
670 if (last_mid
+ 1 < request_mid
)
672 DBG1(DBG_IKE
, "received request with message ID %d, excepted %d",
673 request_mid
, last_mid
+ 1);
678 if (request_mid
!= 0)
680 /* warn, but allow it */
681 DBG1(DBG_IKE
, "first received request has message ID %d, "
682 "excepted 0", request_mid
);
686 /* check if we already have a pre-created transaction for this request */
687 if (this->transaction_in_next
)
689 current
= this->transaction_in_next
;
690 this->transaction_in_next
= NULL
;
694 current
= transaction_create(&this->public, request
);
697 DBG1(DBG_IKE
, "no idea how to handle received message (exchange"
698 " type %d), ignored", request
->get_exchange_type(request
));
703 /* send message. get_request() always gives a valid response */
704 status
= current
->get_response(current
, request
, &response
, &this->transaction_in_next
);
705 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
707 DBG1(DBG_IKE
, "response generation failed, discarding transaction");
708 current
->destroy(current
);
712 charon
->send_queue
->add(charon
->send_queue
, packet
);
713 this->time
.outbound
= time(NULL
);
714 /* act depending on transaction result */
718 /* transactions says we should destroy the IKE_SA, so do it */
719 current
->destroy(current
);
722 /* store for retransmission, destroy old transaction */
723 this->transaction_in
= current
;
733 * process an incoming response
735 static status_t
process_response(private_ike_sa_t
*this, message_t
*response
)
737 transaction_t
*current
, *new = NULL
;
739 current
= this->transaction_out
;
740 /* check if message ID is that of our currently active transaction */
741 if (current
== NULL
||
742 current
->get_message_id(current
) != response
->get_message_id(response
))
744 DBG1(DBG_IKE
, "received response with message ID %d "
745 "not requested, ignored", response
->get_message_id(response
));
749 switch (current
->conclude(current
, response
, &new))
752 /* state requested to destroy IKE_SA */
755 /* discard transaction, process next one */
758 /* transaction comleted, remove */
759 current
->destroy(current
);
760 this->transaction_out
= NULL
;
762 /* queue new transaction */
763 return queue_transaction(this, new, TRUE
);
767 * send a notify back to the sender
769 static void send_notify_response(private_ike_sa_t
*this,
773 notify_payload_t
*notify
;
778 response
= message_create();
779 dst
= request
->get_source(request
);
780 src
= request
->get_destination(request
);
781 response
->set_source(response
, src
->clone(src
));
782 response
->set_destination(response
, dst
->clone(dst
));
783 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
784 response
->set_request(response
, FALSE
);
785 response
->set_message_id(response
, request
->get_message_id(request
));
786 response
->set_ike_sa_id(response
, this->ike_sa_id
);
787 notify
= notify_payload_create_from_protocol_and_type(PROTO_NONE
, type
);
788 response
->add_payload(response
, (payload_t
*)notify
);
789 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
791 response
->destroy(response
);
794 charon
->send_queue
->add(charon
->send_queue
, packet
);
795 this->time
.outbound
= time(NULL
);
796 response
->destroy(response
);
802 * Implementation of ike_sa_t.process_message.
804 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
809 is_request
= message
->get_request(message
);
811 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
812 if (status
!= SUCCESS
)
820 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
823 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
827 DBG1(DBG_IKE
, "message parsing failed");
830 send_notify_response(this, message
, INVALID_SYNTAX
);
834 DBG1(DBG_IKE
, "message verification failed");
837 send_notify_response(this, message
, INVALID_SYNTAX
);
841 DBG1(DBG_IKE
, "integrity check failed");
845 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
848 send_notify_response(this, message
, INVALID_SYNTAX
);
854 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
855 exchange_type_names
, message
->get_exchange_type(message
),
856 message
->get_request(message
) ?
"request" : "response",
857 message
->get_message_id(message
));
861 /* check if message is trustworthy, and update connection information */
862 if (this->state
== IKE_CREATED
||
863 message
->get_exchange_type(message
) != IKE_SA_INIT
)
865 update_hosts(this, message
->get_destination(message
),
866 message
->get_source(message
));
867 this->time
.inbound
= time(NULL
);
871 status
= process_request(this, message
);
875 status
= process_response(this, message
);
882 * Implementation of ike_sa_t.initiate.
884 static status_t
initiate(private_ike_sa_t
*this,
885 connection_t
*connection
, policy_t
*policy
)
891 /* in state CREATED, we must do the ike_sa_init
892 * and ike_auth transactions. Along with these,
893 * a CHILD_SA with the supplied policy is set up.
895 ike_sa_init_t
*ike_sa_init
;
897 DBG2(DBG_IKE
, "initiating new IKE_SA for CHILD_SA");
898 DESTROY_IF(this->my_host
);
899 this->my_host
= connection
->get_my_host(connection
);
900 this->my_host
= this->my_host
->clone(this->my_host
);
901 DESTROY_IF(this->other_host
);
902 this->other_host
= connection
->get_other_host(connection
);
903 this->other_host
= this->other_host
->clone(this->other_host
);
904 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
905 this->dpd_delay
= connection
->get_dpd_delay(connection
);
907 if (this->other_host
->is_anyaddr(this->other_host
))
909 SIG(IKE_UP_START
, "establishing new IKE_SA for CHILD_SA");
910 SIG(IKE_UP_FAILED
, "can not initiate a connection to %%any, aborting");
911 policy
->destroy(policy
);
912 connection
->destroy(connection
);
916 this->message_id_out
= 1;
917 ike_sa_init
= ike_sa_init_create(&this->public);
918 ike_sa_init
->set_config(ike_sa_init
, connection
, policy
);
919 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
924 /* if we are in DELETING/REKEYING, we deny set up of a policy.
925 * TODO: would it make sense to queue the transaction and adopt
926 * all transactions to the new IKE_SA? */
927 SIG(IKE_UP_START
, "creating CHILD_SA in existing IKE_SA");
928 SIG(IKE_UP_FAILED
, "creating CHILD_SA discarded, as IKE_SA is in state %N",
929 ike_sa_state_names
, this->state
);
930 policy
->destroy(policy
);
931 connection
->destroy(connection
);
935 case IKE_ESTABLISHED
:
937 /* if we are ESTABLISHED or CONNECTING, we queue the
938 * transaction to create the CHILD_SA. It gets processed
939 * when the IKE_SA is ready to do so. We don't need the
940 * connection, as the IKE_SA is already established/establishing.
942 create_child_sa_t
*create_child
;
944 DBG1(DBG_IKE
, "creating CHILD_SA in existing IKE_SA");
945 connection
->destroy(connection
);
946 create_child
= create_child_sa_create(&this->public);
947 create_child
->set_policy(create_child
, policy
);
948 return queue_transaction(this, (transaction_t
*)create_child
, FALSE
);
955 * Implementation of ike_sa_t.acquire.
957 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
959 connection_t
*connection
;
961 iterator_t
*iterator
;
962 child_sa_t
*current
, *child_sa
= NULL
;
963 linked_list_t
*my_ts
, *other_ts
;
965 if (this->state
== IKE_DELETING
)
967 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
968 SIG(CHILD_UP_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(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
987 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
988 "CHILD_SA not found", reqid
);
991 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
992 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
994 policy
= charon
->policies
->get_policy(charon
->policies
,
995 this->my_id
, this->other_id
,
997 this->my_host
, this->other_host
,
1001 SIG(CHILD_UP_START
, "acquiring CHILD_SA with reqid %d", reqid
);
1002 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1003 "no policy found", reqid
);
1007 switch (this->state
)
1011 ike_sa_init_t
*ike_sa_init
;
1013 connection
= charon
->connections
->get_connection_by_hosts(
1014 charon
->connections
, this->my_host
, this->other_host
);
1016 if (connection
== NULL
)
1018 SIG(CHILD_UP_START
, "acquiring CHILD_SA with reqid %d", reqid
);
1019 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1020 "no connection found to establsih IKE_SA", reqid
);
1021 policy
->destroy(policy
);
1025 DBG1(DBG_IKE
, "establishing IKE_SA to acquire CHILD_SA "
1026 "with reqid %d", reqid
);
1028 this->message_id_out
= 1;
1029 ike_sa_init
= ike_sa_init_create(&this->public);
1030 ike_sa_init
->set_config(ike_sa_init
, connection
, policy
);
1031 /* reuse existing reqid */
1032 ike_sa_init
->set_reqid(ike_sa_init
, reqid
);
1033 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
1035 case IKE_CONNECTING
:
1036 case IKE_ESTABLISHED
:
1038 create_child_sa_t
*create_child
;
1040 DBG1(DBG_CHD
, "acquiring CHILD_SA with reqid %d", reqid
);
1042 create_child
= create_child_sa_create(&this->public);
1043 create_child
->set_policy(create_child
, policy
);
1044 /* reuse existing reqid */
1045 create_child
->set_reqid(create_child
, reqid
);
1046 return queue_transaction(this, (transaction_t
*)create_child
, FALSE
);
1055 * compare two lists of traffic selectors for equality
1057 static bool ts_list_equals(linked_list_t
*l1
, linked_list_t
*l2
)
1060 iterator_t
*i1
, *i2
;
1061 traffic_selector_t
*t1
, *t2
;
1063 if (l1
->get_count(l1
) != l2
->get_count(l2
))
1068 i1
= l1
->create_iterator(l1
, TRUE
);
1069 i2
= l2
->create_iterator(l2
, TRUE
);
1070 while (i1
->iterate(i1
, (void**)&t1
) && i2
->iterate(i2
, (void**)&t2
))
1072 if (!t1
->equals(t1
, t2
))
1084 * Implementation of ike_sa_t.route.
1086 static status_t
route(private_ike_sa_t
*this, connection_t
*connection
, policy_t
*policy
)
1088 child_sa_t
*child_sa
= NULL
;
1089 iterator_t
*iterator
;
1090 linked_list_t
*my_ts
, *other_ts
;
1093 SIG(CHILD_ROUTE_START
, "routing CHILD_SA");
1095 /* check if not already routed*/
1096 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1097 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1099 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1101 linked_list_t
*my_ts_conf
, *other_ts_conf
;
1103 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1104 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1106 my_ts_conf
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1107 other_ts_conf
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1109 if (ts_list_equals(my_ts
, my_ts_conf
) &&
1110 ts_list_equals(other_ts
, other_ts_conf
))
1112 iterator
->destroy(iterator
);
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
));
1115 SIG(CHILD_ROUTE_FAILED
, "CHILD_SA with such a policy already routed");
1118 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1119 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1122 iterator
->destroy(iterator
);
1124 switch (this->state
)
1127 case IKE_CONNECTING
:
1128 /* we update IKE_SA information as good as possible,
1129 * this allows us to set up the SA later when an acquire comes in. */
1130 if (this->my_id
->get_type(this->my_id
) == ID_ANY
)
1132 this->my_id
->destroy(this->my_id
);
1133 this->my_id
= policy
->get_my_id(policy
);
1134 this->my_id
= this->my_id
->clone(this->my_id
);
1136 if (this->other_id
->get_type(this->other_id
) == ID_ANY
)
1138 this->other_id
->destroy(this->other_id
);
1139 this->other_id
= policy
->get_other_id(policy
);
1140 this->other_id
= this->other_id
->clone(this->other_id
);
1142 if (this->my_host
->is_anyaddr(this->my_host
))
1144 this->my_host
->destroy(this->my_host
);
1145 this->my_host
= connection
->get_my_host(connection
);
1146 this->my_host
= this->my_host
->clone(this->my_host
);
1148 if (this->other_host
->is_anyaddr(this->other_host
))
1150 this->other_host
->destroy(this->other_host
);
1151 this->other_host
= connection
->get_other_host(connection
);
1152 this->other_host
= this->other_host
->clone(this->other_host
);
1154 set_name(this, connection
->get_name(connection
));
1155 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
1156 this->dpd_delay
= connection
->get_dpd_delay(connection
);
1158 case IKE_ESTABLISHED
:
1160 /* nothing to do. We allow it for rekeying, as it will be
1161 * adopted by the new IKE_SA */
1164 /* TODO: hanlde this case, create a new IKE_SA and route CHILD_SA */
1165 SIG(CHILD_ROUTE_FAILED
, "unable to route CHILD_SA, as its IKE_SA gets deleted");
1169 child_sa
= child_sa_create(0, this->my_host
, this->other_host
,
1170 this->my_id
, this->other_id
,
1172 NULL
, policy
->get_hostaccess(policy
),
1174 child_sa
->set_name(child_sa
, policy
->get_name(policy
));
1175 my_ts
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1176 other_ts
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1177 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
);
1178 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1179 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1180 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1181 SIG(CHILD_ROUTE_SUCCESS
, "CHILD_SA routed");
1186 * Implementation of ike_sa_t.unroute.
1188 static status_t
unroute(private_ike_sa_t
*this, policy_t
*policy
)
1190 iterator_t
*iterator
;
1191 child_sa_t
*child_sa
= NULL
;
1193 linked_list_t
*my_ts
, *other_ts
, *my_ts_conf
, *other_ts_conf
;
1195 SIG(CHILD_UNROUTE_START
, "unrouting CHILD_SA");
1197 /* find CHILD_SA in ROUTED state */
1198 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1199 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1201 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1203 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1204 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1206 my_ts_conf
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1207 other_ts_conf
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1209 if (ts_list_equals(my_ts
, my_ts_conf
) &&
1210 ts_list_equals(other_ts
, other_ts_conf
))
1212 iterator
->remove(iterator
);
1213 SIG(CHILD_UNROUTE_SUCCESS
, "CHILD_SA unrouted");
1214 child_sa
->destroy(child_sa
);
1215 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1216 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1220 my_ts_conf
->destroy_offset(my_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1221 other_ts_conf
->destroy_offset(other_ts_conf
, offsetof(traffic_selector_t
, destroy
));
1224 iterator
->destroy(iterator
);
1228 SIG(CHILD_UNROUTE_FAILED
, "CHILD_SA to unroute not found");
1231 /* if we are not established, and we have no more routed childs, remove whole SA */
1232 if (this->state
== IKE_CREATED
&&
1233 this->child_sas
->get_count(this->child_sas
) == 0)
1241 * Implementation of ike_sa_t.send_dpd
1243 static status_t
send_dpd(private_ike_sa_t
*this)
1245 send_dpd_job_t
*job
;
1248 if (this->dpd_delay
== 0)
1254 if (this->transaction_out
)
1256 /* there is a transaction in progress. Come back later */
1261 /* check if there was any inbound traffic */
1262 time_t last_in
, now
;
1263 last_in
= get_time_inbound(this);
1265 diff
= now
- last_in
;
1266 if (diff
>= this->dpd_delay
)
1268 /* to long ago, initiate dead peer detection */
1269 dead_peer_detection_t
*dpd
;
1270 DBG1(DBG_IKE
, "sending DPD request");
1271 dpd
= dead_peer_detection_create(&this->public);
1272 queue_transaction(this, (transaction_t
*)dpd
, FALSE
);
1276 /* recheck in "interval" seconds */
1277 job
= send_dpd_job_create(this->ike_sa_id
);
1278 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
1279 (this->dpd_delay
- diff
) * 1000);
1284 * Implementation of ike_sa_t.send_keepalive
1286 static void send_keepalive(private_ike_sa_t
*this)
1288 send_keepalive_job_t
*job
;
1289 time_t last_out
, now
, diff
, interval
;
1291 last_out
= get_time_outbound(this);
1294 diff
= now
- last_out
;
1295 interval
= charon
->configuration
->get_keepalive_interval(charon
->configuration
);
1297 if (diff
>= interval
)
1302 packet
= packet_create();
1303 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
1304 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
1305 data
.ptr
= malloc(1);
1308 packet
->set_data(packet
, data
);
1309 charon
->send_queue
->add(charon
->send_queue
, packet
);
1310 DBG1(DBG_IKE
, "sending keep alive");
1313 job
= send_keepalive_job_create(this->ike_sa_id
);
1314 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
1315 (interval
- diff
) * 1000);
1319 * Implementation of ike_sa_t.get_state.
1321 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
1327 * Implementation of ike_sa_t.set_state.
1329 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
1331 DBG1(DBG_IKE
, "IKE_SA state change: %N => %N",
1332 ike_sa_state_names
, this->state
,
1333 ike_sa_state_names
, state
);
1335 if (state
== IKE_ESTABLISHED
)
1337 this->time
.established
= time(NULL
);
1338 /* start DPD checks */
1342 this->state
= state
;
1346 * Implementation of ike_sa_t.get_prf.
1348 static prf_t
*get_prf(private_ike_sa_t
*this)
1354 * Implementation of ike_sa_t.get_prf.
1356 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1358 return this->child_prf
;
1362 * Implementation of ike_sa_t.get_auth_bild
1364 static prf_t
*get_auth_build(private_ike_sa_t
*this)
1366 return this->auth_build
;
1370 * Implementation of ike_sa_t.get_auth_verify
1372 static prf_t
*get_auth_verify(private_ike_sa_t
*this)
1374 return this->auth_verify
;
1378 * Implementation of ike_sa_t.get_id.
1380 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1382 return this->ike_sa_id
;
1386 * Implementation of ike_sa_t.get_my_id.
1388 static identification_t
* get_my_id(private_ike_sa_t
*this)
1394 * Implementation of ike_sa_t.set_my_id.
1396 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1398 DESTROY_IF(this->my_id
);
1403 * Implementation of ike_sa_t.get_other_id.
1405 static identification_t
* get_other_id(private_ike_sa_t
*this)
1407 return this->other_id
;
1411 * Implementation of ike_sa_t.set_other_id.
1413 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1415 DESTROY_IF(this->other_id
);
1416 this->other_id
= other
;
1420 * Implementation of ike_sa_t.derive_keys.
1422 static status_t
derive_keys(private_ike_sa_t
*this,
1423 proposal_t
*proposal
, diffie_hellman_t
*dh
,
1424 chunk_t nonce_i
, chunk_t nonce_r
,
1425 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1427 prf_plus_t
*prf_plus
;
1428 chunk_t skeyseed
, secret
, key
, nonces
, prf_plus_seed
;
1431 crypter_t
*crypter_i
, *crypter_r
;
1432 signer_t
*signer_i
, *signer_r
;
1433 prf_t
*prf_i
, *prf_r
;
1434 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1435 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1436 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1438 /* Create SAs general purpose PRF first, we may use it here */
1439 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
1441 DBG1(DBG_IKE
, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1444 this->prf
= prf_create(algo
->algorithm
);
1445 if (this->prf
== NULL
)
1447 DBG1(DBG_IKE
, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1448 "%N not supported!", pseudo_random_function_names
, algo
->algorithm
);
1452 dh
->get_shared_secret(dh
, &secret
);
1453 DBG4(DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1454 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1455 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1456 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1457 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1459 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1461 * if we are rekeying, SKEYSEED is built on another way
1463 if (child_prf
== NULL
) /* not rekeying */
1465 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1466 this->prf
->set_key(this->prf
, nonces
);
1467 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1468 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1469 this->prf
->set_key(this->prf
, skeyseed
);
1470 chunk_free(&skeyseed
);
1471 chunk_free(&secret
);
1472 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1476 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1477 * use OLD SAs PRF functions for both prf_plus and prf */
1478 secret
= chunk_cat("mc", secret
, nonces
);
1479 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1480 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1481 old_prf
->set_key(old_prf
, skeyseed
);
1482 chunk_free(&skeyseed
);
1483 chunk_free(&secret
);
1484 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1486 chunk_free(&nonces
);
1487 chunk_free(&prf_plus_seed
);
1489 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1491 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1492 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1493 this->child_prf
= prf_create(algo
->algorithm
);
1494 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1495 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1496 DBG4(DBG_IKE
, "Sk_d secret %B", &key
);
1497 this->child_prf
->set_key(this->child_prf
, key
);
1500 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1501 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1503 DBG1(DBG_IKE
, "key derivation failed: no INTEGRITY_ALGORITHM");
1506 signer_i
= signer_create(algo
->algorithm
);
1507 signer_r
= signer_create(algo
->algorithm
);
1508 if (signer_i
== NULL
|| signer_r
== NULL
)
1510 DBG1(DBG_IKE
, "key derivation failed: INTEGRITY_ALGORITHM "
1511 "%N not supported!", integrity_algorithm_names
,algo
->algorithm
);
1514 key_size
= signer_i
->get_key_size(signer_i
);
1516 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1517 DBG4(DBG_IKE
, "Sk_ai secret %B", &key
);
1518 signer_i
->set_key(signer_i
, key
);
1521 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1522 DBG4(DBG_IKE
, "Sk_ar secret %B", &key
);
1523 signer_r
->set_key(signer_r
, key
);
1528 this->signer_in
= signer_r
;
1529 this->signer_out
= signer_i
;
1533 this->signer_in
= signer_i
;
1534 this->signer_out
= signer_r
;
1537 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1538 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1540 DBG1(DBG_IKE
, "key derivation failed: no ENCRYPTION_ALGORITHM");
1543 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1544 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1545 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1547 DBG1(DBG_IKE
, "key derivation failed: ENCRYPTION_ALGORITHM "
1548 "%N (key size %d) not supported!",
1549 encryption_algorithm_names
, algo
->algorithm
, algo
->key_size
);
1552 key_size
= crypter_i
->get_key_size(crypter_i
);
1554 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1555 DBG4(DBG_IKE
, "Sk_ei secret %B", &key
);
1556 crypter_i
->set_key(crypter_i
, key
);
1559 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1560 DBG4(DBG_IKE
, "Sk_er secret %B", &key
);
1561 crypter_r
->set_key(crypter_r
, key
);
1566 this->crypter_in
= crypter_r
;
1567 this->crypter_out
= crypter_i
;
1571 this->crypter_in
= crypter_i
;
1572 this->crypter_out
= crypter_r
;
1575 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1576 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1577 prf_i
= prf_create(algo
->algorithm
);
1578 prf_r
= prf_create(algo
->algorithm
);
1580 key_size
= prf_i
->get_key_size(prf_i
);
1581 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1582 DBG4(DBG_IKE
, "Sk_pi secret %B", &key
);
1583 prf_i
->set_key(prf_i
, key
);
1586 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1587 DBG4(DBG_IKE
, "Sk_pr secret %B", &key
);
1588 prf_r
->set_key(prf_r
, key
);
1593 this->auth_verify
= prf_r
;
1594 this->auth_build
= prf_i
;
1598 this->auth_verify
= prf_i
;
1599 this->auth_build
= prf_r
;
1602 /* all done, prf_plus not needed anymore */
1603 prf_plus
->destroy(prf_plus
);
1609 * Implementation of ike_sa_t.add_child_sa.
1611 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1613 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1617 * Implementation of ike_sa_t.has_child_sa.
1619 static bool has_child_sa(private_ike_sa_t
*this, u_int32_t reqid
)
1621 iterator_t
*iterator
;
1622 child_sa_t
*current
;
1625 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1626 while (iterator
->iterate(iterator
, (void**)¤t
))
1628 if (current
->get_reqid(current
) == reqid
)
1634 iterator
->destroy(iterator
);
1639 * Implementation of ike_sa_t.get_child_sa.
1641 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1642 u_int32_t spi
, bool inbound
)
1644 iterator_t
*iterator
;
1645 child_sa_t
*current
, *found
= NULL
;
1647 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1648 while (iterator
->iterate(iterator
, (void**)¤t
))
1650 if (current
->get_spi(current
, inbound
) == spi
&&
1651 current
->get_protocol(current
) == protocol
)
1656 iterator
->destroy(iterator
);
1661 * Implementation of ike_sa_t.create_child_sa_iterator.
1663 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1665 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1669 * Implementation of ike_sa_t.rekey_child_sa.
1671 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1673 create_child_sa_t
*rekey
;
1674 child_sa_t
*child_sa
;
1676 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1677 if (child_sa
== NULL
)
1682 rekey
= create_child_sa_create(&this->public);
1683 rekey
->rekeys_child(rekey
, child_sa
);
1684 return queue_transaction(this, (transaction_t
*)rekey
, FALSE
);
1688 * Implementation of ike_sa_t.delete_child_sa.
1690 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1692 delete_child_sa_t
*del
;
1693 child_sa_t
*child_sa
;
1695 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1696 if (child_sa
== NULL
)
1701 del
= delete_child_sa_create(&this->public);
1702 del
->set_child_sa(del
, child_sa
);
1703 return queue_transaction(this, (transaction_t
*)del
, FALSE
);
1707 * Implementation of ike_sa_t.destroy_child_sa.
1709 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1711 iterator_t
*iterator
;
1712 child_sa_t
*child_sa
;
1713 status_t status
= NOT_FOUND
;
1715 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1716 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1718 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1719 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1721 child_sa
->destroy(child_sa
);
1722 iterator
->remove(iterator
);
1727 iterator
->destroy(iterator
);
1732 * Implementation of ike_sa_t.set_lifetimes.
1734 static void set_lifetimes(private_ike_sa_t
*this,
1735 u_int32_t soft_lifetime
, u_int32_t hard_lifetime
)
1741 this->time
.rekey
= this->time
.established
+ soft_lifetime
;
1742 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
);
1743 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
1744 soft_lifetime
* 1000);
1749 this->time
.delete = this->time
.established
+ hard_lifetime
;
1750 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
1751 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
1752 hard_lifetime
* 1000);
1757 * Implementation of ike_sa_t.rekey.
1759 static status_t
rekey(private_ike_sa_t
*this)
1761 rekey_ike_sa_t
*rekey_ike_sa
;
1763 DBG1(DBG_IKE
, "rekeying IKE_SA between %H[%D]..%H[%D]",
1764 this->my_host
, this->my_id
, this->other_host
, this->other_id
);
1766 if (this->state
!= IKE_ESTABLISHED
)
1768 SIG(IKE_REKEY_START
, "rekeying IKE_SA");
1769 SIG(IKE_REKEY_FAILED
, "unable to rekey IKE_SA in state %N",
1770 ike_sa_state_names
, this->state
);
1774 rekey_ike_sa
= rekey_ike_sa_create(&this->public);
1775 return queue_transaction(this, (transaction_t
*)rekey_ike_sa
, FALSE
);
1779 * Implementation of ike_sa_t.get_rekeying_transaction.
1781 static transaction_t
* get_rekeying_transaction(private_ike_sa_t
*this)
1783 return this->rekeying_transaction
;
1787 * Implementation of ike_sa_t.set_rekeying_transaction.
1789 static void set_rekeying_transaction(private_ike_sa_t
*this, transaction_t
*rekey
)
1791 this->rekeying_transaction
= rekey
;
1795 * Implementation of ike_sa_t.adopt_children.
1797 static void adopt_children(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1799 child_sa_t
*child_sa
;
1801 while (other
->child_sas
->remove_last(other
->child_sas
,
1802 (void**)&child_sa
) == SUCCESS
)
1804 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1809 * Implementation of public_ike_sa_t.delete.
1811 static status_t
delete_(private_ike_sa_t
*this)
1813 switch (this->state
)
1815 case IKE_CONNECTING
:
1817 /* this may happen if a half open IKE_SA gets closed after a
1818 * timeout. We signal here UP_FAILED to complete the SIG schema */
1819 SIG(IKE_UP_FAILED
, "half open IKE_SA deleted after timeout");
1822 case IKE_ESTABLISHED
:
1824 delete_ike_sa_t
*delete_ike_sa
;
1825 if (this->transaction_out
)
1827 /* already a transaction in progress. As this may hang
1828 * around a while, we don't inform the other peer. */
1831 delete_ike_sa
= delete_ike_sa_create(&this->public);
1832 return queue_transaction(this, (transaction_t
*)delete_ike_sa
, FALSE
);
1838 SIG(IKE_DOWN_START
, "closing IKE_SA");
1839 SIG(IKE_DOWN_SUCCESS
, "IKE_SA closed between %H[%D]...%H[%D]",
1840 this->my_host
, this->my_id
, this->other_host
, this->other_id
);
1847 * Implementation of ike_sa_t.get_next_message_id.
1849 static u_int32_t
get_next_message_id (private_ike_sa_t
*this)
1851 return this->message_id_out
++;
1855 * Implementation of ike_sa_t.is_natt_enabled.
1857 static bool is_natt_enabled(private_ike_sa_t
*this)
1859 return this->nat_here
|| this->nat_there
;
1863 * Implementation of ike_sa_t.enable_natt.
1865 static void enable_natt(private_ike_sa_t
*this, bool local
)
1869 DBG1(DBG_IKE
, "local host is behind NAT, using NAT-T, "
1870 "scheduled keep alives");
1871 this->nat_here
= TRUE
;
1872 send_keepalive(this);
1876 DBG1(DBG_IKE
, "remote host is behind NAT, using NAT-T");
1877 this->nat_there
= TRUE
;
1882 * output handler in printf()
1884 static int print(FILE *stream
, const struct printf_info
*info
,
1885 const void *const *args
)
1887 private_ike_sa_t
*this = *((private_ike_sa_t
**)(args
[0]));
1891 return fprintf(stream
, "(null)");
1894 return fprintf(stream
, "%10s: %N, %H[%D]...%H[%D] (%J)",
1895 this->name
, ike_sa_state_names
, this->state
,
1896 this->my_host
, this->my_id
, this->other_host
, this->other_id
,
1901 * arginfo handler in printf()
1903 static int print_arginfo(const struct printf_info
*info
, size_t n
, int *argtypes
)
1907 argtypes
[0] = PA_POINTER
;
1913 * register printf() handlers
1915 static void __attribute__ ((constructor
))print_register()
1917 register_printf_function(IKE_SA_PRINTF_SPEC
, print
, print_arginfo
);
1921 * Implementation of ike_sa_t.destroy.
1923 static void destroy(private_ike_sa_t
*this)
1925 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
1926 this->transaction_queue
->destroy_offset(this->transaction_queue
, offsetof(transaction_t
, destroy
));
1928 DESTROY_IF(this->transaction_in
);
1929 DESTROY_IF(this->transaction_in_next
);
1930 DESTROY_IF(this->transaction_out
);
1931 DESTROY_IF(this->crypter_in
);
1932 DESTROY_IF(this->crypter_out
);
1933 DESTROY_IF(this->signer_in
);
1934 DESTROY_IF(this->signer_out
);
1935 DESTROY_IF(this->prf
);
1936 DESTROY_IF(this->child_prf
);
1937 DESTROY_IF(this->auth_verify
);
1938 DESTROY_IF(this->auth_build
);
1940 DESTROY_IF(this->my_host
);
1941 DESTROY_IF(this->other_host
);
1942 DESTROY_IF(this->my_id
);
1943 DESTROY_IF(this->other_id
);
1946 this->ike_sa_id
->destroy(this->ike_sa_id
);
1951 * Described in header.
1953 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1955 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
1957 /* Public functions */
1958 this->public.get_state
= (ike_sa_state_t(*)(ike_sa_t
*)) get_state
;
1959 this->public.set_state
= (void(*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
1960 this->public.get_name
= (char*(*)(ike_sa_t
*))get_name
;
1961 this->public.set_name
= (void(*)(ike_sa_t
*,char*))set_name
;
1962 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
1963 this->public.initiate
= (status_t(*)(ike_sa_t
*,connection_t
*,policy_t
*)) initiate
;
1964 this->public.route
= (status_t(*)(ike_sa_t
*,connection_t
*,policy_t
*)) route
;
1965 this->public.unroute
= (status_t(*)(ike_sa_t
*,policy_t
*)) unroute
;
1966 this->public.acquire
= (status_t(*)(ike_sa_t
*,u_int32_t
)) acquire
;
1967 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
1968 this->public.get_my_host
= (host_t
*(*)(ike_sa_t
*)) get_my_host
;
1969 this->public.set_my_host
= (void(*)(ike_sa_t
*,host_t
*)) set_my_host
;
1970 this->public.get_other_host
= (host_t
*(*)(ike_sa_t
*)) get_other_host
;
1971 this->public.set_other_host
= (void(*)(ike_sa_t
*,host_t
*)) set_other_host
;
1972 this->public.get_my_id
= (identification_t
*(*)(ike_sa_t
*)) get_my_id
;
1973 this->public.set_my_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_my_id
;
1974 this->public.get_other_id
= (identification_t
*(*)(ike_sa_t
*)) get_other_id
;
1975 this->public.set_other_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_other_id
;
1976 this->public.get_next_message_id
= (u_int32_t(*)(ike_sa_t
*)) get_next_message_id
;
1977 this->public.retransmit_request
= (status_t (*) (ike_sa_t
*, u_int32_t
)) retransmit_request
;
1978 this->public.delete = (status_t(*)(ike_sa_t
*))delete_
;
1979 this->public.destroy
= (void(*)(ike_sa_t
*))destroy
;
1980 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
1981 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
1982 this->public.get_prf
= (prf_t
*(*) (ike_sa_t
*)) get_prf
;
1983 this->public.get_child_prf
= (prf_t
*(*) (ike_sa_t
*)) get_child_prf
;
1984 this->public.get_auth_verify
= (prf_t
*(*) (ike_sa_t
*)) get_auth_verify
;
1985 this->public.get_auth_build
= (prf_t
*(*) (ike_sa_t
*)) get_auth_build
;
1986 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
;
1987 this->public.add_child_sa
= (void (*) (ike_sa_t
*,child_sa_t
*)) add_child_sa
;
1988 this->public.has_child_sa
= (bool(*)(ike_sa_t
*,u_int32_t
)) has_child_sa
;
1989 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
1990 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
1991 this->public.rekey_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
1992 this->public.delete_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
1993 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
1994 this->public.enable_natt
= (void(*)(ike_sa_t
*, bool)) enable_natt
;
1995 this->public.is_natt_enabled
= (bool(*)(ike_sa_t
*)) is_natt_enabled
;
1996 this->public.set_lifetimes
= (void(*)(ike_sa_t
*,u_int32_t
,u_int32_t
))set_lifetimes
;
1997 this->public.apply_connection
= (void(*)(ike_sa_t
*,connection_t
*))apply_connection
;
1998 this->public.rekey
= (status_t(*)(ike_sa_t
*))rekey
;
1999 this->public.get_rekeying_transaction
= (transaction_t
*(*)(ike_sa_t
*))get_rekeying_transaction
;
2000 this->public.set_rekeying_transaction
= (void(*)(ike_sa_t
*,transaction_t
*))set_rekeying_transaction
;
2001 this->public.adopt_children
= (void(*)(ike_sa_t
*,ike_sa_t
*))adopt_children
;
2003 /* initialize private fields */
2004 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2005 this->name
= strdup("(uninitialized)");
2006 this->child_sas
= linked_list_create();
2007 this->my_host
= host_create_from_string("0.0.0.0", 0);
2008 this->other_host
= host_create_from_string("0.0.0.0", 0);
2009 this->my_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
2010 this->other_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
2011 this->crypter_in
= NULL
;
2012 this->crypter_out
= NULL
;
2013 this->signer_in
= NULL
;
2014 this->signer_out
= NULL
;
2016 this->auth_verify
= NULL
;
2017 this->auth_build
= NULL
;
2018 this->child_prf
= NULL
;
2019 this->nat_here
= FALSE
;
2020 this->nat_there
= FALSE
;
2021 this->transaction_queue
= linked_list_create();
2022 this->transaction_in
= NULL
;
2023 this->transaction_in_next
= NULL
;
2024 this->transaction_out
= NULL
;
2025 this->rekeying_transaction
= NULL
;
2026 this->state
= IKE_CREATED
;
2027 this->message_id_out
= 0;
2028 /* set to NOW, as when we rekey an existing IKE_SA no message is exchanged
2029 * and inbound therefore uninitialized */
2030 this->time
.inbound
= this->time
.outbound
= time(NULL
);
2031 this->time
.established
= 0;
2032 this->time
.rekey
= 0;
2033 this->time
.delete = 0;
2034 this->dpd_delay
= 0;
2035 this->retrans_sequences
= 0;
2037 return &this->public;