rerouting CHILD_SA if its IKE_SA gets deleted
[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 default:
499 break;
500 }
501 }
502 else
503 {
504 this->conditions &= ~condition;
505 switch (condition)
506 {
507 case COND_STALE:
508 DBG1(DBG_IKE, "new route to %H found", this->other_host);
509 break;
510 case COND_NAT_HERE:
511 case COND_NAT_THERE:
512 set_condition(this, COND_NAT_ANY,
513 has_condition(this, COND_NAT_HERE) ||
514 has_condition(this, COND_NAT_THERE));
515 break;
516 default:
517 break;
518 }
519 }
520 }
521 }
522
523 /**
524 * Implementation of ike_sa_t.send_dpd
525 */
526 static status_t send_dpd(private_ike_sa_t *this)
527 {
528 send_dpd_job_t *job;
529 time_t diff, delay;
530
531 delay = this->peer_cfg->get_dpd_delay(this->peer_cfg);
532
533 if (delay == 0)
534 {
535 /* DPD disabled */
536 return SUCCESS;
537 }
538
539 if (this->task_manager->busy(this->task_manager))
540 {
541 /* an exchange is in the air, no need to start a DPD check */
542 diff = 0;
543 }
544 else
545 {
546 /* check if there was any inbound traffic */
547 time_t last_in, now;
548 last_in = get_use_time(this, TRUE);
549 now = time(NULL);
550 diff = now - last_in;
551 if (diff >= delay)
552 {
553 /* to long ago, initiate dead peer detection */
554 task_t *task;
555
556 task = (task_t*)ike_dpd_create(TRUE);
557 diff = 0;
558 DBG1(DBG_IKE, "sending DPD request");
559
560 this->task_manager->queue_task(this->task_manager, task);
561 this->task_manager->initiate(this->task_manager);
562 }
563 }
564 /* recheck in "interval" seconds */
565 job = send_dpd_job_create(this->ike_sa_id);
566 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
567 (delay - diff) * 1000);
568 return SUCCESS;
569 }
570
571 /**
572 * Implementation of ike_sa_t.get_state.
573 */
574 static ike_sa_state_t get_state(private_ike_sa_t *this)
575 {
576 return this->state;
577 }
578
579 /**
580 * Implementation of ike_sa_t.set_state.
581 */
582 static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
583 {
584 DBG1(DBG_IKE, "IKE_SA state change: %N => %N",
585 ike_sa_state_names, this->state,
586 ike_sa_state_names, state);
587
588 switch (state)
589 {
590 case IKE_ESTABLISHED:
591 {
592 if (this->state == IKE_CONNECTING)
593 {
594 job_t *job;
595 u_int32_t now = time(NULL);
596 u_int32_t soft, hard;
597 bool reauth;
598
599 this->time.established = now;
600 /* start DPD checks */
601 send_dpd(this);
602
603 /* schedule rekeying/reauthentication */
604 soft = this->peer_cfg->get_lifetime(this->peer_cfg, TRUE);
605 hard = this->peer_cfg->get_lifetime(this->peer_cfg, FALSE);
606 reauth = this->peer_cfg->use_reauth(this->peer_cfg);
607 DBG1(DBG_IKE, "scheduling %s in %ds, maximum lifetime %ds",
608 reauth ? "reauthentication": "rekeying", soft, hard);
609
610 if (soft)
611 {
612 this->time.rekey = now + soft;
613 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, reauth);
614 charon->scheduler->schedule_job(charon->scheduler, job,
615 soft * 1000);
616 }
617
618 if (hard)
619 {
620 this->time.delete = now + hard;
621 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
622 charon->scheduler->schedule_job(charon->scheduler, job,
623 hard * 1000);
624 }
625 }
626 break;
627 }
628 case IKE_DELETING:
629 {
630 /* delete may fail if a packet gets lost, so set a timeout */
631 job_t *job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
632 charon->scheduler->schedule_job(charon->scheduler, job,
633 HALF_OPEN_IKE_SA_TIMEOUT);
634 break;
635 }
636 default:
637 break;
638 }
639
640 this->state = state;
641 }
642
643 /**
644 * Implementation of ike_sa_t.reset
645 */
646 static void reset(private_ike_sa_t *this)
647 {
648 /* the responder ID is reset, as peer may choose another one */
649 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
650 {
651 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
652 }
653
654 set_state(this, IKE_CREATED);
655
656 this->task_manager->reset(this->task_manager);
657 }
658
659 /**
660 * Implementation of ike_sa_t.set_virtual_ip
661 */
662 static void set_virtual_ip(private_ike_sa_t *this, bool local, host_t *ip)
663 {
664 if (local)
665 {
666 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
667 if (this->my_virtual_ip)
668 {
669 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
670 charon->kernel_interface->del_ip(charon->kernel_interface,
671 this->my_virtual_ip);
672 this->my_virtual_ip->destroy(this->my_virtual_ip);
673 }
674 if (charon->kernel_interface->add_ip(charon->kernel_interface, ip,
675 this->my_host) == SUCCESS)
676 {
677 this->my_virtual_ip = ip->clone(ip);
678 }
679 else
680 {
681 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
682 this->my_virtual_ip = NULL;
683 }
684 }
685 else
686 {
687 DESTROY_IF(this->other_virtual_ip);
688 this->other_virtual_ip = ip->clone(ip);
689 }
690 }
691
692 /**
693 * Implementation of ike_sa_t.get_virtual_ip
694 */
695 static host_t* get_virtual_ip(private_ike_sa_t *this, bool local)
696 {
697 if (local)
698 {
699 return this->my_virtual_ip;
700 }
701 else
702 {
703 return this->other_virtual_ip;
704 }
705 }
706
707 /**
708 * Implementation of ike_sa_t.add_additional_address.
709 */
710 static void add_additional_address(private_ike_sa_t *this, host_t *host)
711 {
712 this->additional_addresses->insert_last(this->additional_addresses, host);
713 }
714
715 /**
716 * Implementation of ike_sa_t.create_additional_address_iterator.
717 */
718 static iterator_t* create_additional_address_iterator(private_ike_sa_t *this)
719 {
720 return this->additional_addresses->create_iterator(
721 this->additional_addresses, TRUE);
722 }
723
724 /**
725 * Implementation of ike_sa_t.set_pending_updates.
726 */
727 static void set_pending_updates(private_ike_sa_t *this, u_int32_t updates)
728 {
729 this->pending_updates = updates;
730 }
731
732 /**
733 * Implementation of ike_sa_t.get_pending_updates.
734 */
735 static u_int32_t get_pending_updates(private_ike_sa_t *this)
736 {
737 return this->pending_updates;
738 }
739
740 /**
741 * Update hosts, as addresses may change (NAT)
742 */
743 static void update_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
744 {
745 bool update = FALSE;
746
747 if (supports_extension(this, EXT_MOBIKE))
748 { /* if peer speaks mobike, address updates are explicit only */
749 return;
750 }
751
752 if (me == NULL)
753 {
754 me = this->my_host;
755 }
756 if (other == NULL)
757 {
758 other = this->other_host;
759 }
760
761 /* apply hosts on first received message */
762 if (this->my_host->is_anyaddr(this->my_host) ||
763 this->other_host->is_anyaddr(this->other_host))
764 {
765 set_my_host(this, me->clone(me));
766 set_other_host(this, other->clone(other));
767 update = TRUE;
768 }
769 else
770 {
771 /* update our address in any case */
772 if (!me->equals(me, this->my_host))
773 {
774 set_my_host(this, me->clone(me));
775 update = TRUE;
776 }
777
778 if (!other->equals(other, this->other_host))
779 {
780 /* update others adress if we are NOT NATed,
781 * and allow port changes if we are NATed */
782 if (!has_condition(this, COND_NAT_HERE) ||
783 other->ip_equals(other, this->other_host))
784 {
785 set_other_host(this, other->clone(other));
786 update = TRUE;
787 }
788 }
789 }
790
791 /* update all associated CHILD_SAs, if required */
792 if (update)
793 {
794 iterator_t *iterator;
795 child_sa_t *child_sa;
796
797 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
798 while (iterator->iterate(iterator, (void**)&child_sa))
799 {
800 child_sa->update_hosts(child_sa, this->my_host, this->other_host,
801 has_condition(this, COND_NAT_ANY));
802 }
803 iterator->destroy(iterator);
804 }
805 }
806
807 /**
808 * Implementation of ike_sa_t.generate
809 */
810 static status_t generate_message(private_ike_sa_t *this, message_t *message,
811 packet_t **packet)
812 {
813 this->time.outbound = time(NULL);
814 message->set_ike_sa_id(message, this->ike_sa_id);
815 message->set_destination(message, this->other_host->clone(this->other_host));
816 message->set_source(message, this->my_host->clone(this->my_host));
817 return message->generate(message, this->crypter_out, this->signer_out, packet);
818 }
819
820 /**
821 * send a notify back to the sender
822 */
823 static void send_notify_response(private_ike_sa_t *this, message_t *request,
824 notify_type_t type)
825 {
826 message_t *response;
827 packet_t *packet;
828
829 response = message_create();
830 response->set_exchange_type(response, request->get_exchange_type(request));
831 response->set_request(response, FALSE);
832 response->set_message_id(response, request->get_message_id(request));
833 response->add_notify(response, FALSE, type, chunk_empty);
834 if (this->my_host->is_anyaddr(this->my_host))
835 {
836 this->my_host->destroy(this->my_host);
837 this->my_host = request->get_destination(request);
838 this->my_host = this->my_host->clone(this->my_host);
839 }
840 if (this->other_host->is_anyaddr(this->other_host))
841 {
842 this->other_host->destroy(this->other_host);
843 this->other_host = request->get_source(request);
844 this->other_host = this->other_host->clone(this->other_host);
845 }
846 if (generate_message(this, response, &packet) == SUCCESS)
847 {
848 charon->sender->send(charon->sender, packet);
849 }
850 response->destroy(response);
851 }
852
853 /**
854 * Implementation of ike_sa_t.initiate.
855 */
856 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
857 {
858 task_t *task;
859
860 if (this->state == IKE_CREATED)
861 {
862
863 if (this->other_host->is_anyaddr(this->other_host))
864 {
865 child_cfg->destroy(child_cfg);
866 SIG(IKE_UP_START, "initiating IKE_SA");
867 SIG(IKE_UP_FAILED, "unable to initiate to %%any");
868 return DESTROY_ME;
869 }
870
871 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
872 this->task_manager->queue_task(this->task_manager, task);
873 task = (task_t*)ike_natd_create(&this->public, TRUE);
874 this->task_manager->queue_task(this->task_manager, task);
875 task = (task_t*)ike_cert_create(&this->public, TRUE);
876 this->task_manager->queue_task(this->task_manager, task);
877 task = (task_t*)ike_auth_create(&this->public, TRUE);
878 this->task_manager->queue_task(this->task_manager, task);
879 task = (task_t*)ike_config_create(&this->public, TRUE);
880 this->task_manager->queue_task(this->task_manager, task);
881 task = (task_t*)ike_mobike_create(&this->public, TRUE);
882 this->task_manager->queue_task(this->task_manager, task);
883 }
884
885 task = (task_t*)child_create_create(&this->public, child_cfg);
886 child_cfg->destroy(child_cfg);
887 this->task_manager->queue_task(this->task_manager, task);
888
889 return this->task_manager->initiate(this->task_manager);
890 }
891
892 /**
893 * Implementation of ike_sa_t.acquire.
894 */
895 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
896 {
897 child_cfg_t *child_cfg;
898 iterator_t *iterator;
899 child_sa_t *current, *child_sa = NULL;
900 task_t *task;
901 child_create_t *child_create;
902
903 if (this->state == IKE_DELETING)
904 {
905 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
906 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
907 "IKE_SA is deleting", reqid);
908 return FAILED;
909 }
910
911 /* find CHILD_SA */
912 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
913 while (iterator->iterate(iterator, (void**)&current))
914 {
915 if (current->get_reqid(current) == reqid)
916 {
917 child_sa = current;
918 break;
919 }
920 }
921 iterator->destroy(iterator);
922 if (!child_sa)
923 {
924 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
925 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
926 "CHILD_SA not found", reqid);
927 return FAILED;
928 }
929
930
931 if (this->state == IKE_CREATED)
932 {
933 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
934 this->task_manager->queue_task(this->task_manager, task);
935 task = (task_t*)ike_natd_create(&this->public, TRUE);
936 this->task_manager->queue_task(this->task_manager, task);
937 task = (task_t*)ike_cert_create(&this->public, TRUE);
938 this->task_manager->queue_task(this->task_manager, task);
939 task = (task_t*)ike_auth_create(&this->public, TRUE);
940 this->task_manager->queue_task(this->task_manager, task);
941 task = (task_t*)ike_config_create(&this->public, TRUE);
942 this->task_manager->queue_task(this->task_manager, task);
943 task = (task_t*)ike_mobike_create(&this->public, TRUE);
944 this->task_manager->queue_task(this->task_manager, task);
945 }
946
947 child_cfg = child_sa->get_config(child_sa);
948 child_create = child_create_create(&this->public, child_cfg);
949 child_create->use_reqid(child_create, reqid);
950 this->task_manager->queue_task(this->task_manager, (task_t*)child_create);
951
952 return this->task_manager->initiate(this->task_manager);
953 }
954
955 /**
956 * Implementation of ike_sa_t.route.
957 */
958 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
959 {
960 child_sa_t *child_sa;
961 iterator_t *iterator;
962 linked_list_t *my_ts, *other_ts;
963 host_t *me, *other;
964 status_t status;
965
966 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
967
968 /* check if not already routed*/
969 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
970 while (iterator->iterate(iterator, (void**)&child_sa))
971 {
972 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
973 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
974 {
975 iterator->destroy(iterator);
976 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a config already routed");
977 return FAILED;
978 }
979 }
980 iterator->destroy(iterator);
981
982 switch (this->state)
983 {
984 case IKE_DELETING:
985 case IKE_REKEYING:
986 SIG(CHILD_ROUTE_FAILED,
987 "unable to route CHILD_SA, as its IKE_SA gets deleted");
988 return FAILED;
989 case IKE_CREATED:
990 case IKE_CONNECTING:
991 case IKE_ESTABLISHED:
992 default:
993 break;
994 }
995
996 /* install kernel policies */
997 child_sa = child_sa_create(this->my_host, this->other_host, this->my_id,
998 this->other_id, child_cfg, FALSE, 0);
999 me = this->my_host;
1000 if (this->my_virtual_ip)
1001 {
1002 me = this->my_virtual_ip;
1003 }
1004 other = this->other_host;
1005 if (this->other_virtual_ip)
1006 {
1007 other = this->other_virtual_ip;
1008 }
1009
1010 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, me);
1011 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, other);
1012 status = child_sa->add_policies(child_sa, my_ts, other_ts,
1013 child_cfg->get_mode(child_cfg));
1014 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1015 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1016 if (status == SUCCESS)
1017 {
1018 this->child_sas->insert_last(this->child_sas, child_sa);
1019 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
1020 }
1021 else
1022 {
1023 SIG(CHILD_ROUTE_FAILED, "routing CHILD_SA failed");
1024 }
1025 return status;
1026 }
1027
1028 /**
1029 * Implementation of ike_sa_t.unroute.
1030 */
1031 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
1032 {
1033 iterator_t *iterator;
1034 child_sa_t *child_sa;
1035 bool found = FALSE;
1036
1037 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
1038
1039 /* find CHILD_SA in ROUTED state */
1040 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1041 while (iterator->iterate(iterator, (void**)&child_sa))
1042 {
1043 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1044 child_sa->get_reqid(child_sa) == reqid)
1045 {
1046 iterator->remove(iterator);
1047 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
1048 child_sa->destroy(child_sa);
1049 found = TRUE;
1050 break;
1051 }
1052 }
1053 iterator->destroy(iterator);
1054
1055 if (!found)
1056 {
1057 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
1058 return FAILED;
1059 }
1060 /* if we are not established, and we have no more routed childs, remove whole SA */
1061 if (this->state == IKE_CREATED &&
1062 this->child_sas->get_count(this->child_sas) == 0)
1063 {
1064 return DESTROY_ME;
1065 }
1066 return SUCCESS;
1067 }
1068 /**
1069 * Implementation of ike_sa_t.process_message.
1070 */
1071 static status_t process_message(private_ike_sa_t *this, message_t *message)
1072 {
1073 status_t status;
1074 bool is_request;
1075
1076 is_request = message->get_request(message);
1077
1078 status = message->parse_body(message, this->crypter_in, this->signer_in);
1079 if (status != SUCCESS)
1080 {
1081
1082 if (is_request)
1083 {
1084 switch (status)
1085 {
1086 case NOT_SUPPORTED:
1087 DBG1(DBG_IKE, "ciritcal unknown payloads found");
1088 if (is_request)
1089 {
1090 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
1091 }
1092 break;
1093 case PARSE_ERROR:
1094 DBG1(DBG_IKE, "message parsing failed");
1095 if (is_request)
1096 {
1097 send_notify_response(this, message, INVALID_SYNTAX);
1098 }
1099 break;
1100 case VERIFY_ERROR:
1101 DBG1(DBG_IKE, "message verification failed");
1102 if (is_request)
1103 {
1104 send_notify_response(this, message, INVALID_SYNTAX);
1105 }
1106 break;
1107 case FAILED:
1108 DBG1(DBG_IKE, "integrity check failed");
1109 /* ignored */
1110 break;
1111 case INVALID_STATE:
1112 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1113 if (is_request)
1114 {
1115 send_notify_response(this, message, INVALID_SYNTAX);
1116 }
1117 default:
1118 break;
1119 }
1120 }
1121 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1122 exchange_type_names, message->get_exchange_type(message),
1123 message->get_request(message) ? "request" : "response",
1124 message->get_message_id(message));
1125 return status;
1126 }
1127 else
1128 {
1129 host_t *me, *other;
1130 private_ike_sa_t *new;
1131 iterator_t *iterator;
1132 child_sa_t *child;
1133 bool has_routed = FALSE;
1134
1135 me = message->get_destination(message);
1136 other = message->get_source(message);
1137
1138 /* if this IKE_SA is virgin, we check for a config */
1139 if (this->ike_cfg == NULL)
1140 {
1141 job_t *job;
1142 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1143 me, other);
1144 if (this->ike_cfg == NULL)
1145 {
1146 /* no config found for these hosts, destroy */
1147 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1148 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1149 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
1150 return DESTROY_ME;
1151 }
1152 /* add a timeout if peer does not establish it completely */
1153 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
1154 charon->scheduler->schedule_job(charon->scheduler, job,
1155 HALF_OPEN_IKE_SA_TIMEOUT);
1156 }
1157
1158 /* check if message is trustworthy, and update host information */
1159 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
1160 message->get_exchange_type(message) != IKE_SA_INIT)
1161 {
1162 update_hosts(this, me, other);
1163 this->time.inbound = time(NULL);
1164 }
1165 status = this->task_manager->process_message(this->task_manager, message);
1166 if (status != DESTROY_ME)
1167 {
1168 return status;
1169 }
1170 /* if IKE_SA gets closed for any reasons, reroute routed children */
1171 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1172 while (iterator->iterate(iterator, (void**)&child))
1173 {
1174 if (child->get_state(child) == CHILD_ROUTED)
1175 {
1176 has_routed = TRUE;
1177 break;
1178 }
1179 }
1180 iterator->destroy(iterator);
1181 if (!has_routed)
1182 {
1183 return status;
1184 }
1185 /* move routed children to a new IKE_SA, apply connection info */
1186 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1187 charon->ike_sa_manager, TRUE);
1188 set_peer_cfg(new, this->peer_cfg);
1189 new->other_host->destroy(new->other_host);
1190 new->other_host = this->other_host->clone(this->other_host);
1191 if (!has_condition(this, COND_NAT_THERE))
1192 {
1193 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1194 }
1195 if (this->my_virtual_ip)
1196 {
1197 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1198 }
1199 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1200 while (iterator->iterate(iterator, (void**)&child))
1201 {
1202 if (child->get_state(child) == CHILD_ROUTED)
1203 {
1204 route(new, child->get_config(child));
1205 }
1206 }
1207 iterator->destroy(iterator);
1208 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1209 return status;
1210 }
1211 }
1212
1213 /**
1214 * Implementation of ike_sa_t.retransmit.
1215 */
1216 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
1217 {
1218 this->time.outbound = time(NULL);
1219 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1220 {
1221 child_cfg_t *child_cfg;
1222 child_sa_t* child_sa;
1223 linked_list_t *to_route, *to_restart;
1224 iterator_t *iterator;
1225
1226 /* send a proper signal to brief interested bus listeners */
1227 switch (this->state)
1228 {
1229 case IKE_CONNECTING:
1230 {
1231 /* retry IKE_SA_INIT if we have multiple keyingtries */
1232 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1233 this->keyingtry++;
1234 if (tries == 0 || tries > this->keyingtry)
1235 {
1236 SIG(IKE_UP_FAILED, "peer not responding, trying again "
1237 "(%d/%d) in background ", this->keyingtry + 1, tries);
1238 reset(this);
1239 return this->task_manager->initiate(this->task_manager);
1240 }
1241 SIG(IKE_UP_FAILED, "establishing IKE_SA failed, peer not responding");
1242 break;
1243 }
1244 case IKE_REKEYING:
1245 SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
1246 break;
1247 case IKE_DELETING:
1248 SIG(IKE_DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
1249 break;
1250 default:
1251 break;
1252 }
1253
1254 /* summarize how we have to handle each child */
1255 to_route = linked_list_create();
1256 to_restart = linked_list_create();
1257 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1258 while (iterator->iterate(iterator, (void**)&child_sa))
1259 {
1260 child_cfg = child_sa->get_config(child_sa);
1261
1262 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1263 {
1264 /* reroute routed CHILD_SAs */
1265 to_route->insert_last(to_route, child_cfg);
1266 }
1267 else
1268 {
1269 /* use DPD action for established CHILD_SAs */
1270 switch (this->peer_cfg->get_dpd_action(this->peer_cfg))
1271 {
1272 case DPD_ROUTE:
1273 to_route->insert_last(to_route, child_cfg);
1274 break;
1275 case DPD_RESTART:
1276 to_restart->insert_last(to_restart, child_cfg);
1277 break;
1278 default:
1279 break;
1280 }
1281 }
1282 }
1283 iterator->destroy(iterator);
1284
1285 /* create a new IKE_SA if we have to route or to restart */
1286 if (to_route->get_count(to_route) || to_restart->get_count(to_restart))
1287 {
1288 private_ike_sa_t *new;
1289 task_t *task;
1290
1291 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1292 charon->ike_sa_manager, TRUE);
1293
1294 set_peer_cfg(new, this->peer_cfg);
1295 /* use actual used host, not the wildcarded one in config */
1296 new->other_host->destroy(new->other_host);
1297 new->other_host = this->other_host->clone(this->other_host);
1298 /* reset port to 500, but only if peer is not NATed */
1299 if (!has_condition(this, COND_NAT_THERE))
1300 {
1301 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1302 }
1303 /* take over virtual ip, as we need it for a proper route */
1304 if (this->my_virtual_ip)
1305 {
1306 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1307 }
1308
1309 /* install routes */
1310 while (to_route->remove_last(to_route, (void**)&child_cfg) == SUCCESS)
1311 {
1312 route(new, child_cfg);
1313 }
1314
1315 /* restart children */
1316 if (to_restart->get_count(to_restart))
1317 {
1318 task = (task_t*)ike_init_create(&new->public, TRUE, NULL);
1319 new->task_manager->queue_task(new->task_manager, task);
1320 task = (task_t*)ike_natd_create(&new->public, TRUE);
1321 new->task_manager->queue_task(new->task_manager, task);
1322 task = (task_t*)ike_cert_create(&new->public, TRUE);
1323 new->task_manager->queue_task(new->task_manager, task);
1324 task = (task_t*)ike_config_create(&new->public, TRUE);
1325 new->task_manager->queue_task(new->task_manager, task);
1326 task = (task_t*)ike_auth_create(&new->public, TRUE);
1327 new->task_manager->queue_task(new->task_manager, task);
1328
1329 while (to_restart->remove_last(to_restart, (void**)&child_cfg) == SUCCESS)
1330 {
1331 task = (task_t*)child_create_create(&new->public, child_cfg);
1332 new->task_manager->queue_task(new->task_manager, task);
1333 }
1334 task = (task_t*)ike_mobike_create(&new->public, TRUE);
1335 new->task_manager->queue_task(new->task_manager, task);
1336 new->task_manager->initiate(new->task_manager);
1337 }
1338 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1339 }
1340 to_route->destroy(to_route);
1341 to_restart->destroy(to_restart);
1342 return DESTROY_ME;
1343 }
1344 return SUCCESS;
1345 }
1346
1347 /**
1348 * Implementation of ike_sa_t.get_prf.
1349 */
1350 static prf_t *get_prf(private_ike_sa_t *this)
1351 {
1352 return this->prf;
1353 }
1354
1355 /**
1356 * Implementation of ike_sa_t.get_prf.
1357 */
1358 static prf_t *get_child_prf(private_ike_sa_t *this)
1359 {
1360 return this->child_prf;
1361 }
1362
1363 /**
1364 * Implementation of ike_sa_t.get_skp_bild
1365 */
1366 static chunk_t get_skp_build(private_ike_sa_t *this)
1367 {
1368 return this->skp_build;
1369 }
1370
1371 /**
1372 * Implementation of ike_sa_t.get_skp_verify
1373 */
1374 static chunk_t get_skp_verify(private_ike_sa_t *this)
1375 {
1376 return this->skp_verify;
1377 }
1378
1379 /**
1380 * Implementation of ike_sa_t.get_id.
1381 */
1382 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1383 {
1384 return this->ike_sa_id;
1385 }
1386
1387 /**
1388 * Implementation of ike_sa_t.get_my_id.
1389 */
1390 static identification_t* get_my_id(private_ike_sa_t *this)
1391 {
1392 return this->my_id;
1393 }
1394
1395 /**
1396 * Implementation of ike_sa_t.set_my_id.
1397 */
1398 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1399 {
1400 DESTROY_IF(this->my_id);
1401 this->my_id = me;
1402 }
1403
1404 /**
1405 * Implementation of ike_sa_t.get_other_id.
1406 */
1407 static identification_t* get_other_id(private_ike_sa_t *this)
1408 {
1409 return this->other_id;
1410 }
1411
1412 /**
1413 * Implementation of ike_sa_t.set_other_id.
1414 */
1415 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1416 {
1417 DESTROY_IF(this->other_id);
1418 this->other_id = other;
1419 }
1420
1421 /**
1422 * Implementation of ike_sa_t.get_other_ca.
1423 */
1424 static ca_info_t* get_other_ca(private_ike_sa_t *this)
1425 {
1426 return this->other_ca;
1427 }
1428
1429 /**
1430 * Implementation of ike_sa_t.set_other_ca.
1431 */
1432 static void set_other_ca(private_ike_sa_t *this, ca_info_t *other_ca)
1433 {
1434 this->other_ca = other_ca;
1435 }
1436
1437 /**
1438 * Implementation of ike_sa_t.derive_keys.
1439 */
1440 static status_t derive_keys(private_ike_sa_t *this,
1441 proposal_t *proposal, chunk_t secret,
1442 chunk_t nonce_i, chunk_t nonce_r,
1443 bool initiator, prf_t *child_prf, prf_t *old_prf)
1444 {
1445 prf_plus_t *prf_plus;
1446 chunk_t skeyseed, key, nonces, prf_plus_seed;
1447 algorithm_t *algo;
1448 size_t key_size;
1449 crypter_t *crypter_i, *crypter_r;
1450 signer_t *signer_i, *signer_r;
1451 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1452 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1453 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1454
1455 /* Create SAs general purpose PRF first, we may use it here */
1456 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1457 {
1458 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1459 return FAILED;
1460 }
1461 this->prf = prf_create(algo->algorithm);
1462 if (this->prf == NULL)
1463 {
1464 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1465 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1466 return FAILED;
1467 }
1468
1469 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1470 nonces = chunk_cat("cc", nonce_i, nonce_r);
1471 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1472 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1473 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1474
1475 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1476 *
1477 * if we are rekeying, SKEYSEED is built on another way
1478 */
1479 if (child_prf == NULL) /* not rekeying */
1480 {
1481 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1482 this->prf->set_key(this->prf, nonces);
1483 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1484 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1485 this->prf->set_key(this->prf, skeyseed);
1486 chunk_free(&skeyseed);
1487 chunk_free(&secret);
1488 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1489 }
1490 else
1491 {
1492 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1493 * use OLD SAs PRF functions for both prf_plus and prf */
1494 secret = chunk_cat("mc", secret, nonces);
1495 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1496 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1497 old_prf->set_key(old_prf, skeyseed);
1498 chunk_free(&skeyseed);
1499 chunk_free(&secret);
1500 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1501 }
1502 chunk_free(&nonces);
1503 chunk_free(&prf_plus_seed);
1504
1505 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1506
1507 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1508 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1509 this->child_prf = prf_create(algo->algorithm);
1510 key_size = this->child_prf->get_key_size(this->child_prf);
1511 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1512 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1513 this->child_prf->set_key(this->child_prf, key);
1514 chunk_free(&key);
1515
1516 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1517 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1518 {
1519 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1520 return FAILED;
1521 }
1522 signer_i = signer_create(algo->algorithm);
1523 signer_r = signer_create(algo->algorithm);
1524 if (signer_i == NULL || signer_r == NULL)
1525 {
1526 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1527 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1528 return FAILED;
1529 }
1530 key_size = signer_i->get_key_size(signer_i);
1531
1532 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1533 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1534 signer_i->set_key(signer_i, key);
1535 chunk_free(&key);
1536
1537 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1538 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1539 signer_r->set_key(signer_r, key);
1540 chunk_free(&key);
1541
1542 if (initiator)
1543 {
1544 this->signer_in = signer_r;
1545 this->signer_out = signer_i;
1546 }
1547 else
1548 {
1549 this->signer_in = signer_i;
1550 this->signer_out = signer_r;
1551 }
1552
1553 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1554 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1555 {
1556 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1557 return FAILED;
1558 }
1559 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1560 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1561 if (crypter_i == NULL || crypter_r == NULL)
1562 {
1563 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1564 "%N (key size %d) not supported!",
1565 encryption_algorithm_names, algo->algorithm, algo->key_size);
1566 return FAILED;
1567 }
1568 key_size = crypter_i->get_key_size(crypter_i);
1569
1570 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1571 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1572 crypter_i->set_key(crypter_i, key);
1573 chunk_free(&key);
1574
1575 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1576 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1577 crypter_r->set_key(crypter_r, key);
1578 chunk_free(&key);
1579
1580 if (initiator)
1581 {
1582 this->crypter_in = crypter_r;
1583 this->crypter_out = crypter_i;
1584 }
1585 else
1586 {
1587 this->crypter_in = crypter_i;
1588 this->crypter_out = crypter_r;
1589 }
1590
1591 /* SK_pi/SK_pr used for authentication => stored for later */
1592 key_size = this->prf->get_key_size(this->prf);
1593 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1594 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1595 if (initiator)
1596 {
1597 this->skp_build = key;
1598 }
1599 else
1600 {
1601 this->skp_verify = key;
1602 }
1603 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1604 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1605 if (initiator)
1606 {
1607 this->skp_verify = key;
1608 }
1609 else
1610 {
1611 this->skp_build = key;
1612 }
1613
1614 /* all done, prf_plus not needed anymore */
1615 prf_plus->destroy(prf_plus);
1616
1617 return SUCCESS;
1618 }
1619
1620 /**
1621 * Implementation of ike_sa_t.add_child_sa.
1622 */
1623 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1624 {
1625 this->child_sas->insert_last(this->child_sas, child_sa);
1626 }
1627
1628 /**
1629 * Implementation of ike_sa_t.get_child_sa.
1630 */
1631 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1632 u_int32_t spi, bool inbound)
1633 {
1634 iterator_t *iterator;
1635 child_sa_t *current, *found = NULL;
1636
1637 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1638 while (iterator->iterate(iterator, (void**)&current))
1639 {
1640 if (current->get_spi(current, inbound) == spi &&
1641 current->get_protocol(current) == protocol)
1642 {
1643 found = current;
1644 }
1645 }
1646 iterator->destroy(iterator);
1647 return found;
1648 }
1649
1650 /**
1651 * Implementation of ike_sa_t.create_child_sa_iterator.
1652 */
1653 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1654 {
1655 return this->child_sas->create_iterator(this->child_sas, TRUE);
1656 }
1657
1658 /**
1659 * Implementation of ike_sa_t.rekey_child_sa.
1660 */
1661 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1662 {
1663 child_sa_t *child_sa;
1664 child_rekey_t *child_rekey;
1665
1666 child_sa = get_child_sa(this, protocol, spi, TRUE);
1667 if (child_sa)
1668 {
1669 child_rekey = child_rekey_create(&this->public, child_sa);
1670 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1671 return this->task_manager->initiate(this->task_manager);
1672 }
1673 return FAILED;
1674 }
1675
1676 /**
1677 * Implementation of ike_sa_t.delete_child_sa.
1678 */
1679 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1680 {
1681 child_sa_t *child_sa;
1682 child_delete_t *child_delete;
1683
1684 child_sa = get_child_sa(this, protocol, spi, TRUE);
1685 if (child_sa)
1686 {
1687 child_delete = child_delete_create(&this->public, child_sa);
1688 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1689 return this->task_manager->initiate(this->task_manager);
1690 }
1691 return FAILED;
1692 }
1693
1694 /**
1695 * Implementation of ike_sa_t.destroy_child_sa.
1696 */
1697 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1698 u_int32_t spi)
1699 {
1700 iterator_t *iterator;
1701 child_sa_t *child_sa;
1702 status_t status = NOT_FOUND;
1703
1704 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1705 while (iterator->iterate(iterator, (void**)&child_sa))
1706 {
1707 if (child_sa->get_protocol(child_sa) == protocol &&
1708 child_sa->get_spi(child_sa, TRUE) == spi)
1709 {
1710 child_sa->destroy(child_sa);
1711 iterator->remove(iterator);
1712 status = SUCCESS;
1713 break;
1714 }
1715 }
1716 iterator->destroy(iterator);
1717 return status;
1718 }
1719
1720 /**
1721 * Implementation of public_ike_sa_t.delete.
1722 */
1723 static status_t delete_(private_ike_sa_t *this)
1724 {
1725 ike_delete_t *ike_delete;
1726
1727 switch (this->state)
1728 {
1729 case IKE_ESTABLISHED:
1730 case IKE_REKEYING:
1731 ike_delete = ike_delete_create(&this->public, TRUE);
1732 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1733 return this->task_manager->initiate(this->task_manager);
1734 case IKE_CREATED:
1735 SIG(IKE_DOWN_SUCCESS, "deleting unestablished IKE_SA");
1736 break;
1737 default:
1738 SIG(IKE_DOWN_SUCCESS, "destroying IKE_SA in state %N "
1739 "without notification", ike_sa_state_names, this->state);
1740 break;
1741 }
1742 return DESTROY_ME;
1743 }
1744
1745 /**
1746 * Implementation of ike_sa_t.rekey.
1747 */
1748 static status_t rekey(private_ike_sa_t *this)
1749 {
1750 ike_rekey_t *ike_rekey;
1751
1752 ike_rekey = ike_rekey_create(&this->public, TRUE);
1753
1754 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1755 return this->task_manager->initiate(this->task_manager);
1756 }
1757
1758 /**
1759 * Implementation of ike_sa_t.reestablish
1760 */
1761 static status_t reestablish(private_ike_sa_t *this)
1762 {
1763 task_t *task;
1764
1765 task = (task_t*)ike_reauth_create(&this->public);
1766 this->task_manager->queue_task(this->task_manager, task);
1767
1768 return this->task_manager->initiate(this->task_manager);
1769 }
1770
1771 /**
1772 * Implementation of ike_sa_t.roam.
1773 */
1774 static status_t roam(private_ike_sa_t *this, bool address)
1775 {
1776 host_t *me, *other;
1777 ike_mobike_t *mobike;
1778
1779 /* responder just updates the peer about changed address config */
1780 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
1781 {
1782 if (supports_extension(this, EXT_MOBIKE) && address)
1783 {
1784 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1785 mobike = ike_mobike_create(&this->public, TRUE);
1786 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1787 return this->task_manager->initiate(this->task_manager);
1788 }
1789 return SUCCESS;
1790 }
1791
1792 /* get best address pair to use */
1793 other = this->other_host;
1794 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
1795 other);
1796
1797 /* TODO: find a better path using additional addresses of peer */
1798
1799 if (!me)
1800 {
1801 /* no route found to host, set to stale, wait for a new route */
1802 set_condition(this, COND_STALE, TRUE);
1803 return FAILED;
1804 }
1805
1806 set_condition(this, COND_STALE, FALSE);
1807 if (me->ip_equals(me, this->my_host) &&
1808 other->ip_equals(other, this->other_host))
1809 {
1810 DBG2(DBG_IKE, "%H still reached through %H, no update needed",
1811 this->other_host, me);
1812 me->destroy(me);
1813 return SUCCESS;
1814 }
1815 me->set_port(me, this->my_host->get_port(this->my_host));
1816 other = other->clone(other);
1817 other->set_port(other, this->other_host->get_port(this->other_host));
1818 set_my_host(this, me);
1819 set_other_host(this, other);
1820
1821 /* update addresses with mobike, if supported ... */
1822 if (supports_extension(this, EXT_MOBIKE))
1823 {
1824 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1825 mobike = ike_mobike_create(&this->public, TRUE);
1826 mobike->roam(mobike, address);
1827 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1828 return this->task_manager->initiate(this->task_manager);
1829 }
1830 DBG1(DBG_IKE, "reestablishing IKE_SA due address change");
1831 /* ... reestablish if not */
1832 return reestablish(this);
1833 }
1834
1835 /**
1836 * Implementation of ike_sa_t.inherit.
1837 */
1838 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
1839 {
1840 child_sa_t *child_sa;
1841 host_t *ip;
1842
1843 /* apply hosts and ids */
1844 this->my_host->destroy(this->my_host);
1845 this->other_host->destroy(this->other_host);
1846 this->my_id->destroy(this->my_id);
1847 this->other_id->destroy(this->other_id);
1848 this->my_host = other->my_host->clone(other->my_host);
1849 this->other_host = other->other_host->clone(other->other_host);
1850 this->my_id = other->my_id->clone(other->my_id);
1851 this->other_id = other->other_id->clone(other->other_id);
1852
1853 /* apply virtual assigned IPs... */
1854 if (other->my_virtual_ip)
1855 {
1856 this->my_virtual_ip = other->my_virtual_ip;
1857 other->my_virtual_ip = NULL;
1858 }
1859 if (other->other_virtual_ip)
1860 {
1861 this->other_virtual_ip = other->other_virtual_ip;
1862 other->other_virtual_ip = NULL;
1863 }
1864
1865 /* ... and DNS servers */
1866 while (other->dns_servers->remove_last(other->dns_servers,
1867 (void**)&ip) == SUCCESS)
1868 {
1869 this->dns_servers->insert_first(this->dns_servers, ip);
1870 }
1871
1872 /* adopt all children */
1873 while (other->child_sas->remove_last(other->child_sas,
1874 (void**)&child_sa) == SUCCESS)
1875 {
1876 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1877 }
1878
1879 /* move pending tasks to the new IKE_SA */
1880 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1881
1882 /* we have to initate here, there may be new tasks to handle */
1883 return this->task_manager->initiate(this->task_manager);
1884 }
1885
1886 /**
1887 * Implementation of ike_sa_t.remove_dns_server
1888 */
1889 static void remove_dns_servers(private_ike_sa_t *this)
1890 {
1891 FILE *file;
1892 struct stat stats;
1893 chunk_t contents, line, orig_line, token;
1894 char string[INET6_ADDRSTRLEN];
1895 host_t *ip;
1896 iterator_t *iterator;
1897
1898 if (this->dns_servers->get_count(this->dns_servers) == 0)
1899 {
1900 /* don't touch anything if we have no nameservers installed */
1901 return;
1902 }
1903
1904 file = fopen(RESOLV_CONF, "r");
1905 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1906 {
1907 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1908 RESOLV_CONF, strerror(errno));
1909 return;
1910 }
1911
1912 contents = chunk_alloca((size_t)stats.st_size);
1913
1914 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1915 {
1916 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1917 fclose(file);
1918 return;
1919 }
1920
1921 fclose(file);
1922 file = fopen(RESOLV_CONF, "w");
1923 if (file == NULL)
1924 {
1925 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1926 RESOLV_CONF, strerror(errno));
1927 return;
1928 }
1929
1930 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
1931 while (fetchline(&contents, &line))
1932 {
1933 bool found = FALSE;
1934 orig_line = line;
1935 if (extract_token(&token, ' ', &line) &&
1936 strncasecmp(token.ptr, "nameserver", token.len) == 0)
1937 {
1938 if (!extract_token(&token, ' ', &line))
1939 {
1940 token = line;
1941 }
1942 iterator->reset(iterator);
1943 while (iterator->iterate(iterator, (void**)&ip))
1944 {
1945 snprintf(string, sizeof(string), "%H", ip);
1946 if (strlen(string) == token.len &&
1947 strncmp(token.ptr, string, token.len) == 0)
1948 {
1949 iterator->remove(iterator);
1950 ip->destroy(ip);
1951 found = TRUE;
1952 break;
1953 }
1954 }
1955 }
1956
1957 if (!found)
1958 {
1959 /* write line untouched back to file */
1960 fwrite(orig_line.ptr, orig_line.len, 1, file);
1961 fprintf(file, "\n");
1962 }
1963 }
1964 iterator->destroy(iterator);
1965 fclose(file);
1966 }
1967
1968 /**
1969 * Implementation of ike_sa_t.add_dns_server
1970 */
1971 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
1972 {
1973 FILE *file;
1974 struct stat stats;
1975 chunk_t contents;
1976
1977 DBG1(DBG_IKE, "installing DNS server %H", dns);
1978
1979 file = fopen(RESOLV_CONF, "a+");
1980 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1981 {
1982 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1983 RESOLV_CONF, strerror(errno));
1984 return;
1985 }
1986
1987 contents = chunk_alloca(stats.st_size);
1988
1989 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1990 {
1991 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1992 fclose(file);
1993 return;
1994 }
1995
1996 fclose(file);
1997 file = fopen(RESOLV_CONF, "w");
1998 if (file == NULL)
1999 {
2000 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2001 RESOLV_CONF, strerror(errno));
2002 return;
2003 }
2004
2005 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
2006 dns, this->other_id) < 0)
2007 {
2008 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
2009 }
2010 else
2011 {
2012 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
2013 }
2014 fwrite(contents.ptr, contents.len, 1, file);
2015
2016 fclose(file);
2017 }
2018
2019 /**
2020 * Implementation of ike_sa_t.destroy.
2021 */
2022 static void destroy(private_ike_sa_t *this)
2023 {
2024 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
2025
2026 DESTROY_IF(this->crypter_in);
2027 DESTROY_IF(this->crypter_out);
2028 DESTROY_IF(this->signer_in);
2029 DESTROY_IF(this->signer_out);
2030 DESTROY_IF(this->prf);
2031 DESTROY_IF(this->child_prf);
2032 chunk_free(&this->skp_verify);
2033 chunk_free(&this->skp_build);
2034
2035 if (this->my_virtual_ip)
2036 {
2037 charon->kernel_interface->del_ip(charon->kernel_interface,
2038 this->my_virtual_ip);
2039 this->my_virtual_ip->destroy(this->my_virtual_ip);
2040 }
2041 DESTROY_IF(this->other_virtual_ip);
2042
2043 remove_dns_servers(this);
2044 this->dns_servers->destroy_offset(this->dns_servers,
2045 offsetof(host_t, destroy));
2046 this->additional_addresses->destroy_offset(this->additional_addresses,
2047 offsetof(host_t, destroy));
2048
2049 DESTROY_IF(this->my_host);
2050 DESTROY_IF(this->other_host);
2051 DESTROY_IF(this->my_id);
2052 DESTROY_IF(this->other_id);
2053
2054 DESTROY_IF(this->ike_cfg);
2055 DESTROY_IF(this->peer_cfg);
2056
2057 this->ike_sa_id->destroy(this->ike_sa_id);
2058 this->task_manager->destroy(this->task_manager);
2059 free(this);
2060 }
2061
2062 /*
2063 * Described in header.
2064 */
2065 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2066 {
2067 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2068 static u_int32_t unique_id = 0;
2069
2070 /* Public functions */
2071 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
2072 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
2073 this->public.get_stats = (void (*)(ike_sa_t*,u_int32_t*))get_stats;
2074 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2075 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2076 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2077 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2078 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2079 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2080 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2081 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2082 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2083 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2084 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2085 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2086 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2087 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2088 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2089 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2090 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2091 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2092 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2093 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2094 this->public.get_other_ca = (ca_info_t* (*)(ike_sa_t*)) get_other_ca;
2095 this->public.set_other_ca = (void (*)(ike_sa_t*,ca_info_t*)) set_other_ca;
2096 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2097 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2098 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2099 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2100 this->public.set_pending_updates = (void(*)(ike_sa_t*, u_int32_t updates))set_pending_updates;
2101 this->public.get_pending_updates = (u_int32_t(*)(ike_sa_t*))get_pending_updates;
2102 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2103 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2104 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2105 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2106 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2107 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2108 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2109 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
2110 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
2111 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
2112 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
2113 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2114 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2115 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2116 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2117 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2118 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2119 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2120 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2121 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2122 this->public.roam = (status_t(*)(ike_sa_t*,bool))roam;
2123 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2124 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2125 this->public.reset = (void (*)(ike_sa_t*))reset;
2126 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2127 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2128 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2129 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2130
2131 /* initialize private fields */
2132 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2133 this->child_sas = linked_list_create();
2134 this->my_host = host_create_any(AF_INET);
2135 this->other_host = host_create_any(AF_INET);
2136 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2137 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2138 this->other_ca = NULL;
2139 this->extensions = 0;
2140 this->conditions = 0;
2141 this->crypter_in = NULL;
2142 this->crypter_out = NULL;
2143 this->signer_in = NULL;
2144 this->signer_out = NULL;
2145 this->prf = NULL;
2146 this->skp_verify = chunk_empty;
2147 this->skp_build = chunk_empty;
2148 this->child_prf = NULL;
2149 this->state = IKE_CREATED;
2150 this->time.inbound = this->time.outbound = time(NULL);
2151 this->time.established = 0;
2152 this->time.rekey = 0;
2153 this->time.delete = 0;
2154 this->ike_cfg = NULL;
2155 this->peer_cfg = NULL;
2156 this->task_manager = task_manager_create(&this->public);
2157 this->unique_id = ++unique_id;
2158 this->my_virtual_ip = NULL;
2159 this->other_virtual_ip = NULL;
2160 this->dns_servers = linked_list_create();
2161 this->additional_addresses = linked_list_create();
2162 this->pending_updates = 0;
2163 this->keyingtry = 0;
2164
2165 return &this->public;
2166 }