added extensions management to IKE_SA
[strongswan.git] / src / charon / sa / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Implementation of ike_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include <sys/time.h>
26 #include <string.h>
27 #include <printf.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30
31 #include "ike_sa.h"
32
33 #include <library.h>
34 #include <daemon.h>
35 #include <utils/linked_list.h>
36 #include <utils/lexparser.h>
37 #include <crypto/diffie_hellman.h>
38 #include <crypto/prf_plus.h>
39 #include <crypto/crypters/crypter.h>
40 #include <crypto/hashers/hasher.h>
41 #include <encoding/payloads/sa_payload.h>
42 #include <encoding/payloads/nonce_payload.h>
43 #include <encoding/payloads/ke_payload.h>
44 #include <encoding/payloads/delete_payload.h>
45 #include <encoding/payloads/transform_substructure.h>
46 #include <encoding/payloads/transform_attribute.h>
47 #include <encoding/payloads/ts_payload.h>
48 #include <sa/task_manager.h>
49 #include <sa/tasks/ike_init.h>
50 #include <sa/tasks/ike_natd.h>
51 #include <sa/tasks/ike_auth.h>
52 #include <sa/tasks/ike_config.h>
53 #include <sa/tasks/ike_cert.h>
54 #include <sa/tasks/ike_rekey.h>
55 #include <sa/tasks/ike_reauth.h>
56 #include <sa/tasks/ike_delete.h>
57 #include <sa/tasks/ike_dpd.h>
58 #include <sa/tasks/child_create.h>
59 #include <sa/tasks/child_delete.h>
60 #include <sa/tasks/child_rekey.h>
61 #include <processing/jobs/retransmit_job.h>
62 #include <processing/jobs/delete_ike_sa_job.h>
63 #include <processing/jobs/send_dpd_job.h>
64 #include <processing/jobs/send_keepalive_job.h>
65 #include <processing/jobs/rekey_ike_sa_job.h>
66
67
68 #ifndef RESOLV_CONF
69 #define RESOLV_CONF "/etc/resolv.conf"
70 #endif
71
72 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DELETING,
73 "CREATED",
74 "CONNECTING",
75 "ESTABLISHED",
76 "REKEYING",
77 "DELETING",
78 );
79
80 typedef struct private_ike_sa_t private_ike_sa_t;
81
82 /**
83 * Private data of an ike_sa_t object.
84 */
85 struct private_ike_sa_t {
86
87 /**
88 * Public members
89 */
90 ike_sa_t public;
91
92 /**
93 * Identifier for the current IKE_SA.
94 */
95 ike_sa_id_t *ike_sa_id;
96
97 /**
98 * unique numerical ID for this IKE_SA.
99 */
100 u_int32_t unique_id;
101
102 /**
103 * Current state of the IKE_SA
104 */
105 ike_sa_state_t state;
106
107 /**
108 * IKE configuration used to set up this IKE_SA
109 */
110 ike_cfg_t *ike_cfg;
111
112 /**
113 * Peer and authentication information to establish IKE_SA.
114 */
115 peer_cfg_t *peer_cfg;
116
117 /**
118 * Juggles tasks to process messages
119 */
120 task_manager_t *task_manager;
121
122 /**
123 * Address of local host
124 */
125 host_t *my_host;
126
127 /**
128 * Address of remote host
129 */
130 host_t *other_host;
131
132 /**
133 * Identification used for us
134 */
135 identification_t *my_id;
136
137 /**
138 * Identification used for other
139 */
140 identification_t *other_id;
141
142 /**
143 * CA that issued the certificate of other
144 */
145 ca_info_t *other_ca;
146
147 /**
148 * set of extensions the peer supports
149 */
150 ike_extension_t extensions;
151
152 /**
153 * Linked List containing the child sa's of the current IKE_SA.
154 */
155 linked_list_t *child_sas;
156
157 /**
158 * crypter for inbound traffic
159 */
160 crypter_t *crypter_in;
161
162 /**
163 * crypter for outbound traffic
164 */
165 crypter_t *crypter_out;
166
167 /**
168 * Signer for inbound traffic
169 */
170 signer_t *signer_in;
171
172 /**
173 * Signer for outbound traffic
174 */
175 signer_t *signer_out;
176
177 /**
178 * Multi purpose prf, set key, use it, forget it
179 */
180 prf_t *prf;
181
182 /**
183 * Prf function for derivating keymat child SAs
184 */
185 prf_t *child_prf;
186
187 /**
188 * Key to build outging authentication data (SKp)
189 */
190 chunk_t skp_build;
191
192 /**
193 * Key to verify incoming authentication data (SKp)
194 */
195 chunk_t skp_verify;
196
197 /**
198 * NAT status of local host.
199 */
200 bool nat_here;
201
202 /**
203 * NAT status of remote host.
204 */
205 bool nat_there;
206
207 /**
208 * Virtual IP on local host, if any
209 */
210 host_t *my_virtual_ip;
211
212 /**
213 * Virtual IP on remote host, if any
214 */
215 host_t *other_virtual_ip;
216
217 /**
218 * List of DNS servers installed by us
219 */
220 linked_list_t *dns_servers;
221
222 /**
223 * Timestamps for this IKE_SA
224 */
225 struct {
226 /** last IKE message received */
227 u_int32_t inbound;
228 /** last IKE message sent */
229 u_int32_t outbound;
230 /** when IKE_SA became established */
231 u_int32_t established;
232 /** when IKE_SA gets rekeyed */
233 u_int32_t rekey;
234 /** when IKE_SA gets deleted */
235 u_int32_t delete;
236 } time;
237
238 /**
239 * how many times we have retried so far (keyingtries)
240 */
241 u_int32_t keyingtry;
242 };
243
244 /**
245 * get the time of the latest traffic processed by the kernel
246 */
247 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
248 {
249 iterator_t *iterator;
250 child_sa_t *child_sa;
251 time_t latest = 0, use_time;
252
253 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
254 while (iterator->iterate(iterator, (void**)&child_sa))
255 {
256 if (child_sa->get_use_time(child_sa, inbound, &use_time) == SUCCESS)
257 {
258 latest = max(latest, use_time);
259 }
260 }
261 iterator->destroy(iterator);
262
263 if (inbound)
264 {
265 return max(this->time.inbound, latest);
266 }
267 else
268 {
269 return max(this->time.outbound, latest);
270 }
271 }
272
273 /**
274 * Implementation of ike_sa_t.get_unique_id
275 */
276 static u_int32_t get_unique_id(private_ike_sa_t *this)
277 {
278 return this->unique_id;
279 }
280
281 /**
282 * Implementation of ike_sa_t.get_name.
283 */
284 static char *get_name(private_ike_sa_t *this)
285 {
286 if (this->peer_cfg)
287 {
288 return this->peer_cfg->get_name(this->peer_cfg);
289 }
290 return "(unnamed)";
291 }
292
293
294 /**
295 * Implementation of ike_sa_t.get_stats.
296 */
297 static void get_stats(private_ike_sa_t *this, u_int32_t *next_rekeying)
298 {
299 if (next_rekeying)
300 {
301 *next_rekeying = this->time.rekey;
302 }
303 }
304
305 /**
306 * Implementation of ike_sa_t.get_my_host.
307 */
308 static host_t *get_my_host(private_ike_sa_t *this)
309 {
310 return this->my_host;
311 }
312
313 /**
314 * Implementation of ike_sa_t.set_my_host.
315 */
316 static void set_my_host(private_ike_sa_t *this, host_t *me)
317 {
318 DESTROY_IF(this->my_host);
319 this->my_host = me;
320 }
321
322 /**
323 * Implementation of ike_sa_t.get_other_host.
324 */
325 static host_t *get_other_host(private_ike_sa_t *this)
326 {
327 return this->other_host;
328 }
329
330 /**
331 * Implementation of ike_sa_t.set_other_host.
332 */
333 static void set_other_host(private_ike_sa_t *this, host_t *other)
334 {
335 DESTROY_IF(this->other_host);
336 this->other_host = other;
337 }
338
339 /**
340 * Implementation of ike_sa_t.get_peer_cfg
341 */
342 static peer_cfg_t* get_peer_cfg(private_ike_sa_t *this)
343 {
344 return this->peer_cfg;
345 }
346
347 /**
348 * Implementation of ike_sa_t.set_peer_cfg
349 */
350 static void set_peer_cfg(private_ike_sa_t *this, peer_cfg_t *peer_cfg)
351 {
352 peer_cfg->get_ref(peer_cfg);
353 this->peer_cfg = peer_cfg;
354
355 if (this->ike_cfg == NULL)
356 {
357 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
358 this->ike_cfg->get_ref(this->ike_cfg);
359 }
360
361 /* apply values, so we are ready to initate/acquire */
362 if (this->my_host->is_anyaddr(this->my_host))
363 {
364 host_t *me = this->ike_cfg->get_my_host(this->ike_cfg);
365 set_my_host(this, me->clone(me));
366 }
367 if (this->other_host->is_anyaddr(this->other_host))
368 {
369 host_t *other = this->ike_cfg->get_other_host(this->ike_cfg);
370 set_other_host(this, other->clone(other));
371 }
372 /* apply IDs if they are not already set */
373 if (this->my_id->contains_wildcards(this->my_id))
374 {
375 DESTROY_IF(this->my_id);
376 this->my_id = this->peer_cfg->get_my_id(this->peer_cfg);
377 this->my_id = this->my_id->clone(this->my_id);
378 }
379 if (this->other_id->contains_wildcards(this->other_id))
380 {
381 DESTROY_IF(this->other_id);
382 this->other_id = this->peer_cfg->get_other_id(this->peer_cfg);
383 this->other_id = this->other_id->clone(this->other_id);
384 }
385 }
386
387 /**
388 * Implementation of ike_sa_t.get_ike_cfg
389 */
390 static ike_cfg_t *get_ike_cfg(private_ike_sa_t *this)
391 {
392 return this->ike_cfg;
393 }
394
395 /**
396 * Implementation of ike_sa_t.set_ike_cfg
397 */
398 static void set_ike_cfg(private_ike_sa_t *this, ike_cfg_t *ike_cfg)
399 {
400 ike_cfg->get_ref(ike_cfg);
401 this->ike_cfg = ike_cfg;
402 }
403
404 /**
405 * Implementation of ike_sa_t.send_dpd
406 */
407 static status_t send_dpd(private_ike_sa_t *this)
408 {
409 send_dpd_job_t *job;
410 time_t diff, delay;
411
412 delay = this->peer_cfg->get_dpd_delay(this->peer_cfg);
413
414 if (delay == 0)
415 {
416 /* DPD disabled */
417 return SUCCESS;
418 }
419
420 if (this->task_manager->busy(this->task_manager))
421 {
422 /* an exchange is in the air, no need to start a DPD check */
423 diff = 0;
424 }
425 else
426 {
427 /* check if there was any inbound traffic */
428 time_t last_in, now;
429 last_in = get_use_time(this, TRUE);
430 now = time(NULL);
431 diff = now - last_in;
432 if (diff >= delay)
433 {
434 /* to long ago, initiate dead peer detection */
435 task_t *task;
436
437 task = (task_t*)ike_dpd_create(TRUE);
438 diff = 0;
439 DBG1(DBG_IKE, "sending DPD request");
440
441 this->task_manager->queue_task(this->task_manager, task);
442 this->task_manager->initiate(this->task_manager);
443 }
444 }
445 /* recheck in "interval" seconds */
446 job = send_dpd_job_create(this->ike_sa_id);
447 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
448 (delay - diff) * 1000);
449 return SUCCESS;
450 }
451
452 /**
453 * Implementation of ike_sa_t.send_keepalive
454 */
455 static void send_keepalive(private_ike_sa_t *this)
456 {
457 send_keepalive_job_t *job;
458 time_t last_out, now, diff;
459
460 last_out = get_use_time(this, FALSE);
461 now = time(NULL);
462
463 diff = now - last_out;
464
465 if (diff >= KEEPALIVE_INTERVAL)
466 {
467 packet_t *packet;
468 chunk_t data;
469
470 packet = packet_create();
471 packet->set_source(packet, this->my_host->clone(this->my_host));
472 packet->set_destination(packet, this->other_host->clone(this->other_host));
473 data.ptr = malloc(1);
474 data.ptr[0] = 0xFF;
475 data.len = 1;
476 packet->set_data(packet, data);
477 charon->sender->send(charon->sender, packet);
478 DBG1(DBG_IKE, "sending keep alive");
479 diff = 0;
480 }
481 job = send_keepalive_job_create(this->ike_sa_id);
482 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
483 (KEEPALIVE_INTERVAL - diff) * 1000);
484 }
485
486 /**
487 * Implementation of ike_sa_t.get_state.
488 */
489 static ike_sa_state_t get_state(private_ike_sa_t *this)
490 {
491 return this->state;
492 }
493
494 /**
495 * Implementation of ike_sa_t.set_state.
496 */
497 static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
498 {
499 DBG1(DBG_IKE, "IKE_SA state change: %N => %N",
500 ike_sa_state_names, this->state,
501 ike_sa_state_names, state);
502
503 switch (state)
504 {
505 case IKE_ESTABLISHED:
506 {
507 if (this->state == IKE_CONNECTING)
508 {
509 job_t *job;
510 u_int32_t now = time(NULL);
511 u_int32_t soft, hard;
512 bool reauth;
513
514 this->time.established = now;
515 /* start DPD checks */
516 send_dpd(this);
517
518 /* schedule rekeying/reauthentication */
519 soft = this->peer_cfg->get_lifetime(this->peer_cfg, TRUE);
520 hard = this->peer_cfg->get_lifetime(this->peer_cfg, FALSE);
521 reauth = this->peer_cfg->use_reauth(this->peer_cfg);
522 DBG1(DBG_IKE, "scheduling %s in %ds, maximum lifetime %ds",
523 reauth ? "reauthentication": "rekeying", soft, hard);
524
525 if (soft)
526 {
527 this->time.rekey = now + soft;
528 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, reauth);
529 charon->scheduler->schedule_job(charon->scheduler, job,
530 soft * 1000);
531 }
532
533 if (hard)
534 {
535 this->time.delete = now + hard;
536 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
537 charon->scheduler->schedule_job(charon->scheduler, job,
538 hard * 1000);
539 }
540 }
541 break;
542 }
543 case IKE_DELETING:
544 {
545 /* delete may fail if a packet gets lost, so set a timeout */
546 job_t *job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
547 charon->scheduler->schedule_job(charon->scheduler, job,
548 HALF_OPEN_IKE_SA_TIMEOUT);
549 break;
550 }
551 default:
552 break;
553 }
554
555 this->state = state;
556 }
557
558 /**
559 * Implementation of ike_sa_t.reset
560 */
561 static void reset(private_ike_sa_t *this)
562 {
563 /* the responder ID is reset, as peer may choose another one */
564 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
565 {
566 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
567 }
568
569 set_state(this, IKE_CREATED);
570
571 this->task_manager->reset(this->task_manager);
572 }
573
574 /**
575 * Update hosts, as addresses may change (NAT)
576 */
577 static void update_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
578 {
579 iterator_t *iterator = NULL;
580 child_sa_t *child_sa = NULL;
581 host_diff_t my_diff, other_diff;
582
583 if (this->my_host->is_anyaddr(this->my_host) ||
584 this->other_host->is_anyaddr(this->other_host))
585 {
586 /* on first received message */
587 this->my_host->destroy(this->my_host);
588 this->my_host = me->clone(me);
589 this->other_host->destroy(this->other_host);
590 this->other_host = other->clone(other);
591 return;
592 }
593
594 my_diff = me->get_differences(me, this->my_host);
595 other_diff = other->get_differences(other, this->other_host);
596
597 if (!my_diff && !other_diff)
598 {
599 return;
600 }
601
602 if (my_diff)
603 {
604 this->my_host->destroy(this->my_host);
605 this->my_host = me->clone(me);
606 }
607
608 if (!this->nat_here)
609 {
610 /* update without restrictions if we are not NATted */
611 if (other_diff)
612 {
613 this->other_host->destroy(this->other_host);
614 this->other_host = other->clone(other);
615 }
616 }
617 else
618 {
619 /* if we are natted, only port may change */
620 if (other_diff & HOST_DIFF_ADDR)
621 {
622 return;
623 }
624 else if (other_diff & HOST_DIFF_PORT)
625 {
626 this->other_host->set_port(this->other_host, other->get_port(other));
627 }
628 }
629 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
630 while (iterator->iterate(iterator, (void**)&child_sa))
631 {
632 child_sa->update_hosts(child_sa, this->my_host, this->other_host,
633 my_diff, other_diff);
634 }
635 iterator->destroy(iterator);
636 }
637
638 /**
639 * Implementation of ike_sa_t.generate
640 */
641 static status_t generate_message(private_ike_sa_t *this, message_t *message,
642 packet_t **packet)
643 {
644 this->time.outbound = time(NULL);
645 message->set_ike_sa_id(message, this->ike_sa_id);
646 message->set_destination(message, this->other_host->clone(this->other_host));
647 message->set_source(message, this->my_host->clone(this->my_host));
648 return message->generate(message, this->crypter_out, this->signer_out, packet);
649 }
650
651 /**
652 * send a notify back to the sender
653 */
654 static void send_notify_response(private_ike_sa_t *this, message_t *request,
655 notify_type_t type)
656 {
657 message_t *response;
658 packet_t *packet;
659
660 response = message_create();
661 response->set_exchange_type(response, request->get_exchange_type(request));
662 response->set_request(response, FALSE);
663 response->set_message_id(response, request->get_message_id(request));
664 response->add_notify(response, FALSE, type, chunk_empty);
665 if (this->my_host->is_anyaddr(this->my_host))
666 {
667 this->my_host->destroy(this->my_host);
668 this->my_host = request->get_destination(request);
669 this->my_host = this->my_host->clone(this->my_host);
670 }
671 if (this->other_host->is_anyaddr(this->other_host))
672 {
673 this->other_host->destroy(this->other_host);
674 this->other_host = request->get_source(request);
675 this->other_host = this->other_host->clone(this->other_host);
676 }
677 if (generate_message(this, response, &packet) == SUCCESS)
678 {
679 charon->sender->send(charon->sender, packet);
680 }
681 response->destroy(response);
682 }
683
684 /**
685 * Implementation of ike_sa_t.process_message.
686 */
687 static status_t process_message(private_ike_sa_t *this, message_t *message)
688 {
689 status_t status;
690 bool is_request;
691
692 is_request = message->get_request(message);
693
694 status = message->parse_body(message, this->crypter_in, this->signer_in);
695 if (status != SUCCESS)
696 {
697
698 if (is_request)
699 {
700 switch (status)
701 {
702 case NOT_SUPPORTED:
703 DBG1(DBG_IKE, "ciritcal unknown payloads found");
704 if (is_request)
705 {
706 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
707 }
708 break;
709 case PARSE_ERROR:
710 DBG1(DBG_IKE, "message parsing failed");
711 if (is_request)
712 {
713 send_notify_response(this, message, INVALID_SYNTAX);
714 }
715 break;
716 case VERIFY_ERROR:
717 DBG1(DBG_IKE, "message verification failed");
718 if (is_request)
719 {
720 send_notify_response(this, message, INVALID_SYNTAX);
721 }
722 break;
723 case FAILED:
724 DBG1(DBG_IKE, "integrity check failed");
725 /* ignored */
726 break;
727 case INVALID_STATE:
728 DBG1(DBG_IKE, "found encrypted message, but no keys available");
729 if (is_request)
730 {
731 send_notify_response(this, message, INVALID_SYNTAX);
732 }
733 default:
734 break;
735 }
736 }
737 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
738 exchange_type_names, message->get_exchange_type(message),
739 message->get_request(message) ? "request" : "response",
740 message->get_message_id(message));
741 return status;
742 }
743 else
744 {
745 host_t *me, *other;
746
747 me = message->get_destination(message);
748 other = message->get_source(message);
749
750 /* if this IKE_SA is virgin, we check for a config */
751 if (this->ike_cfg == NULL)
752 {
753 job_t *job;
754 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
755 me, other);
756 if (this->ike_cfg == NULL)
757 {
758 /* no config found for these hosts, destroy */
759 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
760 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
761 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
762 return DESTROY_ME;
763 }
764 /* add a timeout if peer does not establish it completely */
765 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
766 charon->scheduler->schedule_job(charon->scheduler, job,
767 HALF_OPEN_IKE_SA_TIMEOUT);
768 }
769
770 /* check if message is trustworthy, and update host information */
771 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
772 message->get_exchange_type(message) != IKE_SA_INIT)
773 {
774 update_hosts(this, me, other);
775 this->time.inbound = time(NULL);
776 }
777 return this->task_manager->process_message(this->task_manager, message);
778 }
779 }
780
781 /**
782 * Implementation of ike_sa_t.initiate.
783 */
784 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
785 {
786 task_t *task;
787
788 if (this->state == IKE_CREATED)
789 {
790
791 if (this->other_host->is_anyaddr(this->other_host))
792 {
793 child_cfg->destroy(child_cfg);
794 SIG(IKE_UP_START, "initiating IKE_SA");
795 SIG(IKE_UP_FAILED, "unable to initiate to %%any");
796 return DESTROY_ME;
797 }
798
799 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
800 this->task_manager->queue_task(this->task_manager, task);
801 task = (task_t*)ike_natd_create(&this->public, TRUE);
802 this->task_manager->queue_task(this->task_manager, task);
803 task = (task_t*)ike_cert_create(&this->public, TRUE);
804 this->task_manager->queue_task(this->task_manager, task);
805 task = (task_t*)ike_auth_create(&this->public, TRUE);
806 this->task_manager->queue_task(this->task_manager, task);
807 task = (task_t*)ike_config_create(&this->public, TRUE);
808 this->task_manager->queue_task(this->task_manager, task);
809 }
810
811 task = (task_t*)child_create_create(&this->public, child_cfg);
812 child_cfg->destroy(child_cfg);
813 this->task_manager->queue_task(this->task_manager, task);
814
815 return this->task_manager->initiate(this->task_manager);
816 }
817
818 /**
819 * Implementation of ike_sa_t.acquire.
820 */
821 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
822 {
823 child_cfg_t *child_cfg;
824 iterator_t *iterator;
825 child_sa_t *current, *child_sa = NULL;
826 task_t *task;
827 child_create_t *child_create;
828
829 if (this->state == IKE_DELETING)
830 {
831 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
832 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
833 "IKE_SA is deleting", reqid);
834 return FAILED;
835 }
836
837 /* find CHILD_SA */
838 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
839 while (iterator->iterate(iterator, (void**)&current))
840 {
841 if (current->get_reqid(current) == reqid)
842 {
843 child_sa = current;
844 break;
845 }
846 }
847 iterator->destroy(iterator);
848 if (!child_sa)
849 {
850 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
851 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
852 "CHILD_SA not found", reqid);
853 return FAILED;
854 }
855
856
857 if (this->state == IKE_CREATED)
858 {
859 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
860 this->task_manager->queue_task(this->task_manager, task);
861 task = (task_t*)ike_natd_create(&this->public, TRUE);
862 this->task_manager->queue_task(this->task_manager, task);
863 task = (task_t*)ike_cert_create(&this->public, TRUE);
864 this->task_manager->queue_task(this->task_manager, task);
865 task = (task_t*)ike_auth_create(&this->public, TRUE);
866 this->task_manager->queue_task(this->task_manager, task);
867 task = (task_t*)ike_config_create(&this->public, TRUE);
868 this->task_manager->queue_task(this->task_manager, task);
869 }
870
871 child_cfg = child_sa->get_config(child_sa);
872 child_create = child_create_create(&this->public, child_cfg);
873 child_create->use_reqid(child_create, reqid);
874 this->task_manager->queue_task(this->task_manager, (task_t*)child_create);
875
876 return this->task_manager->initiate(this->task_manager);
877 }
878
879 /**
880 * Implementation of ike_sa_t.route.
881 */
882 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
883 {
884 child_sa_t *child_sa;
885 iterator_t *iterator;
886 linked_list_t *my_ts, *other_ts;
887 status_t status;
888
889 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
890
891 /* check if not already routed*/
892 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
893 while (iterator->iterate(iterator, (void**)&child_sa))
894 {
895 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
896 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
897 {
898 iterator->destroy(iterator);
899 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a config already routed");
900 return FAILED;
901 }
902 }
903 iterator->destroy(iterator);
904
905 switch (this->state)
906 {
907 case IKE_DELETING:
908 case IKE_REKEYING:
909 SIG(CHILD_ROUTE_FAILED,
910 "unable to route CHILD_SA, as its IKE_SA gets deleted");
911 return FAILED;
912 case IKE_CREATED:
913 case IKE_CONNECTING:
914 case IKE_ESTABLISHED:
915 default:
916 break;
917 }
918
919 /* install kernel policies */
920 child_sa = child_sa_create(this->my_host, this->other_host, this->my_id,
921 this->other_id, child_cfg, FALSE, 0);
922
923 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL,
924 this->my_host);
925 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL,
926 this->other_host);
927 status = child_sa->add_policies(child_sa, my_ts, other_ts,
928 child_cfg->get_mode(child_cfg));
929 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
930 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
931 if (status == SUCCESS)
932 {
933 this->child_sas->insert_last(this->child_sas, child_sa);
934 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
935 }
936 else
937 {
938 SIG(CHILD_ROUTE_FAILED, "routing CHILD_SA failed");
939 }
940 return status;
941 }
942
943 /**
944 * Implementation of ike_sa_t.unroute.
945 */
946 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
947 {
948 iterator_t *iterator;
949 child_sa_t *child_sa;
950 bool found = FALSE;
951
952 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
953
954 /* find CHILD_SA in ROUTED state */
955 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
956 while (iterator->iterate(iterator, (void**)&child_sa))
957 {
958 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
959 child_sa->get_reqid(child_sa) == reqid)
960 {
961 iterator->remove(iterator);
962 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
963 child_sa->destroy(child_sa);
964 found = TRUE;
965 break;
966 }
967 }
968 iterator->destroy(iterator);
969
970 if (!found)
971 {
972 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
973 return FAILED;
974 }
975 /* if we are not established, and we have no more routed childs, remove whole SA */
976 if (this->state == IKE_CREATED &&
977 this->child_sas->get_count(this->child_sas) == 0)
978 {
979 return DESTROY_ME;
980 }
981 return SUCCESS;
982 }
983
984 /**
985 * Implementation of ike_sa_t.retransmit.
986 */
987 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
988 {
989 this->time.outbound = time(NULL);
990 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
991 {
992 child_cfg_t *child_cfg;
993 child_sa_t* child_sa;
994 linked_list_t *to_route, *to_restart;
995 iterator_t *iterator;
996
997 /* send a proper signal to brief interested bus listeners */
998 switch (this->state)
999 {
1000 case IKE_CONNECTING:
1001 {
1002 /* retry IKE_SA_INIT if we have multiple keyingtries */
1003 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1004 this->keyingtry++;
1005 if (tries == 0 || tries > this->keyingtry)
1006 {
1007 SIG(IKE_UP_FAILED, "peer not responding, trying again "
1008 "(%d/%d) in background ", this->keyingtry + 1, tries);
1009 reset(this);
1010 return this->task_manager->initiate(this->task_manager);
1011 }
1012 SIG(IKE_UP_FAILED, "establishing IKE_SA failed, peer not responding");
1013 break;
1014 }
1015 case IKE_REKEYING:
1016 SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
1017 break;
1018 case IKE_DELETING:
1019 SIG(IKE_DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
1020 break;
1021 default:
1022 break;
1023 }
1024
1025 /* summarize how we have to handle each child */
1026 to_route = linked_list_create();
1027 to_restart = linked_list_create();
1028 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1029 while (iterator->iterate(iterator, (void**)&child_sa))
1030 {
1031 child_cfg = child_sa->get_config(child_sa);
1032
1033 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1034 {
1035 /* reroute routed CHILD_SAs */
1036 to_route->insert_last(to_route, child_cfg);
1037 }
1038 else
1039 {
1040 /* use DPD action for established CHILD_SAs */
1041 switch (this->peer_cfg->get_dpd_action(this->peer_cfg))
1042 {
1043 case DPD_ROUTE:
1044 to_route->insert_last(to_route, child_cfg);
1045 break;
1046 case DPD_RESTART:
1047 to_restart->insert_last(to_restart, child_cfg);
1048 break;
1049 default:
1050 break;
1051 }
1052 }
1053 }
1054 iterator->destroy(iterator);
1055
1056 /* create a new IKE_SA if we have to route or to restart */
1057 if (to_route->get_count(to_route) || to_restart->get_count(to_restart))
1058 {
1059 private_ike_sa_t *new;
1060 task_t *task;
1061
1062 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1063 charon->ike_sa_manager, TRUE);
1064
1065 set_peer_cfg(new, this->peer_cfg);
1066 /* use actual used host, not the wildcarded one in config */
1067 new->other_host->destroy(new->other_host);
1068 new->other_host = this->other_host->clone(this->other_host);
1069
1070 /* install routes */
1071 while (to_route->remove_last(to_route, (void**)&child_cfg) == SUCCESS)
1072 {
1073 route(new, child_cfg);
1074 }
1075
1076 /* restart children */
1077 if (to_restart->get_count(to_restart))
1078 {
1079 task = (task_t*)ike_init_create(&new->public, TRUE, NULL);
1080 new->task_manager->queue_task(new->task_manager, task);
1081 task = (task_t*)ike_natd_create(&new->public, TRUE);
1082 new->task_manager->queue_task(new->task_manager, task);
1083 task = (task_t*)ike_cert_create(&new->public, TRUE);
1084 new->task_manager->queue_task(new->task_manager, task);
1085 task = (task_t*)ike_config_create(&new->public, TRUE);
1086 new->task_manager->queue_task(new->task_manager, task);
1087 task = (task_t*)ike_auth_create(&new->public, TRUE);
1088 new->task_manager->queue_task(new->task_manager, task);
1089
1090 while (to_restart->remove_last(to_restart, (void**)&child_cfg) == SUCCESS)
1091 {
1092 task = (task_t*)child_create_create(&new->public, child_cfg);
1093 new->task_manager->queue_task(new->task_manager, task);
1094 }
1095 new->task_manager->initiate(new->task_manager);
1096 }
1097 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1098 }
1099 to_route->destroy(to_route);
1100 to_restart->destroy(to_restart);
1101 return DESTROY_ME;
1102 }
1103 return SUCCESS;
1104 }
1105
1106 /**
1107 * Implementation of ike_sa_t.get_prf.
1108 */
1109 static prf_t *get_prf(private_ike_sa_t *this)
1110 {
1111 return this->prf;
1112 }
1113
1114 /**
1115 * Implementation of ike_sa_t.get_prf.
1116 */
1117 static prf_t *get_child_prf(private_ike_sa_t *this)
1118 {
1119 return this->child_prf;
1120 }
1121
1122 /**
1123 * Implementation of ike_sa_t.get_skp_bild
1124 */
1125 static chunk_t get_skp_build(private_ike_sa_t *this)
1126 {
1127 return this->skp_build;
1128 }
1129
1130 /**
1131 * Implementation of ike_sa_t.get_skp_verify
1132 */
1133 static chunk_t get_skp_verify(private_ike_sa_t *this)
1134 {
1135 return this->skp_verify;
1136 }
1137
1138 /**
1139 * Implementation of ike_sa_t.get_id.
1140 */
1141 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1142 {
1143 return this->ike_sa_id;
1144 }
1145
1146 /**
1147 * Implementation of ike_sa_t.get_my_id.
1148 */
1149 static identification_t* get_my_id(private_ike_sa_t *this)
1150 {
1151 return this->my_id;
1152 }
1153
1154 /**
1155 * Implementation of ike_sa_t.set_my_id.
1156 */
1157 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1158 {
1159 DESTROY_IF(this->my_id);
1160 this->my_id = me;
1161 }
1162
1163 /**
1164 * Implementation of ike_sa_t.get_other_id.
1165 */
1166 static identification_t* get_other_id(private_ike_sa_t *this)
1167 {
1168 return this->other_id;
1169 }
1170
1171 /**
1172 * Implementation of ike_sa_t.set_other_id.
1173 */
1174 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1175 {
1176 DESTROY_IF(this->other_id);
1177 this->other_id = other;
1178 }
1179
1180 /**
1181 * Implementation of ike_sa_t.get_other_ca.
1182 */
1183 static ca_info_t* get_other_ca(private_ike_sa_t *this)
1184 {
1185 return this->other_ca;
1186 }
1187
1188 /**
1189 * Implementation of ike_sa_t.set_other_ca.
1190 */
1191 static void set_other_ca(private_ike_sa_t *this, ca_info_t *other_ca)
1192 {
1193 this->other_ca = other_ca;
1194 }
1195
1196 /**
1197 * Implementation of ike_sa_t.set_virtual_ip
1198 */
1199 static void set_virtual_ip(private_ike_sa_t *this, bool local, host_t *ip)
1200 {
1201 if (local)
1202 {
1203 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
1204 if (this->my_virtual_ip)
1205 {
1206 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
1207 charon->kernel_interface->del_ip(charon->kernel_interface,
1208 this->my_virtual_ip,
1209 this->my_host);
1210 this->my_virtual_ip->destroy(this->my_virtual_ip);
1211 }
1212 if (charon->kernel_interface->add_ip(charon->kernel_interface, ip,
1213 this->my_host) == SUCCESS)
1214 {
1215 this->my_virtual_ip = ip->clone(ip);
1216 }
1217 else
1218 {
1219 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
1220 this->my_virtual_ip = NULL;
1221 }
1222 }
1223 else
1224 {
1225 DESTROY_IF(this->other_virtual_ip);
1226 this->other_virtual_ip = ip->clone(ip);
1227 }
1228 }
1229
1230 /**
1231 * Implementation of ike_sa_t.get_virtual_ip
1232 */
1233 static host_t* get_virtual_ip(private_ike_sa_t *this, bool local)
1234 {
1235 if (local)
1236 {
1237 return this->my_virtual_ip;
1238 }
1239 else
1240 {
1241 return this->other_virtual_ip;
1242 }
1243 }
1244
1245 /**
1246 * Implementation of ike_sa_t.enable_extension.
1247 */
1248 static void enable_extension(private_ike_sa_t *this, ike_extension_t extension)
1249 {
1250 this->extensions |= extension;
1251 }
1252
1253 /**
1254 * Implementation of ike_sa_t.has_extension.
1255 */
1256 static bool supports_extension(private_ike_sa_t *this, ike_extension_t extension)
1257 {
1258 return this->extensions & extension;
1259 }
1260
1261 /**
1262 * Implementation of ike_sa_t.derive_keys.
1263 */
1264 static status_t derive_keys(private_ike_sa_t *this,
1265 proposal_t *proposal, chunk_t secret,
1266 chunk_t nonce_i, chunk_t nonce_r,
1267 bool initiator, prf_t *child_prf, prf_t *old_prf)
1268 {
1269 prf_plus_t *prf_plus;
1270 chunk_t skeyseed, key, nonces, prf_plus_seed;
1271 algorithm_t *algo;
1272 size_t key_size;
1273 crypter_t *crypter_i, *crypter_r;
1274 signer_t *signer_i, *signer_r;
1275 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1276 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1277 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1278
1279 /* Create SAs general purpose PRF first, we may use it here */
1280 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1281 {
1282 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1283 return FAILED;
1284 }
1285 this->prf = prf_create(algo->algorithm);
1286 if (this->prf == NULL)
1287 {
1288 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1289 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1290 return FAILED;
1291 }
1292
1293 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1294 nonces = chunk_cat("cc", nonce_i, nonce_r);
1295 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1296 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1297 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1298
1299 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1300 *
1301 * if we are rekeying, SKEYSEED is built on another way
1302 */
1303 if (child_prf == NULL) /* not rekeying */
1304 {
1305 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1306 this->prf->set_key(this->prf, nonces);
1307 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1308 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1309 this->prf->set_key(this->prf, skeyseed);
1310 chunk_free(&skeyseed);
1311 chunk_free(&secret);
1312 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1313 }
1314 else
1315 {
1316 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1317 * use OLD SAs PRF functions for both prf_plus and prf */
1318 secret = chunk_cat("mc", secret, nonces);
1319 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1320 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1321 old_prf->set_key(old_prf, skeyseed);
1322 chunk_free(&skeyseed);
1323 chunk_free(&secret);
1324 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1325 }
1326 chunk_free(&nonces);
1327 chunk_free(&prf_plus_seed);
1328
1329 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1330
1331 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1332 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1333 this->child_prf = prf_create(algo->algorithm);
1334 key_size = this->child_prf->get_key_size(this->child_prf);
1335 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1336 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1337 this->child_prf->set_key(this->child_prf, key);
1338 chunk_free(&key);
1339
1340 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1341 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1342 {
1343 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1344 return FAILED;
1345 }
1346 signer_i = signer_create(algo->algorithm);
1347 signer_r = signer_create(algo->algorithm);
1348 if (signer_i == NULL || signer_r == NULL)
1349 {
1350 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1351 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1352 return FAILED;
1353 }
1354 key_size = signer_i->get_key_size(signer_i);
1355
1356 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1357 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1358 signer_i->set_key(signer_i, key);
1359 chunk_free(&key);
1360
1361 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1362 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1363 signer_r->set_key(signer_r, key);
1364 chunk_free(&key);
1365
1366 if (initiator)
1367 {
1368 this->signer_in = signer_r;
1369 this->signer_out = signer_i;
1370 }
1371 else
1372 {
1373 this->signer_in = signer_i;
1374 this->signer_out = signer_r;
1375 }
1376
1377 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1378 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1379 {
1380 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1381 return FAILED;
1382 }
1383 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1384 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1385 if (crypter_i == NULL || crypter_r == NULL)
1386 {
1387 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1388 "%N (key size %d) not supported!",
1389 encryption_algorithm_names, algo->algorithm, algo->key_size);
1390 return FAILED;
1391 }
1392 key_size = crypter_i->get_key_size(crypter_i);
1393
1394 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1395 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1396 crypter_i->set_key(crypter_i, key);
1397 chunk_free(&key);
1398
1399 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1400 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1401 crypter_r->set_key(crypter_r, key);
1402 chunk_free(&key);
1403
1404 if (initiator)
1405 {
1406 this->crypter_in = crypter_r;
1407 this->crypter_out = crypter_i;
1408 }
1409 else
1410 {
1411 this->crypter_in = crypter_i;
1412 this->crypter_out = crypter_r;
1413 }
1414
1415 /* SK_pi/SK_pr used for authentication => stored for later */
1416 key_size = this->prf->get_key_size(this->prf);
1417 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1418 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1419 if (initiator)
1420 {
1421 this->skp_build = key;
1422 }
1423 else
1424 {
1425 this->skp_verify = key;
1426 }
1427 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1428 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1429 if (initiator)
1430 {
1431 this->skp_verify = key;
1432 }
1433 else
1434 {
1435 this->skp_build = key;
1436 }
1437
1438 /* all done, prf_plus not needed anymore */
1439 prf_plus->destroy(prf_plus);
1440
1441 return SUCCESS;
1442 }
1443
1444 /**
1445 * Implementation of ike_sa_t.add_child_sa.
1446 */
1447 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1448 {
1449 this->child_sas->insert_last(this->child_sas, child_sa);
1450 }
1451
1452 /**
1453 * Implementation of ike_sa_t.get_child_sa.
1454 */
1455 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1456 u_int32_t spi, bool inbound)
1457 {
1458 iterator_t *iterator;
1459 child_sa_t *current, *found = NULL;
1460
1461 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1462 while (iterator->iterate(iterator, (void**)&current))
1463 {
1464 if (current->get_spi(current, inbound) == spi &&
1465 current->get_protocol(current) == protocol)
1466 {
1467 found = current;
1468 }
1469 }
1470 iterator->destroy(iterator);
1471 return found;
1472 }
1473
1474 /**
1475 * Implementation of ike_sa_t.create_child_sa_iterator.
1476 */
1477 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1478 {
1479 return this->child_sas->create_iterator(this->child_sas, TRUE);
1480 }
1481
1482 /**
1483 * Implementation of ike_sa_t.rekey_child_sa.
1484 */
1485 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1486 {
1487 child_sa_t *child_sa;
1488 child_rekey_t *child_rekey;
1489
1490 child_sa = get_child_sa(this, protocol, spi, TRUE);
1491 if (child_sa)
1492 {
1493 child_rekey = child_rekey_create(&this->public, child_sa);
1494 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1495 return this->task_manager->initiate(this->task_manager);
1496 }
1497 return FAILED;
1498 }
1499
1500 /**
1501 * Implementation of ike_sa_t.delete_child_sa.
1502 */
1503 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1504 {
1505 child_sa_t *child_sa;
1506 child_delete_t *child_delete;
1507
1508 child_sa = get_child_sa(this, protocol, spi, TRUE);
1509 if (child_sa)
1510 {
1511 child_delete = child_delete_create(&this->public, child_sa);
1512 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1513 return this->task_manager->initiate(this->task_manager);
1514 }
1515 return FAILED;
1516 }
1517
1518 /**
1519 * Implementation of ike_sa_t.destroy_child_sa.
1520 */
1521 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1522 u_int32_t spi)
1523 {
1524 iterator_t *iterator;
1525 child_sa_t *child_sa;
1526 status_t status = NOT_FOUND;
1527
1528 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1529 while (iterator->iterate(iterator, (void**)&child_sa))
1530 {
1531 if (child_sa->get_protocol(child_sa) == protocol &&
1532 child_sa->get_spi(child_sa, TRUE) == spi)
1533 {
1534 child_sa->destroy(child_sa);
1535 iterator->remove(iterator);
1536 status = SUCCESS;
1537 break;
1538 }
1539 }
1540 iterator->destroy(iterator);
1541 return status;
1542 }
1543
1544 /**
1545 * Implementation of public_ike_sa_t.delete.
1546 */
1547 static status_t delete_(private_ike_sa_t *this)
1548 {
1549 ike_delete_t *ike_delete;
1550
1551 switch (this->state)
1552 {
1553 case IKE_ESTABLISHED:
1554 case IKE_REKEYING:
1555 ike_delete = ike_delete_create(&this->public, TRUE);
1556 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1557 return this->task_manager->initiate(this->task_manager);
1558 default:
1559 DBG1(DBG_IKE, "destroying IKE_SA in state %N without notification",
1560 ike_sa_state_names, this->state);
1561 break;
1562 }
1563 return DESTROY_ME;
1564 }
1565
1566 /**
1567 * Implementation of ike_sa_t.rekey.
1568 */
1569 static status_t rekey(private_ike_sa_t *this)
1570 {
1571 ike_rekey_t *ike_rekey;
1572
1573 ike_rekey = ike_rekey_create(&this->public, TRUE);
1574
1575 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1576 return this->task_manager->initiate(this->task_manager);
1577 }
1578
1579 /**
1580 * Implementation of ike_sa_t.reestablish
1581 */
1582 static status_t reestablish(private_ike_sa_t *this)
1583 {
1584 task_t *task;
1585
1586 task = (task_t*)ike_reauth_create(&this->public);
1587 this->task_manager->queue_task(this->task_manager, task);
1588
1589 return this->task_manager->initiate(this->task_manager);
1590 }
1591
1592 /**
1593 * Implementation of ike_sa_t.inherit.
1594 */
1595 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
1596 {
1597 child_sa_t *child_sa;
1598 host_t *ip;
1599
1600 /* apply hosts and ids */
1601 this->my_host->destroy(this->my_host);
1602 this->other_host->destroy(this->other_host);
1603 this->my_id->destroy(this->my_id);
1604 this->other_id->destroy(this->other_id);
1605 this->my_host = other->my_host->clone(other->my_host);
1606 this->other_host = other->other_host->clone(other->other_host);
1607 this->my_id = other->my_id->clone(other->my_id);
1608 this->other_id = other->other_id->clone(other->other_id);
1609
1610 /* apply virtual assigned IPs... */
1611 if (other->my_virtual_ip)
1612 {
1613 this->my_virtual_ip = other->my_virtual_ip;
1614 other->my_virtual_ip = NULL;
1615 }
1616 if (other->other_virtual_ip)
1617 {
1618 this->other_virtual_ip = other->other_virtual_ip;
1619 other->other_virtual_ip = NULL;
1620 }
1621
1622 /* ... and DNS servers */
1623 while (other->dns_servers->remove_last(other->dns_servers,
1624 (void**)&ip) == SUCCESS)
1625 {
1626 this->dns_servers->insert_first(this->dns_servers, ip);
1627 }
1628
1629 /* adopt all children */
1630 while (other->child_sas->remove_last(other->child_sas,
1631 (void**)&child_sa) == SUCCESS)
1632 {
1633 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1634 }
1635
1636 /* move pending tasks to the new IKE_SA */
1637 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1638
1639 /* we have to initate here, there may be new tasks to handle */
1640 return this->task_manager->initiate(this->task_manager);
1641 }
1642
1643 /**
1644 * Implementation of ike_sa_t.is_natt_enabled.
1645 */
1646 static bool is_natt_enabled(private_ike_sa_t *this)
1647 {
1648 return this->nat_here || this->nat_there;
1649 }
1650
1651 /**
1652 * Implementation of ike_sa_t.enable_natt.
1653 */
1654 static void enable_natt(private_ike_sa_t *this, bool local)
1655 {
1656 if (local)
1657 {
1658 DBG1(DBG_IKE, "local host is behind NAT, scheduling keep alives");
1659 this->nat_here = TRUE;
1660 send_keepalive(this);
1661 }
1662 else
1663 {
1664 DBG1(DBG_IKE, "remote host is behind NAT");
1665 this->nat_there = TRUE;
1666 }
1667 }
1668
1669 /**
1670 * Implementation of ike_sa_t.remove_dns_server
1671 */
1672 static void remove_dns_servers(private_ike_sa_t *this)
1673 {
1674 FILE *file;
1675 struct stat stats;
1676 chunk_t contents, line, orig_line, token;
1677 char string[INET6_ADDRSTRLEN];
1678 host_t *ip;
1679 iterator_t *iterator;
1680
1681 if (this->dns_servers->get_count(this->dns_servers) == 0)
1682 {
1683 /* don't touch anything if we have no nameservers installed */
1684 return;
1685 }
1686
1687 file = fopen(RESOLV_CONF, "r");
1688 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1689 {
1690 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1691 RESOLV_CONF, strerror(errno));
1692 return;
1693 }
1694
1695 contents = chunk_alloca((size_t)stats.st_size);
1696
1697 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1698 {
1699 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1700 fclose(file);
1701 return;
1702 }
1703
1704 fclose(file);
1705 file = fopen(RESOLV_CONF, "w");
1706 if (file == NULL)
1707 {
1708 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1709 RESOLV_CONF, strerror(errno));
1710 return;
1711 }
1712
1713 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
1714 while (fetchline(&contents, &line))
1715 {
1716 bool found = FALSE;
1717 orig_line = line;
1718 if (extract_token(&token, ' ', &line) &&
1719 strncasecmp(token.ptr, "nameserver", token.len) == 0)
1720 {
1721 if (!extract_token(&token, ' ', &line))
1722 {
1723 token = line;
1724 }
1725 iterator->reset(iterator);
1726 while (iterator->iterate(iterator, (void**)&ip))
1727 {
1728 snprintf(string, sizeof(string), "%H", ip);
1729 if (strlen(string) == token.len &&
1730 strncmp(token.ptr, string, token.len) == 0)
1731 {
1732 iterator->remove(iterator);
1733 ip->destroy(ip);
1734 found = TRUE;
1735 break;
1736 }
1737 }
1738 }
1739
1740 if (!found)
1741 {
1742 /* write line untouched back to file */
1743 fwrite(orig_line.ptr, orig_line.len, 1, file);
1744 fprintf(file, "\n");
1745 }
1746 }
1747 iterator->destroy(iterator);
1748 fclose(file);
1749 }
1750
1751 /**
1752 * Implementation of ike_sa_t.add_dns_server
1753 */
1754 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
1755 {
1756 FILE *file;
1757 struct stat stats;
1758 chunk_t contents;
1759
1760 DBG1(DBG_IKE, "installing DNS server %H", dns);
1761
1762 file = fopen(RESOLV_CONF, "a+");
1763 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1764 {
1765 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1766 RESOLV_CONF, strerror(errno));
1767 return;
1768 }
1769
1770 contents = chunk_alloca(stats.st_size);
1771
1772 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1773 {
1774 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1775 fclose(file);
1776 return;
1777 }
1778
1779 fclose(file);
1780 file = fopen(RESOLV_CONF, "w");
1781 if (file == NULL)
1782 {
1783 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1784 RESOLV_CONF, strerror(errno));
1785 return;
1786 }
1787
1788 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
1789 dns, this->other_id) < 0)
1790 {
1791 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
1792 }
1793 else
1794 {
1795 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
1796 }
1797 fwrite(contents.ptr, contents.len, 1, file);
1798
1799 fclose(file);
1800 }
1801
1802 /**
1803 * Implementation of ike_sa_t.destroy.
1804 */
1805 static void destroy(private_ike_sa_t *this)
1806 {
1807 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1808
1809 DESTROY_IF(this->crypter_in);
1810 DESTROY_IF(this->crypter_out);
1811 DESTROY_IF(this->signer_in);
1812 DESTROY_IF(this->signer_out);
1813 DESTROY_IF(this->prf);
1814 DESTROY_IF(this->child_prf);
1815 chunk_free(&this->skp_verify);
1816 chunk_free(&this->skp_build);
1817
1818 if (this->my_virtual_ip)
1819 {
1820 charon->kernel_interface->del_ip(charon->kernel_interface,
1821 this->my_virtual_ip, this->my_host);
1822 this->my_virtual_ip->destroy(this->my_virtual_ip);
1823 }
1824 DESTROY_IF(this->other_virtual_ip);
1825
1826 remove_dns_servers(this);
1827 this->dns_servers->destroy_offset(this->dns_servers, offsetof(host_t, destroy));
1828
1829 DESTROY_IF(this->my_host);
1830 DESTROY_IF(this->other_host);
1831 DESTROY_IF(this->my_id);
1832 DESTROY_IF(this->other_id);
1833
1834 DESTROY_IF(this->ike_cfg);
1835 DESTROY_IF(this->peer_cfg);
1836
1837 this->ike_sa_id->destroy(this->ike_sa_id);
1838 this->task_manager->destroy(this->task_manager);
1839 free(this);
1840 }
1841
1842 /*
1843 * Described in header.
1844 */
1845 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
1846 {
1847 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
1848 static u_int32_t unique_id = 0;
1849
1850 /* Public functions */
1851 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
1852 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
1853 this->public.get_stats = (void (*)(ike_sa_t*,u_int32_t*))get_stats;
1854 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
1855 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
1856 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
1857 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
1858 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
1859 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
1860 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
1861 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
1862 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
1863 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
1864 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
1865 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
1866 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
1867 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
1868 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
1869 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
1870 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
1871 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
1872 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
1873 this->public.get_other_ca = (ca_info_t* (*)(ike_sa_t*)) get_other_ca;
1874 this->public.set_other_ca = (void (*)(ike_sa_t*,ca_info_t*)) set_other_ca;
1875 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
1876 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
1877 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
1878 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
1879 this->public.destroy = (void (*)(ike_sa_t*))destroy;
1880 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
1881 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
1882 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
1883 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
1884 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
1885 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
1886 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
1887 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
1888 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
1889 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
1890 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
1891 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
1892 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
1893 this->public.enable_natt = (void (*)(ike_sa_t*, bool)) enable_natt;
1894 this->public.is_natt_enabled = (bool (*)(ike_sa_t*)) is_natt_enabled;
1895 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
1896 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
1897 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
1898 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
1899 this->public.reset = (void (*)(ike_sa_t*))reset;
1900 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
1901 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
1902 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
1903 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
1904
1905 /* initialize private fields */
1906 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
1907 this->child_sas = linked_list_create();
1908 this->my_host = host_create_any(AF_INET);
1909 this->other_host = host_create_any(AF_INET);
1910 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
1911 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
1912 this->other_ca = NULL;
1913 this->extensions = 0;
1914 this->crypter_in = NULL;
1915 this->crypter_out = NULL;
1916 this->signer_in = NULL;
1917 this->signer_out = NULL;
1918 this->prf = NULL;
1919 this->skp_verify = chunk_empty;
1920 this->skp_build = chunk_empty;
1921 this->child_prf = NULL;
1922 this->nat_here = FALSE;
1923 this->nat_there = FALSE;
1924 this->state = IKE_CREATED;
1925 this->time.inbound = this->time.outbound = time(NULL);
1926 this->time.established = 0;
1927 this->time.rekey = 0;
1928 this->time.delete = 0;
1929 this->ike_cfg = NULL;
1930 this->peer_cfg = NULL;
1931 this->task_manager = task_manager_create(&this->public);
1932 this->unique_id = ++unique_id;
1933 this->my_virtual_ip = NULL;
1934 this->other_virtual_ip = NULL;
1935 this->dns_servers = linked_list_create();
1936 this->keyingtry = 0;
1937
1938 return &this->public;
1939 }