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