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
35 #include <utils/linked_list.h>
36 #include <utils/lexparser.h>
37 #include <crypto/diffie_hellman.h>
38 #include <crypto/prf_plus.h>
39 #include <crypto/crypters/crypter.h>
40 #include <crypto/hashers/hasher.h>
41 #include <encoding/payloads/sa_payload.h>
42 #include <encoding/payloads/nonce_payload.h>
43 #include <encoding/payloads/ke_payload.h>
44 #include <encoding/payloads/delete_payload.h>
45 #include <encoding/payloads/transform_substructure.h>
46 #include <encoding/payloads/transform_attribute.h>
47 #include <encoding/payloads/ts_payload.h>
48 #include <sa/task_manager.h>
49 #include <sa/tasks/ike_init.h>
50 #include <sa/tasks/ike_natd.h>
51 #include <sa/tasks/ike_auth.h>
52 #include <sa/tasks/ike_config.h>
53 #include <sa/tasks/ike_cert.h>
54 #include <sa/tasks/ike_rekey.h>
55 #include <sa/tasks/ike_reauth.h>
56 #include <sa/tasks/ike_delete.h>
57 #include <sa/tasks/ike_dpd.h>
58 #include <sa/tasks/child_create.h>
59 #include <sa/tasks/child_delete.h>
60 #include <sa/tasks/child_rekey.h>
61 #include <processing/jobs/retransmit_job.h>
62 #include <processing/jobs/delete_ike_sa_job.h>
63 #include <processing/jobs/send_dpd_job.h>
64 #include <processing/jobs/send_keepalive_job.h>
65 #include <processing/jobs/rekey_ike_sa_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 * CA that issued the certificate of other
148 * set of extensions the peer supports
150 ike_extension_t extensions
;
153 * Linked List containing the child sa's of the current IKE_SA.
155 linked_list_t
*child_sas
;
158 * crypter for inbound traffic
160 crypter_t
*crypter_in
;
163 * crypter for outbound traffic
165 crypter_t
*crypter_out
;
168 * Signer for inbound traffic
173 * Signer for outbound traffic
175 signer_t
*signer_out
;
178 * Multi purpose prf, set key, use it, forget it
183 * Prf function for derivating keymat child SAs
188 * Key to build outging authentication data (SKp)
193 * Key to verify incoming authentication data (SKp)
198 * NAT status of local host.
203 * NAT status of remote host.
208 * Virtual IP on local host, if any
210 host_t
*my_virtual_ip
;
213 * Virtual IP on remote host, if any
215 host_t
*other_virtual_ip
;
218 * List of DNS servers installed by us
220 linked_list_t
*dns_servers
;
223 * Timestamps for this IKE_SA
226 /** last IKE message received */
228 /** last IKE message sent */
230 /** when IKE_SA became established */
231 u_int32_t established
;
232 /** when IKE_SA gets rekeyed */
234 /** when IKE_SA gets deleted */
239 * how many times we have retried so far (keyingtries)
245 * get the time of the latest traffic processed by the kernel
247 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
249 iterator_t
*iterator
;
250 child_sa_t
*child_sa
;
251 time_t latest
= 0, use_time
;
253 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
254 while (iterator
->iterate(iterator
, (void**)&child_sa
))
256 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
258 latest
= max(latest
, use_time
);
261 iterator
->destroy(iterator
);
265 return max(this->time
.inbound
, latest
);
269 return max(this->time
.outbound
, latest
);
274 * Implementation of ike_sa_t.get_unique_id
276 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
278 return this->unique_id
;
282 * Implementation of ike_sa_t.get_name.
284 static char *get_name(private_ike_sa_t
*this)
288 return this->peer_cfg
->get_name(this->peer_cfg
);
295 * Implementation of ike_sa_t.get_stats.
297 static void get_stats(private_ike_sa_t
*this, u_int32_t
*next_rekeying
)
301 *next_rekeying
= this->time
.rekey
;
306 * Implementation of ike_sa_t.get_my_host.
308 static host_t
*get_my_host(private_ike_sa_t
*this)
310 return this->my_host
;
314 * Implementation of ike_sa_t.set_my_host.
316 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
318 DESTROY_IF(this->my_host
);
323 * Implementation of ike_sa_t.get_other_host.
325 static host_t
*get_other_host(private_ike_sa_t
*this)
327 return this->other_host
;
331 * Implementation of ike_sa_t.set_other_host.
333 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
335 DESTROY_IF(this->other_host
);
336 this->other_host
= other
;
340 * Implementation of ike_sa_t.get_peer_cfg
342 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
344 return this->peer_cfg
;
348 * Implementation of ike_sa_t.set_peer_cfg
350 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
352 peer_cfg
->get_ref(peer_cfg
);
353 this->peer_cfg
= peer_cfg
;
355 if (this->ike_cfg
== NULL
)
357 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
358 this->ike_cfg
->get_ref(this->ike_cfg
);
361 /* apply values, so we are ready to initate/acquire */
362 if (this->my_host
->is_anyaddr(this->my_host
))
364 host_t
*me
= this->ike_cfg
->get_my_host(this->ike_cfg
);
365 set_my_host(this, me
->clone(me
));
367 if (this->other_host
->is_anyaddr(this->other_host
))
369 host_t
*other
= this->ike_cfg
->get_other_host(this->ike_cfg
);
370 set_other_host(this, other
->clone(other
));
372 /* apply IDs if they are not already set */
373 if (this->my_id
->contains_wildcards(this->my_id
))
375 DESTROY_IF(this->my_id
);
376 this->my_id
= this->peer_cfg
->get_my_id(this->peer_cfg
);
377 this->my_id
= this->my_id
->clone(this->my_id
);
379 if (this->other_id
->contains_wildcards(this->other_id
))
381 DESTROY_IF(this->other_id
);
382 this->other_id
= this->peer_cfg
->get_other_id(this->peer_cfg
);
383 this->other_id
= this->other_id
->clone(this->other_id
);
388 * Implementation of ike_sa_t.get_ike_cfg
390 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
392 return this->ike_cfg
;
396 * Implementation of ike_sa_t.set_ike_cfg
398 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
400 ike_cfg
->get_ref(ike_cfg
);
401 this->ike_cfg
= ike_cfg
;
405 * Implementation of ike_sa_t.send_dpd
407 static status_t
send_dpd(private_ike_sa_t
*this)
412 delay
= this->peer_cfg
->get_dpd_delay(this->peer_cfg
);
420 if (this->task_manager
->busy(this->task_manager
))
422 /* an exchange is in the air, no need to start a DPD check */
427 /* check if there was any inbound traffic */
429 last_in
= get_use_time(this, TRUE
);
431 diff
= now
- last_in
;
434 /* to long ago, initiate dead peer detection */
437 task
= (task_t
*)ike_dpd_create(TRUE
);
439 DBG1(DBG_IKE
, "sending DPD request");
441 this->task_manager
->queue_task(this->task_manager
, task
);
442 this->task_manager
->initiate(this->task_manager
);
445 /* recheck in "interval" seconds */
446 job
= send_dpd_job_create(this->ike_sa_id
);
447 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
448 (delay
- diff
) * 1000);
453 * Implementation of ike_sa_t.send_keepalive
455 static void send_keepalive(private_ike_sa_t
*this)
457 send_keepalive_job_t
*job
;
458 time_t last_out
, now
, diff
;
460 last_out
= get_use_time(this, FALSE
);
463 diff
= now
- last_out
;
465 if (diff
>= KEEPALIVE_INTERVAL
)
470 packet
= packet_create();
471 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
472 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
473 data
.ptr
= malloc(1);
476 packet
->set_data(packet
, data
);
477 charon
->sender
->send(charon
->sender
, packet
);
478 DBG1(DBG_IKE
, "sending keep alive");
481 job
= send_keepalive_job_create(this->ike_sa_id
);
482 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
483 (KEEPALIVE_INTERVAL
- diff
) * 1000);
487 * Implementation of ike_sa_t.get_state.
489 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
495 * Implementation of ike_sa_t.set_state.
497 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
499 DBG1(DBG_IKE
, "IKE_SA state change: %N => %N",
500 ike_sa_state_names
, this->state
,
501 ike_sa_state_names
, state
);
505 case IKE_ESTABLISHED
:
507 if (this->state
== IKE_CONNECTING
)
510 u_int32_t now
= time(NULL
);
511 u_int32_t soft
, hard
;
514 this->time
.established
= now
;
515 /* start DPD checks */
518 /* schedule rekeying/reauthentication */
519 soft
= this->peer_cfg
->get_lifetime(this->peer_cfg
, TRUE
);
520 hard
= this->peer_cfg
->get_lifetime(this->peer_cfg
, FALSE
);
521 reauth
= this->peer_cfg
->use_reauth(this->peer_cfg
);
522 DBG1(DBG_IKE
, "scheduling %s in %ds, maximum lifetime %ds",
523 reauth ?
"reauthentication": "rekeying", soft
, hard
);
527 this->time
.rekey
= now
+ soft
;
528 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, reauth
);
529 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
535 this->time
.delete = now
+ hard
;
536 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
537 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
545 /* delete may fail if a packet gets lost, so set a timeout */
546 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
547 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
548 HALF_OPEN_IKE_SA_TIMEOUT
);
559 * Implementation of ike_sa_t.reset
561 static void reset(private_ike_sa_t
*this)
563 /* the responder ID is reset, as peer may choose another one */
564 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
566 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
569 set_state(this, IKE_CREATED
);
571 this->task_manager
->reset(this->task_manager
);
575 * Update hosts, as addresses may change (NAT)
577 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
579 iterator_t
*iterator
= NULL
;
580 child_sa_t
*child_sa
= NULL
;
581 host_diff_t my_diff
, other_diff
;
583 if (this->my_host
->is_anyaddr(this->my_host
) ||
584 this->other_host
->is_anyaddr(this->other_host
))
586 /* on first received message */
587 this->my_host
->destroy(this->my_host
);
588 this->my_host
= me
->clone(me
);
589 this->other_host
->destroy(this->other_host
);
590 this->other_host
= other
->clone(other
);
594 my_diff
= me
->get_differences(me
, this->my_host
);
595 other_diff
= other
->get_differences(other
, this->other_host
);
597 if (!my_diff
&& !other_diff
)
604 this->my_host
->destroy(this->my_host
);
605 this->my_host
= me
->clone(me
);
610 /* update without restrictions if we are not NATted */
613 this->other_host
->destroy(this->other_host
);
614 this->other_host
= other
->clone(other
);
619 /* if we are natted, only port may change */
620 if (other_diff
& HOST_DIFF_ADDR
)
624 else if (other_diff
& HOST_DIFF_PORT
)
626 this->other_host
->set_port(this->other_host
, other
->get_port(other
));
629 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
630 while (iterator
->iterate(iterator
, (void**)&child_sa
))
632 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
633 my_diff
, other_diff
);
635 iterator
->destroy(iterator
);
639 * Implementation of ike_sa_t.generate
641 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
644 this->time
.outbound
= time(NULL
);
645 message
->set_ike_sa_id(message
, this->ike_sa_id
);
646 message
->set_destination(message
, this->other_host
->clone(this->other_host
));
647 message
->set_source(message
, this->my_host
->clone(this->my_host
));
648 return message
->generate(message
, this->crypter_out
, this->signer_out
, packet
);
652 * send a notify back to the sender
654 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
660 response
= message_create();
661 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
662 response
->set_request(response
, FALSE
);
663 response
->set_message_id(response
, request
->get_message_id(request
));
664 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
665 if (this->my_host
->is_anyaddr(this->my_host
))
667 this->my_host
->destroy(this->my_host
);
668 this->my_host
= request
->get_destination(request
);
669 this->my_host
= this->my_host
->clone(this->my_host
);
671 if (this->other_host
->is_anyaddr(this->other_host
))
673 this->other_host
->destroy(this->other_host
);
674 this->other_host
= request
->get_source(request
);
675 this->other_host
= this->other_host
->clone(this->other_host
);
677 if (generate_message(this, response
, &packet
) == SUCCESS
)
679 charon
->sender
->send(charon
->sender
, packet
);
681 response
->destroy(response
);
685 * Implementation of ike_sa_t.process_message.
687 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
692 is_request
= message
->get_request(message
);
694 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
695 if (status
!= SUCCESS
)
703 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
706 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
710 DBG1(DBG_IKE
, "message parsing failed");
713 send_notify_response(this, message
, INVALID_SYNTAX
);
717 DBG1(DBG_IKE
, "message verification failed");
720 send_notify_response(this, message
, INVALID_SYNTAX
);
724 DBG1(DBG_IKE
, "integrity check failed");
728 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
731 send_notify_response(this, message
, INVALID_SYNTAX
);
737 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
738 exchange_type_names
, message
->get_exchange_type(message
),
739 message
->get_request(message
) ?
"request" : "response",
740 message
->get_message_id(message
));
747 me
= message
->get_destination(message
);
748 other
= message
->get_source(message
);
750 /* if this IKE_SA is virgin, we check for a config */
751 if (this->ike_cfg
== NULL
)
754 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
756 if (this->ike_cfg
== NULL
)
758 /* no config found for these hosts, destroy */
759 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
760 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
761 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
764 /* add a timeout if peer does not establish it completely */
765 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
766 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
767 HALF_OPEN_IKE_SA_TIMEOUT
);
770 /* check if message is trustworthy, and update host information */
771 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
772 message
->get_exchange_type(message
) != IKE_SA_INIT
)
774 update_hosts(this, me
, other
);
775 this->time
.inbound
= time(NULL
);
777 return this->task_manager
->process_message(this->task_manager
, message
);
782 * Implementation of ike_sa_t.initiate.
784 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
788 if (this->state
== IKE_CREATED
)
791 if (this->other_host
->is_anyaddr(this->other_host
))
793 child_cfg
->destroy(child_cfg
);
794 SIG(IKE_UP_START
, "initiating IKE_SA");
795 SIG(IKE_UP_FAILED
, "unable to initiate to %%any");
799 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
800 this->task_manager
->queue_task(this->task_manager
, task
);
801 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
802 this->task_manager
->queue_task(this->task_manager
, task
);
803 task
= (task_t
*)ike_cert_create(&this->public, TRUE
);
804 this->task_manager
->queue_task(this->task_manager
, task
);
805 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
806 this->task_manager
->queue_task(this->task_manager
, task
);
807 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
808 this->task_manager
->queue_task(this->task_manager
, task
);
811 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
812 child_cfg
->destroy(child_cfg
);
813 this->task_manager
->queue_task(this->task_manager
, task
);
815 return this->task_manager
->initiate(this->task_manager
);
819 * Implementation of ike_sa_t.acquire.
821 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
823 child_cfg_t
*child_cfg
;
824 iterator_t
*iterator
;
825 child_sa_t
*current
, *child_sa
= NULL
;
827 child_create_t
*child_create
;
829 if (this->state
== IKE_DELETING
)
831 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
832 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
833 "IKE_SA is deleting", reqid
);
838 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
839 while (iterator
->iterate(iterator
, (void**)¤t
))
841 if (current
->get_reqid(current
) == reqid
)
847 iterator
->destroy(iterator
);
850 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
851 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
852 "CHILD_SA not found", reqid
);
857 if (this->state
== IKE_CREATED
)
859 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
860 this->task_manager
->queue_task(this->task_manager
, task
);
861 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
862 this->task_manager
->queue_task(this->task_manager
, task
);
863 task
= (task_t
*)ike_cert_create(&this->public, TRUE
);
864 this->task_manager
->queue_task(this->task_manager
, task
);
865 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
866 this->task_manager
->queue_task(this->task_manager
, task
);
867 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
868 this->task_manager
->queue_task(this->task_manager
, task
);
871 child_cfg
= child_sa
->get_config(child_sa
);
872 child_create
= child_create_create(&this->public, child_cfg
);
873 child_create
->use_reqid(child_create
, reqid
);
874 this->task_manager
->queue_task(this->task_manager
, (task_t
*)child_create
);
876 return this->task_manager
->initiate(this->task_manager
);
880 * Implementation of ike_sa_t.route.
882 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
884 child_sa_t
*child_sa
;
885 iterator_t
*iterator
;
886 linked_list_t
*my_ts
, *other_ts
;
889 SIG(CHILD_ROUTE_START
, "routing CHILD_SA");
891 /* check if not already routed*/
892 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
893 while (iterator
->iterate(iterator
, (void**)&child_sa
))
895 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
896 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
898 iterator
->destroy(iterator
);
899 SIG(CHILD_ROUTE_FAILED
, "CHILD_SA with such a config already routed");
903 iterator
->destroy(iterator
);
909 SIG(CHILD_ROUTE_FAILED
,
910 "unable to route CHILD_SA, as its IKE_SA gets deleted");
914 case IKE_ESTABLISHED
:
919 /* install kernel policies */
920 child_sa
= child_sa_create(this->my_host
, this->other_host
, this->my_id
,
921 this->other_id
, child_cfg
, FALSE
, 0);
923 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
,
925 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
,
927 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
,
928 child_cfg
->get_mode(child_cfg
));
929 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
930 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
931 if (status
== SUCCESS
)
933 this->child_sas
->insert_last(this->child_sas
, child_sa
);
934 SIG(CHILD_ROUTE_SUCCESS
, "CHILD_SA routed");
938 SIG(CHILD_ROUTE_FAILED
, "routing CHILD_SA failed");
944 * Implementation of ike_sa_t.unroute.
946 static status_t
unroute(private_ike_sa_t
*this, u_int32_t reqid
)
948 iterator_t
*iterator
;
949 child_sa_t
*child_sa
;
952 SIG(CHILD_UNROUTE_START
, "unrouting CHILD_SA");
954 /* find CHILD_SA in ROUTED state */
955 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
956 while (iterator
->iterate(iterator
, (void**)&child_sa
))
958 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
959 child_sa
->get_reqid(child_sa
) == reqid
)
961 iterator
->remove(iterator
);
962 SIG(CHILD_UNROUTE_SUCCESS
, "CHILD_SA unrouted");
963 child_sa
->destroy(child_sa
);
968 iterator
->destroy(iterator
);
972 SIG(CHILD_UNROUTE_FAILED
, "CHILD_SA to unroute not found");
975 /* if we are not established, and we have no more routed childs, remove whole SA */
976 if (this->state
== IKE_CREATED
&&
977 this->child_sas
->get_count(this->child_sas
) == 0)
985 * Implementation of ike_sa_t.retransmit.
987 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
989 this->time
.outbound
= time(NULL
);
990 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
992 child_cfg_t
*child_cfg
;
993 child_sa_t
* child_sa
;
994 linked_list_t
*to_route
, *to_restart
;
995 iterator_t
*iterator
;
997 /* send a proper signal to brief interested bus listeners */
1000 case IKE_CONNECTING
:
1002 /* retry IKE_SA_INIT if we have multiple keyingtries */
1003 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1005 if (tries
== 0 || tries
> this->keyingtry
)
1007 SIG(IKE_UP_FAILED
, "peer not responding, trying again "
1008 "(%d/%d) in background ", this->keyingtry
+ 1, tries
);
1010 return this->task_manager
->initiate(this->task_manager
);
1012 SIG(IKE_UP_FAILED
, "establishing IKE_SA failed, peer not responding");
1016 SIG(IKE_REKEY_FAILED
, "rekeying IKE_SA failed, peer not responding");
1019 SIG(IKE_DOWN_FAILED
, "proper IKE_SA delete failed, peer not responding");
1025 /* summarize how we have to handle each child */
1026 to_route
= linked_list_create();
1027 to_restart
= linked_list_create();
1028 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1029 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1031 child_cfg
= child_sa
->get_config(child_sa
);
1033 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1035 /* reroute routed CHILD_SAs */
1036 to_route
->insert_last(to_route
, child_cfg
);
1040 /* use DPD action for established CHILD_SAs */
1041 switch (this->peer_cfg
->get_dpd_action(this->peer_cfg
))
1044 to_route
->insert_last(to_route
, child_cfg
);
1047 to_restart
->insert_last(to_restart
, child_cfg
);
1054 iterator
->destroy(iterator
);
1056 /* create a new IKE_SA if we have to route or to restart */
1057 if (to_route
->get_count(to_route
) || to_restart
->get_count(to_restart
))
1059 private_ike_sa_t
*new;
1062 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1063 charon
->ike_sa_manager
, TRUE
);
1065 set_peer_cfg(new, this->peer_cfg
);
1066 /* use actual used host, not the wildcarded one in config */
1067 new->other_host
->destroy(new->other_host
);
1068 new->other_host
= this->other_host
->clone(this->other_host
);
1070 /* install routes */
1071 while (to_route
->remove_last(to_route
, (void**)&child_cfg
) == SUCCESS
)
1073 route(new, child_cfg
);
1076 /* restart children */
1077 if (to_restart
->get_count(to_restart
))
1079 task
= (task_t
*)ike_init_create(&new->public, TRUE
, NULL
);
1080 new->task_manager
->queue_task(new->task_manager
, task
);
1081 task
= (task_t
*)ike_natd_create(&new->public, TRUE
);
1082 new->task_manager
->queue_task(new->task_manager
, task
);
1083 task
= (task_t
*)ike_cert_create(&new->public, TRUE
);
1084 new->task_manager
->queue_task(new->task_manager
, task
);
1085 task
= (task_t
*)ike_config_create(&new->public, TRUE
);
1086 new->task_manager
->queue_task(new->task_manager
, task
);
1087 task
= (task_t
*)ike_auth_create(&new->public, TRUE
);
1088 new->task_manager
->queue_task(new->task_manager
, task
);
1090 while (to_restart
->remove_last(to_restart
, (void**)&child_cfg
) == SUCCESS
)
1092 task
= (task_t
*)child_create_create(&new->public, child_cfg
);
1093 new->task_manager
->queue_task(new->task_manager
, task
);
1095 new->task_manager
->initiate(new->task_manager
);
1097 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1099 to_route
->destroy(to_route
);
1100 to_restart
->destroy(to_restart
);
1107 * Implementation of ike_sa_t.get_prf.
1109 static prf_t
*get_prf(private_ike_sa_t
*this)
1115 * Implementation of ike_sa_t.get_prf.
1117 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1119 return this->child_prf
;
1123 * Implementation of ike_sa_t.get_skp_bild
1125 static chunk_t
get_skp_build(private_ike_sa_t
*this)
1127 return this->skp_build
;
1131 * Implementation of ike_sa_t.get_skp_verify
1133 static chunk_t
get_skp_verify(private_ike_sa_t
*this)
1135 return this->skp_verify
;
1139 * Implementation of ike_sa_t.get_id.
1141 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1143 return this->ike_sa_id
;
1147 * Implementation of ike_sa_t.get_my_id.
1149 static identification_t
* get_my_id(private_ike_sa_t
*this)
1155 * Implementation of ike_sa_t.set_my_id.
1157 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1159 DESTROY_IF(this->my_id
);
1164 * Implementation of ike_sa_t.get_other_id.
1166 static identification_t
* get_other_id(private_ike_sa_t
*this)
1168 return this->other_id
;
1172 * Implementation of ike_sa_t.set_other_id.
1174 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1176 DESTROY_IF(this->other_id
);
1177 this->other_id
= other
;
1181 * Implementation of ike_sa_t.get_other_ca.
1183 static ca_info_t
* get_other_ca(private_ike_sa_t
*this)
1185 return this->other_ca
;
1189 * Implementation of ike_sa_t.set_other_ca.
1191 static void set_other_ca(private_ike_sa_t
*this, ca_info_t
*other_ca
)
1193 this->other_ca
= other_ca
;
1197 * Implementation of ike_sa_t.set_virtual_ip
1199 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
1203 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
1204 if (this->my_virtual_ip
)
1206 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
1207 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
1208 this->my_virtual_ip
,
1210 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
1212 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
1213 this->my_host
) == SUCCESS
)
1215 this->my_virtual_ip
= ip
->clone(ip
);
1219 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
1220 this->my_virtual_ip
= NULL
;
1225 DESTROY_IF(this->other_virtual_ip
);
1226 this->other_virtual_ip
= ip
->clone(ip
);
1231 * Implementation of ike_sa_t.get_virtual_ip
1233 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
1237 return this->my_virtual_ip
;
1241 return this->other_virtual_ip
;
1246 * Implementation of ike_sa_t.enable_extension.
1248 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
1250 this->extensions
|= extension
;
1254 * Implementation of ike_sa_t.has_extension.
1256 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
1258 return this->extensions
& extension
;
1262 * Implementation of ike_sa_t.derive_keys.
1264 static status_t
derive_keys(private_ike_sa_t
*this,
1265 proposal_t
*proposal
, chunk_t secret
,
1266 chunk_t nonce_i
, chunk_t nonce_r
,
1267 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1269 prf_plus_t
*prf_plus
;
1270 chunk_t skeyseed
, key
, nonces
, prf_plus_seed
;
1273 crypter_t
*crypter_i
, *crypter_r
;
1274 signer_t
*signer_i
, *signer_r
;
1275 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1276 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1277 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1279 /* Create SAs general purpose PRF first, we may use it here */
1280 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
1282 DBG1(DBG_IKE
, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1285 this->prf
= prf_create(algo
->algorithm
);
1286 if (this->prf
== NULL
)
1288 DBG1(DBG_IKE
, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1289 "%N not supported!", pseudo_random_function_names
, algo
->algorithm
);
1293 DBG4(DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1294 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1295 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1296 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1297 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1299 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1301 * if we are rekeying, SKEYSEED is built on another way
1303 if (child_prf
== NULL
) /* not rekeying */
1305 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1306 this->prf
->set_key(this->prf
, nonces
);
1307 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1308 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1309 this->prf
->set_key(this->prf
, skeyseed
);
1310 chunk_free(&skeyseed
);
1311 chunk_free(&secret
);
1312 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1316 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1317 * use OLD SAs PRF functions for both prf_plus and prf */
1318 secret
= chunk_cat("mc", secret
, nonces
);
1319 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1320 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1321 old_prf
->set_key(old_prf
, skeyseed
);
1322 chunk_free(&skeyseed
);
1323 chunk_free(&secret
);
1324 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1326 chunk_free(&nonces
);
1327 chunk_free(&prf_plus_seed
);
1329 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1331 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1332 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1333 this->child_prf
= prf_create(algo
->algorithm
);
1334 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1335 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1336 DBG4(DBG_IKE
, "Sk_d secret %B", &key
);
1337 this->child_prf
->set_key(this->child_prf
, key
);
1340 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1341 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1343 DBG1(DBG_IKE
, "key derivation failed: no INTEGRITY_ALGORITHM");
1346 signer_i
= signer_create(algo
->algorithm
);
1347 signer_r
= signer_create(algo
->algorithm
);
1348 if (signer_i
== NULL
|| signer_r
== NULL
)
1350 DBG1(DBG_IKE
, "key derivation failed: INTEGRITY_ALGORITHM "
1351 "%N not supported!", integrity_algorithm_names
,algo
->algorithm
);
1354 key_size
= signer_i
->get_key_size(signer_i
);
1356 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1357 DBG4(DBG_IKE
, "Sk_ai secret %B", &key
);
1358 signer_i
->set_key(signer_i
, key
);
1361 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1362 DBG4(DBG_IKE
, "Sk_ar secret %B", &key
);
1363 signer_r
->set_key(signer_r
, key
);
1368 this->signer_in
= signer_r
;
1369 this->signer_out
= signer_i
;
1373 this->signer_in
= signer_i
;
1374 this->signer_out
= signer_r
;
1377 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1378 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1380 DBG1(DBG_IKE
, "key derivation failed: no ENCRYPTION_ALGORITHM");
1383 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1384 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1385 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1387 DBG1(DBG_IKE
, "key derivation failed: ENCRYPTION_ALGORITHM "
1388 "%N (key size %d) not supported!",
1389 encryption_algorithm_names
, algo
->algorithm
, algo
->key_size
);
1392 key_size
= crypter_i
->get_key_size(crypter_i
);
1394 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1395 DBG4(DBG_IKE
, "Sk_ei secret %B", &key
);
1396 crypter_i
->set_key(crypter_i
, key
);
1399 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1400 DBG4(DBG_IKE
, "Sk_er secret %B", &key
);
1401 crypter_r
->set_key(crypter_r
, key
);
1406 this->crypter_in
= crypter_r
;
1407 this->crypter_out
= crypter_i
;
1411 this->crypter_in
= crypter_i
;
1412 this->crypter_out
= crypter_r
;
1415 /* SK_pi/SK_pr used for authentication => stored for later */
1416 key_size
= this->prf
->get_key_size(this->prf
);
1417 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1418 DBG4(DBG_IKE
, "Sk_pi secret %B", &key
);
1421 this->skp_build
= key
;
1425 this->skp_verify
= key
;
1427 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1428 DBG4(DBG_IKE
, "Sk_pr secret %B", &key
);
1431 this->skp_verify
= key
;
1435 this->skp_build
= key
;
1438 /* all done, prf_plus not needed anymore */
1439 prf_plus
->destroy(prf_plus
);
1445 * Implementation of ike_sa_t.add_child_sa.
1447 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1449 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1453 * Implementation of ike_sa_t.get_child_sa.
1455 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1456 u_int32_t spi
, bool inbound
)
1458 iterator_t
*iterator
;
1459 child_sa_t
*current
, *found
= NULL
;
1461 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1462 while (iterator
->iterate(iterator
, (void**)¤t
))
1464 if (current
->get_spi(current
, inbound
) == spi
&&
1465 current
->get_protocol(current
) == protocol
)
1470 iterator
->destroy(iterator
);
1475 * Implementation of ike_sa_t.create_child_sa_iterator.
1477 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1479 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1483 * Implementation of ike_sa_t.rekey_child_sa.
1485 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1487 child_sa_t
*child_sa
;
1488 child_rekey_t
*child_rekey
;
1490 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1493 child_rekey
= child_rekey_create(&this->public, child_sa
);
1494 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1495 return this->task_manager
->initiate(this->task_manager
);
1501 * Implementation of ike_sa_t.delete_child_sa.
1503 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1505 child_sa_t
*child_sa
;
1506 child_delete_t
*child_delete
;
1508 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1511 child_delete
= child_delete_create(&this->public, child_sa
);
1512 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1513 return this->task_manager
->initiate(this->task_manager
);
1519 * Implementation of ike_sa_t.destroy_child_sa.
1521 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1524 iterator_t
*iterator
;
1525 child_sa_t
*child_sa
;
1526 status_t status
= NOT_FOUND
;
1528 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1529 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1531 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1532 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1534 child_sa
->destroy(child_sa
);
1535 iterator
->remove(iterator
);
1540 iterator
->destroy(iterator
);
1545 * Implementation of public_ike_sa_t.delete.
1547 static status_t
delete_(private_ike_sa_t
*this)
1549 ike_delete_t
*ike_delete
;
1551 switch (this->state
)
1553 case IKE_ESTABLISHED
:
1555 ike_delete
= ike_delete_create(&this->public, TRUE
);
1556 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1557 return this->task_manager
->initiate(this->task_manager
);
1559 DBG1(DBG_IKE
, "destroying IKE_SA in state %N without notification",
1560 ike_sa_state_names
, this->state
);
1567 * Implementation of ike_sa_t.rekey.
1569 static status_t
rekey(private_ike_sa_t
*this)
1571 ike_rekey_t
*ike_rekey
;
1573 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1575 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1576 return this->task_manager
->initiate(this->task_manager
);
1580 * Implementation of ike_sa_t.reestablish
1582 static status_t
reestablish(private_ike_sa_t
*this)
1586 task
= (task_t
*)ike_reauth_create(&this->public);
1587 this->task_manager
->queue_task(this->task_manager
, task
);
1589 return this->task_manager
->initiate(this->task_manager
);
1593 * Implementation of ike_sa_t.inherit.
1595 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1597 child_sa_t
*child_sa
;
1600 /* apply hosts and ids */
1601 this->my_host
->destroy(this->my_host
);
1602 this->other_host
->destroy(this->other_host
);
1603 this->my_id
->destroy(this->my_id
);
1604 this->other_id
->destroy(this->other_id
);
1605 this->my_host
= other
->my_host
->clone(other
->my_host
);
1606 this->other_host
= other
->other_host
->clone(other
->other_host
);
1607 this->my_id
= other
->my_id
->clone(other
->my_id
);
1608 this->other_id
= other
->other_id
->clone(other
->other_id
);
1610 /* apply virtual assigned IPs... */
1611 if (other
->my_virtual_ip
)
1613 this->my_virtual_ip
= other
->my_virtual_ip
;
1614 other
->my_virtual_ip
= NULL
;
1616 if (other
->other_virtual_ip
)
1618 this->other_virtual_ip
= other
->other_virtual_ip
;
1619 other
->other_virtual_ip
= NULL
;
1622 /* ... and DNS servers */
1623 while (other
->dns_servers
->remove_last(other
->dns_servers
,
1624 (void**)&ip
) == SUCCESS
)
1626 this->dns_servers
->insert_first(this->dns_servers
, ip
);
1629 /* adopt all children */
1630 while (other
->child_sas
->remove_last(other
->child_sas
,
1631 (void**)&child_sa
) == SUCCESS
)
1633 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1636 /* move pending tasks to the new IKE_SA */
1637 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
1639 /* we have to initate here, there may be new tasks to handle */
1640 return this->task_manager
->initiate(this->task_manager
);
1644 * Implementation of ike_sa_t.is_natt_enabled.
1646 static bool is_natt_enabled(private_ike_sa_t
*this)
1648 return this->nat_here
|| this->nat_there
;
1652 * Implementation of ike_sa_t.enable_natt.
1654 static void enable_natt(private_ike_sa_t
*this, bool local
)
1658 DBG1(DBG_IKE
, "local host is behind NAT, scheduling keep alives");
1659 this->nat_here
= TRUE
;
1660 send_keepalive(this);
1664 DBG1(DBG_IKE
, "remote host is behind NAT");
1665 this->nat_there
= TRUE
;
1670 * Implementation of ike_sa_t.remove_dns_server
1672 static void remove_dns_servers(private_ike_sa_t
*this)
1676 chunk_t contents
, line
, orig_line
, token
;
1677 char string
[INET6_ADDRSTRLEN
];
1679 iterator_t
*iterator
;
1681 if (this->dns_servers
->get_count(this->dns_servers
) == 0)
1683 /* don't touch anything if we have no nameservers installed */
1687 file
= fopen(RESOLV_CONF
, "r");
1688 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
1690 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
1691 RESOLV_CONF
, strerror(errno
));
1695 contents
= chunk_alloca((size_t)stats
.st_size
);
1697 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
1699 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
1705 file
= fopen(RESOLV_CONF
, "w");
1708 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
1709 RESOLV_CONF
, strerror(errno
));
1713 iterator
= this->dns_servers
->create_iterator(this->dns_servers
, TRUE
);
1714 while (fetchline(&contents
, &line
))
1718 if (extract_token(&token
, ' ', &line
) &&
1719 strncasecmp(token
.ptr
, "nameserver", token
.len
) == 0)
1721 if (!extract_token(&token
, ' ', &line
))
1725 iterator
->reset(iterator
);
1726 while (iterator
->iterate(iterator
, (void**)&ip
))
1728 snprintf(string
, sizeof(string
), "%H", ip
);
1729 if (strlen(string
) == token
.len
&&
1730 strncmp(token
.ptr
, string
, token
.len
) == 0)
1732 iterator
->remove(iterator
);
1742 /* write line untouched back to file */
1743 fwrite(orig_line
.ptr
, orig_line
.len
, 1, file
);
1744 fprintf(file
, "\n");
1747 iterator
->destroy(iterator
);
1752 * Implementation of ike_sa_t.add_dns_server
1754 static void add_dns_server(private_ike_sa_t
*this, host_t
*dns
)
1760 DBG1(DBG_IKE
, "installing DNS server %H", dns
);
1762 file
= fopen(RESOLV_CONF
, "a+");
1763 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
1765 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
1766 RESOLV_CONF
, strerror(errno
));
1770 contents
= chunk_alloca(stats
.st_size
);
1772 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
1774 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
1780 file
= fopen(RESOLV_CONF
, "w");
1783 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
1784 RESOLV_CONF
, strerror(errno
));
1788 if (fprintf(file
, "nameserver %H # added by strongSwan, assigned by %D\n",
1789 dns
, this->other_id
) < 0)
1791 DBG1(DBG_IKE
, "unable to write DNS configuration: %s", strerror(errno
));
1795 this->dns_servers
->insert_last(this->dns_servers
, dns
->clone(dns
));
1797 fwrite(contents
.ptr
, contents
.len
, 1, file
);
1803 * Implementation of ike_sa_t.destroy.
1805 static void destroy(private_ike_sa_t
*this)
1807 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
1809 DESTROY_IF(this->crypter_in
);
1810 DESTROY_IF(this->crypter_out
);
1811 DESTROY_IF(this->signer_in
);
1812 DESTROY_IF(this->signer_out
);
1813 DESTROY_IF(this->prf
);
1814 DESTROY_IF(this->child_prf
);
1815 chunk_free(&this->skp_verify
);
1816 chunk_free(&this->skp_build
);
1818 if (this->my_virtual_ip
)
1820 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
1821 this->my_virtual_ip
, this->my_host
);
1822 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
1824 DESTROY_IF(this->other_virtual_ip
);
1826 remove_dns_servers(this);
1827 this->dns_servers
->destroy_offset(this->dns_servers
, offsetof(host_t
, destroy
));
1829 DESTROY_IF(this->my_host
);
1830 DESTROY_IF(this->other_host
);
1831 DESTROY_IF(this->my_id
);
1832 DESTROY_IF(this->other_id
);
1834 DESTROY_IF(this->ike_cfg
);
1835 DESTROY_IF(this->peer_cfg
);
1837 this->ike_sa_id
->destroy(this->ike_sa_id
);
1838 this->task_manager
->destroy(this->task_manager
);
1843 * Described in header.
1845 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1847 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
1848 static u_int32_t unique_id
= 0;
1850 /* Public functions */
1851 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
1852 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
1853 this->public.get_stats
= (void (*)(ike_sa_t
*,u_int32_t
*))get_stats
;
1854 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
1855 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
1856 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
1857 this->public.route
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) route
;
1858 this->public.unroute
= (status_t (*)(ike_sa_t
*,u_int32_t
)) unroute
;
1859 this->public.acquire
= (status_t (*)(ike_sa_t
*,u_int32_t
)) acquire
;
1860 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
1861 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
1862 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
1863 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
1864 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
1865 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
1866 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
1867 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
1868 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
1869 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
1870 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
1871 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
1872 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
1873 this->public.get_other_ca
= (ca_info_t
* (*)(ike_sa_t
*)) get_other_ca
;
1874 this->public.set_other_ca
= (void (*)(ike_sa_t
*,ca_info_t
*)) set_other_ca
;
1875 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
1876 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
1877 this->public.retransmit
= (status_t (*)(ike_sa_t
*, u_int32_t
)) retransmit
;
1878 this->public.delete = (status_t (*)(ike_sa_t
*))delete_
;
1879 this->public.destroy
= (void (*)(ike_sa_t
*))destroy
;
1880 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
1881 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
1882 this->public.get_prf
= (prf_t
* (*)(ike_sa_t
*)) get_prf
;
1883 this->public.get_child_prf
= (prf_t
* (*)(ike_sa_t
*)) get_child_prf
;
1884 this->public.get_skp_verify
= (chunk_t (*)(ike_sa_t
*)) get_skp_verify
;
1885 this->public.get_skp_build
= (chunk_t (*)(ike_sa_t
*)) get_skp_build
;
1886 this->public.derive_keys
= (status_t (*)(ike_sa_t
*,proposal_t
*,chunk_t
,chunk_t
,chunk_t
,bool,prf_t
*,prf_t
*)) derive_keys
;
1887 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
1888 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
1889 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
1890 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
1891 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
1892 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
1893 this->public.enable_natt
= (void (*)(ike_sa_t
*, bool)) enable_natt
;
1894 this->public.is_natt_enabled
= (bool (*)(ike_sa_t
*)) is_natt_enabled
;
1895 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
1896 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
1897 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
1898 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
1899 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
1900 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
1901 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
1902 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
1903 this->public.add_dns_server
= (void (*)(ike_sa_t
*,host_t
*))add_dns_server
;
1905 /* initialize private fields */
1906 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1907 this->child_sas
= linked_list_create();
1908 this->my_host
= host_create_any(AF_INET
);
1909 this->other_host
= host_create_any(AF_INET
);
1910 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
1911 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
1912 this->other_ca
= NULL
;
1913 this->extensions
= 0;
1914 this->crypter_in
= NULL
;
1915 this->crypter_out
= NULL
;
1916 this->signer_in
= NULL
;
1917 this->signer_out
= NULL
;
1919 this->skp_verify
= chunk_empty
;
1920 this->skp_build
= chunk_empty
;
1921 this->child_prf
= NULL
;
1922 this->nat_here
= FALSE
;
1923 this->nat_there
= FALSE
;
1924 this->state
= IKE_CREATED
;
1925 this->time
.inbound
= this->time
.outbound
= time(NULL
);
1926 this->time
.established
= 0;
1927 this->time
.rekey
= 0;
1928 this->time
.delete = 0;
1929 this->ike_cfg
= NULL
;
1930 this->peer_cfg
= NULL
;
1931 this->task_manager
= task_manager_create(&this->public);
1932 this->unique_id
= ++unique_id
;
1933 this->my_virtual_ip
= NULL
;
1934 this->other_virtual_ip
= NULL
;
1935 this->dns_servers
= linked_list_create();
1936 this->keyingtry
= 0;
1938 return &this->public;