Moved IKE_SA reauth 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 this->task_manager->queue_ike_rekey(this->task_manager);
1329 return this->task_manager->initiate(this->task_manager);
1330 }
1331
1332 METHOD(ike_sa_t, reauth, status_t,
1333 private_ike_sa_t *this)
1334 {
1335 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1336 * If the peer does not support RFC4478, there is no way to keep the
1337 * IKE_SA up. */
1338 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1339 {
1340 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1341 if (this->other_virtual_ip != NULL ||
1342 has_condition(this, COND_EAP_AUTHENTICATED)
1343 #ifdef ME
1344 /* as mediation server we too cannot reauth the IKE_SA */
1345 || this->is_mediation_server
1346 #endif /* ME */
1347 )
1348 {
1349 time_t now = time_monotonic(NULL);
1350
1351 DBG1(DBG_IKE, "IKE_SA will timeout in %V",
1352 &now, &this->stats[STAT_DELETE]);
1353 return FAILED;
1354 }
1355 else
1356 {
1357 DBG1(DBG_IKE, "reauthenticating actively");
1358 }
1359 }
1360 this->task_manager->queue_ike_reauth(this->task_manager);
1361 return this->task_manager->initiate(this->task_manager);
1362 }
1363
1364 METHOD(ike_sa_t, reestablish, status_t,
1365 private_ike_sa_t *this)
1366 {
1367 ike_sa_t *new;
1368 host_t *host;
1369 action_t action;
1370 enumerator_t *enumerator;
1371 child_sa_t *child_sa;
1372 child_cfg_t *child_cfg;
1373 bool restart = FALSE;
1374 status_t status = FAILED;
1375
1376 /* check if we have children to keep up at all */
1377 enumerator = this->child_sas->create_enumerator(this->child_sas);
1378 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1379 {
1380 if (this->state == IKE_DELETING)
1381 {
1382 action = child_sa->get_close_action(child_sa);
1383 }
1384 else
1385 {
1386 action = child_sa->get_dpd_action(child_sa);
1387 }
1388 switch (action)
1389 {
1390 case ACTION_RESTART:
1391 restart = TRUE;
1392 break;
1393 case ACTION_ROUTE:
1394 charon->traps->install(charon->traps, this->peer_cfg,
1395 child_sa->get_config(child_sa));
1396 break;
1397 default:
1398 break;
1399 }
1400 }
1401 enumerator->destroy(enumerator);
1402 #ifdef ME
1403 /* mediation connections have no children, keep them up anyway */
1404 if (this->peer_cfg->is_mediation(this->peer_cfg))
1405 {
1406 restart = TRUE;
1407 }
1408 #endif /* ME */
1409 if (!restart)
1410 {
1411 return FAILED;
1412 }
1413
1414 /* check if we are able to reestablish this IKE_SA */
1415 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1416 (this->other_virtual_ip != NULL ||
1417 has_condition(this, COND_EAP_AUTHENTICATED)
1418 #ifdef ME
1419 || this->is_mediation_server
1420 #endif /* ME */
1421 ))
1422 {
1423 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1424 return FAILED;
1425 }
1426
1427 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1428 this->version, TRUE);
1429 new->set_peer_cfg(new, this->peer_cfg);
1430 host = this->other_host;
1431 new->set_other_host(new, host->clone(host));
1432 host = this->my_host;
1433 new->set_my_host(new, host->clone(host));
1434 /* if we already have a virtual IP, we reuse it */
1435 host = this->my_virtual_ip;
1436 if (host)
1437 {
1438 new->set_virtual_ip(new, TRUE, host);
1439 }
1440
1441 #ifdef ME
1442 if (this->peer_cfg->is_mediation(this->peer_cfg))
1443 {
1444 status = new->initiate(new, NULL, 0, NULL, NULL);
1445 }
1446 else
1447 #endif /* ME */
1448 {
1449 enumerator = this->child_sas->create_enumerator(this->child_sas);
1450 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1451 {
1452 if (this->state == IKE_DELETING)
1453 {
1454 action = child_sa->get_close_action(child_sa);
1455 }
1456 else
1457 {
1458 action = child_sa->get_dpd_action(child_sa);
1459 }
1460 switch (action)
1461 {
1462 case ACTION_RESTART:
1463 child_cfg = child_sa->get_config(child_sa);
1464 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1465 child_cfg->get_name(child_cfg));
1466 child_cfg->get_ref(child_cfg);
1467 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1468 break;
1469 default:
1470 continue;
1471 }
1472 if (status == DESTROY_ME)
1473 {
1474 break;
1475 }
1476 }
1477 enumerator->destroy(enumerator);
1478 }
1479
1480 if (status == DESTROY_ME)
1481 {
1482 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1483 status = FAILED;
1484 }
1485 else
1486 {
1487 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1488 status = SUCCESS;
1489 }
1490 charon->bus->set_sa(charon->bus, &this->public);
1491 return status;
1492 }
1493
1494 /**
1495 * Requeue the IKE_SA_INIT tasks for initiation, if required
1496 */
1497 static void requeue_init_tasks(private_ike_sa_t *this)
1498 {
1499 enumerator_t *enumerator;
1500 bool has_init = FALSE;
1501 task_t *task;
1502
1503 /* if we have advanced to IKE_AUTH, the IKE_INIT and related tasks
1504 * have already completed. Recreate them if necessary. */
1505 enumerator = this->task_manager->create_task_enumerator(
1506 this->task_manager, TASK_QUEUE_QUEUED);
1507 while (enumerator->enumerate(enumerator, &task))
1508 {
1509 if (task->get_type(task) == TASK_IKE_INIT)
1510 {
1511 has_init = TRUE;
1512 break;
1513 }
1514 }
1515 enumerator->destroy(enumerator);
1516
1517 if (!has_init)
1518 {
1519 task = (task_t*)ike_vendor_create(&this->public, TRUE);
1520 this->task_manager->queue_task(this->task_manager, task);
1521 task = (task_t*)ike_natd_create(&this->public, TRUE);
1522 this->task_manager->queue_task(this->task_manager, task);
1523 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1524 this->task_manager->queue_task(this->task_manager, task);
1525 }
1526 }
1527
1528 METHOD(ike_sa_t, retransmit, status_t,
1529 private_ike_sa_t *this, u_int32_t message_id)
1530 {
1531 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1532 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1533 {
1534 /* send a proper signal to brief interested bus listeners */
1535 switch (this->state)
1536 {
1537 case IKE_CONNECTING:
1538 {
1539 /* retry IKE_SA_INIT if we have multiple keyingtries */
1540 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1541 this->keyingtry++;
1542 if (tries == 0 || tries > this->keyingtry)
1543 {
1544 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1545 this->keyingtry + 1, tries);
1546 reset(this);
1547 requeue_init_tasks(this);
1548 return this->task_manager->initiate(this->task_manager);
1549 }
1550 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1551 break;
1552 }
1553 case IKE_DELETING:
1554 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1555 break;
1556 case IKE_REKEYING:
1557 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1558 /* FALL */
1559 default:
1560 reestablish(this);
1561 break;
1562 }
1563 return DESTROY_ME;
1564 }
1565 return SUCCESS;
1566 }
1567
1568 METHOD(ike_sa_t, set_auth_lifetime, void,
1569 private_ike_sa_t *this, u_int32_t lifetime)
1570 {
1571 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
1572 u_int32_t reauth_time = time_monotonic(NULL) + lifetime - reduction;
1573
1574 if (lifetime < reduction)
1575 {
1576 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
1577 lifetime);
1578 lib->processor->queue_job(lib->processor,
1579 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1580 }
1581 else if (this->stats[STAT_REAUTH] == 0 ||
1582 this->stats[STAT_REAUTH] > reauth_time)
1583 {
1584 this->stats[STAT_REAUTH] = reauth_time;
1585 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1586 " in %ds", lifetime, lifetime - reduction);
1587 lib->scheduler->schedule_job(lib->scheduler,
1588 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1589 lifetime - reduction);
1590 }
1591 else
1592 {
1593 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1594 "reauthentication already scheduled in %ds", lifetime,
1595 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1596 }
1597 }
1598
1599 /**
1600 * Check if the current combination of source and destination address is still
1601 * valid.
1602 */
1603 static bool is_current_path_valid(private_ike_sa_t *this)
1604 {
1605 bool valid = FALSE;
1606 host_t *src;
1607 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1608 this->other_host, this->my_host);
1609 if (src)
1610 {
1611 if (src->ip_equals(src, this->my_host))
1612 {
1613 valid = TRUE;
1614 }
1615 src->destroy(src);
1616 }
1617 return valid;
1618 }
1619
1620 /**
1621 * Check if we have any path avialable for this IKE SA.
1622 */
1623 static bool is_any_path_valid(private_ike_sa_t *this)
1624 {
1625 bool valid = FALSE;
1626 enumerator_t *enumerator;
1627 host_t *src, *addr;
1628 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1629 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1630 this->other_host, NULL);
1631 if (!src)
1632 {
1633 enumerator = this->additional_addresses->create_enumerator(
1634 this->additional_addresses);
1635 while (enumerator->enumerate(enumerator, &addr))
1636 {
1637 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1638 src = hydra->kernel_interface->get_source_addr(
1639 hydra->kernel_interface, addr, NULL);
1640 if (src)
1641 {
1642 break;
1643 }
1644 }
1645 enumerator->destroy(enumerator);
1646 }
1647 if (src)
1648 {
1649 valid = TRUE;
1650 src->destroy(src);
1651 }
1652 return valid;
1653 }
1654
1655 METHOD(ike_sa_t, roam, status_t,
1656 private_ike_sa_t *this, bool address)
1657 {
1658 ike_mobike_t *mobike;
1659
1660 switch (this->state)
1661 {
1662 case IKE_CREATED:
1663 case IKE_DELETING:
1664 case IKE_DESTROYING:
1665 case IKE_PASSIVE:
1666 return SUCCESS;
1667 default:
1668 break;
1669 }
1670
1671 /* keep existing path if possible */
1672 if (is_current_path_valid(this))
1673 {
1674 DBG2(DBG_IKE, "keeping connection path %H - %H",
1675 this->my_host, this->other_host);
1676 set_condition(this, COND_STALE, FALSE);
1677
1678 if (supports_extension(this, EXT_MOBIKE) && address)
1679 { /* if any addresses changed, send an updated list */
1680 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1681 mobike = ike_mobike_create(&this->public, TRUE);
1682 mobike->addresses(mobike);
1683 this->task_manager->queue_task(this->task_manager,
1684 (task_t*)mobike);
1685 return this->task_manager->initiate(this->task_manager);
1686 }
1687 return SUCCESS;
1688 }
1689
1690 if (!is_any_path_valid(this))
1691 {
1692 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1693 this->other_host);
1694 set_condition(this, COND_STALE, TRUE);
1695 return SUCCESS;
1696 }
1697 set_condition(this, COND_STALE, FALSE);
1698
1699 /* update addresses with mobike, if supported ... */
1700 if (supports_extension(this, EXT_MOBIKE))
1701 {
1702 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1703 { /* responder updates the peer about changed address config */
1704 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1705 "implicitly requesting an address change");
1706 address = TRUE;
1707 }
1708 else
1709 {
1710 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1711 }
1712 mobike = ike_mobike_create(&this->public, TRUE);
1713 mobike->roam(mobike, address);
1714 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1715 return this->task_manager->initiate(this->task_manager);
1716 }
1717
1718 /* ... reauth if not */
1719 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1720 { /* responder does not reauthenticate */
1721 set_condition(this, COND_STALE, TRUE);
1722 return SUCCESS;
1723 }
1724 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1725 return reauth(this);
1726 }
1727
1728 METHOD(ike_sa_t, add_configuration_attribute, void,
1729 private_ike_sa_t *this, attribute_handler_t *handler,
1730 configuration_attribute_type_t type, chunk_t data)
1731 {
1732 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1733
1734 entry->handler = handler;
1735 entry->type = type;
1736 entry->data = chunk_clone(data);
1737
1738 this->attributes->insert_last(this->attributes, entry);
1739 }
1740
1741 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1742 private_ike_sa_t *this, task_queue_t queue)
1743 {
1744 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1745 }
1746
1747 METHOD(ike_sa_t, queue_task, void,
1748 private_ike_sa_t *this, task_t *task)
1749 {
1750 this->task_manager->queue_task(this->task_manager, task);
1751 }
1752
1753 METHOD(ike_sa_t, inherit, void,
1754 private_ike_sa_t *this, ike_sa_t *other_public)
1755 {
1756 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1757 child_sa_t *child_sa;
1758 attribute_entry_t *entry;
1759 enumerator_t *enumerator;
1760 auth_cfg_t *cfg;
1761
1762 /* apply hosts and ids */
1763 this->my_host->destroy(this->my_host);
1764 this->other_host->destroy(this->other_host);
1765 this->my_id->destroy(this->my_id);
1766 this->other_id->destroy(this->other_id);
1767 this->my_host = other->my_host->clone(other->my_host);
1768 this->other_host = other->other_host->clone(other->other_host);
1769 this->my_id = other->my_id->clone(other->my_id);
1770 this->other_id = other->other_id->clone(other->other_id);
1771
1772 /* apply virtual assigned IPs... */
1773 if (other->my_virtual_ip)
1774 {
1775 this->my_virtual_ip = other->my_virtual_ip;
1776 other->my_virtual_ip = NULL;
1777 }
1778 if (other->other_virtual_ip)
1779 {
1780 this->other_virtual_ip = other->other_virtual_ip;
1781 other->other_virtual_ip = NULL;
1782 }
1783
1784 /* authentication information */
1785 enumerator = other->my_auths->create_enumerator(other->my_auths);
1786 while (enumerator->enumerate(enumerator, &cfg))
1787 {
1788 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1789 }
1790 enumerator->destroy(enumerator);
1791 enumerator = other->other_auths->create_enumerator(other->other_auths);
1792 while (enumerator->enumerate(enumerator, &cfg))
1793 {
1794 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1795 }
1796 enumerator->destroy(enumerator);
1797
1798 /* ... and configuration attributes */
1799 while (other->attributes->remove_last(other->attributes,
1800 (void**)&entry) == SUCCESS)
1801 {
1802 this->attributes->insert_first(this->attributes, entry);
1803 }
1804
1805 /* inherit all conditions */
1806 this->conditions = other->conditions;
1807 if (this->conditions & COND_NAT_HERE)
1808 {
1809 send_keepalive(this);
1810 }
1811
1812 #ifdef ME
1813 if (other->is_mediation_server)
1814 {
1815 act_as_mediation_server(this);
1816 }
1817 else if (other->server_reflexive_host)
1818 {
1819 this->server_reflexive_host = other->server_reflexive_host->clone(
1820 other->server_reflexive_host);
1821 }
1822 #endif /* ME */
1823
1824 /* adopt all children */
1825 while (other->child_sas->remove_last(other->child_sas,
1826 (void**)&child_sa) == SUCCESS)
1827 {
1828 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1829 }
1830
1831 /* move pending tasks to the new IKE_SA */
1832 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1833
1834 /* reauthentication timeout survives a rekeying */
1835 if (other->stats[STAT_REAUTH])
1836 {
1837 time_t reauth, delete, now = time_monotonic(NULL);
1838
1839 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
1840 reauth = this->stats[STAT_REAUTH] - now;
1841 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
1842 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
1843 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
1844 "lifetime reduced to %ds", reauth, delete);
1845 lib->scheduler->schedule_job(lib->scheduler,
1846 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
1847 lib->scheduler->schedule_job(lib->scheduler,
1848 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
1849 }
1850 }
1851
1852 METHOD(ike_sa_t, destroy, void,
1853 private_ike_sa_t *this)
1854 {
1855 attribute_entry_t *entry;
1856
1857 charon->bus->set_sa(charon->bus, &this->public);
1858
1859 set_state(this, IKE_DESTROYING);
1860 this->task_manager->destroy(this->task_manager);
1861
1862 /* remove attributes first, as we pass the IKE_SA to the handler */
1863 while (this->attributes->remove_last(this->attributes,
1864 (void**)&entry) == SUCCESS)
1865 {
1866 hydra->attributes->release(hydra->attributes, entry->handler,
1867 this->other_id, entry->type, entry->data);
1868 free(entry->data.ptr);
1869 free(entry);
1870 }
1871 this->attributes->destroy(this->attributes);
1872
1873 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1874
1875 /* unset SA after here to avoid usage by the listeners */
1876 charon->bus->set_sa(charon->bus, NULL);
1877
1878 this->keymat->destroy(this->keymat);
1879
1880 if (this->my_virtual_ip)
1881 {
1882 hydra->kernel_interface->del_ip(hydra->kernel_interface,
1883 this->my_virtual_ip);
1884 this->my_virtual_ip->destroy(this->my_virtual_ip);
1885 }
1886 if (this->other_virtual_ip)
1887 {
1888 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
1889 {
1890 hydra->attributes->release_address(hydra->attributes,
1891 this->peer_cfg->get_pool(this->peer_cfg),
1892 this->other_virtual_ip, get_other_eap_id(this));
1893 }
1894 this->other_virtual_ip->destroy(this->other_virtual_ip);
1895 }
1896 this->additional_addresses->destroy_offset(this->additional_addresses,
1897 offsetof(host_t, destroy));
1898 #ifdef ME
1899 if (this->is_mediation_server)
1900 {
1901 charon->mediation_manager->remove(charon->mediation_manager,
1902 this->ike_sa_id);
1903 }
1904 DESTROY_IF(this->server_reflexive_host);
1905 chunk_free(&this->connect_id);
1906 #endif /* ME */
1907 free(this->nat_detection_dest.ptr);
1908
1909 DESTROY_IF(this->my_host);
1910 DESTROY_IF(this->other_host);
1911 DESTROY_IF(this->my_id);
1912 DESTROY_IF(this->other_id);
1913 DESTROY_IF(this->local_host);
1914 DESTROY_IF(this->remote_host);
1915
1916 DESTROY_IF(this->ike_cfg);
1917 DESTROY_IF(this->peer_cfg);
1918 DESTROY_IF(this->proposal);
1919 this->my_auth->destroy(this->my_auth);
1920 this->other_auth->destroy(this->other_auth);
1921 this->my_auths->destroy_offset(this->my_auths,
1922 offsetof(auth_cfg_t, destroy));
1923 this->other_auths->destroy_offset(this->other_auths,
1924 offsetof(auth_cfg_t, destroy));
1925
1926 this->ike_sa_id->destroy(this->ike_sa_id);
1927 free(this);
1928 }
1929
1930 /*
1931 * Described in header.
1932 */
1933 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
1934 ike_version_t version)
1935 {
1936 private_ike_sa_t *this;
1937 static u_int32_t unique_id = 0;
1938
1939 if (version != IKEV1)
1940 {
1941 version = IKEV2;
1942 }
1943
1944 INIT(this,
1945 .public = {
1946 .get_version = _get_version,
1947 .get_state = _get_state,
1948 .set_state = _set_state,
1949 .get_name = _get_name,
1950 .get_statistic = _get_statistic,
1951 .set_statistic = _set_statistic,
1952 .process_message = _process_message,
1953 .initiate = _initiate,
1954 .get_ike_cfg = _get_ike_cfg,
1955 .set_ike_cfg = _set_ike_cfg,
1956 .get_peer_cfg = _get_peer_cfg,
1957 .set_peer_cfg = _set_peer_cfg,
1958 .get_auth_cfg = _get_auth_cfg,
1959 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
1960 .add_auth_cfg = _add_auth_cfg,
1961 .get_proposal = _get_proposal,
1962 .set_proposal = _set_proposal,
1963 .get_id = _get_id,
1964 .get_my_host = _get_my_host,
1965 .set_my_host = _set_my_host,
1966 .get_other_host = _get_other_host,
1967 .set_other_host = _set_other_host,
1968 .set_message_id = _set_message_id,
1969 .float_ports = _float_ports,
1970 .update_hosts = _update_hosts,
1971 .get_my_id = _get_my_id,
1972 .set_my_id = _set_my_id,
1973 .get_other_id = _get_other_id,
1974 .set_other_id = _set_other_id,
1975 .get_other_eap_id = _get_other_eap_id,
1976 .enable_extension = _enable_extension,
1977 .supports_extension = _supports_extension,
1978 .set_condition = _set_condition,
1979 .has_condition = _has_condition,
1980 .set_pending_updates = _set_pending_updates,
1981 .get_pending_updates = _get_pending_updates,
1982 .create_additional_address_enumerator = _create_additional_address_enumerator,
1983 .add_additional_address = _add_additional_address,
1984 .remove_additional_addresses = _remove_additional_addresses,
1985 .has_mapping_changed = _has_mapping_changed,
1986 .retransmit = _retransmit,
1987 .delete = _delete_,
1988 .destroy = _destroy,
1989 .send_dpd = _send_dpd,
1990 .send_keepalive = _send_keepalive,
1991 .get_keymat = _get_keymat,
1992 .add_child_sa = _add_child_sa,
1993 .get_child_sa = _get_child_sa,
1994 .get_child_count = _get_child_count,
1995 .create_child_sa_enumerator = _create_child_sa_enumerator,
1996 .remove_child_sa = _remove_child_sa,
1997 .rekey_child_sa = _rekey_child_sa,
1998 .delete_child_sa = _delete_child_sa,
1999 .destroy_child_sa = _destroy_child_sa,
2000 .rekey = _rekey,
2001 .reauth = _reauth,
2002 .reestablish = _reestablish,
2003 .set_auth_lifetime = _set_auth_lifetime,
2004 .roam = _roam,
2005 .inherit = _inherit,
2006 .generate_message = _generate_message,
2007 .reset = _reset,
2008 .get_unique_id = _get_unique_id,
2009 .set_virtual_ip = _set_virtual_ip,
2010 .get_virtual_ip = _get_virtual_ip,
2011 .add_configuration_attribute = _add_configuration_attribute,
2012 .set_kmaddress = _set_kmaddress,
2013 .create_task_enumerator = _create_task_enumerator,
2014 .queue_task = _queue_task,
2015 #ifdef ME
2016 .act_as_mediation_server = _act_as_mediation_server,
2017 .get_server_reflexive_host = _get_server_reflexive_host,
2018 .set_server_reflexive_host = _set_server_reflexive_host,
2019 .get_connect_id = _get_connect_id,
2020 .initiate_mediation = _initiate_mediation,
2021 .initiate_mediated = _initiate_mediated,
2022 .relay = _relay,
2023 .callback = _callback,
2024 .respond = _respond,
2025 #endif /* ME */
2026 },
2027 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2028 .version = version,
2029 .child_sas = linked_list_create(),
2030 .my_host = host_create_any(AF_INET),
2031 .other_host = host_create_any(AF_INET),
2032 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2033 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2034 .keymat = keymat_create(version, initiator),
2035 .state = IKE_CREATED,
2036 .stats[STAT_INBOUND] = time_monotonic(NULL),
2037 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2038 .my_auth = auth_cfg_create(),
2039 .other_auth = auth_cfg_create(),
2040 .my_auths = linked_list_create(),
2041 .other_auths = linked_list_create(),
2042 .unique_id = ++unique_id,
2043 .additional_addresses = linked_list_create(),
2044 .attributes = linked_list_create(),
2045 .keepalive_interval = lib->settings->get_time(lib->settings,
2046 "charon.keep_alive", KEEPALIVE_INTERVAL),
2047 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2048 "charon.flush_auth_cfg", FALSE),
2049 );
2050
2051 this->task_manager = task_manager_create(&this->public);
2052 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2053
2054 return &this->public;
2055 }