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