ike: Fix memory leak if remote address is kept
[strongswan.git] / src / libcharon / sa / ike_sa.c
1 /*
2 * Copyright (C) 2006-2015 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 charon->bus->ike_update(charon->bus, &this->public, TRUE, me);
936 set_my_host(this, me->clone(me));
937 update = TRUE;
938 }
939
940 if (!other->equals(other, this->other_host) &&
941 (force || has_condition(this, COND_NAT_THERE)))
942 {
943 /* only update other's address if we are behind a static NAT,
944 * which we assume is the case if we are not initiator */
945 if (force ||
946 (!has_condition(this, COND_NAT_HERE) ||
947 !has_condition(this, COND_ORIGINAL_INITIATOR)))
948 {
949 charon->bus->ike_update(charon->bus, &this->public, FALSE, other);
950 set_other_host(this, other->clone(other));
951 update = TRUE;
952 }
953 }
954 }
955
956 /* update all associated CHILD_SAs, if required */
957 if (update)
958 {
959 enumerator_t *enumerator;
960 child_sa_t *child_sa;
961 linked_list_t *vips;
962
963 vips = linked_list_create_from_enumerator(
964 array_create_enumerator(this->my_vips));
965
966 enumerator = array_create_enumerator(this->child_sas);
967 while (enumerator->enumerate(enumerator, &child_sa))
968 {
969 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
970 charon->child_sa_manager->add(charon->child_sa_manager,
971 child_sa, &this->public);
972
973 if (child_sa->update(child_sa, this->my_host, this->other_host,
974 vips, has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
975 {
976 this->public.rekey_child_sa(&this->public,
977 child_sa->get_protocol(child_sa),
978 child_sa->get_spi(child_sa, TRUE));
979 }
980
981 }
982 enumerator->destroy(enumerator);
983
984 vips->destroy(vips);
985 }
986 }
987
988 /**
989 * Set configured DSCP value on packet
990 */
991 static void set_dscp(private_ike_sa_t *this, packet_t *packet)
992 {
993 ike_cfg_t *ike_cfg;
994
995 /* prefer IKE config on peer_cfg, as its selection is more accurate
996 * then the initial IKE config */
997 if (this->peer_cfg)
998 {
999 ike_cfg = this->peer_cfg->get_ike_cfg(this->peer_cfg);
1000 }
1001 else
1002 {
1003 ike_cfg = this->ike_cfg;
1004 }
1005 if (ike_cfg)
1006 {
1007 packet->set_dscp(packet, ike_cfg->get_dscp(ike_cfg));
1008 }
1009 }
1010
1011 METHOD(ike_sa_t, generate_message, status_t,
1012 private_ike_sa_t *this, message_t *message, packet_t **packet)
1013 {
1014 status_t status;
1015
1016 if (message->is_encoded(message))
1017 { /* already encoded in task, but set DSCP value */
1018 *packet = message->get_packet(message);
1019 set_dscp(this, *packet);
1020 return SUCCESS;
1021 }
1022 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1023 message->set_ike_sa_id(message, this->ike_sa_id);
1024 charon->bus->message(charon->bus, message, FALSE, TRUE);
1025 status = message->generate(message, this->keymat, packet);
1026 if (status == SUCCESS)
1027 {
1028 set_dscp(this, *packet);
1029 charon->bus->message(charon->bus, message, FALSE, FALSE);
1030 }
1031 return status;
1032 }
1033
1034 static bool filter_fragments(private_ike_sa_t *this, packet_t **fragment,
1035 packet_t **packet)
1036 {
1037 *packet = (*fragment)->clone(*fragment);
1038 set_dscp(this, *packet);
1039 return TRUE;
1040 }
1041
1042 METHOD(ike_sa_t, generate_message_fragmented, status_t,
1043 private_ike_sa_t *this, message_t *message, enumerator_t **packets)
1044 {
1045 enumerator_t *fragments;
1046 packet_t *packet;
1047 status_t status;
1048 bool use_frags = FALSE;
1049
1050 if (this->ike_cfg)
1051 {
1052 switch (this->ike_cfg->fragmentation(this->ike_cfg))
1053 {
1054 case FRAGMENTATION_FORCE:
1055 use_frags = TRUE;
1056 break;
1057 case FRAGMENTATION_YES:
1058 use_frags = supports_extension(this, EXT_IKE_FRAGMENTATION);
1059 if (use_frags && this->version == IKEV1 &&
1060 supports_extension(this, EXT_MS_WINDOWS))
1061 {
1062 /* It seems Windows 7 and 8 peers only accept proprietary
1063 * fragmented messages if they expect certificates. */
1064 use_frags = message->get_payload(message,
1065 PLV1_CERTIFICATE) != NULL;
1066 }
1067 break;
1068 default:
1069 break;
1070 }
1071 }
1072 if (!use_frags)
1073 {
1074 status = generate_message(this, message, &packet);
1075 if (status != SUCCESS)
1076 {
1077 return status;
1078 }
1079 *packets = enumerator_create_single(packet, NULL);
1080 return SUCCESS;
1081 }
1082
1083 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1084 message->set_ike_sa_id(message, this->ike_sa_id);
1085 charon->bus->message(charon->bus, message, FALSE, TRUE);
1086 status = message->fragment(message, this->keymat, this->fragment_size,
1087 &fragments);
1088 if (status == SUCCESS)
1089 {
1090 charon->bus->message(charon->bus, message, FALSE, FALSE);
1091 *packets = enumerator_create_filter(fragments, (void*)filter_fragments,
1092 this, NULL);
1093 }
1094 return status;
1095 }
1096
1097 METHOD(ike_sa_t, set_kmaddress, void,
1098 private_ike_sa_t *this, host_t *local, host_t *remote)
1099 {
1100 DESTROY_IF(this->local_host);
1101 DESTROY_IF(this->remote_host);
1102 this->local_host = local->clone(local);
1103 this->remote_host = remote->clone(remote);
1104 }
1105
1106 #ifdef ME
1107 METHOD(ike_sa_t, act_as_mediation_server, void,
1108 private_ike_sa_t *this)
1109 {
1110 charon->mediation_manager->update_sa_id(charon->mediation_manager,
1111 this->other_id, this->ike_sa_id);
1112 this->is_mediation_server = TRUE;
1113 }
1114
1115 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
1116 private_ike_sa_t *this)
1117 {
1118 return this->server_reflexive_host;
1119 }
1120
1121 METHOD(ike_sa_t, set_server_reflexive_host, void,
1122 private_ike_sa_t *this, host_t *host)
1123 {
1124 DESTROY_IF(this->server_reflexive_host);
1125 this->server_reflexive_host = host;
1126 }
1127
1128 METHOD(ike_sa_t, get_connect_id, chunk_t,
1129 private_ike_sa_t *this)
1130 {
1131 return this->connect_id;
1132 }
1133
1134 METHOD(ike_sa_t, respond, status_t,
1135 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
1136 {
1137 ike_me_t *task = ike_me_create(&this->public, TRUE);
1138 task->respond(task, peer_id, connect_id);
1139 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1140 return this->task_manager->initiate(this->task_manager);
1141 }
1142
1143 METHOD(ike_sa_t, callback, status_t,
1144 private_ike_sa_t *this, identification_t *peer_id)
1145 {
1146 ike_me_t *task = ike_me_create(&this->public, TRUE);
1147 task->callback(task, peer_id);
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, relay, status_t,
1153 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
1154 chunk_t connect_key, linked_list_t *endpoints, bool response)
1155 {
1156 ike_me_t *task = ike_me_create(&this->public, TRUE);
1157 task->relay(task, requester, connect_id, connect_key, endpoints, response);
1158 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1159 return this->task_manager->initiate(this->task_manager);
1160 }
1161
1162 METHOD(ike_sa_t, initiate_mediation, status_t,
1163 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1164 {
1165 ike_me_t *task = ike_me_create(&this->public, TRUE);
1166 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1167 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1168 return this->task_manager->initiate(this->task_manager);
1169 }
1170
1171 METHOD(ike_sa_t, initiate_mediated, status_t,
1172 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1173 {
1174 set_my_host(this, me->clone(me));
1175 set_other_host(this, other->clone(other));
1176 chunk_free(&this->connect_id);
1177 this->connect_id = chunk_clone(connect_id);
1178 return this->task_manager->initiate(this->task_manager);
1179 }
1180 #endif /* ME */
1181
1182 /**
1183 * Resolve DNS host in configuration
1184 */
1185 static void resolve_hosts(private_ike_sa_t *this)
1186 {
1187 host_t *host;
1188 int family = AF_UNSPEC;
1189
1190 switch (charon->socket->supported_families(charon->socket))
1191 {
1192 case SOCKET_FAMILY_IPV4:
1193 family = AF_INET;
1194 break;
1195 case SOCKET_FAMILY_IPV6:
1196 family = AF_INET6;
1197 break;
1198 case SOCKET_FAMILY_BOTH:
1199 case SOCKET_FAMILY_NONE:
1200 break;
1201 }
1202
1203 /* if an IP address is set locally, use the same family to resolve remote */
1204 if (family == AF_UNSPEC && !this->remote_host)
1205 {
1206 if (this->local_host)
1207 {
1208 family = this->local_host->get_family(this->local_host);
1209 }
1210 else
1211 {
1212 family = ike_cfg_get_family(this->ike_cfg, TRUE);
1213 }
1214 }
1215
1216 if (this->remote_host)
1217 {
1218 host = this->remote_host->clone(this->remote_host);
1219 host->set_port(host, IKEV2_UDP_PORT);
1220 }
1221 else
1222 {
1223 host = this->ike_cfg->resolve_other(this->ike_cfg, family);
1224 }
1225 if (host)
1226 {
1227 if (!host->is_anyaddr(host) ||
1228 this->other_host->is_anyaddr(this->other_host))
1229 { /* don't set to %any if we currently have an address, but the
1230 * address family might have changed */
1231 set_other_host(this, host);
1232 }
1233 else
1234 {
1235 host->destroy(host);
1236 }
1237 }
1238
1239 if (this->local_host)
1240 {
1241 host = this->local_host->clone(this->local_host);
1242 host->set_port(host, charon->socket->get_port(charon->socket, FALSE));
1243 }
1244 else
1245 {
1246 /* use same address family as for other */
1247 if (!this->other_host->is_anyaddr(this->other_host))
1248 {
1249 family = this->other_host->get_family(this->other_host);
1250 }
1251 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1252
1253 if (host && host->is_anyaddr(host) &&
1254 !this->other_host->is_anyaddr(this->other_host))
1255 {
1256 host->destroy(host);
1257 host = hydra->kernel_interface->get_source_addr(
1258 hydra->kernel_interface, this->other_host, NULL);
1259 if (host)
1260 {
1261 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1262 }
1263 else
1264 { /* fallback to address family specific %any(6), if configured */
1265 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1266 }
1267 }
1268 }
1269 if (host)
1270 {
1271 set_my_host(this, host);
1272 }
1273 }
1274
1275 METHOD(ike_sa_t, initiate, status_t,
1276 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1277 traffic_selector_t *tsi, traffic_selector_t *tsr)
1278 {
1279 bool defer_initiate = FALSE;
1280
1281 if (this->state == IKE_CREATED)
1282 {
1283 if (this->my_host->is_anyaddr(this->my_host) ||
1284 this->other_host->is_anyaddr(this->other_host))
1285 {
1286 resolve_hosts(this);
1287 }
1288
1289 if (this->other_host->is_anyaddr(this->other_host)
1290 #ifdef ME
1291 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1292 #endif /* ME */
1293 )
1294 {
1295 char *addr;
1296
1297 addr = this->ike_cfg->get_other_addr(this->ike_cfg);
1298 if (!this->retry_initiate_interval)
1299 {
1300 DBG1(DBG_IKE, "unable to resolve %s, initiate aborted",
1301 addr);
1302 DESTROY_IF(child_cfg);
1303 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1304 return DESTROY_ME;
1305 }
1306 DBG1(DBG_IKE, "unable to resolve %s, retrying in %ds",
1307 addr, this->retry_initiate_interval);
1308 defer_initiate = TRUE;
1309 }
1310
1311 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1312 this->task_manager->queue_ike(this->task_manager);
1313 }
1314
1315 #ifdef ME
1316 if (this->peer_cfg->is_mediation(this->peer_cfg))
1317 {
1318 if (this->state == IKE_ESTABLISHED)
1319 {
1320 /* mediation connection is already established, retrigger state
1321 * change to notify bus listeners */
1322 DBG1(DBG_IKE, "mediation connection is already up");
1323 set_state(this, IKE_ESTABLISHED);
1324 }
1325 DESTROY_IF(child_cfg);
1326 }
1327 else
1328 #endif /* ME */
1329 if (child_cfg)
1330 {
1331 /* normal IKE_SA with CHILD_SA */
1332 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1333 tsi, tsr);
1334 #ifdef ME
1335 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1336 {
1337 /* mediated connection, initiate mediation process */
1338 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1339 lib->processor->queue_job(lib->processor, job);
1340 return SUCCESS;
1341 }
1342 #endif /* ME */
1343 }
1344
1345 if (defer_initiate)
1346 {
1347 if (!this->retry_initiate_queued)
1348 {
1349 job_t *job = (job_t*)retry_initiate_job_create(this->ike_sa_id);
1350 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
1351 this->retry_initiate_interval);
1352 this->retry_initiate_queued = TRUE;
1353 }
1354 return SUCCESS;
1355 }
1356 this->retry_initiate_queued = FALSE;
1357 return this->task_manager->initiate(this->task_manager);
1358 }
1359
1360 METHOD(ike_sa_t, retry_initiate, status_t,
1361 private_ike_sa_t *this)
1362 {
1363 if (this->retry_initiate_queued)
1364 {
1365 this->retry_initiate_queued = FALSE;
1366 return initiate(this, NULL, 0, NULL, NULL);
1367 }
1368 return SUCCESS;
1369 }
1370
1371 METHOD(ike_sa_t, process_message, status_t,
1372 private_ike_sa_t *this, message_t *message)
1373 {
1374 status_t status;
1375
1376 if (this->state == IKE_PASSIVE)
1377 { /* do not handle messages in passive state */
1378 return FAILED;
1379 }
1380 if (message->get_major_version(message) != this->version)
1381 {
1382 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1383 exchange_type_names, message->get_exchange_type(message),
1384 message->get_major_version(message),
1385 ike_version_names, this->version);
1386 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1387 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1388 return FAILED;
1389 }
1390 status = this->task_manager->process_message(this->task_manager, message);
1391 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1392 {
1393 /* authentication completed */
1394 this->flush_auth_cfg = FALSE;
1395 flush_auth_cfgs(this);
1396 }
1397 return status;
1398 }
1399
1400 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1401 private_ike_sa_t *this)
1402 {
1403 return this->ike_sa_id;
1404 }
1405
1406 METHOD(ike_sa_t, get_version, ike_version_t,
1407 private_ike_sa_t *this)
1408 {
1409 return this->version;
1410 }
1411
1412 METHOD(ike_sa_t, get_my_id, identification_t*,
1413 private_ike_sa_t *this)
1414 {
1415 return this->my_id;
1416 }
1417
1418 METHOD(ike_sa_t, set_my_id, void,
1419 private_ike_sa_t *this, identification_t *me)
1420 {
1421 DESTROY_IF(this->my_id);
1422 this->my_id = me;
1423 }
1424
1425 METHOD(ike_sa_t, get_other_id, identification_t*,
1426 private_ike_sa_t *this)
1427 {
1428 return this->other_id;
1429 }
1430
1431 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1432 private_ike_sa_t *this)
1433 {
1434 identification_t *id = NULL, *current;
1435 enumerator_t *enumerator;
1436 auth_cfg_t *cfg;
1437
1438 enumerator = array_create_enumerator(this->other_auths);
1439 while (enumerator->enumerate(enumerator, &cfg))
1440 {
1441 /* prefer EAP-Identity of last round */
1442 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1443 if (!current || current->get_type(current) == ID_ANY)
1444 {
1445 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1446 }
1447 if (!current || current->get_type(current) == ID_ANY)
1448 {
1449 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1450 }
1451 if (current && current->get_type(current) != ID_ANY)
1452 {
1453 id = current;
1454 continue;
1455 }
1456 }
1457 enumerator->destroy(enumerator);
1458 if (id)
1459 {
1460 return id;
1461 }
1462 return this->other_id;
1463 }
1464
1465 METHOD(ike_sa_t, set_other_id, void,
1466 private_ike_sa_t *this, identification_t *other)
1467 {
1468 DESTROY_IF(this->other_id);
1469 this->other_id = other;
1470 }
1471
1472 METHOD(ike_sa_t, add_child_sa, void,
1473 private_ike_sa_t *this, child_sa_t *child_sa)
1474 {
1475 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
1476 charon->child_sa_manager->add(charon->child_sa_manager,
1477 child_sa, &this->public);
1478 }
1479
1480 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1481 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1482 {
1483 enumerator_t *enumerator;
1484 child_sa_t *current, *found = NULL;
1485
1486 enumerator = array_create_enumerator(this->child_sas);
1487 while (enumerator->enumerate(enumerator, (void**)&current))
1488 {
1489 if (current->get_spi(current, inbound) == spi &&
1490 current->get_protocol(current) == protocol)
1491 {
1492 found = current;
1493 }
1494 }
1495 enumerator->destroy(enumerator);
1496 return found;
1497 }
1498
1499 METHOD(ike_sa_t, get_child_count, int,
1500 private_ike_sa_t *this)
1501 {
1502 return array_count(this->child_sas);
1503 }
1504
1505 /**
1506 * Private data of a create_child_sa_enumerator()
1507 */
1508 typedef struct {
1509 /** implements enumerator */
1510 enumerator_t public;
1511 /** inner array enumerator */
1512 enumerator_t *inner;
1513 /** current item */
1514 child_sa_t *current;
1515 } child_enumerator_t;
1516
1517 METHOD(enumerator_t, child_enumerate, bool,
1518 child_enumerator_t *this, child_sa_t **child_sa)
1519 {
1520 if (this->inner->enumerate(this->inner, &this->current))
1521 {
1522 *child_sa = this->current;
1523 return TRUE;
1524 }
1525 return FALSE;
1526 }
1527
1528 METHOD(enumerator_t, child_enumerator_destroy, void,
1529 child_enumerator_t *this)
1530 {
1531 this->inner->destroy(this->inner);
1532 free(this);
1533 }
1534
1535 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1536 private_ike_sa_t *this)
1537 {
1538 child_enumerator_t *enumerator;
1539
1540 INIT(enumerator,
1541 .public = {
1542 .enumerate = (void*)_child_enumerate,
1543 .destroy = _child_enumerator_destroy,
1544 },
1545 .inner = array_create_enumerator(this->child_sas),
1546 );
1547 return &enumerator->public;
1548 }
1549
1550 METHOD(ike_sa_t, remove_child_sa, void,
1551 private_ike_sa_t *this, enumerator_t *enumerator)
1552 {
1553 child_enumerator_t *ce = (child_enumerator_t*)enumerator;
1554
1555 charon->child_sa_manager->remove(charon->child_sa_manager, ce->current);
1556 array_remove_at(this->child_sas, ce->inner);
1557 }
1558
1559 METHOD(ike_sa_t, rekey_child_sa, status_t,
1560 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1561 {
1562 if (this->state == IKE_PASSIVE)
1563 {
1564 return INVALID_STATE;
1565 }
1566 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1567 return this->task_manager->initiate(this->task_manager);
1568 }
1569
1570 METHOD(ike_sa_t, delete_child_sa, status_t,
1571 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool expired)
1572 {
1573 if (this->state == IKE_PASSIVE)
1574 {
1575 return INVALID_STATE;
1576 }
1577 this->task_manager->queue_child_delete(this->task_manager,
1578 protocol, spi, expired);
1579 return this->task_manager->initiate(this->task_manager);
1580 }
1581
1582 METHOD(ike_sa_t, destroy_child_sa, status_t,
1583 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1584 {
1585 enumerator_t *enumerator;
1586 child_sa_t *child_sa;
1587 status_t status = NOT_FOUND;
1588
1589 enumerator = create_child_sa_enumerator(this);
1590 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1591 {
1592 if (child_sa->get_protocol(child_sa) == protocol &&
1593 child_sa->get_spi(child_sa, TRUE) == spi)
1594 {
1595 remove_child_sa(this, enumerator);
1596 child_sa->destroy(child_sa);
1597 status = SUCCESS;
1598 break;
1599 }
1600 }
1601 enumerator->destroy(enumerator);
1602 return status;
1603 }
1604
1605 METHOD(ike_sa_t, delete_, status_t,
1606 private_ike_sa_t *this)
1607 {
1608 switch (this->state)
1609 {
1610 case IKE_REKEYING:
1611 if (this->version == IKEV1)
1612 { /* SA has been reauthenticated, delete */
1613 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1614 break;
1615 }
1616 /* FALL */
1617 case IKE_ESTABLISHED:
1618 if (time_monotonic(NULL) >= this->stats[STAT_DELETE])
1619 { /* IKE_SA hard lifetime hit */
1620 charon->bus->alert(charon->bus, ALERT_IKE_SA_EXPIRED);
1621 }
1622 this->task_manager->queue_ike_delete(this->task_manager);
1623 return this->task_manager->initiate(this->task_manager);
1624 case IKE_CREATED:
1625 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1626 break;
1627 case IKE_PASSIVE:
1628 break;
1629 default:
1630 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1631 "without notification", ike_sa_state_names, this->state);
1632 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1633 break;
1634 }
1635 return DESTROY_ME;
1636 }
1637
1638 METHOD(ike_sa_t, rekey, status_t,
1639 private_ike_sa_t *this)
1640 {
1641 if (this->state == IKE_PASSIVE)
1642 {
1643 return INVALID_STATE;
1644 }
1645 this->task_manager->queue_ike_rekey(this->task_manager);
1646 return this->task_manager->initiate(this->task_manager);
1647 }
1648
1649 METHOD(ike_sa_t, reauth, status_t,
1650 private_ike_sa_t *this)
1651 {
1652 if (this->state == IKE_PASSIVE)
1653 {
1654 return INVALID_STATE;
1655 }
1656 if (this->state == IKE_CONNECTING)
1657 {
1658 DBG0(DBG_IKE, "reinitiating IKE_SA %s[%d]",
1659 get_name(this), this->unique_id);
1660 reset(this);
1661 this->task_manager->queue_ike(this->task_manager);
1662 return this->task_manager->initiate(this->task_manager);
1663 }
1664 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1665 * If the peer does not support RFC4478, there is no way to keep the
1666 * IKE_SA up. */
1667 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1668 {
1669 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1670 if (array_count(this->other_vips) != 0 ||
1671 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1672 has_condition(this, COND_EAP_AUTHENTICATED)
1673 #ifdef ME
1674 /* as mediation server we too cannot reauth the IKE_SA */
1675 || this->is_mediation_server
1676 #endif /* ME */
1677 )
1678 {
1679 time_t del, now;
1680
1681 del = this->stats[STAT_DELETE];
1682 now = time_monotonic(NULL);
1683 DBG1(DBG_IKE, "IKE_SA %s[%d] will timeout in %V",
1684 get_name(this), this->unique_id, &now, &del);
1685 return FAILED;
1686 }
1687 else
1688 {
1689 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d] actively",
1690 get_name(this), this->unique_id);
1691 }
1692 }
1693 else
1694 {
1695 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d]",
1696 get_name(this), this->unique_id);
1697 }
1698 set_condition(this, COND_REAUTHENTICATING, TRUE);
1699 this->task_manager->queue_ike_reauth(this->task_manager);
1700 return this->task_manager->initiate(this->task_manager);
1701 }
1702
1703 /**
1704 * Check if tasks to create CHILD_SAs are queued in the given queue
1705 */
1706 static bool is_child_queued(private_ike_sa_t *this, task_queue_t queue)
1707 {
1708 enumerator_t *enumerator;
1709 task_t *task;
1710 bool found = FALSE;
1711
1712 enumerator = this->task_manager->create_task_enumerator(this->task_manager,
1713 queue);
1714 while (enumerator->enumerate(enumerator, &task))
1715 {
1716 if (task->get_type(task) == TASK_CHILD_CREATE ||
1717 task->get_type(task) == TASK_QUICK_MODE)
1718 {
1719 found = TRUE;
1720 break;
1721 }
1722 }
1723 enumerator->destroy(enumerator);
1724 return found;
1725 }
1726
1727 METHOD(ike_sa_t, reestablish, status_t,
1728 private_ike_sa_t *this)
1729 {
1730 ike_sa_t *new;
1731 host_t *host;
1732 action_t action;
1733 enumerator_t *enumerator;
1734 child_sa_t *child_sa;
1735 child_cfg_t *child_cfg;
1736 bool restart = FALSE;
1737 status_t status = FAILED;
1738
1739 if (has_condition(this, COND_REAUTHENTICATING))
1740 { /* only reauthenticate if we have children */
1741 if (array_count(this->child_sas) == 0
1742 #ifdef ME
1743 /* allow reauth of mediation connections without CHILD_SAs */
1744 && !this->peer_cfg->is_mediation(this->peer_cfg)
1745 #endif /* ME */
1746 )
1747 {
1748 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA "
1749 "to recreate");
1750 }
1751 else
1752 {
1753 restart = TRUE;
1754 }
1755 }
1756 else
1757 { /* check if we have children to keep up at all */
1758 enumerator = array_create_enumerator(this->child_sas);
1759 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1760 {
1761 if (this->state == IKE_DELETING)
1762 {
1763 action = child_sa->get_close_action(child_sa);
1764 }
1765 else
1766 {
1767 action = child_sa->get_dpd_action(child_sa);
1768 }
1769 switch (action)
1770 {
1771 case ACTION_RESTART:
1772 restart = TRUE;
1773 break;
1774 case ACTION_ROUTE:
1775 charon->traps->install(charon->traps, this->peer_cfg,
1776 child_sa->get_config(child_sa),
1777 child_sa->get_reqid(child_sa));
1778 break;
1779 default:
1780 break;
1781 }
1782 }
1783 enumerator->destroy(enumerator);
1784 /* check if we have tasks that recreate children */
1785 if (!restart)
1786 {
1787 restart = is_child_queued(this, TASK_QUEUE_ACTIVE) ||
1788 is_child_queued(this, TASK_QUEUE_QUEUED);
1789 }
1790 #ifdef ME
1791 /* mediation connections have no children, keep them up anyway */
1792 if (this->peer_cfg->is_mediation(this->peer_cfg))
1793 {
1794 restart = TRUE;
1795 }
1796 #endif /* ME */
1797 }
1798 if (!restart)
1799 {
1800 return FAILED;
1801 }
1802
1803 /* check if we are able to reestablish this IKE_SA */
1804 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1805 (array_count(this->other_vips) != 0 ||
1806 has_condition(this, COND_EAP_AUTHENTICATED)
1807 #ifdef ME
1808 || this->is_mediation_server
1809 #endif /* ME */
1810 ))
1811 {
1812 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1813 return FAILED;
1814 }
1815
1816 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1817 this->version, TRUE);
1818 if (!new)
1819 {
1820 return FAILED;
1821 }
1822 new->set_peer_cfg(new, this->peer_cfg);
1823 host = this->other_host;
1824 new->set_other_host(new, host->clone(host));
1825 host = this->my_host;
1826 new->set_my_host(new, host->clone(host));
1827 charon->bus->ike_reestablish_pre(charon->bus, &this->public, new);
1828 /* resolve hosts but use the old addresses above as fallback */
1829 resolve_hosts((private_ike_sa_t*)new);
1830 /* if we already have a virtual IP, we reuse it */
1831 enumerator = array_create_enumerator(this->my_vips);
1832 while (enumerator->enumerate(enumerator, &host))
1833 {
1834 new->add_virtual_ip(new, TRUE, host);
1835 }
1836 enumerator->destroy(enumerator);
1837
1838 #ifdef ME
1839 if (this->peer_cfg->is_mediation(this->peer_cfg))
1840 {
1841 status = new->initiate(new, NULL, 0, NULL, NULL);
1842 }
1843 else
1844 #endif /* ME */
1845 {
1846 /* handle existing CHILD_SAs */
1847 enumerator = create_child_sa_enumerator(this);
1848 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1849 {
1850 if (has_condition(this, COND_REAUTHENTICATING))
1851 {
1852 switch (child_sa->get_state(child_sa))
1853 {
1854 case CHILD_ROUTED:
1855 { /* move routed child directly */
1856 remove_child_sa(this, enumerator);
1857 new->add_child_sa(new, child_sa);
1858 action = ACTION_NONE;
1859 break;
1860 }
1861 default:
1862 { /* initiate/queue all other CHILD_SAs */
1863 action = ACTION_RESTART;
1864 break;
1865 }
1866 }
1867 }
1868 else
1869 { /* only restart CHILD_SAs that are configured accordingly */
1870 if (this->state == IKE_DELETING)
1871 {
1872 action = child_sa->get_close_action(child_sa);
1873 }
1874 else
1875 {
1876 action = child_sa->get_dpd_action(child_sa);
1877 }
1878 }
1879 switch (action)
1880 {
1881 case ACTION_RESTART:
1882 child_cfg = child_sa->get_config(child_sa);
1883 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1884 child_cfg->get_name(child_cfg));
1885 child_cfg->get_ref(child_cfg);
1886 status = new->initiate(new, child_cfg,
1887 child_sa->get_reqid(child_sa), NULL, NULL);
1888 break;
1889 default:
1890 continue;
1891 }
1892 if (status == DESTROY_ME)
1893 {
1894 break;
1895 }
1896 }
1897 enumerator->destroy(enumerator);
1898 /* adopt any active or queued CHILD-creating tasks */
1899 if (status != DESTROY_ME)
1900 {
1901 task_manager_t *other_tasks = ((private_ike_sa_t*)new)->task_manager;
1902 other_tasks->adopt_child_tasks(other_tasks, this->task_manager);
1903 if (new->get_state(new) == IKE_CREATED)
1904 {
1905 status = new->initiate(new, NULL, 0, NULL, NULL);
1906 }
1907 }
1908 }
1909
1910 if (status == DESTROY_ME)
1911 {
1912 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
1913 FALSE);
1914 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1915 status = FAILED;
1916 }
1917 else
1918 {
1919 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
1920 TRUE);
1921 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1922 status = SUCCESS;
1923 }
1924 charon->bus->set_sa(charon->bus, &this->public);
1925 return status;
1926 }
1927
1928 METHOD(ike_sa_t, retransmit, status_t,
1929 private_ike_sa_t *this, u_int32_t message_id)
1930 {
1931 if (this->state == IKE_PASSIVE)
1932 {
1933 return INVALID_STATE;
1934 }
1935 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1936 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1937 {
1938 /* send a proper signal to brief interested bus listeners */
1939 switch (this->state)
1940 {
1941 case IKE_CONNECTING:
1942 {
1943 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1944 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1945 charon->bus->alert(charon->bus, ALERT_PEER_INIT_UNREACHABLE,
1946 this->keyingtry);
1947 this->keyingtry++;
1948 if (tries == 0 || tries > this->keyingtry)
1949 {
1950 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1951 this->keyingtry + 1, tries);
1952 reset(this);
1953 resolve_hosts(this);
1954 this->task_manager->queue_ike(this->task_manager);
1955 return this->task_manager->initiate(this->task_manager);
1956 }
1957 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1958 break;
1959 }
1960 case IKE_DELETING:
1961 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1962 if (has_condition(this, COND_REAUTHENTICATING))
1963 {
1964 DBG1(DBG_IKE, "delete during reauthentication failed, "
1965 "trying to reestablish IKE_SA anyway");
1966 reestablish(this);
1967 }
1968 break;
1969 case IKE_REKEYING:
1970 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1971 /* FALL */
1972 default:
1973 reestablish(this);
1974 break;
1975 }
1976 if (this->state != IKE_CONNECTING)
1977 {
1978 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1979 }
1980 return DESTROY_ME;
1981 }
1982 return SUCCESS;
1983 }
1984
1985 METHOD(ike_sa_t, set_auth_lifetime, status_t,
1986 private_ike_sa_t *this, u_int32_t lifetime)
1987 {
1988 u_int32_t diff, hard, soft, now;
1989 bool send_update;
1990
1991 diff = this->peer_cfg->get_over_time(this->peer_cfg);
1992 now = time_monotonic(NULL);
1993 hard = now + lifetime;
1994 soft = hard - diff;
1995
1996 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1997 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1998 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
1999 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
2000 (array_count(this->other_vips) != 0 ||
2001 has_condition(this, COND_EAP_AUTHENTICATED));
2002
2003 if (lifetime < diff)
2004 {
2005 this->stats[STAT_REAUTH] = now;
2006
2007 if (!send_update)
2008 {
2009 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
2010 "starting reauthentication", lifetime);
2011 lib->processor->queue_job(lib->processor,
2012 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
2013 }
2014 }
2015 else if (this->stats[STAT_REAUTH] == 0 ||
2016 this->stats[STAT_REAUTH] > soft)
2017 {
2018 this->stats[STAT_REAUTH] = soft;
2019 if (!send_update)
2020 {
2021 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
2022 "reauthentication in %ds", lifetime, lifetime - diff);
2023 lib->scheduler->schedule_job(lib->scheduler,
2024 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2025 lifetime - diff);
2026 }
2027 }
2028 else
2029 {
2030 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
2031 "reauthentication already scheduled in %ds", lifetime,
2032 this->stats[STAT_REAUTH] - time_monotonic(NULL));
2033 send_update = FALSE;
2034 }
2035 /* give at least some seconds to reauthenticate */
2036 this->stats[STAT_DELETE] = max(hard, now + 10);
2037
2038 #ifdef USE_IKEV2
2039 if (send_update)
2040 {
2041 ike_auth_lifetime_t *task;
2042
2043 task = ike_auth_lifetime_create(&this->public, TRUE);
2044 this->task_manager->queue_task(this->task_manager, &task->task);
2045 return this->task_manager->initiate(this->task_manager);
2046 }
2047 #endif
2048 return SUCCESS;
2049 }
2050
2051 /**
2052 * Check if the current combination of source and destination address is still
2053 * valid.
2054 */
2055 static bool is_current_path_valid(private_ike_sa_t *this)
2056 {
2057 bool valid = FALSE;
2058 host_t *src;
2059 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
2060 this->other_host, this->my_host);
2061 if (src)
2062 {
2063 if (src->ip_equals(src, this->my_host))
2064 {
2065 valid = TRUE;
2066 }
2067 src->destroy(src);
2068 }
2069 return valid;
2070 }
2071
2072 /**
2073 * Check if we have any path avialable for this IKE SA.
2074 */
2075 static bool is_any_path_valid(private_ike_sa_t *this)
2076 {
2077 bool valid = FALSE;
2078 enumerator_t *enumerator;
2079 host_t *src = NULL, *addr;
2080 int family = AF_UNSPEC;
2081
2082 switch (charon->socket->supported_families(charon->socket))
2083 {
2084 case SOCKET_FAMILY_IPV4:
2085 family = AF_INET;
2086 break;
2087 case SOCKET_FAMILY_IPV6:
2088 family = AF_INET6;
2089 break;
2090 case SOCKET_FAMILY_BOTH:
2091 case SOCKET_FAMILY_NONE:
2092 break;
2093 }
2094
2095 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
2096 enumerator = create_peer_address_enumerator(this);
2097 while (enumerator->enumerate(enumerator, &addr))
2098 {
2099 if (family != AF_UNSPEC && addr->get_family(addr) != family)
2100 {
2101 continue;
2102 }
2103 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
2104 src = hydra->kernel_interface->get_source_addr(
2105 hydra->kernel_interface, addr, NULL);
2106 if (src)
2107 {
2108 break;
2109 }
2110 }
2111 enumerator->destroy(enumerator);
2112 if (src)
2113 {
2114 valid = TRUE;
2115 src->destroy(src);
2116 }
2117 return valid;
2118 }
2119
2120 METHOD(ike_sa_t, roam, status_t,
2121 private_ike_sa_t *this, bool address)
2122 {
2123 switch (this->state)
2124 {
2125 case IKE_CREATED:
2126 case IKE_DELETING:
2127 case IKE_DESTROYING:
2128 case IKE_PASSIVE:
2129 return SUCCESS;
2130 default:
2131 break;
2132 }
2133
2134 /* keep existing path if possible */
2135 if (is_current_path_valid(this))
2136 {
2137 DBG2(DBG_IKE, "keeping connection path %H - %H",
2138 this->my_host, this->other_host);
2139 set_condition(this, COND_STALE, FALSE);
2140
2141 if (supports_extension(this, EXT_MOBIKE) && address)
2142 { /* if any addresses changed, send an updated list */
2143 DBG1(DBG_IKE, "sending address list update using MOBIKE");
2144 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
2145 return this->task_manager->initiate(this->task_manager);
2146 }
2147 return SUCCESS;
2148 }
2149
2150 if (!is_any_path_valid(this))
2151 {
2152 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
2153 this->other_host);
2154 set_condition(this, COND_STALE, TRUE);
2155 return SUCCESS;
2156 }
2157 set_condition(this, COND_STALE, FALSE);
2158
2159 /* update addresses with mobike, if supported ... */
2160 if (supports_extension(this, EXT_MOBIKE))
2161 {
2162 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2163 { /* responder updates the peer about changed address config */
2164 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
2165 "implicitly requesting an address change");
2166 address = TRUE;
2167 }
2168 else
2169 {
2170 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2171 }
2172 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
2173 return this->task_manager->initiate(this->task_manager);
2174 }
2175
2176 /* ... reauth if not */
2177 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2178 { /* responder does not reauthenticate */
2179 set_condition(this, COND_STALE, TRUE);
2180 return SUCCESS;
2181 }
2182 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
2183 /* since our previous path is not valid anymore, try and find a new one */
2184 resolve_hosts(this);
2185 return reauth(this);
2186 }
2187
2188 METHOD(ike_sa_t, add_configuration_attribute, void,
2189 private_ike_sa_t *this, attribute_handler_t *handler,
2190 configuration_attribute_type_t type, chunk_t data)
2191 {
2192 attribute_entry_t entry = {
2193 .handler = handler,
2194 .type = type,
2195 .data = chunk_clone(data),
2196 };
2197 array_insert(this->attributes, ARRAY_TAIL, &entry);
2198 }
2199
2200 /**
2201 * Enumerator filter for attributes
2202 */
2203 static bool filter_attribute(void *null, attribute_entry_t **in,
2204 configuration_attribute_type_t *type, void *in2,
2205 chunk_t *data, void *in3, bool *handled)
2206 {
2207 *type = (*in)->type;
2208 *data = (*in)->data;
2209 *handled = (*in)->handler != NULL;
2210 return TRUE;
2211 }
2212
2213 METHOD(ike_sa_t, create_attribute_enumerator, enumerator_t*,
2214 private_ike_sa_t *this)
2215 {
2216 return enumerator_create_filter(array_create_enumerator(this->attributes),
2217 (void*)filter_attribute, NULL, NULL);
2218 }
2219
2220 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
2221 private_ike_sa_t *this, task_queue_t queue)
2222 {
2223 return this->task_manager->create_task_enumerator(this->task_manager, queue);
2224 }
2225
2226 METHOD(ike_sa_t, flush_queue, void,
2227 private_ike_sa_t *this, task_queue_t queue)
2228 {
2229 this->task_manager->flush_queue(this->task_manager, queue);
2230 }
2231
2232 METHOD(ike_sa_t, queue_task, void,
2233 private_ike_sa_t *this, task_t *task)
2234 {
2235 this->task_manager->queue_task(this->task_manager, task);
2236 }
2237
2238 METHOD(ike_sa_t, inherit_pre, void,
2239 private_ike_sa_t *this, ike_sa_t *other_public)
2240 {
2241 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2242
2243 /* apply config and hosts */
2244 set_peer_cfg(this, other->peer_cfg);
2245 set_my_host(this, other->my_host->clone(other->my_host));
2246 set_other_host(this, other->other_host->clone(other->other_host));
2247
2248 /* apply extensions and conditions with a few exceptions */
2249 this->extensions = other->extensions;
2250 this->conditions = other->conditions;
2251 this->conditions &= ~COND_STALE;
2252 this->conditions &= ~COND_REAUTHENTICATING;
2253 }
2254
2255 METHOD(ike_sa_t, inherit_post, void,
2256 private_ike_sa_t *this, ike_sa_t *other_public)
2257 {
2258 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2259 child_sa_t *child_sa;
2260 enumerator_t *enumerator;
2261 attribute_entry_t entry;
2262 auth_cfg_t *cfg;
2263 host_t *vip;
2264
2265 /* apply hosts and ids */
2266 this->my_host->destroy(this->my_host);
2267 this->other_host->destroy(this->other_host);
2268 this->my_id->destroy(this->my_id);
2269 this->other_id->destroy(this->other_id);
2270 this->my_host = other->my_host->clone(other->my_host);
2271 this->other_host = other->other_host->clone(other->other_host);
2272 this->my_id = other->my_id->clone(other->my_id);
2273 this->other_id = other->other_id->clone(other->other_id);
2274
2275 /* apply assigned virtual IPs... */
2276 while (array_remove(other->my_vips, ARRAY_HEAD, &vip))
2277 {
2278 array_insert_create(&this->my_vips, ARRAY_TAIL, vip);
2279 }
2280 while (array_remove(other->other_vips, ARRAY_HEAD, &vip))
2281 {
2282 array_insert_create(&this->other_vips, ARRAY_TAIL, vip);
2283 }
2284
2285 /* MOBIKE additional addresses */
2286 while (array_remove(other->peer_addresses, ARRAY_HEAD, &vip))
2287 {
2288 array_insert_create(&this->peer_addresses, ARRAY_TAIL, vip);
2289 }
2290
2291 /* authentication information */
2292 enumerator = array_create_enumerator(other->my_auths);
2293 while (enumerator->enumerate(enumerator, &cfg))
2294 {
2295 array_insert(this->my_auths, ARRAY_TAIL, cfg->clone(cfg));
2296 }
2297 enumerator->destroy(enumerator);
2298 enumerator = array_create_enumerator(other->other_auths);
2299 while (enumerator->enumerate(enumerator, &cfg))
2300 {
2301 array_insert(this->other_auths, ARRAY_TAIL, cfg->clone(cfg));
2302 }
2303 enumerator->destroy(enumerator);
2304
2305 /* ... and configuration attributes */
2306 while (array_remove(other->attributes, ARRAY_HEAD, &entry))
2307 {
2308 array_insert(this->attributes, ARRAY_TAIL, &entry);
2309 }
2310
2311 /* inherit all conditions */
2312 this->conditions = other->conditions;
2313 if (this->conditions & COND_NAT_HERE)
2314 {
2315 send_keepalive(this);
2316 }
2317
2318 #ifdef ME
2319 if (other->is_mediation_server)
2320 {
2321 act_as_mediation_server(this);
2322 }
2323 else if (other->server_reflexive_host)
2324 {
2325 this->server_reflexive_host = other->server_reflexive_host->clone(
2326 other->server_reflexive_host);
2327 }
2328 #endif /* ME */
2329
2330 /* adopt all children */
2331 while (array_remove(other->child_sas, ARRAY_HEAD, &child_sa))
2332 {
2333 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
2334 add_child_sa(this, child_sa);
2335 }
2336
2337 /* move pending tasks to the new IKE_SA */
2338 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2339
2340 /* reauthentication timeout survives a rekeying */
2341 if (other->stats[STAT_REAUTH])
2342 {
2343 time_t reauth, delete, now = time_monotonic(NULL);
2344
2345 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2346 reauth = this->stats[STAT_REAUTH] - now;
2347 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2348 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2349 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2350 "lifetime reduced to %ds", reauth, delete);
2351 lib->scheduler->schedule_job(lib->scheduler,
2352 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2353 lib->scheduler->schedule_job(lib->scheduler,
2354 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2355 }
2356 }
2357
2358 METHOD(ike_sa_t, destroy, void,
2359 private_ike_sa_t *this)
2360 {
2361 attribute_entry_t entry;
2362 child_sa_t *child_sa;
2363 host_t *vip;
2364
2365 charon->bus->set_sa(charon->bus, &this->public);
2366
2367 set_state(this, IKE_DESTROYING);
2368 if (this->task_manager)
2369 {
2370 this->task_manager->flush(this->task_manager);
2371 }
2372
2373 /* remove attributes first, as we pass the IKE_SA to the handler */
2374 charon->bus->handle_vips(charon->bus, &this->public, FALSE);
2375 while (array_remove(this->attributes, ARRAY_TAIL, &entry))
2376 {
2377 if (entry.handler)
2378 {
2379 charon->attributes->release(charon->attributes, entry.handler,
2380 &this->public, entry.type, entry.data);
2381 }
2382 free(entry.data.ptr);
2383 }
2384 /* uninstall CHILD_SAs before virtual IPs, otherwise we might kill
2385 * routes that the CHILD_SA tries to uninstall. */
2386 while (array_remove(this->child_sas, ARRAY_TAIL, &child_sa))
2387 {
2388 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
2389 child_sa->destroy(child_sa);
2390 }
2391 while (array_remove(this->my_vips, ARRAY_TAIL, &vip))
2392 {
2393 hydra->kernel_interface->del_ip(hydra->kernel_interface, vip, -1, TRUE);
2394 vip->destroy(vip);
2395 }
2396 if (array_count(this->other_vips))
2397 {
2398 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
2399 }
2400 while (array_remove(this->other_vips, ARRAY_TAIL, &vip))
2401 {
2402 if (this->peer_cfg)
2403 {
2404 linked_list_t *pools;
2405
2406 pools = linked_list_create_from_enumerator(
2407 this->peer_cfg->create_pool_enumerator(this->peer_cfg));
2408 charon->attributes->release_address(charon->attributes,
2409 pools, vip, &this->public);
2410 pools->destroy(pools);
2411 }
2412 vip->destroy(vip);
2413 }
2414
2415 /* unset SA after here to avoid usage by the listeners */
2416 charon->bus->set_sa(charon->bus, NULL);
2417
2418 array_destroy(this->child_sas);
2419 DESTROY_IF(this->task_manager);
2420 DESTROY_IF(this->keymat);
2421 array_destroy(this->attributes);
2422 array_destroy(this->my_vips);
2423 array_destroy(this->other_vips);
2424 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
2425 #ifdef ME
2426 if (this->is_mediation_server)
2427 {
2428 charon->mediation_manager->remove(charon->mediation_manager,
2429 this->ike_sa_id);
2430 }
2431 DESTROY_IF(this->server_reflexive_host);
2432 chunk_free(&this->connect_id);
2433 #endif /* ME */
2434 free(this->nat_detection_dest.ptr);
2435
2436 DESTROY_IF(this->my_host);
2437 DESTROY_IF(this->other_host);
2438 DESTROY_IF(this->my_id);
2439 DESTROY_IF(this->other_id);
2440 DESTROY_IF(this->local_host);
2441 DESTROY_IF(this->remote_host);
2442
2443 DESTROY_IF(this->ike_cfg);
2444 DESTROY_IF(this->peer_cfg);
2445 DESTROY_IF(this->proposal);
2446 this->my_auth->destroy(this->my_auth);
2447 this->other_auth->destroy(this->other_auth);
2448 array_destroy_offset(this->my_auths, offsetof(auth_cfg_t, destroy));
2449 array_destroy_offset(this->other_auths, offsetof(auth_cfg_t, destroy));
2450
2451 this->ike_sa_id->destroy(this->ike_sa_id);
2452 free(this);
2453 }
2454
2455 /*
2456 * Described in header.
2457 */
2458 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
2459 ike_version_t version)
2460 {
2461 private_ike_sa_t *this;
2462 static refcount_t unique_id = 0;
2463
2464 if (version == IKE_ANY)
2465 { /* prefer IKEv2 if protocol not specified */
2466 #ifdef USE_IKEV2
2467 version = IKEV2;
2468 #else
2469 version = IKEV1;
2470 #endif
2471 }
2472
2473 INIT(this,
2474 .public = {
2475 .get_version = _get_version,
2476 .get_state = _get_state,
2477 .set_state = _set_state,
2478 .get_name = _get_name,
2479 .get_statistic = _get_statistic,
2480 .set_statistic = _set_statistic,
2481 .process_message = _process_message,
2482 .initiate = _initiate,
2483 .retry_initiate = _retry_initiate,
2484 .get_ike_cfg = _get_ike_cfg,
2485 .set_ike_cfg = _set_ike_cfg,
2486 .get_peer_cfg = _get_peer_cfg,
2487 .set_peer_cfg = _set_peer_cfg,
2488 .get_auth_cfg = _get_auth_cfg,
2489 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2490 .add_auth_cfg = _add_auth_cfg,
2491 .get_proposal = _get_proposal,
2492 .set_proposal = _set_proposal,
2493 .get_id = _get_id,
2494 .get_my_host = _get_my_host,
2495 .set_my_host = _set_my_host,
2496 .get_other_host = _get_other_host,
2497 .set_other_host = _set_other_host,
2498 .set_message_id = _set_message_id,
2499 .float_ports = _float_ports,
2500 .update_hosts = _update_hosts,
2501 .get_my_id = _get_my_id,
2502 .set_my_id = _set_my_id,
2503 .get_other_id = _get_other_id,
2504 .set_other_id = _set_other_id,
2505 .get_other_eap_id = _get_other_eap_id,
2506 .enable_extension = _enable_extension,
2507 .supports_extension = _supports_extension,
2508 .set_condition = _set_condition,
2509 .has_condition = _has_condition,
2510 .set_pending_updates = _set_pending_updates,
2511 .get_pending_updates = _get_pending_updates,
2512 .create_peer_address_enumerator = _create_peer_address_enumerator,
2513 .add_peer_address = _add_peer_address,
2514 .clear_peer_addresses = _clear_peer_addresses,
2515 .has_mapping_changed = _has_mapping_changed,
2516 .retransmit = _retransmit,
2517 .delete = _delete_,
2518 .destroy = _destroy,
2519 .send_dpd = _send_dpd,
2520 .send_keepalive = _send_keepalive,
2521 .get_keymat = _get_keymat,
2522 .add_child_sa = _add_child_sa,
2523 .get_child_sa = _get_child_sa,
2524 .get_child_count = _get_child_count,
2525 .create_child_sa_enumerator = _create_child_sa_enumerator,
2526 .remove_child_sa = _remove_child_sa,
2527 .rekey_child_sa = _rekey_child_sa,
2528 .delete_child_sa = _delete_child_sa,
2529 .destroy_child_sa = _destroy_child_sa,
2530 .rekey = _rekey,
2531 .reauth = _reauth,
2532 .reestablish = _reestablish,
2533 .set_auth_lifetime = _set_auth_lifetime,
2534 .roam = _roam,
2535 .inherit_pre = _inherit_pre,
2536 .inherit_post = _inherit_post,
2537 .generate_message = _generate_message,
2538 .generate_message_fragmented = _generate_message_fragmented,
2539 .reset = _reset,
2540 .get_unique_id = _get_unique_id,
2541 .add_virtual_ip = _add_virtual_ip,
2542 .clear_virtual_ips = _clear_virtual_ips,
2543 .create_virtual_ip_enumerator = _create_virtual_ip_enumerator,
2544 .add_configuration_attribute = _add_configuration_attribute,
2545 .create_attribute_enumerator = _create_attribute_enumerator,
2546 .set_kmaddress = _set_kmaddress,
2547 .create_task_enumerator = _create_task_enumerator,
2548 .flush_queue = _flush_queue,
2549 .queue_task = _queue_task,
2550 #ifdef ME
2551 .act_as_mediation_server = _act_as_mediation_server,
2552 .get_server_reflexive_host = _get_server_reflexive_host,
2553 .set_server_reflexive_host = _set_server_reflexive_host,
2554 .get_connect_id = _get_connect_id,
2555 .initiate_mediation = _initiate_mediation,
2556 .initiate_mediated = _initiate_mediated,
2557 .relay = _relay,
2558 .callback = _callback,
2559 .respond = _respond,
2560 #endif /* ME */
2561 },
2562 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2563 .version = version,
2564 .my_host = host_create_any(AF_INET),
2565 .other_host = host_create_any(AF_INET),
2566 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2567 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2568 .keymat = keymat_create(version, initiator),
2569 .state = IKE_CREATED,
2570 .stats[STAT_INBOUND] = time_monotonic(NULL),
2571 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2572 .my_auth = auth_cfg_create(),
2573 .other_auth = auth_cfg_create(),
2574 .my_auths = array_create(0, 0),
2575 .other_auths = array_create(0, 0),
2576 .attributes = array_create(sizeof(attribute_entry_t), 0),
2577 .unique_id = ref_get(&unique_id),
2578 .keepalive_interval = lib->settings->get_time(lib->settings,
2579 "%s.keep_alive", KEEPALIVE_INTERVAL, lib->ns),
2580 .retry_initiate_interval = lib->settings->get_time(lib->settings,
2581 "%s.retry_initiate_interval", 0, lib->ns),
2582 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2583 "%s.flush_auth_cfg", FALSE, lib->ns),
2584 .fragment_size = lib->settings->get_int(lib->settings,
2585 "%s.fragment_size", 0, lib->ns),
2586 );
2587
2588 if (version == IKEV2)
2589 { /* always supported with IKEv2 */
2590 enable_extension(this, EXT_DPD);
2591 }
2592
2593 this->task_manager = task_manager_create(&this->public);
2594 this->my_host->set_port(this->my_host,
2595 charon->socket->get_port(charon->socket, FALSE));
2596
2597 if (!this->task_manager || !this->keymat)
2598 {
2599 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2600 destroy(this);
2601 return NULL;
2602 }
2603 return &this->public;
2604 }