2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2008 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31 #include <utils/linked_list.h>
32 #include <utils/lexparser.h>
33 #include <crypto/diffie_hellman.h>
34 #include <crypto/prf_plus.h>
35 #include <crypto/crypters/crypter.h>
36 #include <crypto/hashers/hasher.h>
37 #include <encoding/payloads/sa_payload.h>
38 #include <encoding/payloads/nonce_payload.h>
39 #include <encoding/payloads/ke_payload.h>
40 #include <encoding/payloads/delete_payload.h>
41 #include <encoding/payloads/transform_substructure.h>
42 #include <encoding/payloads/transform_attribute.h>
43 #include <encoding/payloads/ts_payload.h>
44 #include <sa/task_manager.h>
45 #include <sa/tasks/ike_init.h>
46 #include <sa/tasks/ike_natd.h>
47 #include <sa/tasks/ike_mobike.h>
48 #include <sa/tasks/ike_auth.h>
49 #include <sa/tasks/ike_auth_lifetime.h>
50 #include <sa/tasks/ike_config.h>
51 #include <sa/tasks/ike_cert_pre.h>
52 #include <sa/tasks/ike_cert_post.h>
53 #include <sa/tasks/ike_rekey.h>
54 #include <sa/tasks/ike_reauth.h>
55 #include <sa/tasks/ike_delete.h>
56 #include <sa/tasks/ike_dpd.h>
57 #include <sa/tasks/child_create.h>
58 #include <sa/tasks/child_delete.h>
59 #include <sa/tasks/child_rekey.h>
60 #include <processing/jobs/retransmit_job.h>
61 #include <processing/jobs/delete_ike_sa_job.h>
62 #include <processing/jobs/send_dpd_job.h>
63 #include <processing/jobs/send_keepalive_job.h>
64 #include <processing/jobs/rekey_ike_sa_job.h>
67 #include <sa/tasks/ike_me.h>
68 #include <processing/jobs/initiate_mediation_job.h>
72 #define RESOLV_CONF "/etc/resolv.conf"
75 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DESTROYING
,
84 typedef struct private_ike_sa_t private_ike_sa_t
;
87 * Private data of an ike_sa_t object.
89 struct private_ike_sa_t
{
97 * Identifier for the current IKE_SA.
99 ike_sa_id_t
*ike_sa_id
;
102 * unique numerical ID for this IKE_SA.
107 * Current state of the IKE_SA
109 ike_sa_state_t state
;
112 * IKE configuration used to set up this IKE_SA
117 * Peer and authentication information to establish IKE_SA.
119 peer_cfg_t
*peer_cfg
;
122 * associated authentication/authorization info for local peer
124 auth_info_t
*my_auth
;
127 * associated authentication/authorization info for remote peer
129 auth_info_t
*other_auth
;
132 * Selected IKE proposal
134 proposal_t
*proposal
;
137 * Juggles tasks to process messages
139 task_manager_t
*task_manager
;
142 * Address of local host
147 * Address of remote host
153 * Are we mediation server
155 bool is_mediation_server
;
158 * Server reflexive host
160 host_t
*server_reflexive_host
;
169 * Identification used for us
171 identification_t
*my_id
;
174 * Identification used for other
176 identification_t
*other_id
;
179 * EAP Identity exchange in EAP-Identity method
181 identification_t
*eap_identity
;;
184 * set of extensions the peer supports
186 ike_extension_t extensions
;
189 * set of condition flags currently enabled for this IKE_SA
191 ike_condition_t conditions
;
194 * Linked List containing the child sa's of the current IKE_SA.
196 linked_list_t
*child_sas
;
199 * keymat of this IKE_SA
204 * Virtual IP on local host, if any
206 host_t
*my_virtual_ip
;
209 * Virtual IP on remote host, if any
211 host_t
*other_virtual_ip
;
214 * List of DNS servers installed by us
216 linked_list_t
*dns_servers
;
219 * list of peers additional addresses, transmitted via MOBIKE
221 linked_list_t
*additional_addresses
;
224 * previously value of received DESTINATION_IP hash
226 chunk_t nat_detection_dest
;
229 * number pending UPDATE_SA_ADDRESS (MOBIKE)
231 u_int32_t pending_updates
;
234 * NAT keep alive interval
236 u_int32_t keepalive_interval
;
239 * Timestamps for this IKE_SA
241 u_int32_t stats
[STAT_MAX
];
244 * how many times we have retried so far (keyingtries)
249 * are we the initiator of this IKE_SA (rekeying does not affect this flag)
255 * get the time of the latest traffic processed by the kernel
257 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
259 enumerator_t
*enumerator
;
260 child_sa_t
*child_sa
;
265 use_time
= this->stats
[STAT_INBOUND
];
269 use_time
= this->stats
[STAT_OUTBOUND
];
271 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
272 while (enumerator
->enumerate(enumerator
, &child_sa
))
274 use_time
= max(use_time
, child_sa
->get_usetime(child_sa
, inbound
));
276 enumerator
->destroy(enumerator
);
282 * Implementation of ike_sa_t.get_unique_id
284 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
286 return this->unique_id
;
290 * Implementation of ike_sa_t.get_name.
292 static char *get_name(private_ike_sa_t
*this)
296 return this->peer_cfg
->get_name(this->peer_cfg
);
302 * Implementation of ike_sa_t.get_statistic.
304 static u_int32_t
get_statistic(private_ike_sa_t
*this, statistic_t kind
)
308 return this->stats
[kind
];
314 * Implementation of ike_sa_t.get_my_host.
316 static host_t
*get_my_host(private_ike_sa_t
*this)
318 return this->my_host
;
322 * Implementation of ike_sa_t.set_my_host.
324 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
326 DESTROY_IF(this->my_host
);
331 * Implementation of ike_sa_t.get_other_host.
333 static host_t
*get_other_host(private_ike_sa_t
*this)
335 return this->other_host
;
339 * Implementation of ike_sa_t.set_other_host.
341 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
343 DESTROY_IF(this->other_host
);
344 this->other_host
= other
;
348 * Implementation of ike_sa_t.get_peer_cfg
350 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
352 return this->peer_cfg
;
356 * Implementation of ike_sa_t.set_peer_cfg
358 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
360 DESTROY_IF(this->peer_cfg
);
361 peer_cfg
->get_ref(peer_cfg
);
362 this->peer_cfg
= peer_cfg
;
364 if (this->ike_cfg
== NULL
)
366 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
367 this->ike_cfg
->get_ref(this->ike_cfg
);
369 /* apply IDs if they are not already set */
370 if (this->my_id
->contains_wildcards(this->my_id
))
372 DESTROY_IF(this->my_id
);
373 this->my_id
= this->peer_cfg
->get_my_id(this->peer_cfg
);
374 this->my_id
= this->my_id
->clone(this->my_id
);
376 if (this->other_id
->contains_wildcards(this->other_id
))
378 DESTROY_IF(this->other_id
);
379 this->other_id
= this->peer_cfg
->get_other_id(this->peer_cfg
);
380 this->other_id
= this->other_id
->clone(this->other_id
);
385 * Implementation of ike_sa_t.get_my_auth.
387 static auth_info_t
* get_my_auth(private_ike_sa_t
*this)
389 return this->my_auth
;
393 * Implementation of ike_sa_t.get_other_auth.
395 static auth_info_t
* get_other_auth(private_ike_sa_t
*this)
397 return this->other_auth
;
401 * Implementation of ike_sa_t.get_proposal
403 static proposal_t
* get_proposal(private_ike_sa_t
*this)
405 return this->proposal
;
409 * Implementation of ike_sa_t.set_proposal
411 static void set_proposal(private_ike_sa_t
*this, proposal_t
*proposal
)
413 DESTROY_IF(this->proposal
);
414 this->proposal
= proposal
->clone(proposal
);
418 * Implementation of ike_sa_t.send_keepalive
420 static void send_keepalive(private_ike_sa_t
*this)
422 send_keepalive_job_t
*job
;
423 time_t last_out
, now
, diff
;
425 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
426 { /* disable keep alives if we are not NATed anymore */
430 last_out
= get_use_time(this, FALSE
);
433 diff
= now
- last_out
;
435 if (diff
>= this->keepalive_interval
)
440 packet
= packet_create();
441 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
442 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
443 data
.ptr
= malloc(1);
446 packet
->set_data(packet
, data
);
447 DBG1(DBG_IKE
, "sending keep alive");
448 charon
->sender
->send(charon
->sender
, packet
);
451 job
= send_keepalive_job_create(this->ike_sa_id
);
452 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
453 (this->keepalive_interval
- diff
) * 1000);
457 * Implementation of ike_sa_t.get_ike_cfg
459 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
461 return this->ike_cfg
;
465 * Implementation of ike_sa_t.set_ike_cfg
467 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
469 ike_cfg
->get_ref(ike_cfg
);
470 this->ike_cfg
= ike_cfg
;
474 * Implementation of ike_sa_t.is_ike_initiator
476 static bool is_ike_initiator(private_ike_sa_t
*this)
478 return this->ike_initiator
;
482 * Implementation of ike_sa_t.enable_extension.
484 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
486 this->extensions
|= extension
;
490 * Implementation of ike_sa_t.has_extension.
492 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
494 return (this->extensions
& extension
) != FALSE
;
498 * Implementation of ike_sa_t.has_condition.
500 static bool has_condition(private_ike_sa_t
*this, ike_condition_t condition
)
502 return (this->conditions
& condition
) != FALSE
;
506 * Implementation of ike_sa_t.enable_condition.
508 static void set_condition(private_ike_sa_t
*this, ike_condition_t condition
,
511 if (has_condition(this, condition
) != enable
)
515 this->conditions
|= condition
;
519 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
520 this->conditions
|= COND_NAT_ANY
;
521 send_keepalive(this);
524 DBG1(DBG_IKE
, "remote host is behind NAT");
525 this->conditions
|= COND_NAT_ANY
;
528 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
529 this->conditions
|= COND_NAT_ANY
;
537 this->conditions
&= ~condition
;
543 set_condition(this, COND_NAT_ANY
,
544 has_condition(this, COND_NAT_HERE
) ||
545 has_condition(this, COND_NAT_THERE
) ||
546 has_condition(this, COND_NAT_FAKE
));
556 * Implementation of ike_sa_t.send_dpd
558 static status_t
send_dpd(private_ike_sa_t
*this)
563 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
571 if (this->task_manager
->busy(this->task_manager
))
573 /* an exchange is in the air, no need to start a DPD check */
578 /* check if there was any inbound traffic */
580 last_in
= get_use_time(this, TRUE
);
582 diff
= now
- last_in
;
585 /* to long ago, initiate dead peer detection */
587 ike_mobike_t
*mobike
;
589 if (supports_extension(this, EXT_MOBIKE
) &&
590 has_condition(this, COND_NAT_HERE
))
592 /* use mobike enabled DPD to detect NAT mapping changes */
593 mobike
= ike_mobike_create(&this->public, TRUE
);
595 task
= &mobike
->task
;
599 task
= (task_t
*)ike_dpd_create(TRUE
);
602 DBG1(DBG_IKE
, "sending DPD request");
604 this->task_manager
->queue_task(this->task_manager
, task
);
605 this->task_manager
->initiate(this->task_manager
);
608 /* recheck in "interval" seconds */
609 job
= send_dpd_job_create(this->ike_sa_id
);
610 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
611 (delay
- diff
) * 1000);
616 * Implementation of ike_sa_t.get_state.
618 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
624 * Implementation of ike_sa_t.set_state.
626 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
628 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
629 get_name(this), this->unique_id
,
630 ike_sa_state_names
, this->state
,
631 ike_sa_state_names
, state
);
635 case IKE_ESTABLISHED
:
637 if (this->state
== IKE_CONNECTING
)
642 /* calculate rekey, reauth and lifetime */
643 this->stats
[STAT_ESTABLISHED
] = time(NULL
);
645 /* schedule rekeying if we have a time which is smaller than
646 * an already scheduled rekeying */
647 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
);
648 if (t
&& (this->stats
[STAT_REKEY
] == 0 ||
649 (this->stats
[STAT_REKEY
] > t
+ this->stats
[STAT_ESTABLISHED
])))
651 this->stats
[STAT_REKEY
] = t
+ this->stats
[STAT_ESTABLISHED
];
652 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
653 charon
->scheduler
->schedule_job(charon
->scheduler
,
655 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
657 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
);
658 if (t
&& (this->stats
[STAT_REAUTH
] == 0 ||
659 (this->stats
[STAT_REAUTH
] > t
+ this->stats
[STAT_ESTABLISHED
])))
661 this->stats
[STAT_REAUTH
] = t
+ this->stats
[STAT_ESTABLISHED
];
662 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
663 charon
->scheduler
->schedule_job(charon
->scheduler
,
665 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
667 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
668 if (this->stats
[STAT_REKEY
] || this->stats
[STAT_REAUTH
])
670 if (this->stats
[STAT_REAUTH
] == 0)
672 this->stats
[STAT_DELETE
] = this->stats
[STAT_REKEY
];
674 else if (this->stats
[STAT_REKEY
] == 0)
676 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
];
680 this->stats
[STAT_DELETE
] = min(this->stats
[STAT_REKEY
],
681 this->stats
[STAT_REAUTH
]);
683 this->stats
[STAT_DELETE
] += t
;
684 t
= this->stats
[STAT_DELETE
] - this->stats
[STAT_ESTABLISHED
];
685 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
686 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
688 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
691 /* start DPD checks */
698 /* delete may fail if a packet gets lost, so set a timeout */
699 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
700 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
701 HALF_OPEN_IKE_SA_TIMEOUT
);
707 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
712 * Implementation of ike_sa_t.reset
714 static void reset(private_ike_sa_t
*this)
716 /* the responder ID is reset, as peer may choose another one */
717 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
719 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
722 set_state(this, IKE_CREATED
);
724 this->task_manager
->reset(this->task_manager
);
728 * Implementation of ike_sa_t.get_keymat
730 static keymat_t
* get_keymat(private_ike_sa_t
*this)
736 * Implementation of ike_sa_t.set_virtual_ip
738 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
742 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
743 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
744 this->my_host
) == SUCCESS
)
746 if (this->my_virtual_ip
)
748 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
749 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
750 this->my_virtual_ip
);
752 DESTROY_IF(this->my_virtual_ip
);
753 this->my_virtual_ip
= ip
->clone(ip
);
757 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
758 this->my_virtual_ip
= NULL
;
763 DESTROY_IF(this->other_virtual_ip
);
764 this->other_virtual_ip
= ip
->clone(ip
);
769 * Implementation of ike_sa_t.get_virtual_ip
771 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
775 return this->my_virtual_ip
;
779 return this->other_virtual_ip
;
784 * Implementation of ike_sa_t.add_additional_address.
786 static void add_additional_address(private_ike_sa_t
*this, host_t
*host
)
788 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
792 * Implementation of ike_sa_t.create_additional_address_iterator.
794 static iterator_t
* create_additional_address_iterator(private_ike_sa_t
*this)
796 return this->additional_addresses
->create_iterator(
797 this->additional_addresses
, TRUE
);
801 * Implementation of ike_sa_t.has_mapping_changed
803 static bool has_mapping_changed(private_ike_sa_t
*this, chunk_t hash
)
805 if (this->nat_detection_dest
.ptr
== NULL
)
807 this->nat_detection_dest
= chunk_clone(hash
);
810 if (chunk_equals(hash
, this->nat_detection_dest
))
814 free(this->nat_detection_dest
.ptr
);
815 this->nat_detection_dest
= chunk_clone(hash
);
820 * Implementation of ike_sa_t.set_pending_updates.
822 static void set_pending_updates(private_ike_sa_t
*this, u_int32_t updates
)
824 this->pending_updates
= updates
;
828 * Implementation of ike_sa_t.get_pending_updates.
830 static u_int32_t
get_pending_updates(private_ike_sa_t
*this)
832 return this->pending_updates
;
836 * Update hosts, as addresses may change (NAT)
838 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
848 other
= this->other_host
;
851 /* apply hosts on first received message */
852 if (this->my_host
->is_anyaddr(this->my_host
) ||
853 this->other_host
->is_anyaddr(this->other_host
))
855 set_my_host(this, me
->clone(me
));
856 set_other_host(this, other
->clone(other
));
861 /* update our address in any case */
862 if (!me
->equals(me
, this->my_host
))
864 set_my_host(this, me
->clone(me
));
868 if (!other
->equals(other
, this->other_host
))
870 /* update others adress if we are NOT NATed,
871 * and allow port changes if we are NATed */
872 if (!has_condition(this, COND_NAT_HERE
) ||
873 other
->ip_equals(other
, this->other_host
))
875 set_other_host(this, other
->clone(other
));
881 /* update all associated CHILD_SAs, if required */
884 iterator_t
*iterator
;
885 child_sa_t
*child_sa
;
887 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
888 while (iterator
->iterate(iterator
, (void**)&child_sa
))
890 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
891 this->my_virtual_ip
, has_condition(this, COND_NAT_ANY
));
893 iterator
->destroy(iterator
);
898 * Implementation of ike_sa_t.generate
900 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
903 this->stats
[STAT_OUTBOUND
] = time(NULL
);
904 message
->set_ike_sa_id(message
, this->ike_sa_id
);
905 return message
->generate(message
,
906 this->keymat
->get_crypter(this->keymat
, FALSE
),
907 this->keymat
->get_signer(this->keymat
, FALSE
), packet
);
911 * send a notify back to the sender
913 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
919 response
= message_create();
920 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
921 response
->set_request(response
, FALSE
);
922 response
->set_message_id(response
, request
->get_message_id(request
));
923 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
924 if (this->my_host
->is_anyaddr(this->my_host
))
926 this->my_host
->destroy(this->my_host
);
927 this->my_host
= request
->get_destination(request
);
928 this->my_host
= this->my_host
->clone(this->my_host
);
930 if (this->other_host
->is_anyaddr(this->other_host
))
932 this->other_host
->destroy(this->other_host
);
933 this->other_host
= request
->get_source(request
);
934 this->other_host
= this->other_host
->clone(this->other_host
);
936 response
->set_source(response
, this->my_host
->clone(this->my_host
));
937 response
->set_destination(response
, this->other_host
->clone(this->other_host
));
938 if (generate_message(this, response
, &packet
) == SUCCESS
)
940 charon
->sender
->send(charon
->sender
, packet
);
942 response
->destroy(response
);
947 * Implementation of ike_sa_t.act_as_mediation_server.
949 static void act_as_mediation_server(private_ike_sa_t
*this)
951 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
952 this->other_id
, this->ike_sa_id
);
953 this->is_mediation_server
= TRUE
;
957 * Implementation of ike_sa_t.get_server_reflexive_host.
959 static host_t
*get_server_reflexive_host(private_ike_sa_t
*this)
961 return this->server_reflexive_host
;
965 * Implementation of ike_sa_t.set_server_reflexive_host.
967 static void set_server_reflexive_host(private_ike_sa_t
*this, host_t
*host
)
969 DESTROY_IF(this->server_reflexive_host
);
970 this->server_reflexive_host
= host
;
974 * Implementation of ike_sa_t.get_connect_id.
976 static chunk_t
get_connect_id(private_ike_sa_t
*this)
978 return this->connect_id
;
982 * Implementation of ike_sa_t.respond
984 static status_t
respond(private_ike_sa_t
*this, identification_t
*peer_id
,
987 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
988 task
->respond(task
, peer_id
, connect_id
);
989 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
990 return this->task_manager
->initiate(this->task_manager
);
994 * Implementation of ike_sa_t.callback
996 static status_t
callback(private_ike_sa_t
*this, identification_t
*peer_id
)
998 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
999 task
->callback(task
, peer_id
);
1000 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1001 return this->task_manager
->initiate(this->task_manager
);
1005 * Implementation of ike_sa_t.relay
1007 static status_t
relay(private_ike_sa_t
*this, identification_t
*requester
,
1008 chunk_t connect_id
, chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
1010 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1011 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1012 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1013 return this->task_manager
->initiate(this->task_manager
);
1017 * Implementation of ike_sa_t.initiate_mediation
1019 static status_t
initiate_mediation(private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
1021 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1022 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1023 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1024 return this->task_manager
->initiate(this->task_manager
);
1028 * Implementation of ike_sa_t.initiate_mediated
1030 static status_t
initiate_mediated(private_ike_sa_t
*this, host_t
*me
, host_t
*other
,
1033 set_my_host(this, me
->clone(me
));
1034 set_other_host(this, other
->clone(other
));
1035 chunk_free(&this->connect_id
);
1036 this->connect_id
= chunk_clone(connect_id
);
1038 return this->task_manager
->initiate(this->task_manager
);
1043 * Resolve DNS host in configuration
1045 static void resolve_hosts(private_ike_sa_t
*this)
1049 host
= host_create_from_dns(this->ike_cfg
->get_other_addr(this->ike_cfg
),
1053 set_other_host(this, host
);
1056 host
= host_create_from_dns(this->ike_cfg
->get_my_addr(this->ike_cfg
),
1057 this->my_host
->get_family(this->my_host
),
1060 if (host
&& host
->is_anyaddr(host
) &&
1061 !this->other_host
->is_anyaddr(this->other_host
))
1063 host
->destroy(host
);
1064 host
= charon
->kernel_interface
->get_source_addr(
1065 charon
->kernel_interface
, this->other_host
, NULL
);
1068 host
->set_port(host
, IKEV2_UDP_PORT
);
1073 set_my_host(this, host
);
1078 * Initiates a CHILD_SA using the appropriate reqid
1080 static status_t
initiate_with_reqid(private_ike_sa_t
*this, child_cfg_t
*child_cfg
, u_int32_t reqid
)
1084 if (this->state
== IKE_CREATED
)
1086 resolve_hosts(this);
1088 if (this->other_host
->is_anyaddr(this->other_host
)
1090 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1094 child_cfg
->destroy(child_cfg
);
1095 DBG1(DBG_IKE
, "unable to initiate to %%any");
1099 this->ike_initiator
= TRUE
;
1101 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1102 this->task_manager
->queue_task(this->task_manager
, task
);
1103 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1104 this->task_manager
->queue_task(this->task_manager
, task
);
1105 task
= (task_t
*)ike_cert_pre_create(&this->public, TRUE
);
1106 this->task_manager
->queue_task(this->task_manager
, task
);
1107 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1108 this->task_manager
->queue_task(this->task_manager
, task
);
1109 task
= (task_t
*)ike_cert_post_create(&this->public, TRUE
);
1110 this->task_manager
->queue_task(this->task_manager
, task
);
1111 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1112 this->task_manager
->queue_task(this->task_manager
, task
);
1113 task
= (task_t
*)ike_auth_lifetime_create(&this->public, TRUE
);
1114 this->task_manager
->queue_task(this->task_manager
, task
);
1115 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1117 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1118 this->task_manager
->queue_task(this->task_manager
, task
);
1121 task
= (task_t
*)ike_me_create(&this->public, TRUE
);
1122 this->task_manager
->queue_task(this->task_manager
, task
);
1127 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1128 { /* mediation connection is already established, retrigger state change
1129 * to notify bus listeners */
1130 DBG1(DBG_IKE
, "mediation connection is already up");
1131 set_state(this, IKE_ESTABLISHED
);
1132 DESTROY_IF(child_cfg
);
1137 /* normal IKE_SA with CHILD_SA */
1138 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
1139 child_cfg
->destroy(child_cfg
);
1142 child_create_t
*child_create
= (child_create_t
*)task
;
1143 child_create
->use_reqid(child_create
, reqid
);
1145 this->task_manager
->queue_task(this->task_manager
, task
);
1148 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1150 /* mediated connection, initiate mediation process */
1151 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1152 charon
->processor
->queue_job(charon
->processor
, job
);
1158 return this->task_manager
->initiate(this->task_manager
);
1162 * Implementation of ike_sa_t.initiate.
1164 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1166 return initiate_with_reqid(this, child_cfg
, 0);
1170 * Implementation of ike_sa_t.acquire.
1172 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
1174 child_cfg_t
*child_cfg
;
1175 iterator_t
*iterator
;
1176 child_sa_t
*current
, *child_sa
= NULL
;
1178 if (this->state
== IKE_DELETING
)
1180 DBG1(DBG_IKE
, "acquiring CHILD_SA {reqid %d} failed: "
1181 "IKE_SA is deleting", reqid
);
1186 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1187 while (iterator
->iterate(iterator
, (void**)¤t
))
1189 if (current
->get_reqid(current
) == reqid
)
1195 iterator
->destroy(iterator
);
1198 DBG1(DBG_IKE
, "acquiring CHILD_SA {reqid %d} failed: "
1199 "CHILD_SA not found", reqid
);
1203 child_cfg
= child_sa
->get_config(child_sa
);
1204 child_cfg
->get_ref(child_cfg
);
1206 return initiate_with_reqid(this, child_cfg
, reqid
);
1210 * Implementation of ike_sa_t.route.
1212 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1214 child_sa_t
*child_sa
;
1215 iterator_t
*iterator
;
1216 linked_list_t
*my_ts
, *other_ts
;
1220 /* check if not already routed*/
1221 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1222 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1224 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1225 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
1227 iterator
->destroy(iterator
);
1228 DBG1(DBG_IKE
, "routing CHILD_SA failed: already routed");
1232 iterator
->destroy(iterator
);
1234 switch (this->state
)
1238 DBG1(DBG_IKE
, "routing CHILD_SA failed: IKE_SA is %N",
1239 ike_sa_state_names
, this->state
);
1242 case IKE_CONNECTING
:
1243 case IKE_ESTABLISHED
:
1248 resolve_hosts(this);
1250 /* install kernel policies */
1251 child_sa
= child_sa_create(this->my_host
, this->other_host
,
1252 child_cfg
, 0, FALSE
);
1254 if (this->my_virtual_ip
)
1256 me
= this->my_virtual_ip
;
1258 other
= this->other_host
;
1259 if (this->other_virtual_ip
)
1261 other
= this->other_virtual_ip
;
1264 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, me
);
1265 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, other
);
1266 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
,
1267 child_cfg
->get_mode(child_cfg
), PROTO_NONE
);
1268 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1269 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1270 if (status
== SUCCESS
)
1272 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1273 DBG1(DBG_IKE
, "CHILD_SA routed");
1277 DBG1(DBG_IKE
, "routing CHILD_SA failed");
1283 * Implementation of ike_sa_t.unroute.
1285 static status_t
unroute(private_ike_sa_t
*this, u_int32_t reqid
)
1287 iterator_t
*iterator
;
1288 child_sa_t
*child_sa
;
1291 /* find CHILD_SA in ROUTED state */
1292 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1293 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1295 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1296 child_sa
->get_reqid(child_sa
) == reqid
)
1298 iterator
->remove(iterator
);
1299 DBG1(DBG_IKE
, "CHILD_SA unrouted");
1300 child_sa
->destroy(child_sa
);
1305 iterator
->destroy(iterator
);
1309 DBG1(DBG_IKE
, "unrouting CHILD_SA failed: reqid %d not found", reqid
);
1312 /* if we are not established, and we have no more routed childs, remove whole SA */
1313 if (this->state
== IKE_CREATED
&&
1314 this->child_sas
->get_count(this->child_sas
) == 0)
1321 * Implementation of ike_sa_t.process_message.
1323 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
1328 is_request
= message
->get_request(message
);
1330 status
= message
->parse_body(message
,
1331 this->keymat
->get_crypter(this->keymat
, TRUE
),
1332 this->keymat
->get_signer(this->keymat
, TRUE
));
1333 if (status
!= SUCCESS
)
1341 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
1344 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
1348 DBG1(DBG_IKE
, "message parsing failed");
1351 send_notify_response(this, message
, INVALID_SYNTAX
);
1355 DBG1(DBG_IKE
, "message verification failed");
1358 send_notify_response(this, message
, INVALID_SYNTAX
);
1362 DBG1(DBG_IKE
, "integrity check failed");
1366 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1369 send_notify_response(this, message
, INVALID_SYNTAX
);
1375 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1376 exchange_type_names
, message
->get_exchange_type(message
),
1377 message
->get_request(message
) ?
"request" : "response",
1378 message
->get_message_id(message
));
1384 private_ike_sa_t
*new;
1385 iterator_t
*iterator
;
1387 bool has_routed
= FALSE
;
1389 me
= message
->get_destination(message
);
1390 other
= message
->get_source(message
);
1392 /* if this IKE_SA is virgin, we check for a config */
1393 if (this->ike_cfg
== NULL
)
1396 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1398 if (this->ike_cfg
== NULL
)
1400 /* no config found for these hosts, destroy */
1401 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1402 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1403 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
1406 /* add a timeout if peer does not establish it completely */
1407 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
1408 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
1409 HALF_OPEN_IKE_SA_TIMEOUT
);
1411 this->stats
[STAT_INBOUND
] = time(NULL
);
1412 /* check if message is trustworthy, and update host information */
1413 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
1414 message
->get_exchange_type(message
) != IKE_SA_INIT
)
1416 if (!supports_extension(this, EXT_MOBIKE
))
1417 { /* with MOBIKE, we do no implicit updates */
1418 update_hosts(this, me
, other
);
1421 status
= this->task_manager
->process_message(this->task_manager
, message
);
1422 if (status
!= DESTROY_ME
)
1426 /* if IKE_SA gets closed for any reasons, reroute routed children */
1427 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1428 while (iterator
->iterate(iterator
, (void**)&child
))
1430 if (child
->get_state(child
) == CHILD_ROUTED
)
1436 iterator
->destroy(iterator
);
1441 /* move routed children to a new IKE_SA, apply connection info */
1442 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1443 charon
->ike_sa_manager
, TRUE
);
1444 set_peer_cfg(new, this->peer_cfg
);
1445 new->other_host
->destroy(new->other_host
);
1446 new->other_host
= this->other_host
->clone(this->other_host
);
1447 if (!has_condition(this, COND_NAT_THERE
))
1449 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1451 if (this->my_virtual_ip
)
1453 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1455 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1456 while (iterator
->iterate(iterator
, (void**)&child
))
1458 if (child
->get_state(child
) == CHILD_ROUTED
)
1460 route(new, child
->get_config(child
));
1463 iterator
->destroy(iterator
);
1464 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1470 * Implementation of ike_sa_t.get_id.
1472 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1474 return this->ike_sa_id
;
1478 * Implementation of ike_sa_t.get_my_id.
1480 static identification_t
* get_my_id(private_ike_sa_t
*this)
1486 * Implementation of ike_sa_t.set_my_id.
1488 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1490 DESTROY_IF(this->my_id
);
1495 * Implementation of ike_sa_t.get_other_id.
1497 static identification_t
* get_other_id(private_ike_sa_t
*this)
1499 return this->other_id
;
1503 * Implementation of ike_sa_t.set_other_id.
1505 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1507 DESTROY_IF(this->other_id
);
1508 this->other_id
= other
;
1512 * Implementation of ike_sa_t.get_eap_identity.
1514 static identification_t
* get_eap_identity(private_ike_sa_t
*this)
1516 return this->eap_identity
;
1520 * Implementation of ike_sa_t.set_eap_identity.
1522 static void set_eap_identity(private_ike_sa_t
*this, identification_t
*id
)
1524 DESTROY_IF(this->eap_identity
);
1525 this->eap_identity
= id
;
1529 * Implementation of ike_sa_t.add_child_sa.
1531 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1533 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1537 * Implementation of ike_sa_t.get_child_sa.
1539 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1540 u_int32_t spi
, bool inbound
)
1542 iterator_t
*iterator
;
1543 child_sa_t
*current
, *found
= NULL
;
1545 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1546 while (iterator
->iterate(iterator
, (void**)¤t
))
1548 if (current
->get_spi(current
, inbound
) == spi
&&
1549 current
->get_protocol(current
) == protocol
)
1554 iterator
->destroy(iterator
);
1559 * Implementation of ike_sa_t.create_child_sa_iterator.
1561 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1563 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1567 * Implementation of ike_sa_t.rekey_child_sa.
1569 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1571 child_sa_t
*child_sa
;
1572 child_rekey_t
*child_rekey
;
1574 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1577 child_rekey
= child_rekey_create(&this->public, child_sa
);
1578 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1579 return this->task_manager
->initiate(this->task_manager
);
1585 * Implementation of ike_sa_t.delete_child_sa.
1587 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1589 child_sa_t
*child_sa
;
1590 child_delete_t
*child_delete
;
1592 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1595 child_delete
= child_delete_create(&this->public, child_sa
);
1596 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1597 return this->task_manager
->initiate(this->task_manager
);
1603 * Implementation of ike_sa_t.destroy_child_sa.
1605 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1608 iterator_t
*iterator
;
1609 child_sa_t
*child_sa
;
1610 status_t status
= NOT_FOUND
;
1612 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1613 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1615 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1616 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1618 child_sa
->destroy(child_sa
);
1619 iterator
->remove(iterator
);
1624 iterator
->destroy(iterator
);
1629 * Implementation of public_ike_sa_t.delete.
1631 static status_t
delete_(private_ike_sa_t
*this)
1633 ike_delete_t
*ike_delete
;
1635 switch (this->state
)
1637 case IKE_ESTABLISHED
:
1639 ike_delete
= ike_delete_create(&this->public, TRUE
);
1640 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1641 return this->task_manager
->initiate(this->task_manager
);
1643 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1646 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1647 "without notification", ike_sa_state_names
, this->state
);
1654 * Implementation of ike_sa_t.rekey.
1656 static status_t
rekey(private_ike_sa_t
*this)
1658 ike_rekey_t
*ike_rekey
;
1660 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1662 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1663 return this->task_manager
->initiate(this->task_manager
);
1667 * Implementation of ike_sa_t.reauth
1669 static status_t
reauth(private_ike_sa_t
*this)
1673 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1674 * If the peer does not support RFC4478, there is no way to keep the
1676 if (!this->ike_initiator
)
1678 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1679 if (this->other_virtual_ip
!= NULL
||
1680 has_condition(this, COND_EAP_AUTHENTICATED
)
1682 /* if we are mediation server we too cannot reauth the IKE_SA */
1683 || this->is_mediation_server
1687 time_t now
= time(NULL
);
1689 DBG1(DBG_IKE
, "IKE_SA will timeout in %#V",
1690 &now
, &this->stats
[STAT_DELETE
]);
1695 DBG1(DBG_IKE
, "reauthenticating actively");
1698 task
= (task_t
*)ike_reauth_create(&this->public);
1699 this->task_manager
->queue_task(this->task_manager
, task
);
1701 return this->task_manager
->initiate(this->task_manager
);
1705 * Implementation of ike_sa_t.reestablish
1707 static status_t
reestablish(private_ike_sa_t
*this)
1712 iterator_t
*iterator
;
1713 child_sa_t
*child_sa
;
1714 child_cfg_t
*child_cfg
;
1715 bool required
= FALSE
;
1716 status_t status
= FAILED
;
1718 /* check if we have children to keep up at all*/
1719 iterator
= create_child_sa_iterator(this);
1720 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1722 child_cfg
= child_sa
->get_config(child_sa
);
1723 if (this->state
== IKE_DELETING
)
1725 action
= child_cfg
->get_close_action(child_cfg
);
1729 action
= child_cfg
->get_dpd_action(child_cfg
);
1733 case ACTION_RESTART
:
1740 iterator
->destroy(iterator
);
1742 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
1743 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1753 /* check if we are able to reestablish this IKE_SA */
1754 if (!this->ike_initiator
&&
1755 (this->other_virtual_ip
!= NULL
||
1756 has_condition(this, COND_EAP_AUTHENTICATED
)
1758 || this->is_mediation_server
1762 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due asymetric setup");
1766 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
, TRUE
);
1767 new->set_peer_cfg(new, this->peer_cfg
);
1768 host
= this->other_host
;
1769 new->set_other_host(new, host
->clone(host
));
1770 host
= this->my_host
;
1771 new->set_my_host(new, host
->clone(host
));
1772 /* if we already have a virtual IP, we reuse it */
1773 host
= this->my_virtual_ip
;
1776 new->set_virtual_ip(new, TRUE
, host
);
1780 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1782 status
= new->initiate(new, NULL
);
1787 iterator
= create_child_sa_iterator(this);
1788 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1790 child_cfg
= child_sa
->get_config(child_sa
);
1791 if (this->state
== IKE_DELETING
)
1793 action
= child_cfg
->get_close_action(child_cfg
);
1797 action
= child_cfg
->get_dpd_action(child_cfg
);
1801 case ACTION_RESTART
:
1802 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
1803 child_cfg
->get_name(child_cfg
));
1804 child_cfg
->get_ref(child_cfg
);
1805 status
= new->initiate(new, child_cfg
);
1808 status
= new->route(new, child_cfg
);
1813 if (status
== DESTROY_ME
)
1818 iterator
->destroy(iterator
);
1821 if (status
== DESTROY_ME
)
1823 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1828 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1834 * Implementation of ike_sa_t.retransmit.
1836 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
1838 this->stats
[STAT_OUTBOUND
] = time(NULL
);
1839 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1841 /* send a proper signal to brief interested bus listeners */
1842 switch (this->state
)
1844 case IKE_CONNECTING
:
1846 /* retry IKE_SA_INIT if we have multiple keyingtries */
1847 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1849 if (tries
== 0 || tries
> this->keyingtry
)
1851 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
1852 this->keyingtry
+ 1, tries
);
1854 return this->task_manager
->initiate(this->task_manager
);
1856 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
1860 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
1863 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
1875 * Implementation of ike_sa_t.set_auth_lifetime.
1877 static void set_auth_lifetime(private_ike_sa_t
*this, u_int32_t lifetime
)
1879 u_int32_t reduction
= this->peer_cfg
->get_over_time(this->peer_cfg
);
1880 u_int32_t reauth_time
= time(NULL
) + lifetime
- reduction
;
1882 if (lifetime
< reduction
)
1884 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, starting reauthentication",
1886 charon
->processor
->queue_job(charon
->processor
,
1887 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
1889 else if (this->stats
[STAT_REAUTH
] == 0 ||
1890 this->stats
[STAT_REAUTH
] > reauth_time
)
1892 this->stats
[STAT_REAUTH
] = reauth_time
;
1893 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1894 " in %ds", lifetime
, lifetime
- reduction
);
1895 charon
->scheduler
->schedule_job(charon
->scheduler
,
1896 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
1897 (lifetime
- reduction
) * 1000);
1901 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, reauthentication already "
1902 "scheduled in %ds", lifetime
, this->stats
[STAT_REAUTH
] - time(NULL
));
1907 * Implementation of ike_sa_t.roam.
1909 static status_t
roam(private_ike_sa_t
*this, bool address
)
1912 ike_mobike_t
*mobike
;
1914 switch (this->state
)
1922 /* responder just updates the peer about changed address config */
1923 if (!this->ike_sa_id
->is_initiator(this->ike_sa_id
))
1925 if (supports_extension(this, EXT_MOBIKE
) && address
)
1927 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1928 mobike
= ike_mobike_create(&this->public, TRUE
);
1929 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1930 return this->task_manager
->initiate(this->task_manager
);
1935 /* keep existing path if possible */
1936 src
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
1937 this->other_host
, this->my_host
);
1940 if (src
->ip_equals(src
, this->my_host
))
1942 DBG2(DBG_IKE
, "keeping connection path %H - %H",
1943 src
, this->other_host
);
1950 /* update addresses with mobike, if supported ... */
1951 if (supports_extension(this, EXT_MOBIKE
))
1953 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
1954 mobike
= ike_mobike_create(&this->public, TRUE
);
1955 mobike
->roam(mobike
, address
);
1956 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1957 return this->task_manager
->initiate(this->task_manager
);
1959 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
1960 /* ... reauth if not */
1961 return reauth(this);
1965 * Implementation of ike_sa_t.inherit.
1967 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1969 child_sa_t
*child_sa
;
1972 /* apply hosts and ids */
1973 this->my_host
->destroy(this->my_host
);
1974 this->other_host
->destroy(this->other_host
);
1975 this->my_id
->destroy(this->my_id
);
1976 this->other_id
->destroy(this->other_id
);
1977 this->my_host
= other
->my_host
->clone(other
->my_host
);
1978 this->other_host
= other
->other_host
->clone(other
->other_host
);
1979 this->my_id
= other
->my_id
->clone(other
->my_id
);
1980 this->other_id
= other
->other_id
->clone(other
->other_id
);
1981 this->ike_initiator
= other
->ike_initiator
;
1983 /* apply virtual assigned IPs... */
1984 if (other
->my_virtual_ip
)
1986 this->my_virtual_ip
= other
->my_virtual_ip
;
1987 other
->my_virtual_ip
= NULL
;
1989 if (other
->other_virtual_ip
)
1991 this->other_virtual_ip
= other
->other_virtual_ip
;
1992 other
->other_virtual_ip
= NULL
;
1995 /* ... and DNS servers */
1996 while (other
->dns_servers
->remove_last(other
->dns_servers
,
1997 (void**)&ip
) == SUCCESS
)
1999 this->dns_servers
->insert_first(this->dns_servers
, ip
);
2002 /* inherit NAT-T conditions */
2003 this->conditions
= other
->conditions
;
2004 if (this->conditions
& COND_NAT_HERE
)
2006 send_keepalive(this);
2010 if (other
->is_mediation_server
)
2012 act_as_mediation_server(this);
2014 else if (other
->server_reflexive_host
)
2016 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
2017 other
->server_reflexive_host
);
2021 /* adopt all children */
2022 while (other
->child_sas
->remove_last(other
->child_sas
,
2023 (void**)&child_sa
) == SUCCESS
)
2025 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2028 /* move pending tasks to the new IKE_SA */
2029 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2031 /* reauthentication timeout survives a rekeying */
2032 if (other
->stats
[STAT_REAUTH
])
2034 time_t reauth
, delete, now
= time(NULL
);
2036 this->stats
[STAT_REAUTH
] = other
->stats
[STAT_REAUTH
];
2037 reauth
= this->stats
[STAT_REAUTH
] - now
;
2038 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
2039 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
] + delete;
2040 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
2041 "lifetime reduced to %ds", reauth
, delete);
2042 charon
->scheduler
->schedule_job(charon
->scheduler
,
2043 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2045 charon
->scheduler
->schedule_job(charon
->scheduler
,
2046 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2049 /* we have to initate here, there may be new tasks to handle */
2050 return this->task_manager
->initiate(this->task_manager
);
2054 * Implementation of ike_sa_t.remove_dns_server
2056 static void remove_dns_servers(private_ike_sa_t
*this)
2060 chunk_t contents
, line
, orig_line
, token
;
2061 char string
[INET6_ADDRSTRLEN
];
2063 iterator_t
*iterator
;
2065 if (this->dns_servers
->get_count(this->dns_servers
) == 0)
2067 /* don't touch anything if we have no nameservers installed */
2071 file
= fopen(RESOLV_CONF
, "r");
2072 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2074 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2075 RESOLV_CONF
, strerror(errno
));
2079 contents
= chunk_alloca((size_t)stats
.st_size
);
2081 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2083 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2089 file
= fopen(RESOLV_CONF
, "w");
2092 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2093 RESOLV_CONF
, strerror(errno
));
2097 iterator
= this->dns_servers
->create_iterator(this->dns_servers
, TRUE
);
2098 while (fetchline(&contents
, &line
))
2102 if (extract_token(&token
, ' ', &line
) &&
2103 strncasecmp(token
.ptr
, "nameserver", token
.len
) == 0)
2105 if (!extract_token(&token
, ' ', &line
))
2109 iterator
->reset(iterator
);
2110 while (iterator
->iterate(iterator
, (void**)&ip
))
2112 snprintf(string
, sizeof(string
), "%H", ip
);
2113 if (strlen(string
) == token
.len
&&
2114 strncmp(token
.ptr
, string
, token
.len
) == 0)
2116 iterator
->remove(iterator
);
2126 /* write line untouched back to file */
2127 fwrite(orig_line
.ptr
, orig_line
.len
, 1, file
);
2128 fprintf(file
, "\n");
2131 iterator
->destroy(iterator
);
2136 * Implementation of ike_sa_t.add_dns_server
2138 static void add_dns_server(private_ike_sa_t
*this, host_t
*dns
)
2144 DBG1(DBG_IKE
, "installing DNS server %H", dns
);
2146 file
= fopen(RESOLV_CONF
, "a+");
2147 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2149 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2150 RESOLV_CONF
, strerror(errno
));
2154 contents
= chunk_alloca(stats
.st_size
);
2156 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2158 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2164 file
= fopen(RESOLV_CONF
, "w");
2167 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2168 RESOLV_CONF
, strerror(errno
));
2172 if (fprintf(file
, "nameserver %H # added by strongSwan, assigned by %D\n",
2173 dns
, this->other_id
) < 0)
2175 DBG1(DBG_IKE
, "unable to write DNS configuration: %s", strerror(errno
));
2179 this->dns_servers
->insert_last(this->dns_servers
, dns
->clone(dns
));
2181 fwrite(contents
.ptr
, contents
.len
, 1, file
);
2187 * Implementation of ike_sa_t.destroy.
2189 static void destroy(private_ike_sa_t
*this)
2191 set_state(this, IKE_DESTROYING
);
2193 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2195 /* unset SA after here to avoid usage by the listeners */
2196 charon
->bus
->set_sa(charon
->bus
, NULL
);
2198 this->task_manager
->destroy(this->task_manager
);
2199 this->keymat
->destroy(this->keymat
);
2201 if (this->my_virtual_ip
)
2203 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
2204 this->my_virtual_ip
);
2205 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
2207 if (this->other_virtual_ip
)
2209 if (this->peer_cfg
&& this->peer_cfg
->get_pool(this->peer_cfg
))
2211 charon
->attributes
->release_address(charon
->attributes
,
2212 this->peer_cfg
->get_pool(this->peer_cfg
),
2213 this->other_virtual_ip
);
2215 this->other_virtual_ip
->destroy(this->other_virtual_ip
);
2218 remove_dns_servers(this);
2219 this->dns_servers
->destroy_offset(this->dns_servers
,
2220 offsetof(host_t
, destroy
));
2221 this->additional_addresses
->destroy_offset(this->additional_addresses
,
2222 offsetof(host_t
, destroy
));
2224 if (this->is_mediation_server
)
2226 charon
->mediation_manager
->remove(charon
->mediation_manager
, this->ike_sa_id
);
2228 DESTROY_IF(this->server_reflexive_host
);
2229 chunk_free(&this->connect_id
);
2231 free(this->nat_detection_dest
.ptr
);
2233 DESTROY_IF(this->my_host
);
2234 DESTROY_IF(this->other_host
);
2235 DESTROY_IF(this->my_id
);
2236 DESTROY_IF(this->other_id
);
2237 DESTROY_IF(this->eap_identity
);
2239 DESTROY_IF(this->ike_cfg
);
2240 DESTROY_IF(this->peer_cfg
);
2241 DESTROY_IF(this->my_auth
);
2242 DESTROY_IF(this->other_auth
);
2243 DESTROY_IF(this->proposal
);
2245 this->ike_sa_id
->destroy(this->ike_sa_id
);
2250 * Described in header.
2252 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2254 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2255 static u_int32_t unique_id
= 0;
2257 /* Public functions */
2258 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
2259 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2260 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
2261 this->public.get_statistic
= (u_int32_t(*)(ike_sa_t
*, statistic_t kind
))get_statistic
;
2262 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
2263 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
2264 this->public.route
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) route
;
2265 this->public.unroute
= (status_t (*)(ike_sa_t
*,u_int32_t
)) unroute
;
2266 this->public.acquire
= (status_t (*)(ike_sa_t
*,u_int32_t
)) acquire
;
2267 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
2268 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
2269 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
2270 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
2271 this->public.get_my_auth
= (auth_info_t
*(*)(ike_sa_t
*))get_my_auth
;
2272 this->public.get_other_auth
= (auth_info_t
*(*)(ike_sa_t
*))get_other_auth
;
2273 this->public.get_proposal
= (proposal_t
*(*)(ike_sa_t
*))get_proposal
;
2274 this->public.set_proposal
= (void(*)(ike_sa_t
*, proposal_t
*proposal
))set_proposal
;
2275 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
2276 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
2277 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
2278 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
2279 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
2280 this->public.update_hosts
= (void(*)(ike_sa_t
*, host_t
*me
, host_t
*other
))update_hosts
;
2281 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
2282 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2283 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
2284 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2285 this->public.get_eap_identity
= (identification_t
* (*)(ike_sa_t
*)) get_eap_identity
;
2286 this->public.set_eap_identity
= (void (*)(ike_sa_t
*,identification_t
*)) set_eap_identity
;
2287 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
2288 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
2289 this->public.set_condition
= (void (*)(ike_sa_t
*, ike_condition_t
,bool)) set_condition
;
2290 this->public.has_condition
= (bool (*)(ike_sa_t
*,ike_condition_t
)) has_condition
;
2291 this->public.set_pending_updates
= (void(*)(ike_sa_t
*, u_int32_t updates
))set_pending_updates
;
2292 this->public.get_pending_updates
= (u_int32_t(*)(ike_sa_t
*))get_pending_updates
;
2293 this->public.is_ike_initiator
= (bool (*)(ike_sa_t
*))is_ike_initiator
;
2294 this->public.create_additional_address_iterator
= (iterator_t
*(*)(ike_sa_t
*))create_additional_address_iterator
;
2295 this->public.add_additional_address
= (void(*)(ike_sa_t
*, host_t
*host
))add_additional_address
;
2296 this->public.has_mapping_changed
= (bool(*)(ike_sa_t
*, chunk_t hash
))has_mapping_changed
;
2297 this->public.retransmit
= (status_t (*)(ike_sa_t
*, u_int32_t
)) retransmit
;
2298 this->public.delete = (status_t (*)(ike_sa_t
*))delete_
;
2299 this->public.destroy
= (void (*)(ike_sa_t
*))destroy
;
2300 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
2301 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
2302 this->public.get_keymat
= (keymat_t
*(*)(ike_sa_t
*))get_keymat
;
2303 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2304 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2305 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2306 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2307 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2308 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2309 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
2310 this->public.reauth
= (status_t (*)(ike_sa_t
*))reauth
;
2311 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
2312 this->public.set_auth_lifetime
= (void(*)(ike_sa_t
*, u_int32_t lifetime
))set_auth_lifetime
;
2313 this->public.roam
= (status_t(*)(ike_sa_t
*,bool))roam
;
2314 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
2315 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
2316 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
2317 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
2318 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
2319 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
2320 this->public.add_dns_server
= (void (*)(ike_sa_t
*,host_t
*))add_dns_server
;
2322 this->public.act_as_mediation_server
= (void (*)(ike_sa_t
*)) act_as_mediation_server
;
2323 this->public.get_server_reflexive_host
= (host_t
* (*)(ike_sa_t
*)) get_server_reflexive_host
;
2324 this->public.set_server_reflexive_host
= (void (*)(ike_sa_t
*,host_t
*)) set_server_reflexive_host
;
2325 this->public.get_connect_id
= (chunk_t (*)(ike_sa_t
*)) get_connect_id
;
2326 this->public.initiate_mediation
= (status_t (*)(ike_sa_t
*,peer_cfg_t
*)) initiate_mediation
;
2327 this->public.initiate_mediated
= (status_t (*)(ike_sa_t
*,host_t
*,host_t
*,chunk_t
)) initiate_mediated
;
2328 this->public.relay
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
,chunk_t
,linked_list_t
*,bool)) relay
;
2329 this->public.callback
= (status_t (*)(ike_sa_t
*,identification_t
*)) callback
;
2330 this->public.respond
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
)) respond
;
2333 /* initialize private fields */
2334 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2335 this->child_sas
= linked_list_create();
2336 this->my_host
= host_create_from_string("%any", IKEV2_UDP_PORT
);
2337 this->other_host
= host_create_from_string("%any", IKEV2_UDP_PORT
);
2338 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2339 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2340 this->eap_identity
= NULL
;
2341 this->extensions
= 0;
2342 this->conditions
= 0;
2343 this->keymat
= keymat_create(ike_sa_id
->is_initiator(ike_sa_id
));
2344 this->state
= IKE_CREATED
;
2345 this->keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2346 "charon.keep_alive", KEEPALIVE_INTERVAL
);
2347 memset(this->stats
, 0, sizeof(this->stats
));
2348 this->stats
[STAT_INBOUND
] = this->stats
[STAT_OUTBOUND
] = time(NULL
);
2349 this->ike_cfg
= NULL
;
2350 this->peer_cfg
= NULL
;
2351 this->my_auth
= auth_info_create();
2352 this->other_auth
= auth_info_create();
2353 this->proposal
= NULL
;
2354 this->task_manager
= task_manager_create(&this->public);
2355 this->unique_id
= ++unique_id
;
2356 this->my_virtual_ip
= NULL
;
2357 this->other_virtual_ip
= NULL
;
2358 this->dns_servers
= linked_list_create();
2359 this->additional_addresses
= linked_list_create();
2360 this->nat_detection_dest
= chunk_empty
;
2361 this->pending_updates
= 0;
2362 this->keyingtry
= 0;
2363 this->ike_initiator
= FALSE
;
2365 this->is_mediation_server
= FALSE
;
2366 this->server_reflexive_host
= NULL
;
2367 this->connect_id
= chunk_empty
;
2370 return &this->public;