do a route lookup to allow routing of left=%any connections
[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_other_addr(this->ike_cfg),
1045 0, IKEV2_UDP_PORT);
1046 if (host)
1047 {
1048 set_other_host(this, host);
1049 }
1050
1051 host = host_create_from_dns(this->ike_cfg->get_my_addr(this->ike_cfg),
1052 this->my_host->get_family(this->my_host),
1053 IKEV2_UDP_PORT);
1054
1055 if (host && host->is_anyaddr(host) &&
1056 !this->other_host->is_anyaddr(this->other_host))
1057 {
1058 host->destroy(host);
1059 host = charon->kernel_interface->get_source_addr(
1060 charon->kernel_interface, this->other_host);
1061 host->set_port(host, IKEV2_UDP_PORT);
1062 }
1063 if (host)
1064 {
1065 set_my_host(this, host);
1066 }
1067 }
1068
1069 /**
1070 * Initiates a CHILD_SA using the appropriate reqid
1071 */
1072 static status_t initiate_with_reqid(private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid)
1073 {
1074 task_t *task;
1075
1076 if (this->state == IKE_CREATED)
1077 {
1078 resolve_hosts(this);
1079
1080 if (this->other_host->is_anyaddr(this->other_host)
1081 #ifdef ME
1082 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1083 #endif /* ME */
1084 )
1085 {
1086 child_cfg->destroy(child_cfg);
1087 SIG(IKE_UP_START, "initiating IKE_SA");
1088 SIG(IKE_UP_FAILED, "unable to initiate to %%any");
1089 return DESTROY_ME;
1090 }
1091
1092 this->ike_initiator = TRUE;
1093
1094 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1095 this->task_manager->queue_task(this->task_manager, task);
1096 task = (task_t*)ike_natd_create(&this->public, TRUE);
1097 this->task_manager->queue_task(this->task_manager, task);
1098 task = (task_t*)ike_cert_pre_create(&this->public, TRUE);
1099 this->task_manager->queue_task(this->task_manager, task);
1100 task = (task_t*)ike_auth_create(&this->public, TRUE);
1101 this->task_manager->queue_task(this->task_manager, task);
1102 task = (task_t*)ike_cert_post_create(&this->public, TRUE);
1103 this->task_manager->queue_task(this->task_manager, task);
1104 task = (task_t*)ike_config_create(&this->public, TRUE);
1105 this->task_manager->queue_task(this->task_manager, task);
1106 task = (task_t*)ike_auth_lifetime_create(&this->public, TRUE);
1107 this->task_manager->queue_task(this->task_manager, task);
1108 if (this->peer_cfg->use_mobike(this->peer_cfg))
1109 {
1110 task = (task_t*)ike_mobike_create(&this->public, TRUE);
1111 this->task_manager->queue_task(this->task_manager, task);
1112 }
1113 #ifdef ME
1114 task = (task_t*)ike_me_create(&this->public, TRUE);
1115 this->task_manager->queue_task(this->task_manager, task);
1116 #endif /* ME */
1117 }
1118
1119 #ifdef ME
1120 if (this->peer_cfg->is_mediation(this->peer_cfg))
1121 {
1122 /* mediation connection */
1123 if (this->state == IKE_ESTABLISHED)
1124 { /* FIXME: we should try to find a better solution to this */
1125 SIG(CHILD_UP_SUCCESS, "mediation connection is already up and running");
1126 }
1127 DESTROY_IF(child_cfg);
1128 }
1129 else
1130 #endif /* ME */
1131 {
1132 /* normal IKE_SA with CHILD_SA */
1133 task = (task_t*)child_create_create(&this->public, child_cfg);
1134 child_cfg->destroy(child_cfg);
1135 if (reqid)
1136 {
1137 child_create_t *child_create = (child_create_t*)task;
1138 child_create->use_reqid(child_create, reqid);
1139 }
1140 this->task_manager->queue_task(this->task_manager, task);
1141
1142 #ifdef ME
1143 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1144 {
1145 /* mediated connection, initiate mediation process */
1146 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1147 charon->processor->queue_job(charon->processor, job);
1148 return SUCCESS;
1149 }
1150 #endif /* ME */
1151 }
1152
1153 return this->task_manager->initiate(this->task_manager);
1154 }
1155
1156 /**
1157 * Implementation of ike_sa_t.initiate.
1158 */
1159 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
1160 {
1161 return initiate_with_reqid(this, child_cfg, 0);
1162 }
1163
1164 /**
1165 * Implementation of ike_sa_t.acquire.
1166 */
1167 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
1168 {
1169 child_cfg_t *child_cfg;
1170 iterator_t *iterator;
1171 child_sa_t *current, *child_sa = NULL;
1172
1173 if (this->state == IKE_DELETING)
1174 {
1175 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
1176 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
1177 "IKE_SA is deleting", reqid);
1178 return FAILED;
1179 }
1180
1181 /* find CHILD_SA */
1182 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1183 while (iterator->iterate(iterator, (void**)&current))
1184 {
1185 if (current->get_reqid(current) == reqid)
1186 {
1187 child_sa = current;
1188 break;
1189 }
1190 }
1191 iterator->destroy(iterator);
1192 if (!child_sa)
1193 {
1194 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
1195 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
1196 "CHILD_SA not found", reqid);
1197 return FAILED;
1198 }
1199
1200 child_cfg = child_sa->get_config(child_sa);
1201 child_cfg->get_ref(child_cfg);
1202
1203 return initiate_with_reqid(this, child_cfg, reqid);
1204 }
1205
1206 /**
1207 * Implementation of ike_sa_t.route.
1208 */
1209 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
1210 {
1211 child_sa_t *child_sa;
1212 iterator_t *iterator;
1213 linked_list_t *my_ts, *other_ts;
1214 host_t *me, *other;
1215 status_t status;
1216
1217 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
1218
1219 /* check if not already routed*/
1220 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1221 while (iterator->iterate(iterator, (void**)&child_sa))
1222 {
1223 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1224 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
1225 {
1226 iterator->destroy(iterator);
1227 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a config already routed");
1228 return FAILED;
1229 }
1230 }
1231 iterator->destroy(iterator);
1232
1233 switch (this->state)
1234 {
1235 case IKE_DELETING:
1236 case IKE_REKEYING:
1237 SIG(CHILD_ROUTE_FAILED,
1238 "unable to route CHILD_SA, as its IKE_SA gets deleted");
1239 return FAILED;
1240 case IKE_CREATED:
1241 case IKE_CONNECTING:
1242 case IKE_ESTABLISHED:
1243 default:
1244 break;
1245 }
1246
1247 resolve_hosts(this);
1248
1249 /* install kernel policies */
1250 child_sa = child_sa_create(this->my_host, this->other_host, this->my_id,
1251 this->other_id, child_cfg, 0, FALSE);
1252 me = this->my_host;
1253 if (this->my_virtual_ip)
1254 {
1255 me = this->my_virtual_ip;
1256 }
1257 other = this->other_host;
1258 if (this->other_virtual_ip)
1259 {
1260 other = this->other_virtual_ip;
1261 }
1262
1263 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, me);
1264 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, other);
1265 status = child_sa->add_policies(child_sa, my_ts, other_ts,
1266 child_cfg->get_mode(child_cfg));
1267 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1268 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1269 if (status == SUCCESS)
1270 {
1271 this->child_sas->insert_last(this->child_sas, child_sa);
1272 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
1273 }
1274 else
1275 {
1276 SIG(CHILD_ROUTE_FAILED, "routing CHILD_SA failed");
1277 }
1278 return status;
1279 }
1280
1281 /**
1282 * Implementation of ike_sa_t.unroute.
1283 */
1284 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
1285 {
1286 iterator_t *iterator;
1287 child_sa_t *child_sa;
1288 bool found = FALSE;
1289
1290 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
1291
1292 /* find CHILD_SA in ROUTED state */
1293 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1294 while (iterator->iterate(iterator, (void**)&child_sa))
1295 {
1296 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1297 child_sa->get_reqid(child_sa) == reqid)
1298 {
1299 iterator->remove(iterator);
1300 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
1301 child_sa->destroy(child_sa);
1302 found = TRUE;
1303 break;
1304 }
1305 }
1306 iterator->destroy(iterator);
1307
1308 if (!found)
1309 {
1310 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
1311 return FAILED;
1312 }
1313 /* if we are not established, and we have no more routed childs, remove whole SA */
1314 if (this->state == IKE_CREATED &&
1315 this->child_sas->get_count(this->child_sas) == 0)
1316 {
1317 return DESTROY_ME;
1318 }
1319 return SUCCESS;
1320 }
1321 /**
1322 * Implementation of ike_sa_t.process_message.
1323 */
1324 static status_t process_message(private_ike_sa_t *this, message_t *message)
1325 {
1326 status_t status;
1327 bool is_request;
1328
1329 is_request = message->get_request(message);
1330
1331 status = message->parse_body(message, this->crypter_in, this->signer_in);
1332 if (status != SUCCESS)
1333 {
1334
1335 if (is_request)
1336 {
1337 switch (status)
1338 {
1339 case NOT_SUPPORTED:
1340 DBG1(DBG_IKE, "ciritcal unknown payloads found");
1341 if (is_request)
1342 {
1343 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
1344 }
1345 break;
1346 case PARSE_ERROR:
1347 DBG1(DBG_IKE, "message parsing failed");
1348 if (is_request)
1349 {
1350 send_notify_response(this, message, INVALID_SYNTAX);
1351 }
1352 break;
1353 case VERIFY_ERROR:
1354 DBG1(DBG_IKE, "message verification failed");
1355 if (is_request)
1356 {
1357 send_notify_response(this, message, INVALID_SYNTAX);
1358 }
1359 break;
1360 case FAILED:
1361 DBG1(DBG_IKE, "integrity check failed");
1362 /* ignored */
1363 break;
1364 case INVALID_STATE:
1365 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1366 if (is_request)
1367 {
1368 send_notify_response(this, message, INVALID_SYNTAX);
1369 }
1370 default:
1371 break;
1372 }
1373 }
1374 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1375 exchange_type_names, message->get_exchange_type(message),
1376 message->get_request(message) ? "request" : "response",
1377 message->get_message_id(message));
1378 return status;
1379 }
1380 else
1381 {
1382 host_t *me, *other;
1383 private_ike_sa_t *new;
1384 iterator_t *iterator;
1385 child_sa_t *child;
1386 bool has_routed = FALSE;
1387
1388 me = message->get_destination(message);
1389 other = message->get_source(message);
1390
1391 /* if this IKE_SA is virgin, we check for a config */
1392 if (this->ike_cfg == NULL)
1393 {
1394 job_t *job;
1395 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1396 me, other);
1397 if (this->ike_cfg == NULL)
1398 {
1399 /* no config found for these hosts, destroy */
1400 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1401 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1402 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
1403 return DESTROY_ME;
1404 }
1405 /* add a timeout if peer does not establish it completely */
1406 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
1407 charon->scheduler->schedule_job(charon->scheduler, job,
1408 HALF_OPEN_IKE_SA_TIMEOUT);
1409 }
1410
1411 /* check if message is trustworthy, and update host information */
1412 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
1413 message->get_exchange_type(message) != IKE_SA_INIT)
1414 {
1415 update_hosts(this, me, other);
1416 this->time.inbound = time(NULL);
1417 }
1418 status = this->task_manager->process_message(this->task_manager, message);
1419 if (status != DESTROY_ME)
1420 {
1421 return status;
1422 }
1423 /* if IKE_SA gets closed for any reasons, reroute routed children */
1424 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1425 while (iterator->iterate(iterator, (void**)&child))
1426 {
1427 if (child->get_state(child) == CHILD_ROUTED)
1428 {
1429 has_routed = TRUE;
1430 break;
1431 }
1432 }
1433 iterator->destroy(iterator);
1434 if (!has_routed)
1435 {
1436 return status;
1437 }
1438 /* move routed children to a new IKE_SA, apply connection info */
1439 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1440 charon->ike_sa_manager, TRUE);
1441 set_peer_cfg(new, this->peer_cfg);
1442 new->other_host->destroy(new->other_host);
1443 new->other_host = this->other_host->clone(this->other_host);
1444 if (!has_condition(this, COND_NAT_THERE))
1445 {
1446 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1447 }
1448 if (this->my_virtual_ip)
1449 {
1450 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1451 }
1452 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1453 while (iterator->iterate(iterator, (void**)&child))
1454 {
1455 if (child->get_state(child) == CHILD_ROUTED)
1456 {
1457 route(new, child->get_config(child));
1458 }
1459 }
1460 iterator->destroy(iterator);
1461 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1462 return status;
1463 }
1464 }
1465
1466 /**
1467 * Implementation of ike_sa_t.get_prf.
1468 */
1469 static prf_t *get_prf(private_ike_sa_t *this)
1470 {
1471 return this->prf;
1472 }
1473
1474 /**
1475 * Implementation of ike_sa_t.get_prf.
1476 */
1477 static prf_t *get_child_prf(private_ike_sa_t *this)
1478 {
1479 return this->child_prf;
1480 }
1481
1482 /**
1483 * Implementation of ike_sa_t.get_skp_bild
1484 */
1485 static chunk_t get_skp_build(private_ike_sa_t *this)
1486 {
1487 return this->skp_build;
1488 }
1489
1490 /**
1491 * Implementation of ike_sa_t.get_skp_verify
1492 */
1493 static chunk_t get_skp_verify(private_ike_sa_t *this)
1494 {
1495 return this->skp_verify;
1496 }
1497
1498 /**
1499 * Implementation of ike_sa_t.get_id.
1500 */
1501 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1502 {
1503 return this->ike_sa_id;
1504 }
1505
1506 /**
1507 * Implementation of ike_sa_t.get_my_id.
1508 */
1509 static identification_t* get_my_id(private_ike_sa_t *this)
1510 {
1511 return this->my_id;
1512 }
1513
1514 /**
1515 * Implementation of ike_sa_t.set_my_id.
1516 */
1517 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1518 {
1519 DESTROY_IF(this->my_id);
1520 this->my_id = me;
1521 }
1522
1523 /**
1524 * Implementation of ike_sa_t.get_other_id.
1525 */
1526 static identification_t* get_other_id(private_ike_sa_t *this)
1527 {
1528 return this->other_id;
1529 }
1530
1531 /**
1532 * Implementation of ike_sa_t.set_other_id.
1533 */
1534 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1535 {
1536 DESTROY_IF(this->other_id);
1537 this->other_id = other;
1538 }
1539
1540 /**
1541 * Implementation of ike_sa_t.derive_keys.
1542 */
1543 static status_t derive_keys(private_ike_sa_t *this,
1544 proposal_t *proposal, chunk_t secret,
1545 chunk_t nonce_i, chunk_t nonce_r,
1546 bool initiator, prf_t *child_prf, prf_t *old_prf)
1547 {
1548 prf_plus_t *prf_plus;
1549 chunk_t skeyseed, key, full_nonce, fixed_nonce, prf_plus_seed;
1550 u_int16_t alg, key_size;
1551 crypter_t *crypter_i, *crypter_r;
1552 signer_t *signer_i, *signer_r;
1553 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1554 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1555 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1556
1557 /* Create SAs general purpose PRF first, we may use it here */
1558 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL))
1559 {
1560 DBG1(DBG_IKE, "no %N selected",
1561 transform_type_names, PSEUDO_RANDOM_FUNCTION);
1562 return FAILED;
1563 }
1564 this->prf = lib->crypto->create_prf(lib->crypto, alg);
1565 if (this->prf == NULL)
1566 {
1567 DBG1(DBG_IKE, "%N %N not supported!",
1568 transform_type_names, PSEUDO_RANDOM_FUNCTION,
1569 pseudo_random_function_names, alg);
1570 return FAILED;
1571 }
1572 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1573 /* full nonce is used as seed for PRF+ ... */
1574 full_nonce = chunk_cat("cc", nonce_i, nonce_r);
1575 /* but the PRF may need a fixed key which only uses the first bytes of
1576 * the nonces. */
1577 switch (alg)
1578 {
1579 case PRF_AES128_XCBC:
1580 /* while rfc4434 defines variable keys for AES-XCBC, rfc3664 does
1581 * not and therefore fixed key semantics apply to XCBC for key
1582 * derivation. */
1583 nonce_i.len = min(nonce_i.len, this->prf->get_key_size(this->prf)/2);
1584 nonce_r.len = min(nonce_r.len, this->prf->get_key_size(this->prf)/2);
1585 break;
1586 default:
1587 /* all other algorithms use variable key length, full nonce */
1588 break;
1589 }
1590 fixed_nonce = chunk_cat("cc", nonce_i, nonce_r);
1591 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1592 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1593 prf_plus_seed = chunk_cat("ccc", full_nonce, spi_i, spi_r);
1594
1595 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1596 *
1597 * if we are rekeying, SKEYSEED is built on another way
1598 */
1599 if (child_prf == NULL) /* not rekeying */
1600 {
1601 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1602 this->prf->set_key(this->prf, fixed_nonce);
1603 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1604 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1605 this->prf->set_key(this->prf, skeyseed);
1606 chunk_free(&skeyseed);
1607 chunk_free(&secret);
1608 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1609 }
1610 else
1611 {
1612 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1613 * use OLD SAs PRF functions for both prf_plus and prf */
1614 secret = chunk_cat("mc", secret, full_nonce);
1615 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1616 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1617 old_prf->set_key(old_prf, skeyseed);
1618 chunk_free(&skeyseed);
1619 chunk_free(&secret);
1620 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1621 }
1622 chunk_free(&full_nonce);
1623 chunk_free(&fixed_nonce);
1624 chunk_free(&prf_plus_seed);
1625
1626 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1627
1628 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1629 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL);
1630 this->child_prf = lib->crypto->create_prf(lib->crypto, alg);
1631 key_size = this->child_prf->get_key_size(this->child_prf);
1632 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1633 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1634 this->child_prf->set_key(this->child_prf, key);
1635 chunk_free(&key);
1636
1637 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1638 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, NULL))
1639 {
1640 DBG1(DBG_IKE, "no %N selected",
1641 transform_type_names, INTEGRITY_ALGORITHM);
1642 return FAILED;
1643 }
1644 signer_i = lib->crypto->create_signer(lib->crypto, alg);
1645 signer_r = lib->crypto->create_signer(lib->crypto, alg);
1646 if (signer_i == NULL || signer_r == NULL)
1647 {
1648 DBG1(DBG_IKE, "%N %N not supported!",
1649 transform_type_names, INTEGRITY_ALGORITHM,
1650 integrity_algorithm_names ,alg);
1651 prf_plus->destroy(prf_plus);
1652 return FAILED;
1653 }
1654 key_size = signer_i->get_key_size(signer_i);
1655
1656 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1657 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1658 signer_i->set_key(signer_i, key);
1659 chunk_free(&key);
1660
1661 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1662 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1663 signer_r->set_key(signer_r, key);
1664 chunk_free(&key);
1665
1666 if (initiator)
1667 {
1668 this->signer_in = signer_r;
1669 this->signer_out = signer_i;
1670 }
1671 else
1672 {
1673 this->signer_in = signer_i;
1674 this->signer_out = signer_r;
1675 }
1676
1677 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1678 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &key_size))
1679 {
1680 DBG1(DBG_IKE, "no %N selected",
1681 transform_type_names, ENCRYPTION_ALGORITHM);
1682 prf_plus->destroy(prf_plus);
1683 return FAILED;
1684 }
1685 crypter_i = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
1686 crypter_r = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
1687 if (crypter_i == NULL || crypter_r == NULL)
1688 {
1689 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
1690 transform_type_names, ENCRYPTION_ALGORITHM,
1691 encryption_algorithm_names, alg, key_size);
1692 prf_plus->destroy(prf_plus);
1693 return FAILED;
1694 }
1695 key_size = crypter_i->get_key_size(crypter_i);
1696
1697 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1698 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1699 crypter_i->set_key(crypter_i, key);
1700 chunk_free(&key);
1701
1702 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1703 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1704 crypter_r->set_key(crypter_r, key);
1705 chunk_free(&key);
1706
1707 if (initiator)
1708 {
1709 this->crypter_in = crypter_r;
1710 this->crypter_out = crypter_i;
1711 }
1712 else
1713 {
1714 this->crypter_in = crypter_i;
1715 this->crypter_out = crypter_r;
1716 }
1717
1718 /* SK_pi/SK_pr used for authentication => stored for later */
1719 key_size = this->prf->get_key_size(this->prf);
1720 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1721 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1722 if (initiator)
1723 {
1724 this->skp_build = key;
1725 }
1726 else
1727 {
1728 this->skp_verify = key;
1729 }
1730 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1731 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1732 if (initiator)
1733 {
1734 this->skp_verify = key;
1735 }
1736 else
1737 {
1738 this->skp_build = key;
1739 }
1740
1741 /* all done, prf_plus not needed anymore */
1742 prf_plus->destroy(prf_plus);
1743
1744 return SUCCESS;
1745 }
1746
1747 /**
1748 * Implementation of ike_sa_t.get_proposal.
1749 */
1750 static char* get_proposal(private_ike_sa_t *this)
1751 {
1752 return this->selected_proposal;
1753 }
1754
1755 /**
1756 * Implementation of ike_sa_t.set_proposal.
1757 */
1758 static void set_proposal(private_ike_sa_t *this, char *proposal)
1759 {
1760 free(this->selected_proposal);
1761 this->selected_proposal = strdup(proposal);
1762 }
1763
1764 /**
1765 * Implementation of ike_sa_t.add_child_sa.
1766 */
1767 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1768 {
1769 this->child_sas->insert_last(this->child_sas, child_sa);
1770 }
1771
1772 /**
1773 * Implementation of ike_sa_t.get_child_sa.
1774 */
1775 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1776 u_int32_t spi, bool inbound)
1777 {
1778 iterator_t *iterator;
1779 child_sa_t *current, *found = NULL;
1780
1781 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1782 while (iterator->iterate(iterator, (void**)&current))
1783 {
1784 if (current->get_spi(current, inbound) == spi &&
1785 current->get_protocol(current) == protocol)
1786 {
1787 found = current;
1788 }
1789 }
1790 iterator->destroy(iterator);
1791 return found;
1792 }
1793
1794 /**
1795 * Implementation of ike_sa_t.create_child_sa_iterator.
1796 */
1797 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1798 {
1799 return this->child_sas->create_iterator(this->child_sas, TRUE);
1800 }
1801
1802 /**
1803 * Implementation of ike_sa_t.rekey_child_sa.
1804 */
1805 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1806 {
1807 child_sa_t *child_sa;
1808 child_rekey_t *child_rekey;
1809
1810 child_sa = get_child_sa(this, protocol, spi, TRUE);
1811 if (child_sa)
1812 {
1813 child_rekey = child_rekey_create(&this->public, child_sa);
1814 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1815 return this->task_manager->initiate(this->task_manager);
1816 }
1817 return FAILED;
1818 }
1819
1820 /**
1821 * Implementation of ike_sa_t.delete_child_sa.
1822 */
1823 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1824 {
1825 child_sa_t *child_sa;
1826 child_delete_t *child_delete;
1827
1828 child_sa = get_child_sa(this, protocol, spi, TRUE);
1829 if (child_sa)
1830 {
1831 child_delete = child_delete_create(&this->public, child_sa);
1832 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1833 return this->task_manager->initiate(this->task_manager);
1834 }
1835 return FAILED;
1836 }
1837
1838 /**
1839 * Implementation of ike_sa_t.destroy_child_sa.
1840 */
1841 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1842 u_int32_t spi)
1843 {
1844 iterator_t *iterator;
1845 child_sa_t *child_sa;
1846 status_t status = NOT_FOUND;
1847
1848 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1849 while (iterator->iterate(iterator, (void**)&child_sa))
1850 {
1851 if (child_sa->get_protocol(child_sa) == protocol &&
1852 child_sa->get_spi(child_sa, TRUE) == spi)
1853 {
1854 child_sa->destroy(child_sa);
1855 iterator->remove(iterator);
1856 status = SUCCESS;
1857 break;
1858 }
1859 }
1860 iterator->destroy(iterator);
1861 return status;
1862 }
1863
1864 /**
1865 * Implementation of public_ike_sa_t.delete.
1866 */
1867 static status_t delete_(private_ike_sa_t *this)
1868 {
1869 ike_delete_t *ike_delete;
1870
1871 switch (this->state)
1872 {
1873 case IKE_ESTABLISHED:
1874 case IKE_REKEYING:
1875 ike_delete = ike_delete_create(&this->public, TRUE);
1876 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1877 return this->task_manager->initiate(this->task_manager);
1878 case IKE_CREATED:
1879 SIG(IKE_DOWN_SUCCESS, "deleting unestablished IKE_SA");
1880 break;
1881 default:
1882 SIG(IKE_DOWN_SUCCESS, "destroying IKE_SA in state %N "
1883 "without notification", ike_sa_state_names, this->state);
1884 break;
1885 }
1886 return DESTROY_ME;
1887 }
1888
1889 /**
1890 * Implementation of ike_sa_t.rekey.
1891 */
1892 static status_t rekey(private_ike_sa_t *this)
1893 {
1894 ike_rekey_t *ike_rekey;
1895
1896 ike_rekey = ike_rekey_create(&this->public, TRUE);
1897
1898 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1899 return this->task_manager->initiate(this->task_manager);
1900 }
1901
1902 /**
1903 * Implementation of ike_sa_t.reauth
1904 */
1905 static status_t reauth(private_ike_sa_t *this)
1906 {
1907 task_t *task;
1908
1909 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1910 * If the peer does not support RFC4478, there is no way to keep the
1911 * IKE_SA up. */
1912 if (!this->ike_initiator)
1913 {
1914 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1915 if (this->other_virtual_ip != NULL ||
1916 has_condition(this, COND_EAP_AUTHENTICATED)
1917 #ifdef ME
1918 /* if we are mediation server we too cannot reauth the IKE_SA */
1919 || this->is_mediation_server
1920 #endif /* ME */
1921 )
1922 {
1923 time_t now = time(NULL);
1924
1925 DBG1(DBG_IKE, "IKE_SA will timeout in %#V", &now, &this->time.delete);
1926 return FAILED;
1927 }
1928 else
1929 {
1930 DBG1(DBG_IKE, "reauthenticating actively");
1931 }
1932 }
1933 task = (task_t*)ike_reauth_create(&this->public);
1934 this->task_manager->queue_task(this->task_manager, task);
1935
1936 return this->task_manager->initiate(this->task_manager);
1937 }
1938
1939 /**
1940 * Implementation of ike_sa_t.reestablish
1941 */
1942 static status_t reestablish(private_ike_sa_t *this)
1943 {
1944 ike_sa_t *new;
1945 host_t *host;
1946 action_t action;
1947 iterator_t *iterator;
1948 child_sa_t *child_sa;
1949 child_cfg_t *child_cfg;
1950 bool required = FALSE;
1951 status_t status = FAILED;
1952
1953 /* check if we have children to keep up at all*/
1954 iterator = create_child_sa_iterator(this);
1955 while (iterator->iterate(iterator, (void**)&child_sa))
1956 {
1957 child_cfg = child_sa->get_config(child_sa);
1958 if (this->state == IKE_DELETING)
1959 {
1960 action = child_cfg->get_close_action(child_cfg);
1961 }
1962 else
1963 {
1964 action = child_cfg->get_dpd_action(child_cfg);
1965 }
1966 switch (action)
1967 {
1968 case ACTION_RESTART:
1969 case ACTION_ROUTE:
1970 required = TRUE;
1971 default:
1972 break;
1973 }
1974 }
1975 iterator->destroy(iterator);
1976 #ifdef ME
1977 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
1978 if (this->peer_cfg->is_mediation(this->peer_cfg))
1979 {
1980 required = TRUE;
1981 }
1982 #endif /* ME */
1983 if (!required)
1984 {
1985 return FAILED;
1986 }
1987
1988 /* check if we are able to reestablish this IKE_SA */
1989 if (!this->ike_initiator &&
1990 (this->other_virtual_ip != NULL ||
1991 has_condition(this, COND_EAP_AUTHENTICATED)
1992 #ifdef ME
1993 || this->is_mediation_server
1994 #endif /* ME */
1995 ))
1996 {
1997 DBG1(DBG_IKE, "unable to reestablish IKE_SA due asymetric setup");
1998 return FAILED;
1999 }
2000
2001 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager, TRUE);
2002 new->set_peer_cfg(new, this->peer_cfg);
2003 host = this->other_host;
2004 new->set_other_host(new, host->clone(host));
2005 host = this->my_host;
2006 new->set_my_host(new, host->clone(host));
2007 /* if we already have a virtual IP, we reuse it */
2008 host = this->my_virtual_ip;
2009 if (host)
2010 {
2011 new->set_virtual_ip(new, TRUE, host);
2012 }
2013
2014 #ifdef ME
2015 if (this->peer_cfg->is_mediation(this->peer_cfg))
2016 {
2017 status = new->initiate(new, NULL);
2018 }
2019 else
2020 #endif /* ME */
2021 {
2022 iterator = create_child_sa_iterator(this);
2023 while (iterator->iterate(iterator, (void**)&child_sa))
2024 {
2025 child_cfg = child_sa->get_config(child_sa);
2026 if (this->state == IKE_DELETING)
2027 {
2028 action = child_cfg->get_close_action(child_cfg);
2029 }
2030 else
2031 {
2032 action = child_cfg->get_dpd_action(child_cfg);
2033 }
2034 switch (action)
2035 {
2036 case ACTION_RESTART:
2037 DBG1(DBG_IKE, "restarting CHILD_SA %s",
2038 child_cfg->get_name(child_cfg));
2039 child_cfg->get_ref(child_cfg);
2040 status = new->initiate(new, child_cfg);
2041 break;
2042 case ACTION_ROUTE:
2043 status = new->route(new, child_cfg);
2044 break;
2045 default:
2046 continue;
2047 }
2048 if (status == DESTROY_ME)
2049 {
2050 break;
2051 }
2052 }
2053 iterator->destroy(iterator);
2054 }
2055
2056 if (status == DESTROY_ME)
2057 {
2058 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
2059 return FAILED;
2060 }
2061 else
2062 {
2063 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
2064 return SUCCESS;
2065 }
2066 }
2067
2068 /**
2069 * Implementation of ike_sa_t.retransmit.
2070 */
2071 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
2072 {
2073 this->time.outbound = time(NULL);
2074 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
2075 {
2076 /* send a proper signal to brief interested bus listeners */
2077 switch (this->state)
2078 {
2079 case IKE_CONNECTING:
2080 {
2081 /* retry IKE_SA_INIT if we have multiple keyingtries */
2082 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
2083 this->keyingtry++;
2084 if (tries == 0 || tries > this->keyingtry)
2085 {
2086 SIG(IKE_UP_FAILED, "peer not responding, trying again "
2087 "(%d/%d) in background ", this->keyingtry + 1, tries);
2088 reset(this);
2089 return this->task_manager->initiate(this->task_manager);
2090 }
2091 SIG(IKE_UP_FAILED, "establishing IKE_SA failed, peer not responding");
2092 break;
2093 }
2094 case IKE_DELETING:
2095 SIG(IKE_DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
2096 break;
2097 case IKE_REKEYING:
2098 SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
2099 /* FALL */
2100 default:
2101 reestablish(this);
2102 break;
2103 }
2104 return DESTROY_ME;
2105 }
2106 return SUCCESS;
2107 }
2108
2109 /**
2110 * Implementation of ike_sa_t.set_auth_lifetime.
2111 */
2112 static void set_auth_lifetime(private_ike_sa_t *this, u_int32_t lifetime)
2113 {
2114 job_t *job;
2115 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
2116
2117 this->time.reauth = time(NULL) + lifetime - reduction;
2118 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE);
2119
2120 if (lifetime < reduction)
2121 {
2122 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
2123 lifetime);
2124 charon->processor->queue_job(charon->processor, job);
2125 }
2126 else
2127 {
2128 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
2129 " in %ds", lifetime, lifetime - reduction);
2130 charon->scheduler->schedule_job(charon->scheduler, job,
2131 (lifetime - reduction) * 1000);
2132 }
2133 }
2134
2135 /**
2136 * Implementation of ike_sa_t.roam.
2137 */
2138 static status_t roam(private_ike_sa_t *this, bool address)
2139 {
2140 host_t *me, *other;
2141 ike_mobike_t *mobike;
2142
2143 switch (this->state)
2144 {
2145 case IKE_CREATED:
2146 case IKE_DELETING:
2147 return SUCCESS;
2148 default:
2149 break;
2150 }
2151 /* responder just updates the peer about changed address config */
2152 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
2153 {
2154 if (supports_extension(this, EXT_MOBIKE) && address)
2155 {
2156 DBG1(DBG_IKE, "sending address list update using MOBIKE");
2157 mobike = ike_mobike_create(&this->public, TRUE);
2158 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
2159 return this->task_manager->initiate(this->task_manager);
2160 }
2161 return SUCCESS;
2162 }
2163
2164 /* get best address pair to use */
2165 other = this->other_host;
2166 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
2167 other);
2168
2169 if (me)
2170 {
2171 if (me->ip_equals(me, this->my_host) &&
2172 other->ip_equals(other, this->other_host))
2173 {
2174 DBG2(DBG_IKE, "keeping connection path %H - %H", this->other_host, me);
2175 me->destroy(me);
2176 return SUCCESS;
2177 }
2178 me->destroy(me);
2179 }
2180
2181 /* update addresses with mobike, if supported ... */
2182 if (supports_extension(this, EXT_MOBIKE))
2183 {
2184 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2185 mobike = ike_mobike_create(&this->public, TRUE);
2186 mobike->roam(mobike, address);
2187 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
2188 return this->task_manager->initiate(this->task_manager);
2189 }
2190 DBG1(DBG_IKE, "reauthenticating IKE_SA due address change");
2191 /* ... reauth if not */
2192 return reauth(this);
2193 }
2194
2195 /**
2196 * Implementation of ike_sa_t.inherit.
2197 */
2198 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
2199 {
2200 child_sa_t *child_sa;
2201 host_t *ip;
2202
2203 /* apply hosts and ids */
2204 this->my_host->destroy(this->my_host);
2205 this->other_host->destroy(this->other_host);
2206 this->my_id->destroy(this->my_id);
2207 this->other_id->destroy(this->other_id);
2208 this->my_host = other->my_host->clone(other->my_host);
2209 this->other_host = other->other_host->clone(other->other_host);
2210 this->my_id = other->my_id->clone(other->my_id);
2211 this->other_id = other->other_id->clone(other->other_id);
2212 this->ike_initiator = other->ike_initiator;
2213
2214 /* apply virtual assigned IPs... */
2215 if (other->my_virtual_ip)
2216 {
2217 this->my_virtual_ip = other->my_virtual_ip;
2218 other->my_virtual_ip = NULL;
2219 }
2220 if (other->other_virtual_ip)
2221 {
2222 this->other_virtual_ip = other->other_virtual_ip;
2223 other->other_virtual_ip = NULL;
2224 }
2225
2226 /* ... and DNS servers */
2227 while (other->dns_servers->remove_last(other->dns_servers,
2228 (void**)&ip) == SUCCESS)
2229 {
2230 this->dns_servers->insert_first(this->dns_servers, ip);
2231 }
2232
2233 /* inherit NAT-T conditions */
2234 this->conditions = other->conditions;
2235 if (this->conditions & COND_NAT_HERE)
2236 {
2237 send_keepalive(this);
2238 }
2239
2240 #ifdef ME
2241 if (other->is_mediation_server)
2242 {
2243 act_as_mediation_server(this);
2244 }
2245 else if (other->server_reflexive_host)
2246 {
2247 this->server_reflexive_host = other->server_reflexive_host->clone(
2248 other->server_reflexive_host);
2249 }
2250 #endif /* ME */
2251
2252 /* adopt all children */
2253 while (other->child_sas->remove_last(other->child_sas,
2254 (void**)&child_sa) == SUCCESS)
2255 {
2256 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
2257 }
2258
2259 /* move pending tasks to the new IKE_SA */
2260 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2261
2262 /* reauthentication timeout survives a rekeying */
2263 if (other->time.reauth)
2264 {
2265 time_t reauth, delete, now = time(NULL);
2266
2267 this->time.reauth = other->time.reauth;
2268 reauth = this->time.reauth - now;
2269 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2270 this->time.delete = this->time.reauth + delete;
2271 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2272 "lifetime reduced to %ds", reauth, delete);
2273 charon->scheduler->schedule_job(charon->scheduler,
2274 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2275 reauth * 1000);
2276 charon->scheduler->schedule_job(charon->scheduler,
2277 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE),
2278 delete * 1000);
2279 }
2280 /* we have to initate here, there may be new tasks to handle */
2281 return this->task_manager->initiate(this->task_manager);
2282 }
2283
2284 /**
2285 * Implementation of ike_sa_t.remove_dns_server
2286 */
2287 static void remove_dns_servers(private_ike_sa_t *this)
2288 {
2289 FILE *file;
2290 struct stat stats;
2291 chunk_t contents, line, orig_line, token;
2292 char string[INET6_ADDRSTRLEN];
2293 host_t *ip;
2294 iterator_t *iterator;
2295
2296 if (this->dns_servers->get_count(this->dns_servers) == 0)
2297 {
2298 /* don't touch anything if we have no nameservers installed */
2299 return;
2300 }
2301
2302 file = fopen(RESOLV_CONF, "r");
2303 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
2304 {
2305 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2306 RESOLV_CONF, strerror(errno));
2307 return;
2308 }
2309
2310 contents = chunk_alloca((size_t)stats.st_size);
2311
2312 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
2313 {
2314 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
2315 fclose(file);
2316 return;
2317 }
2318
2319 fclose(file);
2320 file = fopen(RESOLV_CONF, "w");
2321 if (file == NULL)
2322 {
2323 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2324 RESOLV_CONF, strerror(errno));
2325 return;
2326 }
2327
2328 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
2329 while (fetchline(&contents, &line))
2330 {
2331 bool found = FALSE;
2332 orig_line = line;
2333 if (extract_token(&token, ' ', &line) &&
2334 strncasecmp(token.ptr, "nameserver", token.len) == 0)
2335 {
2336 if (!extract_token(&token, ' ', &line))
2337 {
2338 token = line;
2339 }
2340 iterator->reset(iterator);
2341 while (iterator->iterate(iterator, (void**)&ip))
2342 {
2343 snprintf(string, sizeof(string), "%H", ip);
2344 if (strlen(string) == token.len &&
2345 strncmp(token.ptr, string, token.len) == 0)
2346 {
2347 iterator->remove(iterator);
2348 ip->destroy(ip);
2349 found = TRUE;
2350 break;
2351 }
2352 }
2353 }
2354
2355 if (!found)
2356 {
2357 /* write line untouched back to file */
2358 fwrite(orig_line.ptr, orig_line.len, 1, file);
2359 fprintf(file, "\n");
2360 }
2361 }
2362 iterator->destroy(iterator);
2363 fclose(file);
2364 }
2365
2366 /**
2367 * Implementation of ike_sa_t.add_dns_server
2368 */
2369 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
2370 {
2371 FILE *file;
2372 struct stat stats;
2373 chunk_t contents;
2374
2375 DBG1(DBG_IKE, "installing DNS server %H", dns);
2376
2377 file = fopen(RESOLV_CONF, "a+");
2378 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
2379 {
2380 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2381 RESOLV_CONF, strerror(errno));
2382 return;
2383 }
2384
2385 contents = chunk_alloca(stats.st_size);
2386
2387 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
2388 {
2389 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
2390 fclose(file);
2391 return;
2392 }
2393
2394 fclose(file);
2395 file = fopen(RESOLV_CONF, "w");
2396 if (file == NULL)
2397 {
2398 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
2399 RESOLV_CONF, strerror(errno));
2400 return;
2401 }
2402
2403 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
2404 dns, this->other_id) < 0)
2405 {
2406 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
2407 }
2408 else
2409 {
2410 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
2411 }
2412 fwrite(contents.ptr, contents.len, 1, file);
2413
2414 fclose(file);
2415 }
2416
2417 /**
2418 * Implementation of ike_sa_t.destroy.
2419 */
2420 static void destroy(private_ike_sa_t *this)
2421 {
2422 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
2423
2424 this->task_manager->destroy(this->task_manager);
2425
2426 DESTROY_IF(this->crypter_in);
2427 DESTROY_IF(this->crypter_out);
2428 DESTROY_IF(this->signer_in);
2429 DESTROY_IF(this->signer_out);
2430 DESTROY_IF(this->prf);
2431 DESTROY_IF(this->child_prf);
2432 chunk_free(&this->skp_verify);
2433 chunk_free(&this->skp_build);
2434 free(this->selected_proposal);
2435
2436 if (this->my_virtual_ip)
2437 {
2438 charon->kernel_interface->del_ip(charon->kernel_interface,
2439 this->my_virtual_ip);
2440 this->my_virtual_ip->destroy(this->my_virtual_ip);
2441 }
2442 if (this->other_virtual_ip)
2443 {
2444 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
2445 {
2446 charon->attributes->release_address(charon->attributes,
2447 this->peer_cfg->get_pool(this->peer_cfg),
2448 this->other_virtual_ip);
2449 }
2450 this->other_virtual_ip->destroy(this->other_virtual_ip);
2451 }
2452
2453 remove_dns_servers(this);
2454 this->dns_servers->destroy_offset(this->dns_servers,
2455 offsetof(host_t, destroy));
2456 this->additional_addresses->destroy_offset(this->additional_addresses,
2457 offsetof(host_t, destroy));
2458 #ifdef ME
2459 if (this->is_mediation_server)
2460 {
2461 charon->mediation_manager->remove(charon->mediation_manager, this->ike_sa_id);
2462 }
2463 DESTROY_IF(this->server_reflexive_host);
2464 chunk_free(&this->connect_id);
2465 #endif /* ME */
2466
2467 DESTROY_IF(this->my_host);
2468 DESTROY_IF(this->other_host);
2469 DESTROY_IF(this->my_id);
2470 DESTROY_IF(this->other_id);
2471
2472 DESTROY_IF(this->ike_cfg);
2473 DESTROY_IF(this->peer_cfg);
2474 DESTROY_IF(this->my_auth);
2475 DESTROY_IF(this->other_auth);
2476
2477 this->ike_sa_id->destroy(this->ike_sa_id);
2478 free(this);
2479 }
2480
2481 /*
2482 * Described in header.
2483 */
2484 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2485 {
2486 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2487 static u_int32_t unique_id = 0;
2488
2489 /* Public functions */
2490 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
2491 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
2492 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2493 this->public.get_statistic = (u_int32_t(*)(ike_sa_t*, statistic_t kind))get_statistic;
2494 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2495 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2496 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2497 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2498 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2499 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2500 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2501 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2502 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2503 this->public.get_my_auth = (auth_info_t*(*)(ike_sa_t*))get_my_auth;
2504 this->public.get_other_auth = (auth_info_t*(*)(ike_sa_t*))get_other_auth;
2505 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2506 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2507 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2508 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2509 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2510 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2511 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2512 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2513 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2514 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2515 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2516 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2517 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2518 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2519 this->public.set_pending_updates = (void(*)(ike_sa_t*, u_int32_t updates))set_pending_updates;
2520 this->public.get_pending_updates = (u_int32_t(*)(ike_sa_t*))get_pending_updates;
2521 this->public.is_ike_initiator = (bool (*)(ike_sa_t*))is_ike_initiator;
2522 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2523 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2524 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2525 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2526 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2527 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2528 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2529 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
2530 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
2531 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
2532 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
2533 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2534 this->public.get_proposal = (char* (*)(ike_sa_t*)) get_proposal;
2535 this->public.set_proposal = (void (*)(ike_sa_t*,char*)) set_proposal;
2536 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2537 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2538 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2539 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2540 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2541 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2542 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2543 this->public.reauth = (status_t (*)(ike_sa_t*))reauth;
2544 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2545 this->public.set_auth_lifetime = (void(*)(ike_sa_t*, u_int32_t lifetime))set_auth_lifetime;
2546 this->public.roam = (status_t(*)(ike_sa_t*,bool))roam;
2547 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2548 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2549 this->public.reset = (void (*)(ike_sa_t*))reset;
2550 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2551 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2552 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2553 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2554 #ifdef ME
2555 this->public.act_as_mediation_server = (void (*)(ike_sa_t*)) act_as_mediation_server;
2556 this->public.get_server_reflexive_host = (host_t* (*)(ike_sa_t*)) get_server_reflexive_host;
2557 this->public.set_server_reflexive_host = (void (*)(ike_sa_t*,host_t*)) set_server_reflexive_host;
2558 this->public.get_connect_id = (chunk_t (*)(ike_sa_t*)) get_connect_id;
2559 this->public.initiate_mediation = (status_t (*)(ike_sa_t*,peer_cfg_t*)) initiate_mediation;
2560 this->public.initiate_mediated = (status_t (*)(ike_sa_t*,host_t*,host_t*,chunk_t)) initiate_mediated;
2561 this->public.relay = (status_t (*)(ike_sa_t*,identification_t*,chunk_t,chunk_t,linked_list_t*,bool)) relay;
2562 this->public.callback = (status_t (*)(ike_sa_t*,identification_t*)) callback;
2563 this->public.respond = (status_t (*)(ike_sa_t*,identification_t*,chunk_t)) respond;
2564 #endif /* ME */
2565
2566 /* initialize private fields */
2567 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2568 this->child_sas = linked_list_create();
2569 this->my_host = host_create_from_string("0.0.0.0", IKEV2_UDP_PORT);
2570 this->other_host = host_create_from_string("0.0.0.0", IKEV2_UDP_PORT);
2571 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2572 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2573 this->extensions = 0;
2574 this->conditions = 0;
2575 this->selected_proposal = NULL;
2576 this->crypter_in = NULL;
2577 this->crypter_out = NULL;
2578 this->signer_in = NULL;
2579 this->signer_out = NULL;
2580 this->prf = NULL;
2581 this->skp_verify = chunk_empty;
2582 this->skp_build = chunk_empty;
2583 this->child_prf = NULL;
2584 this->state = IKE_CREATED;
2585 this->time.inbound = this->time.outbound = time(NULL);
2586 this->time.established = 0;
2587 this->time.rekey = 0;
2588 this->time.reauth = 0;
2589 this->time.delete = 0;
2590 this->ike_cfg = NULL;
2591 this->peer_cfg = NULL;
2592 this->my_auth = auth_info_create();
2593 this->other_auth = auth_info_create();
2594 this->task_manager = task_manager_create(&this->public);
2595 this->unique_id = ++unique_id;
2596 this->my_virtual_ip = NULL;
2597 this->other_virtual_ip = NULL;
2598 this->dns_servers = linked_list_create();
2599 this->additional_addresses = linked_list_create();
2600 this->pending_updates = 0;
2601 this->keyingtry = 0;
2602 this->ike_initiator = FALSE;
2603 #ifdef ME
2604 this->is_mediation_server = FALSE;
2605 this->server_reflexive_host = NULL;
2606 this->connect_id = chunk_empty;
2607 #endif /* ME */
2608
2609 return &this->public;
2610 }