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