Invoke ike_updown hooks for reauthenticated IKEv1 SAs
[strongswan.git] / src / libcharon / sa / ike_sa.c
1 /*
2 * Copyright (C) 2006-2011 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2009 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <errno.h>
22 #include <time.h>
23
24 #include "ike_sa.h"
25
26 #include <library.h>
27 #include <hydra.h>
28 #include <daemon.h>
29 #include <utils/linked_list.h>
30 #include <utils/lexparser.h>
31 #include <processing/jobs/retransmit_job.h>
32 #include <processing/jobs/delete_ike_sa_job.h>
33 #include <processing/jobs/send_dpd_job.h>
34 #include <processing/jobs/send_keepalive_job.h>
35 #include <processing/jobs/rekey_ike_sa_job.h>
36
37 #ifdef ME
38 #include <sa/ikev2/tasks/ike_me.h>
39 #include <processing/jobs/initiate_mediation_job.h>
40 #endif
41
42 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DESTROYING,
43 "CREATED",
44 "CONNECTING",
45 "ESTABLISHED",
46 "PASSIVE",
47 "REKEYING",
48 "DELETING",
49 "DESTROYING",
50 );
51
52 typedef struct private_ike_sa_t private_ike_sa_t;
53 typedef struct attribute_entry_t attribute_entry_t;
54
55 /**
56 * Private data of an ike_sa_t object.
57 */
58 struct private_ike_sa_t {
59
60 /**
61 * Public members
62 */
63 ike_sa_t public;
64
65 /**
66 * Identifier for the current IKE_SA.
67 */
68 ike_sa_id_t *ike_sa_id;
69
70 /**
71 * IKE version of this SA.
72 */
73 ike_version_t version;
74
75 /**
76 * unique numerical ID for this IKE_SA.
77 */
78 u_int32_t unique_id;
79
80 /**
81 * Current state of the IKE_SA
82 */
83 ike_sa_state_t state;
84
85 /**
86 * IKE configuration used to set up this IKE_SA
87 */
88 ike_cfg_t *ike_cfg;
89
90 /**
91 * Peer and authentication information to establish IKE_SA.
92 */
93 peer_cfg_t *peer_cfg;
94
95 /**
96 * currently used authentication ruleset, local (as auth_cfg_t)
97 */
98 auth_cfg_t *my_auth;
99
100 /**
101 * list of completed local authentication rounds
102 */
103 linked_list_t *my_auths;
104
105 /**
106 * list of completed remote authentication rounds
107 */
108 linked_list_t *other_auths;
109
110 /**
111 * currently used authentication constraints, remote (as auth_cfg_t)
112 */
113 auth_cfg_t *other_auth;
114
115 /**
116 * Selected IKE proposal
117 */
118 proposal_t *proposal;
119
120 /**
121 * Juggles tasks to process messages
122 */
123 task_manager_t *task_manager;
124
125 /**
126 * Address of local host
127 */
128 host_t *my_host;
129
130 /**
131 * Address of remote host
132 */
133 host_t *other_host;
134
135 #ifdef ME
136 /**
137 * Are we mediation server
138 */
139 bool is_mediation_server;
140
141 /**
142 * Server reflexive host
143 */
144 host_t *server_reflexive_host;
145
146 /**
147 * Connect ID
148 */
149 chunk_t connect_id;
150 #endif /* ME */
151
152 /**
153 * Identification used for us
154 */
155 identification_t *my_id;
156
157 /**
158 * Identification used for other
159 */
160 identification_t *other_id;
161
162 /**
163 * set of extensions the peer supports
164 */
165 ike_extension_t extensions;
166
167 /**
168 * set of condition flags currently enabled for this IKE_SA
169 */
170 ike_condition_t conditions;
171
172 /**
173 * Linked List containing the child sa's of the current IKE_SA.
174 */
175 linked_list_t *child_sas;
176
177 /**
178 * keymat of this IKE_SA
179 */
180 keymat_t *keymat;
181
182 /**
183 * Virtual IP on local host, if any
184 */
185 host_t *my_virtual_ip;
186
187 /**
188 * Virtual IP on remote host, if any
189 */
190 host_t *other_virtual_ip;
191
192 /**
193 * List of configuration attributes (attribute_entry_t)
194 */
195 linked_list_t *attributes;
196
197 /**
198 * list of peers additional addresses, transmitted via MOBIKE
199 */
200 linked_list_t *additional_addresses;
201
202 /**
203 * previously value of received DESTINATION_IP hash
204 */
205 chunk_t nat_detection_dest;
206
207 /**
208 * number pending UPDATE_SA_ADDRESS (MOBIKE)
209 */
210 u_int32_t pending_updates;
211
212 /**
213 * NAT keep alive interval
214 */
215 u_int32_t keepalive_interval;
216
217 /**
218 * Timestamps for this IKE_SA
219 */
220 u_int32_t stats[STAT_MAX];
221
222 /**
223 * how many times we have retried so far (keyingtries)
224 */
225 u_int32_t keyingtry;
226
227 /**
228 * local host address to be used for IKE, set via MIGRATE kernel message
229 */
230 host_t *local_host;
231
232 /**
233 * remote host address to be used for IKE, set via MIGRATE kernel message
234 */
235 host_t *remote_host;
236
237 /**
238 * Flush auth configs once established?
239 */
240 bool flush_auth_cfg;
241 };
242
243 /**
244 * Entry to maintain install configuration attributes during IKE_SA lifetime
245 */
246 struct attribute_entry_t {
247 /** handler used to install this attribute */
248 attribute_handler_t *handler;
249 /** attribute type */
250 configuration_attribute_type_t type;
251 /** attribute data */
252 chunk_t data;
253 };
254
255 /**
256 * get the time of the latest traffic processed by the kernel
257 */
258 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
259 {
260 enumerator_t *enumerator;
261 child_sa_t *child_sa;
262 time_t use_time, current;
263
264 if (inbound)
265 {
266 use_time = this->stats[STAT_INBOUND];
267 }
268 else
269 {
270 use_time = this->stats[STAT_OUTBOUND];
271 }
272 enumerator = this->child_sas->create_enumerator(this->child_sas);
273 while (enumerator->enumerate(enumerator, &child_sa))
274 {
275 child_sa->get_usestats(child_sa, inbound, &current, NULL);
276 use_time = max(use_time, current);
277 }
278 enumerator->destroy(enumerator);
279
280 return use_time;
281 }
282
283 METHOD(ike_sa_t, get_unique_id, u_int32_t,
284 private_ike_sa_t *this)
285 {
286 return this->unique_id;
287 }
288
289 METHOD(ike_sa_t, get_name, char*,
290 private_ike_sa_t *this)
291 {
292 if (this->peer_cfg)
293 {
294 return this->peer_cfg->get_name(this->peer_cfg);
295 }
296 return "(unnamed)";
297 }
298
299 METHOD(ike_sa_t, get_statistic, u_int32_t,
300 private_ike_sa_t *this, statistic_t kind)
301 {
302 if (kind < STAT_MAX)
303 {
304 return this->stats[kind];
305 }
306 return 0;
307 }
308
309 METHOD(ike_sa_t, set_statistic, void,
310 private_ike_sa_t *this, statistic_t kind, u_int32_t value)
311 {
312 if (kind < STAT_MAX)
313 {
314 this->stats[kind] = value;
315 }
316 }
317
318 METHOD(ike_sa_t, get_my_host, host_t*,
319 private_ike_sa_t *this)
320 {
321 return this->my_host;
322 }
323
324 METHOD(ike_sa_t, set_my_host, void,
325 private_ike_sa_t *this, host_t *me)
326 {
327 DESTROY_IF(this->my_host);
328 this->my_host = me;
329 }
330
331 METHOD(ike_sa_t, get_other_host, host_t*,
332 private_ike_sa_t *this)
333 {
334 return this->other_host;
335 }
336
337 METHOD(ike_sa_t, set_other_host, void,
338 private_ike_sa_t *this, host_t *other)
339 {
340 DESTROY_IF(this->other_host);
341 this->other_host = other;
342 }
343
344 METHOD(ike_sa_t, get_peer_cfg, peer_cfg_t*,
345 private_ike_sa_t *this)
346 {
347 return this->peer_cfg;
348 }
349
350 METHOD(ike_sa_t, set_peer_cfg, void,
351 private_ike_sa_t *this, peer_cfg_t *peer_cfg)
352 {
353 peer_cfg->get_ref(peer_cfg);
354 DESTROY_IF(this->peer_cfg);
355 this->peer_cfg = peer_cfg;
356
357 if (this->ike_cfg == NULL)
358 {
359 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
360 this->ike_cfg->get_ref(this->ike_cfg);
361 }
362 }
363
364 METHOD(ike_sa_t, get_auth_cfg, auth_cfg_t*,
365 private_ike_sa_t *this, bool local)
366 {
367 if (local)
368 {
369 return this->my_auth;
370 }
371 return this->other_auth;
372 }
373
374 METHOD(ike_sa_t, add_auth_cfg, void,
375 private_ike_sa_t *this, bool local, auth_cfg_t *cfg)
376 {
377 if (local)
378 {
379 this->my_auths->insert_last(this->my_auths, cfg);
380 }
381 else
382 {
383 this->other_auths->insert_last(this->other_auths, cfg);
384 }
385 }
386
387 METHOD(ike_sa_t, create_auth_cfg_enumerator, enumerator_t*,
388 private_ike_sa_t *this, bool local)
389 {
390 if (local)
391 {
392 return this->my_auths->create_enumerator(this->my_auths);
393 }
394 return this->other_auths->create_enumerator(this->other_auths);
395 }
396
397 /**
398 * Flush the stored authentication round information
399 */
400 static void flush_auth_cfgs(private_ike_sa_t *this)
401 {
402 auth_cfg_t *cfg;
403
404 this->my_auth->purge(this->my_auth, FALSE);
405 this->other_auth->purge(this->other_auth, FALSE);
406
407 while (this->my_auths->remove_last(this->my_auths,
408 (void**)&cfg) == SUCCESS)
409 {
410 cfg->destroy(cfg);
411 }
412 while (this->other_auths->remove_last(this->other_auths,
413 (void**)&cfg) == SUCCESS)
414 {
415 cfg->destroy(cfg);
416 }
417 }
418
419 METHOD(ike_sa_t, get_proposal, proposal_t*,
420 private_ike_sa_t *this)
421 {
422 return this->proposal;
423 }
424
425 METHOD(ike_sa_t, set_proposal, void,
426 private_ike_sa_t *this, proposal_t *proposal)
427 {
428 DESTROY_IF(this->proposal);
429 this->proposal = proposal->clone(proposal);
430 }
431
432 METHOD(ike_sa_t, set_message_id, void,
433 private_ike_sa_t *this, bool initiate, u_int32_t mid)
434 {
435 if (initiate)
436 {
437 this->task_manager->reset(this->task_manager, mid, UINT_MAX);
438 }
439 else
440 {
441 this->task_manager->reset(this->task_manager, UINT_MAX, mid);
442 }
443 }
444
445 METHOD(ike_sa_t, send_keepalive, void,
446 private_ike_sa_t *this)
447 {
448 send_keepalive_job_t *job;
449 time_t last_out, now, diff;
450
451 if (!(this->conditions & COND_NAT_HERE) || this->keepalive_interval == 0)
452 { /* disable keep alives if we are not NATed anymore */
453 return;
454 }
455
456 last_out = get_use_time(this, FALSE);
457 now = time_monotonic(NULL);
458
459 diff = now - last_out;
460
461 if (diff >= this->keepalive_interval)
462 {
463 packet_t *packet;
464 chunk_t data;
465
466 packet = packet_create();
467 packet->set_source(packet, this->my_host->clone(this->my_host));
468 packet->set_destination(packet, this->other_host->clone(this->other_host));
469 data.ptr = malloc(1);
470 data.ptr[0] = 0xFF;
471 data.len = 1;
472 packet->set_data(packet, data);
473 DBG1(DBG_IKE, "sending keep alive");
474 charon->sender->send(charon->sender, packet);
475 diff = 0;
476 }
477 job = send_keepalive_job_create(this->ike_sa_id);
478 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
479 this->keepalive_interval - diff);
480 }
481
482 METHOD(ike_sa_t, get_ike_cfg, ike_cfg_t*,
483 private_ike_sa_t *this)
484 {
485 return this->ike_cfg;
486 }
487
488 METHOD(ike_sa_t, set_ike_cfg, void,
489 private_ike_sa_t *this, ike_cfg_t *ike_cfg)
490 {
491 ike_cfg->get_ref(ike_cfg);
492 this->ike_cfg = ike_cfg;
493 }
494
495 METHOD(ike_sa_t, enable_extension, void,
496 private_ike_sa_t *this, ike_extension_t extension)
497 {
498 this->extensions |= extension;
499 }
500
501 METHOD(ike_sa_t, supports_extension, bool,
502 private_ike_sa_t *this, ike_extension_t extension)
503 {
504 return (this->extensions & extension) != FALSE;
505 }
506
507 METHOD(ike_sa_t, has_condition, bool,
508 private_ike_sa_t *this, ike_condition_t condition)
509 {
510 return (this->conditions & condition) != FALSE;
511 }
512
513 METHOD(ike_sa_t, set_condition, void,
514 private_ike_sa_t *this, ike_condition_t condition, bool enable)
515 {
516 if (has_condition(this, condition) != enable)
517 {
518 if (enable)
519 {
520 this->conditions |= condition;
521 switch (condition)
522 {
523 case COND_NAT_HERE:
524 DBG1(DBG_IKE, "local host is behind NAT, sending keep alives");
525 this->conditions |= COND_NAT_ANY;
526 send_keepalive(this);
527 break;
528 case COND_NAT_THERE:
529 DBG1(DBG_IKE, "remote host is behind NAT");
530 this->conditions |= COND_NAT_ANY;
531 break;
532 case COND_NAT_FAKE:
533 DBG1(DBG_IKE, "faking NAT situation to enforce UDP encapsulation");
534 this->conditions |= COND_NAT_ANY;
535 break;
536 default:
537 break;
538 }
539 }
540 else
541 {
542 this->conditions &= ~condition;
543 switch (condition)
544 {
545 case COND_NAT_HERE:
546 case COND_NAT_FAKE:
547 case COND_NAT_THERE:
548 set_condition(this, COND_NAT_ANY,
549 has_condition(this, COND_NAT_HERE) ||
550 has_condition(this, COND_NAT_THERE) ||
551 has_condition(this, COND_NAT_FAKE));
552 break;
553 default:
554 break;
555 }
556 }
557 }
558 }
559
560 METHOD(ike_sa_t, send_dpd, status_t,
561 private_ike_sa_t *this)
562 {
563 job_t *job;
564 time_t diff, delay;
565
566 if (this->state == IKE_PASSIVE)
567 {
568 return INVALID_STATE;
569 }
570 delay = this->peer_cfg->get_dpd(this->peer_cfg);
571 if (this->task_manager->busy(this->task_manager))
572 {
573 /* an exchange is in the air, no need to start a DPD check */
574 diff = 0;
575 }
576 else
577 {
578 /* check if there was any inbound traffic */
579 time_t last_in, now;
580 last_in = get_use_time(this, TRUE);
581 now = time_monotonic(NULL);
582 diff = now - last_in;
583 if (!delay || diff >= delay)
584 {
585 /* to long ago, initiate dead peer detection */
586 DBG1(DBG_IKE, "sending DPD request");
587 this->task_manager->queue_dpd(this->task_manager);
588 diff = 0;
589 }
590 }
591 /* recheck in "interval" seconds */
592 if (delay)
593 {
594 job = (job_t*)send_dpd_job_create(this->ike_sa_id);
595 lib->scheduler->schedule_job(lib->scheduler, job, delay - diff);
596 }
597 return this->task_manager->initiate(this->task_manager);
598 }
599
600 METHOD(ike_sa_t, get_state, ike_sa_state_t,
601 private_ike_sa_t *this)
602 {
603 return this->state;
604 }
605
606 METHOD(ike_sa_t, set_state, void,
607 private_ike_sa_t *this, ike_sa_state_t state)
608 {
609 DBG2(DBG_IKE, "IKE_SA %s[%d] state change: %N => %N",
610 get_name(this), this->unique_id,
611 ike_sa_state_names, this->state,
612 ike_sa_state_names, state);
613
614 switch (state)
615 {
616 case IKE_ESTABLISHED:
617 {
618 if (this->state == IKE_CONNECTING ||
619 this->state == IKE_PASSIVE)
620 {
621 job_t *job;
622 u_int32_t t;
623
624 /* calculate rekey, reauth and lifetime */
625 this->stats[STAT_ESTABLISHED] = time_monotonic(NULL);
626
627 /* schedule rekeying if we have a time which is smaller than
628 * an already scheduled rekeying */
629 t = this->peer_cfg->get_rekey_time(this->peer_cfg, TRUE);
630 if (t && (this->stats[STAT_REKEY] == 0 ||
631 (this->stats[STAT_REKEY] > t + this->stats[STAT_ESTABLISHED])))
632 {
633 this->stats[STAT_REKEY] = t + this->stats[STAT_ESTABLISHED];
634 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, FALSE);
635 lib->scheduler->schedule_job(lib->scheduler, job, t);
636 DBG1(DBG_IKE, "scheduling rekeying in %ds", t);
637 }
638 t = this->peer_cfg->get_reauth_time(this->peer_cfg, TRUE);
639 if (t && (this->stats[STAT_REAUTH] == 0 ||
640 (this->stats[STAT_REAUTH] > t + this->stats[STAT_ESTABLISHED])))
641 {
642 this->stats[STAT_REAUTH] = t + this->stats[STAT_ESTABLISHED];
643 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE);
644 lib->scheduler->schedule_job(lib->scheduler, job, t);
645 DBG1(DBG_IKE, "scheduling reauthentication in %ds", t);
646 }
647 t = this->peer_cfg->get_over_time(this->peer_cfg);
648 if (this->stats[STAT_REKEY] || this->stats[STAT_REAUTH])
649 {
650 if (this->stats[STAT_REAUTH] == 0)
651 {
652 this->stats[STAT_DELETE] = this->stats[STAT_REKEY];
653 }
654 else if (this->stats[STAT_REKEY] == 0)
655 {
656 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH];
657 }
658 else
659 {
660 this->stats[STAT_DELETE] = min(this->stats[STAT_REKEY],
661 this->stats[STAT_REAUTH]);
662 }
663 this->stats[STAT_DELETE] += t;
664 t = this->stats[STAT_DELETE] - this->stats[STAT_ESTABLISHED];
665 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
666 lib->scheduler->schedule_job(lib->scheduler, job, t);
667 DBG1(DBG_IKE, "maximum IKE_SA lifetime %ds", t);
668 }
669
670 /* start DPD checks */
671 if (this->peer_cfg->get_dpd(this->peer_cfg))
672 {
673 if (supports_extension(this, EXT_DPD))
674 {
675 send_dpd(this);
676 }
677 else
678 {
679 DBG1(DBG_IKE, "DPD not supported by peer, disabled");
680 }
681 }
682 }
683 break;
684 }
685 case IKE_DELETING:
686 {
687 /* delete may fail if a packet gets lost, so set a timeout */
688 job_t *job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
689 lib->scheduler->schedule_job(lib->scheduler, job,
690 HALF_OPEN_IKE_SA_TIMEOUT);
691 break;
692 }
693 default:
694 break;
695 }
696 charon->bus->ike_state_change(charon->bus, &this->public, state);
697 this->state = state;
698 }
699
700 METHOD(ike_sa_t, reset, void,
701 private_ike_sa_t *this)
702 {
703 /* the responder ID is reset, as peer may choose another one */
704 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
705 {
706 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
707 }
708
709 set_state(this, IKE_CREATED);
710
711 flush_auth_cfgs(this);
712
713 this->keymat->destroy(this->keymat);
714 this->keymat = keymat_create(this->version,
715 this->ike_sa_id->is_initiator(this->ike_sa_id));
716
717 this->task_manager->reset(this->task_manager, 0, 0);
718 }
719
720 METHOD(ike_sa_t, get_keymat, keymat_t*,
721 private_ike_sa_t *this)
722 {
723 return this->keymat;
724 }
725
726 METHOD(ike_sa_t, set_virtual_ip, void,
727 private_ike_sa_t *this, bool local, host_t *ip)
728 {
729 if (local)
730 {
731 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
732 if (hydra->kernel_interface->add_ip(hydra->kernel_interface, ip,
733 this->my_host) == SUCCESS)
734 {
735 if (this->my_virtual_ip)
736 {
737 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
738 hydra->kernel_interface->del_ip(hydra->kernel_interface,
739 this->my_virtual_ip);
740 }
741 DESTROY_IF(this->my_virtual_ip);
742 this->my_virtual_ip = ip->clone(ip);
743 }
744 else
745 {
746 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
747 this->my_virtual_ip = NULL;
748 }
749 }
750 else
751 {
752 DESTROY_IF(this->other_virtual_ip);
753 this->other_virtual_ip = ip->clone(ip);
754 }
755 }
756
757 METHOD(ike_sa_t, get_virtual_ip, host_t*,
758 private_ike_sa_t *this, bool local)
759 {
760 if (local)
761 {
762 return this->my_virtual_ip;
763 }
764 else
765 {
766 return this->other_virtual_ip;
767 }
768 }
769
770 METHOD(ike_sa_t, add_additional_address, void,
771 private_ike_sa_t *this, host_t *host)
772 {
773 this->additional_addresses->insert_last(this->additional_addresses, host);
774 }
775
776 METHOD(ike_sa_t, create_additional_address_enumerator, enumerator_t*,
777 private_ike_sa_t *this)
778 {
779 return this->additional_addresses->create_enumerator(
780 this->additional_addresses);
781 }
782
783 METHOD(ike_sa_t, remove_additional_addresses, void,
784 private_ike_sa_t *this)
785 {
786 enumerator_t *enumerator = create_additional_address_enumerator(this);
787 host_t *host;
788 while (enumerator->enumerate(enumerator, (void**)&host))
789 {
790 this->additional_addresses->remove_at(this->additional_addresses,
791 enumerator);
792 host->destroy(host);
793 }
794 enumerator->destroy(enumerator);
795 }
796
797 METHOD(ike_sa_t, has_mapping_changed, bool,
798 private_ike_sa_t *this, chunk_t hash)
799 {
800 if (this->nat_detection_dest.ptr == NULL)
801 {
802 this->nat_detection_dest = chunk_clone(hash);
803 return FALSE;
804 }
805 if (chunk_equals(hash, this->nat_detection_dest))
806 {
807 return FALSE;
808 }
809 free(this->nat_detection_dest.ptr);
810 this->nat_detection_dest = chunk_clone(hash);
811 return TRUE;
812 }
813
814 METHOD(ike_sa_t, set_pending_updates, void,
815 private_ike_sa_t *this, u_int32_t updates)
816 {
817 this->pending_updates = updates;
818 }
819
820 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
821 private_ike_sa_t *this)
822 {
823 return this->pending_updates;
824 }
825
826 METHOD(ike_sa_t, float_ports, void,
827 private_ike_sa_t *this)
828 {
829 /* do not switch if we have a custom port from MOBIKE/NAT */
830 if (this->my_host->get_port(this->my_host) == IKEV2_UDP_PORT)
831 {
832 this->my_host->set_port(this->my_host, IKEV2_NATT_PORT);
833 }
834 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
835 {
836 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
837 }
838 }
839
840 METHOD(ike_sa_t, update_hosts, void,
841 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
842 {
843 bool update = FALSE;
844
845 if (me == NULL)
846 {
847 me = this->my_host;
848 }
849 if (other == NULL)
850 {
851 other = this->other_host;
852 }
853
854 /* apply hosts on first received message */
855 if (this->my_host->is_anyaddr(this->my_host) ||
856 this->other_host->is_anyaddr(this->other_host))
857 {
858 set_my_host(this, me->clone(me));
859 set_other_host(this, other->clone(other));
860 update = TRUE;
861 }
862 else
863 {
864 /* update our address in any case */
865 if (!me->equals(me, this->my_host))
866 {
867 set_my_host(this, me->clone(me));
868 update = TRUE;
869 }
870
871 if (!other->equals(other, this->other_host))
872 {
873 /* update others address if we are NOT NATed */
874 if (force || !has_condition(this, COND_NAT_HERE))
875 {
876 set_other_host(this, other->clone(other));
877 update = TRUE;
878 }
879 }
880 }
881
882 /* update all associated CHILD_SAs, if required */
883 if (update)
884 {
885 enumerator_t *enumerator;
886 child_sa_t *child_sa;
887
888 enumerator = this->child_sas->create_enumerator(this->child_sas);
889 while (enumerator->enumerate(enumerator, (void**)&child_sa))
890 {
891 if (child_sa->update(child_sa, this->my_host,
892 this->other_host, this->my_virtual_ip,
893 has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
894 {
895 this->public.rekey_child_sa(&this->public,
896 child_sa->get_protocol(child_sa),
897 child_sa->get_spi(child_sa, TRUE));
898 }
899 }
900 enumerator->destroy(enumerator);
901 }
902 }
903
904 METHOD(ike_sa_t, generate_message, status_t,
905 private_ike_sa_t *this, message_t *message, packet_t **packet)
906 {
907 if (message->is_encoded(message))
908 { /* already done */
909 *packet = message->get_packet(message);
910 return SUCCESS;
911 }
912 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
913 message->set_ike_sa_id(message, this->ike_sa_id);
914 charon->bus->message(charon->bus, message, FALSE);
915 return message->generate(message, this->keymat, packet);
916 }
917
918 METHOD(ike_sa_t, set_kmaddress, void,
919 private_ike_sa_t *this, host_t *local, host_t *remote)
920 {
921 DESTROY_IF(this->local_host);
922 DESTROY_IF(this->remote_host);
923 this->local_host = local->clone(local);
924 this->remote_host = remote->clone(remote);
925 }
926
927 #ifdef ME
928 METHOD(ike_sa_t, act_as_mediation_server, void,
929 private_ike_sa_t *this)
930 {
931 charon->mediation_manager->update_sa_id(charon->mediation_manager,
932 this->other_id, this->ike_sa_id);
933 this->is_mediation_server = TRUE;
934 }
935
936 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
937 private_ike_sa_t *this)
938 {
939 return this->server_reflexive_host;
940 }
941
942 METHOD(ike_sa_t, set_server_reflexive_host, void,
943 private_ike_sa_t *this, host_t *host)
944 {
945 DESTROY_IF(this->server_reflexive_host);
946 this->server_reflexive_host = host;
947 }
948
949 METHOD(ike_sa_t, get_connect_id, chunk_t,
950 private_ike_sa_t *this)
951 {
952 return this->connect_id;
953 }
954
955 METHOD(ike_sa_t, respond, status_t,
956 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
957 {
958 ike_me_t *task = ike_me_create(&this->public, TRUE);
959 task->respond(task, peer_id, connect_id);
960 this->task_manager->queue_task(this->task_manager, (task_t*)task);
961 return this->task_manager->initiate(this->task_manager);
962 }
963
964 METHOD(ike_sa_t, callback, status_t,
965 private_ike_sa_t *this, identification_t *peer_id)
966 {
967 ike_me_t *task = ike_me_create(&this->public, TRUE);
968 task->callback(task, peer_id);
969 this->task_manager->queue_task(this->task_manager, (task_t*)task);
970 return this->task_manager->initiate(this->task_manager);
971 }
972
973 METHOD(ike_sa_t, relay, status_t,
974 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
975 chunk_t connect_key, linked_list_t *endpoints, bool response)
976 {
977 ike_me_t *task = ike_me_create(&this->public, TRUE);
978 task->relay(task, requester, connect_id, connect_key, endpoints, response);
979 this->task_manager->queue_task(this->task_manager, (task_t*)task);
980 return this->task_manager->initiate(this->task_manager);
981 }
982
983 METHOD(ike_sa_t, initiate_mediation, status_t,
984 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
985 {
986 ike_me_t *task = ike_me_create(&this->public, TRUE);
987 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
988 this->task_manager->queue_task(this->task_manager, (task_t*)task);
989 return this->task_manager->initiate(this->task_manager);
990 }
991
992 METHOD(ike_sa_t, initiate_mediated, status_t,
993 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
994 {
995 set_my_host(this, me->clone(me));
996 set_other_host(this, other->clone(other));
997 chunk_free(&this->connect_id);
998 this->connect_id = chunk_clone(connect_id);
999 return this->task_manager->initiate(this->task_manager);
1000 }
1001 #endif /* ME */
1002
1003 /**
1004 * Resolve DNS host in configuration
1005 */
1006 static void resolve_hosts(private_ike_sa_t *this)
1007 {
1008 host_t *host;
1009
1010 if (this->remote_host)
1011 {
1012 host = this->remote_host->clone(this->remote_host);
1013 host->set_port(host, IKEV2_UDP_PORT);
1014 }
1015 else
1016 {
1017 host = host_create_from_dns(this->ike_cfg->get_other_addr(this->ike_cfg),
1018 0, this->ike_cfg->get_other_port(this->ike_cfg));
1019 }
1020 if (host)
1021 {
1022 set_other_host(this, host);
1023 }
1024
1025 if (this->local_host)
1026 {
1027 host = this->local_host->clone(this->local_host);
1028 host->set_port(host, IKEV2_UDP_PORT);
1029 }
1030 else
1031 {
1032 int family = 0;
1033
1034 /* use same address family as for other */
1035 if (!this->other_host->is_anyaddr(this->other_host))
1036 {
1037 family = this->other_host->get_family(this->other_host);
1038 }
1039 host = host_create_from_dns(this->ike_cfg->get_my_addr(this->ike_cfg),
1040 family, this->ike_cfg->get_my_port(this->ike_cfg));
1041
1042 if (host && host->is_anyaddr(host) &&
1043 !this->other_host->is_anyaddr(this->other_host))
1044 {
1045 host->destroy(host);
1046 host = hydra->kernel_interface->get_source_addr(
1047 hydra->kernel_interface, this->other_host, NULL);
1048 if (host)
1049 {
1050 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1051 }
1052 else
1053 { /* fallback to address family specific %any(6), if configured */
1054 host = host_create_from_dns(
1055 this->ike_cfg->get_my_addr(this->ike_cfg),
1056 0, this->ike_cfg->get_my_port(this->ike_cfg));
1057 }
1058 }
1059 }
1060 if (host)
1061 {
1062 set_my_host(this, host);
1063 }
1064 }
1065
1066 METHOD(ike_sa_t, initiate, status_t,
1067 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1068 traffic_selector_t *tsi, traffic_selector_t *tsr)
1069 {
1070 if (this->state == IKE_CREATED)
1071 {
1072 resolve_hosts(this);
1073
1074 if (this->other_host->is_anyaddr(this->other_host)
1075 #ifdef ME
1076 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1077 #endif /* ME */
1078 )
1079 {
1080 DESTROY_IF(child_cfg);
1081 DBG1(DBG_IKE, "unable to initiate to %%any");
1082 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1083 return DESTROY_ME;
1084 }
1085
1086 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1087 this->task_manager->queue_ike(this->task_manager);
1088 }
1089
1090 #ifdef ME
1091 if (this->peer_cfg->is_mediation(this->peer_cfg))
1092 {
1093 if (this->state == IKE_ESTABLISHED)
1094 {
1095 /* mediation connection is already established, retrigger state
1096 * change to notify bus listeners */
1097 DBG1(DBG_IKE, "mediation connection is already up");
1098 set_state(this, IKE_ESTABLISHED);
1099 }
1100 DESTROY_IF(child_cfg);
1101 }
1102 else
1103 #endif /* ME */
1104 if (child_cfg)
1105 {
1106 /* normal IKE_SA with CHILD_SA */
1107 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1108 tsi, tsr);
1109 #ifdef ME
1110 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1111 {
1112 /* mediated connection, initiate mediation process */
1113 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1114 lib->processor->queue_job(lib->processor, job);
1115 return SUCCESS;
1116 }
1117 #endif /* ME */
1118 }
1119
1120 return this->task_manager->initiate(this->task_manager);
1121 }
1122
1123 METHOD(ike_sa_t, process_message, status_t,
1124 private_ike_sa_t *this, message_t *message)
1125 {
1126 status_t status;
1127
1128 if (this->state == IKE_PASSIVE)
1129 { /* do not handle messages in passive state */
1130 return FAILED;
1131 }
1132 if (message->get_major_version(message) != this->version)
1133 {
1134 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1135 exchange_type_names, message->get_exchange_type(message),
1136 message->get_major_version(message),
1137 ike_version_names, this->version);
1138 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1139 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1140 return FAILED;
1141 }
1142 status = this->task_manager->process_message(this->task_manager, message);
1143 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1144 {
1145 /* authentication completed */
1146 this->flush_auth_cfg = FALSE;
1147 flush_auth_cfgs(this);
1148 }
1149 return status;
1150 }
1151
1152 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1153 private_ike_sa_t *this)
1154 {
1155 return this->ike_sa_id;
1156 }
1157
1158 METHOD(ike_sa_t, get_version, ike_version_t,
1159 private_ike_sa_t *this)
1160 {
1161 return this->version;
1162 }
1163
1164 METHOD(ike_sa_t, get_my_id, identification_t*,
1165 private_ike_sa_t *this)
1166 {
1167 return this->my_id;
1168 }
1169
1170 METHOD(ike_sa_t, set_my_id, void,
1171 private_ike_sa_t *this, identification_t *me)
1172 {
1173 DESTROY_IF(this->my_id);
1174 this->my_id = me;
1175 }
1176
1177 METHOD(ike_sa_t, get_other_id, identification_t*,
1178 private_ike_sa_t *this)
1179 {
1180 return this->other_id;
1181 }
1182
1183 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1184 private_ike_sa_t *this)
1185 {
1186 identification_t *id = NULL, *current;
1187 enumerator_t *enumerator;
1188 auth_cfg_t *cfg;
1189
1190 enumerator = this->other_auths->create_enumerator(this->other_auths);
1191 while (enumerator->enumerate(enumerator, &cfg))
1192 {
1193 /* prefer EAP-Identity of last round */
1194 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1195 if (!current || current->get_type(current) == ID_ANY)
1196 {
1197 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1198 }
1199 if (!current || current->get_type(current) == ID_ANY)
1200 {
1201 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1202 }
1203 if (current && current->get_type(current) != ID_ANY)
1204 {
1205 id = current;
1206 continue;
1207 }
1208 }
1209 enumerator->destroy(enumerator);
1210 if (id)
1211 {
1212 return id;
1213 }
1214 return this->other_id;
1215 }
1216
1217 METHOD(ike_sa_t, set_other_id, void,
1218 private_ike_sa_t *this, identification_t *other)
1219 {
1220 DESTROY_IF(this->other_id);
1221 this->other_id = other;
1222 }
1223
1224 METHOD(ike_sa_t, add_child_sa, void,
1225 private_ike_sa_t *this, child_sa_t *child_sa)
1226 {
1227 this->child_sas->insert_last(this->child_sas, child_sa);
1228 }
1229
1230 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1231 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1232 {
1233 enumerator_t *enumerator;
1234 child_sa_t *current, *found = NULL;
1235
1236 enumerator = this->child_sas->create_enumerator(this->child_sas);
1237 while (enumerator->enumerate(enumerator, (void**)&current))
1238 {
1239 if (current->get_spi(current, inbound) == spi &&
1240 current->get_protocol(current) == protocol)
1241 {
1242 found = current;
1243 }
1244 }
1245 enumerator->destroy(enumerator);
1246 return found;
1247 }
1248
1249 METHOD(ike_sa_t, get_child_count, int,
1250 private_ike_sa_t *this)
1251 {
1252 return this->child_sas->get_count(this->child_sas);
1253 }
1254
1255 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1256 private_ike_sa_t *this)
1257 {
1258 return this->child_sas->create_enumerator(this->child_sas);
1259 }
1260
1261 METHOD(ike_sa_t, remove_child_sa, void,
1262 private_ike_sa_t *this, enumerator_t *enumerator)
1263 {
1264 this->child_sas->remove_at(this->child_sas, enumerator);
1265 }
1266
1267 METHOD(ike_sa_t, rekey_child_sa, status_t,
1268 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1269 {
1270 if (this->state == IKE_PASSIVE)
1271 {
1272 return INVALID_STATE;
1273 }
1274 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1275 return this->task_manager->initiate(this->task_manager);
1276 }
1277
1278 METHOD(ike_sa_t, delete_child_sa, status_t,
1279 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool expired)
1280 {
1281 if (this->state == IKE_PASSIVE)
1282 {
1283 return INVALID_STATE;
1284 }
1285 this->task_manager->queue_child_delete(this->task_manager,
1286 protocol, spi, expired);
1287 return this->task_manager->initiate(this->task_manager);
1288 }
1289
1290 METHOD(ike_sa_t, destroy_child_sa, status_t,
1291 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1292 {
1293 enumerator_t *enumerator;
1294 child_sa_t *child_sa;
1295 status_t status = NOT_FOUND;
1296
1297 enumerator = this->child_sas->create_enumerator(this->child_sas);
1298 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1299 {
1300 if (child_sa->get_protocol(child_sa) == protocol &&
1301 child_sa->get_spi(child_sa, TRUE) == spi)
1302 {
1303 this->child_sas->remove_at(this->child_sas, enumerator);
1304 child_sa->destroy(child_sa);
1305 status = SUCCESS;
1306 break;
1307 }
1308 }
1309 enumerator->destroy(enumerator);
1310 return status;
1311 }
1312
1313 METHOD(ike_sa_t, delete_, status_t,
1314 private_ike_sa_t *this)
1315 {
1316 switch (this->state)
1317 {
1318 case IKE_REKEYING:
1319 if (this->version == IKEV1)
1320 { /* SA has been reauthenticated, delete */
1321 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1322 break;
1323 }
1324 /* FALL */
1325 case IKE_ESTABLISHED:
1326 this->task_manager->queue_ike_delete(this->task_manager);
1327 return this->task_manager->initiate(this->task_manager);
1328 case IKE_CREATED:
1329 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1330 break;
1331 case IKE_PASSIVE:
1332 break;
1333 default:
1334 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1335 "without notification", ike_sa_state_names, this->state);
1336 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1337 break;
1338 }
1339 return DESTROY_ME;
1340 }
1341
1342 METHOD(ike_sa_t, rekey, status_t,
1343 private_ike_sa_t *this)
1344 {
1345 if (this->state == IKE_PASSIVE)
1346 {
1347 return INVALID_STATE;
1348 }
1349 this->task_manager->queue_ike_rekey(this->task_manager);
1350 return this->task_manager->initiate(this->task_manager);
1351 }
1352
1353 METHOD(ike_sa_t, reauth, status_t,
1354 private_ike_sa_t *this)
1355 {
1356 if (this->state == IKE_PASSIVE)
1357 {
1358 return INVALID_STATE;
1359 }
1360 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1361 * If the peer does not support RFC4478, there is no way to keep the
1362 * IKE_SA up. */
1363 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1364 {
1365 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1366 if (this->other_virtual_ip != NULL ||
1367 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1368 has_condition(this, COND_EAP_AUTHENTICATED)
1369 #ifdef ME
1370 /* as mediation server we too cannot reauth the IKE_SA */
1371 || this->is_mediation_server
1372 #endif /* ME */
1373 )
1374 {
1375 time_t now = time_monotonic(NULL);
1376
1377 DBG1(DBG_IKE, "IKE_SA will timeout in %V",
1378 &now, &this->stats[STAT_DELETE]);
1379 return FAILED;
1380 }
1381 else
1382 {
1383 DBG1(DBG_IKE, "reauthenticating actively");
1384 }
1385 }
1386 this->task_manager->queue_ike_reauth(this->task_manager);
1387 return this->task_manager->initiate(this->task_manager);
1388 }
1389
1390 METHOD(ike_sa_t, reestablish, status_t,
1391 private_ike_sa_t *this)
1392 {
1393 ike_sa_t *new;
1394 host_t *host;
1395 action_t action;
1396 enumerator_t *enumerator;
1397 child_sa_t *child_sa;
1398 child_cfg_t *child_cfg;
1399 bool restart = FALSE;
1400 status_t status = FAILED;
1401
1402 /* check if we have children to keep up at all */
1403 enumerator = this->child_sas->create_enumerator(this->child_sas);
1404 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1405 {
1406 if (this->state == IKE_DELETING)
1407 {
1408 action = child_sa->get_close_action(child_sa);
1409 }
1410 else
1411 {
1412 action = child_sa->get_dpd_action(child_sa);
1413 }
1414 switch (action)
1415 {
1416 case ACTION_RESTART:
1417 restart = TRUE;
1418 break;
1419 case ACTION_ROUTE:
1420 charon->traps->install(charon->traps, this->peer_cfg,
1421 child_sa->get_config(child_sa));
1422 break;
1423 default:
1424 break;
1425 }
1426 }
1427 enumerator->destroy(enumerator);
1428 #ifdef ME
1429 /* mediation connections have no children, keep them up anyway */
1430 if (this->peer_cfg->is_mediation(this->peer_cfg))
1431 {
1432 restart = TRUE;
1433 }
1434 #endif /* ME */
1435 if (!restart)
1436 {
1437 return FAILED;
1438 }
1439
1440 /* check if we are able to reestablish this IKE_SA */
1441 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1442 (this->other_virtual_ip != NULL ||
1443 has_condition(this, COND_EAP_AUTHENTICATED)
1444 #ifdef ME
1445 || this->is_mediation_server
1446 #endif /* ME */
1447 ))
1448 {
1449 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1450 return FAILED;
1451 }
1452
1453 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1454 this->version, TRUE);
1455 if (!new)
1456 {
1457 return FAILED;
1458 }
1459 new->set_peer_cfg(new, this->peer_cfg);
1460 host = this->other_host;
1461 new->set_other_host(new, host->clone(host));
1462 host = this->my_host;
1463 new->set_my_host(new, host->clone(host));
1464 /* if we already have a virtual IP, we reuse it */
1465 host = this->my_virtual_ip;
1466 if (host)
1467 {
1468 new->set_virtual_ip(new, TRUE, host);
1469 }
1470
1471 #ifdef ME
1472 if (this->peer_cfg->is_mediation(this->peer_cfg))
1473 {
1474 status = new->initiate(new, NULL, 0, NULL, NULL);
1475 }
1476 else
1477 #endif /* ME */
1478 {
1479 enumerator = this->child_sas->create_enumerator(this->child_sas);
1480 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1481 {
1482 if (this->state == IKE_DELETING)
1483 {
1484 action = child_sa->get_close_action(child_sa);
1485 }
1486 else
1487 {
1488 action = child_sa->get_dpd_action(child_sa);
1489 }
1490 switch (action)
1491 {
1492 case ACTION_RESTART:
1493 child_cfg = child_sa->get_config(child_sa);
1494 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1495 child_cfg->get_name(child_cfg));
1496 child_cfg->get_ref(child_cfg);
1497 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1498 break;
1499 default:
1500 continue;
1501 }
1502 if (status == DESTROY_ME)
1503 {
1504 break;
1505 }
1506 }
1507 enumerator->destroy(enumerator);
1508 }
1509
1510 if (status == DESTROY_ME)
1511 {
1512 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1513 status = FAILED;
1514 }
1515 else
1516 {
1517 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1518 status = SUCCESS;
1519 }
1520 charon->bus->set_sa(charon->bus, &this->public);
1521 return status;
1522 }
1523
1524 METHOD(ike_sa_t, retransmit, status_t,
1525 private_ike_sa_t *this, u_int32_t message_id)
1526 {
1527 if (this->state == IKE_PASSIVE)
1528 {
1529 return INVALID_STATE;
1530 }
1531 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1532 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1533 {
1534 /* send a proper signal to brief interested bus listeners */
1535 switch (this->state)
1536 {
1537 case IKE_CONNECTING:
1538 {
1539 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1540 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1541 this->keyingtry++;
1542 if (tries == 0 || tries > this->keyingtry)
1543 {
1544 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1545 this->keyingtry + 1, tries);
1546 reset(this);
1547 this->task_manager->queue_ike(this->task_manager);
1548 return this->task_manager->initiate(this->task_manager);
1549 }
1550 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1551 break;
1552 }
1553 case IKE_DELETING:
1554 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1555 break;
1556 case IKE_REKEYING:
1557 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1558 /* FALL */
1559 default:
1560 reestablish(this);
1561 break;
1562 }
1563 return DESTROY_ME;
1564 }
1565 return SUCCESS;
1566 }
1567
1568 METHOD(ike_sa_t, set_auth_lifetime, void,
1569 private_ike_sa_t *this, u_int32_t lifetime)
1570 {
1571 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
1572 u_int32_t reauth_time = time_monotonic(NULL) + lifetime - reduction;
1573
1574 if (lifetime < reduction)
1575 {
1576 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
1577 lifetime);
1578 lib->processor->queue_job(lib->processor,
1579 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1580 }
1581 else if (this->stats[STAT_REAUTH] == 0 ||
1582 this->stats[STAT_REAUTH] > reauth_time)
1583 {
1584 this->stats[STAT_REAUTH] = reauth_time;
1585 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1586 " in %ds", lifetime, lifetime - reduction);
1587 lib->scheduler->schedule_job(lib->scheduler,
1588 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1589 lifetime - reduction);
1590 }
1591 else
1592 {
1593 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1594 "reauthentication already scheduled in %ds", lifetime,
1595 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1596 }
1597 }
1598
1599 /**
1600 * Check if the current combination of source and destination address is still
1601 * valid.
1602 */
1603 static bool is_current_path_valid(private_ike_sa_t *this)
1604 {
1605 bool valid = FALSE;
1606 host_t *src;
1607 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1608 this->other_host, this->my_host);
1609 if (src)
1610 {
1611 if (src->ip_equals(src, this->my_host))
1612 {
1613 valid = TRUE;
1614 }
1615 src->destroy(src);
1616 }
1617 return valid;
1618 }
1619
1620 /**
1621 * Check if we have any path avialable for this IKE SA.
1622 */
1623 static bool is_any_path_valid(private_ike_sa_t *this)
1624 {
1625 bool valid = FALSE;
1626 enumerator_t *enumerator;
1627 host_t *src, *addr;
1628 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1629 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1630 this->other_host, NULL);
1631 if (!src)
1632 {
1633 enumerator = this->additional_addresses->create_enumerator(
1634 this->additional_addresses);
1635 while (enumerator->enumerate(enumerator, &addr))
1636 {
1637 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1638 src = hydra->kernel_interface->get_source_addr(
1639 hydra->kernel_interface, addr, NULL);
1640 if (src)
1641 {
1642 break;
1643 }
1644 }
1645 enumerator->destroy(enumerator);
1646 }
1647 if (src)
1648 {
1649 valid = TRUE;
1650 src->destroy(src);
1651 }
1652 return valid;
1653 }
1654
1655 METHOD(ike_sa_t, roam, status_t,
1656 private_ike_sa_t *this, bool address)
1657 {
1658 switch (this->state)
1659 {
1660 case IKE_CREATED:
1661 case IKE_DELETING:
1662 case IKE_DESTROYING:
1663 case IKE_PASSIVE:
1664 return SUCCESS;
1665 default:
1666 break;
1667 }
1668
1669 /* keep existing path if possible */
1670 if (is_current_path_valid(this))
1671 {
1672 DBG2(DBG_IKE, "keeping connection path %H - %H",
1673 this->my_host, this->other_host);
1674 set_condition(this, COND_STALE, FALSE);
1675
1676 if (supports_extension(this, EXT_MOBIKE) && address)
1677 { /* if any addresses changed, send an updated list */
1678 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1679 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
1680 return this->task_manager->initiate(this->task_manager);
1681 }
1682 return SUCCESS;
1683 }
1684
1685 if (!is_any_path_valid(this))
1686 {
1687 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1688 this->other_host);
1689 set_condition(this, COND_STALE, TRUE);
1690 return SUCCESS;
1691 }
1692 set_condition(this, COND_STALE, FALSE);
1693
1694 /* update addresses with mobike, if supported ... */
1695 if (supports_extension(this, EXT_MOBIKE))
1696 {
1697 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1698 { /* responder updates the peer about changed address config */
1699 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1700 "implicitly requesting an address change");
1701 address = TRUE;
1702 }
1703 else
1704 {
1705 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1706 }
1707 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
1708 return this->task_manager->initiate(this->task_manager);
1709 }
1710
1711 /* ... reauth if not */
1712 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1713 { /* responder does not reauthenticate */
1714 set_condition(this, COND_STALE, TRUE);
1715 return SUCCESS;
1716 }
1717 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1718 return reauth(this);
1719 }
1720
1721 METHOD(ike_sa_t, add_configuration_attribute, void,
1722 private_ike_sa_t *this, attribute_handler_t *handler,
1723 configuration_attribute_type_t type, chunk_t data)
1724 {
1725 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1726
1727 entry->handler = handler;
1728 entry->type = type;
1729 entry->data = chunk_clone(data);
1730
1731 this->attributes->insert_last(this->attributes, entry);
1732 }
1733
1734 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1735 private_ike_sa_t *this, task_queue_t queue)
1736 {
1737 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1738 }
1739
1740 METHOD(ike_sa_t, queue_task, void,
1741 private_ike_sa_t *this, task_t *task)
1742 {
1743 this->task_manager->queue_task(this->task_manager, task);
1744 }
1745
1746 METHOD(ike_sa_t, inherit, void,
1747 private_ike_sa_t *this, ike_sa_t *other_public)
1748 {
1749 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1750 child_sa_t *child_sa;
1751 attribute_entry_t *entry;
1752 enumerator_t *enumerator;
1753 auth_cfg_t *cfg;
1754
1755 /* apply hosts and ids */
1756 this->my_host->destroy(this->my_host);
1757 this->other_host->destroy(this->other_host);
1758 this->my_id->destroy(this->my_id);
1759 this->other_id->destroy(this->other_id);
1760 this->my_host = other->my_host->clone(other->my_host);
1761 this->other_host = other->other_host->clone(other->other_host);
1762 this->my_id = other->my_id->clone(other->my_id);
1763 this->other_id = other->other_id->clone(other->other_id);
1764
1765 /* apply virtual assigned IPs... */
1766 if (other->my_virtual_ip)
1767 {
1768 this->my_virtual_ip = other->my_virtual_ip;
1769 other->my_virtual_ip = NULL;
1770 }
1771 if (other->other_virtual_ip)
1772 {
1773 this->other_virtual_ip = other->other_virtual_ip;
1774 other->other_virtual_ip = NULL;
1775 }
1776
1777 /* authentication information */
1778 enumerator = other->my_auths->create_enumerator(other->my_auths);
1779 while (enumerator->enumerate(enumerator, &cfg))
1780 {
1781 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1782 }
1783 enumerator->destroy(enumerator);
1784 enumerator = other->other_auths->create_enumerator(other->other_auths);
1785 while (enumerator->enumerate(enumerator, &cfg))
1786 {
1787 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1788 }
1789 enumerator->destroy(enumerator);
1790
1791 /* ... and configuration attributes */
1792 while (other->attributes->remove_last(other->attributes,
1793 (void**)&entry) == SUCCESS)
1794 {
1795 this->attributes->insert_first(this->attributes, entry);
1796 }
1797
1798 /* inherit all conditions */
1799 this->conditions = other->conditions;
1800 if (this->conditions & COND_NAT_HERE)
1801 {
1802 send_keepalive(this);
1803 }
1804
1805 #ifdef ME
1806 if (other->is_mediation_server)
1807 {
1808 act_as_mediation_server(this);
1809 }
1810 else if (other->server_reflexive_host)
1811 {
1812 this->server_reflexive_host = other->server_reflexive_host->clone(
1813 other->server_reflexive_host);
1814 }
1815 #endif /* ME */
1816
1817 /* adopt all children */
1818 while (other->child_sas->remove_last(other->child_sas,
1819 (void**)&child_sa) == SUCCESS)
1820 {
1821 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1822 }
1823
1824 /* move pending tasks to the new IKE_SA */
1825 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1826
1827 /* reauthentication timeout survives a rekeying */
1828 if (other->stats[STAT_REAUTH])
1829 {
1830 time_t reauth, delete, now = time_monotonic(NULL);
1831
1832 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
1833 reauth = this->stats[STAT_REAUTH] - now;
1834 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
1835 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
1836 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
1837 "lifetime reduced to %ds", reauth, delete);
1838 lib->scheduler->schedule_job(lib->scheduler,
1839 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
1840 lib->scheduler->schedule_job(lib->scheduler,
1841 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
1842 }
1843 }
1844
1845 METHOD(ike_sa_t, destroy, void,
1846 private_ike_sa_t *this)
1847 {
1848 attribute_entry_t *entry;
1849
1850 charon->bus->set_sa(charon->bus, &this->public);
1851
1852 set_state(this, IKE_DESTROYING);
1853 DESTROY_IF(this->task_manager);
1854
1855 /* remove attributes first, as we pass the IKE_SA to the handler */
1856 while (this->attributes->remove_last(this->attributes,
1857 (void**)&entry) == SUCCESS)
1858 {
1859 hydra->attributes->release(hydra->attributes, entry->handler,
1860 this->other_id, entry->type, entry->data);
1861 free(entry->data.ptr);
1862 free(entry);
1863 }
1864 this->attributes->destroy(this->attributes);
1865
1866 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1867
1868 /* unset SA after here to avoid usage by the listeners */
1869 charon->bus->set_sa(charon->bus, NULL);
1870
1871 DESTROY_IF(this->keymat);
1872
1873 if (this->my_virtual_ip)
1874 {
1875 hydra->kernel_interface->del_ip(hydra->kernel_interface,
1876 this->my_virtual_ip);
1877 this->my_virtual_ip->destroy(this->my_virtual_ip);
1878 }
1879 if (this->other_virtual_ip)
1880 {
1881 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
1882 {
1883 hydra->attributes->release_address(hydra->attributes,
1884 this->peer_cfg->get_pool(this->peer_cfg),
1885 this->other_virtual_ip, get_other_eap_id(this));
1886 }
1887 this->other_virtual_ip->destroy(this->other_virtual_ip);
1888 }
1889 this->additional_addresses->destroy_offset(this->additional_addresses,
1890 offsetof(host_t, destroy));
1891 #ifdef ME
1892 if (this->is_mediation_server)
1893 {
1894 charon->mediation_manager->remove(charon->mediation_manager,
1895 this->ike_sa_id);
1896 }
1897 DESTROY_IF(this->server_reflexive_host);
1898 chunk_free(&this->connect_id);
1899 #endif /* ME */
1900 free(this->nat_detection_dest.ptr);
1901
1902 DESTROY_IF(this->my_host);
1903 DESTROY_IF(this->other_host);
1904 DESTROY_IF(this->my_id);
1905 DESTROY_IF(this->other_id);
1906 DESTROY_IF(this->local_host);
1907 DESTROY_IF(this->remote_host);
1908
1909 DESTROY_IF(this->ike_cfg);
1910 DESTROY_IF(this->peer_cfg);
1911 DESTROY_IF(this->proposal);
1912 this->my_auth->destroy(this->my_auth);
1913 this->other_auth->destroy(this->other_auth);
1914 this->my_auths->destroy_offset(this->my_auths,
1915 offsetof(auth_cfg_t, destroy));
1916 this->other_auths->destroy_offset(this->other_auths,
1917 offsetof(auth_cfg_t, destroy));
1918
1919 this->ike_sa_id->destroy(this->ike_sa_id);
1920 free(this);
1921 }
1922
1923 /*
1924 * Described in header.
1925 */
1926 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
1927 ike_version_t version)
1928 {
1929 private_ike_sa_t *this;
1930 static u_int32_t unique_id = 0;
1931
1932 if (version == IKE_ANY)
1933 { /* prefer IKEv2 if protocol not specified */
1934 #ifdef USE_IKEV2
1935 version = IKEV2;
1936 #else
1937 version = IKEV1;
1938 #endif
1939 }
1940
1941 INIT(this,
1942 .public = {
1943 .get_version = _get_version,
1944 .get_state = _get_state,
1945 .set_state = _set_state,
1946 .get_name = _get_name,
1947 .get_statistic = _get_statistic,
1948 .set_statistic = _set_statistic,
1949 .process_message = _process_message,
1950 .initiate = _initiate,
1951 .get_ike_cfg = _get_ike_cfg,
1952 .set_ike_cfg = _set_ike_cfg,
1953 .get_peer_cfg = _get_peer_cfg,
1954 .set_peer_cfg = _set_peer_cfg,
1955 .get_auth_cfg = _get_auth_cfg,
1956 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
1957 .add_auth_cfg = _add_auth_cfg,
1958 .get_proposal = _get_proposal,
1959 .set_proposal = _set_proposal,
1960 .get_id = _get_id,
1961 .get_my_host = _get_my_host,
1962 .set_my_host = _set_my_host,
1963 .get_other_host = _get_other_host,
1964 .set_other_host = _set_other_host,
1965 .set_message_id = _set_message_id,
1966 .float_ports = _float_ports,
1967 .update_hosts = _update_hosts,
1968 .get_my_id = _get_my_id,
1969 .set_my_id = _set_my_id,
1970 .get_other_id = _get_other_id,
1971 .set_other_id = _set_other_id,
1972 .get_other_eap_id = _get_other_eap_id,
1973 .enable_extension = _enable_extension,
1974 .supports_extension = _supports_extension,
1975 .set_condition = _set_condition,
1976 .has_condition = _has_condition,
1977 .set_pending_updates = _set_pending_updates,
1978 .get_pending_updates = _get_pending_updates,
1979 .create_additional_address_enumerator = _create_additional_address_enumerator,
1980 .add_additional_address = _add_additional_address,
1981 .remove_additional_addresses = _remove_additional_addresses,
1982 .has_mapping_changed = _has_mapping_changed,
1983 .retransmit = _retransmit,
1984 .delete = _delete_,
1985 .destroy = _destroy,
1986 .send_dpd = _send_dpd,
1987 .send_keepalive = _send_keepalive,
1988 .get_keymat = _get_keymat,
1989 .add_child_sa = _add_child_sa,
1990 .get_child_sa = _get_child_sa,
1991 .get_child_count = _get_child_count,
1992 .create_child_sa_enumerator = _create_child_sa_enumerator,
1993 .remove_child_sa = _remove_child_sa,
1994 .rekey_child_sa = _rekey_child_sa,
1995 .delete_child_sa = _delete_child_sa,
1996 .destroy_child_sa = _destroy_child_sa,
1997 .rekey = _rekey,
1998 .reauth = _reauth,
1999 .reestablish = _reestablish,
2000 .set_auth_lifetime = _set_auth_lifetime,
2001 .roam = _roam,
2002 .inherit = _inherit,
2003 .generate_message = _generate_message,
2004 .reset = _reset,
2005 .get_unique_id = _get_unique_id,
2006 .set_virtual_ip = _set_virtual_ip,
2007 .get_virtual_ip = _get_virtual_ip,
2008 .add_configuration_attribute = _add_configuration_attribute,
2009 .set_kmaddress = _set_kmaddress,
2010 .create_task_enumerator = _create_task_enumerator,
2011 .queue_task = _queue_task,
2012 #ifdef ME
2013 .act_as_mediation_server = _act_as_mediation_server,
2014 .get_server_reflexive_host = _get_server_reflexive_host,
2015 .set_server_reflexive_host = _set_server_reflexive_host,
2016 .get_connect_id = _get_connect_id,
2017 .initiate_mediation = _initiate_mediation,
2018 .initiate_mediated = _initiate_mediated,
2019 .relay = _relay,
2020 .callback = _callback,
2021 .respond = _respond,
2022 #endif /* ME */
2023 },
2024 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2025 .version = version,
2026 .child_sas = linked_list_create(),
2027 .my_host = host_create_any(AF_INET),
2028 .other_host = host_create_any(AF_INET),
2029 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2030 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2031 .keymat = keymat_create(version, initiator),
2032 .state = IKE_CREATED,
2033 .stats[STAT_INBOUND] = time_monotonic(NULL),
2034 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2035 .my_auth = auth_cfg_create(),
2036 .other_auth = auth_cfg_create(),
2037 .my_auths = linked_list_create(),
2038 .other_auths = linked_list_create(),
2039 .unique_id = ++unique_id,
2040 .additional_addresses = linked_list_create(),
2041 .attributes = linked_list_create(),
2042 .keepalive_interval = lib->settings->get_time(lib->settings,
2043 "charon.keep_alive", KEEPALIVE_INTERVAL),
2044 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2045 "charon.flush_auth_cfg", FALSE),
2046 );
2047
2048 if (version == IKEV2)
2049 { /* always supported with IKEv2 */
2050 enable_extension(this, EXT_DPD);
2051 }
2052
2053 this->task_manager = task_manager_create(&this->public);
2054 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2055
2056 if (!this->task_manager || !this->keymat)
2057 {
2058 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2059 destroy(this);
2060 return NULL;
2061 }
2062 return &this->public;
2063 }