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