fallback to family specific %any(6) if kernel lookup fails
[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 else
1106 { /* fallback to address family specific %any(6), if configured */
1107 host = host_create_from_dns(
1108 this->ike_cfg->get_my_addr(this->ike_cfg),
1109 0, IKEV2_UDP_PORT);
1110 }
1111 }
1112 }
1113 if (host)
1114 {
1115 set_my_host(this, host);
1116 }
1117 }
1118
1119 /**
1120 * Initiates a CHILD_SA using the appropriate reqid
1121 */
1122 static status_t initiate_with_reqid(private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid)
1123 {
1124 task_t *task;
1125
1126 if (this->state == IKE_CREATED)
1127 {
1128 resolve_hosts(this);
1129
1130 if (this->other_host->is_anyaddr(this->other_host)
1131 #ifdef ME
1132 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1133 #endif /* ME */
1134 )
1135 {
1136 child_cfg->destroy(child_cfg);
1137 DBG1(DBG_IKE, "unable to initiate to %%any");
1138 return DESTROY_ME;
1139 }
1140
1141 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1142
1143 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1144 this->task_manager->queue_task(this->task_manager, task);
1145 task = (task_t*)ike_natd_create(&this->public, TRUE);
1146 this->task_manager->queue_task(this->task_manager, task);
1147 task = (task_t*)ike_cert_pre_create(&this->public, TRUE);
1148 this->task_manager->queue_task(this->task_manager, task);
1149 task = (task_t*)ike_auth_create(&this->public, TRUE);
1150 this->task_manager->queue_task(this->task_manager, task);
1151 task = (task_t*)ike_cert_post_create(&this->public, TRUE);
1152 this->task_manager->queue_task(this->task_manager, task);
1153 task = (task_t*)ike_config_create(&this->public, TRUE);
1154 this->task_manager->queue_task(this->task_manager, task);
1155 task = (task_t*)ike_auth_lifetime_create(&this->public, TRUE);
1156 this->task_manager->queue_task(this->task_manager, task);
1157 if (this->peer_cfg->use_mobike(this->peer_cfg))
1158 {
1159 task = (task_t*)ike_mobike_create(&this->public, TRUE);
1160 this->task_manager->queue_task(this->task_manager, task);
1161 }
1162 #ifdef ME
1163 task = (task_t*)ike_me_create(&this->public, TRUE);
1164 this->task_manager->queue_task(this->task_manager, task);
1165 #endif /* ME */
1166 }
1167
1168 #ifdef ME
1169 if (this->peer_cfg->is_mediation(this->peer_cfg))
1170 {
1171 if (this->state == IKE_ESTABLISHED)
1172 {
1173 /* mediation connection is already established, retrigger state change
1174 * to notify bus listeners */
1175 DBG1(DBG_IKE, "mediation connection is already up");
1176 set_state(this, IKE_ESTABLISHED);
1177 }
1178 DESTROY_IF(child_cfg);
1179 }
1180 else
1181 #endif /* ME */
1182 {
1183 /* normal IKE_SA with CHILD_SA */
1184 task = (task_t*)child_create_create(&this->public, child_cfg);
1185 child_cfg->destroy(child_cfg);
1186 if (reqid)
1187 {
1188 child_create_t *child_create = (child_create_t*)task;
1189 child_create->use_reqid(child_create, reqid);
1190 }
1191 this->task_manager->queue_task(this->task_manager, task);
1192
1193 #ifdef ME
1194 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1195 {
1196 /* mediated connection, initiate mediation process */
1197 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1198 charon->processor->queue_job(charon->processor, job);
1199 return SUCCESS;
1200 }
1201 #endif /* ME */
1202 }
1203
1204 return this->task_manager->initiate(this->task_manager);
1205 }
1206
1207 /**
1208 * Implementation of ike_sa_t.initiate.
1209 */
1210 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
1211 {
1212 return initiate_with_reqid(this, child_cfg, 0);
1213 }
1214
1215 /**
1216 * Implementation of ike_sa_t.acquire.
1217 */
1218 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
1219 {
1220 child_cfg_t *child_cfg;
1221 iterator_t *iterator;
1222 child_sa_t *current, *child_sa = NULL;
1223
1224 switch (this->state)
1225 {
1226 case IKE_DELETING:
1227 DBG1(DBG_IKE, "acquiring CHILD_SA {reqid %d} failed: "
1228 "IKE_SA is deleting", reqid);
1229 return FAILED;
1230 case IKE_PASSIVE:
1231 /* do not process acquires if passive */
1232 return FAILED;
1233 default:
1234 break;
1235 }
1236
1237 /* find CHILD_SA */
1238 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1239 while (iterator->iterate(iterator, (void**)&current))
1240 {
1241 if (current->get_reqid(current) == reqid)
1242 {
1243 child_sa = current;
1244 break;
1245 }
1246 }
1247 iterator->destroy(iterator);
1248 if (!child_sa)
1249 {
1250 DBG1(DBG_IKE, "acquiring CHILD_SA {reqid %d} failed: "
1251 "CHILD_SA not found", reqid);
1252 return FAILED;
1253 }
1254
1255 child_cfg = child_sa->get_config(child_sa);
1256 child_cfg->get_ref(child_cfg);
1257
1258 return initiate_with_reqid(this, child_cfg, reqid);
1259 }
1260
1261 /**
1262 * Implementation of ike_sa_t.route.
1263 */
1264 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
1265 {
1266 child_sa_t *child_sa;
1267 iterator_t *iterator;
1268 linked_list_t *my_ts, *other_ts;
1269 host_t *me, *other;
1270 status_t status;
1271
1272 /* check if not already routed*/
1273 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1274 while (iterator->iterate(iterator, (void**)&child_sa))
1275 {
1276 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1277 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
1278 {
1279 iterator->destroy(iterator);
1280 DBG1(DBG_IKE, "routing CHILD_SA failed: already routed");
1281 return FAILED;
1282 }
1283 }
1284 iterator->destroy(iterator);
1285
1286 switch (this->state)
1287 {
1288 case IKE_DELETING:
1289 case IKE_REKEYING:
1290 DBG1(DBG_IKE, "routing CHILD_SA failed: IKE_SA is %N",
1291 ike_sa_state_names, this->state);
1292 return FAILED;
1293 case IKE_CREATED:
1294 case IKE_CONNECTING:
1295 case IKE_ESTABLISHED:
1296 case IKE_PASSIVE:
1297 default:
1298 break;
1299 }
1300
1301 resolve_hosts(this);
1302
1303 /* install kernel policies */
1304 child_sa = child_sa_create(this->my_host, this->other_host,
1305 child_cfg, 0, FALSE);
1306 me = this->my_host;
1307 if (this->my_virtual_ip)
1308 {
1309 me = this->my_virtual_ip;
1310 }
1311 other = this->other_host;
1312 if (this->other_virtual_ip)
1313 {
1314 other = this->other_virtual_ip;
1315 }
1316
1317 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, me);
1318 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, other);
1319
1320 child_sa->set_mode(child_sa, child_cfg->get_mode(child_cfg));
1321 status = child_sa->add_policies(child_sa, my_ts, other_ts);
1322
1323 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1324 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1325 if (status == SUCCESS)
1326 {
1327 this->child_sas->insert_last(this->child_sas, child_sa);
1328 DBG1(DBG_IKE, "CHILD_SA routed");
1329 }
1330 else
1331 {
1332 child_sa->destroy(child_sa);
1333 DBG1(DBG_IKE, "routing CHILD_SA failed");
1334 }
1335 return status;
1336 }
1337
1338 /**
1339 * Implementation of ike_sa_t.unroute.
1340 */
1341 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
1342 {
1343 iterator_t *iterator;
1344 child_sa_t *child_sa;
1345 bool found = FALSE;
1346
1347 /* find CHILD_SA in ROUTED state */
1348 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1349 while (iterator->iterate(iterator, (void**)&child_sa))
1350 {
1351 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1352 child_sa->get_reqid(child_sa) == reqid)
1353 {
1354 iterator->remove(iterator);
1355 DBG1(DBG_IKE, "CHILD_SA unrouted");
1356 child_sa->destroy(child_sa);
1357 found = TRUE;
1358 break;
1359 }
1360 }
1361 iterator->destroy(iterator);
1362
1363 if (!found)
1364 {
1365 DBG1(DBG_IKE, "unrouting CHILD_SA failed: reqid %d not found", reqid);
1366 return FAILED;
1367 }
1368 /* if we are not established, and we have no more routed childs, remove whole SA */
1369 if (this->state == IKE_CREATED &&
1370 this->child_sas->get_count(this->child_sas) == 0)
1371 {
1372 return DESTROY_ME;
1373 }
1374 return SUCCESS;
1375 }
1376
1377 /**
1378 * Implementation of ike_sa_t.process_message.
1379 */
1380 static status_t process_message(private_ike_sa_t *this, message_t *message)
1381 {
1382 status_t status;
1383 bool is_request;
1384
1385 if (this->state == IKE_PASSIVE)
1386 { /* do not handle messages in passive state */
1387 return FAILED;
1388 }
1389
1390 is_request = message->get_request(message);
1391
1392 status = message->parse_body(message,
1393 this->keymat->get_crypter(this->keymat, TRUE),
1394 this->keymat->get_signer(this->keymat, TRUE));
1395 if (status != SUCCESS)
1396 {
1397
1398 if (is_request)
1399 {
1400 switch (status)
1401 {
1402 case NOT_SUPPORTED:
1403 DBG1(DBG_IKE, "critical unknown payloads found");
1404 if (is_request)
1405 {
1406 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
1407 }
1408 break;
1409 case PARSE_ERROR:
1410 DBG1(DBG_IKE, "message parsing failed");
1411 if (is_request)
1412 {
1413 send_notify_response(this, message, INVALID_SYNTAX);
1414 }
1415 break;
1416 case VERIFY_ERROR:
1417 DBG1(DBG_IKE, "message verification failed");
1418 if (is_request)
1419 {
1420 send_notify_response(this, message, INVALID_SYNTAX);
1421 }
1422 break;
1423 case FAILED:
1424 DBG1(DBG_IKE, "integrity check failed");
1425 /* ignored */
1426 break;
1427 case INVALID_STATE:
1428 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1429 if (is_request)
1430 {
1431 send_notify_response(this, message, INVALID_SYNTAX);
1432 }
1433 default:
1434 break;
1435 }
1436 }
1437 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1438 exchange_type_names, message->get_exchange_type(message),
1439 message->get_request(message) ? "request" : "response",
1440 message->get_message_id(message));
1441 return status;
1442 }
1443 else
1444 {
1445 host_t *me, *other;
1446 private_ike_sa_t *new;
1447 iterator_t *iterator;
1448 child_sa_t *child;
1449 bool has_routed = FALSE;
1450
1451 me = message->get_destination(message);
1452 other = message->get_source(message);
1453
1454 /* if this IKE_SA is virgin, we check for a config */
1455 if (this->ike_cfg == NULL)
1456 {
1457 job_t *job;
1458 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1459 me, other);
1460 if (this->ike_cfg == NULL)
1461 {
1462 /* no config found for these hosts, destroy */
1463 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1464 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1465 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
1466 return DESTROY_ME;
1467 }
1468 /* add a timeout if peer does not establish it completely */
1469 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
1470 charon->scheduler->schedule_job(charon->scheduler, job,
1471 HALF_OPEN_IKE_SA_TIMEOUT);
1472 }
1473 this->stats[STAT_INBOUND] = time(NULL);
1474 /* check if message is trustworthy, and update host information */
1475 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
1476 message->get_exchange_type(message) != IKE_SA_INIT)
1477 {
1478 if (!supports_extension(this, EXT_MOBIKE))
1479 { /* with MOBIKE, we do no implicit updates */
1480 update_hosts(this, me, other);
1481 }
1482 }
1483 status = this->task_manager->process_message(this->task_manager, message);
1484 if (status != DESTROY_ME)
1485 {
1486 if (message->get_exchange_type(message) == IKE_AUTH &&
1487 this->state == IKE_ESTABLISHED)
1488 {
1489 /* purge auth items if SA is up, as they contain certs
1490 * and other memory wasting elements */
1491 this->my_auth->purge(this->my_auth);
1492 this->other_auth->purge(this->other_auth);
1493 }
1494 return status;
1495 }
1496 /* if IKE_SA gets closed for any reasons, reroute routed children */
1497 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1498 while (iterator->iterate(iterator, (void**)&child))
1499 {
1500 if (child->get_state(child) == CHILD_ROUTED)
1501 {
1502 has_routed = TRUE;
1503 break;
1504 }
1505 }
1506 iterator->destroy(iterator);
1507 if (!has_routed)
1508 {
1509 return status;
1510 }
1511 /* move routed children to a new IKE_SA, apply connection info */
1512 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1513 charon->ike_sa_manager, TRUE);
1514 set_peer_cfg(new, this->peer_cfg);
1515 new->other_host->destroy(new->other_host);
1516 new->other_host = this->other_host->clone(this->other_host);
1517 if (!has_condition(this, COND_NAT_THERE))
1518 {
1519 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1520 }
1521 if (this->my_virtual_ip)
1522 {
1523 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1524 }
1525 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1526 while (iterator->iterate(iterator, (void**)&child))
1527 {
1528 if (child->get_state(child) == CHILD_ROUTED)
1529 {
1530 route(new, child->get_config(child));
1531 }
1532 }
1533 iterator->destroy(iterator);
1534 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1535 return status;
1536 }
1537 }
1538
1539 /**
1540 * Implementation of ike_sa_t.get_id.
1541 */
1542 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1543 {
1544 return this->ike_sa_id;
1545 }
1546
1547 /**
1548 * Implementation of ike_sa_t.get_my_id.
1549 */
1550 static identification_t* get_my_id(private_ike_sa_t *this)
1551 {
1552 return this->my_id;
1553 }
1554
1555 /**
1556 * Implementation of ike_sa_t.set_my_id.
1557 */
1558 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1559 {
1560 DESTROY_IF(this->my_id);
1561 this->my_id = me;
1562 }
1563
1564 /**
1565 * Implementation of ike_sa_t.get_other_id.
1566 */
1567 static identification_t* get_other_id(private_ike_sa_t *this)
1568 {
1569 return this->other_id;
1570 }
1571
1572 /**
1573 * Implementation of ike_sa_t.set_other_id.
1574 */
1575 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1576 {
1577 DESTROY_IF(this->other_id);
1578 this->other_id = other;
1579 }
1580
1581 /**
1582 * Implementation of ike_sa_t.get_eap_identity.
1583 */
1584 static identification_t* get_eap_identity(private_ike_sa_t *this)
1585 {
1586 return this->eap_identity;
1587 }
1588
1589 /**
1590 * Implementation of ike_sa_t.set_eap_identity.
1591 */
1592 static void set_eap_identity(private_ike_sa_t *this, identification_t *id)
1593 {
1594 DESTROY_IF(this->eap_identity);
1595 this->eap_identity = id;
1596 }
1597
1598 /**
1599 * Implementation of ike_sa_t.add_child_sa.
1600 */
1601 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1602 {
1603 this->child_sas->insert_last(this->child_sas, child_sa);
1604 }
1605
1606 /**
1607 * Implementation of ike_sa_t.get_child_sa.
1608 */
1609 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1610 u_int32_t spi, bool inbound)
1611 {
1612 iterator_t *iterator;
1613 child_sa_t *current, *found = NULL;
1614
1615 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1616 while (iterator->iterate(iterator, (void**)&current))
1617 {
1618 if (current->get_spi(current, inbound) == spi &&
1619 current->get_protocol(current) == protocol)
1620 {
1621 found = current;
1622 }
1623 }
1624 iterator->destroy(iterator);
1625 return found;
1626 }
1627
1628 /**
1629 * Implementation of ike_sa_t.create_child_sa_iterator.
1630 */
1631 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1632 {
1633 return this->child_sas->create_iterator(this->child_sas, TRUE);
1634 }
1635
1636 /**
1637 * Implementation of ike_sa_t.rekey_child_sa.
1638 */
1639 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1640 u_int32_t spi)
1641 {
1642 child_rekey_t *child_rekey;
1643
1644 child_rekey = child_rekey_create(&this->public, protocol, spi);
1645 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1646 return this->task_manager->initiate(this->task_manager);
1647 }
1648
1649 /**
1650 * Implementation of ike_sa_t.delete_child_sa.
1651 */
1652 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1653 u_int32_t spi)
1654 {
1655 child_delete_t *child_delete;
1656
1657 child_delete = child_delete_create(&this->public, protocol, spi);
1658 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1659 return this->task_manager->initiate(this->task_manager);
1660 }
1661
1662 /**
1663 * Implementation of ike_sa_t.destroy_child_sa.
1664 */
1665 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1666 u_int32_t spi)
1667 {
1668 iterator_t *iterator;
1669 child_sa_t *child_sa;
1670 status_t status = NOT_FOUND;
1671
1672 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1673 while (iterator->iterate(iterator, (void**)&child_sa))
1674 {
1675 if (child_sa->get_protocol(child_sa) == protocol &&
1676 child_sa->get_spi(child_sa, TRUE) == spi)
1677 {
1678 child_sa->destroy(child_sa);
1679 iterator->remove(iterator);
1680 status = SUCCESS;
1681 break;
1682 }
1683 }
1684 iterator->destroy(iterator);
1685 return status;
1686 }
1687
1688 /**
1689 * Implementation of public_ike_sa_t.delete.
1690 */
1691 static status_t delete_(private_ike_sa_t *this)
1692 {
1693 ike_delete_t *ike_delete;
1694
1695 switch (this->state)
1696 {
1697 case IKE_ESTABLISHED:
1698 case IKE_REKEYING:
1699 ike_delete = ike_delete_create(&this->public, TRUE);
1700 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1701 return this->task_manager->initiate(this->task_manager);
1702 case IKE_CREATED:
1703 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1704 break;
1705 case IKE_PASSIVE:
1706 break;
1707 default:
1708 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1709 "without notification", ike_sa_state_names, this->state);
1710 break;
1711 }
1712 return DESTROY_ME;
1713 }
1714
1715 /**
1716 * Implementation of ike_sa_t.rekey.
1717 */
1718 static status_t rekey(private_ike_sa_t *this)
1719 {
1720 ike_rekey_t *ike_rekey;
1721
1722 ike_rekey = ike_rekey_create(&this->public, TRUE);
1723
1724 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1725 return this->task_manager->initiate(this->task_manager);
1726 }
1727
1728 /**
1729 * Implementation of ike_sa_t.reauth
1730 */
1731 static status_t reauth(private_ike_sa_t *this)
1732 {
1733 task_t *task;
1734
1735 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1736 * If the peer does not support RFC4478, there is no way to keep the
1737 * IKE_SA up. */
1738 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1739 {
1740 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1741 if (this->other_virtual_ip != NULL ||
1742 has_condition(this, COND_EAP_AUTHENTICATED)
1743 #ifdef ME
1744 /* if we are mediation server we too cannot reauth the IKE_SA */
1745 || this->is_mediation_server
1746 #endif /* ME */
1747 )
1748 {
1749 time_t now = time(NULL);
1750
1751 DBG1(DBG_IKE, "IKE_SA will timeout in %V",
1752 &now, &this->stats[STAT_DELETE]);
1753 return FAILED;
1754 }
1755 else
1756 {
1757 DBG1(DBG_IKE, "reauthenticating actively");
1758 }
1759 }
1760 task = (task_t*)ike_reauth_create(&this->public);
1761 this->task_manager->queue_task(this->task_manager, task);
1762
1763 return this->task_manager->initiate(this->task_manager);
1764 }
1765
1766 /**
1767 * Implementation of ike_sa_t.reestablish
1768 */
1769 static status_t reestablish(private_ike_sa_t *this)
1770 {
1771 ike_sa_t *new;
1772 host_t *host;
1773 action_t action;
1774 iterator_t *iterator;
1775 child_sa_t *child_sa;
1776 child_cfg_t *child_cfg;
1777 bool required = FALSE;
1778 status_t status = FAILED;
1779
1780 /* check if we have children to keep up at all*/
1781 iterator = create_child_sa_iterator(this);
1782 while (iterator->iterate(iterator, (void**)&child_sa))
1783 {
1784 child_cfg = child_sa->get_config(child_sa);
1785 if (this->state == IKE_DELETING)
1786 {
1787 action = child_cfg->get_close_action(child_cfg);
1788 }
1789 else
1790 {
1791 action = child_cfg->get_dpd_action(child_cfg);
1792 }
1793 switch (action)
1794 {
1795 case ACTION_RESTART:
1796 case ACTION_ROUTE:
1797 required = TRUE;
1798 default:
1799 break;
1800 }
1801 }
1802 iterator->destroy(iterator);
1803 #ifdef ME
1804 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
1805 if (this->peer_cfg->is_mediation(this->peer_cfg))
1806 {
1807 required = TRUE;
1808 }
1809 #endif /* ME */
1810 if (!required)
1811 {
1812 return FAILED;
1813 }
1814
1815 /* check if we are able to reestablish this IKE_SA */
1816 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1817 (this->other_virtual_ip != NULL ||
1818 has_condition(this, COND_EAP_AUTHENTICATED)
1819 #ifdef ME
1820 || this->is_mediation_server
1821 #endif /* ME */
1822 ))
1823 {
1824 DBG1(DBG_IKE, "unable to reestablish IKE_SA due asymetric setup");
1825 return FAILED;
1826 }
1827
1828 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager, TRUE);
1829 new->set_peer_cfg(new, this->peer_cfg);
1830 host = this->other_host;
1831 new->set_other_host(new, host->clone(host));
1832 host = this->my_host;
1833 new->set_my_host(new, host->clone(host));
1834 /* if we already have a virtual IP, we reuse it */
1835 host = this->my_virtual_ip;
1836 if (host)
1837 {
1838 new->set_virtual_ip(new, TRUE, host);
1839 }
1840
1841 #ifdef ME
1842 if (this->peer_cfg->is_mediation(this->peer_cfg))
1843 {
1844 status = new->initiate(new, NULL);
1845 }
1846 else
1847 #endif /* ME */
1848 {
1849 iterator = create_child_sa_iterator(this);
1850 while (iterator->iterate(iterator, (void**)&child_sa))
1851 {
1852 child_cfg = child_sa->get_config(child_sa);
1853 if (this->state == IKE_DELETING)
1854 {
1855 action = child_cfg->get_close_action(child_cfg);
1856 }
1857 else
1858 {
1859 action = child_cfg->get_dpd_action(child_cfg);
1860 }
1861 switch (action)
1862 {
1863 case ACTION_RESTART:
1864 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1865 child_cfg->get_name(child_cfg));
1866 child_cfg->get_ref(child_cfg);
1867 status = new->initiate(new, child_cfg);
1868 break;
1869 case ACTION_ROUTE:
1870 status = new->route(new, child_cfg);
1871 break;
1872 default:
1873 continue;
1874 }
1875 if (status == DESTROY_ME)
1876 {
1877 break;
1878 }
1879 }
1880 iterator->destroy(iterator);
1881 }
1882
1883 if (status == DESTROY_ME)
1884 {
1885 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1886 return FAILED;
1887 }
1888 else
1889 {
1890 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1891 return SUCCESS;
1892 }
1893 }
1894
1895 /**
1896 * Implementation of ike_sa_t.retransmit.
1897 */
1898 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
1899 {
1900 this->stats[STAT_OUTBOUND] = time(NULL);
1901 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1902 {
1903 /* send a proper signal to brief interested bus listeners */
1904 switch (this->state)
1905 {
1906 case IKE_CONNECTING:
1907 {
1908 /* retry IKE_SA_INIT if we have multiple keyingtries */
1909 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1910 this->keyingtry++;
1911 if (tries == 0 || tries > this->keyingtry)
1912 {
1913 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1914 this->keyingtry + 1, tries);
1915 reset(this);
1916 return this->task_manager->initiate(this->task_manager);
1917 }
1918 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1919 break;
1920 }
1921 case IKE_DELETING:
1922 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1923 break;
1924 case IKE_REKEYING:
1925 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1926 /* FALL */
1927 default:
1928 reestablish(this);
1929 break;
1930 }
1931 return DESTROY_ME;
1932 }
1933 return SUCCESS;
1934 }
1935
1936 /**
1937 * Implementation of ike_sa_t.set_auth_lifetime.
1938 */
1939 static void set_auth_lifetime(private_ike_sa_t *this, u_int32_t lifetime)
1940 {
1941 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
1942 u_int32_t reauth_time = time(NULL) + lifetime - reduction;
1943
1944 if (lifetime < reduction)
1945 {
1946 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
1947 lifetime);
1948 charon->processor->queue_job(charon->processor,
1949 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1950 }
1951 else if (this->stats[STAT_REAUTH] == 0 ||
1952 this->stats[STAT_REAUTH] > reauth_time)
1953 {
1954 this->stats[STAT_REAUTH] = reauth_time;
1955 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1956 " in %ds", lifetime, lifetime - reduction);
1957 charon->scheduler->schedule_job(charon->scheduler,
1958 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1959 (lifetime - reduction) * 1000);
1960 }
1961 else
1962 {
1963 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, reauthentication already "
1964 "scheduled in %ds", lifetime, this->stats[STAT_REAUTH] - time(NULL));
1965 }
1966 }
1967
1968 /**
1969 * Implementation of ike_sa_t.roam.
1970 */
1971 static status_t roam(private_ike_sa_t *this, bool address)
1972 {
1973 host_t *src;
1974 ike_mobike_t *mobike;
1975
1976 switch (this->state)
1977 {
1978 case IKE_CREATED:
1979 case IKE_DELETING:
1980 case IKE_PASSIVE:
1981 return SUCCESS;
1982 default:
1983 break;
1984 }
1985 /* responder just updates the peer about changed address config */
1986 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
1987 {
1988 if (supports_extension(this, EXT_MOBIKE) && address)
1989 {
1990 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1991 mobike = ike_mobike_create(&this->public, TRUE);
1992 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1993 return this->task_manager->initiate(this->task_manager);
1994 }
1995 return SUCCESS;
1996 }
1997
1998 /* keep existing path if possible */
1999 src = charon->kernel_interface->get_source_addr(charon->kernel_interface,
2000 this->other_host, this->my_host);
2001 if (src)
2002 {
2003 if (src->ip_equals(src, this->my_host))
2004 {
2005 DBG2(DBG_IKE, "keeping connection path %H - %H",
2006 src, this->other_host);
2007 src->destroy(src);
2008 return SUCCESS;
2009 }
2010 src->destroy(src);
2011 }
2012
2013 /* update addresses with mobike, if supported ... */
2014 if (supports_extension(this, EXT_MOBIKE))
2015 {
2016 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2017 mobike = ike_mobike_create(&this->public, TRUE);
2018 mobike->roam(mobike, address);
2019 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
2020 return this->task_manager->initiate(this->task_manager);
2021 }
2022 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
2023 /* ... reauth if not */
2024 return reauth(this);
2025 }
2026
2027 /**
2028 * Implementation of ike_sa_t.inherit.
2029 */
2030 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
2031 {
2032 child_sa_t *child_sa;
2033 host_t *ip;
2034
2035 /* apply hosts and ids */
2036 this->my_host->destroy(this->my_host);
2037 this->other_host->destroy(this->other_host);
2038 this->my_id->destroy(this->my_id);
2039 this->other_id->destroy(this->other_id);
2040 this->my_host = other->my_host->clone(other->my_host);
2041 this->other_host = other->other_host->clone(other->other_host);
2042 this->my_id = other->my_id->clone(other->my_id);
2043 this->other_id = other->other_id->clone(other->other_id);
2044
2045 /* apply virtual assigned IPs... */
2046 if (other->my_virtual_ip)
2047 {
2048 this->my_virtual_ip = other->my_virtual_ip;
2049 other->my_virtual_ip = NULL;
2050 }
2051 if (other->other_virtual_ip)
2052 {
2053 this->other_virtual_ip = other->other_virtual_ip;
2054 other->other_virtual_ip = NULL;
2055 }
2056
2057 /* ... and DNS servers */
2058 while (other->dns_servers->remove_last(other->dns_servers,
2059 (void**)&ip) == SUCCESS)
2060 {
2061 this->dns_servers->insert_first(this->dns_servers, ip);
2062 }
2063
2064 /* inherit all conditions */
2065 this->conditions = other->conditions;
2066 if (this->conditions & COND_NAT_HERE)
2067 {
2068 send_keepalive(this);
2069 }
2070
2071 #ifdef ME
2072 if (other->is_mediation_server)
2073 {
2074 act_as_mediation_server(this);
2075 }
2076 else if (other->server_reflexive_host)
2077 {
2078 this->server_reflexive_host = other->server_reflexive_host->clone(
2079 other->server_reflexive_host);
2080 }
2081 #endif /* ME */
2082
2083 /* adopt all children */
2084 while (other->child_sas->remove_last(other->child_sas,
2085 (void**)&child_sa) == SUCCESS)
2086 {
2087 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
2088 }
2089
2090 /* move pending tasks to the new IKE_SA */
2091 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2092
2093 /* reauthentication timeout survives a rekeying */
2094 if (other->stats[STAT_REAUTH])
2095 {
2096 time_t reauth, delete, now = time(NULL);
2097
2098 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2099 reauth = this->stats[STAT_REAUTH] - now;
2100 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2101 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2102 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2103 "lifetime reduced to %ds", reauth, delete);
2104 charon->scheduler->schedule_job(charon->scheduler,
2105 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2106 reauth * 1000);
2107 charon->scheduler->schedule_job(charon->scheduler,
2108 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE),
2109 delete * 1000);
2110 }
2111 /* we have to initate here, there may be new tasks to handle */
2112 return this->task_manager->initiate(this->task_manager);
2113 }
2114
2115 /**
2116 * Implementation of ike_sa_t.remove_dns_server
2117 */
2118 static void remove_dns_servers(private_ike_sa_t *this)
2119 {
2120 FILE *file;
2121 struct stat stats;
2122 chunk_t contents, line, orig_line, token;
2123 char string[INET6_ADDRSTRLEN];
2124 host_t *ip;
2125 iterator_t *iterator;
2126
2127 if (this->dns_servers->get_count(this->dns_servers) == 0)
2128 {
2129 /* don't touch anything if we have no nameservers installed */
2130 return;
2131 }
2132
2133 file = fopen(RESOLV_CONF, "r");
2134 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
2135 {
2136 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2137 RESOLV_CONF, strerror(errno));
2138 return;
2139 }
2140
2141 contents = chunk_alloca((size_t)stats.st_size);
2142
2143 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
2144 {
2145 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
2146 fclose(file);
2147 return;
2148 }
2149
2150 fclose(file);
2151 file = fopen(RESOLV_CONF, "w");
2152 if (file == NULL)
2153 {
2154 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2155 RESOLV_CONF, strerror(errno));
2156 return;
2157 }
2158
2159 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
2160 while (fetchline(&contents, &line))
2161 {
2162 bool found = FALSE;
2163 orig_line = line;
2164 if (extract_token(&token, ' ', &line) &&
2165 strncasecmp(token.ptr, "nameserver", token.len) == 0)
2166 {
2167 if (!extract_token(&token, ' ', &line))
2168 {
2169 token = line;
2170 }
2171 iterator->reset(iterator);
2172 while (iterator->iterate(iterator, (void**)&ip))
2173 {
2174 snprintf(string, sizeof(string), "%H", ip);
2175 if (strlen(string) == token.len &&
2176 strncmp(token.ptr, string, token.len) == 0)
2177 {
2178 iterator->remove(iterator);
2179 ip->destroy(ip);
2180 found = TRUE;
2181 break;
2182 }
2183 }
2184 }
2185
2186 if (!found)
2187 {
2188 /* write line untouched back to file */
2189 ignore_result(fwrite(orig_line.ptr, orig_line.len, 1, file));
2190 fprintf(file, "\n");
2191 }
2192 }
2193 iterator->destroy(iterator);
2194 fclose(file);
2195 }
2196
2197 /**
2198 * Implementation of ike_sa_t.add_dns_server
2199 */
2200 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
2201 {
2202 FILE *file;
2203 struct stat stats;
2204 chunk_t contents;
2205
2206 DBG1(DBG_IKE, "installing DNS server %H", dns);
2207
2208 file = fopen(RESOLV_CONF, "a+");
2209 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
2210 {
2211 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2212 RESOLV_CONF, strerror(errno));
2213 return;
2214 }
2215
2216 contents = chunk_alloca(stats.st_size);
2217
2218 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
2219 {
2220 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
2221 fclose(file);
2222 return;
2223 }
2224
2225 fclose(file);
2226 file = fopen(RESOLV_CONF, "w");
2227 if (file == NULL)
2228 {
2229 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2230 RESOLV_CONF, strerror(errno));
2231 return;
2232 }
2233
2234 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
2235 dns, this->other_id) < 0)
2236 {
2237 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
2238 }
2239 else
2240 {
2241 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
2242 }
2243 ignore_result(fwrite(contents.ptr, contents.len, 1, file));
2244
2245 fclose(file);
2246 }
2247
2248 /**
2249 * Implementation of ike_sa_t.destroy.
2250 */
2251 static void destroy(private_ike_sa_t *this)
2252 {
2253 charon->bus->set_sa(charon->bus, &this->public);
2254
2255 set_state(this, IKE_DESTROYING);
2256
2257 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
2258
2259 /* unset SA after here to avoid usage by the listeners */
2260 charon->bus->set_sa(charon->bus, NULL);
2261
2262 this->task_manager->destroy(this->task_manager);
2263 this->keymat->destroy(this->keymat);
2264
2265 if (this->my_virtual_ip)
2266 {
2267 charon->kernel_interface->del_ip(charon->kernel_interface,
2268 this->my_virtual_ip);
2269 this->my_virtual_ip->destroy(this->my_virtual_ip);
2270 }
2271 if (this->other_virtual_ip)
2272 {
2273 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
2274 {
2275 charon->attributes->release_address(charon->attributes,
2276 this->peer_cfg->get_pool(this->peer_cfg),
2277 this->other_virtual_ip, this->other_id);
2278 }
2279 this->other_virtual_ip->destroy(this->other_virtual_ip);
2280 }
2281
2282 remove_dns_servers(this);
2283 this->dns_servers->destroy_offset(this->dns_servers,
2284 offsetof(host_t, destroy));
2285 this->additional_addresses->destroy_offset(this->additional_addresses,
2286 offsetof(host_t, destroy));
2287 #ifdef ME
2288 if (this->is_mediation_server)
2289 {
2290 charon->mediation_manager->remove(charon->mediation_manager, this->ike_sa_id);
2291 }
2292 DESTROY_IF(this->server_reflexive_host);
2293 chunk_free(&this->connect_id);
2294 #endif /* ME */
2295 free(this->nat_detection_dest.ptr);
2296
2297 DESTROY_IF(this->my_host);
2298 DESTROY_IF(this->other_host);
2299 DESTROY_IF(this->my_id);
2300 DESTROY_IF(this->other_id);
2301 DESTROY_IF(this->local_host);
2302 DESTROY_IF(this->remote_host);
2303 DESTROY_IF(this->eap_identity);
2304
2305 DESTROY_IF(this->ike_cfg);
2306 DESTROY_IF(this->peer_cfg);
2307 DESTROY_IF(this->my_auth);
2308 DESTROY_IF(this->other_auth);
2309 DESTROY_IF(this->proposal);
2310
2311 this->ike_sa_id->destroy(this->ike_sa_id);
2312 free(this);
2313 }
2314
2315 /*
2316 * Described in header.
2317 */
2318 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2319 {
2320 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2321 static u_int32_t unique_id = 0;
2322
2323 /* Public functions */
2324 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
2325 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
2326 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2327 this->public.get_statistic = (u_int32_t(*)(ike_sa_t*, statistic_t kind))get_statistic;
2328 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2329 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2330 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2331 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2332 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2333 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2334 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2335 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2336 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2337 this->public.get_my_auth = (auth_info_t*(*)(ike_sa_t*))get_my_auth;
2338 this->public.get_other_auth = (auth_info_t*(*)(ike_sa_t*))get_other_auth;
2339 this->public.get_proposal = (proposal_t*(*)(ike_sa_t*))get_proposal;
2340 this->public.set_proposal = (void(*)(ike_sa_t*, proposal_t *proposal))set_proposal;
2341 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2342 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2343 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2344 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2345 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2346 this->public.set_message_id = (void(*)(ike_sa_t*, bool inbound, u_int32_t mid))set_message_id;
2347 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2348 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2349 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2350 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2351 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2352 this->public.get_eap_identity = (identification_t* (*)(ike_sa_t*)) get_eap_identity;
2353 this->public.set_eap_identity = (void (*)(ike_sa_t*,identification_t*)) set_eap_identity;
2354 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2355 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2356 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2357 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2358 this->public.set_pending_updates = (void(*)(ike_sa_t*, u_int32_t updates))set_pending_updates;
2359 this->public.get_pending_updates = (u_int32_t(*)(ike_sa_t*))get_pending_updates;
2360 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2361 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2362 this->public.has_mapping_changed = (bool(*)(ike_sa_t*, chunk_t hash))has_mapping_changed;
2363 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2364 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2365 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2366 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2367 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2368 this->public.get_keymat = (keymat_t*(*)(ike_sa_t*))get_keymat;
2369 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2370 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2371 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2372 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2373 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2374 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2375 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2376 this->public.reauth = (status_t (*)(ike_sa_t*))reauth;
2377 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2378 this->public.set_auth_lifetime = (void(*)(ike_sa_t*, u_int32_t lifetime))set_auth_lifetime;
2379 this->public.roam = (status_t(*)(ike_sa_t*,bool))roam;
2380 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2381 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2382 this->public.reset = (void (*)(ike_sa_t*))reset;
2383 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2384 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2385 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2386 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2387 this->public.set_kmaddress = (void (*)(ike_sa_t*,host_t*,host_t*))set_kmaddress;
2388 #ifdef ME
2389 this->public.act_as_mediation_server = (void (*)(ike_sa_t*)) act_as_mediation_server;
2390 this->public.get_server_reflexive_host = (host_t* (*)(ike_sa_t*)) get_server_reflexive_host;
2391 this->public.set_server_reflexive_host = (void (*)(ike_sa_t*,host_t*)) set_server_reflexive_host;
2392 this->public.get_connect_id = (chunk_t (*)(ike_sa_t*)) get_connect_id;
2393 this->public.initiate_mediation = (status_t (*)(ike_sa_t*,peer_cfg_t*)) initiate_mediation;
2394 this->public.initiate_mediated = (status_t (*)(ike_sa_t*,host_t*,host_t*,chunk_t)) initiate_mediated;
2395 this->public.relay = (status_t (*)(ike_sa_t*,identification_t*,chunk_t,chunk_t,linked_list_t*,bool)) relay;
2396 this->public.callback = (status_t (*)(ike_sa_t*,identification_t*)) callback;
2397 this->public.respond = (status_t (*)(ike_sa_t*,identification_t*,chunk_t)) respond;
2398 #endif /* ME */
2399
2400 /* initialize private fields */
2401 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2402 this->child_sas = linked_list_create();
2403 this->my_host = host_create_any(AF_INET);
2404 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2405 this->other_host = host_create_any(AF_INET);
2406 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2407 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2408 this->eap_identity = NULL;
2409 this->extensions = 0;
2410 this->conditions = 0;
2411 this->keymat = keymat_create(ike_sa_id->is_initiator(ike_sa_id));
2412 this->state = IKE_CREATED;
2413 this->keepalive_interval = lib->settings->get_time(lib->settings,
2414 "charon.keep_alive", KEEPALIVE_INTERVAL);
2415 memset(this->stats, 0, sizeof(this->stats));
2416 this->stats[STAT_INBOUND] = this->stats[STAT_OUTBOUND] = time(NULL);
2417 this->ike_cfg = NULL;
2418 this->peer_cfg = NULL;
2419 this->my_auth = auth_info_create();
2420 this->other_auth = auth_info_create();
2421 this->proposal = NULL;
2422 this->task_manager = task_manager_create(&this->public);
2423 this->unique_id = ++unique_id;
2424 this->my_virtual_ip = NULL;
2425 this->other_virtual_ip = NULL;
2426 this->dns_servers = linked_list_create();
2427 this->additional_addresses = linked_list_create();
2428 this->nat_detection_dest = chunk_empty;
2429 this->pending_updates = 0;
2430 this->keyingtry = 0;
2431 this->local_host = NULL;
2432 this->remote_host = NULL;
2433 #ifdef ME
2434 this->is_mediation_server = FALSE;
2435 this->server_reflexive_host = NULL;
2436 this->connect_id = chunk_empty;
2437 #endif /* ME */
2438
2439 return &this->public;
2440 }