2 * Copyright (C) 2006-2011 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>
38 #include <sa/ikev2/tasks/ike_me.h>
39 #include <processing/jobs/initiate_mediation_job.h>
42 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DESTROYING
,
52 typedef struct private_ike_sa_t private_ike_sa_t
;
53 typedef struct attribute_entry_t attribute_entry_t
;
56 * Private data of an ike_sa_t object.
58 struct private_ike_sa_t
{
66 * Identifier for the current IKE_SA.
68 ike_sa_id_t
*ike_sa_id
;
71 * IKE version of this SA.
73 ike_version_t version
;
76 * unique numerical ID for this IKE_SA.
81 * Current state of the IKE_SA
86 * IKE configuration used to set up this IKE_SA
91 * Peer and authentication information to establish IKE_SA.
96 * currently used authentication ruleset, local (as auth_cfg_t)
101 * list of completed local authentication rounds
103 linked_list_t
*my_auths
;
106 * list of completed remote authentication rounds
108 linked_list_t
*other_auths
;
111 * currently used authentication constraints, remote (as auth_cfg_t)
113 auth_cfg_t
*other_auth
;
116 * Selected IKE proposal
118 proposal_t
*proposal
;
121 * Juggles tasks to process messages
123 task_manager_t
*task_manager
;
126 * Address of local host
131 * Address of remote host
137 * Are we mediation server
139 bool is_mediation_server
;
142 * Server reflexive host
144 host_t
*server_reflexive_host
;
153 * Identification used for us
155 identification_t
*my_id
;
158 * Identification used for other
160 identification_t
*other_id
;
163 * set of extensions the peer supports
165 ike_extension_t extensions
;
168 * set of condition flags currently enabled for this IKE_SA
170 ike_condition_t conditions
;
173 * Linked List containing the child sa's of the current IKE_SA.
175 linked_list_t
*child_sas
;
178 * keymat of this IKE_SA
183 * Virtual IP on local host, if any
185 host_t
*my_virtual_ip
;
188 * Virtual IP on remote host, if any
190 host_t
*other_virtual_ip
;
193 * List of configuration attributes (attribute_entry_t)
195 linked_list_t
*attributes
;
198 * list of peers additional addresses, transmitted via MOBIKE
200 linked_list_t
*additional_addresses
;
203 * previously value of received DESTINATION_IP hash
205 chunk_t nat_detection_dest
;
208 * number pending UPDATE_SA_ADDRESS (MOBIKE)
210 u_int32_t pending_updates
;
213 * NAT keep alive interval
215 u_int32_t keepalive_interval
;
218 * Timestamps for this IKE_SA
220 u_int32_t stats
[STAT_MAX
];
223 * how many times we have retried so far (keyingtries)
228 * local host address to be used for IKE, set via MIGRATE kernel message
233 * remote host address to be used for IKE, set via MIGRATE kernel message
238 * Flush auth configs once established?
244 * Entry to maintain install configuration attributes during IKE_SA lifetime
246 struct attribute_entry_t
{
247 /** handler used to install this attribute */
248 attribute_handler_t
*handler
;
249 /** attribute type */
250 configuration_attribute_type_t type
;
251 /** attribute data */
256 * get the time of the latest traffic processed by the kernel
258 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
260 enumerator_t
*enumerator
;
261 child_sa_t
*child_sa
;
262 time_t use_time
, current
;
266 use_time
= this->stats
[STAT_INBOUND
];
270 use_time
= this->stats
[STAT_OUTBOUND
];
272 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
273 while (enumerator
->enumerate(enumerator
, &child_sa
))
275 child_sa
->get_usestats(child_sa
, inbound
, ¤t
, NULL
);
276 use_time
= max(use_time
, current
);
278 enumerator
->destroy(enumerator
);
283 METHOD(ike_sa_t
, get_unique_id
, u_int32_t
,
284 private_ike_sa_t
*this)
286 return this->unique_id
;
289 METHOD(ike_sa_t
, get_name
, char*,
290 private_ike_sa_t
*this)
294 return this->peer_cfg
->get_name(this->peer_cfg
);
299 METHOD(ike_sa_t
, get_statistic
, u_int32_t
,
300 private_ike_sa_t
*this, statistic_t kind
)
304 return this->stats
[kind
];
309 METHOD(ike_sa_t
, set_statistic
, void,
310 private_ike_sa_t
*this, statistic_t kind
, u_int32_t value
)
314 this->stats
[kind
] = value
;
318 METHOD(ike_sa_t
, get_my_host
, host_t
*,
319 private_ike_sa_t
*this)
321 return this->my_host
;
324 METHOD(ike_sa_t
, set_my_host
, void,
325 private_ike_sa_t
*this, host_t
*me
)
327 DESTROY_IF(this->my_host
);
331 METHOD(ike_sa_t
, get_other_host
, host_t
*,
332 private_ike_sa_t
*this)
334 return this->other_host
;
337 METHOD(ike_sa_t
, set_other_host
, void,
338 private_ike_sa_t
*this, host_t
*other
)
340 DESTROY_IF(this->other_host
);
341 this->other_host
= other
;
344 METHOD(ike_sa_t
, get_peer_cfg
, peer_cfg_t
*,
345 private_ike_sa_t
*this)
347 return this->peer_cfg
;
350 METHOD(ike_sa_t
, set_peer_cfg
, void,
351 private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
353 peer_cfg
->get_ref(peer_cfg
);
354 DESTROY_IF(this->peer_cfg
);
355 this->peer_cfg
= peer_cfg
;
357 if (this->ike_cfg
== NULL
)
359 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
360 this->ike_cfg
->get_ref(this->ike_cfg
);
364 METHOD(ike_sa_t
, get_auth_cfg
, auth_cfg_t
*,
365 private_ike_sa_t
*this, bool local
)
369 return this->my_auth
;
371 return this->other_auth
;
374 METHOD(ike_sa_t
, add_auth_cfg
, void,
375 private_ike_sa_t
*this, bool local
, auth_cfg_t
*cfg
)
379 this->my_auths
->insert_last(this->my_auths
, cfg
);
383 this->other_auths
->insert_last(this->other_auths
, cfg
);
387 METHOD(ike_sa_t
, create_auth_cfg_enumerator
, enumerator_t
*,
388 private_ike_sa_t
*this, bool local
)
392 return this->my_auths
->create_enumerator(this->my_auths
);
394 return this->other_auths
->create_enumerator(this->other_auths
);
398 * Flush the stored authentication round information
400 static void flush_auth_cfgs(private_ike_sa_t
*this)
404 this->my_auth
->purge(this->my_auth
, FALSE
);
405 this->other_auth
->purge(this->other_auth
, FALSE
);
407 while (this->my_auths
->remove_last(this->my_auths
,
408 (void**)&cfg
) == SUCCESS
)
412 while (this->other_auths
->remove_last(this->other_auths
,
413 (void**)&cfg
) == SUCCESS
)
419 METHOD(ike_sa_t
, get_proposal
, proposal_t
*,
420 private_ike_sa_t
*this)
422 return this->proposal
;
425 METHOD(ike_sa_t
, set_proposal
, void,
426 private_ike_sa_t
*this, proposal_t
*proposal
)
428 DESTROY_IF(this->proposal
);
429 this->proposal
= proposal
->clone(proposal
);
432 METHOD(ike_sa_t
, set_message_id
, void,
433 private_ike_sa_t
*this, bool initiate
, u_int32_t mid
)
437 this->task_manager
->reset(this->task_manager
, mid
, UINT_MAX
);
441 this->task_manager
->reset(this->task_manager
, UINT_MAX
, mid
);
445 METHOD(ike_sa_t
, send_keepalive
, void,
446 private_ike_sa_t
*this)
448 send_keepalive_job_t
*job
;
449 time_t last_out
, now
, diff
;
451 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
452 { /* disable keep alives if we are not NATed anymore */
456 last_out
= get_use_time(this, FALSE
);
457 now
= time_monotonic(NULL
);
459 diff
= now
- last_out
;
461 if (diff
>= this->keepalive_interval
)
466 packet
= packet_create();
467 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
468 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
469 data
.ptr
= malloc(1);
472 packet
->set_data(packet
, data
);
473 DBG1(DBG_IKE
, "sending keep alive");
474 charon
->sender
->send(charon
->sender
, packet
);
477 job
= send_keepalive_job_create(this->ike_sa_id
);
478 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)job
,
479 this->keepalive_interval
- diff
);
482 METHOD(ike_sa_t
, get_ike_cfg
, ike_cfg_t
*,
483 private_ike_sa_t
*this)
485 return this->ike_cfg
;
488 METHOD(ike_sa_t
, set_ike_cfg
, void,
489 private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
491 ike_cfg
->get_ref(ike_cfg
);
492 this->ike_cfg
= ike_cfg
;
495 METHOD(ike_sa_t
, enable_extension
, void,
496 private_ike_sa_t
*this, ike_extension_t extension
)
498 this->extensions
|= extension
;
501 METHOD(ike_sa_t
, supports_extension
, bool,
502 private_ike_sa_t
*this, ike_extension_t extension
)
504 return (this->extensions
& extension
) != FALSE
;
507 METHOD(ike_sa_t
, has_condition
, bool,
508 private_ike_sa_t
*this, ike_condition_t condition
)
510 return (this->conditions
& condition
) != FALSE
;
513 METHOD(ike_sa_t
, set_condition
, void,
514 private_ike_sa_t
*this, ike_condition_t condition
, bool enable
)
516 if (has_condition(this, condition
) != enable
)
520 this->conditions
|= condition
;
524 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
525 this->conditions
|= COND_NAT_ANY
;
526 send_keepalive(this);
529 DBG1(DBG_IKE
, "remote host is behind NAT");
530 this->conditions
|= COND_NAT_ANY
;
533 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
534 this->conditions
|= COND_NAT_ANY
;
542 this->conditions
&= ~condition
;
548 set_condition(this, COND_NAT_ANY
,
549 has_condition(this, COND_NAT_HERE
) ||
550 has_condition(this, COND_NAT_THERE
) ||
551 has_condition(this, COND_NAT_FAKE
));
560 METHOD(ike_sa_t
, send_dpd
, status_t
,
561 private_ike_sa_t
*this)
566 if (this->state
== IKE_PASSIVE
)
568 return INVALID_STATE
;
570 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
571 if (this->task_manager
->busy(this->task_manager
))
573 /* an exchange is in the air, no need to start a DPD check */
578 /* check if there was any inbound traffic */
580 last_in
= get_use_time(this, TRUE
);
581 now
= time_monotonic(NULL
);
582 diff
= now
- last_in
;
583 if (!delay
|| diff
>= delay
)
585 /* to long ago, initiate dead peer detection */
586 DBG1(DBG_IKE
, "sending DPD request");
587 this->task_manager
->queue_dpd(this->task_manager
);
591 /* recheck in "interval" seconds */
594 job
= (job_t
*)send_dpd_job_create(this->ike_sa_id
);
595 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, delay
- diff
);
597 return this->task_manager
->initiate(this->task_manager
);
600 METHOD(ike_sa_t
, get_state
, ike_sa_state_t
,
601 private_ike_sa_t
*this)
606 METHOD(ike_sa_t
, set_state
, void,
607 private_ike_sa_t
*this, ike_sa_state_t state
)
609 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
610 get_name(this), this->unique_id
,
611 ike_sa_state_names
, this->state
,
612 ike_sa_state_names
, state
);
616 case IKE_ESTABLISHED
:
618 if (this->state
== IKE_CONNECTING
||
619 this->state
== IKE_PASSIVE
)
624 /* calculate rekey, reauth and lifetime */
625 this->stats
[STAT_ESTABLISHED
] = time_monotonic(NULL
);
627 /* schedule rekeying if we have a time which is smaller than
628 * an already scheduled rekeying */
629 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
, TRUE
);
630 if (t
&& (this->stats
[STAT_REKEY
] == 0 ||
631 (this->stats
[STAT_REKEY
] > t
+ this->stats
[STAT_ESTABLISHED
])))
633 this->stats
[STAT_REKEY
] = t
+ this->stats
[STAT_ESTABLISHED
];
634 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
635 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
636 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
638 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
, TRUE
);
639 if (t
&& (this->stats
[STAT_REAUTH
] == 0 ||
640 (this->stats
[STAT_REAUTH
] > t
+ this->stats
[STAT_ESTABLISHED
])))
642 this->stats
[STAT_REAUTH
] = t
+ this->stats
[STAT_ESTABLISHED
];
643 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
644 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
645 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
647 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
648 if (this->stats
[STAT_REKEY
] || this->stats
[STAT_REAUTH
])
650 if (this->stats
[STAT_REAUTH
] == 0)
652 this->stats
[STAT_DELETE
] = this->stats
[STAT_REKEY
];
654 else if (this->stats
[STAT_REKEY
] == 0)
656 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
];
660 this->stats
[STAT_DELETE
] = min(this->stats
[STAT_REKEY
],
661 this->stats
[STAT_REAUTH
]);
663 this->stats
[STAT_DELETE
] += t
;
664 t
= this->stats
[STAT_DELETE
] - this->stats
[STAT_ESTABLISHED
];
665 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
666 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, t
);
667 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
670 /* start DPD checks */
671 if (this->peer_cfg
->get_dpd(this->peer_cfg
))
680 /* delete may fail if a packet gets lost, so set a timeout */
681 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
682 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
683 HALF_OPEN_IKE_SA_TIMEOUT
);
689 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
693 METHOD(ike_sa_t
, reset
, void,
694 private_ike_sa_t
*this)
696 /* the responder ID is reset, as peer may choose another one */
697 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
699 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
702 set_state(this, IKE_CREATED
);
704 flush_auth_cfgs(this);
706 this->keymat
->destroy(this->keymat
);
707 this->keymat
= keymat_create(this->version
,
708 this->ike_sa_id
->is_initiator(this->ike_sa_id
));
710 this->task_manager
->reset(this->task_manager
, 0, 0);
713 METHOD(ike_sa_t
, get_keymat
, keymat_t
*,
714 private_ike_sa_t
*this)
719 METHOD(ike_sa_t
, set_virtual_ip
, void,
720 private_ike_sa_t
*this, bool local
, host_t
*ip
)
724 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
725 if (hydra
->kernel_interface
->add_ip(hydra
->kernel_interface
, ip
,
726 this->my_host
) == SUCCESS
)
728 if (this->my_virtual_ip
)
730 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
731 hydra
->kernel_interface
->del_ip(hydra
->kernel_interface
,
732 this->my_virtual_ip
);
734 DESTROY_IF(this->my_virtual_ip
);
735 this->my_virtual_ip
= ip
->clone(ip
);
739 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
740 this->my_virtual_ip
= NULL
;
745 DESTROY_IF(this->other_virtual_ip
);
746 this->other_virtual_ip
= ip
->clone(ip
);
750 METHOD(ike_sa_t
, get_virtual_ip
, host_t
*,
751 private_ike_sa_t
*this, bool local
)
755 return this->my_virtual_ip
;
759 return this->other_virtual_ip
;
763 METHOD(ike_sa_t
, add_additional_address
, void,
764 private_ike_sa_t
*this, host_t
*host
)
766 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
769 METHOD(ike_sa_t
, create_additional_address_enumerator
, enumerator_t
*,
770 private_ike_sa_t
*this)
772 return this->additional_addresses
->create_enumerator(
773 this->additional_addresses
);
776 METHOD(ike_sa_t
, remove_additional_addresses
, void,
777 private_ike_sa_t
*this)
779 enumerator_t
*enumerator
= create_additional_address_enumerator(this);
781 while (enumerator
->enumerate(enumerator
, (void**)&host
))
783 this->additional_addresses
->remove_at(this->additional_addresses
,
787 enumerator
->destroy(enumerator
);
790 METHOD(ike_sa_t
, has_mapping_changed
, bool,
791 private_ike_sa_t
*this, chunk_t hash
)
793 if (this->nat_detection_dest
.ptr
== NULL
)
795 this->nat_detection_dest
= chunk_clone(hash
);
798 if (chunk_equals(hash
, this->nat_detection_dest
))
802 free(this->nat_detection_dest
.ptr
);
803 this->nat_detection_dest
= chunk_clone(hash
);
807 METHOD(ike_sa_t
, set_pending_updates
, void,
808 private_ike_sa_t
*this, u_int32_t updates
)
810 this->pending_updates
= updates
;
813 METHOD(ike_sa_t
, get_pending_updates
, u_int32_t
,
814 private_ike_sa_t
*this)
816 return this->pending_updates
;
819 METHOD(ike_sa_t
, float_ports
, void,
820 private_ike_sa_t
*this)
822 /* do not switch if we have a custom port from MOBIKE/NAT */
823 if (this->my_host
->get_port(this->my_host
) == IKEV2_UDP_PORT
)
825 this->my_host
->set_port(this->my_host
, IKEV2_NATT_PORT
);
827 if (this->other_host
->get_port(this->other_host
) == IKEV2_UDP_PORT
)
829 this->other_host
->set_port(this->other_host
, IKEV2_NATT_PORT
);
833 METHOD(ike_sa_t
, update_hosts
, void,
834 private_ike_sa_t
*this, host_t
*me
, host_t
*other
, bool force
)
844 other
= this->other_host
;
847 /* apply hosts on first received message */
848 if (this->my_host
->is_anyaddr(this->my_host
) ||
849 this->other_host
->is_anyaddr(this->other_host
))
851 set_my_host(this, me
->clone(me
));
852 set_other_host(this, other
->clone(other
));
857 /* update our address in any case */
858 if (!me
->equals(me
, this->my_host
))
860 set_my_host(this, me
->clone(me
));
864 if (!other
->equals(other
, this->other_host
))
866 /* update others address if we are NOT NATed */
867 if (force
|| !has_condition(this, COND_NAT_HERE
))
869 set_other_host(this, other
->clone(other
));
875 /* update all associated CHILD_SAs, if required */
878 enumerator_t
*enumerator
;
879 child_sa_t
*child_sa
;
881 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
882 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
884 if (child_sa
->update(child_sa
, this->my_host
,
885 this->other_host
, this->my_virtual_ip
,
886 has_condition(this, COND_NAT_ANY
)) == NOT_SUPPORTED
)
888 this->public.rekey_child_sa(&this->public,
889 child_sa
->get_protocol(child_sa
),
890 child_sa
->get_spi(child_sa
, TRUE
));
893 enumerator
->destroy(enumerator
);
897 METHOD(ike_sa_t
, generate_message
, status_t
,
898 private_ike_sa_t
*this, message_t
*message
, packet_t
**packet
)
900 if (message
->is_encoded(message
))
902 *packet
= message
->get_packet(message
);
905 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
906 message
->set_ike_sa_id(message
, this->ike_sa_id
);
907 charon
->bus
->message(charon
->bus
, message
, FALSE
);
908 return message
->generate(message
, this->keymat
, packet
);
911 METHOD(ike_sa_t
, set_kmaddress
, void,
912 private_ike_sa_t
*this, host_t
*local
, host_t
*remote
)
914 DESTROY_IF(this->local_host
);
915 DESTROY_IF(this->remote_host
);
916 this->local_host
= local
->clone(local
);
917 this->remote_host
= remote
->clone(remote
);
921 METHOD(ike_sa_t
, act_as_mediation_server
, void,
922 private_ike_sa_t
*this)
924 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
925 this->other_id
, this->ike_sa_id
);
926 this->is_mediation_server
= TRUE
;
929 METHOD(ike_sa_t
, get_server_reflexive_host
, host_t
*,
930 private_ike_sa_t
*this)
932 return this->server_reflexive_host
;
935 METHOD(ike_sa_t
, set_server_reflexive_host
, void,
936 private_ike_sa_t
*this, host_t
*host
)
938 DESTROY_IF(this->server_reflexive_host
);
939 this->server_reflexive_host
= host
;
942 METHOD(ike_sa_t
, get_connect_id
, chunk_t
,
943 private_ike_sa_t
*this)
945 return this->connect_id
;
948 METHOD(ike_sa_t
, respond
, status_t
,
949 private_ike_sa_t
*this, identification_t
*peer_id
, chunk_t connect_id
)
951 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
952 task
->respond(task
, peer_id
, connect_id
);
953 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
954 return this->task_manager
->initiate(this->task_manager
);
957 METHOD(ike_sa_t
, callback
, status_t
,
958 private_ike_sa_t
*this, identification_t
*peer_id
)
960 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
961 task
->callback(task
, peer_id
);
962 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
963 return this->task_manager
->initiate(this->task_manager
);
966 METHOD(ike_sa_t
, relay
, status_t
,
967 private_ike_sa_t
*this, identification_t
*requester
, chunk_t connect_id
,
968 chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
970 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
971 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
972 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
973 return this->task_manager
->initiate(this->task_manager
);
976 METHOD(ike_sa_t
, initiate_mediation
, status_t
,
977 private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
979 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
980 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
981 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
982 return this->task_manager
->initiate(this->task_manager
);
985 METHOD(ike_sa_t
, initiate_mediated
, status_t
,
986 private_ike_sa_t
*this, host_t
*me
, host_t
*other
, chunk_t connect_id
)
988 set_my_host(this, me
->clone(me
));
989 set_other_host(this, other
->clone(other
));
990 chunk_free(&this->connect_id
);
991 this->connect_id
= chunk_clone(connect_id
);
992 return this->task_manager
->initiate(this->task_manager
);
997 * Resolve DNS host in configuration
999 static void resolve_hosts(private_ike_sa_t
*this)
1003 if (this->remote_host
)
1005 host
= this->remote_host
->clone(this->remote_host
);
1006 host
->set_port(host
, IKEV2_UDP_PORT
);
1010 host
= host_create_from_dns(this->ike_cfg
->get_other_addr(this->ike_cfg
),
1011 0, this->ike_cfg
->get_other_port(this->ike_cfg
));
1015 set_other_host(this, host
);
1018 if (this->local_host
)
1020 host
= this->local_host
->clone(this->local_host
);
1021 host
->set_port(host
, IKEV2_UDP_PORT
);
1027 /* use same address family as for other */
1028 if (!this->other_host
->is_anyaddr(this->other_host
))
1030 family
= this->other_host
->get_family(this->other_host
);
1032 host
= host_create_from_dns(this->ike_cfg
->get_my_addr(this->ike_cfg
),
1033 family
, this->ike_cfg
->get_my_port(this->ike_cfg
));
1035 if (host
&& host
->is_anyaddr(host
) &&
1036 !this->other_host
->is_anyaddr(this->other_host
))
1038 host
->destroy(host
);
1039 host
= hydra
->kernel_interface
->get_source_addr(
1040 hydra
->kernel_interface
, this->other_host
, NULL
);
1043 host
->set_port(host
, this->ike_cfg
->get_my_port(this->ike_cfg
));
1046 { /* fallback to address family specific %any(6), if configured */
1047 host
= host_create_from_dns(
1048 this->ike_cfg
->get_my_addr(this->ike_cfg
),
1049 0, this->ike_cfg
->get_my_port(this->ike_cfg
));
1055 set_my_host(this, host
);
1059 METHOD(ike_sa_t
, initiate
, status_t
,
1060 private_ike_sa_t
*this, child_cfg_t
*child_cfg
, u_int32_t reqid
,
1061 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1063 if (this->state
== IKE_CREATED
)
1065 resolve_hosts(this);
1067 if (this->other_host
->is_anyaddr(this->other_host
)
1069 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1073 DESTROY_IF(child_cfg
);
1074 DBG1(DBG_IKE
, "unable to initiate to %%any");
1075 charon
->bus
->alert(charon
->bus
, ALERT_PEER_ADDR_FAILED
);
1079 set_condition(this, COND_ORIGINAL_INITIATOR
, TRUE
);
1080 this->task_manager
->queue_ike(this->task_manager
);
1084 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1086 if (this->state
== IKE_ESTABLISHED
)
1088 /* mediation connection is already established, retrigger state
1089 * change to notify bus listeners */
1090 DBG1(DBG_IKE
, "mediation connection is already up");
1091 set_state(this, IKE_ESTABLISHED
);
1093 DESTROY_IF(child_cfg
);
1099 /* normal IKE_SA with CHILD_SA */
1100 this->task_manager
->queue_child(this->task_manager
, child_cfg
, reqid
,
1103 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1105 /* mediated connection, initiate mediation process */
1106 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1107 lib
->processor
->queue_job(lib
->processor
, job
);
1113 return this->task_manager
->initiate(this->task_manager
);
1116 METHOD(ike_sa_t
, process_message
, status_t
,
1117 private_ike_sa_t
*this, message_t
*message
)
1121 if (this->state
== IKE_PASSIVE
)
1122 { /* do not handle messages in passive state */
1125 if (message
->get_major_version(message
) != this->version
)
1127 DBG1(DBG_IKE
, "ignoring %N IKEv%u exchange on %N SA",
1128 exchange_type_names
, message
->get_exchange_type(message
),
1129 message
->get_major_version(message
),
1130 ike_version_names
, this->version
);
1131 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1132 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1135 status
= this->task_manager
->process_message(this->task_manager
, message
);
1136 if (this->flush_auth_cfg
&& this->state
== IKE_ESTABLISHED
)
1138 /* authentication completed */
1139 this->flush_auth_cfg
= FALSE
;
1140 flush_auth_cfgs(this);
1145 METHOD(ike_sa_t
, get_id
, ike_sa_id_t
*,
1146 private_ike_sa_t
*this)
1148 return this->ike_sa_id
;
1151 METHOD(ike_sa_t
, get_version
, ike_version_t
,
1152 private_ike_sa_t
*this)
1154 return this->version
;
1157 METHOD(ike_sa_t
, get_my_id
, identification_t
*,
1158 private_ike_sa_t
*this)
1163 METHOD(ike_sa_t
, set_my_id
, void,
1164 private_ike_sa_t
*this, identification_t
*me
)
1166 DESTROY_IF(this->my_id
);
1170 METHOD(ike_sa_t
, get_other_id
, identification_t
*,
1171 private_ike_sa_t
*this)
1173 return this->other_id
;
1176 METHOD(ike_sa_t
, get_other_eap_id
, identification_t
*,
1177 private_ike_sa_t
*this)
1179 identification_t
*id
= NULL
, *current
;
1180 enumerator_t
*enumerator
;
1183 enumerator
= this->other_auths
->create_enumerator(this->other_auths
);
1184 while (enumerator
->enumerate(enumerator
, &cfg
))
1186 /* prefer EAP-Identity of last round */
1187 current
= cfg
->get(cfg
, AUTH_RULE_EAP_IDENTITY
);
1188 if (!current
|| current
->get_type(current
) == ID_ANY
)
1190 current
= cfg
->get(cfg
, AUTH_RULE_XAUTH_IDENTITY
);
1192 if (!current
|| current
->get_type(current
) == ID_ANY
)
1194 current
= cfg
->get(cfg
, AUTH_RULE_IDENTITY
);
1196 if (current
&& current
->get_type(current
) != ID_ANY
)
1202 enumerator
->destroy(enumerator
);
1207 return this->other_id
;
1210 METHOD(ike_sa_t
, set_other_id
, void,
1211 private_ike_sa_t
*this, identification_t
*other
)
1213 DESTROY_IF(this->other_id
);
1214 this->other_id
= other
;
1217 METHOD(ike_sa_t
, add_child_sa
, void,
1218 private_ike_sa_t
*this, child_sa_t
*child_sa
)
1220 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1223 METHOD(ike_sa_t
, get_child_sa
, child_sa_t
*,
1224 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
, bool inbound
)
1226 enumerator_t
*enumerator
;
1227 child_sa_t
*current
, *found
= NULL
;
1229 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1230 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
1232 if (current
->get_spi(current
, inbound
) == spi
&&
1233 current
->get_protocol(current
) == protocol
)
1238 enumerator
->destroy(enumerator
);
1242 METHOD(ike_sa_t
, get_child_count
, int,
1243 private_ike_sa_t
*this)
1245 return this->child_sas
->get_count(this->child_sas
);
1248 METHOD(ike_sa_t
, create_child_sa_enumerator
, enumerator_t
*,
1249 private_ike_sa_t
*this)
1251 return this->child_sas
->create_enumerator(this->child_sas
);
1254 METHOD(ike_sa_t
, remove_child_sa
, void,
1255 private_ike_sa_t
*this, enumerator_t
*enumerator
)
1257 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1260 METHOD(ike_sa_t
, rekey_child_sa
, status_t
,
1261 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1263 if (this->state
== IKE_PASSIVE
)
1265 return INVALID_STATE
;
1267 this->task_manager
->queue_child_rekey(this->task_manager
, protocol
, spi
);
1268 return this->task_manager
->initiate(this->task_manager
);
1271 METHOD(ike_sa_t
, delete_child_sa
, status_t
,
1272 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
, bool expired
)
1274 if (this->state
== IKE_PASSIVE
)
1276 return INVALID_STATE
;
1278 this->task_manager
->queue_child_delete(this->task_manager
,
1279 protocol
, spi
, expired
);
1280 return this->task_manager
->initiate(this->task_manager
);
1283 METHOD(ike_sa_t
, destroy_child_sa
, status_t
,
1284 private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1286 enumerator_t
*enumerator
;
1287 child_sa_t
*child_sa
;
1288 status_t status
= NOT_FOUND
;
1290 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1291 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1293 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1294 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1296 this->child_sas
->remove_at(this->child_sas
, enumerator
);
1297 child_sa
->destroy(child_sa
);
1302 enumerator
->destroy(enumerator
);
1306 METHOD(ike_sa_t
, delete_
, status_t
,
1307 private_ike_sa_t
*this)
1309 switch (this->state
)
1312 if (this->version
== IKEV1
)
1313 { /* SA has been reauthenticated, delete */
1317 case IKE_ESTABLISHED
:
1318 this->task_manager
->queue_ike_delete(this->task_manager
);
1319 return this->task_manager
->initiate(this->task_manager
);
1321 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1326 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1327 "without notification", ike_sa_state_names
, this->state
);
1328 charon
->bus
->ike_updown(charon
->bus
, &this->public, FALSE
);
1334 METHOD(ike_sa_t
, rekey
, status_t
,
1335 private_ike_sa_t
*this)
1337 if (this->state
== IKE_PASSIVE
)
1339 return INVALID_STATE
;
1341 this->task_manager
->queue_ike_rekey(this->task_manager
);
1342 return this->task_manager
->initiate(this->task_manager
);
1345 METHOD(ike_sa_t
, reauth
, status_t
,
1346 private_ike_sa_t
*this)
1348 if (this->state
== IKE_PASSIVE
)
1350 return INVALID_STATE
;
1352 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1353 * If the peer does not support RFC4478, there is no way to keep the
1355 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1357 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1358 if (this->other_virtual_ip
!= NULL
||
1359 has_condition(this, COND_XAUTH_AUTHENTICATED
) ||
1360 has_condition(this, COND_EAP_AUTHENTICATED
)
1362 /* as mediation server we too cannot reauth the IKE_SA */
1363 || this->is_mediation_server
1367 time_t now
= time_monotonic(NULL
);
1369 DBG1(DBG_IKE
, "IKE_SA will timeout in %V",
1370 &now
, &this->stats
[STAT_DELETE
]);
1375 DBG1(DBG_IKE
, "reauthenticating actively");
1378 this->task_manager
->queue_ike_reauth(this->task_manager
);
1379 return this->task_manager
->initiate(this->task_manager
);
1382 METHOD(ike_sa_t
, reestablish
, status_t
,
1383 private_ike_sa_t
*this)
1388 enumerator_t
*enumerator
;
1389 child_sa_t
*child_sa
;
1390 child_cfg_t
*child_cfg
;
1391 bool restart
= FALSE
;
1392 status_t status
= FAILED
;
1394 /* check if we have children to keep up at all */
1395 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1396 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1398 if (this->state
== IKE_DELETING
)
1400 action
= child_sa
->get_close_action(child_sa
);
1404 action
= child_sa
->get_dpd_action(child_sa
);
1408 case ACTION_RESTART
:
1412 charon
->traps
->install(charon
->traps
, this->peer_cfg
,
1413 child_sa
->get_config(child_sa
));
1419 enumerator
->destroy(enumerator
);
1421 /* mediation connections have no children, keep them up anyway */
1422 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1432 /* check if we are able to reestablish this IKE_SA */
1433 if (!has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1434 (this->other_virtual_ip
!= NULL
||
1435 has_condition(this, COND_EAP_AUTHENTICATED
)
1437 || this->is_mediation_server
1441 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due to asymmetric setup");
1445 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1446 this->version
, TRUE
);
1451 new->set_peer_cfg(new, this->peer_cfg
);
1452 host
= this->other_host
;
1453 new->set_other_host(new, host
->clone(host
));
1454 host
= this->my_host
;
1455 new->set_my_host(new, host
->clone(host
));
1456 /* if we already have a virtual IP, we reuse it */
1457 host
= this->my_virtual_ip
;
1460 new->set_virtual_ip(new, TRUE
, host
);
1464 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1466 status
= new->initiate(new, NULL
, 0, NULL
, NULL
);
1471 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
1472 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
1474 if (this->state
== IKE_DELETING
)
1476 action
= child_sa
->get_close_action(child_sa
);
1480 action
= child_sa
->get_dpd_action(child_sa
);
1484 case ACTION_RESTART
:
1485 child_cfg
= child_sa
->get_config(child_sa
);
1486 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
1487 child_cfg
->get_name(child_cfg
));
1488 child_cfg
->get_ref(child_cfg
);
1489 status
= new->initiate(new, child_cfg
, 0, NULL
, NULL
);
1494 if (status
== DESTROY_ME
)
1499 enumerator
->destroy(enumerator
);
1502 if (status
== DESTROY_ME
)
1504 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1509 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1512 charon
->bus
->set_sa(charon
->bus
, &this->public);
1516 METHOD(ike_sa_t
, retransmit
, status_t
,
1517 private_ike_sa_t
*this, u_int32_t message_id
)
1519 if (this->state
== IKE_PASSIVE
)
1521 return INVALID_STATE
;
1523 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
1524 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1526 /* send a proper signal to brief interested bus listeners */
1527 switch (this->state
)
1529 case IKE_CONNECTING
:
1531 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1532 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1534 if (tries
== 0 || tries
> this->keyingtry
)
1536 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
1537 this->keyingtry
+ 1, tries
);
1539 this->task_manager
->queue_ike(this->task_manager
);
1540 return this->task_manager
->initiate(this->task_manager
);
1542 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
1546 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
1549 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
1560 METHOD(ike_sa_t
, set_auth_lifetime
, void,
1561 private_ike_sa_t
*this, u_int32_t lifetime
)
1563 u_int32_t reduction
= this->peer_cfg
->get_over_time(this->peer_cfg
);
1564 u_int32_t reauth_time
= time_monotonic(NULL
) + lifetime
- reduction
;
1566 if (lifetime
< reduction
)
1568 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, starting reauthentication",
1570 lib
->processor
->queue_job(lib
->processor
,
1571 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
1573 else if (this->stats
[STAT_REAUTH
] == 0 ||
1574 this->stats
[STAT_REAUTH
] > reauth_time
)
1576 this->stats
[STAT_REAUTH
] = reauth_time
;
1577 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1578 " in %ds", lifetime
, lifetime
- reduction
);
1579 lib
->scheduler
->schedule_job(lib
->scheduler
,
1580 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
1581 lifetime
- reduction
);
1585 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, "
1586 "reauthentication already scheduled in %ds", lifetime
,
1587 this->stats
[STAT_REAUTH
] - time_monotonic(NULL
));
1592 * Check if the current combination of source and destination address is still
1595 static bool is_current_path_valid(private_ike_sa_t
*this)
1599 src
= hydra
->kernel_interface
->get_source_addr(hydra
->kernel_interface
,
1600 this->other_host
, this->my_host
);
1603 if (src
->ip_equals(src
, this->my_host
))
1613 * Check if we have any path avialable for this IKE SA.
1615 static bool is_any_path_valid(private_ike_sa_t
*this)
1618 enumerator_t
*enumerator
;
1620 DBG1(DBG_IKE
, "old path is not available anymore, try to find another");
1621 src
= hydra
->kernel_interface
->get_source_addr(hydra
->kernel_interface
,
1622 this->other_host
, NULL
);
1625 enumerator
= this->additional_addresses
->create_enumerator(
1626 this->additional_addresses
);
1627 while (enumerator
->enumerate(enumerator
, &addr
))
1629 DBG1(DBG_IKE
, "looking for a route to %H ...", addr
);
1630 src
= hydra
->kernel_interface
->get_source_addr(
1631 hydra
->kernel_interface
, addr
, NULL
);
1637 enumerator
->destroy(enumerator
);
1647 METHOD(ike_sa_t
, roam
, status_t
,
1648 private_ike_sa_t
*this, bool address
)
1650 switch (this->state
)
1654 case IKE_DESTROYING
:
1661 /* keep existing path if possible */
1662 if (is_current_path_valid(this))
1664 DBG2(DBG_IKE
, "keeping connection path %H - %H",
1665 this->my_host
, this->other_host
);
1666 set_condition(this, COND_STALE
, FALSE
);
1668 if (supports_extension(this, EXT_MOBIKE
) && address
)
1669 { /* if any addresses changed, send an updated list */
1670 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1671 this->task_manager
->queue_mobike(this->task_manager
, FALSE
, TRUE
);
1672 return this->task_manager
->initiate(this->task_manager
);
1677 if (!is_any_path_valid(this))
1679 DBG1(DBG_IKE
, "no route found to reach %H, MOBIKE update deferred",
1681 set_condition(this, COND_STALE
, TRUE
);
1684 set_condition(this, COND_STALE
, FALSE
);
1686 /* update addresses with mobike, if supported ... */
1687 if (supports_extension(this, EXT_MOBIKE
))
1689 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1690 { /* responder updates the peer about changed address config */
1691 DBG1(DBG_IKE
, "sending address list update using MOBIKE, "
1692 "implicitly requesting an address change");
1697 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
1699 this->task_manager
->queue_mobike(this->task_manager
, TRUE
, address
);
1700 return this->task_manager
->initiate(this->task_manager
);
1703 /* ... reauth if not */
1704 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1705 { /* responder does not reauthenticate */
1706 set_condition(this, COND_STALE
, TRUE
);
1709 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
1710 return reauth(this);
1713 METHOD(ike_sa_t
, add_configuration_attribute
, void,
1714 private_ike_sa_t
*this, attribute_handler_t
*handler
,
1715 configuration_attribute_type_t type
, chunk_t data
)
1717 attribute_entry_t
*entry
= malloc_thing(attribute_entry_t
);
1719 entry
->handler
= handler
;
1721 entry
->data
= chunk_clone(data
);
1723 this->attributes
->insert_last(this->attributes
, entry
);
1726 METHOD(ike_sa_t
, create_task_enumerator
, enumerator_t
*,
1727 private_ike_sa_t
*this, task_queue_t queue
)
1729 return this->task_manager
->create_task_enumerator(this->task_manager
, queue
);
1732 METHOD(ike_sa_t
, queue_task
, void,
1733 private_ike_sa_t
*this, task_t
*task
)
1735 this->task_manager
->queue_task(this->task_manager
, task
);
1738 METHOD(ike_sa_t
, inherit
, void,
1739 private_ike_sa_t
*this, ike_sa_t
*other_public
)
1741 private_ike_sa_t
*other
= (private_ike_sa_t
*)other_public
;
1742 child_sa_t
*child_sa
;
1743 attribute_entry_t
*entry
;
1744 enumerator_t
*enumerator
;
1747 /* apply hosts and ids */
1748 this->my_host
->destroy(this->my_host
);
1749 this->other_host
->destroy(this->other_host
);
1750 this->my_id
->destroy(this->my_id
);
1751 this->other_id
->destroy(this->other_id
);
1752 this->my_host
= other
->my_host
->clone(other
->my_host
);
1753 this->other_host
= other
->other_host
->clone(other
->other_host
);
1754 this->my_id
= other
->my_id
->clone(other
->my_id
);
1755 this->other_id
= other
->other_id
->clone(other
->other_id
);
1757 /* apply virtual assigned IPs... */
1758 if (other
->my_virtual_ip
)
1760 this->my_virtual_ip
= other
->my_virtual_ip
;
1761 other
->my_virtual_ip
= NULL
;
1763 if (other
->other_virtual_ip
)
1765 this->other_virtual_ip
= other
->other_virtual_ip
;
1766 other
->other_virtual_ip
= NULL
;
1769 /* authentication information */
1770 enumerator
= other
->my_auths
->create_enumerator(other
->my_auths
);
1771 while (enumerator
->enumerate(enumerator
, &cfg
))
1773 this->my_auths
->insert_last(this->my_auths
, cfg
->clone(cfg
));
1775 enumerator
->destroy(enumerator
);
1776 enumerator
= other
->other_auths
->create_enumerator(other
->other_auths
);
1777 while (enumerator
->enumerate(enumerator
, &cfg
))
1779 this->other_auths
->insert_last(this->other_auths
, cfg
->clone(cfg
));
1781 enumerator
->destroy(enumerator
);
1783 /* ... and configuration attributes */
1784 while (other
->attributes
->remove_last(other
->attributes
,
1785 (void**)&entry
) == SUCCESS
)
1787 this->attributes
->insert_first(this->attributes
, entry
);
1790 /* inherit all conditions */
1791 this->conditions
= other
->conditions
;
1792 if (this->conditions
& COND_NAT_HERE
)
1794 send_keepalive(this);
1798 if (other
->is_mediation_server
)
1800 act_as_mediation_server(this);
1802 else if (other
->server_reflexive_host
)
1804 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
1805 other
->server_reflexive_host
);
1809 /* adopt all children */
1810 while (other
->child_sas
->remove_last(other
->child_sas
,
1811 (void**)&child_sa
) == SUCCESS
)
1813 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1816 /* move pending tasks to the new IKE_SA */
1817 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
1819 /* reauthentication timeout survives a rekeying */
1820 if (other
->stats
[STAT_REAUTH
])
1822 time_t reauth
, delete, now
= time_monotonic(NULL
);
1824 this->stats
[STAT_REAUTH
] = other
->stats
[STAT_REAUTH
];
1825 reauth
= this->stats
[STAT_REAUTH
] - now
;
1826 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
1827 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
] + delete;
1828 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
1829 "lifetime reduced to %ds", reauth
, delete);
1830 lib
->scheduler
->schedule_job(lib
->scheduler
,
1831 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
), reauth
);
1832 lib
->scheduler
->schedule_job(lib
->scheduler
,
1833 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
), delete);
1837 METHOD(ike_sa_t
, destroy
, void,
1838 private_ike_sa_t
*this)
1840 attribute_entry_t
*entry
;
1842 charon
->bus
->set_sa(charon
->bus
, &this->public);
1844 set_state(this, IKE_DESTROYING
);
1845 DESTROY_IF(this->task_manager
);
1847 /* remove attributes first, as we pass the IKE_SA to the handler */
1848 while (this->attributes
->remove_last(this->attributes
,
1849 (void**)&entry
) == SUCCESS
)
1851 hydra
->attributes
->release(hydra
->attributes
, entry
->handler
,
1852 this->other_id
, entry
->type
, entry
->data
);
1853 free(entry
->data
.ptr
);
1856 this->attributes
->destroy(this->attributes
);
1858 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
1860 /* unset SA after here to avoid usage by the listeners */
1861 charon
->bus
->set_sa(charon
->bus
, NULL
);
1863 DESTROY_IF(this->keymat
);
1865 if (this->my_virtual_ip
)
1867 hydra
->kernel_interface
->del_ip(hydra
->kernel_interface
,
1868 this->my_virtual_ip
);
1869 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
1871 if (this->other_virtual_ip
)
1873 if (this->peer_cfg
&& this->peer_cfg
->get_pool(this->peer_cfg
))
1875 hydra
->attributes
->release_address(hydra
->attributes
,
1876 this->peer_cfg
->get_pool(this->peer_cfg
),
1877 this->other_virtual_ip
, get_other_eap_id(this));
1879 this->other_virtual_ip
->destroy(this->other_virtual_ip
);
1881 this->additional_addresses
->destroy_offset(this->additional_addresses
,
1882 offsetof(host_t
, destroy
));
1884 if (this->is_mediation_server
)
1886 charon
->mediation_manager
->remove(charon
->mediation_manager
,
1889 DESTROY_IF(this->server_reflexive_host
);
1890 chunk_free(&this->connect_id
);
1892 free(this->nat_detection_dest
.ptr
);
1894 DESTROY_IF(this->my_host
);
1895 DESTROY_IF(this->other_host
);
1896 DESTROY_IF(this->my_id
);
1897 DESTROY_IF(this->other_id
);
1898 DESTROY_IF(this->local_host
);
1899 DESTROY_IF(this->remote_host
);
1901 DESTROY_IF(this->ike_cfg
);
1902 DESTROY_IF(this->peer_cfg
);
1903 DESTROY_IF(this->proposal
);
1904 this->my_auth
->destroy(this->my_auth
);
1905 this->other_auth
->destroy(this->other_auth
);
1906 this->my_auths
->destroy_offset(this->my_auths
,
1907 offsetof(auth_cfg_t
, destroy
));
1908 this->other_auths
->destroy_offset(this->other_auths
,
1909 offsetof(auth_cfg_t
, destroy
));
1911 this->ike_sa_id
->destroy(this->ike_sa_id
);
1916 * Described in header.
1918 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
, bool initiator
,
1919 ike_version_t version
)
1921 private_ike_sa_t
*this;
1922 static u_int32_t unique_id
= 0;
1924 if (version
== IKE_ANY
)
1925 { /* prefer IKEv2 if protocol not specified */
1935 .get_version
= _get_version
,
1936 .get_state
= _get_state
,
1937 .set_state
= _set_state
,
1938 .get_name
= _get_name
,
1939 .get_statistic
= _get_statistic
,
1940 .set_statistic
= _set_statistic
,
1941 .process_message
= _process_message
,
1942 .initiate
= _initiate
,
1943 .get_ike_cfg
= _get_ike_cfg
,
1944 .set_ike_cfg
= _set_ike_cfg
,
1945 .get_peer_cfg
= _get_peer_cfg
,
1946 .set_peer_cfg
= _set_peer_cfg
,
1947 .get_auth_cfg
= _get_auth_cfg
,
1948 .create_auth_cfg_enumerator
= _create_auth_cfg_enumerator
,
1949 .add_auth_cfg
= _add_auth_cfg
,
1950 .get_proposal
= _get_proposal
,
1951 .set_proposal
= _set_proposal
,
1953 .get_my_host
= _get_my_host
,
1954 .set_my_host
= _set_my_host
,
1955 .get_other_host
= _get_other_host
,
1956 .set_other_host
= _set_other_host
,
1957 .set_message_id
= _set_message_id
,
1958 .float_ports
= _float_ports
,
1959 .update_hosts
= _update_hosts
,
1960 .get_my_id
= _get_my_id
,
1961 .set_my_id
= _set_my_id
,
1962 .get_other_id
= _get_other_id
,
1963 .set_other_id
= _set_other_id
,
1964 .get_other_eap_id
= _get_other_eap_id
,
1965 .enable_extension
= _enable_extension
,
1966 .supports_extension
= _supports_extension
,
1967 .set_condition
= _set_condition
,
1968 .has_condition
= _has_condition
,
1969 .set_pending_updates
= _set_pending_updates
,
1970 .get_pending_updates
= _get_pending_updates
,
1971 .create_additional_address_enumerator
= _create_additional_address_enumerator
,
1972 .add_additional_address
= _add_additional_address
,
1973 .remove_additional_addresses
= _remove_additional_addresses
,
1974 .has_mapping_changed
= _has_mapping_changed
,
1975 .retransmit
= _retransmit
,
1977 .destroy
= _destroy
,
1978 .send_dpd
= _send_dpd
,
1979 .send_keepalive
= _send_keepalive
,
1980 .get_keymat
= _get_keymat
,
1981 .add_child_sa
= _add_child_sa
,
1982 .get_child_sa
= _get_child_sa
,
1983 .get_child_count
= _get_child_count
,
1984 .create_child_sa_enumerator
= _create_child_sa_enumerator
,
1985 .remove_child_sa
= _remove_child_sa
,
1986 .rekey_child_sa
= _rekey_child_sa
,
1987 .delete_child_sa
= _delete_child_sa
,
1988 .destroy_child_sa
= _destroy_child_sa
,
1991 .reestablish
= _reestablish
,
1992 .set_auth_lifetime
= _set_auth_lifetime
,
1994 .inherit
= _inherit
,
1995 .generate_message
= _generate_message
,
1997 .get_unique_id
= _get_unique_id
,
1998 .set_virtual_ip
= _set_virtual_ip
,
1999 .get_virtual_ip
= _get_virtual_ip
,
2000 .add_configuration_attribute
= _add_configuration_attribute
,
2001 .set_kmaddress
= _set_kmaddress
,
2002 .create_task_enumerator
= _create_task_enumerator
,
2003 .queue_task
= _queue_task
,
2005 .act_as_mediation_server
= _act_as_mediation_server
,
2006 .get_server_reflexive_host
= _get_server_reflexive_host
,
2007 .set_server_reflexive_host
= _set_server_reflexive_host
,
2008 .get_connect_id
= _get_connect_id
,
2009 .initiate_mediation
= _initiate_mediation
,
2010 .initiate_mediated
= _initiate_mediated
,
2012 .callback
= _callback
,
2013 .respond
= _respond
,
2016 .ike_sa_id
= ike_sa_id
->clone(ike_sa_id
),
2018 .child_sas
= linked_list_create(),
2019 .my_host
= host_create_any(AF_INET
),
2020 .other_host
= host_create_any(AF_INET
),
2021 .my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
2022 .other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
2023 .keymat
= keymat_create(version
, initiator
),
2024 .state
= IKE_CREATED
,
2025 .stats
[STAT_INBOUND
] = time_monotonic(NULL
),
2026 .stats
[STAT_OUTBOUND
] = time_monotonic(NULL
),
2027 .my_auth
= auth_cfg_create(),
2028 .other_auth
= auth_cfg_create(),
2029 .my_auths
= linked_list_create(),
2030 .other_auths
= linked_list_create(),
2031 .unique_id
= ++unique_id
,
2032 .additional_addresses
= linked_list_create(),
2033 .attributes
= linked_list_create(),
2034 .keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2035 "charon.keep_alive", KEEPALIVE_INTERVAL
),
2036 .flush_auth_cfg
= lib
->settings
->get_bool(lib
->settings
,
2037 "charon.flush_auth_cfg", FALSE
),
2040 this->task_manager
= task_manager_create(&this->public);
2041 this->my_host
->set_port(this->my_host
, IKEV2_UDP_PORT
);
2043 if (!this->task_manager
|| !this->keymat
)
2045 DBG1(DBG_IKE
, "IKE version %d not supported", this->version
);
2049 return &this->public;