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