1e1624dca83dd0820a2816ed9457109d11df3f6a
[strongswan.git] / src / charon / sa / tasks / child_create.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 #include "child_create.h"
21
22 #include <daemon.h>
23 #include <crypto/diffie_hellman.h>
24 #include <encoding/payloads/sa_payload.h>
25 #include <encoding/payloads/ke_payload.h>
26 #include <encoding/payloads/ts_payload.h>
27 #include <encoding/payloads/nonce_payload.h>
28 #include <encoding/payloads/notify_payload.h>
29 #include <processing/jobs/delete_ike_sa_job.h>
30
31
32 typedef struct private_child_create_t private_child_create_t;
33
34 /**
35 * Private members of a child_create_t task.
36 */
37 struct private_child_create_t {
38
39 /**
40 * Public methods and task_t interface.
41 */
42 child_create_t public;
43
44 /**
45 * Assigned IKE_SA.
46 */
47 ike_sa_t *ike_sa;
48
49 /**
50 * Are we the initiator?
51 */
52 bool initiator;
53
54 /**
55 * nonce chosen by us
56 */
57 chunk_t my_nonce;
58
59 /**
60 * nonce chosen by peer
61 */
62 chunk_t other_nonce;
63
64 /**
65 * config to create the CHILD_SA from
66 */
67 child_cfg_t *config;
68
69 /**
70 * list of proposal candidates
71 */
72 linked_list_t *proposals;
73
74 /**
75 * selected proposal to use for CHILD_SA
76 */
77 proposal_t *proposal;
78
79 /**
80 * traffic selectors for initiators side
81 */
82 linked_list_t *tsi;
83
84 /**
85 * traffic selectors for responders side
86 */
87 linked_list_t *tsr;
88
89 /**
90 * optional diffie hellman exchange
91 */
92 diffie_hellman_t *dh;
93
94 /**
95 * group used for DH exchange
96 */
97 diffie_hellman_group_t dh_group;
98
99 /**
100 * IKE_SAs keymat
101 */
102 keymat_t *keymat;
103
104 /**
105 * mode the new CHILD_SA uses (transport/tunnel/beet)
106 */
107 ipsec_mode_t mode;
108
109 /**
110 * IPComp transform to use
111 */
112 ipcomp_transform_t ipcomp;
113
114 /**
115 * IPComp transform proposed or accepted by the other peer
116 */
117 ipcomp_transform_t ipcomp_received;
118
119 /**
120 * Own allocated SPI
121 */
122 u_int32_t my_spi;
123
124 /**
125 * SPI received in proposal
126 */
127 u_int32_t other_spi;
128
129 /**
130 * Own allocated Compression Parameter Index (CPI)
131 */
132 u_int16_t my_cpi;
133
134 /**
135 * Other Compression Parameter Index (CPI), received via IPCOMP_SUPPORTED
136 */
137 u_int16_t other_cpi;
138
139 /**
140 * reqid to use if we are rekeying
141 */
142 u_int32_t reqid;
143
144 /**
145 * CHILD_SA which gets established
146 */
147 child_sa_t *child_sa;
148
149 /**
150 * successfully established the CHILD?
151 */
152 bool established;
153 };
154
155 /**
156 * get the nonce from a message
157 */
158 static status_t get_nonce(message_t *message, chunk_t *nonce)
159 {
160 nonce_payload_t *payload;
161
162 payload = (nonce_payload_t*)message->get_payload(message, NONCE);
163 if (payload == NULL)
164 {
165 return FAILED;
166 }
167 *nonce = payload->get_nonce(payload);
168 return NEED_MORE;
169 }
170
171 /**
172 * generate a new nonce to include in a CREATE_CHILD_SA message
173 */
174 static status_t generate_nonce(chunk_t *nonce)
175 {
176 rng_t *rng;
177
178 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
179 if (!rng)
180 {
181 DBG1(DBG_IKE, "error generating nonce value, no RNG found");
182 return FAILED;
183 }
184 rng->allocate_bytes(rng, NONCE_SIZE, nonce);
185 rng->destroy(rng);
186 return SUCCESS;
187 }
188
189 /**
190 * Check a list of traffic selectors if any selector belongs to host
191 */
192 static bool ts_list_is_host(linked_list_t *list, host_t *host)
193 {
194 traffic_selector_t *ts;
195 bool is_host = TRUE;
196 iterator_t *iterator = list->create_iterator(list, TRUE);
197
198 while (is_host && iterator->iterate(iterator, (void**)&ts))
199 {
200 is_host = is_host && ts->is_host(ts, host);
201 }
202 iterator->destroy(iterator);
203 return is_host;
204 }
205
206 /**
207 * Allocate SPIs and update proposals
208 */
209 static bool allocate_spi(private_child_create_t *this)
210 {
211 enumerator_t *enumerator;
212 proposal_t *proposal;
213
214 /* TODO: allocate additional SPI for AH if we have such proposals */
215 this->my_spi = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
216 if (this->my_spi)
217 {
218 if (this->initiator)
219 {
220 enumerator = this->proposals->create_enumerator(this->proposals);
221 while (enumerator->enumerate(enumerator, &proposal))
222 {
223 proposal->set_spi(proposal, this->my_spi);
224 }
225 enumerator->destroy(enumerator);
226 }
227 else
228 {
229 this->proposal->set_spi(this->proposal, this->my_spi);
230 }
231 return TRUE;
232 }
233 return FALSE;
234 }
235
236 /**
237 * Install a CHILD_SA for usage, return value:
238 * - FAILED: no acceptable proposal
239 * - INVALID_ARG: diffie hellman group inacceptable
240 * - NOT_FOUND: TS inacceptable
241 */
242 static status_t select_and_install(private_child_create_t *this, bool no_dh)
243 {
244 status_t status;
245 chunk_t nonce_i, nonce_r;
246 chunk_t encr_i = chunk_empty, encr_r = chunk_empty;
247 chunk_t integ_i = chunk_empty, integ_r = chunk_empty;
248 linked_list_t *my_ts, *other_ts;
249 host_t *me, *other, *other_vip, *my_vip;
250
251 if (this->proposals == NULL)
252 {
253 DBG1(DBG_IKE, "SA payload missing in message");
254 return FAILED;
255 }
256 if (this->tsi == NULL || this->tsr == NULL)
257 {
258 DBG1(DBG_IKE, "TS payloads missing in message");
259 return NOT_FOUND;
260 }
261
262 me = this->ike_sa->get_my_host(this->ike_sa);
263 other = this->ike_sa->get_other_host(this->ike_sa);
264 my_vip = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
265 other_vip = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
266
267 this->proposal = this->config->select_proposal(this->config, this->proposals,
268 no_dh);
269 if (this->proposal == NULL)
270 {
271 DBG1(DBG_IKE, "no acceptable proposal found");
272 return FAILED;
273 }
274 this->other_spi = this->proposal->get_spi(this->proposal);
275
276 if (!this->initiator && !allocate_spi(this))
277 { /* responder has no SPI allocated yet */
278 DBG1(DBG_IKE, "allocating SPI failed");
279 return FAILED;
280 }
281 this->child_sa->set_proposal(this->child_sa, this->proposal);
282
283 if (!this->proposal->has_dh_group(this->proposal, this->dh_group))
284 {
285 u_int16_t group;
286
287 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
288 &group, NULL))
289 {
290 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
291 diffie_hellman_group_names, this->dh_group,
292 diffie_hellman_group_names, group);
293 this->dh_group = group;
294 return INVALID_ARG;
295 }
296 else
297 {
298 DBG1(DBG_IKE, "no acceptable proposal found");
299 return FAILED;
300 }
301 }
302
303 if (my_vip == NULL)
304 {
305 my_vip = me;
306 }
307 if (other_vip == NULL)
308 {
309 other_vip = other;
310 }
311
312 if (this->initiator)
313 {
314 nonce_i = this->my_nonce;
315 nonce_r = this->other_nonce;
316 my_ts = this->tsi;
317 other_ts = this->tsr;
318 }
319 else
320 {
321 nonce_r = this->my_nonce;
322 nonce_i = this->other_nonce;
323 my_ts = this->tsr;
324 other_ts = this->tsi;
325 }
326 my_ts = this->config->get_traffic_selectors(this->config, TRUE, my_ts,
327 my_vip);
328 other_ts = this->config->get_traffic_selectors(this->config, FALSE, other_ts,
329 other_vip);
330
331 if (my_ts->get_count(my_ts) == 0 || other_ts->get_count(other_ts) == 0)
332 {
333 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
334 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
335 DBG1(DBG_IKE, "no acceptable traffic selectors found");
336 return NOT_FOUND;
337 }
338
339 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
340 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
341 if (this->initiator)
342 {
343 this->tsi = my_ts;
344 this->tsr = other_ts;
345 }
346 else
347 {
348 this->tsr = my_ts;
349 this->tsi = other_ts;
350 }
351
352 if (!this->initiator)
353 {
354 /* check if requested mode is acceptable, downgrade if required */
355 switch (this->mode)
356 {
357 case MODE_TRANSPORT:
358 if (!this->config->use_proxy_mode(this->config) &&
359 (!ts_list_is_host(this->tsi, other) ||
360 !ts_list_is_host(this->tsr, me))
361 )
362 {
363 this->mode = MODE_TUNNEL;
364 DBG1(DBG_IKE, "not using transport mode, not host-to-host");
365 }
366 else if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY))
367 {
368 this->mode = MODE_TUNNEL;
369 DBG1(DBG_IKE, "not using transport mode, connection NATed");
370 }
371 break;
372 case MODE_BEET:
373 if (!ts_list_is_host(this->tsi, NULL) ||
374 !ts_list_is_host(this->tsr, NULL))
375 {
376 this->mode = MODE_TUNNEL;
377 DBG1(DBG_IKE, "not using BEET mode, not host-to-host");
378 }
379 break;
380 default:
381 break;
382 }
383 }
384
385 this->child_sa->set_state(this->child_sa, CHILD_INSTALLING);
386 this->child_sa->set_ipcomp(this->child_sa, this->ipcomp);
387 this->child_sa->set_mode(this->child_sa, this->mode);
388 this->child_sa->set_protocol(this->child_sa,
389 this->proposal->get_protocol(this->proposal));
390
391 if (this->my_cpi == 0 || this->other_cpi == 0 || this->ipcomp == IPCOMP_NONE)
392 {
393 this->my_cpi = this->other_cpi = 0;
394 this->ipcomp = IPCOMP_NONE;
395 }
396 status = FAILED;
397 if (this->keymat->derive_child_keys(this->keymat, this->proposal,
398 this->dh, nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r))
399 {
400 if (this->initiator)
401 {
402 status = this->child_sa->install(this->child_sa, encr_r, integ_r,
403 this->my_spi, this->my_cpi, TRUE);
404 status = this->child_sa->install(this->child_sa, encr_i, integ_i,
405 this->other_spi, this->other_cpi, FALSE);
406 }
407 else
408 {
409 status = this->child_sa->install(this->child_sa, encr_i, integ_i,
410 this->my_spi, this->my_cpi, TRUE);
411 status = this->child_sa->install(this->child_sa, encr_r, integ_r,
412 this->other_spi, this->other_cpi, FALSE);
413 }
414 }
415 chunk_clear(&integ_i);
416 chunk_clear(&integ_r);
417 chunk_clear(&encr_i);
418 chunk_clear(&encr_r);
419
420 if (status != SUCCESS)
421 {
422 DBG1(DBG_IKE, "unable to install IPsec SA (SAD) in kernel");
423 return FAILED;
424 }
425
426 status = this->child_sa->add_policies(this->child_sa, my_ts, other_ts);
427 if (status != SUCCESS)
428 {
429 DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel");
430 return NOT_FOUND;
431 }
432
433 charon->bus->child_keys(charon->bus, this->child_sa, this->dh,
434 nonce_i, nonce_r);
435
436 /* add to IKE_SA, and remove from task */
437 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
438 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
439 this->established = TRUE;
440 return SUCCESS;
441 }
442
443 /**
444 * build the payloads for the message
445 */
446 static void build_payloads(private_child_create_t *this, message_t *message)
447 {
448 sa_payload_t *sa_payload;
449 nonce_payload_t *nonce_payload;
450 ke_payload_t *ke_payload;
451 ts_payload_t *ts_payload;
452
453 /* add SA payload */
454 if (this->initiator)
455 {
456 sa_payload = sa_payload_create_from_proposal_list(this->proposals);
457 }
458 else
459 {
460 sa_payload = sa_payload_create_from_proposal(this->proposal);
461 }
462 message->add_payload(message, (payload_t*)sa_payload);
463
464 /* add nonce payload if not in IKE_AUTH */
465 if (message->get_exchange_type(message) == CREATE_CHILD_SA)
466 {
467 nonce_payload = nonce_payload_create();
468 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
469 message->add_payload(message, (payload_t*)nonce_payload);
470 }
471
472 /* diffie hellman exchange, if PFS enabled */
473 if (this->dh)
474 {
475 ke_payload = ke_payload_create_from_diffie_hellman(this->dh);
476 message->add_payload(message, (payload_t*)ke_payload);
477 }
478
479 /* add TSi/TSr payloads */
480 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
481 message->add_payload(message, (payload_t*)ts_payload);
482 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
483 message->add_payload(message, (payload_t*)ts_payload);
484
485 /* add a notify if we are not in tunnel mode */
486 switch (this->mode)
487 {
488 case MODE_TRANSPORT:
489 message->add_notify(message, FALSE, USE_TRANSPORT_MODE, chunk_empty);
490 break;
491 case MODE_BEET:
492 message->add_notify(message, FALSE, USE_BEET_MODE, chunk_empty);
493 break;
494 default:
495 break;
496 }
497 }
498
499 /**
500 * Adds an IPCOMP_SUPPORTED notify to the message, allocating a CPI
501 */
502 static void add_ipcomp_notify(private_child_create_t *this,
503 message_t *message, u_int8_t ipcomp)
504 {
505 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY))
506 {
507 DBG1(DBG_IKE, "IPComp is not supported if either peer is natted, "
508 "IPComp disabled");
509 return;
510 }
511
512 this->my_cpi = this->child_sa->alloc_cpi(this->child_sa);
513 if (this->my_cpi)
514 {
515 this->ipcomp = ipcomp;
516 message->add_notify(message, FALSE, IPCOMP_SUPPORTED,
517 chunk_cata("cc", chunk_from_thing(this->my_cpi),
518 chunk_from_thing(ipcomp)));
519 }
520 else
521 {
522 DBG1(DBG_IKE, "unable to allocate a CPI from kernel, IPComp disabled");
523 }
524 }
525
526 /**
527 * handle a received notify payload
528 */
529 static void handle_notify(private_child_create_t *this, notify_payload_t *notify)
530 {
531 switch (notify->get_notify_type(notify))
532 {
533 case USE_TRANSPORT_MODE:
534 this->mode = MODE_TRANSPORT;
535 break;
536 case USE_BEET_MODE:
537 this->mode = MODE_BEET;
538 break;
539 case IPCOMP_SUPPORTED:
540 {
541 ipcomp_transform_t ipcomp;
542 u_int16_t cpi;
543 chunk_t data;
544
545 data = notify->get_notification_data(notify);
546 cpi = *(u_int16_t*)data.ptr;
547 ipcomp = (ipcomp_transform_t)(*(data.ptr + 2));
548 switch (ipcomp)
549 {
550 case IPCOMP_DEFLATE:
551 this->other_cpi = cpi;
552 this->ipcomp_received = ipcomp;
553 break;
554 case IPCOMP_LZS:
555 case IPCOMP_LZJH:
556 default:
557 DBG1(DBG_IKE, "received IPCOMP_SUPPORTED notify with a "
558 "transform ID we don't support %N",
559 ipcomp_transform_names, ipcomp);
560 break;
561 }
562 }
563 default:
564 break;
565 }
566 }
567
568 /**
569 * Read payloads from message
570 */
571 static void process_payloads(private_child_create_t *this, message_t *message)
572 {
573 enumerator_t *enumerator;
574 payload_t *payload;
575 sa_payload_t *sa_payload;
576 ke_payload_t *ke_payload;
577 ts_payload_t *ts_payload;
578
579 /* defaults to TUNNEL mode */
580 this->mode = MODE_TUNNEL;
581
582 enumerator = message->create_payload_enumerator(message);
583 while (enumerator->enumerate(enumerator, &payload))
584 {
585 switch (payload->get_type(payload))
586 {
587 case SECURITY_ASSOCIATION:
588 sa_payload = (sa_payload_t*)payload;
589 this->proposals = sa_payload->get_proposals(sa_payload);
590 break;
591 case KEY_EXCHANGE:
592 ke_payload = (ke_payload_t*)payload;
593 if (!this->initiator)
594 {
595 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
596 this->dh = this->keymat->create_dh(this->keymat, this->dh_group);
597 }
598 if (this->dh)
599 {
600 this->dh->set_other_public_value(this->dh,
601 ke_payload->get_key_exchange_data(ke_payload));
602 }
603 break;
604 case TRAFFIC_SELECTOR_INITIATOR:
605 ts_payload = (ts_payload_t*)payload;
606 this->tsi = ts_payload->get_traffic_selectors(ts_payload);
607 break;
608 case TRAFFIC_SELECTOR_RESPONDER:
609 ts_payload = (ts_payload_t*)payload;
610 this->tsr = ts_payload->get_traffic_selectors(ts_payload);
611 break;
612 case NOTIFY:
613 handle_notify(this, (notify_payload_t*)payload);
614 break;
615 default:
616 break;
617 }
618 }
619 enumerator->destroy(enumerator);
620 }
621
622 /**
623 * Implementation of task_t.build for initiator
624 */
625 static status_t build_i(private_child_create_t *this, message_t *message)
626 {
627 host_t *me, *other, *vip;
628 peer_cfg_t *peer_cfg;
629
630 switch (message->get_exchange_type(message))
631 {
632 case IKE_SA_INIT:
633 return get_nonce(message, &this->my_nonce);
634 case CREATE_CHILD_SA:
635 if (generate_nonce(&this->my_nonce) != SUCCESS)
636 {
637 message->add_notify(message, FALSE, NO_PROPOSAL_CHOSEN, chunk_empty);
638 return SUCCESS;
639 }
640 if (this->dh_group == MODP_NONE)
641 {
642 this->dh_group = this->config->get_dh_group(this->config);
643 }
644 break;
645 case IKE_AUTH:
646 if (message->get_message_id(message) != 1)
647 {
648 /* send only in the first request, not in subsequent rounds */
649 return NEED_MORE;
650 }
651 break;
652 default:
653 break;
654 }
655
656 if (this->reqid)
657 {
658 DBG0(DBG_IKE, "establishing CHILD_SA %s{%d}",
659 this->config->get_name(this->config), this->reqid);
660 }
661 else
662 {
663 DBG0(DBG_IKE, "establishing CHILD_SA %s",
664 this->config->get_name(this->config));
665 }
666
667 /* reuse virtual IP if we already have one */
668 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
669 if (me == NULL)
670 {
671 me = this->ike_sa->get_my_host(this->ike_sa);
672 }
673 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
674 if (other == NULL)
675 {
676 other = this->ike_sa->get_other_host(this->ike_sa);
677 }
678
679 /* check if we want a virtual IP, but don't have one */
680 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
681 vip = peer_cfg->get_virtual_ip(peer_cfg);
682 if (!this->reqid && vip)
683 {
684 /* propose a 0.0.0.0/0 or ::/0 subnet when we use virtual ip */
685 vip = host_create_any(vip->get_family(vip));
686 this->tsi = this->config->get_traffic_selectors(this->config, TRUE,
687 NULL, vip);
688 vip->destroy(vip);
689 }
690 else
691 { /* but narrow it for host2host / if we already have a vip */
692 this->tsi = this->config->get_traffic_selectors(this->config, TRUE,
693 NULL, me);
694 }
695 this->tsr = this->config->get_traffic_selectors(this->config, FALSE,
696 NULL, other);
697
698 this->proposals = this->config->get_proposals(this->config,
699 this->dh_group == MODP_NONE);
700 this->mode = this->config->get_mode(this->config);
701
702 this->child_sa = child_sa_create(this->ike_sa->get_my_host(this->ike_sa),
703 this->ike_sa->get_other_host(this->ike_sa), this->config, this->reqid,
704 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY));
705
706 if (!allocate_spi(this))
707 {
708 DBG1(DBG_IKE, "unable to allocate SPIs from kernel");
709 return FAILED;
710 }
711
712 if (this->dh_group != MODP_NONE)
713 {
714 this->dh = this->keymat->create_dh(this->keymat, this->dh_group);
715 }
716
717 if (this->config->use_ipcomp(this->config))
718 {
719 /* IPCOMP_DEFLATE is the only transform we support at the moment */
720 add_ipcomp_notify(this, message, IPCOMP_DEFLATE);
721 }
722
723 build_payloads(this, message);
724
725 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
726 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
727 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
728 this->tsi = NULL;
729 this->tsr = NULL;
730 this->proposals = NULL;
731
732 return NEED_MORE;
733 }
734
735 /**
736 * Implementation of task_t.process for responder
737 */
738 static status_t process_r(private_child_create_t *this, message_t *message)
739 {
740 switch (message->get_exchange_type(message))
741 {
742 case IKE_SA_INIT:
743 return get_nonce(message, &this->other_nonce);
744 case CREATE_CHILD_SA:
745 get_nonce(message, &this->other_nonce);
746 break;
747 case IKE_AUTH:
748 if (message->get_message_id(message) != 1)
749 {
750 /* only handle first AUTH payload, not additional rounds */
751 return NEED_MORE;
752 }
753 default:
754 break;
755 }
756
757 process_payloads(this, message);
758
759 return NEED_MORE;
760 }
761
762 /**
763 * handle CHILD_SA setup failure
764 */
765 static void handle_child_sa_failure(private_child_create_t *this,
766 message_t *message)
767 {
768 if (message->get_exchange_type(message) == IKE_AUTH &&
769 lib->settings->get_bool(lib->settings,
770 "charon.close_ike_on_child_failure", FALSE))
771 {
772 /* we delay the delete for 100ms, as the IKE_AUTH response must arrive
773 * first */
774 DBG1(DBG_IKE, "closing IKE_SA due CHILD_SA setup failure");
775 charon->scheduler->schedule_job(charon->scheduler, (job_t*)
776 delete_ike_sa_job_create(this->ike_sa->get_id(this->ike_sa), TRUE),
777 100);
778 }
779 }
780
781 /**
782 * Implementation of task_t.build for responder
783 */
784 static status_t build_r(private_child_create_t *this, message_t *message)
785 {
786 peer_cfg_t *peer_cfg;
787 payload_t *payload;
788 enumerator_t *enumerator;
789 bool no_dh = TRUE;
790
791 switch (message->get_exchange_type(message))
792 {
793 case IKE_SA_INIT:
794 return get_nonce(message, &this->my_nonce);
795 case CREATE_CHILD_SA:
796 if (generate_nonce(&this->my_nonce) != SUCCESS)
797 {
798 message->add_notify(message, FALSE, NO_PROPOSAL_CHOSEN,
799 chunk_empty);
800 return SUCCESS;
801 }
802 no_dh = FALSE;
803 break;
804 case IKE_AUTH:
805 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
806 { /* wait until all authentication round completed */
807 return NEED_MORE;
808 }
809 default:
810 break;
811 }
812
813 if (this->ike_sa->get_state(this->ike_sa) == IKE_REKEYING)
814 {
815 DBG1(DBG_IKE, "unable to create CHILD_SA while rekeying IKE_SA");
816 message->add_notify(message, TRUE, NO_ADDITIONAL_SAS, chunk_empty);
817 return SUCCESS;
818 }
819
820 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
821 if (peer_cfg && this->tsi && this->tsr)
822 {
823 host_t *me, *other;
824
825 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
826 if (me == NULL)
827 {
828 me = this->ike_sa->get_my_host(this->ike_sa);
829 }
830 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
831 if (other == NULL)
832 {
833 other = this->ike_sa->get_other_host(this->ike_sa);
834 }
835 this->config = peer_cfg->select_child_cfg(peer_cfg, this->tsr,
836 this->tsi, me, other);
837 }
838
839 if (this->config == NULL)
840 {
841 DBG1(DBG_IKE, "traffic selectors %#R=== %#R inacceptable",
842 this->tsr, this->tsi);
843 message->add_notify(message, FALSE, TS_UNACCEPTABLE, chunk_empty);
844 handle_child_sa_failure(this, message);
845 return SUCCESS;
846 }
847
848 /* check if ike_config_t included non-critical error notifies */
849 enumerator = message->create_payload_enumerator(message);
850 while (enumerator->enumerate(enumerator, &payload))
851 {
852 if (payload->get_type(payload) == NOTIFY)
853 {
854 notify_payload_t *notify = (notify_payload_t*)payload;
855
856 switch (notify->get_notify_type(notify))
857 {
858 case INTERNAL_ADDRESS_FAILURE:
859 case FAILED_CP_REQUIRED:
860 {
861 DBG1(DBG_IKE,"configuration payload negotation "
862 "failed, no CHILD_SA built");
863 enumerator->destroy(enumerator);
864 handle_child_sa_failure(this, message);
865 return SUCCESS;
866 }
867 default:
868 break;
869 }
870 }
871 }
872 enumerator->destroy(enumerator);
873
874 this->child_sa = child_sa_create(this->ike_sa->get_my_host(this->ike_sa),
875 this->ike_sa->get_other_host(this->ike_sa), this->config, this->reqid,
876 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY));
877
878 if (this->ipcomp_received != IPCOMP_NONE)
879 {
880 if (this->config->use_ipcomp(this->config))
881 {
882 add_ipcomp_notify(this, message, this->ipcomp_received);
883 }
884 else
885 {
886 DBG1(DBG_IKE, "received %N notify but IPComp is disabled, ignoring",
887 notify_type_names, IPCOMP_SUPPORTED);
888 }
889 }
890
891 switch (select_and_install(this, no_dh))
892 {
893 case SUCCESS:
894 break;
895 case NOT_FOUND:
896 message->add_notify(message, FALSE, TS_UNACCEPTABLE, chunk_empty);
897 handle_child_sa_failure(this, message);
898 return SUCCESS;
899 case INVALID_ARG:
900 {
901 u_int16_t group = htons(this->dh_group);
902 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
903 chunk_from_thing(group));
904 handle_child_sa_failure(this, message);
905 return SUCCESS;
906 }
907 case FAILED:
908 default:
909 message->add_notify(message, FALSE, NO_PROPOSAL_CHOSEN, chunk_empty);
910 handle_child_sa_failure(this, message);
911 return SUCCESS;
912 }
913
914 build_payloads(this, message);
915
916 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
917 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
918 this->child_sa->get_name(this->child_sa),
919 this->child_sa->get_reqid(this->child_sa),
920 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
921 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
922 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
923 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
924
925 return SUCCESS;
926 }
927
928 /**
929 * Implementation of task_t.process for initiator
930 */
931 static status_t process_i(private_child_create_t *this, message_t *message)
932 {
933 enumerator_t *enumerator;
934 payload_t *payload;
935 bool no_dh = TRUE;
936
937 switch (message->get_exchange_type(message))
938 {
939 case IKE_SA_INIT:
940 return get_nonce(message, &this->other_nonce);
941 case CREATE_CHILD_SA:
942 get_nonce(message, &this->other_nonce);
943 no_dh = FALSE;
944 break;
945 case IKE_AUTH:
946 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
947 { /* wait until all authentication round completed */
948 return NEED_MORE;
949 }
950 default:
951 break;
952 }
953
954 /* check for erronous notifies */
955 enumerator = message->create_payload_enumerator(message);
956 while (enumerator->enumerate(enumerator, &payload))
957 {
958 if (payload->get_type(payload) == NOTIFY)
959 {
960 notify_payload_t *notify = (notify_payload_t*)payload;
961 notify_type_t type = notify->get_notify_type(notify);
962
963 switch (type)
964 {
965 /* handle notify errors related to CHILD_SA only */
966 case NO_PROPOSAL_CHOSEN:
967 case SINGLE_PAIR_REQUIRED:
968 case NO_ADDITIONAL_SAS:
969 case INTERNAL_ADDRESS_FAILURE:
970 case FAILED_CP_REQUIRED:
971 case TS_UNACCEPTABLE:
972 case INVALID_SELECTORS:
973 {
974 DBG1(DBG_IKE, "received %N notify, no CHILD_SA built",
975 notify_type_names, type);
976 enumerator->destroy(enumerator);
977 handle_child_sa_failure(this, message);
978 /* an error in CHILD_SA creation is not critical */
979 return SUCCESS;
980 }
981 case INVALID_KE_PAYLOAD:
982 {
983 chunk_t data;
984 diffie_hellman_group_t bad_group;
985
986 bad_group = this->dh_group;
987 data = notify->get_notification_data(notify);
988 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
989 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
990 "it requested %N", diffie_hellman_group_names,
991 bad_group, diffie_hellman_group_names, this->dh_group);
992
993 this->public.task.migrate(&this->public.task, this->ike_sa);
994 enumerator->destroy(enumerator);
995 return NEED_MORE;
996 }
997 default:
998 break;
999 }
1000 }
1001 }
1002 enumerator->destroy(enumerator);
1003
1004 process_payloads(this, message);
1005
1006 if (this->ipcomp == IPCOMP_NONE && this->ipcomp_received != IPCOMP_NONE)
1007 {
1008 DBG1(DBG_IKE, "received an IPCOMP_SUPPORTED notify without requesting"
1009 " one, no CHILD_SA built");
1010 handle_child_sa_failure(this, message);
1011 return SUCCESS;
1012 }
1013 else if (this->ipcomp != IPCOMP_NONE && this->ipcomp_received == IPCOMP_NONE)
1014 {
1015 DBG1(DBG_IKE, "peer didn't accept our proposed IPComp transforms, "
1016 "IPComp is disabled");
1017 this->ipcomp = IPCOMP_NONE;
1018 }
1019 else if (this->ipcomp != IPCOMP_NONE && this->ipcomp != this->ipcomp_received)
1020 {
1021 DBG1(DBG_IKE, "received an IPCOMP_SUPPORTED notify we didn't propose, "
1022 "no CHILD_SA built");
1023 handle_child_sa_failure(this, message);
1024 return SUCCESS;
1025 }
1026
1027 if (select_and_install(this, no_dh) == SUCCESS)
1028 {
1029 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
1030 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
1031 this->child_sa->get_name(this->child_sa),
1032 this->child_sa->get_reqid(this->child_sa),
1033 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
1034 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
1035 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
1036 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
1037 }
1038 else
1039 {
1040 handle_child_sa_failure(this, message);
1041 }
1042 return SUCCESS;
1043 }
1044
1045 /**
1046 * Implementation of task_t.get_type
1047 */
1048 static task_type_t get_type(private_child_create_t *this)
1049 {
1050 return CHILD_CREATE;
1051 }
1052
1053 /**
1054 * Implementation of child_create_t.use_reqid
1055 */
1056 static void use_reqid(private_child_create_t *this, u_int32_t reqid)
1057 {
1058 this->reqid = reqid;
1059 }
1060
1061 /**
1062 * Implementation of child_create_t.get_child
1063 */
1064 static child_sa_t* get_child(private_child_create_t *this)
1065 {
1066 return this->child_sa;
1067 }
1068
1069 /**
1070 * Implementation of child_create_t.get_lower_nonce
1071 */
1072 static chunk_t get_lower_nonce(private_child_create_t *this)
1073 {
1074 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
1075 min(this->my_nonce.len, this->other_nonce.len)) < 0)
1076 {
1077 return this->my_nonce;
1078 }
1079 else
1080 {
1081 return this->other_nonce;
1082 }
1083 }
1084
1085 /**
1086 * Implementation of task_t.migrate
1087 */
1088 static void migrate(private_child_create_t *this, ike_sa_t *ike_sa)
1089 {
1090 chunk_free(&this->my_nonce);
1091 chunk_free(&this->other_nonce);
1092 if (this->tsi)
1093 {
1094 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
1095 }
1096 if (this->tsr)
1097 {
1098 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
1099 }
1100 DESTROY_IF(this->child_sa);
1101 DESTROY_IF(this->proposal);
1102 DESTROY_IF(this->dh);
1103 if (this->proposals)
1104 {
1105 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
1106 }
1107
1108 this->ike_sa = ike_sa;
1109 this->keymat = ike_sa->get_keymat(ike_sa);
1110 this->proposal = NULL;
1111 this->proposals = NULL;
1112 this->tsi = NULL;
1113 this->tsr = NULL;
1114 this->dh = NULL;
1115 this->child_sa = NULL;
1116 this->mode = MODE_TUNNEL;
1117 this->ipcomp = IPCOMP_NONE;
1118 this->ipcomp_received = IPCOMP_NONE;
1119 this->other_cpi = 0;
1120 this->reqid = 0;
1121 this->established = FALSE;
1122 }
1123
1124 /**
1125 * Implementation of task_t.destroy
1126 */
1127 static void destroy(private_child_create_t *this)
1128 {
1129 chunk_free(&this->my_nonce);
1130 chunk_free(&this->other_nonce);
1131 if (this->tsi)
1132 {
1133 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
1134 }
1135 if (this->tsr)
1136 {
1137 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
1138 }
1139 if (!this->established)
1140 {
1141 DESTROY_IF(this->child_sa);
1142 }
1143 DESTROY_IF(this->proposal);
1144 DESTROY_IF(this->dh);
1145 if (this->proposals)
1146 {
1147 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
1148 }
1149
1150 DESTROY_IF(this->config);
1151 free(this);
1152 }
1153
1154 /*
1155 * Described in header.
1156 */
1157 child_create_t *child_create_create(ike_sa_t *ike_sa, child_cfg_t *config)
1158 {
1159 private_child_create_t *this = malloc_thing(private_child_create_t);
1160
1161 this->public.get_child = (child_sa_t*(*)(child_create_t*))get_child;
1162 this->public.get_lower_nonce = (chunk_t(*)(child_create_t*))get_lower_nonce;
1163 this->public.use_reqid = (void(*)(child_create_t*,u_int32_t))use_reqid;
1164 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
1165 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
1166 this->public.task.destroy = (void(*)(task_t*))destroy;
1167 if (config)
1168 {
1169 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
1170 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
1171 this->initiator = TRUE;
1172 config->get_ref(config);
1173 }
1174 else
1175 {
1176 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
1177 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
1178 this->initiator = FALSE;
1179 }
1180
1181 this->ike_sa = ike_sa;
1182 this->config = config;
1183 this->my_nonce = chunk_empty;
1184 this->other_nonce = chunk_empty;
1185 this->proposals = NULL;
1186 this->proposal = NULL;
1187 this->tsi = NULL;
1188 this->tsr = NULL;
1189 this->dh = NULL;
1190 this->dh_group = MODP_NONE;
1191 this->keymat = ike_sa->get_keymat(ike_sa);
1192 this->child_sa = NULL;
1193 this->mode = MODE_TUNNEL;
1194 this->ipcomp = IPCOMP_NONE;
1195 this->ipcomp_received = IPCOMP_NONE;
1196 this->my_spi = 0;
1197 this->other_spi = 0;
1198 this->my_cpi = 0;
1199 this->other_cpi = 0;
1200 this->reqid = 0;
1201 this->established = FALSE;
1202
1203 return &this->public;
1204 }