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