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