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