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