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 * Install negotiated CHILD_SA
180 static bool install(private_quick_mode_t
*this)
182 status_t status
, status_i
, status_o
;
183 chunk_t encr_i
, encr_r
, integ_i
, integ_r
;
184 linked_list_t
*tsi
, *tsr
;
185 child_sa_t
*old
= NULL
;
187 this->child_sa
->set_proposal(this->child_sa
, this->proposal
);
188 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLING
);
189 this->child_sa
->set_mode(this->child_sa
, this->mode
);
191 if (this->cpi_i
&& this->cpi_r
)
192 { /* DEFLATE is the only transform we currently support */
193 this->child_sa
->set_ipcomp(this->child_sa
, IPCOMP_DEFLATE
);
197 this->cpi_i
= this->cpi_r
= 0;
200 this->child_sa
->set_protocol(this->child_sa
,
201 this->proposal
->get_protocol(this->proposal
));
203 status_i
= status_o
= FAILED
;
204 encr_i
= encr_r
= integ_i
= integ_r
= chunk_empty
;
205 tsi
= linked_list_create();
206 tsr
= linked_list_create();
207 tsi
->insert_last(tsi
, this->tsi
->clone(this->tsi
));
208 tsr
->insert_last(tsr
, this->tsr
->clone(this->tsr
));
211 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
212 NARROW_INITIATOR_POST_AUTH
, tsi
, tsr
);
216 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
217 NARROW_RESPONDER
, tsr
, tsi
);
219 if (tsi
->get_count(tsi
) == 0 || tsr
->get_count(tsr
) == 0)
221 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
222 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
223 DBG1(DBG_IKE
, "no acceptable traffic selectors found");
227 if (this->keymat
->derive_child_keys(this->keymat
, this->proposal
, this->dh
,
228 this->spi_i
, this->spi_r
, this->nonce_i
, this->nonce_r
,
229 &encr_i
, &integ_i
, &encr_r
, &integ_r
))
233 status_i
= this->child_sa
->install(this->child_sa
, encr_r
, integ_r
,
234 this->spi_i
, this->cpi_i
, TRUE
, FALSE
, tsi
, tsr
);
235 status_o
= this->child_sa
->install(this->child_sa
, encr_i
, integ_i
,
236 this->spi_r
, this->cpi_r
, FALSE
, FALSE
, tsi
, tsr
);
240 status_i
= this->child_sa
->install(this->child_sa
, encr_i
, integ_i
,
241 this->spi_r
, this->cpi_r
, TRUE
, FALSE
, tsr
, tsi
);
242 status_o
= this->child_sa
->install(this->child_sa
, encr_r
, integ_r
,
243 this->spi_i
, this->cpi_i
, FALSE
, FALSE
, tsr
, tsi
);
246 chunk_clear(&integ_i
);
247 chunk_clear(&integ_r
);
248 chunk_clear(&encr_i
);
249 chunk_clear(&encr_r
);
251 if (status_i
!= SUCCESS
|| status_o
!= SUCCESS
)
253 DBG1(DBG_IKE
, "unable to install %s%s%sIPsec SA (SAD) in kernel",
254 (status_i
!= SUCCESS
) ?
"inbound " : "",
255 (status_i
!= SUCCESS
&& status_o
!= SUCCESS
) ?
"and ": "",
256 (status_o
!= SUCCESS
) ?
"outbound " : "");
257 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
258 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
264 status
= this->child_sa
->add_policies(this->child_sa
, tsi
, tsr
);
268 status
= this->child_sa
->add_policies(this->child_sa
, tsr
, tsi
);
270 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
271 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
272 if (status
!= SUCCESS
)
274 DBG1(DBG_IKE
, "unable to install IPsec policies (SPD) in kernel");
278 charon
->bus
->child_keys(charon
->bus
, this->child_sa
, this->initiator
,
279 this->dh
, this->nonce_i
, this->nonce_r
);
281 /* add to IKE_SA, and remove from task */
282 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
283 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
285 DBG0(DBG_IKE
, "CHILD_SA %s{%d} established "
286 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
287 this->child_sa
->get_name(this->child_sa
),
288 this->child_sa
->get_reqid(this->child_sa
),
289 ntohl(this->child_sa
->get_spi(this->child_sa
, TRUE
)),
290 ntohl(this->child_sa
->get_spi(this->child_sa
, FALSE
)),
291 this->child_sa
->get_traffic_selectors(this->child_sa
, TRUE
),
292 this->child_sa
->get_traffic_selectors(this->child_sa
, FALSE
));
296 old
= this->ike_sa
->get_child_sa(this->ike_sa
,
297 this->proposal
->get_protocol(this->proposal
),
302 charon
->bus
->child_rekey(charon
->bus
, old
, this->child_sa
);
306 charon
->bus
->child_updown(charon
->bus
, this->child_sa
, TRUE
);
310 schedule_inactivity_timeout(this);
312 this->child_sa
= NULL
;
317 * Generate and add NONCE
319 static bool add_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
322 nonce_payload_t
*nonce_payload
;
325 nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
328 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
331 if (!nonceg
->allocate_nonce(nonceg
, NONCE_SIZE
, nonce
))
333 DBG1(DBG_IKE
, "nonce allocation failed");
334 nonceg
->destroy(nonceg
);
337 nonceg
->destroy(nonceg
);
339 nonce_payload
= nonce_payload_create(NONCE_V1
);
340 nonce_payload
->set_nonce(nonce_payload
, *nonce
);
341 message
->add_payload(message
, &nonce_payload
->payload_interface
);
347 * Extract nonce from NONCE payload
349 static bool get_nonce(private_quick_mode_t
*this, chunk_t
*nonce
,
352 nonce_payload_t
*nonce_payload
;
354 nonce_payload
= (nonce_payload_t
*)message
->get_payload(message
, NONCE_V1
);
357 DBG1(DBG_IKE
, "NONCE payload missing in message");
360 *nonce
= nonce_payload
->get_nonce(nonce_payload
);
366 * Add KE payload to message
368 static void add_ke(private_quick_mode_t
*this, message_t
*message
)
370 ke_payload_t
*ke_payload
;
372 ke_payload
= ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1
, this->dh
);
373 message
->add_payload(message
, &ke_payload
->payload_interface
);
377 * Get DH value from a KE payload
379 static bool get_ke(private_quick_mode_t
*this, message_t
*message
)
381 ke_payload_t
*ke_payload
;
383 ke_payload
= (ke_payload_t
*)message
->get_payload(message
, KEY_EXCHANGE_V1
);
386 DBG1(DBG_IKE
, "KE payload missing");
389 this->dh
->set_other_public_value(this->dh
,
390 ke_payload
->get_key_exchange_data(ke_payload
));
395 * Select a traffic selector from configuration
397 static traffic_selector_t
* select_ts(private_quick_mode_t
*this, bool local
,
398 linked_list_t
*supplied
)
400 traffic_selector_t
*ts
;
401 enumerator_t
*enumerator
;
405 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
, local
);
406 if (!enumerator
->enumerate(enumerator
, &host
))
410 host
= this->ike_sa
->get_my_host(this->ike_sa
);
414 host
= this->ike_sa
->get_other_host(this->ike_sa
);
417 enumerator
->destroy(enumerator
);
418 list
= this->config
->get_traffic_selectors(this->config
, local
,
420 if (list
->get_first(list
, (void**)&ts
) == SUCCESS
)
422 if (list
->get_count(list
) > 1)
424 DBG1(DBG_IKE
, "configuration has more than one %s traffic selector,"
425 " using first only", local ?
"local" : "remote");
431 DBG1(DBG_IKE
, "%s traffic selector missing in configuration",
432 local ?
"local" : "local");
435 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
440 * Add selected traffic selectors to message
442 static void add_ts(private_quick_mode_t
*this, message_t
*message
)
444 id_payload_t
*id_payload
;
449 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
450 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
454 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
455 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
457 /* add ID payload only if negotiating non host2host tunnels */
458 if (!this->tsi
->is_host(this->tsi
, hsi
) ||
459 !this->tsr
->is_host(this->tsr
, hsr
) ||
460 this->tsi
->get_protocol(this->tsi
) ||
461 this->tsr
->get_protocol(this->tsr
) ||
462 this->tsi
->get_from_port(this->tsi
) ||
463 this->tsr
->get_from_port(this->tsr
) ||
464 this->tsi
->get_to_port(this->tsi
) != 65535 ||
465 this->tsr
->get_to_port(this->tsr
) != 65535)
467 id_payload
= id_payload_create_from_ts(this->tsi
);
468 message
->add_payload(message
, &id_payload
->payload_interface
);
469 id_payload
= id_payload_create_from_ts(this->tsr
);
470 message
->add_payload(message
, &id_payload
->payload_interface
);
475 * Get traffic selectors from received message
477 static bool get_ts(private_quick_mode_t
*this, message_t
*message
)
479 traffic_selector_t
*tsi
= NULL
, *tsr
= NULL
;
480 enumerator_t
*enumerator
;
481 id_payload_t
*id_payload
;
486 enumerator
= message
->create_payload_enumerator(message
);
487 while (enumerator
->enumerate(enumerator
, &payload
))
489 if (payload
->get_type(payload
) == ID_V1
)
491 id_payload
= (id_payload_t
*)payload
;
495 tsi
= id_payload
->get_ts(id_payload
);
500 tsr
= id_payload
->get_ts(id_payload
);
505 enumerator
->destroy(enumerator
);
507 /* create host2host selectors if ID payloads missing */
510 hsi
= this->ike_sa
->get_my_host(this->ike_sa
);
511 hsr
= this->ike_sa
->get_other_host(this->ike_sa
);
515 hsr
= this->ike_sa
->get_my_host(this->ike_sa
);
516 hsi
= this->ike_sa
->get_other_host(this->ike_sa
);
520 tsi
= traffic_selector_create_from_subnet(hsi
->clone(hsi
),
521 hsi
->get_family(hsi
) == AF_INET ?
32 : 128, 0, 0);
525 tsr
= traffic_selector_create_from_subnet(hsr
->clone(hsr
),
526 hsr
->get_family(hsr
) == AF_INET ?
32 : 128, 0, 0);
530 /* check if peer selection valid */
531 if (!tsr
->is_contained_in(tsr
, this->tsr
) ||
532 !tsi
->is_contained_in(tsi
, this->tsi
))
534 DBG1(DBG_IKE
, "peer selected invalid traffic selectors: ",
535 "%R for %R, %R for %R", tsi
, this->tsi
, tsr
, this->tsr
);
540 this->tsi
->destroy(this->tsi
);
541 this->tsr
->destroy(this->tsr
);
554 * Add NAT-OA payloads
556 static void add_nat_oa_payloads(private_quick_mode_t
*this, message_t
*message
)
558 identification_t
*id
;
559 id_payload_t
*nat_oa
;
562 src
= message
->get_source(message
);
563 dst
= message
->get_destination(message
);
565 src
= this->initiator ? src
: dst
;
566 dst
= this->initiator ? dst
: src
;
568 /* first NAT-OA is the initiator's address */
569 id
= identification_create_from_sockaddr(src
->get_sockaddr(src
));
570 nat_oa
= id_payload_create_from_identification(NAT_OA_V1
, id
);
571 message
->add_payload(message
, (payload_t
*)nat_oa
);
574 /* second NAT-OA is that of the responder */
575 id
= identification_create_from_sockaddr(dst
->get_sockaddr(dst
));
576 nat_oa
= id_payload_create_from_identification(NAT_OA_V1
, id
);
577 message
->add_payload(message
, (payload_t
*)nat_oa
);
584 static void get_lifetimes(private_quick_mode_t
*this)
588 lft
= this->config
->get_lifetime(this->config
);
591 this->lifetime
= lft
->time
.life
;
593 else if (lft
->bytes
.life
)
595 this->lifebytes
= lft
->bytes
.life
;
601 * Check and apply lifetimes
603 static void apply_lifetimes(private_quick_mode_t
*this, sa_payload_t
*sa_payload
)
608 lifetime
= sa_payload
->get_lifetime(sa_payload
);
609 lifebytes
= sa_payload
->get_lifebytes(sa_payload
);
610 if (this->lifetime
!= lifetime
)
612 DBG1(DBG_IKE
, "received %us lifetime, configured %us",
613 lifetime
, this->lifetime
);
614 this->lifetime
= lifetime
;
616 if (this->lifebytes
!= lifebytes
)
618 DBG1(DBG_IKE
, "received %llu lifebytes, configured %llu",
619 lifebytes
, this->lifebytes
);
620 this->lifebytes
= lifebytes
;
625 * Set the task ready to build notify error message
627 static status_t
send_notify(private_quick_mode_t
*this, notify_type_t type
)
629 notify_payload_t
*notify
;
631 notify
= notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
633 notify
->set_spi(notify
, this->spi_i
);
635 this->ike_sa
->queue_task(this->ike_sa
,
636 (task_t
*)informational_create(this->ike_sa
, notify
));
637 /* cancel all active/passive tasks in favour of informational */
638 this->ike_sa
->flush_queue(this->ike_sa
,
639 this->initiator ? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
643 METHOD(task_t
, build_i
, status_t
,
644 private_quick_mode_t
*this, message_t
*message
)
650 enumerator_t
*enumerator
;
651 sa_payload_t
*sa_payload
;
652 linked_list_t
*list
, *tsi
, *tsr
;
653 proposal_t
*proposal
;
654 diffie_hellman_group_t group
;
656 this->udp
= this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
);
657 this->mode
= this->config
->get_mode(this->config
);
658 this->child_sa
= child_sa_create(
659 this->ike_sa
->get_my_host(this->ike_sa
),
660 this->ike_sa
->get_other_host(this->ike_sa
),
661 this->config
, this->reqid
, this->udp
);
663 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
665 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
666 add_nat_oa_payloads(this, message
);
669 if (this->config
->use_ipcomp(this->config
))
673 DBG1(DBG_IKE
, "IPComp is not supported if either peer is "
674 "natted, IPComp disabled");
678 this->cpi_i
= this->child_sa
->alloc_cpi(this->child_sa
);
681 DBG1(DBG_IKE
, "unable to allocate a CPI from kernel, "
687 this->spi_i
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
690 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
694 list
= this->config
->get_proposals(this->config
, FALSE
);
695 enumerator
= list
->create_enumerator(list
);
696 while (enumerator
->enumerate(enumerator
, &proposal
))
698 proposal
->set_spi(proposal
, this->spi_i
);
700 enumerator
->destroy(enumerator
);
703 sa_payload
= sa_payload_create_from_proposals_v1(list
,
704 this->lifetime
, this->lifebytes
, AUTH_NONE
,
705 this->mode
, this->udp
, this->cpi_i
);
706 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
707 message
->add_payload(message
, &sa_payload
->payload_interface
);
709 if (!add_nonce(this, &this->nonce_i
, message
))
714 group
= this->config
->get_dh_group(this->config
);
715 if (group
!= MODP_NONE
)
717 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
721 DBG1(DBG_IKE
, "configured DH group %N not supported",
722 diffie_hellman_group_names
, group
);
725 add_ke(this, message
);
729 this->tsi
= select_ts(this, TRUE
, NULL
);
733 this->tsr
= select_ts(this, FALSE
, NULL
);
735 tsi
= linked_list_create();
736 tsr
= linked_list_create();
737 tsi
->insert_last(tsi
, this->tsi
);
738 tsr
->insert_last(tsr
, this->tsr
);
739 this->tsi
= this->tsr
= NULL
;
740 charon
->bus
->narrow(charon
->bus
, this->child_sa
,
741 NARROW_INITIATOR_PRE_AUTH
, tsi
, tsr
);
742 tsi
->remove_first(tsi
, (void**)&this->tsi
);
743 tsr
->remove_first(tsr
, (void**)&this->tsr
);
744 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
745 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
746 if (!this->tsi
|| !this->tsr
)
750 add_ts(this, message
);
763 * Check for notify errors, return TRUE if error found
765 static bool has_notify_errors(private_quick_mode_t
*this, message_t
*message
)
767 enumerator_t
*enumerator
;
771 enumerator
= message
->create_payload_enumerator(message
);
772 while (enumerator
->enumerate(enumerator
, &payload
))
774 if (payload
->get_type(payload
) == NOTIFY_V1
)
776 notify_payload_t
*notify
;
779 notify
= (notify_payload_t
*)payload
;
780 type
= notify
->get_notify_type(notify
);
784 DBG1(DBG_IKE
, "received %N error notify",
785 notify_type_names
, type
);
790 DBG1(DBG_IKE
, "received %N notify", notify_type_names
, type
);
794 enumerator
->destroy(enumerator
);
800 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
802 static void check_for_rekeyed_child(private_quick_mode_t
*this)
804 enumerator_t
*enumerator
, *policies
;
805 traffic_selector_t
*local
, *remote
;
806 child_sa_t
*child_sa
;
808 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
809 while (this->reqid
== 0 && enumerator
->enumerate(enumerator
, &child_sa
))
811 if (child_sa
->get_state(child_sa
) == CHILD_INSTALLED
&&
812 streq(child_sa
->get_name(child_sa
),
813 this->config
->get_name(this->config
)))
815 policies
= child_sa
->create_policy_enumerator(child_sa
);
816 if (policies
->enumerate(policies
, &local
, &remote
))
818 if (local
->equals(local
, this->tsr
) &&
819 remote
->equals(remote
, this->tsi
) &&
820 this->proposal
->equals(this->proposal
,
821 child_sa
->get_proposal(child_sa
)))
823 this->reqid
= child_sa
->get_reqid(child_sa
);
824 this->rekey
= child_sa
->get_spi(child_sa
, TRUE
);
825 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
826 DBG1(DBG_IKE
, "detected rekeying of CHILD_SA %s{%u}",
827 child_sa
->get_name(child_sa
), this->reqid
);
830 policies
->destroy(policies
);
833 enumerator
->destroy(enumerator
);
837 * Get host to use for dynamic traffic selectors
839 static host_t
*get_dynamic_host(ike_sa_t
*ike_sa
, bool local
)
841 enumerator_t
*enumerator
;
844 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
845 if (!enumerator
->enumerate(enumerator
, &host
))
849 host
= ike_sa
->get_my_host(ike_sa
);
853 host
= ike_sa
->get_other_host(ike_sa
);
856 enumerator
->destroy(enumerator
);
860 METHOD(task_t
, process_r
, status_t
,
861 private_quick_mode_t
*this, message_t
*message
)
867 sa_payload_t
*sa_payload
;
868 linked_list_t
*tsi
, *tsr
, *list
= NULL
;
869 peer_cfg_t
*peer_cfg
;
873 if (!get_ts(this, message
))
877 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
878 tsi
= linked_list_create();
879 tsr
= linked_list_create();
880 tsi
->insert_last(tsi
, this->tsi
);
881 tsr
->insert_last(tsr
, this->tsr
);
882 this->tsi
= this->tsr
= NULL
;
883 this->config
= peer_cfg
->select_child_cfg(peer_cfg
, tsr
, tsi
,
884 get_dynamic_host(this->ike_sa
, TRUE
),
885 get_dynamic_host(this->ike_sa
, FALSE
));
888 this->tsi
= select_ts(this, FALSE
, tsi
);
889 this->tsr
= select_ts(this, TRUE
, tsr
);
891 tsi
->destroy_offset(tsi
, offsetof(traffic_selector_t
, destroy
));
892 tsr
->destroy_offset(tsr
, offsetof(traffic_selector_t
, destroy
));
893 if (!this->config
|| !this->tsi
|| !this->tsr
)
895 DBG1(DBG_IKE
, "no matching CHILD_SA config found");
896 return send_notify(this, INVALID_ID_INFORMATION
);
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
);
906 if (this->config
->use_ipcomp(this->config
))
908 if (this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
))
910 DBG1(DBG_IKE
, "IPComp is not supported if either peer is "
911 "natted, IPComp disabled");
915 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
917 if (!list
->get_count(list
))
919 DBG1(DBG_IKE
, "expected IPComp proposal but peer did "
920 "not send one, IPComp disabled");
925 if (!list
|| !list
->get_count(list
))
928 list
= sa_payload
->get_proposals(sa_payload
);
930 private = this->ike_sa
->supports_extension(this->ike_sa
,
932 this->proposal
= this->config
->select_proposal(this->config
,
933 list
, FALSE
, private);
934 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
936 this->mode
= sa_payload
->get_encap_mode(sa_payload
, &this->udp
);
939 apply_lifetimes(this, sa_payload
);
943 DBG1(DBG_IKE
, "no matching proposal found, sending %N",
944 notify_type_names
, NO_PROPOSAL_CHOSEN
);
945 return send_notify(this, NO_PROPOSAL_CHOSEN
);
947 this->spi_i
= this->proposal
->get_spi(this->proposal
);
949 if (!get_nonce(this, &this->nonce_i
, message
))
951 return send_notify(this, INVALID_PAYLOAD_TYPE
);
954 if (this->proposal
->get_algorithm(this->proposal
,
955 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
957 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
961 DBG1(DBG_IKE
, "negotiated DH group %N not supported",
962 diffie_hellman_group_names
, group
);
963 return send_notify(this, INVALID_KEY_INFORMATION
);
965 if (!get_ke(this, message
))
967 return send_notify(this, INVALID_PAYLOAD_TYPE
);
971 check_for_rekeyed_child(this);
973 this->child_sa
= child_sa_create(
974 this->ike_sa
->get_my_host(this->ike_sa
),
975 this->ike_sa
->get_other_host(this->ike_sa
),
976 this->config
, this->reqid
, this->udp
);
981 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
982 has_notify_errors(this, message
))
988 ike_sa_t
*ike_sa
= this->ike_sa
;
991 task
= (task_t
*)quick_delete_create(this->ike_sa
,
992 this->proposal
->get_protocol(this->proposal
),
993 this->spi_i
, TRUE
, TRUE
);
994 /* flush_queue() destroys the current task */
995 ike_sa
->flush_queue(ike_sa
, TASK_QUEUE_PASSIVE
);
996 ike_sa
->queue_task(ike_sa
, task
);
1006 METHOD(task_t
, build_r
, status_t
,
1007 private_quick_mode_t
*this, message_t
*message
)
1009 switch (this->state
)
1013 sa_payload_t
*sa_payload
;
1015 this->spi_r
= this->child_sa
->alloc_spi(this->child_sa
, PROTO_ESP
);
1018 DBG1(DBG_IKE
, "allocating SPI from kernel failed");
1019 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1021 this->proposal
->set_spi(this->proposal
, this->spi_r
);
1025 this->cpi_r
= this->child_sa
->alloc_cpi(this->child_sa
);
1028 DBG1(DBG_IKE
, "unable to allocate a CPI from "
1029 "kernel, IPComp disabled");
1030 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1034 if (this->udp
&& this->mode
== MODE_TRANSPORT
)
1036 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1037 add_nat_oa_payloads(this, message
);
1040 sa_payload
= sa_payload_create_from_proposal_v1(this->proposal
,
1041 this->lifetime
, this->lifebytes
, AUTH_NONE
,
1042 this->mode
, this->udp
, this->cpi_r
);
1043 message
->add_payload(message
, &sa_payload
->payload_interface
);
1045 if (!add_nonce(this, &this->nonce_r
, message
))
1051 add_ke(this, message
);
1054 add_ts(this, message
);
1056 this->state
= QM_NEGOTIATED
;
1064 METHOD(task_t
, process_i
, status_t
,
1065 private_quick_mode_t
*this, message_t
*message
)
1067 switch (this->state
)
1071 sa_payload_t
*sa_payload
;
1072 linked_list_t
*list
= NULL
;
1075 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
1076 SECURITY_ASSOCIATION_V1
);
1079 DBG1(DBG_IKE
, "sa payload missing");
1080 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1084 list
= sa_payload
->get_ipcomp_proposals(sa_payload
,
1086 if (!list
->get_count(list
))
1088 DBG1(DBG_IKE
, "peer did not acccept our IPComp proposal, "
1093 if (!list
|| !list
->get_count(list
))
1096 list
= sa_payload
->get_proposals(sa_payload
);
1098 private = this->ike_sa
->supports_extension(this->ike_sa
,
1100 this->proposal
= this->config
->select_proposal(this->config
,
1101 list
, FALSE
, private);
1102 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
1103 if (!this->proposal
)
1105 DBG1(DBG_IKE
, "no matching proposal found");
1106 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1108 this->spi_r
= this->proposal
->get_spi(this->proposal
);
1110 apply_lifetimes(this, sa_payload
);
1112 if (!get_nonce(this, &this->nonce_r
, message
))
1114 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1116 if (this->dh
&& !get_ke(this, message
))
1118 return send_notify(this, INVALID_KEY_INFORMATION
);
1120 if (!get_ts(this, message
))
1122 return send_notify(this, INVALID_PAYLOAD_TYPE
);
1126 return send_notify(this, NO_PROPOSAL_CHOSEN
);
1128 this->state
= QM_NEGOTIATED
;
1136 METHOD(task_t
, get_type
, task_type_t
,
1137 private_quick_mode_t
*this)
1139 return TASK_QUICK_MODE
;
1142 METHOD(quick_mode_t
, use_reqid
, void,
1143 private_quick_mode_t
*this, u_int32_t reqid
)
1145 this->reqid
= reqid
;
1148 METHOD(quick_mode_t
, rekey
, void,
1149 private_quick_mode_t
*this, u_int32_t spi
)
1154 METHOD(task_t
, migrate
, void,
1155 private_quick_mode_t
*this, ike_sa_t
*ike_sa
)
1157 chunk_free(&this->nonce_i
);
1158 chunk_free(&this->nonce_r
);
1159 DESTROY_IF(this->tsi
);
1160 DESTROY_IF(this->tsr
);
1161 DESTROY_IF(this->proposal
);
1162 DESTROY_IF(this->child_sa
);
1163 DESTROY_IF(this->dh
);
1165 this->ike_sa
= ike_sa
;
1166 this->keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
);
1167 this->state
= QM_INIT
;
1170 this->proposal
= NULL
;
1171 this->child_sa
= NULL
;
1176 if (!this->initiator
)
1178 DESTROY_IF(this->config
);
1179 this->config
= NULL
;
1183 METHOD(task_t
, destroy
, void,
1184 private_quick_mode_t
*this)
1186 chunk_free(&this->nonce_i
);
1187 chunk_free(&this->nonce_r
);
1188 DESTROY_IF(this->tsi
);
1189 DESTROY_IF(this->tsr
);
1190 DESTROY_IF(this->proposal
);
1191 DESTROY_IF(this->child_sa
);
1192 DESTROY_IF(this->config
);
1193 DESTROY_IF(this->dh
);
1198 * Described in header.
1200 quick_mode_t
*quick_mode_create(ike_sa_t
*ike_sa
, child_cfg_t
*config
,
1201 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1203 private_quick_mode_t
*this;
1208 .get_type
= _get_type
,
1209 .migrate
= _migrate
,
1210 .destroy
= _destroy
,
1212 .use_reqid
= _use_reqid
,
1216 .initiator
= config
!= NULL
,
1218 .keymat
= (keymat_v1_t
*)ike_sa
->get_keymat(ike_sa
),
1220 .tsi
= tsi ? tsi
->clone(tsi
) : NULL
,
1221 .tsr
= tsr ? tsr
->clone(tsr
) : NULL
,
1226 this->public.task
.build
= _build_i
;
1227 this->public.task
.process
= _process_i
;
1231 this->public.task
.build
= _build_r
;
1232 this->public.task
.process
= _process_r
;
1235 return &this->public;