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