ikev1: Add fragmentation support for Windows peers
[strongswan.git] / src / libcharon / sa / ike_sa.c
1 /*
2 * Copyright (C) 2006-2014 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 /*
20 * Copyright (c) 2014 Volker RĂ¼melin
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44 #include <time.h>
45
46 #include "ike_sa.h"
47
48 #include <library.h>
49 #include <hydra.h>
50 #include <daemon.h>
51 #include <collections/array.h>
52 #include <utils/lexparser.h>
53 #include <processing/jobs/retransmit_job.h>
54 #include <processing/jobs/delete_ike_sa_job.h>
55 #include <processing/jobs/send_dpd_job.h>
56 #include <processing/jobs/send_keepalive_job.h>
57 #include <processing/jobs/rekey_ike_sa_job.h>
58 #include <processing/jobs/retry_initiate_job.h>
59 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
60
61 #ifdef ME
62 #include <sa/ikev2/tasks/ike_me.h>
63 #include <processing/jobs/initiate_mediation_job.h>
64 #endif
65
66 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DESTROYING,
67 "CREATED",
68 "CONNECTING",
69 "ESTABLISHED",
70 "PASSIVE",
71 "REKEYING",
72 "DELETING",
73 "DESTROYING",
74 );
75
76 typedef struct private_ike_sa_t private_ike_sa_t;
77 typedef struct attribute_entry_t attribute_entry_t;
78
79 /**
80 * Private data of an ike_sa_t object.
81 */
82 struct private_ike_sa_t {
83
84 /**
85 * Public members
86 */
87 ike_sa_t public;
88
89 /**
90 * Identifier for the current IKE_SA.
91 */
92 ike_sa_id_t *ike_sa_id;
93
94 /**
95 * IKE version of this SA.
96 */
97 ike_version_t version;
98
99 /**
100 * unique numerical ID for this IKE_SA.
101 */
102 u_int32_t unique_id;
103
104 /**
105 * Current state of the IKE_SA
106 */
107 ike_sa_state_t state;
108
109 /**
110 * IKE configuration used to set up this IKE_SA
111 */
112 ike_cfg_t *ike_cfg;
113
114 /**
115 * Peer and authentication information to establish IKE_SA.
116 */
117 peer_cfg_t *peer_cfg;
118
119 /**
120 * currently used authentication ruleset, local
121 */
122 auth_cfg_t *my_auth;
123
124 /**
125 * currently used authentication constraints, remote
126 */
127 auth_cfg_t *other_auth;
128
129 /**
130 * Array of completed local authentication rounds (as auth_cfg_t)
131 */
132 array_t *my_auths;
133
134 /**
135 * Array of completed remote authentication rounds (as auth_cfg_t)
136 */
137 array_t *other_auths;
138
139 /**
140 * Selected IKE proposal
141 */
142 proposal_t *proposal;
143
144 /**
145 * Juggles tasks to process messages
146 */
147 task_manager_t *task_manager;
148
149 /**
150 * Address of local host
151 */
152 host_t *my_host;
153
154 /**
155 * Address of remote host
156 */
157 host_t *other_host;
158
159 #ifdef ME
160 /**
161 * Are we mediation server
162 */
163 bool is_mediation_server;
164
165 /**
166 * Server reflexive host
167 */
168 host_t *server_reflexive_host;
169
170 /**
171 * Connect ID
172 */
173 chunk_t connect_id;
174 #endif /* ME */
175
176 /**
177 * Identification used for us
178 */
179 identification_t *my_id;
180
181 /**
182 * Identification used for other
183 */
184 identification_t *other_id;
185
186 /**
187 * set of extensions the peer supports
188 */
189 ike_extension_t extensions;
190
191 /**
192 * set of condition flags currently enabled for this IKE_SA
193 */
194 ike_condition_t conditions;
195
196 /**
197 * Array containing the child sa's of the current IKE_SA.
198 */
199 array_t *child_sas;
200
201 /**
202 * keymat of this IKE_SA
203 */
204 keymat_t *keymat;
205
206 /**
207 * Virtual IPs on local host
208 */
209 array_t *my_vips;
210
211 /**
212 * Virtual IPs on remote host
213 */
214 array_t *other_vips;
215
216 /**
217 * List of configuration attributes (attribute_entry_t)
218 */
219 array_t *attributes;
220
221 /**
222 * list of peer's addresses, additional ones transmitted via MOBIKE
223 */
224 array_t *peer_addresses;
225
226 /**
227 * previously value of received DESTINATION_IP hash
228 */
229 chunk_t nat_detection_dest;
230
231 /**
232 * number pending UPDATE_SA_ADDRESS (MOBIKE)
233 */
234 u_int32_t pending_updates;
235
236 /**
237 * NAT keep alive interval
238 */
239 u_int32_t keepalive_interval;
240
241 /**
242 * interval for retries during initiation (e.g. if DNS resolution failed),
243 * 0 to disable (default)
244 */
245 u_int32_t retry_initiate_interval;
246
247 /**
248 * TRUE if a retry_initiate_job has been queued
249 */
250 bool retry_initiate_queued;
251
252 /**
253 * Timestamps for this IKE_SA
254 */
255 u_int32_t stats[STAT_MAX];
256
257 /**
258 * how many times we have retried so far (keyingtries)
259 */
260 u_int32_t keyingtry;
261
262 /**
263 * local host address to be used for IKE, set via MIGRATE kernel message
264 */
265 host_t *local_host;
266
267 /**
268 * remote host address to be used for IKE, set via MIGRATE kernel message
269 */
270 host_t *remote_host;
271
272 /**
273 * Flush auth configs once established?
274 */
275 bool flush_auth_cfg;
276
277 /**
278 * Maximum length of a single fragment, 0 for address-specific defaults
279 */
280 size_t fragment_size;
281 };
282
283 /**
284 * Entry to maintain install configuration attributes during IKE_SA lifetime
285 */
286 struct attribute_entry_t {
287 /** handler used to install this attribute */
288 attribute_handler_t *handler;
289 /** attribute type */
290 configuration_attribute_type_t type;
291 /** attribute data */
292 chunk_t data;
293 };
294
295 /**
296 * get the time of the latest traffic processed by the kernel
297 */
298 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
299 {
300 enumerator_t *enumerator;
301 child_sa_t *child_sa;
302 time_t use_time, current;
303
304 if (inbound)
305 {
306 use_time = this->stats[STAT_INBOUND];
307 }
308 else
309 {
310 use_time = this->stats[STAT_OUTBOUND];
311 }
312
313 enumerator = array_create_enumerator(this->child_sas);
314 while (enumerator->enumerate(enumerator, &child_sa))
315 {
316 child_sa->get_usestats(child_sa, inbound, &current, NULL, NULL);
317 use_time = max(use_time, current);
318 }
319 enumerator->destroy(enumerator);
320
321 return use_time;
322 }
323
324 METHOD(ike_sa_t, get_unique_id, u_int32_t,
325 private_ike_sa_t *this)
326 {
327 return this->unique_id;
328 }
329
330 METHOD(ike_sa_t, get_name, char*,
331 private_ike_sa_t *this)
332 {
333 if (this->peer_cfg)
334 {
335 return this->peer_cfg->get_name(this->peer_cfg);
336 }
337 return "(unnamed)";
338 }
339
340 METHOD(ike_sa_t, get_statistic, u_int32_t,
341 private_ike_sa_t *this, statistic_t kind)
342 {
343 if (kind < STAT_MAX)
344 {
345 return this->stats[kind];
346 }
347 return 0;
348 }
349
350 METHOD(ike_sa_t, set_statistic, void,
351 private_ike_sa_t *this, statistic_t kind, u_int32_t value)
352 {
353 if (kind < STAT_MAX)
354 {
355 this->stats[kind] = value;
356 }
357 }
358
359 METHOD(ike_sa_t, get_my_host, host_t*,
360 private_ike_sa_t *this)
361 {
362 return this->my_host;
363 }
364
365 METHOD(ike_sa_t, set_my_host, void,
366 private_ike_sa_t *this, host_t *me)
367 {
368 DESTROY_IF(this->my_host);
369 this->my_host = me;
370 }
371
372 METHOD(ike_sa_t, get_other_host, host_t*,
373 private_ike_sa_t *this)
374 {
375 return this->other_host;
376 }
377
378 METHOD(ike_sa_t, set_other_host, void,
379 private_ike_sa_t *this, host_t *other)
380 {
381 DESTROY_IF(this->other_host);
382 this->other_host = other;
383 }
384
385 METHOD(ike_sa_t, get_peer_cfg, peer_cfg_t*,
386 private_ike_sa_t *this)
387 {
388 return this->peer_cfg;
389 }
390
391 METHOD(ike_sa_t, set_peer_cfg, void,
392 private_ike_sa_t *this, peer_cfg_t *peer_cfg)
393 {
394 peer_cfg->get_ref(peer_cfg);
395 DESTROY_IF(this->peer_cfg);
396 this->peer_cfg = peer_cfg;
397
398 if (this->ike_cfg == NULL)
399 {
400 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
401 this->ike_cfg->get_ref(this->ike_cfg);
402 }
403 }
404
405 METHOD(ike_sa_t, get_auth_cfg, auth_cfg_t*,
406 private_ike_sa_t *this, bool local)
407 {
408 if (local)
409 {
410 return this->my_auth;
411 }
412 return this->other_auth;
413 }
414
415 METHOD(ike_sa_t, add_auth_cfg, void,
416 private_ike_sa_t *this, bool local, auth_cfg_t *cfg)
417 {
418 if (local)
419 {
420 array_insert(this->my_auths, ARRAY_TAIL, cfg);
421 }
422 else
423 {
424 array_insert(this->other_auths, ARRAY_TAIL, cfg);
425 }
426 }
427
428 METHOD(ike_sa_t, create_auth_cfg_enumerator, enumerator_t*,
429 private_ike_sa_t *this, bool local)
430 {
431 if (local)
432 {
433 return array_create_enumerator(this->my_auths);
434 }
435 return array_create_enumerator(this->other_auths);
436 }
437
438 /**
439 * Flush the stored authentication round information
440 */
441 static void flush_auth_cfgs(private_ike_sa_t *this)
442 {
443 auth_cfg_t *cfg;
444
445 this->my_auth->purge(this->my_auth, FALSE);
446 this->other_auth->purge(this->other_auth, FALSE);
447
448 while (array_remove(this->my_auths, ARRAY_TAIL, &cfg))
449 {
450 cfg->destroy(cfg);
451 }
452 while (array_remove(this->other_auths, ARRAY_TAIL, &cfg))
453 {
454 cfg->destroy(cfg);
455 }
456 }
457
458 METHOD(ike_sa_t, get_proposal, proposal_t*,
459 private_ike_sa_t *this)
460 {
461 return this->proposal;
462 }
463
464 METHOD(ike_sa_t, set_proposal, void,
465 private_ike_sa_t *this, proposal_t *proposal)
466 {
467 DESTROY_IF(this->proposal);
468 this->proposal = proposal->clone(proposal);
469 }
470
471 METHOD(ike_sa_t, set_message_id, void,
472 private_ike_sa_t *this, bool initiate, u_int32_t mid)
473 {
474 if (initiate)
475 {
476 this->task_manager->reset(this->task_manager, mid, UINT_MAX);
477 }
478 else
479 {
480 this->task_manager->reset(this->task_manager, UINT_MAX, mid);
481 }
482 }
483
484 METHOD(ike_sa_t, send_keepalive, void,
485 private_ike_sa_t *this)
486 {
487 send_keepalive_job_t *job;
488 time_t last_out, now, diff;
489
490 if (!(this->conditions & COND_NAT_HERE) || this->keepalive_interval == 0)
491 { /* disable keep alives if we are not NATed anymore */
492 return;
493 }
494
495 last_out = get_use_time(this, FALSE);
496 now = time_monotonic(NULL);
497
498 diff = now - last_out;
499
500 if (diff >= this->keepalive_interval)
501 {
502 packet_t *packet;
503 chunk_t data;
504
505 packet = packet_create();
506 packet->set_source(packet, this->my_host->clone(this->my_host));
507 packet->set_destination(packet, this->other_host->clone(this->other_host));
508 data.ptr = malloc(1);
509 data.ptr[0] = 0xFF;
510 data.len = 1;
511 packet->set_data(packet, data);
512 DBG1(DBG_IKE, "sending keep alive to %#H", this->other_host);
513 charon->sender->send_no_marker(charon->sender, packet);
514 diff = 0;
515 }
516 job = send_keepalive_job_create(this->ike_sa_id);
517 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
518 this->keepalive_interval - diff);
519 }
520
521 METHOD(ike_sa_t, get_ike_cfg, ike_cfg_t*,
522 private_ike_sa_t *this)
523 {
524 return this->ike_cfg;
525 }
526
527 METHOD(ike_sa_t, set_ike_cfg, void,
528 private_ike_sa_t *this, ike_cfg_t *ike_cfg)
529 {
530 ike_cfg->get_ref(ike_cfg);
531 this->ike_cfg = ike_cfg;
532 }
533
534 METHOD(ike_sa_t, enable_extension, void,
535 private_ike_sa_t *this, ike_extension_t extension)
536 {
537 this->extensions |= extension;
538 }
539
540 METHOD(ike_sa_t, supports_extension, bool,
541 private_ike_sa_t *this, ike_extension_t extension)
542 {
543 return (this->extensions & extension) != FALSE;
544 }
545
546 METHOD(ike_sa_t, has_condition, bool,
547 private_ike_sa_t *this, ike_condition_t condition)
548 {
549 return (this->conditions & condition) != FALSE;
550 }
551
552 METHOD(ike_sa_t, set_condition, void,
553 private_ike_sa_t *this, ike_condition_t condition, bool enable)
554 {
555 if (has_condition(this, condition) != enable)
556 {
557 if (enable)
558 {
559 this->conditions |= condition;
560 switch (condition)
561 {
562 case COND_NAT_HERE:
563 DBG1(DBG_IKE, "local host is behind NAT, sending keep alives");
564 this->conditions |= COND_NAT_ANY;
565 send_keepalive(this);
566 break;
567 case COND_NAT_THERE:
568 DBG1(DBG_IKE, "remote host is behind NAT");
569 this->conditions |= COND_NAT_ANY;
570 break;
571 case COND_NAT_FAKE:
572 DBG1(DBG_IKE, "faking NAT situation to enforce UDP encapsulation");
573 this->conditions |= COND_NAT_ANY;
574 break;
575 default:
576 break;
577 }
578 }
579 else
580 {
581 this->conditions &= ~condition;
582 switch (condition)
583 {
584 case COND_NAT_HERE:
585 case COND_NAT_FAKE:
586 case COND_NAT_THERE:
587 set_condition(this, COND_NAT_ANY,
588 has_condition(this, COND_NAT_HERE) ||
589 has_condition(this, COND_NAT_THERE) ||
590 has_condition(this, COND_NAT_FAKE));
591 break;
592 default:
593 break;
594 }
595 }
596 }
597 }
598
599 METHOD(ike_sa_t, send_dpd, status_t,
600 private_ike_sa_t *this)
601 {
602 job_t *job;
603 time_t diff, delay;
604 bool task_queued = FALSE;
605
606 if (this->state == IKE_PASSIVE)
607 {
608 return INVALID_STATE;
609 }
610 delay = this->peer_cfg->get_dpd(this->peer_cfg);
611 if (this->task_manager->busy(this->task_manager))
612 {
613 /* an exchange is in the air, no need to start a DPD check */
614 diff = 0;
615 }
616 else
617 {
618 /* check if there was any inbound traffic */
619 time_t last_in, now;
620 last_in = get_use_time(this, TRUE);
621 now = time_monotonic(NULL);
622 diff = now - last_in;
623 if (!delay || diff >= delay)
624 {
625 /* too long ago, initiate dead peer detection */
626 DBG1(DBG_IKE, "sending DPD request");
627 this->task_manager->queue_dpd(this->task_manager);
628 task_queued = TRUE;
629 diff = 0;
630 }
631 }
632 /* recheck in "interval" seconds */
633 if (delay)
634 {
635 job = (job_t*)send_dpd_job_create(this->ike_sa_id);
636 lib->scheduler->schedule_job(lib->scheduler, job, delay - diff);
637 }
638 if (task_queued)
639 {
640 return this->task_manager->initiate(this->task_manager);
641 }
642 return SUCCESS;
643 }
644
645 METHOD(ike_sa_t, get_state, ike_sa_state_t,
646 private_ike_sa_t *this)
647 {
648 return this->state;
649 }
650
651 METHOD(ike_sa_t, set_state, void,
652 private_ike_sa_t *this, ike_sa_state_t state)
653 {
654 bool trigger_dpd = FALSE;
655
656 DBG2(DBG_IKE, "IKE_SA %s[%d] state change: %N => %N",
657 get_name(this), this->unique_id,
658 ike_sa_state_names, this->state,
659 ike_sa_state_names, state);
660
661 switch (state)
662 {
663 case IKE_ESTABLISHED:
664 {
665 if (this->state == IKE_CONNECTING ||
666 this->state == IKE_PASSIVE)
667 {
668 job_t *job;
669 u_int32_t t;
670
671 /* calculate rekey, reauth and lifetime */
672 this->stats[STAT_ESTABLISHED] = time_monotonic(NULL);
673
674 /* schedule rekeying if we have a time which is smaller than
675 * an already scheduled rekeying */
676 t = this->peer_cfg->get_rekey_time(this->peer_cfg, TRUE);
677 if (t && (this->stats[STAT_REKEY] == 0 ||
678 (this->stats[STAT_REKEY] > t + this->stats[STAT_ESTABLISHED])))
679 {
680 this->stats[STAT_REKEY] = t + this->stats[STAT_ESTABLISHED];
681 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, FALSE);
682 lib->scheduler->schedule_job(lib->scheduler, job, t);
683 DBG1(DBG_IKE, "scheduling rekeying in %ds", t);
684 }
685 t = this->peer_cfg->get_reauth_time(this->peer_cfg, TRUE);
686 if (t && (this->stats[STAT_REAUTH] == 0 ||
687 (this->stats[STAT_REAUTH] > t + this->stats[STAT_ESTABLISHED])))
688 {
689 this->stats[STAT_REAUTH] = t + this->stats[STAT_ESTABLISHED];
690 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE);
691 lib->scheduler->schedule_job(lib->scheduler, job, t);
692 DBG1(DBG_IKE, "scheduling reauthentication in %ds", t);
693 }
694 t = this->peer_cfg->get_over_time(this->peer_cfg);
695 if (this->stats[STAT_REKEY] || this->stats[STAT_REAUTH])
696 {
697 if (this->stats[STAT_REAUTH] == 0)
698 {
699 this->stats[STAT_DELETE] = this->stats[STAT_REKEY];
700 }
701 else if (this->stats[STAT_REKEY] == 0)
702 {
703 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH];
704 }
705 else
706 {
707 this->stats[STAT_DELETE] = min(this->stats[STAT_REKEY],
708 this->stats[STAT_REAUTH]);
709 }
710 this->stats[STAT_DELETE] += t;
711 t = this->stats[STAT_DELETE] - this->stats[STAT_ESTABLISHED];
712 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
713 lib->scheduler->schedule_job(lib->scheduler, job, t);
714 DBG1(DBG_IKE, "maximum IKE_SA lifetime %ds", t);
715 }
716 trigger_dpd = this->peer_cfg->get_dpd(this->peer_cfg);
717 if (trigger_dpd)
718 {
719 /* Some peers delay the DELETE after rekeying an IKE_SA.
720 * If this delay is longer than our DPD delay, we would
721 * send a DPD request here. The IKE_SA is not ready to do
722 * so yet, so prevent that. */
723 this->stats[STAT_INBOUND] = this->stats[STAT_ESTABLISHED];
724 }
725 }
726 break;
727 }
728 default:
729 break;
730 }
731 charon->bus->ike_state_change(charon->bus, &this->public, state);
732 this->state = state;
733
734 if (trigger_dpd)
735 {
736 if (supports_extension(this, EXT_DPD))
737 {
738 send_dpd(this);
739 }
740 else
741 {
742 DBG1(DBG_IKE, "DPD not supported by peer, disabled");
743 }
744 }
745 }
746
747 METHOD(ike_sa_t, reset, void,
748 private_ike_sa_t *this)
749 {
750 /* the responder ID is reset, as peer may choose another one */
751 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
752 {
753 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
754 }
755
756 set_state(this, IKE_CREATED);
757
758 flush_auth_cfgs(this);
759
760 this->keymat->destroy(this->keymat);
761 this->keymat = keymat_create(this->version,
762 this->ike_sa_id->is_initiator(this->ike_sa_id));
763
764 this->task_manager->reset(this->task_manager, 0, 0);
765 }
766
767 METHOD(ike_sa_t, get_keymat, keymat_t*,
768 private_ike_sa_t *this)
769 {
770 return this->keymat;
771 }
772
773 METHOD(ike_sa_t, add_virtual_ip, void,
774 private_ike_sa_t *this, bool local, host_t *ip)
775 {
776 if (local)
777 {
778 char *iface;
779
780 if (hydra->kernel_interface->get_interface(hydra->kernel_interface,
781 this->my_host, &iface))
782 {
783 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
784 if (hydra->kernel_interface->add_ip(hydra->kernel_interface,
785 ip, -1, iface) == SUCCESS)
786 {
787 array_insert_create(&this->my_vips, ARRAY_TAIL, ip->clone(ip));
788 }
789 else
790 {
791 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
792 }
793 free(iface);
794 }
795 else
796 {
797 DBG1(DBG_IKE, "looking up interface for virtual IP %H failed", ip);
798 }
799 }
800 else
801 {
802 array_insert_create(&this->other_vips, ARRAY_TAIL, ip->clone(ip));
803 }
804 }
805
806
807 METHOD(ike_sa_t, clear_virtual_ips, void,
808 private_ike_sa_t *this, bool local)
809 {
810 array_t *vips;
811 host_t *vip;
812
813 vips = local ? this->my_vips : this->other_vips;
814 if (!local && array_count(vips))
815 {
816 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
817 }
818 while (array_remove(vips, ARRAY_HEAD, &vip))
819 {
820 if (local)
821 {
822 hydra->kernel_interface->del_ip(hydra->kernel_interface,
823 vip, -1, TRUE);
824 }
825 vip->destroy(vip);
826 }
827 }
828
829 METHOD(ike_sa_t, create_virtual_ip_enumerator, enumerator_t*,
830 private_ike_sa_t *this, bool local)
831 {
832 if (local)
833 {
834 return array_create_enumerator(this->my_vips);
835 }
836 return array_create_enumerator(this->other_vips);
837 }
838
839 METHOD(ike_sa_t, add_peer_address, void,
840 private_ike_sa_t *this, host_t *host)
841 {
842 array_insert_create(&this->peer_addresses, ARRAY_TAIL, host);
843 }
844
845 METHOD(ike_sa_t, create_peer_address_enumerator, enumerator_t*,
846 private_ike_sa_t *this)
847 {
848 if (this->peer_addresses)
849 {
850 return array_create_enumerator(this->peer_addresses);
851 }
852 /* in case we don't have MOBIKE */
853 return enumerator_create_single(this->other_host, NULL);
854 }
855
856 METHOD(ike_sa_t, clear_peer_addresses, void,
857 private_ike_sa_t *this)
858 {
859 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
860 this->peer_addresses = NULL;
861 }
862
863 METHOD(ike_sa_t, has_mapping_changed, bool,
864 private_ike_sa_t *this, chunk_t hash)
865 {
866 if (this->nat_detection_dest.ptr == NULL)
867 {
868 this->nat_detection_dest = chunk_clone(hash);
869 return FALSE;
870 }
871 if (chunk_equals(hash, this->nat_detection_dest))
872 {
873 return FALSE;
874 }
875 free(this->nat_detection_dest.ptr);
876 this->nat_detection_dest = chunk_clone(hash);
877 return TRUE;
878 }
879
880 METHOD(ike_sa_t, set_pending_updates, void,
881 private_ike_sa_t *this, u_int32_t updates)
882 {
883 this->pending_updates = updates;
884 }
885
886 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
887 private_ike_sa_t *this)
888 {
889 return this->pending_updates;
890 }
891
892 METHOD(ike_sa_t, float_ports, void,
893 private_ike_sa_t *this)
894 {
895 /* do not switch if we have a custom port from MOBIKE/NAT */
896 if (this->my_host->get_port(this->my_host) ==
897 charon->socket->get_port(charon->socket, FALSE))
898 {
899 this->my_host->set_port(this->my_host,
900 charon->socket->get_port(charon->socket, TRUE));
901 }
902 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
903 {
904 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
905 }
906 }
907
908 METHOD(ike_sa_t, update_hosts, void,
909 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
910 {
911 bool update = FALSE;
912
913 if (me == NULL)
914 {
915 me = this->my_host;
916 }
917 if (other == NULL)
918 {
919 other = this->other_host;
920 }
921
922 /* apply hosts on first received message */
923 if (this->my_host->is_anyaddr(this->my_host) ||
924 this->other_host->is_anyaddr(this->other_host))
925 {
926 set_my_host(this, me->clone(me));
927 set_other_host(this, other->clone(other));
928 update = TRUE;
929 }
930 else
931 {
932 /* update our address in any case */
933 if (force && !me->equals(me, this->my_host))
934 {
935 set_my_host(this, me->clone(me));
936 update = TRUE;
937 }
938
939 if (!other->equals(other, this->other_host))
940 {
941 /* update others address if we are NOT NATed */
942 if ((has_condition(this, COND_NAT_THERE) &&
943 !has_condition(this, COND_NAT_HERE)) || force )
944 {
945 set_other_host(this, other->clone(other));
946 update = TRUE;
947 }
948 }
949 }
950
951 /* update all associated CHILD_SAs, if required */
952 if (update)
953 {
954 enumerator_t *enumerator;
955 child_sa_t *child_sa;
956 linked_list_t *vips;
957
958 vips = linked_list_create_from_enumerator(
959 array_create_enumerator(this->my_vips));
960
961 enumerator = array_create_enumerator(this->child_sas);
962 while (enumerator->enumerate(enumerator, &child_sa))
963 {
964 if (child_sa->update(child_sa, this->my_host, this->other_host,
965 vips, has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
966 {
967 this->public.rekey_child_sa(&this->public,
968 child_sa->get_protocol(child_sa),
969 child_sa->get_spi(child_sa, TRUE));
970 }
971 }
972 enumerator->destroy(enumerator);
973
974 vips->destroy(vips);
975 }
976 }
977
978 /**
979 * Set configured DSCP value on packet
980 */
981 static void set_dscp(private_ike_sa_t *this, packet_t *packet)
982 {
983 ike_cfg_t *ike_cfg;
984
985 /* prefer IKE config on peer_cfg, as its selection is more accurate
986 * then the initial IKE config */
987 if (this->peer_cfg)
988 {
989 ike_cfg = this->peer_cfg->get_ike_cfg(this->peer_cfg);
990 }
991 else
992 {
993 ike_cfg = this->ike_cfg;
994 }
995 if (ike_cfg)
996 {
997 packet->set_dscp(packet, ike_cfg->get_dscp(ike_cfg));
998 }
999 }
1000
1001 METHOD(ike_sa_t, generate_message, status_t,
1002 private_ike_sa_t *this, message_t *message, packet_t **packet)
1003 {
1004 status_t status;
1005
1006 if (message->is_encoded(message))
1007 { /* already encoded in task, but set DSCP value */
1008 *packet = message->get_packet(message);
1009 set_dscp(this, *packet);
1010 return SUCCESS;
1011 }
1012 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1013 message->set_ike_sa_id(message, this->ike_sa_id);
1014 charon->bus->message(charon->bus, message, FALSE, TRUE);
1015 status = message->generate(message, this->keymat, packet);
1016 if (status == SUCCESS)
1017 {
1018 set_dscp(this, *packet);
1019 charon->bus->message(charon->bus, message, FALSE, FALSE);
1020 }
1021 return status;
1022 }
1023
1024 static bool filter_fragments(private_ike_sa_t *this, packet_t **fragment,
1025 packet_t **packet)
1026 {
1027 *packet = (*fragment)->clone(*fragment);
1028 set_dscp(this, *packet);
1029 return TRUE;
1030 }
1031
1032 METHOD(ike_sa_t, generate_message_fragmented, status_t,
1033 private_ike_sa_t *this, message_t *message, enumerator_t **packets)
1034 {
1035 enumerator_t *fragments;
1036 packet_t *packet;
1037 status_t status;
1038 bool use_frags = FALSE;
1039
1040 if (this->ike_cfg)
1041 {
1042 switch (this->ike_cfg->fragmentation(this->ike_cfg))
1043 {
1044 case FRAGMENTATION_FORCE:
1045 use_frags = TRUE;
1046 break;
1047 case FRAGMENTATION_YES:
1048 use_frags = supports_extension(this, EXT_IKE_FRAGMENTATION);
1049 if (use_frags && this->version == IKEV1 &&
1050 supports_extension(this, EXT_MS_WINDOWS))
1051 {
1052 /* It seems Windows 7 and 8 peers only accept proprietary
1053 * fragmented messages if they expect certificates. */
1054 use_frags = message->get_payload(message,
1055 PLV1_CERTIFICATE) != NULL;
1056 }
1057 break;
1058 default:
1059 break;
1060 }
1061 }
1062 if (!use_frags)
1063 {
1064 status = generate_message(this, message, &packet);
1065 if (status != SUCCESS)
1066 {
1067 return status;
1068 }
1069 *packets = enumerator_create_single(packet, NULL);
1070 return SUCCESS;
1071 }
1072
1073 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1074 message->set_ike_sa_id(message, this->ike_sa_id);
1075 charon->bus->message(charon->bus, message, FALSE, TRUE);
1076 status = message->fragment(message, this->keymat, this->fragment_size,
1077 &fragments);
1078 if (status == SUCCESS)
1079 {
1080 charon->bus->message(charon->bus, message, FALSE, FALSE);
1081 *packets = enumerator_create_filter(fragments, (void*)filter_fragments,
1082 this, NULL);
1083 }
1084 return status;
1085 }
1086
1087 METHOD(ike_sa_t, set_kmaddress, void,
1088 private_ike_sa_t *this, host_t *local, host_t *remote)
1089 {
1090 DESTROY_IF(this->local_host);
1091 DESTROY_IF(this->remote_host);
1092 this->local_host = local->clone(local);
1093 this->remote_host = remote->clone(remote);
1094 }
1095
1096 #ifdef ME
1097 METHOD(ike_sa_t, act_as_mediation_server, void,
1098 private_ike_sa_t *this)
1099 {
1100 charon->mediation_manager->update_sa_id(charon->mediation_manager,
1101 this->other_id, this->ike_sa_id);
1102 this->is_mediation_server = TRUE;
1103 }
1104
1105 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
1106 private_ike_sa_t *this)
1107 {
1108 return this->server_reflexive_host;
1109 }
1110
1111 METHOD(ike_sa_t, set_server_reflexive_host, void,
1112 private_ike_sa_t *this, host_t *host)
1113 {
1114 DESTROY_IF(this->server_reflexive_host);
1115 this->server_reflexive_host = host;
1116 }
1117
1118 METHOD(ike_sa_t, get_connect_id, chunk_t,
1119 private_ike_sa_t *this)
1120 {
1121 return this->connect_id;
1122 }
1123
1124 METHOD(ike_sa_t, respond, status_t,
1125 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
1126 {
1127 ike_me_t *task = ike_me_create(&this->public, TRUE);
1128 task->respond(task, peer_id, connect_id);
1129 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1130 return this->task_manager->initiate(this->task_manager);
1131 }
1132
1133 METHOD(ike_sa_t, callback, status_t,
1134 private_ike_sa_t *this, identification_t *peer_id)
1135 {
1136 ike_me_t *task = ike_me_create(&this->public, TRUE);
1137 task->callback(task, peer_id);
1138 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1139 return this->task_manager->initiate(this->task_manager);
1140 }
1141
1142 METHOD(ike_sa_t, relay, status_t,
1143 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
1144 chunk_t connect_key, linked_list_t *endpoints, bool response)
1145 {
1146 ike_me_t *task = ike_me_create(&this->public, TRUE);
1147 task->relay(task, requester, connect_id, connect_key, endpoints, response);
1148 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1149 return this->task_manager->initiate(this->task_manager);
1150 }
1151
1152 METHOD(ike_sa_t, initiate_mediation, status_t,
1153 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1154 {
1155 ike_me_t *task = ike_me_create(&this->public, TRUE);
1156 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1157 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1158 return this->task_manager->initiate(this->task_manager);
1159 }
1160
1161 METHOD(ike_sa_t, initiate_mediated, status_t,
1162 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1163 {
1164 set_my_host(this, me->clone(me));
1165 set_other_host(this, other->clone(other));
1166 chunk_free(&this->connect_id);
1167 this->connect_id = chunk_clone(connect_id);
1168 return this->task_manager->initiate(this->task_manager);
1169 }
1170 #endif /* ME */
1171
1172 /**
1173 * Resolve DNS host in configuration
1174 */
1175 static void resolve_hosts(private_ike_sa_t *this)
1176 {
1177 host_t *host;
1178 int family = AF_UNSPEC;
1179
1180 switch (charon->socket->supported_families(charon->socket))
1181 {
1182 case SOCKET_FAMILY_IPV4:
1183 family = AF_INET;
1184 break;
1185 case SOCKET_FAMILY_IPV6:
1186 family = AF_INET6;
1187 break;
1188 case SOCKET_FAMILY_BOTH:
1189 case SOCKET_FAMILY_NONE:
1190 break;
1191 }
1192
1193 if (this->remote_host)
1194 {
1195 host = this->remote_host->clone(this->remote_host);
1196 host->set_port(host, IKEV2_UDP_PORT);
1197 }
1198 else
1199 {
1200 host = this->ike_cfg->resolve_other(this->ike_cfg, family);
1201 }
1202 if (host)
1203 {
1204 set_other_host(this, host);
1205 }
1206
1207 if (this->local_host)
1208 {
1209 host = this->local_host->clone(this->local_host);
1210 host->set_port(host, charon->socket->get_port(charon->socket, FALSE));
1211 }
1212 else
1213 {
1214 /* use same address family as for other */
1215 if (!this->other_host->is_anyaddr(this->other_host))
1216 {
1217 family = this->other_host->get_family(this->other_host);
1218 }
1219 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1220
1221 if (host && host->is_anyaddr(host) &&
1222 !this->other_host->is_anyaddr(this->other_host))
1223 {
1224 host->destroy(host);
1225 host = hydra->kernel_interface->get_source_addr(
1226 hydra->kernel_interface, this->other_host, NULL);
1227 if (host)
1228 {
1229 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1230 }
1231 else
1232 { /* fallback to address family specific %any(6), if configured */
1233 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1234 }
1235 }
1236 }
1237 if (host)
1238 {
1239 set_my_host(this, host);
1240 }
1241 }
1242
1243 METHOD(ike_sa_t, initiate, status_t,
1244 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1245 traffic_selector_t *tsi, traffic_selector_t *tsr)
1246 {
1247 bool defer_initiate = FALSE;
1248
1249 if (this->state == IKE_CREATED)
1250 {
1251 if (this->my_host->is_anyaddr(this->my_host) ||
1252 this->other_host->is_anyaddr(this->other_host))
1253 {
1254 resolve_hosts(this);
1255 }
1256
1257 if (this->other_host->is_anyaddr(this->other_host)
1258 #ifdef ME
1259 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1260 #endif /* ME */
1261 )
1262 {
1263 char *addr;
1264
1265 addr = this->ike_cfg->get_other_addr(this->ike_cfg);
1266 if (!this->retry_initiate_interval)
1267 {
1268 DBG1(DBG_IKE, "unable to resolve %s, initiate aborted",
1269 addr);
1270 DESTROY_IF(child_cfg);
1271 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1272 return DESTROY_ME;
1273 }
1274 DBG1(DBG_IKE, "unable to resolve %s, retrying in %ds",
1275 addr, this->retry_initiate_interval);
1276 defer_initiate = TRUE;
1277 }
1278
1279 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1280 this->task_manager->queue_ike(this->task_manager);
1281 }
1282
1283 #ifdef ME
1284 if (this->peer_cfg->is_mediation(this->peer_cfg))
1285 {
1286 if (this->state == IKE_ESTABLISHED)
1287 {
1288 /* mediation connection is already established, retrigger state
1289 * change to notify bus listeners */
1290 DBG1(DBG_IKE, "mediation connection is already up");
1291 set_state(this, IKE_ESTABLISHED);
1292 }
1293 DESTROY_IF(child_cfg);
1294 }
1295 else
1296 #endif /* ME */
1297 if (child_cfg)
1298 {
1299 /* normal IKE_SA with CHILD_SA */
1300 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1301 tsi, tsr);
1302 #ifdef ME
1303 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1304 {
1305 /* mediated connection, initiate mediation process */
1306 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1307 lib->processor->queue_job(lib->processor, job);
1308 return SUCCESS;
1309 }
1310 #endif /* ME */
1311 }
1312
1313 if (defer_initiate)
1314 {
1315 if (!this->retry_initiate_queued)
1316 {
1317 job_t *job = (job_t*)retry_initiate_job_create(this->ike_sa_id);
1318 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
1319 this->retry_initiate_interval);
1320 this->retry_initiate_queued = TRUE;
1321 }
1322 return SUCCESS;
1323 }
1324 this->retry_initiate_queued = FALSE;
1325 return this->task_manager->initiate(this->task_manager);
1326 }
1327
1328 METHOD(ike_sa_t, retry_initiate, status_t,
1329 private_ike_sa_t *this)
1330 {
1331 if (this->retry_initiate_queued)
1332 {
1333 this->retry_initiate_queued = FALSE;
1334 return initiate(this, NULL, 0, NULL, NULL);
1335 }
1336 return SUCCESS;
1337 }
1338
1339 METHOD(ike_sa_t, process_message, status_t,
1340 private_ike_sa_t *this, message_t *message)
1341 {
1342 status_t status;
1343
1344 if (this->state == IKE_PASSIVE)
1345 { /* do not handle messages in passive state */
1346 return FAILED;
1347 }
1348 if (message->get_major_version(message) != this->version)
1349 {
1350 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1351 exchange_type_names, message->get_exchange_type(message),
1352 message->get_major_version(message),
1353 ike_version_names, this->version);
1354 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1355 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1356 return FAILED;
1357 }
1358 status = this->task_manager->process_message(this->task_manager, message);
1359 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1360 {
1361 /* authentication completed */
1362 this->flush_auth_cfg = FALSE;
1363 flush_auth_cfgs(this);
1364 }
1365 return status;
1366 }
1367
1368 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1369 private_ike_sa_t *this)
1370 {
1371 return this->ike_sa_id;
1372 }
1373
1374 METHOD(ike_sa_t, get_version, ike_version_t,
1375 private_ike_sa_t *this)
1376 {
1377 return this->version;
1378 }
1379
1380 METHOD(ike_sa_t, get_my_id, identification_t*,
1381 private_ike_sa_t *this)
1382 {
1383 return this->my_id;
1384 }
1385
1386 METHOD(ike_sa_t, set_my_id, void,
1387 private_ike_sa_t *this, identification_t *me)
1388 {
1389 DESTROY_IF(this->my_id);
1390 this->my_id = me;
1391 }
1392
1393 METHOD(ike_sa_t, get_other_id, identification_t*,
1394 private_ike_sa_t *this)
1395 {
1396 return this->other_id;
1397 }
1398
1399 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1400 private_ike_sa_t *this)
1401 {
1402 identification_t *id = NULL, *current;
1403 enumerator_t *enumerator;
1404 auth_cfg_t *cfg;
1405
1406 enumerator = array_create_enumerator(this->other_auths);
1407 while (enumerator->enumerate(enumerator, &cfg))
1408 {
1409 /* prefer EAP-Identity of last round */
1410 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1411 if (!current || current->get_type(current) == ID_ANY)
1412 {
1413 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1414 }
1415 if (!current || current->get_type(current) == ID_ANY)
1416 {
1417 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1418 }
1419 if (current && current->get_type(current) != ID_ANY)
1420 {
1421 id = current;
1422 continue;
1423 }
1424 }
1425 enumerator->destroy(enumerator);
1426 if (id)
1427 {
1428 return id;
1429 }
1430 return this->other_id;
1431 }
1432
1433 METHOD(ike_sa_t, set_other_id, void,
1434 private_ike_sa_t *this, identification_t *other)
1435 {
1436 DESTROY_IF(this->other_id);
1437 this->other_id = other;
1438 }
1439
1440 METHOD(ike_sa_t, add_child_sa, void,
1441 private_ike_sa_t *this, child_sa_t *child_sa)
1442 {
1443 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
1444 }
1445
1446 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1447 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1448 {
1449 enumerator_t *enumerator;
1450 child_sa_t *current, *found = NULL;
1451
1452 enumerator = array_create_enumerator(this->child_sas);
1453 while (enumerator->enumerate(enumerator, (void**)&current))
1454 {
1455 if (current->get_spi(current, inbound) == spi &&
1456 current->get_protocol(current) == protocol)
1457 {
1458 found = current;
1459 }
1460 }
1461 enumerator->destroy(enumerator);
1462 return found;
1463 }
1464
1465 METHOD(ike_sa_t, get_child_count, int,
1466 private_ike_sa_t *this)
1467 {
1468 return array_count(this->child_sas);
1469 }
1470
1471 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1472 private_ike_sa_t *this)
1473 {
1474 return array_create_enumerator(this->child_sas);
1475 }
1476
1477 METHOD(ike_sa_t, remove_child_sa, void,
1478 private_ike_sa_t *this, enumerator_t *enumerator)
1479 {
1480 array_remove_at(this->child_sas, enumerator);
1481 }
1482
1483 METHOD(ike_sa_t, rekey_child_sa, status_t,
1484 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1485 {
1486 if (this->state == IKE_PASSIVE)
1487 {
1488 return INVALID_STATE;
1489 }
1490 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1491 return this->task_manager->initiate(this->task_manager);
1492 }
1493
1494 METHOD(ike_sa_t, delete_child_sa, status_t,
1495 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool expired)
1496 {
1497 if (this->state == IKE_PASSIVE)
1498 {
1499 return INVALID_STATE;
1500 }
1501 this->task_manager->queue_child_delete(this->task_manager,
1502 protocol, spi, expired);
1503 return this->task_manager->initiate(this->task_manager);
1504 }
1505
1506 METHOD(ike_sa_t, destroy_child_sa, status_t,
1507 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1508 {
1509 enumerator_t *enumerator;
1510 child_sa_t *child_sa;
1511 status_t status = NOT_FOUND;
1512
1513 enumerator = array_create_enumerator(this->child_sas);
1514 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1515 {
1516 if (child_sa->get_protocol(child_sa) == protocol &&
1517 child_sa->get_spi(child_sa, TRUE) == spi)
1518 {
1519 array_remove_at(this->child_sas, enumerator);
1520 child_sa->destroy(child_sa);
1521 status = SUCCESS;
1522 break;
1523 }
1524 }
1525 enumerator->destroy(enumerator);
1526 return status;
1527 }
1528
1529 METHOD(ike_sa_t, delete_, status_t,
1530 private_ike_sa_t *this)
1531 {
1532 switch (this->state)
1533 {
1534 case IKE_REKEYING:
1535 if (this->version == IKEV1)
1536 { /* SA has been reauthenticated, delete */
1537 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1538 break;
1539 }
1540 /* FALL */
1541 case IKE_ESTABLISHED:
1542 if (time_monotonic(NULL) >= this->stats[STAT_DELETE])
1543 { /* IKE_SA hard lifetime hit */
1544 charon->bus->alert(charon->bus, ALERT_IKE_SA_EXPIRED);
1545 }
1546 this->task_manager->queue_ike_delete(this->task_manager);
1547 return this->task_manager->initiate(this->task_manager);
1548 case IKE_CREATED:
1549 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1550 break;
1551 case IKE_PASSIVE:
1552 break;
1553 default:
1554 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1555 "without notification", ike_sa_state_names, this->state);
1556 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1557 break;
1558 }
1559 return DESTROY_ME;
1560 }
1561
1562 METHOD(ike_sa_t, rekey, status_t,
1563 private_ike_sa_t *this)
1564 {
1565 if (this->state == IKE_PASSIVE)
1566 {
1567 return INVALID_STATE;
1568 }
1569 this->task_manager->queue_ike_rekey(this->task_manager);
1570 return this->task_manager->initiate(this->task_manager);
1571 }
1572
1573 METHOD(ike_sa_t, reauth, status_t,
1574 private_ike_sa_t *this)
1575 {
1576 if (this->state == IKE_PASSIVE)
1577 {
1578 return INVALID_STATE;
1579 }
1580 if (this->state == IKE_CONNECTING)
1581 {
1582 DBG0(DBG_IKE, "reinitiating IKE_SA %s[%d]",
1583 get_name(this), this->unique_id);
1584 reset(this);
1585 this->task_manager->queue_ike(this->task_manager);
1586 return this->task_manager->initiate(this->task_manager);
1587 }
1588 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1589 * If the peer does not support RFC4478, there is no way to keep the
1590 * IKE_SA up. */
1591 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1592 {
1593 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1594 if (array_count(this->other_vips) != 0 ||
1595 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1596 has_condition(this, COND_EAP_AUTHENTICATED)
1597 #ifdef ME
1598 /* as mediation server we too cannot reauth the IKE_SA */
1599 || this->is_mediation_server
1600 #endif /* ME */
1601 )
1602 {
1603 time_t del, now;
1604
1605 del = this->stats[STAT_DELETE];
1606 now = time_monotonic(NULL);
1607 DBG1(DBG_IKE, "IKE_SA %s[%d] will timeout in %V",
1608 get_name(this), this->unique_id, &now, &del);
1609 return FAILED;
1610 }
1611 else
1612 {
1613 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d] actively",
1614 get_name(this), this->unique_id);
1615 }
1616 }
1617 else
1618 {
1619 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d]",
1620 get_name(this), this->unique_id);
1621 }
1622 set_condition(this, COND_REAUTHENTICATING, TRUE);
1623 this->task_manager->queue_ike_reauth(this->task_manager);
1624 return this->task_manager->initiate(this->task_manager);
1625 }
1626
1627 /**
1628 * Check if tasks to create CHILD_SAs are queued in the given queue
1629 */
1630 static bool is_child_queued(private_ike_sa_t *this, task_queue_t queue)
1631 {
1632 enumerator_t *enumerator;
1633 task_t *task;
1634 bool found = FALSE;
1635
1636 enumerator = this->task_manager->create_task_enumerator(this->task_manager,
1637 queue);
1638 while (enumerator->enumerate(enumerator, &task))
1639 {
1640 if (task->get_type(task) == TASK_CHILD_CREATE ||
1641 task->get_type(task) == TASK_QUICK_MODE)
1642 {
1643 found = TRUE;
1644 break;
1645 }
1646 }
1647 enumerator->destroy(enumerator);
1648 return found;
1649 }
1650
1651 METHOD(ike_sa_t, reestablish, status_t,
1652 private_ike_sa_t *this)
1653 {
1654 ike_sa_t *new;
1655 host_t *host;
1656 action_t action;
1657 enumerator_t *enumerator;
1658 child_sa_t *child_sa;
1659 child_cfg_t *child_cfg;
1660 bool restart = FALSE;
1661 status_t status = FAILED;
1662
1663 if (has_condition(this, COND_REAUTHENTICATING))
1664 { /* only reauthenticate if we have children */
1665 if (array_count(this->child_sas) == 0
1666 #ifdef ME
1667 /* allow reauth of mediation connections without CHILD_SAs */
1668 && !this->peer_cfg->is_mediation(this->peer_cfg)
1669 #endif /* ME */
1670 )
1671 {
1672 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA "
1673 "to recreate");
1674 }
1675 else
1676 {
1677 restart = TRUE;
1678 }
1679 }
1680 else
1681 { /* check if we have children to keep up at all */
1682 enumerator = array_create_enumerator(this->child_sas);
1683 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1684 {
1685 if (this->state == IKE_DELETING)
1686 {
1687 action = child_sa->get_close_action(child_sa);
1688 }
1689 else
1690 {
1691 action = child_sa->get_dpd_action(child_sa);
1692 }
1693 switch (action)
1694 {
1695 case ACTION_RESTART:
1696 restart = TRUE;
1697 break;
1698 case ACTION_ROUTE:
1699 charon->traps->install(charon->traps, this->peer_cfg,
1700 child_sa->get_config(child_sa),
1701 child_sa->get_reqid(child_sa));
1702 break;
1703 default:
1704 break;
1705 }
1706 }
1707 enumerator->destroy(enumerator);
1708 /* check if we have tasks that recreate children */
1709 if (!restart)
1710 {
1711 restart = is_child_queued(this, TASK_QUEUE_ACTIVE) ||
1712 is_child_queued(this, TASK_QUEUE_QUEUED);
1713 }
1714 #ifdef ME
1715 /* mediation connections have no children, keep them up anyway */
1716 if (this->peer_cfg->is_mediation(this->peer_cfg))
1717 {
1718 restart = TRUE;
1719 }
1720 #endif /* ME */
1721 }
1722 if (!restart)
1723 {
1724 return FAILED;
1725 }
1726
1727 /* check if we are able to reestablish this IKE_SA */
1728 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1729 (array_count(this->other_vips) != 0 ||
1730 has_condition(this, COND_EAP_AUTHENTICATED)
1731 #ifdef ME
1732 || this->is_mediation_server
1733 #endif /* ME */
1734 ))
1735 {
1736 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1737 return FAILED;
1738 }
1739
1740 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1741 this->version, TRUE);
1742 if (!new)
1743 {
1744 return FAILED;
1745 }
1746 new->set_peer_cfg(new, this->peer_cfg);
1747 host = this->other_host;
1748 new->set_other_host(new, host->clone(host));
1749 host = this->my_host;
1750 new->set_my_host(new, host->clone(host));
1751 charon->bus->ike_reestablish_pre(charon->bus, &this->public, new);
1752 /* resolve hosts but use the old addresses above as fallback */
1753 resolve_hosts((private_ike_sa_t*)new);
1754 /* if we already have a virtual IP, we reuse it */
1755 enumerator = array_create_enumerator(this->my_vips);
1756 while (enumerator->enumerate(enumerator, &host))
1757 {
1758 new->add_virtual_ip(new, TRUE, host);
1759 }
1760 enumerator->destroy(enumerator);
1761
1762 #ifdef ME
1763 if (this->peer_cfg->is_mediation(this->peer_cfg))
1764 {
1765 status = new->initiate(new, NULL, 0, NULL, NULL);
1766 }
1767 else
1768 #endif /* ME */
1769 {
1770 /* handle existing CHILD_SAs */
1771 enumerator = array_create_enumerator(this->child_sas);
1772 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1773 {
1774 if (has_condition(this, COND_REAUTHENTICATING))
1775 {
1776 switch (child_sa->get_state(child_sa))
1777 {
1778 case CHILD_ROUTED:
1779 { /* move routed child directly */
1780 array_remove_at(this->child_sas, enumerator);
1781 new->add_child_sa(new, child_sa);
1782 action = ACTION_NONE;
1783 break;
1784 }
1785 default:
1786 { /* initiate/queue all other CHILD_SAs */
1787 action = ACTION_RESTART;
1788 break;
1789 }
1790 }
1791 }
1792 else
1793 { /* only restart CHILD_SAs that are configured accordingly */
1794 if (this->state == IKE_DELETING)
1795 {
1796 action = child_sa->get_close_action(child_sa);
1797 }
1798 else
1799 {
1800 action = child_sa->get_dpd_action(child_sa);
1801 }
1802 }
1803 switch (action)
1804 {
1805 case ACTION_RESTART:
1806 child_cfg = child_sa->get_config(child_sa);
1807 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1808 child_cfg->get_name(child_cfg));
1809 child_cfg->get_ref(child_cfg);
1810 status = new->initiate(new, child_cfg,
1811 child_sa->get_reqid(child_sa), NULL, NULL);
1812 break;
1813 default:
1814 continue;
1815 }
1816 if (status == DESTROY_ME)
1817 {
1818 break;
1819 }
1820 }
1821 enumerator->destroy(enumerator);
1822 /* adopt any active or queued CHILD-creating tasks */
1823 if (status != DESTROY_ME)
1824 {
1825 task_manager_t *other_tasks = ((private_ike_sa_t*)new)->task_manager;
1826 other_tasks->adopt_child_tasks(other_tasks, this->task_manager);
1827 if (new->get_state(new) == IKE_CREATED)
1828 {
1829 status = new->initiate(new, NULL, 0, NULL, NULL);
1830 }
1831 }
1832 }
1833
1834 if (status == DESTROY_ME)
1835 {
1836 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
1837 FALSE);
1838 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1839 status = FAILED;
1840 }
1841 else
1842 {
1843 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
1844 TRUE);
1845 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1846 status = SUCCESS;
1847 }
1848 charon->bus->set_sa(charon->bus, &this->public);
1849 return status;
1850 }
1851
1852 METHOD(ike_sa_t, retransmit, status_t,
1853 private_ike_sa_t *this, u_int32_t message_id)
1854 {
1855 if (this->state == IKE_PASSIVE)
1856 {
1857 return INVALID_STATE;
1858 }
1859 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1860 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1861 {
1862 /* send a proper signal to brief interested bus listeners */
1863 switch (this->state)
1864 {
1865 case IKE_CONNECTING:
1866 {
1867 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1868 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1869 charon->bus->alert(charon->bus, ALERT_PEER_INIT_UNREACHABLE,
1870 this->keyingtry);
1871 this->keyingtry++;
1872 if (tries == 0 || tries > this->keyingtry)
1873 {
1874 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1875 this->keyingtry + 1, tries);
1876 reset(this);
1877 resolve_hosts(this);
1878 this->task_manager->queue_ike(this->task_manager);
1879 return this->task_manager->initiate(this->task_manager);
1880 }
1881 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1882 break;
1883 }
1884 case IKE_DELETING:
1885 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1886 if (has_condition(this, COND_REAUTHENTICATING))
1887 {
1888 DBG1(DBG_IKE, "delete during reauthentication failed, "
1889 "trying to reestablish IKE_SA anyway");
1890 reestablish(this);
1891 }
1892 break;
1893 case IKE_REKEYING:
1894 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1895 /* FALL */
1896 default:
1897 reestablish(this);
1898 break;
1899 }
1900 if (this->state != IKE_CONNECTING)
1901 {
1902 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1903 }
1904 return DESTROY_ME;
1905 }
1906 return SUCCESS;
1907 }
1908
1909 METHOD(ike_sa_t, set_auth_lifetime, status_t,
1910 private_ike_sa_t *this, u_int32_t lifetime)
1911 {
1912 u_int32_t diff, hard, soft, now;
1913 bool send_update;
1914
1915 diff = this->peer_cfg->get_over_time(this->peer_cfg);
1916 now = time_monotonic(NULL);
1917 hard = now + lifetime;
1918 soft = hard - diff;
1919
1920 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1921 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1922 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
1923 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
1924 (array_count(this->other_vips) != 0 ||
1925 has_condition(this, COND_EAP_AUTHENTICATED));
1926
1927 if (lifetime < diff)
1928 {
1929 this->stats[STAT_REAUTH] = now;
1930
1931 if (!send_update)
1932 {
1933 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1934 "starting reauthentication", lifetime);
1935 lib->processor->queue_job(lib->processor,
1936 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1937 }
1938 }
1939 else if (this->stats[STAT_REAUTH] == 0 ||
1940 this->stats[STAT_REAUTH] > soft)
1941 {
1942 this->stats[STAT_REAUTH] = soft;
1943 if (!send_update)
1944 {
1945 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
1946 "reauthentication in %ds", lifetime, lifetime - diff);
1947 lib->scheduler->schedule_job(lib->scheduler,
1948 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1949 lifetime - diff);
1950 }
1951 }
1952 else
1953 {
1954 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1955 "reauthentication already scheduled in %ds", lifetime,
1956 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1957 send_update = FALSE;
1958 }
1959 /* give at least some seconds to reauthenticate */
1960 this->stats[STAT_DELETE] = max(hard, now + 10);
1961
1962 #ifdef USE_IKEV2
1963 if (send_update)
1964 {
1965 ike_auth_lifetime_t *task;
1966
1967 task = ike_auth_lifetime_create(&this->public, TRUE);
1968 this->task_manager->queue_task(this->task_manager, &task->task);
1969 return this->task_manager->initiate(this->task_manager);
1970 }
1971 #endif
1972 return SUCCESS;
1973 }
1974
1975 /**
1976 * Check if the current combination of source and destination address is still
1977 * valid.
1978 */
1979 static bool is_current_path_valid(private_ike_sa_t *this)
1980 {
1981 bool valid = FALSE;
1982 host_t *src;
1983 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1984 this->other_host, this->my_host);
1985 if (src)
1986 {
1987 if (src->ip_equals(src, this->my_host))
1988 {
1989 valid = TRUE;
1990 }
1991 src->destroy(src);
1992 }
1993 return valid;
1994 }
1995
1996 /**
1997 * Check if we have any path avialable for this IKE SA.
1998 */
1999 static bool is_any_path_valid(private_ike_sa_t *this)
2000 {
2001 bool valid = FALSE;
2002 enumerator_t *enumerator;
2003 host_t *src = NULL, *addr;
2004 int family = AF_UNSPEC;
2005
2006 switch (charon->socket->supported_families(charon->socket))
2007 {
2008 case SOCKET_FAMILY_IPV4:
2009 family = AF_INET;
2010 break;
2011 case SOCKET_FAMILY_IPV6:
2012 family = AF_INET6;
2013 break;
2014 case SOCKET_FAMILY_BOTH:
2015 case SOCKET_FAMILY_NONE:
2016 break;
2017 }
2018
2019 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
2020 enumerator = create_peer_address_enumerator(this);
2021 while (enumerator->enumerate(enumerator, &addr))
2022 {
2023 if (family != AF_UNSPEC && addr->get_family(addr) != family)
2024 {
2025 continue;
2026 }
2027 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
2028 src = hydra->kernel_interface->get_source_addr(
2029 hydra->kernel_interface, addr, NULL);
2030 if (src)
2031 {
2032 break;
2033 }
2034 }
2035 enumerator->destroy(enumerator);
2036 if (src)
2037 {
2038 valid = TRUE;
2039 src->destroy(src);
2040 }
2041 return valid;
2042 }
2043
2044 METHOD(ike_sa_t, roam, status_t,
2045 private_ike_sa_t *this, bool address)
2046 {
2047 switch (this->state)
2048 {
2049 case IKE_CREATED:
2050 case IKE_DELETING:
2051 case IKE_DESTROYING:
2052 case IKE_PASSIVE:
2053 return SUCCESS;
2054 default:
2055 break;
2056 }
2057
2058 /* keep existing path if possible */
2059 if (is_current_path_valid(this))
2060 {
2061 DBG2(DBG_IKE, "keeping connection path %H - %H",
2062 this->my_host, this->other_host);
2063 set_condition(this, COND_STALE, FALSE);
2064
2065 if (supports_extension(this, EXT_MOBIKE) && address)
2066 { /* if any addresses changed, send an updated list */
2067 DBG1(DBG_IKE, "sending address list update using MOBIKE");
2068 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
2069 return this->task_manager->initiate(this->task_manager);
2070 }
2071 return SUCCESS;
2072 }
2073
2074 if (!is_any_path_valid(this))
2075 {
2076 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
2077 this->other_host);
2078 set_condition(this, COND_STALE, TRUE);
2079 return SUCCESS;
2080 }
2081 set_condition(this, COND_STALE, FALSE);
2082
2083 /* update addresses with mobike, if supported ... */
2084 if (supports_extension(this, EXT_MOBIKE))
2085 {
2086 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2087 { /* responder updates the peer about changed address config */
2088 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
2089 "implicitly requesting an address change");
2090 address = TRUE;
2091 }
2092 else
2093 {
2094 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2095 }
2096 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
2097 return this->task_manager->initiate(this->task_manager);
2098 }
2099
2100 /* ... reauth if not */
2101 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2102 { /* responder does not reauthenticate */
2103 set_condition(this, COND_STALE, TRUE);
2104 return SUCCESS;
2105 }
2106 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
2107 /* since our previous path is not valid anymore, try and find a new one */
2108 resolve_hosts(this);
2109 return reauth(this);
2110 }
2111
2112 METHOD(ike_sa_t, add_configuration_attribute, void,
2113 private_ike_sa_t *this, attribute_handler_t *handler,
2114 configuration_attribute_type_t type, chunk_t data)
2115 {
2116 attribute_entry_t entry = {
2117 .handler = handler,
2118 .type = type,
2119 .data = chunk_clone(data),
2120 };
2121 array_insert(this->attributes, ARRAY_TAIL, &entry);
2122 }
2123
2124 /**
2125 * Enumerator filter for attributes
2126 */
2127 static bool filter_attribute(void *null, attribute_entry_t **in,
2128 configuration_attribute_type_t *type, void *in2,
2129 chunk_t *data, void *in3, bool *handled)
2130 {
2131 *type = (*in)->type;
2132 *data = (*in)->data;
2133 *handled = (*in)->handler != NULL;
2134 return TRUE;
2135 }
2136
2137 METHOD(ike_sa_t, create_attribute_enumerator, enumerator_t*,
2138 private_ike_sa_t *this)
2139 {
2140 return enumerator_create_filter(array_create_enumerator(this->attributes),
2141 (void*)filter_attribute, NULL, NULL);
2142 }
2143
2144 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
2145 private_ike_sa_t *this, task_queue_t queue)
2146 {
2147 return this->task_manager->create_task_enumerator(this->task_manager, queue);
2148 }
2149
2150 METHOD(ike_sa_t, flush_queue, void,
2151 private_ike_sa_t *this, task_queue_t queue)
2152 {
2153 this->task_manager->flush_queue(this->task_manager, queue);
2154 }
2155
2156 METHOD(ike_sa_t, queue_task, void,
2157 private_ike_sa_t *this, task_t *task)
2158 {
2159 this->task_manager->queue_task(this->task_manager, task);
2160 }
2161
2162 METHOD(ike_sa_t, inherit_pre, void,
2163 private_ike_sa_t *this, ike_sa_t *other_public)
2164 {
2165 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2166
2167 /* apply config and hosts */
2168 set_peer_cfg(this, other->peer_cfg);
2169 set_my_host(this, other->my_host->clone(other->my_host));
2170 set_other_host(this, other->other_host->clone(other->other_host));
2171
2172 /* apply extensions and conditions with a few exceptions */
2173 this->extensions = other->extensions;
2174 this->conditions = other->conditions;
2175 this->conditions &= ~COND_STALE;
2176 this->conditions &= ~COND_REAUTHENTICATING;
2177 }
2178
2179 METHOD(ike_sa_t, inherit_post, void,
2180 private_ike_sa_t *this, ike_sa_t *other_public)
2181 {
2182 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2183 child_sa_t *child_sa;
2184 enumerator_t *enumerator;
2185 attribute_entry_t entry;
2186 auth_cfg_t *cfg;
2187 host_t *vip;
2188
2189 /* apply hosts and ids */
2190 this->my_host->destroy(this->my_host);
2191 this->other_host->destroy(this->other_host);
2192 this->my_id->destroy(this->my_id);
2193 this->other_id->destroy(this->other_id);
2194 this->my_host = other->my_host->clone(other->my_host);
2195 this->other_host = other->other_host->clone(other->other_host);
2196 this->my_id = other->my_id->clone(other->my_id);
2197 this->other_id = other->other_id->clone(other->other_id);
2198
2199 /* apply assigned virtual IPs... */
2200 while (array_remove(other->my_vips, ARRAY_HEAD, &vip))
2201 {
2202 array_insert_create(&this->my_vips, ARRAY_TAIL, vip);
2203 }
2204 while (array_remove(other->other_vips, ARRAY_HEAD, &vip))
2205 {
2206 array_insert_create(&this->other_vips, ARRAY_TAIL, vip);
2207 }
2208
2209 /* authentication information */
2210 enumerator = array_create_enumerator(other->my_auths);
2211 while (enumerator->enumerate(enumerator, &cfg))
2212 {
2213 array_insert(this->my_auths, ARRAY_TAIL, cfg->clone(cfg));
2214 }
2215 enumerator->destroy(enumerator);
2216 enumerator = array_create_enumerator(other->other_auths);
2217 while (enumerator->enumerate(enumerator, &cfg))
2218 {
2219 array_insert(this->other_auths, ARRAY_TAIL, cfg->clone(cfg));
2220 }
2221 enumerator->destroy(enumerator);
2222
2223 /* ... and configuration attributes */
2224 while (array_remove(other->attributes, ARRAY_HEAD, &entry))
2225 {
2226 array_insert(this->attributes, ARRAY_TAIL, &entry);
2227 }
2228
2229 /* inherit all conditions */
2230 this->conditions = other->conditions;
2231 if (this->conditions & COND_NAT_HERE)
2232 {
2233 send_keepalive(this);
2234 }
2235
2236 #ifdef ME
2237 if (other->is_mediation_server)
2238 {
2239 act_as_mediation_server(this);
2240 }
2241 else if (other->server_reflexive_host)
2242 {
2243 this->server_reflexive_host = other->server_reflexive_host->clone(
2244 other->server_reflexive_host);
2245 }
2246 #endif /* ME */
2247
2248 /* adopt all children */
2249 while (array_remove(other->child_sas, ARRAY_HEAD, &child_sa))
2250 {
2251 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
2252 }
2253
2254 /* move pending tasks to the new IKE_SA */
2255 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2256
2257 /* reauthentication timeout survives a rekeying */
2258 if (other->stats[STAT_REAUTH])
2259 {
2260 time_t reauth, delete, now = time_monotonic(NULL);
2261
2262 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2263 reauth = this->stats[STAT_REAUTH] - now;
2264 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2265 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2266 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2267 "lifetime reduced to %ds", reauth, delete);
2268 lib->scheduler->schedule_job(lib->scheduler,
2269 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2270 lib->scheduler->schedule_job(lib->scheduler,
2271 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2272 }
2273 }
2274
2275 METHOD(ike_sa_t, destroy, void,
2276 private_ike_sa_t *this)
2277 {
2278 attribute_entry_t entry;
2279 child_sa_t *child_sa;
2280 host_t *vip;
2281
2282 charon->bus->set_sa(charon->bus, &this->public);
2283
2284 set_state(this, IKE_DESTROYING);
2285 if (this->task_manager)
2286 {
2287 this->task_manager->flush(this->task_manager);
2288 }
2289
2290 /* remove attributes first, as we pass the IKE_SA to the handler */
2291 charon->bus->handle_vips(charon->bus, &this->public, FALSE);
2292 while (array_remove(this->attributes, ARRAY_TAIL, &entry))
2293 {
2294 if (entry.handler)
2295 {
2296 hydra->attributes->release(hydra->attributes, entry.handler,
2297 this->other_id, entry.type, entry.data);
2298 }
2299 free(entry.data.ptr);
2300 }
2301 /* uninstall CHILD_SAs before virtual IPs, otherwise we might kill
2302 * routes that the CHILD_SA tries to uninstall. */
2303 while (array_remove(this->child_sas, ARRAY_TAIL, &child_sa))
2304 {
2305 child_sa->destroy(child_sa);
2306 }
2307 while (array_remove(this->my_vips, ARRAY_TAIL, &vip))
2308 {
2309 hydra->kernel_interface->del_ip(hydra->kernel_interface, vip, -1, TRUE);
2310 vip->destroy(vip);
2311 }
2312 if (array_count(this->other_vips))
2313 {
2314 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
2315 }
2316 while (array_remove(this->other_vips, ARRAY_TAIL, &vip))
2317 {
2318 if (this->peer_cfg)
2319 {
2320 linked_list_t *pools;
2321 identification_t *id;
2322
2323 id = get_other_eap_id(this);
2324 pools = linked_list_create_from_enumerator(
2325 this->peer_cfg->create_pool_enumerator(this->peer_cfg));
2326 hydra->attributes->release_address(hydra->attributes, pools, vip, id);
2327 pools->destroy(pools);
2328 }
2329 vip->destroy(vip);
2330 }
2331
2332 /* unset SA after here to avoid usage by the listeners */
2333 charon->bus->set_sa(charon->bus, NULL);
2334
2335 array_destroy(this->child_sas);
2336 DESTROY_IF(this->task_manager);
2337 DESTROY_IF(this->keymat);
2338 array_destroy(this->attributes);
2339 array_destroy(this->my_vips);
2340 array_destroy(this->other_vips);
2341 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
2342 #ifdef ME
2343 if (this->is_mediation_server)
2344 {
2345 charon->mediation_manager->remove(charon->mediation_manager,
2346 this->ike_sa_id);
2347 }
2348 DESTROY_IF(this->server_reflexive_host);
2349 chunk_free(&this->connect_id);
2350 #endif /* ME */
2351 free(this->nat_detection_dest.ptr);
2352
2353 DESTROY_IF(this->my_host);
2354 DESTROY_IF(this->other_host);
2355 DESTROY_IF(this->my_id);
2356 DESTROY_IF(this->other_id);
2357 DESTROY_IF(this->local_host);
2358 DESTROY_IF(this->remote_host);
2359
2360 DESTROY_IF(this->ike_cfg);
2361 DESTROY_IF(this->peer_cfg);
2362 DESTROY_IF(this->proposal);
2363 this->my_auth->destroy(this->my_auth);
2364 this->other_auth->destroy(this->other_auth);
2365 array_destroy_offset(this->my_auths, offsetof(auth_cfg_t, destroy));
2366 array_destroy_offset(this->other_auths, offsetof(auth_cfg_t, destroy));
2367
2368 this->ike_sa_id->destroy(this->ike_sa_id);
2369 free(this);
2370 }
2371
2372 /*
2373 * Described in header.
2374 */
2375 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
2376 ike_version_t version)
2377 {
2378 private_ike_sa_t *this;
2379 static refcount_t unique_id = 0;
2380
2381 if (version == IKE_ANY)
2382 { /* prefer IKEv2 if protocol not specified */
2383 #ifdef USE_IKEV2
2384 version = IKEV2;
2385 #else
2386 version = IKEV1;
2387 #endif
2388 }
2389
2390 INIT(this,
2391 .public = {
2392 .get_version = _get_version,
2393 .get_state = _get_state,
2394 .set_state = _set_state,
2395 .get_name = _get_name,
2396 .get_statistic = _get_statistic,
2397 .set_statistic = _set_statistic,
2398 .process_message = _process_message,
2399 .initiate = _initiate,
2400 .retry_initiate = _retry_initiate,
2401 .get_ike_cfg = _get_ike_cfg,
2402 .set_ike_cfg = _set_ike_cfg,
2403 .get_peer_cfg = _get_peer_cfg,
2404 .set_peer_cfg = _set_peer_cfg,
2405 .get_auth_cfg = _get_auth_cfg,
2406 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2407 .add_auth_cfg = _add_auth_cfg,
2408 .get_proposal = _get_proposal,
2409 .set_proposal = _set_proposal,
2410 .get_id = _get_id,
2411 .get_my_host = _get_my_host,
2412 .set_my_host = _set_my_host,
2413 .get_other_host = _get_other_host,
2414 .set_other_host = _set_other_host,
2415 .set_message_id = _set_message_id,
2416 .float_ports = _float_ports,
2417 .update_hosts = _update_hosts,
2418 .get_my_id = _get_my_id,
2419 .set_my_id = _set_my_id,
2420 .get_other_id = _get_other_id,
2421 .set_other_id = _set_other_id,
2422 .get_other_eap_id = _get_other_eap_id,
2423 .enable_extension = _enable_extension,
2424 .supports_extension = _supports_extension,
2425 .set_condition = _set_condition,
2426 .has_condition = _has_condition,
2427 .set_pending_updates = _set_pending_updates,
2428 .get_pending_updates = _get_pending_updates,
2429 .create_peer_address_enumerator = _create_peer_address_enumerator,
2430 .add_peer_address = _add_peer_address,
2431 .clear_peer_addresses = _clear_peer_addresses,
2432 .has_mapping_changed = _has_mapping_changed,
2433 .retransmit = _retransmit,
2434 .delete = _delete_,
2435 .destroy = _destroy,
2436 .send_dpd = _send_dpd,
2437 .send_keepalive = _send_keepalive,
2438 .get_keymat = _get_keymat,
2439 .add_child_sa = _add_child_sa,
2440 .get_child_sa = _get_child_sa,
2441 .get_child_count = _get_child_count,
2442 .create_child_sa_enumerator = _create_child_sa_enumerator,
2443 .remove_child_sa = _remove_child_sa,
2444 .rekey_child_sa = _rekey_child_sa,
2445 .delete_child_sa = _delete_child_sa,
2446 .destroy_child_sa = _destroy_child_sa,
2447 .rekey = _rekey,
2448 .reauth = _reauth,
2449 .reestablish = _reestablish,
2450 .set_auth_lifetime = _set_auth_lifetime,
2451 .roam = _roam,
2452 .inherit_pre = _inherit_pre,
2453 .inherit_post = _inherit_post,
2454 .generate_message = _generate_message,
2455 .generate_message_fragmented = _generate_message_fragmented,
2456 .reset = _reset,
2457 .get_unique_id = _get_unique_id,
2458 .add_virtual_ip = _add_virtual_ip,
2459 .clear_virtual_ips = _clear_virtual_ips,
2460 .create_virtual_ip_enumerator = _create_virtual_ip_enumerator,
2461 .add_configuration_attribute = _add_configuration_attribute,
2462 .create_attribute_enumerator = _create_attribute_enumerator,
2463 .set_kmaddress = _set_kmaddress,
2464 .create_task_enumerator = _create_task_enumerator,
2465 .flush_queue = _flush_queue,
2466 .queue_task = _queue_task,
2467 #ifdef ME
2468 .act_as_mediation_server = _act_as_mediation_server,
2469 .get_server_reflexive_host = _get_server_reflexive_host,
2470 .set_server_reflexive_host = _set_server_reflexive_host,
2471 .get_connect_id = _get_connect_id,
2472 .initiate_mediation = _initiate_mediation,
2473 .initiate_mediated = _initiate_mediated,
2474 .relay = _relay,
2475 .callback = _callback,
2476 .respond = _respond,
2477 #endif /* ME */
2478 },
2479 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2480 .version = version,
2481 .my_host = host_create_any(AF_INET),
2482 .other_host = host_create_any(AF_INET),
2483 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2484 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2485 .keymat = keymat_create(version, initiator),
2486 .state = IKE_CREATED,
2487 .stats[STAT_INBOUND] = time_monotonic(NULL),
2488 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2489 .my_auth = auth_cfg_create(),
2490 .other_auth = auth_cfg_create(),
2491 .my_auths = array_create(0, 0),
2492 .other_auths = array_create(0, 0),
2493 .attributes = array_create(sizeof(attribute_entry_t), 0),
2494 .unique_id = ref_get(&unique_id),
2495 .keepalive_interval = lib->settings->get_time(lib->settings,
2496 "%s.keep_alive", KEEPALIVE_INTERVAL, lib->ns),
2497 .retry_initiate_interval = lib->settings->get_time(lib->settings,
2498 "%s.retry_initiate_interval", 0, lib->ns),
2499 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2500 "%s.flush_auth_cfg", FALSE, lib->ns),
2501 .fragment_size = lib->settings->get_int(lib->settings,
2502 "%s.fragment_size", 0, lib->ns),
2503 );
2504
2505 if (version == IKEV2)
2506 { /* always supported with IKEv2 */
2507 enable_extension(this, EXT_DPD);
2508 }
2509
2510 this->task_manager = task_manager_create(&this->public);
2511 this->my_host->set_port(this->my_host,
2512 charon->socket->get_port(charon->socket, FALSE));
2513
2514 if (!this->task_manager || !this->keymat)
2515 {
2516 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2517 destroy(this);
2518 return NULL;
2519 }
2520 return &this->public;
2521 }