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