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