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