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