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