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