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 <utils/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?
256 * TRUE if we are currently reauthenticating this IKE_SA
258 bool is_reauthenticating
;
262 * Entry to maintain install configuration attributes during IKE_SA lifetime
264 struct attribute_entry_t
{
265 /** handler used to install this attribute */
266 attribute_handler_t
*handler
;
267 /** attribute type */
268 configuration_attribute_type_t type
;
269 /** attribute data */
274 * get the time of the latest traffic processed by the kernel
276 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
278 enumerator_t
*enumerator
;
279 child_sa_t
*child_sa
;
280 time_t use_time
, current
;
284 use_time
= this->stats
[STAT_INBOUND
];
288 use_time
= this->stats
[STAT_OUTBOUND
];
290 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
291 while (enumerator
->enumerate(enumerator
, &child_sa
))
293 child_sa
->get_usestats(child_sa
, inbound
, ¤t
, NULL
);
294 use_time
= max(use_time
, current
);
296 enumerator
->destroy(enumerator
);
301 METHOD(ike_sa_t
, get_unique_id
, u_int32_t
,
302 private_ike_sa_t
*this)
304 return this->unique_id
;
307 METHOD(ike_sa_t
, get_name
, char*,
308 private_ike_sa_t
*this)
312 return this->peer_cfg
->get_name(this->peer_cfg
);
317 METHOD(ike_sa_t
, get_statistic
, u_int32_t
,
318 private_ike_sa_t
*this, statistic_t kind
)
322 return this->stats
[kind
];
327 METHOD(ike_sa_t
, set_statistic
, void,
328 private_ike_sa_t
*this, statistic_t kind
, u_int32_t value
)
332 this->stats
[kind
] = value
;
336 METHOD(ike_sa_t
, get_my_host
, host_t
*,
337 private_ike_sa_t
*this)
339 return this->my_host
;
342 METHOD(ike_sa_t
, set_my_host
, void,
343 private_ike_sa_t
*this, host_t
*me
)
345 DESTROY_IF(this->my_host
);
349 METHOD(ike_sa_t
, get_other_host
, host_t
*,
350 private_ike_sa_t
*this)
352 return this->other_host
;
355 METHOD(ike_sa_t
, set_other_host
, void,
356 private_ike_sa_t
*this, host_t
*other
)
358 DESTROY_IF(this->other_host
);
359 this->other_host
= other
;
362 METHOD(ike_sa_t
, get_peer_cfg
, peer_cfg_t
*,
363 private_ike_sa_t
*this)
365 return this->peer_cfg
;
368 METHOD(ike_sa_t
, set_peer_cfg
, void,
369 private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
371 peer_cfg
->get_ref(peer_cfg
);
372 DESTROY_IF(this->peer_cfg
);
373 this->peer_cfg
= peer_cfg
;
375 if (this->ike_cfg
== NULL
)
377 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
378 this->ike_cfg
->get_ref(this->ike_cfg
);
382 METHOD(ike_sa_t
, get_auth_cfg
, auth_cfg_t
*,
383 private_ike_sa_t
*this, bool local
)
387 return this->my_auth
;
389 return this->other_auth
;
392 METHOD(ike_sa_t
, add_auth_cfg
, void,
393 private_ike_sa_t
*this, bool local
, auth_cfg_t
*cfg
)
397 this->my_auths
->insert_last(this->my_auths
, cfg
);
401 this->other_auths
->insert_last(this->other_auths
, cfg
);
405 METHOD(ike_sa_t
, create_auth_cfg_enumerator
, enumerator_t
*,
406 private_ike_sa_t
*this, bool local
)
410 return this->my_auths
->create_enumerator(this->my_auths
);
412 return this->other_auths
->create_enumerator(this->other_auths
);
416 * Flush the stored authentication round information
418 static void flush_auth_cfgs(private_ike_sa_t
*this)
422 this->my_auth
->purge(this->my_auth
, FALSE
);
423 this->other_auth
->purge(this->other_auth
, FALSE
);
425 while (this->my_auths
->remove_last(this->my_auths
,
426 (void**)&cfg
) == SUCCESS
)
430 while (this->other_auths
->remove_last(this->other_auths
,
431 (void**)&cfg
) == SUCCESS
)
437 METHOD(ike_sa_t
, get_proposal
, proposal_t
*,
438 private_ike_sa_t
*this)
440 return this->proposal
;
443 METHOD(ike_sa_t
, set_proposal
, void,
444 private_ike_sa_t
*this, proposal_t
*proposal
)
446 DESTROY_IF(this->proposal
);
447 this->proposal
= proposal
->clone(proposal
);
450 METHOD(ike_sa_t
, set_message_id
, void,
451 private_ike_sa_t
*this, bool initiate
, u_int32_t mid
)
455 this->task_manager
->reset(this->task_manager
, mid
, UINT_MAX
);
459 this->task_manager
->reset(this->task_manager
, UINT_MAX
, mid
);
463 METHOD(ike_sa_t
, send_keepalive
, void,
464 private_ike_sa_t
*this)
466 send_keepalive_job_t
*job
;
467 time_t last_out
, now
, diff
;
469 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
470 { /* disable keep alives if we are not NATed anymore */
474 last_out
= get_use_time(this, FALSE
);
475 now
= time_monotonic(NULL
);
477 diff
= now
- last_out
;
479 if (diff
>= this->keepalive_interval
)
484 packet
= packet_create();
485 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
486 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
487 data
.ptr
= malloc(1);
490 packet
->set_data(packet
, data
);
491 DBG1(DBG_IKE
, "sending keep alive to %#H", this->other_host
);
492 charon
->sender
->send_no_marker(charon
->sender
, packet
);
495 job
= send_keepalive_job_create(this->ike_sa_id
);
496 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)job
,
497 this->keepalive_interval
- diff
);
500 METHOD(ike_sa_t
, get_ike_cfg
, ike_cfg_t
*,
501 private_ike_sa_t
*this)
503 return this->ike_cfg
;
506 METHOD(ike_sa_t
, set_ike_cfg
, void,
507 private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
509 ike_cfg
->get_ref(ike_cfg
);
510 this->ike_cfg
= ike_cfg
;
513 METHOD(ike_sa_t
, enable_extension
, void,
514 private_ike_sa_t
*this, ike_extension_t extension
)
516 this->extensions
|= extension
;
519 METHOD(ike_sa_t
, supports_extension
, bool,
520 private_ike_sa_t
*this, ike_extension_t extension
)
522 return (this->extensions
& extension
) != FALSE
;
525 METHOD(ike_sa_t
, has_condition
, bool,
526 private_ike_sa_t
*this, ike_condition_t condition
)
528 return (this->conditions
& condition
) != FALSE
;
531 METHOD(ike_sa_t
, set_condition
, void,
532 private_ike_sa_t
*this, ike_condition_t condition
, bool enable
)
534 if (has_condition(this, condition
) != enable
)
538 this->conditions
|= condition
;
542 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
543 this->conditions
|= COND_NAT_ANY
;
544 send_keepalive(this);
547 DBG1(DBG_IKE
, "remote host is behind NAT");
548 this->conditions
|= COND_NAT_ANY
;
551 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
552 this->conditions
|= COND_NAT_ANY
;
560 this->conditions
&= ~condition
;
566 set_condition(this, COND_NAT_ANY
,
567 has_condition(this, COND_NAT_HERE
) ||
568 has_condition(this, COND_NAT_THERE
) ||
569 has_condition(this, COND_NAT_FAKE
));
578 METHOD(ike_sa_t
, send_dpd
, status_t
,
579 private_ike_sa_t
*this)
584 if (this->state
== IKE_PASSIVE
)
586 return INVALID_STATE
;
588 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
589 if (this->task_manager
->busy(this->task_manager
))
591 /* an exchange is in the air, no need to start a DPD check */
596 /* check if there was any inbound traffic */
598 last_in
= get_use_time(this, TRUE
);
599 now
= time_monotonic(NULL
);
600 diff
= now
- last_in
;
601 if (!delay
|| diff
>= delay
)
603 /* to long ago, initiate dead peer detection */
604 DBG1(DBG_IKE
, "sending DPD request");
605 this->task_manager
->queue_dpd(this->task_manager
);
609 /* recheck in "interval" seconds */
612 job
= (job_t
*)send_dpd_job_create(this->ike_sa_id
);
613 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, delay
- diff
);
615 return this->task_manager
->initiate(this->task_manager
);
618 METHOD(ike_sa_t
, get_state
, ike_sa_state_t
,
619 private_ike_sa_t
*this)
624 METHOD(ike_sa_t
, set_state
, void,
625 private_ike_sa_t
*this, ike_sa_state_t state
)
627 bool trigger_dpd
= FALSE
;
629 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
630 get_name(this), this->unique_id
,
631 ike_sa_state_names
, this->state
,
632 ike_sa_state_names
, state
);
636 case IKE_ESTABLISHED
:
638 if (this->state
== IKE_CONNECTING
||
639 this->state
== IKE_PASSIVE
)
644 /* calculate rekey, reauth and lifetime */
645 this->stats
[STAT_ESTABLISHED
] = time_monotonic(NULL
);
647 /* schedule rekeying if we have a time which is smaller than
648 * an already scheduled rekeying */
649 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
, TRUE
);
650 if (t
&& (this->stats
[STAT_REKEY
] == 0 ||
651 (this->stats
[STAT_REKEY
] > t
+ this->stats
[STAT_ESTABLISHED
])))
653 this->stats
[STAT_REKEY
] = t
+ this->stats
[STAT_ESTABLISHED
];
654 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
655 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
656 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
658 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
, TRUE
);
659 if (t
&& (this->stats
[STAT_REAUTH
] == 0 ||
660 (this->stats
[STAT_REAUTH
] > t
+ this->stats
[STAT_ESTABLISHED
])))
662 this->stats
[STAT_REAUTH
] = t
+ this->stats
[STAT_ESTABLISHED
];
663 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
664 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
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 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
687 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
689 trigger_dpd
= this->peer_cfg
->get_dpd(this->peer_cfg
);
696 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
701 if (supports_extension(this, EXT_DPD
))
707 DBG1(DBG_IKE
, "DPD not supported by peer, disabled");
712 METHOD(ike_sa_t
, reset
, void,
713 private_ike_sa_t
*this)
715 /* the responder ID is reset, as peer may choose another one */
716 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
718 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
721 set_state(this, IKE_CREATED
);
723 flush_auth_cfgs(this);
725 this->keymat
->destroy(this->keymat
);
726 this->keymat
= keymat_create(this->version
,
727 this->ike_sa_id
->is_initiator(this->ike_sa_id
));
729 this->task_manager
->reset(this->task_manager
, 0, 0);
732 METHOD(ike_sa_t
, get_keymat
, keymat_t
*,
733 private_ike_sa_t
*this)
738 METHOD(ike_sa_t
, add_virtual_ip
, void,
739 private_ike_sa_t
*this, bool local
, host_t
*ip
)
743 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
744 if (hydra
->kernel_interface
->add_ip(hydra
->kernel_interface
, ip
,
745 this->my_host
) == SUCCESS
)
747 this->my_vips
->insert_last(this->my_vips
, ip
->clone(ip
));
751 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
756 this->other_vips
->insert_last(this->other_vips
, ip
->clone(ip
));
761 METHOD(ike_sa_t
, clear_virtual_ips
, void,
762 private_ike_sa_t
*this, bool local
)
764 linked_list_t
*vips
= local ?
this->my_vips
: this->other_vips
;
767 while (vips
->remove_first(vips
, (void**)&vip
) == SUCCESS
)
771 hydra
->kernel_interface
->del_ip(hydra
->kernel_interface
, vip
);
777 METHOD(ike_sa_t
, create_virtual_ip_enumerator
, enumerator_t
*,
778 private_ike_sa_t
*this, bool local
)
782 return this->my_vips
->create_enumerator(this->my_vips
);
784 return this->other_vips
->create_enumerator(this->other_vips
);
787 METHOD(ike_sa_t
, add_peer_address
, void,
788 private_ike_sa_t
*this, host_t
*host
)
790 this->peer_addresses
->insert_last(this->peer_addresses
, host
);
793 METHOD(ike_sa_t
, create_peer_address_enumerator
, enumerator_t
*,
794 private_ike_sa_t
*this)
796 if (this->peer_addresses
->get_count(this->peer_addresses
))
798 return this->peer_addresses
->create_enumerator(this->peer_addresses
);
800 /* in case we don't have MOBIKE */
801 return enumerator_create_single(this->other_host
, NULL
);
804 METHOD(ike_sa_t
, clear_peer_addresses
, void,
805 private_ike_sa_t
*this)
807 enumerator_t
*enumerator
;
810 enumerator
= this->peer_addresses
->create_enumerator(this->peer_addresses
);
811 while (enumerator
->enumerate(enumerator
, (void**)&host
))
813 this->peer_addresses
->remove_at(this->peer_addresses
,
817 enumerator
->destroy(enumerator
);
820 METHOD(ike_sa_t
, has_mapping_changed
, bool,
821 private_ike_sa_t
*this, chunk_t hash
)
823 if (this->nat_detection_dest
.ptr
== NULL
)
825 this->nat_detection_dest
= chunk_clone(hash
);
828 if (chunk_equals(hash
, this->nat_detection_dest
))
832 free(this->nat_detection_dest
.ptr
);
833 this->nat_detection_dest
= chunk_clone(hash
);
837 METHOD(ike_sa_t
, set_pending_updates
, void,
838 private_ike_sa_t
*this, u_int32_t updates
)
840 this->pending_updates
= updates
;
843 METHOD(ike_sa_t
, get_pending_updates
, u_int32_t
,
844 private_ike_sa_t
*this)
846 return this->pending_updates
;
849 METHOD(ike_sa_t
, float_ports
, void,
850 private_ike_sa_t
*this)
852 /* do not switch if we have a custom port from MOBIKE/NAT */
853 if (this->my_host
->get_port(this->my_host
) ==
854 charon
->socket
->get_port(charon
->socket
, FALSE
))
856 this->my_host
->set_port(this->my_host
,
857 charon
->socket
->get_port(charon
->socket
, TRUE
));
859 if (this->other_host
->get_port(this->other_host
) == IKEV2_UDP_PORT
)
861 this->other_host
->set_port(this->other_host
, IKEV2_NATT_PORT
);
865 METHOD(ike_sa_t
, update_hosts
, void,
866 private_ike_sa_t
*this, host_t
*me
, host_t
*other
, bool force
)
876 other
= this->other_host
;
879 /* apply hosts on first received message */
880 if (this->my_host
->is_anyaddr(this->my_host
) ||
881 this->other_host
->is_anyaddr(this->other_host
))
883 set_my_host(this, me
->clone(me
));
884 set_other_host(this, other
->clone(other
));
889 /* update our address in any case */
890 if (!me
->equals(me
, this->my_host
))
892 set_my_host(this, me
->clone(me
));
896 if (!other
->equals(other
, this->other_host
))
898 /* update others address if we are NOT NATed */
899 if (force
|| !has_condition(this, COND_NAT_HERE
))
901 set_other_host(this, other
->clone(other
));
907 /* update all associated CHILD_SAs, if required */
910 enumerator_t
*enumerator
;
911 child_sa_t
*child_sa
;
913 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
914 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
916 if (child_sa
->update(child_sa
, this->my_host
,
917 this->other_host
, this->my_vips
,
918 has_condition(this, COND_NAT_ANY
)) == NOT_SUPPORTED
)
920 this->public.rekey_child_sa(&this->public,
921 child_sa
->get_protocol(child_sa
),
922 child_sa
->get_spi(child_sa
, TRUE
));
925 enumerator
->destroy(enumerator
);
929 METHOD(ike_sa_t
, generate_message
, status_t
,
930 private_ike_sa_t
*this, message_t
*message
, packet_t
**packet
)
934 if (message
->is_encoded(message
))
936 *packet
= message
->get_packet(message
);
939 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
940 message
->set_ike_sa_id(message
, this->ike_sa_id
);
941 charon
->bus
->message(charon
->bus
, message
, FALSE
, TRUE
);
942 status
= message
->generate(message
, this->keymat
, packet
);
943 if (status
== SUCCESS
)
945 charon
->bus
->message(charon
->bus
, message
, FALSE
, FALSE
);
950 METHOD(ike_sa_t
, set_kmaddress
, void,
951 private_ike_sa_t
*this, host_t
*local
, host_t
*remote
)
953 DESTROY_IF(this->local_host
);
954 DESTROY_IF(this->remote_host
);
955 this->local_host
= local
->clone(local
);
956 this->remote_host
= remote
->clone(remote
);
960 METHOD(ike_sa_t
, act_as_mediation_server
, void,
961 private_ike_sa_t
*this)
963 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
964 this->other_id
, this->ike_sa_id
);
965 this->is_mediation_server
= TRUE
;
968 METHOD(ike_sa_t
, get_server_reflexive_host
, host_t
*,
969 private_ike_sa_t
*this)
971 return this->server_reflexive_host
;
974 METHOD(ike_sa_t
, set_server_reflexive_host
, void,
975 private_ike_sa_t
*this, host_t
*host
)
977 DESTROY_IF(this->server_reflexive_host
);
978 this->server_reflexive_host
= host
;
981 METHOD(ike_sa_t
, get_connect_id
, chunk_t
,
982 private_ike_sa_t
*this)
984 return this->connect_id
;
987 METHOD(ike_sa_t
, respond
, status_t
,
988 private_ike_sa_t
*this, identification_t
*peer_id
, chunk_t connect_id
)
990 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
991 task
->respond(task
, peer_id
, connect_id
);
992 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
993 return this->task_manager
->initiate(this->task_manager
);
996 METHOD(ike_sa_t
, callback
, status_t
,
997 private_ike_sa_t
*this, identification_t
*peer_id
)
999 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1000 task
->callback(task
, peer_id
);
1001 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1002 return this->task_manager
->initiate(this->task_manager
);
1005 METHOD(ike_sa_t
, relay
, status_t
,
1006 private_ike_sa_t
*this, identification_t
*requester
, chunk_t connect_id
,
1007 chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
1009 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1010 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1011 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1012 return this->task_manager
->initiate(this->task_manager
);
1015 METHOD(ike_sa_t
, initiate_mediation
, status_t
,
1016 private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
1018 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1019 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1020 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1021 return this->task_manager
->initiate(this->task_manager
);
1024 METHOD(ike_sa_t
, initiate_mediated
, status_t
,
1025 private_ike_sa_t
*this, host_t
*me
, host_t
*other
, chunk_t connect_id
)
1027 set_my_host(this, me
->clone(me
));
1028 set_other_host(this, other
->clone(other
));
1029 chunk_free(&this->connect_id
);
1030 this->connect_id
= chunk_clone(connect_id
);
1031 return this->task_manager
->initiate(this->task_manager
);
1036 * Resolve DNS host in configuration
1038 static void resolve_hosts(private_ike_sa_t
*this)
1042 if (this->remote_host
)
1044 host
= this->remote_host
->clone(this->remote_host
);
1045 host
->set_port(host
, IKEV2_UDP_PORT
);
1050 u_int16_t other_port
;
1052 other_addr
= this->ike_cfg
->get_other_addr(this->ike_cfg
, NULL
);
1053 other_port
= this->ike_cfg
->get_other_port(this->ike_cfg
);
1054 host
= host_create_from_dns(other_addr
, 0, other_port
);
1058 set_other_host(this, host
);
1061 if (this->local_host
)
1063 host
= this->local_host
->clone(this->local_host
);
1064 host
->set_port(host
, charon
->socket
->get_port(charon
->socket
, FALSE
));
1072 /* use same address family as for other */
1073 if (!this->other_host
->is_anyaddr(this->other_host
))
1075 family
= this->other_host
->get_family(this->other_host
);
1077 my_addr
= this->ike_cfg
->get_my_addr(this->ike_cfg
, NULL
);
1078 my_port
= this->ike_cfg
->get_my_port(this->ike_cfg
);
1079 host
= host_create_from_dns(my_addr
, family
, my_port
);
1081 if (host
&& host
->is_anyaddr(host
) &&
1082 !this->other_host
->is_anyaddr(this->other_host
))
1084 host
->destroy(host
);
1085 host
= hydra
->kernel_interface
->get_source_addr(
1086 hydra
->kernel_interface
, this->other_host
, NULL
);
1089 host
->set_port(host
, this->ike_cfg
->get_my_port(this->ike_cfg
));
1092 { /* fallback to address family specific %any(6), if configured */
1093 host
= host_create_from_dns(my_addr
, 0, my_port
);
1099 set_my_host(this, host
);
1103 METHOD(ike_sa_t
, initiate
, status_t
,
1104 private_ike_sa_t
*this, child_cfg_t
*child_cfg
, u_int32_t reqid
,
1105 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1107 bool defer_initiate
= FALSE
;
1109 if (this->state
== IKE_CREATED
)
1111 if (this->my_host
->is_anyaddr(this->my_host
) ||
1112 this->other_host
->is_anyaddr(this->other_host
))
1114 resolve_hosts(this);
1117 if (this->other_host
->is_anyaddr(this->other_host
)
1119 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1123 char *addr
= this->ike_cfg
->get_other_addr(this->ike_cfg
, NULL
);
1124 bool is_anyaddr
= streq(addr
, "%any") || streq(addr
, "%any6");
1126 if (is_anyaddr
|| !this->retry_initiate_interval
)
1130 DBG1(DBG_IKE
, "unable to initiate to %s", addr
);
1134 DBG1(DBG_IKE
, "unable to resolve %s, initiate aborted",
1137 DESTROY_IF(child_cfg
);
1138 charon
->bus
->alert(charon
->bus
, ALERT_PEER_ADDR_FAILED
);
1141 DBG1(DBG_IKE
, "unable to resolve %s, retrying in %ds",
1142 addr
, this->retry_initiate_interval
);
1143 defer_initiate
= TRUE
;
1146 set_condition(this, COND_ORIGINAL_INITIATOR
, TRUE
);
1147 this->task_manager
->queue_ike(this->task_manager
);
1151 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1153 if (this->state
== IKE_ESTABLISHED
)
1155 /* mediation connection is already established, retrigger state
1156 * change to notify bus listeners */
1157 DBG1(DBG_IKE
, "mediation connection is already up");
1158 set_state(this, IKE_ESTABLISHED
);
1160 DESTROY_IF(child_cfg
);
1166 /* normal IKE_SA with CHILD_SA */
1167 this->task_manager
->queue_child(this->task_manager
, child_cfg
, reqid
,
1170 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1172 /* mediated connection, initiate mediation process */
1173 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1174 lib
->processor
->queue_job(lib
->processor
, job
);
1182 if (!this->retry_initiate_queued
)
1184 job_t
*job
= (job_t
*)retry_initiate_job_create(this->ike_sa_id
);
1185 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)job
,
1186 this->retry_initiate_interval
);
1187 this->retry_initiate_queued
= TRUE
;
1191 this->retry_initiate_queued
= FALSE
;
1192 return this->task_manager
->initiate(this->task_manager
);
1195 METHOD(ike_sa_t
, retry_initiate
, status_t
,
1196 private_ike_sa_t
*this)
1198 if (this->retry_initiate_queued
)
1200 this->retry_initiate_queued
= FALSE
;
1201 return initiate(this, NULL
, 0, NULL
, NULL
);
1206 METHOD(ike_sa_t
, process_message
, status_t
,
1207 private_ike_sa_t
*this, message_t
*message
)
1211 if (this->state
== IKE_PASSIVE
)
1212 { /* do not handle messages in passive state */
1215 switch (message
->get_exchange_type(message
))
1221 if (this->state
!= IKE_CREATED
&&
1222 this->state
!= IKE_CONNECTING
)
1224 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1225 exchange_type_names
, message
->get_exchange_type(message
));
1232 if (message
->get_major_version(message
) != this->version
)
1234 DBG1(DBG_IKE
, "ignoring %N IKEv%u exchange on %N SA",
1235 exchange_type_names
, message
->get_exchange_type(message
),
1236 message
->get_major_version(message
),
1237 ike_version_names
, this->version
);
1238 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1239 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1242 status
= this->task_manager
->process_message(this->task_manager
, message
);
1243 if (this->flush_auth_cfg
&& this->state
== IKE_ESTABLISHED
)
1245 /* authentication completed */
1246 this->flush_auth_cfg
= FALSE
;
1247 flush_auth_cfgs(this);
1252 METHOD(ike_sa_t
, get_id
, ike_sa_id_t
*,
1253 private_ike_sa_t
*this)
1255 return this->ike_sa_id
;
1258 METHOD(ike_sa_t
, get_version
, ike_version_t
,
1259 private_ike_sa_t
*this)
1261 return this->version
;
1264 METHOD(ike_sa_t
, get_my_id
, identification_t
*,
1265 private_ike_sa_t
*this)
1270 METHOD(ike_sa_t
, set_my_id
, void,
1271 private_ike_sa_t
*this, identification_t
*me
)
1273 DESTROY_IF(this->my_id
);
1277 METHOD(ike_sa_t
, get_other_id
, identification_t
*,
1278 private_ike_sa_t
*this)
1280 return this->other_id
;
1283 METHOD(ike_sa_t
, get_other_eap_id
, identification_t
*,
1284 private_ike_sa_t
*this)
1286 identification_t
*id
= NULL
, *current
;
1287 enumerator_t
*enumerator
;
1290 enumerator
= this->other_auths
->create_enumerator(this->other_auths
);
1291 while (enumerator
->enumerate(enumerator
, &cfg
))
1293 /* prefer EAP-Identity of last round */
1294 current
= cfg
->get(cfg
, AUTH_RULE_EAP_IDENTITY
);
1295 if (!current
|| current
->get_type(current
) == ID_ANY
)
1297 current
= cfg
->get(cfg
, AUTH_RULE_XAUTH_IDENTITY
);
1299 if (!current
|| current
->get_type(current
) == ID_ANY
)
1301 current
= cfg
->get(cfg
, AUTH_RULE_IDENTITY
);
1303 if (current
&& current
->get_type(current
) != ID_ANY
)
1309 enumerator
->destroy(enumerator
);
1314 return this->other_id
;
1317 METHOD(ike_sa_t
, set_other_id
, void,
1318 private_ike_sa_t
*this, identification_t
*other
)
1320 DESTROY_IF(this->other_id
);
1321 this->other_id
= other
;
1324 METHOD(ike_sa_t
, add_child_sa
, void,
1325 private_ike_sa_t
*this, child_sa_t
*child_sa
)
1327 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1330 METHOD(ike_sa_t
, get_child_sa
, child_sa_t
*,
1331 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
, bool inbound
)
1333 enumerator_t
*enumerator
;
1334 child_sa_t
*current
, *found
= NULL
;
1336 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1337 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
1339 if (current
->get_spi(current
, inbound
) == spi
&&
1340 current
->get_protocol(current
) == protocol
)
1345 enumerator
->destroy(enumerator
);
1349 METHOD(ike_sa_t
, get_child_count
, int,
1350 private_ike_sa_t
*this)
1352 return this->child_sas
->get_count(this->child_sas
);
1355 METHOD(ike_sa_t
, create_child_sa_enumerator
, enumerator_t
*,
1356 private_ike_sa_t
*this)
1358 return this->child_sas
->create_enumerator(this->child_sas
);
1361 METHOD(ike_sa_t
, remove_child_sa
, void,
1362 private_ike_sa_t
*this, enumerator_t
*enumerator
)
1364 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1367 METHOD(ike_sa_t
, rekey_child_sa
, status_t
,
1368 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1370 if (this->state
== IKE_PASSIVE
)
1372 return INVALID_STATE
;
1374 this->task_manager
->queue_child_rekey(this->task_manager
, protocol
, spi
);
1375 return this->task_manager
->initiate(this->task_manager
);
1378 METHOD(ike_sa_t
, delete_child_sa
, status_t
,
1379 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
, bool expired
)
1381 if (this->state
== IKE_PASSIVE
)
1383 return INVALID_STATE
;
1385 this->task_manager
->queue_child_delete(this->task_manager
,
1386 protocol
, spi
, expired
);
1387 return this->task_manager
->initiate(this->task_manager
);
1390 METHOD(ike_sa_t
, destroy_child_sa
, status_t
,
1391 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1393 enumerator_t
*enumerator
;
1394 child_sa_t
*child_sa
;
1395 status_t status
= NOT_FOUND
;
1397 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1398 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1400 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1401 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1403 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1404 child_sa
->destroy(child_sa
);
1409 enumerator
->destroy(enumerator
);
1413 METHOD(ike_sa_t
, delete_
, status_t
,
1414 private_ike_sa_t
*this)
1416 switch (this->state
)
1419 if (this->version
== IKEV1
)
1420 { /* SA has been reauthenticated, delete */
1421 charon
->bus
->ike_updown(charon
->bus
, &this->public, FALSE
);
1425 case IKE_ESTABLISHED
:
1426 this->task_manager
->queue_ike_delete(this->task_manager
);
1427 return this->task_manager
->initiate(this->task_manager
);
1429 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1434 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1435 "without notification", ike_sa_state_names
, this->state
);
1436 charon
->bus
->ike_updown(charon
->bus
, &this->public, FALSE
);
1442 METHOD(ike_sa_t
, rekey
, status_t
,
1443 private_ike_sa_t
*this)
1445 if (this->state
== IKE_PASSIVE
)
1447 return INVALID_STATE
;
1449 this->task_manager
->queue_ike_rekey(this->task_manager
);
1450 return this->task_manager
->initiate(this->task_manager
);
1453 METHOD(ike_sa_t
, reauth
, status_t
,
1454 private_ike_sa_t
*this)
1456 if (this->state
== IKE_PASSIVE
)
1458 return INVALID_STATE
;
1460 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1461 * If the peer does not support RFC4478, there is no way to keep the
1463 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1465 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1466 if (this->other_vips
->get_count(this->other_vips
) != 0 ||
1467 has_condition(this, COND_XAUTH_AUTHENTICATED
) ||
1468 has_condition(this, COND_EAP_AUTHENTICATED
)
1470 /* as mediation server we too cannot reauth the IKE_SA */
1471 || this->is_mediation_server
1477 del
= this->stats
[STAT_DELETE
];
1478 now
= time_monotonic(NULL
);
1479 DBG1(DBG_IKE
, "IKE_SA %s[%d] will timeout in %V",
1480 get_name(this), this->unique_id
, &now
, &del
);
1485 DBG0(DBG_IKE
, "reauthenticating IKE_SA %s[%d] actively",
1486 get_name(this), this->unique_id
);
1491 DBG0(DBG_IKE
, "reauthenticating IKE_SA %s[%d]",
1492 get_name(this), this->unique_id
);
1494 this->is_reauthenticating
= TRUE
;
1495 this->task_manager
->queue_ike_reauth(this->task_manager
);
1496 return this->task_manager
->initiate(this->task_manager
);
1499 METHOD(ike_sa_t
, reestablish
, status_t
,
1500 private_ike_sa_t
*this)
1505 enumerator_t
*enumerator
;
1506 child_sa_t
*child_sa
;
1507 child_cfg_t
*child_cfg
;
1508 bool restart
= FALSE
;
1509 status_t status
= FAILED
;
1511 if (this->is_reauthenticating
)
1512 { /* only reauthenticate if we have children */
1513 if (this->child_sas
->get_count(this->child_sas
) == 0
1515 /* allow reauth of mediation connections without CHILD_SAs */
1516 && !this->peer_cfg
->is_mediation(this->peer_cfg
)
1520 DBG1(DBG_IKE
, "unable to reauthenticate IKE_SA, no CHILD_SA "
1529 { /* check if we have children to keep up at all */
1530 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1531 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1533 if (this->state
== IKE_DELETING
)
1535 action
= child_sa
->get_close_action(child_sa
);
1539 action
= child_sa
->get_dpd_action(child_sa
);
1543 case ACTION_RESTART
:
1547 charon
->traps
->install(charon
->traps
, this->peer_cfg
,
1548 child_sa
->get_config(child_sa
));
1554 enumerator
->destroy(enumerator
);
1556 /* mediation connections have no children, keep them up anyway */
1557 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1568 /* check if we are able to reestablish this IKE_SA */
1569 if (!has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1570 (this->other_vips
->get_count(this->other_vips
) != 0 ||
1571 has_condition(this, COND_EAP_AUTHENTICATED
)
1573 || this->is_mediation_server
1577 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due to asymmetric setup");
1581 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1582 this->version
, TRUE
);
1587 new->set_peer_cfg(new, this->peer_cfg
);
1588 host
= this->other_host
;
1589 new->set_other_host(new, host
->clone(host
));
1590 host
= this->my_host
;
1591 new->set_my_host(new, host
->clone(host
));
1592 /* if we already have a virtual IP, we reuse it */
1593 enumerator
= this->my_vips
->create_enumerator(this->my_vips
);
1594 while (enumerator
->enumerate(enumerator
, &host
))
1596 new->add_virtual_ip(new, TRUE
, host
);
1598 enumerator
->destroy(enumerator
);
1601 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1603 status
= new->initiate(new, NULL
, 0, NULL
, NULL
);
1608 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1609 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1611 if (this->is_reauthenticating
)
1613 switch (child_sa
->get_state(child_sa
))
1616 { /* move routed child directly */
1617 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1618 new->add_child_sa(new, child_sa
);
1619 action
= ACTION_NONE
;
1623 { /* initiate/queue all other CHILD_SAs */
1624 action
= ACTION_RESTART
;
1630 { /* only restart CHILD_SAs that are configured accordingly */
1631 if (this->state
== IKE_DELETING
)
1633 action
= child_sa
->get_close_action(child_sa
);
1637 action
= child_sa
->get_dpd_action(child_sa
);
1642 case ACTION_RESTART
:
1643 child_cfg
= child_sa
->get_config(child_sa
);
1644 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
1645 child_cfg
->get_name(child_cfg
));
1646 child_cfg
->get_ref(child_cfg
);
1647 status
= new->initiate(new, child_cfg
, 0, NULL
, NULL
);
1652 if (status
== DESTROY_ME
)
1657 enumerator
->destroy(enumerator
);
1660 if (status
== DESTROY_ME
)
1662 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1667 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1670 charon
->bus
->set_sa(charon
->bus
, &this->public);
1674 METHOD(ike_sa_t
, retransmit
, status_t
,
1675 private_ike_sa_t
*this, u_int32_t message_id
)
1677 if (this->state
== IKE_PASSIVE
)
1679 return INVALID_STATE
;
1681 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
1682 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1684 /* send a proper signal to brief interested bus listeners */
1685 switch (this->state
)
1687 case IKE_CONNECTING
:
1689 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1690 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1692 if (tries
== 0 || tries
> this->keyingtry
)
1694 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
1695 this->keyingtry
+ 1, tries
);
1697 resolve_hosts(this);
1698 this->task_manager
->queue_ike(this->task_manager
);
1699 return this->task_manager
->initiate(this->task_manager
);
1701 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
1705 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
1706 if (this->is_reauthenticating
)
1708 DBG1(DBG_IKE
, "delete during reauthentication failed, "
1709 "trying to reestablish IKE_SA anyway");
1714 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
1725 METHOD(ike_sa_t
, set_auth_lifetime
, status_t
,
1726 private_ike_sa_t
*this, u_int32_t lifetime
)
1728 u_int32_t diff
, hard
, soft
, now
;
1729 ike_auth_lifetime_t
*task
;
1732 diff
= this->peer_cfg
->get_over_time(this->peer_cfg
);
1733 now
= time_monotonic(NULL
);
1734 hard
= now
+ lifetime
;
1737 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1738 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1739 send_update
= this->state
== IKE_ESTABLISHED
&& this->version
== IKEV2
&&
1740 !has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1741 (this->other_vips
->get_count(this->other_vips
) != 0 ||
1742 has_condition(this, COND_EAP_AUTHENTICATED
));
1744 if (lifetime
< diff
)
1746 this->stats
[STAT_REAUTH
] = now
;
1750 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, "
1751 "starting reauthentication", lifetime
);
1752 lib
->processor
->queue_job(lib
->processor
,
1753 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
1756 else if (this->stats
[STAT_REAUTH
] == 0 ||
1757 this->stats
[STAT_REAUTH
] > soft
)
1759 this->stats
[STAT_REAUTH
] = soft
;
1762 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling "
1763 "reauthentication in %ds", lifetime
, lifetime
- diff
);
1764 lib
->scheduler
->schedule_job(lib
->scheduler
,
1765 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
1771 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, "
1772 "reauthentication already scheduled in %ds", lifetime
,
1773 this->stats
[STAT_REAUTH
] - time_monotonic(NULL
));
1774 send_update
= FALSE
;
1776 /* give at least some seconds to reauthenticate */
1777 this->stats
[STAT_DELETE
] = max(hard
, now
+ 10);
1781 task
= ike_auth_lifetime_create(&this->public, TRUE
);
1782 this->task_manager
->queue_task(this->task_manager
, &task
->task
);
1783 return this->task_manager
->initiate(this->task_manager
);
1789 * Check if the current combination of source and destination address is still
1792 static bool is_current_path_valid(private_ike_sa_t
*this)
1796 src
= hydra
->kernel_interface
->get_source_addr(hydra
->kernel_interface
,
1797 this->other_host
, this->my_host
);
1800 if (src
->ip_equals(src
, this->my_host
))
1810 * Check if we have any path avialable for this IKE SA.
1812 static bool is_any_path_valid(private_ike_sa_t
*this)
1815 enumerator_t
*enumerator
;
1816 host_t
*src
= NULL
, *addr
;
1818 DBG1(DBG_IKE
, "old path is not available anymore, try to find another");
1819 enumerator
= create_peer_address_enumerator(this);
1820 while (enumerator
->enumerate(enumerator
, &addr
))
1822 DBG1(DBG_IKE
, "looking for a route to %H ...", addr
);
1823 src
= hydra
->kernel_interface
->get_source_addr(
1824 hydra
->kernel_interface
, addr
, NULL
);
1830 enumerator
->destroy(enumerator
);
1839 METHOD(ike_sa_t
, roam
, status_t
,
1840 private_ike_sa_t
*this, bool address
)
1842 switch (this->state
)
1846 case IKE_DESTROYING
:
1853 /* keep existing path if possible */
1854 if (is_current_path_valid(this))
1856 DBG2(DBG_IKE
, "keeping connection path %H - %H",
1857 this->my_host
, this->other_host
);
1858 set_condition(this, COND_STALE
, FALSE
);
1860 if (supports_extension(this, EXT_MOBIKE
) && address
)
1861 { /* if any addresses changed, send an updated list */
1862 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1863 this->task_manager
->queue_mobike(this->task_manager
, FALSE
, TRUE
);
1864 return this->task_manager
->initiate(this->task_manager
);
1869 if (!is_any_path_valid(this))
1871 DBG1(DBG_IKE
, "no route found to reach %H, MOBIKE update deferred",
1873 set_condition(this, COND_STALE
, TRUE
);
1876 set_condition(this, COND_STALE
, FALSE
);
1878 /* update addresses with mobike, if supported ... */
1879 if (supports_extension(this, EXT_MOBIKE
))
1881 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1882 { /* responder updates the peer about changed address config */
1883 DBG1(DBG_IKE
, "sending address list update using MOBIKE, "
1884 "implicitly requesting an address change");
1889 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
1891 this->task_manager
->queue_mobike(this->task_manager
, TRUE
, address
);
1892 return this->task_manager
->initiate(this->task_manager
);
1895 /* ... reauth if not */
1896 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1897 { /* responder does not reauthenticate */
1898 set_condition(this, COND_STALE
, TRUE
);
1901 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
1902 /* since our previous path is not valid anymore, try and find a new one */
1903 resolve_hosts(this);
1904 return reauth(this);
1907 METHOD(ike_sa_t
, add_configuration_attribute
, void,
1908 private_ike_sa_t
*this, attribute_handler_t
*handler
,
1909 configuration_attribute_type_t type
, chunk_t data
)
1911 attribute_entry_t
*entry
= malloc_thing(attribute_entry_t
);
1913 entry
->handler
= handler
;
1915 entry
->data
= chunk_clone(data
);
1917 this->attributes
->insert_last(this->attributes
, entry
);
1920 METHOD(ike_sa_t
, create_task_enumerator
, enumerator_t
*,
1921 private_ike_sa_t
*this, task_queue_t queue
)
1923 return this->task_manager
->create_task_enumerator(this->task_manager
, queue
);
1926 METHOD(ike_sa_t
, flush_queue
, void,
1927 private_ike_sa_t
*this, task_queue_t queue
)
1929 this->task_manager
->flush_queue(this->task_manager
, queue
);
1932 METHOD(ike_sa_t
, queue_task
, void,
1933 private_ike_sa_t
*this, task_t
*task
)
1935 this->task_manager
->queue_task(this->task_manager
, task
);
1938 METHOD(ike_sa_t
, inherit
, void,
1939 private_ike_sa_t
*this, ike_sa_t
*other_public
)
1941 private_ike_sa_t
*other
= (private_ike_sa_t
*)other_public
;
1942 child_sa_t
*child_sa
;
1943 attribute_entry_t
*entry
;
1944 enumerator_t
*enumerator
;
1948 /* apply hosts and ids */
1949 this->my_host
->destroy(this->my_host
);
1950 this->other_host
->destroy(this->other_host
);
1951 this->my_id
->destroy(this->my_id
);
1952 this->other_id
->destroy(this->other_id
);
1953 this->my_host
= other
->my_host
->clone(other
->my_host
);
1954 this->other_host
= other
->other_host
->clone(other
->other_host
);
1955 this->my_id
= other
->my_id
->clone(other
->my_id
);
1956 this->other_id
= other
->other_id
->clone(other
->other_id
);
1958 /* apply assigned virtual IPs... */
1959 while (this->my_vips
->remove_last(this->my_vips
, (void**)&vip
) == SUCCESS
)
1961 other
->my_vips
->insert_first(other
->my_vips
, vip
);
1963 while (this->other_vips
->remove_last(this->other_vips
,
1964 (void**)&vip
) == SUCCESS
)
1966 other
->other_vips
->insert_first(other
->other_vips
, vip
);
1969 /* authentication information */
1970 enumerator
= other
->my_auths
->create_enumerator(other
->my_auths
);
1971 while (enumerator
->enumerate(enumerator
, &cfg
))
1973 this->my_auths
->insert_last(this->my_auths
, cfg
->clone(cfg
));
1975 enumerator
->destroy(enumerator
);
1976 enumerator
= other
->other_auths
->create_enumerator(other
->other_auths
);
1977 while (enumerator
->enumerate(enumerator
, &cfg
))
1979 this->other_auths
->insert_last(this->other_auths
, cfg
->clone(cfg
));
1981 enumerator
->destroy(enumerator
);
1983 /* ... and configuration attributes */
1984 while (other
->attributes
->remove_last(other
->attributes
,
1985 (void**)&entry
) == SUCCESS
)
1987 this->attributes
->insert_first(this->attributes
, entry
);
1990 /* inherit all conditions */
1991 this->conditions
= other
->conditions
;
1992 if (this->conditions
& COND_NAT_HERE
)
1994 send_keepalive(this);
1998 if (other
->is_mediation_server
)
2000 act_as_mediation_server(this);
2002 else if (other
->server_reflexive_host
)
2004 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
2005 other
->server_reflexive_host
);
2009 /* adopt all children */
2010 while (other
->child_sas
->remove_last(other
->child_sas
,
2011 (void**)&child_sa
) == SUCCESS
)
2013 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2016 /* move pending tasks to the new IKE_SA */
2017 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2019 /* reauthentication timeout survives a rekeying */
2020 if (other
->stats
[STAT_REAUTH
])
2022 time_t reauth
, delete, now
= time_monotonic(NULL
);
2024 this->stats
[STAT_REAUTH
] = other
->stats
[STAT_REAUTH
];
2025 reauth
= this->stats
[STAT_REAUTH
] - now
;
2026 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
2027 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
] + delete;
2028 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
2029 "lifetime reduced to %ds", reauth
, delete);
2030 lib
->scheduler
->schedule_job(lib
->scheduler
,
2031 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
), reauth
);
2032 lib
->scheduler
->schedule_job(lib
->scheduler
,
2033 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
), delete);
2037 METHOD(ike_sa_t
, destroy
, void,
2038 private_ike_sa_t
*this)
2040 attribute_entry_t
*entry
;
2043 charon
->bus
->set_sa(charon
->bus
, &this->public);
2045 set_state(this, IKE_DESTROYING
);
2046 DESTROY_IF(this->task_manager
);
2048 /* remove attributes first, as we pass the IKE_SA to the handler */
2049 while (this->attributes
->remove_last(this->attributes
,
2050 (void**)&entry
) == SUCCESS
)
2052 hydra
->attributes
->release(hydra
->attributes
, entry
->handler
,
2053 this->other_id
, entry
->type
, entry
->data
);
2054 free(entry
->data
.ptr
);
2057 this->attributes
->destroy(this->attributes
);
2059 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2061 /* unset SA after here to avoid usage by the listeners */
2062 charon
->bus
->set_sa(charon
->bus
, NULL
);
2064 DESTROY_IF(this->keymat
);
2066 while (this->my_vips
->remove_last(this->my_vips
, (void**)&vip
) == SUCCESS
)
2068 hydra
->kernel_interface
->del_ip(hydra
->kernel_interface
, vip
);
2071 this->my_vips
->destroy(this->my_vips
);
2072 while (this->other_vips
->remove_last(this->other_vips
,
2073 (void**)&vip
) == SUCCESS
)
2077 enumerator_t
*enumerator
;
2080 enumerator
= this->peer_cfg
->create_pool_enumerator(this->peer_cfg
);
2081 while (enumerator
->enumerate(enumerator
, &pool
))
2083 if (hydra
->attributes
->release_address(hydra
->attributes
, pool
,
2084 vip
, get_other_eap_id(this)))
2089 enumerator
->destroy(enumerator
);
2093 this->other_vips
->destroy(this->other_vips
);
2094 this->peer_addresses
->destroy_offset(this->peer_addresses
,
2095 offsetof(host_t
, destroy
));
2097 if (this->is_mediation_server
)
2099 charon
->mediation_manager
->remove(charon
->mediation_manager
,
2102 DESTROY_IF(this->server_reflexive_host
);
2103 chunk_free(&this->connect_id
);
2105 free(this->nat_detection_dest
.ptr
);
2107 DESTROY_IF(this->my_host
);
2108 DESTROY_IF(this->other_host
);
2109 DESTROY_IF(this->my_id
);
2110 DESTROY_IF(this->other_id
);
2111 DESTROY_IF(this->local_host
);
2112 DESTROY_IF(this->remote_host
);
2114 DESTROY_IF(this->ike_cfg
);
2115 DESTROY_IF(this->peer_cfg
);
2116 DESTROY_IF(this->proposal
);
2117 this->my_auth
->destroy(this->my_auth
);
2118 this->other_auth
->destroy(this->other_auth
);
2119 this->my_auths
->destroy_offset(this->my_auths
,
2120 offsetof(auth_cfg_t
, destroy
));
2121 this->other_auths
->destroy_offset(this->other_auths
,
2122 offsetof(auth_cfg_t
, destroy
));
2124 this->ike_sa_id
->destroy(this->ike_sa_id
);
2129 * Described in header.
2131 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
, bool initiator
,
2132 ike_version_t version
)
2134 private_ike_sa_t
*this;
2135 static u_int32_t unique_id
= 0;
2137 if (version
== IKE_ANY
)
2138 { /* prefer IKEv2 if protocol not specified */
2148 .get_version
= _get_version
,
2149 .get_state
= _get_state
,
2150 .set_state
= _set_state
,
2151 .get_name
= _get_name
,
2152 .get_statistic
= _get_statistic
,
2153 .set_statistic
= _set_statistic
,
2154 .process_message
= _process_message
,
2155 .initiate
= _initiate
,
2156 .retry_initiate
= _retry_initiate
,
2157 .get_ike_cfg
= _get_ike_cfg
,
2158 .set_ike_cfg
= _set_ike_cfg
,
2159 .get_peer_cfg
= _get_peer_cfg
,
2160 .set_peer_cfg
= _set_peer_cfg
,
2161 .get_auth_cfg
= _get_auth_cfg
,
2162 .create_auth_cfg_enumerator
= _create_auth_cfg_enumerator
,
2163 .add_auth_cfg
= _add_auth_cfg
,
2164 .get_proposal
= _get_proposal
,
2165 .set_proposal
= _set_proposal
,
2167 .get_my_host
= _get_my_host
,
2168 .set_my_host
= _set_my_host
,
2169 .get_other_host
= _get_other_host
,
2170 .set_other_host
= _set_other_host
,
2171 .set_message_id
= _set_message_id
,
2172 .float_ports
= _float_ports
,
2173 .update_hosts
= _update_hosts
,
2174 .get_my_id
= _get_my_id
,
2175 .set_my_id
= _set_my_id
,
2176 .get_other_id
= _get_other_id
,
2177 .set_other_id
= _set_other_id
,
2178 .get_other_eap_id
= _get_other_eap_id
,
2179 .enable_extension
= _enable_extension
,
2180 .supports_extension
= _supports_extension
,
2181 .set_condition
= _set_condition
,
2182 .has_condition
= _has_condition
,
2183 .set_pending_updates
= _set_pending_updates
,
2184 .get_pending_updates
= _get_pending_updates
,
2185 .create_peer_address_enumerator
= _create_peer_address_enumerator
,
2186 .add_peer_address
= _add_peer_address
,
2187 .clear_peer_addresses
= _clear_peer_addresses
,
2188 .has_mapping_changed
= _has_mapping_changed
,
2189 .retransmit
= _retransmit
,
2191 .destroy
= _destroy
,
2192 .send_dpd
= _send_dpd
,
2193 .send_keepalive
= _send_keepalive
,
2194 .get_keymat
= _get_keymat
,
2195 .add_child_sa
= _add_child_sa
,
2196 .get_child_sa
= _get_child_sa
,
2197 .get_child_count
= _get_child_count
,
2198 .create_child_sa_enumerator
= _create_child_sa_enumerator
,
2199 .remove_child_sa
= _remove_child_sa
,
2200 .rekey_child_sa
= _rekey_child_sa
,
2201 .delete_child_sa
= _delete_child_sa
,
2202 .destroy_child_sa
= _destroy_child_sa
,
2205 .reestablish
= _reestablish
,
2206 .set_auth_lifetime
= _set_auth_lifetime
,
2208 .inherit
= _inherit
,
2209 .generate_message
= _generate_message
,
2211 .get_unique_id
= _get_unique_id
,
2212 .add_virtual_ip
= _add_virtual_ip
,
2213 .clear_virtual_ips
= _clear_virtual_ips
,
2214 .create_virtual_ip_enumerator
= _create_virtual_ip_enumerator
,
2215 .add_configuration_attribute
= _add_configuration_attribute
,
2216 .set_kmaddress
= _set_kmaddress
,
2217 .create_task_enumerator
= _create_task_enumerator
,
2218 .flush_queue
= _flush_queue
,
2219 .queue_task
= _queue_task
,
2221 .act_as_mediation_server
= _act_as_mediation_server
,
2222 .get_server_reflexive_host
= _get_server_reflexive_host
,
2223 .set_server_reflexive_host
= _set_server_reflexive_host
,
2224 .get_connect_id
= _get_connect_id
,
2225 .initiate_mediation
= _initiate_mediation
,
2226 .initiate_mediated
= _initiate_mediated
,
2228 .callback
= _callback
,
2229 .respond
= _respond
,
2232 .ike_sa_id
= ike_sa_id
->clone(ike_sa_id
),
2234 .child_sas
= linked_list_create(),
2235 .my_host
= host_create_any(AF_INET
),
2236 .other_host
= host_create_any(AF_INET
),
2237 .my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
2238 .other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
2239 .keymat
= keymat_create(version
, initiator
),
2240 .state
= IKE_CREATED
,
2241 .stats
[STAT_INBOUND
] = time_monotonic(NULL
),
2242 .stats
[STAT_OUTBOUND
] = time_monotonic(NULL
),
2243 .my_auth
= auth_cfg_create(),
2244 .other_auth
= auth_cfg_create(),
2245 .my_auths
= linked_list_create(),
2246 .other_auths
= linked_list_create(),
2247 .unique_id
= ++unique_id
,
2248 .peer_addresses
= linked_list_create(),
2249 .my_vips
= linked_list_create(),
2250 .other_vips
= linked_list_create(),
2251 .attributes
= linked_list_create(),
2252 .keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2253 "%s.keep_alive", KEEPALIVE_INTERVAL
, charon
->name
),
2254 .retry_initiate_interval
= lib
->settings
->get_time(lib
->settings
,
2255 "%s.retry_initiate_interval", 0, charon
->name
),
2256 .flush_auth_cfg
= lib
->settings
->get_bool(lib
->settings
,
2257 "%s.flush_auth_cfg", FALSE
, charon
->name
),
2260 if (version
== IKEV2
)
2261 { /* always supported with IKEv2 */
2262 enable_extension(this, EXT_DPD
);
2265 this->task_manager
= task_manager_create(&this->public);
2266 this->my_host
->set_port(this->my_host
,
2267 charon
->socket
->get_port(charon
->socket
, FALSE
));
2269 if (!this->task_manager
|| !this->keymat
)
2271 DBG1(DBG_IKE
, "IKE version %d not supported", this->version
);
2275 return &this->public;