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