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
19 #include "quick_mode.h"
24 #include <sa/ikev1/keymat_v1.h>
25 #include <encoding/payloads/sa_payload.h>
26 #include <encoding/payloads/nonce_payload.h>
27 #include <encoding/payloads/ke_payload.h>
28 #include <encoding/payloads/id_payload.h>
29 #include <encoding/payloads/payload.h>
30 #include <sa/ikev1/tasks/informational.h>
31 #include <sa/ikev1/tasks/quick_delete.h>
32 #include <processing/jobs/inactivity_job.h>
34 typedef struct private_quick_mode_t private_quick_mode_t
;
37 * Private members of a quick_mode_t task.
39 struct private_quick_mode_t
{
42 * Public methods and task_t interface.
52 * TRUE if we are initiating quick mode
57 * Traffic selector of initiator
59 traffic_selector_t
*tsi
;
62 * Traffic selector of responder
64 traffic_selector_t
*tsr
;
87 * Initiators IPComp CPI
92 * Responders IPComp CPI
97 * selected CHILD_SA proposal
102 * Config of CHILD_SA to establish
107 * CHILD_SA we are about to establish
109 child_sa_t
*child_sa
;
117 * DH exchange, when PFS is in use
119 diffie_hellman_t
*dh
;
122 * Negotiated lifetime of new SA
127 * Negotaited lifebytes of new SA
132 * Reqid to use, 0 for auto-allocate
142 * Negotiated mode, tunnel or transport
147 * Use UDP encapsulation
151 /** states of quick mode */
159 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
161 static void schedule_inactivity_timeout(private_quick_mode_t
*this)
166 timeout
= this->config
->get_inactivity(this->config
);
169 close_ike
= lib
->settings
->get_bool(lib
->settings
,
170 "%s.inactivity_close_ike", FALSE
, charon
->name
);
171 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
172 inactivity_job_create(this->child_sa
->get_reqid(this->child_sa
),
173 timeout
, close_ike
), timeout
);
178 * Check if we have a an address pool configured
180 static bool have_pool(ike_sa_t
*ike_sa
)
182 enumerator_t
*enumerator
;
183 peer_cfg_t
*peer_cfg
;
187 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
190 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
191 if (enumerator
->enumerate(enumerator
, &pool
))
195 enumerator
->destroy(enumerator
);
201 * Get host to use for dynamic traffic selectors
203 static host_t
*get_dynamic_host(ike_sa_t
*ike_sa
, bool local
)
205 enumerator_t
*enumerator
;
208 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
209 if (!enumerator
->enumerate(enumerator
, &host
))
213 host
= ike_sa
->get_my_host(ike_sa
);
217 if (have_pool(ike_sa
))
219 /* we have an IP address pool, but didn't negotiate a
225 host
= ike_sa
->get_other_host(ike_sa
);
229 enumerator
->destroy(enumerator
);
234 * Install negotiated CHILD_SA
236 static bool install(private_quick_mode_t
*this)
238 status_t status
, status_i
, status_o
;
239 chunk_t encr_i
, encr_r
, integ_i
, integ_r
;
240 linked_list_t
*tsi
, *tsr
;
241 child_sa_t
*old
= NULL
;
243 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
244 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
245 this->child_sa
->set_mode(this->child_sa
, this->mode
);
247 if (this->cpi_i
&& this->cpi_r
)
248 { /* DEFLATE is the only transform we currently support */
249 this->child_sa
->set_ipcomp(this->child_sa
, IPCOMP_DEFLATE
);
253 this->cpi_i
= this->cpi_r
= 0;
256 this->child_sa
->set_protocol(this->child_sa
,
257 this->proposal
->get_protocol(this->proposal
));
259 status_i
= status_o
= FAILED
;
260 encr_i
= encr_r
= integ_i
= integ_r
= chunk_empty
;
261 tsi
= linked_list_create();
262 tsr
= linked_list_create();
263 tsi
->insert_last(tsi
, this->tsi
->clone(this->tsi
));
264 tsr
->insert_last(tsr
, this->tsr
->clone(this->tsr
));
267 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
268 NARROW_INITIATOR_POST_AUTH
, tsi
, tsr
);
272 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
273 NARROW_RESPONDER_POST
, tsr
, tsi
);
275 if (tsi
->get_count(tsi
) == 0 || tsr
->get_count(tsr
) == 0)
277 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
278 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
279 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
283 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
, this->dh
,
284 this->spi_i
, this->spi_r
, this->nonce_i
, this->nonce_r
,
285 &encr_i
, &integ_i
, &encr_r
, &integ_r
))
289 status_i
= this->child_sa
->install(this->child_sa
, encr_r
, integ_r
,
290 this->spi_i
, this->cpi_i
, TRUE
, FALSE
, tsi
, tsr
);
291 status_o
= this->child_sa
->install(this->child_sa
, encr_i
, integ_i
,
292 this->spi_r
, this->cpi_r
, FALSE
, FALSE
, tsi
, tsr
);
296 status_i
= this->child_sa
->install(this->child_sa
, encr_i
, integ_i
,
297 this->spi_r
, this->cpi_r
, TRUE
, FALSE
, tsr
, tsi
);
298 status_o
= this->child_sa
->install(this->child_sa
, encr_r
, integ_r
,
299 this->spi_i
, this->cpi_i
, FALSE
, FALSE
, tsr
, tsi
);
302 chunk_clear(&integ_i
);
303 chunk_clear(&integ_r
);
304 chunk_clear(&encr_i
);
305 chunk_clear(&encr_r
);
307 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
309 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
310 (status_i
!= SUCCESS
) ?
"inbound " : "",
311 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ?
"and ": "",
312 (status_o
!= SUCCESS
) ?
"outbound " : "");
313 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
314 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
320 status
= this->child_sa
->add_policies(this->child_sa
, tsi
, tsr
);
324 status
= this->child_sa
->add_policies(this->child_sa
, tsr
, tsi
);
326 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
327 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
328 if (status
!= SUCCESS
)
330 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
334 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
335 this->dh
, this->nonce_i
, this->nonce_r
);
337 /* add to IKE_SA, and remove from task */
338 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
339 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
341 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
342 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
343 this->child_sa
->get_name(this->child_sa
),
344 this->child_sa
->get_reqid(this->child_sa
),
345 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
346 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)),
347 this->child_sa
->get_traffic_selectors(this->child_sa
, TRUE
),
348 this->child_sa
->get_traffic_selectors(this->child_sa
, FALSE
));
352 old
= this->ike_sa
->get_child_sa(this->ike_sa
,
353 this->proposal
->get_protocol(this->proposal
),
358 charon
->bus
->child_rekey(charon
->bus
, old
, this->child_sa
);
362 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
366 schedule_inactivity_timeout(this);
368 this->child_sa
= NULL
;
373 * Generate and add NONCE
375 static bool add_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
378 nonce_payload_t
*nonce_payload
;
381 nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
384 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
387 if (!nonceg
->allocate_nonce(nonceg
, NONCE_SIZE
, nonce
))
389 DBG1(DBG_IKE
, "nonce allocation failed");
390 nonceg
->destroy(nonceg
);
393 nonceg
->destroy(nonceg
);
395 nonce_payload
= nonce_payload_create(NONCE_V1
);
396 nonce_payload
->set_nonce(nonce_payload
, *nonce
);
397 message
->add_payload(message
, &nonce_payload
->payload_interface
);
403 * Extract nonce from NONCE payload
405 static bool get_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
408 nonce_payload_t
*nonce_payload
;
410 nonce_payload
= (nonce_payload_t
*)message
->get_payload(message
, NONCE_V1
);
413 DBG1(DBG_IKE
, "NONCE payload missing in message");
416 *nonce
= nonce_payload
->get_nonce(nonce_payload
);
422 * Add KE payload to message
424 static void add_ke(private_quick_mode_t
*this, message_t
*message
)
426 ke_payload_t
*ke_payload
;
428 ke_payload
= ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1
, this->dh
);
429 message
->add_payload(message
, &ke_payload
->payload_interface
);
433 * Get DH value from a KE payload
435 static bool get_ke(private_quick_mode_t
*this, message_t
*message
)
437 ke_payload_t
*ke_payload
;
439 ke_payload
= (ke_payload_t
*)message
->get_payload(message
, KEY_EXCHANGE_V1
);
442 DBG1(DBG_IKE
, "KE payload missing");
445 this->dh
->set_other_public_value(this->dh
,
446 ke_payload
->get_key_exchange_data(ke_payload
));
451 * Select a traffic selector from configuration
453 static traffic_selector_t
* select_ts(private_quick_mode_t
*this, bool local
,
454 linked_list_t
*supplied
)
456 traffic_selector_t
*ts
;
459 list
= this->config
->get_traffic_selectors(this->config
, local
,
460 supplied
, get_dynamic_host(this->ike_sa
, local
));
461 if (list
->get_first(list
, (void**)&ts
) == SUCCESS
)
463 if (this->initiator
&& list
->get_count(list
) > 1)
465 DBG1(DBG_IKE
, "configuration has more than one %s traffic selector,"
466 " using first only", local ?
"local" : "remote");
472 DBG1(DBG_IKE
, "%s traffic selector missing in configuration",
473 local ?
"local" : "local");
476 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
481 * Add selected traffic selectors to message
483 static void add_ts(private_quick_mode_t
*this, message_t
*message
)
485 id_payload_t
*id_payload
;
490 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
491 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
495 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
496 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
498 /* add ID payload only if negotiating non host2host tunnels */
499 if (!this->tsi
->is_host(this->tsi
, hsi
) ||
500 !this->tsr
->is_host(this->tsr
, hsr
) ||
501 this->tsi
->get_protocol(this->tsi
) ||
502 this->tsr
->get_protocol(this->tsr
) ||
503 this->tsi
->get_from_port(this->tsi
) ||
504 this->tsr
->get_from_port(this->tsr
) ||
505 this->tsi
->get_to_port(this->tsi
) != 65535 ||
506 this->tsr
->get_to_port(this->tsr
) != 65535)
508 id_payload
= id_payload_create_from_ts(this->tsi
);
509 message
->add_payload(message
, &id_payload
->payload_interface
);
510 id_payload
= id_payload_create_from_ts(this->tsr
);
511 message
->add_payload(message
, &id_payload
->payload_interface
);
516 * Get traffic selectors from received message
518 static bool get_ts(private_quick_mode_t
*this, message_t
*message
)
520 traffic_selector_t
*tsi
= NULL
, *tsr
= NULL
;
521 enumerator_t
*enumerator
;
522 id_payload_t
*id_payload
;
527 enumerator
= message
->create_payload_enumerator(message
);
528 while (enumerator
->enumerate(enumerator
, &payload
))
530 if (payload
->get_type(payload
) == ID_V1
)
532 id_payload
= (id_payload_t
*)payload
;
536 tsi
= id_payload
->get_ts(id_payload
);
541 tsr
= id_payload
->get_ts(id_payload
);
546 enumerator
->destroy(enumerator
);
548 /* create host2host selectors if ID payloads missing */
551 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
552 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
556 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
557 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
561 tsi
= traffic_selector_create_from_subnet(hsi
->clone(hsi
),
562 hsi
->get_family(hsi
) == AF_INET ?
32 : 128, 0, 0);
566 tsr
= traffic_selector_create_from_subnet(hsr
->clone(hsr
),
567 hsr
->get_family(hsr
) == AF_INET ?
32 : 128, 0, 0);
569 if (!this->initiator
&& this->mode
== MODE_TRANSPORT
&& this->udp
&&
570 (!tsi
->is_host(tsi
, hsi
) || !tsr
->is_host(tsr
, hsr
)))
571 { /* change TS in case of a NAT in transport mode */
572 DBG2(DBG_IKE
, "changing received traffic selectors %R=== %R due to NAT",
574 tsi
->set_address(tsi
, hsi
);
575 tsr
->set_address(tsr
, hsr
);
580 /* check if peer selection valid */
581 if (!tsr
->is_contained_in(tsr
, this->tsr
) ||
582 !tsi
->is_contained_in(tsi
, this->tsi
))
584 DBG1(DBG_IKE
, "peer selected invalid traffic selectors: ",
585 "%R for %R, %R for %R", tsi
, this->tsi
, tsr
, this->tsr
);
590 this->tsi
->destroy(this->tsi
);
591 this->tsr
->destroy(this->tsr
);
604 * Add NAT-OA payloads
606 static void add_nat_oa_payloads(private_quick_mode_t
*this, message_t
*message
)
608 identification_t
*id
;
609 id_payload_t
*nat_oa
;
612 src
= message
->get_source(message
);
613 dst
= message
->get_destination(message
);
615 src
= this->initiator ? src
: dst
;
616 dst
= this->initiator ? dst
: src
;
618 /* first NAT-OA is the initiator's address */
619 id
= identification_create_from_sockaddr(src
->get_sockaddr(src
));
620 nat_oa
= id_payload_create_from_identification(NAT_OA_V1
, id
);
621 message
->add_payload(message
, (payload_t
*)nat_oa
);
624 /* second NAT-OA is that of the responder */
625 id
= identification_create_from_sockaddr(dst
->get_sockaddr(dst
));
626 nat_oa
= id_payload_create_from_identification(NAT_OA_V1
, id
);
627 message
->add_payload(message
, (payload_t
*)nat_oa
);
634 static void get_lifetimes(private_quick_mode_t
*this)
638 lft
= this->config
->get_lifetime(this->config
);
641 this->lifetime
= lft
->time
.life
;
643 else if (lft
->bytes
.life
)
645 this->lifebytes
= lft
->bytes
.life
;
651 * Check and apply lifetimes
653 static void apply_lifetimes(private_quick_mode_t
*this, sa_payload_t
*sa_payload
)
658 lifetime
= sa_payload
->get_lifetime(sa_payload
);
659 lifebytes
= sa_payload
->get_lifebytes(sa_payload
);
660 if (this->lifetime
!= lifetime
)
662 DBG1(DBG_IKE
, "received %us lifetime, configured %us",
663 lifetime
, this->lifetime
);
664 this->lifetime
= lifetime
;
666 if (this->lifebytes
!= lifebytes
)
668 DBG1(DBG_IKE
, "received %llu lifebytes, configured %llu",
669 lifebytes
, this->lifebytes
);
670 this->lifebytes
= lifebytes
;
675 * Set the task ready to build notify error message
677 static status_t
send_notify(private_quick_mode_t
*this, notify_type_t type
)
679 notify_payload_t
*notify
;
681 notify
= notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
683 notify
->set_spi(notify
, this->spi_i
);
685 this->ike_sa
->queue_task(this->ike_sa
,
686 (task_t
*)informational_create(this->ike_sa
, notify
));
687 /* cancel all active/passive tasks in favour of informational */
688 this->ike_sa
->flush_queue(this->ike_sa
,
689 this->initiator ? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
693 METHOD(task_t
, build_i
, status_t
,
694 private_quick_mode_t
*this, message_t
*message
)
700 enumerator_t
*enumerator
;
701 sa_payload_t
*sa_payload
;
702 linked_list_t
*list
, *tsi
, *tsr
;
703 proposal_t
*proposal
;
704 diffie_hellman_group_t group
;
706 this->udp
= this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
);
707 this->mode
= this->config
->get_mode(this->config
);
708 this->child_sa
= child_sa_create(
709 this->ike_sa
->get_my_host(this->ike_sa
),
710 this->ike_sa
->get_other_host(this->ike_sa
),
711 this->config
, this->reqid
, this->udp
);
713 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
715 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
716 add_nat_oa_payloads(this, message
);
719 if (this->config
->use_ipcomp(this->config
))
723 DBG1(DBG_IKE
, "IPComp is not supported if either peer is "
724 "natted, IPComp disabled");
728 this->cpi_i
= this->child_sa
->alloc_cpi(this->child_sa
);
731 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, "
737 this->spi_i
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
740 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
744 list
= this->config
->get_proposals(this->config
, FALSE
);
745 enumerator
= list
->create_enumerator(list
);
746 while (enumerator
->enumerate(enumerator
, &proposal
))
748 proposal
->set_spi(proposal
, this->spi_i
);
750 enumerator
->destroy(enumerator
);
753 sa_payload
= sa_payload_create_from_proposals_v1(list
,
754 this->lifetime
, this->lifebytes
, AUTH_NONE
,
755 this->mode
, this->udp
, this->cpi_i
);
756 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
757 message
->add_payload(message
, &sa_payload
->payload_interface
);
759 if (!add_nonce(this, &this->nonce_i
, message
))
764 group
= this->config
->get_dh_group(this->config
);
765 if (group
!= MODP_NONE
)
767 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
771 DBG1(DBG_IKE
, "configured DH group %N not supported",
772 diffie_hellman_group_names
, group
);
775 add_ke(this, message
);
779 this->tsi
= select_ts(this, TRUE
, NULL
);
783 this->tsr
= select_ts(this, FALSE
, NULL
);
785 tsi
= linked_list_create();
786 tsr
= linked_list_create();
787 tsi
->insert_last(tsi
, this->tsi
);
788 tsr
->insert_last(tsr
, this->tsr
);
789 this->tsi
= this->tsr
= NULL
;
790 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
791 NARROW_INITIATOR_PRE_AUTH
, tsi
, tsr
);
792 tsi
->remove_first(tsi
, (void**)&this->tsi
);
793 tsr
->remove_first(tsr
, (void**)&this->tsr
);
794 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
795 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
796 if (!this->tsi
|| !this->tsr
)
800 add_ts(this, message
);
813 * Check for notify errors, return TRUE if error found
815 static bool has_notify_errors(private_quick_mode_t
*this, message_t
*message
)
817 enumerator_t
*enumerator
;
821 enumerator
= message
->create_payload_enumerator(message
);
822 while (enumerator
->enumerate(enumerator
, &payload
))
824 if (payload
->get_type(payload
) == NOTIFY_V1
)
826 notify_payload_t
*notify
;
829 notify
= (notify_payload_t
*)payload
;
830 type
= notify
->get_notify_type(notify
);
834 DBG1(DBG_IKE
, "received %N error notify",
835 notify_type_names
, type
);
840 DBG1(DBG_IKE
, "received %N notify", notify_type_names
, type
);
844 enumerator
->destroy(enumerator
);
850 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
852 static void check_for_rekeyed_child(private_quick_mode_t
*this)
854 enumerator_t
*enumerator
, *policies
;
855 traffic_selector_t
*local
, *remote
;
856 child_sa_t
*child_sa
;
858 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
859 while (this->reqid
== 0 && enumerator
->enumerate(enumerator
, &child_sa
))
861 if (child_sa
->get_state(child_sa
) == CHILD_INSTALLED
&&
862 streq(child_sa
->get_name(child_sa
),
863 this->config
->get_name(this->config
)))
865 policies
= child_sa
->create_policy_enumerator(child_sa
);
866 if (policies
->enumerate(policies
, &local
, &remote
))
868 if (local
->equals(local
, this->tsr
) &&
869 remote
->equals(remote
, this->tsi
) &&
870 this->proposal
->equals(this->proposal
,
871 child_sa
->get_proposal(child_sa
)))
873 this->reqid
= child_sa
->get_reqid(child_sa
);
874 this->rekey
= child_sa
->get_spi(child_sa
, TRUE
);
875 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
876 DBG1(DBG_IKE
, "detected rekeying of CHILD_SA %s{%u}",
877 child_sa
->get_name(child_sa
), this->reqid
);
880 policies
->destroy(policies
);
883 enumerator
->destroy(enumerator
);
886 METHOD(task_t
, process_r
, status_t
,
887 private_quick_mode_t
*this, message_t
*message
)
893 sa_payload_t
*sa_payload
;
894 linked_list_t
*tsi
, *tsr
, *list
= NULL
;
895 peer_cfg_t
*peer_cfg
;
899 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
900 SECURITY_ASSOCIATION_V1
);
903 DBG1(DBG_IKE
, "sa payload missing");
904 return send_notify(this, INVALID_PAYLOAD_TYPE
);
907 this->mode
= sa_payload
->get_encap_mode(sa_payload
, &this->udp
);
909 if (!get_ts(this, message
))
913 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
914 tsi
= linked_list_create();
915 tsr
= linked_list_create();
916 tsi
->insert_last(tsi
, this->tsi
);
917 tsr
->insert_last(tsr
, this->tsr
);
918 this->tsi
= this->tsr
= NULL
;
919 this->config
= peer_cfg
->select_child_cfg(peer_cfg
, tsr
, tsi
,
920 get_dynamic_host(this->ike_sa
, TRUE
),
921 get_dynamic_host(this->ike_sa
, FALSE
));
924 this->tsi
= select_ts(this, FALSE
, tsi
);
925 this->tsr
= select_ts(this, TRUE
, tsr
);
927 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
928 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
929 if (!this->config
|| !this->tsi
|| !this->tsr
)
931 DBG1(DBG_IKE
, "no matching CHILD_SA config found");
932 return send_notify(this, INVALID_ID_INFORMATION
);
935 if (this->config
->use_ipcomp(this->config
))
937 if (this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
))
939 DBG1(DBG_IKE
, "IPComp is not supported if either peer is "
940 "natted, IPComp disabled");
944 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
946 if (!list
->get_count(list
))
948 DBG1(DBG_IKE
, "expected IPComp proposal but peer did "
949 "not send one, IPComp disabled");
954 if (!list
|| !list
->get_count(list
))
957 list
= sa_payload
->get_proposals(sa_payload
);
959 private = this->ike_sa
->supports_extension(this->ike_sa
,
961 this->proposal
= this->config
->select_proposal(this->config
,
962 list
, FALSE
, private);
963 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
966 apply_lifetimes(this, sa_payload
);
970 DBG1(DBG_IKE
, "no matching proposal found, sending %N",
971 notify_type_names
, NO_PROPOSAL_CHOSEN
);
972 return send_notify(this, NO_PROPOSAL_CHOSEN
);
974 this->spi_i
= this->proposal
->get_spi(this->proposal
);
976 if (!get_nonce(this, &this->nonce_i
, message
))
978 return send_notify(this, INVALID_PAYLOAD_TYPE
);
981 if (this->proposal
->get_algorithm(this->proposal
,
982 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
984 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
988 DBG1(DBG_IKE
, "negotiated DH group %N not supported",
989 diffie_hellman_group_names
, group
);
990 return send_notify(this, INVALID_KEY_INFORMATION
);
992 if (!get_ke(this, message
))
994 return send_notify(this, INVALID_PAYLOAD_TYPE
);
998 check_for_rekeyed_child(this);
1000 this->child_sa
= child_sa_create(
1001 this->ike_sa
->get_my_host(this->ike_sa
),
1002 this->ike_sa
->get_other_host(this->ike_sa
),
1003 this->config
, this->reqid
, this->udp
);
1005 tsi
= linked_list_create();
1006 tsr
= linked_list_create();
1007 tsi
->insert_last(tsi
, this->tsi
);
1008 tsr
->insert_last(tsr
, this->tsr
);
1009 this->tsi
= this->tsr
= NULL
;
1010 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
1011 NARROW_RESPONDER
, tsr
, tsi
);
1012 if (tsi
->remove_first(tsi
, (void**)&this->tsi
) != SUCCESS
||
1013 tsr
->remove_first(tsr
, (void**)&this->tsr
) != SUCCESS
)
1015 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1016 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1017 return send_notify(this, INVALID_ID_INFORMATION
);
1019 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
1020 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
1026 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
1027 has_notify_errors(this, message
))
1033 ike_sa_t
*ike_sa
= this->ike_sa
;
1036 task
= (task_t
*)quick_delete_create(this->ike_sa
,
1037 this->proposal
->get_protocol(this->proposal
),
1038 this->spi_i
, TRUE
, TRUE
);
1039 /* flush_queue() destroys the current task */
1040 ike_sa
->flush_queue(ike_sa
, TASK_QUEUE_PASSIVE
);
1041 ike_sa
->queue_task(ike_sa
, task
);
1042 return ALREADY_DONE
;
1051 METHOD(task_t
, build_r
, status_t
,
1052 private_quick_mode_t
*this, message_t
*message
)
1054 switch (this->state
)
1058 sa_payload_t
*sa_payload
;
1060 this->spi_r
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
1063 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
1064 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1066 this->proposal
->set_spi(this->proposal
, this->spi_r
);
1070 this->cpi_r
= this->child_sa
->alloc_cpi(this->child_sa
);
1073 DBG1(DBG_IKE
, "unable to allocate a CPI from "
1074 "kernel, IPComp disabled");
1075 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1079 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
1081 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1082 add_nat_oa_payloads(this, message
);
1085 sa_payload
= sa_payload_create_from_proposal_v1(this->proposal
,
1086 this->lifetime
, this->lifebytes
, AUTH_NONE
,
1087 this->mode
, this->udp
, this->cpi_r
);
1088 message
->add_payload(message
, &sa_payload
->payload_interface
);
1090 if (!add_nonce(this, &this->nonce_r
, message
))
1096 add_ke(this, message
);
1099 add_ts(this, message
);
1101 this->state
= QM_NEGOTIATED
;
1109 METHOD(task_t
, process_i
, status_t
,
1110 private_quick_mode_t
*this, message_t
*message
)
1112 switch (this->state
)
1116 sa_payload_t
*sa_payload
;
1117 linked_list_t
*list
= NULL
;
1120 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1121 SECURITY_ASSOCIATION_V1
);
1124 DBG1(DBG_IKE
, "sa payload missing");
1125 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1129 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1131 if (!list
->get_count(list
))
1133 DBG1(DBG_IKE
, "peer did not acccept our IPComp proposal, "
1138 if (!list
|| !list
->get_count(list
))
1141 list
= sa_payload
->get_proposals(sa_payload
);
1143 private = this->ike_sa
->supports_extension(this->ike_sa
,
1145 this->proposal
= this->config
->select_proposal(this->config
,
1146 list
, FALSE
, private);
1147 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1148 if (!this->proposal
)
1150 DBG1(DBG_IKE
, "no matching proposal found");
1151 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1153 this->spi_r
= this->proposal
->get_spi(this->proposal
);
1155 apply_lifetimes(this, sa_payload
);
1157 if (!get_nonce(this, &this->nonce_r
, message
))
1159 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1161 if (this->dh
&& !get_ke(this, message
))
1163 return send_notify(this, INVALID_KEY_INFORMATION
);
1165 if (!get_ts(this, message
))
1167 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1171 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1173 this->state
= QM_NEGOTIATED
;
1181 METHOD(task_t
, get_type
, task_type_t
,
1182 private_quick_mode_t
*this)
1184 return TASK_QUICK_MODE
;
1187 METHOD(quick_mode_t
, use_reqid
, void,
1188 private_quick_mode_t
*this, u_int32_t reqid
)
1190 this->reqid
= reqid
;
1193 METHOD(quick_mode_t
, rekey
, void,
1194 private_quick_mode_t
*this, u_int32_t spi
)
1199 METHOD(task_t
, migrate
, void,
1200 private_quick_mode_t
*this, ike_sa_t
*ike_sa
)
1202 chunk_free(&this->nonce_i
);
1203 chunk_free(&this->nonce_r
);
1204 DESTROY_IF(this->tsi
);
1205 DESTROY_IF(this->tsr
);
1206 DESTROY_IF(this->proposal
);
1207 DESTROY_IF(this->child_sa
);
1208 DESTROY_IF(this->dh
);
1210 this->ike_sa
= ike_sa
;
1211 this->keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
);
1212 this->state
= QM_INIT
;
1215 this->proposal
= NULL
;
1216 this->child_sa
= NULL
;
1221 if (!this->initiator
)
1223 DESTROY_IF(this->config
);
1224 this->config
= NULL
;
1228 METHOD(task_t
, destroy
, void,
1229 private_quick_mode_t
*this)
1231 chunk_free(&this->nonce_i
);
1232 chunk_free(&this->nonce_r
);
1233 DESTROY_IF(this->tsi
);
1234 DESTROY_IF(this->tsr
);
1235 DESTROY_IF(this->proposal
);
1236 DESTROY_IF(this->child_sa
);
1237 DESTROY_IF(this->config
);
1238 DESTROY_IF(this->dh
);
1243 * Described in header.
1245 quick_mode_t
*quick_mode_create(ike_sa_t
*ike_sa
, child_cfg_t
*config
,
1246 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1248 private_quick_mode_t
*this;
1253 .get_type
= _get_type
,
1254 .migrate
= _migrate
,
1255 .destroy
= _destroy
,
1257 .use_reqid
= _use_reqid
,
1261 .initiator
= config
!= NULL
,
1263 .keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
),
1265 .tsi
= tsi ? tsi
->clone(tsi
) : NULL
,
1266 .tsr
= tsr ? tsr
->clone(tsr
) : NULL
,
1271 this->public.task
.build
= _build_i
;
1272 this->public.task
.process
= _process_i
;
1276 this->public.task
.build
= _build_r
;
1277 this->public.task
.process
= _process_r
;
1280 return &this->public;