added support for transport mode and (experimental!) BEET mode
[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 <library.h>
32 #include <daemon.h>
33 #include <utils/linked_list.h>
34 #include <crypto/diffie_hellman.h>
35 #include <crypto/prf_plus.h>
36 #include <crypto/crypters/crypter.h>
37 #include <crypto/hashers/hasher.h>
38 #include <encoding/payloads/sa_payload.h>
39 #include <encoding/payloads/nonce_payload.h>
40 #include <encoding/payloads/ke_payload.h>
41 #include <encoding/payloads/delete_payload.h>
42 #include <encoding/payloads/transform_substructure.h>
43 #include <encoding/payloads/transform_attribute.h>
44 #include <encoding/payloads/ts_payload.h>
45 #include <sa/transactions/transaction.h>
46 #include <sa/transactions/ike_sa_init.h>
47 #include <sa/transactions/delete_ike_sa.h>
48 #include <sa/transactions/create_child_sa.h>
49 #include <sa/transactions/delete_child_sa.h>
50 #include <sa/transactions/dead_peer_detection.h>
51 #include <sa/transactions/rekey_ike_sa.h>
52 #include <queues/jobs/retransmit_request_job.h>
53 #include <queues/jobs/delete_ike_sa_job.h>
54 #include <queues/jobs/send_dpd_job.h>
55 #include <queues/jobs/send_keepalive_job.h>
56 #include <queues/jobs/rekey_ike_sa_job.h>
57 #include <queues/jobs/route_job.h>
58 #include <queues/jobs/initiate_job.h>
59
60 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DELETING,
61 "CREATED",
62 "CONNECTING",
63 "ESTABLISHED",
64 "REKEYING",
65 "DELETING",
66 );
67
68 typedef struct private_ike_sa_t private_ike_sa_t;
69
70 /**
71 * Private data of an ike_sa_t object.
72 */
73 struct private_ike_sa_t {
74
75 /**
76 * Public members
77 */
78 ike_sa_t public;
79
80 /**
81 * Identifier for the current IKE_SA.
82 */
83 ike_sa_id_t *ike_sa_id;
84
85 /**
86 * Current state of the IKE_SA
87 */
88 ike_sa_state_t state;
89
90 /**
91 * Name of the connection used by this IKE_SA
92 */
93 char *name;
94
95 /**
96 * Address of local host
97 */
98 host_t *my_host;
99
100 /**
101 * Address of remote host
102 */
103 host_t *other_host;
104
105 /**
106 * Identification used for us
107 */
108 identification_t *my_id;
109
110 /**
111 * Identification used for other
112 */
113 identification_t *other_id;
114
115 /**
116 * Linked List containing the child sa's of the current IKE_SA.
117 */
118 linked_list_t *child_sas;
119
120 /**
121 * crypter for inbound traffic
122 */
123 crypter_t *crypter_in;
124
125 /**
126 * crypter for outbound traffic
127 */
128 crypter_t *crypter_out;
129
130 /**
131 * Signer for inbound traffic
132 */
133 signer_t *signer_in;
134
135 /**
136 * Signer for outbound traffic
137 */
138 signer_t *signer_out;
139
140 /**
141 * Multi purpose prf, set key, use it, forget it
142 */
143 prf_t *prf;
144
145 /**
146 * Prf function for derivating keymat child SAs
147 */
148 prf_t *child_prf;
149
150 /**
151 * PRF to build outging authentication data
152 */
153 prf_t *auth_build;
154
155 /**
156 * PRF to verify incoming authentication data
157 */
158 prf_t *auth_verify;
159
160 /**
161 * NAT hasher.
162 */
163 hasher_t *nat_hasher;
164
165 /**
166 * NAT status of local host.
167 */
168 bool nat_here;
169
170 /**
171 * NAT status of remote host.
172 */
173 bool nat_there;
174
175 /**
176 * message ID for next outgoung request
177 */
178 u_int32_t message_id_out;
179
180 /**
181 * Timestamps for this IKE_SA
182 */
183 struct {
184 /** last IKE message received */
185 u_int32_t inbound;
186 /** last IKE message sent */
187 u_int32_t outbound;
188 /** when IKE_SA became established */
189 u_int32_t established;
190 /** when IKE_SA gets rekeyed */
191 u_int32_t rekey;
192 /** when IKE_SA gets deleted */
193 u_int32_t delete;
194 } time;
195
196 /**
197 * interval to send DPD liveness check
198 */
199 time_t dpd_delay;
200
201 /**
202 * number of retransmit sequences to go through before giving up (keyingtries)
203 */
204 u_int32_t retrans_sequences;
205
206 /**
207 * List of queued transactions to process
208 */
209 linked_list_t *transaction_queue;
210
211 /**
212 * Transaction currently initiated
213 * (only one supported yet, window size = 1)
214 */
215 transaction_t *transaction_out;
216
217 /**
218 * last transaction initiated by peer processed.
219 * (only one supported yet, window size = 1)
220 * Stored for retransmission.
221 */
222 transaction_t *transaction_in;
223
224 /**
225 * Next incoming transaction expected. Used to
226 * do multi transaction operations.
227 */
228 transaction_t *transaction_in_next;
229
230 /**
231 * Transaction which rekeys this IKE_SA, used do detect simultaneus rekeying
232 */
233 transaction_t *rekeying_transaction;
234 };
235
236 /**
237 * get the time of the latest traffic processed by the kernel
238 */
239 static time_t get_kernel_time(private_ike_sa_t* this, bool inbound)
240 {
241 iterator_t *iterator;
242 child_sa_t *child_sa;
243 time_t latest = 0, use_time;
244
245 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
246 while (iterator->iterate(iterator, (void**)&child_sa))
247 {
248 if (child_sa->get_use_time(child_sa, inbound, &use_time) == SUCCESS)
249 {
250 latest = max(latest, use_time);
251 }
252 }
253 iterator->destroy(iterator);
254
255 return latest;
256 }
257
258 /**
259 * get the time of the latest received traffice
260 */
261 static time_t get_time_inbound(private_ike_sa_t *this)
262 {
263 return max(this->time.inbound, get_kernel_time(this, TRUE));
264 }
265
266 /**
267 * get the time of the latest sent traffic
268 */
269 static time_t get_time_outbound(private_ike_sa_t *this)
270 {
271 return max(this->time.outbound, get_kernel_time(this, FALSE));
272 }
273
274 /**
275 * Implementation of ike_sa_t.get_name.
276 */
277 static char *get_name(private_ike_sa_t *this)
278 {
279 return this->name;
280 }
281
282 /**
283 * Implementation of ike_sa_t.set_name.
284 */
285 static void set_name(private_ike_sa_t *this, char* name)
286 {
287 free(this->name);
288 this->name = strdup(name);
289 }
290
291 /**
292 * Implementation of ike_sa_t.apply_connection.
293 */
294 static void apply_connection(private_ike_sa_t *this, connection_t *connection)
295 {
296 this->dpd_delay = connection->get_dpd_delay(connection);
297 this->retrans_sequences = connection->get_retrans_seq(connection);
298 }
299
300 /**
301 * Implementation of ike_sa_t.get_my_host.
302 */
303 static host_t *get_my_host(private_ike_sa_t *this)
304 {
305 return this->my_host;
306 }
307
308 /**
309 * Implementation of ike_sa_t.set_my_host.
310 */
311 static void set_my_host(private_ike_sa_t *this, host_t *me)
312 {
313 DESTROY_IF(this->my_host);
314 this->my_host = me;
315 }
316
317 /**
318 * Implementation of ike_sa_t.get_other_host.
319 */
320 static host_t *get_other_host(private_ike_sa_t *this)
321 {
322 return this->other_host;
323 }
324
325 /**
326 * Implementation of ike_sa_t.set_other_host.
327 */
328 static void set_other_host(private_ike_sa_t *this, host_t *other)
329 {
330 DESTROY_IF(this->other_host);
331 this->other_host = other;
332 }
333
334 /**
335 * Update connection host, as addresses may change (NAT)
336 */
337 static void update_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
338 {
339 /*
340 * Quoting RFC 4306:
341 *
342 * 2.11. Address and Port Agility
343 *
344 * IKE runs over UDP ports 500 and 4500, and implicitly sets up ESP and
345 * AH associations for the same IP addresses it runs over. The IP
346 * addresses and ports in the outer header are, however, not themselves
347 * cryptographically protected, and IKE is designed to work even through
348 * Network Address Translation (NAT) boxes. An implementation MUST
349 * accept incoming requests even if the source port is not 500 or 4500,
350 * and MUST respond to the address and port from which the request was
351 * received. It MUST specify the address and port at which the request
352 * was received as the source address and port in the response. IKE
353 * functions identically over IPv4 or IPv6.
354 *
355 * [...]
356 *
357 * There are cases where a NAT box decides to remove mappings that
358 * are still alive (for example, the keepalive interval is too long,
359 * or the NAT box is rebooted). To recover in these cases, hosts
360 * that are not behind a NAT SHOULD send all packets (including
361 * retransmission packets) to the IP address and port from the last
362 * valid authenticated packet from the other end (i.e., dynamically
363 * update the address). A host behind a NAT SHOULD NOT do this
364 * because it opens a DoS attack possibility. Any authenticated IKE
365 * packet or any authenticated UDP-encapsulated ESP packet can be
366 * used to detect that the IP address or the port has changed.
367 */
368 iterator_t *iterator = NULL;
369 child_sa_t *child_sa = NULL;
370 host_diff_t my_diff, other_diff;
371
372 if (this->my_host->is_anyaddr(this->my_host) ||
373 this->other_host->is_anyaddr(this->other_host))
374 {
375 /* on first received message */
376 this->my_host->destroy(this->my_host);
377 this->my_host = me->clone(me);
378 this->other_host->destroy(this->other_host);
379 this->other_host = other->clone(other);
380 return;
381 }
382
383 my_diff = me->get_differences(me, this->my_host);
384 other_diff = other->get_differences(other, this->other_host);
385
386 if (!my_diff && !other_diff)
387 {
388 return;
389 }
390
391 if (my_diff)
392 {
393 this->my_host->destroy(this->my_host);
394 this->my_host = me->clone(me);
395 }
396
397 if (!this->nat_here)
398 {
399 /* update without restrictions if we are not NATted */
400 if (other_diff)
401 {
402 this->other_host->destroy(this->other_host);
403 this->other_host = other->clone(other);
404 }
405 }
406 else
407 {
408 /* if we are natted, only port may change */
409 if (other_diff & HOST_DIFF_ADDR)
410 {
411 return;
412 }
413 else if (other_diff & HOST_DIFF_PORT)
414 {
415 this->other_host->set_port(this->other_host, other->get_port(other));
416 }
417 }
418 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
419 while (iterator->iterate(iterator, (void**)&child_sa))
420 {
421 child_sa->update_hosts(child_sa, this->my_host, this->other_host,
422 my_diff, other_diff);
423 /* TODO: what to do if update fails? Delete CHILD_SA? */
424 }
425 iterator->destroy(iterator);
426 }
427
428 /**
429 * called when the peer is not responding anymore
430 */
431 static void dpd_detected(private_ike_sa_t *this)
432 {
433 connection_t *connection = NULL;
434 policy_t *policy;
435 linked_list_t *my_ts, *other_ts;
436 child_sa_t* child_sa;
437 dpd_action_t action;
438 job_t *job;
439
440 DBG2(DBG_IKE, "dead peer detected, handling CHILD_SAs dpd action");
441
442 /* check for childrens with dpdaction = hold */
443 while(this->child_sas->remove_first(this->child_sas,
444 (void**)&child_sa) == SUCCESS)
445 {
446 /* get the policy which belongs to this CHILD */
447 my_ts = child_sa->get_my_traffic_selectors(child_sa);
448 other_ts = child_sa->get_other_traffic_selectors(child_sa);
449 policy = charon->policies->get_policy(charon->policies,
450 this->my_id, this->other_id,
451 my_ts, other_ts,
452 this->my_host, this->other_host,
453 NULL);
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 NULL);
998 if (policy == NULL)
999 {
1000 SIG(CHILD_UP_START, "acquiring CHILD_SA with reqid %d", reqid);
1001 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
1002 "no policy found", reqid);
1003 return FAILED;
1004 }
1005
1006 switch (this->state)
1007 {
1008 case IKE_CREATED:
1009 {
1010 ike_sa_init_t *ike_sa_init;
1011
1012 connection = charon->connections->get_connection_by_hosts(
1013 charon->connections, this->my_host, this->other_host);
1014
1015 if (connection == NULL)
1016 {
1017 SIG(CHILD_UP_START, "acquiring CHILD_SA with reqid %d", reqid);
1018 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
1019 "no connection found to establsih IKE_SA", reqid);
1020 policy->destroy(policy);
1021 return FAILED;
1022 }
1023
1024 DBG1(DBG_IKE, "establishing IKE_SA to acquire CHILD_SA "
1025 "with reqid %d", reqid);
1026
1027 this->message_id_out = 1;
1028 ike_sa_init = ike_sa_init_create(&this->public);
1029 ike_sa_init->set_config(ike_sa_init, connection, policy);
1030 /* reuse existing reqid */
1031 ike_sa_init->set_reqid(ike_sa_init, reqid);
1032 return queue_transaction(this, (transaction_t*)ike_sa_init, TRUE);
1033 }
1034 case IKE_CONNECTING:
1035 case IKE_ESTABLISHED:
1036 {
1037 create_child_sa_t *create_child;
1038
1039 DBG1(DBG_CHD, "acquiring CHILD_SA with reqid %d", reqid);
1040
1041 create_child = create_child_sa_create(&this->public);
1042 create_child->set_policy(create_child, policy);
1043 /* reuse existing reqid */
1044 create_child->set_reqid(create_child, reqid);
1045 return queue_transaction(this, (transaction_t*)create_child, FALSE);
1046 }
1047 default:
1048 break;
1049 }
1050 return FAILED;
1051 }
1052
1053 /**
1054 * compare two lists of traffic selectors for equality
1055 */
1056 static bool ts_list_equals(linked_list_t *l1, linked_list_t *l2)
1057 {
1058 bool equals = TRUE;
1059 iterator_t *i1, *i2;
1060 traffic_selector_t *t1, *t2;
1061
1062 if (l1->get_count(l1) != l2->get_count(l2))
1063 {
1064 return FALSE;
1065 }
1066
1067 i1 = l1->create_iterator(l1, TRUE);
1068 i2 = l2->create_iterator(l2, TRUE);
1069 while (i1->iterate(i1, (void**)&t1) && i2->iterate(i2, (void**)&t2))
1070 {
1071 if (!t1->equals(t1, t2))
1072 {
1073 equals = FALSE;
1074 break;
1075 }
1076 }
1077 i1->destroy(i1);
1078 i2->destroy(i2);
1079 return equals;
1080 }
1081
1082 /**
1083 * Implementation of ike_sa_t.route.
1084 */
1085 static status_t route(private_ike_sa_t *this, connection_t *connection, policy_t *policy)
1086 {
1087 child_sa_t *child_sa = NULL;
1088 iterator_t *iterator;
1089 linked_list_t *my_ts, *other_ts;
1090 status_t status;
1091
1092 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
1093
1094 /* check if not already routed*/
1095 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1096 while (iterator->iterate(iterator, (void**)&child_sa))
1097 {
1098 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1099 {
1100 linked_list_t *my_ts_conf, *other_ts_conf;
1101
1102 my_ts = child_sa->get_my_traffic_selectors(child_sa);
1103 other_ts = child_sa->get_other_traffic_selectors(child_sa);
1104
1105 my_ts_conf = policy->get_my_traffic_selectors(policy, this->my_host);
1106 other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host);
1107
1108 if (ts_list_equals(my_ts, my_ts_conf) &&
1109 ts_list_equals(other_ts, other_ts_conf))
1110 {
1111 iterator->destroy(iterator);
1112 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
1113 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
1114 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a policy already routed");
1115 return FAILED;
1116 }
1117 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
1118 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
1119 }
1120 }
1121 iterator->destroy(iterator);
1122
1123 switch (this->state)
1124 {
1125 case IKE_CREATED:
1126 case IKE_CONNECTING:
1127 /* we update IKE_SA information as good as possible,
1128 * this allows us to set up the SA later when an acquire comes in. */
1129 if (this->my_id->get_type(this->my_id) == ID_ANY)
1130 {
1131 this->my_id->destroy(this->my_id);
1132 this->my_id = policy->get_my_id(policy);
1133 this->my_id = this->my_id->clone(this->my_id);
1134 }
1135 if (this->other_id->get_type(this->other_id) == ID_ANY)
1136 {
1137 this->other_id->destroy(this->other_id);
1138 this->other_id = policy->get_other_id(policy);
1139 this->other_id = this->other_id->clone(this->other_id);
1140 }
1141 if (this->my_host->is_anyaddr(this->my_host))
1142 {
1143 this->my_host->destroy(this->my_host);
1144 this->my_host = connection->get_my_host(connection);
1145 this->my_host = this->my_host->clone(this->my_host);
1146 }
1147 if (this->other_host->is_anyaddr(this->other_host))
1148 {
1149 this->other_host->destroy(this->other_host);
1150 this->other_host = connection->get_other_host(connection);
1151 this->other_host = this->other_host->clone(this->other_host);
1152 }
1153 set_name(this, connection->get_name(connection));
1154 this->retrans_sequences = connection->get_retrans_seq(connection);
1155 this->dpd_delay = connection->get_dpd_delay(connection);
1156 break;
1157 case IKE_ESTABLISHED:
1158 case IKE_REKEYING:
1159 /* nothing to do. We allow it for rekeying, as it will be
1160 * adopted by the new IKE_SA */
1161 break;
1162 case IKE_DELETING:
1163 /* TODO: hanlde this case, create a new IKE_SA and route CHILD_SA */
1164 SIG(CHILD_ROUTE_FAILED, "unable to route CHILD_SA, as its IKE_SA gets deleted");
1165 return FAILED;
1166 }
1167
1168 child_sa = child_sa_create(0, this->my_host, this->other_host,
1169 this->my_id, this->other_id,
1170 0, 0,
1171 NULL, policy->get_hostaccess(policy),
1172 FALSE);
1173 child_sa->set_name(child_sa, policy->get_name(policy));
1174 my_ts = policy->get_my_traffic_selectors(policy, this->my_host);
1175 other_ts = policy->get_other_traffic_selectors(policy, this->other_host);
1176 status = child_sa->add_policies(child_sa, my_ts, other_ts,
1177 policy->get_mode(policy));
1178 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1179 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1180 this->child_sas->insert_last(this->child_sas, child_sa);
1181 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
1182 return status;
1183 }
1184
1185 /**
1186 * Implementation of ike_sa_t.unroute.
1187 */
1188 static status_t unroute(private_ike_sa_t *this, policy_t *policy)
1189 {
1190 iterator_t *iterator;
1191 child_sa_t *child_sa = NULL;
1192 bool found = FALSE;
1193 linked_list_t *my_ts, *other_ts, *my_ts_conf, *other_ts_conf;
1194
1195 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
1196
1197 /* find CHILD_SA in ROUTED state */
1198 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1199 while (iterator->iterate(iterator, (void**)&child_sa))
1200 {
1201 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1202 {
1203 my_ts = child_sa->get_my_traffic_selectors(child_sa);
1204 other_ts = child_sa->get_other_traffic_selectors(child_sa);
1205
1206 my_ts_conf = policy->get_my_traffic_selectors(policy, this->my_host);
1207 other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host);
1208
1209 if (ts_list_equals(my_ts, my_ts_conf) &&
1210 ts_list_equals(other_ts, other_ts_conf))
1211 {
1212 iterator->remove(iterator);
1213 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
1214 child_sa->destroy(child_sa);
1215 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
1216 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
1217 found = TRUE;
1218 break;
1219 }
1220 my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy));
1221 other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy));
1222 }
1223 }
1224 iterator->destroy(iterator);
1225
1226 if (!found)
1227 {
1228 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
1229 return FAILED;
1230 }
1231 /* if we are not established, and we have no more routed childs, remove whole SA */
1232 if (this->state == IKE_CREATED &&
1233 this->child_sas->get_count(this->child_sas) == 0)
1234 {
1235 return DESTROY_ME;
1236 }
1237 return SUCCESS;
1238 }
1239
1240 /**
1241 * Implementation of ike_sa_t.send_dpd
1242 */
1243 static status_t send_dpd(private_ike_sa_t *this)
1244 {
1245 send_dpd_job_t *job;
1246 time_t diff;
1247
1248 if (this->dpd_delay == 0)
1249 {
1250 /* DPD disabled */
1251 return SUCCESS;
1252 }
1253
1254 if (this->transaction_out)
1255 {
1256 /* there is a transaction in progress. Come back later */
1257 diff = 0;
1258 }
1259 else
1260 {
1261 /* check if there was any inbound traffic */
1262 time_t last_in, now;
1263 last_in = get_time_inbound(this);
1264 now = time(NULL);
1265 diff = now - last_in;
1266 if (diff >= this->dpd_delay)
1267 {
1268 /* to long ago, initiate dead peer detection */
1269 dead_peer_detection_t *dpd;
1270 DBG1(DBG_IKE, "sending DPD request");
1271 dpd = dead_peer_detection_create(&this->public);
1272 queue_transaction(this, (transaction_t*)dpd, FALSE);
1273 diff = 0;
1274 }
1275 }
1276 /* recheck in "interval" seconds */
1277 job = send_dpd_job_create(this->ike_sa_id);
1278 charon->event_queue->add_relative(charon->event_queue, (job_t*)job,
1279 (this->dpd_delay - diff) * 1000);
1280 return SUCCESS;
1281 }
1282
1283 /**
1284 * Implementation of ike_sa_t.send_keepalive
1285 */
1286 static void send_keepalive(private_ike_sa_t *this)
1287 {
1288 send_keepalive_job_t *job;
1289 time_t last_out, now, diff, interval;
1290
1291 last_out = get_time_outbound(this);
1292 now = time(NULL);
1293
1294 diff = now - last_out;
1295 interval = charon->configuration->get_keepalive_interval(charon->configuration);
1296
1297 if (diff >= interval)
1298 {
1299 packet_t *packet;
1300 chunk_t data;
1301
1302 packet = packet_create();
1303 packet->set_source(packet, this->my_host->clone(this->my_host));
1304 packet->set_destination(packet, this->other_host->clone(this->other_host));
1305 data.ptr = malloc(1);
1306 data.ptr[0] = 0xFF;
1307 data.len = 1;
1308 packet->set_data(packet, data);
1309 charon->send_queue->add(charon->send_queue, packet);
1310 DBG1(DBG_IKE, "sending keep alive");
1311 diff = 0;
1312 }
1313 job = send_keepalive_job_create(this->ike_sa_id);
1314 charon->event_queue->add_relative(charon->event_queue, (job_t*)job,
1315 (interval - diff) * 1000);
1316 }
1317
1318 /**
1319 * Implementation of ike_sa_t.get_state.
1320 */
1321 static ike_sa_state_t get_state(private_ike_sa_t *this)
1322 {
1323 return this->state;
1324 }
1325
1326 /**
1327 * Implementation of ike_sa_t.set_state.
1328 */
1329 static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
1330 {
1331 DBG1(DBG_IKE, "IKE_SA state change: %N => %N",
1332 ike_sa_state_names, this->state,
1333 ike_sa_state_names, state);
1334
1335 if (state == IKE_ESTABLISHED)
1336 {
1337 this->time.established = time(NULL);
1338 /* start DPD checks */
1339 send_dpd(this);
1340 }
1341
1342 this->state = state;
1343 }
1344
1345 /**
1346 * Implementation of ike_sa_t.get_prf.
1347 */
1348 static prf_t *get_prf(private_ike_sa_t *this)
1349 {
1350 return this->prf;
1351 }
1352
1353 /**
1354 * Implementation of ike_sa_t.get_prf.
1355 */
1356 static prf_t *get_child_prf(private_ike_sa_t *this)
1357 {
1358 return this->child_prf;
1359 }
1360
1361 /**
1362 * Implementation of ike_sa_t.get_auth_bild
1363 */
1364 static prf_t *get_auth_build(private_ike_sa_t *this)
1365 {
1366 return this->auth_build;
1367 }
1368
1369 /**
1370 * Implementation of ike_sa_t.get_auth_verify
1371 */
1372 static prf_t *get_auth_verify(private_ike_sa_t *this)
1373 {
1374 return this->auth_verify;
1375 }
1376
1377 /**
1378 * Implementation of ike_sa_t.get_id.
1379 */
1380 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1381 {
1382 return this->ike_sa_id;
1383 }
1384
1385 /**
1386 * Implementation of ike_sa_t.get_my_id.
1387 */
1388 static identification_t* get_my_id(private_ike_sa_t *this)
1389 {
1390 return this->my_id;
1391 }
1392
1393 /**
1394 * Implementation of ike_sa_t.set_my_id.
1395 */
1396 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1397 {
1398 DESTROY_IF(this->my_id);
1399 this->my_id = me;
1400 }
1401
1402 /**
1403 * Implementation of ike_sa_t.get_other_id.
1404 */
1405 static identification_t* get_other_id(private_ike_sa_t *this)
1406 {
1407 return this->other_id;
1408 }
1409
1410 /**
1411 * Implementation of ike_sa_t.set_other_id.
1412 */
1413 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1414 {
1415 DESTROY_IF(this->other_id);
1416 this->other_id = other;
1417 }
1418
1419 /**
1420 * Implementation of ike_sa_t.derive_keys.
1421 */
1422 static status_t derive_keys(private_ike_sa_t *this,
1423 proposal_t *proposal, diffie_hellman_t *dh,
1424 chunk_t nonce_i, chunk_t nonce_r,
1425 bool initiator, prf_t *child_prf, prf_t *old_prf)
1426 {
1427 prf_plus_t *prf_plus;
1428 chunk_t skeyseed, secret, key, nonces, prf_plus_seed;
1429 algorithm_t *algo;
1430 size_t key_size;
1431 crypter_t *crypter_i, *crypter_r;
1432 signer_t *signer_i, *signer_r;
1433 prf_t *prf_i, *prf_r;
1434 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1435 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1436 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1437
1438 /* Create SAs general purpose PRF first, we may use it here */
1439 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1440 {
1441 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1442 return FAILED;
1443 }
1444 this->prf = prf_create(algo->algorithm);
1445 if (this->prf == NULL)
1446 {
1447 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1448 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1449 return FAILED;
1450 }
1451
1452 dh->get_shared_secret(dh, &secret);
1453 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1454 nonces = chunk_cat("cc", nonce_i, nonce_r);
1455 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1456 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1457 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1458
1459 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1460 *
1461 * if we are rekeying, SKEYSEED is built on another way
1462 */
1463 if (child_prf == NULL) /* not rekeying */
1464 {
1465 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1466 this->prf->set_key(this->prf, nonces);
1467 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1468 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1469 this->prf->set_key(this->prf, skeyseed);
1470 chunk_free(&skeyseed);
1471 chunk_free(&secret);
1472 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1473 }
1474 else
1475 {
1476 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1477 * use OLD SAs PRF functions for both prf_plus and prf */
1478 secret = chunk_cat("mc", secret, nonces);
1479 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1480 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1481 old_prf->set_key(old_prf, skeyseed);
1482 chunk_free(&skeyseed);
1483 chunk_free(&secret);
1484 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1485 }
1486 chunk_free(&nonces);
1487 chunk_free(&prf_plus_seed);
1488
1489 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1490
1491 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1492 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1493 this->child_prf = prf_create(algo->algorithm);
1494 key_size = this->child_prf->get_key_size(this->child_prf);
1495 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1496 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1497 this->child_prf->set_key(this->child_prf, key);
1498 chunk_free(&key);
1499
1500 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1501 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1502 {
1503 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1504 return FAILED;
1505 }
1506 signer_i = signer_create(algo->algorithm);
1507 signer_r = signer_create(algo->algorithm);
1508 if (signer_i == NULL || signer_r == NULL)
1509 {
1510 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1511 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1512 return FAILED;
1513 }
1514 key_size = signer_i->get_key_size(signer_i);
1515
1516 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1517 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1518 signer_i->set_key(signer_i, key);
1519 chunk_free(&key);
1520
1521 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1522 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1523 signer_r->set_key(signer_r, key);
1524 chunk_free(&key);
1525
1526 if (initiator)
1527 {
1528 this->signer_in = signer_r;
1529 this->signer_out = signer_i;
1530 }
1531 else
1532 {
1533 this->signer_in = signer_i;
1534 this->signer_out = signer_r;
1535 }
1536
1537 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1538 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1539 {
1540 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1541 return FAILED;
1542 }
1543 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1544 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1545 if (crypter_i == NULL || crypter_r == NULL)
1546 {
1547 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1548 "%N (key size %d) not supported!",
1549 encryption_algorithm_names, algo->algorithm, algo->key_size);
1550 return FAILED;
1551 }
1552 key_size = crypter_i->get_key_size(crypter_i);
1553
1554 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1555 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1556 crypter_i->set_key(crypter_i, key);
1557 chunk_free(&key);
1558
1559 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1560 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1561 crypter_r->set_key(crypter_r, key);
1562 chunk_free(&key);
1563
1564 if (initiator)
1565 {
1566 this->crypter_in = crypter_r;
1567 this->crypter_out = crypter_i;
1568 }
1569 else
1570 {
1571 this->crypter_in = crypter_i;
1572 this->crypter_out = crypter_r;
1573 }
1574
1575 /* SK_pi/SK_pr used for authentication => prf_auth_i, prf_auth_r */
1576 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1577 prf_i = prf_create(algo->algorithm);
1578 prf_r = prf_create(algo->algorithm);
1579
1580 key_size = prf_i->get_key_size(prf_i);
1581 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1582 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1583 prf_i->set_key(prf_i, key);
1584 chunk_free(&key);
1585
1586 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1587 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1588 prf_r->set_key(prf_r, key);
1589 chunk_free(&key);
1590
1591 if (initiator)
1592 {
1593 this->auth_verify = prf_r;
1594 this->auth_build = prf_i;
1595 }
1596 else
1597 {
1598 this->auth_verify = prf_i;
1599 this->auth_build = prf_r;
1600 }
1601
1602 /* all done, prf_plus not needed anymore */
1603 prf_plus->destroy(prf_plus);
1604
1605 return SUCCESS;
1606 }
1607
1608 /**
1609 * Implementation of ike_sa_t.add_child_sa.
1610 */
1611 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1612 {
1613 this->child_sas->insert_last(this->child_sas, child_sa);
1614 }
1615
1616 /**
1617 * Implementation of ike_sa_t.has_child_sa.
1618 */
1619 static bool has_child_sa(private_ike_sa_t *this, u_int32_t reqid)
1620 {
1621 iterator_t *iterator;
1622 child_sa_t *current;
1623 bool found = FALSE;
1624
1625 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1626 while (iterator->iterate(iterator, (void**)&current))
1627 {
1628 if (current->get_reqid(current) == reqid)
1629 {
1630 found = TRUE;
1631 break;
1632 }
1633 }
1634 iterator->destroy(iterator);
1635 return found;
1636 }
1637
1638 /**
1639 * Implementation of ike_sa_t.get_child_sa.
1640 */
1641 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1642 u_int32_t spi, bool inbound)
1643 {
1644 iterator_t *iterator;
1645 child_sa_t *current, *found = NULL;
1646
1647 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1648 while (iterator->iterate(iterator, (void**)&current))
1649 {;
1650 if (current->get_spi(current, inbound) == spi &&
1651 current->get_protocol(current) == protocol)
1652 {
1653 found = current;
1654 }
1655 }
1656 iterator->destroy(iterator);
1657 return found;
1658 }
1659
1660 /**
1661 * Implementation of ike_sa_t.create_child_sa_iterator.
1662 */
1663 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1664 {
1665 return this->child_sas->create_iterator(this->child_sas, TRUE);
1666 }
1667
1668 /**
1669 * Implementation of ike_sa_t.rekey_child_sa.
1670 */
1671 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1672 {
1673 create_child_sa_t *rekey;
1674 child_sa_t *child_sa;
1675
1676 child_sa = get_child_sa(this, protocol, spi, TRUE);
1677 if (child_sa == NULL)
1678 {
1679 return NOT_FOUND;
1680 }
1681
1682 rekey = create_child_sa_create(&this->public);
1683 rekey->rekeys_child(rekey, child_sa);
1684 return queue_transaction(this, (transaction_t*)rekey, FALSE);
1685 }
1686
1687 /**
1688 * Implementation of ike_sa_t.delete_child_sa.
1689 */
1690 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1691 {
1692 delete_child_sa_t *del;
1693 child_sa_t *child_sa;
1694
1695 child_sa = get_child_sa(this, protocol, spi, TRUE);
1696 if (child_sa == NULL)
1697 {
1698 return NOT_FOUND;
1699 }
1700
1701 del = delete_child_sa_create(&this->public);
1702 del->set_child_sa(del, child_sa);
1703 return queue_transaction(this, (transaction_t*)del, FALSE);
1704 }
1705
1706 /**
1707 * Implementation of ike_sa_t.destroy_child_sa.
1708 */
1709 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1710 {
1711 iterator_t *iterator;
1712 child_sa_t *child_sa;
1713 status_t status = NOT_FOUND;
1714
1715 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1716 while (iterator->iterate(iterator, (void**)&child_sa))
1717 {
1718 if (child_sa->get_protocol(child_sa) == protocol &&
1719 child_sa->get_spi(child_sa, TRUE) == spi)
1720 {
1721 child_sa->destroy(child_sa);
1722 iterator->remove(iterator);
1723 status = SUCCESS;
1724 break;
1725 }
1726 }
1727 iterator->destroy(iterator);
1728 return status;
1729 }
1730
1731 /**
1732 * Implementation of ike_sa_t.set_lifetimes.
1733 */
1734 static void set_lifetimes(private_ike_sa_t *this, bool reauth,
1735 u_int32_t soft_lifetime, u_int32_t hard_lifetime)
1736 {
1737 job_t *job;
1738
1739 if (soft_lifetime)
1740 {
1741 this->time.rekey = this->time.established + soft_lifetime;
1742 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, reauth);
1743 charon->event_queue->add_relative(charon->event_queue, job,
1744 soft_lifetime * 1000);
1745 }
1746
1747 if (hard_lifetime)
1748 {
1749 this->time.delete = this->time.established + hard_lifetime;
1750 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
1751 charon->event_queue->add_relative(charon->event_queue, job,
1752 hard_lifetime * 1000);
1753 }
1754 }
1755
1756 /**
1757 * Implementation of public_ike_sa_t.delete.
1758 */
1759 static status_t delete_(private_ike_sa_t *this)
1760 {
1761 switch (this->state)
1762 {
1763 case IKE_CONNECTING:
1764 {
1765 /* this may happen if a half open IKE_SA gets closed after a
1766 * timeout. We signal here UP_FAILED to complete the SIG schema */
1767 SIG(IKE_UP_FAILED, "half open IKE_SA deleted after timeout");
1768 return DESTROY_ME;
1769 }
1770 case IKE_ESTABLISHED:
1771 {
1772 delete_ike_sa_t *delete_ike_sa;
1773 if (this->transaction_out)
1774 {
1775 /* already a transaction in progress. As this may hang
1776 * around a while, we don't inform the other peer. */
1777 return DESTROY_ME;
1778 }
1779 delete_ike_sa = delete_ike_sa_create(&this->public);
1780 return queue_transaction(this, (transaction_t*)delete_ike_sa, FALSE);
1781 }
1782 case IKE_CREATED:
1783 case IKE_DELETING:
1784 default:
1785 {
1786 SIG(IKE_DOWN_START, "closing IKE_SA");
1787 SIG(IKE_DOWN_SUCCESS, "IKE_SA closed between %H[%D]...%H[%D]",
1788 this->my_host, this->my_id, this->other_host, this->other_id);
1789 return DESTROY_ME;
1790 }
1791 }
1792 }
1793
1794 /**
1795 * Implementation of ike_sa_t.rekey.
1796 */
1797 static status_t rekey(private_ike_sa_t *this)
1798 {
1799 rekey_ike_sa_t *rekey_ike_sa;
1800
1801 DBG1(DBG_IKE, "rekeying IKE_SA between %H[%D]..%H[%D]",
1802 this->my_host, this->my_id, this->other_host, this->other_id);
1803
1804 if (this->state != IKE_ESTABLISHED)
1805 {
1806 SIG(IKE_REKEY_START, "rekeying IKE_SA");
1807 SIG(IKE_REKEY_FAILED, "unable to rekey IKE_SA in state %N",
1808 ike_sa_state_names, this->state);
1809 return FAILED;
1810 }
1811
1812 rekey_ike_sa = rekey_ike_sa_create(&this->public);
1813 return queue_transaction(this, (transaction_t*)rekey_ike_sa, FALSE);
1814 }
1815
1816 /**
1817 * Implementation of ike_sa_t.reauth.
1818 */
1819 static status_t reauth(private_ike_sa_t *this)
1820 {
1821 connection_t *connection;
1822 child_sa_t *child_sa;
1823 iterator_t *iterator;
1824
1825 DBG1(DBG_IKE, "reauthenticating IKE_SA between %H[%D]..%H[%D]",
1826 this->my_host, this->my_id, this->other_host, this->other_id);
1827
1828 /* get a connection to initiate */
1829 connection = charon->connections->get_connection_by_hosts(charon->connections,
1830 this->my_host, this->other_host);
1831 if (connection == NULL)
1832 {
1833 DBG1(DBG_IKE, "no connection found to reauthenticate");
1834 return FAILED;
1835 }
1836
1837 /* queue CREATE_CHILD_SA transactions to set up all CHILD_SAs */
1838 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1839 while (iterator->iterate(iterator, (void**)&child_sa))
1840 {
1841 job_t *job;
1842 policy_t *policy;
1843 linked_list_t *my_ts, *other_ts;
1844 my_ts = child_sa->get_my_traffic_selectors(child_sa);
1845 other_ts = child_sa->get_other_traffic_selectors(child_sa);
1846 policy = charon->policies->get_policy(charon->policies,
1847 this->my_id, this->other_id, my_ts, other_ts,
1848 this->my_host, this->other_host, NULL);
1849 if (policy == NULL)
1850 {
1851 DBG1(DBG_IKE, "policy not found to recreate CHILD_SA, skipped");
1852 continue;
1853 }
1854
1855 connection->get_ref(connection);
1856 job = (job_t*)initiate_job_create(connection, policy);
1857 charon->job_queue->add(charon->job_queue, job);
1858 }
1859 iterator->destroy(iterator);
1860 connection->destroy(connection);
1861
1862 /* delete the old IKE_SA
1863 * TODO: we should delay the delete to avoid connectivity gaps?! */
1864 return delete_(this);
1865 }
1866
1867 /**
1868 * Implementation of ike_sa_t.get_rekeying_transaction.
1869 */
1870 static transaction_t* get_rekeying_transaction(private_ike_sa_t *this)
1871 {
1872 return this->rekeying_transaction;
1873 }
1874
1875 /**
1876 * Implementation of ike_sa_t.set_rekeying_transaction.
1877 */
1878 static void set_rekeying_transaction(private_ike_sa_t *this, transaction_t *rekey)
1879 {
1880 this->rekeying_transaction = rekey;
1881 }
1882
1883 /**
1884 * Implementation of ike_sa_t.adopt_children.
1885 */
1886 static void adopt_children(private_ike_sa_t *this, private_ike_sa_t *other)
1887 {
1888 child_sa_t *child_sa;
1889
1890 while (other->child_sas->remove_last(other->child_sas,
1891 (void**)&child_sa) == SUCCESS)
1892 {
1893 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1894 }
1895 }
1896
1897 /**
1898 * Implementation of ike_sa_t.get_next_message_id.
1899 */
1900 static u_int32_t get_next_message_id (private_ike_sa_t *this)
1901 {
1902 return this->message_id_out++;
1903 }
1904
1905 /**
1906 * Implementation of ike_sa_t.is_natt_enabled.
1907 */
1908 static bool is_natt_enabled(private_ike_sa_t *this)
1909 {
1910 return this->nat_here || this->nat_there;
1911 }
1912
1913 /**
1914 * Implementation of ike_sa_t.enable_natt.
1915 */
1916 static void enable_natt(private_ike_sa_t *this, bool local)
1917 {
1918 if (local)
1919 {
1920 DBG1(DBG_IKE, "local host is behind NAT, using NAT-T, "
1921 "scheduled keep alives");
1922 this->nat_here = TRUE;
1923 send_keepalive(this);
1924 }
1925 else
1926 {
1927 DBG1(DBG_IKE, "remote host is behind NAT, using NAT-T");
1928 this->nat_there = TRUE;
1929 }
1930 }
1931
1932 /**
1933 * output handler in printf()
1934 */
1935 static int print(FILE *stream, const struct printf_info *info,
1936 const void *const *args)
1937 {
1938 int written = 0;
1939 private_ike_sa_t *this = *((private_ike_sa_t**)(args[0]));
1940
1941 if (this == NULL)
1942 {
1943 return fprintf(stream, "(null)");
1944 }
1945
1946 written = fprintf(stream, "%12s: %N, %H[%D]...%H[%D]",
1947 this->name, ike_sa_state_names, this->state,
1948 this->my_host, this->my_id, this->other_host, this->other_id);
1949 written += fprintf(stream, "\n%12s: IKE SPIs: %J", this->name, this->ike_sa_id);
1950
1951 if (info->alt)
1952 {
1953
1954 }
1955 return written;
1956 }
1957
1958 /**
1959 * register printf() handlers
1960 */
1961 static void __attribute__ ((constructor))print_register()
1962 {
1963 register_printf_function(PRINTF_IKE_SA, print, arginfo_ptr);
1964 }
1965
1966 /**
1967 * Implementation of ike_sa_t.destroy.
1968 */
1969 static void destroy(private_ike_sa_t *this)
1970 {
1971 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1972 this->transaction_queue->destroy_offset(this->transaction_queue, offsetof(transaction_t, destroy));
1973
1974 DESTROY_IF(this->transaction_in);
1975 DESTROY_IF(this->transaction_in_next);
1976 DESTROY_IF(this->transaction_out);
1977 DESTROY_IF(this->crypter_in);
1978 DESTROY_IF(this->crypter_out);
1979 DESTROY_IF(this->signer_in);
1980 DESTROY_IF(this->signer_out);
1981 DESTROY_IF(this->prf);
1982 DESTROY_IF(this->child_prf);
1983 DESTROY_IF(this->auth_verify);
1984 DESTROY_IF(this->auth_build);
1985
1986 DESTROY_IF(this->my_host);
1987 DESTROY_IF(this->other_host);
1988 DESTROY_IF(this->my_id);
1989 DESTROY_IF(this->other_id);
1990
1991 free(this->name);
1992 this->ike_sa_id->destroy(this->ike_sa_id);
1993 free(this);
1994 }
1995
1996 /*
1997 * Described in header.
1998 */
1999 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
2000 {
2001 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2002
2003 /* Public functions */
2004 this->public.get_state = (ike_sa_state_t(*)(ike_sa_t*)) get_state;
2005 this->public.set_state = (void(*)(ike_sa_t*,ike_sa_state_t)) set_state;
2006 this->public.get_name = (char*(*)(ike_sa_t*))get_name;
2007 this->public.set_name = (void(*)(ike_sa_t*,char*))set_name;
2008 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
2009 this->public.initiate = (status_t(*)(ike_sa_t*,connection_t*,policy_t*)) initiate;
2010 this->public.route = (status_t(*)(ike_sa_t*,connection_t*,policy_t*)) route;
2011 this->public.unroute = (status_t(*)(ike_sa_t*,policy_t*)) unroute;
2012 this->public.acquire = (status_t(*)(ike_sa_t*,u_int32_t)) acquire;
2013 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
2014 this->public.get_my_host = (host_t*(*)(ike_sa_t*)) get_my_host;
2015 this->public.set_my_host = (void(*)(ike_sa_t*,host_t*)) set_my_host;
2016 this->public.get_other_host = (host_t*(*)(ike_sa_t*)) get_other_host;
2017 this->public.set_other_host = (void(*)(ike_sa_t*,host_t*)) set_other_host;
2018 this->public.get_my_id = (identification_t*(*)(ike_sa_t*)) get_my_id;
2019 this->public.set_my_id = (void(*)(ike_sa_t*,identification_t*)) set_my_id;
2020 this->public.get_other_id = (identification_t*(*)(ike_sa_t*)) get_other_id;
2021 this->public.set_other_id = (void(*)(ike_sa_t*,identification_t*)) set_other_id;
2022 this->public.get_next_message_id = (u_int32_t(*)(ike_sa_t*)) get_next_message_id;
2023 this->public.retransmit_request = (status_t (*) (ike_sa_t *, u_int32_t)) retransmit_request;
2024 this->public.delete = (status_t(*)(ike_sa_t*))delete_;
2025 this->public.destroy = (void(*)(ike_sa_t*))destroy;
2026 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2027 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2028 this->public.get_prf = (prf_t *(*) (ike_sa_t *)) get_prf;
2029 this->public.get_child_prf = (prf_t *(*) (ike_sa_t *)) get_child_prf;
2030 this->public.get_auth_verify = (prf_t *(*) (ike_sa_t *)) get_auth_verify;
2031 this->public.get_auth_build = (prf_t *(*) (ike_sa_t *)) get_auth_build;
2032 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;
2033 this->public.add_child_sa = (void (*) (ike_sa_t*,child_sa_t*)) add_child_sa;
2034 this->public.has_child_sa = (bool(*)(ike_sa_t*,u_int32_t)) has_child_sa;
2035 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2036 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2037 this->public.rekey_child_sa = (status_t(*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2038 this->public.delete_child_sa = (status_t(*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2039 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2040 this->public.enable_natt = (void(*)(ike_sa_t*, bool)) enable_natt;
2041 this->public.is_natt_enabled = (bool(*)(ike_sa_t*)) is_natt_enabled;
2042 this->public.set_lifetimes = (void(*)(ike_sa_t*,bool,u_int32_t,u_int32_t))set_lifetimes;
2043 this->public.apply_connection = (void(*)(ike_sa_t*,connection_t*))apply_connection;
2044 this->public.rekey = (status_t(*)(ike_sa_t*))rekey;
2045 this->public.reauth = (status_t(*)(ike_sa_t*))reauth;
2046 this->public.get_rekeying_transaction = (transaction_t*(*)(ike_sa_t*))get_rekeying_transaction;
2047 this->public.set_rekeying_transaction = (void(*)(ike_sa_t*,transaction_t*))set_rekeying_transaction;
2048 this->public.adopt_children = (void(*)(ike_sa_t*,ike_sa_t*))adopt_children;
2049
2050 /* initialize private fields */
2051 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2052 this->name = strdup("(uninitialized)");
2053 this->child_sas = linked_list_create();
2054 this->my_host = host_create_from_string("0.0.0.0", 0);
2055 this->other_host = host_create_from_string("0.0.0.0", 0);
2056 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2057 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2058 this->crypter_in = NULL;
2059 this->crypter_out = NULL;
2060 this->signer_in = NULL;
2061 this->signer_out = NULL;
2062 this->prf = NULL;
2063 this->auth_verify = NULL;
2064 this->auth_build = NULL;
2065 this->child_prf = NULL;
2066 this->nat_here = FALSE;
2067 this->nat_there = FALSE;
2068 this->transaction_queue = linked_list_create();
2069 this->transaction_in = NULL;
2070 this->transaction_in_next = NULL;
2071 this->transaction_out = NULL;
2072 this->rekeying_transaction = NULL;
2073 this->state = IKE_CREATED;
2074 this->message_id_out = 0;
2075 /* set to NOW, as when we rekey an existing IKE_SA no message is exchanged
2076 * and inbound therefore uninitialized */
2077 this->time.inbound = this->time.outbound = time(NULL);
2078 this->time.established = 0;
2079 this->time.rekey = 0;
2080 this->time.delete = 0;
2081 this->dpd_delay = 0;
2082 this->retrans_sequences = 0;
2083
2084 return &this->public;
2085 }