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