]>
Commit | Line | Data |
---|---|---|
4a09d9ee | 1 | /* |
4505e3c0 | 2 | * Copyright (C) 2007-2018 Tobias Brunner |
68c6863b | 3 | * Copyright (C) 2007-2011 Martin Willi |
208678e6 | 4 | * HSR Hochschule fuer Technik Rapperswil |
4a09d9ee MW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
15 | */ | |
16 | ||
17 | #include "task_manager_v1.h" | |
18 | ||
da063ec9 MW |
19 | #include <math.h> |
20 | ||
4a09d9ee | 21 | #include <daemon.h> |
15a682f4 | 22 | #include <sa/ikev1/tasks/main_mode.h> |
830ab2ae | 23 | #include <sa/ikev1/tasks/aggressive_mode.h> |
15a682f4 | 24 | #include <sa/ikev1/tasks/quick_mode.h> |
83c5fda0 | 25 | #include <sa/ikev1/tasks/quick_delete.h> |
15a682f4 MW |
26 | #include <sa/ikev1/tasks/xauth.h> |
27 | #include <sa/ikev1/tasks/mode_config.h> | |
28 | #include <sa/ikev1/tasks/informational.h> | |
29 | #include <sa/ikev1/tasks/isakmp_natd.h> | |
30 | #include <sa/ikev1/tasks/isakmp_vendor.h> | |
31 | #include <sa/ikev1/tasks/isakmp_cert_pre.h> | |
32 | #include <sa/ikev1/tasks/isakmp_cert_post.h> | |
3ed148b3 | 33 | #include <sa/ikev1/tasks/isakmp_delete.h> |
3e6b7403 CO |
34 | #include <sa/ikev1/tasks/isakmp_dpd.h> |
35 | ||
da063ec9 | 36 | #include <processing/jobs/retransmit_job.h> |
68c6863b | 37 | #include <processing/jobs/delete_ike_sa_job.h> |
4b38c22c | 38 | #include <processing/jobs/dpd_timeout_job.h> |
30216000 TB |
39 | #include <processing/jobs/process_message_job.h> |
40 | ||
127a98dc | 41 | #include <collections/array.h> |
4a09d9ee | 42 | |
fce566a8 MW |
43 | /** |
44 | * Number of old messages hashes we keep for retransmission. | |
45 | * | |
46 | * In Main Mode, we must ignore messages from a previous message pair if | |
47 | * we already continued to the next. Otherwise a late retransmission | |
48 | * could be considered as a reply to the newer request. | |
49 | */ | |
50 | #define MAX_OLD_HASHES 2 | |
51 | ||
f5a84055 MW |
52 | /** |
53 | * First sequence number of responding packets. | |
54 | * | |
55 | * To distinguish retransmission jobs for initiating and responding packets, | |
56 | * we split up the sequence counter and use the upper half for responding. | |
57 | */ | |
58 | #define RESPONDING_SEQ INT_MAX | |
59 | ||
4a09d9ee MW |
60 | typedef struct exchange_t exchange_t; |
61 | ||
62 | /** | |
63 | * An exchange in the air, used do detect and handle retransmission | |
64 | */ | |
65 | struct exchange_t { | |
66 | ||
67 | /** | |
68 | * Message ID used for this transaction | |
69 | */ | |
b12c53ce | 70 | uint32_t mid; |
4a09d9ee MW |
71 | |
72 | /** | |
73 | * generated packet for retransmission | |
74 | */ | |
75 | packet_t *packet; | |
76 | }; | |
77 | ||
78 | typedef struct private_task_manager_t private_task_manager_t; | |
79 | ||
80 | /** | |
81 | * private data of the task manager | |
82 | */ | |
83 | struct private_task_manager_t { | |
84 | ||
85 | /** | |
86 | * public functions | |
87 | */ | |
88 | task_manager_v1_t public; | |
89 | ||
90 | /** | |
91 | * associated IKE_SA we are serving | |
92 | */ | |
93 | ike_sa_t *ike_sa; | |
94 | ||
73aaf76b MW |
95 | /** |
96 | * RNG to create message IDs | |
97 | */ | |
98 | rng_t *rng; | |
99 | ||
4a09d9ee MW |
100 | /** |
101 | * Exchange we are currently handling as responder | |
102 | */ | |
103 | struct { | |
f5a84055 MW |
104 | /** |
105 | * Message ID of the last response | |
106 | */ | |
b12c53ce | 107 | uint32_t mid; |
f5a84055 | 108 | |
9cc8bd4f MW |
109 | /** |
110 | * Hash of a previously received message | |
111 | */ | |
b12c53ce | 112 | uint32_t hash; |
9cc8bd4f | 113 | |
4a09d9ee | 114 | /** |
127a98dc | 115 | * packet(s) for retransmission |
4a09d9ee | 116 | */ |
127a98dc | 117 | array_t *packets; |
4a09d9ee | 118 | |
f5a84055 MW |
119 | /** |
120 | * Sequence number of the last sent message | |
121 | */ | |
b12c53ce | 122 | uint32_t seqnr; |
f5a84055 MW |
123 | |
124 | /** | |
125 | * how many times we have retransmitted so far | |
126 | */ | |
127 | u_int retransmitted; | |
128 | ||
4a09d9ee MW |
129 | } responding; |
130 | ||
131 | /** | |
132 | * Exchange we are currently handling as initiator | |
133 | */ | |
134 | struct { | |
135 | /** | |
136 | * Message ID of the exchange | |
137 | */ | |
b12c53ce | 138 | uint32_t mid; |
4a09d9ee | 139 | |
fce566a8 MW |
140 | /** |
141 | * Hashes of old responses we can ignore | |
142 | */ | |
b12c53ce | 143 | uint32_t old_hashes[MAX_OLD_HASHES]; |
fce566a8 MW |
144 | |
145 | /** | |
146 | * Position in old hash array | |
147 | */ | |
148 | int old_hash_pos; | |
149 | ||
9cc8bd4f | 150 | /** |
751bd02e | 151 | * Sequence number of the last sent message |
9cc8bd4f | 152 | */ |
b12c53ce | 153 | uint32_t seqnr; |
9cc8bd4f | 154 | |
4a09d9ee MW |
155 | /** |
156 | * how many times we have retransmitted so far | |
157 | */ | |
158 | u_int retransmitted; | |
159 | ||
160 | /** | |
127a98dc | 161 | * packet(s) for retransmission |
4a09d9ee | 162 | */ |
127a98dc | 163 | array_t *packets; |
4a09d9ee MW |
164 | |
165 | /** | |
30216000 | 166 | * type of the initiated exchange |
4a09d9ee MW |
167 | */ |
168 | exchange_type_t type; | |
169 | ||
170 | } initiating; | |
171 | ||
30216000 | 172 | /** |
70e0be07 | 173 | * Message we are currently defragmenting, if any (only one at a time) |
30216000 | 174 | */ |
70e0be07 | 175 | message_t *defrag; |
30216000 | 176 | |
4a09d9ee MW |
177 | /** |
178 | * List of queued tasks not yet in action | |
179 | */ | |
180 | linked_list_t *queued_tasks; | |
181 | ||
182 | /** | |
30216000 | 183 | * List of active tasks, initiated by ourselves |
4a09d9ee MW |
184 | */ |
185 | linked_list_t *active_tasks; | |
186 | ||
187 | /** | |
188 | * List of tasks initiated by peer | |
189 | */ | |
190 | linked_list_t *passive_tasks; | |
191 | ||
f91b6ac7 MW |
192 | /** |
193 | * Queued messages not yet ready to process | |
194 | */ | |
195 | message_t *queued; | |
196 | ||
4a09d9ee MW |
197 | /** |
198 | * Number of times we retransmit messages before giving up | |
199 | */ | |
200 | u_int retransmit_tries; | |
201 | ||
202 | /** | |
203 | * Retransmission timeout | |
204 | */ | |
205 | double retransmit_timeout; | |
206 | ||
207 | /** | |
208 | * Base to calculate retransmission timeout | |
209 | */ | |
210 | double retransmit_base; | |
3e6b7403 | 211 | |
389e4b8e TB |
212 | /** |
213 | * Jitter to apply to calculated retransmit timeout (in percent) | |
214 | */ | |
215 | u_int retransmit_jitter; | |
216 | ||
217 | /** | |
218 | * Limit retransmit timeout to this value | |
219 | */ | |
220 | uint32_t retransmit_limit; | |
221 | ||
3e6b7403 CO |
222 | /** |
223 | * Sequence number for sending DPD requests | |
224 | */ | |
b12c53ce | 225 | uint32_t dpd_send; |
3e6b7403 CO |
226 | |
227 | /** | |
228 | * Sequence number for received DPD requests | |
229 | */ | |
b12c53ce | 230 | uint32_t dpd_recv; |
4a09d9ee MW |
231 | }; |
232 | ||
127a98dc TB |
233 | /** |
234 | * Reset retransmission packet list | |
235 | */ | |
236 | static void clear_packets(array_t *array) | |
237 | { | |
238 | packet_t *packet; | |
239 | ||
240 | while (array_remove(array, ARRAY_TAIL, &packet)) | |
241 | { | |
242 | packet->destroy(packet); | |
243 | } | |
244 | } | |
245 | ||
a5c79960 MW |
246 | METHOD(task_manager_t, flush_queue, void, |
247 | private_task_manager_t *this, task_queue_t queue) | |
0f61964e | 248 | { |
a5c79960 | 249 | linked_list_t *list; |
0f61964e MW |
250 | task_t *task; |
251 | ||
429d95fe MW |
252 | if (this->queued) |
253 | { | |
254 | this->queued->destroy(this->queued); | |
255 | this->queued = NULL; | |
256 | } | |
a5c79960 MW |
257 | switch (queue) |
258 | { | |
259 | case TASK_QUEUE_ACTIVE: | |
260 | list = this->active_tasks; | |
17949695 MW |
261 | /* cancel pending retransmits */ |
262 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; | |
127a98dc | 263 | clear_packets(this->initiating.packets); |
a5c79960 MW |
264 | break; |
265 | case TASK_QUEUE_PASSIVE: | |
266 | list = this->passive_tasks; | |
267 | break; | |
268 | case TASK_QUEUE_QUEUED: | |
269 | list = this->queued_tasks; | |
270 | break; | |
271 | default: | |
272 | return; | |
273 | } | |
0f61964e MW |
274 | while (list->remove_last(list, (void**)&task) == SUCCESS) |
275 | { | |
276 | task->destroy(task); | |
277 | } | |
278 | } | |
279 | ||
b1908994 TE |
280 | METHOD(task_manager_t, flush, void, |
281 | private_task_manager_t *this) | |
4a09d9ee | 282 | { |
a5c79960 MW |
283 | flush_queue(this, TASK_QUEUE_QUEUED); |
284 | flush_queue(this, TASK_QUEUE_PASSIVE); | |
285 | flush_queue(this, TASK_QUEUE_ACTIVE); | |
4a09d9ee MW |
286 | } |
287 | ||
26b55dc6 MW |
288 | /** |
289 | * move a task of a specific type from the queue to the active list | |
290 | */ | |
291 | static bool activate_task(private_task_manager_t *this, task_type_t type) | |
292 | { | |
293 | enumerator_t *enumerator; | |
294 | task_t *task; | |
295 | bool found = FALSE; | |
296 | ||
297 | enumerator = this->queued_tasks->create_enumerator(this->queued_tasks); | |
298 | while (enumerator->enumerate(enumerator, (void**)&task)) | |
299 | { | |
300 | if (task->get_type(task) == type) | |
301 | { | |
302 | DBG2(DBG_IKE, " activating %N task", task_type_names, type); | |
303 | this->queued_tasks->remove_at(this->queued_tasks, enumerator); | |
304 | this->active_tasks->insert_last(this->active_tasks, task); | |
305 | found = TRUE; | |
306 | break; | |
307 | } | |
308 | } | |
309 | enumerator->destroy(enumerator); | |
310 | return found; | |
311 | } | |
312 | ||
5cafefcd | 313 | /** |
127a98dc | 314 | * Send packets in the given array (they get cloned) |
5cafefcd | 315 | */ |
127a98dc | 316 | static void send_packets(private_task_manager_t *this, array_t *packets) |
5cafefcd | 317 | { |
127a98dc | 318 | enumerator_t *enumerator; |
5cafefcd | 319 | packet_t *packet; |
5cafefcd | 320 | |
127a98dc TB |
321 | enumerator = array_create_enumerator(packets); |
322 | while (enumerator->enumerate(enumerator, &packet)) | |
5cafefcd | 323 | { |
127a98dc | 324 | charon->sender->send(charon->sender, packet->clone(packet)); |
5cafefcd | 325 | } |
127a98dc | 326 | enumerator->destroy(enumerator); |
5cafefcd TB |
327 | } |
328 | ||
329 | /** | |
127a98dc | 330 | * Generates the given message and stores packet(s) in the given array |
5cafefcd | 331 | */ |
127a98dc TB |
332 | static bool generate_message(private_task_manager_t *this, message_t *message, |
333 | array_t **packets) | |
5cafefcd | 334 | { |
127a98dc | 335 | enumerator_t *fragments; |
40bab9a1 | 336 | packet_t *fragment; |
127a98dc | 337 | |
40bab9a1 TB |
338 | if (this->ike_sa->generate_message_fragmented(this->ike_sa, message, |
339 | &fragments) != SUCCESS) | |
127a98dc | 340 | { |
127a98dc TB |
341 | return FALSE; |
342 | } | |
40bab9a1 | 343 | while (fragments->enumerate(fragments, &fragment)) |
127a98dc | 344 | { |
40bab9a1 | 345 | array_insert_create(packets, ARRAY_TAIL, fragment); |
5cafefcd | 346 | } |
127a98dc | 347 | fragments->destroy(fragments); |
40bab9a1 | 348 | return TRUE; |
5cafefcd TB |
349 | } |
350 | ||
f5a84055 | 351 | /** |
127a98dc | 352 | * Retransmit a packet (or its fragments) |
f5a84055 | 353 | */ |
b12c53ce | 354 | static status_t retransmit_packet(private_task_manager_t *this, uint32_t seqnr, |
127a98dc | 355 | u_int mid, u_int retransmitted, array_t *packets) |
23f4e4b4 | 356 | { |
127a98dc | 357 | packet_t *packet; |
389e4b8e | 358 | uint32_t t, max_jitter; |
23f4e4b4 | 359 | |
127a98dc | 360 | array_get(packets, 0, &packet); |
f5a84055 MW |
361 | if (retransmitted > this->retransmit_tries) |
362 | { | |
363 | DBG1(DBG_IKE, "giving up after %u retransmits", retransmitted - 1); | |
3c79b7b7 | 364 | charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND_TIMEOUT, packet); |
f5a84055 MW |
365 | return DESTROY_ME; |
366 | } | |
b12c53ce | 367 | t = (uint32_t)(this->retransmit_timeout * 1000.0 * |
f5a84055 | 368 | pow(this->retransmit_base, retransmitted)); |
00d54711 TB |
369 | if (this->retransmit_limit) |
370 | { | |
371 | t = min(t, this->retransmit_limit); | |
372 | } | |
389e4b8e TB |
373 | if (this->retransmit_jitter) |
374 | { | |
375 | max_jitter = (t / 100.0) * this->retransmit_jitter; | |
376 | t -= max_jitter * (random() / (RAND_MAX + 1.0)); | |
377 | } | |
f5a84055 MW |
378 | if (retransmitted) |
379 | { | |
380 | DBG1(DBG_IKE, "sending retransmit %u of %s message ID %u, seq %u", | |
381 | retransmitted, seqnr < RESPONDING_SEQ ? "request" : "response", | |
382 | mid, seqnr < RESPONDING_SEQ ? seqnr : seqnr - RESPONDING_SEQ); | |
9da65eae TE |
383 | charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND, packet, |
384 | retransmitted); | |
f5a84055 | 385 | } |
127a98dc | 386 | send_packets(this, packets); |
f5a84055 MW |
387 | lib->scheduler->schedule_job_ms(lib->scheduler, (job_t*) |
388 | retransmit_job_create(seqnr, this->ike_sa->get_id(this->ike_sa)), t); | |
8c50bb6c | 389 | return SUCCESS; |
f5a84055 MW |
390 | } |
391 | ||
392 | METHOD(task_manager_t, retransmit, status_t, | |
b12c53ce | 393 | private_task_manager_t *this, uint32_t seqnr) |
f5a84055 MW |
394 | { |
395 | status_t status = SUCCESS; | |
396 | ||
127a98dc TB |
397 | if (seqnr == this->initiating.seqnr && |
398 | array_count(this->initiating.packets)) | |
f5a84055 | 399 | { |
127a98dc TB |
400 | status = retransmit_packet(this, seqnr, this->initiating.mid, |
401 | this->initiating.retransmitted, this->initiating.packets); | |
8c50bb6c | 402 | if (status == SUCCESS) |
23f4e4b4 | 403 | { |
f5a84055 | 404 | this->initiating.retransmitted++; |
23f4e4b4 | 405 | } |
f5a84055 | 406 | } |
127a98dc TB |
407 | if (seqnr == this->responding.seqnr && |
408 | array_count(this->responding.packets)) | |
f5a84055 | 409 | { |
127a98dc TB |
410 | status = retransmit_packet(this, seqnr, this->responding.mid, |
411 | this->responding.retransmitted, this->responding.packets); | |
8c50bb6c | 412 | if (status == SUCCESS) |
23f4e4b4 | 413 | { |
f5a84055 | 414 | this->responding.retransmitted++; |
23f4e4b4 | 415 | } |
23f4e4b4 | 416 | } |
f5a84055 | 417 | return status; |
23f4e4b4 CO |
418 | } |
419 | ||
27137790 MW |
420 | /** |
421 | * Check if we have to wait for a mode config before starting a quick mode | |
422 | */ | |
423 | static bool mode_config_expected(private_task_manager_t *this) | |
424 | { | |
101d26ba | 425 | enumerator_t *enumerator; |
27137790 | 426 | peer_cfg_t *peer_cfg; |
497ce2cf | 427 | char *pool; |
e3311e9b | 428 | bool local; |
101d26ba | 429 | host_t *host; |
27137790 MW |
430 | |
431 | peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa); | |
497ce2cf | 432 | if (peer_cfg) |
27137790 | 433 | { |
e3311e9b MW |
434 | if (peer_cfg->use_pull_mode(peer_cfg)) |
435 | { | |
436 | enumerator = peer_cfg->create_pool_enumerator(peer_cfg); | |
437 | if (!enumerator->enumerate(enumerator, &pool)) | |
438 | { /* no pool configured */ | |
439 | enumerator->destroy(enumerator); | |
440 | return FALSE; | |
441 | } | |
497ce2cf | 442 | enumerator->destroy(enumerator); |
e3311e9b MW |
443 | |
444 | local = FALSE; | |
497ce2cf | 445 | } |
e3311e9b MW |
446 | else |
447 | { | |
448 | enumerator = peer_cfg->create_virtual_ip_enumerator(peer_cfg); | |
449 | if (!enumerator->enumerate(enumerator, &host)) | |
450 | { /* not requesting a vip */ | |
451 | enumerator->destroy(enumerator); | |
452 | return FALSE; | |
453 | } | |
454 | enumerator->destroy(enumerator); | |
497ce2cf | 455 | |
e3311e9b MW |
456 | local = TRUE; |
457 | } | |
101d26ba | 458 | enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa, |
e3311e9b | 459 | local); |
101d26ba | 460 | if (!enumerator->enumerate(enumerator, &host)) |
e3311e9b | 461 | { /* expecting a VIP exchange, but no VIP assigned yet */ |
101d26ba | 462 | enumerator->destroy(enumerator); |
27137790 MW |
463 | return TRUE; |
464 | } | |
101d26ba | 465 | enumerator->destroy(enumerator); |
27137790 MW |
466 | } |
467 | return FALSE; | |
468 | } | |
469 | ||
4a09d9ee MW |
470 | METHOD(task_manager_t, initiate, status_t, |
471 | private_task_manager_t *this) | |
472 | { | |
26b55dc6 MW |
473 | enumerator_t *enumerator; |
474 | task_t *task; | |
475 | message_t *message; | |
476 | host_t *me, *other; | |
73aaf76b | 477 | exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED; |
7ce504e1 | 478 | bool new_mid = FALSE, expect_response = FALSE, cancelled = FALSE, keep = FALSE; |
73aaf76b | 479 | |
477559ca MW |
480 | if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED && |
481 | this->initiating.type != INFORMATIONAL_V1) | |
26b55dc6 MW |
482 | { |
483 | DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress", | |
484 | exchange_type_names, this->initiating.type); | |
485 | /* do not initiate if we already have a message in the air */ | |
486 | return SUCCESS; | |
487 | } | |
488 | ||
489 | if (this->active_tasks->get_count(this->active_tasks) == 0) | |
490 | { | |
491 | DBG2(DBG_IKE, "activating new tasks"); | |
492 | switch (this->ike_sa->get_state(this->ike_sa)) | |
493 | { | |
494 | case IKE_CREATED: | |
2e3c9f87 | 495 | activate_task(this, TASK_ISAKMP_VENDOR); |
824dc0ad | 496 | activate_task(this, TASK_ISAKMP_CERT_PRE); |
a09972df | 497 | if (activate_task(this, TASK_MAIN_MODE)) |
26b55dc6 MW |
498 | { |
499 | exchange = ID_PROT; | |
500 | } | |
830ab2ae MW |
501 | else if (activate_task(this, TASK_AGGRESSIVE_MODE)) |
502 | { | |
503 | exchange = AGGRESSIVE; | |
504 | } | |
505 | activate_task(this, TASK_ISAKMP_CERT_POST); | |
506 | activate_task(this, TASK_ISAKMP_NATD); | |
26b55dc6 | 507 | break; |
c5dc9d33 | 508 | case IKE_CONNECTING: |
429d95fe MW |
509 | if (activate_task(this, TASK_ISAKMP_DELETE)) |
510 | { | |
511 | exchange = INFORMATIONAL_V1; | |
512 | new_mid = TRUE; | |
513 | break; | |
514 | } | |
69adeb5b | 515 | if (activate_task(this, TASK_XAUTH)) |
744c0801 | 516 | { |
c5dc9d33 | 517 | exchange = TRANSACTION; |
3e246c48 | 518 | new_mid = TRUE; |
429d95fe | 519 | break; |
744c0801 | 520 | } |
accf4612 MW |
521 | if (activate_task(this, TASK_INFORMATIONAL)) |
522 | { | |
523 | exchange = INFORMATIONAL_V1; | |
524 | new_mid = TRUE; | |
429d95fe | 525 | break; |
accf4612 | 526 | } |
c5dc9d33 CO |
527 | break; |
528 | case IKE_ESTABLISHED: | |
156b8662 MW |
529 | if (activate_task(this, TASK_MODE_CONFIG)) |
530 | { | |
531 | exchange = TRANSACTION; | |
532 | new_mid = TRUE; | |
533 | break; | |
534 | } | |
2e06179f | 535 | if (activate_task(this, TASK_QUICK_DELETE)) |
23f4e4b4 | 536 | { |
612fe541 | 537 | exchange = INFORMATIONAL_V1; |
3e246c48 MW |
538 | new_mid = TRUE; |
539 | break; | |
23f4e4b4 | 540 | } |
2e06179f | 541 | if (activate_task(this, TASK_ISAKMP_DELETE)) |
accf4612 MW |
542 | { |
543 | exchange = INFORMATIONAL_V1; | |
544 | new_mid = TRUE; | |
429d95fe | 545 | break; |
accf4612 | 546 | } |
2e49bbcc | 547 | if (activate_task(this, TASK_ISAKMP_DPD)) |
8ef2bae4 | 548 | { |
2e49bbcc | 549 | exchange = INFORMATIONAL_V1; |
8ef2bae4 | 550 | new_mid = TRUE; |
429d95fe | 551 | break; |
8ef2bae4 | 552 | } |
2e49bbcc AH |
553 | if (!mode_config_expected(this) && |
554 | activate_task(this, TASK_QUICK_MODE)) | |
8ef2bae4 | 555 | { |
2e49bbcc | 556 | exchange = QUICK_MODE; |
8ef2bae4 | 557 | new_mid = TRUE; |
429d95fe | 558 | break; |
8ef2bae4 | 559 | } |
2e49bbcc | 560 | if (activate_task(this, TASK_INFORMATIONAL)) |
3e6b7403 CO |
561 | { |
562 | exchange = INFORMATIONAL_V1; | |
563 | new_mid = TRUE; | |
564 | break; | |
565 | } | |
744c0801 | 566 | break; |
b56b8502 TB |
567 | case IKE_REKEYING: |
568 | if (activate_task(this, TASK_ISAKMP_DELETE)) | |
569 | { | |
570 | exchange = INFORMATIONAL_V1; | |
571 | new_mid = TRUE; | |
572 | break; | |
573 | } | |
f15c85a4 TB |
574 | if (activate_task(this, TASK_ISAKMP_DPD)) |
575 | { | |
576 | exchange = INFORMATIONAL_V1; | |
577 | new_mid = TRUE; | |
578 | break; | |
579 | } | |
b56b8502 | 580 | break; |
26b55dc6 MW |
581 | default: |
582 | break; | |
583 | } | |
584 | } | |
585 | else | |
586 | { | |
587 | DBG2(DBG_IKE, "reinitiating already active tasks"); | |
588 | enumerator = this->active_tasks->create_enumerator(this->active_tasks); | |
589 | while (enumerator->enumerate(enumerator, (void**)&task)) | |
590 | { | |
591 | DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task)); | |
592 | switch (task->get_type(task)) | |
593 | { | |
a09972df | 594 | case TASK_MAIN_MODE: |
26b55dc6 MW |
595 | exchange = ID_PROT; |
596 | break; | |
830ab2ae MW |
597 | case TASK_AGGRESSIVE_MODE: |
598 | exchange = AGGRESSIVE; | |
599 | break; | |
744c0801 MW |
600 | case TASK_QUICK_MODE: |
601 | exchange = QUICK_MODE; | |
602 | break; | |
69adeb5b | 603 | case TASK_XAUTH: |
52ac2ceb CO |
604 | exchange = TRANSACTION; |
605 | new_mid = TRUE; | |
606 | break; | |
26b55dc6 MW |
607 | default: |
608 | continue; | |
609 | } | |
610 | break; | |
611 | } | |
612 | enumerator->destroy(enumerator); | |
613 | } | |
614 | ||
73aaf76b | 615 | if (exchange == EXCHANGE_TYPE_UNDEFINED) |
26b55dc6 MW |
616 | { |
617 | DBG2(DBG_IKE, "nothing to initiate"); | |
618 | /* nothing to do yet... */ | |
619 | return SUCCESS; | |
620 | } | |
621 | ||
622 | me = this->ike_sa->get_my_host(this->ike_sa); | |
623 | other = this->ike_sa->get_other_host(this->ike_sa); | |
624 | ||
3e246c48 | 625 | if (new_mid) |
26b55dc6 | 626 | { |
1bb9c51e TB |
627 | if (!this->rng->get_bytes(this->rng, sizeof(this->initiating.mid), |
628 | (void*)&this->initiating.mid)) | |
629 | { | |
630 | DBG1(DBG_IKE, "failed to allocate message ID, destroying IKE_SA"); | |
631 | flush(this); | |
632 | return DESTROY_ME; | |
633 | } | |
26b55dc6 | 634 | } |
1bb9c51e | 635 | message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION); |
3e246c48 | 636 | message->set_message_id(message, this->initiating.mid); |
26b55dc6 MW |
637 | message->set_source(message, me->clone(me)); |
638 | message->set_destination(message, other->clone(other)); | |
639 | message->set_exchange_type(message, exchange); | |
640 | this->initiating.type = exchange; | |
641 | this->initiating.retransmitted = 0; | |
642 | ||
643 | enumerator = this->active_tasks->create_enumerator(this->active_tasks); | |
644 | while (enumerator->enumerate(enumerator, (void*)&task)) | |
645 | { | |
646 | switch (task->build(task, message)) | |
647 | { | |
648 | case SUCCESS: | |
649 | /* task completed, remove it */ | |
650 | this->active_tasks->remove_at(this->active_tasks, enumerator); | |
182d55b2 MW |
651 | if (task->get_type(task) == TASK_AGGRESSIVE_MODE || |
652 | task->get_type(task) == TASK_QUICK_MODE) | |
1b82eb23 MW |
653 | { /* last message of three message exchange */ |
654 | keep = TRUE; | |
655 | } | |
26b55dc6 | 656 | task->destroy(task); |
590ca1d4 | 657 | continue; |
26b55dc6 | 658 | case NEED_MORE: |
751bd02e | 659 | expect_response = TRUE; |
26b55dc6 | 660 | /* processed, but task needs another exchange */ |
590ca1d4 MW |
661 | continue; |
662 | case ALREADY_DONE: | |
7ce504e1 | 663 | cancelled = TRUE; |
26b55dc6 MW |
664 | break; |
665 | case FAILED: | |
666 | default: | |
667 | if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING) | |
668 | { | |
669 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); | |
670 | } | |
671 | /* FALL */ | |
672 | case DESTROY_ME: | |
673 | /* critical failure, destroy IKE_SA */ | |
674 | enumerator->destroy(enumerator); | |
675 | message->destroy(message); | |
676 | flush(this); | |
677 | return DESTROY_ME; | |
678 | } | |
590ca1d4 | 679 | break; |
26b55dc6 MW |
680 | } |
681 | enumerator->destroy(enumerator); | |
682 | ||
1b82eb23 MW |
683 | if (this->active_tasks->get_count(this->active_tasks) == 0 && |
684 | (exchange == QUICK_MODE || exchange == AGGRESSIVE)) | |
41fbde45 MW |
685 | { /* tasks completed, no exchange active anymore */ |
686 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; | |
687 | } | |
7ce504e1 | 688 | if (cancelled) |
b64d6423 MW |
689 | { |
690 | message->destroy(message); | |
691 | return initiate(this); | |
692 | } | |
26b55dc6 | 693 | |
127a98dc TB |
694 | clear_packets(this->initiating.packets); |
695 | if (!generate_message(this, message, &this->initiating.packets)) | |
26b55dc6 MW |
696 | { |
697 | /* message generation failed. There is nothing more to do than to | |
698 | * close the SA */ | |
699 | message->destroy(message); | |
700 | flush(this); | |
701 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); | |
702 | return DESTROY_ME; | |
703 | } | |
26b55dc6 | 704 | |
f5a84055 | 705 | this->initiating.seqnr++; |
3babde90 | 706 | if (expect_response) |
751bd02e | 707 | { |
f5a84055 | 708 | message->destroy(message); |
751bd02e CO |
709 | return retransmit(this, this->initiating.seqnr); |
710 | } | |
8c50bb6c TE |
711 | send_packets(this, this->initiating.packets); |
712 | if (!keep) | |
f5a84055 | 713 | { |
127a98dc | 714 | clear_packets(this->initiating.packets); |
f5a84055 MW |
715 | } |
716 | message->destroy(message); | |
46505067 | 717 | |
07095794 | 718 | if (exchange == INFORMATIONAL_V1) |
46505067 | 719 | { |
07095794 MW |
720 | switch (this->ike_sa->get_state(this->ike_sa)) |
721 | { | |
722 | case IKE_CONNECTING: | |
723 | /* close after sending an INFORMATIONAL when unestablished */ | |
85a6fe23 | 724 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); |
07095794 MW |
725 | return FAILED; |
726 | case IKE_DELETING: | |
727 | /* close after sending a DELETE */ | |
728 | return DESTROY_ME; | |
729 | default: | |
730 | break; | |
731 | } | |
46505067 | 732 | } |
daee47ba | 733 | return initiate(this); |
4a09d9ee MW |
734 | } |
735 | ||
4a09d9ee MW |
736 | /** |
737 | * build a response depending on the "passive" task list | |
738 | */ | |
739 | static status_t build_response(private_task_manager_t *this, message_t *request) | |
740 | { | |
741 | enumerator_t *enumerator; | |
742 | task_t *task; | |
743 | message_t *message; | |
744 | host_t *me, *other; | |
7ce504e1 | 745 | bool delete = FALSE, cancelled = FALSE, expect_request = FALSE; |
4a09d9ee MW |
746 | |
747 | me = request->get_destination(request); | |
748 | other = request->get_source(request); | |
749 | ||
750 | message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION); | |
751 | message->set_exchange_type(message, request->get_exchange_type(request)); | |
752 | /* send response along the path the request came in */ | |
753 | message->set_source(message, me->clone(me)); | |
754 | message->set_destination(message, other->clone(other)); | |
1b82eb23 | 755 | message->set_message_id(message, request->get_message_id(request)); |
4a09d9ee MW |
756 | message->set_request(message, FALSE); |
757 | ||
f5a84055 MW |
758 | this->responding.mid = request->get_message_id(request); |
759 | this->responding.retransmitted = 0; | |
760 | this->responding.seqnr++; | |
761 | ||
4a09d9ee MW |
762 | enumerator = this->passive_tasks->create_enumerator(this->passive_tasks); |
763 | while (enumerator->enumerate(enumerator, (void*)&task)) | |
764 | { | |
765 | switch (task->build(task, message)) | |
766 | { | |
767 | case SUCCESS: | |
768 | /* task completed, remove it */ | |
769 | this->passive_tasks->remove_at(this->passive_tasks, enumerator); | |
f5a84055 | 770 | task->destroy(task); |
590ca1d4 | 771 | continue; |
4a09d9ee MW |
772 | case NEED_MORE: |
773 | /* processed, but task needs another exchange */ | |
346dad30 MW |
774 | if (task->get_type(task) == TASK_QUICK_MODE || |
775 | task->get_type(task) == TASK_AGGRESSIVE_MODE) | |
f5a84055 MW |
776 | { /* we rely on initiator retransmission, except for |
777 | * three-message exchanges */ | |
778 | expect_request = TRUE; | |
4a09d9ee | 779 | } |
590ca1d4 MW |
780 | continue; |
781 | case ALREADY_DONE: | |
7ce504e1 | 782 | cancelled = TRUE; |
4a09d9ee | 783 | break; |
37a22a16 TB |
784 | case INVALID_ARG: |
785 | if (task->get_type(task) == TASK_QUICK_MODE) | |
786 | { /* not responsible for this exchange */ | |
787 | continue; | |
788 | } | |
789 | /* FALL */ | |
4a09d9ee MW |
790 | case FAILED: |
791 | default: | |
792 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); | |
793 | /* FALL */ | |
794 | case DESTROY_ME: | |
795 | /* destroy IKE_SA, but SEND response first */ | |
796 | delete = TRUE; | |
797 | break; | |
798 | } | |
590ca1d4 | 799 | break; |
4a09d9ee MW |
800 | } |
801 | enumerator->destroy(enumerator); | |
802 | ||
127a98dc | 803 | clear_packets(this->responding.packets); |
7ce504e1 | 804 | if (cancelled) |
b64d6423 MW |
805 | { |
806 | message->destroy(message); | |
807 | return initiate(this); | |
808 | } | |
127a98dc | 809 | if (!generate_message(this, message, &this->responding.packets)) |
4a09d9ee | 810 | { |
127a98dc | 811 | message->destroy(message); |
4a09d9ee MW |
812 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); |
813 | return DESTROY_ME; | |
814 | } | |
127a98dc | 815 | message->destroy(message); |
4a09d9ee | 816 | |
f5a84055 MW |
817 | if (expect_request && !delete) |
818 | { | |
819 | return retransmit(this, this->responding.seqnr); | |
820 | } | |
127a98dc | 821 | send_packets(this, this->responding.packets); |
4a09d9ee MW |
822 | if (delete) |
823 | { | |
824 | return DESTROY_ME; | |
825 | } | |
826 | return SUCCESS; | |
827 | } | |
828 | ||
fbbd439b CO |
829 | /** |
830 | * Send a notify in a separate INFORMATIONAL exchange back to the sender. | |
e647c98a | 831 | * The notify protocol_id is set to ISAKMP |
fbbd439b | 832 | */ |
b4705269 MW |
833 | static void send_notify(private_task_manager_t *this, message_t *request, |
834 | notify_type_t type) | |
fbbd439b CO |
835 | { |
836 | message_t *response; | |
127a98dc | 837 | array_t *packets = NULL; |
fbbd439b | 838 | host_t *me, *other; |
b12c53ce | 839 | uint32_t mid; |
fbbd439b | 840 | |
3973ffa4 | 841 | if (request->get_exchange_type(request) == INFORMATIONAL_V1) |
fbbd439b CO |
842 | { /* don't respond to INFORMATIONAL requests to avoid a notify war */ |
843 | DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request"); | |
844 | return; | |
845 | } | |
1bb9c51e TB |
846 | if (!this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid)) |
847 | { | |
848 | DBG1(DBG_IKE, "failed to allocate message ID"); | |
849 | return; | |
850 | } | |
fbbd439b CO |
851 | response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION); |
852 | response->set_exchange_type(response, INFORMATIONAL_V1); | |
853 | response->set_request(response, TRUE); | |
fbbd439b | 854 | response->set_message_id(response, mid); |
b4705269 | 855 | response->add_payload(response, (payload_t*) |
3ecfc83c | 856 | notify_payload_create_from_protocol_and_type(PLV1_NOTIFY, |
b4705269 | 857 | PROTO_IKE, type)); |
fbbd439b CO |
858 | |
859 | me = this->ike_sa->get_my_host(this->ike_sa); | |
860 | if (me->is_anyaddr(me)) | |
861 | { | |
862 | me = request->get_destination(request); | |
863 | this->ike_sa->set_my_host(this->ike_sa, me->clone(me)); | |
864 | } | |
865 | other = this->ike_sa->get_other_host(this->ike_sa); | |
866 | if (other->is_anyaddr(other)) | |
867 | { | |
868 | other = request->get_source(request); | |
869 | this->ike_sa->set_other_host(this->ike_sa, other->clone(other)); | |
870 | } | |
871 | response->set_source(response, me->clone(me)); | |
872 | response->set_destination(response, other->clone(other)); | |
127a98dc | 873 | if (generate_message(this, response, &packets)) |
fbbd439b | 874 | { |
127a98dc | 875 | send_packets(this, packets); |
fbbd439b | 876 | } |
127a98dc TB |
877 | clear_packets(packets); |
878 | array_destroy(packets); | |
fbbd439b CO |
879 | response->destroy(response); |
880 | } | |
881 | ||
24742c0f MW |
882 | /** |
883 | * Process a DPD request/response | |
884 | */ | |
885 | static bool process_dpd(private_task_manager_t *this, message_t *message) | |
886 | { | |
887 | notify_payload_t *notify; | |
888 | notify_type_t type; | |
b12c53ce | 889 | uint32_t seq; |
24742c0f MW |
890 | chunk_t data; |
891 | ||
892 | type = DPD_R_U_THERE; | |
893 | notify = message->get_notify(message, type); | |
894 | if (!notify) | |
895 | { | |
896 | type = DPD_R_U_THERE_ACK; | |
897 | notify = message->get_notify(message, type); | |
898 | } | |
899 | if (!notify) | |
900 | { | |
901 | return FALSE; | |
902 | } | |
903 | data = notify->get_notification_data(notify); | |
904 | if (data.len != 4) | |
905 | { | |
906 | return FALSE; | |
907 | } | |
908 | seq = untoh32(data.ptr); | |
909 | ||
910 | if (type == DPD_R_U_THERE) | |
911 | { | |
912 | if (this->dpd_recv == 0 || seq == this->dpd_recv) | |
913 | { /* check sequence validity */ | |
914 | this->dpd_recv = seq + 1; | |
915 | this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, | |
916 | time_monotonic(NULL)); | |
917 | } | |
918 | /* but respond anyway */ | |
919 | this->ike_sa->queue_task(this->ike_sa, | |
920 | &isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE_ACK, seq)->task); | |
921 | } | |
922 | else /* DPD_R_U_THERE_ACK */ | |
923 | { | |
9de3140d | 924 | if (seq == this->dpd_send) |
24742c0f | 925 | { |
9de3140d | 926 | this->dpd_send++; |
24742c0f MW |
927 | this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, |
928 | time_monotonic(NULL)); | |
929 | } | |
930 | else | |
931 | { | |
932 | DBG1(DBG_IKE, "received invalid DPD sequence number %u " | |
9de3140d | 933 | "(expected %u), ignored", seq, this->dpd_send); |
24742c0f MW |
934 | } |
935 | } | |
936 | return TRUE; | |
937 | } | |
938 | ||
4de361d9 TB |
939 | /** |
940 | * Check if we already have a quick mode task queued for the exchange with the | |
941 | * given message ID | |
942 | */ | |
b12c53ce | 943 | static bool have_quick_mode_task(private_task_manager_t *this, uint32_t mid) |
4de361d9 TB |
944 | { |
945 | enumerator_t *enumerator; | |
946 | quick_mode_t *qm; | |
947 | task_t *task; | |
948 | bool found = FALSE; | |
949 | ||
950 | enumerator = this->passive_tasks->create_enumerator(this->passive_tasks); | |
951 | while (enumerator->enumerate(enumerator, &task)) | |
952 | { | |
953 | if (task->get_type(task) == TASK_QUICK_MODE) | |
954 | { | |
955 | qm = (quick_mode_t*)task; | |
956 | if (qm->get_mid(qm) == mid) | |
957 | { | |
958 | found = TRUE; | |
959 | break; | |
960 | } | |
961 | } | |
962 | } | |
963 | enumerator->destroy(enumerator); | |
964 | return found; | |
965 | } | |
966 | ||
b0b0c913 | 967 | /** |
1b4e7fe1 | 968 | * Check if we still have a specific task queued |
b0b0c913 | 969 | */ |
1b4e7fe1 | 970 | static bool have_task_queued(private_task_manager_t *this, task_type_t type) |
b0b0c913 TB |
971 | { |
972 | enumerator_t *enumerator; | |
973 | task_t *task; | |
974 | bool found = FALSE; | |
975 | ||
976 | enumerator = this->passive_tasks->create_enumerator(this->passive_tasks); | |
977 | while (enumerator->enumerate(enumerator, &task)) | |
978 | { | |
1b4e7fe1 | 979 | if (task->get_type(task) == type) |
b0b0c913 TB |
980 | { |
981 | found = TRUE; | |
982 | break; | |
983 | } | |
984 | } | |
985 | enumerator->destroy(enumerator); | |
986 | return found; | |
987 | } | |
988 | ||
4a09d9ee MW |
989 | /** |
990 | * handle an incoming request message | |
991 | */ | |
992 | static status_t process_request(private_task_manager_t *this, | |
993 | message_t *message) | |
994 | { | |
995 | enumerator_t *enumerator; | |
996 | task_t *task = NULL; | |
ff6b084a | 997 | bool send_response = FALSE, dpd = FALSE; |
4a09d9ee | 998 | |
9276f712 | 999 | if (message->get_exchange_type(message) == INFORMATIONAL_V1 || |
4de361d9 | 1000 | message->get_exchange_type(message) == QUICK_MODE || |
9276f712 | 1001 | this->passive_tasks->get_count(this->passive_tasks) == 0) |
4a09d9ee MW |
1002 | { /* create tasks depending on request type, if not already some queued */ |
1003 | switch (message->get_exchange_type(message)) | |
1004 | { | |
1005 | case ID_PROT: | |
2e3c9f87 | 1006 | task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE); |
01685247 | 1007 | this->passive_tasks->insert_last(this->passive_tasks, task); |
824dc0ad | 1008 | task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE); |
8ad5cd1f | 1009 | this->passive_tasks->insert_last(this->passive_tasks, task); |
c73c832c MW |
1010 | task = (task_t *)main_mode_create(this->ike_sa, FALSE); |
1011 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
0aa2af5e | 1012 | task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE); |
c64a4b4f | 1013 | this->passive_tasks->insert_last(this->passive_tasks, task); |
79d6fc7f | 1014 | task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE); |
1cc4ec46 | 1015 | this->passive_tasks->insert_last(this->passive_tasks, task); |
4a09d9ee MW |
1016 | break; |
1017 | case AGGRESSIVE: | |
830ab2ae MW |
1018 | task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE); |
1019 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
1020 | task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE); | |
1021 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
1022 | task = (task_t *)aggressive_mode_create(this->ike_sa, FALSE); | |
1023 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
1024 | task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE); | |
1025 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
1026 | task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE); | |
1027 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
1028 | break; | |
4a09d9ee | 1029 | case QUICK_MODE: |
a22b9e4f MW |
1030 | if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED) |
1031 | { | |
1032 | DBG1(DBG_IKE, "received quick mode request for " | |
1033 | "unestablished IKE_SA, ignored"); | |
1034 | return FAILED; | |
1035 | } | |
4de361d9 TB |
1036 | if (have_quick_mode_task(this, message->get_message_id(message))) |
1037 | { | |
1038 | break; | |
1039 | } | |
744c0801 MW |
1040 | task = (task_t *)quick_mode_create(this->ike_sa, NULL, |
1041 | NULL, NULL); | |
1042 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
1043 | break; | |
4a09d9ee | 1044 | case INFORMATIONAL_V1: |
24742c0f | 1045 | if (process_dpd(this, message)) |
2ddd45c9 | 1046 | { |
ff6b084a | 1047 | dpd = TRUE; |
2ddd45c9 MW |
1048 | } |
1049 | else | |
1050 | { | |
1051 | task = (task_t *)informational_create(this->ike_sa, NULL); | |
24742c0f | 1052 | this->passive_tasks->insert_first(this->passive_tasks, task); |
2ddd45c9 | 1053 | } |
07abb470 | 1054 | break; |
c5dc9d33 | 1055 | case TRANSACTION: |
764035d5 | 1056 | if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING) |
156b8662 | 1057 | { |
e3311e9b MW |
1058 | task = (task_t *)mode_config_create(this->ike_sa, |
1059 | FALSE, TRUE); | |
156b8662 MW |
1060 | } |
1061 | else | |
1062 | { | |
1063 | task = (task_t *)xauth_create(this->ike_sa, FALSE); | |
1064 | } | |
c5dc9d33 CO |
1065 | this->passive_tasks->insert_last(this->passive_tasks, task); |
1066 | break; | |
4a09d9ee MW |
1067 | default: |
1068 | return FAILED; | |
1069 | } | |
1070 | } | |
24742c0f | 1071 | if (dpd) |
ff6b084a | 1072 | { |
24742c0f | 1073 | return initiate(this); |
ff6b084a | 1074 | } |
24742c0f MW |
1075 | this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, time_monotonic(NULL)); |
1076 | ||
4a09d9ee MW |
1077 | /* let the tasks process the message */ |
1078 | enumerator = this->passive_tasks->create_enumerator(this->passive_tasks); | |
1079 | while (enumerator->enumerate(enumerator, (void*)&task)) | |
1080 | { | |
1081 | switch (task->process(task, message)) | |
1082 | { | |
1083 | case SUCCESS: | |
1084 | /* task completed, remove it */ | |
1085 | this->passive_tasks->remove_at(this->passive_tasks, enumerator); | |
1086 | task->destroy(task); | |
590ca1d4 | 1087 | continue; |
4a09d9ee MW |
1088 | case NEED_MORE: |
1089 | /* processed, but task needs at least another call to build() */ | |
8cb6f4f9 | 1090 | send_response = TRUE; |
590ca1d4 MW |
1091 | continue; |
1092 | case ALREADY_DONE: | |
1093 | send_response = FALSE; | |
4a09d9ee | 1094 | break; |
37a22a16 TB |
1095 | case INVALID_ARG: |
1096 | if (task->get_type(task) == TASK_QUICK_MODE) | |
1097 | { /* not responsible for this exchange */ | |
1098 | continue; | |
1099 | } | |
1100 | /* FALL */ | |
4a09d9ee MW |
1101 | case FAILED: |
1102 | default: | |
1103 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); | |
1104 | /* FALL */ | |
1105 | case DESTROY_ME: | |
1106 | /* critical failure, destroy IKE_SA */ | |
1107 | this->passive_tasks->remove_at(this->passive_tasks, enumerator); | |
1108 | enumerator->destroy(enumerator); | |
1109 | task->destroy(task); | |
1110 | return DESTROY_ME; | |
1111 | } | |
590ca1d4 | 1112 | break; |
4a09d9ee MW |
1113 | } |
1114 | enumerator->destroy(enumerator); | |
1115 | ||
8cb6f4f9 TB |
1116 | if (send_response) |
1117 | { | |
69adeb5b MW |
1118 | if (build_response(this, message) != SUCCESS) |
1119 | { | |
1120 | return DESTROY_ME; | |
1121 | } | |
1122 | } | |
7b34de45 | 1123 | else |
8c732b8e TE |
1124 | { |
1125 | if (this->responding.retransmitted > 1) | |
1126 | { | |
1127 | packet_t *packet = NULL; | |
1128 | array_get(this->responding.packets, 0, &packet); | |
1129 | charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND_CLEARED, | |
1130 | packet); | |
1131 | } | |
1132 | /* We don't send a response, so don't retransmit one if we get | |
7b34de45 | 1133 | * the same message again. */ |
127a98dc | 1134 | clear_packets(this->responding.packets); |
7b34de45 | 1135 | } |
25863fe1 TB |
1136 | if (this->queued && |
1137 | this->queued->get_exchange_type(this->queued) == INFORMATIONAL_V1) | |
1138 | { | |
1139 | message_t *queued; | |
1140 | status_t status; | |
1141 | ||
1142 | queued = this->queued; | |
1143 | this->queued = NULL; | |
1144 | status = this->public.task_manager.process_message( | |
1145 | &this->public.task_manager, queued); | |
1146 | queued->destroy(queued); | |
1147 | if (status == DESTROY_ME) | |
1148 | { | |
1149 | return status; | |
1150 | } | |
1151 | } | |
69adeb5b MW |
1152 | if (this->passive_tasks->get_count(this->passive_tasks) == 0 && |
1153 | this->queued_tasks->get_count(this->queued_tasks) > 0) | |
1154 | { | |
1155 | /* passive tasks completed, check if an active task has been queued, | |
1156 | * such as XAUTH or modeconfig push */ | |
1157 | return initiate(this); | |
8cb6f4f9 TB |
1158 | } |
1159 | return SUCCESS; | |
4a09d9ee MW |
1160 | } |
1161 | ||
26b55dc6 MW |
1162 | /** |
1163 | * handle an incoming response message | |
1164 | */ | |
1165 | static status_t process_response(private_task_manager_t *this, | |
1166 | message_t *message) | |
1167 | { | |
1168 | enumerator_t *enumerator; | |
4e98ca18 | 1169 | message_t *queued; |
f91b6ac7 | 1170 | status_t status; |
26b55dc6 MW |
1171 | task_t *task; |
1172 | ||
1173 | if (message->get_exchange_type(message) != this->initiating.type) | |
1174 | { | |
0235914d MW |
1175 | /* Windows server sends a fourth quick mode message having an initial |
1176 | * contact notify. Ignore this message for compatibility. */ | |
1177 | if (this->initiating.type == EXCHANGE_TYPE_UNDEFINED && | |
1178 | message->get_exchange_type(message) == QUICK_MODE && | |
1179 | message->get_notify(message, INITIAL_CONTACT)) | |
1180 | { | |
1181 | DBG1(DBG_IKE, "ignoring fourth Quick Mode message"); | |
1182 | return SUCCESS; | |
1183 | } | |
26b55dc6 MW |
1184 | DBG1(DBG_IKE, "received %N response, but expected %N", |
1185 | exchange_type_names, message->get_exchange_type(message), | |
1186 | exchange_type_names, this->initiating.type); | |
1187 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); | |
1188 | return DESTROY_ME; | |
1189 | } | |
1190 | ||
1191 | enumerator = this->active_tasks->create_enumerator(this->active_tasks); | |
1192 | while (enumerator->enumerate(enumerator, (void*)&task)) | |
1193 | { | |
1194 | switch (task->process(task, message)) | |
1195 | { | |
1196 | case SUCCESS: | |
1197 | /* task completed, remove it */ | |
1198 | this->active_tasks->remove_at(this->active_tasks, enumerator); | |
1199 | task->destroy(task); | |
590ca1d4 | 1200 | continue; |
26b55dc6 MW |
1201 | case NEED_MORE: |
1202 | /* processed, but task needs another exchange */ | |
590ca1d4 MW |
1203 | continue; |
1204 | case ALREADY_DONE: | |
26b55dc6 MW |
1205 | break; |
1206 | case FAILED: | |
1207 | default: | |
1208 | charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE); | |
1209 | /* FALL */ | |
1210 | case DESTROY_ME: | |
1211 | /* critical failure, destroy IKE_SA */ | |
1212 | this->active_tasks->remove_at(this->active_tasks, enumerator); | |
1213 | enumerator->destroy(enumerator); | |
1214 | task->destroy(task); | |
1215 | return DESTROY_ME; | |
1216 | } | |
590ca1d4 | 1217 | break; |
26b55dc6 MW |
1218 | } |
1219 | enumerator->destroy(enumerator); | |
1220 | ||
0ddda7ca | 1221 | if (this->initiating.retransmitted > 1) |
bd71ba0f TB |
1222 | { |
1223 | packet_t *packet = NULL; | |
1224 | array_get(this->initiating.packets, 0, &packet); | |
1225 | charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND_CLEARED, packet); | |
1226 | } | |
26b55dc6 | 1227 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; |
127a98dc | 1228 | clear_packets(this->initiating.packets); |
26b55dc6 | 1229 | |
3805880c TB |
1230 | if (this->queued && !this->active_tasks->get_count(this->active_tasks) && |
1231 | this->queued->get_exchange_type(this->queued) == TRANSACTION) | |
f91b6ac7 | 1232 | { |
4e98ca18 | 1233 | queued = this->queued; |
f91b6ac7 | 1234 | this->queued = NULL; |
4e98ca18 MW |
1235 | status = this->public.task_manager.process_message( |
1236 | &this->public.task_manager, queued); | |
1237 | queued->destroy(queued); | |
f91b6ac7 MW |
1238 | if (status == DESTROY_ME) |
1239 | { | |
1240 | return status; | |
1241 | } | |
1242 | } | |
1243 | ||
26b55dc6 MW |
1244 | return initiate(this); |
1245 | } | |
1246 | ||
30216000 TB |
1247 | static status_t handle_fragment(private_task_manager_t *this, message_t *msg) |
1248 | { | |
70e0be07 | 1249 | status_t status; |
30216000 | 1250 | |
70e0be07 | 1251 | if (!this->defrag) |
30216000 | 1252 | { |
70e0be07 TB |
1253 | this->defrag = message_create_defrag(msg); |
1254 | if (!this->defrag) | |
30216000 | 1255 | { |
70e0be07 | 1256 | return FAILED; |
30216000 TB |
1257 | } |
1258 | } | |
70e0be07 TB |
1259 | status = this->defrag->add_fragment(this->defrag, msg); |
1260 | if (status == SUCCESS) | |
8fc83834 | 1261 | { |
70e0be07 TB |
1262 | lib->processor->queue_job(lib->processor, |
1263 | (job_t*)process_message_job_create(this->defrag)); | |
1264 | this->defrag = NULL; | |
1265 | /* do not process the last fragment */ | |
30216000 TB |
1266 | status = NEED_MORE; |
1267 | } | |
1268 | return status; | |
1269 | } | |
1270 | ||
b235e69c TB |
1271 | /** |
1272 | * Parse the given message and verify that it is valid. | |
1273 | */ | |
1274 | static status_t parse_message(private_task_manager_t *this, message_t *msg) | |
1275 | { | |
1276 | status_t status; | |
1277 | ||
1278 | status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa)); | |
1279 | ||
1280 | if (status != SUCCESS) | |
1281 | { | |
1282 | switch (status) | |
1283 | { | |
29a5e070 TB |
1284 | case NOT_SUPPORTED: |
1285 | DBG1(DBG_IKE, "unsupported exchange type"); | |
b4705269 | 1286 | send_notify(this, msg, INVALID_EXCHANGE_TYPE); |
29a5e070 | 1287 | break; |
b235e69c TB |
1288 | case PARSE_ERROR: |
1289 | DBG1(DBG_IKE, "message parsing failed"); | |
b4705269 | 1290 | send_notify(this, msg, PAYLOAD_MALFORMED); |
b235e69c TB |
1291 | break; |
1292 | case VERIFY_ERROR: | |
1293 | DBG1(DBG_IKE, "message verification failed"); | |
b4705269 | 1294 | send_notify(this, msg, PAYLOAD_MALFORMED); |
b235e69c TB |
1295 | break; |
1296 | case FAILED: | |
1297 | DBG1(DBG_IKE, "integrity check failed"); | |
b4705269 | 1298 | send_notify(this, msg, INVALID_HASH_INFORMATION); |
b235e69c TB |
1299 | break; |
1300 | case INVALID_STATE: | |
1301 | DBG1(DBG_IKE, "found encrypted message, but no keys available"); | |
b4705269 | 1302 | send_notify(this, msg, PAYLOAD_MALFORMED); |
b235e69c TB |
1303 | default: |
1304 | break; | |
1305 | } | |
f5ef3577 | 1306 | DBG1(DBG_IKE, "%N %s with message ID %u processing failed", |
b235e69c TB |
1307 | exchange_type_names, msg->get_exchange_type(msg), |
1308 | msg->get_request(msg) ? "request" : "response", | |
1309 | msg->get_message_id(msg)); | |
1310 | ||
418f4bc7 MW |
1311 | charon->bus->alert(charon->bus, ALERT_PARSE_ERROR_BODY, msg, status); |
1312 | ||
b235e69c TB |
1313 | if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED) |
1314 | { /* invalid initiation attempt, close SA */ | |
1315 | return DESTROY_ME; | |
1316 | } | |
1317 | } | |
30216000 | 1318 | |
3ecfc83c | 1319 | if (msg->get_first_payload_type(msg) == PLV1_FRAGMENT) |
30216000 TB |
1320 | { |
1321 | return handle_fragment(this, msg); | |
1322 | } | |
b235e69c TB |
1323 | return status; |
1324 | } | |
1325 | ||
3f56f731 TB |
1326 | /** |
1327 | * Queue the given message if possible | |
1328 | */ | |
1329 | static status_t queue_message(private_task_manager_t *this, message_t *msg) | |
1330 | { | |
1331 | if (this->queued) | |
1332 | { | |
1333 | DBG1(DBG_IKE, "ignoring %N request, queue full", | |
1334 | exchange_type_names, msg->get_exchange_type(msg)); | |
1335 | return FAILED; | |
1336 | } | |
1337 | this->queued = message_create_from_packet(msg->get_packet(msg)); | |
1338 | if (this->queued->parse_header(this->queued) != SUCCESS) | |
1339 | { | |
1340 | this->queued->destroy(this->queued); | |
1341 | this->queued = NULL; | |
1342 | return FAILED; | |
1343 | } | |
1344 | DBG1(DBG_IKE, "queueing %N request as tasks still active", | |
1345 | exchange_type_names, msg->get_exchange_type(msg)); | |
1346 | return SUCCESS; | |
1347 | } | |
1348 | ||
4a09d9ee MW |
1349 | METHOD(task_manager_t, process_message, status_t, |
1350 | private_task_manager_t *this, message_t *msg) | |
1351 | { | |
b12c53ce | 1352 | uint32_t hash, mid, i; |
3d59c5c3 | 1353 | host_t *me, *other; |
68c6863b | 1354 | status_t status; |
73aaf76b | 1355 | |
1960312c | 1356 | /* TODO-IKEv1: update hosts more selectively */ |
3d59c5c3 TB |
1357 | me = msg->get_destination(msg); |
1358 | other = msg->get_source(msg); | |
68c6863b | 1359 | mid = msg->get_message_id(msg); |
fce566a8 MW |
1360 | hash = chunk_hash(msg->get_packet_data(msg)); |
1361 | for (i = 0; i < MAX_OLD_HASHES; i++) | |
1362 | { | |
1363 | if (this->initiating.old_hashes[i] == hash) | |
1364 | { | |
127a98dc | 1365 | if (array_count(this->initiating.packets) && |
f5a84055 | 1366 | i == (this->initiating.old_hash_pos % MAX_OLD_HASHES) && |
346dad30 MW |
1367 | (msg->get_exchange_type(msg) == QUICK_MODE || |
1368 | msg->get_exchange_type(msg) == AGGRESSIVE)) | |
f5a84055 MW |
1369 | { |
1370 | DBG1(DBG_IKE, "received retransmit of response with ID %u, " | |
1371 | "resending last request", mid); | |
127a98dc | 1372 | send_packets(this, this->initiating.packets); |
f5a84055 MW |
1373 | return SUCCESS; |
1374 | } | |
fce566a8 MW |
1375 | DBG1(DBG_IKE, "received retransmit of response with ID %u, " |
1376 | "but next request already sent", mid); | |
1377 | return SUCCESS; | |
1378 | } | |
1379 | } | |
1960312c | 1380 | |
2ddd45c9 | 1381 | if ((mid && mid == this->initiating.mid) || |
73aaf76b | 1382 | (this->initiating.mid == 0 && |
1cc3e92c | 1383 | msg->get_exchange_type(msg) == this->initiating.type && |
dc8e9647 | 1384 | this->active_tasks->get_count(this->active_tasks))) |
4a09d9ee | 1385 | { |
7519106d | 1386 | msg->set_request(msg, FALSE); |
47b8f6ef | 1387 | charon->bus->message(charon->bus, msg, TRUE, FALSE); |
b235e69c | 1388 | status = parse_message(this, msg); |
30216000 TB |
1389 | if (status == NEED_MORE) |
1390 | { | |
1391 | return SUCCESS; | |
1392 | } | |
1960312c TB |
1393 | if (status != SUCCESS) |
1394 | { | |
1395 | return status; | |
1396 | } | |
1397 | this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, | |
1398 | time_monotonic(NULL)); | |
3d59c5c3 | 1399 | this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE); |
47b8f6ef | 1400 | charon->bus->message(charon->bus, msg, TRUE, TRUE); |
73aaf76b | 1401 | if (process_response(this, msg) != SUCCESS) |
4a09d9ee MW |
1402 | { |
1403 | flush(this); | |
1404 | return DESTROY_ME; | |
1405 | } | |
f5a84055 | 1406 | this->initiating.old_hashes[(++this->initiating.old_hash_pos) % |
fce566a8 | 1407 | MAX_OLD_HASHES] = hash; |
4a09d9ee MW |
1408 | } |
1409 | else | |
1410 | { | |
7b34de45 | 1411 | if (hash == this->responding.hash) |
73aaf76b | 1412 | { |
127a98dc | 1413 | if (array_count(this->responding.packets)) |
7b34de45 MW |
1414 | { |
1415 | DBG1(DBG_IKE, "received retransmit of request with ID %u, " | |
1416 | "retransmitting response", mid); | |
127a98dc | 1417 | send_packets(this, this->responding.packets); |
7b34de45 | 1418 | } |
127a98dc | 1419 | else if (array_count(this->initiating.packets) && |
1b82eb23 MW |
1420 | this->initiating.type == INFORMATIONAL_V1) |
1421 | { | |
1422 | DBG1(DBG_IKE, "received retransmit of DPD request, " | |
1423 | "retransmitting response"); | |
127a98dc | 1424 | send_packets(this, this->initiating.packets); |
1b82eb23 | 1425 | } |
7b34de45 MW |
1426 | else |
1427 | { | |
1428 | DBG1(DBG_IKE, "received retransmit of request with ID %u, " | |
1429 | "but no response to retransmit", mid); | |
1430 | } | |
3c79b7b7 | 1431 | charon->bus->alert(charon->bus, ALERT_RETRANSMIT_RECEIVE, msg); |
73aaf76b MW |
1432 | return SUCCESS; |
1433 | } | |
cdf75a39 | 1434 | |
cf95d292 | 1435 | /* reject Main/Aggressive Modes once established */ |
cdf75a39 MW |
1436 | if (msg->get_exchange_type(msg) == ID_PROT || |
1437 | msg->get_exchange_type(msg) == AGGRESSIVE) | |
1438 | { | |
1439 | if (this->ike_sa->get_state(this->ike_sa) != IKE_CREATED && | |
1440 | this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING && | |
3ecfc83c | 1441 | msg->get_first_payload_type(msg) != PLV1_FRAGMENT) |
cdf75a39 MW |
1442 | { |
1443 | DBG1(DBG_IKE, "ignoring %N in established IKE_SA state", | |
1444 | exchange_type_names, msg->get_exchange_type(msg)); | |
1445 | return FAILED; | |
1446 | } | |
1447 | } | |
1448 | ||
b0b0c913 | 1449 | /* drop XAuth/Mode Config/Quick Mode messages until we received the last |
1c8dfa30 TB |
1450 | * Aggressive Mode message. since Informational messages are not |
1451 | * retransmitted we queue them. */ | |
1b4e7fe1 | 1452 | if (have_task_queued(this, TASK_AGGRESSIVE_MODE)) |
b0b0c913 | 1453 | { |
1c8dfa30 TB |
1454 | if (msg->get_exchange_type(msg) == INFORMATIONAL_V1) |
1455 | { | |
1456 | return queue_message(this, msg); | |
1457 | } | |
1458 | else if (msg->get_exchange_type(msg) != AGGRESSIVE) | |
1459 | { | |
1460 | DBG1(DBG_IKE, "ignoring %N request while phase 1 is incomplete", | |
1461 | exchange_type_names, msg->get_exchange_type(msg)); | |
1462 | return FAILED; | |
1463 | } | |
b0b0c913 TB |
1464 | } |
1465 | ||
3f56f731 TB |
1466 | /* queue XAuth/Mode Config messages unless the Main Mode exchange we |
1467 | * initiated is complete */ | |
f91b6ac7 | 1468 | if (msg->get_exchange_type(msg) == TRANSACTION && |
0b0191e1 | 1469 | this->active_tasks->get_count(this->active_tasks)) |
3f56f731 TB |
1470 | { |
1471 | return queue_message(this, msg); | |
f91b6ac7 MW |
1472 | } |
1473 | ||
1b4e7fe1 TB |
1474 | /* some peers send INITIAL_CONTACT notifies during XAuth, cache it */ |
1475 | if (have_task_queued(this, TASK_XAUTH) && | |
1476 | msg->get_exchange_type(msg) == INFORMATIONAL_V1) | |
1477 | { | |
1478 | return queue_message(this, msg); | |
1479 | } | |
1480 | ||
7519106d | 1481 | msg->set_request(msg, TRUE); |
47b8f6ef | 1482 | charon->bus->message(charon->bus, msg, TRUE, FALSE); |
b235e69c | 1483 | status = parse_message(this, msg); |
30216000 TB |
1484 | if (status == NEED_MORE) |
1485 | { | |
1486 | return SUCCESS; | |
1487 | } | |
1960312c TB |
1488 | if (status != SUCCESS) |
1489 | { | |
1490 | return status; | |
1491 | } | |
1492 | /* if this IKE_SA is virgin, we check for a config */ | |
1493 | if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL) | |
1494 | { | |
1495 | ike_sa_id_t *ike_sa_id; | |
1496 | ike_cfg_t *ike_cfg; | |
1497 | job_t *job; | |
f5a84055 | 1498 | |
79101163 MW |
1499 | ike_cfg = charon->backends->get_ike_cfg(charon->backends, |
1500 | me, other, IKEV1); | |
1960312c TB |
1501 | if (ike_cfg == NULL) |
1502 | { | |
1503 | /* no config found for these hosts, destroy */ | |
4cfd0db8 TB |
1504 | DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N", |
1505 | me, other, notify_type_names, NO_PROPOSAL_CHOSEN); | |
b4705269 | 1506 | send_notify(this, msg, NO_PROPOSAL_CHOSEN); |
1960312c TB |
1507 | return DESTROY_ME; |
1508 | } | |
1509 | this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg); | |
1510 | ike_cfg->destroy(ike_cfg); | |
1511 | /* add a timeout if peer does not establish it completely */ | |
1512 | ike_sa_id = this->ike_sa->get_id(this->ike_sa); | |
1513 | job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE); | |
1514 | lib->scheduler->schedule_job(lib->scheduler, job, | |
1515 | lib->settings->get_int(lib->settings, | |
42500c27 | 1516 | "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT, |
d223fe80 | 1517 | lib->ns)); |
1960312c | 1518 | } |
3d59c5c3 | 1519 | this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE); |
47b8f6ef | 1520 | charon->bus->message(charon->bus, msg, TRUE, TRUE); |
73aaf76b | 1521 | if (process_request(this, msg) != SUCCESS) |
26b55dc6 MW |
1522 | { |
1523 | flush(this); | |
1524 | return DESTROY_ME; | |
1525 | } | |
9cc8bd4f | 1526 | this->responding.hash = hash; |
4a09d9ee MW |
1527 | } |
1528 | return SUCCESS; | |
1529 | } | |
1530 | ||
8ed976c0 MW |
1531 | /** |
1532 | * Check if a given task has been queued already | |
1533 | */ | |
1534 | static bool has_queued(private_task_manager_t *this, task_type_t type) | |
1535 | { | |
1536 | enumerator_t *enumerator; | |
1537 | bool found = FALSE; | |
1538 | task_t *task; | |
1539 | ||
1540 | enumerator = this->queued_tasks->create_enumerator(this->queued_tasks); | |
1541 | while (enumerator->enumerate(enumerator, &task)) | |
1542 | { | |
1543 | if (task->get_type(task) == type) | |
1544 | { | |
1545 | found = TRUE; | |
1546 | break; | |
1547 | } | |
1548 | } | |
1549 | enumerator->destroy(enumerator); | |
1550 | return found; | |
1551 | } | |
1552 | ||
208678e6 TB |
1553 | METHOD(task_manager_t, queue_task_delayed, void, |
1554 | private_task_manager_t *this, task_t *task, uint32_t delay) | |
d4828f51 TB |
1555 | { |
1556 | task_type_t type = task->get_type(task); | |
1557 | ||
1558 | switch (type) | |
1559 | { | |
1560 | case TASK_MODE_CONFIG: | |
1561 | case TASK_XAUTH: | |
1562 | if (has_queued(this, type)) | |
1563 | { | |
1564 | task->destroy(task); | |
1565 | return; | |
1566 | } | |
1567 | break; | |
1568 | default: | |
1569 | break; | |
1570 | } | |
1571 | DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task)); | |
1572 | this->queued_tasks->insert_last(this->queued_tasks, task); | |
1573 | } | |
1574 | ||
208678e6 TB |
1575 | METHOD(task_manager_t, queue_task, void, |
1576 | private_task_manager_t *this, task_t *task) | |
1577 | { | |
1578 | queue_task_delayed(this, task, 0); | |
1579 | } | |
1580 | ||
a60daa07 MW |
1581 | METHOD(task_manager_t, queue_ike, void, |
1582 | private_task_manager_t *this) | |
1583 | { | |
830ab2ae MW |
1584 | peer_cfg_t *peer_cfg; |
1585 | ||
8ed976c0 MW |
1586 | if (!has_queued(this, TASK_ISAKMP_VENDOR)) |
1587 | { | |
1588 | queue_task(this, (task_t*)isakmp_vendor_create(this->ike_sa, TRUE)); | |
1589 | } | |
1590 | if (!has_queued(this, TASK_ISAKMP_CERT_PRE)) | |
1591 | { | |
1592 | queue_task(this, (task_t*)isakmp_cert_pre_create(this->ike_sa, TRUE)); | |
1593 | } | |
830ab2ae MW |
1594 | peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa); |
1595 | if (peer_cfg->use_aggressive(peer_cfg)) | |
8ed976c0 | 1596 | { |
830ab2ae MW |
1597 | if (!has_queued(this, TASK_AGGRESSIVE_MODE)) |
1598 | { | |
1599 | queue_task(this, (task_t*)aggressive_mode_create(this->ike_sa, TRUE)); | |
1600 | } | |
1601 | } | |
1602 | else | |
1603 | { | |
1604 | if (!has_queued(this, TASK_MAIN_MODE)) | |
1605 | { | |
1606 | queue_task(this, (task_t*)main_mode_create(this->ike_sa, TRUE)); | |
1607 | } | |
8ed976c0 MW |
1608 | } |
1609 | if (!has_queued(this, TASK_ISAKMP_CERT_POST)) | |
1610 | { | |
1611 | queue_task(this, (task_t*)isakmp_cert_post_create(this->ike_sa, TRUE)); | |
1612 | } | |
1613 | if (!has_queued(this, TASK_ISAKMP_NATD)) | |
1614 | { | |
1615 | queue_task(this, (task_t*)isakmp_natd_create(this->ike_sa, TRUE)); | |
1616 | } | |
a60daa07 MW |
1617 | } |
1618 | ||
4f49b068 | 1619 | METHOD(task_manager_t, queue_ike_reauth, void, |
dab60d64 MW |
1620 | private_task_manager_t *this) |
1621 | { | |
4f49b068 MW |
1622 | enumerator_t *enumerator; |
1623 | child_sa_t *child_sa; | |
1624 | ike_sa_t *new; | |
1625 | host_t *host; | |
1626 | ||
1627 | new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager, | |
1628 | this->ike_sa->get_version(this->ike_sa), TRUE); | |
1629 | if (!new) | |
1630 | { /* shouldn't happen */ | |
1631 | return; | |
1632 | } | |
1633 | ||
1634 | new->set_peer_cfg(new, this->ike_sa->get_peer_cfg(this->ike_sa)); | |
1635 | host = this->ike_sa->get_other_host(this->ike_sa); | |
1636 | new->set_other_host(new, host->clone(host)); | |
1637 | host = this->ike_sa->get_my_host(this->ike_sa); | |
1638 | new->set_my_host(new, host->clone(host)); | |
101d26ba MW |
1639 | enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa, TRUE); |
1640 | while (enumerator->enumerate(enumerator, &host)) | |
4f49b068 | 1641 | { |
101d26ba | 1642 | new->add_virtual_ip(new, TRUE, host); |
4f49b068 | 1643 | } |
101d26ba | 1644 | enumerator->destroy(enumerator); |
4f49b068 | 1645 | |
6a9a69ae TB |
1646 | charon->bus->children_migrate(charon->bus, new->get_id(new), |
1647 | new->get_unique_id(new)); | |
4f49b068 MW |
1648 | enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa); |
1649 | while (enumerator->enumerate(enumerator, &child_sa)) | |
1650 | { | |
1651 | this->ike_sa->remove_child_sa(this->ike_sa, enumerator); | |
1652 | new->add_child_sa(new, child_sa); | |
1653 | } | |
1654 | enumerator->destroy(enumerator); | |
6a9a69ae TB |
1655 | charon->bus->set_sa(charon->bus, new); |
1656 | charon->bus->children_migrate(charon->bus, NULL, 0); | |
1657 | charon->bus->set_sa(charon->bus, this->ike_sa); | |
4f49b068 | 1658 | |
8254e7ec TB |
1659 | if (!new->get_child_count(new)) |
1660 | { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */ | |
1661 | task_t *task; | |
1662 | ||
1663 | enumerator = this->queued_tasks->create_enumerator(this->queued_tasks); | |
1664 | while (enumerator->enumerate(enumerator, &task)) | |
1665 | { | |
1666 | if (task->get_type(task) == TASK_QUICK_MODE) | |
1667 | { | |
1668 | this->queued_tasks->remove_at(this->queued_tasks, enumerator); | |
1669 | task->migrate(task, new); | |
1670 | new->queue_task(new, task); | |
1671 | } | |
1672 | } | |
1673 | enumerator->destroy(enumerator); | |
1674 | } | |
1675 | ||
4f49b068 MW |
1676 | if (new->initiate(new, NULL, 0, NULL, NULL) != DESTROY_ME) |
1677 | { | |
1678 | charon->ike_sa_manager->checkin(charon->ike_sa_manager, new); | |
1b79299b | 1679 | this->ike_sa->set_state(this->ike_sa, IKE_REKEYING); |
4f49b068 MW |
1680 | } |
1681 | else | |
1682 | { | |
1683 | charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new); | |
1684 | DBG1(DBG_IKE, "reauthenticating IKE_SA failed"); | |
1685 | } | |
1686 | charon->bus->set_sa(charon->bus, this->ike_sa); | |
dab60d64 MW |
1687 | } |
1688 | ||
4f49b068 | 1689 | METHOD(task_manager_t, queue_ike_rekey, void, |
cedb412e MW |
1690 | private_task_manager_t *this) |
1691 | { | |
4f49b068 | 1692 | queue_ike_reauth(this); |
cedb412e MW |
1693 | } |
1694 | ||
3ed148b3 MW |
1695 | METHOD(task_manager_t, queue_ike_delete, void, |
1696 | private_task_manager_t *this) | |
1697 | { | |
daee47ba MW |
1698 | enumerator_t *enumerator; |
1699 | child_sa_t *child_sa; | |
1700 | ||
60d0f52f TB |
1701 | /* cancel any currently active task to get the DELETE done quickly */ |
1702 | flush_queue(this, TASK_QUEUE_ACTIVE); | |
1703 | ||
daee47ba MW |
1704 | enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa); |
1705 | while (enumerator->enumerate(enumerator, &child_sa)) | |
1706 | { | |
1707 | queue_task(this, (task_t*) | |
1708 | quick_delete_create(this->ike_sa, child_sa->get_protocol(child_sa), | |
3a925f74 | 1709 | child_sa->get_spi(child_sa, TRUE), FALSE, FALSE)); |
daee47ba MW |
1710 | } |
1711 | enumerator->destroy(enumerator); | |
1712 | ||
3ed148b3 MW |
1713 | queue_task(this, (task_t*)isakmp_delete_create(this->ike_sa, TRUE)); |
1714 | } | |
1715 | ||
873df908 MW |
1716 | METHOD(task_manager_t, queue_mobike, void, |
1717 | private_task_manager_t *this, bool roam, bool address) | |
1718 | { | |
1719 | /* Not supported in IKEv1 */ | |
1720 | } | |
1721 | ||
fe43d9a2 | 1722 | METHOD(task_manager_t, queue_child, void, |
b12c53ce | 1723 | private_task_manager_t *this, child_cfg_t *cfg, uint32_t reqid, |
fe43d9a2 MW |
1724 | traffic_selector_t *tsi, traffic_selector_t *tsr) |
1725 | { | |
14dc7941 MW |
1726 | quick_mode_t *task; |
1727 | ||
1728 | task = quick_mode_create(this->ike_sa, cfg, tsi, tsr); | |
1729 | task->use_reqid(task, reqid); | |
1730 | ||
1731 | queue_task(this, &task->task); | |
fe43d9a2 MW |
1732 | } |
1733 | ||
2d4c347a MW |
1734 | /** |
1735 | * Check if two CHILD_SAs have the same traffic selector | |
1736 | */ | |
553bb787 | 1737 | static bool have_equal_ts(child_sa_t *child1, child_sa_t *child2, bool local) |
2d4c347a | 1738 | { |
553bb787 MW |
1739 | enumerator_t *e1, *e2; |
1740 | traffic_selector_t *ts1, *ts2; | |
1741 | bool equal = FALSE; | |
2d4c347a | 1742 | |
553bb787 MW |
1743 | e1 = child1->create_ts_enumerator(child1, local); |
1744 | e2 = child2->create_ts_enumerator(child2, local); | |
1745 | if (e1->enumerate(e1, &ts1) && e2->enumerate(e2, &ts2)) | |
2d4c347a | 1746 | { |
553bb787 | 1747 | equal = ts1->equals(ts1, ts2); |
2d4c347a | 1748 | } |
fafa7684 | 1749 | e2->destroy(e2); |
553bb787 MW |
1750 | e1->destroy(e1); |
1751 | ||
1752 | return equal; | |
2d4c347a MW |
1753 | } |
1754 | ||
1e24fa46 MW |
1755 | /** |
1756 | * Check if a CHILD_SA is redundant and we should delete instead of rekey | |
1757 | */ | |
1758 | static bool is_redundant(private_task_manager_t *this, child_sa_t *child_sa) | |
1759 | { | |
1760 | enumerator_t *enumerator; | |
1761 | child_sa_t *current; | |
1762 | bool redundant = FALSE; | |
1763 | ||
1764 | enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa); | |
1765 | while (enumerator->enumerate(enumerator, ¤t)) | |
1766 | { | |
1767 | if (current->get_state(current) == CHILD_INSTALLED && | |
1768 | streq(current->get_name(current), child_sa->get_name(child_sa)) && | |
2d4c347a MW |
1769 | have_equal_ts(current, child_sa, TRUE) && |
1770 | have_equal_ts(current, child_sa, FALSE) && | |
1e24fa46 MW |
1771 | current->get_lifetime(current, FALSE) > |
1772 | child_sa->get_lifetime(child_sa, FALSE)) | |
1773 | { | |
1774 | DBG1(DBG_IKE, "deleting redundant CHILD_SA %s{%d}", | |
246c969d MW |
1775 | child_sa->get_name(child_sa), |
1776 | child_sa->get_unique_id(child_sa)); | |
1e24fa46 MW |
1777 | redundant = TRUE; |
1778 | break; | |
1779 | } | |
1780 | } | |
1781 | enumerator->destroy(enumerator); | |
1782 | ||
1783 | return redundant; | |
1784 | } | |
1785 | ||
7a5f372c MW |
1786 | /** |
1787 | * Get the first traffic selector of a CHILD_SA, local or remote | |
1788 | */ | |
1789 | static traffic_selector_t* get_first_ts(child_sa_t *child_sa, bool local) | |
1790 | { | |
1791 | traffic_selector_t *ts = NULL; | |
553bb787 | 1792 | enumerator_t *enumerator; |
7a5f372c | 1793 | |
553bb787 MW |
1794 | enumerator = child_sa->create_ts_enumerator(child_sa, local); |
1795 | enumerator->enumerate(enumerator, &ts); | |
1796 | enumerator->destroy(enumerator); | |
1797 | ||
1798 | return ts; | |
7a5f372c MW |
1799 | } |
1800 | ||
463a73cc | 1801 | METHOD(task_manager_t, queue_child_rekey, void, |
b12c53ce | 1802 | private_task_manager_t *this, protocol_id_t protocol, uint32_t spi) |
463a73cc | 1803 | { |
23eb447c MW |
1804 | child_sa_t *child_sa; |
1805 | child_cfg_t *cfg; | |
1806 | quick_mode_t *task; | |
1807 | ||
1808 | child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE); | |
1809 | if (!child_sa) | |
1810 | { | |
1811 | child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE); | |
1812 | } | |
1813 | if (child_sa && child_sa->get_state(child_sa) == CHILD_INSTALLED) | |
1814 | { | |
1e24fa46 MW |
1815 | if (is_redundant(this, child_sa)) |
1816 | { | |
a01eb5e4 | 1817 | child_sa->set_state(child_sa, CHILD_REKEYED); |
083208e8 TB |
1818 | if (lib->settings->get_bool(lib->settings, "%s.delete_rekeyed", |
1819 | FALSE, lib->ns)) | |
1820 | { | |
1821 | queue_task(this, (task_t*)quick_delete_create(this->ike_sa, | |
1e24fa46 | 1822 | protocol, spi, FALSE, FALSE)); |
083208e8 | 1823 | } |
1e24fa46 MW |
1824 | } |
1825 | else | |
1826 | { | |
1827 | child_sa->set_state(child_sa, CHILD_REKEYING); | |
1828 | cfg = child_sa->get_config(child_sa); | |
7a5f372c MW |
1829 | task = quick_mode_create(this->ike_sa, cfg->get_ref(cfg), |
1830 | get_first_ts(child_sa, TRUE), get_first_ts(child_sa, FALSE)); | |
1e24fa46 | 1831 | task->use_reqid(task, child_sa->get_reqid(child_sa)); |
85b23888 MW |
1832 | task->use_marks(task, child_sa->get_mark(child_sa, TRUE).value, |
1833 | child_sa->get_mark(child_sa, FALSE).value); | |
1e24fa46 | 1834 | task->rekey(task, child_sa->get_spi(child_sa, TRUE)); |
23eb447c | 1835 | |
1e24fa46 MW |
1836 | queue_task(this, &task->task); |
1837 | } | |
23eb447c | 1838 | } |
463a73cc MW |
1839 | } |
1840 | ||
83c5fda0 | 1841 | METHOD(task_manager_t, queue_child_delete, void, |
b12c53ce | 1842 | private_task_manager_t *this, protocol_id_t protocol, uint32_t spi, |
3a925f74 | 1843 | bool expired) |
83c5fda0 MW |
1844 | { |
1845 | queue_task(this, (task_t*)quick_delete_create(this->ike_sa, protocol, | |
3a925f74 | 1846 | spi, FALSE, expired)); |
83c5fda0 MW |
1847 | } |
1848 | ||
244d715d MW |
1849 | METHOD(task_manager_t, queue_dpd, void, |
1850 | private_task_manager_t *this) | |
1851 | { | |
80c5b17d | 1852 | peer_cfg_t *peer_cfg; |
b12c53ce | 1853 | uint32_t t, retransmit; |
4b38c22c | 1854 | |
24742c0f | 1855 | queue_task(this, (task_t*)isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE, |
9de3140d | 1856 | this->dpd_send)); |
80c5b17d | 1857 | peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa); |
4b38c22c | 1858 | |
80c5b17d AS |
1859 | /* compute timeout in milliseconds */ |
1860 | t = 1000 * peer_cfg->get_dpd_timeout(peer_cfg); | |
1861 | if (t == 0) | |
4b38c22c | 1862 | { |
80c5b17d AS |
1863 | /* use the same timeout as a retransmitting IKE message would have */ |
1864 | for (retransmit = 0; retransmit <= this->retransmit_tries; retransmit++) | |
1865 | { | |
b12c53ce | 1866 | t += (uint32_t)(this->retransmit_timeout * 1000.0 * |
80c5b17d AS |
1867 | pow(this->retransmit_base, retransmit)); |
1868 | } | |
4b38c22c | 1869 | } |
747b6487 TB |
1870 | /* compensate for the already elapsed dpd delay */ |
1871 | t -= 1000 * peer_cfg->get_dpd(peer_cfg); | |
80c5b17d AS |
1872 | |
1873 | /* schedule DPD timeout job */ | |
4b38c22c MW |
1874 | lib->scheduler->schedule_job_ms(lib->scheduler, |
1875 | (job_t*)dpd_timeout_job_create(this->ike_sa->get_id(this->ike_sa)), t); | |
244d715d MW |
1876 | } |
1877 | ||
4a09d9ee MW |
1878 | METHOD(task_manager_t, adopt_tasks, void, |
1879 | private_task_manager_t *this, task_manager_t *other_public) | |
1880 | { | |
1881 | private_task_manager_t *other = (private_task_manager_t*)other_public; | |
1882 | task_t *task; | |
1883 | ||
1884 | /* move queued tasks from other to this */ | |
1885 | while (other->queued_tasks->remove_last(other->queued_tasks, | |
1886 | (void**)&task) == SUCCESS) | |
1887 | { | |
1888 | DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task)); | |
1889 | task->migrate(task, this->ike_sa); | |
1890 | this->queued_tasks->insert_first(this->queued_tasks, task); | |
1891 | } | |
1892 | } | |
1893 | ||
68db844f TB |
1894 | /** |
1895 | * Migrates child-creating tasks from src to dst | |
1896 | */ | |
1897 | static void migrate_child_tasks(private_task_manager_t *this, | |
1898 | linked_list_t *src, linked_list_t *dst) | |
1899 | { | |
1900 | enumerator_t *enumerator; | |
1901 | task_t *task; | |
1902 | ||
1903 | enumerator = src->create_enumerator(src); | |
1904 | while (enumerator->enumerate(enumerator, &task)) | |
1905 | { | |
1906 | if (task->get_type(task) == TASK_QUICK_MODE) | |
1907 | { | |
1908 | src->remove_at(src, enumerator); | |
1909 | task->migrate(task, this->ike_sa); | |
1910 | dst->insert_last(dst, task); | |
1911 | } | |
1912 | } | |
1913 | enumerator->destroy(enumerator); | |
1914 | } | |
1915 | ||
1916 | METHOD(task_manager_t, adopt_child_tasks, void, | |
1917 | private_task_manager_t *this, task_manager_t *other_public) | |
1918 | { | |
1919 | private_task_manager_t *other = (private_task_manager_t*)other_public; | |
1920 | ||
1921 | /* move active child tasks from other to this */ | |
1922 | migrate_child_tasks(this, other->active_tasks, this->queued_tasks); | |
1923 | /* do the same for queued tasks */ | |
1924 | migrate_child_tasks(this, other->queued_tasks, this->queued_tasks); | |
1925 | } | |
1926 | ||
4a09d9ee MW |
1927 | METHOD(task_manager_t, busy, bool, |
1928 | private_task_manager_t *this) | |
1929 | { | |
1930 | return (this->active_tasks->get_count(this->active_tasks) > 0); | |
1931 | } | |
1932 | ||
1933 | METHOD(task_manager_t, incr_mid, void, | |
1934 | private_task_manager_t *this, bool initiate) | |
1935 | { | |
4a09d9ee MW |
1936 | } |
1937 | ||
05a2be82 TB |
1938 | METHOD(task_manager_t, get_mid, uint32_t, |
1939 | private_task_manager_t *this, bool initiate) | |
1940 | { | |
1941 | return initiate ? this->initiating.mid : this->responding.mid; | |
1942 | } | |
1943 | ||
4a09d9ee | 1944 | METHOD(task_manager_t, reset, void, |
b12c53ce | 1945 | private_task_manager_t *this, uint32_t initiate, uint32_t respond) |
4a09d9ee | 1946 | { |
d9c1dae2 MW |
1947 | enumerator_t *enumerator; |
1948 | task_t *task; | |
1949 | ||
1950 | /* reset message counters and retransmit packets */ | |
127a98dc TB |
1951 | clear_packets(this->responding.packets); |
1952 | clear_packets(this->initiating.packets); | |
f5a84055 MW |
1953 | this->responding.seqnr = RESPONDING_SEQ; |
1954 | this->responding.retransmitted = 0; | |
d9c1dae2 MW |
1955 | this->initiating.mid = 0; |
1956 | this->initiating.seqnr = 0; | |
1957 | this->initiating.retransmitted = 0; | |
1958 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; | |
70e0be07 TB |
1959 | DESTROY_IF(this->defrag); |
1960 | this->defrag = NULL; | |
fd6fbf17 MW |
1961 | if (initiate != UINT_MAX) |
1962 | { | |
1963 | this->dpd_send = initiate; | |
1964 | } | |
1965 | if (respond != UINT_MAX) | |
1966 | { | |
1967 | this->dpd_recv = respond; | |
1968 | } | |
d9c1dae2 MW |
1969 | |
1970 | /* reset queued tasks */ | |
1971 | enumerator = this->queued_tasks->create_enumerator(this->queued_tasks); | |
1972 | while (enumerator->enumerate(enumerator, &task)) | |
1973 | { | |
1974 | task->migrate(task, this->ike_sa); | |
1975 | } | |
1976 | enumerator->destroy(enumerator); | |
1977 | ||
1978 | /* reset active tasks */ | |
1979 | while (this->active_tasks->remove_last(this->active_tasks, | |
1980 | (void**)&task) == SUCCESS) | |
1981 | { | |
1982 | task->migrate(task, this->ike_sa); | |
1983 | this->queued_tasks->insert_first(this->queued_tasks, task); | |
1984 | } | |
4a09d9ee MW |
1985 | } |
1986 | ||
4505e3c0 TB |
1987 | /** |
1988 | * Data for a task queue enumerator | |
1989 | */ | |
1990 | typedef struct { | |
1991 | enumerator_t public; | |
1992 | task_queue_t queue; | |
1993 | enumerator_t *inner; | |
1994 | } task_enumerator_t; | |
1995 | ||
1996 | METHOD(enumerator_t, task_enumerator_destroy, void, | |
1997 | task_enumerator_t *this) | |
1998 | { | |
1999 | this->inner->destroy(this->inner); | |
2000 | free(this); | |
2001 | } | |
2002 | ||
2003 | METHOD(enumerator_t, task_enumerator_enumerate, bool, | |
2004 | task_enumerator_t *this, va_list args) | |
2005 | { | |
2006 | task_t **task; | |
2007 | ||
2008 | VA_ARGS_VGET(args, task); | |
2009 | return this->inner->enumerate(this->inner, task); | |
2010 | } | |
2011 | ||
4a09d9ee MW |
2012 | METHOD(task_manager_t, create_task_enumerator, enumerator_t*, |
2013 | private_task_manager_t *this, task_queue_t queue) | |
2014 | { | |
4505e3c0 TB |
2015 | task_enumerator_t *enumerator; |
2016 | ||
2017 | INIT(enumerator, | |
2018 | .public = { | |
2019 | .enumerate = enumerator_enumerate_default, | |
2020 | .venumerate = _task_enumerator_enumerate, | |
2021 | .destroy = _task_enumerator_destroy, | |
2022 | }, | |
2023 | .queue = queue, | |
2024 | ); | |
4a09d9ee MW |
2025 | switch (queue) |
2026 | { | |
2027 | case TASK_QUEUE_ACTIVE: | |
4505e3c0 TB |
2028 | enumerator->inner = this->active_tasks->create_enumerator( |
2029 | this->active_tasks); | |
2030 | break; | |
4a09d9ee | 2031 | case TASK_QUEUE_PASSIVE: |
4505e3c0 TB |
2032 | enumerator->inner = this->passive_tasks->create_enumerator( |
2033 | this->passive_tasks); | |
2034 | break; | |
4a09d9ee | 2035 | case TASK_QUEUE_QUEUED: |
4505e3c0 TB |
2036 | enumerator->inner = this->queued_tasks->create_enumerator( |
2037 | this->queued_tasks); | |
2038 | break; | |
4a09d9ee | 2039 | default: |
4505e3c0 TB |
2040 | enumerator->inner = enumerator_create_empty(); |
2041 | break; | |
2042 | } | |
2043 | return &enumerator->public; | |
2044 | } | |
2045 | ||
2046 | METHOD(task_manager_t, remove_task, void, | |
2047 | private_task_manager_t *this, enumerator_t *enumerator_public) | |
2048 | { | |
2049 | task_enumerator_t *enumerator = (task_enumerator_t*)enumerator_public; | |
2050 | ||
2051 | switch (enumerator->queue) | |
2052 | { | |
2053 | case TASK_QUEUE_ACTIVE: | |
2054 | this->active_tasks->remove_at(this->active_tasks, | |
2055 | enumerator->inner); | |
2056 | break; | |
2057 | case TASK_QUEUE_PASSIVE: | |
2058 | this->passive_tasks->remove_at(this->passive_tasks, | |
2059 | enumerator->inner); | |
2060 | break; | |
2061 | case TASK_QUEUE_QUEUED: | |
2062 | this->queued_tasks->remove_at(this->queued_tasks, | |
2063 | enumerator->inner); | |
2064 | break; | |
2065 | default: | |
2066 | break; | |
4a09d9ee MW |
2067 | } |
2068 | } | |
2069 | ||
2070 | METHOD(task_manager_t, destroy, void, | |
2071 | private_task_manager_t *this) | |
2072 | { | |
2073 | flush(this); | |
2074 | ||
2075 | this->active_tasks->destroy(this->active_tasks); | |
2076 | this->queued_tasks->destroy(this->queued_tasks); | |
2077 | this->passive_tasks->destroy(this->passive_tasks); | |
70e0be07 | 2078 | DESTROY_IF(this->defrag); |
4a09d9ee | 2079 | |
f91b6ac7 | 2080 | DESTROY_IF(this->queued); |
127a98dc TB |
2081 | clear_packets(this->responding.packets); |
2082 | array_destroy(this->responding.packets); | |
2083 | clear_packets(this->initiating.packets); | |
2084 | array_destroy(this->initiating.packets); | |
73aaf76b | 2085 | DESTROY_IF(this->rng); |
4a09d9ee MW |
2086 | free(this); |
2087 | } | |
2088 | ||
2089 | /* | |
2090 | * see header file | |
2091 | */ | |
2092 | task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa) | |
2093 | { | |
2094 | private_task_manager_t *this; | |
2095 | ||
2096 | INIT(this, | |
2097 | .public = { | |
2098 | .task_manager = { | |
2099 | .process_message = _process_message, | |
2100 | .queue_task = _queue_task, | |
208678e6 | 2101 | .queue_task_delayed = _queue_task_delayed, |
a60daa07 | 2102 | .queue_ike = _queue_ike, |
dab60d64 | 2103 | .queue_ike_rekey = _queue_ike_rekey, |
cedb412e | 2104 | .queue_ike_reauth = _queue_ike_reauth, |
3ed148b3 | 2105 | .queue_ike_delete = _queue_ike_delete, |
873df908 | 2106 | .queue_mobike = _queue_mobike, |
fe43d9a2 | 2107 | .queue_child = _queue_child, |
463a73cc | 2108 | .queue_child_rekey = _queue_child_rekey, |
83c5fda0 | 2109 | .queue_child_delete = _queue_child_delete, |
244d715d | 2110 | .queue_dpd = _queue_dpd, |
4a09d9ee MW |
2111 | .initiate = _initiate, |
2112 | .retransmit = _retransmit, | |
2113 | .incr_mid = _incr_mid, | |
05a2be82 | 2114 | .get_mid = _get_mid, |
4a09d9ee MW |
2115 | .reset = _reset, |
2116 | .adopt_tasks = _adopt_tasks, | |
68db844f | 2117 | .adopt_child_tasks = _adopt_child_tasks, |
4a09d9ee MW |
2118 | .busy = _busy, |
2119 | .create_task_enumerator = _create_task_enumerator, | |
4505e3c0 | 2120 | .remove_task = _remove_task, |
b1908994 | 2121 | .flush = _flush, |
a5c79960 | 2122 | .flush_queue = _flush_queue, |
4a09d9ee MW |
2123 | .destroy = _destroy, |
2124 | }, | |
2125 | }, | |
dcbdc914 TB |
2126 | .initiating = { |
2127 | .type = EXCHANGE_TYPE_UNDEFINED, | |
2128 | }, | |
2129 | .responding = { | |
2130 | .seqnr = RESPONDING_SEQ, | |
2131 | }, | |
4a09d9ee | 2132 | .ike_sa = ike_sa, |
73aaf76b | 2133 | .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK), |
4a09d9ee MW |
2134 | .queued_tasks = linked_list_create(), |
2135 | .active_tasks = linked_list_create(), | |
2136 | .passive_tasks = linked_list_create(), | |
2137 | .retransmit_tries = lib->settings->get_int(lib->settings, | |
389e4b8e | 2138 | "%s.retransmit_tries", RETRANSMIT_TRIES, lib->ns), |
4a09d9ee | 2139 | .retransmit_timeout = lib->settings->get_double(lib->settings, |
389e4b8e | 2140 | "%s.retransmit_timeout", RETRANSMIT_TIMEOUT, lib->ns), |
4a09d9ee | 2141 | .retransmit_base = lib->settings->get_double(lib->settings, |
389e4b8e TB |
2142 | "%s.retransmit_base", RETRANSMIT_BASE, lib->ns), |
2143 | .retransmit_jitter = min(lib->settings->get_int(lib->settings, | |
2144 | "%s.retransmit_jitter", 0, lib->ns), RETRANSMIT_JITTER_MAX), | |
2145 | .retransmit_limit = lib->settings->get_int(lib->settings, | |
2146 | "%s.retransmit_limit", 0, lib->ns) * 1000, | |
4a09d9ee MW |
2147 | ); |
2148 | ||
3fca5bd1 MW |
2149 | if (!this->rng) |
2150 | { | |
2151 | DBG1(DBG_IKE, "no RNG found, unable to create IKE_SA"); | |
2152 | destroy(this); | |
2153 | return NULL; | |
2154 | } | |
1bb9c51e TB |
2155 | if (!this->rng->get_bytes(this->rng, sizeof(this->dpd_send), |
2156 | (void*)&this->dpd_send)) | |
2157 | { | |
2158 | DBG1(DBG_IKE, "failed to allocate message ID, unable to create IKE_SA"); | |
2159 | destroy(this); | |
2160 | return NULL; | |
2161 | } | |
2ddd45c9 | 2162 | this->dpd_send &= 0x7FFFFFFF; |
3e6b7403 | 2163 | |
4a09d9ee MW |
2164 | return &this->public; |
2165 | } |