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