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