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