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