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