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