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