Moved CHILD_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 if (this->state == IKE_CREATED)
1084 {
1085 resolve_hosts(this);
1086
1087 if (this->other_host->is_anyaddr(this->other_host)
1088 #ifdef ME
1089 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1090 #endif /* ME */
1091 )
1092 {
1093 child_cfg->destroy(child_cfg);
1094 DBG1(DBG_IKE, "unable to initiate to %%any");
1095 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1096 return DESTROY_ME;
1097 }
1098
1099 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1100 this->task_manager->queue_ike(this->task_manager);
1101 }
1102
1103 #ifdef ME
1104 if (this->peer_cfg->is_mediation(this->peer_cfg))
1105 {
1106 if (this->state == IKE_ESTABLISHED)
1107 {
1108 /* mediation connection is already established, retrigger state
1109 * change to notify bus listeners */
1110 DBG1(DBG_IKE, "mediation connection is already up");
1111 set_state(this, IKE_ESTABLISHED);
1112 }
1113 DESTROY_IF(child_cfg);
1114 }
1115 else
1116 #endif /* ME */
1117 {
1118 /* normal IKE_SA with CHILD_SA */
1119 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1120 tsi, tsr);
1121 #ifdef ME
1122 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1123 {
1124 /* mediated connection, initiate mediation process */
1125 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1126 lib->processor->queue_job(lib->processor, job);
1127 return SUCCESS;
1128 }
1129 #endif /* ME */
1130 }
1131
1132 return this->task_manager->initiate(this->task_manager);
1133 }
1134
1135 METHOD(ike_sa_t, process_message, status_t,
1136 private_ike_sa_t *this, message_t *message)
1137 {
1138 status_t status;
1139
1140 if (this->state == IKE_PASSIVE)
1141 { /* do not handle messages in passive state */
1142 return FAILED;
1143 }
1144 status = this->task_manager->process_message(this->task_manager, message);
1145 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1146 {
1147 /* authentication completed */
1148 this->flush_auth_cfg = FALSE;
1149 flush_auth_cfgs(this);
1150 }
1151 return status;
1152 }
1153
1154 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1155 private_ike_sa_t *this)
1156 {
1157 return this->ike_sa_id;
1158 }
1159
1160 METHOD(ike_sa_t, get_version, ike_version_t,
1161 private_ike_sa_t *this)
1162 {
1163 return this->version;
1164 }
1165
1166 METHOD(ike_sa_t, get_my_id, identification_t*,
1167 private_ike_sa_t *this)
1168 {
1169 return this->my_id;
1170 }
1171
1172 METHOD(ike_sa_t, set_my_id, void,
1173 private_ike_sa_t *this, identification_t *me)
1174 {
1175 DESTROY_IF(this->my_id);
1176 this->my_id = me;
1177 }
1178
1179 METHOD(ike_sa_t, get_other_id, identification_t*,
1180 private_ike_sa_t *this)
1181 {
1182 return this->other_id;
1183 }
1184
1185 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1186 private_ike_sa_t *this)
1187 {
1188 identification_t *id = NULL, *current;
1189 enumerator_t *enumerator;
1190 auth_cfg_t *cfg;
1191
1192 enumerator = this->other_auths->create_enumerator(this->other_auths);
1193 while (enumerator->enumerate(enumerator, &cfg))
1194 {
1195 /* prefer EAP-Identity of last round */
1196 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1197 if (!current || current->get_type(current) == ID_ANY)
1198 {
1199 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1200 }
1201 if (current && current->get_type(current) != ID_ANY)
1202 {
1203 id = current;
1204 continue;
1205 }
1206 }
1207 enumerator->destroy(enumerator);
1208 if (id)
1209 {
1210 return id;
1211 }
1212 return this->other_id;
1213 }
1214
1215 METHOD(ike_sa_t, set_other_id, void,
1216 private_ike_sa_t *this, identification_t *other)
1217 {
1218 DESTROY_IF(this->other_id);
1219 this->other_id = other;
1220 }
1221
1222 METHOD(ike_sa_t, add_child_sa, void,
1223 private_ike_sa_t *this, child_sa_t *child_sa)
1224 {
1225 this->child_sas->insert_last(this->child_sas, child_sa);
1226 }
1227
1228 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1229 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1230 {
1231 enumerator_t *enumerator;
1232 child_sa_t *current, *found = NULL;
1233
1234 enumerator = this->child_sas->create_enumerator(this->child_sas);
1235 while (enumerator->enumerate(enumerator, (void**)&current))
1236 {
1237 if (current->get_spi(current, inbound) == spi &&
1238 current->get_protocol(current) == protocol)
1239 {
1240 found = current;
1241 }
1242 }
1243 enumerator->destroy(enumerator);
1244 return found;
1245 }
1246
1247 METHOD(ike_sa_t, get_child_count, int,
1248 private_ike_sa_t *this)
1249 {
1250 return this->child_sas->get_count(this->child_sas);
1251 }
1252
1253 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1254 private_ike_sa_t *this)
1255 {
1256 return this->child_sas->create_enumerator(this->child_sas);
1257 }
1258
1259 METHOD(ike_sa_t, remove_child_sa, void,
1260 private_ike_sa_t *this, enumerator_t *enumerator)
1261 {
1262 this->child_sas->remove_at(this->child_sas, enumerator);
1263 }
1264
1265 METHOD(ike_sa_t, rekey_child_sa, status_t,
1266 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1267 {
1268 child_rekey_t *child_rekey;
1269
1270 child_rekey = child_rekey_create(&this->public, protocol, spi);
1271 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1272 return this->task_manager->initiate(this->task_manager);
1273 }
1274
1275 METHOD(ike_sa_t, delete_child_sa, status_t,
1276 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1277 {
1278 task_t *task;
1279
1280 if (this->version == IKEV1)
1281 {
1282 task = (task_t*)quick_delete_create(&this->public, protocol, spi, FALSE);
1283 }
1284 else
1285 {
1286 task = (task_t*)child_delete_create(&this->public, protocol, spi);
1287 }
1288 this->task_manager->queue_task(this->task_manager, task);
1289 return this->task_manager->initiate(this->task_manager);
1290 }
1291
1292 METHOD(ike_sa_t, destroy_child_sa, status_t,
1293 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1294 {
1295 enumerator_t *enumerator;
1296 child_sa_t *child_sa;
1297 status_t status = NOT_FOUND;
1298
1299 enumerator = this->child_sas->create_enumerator(this->child_sas);
1300 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1301 {
1302 if (child_sa->get_protocol(child_sa) == protocol &&
1303 child_sa->get_spi(child_sa, TRUE) == spi)
1304 {
1305 this->child_sas->remove_at(this->child_sas, enumerator);
1306 child_sa->destroy(child_sa);
1307 status = SUCCESS;
1308 break;
1309 }
1310 }
1311 enumerator->destroy(enumerator);
1312 return status;
1313 }
1314
1315 METHOD(ike_sa_t, delete_, status_t,
1316 private_ike_sa_t *this)
1317 {
1318 task_t *task;
1319
1320 switch (this->state)
1321 {
1322 case IKE_ESTABLISHED:
1323 case IKE_REKEYING:
1324 if (this->version == IKEV1)
1325 {
1326 task = (task_t*)isakmp_delete_create(&this->public, TRUE);
1327 }
1328 else
1329 {
1330 task = (task_t*)ike_delete_create(&this->public, TRUE);
1331 }
1332 this->task_manager->queue_task(this->task_manager, task);
1333 return this->task_manager->initiate(this->task_manager);
1334 case IKE_CREATED:
1335 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1336 break;
1337 case IKE_PASSIVE:
1338 break;
1339 default:
1340 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1341 "without notification", ike_sa_state_names, this->state);
1342 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1343 break;
1344 }
1345 return DESTROY_ME;
1346 }
1347
1348 METHOD(ike_sa_t, rekey, status_t,
1349 private_ike_sa_t *this)
1350 {
1351 ike_rekey_t *ike_rekey;
1352
1353 ike_rekey = ike_rekey_create(&this->public, TRUE);
1354
1355 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1356 return this->task_manager->initiate(this->task_manager);
1357 }
1358
1359 METHOD(ike_sa_t, reauth, status_t,
1360 private_ike_sa_t *this)
1361 {
1362 task_t *task;
1363
1364 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1365 * If the peer does not support RFC4478, there is no way to keep the
1366 * IKE_SA up. */
1367 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1368 {
1369 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1370 if (this->other_virtual_ip != NULL ||
1371 has_condition(this, COND_EAP_AUTHENTICATED)
1372 #ifdef ME
1373 /* as mediation server we too cannot reauth the IKE_SA */
1374 || this->is_mediation_server
1375 #endif /* ME */
1376 )
1377 {
1378 time_t now = time_monotonic(NULL);
1379
1380 DBG1(DBG_IKE, "IKE_SA will timeout in %V",
1381 &now, &this->stats[STAT_DELETE]);
1382 return FAILED;
1383 }
1384 else
1385 {
1386 DBG1(DBG_IKE, "reauthenticating actively");
1387 }
1388 }
1389 task = (task_t*)ike_reauth_create(&this->public);
1390 this->task_manager->queue_task(this->task_manager, task);
1391
1392 return this->task_manager->initiate(this->task_manager);
1393 }
1394
1395 METHOD(ike_sa_t, reestablish, status_t,
1396 private_ike_sa_t *this)
1397 {
1398 ike_sa_t *new;
1399 host_t *host;
1400 action_t action;
1401 enumerator_t *enumerator;
1402 child_sa_t *child_sa;
1403 child_cfg_t *child_cfg;
1404 bool restart = FALSE;
1405 status_t status = FAILED;
1406
1407 /* check if we have children to keep up at all */
1408 enumerator = this->child_sas->create_enumerator(this->child_sas);
1409 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1410 {
1411 if (this->state == IKE_DELETING)
1412 {
1413 action = child_sa->get_close_action(child_sa);
1414 }
1415 else
1416 {
1417 action = child_sa->get_dpd_action(child_sa);
1418 }
1419 switch (action)
1420 {
1421 case ACTION_RESTART:
1422 restart = TRUE;
1423 break;
1424 case ACTION_ROUTE:
1425 charon->traps->install(charon->traps, this->peer_cfg,
1426 child_sa->get_config(child_sa));
1427 break;
1428 default:
1429 break;
1430 }
1431 }
1432 enumerator->destroy(enumerator);
1433 #ifdef ME
1434 /* mediation connections have no children, keep them up anyway */
1435 if (this->peer_cfg->is_mediation(this->peer_cfg))
1436 {
1437 restart = TRUE;
1438 }
1439 #endif /* ME */
1440 if (!restart)
1441 {
1442 return FAILED;
1443 }
1444
1445 /* check if we are able to reestablish this IKE_SA */
1446 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1447 (this->other_virtual_ip != NULL ||
1448 has_condition(this, COND_EAP_AUTHENTICATED)
1449 #ifdef ME
1450 || this->is_mediation_server
1451 #endif /* ME */
1452 ))
1453 {
1454 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1455 return FAILED;
1456 }
1457
1458 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1459 this->version, TRUE);
1460 new->set_peer_cfg(new, this->peer_cfg);
1461 host = this->other_host;
1462 new->set_other_host(new, host->clone(host));
1463 host = this->my_host;
1464 new->set_my_host(new, host->clone(host));
1465 /* if we already have a virtual IP, we reuse it */
1466 host = this->my_virtual_ip;
1467 if (host)
1468 {
1469 new->set_virtual_ip(new, TRUE, host);
1470 }
1471
1472 #ifdef ME
1473 if (this->peer_cfg->is_mediation(this->peer_cfg))
1474 {
1475 status = new->initiate(new, NULL, 0, NULL, NULL);
1476 }
1477 else
1478 #endif /* ME */
1479 {
1480 enumerator = this->child_sas->create_enumerator(this->child_sas);
1481 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1482 {
1483 if (this->state == IKE_DELETING)
1484 {
1485 action = child_sa->get_close_action(child_sa);
1486 }
1487 else
1488 {
1489 action = child_sa->get_dpd_action(child_sa);
1490 }
1491 switch (action)
1492 {
1493 case ACTION_RESTART:
1494 child_cfg = child_sa->get_config(child_sa);
1495 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1496 child_cfg->get_name(child_cfg));
1497 child_cfg->get_ref(child_cfg);
1498 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1499 break;
1500 default:
1501 continue;
1502 }
1503 if (status == DESTROY_ME)
1504 {
1505 break;
1506 }
1507 }
1508 enumerator->destroy(enumerator);
1509 }
1510
1511 if (status == DESTROY_ME)
1512 {
1513 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1514 status = FAILED;
1515 }
1516 else
1517 {
1518 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1519 status = SUCCESS;
1520 }
1521 charon->bus->set_sa(charon->bus, &this->public);
1522 return status;
1523 }
1524
1525 /**
1526 * Requeue the IKE_SA_INIT tasks for initiation, if required
1527 */
1528 static void requeue_init_tasks(private_ike_sa_t *this)
1529 {
1530 enumerator_t *enumerator;
1531 bool has_init = FALSE;
1532 task_t *task;
1533
1534 /* if we have advanced to IKE_AUTH, the IKE_INIT and related tasks
1535 * have already completed. Recreate them if necessary. */
1536 enumerator = this->task_manager->create_task_enumerator(
1537 this->task_manager, TASK_QUEUE_QUEUED);
1538 while (enumerator->enumerate(enumerator, &task))
1539 {
1540 if (task->get_type(task) == TASK_IKE_INIT)
1541 {
1542 has_init = TRUE;
1543 break;
1544 }
1545 }
1546 enumerator->destroy(enumerator);
1547
1548 if (!has_init)
1549 {
1550 task = (task_t*)ike_vendor_create(&this->public, TRUE);
1551 this->task_manager->queue_task(this->task_manager, task);
1552 task = (task_t*)ike_natd_create(&this->public, TRUE);
1553 this->task_manager->queue_task(this->task_manager, task);
1554 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1555 this->task_manager->queue_task(this->task_manager, task);
1556 }
1557 }
1558
1559 METHOD(ike_sa_t, retransmit, status_t,
1560 private_ike_sa_t *this, u_int32_t message_id)
1561 {
1562 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1563 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1564 {
1565 /* send a proper signal to brief interested bus listeners */
1566 switch (this->state)
1567 {
1568 case IKE_CONNECTING:
1569 {
1570 /* retry IKE_SA_INIT if we have multiple keyingtries */
1571 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1572 this->keyingtry++;
1573 if (tries == 0 || tries > this->keyingtry)
1574 {
1575 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1576 this->keyingtry + 1, tries);
1577 reset(this);
1578 requeue_init_tasks(this);
1579 return this->task_manager->initiate(this->task_manager);
1580 }
1581 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1582 break;
1583 }
1584 case IKE_DELETING:
1585 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1586 break;
1587 case IKE_REKEYING:
1588 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1589 /* FALL */
1590 default:
1591 reestablish(this);
1592 break;
1593 }
1594 return DESTROY_ME;
1595 }
1596 return SUCCESS;
1597 }
1598
1599 METHOD(ike_sa_t, set_auth_lifetime, void,
1600 private_ike_sa_t *this, u_int32_t lifetime)
1601 {
1602 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
1603 u_int32_t reauth_time = time_monotonic(NULL) + lifetime - reduction;
1604
1605 if (lifetime < reduction)
1606 {
1607 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
1608 lifetime);
1609 lib->processor->queue_job(lib->processor,
1610 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1611 }
1612 else if (this->stats[STAT_REAUTH] == 0 ||
1613 this->stats[STAT_REAUTH] > reauth_time)
1614 {
1615 this->stats[STAT_REAUTH] = reauth_time;
1616 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1617 " in %ds", lifetime, lifetime - reduction);
1618 lib->scheduler->schedule_job(lib->scheduler,
1619 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1620 lifetime - reduction);
1621 }
1622 else
1623 {
1624 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1625 "reauthentication already scheduled in %ds", lifetime,
1626 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1627 }
1628 }
1629
1630 /**
1631 * Check if the current combination of source and destination address is still
1632 * valid.
1633 */
1634 static bool is_current_path_valid(private_ike_sa_t *this)
1635 {
1636 bool valid = FALSE;
1637 host_t *src;
1638 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1639 this->other_host, this->my_host);
1640 if (src)
1641 {
1642 if (src->ip_equals(src, this->my_host))
1643 {
1644 valid = TRUE;
1645 }
1646 src->destroy(src);
1647 }
1648 return valid;
1649 }
1650
1651 /**
1652 * Check if we have any path avialable for this IKE SA.
1653 */
1654 static bool is_any_path_valid(private_ike_sa_t *this)
1655 {
1656 bool valid = FALSE;
1657 enumerator_t *enumerator;
1658 host_t *src, *addr;
1659 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1660 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1661 this->other_host, NULL);
1662 if (!src)
1663 {
1664 enumerator = this->additional_addresses->create_enumerator(
1665 this->additional_addresses);
1666 while (enumerator->enumerate(enumerator, &addr))
1667 {
1668 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1669 src = hydra->kernel_interface->get_source_addr(
1670 hydra->kernel_interface, addr, NULL);
1671 if (src)
1672 {
1673 break;
1674 }
1675 }
1676 enumerator->destroy(enumerator);
1677 }
1678 if (src)
1679 {
1680 valid = TRUE;
1681 src->destroy(src);
1682 }
1683 return valid;
1684 }
1685
1686 METHOD(ike_sa_t, roam, status_t,
1687 private_ike_sa_t *this, bool address)
1688 {
1689 ike_mobike_t *mobike;
1690
1691 switch (this->state)
1692 {
1693 case IKE_CREATED:
1694 case IKE_DELETING:
1695 case IKE_DESTROYING:
1696 case IKE_PASSIVE:
1697 return SUCCESS;
1698 default:
1699 break;
1700 }
1701
1702 /* keep existing path if possible */
1703 if (is_current_path_valid(this))
1704 {
1705 DBG2(DBG_IKE, "keeping connection path %H - %H",
1706 this->my_host, this->other_host);
1707 set_condition(this, COND_STALE, FALSE);
1708
1709 if (supports_extension(this, EXT_MOBIKE) && address)
1710 { /* if any addresses changed, send an updated list */
1711 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1712 mobike = ike_mobike_create(&this->public, TRUE);
1713 mobike->addresses(mobike);
1714 this->task_manager->queue_task(this->task_manager,
1715 (task_t*)mobike);
1716 return this->task_manager->initiate(this->task_manager);
1717 }
1718 return SUCCESS;
1719 }
1720
1721 if (!is_any_path_valid(this))
1722 {
1723 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1724 this->other_host);
1725 set_condition(this, COND_STALE, TRUE);
1726 return SUCCESS;
1727 }
1728 set_condition(this, COND_STALE, FALSE);
1729
1730 /* update addresses with mobike, if supported ... */
1731 if (supports_extension(this, EXT_MOBIKE))
1732 {
1733 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1734 { /* responder updates the peer about changed address config */
1735 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1736 "implicitly requesting an address change");
1737 address = TRUE;
1738 }
1739 else
1740 {
1741 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1742 }
1743 mobike = ike_mobike_create(&this->public, TRUE);
1744 mobike->roam(mobike, address);
1745 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1746 return this->task_manager->initiate(this->task_manager);
1747 }
1748
1749 /* ... reauth if not */
1750 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1751 { /* responder does not reauthenticate */
1752 set_condition(this, COND_STALE, TRUE);
1753 return SUCCESS;
1754 }
1755 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1756 return reauth(this);
1757 }
1758
1759 METHOD(ike_sa_t, add_configuration_attribute, void,
1760 private_ike_sa_t *this, attribute_handler_t *handler,
1761 configuration_attribute_type_t type, chunk_t data)
1762 {
1763 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1764
1765 entry->handler = handler;
1766 entry->type = type;
1767 entry->data = chunk_clone(data);
1768
1769 this->attributes->insert_last(this->attributes, entry);
1770 }
1771
1772 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1773 private_ike_sa_t *this, task_queue_t queue)
1774 {
1775 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1776 }
1777
1778 METHOD(ike_sa_t, queue_task, void,
1779 private_ike_sa_t *this, task_t *task)
1780 {
1781 this->task_manager->queue_task(this->task_manager, task);
1782 }
1783
1784 METHOD(ike_sa_t, inherit, void,
1785 private_ike_sa_t *this, ike_sa_t *other_public)
1786 {
1787 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1788 child_sa_t *child_sa;
1789 attribute_entry_t *entry;
1790 enumerator_t *enumerator;
1791 auth_cfg_t *cfg;
1792
1793 /* apply hosts and ids */
1794 this->my_host->destroy(this->my_host);
1795 this->other_host->destroy(this->other_host);
1796 this->my_id->destroy(this->my_id);
1797 this->other_id->destroy(this->other_id);
1798 this->my_host = other->my_host->clone(other->my_host);
1799 this->other_host = other->other_host->clone(other->other_host);
1800 this->my_id = other->my_id->clone(other->my_id);
1801 this->other_id = other->other_id->clone(other->other_id);
1802
1803 /* apply virtual assigned IPs... */
1804 if (other->my_virtual_ip)
1805 {
1806 this->my_virtual_ip = other->my_virtual_ip;
1807 other->my_virtual_ip = NULL;
1808 }
1809 if (other->other_virtual_ip)
1810 {
1811 this->other_virtual_ip = other->other_virtual_ip;
1812 other->other_virtual_ip = NULL;
1813 }
1814
1815 /* authentication information */
1816 enumerator = other->my_auths->create_enumerator(other->my_auths);
1817 while (enumerator->enumerate(enumerator, &cfg))
1818 {
1819 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1820 }
1821 enumerator->destroy(enumerator);
1822 enumerator = other->other_auths->create_enumerator(other->other_auths);
1823 while (enumerator->enumerate(enumerator, &cfg))
1824 {
1825 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1826 }
1827 enumerator->destroy(enumerator);
1828
1829 /* ... and configuration attributes */
1830 while (other->attributes->remove_last(other->attributes,
1831 (void**)&entry) == SUCCESS)
1832 {
1833 this->attributes->insert_first(this->attributes, entry);
1834 }
1835
1836 /* inherit all conditions */
1837 this->conditions = other->conditions;
1838 if (this->conditions & COND_NAT_HERE)
1839 {
1840 send_keepalive(this);
1841 }
1842
1843 #ifdef ME
1844 if (other->is_mediation_server)
1845 {
1846 act_as_mediation_server(this);
1847 }
1848 else if (other->server_reflexive_host)
1849 {
1850 this->server_reflexive_host = other->server_reflexive_host->clone(
1851 other->server_reflexive_host);
1852 }
1853 #endif /* ME */
1854
1855 /* adopt all children */
1856 while (other->child_sas->remove_last(other->child_sas,
1857 (void**)&child_sa) == SUCCESS)
1858 {
1859 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1860 }
1861
1862 /* move pending tasks to the new IKE_SA */
1863 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1864
1865 /* reauthentication timeout survives a rekeying */
1866 if (other->stats[STAT_REAUTH])
1867 {
1868 time_t reauth, delete, now = time_monotonic(NULL);
1869
1870 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
1871 reauth = this->stats[STAT_REAUTH] - now;
1872 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
1873 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
1874 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
1875 "lifetime reduced to %ds", reauth, delete);
1876 lib->scheduler->schedule_job(lib->scheduler,
1877 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
1878 lib->scheduler->schedule_job(lib->scheduler,
1879 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
1880 }
1881 }
1882
1883 METHOD(ike_sa_t, destroy, void,
1884 private_ike_sa_t *this)
1885 {
1886 attribute_entry_t *entry;
1887
1888 charon->bus->set_sa(charon->bus, &this->public);
1889
1890 set_state(this, IKE_DESTROYING);
1891 this->task_manager->destroy(this->task_manager);
1892
1893 /* remove attributes first, as we pass the IKE_SA to the handler */
1894 while (this->attributes->remove_last(this->attributes,
1895 (void**)&entry) == SUCCESS)
1896 {
1897 hydra->attributes->release(hydra->attributes, entry->handler,
1898 this->other_id, entry->type, entry->data);
1899 free(entry->data.ptr);
1900 free(entry);
1901 }
1902 this->attributes->destroy(this->attributes);
1903
1904 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1905
1906 /* unset SA after here to avoid usage by the listeners */
1907 charon->bus->set_sa(charon->bus, NULL);
1908
1909 this->keymat->destroy(this->keymat);
1910
1911 if (this->my_virtual_ip)
1912 {
1913 hydra->kernel_interface->del_ip(hydra->kernel_interface,
1914 this->my_virtual_ip);
1915 this->my_virtual_ip->destroy(this->my_virtual_ip);
1916 }
1917 if (this->other_virtual_ip)
1918 {
1919 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
1920 {
1921 hydra->attributes->release_address(hydra->attributes,
1922 this->peer_cfg->get_pool(this->peer_cfg),
1923 this->other_virtual_ip, get_other_eap_id(this));
1924 }
1925 this->other_virtual_ip->destroy(this->other_virtual_ip);
1926 }
1927 this->additional_addresses->destroy_offset(this->additional_addresses,
1928 offsetof(host_t, destroy));
1929 #ifdef ME
1930 if (this->is_mediation_server)
1931 {
1932 charon->mediation_manager->remove(charon->mediation_manager,
1933 this->ike_sa_id);
1934 }
1935 DESTROY_IF(this->server_reflexive_host);
1936 chunk_free(&this->connect_id);
1937 #endif /* ME */
1938 free(this->nat_detection_dest.ptr);
1939
1940 DESTROY_IF(this->my_host);
1941 DESTROY_IF(this->other_host);
1942 DESTROY_IF(this->my_id);
1943 DESTROY_IF(this->other_id);
1944 DESTROY_IF(this->local_host);
1945 DESTROY_IF(this->remote_host);
1946
1947 DESTROY_IF(this->ike_cfg);
1948 DESTROY_IF(this->peer_cfg);
1949 DESTROY_IF(this->proposal);
1950 this->my_auth->destroy(this->my_auth);
1951 this->other_auth->destroy(this->other_auth);
1952 this->my_auths->destroy_offset(this->my_auths,
1953 offsetof(auth_cfg_t, destroy));
1954 this->other_auths->destroy_offset(this->other_auths,
1955 offsetof(auth_cfg_t, destroy));
1956
1957 this->ike_sa_id->destroy(this->ike_sa_id);
1958 free(this);
1959 }
1960
1961 /*
1962 * Described in header.
1963 */
1964 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
1965 ike_version_t version)
1966 {
1967 private_ike_sa_t *this;
1968 static u_int32_t unique_id = 0;
1969
1970 if (version != IKEV1)
1971 {
1972 version = IKEV2;
1973 }
1974
1975 INIT(this,
1976 .public = {
1977 .get_version = _get_version,
1978 .get_state = _get_state,
1979 .set_state = _set_state,
1980 .get_name = _get_name,
1981 .get_statistic = _get_statistic,
1982 .set_statistic = _set_statistic,
1983 .process_message = _process_message,
1984 .initiate = _initiate,
1985 .get_ike_cfg = _get_ike_cfg,
1986 .set_ike_cfg = _set_ike_cfg,
1987 .get_peer_cfg = _get_peer_cfg,
1988 .set_peer_cfg = _set_peer_cfg,
1989 .get_auth_cfg = _get_auth_cfg,
1990 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
1991 .add_auth_cfg = _add_auth_cfg,
1992 .get_proposal = _get_proposal,
1993 .set_proposal = _set_proposal,
1994 .get_id = _get_id,
1995 .get_my_host = _get_my_host,
1996 .set_my_host = _set_my_host,
1997 .get_other_host = _get_other_host,
1998 .set_other_host = _set_other_host,
1999 .set_message_id = _set_message_id,
2000 .float_ports = _float_ports,
2001 .update_hosts = _update_hosts,
2002 .get_my_id = _get_my_id,
2003 .set_my_id = _set_my_id,
2004 .get_other_id = _get_other_id,
2005 .set_other_id = _set_other_id,
2006 .get_other_eap_id = _get_other_eap_id,
2007 .enable_extension = _enable_extension,
2008 .supports_extension = _supports_extension,
2009 .set_condition = _set_condition,
2010 .has_condition = _has_condition,
2011 .set_pending_updates = _set_pending_updates,
2012 .get_pending_updates = _get_pending_updates,
2013 .create_additional_address_enumerator = _create_additional_address_enumerator,
2014 .add_additional_address = _add_additional_address,
2015 .remove_additional_addresses = _remove_additional_addresses,
2016 .has_mapping_changed = _has_mapping_changed,
2017 .retransmit = _retransmit,
2018 .delete = _delete_,
2019 .destroy = _destroy,
2020 .send_dpd = _send_dpd,
2021 .send_keepalive = _send_keepalive,
2022 .get_keymat = _get_keymat,
2023 .add_child_sa = _add_child_sa,
2024 .get_child_sa = _get_child_sa,
2025 .get_child_count = _get_child_count,
2026 .create_child_sa_enumerator = _create_child_sa_enumerator,
2027 .remove_child_sa = _remove_child_sa,
2028 .rekey_child_sa = _rekey_child_sa,
2029 .delete_child_sa = _delete_child_sa,
2030 .destroy_child_sa = _destroy_child_sa,
2031 .rekey = _rekey,
2032 .reauth = _reauth,
2033 .reestablish = _reestablish,
2034 .set_auth_lifetime = _set_auth_lifetime,
2035 .roam = _roam,
2036 .inherit = _inherit,
2037 .generate_message = _generate_message,
2038 .reset = _reset,
2039 .get_unique_id = _get_unique_id,
2040 .set_virtual_ip = _set_virtual_ip,
2041 .get_virtual_ip = _get_virtual_ip,
2042 .add_configuration_attribute = _add_configuration_attribute,
2043 .set_kmaddress = _set_kmaddress,
2044 .create_task_enumerator = _create_task_enumerator,
2045 .queue_task = _queue_task,
2046 #ifdef ME
2047 .act_as_mediation_server = _act_as_mediation_server,
2048 .get_server_reflexive_host = _get_server_reflexive_host,
2049 .set_server_reflexive_host = _set_server_reflexive_host,
2050 .get_connect_id = _get_connect_id,
2051 .initiate_mediation = _initiate_mediation,
2052 .initiate_mediated = _initiate_mediated,
2053 .relay = _relay,
2054 .callback = _callback,
2055 .respond = _respond,
2056 #endif /* ME */
2057 },
2058 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2059 .version = version,
2060 .child_sas = linked_list_create(),
2061 .my_host = host_create_any(AF_INET),
2062 .other_host = host_create_any(AF_INET),
2063 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2064 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2065 .keymat = keymat_create(version, initiator),
2066 .state = IKE_CREATED,
2067 .stats[STAT_INBOUND] = time_monotonic(NULL),
2068 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2069 .my_auth = auth_cfg_create(),
2070 .other_auth = auth_cfg_create(),
2071 .my_auths = linked_list_create(),
2072 .other_auths = linked_list_create(),
2073 .unique_id = ++unique_id,
2074 .additional_addresses = linked_list_create(),
2075 .attributes = linked_list_create(),
2076 .keepalive_interval = lib->settings->get_time(lib->settings,
2077 "charon.keep_alive", KEEPALIVE_INTERVAL),
2078 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2079 "charon.flush_auth_cfg", FALSE),
2080 );
2081
2082 this->task_manager = task_manager_create(&this->public);
2083 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2084
2085 return &this->public;
2086 }