af2c7d0b080be08425a88797604f411c23796f58
[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 this->time.inbound = time(NULL);
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 }
1464 status = this->task_manager->process_message(this->task_manager, message);
1465 if (status != DESTROY_ME)
1466 {
1467 return status;
1468 }
1469 /* if IKE_SA gets closed for any reasons, reroute routed children */
1470 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1471 while (iterator->iterate(iterator, (void**)&child))
1472 {
1473 if (child->get_state(child) == CHILD_ROUTED)
1474 {
1475 has_routed = TRUE;
1476 break;
1477 }
1478 }
1479 iterator->destroy(iterator);
1480 if (!has_routed)
1481 {
1482 return status;
1483 }
1484 /* move routed children to a new IKE_SA, apply connection info */
1485 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1486 charon->ike_sa_manager, TRUE);
1487 set_peer_cfg(new, this->peer_cfg);
1488 new->other_host->destroy(new->other_host);
1489 new->other_host = this->other_host->clone(this->other_host);
1490 if (!has_condition(this, COND_NAT_THERE))
1491 {
1492 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1493 }
1494 if (this->my_virtual_ip)
1495 {
1496 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1497 }
1498 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1499 while (iterator->iterate(iterator, (void**)&child))
1500 {
1501 if (child->get_state(child) == CHILD_ROUTED)
1502 {
1503 route(new, child->get_config(child));
1504 }
1505 }
1506 iterator->destroy(iterator);
1507 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1508 return status;
1509 }
1510 }
1511
1512 /**
1513 * Implementation of ike_sa_t.get_prf.
1514 */
1515 static prf_t *get_prf(private_ike_sa_t *this)
1516 {
1517 return this->prf;
1518 }
1519
1520 /**
1521 * Implementation of ike_sa_t.get_prf.
1522 */
1523 static prf_t *get_child_prf(private_ike_sa_t *this)
1524 {
1525 return this->child_prf;
1526 }
1527
1528 /**
1529 * Implementation of ike_sa_t.get_skp_bild
1530 */
1531 static chunk_t get_skp_build(private_ike_sa_t *this)
1532 {
1533 return this->skp_build;
1534 }
1535
1536 /**
1537 * Implementation of ike_sa_t.get_skp_verify
1538 */
1539 static chunk_t get_skp_verify(private_ike_sa_t *this)
1540 {
1541 return this->skp_verify;
1542 }
1543
1544 /**
1545 * Implementation of ike_sa_t.get_id.
1546 */
1547 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1548 {
1549 return this->ike_sa_id;
1550 }
1551
1552 /**
1553 * Implementation of ike_sa_t.get_my_id.
1554 */
1555 static identification_t* get_my_id(private_ike_sa_t *this)
1556 {
1557 return this->my_id;
1558 }
1559
1560 /**
1561 * Implementation of ike_sa_t.set_my_id.
1562 */
1563 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1564 {
1565 DESTROY_IF(this->my_id);
1566 this->my_id = me;
1567 }
1568
1569 /**
1570 * Implementation of ike_sa_t.get_other_id.
1571 */
1572 static identification_t* get_other_id(private_ike_sa_t *this)
1573 {
1574 return this->other_id;
1575 }
1576
1577 /**
1578 * Implementation of ike_sa_t.set_other_id.
1579 */
1580 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1581 {
1582 DESTROY_IF(this->other_id);
1583 this->other_id = other;
1584 }
1585
1586 /**
1587 * Implementation of ike_sa_t.get_eap_identity.
1588 */
1589 static identification_t* get_eap_identity(private_ike_sa_t *this)
1590 {
1591 return this->eap_identity;
1592 }
1593
1594 /**
1595 * Implementation of ike_sa_t.set_eap_identity.
1596 */
1597 static void set_eap_identity(private_ike_sa_t *this, identification_t *id)
1598 {
1599 DESTROY_IF(this->eap_identity);
1600 this->eap_identity = id;
1601 }
1602
1603 /**
1604 * Implementation of ike_sa_t.derive_keys.
1605 */
1606 static status_t derive_keys(private_ike_sa_t *this,
1607 proposal_t *proposal, chunk_t secret,
1608 chunk_t nonce_i, chunk_t nonce_r,
1609 bool initiator, prf_t *child_prf, prf_t *old_prf)
1610 {
1611 prf_plus_t *prf_plus;
1612 chunk_t skeyseed, key, full_nonce, fixed_nonce, prf_plus_seed;
1613 u_int16_t alg, key_size;
1614 crypter_t *crypter_i, *crypter_r;
1615 signer_t *signer_i, *signer_r;
1616 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1617 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1618 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1619
1620 /* Create SAs general purpose PRF first, we may use it here */
1621 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL))
1622 {
1623 DBG1(DBG_IKE, "no %N selected",
1624 transform_type_names, PSEUDO_RANDOM_FUNCTION);
1625 return FAILED;
1626 }
1627 this->prf = lib->crypto->create_prf(lib->crypto, alg);
1628 if (this->prf == NULL)
1629 {
1630 DBG1(DBG_IKE, "%N %N not supported!",
1631 transform_type_names, PSEUDO_RANDOM_FUNCTION,
1632 pseudo_random_function_names, alg);
1633 return FAILED;
1634 }
1635 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1636 /* full nonce is used as seed for PRF+ ... */
1637 full_nonce = chunk_cat("cc", nonce_i, nonce_r);
1638 /* but the PRF may need a fixed key which only uses the first bytes of
1639 * the nonces. */
1640 switch (alg)
1641 {
1642 case PRF_AES128_XCBC:
1643 /* while rfc4434 defines variable keys for AES-XCBC, rfc3664 does
1644 * not and therefore fixed key semantics apply to XCBC for key
1645 * derivation. */
1646 nonce_i.len = min(nonce_i.len, this->prf->get_key_size(this->prf)/2);
1647 nonce_r.len = min(nonce_r.len, this->prf->get_key_size(this->prf)/2);
1648 break;
1649 default:
1650 /* all other algorithms use variable key length, full nonce */
1651 break;
1652 }
1653 fixed_nonce = chunk_cat("cc", nonce_i, nonce_r);
1654 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1655 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1656 prf_plus_seed = chunk_cat("ccc", full_nonce, spi_i, spi_r);
1657
1658 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1659 *
1660 * if we are rekeying, SKEYSEED is built on another way
1661 */
1662 if (child_prf == NULL) /* not rekeying */
1663 {
1664 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1665 this->prf->set_key(this->prf, fixed_nonce);
1666 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1667 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1668 this->prf->set_key(this->prf, skeyseed);
1669 chunk_free(&skeyseed);
1670 chunk_free(&secret);
1671 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1672 }
1673 else
1674 {
1675 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1676 * use OLD SAs PRF functions for both prf_plus and prf */
1677 secret = chunk_cat("mc", secret, full_nonce);
1678 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1679 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1680 old_prf->set_key(old_prf, skeyseed);
1681 chunk_free(&skeyseed);
1682 chunk_free(&secret);
1683 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1684 }
1685 chunk_free(&full_nonce);
1686 chunk_free(&fixed_nonce);
1687 chunk_free(&prf_plus_seed);
1688
1689 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1690
1691 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1692 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL);
1693 this->child_prf = lib->crypto->create_prf(lib->crypto, alg);
1694 key_size = this->child_prf->get_key_size(this->child_prf);
1695 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1696 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1697 this->child_prf->set_key(this->child_prf, key);
1698 chunk_free(&key);
1699
1700 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1701 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, NULL))
1702 {
1703 DBG1(DBG_IKE, "no %N selected",
1704 transform_type_names, INTEGRITY_ALGORITHM);
1705 return FAILED;
1706 }
1707 signer_i = lib->crypto->create_signer(lib->crypto, alg);
1708 signer_r = lib->crypto->create_signer(lib->crypto, alg);
1709 if (signer_i == NULL || signer_r == NULL)
1710 {
1711 DBG1(DBG_IKE, "%N %N not supported!",
1712 transform_type_names, INTEGRITY_ALGORITHM,
1713 integrity_algorithm_names ,alg);
1714 prf_plus->destroy(prf_plus);
1715 return FAILED;
1716 }
1717 key_size = signer_i->get_key_size(signer_i);
1718
1719 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1720 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1721 signer_i->set_key(signer_i, key);
1722 chunk_free(&key);
1723
1724 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1725 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1726 signer_r->set_key(signer_r, key);
1727 chunk_free(&key);
1728
1729 if (initiator)
1730 {
1731 this->signer_in = signer_r;
1732 this->signer_out = signer_i;
1733 }
1734 else
1735 {
1736 this->signer_in = signer_i;
1737 this->signer_out = signer_r;
1738 }
1739
1740 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1741 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &key_size))
1742 {
1743 DBG1(DBG_IKE, "no %N selected",
1744 transform_type_names, ENCRYPTION_ALGORITHM);
1745 prf_plus->destroy(prf_plus);
1746 return FAILED;
1747 }
1748 crypter_i = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
1749 crypter_r = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
1750 if (crypter_i == NULL || crypter_r == NULL)
1751 {
1752 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
1753 transform_type_names, ENCRYPTION_ALGORITHM,
1754 encryption_algorithm_names, alg, key_size);
1755 prf_plus->destroy(prf_plus);
1756 return FAILED;
1757 }
1758 key_size = crypter_i->get_key_size(crypter_i);
1759
1760 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1761 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1762 crypter_i->set_key(crypter_i, key);
1763 chunk_free(&key);
1764
1765 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1766 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1767 crypter_r->set_key(crypter_r, key);
1768 chunk_free(&key);
1769
1770 if (initiator)
1771 {
1772 this->crypter_in = crypter_r;
1773 this->crypter_out = crypter_i;
1774 }
1775 else
1776 {
1777 this->crypter_in = crypter_i;
1778 this->crypter_out = crypter_r;
1779 }
1780
1781 /* SK_pi/SK_pr used for authentication => stored for later */
1782 key_size = this->prf->get_key_size(this->prf);
1783 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1784 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1785 if (initiator)
1786 {
1787 this->skp_build = key;
1788 }
1789 else
1790 {
1791 this->skp_verify = key;
1792 }
1793 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1794 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1795 if (initiator)
1796 {
1797 this->skp_verify = key;
1798 }
1799 else
1800 {
1801 this->skp_build = key;
1802 }
1803
1804 /* all done, prf_plus not needed anymore */
1805 prf_plus->destroy(prf_plus);
1806
1807 return SUCCESS;
1808 }
1809
1810 /**
1811 * Implementation of ike_sa_t.get_proposal.
1812 */
1813 static char* get_proposal(private_ike_sa_t *this)
1814 {
1815 return this->selected_proposal;
1816 }
1817
1818 /**
1819 * Implementation of ike_sa_t.set_proposal.
1820 */
1821 static void set_proposal(private_ike_sa_t *this, char *proposal)
1822 {
1823 free(this->selected_proposal);
1824 this->selected_proposal = strdup(proposal);
1825 }
1826
1827 /**
1828 * Implementation of ike_sa_t.add_child_sa.
1829 */
1830 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1831 {
1832 this->child_sas->insert_last(this->child_sas, child_sa);
1833 }
1834
1835 /**
1836 * Implementation of ike_sa_t.get_child_sa.
1837 */
1838 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1839 u_int32_t spi, bool inbound)
1840 {
1841 iterator_t *iterator;
1842 child_sa_t *current, *found = NULL;
1843
1844 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1845 while (iterator->iterate(iterator, (void**)&current))
1846 {
1847 if (current->get_spi(current, inbound) == spi &&
1848 current->get_protocol(current) == protocol)
1849 {
1850 found = current;
1851 }
1852 }
1853 iterator->destroy(iterator);
1854 return found;
1855 }
1856
1857 /**
1858 * Implementation of ike_sa_t.create_child_sa_iterator.
1859 */
1860 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1861 {
1862 return this->child_sas->create_iterator(this->child_sas, TRUE);
1863 }
1864
1865 /**
1866 * Implementation of ike_sa_t.rekey_child_sa.
1867 */
1868 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1869 {
1870 child_sa_t *child_sa;
1871 child_rekey_t *child_rekey;
1872
1873 child_sa = get_child_sa(this, protocol, spi, TRUE);
1874 if (child_sa)
1875 {
1876 child_rekey = child_rekey_create(&this->public, child_sa);
1877 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1878 return this->task_manager->initiate(this->task_manager);
1879 }
1880 return FAILED;
1881 }
1882
1883 /**
1884 * Implementation of ike_sa_t.delete_child_sa.
1885 */
1886 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1887 {
1888 child_sa_t *child_sa;
1889 child_delete_t *child_delete;
1890
1891 child_sa = get_child_sa(this, protocol, spi, TRUE);
1892 if (child_sa)
1893 {
1894 child_delete = child_delete_create(&this->public, child_sa);
1895 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1896 return this->task_manager->initiate(this->task_manager);
1897 }
1898 return FAILED;
1899 }
1900
1901 /**
1902 * Implementation of ike_sa_t.destroy_child_sa.
1903 */
1904 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1905 u_int32_t spi)
1906 {
1907 iterator_t *iterator;
1908 child_sa_t *child_sa;
1909 status_t status = NOT_FOUND;
1910
1911 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1912 while (iterator->iterate(iterator, (void**)&child_sa))
1913 {
1914 if (child_sa->get_protocol(child_sa) == protocol &&
1915 child_sa->get_spi(child_sa, TRUE) == spi)
1916 {
1917 child_sa->destroy(child_sa);
1918 iterator->remove(iterator);
1919 status = SUCCESS;
1920 break;
1921 }
1922 }
1923 iterator->destroy(iterator);
1924 return status;
1925 }
1926
1927 /**
1928 * Implementation of public_ike_sa_t.delete.
1929 */
1930 static status_t delete_(private_ike_sa_t *this)
1931 {
1932 ike_delete_t *ike_delete;
1933
1934 switch (this->state)
1935 {
1936 case IKE_ESTABLISHED:
1937 case IKE_REKEYING:
1938 ike_delete = ike_delete_create(&this->public, TRUE);
1939 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1940 return this->task_manager->initiate(this->task_manager);
1941 case IKE_CREATED:
1942 SIG_IKE(DOWN_SUCCESS, "deleting unestablished IKE_SA");
1943 break;
1944 default:
1945 SIG_IKE(DOWN_SUCCESS, "destroying IKE_SA in state %N "
1946 "without notification", ike_sa_state_names, this->state);
1947 break;
1948 }
1949 return DESTROY_ME;
1950 }
1951
1952 /**
1953 * Implementation of ike_sa_t.rekey.
1954 */
1955 static status_t rekey(private_ike_sa_t *this)
1956 {
1957 ike_rekey_t *ike_rekey;
1958
1959 ike_rekey = ike_rekey_create(&this->public, TRUE);
1960
1961 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1962 return this->task_manager->initiate(this->task_manager);
1963 }
1964
1965 /**
1966 * Implementation of ike_sa_t.reauth
1967 */
1968 static status_t reauth(private_ike_sa_t *this)
1969 {
1970 task_t *task;
1971
1972 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1973 * If the peer does not support RFC4478, there is no way to keep the
1974 * IKE_SA up. */
1975 if (!this->ike_initiator)
1976 {
1977 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1978 if (this->other_virtual_ip != NULL ||
1979 has_condition(this, COND_EAP_AUTHENTICATED)
1980 #ifdef ME
1981 /* if we are mediation server we too cannot reauth the IKE_SA */
1982 || this->is_mediation_server
1983 #endif /* ME */
1984 )
1985 {
1986 time_t now = time(NULL);
1987
1988 DBG1(DBG_IKE, "IKE_SA will timeout in %#V", &now, &this->time.delete);
1989 return FAILED;
1990 }
1991 else
1992 {
1993 DBG1(DBG_IKE, "reauthenticating actively");
1994 }
1995 }
1996 task = (task_t*)ike_reauth_create(&this->public);
1997 this->task_manager->queue_task(this->task_manager, task);
1998
1999 return this->task_manager->initiate(this->task_manager);
2000 }
2001
2002 /**
2003 * Implementation of ike_sa_t.reestablish
2004 */
2005 static status_t reestablish(private_ike_sa_t *this)
2006 {
2007 ike_sa_t *new;
2008 host_t *host;
2009 action_t action;
2010 iterator_t *iterator;
2011 child_sa_t *child_sa;
2012 child_cfg_t *child_cfg;
2013 bool required = FALSE;
2014 status_t status = FAILED;
2015
2016 /* check if we have children to keep up at all*/
2017 iterator = create_child_sa_iterator(this);
2018 while (iterator->iterate(iterator, (void**)&child_sa))
2019 {
2020 child_cfg = child_sa->get_config(child_sa);
2021 if (this->state == IKE_DELETING)
2022 {
2023 action = child_cfg->get_close_action(child_cfg);
2024 }
2025 else
2026 {
2027 action = child_cfg->get_dpd_action(child_cfg);
2028 }
2029 switch (action)
2030 {
2031 case ACTION_RESTART:
2032 case ACTION_ROUTE:
2033 required = TRUE;
2034 default:
2035 break;
2036 }
2037 }
2038 iterator->destroy(iterator);
2039 #ifdef ME
2040 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
2041 if (this->peer_cfg->is_mediation(this->peer_cfg))
2042 {
2043 required = TRUE;
2044 }
2045 #endif /* ME */
2046 if (!required)
2047 {
2048 return FAILED;
2049 }
2050
2051 /* check if we are able to reestablish this IKE_SA */
2052 if (!this->ike_initiator &&
2053 (this->other_virtual_ip != NULL ||
2054 has_condition(this, COND_EAP_AUTHENTICATED)
2055 #ifdef ME
2056 || this->is_mediation_server
2057 #endif /* ME */
2058 ))
2059 {
2060 DBG1(DBG_IKE, "unable to reestablish IKE_SA due asymetric setup");
2061 return FAILED;
2062 }
2063
2064 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager, TRUE);
2065 new->set_peer_cfg(new, this->peer_cfg);
2066 host = this->other_host;
2067 new->set_other_host(new, host->clone(host));
2068 host = this->my_host;
2069 new->set_my_host(new, host->clone(host));
2070 /* if we already have a virtual IP, we reuse it */
2071 host = this->my_virtual_ip;
2072 if (host)
2073 {
2074 new->set_virtual_ip(new, TRUE, host);
2075 }
2076
2077 #ifdef ME
2078 if (this->peer_cfg->is_mediation(this->peer_cfg))
2079 {
2080 status = new->initiate(new, NULL);
2081 }
2082 else
2083 #endif /* ME */
2084 {
2085 iterator = create_child_sa_iterator(this);
2086 while (iterator->iterate(iterator, (void**)&child_sa))
2087 {
2088 child_cfg = child_sa->get_config(child_sa);
2089 if (this->state == IKE_DELETING)
2090 {
2091 action = child_cfg->get_close_action(child_cfg);
2092 }
2093 else
2094 {
2095 action = child_cfg->get_dpd_action(child_cfg);
2096 }
2097 switch (action)
2098 {
2099 case ACTION_RESTART:
2100 DBG1(DBG_IKE, "restarting CHILD_SA %s",
2101 child_cfg->get_name(child_cfg));
2102 child_cfg->get_ref(child_cfg);
2103 status = new->initiate(new, child_cfg);
2104 break;
2105 case ACTION_ROUTE:
2106 status = new->route(new, child_cfg);
2107 break;
2108 default:
2109 continue;
2110 }
2111 if (status == DESTROY_ME)
2112 {
2113 break;
2114 }
2115 }
2116 iterator->destroy(iterator);
2117 }
2118
2119 if (status == DESTROY_ME)
2120 {
2121 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
2122 return FAILED;
2123 }
2124 else
2125 {
2126 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
2127 return SUCCESS;
2128 }
2129 }
2130
2131 /**
2132 * Implementation of ike_sa_t.retransmit.
2133 */
2134 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
2135 {
2136 this->time.outbound = time(NULL);
2137 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
2138 {
2139 /* send a proper signal to brief interested bus listeners */
2140 switch (this->state)
2141 {
2142 case IKE_CONNECTING:
2143 {
2144 /* retry IKE_SA_INIT if we have multiple keyingtries */
2145 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
2146 this->keyingtry++;
2147 if (tries == 0 || tries > this->keyingtry)
2148 {
2149 SIG_IKE(UP_FAILED, "peer not responding, trying again "
2150 "(%d/%d) in background ", this->keyingtry + 1, tries);
2151 reset(this);
2152 return this->task_manager->initiate(this->task_manager);
2153 }
2154 SIG_IKE(UP_FAILED, "establishing IKE_SA failed, peer not responding");
2155 break;
2156 }
2157 case IKE_DELETING:
2158 SIG_IKE(DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
2159 break;
2160 case IKE_REKEYING:
2161 SIG_IKE(REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
2162 /* FALL */
2163 default:
2164 reestablish(this);
2165 break;
2166 }
2167 return DESTROY_ME;
2168 }
2169 return SUCCESS;
2170 }
2171
2172 /**
2173 * Implementation of ike_sa_t.set_auth_lifetime.
2174 */
2175 static void set_auth_lifetime(private_ike_sa_t *this, u_int32_t lifetime)
2176 {
2177 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
2178 u_int32_t reauth_time = time(NULL) + lifetime - reduction;
2179
2180 if (lifetime < reduction)
2181 {
2182 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
2183 lifetime);
2184 charon->processor->queue_job(charon->processor,
2185 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
2186 }
2187 else if (this->time.reauth == 0 || this->time.reauth > reauth_time)
2188 {
2189 this->time.reauth = reauth_time;
2190 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
2191 " in %ds", lifetime, lifetime - reduction);
2192 charon->scheduler->schedule_job(charon->scheduler,
2193 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2194 (lifetime - reduction) * 1000);
2195 }
2196 else
2197 {
2198 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, reauthentication already "
2199 "scheduled in %ds", lifetime, this->time.reauth - time(NULL));
2200 }
2201 }
2202
2203 /**
2204 * Implementation of ike_sa_t.roam.
2205 */
2206 static status_t roam(private_ike_sa_t *this, bool address)
2207 {
2208 host_t *src;
2209 ike_mobike_t *mobike;
2210
2211 switch (this->state)
2212 {
2213 case IKE_CREATED:
2214 case IKE_DELETING:
2215 return SUCCESS;
2216 default:
2217 break;
2218 }
2219 /* responder just updates the peer about changed address config */
2220 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
2221 {
2222 if (supports_extension(this, EXT_MOBIKE) && address)
2223 {
2224 DBG1(DBG_IKE, "sending address list update using MOBIKE");
2225 mobike = ike_mobike_create(&this->public, TRUE);
2226 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
2227 return this->task_manager->initiate(this->task_manager);
2228 }
2229 return SUCCESS;
2230 }
2231
2232 /* keep existing path if possible */
2233 src = charon->kernel_interface->get_source_addr(charon->kernel_interface,
2234 this->other_host, this->my_host);
2235 if (src)
2236 {
2237 if (src->ip_equals(src, this->my_host))
2238 {
2239 DBG2(DBG_IKE, "keeping connection path %H - %H",
2240 src, this->other_host);
2241 src->destroy(src);
2242 return SUCCESS;
2243 }
2244 src->destroy(src);
2245 }
2246
2247 /* update addresses with mobike, if supported ... */
2248 if (supports_extension(this, EXT_MOBIKE))
2249 {
2250 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2251 mobike = ike_mobike_create(&this->public, TRUE);
2252 mobike->roam(mobike, address);
2253 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
2254 return this->task_manager->initiate(this->task_manager);
2255 }
2256 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
2257 /* ... reauth if not */
2258 return reauth(this);
2259 }
2260
2261 /**
2262 * Implementation of ike_sa_t.inherit.
2263 */
2264 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
2265 {
2266 child_sa_t *child_sa;
2267 host_t *ip;
2268
2269 /* apply hosts and ids */
2270 this->my_host->destroy(this->my_host);
2271 this->other_host->destroy(this->other_host);
2272 this->my_id->destroy(this->my_id);
2273 this->other_id->destroy(this->other_id);
2274 this->my_host = other->my_host->clone(other->my_host);
2275 this->other_host = other->other_host->clone(other->other_host);
2276 this->my_id = other->my_id->clone(other->my_id);
2277 this->other_id = other->other_id->clone(other->other_id);
2278 this->ike_initiator = other->ike_initiator;
2279
2280 /* apply virtual assigned IPs... */
2281 if (other->my_virtual_ip)
2282 {
2283 this->my_virtual_ip = other->my_virtual_ip;
2284 other->my_virtual_ip = NULL;
2285 }
2286 if (other->other_virtual_ip)
2287 {
2288 this->other_virtual_ip = other->other_virtual_ip;
2289 other->other_virtual_ip = NULL;
2290 }
2291
2292 /* ... and DNS servers */
2293 while (other->dns_servers->remove_last(other->dns_servers,
2294 (void**)&ip) == SUCCESS)
2295 {
2296 this->dns_servers->insert_first(this->dns_servers, ip);
2297 }
2298
2299 /* inherit NAT-T conditions */
2300 this->conditions = other->conditions;
2301 if (this->conditions & COND_NAT_HERE)
2302 {
2303 send_keepalive(this);
2304 }
2305
2306 #ifdef ME
2307 if (other->is_mediation_server)
2308 {
2309 act_as_mediation_server(this);
2310 }
2311 else if (other->server_reflexive_host)
2312 {
2313 this->server_reflexive_host = other->server_reflexive_host->clone(
2314 other->server_reflexive_host);
2315 }
2316 #endif /* ME */
2317
2318 /* adopt all children */
2319 while (other->child_sas->remove_last(other->child_sas,
2320 (void**)&child_sa) == SUCCESS)
2321 {
2322 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
2323 }
2324
2325 /* move pending tasks to the new IKE_SA */
2326 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2327
2328 /* reauthentication timeout survives a rekeying */
2329 if (other->time.reauth)
2330 {
2331 time_t reauth, delete, now = time(NULL);
2332
2333 this->time.reauth = other->time.reauth;
2334 reauth = this->time.reauth - now;
2335 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2336 this->time.delete = this->time.reauth + delete;
2337 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2338 "lifetime reduced to %ds", reauth, delete);
2339 charon->scheduler->schedule_job(charon->scheduler,
2340 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2341 reauth * 1000);
2342 charon->scheduler->schedule_job(charon->scheduler,
2343 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE),
2344 delete * 1000);
2345 }
2346 /* we have to initate here, there may be new tasks to handle */
2347 return this->task_manager->initiate(this->task_manager);
2348 }
2349
2350 /**
2351 * Implementation of ike_sa_t.remove_dns_server
2352 */
2353 static void remove_dns_servers(private_ike_sa_t *this)
2354 {
2355 FILE *file;
2356 struct stat stats;
2357 chunk_t contents, line, orig_line, token;
2358 char string[INET6_ADDRSTRLEN];
2359 host_t *ip;
2360 iterator_t *iterator;
2361
2362 if (this->dns_servers->get_count(this->dns_servers) == 0)
2363 {
2364 /* don't touch anything if we have no nameservers installed */
2365 return;
2366 }
2367
2368 file = fopen(RESOLV_CONF, "r");
2369 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
2370 {
2371 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2372 RESOLV_CONF, strerror(errno));
2373 return;
2374 }
2375
2376 contents = chunk_alloca((size_t)stats.st_size);
2377
2378 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
2379 {
2380 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
2381 fclose(file);
2382 return;
2383 }
2384
2385 fclose(file);
2386 file = fopen(RESOLV_CONF, "w");
2387 if (file == NULL)
2388 {
2389 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2390 RESOLV_CONF, strerror(errno));
2391 return;
2392 }
2393
2394 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
2395 while (fetchline(&contents, &line))
2396 {
2397 bool found = FALSE;
2398 orig_line = line;
2399 if (extract_token(&token, ' ', &line) &&
2400 strncasecmp(token.ptr, "nameserver", token.len) == 0)
2401 {
2402 if (!extract_token(&token, ' ', &line))
2403 {
2404 token = line;
2405 }
2406 iterator->reset(iterator);
2407 while (iterator->iterate(iterator, (void**)&ip))
2408 {
2409 snprintf(string, sizeof(string), "%H", ip);
2410 if (strlen(string) == token.len &&
2411 strncmp(token.ptr, string, token.len) == 0)
2412 {
2413 iterator->remove(iterator);
2414 ip->destroy(ip);
2415 found = TRUE;
2416 break;
2417 }
2418 }
2419 }
2420
2421 if (!found)
2422 {
2423 /* write line untouched back to file */
2424 fwrite(orig_line.ptr, orig_line.len, 1, file);
2425 fprintf(file, "\n");
2426 }
2427 }
2428 iterator->destroy(iterator);
2429 fclose(file);
2430 }
2431
2432 /**
2433 * Implementation of ike_sa_t.add_dns_server
2434 */
2435 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
2436 {
2437 FILE *file;
2438 struct stat stats;
2439 chunk_t contents;
2440
2441 DBG1(DBG_IKE, "installing DNS server %H", dns);
2442
2443 file = fopen(RESOLV_CONF, "a+");
2444 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
2445 {
2446 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2447 RESOLV_CONF, strerror(errno));
2448 return;
2449 }
2450
2451 contents = chunk_alloca(stats.st_size);
2452
2453 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
2454 {
2455 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
2456 fclose(file);
2457 return;
2458 }
2459
2460 fclose(file);
2461 file = fopen(RESOLV_CONF, "w");
2462 if (file == NULL)
2463 {
2464 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2465 RESOLV_CONF, strerror(errno));
2466 return;
2467 }
2468
2469 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
2470 dns, this->other_id) < 0)
2471 {
2472 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
2473 }
2474 else
2475 {
2476 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
2477 }
2478 fwrite(contents.ptr, contents.len, 1, file);
2479
2480 fclose(file);
2481 }
2482
2483 /**
2484 * Implementation of ike_sa_t.destroy.
2485 */
2486 static void destroy(private_ike_sa_t *this)
2487 {
2488 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
2489
2490 this->task_manager->destroy(this->task_manager);
2491
2492 DESTROY_IF(this->crypter_in);
2493 DESTROY_IF(this->crypter_out);
2494 DESTROY_IF(this->signer_in);
2495 DESTROY_IF(this->signer_out);
2496 DESTROY_IF(this->prf);
2497 DESTROY_IF(this->child_prf);
2498 chunk_free(&this->skp_verify);
2499 chunk_free(&this->skp_build);
2500 free(this->selected_proposal);
2501
2502 if (this->my_virtual_ip)
2503 {
2504 charon->kernel_interface->del_ip(charon->kernel_interface,
2505 this->my_virtual_ip);
2506 this->my_virtual_ip->destroy(this->my_virtual_ip);
2507 }
2508 if (this->other_virtual_ip)
2509 {
2510 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
2511 {
2512 charon->attributes->release_address(charon->attributes,
2513 this->peer_cfg->get_pool(this->peer_cfg),
2514 this->other_virtual_ip);
2515 }
2516 this->other_virtual_ip->destroy(this->other_virtual_ip);
2517 }
2518
2519 remove_dns_servers(this);
2520 this->dns_servers->destroy_offset(this->dns_servers,
2521 offsetof(host_t, destroy));
2522 this->additional_addresses->destroy_offset(this->additional_addresses,
2523 offsetof(host_t, destroy));
2524 #ifdef ME
2525 if (this->is_mediation_server)
2526 {
2527 charon->mediation_manager->remove(charon->mediation_manager, this->ike_sa_id);
2528 }
2529 DESTROY_IF(this->server_reflexive_host);
2530 chunk_free(&this->connect_id);
2531 #endif /* ME */
2532 free(this->nat_detection_dest.ptr);
2533
2534 DESTROY_IF(this->my_host);
2535 DESTROY_IF(this->other_host);
2536 DESTROY_IF(this->my_id);
2537 DESTROY_IF(this->other_id);
2538 DESTROY_IF(this->eap_identity);
2539
2540 DESTROY_IF(this->ike_cfg);
2541 DESTROY_IF(this->peer_cfg);
2542 DESTROY_IF(this->my_auth);
2543 DESTROY_IF(this->other_auth);
2544
2545 this->ike_sa_id->destroy(this->ike_sa_id);
2546 free(this);
2547 }
2548
2549 /*
2550 * Described in header.
2551 */
2552 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2553 {
2554 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2555 static u_int32_t unique_id = 0;
2556
2557 /* Public functions */
2558 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
2559 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
2560 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2561 this->public.get_statistic = (u_int32_t(*)(ike_sa_t*, statistic_t kind))get_statistic;
2562 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2563 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2564 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2565 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2566 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2567 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2568 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2569 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2570 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2571 this->public.get_my_auth = (auth_info_t*(*)(ike_sa_t*))get_my_auth;
2572 this->public.get_other_auth = (auth_info_t*(*)(ike_sa_t*))get_other_auth;
2573 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2574 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2575 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2576 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2577 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2578 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2579 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2580 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2581 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2582 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2583 this->public.get_eap_identity = (identification_t* (*)(ike_sa_t*)) get_eap_identity;
2584 this->public.set_eap_identity = (void (*)(ike_sa_t*,identification_t*)) set_eap_identity;
2585 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2586 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2587 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2588 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2589 this->public.set_pending_updates = (void(*)(ike_sa_t*, u_int32_t updates))set_pending_updates;
2590 this->public.get_pending_updates = (u_int32_t(*)(ike_sa_t*))get_pending_updates;
2591 this->public.is_ike_initiator = (bool (*)(ike_sa_t*))is_ike_initiator;
2592 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2593 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2594 this->public.has_mapping_changed = (bool(*)(ike_sa_t*, chunk_t hash))has_mapping_changed;
2595 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2596 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2597 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2598 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2599 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2600 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
2601 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
2602 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
2603 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
2604 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2605 this->public.get_proposal = (char* (*)(ike_sa_t*)) get_proposal;
2606 this->public.set_proposal = (void (*)(ike_sa_t*,char*)) set_proposal;
2607 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2608 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2609 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2610 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2611 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2612 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2613 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2614 this->public.reauth = (status_t (*)(ike_sa_t*))reauth;
2615 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2616 this->public.set_auth_lifetime = (void(*)(ike_sa_t*, u_int32_t lifetime))set_auth_lifetime;
2617 this->public.roam = (status_t(*)(ike_sa_t*,bool))roam;
2618 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2619 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2620 this->public.reset = (void (*)(ike_sa_t*))reset;
2621 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2622 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2623 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2624 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2625 #ifdef ME
2626 this->public.act_as_mediation_server = (void (*)(ike_sa_t*)) act_as_mediation_server;
2627 this->public.get_server_reflexive_host = (host_t* (*)(ike_sa_t*)) get_server_reflexive_host;
2628 this->public.set_server_reflexive_host = (void (*)(ike_sa_t*,host_t*)) set_server_reflexive_host;
2629 this->public.get_connect_id = (chunk_t (*)(ike_sa_t*)) get_connect_id;
2630 this->public.initiate_mediation = (status_t (*)(ike_sa_t*,peer_cfg_t*)) initiate_mediation;
2631 this->public.initiate_mediated = (status_t (*)(ike_sa_t*,host_t*,host_t*,chunk_t)) initiate_mediated;
2632 this->public.relay = (status_t (*)(ike_sa_t*,identification_t*,chunk_t,chunk_t,linked_list_t*,bool)) relay;
2633 this->public.callback = (status_t (*)(ike_sa_t*,identification_t*)) callback;
2634 this->public.respond = (status_t (*)(ike_sa_t*,identification_t*,chunk_t)) respond;
2635 #endif /* ME */
2636
2637 /* initialize private fields */
2638 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2639 this->child_sas = linked_list_create();
2640 this->my_host = host_create_from_string("0.0.0.0", IKEV2_UDP_PORT);
2641 this->other_host = host_create_from_string("0.0.0.0", IKEV2_UDP_PORT);
2642 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2643 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2644 this->eap_identity = NULL;
2645 this->extensions = 0;
2646 this->conditions = 0;
2647 this->selected_proposal = NULL;
2648 this->crypter_in = NULL;
2649 this->crypter_out = NULL;
2650 this->signer_in = NULL;
2651 this->signer_out = NULL;
2652 this->prf = NULL;
2653 this->skp_verify = chunk_empty;
2654 this->skp_build = chunk_empty;
2655 this->child_prf = NULL;
2656 this->state = IKE_CREATED;
2657 this->keepalive_interval = lib->settings->get_time(lib->settings,
2658 "charon.keep_alive", KEEPALIVE_INTERVAL);
2659 this->time.inbound = this->time.outbound = time(NULL);
2660 this->time.established = 0;
2661 this->time.rekey = 0;
2662 this->time.reauth = 0;
2663 this->time.delete = 0;
2664 this->ike_cfg = NULL;
2665 this->peer_cfg = NULL;
2666 this->my_auth = auth_info_create();
2667 this->other_auth = auth_info_create();
2668 this->task_manager = task_manager_create(&this->public);
2669 this->unique_id = ++unique_id;
2670 this->my_virtual_ip = NULL;
2671 this->other_virtual_ip = NULL;
2672 this->dns_servers = linked_list_create();
2673 this->additional_addresses = linked_list_create();
2674 this->nat_detection_dest = chunk_empty;
2675 this->pending_updates = 0;
2676 this->keyingtry = 0;
2677 this->ike_initiator = FALSE;
2678 #ifdef ME
2679 this->is_mediation_server = FALSE;
2680 this->server_reflexive_host = NULL;
2681 this->connect_id = chunk_empty;
2682 #endif /* ME */
2683
2684 return &this->public;
2685 }