fixed payload order
[strongswan.git] / src / charon / sa / transactions / create_child_sa.c
1 /**
2 * @file create_child_sa.c
3 *
4 * @brief Implementation of create_child_sa_t transaction.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "create_child_sa.h"
24
25 #include <string.h>
26
27 #include <daemon.h>
28 #include <encoding/payloads/sa_payload.h>
29 #include <encoding/payloads/nonce_payload.h>
30 #include <encoding/payloads/ts_payload.h>
31 #include <sa/transactions/delete_child_sa.h>
32 #include <utils/randomizer.h>
33
34
35 typedef struct private_create_child_sa_t private_create_child_sa_t;
36
37 /**
38 * Private members of a create_child_sa_t object..
39 */
40 struct private_create_child_sa_t {
41
42 /**
43 * Public methods and transaction_t interface.
44 */
45 create_child_sa_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 ike_sa_t *ike_sa;
51
52 /**
53 * Message sent by our peer, if already generated
54 */
55 message_t *message;
56
57 /**
58 * Message ID this transaction uses
59 */
60 u_int32_t message_id;
61
62 /**
63 * Times we did send the request
64 */
65 u_int32_t requested;
66
67 /**
68 * initiators inbound SPI of the CHILD_SA which gets rekeyed
69 */
70 u_int32_t rekey_spi;
71
72 /**
73 * connection of IKE_SA
74 */
75 connection_t *connection;
76
77 /**
78 * policy definition used
79 */
80 policy_t *policy;
81
82 /**
83 * Negotiated proposal used for CHILD_SA
84 */
85 proposal_t *proposal;
86
87 /**
88 * initiator chosen nonce
89 */
90 chunk_t nonce_i;
91
92 /**
93 * responder chosen nonce
94 */
95 chunk_t nonce_r;
96
97 /**
98 * Negotiated traffic selectors for initiator
99 */
100 linked_list_t *tsi;
101
102 /**
103 * Negotiated traffic selectors for responder
104 */
105 linked_list_t *tsr;
106
107 /**
108 * CHILD_SA created by this transaction
109 */
110 child_sa_t *child_sa;
111
112 /**
113 * CHILD_SA rekeyed if we are rekeying
114 */
115 child_sa_t *rekeyed_sa;
116
117 /**
118 * Have we lost the simultaneous rekeying nonce compare?
119 */
120 bool lost;
121
122 /**
123 * source of randomness
124 */
125 randomizer_t *randomizer;
126
127 /**
128 * Assigned logger.
129 */
130 logger_t *logger;
131 };
132
133 /**
134 * Implementation of transaction_t.get_message_id.
135 */
136 static u_int32_t get_message_id(private_create_child_sa_t *this)
137 {
138 return this->message_id;
139 }
140
141 /**
142 * Implementation of transaction_t.requested.
143 */
144 static u_int32_t requested(private_create_child_sa_t *this)
145 {
146 return this->requested++;
147 }
148
149 /**
150 * Implementation of create_child_sa_t.rekeys_child.
151 */
152 static void rekeys_child(private_create_child_sa_t *this, child_sa_t *child_sa)
153 {
154 this->rekeyed_sa = child_sa;
155 }
156
157 /**
158 * Implementation of create_child_sa_t.cancel.
159 */
160 static void cancel(private_create_child_sa_t *this)
161 {
162 this->rekeyed_sa = NULL;
163 this->lost = TRUE;
164 }
165
166 /**
167 * Implementation of transaction_t.get_request.
168 */
169 static status_t get_request(private_create_child_sa_t *this, message_t **result)
170 {
171 message_t *request;
172 host_t *me, *other;
173
174 /* check if we are not already rekeying */
175 if (this->rekeyed_sa &&
176 this->rekeyed_sa->get_rekeying_transaction(this->rekeyed_sa))
177 {
178 this->logger->log(this->logger, ERROR,
179 "rekeying a CHILD_SA which is already rekeying, aborted");
180 return FAILED;
181 }
182
183 /* check if we already have built a message (retransmission) */
184 if (this->message)
185 {
186 *result = this->message;
187 return SUCCESS;
188 }
189
190 this->connection = this->ike_sa->get_connection(this->ike_sa);
191 me = this->connection->get_my_host(this->connection);
192 other = this->connection->get_other_host(this->connection);
193 this->policy = this->ike_sa->get_policy(this->ike_sa);
194
195 /* build the request */
196 request = message_create();
197 request->set_source(request, me->clone(me));
198 request->set_destination(request, other->clone(other));
199 request->set_exchange_type(request, CREATE_CHILD_SA);
200 request->set_request(request, TRUE);
201 request->set_message_id(request, this->message_id);
202 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
203 *result = request;
204 this->message = request;
205
206 { /* build SA payload */
207 sa_payload_t *sa_payload;
208 linked_list_t *proposals;
209 bool use_natt;
210 u_int32_t reqid = 0;
211
212 if (this->rekeyed_sa)
213 {
214 reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
215 }
216
217 proposals = this->policy->get_proposals(this->policy);
218 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
219 this->child_sa = child_sa_create(reqid, me, other,
220 this->policy->get_soft_lifetime(this->policy),
221 this->policy->get_hard_lifetime(this->policy),
222 use_natt);
223 if (this->child_sa->alloc(this->child_sa, proposals) != SUCCESS)
224 {
225 this->logger->log(this->logger, ERROR,
226 "could not install CHILD_SA, CHILD_SA creation aborted");
227 return FAILED;
228 }
229 sa_payload = sa_payload_create_from_proposal_list(proposals);
230 request->add_payload(request, (payload_t*)sa_payload);
231 }
232
233 { /* build the NONCE payload for us (initiator) */
234 nonce_payload_t *nonce_payload;
235
236 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
237 NONCE_SIZE, &this->nonce_i) != SUCCESS)
238 {
239 return FAILED;
240 }
241 nonce_payload = nonce_payload_create();
242 nonce_payload->set_nonce(nonce_payload, this->nonce_i);
243 request->add_payload(request, (payload_t*)nonce_payload);
244 }
245
246 { /* build TSi payload */
247 linked_list_t *ts_list;
248 ts_payload_t *ts_payload;
249
250 ts_list = this->policy->get_my_traffic_selectors(this->policy);
251 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list);
252 request->add_payload(request, (payload_t*)ts_payload);
253 }
254
255 { /* build TSr payload */
256 linked_list_t *ts_list;
257 ts_payload_t *ts_payload;
258
259 ts_list = this->policy->get_other_traffic_selectors(this->policy);
260 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list);
261 request->add_payload(request, (payload_t*)ts_payload);
262 }
263
264 if (this->rekeyed_sa)
265 { /* add REKEY_SA notify if we are rekeying */
266 notify_payload_t *notify;
267 protocol_id_t protocol;
268
269 protocol = this->rekeyed_sa->get_protocol(this->rekeyed_sa);
270 notify = notify_payload_create_from_protocol_and_type(protocol, REKEY_SA);
271 notify->set_spi(notify, this->rekeyed_sa->get_spi(this->rekeyed_sa, TRUE));
272 request->add_payload(request, (payload_t*)notify);
273
274 /* register us as rekeying to detect multiple rekeying */
275 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, &this->public);
276 }
277
278 return SUCCESS;
279 }
280
281 /**
282 * Handle all kind of notifys
283 */
284 static status_t process_notifys(private_create_child_sa_t *this, notify_payload_t *notify_payload)
285 {
286 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
287
288 this->logger->log(this->logger, CONTROL|LEVEL1, "process notify type %s",
289 mapping_find(notify_type_m, notify_type));
290
291 switch (notify_type)
292 {
293 case SINGLE_PAIR_REQUIRED:
294 {
295 this->logger->log(this->logger, AUDIT,
296 "received a SINGLE_PAIR_REQUIRED notify");
297 return FAILED;
298 }
299 case TS_UNACCEPTABLE:
300 {
301 this->logger->log(this->logger, CONTROL,
302 "received TS_UNACCEPTABLE notify");
303 return FAILED;
304 }
305 case NO_PROPOSAL_CHOSEN:
306 {
307 this->logger->log(this->logger, CONTROL,
308 "received NO_PROPOSAL_CHOSEN notify");
309 return FAILED;
310 }
311 case REKEY_SA:
312 {
313 u_int32_t spi;
314 protocol_id_t protocol;
315
316 protocol = notify_payload->get_protocol_id(notify_payload);
317 switch (protocol)
318 {
319 case PROTO_AH:
320 case PROTO_ESP:
321 spi = notify_payload->get_spi(notify_payload);
322 this->rekeyed_sa = this->ike_sa->get_child_sa(this->ike_sa,
323 protocol, spi,
324 FALSE);
325 break;
326 default:
327 break;
328 }
329 return SUCCESS;
330 }
331 default:
332 {
333 if (notify_type < 16383)
334 {
335 this->logger->log(this->logger, AUDIT,
336 "received %s notify error (%d), deleting IKE_SA",
337 mapping_find(notify_type_m, notify_type),
338 notify_type);
339 return FAILED;
340 }
341 else
342 {
343 this->logger->log(this->logger, CONTROL,
344 "received %s notify (%d), ignored",
345 mapping_find(notify_type_m, notify_type),
346 notify_type);
347 return SUCCESS;
348 }
349 }
350 }
351 }
352
353 /**
354 * Build a notify message.
355 */
356 static void build_notify(notify_type_t type, message_t *message, bool flush_message)
357 {
358 notify_payload_t *notify;
359
360 if (flush_message)
361 {
362 payload_t *payload;
363 iterator_t *iterator = message->get_payload_iterator(message);
364 while (iterator->iterate(iterator, (void**)&payload))
365 {
366 payload->destroy(payload);
367 iterator->remove(iterator);
368 }
369 iterator->destroy(iterator);
370 }
371
372 notify = notify_payload_create();
373 notify->set_notify_type(notify, type);
374 message->add_payload(message, (payload_t*)notify);
375 }
376
377 /**
378 * Install a CHILD_SA for usage
379 */
380 static status_t install_child_sa(private_create_child_sa_t *this, bool initiator)
381 {
382 prf_plus_t *prf_plus;
383 chunk_t seed;
384 status_t status;
385
386 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
387 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
388 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
389 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
390 chunk_free(&seed);
391
392 if (initiator)
393 {
394 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
395 }
396 else
397 {
398 status = this->child_sa->add(this->child_sa, this->proposal, prf_plus);
399 }
400 prf_plus->destroy(prf_plus);
401 if (status != SUCCESS)
402 {
403 return DESTROY_ME;
404 }
405 if (initiator)
406 {
407 status = this->child_sa->add_policies(this->child_sa, this->tsi, this->tsr);
408 }
409 else
410 {
411 status = this->child_sa->add_policies(this->child_sa, this->tsr, this->tsi);
412 }
413 if (status != SUCCESS)
414 {
415 return DESTROY_ME;
416 }
417 /* add to IKE_SA, and remove from transaction */
418 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
419 this->child_sa = NULL;
420 return SUCCESS;
421 }
422
423 /**
424 * destroy a list of traffic selectors
425 */
426 static void destroy_ts_list(linked_list_t *list)
427 {
428 if (list)
429 {
430 traffic_selector_t *ts;
431 while (list->remove_last(list, (void**)&ts) == SUCCESS)
432 {
433 ts->destroy(ts);
434 }
435 list->destroy(list);
436 }
437 }
438
439 /**
440 * Implementation of transaction_t.get_response.
441 */
442 static status_t get_response(private_create_child_sa_t *this, message_t *request,
443 message_t **result, transaction_t **next)
444 {
445 host_t *me, *other;
446 message_t *response;
447 status_t status;
448 iterator_t *payloads;
449 sa_payload_t *sa_request = NULL;
450 nonce_payload_t *nonce_request = NULL;
451 ts_payload_t *tsi_request = NULL;
452 ts_payload_t *tsr_request = NULL;
453 nonce_payload_t *nonce_response;
454
455 /* check if we already have built a response (retransmission) */
456 if (this->message)
457 {
458 *result = this->message;
459 return SUCCESS;
460 }
461
462 this->connection = this->ike_sa->get_connection(this->ike_sa);
463 me = this->connection->get_my_host(this->connection);
464 other = this->connection->get_other_host(this->connection);
465 this->policy = this->ike_sa->get_policy(this->ike_sa);
466
467 /* set up response */
468 response = message_create();
469 response->set_source(response, me->clone(me));
470 response->set_destination(response, other->clone(other));
471 response->set_exchange_type(response, CREATE_CHILD_SA);
472 response->set_request(response, FALSE);
473 response->set_message_id(response, this->message_id);
474 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
475 this->message = response;
476 *result = response;
477
478 /* check message type */
479 if (request->get_exchange_type(request) != CREATE_CHILD_SA)
480 {
481 this->logger->log(this->logger, ERROR,
482 "CREATE_CHILD_SA response of invalid type, aborted");
483 return FAILED;
484 }
485
486 /* Iterate over all payloads. */
487 payloads = request->get_payload_iterator(request);
488 while (payloads->has_next(payloads))
489 {
490 payload_t *payload;
491 payloads->current(payloads, (void**)&payload);
492 switch (payload->get_type(payload))
493 {
494 case SECURITY_ASSOCIATION:
495 sa_request = (sa_payload_t*)payload;
496 break;
497 case NONCE:
498 nonce_request = (nonce_payload_t*)payload;
499 break;
500 case TRAFFIC_SELECTOR_INITIATOR:
501 tsi_request = (ts_payload_t*)payload;
502 break;
503 case TRAFFIC_SELECTOR_RESPONDER:
504 tsr_request = (ts_payload_t*)payload;
505 break;
506 case NOTIFY:
507 {
508 status = process_notifys(this, (notify_payload_t*)payload);
509 if (status != SUCCESS)
510 {
511 payloads->destroy(payloads);
512 return status;
513 }
514 break;
515 }
516 default:
517 {
518 this->logger->log(this->logger, ERROR, "ignoring %s payload (%d)",
519 mapping_find(payload_type_m, payload->get_type(payload)),
520 payload->get_type(payload));
521 break;
522 }
523 }
524 }
525 payloads->destroy(payloads);
526
527 /* check if we have all payloads */
528 if (!(sa_request && nonce_request && tsi_request && tsr_request))
529 {
530 build_notify(INVALID_SYNTAX, response, TRUE);
531 this->logger->log(this->logger, AUDIT,
532 "request message incomplete, no CHILD_SA created");
533 return FAILED;
534 }
535
536 { /* process nonce payload */
537 this->nonce_i = nonce_request->get_nonce(nonce_request);
538 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
539 NONCE_SIZE, &this->nonce_r) != SUCCESS)
540 {
541 build_notify(NO_PROPOSAL_CHOSEN, response, TRUE);
542 return FAILED;
543 }
544 nonce_response = nonce_payload_create();
545 nonce_response->set_nonce(nonce_response, this->nonce_r);
546 }
547
548 { /* process traffic selectors for other */
549 linked_list_t *ts_received = tsi_request->get_traffic_selectors(tsi_request);
550 this->tsi = this->policy->select_other_traffic_selectors(this->policy, ts_received);
551 destroy_ts_list(ts_received);
552 }
553
554 { /* process traffic selectors for us */
555 linked_list_t *ts_received = ts_received = tsr_request->get_traffic_selectors(tsr_request);
556 this->tsr = this->policy->select_my_traffic_selectors(this->policy, ts_received);
557 destroy_ts_list(ts_received);
558 }
559
560 { /* process SA payload */
561 proposal_t *proposal;
562 linked_list_t *proposal_list;
563 sa_payload_t *sa_response;
564 ts_payload_t *ts_response;
565 bool use_natt;
566 u_int32_t soft_lifetime, hard_lifetime;
567
568 sa_response = sa_payload_create();
569 /* get proposals from request, and select one with ours */
570 proposal_list = sa_request->get_proposals(sa_request);
571 this->logger->log(this->logger, CONTROL|LEVEL1, "selecting proposals:");
572 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
573 /* list is not needed anymore */
574 while (proposal_list->remove_last(proposal_list, (void**)&proposal) == SUCCESS)
575 {
576 proposal->destroy(proposal);
577 }
578 proposal_list->destroy(proposal_list);
579
580 /* do we have a proposal? */
581 if (this->proposal == NULL)
582 {
583 this->logger->log(this->logger, AUDIT,
584 "CHILD_SA proposals unacceptable, adding NO_PROPOSAL_CHOSEN notify");
585 build_notify(NO_PROPOSAL_CHOSEN, response, TRUE);
586 return FAILED;
587 }
588 /* do we have traffic selectors? */
589 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
590 {
591 this->logger->log(this->logger, AUDIT,
592 "CHILD_SA traffic selectors unacceptable, adding TS_UNACCEPTABLE notify");
593 build_notify(TS_UNACCEPTABLE, response, TRUE);
594 return FAILED;
595 }
596 else
597 { /* create child sa */
598 u_int32_t reqid = 0;
599
600 if (this->rekeyed_sa)
601 {
602 reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
603 }
604 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
605 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
606 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
607 this->child_sa = child_sa_create(reqid, me, other,
608 soft_lifetime, hard_lifetime,
609 use_natt);
610 if (install_child_sa(this, FALSE) != SUCCESS)
611 {
612 this->logger->log(this->logger, ERROR,
613 "installing CHILD_SA failed, adding NO_PROPOSAL_CHOSEN notify");
614 build_notify(NO_PROPOSAL_CHOSEN, response, TRUE);
615 return FAILED;
616 }
617 /* add proposal to sa payload */
618 sa_response->add_proposal(sa_response, this->proposal);
619 }
620 response->add_payload(response, (payload_t*)sa_response);
621
622 /* add nonce/ts payload after sa payload */
623 response->add_payload(response, (payload_t *)nonce_response);
624 ts_response = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
625 response->add_payload(response, (payload_t*)ts_response);
626 ts_response = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
627 response->add_payload(response, (payload_t*)ts_response);
628 }
629 /* CHILD_SA successfully created. We set us as the rekeying transaction of
630 * the rekeyed SA. If we already initiated rekeying of the same SA, we will detect
631 * this later in the conclude() call. */
632 if (this->rekeyed_sa)
633 {
634 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, &this->public);
635 }
636 return SUCCESS;
637 }
638
639 /**
640 * Implementation of transaction_t.conclude
641 */
642 static status_t conclude(private_create_child_sa_t *this, message_t *response,
643 transaction_t **next)
644 {
645 iterator_t *payloads;
646 host_t *me, *other;
647 sa_payload_t *sa_payload = NULL;
648 nonce_payload_t *nonce_payload = NULL;
649 ts_payload_t *tsi_payload = NULL;
650 ts_payload_t *tsr_payload = NULL;
651 status_t status;
652 child_sa_t *new_child = NULL;
653 delete_child_sa_t *delete_child_sa;
654
655 /* check message type */
656 if (response->get_exchange_type(response) != CREATE_CHILD_SA)
657 {
658 this->logger->log(this->logger, ERROR,
659 "CREATE_CHILD_SA response of invalid type, aborting");
660 return FAILED;
661 }
662
663 me = this->connection->get_my_host(this->connection);
664 other = this->connection->get_other_host(this->connection);
665
666 /* Iterate over all payloads to collect them */
667 payloads = response->get_payload_iterator(response);
668 while (payloads->has_next(payloads))
669 {
670 payload_t *payload;
671 payloads->current(payloads, (void**)&payload);
672 switch (payload->get_type(payload))
673 {
674 case SECURITY_ASSOCIATION:
675 sa_payload = (sa_payload_t*)payload;
676 break;
677 case NONCE:
678 nonce_payload = (nonce_payload_t*)payload;
679 break;
680 case TRAFFIC_SELECTOR_INITIATOR:
681 tsi_payload = (ts_payload_t*)payload;
682 break;
683 case TRAFFIC_SELECTOR_RESPONDER:
684 tsr_payload = (ts_payload_t*)payload;
685 break;
686 case NOTIFY:
687 {
688 status = process_notifys(this, (notify_payload_t*)payload);
689 if (status != SUCCESS)
690 {
691 payloads->destroy(payloads);
692 return status;
693 }
694 break;
695 }
696 default:
697 {
698 this->logger->log(this->logger, ERROR, "ignoring %s payload (%d)",
699 mapping_find(payload_type_m, payload->get_type(payload)),
700 payload->get_type(payload));
701 break;
702 }
703 }
704 }
705 payloads->destroy(payloads);
706
707 if (!(sa_payload && nonce_payload && tsi_payload && tsr_payload))
708 {
709 this->logger->log(this->logger, AUDIT, "response message incomplete, no CHILD_SA built");
710 return FAILED;
711 }
712
713 { /* process NONCE payload */
714 this->nonce_r = nonce_payload->get_nonce(nonce_payload);
715 }
716
717 { /* process traffic selectors for us */
718 linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload);
719 this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received);
720 destroy_ts_list(ts_received);
721 }
722
723 { /* process traffic selectors for other */
724 linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload);
725 this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received);
726 destroy_ts_list(ts_received);
727 }
728
729 { /* process sa payload */
730 proposal_t *proposal;
731 linked_list_t *proposal_list;
732
733 proposal_list = sa_payload->get_proposals(sa_payload);
734 /* we have to re-check here if other's selection is valid */
735 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
736 /* list not needed anymore */
737 while (proposal_list->remove_last(proposal_list, (void**)&proposal) == SUCCESS)
738 {
739 proposal->destroy(proposal);
740 }
741 proposal_list->destroy(proposal_list);
742
743 /* everything fine to create CHILD? */
744 if (this->proposal == NULL ||
745 this->tsi->get_count(this->tsi) == 0 ||
746 this->tsr->get_count(this->tsr) == 0)
747 {
748 this->logger->log(this->logger, AUDIT,
749 "CHILD_SA creation failed");
750 return FAILED;
751 }
752 new_child = this->child_sa;
753 if (install_child_sa(this, TRUE) != SUCCESS)
754 {
755 this->logger->log(this->logger, ERROR,
756 "installing CHILD_SA failed, no CHILD_SA built");
757 return FAILED;
758 }
759 }
760 /* CHILD_SA successfully created. If the other peer initiated rekeying
761 * in the meantime, we detect this by comparing the rekeying_transaction
762 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
763 * If no simultaneous rekeying is going on, we just initiate the delete of
764 * the superseded SA. */
765 if (this->rekeyed_sa)
766 {
767 private_create_child_sa_t *other;
768
769 other = (private_create_child_sa_t*)
770 this->rekeyed_sa->get_rekeying_transaction(this->rekeyed_sa);
771
772 /* rekeying finished, update SA status */
773 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, NULL);
774
775 if (other != this)
776 { /* simlutaneous rekeying is going on, not so good */
777 chunk_t this_lowest, other_lowest;
778
779 /* check if this has a lower nonce than other */
780 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
781 min(this->nonce_i.len, this->nonce_r.len)) < 0)
782 {
783 this_lowest = this->nonce_i;
784 }
785 else
786 {
787 this_lowest = this->nonce_r;
788 }
789 if (memcmp(other->nonce_i.ptr, other->nonce_r.ptr,
790 min(other->nonce_i.len, other->nonce_r.len)) < 0)
791 {
792 other_lowest = other->nonce_i;
793 }
794 else
795 {
796 other_lowest = other->nonce_r;
797 }
798 if (memcmp(this_lowest.ptr, other_lowest.ptr,
799 min(this_lowest.len, other_lowest.len)) < 0)
800 {
801 this->logger->log(this->logger, ERROR,
802 "detected simultaneous CHILD_SA rekeying, but ours is preferred");
803 }
804 else
805 {
806
807 this->logger->log(this->logger, ERROR,
808 "detected simultaneous CHILD_SA rekeying, deleting ours");
809 this->lost = TRUE;
810 }
811 }
812 /* delete the old SA if we have won the rekeying nonce compare*/
813 if (!this->lost)
814 {
815 delete_child_sa = delete_child_sa_create(this->ike_sa, this->message_id + 1);
816 delete_child_sa->set_child_sa(delete_child_sa, this->rekeyed_sa);
817 *next = (transaction_t*)delete_child_sa;
818 }
819 }
820 if (this->lost)
821 {
822 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
823 delete_child_sa = delete_child_sa_create(this->ike_sa, this->message_id + 1);
824 delete_child_sa->set_child_sa(delete_child_sa, new_child);
825 *next = (transaction_t*)delete_child_sa;
826 }
827 return SUCCESS;
828 }
829
830 /**
831 * implements transaction_t.destroy
832 */
833 static void destroy(private_create_child_sa_t *this)
834 {
835 if (this->message)
836 {
837 this->message->destroy(this->message);
838 }
839 if (this->proposal)
840 {
841 this->proposal->destroy(this->proposal);
842 }
843 if (this->child_sa)
844 {
845 this->child_sa->destroy(this->child_sa);
846 }
847 destroy_ts_list(this->tsi);
848 destroy_ts_list(this->tsr);
849 chunk_free(&this->nonce_i);
850 chunk_free(&this->nonce_r);
851 this->randomizer->destroy(this->randomizer);
852 free(this);
853 }
854
855 /*
856 * Described in header.
857 */
858 create_child_sa_t *create_child_sa_create(ike_sa_t *ike_sa, u_int32_t message_id)
859 {
860 private_create_child_sa_t *this = malloc_thing(private_create_child_sa_t);
861
862 /* transaction interface functions */
863 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
864 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
865 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
866 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
867 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
868 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
869
870 /* public functions */
871 this->public.rekeys_child = (void(*)(create_child_sa_t*,child_sa_t*))rekeys_child;
872 this->public.cancel = (void(*)(create_child_sa_t*))cancel;
873
874 /* private data */
875 this->ike_sa = ike_sa;
876 this->message_id = message_id;
877 this->message = NULL;
878 this->requested = 0;
879 this->rekey_spi = 0;
880 this->nonce_i = CHUNK_INITIALIZER;
881 this->nonce_r = CHUNK_INITIALIZER;
882 this->child_sa = NULL;
883 this->rekeyed_sa = NULL;
884 this->lost = FALSE;
885 this->proposal = NULL;
886 this->tsi = NULL;
887 this->tsr = NULL;
888 this->randomizer = randomizer_create();
889 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
890
891 return &this->public;
892 }