*/
#define MAX_OLD_HASHES 2
+/**
+ * First sequence number of responding packets.
+ *
+ * To distinguish retransmission jobs for initiating and responding packets,
+ * we split up the sequence counter and use the upper half for responding.
+ */
+#define RESPONDING_SEQ INT_MAX
+
typedef struct exchange_t exchange_t;
/**
* Exchange we are currently handling as responder
*/
struct {
+ /**
+ * Message ID of the last response
+ */
+ u_int32_t mid;
+
/**
* Hash of a previously received message
*/
*/
packet_t *packet;
+ /**
+ * Sequence number of the last sent message
+ */
+ u_int32_t seqnr;
+
+ /**
+ * how many times we have retransmitted so far
+ */
+ u_int retransmitted;
+
} responding;
/**
return found;
}
-METHOD(task_manager_t, retransmit, status_t,
- private_task_manager_t *this, u_int32_t message_seqnr)
+/**
+ * Retransmit a packet, either as initiator or as responder
+ */
+static status_t retransmit_packet(private_task_manager_t *this, u_int32_t seqnr,
+ u_int mid, u_int retransmitted, packet_t *packet)
{
- /* this.initiating packet used as marker for received response */
- if (message_seqnr == this->initiating.seqnr && this->initiating.packet )
- {
- u_int32_t timeout;
- packet_t *packet;
- job_t *job;
+ u_int32_t t;
- if (this->initiating.retransmitted <= this->retransmit_tries)
+ if (retransmitted > this->retransmit_tries)
+ {
+ DBG1(DBG_IKE, "giving up after %u retransmits", retransmitted - 1);
+ if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
{
- timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
- pow(this->retransmit_base, this->initiating.retransmitted));
+ charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
}
- else
+ return DESTROY_ME;
+ }
+ t = (u_int32_t)(this->retransmit_timeout * 1000.0 *
+ pow(this->retransmit_base, retransmitted));
+ if (retransmitted)
+ {
+ DBG1(DBG_IKE, "sending retransmit %u of %s message ID %u, seq %u",
+ retransmitted, seqnr < RESPONDING_SEQ ? "request" : "response",
+ mid, seqnr < RESPONDING_SEQ ? seqnr : seqnr - RESPONDING_SEQ);
+ }
+ charon->sender->send(charon->sender, packet->clone(packet));
+ lib->scheduler->schedule_job_ms(lib->scheduler, (job_t*)
+ retransmit_job_create(seqnr, this->ike_sa->get_id(this->ike_sa)), t);
+ return NEED_MORE;
+}
+
+METHOD(task_manager_t, retransmit, status_t,
+ private_task_manager_t *this, u_int32_t seqnr)
+{
+ status_t status = SUCCESS;
+
+ if (seqnr == this->initiating.seqnr && this->initiating.packet)
+ {
+ status = retransmit_packet(this, seqnr, this->initiating.mid,
+ this->initiating.retransmitted, this->initiating.packet);
+ if (status == NEED_MORE)
{
- DBG1(DBG_IKE, "giving up after %d retransmits",
- this->initiating.retransmitted - 1);
- if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
- {
- charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
- }
- return DESTROY_ME;
+ this->initiating.retransmitted++;
+ status = SUCCESS;
}
-
- if (this->initiating.retransmitted)
+ }
+ if (seqnr == this->responding.seqnr && this->responding.packet)
+ {
+ status = retransmit_packet(this, seqnr, this->responding.mid,
+ this->responding.retransmitted, this->responding.packet);
+ if (status == NEED_MORE)
{
- DBG1(DBG_IKE, "retransmit %d of request with message ID %u seqnr (%d)",
- this->initiating.retransmitted, this->initiating.mid, message_seqnr);
+ this->responding.retransmitted++;
+ status = SUCCESS;
}
- packet = this->initiating.packet->clone(this->initiating.packet);
- charon->sender->send(charon->sender, packet);
-
- this->initiating.retransmitted++;
- job = (job_t*)retransmit_job_create(this->initiating.seqnr,
- this->ike_sa->get_id(this->ike_sa));
- lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
}
- return SUCCESS;
+ return status;
}
METHOD(task_manager_t, initiate, status_t,
message->destroy(message);
return initiate(this);
}
- this->initiating.seqnr++;
+ DESTROY_IF(this->initiating.packet);
status = this->ike_sa->generate_message(this->ike_sa, message,
&this->initiating.packet);
if (status != SUCCESS)
charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
return DESTROY_ME;
}
- message->destroy(message);
+ this->initiating.seqnr++;
if (expect_response)
{
+ message->destroy(message);
return retransmit(this, this->initiating.seqnr);
}
- charon->sender->send(charon->sender,
- this->initiating.packet->clone(this->initiating.packet));
- this->initiating.packet->destroy(this->initiating.packet);
- this->initiating.packet = NULL;
+ if (message->get_exchange_type(message) == QUICK_MODE)
+ { /* keep the packet for retransmission in quick mode. The responder
+ * might request a retransmission */
+ charon->sender->send(charon->sender,
+ this->initiating.packet->clone(this->initiating.packet));
+ }
+ else
+ {
+ charon->sender->send(charon->sender, this->initiating.packet);
+ this->initiating.packet = NULL;
+ }
+ message->destroy(message);
if (exchange == INFORMATIONAL_V1)
{
return initiate(this);
}
-/**
- * handle exchange collisions
- */
-static bool handle_collisions(private_task_manager_t *this, task_t *task)
-{
- return FALSE;
-}
-
/**
* build a response depending on the "passive" task list
*/
task_t *task;
message_t *message;
host_t *me, *other;
- bool delete = FALSE, flushed = FALSE;
+ bool delete = FALSE, flushed = FALSE, expect_request = FALSE;
status_t status;
me = request->get_destination(request);
message->set_message_id(message, request->get_message_id(request));
message->set_request(message, FALSE);
+ this->responding.mid = request->get_message_id(request);
+ this->responding.retransmitted = 0;
+ this->responding.seqnr++;
+
enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
while (enumerator->enumerate(enumerator, (void*)&task))
{
case SUCCESS:
/* task completed, remove it */
this->passive_tasks->remove_at(this->passive_tasks, enumerator);
- if (!handle_collisions(this, task))
- {
- task->destroy(task);
- }
+ task->destroy(task);
continue;
case NEED_MORE:
/* processed, but task needs another exchange */
- if (handle_collisions(this, task))
- {
- this->passive_tasks->remove_at(this->passive_tasks,
- enumerator);
+ if (task->get_type(task) == TASK_QUICK_MODE)
+ { /* we rely on initiator retransmission, except for
+ * three-message exchanges */
+ expect_request = TRUE;
}
continue;
case ALREADY_DONE:
return DESTROY_ME;
}
+ if (expect_request && !delete)
+ {
+ return retransmit(this, this->responding.seqnr);
+ }
charon->sender->send(charon->sender,
- this->responding.packet->clone(this->responding.packet));
+ this->responding.packet->clone(this->responding.packet));
if (delete)
{
return DESTROY_ME;
{
if (this->initiating.old_hashes[i] == hash)
{
+ if (this->initiating.packet &&
+ i == (this->initiating.old_hash_pos % MAX_OLD_HASHES) &&
+ msg->get_exchange_type(msg) == QUICK_MODE)
+ {
+ DBG1(DBG_IKE, "received retransmit of response with ID %u, "
+ "resending last request", mid);
+ charon->sender->send(charon->sender,
+ this->initiating.packet->clone(this->initiating.packet));
+ return SUCCESS;
+ }
DBG1(DBG_IKE, "received retransmit of response with ID %u, "
"but next request already sent", mid);
return SUCCESS;
flush(this);
return DESTROY_ME;
}
- this->initiating.old_hashes[(this->initiating.old_hash_pos++) %
+ this->initiating.old_hashes[(++this->initiating.old_hash_pos) %
MAX_OLD_HASHES] = hash;
}
else
ike_sa_id_t *ike_sa_id;
ike_cfg_t *ike_cfg;
job_t *job;
+
ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
if (ike_cfg == NULL)
{
DESTROY_IF(this->responding.packet);
DESTROY_IF(this->initiating.packet);
this->responding.packet = NULL;
+ this->responding.seqnr = RESPONDING_SEQ;
+ this->responding.retransmitted = 0;
this->initiating.packet = NULL;
this->initiating.mid = 0;
this->initiating.seqnr = 0;
},
.ike_sa = ike_sa,
.initiating.type = EXCHANGE_TYPE_UNDEFINED,
+ .responding.seqnr = RESPONDING_SEQ,
.rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
.queued_tasks = linked_list_create(),
.active_tasks = linked_list_create(),