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