applied new changes from NATT team
[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 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <sys/time.h>
25 #include <string.h>
26
27 #include "ike_sa.h"
28
29 #include <types.h>
30 #include <daemon.h>
31 #include <definitions.h>
32 #include <utils/linked_list.h>
33 #include <utils/logger_manager.h>
34 #include <utils/randomizer.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/states/initiator_init.h>
47 #include <sa/states/responder_init.h>
48 #include <sa/states/create_child_sa_requested.h>
49 #include <sa/states/delete_child_sa_requested.h>
50 #include <sa/states/delete_ike_sa_requested.h>
51 #include <queues/jobs/retransmit_request_job.h>
52 #include <queues/jobs/delete_established_ike_sa_job.h>
53 #include <queues/jobs/delete_half_open_ike_sa_job.h>
54
55
56
57
58 typedef struct private_ike_sa_t private_ike_sa_t;
59
60 /**
61 * Private data of an ike_sa_t object.
62 */
63 struct private_ike_sa_t {
64
65 /**
66 * Protected part of a ike_sa_t object.
67 */
68 protected_ike_sa_t protected;
69
70 /**
71 * Update a timestamp on ike traffic
72 */
73 void (*update_timestamp)(private_ike_sa_t *this, bool in);
74
75 /**
76 * Returns the time since last traffic on kernel policies
77 */
78 struct timeval (*get_last_esp_traffic_tv)(private_ike_sa_t * this, bool inbound);
79
80 /**
81 * Identifier for the current IKE_SA.
82 */
83 ike_sa_id_t *ike_sa_id;
84
85 /**
86 * Linked List containing the child sa's of the current IKE_SA.
87 */
88 linked_list_t *child_sas;
89
90 /**
91 * Current state of the IKE_SA represented as state_t object.
92 *
93 * A state object representates one of the following states and is processing
94 * messages in the specific state:
95 * - INITIATOR_INIT
96 * - RESPONDER_INIT
97 * - IKE_SA_INIT_REQUESTED
98 * - IKE_SA_INIT_RESPONDED
99 * - IKE_AUTH_REQUESTED
100 * -IKE_SA_ESTABLISHED
101 */
102 state_t *current_state;
103
104 /**
105 * Connection definition used for this IKE_SA
106 */
107 connection_t *connection;
108
109 /**
110 * Policy definition used for this IKE_SA
111 */
112 policy_t *policy;
113
114 /**
115 * This SA's source for random data.
116 *
117 * Is available in every state.
118 */
119 randomizer_t *randomizer;
120
121 /**
122 * The last responded message.
123 */
124 message_t *last_responded_message;
125
126 /**
127 * The ast requested message.
128 */
129 message_t *last_requested_message;
130
131 /**
132 * Crypter object for initiator.
133 */
134 crypter_t *crypter_initiator;
135
136 /**
137 * Crypter object for responder.
138 */
139 crypter_t *crypter_responder;
140
141 /**
142 * Signer object for initiator.
143 */
144 signer_t *signer_initiator;
145
146 /**
147 * Signer object for responder.
148 */
149 signer_t *signer_responder;
150
151 /**
152 * Multi purpose prf, set key, use it, forget it
153 */
154 prf_t *prf;
155
156 /**
157 * Prf function for derivating keymat child SAs
158 */
159 prf_t *child_prf;
160
161 /**
162 * PRF, with key set to pi_key, used for authentication
163 */
164 prf_t *prf_auth_i;
165
166 /**
167 * PRF, with key set to pr_key, used for authentication
168 */
169 prf_t *prf_auth_r;
170
171 /**
172 * Next message id to receive.
173 */
174 u_int32_t message_id_in;
175
176 /**
177 * Next message id to send.
178 */
179 u_int32_t message_id_out;
180
181 /**
182 * Last reply id which was successfully received.
183 */
184 int32_t last_replied_message_id;
185
186 /**
187 * A logger for this IKE_SA.
188 */
189 logger_t *logger;
190
191 /**
192 * NAT hasher.
193 */
194 hasher_t *nat_hasher;
195
196 /**
197 * NAT status of local host.
198 */
199 bool nat_here;
200
201 /**
202 * NAT status of remote host.
203 */
204 bool nat_there;
205
206 /**
207 * Timestamp of last IKE message received on this SA
208 */
209 struct timeval last_msg_in_tv;
210
211 /**
212 * Timestamp of last IKE message sent on this SA
213 */
214 struct timeval last_msg_out_tv;
215
216 /*
217 * Message ID of last DPD message
218 */
219 u_int32_t last_dpd_message_id;
220 };
221
222 /**
223 * Implementation of protected_ike_sa_t.build_message.
224 */
225 static void build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message)
226 {
227 message_t *new_message;
228 host_t *me, *other;
229
230 me = this->connection->get_my_host(this->connection);
231 other = this->connection->get_other_host(this->connection);
232
233 this->logger->log(this->logger, CONTROL|LEVEL2, "Build empty message");
234 new_message = message_create();
235 new_message->set_source(new_message, me->clone(me));
236 new_message->set_destination(new_message, other->clone(other));
237 new_message->set_exchange_type(new_message, type);
238 new_message->set_request(new_message, request);
239 new_message->set_message_id(new_message, (request) ? this->message_id_out : this->message_id_in);
240 new_message->set_ike_sa_id(new_message, this->ike_sa_id);
241
242 *message = new_message;
243 }
244
245 /**
246 * Implementation of ike_sa_t.get_state.
247 */
248 static ike_sa_state_t get_state(private_ike_sa_t *this)
249 {
250 return this->current_state->get_state(this->current_state);
251 }
252
253 /**
254 * Implementation of protected_ike_sa_t.set_new_state.
255 */
256 static void set_new_state(private_ike_sa_t *this, state_t *state)
257 {
258 this->logger->log(this->logger, CONTROL, "statechange: %s => %s",
259 mapping_find(ike_sa_state_m, get_state(this)),
260 mapping_find(ike_sa_state_m, state->get_state(state)));
261 this->current_state = state;
262 }
263
264 /**
265 * Implementation of protected_ike_sa_t.get_connection.
266 */
267 static connection_t *get_connection(private_ike_sa_t *this)
268 {
269 return this->connection;
270 }
271
272 /**
273 * Implementation of protected_ike_sa_t.set_connection.
274 */
275 static void set_connection(private_ike_sa_t *this,connection_t * connection)
276 {
277 this->connection = connection;
278 }
279
280 /**
281 * Implementation of protected_ike_sa_t.get_policy.
282 */
283 static policy_t *get_policy(private_ike_sa_t *this)
284 {
285 return this->policy;
286 }
287
288 /**
289 * Implementation of protected_ike_sa_t.set_policy.
290 */
291 static void set_policy(private_ike_sa_t *this,policy_t * policy)
292 {
293 this->policy = policy;
294 }
295
296 /**
297 * Implementation of protected_ike_sa_t.get_prf.
298 */
299 static prf_t *get_prf(private_ike_sa_t *this)
300 {
301 return this->prf;
302 }
303
304 /**
305 * Implementation of protected_ike_sa_t.get_prf.
306 */
307 static prf_t *get_child_prf(private_ike_sa_t *this)
308 {
309 return this->child_prf;
310 }
311
312 /**
313 * Implementation of protected_ike_sa_t.get_prf_auth_i.
314 */
315 static prf_t *get_prf_auth_i(private_ike_sa_t *this)
316 {
317 return this->prf_auth_i;
318 }
319
320 /**
321 * Implementation of protected_ike_sa_t.get_prf_auth_r.
322 */
323 static prf_t *get_prf_auth_r(private_ike_sa_t *this)
324 {
325 return this->prf_auth_r;
326 }
327 /**
328 * Implementation of ike_sa_t.get_id.
329 */
330 static ike_sa_id_t* get_id(private_ike_sa_t *this)
331 {
332 return this->ike_sa_id;
333 }
334
335 /**
336 * Implementation of ike_sa_t.get_my_host.
337 */
338 static host_t* get_my_host(private_ike_sa_t *this)
339 {
340 return this->connection->get_my_host(this->connection);;
341 }
342
343 /**
344 * Implementation of ike_sa_t.get_other_host.
345 */
346 static host_t* get_other_host(private_ike_sa_t *this)
347 {
348 return this->connection->get_other_host(this->connection);
349 }
350
351 /**
352 * Implementation of ike_sa_t.get_my_id.
353 */
354 static identification_t* get_my_id(private_ike_sa_t *this)
355 {
356 return this->policy->get_my_id(this->policy);
357 }
358
359 /**
360 * Implementation of ike_sa_t.get_other_id.
361 */
362 static identification_t* get_other_id(private_ike_sa_t *this)
363 {
364 return this->policy->get_other_id(this->policy);
365 }
366
367 /**
368 * Implementation of ike_sa_t.retransmit_possible.
369 */
370 static bool retransmit_possible(private_ike_sa_t *this, u_int32_t message_id)
371 {
372 return ((this->last_requested_message)
373 && (message_id != this->last_replied_message_id)
374 && (message_id == this->last_requested_message->get_message_id(
375 this->last_requested_message)));
376 }
377
378 /**
379 * Implementation of ike_sa_t.retransmit_request.
380 */
381 static status_t retransmit_request(private_ike_sa_t *this, u_int32_t message_id)
382 {
383 packet_t *packet;
384
385 if (!this->protected.public.retransmit_possible(&this->protected.public, message_id))
386 {
387 return NOT_FOUND;
388 }
389
390 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to retransmit message with id %d",message_id);
391 packet = this->last_requested_message->get_packet(this->last_requested_message);
392 charon->send_queue->add(charon->send_queue, packet);
393 this->update_timestamp(this, FALSE);
394 return SUCCESS;
395 }
396
397 /**
398 * Implementation of protected_ike_sa_t.build_transforms.
399 */
400 static status_t build_transforms(private_ike_sa_t *this, proposal_t *proposal, diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r)
401 {
402 chunk_t nonces, nonces_spis, skeyseed, key, secret;
403 u_int64_t spi_i, spi_r;
404 prf_plus_t *prf_plus;
405 algorithm_t *algo;
406 size_t key_size;
407
408 /*
409 * Build the PRF+ instance for deriving keys
410 */
411 if (this->prf != NULL)
412 {
413 this->prf->destroy(this->prf);
414 }
415 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
416 {
417 this->logger->log(this->logger, ERROR|LEVEL2, "No PRF algoithm selected!?");
418 return FAILED;
419 }
420 this->prf = prf_create(algo->algorithm);
421 if (this->prf == NULL)
422 {
423 this->logger->log(this->logger, ERROR|LEVEL1,
424 "PSEUDO_RANDOM_FUNCTION %s not supported!",
425 mapping_find(pseudo_random_function_m, algo->algorithm));
426 return FAILED;
427 }
428
429 /* concatenate nonces = nonce_i | nonce_r */
430 nonces = chunk_alloc(nonce_i.len + nonce_r.len);
431 memcpy(nonces.ptr, nonce_i.ptr, nonce_i.len);
432 memcpy(nonces.ptr + nonce_i.len, nonce_r.ptr, nonce_r.len);
433
434 /* concatenate prf_seed = nonce_i | nonce_r | spi_i | spi_r */
435 nonces_spis = chunk_alloc(nonces.len + 16);
436 memcpy(nonces_spis.ptr, nonces.ptr, nonces.len);
437 spi_i = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
438 spi_r = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
439 memcpy(nonces_spis.ptr + nonces.len, &spi_i, 8);
440 memcpy(nonces_spis.ptr + nonces.len + 8, &spi_r, 8);
441
442 /* SKEYSEED = prf(Ni | Nr, g^ir) */
443 dh->get_shared_secret(dh, &secret);
444 this->logger->log_chunk(this->logger, PRIVATE, "Shared Diffie Hellman secret", secret);
445 this->prf->set_key(this->prf, nonces);
446 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
447 this->logger->log_chunk(this->logger, PRIVATE | LEVEL1, "SKEYSEED", skeyseed);
448 chunk_free(&secret);
449
450 /* prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr )
451 * = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr
452 *
453 * we use the prf directly for prf+
454 */
455 this->prf->set_key(this->prf, skeyseed);
456 prf_plus = prf_plus_create(this->prf, nonces_spis);
457
458 /* clean up unused stuff */
459 chunk_free(&nonces);
460 chunk_free(&nonces_spis);
461 chunk_free(&skeyseed);
462
463
464 /*
465 * We now can derive all of our key. We build the transforms
466 * directly.
467 */
468
469
470 /* SK_d used for prf+ to derive keys for child SAs */
471 this->child_prf = prf_create(algo->algorithm);
472 key_size = this->child_prf->get_key_size(this->child_prf);
473 prf_plus->allocate_bytes(prf_plus, key_size, &key);
474 this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", key);
475 this->child_prf->set_key(this->child_prf, key);
476 chunk_free(&key);
477
478
479 /* SK_ai/SK_ar used for integrity protection */
480 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
481 {
482 this->logger->log(this->logger, ERROR, "No integrity algoithm selected?!");
483 return FAILED;
484 }
485 if (this->signer_initiator != NULL)
486 {
487 this->signer_initiator->destroy(this->signer_initiator);
488 }
489 if (this->signer_responder != NULL)
490 {
491 this->signer_responder->destroy(this->signer_responder);
492 }
493
494 this->signer_initiator = signer_create(algo->algorithm);
495 this->signer_responder = signer_create(algo->algorithm);
496 if (this->signer_initiator == NULL || this->signer_responder == NULL)
497 {
498 this->logger->log(this->logger, ERROR,
499 "INTEGRITY_ALGORITHM %s not supported!",
500 mapping_find(integrity_algorithm_m,algo->algorithm));
501 return FAILED;
502 }
503 key_size = this->signer_initiator->get_key_size(this->signer_initiator);
504
505 prf_plus->allocate_bytes(prf_plus, key_size, &key);
506 this->logger->log_chunk(this->logger, CONTROL|LEVEL1, "Sk_ai secret", key);
507 this->signer_initiator->set_key(this->signer_initiator, key);
508 chunk_free(&key);
509
510 prf_plus->allocate_bytes(prf_plus, key_size, &key);
511 this->logger->log_chunk(this->logger, CONTROL|LEVEL1, "Sk_ar secret", key);
512 this->signer_responder->set_key(this->signer_responder, key);
513 chunk_free(&key);
514
515
516 /* SK_ei/SK_er used for encryption */
517 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
518 {
519 this->logger->log(this->logger, ERROR, "No encryption algoithm selected!?");
520 return FAILED;
521 }
522 if (this->crypter_initiator != NULL)
523 {
524 this->crypter_initiator->destroy(this->crypter_initiator);
525 }
526 if (this->crypter_responder != NULL)
527 {
528 this->crypter_responder->destroy(this->crypter_responder);
529 }
530
531 this->crypter_initiator = crypter_create(algo->algorithm, algo->key_size / 8);
532 this->crypter_responder = crypter_create(algo->algorithm, algo->key_size / 8);
533 if (this->crypter_initiator == NULL || this->crypter_responder == NULL)
534 {
535 this->logger->log(this->logger, ERROR,
536 "ENCRYPTION_ALGORITHM %s (key size %d) not supported!",
537 mapping_find(encryption_algorithm_m, algo->algorithm),
538 algo->key_size);
539 return FAILED;
540 }
541 key_size = this->crypter_initiator->get_key_size(this->crypter_initiator);
542
543 prf_plus->allocate_bytes(prf_plus, key_size, &key);
544 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", key);
545 this->crypter_initiator->set_key(this->crypter_initiator, key);
546 chunk_free(&key);
547
548 prf_plus->allocate_bytes(prf_plus, key_size, &key);
549 this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", key);
550 this->crypter_responder->set_key(this->crypter_responder, key);
551 chunk_free(&key);
552
553 /* SK_pi/SK_pr used for authentication */
554 if (this->prf_auth_i != NULL)
555 {
556 this->prf_auth_i->destroy(this->prf_auth_i);
557 }
558 if (this->prf_auth_r != NULL)
559 {
560 this->prf_auth_r->destroy(this->prf_auth_r);
561 }
562
563 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
564 this->prf_auth_i = prf_create(algo->algorithm);
565 this->prf_auth_r = prf_create(algo->algorithm);
566
567 key_size = this->prf_auth_i->get_key_size(this->prf_auth_i);
568 prf_plus->allocate_bytes(prf_plus, key_size, &key);
569 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", key);
570 this->prf_auth_i->set_key(this->prf_auth_i, key);
571 chunk_free(&key);
572
573 prf_plus->allocate_bytes(prf_plus, key_size, &key);
574 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", key);
575 this->prf_auth_r->set_key(this->prf_auth_r, key);
576 chunk_free(&key);
577
578 /* all done, prf_plus not needed anymore */
579 prf_plus->destroy(prf_plus);
580
581 return SUCCESS;
582 }
583
584 /**
585 * Implementation of protected_ike_sa_t.get_randomizer.
586 */
587 static randomizer_t *get_randomizer(private_ike_sa_t *this)
588 {
589 return this->randomizer;
590 }
591
592 /**
593 * Implementation of protected_ike_sa_t.get_crypter_initiator.
594 */
595 static crypter_t *get_crypter_initiator(private_ike_sa_t *this)
596 {
597 return this->crypter_initiator;
598 }
599
600 /**
601 * Implementation of protected_ike_sa_t.get_signer_initiator.
602 */
603 static signer_t *get_signer_initiator(private_ike_sa_t *this)
604 {
605 return this->signer_initiator;
606 }
607
608 /**
609 * Implementation of protected_ike_sa_t.get_crypter_responder.
610 */
611 static crypter_t *get_crypter_responder(private_ike_sa_t *this)
612 {
613 return this->crypter_responder;
614 }
615
616 /**
617 * Implementation of protected_ike_sa_t.get_signer_responder.
618 */
619 static signer_t *get_signer_responder(private_ike_sa_t *this)
620 {
621 return this->signer_responder;
622 }
623
624 /**
625 * Implementation of protected_ike_sa_t.update_timestamp
626 */
627 static void update_timestamp(private_ike_sa_t *this, bool in)
628 {
629 /* bump last message sent timestamp */
630 struct timeval *tv = in ? &this->last_msg_in_tv : &this->last_msg_out_tv;
631 if (0 > gettimeofday(tv, NULL))
632 {
633 this->logger->log(this->logger, ERROR|LEVEL1,
634 "Warning: Failed to get time of day.");
635 }
636 }
637
638 /**
639 * Implementation of protected_ike_sa_t.send_request.
640 */
641 static status_t send_request(private_ike_sa_t *this, message_t *message)
642 {
643 retransmit_request_job_t *retransmit_job;
644 u_int32_t timeout;
645 crypter_t *crypter;
646 signer_t *signer;
647 packet_t *packet;
648 status_t status;
649
650 if (message->get_message_id(message) != this->message_id_out)
651 {
652 this->logger->log(this->logger, ERROR, "Message could not be sent cause id (%d) was not as expected (%d)",
653 message->get_message_id(message),this->message_id_out);
654 return FAILED;
655 }
656
657 /* generate packet */
658 this->logger->log(this->logger, CONTROL|LEVEL2, "Generate packet from message");
659
660 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
661 {
662 crypter = this->crypter_initiator;
663 signer = this->signer_initiator;
664 }
665 else
666 {
667 crypter = this->crypter_responder;
668 signer =this->signer_responder;
669 }
670
671 status = message->generate(message, crypter,signer, &packet);
672 if (status != SUCCESS)
673 {
674 this->logger->log(this->logger, ERROR, "Could not generate packet from message");
675 return FAILED;
676 }
677
678 this->logger->log(this->logger, CONTROL|LEVEL3,
679 "Add request packet with message id %d to global send queue",
680 this->message_id_out);
681 charon->send_queue->add(charon->send_queue, packet);
682
683 /* replace last message for retransmit with current */
684 if (this->last_requested_message != NULL)
685 {
686 this->last_requested_message->destroy(this->last_requested_message);
687 }
688 this->logger->log(this->logger, CONTROL|LEVEL3, "Replace last requested message with new one");
689 this->last_requested_message = message;
690
691 /* schedule a job for retransmission */
692 status = charon->configuration->get_retransmit_timeout(charon->configuration, 0, &timeout);
693 if (status != SUCCESS)
694 {
695 this->logger->log(this->logger, CONTROL|LEVEL2, "No retransmit job for message created!");
696 }
697 else
698 {
699 this->logger->log(this->logger, CONTROL|LEVEL2, "Request will be retransmitted in %d ms.", timeout);
700 retransmit_job = retransmit_request_job_create(this->message_id_out, this->ike_sa_id);
701 charon->event_queue->add_relative(charon->event_queue, (job_t *)retransmit_job, timeout);
702 }
703
704 /* message counter can now be increased */
705 this->logger->log(this->logger, CONTROL|LEVEL3,
706 "Increase message counter for outgoing messages from %d",
707 this->message_id_out);
708 this->message_id_out++;
709
710 this->update_timestamp(this, FALSE);
711 return SUCCESS;
712 }
713
714 /**
715 * Implementation of protected_ike_sa_t.send_response.
716 */
717 static status_t send_response(private_ike_sa_t *this, message_t *message)
718 {
719 crypter_t *crypter;
720 signer_t *signer;
721 packet_t *packet;
722 status_t status;
723
724 if (message->get_message_id(message) != this->message_id_in)
725 {
726
727 this->logger->log(this->logger, ERROR, "Message could not be sent cause id (%d) was not as expected (%d)",
728 message->get_message_id(message),this->message_id_in);
729 return FAILED;
730 }
731
732 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
733 {
734 crypter = this->crypter_initiator;
735 signer = this->signer_initiator;
736 }
737 else
738 {
739 crypter = this->crypter_responder;
740 signer =this->signer_responder;
741 }
742
743 status = message->generate(message, crypter,signer, &packet);
744 if (status != SUCCESS)
745 {
746 this->logger->log(this->logger, ERROR, "Could not generate packet from message");
747 return FAILED;
748 }
749
750 this->logger->log(this->logger, CONTROL|LEVEL3,
751 "Add response packet with message id %d to global send queue",
752 this->message_id_in);
753 charon->send_queue->add(charon->send_queue, packet);
754
755 if (this->last_responded_message != NULL)
756 {
757 /* destroy message */
758 this->last_responded_message->destroy(this->last_responded_message);
759 }
760
761 this->logger->log(this->logger, CONTROL|LEVEL3, "Replace last responded message with new one");
762 this->last_responded_message = message;
763
764 /* message counter can now be increased */
765 this->logger->log(this->logger, CONTROL|LEVEL3, "Increase message counter for incoming messages");
766 this->message_id_in++;
767
768 this->update_timestamp(this, FALSE);
769
770 return SUCCESS;
771 }
772
773 /**
774 * Implementation of of private_responder_init_t.send_notify_reply.
775 */
776 static void send_notify(private_ike_sa_t *this, exchange_type_t exchange_type, notify_message_type_t type, chunk_t data)
777 {
778 notify_payload_t *payload;
779 message_t *response;
780 packet_t *packet;
781 status_t status;
782
783 this->logger->log(this->logger, CONTROL|LEVEL2, "Going to build message with notify payload");
784 /* set up the reply */
785 build_message(this, exchange_type, FALSE, &response);
786 payload = notify_payload_create_from_protocol_and_type(PROTO_NONE, type);
787 if ((data.ptr != NULL) && (data.len > 0))
788 {
789 this->logger->log(this->logger, CONTROL|LEVEL2, "Add Data to notify payload");
790 payload->set_notification_data(payload,data);
791 }
792
793 this->logger->log(this->logger, CONTROL|LEVEL2, "Add Notify payload to message");
794 response->add_payload(response,(payload_t *) payload);
795
796 /* generate packet */
797 this->logger->log(this->logger, CONTROL|LEVEL2, "Generate packet from message");
798 status = response->generate(response, this->crypter_responder, this->signer_responder, &packet);
799 if (status != SUCCESS)
800 {
801 this->logger->log(this->logger, ERROR|LEVEL1, "Could not generate notify message");
802 response->destroy(response);
803 return;
804 }
805
806 this->logger->log(this->logger, CONTROL|LEVEL2, "Add packet to global send queue");
807 charon->send_queue->add(charon->send_queue, packet);
808 this->logger->log(this->logger, CONTROL|LEVEL2, "Destroy message");
809 response->destroy(response);
810
811 this->update_timestamp(this, FALSE);
812 }
813
814 /**
815 * Implementation of protected_ike_sa_t.set_last_replied_message_id.
816 */
817 static void set_last_replied_message_id (private_ike_sa_t *this,u_int32_t message_id)
818 {
819 this->last_replied_message_id = message_id;
820 }
821
822 /**
823 * Implementation of protected_ike_sa_t.get_last_responded_message.
824 */
825 static message_t *get_last_responded_message (private_ike_sa_t *this)
826 {
827 return this->last_responded_message;
828 }
829
830 /**
831 * Implementation of protected_ike_sa_t.get_last_requested_message.
832 */
833 static message_t *get_last_requested_message(private_ike_sa_t *this)
834 {
835 return this->last_requested_message;
836 }
837
838 /**
839 * Implementation of protected_ike_sa_t.add_child_sa.
840 */
841 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
842 {
843 this->child_sas->insert_last(this->child_sas, child_sa);
844 }
845
846 /**
847 * Implementation of ike_sa_t.process_message.
848 */
849 static status_t process_message(private_ike_sa_t *this, message_t *message)
850 {
851 u_int32_t message_id;
852 exchange_type_t exchange_type;
853 bool is_request;
854
855 /* Find out type of message (request or response) */
856 is_request = message->get_request(message);
857 exchange_type = message->get_exchange_type(message);
858
859 this->logger->log(this->logger, CONTROL|LEVEL1, "Process %s of exchange type %s",
860 (is_request) ? "request" : "response",
861 mapping_find(exchange_type_m, exchange_type));
862
863 message_id = message->get_message_id(message);
864
865 /* check if message already received, and retransmit its reply */
866 if (is_request && (message_id == (this->message_id_in - 1)))
867 {
868 /* resend last message, if any */
869 if (this->last_responded_message)
870 {
871 packet_t *packet = this->last_responded_message->get_packet(this->last_responded_message);
872 this->logger->log(this->logger, CONTROL|LEVEL1, "Resent request detected. Send stored reply.");
873 charon->send_queue->add(charon->send_queue, packet);
874 this->update_timestamp(this, FALSE);
875 return SUCCESS;
876 }
877 else
878 {
879 /* somebody does something nasty here... */
880 return FAILED;
881 }
882 }
883
884 /* Now, the message id is checked for request AND reply */
885 if (is_request)
886 {
887 /* In a request, the message has to be this->message_id_in (other case is already handled) */
888 if (message_id != this->message_id_in)
889 {
890 this->logger->log(this->logger, ERROR | LEVEL1,
891 "Message request with message id %d received, but %d expected",
892 message_id,this->message_id_in);
893 return FAILED;
894 }
895 }
896 else
897 {
898 /* In a reply, the message has to be this->message_id_out -1 cause it is the reply to the last sent message*/
899 if (message_id != (this->message_id_out - 1))
900 {
901 this->logger->log(this->logger, ERROR | LEVEL1,
902 "Message reply with message id %d received, but %d expected",
903 message_id,this->message_id_in);
904 return FAILED;
905 }
906 }
907
908 this->update_timestamp(this, TRUE);
909
910 /* now the message is processed by the current state object.
911 * The specific state object is responsible to check if a message can be received in
912 * the state it represents.
913 * The current state is also responsible to change the state object to the next state
914 * by calling protected_ike_sa_t.set_new_state
915 */
916 return this->current_state->process_message(this->current_state, message);
917 }
918
919 /**
920 * Implementation of protected_ike_sa_t.initiate_connection.
921 */
922 static status_t initiate_connection(private_ike_sa_t *this, connection_t *connection)
923 {
924 initiator_init_t *current_state;
925
926 /* Work is done in state object of type INITIATOR_INIT. All other states are not
927 * initial states and so don't have a initiate_connection function */
928
929 if (this->current_state->get_state(this->current_state) != INITIATOR_INIT)
930 {
931 return FAILED;
932 }
933
934 current_state = (initiator_init_t *) this->current_state;
935
936 return current_state->initiate_connection(current_state, connection);
937 }
938
939 /**
940 * Implementation of protected_ike_sa_t.update_connection_hosts.
941 *
942 * Quoting RFC 4306:
943 *
944 * 2.11. Address and Port Agility
945 *
946 * IKE runs over UDP ports 500 and 4500, and implicitly sets up ESP and
947 * AH associations for the same IP addresses it runs over. The IP
948 * addresses and ports in the outer header are, however, not themselves
949 * cryptographically protected, and IKE is designed to work even through
950 * Network Address Translation (NAT) boxes. An implementation MUST
951 * accept incoming requests even if the source port is not 500 or 4500,
952 * and MUST respond to the address and port from which the request was
953 * received. It MUST specify the address and port at which the request
954 * was received as the source address and port in the response. IKE
955 * functions identically over IPv4 or IPv6.
956 *
957 * [...]
958 *
959 * There are cases where a NAT box decides to remove mappings that
960 * are still alive (for example, the keepalive interval is too long,
961 * or the NAT box is rebooted). To recover in these cases, hosts
962 * that are not behind a NAT SHOULD send all packets (including
963 * retransmission packets) to the IP address and port from the last
964 * valid authenticated packet from the other end (i.e., dynamically
965 * update the address). A host behind a NAT SHOULD NOT do this
966 * because it opens a DoS attack possibility. Any authenticated IKE
967 * packet or any authenticated UDP-encapsulated ESP packet can be
968 * used to detect that the IP address or the port has changed.
969 */
970 static status_t update_connection_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
971 {
972 host_t *old_other = NULL;
973 iterator_t *iterator = NULL;
974 child_sa_t *child_sa = NULL;
975 int my_changes, other_changes;
976 ike_sa_state_t s;
977
978 my_changes = me->get_differences(me, this->connection->get_my_host(this->connection));
979
980 old_other = this->connection->get_other_host(this->connection);
981 other_changes = other->get_differences(other, old_other);
982
983 if (!my_changes && !other_changes) {
984 return SUCCESS;
985 }
986
987 if (my_changes)
988 {
989 this->connection->update_my_host(this->connection, me->clone(me));
990 }
991
992 s = this->protected.public.get_state(&this->protected.public);
993
994 if (s == RESPONDER_INIT || s == IKE_SA_INIT_REQUESTED || !this->nat_here)
995 {
996 if (other_changes)
997 {
998 this->connection->update_other_host(this->connection, other->clone(other));
999 }
1000 }
1001 else
1002 {
1003 if (other_changes & HOST_DIFF_ADDR)
1004 {
1005 this->logger->log(this->logger, ERROR|LEVEL1,
1006 "Destination ip changed from %s to %s. As we are NATed this is not allowed!",
1007 old_other->get_address(old_other), other->get_address(other));
1008 return DESTROY_ME;
1009 }
1010 else if (other_changes & HOST_DIFF_PORT)
1011 {
1012 old_other->set_port(old_other, other->get_port(other));
1013 }
1014 }
1015
1016 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1017 while (iterator->iterate(iterator, (void**)&child_sa))
1018 {
1019 child_sa->update_hosts(child_sa,
1020 this->connection->get_my_host(this->connection),
1021 this->connection->get_other_host(this->connection),
1022 my_changes, other_changes);
1023 /* XXX error handling */
1024 }
1025 iterator->destroy(iterator);
1026
1027 return SUCCESS;
1028 }
1029
1030 /**
1031 * Implementation of protected_ike_sa_t.build_transforms.
1032 * TODO: IPv6 support.
1033 */
1034 static chunk_t generate_natd_hash(private_ike_sa_t *this, u_int64_t spi_i, u_int64_t spi_r, host_t *host)
1035 {
1036 chunk_t natd_string;
1037 chunk_t natd_hash;
1038 void *p;
1039 struct sockaddr_in* sai;
1040 char buf[512];
1041
1042 natd_hash = chunk_alloc(this->nat_hasher->get_hash_size(this->nat_hasher));
1043 natd_string = chunk_alloc(8 + 8 + 4 + 2);
1044
1045 sai = (struct sockaddr_in*)host->get_sockaddr(host);
1046 p = natd_string.ptr;
1047 *(u_int64_t*)p = spi_i; p += sizeof(spi_i);
1048 *(u_int64_t*)p = spi_r; p += sizeof(spi_r);
1049 *(u_int32_t*)p = sai->sin_addr.s_addr; p += sizeof(sai->sin_addr.s_addr);
1050 *(u_int16_t*)p = sai->sin_port; p += sizeof(sai->sin_port);
1051
1052 this->nat_hasher->get_hash(this->nat_hasher, natd_string, natd_hash.ptr);
1053 this->nat_hasher->reset(this->nat_hasher);
1054
1055 sprintf(buf, "natd_hash(%016llx %016llx %s:%d)\n == SHA1(", spi_i, spi_r,
1056 host->get_address(host), host->get_port(host));
1057 chunk_to_hex(buf + strlen(buf), sizeof(buf) - strlen(buf), natd_string);
1058 strcat(buf, ") == ");
1059 chunk_to_hex(buf + strlen(buf), sizeof(buf) - strlen(buf), natd_hash);
1060 this->logger->log(this->logger, CONTROL|LEVEL3, buf);
1061
1062 chunk_free(&natd_string);
1063 return natd_hash;
1064 }
1065
1066 /**
1067 * Implementation of ike_sa_t.send_dpd_request.
1068 */
1069 static status_t send_dpd_request(private_ike_sa_t *this)
1070 {
1071 message_t *dpd_msg;
1072 status_t status;
1073 this->protected.build_message(&this->protected, INFORMATIONAL, TRUE, &dpd_msg);
1074 status = this->protected.send_request(&this->protected, dpd_msg);
1075 if (status != SUCCESS)
1076 {
1077 dpd_msg->destroy(dpd_msg);
1078 }
1079 this->last_dpd_message_id = dpd_msg->get_message_id(dpd_msg);
1080 return status;
1081 }
1082
1083 /**
1084 * Implementation of ike_sa_t.get_last_dpd_message_id
1085 */
1086 static u_int32_t get_last_dpd_message_id(private_ike_sa_t *this)
1087 {
1088 return this->last_dpd_message_id;
1089 }
1090
1091 /**
1092 * Implementation of ike_sa_t.get_child_sa.
1093 */
1094 static child_sa_t *get_child_sa(private_ike_sa_t *this, u_int32_t reqid)
1095 {
1096 iterator_t *iterator;
1097 child_sa_t *current, *found = NULL;
1098
1099 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1100 while (iterator->has_next(iterator))
1101 {
1102 iterator->current(iterator, (void**)&current);
1103 if (current->get_reqid(current) == reqid)
1104 {
1105 found = current;
1106 break;
1107 }
1108 }
1109 iterator->destroy(iterator);
1110 return found;
1111 }
1112
1113 /**
1114 * Implementation of ike_sa_t.delete_child_sa.
1115 */
1116 static status_t delete_child_sa(private_ike_sa_t *this, u_int32_t reqid)
1117 {
1118 message_t *request;
1119 child_sa_t *child_sa;
1120 delete_payload_t *delete_payload;
1121 state_t *old_state;
1122
1123 if (this->current_state->get_state(this->current_state) != IKE_SA_ESTABLISHED)
1124 {
1125 this->logger->log(this->logger, ERROR|LEVEL1,
1126 "Delete of a CHILD_SA whose IKE_SA not in state IKE_SA_ESTABLISHED, aborting");
1127 return FAILED;
1128 }
1129
1130 child_sa = get_child_sa(this, reqid);
1131 if (child_sa == NULL)
1132 {
1133 this->logger->log(this->logger, ERROR|LEVEL1,
1134 "IKE_SA does not contain a CHILD_SA with reqid %d", reqid);
1135 return FAILED;
1136 }
1137 build_message(this, INFORMATIONAL, TRUE, &request);
1138 delete_payload = delete_payload_create(child_sa->get_protocol(child_sa));
1139 delete_payload->add_spi(delete_payload, child_sa->get_spi(child_sa, TRUE));
1140 request->add_payload(request, (payload_t*)delete_payload);
1141
1142 send_request(this, request);
1143
1144 old_state = this->current_state;
1145 set_new_state(this, (state_t*)delete_child_sa_requested_create(&this->protected));
1146 old_state->destroy(old_state);
1147 return SUCCESS;
1148 }
1149
1150 /**
1151 * Implementation of protected_ike_sa_t.destroy_child_sa.
1152 */
1153 static u_int32_t destroy_child_sa(private_ike_sa_t *this, u_int32_t spi)
1154 {
1155 iterator_t *iterator;
1156 child_sa_t *child_sa;
1157
1158 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1159 while (iterator->has_next(iterator))
1160 {
1161 iterator->current(iterator, (void**)&child_sa);
1162 if (child_sa->get_spi(child_sa, FALSE) == spi)
1163 {
1164 iterator->remove(iterator);
1165 break;
1166 }
1167 else
1168 {
1169 child_sa = NULL;
1170 }
1171 }
1172 iterator->destroy(iterator);
1173 if (child_sa == NULL)
1174 {
1175 this->logger->log(this->logger, ERROR,
1176 "IKE_SA does not contain a CHILD_SA with spi 0x%x", spi);
1177 return 0;
1178 }
1179
1180 spi = child_sa->get_spi(child_sa, TRUE);
1181 child_sa->destroy(child_sa);
1182 return spi;
1183 }
1184
1185 /**
1186 * Implementation of protected_ike_sa_t.get_child_sa.
1187 */
1188 static child_sa_t* get_child_sa_by_spi(private_ike_sa_t *this, u_int32_t spi)
1189 {
1190 iterator_t *iterator;
1191 child_sa_t *current, *found = NULL;
1192
1193 iterator = this->child_sas->create_iterator(this->child_sas, FALSE);
1194 while (iterator->has_next(iterator))
1195 {
1196 iterator->current(iterator, (void**)&current);
1197 if (current->get_spi(current, FALSE) == spi)
1198 {
1199 found = current;
1200 }
1201 }
1202 iterator->destroy(iterator);
1203 return found;
1204 }
1205
1206 /**
1207 * Implementation of ike_sa_t.rekey_child_sa.
1208 */
1209 static status_t rekey_child_sa(private_ike_sa_t *this, u_int32_t reqid)
1210 {
1211 message_t *request;
1212 child_sa_t *child_sa;
1213 notify_payload_t *notify;
1214 sa_payload_t *sa_payload;
1215 ts_payload_t *tsi_payload, *tsr_payload;
1216 nonce_payload_t *nonce_payload;
1217 linked_list_t *proposals;
1218 chunk_t nonce;
1219 linked_list_t *my_ts, *other_ts;
1220 state_t *old_state;
1221
1222 if (this->current_state->get_state(this->current_state) != IKE_SA_ESTABLISHED)
1223 {
1224 this->logger->log(this->logger, ERROR|LEVEL1,
1225 "Rekeying of an CHILD_SA whose IKE_SA not in state IKE_SA_ESTABLISHED, aborting");
1226 return FAILED;
1227 }
1228
1229 child_sa = get_child_sa(this, reqid);
1230 if (child_sa == NULL)
1231 {
1232 this->logger->log(this->logger, ERROR|LEVEL1,
1233 "IKE_SA does not contain a CHILD_SA with reqid %d", reqid);
1234 return FAILED;
1235 }
1236
1237 build_message(this, CREATE_CHILD_SA, TRUE, &request);
1238 notify = notify_payload_create_from_protocol_and_type(
1239 child_sa->get_protocol(child_sa), REKEY_SA);
1240 notify->set_spi(notify, child_sa->get_spi(child_sa, TRUE));
1241 request->add_payload(request, (payload_t*)notify);
1242
1243 proposals = this->policy->get_proposals(this->policy);
1244 child_sa = child_sa_create(reqid,
1245 this->connection->get_my_host(this->connection),
1246 this->connection->get_other_host(this->connection),
1247 this->policy->get_soft_lifetime(this->policy),
1248 this->policy->get_hard_lifetime(this->policy),
1249 this->nat_here || this->nat_there);
1250 child_sa->alloc(child_sa, proposals);
1251 sa_payload = sa_payload_create_from_proposal_list(proposals);
1252 request->add_payload(request, (payload_t*)sa_payload);
1253
1254 nonce_payload = nonce_payload_create();
1255 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
1256 NONCE_SIZE, &nonce))
1257 {
1258 request->destroy(request);
1259 return FAILED;
1260 }
1261 nonce_payload->set_nonce(nonce_payload, nonce);
1262 request->add_payload(request, (payload_t*)nonce_payload);
1263
1264 my_ts = this->policy->get_my_traffic_selectors(this->policy);
1265 other_ts = this->policy->get_other_traffic_selectors(this->policy);
1266 tsi_payload = ts_payload_create_from_traffic_selectors(TRUE, my_ts);
1267 tsr_payload = ts_payload_create_from_traffic_selectors(FALSE, other_ts);
1268 request->add_payload(request, (payload_t*)tsi_payload);
1269 request->add_payload(request, (payload_t*)tsr_payload);
1270
1271 send_request(this, request);
1272
1273 old_state = this->current_state;
1274 set_new_state(this, (state_t*)create_child_sa_requested_create(&this->protected, child_sa, nonce, reqid));
1275 old_state->destroy(old_state);
1276
1277 return SUCCESS;
1278 }
1279
1280 /**
1281 * Implementation of protected_ike_sa_t.reset_message_buffers.
1282 */
1283 static void reset_message_buffers(private_ike_sa_t *this)
1284 {
1285 this->logger->log(this->logger, CONTROL|LEVEL2, "Reset message counters and destroy stored messages");
1286 /* destroy stored requested message */
1287 if (this->last_requested_message != NULL)
1288 {
1289 this->last_requested_message->destroy(this->last_requested_message);
1290 this->last_requested_message = NULL;
1291 }
1292
1293 /* destroy stored responded messages */
1294 if (this->last_responded_message != NULL)
1295 {
1296 this->last_responded_message->destroy(this->last_responded_message);
1297 this->last_responded_message = NULL;
1298 }
1299
1300 this->message_id_out = 0;
1301 this->message_id_in = 0;
1302 this->last_replied_message_id = -1;
1303 }
1304
1305 /**
1306 * Implementation of protected_ike_sa_t.log_status.
1307 */
1308 static void log_status(private_ike_sa_t *this, logger_t *logger, char *name)
1309 {
1310 iterator_t *iterator;
1311 child_sa_t *child_sa;
1312 host_t *my_host, *other_host;
1313 identification_t *my_id = NULL, *other_id = NULL;
1314
1315 /* only log if name == NULL or name == connection_name */
1316 if (name)
1317 {
1318 if (streq(this->connection->get_name(this->connection), name))
1319 {
1320 return;
1321 }
1322 }
1323 my_host = this->connection->get_my_host(this->connection);
1324 other_host = this->connection->get_other_host(this->connection);
1325
1326 /* use policy information, if available */
1327 if (this->policy)
1328 {
1329 my_id = this->policy->get_my_id(this->policy);
1330 other_id = this->policy->get_other_id(this->policy);
1331 name = this->policy->get_name(this->policy);
1332 }
1333 else
1334 {
1335 name = this->connection->get_name(this->connection);
1336 }
1337
1338 if (logger == NULL)
1339 {
1340 logger = this->logger;
1341 }
1342 logger->log(logger, CONTROL|LEVEL1, " \"%s\": IKE_SA in state %s, SPIs: 0x%.16llx 0x%.16llx",
1343 name,
1344 mapping_find(ike_sa_state_m, this->current_state->get_state(this->current_state)),
1345 this->ike_sa_id->get_initiator_spi(this->ike_sa_id),
1346 this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1347 logger->log(logger, CONTROL, " \"%s\": %s[%s]...%s[%s]",
1348 name,
1349 my_host->get_address(my_host),
1350 my_id ? my_id->get_string(my_id) : "(unknown)",
1351 other_host->get_address(other_host),
1352 other_id ? other_id->get_string(other_id) : "(unknown)");
1353
1354 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1355 while (iterator->has_next(iterator))
1356 {
1357 iterator->current(iterator, (void**)&child_sa);
1358 child_sa->log_status(child_sa, logger, name);
1359 }
1360 iterator->destroy(iterator);
1361 }
1362
1363 /**
1364 * Implementation of public_ike_sa_t.delete.
1365 */
1366 static status_t delete_(private_ike_sa_t *this)
1367 {
1368 message_t *informational_request;
1369 delete_payload_t *delete_payload;
1370 u_int32_t timeout;
1371 delete_half_open_ike_sa_job_t *job;
1372 state_t *old_state;
1373
1374 if (get_state(this) != IKE_SA_ESTABLISHED)
1375 {
1376 return INVALID_STATE;
1377 }
1378
1379 build_message(this, INFORMATIONAL, TRUE, &informational_request);
1380 /* delete for the full IKE_SA, this deletes all child_sa's implicit */
1381 delete_payload = delete_payload_create(PROTO_IKE);
1382
1383 informational_request->add_payload(informational_request, (payload_t*)delete_payload);
1384
1385 if (send_request(this, informational_request) != SUCCESS)
1386 {
1387 /* send failed, but we ignore this, SA will get deleted anyway later */
1388 informational_request->destroy(informational_request);
1389 }
1390
1391 /* transit to state delete_ike_sa_requested */
1392 old_state = this->current_state;
1393 set_new_state(this, (state_t*)delete_ike_sa_requested_create(&this->protected));
1394 old_state->destroy(old_state);
1395
1396 /* there is no guarantee that the other peer will acknowledge the delete,
1397 * so we have to set a timeout where we destroy the SA... This is done with
1398 * the delete_half_open_ike_sa_job as used in IKE SA setup.
1399 */
1400 timeout = charon->configuration->get_half_open_ike_sa_timeout(charon->configuration);
1401 job = delete_half_open_ike_sa_job_create(this->ike_sa_id);
1402 charon->event_queue->add_relative(charon->event_queue, (job_t*)job, timeout);
1403 return SUCCESS;
1404 }
1405
1406 /**
1407 * Implementation of ike_sa_t.is_my_host_behind_nat.
1408 */
1409 static bool is_my_host_behind_nat (private_ike_sa_t *this)
1410 {
1411 return this->nat_here;
1412 }
1413
1414 /**
1415 * Implementation of ike_sa_t.is_other_host_behind_nat.
1416 */
1417 static bool is_other_host_behind_nat (private_ike_sa_t *this)
1418 {
1419 return this->nat_there;
1420 }
1421
1422 /**
1423 * Implementation of ike_sa_t.is_any_host_behind_nat.
1424 */
1425 static bool is_any_host_behind_nat (private_ike_sa_t *this)
1426 {
1427 return this->nat_here || this->nat_there;
1428 }
1429
1430 /**
1431 * Implementation of protected_ike_sa_t.set_my_host_behind_nat.
1432 */
1433 static void set_my_host_behind_nat (private_ike_sa_t *this, bool nat)
1434 {
1435 this->nat_here = nat;
1436 }
1437
1438 /**
1439 * Implementation of protected_ike_sa_t.set_other_host_behind_nat.
1440 */
1441 static void set_other_host_behind_nat (private_ike_sa_t *this, bool nat)
1442 {
1443 this->nat_there = nat;
1444 }
1445
1446 /**
1447 * Implementation of private_ike_sa_t.get_last_esp_traffic_tv
1448 */
1449 static struct timeval get_last_esp_traffic_tv(private_ike_sa_t * this, bool inbound)
1450 {
1451 iterator_t *iterator;
1452 child_sa_t *child_sa;
1453 bool ret = TRUE;
1454 time_t use_time = 0;
1455 struct timeval tv = {0, 0};
1456
1457 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1458 while (iterator->iterate(iterator, (void**)&child_sa))
1459 {
1460 if (child_sa->get_use_time(child_sa, inbound, &use_time) == SUCCESS
1461 && use_time != 0)
1462 {
1463 tv.tv_sec = max(tv.tv_sec, use_time);
1464 }
1465 }
1466 iterator->destroy(iterator);
1467
1468 return tv;
1469 }
1470
1471 /**
1472 * Implementation of ike_sa_t.get_last_traffic_in_tv.
1473 */
1474 static struct timeval get_last_traffic_in_tv (private_ike_sa_t *this)
1475 {
1476 struct timeval esp_tv = this->get_last_esp_traffic_tv(this, TRUE);
1477 return this->last_msg_in_tv.tv_sec > esp_tv.tv_sec ? this->last_msg_in_tv
1478 : this->last_msg_in_tv.tv_sec < esp_tv.tv_sec ? esp_tv
1479 : this->last_msg_in_tv.tv_usec > esp_tv.tv_usec ? this->last_msg_in_tv : esp_tv;
1480 }
1481
1482 /**
1483 * Implementation of ike_sa_t.get_last_traffic_out_tv.
1484 */
1485 static struct timeval get_last_traffic_out_tv (private_ike_sa_t *this)
1486 {
1487 struct timeval esp_tv = this->get_last_esp_traffic_tv(this, FALSE);
1488 return this->last_msg_out_tv.tv_sec > esp_tv.tv_sec ? this->last_msg_out_tv
1489 : this->last_msg_out_tv.tv_sec < esp_tv.tv_sec ? esp_tv
1490 : this->last_msg_out_tv.tv_usec > esp_tv.tv_usec ? this->last_msg_out_tv : esp_tv;
1491 }
1492
1493 /**
1494 * Implementation of protected_ike_sa_t.destroy.
1495 */
1496 static void destroy(private_ike_sa_t *this)
1497 {
1498 child_sa_t *child_sa;
1499
1500 this->logger->log(this->logger, CONTROL|LEVEL2, "Going to destroy IKE SA %llu:%llu, role %s",
1501 this->ike_sa_id->get_initiator_spi(this->ike_sa_id),
1502 this->ike_sa_id->get_responder_spi(this->ike_sa_id),
1503 this->ike_sa_id->is_initiator(this->ike_sa_id) ? "initiator" : "responder");
1504
1505 if (get_state(this) == IKE_SA_ESTABLISHED)
1506 {
1507 this->logger->log(this->logger, ERROR, "Destroying an established IKE SA without knowledge from remote peer!");
1508 }
1509
1510 while (this->child_sas->remove_last(this->child_sas, (void**)&child_sa) == SUCCESS)
1511 {
1512 child_sa->destroy(child_sa);
1513 }
1514 this->child_sas->destroy(this->child_sas);
1515
1516 if (this->crypter_initiator)
1517 {
1518 this->crypter_initiator->destroy(this->crypter_initiator);
1519 }
1520 if (this->crypter_responder)
1521 {
1522 this->crypter_responder->destroy(this->crypter_responder);
1523 }
1524 if (this->signer_initiator)
1525 {
1526 this->signer_initiator->destroy(this->signer_initiator);
1527 }
1528 if (this->signer_responder)
1529 {
1530 this->signer_responder->destroy(this->signer_responder);
1531 }
1532 if (this->prf)
1533 {
1534 this->prf->destroy(this->prf);
1535 }
1536 if (this->child_prf)
1537 {
1538 this->child_prf->destroy(this->child_prf);
1539 }
1540 if (this->prf_auth_i)
1541 {
1542 this->prf_auth_i->destroy(this->prf_auth_i);
1543 }
1544 if (this->prf_auth_r)
1545 {
1546 this->prf_auth_r->destroy(this->prf_auth_r);
1547 }
1548 if (this->connection)
1549 {
1550 host_t *my_host, *other_host;
1551 identification_t *my_id = NULL, *other_id = NULL;
1552 my_host = this->connection->get_my_host(this->connection);
1553 other_host = this->connection->get_other_host(this->connection);
1554 if (this->policy)
1555 {
1556 my_id = this->policy->get_my_id(this->policy);
1557 other_id = this->policy->get_other_id(this->policy);
1558 }
1559
1560 this->logger->log(this->logger, AUDIT, "IKE_SA deleted between %s[%s]...%s[%s]",
1561 my_host->get_address(my_host),
1562 my_id ? my_id->get_string(my_id) : "(unknown)",
1563 other_host->get_address(other_host),
1564 other_id ? other_id->get_string(other_id) : "(unknown)");
1565 this->connection->destroy(this->connection);
1566 }
1567 if (this->policy)
1568 {
1569 this->policy->destroy(this->policy);
1570 }
1571 if (this->last_requested_message)
1572 {
1573 this->last_requested_message->destroy(this->last_requested_message);
1574 }
1575 if (this->last_responded_message)
1576 {
1577 this->last_responded_message->destroy(this->last_responded_message);
1578 }
1579 this->nat_hasher->destroy(this->nat_hasher);
1580 this->ike_sa_id->destroy(this->ike_sa_id);
1581 this->randomizer->destroy(this->randomizer);
1582 this->current_state->destroy(this->current_state);
1583 free(this);
1584 }
1585
1586 /*
1587 * Described in header.
1588 */
1589 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
1590 {
1591 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
1592
1593 /* Public functions */
1594 this->protected.public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
1595 this->protected.public.initiate_connection = (status_t(*)(ike_sa_t*,connection_t*)) initiate_connection;
1596 this->protected.public.delete_child_sa = (status_t(*)(ike_sa_t*,u_int32_t)) delete_child_sa;
1597 this->protected.public.rekey_child_sa = (status_t(*)(ike_sa_t*,u_int32_t)) rekey_child_sa;
1598 this->protected.public.get_child_sa = (child_sa_t*(*)(ike_sa_t*,u_int32_t))get_child_sa;
1599 this->protected.public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
1600 this->protected.public.get_my_host = (host_t*(*)(ike_sa_t*)) get_my_host;
1601 this->protected.public.get_other_host = (host_t*(*)(ike_sa_t*)) get_other_host;
1602 this->protected.public.get_my_id = (identification_t*(*)(ike_sa_t*)) get_my_id;
1603 this->protected.public.get_other_id = (identification_t*(*)(ike_sa_t*)) get_other_id;
1604 this->protected.public.get_connection = (connection_t*(*)(ike_sa_t*)) get_connection;
1605 this->protected.public.retransmit_possible = (bool (*) (ike_sa_t *, u_int32_t)) retransmit_possible;
1606 this->protected.public.retransmit_request = (status_t (*) (ike_sa_t *, u_int32_t)) retransmit_request;
1607 this->protected.public.get_state = (ike_sa_state_t (*) (ike_sa_t *this)) get_state;
1608 this->protected.public.log_status = (void (*) (ike_sa_t*,logger_t*,char*))log_status;
1609 this->protected.public.delete = (status_t(*)(ike_sa_t*))delete_;
1610 this->protected.public.destroy = (void(*)(ike_sa_t*))destroy;
1611 this->protected.public.is_my_host_behind_nat = (bool(*)(ike_sa_t*)) is_my_host_behind_nat;
1612 this->protected.public.is_other_host_behind_nat = (bool(*)(ike_sa_t*)) is_other_host_behind_nat;
1613 this->protected.public.is_any_host_behind_nat = (bool(*)(ike_sa_t*)) is_any_host_behind_nat;
1614 this->protected.public.get_last_traffic_in_tv = (struct timeval (*)(ike_sa_t*)) get_last_traffic_in_tv;
1615 this->protected.public.get_last_traffic_out_tv = (struct timeval (*)(ike_sa_t*)) get_last_traffic_out_tv;
1616 this->protected.public.send_dpd_request = (status_t (*)(ike_sa_t*)) send_dpd_request;
1617
1618 /* protected functions */
1619 this->protected.build_message = (void (*) (protected_ike_sa_t *, exchange_type_t,bool,message_t**)) build_message;
1620 this->protected.get_prf = (prf_t *(*) (protected_ike_sa_t *)) get_prf;
1621 this->protected.get_child_prf = (prf_t *(*) (protected_ike_sa_t *)) get_child_prf;
1622 this->protected.get_prf_auth_i = (prf_t *(*) (protected_ike_sa_t *)) get_prf_auth_i;
1623 this->protected.get_prf_auth_r = (prf_t *(*) (protected_ike_sa_t *)) get_prf_auth_r;
1624 this->protected.add_child_sa = (void (*) (protected_ike_sa_t*,child_sa_t*)) add_child_sa;
1625 this->protected.set_connection = (void (*) (protected_ike_sa_t *,connection_t *)) set_connection;
1626 this->protected.get_connection = (connection_t *(*) (protected_ike_sa_t *)) get_connection;
1627 this->protected.set_policy = (void (*) (protected_ike_sa_t *,policy_t *)) set_policy;
1628 this->protected.get_policy = (policy_t *(*) (protected_ike_sa_t *)) get_policy;
1629 this->protected.get_randomizer = (randomizer_t *(*) (protected_ike_sa_t *)) get_randomizer;
1630 this->protected.send_request = (status_t (*) (protected_ike_sa_t *,message_t *)) send_request;
1631 this->protected.send_response = (status_t (*) (protected_ike_sa_t *,message_t *)) send_response;
1632 this->protected.send_notify = (void (*)(protected_ike_sa_t*,exchange_type_t,notify_message_type_t,chunk_t)) send_notify;
1633 this->protected.build_transforms = (status_t (*) (protected_ike_sa_t *,proposal_t*,diffie_hellman_t*,chunk_t,chunk_t)) build_transforms;
1634 this->protected.set_new_state = (void (*) (protected_ike_sa_t *,state_t *)) set_new_state;
1635 this->protected.get_crypter_initiator = (crypter_t *(*) (protected_ike_sa_t *)) get_crypter_initiator;
1636 this->protected.get_signer_initiator = (signer_t *(*) (protected_ike_sa_t *)) get_signer_initiator;
1637 this->protected.get_crypter_responder = (crypter_t *(*) (protected_ike_sa_t *)) get_crypter_responder;
1638 this->protected.get_signer_responder = (signer_t *(*) (protected_ike_sa_t *)) get_signer_responder;
1639 this->protected.reset_message_buffers = (void (*) (protected_ike_sa_t *)) reset_message_buffers;
1640 this->protected.get_last_responded_message = (message_t * (*) (protected_ike_sa_t *)) get_last_responded_message;
1641 this->protected.get_last_requested_message = (message_t * (*) (protected_ike_sa_t *)) get_last_requested_message;
1642 this->protected.set_last_replied_message_id = (void (*) (protected_ike_sa_t *,u_int32_t)) set_last_replied_message_id;
1643 this->protected.destroy_child_sa = (u_int32_t (*)(protected_ike_sa_t*,u_int32_t))destroy_child_sa;
1644 this->protected.get_child_sa = (child_sa_t* (*)(protected_ike_sa_t*,u_int32_t))get_child_sa_by_spi;
1645 this->protected.set_my_host_behind_nat = (void(*)(protected_ike_sa_t*, bool)) set_my_host_behind_nat;
1646 this->protected.set_other_host_behind_nat = (void(*)(protected_ike_sa_t*, bool)) set_other_host_behind_nat;
1647 this->protected.generate_natd_hash = (chunk_t (*) (protected_ike_sa_t *, u_int64_t, u_int64_t, host_t*)) generate_natd_hash;
1648 this->protected.get_last_dpd_message_id = (u_int32_t (*) (protected_ike_sa_t*)) get_last_dpd_message_id;
1649 this->protected.update_connection_hosts = (status_t (*) (protected_ike_sa_t *, host_t*, host_t*)) update_connection_hosts;
1650
1651 /* private functions */
1652 this->update_timestamp = (void (*) (private_ike_sa_t*,bool))update_timestamp;
1653 this->get_last_esp_traffic_tv = (struct timeval (*) (private_ike_sa_t *,bool))get_last_esp_traffic_tv;
1654
1655 /* initialize private fields */
1656 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
1657
1658 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
1659 this->child_sas = linked_list_create();
1660 this->randomizer = randomizer_create();
1661
1662 this->last_requested_message = NULL;
1663 this->last_responded_message = NULL;
1664 this->message_id_out = 0;
1665 this->message_id_in = 0;
1666 this->last_replied_message_id = -1;
1667 this->crypter_initiator = NULL;
1668 this->crypter_responder = NULL;
1669 this->signer_initiator = NULL;
1670 this->signer_responder = NULL;
1671 this->prf = NULL;
1672 this->prf_auth_i = NULL;
1673 this->prf_auth_r = NULL;
1674 this->child_prf = NULL;
1675 this->connection = NULL;
1676 this->policy = NULL;
1677 this->nat_hasher = hasher_create(HASH_SHA1);
1678 this->nat_here = FALSE;
1679 this->nat_there = FALSE;
1680 this->last_msg_in_tv.tv_sec = 0;
1681 this->last_msg_in_tv.tv_usec = 0;
1682 this->last_msg_out_tv.tv_sec = 0;
1683 this->last_msg_out_tv.tv_usec = 0;
1684 this->last_dpd_message_id = 0;
1685
1686 /* at creation time, IKE_SA is in a initiator state */
1687 if (ike_sa_id->is_initiator(ike_sa_id))
1688 {
1689 this->logger->log(this->logger, CONTROL | LEVEL2, "Create first state_t object of type INITIATOR_INIT");
1690 this->current_state = (state_t *) initiator_init_create(&(this->protected));
1691 }
1692 else
1693 {
1694 this->logger->log(this->logger, CONTROL | LEVEL2, "Create first state_t object of type RESPONDER_INIT");
1695 this->current_state = (state_t *) responder_init_create(&(this->protected));
1696 }
1697 return &(this->protected.public);
1698 }