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