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