corrected typo
[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 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
624 return DESTROY_ME;
625 }
626 }
627
628 /* check if message is trustworthy, and update connection information */
629 if (this->state == IKE_CREATED ||
630 message->get_exchange_type(message) != IKE_SA_INIT)
631 {
632 update_hosts(this, me, other);
633 this->time.inbound = time(NULL);
634 }
635 return this->task_manager->process_message(this->task_manager, message);
636 }
637 }
638
639 /**
640 * apply the connection/policy information to this IKE_SA
641 */
642 static void apply_config(private_ike_sa_t *this,
643 connection_t *connection, policy_t *policy)
644 {
645 host_t *me, *other;
646 identification_t *my_id, *other_id;
647
648 if (this->connection == NULL && this->policy == NULL)
649 {
650 this->connection = connection;
651 connection->get_ref(connection);
652 this->policy = policy;
653 policy->get_ref(policy);
654
655 me = connection->get_my_host(connection);
656 other = connection->get_other_host(connection);
657 my_id = policy->get_my_id(policy);
658 other_id = policy->get_other_id(policy);
659 set_my_host(this, me->clone(me));
660 set_other_host(this, other->clone(other));
661 DESTROY_IF(this->my_id);
662 DESTROY_IF(this->other_id);
663 this->my_id = my_id->clone(my_id);
664 this->other_id = other_id->clone(other_id);
665 }
666 }
667
668 /**
669 * Implementation of ike_sa_t.initiate.
670 */
671 static status_t initiate(private_ike_sa_t *this,
672 connection_t *connection, policy_t *policy)
673 {
674 task_t *task;
675
676 if (this->state == IKE_CREATED)
677 {
678 /* if we aren't established/establishing, do so */
679 apply_config(this, connection, policy);
680
681 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
682 this->task_manager->queue_task(this->task_manager, task);
683 task = (task_t*)ike_natd_create(&this->public, TRUE);
684 this->task_manager->queue_task(this->task_manager, task);
685 task = (task_t*)ike_cert_create(&this->public, TRUE);
686 this->task_manager->queue_task(this->task_manager, task);
687 task = (task_t*)ike_config_create(&this->public, policy);
688 this->task_manager->queue_task(this->task_manager, task);
689 task = (task_t*)ike_auth_create(&this->public, TRUE);
690 this->task_manager->queue_task(this->task_manager, task);
691 }
692
693 task = (task_t*)child_create_create(&this->public, policy);
694 this->task_manager->queue_task(this->task_manager, task);
695
696 return this->task_manager->initiate(this->task_manager);
697 }
698
699 /**
700 * Implementation of ike_sa_t.acquire.
701 */
702 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
703 {
704 policy_t *policy;
705 iterator_t *iterator;
706 child_sa_t *current, *child_sa = NULL;
707 task_t *task;
708 child_create_t *child_create;
709
710 if (this->state == IKE_DELETING)
711 {
712 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
713 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
714 "IKE_SA is deleting", reqid);
715 return FAILED;
716 }
717
718 /* find CHILD_SA */
719 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
720 while (iterator->iterate(iterator, (void**)&current))
721 {
722 if (current->get_reqid(current) == reqid)
723 {
724 child_sa = current;
725 break;
726 }
727 }
728 iterator->destroy(iterator);
729 if (!child_sa)
730 {
731 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
732 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
733 "CHILD_SA not found", reqid);
734 return FAILED;
735 }
736
737 policy = child_sa->get_policy(child_sa);
738
739 if (this->state == IKE_CREATED)
740 {
741 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
742 this->task_manager->queue_task(this->task_manager, task);
743 task = (task_t*)ike_natd_create(&this->public, TRUE);
744 this->task_manager->queue_task(this->task_manager, task);
745 task = (task_t*)ike_cert_create(&this->public, TRUE);
746 this->task_manager->queue_task(this->task_manager, task);
747 task = (task_t*)ike_config_create(&this->public, policy);
748 this->task_manager->queue_task(this->task_manager, task);
749 task = (task_t*)ike_auth_create(&this->public, TRUE);
750 this->task_manager->queue_task(this->task_manager, task);
751 }
752
753 child_create = child_create_create(&this->public, policy);
754 child_create->use_reqid(child_create, reqid);
755 this->task_manager->queue_task(this->task_manager, (task_t*)child_create);
756
757 return this->task_manager->initiate(this->task_manager);
758 }
759
760 /**
761 * compare two lists of traffic selectors for equality
762 */
763 static bool ts_list_equals(linked_list_t *l1, linked_list_t *l2)
764 {
765 bool equals = TRUE;
766 iterator_t *i1, *i2;
767 traffic_selector_t *t1, *t2;
768
769 if (l1->get_count(l1) != l2->get_count(l2))
770 {
771 return FALSE;
772 }
773
774 i1 = l1->create_iterator(l1, TRUE);
775 i2 = l2->create_iterator(l2, TRUE);
776 while (i1->iterate(i1, (void**)&t1) && i2->iterate(i2, (void**)&t2))
777 {
778 if (!t1->equals(t1, t2))
779 {
780 equals = FALSE;
781 break;
782 }
783 }
784 i1->destroy(i1);
785 i2->destroy(i2);
786 return equals;
787 }
788
789 /**
790 * Implementation of ike_sa_t.route.
791 */
792 static status_t route(private_ike_sa_t *this, connection_t *connection, policy_t *policy)
793 {
794 child_sa_t *child_sa = NULL;
795 iterator_t *iterator;
796 linked_list_t *my_ts, *other_ts;
797 status_t status;
798
799 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
800
801 /* check if not already routed*/
802 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
803 while (iterator->iterate(iterator, (void**)&child_sa))
804 {
805 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
806 {
807 linked_list_t *my_ts_conf, *other_ts_conf;
808
809 my_ts = child_sa->get_my_traffic_selectors(child_sa);
810 other_ts = child_sa->get_other_traffic_selectors(child_sa);
811
812 my_ts_conf = policy->get_my_traffic_selectors(policy, this->my_host);
813 other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host);
814
815 if (ts_list_equals(my_ts, my_ts_conf) &&
816 ts_list_equals(other_ts, other_ts_conf))
817 {
818 iterator->destroy(iterator);
819 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
820 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
821 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a policy already routed");
822 return FAILED;
823 }
824 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
825 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
826 }
827 }
828 iterator->destroy(iterator);
829
830 switch (this->state)
831 {
832 case IKE_DELETING:
833 case IKE_REKEYING:
834 SIG(CHILD_ROUTE_FAILED,
835 "unable to route CHILD_SA, as its IKE_SA gets deleted");
836 return FAILED;
837 case IKE_CREATED:
838 /* apply connection information, we need it to acquire */
839 apply_config(this, connection, policy);
840 break;
841 case IKE_CONNECTING:
842 case IKE_ESTABLISHED:
843 default:
844 break;
845 }
846
847 /* install kernel policies */
848 child_sa = child_sa_create(this->my_host, this->other_host,
849 this->my_id, this->other_id, policy, FALSE, 0);
850
851 my_ts = policy->get_my_traffic_selectors(policy, this->my_host);
852 other_ts = policy->get_other_traffic_selectors(policy, this->other_host);
853 status = child_sa->add_policies(child_sa, my_ts, other_ts,
854 policy->get_mode(policy));
855 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
856 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
857 this->child_sas->insert_last(this->child_sas, child_sa);
858 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
859 return status;
860 }
861
862 /**
863 * Implementation of ike_sa_t.unroute.
864 */
865 static status_t unroute(private_ike_sa_t *this, policy_t *policy)
866 {
867 iterator_t *iterator;
868 child_sa_t *child_sa = NULL;
869 bool found = FALSE;
870 linked_list_t *my_ts, *other_ts, *my_ts_conf, *other_ts_conf;
871
872 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
873
874 /* find CHILD_SA in ROUTED state */
875 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
876 while (iterator->iterate(iterator, (void**)&child_sa))
877 {
878 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
879 {
880 my_ts = child_sa->get_my_traffic_selectors(child_sa);
881 other_ts = child_sa->get_other_traffic_selectors(child_sa);
882
883 my_ts_conf = policy->get_my_traffic_selectors(policy, this->my_host);
884 other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host);
885
886 if (ts_list_equals(my_ts, my_ts_conf) &&
887 ts_list_equals(other_ts, other_ts_conf))
888 {
889 iterator->remove(iterator);
890 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
891 child_sa->destroy(child_sa);
892 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
893 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
894 found = TRUE;
895 break;
896 }
897 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
898 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
899 }
900 }
901 iterator->destroy(iterator);
902
903 if (!found)
904 {
905 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
906 return FAILED;
907 }
908 /* if we are not established, and we have no more routed childs, remove whole SA */
909 if (this->state == IKE_CREATED &&
910 this->child_sas->get_count(this->child_sas) == 0)
911 {
912 return DESTROY_ME;
913 }
914 return SUCCESS;
915 }
916
917 /**
918 * Implementation of ike_sa_t.send_dpd
919 */
920 static status_t send_dpd(private_ike_sa_t *this)
921 {
922 send_dpd_job_t *job;
923 time_t diff, delay;
924
925 delay = this->connection->get_dpd_delay(this->connection);
926
927 if (delay == 0)
928 {
929 /* DPD disabled */
930 return SUCCESS;
931 }
932
933 if (this->task_manager->busy(this->task_manager))
934 {
935 /* an exchange is in the air, no need to start a DPD check */
936 diff = 0;
937 }
938 else
939 {
940 /* check if there was any inbound traffic */
941 time_t last_in, now;
942 last_in = get_use_time(this, TRUE);
943 now = time(NULL);
944 diff = now - last_in;
945 if (diff >= delay)
946 {
947 /* to long ago, initiate dead peer detection */
948 task_t *task;
949
950 task = (task_t*)ike_dpd_create(TRUE);
951 diff = 0;
952 DBG1(DBG_IKE, "sending DPD request");
953
954 this->task_manager->queue_task(this->task_manager, task);
955 this->task_manager->initiate(this->task_manager);
956 }
957 }
958 /* recheck in "interval" seconds */
959 job = send_dpd_job_create(this->ike_sa_id);
960 charon->event_queue->add_relative(charon->event_queue, (job_t*)job,
961 (delay - diff) * 1000);
962 return SUCCESS;
963 }
964
965 /**
966 * Implementation of ike_sa_t.send_keepalive
967 */
968 static void send_keepalive(private_ike_sa_t *this)
969 {
970 send_keepalive_job_t *job;
971 time_t last_out, now, diff, interval;
972
973 last_out = get_use_time(this, FALSE);
974 now = time(NULL);
975
976 diff = now - last_out;
977 interval = charon->configuration->get_keepalive_interval(charon->configuration);
978
979 if (diff >= interval)
980 {
981 packet_t *packet;
982 chunk_t data;
983
984 packet = packet_create();
985 packet->set_source(packet, this->my_host->clone(this->my_host));
986 packet->set_destination(packet, this->other_host->clone(this->other_host));
987 data.ptr = malloc(1);
988 data.ptr[0] = 0xFF;
989 data.len = 1;
990 packet->set_data(packet, data);
991 charon->send_queue->add(charon->send_queue, packet);
992 DBG1(DBG_IKE, "sending keep alive");
993 diff = 0;
994 }
995 job = send_keepalive_job_create(this->ike_sa_id);
996 charon->event_queue->add_relative(charon->event_queue, (job_t*)job,
997 (interval - diff) * 1000);
998 }
999
1000 /**
1001 * Implementation of ike_sa_t.get_state.
1002 */
1003 static ike_sa_state_t get_state(private_ike_sa_t *this)
1004 {
1005 return this->state;
1006 }
1007
1008 /**
1009 * Implementation of ike_sa_t.set_state.
1010 */
1011 static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
1012 {
1013 DBG1(DBG_IKE, "IKE_SA state change: %N => %N",
1014 ike_sa_state_names, this->state,
1015 ike_sa_state_names, state);
1016
1017 if (state == IKE_ESTABLISHED)
1018 {
1019 job_t *job;
1020 u_int32_t now = time(NULL);
1021 u_int32_t soft, hard;
1022 bool reauth;
1023
1024 this->time.established = now;
1025 /* start DPD checks */
1026 send_dpd(this);
1027
1028 /* schedule rekeying/reauthentication */
1029 soft = this->connection->get_soft_lifetime(this->connection);
1030 hard = this->connection->get_hard_lifetime(this->connection);
1031 reauth = this->connection->get_reauth(this->connection);
1032 DBG1(DBG_IKE, "scheduling %s in %ds, maximum lifetime %ds",
1033 reauth ? "reauthentication": "rekeying", soft, hard);
1034
1035 if (soft)
1036 {
1037 this->time.rekey = now + soft;
1038 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, reauth);
1039 charon->event_queue->add_relative(charon->event_queue, job,
1040 soft * 1000);
1041 }
1042
1043 if (hard)
1044 {
1045 this->time.delete = now + hard;
1046 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
1047 charon->event_queue->add_relative(charon->event_queue, job,
1048 hard * 1000);
1049 }
1050 }
1051
1052 this->state = state;
1053 }
1054
1055 /**
1056 * Implementation of ike_sa_t.get_prf.
1057 */
1058 static prf_t *get_prf(private_ike_sa_t *this)
1059 {
1060 return this->prf;
1061 }
1062
1063 /**
1064 * Implementation of ike_sa_t.get_prf.
1065 */
1066 static prf_t *get_child_prf(private_ike_sa_t *this)
1067 {
1068 return this->child_prf;
1069 }
1070
1071 /**
1072 * Implementation of ike_sa_t.get_auth_bild
1073 */
1074 static prf_t *get_auth_build(private_ike_sa_t *this)
1075 {
1076 return this->auth_build;
1077 }
1078
1079 /**
1080 * Implementation of ike_sa_t.get_auth_verify
1081 */
1082 static prf_t *get_auth_verify(private_ike_sa_t *this)
1083 {
1084 return this->auth_verify;
1085 }
1086
1087 /**
1088 * Implementation of ike_sa_t.get_id.
1089 */
1090 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1091 {
1092 return this->ike_sa_id;
1093 }
1094
1095 /**
1096 * Implementation of ike_sa_t.get_my_id.
1097 */
1098 static identification_t* get_my_id(private_ike_sa_t *this)
1099 {
1100 return this->my_id;
1101 }
1102
1103 /**
1104 * Implementation of ike_sa_t.set_my_id.
1105 */
1106 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1107 {
1108 DESTROY_IF(this->my_id);
1109 this->my_id = me;
1110 }
1111
1112 /**
1113 * Implementation of ike_sa_t.get_other_id.
1114 */
1115 static identification_t* get_other_id(private_ike_sa_t *this)
1116 {
1117 return this->other_id;
1118 }
1119
1120 /**
1121 * Implementation of ike_sa_t.set_other_id.
1122 */
1123 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1124 {
1125 DESTROY_IF(this->other_id);
1126 this->other_id = other;
1127 }
1128
1129 /**
1130 * Implementation of ike_sa_t.derive_keys.
1131 */
1132 static status_t derive_keys(private_ike_sa_t *this,
1133 proposal_t *proposal, chunk_t secret,
1134 chunk_t nonce_i, chunk_t nonce_r,
1135 bool initiator, prf_t *child_prf, prf_t *old_prf)
1136 {
1137 prf_plus_t *prf_plus;
1138 chunk_t skeyseed, key, nonces, prf_plus_seed;
1139 algorithm_t *algo;
1140 size_t key_size;
1141 crypter_t *crypter_i, *crypter_r;
1142 signer_t *signer_i, *signer_r;
1143 prf_t *prf_i, *prf_r;
1144 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1145 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1146 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1147
1148 /* Create SAs general purpose PRF first, we may use it here */
1149 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1150 {
1151 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1152 return FAILED;
1153 }
1154 this->prf = prf_create(algo->algorithm);
1155 if (this->prf == NULL)
1156 {
1157 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1158 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1159 return FAILED;
1160 }
1161
1162 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1163 nonces = chunk_cat("cc", nonce_i, nonce_r);
1164 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1165 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1166 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1167
1168 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1169 *
1170 * if we are rekeying, SKEYSEED is built on another way
1171 */
1172 if (child_prf == NULL) /* not rekeying */
1173 {
1174 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1175 this->prf->set_key(this->prf, nonces);
1176 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1177 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1178 this->prf->set_key(this->prf, skeyseed);
1179 chunk_free(&skeyseed);
1180 chunk_free(&secret);
1181 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1182 }
1183 else
1184 {
1185 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1186 * use OLD SAs PRF functions for both prf_plus and prf */
1187 secret = chunk_cat("mc", secret, nonces);
1188 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1189 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1190 old_prf->set_key(old_prf, skeyseed);
1191 chunk_free(&skeyseed);
1192 chunk_free(&secret);
1193 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1194 }
1195 chunk_free(&nonces);
1196 chunk_free(&prf_plus_seed);
1197
1198 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1199
1200 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1201 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1202 this->child_prf = prf_create(algo->algorithm);
1203 key_size = this->child_prf->get_key_size(this->child_prf);
1204 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1205 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1206 this->child_prf->set_key(this->child_prf, key);
1207 chunk_free(&key);
1208
1209 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1210 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1211 {
1212 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1213 return FAILED;
1214 }
1215 signer_i = signer_create(algo->algorithm);
1216 signer_r = signer_create(algo->algorithm);
1217 if (signer_i == NULL || signer_r == NULL)
1218 {
1219 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1220 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1221 return FAILED;
1222 }
1223 key_size = signer_i->get_key_size(signer_i);
1224
1225 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1226 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1227 signer_i->set_key(signer_i, key);
1228 chunk_free(&key);
1229
1230 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1231 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1232 signer_r->set_key(signer_r, key);
1233 chunk_free(&key);
1234
1235 if (initiator)
1236 {
1237 this->signer_in = signer_r;
1238 this->signer_out = signer_i;
1239 }
1240 else
1241 {
1242 this->signer_in = signer_i;
1243 this->signer_out = signer_r;
1244 }
1245
1246 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1247 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1248 {
1249 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1250 return FAILED;
1251 }
1252 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1253 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1254 if (crypter_i == NULL || crypter_r == NULL)
1255 {
1256 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1257 "%N (key size %d) not supported!",
1258 encryption_algorithm_names, algo->algorithm, algo->key_size);
1259 return FAILED;
1260 }
1261 key_size = crypter_i->get_key_size(crypter_i);
1262
1263 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1264 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1265 crypter_i->set_key(crypter_i, key);
1266 chunk_free(&key);
1267
1268 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1269 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1270 crypter_r->set_key(crypter_r, key);
1271 chunk_free(&key);
1272
1273 if (initiator)
1274 {
1275 this->crypter_in = crypter_r;
1276 this->crypter_out = crypter_i;
1277 }
1278 else
1279 {
1280 this->crypter_in = crypter_i;
1281 this->crypter_out = crypter_r;
1282 }
1283
1284 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1285 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1286 prf_i = prf_create(algo->algorithm);
1287 prf_r = prf_create(algo->algorithm);
1288
1289 key_size = prf_i->get_key_size(prf_i);
1290 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1291 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1292 prf_i->set_key(prf_i, key);
1293 chunk_free(&key);
1294
1295 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1296 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1297 prf_r->set_key(prf_r, key);
1298 chunk_free(&key);
1299
1300 if (initiator)
1301 {
1302 this->auth_verify = prf_r;
1303 this->auth_build = prf_i;
1304 }
1305 else
1306 {
1307 this->auth_verify = prf_i;
1308 this->auth_build = prf_r;
1309 }
1310
1311 /* all done, prf_plus not needed anymore */
1312 prf_plus->destroy(prf_plus);
1313
1314 return SUCCESS;
1315 }
1316
1317 /**
1318 * Implementation of ike_sa_t.add_child_sa.
1319 */
1320 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1321 {
1322 this->child_sas->insert_last(this->child_sas, child_sa);
1323 }
1324
1325 /**
1326 * Implementation of ike_sa_t.get_child_sa.
1327 */
1328 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1329 u_int32_t spi, bool inbound)
1330 {
1331 iterator_t *iterator;
1332 child_sa_t *current, *found = NULL;
1333
1334 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1335 while (iterator->iterate(iterator, (void**)&current))
1336 {
1337 if (current->get_spi(current, inbound) == spi &&
1338 current->get_protocol(current) == protocol)
1339 {
1340 found = current;
1341 }
1342 }
1343 iterator->destroy(iterator);
1344 return found;
1345 }
1346
1347 /**
1348 * Implementation of ike_sa_t.create_child_sa_iterator.
1349 */
1350 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1351 {
1352 return this->child_sas->create_iterator(this->child_sas, TRUE);
1353 }
1354
1355 /**
1356 * Implementation of ike_sa_t.rekey_child_sa.
1357 */
1358 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1359 {
1360 child_sa_t *child_sa;
1361 child_rekey_t *child_rekey;
1362
1363 child_sa = get_child_sa(this, protocol, spi, TRUE);
1364 if (child_sa)
1365 {
1366 child_rekey = child_rekey_create(&this->public, child_sa);
1367 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1368 return this->task_manager->initiate(this->task_manager);
1369 }
1370 return FAILED;
1371 }
1372
1373 /**
1374 * Implementation of ike_sa_t.delete_child_sa.
1375 */
1376 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1377 {
1378 child_sa_t *child_sa;
1379 child_delete_t *child_delete;
1380
1381 child_sa = get_child_sa(this, protocol, spi, TRUE);
1382 if (child_sa)
1383 {
1384 child_delete = child_delete_create(&this->public, child_sa);
1385 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1386 return this->task_manager->initiate(this->task_manager);
1387 }
1388 return FAILED;
1389 }
1390
1391 /**
1392 * Implementation of ike_sa_t.destroy_child_sa.
1393 */
1394 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1395 u_int32_t spi)
1396 {
1397 iterator_t *iterator;
1398 child_sa_t *child_sa;
1399 status_t status = NOT_FOUND;
1400
1401 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1402 while (iterator->iterate(iterator, (void**)&child_sa))
1403 {
1404 if (child_sa->get_protocol(child_sa) == protocol &&
1405 child_sa->get_spi(child_sa, TRUE) == spi)
1406 {
1407 child_sa->destroy(child_sa);
1408 iterator->remove(iterator);
1409 status = SUCCESS;
1410 break;
1411 }
1412 }
1413 iterator->destroy(iterator);
1414 return status;
1415 }
1416
1417 /**
1418 * Implementation of public_ike_sa_t.delete.
1419 */
1420 static status_t delete_(private_ike_sa_t *this)
1421 {
1422 ike_delete_t *ike_delete;
1423
1424 switch (this->state)
1425 {
1426 case IKE_ESTABLISHED:
1427 DBG1(DBG_IKE, "deleting IKE_SA");
1428 /* do not log when rekeyed */
1429 case IKE_REKEYING:
1430 ike_delete = ike_delete_create(&this->public, TRUE);
1431 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1432 return this->task_manager->initiate(this->task_manager);
1433 default:
1434 DBG1(DBG_IKE, "destroying IKE_SA in state %N without notification",
1435 ike_sa_state_names, this->state);
1436 break;
1437 }
1438 return DESTROY_ME;
1439 }
1440
1441 /**
1442 * Implementation of ike_sa_t.rekey.
1443 */
1444 static status_t rekey(private_ike_sa_t *this)
1445 {
1446 ike_rekey_t *ike_rekey;
1447
1448 ike_rekey = ike_rekey_create(&this->public, TRUE);
1449
1450 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1451 return this->task_manager->initiate(this->task_manager);
1452 }
1453
1454 /**
1455 * Implementation of ike_sa_t.reestablish
1456 */
1457 static void reestablish(private_ike_sa_t *this)
1458 {
1459 ike_sa_id_t *other_id;
1460 private_ike_sa_t *other;
1461 iterator_t *iterator;
1462 child_sa_t *child_sa;
1463 policy_t *policy;
1464 task_t *task;
1465 job_t *job;
1466
1467 other_id = ike_sa_id_create(0, 0, TRUE);
1468 other = (private_ike_sa_t*)charon->ike_sa_manager->checkout(
1469 charon->ike_sa_manager, other_id);
1470 other_id->destroy(other_id);
1471
1472 apply_config(other, this->connection, this->policy);
1473 other->other_host->destroy(other->other_host);
1474 other->other_host = this->other_host->clone(this->other_host);
1475
1476 if (this->state == IKE_ESTABLISHED)
1477 {
1478 task = (task_t*)ike_init_create(&other->public, TRUE, NULL);
1479 other->task_manager->queue_task(other->task_manager, task);
1480 task = (task_t*)ike_natd_create(&other->public, TRUE);
1481 other->task_manager->queue_task(other->task_manager, task);
1482 task = (task_t*)ike_cert_create(&other->public, TRUE);
1483 other->task_manager->queue_task(other->task_manager, task);
1484 task = (task_t*)ike_config_create(&other->public, other->policy);
1485 other->task_manager->queue_task(other->task_manager, task);
1486 task = (task_t*)ike_auth_create(&other->public, TRUE);
1487 other->task_manager->queue_task(other->task_manager, task);
1488 }
1489
1490 other->task_manager->adopt_tasks(other->task_manager, this->task_manager);
1491
1492 /* Create task for established children, adopt routed children directly */
1493 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1494 while(iterator->iterate(iterator, (void**)&child_sa))
1495 {
1496 switch (child_sa->get_state(child_sa))
1497 {
1498 case CHILD_ROUTED:
1499 {
1500 iterator->remove(iterator);
1501 other->child_sas->insert_first(other->child_sas, child_sa);
1502 break;
1503 }
1504 default:
1505 {
1506 policy = child_sa->get_policy(child_sa);
1507 task = (task_t*)child_create_create(&other->public, policy);
1508 other->task_manager->queue_task(other->task_manager, task);
1509 break;
1510 }
1511 }
1512 }
1513 iterator->destroy(iterator);
1514
1515 other->task_manager->initiate(other->task_manager);
1516
1517 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &other->public);
1518
1519 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
1520 charon->job_queue->add(charon->job_queue, job);
1521 }
1522
1523 /**
1524 * Implementation of ike_sa_t.inherit.
1525 */
1526 static void inherit(private_ike_sa_t *this, private_ike_sa_t *other)
1527 {
1528 child_sa_t *child_sa;
1529 host_t *ip;
1530
1531 /* apply hosts and ids */
1532 this->my_host->destroy(this->my_host);
1533 this->other_host->destroy(this->other_host);
1534 this->my_id->destroy(this->my_id);
1535 this->other_id->destroy(this->other_id);
1536 this->my_host = other->my_host->clone(other->my_host);
1537 this->other_host = other->other_host->clone(other->other_host);
1538 this->my_id = other->my_id->clone(other->my_id);
1539 this->other_id = other->other_id->clone(other->other_id);
1540
1541 /* apply virtual assigned IPs... */
1542 if (other->my_virtual_ip)
1543 {
1544 this->my_virtual_ip = other->my_virtual_ip;
1545 other->my_virtual_ip = NULL;
1546 }
1547 if (other->other_virtual_ip)
1548 {
1549 this->other_virtual_ip = other->other_virtual_ip;
1550 other->other_virtual_ip = NULL;
1551 }
1552
1553 /* ... and DNS servers */
1554 while (other->dns_servers->remove_last(other->dns_servers,
1555 (void**)&ip) == SUCCESS)
1556 {
1557 this->dns_servers->insert_first(this->dns_servers, ip);
1558 }
1559
1560 /* adopt all children */
1561 while (other->child_sas->remove_last(other->child_sas,
1562 (void**)&child_sa) == SUCCESS)
1563 {
1564 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1565 }
1566 }
1567
1568 /**
1569 * Implementation of ike_sa_t.is_natt_enabled.
1570 */
1571 static bool is_natt_enabled(private_ike_sa_t *this)
1572 {
1573 return this->nat_here || this->nat_there;
1574 }
1575
1576 /**
1577 * Implementation of ike_sa_t.enable_natt.
1578 */
1579 static void enable_natt(private_ike_sa_t *this, bool local)
1580 {
1581 if (local)
1582 {
1583 DBG1(DBG_IKE, "local host is behind NAT, scheduling keep alives");
1584 this->nat_here = TRUE;
1585 send_keepalive(this);
1586 }
1587 else
1588 {
1589 DBG1(DBG_IKE, "remote host is behind NAT");
1590 this->nat_there = TRUE;
1591 }
1592 }
1593
1594 /**
1595 * Implementation of ike_sa_t.reset
1596 */
1597 static void reset(private_ike_sa_t *this)
1598 {
1599 /* the responder ID is reset, as peer may choose another one */
1600 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
1601 {
1602 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
1603 }
1604
1605 set_state(this, IKE_CREATED);
1606
1607 this->task_manager->reset(this->task_manager);
1608 }
1609
1610 /**
1611 * Implementation of ike_sa_t.set_virtual_ip
1612 */
1613 static void set_virtual_ip(private_ike_sa_t *this, bool local, host_t *ip)
1614 {
1615 if (local)
1616 {
1617 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
1618 if (this->my_virtual_ip)
1619 {
1620 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
1621 charon->kernel_interface->del_ip(charon->kernel_interface,
1622 this->my_virtual_ip,
1623 this->other_host);
1624 this->my_virtual_ip->destroy(this->my_virtual_ip);
1625 }
1626 if (charon->kernel_interface->add_ip(charon->kernel_interface, ip,
1627 this->other_host) == SUCCESS)
1628 {
1629 this->my_virtual_ip = ip->clone(ip);
1630 }
1631 else
1632 {
1633 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
1634 this->my_virtual_ip = NULL;
1635 }
1636 }
1637 else
1638 {
1639 DESTROY_IF(this->other_virtual_ip);
1640 this->other_virtual_ip = ip->clone(ip);
1641 }
1642 }
1643
1644 /**
1645 * Implementation of ike_sa_t.get_virtual_ip
1646 */
1647 static host_t* get_virtual_ip(private_ike_sa_t *this, bool local)
1648 {
1649 if (local)
1650 {
1651 return this->my_virtual_ip;
1652 }
1653 else
1654 {
1655 return this->other_virtual_ip;
1656 }
1657 }
1658
1659 /**
1660 * Implementation of ike_sa_t.remove_dns_server
1661 */
1662 static void remove_dns_servers(private_ike_sa_t *this)
1663 {
1664 FILE *file;
1665 struct stat stats;
1666 chunk_t contents, line, orig_line, token;
1667 char string[INET6_ADDRSTRLEN];
1668 host_t *ip;
1669 iterator_t *iterator;
1670
1671 if (this->dns_servers->get_count(this->dns_servers) == 0)
1672 {
1673 /* don't touch anything if we have no nameservers installed */
1674 return;
1675 }
1676
1677 file = fopen(RESOLV_CONF, "r");
1678 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1679 {
1680 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %m", RESOLV_CONF);
1681 return;
1682 }
1683
1684 contents = chunk_alloca((size_t)stats.st_size);
1685
1686 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1687 {
1688 DBG1(DBG_IKE, "unable to read DNS configuration file: %m");
1689 fclose(file);
1690 return;
1691 }
1692
1693 fclose(file);
1694 file = fopen(RESOLV_CONF, "w");
1695 if (file == NULL)
1696 {
1697 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %m", RESOLV_CONF);
1698 return;
1699 }
1700
1701 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
1702 while (fetchline(&contents, &line))
1703 {
1704 bool found = FALSE;
1705 orig_line = line;
1706 if (extract_token(&token, ' ', &line) &&
1707 strncasecmp(token.ptr, "nameserver", token.len) == 0)
1708 {
1709 if (!extract_token(&token, ' ', &line))
1710 {
1711 token = line;
1712 }
1713 iterator->reset(iterator);
1714 while (iterator->iterate(iterator, (void**)&ip))
1715 {
1716 snprintf(string, sizeof(string), "%H", ip);
1717 if (strlen(string) == token.len &&
1718 strncmp(token.ptr, string, token.len) == 0)
1719 {
1720 iterator->remove(iterator);
1721 ip->destroy(ip);
1722 found = TRUE;
1723 break;
1724 }
1725 }
1726 }
1727
1728 if (!found)
1729 {
1730 /* write line untouched back to file */
1731 fwrite(orig_line.ptr, orig_line.len, 1, file);
1732 fprintf(file, "\n");
1733 }
1734 }
1735 iterator->destroy(iterator);
1736 fclose(file);
1737 }
1738
1739 /**
1740 * Implementation of ike_sa_t.add_dns_server
1741 */
1742 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
1743 {
1744 FILE *file;
1745 struct stat stats;
1746 chunk_t contents;
1747
1748 DBG1(DBG_IKE, "installing DNS server %H", dns);
1749
1750 file = fopen(RESOLV_CONF, "a+");
1751 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1752 {
1753 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %m", RESOLV_CONF);
1754 return;
1755 }
1756
1757 contents = chunk_alloca(stats.st_size);
1758
1759 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1760 {
1761 DBG1(DBG_IKE, "unable to read DNS configuration file: %m");
1762 fclose(file);
1763 return;
1764 }
1765
1766 fclose(file);
1767 file = fopen(RESOLV_CONF, "w");
1768 if (file == NULL)
1769 {
1770 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %m", RESOLV_CONF);
1771 return;
1772 }
1773
1774 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
1775 dns, this->other_id) < 0)
1776 {
1777 DBG1(DBG_IKE, "unable to write DNS configuration: %m");
1778 }
1779 else
1780 {
1781 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
1782 }
1783 fwrite(contents.ptr, contents.len, 1, file);
1784
1785 fclose(file);
1786 }
1787
1788 /**
1789 * output handler in printf()
1790 */
1791 static int print(FILE *stream, const struct printf_info *info,
1792 const void *const *args)
1793 {
1794 int written = 0;
1795 bool reauth = FALSE;
1796 private_ike_sa_t *this = *((private_ike_sa_t**)(args[0]));
1797
1798 if (this->connection)
1799 {
1800 reauth = this->connection->get_reauth(this->connection);
1801 }
1802
1803 if (this == NULL)
1804 {
1805 return fprintf(stream, "(null)");
1806 }
1807
1808 written = fprintf(stream, "%12s[%d]: %N, %H[%D]...%H[%D]", get_name(this),
1809 this->unique_id, ike_sa_state_names, this->state,
1810 this->my_host, this->my_id, this->other_host,
1811 this->other_id);
1812 written += fprintf(stream, "\n%12s[%d]: IKE SPIs: %J, %s in %ds",
1813 get_name(this), this->unique_id, this->ike_sa_id,
1814 this->connection && reauth? "reauthentication":"rekeying",
1815 this->time.rekey - time(NULL));
1816
1817 if (info->alt)
1818 {
1819
1820 }
1821 return written;
1822 }
1823
1824 /**
1825 * register printf() handlers
1826 */
1827 static void __attribute__ ((constructor))print_register()
1828 {
1829 register_printf_function(PRINTF_IKE_SA, print, arginfo_ptr);
1830 }
1831
1832 /**
1833 * Implementation of ike_sa_t.destroy.
1834 */
1835 static void destroy(private_ike_sa_t *this)
1836 {
1837 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1838
1839 DESTROY_IF(this->crypter_in);
1840 DESTROY_IF(this->crypter_out);
1841 DESTROY_IF(this->signer_in);
1842 DESTROY_IF(this->signer_out);
1843 DESTROY_IF(this->prf);
1844 DESTROY_IF(this->child_prf);
1845 DESTROY_IF(this->auth_verify);
1846 DESTROY_IF(this->auth_build);
1847
1848 if (this->my_virtual_ip)
1849 {
1850 charon->kernel_interface->del_ip(charon->kernel_interface,
1851 this->my_virtual_ip, this->other_host);
1852 this->my_virtual_ip->destroy(this->my_virtual_ip);
1853 }
1854 DESTROY_IF(this->other_virtual_ip);
1855
1856 remove_dns_servers(this);
1857 this->dns_servers->destroy_offset(this->dns_servers, offsetof(host_t, destroy));
1858
1859 DESTROY_IF(this->my_host);
1860 DESTROY_IF(this->other_host);
1861 DESTROY_IF(this->my_id);
1862 DESTROY_IF(this->other_id);
1863
1864 DESTROY_IF(this->connection);
1865 DESTROY_IF(this->policy);
1866
1867 this->ike_sa_id->destroy(this->ike_sa_id);
1868 this->task_manager->destroy(this->task_manager);
1869 free(this);
1870 }
1871
1872 /*
1873 * Described in header.
1874 */
1875 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
1876 {
1877 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
1878 static u_int32_t unique_id = 0;
1879
1880 /* Public functions */
1881 this->public.get_state = (ike_sa_state_t(*)(ike_sa_t*)) get_state;
1882 this->public.set_state = (void(*)(ike_sa_t*,ike_sa_state_t)) set_state;
1883 this->public.get_name = (char*(*)(ike_sa_t*))get_name;
1884 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
1885 this->public.initiate = (status_t(*)(ike_sa_t*,connection_t*,policy_t*)) initiate;
1886 this->public.route = (status_t(*)(ike_sa_t*,connection_t*,policy_t*)) route;
1887 this->public.unroute = (status_t(*)(ike_sa_t*,policy_t*)) unroute;
1888 this->public.acquire = (status_t(*)(ike_sa_t*,u_int32_t)) acquire;
1889 this->public.get_connection = (connection_t*(*)(ike_sa_t*))get_connection;
1890 this->public.set_connection = (void(*)(ike_sa_t*,connection_t*))set_connection;
1891 this->public.get_policy = (policy_t*(*)(ike_sa_t*))get_policy;
1892 this->public.set_policy = (void(*)(ike_sa_t*,policy_t*))set_policy;
1893 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
1894 this->public.get_my_host = (host_t*(*)(ike_sa_t*)) get_my_host;
1895 this->public.set_my_host = (void(*)(ike_sa_t*,host_t*)) set_my_host;
1896 this->public.get_other_host = (host_t*(*)(ike_sa_t*)) get_other_host;
1897 this->public.set_other_host = (void(*)(ike_sa_t*,host_t*)) set_other_host;
1898 this->public.get_my_id = (identification_t*(*)(ike_sa_t*)) get_my_id;
1899 this->public.set_my_id = (void(*)(ike_sa_t*,identification_t*)) set_my_id;
1900 this->public.get_other_id = (identification_t*(*)(ike_sa_t*)) get_other_id;
1901 this->public.set_other_id = (void(*)(ike_sa_t*,identification_t*)) set_other_id;
1902 this->public.retransmit = (status_t (*) (ike_sa_t *, u_int32_t)) retransmit;
1903 this->public.delete = (status_t(*)(ike_sa_t*))delete_;
1904 this->public.destroy = (void(*)(ike_sa_t*))destroy;
1905 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
1906 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
1907 this->public.get_prf = (prf_t *(*) (ike_sa_t *)) get_prf;
1908 this->public.get_child_prf = (prf_t *(*) (ike_sa_t *)) get_child_prf;
1909 this->public.get_auth_verify = (prf_t *(*) (ike_sa_t *)) get_auth_verify;
1910 this->public.get_auth_build = (prf_t *(*) (ike_sa_t *)) get_auth_build;
1911 this->public.derive_keys = (status_t (*) (ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
1912 this->public.add_child_sa = (void (*) (ike_sa_t*,child_sa_t*)) add_child_sa;
1913 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
1914 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
1915 this->public.rekey_child_sa = (status_t(*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
1916 this->public.delete_child_sa = (status_t(*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
1917 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
1918 this->public.enable_natt = (void(*)(ike_sa_t*, bool)) enable_natt;
1919 this->public.is_natt_enabled = (bool(*)(ike_sa_t*)) is_natt_enabled;
1920 this->public.rekey = (status_t(*)(ike_sa_t*))rekey;
1921 this->public.reestablish = (void(*)(ike_sa_t*))reestablish;
1922 this->public.inherit = (void(*)(ike_sa_t*,ike_sa_t*))inherit;
1923 this->public.generate_message = (status_t(*)(ike_sa_t*,message_t*,packet_t**))generate_message;
1924 this->public.reset = (void(*)(ike_sa_t*))reset;
1925 this->public.get_unique_id = (u_int32_t(*)(ike_sa_t*))get_unique_id;
1926 this->public.set_virtual_ip = (void(*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
1927 this->public.get_virtual_ip = (host_t*(*)(ike_sa_t*,bool))get_virtual_ip;
1928 this->public.add_dns_server = (void(*)(ike_sa_t*,host_t*))add_dns_server;
1929
1930 /* initialize private fields */
1931 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
1932 this->child_sas = linked_list_create();
1933 this->my_host = host_create_any(AF_INET);
1934 this->other_host = host_create_any(AF_INET);
1935 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
1936 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
1937 this->crypter_in = NULL;
1938 this->crypter_out = NULL;
1939 this->signer_in = NULL;
1940 this->signer_out = NULL;
1941 this->prf = NULL;
1942 this->auth_verify = NULL;
1943 this->auth_build = NULL;
1944 this->child_prf = NULL;
1945 this->nat_here = FALSE;
1946 this->nat_there = FALSE;
1947 this->state = IKE_CREATED;
1948 this->time.inbound = this->time.outbound = time(NULL);
1949 this->time.established = 0;
1950 this->time.rekey = 0;
1951 this->time.delete = 0;
1952 this->connection = NULL;
1953 this->policy = NULL;
1954 this->task_manager = task_manager_create(&this->public);
1955 this->unique_id = ++unique_id;
1956 this->my_virtual_ip = NULL;
1957 this->other_virtual_ip = NULL;
1958 this->dns_servers = linked_list_create();
1959
1960 return &this->public;
1961 }