add priority management for kernel policy
[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
28 #include "ike_sa.h"
29
30 #include <types.h>
31 #include <daemon.h>
32 #include <definitions.h>
33 #include <utils/linked_list.h>
34 #include <utils/logger_manager.h>
35 #include <crypto/diffie_hellman.h>
36 #include <crypto/prf_plus.h>
37 #include <crypto/crypters/crypter.h>
38 #include <crypto/hashers/hasher.h>
39 #include <encoding/payloads/sa_payload.h>
40 #include <encoding/payloads/nonce_payload.h>
41 #include <encoding/payloads/ke_payload.h>
42 #include <encoding/payloads/delete_payload.h>
43 #include <encoding/payloads/transform_substructure.h>
44 #include <encoding/payloads/transform_attribute.h>
45 #include <encoding/payloads/ts_payload.h>
46 #include <sa/transactions/transaction.h>
47 #include <sa/transactions/ike_sa_init.h>
48 #include <sa/transactions/delete_ike_sa.h>
49 #include <sa/transactions/create_child_sa.h>
50 #include <sa/transactions/delete_child_sa.h>
51 #include <sa/transactions/dead_peer_detection.h>
52 #include <sa/transactions/rekey_ike_sa.h>
53 #include <queues/jobs/retransmit_request_job.h>
54 #include <queues/jobs/delete_ike_sa_job.h>
55 #include <queues/jobs/send_dpd_job.h>
56 #include <queues/jobs/send_keepalive_job.h>
57 #include <queues/jobs/rekey_ike_sa_job.h>
58 #include <queues/jobs/route_job.h>
59 #include <queues/jobs/initiate_job.h>
60
61 /**
62 * String mappings for ike_sa_state_t.
63 */
64 mapping_t ike_sa_state_m[] = {
65 {IKE_CREATED, "CREATED"},
66 {IKE_CONNECTING, "CONNECTING"},
67 {IKE_ESTABLISHED, "ESTABLISHED"},
68 {IKE_REKEYING, "REKEYING"},
69 {IKE_DELETING, "DELETING"},
70 {MAPPING_END, NULL}
71 };
72
73
74 typedef struct private_ike_sa_t private_ike_sa_t;
75
76 /**
77 * Private data of an ike_sa_t object.
78 */
79 struct private_ike_sa_t {
80
81 /**
82 * Public members
83 */
84 ike_sa_t public;
85
86 /**
87 * Identifier for the current IKE_SA.
88 */
89 ike_sa_id_t *ike_sa_id;
90
91 /**
92 * Current state of the IKE_SA
93 */
94 ike_sa_state_t state;
95
96 /**
97 * Name of the connection used by this IKE_SA
98 */
99 char *name;
100
101 /**
102 * Address of local host
103 */
104 host_t *my_host;
105
106 /**
107 * Address of remote host
108 */
109 host_t *other_host;
110
111 /**
112 * Identification used for us
113 */
114 identification_t *my_id;
115
116 /**
117 * Identification used for other
118 */
119 identification_t *other_id;
120
121 /**
122 * Linked List containing the child sa's of the current IKE_SA.
123 */
124 linked_list_t *child_sas;
125
126 /**
127 * crypter for inbound traffic
128 */
129 crypter_t *crypter_in;
130
131 /**
132 * crypter for outbound traffic
133 */
134 crypter_t *crypter_out;
135
136 /**
137 * Signer for inbound traffic
138 */
139 signer_t *signer_in;
140
141 /**
142 * Signer for outbound traffic
143 */
144 signer_t *signer_out;
145
146 /**
147 * Multi purpose prf, set key, use it, forget it
148 */
149 prf_t *prf;
150
151 /**
152 * Prf function for derivating keymat child SAs
153 */
154 prf_t *child_prf;
155
156 /**
157 * PRF, with key set to pi_key, used for authentication
158 */
159 prf_t *prf_auth_i;
160
161 /**
162 * PRF, with key set to pr_key, used for authentication
163 */
164 prf_t *prf_auth_r;
165
166 /**
167 * A logger for this IKE_SA.
168 */
169 logger_t *logger;
170
171 /**
172 * NAT hasher.
173 */
174 hasher_t *nat_hasher;
175
176 /**
177 * NAT status of local host.
178 */
179 bool nat_here;
180
181 /**
182 * NAT status of remote host.
183 */
184 bool nat_there;
185
186 /**
187 * message ID for next outgoung request
188 */
189 u_int32_t message_id_out;
190
191 /**
192 * Timestamps for this IKE_SA
193 */
194 struct {
195 /** last IKE message received */
196 u_int32_t inbound;
197 /** last IKE message sent */
198 u_int32_t outbound;
199 /** when IKE_SA became established */
200 u_int32_t established;
201 /** when IKE_SA gets rekeyed */
202 u_int32_t rekey;
203 /** when IKE_SA gets deleted */
204 u_int32_t delete;
205 } time;
206
207 /**
208 * interval to send DPD liveness check
209 */
210 time_t dpd_delay;
211
212 /**
213 * number of retransmit sequences to go through before giving up (keyingtries)
214 */
215 u_int32_t retrans_sequences;
216
217 /**
218 * List of queued transactions to process
219 */
220 linked_list_t *transaction_queue;
221
222 /**
223 * Transaction currently initiated
224 * (only one supported yet, window size = 1)
225 */
226 transaction_t *transaction_out;
227
228 /**
229 * last transaction initiated by peer processed.
230 * (only one supported yet, window size = 1)
231 * Stored for retransmission.
232 */
233 transaction_t *transaction_in;
234
235 /**
236 * Next incoming transaction expected. Used to
237 * do multi transaction operations.
238 */
239 transaction_t *transaction_in_next;
240
241 /**
242 * Transaction which rekeys this IKE_SA, used do detect simultaneus rekeying
243 */
244 rekey_ike_sa_t *rekeying_transaction;
245 };
246
247 /**
248 * get the time of the latest traffic processed by the kernel
249 */
250 static time_t get_kernel_time(private_ike_sa_t* this, bool inbound)
251 {
252 iterator_t *iterator;
253 child_sa_t *child_sa;
254 time_t latest = 0, use_time;
255
256 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
257 while (iterator->iterate(iterator, (void**)&child_sa))
258 {
259 if (child_sa->get_use_time(child_sa, inbound, &use_time) == SUCCESS)
260 {
261 latest = max(latest, use_time);
262 }
263 }
264 iterator->destroy(iterator);
265
266 return latest;
267 }
268
269 /**
270 * get the time of the latest received traffice
271 */
272 static time_t get_time_inbound(private_ike_sa_t *this)
273 {
274 return max(this->time.inbound, get_kernel_time(this, TRUE));
275 }
276
277 /**
278 * get the time of the latest sent traffic
279 */
280 static time_t get_time_outbound(private_ike_sa_t *this)
281 {
282 return max(this->time.outbound, get_kernel_time(this, FALSE));
283 }
284
285 /**
286 * Implementation of ike_sa_t.get_name.
287 */
288 static char *get_name(private_ike_sa_t *this)
289 {
290 return this->name;
291 }
292
293 /**
294 * Implementation of ike_sa_t.set_name.
295 */
296 static void set_name(private_ike_sa_t *this, char* name)
297 {
298 free(this->name);
299 this->name = strdup(name);
300 }
301
302 /**
303 * Implementation of ike_sa_t.get_my_host.
304 */
305 static host_t *get_my_host(private_ike_sa_t *this)
306 {
307 return this->my_host;
308 }
309
310 /**
311 * Implementation of ike_sa_t.set_my_host.
312 */
313 static void set_my_host(private_ike_sa_t *this, host_t *me)
314 {
315 DESTROY_IF(this->my_host);
316 this->my_host = me;
317 }
318
319 /**
320 * Implementation of ike_sa_t.get_other_host.
321 */
322 static host_t *get_other_host(private_ike_sa_t *this)
323 {
324 return this->other_host;
325 }
326
327 /**
328 * Implementation of ike_sa_t.set_other_host.
329 */
330 static void set_other_host(private_ike_sa_t *this, host_t *other)
331 {
332 DESTROY_IF(this->other_host);
333 this->other_host = other;
334 }
335
336 /**
337 * Update connection host, as addresses may change (NAT)
338 */
339 static void update_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
340 {
341 /*
342 * Quoting RFC 4306:
343 *
344 * 2.11. Address and Port Agility
345 *
346 * IKE runs over UDP ports 500 and 4500, and implicitly sets up ESP and
347 * AH associations for the same IP addresses it runs over. The IP
348 * addresses and ports in the outer header are, however, not themselves
349 * cryptographically protected, and IKE is designed to work even through
350 * Network Address Translation (NAT) boxes. An implementation MUST
351 * accept incoming requests even if the source port is not 500 or 4500,
352 * and MUST respond to the address and port from which the request was
353 * received. It MUST specify the address and port at which the request
354 * was received as the source address and port in the response. IKE
355 * functions identically over IPv4 or IPv6.
356 *
357 * [...]
358 *
359 * There are cases where a NAT box decides to remove mappings that
360 * are still alive (for example, the keepalive interval is too long,
361 * or the NAT box is rebooted). To recover in these cases, hosts
362 * that are not behind a NAT SHOULD send all packets (including
363 * retransmission packets) to the IP address and port from the last
364 * valid authenticated packet from the other end (i.e., dynamically
365 * update the address). A host behind a NAT SHOULD NOT do this
366 * because it opens a DoS attack possibility. Any authenticated IKE
367 * packet or any authenticated UDP-encapsulated ESP packet can be
368 * used to detect that the IP address or the port has changed.
369 */
370 iterator_t *iterator = NULL;
371 child_sa_t *child_sa = NULL;
372 host_diff_t my_diff, other_diff;
373
374 if (this->my_host->is_anyaddr(this->my_host) ||
375 this->other_host->is_anyaddr(this->other_host))
376 {
377 /* on first received message */
378 this->my_host->destroy(this->my_host);
379 this->my_host = me->clone(me);
380 this->other_host->destroy(this->other_host);
381 this->other_host = other->clone(other);
382 return;
383 }
384
385 my_diff = me->get_differences(me, this->my_host);
386 other_diff = other->get_differences(other, this->other_host);
387
388 if (!my_diff && !other_diff)
389 {
390 return;
391 }
392
393 if (my_diff)
394 {
395 this->my_host->destroy(this->my_host);
396 this->my_host = me->clone(me);
397 }
398
399 if (!this->nat_here)
400 {
401 /* update without restrictions if we are not NATted */
402 if (other_diff)
403 {
404 this->other_host->destroy(this->other_host);
405 this->other_host = other->clone(other);
406 }
407 }
408 else
409 {
410 /* if we are natted, only port may change */
411 if (other_diff & HOST_DIFF_ADDR)
412 {
413 return;
414 }
415 else if (other_diff & HOST_DIFF_PORT)
416 {
417 this->other_host->set_port(this->other_host, other->get_port(other));
418 }
419 }
420 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
421 while (iterator->iterate(iterator, (void**)&child_sa))
422 {
423 child_sa->update_hosts(child_sa, this->my_host, this->other_host,
424 my_diff, other_diff);
425 /* TODO: what to do if update fails? Delete CHILD_SA? */
426 }
427 iterator->destroy(iterator);
428 }
429
430 /**
431 * called when the peer is not responding anymore
432 */
433 static void dpd_detected(private_ike_sa_t *this)
434 {
435 /* check for childrens with dpdaction=hold */
436 connection_t *connection = NULL;
437 policy_t *policy;
438 linked_list_t *my_ts, *other_ts;
439 child_sa_t* child_sa;
440 dpd_action_t action;
441 job_t *job;
442
443 this->logger->log(this->logger, CONTROL|LEVEL1,
444 "dead peer detected, handling CHILD_SAs dpd action");
445
446 while(this->child_sas->remove_first(this->child_sas,
447 (void**)&child_sa) == SUCCESS)
448 {
449 /* get the policy which belongs to this CHILD */
450 my_ts = child_sa->get_my_traffic_selectors(child_sa);
451 other_ts = child_sa->get_other_traffic_selectors(child_sa);
452 policy = charon->policies->get_policy(charon->policies,
453 this->my_id, this->other_id,
454 my_ts, other_ts,
455 this->my_host, this->other_host);
456 if (policy == NULL)
457 {
458 this->logger->log(this->logger, ERROR,
459 "no policy found for this CHILD_SA");
460 continue;
461 }
462
463 action = policy->get_dpd_action(policy);
464 /* get a connection for further actions */
465 if (connection == NULL &&
466 (action == DPD_ROUTE || action == DPD_RESTART))
467 {
468 connection = charon->connections->get_connection_by_hosts(
469 charon->connections,
470 this->my_host, this->other_host);
471 if (connection == NULL)
472 {
473 this->logger->log(this->logger, ERROR,
474 "no connection found for this IKE_SA");
475 break;
476 }
477 }
478
479 this->logger->log(this->logger, CONTROL, "dpd action for %s is %s",
480 policy->get_name(policy),
481 mapping_find(dpd_action_m, action));
482
483 switch (action)
484 {
485 case DPD_ROUTE:
486 connection->get_ref(connection);
487 job = (job_t*)route_job_create(connection, policy, TRUE);
488 charon->job_queue->add(charon->job_queue, job);
489 break;
490 case DPD_RESTART:
491 connection->get_ref(connection);
492 job = (job_t*)initiate_job_create(connection, policy);
493 charon->job_queue->add(charon->job_queue, job);
494 break;
495 default:
496 policy->destroy(policy);
497 break;
498 }
499 child_sa->destroy(child_sa);
500 }
501 DESTROY_IF(connection);
502 }
503
504 /**
505 * send a request and schedule retransmission
506 */
507 static status_t transmit_request(private_ike_sa_t *this)
508 {
509 message_t *request;
510 packet_t *packet;
511 status_t status;
512 retransmit_request_job_t *job;
513 u_int32_t transmitted;
514 u_int32_t timeout;
515 transaction_t *transaction = this->transaction_out;
516 u_int32_t message_id;
517
518 transmitted = transaction->requested(transaction);
519 timeout = charon->configuration->get_retransmit_timeout(charon->configuration,
520 transmitted,
521 this->retrans_sequences);
522 if (timeout == 0)
523 {
524 this->logger->log(this->logger, ERROR,
525 "giving up after %d retransmits, deleting IKE_SA",
526 transmitted - 1);
527 dpd_detected(this);
528 return DESTROY_ME;
529 }
530
531 status = transaction->get_request(transaction, &request);
532 if (status != SUCCESS)
533 {
534 this->logger->log(this->logger, ERROR,
535 "generating request failed");
536 return status;
537 }
538 message_id = transaction->get_message_id(transaction);
539 /* if we retransmit, the request is already generated */
540 if (transmitted == 0)
541 {
542 status = request->generate(request, this->crypter_out, this->signer_out, &packet);
543 if (status != SUCCESS)
544 {
545 this->logger->log(this->logger, ERROR,
546 "request generation failed. transaction discarded");
547 return FAILED;
548 }
549 }
550 else
551 {
552 this->logger->log(this->logger, CONTROL,
553 "sending retransmit %d for %s request with message ID %d",
554 transmitted,
555 mapping_find(exchange_type_m, request->get_exchange_type(request)),
556 message_id);
557 packet = request->get_packet(request);
558 }
559 /* finally send */
560 charon->send_queue->add(charon->send_queue, packet);
561 this->time.outbound = time(NULL);
562
563 /* schedule retransmission job */
564 job = retransmit_request_job_create(message_id, this->ike_sa_id);
565 charon->event_queue->add_relative(charon->event_queue, (job_t*)job, timeout);
566 return SUCCESS;
567 }
568
569 /**
570 * Implementation of ike_sa.retransmit_request.
571 */
572 static status_t retransmit_request(private_ike_sa_t *this, u_int32_t message_id)
573 {
574 if (this->transaction_out == NULL ||
575 this->transaction_out->get_message_id(this->transaction_out) != message_id)
576 {
577 /* no retransmit necessary, transaction did already complete */
578 return SUCCESS;
579 }
580 return transmit_request(this);
581 }
582
583 /**
584 * Check for transactions in the queue and initiate the first transaction found.
585 */
586 static status_t process_transaction_queue(private_ike_sa_t *this)
587 {
588 if (this->transaction_out)
589 {
590 /* already a transaction in progress */
591 return SUCCESS;
592 }
593
594 while (TRUE)
595 {
596 if (this->transaction_queue->remove_first(this->transaction_queue,
597 (void**)&this->transaction_out) != SUCCESS)
598 {
599 /* transaction queue empty */
600 return SUCCESS;
601 }
602 switch (transmit_request(this))
603 {
604 case SUCCESS:
605 return SUCCESS;
606 case DESTROY_ME:
607 /* critical, IKE_SA unusable, destroy immediately */
608 this->logger->log(this->logger, ERROR,
609 "transaction initiaton failed, deleting IKE_SA");
610 return DESTROY_ME;
611 default:
612 /* discard transaction, process next one */
613 this->logger->log(this->logger, ERROR,
614 "transaction initiation failed, discarded");
615 this->transaction_out->destroy(this->transaction_out);
616 this->transaction_out = NULL;
617 /* handle next transaction */
618 continue;
619 }
620 }
621 }
622
623 /**
624 * Queue a new transaction and execute the next outstanding transaction
625 */
626 static status_t queue_transaction(private_ike_sa_t *this, transaction_t *transaction, bool prefer)
627 {
628 /* inject next transaction */
629 if (transaction)
630 {
631 if (prefer)
632 {
633 this->transaction_queue->insert_first(this->transaction_queue, transaction);
634 }
635 else
636 {
637 this->transaction_queue->insert_last(this->transaction_queue, transaction);
638 }
639 }
640 /* process a transaction */
641 return process_transaction_queue(this);
642 }
643
644 /**
645 * process an incoming request.
646 */
647 static status_t process_request(private_ike_sa_t *this, message_t *request)
648 {
649 transaction_t *last, *current = NULL;
650 message_t *response;
651 packet_t *packet;
652 u_int32_t request_mid;
653 status_t status;
654
655 request_mid = request->get_message_id(request);
656 last = this->transaction_in;
657
658 /* check if message ID is correct */
659 if (last)
660 {
661 u_int32_t last_mid = last->get_message_id(last);
662
663 if (last_mid == request_mid)
664 {
665 /* retransmit detected */
666 this->logger->log(this->logger, ERROR,
667 "received retransmitted request for message ID %d, retransmitting response",
668 request_mid);
669 last->get_response(last, request, &response, &this->transaction_in_next);
670 packet = response->get_packet(response);
671 charon->send_queue->add(charon->send_queue, packet);
672 this->time.outbound = time(NULL);
673 return SUCCESS;
674 }
675
676 if (last_mid > request_mid)
677 {
678 /* something seriously wrong here, message id may not decrease */
679 this->logger->log(this->logger, ERROR,
680 "received request with message ID %d, excepted %d, ingored",
681 request_mid, last_mid + 1);
682 return FAILED;
683 }
684 /* we allow jumps in message IDs, as long as they are incremental */
685 if (last_mid + 1 < request_mid)
686 {
687 this->logger->log(this->logger, ERROR,
688 "received request with message ID %d, excepted %d",
689 request_mid, last_mid + 1);
690 }
691 }
692 else
693 {
694 if (request_mid != 0)
695 {
696 /* warn, but allow it */
697 this->logger->log(this->logger, CONTROL,
698 "first received request has message ID %d, excepted 0",
699 request_mid);
700 }
701 }
702
703 /* check if we already have a pre-created transaction for this request */
704 if (this->transaction_in_next)
705 {
706 current = this->transaction_in_next;
707 this->transaction_in_next = NULL;
708 }
709 else
710 {
711 current = transaction_create(&this->public, request);
712 if (current == NULL)
713 {
714 this->logger->log(this->logger, ERROR,
715 "no idea how to handle received message (%d), ignored",
716 request->get_exchange_type(request));
717 return FAILED;
718 }
719 }
720
721 /* send message. get_request() always gives a valid response */
722 status = current->get_response(current, request, &response, &this->transaction_in_next);
723 if (response->generate(response, this->crypter_out, this->signer_out, &packet) != SUCCESS)
724 {
725 this->logger->log(this->logger, ERROR,
726 "response generation failed, discarding transaction");
727 current->destroy(current);
728 return FAILED;
729 }
730
731 charon->send_queue->add(charon->send_queue, packet);
732 this->time.outbound = time(NULL);
733 /* act depending on transaction result */
734 switch (status)
735 {
736 case DESTROY_ME:
737 /* transactions says we should destroy the IKE_SA, so do it */
738 current->destroy(current);
739 return DESTROY_ME;
740 default:
741 /* store for retransmission, destroy old transaction */
742 this->transaction_in = current;
743 if (last)
744 {
745 last->destroy(last);
746 }
747 return SUCCESS;
748 }
749 }
750
751 /**
752 * process an incoming response
753 */
754 static status_t process_response(private_ike_sa_t *this, message_t *response)
755 {
756 transaction_t *current, *new = NULL;
757
758 current = this->transaction_out;
759 /* check if message ID is that of our currently active transaction */
760 if (current == NULL ||
761 current->get_message_id(current) != response->get_message_id(response))
762 {
763 this->logger->log(this->logger, ERROR,
764 "received response with message ID %d not requested, ignored");
765 return FAILED;
766 }
767
768 switch (current->conclude(current, response, &new))
769 {
770 case DESTROY_ME:
771 /* state requested to destroy IKE_SA */
772 return DESTROY_ME;
773 default:
774 /* discard transaction, process next one */
775 break;
776 }
777 /* transaction comleted, remove */
778 current->destroy(current);
779 this->transaction_out = NULL;
780
781 /* queue new transaction */
782 return queue_transaction(this, new, TRUE);
783 }
784
785 /**
786 * send a notify back to the sender
787 */
788 static void send_notify_response(private_ike_sa_t *this,
789 message_t *request,
790 notify_type_t type)
791 {
792 notify_payload_t *notify;
793 message_t *response;
794 host_t *src, *dst;
795 packet_t *packet;
796
797 response = message_create();
798 dst = request->get_source(request);
799 src = request->get_destination(request);
800 response->set_source(response, src->clone(src));
801 response->set_destination(response, dst->clone(dst));
802 response->set_exchange_type(response, request->get_exchange_type(request));
803 response->set_request(response, FALSE);
804 response->set_message_id(response, request->get_message_id(request));
805 response->set_ike_sa_id(response, this->ike_sa_id);
806 notify = notify_payload_create_from_protocol_and_type(PROTO_NONE, type);
807 response->add_payload(response, (payload_t *)notify);
808 if (response->generate(response, this->crypter_out, this->signer_out, &packet) != SUCCESS)
809 {
810 response->destroy(response);
811 return;
812 }
813 charon->send_queue->add(charon->send_queue, packet);
814 this->time.outbound = time(NULL);
815 response->destroy(response);
816 return;
817 }
818
819
820 /**
821 * Implementation of ike_sa_t.process_message.
822 */
823 static status_t process_message(private_ike_sa_t *this, message_t *message)
824 {
825 status_t status;
826 bool is_request;
827
828 is_request = message->get_request(message);
829
830 status = message->parse_body(message, this->crypter_in, this->signer_in);
831 if (status != SUCCESS)
832 {
833 if (is_request)
834 {
835 switch (status)
836 {
837 case NOT_SUPPORTED:
838 this->logger->log(this->logger, ERROR,
839 "ciritcal unknown payloads found");
840 if (is_request)
841 {
842 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
843 }
844 break;
845 case PARSE_ERROR:
846 this->logger->log(this->logger, ERROR,
847 "message parsing failed");
848 if (is_request)
849 {
850 send_notify_response(this, message, INVALID_SYNTAX);
851 }
852 break;
853 case VERIFY_ERROR:
854 this->logger->log(this->logger, ERROR,
855 "message verification failed");
856 if (is_request)
857 {
858 send_notify_response(this, message, INVALID_SYNTAX);
859 }
860 break;
861 case FAILED:
862 this->logger->log(this->logger, ERROR,
863 "integrity check failed");
864 /* ignored */
865 break;
866 case INVALID_STATE:
867 this->logger->log(this->logger, ERROR,
868 "found encrypted message, but no keys available");
869 if (is_request)
870 {
871 send_notify_response(this, message, INVALID_SYNTAX);
872 }
873 default:
874 break;
875 }
876 }
877 this->logger->log(this->logger, ERROR,
878 "%s %s with message ID %d processing failed",
879 mapping_find(exchange_type_m, message->get_exchange_type(message)),
880 message->get_request(message) ? "request" : "response",
881 message->get_message_id(message));
882 }
883 else
884 {
885 /* check if message is trustworthy, and update connection information */
886 if (this->state == IKE_CREATED ||
887 message->get_exchange_type(message) != IKE_SA_INIT)
888 {
889 update_hosts(this, message->get_destination(message),
890 message->get_source(message));
891 this->time.inbound = time(NULL);
892 }
893 if (is_request)
894 {
895 status = process_request(this, message);
896 }
897 else
898 {
899 status = process_response(this, message);
900 }
901 }
902 return status;
903 }
904
905 /**
906 * Implementation of ike_sa_t.initiate.
907 */
908 static status_t initiate(private_ike_sa_t *this,
909 connection_t *connection, policy_t *policy)
910 {
911 switch (this->state)
912 {
913 case IKE_CREATED:
914 {
915 /* in state CREATED, we must do the ike_sa_init
916 * and ike_auth transactions. Along with these,
917 * a CHILD_SA with the supplied policy is set up.
918 */
919 ike_sa_init_t *ike_sa_init;
920
921 this->logger->log(this->logger, CONTROL,
922 "initiating IKE_SA");
923 DESTROY_IF(this->my_host);
924 this->my_host = connection->get_my_host(connection);
925 this->my_host = this->my_host->clone(this->my_host);
926 DESTROY_IF(this->other_host);
927 this->other_host = connection->get_other_host(connection);
928 this->other_host = this->other_host->clone(this->other_host);
929 this->retrans_sequences = connection->get_retrans_seq(connection);
930 this->dpd_delay = connection->get_dpd_delay(connection);
931
932 this->message_id_out = 1;
933 ike_sa_init = ike_sa_init_create(&this->public);
934 ike_sa_init->set_config(ike_sa_init, connection, policy);
935 return queue_transaction(this, (transaction_t*)ike_sa_init, TRUE);
936 }
937 case IKE_DELETING:
938 case IKE_REKEYING:
939 {
940 /* if we are in DELETING/REKEYING, we deny set up of a policy. */
941 this->logger->log(this->logger, CONTROL,
942 "creating CHILD_SA discarded, as IKE_SA is in state %s",
943 mapping_find(ike_sa_state_m, this->state));
944 policy->destroy(policy);
945 connection->destroy(connection);
946 return FAILED;
947 }
948 case IKE_CONNECTING:
949 case IKE_ESTABLISHED:
950 {
951 /* if we are ESTABLISHED or CONNECTING,we queue the
952 * transaction to create the CHILD_SA. It gets processed
953 * when the IKE_SA is ready to do so. We don't need the
954 * connection, as the IKE_SA is already established/establishing.
955 */
956 create_child_sa_t *create_child;
957
958 this->logger->log(this->logger, CONTROL,
959 "initiating CHILD_SA");
960
961 connection->destroy(connection);
962 create_child = create_child_sa_create(&this->public);
963 create_child->set_policy(create_child, policy);
964 return queue_transaction(this, (transaction_t*)create_child, FALSE);
965 }
966 }
967 return FAILED;
968 }
969
970 /**
971 * Implementation of ike_sa_t.acquire.
972 */
973 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
974 {
975 connection_t *connection;
976 policy_t *policy;
977 iterator_t *iterator;
978 child_sa_t *current, *child_sa = NULL;
979 linked_list_t *my_ts, *other_ts;
980
981 if (this->state == IKE_DELETING)
982 {
983 this->logger->log(this->logger, CONTROL,
984 "acquiring CHILD_SA with reqid %d discarded, as IKE_SA is deleting",
985 reqid);
986 return FAILED;
987 }
988
989
990 /* find CHILD_SA */
991 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
992 while (iterator->iterate(iterator, (void**)&current))
993 {
994 if (current->get_reqid(current) == reqid)
995 {
996 //iterator->remove(iterator);
997 child_sa = current;
998 break;
999 }
1000 }
1001 iterator->destroy(iterator);
1002 if (!child_sa)
1003 {
1004 this->logger->log(this->logger, ERROR,
1005 "CHILD_SA with reqid %d not found, unable to acquire",
1006 reqid);
1007 return FAILED;
1008 }
1009 my_ts = child_sa->get_my_traffic_selectors(child_sa);
1010 other_ts = child_sa->get_other_traffic_selectors(child_sa);
1011
1012 policy = charon->policies->get_policy(charon->policies,
1013 this->my_id, this->other_id,
1014 my_ts, other_ts,
1015 this->my_host, this->other_host);
1016 //child_sa->destroy(child_sa);
1017 if (policy == NULL)
1018 {
1019 this->logger->log(this->logger, ERROR,
1020 "no policy found to acquire CHILD_SA with reqid %d",
1021 reqid);
1022 return FAILED;
1023 }
1024
1025 switch (this->state)
1026 {
1027 case IKE_CREATED:
1028 {
1029 ike_sa_init_t *ike_sa_init;
1030
1031 this->logger->log(this->logger, CONTROL,
1032 "acquiring CHILD_SA with reqid %d, IKE_SA setup needed",
1033 reqid);
1034
1035 connection = charon->connections->get_connection_by_hosts(
1036 charon->connections, this->my_host, this->other_host);
1037
1038 if (connection == NULL)
1039 {
1040 this->logger->log(this->logger, ERROR,
1041 "no connection found to acquire IKE_SA for CHILD_SA with reqid %d",
1042 reqid);
1043 policy->destroy(policy);
1044 return FAILED;
1045 }
1046
1047 this->message_id_out = 1;
1048 ike_sa_init = ike_sa_init_create(&this->public);
1049 ike_sa_init->set_config(ike_sa_init, connection, policy);
1050 /* reuse existing reqid */
1051 ike_sa_init->set_reqid(ike_sa_init, reqid);
1052 return queue_transaction(this, (transaction_t*)ike_sa_init, TRUE);
1053 }
1054 case IKE_CONNECTING:
1055 case IKE_ESTABLISHED:
1056 {
1057 create_child_sa_t *create_child;
1058
1059 this->logger->log(this->logger, CONTROL,
1060 "acquiring CHILD_SA with reqid %d",
1061 reqid);
1062
1063 create_child = create_child_sa_create(&this->public);
1064 create_child->set_policy(create_child, policy);
1065 /* reuse existing reqid */
1066 create_child->set_reqid(create_child, reqid);
1067 return queue_transaction(this, (transaction_t*)create_child, FALSE);
1068 }
1069 default:
1070 break;
1071 }
1072 return FAILED;
1073 }
1074
1075 /**
1076 * destroy a list of traffic selectors
1077 */
1078 static void ts_list_destroy(linked_list_t *list)
1079 {
1080 traffic_selector_t *ts;
1081 while (list->remove_last(list, (void**)&ts) == SUCCESS)
1082 {
1083 ts->destroy(ts);
1084 }
1085 list->destroy(list);
1086 }
1087
1088 /**
1089 * compare two lists of traffic selectors for equality
1090 */
1091 static bool ts_list_equals(linked_list_t *l1, linked_list_t *l2)
1092 {
1093 bool equals = TRUE;
1094 iterator_t *i1, *i2;
1095 traffic_selector_t *t1, *t2;
1096
1097 i1 = l1->create_iterator(l1, TRUE);
1098 i2 = l2->create_iterator(l2, TRUE);
1099 while (i1->iterate(i1, (void**)&t1) && i2->iterate(i2, (void**)&t2))
1100 {
1101 if (!t1->equals(t1, t2))
1102 {
1103 equals = FALSE;
1104 break;
1105 }
1106 }
1107 /* check if one iterator is not at the end */
1108 if (i1->has_next(i1) || i2->has_next(i2))
1109 {
1110 equals = FALSE;
1111 }
1112 i1->destroy(i1);
1113 i2->destroy(i2);
1114 return equals;
1115 }
1116
1117 /**
1118 * Implementation of ike_sa_t.route.
1119 */
1120 static status_t route(private_ike_sa_t *this, connection_t *connection, policy_t *policy)
1121 {
1122 child_sa_t *child_sa = NULL;
1123 iterator_t *iterator;
1124 linked_list_t *my_ts, *other_ts;
1125 status_t status;
1126
1127 /* check if not already routed*/
1128 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1129 while (iterator->iterate(iterator, (void**)&child_sa))
1130 {
1131 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1132 {
1133 linked_list_t *my_ts_conf, *other_ts_conf;
1134
1135 my_ts = child_sa->get_my_traffic_selectors(child_sa);
1136 other_ts = child_sa->get_other_traffic_selectors(child_sa);
1137
1138 my_ts_conf = policy->get_my_traffic_selectors(policy, this->my_host);
1139 other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host);
1140
1141 if (ts_list_equals(my_ts, my_ts_conf) &&
1142 ts_list_equals(other_ts, other_ts_conf))
1143 {
1144 ts_list_destroy(my_ts_conf);
1145 ts_list_destroy(other_ts_conf);
1146 iterator->destroy(iterator);
1147 this->logger->log(this->logger, CONTROL,
1148 "a CHILD_SA with such a policy already routed");
1149
1150 return FAILED;
1151 }
1152 ts_list_destroy(my_ts_conf);
1153 ts_list_destroy(other_ts_conf);
1154 }
1155 }
1156 iterator->destroy(iterator);
1157
1158 switch (this->state)
1159 {
1160 case IKE_CREATED:
1161 case IKE_CONNECTING:
1162 /* we update IKE_SA information as good as possible,
1163 * this allows us to set up the SA later when an acquire comes in. */
1164 if (this->my_id->get_type(this->my_id) == ID_ANY)
1165 {
1166 this->my_id->destroy(this->my_id);
1167 this->my_id = policy->get_my_id(policy);
1168 this->my_id = this->my_id->clone(this->my_id);
1169 }
1170 if (this->other_id->get_type(this->other_id) == ID_ANY)
1171 {
1172 this->other_id->destroy(this->other_id);
1173 this->other_id = policy->get_other_id(policy);
1174 this->other_id = this->other_id->clone(this->other_id);
1175 }
1176 if (this->my_host->is_anyaddr(this->my_host))
1177 {
1178 this->my_host->destroy(this->my_host);
1179 this->my_host = connection->get_my_host(connection);
1180 this->my_host = this->my_host->clone(this->my_host);
1181 }
1182 if (this->other_host->is_anyaddr(this->other_host))
1183 {
1184 this->other_host->destroy(this->other_host);
1185 this->other_host = connection->get_other_host(connection);
1186 this->other_host = this->other_host->clone(this->other_host);
1187 }
1188 set_name(this, connection->get_name(connection));
1189 this->retrans_sequences = connection->get_retrans_seq(connection);
1190 this->dpd_delay = connection->get_dpd_delay(connection);
1191 break;
1192 case IKE_ESTABLISHED:
1193 case IKE_REKEYING:
1194 /* nothing to do. We allow it for rekeying, as it will be
1195 * adopted by the new IKE_SA */
1196 break;
1197 case IKE_DELETING:
1198 /* deny */
1199 return FAILED;
1200 }
1201
1202 child_sa = child_sa_create(0, this->my_host, this->other_host,
1203 0, 0, FALSE);
1204 child_sa->set_name(child_sa, policy->get_name(policy));
1205 my_ts = policy->get_my_traffic_selectors(policy, this->my_host);
1206 other_ts = policy->get_other_traffic_selectors(policy, this->other_host);
1207 status = child_sa->add_policies(child_sa, my_ts, other_ts);
1208 ts_list_destroy(my_ts);
1209 ts_list_destroy(other_ts);
1210 this->child_sas->insert_last(this->child_sas, child_sa);
1211
1212 return status;
1213 }
1214
1215 /**
1216 * Implementation of ike_sa_t.unroute.
1217 */
1218 static status_t unroute(private_ike_sa_t *this, policy_t *policy)
1219 {
1220 iterator_t *iterator;
1221 child_sa_t *child_sa = NULL;
1222 linked_list_t *my_ts, *other_ts, *my_ts_conf, *other_ts_conf;
1223
1224 /* find CHILD_SA in ROUTED state */
1225 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1226 while (iterator->iterate(iterator, (void**)&child_sa))
1227 {
1228 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1229 {
1230 my_ts = child_sa->get_my_traffic_selectors(child_sa);
1231 other_ts = child_sa->get_other_traffic_selectors(child_sa);
1232
1233 my_ts_conf = policy->get_my_traffic_selectors(policy, this->my_host);
1234 other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host);
1235
1236 if (ts_list_equals(my_ts, my_ts_conf) &&
1237 ts_list_equals(other_ts, other_ts_conf))
1238 {
1239 iterator->remove(iterator);
1240 child_sa->destroy(child_sa);
1241 ts_list_destroy(my_ts_conf);
1242 ts_list_destroy(other_ts_conf);
1243 break;
1244 }
1245 ts_list_destroy(my_ts_conf);
1246 ts_list_destroy(other_ts_conf);
1247 }
1248 }
1249 iterator->destroy(iterator);
1250 /* if we are not established, and we have no more routed childs, remove whole SA */
1251 if (this->state == IKE_CREATED &&
1252 this->child_sas->get_count(this->child_sas) == 0)
1253 {
1254 return DESTROY_ME;
1255 }
1256 return SUCCESS;
1257 }
1258
1259 /**
1260 * Implementation of ike_sa_t.send_dpd
1261 */
1262 static status_t send_dpd(private_ike_sa_t *this)
1263 {
1264 send_dpd_job_t *job;
1265 time_t diff;
1266
1267 if (this->dpd_delay == 0)
1268 {
1269 /* DPD disabled */
1270 return SUCCESS;
1271 }
1272
1273 if (this->transaction_out)
1274 {
1275 /* there is a transaction in progress. Come back later */
1276 diff = 0;
1277 }
1278 else
1279 {
1280 /* check if there was any inbound traffic */
1281 time_t last_in, now;
1282 last_in = get_time_inbound(this);
1283 now = time(NULL);
1284 diff = now - last_in;
1285 if (diff >= this->dpd_delay)
1286 {
1287 /* to long ago, initiate dead peer detection */
1288 dead_peer_detection_t *dpd;
1289 this->logger->log(this->logger, CONTROL, "sending DPD request");
1290 dpd = dead_peer_detection_create(&this->public);
1291 queue_transaction(this, (transaction_t*)dpd, FALSE);
1292 diff = 0;
1293 }
1294 }
1295 /* recheck in "interval" seconds */
1296 job = send_dpd_job_create(this->ike_sa_id);
1297 charon->event_queue->add_relative(charon->event_queue, (job_t*)job,
1298 (this->dpd_delay - diff) * 1000);
1299 return SUCCESS;
1300 }
1301
1302 /**
1303 * Implementation of ike_sa_t.send_keepalive
1304 */
1305 static void send_keepalive(private_ike_sa_t *this)
1306 {
1307 send_keepalive_job_t *job;
1308 time_t last_out, now, diff, interval;
1309
1310 last_out = get_time_outbound(this);
1311 now = time(NULL);
1312
1313 diff = now - last_out;
1314 interval = charon->configuration->get_keepalive_interval(charon->configuration);
1315
1316 if (diff >= interval)
1317 {
1318 packet_t *packet;
1319 chunk_t data;
1320
1321 packet = packet_create();
1322 packet->set_source(packet, this->my_host->clone(this->my_host));
1323 packet->set_destination(packet, this->other_host->clone(this->other_host));
1324 data.ptr = malloc(1);
1325 data.ptr[0] = 0xFF;
1326 data.len = 1;
1327 packet->set_data(packet, data);
1328 charon->send_queue->add(charon->send_queue, packet);
1329 this->logger->log(this->logger, CONTROL, "sending keep alive");
1330 diff = 0;
1331 }
1332 job = send_keepalive_job_create(this->ike_sa_id);
1333 charon->event_queue->add_relative(charon->event_queue, (job_t*)job,
1334 (interval - diff) * 1000);
1335 }
1336
1337 /**
1338 * Implementation of ike_sa_t.get_state.
1339 */
1340 static ike_sa_state_t get_state(private_ike_sa_t *this)
1341 {
1342 return this->state;
1343 }
1344
1345 /**
1346 * Implementation of ike_sa_t.set_state.
1347 */
1348 static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
1349 {
1350 this->logger->log(this->logger, CONTROL, "state change: %s => %s",
1351 mapping_find(ike_sa_state_m, this->state),
1352 mapping_find(ike_sa_state_m, state));
1353 if (state == IKE_ESTABLISHED)
1354 {
1355 this->time.established = time(NULL);
1356 this->logger->log(this->logger, AUDIT, "IKE_SA established: %s[%s]...%s[%s]",
1357 this->my_host->get_string(this->my_host),
1358 this->my_id->get_string(this->my_id),
1359 this->other_host->get_string(this->other_host),
1360 this->other_id->get_string(this->other_id));
1361 /* start DPD checks */
1362 send_dpd(this);
1363 }
1364 this->state = state;
1365 }
1366
1367 /**
1368 * Implementation of ike_sa_t.get_prf.
1369 */
1370 static prf_t *get_prf(private_ike_sa_t *this)
1371 {
1372 return this->prf;
1373 }
1374
1375 /**
1376 * Implementation of ike_sa_t.get_prf.
1377 */
1378 static prf_t *get_child_prf(private_ike_sa_t *this)
1379 {
1380 return this->child_prf;
1381 }
1382
1383 /**
1384 * Implementation of ike_sa_t.get_prf_auth_i.
1385 */
1386 static prf_t *get_prf_auth_i(private_ike_sa_t *this)
1387 {
1388 return this->prf_auth_i;
1389 }
1390
1391 /**
1392 * Implementation of ike_sa_t.get_prf_auth_r.
1393 */
1394 static prf_t *get_prf_auth_r(private_ike_sa_t *this)
1395 {
1396 return this->prf_auth_r;
1397 }
1398
1399 /**
1400 * Implementation of ike_sa_t.get_id.
1401 */
1402 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1403 {
1404 return this->ike_sa_id;
1405 }
1406
1407 /**
1408 * Implementation of ike_sa_t.get_my_id.
1409 */
1410 static identification_t* get_my_id(private_ike_sa_t *this)
1411 {
1412 return this->my_id;
1413 }
1414
1415 /**
1416 * Implementation of ike_sa_t.set_my_id.
1417 */
1418 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1419 {
1420 DESTROY_IF(this->my_id);
1421 this->my_id = me;
1422 }
1423
1424 /**
1425 * Implementation of ike_sa_t.get_other_id.
1426 */
1427 static identification_t* get_other_id(private_ike_sa_t *this)
1428 {
1429 return this->other_id;
1430 }
1431
1432 /**
1433 * Implementation of ike_sa_t.set_other_id.
1434 */
1435 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1436 {
1437 DESTROY_IF(this->other_id);
1438 this->other_id = other;
1439 }
1440
1441 /**
1442 * Implementation of ike_sa_t.derive_keys.
1443 */
1444 static status_t derive_keys(private_ike_sa_t *this,
1445 proposal_t *proposal, diffie_hellman_t *dh,
1446 chunk_t nonce_i, chunk_t nonce_r,
1447 bool initiator, prf_t *child_prf, prf_t *old_prf)
1448 {
1449 prf_plus_t *prf_plus;
1450 chunk_t skeyseed, secret, key, nonces, prf_plus_seed;
1451 algorithm_t *algo;
1452 size_t key_size;
1453 crypter_t *crypter_i, *crypter_r;
1454 signer_t *signer_i, *signer_r;
1455 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1456 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1457 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1458
1459 /* Create SAs general purpose PRF first, we may use it here */
1460 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1461 {
1462 this->logger->log(this->logger, ERROR, "no PSEUDO_RANDOM_FUNCTION selected!");
1463 return FAILED;
1464 }
1465 this->prf = prf_create(algo->algorithm);
1466 if (this->prf == NULL)
1467 {
1468 this->logger->log(this->logger, ERROR, "PSEUDO_RANDOM_FUNCTION %s not supported!",
1469 mapping_find(pseudo_random_function_m, algo->algorithm));
1470 return FAILED;
1471 }
1472
1473 dh->get_shared_secret(dh, &secret);
1474 this->logger->log_chunk(this->logger, PRIVATE, "shared Diffie Hellman secret", secret);
1475 nonces = chunk_cat("cc", nonce_i, nonce_r);
1476 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1477 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1478 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1479
1480 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1481 *
1482 * if we are rekeying, SKEYSEED built on another way
1483 */
1484 if (child_prf == NULL) /* not rekeying */
1485 {
1486 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1487 this->prf->set_key(this->prf, nonces);
1488 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1489 this->logger->log_chunk(this->logger, PRIVATE|LEVEL1, "SKEYSEED", skeyseed);
1490 this->prf->set_key(this->prf, skeyseed);
1491 chunk_free(&skeyseed);
1492 chunk_free(&secret);
1493 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1494 }
1495 else
1496 {
1497 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1498 * use OLD SAs PRF functions for both prf_plus and prf */
1499 secret = chunk_cat("mc", secret, nonces);
1500 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1501 this->logger->log_chunk(this->logger, PRIVATE|LEVEL1, "SKEYSEED", skeyseed);
1502 old_prf->set_key(old_prf, skeyseed);
1503 chunk_free(&skeyseed);
1504 chunk_free(&secret);
1505 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1506 }
1507 chunk_free(&nonces);
1508 chunk_free(&prf_plus_seed);
1509
1510 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1511
1512 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1513 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1514 this->child_prf = prf_create(algo->algorithm);
1515 key_size = this->child_prf->get_key_size(this->child_prf);
1516 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1517 this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", key);
1518 this->child_prf->set_key(this->child_prf, key);
1519 chunk_free(&key);
1520
1521 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1522 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1523 {
1524 this->logger->log(this->logger, ERROR, "no INTEGRITY_ALGORITHM selected?!");
1525 return FAILED;
1526 }
1527 signer_i = signer_create(algo->algorithm);
1528 signer_r = signer_create(algo->algorithm);
1529 if (signer_i == NULL || signer_r == NULL)
1530 {
1531 this->logger->log(this->logger, ERROR, "INTEGRITY_ALGORITHM %s not supported!",
1532 mapping_find(integrity_algorithm_m,algo->algorithm));
1533 return FAILED;
1534 }
1535 key_size = signer_i->get_key_size(signer_i);
1536
1537 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1538 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ai secret", key);
1539 signer_i->set_key(signer_i, key);
1540 chunk_free(&key);
1541
1542 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1543 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ar secret", key);
1544 signer_r->set_key(signer_r, key);
1545 chunk_free(&key);
1546
1547 if (initiator)
1548 {
1549 this->signer_in = signer_r;
1550 this->signer_out = signer_i;
1551 }
1552 else
1553 {
1554 this->signer_in = signer_i;
1555 this->signer_out = signer_r;
1556 }
1557
1558 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1559 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1560 {
1561 this->logger->log(this->logger, ERROR, "no ENCRYPTION_ALGORITHM selected!");
1562 return FAILED;
1563 }
1564 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1565 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1566 if (crypter_i == NULL || crypter_r == NULL)
1567 {
1568 this->logger->log(this->logger, ERROR,
1569 "ENCRYPTION_ALGORITHM %s (key size %d) not supported!",
1570 mapping_find(encryption_algorithm_m, algo->algorithm),
1571 algo->key_size);
1572 return FAILED;
1573 }
1574 key_size = crypter_i->get_key_size(crypter_i);
1575
1576 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1577 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", key);
1578 crypter_i->set_key(crypter_i, key);
1579 chunk_free(&key);
1580
1581 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1582 this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", key);
1583 crypter_r->set_key(crypter_r, key);
1584 chunk_free(&key);
1585
1586 if (initiator)
1587 {
1588 this->crypter_in = crypter_r;
1589 this->crypter_out = crypter_i;
1590 }
1591 else
1592 {
1593 this->crypter_in = crypter_i;
1594 this->crypter_out = crypter_r;
1595 }
1596
1597 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1598 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1599 this->prf_auth_i = prf_create(algo->algorithm);
1600 this->prf_auth_r = prf_create(algo->algorithm);
1601
1602 key_size = this->prf_auth_i->get_key_size(this->prf_auth_i);
1603 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1604 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", key);
1605 this->prf_auth_i->set_key(this->prf_auth_i, key);
1606 chunk_free(&key);
1607
1608 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1609 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", key);
1610 this->prf_auth_r->set_key(this->prf_auth_r, key);
1611 chunk_free(&key);
1612
1613 /* all done, prf_plus not needed anymore */
1614 prf_plus->destroy(prf_plus);
1615
1616 return SUCCESS;
1617
1618 }
1619
1620 /**
1621 * Implementation of ike_sa_t.add_child_sa.
1622 */
1623 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1624 {
1625 this->child_sas->insert_last(this->child_sas, child_sa);
1626 }
1627
1628 /**
1629 * Implementation of ike_sa_t.has_child_sa.
1630 */
1631 static bool has_child_sa(private_ike_sa_t *this, u_int32_t reqid)
1632 {
1633 iterator_t *iterator;
1634 child_sa_t *current;
1635 bool found = FALSE;
1636
1637 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1638 while (iterator->iterate(iterator, (void**)&current))
1639 {
1640 if (current->get_reqid(current) == reqid)
1641 {
1642 found = TRUE;
1643 break;
1644 }
1645 }
1646 iterator->destroy(iterator);
1647 return found;
1648 }
1649
1650 /**
1651 * Implementation of ike_sa_t.get_child_sa.
1652 */
1653 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1654 u_int32_t spi, bool inbound)
1655 {
1656 iterator_t *iterator;
1657 child_sa_t *current, *found = NULL;
1658
1659 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1660 while (iterator->has_next(iterator))
1661 {
1662 iterator->current(iterator, (void**)&current);
1663 if (current->get_spi(current, inbound) == spi &&
1664 current->get_protocol(current) == protocol)
1665 {
1666 found = current;
1667 }
1668 }
1669 iterator->destroy(iterator);
1670 return found;
1671 }
1672
1673 /**
1674 * Implementation of ike_sa_t.create_child_sa_iterator.
1675 */
1676 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1677 {
1678 return this->child_sas->create_iterator(this->child_sas, TRUE);
1679 }
1680
1681 /**
1682 * Implementation of ike_sa_t.rekey_child_sa.
1683 */
1684 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1685 {
1686 create_child_sa_t *rekey;
1687 child_sa_t *child_sa;
1688
1689 child_sa = get_child_sa(this, protocol, spi, TRUE);
1690 if (child_sa == NULL)
1691 {
1692 return NOT_FOUND;
1693 }
1694
1695 rekey = create_child_sa_create(&this->public);
1696 rekey->rekeys_child(rekey, child_sa);
1697 return queue_transaction(this, (transaction_t*)rekey, FALSE);
1698 }
1699
1700 /**
1701 * Implementation of ike_sa_t.delete_child_sa.
1702 */
1703 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1704 {
1705 delete_child_sa_t *del;
1706 child_sa_t *child_sa;
1707
1708 child_sa = get_child_sa(this, protocol, spi, TRUE);
1709 if (child_sa == NULL)
1710 {
1711 return NOT_FOUND;
1712 }
1713
1714 del = delete_child_sa_create(&this->public);
1715 del->set_child_sa(del, child_sa);
1716 return queue_transaction(this, (transaction_t*)del, FALSE);
1717 }
1718
1719 /**
1720 * Implementation of ike_sa_t.destroy_child_sa.
1721 */
1722 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1723 {
1724 iterator_t *iterator;
1725 child_sa_t *child_sa;
1726 status_t status = NOT_FOUND;
1727
1728 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1729 while (iterator->iterate(iterator, (void**)&child_sa))
1730 {
1731 if (child_sa->get_protocol(child_sa) == protocol &&
1732 child_sa->get_spi(child_sa, TRUE) == spi)
1733 {
1734 child_sa->destroy(child_sa);
1735 iterator->remove(iterator);
1736 status = SUCCESS;
1737 break;
1738 }
1739 }
1740 iterator->destroy(iterator);
1741 return status;
1742 }
1743
1744 /**
1745 * Implementation of ike_sa_t.set_lifetimes.
1746 */
1747 static void set_lifetimes(private_ike_sa_t *this,
1748 u_int32_t soft_lifetime, u_int32_t hard_lifetime)
1749 {
1750 job_t *job;
1751
1752 if (soft_lifetime)
1753 {
1754 this->time.rekey = this->time.established + soft_lifetime;
1755 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id);
1756 charon->event_queue->add_relative(charon->event_queue, job,
1757 soft_lifetime * 1000);
1758 }
1759
1760 if (hard_lifetime)
1761 {
1762 this->time.delete = this->time.established + hard_lifetime;
1763 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
1764 charon->event_queue->add_relative(charon->event_queue, job,
1765 hard_lifetime * 1000);
1766 }
1767 }
1768
1769 /**
1770 * Implementation of ike_sa_t.rekey.
1771 */
1772 static status_t rekey(private_ike_sa_t *this)
1773 {
1774 rekey_ike_sa_t *rekey_ike_sa;
1775
1776 this->logger->log(this->logger, CONTROL,
1777 "rekeying IKE_SA between %s[%s]..%s[%s]",
1778 this->my_host->get_string(this->my_host),
1779 this->my_id->get_string(this->my_id),
1780 this->other_host->get_string(this->other_host),
1781 this->other_id->get_string(this->other_id));
1782
1783 if (this->state != IKE_ESTABLISHED)
1784 {
1785 this->logger->log(this->logger, ERROR,
1786 "unable to rekey IKE_SA in state %s",
1787 mapping_find(ike_sa_state_m, this->state));
1788 return FAILED;
1789 }
1790
1791 rekey_ike_sa = rekey_ike_sa_create(&this->public);
1792 return queue_transaction(this, (transaction_t*)rekey_ike_sa, FALSE);
1793 }
1794
1795 /**
1796 * Implementation of ike_sa_t.get_rekeying_transaction.
1797 */
1798 static rekey_ike_sa_t* get_rekeying_transaction(private_ike_sa_t *this)
1799 {
1800 return this->rekeying_transaction;
1801 }
1802
1803 /**
1804 * Implementation of ike_sa_t.set_rekeying_transaction.
1805 */
1806 static void set_rekeying_transaction(private_ike_sa_t *this, rekey_ike_sa_t *rekey)
1807 {
1808 this->rekeying_transaction = rekey;
1809 }
1810
1811 /**
1812 * Implementation of ike_sa_t.adopt_children.
1813 */
1814 static void adopt_children(private_ike_sa_t *this, private_ike_sa_t *other)
1815 {
1816 child_sa_t *child_sa;
1817
1818 while (other->child_sas->remove_last(other->child_sas,
1819 (void**)&child_sa) == SUCCESS)
1820 {
1821 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1822 }
1823 }
1824
1825 /**
1826 * Implementation of ike_sa_t.log_status.
1827 */
1828 static void log_status(private_ike_sa_t *this, logger_t *logger, char *name)
1829 {
1830 iterator_t *iterator;
1831 child_sa_t *child_sa;
1832 bool contains_child = FALSE;
1833
1834 /* check for a CHILD_SA with specified name. We then print the IKE_SA,
1835 * even it has another name */
1836 if (name != NULL)
1837 {
1838 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1839 while (iterator->iterate(iterator, (void**)&child_sa))
1840 {
1841 if (streq(name, child_sa->get_name(child_sa)))
1842 {
1843 contains_child = TRUE;
1844 break;
1845 }
1846 }
1847 iterator->destroy(iterator);
1848 }
1849
1850 if (name == NULL || contains_child || streq(name, this->name))
1851 {
1852 if (logger == NULL)
1853 {
1854 logger = this->logger;
1855 }
1856 logger->log(logger, CONTROL|LEVEL1,
1857 " \"%s\": IKE_SA in state %s, SPIs: 0x%.16llx 0x%.16llx",
1858 this->name,
1859 mapping_find(ike_sa_state_m, this->state),
1860 this->ike_sa_id->get_initiator_spi(this->ike_sa_id),
1861 this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1862 logger->log(logger, CONTROL, " \"%s\": %s[%s]...%s[%s]",
1863 this->name,
1864 this->my_host->get_string(this->my_host),
1865 this->my_id->get_string(this->my_id),
1866 this->other_host->get_string(this->other_host),
1867 this->other_id->get_string(this->other_id));
1868
1869 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1870 while (iterator->has_next(iterator))
1871 {
1872 iterator->current(iterator, (void**)&child_sa);
1873 child_sa->log_status(child_sa, logger);
1874 }
1875 iterator->destroy(iterator);
1876 }
1877 }
1878
1879 /**
1880 * Implementation of public_ike_sa_t.delete.
1881 */
1882 static status_t delete_(private_ike_sa_t *this)
1883 {
1884 switch (this->state)
1885 {
1886 case IKE_CONNECTING:
1887 case IKE_ESTABLISHED:
1888 {
1889 delete_ike_sa_t *delete_ike_sa;
1890 if (this->transaction_out)
1891 {
1892 /* already a transaction in progress. As this may hang
1893 * around a while, we don't inform the other peer. */
1894 return DESTROY_ME;
1895 }
1896 delete_ike_sa = delete_ike_sa_create(&this->public);
1897 return queue_transaction(this, (transaction_t*)delete_ike_sa, FALSE);
1898 }
1899 case IKE_CREATED:
1900 case IKE_DELETING:
1901 default:
1902 {
1903 return DESTROY_ME;
1904 }
1905 }
1906 }
1907
1908 /**
1909 * Implementation of ike_sa_t.get_next_message_id.
1910 */
1911 static u_int32_t get_next_message_id (private_ike_sa_t *this)
1912 {
1913 return this->message_id_out++;
1914 }
1915
1916 /**
1917 * Implementation of ike_sa_t.is_natt_enabled.
1918 */
1919 static bool is_natt_enabled (private_ike_sa_t *this)
1920 {
1921 return this->nat_here || this->nat_there;
1922 }
1923
1924 /**
1925 * Implementation of ike_sa_t.enable_natt.
1926 */
1927 static void enable_natt (private_ike_sa_t *this, bool local)
1928 {
1929 if (local)
1930 {
1931 this->logger->log(this->logger, CONTROL,
1932 "local host is behind NAT, using NAT-T, scheduled keep alives");
1933 this->nat_here = TRUE;
1934 send_keepalive(this);
1935 }
1936 else
1937 {
1938 this->logger->log(this->logger, CONTROL,
1939 "remote host is behind NAT, using NAT-T");
1940 this->nat_there = TRUE;
1941 }
1942 }
1943
1944 /**
1945 * Implementation of ike_sa_t.destroy.
1946 */
1947 static void destroy(private_ike_sa_t *this)
1948 {
1949 child_sa_t *child_sa;
1950 transaction_t *transaction;
1951
1952 this->logger->log(this->logger, CONTROL|LEVEL2, "going to destroy IKE SA %llu:%llu, role %s",
1953 this->ike_sa_id->get_initiator_spi(this->ike_sa_id),
1954 this->ike_sa_id->get_responder_spi(this->ike_sa_id),
1955 this->ike_sa_id->is_initiator(this->ike_sa_id) ? "initiator" : "responder");
1956
1957 if (this->state == IKE_ESTABLISHED)
1958 {
1959 this->logger->log(this->logger, ERROR,
1960 "destroying an established IKE SA without knowledge from remote peer!");
1961 }
1962
1963 while (this->child_sas->remove_last(this->child_sas, (void**)&child_sa) == SUCCESS)
1964 {
1965 child_sa->destroy(child_sa);
1966 }
1967 this->child_sas->destroy(this->child_sas);
1968
1969 while (this->transaction_queue->remove_last(this->transaction_queue, (void**)&transaction) == SUCCESS)
1970 {
1971 transaction->destroy(transaction);
1972 }
1973 this->transaction_queue->destroy(this->transaction_queue);
1974
1975 DESTROY_IF(this->transaction_in);
1976 DESTROY_IF(this->transaction_in_next);
1977 DESTROY_IF(this->transaction_out);
1978 DESTROY_IF(this->crypter_in);
1979 DESTROY_IF(this->crypter_out);
1980 DESTROY_IF(this->signer_in);
1981 DESTROY_IF(this->signer_out);
1982 DESTROY_IF(this->prf);
1983 DESTROY_IF(this->child_prf);
1984 DESTROY_IF(this->prf_auth_i);
1985 DESTROY_IF(this->prf_auth_r);
1986
1987 this->logger->log(this->logger, AUDIT,
1988 "IKE_SA deleted between %s[%s]...%s[%s]",
1989 this->my_host->get_string(this->my_host),
1990 this->my_id->get_string(this->my_id),
1991 this->other_host->get_string(this->other_host),
1992 this->other_id->get_string(this->other_id));
1993
1994 DESTROY_IF(this->my_host);
1995 DESTROY_IF(this->other_host);
1996 DESTROY_IF(this->my_id);
1997 DESTROY_IF(this->other_id);
1998
1999 free(this->name);
2000 this->ike_sa_id->destroy(this->ike_sa_id);
2001 free(this);
2002 }
2003
2004 /*
2005 * Described in header.
2006 */
2007 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2008 {
2009 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2010
2011 /* Public functions */
2012 this->public.get_state = (ike_sa_state_t(*)(ike_sa_t*)) get_state;
2013 this->public.set_state = (void(*)(ike_sa_t*,ike_sa_state_t)) set_state;
2014 this->public.get_name = (char*(*)(ike_sa_t*))get_name;
2015 this->public.set_name = (void(*)(ike_sa_t*,char*))set_name;
2016 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
2017 this->public.initiate = (status_t(*)(ike_sa_t*,connection_t*,policy_t*)) initiate;
2018 this->public.route = (status_t(*)(ike_sa_t*,connection_t*,policy_t*)) route;
2019 this->public.unroute = (status_t(*)(ike_sa_t*,policy_t*)) unroute;
2020 this->public.acquire = (status_t(*)(ike_sa_t*,u_int32_t)) acquire;
2021 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
2022 this->public.get_my_host = (host_t*(*)(ike_sa_t*)) get_my_host;
2023 this->public.set_my_host = (void(*)(ike_sa_t*,host_t*)) set_my_host;
2024 this->public.get_other_host = (host_t*(*)(ike_sa_t*)) get_other_host;
2025 this->public.set_other_host = (void(*)(ike_sa_t*,host_t*)) set_other_host;
2026 this->public.get_my_id = (identification_t*(*)(ike_sa_t*)) get_my_id;
2027 this->public.set_my_id = (void(*)(ike_sa_t*,identification_t*)) set_my_id;
2028 this->public.get_other_id = (identification_t*(*)(ike_sa_t*)) get_other_id;
2029 this->public.set_other_id = (void(*)(ike_sa_t*,identification_t*)) set_other_id;
2030 this->public.get_next_message_id = (u_int32_t(*)(ike_sa_t*)) get_next_message_id;
2031 this->public.retransmit_request = (status_t (*) (ike_sa_t *, u_int32_t)) retransmit_request;
2032 this->public.log_status = (void (*) (ike_sa_t*,logger_t*,char*))log_status;
2033 this->public.delete = (status_t(*)(ike_sa_t*))delete_;
2034 this->public.destroy = (void(*)(ike_sa_t*))destroy;
2035 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2036 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2037 this->public.get_prf = (prf_t *(*) (ike_sa_t *)) get_prf;
2038 this->public.get_child_prf = (prf_t *(*) (ike_sa_t *)) get_child_prf;
2039 this->public.get_prf_auth_i = (prf_t *(*) (ike_sa_t *)) get_prf_auth_i;
2040 this->public.get_prf_auth_r = (prf_t *(*) (ike_sa_t *)) get_prf_auth_r;
2041 this->public.derive_keys = (status_t (*) (ike_sa_t *,proposal_t*,diffie_hellman_t*,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2042 this->public.add_child_sa = (void (*) (ike_sa_t*,child_sa_t*)) add_child_sa;
2043 this->public.has_child_sa = (bool(*)(ike_sa_t*,u_int32_t)) has_child_sa;
2044 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2045 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2046 this->public.rekey_child_sa = (status_t(*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2047 this->public.delete_child_sa = (status_t(*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2048 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2049 this->public.enable_natt = (void(*)(ike_sa_t*, bool)) enable_natt;
2050 this->public.is_natt_enabled = (bool(*)(ike_sa_t*)) is_natt_enabled;
2051 this->public.set_lifetimes = (void(*)(ike_sa_t*,u_int32_t,u_int32_t))set_lifetimes;
2052 this->public.rekey = (status_t(*)(ike_sa_t*))rekey;
2053 this->public.get_rekeying_transaction = (void*(*)(ike_sa_t*))get_rekeying_transaction;
2054 this->public.set_rekeying_transaction = (void(*)(ike_sa_t*,void*))set_rekeying_transaction;
2055 this->public.adopt_children = (void(*)(ike_sa_t*,ike_sa_t*))adopt_children;
2056
2057 /* initialize private fields */
2058 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
2059 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2060 this->name = strdup("(uninitialized)");
2061 this->child_sas = linked_list_create();
2062 this->my_host = host_create_from_string("0.0.0.0", 0);
2063 this->other_host = host_create_from_string("0.0.0.0", 0);
2064 this->my_id = identification_create_from_encoding(ID_ANY, CHUNK_INITIALIZER);
2065 this->other_id = identification_create_from_encoding(ID_ANY, CHUNK_INITIALIZER);
2066 this->crypter_in = NULL;
2067 this->crypter_out = NULL;
2068 this->signer_in = NULL;
2069 this->signer_out = NULL;
2070 this->prf = NULL;
2071 this->prf_auth_i = NULL;
2072 this->prf_auth_r = NULL;
2073 this->child_prf = NULL;
2074 this->nat_here = FALSE;
2075 this->nat_there = FALSE;
2076 this->transaction_queue = linked_list_create();
2077 this->transaction_in = NULL;
2078 this->transaction_in_next = NULL;
2079 this->transaction_out = NULL;
2080 this->rekeying_transaction = NULL;
2081 this->state = IKE_CREATED;
2082 this->message_id_out = 0;
2083 /* set to NOW, as when we rekey an existing IKE_SA no message is exchanged */
2084 this->time.inbound = this->time.outbound = time(NULL);
2085 this->time.established = 0;
2086 this->time.rekey = 0;
2087 this->time.delete = 0;
2088 this->dpd_delay = 0;
2089 this->retrans_sequences = 0;
2090
2091 return &this->public;
2092 }