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