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
32 #include <definitions.h>
33 #include <utils/linked_list.h>
34 #include <utils/logger_manager.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>
62 * String mappings for ike_sa_state_t.
64 mapping_t ike_sa_state_m
[] = {
65 {IKE_CREATED
, "CREATED"},
66 {IKE_CONNECTING
, "CONNECTING"},
67 {IKE_ESTABLISHED
, "ESTABLISHED"},
68 {IKE_REKEYING
, "REKEYING"},
69 {IKE_DELETING
, "DELETING"},
74 typedef struct private_ike_sa_t private_ike_sa_t
;
77 * Private data of an ike_sa_t object.
79 struct private_ike_sa_t
{
87 * Identifier for the current IKE_SA.
89 ike_sa_id_t
*ike_sa_id
;
92 * Current state of the IKE_SA
97 * Name of the connection used by this IKE_SA
102 * Address of local host
107 * Address of remote host
112 * Identification used for us
114 identification_t
*my_id
;
117 * Identification used for other
119 identification_t
*other_id
;
122 * Linked List containing the child sa's of the current IKE_SA.
124 linked_list_t
*child_sas
;
127 * crypter for inbound traffic
129 crypter_t
*crypter_in
;
132 * crypter for outbound traffic
134 crypter_t
*crypter_out
;
137 * Signer for inbound traffic
142 * Signer for outbound traffic
144 signer_t
*signer_out
;
147 * Multi purpose prf, set key, use it, forget it
152 * Prf function for derivating keymat child SAs
157 * PRF, with key set to pi_key, used for authentication
162 * PRF, with key set to pr_key, used for authentication
167 * A logger for this IKE_SA.
174 hasher_t
*nat_hasher
;
177 * NAT status of local host.
182 * NAT status of remote host.
187 * message ID for next outgoung request
189 u_int32_t message_id_out
;
192 * Timestamps for this IKE_SA
195 /** last IKE message received */
197 /** last IKE message sent */
199 /** when IKE_SA became established */
200 u_int32_t established
;
201 /** when IKE_SA gets rekeyed */
203 /** when IKE_SA gets deleted */
208 * interval to send DPD liveness check
213 * number of retransmit sequences to go through before giving up (keyingtries)
215 u_int32_t retrans_sequences
;
218 * List of queued transactions to process
220 linked_list_t
*transaction_queue
;
223 * Transaction currently initiated
224 * (only one supported yet, window size = 1)
226 transaction_t
*transaction_out
;
229 * last transaction initiated by peer processed.
230 * (only one supported yet, window size = 1)
231 * Stored for retransmission.
233 transaction_t
*transaction_in
;
236 * Next incoming transaction expected. Used to
237 * do multi transaction operations.
239 transaction_t
*transaction_in_next
;
242 * Transaction which rekeys this IKE_SA, used do detect simultaneus rekeying
244 rekey_ike_sa_t
*rekeying_transaction
;
248 * get the time of the latest traffic processed by the kernel
250 static time_t get_kernel_time(private_ike_sa_t
* this, bool inbound
)
252 iterator_t
*iterator
;
253 child_sa_t
*child_sa
;
254 time_t latest
= 0, use_time
;
256 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
257 while (iterator
->iterate(iterator
, (void**)&child_sa
))
259 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
261 latest
= max(latest
, use_time
);
264 iterator
->destroy(iterator
);
270 * get the time of the latest received traffice
272 static time_t get_time_inbound(private_ike_sa_t
*this)
274 return max(this->time
.inbound
, get_kernel_time(this, TRUE
));
278 * get the time of the latest sent traffic
280 static time_t get_time_outbound(private_ike_sa_t
*this)
282 return max(this->time
.outbound
, get_kernel_time(this, FALSE
));
286 * Implementation of ike_sa_t.get_name.
288 static char *get_name(private_ike_sa_t
*this)
294 * Implementation of ike_sa_t.set_name.
296 static void set_name(private_ike_sa_t
*this, char* name
)
299 this->name
= strdup(name
);
303 * Implementation of ike_sa_t.set_dpd_delay.
305 static void set_dpd_delay(private_ike_sa_t
*this, u_int32_t delay
)
307 this->dpd_delay
= delay
;
311 * Implementation of ike_sa_t.get_my_host.
313 static host_t
*get_my_host(private_ike_sa_t
*this)
315 return this->my_host
;
319 * Implementation of ike_sa_t.set_my_host.
321 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
323 DESTROY_IF(this->my_host
);
328 * Implementation of ike_sa_t.get_other_host.
330 static host_t
*get_other_host(private_ike_sa_t
*this)
332 return this->other_host
;
336 * Implementation of ike_sa_t.set_other_host.
338 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
340 DESTROY_IF(this->other_host
);
341 this->other_host
= other
;
345 * Update connection host, as addresses may change (NAT)
347 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
352 * 2.11. Address and Port Agility
354 * IKE runs over UDP ports 500 and 4500, and implicitly sets up ESP and
355 * AH associations for the same IP addresses it runs over. The IP
356 * addresses and ports in the outer header are, however, not themselves
357 * cryptographically protected, and IKE is designed to work even through
358 * Network Address Translation (NAT) boxes. An implementation MUST
359 * accept incoming requests even if the source port is not 500 or 4500,
360 * and MUST respond to the address and port from which the request was
361 * received. It MUST specify the address and port at which the request
362 * was received as the source address and port in the response. IKE
363 * functions identically over IPv4 or IPv6.
367 * There are cases where a NAT box decides to remove mappings that
368 * are still alive (for example, the keepalive interval is too long,
369 * or the NAT box is rebooted). To recover in these cases, hosts
370 * that are not behind a NAT SHOULD send all packets (including
371 * retransmission packets) to the IP address and port from the last
372 * valid authenticated packet from the other end (i.e., dynamically
373 * update the address). A host behind a NAT SHOULD NOT do this
374 * because it opens a DoS attack possibility. Any authenticated IKE
375 * packet or any authenticated UDP-encapsulated ESP packet can be
376 * used to detect that the IP address or the port has changed.
378 iterator_t
*iterator
= NULL
;
379 child_sa_t
*child_sa
= NULL
;
380 host_diff_t my_diff
, other_diff
;
382 if (this->my_host
->is_anyaddr(this->my_host
) ||
383 this->other_host
->is_anyaddr(this->other_host
))
385 /* on first received message */
386 this->my_host
->destroy(this->my_host
);
387 this->my_host
= me
->clone(me
);
388 this->other_host
->destroy(this->other_host
);
389 this->other_host
= other
->clone(other
);
393 my_diff
= me
->get_differences(me
, this->my_host
);
394 other_diff
= other
->get_differences(other
, this->other_host
);
396 if (!my_diff
&& !other_diff
)
403 this->my_host
->destroy(this->my_host
);
404 this->my_host
= me
->clone(me
);
409 /* update without restrictions if we are not NATted */
412 this->other_host
->destroy(this->other_host
);
413 this->other_host
= other
->clone(other
);
418 /* if we are natted, only port may change */
419 if (other_diff
& HOST_DIFF_ADDR
)
423 else if (other_diff
& HOST_DIFF_PORT
)
425 this->other_host
->set_port(this->other_host
, other
->get_port(other
));
428 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
429 while (iterator
->iterate(iterator
, (void**)&child_sa
))
431 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
432 my_diff
, other_diff
);
433 /* TODO: what to do if update fails? Delete CHILD_SA? */
435 iterator
->destroy(iterator
);
439 * called when the peer is not responding anymore
441 static void dpd_detected(private_ike_sa_t
*this)
443 /* check for childrens with dpdaction=hold */
444 connection_t
*connection
= NULL
;
446 linked_list_t
*my_ts
, *other_ts
;
447 child_sa_t
* child_sa
;
451 this->logger
->log(this->logger
, CONTROL
|LEVEL1
,
452 "dead peer detected, handling CHILD_SAs dpd action");
454 while(this->child_sas
->remove_first(this->child_sas
,
455 (void**)&child_sa
) == SUCCESS
)
457 /* get the policy which belongs to this CHILD */
458 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
459 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
460 policy
= charon
->policies
->get_policy(charon
->policies
,
461 this->my_id
, this->other_id
,
463 this->my_host
, this->other_host
);
466 this->logger
->log(this->logger
, ERROR
,
467 "no policy found for this CHILD_SA");
471 action
= policy
->get_dpd_action(policy
);
472 /* get a connection for further actions */
473 if (connection
== NULL
&&
474 (action
== DPD_ROUTE
|| action
== DPD_RESTART
))
476 connection
= charon
->connections
->get_connection_by_hosts(
478 this->my_host
, this->other_host
);
479 if (connection
== NULL
)
481 this->logger
->log(this->logger
, ERROR
,
482 "no connection found for this IKE_SA");
487 this->logger
->log(this->logger
, CONTROL
, "dpd action for %s is %s",
488 policy
->get_name(policy
),
489 enum_name(&dpd_action_names
, action
));
494 connection
->get_ref(connection
);
495 job
= (job_t
*)route_job_create(connection
, policy
, TRUE
);
496 charon
->job_queue
->add(charon
->job_queue
, job
);
499 connection
->get_ref(connection
);
500 job
= (job_t
*)initiate_job_create(connection
, policy
);
501 charon
->job_queue
->add(charon
->job_queue
, job
);
504 policy
->destroy(policy
);
507 child_sa
->destroy(child_sa
);
509 DESTROY_IF(connection
);
513 * send a request and schedule retransmission
515 static status_t
transmit_request(private_ike_sa_t
*this)
520 retransmit_request_job_t
*job
;
521 u_int32_t transmitted
;
523 transaction_t
*transaction
= this->transaction_out
;
524 u_int32_t message_id
;
526 transmitted
= transaction
->requested(transaction
);
527 timeout
= charon
->configuration
->get_retransmit_timeout(charon
->configuration
,
529 this->retrans_sequences
);
532 this->logger
->log(this->logger
, ERROR
,
533 "giving up after %d retransmits, deleting IKE_SA",
539 status
= transaction
->get_request(transaction
, &request
);
540 if (status
!= SUCCESS
)
542 this->logger
->log(this->logger
, ERROR
,
543 "generating request failed");
546 message_id
= transaction
->get_message_id(transaction
);
547 /* if we retransmit, the request is already generated */
548 if (transmitted
== 0)
550 status
= request
->generate(request
, this->crypter_out
, this->signer_out
, &packet
);
551 if (status
!= SUCCESS
)
553 this->logger
->log(this->logger
, ERROR
,
554 "request generation failed. transaction discarded");
560 this->logger
->log(this->logger
, CONTROL
,
561 "sending retransmit %d for %s request with message ID %d",
563 mapping_find(exchange_type_m
, request
->get_exchange_type(request
)),
565 packet
= request
->get_packet(request
);
568 charon
->send_queue
->add(charon
->send_queue
, packet
);
569 this->time
.outbound
= time(NULL
);
571 /* schedule retransmission job */
572 job
= retransmit_request_job_create(message_id
, this->ike_sa_id
);
573 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
, timeout
);
578 * Implementation of ike_sa.retransmit_request.
580 static status_t
retransmit_request(private_ike_sa_t
*this, u_int32_t message_id
)
582 if (this->transaction_out
== NULL
||
583 this->transaction_out
->get_message_id(this->transaction_out
) != message_id
)
585 /* no retransmit necessary, transaction did already complete */
588 return transmit_request(this);
592 * Check for transactions in the queue and initiate the first transaction found.
594 static status_t
process_transaction_queue(private_ike_sa_t
*this)
596 if (this->transaction_out
)
598 /* already a transaction in progress */
604 if (this->transaction_queue
->remove_first(this->transaction_queue
,
605 (void**)&this->transaction_out
) != SUCCESS
)
607 /* transaction queue empty */
610 switch (transmit_request(this))
615 /* critical, IKE_SA unusable, destroy immediately */
616 this->logger
->log(this->logger
, ERROR
,
617 "transaction initiaton failed, deleting IKE_SA");
620 /* discard transaction, process next one */
621 this->logger
->log(this->logger
, ERROR
,
622 "transaction initiation failed, discarded");
623 this->transaction_out
->destroy(this->transaction_out
);
624 this->transaction_out
= NULL
;
625 /* handle next transaction */
632 * Queue a new transaction and execute the next outstanding transaction
634 static status_t
queue_transaction(private_ike_sa_t
*this, transaction_t
*transaction
, bool prefer
)
636 /* inject next transaction */
641 this->transaction_queue
->insert_first(this->transaction_queue
, transaction
);
645 this->transaction_queue
->insert_last(this->transaction_queue
, transaction
);
648 /* process a transaction */
649 return process_transaction_queue(this);
653 * process an incoming request.
655 static status_t
process_request(private_ike_sa_t
*this, message_t
*request
)
657 transaction_t
*last
, *current
= NULL
;
660 u_int32_t request_mid
;
663 request_mid
= request
->get_message_id(request
);
664 last
= this->transaction_in
;
666 /* check if message ID is correct */
669 u_int32_t last_mid
= last
->get_message_id(last
);
671 if (last_mid
== request_mid
)
673 /* retransmit detected */
674 this->logger
->log(this->logger
, ERROR
,
675 "received retransmitted request for message ID %d, retransmitting response",
677 last
->get_response(last
, request
, &response
, &this->transaction_in_next
);
678 packet
= response
->get_packet(response
);
679 charon
->send_queue
->add(charon
->send_queue
, packet
);
680 this->time
.outbound
= time(NULL
);
684 if (last_mid
> request_mid
)
686 /* something seriously wrong here, message id may not decrease */
687 this->logger
->log(this->logger
, ERROR
,
688 "received request with message ID %d, excepted %d, ingored",
689 request_mid
, last_mid
+ 1);
692 /* we allow jumps in message IDs, as long as they are incremental */
693 if (last_mid
+ 1 < request_mid
)
695 this->logger
->log(this->logger
, ERROR
,
696 "received request with message ID %d, excepted %d",
697 request_mid
, last_mid
+ 1);
702 if (request_mid
!= 0)
704 /* warn, but allow it */
705 this->logger
->log(this->logger
, CONTROL
,
706 "first received request has message ID %d, excepted 0",
711 /* check if we already have a pre-created transaction for this request */
712 if (this->transaction_in_next
)
714 current
= this->transaction_in_next
;
715 this->transaction_in_next
= NULL
;
719 current
= transaction_create(&this->public, request
);
722 this->logger
->log(this->logger
, ERROR
,
723 "no idea how to handle received message (%d), ignored",
724 request
->get_exchange_type(request
));
729 /* send message. get_request() always gives a valid response */
730 status
= current
->get_response(current
, request
, &response
, &this->transaction_in_next
);
731 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
733 this->logger
->log(this->logger
, ERROR
,
734 "response generation failed, discarding transaction");
735 current
->destroy(current
);
739 charon
->send_queue
->add(charon
->send_queue
, packet
);
740 this->time
.outbound
= time(NULL
);
741 /* act depending on transaction result */
745 /* transactions says we should destroy the IKE_SA, so do it */
746 current
->destroy(current
);
749 /* store for retransmission, destroy old transaction */
750 this->transaction_in
= current
;
760 * process an incoming response
762 static status_t
process_response(private_ike_sa_t
*this, message_t
*response
)
764 transaction_t
*current
, *new = NULL
;
766 current
= this->transaction_out
;
767 /* check if message ID is that of our currently active transaction */
768 if (current
== NULL
||
769 current
->get_message_id(current
) != response
->get_message_id(response
))
771 this->logger
->log(this->logger
, ERROR
,
772 "received response with message ID %d not requested, ignored");
776 switch (current
->conclude(current
, response
, &new))
779 /* state requested to destroy IKE_SA */
782 /* discard transaction, process next one */
785 /* transaction comleted, remove */
786 current
->destroy(current
);
787 this->transaction_out
= NULL
;
789 /* queue new transaction */
790 return queue_transaction(this, new, TRUE
);
794 * send a notify back to the sender
796 static void send_notify_response(private_ike_sa_t
*this,
800 notify_payload_t
*notify
;
805 response
= message_create();
806 dst
= request
->get_source(request
);
807 src
= request
->get_destination(request
);
808 response
->set_source(response
, src
->clone(src
));
809 response
->set_destination(response
, dst
->clone(dst
));
810 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
811 response
->set_request(response
, FALSE
);
812 response
->set_message_id(response
, request
->get_message_id(request
));
813 response
->set_ike_sa_id(response
, this->ike_sa_id
);
814 notify
= notify_payload_create_from_protocol_and_type(PROTO_NONE
, type
);
815 response
->add_payload(response
, (payload_t
*)notify
);
816 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
818 response
->destroy(response
);
821 charon
->send_queue
->add(charon
->send_queue
, packet
);
822 this->time
.outbound
= time(NULL
);
823 response
->destroy(response
);
829 * Implementation of ike_sa_t.process_message.
831 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
836 is_request
= message
->get_request(message
);
838 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
839 if (status
!= SUCCESS
)
846 this->logger
->log(this->logger
, ERROR
,
847 "ciritcal unknown payloads found");
850 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
854 this->logger
->log(this->logger
, ERROR
,
855 "message parsing failed");
858 send_notify_response(this, message
, INVALID_SYNTAX
);
862 this->logger
->log(this->logger
, ERROR
,
863 "message verification failed");
866 send_notify_response(this, message
, INVALID_SYNTAX
);
870 this->logger
->log(this->logger
, ERROR
,
871 "integrity check failed");
875 this->logger
->log(this->logger
, ERROR
,
876 "found encrypted message, but no keys available");
879 send_notify_response(this, message
, INVALID_SYNTAX
);
885 this->logger
->log(this->logger
, ERROR
,
886 "%s %s with message ID %d processing failed",
887 mapping_find(exchange_type_m
, message
->get_exchange_type(message
)),
888 message
->get_request(message
) ?
"request" : "response",
889 message
->get_message_id(message
));
893 /* check if message is trustworthy, and update connection information */
894 if (this->state
== IKE_CREATED
||
895 message
->get_exchange_type(message
) != IKE_SA_INIT
)
897 update_hosts(this, message
->get_destination(message
),
898 message
->get_source(message
));
899 this->time
.inbound
= time(NULL
);
903 status
= process_request(this, message
);
907 status
= process_response(this, message
);
914 * Implementation of ike_sa_t.initiate.
916 static status_t
initiate(private_ike_sa_t
*this,
917 connection_t
*connection
, policy_t
*policy
)
923 /* in state CREATED, we must do the ike_sa_init
924 * and ike_auth transactions. Along with these,
925 * a CHILD_SA with the supplied policy is set up.
927 ike_sa_init_t
*ike_sa_init
;
929 this->logger
->log(this->logger
, CONTROL
,
930 "initiating IKE_SA");
931 DESTROY_IF(this->my_host
);
932 this->my_host
= connection
->get_my_host(connection
);
933 this->my_host
= this->my_host
->clone(this->my_host
);
934 DESTROY_IF(this->other_host
);
935 this->other_host
= connection
->get_other_host(connection
);
936 this->other_host
= this->other_host
->clone(this->other_host
);
937 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
938 this->dpd_delay
= connection
->get_dpd_delay(connection
);
940 this->message_id_out
= 1;
941 ike_sa_init
= ike_sa_init_create(&this->public);
942 ike_sa_init
->set_config(ike_sa_init
, connection
, policy
);
943 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
948 /* if we are in DELETING/REKEYING, we deny set up of a policy. */
949 this->logger
->log(this->logger
, CONTROL
,
950 "creating CHILD_SA discarded, as IKE_SA is in state %s",
951 mapping_find(ike_sa_state_m
, this->state
));
952 policy
->destroy(policy
);
953 connection
->destroy(connection
);
957 case IKE_ESTABLISHED
:
959 /* if we are ESTABLISHED or CONNECTING,we queue the
960 * transaction to create the CHILD_SA. It gets processed
961 * when the IKE_SA is ready to do so. We don't need the
962 * connection, as the IKE_SA is already established/establishing.
964 create_child_sa_t
*create_child
;
966 this->logger
->log(this->logger
, CONTROL
,
967 "initiating CHILD_SA");
969 connection
->destroy(connection
);
970 create_child
= create_child_sa_create(&this->public);
971 create_child
->set_policy(create_child
, policy
);
972 return queue_transaction(this, (transaction_t
*)create_child
, FALSE
);
979 * Implementation of ike_sa_t.acquire.
981 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
983 connection_t
*connection
;
985 iterator_t
*iterator
;
986 child_sa_t
*current
, *child_sa
= NULL
;
987 linked_list_t
*my_ts
, *other_ts
;
989 if (this->state
== IKE_DELETING
)
991 this->logger
->log(this->logger
, CONTROL
,
992 "acquiring CHILD_SA with reqid %d discarded, as IKE_SA is deleting",
999 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1000 while (iterator
->iterate(iterator
, (void**)¤t
))
1002 if (current
->get_reqid(current
) == reqid
)
1008 iterator
->destroy(iterator
);
1011 this->logger
->log(this->logger
, ERROR
,
1012 "CHILD_SA with reqid %d not found, unable to acquire",
1016 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1017 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1019 policy
= charon
->policies
->get_policy(charon
->policies
,
1020 this->my_id
, this->other_id
,
1022 this->my_host
, this->other_host
);
1025 this->logger
->log(this->logger
, ERROR
,
1026 "no policy found to acquire CHILD_SA with reqid %d",
1031 switch (this->state
)
1035 ike_sa_init_t
*ike_sa_init
;
1037 this->logger
->log(this->logger
, CONTROL
,
1038 "acquiring CHILD_SA with reqid %d, IKE_SA setup needed",
1041 connection
= charon
->connections
->get_connection_by_hosts(
1042 charon
->connections
, this->my_host
, this->other_host
);
1044 if (connection
== NULL
)
1046 this->logger
->log(this->logger
, ERROR
,
1047 "no connection found to acquire IKE_SA for CHILD_SA with reqid %d",
1049 policy
->destroy(policy
);
1053 this->message_id_out
= 1;
1054 ike_sa_init
= ike_sa_init_create(&this->public);
1055 ike_sa_init
->set_config(ike_sa_init
, connection
, policy
);
1056 /* reuse existing reqid */
1057 ike_sa_init
->set_reqid(ike_sa_init
, reqid
);
1058 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
1060 case IKE_CONNECTING
:
1061 case IKE_ESTABLISHED
:
1063 create_child_sa_t
*create_child
;
1065 this->logger
->log(this->logger
, CONTROL
,
1066 "acquiring CHILD_SA with reqid %d",
1069 create_child
= create_child_sa_create(&this->public);
1070 create_child
->set_policy(create_child
, policy
);
1071 /* reuse existing reqid */
1072 create_child
->set_reqid(create_child
, reqid
);
1073 return queue_transaction(this, (transaction_t
*)create_child
, FALSE
);
1082 * destroy a list of traffic selectors
1084 static void ts_list_destroy(linked_list_t
*list
)
1086 traffic_selector_t
*ts
;
1087 while (list
->remove_last(list
, (void**)&ts
) == SUCCESS
)
1091 list
->destroy(list
);
1095 * compare two lists of traffic selectors for equality
1097 static bool ts_list_equals(linked_list_t
*l1
, linked_list_t
*l2
)
1100 iterator_t
*i1
, *i2
;
1101 traffic_selector_t
*t1
, *t2
;
1103 i1
= l1
->create_iterator(l1
, TRUE
);
1104 i2
= l2
->create_iterator(l2
, TRUE
);
1105 while (i1
->iterate(i1
, (void**)&t1
) && i2
->iterate(i2
, (void**)&t2
))
1107 if (!t1
->equals(t1
, t2
))
1113 /* check if one iterator is not at the end */
1114 if (i1
->has_next(i1
) || i2
->has_next(i2
))
1124 * Implementation of ike_sa_t.route.
1126 static status_t
route(private_ike_sa_t
*this, connection_t
*connection
, policy_t
*policy
)
1128 child_sa_t
*child_sa
= NULL
;
1129 iterator_t
*iterator
;
1130 linked_list_t
*my_ts
, *other_ts
;
1133 /* check if not already routed*/
1134 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1135 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1137 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1139 linked_list_t
*my_ts_conf
, *other_ts_conf
;
1141 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1142 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1144 my_ts_conf
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1145 other_ts_conf
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1147 if (ts_list_equals(my_ts
, my_ts_conf
) &&
1148 ts_list_equals(other_ts
, other_ts_conf
))
1150 ts_list_destroy(my_ts_conf
);
1151 ts_list_destroy(other_ts_conf
);
1152 iterator
->destroy(iterator
);
1153 this->logger
->log(this->logger
, CONTROL
,
1154 "a CHILD_SA with such a policy already routed");
1158 ts_list_destroy(my_ts_conf
);
1159 ts_list_destroy(other_ts_conf
);
1162 iterator
->destroy(iterator
);
1164 switch (this->state
)
1167 case IKE_CONNECTING
:
1168 /* we update IKE_SA information as good as possible,
1169 * this allows us to set up the SA later when an acquire comes in. */
1170 if (this->my_id
->get_type(this->my_id
) == ID_ANY
)
1172 this->my_id
->destroy(this->my_id
);
1173 this->my_id
= policy
->get_my_id(policy
);
1174 this->my_id
= this->my_id
->clone(this->my_id
);
1176 if (this->other_id
->get_type(this->other_id
) == ID_ANY
)
1178 this->other_id
->destroy(this->other_id
);
1179 this->other_id
= policy
->get_other_id(policy
);
1180 this->other_id
= this->other_id
->clone(this->other_id
);
1182 if (this->my_host
->is_anyaddr(this->my_host
))
1184 this->my_host
->destroy(this->my_host
);
1185 this->my_host
= connection
->get_my_host(connection
);
1186 this->my_host
= this->my_host
->clone(this->my_host
);
1188 if (this->other_host
->is_anyaddr(this->other_host
))
1190 this->other_host
->destroy(this->other_host
);
1191 this->other_host
= connection
->get_other_host(connection
);
1192 this->other_host
= this->other_host
->clone(this->other_host
);
1194 set_name(this, connection
->get_name(connection
));
1195 this->retrans_sequences
= connection
->get_retrans_seq(connection
);
1196 this->dpd_delay
= connection
->get_dpd_delay(connection
);
1198 case IKE_ESTABLISHED
:
1200 /* nothing to do. We allow it for rekeying, as it will be
1201 * adopted by the new IKE_SA */
1208 child_sa
= child_sa_create(0, this->my_host
, this->other_host
,
1209 this->my_id
, this->other_id
,
1211 NULL
, policy
->get_hostaccess(policy
),
1213 child_sa
->set_name(child_sa
, policy
->get_name(policy
));
1214 my_ts
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1215 other_ts
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1216 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
);
1217 ts_list_destroy(my_ts
);
1218 ts_list_destroy(other_ts
);
1219 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1225 * Implementation of ike_sa_t.unroute.
1227 static status_t
unroute(private_ike_sa_t
*this, policy_t
*policy
)
1229 iterator_t
*iterator
;
1230 child_sa_t
*child_sa
= NULL
;
1231 linked_list_t
*my_ts
, *other_ts
, *my_ts_conf
, *other_ts_conf
;
1233 /* find CHILD_SA in ROUTED state */
1234 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1235 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1237 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1239 my_ts
= child_sa
->get_my_traffic_selectors(child_sa
);
1240 other_ts
= child_sa
->get_other_traffic_selectors(child_sa
);
1242 my_ts_conf
= policy
->get_my_traffic_selectors(policy
, this->my_host
);
1243 other_ts_conf
= policy
->get_other_traffic_selectors(policy
, this->other_host
);
1245 if (ts_list_equals(my_ts
, my_ts_conf
) &&
1246 ts_list_equals(other_ts
, other_ts_conf
))
1248 iterator
->remove(iterator
);
1249 child_sa
->destroy(child_sa
);
1250 ts_list_destroy(my_ts_conf
);
1251 ts_list_destroy(other_ts_conf
);
1254 ts_list_destroy(my_ts_conf
);
1255 ts_list_destroy(other_ts_conf
);
1258 iterator
->destroy(iterator
);
1259 /* if we are not established, and we have no more routed childs, remove whole SA */
1260 if (this->state
== IKE_CREATED
&&
1261 this->child_sas
->get_count(this->child_sas
) == 0)
1269 * Implementation of ike_sa_t.send_dpd
1271 static status_t
send_dpd(private_ike_sa_t
*this)
1273 send_dpd_job_t
*job
;
1276 if (this->dpd_delay
== 0)
1282 if (this->transaction_out
)
1284 /* there is a transaction in progress. Come back later */
1289 /* check if there was any inbound traffic */
1290 time_t last_in
, now
;
1291 last_in
= get_time_inbound(this);
1293 diff
= now
- last_in
;
1294 if (diff
>= this->dpd_delay
)
1296 /* to long ago, initiate dead peer detection */
1297 dead_peer_detection_t
*dpd
;
1298 this->logger
->log(this->logger
, CONTROL
, "sending DPD request");
1299 dpd
= dead_peer_detection_create(&this->public);
1300 queue_transaction(this, (transaction_t
*)dpd
, FALSE
);
1304 /* recheck in "interval" seconds */
1305 job
= send_dpd_job_create(this->ike_sa_id
);
1306 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
1307 (this->dpd_delay
- diff
) * 1000);
1312 * Implementation of ike_sa_t.send_keepalive
1314 static void send_keepalive(private_ike_sa_t
*this)
1316 send_keepalive_job_t
*job
;
1317 time_t last_out
, now
, diff
, interval
;
1319 last_out
= get_time_outbound(this);
1322 diff
= now
- last_out
;
1323 interval
= charon
->configuration
->get_keepalive_interval(charon
->configuration
);
1325 if (diff
>= interval
)
1330 packet
= packet_create();
1331 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
1332 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
1333 data
.ptr
= malloc(1);
1336 packet
->set_data(packet
, data
);
1337 charon
->send_queue
->add(charon
->send_queue
, packet
);
1338 this->logger
->log(this->logger
, CONTROL
, "sending keep alive");
1341 job
= send_keepalive_job_create(this->ike_sa_id
);
1342 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
1343 (interval
- diff
) * 1000);
1347 * Implementation of ike_sa_t.get_state.
1349 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
1355 * Implementation of ike_sa_t.set_state.
1357 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
1359 this->logger
->log(this->logger
, CONTROL
, "state change: %s => %s",
1360 mapping_find(ike_sa_state_m
, this->state
),
1361 mapping_find(ike_sa_state_m
, state
));
1362 if (state
== IKE_ESTABLISHED
)
1364 this->time
.established
= time(NULL
);
1365 this->logger
->log(this->logger
, AUDIT
, "IKE_SA established: %s[%s]...%s[%s]",
1366 this->my_host
->get_string(this->my_host
),
1367 this->my_id
->get_string(this->my_id
),
1368 this->other_host
->get_string(this->other_host
),
1369 this->other_id
->get_string(this->other_id
));
1370 /* start DPD checks */
1373 this->state
= state
;
1377 * Implementation of ike_sa_t.get_prf.
1379 static prf_t
*get_prf(private_ike_sa_t
*this)
1385 * Implementation of ike_sa_t.get_prf.
1387 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1389 return this->child_prf
;
1393 * Implementation of ike_sa_t.get_prf_auth_i.
1395 static prf_t
*get_prf_auth_i(private_ike_sa_t
*this)
1397 return this->prf_auth_i
;
1401 * Implementation of ike_sa_t.get_prf_auth_r.
1403 static prf_t
*get_prf_auth_r(private_ike_sa_t
*this)
1405 return this->prf_auth_r
;
1409 * Implementation of ike_sa_t.get_id.
1411 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1413 return this->ike_sa_id
;
1417 * Implementation of ike_sa_t.get_my_id.
1419 static identification_t
* get_my_id(private_ike_sa_t
*this)
1425 * Implementation of ike_sa_t.set_my_id.
1427 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1429 DESTROY_IF(this->my_id
);
1434 * Implementation of ike_sa_t.get_other_id.
1436 static identification_t
* get_other_id(private_ike_sa_t
*this)
1438 return this->other_id
;
1442 * Implementation of ike_sa_t.set_other_id.
1444 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1446 DESTROY_IF(this->other_id
);
1447 this->other_id
= other
;
1451 * Implementation of ike_sa_t.derive_keys.
1453 static status_t
derive_keys(private_ike_sa_t
*this,
1454 proposal_t
*proposal
, diffie_hellman_t
*dh
,
1455 chunk_t nonce_i
, chunk_t nonce_r
,
1456 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1458 prf_plus_t
*prf_plus
;
1459 chunk_t skeyseed
, secret
, key
, nonces
, prf_plus_seed
;
1462 crypter_t
*crypter_i
, *crypter_r
;
1463 signer_t
*signer_i
, *signer_r
;
1464 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1465 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1466 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1468 /* Create SAs general purpose PRF first, we may use it here */
1469 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
1471 this->logger
->log(this->logger
, ERROR
, "no PSEUDO_RANDOM_FUNCTION selected!");
1474 this->prf
= prf_create(algo
->algorithm
);
1475 if (this->prf
== NULL
)
1477 this->logger
->log(this->logger
, ERROR
, "PSEUDO_RANDOM_FUNCTION %s not supported!",
1478 mapping_find(pseudo_random_function_m
, algo
->algorithm
));
1482 dh
->get_shared_secret(dh
, &secret
);
1483 this->logger
->log_chunk(this->logger
, PRIVATE
, "shared Diffie Hellman secret", secret
);
1484 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1485 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1486 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1487 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1489 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1491 * if we are rekeying, SKEYSEED built on another way
1493 if (child_prf
== NULL
) /* not rekeying */
1495 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1496 this->prf
->set_key(this->prf
, nonces
);
1497 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1498 this->logger
->log_chunk(this->logger
, PRIVATE
|LEVEL1
, "SKEYSEED", skeyseed
);
1499 this->prf
->set_key(this->prf
, skeyseed
);
1500 chunk_free(&skeyseed
);
1501 chunk_free(&secret
);
1502 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1506 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1507 * use OLD SAs PRF functions for both prf_plus and prf */
1508 secret
= chunk_cat("mc", secret
, nonces
);
1509 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1510 this->logger
->log_chunk(this->logger
, PRIVATE
|LEVEL1
, "SKEYSEED", skeyseed
);
1511 old_prf
->set_key(old_prf
, skeyseed
);
1512 chunk_free(&skeyseed
);
1513 chunk_free(&secret
);
1514 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1516 chunk_free(&nonces
);
1517 chunk_free(&prf_plus_seed
);
1519 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1521 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1522 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1523 this->child_prf
= prf_create(algo
->algorithm
);
1524 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1525 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1526 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_d secret", key
);
1527 this->child_prf
->set_key(this->child_prf
, key
);
1530 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1531 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1533 this->logger
->log(this->logger
, ERROR
, "no INTEGRITY_ALGORITHM selected?!");
1536 signer_i
= signer_create(algo
->algorithm
);
1537 signer_r
= signer_create(algo
->algorithm
);
1538 if (signer_i
== NULL
|| signer_r
== NULL
)
1540 this->logger
->log(this->logger
, ERROR
, "INTEGRITY_ALGORITHM %s not supported!",
1541 mapping_find(integrity_algorithm_m
,algo
->algorithm
));
1544 key_size
= signer_i
->get_key_size(signer_i
);
1546 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1547 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_ai secret", key
);
1548 signer_i
->set_key(signer_i
, key
);
1551 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1552 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_ar secret", key
);
1553 signer_r
->set_key(signer_r
, key
);
1558 this->signer_in
= signer_r
;
1559 this->signer_out
= signer_i
;
1563 this->signer_in
= signer_i
;
1564 this->signer_out
= signer_r
;
1567 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1568 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1570 this->logger
->log(this->logger
, ERROR
, "no ENCRYPTION_ALGORITHM selected!");
1573 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1574 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1575 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1577 this->logger
->log(this->logger
, ERROR
,
1578 "ENCRYPTION_ALGORITHM %s (key size %d) not supported!",
1579 mapping_find(encryption_algorithm_m
, algo
->algorithm
),
1583 key_size
= crypter_i
->get_key_size(crypter_i
);
1585 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1586 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_ei secret", key
);
1587 crypter_i
->set_key(crypter_i
, key
);
1590 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1591 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_er secret", key
);
1592 crypter_r
->set_key(crypter_r
, key
);
1597 this->crypter_in
= crypter_r
;
1598 this->crypter_out
= crypter_i
;
1602 this->crypter_in
= crypter_i
;
1603 this->crypter_out
= crypter_r
;
1606 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1607 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1608 this->prf_auth_i
= prf_create(algo
->algorithm
);
1609 this->prf_auth_r
= prf_create(algo
->algorithm
);
1611 key_size
= this->prf_auth_i
->get_key_size(this->prf_auth_i
);
1612 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1613 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_pi secret", key
);
1614 this->prf_auth_i
->set_key(this->prf_auth_i
, key
);
1617 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1618 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_pr secret", key
);
1619 this->prf_auth_r
->set_key(this->prf_auth_r
, key
);
1622 /* all done, prf_plus not needed anymore */
1623 prf_plus
->destroy(prf_plus
);
1630 * Implementation of ike_sa_t.add_child_sa.
1632 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1634 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1638 * Implementation of ike_sa_t.has_child_sa.
1640 static bool has_child_sa(private_ike_sa_t
*this, u_int32_t reqid
)
1642 iterator_t
*iterator
;
1643 child_sa_t
*current
;
1646 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1647 while (iterator
->iterate(iterator
, (void**)¤t
))
1649 if (current
->get_reqid(current
) == reqid
)
1655 iterator
->destroy(iterator
);
1660 * Implementation of ike_sa_t.get_child_sa.
1662 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1663 u_int32_t spi
, bool inbound
)
1665 iterator_t
*iterator
;
1666 child_sa_t
*current
, *found
= NULL
;
1668 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1669 while (iterator
->has_next(iterator
))
1671 iterator
->current(iterator
, (void**)¤t
);
1672 if (current
->get_spi(current
, inbound
) == spi
&&
1673 current
->get_protocol(current
) == protocol
)
1678 iterator
->destroy(iterator
);
1683 * Implementation of ike_sa_t.create_child_sa_iterator.
1685 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1687 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1691 * Implementation of ike_sa_t.rekey_child_sa.
1693 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1695 create_child_sa_t
*rekey
;
1696 child_sa_t
*child_sa
;
1698 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1699 if (child_sa
== NULL
)
1704 rekey
= create_child_sa_create(&this->public);
1705 rekey
->rekeys_child(rekey
, child_sa
);
1706 return queue_transaction(this, (transaction_t
*)rekey
, FALSE
);
1710 * Implementation of ike_sa_t.delete_child_sa.
1712 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1714 delete_child_sa_t
*del
;
1715 child_sa_t
*child_sa
;
1717 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1718 if (child_sa
== NULL
)
1723 del
= delete_child_sa_create(&this->public);
1724 del
->set_child_sa(del
, child_sa
);
1725 return queue_transaction(this, (transaction_t
*)del
, FALSE
);
1729 * Implementation of ike_sa_t.destroy_child_sa.
1731 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1733 iterator_t
*iterator
;
1734 child_sa_t
*child_sa
;
1735 status_t status
= NOT_FOUND
;
1737 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1738 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1740 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1741 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1743 child_sa
->destroy(child_sa
);
1744 iterator
->remove(iterator
);
1749 iterator
->destroy(iterator
);
1754 * Implementation of ike_sa_t.set_lifetimes.
1756 static void set_lifetimes(private_ike_sa_t
*this,
1757 u_int32_t soft_lifetime
, u_int32_t hard_lifetime
)
1763 this->time
.rekey
= this->time
.established
+ soft_lifetime
;
1764 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
);
1765 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
1766 soft_lifetime
* 1000);
1771 this->time
.delete = this->time
.established
+ hard_lifetime
;
1772 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
1773 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
1774 hard_lifetime
* 1000);
1779 * Implementation of ike_sa_t.rekey.
1781 static status_t
rekey(private_ike_sa_t
*this)
1783 rekey_ike_sa_t
*rekey_ike_sa
;
1785 this->logger
->log(this->logger
, CONTROL
,
1786 "rekeying IKE_SA between %s[%s]..%s[%s]",
1787 this->my_host
->get_string(this->my_host
),
1788 this->my_id
->get_string(this->my_id
),
1789 this->other_host
->get_string(this->other_host
),
1790 this->other_id
->get_string(this->other_id
));
1792 if (this->state
!= IKE_ESTABLISHED
)
1794 this->logger
->log(this->logger
, ERROR
,
1795 "unable to rekey IKE_SA in state %s",
1796 mapping_find(ike_sa_state_m
, this->state
));
1800 rekey_ike_sa
= rekey_ike_sa_create(&this->public);
1801 return queue_transaction(this, (transaction_t
*)rekey_ike_sa
, FALSE
);
1805 * Implementation of ike_sa_t.get_rekeying_transaction.
1807 static rekey_ike_sa_t
* get_rekeying_transaction(private_ike_sa_t
*this)
1809 return this->rekeying_transaction
;
1813 * Implementation of ike_sa_t.set_rekeying_transaction.
1815 static void set_rekeying_transaction(private_ike_sa_t
*this, rekey_ike_sa_t
*rekey
)
1817 this->rekeying_transaction
= rekey
;
1821 * Implementation of ike_sa_t.adopt_children.
1823 static void adopt_children(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1825 child_sa_t
*child_sa
;
1827 while (other
->child_sas
->remove_last(other
->child_sas
,
1828 (void**)&child_sa
) == SUCCESS
)
1830 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1835 * Implementation of ike_sa_t.log_status.
1837 static void log_status(private_ike_sa_t
*this, logger_t
*logger
, char *name
)
1839 iterator_t
*iterator
;
1840 child_sa_t
*child_sa
;
1841 bool contains_child
= FALSE
;
1843 /* check for a CHILD_SA with specified name. We then print the IKE_SA,
1844 * even it has another name */
1847 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1848 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1850 if (streq(name
, child_sa
->get_name(child_sa
)))
1852 contains_child
= TRUE
;
1856 iterator
->destroy(iterator
);
1859 if (name
== NULL
|| contains_child
|| streq(name
, this->name
))
1863 logger
= this->logger
;
1865 logger
->log(logger
, CONTROL
|LEVEL1
,
1866 " \"%s\": IKE_SA in state %s, SPIs: 0x%.16llx 0x%.16llx",
1868 mapping_find(ike_sa_state_m
, this->state
),
1869 this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
),
1870 this->ike_sa_id
->get_responder_spi(this->ike_sa_id
));
1871 logger
->log(logger
, CONTROL
, " \"%s\": %s[%s]...%s[%s]",
1873 this->my_host
->get_string(this->my_host
),
1874 this->my_id
->get_string(this->my_id
),
1875 this->other_host
->get_string(this->other_host
),
1876 this->other_id
->get_string(this->other_id
));
1878 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1879 while (iterator
->has_next(iterator
))
1881 iterator
->current(iterator
, (void**)&child_sa
);
1882 child_sa
->log_status(child_sa
, logger
);
1884 iterator
->destroy(iterator
);
1889 * Implementation of public_ike_sa_t.delete.
1891 static status_t
delete_(private_ike_sa_t
*this)
1893 switch (this->state
)
1895 case IKE_CONNECTING
:
1896 case IKE_ESTABLISHED
:
1898 delete_ike_sa_t
*delete_ike_sa
;
1899 if (this->transaction_out
)
1901 /* already a transaction in progress. As this may hang
1902 * around a while, we don't inform the other peer. */
1905 delete_ike_sa
= delete_ike_sa_create(&this->public);
1906 return queue_transaction(this, (transaction_t
*)delete_ike_sa
, FALSE
);
1918 * Implementation of ike_sa_t.get_next_message_id.
1920 static u_int32_t
get_next_message_id (private_ike_sa_t
*this)
1922 return this->message_id_out
++;
1926 * Implementation of ike_sa_t.is_natt_enabled.
1928 static bool is_natt_enabled (private_ike_sa_t
*this)
1930 return this->nat_here
|| this->nat_there
;
1934 * Implementation of ike_sa_t.enable_natt.
1936 static void enable_natt (private_ike_sa_t
*this, bool local
)
1940 this->logger
->log(this->logger
, CONTROL
,
1941 "local host is behind NAT, using NAT-T, scheduled keep alives");
1942 this->nat_here
= TRUE
;
1943 send_keepalive(this);
1947 this->logger
->log(this->logger
, CONTROL
,
1948 "remote host is behind NAT, using NAT-T");
1949 this->nat_there
= TRUE
;
1954 * Implementation of ike_sa_t.destroy.
1956 static void destroy(private_ike_sa_t
*this)
1958 child_sa_t
*child_sa
;
1959 transaction_t
*transaction
;
1961 this->logger
->log(this->logger
, CONTROL
|LEVEL2
, "going to destroy IKE SA %llu:%llu, role %s",
1962 this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
),
1963 this->ike_sa_id
->get_responder_spi(this->ike_sa_id
),
1964 this->ike_sa_id
->is_initiator(this->ike_sa_id
) ?
"initiator" : "responder");
1966 if (this->state
== IKE_ESTABLISHED
)
1968 this->logger
->log(this->logger
, ERROR
,
1969 "destroying an established IKE SA without knowledge from remote peer!");
1972 while (this->child_sas
->remove_last(this->child_sas
, (void**)&child_sa
) == SUCCESS
)
1974 child_sa
->destroy(child_sa
);
1976 this->child_sas
->destroy(this->child_sas
);
1978 while (this->transaction_queue
->remove_last(this->transaction_queue
, (void**)&transaction
) == SUCCESS
)
1980 transaction
->destroy(transaction
);
1982 this->transaction_queue
->destroy(this->transaction_queue
);
1984 DESTROY_IF(this->transaction_in
);
1985 DESTROY_IF(this->transaction_in_next
);
1986 DESTROY_IF(this->transaction_out
);
1987 DESTROY_IF(this->crypter_in
);
1988 DESTROY_IF(this->crypter_out
);
1989 DESTROY_IF(this->signer_in
);
1990 DESTROY_IF(this->signer_out
);
1991 DESTROY_IF(this->prf
);
1992 DESTROY_IF(this->child_prf
);
1993 DESTROY_IF(this->prf_auth_i
);
1994 DESTROY_IF(this->prf_auth_r
);
1996 this->logger
->log(this->logger
, AUDIT
,
1997 "IKE_SA deleted between %s[%s]...%s[%s]",
1998 this->my_host
->get_string(this->my_host
),
1999 this->my_id
->get_string(this->my_id
),
2000 this->other_host
->get_string(this->other_host
),
2001 this->other_id
->get_string(this->other_id
));
2003 DESTROY_IF(this->my_host
);
2004 DESTROY_IF(this->other_host
);
2005 DESTROY_IF(this->my_id
);
2006 DESTROY_IF(this->other_id
);
2009 this->ike_sa_id
->destroy(this->ike_sa_id
);
2014 * Described in header.
2016 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2018 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2020 /* Public functions */
2021 this->public.get_state
= (ike_sa_state_t(*)(ike_sa_t
*)) get_state
;
2022 this->public.set_state
= (void(*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2023 this->public.get_name
= (char*(*)(ike_sa_t
*))get_name
;
2024 this->public.set_name
= (void(*)(ike_sa_t
*,char*))set_name
;
2025 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
2026 this->public.initiate
= (status_t(*)(ike_sa_t
*,connection_t
*,policy_t
*)) initiate
;
2027 this->public.route
= (status_t(*)(ike_sa_t
*,connection_t
*,policy_t
*)) route
;
2028 this->public.unroute
= (status_t(*)(ike_sa_t
*,policy_t
*)) unroute
;
2029 this->public.acquire
= (status_t(*)(ike_sa_t
*,u_int32_t
)) acquire
;
2030 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
2031 this->public.get_my_host
= (host_t
*(*)(ike_sa_t
*)) get_my_host
;
2032 this->public.set_my_host
= (void(*)(ike_sa_t
*,host_t
*)) set_my_host
;
2033 this->public.get_other_host
= (host_t
*(*)(ike_sa_t
*)) get_other_host
;
2034 this->public.set_other_host
= (void(*)(ike_sa_t
*,host_t
*)) set_other_host
;
2035 this->public.get_my_id
= (identification_t
*(*)(ike_sa_t
*)) get_my_id
;
2036 this->public.set_my_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2037 this->public.get_other_id
= (identification_t
*(*)(ike_sa_t
*)) get_other_id
;
2038 this->public.set_other_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2039 this->public.get_next_message_id
= (u_int32_t(*)(ike_sa_t
*)) get_next_message_id
;
2040 this->public.retransmit_request
= (status_t (*) (ike_sa_t
*, u_int32_t
)) retransmit_request
;
2041 this->public.log_status
= (void (*) (ike_sa_t
*,logger_t
*,char*))log_status
;
2042 this->public.delete = (status_t(*)(ike_sa_t
*))delete_
;
2043 this->public.destroy
= (void(*)(ike_sa_t
*))destroy
;
2044 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
2045 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
2046 this->public.get_prf
= (prf_t
*(*) (ike_sa_t
*)) get_prf
;
2047 this->public.get_child_prf
= (prf_t
*(*) (ike_sa_t
*)) get_child_prf
;
2048 this->public.get_prf_auth_i
= (prf_t
*(*) (ike_sa_t
*)) get_prf_auth_i
;
2049 this->public.get_prf_auth_r
= (prf_t
*(*) (ike_sa_t
*)) get_prf_auth_r
;
2050 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
;
2051 this->public.add_child_sa
= (void (*) (ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2052 this->public.has_child_sa
= (bool(*)(ike_sa_t
*,u_int32_t
)) has_child_sa
;
2053 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2054 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2055 this->public.rekey_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2056 this->public.delete_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2057 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2058 this->public.enable_natt
= (void(*)(ike_sa_t
*, bool)) enable_natt
;
2059 this->public.is_natt_enabled
= (bool(*)(ike_sa_t
*)) is_natt_enabled
;
2060 this->public.set_lifetimes
= (void(*)(ike_sa_t
*,u_int32_t
,u_int32_t
))set_lifetimes
;
2061 this->public.set_dpd_delay
= (void(*)(ike_sa_t
*,u_int32_t
))set_dpd_delay
;
2062 this->public.rekey
= (status_t(*)(ike_sa_t
*))rekey
;
2063 this->public.get_rekeying_transaction
= (void*(*)(ike_sa_t
*))get_rekeying_transaction
;
2064 this->public.set_rekeying_transaction
= (void(*)(ike_sa_t
*,void*))set_rekeying_transaction
;
2065 this->public.adopt_children
= (void(*)(ike_sa_t
*,ike_sa_t
*))adopt_children
;
2067 /* initialize private fields */
2068 this->logger
= logger_manager
->get_logger(logger_manager
, IKE_SA
);
2069 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2070 this->name
= strdup("(uninitialized)");
2071 this->child_sas
= linked_list_create();
2072 this->my_host
= host_create_from_string("0.0.0.0", 0);
2073 this->other_host
= host_create_from_string("0.0.0.0", 0);
2074 this->my_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
2075 this->other_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
2076 this->crypter_in
= NULL
;
2077 this->crypter_out
= NULL
;
2078 this->signer_in
= NULL
;
2079 this->signer_out
= NULL
;
2081 this->prf_auth_i
= NULL
;
2082 this->prf_auth_r
= NULL
;
2083 this->child_prf
= NULL
;
2084 this->nat_here
= FALSE
;
2085 this->nat_there
= FALSE
;
2086 this->transaction_queue
= linked_list_create();
2087 this->transaction_in
= NULL
;
2088 this->transaction_in_next
= NULL
;
2089 this->transaction_out
= NULL
;
2090 this->rekeying_transaction
= NULL
;
2091 this->state
= IKE_CREATED
;
2092 this->message_id_out
= 0;
2093 /* set to NOW, as when we rekey an existing IKE_SA no message is exchanged */
2094 this->time
.inbound
= this->time
.outbound
= time(NULL
);
2095 this->time
.established
= 0;
2096 this->time
.rekey
= 0;
2097 this->time
.delete = 0;
2098 this->dpd_delay
= 0;
2099 this->retrans_sequences
= 0;
2101 return &this->public;