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