2 * Copyright (C) 2012-2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
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>.
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
20 * Copyright (C) 2012 Volker RĂ¼melin
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:
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
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
41 #include "quick_mode.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>
56 typedef struct private_quick_mode_t private_quick_mode_t
;
59 * Private members of a quick_mode_t task.
61 struct private_quick_mode_t
{
64 * Public methods and task_t interface.
74 * TRUE if we are initiating quick mode
79 * Traffic selector of initiator
81 traffic_selector_t
*tsi
;
84 * Traffic selector of responder
86 traffic_selector_t
*tsr
;
109 * Initiators IPComp CPI
114 * Responders IPComp CPI
119 * selected CHILD_SA proposal
121 proposal_t
*proposal
;
124 * Config of CHILD_SA to establish
129 * CHILD_SA we are about to establish
131 child_sa_t
*child_sa
;
139 * DH exchange, when PFS is in use
141 diffie_hellman_t
*dh
;
144 * Negotiated lifetime of new SA
149 * Negotiated lifebytes of new SA
154 * Reqid to use, 0 for auto-allocate
159 * Explicit inbound mark value to use, if any
164 * Explicit inbound mark value to use, if any
174 * Delete old child after successful rekey
179 * Negotiated mode, tunnel or transport
184 * SA protocol (ESP|AH) negotiated
189 * Use UDP encapsulation
194 * Message ID of handled quick mode exchange
198 /** states of quick mode */
206 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
208 static void schedule_inactivity_timeout(private_quick_mode_t
*this)
213 timeout
= this->config
->get_inactivity(this->config
);
216 close_ike
= lib
->settings
->get_bool(lib
->settings
,
217 "%s.inactivity_close_ike", FALSE
, lib
->ns
);
218 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
219 inactivity_job_create(this->child_sa
->get_unique_id(this->child_sa
),
220 timeout
, close_ike
), timeout
);
225 * Check if we have a an address pool configured
227 static bool have_pool(ike_sa_t
*ike_sa
)
229 enumerator_t
*enumerator
;
230 peer_cfg_t
*peer_cfg
;
234 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
237 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
238 if (enumerator
->enumerate(enumerator
, &pool
))
242 enumerator
->destroy(enumerator
);
248 * Get hosts to use for dynamic traffic selectors
250 static linked_list_t
*get_dynamic_hosts(ike_sa_t
*ike_sa
, bool local
)
252 enumerator_t
*enumerator
;
256 list
= linked_list_create();
257 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
258 while (enumerator
->enumerate(enumerator
, &host
))
260 list
->insert_last(list
, host
);
262 enumerator
->destroy(enumerator
);
264 if (list
->get_count(list
) == 0)
265 { /* no virtual IPs assigned */
268 host
= ike_sa
->get_my_host(ike_sa
);
269 list
->insert_last(list
, host
);
271 else if (!have_pool(ike_sa
))
272 { /* use host only if we don't have a pool configured */
273 host
= ike_sa
->get_other_host(ike_sa
);
274 list
->insert_last(list
, host
);
281 * Install negotiated CHILD_SA
283 static bool install(private_quick_mode_t
*this)
285 status_t status
, status_i
, status_o
;
286 chunk_t encr_i
, encr_r
, integ_i
, integ_r
;
287 linked_list_t
*tsi
, *tsr
, *my_ts
, *other_ts
;
288 child_sa_t
*old
= NULL
;
290 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
291 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
292 this->child_sa
->set_mode(this->child_sa
, this->mode
);
294 if (this->cpi_i
&& this->cpi_r
)
295 { /* DEFLATE is the only transform we currently support */
296 this->child_sa
->set_ipcomp(this->child_sa
, IPCOMP_DEFLATE
);
300 this->cpi_i
= this->cpi_r
= 0;
303 this->child_sa
->set_protocol(this->child_sa
,
304 this->proposal
->get_protocol(this->proposal
));
306 status_i
= status_o
= FAILED
;
307 encr_i
= encr_r
= integ_i
= integ_r
= chunk_empty
;
308 tsi
= linked_list_create_with_items(this->tsi
->clone(this->tsi
), NULL
);
309 tsr
= linked_list_create_with_items(this->tsr
->clone(this->tsr
), NULL
);
312 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
313 NARROW_INITIATOR_POST_AUTH
, tsi
, tsr
);
317 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
318 NARROW_RESPONDER_POST
, tsr
, tsi
);
320 if (tsi
->get_count(tsi
) == 0 || tsr
->get_count(tsr
) == 0)
322 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
323 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
324 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
330 this->child_sa
->set_policies(this->child_sa
, tsi
, tsr
);
334 this->child_sa
->set_policies(this->child_sa
, tsr
, tsi
);
336 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
337 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
339 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
, this->dh
,
340 this->spi_i
, this->spi_r
, this->nonce_i
, this->nonce_r
,
341 &encr_i
, &integ_i
, &encr_r
, &integ_r
))
345 status_i
= this->child_sa
->install(this->child_sa
,
346 encr_r
, integ_r
, this->spi_i
, this->cpi_i
,
347 this->initiator
, TRUE
, FALSE
);
348 status_o
= this->child_sa
->install(this->child_sa
,
349 encr_i
, integ_i
, this->spi_r
, this->cpi_r
,
350 this->initiator
, FALSE
, FALSE
);
354 status_i
= this->child_sa
->install(this->child_sa
,
355 encr_i
, integ_i
, this->spi_r
, this->cpi_r
,
356 this->initiator
, TRUE
, FALSE
);
357 status_o
= this->child_sa
->install(this->child_sa
,
358 encr_r
, integ_r
, this->spi_i
, this->cpi_i
,
359 this->initiator
, FALSE
, FALSE
);
363 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
365 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
366 (status_i
!= SUCCESS
) ?
"inbound " : "",
367 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ?
"and ": "",
368 (status_o
!= SUCCESS
) ?
"outbound " : "");
373 status
= this->child_sa
->install_policies(this->child_sa
);
375 if (status
!= SUCCESS
)
377 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
381 charon
->bus
->child_derived_keys(charon
->bus
, this->child_sa
,
382 this->initiator
, encr_i
, encr_r
,
386 chunk_clear(&integ_i
);
387 chunk_clear(&integ_r
);
388 chunk_clear(&encr_i
);
389 chunk_clear(&encr_r
);
391 if (status
!= SUCCESS
)
396 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
397 this->dh
, this->nonce_i
, this->nonce_r
);
399 my_ts
= linked_list_create_from_enumerator(
400 this->child_sa
->create_ts_enumerator(this->child_sa
, TRUE
));
401 other_ts
= linked_list_create_from_enumerator(
402 this->child_sa
->create_ts_enumerator(this->child_sa
, FALSE
));
404 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
405 "with SPIs %.8x_i %.8x_o and TS %#R === %#R",
406 this->child_sa
->get_name(this->child_sa
),
407 this->child_sa
->get_unique_id(this->child_sa
),
408 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
409 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)), my_ts
, other_ts
);
411 my_ts
->destroy(my_ts
);
412 other_ts
->destroy(other_ts
);
414 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
415 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
419 old
= this->ike_sa
->get_child_sa(this->ike_sa
,
420 this->proposal
->get_protocol(this->proposal
),
425 charon
->bus
->child_rekey(charon
->bus
, old
, this->child_sa
);
426 /* rekeyed CHILD_SAs stay installed until they expire or are deleted
427 * by the other peer */
428 old
->set_state(old
, CHILD_REKEYED
);
429 /* as initiator we delete the CHILD_SA if configured to do so */
430 if (this->initiator
&& this->delete)
432 this->ike_sa
->queue_task(this->ike_sa
,
433 (task_t
*)quick_delete_create(this->ike_sa
,
434 this->proposal
->get_protocol(this->proposal
),
435 this->rekey
, TRUE
, FALSE
));
440 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
442 schedule_inactivity_timeout(this);
443 this->child_sa
= NULL
;
448 * Generate and add NONCE
450 static bool add_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
453 nonce_payload_t
*nonce_payload
;
456 nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
459 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
462 if (!nonceg
->allocate_nonce(nonceg
, NONCE_SIZE
, nonce
))
464 DBG1(DBG_IKE
, "nonce allocation failed");
465 nonceg
->destroy(nonceg
);
468 nonceg
->destroy(nonceg
);
470 nonce_payload
= nonce_payload_create(PLV1_NONCE
);
471 nonce_payload
->set_nonce(nonce_payload
, *nonce
);
472 message
->add_payload(message
, &nonce_payload
->payload_interface
);
478 * Extract nonce from NONCE payload
480 static bool get_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
483 nonce_payload_t
*nonce_payload
;
485 nonce_payload
= (nonce_payload_t
*)message
->get_payload(message
, PLV1_NONCE
);
488 DBG1(DBG_IKE
, "NONCE payload missing in message");
491 *nonce
= nonce_payload
->get_nonce(nonce_payload
);
497 * Add KE payload to message
499 static bool add_ke(private_quick_mode_t
*this, message_t
*message
)
501 ke_payload_t
*ke_payload
;
503 ke_payload
= ke_payload_create_from_diffie_hellman(PLV1_KEY_EXCHANGE
,
507 DBG1(DBG_IKE
, "creating KE payload failed");
510 message
->add_payload(message
, &ke_payload
->payload_interface
);
515 * Get DH value from a KE payload
517 static bool get_ke(private_quick_mode_t
*this, message_t
*message
)
519 ke_payload_t
*ke_payload
;
521 ke_payload
= (ke_payload_t
*)message
->get_payload(message
, PLV1_KEY_EXCHANGE
);
524 DBG1(DBG_IKE
, "KE payload missing");
527 if (!this->dh
->set_other_public_value(this->dh
,
528 ke_payload
->get_key_exchange_data(ke_payload
)))
530 DBG1(DBG_IKE
, "unable to apply received KE value");
537 * Select a traffic selector from configuration
539 static traffic_selector_t
* select_ts(private_quick_mode_t
*this, bool local
,
540 linked_list_t
*supplied
)
542 traffic_selector_t
*ts
;
543 linked_list_t
*list
, *hosts
;
545 hosts
= get_dynamic_hosts(this->ike_sa
, local
);
546 list
= this->config
->get_traffic_selectors(this->config
,
547 local
, supplied
, hosts
);
548 hosts
->destroy(hosts
);
549 if (list
->get_first(list
, (void**)&ts
) == SUCCESS
)
555 DBG1(DBG_IKE
, "%s traffic selector missing in configuration",
556 local ?
"local" : "remote");
559 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
564 * Add selected traffic selectors to message
566 static void add_ts(private_quick_mode_t
*this, message_t
*message
)
568 id_payload_t
*id_payload
;
570 id_payload
= id_payload_create_from_ts(this->tsi
);
571 message
->add_payload(message
, &id_payload
->payload_interface
);
572 id_payload
= id_payload_create_from_ts(this->tsr
);
573 message
->add_payload(message
, &id_payload
->payload_interface
);
577 * Get traffic selectors from received message
579 static bool get_ts(private_quick_mode_t
*this, message_t
*message
)
581 traffic_selector_t
*tsi
= NULL
, *tsr
= NULL
;
582 enumerator_t
*enumerator
;
583 id_payload_t
*id_payload
;
588 enumerator
= message
->create_payload_enumerator(message
);
589 while (enumerator
->enumerate(enumerator
, &payload
))
591 if (payload
->get_type(payload
) == PLV1_ID
)
593 id_payload
= (id_payload_t
*)payload
;
597 tsi
= id_payload
->get_ts(id_payload
);
602 tsr
= id_payload
->get_ts(id_payload
);
607 enumerator
->destroy(enumerator
);
609 /* create host2host selectors if ID payloads missing */
612 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
613 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
617 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
618 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
622 tsi
= traffic_selector_create_from_subnet(hsi
->clone(hsi
),
623 hsi
->get_family(hsi
) == AF_INET ?
32 : 128, 0, 0, 65535);
627 tsr
= traffic_selector_create_from_subnet(hsr
->clone(hsr
),
628 hsr
->get_family(hsr
) == AF_INET ?
32 : 128, 0, 0, 65535);
630 if (this->mode
== MODE_TRANSPORT
&& this->udp
&&
631 (!tsi
->is_host(tsi
, hsi
) || !tsr
->is_host(tsr
, hsr
)))
632 { /* change TS in case of a NAT in transport mode */
633 DBG2(DBG_IKE
, "changing received traffic selectors %R=== %R due to NAT",
635 tsi
->set_address(tsi
, hsi
);
636 tsr
->set_address(tsr
, hsr
);
641 traffic_selector_t
*tsisub
, *tsrsub
;
643 /* check if peer selection is valid */
644 tsisub
= this->tsi
->get_subset(this->tsi
, tsi
);
645 tsrsub
= this->tsr
->get_subset(this->tsr
, tsr
);
646 if (!tsisub
|| !tsrsub
)
648 DBG1(DBG_IKE
, "peer selected invalid traffic selectors: "
649 "%R for %R, %R for %R", tsi
, this->tsi
, tsr
, this->tsr
);
658 this->tsi
->destroy(this->tsi
);
659 this->tsr
->destroy(this->tsr
);
674 static encap_t
get_encap(ike_sa_t
* ike_sa
, bool udp
)
680 if (ike_sa
->supports_extension(ike_sa
, EXT_NATT_DRAFT_02_03
))
682 return ENCAP_UDP_DRAFT_00_03
;
688 * Get NAT-OA payload type (RFC 3947 or RFC 3947 drafts).
690 static payload_type_t
get_nat_oa_payload_type(ike_sa_t
*ike_sa
)
692 if (ike_sa
->supports_extension(ike_sa
, EXT_NATT_DRAFT_02_03
))
694 return PLV1_NAT_OA_DRAFT_00_03
;
700 * Add NAT-OA payloads
702 static void add_nat_oa_payloads(private_quick_mode_t
*this, message_t
*message
)
704 identification_t
*id
;
705 id_payload_t
*nat_oa
;
707 payload_type_t nat_oa_payload_type
;
711 init
= message
->get_source(message
);
712 resp
= message
->get_destination(message
);
716 init
= message
->get_destination(message
);
717 resp
= message
->get_source(message
);
720 nat_oa_payload_type
= get_nat_oa_payload_type(this->ike_sa
);
722 /* first NAT-OA is the initiator's address */
723 id
= identification_create_from_sockaddr(init
->get_sockaddr(init
));
724 nat_oa
= id_payload_create_from_identification(nat_oa_payload_type
, id
);
725 message
->add_payload(message
, (payload_t
*)nat_oa
);
728 /* second NAT-OA is that of the responder */
729 id
= identification_create_from_sockaddr(resp
->get_sockaddr(resp
));
730 nat_oa
= id_payload_create_from_identification(nat_oa_payload_type
, id
);
731 message
->add_payload(message
, (payload_t
*)nat_oa
);
738 static void get_lifetimes(private_quick_mode_t
*this)
742 lft
= this->config
->get_lifetime(this->config
, TRUE
);
745 this->lifetime
= lft
->time
.life
;
749 this->lifebytes
= lft
->bytes
.life
;
755 * Check and apply lifetimes
757 static void apply_lifetimes(private_quick_mode_t
*this, sa_payload_t
*sa_payload
)
762 lifetime
= sa_payload
->get_lifetime(sa_payload
);
763 lifebytes
= sa_payload
->get_lifebytes(sa_payload
);
764 if (this->lifetime
!= lifetime
)
766 DBG1(DBG_IKE
, "received %us lifetime, configured %us",
767 lifetime
, this->lifetime
);
768 this->lifetime
= lifetime
;
770 if (this->lifebytes
!= lifebytes
)
772 DBG1(DBG_IKE
, "received %llu lifebytes, configured %llu",
773 lifebytes
, this->lifebytes
);
774 this->lifebytes
= lifebytes
;
779 * Set the task ready to build notify error message
781 static status_t
send_notify(private_quick_mode_t
*this, notify_type_t type
)
783 notify_payload_t
*notify
;
785 notify
= notify_payload_create_from_protocol_and_type(PLV1_NOTIFY
,
787 notify
->set_spi(notify
, this->spi_i
);
789 this->ike_sa
->queue_task(this->ike_sa
,
790 (task_t
*)informational_create(this->ike_sa
, notify
));
791 /* cancel all active/passive tasks in favour of informational */
792 this->ike_sa
->flush_queue(this->ike_sa
,
793 this->initiator ? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
798 * Prepare a list of proposals from child_config containing only the specified
799 * DH group, unless it is set to MODP_NONE.
801 static linked_list_t
*get_proposals(private_quick_mode_t
*this,
802 diffie_hellman_group_t group
)
805 proposal_t
*proposal
;
806 enumerator_t
*enumerator
;
808 list
= this->config
->get_proposals(this->config
, FALSE
);
809 enumerator
= list
->create_enumerator(list
);
810 while (enumerator
->enumerate(enumerator
, &proposal
))
812 if (group
!= MODP_NONE
)
814 if (!proposal
->has_dh_group(proposal
, group
))
816 list
->remove_at(list
, enumerator
);
817 proposal
->destroy(proposal
);
820 proposal
->strip_dh(proposal
, group
);
822 proposal
->set_spi(proposal
, this->spi_i
);
824 enumerator
->destroy(enumerator
);
829 METHOD(task_t
, build_i
, status_t
,
830 private_quick_mode_t
*this, message_t
*message
)
836 sa_payload_t
*sa_payload
;
837 linked_list_t
*list
, *tsi
, *tsr
;
838 proposal_t
*proposal
;
839 diffie_hellman_group_t group
;
842 this->udp
= this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
);
843 this->mode
= this->config
->get_mode(this->config
);
844 this->child_sa
= child_sa_create(
845 this->ike_sa
->get_my_host(this->ike_sa
),
846 this->ike_sa
->get_other_host(this->ike_sa
),
847 this->config
, this->reqid
, this->udp
,
848 this->mark_in
, this->mark_out
);
850 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
852 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
853 add_nat_oa_payloads(this, message
);
856 if (this->config
->has_option(this->config
, OPT_IPCOMP
))
858 this->cpi_i
= this->child_sa
->alloc_cpi(this->child_sa
);
861 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, "
866 list
= this->config
->get_proposals(this->config
, MODP_NONE
);
867 if (list
->get_first(list
, (void**)&proposal
) == SUCCESS
)
869 this->proto
= proposal
->get_protocol(proposal
);
871 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
872 this->spi_i
= this->child_sa
->alloc_spi(this->child_sa
, this->proto
);
875 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
879 group
= this->config
->get_dh_group(this->config
);
880 if (group
!= MODP_NONE
)
882 proposal_t
*proposal
;
883 uint16_t preferred_group
;
885 proposal
= this->ike_sa
->get_proposal(this->ike_sa
);
886 proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
,
887 &preferred_group
, NULL
);
888 /* try the negotiated DH group from IKE_SA */
889 list
= get_proposals(this, preferred_group
);
890 if (list
->get_count(list
))
892 group
= preferred_group
;
896 /* fall back to the first configured DH group */
898 list
= get_proposals(this, group
);
901 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
905 DBG1(DBG_IKE
, "configured DH group %N not supported",
906 diffie_hellman_group_names
, group
);
907 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
913 list
= get_proposals(this, MODP_NONE
);
917 encap
= get_encap(this->ike_sa
, this->udp
);
918 sa_payload
= sa_payload_create_from_proposals_v1(list
,
919 this->lifetime
, this->lifebytes
, AUTH_NONE
,
920 this->mode
, encap
, this->cpi_i
);
921 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
922 message
->add_payload(message
, &sa_payload
->payload_interface
);
924 if (!add_nonce(this, &this->nonce_i
, message
))
928 if (group
!= MODP_NONE
)
930 if (!add_ke(this, message
))
937 this->tsi
= select_ts(this, TRUE
, NULL
);
941 this->tsr
= select_ts(this, FALSE
, NULL
);
943 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
944 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
945 this->tsi
= this->tsr
= NULL
;
946 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
947 NARROW_INITIATOR_PRE_AUTH
, tsi
, tsr
);
948 tsi
->remove_first(tsi
, (void**)&this->tsi
);
949 tsr
->remove_first(tsr
, (void**)&this->tsr
);
950 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
951 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
952 if (!this->tsi
|| !this->tsr
)
956 add_ts(this, message
);
969 * Check for notify errors, return TRUE if error found
971 static bool has_notify_errors(private_quick_mode_t
*this, message_t
*message
)
973 enumerator_t
*enumerator
;
977 enumerator
= message
->create_payload_enumerator(message
);
978 while (enumerator
->enumerate(enumerator
, &payload
))
980 if (payload
->get_type(payload
) == PLV1_NOTIFY
)
982 notify_payload_t
*notify
;
985 notify
= (notify_payload_t
*)payload
;
986 type
= notify
->get_notify_type(notify
);
990 DBG1(DBG_IKE
, "received %N error notify",
991 notify_type_names
, type
);
996 DBG1(DBG_IKE
, "received %N notify", notify_type_names
, type
);
1000 enumerator
->destroy(enumerator
);
1006 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
1008 static void check_for_rekeyed_child(private_quick_mode_t
*this, bool responder
)
1010 enumerator_t
*enumerator
, *policies
;
1011 traffic_selector_t
*local
, *remote
, *my_ts
, *other_ts
;
1012 child_sa_t
*child_sa
;
1013 proposal_t
*proposal
;
1019 other_ts
= this->tsi
;
1024 other_ts
= this->tsr
;
1027 name
= this->config
->get_name(this->config
);
1028 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1029 while (this->reqid
== 0 && enumerator
->enumerate(enumerator
, &child_sa
))
1031 if (streq(child_sa
->get_name(child_sa
), name
))
1033 proposal
= child_sa
->get_proposal(child_sa
);
1034 switch (child_sa
->get_state(child_sa
))
1036 case CHILD_INSTALLED
:
1037 case CHILD_REKEYING
:
1038 policies
= child_sa
->create_policy_enumerator(child_sa
);
1039 if (policies
->enumerate(policies
, &local
, &remote
) &&
1040 local
->equals(local
, my_ts
) &&
1041 remote
->equals(remote
, other_ts
) &&
1042 this->proposal
->equals(this->proposal
, proposal
))
1044 this->reqid
= child_sa
->get_reqid(child_sa
);
1045 this->rekey
= child_sa
->get_spi(child_sa
, TRUE
);
1046 this->mark_in
= child_sa
->get_mark(child_sa
,
1048 this->mark_out
= child_sa
->get_mark(child_sa
,
1050 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
1051 DBG1(DBG_IKE
, "detected rekeying of CHILD_SA %s{%u}",
1052 child_sa
->get_name(child_sa
),
1053 child_sa
->get_unique_id(child_sa
));
1055 policies
->destroy(policies
);
1063 enumerator
->destroy(enumerator
);
1066 METHOD(task_t
, process_r
, status_t
,
1067 private_quick_mode_t
*this, message_t
*message
)
1069 if (this->mid
&& this->mid
!= message
->get_message_id(message
))
1070 { /* not responsible for this quick mode exchange */
1074 switch (this->state
)
1078 sa_payload_t
*sa_payload
;
1079 linked_list_t
*tsi
, *tsr
, *hostsi
, *hostsr
, *list
= NULL
;
1080 peer_cfg_t
*peer_cfg
;
1082 bool private, prefer_configured
;
1084 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1085 PLV1_SECURITY_ASSOCIATION
);
1088 DBG1(DBG_IKE
, "sa payload missing");
1089 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1092 this->mode
= sa_payload
->get_encap_mode(sa_payload
, &this->udp
);
1094 if (!get_ts(this, message
))
1098 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1099 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
1100 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
1101 this->tsi
= this->tsr
= NULL
;
1102 hostsi
= get_dynamic_hosts(this->ike_sa
, FALSE
);
1103 hostsr
= get_dynamic_hosts(this->ike_sa
, TRUE
);
1104 this->config
= peer_cfg
->select_child_cfg(peer_cfg
, tsr
, tsi
,
1106 hostsi
->destroy(hostsi
);
1107 hostsr
->destroy(hostsr
);
1110 this->tsi
= select_ts(this, FALSE
, tsi
);
1111 this->tsr
= select_ts(this, TRUE
, tsr
);
1113 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1114 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1115 if (!this->config
|| !this->tsi
|| !this->tsr
||
1116 this->mode
!= this->config
->get_mode(this->config
))
1118 DBG1(DBG_IKE
, "no matching CHILD_SA config found");
1119 return send_notify(this, INVALID_ID_INFORMATION
);
1122 if (this->config
->has_option(this->config
, OPT_IPCOMP
))
1124 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1126 if (!list
->get_count(list
))
1128 DBG1(DBG_IKE
, "expected IPComp proposal but peer did "
1129 "not send one, IPComp disabled");
1133 if (!list
|| !list
->get_count(list
))
1136 list
= sa_payload
->get_proposals(sa_payload
);
1138 private = this->ike_sa
->supports_extension(this->ike_sa
,
1140 prefer_configured
= lib
->settings
->get_bool(lib
->settings
,
1141 "%s.prefer_configured_proposals", TRUE
, lib
->ns
);
1142 this->proposal
= this->config
->select_proposal(this->config
, list
,
1143 FALSE
, private, prefer_configured
);
1144 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1146 get_lifetimes(this);
1147 apply_lifetimes(this, sa_payload
);
1149 if (!this->proposal
)
1151 DBG1(DBG_IKE
, "no matching proposal found, sending %N",
1152 notify_type_names
, NO_PROPOSAL_CHOSEN
);
1153 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1155 this->spi_i
= this->proposal
->get_spi(this->proposal
);
1157 if (!get_nonce(this, &this->nonce_i
, message
))
1159 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1162 if (this->proposal
->get_algorithm(this->proposal
,
1163 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
1165 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
1169 DBG1(DBG_IKE
, "negotiated DH group %N not supported",
1170 diffie_hellman_group_names
, group
);
1171 return send_notify(this, INVALID_KEY_INFORMATION
);
1173 if (!get_ke(this, message
))
1175 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1179 check_for_rekeyed_child(this, TRUE
);
1181 this->child_sa
= child_sa_create(
1182 this->ike_sa
->get_my_host(this->ike_sa
),
1183 this->ike_sa
->get_other_host(this->ike_sa
),
1184 this->config
, this->reqid
, this->udp
,
1185 this->mark_in
, this->mark_out
);
1187 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
1188 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
1189 this->tsi
= this->tsr
= NULL
;
1190 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
1191 NARROW_RESPONDER
, tsr
, tsi
);
1192 if (tsi
->remove_first(tsi
, (void**)&this->tsi
) != SUCCESS
||
1193 tsr
->remove_first(tsr
, (void**)&this->tsr
) != SUCCESS
)
1195 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1196 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1197 return send_notify(this, INVALID_ID_INFORMATION
);
1199 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1200 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1206 if (has_notify_errors(this, message
))
1210 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
)
1212 if (message
->get_payload(message
, PLV1_DELETE
))
1214 /* If the DELETE for a Quick Mode follows immediately
1215 * after rekeying, we might receive it before the
1216 * third completing Quick Mode message. Ignore it, as
1217 * it gets handled by a separately queued delete task. */
1224 ike_sa_t
*ike_sa
= this->ike_sa
;
1227 task
= (task_t
*)quick_delete_create(this->ike_sa
,
1228 this->proposal
->get_protocol(this->proposal
),
1229 this->spi_i
, TRUE
, TRUE
);
1230 /* flush_queue() destroys the current task */
1231 ike_sa
->flush_queue(ike_sa
, TASK_QUEUE_PASSIVE
);
1232 ike_sa
->queue_task(ike_sa
, task
);
1233 return ALREADY_DONE
;
1242 METHOD(task_t
, build_r
, status_t
,
1243 private_quick_mode_t
*this, message_t
*message
)
1245 if (this->mid
&& this->mid
!= message
->get_message_id(message
))
1246 { /* not responsible for this quick mode exchange */
1250 switch (this->state
)
1254 sa_payload_t
*sa_payload
;
1257 this->proto
= this->proposal
->get_protocol(this->proposal
);
1258 this->spi_r
= this->child_sa
->alloc_spi(this->child_sa
, this->proto
);
1261 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
1262 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1264 this->proposal
->set_spi(this->proposal
, this->spi_r
);
1268 this->cpi_r
= this->child_sa
->alloc_cpi(this->child_sa
);
1271 DBG1(DBG_IKE
, "unable to allocate a CPI from "
1272 "kernel, IPComp disabled");
1273 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1277 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
1279 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1280 add_nat_oa_payloads(this, message
);
1283 encap
= get_encap(this->ike_sa
, this->udp
);
1284 sa_payload
= sa_payload_create_from_proposal_v1(this->proposal
,
1285 this->lifetime
, this->lifebytes
, AUTH_NONE
,
1286 this->mode
, encap
, this->cpi_r
);
1287 message
->add_payload(message
, &sa_payload
->payload_interface
);
1289 if (!add_nonce(this, &this->nonce_r
, message
))
1295 if (!add_ke(this, message
))
1301 add_ts(this, message
);
1303 this->state
= QM_NEGOTIATED
;
1304 this->mid
= message
->get_message_id(message
);
1308 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
)
1310 /* skip INFORMATIONAL response if we received a INFORMATIONAL
1311 * delete, see process_r() */
1312 return ALREADY_DONE
;
1320 METHOD(task_t
, process_i
, status_t
,
1321 private_quick_mode_t
*this, message_t
*message
)
1323 switch (this->state
)
1327 sa_payload_t
*sa_payload
;
1328 linked_list_t
*list
= NULL
;
1331 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1332 PLV1_SECURITY_ASSOCIATION
);
1335 DBG1(DBG_IKE
, "sa payload missing");
1336 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1340 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1342 if (!list
->get_count(list
))
1344 DBG1(DBG_IKE
, "peer did not accept our IPComp proposal, "
1349 if (!list
|| !list
->get_count(list
))
1352 list
= sa_payload
->get_proposals(sa_payload
);
1354 private = this->ike_sa
->supports_extension(this->ike_sa
,
1356 this->proposal
= this->config
->select_proposal(this->config
, list
,
1357 FALSE
, private, TRUE
);
1358 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1359 if (!this->proposal
)
1361 DBG1(DBG_IKE
, "no matching proposal found");
1362 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1364 this->spi_r
= this->proposal
->get_spi(this->proposal
);
1366 apply_lifetimes(this, sa_payload
);
1368 if (!get_nonce(this, &this->nonce_r
, message
))
1370 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1372 if (this->dh
&& !get_ke(this, message
))
1374 return send_notify(this, INVALID_KEY_INFORMATION
);
1376 if (!get_ts(this, message
))
1378 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1380 check_for_rekeyed_child(this, FALSE
);
1383 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1385 this->state
= QM_NEGOTIATED
;
1393 METHOD(task_t
, get_type
, task_type_t
,
1394 private_quick_mode_t
*this)
1396 return TASK_QUICK_MODE
;
1399 METHOD(quick_mode_t
, get_mid
, uint32_t,
1400 private_quick_mode_t
*this)
1405 METHOD(quick_mode_t
, use_reqid
, void,
1406 private_quick_mode_t
*this, uint32_t reqid
)
1408 this->reqid
= reqid
;
1411 METHOD(quick_mode_t
, use_marks
, void,
1412 private_quick_mode_t
*this, u_int in
, u_int out
)
1415 this->mark_out
= out
;
1418 METHOD(quick_mode_t
, rekey
, void,
1419 private_quick_mode_t
*this, uint32_t spi
)
1424 METHOD(task_t
, migrate
, void,
1425 private_quick_mode_t
*this, ike_sa_t
*ike_sa
)
1427 chunk_free(&this->nonce_i
);
1428 chunk_free(&this->nonce_r
);
1429 DESTROY_IF(this->tsi
);
1430 DESTROY_IF(this->tsr
);
1431 DESTROY_IF(this->proposal
);
1432 DESTROY_IF(this->child_sa
);
1433 DESTROY_IF(this->dh
);
1435 this->ike_sa
= ike_sa
;
1436 this->keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
);
1437 this->state
= QM_INIT
;
1441 this->proposal
= NULL
;
1442 this->child_sa
= NULL
;
1449 if (!this->initiator
)
1451 DESTROY_IF(this->config
);
1452 this->config
= NULL
;
1456 METHOD(task_t
, destroy
, void,
1457 private_quick_mode_t
*this)
1459 chunk_free(&this->nonce_i
);
1460 chunk_free(&this->nonce_r
);
1461 DESTROY_IF(this->tsi
);
1462 DESTROY_IF(this->tsr
);
1463 DESTROY_IF(this->proposal
);
1464 DESTROY_IF(this->child_sa
);
1465 DESTROY_IF(this->config
);
1466 DESTROY_IF(this->dh
);
1471 * Described in header.
1473 quick_mode_t
*quick_mode_create(ike_sa_t
*ike_sa
, child_cfg_t
*config
,
1474 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1476 private_quick_mode_t
*this;
1481 .get_type
= _get_type
,
1482 .migrate
= _migrate
,
1483 .destroy
= _destroy
,
1485 .get_mid
= _get_mid
,
1486 .use_reqid
= _use_reqid
,
1487 .use_marks
= _use_marks
,
1491 .initiator
= config
!= NULL
,
1493 .keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
),
1495 .tsi
= tsi ? tsi
->clone(tsi
) : NULL
,
1496 .tsr
= tsr ? tsr
->clone(tsr
) : NULL
,
1498 .delete = lib
->settings
->get_bool(lib
->settings
,
1499 "%s.delete_rekeyed", FALSE
, lib
->ns
),
1504 this->public.task
.build
= _build_i
;
1505 this->public.task
.process
= _process_i
;
1509 this->public.task
.build
= _build_r
;
1510 this->public.task
.process
= _process_r
;
1513 return &this->public;