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