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