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