dab6bf1785c418babf89a882ed80c16938b2a249
[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 * reqid to use for new CHILD_SA
74 */
75 u_int32_t reqid;
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 * lower of the nonces of a simultaneus rekeying request
99 */
100 chunk_t nonce_s;
101
102 /**
103 * Negotiated traffic selectors for initiator
104 */
105 linked_list_t *tsi;
106
107 /**
108 * Negotiated traffic selectors for responder
109 */
110 linked_list_t *tsr;
111
112 /**
113 * CHILD_SA created by this transaction
114 */
115 child_sa_t *child_sa;
116
117 /**
118 * CHILD_SA rekeyed if we are rekeying
119 */
120 child_sa_t *rekeyed_sa;
121
122 /**
123 * Have we lost the simultaneous rekeying nonce compare?
124 */
125 bool lost;
126
127 /**
128 * source of randomness
129 */
130 randomizer_t *randomizer;
131
132 /**
133 * signal to emit when transaction fails. As this transaction is used
134 * for CHILD_SA creation AND rekeying, we must emit different signals.
135 */
136 signal_t failsig;
137 };
138
139 /**
140 * Implementation of transaction_t.get_message_id.
141 */
142 static u_int32_t get_message_id(private_create_child_sa_t *this)
143 {
144 return this->message_id;
145 }
146
147 /**
148 * Implementation of transaction_t.requested.
149 */
150 static u_int32_t requested(private_create_child_sa_t *this)
151 {
152 return this->requested++;
153 }
154
155 /**
156 * Implementation of create_child_sa_t.set_policy.
157 */
158 static void set_policy(private_create_child_sa_t *this, policy_t *policy)
159 {
160 this->policy = policy;
161 }
162
163 /**
164 * Implementation of create_child_sa_t.set_reqid.
165 */
166 static void set_reqid(private_create_child_sa_t *this, u_int32_t reqid)
167 {
168 this->reqid = reqid;
169 }
170
171 /**
172 * Implementation of create_child_sa_t.rekeys_child.
173 */
174 static void rekeys_child(private_create_child_sa_t *this, child_sa_t *child_sa)
175 {
176 this->rekeyed_sa = child_sa;
177 this->failsig = CHILD_REKEY_FAILED;
178 }
179
180 /**
181 * Implementation of create_child_sa_t.cancel.
182 */
183 static void cancel(private_create_child_sa_t *this)
184 {
185 this->rekeyed_sa = NULL;
186 this->lost = TRUE;
187 }
188
189 /**
190 * Implementation of transaction_t.get_request.
191 */
192 static status_t get_request(private_create_child_sa_t *this, message_t **result)
193 {
194 message_t *request;
195 host_t *me, *other;
196 identification_t *my_id, *other_id;
197
198 /* check if we already have built a message (retransmission) */
199 if (this->message)
200 {
201 *result = this->message;
202 return SUCCESS;
203 }
204
205 /* check if we are not already rekeying */
206 if (this->rekeyed_sa)
207 {
208 SIG(CHILD_REKEY_START, "rekeying CHILD_SA");
209
210 switch (this->rekeyed_sa->get_state(this->rekeyed_sa))
211 {
212 case CHILD_REKEYING:
213 SIG(CHILD_REKEY_FAILED,
214 "rekeying a CHILD_SA which is already rekeying, aborted");
215 return FAILED;
216 case CHILD_DELETING:
217 SIG(CHILD_REKEY_FAILED,
218 "rekeying a CHILD_SA which is deleting, aborted");
219 return FAILED;
220 default:
221 break;
222 }
223 this->rekeyed_sa->set_state(this->rekeyed_sa, CHILD_REKEYING);
224 }
225 else
226 {
227 SIG(CHILD_UP_START, "creating CHILD_SA");
228 }
229
230 me = this->ike_sa->get_my_host(this->ike_sa);
231 other = this->ike_sa->get_other_host(this->ike_sa);
232 my_id = this->ike_sa->get_my_id(this->ike_sa);
233 other_id = this->ike_sa->get_other_id(this->ike_sa);
234
235 /* build the request */
236 request = message_create();
237 request->set_source(request, me->clone(me));
238 request->set_destination(request, other->clone(other));
239 request->set_exchange_type(request, CREATE_CHILD_SA);
240 request->set_request(request, TRUE);
241 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
242 *result = request;
243 this->message = request;
244
245 { /* build SA payload */
246 sa_payload_t *sa_payload;
247 linked_list_t *proposals;
248 bool use_natt;
249
250 /* get a policy, if we are rekeying */
251 if (this->rekeyed_sa)
252 {
253 linked_list_t *my_ts, *other_ts;
254 identification_t *my_id, *other_id;
255
256 my_ts = this->rekeyed_sa->get_my_traffic_selectors(this->rekeyed_sa);
257 other_ts = this->rekeyed_sa->get_other_traffic_selectors(this->rekeyed_sa);
258 my_id = this->ike_sa->get_my_id(this->ike_sa);
259 other_id = this->ike_sa->get_other_id(this->ike_sa);
260
261 this->policy = charon->policies->get_policy(charon->policies,
262 my_id, other_id,
263 my_ts, other_ts,
264 me, other);
265
266 this->reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
267
268 if (this->policy == NULL)
269 {
270 SIG(IKE_REKEY_FAILED, "no policy found to rekey "
271 "CHILD_SA with reqid %d", this->reqid);
272 return FAILED;
273 }
274 }
275
276 proposals = this->policy->get_proposals(this->policy);
277 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
278 this->child_sa = child_sa_create(this->reqid, me, other, my_id, other_id,
279 this->policy->get_soft_lifetime(this->policy),
280 this->policy->get_hard_lifetime(this->policy),
281 this->policy->get_updown(this->policy),
282 this->policy->get_hostaccess(this->policy),
283 use_natt);
284 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
285 if (this->child_sa->alloc(this->child_sa, proposals) != SUCCESS)
286 {
287 SIG(this->failsig, "could not install CHILD_SA, CHILD_SA creation failed");
288 return FAILED;
289 }
290 sa_payload = sa_payload_create_from_proposal_list(proposals);
291 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
292 request->add_payload(request, (payload_t*)sa_payload);
293 }
294
295 { /* build the NONCE payload for us (initiator) */
296 nonce_payload_t *nonce_payload;
297
298 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
299 NONCE_SIZE, &this->nonce_i) != SUCCESS)
300 {
301 SIG(this->failsig, "could not create nonce, CHILD_SA creation failed");
302 return FAILED;
303 }
304 nonce_payload = nonce_payload_create();
305 nonce_payload->set_nonce(nonce_payload, this->nonce_i);
306 request->add_payload(request, (payload_t*)nonce_payload);
307 }
308
309 { /* build TSi payload */
310 linked_list_t *ts_list;
311 ts_payload_t *ts_payload;
312
313 ts_list = this->policy->get_my_traffic_selectors(this->policy, me);
314 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list);
315 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
316 request->add_payload(request, (payload_t*)ts_payload);
317 }
318
319 { /* build TSr payload */
320 linked_list_t *ts_list;
321 ts_payload_t *ts_payload;
322
323 ts_list = this->policy->get_other_traffic_selectors(this->policy, other);
324 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list);
325 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
326 request->add_payload(request, (payload_t*)ts_payload);
327 }
328
329 if (this->rekeyed_sa)
330 { /* add REKEY_SA notify if we are rekeying */
331 notify_payload_t *notify;
332 protocol_id_t protocol;
333
334 protocol = this->rekeyed_sa->get_protocol(this->rekeyed_sa);
335 notify = notify_payload_create_from_protocol_and_type(protocol, REKEY_SA);
336 notify->set_spi(notify, this->rekeyed_sa->get_spi(this->rekeyed_sa, TRUE));
337 request->add_payload(request, (payload_t*)notify);
338
339 /* register us as rekeying to detect multiple rekeying */
340 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, &this->public);
341 }
342
343 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
344 request->set_message_id(request, this->message_id);
345
346 return SUCCESS;
347 }
348
349 /**
350 * Handle all kind of notifys
351 */
352 static status_t process_notifys(private_create_child_sa_t *this, notify_payload_t *notify_payload)
353 {
354 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
355
356 DBG2(DBG_IKE, "process notify type %N", notify_type_names, notify_type);
357
358 switch (notify_type)
359 {
360 case SINGLE_PAIR_REQUIRED:
361 {
362 SIG(this->failsig, "received a SINGLE_PAIR_REQUIRED notify");
363 return FAILED;
364 }
365 case TS_UNACCEPTABLE:
366 {
367 SIG(this->failsig, "received TS_UNACCEPTABLE notify");
368 return FAILED;
369 }
370 case NO_PROPOSAL_CHOSEN:
371 {
372 SIG(this->failsig, "received NO_PROPOSAL_CHOSEN notify");
373 return FAILED;
374 }
375 case REKEY_SA:
376 {
377 u_int32_t spi;
378 protocol_id_t protocol;
379
380 protocol = notify_payload->get_protocol_id(notify_payload);
381 switch (protocol)
382 {
383 case PROTO_AH:
384 case PROTO_ESP:
385 spi = notify_payload->get_spi(notify_payload);
386 this->rekeyed_sa = this->ike_sa->get_child_sa(this->ike_sa,
387 protocol, spi,
388 FALSE);
389 this->failsig = CHILD_REKEY_FAILED;
390 break;
391 default:
392 break;
393 }
394 return SUCCESS;
395 }
396 default:
397 {
398 if (notify_type < 16383)
399 {
400 SIG(this->failsig, "received %N notify error, CHILD_SA "
401 "creation failed", notify_type_names, notify_type);
402 return FAILED;
403 }
404 else
405 {
406 DBG1(DBG_IKE, "received %N notify, ignored",
407 notify_type_names, notify_type);
408 return SUCCESS;
409 }
410 }
411 }
412 }
413
414 /**
415 * Build a notify message.
416 */
417 static void build_notify(notify_type_t type, chunk_t data, message_t *message, bool flush_message)
418 {
419 notify_payload_t *notify;
420
421 if (flush_message)
422 {
423 payload_t *payload;
424 iterator_t *iterator = message->get_payload_iterator(message);
425 while (iterator->iterate(iterator, (void**)&payload))
426 {
427 payload->destroy(payload);
428 iterator->remove(iterator);
429 }
430 iterator->destroy(iterator);
431 }
432
433 notify = notify_payload_create();
434 notify->set_notify_type(notify, type);
435 notify->set_notification_data(notify, data);
436 message->add_payload(message, (payload_t*)notify);
437 }
438
439 /**
440 * Install a CHILD_SA for usage
441 */
442 static status_t install_child_sa(private_create_child_sa_t *this, bool initiator)
443 {
444 prf_plus_t *prf_plus;
445 chunk_t seed;
446 status_t status;
447
448 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
449 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
450 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
451 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
452 chunk_free(&seed);
453
454 if (initiator)
455 {
456 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
457 }
458 else
459 {
460 status = this->child_sa->add(this->child_sa, this->proposal, prf_plus);
461 }
462 prf_plus->destroy(prf_plus);
463 if (status != SUCCESS)
464 {
465 return DESTROY_ME;
466 }
467 if (initiator)
468 {
469 status = this->child_sa->add_policies(this->child_sa, this->tsi, this->tsr);
470 }
471 else
472 {
473 status = this->child_sa->add_policies(this->child_sa, this->tsr, this->tsi);
474 }
475 if (status != SUCCESS)
476 {
477 return DESTROY_ME;
478 }
479
480 /* add to IKE_SA, and remove from transaction */
481 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
482 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
483 this->child_sa = NULL;
484 return SUCCESS;
485 }
486
487 /**
488 * Implementation of transaction_t.get_response.
489 */
490 static status_t get_response(private_create_child_sa_t *this, message_t *request,
491 message_t **result, transaction_t **next)
492 {
493 host_t *me, *other;
494 identification_t *my_id, *other_id;
495 message_t *response;
496 status_t status;
497 iterator_t *payloads;
498 payload_t *payload;
499 sa_payload_t *sa_request = NULL;
500 nonce_payload_t *nonce_request = NULL;
501 ts_payload_t *tsi_request = NULL;
502 ts_payload_t *tsr_request = NULL;
503 nonce_payload_t *nonce_response;
504
505 /* check if we already have built a response (retransmission) */
506 if (this->message)
507 {
508 *result = this->message;
509 return SUCCESS;
510 }
511
512 me = this->ike_sa->get_my_host(this->ike_sa);
513 other = this->ike_sa->get_other_host(this->ike_sa);
514 my_id = this->ike_sa->get_my_id(this->ike_sa);
515 other_id = this->ike_sa->get_other_id(this->ike_sa);
516 this->message_id = request->get_message_id(request);
517
518 /* set up response */
519 response = message_create();
520 response->set_source(response, me->clone(me));
521 response->set_destination(response, other->clone(other));
522 response->set_exchange_type(response, CREATE_CHILD_SA);
523 response->set_request(response, FALSE);
524 response->set_message_id(response, this->message_id);
525 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
526 this->message = response;
527 *result = response;
528
529 /* check message type */
530 if (request->get_exchange_type(request) != CREATE_CHILD_SA)
531 {
532 DBG1(DBG_IKE, "CREATE_CHILD_SA response of invalid type, aborted");
533 return FAILED;
534 }
535
536 /* we do not allow the creation of new CHILDren/rekeying when IKE_SA is
537 * rekeying */
538 if (this->ike_sa->get_state(this->ike_sa) == IKE_REKEYING ||
539 this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
540 {
541 build_notify(NO_ADDITIONAL_SAS, CHUNK_INITIALIZER, response, TRUE);
542 DBG1(DBG_IKE, "unable to create new CHILD_SAs, as rekeying in progress");
543 return FAILED;
544 }
545
546 /* Iterate over all payloads. */
547 payloads = request->get_payload_iterator(request);
548 while (payloads->iterate(payloads, (void**)&payload))
549 {
550 switch (payload->get_type(payload))
551 {
552 case SECURITY_ASSOCIATION:
553 sa_request = (sa_payload_t*)payload;
554 break;
555 case NONCE:
556 nonce_request = (nonce_payload_t*)payload;
557 break;
558 case TRAFFIC_SELECTOR_INITIATOR:
559 tsi_request = (ts_payload_t*)payload;
560 break;
561 case TRAFFIC_SELECTOR_RESPONDER:
562 tsr_request = (ts_payload_t*)payload;
563 break;
564 case KEY_EXCHANGE:
565 {
566 u_int8_t dh_buffer[] = {0x00, 0x00}; /* MODP_NONE */
567 chunk_t group = chunk_from_buf(dh_buffer);
568 build_notify(INVALID_KE_PAYLOAD, group, response, TRUE);
569 DBG1(DBG_IKE, "CREATE_CHILD_SA used PFS, sending INVALID_KE_PAYLOAD");
570 return FAILED;
571 }
572 case NOTIFY:
573 {
574 status = process_notifys(this, (notify_payload_t*)payload);
575 if (status != SUCCESS)
576 {
577 payloads->destroy(payloads);
578 return status;
579 }
580 break;
581 }
582 default:
583 {
584 DBG1(DBG_IKE, "ignoring %N payload",
585 payload_type_names, payload->get_type(payload));
586 break;
587 }
588 }
589 }
590 payloads->destroy(payloads);
591
592 /* after processing the notify payloads, we know if this transaction is
593 * for rekeying or for a new CHILD_SA. We can emit the signals now. */
594 if (this->rekeyed_sa)
595 {
596 SIG(CHILD_REKEY_START, "rekeying CHILD_SA");
597 }
598 else
599 {
600 SIG(CHILD_UP_START, "creating CHILD_SA");
601 }
602
603 /* check if we have all payloads */
604 if (!(sa_request && nonce_request && tsi_request && tsr_request))
605 {
606 build_notify(INVALID_SYNTAX, CHUNK_INITIALIZER, response, TRUE);
607 SIG(this->failsig, "request message incomplete, no CHILD_SA created");
608 return FAILED;
609 }
610
611 { /* process nonce payload */
612 this->nonce_i = nonce_request->get_nonce(nonce_request);
613 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
614 NONCE_SIZE, &this->nonce_r) != SUCCESS)
615 {
616 build_notify(NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER, response, TRUE);
617 SIG(this->failsig, "nonce generation failed, no CHILD_SA created");
618 return FAILED;
619 }
620 nonce_response = nonce_payload_create();
621 nonce_response->set_nonce(nonce_response, this->nonce_r);
622 }
623
624 { /* get a policy and process traffic selectors */
625 identification_t *my_id, *other_id;
626 linked_list_t *my_ts, *other_ts;
627
628 my_id = this->ike_sa->get_my_id(this->ike_sa);
629 other_id = this->ike_sa->get_other_id(this->ike_sa);
630
631 my_ts = tsr_request->get_traffic_selectors(tsr_request);
632 other_ts = tsi_request->get_traffic_selectors(tsi_request);
633
634 this->policy = charon->policies->get_policy(charon->policies,
635 my_id, other_id,
636 my_ts, other_ts,
637 me, other);
638 if (this->policy)
639 {
640 this->tsr = this->policy->select_my_traffic_selectors(this->policy, my_ts, me);
641 this->tsi = this->policy->select_other_traffic_selectors(this->policy, other_ts, other);
642 }
643 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
644 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
645
646 if (this->policy == NULL)
647 {
648 SIG(this->failsig, "no acceptable policy found, sending TS_UNACCEPTABLE notify");
649 build_notify(TS_UNACCEPTABLE, CHUNK_INITIALIZER, response, TRUE);
650 return FAILED;
651 }
652 }
653
654 { /* process SA payload */
655 linked_list_t *proposal_list;
656 sa_payload_t *sa_response;
657 ts_payload_t *ts_response;
658 bool use_natt;
659 u_int32_t soft_lifetime, hard_lifetime;
660
661 sa_response = sa_payload_create();
662 /* get proposals from request, and select one with ours */
663 proposal_list = sa_request->get_proposals(sa_request);
664 DBG2(DBG_IKE, "selecting proposals:");
665 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
666 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
667
668 /* do we have a proposal? */
669 if (this->proposal == NULL)
670 {
671 SIG(this->failsig, "CHILD_SA proposals unacceptable, sending NO_PROPOSAL_CHOSEN notify");
672 build_notify(NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER, response, TRUE);
673 return FAILED;
674 }
675 /* do we have traffic selectors? */
676 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
677 {
678 SIG(this->failsig, "CHILD_SA traffic selectors unacceptable, sending TS_UNACCEPTABLE notify");
679 build_notify(TS_UNACCEPTABLE, CHUNK_INITIALIZER, response, TRUE);
680 return FAILED;
681 }
682 else
683 { /* create child sa */
684 if (this->rekeyed_sa)
685 {
686 this->reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
687 }
688 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
689 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
690 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
691 this->child_sa = child_sa_create(this->reqid, me, other, my_id, other_id,
692 soft_lifetime, hard_lifetime,
693 this->policy->get_updown(this->policy),
694 this->policy->get_hostaccess(this->policy),
695 use_natt);
696 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
697 if (install_child_sa(this, FALSE) != SUCCESS)
698 {
699 SIG(this->failsig, "installing CHILD_SA failed, sending NO_PROPOSAL_CHOSEN notify");
700 build_notify(NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER, response, TRUE);
701 return FAILED;
702 }
703 /* add proposal to sa payload */
704 sa_response->add_proposal(sa_response, this->proposal);
705 }
706 response->add_payload(response, (payload_t*)sa_response);
707
708 /* add nonce/ts payload after sa payload */
709 response->add_payload(response, (payload_t *)nonce_response);
710 ts_response = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
711 response->add_payload(response, (payload_t*)ts_response);
712 ts_response = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
713 response->add_payload(response, (payload_t*)ts_response);
714 }
715 /* CHILD_SA successfully created. If another transaction is already rekeying
716 * this SA, our lower nonce must be registered for a later nonce compare. */
717 if (this->rekeyed_sa)
718 {
719 private_create_child_sa_t *other;
720
721 other = this->rekeyed_sa->get_rekeying_transaction(this->rekeyed_sa);
722 if (other)
723 {
724 /* store our lower nonce in the simultaneus transaction, it
725 * will later compare it against his nonces when it calls conclude().
726 */
727 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
728 min(this->nonce_i.len, this->nonce_r.len)) < 0)
729 {
730 other->nonce_s = chunk_clone(this->nonce_i);
731 }
732 else
733 {
734 other->nonce_s = chunk_clone(this->nonce_r);
735 }
736 }
737 else
738 {
739 /* we only signal when no other transaction is rekeying */
740 SIG(CHILD_REKEY_SUCCESS, "CHILD_SA rekeyed");
741 }
742 this->rekeyed_sa->set_state(this->rekeyed_sa, CHILD_REKEYING);
743 }
744 else
745 {
746 SIG(CHILD_UP_SUCCESS, "CHILD_SA created");
747 }
748 return SUCCESS;
749 }
750
751 /**
752 * Implementation of transaction_t.conclude
753 */
754 static status_t conclude(private_create_child_sa_t *this, message_t *response,
755 transaction_t **next)
756 {
757 iterator_t *payloads;
758 payload_t *payload;
759 host_t *me, *other;
760 sa_payload_t *sa_payload = NULL;
761 nonce_payload_t *nonce_payload = NULL;
762 ts_payload_t *tsi_payload = NULL;
763 ts_payload_t *tsr_payload = NULL;
764 status_t status;
765 child_sa_t *new_child = NULL;
766 delete_child_sa_t *delete_child_sa;
767
768 /* check message type */
769 if (response->get_exchange_type(response) != CREATE_CHILD_SA)
770 {
771 SIG(this->failsig, "CREATE_CHILD_SA response of invalid type, aborting");
772 return FAILED;
773 }
774
775 me = this->ike_sa->get_my_host(this->ike_sa);
776 other = this->ike_sa->get_other_host(this->ike_sa);
777
778 /* Iterate over all payloads to collect them */
779 payloads = response->get_payload_iterator(response);
780 while (payloads->iterate(payloads, (void**)&payload))
781 {
782 switch (payload->get_type(payload))
783 {
784 case SECURITY_ASSOCIATION:
785 sa_payload = (sa_payload_t*)payload;
786 break;
787 case NONCE:
788 nonce_payload = (nonce_payload_t*)payload;
789 break;
790 case TRAFFIC_SELECTOR_INITIATOR:
791 tsi_payload = (ts_payload_t*)payload;
792 break;
793 case TRAFFIC_SELECTOR_RESPONDER:
794 tsr_payload = (ts_payload_t*)payload;
795 break;
796 case NOTIFY:
797 {
798 status = process_notifys(this, (notify_payload_t*)payload);
799 if (status != SUCCESS)
800 {
801 payloads->destroy(payloads);
802 return status;
803 }
804 break;
805 }
806 default:
807 {
808 DBG1(DBG_IKE, "ignoring %N payload",
809 payload_type_names, payload->get_type(payload));
810 break;
811 }
812 }
813 }
814 payloads->destroy(payloads);
815
816 if (!(sa_payload && nonce_payload && tsi_payload && tsr_payload))
817 {
818 SIG(this->failsig, "response message incomplete, no CHILD_SA built");
819 return FAILED;
820 }
821
822 { /* process NONCE payload */
823 this->nonce_r = nonce_payload->get_nonce(nonce_payload);
824 }
825
826 { /* process traffic selectors for us */
827 linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload);
828 this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received, me);
829 ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy));
830 }
831
832 { /* process traffic selectors for other */
833 linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload);
834 this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received, other);
835 ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy));
836 }
837
838 { /* process sa payload */
839 linked_list_t *proposal_list;
840
841 proposal_list = sa_payload->get_proposals(sa_payload);
842 /* we have to re-check here if other's selection is valid */
843 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
844 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
845
846 /* everything fine to create CHILD? */
847 if (this->proposal == NULL ||
848 this->tsi->get_count(this->tsi) == 0 ||
849 this->tsr->get_count(this->tsr) == 0)
850 {
851 SIG(this->failsig, "CHILD_SA negotiation failed, no CHILD_SA built");
852 return FAILED;
853 }
854 new_child = this->child_sa;
855 if (install_child_sa(this, TRUE) != SUCCESS)
856 {
857 SIG(this->failsig, "installing CHILD_SA failed, no CHILD_SA built");
858 return FAILED;
859 }
860 }
861 /* CHILD_SA successfully created. If the other peer initiated rekeying
862 * in the meantime, we detect this by comparing the rekeying_transaction
863 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
864 * If no simultaneous rekeying is going on, we just initiate the delete of
865 * the superseded SA. */
866 if (this->rekeyed_sa)
867 {
868 /* rekeying finished, update SA status */
869 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, NULL);
870
871 if (this->nonce_s.ptr)
872 { /* simlutaneous rekeying is going on, not so good */
873 chunk_t this_lowest;
874
875 /* first get our lowest nonce */
876 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
877 min(this->nonce_i.len, this->nonce_r.len)) < 0)
878 {
879 this_lowest = this->nonce_i;
880 }
881 else
882 {
883 this_lowest = this->nonce_r;
884 }
885 /* then compare against other lowest nonce */
886 if (memcmp(this_lowest.ptr, this->nonce_s.ptr,
887 min(this_lowest.len, this->nonce_s.len)) < 0)
888 {
889 DBG1(DBG_IKE, "detected simultaneous CHILD_SA rekeying, deleting ours");
890 this->lost = TRUE;
891 }
892 else
893 {
894 DBG1(DBG_IKE, "detected simultaneous CHILD_SA rekeying, but ours is preferred");
895 }
896 }
897 /* delete the old SA if we have won the rekeying nonce compare*/
898 if (!this->lost)
899 {
900 delete_child_sa = delete_child_sa_create(this->ike_sa);
901 delete_child_sa->set_child_sa(delete_child_sa, this->rekeyed_sa);
902 *next = (transaction_t*)delete_child_sa;
903 }
904 /* we send a rekey SUCCESS signal in any case. If the other transaction
905 * detected our transaction, it did not send a signal. We do it for it. */
906 SIG(CHILD_REKEY_SUCCESS, "CHILD_SA rekeyed");
907 }
908 else
909 {
910 SIG(CHILD_UP_SUCCESS, "CHILD_SA created");
911 }
912 if (this->lost)
913 {
914 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
915 delete_child_sa = delete_child_sa_create(this->ike_sa);
916 delete_child_sa->set_child_sa(delete_child_sa, new_child);
917 *next = (transaction_t*)delete_child_sa;
918 }
919 return SUCCESS;
920 }
921
922 /**
923 * implements transaction_t.destroy
924 */
925 static void destroy(private_create_child_sa_t *this)
926 {
927 DESTROY_IF(this->message);
928 DESTROY_IF(this->proposal);
929 DESTROY_IF(this->child_sa);
930 DESTROY_IF(this->policy);
931 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
932 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
933 chunk_free(&this->nonce_i);
934 chunk_free(&this->nonce_r);
935 chunk_free(&this->nonce_s);
936 this->randomizer->destroy(this->randomizer);
937 free(this);
938 }
939
940 /*
941 * Described in header.
942 */
943 create_child_sa_t *create_child_sa_create(ike_sa_t *ike_sa)
944 {
945 private_create_child_sa_t *this = malloc_thing(private_create_child_sa_t);
946
947 /* transaction interface functions */
948 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
949 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
950 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
951 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
952 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
953 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
954
955 /* public functions */
956 this->public.set_policy = (void(*)(create_child_sa_t*,policy_t*))set_policy;
957 this->public.set_reqid = (void(*)(create_child_sa_t*,u_int32_t))set_reqid;
958 this->public.rekeys_child = (void(*)(create_child_sa_t*,child_sa_t*))rekeys_child;
959 this->public.cancel = (void(*)(create_child_sa_t*))cancel;
960
961 /* private data */
962 this->ike_sa = ike_sa;
963 this->message_id = 0;
964 this->message = NULL;
965 this->requested = 0;
966 this->rekey_spi = 0;
967 this->reqid = 0;
968 this->nonce_i = CHUNK_INITIALIZER;
969 this->nonce_r = CHUNK_INITIALIZER;
970 this->nonce_s = CHUNK_INITIALIZER;
971 this->child_sa = NULL;
972 this->rekeyed_sa = NULL;
973 this->lost = FALSE;
974 this->proposal = NULL;
975 this->policy = NULL;
976 this->tsi = NULL;
977 this->tsr = NULL;
978 this->randomizer = randomizer_create();
979 this->failsig = CHILD_UP_FAILED;
980
981 return &this->public;
982 }