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
34 #include <utils/linked_list.h>
35 #include <utils/lexparser.h>
36 #include <crypto/diffie_hellman.h>
37 #include <crypto/prf_plus.h>
38 #include <crypto/crypters/crypter.h>
39 #include <crypto/hashers/hasher.h>
40 #include <encoding/payloads/sa_payload.h>
41 #include <encoding/payloads/nonce_payload.h>
42 #include <encoding/payloads/ke_payload.h>
43 #include <encoding/payloads/delete_payload.h>
44 #include <encoding/payloads/transform_substructure.h>
45 #include <encoding/payloads/transform_attribute.h>
46 #include <encoding/payloads/ts_payload.h>
47 #include <sa/task_manager.h>
48 #include <sa/tasks/ike_init.h>
49 #include <sa/tasks/ike_natd.h>
50 #include <sa/tasks/ike_auth.h>
51 #include <sa/tasks/ike_config.h>
52 #include <sa/tasks/ike_cert.h>
53 #include <sa/tasks/ike_rekey.h>
54 #include <sa/tasks/ike_delete.h>
55 #include <sa/tasks/ike_dpd.h>
56 #include <sa/tasks/child_create.h>
57 #include <sa/tasks/child_delete.h>
58 #include <sa/tasks/child_rekey.h>
59 #include <processing/jobs/retransmit_job.h>
60 #include <processing/jobs/delete_ike_sa_job.h>
61 #include <processing/jobs/send_dpd_job.h>
62 #include <processing/jobs/send_keepalive_job.h>
63 #include <processing/jobs/rekey_ike_sa_job.h>
64 #include <processing/jobs/route_job.h>
65 #include <processing/jobs/initiate_job.h>
69 #define RESOLV_CONF "/etc/resolv.conf"
72 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DELETING
,
80 typedef struct private_ike_sa_t private_ike_sa_t
;
83 * Private data of an ike_sa_t object.
85 struct private_ike_sa_t
{
93 * Identifier for the current IKE_SA.
95 ike_sa_id_t
*ike_sa_id
;
98 * unique numerical ID for this IKE_SA.
103 * Current state of the IKE_SA
105 ike_sa_state_t state
;
108 * IKE configuration used to set up this IKE_SA
113 * Peer and authentication information to establish IKE_SA.
115 peer_cfg_t
*peer_cfg
;
118 * Juggles tasks to process messages
120 task_manager_t
*task_manager
;
123 * Address of local host
128 * Address of remote host
133 * Identification used for us
135 identification_t
*my_id
;
138 * Identification used for other
140 identification_t
*other_id
;
143 * Linked List containing the child sa's of the current IKE_SA.
145 linked_list_t
*child_sas
;
148 * crypter for inbound traffic
150 crypter_t
*crypter_in
;
153 * crypter for outbound traffic
155 crypter_t
*crypter_out
;
158 * Signer for inbound traffic
163 * Signer for outbound traffic
165 signer_t
*signer_out
;
168 * Multi purpose prf, set key, use it, forget it
173 * Prf function for derivating keymat child SAs
178 * PRF to build outging authentication data
183 * PRF to verify incoming authentication data
188 * NAT status of local host.
193 * NAT status of remote host.
198 * Virtual IP on local host, if any
200 host_t
*my_virtual_ip
;
203 * Virtual IP on remote host, if any
205 host_t
*other_virtual_ip
;
208 * List of DNS servers installed by us
210 linked_list_t
*dns_servers
;
213 * Timestamps for this IKE_SA
216 /** last IKE message received */
218 /** last IKE message sent */
220 /** when IKE_SA became established */
221 u_int32_t established
;
222 /** when IKE_SA gets rekeyed */
224 /** when IKE_SA gets deleted */
229 * how many times we have retried so far (keyingtries)
235 * get the time of the latest traffic processed by the kernel
237 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
239 iterator_t
*iterator
;
240 child_sa_t
*child_sa
;
241 time_t latest
= 0, use_time
;
243 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
244 while (iterator
->iterate(iterator
, (void**)&child_sa
))
246 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
248 latest
= max(latest
, use_time
);
251 iterator
->destroy(iterator
);
255 return max(this->time
.inbound
, latest
);
259 return max(this->time
.outbound
, latest
);
264 * Implementation of ike_sa_t.get_unique_id
266 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
268 return this->unique_id
;
272 * Implementation of ike_sa_t.get_name.
274 static char *get_name(private_ike_sa_t
*this)
278 return this->peer_cfg
->get_name(this->peer_cfg
);
285 * Implementation of ike_sa_t.get_stats.
287 static void get_stats(private_ike_sa_t
*this, u_int32_t
*next_rekeying
)
291 *next_rekeying
= this->time
.rekey
;
296 * Implementation of ike_sa_t.get_my_host.
298 static host_t
*get_my_host(private_ike_sa_t
*this)
300 return this->my_host
;
304 * Implementation of ike_sa_t.set_my_host.
306 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
308 DESTROY_IF(this->my_host
);
313 * Implementation of ike_sa_t.get_other_host.
315 static host_t
*get_other_host(private_ike_sa_t
*this)
317 return this->other_host
;
321 * Implementation of ike_sa_t.set_other_host.
323 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
325 DESTROY_IF(this->other_host
);
326 this->other_host
= other
;
330 * Implementation of ike_sa_t.get_peer_cfg
332 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
334 return this->peer_cfg
;
338 * Implementation of ike_sa_t.set_peer_cfg
340 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
342 peer_cfg
->get_ref(peer_cfg
);
343 this->peer_cfg
= peer_cfg
;
345 if (this->ike_cfg
== NULL
)
347 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
348 this->ike_cfg
->get_ref(this->ike_cfg
);
351 /* apply values, so we are ready to initate/acquire */
352 if (this->my_host
->is_anyaddr(this->my_host
))
354 host_t
*me
= this->ike_cfg
->get_my_host(this->ike_cfg
);
356 set_my_host(this, me
->clone(me
));
358 if (this->other_host
->is_anyaddr(this->other_host
))
360 host_t
*other
= this->ike_cfg
->get_other_host(this->ike_cfg
);
362 set_other_host(this, other
->clone(other
));
364 if (this->my_id
== NULL
|| this->my_id
->contains_wildcards(this->my_id
))
366 identification_t
*my_id
= this->peer_cfg
->get_my_id(this->peer_cfg
);
368 DESTROY_IF(this->my_id
);
369 this->my_id
= my_id
->clone(my_id
);
371 if (this->other_id
== NULL
|| this->other_id
->contains_wildcards(this->other_id
))
373 identification_t
*other_id
= this->peer_cfg
->get_other_id(this->peer_cfg
);
375 DESTROY_IF(this->other_id
);
376 this->other_id
= other_id
->clone(other_id
);
381 * Implementation of ike_sa_t.get_ike_cfg
383 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
385 return this->ike_cfg
;
389 * Implementation of ike_sa_t.set_ike_cfg
391 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
393 ike_cfg
->get_ref(ike_cfg
);
394 this->ike_cfg
= ike_cfg
;
398 * Implementation of ike_sa_t.send_dpd
400 static status_t
send_dpd(private_ike_sa_t
*this)
405 delay
= this->peer_cfg
->get_dpd_delay(this->peer_cfg
);
413 if (this->task_manager
->busy(this->task_manager
))
415 /* an exchange is in the air, no need to start a DPD check */
420 /* check if there was any inbound traffic */
422 last_in
= get_use_time(this, TRUE
);
424 diff
= now
- last_in
;
427 /* to long ago, initiate dead peer detection */
430 task
= (task_t
*)ike_dpd_create(TRUE
);
432 DBG1(DBG_IKE
, "sending DPD request");
434 this->task_manager
->queue_task(this->task_manager
, task
);
435 this->task_manager
->initiate(this->task_manager
);
438 /* recheck in "interval" seconds */
439 job
= send_dpd_job_create(this->ike_sa_id
);
440 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
441 (delay
- diff
) * 1000);
446 * Implementation of ike_sa_t.send_keepalive
448 static void send_keepalive(private_ike_sa_t
*this)
450 send_keepalive_job_t
*job
;
451 time_t last_out
, now
, diff
;
453 last_out
= get_use_time(this, FALSE
);
456 diff
= now
- last_out
;
458 if (diff
>= KEEPALIVE_INTERVAL
)
463 packet
= packet_create();
464 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
465 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
466 data
.ptr
= malloc(1);
469 packet
->set_data(packet
, data
);
470 charon
->sender
->send(charon
->sender
, packet
);
471 DBG1(DBG_IKE
, "sending keep alive");
474 job
= send_keepalive_job_create(this->ike_sa_id
);
475 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
476 (KEEPALIVE_INTERVAL
- diff
) * 1000);
480 * Implementation of ike_sa_t.get_state.
482 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
488 * Implementation of ike_sa_t.set_state.
490 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
492 DBG1(DBG_IKE
, "IKE_SA state change: %N => %N",
493 ike_sa_state_names
, this->state
,
494 ike_sa_state_names
, state
);
498 case IKE_ESTABLISHED
:
500 if (this->state
== IKE_CONNECTING
)
503 u_int32_t now
= time(NULL
);
504 u_int32_t soft
, hard
;
507 this->time
.established
= now
;
508 /* start DPD checks */
511 /* schedule rekeying/reauthentication */
512 soft
= this->peer_cfg
->get_lifetime(this->peer_cfg
, TRUE
);
513 hard
= this->peer_cfg
->get_lifetime(this->peer_cfg
, FALSE
);
514 reauth
= this->peer_cfg
->use_reauth(this->peer_cfg
);
515 DBG1(DBG_IKE
, "scheduling %s in %ds, maximum lifetime %ds",
516 reauth ?
"reauthentication": "rekeying", soft
, hard
);
520 this->time
.rekey
= now
+ soft
;
521 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, reauth
);
522 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
528 this->time
.delete = now
+ hard
;
529 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
530 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
538 /* delete may fail if a packet gets lost, so set a timeout */
539 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
540 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
541 HALF_OPEN_IKE_SA_TIMEOUT
);
552 * Implementation of ike_sa_t.reset
554 static void reset(private_ike_sa_t
*this)
556 /* the responder ID is reset, as peer may choose another one */
557 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
559 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
562 set_state(this, IKE_CREATED
);
564 this->task_manager
->reset(this->task_manager
);
568 * Update hosts, as addresses may change (NAT)
570 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
572 iterator_t
*iterator
= NULL
;
573 child_sa_t
*child_sa
= NULL
;
574 host_diff_t my_diff
, other_diff
;
576 if (this->my_host
->is_anyaddr(this->my_host
) ||
577 this->other_host
->is_anyaddr(this->other_host
))
579 /* on first received message */
580 this->my_host
->destroy(this->my_host
);
581 this->my_host
= me
->clone(me
);
582 this->other_host
->destroy(this->other_host
);
583 this->other_host
= other
->clone(other
);
587 my_diff
= me
->get_differences(me
, this->my_host
);
588 other_diff
= other
->get_differences(other
, this->other_host
);
590 if (!my_diff
&& !other_diff
)
597 this->my_host
->destroy(this->my_host
);
598 this->my_host
= me
->clone(me
);
603 /* update without restrictions if we are not NATted */
606 this->other_host
->destroy(this->other_host
);
607 this->other_host
= other
->clone(other
);
612 /* if we are natted, only port may change */
613 if (other_diff
& HOST_DIFF_ADDR
)
617 else if (other_diff
& HOST_DIFF_PORT
)
619 this->other_host
->set_port(this->other_host
, other
->get_port(other
));
622 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
623 while (iterator
->iterate(iterator
, (void**)&child_sa
))
625 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
626 my_diff
, other_diff
);
628 iterator
->destroy(iterator
);
632 * Implementation of ike_sa_t.generate
634 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
637 this->time
.outbound
= time(NULL
);
638 message
->set_ike_sa_id(message
, this->ike_sa_id
);
639 message
->set_destination(message
, this->other_host
->clone(this->other_host
));
640 message
->set_source(message
, this->my_host
->clone(this->my_host
));
641 return message
->generate(message
, this->crypter_out
, this->signer_out
, packet
);
645 * send a notify back to the sender
647 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
653 response
= message_create();
654 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
655 response
->set_request(response
, FALSE
);
656 response
->set_message_id(response
, request
->get_message_id(request
));
657 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
658 if (this->my_host
->is_anyaddr(this->my_host
))
660 this->my_host
->destroy(this->my_host
);
661 this->my_host
= request
->get_destination(request
);
662 this->my_host
= this->my_host
->clone(this->my_host
);
664 if (this->other_host
->is_anyaddr(this->other_host
))
666 this->other_host
->destroy(this->other_host
);
667 this->other_host
= request
->get_source(request
);
668 this->other_host
= this->other_host
->clone(this->other_host
);
670 if (generate_message(this, response
, &packet
) == SUCCESS
)
672 charon
->sender
->send(charon
->sender
, packet
);
674 response
->destroy(response
);
678 * Implementation of ike_sa_t.process_message.
680 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
685 is_request
= message
->get_request(message
);
687 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
688 if (status
!= SUCCESS
)
696 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
699 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
703 DBG1(DBG_IKE
, "message parsing failed");
706 send_notify_response(this, message
, INVALID_SYNTAX
);
710 DBG1(DBG_IKE
, "message verification failed");
713 send_notify_response(this, message
, INVALID_SYNTAX
);
717 DBG1(DBG_IKE
, "integrity check failed");
721 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
724 send_notify_response(this, message
, INVALID_SYNTAX
);
730 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
731 exchange_type_names
, message
->get_exchange_type(message
),
732 message
->get_request(message
) ?
"request" : "response",
733 message
->get_message_id(message
));
740 me
= message
->get_destination(message
);
741 other
= message
->get_source(message
);
743 /* if this IKE_SA is virgin, we check for a config */
744 if (this->ike_cfg
== NULL
)
747 this->ike_cfg
= charon
->cfg_store
->get_ike_cfg(charon
->cfg_store
,
749 if (this->ike_cfg
== NULL
)
751 /* no config found for these hosts, destroy */
752 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
753 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
754 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
757 /* add a timeout if peer does not establish it completely */
758 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
759 charon
->event_queue
->add_relative(charon
->event_queue
, job
,
760 HALF_OPEN_IKE_SA_TIMEOUT
);
763 /* check if message is trustworthy, and update host information */
764 if (this->state
== IKE_CREATED
||
765 message
->get_exchange_type(message
) != IKE_SA_INIT
)
767 update_hosts(this, me
, other
);
768 this->time
.inbound
= time(NULL
);
770 return this->task_manager
->process_message(this->task_manager
, message
);
775 * Implementation of ike_sa_t.initiate.
777 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
781 if (this->state
== IKE_CREATED
)
784 if (this->other_host
->is_anyaddr(this->other_host
))
786 SIG(IKE_UP_START
, "initiating IKE_SA");
787 SIG(IKE_UP_FAILED
, "unable to initiate to %%any");
791 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
792 this->task_manager
->queue_task(this->task_manager
, task
);
793 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
794 this->task_manager
->queue_task(this->task_manager
, task
);
795 task
= (task_t
*)ike_cert_create(&this->public, TRUE
);
796 this->task_manager
->queue_task(this->task_manager
, task
);
797 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
798 this->task_manager
->queue_task(this->task_manager
, task
);
799 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
800 this->task_manager
->queue_task(this->task_manager
, task
);
803 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
804 this->task_manager
->queue_task(this->task_manager
, task
);
806 return this->task_manager
->initiate(this->task_manager
);
810 * Implementation of ike_sa_t.acquire.
812 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
814 child_cfg_t
*child_cfg
;
815 iterator_t
*iterator
;
816 child_sa_t
*current
, *child_sa
= NULL
;
818 child_create_t
*child_create
;
820 if (this->state
== IKE_DELETING
)
822 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
823 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
824 "IKE_SA is deleting", reqid
);
829 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
830 while (iterator
->iterate(iterator
, (void**)¤t
))
832 if (current
->get_reqid(current
) == reqid
)
838 iterator
->destroy(iterator
);
841 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
842 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
843 "CHILD_SA not found", reqid
);
848 if (this->state
== IKE_CREATED
)
850 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
851 this->task_manager
->queue_task(this->task_manager
, task
);
852 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
853 this->task_manager
->queue_task(this->task_manager
, task
);
854 task
= (task_t
*)ike_cert_create(&this->public, TRUE
);
855 this->task_manager
->queue_task(this->task_manager
, task
);
856 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
857 this->task_manager
->queue_task(this->task_manager
, task
);
858 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
859 this->task_manager
->queue_task(this->task_manager
, task
);
862 child_cfg
= child_sa
->get_config(child_sa
);
863 child_create
= child_create_create(&this->public, child_cfg
);
864 child_create
->use_reqid(child_create
, reqid
);
865 this->task_manager
->queue_task(this->task_manager
, (task_t
*)child_create
);
867 return this->task_manager
->initiate(this->task_manager
);
871 * Implementation of ike_sa_t.route.
873 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
875 child_sa_t
*child_sa
;
876 iterator_t
*iterator
;
877 linked_list_t
*my_ts
, *other_ts
;
880 SIG(CHILD_ROUTE_START
, "routing CHILD_SA");
882 /* check if not already routed*/
883 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
884 while (iterator
->iterate(iterator
, (void**)&child_sa
))
886 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
887 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
889 iterator
->destroy(iterator
);
890 SIG(CHILD_ROUTE_FAILED
, "CHILD_SA with such a config already routed");
894 iterator
->destroy(iterator
);
900 SIG(CHILD_ROUTE_FAILED
,
901 "unable to route CHILD_SA, as its IKE_SA gets deleted");
905 case IKE_ESTABLISHED
:
910 /* install kernel policies */
911 child_sa
= child_sa_create(this->my_host
, this->other_host
, this->my_id
,
912 this->other_id
, child_cfg
, FALSE
, 0);
914 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
,
916 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
,
918 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
,
919 child_cfg
->get_mode(child_cfg
));
920 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
921 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
922 if (status
== SUCCESS
)
924 this->child_sas
->insert_last(this->child_sas
, child_sa
);
925 SIG(CHILD_ROUTE_SUCCESS
, "CHILD_SA routed");
929 SIG(CHILD_ROUTE_FAILED
, "routing CHILD_SA failed");
935 * Implementation of ike_sa_t.unroute.
937 static status_t
unroute(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
939 iterator_t
*iterator
;
940 child_sa_t
*child_sa
;
943 SIG(CHILD_UNROUTE_START
, "unrouting CHILD_SA");
945 /* find CHILD_SA in ROUTED state */
946 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
947 while (iterator
->iterate(iterator
, (void**)&child_sa
))
949 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
950 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
952 iterator
->remove(iterator
);
953 SIG(CHILD_UNROUTE_SUCCESS
, "CHILD_SA unrouted");
954 child_sa
->destroy(child_sa
);
959 iterator
->destroy(iterator
);
963 SIG(CHILD_UNROUTE_FAILED
, "CHILD_SA to unroute not found");
966 /* if we are not established, and we have no more routed childs, remove whole SA */
967 if (this->state
== IKE_CREATED
&&
968 this->child_sas
->get_count(this->child_sas
) == 0)
976 * Implementation of ike_sa_t.retransmit.
978 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
980 this->time
.outbound
= time(NULL
);
981 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
983 child_cfg_t
*child_cfg
;
984 child_sa_t
* child_sa
;
985 linked_list_t
*to_route
, *to_restart
;
986 iterator_t
*iterator
;
988 /* send a proper signal to brief interested bus listeners */
993 /* retry IKE_SA_INIT if we have multiple keyingtries */
994 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
996 if (tries
== 0 || tries
> this->keyingtry
)
998 SIG(IKE_UP_FAILED
, "peer not responding, trying again "
999 "(%d/%d) in background ", this->keyingtry
+ 1, tries
);
1001 return this->task_manager
->initiate(this->task_manager
);
1003 SIG(IKE_UP_FAILED
, "establishing IKE_SA failed, peer not responding");
1007 SIG(IKE_REKEY_FAILED
, "rekeying IKE_SA failed, peer not responding");
1010 SIG(IKE_DOWN_FAILED
, "proper IKE_SA delete failed, peer not responding");
1016 /* summarize how we have to handle each child */
1017 to_route
= linked_list_create();
1018 to_restart
= linked_list_create();
1019 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1020 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1022 child_cfg
= child_sa
->get_config(child_sa
);
1024 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1026 /* reroute routed CHILD_SAs */
1027 to_route
->insert_last(to_route
, child_cfg
);
1031 /* use DPD action for established CHILD_SAs */
1032 switch (this->peer_cfg
->get_dpd_action(this->peer_cfg
))
1035 to_route
->insert_last(to_route
, child_cfg
);
1038 to_restart
->insert_last(to_restart
, child_cfg
);
1045 iterator
->destroy(iterator
);
1047 /* create a new IKE_SA if we have to route or to restart */
1048 if (to_route
->get_count(to_route
) || to_restart
->get_count(to_restart
))
1050 private_ike_sa_t
*new;
1053 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1054 charon
->ike_sa_manager
, TRUE
);
1056 set_peer_cfg(new, this->peer_cfg
);
1057 /* use actual used host, not the wildcarded one in config */
1058 new->other_host
->destroy(new->other_host
);
1059 new->other_host
= this->other_host
->clone(this->other_host
);
1061 /* install routes */
1062 while (to_route
->remove_last(to_route
, (void**)&child_cfg
) == SUCCESS
)
1064 route(new, child_cfg
);
1067 /* restart children */
1068 if (to_restart
->get_count(to_restart
))
1070 task
= (task_t
*)ike_init_create(&new->public, TRUE
, NULL
);
1071 new->task_manager
->queue_task(new->task_manager
, task
);
1072 task
= (task_t
*)ike_natd_create(&new->public, TRUE
);
1073 new->task_manager
->queue_task(new->task_manager
, task
);
1074 task
= (task_t
*)ike_cert_create(&new->public, TRUE
);
1075 new->task_manager
->queue_task(new->task_manager
, task
);
1076 task
= (task_t
*)ike_config_create(&new->public, TRUE
);
1077 new->task_manager
->queue_task(new->task_manager
, task
);
1078 task
= (task_t
*)ike_auth_create(&new->public, TRUE
);
1079 new->task_manager
->queue_task(new->task_manager
, task
);
1081 while (to_restart
->remove_last(to_restart
, (void**)&child_cfg
) == SUCCESS
)
1083 task
= (task_t
*)child_create_create(&new->public, child_cfg
);
1084 new->task_manager
->queue_task(new->task_manager
, task
);
1086 new->task_manager
->initiate(new->task_manager
);
1088 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1090 to_route
->destroy(to_route
);
1091 to_restart
->destroy(to_restart
);
1098 * Implementation of ike_sa_t.get_prf.
1100 static prf_t
*get_prf(private_ike_sa_t
*this)
1106 * Implementation of ike_sa_t.get_prf.
1108 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1110 return this->child_prf
;
1114 * Implementation of ike_sa_t.get_auth_bild
1116 static prf_t
*get_auth_build(private_ike_sa_t
*this)
1118 return this->auth_build
;
1122 * Implementation of ike_sa_t.get_auth_verify
1124 static prf_t
*get_auth_verify(private_ike_sa_t
*this)
1126 return this->auth_verify
;
1130 * Implementation of ike_sa_t.get_id.
1132 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1134 return this->ike_sa_id
;
1138 * Implementation of ike_sa_t.get_my_id.
1140 static identification_t
* get_my_id(private_ike_sa_t
*this)
1146 * Implementation of ike_sa_t.set_my_id.
1148 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1150 DESTROY_IF(this->my_id
);
1155 * Implementation of ike_sa_t.get_other_id.
1157 static identification_t
* get_other_id(private_ike_sa_t
*this)
1159 return this->other_id
;
1163 * Implementation of ike_sa_t.set_other_id.
1165 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1167 DESTROY_IF(this->other_id
);
1168 this->other_id
= other
;
1172 * Implementation of ike_sa_t.set_virtual_ip
1174 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
1178 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
1179 if (this->my_virtual_ip
)
1181 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
1182 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
1183 this->my_virtual_ip
,
1185 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
1187 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
1188 this->my_host
) == SUCCESS
)
1190 this->my_virtual_ip
= ip
->clone(ip
);
1194 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
1195 this->my_virtual_ip
= NULL
;
1200 DESTROY_IF(this->other_virtual_ip
);
1201 this->other_virtual_ip
= ip
->clone(ip
);
1206 * Implementation of ike_sa_t.get_virtual_ip
1208 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
1212 return this->my_virtual_ip
;
1216 return this->other_virtual_ip
;
1221 * Implementation of ike_sa_t.derive_keys.
1223 static status_t
derive_keys(private_ike_sa_t
*this,
1224 proposal_t
*proposal
, chunk_t secret
,
1225 chunk_t nonce_i
, chunk_t nonce_r
,
1226 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1228 prf_plus_t
*prf_plus
;
1229 chunk_t skeyseed
, key
, nonces
, prf_plus_seed
;
1232 crypter_t
*crypter_i
, *crypter_r
;
1233 signer_t
*signer_i
, *signer_r
;
1234 prf_t
*prf_i
, *prf_r
;
1235 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1236 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1237 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1239 /* Create SAs general purpose PRF first, we may use it here */
1240 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
1242 DBG1(DBG_IKE
, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1245 this->prf
= prf_create(algo
->algorithm
);
1246 if (this->prf
== NULL
)
1248 DBG1(DBG_IKE
, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1249 "%N not supported!", pseudo_random_function_names
, algo
->algorithm
);
1253 DBG4(DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1254 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1255 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1256 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1257 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1259 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1261 * if we are rekeying, SKEYSEED is built on another way
1263 if (child_prf
== NULL
) /* not rekeying */
1265 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1266 this->prf
->set_key(this->prf
, nonces
);
1267 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1268 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1269 this->prf
->set_key(this->prf
, skeyseed
);
1270 chunk_free(&skeyseed
);
1271 chunk_free(&secret
);
1272 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1276 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1277 * use OLD SAs PRF functions for both prf_plus and prf */
1278 secret
= chunk_cat("mc", secret
, nonces
);
1279 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1280 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1281 old_prf
->set_key(old_prf
, skeyseed
);
1282 chunk_free(&skeyseed
);
1283 chunk_free(&secret
);
1284 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1286 chunk_free(&nonces
);
1287 chunk_free(&prf_plus_seed
);
1289 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1291 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1292 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1293 this->child_prf
= prf_create(algo
->algorithm
);
1294 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1295 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1296 DBG4(DBG_IKE
, "Sk_d secret %B", &key
);
1297 this->child_prf
->set_key(this->child_prf
, key
);
1300 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1301 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1303 DBG1(DBG_IKE
, "key derivation failed: no INTEGRITY_ALGORITHM");
1306 signer_i
= signer_create(algo
->algorithm
);
1307 signer_r
= signer_create(algo
->algorithm
);
1308 if (signer_i
== NULL
|| signer_r
== NULL
)
1310 DBG1(DBG_IKE
, "key derivation failed: INTEGRITY_ALGORITHM "
1311 "%N not supported!", integrity_algorithm_names
,algo
->algorithm
);
1314 key_size
= signer_i
->get_key_size(signer_i
);
1316 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1317 DBG4(DBG_IKE
, "Sk_ai secret %B", &key
);
1318 signer_i
->set_key(signer_i
, key
);
1321 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1322 DBG4(DBG_IKE
, "Sk_ar secret %B", &key
);
1323 signer_r
->set_key(signer_r
, key
);
1328 this->signer_in
= signer_r
;
1329 this->signer_out
= signer_i
;
1333 this->signer_in
= signer_i
;
1334 this->signer_out
= signer_r
;
1337 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1338 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1340 DBG1(DBG_IKE
, "key derivation failed: no ENCRYPTION_ALGORITHM");
1343 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1344 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1345 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1347 DBG1(DBG_IKE
, "key derivation failed: ENCRYPTION_ALGORITHM "
1348 "%N (key size %d) not supported!",
1349 encryption_algorithm_names
, algo
->algorithm
, algo
->key_size
);
1352 key_size
= crypter_i
->get_key_size(crypter_i
);
1354 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1355 DBG4(DBG_IKE
, "Sk_ei secret %B", &key
);
1356 crypter_i
->set_key(crypter_i
, key
);
1359 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1360 DBG4(DBG_IKE
, "Sk_er secret %B", &key
);
1361 crypter_r
->set_key(crypter_r
, key
);
1366 this->crypter_in
= crypter_r
;
1367 this->crypter_out
= crypter_i
;
1371 this->crypter_in
= crypter_i
;
1372 this->crypter_out
= crypter_r
;
1375 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1376 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1377 prf_i
= prf_create(algo
->algorithm
);
1378 prf_r
= prf_create(algo
->algorithm
);
1380 key_size
= prf_i
->get_key_size(prf_i
);
1381 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1382 DBG4(DBG_IKE
, "Sk_pi secret %B", &key
);
1383 prf_i
->set_key(prf_i
, key
);
1386 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1387 DBG4(DBG_IKE
, "Sk_pr secret %B", &key
);
1388 prf_r
->set_key(prf_r
, key
);
1393 this->auth_verify
= prf_r
;
1394 this->auth_build
= prf_i
;
1398 this->auth_verify
= prf_i
;
1399 this->auth_build
= prf_r
;
1402 /* all done, prf_plus not needed anymore */
1403 prf_plus
->destroy(prf_plus
);
1409 * Implementation of ike_sa_t.add_child_sa.
1411 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1413 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1417 * Implementation of ike_sa_t.get_child_sa.
1419 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1420 u_int32_t spi
, bool inbound
)
1422 iterator_t
*iterator
;
1423 child_sa_t
*current
, *found
= NULL
;
1425 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1426 while (iterator
->iterate(iterator
, (void**)¤t
))
1428 if (current
->get_spi(current
, inbound
) == spi
&&
1429 current
->get_protocol(current
) == protocol
)
1434 iterator
->destroy(iterator
);
1439 * Implementation of ike_sa_t.create_child_sa_iterator.
1441 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1443 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1447 * Implementation of ike_sa_t.rekey_child_sa.
1449 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1451 child_sa_t
*child_sa
;
1452 child_rekey_t
*child_rekey
;
1454 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1457 child_rekey
= child_rekey_create(&this->public, child_sa
);
1458 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1459 return this->task_manager
->initiate(this->task_manager
);
1465 * Implementation of ike_sa_t.delete_child_sa.
1467 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1469 child_sa_t
*child_sa
;
1470 child_delete_t
*child_delete
;
1472 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1475 child_delete
= child_delete_create(&this->public, child_sa
);
1476 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1477 return this->task_manager
->initiate(this->task_manager
);
1483 * Implementation of ike_sa_t.destroy_child_sa.
1485 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1488 iterator_t
*iterator
;
1489 child_sa_t
*child_sa
;
1490 status_t status
= NOT_FOUND
;
1492 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1493 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1495 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1496 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1498 child_sa
->destroy(child_sa
);
1499 iterator
->remove(iterator
);
1504 iterator
->destroy(iterator
);
1509 * Implementation of public_ike_sa_t.delete.
1511 static status_t
delete_(private_ike_sa_t
*this)
1513 ike_delete_t
*ike_delete
;
1515 switch (this->state
)
1517 case IKE_ESTABLISHED
:
1518 DBG1(DBG_IKE
, "deleting IKE_SA");
1519 /* do not log when rekeyed */
1521 ike_delete
= ike_delete_create(&this->public, TRUE
);
1522 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1523 return this->task_manager
->initiate(this->task_manager
);
1525 DBG1(DBG_IKE
, "destroying IKE_SA in state %N without notification",
1526 ike_sa_state_names
, this->state
);
1533 * Implementation of ike_sa_t.rekey.
1535 static status_t
rekey(private_ike_sa_t
*this)
1537 ike_rekey_t
*ike_rekey
;
1539 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1541 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1542 return this->task_manager
->initiate(this->task_manager
);
1546 * Implementation of ike_sa_t.reestablish
1548 static void reestablish(private_ike_sa_t
*this)
1550 private_ike_sa_t
*other
;
1551 iterator_t
*iterator
;
1552 child_sa_t
*child_sa
;
1553 child_cfg_t
*child_cfg
;
1557 other
= (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1558 charon
->ike_sa_manager
, TRUE
);
1560 set_peer_cfg(other
, this->peer_cfg
);
1561 other
->other_host
->destroy(other
->other_host
);
1562 other
->other_host
= this->other_host
->clone(this->other_host
);
1563 if (this->my_virtual_ip
)
1565 /* if we already have a virtual IP, we reuse it */
1566 set_virtual_ip(other
, TRUE
, this->my_virtual_ip
);
1569 if (this->state
== IKE_ESTABLISHED
)
1571 task
= (task_t
*)ike_init_create(&other
->public, TRUE
, NULL
);
1572 other
->task_manager
->queue_task(other
->task_manager
, task
);
1573 task
= (task_t
*)ike_natd_create(&other
->public, TRUE
);
1574 other
->task_manager
->queue_task(other
->task_manager
, task
);
1575 task
= (task_t
*)ike_cert_create(&other
->public, TRUE
);
1576 other
->task_manager
->queue_task(other
->task_manager
, task
);
1577 task
= (task_t
*)ike_config_create(&other
->public, TRUE
);
1578 other
->task_manager
->queue_task(other
->task_manager
, task
);
1579 task
= (task_t
*)ike_auth_create(&other
->public, TRUE
);
1580 other
->task_manager
->queue_task(other
->task_manager
, task
);
1583 other
->task_manager
->adopt_tasks(other
->task_manager
, this->task_manager
);
1585 /* Create task for established children, adopt routed children directly */
1586 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1587 while(iterator
->iterate(iterator
, (void**)&child_sa
))
1589 switch (child_sa
->get_state(child_sa
))
1593 iterator
->remove(iterator
);
1594 other
->child_sas
->insert_first(other
->child_sas
, child_sa
);
1599 child_cfg
= child_sa
->get_config(child_sa
);
1600 task
= (task_t
*)child_create_create(&other
->public, child_cfg
);
1601 other
->task_manager
->queue_task(other
->task_manager
, task
);
1606 iterator
->destroy(iterator
);
1608 other
->task_manager
->initiate(other
->task_manager
);
1610 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &other
->public);
1612 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
1613 charon
->job_queue
->add(charon
->job_queue
, job
);
1617 * Implementation of ike_sa_t.inherit.
1619 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1621 child_sa_t
*child_sa
;
1624 /* apply hosts and ids */
1625 this->my_host
->destroy(this->my_host
);
1626 this->other_host
->destroy(this->other_host
);
1627 this->my_id
->destroy(this->my_id
);
1628 this->other_id
->destroy(this->other_id
);
1629 this->my_host
= other
->my_host
->clone(other
->my_host
);
1630 this->other_host
= other
->other_host
->clone(other
->other_host
);
1631 this->my_id
= other
->my_id
->clone(other
->my_id
);
1632 this->other_id
= other
->other_id
->clone(other
->other_id
);
1634 /* apply virtual assigned IPs... */
1635 if (other
->my_virtual_ip
)
1637 this->my_virtual_ip
= other
->my_virtual_ip
;
1638 other
->my_virtual_ip
= NULL
;
1640 if (other
->other_virtual_ip
)
1642 this->other_virtual_ip
= other
->other_virtual_ip
;
1643 other
->other_virtual_ip
= NULL
;
1646 /* ... and DNS servers */
1647 while (other
->dns_servers
->remove_last(other
->dns_servers
,
1648 (void**)&ip
) == SUCCESS
)
1650 this->dns_servers
->insert_first(this->dns_servers
, ip
);
1653 /* adopt all children */
1654 while (other
->child_sas
->remove_last(other
->child_sas
,
1655 (void**)&child_sa
) == SUCCESS
)
1657 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1660 /* move pending tasks to the new IKE_SA */
1661 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
1663 /* we have to initate here, there may be new tasks to handle */
1664 return this->task_manager
->initiate(this->task_manager
);
1668 * Implementation of ike_sa_t.is_natt_enabled.
1670 static bool is_natt_enabled(private_ike_sa_t
*this)
1672 return this->nat_here
|| this->nat_there
;
1676 * Implementation of ike_sa_t.enable_natt.
1678 static void enable_natt(private_ike_sa_t
*this, bool local
)
1682 DBG1(DBG_IKE
, "local host is behind NAT, scheduling keep alives");
1683 this->nat_here
= TRUE
;
1684 send_keepalive(this);
1688 DBG1(DBG_IKE
, "remote host is behind NAT");
1689 this->nat_there
= TRUE
;
1694 * Implementation of ike_sa_t.remove_dns_server
1696 static void remove_dns_servers(private_ike_sa_t
*this)
1700 chunk_t contents
, line
, orig_line
, token
;
1701 char string
[INET6_ADDRSTRLEN
];
1703 iterator_t
*iterator
;
1705 if (this->dns_servers
->get_count(this->dns_servers
) == 0)
1707 /* don't touch anything if we have no nameservers installed */
1711 file
= fopen(RESOLV_CONF
, "r");
1712 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
1714 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %m", RESOLV_CONF
);
1718 contents
= chunk_alloca((size_t)stats
.st_size
);
1720 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
1722 DBG1(DBG_IKE
, "unable to read DNS configuration file: %m");
1728 file
= fopen(RESOLV_CONF
, "w");
1731 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %m", RESOLV_CONF
);
1735 iterator
= this->dns_servers
->create_iterator(this->dns_servers
, TRUE
);
1736 while (fetchline(&contents
, &line
))
1740 if (extract_token(&token
, ' ', &line
) &&
1741 strncasecmp(token
.ptr
, "nameserver", token
.len
) == 0)
1743 if (!extract_token(&token
, ' ', &line
))
1747 iterator
->reset(iterator
);
1748 while (iterator
->iterate(iterator
, (void**)&ip
))
1750 snprintf(string
, sizeof(string
), "%H", ip
);
1751 if (strlen(string
) == token
.len
&&
1752 strncmp(token
.ptr
, string
, token
.len
) == 0)
1754 iterator
->remove(iterator
);
1764 /* write line untouched back to file */
1765 fwrite(orig_line
.ptr
, orig_line
.len
, 1, file
);
1766 fprintf(file
, "\n");
1769 iterator
->destroy(iterator
);
1774 * Implementation of ike_sa_t.add_dns_server
1776 static void add_dns_server(private_ike_sa_t
*this, host_t
*dns
)
1782 DBG1(DBG_IKE
, "installing DNS server %H", dns
);
1784 file
= fopen(RESOLV_CONF
, "a+");
1785 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
1787 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %m", RESOLV_CONF
);
1791 contents
= chunk_alloca(stats
.st_size
);
1793 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
1795 DBG1(DBG_IKE
, "unable to read DNS configuration file: %m");
1801 file
= fopen(RESOLV_CONF
, "w");
1804 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %m", RESOLV_CONF
);
1808 if (fprintf(file
, "nameserver %H # added by strongSwan, assigned by %D\n",
1809 dns
, this->other_id
) < 0)
1811 DBG1(DBG_IKE
, "unable to write DNS configuration: %m");
1815 this->dns_servers
->insert_last(this->dns_servers
, dns
->clone(dns
));
1817 fwrite(contents
.ptr
, contents
.len
, 1, file
);
1823 * Implementation of ike_sa_t.destroy.
1825 static void destroy(private_ike_sa_t
*this)
1827 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
1829 DESTROY_IF(this->crypter_in
);
1830 DESTROY_IF(this->crypter_out
);
1831 DESTROY_IF(this->signer_in
);
1832 DESTROY_IF(this->signer_out
);
1833 DESTROY_IF(this->prf
);
1834 DESTROY_IF(this->child_prf
);
1835 DESTROY_IF(this->auth_verify
);
1836 DESTROY_IF(this->auth_build
);
1838 if (this->my_virtual_ip
)
1840 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
1841 this->my_virtual_ip
, this->my_host
);
1842 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
1844 DESTROY_IF(this->other_virtual_ip
);
1846 remove_dns_servers(this);
1847 this->dns_servers
->destroy_offset(this->dns_servers
, offsetof(host_t
, destroy
));
1849 DESTROY_IF(this->my_host
);
1850 DESTROY_IF(this->other_host
);
1851 DESTROY_IF(this->my_id
);
1852 DESTROY_IF(this->other_id
);
1854 DESTROY_IF(this->ike_cfg
);
1855 DESTROY_IF(this->peer_cfg
);
1857 this->ike_sa_id
->destroy(this->ike_sa_id
);
1858 this->task_manager
->destroy(this->task_manager
);
1863 * Described in header.
1865 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1867 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
1868 static u_int32_t unique_id
= 0;
1870 /* Public functions */
1871 this->public.get_state
= (ike_sa_state_t(*)(ike_sa_t
*)) get_state
;
1872 this->public.set_state
= (void(*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
1873 this->public.get_stats
= (void(*)(ike_sa_t
*,u_int32_t
*))get_stats
;
1874 this->public.get_name
= (char*(*)(ike_sa_t
*))get_name
;
1875 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
1876 this->public.initiate
= (status_t(*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
1877 this->public.route
= (status_t(*)(ike_sa_t
*,child_cfg_t
*)) route
;
1878 this->public.unroute
= (status_t(*)(ike_sa_t
*,child_cfg_t
*)) unroute
;
1879 this->public.acquire
= (status_t(*)(ike_sa_t
*,u_int32_t
)) acquire
;
1880 this->public.get_ike_cfg
= (ike_cfg_t
*(*)(ike_sa_t
*))get_ike_cfg
;
1881 this->public.set_ike_cfg
= (void(*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
1882 this->public.get_peer_cfg
= (peer_cfg_t
*(*)(ike_sa_t
*))get_peer_cfg
;
1883 this->public.set_peer_cfg
= (void(*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
1884 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
1885 this->public.get_my_host
= (host_t
*(*)(ike_sa_t
*)) get_my_host
;
1886 this->public.set_my_host
= (void(*)(ike_sa_t
*,host_t
*)) set_my_host
;
1887 this->public.get_other_host
= (host_t
*(*)(ike_sa_t
*)) get_other_host
;
1888 this->public.set_other_host
= (void(*)(ike_sa_t
*,host_t
*)) set_other_host
;
1889 this->public.get_my_id
= (identification_t
*(*)(ike_sa_t
*)) get_my_id
;
1890 this->public.set_my_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_my_id
;
1891 this->public.get_other_id
= (identification_t
*(*)(ike_sa_t
*)) get_other_id
;
1892 this->public.set_other_id
= (void(*)(ike_sa_t
*,identification_t
*)) set_other_id
;
1893 this->public.retransmit
= (status_t (*) (ike_sa_t
*, u_int32_t
)) retransmit
;
1894 this->public.delete = (status_t(*)(ike_sa_t
*))delete_
;
1895 this->public.destroy
= (void(*)(ike_sa_t
*))destroy
;
1896 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
1897 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
1898 this->public.get_prf
= (prf_t
*(*) (ike_sa_t
*)) get_prf
;
1899 this->public.get_child_prf
= (prf_t
*(*) (ike_sa_t
*)) get_child_prf
;
1900 this->public.get_auth_verify
= (prf_t
*(*) (ike_sa_t
*)) get_auth_verify
;
1901 this->public.get_auth_build
= (prf_t
*(*) (ike_sa_t
*)) get_auth_build
;
1902 this->public.derive_keys
= (status_t (*) (ike_sa_t
*,proposal_t
*,chunk_t
,chunk_t
,chunk_t
,bool,prf_t
*,prf_t
*)) derive_keys
;
1903 this->public.add_child_sa
= (void (*) (ike_sa_t
*,child_sa_t
*)) add_child_sa
;
1904 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
1905 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
1906 this->public.rekey_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
1907 this->public.delete_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
1908 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
1909 this->public.enable_natt
= (void(*)(ike_sa_t
*, bool)) enable_natt
;
1910 this->public.is_natt_enabled
= (bool(*)(ike_sa_t
*)) is_natt_enabled
;
1911 this->public.rekey
= (status_t(*)(ike_sa_t
*))rekey
;
1912 this->public.reestablish
= (void(*)(ike_sa_t
*))reestablish
;
1913 this->public.inherit
= (status_t(*)(ike_sa_t
*,ike_sa_t
*))inherit
;
1914 this->public.generate_message
= (status_t(*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
1915 this->public.reset
= (void(*)(ike_sa_t
*))reset
;
1916 this->public.get_unique_id
= (u_int32_t(*)(ike_sa_t
*))get_unique_id
;
1917 this->public.set_virtual_ip
= (void(*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
1918 this->public.get_virtual_ip
= (host_t
*(*)(ike_sa_t
*,bool))get_virtual_ip
;
1919 this->public.add_dns_server
= (void(*)(ike_sa_t
*,host_t
*))add_dns_server
;
1921 /* initialize private fields */
1922 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1923 this->child_sas
= linked_list_create();
1924 this->my_host
= host_create_any(AF_INET
);
1925 this->other_host
= host_create_any(AF_INET
);
1926 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
1927 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
1928 this->crypter_in
= NULL
;
1929 this->crypter_out
= NULL
;
1930 this->signer_in
= NULL
;
1931 this->signer_out
= NULL
;
1933 this->auth_verify
= NULL
;
1934 this->auth_build
= NULL
;
1935 this->child_prf
= NULL
;
1936 this->nat_here
= FALSE
;
1937 this->nat_there
= FALSE
;
1938 this->state
= IKE_CREATED
;
1939 this->time
.inbound
= this->time
.outbound
= time(NULL
);
1940 this->time
.established
= 0;
1941 this->time
.rekey
= 0;
1942 this->time
.delete = 0;
1943 this->ike_cfg
= NULL
;
1944 this->peer_cfg
= NULL
;
1945 this->task_manager
= task_manager_create(&this->public);
1946 this->unique_id
= ++unique_id
;
1947 this->my_virtual_ip
= NULL
;
1948 this->other_virtual_ip
= NULL
;
1949 this->dns_servers
= linked_list_create();
1950 this->keyingtry
= 0;
1952 return &this->public;