2 * Copyright (C) 2006-2008 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
28 #include <utils/linked_list.h>
29 #include <utils/lexparser.h>
30 #include <sa/task_manager.h>
31 #include <sa/tasks/ike_init.h>
32 #include <sa/tasks/ike_natd.h>
33 #include <sa/tasks/ike_mobike.h>
34 #include <sa/tasks/ike_auth.h>
35 #include <sa/tasks/ike_auth_lifetime.h>
36 #include <sa/tasks/ike_config.h>
37 #include <sa/tasks/ike_cert_pre.h>
38 #include <sa/tasks/ike_cert_post.h>
39 #include <sa/tasks/ike_rekey.h>
40 #include <sa/tasks/ike_reauth.h>
41 #include <sa/tasks/ike_delete.h>
42 #include <sa/tasks/ike_dpd.h>
43 #include <sa/tasks/child_create.h>
44 #include <sa/tasks/child_delete.h>
45 #include <sa/tasks/child_rekey.h>
46 #include <processing/jobs/retransmit_job.h>
47 #include <processing/jobs/delete_ike_sa_job.h>
48 #include <processing/jobs/send_dpd_job.h>
49 #include <processing/jobs/send_keepalive_job.h>
50 #include <processing/jobs/rekey_ike_sa_job.h>
53 #include <sa/tasks/ike_me.h>
54 #include <processing/jobs/initiate_mediation_job.h>
57 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DESTROYING
,
67 typedef struct private_ike_sa_t private_ike_sa_t
;
68 typedef struct attribute_entry_t attribute_entry_t
;
71 * Private data of an ike_sa_t object.
73 struct private_ike_sa_t
{
81 * Identifier for the current IKE_SA.
83 ike_sa_id_t
*ike_sa_id
;
86 * unique numerical ID for this IKE_SA.
91 * Current state of the IKE_SA
96 * IKE configuration used to set up this IKE_SA
101 * Peer and authentication information to establish IKE_SA.
103 peer_cfg_t
*peer_cfg
;
106 * currently used authentication ruleset, local (as auth_cfg_t)
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
239 * Entry to maintain install configuration attributes during IKE_SA lifetime
241 struct attribute_entry_t
{
242 /** handler used to install this attribute */
243 attribute_handler_t
*handler
;
244 /** attribute type */
245 configuration_attribute_type_t type
;
246 /** attribute data */
251 * get the time of the latest traffic processed by the kernel
253 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
255 enumerator_t
*enumerator
;
256 child_sa_t
*child_sa
;
257 time_t use_time
, current
;
261 use_time
= this->stats
[STAT_INBOUND
];
265 use_time
= this->stats
[STAT_OUTBOUND
];
267 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
268 while (enumerator
->enumerate(enumerator
, &child_sa
))
270 child_sa
->get_usestats(child_sa
, inbound
, ¤t
, NULL
);
271 use_time
= max(use_time
, current
);
273 enumerator
->destroy(enumerator
);
279 * Implementation of ike_sa_t.get_unique_id
281 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
283 return this->unique_id
;
287 * Implementation of ike_sa_t.get_name.
289 static char *get_name(private_ike_sa_t
*this)
293 return this->peer_cfg
->get_name(this->peer_cfg
);
299 * Implementation of ike_sa_t.get_statistic.
301 static u_int32_t
get_statistic(private_ike_sa_t
*this, statistic_t kind
)
305 return this->stats
[kind
];
311 * Implementation of ike_sa_t.get_my_host.
313 static host_t
*get_my_host(private_ike_sa_t
*this)
315 return this->my_host
;
319 * Implementation of ike_sa_t.set_my_host.
321 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
323 DESTROY_IF(this->my_host
);
328 * Implementation of ike_sa_t.get_other_host.
330 static host_t
*get_other_host(private_ike_sa_t
*this)
332 return this->other_host
;
336 * Implementation of ike_sa_t.set_other_host.
338 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
340 DESTROY_IF(this->other_host
);
341 this->other_host
= other
;
345 * Implementation of ike_sa_t.get_peer_cfg
347 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
349 return this->peer_cfg
;
353 * Implementation of ike_sa_t.set_peer_cfg
355 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
357 DESTROY_IF(this->peer_cfg
);
358 peer_cfg
->get_ref(peer_cfg
);
359 this->peer_cfg
= peer_cfg
;
361 if (this->ike_cfg
== NULL
)
363 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
364 this->ike_cfg
->get_ref(this->ike_cfg
);
369 * Implementation of ike_sa_t.get_auth_cfg
371 static auth_cfg_t
* get_auth_cfg(private_ike_sa_t
*this, bool local
)
375 return this->my_auth
;
377 return this->other_auth
;
381 * Implementation of ike_sa_t.get_proposal
383 static proposal_t
* get_proposal(private_ike_sa_t
*this)
385 return this->proposal
;
389 * Implementation of ike_sa_t.set_proposal
391 static void set_proposal(private_ike_sa_t
*this, proposal_t
*proposal
)
393 DESTROY_IF(this->proposal
);
394 this->proposal
= proposal
->clone(proposal
);
398 * Implementation of ike_sa_t.set_message_id
400 static void set_message_id(private_ike_sa_t
*this, bool initiate
, u_int32_t mid
)
404 this->task_manager
->reset(this->task_manager
, mid
, UINT_MAX
);
408 this->task_manager
->reset(this->task_manager
, UINT_MAX
, mid
);
413 * Implementation of ike_sa_t.send_keepalive
415 static void send_keepalive(private_ike_sa_t
*this)
417 send_keepalive_job_t
*job
;
418 time_t last_out
, now
, diff
;
420 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
421 { /* disable keep alives if we are not NATed anymore */
425 last_out
= get_use_time(this, FALSE
);
426 now
= time_monotonic(NULL
);
428 diff
= now
- last_out
;
430 if (diff
>= this->keepalive_interval
)
435 packet
= packet_create();
436 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
437 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
438 data
.ptr
= malloc(1);
441 packet
->set_data(packet
, data
);
442 DBG1(DBG_IKE
, "sending keep alive");
443 charon
->sender
->send(charon
->sender
, packet
);
446 job
= send_keepalive_job_create(this->ike_sa_id
);
447 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
448 this->keepalive_interval
- diff
);
452 * Implementation of ike_sa_t.get_ike_cfg
454 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
456 return this->ike_cfg
;
460 * Implementation of ike_sa_t.set_ike_cfg
462 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
464 ike_cfg
->get_ref(ike_cfg
);
465 this->ike_cfg
= ike_cfg
;
469 * Implementation of ike_sa_t.enable_extension.
471 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
473 this->extensions
|= extension
;
477 * Implementation of ike_sa_t.has_extension.
479 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
481 return (this->extensions
& extension
) != FALSE
;
485 * Implementation of ike_sa_t.has_condition.
487 static bool has_condition(private_ike_sa_t
*this, ike_condition_t condition
)
489 return (this->conditions
& condition
) != FALSE
;
493 * Implementation of ike_sa_t.enable_condition.
495 static void set_condition(private_ike_sa_t
*this, ike_condition_t condition
,
498 if (has_condition(this, condition
) != enable
)
502 this->conditions
|= condition
;
506 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
507 this->conditions
|= COND_NAT_ANY
;
508 send_keepalive(this);
511 DBG1(DBG_IKE
, "remote host is behind NAT");
512 this->conditions
|= COND_NAT_ANY
;
515 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
516 this->conditions
|= COND_NAT_ANY
;
524 this->conditions
&= ~condition
;
530 set_condition(this, COND_NAT_ANY
,
531 has_condition(this, COND_NAT_HERE
) ||
532 has_condition(this, COND_NAT_THERE
) ||
533 has_condition(this, COND_NAT_FAKE
));
543 * Implementation of ike_sa_t.send_dpd
545 static status_t
send_dpd(private_ike_sa_t
*this)
550 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
558 if (this->task_manager
->busy(this->task_manager
))
560 /* an exchange is in the air, no need to start a DPD check */
565 /* check if there was any inbound traffic */
567 last_in
= get_use_time(this, TRUE
);
568 now
= time_monotonic(NULL
);
569 diff
= now
- last_in
;
572 /* to long ago, initiate dead peer detection */
574 ike_mobike_t
*mobike
;
576 if (supports_extension(this, EXT_MOBIKE
) &&
577 has_condition(this, COND_NAT_HERE
))
579 /* use mobike enabled DPD to detect NAT mapping changes */
580 mobike
= ike_mobike_create(&this->public, TRUE
);
582 task
= &mobike
->task
;
586 task
= (task_t
*)ike_dpd_create(TRUE
);
589 DBG1(DBG_IKE
, "sending DPD request");
591 this->task_manager
->queue_task(this->task_manager
, task
);
592 this->task_manager
->initiate(this->task_manager
);
595 /* recheck in "interval" seconds */
596 job
= (job_t
*)send_dpd_job_create(this->ike_sa_id
);
597 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, delay
- diff
);
602 * Implementation of ike_sa_t.get_state.
604 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
610 * Implementation of ike_sa_t.set_state.
612 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
614 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
615 get_name(this), this->unique_id
,
616 ike_sa_state_names
, this->state
,
617 ike_sa_state_names
, state
);
621 case IKE_ESTABLISHED
:
623 if (this->state
== IKE_CONNECTING
||
624 this->state
== IKE_PASSIVE
)
629 /* calculate rekey, reauth and lifetime */
630 this->stats
[STAT_ESTABLISHED
] = time_monotonic(NULL
);
632 /* schedule rekeying if we have a time which is smaller than
633 * an already scheduled rekeying */
634 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
);
635 if (t
&& (this->stats
[STAT_REKEY
] == 0 ||
636 (this->stats
[STAT_REKEY
] > t
+ this->stats
[STAT_ESTABLISHED
])))
638 this->stats
[STAT_REKEY
] = t
+ this->stats
[STAT_ESTABLISHED
];
639 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
640 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, t
);
641 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
643 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
);
644 if (t
&& (this->stats
[STAT_REAUTH
] == 0 ||
645 (this->stats
[STAT_REAUTH
] > t
+ this->stats
[STAT_ESTABLISHED
])))
647 this->stats
[STAT_REAUTH
] = t
+ this->stats
[STAT_ESTABLISHED
];
648 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
649 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, t
);
650 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
652 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
653 if (this->stats
[STAT_REKEY
] || this->stats
[STAT_REAUTH
])
655 if (this->stats
[STAT_REAUTH
] == 0)
657 this->stats
[STAT_DELETE
] = this->stats
[STAT_REKEY
];
659 else if (this->stats
[STAT_REKEY
] == 0)
661 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
];
665 this->stats
[STAT_DELETE
] = min(this->stats
[STAT_REKEY
],
666 this->stats
[STAT_REAUTH
]);
668 this->stats
[STAT_DELETE
] += t
;
669 t
= this->stats
[STAT_DELETE
] - this->stats
[STAT_ESTABLISHED
];
670 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
671 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, t
);
672 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
675 /* start DPD checks */
682 /* delete may fail if a packet gets lost, so set a timeout */
683 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
684 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
685 HALF_OPEN_IKE_SA_TIMEOUT
);
691 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
696 * Implementation of ike_sa_t.reset
698 static void reset(private_ike_sa_t
*this)
700 /* the responder ID is reset, as peer may choose another one */
701 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
703 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
706 set_state(this, IKE_CREATED
);
708 this->task_manager
->reset(this->task_manager
, 0, 0);
712 * Implementation of ike_sa_t.get_keymat
714 static keymat_t
* get_keymat(private_ike_sa_t
*this)
720 * Implementation of ike_sa_t.set_virtual_ip
722 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
726 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
727 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
728 this->my_host
) == SUCCESS
)
730 if (this->my_virtual_ip
)
732 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
733 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
734 this->my_virtual_ip
);
736 DESTROY_IF(this->my_virtual_ip
);
737 this->my_virtual_ip
= ip
->clone(ip
);
741 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
742 this->my_virtual_ip
= NULL
;
747 DESTROY_IF(this->other_virtual_ip
);
748 this->other_virtual_ip
= ip
->clone(ip
);
753 * Implementation of ike_sa_t.get_virtual_ip
755 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
759 return this->my_virtual_ip
;
763 return this->other_virtual_ip
;
768 * Implementation of ike_sa_t.add_additional_address.
770 static void add_additional_address(private_ike_sa_t
*this, host_t
*host
)
772 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
776 * Implementation of ike_sa_t.create_additional_address_iterator.
778 static iterator_t
* create_additional_address_iterator(private_ike_sa_t
*this)
780 return this->additional_addresses
->create_iterator(
781 this->additional_addresses
, TRUE
);
785 * Implementation of ike_sa_t.has_mapping_changed
787 static bool has_mapping_changed(private_ike_sa_t
*this, chunk_t hash
)
789 if (this->nat_detection_dest
.ptr
== NULL
)
791 this->nat_detection_dest
= chunk_clone(hash
);
794 if (chunk_equals(hash
, this->nat_detection_dest
))
798 free(this->nat_detection_dest
.ptr
);
799 this->nat_detection_dest
= chunk_clone(hash
);
804 * Implementation of ike_sa_t.set_pending_updates.
806 static void set_pending_updates(private_ike_sa_t
*this, u_int32_t updates
)
808 this->pending_updates
= updates
;
812 * Implementation of ike_sa_t.get_pending_updates.
814 static u_int32_t
get_pending_updates(private_ike_sa_t
*this)
816 return this->pending_updates
;
820 * Update hosts, as addresses may change (NAT)
822 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
832 other
= this->other_host
;
835 /* apply hosts on first received message */
836 if (this->my_host
->is_anyaddr(this->my_host
) ||
837 this->other_host
->is_anyaddr(this->other_host
))
839 set_my_host(this, me
->clone(me
));
840 set_other_host(this, other
->clone(other
));
845 /* update our address in any case */
846 if (!me
->equals(me
, this->my_host
))
848 set_my_host(this, me
->clone(me
));
852 if (!other
->equals(other
, this->other_host
))
854 /* update others adress if we are NOT NATed,
855 * and allow port changes if we are NATed */
856 if (!has_condition(this, COND_NAT_HERE
) ||
857 other
->ip_equals(other
, this->other_host
))
859 set_other_host(this, other
->clone(other
));
865 /* update all associated CHILD_SAs, if required */
868 iterator_t
*iterator
;
869 child_sa_t
*child_sa
;
871 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
872 while (iterator
->iterate(iterator
, (void**)&child_sa
))
874 if (child_sa
->update(child_sa
, this->my_host
,
875 this->other_host
, this->my_virtual_ip
,
876 has_condition(this, COND_NAT_ANY
)) == NOT_SUPPORTED
)
878 this->public.rekey_child_sa(&this->public,
879 child_sa
->get_protocol(child_sa
),
880 child_sa
->get_spi(child_sa
, TRUE
));
883 iterator
->destroy(iterator
);
888 * Implementation of ike_sa_t.generate
890 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
893 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
894 message
->set_ike_sa_id(message
, this->ike_sa_id
);
895 return message
->generate(message
,
896 this->keymat
->get_crypter(this->keymat
, FALSE
),
897 this->keymat
->get_signer(this->keymat
, FALSE
), packet
);
901 * send a notify back to the sender
903 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
909 response
= message_create();
910 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
911 response
->set_request(response
, FALSE
);
912 response
->set_message_id(response
, request
->get_message_id(request
));
913 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
914 if (this->my_host
->is_anyaddr(this->my_host
))
916 this->my_host
->destroy(this->my_host
);
917 this->my_host
= request
->get_destination(request
);
918 this->my_host
= this->my_host
->clone(this->my_host
);
920 if (this->other_host
->is_anyaddr(this->other_host
))
922 this->other_host
->destroy(this->other_host
);
923 this->other_host
= request
->get_source(request
);
924 this->other_host
= this->other_host
->clone(this->other_host
);
926 response
->set_source(response
, this->my_host
->clone(this->my_host
));
927 response
->set_destination(response
, this->other_host
->clone(this->other_host
));
928 if (generate_message(this, response
, &packet
) == SUCCESS
)
930 charon
->sender
->send(charon
->sender
, packet
);
932 response
->destroy(response
);
936 * Implementation of ike_sa_t.set_kmaddress.
938 static void set_kmaddress(private_ike_sa_t
*this, host_t
*local
, host_t
*remote
)
940 DESTROY_IF(this->local_host
);
941 DESTROY_IF(this->remote_host
);
942 this->local_host
= local
->clone(local
);
943 this->remote_host
= remote
->clone(remote
);
948 * Implementation of ike_sa_t.act_as_mediation_server.
950 static void act_as_mediation_server(private_ike_sa_t
*this)
952 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
953 this->other_id
, this->ike_sa_id
);
954 this->is_mediation_server
= TRUE
;
958 * Implementation of ike_sa_t.get_server_reflexive_host.
960 static host_t
*get_server_reflexive_host(private_ike_sa_t
*this)
962 return this->server_reflexive_host
;
966 * Implementation of ike_sa_t.set_server_reflexive_host.
968 static void set_server_reflexive_host(private_ike_sa_t
*this, host_t
*host
)
970 DESTROY_IF(this->server_reflexive_host
);
971 this->server_reflexive_host
= host
;
975 * Implementation of ike_sa_t.get_connect_id.
977 static chunk_t
get_connect_id(private_ike_sa_t
*this)
979 return this->connect_id
;
983 * Implementation of ike_sa_t.respond
985 static status_t
respond(private_ike_sa_t
*this, identification_t
*peer_id
,
988 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
989 task
->respond(task
, peer_id
, connect_id
);
990 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
991 return this->task_manager
->initiate(this->task_manager
);
995 * Implementation of ike_sa_t.callback
997 static status_t
callback(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
);
1006 * Implementation of ike_sa_t.relay
1008 static status_t
relay(private_ike_sa_t
*this, identification_t
*requester
,
1009 chunk_t connect_id
, chunk_t connect_key
,
1010 linked_list_t
*endpoints
, bool response
)
1012 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1013 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1014 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1015 return this->task_manager
->initiate(this->task_manager
);
1019 * Implementation of ike_sa_t.initiate_mediation
1021 static status_t
initiate_mediation(private_ike_sa_t
*this,
1022 peer_cfg_t
*mediated_cfg
)
1024 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1025 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1026 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1027 return this->task_manager
->initiate(this->task_manager
);
1031 * Implementation of ike_sa_t.initiate_mediated
1033 static status_t
initiate_mediated(private_ike_sa_t
*this, host_t
*me
,
1034 host_t
*other
, chunk_t connect_id
)
1036 set_my_host(this, me
->clone(me
));
1037 set_other_host(this, other
->clone(other
));
1038 chunk_free(&this->connect_id
);
1039 this->connect_id
= chunk_clone(connect_id
);
1040 return this->task_manager
->initiate(this->task_manager
);
1045 * Resolve DNS host in configuration
1047 static void resolve_hosts(private_ike_sa_t
*this)
1051 if (this->remote_host
)
1053 host
= this->remote_host
->clone(this->remote_host
);
1054 host
->set_port(host
, IKEV2_UDP_PORT
);
1058 host
= host_create_from_dns(this->ike_cfg
->get_other_addr(this->ike_cfg
),
1063 set_other_host(this, host
);
1066 if (this->local_host
)
1068 host
= this->local_host
->clone(this->local_host
);
1069 host
->set_port(host
, IKEV2_UDP_PORT
);
1075 /* use same address family as for other */
1076 if (!this->other_host
->is_anyaddr(this->other_host
))
1078 family
= this->other_host
->get_family(this->other_host
);
1080 host
= host_create_from_dns(this->ike_cfg
->get_my_addr(this->ike_cfg
),
1081 family
, IKEV2_UDP_PORT
);
1083 if (host
&& host
->is_anyaddr(host
) &&
1084 !this->other_host
->is_anyaddr(this->other_host
))
1086 host
->destroy(host
);
1087 host
= charon
->kernel_interface
->get_source_addr(
1088 charon
->kernel_interface
, this->other_host
, NULL
);
1091 host
->set_port(host
, IKEV2_UDP_PORT
);
1094 { /* fallback to address family specific %any(6), if configured */
1095 host
= host_create_from_dns(
1096 this->ike_cfg
->get_my_addr(this->ike_cfg
),
1103 set_my_host(this, host
);
1108 * Implementation of ike_sa_t.initiate
1110 static status_t
initiate(private_ike_sa_t
*this,
1111 child_cfg_t
*child_cfg
, u_int32_t reqid
,
1112 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1116 if (this->state
== IKE_CREATED
)
1118 resolve_hosts(this);
1120 if (this->other_host
->is_anyaddr(this->other_host
)
1122 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1126 child_cfg
->destroy(child_cfg
);
1127 DBG1(DBG_IKE
, "unable to initiate to %%any");
1131 set_condition(this, COND_ORIGINAL_INITIATOR
, TRUE
);
1133 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1134 this->task_manager
->queue_task(this->task_manager
, task
);
1135 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1136 this->task_manager
->queue_task(this->task_manager
, task
);
1137 task
= (task_t
*)ike_cert_pre_create(&this->public, TRUE
);
1138 this->task_manager
->queue_task(this->task_manager
, task
);
1139 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1140 this->task_manager
->queue_task(this->task_manager
, task
);
1141 task
= (task_t
*)ike_cert_post_create(&this->public, TRUE
);
1142 this->task_manager
->queue_task(this->task_manager
, task
);
1143 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1144 this->task_manager
->queue_task(this->task_manager
, task
);
1145 task
= (task_t
*)ike_auth_lifetime_create(&this->public, TRUE
);
1146 this->task_manager
->queue_task(this->task_manager
, task
);
1147 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1149 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1150 this->task_manager
->queue_task(this->task_manager
, task
);
1153 task
= (task_t
*)ike_me_create(&this->public, TRUE
);
1154 this->task_manager
->queue_task(this->task_manager
, task
);
1159 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1161 if (this->state
== IKE_ESTABLISHED
)
1163 /* mediation connection is already established, retrigger state
1164 * change to notify bus listeners */
1165 DBG1(DBG_IKE
, "mediation connection is already up");
1166 set_state(this, IKE_ESTABLISHED
);
1168 DESTROY_IF(child_cfg
);
1173 /* normal IKE_SA with CHILD_SA */
1174 task
= (task_t
*)child_create_create(&this->public, child_cfg
, FALSE
,
1176 child_cfg
->destroy(child_cfg
);
1179 child_create_t
*child_create
= (child_create_t
*)task
;
1180 child_create
->use_reqid(child_create
, reqid
);
1182 this->task_manager
->queue_task(this->task_manager
, task
);
1185 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1187 /* mediated connection, initiate mediation process */
1188 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1189 charon
->processor
->queue_job(charon
->processor
, job
);
1195 return this->task_manager
->initiate(this->task_manager
);
1199 * Implementation of ike_sa_t.process_message.
1201 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
1206 if (this->state
== IKE_PASSIVE
)
1207 { /* do not handle messages in passive state */
1211 is_request
= message
->get_request(message
);
1213 status
= message
->parse_body(message
,
1214 this->keymat
->get_crypter(this->keymat
, TRUE
),
1215 this->keymat
->get_signer(this->keymat
, TRUE
));
1216 if (status
!= SUCCESS
)
1224 DBG1(DBG_IKE
, "critical unknown payloads found");
1227 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
1231 DBG1(DBG_IKE
, "message parsing failed");
1234 send_notify_response(this, message
, INVALID_SYNTAX
);
1238 DBG1(DBG_IKE
, "message verification failed");
1241 send_notify_response(this, message
, INVALID_SYNTAX
);
1245 DBG1(DBG_IKE
, "integrity check failed");
1249 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1252 send_notify_response(this, message
, INVALID_SYNTAX
);
1258 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1259 exchange_type_names
, message
->get_exchange_type(message
),
1260 message
->get_request(message
) ?
"request" : "response",
1261 message
->get_message_id(message
));
1263 if (this->state
== IKE_CREATED
)
1264 { /* invalid initiation attempt, close SA */
1273 me
= message
->get_destination(message
);
1274 other
= message
->get_source(message
);
1276 /* if this IKE_SA is virgin, we check for a config */
1277 if (this->ike_cfg
== NULL
)
1280 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1282 if (this->ike_cfg
== NULL
)
1284 /* no config found for these hosts, destroy */
1285 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1286 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1287 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
1290 /* add a timeout if peer does not establish it completely */
1291 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
1292 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
1293 HALF_OPEN_IKE_SA_TIMEOUT
);
1295 this->stats
[STAT_INBOUND
] = time_monotonic(NULL
);
1296 /* check if message is trustworthy, and update host information */
1297 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
1298 message
->get_exchange_type(message
) != IKE_SA_INIT
)
1300 if (!supports_extension(this, EXT_MOBIKE
))
1301 { /* with MOBIKE, we do no implicit updates */
1302 update_hosts(this, me
, other
);
1305 return this->task_manager
->process_message(this->task_manager
, message
);
1310 * Implementation of ike_sa_t.get_id.
1312 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1314 return this->ike_sa_id
;
1318 * Implementation of ike_sa_t.get_my_id.
1320 static identification_t
* get_my_id(private_ike_sa_t
*this)
1326 * Implementation of ike_sa_t.set_my_id.
1328 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1330 DESTROY_IF(this->my_id
);
1335 * Implementation of ike_sa_t.get_other_id.
1337 static identification_t
* get_other_id(private_ike_sa_t
*this)
1339 return this->other_id
;
1343 * Implementation of ike_sa_t.set_other_id.
1345 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1347 DESTROY_IF(this->other_id
);
1348 this->other_id
= other
;
1352 * Implementation of ike_sa_t.add_child_sa.
1354 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1356 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1360 * Implementation of ike_sa_t.get_child_sa.
1362 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1363 u_int32_t spi
, bool inbound
)
1365 iterator_t
*iterator
;
1366 child_sa_t
*current
, *found
= NULL
;
1368 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1369 while (iterator
->iterate(iterator
, (void**)¤t
))
1371 if (current
->get_spi(current
, inbound
) == spi
&&
1372 current
->get_protocol(current
) == protocol
)
1377 iterator
->destroy(iterator
);
1382 * Implementation of ike_sa_t.create_child_sa_iterator.
1384 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1386 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1390 * Implementation of ike_sa_t.rekey_child_sa.
1392 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1395 child_rekey_t
*child_rekey
;
1397 child_rekey
= child_rekey_create(&this->public, protocol
, spi
);
1398 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1399 return this->task_manager
->initiate(this->task_manager
);
1403 * Implementation of ike_sa_t.delete_child_sa.
1405 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1408 child_delete_t
*child_delete
;
1410 child_delete
= child_delete_create(&this->public, protocol
, spi
);
1411 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1412 return this->task_manager
->initiate(this->task_manager
);
1416 * Implementation of ike_sa_t.destroy_child_sa.
1418 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1421 iterator_t
*iterator
;
1422 child_sa_t
*child_sa
;
1423 status_t status
= NOT_FOUND
;
1425 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1426 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1428 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1429 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1431 child_sa
->destroy(child_sa
);
1432 iterator
->remove(iterator
);
1437 iterator
->destroy(iterator
);
1442 * Implementation of public_ike_sa_t.delete.
1444 static status_t
delete_(private_ike_sa_t
*this)
1446 ike_delete_t
*ike_delete
;
1448 switch (this->state
)
1450 case IKE_ESTABLISHED
:
1452 ike_delete
= ike_delete_create(&this->public, TRUE
);
1453 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1454 return this->task_manager
->initiate(this->task_manager
);
1456 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1461 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1462 "without notification", ike_sa_state_names
, this->state
);
1469 * Implementation of ike_sa_t.rekey.
1471 static status_t
rekey(private_ike_sa_t
*this)
1473 ike_rekey_t
*ike_rekey
;
1475 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1477 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1478 return this->task_manager
->initiate(this->task_manager
);
1482 * Implementation of ike_sa_t.reauth
1484 static status_t
reauth(private_ike_sa_t
*this)
1488 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1489 * If the peer does not support RFC4478, there is no way to keep the
1491 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1493 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1494 if (this->other_virtual_ip
!= NULL
||
1495 has_condition(this, COND_EAP_AUTHENTICATED
)
1497 /* as mediation server we too cannot reauth the IKE_SA */
1498 || this->is_mediation_server
1502 time_t now
= time_monotonic(NULL
);
1504 DBG1(DBG_IKE
, "IKE_SA will timeout in %V",
1505 &now
, &this->stats
[STAT_DELETE
]);
1510 DBG1(DBG_IKE
, "reauthenticating actively");
1513 task
= (task_t
*)ike_reauth_create(&this->public);
1514 this->task_manager
->queue_task(this->task_manager
, task
);
1516 return this->task_manager
->initiate(this->task_manager
);
1520 * Implementation of ike_sa_t.reestablish
1522 static status_t
reestablish(private_ike_sa_t
*this)
1527 iterator_t
*iterator
;
1528 child_sa_t
*child_sa
;
1529 child_cfg_t
*child_cfg
;
1530 bool restart
= FALSE
;
1531 status_t status
= FAILED
;
1533 /* check if we have children to keep up at all */
1534 iterator
= create_child_sa_iterator(this);
1535 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1537 child_cfg
= child_sa
->get_config(child_sa
);
1538 if (this->state
== IKE_DELETING
)
1540 action
= child_cfg
->get_close_action(child_cfg
);
1544 action
= child_cfg
->get_dpd_action(child_cfg
);
1548 case ACTION_RESTART
:
1552 charon
->traps
->install(charon
->traps
, this->peer_cfg
, child_cfg
);
1558 iterator
->destroy(iterator
);
1560 /* mediation connections have no children, keep them up anyway */
1561 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1571 /* check if we are able to reestablish this IKE_SA */
1572 if (!has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1573 (this->other_virtual_ip
!= NULL
||
1574 has_condition(this, COND_EAP_AUTHENTICATED
)
1576 || this->is_mediation_server
1580 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due asymetric setup");
1584 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
, TRUE
);
1585 new->set_peer_cfg(new, this->peer_cfg
);
1586 host
= this->other_host
;
1587 new->set_other_host(new, host
->clone(host
));
1588 host
= this->my_host
;
1589 new->set_my_host(new, host
->clone(host
));
1590 /* if we already have a virtual IP, we reuse it */
1591 host
= this->my_virtual_ip
;
1594 new->set_virtual_ip(new, TRUE
, host
);
1598 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1600 status
= new->initiate(new, NULL
, 0, NULL
, NULL
);
1605 iterator
= create_child_sa_iterator(this);
1606 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1608 child_cfg
= child_sa
->get_config(child_sa
);
1609 if (this->state
== IKE_DELETING
)
1611 action
= child_cfg
->get_close_action(child_cfg
);
1615 action
= child_cfg
->get_dpd_action(child_cfg
);
1619 case ACTION_RESTART
:
1620 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
1621 child_cfg
->get_name(child_cfg
));
1622 child_cfg
->get_ref(child_cfg
);
1623 status
= new->initiate(new, child_cfg
, 0, NULL
, NULL
);
1628 if (status
== DESTROY_ME
)
1633 iterator
->destroy(iterator
);
1636 if (status
== DESTROY_ME
)
1638 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1643 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1646 charon
->bus
->set_sa(charon
->bus
, &this->public);
1651 * Implementation of ike_sa_t.retransmit.
1653 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
1655 this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
1656 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1658 /* send a proper signal to brief interested bus listeners */
1659 switch (this->state
)
1661 case IKE_CONNECTING
:
1663 /* retry IKE_SA_INIT if we have multiple keyingtries */
1664 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1666 if (tries
== 0 || tries
> this->keyingtry
)
1668 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
1669 this->keyingtry
+ 1, tries
);
1671 return this->task_manager
->initiate(this->task_manager
);
1673 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
1677 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
1680 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
1692 * Implementation of ike_sa_t.set_auth_lifetime.
1694 static void set_auth_lifetime(private_ike_sa_t
*this, u_int32_t lifetime
)
1696 u_int32_t reduction
= this->peer_cfg
->get_over_time(this->peer_cfg
);
1697 u_int32_t reauth_time
= time_monotonic(NULL
) + lifetime
- reduction
;
1699 if (lifetime
< reduction
)
1701 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, starting reauthentication",
1703 charon
->processor
->queue_job(charon
->processor
,
1704 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
1706 else if (this->stats
[STAT_REAUTH
] == 0 ||
1707 this->stats
[STAT_REAUTH
] > reauth_time
)
1709 this->stats
[STAT_REAUTH
] = reauth_time
;
1710 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1711 " in %ds", lifetime
, lifetime
- reduction
);
1712 charon
->scheduler
->schedule_job(charon
->scheduler
,
1713 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
1714 lifetime
- reduction
);
1718 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, "
1719 "reauthentication already scheduled in %ds", lifetime
,
1720 this->stats
[STAT_REAUTH
] - time_monotonic(NULL
));
1725 * Implementation of ike_sa_t.roam.
1727 static status_t
roam(private_ike_sa_t
*this, bool address
)
1730 ike_mobike_t
*mobike
;
1732 switch (this->state
)
1736 case IKE_DESTROYING
:
1742 /* responder just updates the peer about changed address config */
1743 if (!this->ike_sa_id
->is_initiator(this->ike_sa_id
))
1745 if (supports_extension(this, EXT_MOBIKE
) && address
)
1747 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1748 mobike
= ike_mobike_create(&this->public, TRUE
);
1749 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1750 return this->task_manager
->initiate(this->task_manager
);
1755 /* keep existing path if possible */
1756 src
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
1757 this->other_host
, this->my_host
);
1760 if (src
->ip_equals(src
, this->my_host
))
1762 DBG2(DBG_IKE
, "keeping connection path %H - %H",
1763 src
, this->other_host
);
1765 set_condition(this, COND_STALE
, FALSE
);
1773 /* check if we find a route at all */
1774 enumerator_t
*enumerator
;
1777 src
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
1778 this->other_host
, NULL
);
1781 enumerator
= this->additional_addresses
->create_enumerator(
1782 this->additional_addresses
);
1783 while (enumerator
->enumerate(enumerator
, &addr
))
1785 DBG1(DBG_IKE
, "looking for a route to %H ...", addr
);
1786 src
= charon
->kernel_interface
->get_source_addr(
1787 charon
->kernel_interface
, addr
, NULL
);
1793 enumerator
->destroy(enumerator
);
1797 DBG1(DBG_IKE
, "no route found to reach %H, MOBIKE update deferred",
1799 set_condition(this, COND_STALE
, TRUE
);
1804 set_condition(this, COND_STALE
, FALSE
);
1806 /* update addresses with mobike, if supported ... */
1807 if (supports_extension(this, EXT_MOBIKE
))
1809 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
1810 mobike
= ike_mobike_create(&this->public, TRUE
);
1811 mobike
->roam(mobike
, address
);
1812 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1813 return this->task_manager
->initiate(this->task_manager
);
1815 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
1816 /* ... reauth if not */
1817 return reauth(this);
1821 * Implementation of ike_sa_t.add_configuration_attribute
1823 static void add_configuration_attribute(private_ike_sa_t
*this,
1824 attribute_handler_t
*handler
,
1825 configuration_attribute_type_t type
, chunk_t data
)
1827 attribute_entry_t
*entry
= malloc_thing(attribute_entry_t
);
1829 entry
->handler
= handler
;
1831 entry
->data
= chunk_clone(data
);
1833 this->attributes
->insert_last(this->attributes
, entry
);
1837 * Implementation of ike_sa_t.inherit.
1839 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1841 child_sa_t
*child_sa
;
1842 attribute_entry_t
*entry
;
1844 /* apply hosts and ids */
1845 this->my_host
->destroy(this->my_host
);
1846 this->other_host
->destroy(this->other_host
);
1847 this->my_id
->destroy(this->my_id
);
1848 this->other_id
->destroy(this->other_id
);
1849 this->my_host
= other
->my_host
->clone(other
->my_host
);
1850 this->other_host
= other
->other_host
->clone(other
->other_host
);
1851 this->my_id
= other
->my_id
->clone(other
->my_id
);
1852 this->other_id
= other
->other_id
->clone(other
->other_id
);
1854 /* apply virtual assigned IPs... */
1855 if (other
->my_virtual_ip
)
1857 this->my_virtual_ip
= other
->my_virtual_ip
;
1858 other
->my_virtual_ip
= NULL
;
1860 if (other
->other_virtual_ip
)
1862 this->other_virtual_ip
= other
->other_virtual_ip
;
1863 other
->other_virtual_ip
= NULL
;
1866 /* ... and configuration attributes */
1867 while (other
->attributes
->remove_last(other
->attributes
,
1868 (void**)&entry
) == SUCCESS
)
1870 this->attributes
->insert_first(this->attributes
, entry
);
1873 /* inherit all conditions */
1874 this->conditions
= other
->conditions
;
1875 if (this->conditions
& COND_NAT_HERE
)
1877 send_keepalive(this);
1881 if (other
->is_mediation_server
)
1883 act_as_mediation_server(this);
1885 else if (other
->server_reflexive_host
)
1887 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
1888 other
->server_reflexive_host
);
1892 /* adopt all children */
1893 while (other
->child_sas
->remove_last(other
->child_sas
,
1894 (void**)&child_sa
) == SUCCESS
)
1896 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
1899 /* move pending tasks to the new IKE_SA */
1900 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
1902 /* reauthentication timeout survives a rekeying */
1903 if (other
->stats
[STAT_REAUTH
])
1905 time_t reauth
, delete, now
= time_monotonic(NULL
);
1907 this->stats
[STAT_REAUTH
] = other
->stats
[STAT_REAUTH
];
1908 reauth
= this->stats
[STAT_REAUTH
] - now
;
1909 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
1910 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
] + delete;
1911 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
1912 "lifetime reduced to %ds", reauth
, delete);
1913 charon
->scheduler
->schedule_job(charon
->scheduler
,
1914 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
), reauth
);
1915 charon
->scheduler
->schedule_job(charon
->scheduler
,
1916 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
), delete);
1918 /* we have to initate here, there may be new tasks to handle */
1919 return this->task_manager
->initiate(this->task_manager
);
1923 * Implementation of ike_sa_t.destroy.
1925 static void destroy(private_ike_sa_t
*this)
1927 attribute_entry_t
*entry
;
1929 charon
->bus
->set_sa(charon
->bus
, &this->public);
1931 set_state(this, IKE_DESTROYING
);
1933 /* remove attributes first, as we pass the IKE_SA to the handler */
1934 while (this->attributes
->remove_last(this->attributes
,
1935 (void**)&entry
) == SUCCESS
)
1937 lib
->attributes
->release(lib
->attributes
, entry
->handler
,
1938 this->other_id
, entry
->type
, entry
->data
);
1939 free(entry
->data
.ptr
);
1942 this->attributes
->destroy(this->attributes
);
1944 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
1946 /* unset SA after here to avoid usage by the listeners */
1947 charon
->bus
->set_sa(charon
->bus
, NULL
);
1949 this->task_manager
->destroy(this->task_manager
);
1950 this->keymat
->destroy(this->keymat
);
1952 if (this->my_virtual_ip
)
1954 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
1955 this->my_virtual_ip
);
1956 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
1958 if (this->other_virtual_ip
)
1960 if (this->peer_cfg
&& this->peer_cfg
->get_pool(this->peer_cfg
))
1962 lib
->attributes
->release_address(lib
->attributes
,
1963 this->peer_cfg
->get_pool(this->peer_cfg
),
1964 this->other_virtual_ip
, this->other_id
);
1966 this->other_virtual_ip
->destroy(this->other_virtual_ip
);
1968 this->additional_addresses
->destroy_offset(this->additional_addresses
,
1969 offsetof(host_t
, destroy
));
1971 if (this->is_mediation_server
)
1973 charon
->mediation_manager
->remove(charon
->mediation_manager
,
1976 DESTROY_IF(this->server_reflexive_host
);
1977 chunk_free(&this->connect_id
);
1979 free(this->nat_detection_dest
.ptr
);
1981 DESTROY_IF(this->my_host
);
1982 DESTROY_IF(this->other_host
);
1983 DESTROY_IF(this->my_id
);
1984 DESTROY_IF(this->other_id
);
1985 DESTROY_IF(this->local_host
);
1986 DESTROY_IF(this->remote_host
);
1988 DESTROY_IF(this->ike_cfg
);
1989 DESTROY_IF(this->peer_cfg
);
1990 DESTROY_IF(this->proposal
);
1991 this->my_auth
->destroy(this->my_auth
);
1992 this->other_auth
->destroy(this->other_auth
);
1994 this->ike_sa_id
->destroy(this->ike_sa_id
);
1999 * Described in header.
2001 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2003 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2004 static u_int32_t unique_id
= 0;
2006 /* Public functions */
2007 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
2008 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2009 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
2010 this->public.get_statistic
= (u_int32_t(*)(ike_sa_t
*, statistic_t kind
))get_statistic
;
2011 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
2012 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*,u_int32_t
,traffic_selector_t
*,traffic_selector_t
*)) initiate
;
2013 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
2014 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
2015 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
2016 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
2017 this->public.get_auth_cfg
= (auth_cfg_t
*(*)(ike_sa_t
*, bool local
))get_auth_cfg
;
2018 this->public.get_proposal
= (proposal_t
*(*)(ike_sa_t
*))get_proposal
;
2019 this->public.set_proposal
= (void(*)(ike_sa_t
*, proposal_t
*proposal
))set_proposal
;
2020 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
2021 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
2022 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
2023 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
2024 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
2025 this->public.set_message_id
= (void(*)(ike_sa_t
*, bool inbound
, u_int32_t mid
))set_message_id
;
2026 this->public.update_hosts
= (void(*)(ike_sa_t
*, host_t
*me
, host_t
*other
))update_hosts
;
2027 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
2028 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2029 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
2030 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2031 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
2032 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
2033 this->public.set_condition
= (void (*)(ike_sa_t
*, ike_condition_t
,bool)) set_condition
;
2034 this->public.has_condition
= (bool (*)(ike_sa_t
*,ike_condition_t
)) has_condition
;
2035 this->public.set_pending_updates
= (void(*)(ike_sa_t
*, u_int32_t updates
))set_pending_updates
;
2036 this->public.get_pending_updates
= (u_int32_t(*)(ike_sa_t
*))get_pending_updates
;
2037 this->public.create_additional_address_iterator
= (iterator_t
*(*)(ike_sa_t
*))create_additional_address_iterator
;
2038 this->public.add_additional_address
= (void(*)(ike_sa_t
*, host_t
*host
))add_additional_address
;
2039 this->public.has_mapping_changed
= (bool(*)(ike_sa_t
*, chunk_t hash
))has_mapping_changed
;
2040 this->public.retransmit
= (status_t (*)(ike_sa_t
*, u_int32_t
)) retransmit
;
2041 this->public.delete = (status_t (*)(ike_sa_t
*))delete_
;
2042 this->public.destroy
= (void (*)(ike_sa_t
*))destroy
;
2043 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
2044 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
2045 this->public.get_keymat
= (keymat_t
*(*)(ike_sa_t
*))get_keymat
;
2046 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2047 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2048 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2049 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2050 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2051 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2052 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
2053 this->public.reauth
= (status_t (*)(ike_sa_t
*))reauth
;
2054 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
2055 this->public.set_auth_lifetime
= (void(*)(ike_sa_t
*, u_int32_t lifetime
))set_auth_lifetime
;
2056 this->public.roam
= (status_t(*)(ike_sa_t
*,bool))roam
;
2057 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
2058 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
2059 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
2060 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
2061 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
2062 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
2063 this->public.add_configuration_attribute
= (void(*)(ike_sa_t
*, attribute_handler_t
*handler
,configuration_attribute_type_t type
, chunk_t data
))add_configuration_attribute
;
2064 this->public.set_kmaddress
= (void (*)(ike_sa_t
*,host_t
*,host_t
*))set_kmaddress
;
2066 this->public.act_as_mediation_server
= (void (*)(ike_sa_t
*)) act_as_mediation_server
;
2067 this->public.get_server_reflexive_host
= (host_t
* (*)(ike_sa_t
*)) get_server_reflexive_host
;
2068 this->public.set_server_reflexive_host
= (void (*)(ike_sa_t
*,host_t
*)) set_server_reflexive_host
;
2069 this->public.get_connect_id
= (chunk_t (*)(ike_sa_t
*)) get_connect_id
;
2070 this->public.initiate_mediation
= (status_t (*)(ike_sa_t
*,peer_cfg_t
*)) initiate_mediation
;
2071 this->public.initiate_mediated
= (status_t (*)(ike_sa_t
*,host_t
*,host_t
*,chunk_t
)) initiate_mediated
;
2072 this->public.relay
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
,chunk_t
,linked_list_t
*,bool)) relay
;
2073 this->public.callback
= (status_t (*)(ike_sa_t
*,identification_t
*)) callback
;
2074 this->public.respond
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
)) respond
;
2077 /* initialize private fields */
2078 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2079 this->child_sas
= linked_list_create();
2080 this->my_host
= host_create_any(AF_INET
);
2081 this->my_host
->set_port(this->my_host
, IKEV2_UDP_PORT
);
2082 this->other_host
= host_create_any(AF_INET
);
2083 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2084 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2085 this->extensions
= 0;
2086 this->conditions
= 0;
2087 this->keymat
= keymat_create(ike_sa_id
->is_initiator(ike_sa_id
));
2088 this->state
= IKE_CREATED
;
2089 this->keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2090 "charon.keep_alive", KEEPALIVE_INTERVAL
);
2091 memset(this->stats
, 0, sizeof(this->stats
));
2092 this->stats
[STAT_INBOUND
] = this->stats
[STAT_OUTBOUND
] = time_monotonic(NULL
);
2093 this->ike_cfg
= NULL
;
2094 this->peer_cfg
= NULL
;
2095 this->my_auth
= auth_cfg_create();
2096 this->other_auth
= auth_cfg_create();
2097 this->proposal
= NULL
;
2098 this->task_manager
= task_manager_create(&this->public);
2099 this->unique_id
= ++unique_id
;
2100 this->my_virtual_ip
= NULL
;
2101 this->other_virtual_ip
= NULL
;
2102 this->additional_addresses
= linked_list_create();
2103 this->attributes
= linked_list_create();
2104 this->nat_detection_dest
= chunk_empty
;
2105 this->pending_updates
= 0;
2106 this->keyingtry
= 0;
2107 this->local_host
= NULL
;
2108 this->remote_host
= NULL
;
2110 this->is_mediation_server
= FALSE
;
2111 this->server_reflexive_host
= NULL
;
2112 this->connect_id
= chunk_empty
;
2115 return &this->public;