Moved IKE_SA delete task creation to protocol specific task manager
[strongswan.git] / src / libcharon / sa / ike_sa.c
1 /*
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
7 *
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>.
12 *
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
16 * for more details.
17 */
18
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <errno.h>
22 #include <time.h>
23
24 #include "ike_sa.h"
25
26 #include <library.h>
27 #include <hydra.h>
28 #include <daemon.h>
29 #include <utils/linked_list.h>
30 #include <utils/lexparser.h>
31 #include <sa/ikev2/tasks/ike_init.h>
32 #include <sa/ikev2/tasks/ike_natd.h>
33 #include <sa/ikev2/tasks/ike_mobike.h>
34 #include <sa/ikev2/tasks/ike_auth.h>
35 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
36 #include <sa/ikev2/tasks/ike_config.h>
37 #include <sa/ikev2/tasks/ike_cert_pre.h>
38 #include <sa/ikev2/tasks/ike_cert_post.h>
39 #include <sa/ikev2/tasks/ike_rekey.h>
40 #include <sa/ikev2/tasks/ike_reauth.h>
41 #include <sa/ikev2/tasks/ike_delete.h>
42 #include <sa/ikev2/tasks/ike_dpd.h>
43 #include <sa/ikev2/tasks/ike_vendor.h>
44 #include <sa/ikev2/tasks/child_create.h>
45 #include <sa/ikev2/tasks/child_delete.h>
46 #include <sa/ikev2/tasks/child_rekey.h>
47 #include <sa/ikev1/tasks/main_mode.h>
48 #include <sa/ikev1/tasks/isakmp_cert_pre.h>
49 #include <sa/ikev1/tasks/isakmp_cert_post.h>
50 #include <sa/ikev1/tasks/isakmp_natd.h>
51 #include <sa/ikev1/tasks/quick_mode.h>
52 #include <sa/ikev1/tasks/quick_delete.h>
53 #include <sa/ikev1/tasks/isakmp_vendor.h>
54 #include <sa/ikev1/tasks/isakmp_delete.h>
55 #include <processing/jobs/retransmit_job.h>
56 #include <processing/jobs/delete_ike_sa_job.h>
57 #include <processing/jobs/send_dpd_job.h>
58 #include <processing/jobs/send_keepalive_job.h>
59 #include <processing/jobs/rekey_ike_sa_job.h>
60
61 #ifdef ME
62 #include <sa/ikev2/tasks/ike_me.h>
63 #include <processing/jobs/initiate_mediation_job.h>
64 #endif
65
66 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DESTROYING,
67 "CREATED",
68 "CONNECTING",
69 "ESTABLISHED",
70 "PASSIVE",
71 "REKEYING",
72 "DELETING",
73 "DESTROYING",
74 );
75
76 typedef struct private_ike_sa_t private_ike_sa_t;
77 typedef struct attribute_entry_t attribute_entry_t;
78
79 /**
80 * Private data of an ike_sa_t object.
81 */
82 struct private_ike_sa_t {
83
84 /**
85 * Public members
86 */
87 ike_sa_t public;
88
89 /**
90 * Identifier for the current IKE_SA.
91 */
92 ike_sa_id_t *ike_sa_id;
93
94 /**
95 * IKE version of this SA.
96 */
97 ike_version_t version;
98
99 /**
100 * unique numerical ID for this IKE_SA.
101 */
102 u_int32_t unique_id;
103
104 /**
105 * Current state of the IKE_SA
106 */
107 ike_sa_state_t state;
108
109 /**
110 * IKE configuration used to set up this IKE_SA
111 */
112 ike_cfg_t *ike_cfg;
113
114 /**
115 * Peer and authentication information to establish IKE_SA.
116 */
117 peer_cfg_t *peer_cfg;
118
119 /**
120 * currently used authentication ruleset, local (as auth_cfg_t)
121 */
122 auth_cfg_t *my_auth;
123
124 /**
125 * list of completed local authentication rounds
126 */
127 linked_list_t *my_auths;
128
129 /**
130 * list of completed remote authentication rounds
131 */
132 linked_list_t *other_auths;
133
134 /**
135 * currently used authentication constraints, remote (as auth_cfg_t)
136 */
137 auth_cfg_t *other_auth;
138
139 /**
140 * Selected IKE proposal
141 */
142 proposal_t *proposal;
143
144 /**
145 * Juggles tasks to process messages
146 */
147 task_manager_t *task_manager;
148
149 /**
150 * Address of local host
151 */
152 host_t *my_host;
153
154 /**
155 * Address of remote host
156 */
157 host_t *other_host;
158
159 #ifdef ME
160 /**
161 * Are we mediation server
162 */
163 bool is_mediation_server;
164
165 /**
166 * Server reflexive host
167 */
168 host_t *server_reflexive_host;
169
170 /**
171 * Connect ID
172 */
173 chunk_t connect_id;
174 #endif /* ME */
175
176 /**
177 * Identification used for us
178 */
179 identification_t *my_id;
180
181 /**
182 * Identification used for other
183 */
184 identification_t *other_id;
185
186 /**
187 * set of extensions the peer supports
188 */
189 ike_extension_t extensions;
190
191 /**
192 * set of condition flags currently enabled for this IKE_SA
193 */
194 ike_condition_t conditions;
195
196 /**
197 * Linked List containing the child sa's of the current IKE_SA.
198 */
199 linked_list_t *child_sas;
200
201 /**
202 * keymat of this IKE_SA
203 */
204 keymat_t *keymat;
205
206 /**
207 * Virtual IP on local host, if any
208 */
209 host_t *my_virtual_ip;
210
211 /**
212 * Virtual IP on remote host, if any
213 */
214 host_t *other_virtual_ip;
215
216 /**
217 * List of configuration attributes (attribute_entry_t)
218 */
219 linked_list_t *attributes;
220
221 /**
222 * list of peers additional addresses, transmitted via MOBIKE
223 */
224 linked_list_t *additional_addresses;
225
226 /**
227 * previously value of received DESTINATION_IP hash
228 */
229 chunk_t nat_detection_dest;
230
231 /**
232 * number pending UPDATE_SA_ADDRESS (MOBIKE)
233 */
234 u_int32_t pending_updates;
235
236 /**
237 * NAT keep alive interval
238 */
239 u_int32_t keepalive_interval;
240
241 /**
242 * Timestamps for this IKE_SA
243 */
244 u_int32_t stats[STAT_MAX];
245
246 /**
247 * how many times we have retried so far (keyingtries)
248 */
249 u_int32_t keyingtry;
250
251 /**
252 * local host address to be used for IKE, set via MIGRATE kernel message
253 */
254 host_t *local_host;
255
256 /**
257 * remote host address to be used for IKE, set via MIGRATE kernel message
258 */
259 host_t *remote_host;
260
261 /**
262 * Flush auth configs once established?
263 */
264 bool flush_auth_cfg;
265 };
266
267 /**
268 * Entry to maintain install configuration attributes during IKE_SA lifetime
269 */
270 struct attribute_entry_t {
271 /** handler used to install this attribute */
272 attribute_handler_t *handler;
273 /** attribute type */
274 configuration_attribute_type_t type;
275 /** attribute data */
276 chunk_t data;
277 };
278
279 /**
280 * get the time of the latest traffic processed by the kernel
281 */
282 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
283 {
284 enumerator_t *enumerator;
285 child_sa_t *child_sa;
286 time_t use_time, current;
287
288 if (inbound)
289 {
290 use_time = this->stats[STAT_INBOUND];
291 }
292 else
293 {
294 use_time = this->stats[STAT_OUTBOUND];
295 }
296 enumerator = this->child_sas->create_enumerator(this->child_sas);
297 while (enumerator->enumerate(enumerator, &child_sa))
298 {
299 child_sa->get_usestats(child_sa, inbound, &current, NULL);
300 use_time = max(use_time, current);
301 }
302 enumerator->destroy(enumerator);
303
304 return use_time;
305 }
306
307 METHOD(ike_sa_t, get_unique_id, u_int32_t,
308 private_ike_sa_t *this)
309 {
310 return this->unique_id;
311 }
312
313 METHOD(ike_sa_t, get_name, char*,
314 private_ike_sa_t *this)
315 {
316 if (this->peer_cfg)
317 {
318 return this->peer_cfg->get_name(this->peer_cfg);
319 }
320 return "(unnamed)";
321 }
322
323 METHOD(ike_sa_t, get_statistic, u_int32_t,
324 private_ike_sa_t *this, statistic_t kind)
325 {
326 if (kind < STAT_MAX)
327 {
328 return this->stats[kind];
329 }
330 return 0;
331 }
332
333 METHOD(ike_sa_t, set_statistic, void,
334 private_ike_sa_t *this, statistic_t kind, u_int32_t value)
335 {
336 if (kind < STAT_MAX)
337 {
338 this->stats[kind] = value;
339 }
340 }
341
342 METHOD(ike_sa_t, get_my_host, host_t*,
343 private_ike_sa_t *this)
344 {
345 return this->my_host;
346 }
347
348 METHOD(ike_sa_t, set_my_host, void,
349 private_ike_sa_t *this, host_t *me)
350 {
351 DESTROY_IF(this->my_host);
352 this->my_host = me;
353 }
354
355 METHOD(ike_sa_t, get_other_host, host_t*,
356 private_ike_sa_t *this)
357 {
358 return this->other_host;
359 }
360
361 METHOD(ike_sa_t, set_other_host, void,
362 private_ike_sa_t *this, host_t *other)
363 {
364 DESTROY_IF(this->other_host);
365 this->other_host = other;
366 }
367
368 METHOD(ike_sa_t, get_peer_cfg, peer_cfg_t*,
369 private_ike_sa_t *this)
370 {
371 return this->peer_cfg;
372 }
373
374 METHOD(ike_sa_t, set_peer_cfg, void,
375 private_ike_sa_t *this, peer_cfg_t *peer_cfg)
376 {
377 peer_cfg->get_ref(peer_cfg);
378 DESTROY_IF(this->peer_cfg);
379 this->peer_cfg = peer_cfg;
380
381 if (this->ike_cfg == NULL)
382 {
383 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
384 this->ike_cfg->get_ref(this->ike_cfg);
385 }
386 }
387
388 METHOD(ike_sa_t, get_auth_cfg, auth_cfg_t*,
389 private_ike_sa_t *this, bool local)
390 {
391 if (local)
392 {
393 return this->my_auth;
394 }
395 return this->other_auth;
396 }
397
398 METHOD(ike_sa_t, add_auth_cfg, void,
399 private_ike_sa_t *this, bool local, auth_cfg_t *cfg)
400 {
401 if (local)
402 {
403 this->my_auths->insert_last(this->my_auths, cfg);
404 }
405 else
406 {
407 this->other_auths->insert_last(this->other_auths, cfg);
408 }
409 }
410
411 METHOD(ike_sa_t, create_auth_cfg_enumerator, enumerator_t*,
412 private_ike_sa_t *this, bool local)
413 {
414 if (local)
415 {
416 return this->my_auths->create_enumerator(this->my_auths);
417 }
418 return this->other_auths->create_enumerator(this->other_auths);
419 }
420
421 /**
422 * Flush the stored authentication round information
423 */
424 static void flush_auth_cfgs(private_ike_sa_t *this)
425 {
426 auth_cfg_t *cfg;
427
428 this->my_auth->purge(this->my_auth, FALSE);
429 this->other_auth->purge(this->other_auth, FALSE);
430
431 while (this->my_auths->remove_last(this->my_auths,
432 (void**)&cfg) == SUCCESS)
433 {
434 cfg->destroy(cfg);
435 }
436 while (this->other_auths->remove_last(this->other_auths,
437 (void**)&cfg) == SUCCESS)
438 {
439 cfg->destroy(cfg);
440 }
441 }
442
443 METHOD(ike_sa_t, get_proposal, proposal_t*,
444 private_ike_sa_t *this)
445 {
446 return this->proposal;
447 }
448
449 METHOD(ike_sa_t, set_proposal, void,
450 private_ike_sa_t *this, proposal_t *proposal)
451 {
452 DESTROY_IF(this->proposal);
453 this->proposal = proposal->clone(proposal);
454 }
455
456 METHOD(ike_sa_t, set_message_id, void,
457 private_ike_sa_t *this, bool initiate, u_int32_t mid)
458 {
459 if (initiate)
460 {
461 this->task_manager->reset(this->task_manager, mid, UINT_MAX);
462 }
463 else
464 {
465 this->task_manager->reset(this->task_manager, UINT_MAX, mid);
466 }
467 }
468
469 METHOD(ike_sa_t, send_keepalive, void,
470 private_ike_sa_t *this)
471 {
472 send_keepalive_job_t *job;
473 time_t last_out, now, diff;
474
475 if (!(this->conditions & COND_NAT_HERE) || this->keepalive_interval == 0)
476 { /* disable keep alives if we are not NATed anymore */
477 return;
478 }
479
480 last_out = get_use_time(this, FALSE);
481 now = time_monotonic(NULL);
482
483 diff = now - last_out;
484
485 if (diff >= this->keepalive_interval)
486 {
487 packet_t *packet;
488 chunk_t data;
489
490 packet = packet_create();
491 packet->set_source(packet, this->my_host->clone(this->my_host));
492 packet->set_destination(packet, this->other_host->clone(this->other_host));
493 data.ptr = malloc(1);
494 data.ptr[0] = 0xFF;
495 data.len = 1;
496 packet->set_data(packet, data);
497 DBG1(DBG_IKE, "sending keep alive");
498 charon->sender->send(charon->sender, packet);
499 diff = 0;
500 }
501 job = send_keepalive_job_create(this->ike_sa_id);
502 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
503 this->keepalive_interval - diff);
504 }
505
506 METHOD(ike_sa_t, get_ike_cfg, ike_cfg_t*,
507 private_ike_sa_t *this)
508 {
509 return this->ike_cfg;
510 }
511
512 METHOD(ike_sa_t, set_ike_cfg, void,
513 private_ike_sa_t *this, ike_cfg_t *ike_cfg)
514 {
515 ike_cfg->get_ref(ike_cfg);
516 this->ike_cfg = ike_cfg;
517 }
518
519 METHOD(ike_sa_t, enable_extension, void,
520 private_ike_sa_t *this, ike_extension_t extension)
521 {
522 this->extensions |= extension;
523 }
524
525 METHOD(ike_sa_t, supports_extension, bool,
526 private_ike_sa_t *this, ike_extension_t extension)
527 {
528 return (this->extensions & extension) != FALSE;
529 }
530
531 METHOD(ike_sa_t, has_condition, bool,
532 private_ike_sa_t *this, ike_condition_t condition)
533 {
534 return (this->conditions & condition) != FALSE;
535 }
536
537 METHOD(ike_sa_t, set_condition, void,
538 private_ike_sa_t *this, ike_condition_t condition, bool enable)
539 {
540 if (has_condition(this, condition) != enable)
541 {
542 if (enable)
543 {
544 this->conditions |= condition;
545 switch (condition)
546 {
547 case COND_NAT_HERE:
548 DBG1(DBG_IKE, "local host is behind NAT, sending keep alives");
549 this->conditions |= COND_NAT_ANY;
550 send_keepalive(this);
551 break;
552 case COND_NAT_THERE:
553 DBG1(DBG_IKE, "remote host is behind NAT");
554 this->conditions |= COND_NAT_ANY;
555 break;
556 case COND_NAT_FAKE:
557 DBG1(DBG_IKE, "faking NAT situation to enforce UDP encapsulation");
558 this->conditions |= COND_NAT_ANY;
559 break;
560 default:
561 break;
562 }
563 }
564 else
565 {
566 this->conditions &= ~condition;
567 switch (condition)
568 {
569 case COND_NAT_HERE:
570 case COND_NAT_FAKE:
571 case COND_NAT_THERE:
572 set_condition(this, COND_NAT_ANY,
573 has_condition(this, COND_NAT_HERE) ||
574 has_condition(this, COND_NAT_THERE) ||
575 has_condition(this, COND_NAT_FAKE));
576 break;
577 default:
578 break;
579 }
580 }
581 }
582 }
583
584 METHOD(ike_sa_t, send_dpd, status_t,
585 private_ike_sa_t *this)
586 {
587 job_t *job;
588 time_t diff, delay;
589
590 delay = this->peer_cfg->get_dpd(this->peer_cfg);
591 if (this->task_manager->busy(this->task_manager))
592 {
593 /* an exchange is in the air, no need to start a DPD check */
594 diff = 0;
595 }
596 else
597 {
598 /* check if there was any inbound traffic */
599 time_t last_in, now;
600 last_in = get_use_time(this, TRUE);
601 now = time_monotonic(NULL);
602 diff = now - last_in;
603 if (!delay || diff >= delay)
604 {
605 /* to long ago, initiate dead peer detection */
606 DBG1(DBG_IKE, "sending DPD request");
607 this->task_manager->queue_dpd(this->task_manager);
608 diff = 0;
609 }
610 }
611 /* recheck in "interval" seconds */
612 if (delay)
613 {
614 job = (job_t*)send_dpd_job_create(this->ike_sa_id);
615 lib->scheduler->schedule_job(lib->scheduler, job, delay - diff);
616 }
617 return this->task_manager->initiate(this->task_manager);
618 }
619
620 METHOD(ike_sa_t, get_state, ike_sa_state_t,
621 private_ike_sa_t *this)
622 {
623 return this->state;
624 }
625
626 METHOD(ike_sa_t, set_state, void,
627 private_ike_sa_t *this, ike_sa_state_t state)
628 {
629 DBG2(DBG_IKE, "IKE_SA %s[%d] state change: %N => %N",
630 get_name(this), this->unique_id,
631 ike_sa_state_names, this->state,
632 ike_sa_state_names, state);
633
634 switch (state)
635 {
636 case IKE_ESTABLISHED:
637 {
638 if (this->state == IKE_CONNECTING ||
639 this->state == IKE_PASSIVE)
640 {
641 job_t *job;
642 u_int32_t t;
643
644 /* calculate rekey, reauth and lifetime */
645 this->stats[STAT_ESTABLISHED] = time_monotonic(NULL);
646
647 /* schedule rekeying if we have a time which is smaller than
648 * an already scheduled rekeying */
649 t = this->peer_cfg->get_rekey_time(this->peer_cfg, TRUE);
650 if (t && (this->stats[STAT_REKEY] == 0 ||
651 (this->stats[STAT_REKEY] > t + this->stats[STAT_ESTABLISHED])))
652 {
653 this->stats[STAT_REKEY] = t + this->stats[STAT_ESTABLISHED];
654 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, FALSE);
655 lib->scheduler->schedule_job(lib->scheduler, job, t);
656 DBG1(DBG_IKE, "scheduling rekeying in %ds", t);
657 }
658 t = this->peer_cfg->get_reauth_time(this->peer_cfg, TRUE);
659 if (t && (this->stats[STAT_REAUTH] == 0 ||
660 (this->stats[STAT_REAUTH] > t + this->stats[STAT_ESTABLISHED])))
661 {
662 this->stats[STAT_REAUTH] = t + this->stats[STAT_ESTABLISHED];
663 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE);
664 lib->scheduler->schedule_job(lib->scheduler, job, t);
665 DBG1(DBG_IKE, "scheduling reauthentication in %ds", t);
666 }
667 t = this->peer_cfg->get_over_time(this->peer_cfg);
668 if (this->stats[STAT_REKEY] || this->stats[STAT_REAUTH])
669 {
670 if (this->stats[STAT_REAUTH] == 0)
671 {
672 this->stats[STAT_DELETE] = this->stats[STAT_REKEY];
673 }
674 else if (this->stats[STAT_REKEY] == 0)
675 {
676 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH];
677 }
678 else
679 {
680 this->stats[STAT_DELETE] = min(this->stats[STAT_REKEY],
681 this->stats[STAT_REAUTH]);
682 }
683 this->stats[STAT_DELETE] += t;
684 t = this->stats[STAT_DELETE] - this->stats[STAT_ESTABLISHED];
685 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
686 lib->scheduler->schedule_job(lib->scheduler, job, t);
687 DBG1(DBG_IKE, "maximum IKE_SA lifetime %ds", t);
688 }
689
690 /* start DPD checks */
691 if (this->peer_cfg->get_dpd(this->peer_cfg))
692 {
693 send_dpd(this);
694 }
695 }
696 break;
697 }
698 case IKE_DELETING:
699 {
700 /* delete may fail if a packet gets lost, so set a timeout */
701 job_t *job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
702 lib->scheduler->schedule_job(lib->scheduler, job,
703 HALF_OPEN_IKE_SA_TIMEOUT);
704 break;
705 }
706 default:
707 break;
708 }
709 charon->bus->ike_state_change(charon->bus, &this->public, state);
710 this->state = state;
711 }
712
713 METHOD(ike_sa_t, reset, void,
714 private_ike_sa_t *this)
715 {
716 /* the responder ID is reset, as peer may choose another one */
717 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
718 {
719 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
720 }
721
722 set_state(this, IKE_CREATED);
723
724 flush_auth_cfgs(this);
725
726 this->keymat->destroy(this->keymat);
727 this->keymat = keymat_create(this->version,
728 this->ike_sa_id->is_initiator(this->ike_sa_id));
729
730 this->task_manager->reset(this->task_manager, 0, 0);
731 }
732
733 METHOD(ike_sa_t, get_keymat, keymat_t*,
734 private_ike_sa_t *this)
735 {
736 return this->keymat;
737 }
738
739 METHOD(ike_sa_t, set_virtual_ip, void,
740 private_ike_sa_t *this, bool local, host_t *ip)
741 {
742 if (local)
743 {
744 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
745 if (hydra->kernel_interface->add_ip(hydra->kernel_interface, ip,
746 this->my_host) == SUCCESS)
747 {
748 if (this->my_virtual_ip)
749 {
750 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
751 hydra->kernel_interface->del_ip(hydra->kernel_interface,
752 this->my_virtual_ip);
753 }
754 DESTROY_IF(this->my_virtual_ip);
755 this->my_virtual_ip = ip->clone(ip);
756 }
757 else
758 {
759 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
760 this->my_virtual_ip = NULL;
761 }
762 }
763 else
764 {
765 DESTROY_IF(this->other_virtual_ip);
766 this->other_virtual_ip = ip->clone(ip);
767 }
768 }
769
770 METHOD(ike_sa_t, get_virtual_ip, host_t*,
771 private_ike_sa_t *this, bool local)
772 {
773 if (local)
774 {
775 return this->my_virtual_ip;
776 }
777 else
778 {
779 return this->other_virtual_ip;
780 }
781 }
782
783 METHOD(ike_sa_t, add_additional_address, void,
784 private_ike_sa_t *this, host_t *host)
785 {
786 this->additional_addresses->insert_last(this->additional_addresses, host);
787 }
788
789 METHOD(ike_sa_t, create_additional_address_enumerator, enumerator_t*,
790 private_ike_sa_t *this)
791 {
792 return this->additional_addresses->create_enumerator(
793 this->additional_addresses);
794 }
795
796 METHOD(ike_sa_t, remove_additional_addresses, void,
797 private_ike_sa_t *this)
798 {
799 enumerator_t *enumerator = create_additional_address_enumerator(this);
800 host_t *host;
801 while (enumerator->enumerate(enumerator, (void**)&host))
802 {
803 this->additional_addresses->remove_at(this->additional_addresses,
804 enumerator);
805 host->destroy(host);
806 }
807 enumerator->destroy(enumerator);
808 }
809
810 METHOD(ike_sa_t, has_mapping_changed, bool,
811 private_ike_sa_t *this, chunk_t hash)
812 {
813 if (this->nat_detection_dest.ptr == NULL)
814 {
815 this->nat_detection_dest = chunk_clone(hash);
816 return FALSE;
817 }
818 if (chunk_equals(hash, this->nat_detection_dest))
819 {
820 return FALSE;
821 }
822 free(this->nat_detection_dest.ptr);
823 this->nat_detection_dest = chunk_clone(hash);
824 return TRUE;
825 }
826
827 METHOD(ike_sa_t, set_pending_updates, void,
828 private_ike_sa_t *this, u_int32_t updates)
829 {
830 this->pending_updates = updates;
831 }
832
833 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
834 private_ike_sa_t *this)
835 {
836 return this->pending_updates;
837 }
838
839 METHOD(ike_sa_t, float_ports, void,
840 private_ike_sa_t *this)
841 {
842 /* do not switch if we have a custom port from MOBIKE/NAT */
843 if (this->my_host->get_port(this->my_host) == IKEV2_UDP_PORT)
844 {
845 this->my_host->set_port(this->my_host, IKEV2_NATT_PORT);
846 }
847 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
848 {
849 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
850 }
851 }
852
853 METHOD(ike_sa_t, update_hosts, void,
854 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
855 {
856 bool update = FALSE;
857
858 if (me == NULL)
859 {
860 me = this->my_host;
861 }
862 if (other == NULL)
863 {
864 other = this->other_host;
865 }
866
867 /* apply hosts on first received message */
868 if (this->my_host->is_anyaddr(this->my_host) ||
869 this->other_host->is_anyaddr(this->other_host))
870 {
871 set_my_host(this, me->clone(me));
872 set_other_host(this, other->clone(other));
873 update = TRUE;
874 }
875 else
876 {
877 /* update our address in any case */
878 if (!me->equals(me, this->my_host))
879 {
880 set_my_host(this, me->clone(me));
881 update = TRUE;
882 }
883
884 if (!other->equals(other, this->other_host))
885 {
886 /* update others address if we are NOT NATed */
887 if (force || !has_condition(this, COND_NAT_HERE))
888 {
889 set_other_host(this, other->clone(other));
890 update = TRUE;
891 }
892 }
893 }
894
895 /* update all associated CHILD_SAs, if required */
896 if (update)
897 {
898 enumerator_t *enumerator;
899 child_sa_t *child_sa;
900
901 enumerator = this->child_sas->create_enumerator(this->child_sas);
902 while (enumerator->enumerate(enumerator, (void**)&child_sa))
903 {
904 if (child_sa->update(child_sa, this->my_host,
905 this->other_host, this->my_virtual_ip,
906 has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
907 {
908 this->public.rekey_child_sa(&this->public,
909 child_sa->get_protocol(child_sa),
910 child_sa->get_spi(child_sa, TRUE));
911 }
912 }
913 enumerator->destroy(enumerator);
914 }
915 }
916
917 METHOD(ike_sa_t, generate_message, status_t,
918 private_ike_sa_t *this, message_t *message, packet_t **packet)
919 {
920 if (message->is_encoded(message))
921 { /* already done */
922 *packet = message->get_packet(message);
923 return SUCCESS;
924 }
925 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
926 message->set_ike_sa_id(message, this->ike_sa_id);
927 charon->bus->message(charon->bus, message, FALSE);
928 return message->generate(message, this->keymat, packet);
929 }
930
931 METHOD(ike_sa_t, set_kmaddress, void,
932 private_ike_sa_t *this, host_t *local, host_t *remote)
933 {
934 DESTROY_IF(this->local_host);
935 DESTROY_IF(this->remote_host);
936 this->local_host = local->clone(local);
937 this->remote_host = remote->clone(remote);
938 }
939
940 #ifdef ME
941 METHOD(ike_sa_t, act_as_mediation_server, void,
942 private_ike_sa_t *this)
943 {
944 charon->mediation_manager->update_sa_id(charon->mediation_manager,
945 this->other_id, this->ike_sa_id);
946 this->is_mediation_server = TRUE;
947 }
948
949 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
950 private_ike_sa_t *this)
951 {
952 return this->server_reflexive_host;
953 }
954
955 METHOD(ike_sa_t, set_server_reflexive_host, void,
956 private_ike_sa_t *this, host_t *host)
957 {
958 DESTROY_IF(this->server_reflexive_host);
959 this->server_reflexive_host = host;
960 }
961
962 METHOD(ike_sa_t, get_connect_id, chunk_t,
963 private_ike_sa_t *this)
964 {
965 return this->connect_id;
966 }
967
968 METHOD(ike_sa_t, respond, status_t,
969 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
970 {
971 ike_me_t *task = ike_me_create(&this->public, TRUE);
972 task->respond(task, peer_id, connect_id);
973 this->task_manager->queue_task(this->task_manager, (task_t*)task);
974 return this->task_manager->initiate(this->task_manager);
975 }
976
977 METHOD(ike_sa_t, callback, status_t,
978 private_ike_sa_t *this, identification_t *peer_id)
979 {
980 ike_me_t *task = ike_me_create(&this->public, TRUE);
981 task->callback(task, peer_id);
982 this->task_manager->queue_task(this->task_manager, (task_t*)task);
983 return this->task_manager->initiate(this->task_manager);
984 }
985
986 METHOD(ike_sa_t, relay, status_t,
987 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
988 chunk_t connect_key, linked_list_t *endpoints, bool response)
989 {
990 ike_me_t *task = ike_me_create(&this->public, TRUE);
991 task->relay(task, requester, connect_id, connect_key, endpoints, response);
992 this->task_manager->queue_task(this->task_manager, (task_t*)task);
993 return this->task_manager->initiate(this->task_manager);
994 }
995
996 METHOD(ike_sa_t, initiate_mediation, status_t,
997 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
998 {
999 ike_me_t *task = ike_me_create(&this->public, TRUE);
1000 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1001 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1002 return this->task_manager->initiate(this->task_manager);
1003 }
1004
1005 METHOD(ike_sa_t, initiate_mediated, status_t,
1006 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1007 {
1008 set_my_host(this, me->clone(me));
1009 set_other_host(this, other->clone(other));
1010 chunk_free(&this->connect_id);
1011 this->connect_id = chunk_clone(connect_id);
1012 return this->task_manager->initiate(this->task_manager);
1013 }
1014 #endif /* ME */
1015
1016 /**
1017 * Resolve DNS host in configuration
1018 */
1019 static void resolve_hosts(private_ike_sa_t *this)
1020 {
1021 host_t *host;
1022
1023 if (this->remote_host)
1024 {
1025 host = this->remote_host->clone(this->remote_host);
1026 host->set_port(host, IKEV2_UDP_PORT);
1027 }
1028 else
1029 {
1030 host = host_create_from_dns(this->ike_cfg->get_other_addr(this->ike_cfg),
1031 0, this->ike_cfg->get_other_port(this->ike_cfg));
1032 }
1033 if (host)
1034 {
1035 set_other_host(this, host);
1036 }
1037
1038 if (this->local_host)
1039 {
1040 host = this->local_host->clone(this->local_host);
1041 host->set_port(host, IKEV2_UDP_PORT);
1042 }
1043 else
1044 {
1045 int family = 0;
1046
1047 /* use same address family as for other */
1048 if (!this->other_host->is_anyaddr(this->other_host))
1049 {
1050 family = this->other_host->get_family(this->other_host);
1051 }
1052 host = host_create_from_dns(this->ike_cfg->get_my_addr(this->ike_cfg),
1053 family, this->ike_cfg->get_my_port(this->ike_cfg));
1054
1055 if (host && host->is_anyaddr(host) &&
1056 !this->other_host->is_anyaddr(this->other_host))
1057 {
1058 host->destroy(host);
1059 host = hydra->kernel_interface->get_source_addr(
1060 hydra->kernel_interface, this->other_host, NULL);
1061 if (host)
1062 {
1063 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1064 }
1065 else
1066 { /* fallback to address family specific %any(6), if configured */
1067 host = host_create_from_dns(
1068 this->ike_cfg->get_my_addr(this->ike_cfg),
1069 0, this->ike_cfg->get_my_port(this->ike_cfg));
1070 }
1071 }
1072 }
1073 if (host)
1074 {
1075 set_my_host(this, host);
1076 }
1077 }
1078
1079 METHOD(ike_sa_t, initiate, status_t,
1080 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1081 traffic_selector_t *tsi, traffic_selector_t *tsr)
1082 {
1083 if (this->state == IKE_CREATED)
1084 {
1085 resolve_hosts(this);
1086
1087 if (this->other_host->is_anyaddr(this->other_host)
1088 #ifdef ME
1089 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1090 #endif /* ME */
1091 )
1092 {
1093 child_cfg->destroy(child_cfg);
1094 DBG1(DBG_IKE, "unable to initiate to %%any");
1095 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1096 return DESTROY_ME;
1097 }
1098
1099 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1100 this->task_manager->queue_ike(this->task_manager);
1101 }
1102
1103 #ifdef ME
1104 if (this->peer_cfg->is_mediation(this->peer_cfg))
1105 {
1106 if (this->state == IKE_ESTABLISHED)
1107 {
1108 /* mediation connection is already established, retrigger state
1109 * change to notify bus listeners */
1110 DBG1(DBG_IKE, "mediation connection is already up");
1111 set_state(this, IKE_ESTABLISHED);
1112 }
1113 DESTROY_IF(child_cfg);
1114 }
1115 else
1116 #endif /* ME */
1117 {
1118 /* normal IKE_SA with CHILD_SA */
1119 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1120 tsi, tsr);
1121 #ifdef ME
1122 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1123 {
1124 /* mediated connection, initiate mediation process */
1125 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1126 lib->processor->queue_job(lib->processor, job);
1127 return SUCCESS;
1128 }
1129 #endif /* ME */
1130 }
1131
1132 return this->task_manager->initiate(this->task_manager);
1133 }
1134
1135 METHOD(ike_sa_t, process_message, status_t,
1136 private_ike_sa_t *this, message_t *message)
1137 {
1138 status_t status;
1139
1140 if (this->state == IKE_PASSIVE)
1141 { /* do not handle messages in passive state */
1142 return FAILED;
1143 }
1144 status = this->task_manager->process_message(this->task_manager, message);
1145 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1146 {
1147 /* authentication completed */
1148 this->flush_auth_cfg = FALSE;
1149 flush_auth_cfgs(this);
1150 }
1151 return status;
1152 }
1153
1154 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1155 private_ike_sa_t *this)
1156 {
1157 return this->ike_sa_id;
1158 }
1159
1160 METHOD(ike_sa_t, get_version, ike_version_t,
1161 private_ike_sa_t *this)
1162 {
1163 return this->version;
1164 }
1165
1166 METHOD(ike_sa_t, get_my_id, identification_t*,
1167 private_ike_sa_t *this)
1168 {
1169 return this->my_id;
1170 }
1171
1172 METHOD(ike_sa_t, set_my_id, void,
1173 private_ike_sa_t *this, identification_t *me)
1174 {
1175 DESTROY_IF(this->my_id);
1176 this->my_id = me;
1177 }
1178
1179 METHOD(ike_sa_t, get_other_id, identification_t*,
1180 private_ike_sa_t *this)
1181 {
1182 return this->other_id;
1183 }
1184
1185 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1186 private_ike_sa_t *this)
1187 {
1188 identification_t *id = NULL, *current;
1189 enumerator_t *enumerator;
1190 auth_cfg_t *cfg;
1191
1192 enumerator = this->other_auths->create_enumerator(this->other_auths);
1193 while (enumerator->enumerate(enumerator, &cfg))
1194 {
1195 /* prefer EAP-Identity of last round */
1196 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1197 if (!current || current->get_type(current) == ID_ANY)
1198 {
1199 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1200 }
1201 if (current && current->get_type(current) != ID_ANY)
1202 {
1203 id = current;
1204 continue;
1205 }
1206 }
1207 enumerator->destroy(enumerator);
1208 if (id)
1209 {
1210 return id;
1211 }
1212 return this->other_id;
1213 }
1214
1215 METHOD(ike_sa_t, set_other_id, void,
1216 private_ike_sa_t *this, identification_t *other)
1217 {
1218 DESTROY_IF(this->other_id);
1219 this->other_id = other;
1220 }
1221
1222 METHOD(ike_sa_t, add_child_sa, void,
1223 private_ike_sa_t *this, child_sa_t *child_sa)
1224 {
1225 this->child_sas->insert_last(this->child_sas, child_sa);
1226 }
1227
1228 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1229 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1230 {
1231 enumerator_t *enumerator;
1232 child_sa_t *current, *found = NULL;
1233
1234 enumerator = this->child_sas->create_enumerator(this->child_sas);
1235 while (enumerator->enumerate(enumerator, (void**)&current))
1236 {
1237 if (current->get_spi(current, inbound) == spi &&
1238 current->get_protocol(current) == protocol)
1239 {
1240 found = current;
1241 }
1242 }
1243 enumerator->destroy(enumerator);
1244 return found;
1245 }
1246
1247 METHOD(ike_sa_t, get_child_count, int,
1248 private_ike_sa_t *this)
1249 {
1250 return this->child_sas->get_count(this->child_sas);
1251 }
1252
1253 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1254 private_ike_sa_t *this)
1255 {
1256 return this->child_sas->create_enumerator(this->child_sas);
1257 }
1258
1259 METHOD(ike_sa_t, remove_child_sa, void,
1260 private_ike_sa_t *this, enumerator_t *enumerator)
1261 {
1262 this->child_sas->remove_at(this->child_sas, enumerator);
1263 }
1264
1265 METHOD(ike_sa_t, rekey_child_sa, status_t,
1266 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1267 {
1268 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1269 return this->task_manager->initiate(this->task_manager);
1270 }
1271
1272 METHOD(ike_sa_t, delete_child_sa, status_t,
1273 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1274 {
1275 this->task_manager->queue_child_delete(this->task_manager, protocol, spi);
1276 return this->task_manager->initiate(this->task_manager);
1277 }
1278
1279 METHOD(ike_sa_t, destroy_child_sa, status_t,
1280 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1281 {
1282 enumerator_t *enumerator;
1283 child_sa_t *child_sa;
1284 status_t status = NOT_FOUND;
1285
1286 enumerator = this->child_sas->create_enumerator(this->child_sas);
1287 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1288 {
1289 if (child_sa->get_protocol(child_sa) == protocol &&
1290 child_sa->get_spi(child_sa, TRUE) == spi)
1291 {
1292 this->child_sas->remove_at(this->child_sas, enumerator);
1293 child_sa->destroy(child_sa);
1294 status = SUCCESS;
1295 break;
1296 }
1297 }
1298 enumerator->destroy(enumerator);
1299 return status;
1300 }
1301
1302 METHOD(ike_sa_t, delete_, status_t,
1303 private_ike_sa_t *this)
1304 {
1305 switch (this->state)
1306 {
1307 case IKE_ESTABLISHED:
1308 case IKE_REKEYING:
1309 this->task_manager->queue_ike_delete(this->task_manager);
1310 return this->task_manager->initiate(this->task_manager);
1311 case IKE_CREATED:
1312 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1313 break;
1314 case IKE_PASSIVE:
1315 break;
1316 default:
1317 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1318 "without notification", ike_sa_state_names, this->state);
1319 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1320 break;
1321 }
1322 return DESTROY_ME;
1323 }
1324
1325 METHOD(ike_sa_t, rekey, status_t,
1326 private_ike_sa_t *this)
1327 {
1328 ike_rekey_t *ike_rekey;
1329
1330 ike_rekey = ike_rekey_create(&this->public, TRUE);
1331
1332 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1333 return this->task_manager->initiate(this->task_manager);
1334 }
1335
1336 METHOD(ike_sa_t, reauth, status_t,
1337 private_ike_sa_t *this)
1338 {
1339 task_t *task;
1340
1341 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1342 * If the peer does not support RFC4478, there is no way to keep the
1343 * IKE_SA up. */
1344 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1345 {
1346 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1347 if (this->other_virtual_ip != NULL ||
1348 has_condition(this, COND_EAP_AUTHENTICATED)
1349 #ifdef ME
1350 /* as mediation server we too cannot reauth the IKE_SA */
1351 || this->is_mediation_server
1352 #endif /* ME */
1353 )
1354 {
1355 time_t now = time_monotonic(NULL);
1356
1357 DBG1(DBG_IKE, "IKE_SA will timeout in %V",
1358 &now, &this->stats[STAT_DELETE]);
1359 return FAILED;
1360 }
1361 else
1362 {
1363 DBG1(DBG_IKE, "reauthenticating actively");
1364 }
1365 }
1366 task = (task_t*)ike_reauth_create(&this->public);
1367 this->task_manager->queue_task(this->task_manager, task);
1368
1369 return this->task_manager->initiate(this->task_manager);
1370 }
1371
1372 METHOD(ike_sa_t, reestablish, status_t,
1373 private_ike_sa_t *this)
1374 {
1375 ike_sa_t *new;
1376 host_t *host;
1377 action_t action;
1378 enumerator_t *enumerator;
1379 child_sa_t *child_sa;
1380 child_cfg_t *child_cfg;
1381 bool restart = FALSE;
1382 status_t status = FAILED;
1383
1384 /* check if we have children to keep up at all */
1385 enumerator = this->child_sas->create_enumerator(this->child_sas);
1386 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1387 {
1388 if (this->state == IKE_DELETING)
1389 {
1390 action = child_sa->get_close_action(child_sa);
1391 }
1392 else
1393 {
1394 action = child_sa->get_dpd_action(child_sa);
1395 }
1396 switch (action)
1397 {
1398 case ACTION_RESTART:
1399 restart = TRUE;
1400 break;
1401 case ACTION_ROUTE:
1402 charon->traps->install(charon->traps, this->peer_cfg,
1403 child_sa->get_config(child_sa));
1404 break;
1405 default:
1406 break;
1407 }
1408 }
1409 enumerator->destroy(enumerator);
1410 #ifdef ME
1411 /* mediation connections have no children, keep them up anyway */
1412 if (this->peer_cfg->is_mediation(this->peer_cfg))
1413 {
1414 restart = TRUE;
1415 }
1416 #endif /* ME */
1417 if (!restart)
1418 {
1419 return FAILED;
1420 }
1421
1422 /* check if we are able to reestablish this IKE_SA */
1423 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1424 (this->other_virtual_ip != NULL ||
1425 has_condition(this, COND_EAP_AUTHENTICATED)
1426 #ifdef ME
1427 || this->is_mediation_server
1428 #endif /* ME */
1429 ))
1430 {
1431 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1432 return FAILED;
1433 }
1434
1435 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1436 this->version, TRUE);
1437 new->set_peer_cfg(new, this->peer_cfg);
1438 host = this->other_host;
1439 new->set_other_host(new, host->clone(host));
1440 host = this->my_host;
1441 new->set_my_host(new, host->clone(host));
1442 /* if we already have a virtual IP, we reuse it */
1443 host = this->my_virtual_ip;
1444 if (host)
1445 {
1446 new->set_virtual_ip(new, TRUE, host);
1447 }
1448
1449 #ifdef ME
1450 if (this->peer_cfg->is_mediation(this->peer_cfg))
1451 {
1452 status = new->initiate(new, NULL, 0, NULL, NULL);
1453 }
1454 else
1455 #endif /* ME */
1456 {
1457 enumerator = this->child_sas->create_enumerator(this->child_sas);
1458 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1459 {
1460 if (this->state == IKE_DELETING)
1461 {
1462 action = child_sa->get_close_action(child_sa);
1463 }
1464 else
1465 {
1466 action = child_sa->get_dpd_action(child_sa);
1467 }
1468 switch (action)
1469 {
1470 case ACTION_RESTART:
1471 child_cfg = child_sa->get_config(child_sa);
1472 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1473 child_cfg->get_name(child_cfg));
1474 child_cfg->get_ref(child_cfg);
1475 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1476 break;
1477 default:
1478 continue;
1479 }
1480 if (status == DESTROY_ME)
1481 {
1482 break;
1483 }
1484 }
1485 enumerator->destroy(enumerator);
1486 }
1487
1488 if (status == DESTROY_ME)
1489 {
1490 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1491 status = FAILED;
1492 }
1493 else
1494 {
1495 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1496 status = SUCCESS;
1497 }
1498 charon->bus->set_sa(charon->bus, &this->public);
1499 return status;
1500 }
1501
1502 /**
1503 * Requeue the IKE_SA_INIT tasks for initiation, if required
1504 */
1505 static void requeue_init_tasks(private_ike_sa_t *this)
1506 {
1507 enumerator_t *enumerator;
1508 bool has_init = FALSE;
1509 task_t *task;
1510
1511 /* if we have advanced to IKE_AUTH, the IKE_INIT and related tasks
1512 * have already completed. Recreate them if necessary. */
1513 enumerator = this->task_manager->create_task_enumerator(
1514 this->task_manager, TASK_QUEUE_QUEUED);
1515 while (enumerator->enumerate(enumerator, &task))
1516 {
1517 if (task->get_type(task) == TASK_IKE_INIT)
1518 {
1519 has_init = TRUE;
1520 break;
1521 }
1522 }
1523 enumerator->destroy(enumerator);
1524
1525 if (!has_init)
1526 {
1527 task = (task_t*)ike_vendor_create(&this->public, TRUE);
1528 this->task_manager->queue_task(this->task_manager, task);
1529 task = (task_t*)ike_natd_create(&this->public, TRUE);
1530 this->task_manager->queue_task(this->task_manager, task);
1531 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1532 this->task_manager->queue_task(this->task_manager, task);
1533 }
1534 }
1535
1536 METHOD(ike_sa_t, retransmit, status_t,
1537 private_ike_sa_t *this, u_int32_t message_id)
1538 {
1539 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1540 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1541 {
1542 /* send a proper signal to brief interested bus listeners */
1543 switch (this->state)
1544 {
1545 case IKE_CONNECTING:
1546 {
1547 /* retry IKE_SA_INIT if we have multiple keyingtries */
1548 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1549 this->keyingtry++;
1550 if (tries == 0 || tries > this->keyingtry)
1551 {
1552 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1553 this->keyingtry + 1, tries);
1554 reset(this);
1555 requeue_init_tasks(this);
1556 return this->task_manager->initiate(this->task_manager);
1557 }
1558 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1559 break;
1560 }
1561 case IKE_DELETING:
1562 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1563 break;
1564 case IKE_REKEYING:
1565 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1566 /* FALL */
1567 default:
1568 reestablish(this);
1569 break;
1570 }
1571 return DESTROY_ME;
1572 }
1573 return SUCCESS;
1574 }
1575
1576 METHOD(ike_sa_t, set_auth_lifetime, void,
1577 private_ike_sa_t *this, u_int32_t lifetime)
1578 {
1579 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
1580 u_int32_t reauth_time = time_monotonic(NULL) + lifetime - reduction;
1581
1582 if (lifetime < reduction)
1583 {
1584 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
1585 lifetime);
1586 lib->processor->queue_job(lib->processor,
1587 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1588 }
1589 else if (this->stats[STAT_REAUTH] == 0 ||
1590 this->stats[STAT_REAUTH] > reauth_time)
1591 {
1592 this->stats[STAT_REAUTH] = reauth_time;
1593 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1594 " in %ds", lifetime, lifetime - reduction);
1595 lib->scheduler->schedule_job(lib->scheduler,
1596 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1597 lifetime - reduction);
1598 }
1599 else
1600 {
1601 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1602 "reauthentication already scheduled in %ds", lifetime,
1603 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1604 }
1605 }
1606
1607 /**
1608 * Check if the current combination of source and destination address is still
1609 * valid.
1610 */
1611 static bool is_current_path_valid(private_ike_sa_t *this)
1612 {
1613 bool valid = FALSE;
1614 host_t *src;
1615 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1616 this->other_host, this->my_host);
1617 if (src)
1618 {
1619 if (src->ip_equals(src, this->my_host))
1620 {
1621 valid = TRUE;
1622 }
1623 src->destroy(src);
1624 }
1625 return valid;
1626 }
1627
1628 /**
1629 * Check if we have any path avialable for this IKE SA.
1630 */
1631 static bool is_any_path_valid(private_ike_sa_t *this)
1632 {
1633 bool valid = FALSE;
1634 enumerator_t *enumerator;
1635 host_t *src, *addr;
1636 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1637 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1638 this->other_host, NULL);
1639 if (!src)
1640 {
1641 enumerator = this->additional_addresses->create_enumerator(
1642 this->additional_addresses);
1643 while (enumerator->enumerate(enumerator, &addr))
1644 {
1645 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1646 src = hydra->kernel_interface->get_source_addr(
1647 hydra->kernel_interface, addr, NULL);
1648 if (src)
1649 {
1650 break;
1651 }
1652 }
1653 enumerator->destroy(enumerator);
1654 }
1655 if (src)
1656 {
1657 valid = TRUE;
1658 src->destroy(src);
1659 }
1660 return valid;
1661 }
1662
1663 METHOD(ike_sa_t, roam, status_t,
1664 private_ike_sa_t *this, bool address)
1665 {
1666 ike_mobike_t *mobike;
1667
1668 switch (this->state)
1669 {
1670 case IKE_CREATED:
1671 case IKE_DELETING:
1672 case IKE_DESTROYING:
1673 case IKE_PASSIVE:
1674 return SUCCESS;
1675 default:
1676 break;
1677 }
1678
1679 /* keep existing path if possible */
1680 if (is_current_path_valid(this))
1681 {
1682 DBG2(DBG_IKE, "keeping connection path %H - %H",
1683 this->my_host, this->other_host);
1684 set_condition(this, COND_STALE, FALSE);
1685
1686 if (supports_extension(this, EXT_MOBIKE) && address)
1687 { /* if any addresses changed, send an updated list */
1688 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1689 mobike = ike_mobike_create(&this->public, TRUE);
1690 mobike->addresses(mobike);
1691 this->task_manager->queue_task(this->task_manager,
1692 (task_t*)mobike);
1693 return this->task_manager->initiate(this->task_manager);
1694 }
1695 return SUCCESS;
1696 }
1697
1698 if (!is_any_path_valid(this))
1699 {
1700 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1701 this->other_host);
1702 set_condition(this, COND_STALE, TRUE);
1703 return SUCCESS;
1704 }
1705 set_condition(this, COND_STALE, FALSE);
1706
1707 /* update addresses with mobike, if supported ... */
1708 if (supports_extension(this, EXT_MOBIKE))
1709 {
1710 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1711 { /* responder updates the peer about changed address config */
1712 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1713 "implicitly requesting an address change");
1714 address = TRUE;
1715 }
1716 else
1717 {
1718 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1719 }
1720 mobike = ike_mobike_create(&this->public, TRUE);
1721 mobike->roam(mobike, address);
1722 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1723 return this->task_manager->initiate(this->task_manager);
1724 }
1725
1726 /* ... reauth if not */
1727 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1728 { /* responder does not reauthenticate */
1729 set_condition(this, COND_STALE, TRUE);
1730 return SUCCESS;
1731 }
1732 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1733 return reauth(this);
1734 }
1735
1736 METHOD(ike_sa_t, add_configuration_attribute, void,
1737 private_ike_sa_t *this, attribute_handler_t *handler,
1738 configuration_attribute_type_t type, chunk_t data)
1739 {
1740 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1741
1742 entry->handler = handler;
1743 entry->type = type;
1744 entry->data = chunk_clone(data);
1745
1746 this->attributes->insert_last(this->attributes, entry);
1747 }
1748
1749 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1750 private_ike_sa_t *this, task_queue_t queue)
1751 {
1752 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1753 }
1754
1755 METHOD(ike_sa_t, queue_task, void,
1756 private_ike_sa_t *this, task_t *task)
1757 {
1758 this->task_manager->queue_task(this->task_manager, task);
1759 }
1760
1761 METHOD(ike_sa_t, inherit, void,
1762 private_ike_sa_t *this, ike_sa_t *other_public)
1763 {
1764 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1765 child_sa_t *child_sa;
1766 attribute_entry_t *entry;
1767 enumerator_t *enumerator;
1768 auth_cfg_t *cfg;
1769
1770 /* apply hosts and ids */
1771 this->my_host->destroy(this->my_host);
1772 this->other_host->destroy(this->other_host);
1773 this->my_id->destroy(this->my_id);
1774 this->other_id->destroy(this->other_id);
1775 this->my_host = other->my_host->clone(other->my_host);
1776 this->other_host = other->other_host->clone(other->other_host);
1777 this->my_id = other->my_id->clone(other->my_id);
1778 this->other_id = other->other_id->clone(other->other_id);
1779
1780 /* apply virtual assigned IPs... */
1781 if (other->my_virtual_ip)
1782 {
1783 this->my_virtual_ip = other->my_virtual_ip;
1784 other->my_virtual_ip = NULL;
1785 }
1786 if (other->other_virtual_ip)
1787 {
1788 this->other_virtual_ip = other->other_virtual_ip;
1789 other->other_virtual_ip = NULL;
1790 }
1791
1792 /* authentication information */
1793 enumerator = other->my_auths->create_enumerator(other->my_auths);
1794 while (enumerator->enumerate(enumerator, &cfg))
1795 {
1796 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1797 }
1798 enumerator->destroy(enumerator);
1799 enumerator = other->other_auths->create_enumerator(other->other_auths);
1800 while (enumerator->enumerate(enumerator, &cfg))
1801 {
1802 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1803 }
1804 enumerator->destroy(enumerator);
1805
1806 /* ... and configuration attributes */
1807 while (other->attributes->remove_last(other->attributes,
1808 (void**)&entry) == SUCCESS)
1809 {
1810 this->attributes->insert_first(this->attributes, entry);
1811 }
1812
1813 /* inherit all conditions */
1814 this->conditions = other->conditions;
1815 if (this->conditions & COND_NAT_HERE)
1816 {
1817 send_keepalive(this);
1818 }
1819
1820 #ifdef ME
1821 if (other->is_mediation_server)
1822 {
1823 act_as_mediation_server(this);
1824 }
1825 else if (other->server_reflexive_host)
1826 {
1827 this->server_reflexive_host = other->server_reflexive_host->clone(
1828 other->server_reflexive_host);
1829 }
1830 #endif /* ME */
1831
1832 /* adopt all children */
1833 while (other->child_sas->remove_last(other->child_sas,
1834 (void**)&child_sa) == SUCCESS)
1835 {
1836 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1837 }
1838
1839 /* move pending tasks to the new IKE_SA */
1840 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1841
1842 /* reauthentication timeout survives a rekeying */
1843 if (other->stats[STAT_REAUTH])
1844 {
1845 time_t reauth, delete, now = time_monotonic(NULL);
1846
1847 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
1848 reauth = this->stats[STAT_REAUTH] - now;
1849 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
1850 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
1851 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
1852 "lifetime reduced to %ds", reauth, delete);
1853 lib->scheduler->schedule_job(lib->scheduler,
1854 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
1855 lib->scheduler->schedule_job(lib->scheduler,
1856 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
1857 }
1858 }
1859
1860 METHOD(ike_sa_t, destroy, void,
1861 private_ike_sa_t *this)
1862 {
1863 attribute_entry_t *entry;
1864
1865 charon->bus->set_sa(charon->bus, &this->public);
1866
1867 set_state(this, IKE_DESTROYING);
1868 this->task_manager->destroy(this->task_manager);
1869
1870 /* remove attributes first, as we pass the IKE_SA to the handler */
1871 while (this->attributes->remove_last(this->attributes,
1872 (void**)&entry) == SUCCESS)
1873 {
1874 hydra->attributes->release(hydra->attributes, entry->handler,
1875 this->other_id, entry->type, entry->data);
1876 free(entry->data.ptr);
1877 free(entry);
1878 }
1879 this->attributes->destroy(this->attributes);
1880
1881 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1882
1883 /* unset SA after here to avoid usage by the listeners */
1884 charon->bus->set_sa(charon->bus, NULL);
1885
1886 this->keymat->destroy(this->keymat);
1887
1888 if (this->my_virtual_ip)
1889 {
1890 hydra->kernel_interface->del_ip(hydra->kernel_interface,
1891 this->my_virtual_ip);
1892 this->my_virtual_ip->destroy(this->my_virtual_ip);
1893 }
1894 if (this->other_virtual_ip)
1895 {
1896 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
1897 {
1898 hydra->attributes->release_address(hydra->attributes,
1899 this->peer_cfg->get_pool(this->peer_cfg),
1900 this->other_virtual_ip, get_other_eap_id(this));
1901 }
1902 this->other_virtual_ip->destroy(this->other_virtual_ip);
1903 }
1904 this->additional_addresses->destroy_offset(this->additional_addresses,
1905 offsetof(host_t, destroy));
1906 #ifdef ME
1907 if (this->is_mediation_server)
1908 {
1909 charon->mediation_manager->remove(charon->mediation_manager,
1910 this->ike_sa_id);
1911 }
1912 DESTROY_IF(this->server_reflexive_host);
1913 chunk_free(&this->connect_id);
1914 #endif /* ME */
1915 free(this->nat_detection_dest.ptr);
1916
1917 DESTROY_IF(this->my_host);
1918 DESTROY_IF(this->other_host);
1919 DESTROY_IF(this->my_id);
1920 DESTROY_IF(this->other_id);
1921 DESTROY_IF(this->local_host);
1922 DESTROY_IF(this->remote_host);
1923
1924 DESTROY_IF(this->ike_cfg);
1925 DESTROY_IF(this->peer_cfg);
1926 DESTROY_IF(this->proposal);
1927 this->my_auth->destroy(this->my_auth);
1928 this->other_auth->destroy(this->other_auth);
1929 this->my_auths->destroy_offset(this->my_auths,
1930 offsetof(auth_cfg_t, destroy));
1931 this->other_auths->destroy_offset(this->other_auths,
1932 offsetof(auth_cfg_t, destroy));
1933
1934 this->ike_sa_id->destroy(this->ike_sa_id);
1935 free(this);
1936 }
1937
1938 /*
1939 * Described in header.
1940 */
1941 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
1942 ike_version_t version)
1943 {
1944 private_ike_sa_t *this;
1945 static u_int32_t unique_id = 0;
1946
1947 if (version != IKEV1)
1948 {
1949 version = IKEV2;
1950 }
1951
1952 INIT(this,
1953 .public = {
1954 .get_version = _get_version,
1955 .get_state = _get_state,
1956 .set_state = _set_state,
1957 .get_name = _get_name,
1958 .get_statistic = _get_statistic,
1959 .set_statistic = _set_statistic,
1960 .process_message = _process_message,
1961 .initiate = _initiate,
1962 .get_ike_cfg = _get_ike_cfg,
1963 .set_ike_cfg = _set_ike_cfg,
1964 .get_peer_cfg = _get_peer_cfg,
1965 .set_peer_cfg = _set_peer_cfg,
1966 .get_auth_cfg = _get_auth_cfg,
1967 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
1968 .add_auth_cfg = _add_auth_cfg,
1969 .get_proposal = _get_proposal,
1970 .set_proposal = _set_proposal,
1971 .get_id = _get_id,
1972 .get_my_host = _get_my_host,
1973 .set_my_host = _set_my_host,
1974 .get_other_host = _get_other_host,
1975 .set_other_host = _set_other_host,
1976 .set_message_id = _set_message_id,
1977 .float_ports = _float_ports,
1978 .update_hosts = _update_hosts,
1979 .get_my_id = _get_my_id,
1980 .set_my_id = _set_my_id,
1981 .get_other_id = _get_other_id,
1982 .set_other_id = _set_other_id,
1983 .get_other_eap_id = _get_other_eap_id,
1984 .enable_extension = _enable_extension,
1985 .supports_extension = _supports_extension,
1986 .set_condition = _set_condition,
1987 .has_condition = _has_condition,
1988 .set_pending_updates = _set_pending_updates,
1989 .get_pending_updates = _get_pending_updates,
1990 .create_additional_address_enumerator = _create_additional_address_enumerator,
1991 .add_additional_address = _add_additional_address,
1992 .remove_additional_addresses = _remove_additional_addresses,
1993 .has_mapping_changed = _has_mapping_changed,
1994 .retransmit = _retransmit,
1995 .delete = _delete_,
1996 .destroy = _destroy,
1997 .send_dpd = _send_dpd,
1998 .send_keepalive = _send_keepalive,
1999 .get_keymat = _get_keymat,
2000 .add_child_sa = _add_child_sa,
2001 .get_child_sa = _get_child_sa,
2002 .get_child_count = _get_child_count,
2003 .create_child_sa_enumerator = _create_child_sa_enumerator,
2004 .remove_child_sa = _remove_child_sa,
2005 .rekey_child_sa = _rekey_child_sa,
2006 .delete_child_sa = _delete_child_sa,
2007 .destroy_child_sa = _destroy_child_sa,
2008 .rekey = _rekey,
2009 .reauth = _reauth,
2010 .reestablish = _reestablish,
2011 .set_auth_lifetime = _set_auth_lifetime,
2012 .roam = _roam,
2013 .inherit = _inherit,
2014 .generate_message = _generate_message,
2015 .reset = _reset,
2016 .get_unique_id = _get_unique_id,
2017 .set_virtual_ip = _set_virtual_ip,
2018 .get_virtual_ip = _get_virtual_ip,
2019 .add_configuration_attribute = _add_configuration_attribute,
2020 .set_kmaddress = _set_kmaddress,
2021 .create_task_enumerator = _create_task_enumerator,
2022 .queue_task = _queue_task,
2023 #ifdef ME
2024 .act_as_mediation_server = _act_as_mediation_server,
2025 .get_server_reflexive_host = _get_server_reflexive_host,
2026 .set_server_reflexive_host = _set_server_reflexive_host,
2027 .get_connect_id = _get_connect_id,
2028 .initiate_mediation = _initiate_mediation,
2029 .initiate_mediated = _initiate_mediated,
2030 .relay = _relay,
2031 .callback = _callback,
2032 .respond = _respond,
2033 #endif /* ME */
2034 },
2035 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2036 .version = version,
2037 .child_sas = linked_list_create(),
2038 .my_host = host_create_any(AF_INET),
2039 .other_host = host_create_any(AF_INET),
2040 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2041 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2042 .keymat = keymat_create(version, initiator),
2043 .state = IKE_CREATED,
2044 .stats[STAT_INBOUND] = time_monotonic(NULL),
2045 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2046 .my_auth = auth_cfg_create(),
2047 .other_auth = auth_cfg_create(),
2048 .my_auths = linked_list_create(),
2049 .other_auths = linked_list_create(),
2050 .unique_id = ++unique_id,
2051 .additional_addresses = linked_list_create(),
2052 .attributes = linked_list_create(),
2053 .keepalive_interval = lib->settings->get_time(lib->settings,
2054 "charon.keep_alive", KEEPALIVE_INTERVAL),
2055 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2056 "charon.flush_auth_cfg", FALSE),
2057 );
2058
2059 this->task_manager = task_manager_create(&this->public);
2060 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2061
2062 return &this->public;
2063 }