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 * SA protocol (ESP|AH) negotiated
174 * Use UDP encapsulation
178 /** states of quick mode */
186 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
188 static void schedule_inactivity_timeout(private_quick_mode_t
*this)
193 timeout
= this->config
->get_inactivity(this->config
);
196 close_ike
= lib
->settings
->get_bool(lib
->settings
,
197 "%s.inactivity_close_ike", FALSE
, lib
->ns
);
198 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
199 inactivity_job_create(this->child_sa
->get_reqid(this->child_sa
),
200 timeout
, close_ike
), timeout
);
205 * Check if we have a an address pool configured
207 static bool have_pool(ike_sa_t
*ike_sa
)
209 enumerator_t
*enumerator
;
210 peer_cfg_t
*peer_cfg
;
214 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
217 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
218 if (enumerator
->enumerate(enumerator
, &pool
))
222 enumerator
->destroy(enumerator
);
228 * Get hosts to use for dynamic traffic selectors
230 static linked_list_t
*get_dynamic_hosts(ike_sa_t
*ike_sa
, bool local
)
232 enumerator_t
*enumerator
;
236 list
= linked_list_create();
237 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
238 while (enumerator
->enumerate(enumerator
, &host
))
240 list
->insert_last(list
, host
);
242 enumerator
->destroy(enumerator
);
244 if (list
->get_count(list
) == 0)
245 { /* no virtual IPs assigned */
248 host
= ike_sa
->get_my_host(ike_sa
);
249 list
->insert_last(list
, host
);
251 else if (!have_pool(ike_sa
))
252 { /* use host only if we don't have a pool configured */
253 host
= ike_sa
->get_other_host(ike_sa
);
254 list
->insert_last(list
, host
);
261 * Install negotiated CHILD_SA
263 static bool install(private_quick_mode_t
*this)
265 status_t status
, status_i
, status_o
;
266 chunk_t encr_i
, encr_r
, integ_i
, integ_r
;
267 linked_list_t
*tsi
, *tsr
, *my_ts
, *other_ts
;
268 child_sa_t
*old
= NULL
;
270 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
271 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
272 this->child_sa
->set_mode(this->child_sa
, this->mode
);
274 if (this->cpi_i
&& this->cpi_r
)
275 { /* DEFLATE is the only transform we currently support */
276 this->child_sa
->set_ipcomp(this->child_sa
, IPCOMP_DEFLATE
);
280 this->cpi_i
= this->cpi_r
= 0;
283 this->child_sa
->set_protocol(this->child_sa
,
284 this->proposal
->get_protocol(this->proposal
));
286 status_i
= status_o
= FAILED
;
287 encr_i
= encr_r
= integ_i
= integ_r
= chunk_empty
;
288 tsi
= linked_list_create_with_items(this->tsi
->clone(this->tsi
), NULL
);
289 tsr
= linked_list_create_with_items(this->tsr
->clone(this->tsr
), NULL
);
292 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
293 NARROW_INITIATOR_POST_AUTH
, tsi
, tsr
);
297 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
298 NARROW_RESPONDER_POST
, tsr
, tsi
);
300 if (tsi
->get_count(tsi
) == 0 || tsr
->get_count(tsr
) == 0)
302 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
303 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
304 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
308 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
, this->dh
,
309 this->spi_i
, this->spi_r
, this->nonce_i
, this->nonce_r
,
310 &encr_i
, &integ_i
, &encr_r
, &integ_r
))
314 status_i
= this->child_sa
->install(this->child_sa
,
315 encr_r
, integ_r
, this->spi_i
, this->cpi_i
,
316 this->initiator
, TRUE
, FALSE
, tsi
, tsr
);
317 status_o
= this->child_sa
->install(this->child_sa
,
318 encr_i
, integ_i
, this->spi_r
, this->cpi_r
,
319 this->initiator
, FALSE
, FALSE
, tsi
, tsr
);
323 status_i
= this->child_sa
->install(this->child_sa
,
324 encr_i
, integ_i
, this->spi_r
, this->cpi_r
,
325 this->initiator
, TRUE
, FALSE
, tsr
, tsi
);
326 status_o
= this->child_sa
->install(this->child_sa
,
327 encr_r
, integ_r
, this->spi_i
, this->cpi_i
,
328 this->initiator
, FALSE
, FALSE
, tsr
, tsi
);
331 chunk_clear(&integ_i
);
332 chunk_clear(&integ_r
);
333 chunk_clear(&encr_i
);
334 chunk_clear(&encr_r
);
336 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
338 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
339 (status_i
!= SUCCESS
) ?
"inbound " : "",
340 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ?
"and ": "",
341 (status_o
!= SUCCESS
) ?
"outbound " : "");
342 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
343 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
349 status
= this->child_sa
->add_policies(this->child_sa
, tsi
, tsr
);
353 status
= this->child_sa
->add_policies(this->child_sa
, tsr
, tsi
);
355 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
356 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
357 if (status
!= SUCCESS
)
359 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
363 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
364 this->dh
, this->nonce_i
, this->nonce_r
);
366 /* add to IKE_SA, and remove from task */
367 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
368 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
370 my_ts
= linked_list_create_from_enumerator(
371 this->child_sa
->create_ts_enumerator(this->child_sa
, TRUE
));
372 other_ts
= linked_list_create_from_enumerator(
373 this->child_sa
->create_ts_enumerator(this->child_sa
, FALSE
));
375 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
376 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
377 this->child_sa
->get_name(this->child_sa
),
378 this->child_sa
->get_reqid(this->child_sa
),
379 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
380 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)), my_ts
, other_ts
);
382 my_ts
->destroy(my_ts
);
383 other_ts
->destroy(other_ts
);
387 old
= this->ike_sa
->get_child_sa(this->ike_sa
,
388 this->proposal
->get_protocol(this->proposal
),
393 charon
->bus
->child_rekey(charon
->bus
, old
, this->child_sa
);
397 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
401 schedule_inactivity_timeout(this);
403 this->child_sa
= NULL
;
408 * Generate and add NONCE
410 static bool add_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
413 nonce_payload_t
*nonce_payload
;
416 nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
419 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
422 if (!nonceg
->allocate_nonce(nonceg
, NONCE_SIZE
, nonce
))
424 DBG1(DBG_IKE
, "nonce allocation failed");
425 nonceg
->destroy(nonceg
);
428 nonceg
->destroy(nonceg
);
430 nonce_payload
= nonce_payload_create(NONCE_V1
);
431 nonce_payload
->set_nonce(nonce_payload
, *nonce
);
432 message
->add_payload(message
, &nonce_payload
->payload_interface
);
438 * Extract nonce from NONCE payload
440 static bool get_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
443 nonce_payload_t
*nonce_payload
;
445 nonce_payload
= (nonce_payload_t
*)message
->get_payload(message
, NONCE_V1
);
448 DBG1(DBG_IKE
, "NONCE payload missing in message");
451 *nonce
= nonce_payload
->get_nonce(nonce_payload
);
457 * Add KE payload to message
459 static void add_ke(private_quick_mode_t
*this, message_t
*message
)
461 ke_payload_t
*ke_payload
;
463 ke_payload
= ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1
, this->dh
);
464 message
->add_payload(message
, &ke_payload
->payload_interface
);
468 * Get DH value from a KE payload
470 static bool get_ke(private_quick_mode_t
*this, message_t
*message
)
472 ke_payload_t
*ke_payload
;
474 ke_payload
= (ke_payload_t
*)message
->get_payload(message
, KEY_EXCHANGE_V1
);
477 DBG1(DBG_IKE
, "KE payload missing");
480 this->dh
->set_other_public_value(this->dh
,
481 ke_payload
->get_key_exchange_data(ke_payload
));
486 * Select a traffic selector from configuration
488 static traffic_selector_t
* select_ts(private_quick_mode_t
*this, bool local
,
489 linked_list_t
*supplied
)
491 traffic_selector_t
*ts
;
492 linked_list_t
*list
, *hosts
;
494 hosts
= get_dynamic_hosts(this->ike_sa
, local
);
495 list
= this->config
->get_traffic_selectors(this->config
,
496 local
, supplied
, hosts
);
497 hosts
->destroy(hosts
);
498 if (list
->get_first(list
, (void**)&ts
) == SUCCESS
)
504 DBG1(DBG_IKE
, "%s traffic selector missing in configuration",
505 local ?
"local" : "remote");
508 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
513 * Add selected traffic selectors to message
515 static void add_ts(private_quick_mode_t
*this, message_t
*message
)
517 id_payload_t
*id_payload
;
519 id_payload
= id_payload_create_from_ts(this->tsi
);
520 message
->add_payload(message
, &id_payload
->payload_interface
);
521 id_payload
= id_payload_create_from_ts(this->tsr
);
522 message
->add_payload(message
, &id_payload
->payload_interface
);
526 * Get traffic selectors from received message
528 static bool get_ts(private_quick_mode_t
*this, message_t
*message
)
530 traffic_selector_t
*tsi
= NULL
, *tsr
= NULL
;
531 enumerator_t
*enumerator
;
532 id_payload_t
*id_payload
;
537 enumerator
= message
->create_payload_enumerator(message
);
538 while (enumerator
->enumerate(enumerator
, &payload
))
540 if (payload
->get_type(payload
) == ID_V1
)
542 id_payload
= (id_payload_t
*)payload
;
546 tsi
= id_payload
->get_ts(id_payload
);
551 tsr
= id_payload
->get_ts(id_payload
);
556 enumerator
->destroy(enumerator
);
558 /* create host2host selectors if ID payloads missing */
561 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
562 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
566 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
567 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
571 tsi
= traffic_selector_create_from_subnet(hsi
->clone(hsi
),
572 hsi
->get_family(hsi
) == AF_INET ?
32 : 128, 0, 0, 65535);
576 tsr
= traffic_selector_create_from_subnet(hsr
->clone(hsr
),
577 hsr
->get_family(hsr
) == AF_INET ?
32 : 128, 0, 0, 65535);
579 if (this->mode
== MODE_TRANSPORT
&& this->udp
&&
580 (!tsi
->is_host(tsi
, hsi
) || !tsr
->is_host(tsr
, hsr
)))
581 { /* change TS in case of a NAT in transport mode */
582 DBG2(DBG_IKE
, "changing received traffic selectors %R=== %R due to NAT",
584 tsi
->set_address(tsi
, hsi
);
585 tsr
->set_address(tsr
, hsr
);
590 traffic_selector_t
*tsisub
, *tsrsub
;
592 /* check if peer selection is valid */
593 tsisub
= this->tsi
->get_subset(this->tsi
, tsi
);
594 tsrsub
= this->tsr
->get_subset(this->tsr
, tsr
);
595 if (!tsisub
|| !tsrsub
)
597 DBG1(DBG_IKE
, "peer selected invalid traffic selectors: "
598 "%R for %R, %R for %R", tsi
, this->tsi
, tsr
, this->tsr
);
607 this->tsi
->destroy(this->tsi
);
608 this->tsr
->destroy(this->tsr
);
623 static encap_t
get_encap(ike_sa_t
* ike_sa
, bool udp
)
629 if (ike_sa
->supports_extension(ike_sa
, EXT_NATT_DRAFT_02_03
))
631 return ENCAP_UDP_DRAFT_00_03
;
637 * Get NAT-OA payload type (RFC 3947 or RFC 3947 drafts).
639 static payload_type_t
get_nat_oa_payload_type(ike_sa_t
*ike_sa
)
641 if (ike_sa
->supports_extension(ike_sa
, EXT_NATT_DRAFT_02_03
))
643 return NAT_OA_DRAFT_00_03_V1
;
649 * Add NAT-OA payloads
651 static void add_nat_oa_payloads(private_quick_mode_t
*this, message_t
*message
)
653 identification_t
*id
;
654 id_payload_t
*nat_oa
;
656 payload_type_t nat_oa_payload_type
;
658 src
= message
->get_source(message
);
659 dst
= message
->get_destination(message
);
661 src
= this->initiator ? src
: dst
;
662 dst
= this->initiator ? dst
: src
;
664 nat_oa_payload_type
= get_nat_oa_payload_type(this->ike_sa
);
666 /* first NAT-OA is the initiator's address */
667 id
= identification_create_from_sockaddr(src
->get_sockaddr(src
));
668 nat_oa
= id_payload_create_from_identification(nat_oa_payload_type
, id
);
669 message
->add_payload(message
, (payload_t
*)nat_oa
);
672 /* second NAT-OA is that of the responder */
673 id
= identification_create_from_sockaddr(dst
->get_sockaddr(dst
));
674 nat_oa
= id_payload_create_from_identification(nat_oa_payload_type
, id
);
675 message
->add_payload(message
, (payload_t
*)nat_oa
);
682 static void get_lifetimes(private_quick_mode_t
*this)
686 lft
= this->config
->get_lifetime(this->config
);
689 this->lifetime
= lft
->time
.life
;
691 else if (lft
->bytes
.life
)
693 this->lifebytes
= lft
->bytes
.life
;
699 * Check and apply lifetimes
701 static void apply_lifetimes(private_quick_mode_t
*this, sa_payload_t
*sa_payload
)
706 lifetime
= sa_payload
->get_lifetime(sa_payload
);
707 lifebytes
= sa_payload
->get_lifebytes(sa_payload
);
708 if (this->lifetime
!= lifetime
)
710 DBG1(DBG_IKE
, "received %us lifetime, configured %us",
711 lifetime
, this->lifetime
);
712 this->lifetime
= lifetime
;
714 if (this->lifebytes
!= lifebytes
)
716 DBG1(DBG_IKE
, "received %llu lifebytes, configured %llu",
717 lifebytes
, this->lifebytes
);
718 this->lifebytes
= lifebytes
;
723 * Set the task ready to build notify error message
725 static status_t
send_notify(private_quick_mode_t
*this, notify_type_t type
)
727 notify_payload_t
*notify
;
729 notify
= notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
731 notify
->set_spi(notify
, this->spi_i
);
733 this->ike_sa
->queue_task(this->ike_sa
,
734 (task_t
*)informational_create(this->ike_sa
, notify
));
735 /* cancel all active/passive tasks in favour of informational */
736 this->ike_sa
->flush_queue(this->ike_sa
,
737 this->initiator ? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
742 * Prepare a list of proposals from child_config containing only the specified
743 * DH group, unless it is set to MODP_NONE.
745 static linked_list_t
*get_proposals(private_quick_mode_t
*this,
746 diffie_hellman_group_t group
)
749 proposal_t
*proposal
;
750 enumerator_t
*enumerator
;
752 list
= this->config
->get_proposals(this->config
, FALSE
);
753 enumerator
= list
->create_enumerator(list
);
754 while (enumerator
->enumerate(enumerator
, &proposal
))
756 if (group
!= MODP_NONE
)
758 if (!proposal
->has_dh_group(proposal
, group
))
760 list
->remove_at(list
, enumerator
);
761 proposal
->destroy(proposal
);
764 proposal
->strip_dh(proposal
, group
);
766 proposal
->set_spi(proposal
, this->spi_i
);
768 enumerator
->destroy(enumerator
);
773 METHOD(task_t
, build_i
, status_t
,
774 private_quick_mode_t
*this, message_t
*message
)
780 sa_payload_t
*sa_payload
;
781 linked_list_t
*list
, *tsi
, *tsr
;
782 proposal_t
*proposal
;
783 diffie_hellman_group_t group
;
786 this->udp
= this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
);
787 this->mode
= this->config
->get_mode(this->config
);
788 this->child_sa
= child_sa_create(
789 this->ike_sa
->get_my_host(this->ike_sa
),
790 this->ike_sa
->get_other_host(this->ike_sa
),
791 this->config
, this->reqid
, this->udp
);
793 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
795 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
796 add_nat_oa_payloads(this, message
);
799 if (this->config
->use_ipcomp(this->config
))
801 this->cpi_i
= this->child_sa
->alloc_cpi(this->child_sa
);
804 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, "
809 list
= this->config
->get_proposals(this->config
, MODP_NONE
);
810 if (list
->get_first(list
, (void**)&proposal
) == SUCCESS
)
812 this->proto
= proposal
->get_protocol(proposal
);
814 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
815 this->spi_i
= this->child_sa
->alloc_spi(this->child_sa
, this->proto
);
818 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
822 group
= this->config
->get_dh_group(this->config
);
823 if (group
!= MODP_NONE
)
825 proposal_t
*proposal
;
826 u_int16_t preferred_group
;
828 proposal
= this->ike_sa
->get_proposal(this->ike_sa
);
829 proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
,
830 &preferred_group
, NULL
);
831 /* try the negotiated DH group from IKE_SA */
832 list
= get_proposals(this, preferred_group
);
833 if (list
->get_count(list
))
835 group
= preferred_group
;
839 /* fall back to the first configured DH group */
841 list
= get_proposals(this, group
);
844 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
848 DBG1(DBG_IKE
, "configured DH group %N not supported",
849 diffie_hellman_group_names
, group
);
850 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
856 list
= get_proposals(this, MODP_NONE
);
860 encap
= get_encap(this->ike_sa
, this->udp
);
861 sa_payload
= sa_payload_create_from_proposals_v1(list
,
862 this->lifetime
, this->lifebytes
, AUTH_NONE
,
863 this->mode
, encap
, this->cpi_i
);
864 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
865 message
->add_payload(message
, &sa_payload
->payload_interface
);
867 if (!add_nonce(this, &this->nonce_i
, message
))
871 if (group
!= MODP_NONE
)
873 add_ke(this, message
);
877 this->tsi
= select_ts(this, TRUE
, NULL
);
881 this->tsr
= select_ts(this, FALSE
, NULL
);
883 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
884 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
885 this->tsi
= this->tsr
= NULL
;
886 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
887 NARROW_INITIATOR_PRE_AUTH
, tsi
, tsr
);
888 tsi
->remove_first(tsi
, (void**)&this->tsi
);
889 tsr
->remove_first(tsr
, (void**)&this->tsr
);
890 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
891 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
892 if (!this->tsi
|| !this->tsr
)
896 add_ts(this, message
);
909 * Check for notify errors, return TRUE if error found
911 static bool has_notify_errors(private_quick_mode_t
*this, message_t
*message
)
913 enumerator_t
*enumerator
;
917 enumerator
= message
->create_payload_enumerator(message
);
918 while (enumerator
->enumerate(enumerator
, &payload
))
920 if (payload
->get_type(payload
) == NOTIFY_V1
)
922 notify_payload_t
*notify
;
925 notify
= (notify_payload_t
*)payload
;
926 type
= notify
->get_notify_type(notify
);
930 DBG1(DBG_IKE
, "received %N error notify",
931 notify_type_names
, type
);
936 DBG1(DBG_IKE
, "received %N notify", notify_type_names
, type
);
940 enumerator
->destroy(enumerator
);
946 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
948 static void check_for_rekeyed_child(private_quick_mode_t
*this)
950 enumerator_t
*enumerator
, *policies
;
951 traffic_selector_t
*local
, *remote
;
952 child_sa_t
*child_sa
;
953 proposal_t
*proposal
;
956 name
= this->config
->get_name(this->config
);
957 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
958 while (this->reqid
== 0 && enumerator
->enumerate(enumerator
, &child_sa
))
960 if (streq(child_sa
->get_name(child_sa
), name
))
962 proposal
= child_sa
->get_proposal(child_sa
);
963 switch (child_sa
->get_state(child_sa
))
965 case CHILD_INSTALLED
:
967 policies
= child_sa
->create_policy_enumerator(child_sa
);
968 if (policies
->enumerate(policies
, &local
, &remote
) &&
969 local
->equals(local
, this->tsr
) &&
970 remote
->equals(remote
, this->tsi
) &&
971 this->proposal
->equals(this->proposal
, proposal
))
973 this->reqid
= child_sa
->get_reqid(child_sa
);
974 this->rekey
= child_sa
->get_spi(child_sa
, TRUE
);
975 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
976 DBG1(DBG_IKE
, "detected rekeying of CHILD_SA %s{%u}",
977 child_sa
->get_name(child_sa
), this->reqid
);
979 policies
->destroy(policies
);
986 enumerator
->destroy(enumerator
);
989 METHOD(task_t
, process_r
, status_t
,
990 private_quick_mode_t
*this, message_t
*message
)
996 sa_payload_t
*sa_payload
;
997 linked_list_t
*tsi
, *tsr
, *hostsi
, *hostsr
, *list
= NULL
;
998 peer_cfg_t
*peer_cfg
;
1002 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1003 SECURITY_ASSOCIATION_V1
);
1006 DBG1(DBG_IKE
, "sa payload missing");
1007 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1010 this->mode
= sa_payload
->get_encap_mode(sa_payload
, &this->udp
);
1012 if (!get_ts(this, message
))
1016 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1017 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
1018 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
1019 this->tsi
= this->tsr
= NULL
;
1020 hostsi
= get_dynamic_hosts(this->ike_sa
, FALSE
);
1021 hostsr
= get_dynamic_hosts(this->ike_sa
, TRUE
);
1022 this->config
= peer_cfg
->select_child_cfg(peer_cfg
, tsr
, tsi
,
1024 hostsi
->destroy(hostsi
);
1025 hostsr
->destroy(hostsr
);
1028 this->tsi
= select_ts(this, FALSE
, tsi
);
1029 this->tsr
= select_ts(this, TRUE
, tsr
);
1031 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1032 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1033 if (!this->config
|| !this->tsi
|| !this->tsr
)
1035 DBG1(DBG_IKE
, "no matching CHILD_SA config found");
1036 return send_notify(this, INVALID_ID_INFORMATION
);
1039 if (this->config
->use_ipcomp(this->config
))
1041 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1043 if (!list
->get_count(list
))
1045 DBG1(DBG_IKE
, "expected IPComp proposal but peer did "
1046 "not send one, IPComp disabled");
1050 if (!list
|| !list
->get_count(list
))
1053 list
= sa_payload
->get_proposals(sa_payload
);
1055 private = this->ike_sa
->supports_extension(this->ike_sa
,
1057 this->proposal
= this->config
->select_proposal(this->config
,
1058 list
, FALSE
, private);
1059 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1061 get_lifetimes(this);
1062 apply_lifetimes(this, sa_payload
);
1064 if (!this->proposal
)
1066 DBG1(DBG_IKE
, "no matching proposal found, sending %N",
1067 notify_type_names
, NO_PROPOSAL_CHOSEN
);
1068 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1070 this->spi_i
= this->proposal
->get_spi(this->proposal
);
1072 if (!get_nonce(this, &this->nonce_i
, message
))
1074 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1077 if (this->proposal
->get_algorithm(this->proposal
,
1078 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
1080 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
1084 DBG1(DBG_IKE
, "negotiated DH group %N not supported",
1085 diffie_hellman_group_names
, group
);
1086 return send_notify(this, INVALID_KEY_INFORMATION
);
1088 if (!get_ke(this, message
))
1090 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1094 check_for_rekeyed_child(this);
1096 this->child_sa
= child_sa_create(
1097 this->ike_sa
->get_my_host(this->ike_sa
),
1098 this->ike_sa
->get_other_host(this->ike_sa
),
1099 this->config
, this->reqid
, this->udp
);
1101 tsi
= linked_list_create_with_items(this->tsi
, NULL
);
1102 tsr
= linked_list_create_with_items(this->tsr
, NULL
);
1103 this->tsi
= this->tsr
= NULL
;
1104 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
1105 NARROW_RESPONDER
, tsr
, tsi
);
1106 if (tsi
->remove_first(tsi
, (void**)&this->tsi
) != SUCCESS
||
1107 tsr
->remove_first(tsr
, (void**)&this->tsr
) != SUCCESS
)
1109 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1110 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1111 return send_notify(this, INVALID_ID_INFORMATION
);
1113 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1114 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1120 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
1121 has_notify_errors(this, message
))
1127 ike_sa_t
*ike_sa
= this->ike_sa
;
1130 task
= (task_t
*)quick_delete_create(this->ike_sa
,
1131 this->proposal
->get_protocol(this->proposal
),
1132 this->spi_i
, TRUE
, TRUE
);
1133 /* flush_queue() destroys the current task */
1134 ike_sa
->flush_queue(ike_sa
, TASK_QUEUE_PASSIVE
);
1135 ike_sa
->queue_task(ike_sa
, task
);
1136 return ALREADY_DONE
;
1145 METHOD(task_t
, build_r
, status_t
,
1146 private_quick_mode_t
*this, message_t
*message
)
1148 switch (this->state
)
1152 sa_payload_t
*sa_payload
;
1155 this->proto
= this->proposal
->get_protocol(this->proposal
);
1156 this->spi_r
= this->child_sa
->alloc_spi(this->child_sa
, this->proto
);
1159 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
1160 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1162 this->proposal
->set_spi(this->proposal
, this->spi_r
);
1166 this->cpi_r
= this->child_sa
->alloc_cpi(this->child_sa
);
1169 DBG1(DBG_IKE
, "unable to allocate a CPI from "
1170 "kernel, IPComp disabled");
1171 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1175 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
1177 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1178 add_nat_oa_payloads(this, message
);
1181 encap
= get_encap(this->ike_sa
, this->udp
);
1182 sa_payload
= sa_payload_create_from_proposal_v1(this->proposal
,
1183 this->lifetime
, this->lifebytes
, AUTH_NONE
,
1184 this->mode
, encap
, this->cpi_r
);
1185 message
->add_payload(message
, &sa_payload
->payload_interface
);
1187 if (!add_nonce(this, &this->nonce_r
, message
))
1193 add_ke(this, message
);
1196 add_ts(this, message
);
1198 this->state
= QM_NEGOTIATED
;
1206 METHOD(task_t
, process_i
, status_t
,
1207 private_quick_mode_t
*this, message_t
*message
)
1209 switch (this->state
)
1213 sa_payload_t
*sa_payload
;
1214 linked_list_t
*list
= NULL
;
1217 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1218 SECURITY_ASSOCIATION_V1
);
1221 DBG1(DBG_IKE
, "sa payload missing");
1222 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1226 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1228 if (!list
->get_count(list
))
1230 DBG1(DBG_IKE
, "peer did not acccept our IPComp proposal, "
1235 if (!list
|| !list
->get_count(list
))
1238 list
= sa_payload
->get_proposals(sa_payload
);
1240 private = this->ike_sa
->supports_extension(this->ike_sa
,
1242 this->proposal
= this->config
->select_proposal(this->config
,
1243 list
, FALSE
, private);
1244 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1245 if (!this->proposal
)
1247 DBG1(DBG_IKE
, "no matching proposal found");
1248 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1250 this->spi_r
= this->proposal
->get_spi(this->proposal
);
1252 apply_lifetimes(this, sa_payload
);
1254 if (!get_nonce(this, &this->nonce_r
, message
))
1256 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1258 if (this->dh
&& !get_ke(this, message
))
1260 return send_notify(this, INVALID_KEY_INFORMATION
);
1262 if (!get_ts(this, message
))
1264 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1268 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1270 this->state
= QM_NEGOTIATED
;
1278 METHOD(task_t
, get_type
, task_type_t
,
1279 private_quick_mode_t
*this)
1281 return TASK_QUICK_MODE
;
1284 METHOD(quick_mode_t
, use_reqid
, void,
1285 private_quick_mode_t
*this, u_int32_t reqid
)
1287 this->reqid
= reqid
;
1290 METHOD(quick_mode_t
, rekey
, void,
1291 private_quick_mode_t
*this, u_int32_t spi
)
1296 METHOD(task_t
, migrate
, void,
1297 private_quick_mode_t
*this, ike_sa_t
*ike_sa
)
1299 chunk_free(&this->nonce_i
);
1300 chunk_free(&this->nonce_r
);
1301 DESTROY_IF(this->tsi
);
1302 DESTROY_IF(this->tsr
);
1303 DESTROY_IF(this->proposal
);
1304 DESTROY_IF(this->child_sa
);
1305 DESTROY_IF(this->dh
);
1307 this->ike_sa
= ike_sa
;
1308 this->keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
);
1309 this->state
= QM_INIT
;
1312 this->proposal
= NULL
;
1313 this->child_sa
= NULL
;
1318 if (!this->initiator
)
1320 DESTROY_IF(this->config
);
1321 this->config
= NULL
;
1325 METHOD(task_t
, destroy
, void,
1326 private_quick_mode_t
*this)
1328 chunk_free(&this->nonce_i
);
1329 chunk_free(&this->nonce_r
);
1330 DESTROY_IF(this->tsi
);
1331 DESTROY_IF(this->tsr
);
1332 DESTROY_IF(this->proposal
);
1333 DESTROY_IF(this->child_sa
);
1334 DESTROY_IF(this->config
);
1335 DESTROY_IF(this->dh
);
1340 * Described in header.
1342 quick_mode_t
*quick_mode_create(ike_sa_t
*ike_sa
, child_cfg_t
*config
,
1343 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1345 private_quick_mode_t
*this;
1350 .get_type
= _get_type
,
1351 .migrate
= _migrate
,
1352 .destroy
= _destroy
,
1354 .use_reqid
= _use_reqid
,
1358 .initiator
= config
!= NULL
,
1360 .keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
),
1362 .tsi
= tsi ? tsi
->clone(tsi
) : NULL
,
1363 .tsr
= tsr ? tsr
->clone(tsr
) : NULL
,
1369 this->public.task
.build
= _build_i
;
1370 this->public.task
.process
= _process_i
;
1374 this->public.task
.build
= _build_r
;
1375 this->public.task
.process
= _process_r
;
1378 return &this->public;