0a996329d59fd3c2e2c6553521e2a0027a31d8eb
[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.process_message.
855 */
856 static status_t process_message(private_ike_sa_t *this, message_t *message)
857 {
858 status_t status;
859 bool is_request;
860
861 is_request = message->get_request(message);
862
863 status = message->parse_body(message, this->crypter_in, this->signer_in);
864 if (status != SUCCESS)
865 {
866
867 if (is_request)
868 {
869 switch (status)
870 {
871 case NOT_SUPPORTED:
872 DBG1(DBG_IKE, "ciritcal unknown payloads found");
873 if (is_request)
874 {
875 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
876 }
877 break;
878 case PARSE_ERROR:
879 DBG1(DBG_IKE, "message parsing failed");
880 if (is_request)
881 {
882 send_notify_response(this, message, INVALID_SYNTAX);
883 }
884 break;
885 case VERIFY_ERROR:
886 DBG1(DBG_IKE, "message verification failed");
887 if (is_request)
888 {
889 send_notify_response(this, message, INVALID_SYNTAX);
890 }
891 break;
892 case FAILED:
893 DBG1(DBG_IKE, "integrity check failed");
894 /* ignored */
895 break;
896 case INVALID_STATE:
897 DBG1(DBG_IKE, "found encrypted message, but no keys available");
898 if (is_request)
899 {
900 send_notify_response(this, message, INVALID_SYNTAX);
901 }
902 default:
903 break;
904 }
905 }
906 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
907 exchange_type_names, message->get_exchange_type(message),
908 message->get_request(message) ? "request" : "response",
909 message->get_message_id(message));
910 return status;
911 }
912 else
913 {
914 host_t *me, *other;
915
916 me = message->get_destination(message);
917 other = message->get_source(message);
918
919 /* if this IKE_SA is virgin, we check for a config */
920 if (this->ike_cfg == NULL)
921 {
922 job_t *job;
923 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
924 me, other);
925 if (this->ike_cfg == NULL)
926 {
927 /* no config found for these hosts, destroy */
928 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
929 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
930 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
931 return DESTROY_ME;
932 }
933 /* add a timeout if peer does not establish it completely */
934 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
935 charon->scheduler->schedule_job(charon->scheduler, job,
936 HALF_OPEN_IKE_SA_TIMEOUT);
937 }
938
939 /* check if message is trustworthy, and update host information */
940 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
941 message->get_exchange_type(message) != IKE_SA_INIT)
942 {
943 update_hosts(this, me, other);
944 this->time.inbound = time(NULL);
945 }
946 return this->task_manager->process_message(this->task_manager, message);
947 }
948 }
949
950 /**
951 * Implementation of ike_sa_t.initiate.
952 */
953 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
954 {
955 task_t *task;
956
957 if (this->state == IKE_CREATED)
958 {
959
960 if (this->other_host->is_anyaddr(this->other_host))
961 {
962 child_cfg->destroy(child_cfg);
963 SIG(IKE_UP_START, "initiating IKE_SA");
964 SIG(IKE_UP_FAILED, "unable to initiate to %%any");
965 return DESTROY_ME;
966 }
967
968 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
969 this->task_manager->queue_task(this->task_manager, task);
970 task = (task_t*)ike_natd_create(&this->public, TRUE);
971 this->task_manager->queue_task(this->task_manager, task);
972 task = (task_t*)ike_cert_create(&this->public, TRUE);
973 this->task_manager->queue_task(this->task_manager, task);
974 task = (task_t*)ike_auth_create(&this->public, TRUE);
975 this->task_manager->queue_task(this->task_manager, task);
976 task = (task_t*)ike_config_create(&this->public, TRUE);
977 this->task_manager->queue_task(this->task_manager, task);
978 task = (task_t*)ike_mobike_create(&this->public, TRUE);
979 this->task_manager->queue_task(this->task_manager, task);
980 }
981
982 task = (task_t*)child_create_create(&this->public, child_cfg);
983 child_cfg->destroy(child_cfg);
984 this->task_manager->queue_task(this->task_manager, task);
985
986 return this->task_manager->initiate(this->task_manager);
987 }
988
989 /**
990 * Implementation of ike_sa_t.acquire.
991 */
992 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
993 {
994 child_cfg_t *child_cfg;
995 iterator_t *iterator;
996 child_sa_t *current, *child_sa = NULL;
997 task_t *task;
998 child_create_t *child_create;
999
1000 if (this->state == IKE_DELETING)
1001 {
1002 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
1003 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
1004 "IKE_SA is deleting", reqid);
1005 return FAILED;
1006 }
1007
1008 /* find CHILD_SA */
1009 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1010 while (iterator->iterate(iterator, (void**)&current))
1011 {
1012 if (current->get_reqid(current) == reqid)
1013 {
1014 child_sa = current;
1015 break;
1016 }
1017 }
1018 iterator->destroy(iterator);
1019 if (!child_sa)
1020 {
1021 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
1022 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
1023 "CHILD_SA not found", reqid);
1024 return FAILED;
1025 }
1026
1027
1028 if (this->state == IKE_CREATED)
1029 {
1030 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1031 this->task_manager->queue_task(this->task_manager, task);
1032 task = (task_t*)ike_natd_create(&this->public, TRUE);
1033 this->task_manager->queue_task(this->task_manager, task);
1034 task = (task_t*)ike_cert_create(&this->public, TRUE);
1035 this->task_manager->queue_task(this->task_manager, task);
1036 task = (task_t*)ike_auth_create(&this->public, TRUE);
1037 this->task_manager->queue_task(this->task_manager, task);
1038 task = (task_t*)ike_config_create(&this->public, TRUE);
1039 this->task_manager->queue_task(this->task_manager, task);
1040 task = (task_t*)ike_mobike_create(&this->public, TRUE);
1041 this->task_manager->queue_task(this->task_manager, task);
1042 }
1043
1044 child_cfg = child_sa->get_config(child_sa);
1045 child_create = child_create_create(&this->public, child_cfg);
1046 child_create->use_reqid(child_create, reqid);
1047 this->task_manager->queue_task(this->task_manager, (task_t*)child_create);
1048
1049 return this->task_manager->initiate(this->task_manager);
1050 }
1051
1052 /**
1053 * Implementation of ike_sa_t.route.
1054 */
1055 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
1056 {
1057 child_sa_t *child_sa;
1058 iterator_t *iterator;
1059 linked_list_t *my_ts, *other_ts;
1060 host_t *me, *other;
1061 status_t status;
1062
1063 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
1064
1065 /* check if not already routed*/
1066 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1067 while (iterator->iterate(iterator, (void**)&child_sa))
1068 {
1069 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1070 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
1071 {
1072 iterator->destroy(iterator);
1073 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a config already routed");
1074 return FAILED;
1075 }
1076 }
1077 iterator->destroy(iterator);
1078
1079 switch (this->state)
1080 {
1081 case IKE_DELETING:
1082 case IKE_REKEYING:
1083 SIG(CHILD_ROUTE_FAILED,
1084 "unable to route CHILD_SA, as its IKE_SA gets deleted");
1085 return FAILED;
1086 case IKE_CREATED:
1087 case IKE_CONNECTING:
1088 case IKE_ESTABLISHED:
1089 default:
1090 break;
1091 }
1092
1093 /* install kernel policies */
1094 child_sa = child_sa_create(this->my_host, this->other_host, this->my_id,
1095 this->other_id, child_cfg, FALSE, 0);
1096 me = this->my_host;
1097 if (this->my_virtual_ip)
1098 {
1099 me = this->my_virtual_ip;
1100 }
1101 other = this->other_host;
1102 if (this->other_virtual_ip)
1103 {
1104 other = this->other_virtual_ip;
1105 }
1106
1107 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, me);
1108 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, other);
1109 status = child_sa->add_policies(child_sa, my_ts, other_ts,
1110 child_cfg->get_mode(child_cfg));
1111 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1112 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1113 if (status == SUCCESS)
1114 {
1115 this->child_sas->insert_last(this->child_sas, child_sa);
1116 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
1117 }
1118 else
1119 {
1120 SIG(CHILD_ROUTE_FAILED, "routing CHILD_SA failed");
1121 }
1122 return status;
1123 }
1124
1125 /**
1126 * Implementation of ike_sa_t.unroute.
1127 */
1128 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
1129 {
1130 iterator_t *iterator;
1131 child_sa_t *child_sa;
1132 bool found = FALSE;
1133
1134 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
1135
1136 /* find CHILD_SA in ROUTED state */
1137 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1138 while (iterator->iterate(iterator, (void**)&child_sa))
1139 {
1140 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1141 child_sa->get_reqid(child_sa) == reqid)
1142 {
1143 iterator->remove(iterator);
1144 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
1145 child_sa->destroy(child_sa);
1146 found = TRUE;
1147 break;
1148 }
1149 }
1150 iterator->destroy(iterator);
1151
1152 if (!found)
1153 {
1154 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
1155 return FAILED;
1156 }
1157 /* if we are not established, and we have no more routed childs, remove whole SA */
1158 if (this->state == IKE_CREATED &&
1159 this->child_sas->get_count(this->child_sas) == 0)
1160 {
1161 return DESTROY_ME;
1162 }
1163 return SUCCESS;
1164 }
1165
1166 /**
1167 * Implementation of ike_sa_t.retransmit.
1168 */
1169 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
1170 {
1171 this->time.outbound = time(NULL);
1172 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1173 {
1174 child_cfg_t *child_cfg;
1175 child_sa_t* child_sa;
1176 linked_list_t *to_route, *to_restart;
1177 iterator_t *iterator;
1178
1179 /* send a proper signal to brief interested bus listeners */
1180 switch (this->state)
1181 {
1182 case IKE_CONNECTING:
1183 {
1184 /* retry IKE_SA_INIT if we have multiple keyingtries */
1185 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1186 this->keyingtry++;
1187 if (tries == 0 || tries > this->keyingtry)
1188 {
1189 SIG(IKE_UP_FAILED, "peer not responding, trying again "
1190 "(%d/%d) in background ", this->keyingtry + 1, tries);
1191 reset(this);
1192 return this->task_manager->initiate(this->task_manager);
1193 }
1194 SIG(IKE_UP_FAILED, "establishing IKE_SA failed, peer not responding");
1195 break;
1196 }
1197 case IKE_REKEYING:
1198 SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
1199 break;
1200 case IKE_DELETING:
1201 SIG(IKE_DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
1202 break;
1203 default:
1204 break;
1205 }
1206
1207 /* summarize how we have to handle each child */
1208 to_route = linked_list_create();
1209 to_restart = linked_list_create();
1210 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1211 while (iterator->iterate(iterator, (void**)&child_sa))
1212 {
1213 child_cfg = child_sa->get_config(child_sa);
1214
1215 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1216 {
1217 /* reroute routed CHILD_SAs */
1218 to_route->insert_last(to_route, child_cfg);
1219 }
1220 else
1221 {
1222 /* use DPD action for established CHILD_SAs */
1223 switch (this->peer_cfg->get_dpd_action(this->peer_cfg))
1224 {
1225 case DPD_ROUTE:
1226 to_route->insert_last(to_route, child_cfg);
1227 break;
1228 case DPD_RESTART:
1229 to_restart->insert_last(to_restart, child_cfg);
1230 break;
1231 default:
1232 break;
1233 }
1234 }
1235 }
1236 iterator->destroy(iterator);
1237
1238 /* create a new IKE_SA if we have to route or to restart */
1239 if (to_route->get_count(to_route) || to_restart->get_count(to_restart))
1240 {
1241 private_ike_sa_t *new;
1242 task_t *task;
1243
1244 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1245 charon->ike_sa_manager, TRUE);
1246
1247 set_peer_cfg(new, this->peer_cfg);
1248 /* use actual used host, not the wildcarded one in config */
1249 new->other_host->destroy(new->other_host);
1250 new->other_host = this->other_host->clone(this->other_host);
1251 /* reset port to 500, but only if peer is not NATed */
1252 if (!has_condition(this, COND_NAT_THERE))
1253 {
1254 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1255 }
1256 /* take over virtual ip, as we need it for a proper route */
1257 if (this->my_virtual_ip)
1258 {
1259 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1260 }
1261
1262 /* install routes */
1263 while (to_route->remove_last(to_route, (void**)&child_cfg) == SUCCESS)
1264 {
1265 route(new, child_cfg);
1266 }
1267
1268 /* restart children */
1269 if (to_restart->get_count(to_restart))
1270 {
1271 task = (task_t*)ike_init_create(&new->public, TRUE, NULL);
1272 new->task_manager->queue_task(new->task_manager, task);
1273 task = (task_t*)ike_natd_create(&new->public, TRUE);
1274 new->task_manager->queue_task(new->task_manager, task);
1275 task = (task_t*)ike_cert_create(&new->public, TRUE);
1276 new->task_manager->queue_task(new->task_manager, task);
1277 task = (task_t*)ike_config_create(&new->public, TRUE);
1278 new->task_manager->queue_task(new->task_manager, task);
1279 task = (task_t*)ike_auth_create(&new->public, TRUE);
1280 new->task_manager->queue_task(new->task_manager, task);
1281
1282 while (to_restart->remove_last(to_restart, (void**)&child_cfg) == SUCCESS)
1283 {
1284 task = (task_t*)child_create_create(&new->public, child_cfg);
1285 new->task_manager->queue_task(new->task_manager, task);
1286 }
1287 task = (task_t*)ike_mobike_create(&new->public, TRUE);
1288 new->task_manager->queue_task(new->task_manager, task);
1289 new->task_manager->initiate(new->task_manager);
1290 }
1291 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1292 }
1293 to_route->destroy(to_route);
1294 to_restart->destroy(to_restart);
1295 return DESTROY_ME;
1296 }
1297 return SUCCESS;
1298 }
1299
1300 /**
1301 * Implementation of ike_sa_t.get_prf.
1302 */
1303 static prf_t *get_prf(private_ike_sa_t *this)
1304 {
1305 return this->prf;
1306 }
1307
1308 /**
1309 * Implementation of ike_sa_t.get_prf.
1310 */
1311 static prf_t *get_child_prf(private_ike_sa_t *this)
1312 {
1313 return this->child_prf;
1314 }
1315
1316 /**
1317 * Implementation of ike_sa_t.get_skp_bild
1318 */
1319 static chunk_t get_skp_build(private_ike_sa_t *this)
1320 {
1321 return this->skp_build;
1322 }
1323
1324 /**
1325 * Implementation of ike_sa_t.get_skp_verify
1326 */
1327 static chunk_t get_skp_verify(private_ike_sa_t *this)
1328 {
1329 return this->skp_verify;
1330 }
1331
1332 /**
1333 * Implementation of ike_sa_t.get_id.
1334 */
1335 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1336 {
1337 return this->ike_sa_id;
1338 }
1339
1340 /**
1341 * Implementation of ike_sa_t.get_my_id.
1342 */
1343 static identification_t* get_my_id(private_ike_sa_t *this)
1344 {
1345 return this->my_id;
1346 }
1347
1348 /**
1349 * Implementation of ike_sa_t.set_my_id.
1350 */
1351 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1352 {
1353 DESTROY_IF(this->my_id);
1354 this->my_id = me;
1355 }
1356
1357 /**
1358 * Implementation of ike_sa_t.get_other_id.
1359 */
1360 static identification_t* get_other_id(private_ike_sa_t *this)
1361 {
1362 return this->other_id;
1363 }
1364
1365 /**
1366 * Implementation of ike_sa_t.set_other_id.
1367 */
1368 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1369 {
1370 DESTROY_IF(this->other_id);
1371 this->other_id = other;
1372 }
1373
1374 /**
1375 * Implementation of ike_sa_t.get_other_ca.
1376 */
1377 static ca_info_t* get_other_ca(private_ike_sa_t *this)
1378 {
1379 return this->other_ca;
1380 }
1381
1382 /**
1383 * Implementation of ike_sa_t.set_other_ca.
1384 */
1385 static void set_other_ca(private_ike_sa_t *this, ca_info_t *other_ca)
1386 {
1387 this->other_ca = other_ca;
1388 }
1389
1390 /**
1391 * Implementation of ike_sa_t.derive_keys.
1392 */
1393 static status_t derive_keys(private_ike_sa_t *this,
1394 proposal_t *proposal, chunk_t secret,
1395 chunk_t nonce_i, chunk_t nonce_r,
1396 bool initiator, prf_t *child_prf, prf_t *old_prf)
1397 {
1398 prf_plus_t *prf_plus;
1399 chunk_t skeyseed, key, nonces, prf_plus_seed;
1400 algorithm_t *algo;
1401 size_t key_size;
1402 crypter_t *crypter_i, *crypter_r;
1403 signer_t *signer_i, *signer_r;
1404 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1405 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1406 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1407
1408 /* Create SAs general purpose PRF first, we may use it here */
1409 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1410 {
1411 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1412 return FAILED;
1413 }
1414 this->prf = prf_create(algo->algorithm);
1415 if (this->prf == NULL)
1416 {
1417 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1418 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1419 return FAILED;
1420 }
1421
1422 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1423 nonces = chunk_cat("cc", nonce_i, nonce_r);
1424 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1425 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1426 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1427
1428 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1429 *
1430 * if we are rekeying, SKEYSEED is built on another way
1431 */
1432 if (child_prf == NULL) /* not rekeying */
1433 {
1434 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1435 this->prf->set_key(this->prf, nonces);
1436 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1437 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1438 this->prf->set_key(this->prf, skeyseed);
1439 chunk_free(&skeyseed);
1440 chunk_free(&secret);
1441 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1442 }
1443 else
1444 {
1445 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1446 * use OLD SAs PRF functions for both prf_plus and prf */
1447 secret = chunk_cat("mc", secret, nonces);
1448 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1449 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1450 old_prf->set_key(old_prf, skeyseed);
1451 chunk_free(&skeyseed);
1452 chunk_free(&secret);
1453 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1454 }
1455 chunk_free(&nonces);
1456 chunk_free(&prf_plus_seed);
1457
1458 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1459
1460 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1461 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1462 this->child_prf = prf_create(algo->algorithm);
1463 key_size = this->child_prf->get_key_size(this->child_prf);
1464 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1465 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1466 this->child_prf->set_key(this->child_prf, key);
1467 chunk_free(&key);
1468
1469 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1470 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1471 {
1472 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1473 return FAILED;
1474 }
1475 signer_i = signer_create(algo->algorithm);
1476 signer_r = signer_create(algo->algorithm);
1477 if (signer_i == NULL || signer_r == NULL)
1478 {
1479 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1480 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1481 return FAILED;
1482 }
1483 key_size = signer_i->get_key_size(signer_i);
1484
1485 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1486 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1487 signer_i->set_key(signer_i, key);
1488 chunk_free(&key);
1489
1490 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1491 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1492 signer_r->set_key(signer_r, key);
1493 chunk_free(&key);
1494
1495 if (initiator)
1496 {
1497 this->signer_in = signer_r;
1498 this->signer_out = signer_i;
1499 }
1500 else
1501 {
1502 this->signer_in = signer_i;
1503 this->signer_out = signer_r;
1504 }
1505
1506 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1507 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1508 {
1509 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1510 return FAILED;
1511 }
1512 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1513 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1514 if (crypter_i == NULL || crypter_r == NULL)
1515 {
1516 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1517 "%N (key size %d) not supported!",
1518 encryption_algorithm_names, algo->algorithm, algo->key_size);
1519 return FAILED;
1520 }
1521 key_size = crypter_i->get_key_size(crypter_i);
1522
1523 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1524 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1525 crypter_i->set_key(crypter_i, key);
1526 chunk_free(&key);
1527
1528 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1529 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1530 crypter_r->set_key(crypter_r, key);
1531 chunk_free(&key);
1532
1533 if (initiator)
1534 {
1535 this->crypter_in = crypter_r;
1536 this->crypter_out = crypter_i;
1537 }
1538 else
1539 {
1540 this->crypter_in = crypter_i;
1541 this->crypter_out = crypter_r;
1542 }
1543
1544 /* SK_pi/SK_pr used for authentication => stored for later */
1545 key_size = this->prf->get_key_size(this->prf);
1546 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1547 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1548 if (initiator)
1549 {
1550 this->skp_build = key;
1551 }
1552 else
1553 {
1554 this->skp_verify = key;
1555 }
1556 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1557 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1558 if (initiator)
1559 {
1560 this->skp_verify = key;
1561 }
1562 else
1563 {
1564 this->skp_build = key;
1565 }
1566
1567 /* all done, prf_plus not needed anymore */
1568 prf_plus->destroy(prf_plus);
1569
1570 return SUCCESS;
1571 }
1572
1573 /**
1574 * Implementation of ike_sa_t.add_child_sa.
1575 */
1576 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1577 {
1578 this->child_sas->insert_last(this->child_sas, child_sa);
1579 }
1580
1581 /**
1582 * Implementation of ike_sa_t.get_child_sa.
1583 */
1584 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1585 u_int32_t spi, bool inbound)
1586 {
1587 iterator_t *iterator;
1588 child_sa_t *current, *found = NULL;
1589
1590 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1591 while (iterator->iterate(iterator, (void**)&current))
1592 {
1593 if (current->get_spi(current, inbound) == spi &&
1594 current->get_protocol(current) == protocol)
1595 {
1596 found = current;
1597 }
1598 }
1599 iterator->destroy(iterator);
1600 return found;
1601 }
1602
1603 /**
1604 * Implementation of ike_sa_t.create_child_sa_iterator.
1605 */
1606 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1607 {
1608 return this->child_sas->create_iterator(this->child_sas, TRUE);
1609 }
1610
1611 /**
1612 * Implementation of ike_sa_t.rekey_child_sa.
1613 */
1614 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1615 {
1616 child_sa_t *child_sa;
1617 child_rekey_t *child_rekey;
1618
1619 child_sa = get_child_sa(this, protocol, spi, TRUE);
1620 if (child_sa)
1621 {
1622 child_rekey = child_rekey_create(&this->public, child_sa);
1623 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1624 return this->task_manager->initiate(this->task_manager);
1625 }
1626 return FAILED;
1627 }
1628
1629 /**
1630 * Implementation of ike_sa_t.delete_child_sa.
1631 */
1632 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1633 {
1634 child_sa_t *child_sa;
1635 child_delete_t *child_delete;
1636
1637 child_sa = get_child_sa(this, protocol, spi, TRUE);
1638 if (child_sa)
1639 {
1640 child_delete = child_delete_create(&this->public, child_sa);
1641 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1642 return this->task_manager->initiate(this->task_manager);
1643 }
1644 return FAILED;
1645 }
1646
1647 /**
1648 * Implementation of ike_sa_t.destroy_child_sa.
1649 */
1650 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1651 u_int32_t spi)
1652 {
1653 iterator_t *iterator;
1654 child_sa_t *child_sa;
1655 status_t status = NOT_FOUND;
1656
1657 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1658 while (iterator->iterate(iterator, (void**)&child_sa))
1659 {
1660 if (child_sa->get_protocol(child_sa) == protocol &&
1661 child_sa->get_spi(child_sa, TRUE) == spi)
1662 {
1663 child_sa->destroy(child_sa);
1664 iterator->remove(iterator);
1665 status = SUCCESS;
1666 break;
1667 }
1668 }
1669 iterator->destroy(iterator);
1670 return status;
1671 }
1672
1673 /**
1674 * Implementation of public_ike_sa_t.delete.
1675 */
1676 static status_t delete_(private_ike_sa_t *this)
1677 {
1678 ike_delete_t *ike_delete;
1679
1680 switch (this->state)
1681 {
1682 case IKE_ESTABLISHED:
1683 case IKE_REKEYING:
1684 ike_delete = ike_delete_create(&this->public, TRUE);
1685 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1686 return this->task_manager->initiate(this->task_manager);
1687 default:
1688 DBG1(DBG_IKE, "destroying IKE_SA in state %N without notification",
1689 ike_sa_state_names, this->state);
1690 break;
1691 }
1692 return DESTROY_ME;
1693 }
1694
1695 /**
1696 * Implementation of ike_sa_t.rekey.
1697 */
1698 static status_t rekey(private_ike_sa_t *this)
1699 {
1700 ike_rekey_t *ike_rekey;
1701
1702 ike_rekey = ike_rekey_create(&this->public, TRUE);
1703
1704 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1705 return this->task_manager->initiate(this->task_manager);
1706 }
1707
1708 /**
1709 * Implementation of ike_sa_t.reestablish
1710 */
1711 static status_t reestablish(private_ike_sa_t *this)
1712 {
1713 task_t *task;
1714
1715 task = (task_t*)ike_reauth_create(&this->public);
1716 this->task_manager->queue_task(this->task_manager, task);
1717
1718 return this->task_manager->initiate(this->task_manager);
1719 }
1720
1721 /**
1722 * Implementation of ike_sa_t.roam.
1723 */
1724 static status_t roam(private_ike_sa_t *this, bool address)
1725 {
1726 host_t *me, *other;
1727 ike_mobike_t *mobike;
1728
1729 /* responder just updates the peer about changed address config */
1730 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
1731 {
1732 if (supports_extension(this, EXT_MOBIKE) && address)
1733 {
1734 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1735 mobike = ike_mobike_create(&this->public, TRUE);
1736 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1737 return this->task_manager->initiate(this->task_manager);
1738 }
1739 return SUCCESS;
1740 }
1741
1742 /* get best address pair to use */
1743 other = this->other_host;
1744 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
1745 other);
1746
1747 /* TODO: find a better path using additional addresses of peer */
1748
1749 if (!me)
1750 {
1751 /* no route found to host, set to stale, wait for a new route */
1752 set_condition(this, COND_STALE, TRUE);
1753 return FAILED;
1754 }
1755
1756 set_condition(this, COND_STALE, FALSE);
1757 if (me->ip_equals(me, this->my_host) &&
1758 other->ip_equals(other, this->other_host))
1759 {
1760 DBG2(DBG_IKE, "%H still reached through %H, no update needed",
1761 this->other_host, me);
1762 me->destroy(me);
1763 return SUCCESS;
1764 }
1765 me->set_port(me, this->my_host->get_port(this->my_host));
1766 other = other->clone(other);
1767 other->set_port(other, this->other_host->get_port(this->other_host));
1768 set_my_host(this, me);
1769 set_other_host(this, other);
1770
1771 /* update addresses with mobike, if supported ... */
1772 if (supports_extension(this, EXT_MOBIKE))
1773 {
1774 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1775 mobike = ike_mobike_create(&this->public, TRUE);
1776 mobike->roam(mobike, address);
1777 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1778 return this->task_manager->initiate(this->task_manager);
1779 }
1780 DBG1(DBG_IKE, "reestablishing IKE_SA due address change");
1781 /* ... reestablish if not */
1782 return reestablish(this);
1783 }
1784
1785 /**
1786 * Implementation of ike_sa_t.inherit.
1787 */
1788 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
1789 {
1790 child_sa_t *child_sa;
1791 host_t *ip;
1792
1793 /* apply hosts and ids */
1794 this->my_host->destroy(this->my_host);
1795 this->other_host->destroy(this->other_host);
1796 this->my_id->destroy(this->my_id);
1797 this->other_id->destroy(this->other_id);
1798 this->my_host = other->my_host->clone(other->my_host);
1799 this->other_host = other->other_host->clone(other->other_host);
1800 this->my_id = other->my_id->clone(other->my_id);
1801 this->other_id = other->other_id->clone(other->other_id);
1802
1803 /* apply virtual assigned IPs... */
1804 if (other->my_virtual_ip)
1805 {
1806 this->my_virtual_ip = other->my_virtual_ip;
1807 other->my_virtual_ip = NULL;
1808 }
1809 if (other->other_virtual_ip)
1810 {
1811 this->other_virtual_ip = other->other_virtual_ip;
1812 other->other_virtual_ip = NULL;
1813 }
1814
1815 /* ... and DNS servers */
1816 while (other->dns_servers->remove_last(other->dns_servers,
1817 (void**)&ip) == SUCCESS)
1818 {
1819 this->dns_servers->insert_first(this->dns_servers, ip);
1820 }
1821
1822 /* adopt all children */
1823 while (other->child_sas->remove_last(other->child_sas,
1824 (void**)&child_sa) == SUCCESS)
1825 {
1826 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1827 }
1828
1829 /* move pending tasks to the new IKE_SA */
1830 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1831
1832 /* we have to initate here, there may be new tasks to handle */
1833 return this->task_manager->initiate(this->task_manager);
1834 }
1835
1836 /**
1837 * Implementation of ike_sa_t.remove_dns_server
1838 */
1839 static void remove_dns_servers(private_ike_sa_t *this)
1840 {
1841 FILE *file;
1842 struct stat stats;
1843 chunk_t contents, line, orig_line, token;
1844 char string[INET6_ADDRSTRLEN];
1845 host_t *ip;
1846 iterator_t *iterator;
1847
1848 if (this->dns_servers->get_count(this->dns_servers) == 0)
1849 {
1850 /* don't touch anything if we have no nameservers installed */
1851 return;
1852 }
1853
1854 file = fopen(RESOLV_CONF, "r");
1855 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1856 {
1857 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1858 RESOLV_CONF, strerror(errno));
1859 return;
1860 }
1861
1862 contents = chunk_alloca((size_t)stats.st_size);
1863
1864 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1865 {
1866 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1867 fclose(file);
1868 return;
1869 }
1870
1871 fclose(file);
1872 file = fopen(RESOLV_CONF, "w");
1873 if (file == NULL)
1874 {
1875 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1876 RESOLV_CONF, strerror(errno));
1877 return;
1878 }
1879
1880 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
1881 while (fetchline(&contents, &line))
1882 {
1883 bool found = FALSE;
1884 orig_line = line;
1885 if (extract_token(&token, ' ', &line) &&
1886 strncasecmp(token.ptr, "nameserver", token.len) == 0)
1887 {
1888 if (!extract_token(&token, ' ', &line))
1889 {
1890 token = line;
1891 }
1892 iterator->reset(iterator);
1893 while (iterator->iterate(iterator, (void**)&ip))
1894 {
1895 snprintf(string, sizeof(string), "%H", ip);
1896 if (strlen(string) == token.len &&
1897 strncmp(token.ptr, string, token.len) == 0)
1898 {
1899 iterator->remove(iterator);
1900 ip->destroy(ip);
1901 found = TRUE;
1902 break;
1903 }
1904 }
1905 }
1906
1907 if (!found)
1908 {
1909 /* write line untouched back to file */
1910 fwrite(orig_line.ptr, orig_line.len, 1, file);
1911 fprintf(file, "\n");
1912 }
1913 }
1914 iterator->destroy(iterator);
1915 fclose(file);
1916 }
1917
1918 /**
1919 * Implementation of ike_sa_t.add_dns_server
1920 */
1921 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
1922 {
1923 FILE *file;
1924 struct stat stats;
1925 chunk_t contents;
1926
1927 DBG1(DBG_IKE, "installing DNS server %H", dns);
1928
1929 file = fopen(RESOLV_CONF, "a+");
1930 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1931 {
1932 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1933 RESOLV_CONF, strerror(errno));
1934 return;
1935 }
1936
1937 contents = chunk_alloca(stats.st_size);
1938
1939 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1940 {
1941 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1942 fclose(file);
1943 return;
1944 }
1945
1946 fclose(file);
1947 file = fopen(RESOLV_CONF, "w");
1948 if (file == NULL)
1949 {
1950 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1951 RESOLV_CONF, strerror(errno));
1952 return;
1953 }
1954
1955 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
1956 dns, this->other_id) < 0)
1957 {
1958 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
1959 }
1960 else
1961 {
1962 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
1963 }
1964 fwrite(contents.ptr, contents.len, 1, file);
1965
1966 fclose(file);
1967 }
1968
1969 /**
1970 * Implementation of ike_sa_t.destroy.
1971 */
1972 static void destroy(private_ike_sa_t *this)
1973 {
1974 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1975
1976 DESTROY_IF(this->crypter_in);
1977 DESTROY_IF(this->crypter_out);
1978 DESTROY_IF(this->signer_in);
1979 DESTROY_IF(this->signer_out);
1980 DESTROY_IF(this->prf);
1981 DESTROY_IF(this->child_prf);
1982 chunk_free(&this->skp_verify);
1983 chunk_free(&this->skp_build);
1984
1985 if (this->my_virtual_ip)
1986 {
1987 charon->kernel_interface->del_ip(charon->kernel_interface,
1988 this->my_virtual_ip);
1989 this->my_virtual_ip->destroy(this->my_virtual_ip);
1990 }
1991 DESTROY_IF(this->other_virtual_ip);
1992
1993 remove_dns_servers(this);
1994 this->dns_servers->destroy_offset(this->dns_servers,
1995 offsetof(host_t, destroy));
1996 this->additional_addresses->destroy_offset(this->additional_addresses,
1997 offsetof(host_t, destroy));
1998
1999 DESTROY_IF(this->my_host);
2000 DESTROY_IF(this->other_host);
2001 DESTROY_IF(this->my_id);
2002 DESTROY_IF(this->other_id);
2003
2004 DESTROY_IF(this->ike_cfg);
2005 DESTROY_IF(this->peer_cfg);
2006
2007 this->ike_sa_id->destroy(this->ike_sa_id);
2008 this->task_manager->destroy(this->task_manager);
2009 free(this);
2010 }
2011
2012 /*
2013 * Described in header.
2014 */
2015 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2016 {
2017 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2018 static u_int32_t unique_id = 0;
2019
2020 /* Public functions */
2021 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
2022 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
2023 this->public.get_stats = (void (*)(ike_sa_t*,u_int32_t*))get_stats;
2024 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2025 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2026 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2027 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2028 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2029 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2030 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2031 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2032 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2033 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2034 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2035 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2036 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2037 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2038 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2039 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2040 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2041 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2042 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2043 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2044 this->public.get_other_ca = (ca_info_t* (*)(ike_sa_t*)) get_other_ca;
2045 this->public.set_other_ca = (void (*)(ike_sa_t*,ca_info_t*)) set_other_ca;
2046 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2047 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2048 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2049 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2050 this->public.set_pending_updates = (void(*)(ike_sa_t*, u_int32_t updates))set_pending_updates;
2051 this->public.get_pending_updates = (u_int32_t(*)(ike_sa_t*))get_pending_updates;
2052 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2053 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2054 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2055 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2056 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2057 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2058 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2059 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
2060 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
2061 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
2062 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
2063 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2064 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2065 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2066 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2067 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2068 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2069 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2070 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2071 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2072 this->public.roam = (status_t(*)(ike_sa_t*,bool))roam;
2073 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2074 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2075 this->public.reset = (void (*)(ike_sa_t*))reset;
2076 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2077 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2078 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2079 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2080
2081 /* initialize private fields */
2082 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2083 this->child_sas = linked_list_create();
2084 this->my_host = host_create_any(AF_INET);
2085 this->other_host = host_create_any(AF_INET);
2086 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2087 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2088 this->other_ca = NULL;
2089 this->extensions = 0;
2090 this->conditions = 0;
2091 this->crypter_in = NULL;
2092 this->crypter_out = NULL;
2093 this->signer_in = NULL;
2094 this->signer_out = NULL;
2095 this->prf = NULL;
2096 this->skp_verify = chunk_empty;
2097 this->skp_build = chunk_empty;
2098 this->child_prf = NULL;
2099 this->state = IKE_CREATED;
2100 this->time.inbound = this->time.outbound = time(NULL);
2101 this->time.established = 0;
2102 this->time.rekey = 0;
2103 this->time.delete = 0;
2104 this->ike_cfg = NULL;
2105 this->peer_cfg = NULL;
2106 this->task_manager = task_manager_create(&this->public);
2107 this->unique_id = ++unique_id;
2108 this->my_virtual_ip = NULL;
2109 this->other_virtual_ip = NULL;
2110 this->dns_servers = linked_list_create();
2111 this->additional_addresses = linked_list_create();
2112 this->pending_updates = 0;
2113 this->keyingtry = 0;
2114
2115 return &this->public;
2116 }