From 15a682f4c23d0b8340b31077698e6f6d924c2861 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 19 Dec 2011 13:10:29 +0100 Subject: Separated libcharon/sa directory with ikev1 and ikev2 subfolders --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 947 ++++++++++++++++++++++++++++++ 1 file changed, 947 insertions(+) create mode 100644 src/libcharon/sa/ikev1/tasks/quick_mode.c (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c new file mode 100644 index 000000000..9e71642af --- /dev/null +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -0,0 +1,947 @@ +/* + * Copyright (C) 2011 Martin Willi + * Copyright (C) 2011 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "quick_mode.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct private_quick_mode_t private_quick_mode_t; + +/** + * Private members of a quick_mode_t task. + */ +struct private_quick_mode_t { + + /** + * Public methods and task_t interface. + */ + quick_mode_t public; + + /** + * Assigned IKE_SA. + */ + ike_sa_t *ike_sa; + + /** + * TRUE if we are initiating quick mode + */ + bool initiator; + + /** + * Traffic selector of initiator + */ + traffic_selector_t *tsi; + + /** + * Traffic selector of responder + */ + traffic_selector_t *tsr; + + /** + * Initiators nonce + */ + chunk_t nonce_i; + + /** + * Responder nonce + */ + chunk_t nonce_r; + + /** + * Initiators ESP SPI + */ + u_int32_t spi_i; + + /** + * Responder ESP SPI + */ + u_int32_t spi_r; + + /** + * selected CHILD_SA proposal + */ + proposal_t *proposal; + + /** + * Config of CHILD_SA to establish + */ + child_cfg_t *config; + + /** + * CHILD_SA we are about to establish + */ + child_sa_t *child_sa; + + /** + * IKEv1 keymat + */ + keymat_v1_t *keymat; + + /** + * DH exchange, when PFS is in use + */ + diffie_hellman_t *dh; + + /** + * Negotiated lifetime of new SA + */ + u_int32_t lifetime; + + /** + * Negotaited lifebytes of new SA + */ + u_int64_t lifebytes; + + /** + * Notify type in case of error + */ + notify_type_t notify_type; + + /** states of quick mode */ + enum { + QM_INIT, + QM_NEGOTIATED, + } state; +}; + +/** + * Install negotiated CHILD_SA + */ +static bool install(private_quick_mode_t *this) +{ + status_t status, status_i, status_o; + chunk_t encr_i, encr_r, integ_i, integ_r; + linked_list_t *tsi, *tsr; + + this->child_sa->set_proposal(this->child_sa, this->proposal); + this->child_sa->set_state(this->child_sa, CHILD_INSTALLING); + this->child_sa->set_mode(this->child_sa, MODE_TUNNEL); + this->child_sa->set_protocol(this->child_sa, + this->proposal->get_protocol(this->proposal)); + + status_i = status_o = FAILED; + encr_i = encr_r = integ_i = integ_r = chunk_empty; + tsi = linked_list_create(); + tsr = linked_list_create(); + tsi->insert_last(tsi, this->tsi); + tsr->insert_last(tsr, this->tsr); + if (this->keymat->derive_child_keys(this->keymat, this->proposal, this->dh, + this->spi_i, this->spi_r, this->nonce_i, this->nonce_r, + &encr_i, &integ_i, &encr_r, &integ_r)) + { + if (this->initiator) + { + status_i = this->child_sa->install(this->child_sa, encr_r, integ_r, + this->spi_i, 0, TRUE, FALSE, tsi, tsr); + status_o = this->child_sa->install(this->child_sa, encr_i, integ_i, + this->spi_r, 0, FALSE, FALSE, tsi, tsr); + } + else + { + status_i = this->child_sa->install(this->child_sa, encr_i, integ_i, + this->spi_r, 0, TRUE, FALSE, tsr, tsi); + status_o = this->child_sa->install(this->child_sa, encr_r, integ_r, + this->spi_i, 0, FALSE, FALSE, tsr, tsi); + } + } + chunk_clear(&integ_i); + chunk_clear(&integ_r); + chunk_clear(&encr_i); + chunk_clear(&encr_r); + + if (status_i != SUCCESS || status_o != SUCCESS) + { + DBG1(DBG_IKE, "unable to install %s%s%sIPsec SA (SAD) in kernel", + (status_i != SUCCESS) ? "inbound " : "", + (status_i != SUCCESS && status_o != SUCCESS) ? "and ": "", + (status_o != SUCCESS) ? "outbound " : ""); + tsi->destroy(tsi); + tsr->destroy(tsr); + return FALSE; + } + + if (this->initiator) + { + status = this->child_sa->add_policies(this->child_sa, tsi, tsr); + } + else + { + status = this->child_sa->add_policies(this->child_sa, tsr, tsi); + } + tsi->destroy(tsi); + tsr->destroy(tsr); + if (status != SUCCESS) + { + DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel"); + return FALSE; + } + + charon->bus->child_keys(charon->bus, this->child_sa, this->initiator, + this->dh, this->nonce_i, this->nonce_r); + + /* add to IKE_SA, and remove from task */ + this->child_sa->set_state(this->child_sa, CHILD_INSTALLED); + this->ike_sa->add_child_sa(this->ike_sa, this->child_sa); + + DBG0(DBG_IKE, "CHILD_SA %s{%d} established " + "with SPIs %.8x_i %.8x_o and TS %#R=== %#R", + this->child_sa->get_name(this->child_sa), + this->child_sa->get_reqid(this->child_sa), + ntohl(this->child_sa->get_spi(this->child_sa, TRUE)), + ntohl(this->child_sa->get_spi(this->child_sa, FALSE)), + this->child_sa->get_traffic_selectors(this->child_sa, TRUE), + this->child_sa->get_traffic_selectors(this->child_sa, FALSE)); + + charon->bus->child_updown(charon->bus, this->child_sa, TRUE); + + this->child_sa = NULL; + + return TRUE; +} + +/** + * Generate and add NONCE + */ +static bool add_nonce(private_quick_mode_t *this, chunk_t *nonce, + message_t *message) +{ + nonce_payload_t *nonce_payload; + rng_t *rng; + + rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK); + if (!rng) + { + DBG1(DBG_IKE, "no RNG found to create nonce"); + return FALSE; + } + rng->allocate_bytes(rng, NONCE_SIZE, nonce); + rng->destroy(rng); + + nonce_payload = nonce_payload_create(NONCE_V1); + nonce_payload->set_nonce(nonce_payload, *nonce); + message->add_payload(message, &nonce_payload->payload_interface); + + return TRUE; +} + +/** + * Extract nonce from NONCE payload + */ +static bool get_nonce(private_quick_mode_t *this, chunk_t *nonce, + message_t *message) +{ + nonce_payload_t *nonce_payload; + + nonce_payload = (nonce_payload_t*)message->get_payload(message, NONCE_V1); + if (!nonce_payload) + { + DBG1(DBG_IKE, "NONCE payload missing in message"); + return FALSE; + } + *nonce = nonce_payload->get_nonce(nonce_payload); + + return TRUE; +} + +/** + * Add KE payload to message + */ +static void add_ke(private_quick_mode_t *this, message_t *message) +{ + ke_payload_t *ke_payload; + + ke_payload = ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1, this->dh); + message->add_payload(message, &ke_payload->payload_interface); +} + +/** + * Get DH value from a KE payload + */ +static bool get_ke(private_quick_mode_t *this, message_t *message) +{ + ke_payload_t *ke_payload; + + ke_payload = (ke_payload_t*)message->get_payload(message, KEY_EXCHANGE_V1); + if (!ke_payload) + { + DBG1(DBG_IKE, "KE payload missing"); + return FALSE; + } + this->dh->set_other_public_value(this->dh, + ke_payload->get_key_exchange_data(ke_payload)); + return TRUE; +} + +/** + * Select a traffic selector from configuration + */ +static traffic_selector_t* select_ts(private_quick_mode_t *this, bool local, + linked_list_t *supplied) +{ + traffic_selector_t *ts; + linked_list_t *list; + host_t *host; + + host = this->ike_sa->get_virtual_ip(this->ike_sa, local); + if (!host) + { + if (local) + { + host = this->ike_sa->get_my_host(this->ike_sa); + } + else + { + host = this->ike_sa->get_other_host(this->ike_sa); + } + } + list = this->config->get_traffic_selectors(this->config, local, + supplied, host); + if (list->get_first(list, (void**)&ts) == SUCCESS) + { + if (list->get_count(list) > 1) + { + DBG1(DBG_IKE, "configuration has more than one %s traffic selector," + " using first only", local ? "local" : "remote"); + } + ts = ts->clone(ts); + } + else + { + DBG1(DBG_IKE, "%s traffic selector missing in configuration", + local ? "local" : "local"); + ts = NULL; + } + list->destroy_offset(list, offsetof(traffic_selector_t, destroy)); + return ts; +} + +/** + * Add selected traffic selectors to message + */ +static void add_ts(private_quick_mode_t *this, message_t *message) +{ + id_payload_t *id_payload; + host_t *hsi, *hsr; + + if (this->initiator) + { + hsi = this->ike_sa->get_my_host(this->ike_sa); + hsr = this->ike_sa->get_other_host(this->ike_sa); + } + else + { + hsr = this->ike_sa->get_my_host(this->ike_sa); + hsi = this->ike_sa->get_other_host(this->ike_sa); + } + /* add ID payload only if negotiating non host2host tunnels */ + if (!this->tsi->is_host(this->tsi, hsi) || + !this->tsr->is_host(this->tsr, hsr) || + this->tsi->get_protocol(this->tsi) || + this->tsr->get_protocol(this->tsr) || + this->tsi->get_from_port(this->tsi) || + this->tsr->get_from_port(this->tsr) || + this->tsi->get_to_port(this->tsi) != 65535 || + this->tsr->get_to_port(this->tsr) != 65535) + { + id_payload = id_payload_create_from_ts(this->tsi); + message->add_payload(message, &id_payload->payload_interface); + id_payload = id_payload_create_from_ts(this->tsr); + message->add_payload(message, &id_payload->payload_interface); + } +} + +/** + * Get traffic selectors from received message + */ +static bool get_ts(private_quick_mode_t *this, message_t *message) +{ + traffic_selector_t *tsi = NULL, *tsr = NULL; + enumerator_t *enumerator; + id_payload_t *id_payload; + payload_t *payload; + host_t *hsi, *hsr; + bool first = TRUE; + + enumerator = message->create_payload_enumerator(message); + while (enumerator->enumerate(enumerator, &payload)) + { + if (payload->get_type(payload) == ID_V1) + { + id_payload = (id_payload_t*)payload; + + if (first) + { + tsi = id_payload->get_ts(id_payload); + first = FALSE; + } + else + { + tsr = id_payload->get_ts(id_payload); + break; + } + } + } + enumerator->destroy(enumerator); + + /* create host2host selectors if ID payloads missing */ + if (this->initiator) + { + hsi = this->ike_sa->get_my_host(this->ike_sa); + hsr = this->ike_sa->get_other_host(this->ike_sa); + } + else + { + hsr = this->ike_sa->get_my_host(this->ike_sa); + hsi = this->ike_sa->get_other_host(this->ike_sa); + } + if (!tsi) + { + tsi = traffic_selector_create_from_subnet(hsi->clone(hsi), + hsi->get_family(hsi) == AF_INET ? 32 : 128, 0, 0); + } + if (!tsr) + { + tsr = traffic_selector_create_from_subnet(hsr->clone(hsr), + hsr->get_family(hsr) == AF_INET ? 32 : 128, 0, 0); + } + if (this->initiator) + { + /* check if peer selection valid */ + if (!tsr->is_contained_in(tsr, this->tsr) || + !tsi->is_contained_in(tsi, this->tsi)) + { + DBG1(DBG_IKE, "peer selected invalid traffic selectors: ", + "%R for %R, %R for %R", tsi, this->tsi, tsr, this->tsr); + tsi->destroy(tsi); + tsr->destroy(tsr); + return FALSE; + } + this->tsi->destroy(this->tsi); + this->tsr->destroy(this->tsr); + this->tsi = tsi; + this->tsr = tsr; + } + else + { + this->tsi = tsi; + this->tsr = tsr; + } + return TRUE; +} + +/** + * Add NAT-OA payloads + */ +static void add_nat_oa_payloads(private_quick_mode_t *this, message_t *message) +{ + identification_t *id; + id_payload_t *nat_oa; + host_t *src, *dst; + + src = message->get_source(message); + dst = message->get_destination(message); + + src = this->initiator ? src : dst; + dst = this->initiator ? dst : src; + + /* first NAT-OA is the initiator's address */ + id = identification_create_from_sockaddr(src->get_sockaddr(src)); + nat_oa = id_payload_create_from_identification(NAT_OA_V1, id); + message->add_payload(message, (payload_t*)nat_oa); + id->destroy(id); + + /* second NAT-OA is that of the responder */ + id = identification_create_from_sockaddr(dst->get_sockaddr(dst)); + nat_oa = id_payload_create_from_identification(NAT_OA_V1, id); + message->add_payload(message, (payload_t*)nat_oa); + id->destroy(id); +} + +/** + * Look up lifetimes + */ +static void get_lifetimes(private_quick_mode_t *this) +{ + lifetime_cfg_t *lft; + + lft = this->config->get_lifetime(this->config); + if (lft->time.life) + { + this->lifetime = lft->time.life; + } + else if (lft->bytes.life) + { + this->lifebytes = lft->bytes.life; + } + free(lft); +} + +/** + * Check and apply lifetimes + */ +static void apply_lifetimes(private_quick_mode_t *this, sa_payload_t *sa_payload) +{ + u_int32_t lifetime; + u_int64_t lifebytes; + + lifetime = sa_payload->get_lifetime(sa_payload); + lifebytes = sa_payload->get_lifebytes(sa_payload); + if (this->lifetime != lifetime) + { + DBG1(DBG_IKE, "received %us lifetime, configured %us, using lower", + lifetime, this->lifetime); + this->lifetime = min(this->lifetime, lifetime); + } + if (this->lifebytes != lifebytes) + { + DBG1(DBG_IKE, "received %llu lifebytes, configured %llu, using lower", + lifebytes, this->lifebytes); + this->lifebytes = min(this->lifebytes, lifebytes); + } +} + +/** + * Set the task ready to build notify error message + */ +static status_t send_notify(private_quick_mode_t *this, notify_type_t type) +{ + notify_payload_t *notify; + + notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1, + PROTO_ESP, type); + notify->set_spi(notify, this->spi_i); + + this->ike_sa->queue_task(this->ike_sa, + (task_t*)informational_create(this->ike_sa, notify)); + /* cancel all active/passive tasks in favour of informational */ + return ALREADY_DONE; +} + +METHOD(task_t, build_i, status_t, + private_quick_mode_t *this, message_t *message) +{ + switch (this->state) + { + case QM_INIT: + { + enumerator_t *enumerator; + sa_payload_t *sa_payload; + linked_list_t *list; + proposal_t *proposal; + ipsec_mode_t mode; + diffie_hellman_group_t group; + bool udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); + + this->child_sa = child_sa_create( + this->ike_sa->get_my_host(this->ike_sa), + this->ike_sa->get_other_host(this->ike_sa), + this->config, 0, udp); + + list = this->config->get_proposals(this->config, FALSE); + + this->spi_i = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP); + if (!this->spi_i) + { + DBG1(DBG_IKE, "allocating SPI from kernel failed"); + return FAILED; + } + enumerator = list->create_enumerator(list); + while (enumerator->enumerate(enumerator, &proposal)) + { + proposal->set_spi(proposal, this->spi_i); + } + enumerator->destroy(enumerator); + + mode = this->config->get_mode(this->config); + if (udp && mode == MODE_TRANSPORT) + { + /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ + add_nat_oa_payloads(this, message); + } + + get_lifetimes(this); + sa_payload = sa_payload_create_from_proposals_v1(list, + this->lifetime, this->lifebytes, AUTH_NONE, + mode, udp); + list->destroy_offset(list, offsetof(proposal_t, destroy)); + message->add_payload(message, &sa_payload->payload_interface); + + if (!add_nonce(this, &this->nonce_i, message)) + { + return FAILED; + } + + group = this->config->get_dh_group(this->config); + if (group != MODP_NONE) + { + this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat, + group); + if (!this->dh) + { + DBG1(DBG_IKE, "configured DH group %N not supported", + diffie_hellman_group_names, group); + return FAILED; + } + add_ke(this, message); + } + this->tsi = select_ts(this, TRUE, NULL); + this->tsr = select_ts(this, FALSE, NULL); + if (!this->tsi || !this->tsr) + { + return FAILED; + } + add_ts(this, message); + return NEED_MORE; + } + case QM_NEGOTIATED: + { + return SUCCESS; + } + default: + return FAILED; + } +} + +/** + * Check for notify errors, return TRUE if error found + */ +static bool has_notify_errors(private_quick_mode_t *this, message_t *message) +{ + enumerator_t *enumerator; + payload_t *payload; + bool err = FALSE; + + enumerator = message->create_payload_enumerator(message); + while (enumerator->enumerate(enumerator, &payload)) + { + if (payload->get_type(payload) == NOTIFY_V1) + { + notify_payload_t *notify; + notify_type_t type; + + notify = (notify_payload_t*)payload; + type = notify->get_notify_type(notify); + if (type < 16384) + { + + DBG1(DBG_IKE, "received %N error notify", + notify_type_names, type); + err = TRUE; + } + else + { + DBG1(DBG_IKE, "received %N notify", notify_type_names, type); + } + } + } + enumerator->destroy(enumerator); + + return err; +} + +METHOD(task_t, process_r, status_t, + private_quick_mode_t *this, message_t *message) +{ + switch (this->state) + { + case QM_INIT: + { + sa_payload_t *sa_payload; + linked_list_t *tsi, *tsr, *list; + peer_cfg_t *peer_cfg; + host_t *me, *other; + u_int16_t group; + bool udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); + + if (!get_ts(this, message)) + { + return FAILED; + } + me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE); + if (!me) + { + me = this->ike_sa->get_my_host(this->ike_sa); + } + other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE); + if (!other) + { + other = this->ike_sa->get_other_host(this->ike_sa); + } + peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa); + tsi = linked_list_create(); + tsr = linked_list_create(); + tsi->insert_last(tsi, this->tsi); + tsr->insert_last(tsr, this->tsr); + this->tsi = this->tsr = NULL; + this->config = peer_cfg->select_child_cfg(peer_cfg, tsr, tsi, + me, other); + this->tsi = select_ts(this, FALSE, tsi); + this->tsr = select_ts(this, TRUE, tsr); + tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); + tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); + if (!this->config) + { + DBG1(DBG_IKE, "no child config found"); + return send_notify(this, NO_PROPOSAL_CHOSEN); + } + + sa_payload = (sa_payload_t*)message->get_payload(message, + SECURITY_ASSOCIATION_V1); + if (!sa_payload) + { + DBG1(DBG_IKE, "sa payload missing"); + return send_notify(this, INVALID_PAYLOAD_TYPE); + } + list = sa_payload->get_proposals(sa_payload); + this->proposal = this->config->select_proposal(this->config, + list, FALSE, FALSE); + list->destroy_offset(list, offsetof(proposal_t, destroy)); + + get_lifetimes(this); + apply_lifetimes(this, sa_payload); + + if (!this->proposal) + { + DBG1(DBG_IKE, "no matching proposal found, sending %N", + notify_type_names, NO_PROPOSAL_CHOSEN); + return send_notify(this, NO_PROPOSAL_CHOSEN); + } + this->spi_i = this->proposal->get_spi(this->proposal); + + if (!get_nonce(this, &this->nonce_i, message)) + { + return send_notify(this, INVALID_PAYLOAD_TYPE); + } + + if (this->proposal->get_algorithm(this->proposal, + DIFFIE_HELLMAN_GROUP, &group, NULL)) + { + this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat, + group); + if (!this->dh) + { + DBG1(DBG_IKE, "negotiated DH group %N not supported", + diffie_hellman_group_names, group); + return send_notify(this, INVALID_KEY_INFORMATION); + } + if (!get_ke(this, message)) + { + return send_notify(this, INVALID_PAYLOAD_TYPE); + } + } + + this->child_sa = child_sa_create( + this->ike_sa->get_my_host(this->ike_sa), + this->ike_sa->get_other_host(this->ike_sa), + this->config, 0, udp); + return NEED_MORE; + } + case QM_NEGOTIATED: + { + if (has_notify_errors(this, message)) + { + return SUCCESS; + } + if (!install(this)) + { + this->ike_sa->queue_task(this->ike_sa, + (task_t*)quick_delete_create(this->ike_sa, + this->proposal->get_protocol(this->proposal), + this->spi_i, TRUE)); + return ALREADY_DONE; + } + return SUCCESS; + } + default: + return FAILED; + } +} + +METHOD(task_t, build_r, status_t, + private_quick_mode_t *this, message_t *message) +{ + switch (this->state) + { + case QM_INIT: + { + sa_payload_t *sa_payload; + ipsec_mode_t mode; + bool udp = this->child_sa->has_encap(this->child_sa); + + this->spi_r = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP); + if (!this->spi_r) + { + DBG1(DBG_IKE, "allocating SPI from kernel failed"); + return send_notify(this, NO_PROPOSAL_CHOSEN); + } + this->proposal->set_spi(this->proposal, this->spi_r); + + mode = this->config->get_mode(this->config); + if (udp && mode == MODE_TRANSPORT) + { + /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ + add_nat_oa_payloads(this, message); + } + + sa_payload = sa_payload_create_from_proposal_v1(this->proposal, + this->lifetime, this->lifebytes, AUTH_NONE, + mode, udp); + message->add_payload(message, &sa_payload->payload_interface); + + if (!add_nonce(this, &this->nonce_r, message)) + { + return FAILED; + } + if (this->dh) + { + add_ke(this, message); + } + + add_ts(this, message); + + this->state = QM_NEGOTIATED; + return NEED_MORE; + } + default: + return FAILED; + } +} + +METHOD(task_t, process_i, status_t, + private_quick_mode_t *this, message_t *message) +{ + switch (this->state) + { + case QM_INIT: + { + sa_payload_t *sa_payload; + linked_list_t *list; + + sa_payload = (sa_payload_t*)message->get_payload(message, + SECURITY_ASSOCIATION_V1); + if (!sa_payload) + { + DBG1(DBG_IKE, "sa payload missing"); + return send_notify(this, NO_PROPOSAL_CHOSEN); + } + list = sa_payload->get_proposals(sa_payload); + this->proposal = this->config->select_proposal(this->config, + list, FALSE, FALSE); + list->destroy_offset(list, offsetof(proposal_t, destroy)); + if (!this->proposal) + { + DBG1(DBG_IKE, "no matching proposal found"); + return send_notify(this, NO_PROPOSAL_CHOSEN); + } + this->spi_r = this->proposal->get_spi(this->proposal); + + apply_lifetimes(this, sa_payload); + + if (!get_nonce(this, &this->nonce_r, message)) + { + return send_notify(this, INVALID_PAYLOAD_TYPE); + } + if (this->dh && !get_ke(this, message)) + { + return send_notify(this, INVALID_KEY_INFORMATION); + } + if (!get_ts(this, message)) + { + return send_notify(this, INVALID_PAYLOAD_TYPE); + } + if (!install(this)) + { + return send_notify(this, NO_PROPOSAL_CHOSEN); + } + this->state = QM_NEGOTIATED; + return NEED_MORE; + } + default: + return FAILED; + } +} + +METHOD(task_t, get_type, task_type_t, + private_quick_mode_t *this) +{ + return TASK_QUICK_MODE; +} + +METHOD(task_t, migrate, void, + private_quick_mode_t *this, ike_sa_t *ike_sa) +{ + this->ike_sa = ike_sa; +} + +METHOD(task_t, destroy, void, + private_quick_mode_t *this) +{ + chunk_free(&this->nonce_i); + chunk_free(&this->nonce_r); + DESTROY_IF(this->tsi); + DESTROY_IF(this->tsr); + DESTROY_IF(this->proposal); + DESTROY_IF(this->child_sa); + DESTROY_IF(this->config); + DESTROY_IF(this->dh); + free(this); +} + +/* + * Described in header. + */ +quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config, + traffic_selector_t *tsi, traffic_selector_t *tsr) +{ + private_quick_mode_t *this; + + INIT(this, + .public = { + .task = { + .get_type = _get_type, + .migrate = _migrate, + .destroy = _destroy, + }, + }, + .ike_sa = ike_sa, + .initiator = config != NULL, + .config = config, + .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa), + .state = QM_INIT, + ); + + if (config) + { + this->public.task.build = _build_i; + this->public.task.process = _process_i; + } + else + { + this->public.task.build = _build_r; + this->public.task.process = _process_r; + } + + return &this->public; +} -- cgit v1.2.3 From 28e3c6595dd888166efe4bcab92fe6ae0d7603bd Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Tue, 20 Dec 2011 11:15:15 +0100 Subject: Check if a config has been selected before narrowing selectors in quick mode --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index 9e71642af..df7b6375f 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -697,8 +697,11 @@ METHOD(task_t, process_r, status_t, this->tsi = this->tsr = NULL; this->config = peer_cfg->select_child_cfg(peer_cfg, tsr, tsi, me, other); - this->tsi = select_ts(this, FALSE, tsi); - this->tsr = select_ts(this, TRUE, tsr); + if (this->config) + { + this->tsi = select_ts(this, FALSE, tsi); + this->tsr = select_ts(this, TRUE, tsr); + } tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); if (!this->config) -- cgit v1.2.3 From bce22af29e340b92d98d3937e6ef6d694ee125c9 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Tue, 20 Dec 2011 18:01:12 +0100 Subject: Implemented migration of quick mode task --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index df7b6375f..13db2bb91 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -114,11 +114,6 @@ struct private_quick_mode_t { */ u_int64_t lifebytes; - /** - * Notify type in case of error - */ - notify_type_t notify_type; - /** states of quick mode */ enum { QM_INIT, @@ -895,7 +890,30 @@ METHOD(task_t, get_type, task_type_t, METHOD(task_t, migrate, void, private_quick_mode_t *this, ike_sa_t *ike_sa) { + chunk_free(&this->nonce_i); + chunk_free(&this->nonce_r); + DESTROY_IF(this->tsi); + DESTROY_IF(this->tsr); + DESTROY_IF(this->proposal); + DESTROY_IF(this->child_sa); + DESTROY_IF(this->dh); + this->ike_sa = ike_sa; + this->keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa); + this->state = QM_INIT; + this->tsi = NULL; + this->tsr = NULL; + this->proposal = NULL; + this->child_sa = NULL; + this->dh = NULL; + this->spi_i = 0; + this->spi_r = 0; + + if (!this->initiator) + { + DESTROY_IF(this->config); + this->config = NULL; + } } METHOD(task_t, destroy, void, -- cgit v1.2.3 From 253d7e3eff9e0351a075c248594f0afde9a5971f Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 21 Dec 2011 14:39:05 +0100 Subject: Don't process notifies in quick mode task when we get an INFORMATIONAL --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index 13db2bb91..94e5bb85a 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -758,7 +758,8 @@ METHOD(task_t, process_r, status_t, } case QM_NEGOTIATED: { - if (has_notify_errors(this, message)) + if (message->get_exchange_type(message) == INFORMATIONAL_V1 || + has_notify_errors(this, message)) { return SUCCESS; } -- cgit v1.2.3 From 5f1df0a060e25f189f8856331d6bf3ce4ac83b73 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Thu, 22 Dec 2011 13:26:38 +0100 Subject: Double check that we could select a TS as quick mode responder --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index 94e5bb85a..017ce97d4 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -699,10 +699,10 @@ METHOD(task_t, process_r, status_t, } tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); - if (!this->config) + if (!this->config || !this->tsi || !this->tsr) { - DBG1(DBG_IKE, "no child config found"); - return send_notify(this, NO_PROPOSAL_CHOSEN); + DBG1(DBG_IKE, "no matching CHILD_SA config found"); + return send_notify(this, INVALID_ID_INFORMATION); } sa_payload = (sa_payload_t*)message->get_payload(message, -- cgit v1.2.3 From 14dc7941656d85dc53e9da2bc3a7c3440d904f0e Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 2 Jan 2012 13:36:10 +0100 Subject: Support installing of quick mode SAs with a specific reqid --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index 017ce97d4..b9acdb054 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -114,6 +114,11 @@ struct private_quick_mode_t { */ u_int64_t lifebytes; + /** + * Reqid to use, 0 for auto-allocate + */ + u_int32_t reqid; + /** states of quick mode */ enum { QM_INIT, @@ -552,7 +557,7 @@ METHOD(task_t, build_i, status_t, this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), - this->config, 0, udp); + this->config, this->reqid, udp); list = this->config->get_proposals(this->config, FALSE); @@ -753,7 +758,7 @@ METHOD(task_t, process_r, status_t, this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), - this->config, 0, udp); + this->config, this->reqid, udp); return NEED_MORE; } case QM_NEGOTIATED: @@ -888,6 +893,12 @@ METHOD(task_t, get_type, task_type_t, return TASK_QUICK_MODE; } +METHOD(quick_mode_t, use_reqid, void, + private_quick_mode_t *this, u_int32_t reqid) +{ + this->reqid = reqid; +} + METHOD(task_t, migrate, void, private_quick_mode_t *this, ike_sa_t *ike_sa) { @@ -946,6 +957,7 @@ quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config, .migrate = _migrate, .destroy = _destroy, }, + .use_reqid = _use_reqid, }, .ike_sa = ike_sa, .initiator = config != NULL, -- cgit v1.2.3 From 3a925f74ab3cc43bafa409b89feaa32caeb33364 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 2 Jan 2012 15:40:31 +0100 Subject: Do not query CHILD_SA during delete if they already expired --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index b9acdb054..dc0a01099 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -773,7 +773,7 @@ METHOD(task_t, process_r, status_t, this->ike_sa->queue_task(this->ike_sa, (task_t*)quick_delete_create(this->ike_sa, this->proposal->get_protocol(this->proposal), - this->spi_i, TRUE)); + this->spi_i, TRUE, TRUE)); return ALREADY_DONE; } return SUCCESS; -- cgit v1.2.3 From 31bd5c8c0e3a913fec42b775acd250147cd5fca8 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 2 Jan 2012 15:49:20 +0100 Subject: Reply quick mode with the same SA lifetime that we received --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index dc0a01099..ff52204a9 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -510,15 +510,15 @@ static void apply_lifetimes(private_quick_mode_t *this, sa_payload_t *sa_payload lifebytes = sa_payload->get_lifebytes(sa_payload); if (this->lifetime != lifetime) { - DBG1(DBG_IKE, "received %us lifetime, configured %us, using lower", + DBG1(DBG_IKE, "received %us lifetime, configured %us", lifetime, this->lifetime); - this->lifetime = min(this->lifetime, lifetime); + this->lifetime = lifetime; } if (this->lifebytes != lifebytes) { - DBG1(DBG_IKE, "received %llu lifebytes, configured %llu, using lower", + DBG1(DBG_IKE, "received %llu lifebytes, configured %llu", lifebytes, this->lifebytes); - this->lifebytes = min(this->lifebytes, lifebytes); + this->lifebytes = lifebytes; } } -- cgit v1.2.3 From f56c3c53f605f7ae807a92651d7ced6d21ce8023 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 2 Jan 2012 16:36:39 +0100 Subject: As responder, try to reuse the reqid of the CHILD_SA the initiator is rekeying --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index ff52204a9..0e8eea95a 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -661,6 +661,42 @@ static bool has_notify_errors(private_quick_mode_t *this, message_t *message) return err; } +/** + * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so + */ +static void check_for_rekeyed_child(private_quick_mode_t *this) +{ + enumerator_t *enumerator, *policies; + traffic_selector_t *local, *remote; + child_sa_t *child_sa; + + enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa); + while (this->reqid == 0 && enumerator->enumerate(enumerator, &child_sa)) + { + if (child_sa->get_state(child_sa) == CHILD_INSTALLED && + streq(child_sa->get_name(child_sa), + this->config->get_name(this->config))) + { + policies = child_sa->create_policy_enumerator(child_sa); + if (policies->enumerate(policies, &local, &remote)) + { + if (local->equals(local, this->tsr) && + remote->equals(remote, this->tsi) && + this->proposal->equals(this->proposal, + child_sa->get_proposal(child_sa))) + { + this->reqid = child_sa->get_reqid(child_sa); + child_sa->set_state(child_sa, CHILD_REKEYING); + DBG1(DBG_IKE, "detected rekeying of CHILD_SA %s{%u}", + child_sa->get_name(child_sa), this->reqid); + } + } + policies->destroy(policies); + } + } + enumerator->destroy(enumerator); +} + METHOD(task_t, process_r, status_t, private_quick_mode_t *this, message_t *message) { @@ -755,6 +791,8 @@ METHOD(task_t, process_r, status_t, } } + check_for_rekeyed_child(this); + this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), -- cgit v1.2.3 From 90731f38c9fdb0af1d726d0ec3aa6550a018420c Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Thu, 5 Jan 2012 15:02:40 +0100 Subject: Install quick mode CHILD_SAs with negotiated encapsulation mode --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index 0e8eea95a..fb7e1a0ff 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -119,6 +119,11 @@ struct private_quick_mode_t { */ u_int32_t reqid; + /** + * Negotiated mode, tunnel or transport + */ + ipsec_mode_t mode; + /** states of quick mode */ enum { QM_INIT, @@ -137,7 +142,7 @@ static bool install(private_quick_mode_t *this) this->child_sa->set_proposal(this->child_sa, this->proposal); this->child_sa->set_state(this->child_sa, CHILD_INSTALLING); - this->child_sa->set_mode(this->child_sa, MODE_TUNNEL); + this->child_sa->set_mode(this->child_sa, this->mode); this->child_sa->set_protocol(this->child_sa, this->proposal->get_protocol(this->proposal)); @@ -550,10 +555,10 @@ METHOD(task_t, build_i, status_t, sa_payload_t *sa_payload; linked_list_t *list; proposal_t *proposal; - ipsec_mode_t mode; diffie_hellman_group_t group; - bool udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); + bool udp; + udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), @@ -574,8 +579,8 @@ METHOD(task_t, build_i, status_t, } enumerator->destroy(enumerator); - mode = this->config->get_mode(this->config); - if (udp && mode == MODE_TRANSPORT) + this->mode = this->config->get_mode(this->config); + if (udp && this->mode == MODE_TRANSPORT) { /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ add_nat_oa_payloads(this, message); @@ -584,7 +589,7 @@ METHOD(task_t, build_i, status_t, get_lifetimes(this); sa_payload = sa_payload_create_from_proposals_v1(list, this->lifetime, this->lifebytes, AUTH_NONE, - mode, udp); + this->mode, udp); list->destroy_offset(list, offsetof(proposal_t, destroy)); message->add_payload(message, &sa_payload->payload_interface); @@ -709,7 +714,7 @@ METHOD(task_t, process_r, status_t, peer_cfg_t *peer_cfg; host_t *me, *other; u_int16_t group; - bool udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); + bool udp; if (!get_ts(this, message)) { @@ -760,6 +765,7 @@ METHOD(task_t, process_r, status_t, get_lifetimes(this); apply_lifetimes(this, sa_payload); + this->mode = sa_payload->get_encap_mode(sa_payload, &udp); if (!this->proposal) { @@ -829,8 +835,7 @@ METHOD(task_t, build_r, status_t, case QM_INIT: { sa_payload_t *sa_payload; - ipsec_mode_t mode; - bool udp = this->child_sa->has_encap(this->child_sa); + bool udp; this->spi_r = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP); if (!this->spi_r) @@ -840,8 +845,8 @@ METHOD(task_t, build_r, status_t, } this->proposal->set_spi(this->proposal, this->spi_r); - mode = this->config->get_mode(this->config); - if (udp && mode == MODE_TRANSPORT) + udp = this->child_sa->has_encap(this->child_sa); + if (udp && this->mode == MODE_TRANSPORT) { /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ add_nat_oa_payloads(this, message); @@ -849,7 +854,7 @@ METHOD(task_t, build_r, status_t, sa_payload = sa_payload_create_from_proposal_v1(this->proposal, this->lifetime, this->lifebytes, AUTH_NONE, - mode, udp); + this->mode, udp); message->add_payload(message, &sa_payload->payload_interface); if (!add_nonce(this, &this->nonce_r, message)) -- cgit v1.2.3 From c40963b457f27464bc88390c7e6419d461264b47 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 9 Jan 2012 18:12:17 +0100 Subject: Enforce encapsulation mode of configuration, in case initiator proposes both --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index fb7e1a0ff..dedeab189 100644 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -765,7 +765,6 @@ METHOD(task_t, process_r, status_t, get_lifetimes(this); apply_lifetimes(this, sa_payload); - this->mode = sa_payload->get_encap_mode(sa_payload, &udp); if (!this->proposal) { @@ -799,6 +798,7 @@ METHOD(task_t, process_r, status_t, check_for_rekeyed_child(this); + udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), @@ -846,6 +846,7 @@ METHOD(task_t, build_r, status_t, this->proposal->set_spi(this->proposal, this->spi_r); udp = this->child_sa->has_encap(this->child_sa); + this->mode = this->config->get_mode(this->config); if (udp && this->mode == MODE_TRANSPORT) { /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ -- cgit v1.2.3 From 3e6b740336e8df61b1a94a1b21f142731f0fd912 Mon Sep 17 00:00:00 2001 From: Clavister OpenSource Date: Tue, 10 Jan 2012 14:37:39 +0100 Subject: Isakmp_dpd task added. --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 src/libcharon/sa/ikev1/tasks/quick_mode.c (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c old mode 100644 new mode 100755 index dedeab189..b27b00de7 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -539,7 +539,7 @@ static status_t send_notify(private_quick_mode_t *this, notify_type_t type) notify->set_spi(notify, this->spi_i); this->ike_sa->queue_task(this->ike_sa, - (task_t*)informational_create(this->ike_sa, notify)); + (task_t*)informational_create(this->ike_sa, notify, 0)); /* cancel all active/passive tasks in favour of informational */ return ALREADY_DONE; } -- cgit v1.2.3 From 2ddd45c9a76a77d673cca5ce0bd2ea0bb7ee859a Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Tue, 10 Jan 2012 17:21:52 +0100 Subject: Simplified DPD handling by using a task for a single message only --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index b27b00de7..dedeab189 100755 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -539,7 +539,7 @@ static status_t send_notify(private_quick_mode_t *this, notify_type_t type) notify->set_spi(notify, this->spi_i); this->ike_sa->queue_task(this->ike_sa, - (task_t*)informational_create(this->ike_sa, notify, 0)); + (task_t*)informational_create(this->ike_sa, notify)); /* cancel all active/passive tasks in favour of informational */ return ALREADY_DONE; } -- cgit v1.2.3 From 669d8bded29d895678b74ed19cab21e34235d657 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 16 Jan 2012 16:17:27 +0100 Subject: Invoke child_rekey hook instead of child_updown when rekeying a quick mode --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index dedeab189..bc2b5cfd1 100755 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -119,6 +119,11 @@ struct private_quick_mode_t { */ u_int32_t reqid; + /** + * SPI of SA we rekey + */ + u_int32_t rekey; + /** * Negotiated mode, tunnel or transport */ @@ -139,6 +144,7 @@ static bool install(private_quick_mode_t *this) status_t status, status_i, status_o; chunk_t encr_i, encr_r, integ_i, integ_r; linked_list_t *tsi, *tsr; + child_sa_t *old = NULL; this->child_sa->set_proposal(this->child_sa, this->proposal); this->child_sa->set_state(this->child_sa, CHILD_INSTALLING); @@ -219,8 +225,20 @@ static bool install(private_quick_mode_t *this) this->child_sa->get_traffic_selectors(this->child_sa, TRUE), this->child_sa->get_traffic_selectors(this->child_sa, FALSE)); - charon->bus->child_updown(charon->bus, this->child_sa, TRUE); - + if (this->rekey) + { + old = this->ike_sa->get_child_sa(this->ike_sa, + this->proposal->get_protocol(this->proposal), + this->rekey, TRUE); + } + if (old) + { + charon->bus->child_rekey(charon->bus, old, this->child_sa); + } + else + { + charon->bus->child_updown(charon->bus, this->child_sa, TRUE); + } this->child_sa = NULL; return TRUE; @@ -691,6 +709,7 @@ static void check_for_rekeyed_child(private_quick_mode_t *this) child_sa->get_proposal(child_sa))) { this->reqid = child_sa->get_reqid(child_sa); + this->rekey = child_sa->get_spi(child_sa, TRUE); child_sa->set_state(child_sa, CHILD_REKEYING); DBG1(DBG_IKE, "detected rekeying of CHILD_SA %s{%u}", child_sa->get_name(child_sa), this->reqid); @@ -943,6 +962,12 @@ METHOD(quick_mode_t, use_reqid, void, this->reqid = reqid; } +METHOD(quick_mode_t, rekey, void, + private_quick_mode_t *this, u_int32_t spi) +{ + this->rekey = spi; +} + METHOD(task_t, migrate, void, private_quick_mode_t *this, ike_sa_t *ike_sa) { @@ -1002,6 +1027,7 @@ quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config, .destroy = _destroy, }, .use_reqid = _use_reqid, + .rekey = _rekey, }, .ike_sa = ike_sa, .initiator = config != NULL, -- cgit v1.2.3 From 696fa8e003f9e0a441f805ef2809b150fd47cc79 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 18 Jan 2012 13:28:15 +0100 Subject: Invoke bus_t.narrow hook in quick mode exchange --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 43 ++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index bc2b5cfd1..6956dcb40 100755 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -156,8 +156,26 @@ static bool install(private_quick_mode_t *this) encr_i = encr_r = integ_i = integ_r = chunk_empty; tsi = linked_list_create(); tsr = linked_list_create(); - tsi->insert_last(tsi, this->tsi); - tsr->insert_last(tsr, this->tsr); + tsi->insert_last(tsi, this->tsi->clone(this->tsi)); + tsr->insert_last(tsr, this->tsr->clone(this->tsr)); + if (this->initiator) + { + charon->bus->narrow(charon->bus, this->child_sa, + NARROW_INITIATOR_POST_AUTH, tsi, tsr); + } + else + { + charon->bus->narrow(charon->bus, this->child_sa, + NARROW_RESPONDER, tsr, tsi); + } + if (tsi->get_count(tsi) == 0 || tsr->get_count(tsr) == 0) + { + tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); + tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); + DBG1(DBG_IKE, "no acceptable traffic selectors found"); + return FALSE; + } + if (this->keymat->derive_child_keys(this->keymat, this->proposal, this->dh, this->spi_i, this->spi_r, this->nonce_i, this->nonce_r, &encr_i, &integ_i, &encr_r, &integ_r)) @@ -188,8 +206,8 @@ static bool install(private_quick_mode_t *this) (status_i != SUCCESS) ? "inbound " : "", (status_i != SUCCESS && status_o != SUCCESS) ? "and ": "", (status_o != SUCCESS) ? "outbound " : ""); - tsi->destroy(tsi); - tsr->destroy(tsr); + tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); + tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); return FALSE; } @@ -201,8 +219,8 @@ static bool install(private_quick_mode_t *this) { status = this->child_sa->add_policies(this->child_sa, tsr, tsi); } - tsi->destroy(tsi); - tsr->destroy(tsr); + tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); + tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); if (status != SUCCESS) { DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel"); @@ -571,7 +589,7 @@ METHOD(task_t, build_i, status_t, { enumerator_t *enumerator; sa_payload_t *sa_payload; - linked_list_t *list; + linked_list_t *list, *tsi, *tsr; proposal_t *proposal; diffie_hellman_group_t group; bool udp; @@ -631,6 +649,17 @@ METHOD(task_t, build_i, status_t, } this->tsi = select_ts(this, TRUE, NULL); this->tsr = select_ts(this, FALSE, NULL); + tsi = linked_list_create(); + tsr = linked_list_create(); + tsi->insert_last(tsi, this->tsi); + tsr->insert_last(tsr, this->tsr); + this->tsi = this->tsr = NULL; + charon->bus->narrow(charon->bus, this->child_sa, + NARROW_INITIATOR_PRE_AUTH, tsi, tsr); + tsi->remove_first(tsi, (void**)&this->tsi); + tsr->remove_first(tsr, (void**)&this->tsr); + tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy)); + tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy)); if (!this->tsi || !this->tsr) { return FAILED; -- cgit v1.2.3 From c60246a6181cdb3306ec423246e2821b734a02ed Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 23 Jan 2012 13:49:56 +0100 Subject: Support inactivity timeout in IKEv1 CHILD_SAs --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index 6956dcb40..d8bc2884c 100755 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -26,6 +26,7 @@ #include #include #include +#include typedef struct private_quick_mode_t private_quick_mode_t; @@ -136,6 +137,25 @@ struct private_quick_mode_t { } state; }; +/** + * Schedule inactivity timeout for CHILD_SA with reqid, if enabled + */ +static void schedule_inactivity_timeout(private_quick_mode_t *this) +{ + u_int32_t timeout; + bool close_ike; + + timeout = this->config->get_inactivity(this->config); + if (timeout) + { + close_ike = lib->settings->get_bool(lib->settings, + "charon.inactivity_close_ike", FALSE); + lib->scheduler->schedule_job(lib->scheduler, (job_t*) + inactivity_job_create(this->child_sa->get_reqid(this->child_sa), + timeout, close_ike), timeout); + } +} + /** * Install negotiated CHILD_SA */ @@ -257,8 +277,11 @@ static bool install(private_quick_mode_t *this) { charon->bus->child_updown(charon->bus, this->child_sa, TRUE); } + if (!this->rekey) + { + schedule_inactivity_timeout(this); + } this->child_sa = NULL; - return TRUE; } -- cgit v1.2.3 From 7fd7ffc6492f766e8cd14b93b4688ca2a7eb495c Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 23 Jan 2012 15:11:13 +0100 Subject: Use UDP encapsulation even in non-NAT situation if initiator requests it --- src/libcharon/sa/ikev1/tasks/quick_mode.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/libcharon/sa/ikev1/tasks/quick_mode.c') diff --git a/src/libcharon/sa/ikev1/tasks/quick_mode.c b/src/libcharon/sa/ikev1/tasks/quick_mode.c index d8bc2884c..30dc95671 100755 --- a/src/libcharon/sa/ikev1/tasks/quick_mode.c +++ b/src/libcharon/sa/ikev1/tasks/quick_mode.c @@ -130,6 +130,11 @@ struct private_quick_mode_t { */ ipsec_mode_t mode; + /** + * Use UDP encapsulation + */ + bool udp; + /** states of quick mode */ enum { QM_INIT, @@ -615,13 +620,12 @@ METHOD(task_t, build_i, status_t, linked_list_t *list, *tsi, *tsr; proposal_t *proposal; diffie_hellman_group_t group; - bool udp; - udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); + this->udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), - this->config, this->reqid, udp); + this->config, this->reqid, this->udp); list = this->config->get_proposals(this->config, FALSE); @@ -639,7 +643,7 @@ METHOD(task_t, build_i, status_t, enumerator->destroy(enumerator); this->mode = this->config->get_mode(this->config); - if (udp && this->mode == MODE_TRANSPORT) + if (this->udp && this->mode == MODE_TRANSPORT) { /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ add_nat_oa_payloads(this, message); @@ -648,7 +652,7 @@ METHOD(task_t, build_i, status_t, get_lifetimes(this); sa_payload = sa_payload_create_from_proposals_v1(list, this->lifetime, this->lifebytes, AUTH_NONE, - this->mode, udp); + this->mode, this->udp); list->destroy_offset(list, offsetof(proposal_t, destroy)); message->add_payload(message, &sa_payload->payload_interface); @@ -785,7 +789,6 @@ METHOD(task_t, process_r, status_t, peer_cfg_t *peer_cfg; host_t *me, *other; u_int16_t group; - bool udp; if (!get_ts(this, message)) { @@ -834,6 +837,8 @@ METHOD(task_t, process_r, status_t, list, FALSE, FALSE); list->destroy_offset(list, offsetof(proposal_t, destroy)); + this->mode = sa_payload->get_encap_mode(sa_payload, &this->udp); + get_lifetimes(this); apply_lifetimes(this, sa_payload); @@ -869,11 +874,10 @@ METHOD(task_t, process_r, status_t, check_for_rekeyed_child(this); - udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY); this->child_sa = child_sa_create( this->ike_sa->get_my_host(this->ike_sa), this->ike_sa->get_other_host(this->ike_sa), - this->config, this->reqid, udp); + this->config, this->reqid, this->udp); return NEED_MORE; } case QM_NEGOTIATED: @@ -906,7 +910,6 @@ METHOD(task_t, build_r, status_t, case QM_INIT: { sa_payload_t *sa_payload; - bool udp; this->spi_r = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP); if (!this->spi_r) @@ -916,9 +919,7 @@ METHOD(task_t, build_r, status_t, } this->proposal->set_spi(this->proposal, this->spi_r); - udp = this->child_sa->has_encap(this->child_sa); - this->mode = this->config->get_mode(this->config); - if (udp && this->mode == MODE_TRANSPORT) + if (this->udp && this->mode == MODE_TRANSPORT) { /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */ add_nat_oa_payloads(this, message); @@ -926,7 +927,7 @@ METHOD(task_t, build_r, status_t, sa_payload = sa_payload_create_from_proposal_v1(this->proposal, this->lifetime, this->lifebytes, AUTH_NONE, - this->mode, udp); + this->mode, this->udp); message->add_payload(message, &sa_payload->payload_interface); if (!add_nonce(this, &this->nonce_r, message)) -- cgit v1.2.3