2 * Copyright (C) 2012 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 * Negotaited lifebytes of new SA
154 * Reqid to use, 0 for auto-allocate
164 * Negotiated mode, tunnel or transport
169 * Use UDP encapsulation
173 /** states of quick mode */
181 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
183 static void schedule_inactivity_timeout(private_quick_mode_t
*this)
188 timeout
= this->config
->get_inactivity(this->config
);
191 close_ike
= lib
->settings
->get_bool(lib
->settings
,
192 "%s.inactivity_close_ike", FALSE
, charon
->name
);
193 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
194 inactivity_job_create(this->child_sa
->get_reqid(this->child_sa
),
195 timeout
, close_ike
), timeout
);
200 * Check if we have a an address pool configured
202 static bool have_pool(ike_sa_t
*ike_sa
)
204 enumerator_t
*enumerator
;
205 peer_cfg_t
*peer_cfg
;
209 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
212 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
213 if (enumerator
->enumerate(enumerator
, &pool
))
217 enumerator
->destroy(enumerator
);
223 * Get hosts to use for dynamic traffic selectors
225 static linked_list_t
*get_dynamic_hosts(ike_sa_t
*ike_sa
, bool local
)
227 enumerator_t
*enumerator
;
231 list
= linked_list_create();
232 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
233 while (enumerator
->enumerate(enumerator
, &host
))
235 list
->insert_last(list
, host
);
237 enumerator
->destroy(enumerator
);
239 if (list
->get_count(list
) == 0)
240 { /* no virtual IPs assigned */
243 host
= ike_sa
->get_my_host(ike_sa
);
244 list
->insert_last(list
, host
);
246 else if (!have_pool(ike_sa
))
247 { /* use host only if we don't have a pool configured */
248 host
= ike_sa
->get_other_host(ike_sa
);
249 list
->insert_last(list
, host
);
256 * Install negotiated CHILD_SA
258 static bool install(private_quick_mode_t
*this)
260 status_t status
, status_i
, status_o
;
261 chunk_t encr_i
, encr_r
, integ_i
, integ_r
;
262 linked_list_t
*tsi
, *tsr
, *my_ts
, *other_ts
;
263 child_sa_t
*old
= NULL
;
265 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
266 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
267 this->child_sa
->set_mode(this->child_sa
, this->mode
);
269 if (this->cpi_i
&& this->cpi_r
)
270 { /* DEFLATE is the only transform we currently support */
271 this->child_sa
->set_ipcomp(this->child_sa
, IPCOMP_DEFLATE
);
275 this->cpi_i
= this->cpi_r
= 0;
278 this->child_sa
->set_protocol(this->child_sa
,
279 this->proposal
->get_protocol(this->proposal
));
281 status_i
= status_o
= FAILED
;
282 encr_i
= encr_r
= integ_i
= integ_r
= chunk_empty
;
283 tsi
= linked_list_create_with_items(this->tsi
->clone(this->tsi
), NULL
);
284 tsr
= linked_list_create_with_items(this->tsr
->clone(this->tsr
), NULL
);
287 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
288 NARROW_INITIATOR_POST_AUTH
, tsi
, tsr
);
292 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
293 NARROW_RESPONDER_POST
, tsr
, tsi
);
295 if (tsi
->get_count(tsi
) == 0 || tsr
->get_count(tsr
) == 0)
297 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
298 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
299 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
303 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
, this->dh
,
304 this->spi_i
, this->spi_r
, this->nonce_i
, this->nonce_r
,
305 &encr_i
, &integ_i
, &encr_r
, &integ_r
))
309 status_i
= this->child_sa
->install(this->child_sa
,
310 encr_r
, integ_r
, this->spi_i
, this->cpi_i
,
311 this->initiator
, TRUE
, FALSE
, tsi
, tsr
);
312 status_o
= this->child_sa
->install(this->child_sa
,
313 encr_i
, integ_i
, this->spi_r
, this->cpi_r
,
314 this->initiator
, FALSE
, FALSE
, tsi
, tsr
);
318 status_i
= this->child_sa
->install(this->child_sa
,
319 encr_i
, integ_i
, this->spi_r
, this->cpi_r
,
320 this->initiator
, TRUE
, FALSE
, tsr
, tsi
);
321 status_o
= this->child_sa
->install(this->child_sa
,
322 encr_r
, integ_r
, this->spi_i
, this->cpi_i
,
323 this->initiator
, FALSE
, FALSE
, tsr
, tsi
);
326 chunk_clear(&integ_i
);
327 chunk_clear(&integ_r
);
328 chunk_clear(&encr_i
);
329 chunk_clear(&encr_r
);
331 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
333 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
334 (status_i
!= SUCCESS
) ? "inbound " : "",
335 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ? "and ": "",
336 (status_o
!= SUCCESS
) ? "outbound " : "");
337 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
338 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
344 status
= this->child_sa
->add_policies(this->child_sa
, tsi
, tsr
);
348 status
= this->child_sa
->add_policies(this->child_sa
, tsr
, tsi
);
350 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
351 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
352 if (status
!= SUCCESS
)
354 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
358 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
359 this->dh
, this->nonce_i
, this->nonce_r
);
361 /* add to IKE_SA, and remove from task */
362 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
363 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
365 my_ts
= linked_list_create_from_enumerator(
366 this->child_sa
->create_ts_enumerator(this->child_sa
, TRUE
));
367 other_ts
= linked_list_create_from_enumerator(
368 this->child_sa
->create_ts_enumerator(this->child_sa
, FALSE
));
370 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
371 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
372 this->child_sa
->get_name(this->child_sa
),
373 this->child_sa
->get_reqid(this->child_sa
),
374 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
375 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)), my_ts
, other_ts
);
377 my_ts
->destroy(my_ts
);
378 other_ts
->destroy(other_ts
);
382 old
= this->ike_sa
->get_child_sa(this->ike_sa
,
383 this->proposal
->get_protocol(this->proposal
),
388 charon
->bus
->child_rekey(charon
->bus
, old
, this->child_sa
);
392 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
396 schedule_inactivity_timeout(this);
398 this->child_sa
= NULL
;
403 * Generate and add NONCE
405 static bool add_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
408 nonce_payload_t
*nonce_payload
;
411 nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
414 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
417 if (!nonceg
->allocate_nonce(nonceg
, NONCE_SIZE
, nonce
))
419 DBG1(DBG_IKE
, "nonce allocation failed");
420 nonceg
->destroy(nonceg
);
423 nonceg
->destroy(nonceg
);
425 nonce_payload
= nonce_payload_create(NONCE_V1
);
426 nonce_payload
->set_nonce(nonce_payload
, *nonce
);
427 message
->add_payload(message
, &nonce_payload
->payload_interface
);
433 * Extract nonce from NONCE payload
435 static bool get_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
438 nonce_payload_t
*nonce_payload
;
440 nonce_payload
= (nonce_payload_t
*)message
->get_payload(message
, NONCE_V1
);
443 DBG1(DBG_IKE
, "NONCE payload missing in message");
446 *nonce
= nonce_payload
->get_nonce(nonce_payload
);
452 * Add KE payload to message
454 static void add_ke(private_quick_mode_t
*this, message_t
*message
)
456 ke_payload_t
*ke_payload
;
458 ke_payload
= ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1
, this->dh
);
459 message
->add_payload(message
, &ke_payload
->payload_interface
);
463 * Get DH value from a KE payload
465 static bool get_ke(private_quick_mode_t
*this, message_t
*message
)
467 ke_payload_t
*ke_payload
;
469 ke_payload
= (ke_payload_t
*)message
->get_payload(message
, KEY_EXCHANGE_V1
);
472 DBG1(DBG_IKE
, "KE payload missing");
475 this->dh
->set_other_public_value(this->dh
,
476 ke_payload
->get_key_exchange_data(ke_payload
));
481 * Select a traffic selector from configuration
483 static traffic_selector_t
* select_ts(private_quick_mode_t
*this, bool local
,
484 linked_list_t
*supplied
)
486 traffic_selector_t
*ts
;
487 linked_list_t
*list
, *hosts
;
489 hosts
= get_dynamic_hosts(this->ike_sa
, local
);
490 list
= this->config
->get_traffic_selectors(this->config
,
491 local
, supplied
, hosts
);
492 hosts
->destroy(hosts
);
493 if (list
->get_first(list
, (void**)&ts
) == SUCCESS
)
499 DBG1(DBG_IKE
, "%s traffic selector missing in configuration",
500 local
? "local" : "local");
503 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
508 * Add selected traffic selectors to message
510 static void add_ts(private_quick_mode_t
*this, message_t
*message
)
512 id_payload_t
*id_payload
;
514 id_payload
= id_payload_create_from_ts(this->tsi
);
515 message
->add_payload(message
, &id_payload
->payload_interface
);
516 id_payload
= id_payload_create_from_ts(this->tsr
);
517 message
->add_payload(message
, &id_payload
->payload_interface
);
521 * Get traffic selectors from received message
523 static bool get_ts(private_quick_mode_t
*this, message_t
*message
)
525 traffic_selector_t
*tsi
= NULL
, *tsr
= NULL
;
526 enumerator_t
*enumerator
;
527 id_payload_t
*id_payload
;
532 enumerator
= message
->create_payload_enumerator(message
);
533 while (enumerator
->enumerate(enumerator
, &payload
))
535 if (payload
->get_type(payload
) == ID_V1
)
537 id_payload
= (id_payload_t
*)payload
;
541 tsi
= id_payload
->get_ts(id_payload
);
546 tsr
= id_payload
->get_ts(id_payload
);
551 enumerator
->destroy(enumerator
);
553 /* create host2host selectors if ID payloads missing */
556 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
557 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
561 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
562 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
566 tsi
= traffic_selector_create_from_subnet(hsi
->clone(hsi
),
567 hsi
->get_family(hsi
) == AF_INET
? 32 : 128, 0, 0, 65535);
571 tsr
= traffic_selector_create_from_subnet(hsr
->clone(hsr
),
572 hsr
->get_family(hsr
) == AF_INET
? 32 : 128, 0, 0, 65535);
574 if (this->mode
== MODE_TRANSPORT
&& this->udp
&&
575 (!tsi
->is_host(tsi
, hsi
) || !tsr
->is_host(tsr
, hsr
)))
576 { /* change TS in case of a NAT in transport mode */
577 DBG2(DBG_IKE
, "changing received traffic selectors %R=== %R due to NAT",
579 tsi
->set_address(tsi
, hsi
);
580 tsr
->set_address(tsr
, hsr
);
585 traffic_selector_t
*tsisub
, *tsrsub
;
587 /* check if peer selection is valid */
588 tsisub
= this->tsi
->get_subset(this->tsi
, tsi
);
589 tsrsub
= this->tsr
->get_subset(this->tsr
, tsr
);
590 if (!tsisub
|| !tsrsub
)
592 DBG1(DBG_IKE
, "peer selected invalid traffic selectors: "
593 "%R for %R, %R for %R", tsi
, this->tsi
, tsr
, this->tsr
);
602 this->tsi
->destroy(this->tsi
);
603 this->tsr
->destroy(this->tsr
);
618 static encap_t
get_encap(ike_sa_t
* ike_sa
, bool udp
)
624 if (ike_sa
->supports_extension(ike_sa
, EXT_NATT_DRAFT_02_03
))
626 return ENCAP_UDP_DRAFT_00_03
;
632 * Get NAT-OA payload type (RFC 3947 or RFC 3947 drafts).
634 static payload_type_t
get_nat_oa_payload_type(ike_sa_t
*ike_sa
)
636 if (ike_sa
->supports_extension(ike_sa
, EXT_NATT_DRAFT_02_03
))
638 return NAT_OA_DRAFT_00_03_V1
;
644 * Add NAT-OA payloads
646 static void add_nat_oa_payloads(private_quick_mode_t
*this, message_t
*message
)
648 identification_t
*id
;
649 id_payload_t
*nat_oa
;
651 payload_type_t nat_oa_payload_type
;
653 src
= message
->get_source(message
);
654 dst
= message
->get_destination(message
);
656 src
= this->initiator
? src
: dst
;
657 dst
= this->initiator
? dst
: src
;
659 nat_oa_payload_type
= get_nat_oa_payload_type(this->ike_sa
);
661 /* first NAT-OA is the initiator's address */
662 id
= identification_create_from_sockaddr(src
->get_sockaddr(src
));
663 nat_oa
= id_payload_create_from_identification(nat_oa_payload_type
, id
);
664 message
->add_payload(message
, (payload_t
*)nat_oa
);
667 /* second NAT-OA is that of the responder */
668 id
= identification_create_from_sockaddr(dst
->get_sockaddr(dst
));
669 nat_oa
= id_payload_create_from_identification(nat_oa_payload_type
, id
);
670 message
->add_payload(message
, (payload_t
*)nat_oa
);
677 static void get_lifetimes(private_quick_mode_t
*this)
681 lft
= this->config
->get_lifetime(this->config
);
684 this->lifetime
= lft
->time
.life
;
686 else if (lft
->bytes
.life
)
688 this->lifebytes
= lft
->bytes
.life
;
694 * Check and apply lifetimes
696 static void apply_lifetimes(private_quick_mode_t
*this, sa_payload_t
*sa_payload
)
701 lifetime
= sa_payload
->get_lifetime(sa_payload
);
702 lifebytes
= sa_payload
->get_lifebytes(sa_payload
);
703 if (this->lifetime
!= lifetime
)
705 DBG1(DBG_IKE
, "received %us lifetime, configured %us",
706 lifetime
, this->lifetime
);
707 this->lifetime
= lifetime
;
709 if (this->lifebytes
!= lifebytes
)
711 DBG1(DBG_IKE
, "received %llu lifebytes, configured %llu",
712 lifebytes
, this->lifebytes
);
713 this->lifebytes
= lifebytes
;
718 * Set the task ready to build notify error message
720 static status_t
send_notify(private_quick_mode_t
*this, notify_type_t type
)
722 notify_payload_t
*notify
;
724 notify
= notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
726 notify
->set_spi(notify
, this->spi_i
);
728 this->ike_sa
->queue_task(this->ike_sa
,
729 (task_t
*)informational_create(this->ike_sa
, notify
));
730 /* cancel all active/passive tasks in favour of informational */
731 this->ike_sa
->flush_queue(this->ike_sa
,
732 this->initiator
? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
737 * Prepare a list of proposals from child_config containing only the specified
738 * DH group, unless it is set to MODP_NONE.
740 static linked_list_t
*get_proposals(private_quick_mode_t
*this,
741 diffie_hellman_group_t group
)
744 proposal_t
*proposal
;
745 enumerator_t
*enumerator
;
747 list
= this->config
->get_proposals(this->config
, FALSE
);
748 enumerator
= list
->create_enumerator(list
);
749 while (enumerator
->enumerate(enumerator
, &proposal
))
751 if (group
!= MODP_NONE
)
753 if (!proposal
->has_dh_group(proposal
, group
))
755 list
->remove_at(list
, enumerator
);
756 proposal
->destroy(proposal
);
759 proposal
->strip_dh(proposal
, group
);
761 proposal
->set_spi(proposal
, this->spi_i
);
763 enumerator
->destroy(enumerator
);
768 METHOD(task_t
, build_i
, status_t
,
769 private_quick_mode_t
*this, message_t
*message
)
775 sa_payload_t
*sa_payload
;
776 linked_list_t
*list
, *tsi
, *tsr
;
777 diffie_hellman_group_t group
;
780 this->udp
= this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
);
781 this->mode
= this->config
->get_mode(this->config
);
782 this->child_sa
= child_sa_create(
783 this->ike_sa
->get_my_host(this->ike_sa
),
784 this->ike_sa
->get_other_host(this->ike_sa
),
785 this->config
, this->reqid
, this->udp
);
787 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
789 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
790 add_nat_oa_payloads(this, message
);
793 if (this->config
->use_ipcomp(this->config
))
795 this->cpi_i
= this->child_sa
->alloc_cpi(this->child_sa
);
798 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, "
803 this->spi_i
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
806 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
809 group
= this->config
->get_dh_group(this->config
);
810 if (group
!= MODP_NONE
)
812 proposal_t
*proposal
;
813 u_int16_t preferred_group
;
815 proposal
= this->ike_sa
->get_proposal(this->ike_sa
);
816 proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
,
817 &preferred_group
, NULL
);
818 /* try the negotiated DH group from IKE_SA */
819 list
= get_proposals(this, preferred_group
);
820 if (list
->get_count(list
))
822 group
= preferred_group
;
826 /* fall back to the first configured DH group */
828 list
= get_proposals(this, group
);
831 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
835 DBG1(DBG_IKE
, "configured DH group %N not supported",
836 diffie_hellman_group_names
, group
);
837 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
843 list
= get_proposals(this, MODP_NONE
);
847 encap
= get_encap(this->ike_sa
, this->udp
);
848 sa_payload
= sa_payload_create_from_proposals_v1(list
,
849 this->lifetime
, this->lifebytes
, AUTH_NONE
,
850 this->mode
, encap
, this->cpi_i
);
851 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
852 message
->add_payload(message
, &sa_payload
->payload_interface
);
854 if (!add_nonce(this, &this->nonce_i
, message
))
858 if (group
!= MODP_NONE
)
860 add_ke(this, message
);
864 this->tsi
= select_ts(this, TRUE
, NULL
);
868 this->tsr
= select_ts(this, FALSE
, NULL
);
870 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
871 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
872 this->tsi
= this->tsr
= NULL
;
873 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
874 NARROW_INITIATOR_PRE_AUTH
, tsi
, tsr
);
875 tsi
->remove_first(tsi
, (void**)&this->tsi
);
876 tsr
->remove_first(tsr
, (void**)&this->tsr
);
877 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
878 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
879 if (!this->tsi
|| !this->tsr
)
883 add_ts(this, message
);
896 * Check for notify errors, return TRUE if error found
898 static bool has_notify_errors(private_quick_mode_t
*this, message_t
*message
)
900 enumerator_t
*enumerator
;
904 enumerator
= message
->create_payload_enumerator(message
);
905 while (enumerator
->enumerate(enumerator
, &payload
))
907 if (payload
->get_type(payload
) == NOTIFY_V1
)
909 notify_payload_t
*notify
;
912 notify
= (notify_payload_t
*)payload
;
913 type
= notify
->get_notify_type(notify
);
917 DBG1(DBG_IKE
, "received %N error notify",
918 notify_type_names
, type
);
923 DBG1(DBG_IKE
, "received %N notify", notify_type_names
, type
);
927 enumerator
->destroy(enumerator
);
933 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
935 static void check_for_rekeyed_child(private_quick_mode_t
*this)
937 enumerator_t
*enumerator
, *policies
;
938 traffic_selector_t
*local
, *remote
;
939 child_sa_t
*child_sa
;
940 proposal_t
*proposal
;
943 name
= this->config
->get_name(this->config
);
944 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
945 while (this->reqid
== 0 && enumerator
->enumerate(enumerator
, &child_sa
))
947 if (streq(child_sa
->get_name(child_sa
), name
))
949 proposal
= child_sa
->get_proposal(child_sa
);
950 switch (child_sa
->get_state(child_sa
))
952 case CHILD_INSTALLED
:
954 policies
= child_sa
->create_policy_enumerator(child_sa
);
955 if (policies
->enumerate(policies
, &local
, &remote
) &&
956 local
->equals(local
, this->tsr
) &&
957 remote
->equals(remote
, this->tsi
) &&
958 this->proposal
->equals(this->proposal
, proposal
))
960 this->reqid
= child_sa
->get_reqid(child_sa
);
961 this->rekey
= child_sa
->get_spi(child_sa
, TRUE
);
962 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
963 DBG1(DBG_IKE
, "detected rekeying of CHILD_SA %s{%u}",
964 child_sa
->get_name(child_sa
), this->reqid
);
966 policies
->destroy(policies
);
973 enumerator
->destroy(enumerator
);
976 METHOD(task_t
, process_r
, status_t
,
977 private_quick_mode_t
*this, message_t
*message
)
983 sa_payload_t
*sa_payload
;
984 linked_list_t
*tsi
, *tsr
, *hostsi
, *hostsr
, *list
= NULL
;
985 peer_cfg_t
*peer_cfg
;
989 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
990 SECURITY_ASSOCIATION_V1
);
993 DBG1(DBG_IKE
, "sa payload missing");
994 return send_notify(this, INVALID_PAYLOAD_TYPE
);
997 this->mode
= sa_payload
->get_encap_mode(sa_payload
, &this->udp
);
999 if (!get_ts(this, message
))
1003 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1004 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
1005 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
1006 this->tsi
= this->tsr
= NULL
;
1007 hostsi
= get_dynamic_hosts(this->ike_sa
, FALSE
);
1008 hostsr
= get_dynamic_hosts(this->ike_sa
, TRUE
);
1009 this->config
= peer_cfg
->select_child_cfg(peer_cfg
, tsr
, tsi
,
1011 hostsi
->destroy(hostsi
);
1012 hostsr
->destroy(hostsr
);
1015 this->tsi
= select_ts(this, FALSE
, tsi
);
1016 this->tsr
= select_ts(this, TRUE
, tsr
);
1018 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1019 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1020 if (!this->config
|| !this->tsi
|| !this->tsr
)
1022 DBG1(DBG_IKE
, "no matching CHILD_SA config found");
1023 return send_notify(this, INVALID_ID_INFORMATION
);
1026 if (this->config
->use_ipcomp(this->config
))
1028 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1030 if (!list
->get_count(list
))
1032 DBG1(DBG_IKE
, "expected IPComp proposal but peer did "
1033 "not send one, IPComp disabled");
1037 if (!list
|| !list
->get_count(list
))
1040 list
= sa_payload
->get_proposals(sa_payload
);
1042 private = this->ike_sa
->supports_extension(this->ike_sa
,
1044 this->proposal
= this->config
->select_proposal(this->config
,
1045 list
, FALSE
, private);
1046 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1048 get_lifetimes(this);
1049 apply_lifetimes(this, sa_payload
);
1051 if (!this->proposal
)
1053 DBG1(DBG_IKE
, "no matching proposal found, sending %N",
1054 notify_type_names
, NO_PROPOSAL_CHOSEN
);
1055 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1057 this->spi_i
= this->proposal
->get_spi(this->proposal
);
1059 if (!get_nonce(this, &this->nonce_i
, message
))
1061 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1064 if (this->proposal
->get_algorithm(this->proposal
,
1065 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
1067 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
1071 DBG1(DBG_IKE
, "negotiated DH group %N not supported",
1072 diffie_hellman_group_names
, group
);
1073 return send_notify(this, INVALID_KEY_INFORMATION
);
1075 if (!get_ke(this, message
))
1077 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1081 check_for_rekeyed_child(this);
1083 this->child_sa
= child_sa_create(
1084 this->ike_sa
->get_my_host(this->ike_sa
),
1085 this->ike_sa
->get_other_host(this->ike_sa
),
1086 this->config
, this->reqid
, this->udp
);
1088 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
1089 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
1090 this->tsi
= this->tsr
= NULL
;
1091 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
1092 NARROW_RESPONDER
, tsr
, tsi
);
1093 if (tsi
->remove_first(tsi
, (void**)&this->tsi
) != SUCCESS
||
1094 tsr
->remove_first(tsr
, (void**)&this->tsr
) != SUCCESS
)
1096 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1097 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1098 return send_notify(this, INVALID_ID_INFORMATION
);
1100 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1101 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1107 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
1108 has_notify_errors(this, message
))
1114 ike_sa_t
*ike_sa
= this->ike_sa
;
1117 task
= (task_t
*)quick_delete_create(this->ike_sa
,
1118 this->proposal
->get_protocol(this->proposal
),
1119 this->spi_i
, TRUE
, TRUE
);
1120 /* flush_queue() destroys the current task */
1121 ike_sa
->flush_queue(ike_sa
, TASK_QUEUE_PASSIVE
);
1122 ike_sa
->queue_task(ike_sa
, task
);
1123 return ALREADY_DONE
;
1132 METHOD(task_t
, build_r
, status_t
,
1133 private_quick_mode_t
*this, message_t
*message
)
1135 switch (this->state
)
1139 sa_payload_t
*sa_payload
;
1142 this->spi_r
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
1145 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
1146 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1148 this->proposal
->set_spi(this->proposal
, this->spi_r
);
1152 this->cpi_r
= this->child_sa
->alloc_cpi(this->child_sa
);
1155 DBG1(DBG_IKE
, "unable to allocate a CPI from "
1156 "kernel, IPComp disabled");
1157 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1161 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
1163 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1164 add_nat_oa_payloads(this, message
);
1167 encap
= get_encap(this->ike_sa
, this->udp
);
1168 sa_payload
= sa_payload_create_from_proposal_v1(this->proposal
,
1169 this->lifetime
, this->lifebytes
, AUTH_NONE
,
1170 this->mode
, encap
, this->cpi_r
);
1171 message
->add_payload(message
, &sa_payload
->payload_interface
);
1173 if (!add_nonce(this, &this->nonce_r
, message
))
1179 add_ke(this, message
);
1182 add_ts(this, message
);
1184 this->state
= QM_NEGOTIATED
;
1192 METHOD(task_t
, process_i
, status_t
,
1193 private_quick_mode_t
*this, message_t
*message
)
1195 switch (this->state
)
1199 sa_payload_t
*sa_payload
;
1200 linked_list_t
*list
= NULL
;
1203 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1204 SECURITY_ASSOCIATION_V1
);
1207 DBG1(DBG_IKE
, "sa payload missing");
1208 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1212 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1214 if (!list
->get_count(list
))
1216 DBG1(DBG_IKE
, "peer did not acccept our IPComp proposal, "
1221 if (!list
|| !list
->get_count(list
))
1224 list
= sa_payload
->get_proposals(sa_payload
);
1226 private = this->ike_sa
->supports_extension(this->ike_sa
,
1228 this->proposal
= this->config
->select_proposal(this->config
,
1229 list
, FALSE
, private);
1230 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1231 if (!this->proposal
)
1233 DBG1(DBG_IKE
, "no matching proposal found");
1234 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1236 this->spi_r
= this->proposal
->get_spi(this->proposal
);
1238 apply_lifetimes(this, sa_payload
);
1240 if (!get_nonce(this, &this->nonce_r
, message
))
1242 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1244 if (this->dh
&& !get_ke(this, message
))
1246 return send_notify(this, INVALID_KEY_INFORMATION
);
1248 if (!get_ts(this, message
))
1250 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1254 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1256 this->state
= QM_NEGOTIATED
;
1264 METHOD(task_t
, get_type
, task_type_t
,
1265 private_quick_mode_t
*this)
1267 return TASK_QUICK_MODE
;
1270 METHOD(quick_mode_t
, use_reqid
, void,
1271 private_quick_mode_t
*this, u_int32_t reqid
)
1273 this->reqid
= reqid
;
1276 METHOD(quick_mode_t
, rekey
, void,
1277 private_quick_mode_t
*this, u_int32_t spi
)
1282 METHOD(task_t
, migrate
, void,
1283 private_quick_mode_t
*this, ike_sa_t
*ike_sa
)
1285 chunk_free(&this->nonce_i
);
1286 chunk_free(&this->nonce_r
);
1287 DESTROY_IF(this->tsi
);
1288 DESTROY_IF(this->tsr
);
1289 DESTROY_IF(this->proposal
);
1290 DESTROY_IF(this->child_sa
);
1291 DESTROY_IF(this->dh
);
1293 this->ike_sa
= ike_sa
;
1294 this->keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
);
1295 this->state
= QM_INIT
;
1298 this->proposal
= NULL
;
1299 this->child_sa
= NULL
;
1304 if (!this->initiator
)
1306 DESTROY_IF(this->config
);
1307 this->config
= NULL
;
1311 METHOD(task_t
, destroy
, void,
1312 private_quick_mode_t
*this)
1314 chunk_free(&this->nonce_i
);
1315 chunk_free(&this->nonce_r
);
1316 DESTROY_IF(this->tsi
);
1317 DESTROY_IF(this->tsr
);
1318 DESTROY_IF(this->proposal
);
1319 DESTROY_IF(this->child_sa
);
1320 DESTROY_IF(this->config
);
1321 DESTROY_IF(this->dh
);
1326 * Described in header.
1328 quick_mode_t
*quick_mode_create(ike_sa_t
*ike_sa
, child_cfg_t
*config
,
1329 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1331 private_quick_mode_t
*this;
1336 .get_type
= _get_type
,
1337 .migrate
= _migrate
,
1338 .destroy
= _destroy
,
1340 .use_reqid
= _use_reqid
,
1344 .initiator
= config
!= NULL
,
1346 .keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
),
1348 .tsi
= tsi
? tsi
->clone(tsi
) : NULL
,
1349 .tsr
= tsr
? tsr
->clone(tsr
) : NULL
,
1354 this->public.task
.build
= _build_i
;
1355 this->public.task
.process
= _process_i
;
1359 this->public.task
.build
= _build_r
;
1360 this->public.task
.process
= _process_r
;
1363 return &this->public;