2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2009 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
29 #include <collections/linked_list.h>
30 #include <utils/lexparser.h>
31 #include <processing/jobs/retransmit_job.h>
32 #include <processing/jobs/delete_ike_sa_job.h>
33 #include <processing/jobs/send_dpd_job.h>
34 #include <processing/jobs/send_keepalive_job.h>
35 #include <processing/jobs/rekey_ike_sa_job.h>
36 #include <processing/jobs/retry_initiate_job.h>
37 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
40 #include <sa/ikev2/tasks/ike_me.h>
41 #include <processing/jobs/initiate_mediation_job.h>
44 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DESTROYING
,
54 typedef struct private_ike_sa_t private_ike_sa_t
;
55 typedef struct attribute_entry_t attribute_entry_t
;
58 * Private data of an ike_sa_t object.
60 struct private_ike_sa_t
{
68 * Identifier for the current IKE_SA.
70 ike_sa_id_t
*ike_sa_id
;
73 * IKE version of this SA.
75 ike_version_t version
;
78 * unique numerical ID for this IKE_SA.
83 * Current state of the IKE_SA
88 * IKE configuration used to set up this IKE_SA
93 * Peer and authentication information to establish IKE_SA.
98 * currently used authentication ruleset, local (as auth_cfg_t)
103 * list of completed local authentication rounds
105 linked_list_t
*my_auths
;
108 * list of completed remote authentication rounds
110 linked_list_t
*other_auths
;
113 * currently used authentication constraints, remote (as auth_cfg_t)
115 auth_cfg_t
*other_auth
;
118 * Selected IKE proposal
120 proposal_t
*proposal
;
123 * Juggles tasks to process messages
125 task_manager_t
*task_manager
;
128 * Address of local host
133 * Address of remote host
139 * Are we mediation server
141 bool is_mediation_server
;
144 * Server reflexive host
146 host_t
*server_reflexive_host
;
155 * Identification used for us
157 identification_t
*my_id
;
160 * Identification used for other
162 identification_t
*other_id
;
165 * set of extensions the peer supports
167 ike_extension_t extensions
;
170 * set of condition flags currently enabled for this IKE_SA
172 ike_condition_t conditions
;
175 * Linked List containing the child sa's of the current IKE_SA.
177 linked_list_t
*child_sas
;
180 * keymat of this IKE_SA
185 * Virtual IPs on local host
187 linked_list_t
*my_vips
;
190 * Virtual IPs on remote host
192 linked_list_t
*other_vips
;
195 * List of configuration attributes (attribute_entry_t)
197 linked_list_t
*attributes
;
200 * list of peer's addresses, additional ones transmitted via MOBIKE
202 linked_list_t
*peer_addresses
;
205 * previously value of received DESTINATION_IP hash
207 chunk_t nat_detection_dest
;
210 * number pending UPDATE_SA_ADDRESS (MOBIKE)
212 u_int32_t pending_updates
;
215 * NAT keep alive interval
217 u_int32_t keepalive_interval
;
220 * interval for retries during initiation (e.g. if DNS resolution failed),
221 * 0 to disable (default)
223 u_int32_t retry_initiate_interval
;
226 * TRUE if a retry_initiate_job has been queued
228 bool retry_initiate_queued
;
231 * Timestamps for this IKE_SA
233 u_int32_t stats
[STAT_MAX
];
236 * how many times we have retried so far (keyingtries)
241 * local host address to be used for IKE, set via MIGRATE kernel message
246 * remote host address to be used for IKE, set via MIGRATE kernel message
251 * Flush auth configs once established?
257 * Entry to maintain install configuration attributes during IKE_SA lifetime
259 struct attribute_entry_t
{
260 /** handler used to install this attribute */
261 attribute_handler_t
*handler
;
262 /** attribute type */
263 configuration_attribute_type_t type
;
264 /** attribute data */
269 * get the time of the latest traffic processed by the kernel
271 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
273 enumerator_t
*enumerator
;
274 child_sa_t
*child_sa
;
275 time_t use_time
, current
;
279 use_time
= this->stats
[STAT_INBOUND
];
283 use_time
= this->stats
[STAT_OUTBOUND
];
285 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
286 while (enumerator
->enumerate(enumerator
, &child_sa
))
288 child_sa
->get_usestats(child_sa
, inbound
, ¤t
, NULL
);
289 use_time
= max(use_time
, current
);
291 enumerator
->destroy(enumerator
);
296 METHOD(ike_sa_t
, get_unique_id
, u_int32_t
,
297 private_ike_sa_t
*this)
299 return this->unique_id
;
302 METHOD(ike_sa_t
, get_name
, char*,
303 private_ike_sa_t
*this)
307 return this->peer_cfg
->get_name(this->peer_cfg
);
312 METHOD(ike_sa_t
, get_statistic
, u_int32_t
,
313 private_ike_sa_t
*this, statistic_t kind
)
317 return this->stats
[kind
];
322 METHOD(ike_sa_t
, set_statistic
, void,
323 private_ike_sa_t
*this, statistic_t kind
, u_int32_t value
)
327 this->stats
[kind
] = value
;
331 METHOD(ike_sa_t
, get_my_host
, host_t
*,
332 private_ike_sa_t
*this)
334 return this->my_host
;
337 METHOD(ike_sa_t
, set_my_host
, void,
338 private_ike_sa_t
*this, host_t
*me
)
340 DESTROY_IF(this->my_host
);
344 METHOD(ike_sa_t
, get_other_host
, host_t
*,
345 private_ike_sa_t
*this)
347 return this->other_host
;
350 METHOD(ike_sa_t
, set_other_host
, void,
351 private_ike_sa_t
*this, host_t
*other
)
353 DESTROY_IF(this->other_host
);
354 this->other_host
= other
;
357 METHOD(ike_sa_t
, get_peer_cfg
, peer_cfg_t
*,
358 private_ike_sa_t
*this)
360 return this->peer_cfg
;
363 METHOD(ike_sa_t
, set_peer_cfg
, void,
364 private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
366 peer_cfg
->get_ref(peer_cfg
);
367 DESTROY_IF(this->peer_cfg
);
368 this->peer_cfg
= peer_cfg
;
370 if (this->ike_cfg
== NULL
)
372 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
373 this->ike_cfg
->get_ref(this->ike_cfg
);
377 METHOD(ike_sa_t
, get_auth_cfg
, auth_cfg_t
*,
378 private_ike_sa_t
*this, bool local
)
382 return this->my_auth
;
384 return this->other_auth
;
387 METHOD(ike_sa_t
, add_auth_cfg
, void,
388 private_ike_sa_t
*this, bool local
, auth_cfg_t
*cfg
)
392 this->my_auths
->insert_last(this->my_auths
, cfg
);
396 this->other_auths
->insert_last(this->other_auths
, cfg
);
400 METHOD(ike_sa_t
, create_auth_cfg_enumerator
, enumerator_t
*,
401 private_ike_sa_t
*this, bool local
)
405 return this->my_auths
->create_enumerator(this->my_auths
);
407 return this->other_auths
->create_enumerator(this->other_auths
);
411 * Flush the stored authentication round information
413 static void flush_auth_cfgs(private_ike_sa_t
*this)
417 this->my_auth
->purge(this->my_auth
, FALSE
);
418 this->other_auth
->purge(this->other_auth
, FALSE
);
420 while (this->my_auths
->remove_last(this->my_auths
,
421 (void**)&cfg
) == SUCCESS
)
425 while (this->other_auths
->remove_last(this->other_auths
,
426 (void**)&cfg
) == SUCCESS
)
432 METHOD(ike_sa_t
, get_proposal
, proposal_t
*,
433 private_ike_sa_t
*this)
435 return this->proposal
;
438 METHOD(ike_sa_t
, set_proposal
, void,
439 private_ike_sa_t
*this, proposal_t
*proposal
)
441 DESTROY_IF(this->proposal
);
442 this->proposal
= proposal
->clone(proposal
);
445 METHOD(ike_sa_t
, set_message_id
, void,
446 private_ike_sa_t
*this, bool initiate
, u_int32_t mid
)
450 this->task_manager
->reset(this->task_manager
, mid
, UINT_MAX
);
454 this->task_manager
->reset(this->task_manager
, UINT_MAX
, mid
);
458 METHOD(ike_sa_t
, send_keepalive
, void,
459 private_ike_sa_t
*this)
461 send_keepalive_job_t
*job
;
462 time_t last_out
, now
, diff
;
464 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
465 { /* disable keep alives if we are not NATed anymore */
469 last_out
= get_use_time(this, FALSE
);
470 now
= time_monotonic(NULL
);
472 diff
= now
- last_out
;
474 if (diff
>= this->keepalive_interval
)
479 packet
= packet_create();
480 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
481 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
482 data
.ptr
= malloc(1);
485 packet
->set_data(packet
, data
);
486 DBG1(DBG_IKE
, "sending keep alive to %#H", this->other_host
);
487 charon
->sender
->send_no_marker(charon
->sender
, packet
);
490 job
= send_keepalive_job_create(this->ike_sa_id
);
491 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)job
,
492 this->keepalive_interval
- diff
);
495 METHOD(ike_sa_t
, get_ike_cfg
, ike_cfg_t
*,
496 private_ike_sa_t
*this)
498 return this->ike_cfg
;
501 METHOD(ike_sa_t
, set_ike_cfg
, void,
502 private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
504 ike_cfg
->get_ref(ike_cfg
);
505 this->ike_cfg
= ike_cfg
;
508 METHOD(ike_sa_t
, enable_extension
, void,
509 private_ike_sa_t
*this, ike_extension_t extension
)
511 this->extensions
|= extension
;
514 METHOD(ike_sa_t
, supports_extension
, bool,
515 private_ike_sa_t
*this, ike_extension_t extension
)
517 return (this->extensions
& extension
) != FALSE
;
520 METHOD(ike_sa_t
, has_condition
, bool,
521 private_ike_sa_t
*this, ike_condition_t condition
)
523 return (this->conditions
& condition
) != FALSE
;
526 METHOD(ike_sa_t
, set_condition
, void,
527 private_ike_sa_t
*this, ike_condition_t condition
, bool enable
)
529 if (has_condition(this, condition
) != enable
)
533 this->conditions
|= condition
;
537 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
538 this->conditions
|= COND_NAT_ANY
;
539 send_keepalive(this);
542 DBG1(DBG_IKE
, "remote host is behind NAT");
543 this->conditions
|= COND_NAT_ANY
;
546 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
547 this->conditions
|= COND_NAT_ANY
;
555 this->conditions
&= ~condition
;
561 set_condition(this, COND_NAT_ANY
,
562 has_condition(this, COND_NAT_HERE
) ||
563 has_condition(this, COND_NAT_THERE
) ||
564 has_condition(this, COND_NAT_FAKE
));
573 METHOD(ike_sa_t
, send_dpd
, status_t
,
574 private_ike_sa_t
*this)
578 bool task_queued
= FALSE
;
580 if (this->state
== IKE_PASSIVE
)
582 return INVALID_STATE
;
584 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
585 if (this->task_manager
->busy(this->task_manager
))
587 /* an exchange is in the air, no need to start a DPD check */
592 /* check if there was any inbound traffic */
594 last_in
= get_use_time(this, TRUE
);
595 now
= time_monotonic(NULL
);
596 diff
= now
- last_in
;
597 if (!delay
|| diff
>= delay
)
599 /* too long ago, initiate dead peer detection */
600 DBG1(DBG_IKE
, "sending DPD request");
601 this->task_manager
->queue_dpd(this->task_manager
);
606 /* recheck in "interval" seconds */
609 job
= (job_t
*)send_dpd_job_create(this->ike_sa_id
);
610 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, delay
- diff
);
614 return this->task_manager
->initiate(this->task_manager
);
619 METHOD(ike_sa_t
, get_state
, ike_sa_state_t
,
620 private_ike_sa_t
*this)
625 METHOD(ike_sa_t
, set_state
, void,
626 private_ike_sa_t
*this, ike_sa_state_t state
)
628 bool trigger_dpd
= FALSE
;
630 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
631 get_name(this), this->unique_id
,
632 ike_sa_state_names
, this->state
,
633 ike_sa_state_names
, state
);
637 case IKE_ESTABLISHED
:
639 if (this->state
== IKE_CONNECTING
||
640 this->state
== IKE_PASSIVE
)
645 /* calculate rekey, reauth and lifetime */
646 this->stats
[STAT_ESTABLISHED
] = time_monotonic(NULL
);
648 /* schedule rekeying if we have a time which is smaller than
649 * an already scheduled rekeying */
650 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
, TRUE
);
651 if (t
&& (this->stats
[STAT_REKEY
] == 0 ||
652 (this->stats
[STAT_REKEY
] > t
+ this->stats
[STAT_ESTABLISHED
])))
654 this->stats
[STAT_REKEY
] = t
+ this->stats
[STAT_ESTABLISHED
];
655 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
656 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
657 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
659 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
, TRUE
);
660 if (t
&& (this->stats
[STAT_REAUTH
] == 0 ||
661 (this->stats
[STAT_REAUTH
] > t
+ this->stats
[STAT_ESTABLISHED
])))
663 this->stats
[STAT_REAUTH
] = t
+ this->stats
[STAT_ESTABLISHED
];
664 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
665 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
666 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
668 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
669 if (this->stats
[STAT_REKEY
] || this->stats
[STAT_REAUTH
])
671 if (this->stats
[STAT_REAUTH
] == 0)
673 this->stats
[STAT_DELETE
] = this->stats
[STAT_REKEY
];
675 else if (this->stats
[STAT_REKEY
] == 0)
677 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
];
681 this->stats
[STAT_DELETE
] = min(this->stats
[STAT_REKEY
],
682 this->stats
[STAT_REAUTH
]);
684 this->stats
[STAT_DELETE
] += t
;
685 t
= this->stats
[STAT_DELETE
] - this->stats
[STAT_ESTABLISHED
];
686 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
687 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
688 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
690 trigger_dpd
= this->peer_cfg
->get_dpd(this->peer_cfg
);
697 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
702 if (supports_extension(this, EXT_DPD
))
708 DBG1(DBG_IKE
, "DPD not supported by peer, disabled");
713 METHOD(ike_sa_t
, reset
, void,
714 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 flush_auth_cfgs(this);
726 this->keymat
->destroy(this->keymat
);
727 this->keymat
= keymat_create(this->version
,
728 this->ike_sa_id
->is_initiator(this->ike_sa_id
));
730 this->task_manager
->reset(this->task_manager
, 0, 0);
733 METHOD(ike_sa_t
, get_keymat
, keymat_t
*,
734 private_ike_sa_t
*this)
739 METHOD(ike_sa_t
, add_virtual_ip
, void,
740 private_ike_sa_t
*this, bool local
, host_t
*ip
)
746 if (hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
747 this->my_host
, &iface
))
749 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
750 if (hydra
->kernel_interface
->add_ip(hydra
->kernel_interface
,
751 ip
, -1, iface
) == SUCCESS
)
753 this->my_vips
->insert_last(this->my_vips
, ip
->clone(ip
));
757 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
763 DBG1(DBG_IKE
, "looking up interface for virtual IP %H failed", ip
);
768 this->other_vips
->insert_last(this->other_vips
, ip
->clone(ip
));
773 METHOD(ike_sa_t
, clear_virtual_ips
, void,
774 private_ike_sa_t
*this, bool local
)
776 linked_list_t
*vips
= local ?
this->my_vips
: this->other_vips
;
779 while (vips
->remove_first(vips
, (void**)&vip
) == SUCCESS
)
783 hydra
->kernel_interface
->del_ip(hydra
->kernel_interface
,
790 METHOD(ike_sa_t
, create_virtual_ip_enumerator
, enumerator_t
*,
791 private_ike_sa_t
*this, bool local
)
795 return this->my_vips
->create_enumerator(this->my_vips
);
797 return this->other_vips
->create_enumerator(this->other_vips
);
800 METHOD(ike_sa_t
, add_peer_address
, void,
801 private_ike_sa_t
*this, host_t
*host
)
803 this->peer_addresses
->insert_last(this->peer_addresses
, host
);
806 METHOD(ike_sa_t
, create_peer_address_enumerator
, enumerator_t
*,
807 private_ike_sa_t
*this)
809 if (this->peer_addresses
->get_count(this->peer_addresses
))
811 return this->peer_addresses
->create_enumerator(this->peer_addresses
);
813 /* in case we don't have MOBIKE */
814 return enumerator_create_single(this->other_host
, NULL
);
817 METHOD(ike_sa_t
, clear_peer_addresses
, void,
818 private_ike_sa_t
*this)
820 enumerator_t
*enumerator
;
823 enumerator
= this->peer_addresses
->create_enumerator(this->peer_addresses
);
824 while (enumerator
->enumerate(enumerator
, (void**)&host
))
826 this->peer_addresses
->remove_at(this->peer_addresses
,
830 enumerator
->destroy(enumerator
);
833 METHOD(ike_sa_t
, has_mapping_changed
, bool,
834 private_ike_sa_t
*this, chunk_t hash
)
836 if (this->nat_detection_dest
.ptr
== NULL
)
838 this->nat_detection_dest
= chunk_clone(hash
);
841 if (chunk_equals(hash
, this->nat_detection_dest
))
845 free(this->nat_detection_dest
.ptr
);
846 this->nat_detection_dest
= chunk_clone(hash
);
850 METHOD(ike_sa_t
, set_pending_updates
, void,
851 private_ike_sa_t
*this, u_int32_t updates
)
853 this->pending_updates
= updates
;
856 METHOD(ike_sa_t
, get_pending_updates
, u_int32_t
,
857 private_ike_sa_t
*this)
859 return this->pending_updates
;
862 METHOD(ike_sa_t
, float_ports
, void,
863 private_ike_sa_t
*this)
865 /* do not switch if we have a custom port from MOBIKE/NAT */
866 if (this->my_host
->get_port(this->my_host
) ==
867 charon
->socket
->get_port(charon
->socket
, FALSE
))
869 this->my_host
->set_port(this->my_host
,
870 charon
->socket
->get_port(charon
->socket
, TRUE
));
872 if (this->other_host
->get_port(this->other_host
) == IKEV2_UDP_PORT
)
874 this->other_host
->set_port(this->other_host
, IKEV2_NATT_PORT
);
878 METHOD(ike_sa_t
, update_hosts
, void,
879 private_ike_sa_t
*this, host_t
*me
, host_t
*other
, bool force
)
889 other
= this->other_host
;
892 /* apply hosts on first received message */
893 if (this->my_host
->is_anyaddr(this->my_host
) ||
894 this->other_host
->is_anyaddr(this->other_host
))
896 set_my_host(this, me
->clone(me
));
897 set_other_host(this, other
->clone(other
));
902 /* update our address in any case */
903 if (force
&& !me
->equals(me
, this->my_host
))
905 set_my_host(this, me
->clone(me
));
909 if (!other
->equals(other
, this->other_host
))
911 /* update others address if we are NOT NATed */
912 if ((has_condition(this, COND_NAT_THERE
) &&
913 !has_condition(this, COND_NAT_HERE
)) || force
)
915 set_other_host(this, other
->clone(other
));
921 /* update all associated CHILD_SAs, if required */
924 enumerator_t
*enumerator
;
925 child_sa_t
*child_sa
;
927 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
928 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
930 if (child_sa
->update(child_sa
, this->my_host
,
931 this->other_host
, this->my_vips
,
932 has_condition(this, COND_NAT_ANY
)) == NOT_SUPPORTED
)
934 this->public.rekey_child_sa(&this->public,
935 child_sa
->get_protocol(child_sa
),
936 child_sa
->get_spi(child_sa
, TRUE
));
939 enumerator
->destroy(enumerator
);
944 * Set configured DSCP value on packet
946 static void set_dscp(private_ike_sa_t
*this, packet_t
*packet
)
950 /* prefer IKE config on peer_cfg, as its selection is more accurate
951 * then the initial IKE config */
954 ike_cfg
= this->peer_cfg
->get_ike_cfg(this->peer_cfg
);
958 ike_cfg
= this->ike_cfg
;
962 packet
->set_dscp(packet
, ike_cfg
->get_dscp(ike_cfg
));
966 METHOD(ike_sa_t
, generate_message
, status_t
,
967 private_ike_sa_t
*this, message_t
*message
, packet_t
**packet
)
971 if (message
->is_encoded(message
))
972 { /* already encoded in task, but set DSCP value */
973 *packet
= message
->get_packet(message
);
974 set_dscp(this, *packet
);
977 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
978 message
->set_ike_sa_id(message
, this->ike_sa_id
);
979 charon
->bus
->message(charon
->bus
, message
, FALSE
, TRUE
);
980 status
= message
->generate(message
, this->keymat
, packet
);
981 if (status
== SUCCESS
)
983 set_dscp(this, *packet
);
984 charon
->bus
->message(charon
->bus
, message
, FALSE
, FALSE
);
989 METHOD(ike_sa_t
, set_kmaddress
, void,
990 private_ike_sa_t
*this, host_t
*local
, host_t
*remote
)
992 DESTROY_IF(this->local_host
);
993 DESTROY_IF(this->remote_host
);
994 this->local_host
= local
->clone(local
);
995 this->remote_host
= remote
->clone(remote
);
999 METHOD(ike_sa_t
, act_as_mediation_server
, void,
1000 private_ike_sa_t
*this)
1002 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
1003 this->other_id
, this->ike_sa_id
);
1004 this->is_mediation_server
= TRUE
;
1007 METHOD(ike_sa_t
, get_server_reflexive_host
, host_t
*,
1008 private_ike_sa_t
*this)
1010 return this->server_reflexive_host
;
1013 METHOD(ike_sa_t
, set_server_reflexive_host
, void,
1014 private_ike_sa_t
*this, host_t
*host
)
1016 DESTROY_IF(this->server_reflexive_host
);
1017 this->server_reflexive_host
= host
;
1020 METHOD(ike_sa_t
, get_connect_id
, chunk_t
,
1021 private_ike_sa_t
*this)
1023 return this->connect_id
;
1026 METHOD(ike_sa_t
, respond
, status_t
,
1027 private_ike_sa_t
*this, identification_t
*peer_id
, chunk_t connect_id
)
1029 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1030 task
->respond(task
, peer_id
, connect_id
);
1031 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1032 return this->task_manager
->initiate(this->task_manager
);
1035 METHOD(ike_sa_t
, callback
, status_t
,
1036 private_ike_sa_t
*this, identification_t
*peer_id
)
1038 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1039 task
->callback(task
, peer_id
);
1040 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1041 return this->task_manager
->initiate(this->task_manager
);
1044 METHOD(ike_sa_t
, relay
, status_t
,
1045 private_ike_sa_t
*this, identification_t
*requester
, chunk_t connect_id
,
1046 chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
1048 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1049 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1050 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1051 return this->task_manager
->initiate(this->task_manager
);
1054 METHOD(ike_sa_t
, initiate_mediation
, status_t
,
1055 private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
1057 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1058 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1059 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1060 return this->task_manager
->initiate(this->task_manager
);
1063 METHOD(ike_sa_t
, initiate_mediated
, status_t
,
1064 private_ike_sa_t
*this, host_t
*me
, host_t
*other
, chunk_t connect_id
)
1066 set_my_host(this, me
->clone(me
));
1067 set_other_host(this, other
->clone(other
));
1068 chunk_free(&this->connect_id
);
1069 this->connect_id
= chunk_clone(connect_id
);
1070 return this->task_manager
->initiate(this->task_manager
);
1075 * Resolve DNS host in configuration
1077 static void resolve_hosts(private_ike_sa_t
*this)
1081 if (this->remote_host
)
1083 host
= this->remote_host
->clone(this->remote_host
);
1084 host
->set_port(host
, IKEV2_UDP_PORT
);
1089 u_int16_t other_port
;
1091 other_addr
= this->ike_cfg
->get_other_addr(this->ike_cfg
, NULL
);
1092 other_port
= this->ike_cfg
->get_other_port(this->ike_cfg
);
1093 host
= host_create_from_dns(other_addr
, 0, other_port
);
1097 set_other_host(this, host
);
1100 if (this->local_host
)
1102 host
= this->local_host
->clone(this->local_host
);
1103 host
->set_port(host
, charon
->socket
->get_port(charon
->socket
, FALSE
));
1111 /* use same address family as for other */
1112 if (!this->other_host
->is_anyaddr(this->other_host
))
1114 family
= this->other_host
->get_family(this->other_host
);
1116 my_addr
= this->ike_cfg
->get_my_addr(this->ike_cfg
, NULL
);
1117 my_port
= this->ike_cfg
->get_my_port(this->ike_cfg
);
1118 host
= host_create_from_dns(my_addr
, family
, my_port
);
1120 if (host
&& host
->is_anyaddr(host
) &&
1121 !this->other_host
->is_anyaddr(this->other_host
))
1123 host
->destroy(host
);
1124 host
= hydra
->kernel_interface
->get_source_addr(
1125 hydra
->kernel_interface
, this->other_host
, NULL
);
1128 host
->set_port(host
, this->ike_cfg
->get_my_port(this->ike_cfg
));
1131 { /* fallback to address family specific %any(6), if configured */
1132 host
= host_create_from_dns(my_addr
, 0, my_port
);
1138 set_my_host(this, host
);
1142 METHOD(ike_sa_t
, initiate
, status_t
,
1143 private_ike_sa_t
*this, child_cfg_t
*child_cfg
, u_int32_t reqid
,
1144 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1146 bool defer_initiate
= FALSE
;
1148 if (this->state
== IKE_CREATED
)
1150 if (this->my_host
->is_anyaddr(this->my_host
) ||
1151 this->other_host
->is_anyaddr(this->other_host
))
1153 resolve_hosts(this);
1156 if (this->other_host
->is_anyaddr(this->other_host
)
1158 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1162 char *addr
= this->ike_cfg
->get_other_addr(this->ike_cfg
, NULL
);
1163 bool is_anyaddr
= streq(addr
, "%any") || streq(addr
, "%any6");
1165 if (is_anyaddr
|| !this->retry_initiate_interval
)
1169 DBG1(DBG_IKE
, "unable to initiate to %s", addr
);
1173 DBG1(DBG_IKE
, "unable to resolve %s, initiate aborted",
1176 DESTROY_IF(child_cfg
);
1177 charon
->bus
->alert(charon
->bus
, ALERT_PEER_ADDR_FAILED
);
1180 DBG1(DBG_IKE
, "unable to resolve %s, retrying in %ds",
1181 addr
, this->retry_initiate_interval
);
1182 defer_initiate
= TRUE
;
1185 set_condition(this, COND_ORIGINAL_INITIATOR
, TRUE
);
1186 this->task_manager
->queue_ike(this->task_manager
);
1190 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1192 if (this->state
== IKE_ESTABLISHED
)
1194 /* mediation connection is already established, retrigger state
1195 * change to notify bus listeners */
1196 DBG1(DBG_IKE
, "mediation connection is already up");
1197 set_state(this, IKE_ESTABLISHED
);
1199 DESTROY_IF(child_cfg
);
1205 /* normal IKE_SA with CHILD_SA */
1206 this->task_manager
->queue_child(this->task_manager
, child_cfg
, reqid
,
1209 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1211 /* mediated connection, initiate mediation process */
1212 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1213 lib
->processor
->queue_job(lib
->processor
, job
);
1221 if (!this->retry_initiate_queued
)
1223 job_t
*job
= (job_t
*)retry_initiate_job_create(this->ike_sa_id
);
1224 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)job
,
1225 this->retry_initiate_interval
);
1226 this->retry_initiate_queued
= TRUE
;
1230 this->retry_initiate_queued
= FALSE
;
1231 return this->task_manager
->initiate(this->task_manager
);
1234 METHOD(ike_sa_t
, retry_initiate
, status_t
,
1235 private_ike_sa_t
*this)
1237 if (this->retry_initiate_queued
)
1239 this->retry_initiate_queued
= FALSE
;
1240 return initiate(this, NULL
, 0, NULL
, NULL
);
1245 METHOD(ike_sa_t
, process_message
, status_t
,
1246 private_ike_sa_t
*this, message_t
*message
)
1250 if (this->state
== IKE_PASSIVE
)
1251 { /* do not handle messages in passive state */
1254 if (message
->get_major_version(message
) != this->version
)
1256 DBG1(DBG_IKE
, "ignoring %N IKEv%u exchange on %N SA",
1257 exchange_type_names
, message
->get_exchange_type(message
),
1258 message
->get_major_version(message
),
1259 ike_version_names
, this->version
);
1260 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1261 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1264 status
= this->task_manager
->process_message(this->task_manager
, message
);
1265 if (this->flush_auth_cfg
&& this->state
== IKE_ESTABLISHED
)
1267 /* authentication completed */
1268 this->flush_auth_cfg
= FALSE
;
1269 flush_auth_cfgs(this);
1274 METHOD(ike_sa_t
, get_id
, ike_sa_id_t
*,
1275 private_ike_sa_t
*this)
1277 return this->ike_sa_id
;
1280 METHOD(ike_sa_t
, get_version
, ike_version_t
,
1281 private_ike_sa_t
*this)
1283 return this->version
;
1286 METHOD(ike_sa_t
, get_my_id
, identification_t
*,
1287 private_ike_sa_t
*this)
1292 METHOD(ike_sa_t
, set_my_id
, void,
1293 private_ike_sa_t
*this, identification_t
*me
)
1295 DESTROY_IF(this->my_id
);
1299 METHOD(ike_sa_t
, get_other_id
, identification_t
*,
1300 private_ike_sa_t
*this)
1302 return this->other_id
;
1305 METHOD(ike_sa_t
, get_other_eap_id
, identification_t
*,
1306 private_ike_sa_t
*this)
1308 identification_t
*id
= NULL
, *current
;
1309 enumerator_t
*enumerator
;
1312 enumerator
= this->other_auths
->create_enumerator(this->other_auths
);
1313 while (enumerator
->enumerate(enumerator
, &cfg
))
1315 /* prefer EAP-Identity of last round */
1316 current
= cfg
->get(cfg
, AUTH_RULE_EAP_IDENTITY
);
1317 if (!current
|| current
->get_type(current
) == ID_ANY
)
1319 current
= cfg
->get(cfg
, AUTH_RULE_XAUTH_IDENTITY
);
1321 if (!current
|| current
->get_type(current
) == ID_ANY
)
1323 current
= cfg
->get(cfg
, AUTH_RULE_IDENTITY
);
1325 if (current
&& current
->get_type(current
) != ID_ANY
)
1331 enumerator
->destroy(enumerator
);
1336 return this->other_id
;
1339 METHOD(ike_sa_t
, set_other_id
, void,
1340 private_ike_sa_t
*this, identification_t
*other
)
1342 DESTROY_IF(this->other_id
);
1343 this->other_id
= other
;
1346 METHOD(ike_sa_t
, add_child_sa
, void,
1347 private_ike_sa_t
*this, child_sa_t
*child_sa
)
1349 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1352 METHOD(ike_sa_t
, get_child_sa
, child_sa_t
*,
1353 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
, bool inbound
)
1355 enumerator_t
*enumerator
;
1356 child_sa_t
*current
, *found
= NULL
;
1358 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1359 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
1361 if (current
->get_spi(current
, inbound
) == spi
&&
1362 current
->get_protocol(current
) == protocol
)
1367 enumerator
->destroy(enumerator
);
1371 METHOD(ike_sa_t
, get_child_count
, int,
1372 private_ike_sa_t
*this)
1374 return this->child_sas
->get_count(this->child_sas
);
1377 METHOD(ike_sa_t
, create_child_sa_enumerator
, enumerator_t
*,
1378 private_ike_sa_t
*this)
1380 return this->child_sas
->create_enumerator(this->child_sas
);
1383 METHOD(ike_sa_t
, remove_child_sa
, void,
1384 private_ike_sa_t
*this, enumerator_t
*enumerator
)
1386 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1389 METHOD(ike_sa_t
, rekey_child_sa
, status_t
,
1390 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1392 if (this->state
== IKE_PASSIVE
)
1394 return INVALID_STATE
;
1396 this->task_manager
->queue_child_rekey(this->task_manager
, protocol
, spi
);
1397 return this->task_manager
->initiate(this->task_manager
);
1400 METHOD(ike_sa_t
, delete_child_sa
, status_t
,
1401 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
, bool expired
)
1403 if (this->state
== IKE_PASSIVE
)
1405 return INVALID_STATE
;
1407 this->task_manager
->queue_child_delete(this->task_manager
,
1408 protocol
, spi
, expired
);
1409 return this->task_manager
->initiate(this->task_manager
);
1412 METHOD(ike_sa_t
, destroy_child_sa
, status_t
,
1413 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1415 enumerator_t
*enumerator
;
1416 child_sa_t
*child_sa
;
1417 status_t status
= NOT_FOUND
;
1419 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1420 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1422 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1423 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1425 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1426 child_sa
->destroy(child_sa
);
1431 enumerator
->destroy(enumerator
);
1435 METHOD(ike_sa_t
, delete_
, status_t
,
1436 private_ike_sa_t
*this)
1438 switch (this->state
)
1441 if (this->version
== IKEV1
)
1442 { /* SA has been reauthenticated, delete */
1443 charon
->bus
->ike_updown(charon
->bus
, &this->public, FALSE
);
1447 case IKE_ESTABLISHED
:
1448 this->task_manager
->queue_ike_delete(this->task_manager
);
1449 return this->task_manager
->initiate(this->task_manager
);
1451 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1456 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1457 "without notification", ike_sa_state_names
, this->state
);
1458 charon
->bus
->ike_updown(charon
->bus
, &this->public, FALSE
);
1464 METHOD(ike_sa_t
, rekey
, status_t
,
1465 private_ike_sa_t
*this)
1467 if (this->state
== IKE_PASSIVE
)
1469 return INVALID_STATE
;
1471 this->task_manager
->queue_ike_rekey(this->task_manager
);
1472 return this->task_manager
->initiate(this->task_manager
);
1475 METHOD(ike_sa_t
, reauth
, status_t
,
1476 private_ike_sa_t
*this)
1478 if (this->state
== IKE_PASSIVE
)
1480 return INVALID_STATE
;
1482 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1483 * If the peer does not support RFC4478, there is no way to keep the
1485 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1487 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1488 if (this->other_vips
->get_count(this->other_vips
) != 0 ||
1489 has_condition(this, COND_XAUTH_AUTHENTICATED
) ||
1490 has_condition(this, COND_EAP_AUTHENTICATED
)
1492 /* as mediation server we too cannot reauth the IKE_SA */
1493 || this->is_mediation_server
1499 del
= this->stats
[STAT_DELETE
];
1500 now
= time_monotonic(NULL
);
1501 DBG1(DBG_IKE
, "IKE_SA %s[%d] will timeout in %V",
1502 get_name(this), this->unique_id
, &now
, &del
);
1507 DBG0(DBG_IKE
, "reauthenticating IKE_SA %s[%d] actively",
1508 get_name(this), this->unique_id
);
1513 DBG0(DBG_IKE
, "reauthenticating IKE_SA %s[%d]",
1514 get_name(this), this->unique_id
);
1516 set_condition(this, COND_REAUTHENTICATING
, TRUE
);
1517 this->task_manager
->queue_ike_reauth(this->task_manager
);
1518 return this->task_manager
->initiate(this->task_manager
);
1521 METHOD(ike_sa_t
, reestablish
, status_t
,
1522 private_ike_sa_t
*this)
1527 enumerator_t
*enumerator
;
1528 child_sa_t
*child_sa
;
1529 child_cfg_t
*child_cfg
;
1530 bool restart
= FALSE
;
1531 status_t status
= FAILED
;
1533 if (has_condition(this, COND_REAUTHENTICATING
))
1534 { /* only reauthenticate if we have children */
1535 if (this->child_sas
->get_count(this->child_sas
) == 0
1537 /* allow reauth of mediation connections without CHILD_SAs */
1538 && !this->peer_cfg
->is_mediation(this->peer_cfg
)
1542 DBG1(DBG_IKE
, "unable to reauthenticate IKE_SA, no CHILD_SA "
1551 { /* check if we have children to keep up at all */
1552 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1553 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1555 if (this->state
== IKE_DELETING
)
1557 action
= child_sa
->get_close_action(child_sa
);
1561 action
= child_sa
->get_dpd_action(child_sa
);
1565 case ACTION_RESTART
:
1569 charon
->traps
->install(charon
->traps
, this->peer_cfg
,
1570 child_sa
->get_config(child_sa
));
1576 enumerator
->destroy(enumerator
);
1578 /* mediation connections have no children, keep them up anyway */
1579 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1590 /* check if we are able to reestablish this IKE_SA */
1591 if (!has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1592 (this->other_vips
->get_count(this->other_vips
) != 0 ||
1593 has_condition(this, COND_EAP_AUTHENTICATED
)
1595 || this->is_mediation_server
1599 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due to asymmetric setup");
1603 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1604 this->version
, TRUE
);
1609 new->set_peer_cfg(new, this->peer_cfg
);
1610 host
= this->other_host
;
1611 new->set_other_host(new, host
->clone(host
));
1612 host
= this->my_host
;
1613 new->set_my_host(new, host
->clone(host
));
1614 /* if we already have a virtual IP, we reuse it */
1615 enumerator
= this->my_vips
->create_enumerator(this->my_vips
);
1616 while (enumerator
->enumerate(enumerator
, &host
))
1618 new->add_virtual_ip(new, TRUE
, host
);
1620 enumerator
->destroy(enumerator
);
1623 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1625 status
= new->initiate(new, NULL
, 0, NULL
, NULL
);
1630 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1631 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1633 if (has_condition(this, COND_REAUTHENTICATING
))
1635 switch (child_sa
->get_state(child_sa
))
1638 { /* move routed child directly */
1639 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1640 new->add_child_sa(new, child_sa
);
1641 action
= ACTION_NONE
;
1645 { /* initiate/queue all other CHILD_SAs */
1646 action
= ACTION_RESTART
;
1652 { /* only restart CHILD_SAs that are configured accordingly */
1653 if (this->state
== IKE_DELETING
)
1655 action
= child_sa
->get_close_action(child_sa
);
1659 action
= child_sa
->get_dpd_action(child_sa
);
1664 case ACTION_RESTART
:
1665 child_cfg
= child_sa
->get_config(child_sa
);
1666 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
1667 child_cfg
->get_name(child_cfg
));
1668 child_cfg
->get_ref(child_cfg
);
1669 status
= new->initiate(new, child_cfg
, 0, NULL
, NULL
);
1674 if (status
== DESTROY_ME
)
1679 enumerator
->destroy(enumerator
);
1682 if (status
== DESTROY_ME
)
1684 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1689 charon
->bus
->ike_reestablish(charon
->bus
, &this->public, new);
1690 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1693 charon
->bus
->set_sa(charon
->bus
, &this->public);
1697 METHOD(ike_sa_t
, retransmit
, status_t
,
1698 private_ike_sa_t
*this, u_int32_t message_id
)
1700 if (this->state
== IKE_PASSIVE
)
1702 return INVALID_STATE
;
1704 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
1705 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1707 /* send a proper signal to brief interested bus listeners */
1708 switch (this->state
)
1710 case IKE_CONNECTING
:
1712 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1713 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1714 charon
->bus
->alert(charon
->bus
, ALERT_PEER_INIT_UNREACHABLE
,
1717 if (tries
== 0 || tries
> this->keyingtry
)
1719 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
1720 this->keyingtry
+ 1, tries
);
1722 resolve_hosts(this);
1723 this->task_manager
->queue_ike(this->task_manager
);
1724 return this->task_manager
->initiate(this->task_manager
);
1726 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
1730 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
1731 if (has_condition(this, COND_REAUTHENTICATING
))
1733 DBG1(DBG_IKE
, "delete during reauthentication failed, "
1734 "trying to reestablish IKE_SA anyway");
1739 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
1745 if (this->state
!= IKE_CONNECTING
)
1747 charon
->bus
->ike_updown(charon
->bus
, &this->public, FALSE
);
1754 METHOD(ike_sa_t
, set_auth_lifetime
, status_t
,
1755 private_ike_sa_t
*this, u_int32_t lifetime
)
1757 u_int32_t diff
, hard
, soft
, now
;
1760 diff
= this->peer_cfg
->get_over_time(this->peer_cfg
);
1761 now
= time_monotonic(NULL
);
1762 hard
= now
+ lifetime
;
1765 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1766 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1767 send_update
= this->state
== IKE_ESTABLISHED
&& this->version
== IKEV2
&&
1768 !has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1769 (this->other_vips
->get_count(this->other_vips
) != 0 ||
1770 has_condition(this, COND_EAP_AUTHENTICATED
));
1772 if (lifetime
< diff
)
1774 this->stats
[STAT_REAUTH
] = now
;
1778 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, "
1779 "starting reauthentication", lifetime
);
1780 lib
->processor
->queue_job(lib
->processor
,
1781 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
1784 else if (this->stats
[STAT_REAUTH
] == 0 ||
1785 this->stats
[STAT_REAUTH
] > soft
)
1787 this->stats
[STAT_REAUTH
] = soft
;
1790 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling "
1791 "reauthentication in %ds", lifetime
, lifetime
- diff
);
1792 lib
->scheduler
->schedule_job(lib
->scheduler
,
1793 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
1799 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, "
1800 "reauthentication already scheduled in %ds", lifetime
,
1801 this->stats
[STAT_REAUTH
] - time_monotonic(NULL
));
1802 send_update
= FALSE
;
1804 /* give at least some seconds to reauthenticate */
1805 this->stats
[STAT_DELETE
] = max(hard
, now
+ 10);
1810 ike_auth_lifetime_t
*task
;
1812 task
= ike_auth_lifetime_create(&this->public, TRUE
);
1813 this->task_manager
->queue_task(this->task_manager
, &task
->task
);
1814 return this->task_manager
->initiate(this->task_manager
);
1821 * Check if the current combination of source and destination address is still
1824 static bool is_current_path_valid(private_ike_sa_t
*this)
1828 src
= hydra
->kernel_interface
->get_source_addr(hydra
->kernel_interface
,
1829 this->other_host
, this->my_host
);
1832 if (src
->ip_equals(src
, this->my_host
))
1842 * Check if we have any path avialable for this IKE SA.
1844 static bool is_any_path_valid(private_ike_sa_t
*this)
1847 enumerator_t
*enumerator
;
1848 host_t
*src
= NULL
, *addr
;
1850 DBG1(DBG_IKE
, "old path is not available anymore, try to find another");
1851 enumerator
= create_peer_address_enumerator(this);
1852 while (enumerator
->enumerate(enumerator
, &addr
))
1854 DBG1(DBG_IKE
, "looking for a route to %H ...", addr
);
1855 src
= hydra
->kernel_interface
->get_source_addr(
1856 hydra
->kernel_interface
, addr
, NULL
);
1862 enumerator
->destroy(enumerator
);
1871 METHOD(ike_sa_t
, roam
, status_t
,
1872 private_ike_sa_t
*this, bool address
)
1874 switch (this->state
)
1878 case IKE_DESTROYING
:
1885 /* keep existing path if possible */
1886 if (is_current_path_valid(this))
1888 DBG2(DBG_IKE
, "keeping connection path %H - %H",
1889 this->my_host
, this->other_host
);
1890 set_condition(this, COND_STALE
, FALSE
);
1892 if (supports_extension(this, EXT_MOBIKE
) && address
)
1893 { /* if any addresses changed, send an updated list */
1894 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1895 this->task_manager
->queue_mobike(this->task_manager
, FALSE
, TRUE
);
1896 return this->task_manager
->initiate(this->task_manager
);
1901 if (!is_any_path_valid(this))
1903 DBG1(DBG_IKE
, "no route found to reach %H, MOBIKE update deferred",
1905 set_condition(this, COND_STALE
, TRUE
);
1908 set_condition(this, COND_STALE
, FALSE
);
1910 /* update addresses with mobike, if supported ... */
1911 if (supports_extension(this, EXT_MOBIKE
))
1913 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1914 { /* responder updates the peer about changed address config */
1915 DBG1(DBG_IKE
, "sending address list update using MOBIKE, "
1916 "implicitly requesting an address change");
1921 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
1923 this->task_manager
->queue_mobike(this->task_manager
, TRUE
, address
);
1924 return this->task_manager
->initiate(this->task_manager
);
1927 /* ... reauth if not */
1928 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1929 { /* responder does not reauthenticate */
1930 set_condition(this, COND_STALE
, TRUE
);
1933 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
1934 /* since our previous path is not valid anymore, try and find a new one */
1935 resolve_hosts(this);
1936 return reauth(this);
1939 METHOD(ike_sa_t
, add_configuration_attribute
, void,
1940 private_ike_sa_t
*this, attribute_handler_t
*handler
,
1941 configuration_attribute_type_t type
, chunk_t data
)
1943 attribute_entry_t
*entry
= malloc_thing(attribute_entry_t
);
1945 entry
->handler
= handler
;
1947 entry
->data
= chunk_clone(data
);
1949 this->attributes
->insert_last(this->attributes
, entry
);
1952 METHOD(ike_sa_t
, create_task_enumerator
, enumerator_t
*,
1953 private_ike_sa_t
*this, task_queue_t queue
)
1955 return this->task_manager
->create_task_enumerator(this->task_manager
, queue
);
1958 METHOD(ike_sa_t
, flush_queue
, void,
1959 private_ike_sa_t
*this, task_queue_t queue
)
1961 this->task_manager
->flush_queue(this->task_manager
, queue
);
1964 METHOD(ike_sa_t
, queue_task
, void,
1965 private_ike_sa_t
*this, task_t
*task
)
1967 this->task_manager
->queue_task(this->task_manager
, task
);
1970 METHOD(ike_sa_t
, inherit
, void,
1971 private_ike_sa_t
*this, ike_sa_t
*other_public
)
1973 private_ike_sa_t
*other
= (private_ike_sa_t
*)other_public
;
1974 child_sa_t
*child_sa
;
1975 attribute_entry_t
*entry
;
1976 enumerator_t
*enumerator
;
1980 /* apply hosts and ids */
1981 this->my_host
->destroy(this->my_host
);
1982 this->other_host
->destroy(this->other_host
);
1983 this->my_id
->destroy(this->my_id
);
1984 this->other_id
->destroy(this->other_id
);
1985 this->my_host
= other
->my_host
->clone(other
->my_host
);
1986 this->other_host
= other
->other_host
->clone(other
->other_host
);
1987 this->my_id
= other
->my_id
->clone(other
->my_id
);
1988 this->other_id
= other
->other_id
->clone(other
->other_id
);
1990 /* apply assigned virtual IPs... */
1991 while (other
->my_vips
->remove_last(other
->my_vips
, (void**)&vip
) == SUCCESS
)
1993 this->my_vips
->insert_first(this->my_vips
, vip
);
1995 while (other
->other_vips
->remove_last(other
->other_vips
,
1996 (void**)&vip
) == SUCCESS
)
1998 this->other_vips
->insert_first(this->other_vips
, vip
);
2001 /* authentication information */
2002 enumerator
= other
->my_auths
->create_enumerator(other
->my_auths
);
2003 while (enumerator
->enumerate(enumerator
, &cfg
))
2005 this->my_auths
->insert_last(this->my_auths
, cfg
->clone(cfg
));
2007 enumerator
->destroy(enumerator
);
2008 enumerator
= other
->other_auths
->create_enumerator(other
->other_auths
);
2009 while (enumerator
->enumerate(enumerator
, &cfg
))
2011 this->other_auths
->insert_last(this->other_auths
, cfg
->clone(cfg
));
2013 enumerator
->destroy(enumerator
);
2015 /* ... and configuration attributes */
2016 while (other
->attributes
->remove_last(other
->attributes
,
2017 (void**)&entry
) == SUCCESS
)
2019 this->attributes
->insert_first(this->attributes
, entry
);
2022 /* inherit all conditions */
2023 this->conditions
= other
->conditions
;
2024 if (this->conditions
& COND_NAT_HERE
)
2026 send_keepalive(this);
2030 if (other
->is_mediation_server
)
2032 act_as_mediation_server(this);
2034 else if (other
->server_reflexive_host
)
2036 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
2037 other
->server_reflexive_host
);
2041 /* adopt all children */
2042 while (other
->child_sas
->remove_last(other
->child_sas
,
2043 (void**)&child_sa
) == SUCCESS
)
2045 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2048 /* move pending tasks to the new IKE_SA */
2049 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2051 /* reauthentication timeout survives a rekeying */
2052 if (other
->stats
[STAT_REAUTH
])
2054 time_t reauth
, delete, now
= time_monotonic(NULL
);
2056 this->stats
[STAT_REAUTH
] = other
->stats
[STAT_REAUTH
];
2057 reauth
= this->stats
[STAT_REAUTH
] - now
;
2058 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
2059 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
] + delete;
2060 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
2061 "lifetime reduced to %ds", reauth
, delete);
2062 lib
->scheduler
->schedule_job(lib
->scheduler
,
2063 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
), reauth
);
2064 lib
->scheduler
->schedule_job(lib
->scheduler
,
2065 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
), delete);
2069 METHOD(ike_sa_t
, destroy
, void,
2070 private_ike_sa_t
*this)
2072 attribute_entry_t
*entry
;
2075 charon
->bus
->set_sa(charon
->bus
, &this->public);
2077 set_state(this, IKE_DESTROYING
);
2078 DESTROY_IF(this->task_manager
);
2080 /* remove attributes first, as we pass the IKE_SA to the handler */
2081 while (this->attributes
->remove_last(this->attributes
,
2082 (void**)&entry
) == SUCCESS
)
2084 hydra
->attributes
->release(hydra
->attributes
, entry
->handler
,
2085 this->other_id
, entry
->type
, entry
->data
);
2086 free(entry
->data
.ptr
);
2089 this->attributes
->destroy(this->attributes
);
2091 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2093 /* unset SA after here to avoid usage by the listeners */
2094 charon
->bus
->set_sa(charon
->bus
, NULL
);
2096 DESTROY_IF(this->keymat
);
2098 while (this->my_vips
->remove_last(this->my_vips
, (void**)&vip
) == SUCCESS
)
2100 hydra
->kernel_interface
->del_ip(hydra
->kernel_interface
, vip
, -1, TRUE
);
2103 this->my_vips
->destroy(this->my_vips
);
2104 while (this->other_vips
->remove_last(this->other_vips
,
2105 (void**)&vip
) == SUCCESS
)
2109 linked_list_t
*pools
;
2110 identification_t
*id
;
2112 id
= get_other_eap_id(this);
2113 pools
= linked_list_create_from_enumerator(
2114 this->peer_cfg
->create_pool_enumerator(this->peer_cfg
));
2115 hydra
->attributes
->release_address(hydra
->attributes
, pools
, vip
, id
);
2116 pools
->destroy(pools
);
2120 this->other_vips
->destroy(this->other_vips
);
2121 this->peer_addresses
->destroy_offset(this->peer_addresses
,
2122 offsetof(host_t
, destroy
));
2124 if (this->is_mediation_server
)
2126 charon
->mediation_manager
->remove(charon
->mediation_manager
,
2129 DESTROY_IF(this->server_reflexive_host
);
2130 chunk_free(&this->connect_id
);
2132 free(this->nat_detection_dest
.ptr
);
2134 DESTROY_IF(this->my_host
);
2135 DESTROY_IF(this->other_host
);
2136 DESTROY_IF(this->my_id
);
2137 DESTROY_IF(this->other_id
);
2138 DESTROY_IF(this->local_host
);
2139 DESTROY_IF(this->remote_host
);
2141 DESTROY_IF(this->ike_cfg
);
2142 DESTROY_IF(this->peer_cfg
);
2143 DESTROY_IF(this->proposal
);
2144 this->my_auth
->destroy(this->my_auth
);
2145 this->other_auth
->destroy(this->other_auth
);
2146 this->my_auths
->destroy_offset(this->my_auths
,
2147 offsetof(auth_cfg_t
, destroy
));
2148 this->other_auths
->destroy_offset(this->other_auths
,
2149 offsetof(auth_cfg_t
, destroy
));
2151 this->ike_sa_id
->destroy(this->ike_sa_id
);
2156 * Described in header.
2158 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
, bool initiator
,
2159 ike_version_t version
)
2161 private_ike_sa_t
*this;
2162 static u_int32_t unique_id
= 0;
2164 if (version
== IKE_ANY
)
2165 { /* prefer IKEv2 if protocol not specified */
2175 .get_version
= _get_version
,
2176 .get_state
= _get_state
,
2177 .set_state
= _set_state
,
2178 .get_name
= _get_name
,
2179 .get_statistic
= _get_statistic
,
2180 .set_statistic
= _set_statistic
,
2181 .process_message
= _process_message
,
2182 .initiate
= _initiate
,
2183 .retry_initiate
= _retry_initiate
,
2184 .get_ike_cfg
= _get_ike_cfg
,
2185 .set_ike_cfg
= _set_ike_cfg
,
2186 .get_peer_cfg
= _get_peer_cfg
,
2187 .set_peer_cfg
= _set_peer_cfg
,
2188 .get_auth_cfg
= _get_auth_cfg
,
2189 .create_auth_cfg_enumerator
= _create_auth_cfg_enumerator
,
2190 .add_auth_cfg
= _add_auth_cfg
,
2191 .get_proposal
= _get_proposal
,
2192 .set_proposal
= _set_proposal
,
2194 .get_my_host
= _get_my_host
,
2195 .set_my_host
= _set_my_host
,
2196 .get_other_host
= _get_other_host
,
2197 .set_other_host
= _set_other_host
,
2198 .set_message_id
= _set_message_id
,
2199 .float_ports
= _float_ports
,
2200 .update_hosts
= _update_hosts
,
2201 .get_my_id
= _get_my_id
,
2202 .set_my_id
= _set_my_id
,
2203 .get_other_id
= _get_other_id
,
2204 .set_other_id
= _set_other_id
,
2205 .get_other_eap_id
= _get_other_eap_id
,
2206 .enable_extension
= _enable_extension
,
2207 .supports_extension
= _supports_extension
,
2208 .set_condition
= _set_condition
,
2209 .has_condition
= _has_condition
,
2210 .set_pending_updates
= _set_pending_updates
,
2211 .get_pending_updates
= _get_pending_updates
,
2212 .create_peer_address_enumerator
= _create_peer_address_enumerator
,
2213 .add_peer_address
= _add_peer_address
,
2214 .clear_peer_addresses
= _clear_peer_addresses
,
2215 .has_mapping_changed
= _has_mapping_changed
,
2216 .retransmit
= _retransmit
,
2218 .destroy
= _destroy
,
2219 .send_dpd
= _send_dpd
,
2220 .send_keepalive
= _send_keepalive
,
2221 .get_keymat
= _get_keymat
,
2222 .add_child_sa
= _add_child_sa
,
2223 .get_child_sa
= _get_child_sa
,
2224 .get_child_count
= _get_child_count
,
2225 .create_child_sa_enumerator
= _create_child_sa_enumerator
,
2226 .remove_child_sa
= _remove_child_sa
,
2227 .rekey_child_sa
= _rekey_child_sa
,
2228 .delete_child_sa
= _delete_child_sa
,
2229 .destroy_child_sa
= _destroy_child_sa
,
2232 .reestablish
= _reestablish
,
2233 .set_auth_lifetime
= _set_auth_lifetime
,
2235 .inherit
= _inherit
,
2236 .generate_message
= _generate_message
,
2238 .get_unique_id
= _get_unique_id
,
2239 .add_virtual_ip
= _add_virtual_ip
,
2240 .clear_virtual_ips
= _clear_virtual_ips
,
2241 .create_virtual_ip_enumerator
= _create_virtual_ip_enumerator
,
2242 .add_configuration_attribute
= _add_configuration_attribute
,
2243 .set_kmaddress
= _set_kmaddress
,
2244 .create_task_enumerator
= _create_task_enumerator
,
2245 .flush_queue
= _flush_queue
,
2246 .queue_task
= _queue_task
,
2248 .act_as_mediation_server
= _act_as_mediation_server
,
2249 .get_server_reflexive_host
= _get_server_reflexive_host
,
2250 .set_server_reflexive_host
= _set_server_reflexive_host
,
2251 .get_connect_id
= _get_connect_id
,
2252 .initiate_mediation
= _initiate_mediation
,
2253 .initiate_mediated
= _initiate_mediated
,
2255 .callback
= _callback
,
2256 .respond
= _respond
,
2259 .ike_sa_id
= ike_sa_id
->clone(ike_sa_id
),
2261 .child_sas
= linked_list_create(),
2262 .my_host
= host_create_any(AF_INET
),
2263 .other_host
= host_create_any(AF_INET
),
2264 .my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
2265 .other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
2266 .keymat
= keymat_create(version
, initiator
),
2267 .state
= IKE_CREATED
,
2268 .stats
[STAT_INBOUND
] = time_monotonic(NULL
),
2269 .stats
[STAT_OUTBOUND
] = time_monotonic(NULL
),
2270 .my_auth
= auth_cfg_create(),
2271 .other_auth
= auth_cfg_create(),
2272 .my_auths
= linked_list_create(),
2273 .other_auths
= linked_list_create(),
2274 .unique_id
= ++unique_id
,
2275 .peer_addresses
= linked_list_create(),
2276 .my_vips
= linked_list_create(),
2277 .other_vips
= linked_list_create(),
2278 .attributes
= linked_list_create(),
2279 .keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2280 "%s.keep_alive", KEEPALIVE_INTERVAL
, charon
->name
),
2281 .retry_initiate_interval
= lib
->settings
->get_time(lib
->settings
,
2282 "%s.retry_initiate_interval", 0, charon
->name
),
2283 .flush_auth_cfg
= lib
->settings
->get_bool(lib
->settings
,
2284 "%s.flush_auth_cfg", FALSE
, charon
->name
),
2287 if (version
== IKEV2
)
2288 { /* always supported with IKEv2 */
2289 enable_extension(this, EXT_DPD
);
2292 this->task_manager
= task_manager_create(&this->public);
2293 this->my_host
->set_port(this->my_host
,
2294 charon
->socket
->get_port(charon
->socket
, FALSE
));
2296 if (!this->task_manager
|| !this->keymat
)
2298 DBG1(DBG_IKE
, "IKE version %d not supported", this->version
);
2302 return &this->public;