encoding: Allow ke_payload_create_from_diffie_hellman() to fail
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / quick_mode.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /*
20 * Copyright (C) 2012 Volker RĂ¼melin
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #include "quick_mode.h"
42
43 #include <string.h>
44
45 #include <daemon.h>
46 #include <sa/ikev1/keymat_v1.h>
47 #include <encoding/payloads/sa_payload.h>
48 #include <encoding/payloads/nonce_payload.h>
49 #include <encoding/payloads/ke_payload.h>
50 #include <encoding/payloads/id_payload.h>
51 #include <encoding/payloads/payload.h>
52 #include <sa/ikev1/tasks/informational.h>
53 #include <sa/ikev1/tasks/quick_delete.h>
54 #include <processing/jobs/inactivity_job.h>
55
56 typedef struct private_quick_mode_t private_quick_mode_t;
57
58 /**
59 * Private members of a quick_mode_t task.
60 */
61 struct private_quick_mode_t {
62
63 /**
64 * Public methods and task_t interface.
65 */
66 quick_mode_t public;
67
68 /**
69 * Assigned IKE_SA.
70 */
71 ike_sa_t *ike_sa;
72
73 /**
74 * TRUE if we are initiating quick mode
75 */
76 bool initiator;
77
78 /**
79 * Traffic selector of initiator
80 */
81 traffic_selector_t *tsi;
82
83 /**
84 * Traffic selector of responder
85 */
86 traffic_selector_t *tsr;
87
88 /**
89 * Initiators nonce
90 */
91 chunk_t nonce_i;
92
93 /**
94 * Responder nonce
95 */
96 chunk_t nonce_r;
97
98 /**
99 * Initiators ESP SPI
100 */
101 u_int32_t spi_i;
102
103 /**
104 * Responder ESP SPI
105 */
106 u_int32_t spi_r;
107
108 /**
109 * Initiators IPComp CPI
110 */
111 u_int16_t cpi_i;
112
113 /**
114 * Responders IPComp CPI
115 */
116 u_int16_t cpi_r;
117
118 /**
119 * selected CHILD_SA proposal
120 */
121 proposal_t *proposal;
122
123 /**
124 * Config of CHILD_SA to establish
125 */
126 child_cfg_t *config;
127
128 /**
129 * CHILD_SA we are about to establish
130 */
131 child_sa_t *child_sa;
132
133 /**
134 * IKEv1 keymat
135 */
136 keymat_v1_t *keymat;
137
138 /**
139 * DH exchange, when PFS is in use
140 */
141 diffie_hellman_t *dh;
142
143 /**
144 * Negotiated lifetime of new SA
145 */
146 u_int32_t lifetime;
147
148 /**
149 * Negotaited lifebytes of new SA
150 */
151 u_int64_t lifebytes;
152
153 /**
154 * Reqid to use, 0 for auto-allocate
155 */
156 u_int32_t reqid;
157
158 /**
159 * Explicit inbound mark value to use, if any
160 */
161 u_int mark_in;
162
163 /**
164 * Explicit inbound mark value to use, if any
165 */
166 u_int mark_out;
167
168 /**
169 * SPI of SA we rekey
170 */
171 u_int32_t rekey;
172
173 /**
174 * Negotiated mode, tunnel or transport
175 */
176 ipsec_mode_t mode;
177
178 /*
179 * SA protocol (ESP|AH) negotiated
180 */
181 protocol_id_t proto;
182
183 /**
184 * Use UDP encapsulation
185 */
186 bool udp;
187
188 /** states of quick mode */
189 enum {
190 QM_INIT,
191 QM_NEGOTIATED,
192 } state;
193 };
194
195 /**
196 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
197 */
198 static void schedule_inactivity_timeout(private_quick_mode_t *this)
199 {
200 u_int32_t timeout;
201 bool close_ike;
202
203 timeout = this->config->get_inactivity(this->config);
204 if (timeout)
205 {
206 close_ike = lib->settings->get_bool(lib->settings,
207 "%s.inactivity_close_ike", FALSE, lib->ns);
208 lib->scheduler->schedule_job(lib->scheduler, (job_t*)
209 inactivity_job_create(this->child_sa->get_unique_id(this->child_sa),
210 timeout, close_ike), timeout);
211 }
212 }
213
214 /**
215 * Check if we have a an address pool configured
216 */
217 static bool have_pool(ike_sa_t *ike_sa)
218 {
219 enumerator_t *enumerator;
220 peer_cfg_t *peer_cfg;
221 char *pool;
222 bool found = FALSE;
223
224 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
225 if (peer_cfg)
226 {
227 enumerator = peer_cfg->create_pool_enumerator(peer_cfg);
228 if (enumerator->enumerate(enumerator, &pool))
229 {
230 found = TRUE;
231 }
232 enumerator->destroy(enumerator);
233 }
234 return found;
235 }
236
237 /**
238 * Get hosts to use for dynamic traffic selectors
239 */
240 static linked_list_t *get_dynamic_hosts(ike_sa_t *ike_sa, bool local)
241 {
242 enumerator_t *enumerator;
243 linked_list_t *list;
244 host_t *host;
245
246 list = linked_list_create();
247 enumerator = ike_sa->create_virtual_ip_enumerator(ike_sa, local);
248 while (enumerator->enumerate(enumerator, &host))
249 {
250 list->insert_last(list, host);
251 }
252 enumerator->destroy(enumerator);
253
254 if (list->get_count(list) == 0)
255 { /* no virtual IPs assigned */
256 if (local)
257 {
258 host = ike_sa->get_my_host(ike_sa);
259 list->insert_last(list, host);
260 }
261 else if (!have_pool(ike_sa))
262 { /* use host only if we don't have a pool configured */
263 host = ike_sa->get_other_host(ike_sa);
264 list->insert_last(list, host);
265 }
266 }
267 return list;
268 }
269
270 /**
271 * Install negotiated CHILD_SA
272 */
273 static bool install(private_quick_mode_t *this)
274 {
275 status_t status, status_i, status_o;
276 chunk_t encr_i, encr_r, integ_i, integ_r;
277 linked_list_t *tsi, *tsr, *my_ts, *other_ts;
278 child_sa_t *old = NULL;
279
280 this->child_sa->set_proposal(this->child_sa, this->proposal);
281 this->child_sa->set_state(this->child_sa, CHILD_INSTALLING);
282 this->child_sa->set_mode(this->child_sa, this->mode);
283
284 if (this->cpi_i && this->cpi_r)
285 { /* DEFLATE is the only transform we currently support */
286 this->child_sa->set_ipcomp(this->child_sa, IPCOMP_DEFLATE);
287 }
288 else
289 {
290 this->cpi_i = this->cpi_r = 0;
291 }
292
293 this->child_sa->set_protocol(this->child_sa,
294 this->proposal->get_protocol(this->proposal));
295
296 status_i = status_o = FAILED;
297 encr_i = encr_r = integ_i = integ_r = chunk_empty;
298 tsi = linked_list_create_with_items(this->tsi->clone(this->tsi), NULL);
299 tsr = linked_list_create_with_items(this->tsr->clone(this->tsr), NULL);
300 if (this->initiator)
301 {
302 charon->bus->narrow(charon->bus, this->child_sa,
303 NARROW_INITIATOR_POST_AUTH, tsi, tsr);
304 }
305 else
306 {
307 charon->bus->narrow(charon->bus, this->child_sa,
308 NARROW_RESPONDER_POST, tsr, tsi);
309 }
310 if (tsi->get_count(tsi) == 0 || tsr->get_count(tsr) == 0)
311 {
312 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
313 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
314 DBG1(DBG_IKE, "no acceptable traffic selectors found");
315 return FALSE;
316 }
317
318 if (this->keymat->derive_child_keys(this->keymat, this->proposal, this->dh,
319 this->spi_i, this->spi_r, this->nonce_i, this->nonce_r,
320 &encr_i, &integ_i, &encr_r, &integ_r))
321 {
322 if (this->initiator)
323 {
324 status_i = this->child_sa->install(this->child_sa,
325 encr_r, integ_r, this->spi_i, this->cpi_i,
326 this->initiator, TRUE, FALSE, tsi, tsr);
327 status_o = this->child_sa->install(this->child_sa,
328 encr_i, integ_i, this->spi_r, this->cpi_r,
329 this->initiator, FALSE, FALSE, tsi, tsr);
330 }
331 else
332 {
333 status_i = this->child_sa->install(this->child_sa,
334 encr_i, integ_i, this->spi_r, this->cpi_r,
335 this->initiator, TRUE, FALSE, tsr, tsi);
336 status_o = this->child_sa->install(this->child_sa,
337 encr_r, integ_r, this->spi_i, this->cpi_i,
338 this->initiator, FALSE, FALSE, tsr, tsi);
339 }
340 }
341 chunk_clear(&integ_i);
342 chunk_clear(&integ_r);
343 chunk_clear(&encr_i);
344 chunk_clear(&encr_r);
345
346 if (status_i != SUCCESS || status_o != SUCCESS)
347 {
348 DBG1(DBG_IKE, "unable to install %s%s%sIPsec SA (SAD) in kernel",
349 (status_i != SUCCESS) ? "inbound " : "",
350 (status_i != SUCCESS && status_o != SUCCESS) ? "and ": "",
351 (status_o != SUCCESS) ? "outbound " : "");
352 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
353 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
354 return FALSE;
355 }
356
357 if (this->initiator)
358 {
359 status = this->child_sa->add_policies(this->child_sa, tsi, tsr);
360 }
361 else
362 {
363 status = this->child_sa->add_policies(this->child_sa, tsr, tsi);
364 }
365 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
366 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
367 if (status != SUCCESS)
368 {
369 DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel");
370 return FALSE;
371 }
372
373 charon->bus->child_keys(charon->bus, this->child_sa, this->initiator,
374 this->dh, this->nonce_i, this->nonce_r);
375
376 /* add to IKE_SA, and remove from task */
377 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
378 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
379
380 my_ts = linked_list_create_from_enumerator(
381 this->child_sa->create_ts_enumerator(this->child_sa, TRUE));
382 other_ts = linked_list_create_from_enumerator(
383 this->child_sa->create_ts_enumerator(this->child_sa, FALSE));
384
385 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
386 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
387 this->child_sa->get_name(this->child_sa),
388 this->child_sa->get_unique_id(this->child_sa),
389 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
390 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)), my_ts, other_ts);
391
392 my_ts->destroy(my_ts);
393 other_ts->destroy(other_ts);
394
395 if (this->rekey)
396 {
397 old = this->ike_sa->get_child_sa(this->ike_sa,
398 this->proposal->get_protocol(this->proposal),
399 this->rekey, TRUE);
400 }
401 if (old)
402 {
403 charon->bus->child_rekey(charon->bus, old, this->child_sa);
404 /* rekeyed CHILD_SAs stay installed until they expire */
405 old->set_state(old, CHILD_INSTALLED);
406 }
407 else
408 {
409 charon->bus->child_updown(charon->bus, this->child_sa, TRUE);
410 }
411 schedule_inactivity_timeout(this);
412 this->child_sa = NULL;
413 return TRUE;
414 }
415
416 /**
417 * Generate and add NONCE
418 */
419 static bool add_nonce(private_quick_mode_t *this, chunk_t *nonce,
420 message_t *message)
421 {
422 nonce_payload_t *nonce_payload;
423 nonce_gen_t *nonceg;
424
425 nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
426 if (!nonceg)
427 {
428 DBG1(DBG_IKE, "no nonce generator found to create nonce");
429 return FALSE;
430 }
431 if (!nonceg->allocate_nonce(nonceg, NONCE_SIZE, nonce))
432 {
433 DBG1(DBG_IKE, "nonce allocation failed");
434 nonceg->destroy(nonceg);
435 return FALSE;
436 }
437 nonceg->destroy(nonceg);
438
439 nonce_payload = nonce_payload_create(PLV1_NONCE);
440 nonce_payload->set_nonce(nonce_payload, *nonce);
441 message->add_payload(message, &nonce_payload->payload_interface);
442
443 return TRUE;
444 }
445
446 /**
447 * Extract nonce from NONCE payload
448 */
449 static bool get_nonce(private_quick_mode_t *this, chunk_t *nonce,
450 message_t *message)
451 {
452 nonce_payload_t *nonce_payload;
453
454 nonce_payload = (nonce_payload_t*)message->get_payload(message, PLV1_NONCE);
455 if (!nonce_payload)
456 {
457 DBG1(DBG_IKE, "NONCE payload missing in message");
458 return FALSE;
459 }
460 *nonce = nonce_payload->get_nonce(nonce_payload);
461
462 return TRUE;
463 }
464
465 /**
466 * Add KE payload to message
467 */
468 static bool add_ke(private_quick_mode_t *this, message_t *message)
469 {
470 ke_payload_t *ke_payload;
471
472 ke_payload = ke_payload_create_from_diffie_hellman(PLV1_KEY_EXCHANGE,
473 this->dh);
474 if (!ke_payload)
475 {
476 DBG1(DBG_IKE, "creating KE payload failed");
477 return FALSE;
478 }
479 message->add_payload(message, &ke_payload->payload_interface);
480 return TRUE;
481 }
482
483 /**
484 * Get DH value from a KE payload
485 */
486 static bool get_ke(private_quick_mode_t *this, message_t *message)
487 {
488 ke_payload_t *ke_payload;
489
490 ke_payload = (ke_payload_t*)message->get_payload(message, PLV1_KEY_EXCHANGE);
491 if (!ke_payload)
492 {
493 DBG1(DBG_IKE, "KE payload missing");
494 return FALSE;
495 }
496 this->dh->set_other_public_value(this->dh,
497 ke_payload->get_key_exchange_data(ke_payload));
498 return TRUE;
499 }
500
501 /**
502 * Select a traffic selector from configuration
503 */
504 static traffic_selector_t* select_ts(private_quick_mode_t *this, bool local,
505 linked_list_t *supplied)
506 {
507 traffic_selector_t *ts;
508 linked_list_t *list, *hosts;
509
510 hosts = get_dynamic_hosts(this->ike_sa, local);
511 list = this->config->get_traffic_selectors(this->config,
512 local, supplied, hosts);
513 hosts->destroy(hosts);
514 if (list->get_first(list, (void**)&ts) == SUCCESS)
515 {
516 ts = ts->clone(ts);
517 }
518 else
519 {
520 DBG1(DBG_IKE, "%s traffic selector missing in configuration",
521 local ? "local" : "remote");
522 ts = NULL;
523 }
524 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
525 return ts;
526 }
527
528 /**
529 * Add selected traffic selectors to message
530 */
531 static void add_ts(private_quick_mode_t *this, message_t *message)
532 {
533 id_payload_t *id_payload;
534
535 id_payload = id_payload_create_from_ts(this->tsi);
536 message->add_payload(message, &id_payload->payload_interface);
537 id_payload = id_payload_create_from_ts(this->tsr);
538 message->add_payload(message, &id_payload->payload_interface);
539 }
540
541 /**
542 * Get traffic selectors from received message
543 */
544 static bool get_ts(private_quick_mode_t *this, message_t *message)
545 {
546 traffic_selector_t *tsi = NULL, *tsr = NULL;
547 enumerator_t *enumerator;
548 id_payload_t *id_payload;
549 payload_t *payload;
550 host_t *hsi, *hsr;
551 bool first = TRUE;
552
553 enumerator = message->create_payload_enumerator(message);
554 while (enumerator->enumerate(enumerator, &payload))
555 {
556 if (payload->get_type(payload) == PLV1_ID)
557 {
558 id_payload = (id_payload_t*)payload;
559
560 if (first)
561 {
562 tsi = id_payload->get_ts(id_payload);
563 first = FALSE;
564 }
565 else
566 {
567 tsr = id_payload->get_ts(id_payload);
568 break;
569 }
570 }
571 }
572 enumerator->destroy(enumerator);
573
574 /* create host2host selectors if ID payloads missing */
575 if (this->initiator)
576 {
577 hsi = this->ike_sa->get_my_host(this->ike_sa);
578 hsr = this->ike_sa->get_other_host(this->ike_sa);
579 }
580 else
581 {
582 hsr = this->ike_sa->get_my_host(this->ike_sa);
583 hsi = this->ike_sa->get_other_host(this->ike_sa);
584 }
585 if (!tsi)
586 {
587 tsi = traffic_selector_create_from_subnet(hsi->clone(hsi),
588 hsi->get_family(hsi) == AF_INET ? 32 : 128, 0, 0, 65535);
589 }
590 if (!tsr)
591 {
592 tsr = traffic_selector_create_from_subnet(hsr->clone(hsr),
593 hsr->get_family(hsr) == AF_INET ? 32 : 128, 0, 0, 65535);
594 }
595 if (this->mode == MODE_TRANSPORT && this->udp &&
596 (!tsi->is_host(tsi, hsi) || !tsr->is_host(tsr, hsr)))
597 { /* change TS in case of a NAT in transport mode */
598 DBG2(DBG_IKE, "changing received traffic selectors %R=== %R due to NAT",
599 tsi, tsr);
600 tsi->set_address(tsi, hsi);
601 tsr->set_address(tsr, hsr);
602 }
603
604 if (this->initiator)
605 {
606 traffic_selector_t *tsisub, *tsrsub;
607
608 /* check if peer selection is valid */
609 tsisub = this->tsi->get_subset(this->tsi, tsi);
610 tsrsub = this->tsr->get_subset(this->tsr, tsr);
611 if (!tsisub || !tsrsub)
612 {
613 DBG1(DBG_IKE, "peer selected invalid traffic selectors: "
614 "%R for %R, %R for %R", tsi, this->tsi, tsr, this->tsr);
615 DESTROY_IF(tsisub);
616 DESTROY_IF(tsrsub);
617 tsi->destroy(tsi);
618 tsr->destroy(tsr);
619 return FALSE;
620 }
621 tsi->destroy(tsi);
622 tsr->destroy(tsr);
623 this->tsi->destroy(this->tsi);
624 this->tsr->destroy(this->tsr);
625 this->tsi = tsisub;
626 this->tsr = tsrsub;
627 }
628 else
629 {
630 this->tsi = tsi;
631 this->tsr = tsr;
632 }
633 return TRUE;
634 }
635
636 /**
637 * Get encap
638 */
639 static encap_t get_encap(ike_sa_t* ike_sa, bool udp)
640 {
641 if (!udp)
642 {
643 return ENCAP_NONE;
644 }
645 if (ike_sa->supports_extension(ike_sa, EXT_NATT_DRAFT_02_03))
646 {
647 return ENCAP_UDP_DRAFT_00_03;
648 }
649 return ENCAP_UDP;
650 }
651
652 /**
653 * Get NAT-OA payload type (RFC 3947 or RFC 3947 drafts).
654 */
655 static payload_type_t get_nat_oa_payload_type(ike_sa_t *ike_sa)
656 {
657 if (ike_sa->supports_extension(ike_sa, EXT_NATT_DRAFT_02_03))
658 {
659 return PLV1_NAT_OA_DRAFT_00_03;
660 }
661 return PLV1_NAT_OA;
662 }
663
664 /**
665 * Add NAT-OA payloads
666 */
667 static void add_nat_oa_payloads(private_quick_mode_t *this, message_t *message)
668 {
669 identification_t *id;
670 id_payload_t *nat_oa;
671 host_t *src, *dst;
672 payload_type_t nat_oa_payload_type;
673
674 src = message->get_source(message);
675 dst = message->get_destination(message);
676
677 src = this->initiator ? src : dst;
678 dst = this->initiator ? dst : src;
679
680 nat_oa_payload_type = get_nat_oa_payload_type(this->ike_sa);
681
682 /* first NAT-OA is the initiator's address */
683 id = identification_create_from_sockaddr(src->get_sockaddr(src));
684 nat_oa = id_payload_create_from_identification(nat_oa_payload_type, id);
685 message->add_payload(message, (payload_t*)nat_oa);
686 id->destroy(id);
687
688 /* second NAT-OA is that of the responder */
689 id = identification_create_from_sockaddr(dst->get_sockaddr(dst));
690 nat_oa = id_payload_create_from_identification(nat_oa_payload_type, id);
691 message->add_payload(message, (payload_t*)nat_oa);
692 id->destroy(id);
693 }
694
695 /**
696 * Look up lifetimes
697 */
698 static void get_lifetimes(private_quick_mode_t *this)
699 {
700 lifetime_cfg_t *lft;
701
702 lft = this->config->get_lifetime(this->config);
703 if (lft->time.life)
704 {
705 this->lifetime = lft->time.life;
706 }
707 else if (lft->bytes.life)
708 {
709 this->lifebytes = lft->bytes.life;
710 }
711 free(lft);
712 }
713
714 /**
715 * Check and apply lifetimes
716 */
717 static void apply_lifetimes(private_quick_mode_t *this, sa_payload_t *sa_payload)
718 {
719 u_int32_t lifetime;
720 u_int64_t lifebytes;
721
722 lifetime = sa_payload->get_lifetime(sa_payload);
723 lifebytes = sa_payload->get_lifebytes(sa_payload);
724 if (this->lifetime != lifetime)
725 {
726 DBG1(DBG_IKE, "received %us lifetime, configured %us",
727 lifetime, this->lifetime);
728 this->lifetime = lifetime;
729 }
730 if (this->lifebytes != lifebytes)
731 {
732 DBG1(DBG_IKE, "received %llu lifebytes, configured %llu",
733 lifebytes, this->lifebytes);
734 this->lifebytes = lifebytes;
735 }
736 }
737
738 /**
739 * Set the task ready to build notify error message
740 */
741 static status_t send_notify(private_quick_mode_t *this, notify_type_t type)
742 {
743 notify_payload_t *notify;
744
745 notify = notify_payload_create_from_protocol_and_type(PLV1_NOTIFY,
746 this->proto, type);
747 notify->set_spi(notify, this->spi_i);
748
749 this->ike_sa->queue_task(this->ike_sa,
750 (task_t*)informational_create(this->ike_sa, notify));
751 /* cancel all active/passive tasks in favour of informational */
752 this->ike_sa->flush_queue(this->ike_sa,
753 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
754 return ALREADY_DONE;
755 }
756
757 /**
758 * Prepare a list of proposals from child_config containing only the specified
759 * DH group, unless it is set to MODP_NONE.
760 */
761 static linked_list_t *get_proposals(private_quick_mode_t *this,
762 diffie_hellman_group_t group)
763 {
764 linked_list_t *list;
765 proposal_t *proposal;
766 enumerator_t *enumerator;
767
768 list = this->config->get_proposals(this->config, FALSE);
769 enumerator = list->create_enumerator(list);
770 while (enumerator->enumerate(enumerator, &proposal))
771 {
772 if (group != MODP_NONE)
773 {
774 if (!proposal->has_dh_group(proposal, group))
775 {
776 list->remove_at(list, enumerator);
777 proposal->destroy(proposal);
778 continue;
779 }
780 proposal->strip_dh(proposal, group);
781 }
782 proposal->set_spi(proposal, this->spi_i);
783 }
784 enumerator->destroy(enumerator);
785
786 return list;
787 }
788
789 METHOD(task_t, build_i, status_t,
790 private_quick_mode_t *this, message_t *message)
791 {
792 switch (this->state)
793 {
794 case QM_INIT:
795 {
796 sa_payload_t *sa_payload;
797 linked_list_t *list, *tsi, *tsr;
798 proposal_t *proposal;
799 diffie_hellman_group_t group;
800 encap_t encap;
801
802 this->udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY);
803 this->mode = this->config->get_mode(this->config);
804 this->child_sa = child_sa_create(
805 this->ike_sa->get_my_host(this->ike_sa),
806 this->ike_sa->get_other_host(this->ike_sa),
807 this->config, this->reqid, this->udp,
808 this->mark_in, this->mark_out);
809
810 if (this->udp && this->mode == MODE_TRANSPORT)
811 {
812 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
813 add_nat_oa_payloads(this, message);
814 }
815
816 if (this->config->use_ipcomp(this->config))
817 {
818 this->cpi_i = this->child_sa->alloc_cpi(this->child_sa);
819 if (!this->cpi_i)
820 {
821 DBG1(DBG_IKE, "unable to allocate a CPI from kernel, "
822 "IPComp disabled");
823 }
824 }
825
826 list = this->config->get_proposals(this->config, MODP_NONE);
827 if (list->get_first(list, (void**)&proposal) == SUCCESS)
828 {
829 this->proto = proposal->get_protocol(proposal);
830 }
831 list->destroy_offset(list, offsetof(proposal_t, destroy));
832 this->spi_i = this->child_sa->alloc_spi(this->child_sa, this->proto);
833 if (!this->spi_i)
834 {
835 DBG1(DBG_IKE, "allocating SPI from kernel failed");
836 return FAILED;
837 }
838
839 group = this->config->get_dh_group(this->config);
840 if (group != MODP_NONE)
841 {
842 proposal_t *proposal;
843 u_int16_t preferred_group;
844
845 proposal = this->ike_sa->get_proposal(this->ike_sa);
846 proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
847 &preferred_group, NULL);
848 /* try the negotiated DH group from IKE_SA */
849 list = get_proposals(this, preferred_group);
850 if (list->get_count(list))
851 {
852 group = preferred_group;
853 }
854 else
855 {
856 /* fall back to the first configured DH group */
857 list->destroy(list);
858 list = get_proposals(this, group);
859 }
860
861 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
862 group);
863 if (!this->dh)
864 {
865 DBG1(DBG_IKE, "configured DH group %N not supported",
866 diffie_hellman_group_names, group);
867 list->destroy_offset(list, offsetof(proposal_t, destroy));
868 return FAILED;
869 }
870 }
871 else
872 {
873 list = get_proposals(this, MODP_NONE);
874 }
875
876 get_lifetimes(this);
877 encap = get_encap(this->ike_sa, this->udp);
878 sa_payload = sa_payload_create_from_proposals_v1(list,
879 this->lifetime, this->lifebytes, AUTH_NONE,
880 this->mode, encap, this->cpi_i);
881 list->destroy_offset(list, offsetof(proposal_t, destroy));
882 message->add_payload(message, &sa_payload->payload_interface);
883
884 if (!add_nonce(this, &this->nonce_i, message))
885 {
886 return FAILED;
887 }
888 if (group != MODP_NONE)
889 {
890 if (!add_ke(this, message))
891 {
892 return FAILED;
893 }
894 }
895 if (!this->tsi)
896 {
897 this->tsi = select_ts(this, TRUE, NULL);
898 }
899 if (!this->tsr)
900 {
901 this->tsr = select_ts(this, FALSE, NULL);
902 }
903 tsi = linked_list_create_with_items(this->tsi, NULL);
904 tsr = linked_list_create_with_items(this->tsr, NULL);
905 this->tsi = this->tsr = NULL;
906 charon->bus->narrow(charon->bus, this->child_sa,
907 NARROW_INITIATOR_PRE_AUTH, tsi, tsr);
908 tsi->remove_first(tsi, (void**)&this->tsi);
909 tsr->remove_first(tsr, (void**)&this->tsr);
910 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
911 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
912 if (!this->tsi || !this->tsr)
913 {
914 return FAILED;
915 }
916 add_ts(this, message);
917 return NEED_MORE;
918 }
919 case QM_NEGOTIATED:
920 {
921 return SUCCESS;
922 }
923 default:
924 return FAILED;
925 }
926 }
927
928 /**
929 * Check for notify errors, return TRUE if error found
930 */
931 static bool has_notify_errors(private_quick_mode_t *this, message_t *message)
932 {
933 enumerator_t *enumerator;
934 payload_t *payload;
935 bool err = FALSE;
936
937 enumerator = message->create_payload_enumerator(message);
938 while (enumerator->enumerate(enumerator, &payload))
939 {
940 if (payload->get_type(payload) == PLV1_NOTIFY)
941 {
942 notify_payload_t *notify;
943 notify_type_t type;
944
945 notify = (notify_payload_t*)payload;
946 type = notify->get_notify_type(notify);
947 if (type < 16384)
948 {
949
950 DBG1(DBG_IKE, "received %N error notify",
951 notify_type_names, type);
952 err = TRUE;
953 }
954 else
955 {
956 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
957 }
958 }
959 }
960 enumerator->destroy(enumerator);
961
962 return err;
963 }
964
965 /**
966 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
967 */
968 static void check_for_rekeyed_child(private_quick_mode_t *this)
969 {
970 enumerator_t *enumerator, *policies;
971 traffic_selector_t *local, *remote;
972 child_sa_t *child_sa;
973 proposal_t *proposal;
974 char *name;
975
976 name = this->config->get_name(this->config);
977 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
978 while (this->reqid == 0 && enumerator->enumerate(enumerator, &child_sa))
979 {
980 if (streq(child_sa->get_name(child_sa), name))
981 {
982 proposal = child_sa->get_proposal(child_sa);
983 switch (child_sa->get_state(child_sa))
984 {
985 case CHILD_INSTALLED:
986 case CHILD_REKEYING:
987 policies = child_sa->create_policy_enumerator(child_sa);
988 if (policies->enumerate(policies, &local, &remote) &&
989 local->equals(local, this->tsr) &&
990 remote->equals(remote, this->tsi) &&
991 this->proposal->equals(this->proposal, proposal))
992 {
993 this->reqid = child_sa->get_reqid(child_sa);
994 this->rekey = child_sa->get_spi(child_sa, TRUE);
995 this->mark_in = child_sa->get_mark(child_sa,
996 TRUE).value;
997 this->mark_out = child_sa->get_mark(child_sa,
998 FALSE).value;
999 child_sa->set_state(child_sa, CHILD_REKEYING);
1000 DBG1(DBG_IKE, "detected rekeying of CHILD_SA %s{%u}",
1001 child_sa->get_name(child_sa),
1002 child_sa->get_unique_id(child_sa));
1003 }
1004 policies->destroy(policies);
1005 break;
1006 default:
1007 break;
1008 }
1009 }
1010 }
1011 enumerator->destroy(enumerator);
1012 }
1013
1014 METHOD(task_t, process_r, status_t,
1015 private_quick_mode_t *this, message_t *message)
1016 {
1017 switch (this->state)
1018 {
1019 case QM_INIT:
1020 {
1021 sa_payload_t *sa_payload;
1022 linked_list_t *tsi, *tsr, *hostsi, *hostsr, *list = NULL;
1023 peer_cfg_t *peer_cfg;
1024 u_int16_t group;
1025 bool private;
1026
1027 sa_payload = (sa_payload_t*)message->get_payload(message,
1028 PLV1_SECURITY_ASSOCIATION);
1029 if (!sa_payload)
1030 {
1031 DBG1(DBG_IKE, "sa payload missing");
1032 return send_notify(this, INVALID_PAYLOAD_TYPE);
1033 }
1034
1035 this->mode = sa_payload->get_encap_mode(sa_payload, &this->udp);
1036
1037 if (!get_ts(this, message))
1038 {
1039 return FAILED;
1040 }
1041 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1042 tsi = linked_list_create_with_items(this->tsi, NULL);
1043 tsr = linked_list_create_with_items(this->tsr, NULL);
1044 this->tsi = this->tsr = NULL;
1045 hostsi = get_dynamic_hosts(this->ike_sa, FALSE);
1046 hostsr = get_dynamic_hosts(this->ike_sa, TRUE);
1047 this->config = peer_cfg->select_child_cfg(peer_cfg, tsr, tsi,
1048 hostsr, hostsi);
1049 hostsi->destroy(hostsi);
1050 hostsr->destroy(hostsr);
1051 if (this->config)
1052 {
1053 this->tsi = select_ts(this, FALSE, tsi);
1054 this->tsr = select_ts(this, TRUE, tsr);
1055 }
1056 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
1057 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
1058 if (!this->config || !this->tsi || !this->tsr ||
1059 this->mode != this->config->get_mode(this->config))
1060 {
1061 DBG1(DBG_IKE, "no matching CHILD_SA config found");
1062 return send_notify(this, INVALID_ID_INFORMATION);
1063 }
1064
1065 if (this->config->use_ipcomp(this->config))
1066 {
1067 list = sa_payload->get_ipcomp_proposals(sa_payload,
1068 &this->cpi_i);
1069 if (!list->get_count(list))
1070 {
1071 DBG1(DBG_IKE, "expected IPComp proposal but peer did "
1072 "not send one, IPComp disabled");
1073 this->cpi_i = 0;
1074 }
1075 }
1076 if (!list || !list->get_count(list))
1077 {
1078 DESTROY_IF(list);
1079 list = sa_payload->get_proposals(sa_payload);
1080 }
1081 private = this->ike_sa->supports_extension(this->ike_sa,
1082 EXT_STRONGSWAN);
1083 this->proposal = this->config->select_proposal(this->config,
1084 list, FALSE, private);
1085 list->destroy_offset(list, offsetof(proposal_t, destroy));
1086
1087 get_lifetimes(this);
1088 apply_lifetimes(this, sa_payload);
1089
1090 if (!this->proposal)
1091 {
1092 DBG1(DBG_IKE, "no matching proposal found, sending %N",
1093 notify_type_names, NO_PROPOSAL_CHOSEN);
1094 return send_notify(this, NO_PROPOSAL_CHOSEN);
1095 }
1096 this->spi_i = this->proposal->get_spi(this->proposal);
1097
1098 if (!get_nonce(this, &this->nonce_i, message))
1099 {
1100 return send_notify(this, INVALID_PAYLOAD_TYPE);
1101 }
1102
1103 if (this->proposal->get_algorithm(this->proposal,
1104 DIFFIE_HELLMAN_GROUP, &group, NULL))
1105 {
1106 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
1107 group);
1108 if (!this->dh)
1109 {
1110 DBG1(DBG_IKE, "negotiated DH group %N not supported",
1111 diffie_hellman_group_names, group);
1112 return send_notify(this, INVALID_KEY_INFORMATION);
1113 }
1114 if (!get_ke(this, message))
1115 {
1116 return send_notify(this, INVALID_PAYLOAD_TYPE);
1117 }
1118 }
1119
1120 check_for_rekeyed_child(this);
1121
1122 this->child_sa = child_sa_create(
1123 this->ike_sa->get_my_host(this->ike_sa),
1124 this->ike_sa->get_other_host(this->ike_sa),
1125 this->config, this->reqid, this->udp,
1126 this->mark_in, this->mark_out);
1127
1128 tsi = linked_list_create_with_items(this->tsi, NULL);
1129 tsr = linked_list_create_with_items(this->tsr, NULL);
1130 this->tsi = this->tsr = NULL;
1131 charon->bus->narrow(charon->bus, this->child_sa,
1132 NARROW_RESPONDER, tsr, tsi);
1133 if (tsi->remove_first(tsi, (void**)&this->tsi) != SUCCESS ||
1134 tsr->remove_first(tsr, (void**)&this->tsr) != SUCCESS)
1135 {
1136 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
1137 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
1138 return send_notify(this, INVALID_ID_INFORMATION);
1139 }
1140 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
1141 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
1142
1143 return NEED_MORE;
1144 }
1145 case QM_NEGOTIATED:
1146 {
1147 if (has_notify_errors(this, message))
1148 {
1149 return SUCCESS;
1150 }
1151 if (message->get_exchange_type(message) == INFORMATIONAL_V1)
1152 {
1153 if (message->get_payload(message, PLV1_DELETE))
1154 {
1155 /* If the DELETE for a Quick Mode follows immediately
1156 * after rekeying, we might receive it before the
1157 * third completing Quick Mode message. Ignore it, as
1158 * it gets handled by a separately queued delete task. */
1159 return NEED_MORE;
1160 }
1161 return SUCCESS;
1162 }
1163 if (!install(this))
1164 {
1165 ike_sa_t *ike_sa = this->ike_sa;
1166 task_t *task;
1167
1168 task = (task_t*)quick_delete_create(this->ike_sa,
1169 this->proposal->get_protocol(this->proposal),
1170 this->spi_i, TRUE, TRUE);
1171 /* flush_queue() destroys the current task */
1172 ike_sa->flush_queue(ike_sa, TASK_QUEUE_PASSIVE);
1173 ike_sa->queue_task(ike_sa, task);
1174 return ALREADY_DONE;
1175 }
1176 return SUCCESS;
1177 }
1178 default:
1179 return FAILED;
1180 }
1181 }
1182
1183 METHOD(task_t, build_r, status_t,
1184 private_quick_mode_t *this, message_t *message)
1185 {
1186 switch (this->state)
1187 {
1188 case QM_INIT:
1189 {
1190 sa_payload_t *sa_payload;
1191 encap_t encap;
1192
1193 this->proto = this->proposal->get_protocol(this->proposal);
1194 this->spi_r = this->child_sa->alloc_spi(this->child_sa, this->proto);
1195 if (!this->spi_r)
1196 {
1197 DBG1(DBG_IKE, "allocating SPI from kernel failed");
1198 return send_notify(this, NO_PROPOSAL_CHOSEN);
1199 }
1200 this->proposal->set_spi(this->proposal, this->spi_r);
1201
1202 if (this->cpi_i)
1203 {
1204 this->cpi_r = this->child_sa->alloc_cpi(this->child_sa);
1205 if (!this->cpi_r)
1206 {
1207 DBG1(DBG_IKE, "unable to allocate a CPI from "
1208 "kernel, IPComp disabled");
1209 return send_notify(this, NO_PROPOSAL_CHOSEN);
1210 }
1211 }
1212
1213 if (this->udp && this->mode == MODE_TRANSPORT)
1214 {
1215 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1216 add_nat_oa_payloads(this, message);
1217 }
1218
1219 encap = get_encap(this->ike_sa, this->udp);
1220 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
1221 this->lifetime, this->lifebytes, AUTH_NONE,
1222 this->mode, encap, this->cpi_r);
1223 message->add_payload(message, &sa_payload->payload_interface);
1224
1225 if (!add_nonce(this, &this->nonce_r, message))
1226 {
1227 return FAILED;
1228 }
1229 if (this->dh)
1230 {
1231 if (!add_ke(this, message))
1232 {
1233 return FAILED;
1234 }
1235 }
1236
1237 add_ts(this, message);
1238
1239 this->state = QM_NEGOTIATED;
1240 return NEED_MORE;
1241 }
1242 case QM_NEGOTIATED:
1243 if (message->get_exchange_type(message) == INFORMATIONAL_V1)
1244 {
1245 /* skip INFORMATIONAL response if we received a INFORMATIONAL
1246 * delete, see process_r() */
1247 return ALREADY_DONE;
1248 }
1249 /* fall */
1250 default:
1251 return FAILED;
1252 }
1253 }
1254
1255 METHOD(task_t, process_i, status_t,
1256 private_quick_mode_t *this, message_t *message)
1257 {
1258 switch (this->state)
1259 {
1260 case QM_INIT:
1261 {
1262 sa_payload_t *sa_payload;
1263 linked_list_t *list = NULL;
1264 bool private;
1265
1266 sa_payload = (sa_payload_t*)message->get_payload(message,
1267 PLV1_SECURITY_ASSOCIATION);
1268 if (!sa_payload)
1269 {
1270 DBG1(DBG_IKE, "sa payload missing");
1271 return send_notify(this, NO_PROPOSAL_CHOSEN);
1272 }
1273 if (this->cpi_i)
1274 {
1275 list = sa_payload->get_ipcomp_proposals(sa_payload,
1276 &this->cpi_r);
1277 if (!list->get_count(list))
1278 {
1279 DBG1(DBG_IKE, "peer did not acccept our IPComp proposal, "
1280 "IPComp disabled");
1281 this->cpi_i = 0;
1282 }
1283 }
1284 if (!list || !list->get_count(list))
1285 {
1286 DESTROY_IF(list);
1287 list = sa_payload->get_proposals(sa_payload);
1288 }
1289 private = this->ike_sa->supports_extension(this->ike_sa,
1290 EXT_STRONGSWAN);
1291 this->proposal = this->config->select_proposal(this->config,
1292 list, FALSE, private);
1293 list->destroy_offset(list, offsetof(proposal_t, destroy));
1294 if (!this->proposal)
1295 {
1296 DBG1(DBG_IKE, "no matching proposal found");
1297 return send_notify(this, NO_PROPOSAL_CHOSEN);
1298 }
1299 this->spi_r = this->proposal->get_spi(this->proposal);
1300
1301 apply_lifetimes(this, sa_payload);
1302
1303 if (!get_nonce(this, &this->nonce_r, message))
1304 {
1305 return send_notify(this, INVALID_PAYLOAD_TYPE);
1306 }
1307 if (this->dh && !get_ke(this, message))
1308 {
1309 return send_notify(this, INVALID_KEY_INFORMATION);
1310 }
1311 if (!get_ts(this, message))
1312 {
1313 return send_notify(this, INVALID_PAYLOAD_TYPE);
1314 }
1315 if (!install(this))
1316 {
1317 return send_notify(this, NO_PROPOSAL_CHOSEN);
1318 }
1319 this->state = QM_NEGOTIATED;
1320 return NEED_MORE;
1321 }
1322 default:
1323 return FAILED;
1324 }
1325 }
1326
1327 METHOD(task_t, get_type, task_type_t,
1328 private_quick_mode_t *this)
1329 {
1330 return TASK_QUICK_MODE;
1331 }
1332
1333 METHOD(quick_mode_t, use_reqid, void,
1334 private_quick_mode_t *this, u_int32_t reqid)
1335 {
1336 this->reqid = reqid;
1337 }
1338
1339 METHOD(quick_mode_t, use_marks, void,
1340 private_quick_mode_t *this, u_int in, u_int out)
1341 {
1342 this->mark_in = in;
1343 this->mark_out = out;
1344 }
1345
1346 METHOD(quick_mode_t, rekey, void,
1347 private_quick_mode_t *this, u_int32_t spi)
1348 {
1349 this->rekey = spi;
1350 }
1351
1352 METHOD(task_t, migrate, void,
1353 private_quick_mode_t *this, ike_sa_t *ike_sa)
1354 {
1355 chunk_free(&this->nonce_i);
1356 chunk_free(&this->nonce_r);
1357 DESTROY_IF(this->tsi);
1358 DESTROY_IF(this->tsr);
1359 DESTROY_IF(this->proposal);
1360 DESTROY_IF(this->child_sa);
1361 DESTROY_IF(this->dh);
1362
1363 this->ike_sa = ike_sa;
1364 this->keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
1365 this->state = QM_INIT;
1366 this->tsi = NULL;
1367 this->tsr = NULL;
1368 this->proposal = NULL;
1369 this->child_sa = NULL;
1370 this->dh = NULL;
1371 this->spi_i = 0;
1372 this->spi_r = 0;
1373 this->mark_in = 0;
1374 this->mark_out = 0;
1375
1376 if (!this->initiator)
1377 {
1378 DESTROY_IF(this->config);
1379 this->config = NULL;
1380 }
1381 }
1382
1383 METHOD(task_t, destroy, void,
1384 private_quick_mode_t *this)
1385 {
1386 chunk_free(&this->nonce_i);
1387 chunk_free(&this->nonce_r);
1388 DESTROY_IF(this->tsi);
1389 DESTROY_IF(this->tsr);
1390 DESTROY_IF(this->proposal);
1391 DESTROY_IF(this->child_sa);
1392 DESTROY_IF(this->config);
1393 DESTROY_IF(this->dh);
1394 free(this);
1395 }
1396
1397 /*
1398 * Described in header.
1399 */
1400 quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config,
1401 traffic_selector_t *tsi, traffic_selector_t *tsr)
1402 {
1403 private_quick_mode_t *this;
1404
1405 INIT(this,
1406 .public = {
1407 .task = {
1408 .get_type = _get_type,
1409 .migrate = _migrate,
1410 .destroy = _destroy,
1411 },
1412 .use_reqid = _use_reqid,
1413 .use_marks = _use_marks,
1414 .rekey = _rekey,
1415 },
1416 .ike_sa = ike_sa,
1417 .initiator = config != NULL,
1418 .config = config,
1419 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
1420 .state = QM_INIT,
1421 .tsi = tsi ? tsi->clone(tsi) : NULL,
1422 .tsr = tsr ? tsr->clone(tsr) : NULL,
1423 .proto = PROTO_ESP,
1424 );
1425
1426 if (config)
1427 {
1428 this->public.task.build = _build_i;
1429 this->public.task.process = _process_i;
1430 }
1431 else
1432 {
1433 this->public.task.build = _build_r;
1434 this->public.task.process = _process_r;
1435 }
1436
1437 return &this->public;
1438 }