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