Use name from initialization to access settings in libcharon.
[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 return this->peer_addresses->create_enumerator(this->peer_addresses);
783 }
784
785 METHOD(ike_sa_t, clear_peer_addresses, void,
786 private_ike_sa_t *this)
787 {
788 enumerator_t *enumerator = create_peer_address_enumerator(this);
789 host_t *host;
790
791 while (enumerator->enumerate(enumerator, (void**)&host))
792 {
793 this->peer_addresses->remove_at(this->peer_addresses,
794 enumerator);
795 host->destroy(host);
796 }
797 enumerator->destroy(enumerator);
798 }
799
800 METHOD(ike_sa_t, has_mapping_changed, bool,
801 private_ike_sa_t *this, chunk_t hash)
802 {
803 if (this->nat_detection_dest.ptr == NULL)
804 {
805 this->nat_detection_dest = chunk_clone(hash);
806 return FALSE;
807 }
808 if (chunk_equals(hash, this->nat_detection_dest))
809 {
810 return FALSE;
811 }
812 free(this->nat_detection_dest.ptr);
813 this->nat_detection_dest = chunk_clone(hash);
814 return TRUE;
815 }
816
817 METHOD(ike_sa_t, set_pending_updates, void,
818 private_ike_sa_t *this, u_int32_t updates)
819 {
820 this->pending_updates = updates;
821 }
822
823 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
824 private_ike_sa_t *this)
825 {
826 return this->pending_updates;
827 }
828
829 METHOD(ike_sa_t, float_ports, void,
830 private_ike_sa_t *this)
831 {
832 /* do not switch if we have a custom port from MOBIKE/NAT */
833 if (this->my_host->get_port(this->my_host) == IKEV2_UDP_PORT)
834 {
835 this->my_host->set_port(this->my_host, IKEV2_NATT_PORT);
836 }
837 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
838 {
839 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
840 }
841 }
842
843 METHOD(ike_sa_t, update_hosts, void,
844 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
845 {
846 bool update = FALSE;
847
848 if (me == NULL)
849 {
850 me = this->my_host;
851 }
852 if (other == NULL)
853 {
854 other = this->other_host;
855 }
856
857 /* apply hosts on first received message */
858 if (this->my_host->is_anyaddr(this->my_host) ||
859 this->other_host->is_anyaddr(this->other_host))
860 {
861 set_my_host(this, me->clone(me));
862 set_other_host(this, other->clone(other));
863 update = TRUE;
864 }
865 else
866 {
867 /* update our address in any case */
868 if (!me->equals(me, this->my_host))
869 {
870 set_my_host(this, me->clone(me));
871 update = TRUE;
872 }
873
874 if (!other->equals(other, this->other_host))
875 {
876 /* update others address if we are NOT NATed */
877 if (force || !has_condition(this, COND_NAT_HERE))
878 {
879 set_other_host(this, other->clone(other));
880 update = TRUE;
881 }
882 }
883 }
884
885 /* update all associated CHILD_SAs, if required */
886 if (update)
887 {
888 enumerator_t *enumerator;
889 child_sa_t *child_sa;
890
891 enumerator = this->child_sas->create_enumerator(this->child_sas);
892 while (enumerator->enumerate(enumerator, (void**)&child_sa))
893 {
894 if (child_sa->update(child_sa, this->my_host,
895 this->other_host, this->my_virtual_ip,
896 has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
897 {
898 this->public.rekey_child_sa(&this->public,
899 child_sa->get_protocol(child_sa),
900 child_sa->get_spi(child_sa, TRUE));
901 }
902 }
903 enumerator->destroy(enumerator);
904 }
905 }
906
907 METHOD(ike_sa_t, generate_message, status_t,
908 private_ike_sa_t *this, message_t *message, packet_t **packet)
909 {
910 status_t status;
911
912 if (message->is_encoded(message))
913 { /* already done */
914 *packet = message->get_packet(message);
915 return SUCCESS;
916 }
917 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
918 message->set_ike_sa_id(message, this->ike_sa_id);
919 charon->bus->message(charon->bus, message, FALSE, TRUE);
920 status = message->generate(message, this->keymat, packet);
921 if (status == SUCCESS)
922 {
923 charon->bus->message(charon->bus, message, FALSE, FALSE);
924 }
925 return status;
926 }
927
928 METHOD(ike_sa_t, set_kmaddress, void,
929 private_ike_sa_t *this, host_t *local, host_t *remote)
930 {
931 DESTROY_IF(this->local_host);
932 DESTROY_IF(this->remote_host);
933 this->local_host = local->clone(local);
934 this->remote_host = remote->clone(remote);
935 }
936
937 #ifdef ME
938 METHOD(ike_sa_t, act_as_mediation_server, void,
939 private_ike_sa_t *this)
940 {
941 charon->mediation_manager->update_sa_id(charon->mediation_manager,
942 this->other_id, this->ike_sa_id);
943 this->is_mediation_server = TRUE;
944 }
945
946 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
947 private_ike_sa_t *this)
948 {
949 return this->server_reflexive_host;
950 }
951
952 METHOD(ike_sa_t, set_server_reflexive_host, void,
953 private_ike_sa_t *this, host_t *host)
954 {
955 DESTROY_IF(this->server_reflexive_host);
956 this->server_reflexive_host = host;
957 }
958
959 METHOD(ike_sa_t, get_connect_id, chunk_t,
960 private_ike_sa_t *this)
961 {
962 return this->connect_id;
963 }
964
965 METHOD(ike_sa_t, respond, status_t,
966 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
967 {
968 ike_me_t *task = ike_me_create(&this->public, TRUE);
969 task->respond(task, peer_id, connect_id);
970 this->task_manager->queue_task(this->task_manager, (task_t*)task);
971 return this->task_manager->initiate(this->task_manager);
972 }
973
974 METHOD(ike_sa_t, callback, status_t,
975 private_ike_sa_t *this, identification_t *peer_id)
976 {
977 ike_me_t *task = ike_me_create(&this->public, TRUE);
978 task->callback(task, peer_id);
979 this->task_manager->queue_task(this->task_manager, (task_t*)task);
980 return this->task_manager->initiate(this->task_manager);
981 }
982
983 METHOD(ike_sa_t, relay, status_t,
984 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
985 chunk_t connect_key, linked_list_t *endpoints, bool response)
986 {
987 ike_me_t *task = ike_me_create(&this->public, TRUE);
988 task->relay(task, requester, connect_id, connect_key, endpoints, response);
989 this->task_manager->queue_task(this->task_manager, (task_t*)task);
990 return this->task_manager->initiate(this->task_manager);
991 }
992
993 METHOD(ike_sa_t, initiate_mediation, status_t,
994 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
995 {
996 ike_me_t *task = ike_me_create(&this->public, TRUE);
997 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
998 this->task_manager->queue_task(this->task_manager, (task_t*)task);
999 return this->task_manager->initiate(this->task_manager);
1000 }
1001
1002 METHOD(ike_sa_t, initiate_mediated, status_t,
1003 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1004 {
1005 set_my_host(this, me->clone(me));
1006 set_other_host(this, other->clone(other));
1007 chunk_free(&this->connect_id);
1008 this->connect_id = chunk_clone(connect_id);
1009 return this->task_manager->initiate(this->task_manager);
1010 }
1011 #endif /* ME */
1012
1013 /**
1014 * Resolve DNS host in configuration
1015 */
1016 static void resolve_hosts(private_ike_sa_t *this)
1017 {
1018 host_t *host;
1019
1020 if (this->remote_host)
1021 {
1022 host = this->remote_host->clone(this->remote_host);
1023 host->set_port(host, IKEV2_UDP_PORT);
1024 }
1025 else
1026 {
1027 host = host_create_from_dns(this->ike_cfg->get_other_addr(this->ike_cfg),
1028 0, this->ike_cfg->get_other_port(this->ike_cfg));
1029 }
1030 if (host)
1031 {
1032 set_other_host(this, host);
1033 }
1034
1035 if (this->local_host)
1036 {
1037 host = this->local_host->clone(this->local_host);
1038 host->set_port(host, IKEV2_UDP_PORT);
1039 }
1040 else
1041 {
1042 int family = 0;
1043
1044 /* use same address family as for other */
1045 if (!this->other_host->is_anyaddr(this->other_host))
1046 {
1047 family = this->other_host->get_family(this->other_host);
1048 }
1049 host = host_create_from_dns(this->ike_cfg->get_my_addr(this->ike_cfg),
1050 family, this->ike_cfg->get_my_port(this->ike_cfg));
1051
1052 if (host && host->is_anyaddr(host) &&
1053 !this->other_host->is_anyaddr(this->other_host))
1054 {
1055 host->destroy(host);
1056 host = hydra->kernel_interface->get_source_addr(
1057 hydra->kernel_interface, this->other_host, NULL);
1058 if (host)
1059 {
1060 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1061 }
1062 else
1063 { /* fallback to address family specific %any(6), if configured */
1064 host = host_create_from_dns(
1065 this->ike_cfg->get_my_addr(this->ike_cfg),
1066 0, this->ike_cfg->get_my_port(this->ike_cfg));
1067 }
1068 }
1069 }
1070 if (host)
1071 {
1072 set_my_host(this, host);
1073 }
1074 }
1075
1076 METHOD(ike_sa_t, initiate, status_t,
1077 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1078 traffic_selector_t *tsi, traffic_selector_t *tsr)
1079 {
1080 if (this->state == IKE_CREATED)
1081 {
1082 if (this->my_host->is_anyaddr(this->my_host) ||
1083 this->other_host->is_anyaddr(this->other_host))
1084 {
1085 resolve_hosts(this);
1086 }
1087
1088 if (this->other_host->is_anyaddr(this->other_host)
1089 #ifdef ME
1090 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1091 #endif /* ME */
1092 )
1093 {
1094 DESTROY_IF(child_cfg);
1095 DBG1(DBG_IKE, "unable to initiate to %%any");
1096 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1097 return DESTROY_ME;
1098 }
1099
1100 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1101 this->task_manager->queue_ike(this->task_manager);
1102 }
1103
1104 #ifdef ME
1105 if (this->peer_cfg->is_mediation(this->peer_cfg))
1106 {
1107 if (this->state == IKE_ESTABLISHED)
1108 {
1109 /* mediation connection is already established, retrigger state
1110 * change to notify bus listeners */
1111 DBG1(DBG_IKE, "mediation connection is already up");
1112 set_state(this, IKE_ESTABLISHED);
1113 }
1114 DESTROY_IF(child_cfg);
1115 }
1116 else
1117 #endif /* ME */
1118 if (child_cfg)
1119 {
1120 /* normal IKE_SA with CHILD_SA */
1121 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1122 tsi, tsr);
1123 #ifdef ME
1124 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1125 {
1126 /* mediated connection, initiate mediation process */
1127 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1128 lib->processor->queue_job(lib->processor, job);
1129 return SUCCESS;
1130 }
1131 #endif /* ME */
1132 }
1133
1134 return this->task_manager->initiate(this->task_manager);
1135 }
1136
1137 METHOD(ike_sa_t, process_message, status_t,
1138 private_ike_sa_t *this, message_t *message)
1139 {
1140 status_t status;
1141
1142 if (this->state == IKE_PASSIVE)
1143 { /* do not handle messages in passive state */
1144 return FAILED;
1145 }
1146 if (message->get_major_version(message) != this->version)
1147 {
1148 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1149 exchange_type_names, message->get_exchange_type(message),
1150 message->get_major_version(message),
1151 ike_version_names, this->version);
1152 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1153 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1154 return FAILED;
1155 }
1156 status = this->task_manager->process_message(this->task_manager, message);
1157 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1158 {
1159 /* authentication completed */
1160 this->flush_auth_cfg = FALSE;
1161 flush_auth_cfgs(this);
1162 }
1163 return status;
1164 }
1165
1166 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1167 private_ike_sa_t *this)
1168 {
1169 return this->ike_sa_id;
1170 }
1171
1172 METHOD(ike_sa_t, get_version, ike_version_t,
1173 private_ike_sa_t *this)
1174 {
1175 return this->version;
1176 }
1177
1178 METHOD(ike_sa_t, get_my_id, identification_t*,
1179 private_ike_sa_t *this)
1180 {
1181 return this->my_id;
1182 }
1183
1184 METHOD(ike_sa_t, set_my_id, void,
1185 private_ike_sa_t *this, identification_t *me)
1186 {
1187 DESTROY_IF(this->my_id);
1188 this->my_id = me;
1189 }
1190
1191 METHOD(ike_sa_t, get_other_id, identification_t*,
1192 private_ike_sa_t *this)
1193 {
1194 return this->other_id;
1195 }
1196
1197 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1198 private_ike_sa_t *this)
1199 {
1200 identification_t *id = NULL, *current;
1201 enumerator_t *enumerator;
1202 auth_cfg_t *cfg;
1203
1204 enumerator = this->other_auths->create_enumerator(this->other_auths);
1205 while (enumerator->enumerate(enumerator, &cfg))
1206 {
1207 /* prefer EAP-Identity of last round */
1208 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1209 if (!current || current->get_type(current) == ID_ANY)
1210 {
1211 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1212 }
1213 if (!current || current->get_type(current) == ID_ANY)
1214 {
1215 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1216 }
1217 if (current && current->get_type(current) != ID_ANY)
1218 {
1219 id = current;
1220 continue;
1221 }
1222 }
1223 enumerator->destroy(enumerator);
1224 if (id)
1225 {
1226 return id;
1227 }
1228 return this->other_id;
1229 }
1230
1231 METHOD(ike_sa_t, set_other_id, void,
1232 private_ike_sa_t *this, identification_t *other)
1233 {
1234 DESTROY_IF(this->other_id);
1235 this->other_id = other;
1236 }
1237
1238 METHOD(ike_sa_t, add_child_sa, void,
1239 private_ike_sa_t *this, child_sa_t *child_sa)
1240 {
1241 this->child_sas->insert_last(this->child_sas, child_sa);
1242 }
1243
1244 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1245 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1246 {
1247 enumerator_t *enumerator;
1248 child_sa_t *current, *found = NULL;
1249
1250 enumerator = this->child_sas->create_enumerator(this->child_sas);
1251 while (enumerator->enumerate(enumerator, (void**)&current))
1252 {
1253 if (current->get_spi(current, inbound) == spi &&
1254 current->get_protocol(current) == protocol)
1255 {
1256 found = current;
1257 }
1258 }
1259 enumerator->destroy(enumerator);
1260 return found;
1261 }
1262
1263 METHOD(ike_sa_t, get_child_count, int,
1264 private_ike_sa_t *this)
1265 {
1266 return this->child_sas->get_count(this->child_sas);
1267 }
1268
1269 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1270 private_ike_sa_t *this)
1271 {
1272 return this->child_sas->create_enumerator(this->child_sas);
1273 }
1274
1275 METHOD(ike_sa_t, remove_child_sa, void,
1276 private_ike_sa_t *this, enumerator_t *enumerator)
1277 {
1278 this->child_sas->remove_at(this->child_sas, enumerator);
1279 }
1280
1281 METHOD(ike_sa_t, rekey_child_sa, status_t,
1282 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1283 {
1284 if (this->state == IKE_PASSIVE)
1285 {
1286 return INVALID_STATE;
1287 }
1288 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1289 return this->task_manager->initiate(this->task_manager);
1290 }
1291
1292 METHOD(ike_sa_t, delete_child_sa, status_t,
1293 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool expired)
1294 {
1295 if (this->state == IKE_PASSIVE)
1296 {
1297 return INVALID_STATE;
1298 }
1299 this->task_manager->queue_child_delete(this->task_manager,
1300 protocol, spi, expired);
1301 return this->task_manager->initiate(this->task_manager);
1302 }
1303
1304 METHOD(ike_sa_t, destroy_child_sa, status_t,
1305 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1306 {
1307 enumerator_t *enumerator;
1308 child_sa_t *child_sa;
1309 status_t status = NOT_FOUND;
1310
1311 enumerator = this->child_sas->create_enumerator(this->child_sas);
1312 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1313 {
1314 if (child_sa->get_protocol(child_sa) == protocol &&
1315 child_sa->get_spi(child_sa, TRUE) == spi)
1316 {
1317 this->child_sas->remove_at(this->child_sas, enumerator);
1318 child_sa->destroy(child_sa);
1319 status = SUCCESS;
1320 break;
1321 }
1322 }
1323 enumerator->destroy(enumerator);
1324 return status;
1325 }
1326
1327 METHOD(ike_sa_t, delete_, status_t,
1328 private_ike_sa_t *this)
1329 {
1330 switch (this->state)
1331 {
1332 case IKE_REKEYING:
1333 if (this->version == IKEV1)
1334 { /* SA has been reauthenticated, delete */
1335 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1336 break;
1337 }
1338 /* FALL */
1339 case IKE_ESTABLISHED:
1340 this->task_manager->queue_ike_delete(this->task_manager);
1341 return this->task_manager->initiate(this->task_manager);
1342 case IKE_CREATED:
1343 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1344 break;
1345 case IKE_PASSIVE:
1346 break;
1347 default:
1348 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1349 "without notification", ike_sa_state_names, this->state);
1350 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1351 break;
1352 }
1353 return DESTROY_ME;
1354 }
1355
1356 METHOD(ike_sa_t, rekey, status_t,
1357 private_ike_sa_t *this)
1358 {
1359 if (this->state == IKE_PASSIVE)
1360 {
1361 return INVALID_STATE;
1362 }
1363 this->task_manager->queue_ike_rekey(this->task_manager);
1364 return this->task_manager->initiate(this->task_manager);
1365 }
1366
1367 METHOD(ike_sa_t, reauth, status_t,
1368 private_ike_sa_t *this)
1369 {
1370 if (this->state == IKE_PASSIVE)
1371 {
1372 return INVALID_STATE;
1373 }
1374 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1375 * If the peer does not support RFC4478, there is no way to keep the
1376 * IKE_SA up. */
1377 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1378 {
1379 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1380 if (this->other_virtual_ip != NULL ||
1381 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1382 has_condition(this, COND_EAP_AUTHENTICATED)
1383 #ifdef ME
1384 /* as mediation server we too cannot reauth the IKE_SA */
1385 || this->is_mediation_server
1386 #endif /* ME */
1387 )
1388 {
1389 time_t del, now;
1390
1391 del = this->stats[STAT_DELETE];
1392 now = time_monotonic(NULL);
1393 DBG1(DBG_IKE, "IKE_SA %s[%d] will timeout in %V",
1394 get_name(this), this->unique_id, &now, &del);
1395 return FAILED;
1396 }
1397 else
1398 {
1399 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d] actively",
1400 get_name(this), this->unique_id);
1401 }
1402 }
1403 else
1404 {
1405 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d]",
1406 get_name(this), this->unique_id);
1407 }
1408 this->task_manager->queue_ike_reauth(this->task_manager);
1409 return this->task_manager->initiate(this->task_manager);
1410 }
1411
1412 METHOD(ike_sa_t, reestablish, status_t,
1413 private_ike_sa_t *this)
1414 {
1415 ike_sa_t *new;
1416 host_t *host;
1417 action_t action;
1418 enumerator_t *enumerator;
1419 child_sa_t *child_sa;
1420 child_cfg_t *child_cfg;
1421 bool restart = FALSE;
1422 status_t status = FAILED;
1423
1424 /* check if we have children to keep up at all */
1425 enumerator = this->child_sas->create_enumerator(this->child_sas);
1426 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1427 {
1428 if (this->state == IKE_DELETING)
1429 {
1430 action = child_sa->get_close_action(child_sa);
1431 }
1432 else
1433 {
1434 action = child_sa->get_dpd_action(child_sa);
1435 }
1436 switch (action)
1437 {
1438 case ACTION_RESTART:
1439 restart = TRUE;
1440 break;
1441 case ACTION_ROUTE:
1442 charon->traps->install(charon->traps, this->peer_cfg,
1443 child_sa->get_config(child_sa));
1444 break;
1445 default:
1446 break;
1447 }
1448 }
1449 enumerator->destroy(enumerator);
1450 #ifdef ME
1451 /* mediation connections have no children, keep them up anyway */
1452 if (this->peer_cfg->is_mediation(this->peer_cfg))
1453 {
1454 restart = TRUE;
1455 }
1456 #endif /* ME */
1457 if (!restart)
1458 {
1459 return FAILED;
1460 }
1461
1462 /* check if we are able to reestablish this IKE_SA */
1463 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1464 (this->other_virtual_ip != NULL ||
1465 has_condition(this, COND_EAP_AUTHENTICATED)
1466 #ifdef ME
1467 || this->is_mediation_server
1468 #endif /* ME */
1469 ))
1470 {
1471 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1472 return FAILED;
1473 }
1474
1475 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1476 this->version, TRUE);
1477 if (!new)
1478 {
1479 return FAILED;
1480 }
1481 new->set_peer_cfg(new, this->peer_cfg);
1482 host = this->other_host;
1483 new->set_other_host(new, host->clone(host));
1484 host = this->my_host;
1485 new->set_my_host(new, host->clone(host));
1486 /* if we already have a virtual IP, we reuse it */
1487 host = this->my_virtual_ip;
1488 if (host)
1489 {
1490 new->set_virtual_ip(new, TRUE, host);
1491 }
1492
1493 #ifdef ME
1494 if (this->peer_cfg->is_mediation(this->peer_cfg))
1495 {
1496 status = new->initiate(new, NULL, 0, NULL, NULL);
1497 }
1498 else
1499 #endif /* ME */
1500 {
1501 enumerator = this->child_sas->create_enumerator(this->child_sas);
1502 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1503 {
1504 if (this->state == IKE_DELETING)
1505 {
1506 action = child_sa->get_close_action(child_sa);
1507 }
1508 else
1509 {
1510 action = child_sa->get_dpd_action(child_sa);
1511 }
1512 switch (action)
1513 {
1514 case ACTION_RESTART:
1515 child_cfg = child_sa->get_config(child_sa);
1516 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1517 child_cfg->get_name(child_cfg));
1518 child_cfg->get_ref(child_cfg);
1519 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1520 break;
1521 default:
1522 continue;
1523 }
1524 if (status == DESTROY_ME)
1525 {
1526 break;
1527 }
1528 }
1529 enumerator->destroy(enumerator);
1530 }
1531
1532 if (status == DESTROY_ME)
1533 {
1534 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1535 status = FAILED;
1536 }
1537 else
1538 {
1539 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1540 status = SUCCESS;
1541 }
1542 charon->bus->set_sa(charon->bus, &this->public);
1543 return status;
1544 }
1545
1546 METHOD(ike_sa_t, retransmit, status_t,
1547 private_ike_sa_t *this, u_int32_t message_id)
1548 {
1549 if (this->state == IKE_PASSIVE)
1550 {
1551 return INVALID_STATE;
1552 }
1553 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1554 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1555 {
1556 /* send a proper signal to brief interested bus listeners */
1557 switch (this->state)
1558 {
1559 case IKE_CONNECTING:
1560 {
1561 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1562 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1563 this->keyingtry++;
1564 if (tries == 0 || tries > this->keyingtry)
1565 {
1566 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1567 this->keyingtry + 1, tries);
1568 reset(this);
1569 resolve_hosts(this);
1570 this->task_manager->queue_ike(this->task_manager);
1571 return this->task_manager->initiate(this->task_manager);
1572 }
1573 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1574 break;
1575 }
1576 case IKE_DELETING:
1577 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1578 break;
1579 case IKE_REKEYING:
1580 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1581 /* FALL */
1582 default:
1583 reestablish(this);
1584 break;
1585 }
1586 return DESTROY_ME;
1587 }
1588 return SUCCESS;
1589 }
1590
1591 METHOD(ike_sa_t, set_auth_lifetime, status_t,
1592 private_ike_sa_t *this, u_int32_t lifetime)
1593 {
1594 u_int32_t diff, hard, soft, now;
1595 ike_auth_lifetime_t *task;
1596 bool send_update;
1597
1598 diff = this->peer_cfg->get_over_time(this->peer_cfg);
1599 now = time_monotonic(NULL);
1600 hard = now + lifetime;
1601 soft = hard - diff;
1602
1603 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1604 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1605 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
1606 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
1607 (this->other_virtual_ip != NULL ||
1608 has_condition(this, COND_EAP_AUTHENTICATED));
1609
1610 if (lifetime < diff)
1611 {
1612 this->stats[STAT_REAUTH] = now;
1613
1614 if (!send_update)
1615 {
1616 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1617 "starting reauthentication", lifetime);
1618 lib->processor->queue_job(lib->processor,
1619 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1620 }
1621 }
1622 else if (this->stats[STAT_REAUTH] == 0 ||
1623 this->stats[STAT_REAUTH] > soft)
1624 {
1625 this->stats[STAT_REAUTH] = soft;
1626 if (!send_update)
1627 {
1628 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
1629 "reauthentication in %ds", lifetime, lifetime - diff);
1630 lib->scheduler->schedule_job(lib->scheduler,
1631 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1632 lifetime - diff);
1633 }
1634 }
1635 else
1636 {
1637 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1638 "reauthentication already scheduled in %ds", lifetime,
1639 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1640 send_update = FALSE;
1641 }
1642 /* give at least some seconds to reauthenticate */
1643 this->stats[STAT_DELETE] = max(hard, now + 10);
1644
1645 if (send_update)
1646 {
1647 task = ike_auth_lifetime_create(&this->public, TRUE);
1648 this->task_manager->queue_task(this->task_manager, &task->task);
1649 return this->task_manager->initiate(this->task_manager);
1650 }
1651 return SUCCESS;
1652 }
1653
1654 /**
1655 * Check if the current combination of source and destination address is still
1656 * valid.
1657 */
1658 static bool is_current_path_valid(private_ike_sa_t *this)
1659 {
1660 bool valid = FALSE;
1661 host_t *src;
1662 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1663 this->other_host, this->my_host);
1664 if (src)
1665 {
1666 if (src->ip_equals(src, this->my_host))
1667 {
1668 valid = TRUE;
1669 }
1670 src->destroy(src);
1671 }
1672 return valid;
1673 }
1674
1675 /**
1676 * Check if we have any path avialable for this IKE SA.
1677 */
1678 static bool is_any_path_valid(private_ike_sa_t *this)
1679 {
1680 bool valid = FALSE;
1681 enumerator_t *enumerator;
1682 host_t *src = NULL, *addr;
1683
1684 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1685 enumerator = this->peer_addresses->create_enumerator(this->peer_addresses);
1686 while (enumerator->enumerate(enumerator, &addr))
1687 {
1688 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1689 src = hydra->kernel_interface->get_source_addr(
1690 hydra->kernel_interface, addr, NULL);
1691 if (src)
1692 {
1693 break;
1694 }
1695 }
1696 enumerator->destroy(enumerator);
1697 if (src)
1698 {
1699 valid = TRUE;
1700 src->destroy(src);
1701 }
1702 return valid;
1703 }
1704
1705 METHOD(ike_sa_t, roam, status_t,
1706 private_ike_sa_t *this, bool address)
1707 {
1708 switch (this->state)
1709 {
1710 case IKE_CREATED:
1711 case IKE_DELETING:
1712 case IKE_DESTROYING:
1713 case IKE_PASSIVE:
1714 return SUCCESS;
1715 default:
1716 break;
1717 }
1718
1719 /* keep existing path if possible */
1720 if (is_current_path_valid(this))
1721 {
1722 DBG2(DBG_IKE, "keeping connection path %H - %H",
1723 this->my_host, this->other_host);
1724 set_condition(this, COND_STALE, FALSE);
1725
1726 if (supports_extension(this, EXT_MOBIKE) && address)
1727 { /* if any addresses changed, send an updated list */
1728 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1729 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
1730 return this->task_manager->initiate(this->task_manager);
1731 }
1732 return SUCCESS;
1733 }
1734
1735 if (!is_any_path_valid(this))
1736 {
1737 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1738 this->other_host);
1739 set_condition(this, COND_STALE, TRUE);
1740 return SUCCESS;
1741 }
1742 set_condition(this, COND_STALE, FALSE);
1743
1744 /* update addresses with mobike, if supported ... */
1745 if (supports_extension(this, EXT_MOBIKE))
1746 {
1747 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1748 { /* responder updates the peer about changed address config */
1749 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1750 "implicitly requesting an address change");
1751 address = TRUE;
1752 }
1753 else
1754 {
1755 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1756 }
1757 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
1758 return this->task_manager->initiate(this->task_manager);
1759 }
1760
1761 /* ... reauth if not */
1762 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1763 { /* responder does not reauthenticate */
1764 set_condition(this, COND_STALE, TRUE);
1765 return SUCCESS;
1766 }
1767 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1768 return reauth(this);
1769 }
1770
1771 METHOD(ike_sa_t, add_configuration_attribute, void,
1772 private_ike_sa_t *this, attribute_handler_t *handler,
1773 configuration_attribute_type_t type, chunk_t data)
1774 {
1775 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1776
1777 entry->handler = handler;
1778 entry->type = type;
1779 entry->data = chunk_clone(data);
1780
1781 this->attributes->insert_last(this->attributes, entry);
1782 }
1783
1784 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1785 private_ike_sa_t *this, task_queue_t queue)
1786 {
1787 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1788 }
1789
1790 METHOD(ike_sa_t, queue_task, void,
1791 private_ike_sa_t *this, task_t *task)
1792 {
1793 this->task_manager->queue_task(this->task_manager, task);
1794 }
1795
1796 METHOD(ike_sa_t, inherit, void,
1797 private_ike_sa_t *this, ike_sa_t *other_public)
1798 {
1799 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1800 child_sa_t *child_sa;
1801 attribute_entry_t *entry;
1802 enumerator_t *enumerator;
1803 auth_cfg_t *cfg;
1804
1805 /* apply hosts and ids */
1806 this->my_host->destroy(this->my_host);
1807 this->other_host->destroy(this->other_host);
1808 this->my_id->destroy(this->my_id);
1809 this->other_id->destroy(this->other_id);
1810 this->my_host = other->my_host->clone(other->my_host);
1811 this->other_host = other->other_host->clone(other->other_host);
1812 this->my_id = other->my_id->clone(other->my_id);
1813 this->other_id = other->other_id->clone(other->other_id);
1814
1815 /* apply virtual assigned IPs... */
1816 if (other->my_virtual_ip)
1817 {
1818 this->my_virtual_ip = other->my_virtual_ip;
1819 other->my_virtual_ip = NULL;
1820 }
1821 if (other->other_virtual_ip)
1822 {
1823 this->other_virtual_ip = other->other_virtual_ip;
1824 other->other_virtual_ip = NULL;
1825 }
1826
1827 /* authentication information */
1828 enumerator = other->my_auths->create_enumerator(other->my_auths);
1829 while (enumerator->enumerate(enumerator, &cfg))
1830 {
1831 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1832 }
1833 enumerator->destroy(enumerator);
1834 enumerator = other->other_auths->create_enumerator(other->other_auths);
1835 while (enumerator->enumerate(enumerator, &cfg))
1836 {
1837 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1838 }
1839 enumerator->destroy(enumerator);
1840
1841 /* ... and configuration attributes */
1842 while (other->attributes->remove_last(other->attributes,
1843 (void**)&entry) == SUCCESS)
1844 {
1845 this->attributes->insert_first(this->attributes, entry);
1846 }
1847
1848 /* inherit all conditions */
1849 this->conditions = other->conditions;
1850 if (this->conditions & COND_NAT_HERE)
1851 {
1852 send_keepalive(this);
1853 }
1854
1855 #ifdef ME
1856 if (other->is_mediation_server)
1857 {
1858 act_as_mediation_server(this);
1859 }
1860 else if (other->server_reflexive_host)
1861 {
1862 this->server_reflexive_host = other->server_reflexive_host->clone(
1863 other->server_reflexive_host);
1864 }
1865 #endif /* ME */
1866
1867 /* adopt all children */
1868 while (other->child_sas->remove_last(other->child_sas,
1869 (void**)&child_sa) == SUCCESS)
1870 {
1871 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1872 }
1873
1874 /* move pending tasks to the new IKE_SA */
1875 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1876
1877 /* reauthentication timeout survives a rekeying */
1878 if (other->stats[STAT_REAUTH])
1879 {
1880 time_t reauth, delete, now = time_monotonic(NULL);
1881
1882 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
1883 reauth = this->stats[STAT_REAUTH] - now;
1884 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
1885 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
1886 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
1887 "lifetime reduced to %ds", reauth, delete);
1888 lib->scheduler->schedule_job(lib->scheduler,
1889 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
1890 lib->scheduler->schedule_job(lib->scheduler,
1891 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
1892 }
1893 }
1894
1895 METHOD(ike_sa_t, destroy, void,
1896 private_ike_sa_t *this)
1897 {
1898 attribute_entry_t *entry;
1899
1900 charon->bus->set_sa(charon->bus, &this->public);
1901
1902 set_state(this, IKE_DESTROYING);
1903 DESTROY_IF(this->task_manager);
1904
1905 /* remove attributes first, as we pass the IKE_SA to the handler */
1906 while (this->attributes->remove_last(this->attributes,
1907 (void**)&entry) == SUCCESS)
1908 {
1909 hydra->attributes->release(hydra->attributes, entry->handler,
1910 this->other_id, entry->type, entry->data);
1911 free(entry->data.ptr);
1912 free(entry);
1913 }
1914 this->attributes->destroy(this->attributes);
1915
1916 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1917
1918 /* unset SA after here to avoid usage by the listeners */
1919 charon->bus->set_sa(charon->bus, NULL);
1920
1921 DESTROY_IF(this->keymat);
1922
1923 if (this->my_virtual_ip)
1924 {
1925 hydra->kernel_interface->del_ip(hydra->kernel_interface,
1926 this->my_virtual_ip);
1927 this->my_virtual_ip->destroy(this->my_virtual_ip);
1928 }
1929 if (this->other_virtual_ip)
1930 {
1931 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
1932 {
1933 hydra->attributes->release_address(hydra->attributes,
1934 this->peer_cfg->get_pool(this->peer_cfg),
1935 this->other_virtual_ip, get_other_eap_id(this));
1936 }
1937 this->other_virtual_ip->destroy(this->other_virtual_ip);
1938 }
1939 this->peer_addresses->destroy_offset(this->peer_addresses,
1940 offsetof(host_t, destroy));
1941 #ifdef ME
1942 if (this->is_mediation_server)
1943 {
1944 charon->mediation_manager->remove(charon->mediation_manager,
1945 this->ike_sa_id);
1946 }
1947 DESTROY_IF(this->server_reflexive_host);
1948 chunk_free(&this->connect_id);
1949 #endif /* ME */
1950 free(this->nat_detection_dest.ptr);
1951
1952 DESTROY_IF(this->my_host);
1953 DESTROY_IF(this->other_host);
1954 DESTROY_IF(this->my_id);
1955 DESTROY_IF(this->other_id);
1956 DESTROY_IF(this->local_host);
1957 DESTROY_IF(this->remote_host);
1958
1959 DESTROY_IF(this->ike_cfg);
1960 DESTROY_IF(this->peer_cfg);
1961 DESTROY_IF(this->proposal);
1962 this->my_auth->destroy(this->my_auth);
1963 this->other_auth->destroy(this->other_auth);
1964 this->my_auths->destroy_offset(this->my_auths,
1965 offsetof(auth_cfg_t, destroy));
1966 this->other_auths->destroy_offset(this->other_auths,
1967 offsetof(auth_cfg_t, destroy));
1968
1969 this->ike_sa_id->destroy(this->ike_sa_id);
1970 free(this);
1971 }
1972
1973 /*
1974 * Described in header.
1975 */
1976 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
1977 ike_version_t version)
1978 {
1979 private_ike_sa_t *this;
1980 static u_int32_t unique_id = 0;
1981
1982 if (version == IKE_ANY)
1983 { /* prefer IKEv2 if protocol not specified */
1984 #ifdef USE_IKEV2
1985 version = IKEV2;
1986 #else
1987 version = IKEV1;
1988 #endif
1989 }
1990
1991 INIT(this,
1992 .public = {
1993 .get_version = _get_version,
1994 .get_state = _get_state,
1995 .set_state = _set_state,
1996 .get_name = _get_name,
1997 .get_statistic = _get_statistic,
1998 .set_statistic = _set_statistic,
1999 .process_message = _process_message,
2000 .initiate = _initiate,
2001 .get_ike_cfg = _get_ike_cfg,
2002 .set_ike_cfg = _set_ike_cfg,
2003 .get_peer_cfg = _get_peer_cfg,
2004 .set_peer_cfg = _set_peer_cfg,
2005 .get_auth_cfg = _get_auth_cfg,
2006 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2007 .add_auth_cfg = _add_auth_cfg,
2008 .get_proposal = _get_proposal,
2009 .set_proposal = _set_proposal,
2010 .get_id = _get_id,
2011 .get_my_host = _get_my_host,
2012 .set_my_host = _set_my_host,
2013 .get_other_host = _get_other_host,
2014 .set_other_host = _set_other_host,
2015 .set_message_id = _set_message_id,
2016 .float_ports = _float_ports,
2017 .update_hosts = _update_hosts,
2018 .get_my_id = _get_my_id,
2019 .set_my_id = _set_my_id,
2020 .get_other_id = _get_other_id,
2021 .set_other_id = _set_other_id,
2022 .get_other_eap_id = _get_other_eap_id,
2023 .enable_extension = _enable_extension,
2024 .supports_extension = _supports_extension,
2025 .set_condition = _set_condition,
2026 .has_condition = _has_condition,
2027 .set_pending_updates = _set_pending_updates,
2028 .get_pending_updates = _get_pending_updates,
2029 .create_peer_address_enumerator = _create_peer_address_enumerator,
2030 .add_peer_address = _add_peer_address,
2031 .clear_peer_addresses = _clear_peer_addresses,
2032 .has_mapping_changed = _has_mapping_changed,
2033 .retransmit = _retransmit,
2034 .delete = _delete_,
2035 .destroy = _destroy,
2036 .send_dpd = _send_dpd,
2037 .send_keepalive = _send_keepalive,
2038 .get_keymat = _get_keymat,
2039 .add_child_sa = _add_child_sa,
2040 .get_child_sa = _get_child_sa,
2041 .get_child_count = _get_child_count,
2042 .create_child_sa_enumerator = _create_child_sa_enumerator,
2043 .remove_child_sa = _remove_child_sa,
2044 .rekey_child_sa = _rekey_child_sa,
2045 .delete_child_sa = _delete_child_sa,
2046 .destroy_child_sa = _destroy_child_sa,
2047 .rekey = _rekey,
2048 .reauth = _reauth,
2049 .reestablish = _reestablish,
2050 .set_auth_lifetime = _set_auth_lifetime,
2051 .roam = _roam,
2052 .inherit = _inherit,
2053 .generate_message = _generate_message,
2054 .reset = _reset,
2055 .get_unique_id = _get_unique_id,
2056 .set_virtual_ip = _set_virtual_ip,
2057 .get_virtual_ip = _get_virtual_ip,
2058 .add_configuration_attribute = _add_configuration_attribute,
2059 .set_kmaddress = _set_kmaddress,
2060 .create_task_enumerator = _create_task_enumerator,
2061 .queue_task = _queue_task,
2062 #ifdef ME
2063 .act_as_mediation_server = _act_as_mediation_server,
2064 .get_server_reflexive_host = _get_server_reflexive_host,
2065 .set_server_reflexive_host = _set_server_reflexive_host,
2066 .get_connect_id = _get_connect_id,
2067 .initiate_mediation = _initiate_mediation,
2068 .initiate_mediated = _initiate_mediated,
2069 .relay = _relay,
2070 .callback = _callback,
2071 .respond = _respond,
2072 #endif /* ME */
2073 },
2074 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2075 .version = version,
2076 .child_sas = linked_list_create(),
2077 .my_host = host_create_any(AF_INET),
2078 .other_host = host_create_any(AF_INET),
2079 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2080 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2081 .keymat = keymat_create(version, initiator),
2082 .state = IKE_CREATED,
2083 .stats[STAT_INBOUND] = time_monotonic(NULL),
2084 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2085 .my_auth = auth_cfg_create(),
2086 .other_auth = auth_cfg_create(),
2087 .my_auths = linked_list_create(),
2088 .other_auths = linked_list_create(),
2089 .unique_id = ++unique_id,
2090 .peer_addresses = linked_list_create(),
2091 .attributes = linked_list_create(),
2092 .keepalive_interval = lib->settings->get_time(lib->settings,
2093 "%s.keep_alive", KEEPALIVE_INTERVAL, charon->name),
2094 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2095 "%s.flush_auth_cfg", FALSE, charon->name),
2096 );
2097
2098 if (version == IKEV2)
2099 { /* always supported with IKEv2 */
2100 enable_extension(this, EXT_DPD);
2101 }
2102
2103 this->task_manager = task_manager_create(&this->public);
2104 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2105
2106 if (!this->task_manager || !this->keymat)
2107 {
2108 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2109 destroy(this);
2110 return NULL;
2111 }
2112 return &this->public;
2113 }