d38987dab889a218573c3faa490acfc2455dca30
[strongswan.git] / src / charon / sa / ike_sa_manager.c
1 /**
2 * @file ike_sa_manager.c
3 *
4 * @brief Implementation of ike_sa_mananger_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <pthread.h>
24 #include <string.h>
25
26 #include "ike_sa_manager.h"
27
28 #include <daemon.h>
29 #include <sa/ike_sa_id.h>
30 #include <utils/logger.h>
31 #include <utils/logger_manager.h>
32 #include <utils/linked_list.h>
33
34 typedef struct ike_sa_entry_t ike_sa_entry_t;
35
36 /**
37 * An entry in the linked list, contains IKE_SA, locking and lookup data.
38 */
39 struct ike_sa_entry_t {
40 /**
41 * Destructor, also destroys associated ike_sa_t object.
42 */
43 status_t (*destroy) (ike_sa_entry_t *this);
44
45 /**
46 * Number of threads waiting for this ike_sa_t object.
47 */
48 int waiting_threads;
49
50 /**
51 * Condvar where threads can wait until ike_sa_t object is free for use again.
52 */
53 pthread_cond_t condvar;
54
55 /**
56 * Is this ike_sa currently checked out?
57 */
58 bool checked_out;
59
60 /**
61 * Does this SA drives out new threads?
62 */
63 bool driveout_new_threads;
64
65 /**
66 * Does this SA drives out waiting threads?
67 */
68 bool driveout_waiting_threads;
69
70 /**
71 * Identifiaction of an IKE_SA (SPIs).
72 */
73 ike_sa_id_t *ike_sa_id;
74
75 /**
76 * The contained ike_sa_t object.
77 */
78 ike_sa_t *ike_sa;
79 };
80
81 /**
82 * Implementation of ike_sa_entry_t.destroy.
83 */
84 static status_t ike_sa_entry_destroy(ike_sa_entry_t *this)
85 {
86 /* also destroy IKE SA */
87 this->ike_sa->destroy(this->ike_sa);
88 this->ike_sa_id->destroy(this->ike_sa_id);
89 free(this);
90 return SUCCESS;
91 }
92
93 /**
94 * @brief Creates a new entry for the ike_sa_t list.
95 *
96 * This constructor additionaly creates a new and empty SA.
97 *
98 * @param ike_sa_id The associated ike_sa_id_t, will be cloned
99 * @return ike_sa_entry_t object
100 */
101 static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id)
102 {
103 ike_sa_entry_t *this = malloc_thing(ike_sa_entry_t);
104
105 /* destroy function */
106 this->destroy = ike_sa_entry_destroy;
107
108 this->waiting_threads = 0;
109 pthread_cond_init(&(this->condvar), NULL);
110
111 /* we set checkout flag when we really give it out */
112 this->checked_out = FALSE;
113 this->driveout_new_threads = FALSE;
114 this->driveout_waiting_threads = FALSE;
115
116 /* ike_sa_id is always cloned */
117 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
118
119 /* create new ike_sa */
120 this->ike_sa = ike_sa_create(ike_sa_id);
121
122 return this;
123 }
124
125
126 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t;
127
128 /**
129 * Additional private members of ike_sa_manager_t.
130 */
131 struct private_ike_sa_manager_t {
132 /**
133 * Public interface of ike_sa_manager_t.
134 */
135 ike_sa_manager_t public;
136
137 /**
138 * @brief Get next spi.
139 *
140 * We give out SPIs from a pseudo random source
141 *
142 * @param this the ike_sa_manager
143 * @return the next spi
144 */
145 u_int64_t (*get_next_spi) (private_ike_sa_manager_t *this);
146
147 /**
148 * @brief Find the ike_sa_entry_t object in the list by SPIs.
149 *
150 * This function simply iterates over the linked list. A hash-table
151 * would be more efficient when storing a lot of IKE_SAs...
152 *
153 * @param this calling object
154 * @param ike_sa_id id of the ike_sa, containing SPIs
155 * @param[out] entry pointer to set to the found entry
156 * @return
157 * - SUCCESS when found,
158 * - NOT_FOUND when no such ike_sa_id in list
159 */
160 status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry);
161
162 /**
163 * @brief Find the ike_sa_entry_t in the list by pointer to SA.
164 *
165 * This function simply iterates over the linked list. A hash-table
166 * would be more efficient when storing a lot of IKE_SAs...
167 *
168 * @param this calling object
169 * @param ike_sa pointer to the ike_sa
170 * @param[out] entry pointer to set to the found entry
171 * @return
172 * - SUCCESS when found,
173 * - NOT_FOUND when no such ike_sa_id in list
174 */
175 status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry);
176
177 /**
178 * @brief Felete an entry from the linked list.
179 *
180 * @param this calling object
181 * @param entry entry to delete
182 * @return
183 * - SUCCESS when found,
184 * - NOT_FOUND when no such ike_sa_id in list
185 */
186 status_t (*delete_entry) (private_ike_sa_manager_t *this, ike_sa_entry_t *entry);
187
188 /**
189 * Lock for exclusivly accessing the manager.
190 */
191 pthread_mutex_t mutex;
192
193 /**
194 * Logger used for this IKE SA Manager.
195 */
196 logger_t *logger;
197
198 /**
199 * Linked list with entries for the ike_sa_t objects.
200 */
201 linked_list_t *ike_sa_list;
202
203 /**
204 * A randomizer, to get random SPIs for our side
205 */
206 randomizer_t *randomizer;
207 };
208
209 /**
210 * Implementation of private_ike_sa_manager_t.get_entry_by_id.
211 */
212 static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry)
213 {
214 linked_list_t *list = this->ike_sa_list;
215 iterator_t *iterator;
216 status_t status;
217
218 /* create iterator over list of ike_sa's */
219 iterator = list->create_iterator(list, TRUE);
220
221 /* default status */
222 status = NOT_FOUND;
223
224 while (iterator->has_next(iterator))
225 {
226 ike_sa_entry_t *current;
227
228 iterator->current(iterator, (void**)&current);
229 if (current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0)
230 {
231 /* seems to be a half ready ike_sa */
232 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) == ike_sa_id->get_initiator_spi(ike_sa_id))
233 && (ike_sa_id->is_initiator(ike_sa_id) == current->ike_sa_id->is_initiator(current->ike_sa_id)))
234 {
235 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by initiator spi %d",
236 ike_sa_id->get_initiator_spi(ike_sa_id));
237 *entry = current;
238 status = SUCCESS;
239 break;
240 }
241 }
242 else if (ike_sa_id->get_responder_spi(ike_sa_id) == 0)
243 {
244 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) == ike_sa_id->get_initiator_spi(ike_sa_id))
245 && (ike_sa_id->is_initiator(ike_sa_id) == current->ike_sa_id->is_initiator(current->ike_sa_id)))
246 {
247 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by initiator spi %d",
248 ike_sa_id->get_initiator_spi(ike_sa_id));
249 *entry = current;
250 status = SUCCESS;
251 break;
252 }
253 }
254 if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id))
255 {
256 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by full ID");
257 *entry = current;
258 status = SUCCESS;
259 break;
260 }
261 }
262
263 iterator->destroy(iterator);
264 return status;
265 }
266
267 /**
268 * Implementation of private_ike_sa_manager_t.get_entry_by_sa.
269 */
270 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry)
271 {
272 linked_list_t *list = this->ike_sa_list;
273 iterator_t *iterator;
274 status_t status;
275
276 iterator = list->create_iterator(list, TRUE);
277
278 /* default status */
279 status = NOT_FOUND;
280
281 while (iterator->has_next(iterator))
282 {
283 ike_sa_entry_t *current;
284 iterator->current(iterator, (void**)&current);
285 /* only pointers are compared */
286 if (current->ike_sa == ike_sa)
287 {
288 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by pointer");
289 *entry = current;
290 status = SUCCESS;
291 break;
292 }
293 }
294 iterator->destroy(iterator);
295
296 return status;
297 }
298
299 /**
300 * Implementation of private_ike_sa_manager_s.delete_entry.
301 */
302 static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry)
303 {
304 linked_list_t *list = this->ike_sa_list;
305 iterator_t *iterator;
306 status_t status;
307
308 iterator = list->create_iterator(list, TRUE);
309
310 status = NOT_FOUND;
311
312 while (iterator->has_next(iterator))
313 {
314 ike_sa_entry_t *current;
315 iterator->current(iterator, (void**)&current);
316 if (current == entry)
317 {
318 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by pointer. Going to delete it.");
319 iterator->remove(iterator);
320 entry->destroy(entry);
321 status = SUCCESS;
322 break;
323 }
324 }
325 iterator->destroy(iterator);
326 return status;
327 }
328
329
330 /**
331 * Implementation of private_ike_sa_manager_t.get_next_spi.
332 */
333 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
334 {
335 u_int64_t spi;
336
337 this->randomizer->get_pseudo_random_bytes(this->randomizer, 8, (u_int8_t*)&spi);
338
339 return spi;
340 }
341
342 /**
343 * Implementation of of ike_sa_manager.create_and_checkout.
344 */
345 static void create_and_checkout(private_ike_sa_manager_t *this,ike_sa_t **ike_sa)
346 {
347 u_int64_t initiator_spi;
348 ike_sa_entry_t *new_ike_sa_entry;
349 ike_sa_id_t *new_ike_sa_id;
350
351 initiator_spi = this->get_next_spi(this);
352 new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
353 new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
354
355 /* create entry */
356 new_ike_sa_entry = ike_sa_entry_create(new_ike_sa_id);
357 new_ike_sa_id->destroy(new_ike_sa_id);
358
359 /* each access is locked */
360 pthread_mutex_lock(&(this->mutex));
361
362 this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
363
364 /* check ike_sa out */
365 this->logger->log(this->logger, CONTROL|LEVEL1, "New IKE_SA created and added to list of known IKE_SA's");
366 new_ike_sa_entry->checked_out = TRUE;
367 *ike_sa = new_ike_sa_entry->ike_sa;
368
369 pthread_mutex_unlock(&(this->mutex));
370 }
371
372 /**
373 * Implementation of of ike_sa_manager.checkout.
374 */
375 static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_t **ike_sa)
376 {
377 bool responder_spi_set;
378 bool initiator_spi_set;
379 bool original_initiator;
380 status_t retval;
381
382 /* each access is locked */
383 pthread_mutex_lock(&(this->mutex));
384
385 responder_spi_set = (FALSE != ike_sa_id->get_responder_spi(ike_sa_id));
386 initiator_spi_set = (FALSE != ike_sa_id->get_initiator_spi(ike_sa_id));
387 original_initiator = ike_sa_id->is_initiator(ike_sa_id);
388
389 if ((initiator_spi_set && responder_spi_set) ||
390 ((initiator_spi_set && !responder_spi_set) && (original_initiator)))
391 {
392 /* we SHOULD have an IKE_SA for these SPIs in the list,
393 * if not, we can't handle the request...
394 */
395 ike_sa_entry_t *entry;
396 /* look for the entry */
397 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
398 {
399 /* can we give this ike_sa out to new requesters?*/
400 if (entry->driveout_new_threads)
401 {
402 this->logger->log(this->logger, CONTROL|LEVEL1, "Drive out new thread for existing IKE_SA");
403 /* no we can't */
404 retval = NOT_FOUND;
405 }
406 else
407 {
408 /* is this IKE_SA already checked out ??
409 * are we welcome to get this SA ? */
410 while (entry->checked_out && !entry->driveout_waiting_threads)
411 {
412 /* so wait until we can get it for us.
413 * we register us as waiting.
414 */
415 entry->waiting_threads++;
416 pthread_cond_wait(&(entry->condvar), &(this->mutex));
417 entry->waiting_threads--;
418 }
419
420 /* hm, a deletion request forbids us to get this SA, go home */
421 if (entry->driveout_waiting_threads)
422 {
423 /* we must signal here, others are interested that we leave */
424 pthread_cond_signal(&(entry->condvar));
425 this->logger->log(this->logger, CONTROL|LEVEL1, "Drive out waiting thread for existing IKE_SA");
426 retval = NOT_FOUND;
427 }
428 else
429 {
430 this->logger->log(this->logger, CONTROL|LEVEL2, "IKE SA successfully checked out");
431 /* ok, this IKE_SA is finally ours */
432 entry->checked_out = TRUE;
433 *ike_sa = entry->ike_sa;
434 /* DON'T use return, we must unlock the mutex! */
435 retval = SUCCESS;
436 }
437 }
438 }
439 else
440 {
441 this->logger->log(this->logger, ERROR|LEVEL1, "IKE SA not stored in known IKE_SA list");
442 /* looks like there is no such IKE_SA, better luck next time... */
443 /* DON'T use return, we must unlock the mutex! */
444 retval = NOT_FOUND;
445 }
446 }
447 else if ((initiator_spi_set && !responder_spi_set) && (!original_initiator))
448 {
449 /* an IKE_SA_INIT from an another endpoint,
450 * he is the initiator.
451 * For simplicity, we do NOT check for retransmitted
452 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
453 * Request (even a retransmitted one) will result in a
454 * IKE_SA. This could be improved...
455 */
456 u_int64_t responder_spi;
457 ike_sa_entry_t *new_ike_sa_entry;
458
459
460 /* set SPIs, we are the responder */
461 responder_spi = this->get_next_spi(this);
462
463 /* we also set arguments spi, so its still valid */
464 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
465
466 /* create entry */
467 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
468
469 this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
470
471 /* check ike_sa out */
472 this->logger->log(this->logger, CONTROL|LEVEL1 ,"IKE_SA added to list of known IKE_SA's");
473 new_ike_sa_entry->checked_out = TRUE;
474 *ike_sa = new_ike_sa_entry->ike_sa;
475
476 retval = CREATED;
477 }
478 else
479 {
480 /* responder set, initiator not: here is something seriously wrong! */
481 this->logger->log(this->logger, ERROR|LEVEL1, "Invalid IKE_SA SPI's");
482 /* DON'T use return, we must unlock the mutex! */
483 retval = INVALID_ARG;
484 }
485
486 pthread_mutex_unlock(&(this->mutex));
487 /* OK, unlocked... */
488 return retval;
489 }
490
491 /**
492 * Implementation of of ike_sa_manager.checkout_by_reqid.
493 */
494 static status_t checkout_by_reqid(private_ike_sa_manager_t *this, u_int32_t reqid, ike_sa_t **ike_sa)
495 {
496 iterator_t *iterator;
497 status_t status = NOT_FOUND;
498
499 pthread_mutex_lock(&(this->mutex));
500
501 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
502 while (iterator->has_next(iterator))
503 {
504 ike_sa_entry_t *entry;
505
506 iterator->current(iterator, (void**)&entry);
507 if (entry->driveout_new_threads)
508 {
509 /* we are not allowed to get this, get next one */
510 continue;
511 }
512 while (entry->checked_out && !entry->driveout_waiting_threads)
513 {
514 /* so wait until we can get it for us.
515 * we register us as waiting. */
516 entry->waiting_threads++;
517 pthread_cond_wait(&(entry->condvar), &(this->mutex));
518 entry->waiting_threads--;
519 }
520 /* hm, a deletion request forbids us to get this SA, get next one */
521 if (entry->driveout_waiting_threads)
522 {
523 /* we must signal here, others may be waiting on it, too */
524 pthread_cond_signal(&(entry->condvar));
525 continue;
526 }
527 /* ok, access is exclusive for us, check reqid */
528 if (entry->ike_sa->get_child_sa(entry->ike_sa, reqid) != NULL)
529 {
530 /* match */
531 entry->checked_out = TRUE;
532 *ike_sa = entry->ike_sa;
533 status = SUCCESS;
534 break;
535 }
536 }
537 iterator->destroy(iterator);
538 pthread_mutex_unlock(&(this->mutex));
539
540 return status;
541 }
542
543 /**
544 * Implementation of ike_sa_manager_t.get_ike_sa_list.
545 */
546 linked_list_t *get_ike_sa_list(private_ike_sa_manager_t* this)
547 {
548 linked_list_t *list;
549 iterator_t *iterator;
550
551 pthread_mutex_lock(&(this->mutex));
552
553 list = linked_list_create();
554 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
555 while (iterator->has_next(iterator))
556 {
557 ike_sa_entry_t *entry;
558 iterator->current(iterator, (void**)&entry);
559 list->insert_last(list, (void*)entry->ike_sa_id->clone(entry->ike_sa_id));
560 }
561 iterator->destroy(iterator);
562
563 pthread_mutex_unlock(&(this->mutex));
564 return list;
565 }
566
567 /**
568 * Implementation of ike_sa_manager_t.get_ike_sa_list_by_name.
569 */
570 linked_list_t *get_ike_sa_list_by_name(private_ike_sa_manager_t* this, const char *name)
571 {
572 linked_list_t *list;
573 iterator_t *iterator;
574
575 pthread_mutex_lock(&(this->mutex));
576
577 list = linked_list_create();
578 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
579 while (iterator->has_next(iterator))
580 {
581 ike_sa_entry_t *entry;
582 connection_t *connection;
583
584 iterator->current(iterator, (void**)&entry);
585 connection = entry->ike_sa->get_connection(entry->ike_sa);
586 if (strcmp(name, connection->get_name(connection)) == 0)
587 {
588 list->insert_last(list, (void*)entry->ike_sa_id->clone(entry->ike_sa_id));
589 }
590 }
591 iterator->destroy(iterator);
592
593 pthread_mutex_unlock(&(this->mutex));
594 return list;
595 }
596
597 /**
598 * Implementation of ike_sa_manager_t.log_status.
599 */
600 static void log_status(private_ike_sa_manager_t* this, logger_t* logger, char* name)
601 {
602 iterator_t *iterator;
603
604 logger->log(logger, CONTROL, "Instances:");
605
606 pthread_mutex_lock(&(this->mutex));
607
608 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
609 while (iterator->has_next(iterator))
610 {
611 ike_sa_entry_t *entry;
612 iterator->current(iterator, (void**)&entry);
613 entry->ike_sa->log_status(entry->ike_sa, logger, name);
614 }
615 iterator->destroy(iterator);
616
617 pthread_mutex_unlock(&(this->mutex));
618 }
619
620 /**
621 * Implementation of ike_sa_manager_t.checkin.
622 */
623 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
624 {
625 /* to check the SA back in, we look for the pointer of the ike_sa
626 * in all entries.
627 * We can't search by SPI's since the MAY have changed (e.g. on reception
628 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
629 */
630 status_t retval;
631 ike_sa_entry_t *entry;
632
633 pthread_mutex_lock(&(this->mutex));
634
635 /* look for the entry */
636 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
637 {
638 /* ike_sa_id must be updated */
639 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
640 /* signal waiting threads */
641 entry->checked_out = FALSE;
642 this->logger->log(this->logger, CONTROL|LEVEL1, "Checkin of IKE_SA successful.");
643 pthread_cond_signal(&(entry->condvar));
644 retval = SUCCESS;
645 }
646 else
647 {
648 this->logger->log(this->logger, ERROR, "Tried to checkin nonexisting IKE_SA");
649 /* this SA is no more, this REALLY should not happen */
650 retval = NOT_FOUND;
651 }
652 pthread_mutex_unlock(&(this->mutex));
653 return retval;
654 }
655
656
657 /**
658 * Implementation of ike_sa_manager_t.checkin_and_destroy.
659 */
660 static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
661 {
662 /* deletion is a bit complex, we must garant that no thread is waiting for
663 * this SA.
664 * We take this SA from the list, and start signaling while threads
665 * are in the condvar.
666 */
667 ike_sa_entry_t *entry;
668 status_t retval;
669
670 pthread_mutex_lock(&(this->mutex));
671
672 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
673 {
674 /* mark it, so now new threads can acquire this SA */
675 entry->driveout_new_threads = TRUE;
676 /* additionaly, drive out waiting threads */
677 entry->driveout_waiting_threads = TRUE;
678
679 /* wait until all workers have done their work */
680 while (entry->waiting_threads)
681 {
682 /* let the other threads leave the manager */
683 pthread_cond_broadcast(&(entry->condvar));
684 /* and the nice thing, they will wake us again when their work is done */
685 pthread_cond_wait(&(entry->condvar), &(this->mutex));
686 }
687 /* ok, we are alone now, no threads waiting in the entry's condvar */
688 this->delete_entry(this, entry);
689 this->logger->log(this->logger, CONTROL|LEVEL1, "Checkin and destroy of IKE_SA successful");
690 retval = SUCCESS;
691 }
692 else
693 {
694 this->logger->log(this->logger,ERROR, "Tried to checkin and delete nonexisting IKE_SA");
695 retval = NOT_FOUND;
696 }
697
698 pthread_mutex_unlock(&(this->mutex));
699 return retval;
700 }
701
702 /**
703 * Implementation of ike_sa_manager_t.delete.
704 */
705 static status_t delete_(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
706 {
707 /* deletion is a bit complex, we must garant that no thread is waiting for
708 * this SA.
709 * We take this SA from the list, and start signaling while threads
710 * are in the condvar.
711 */
712 ike_sa_entry_t *entry;
713 status_t retval;
714
715 pthread_mutex_lock(&(this->mutex));
716
717 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
718 {
719 /* we try a delete. If it succeeds, our job is done here. The
720 * other peer will reply, and the IKE SA gets the finally deleted...
721 */
722 if (entry->ike_sa->delete(entry->ike_sa) == SUCCESS)
723 {
724 this->logger->log(this->logger, CONTROL|LEVEL1, "Initiated delete for IKE_SA");
725 }
726 /* but if the IKE SA is not in a state where the deletion is negotiated with
727 * the other peer, we can destroy the IKE SA on our own. For this, we must
728 * be sure that really NO other threads are waiting for this SA...
729 */
730 else
731 {
732 /* mark it, so now new threads can acquire this SA */
733 entry->driveout_new_threads = TRUE;
734 /* wait until all workers have done their work */
735 while (entry->waiting_threads)
736 {
737 /* wake up all */
738 pthread_cond_broadcast(&(entry->condvar));
739 /* and the nice thing, they will wake us again when their work is done */
740 pthread_cond_wait(&(entry->condvar), &(this->mutex));
741 }
742 /* ok, we are alone now, no threads waiting in the entry's condvar */
743 this->delete_entry(this, entry);
744 this->logger->log(this->logger, CONTROL|LEVEL1, "Destroyed IKE_SA");
745 }
746 retval = SUCCESS;
747 }
748 else
749 {
750 this->logger->log(this->logger,ERROR, "Tried to delete nonexisting IKE_SA");
751 retval = NOT_FOUND;
752 }
753
754 pthread_mutex_unlock(&(this->mutex));
755 return retval;
756 }
757
758 /**
759 * Implementation of ike_sa_manager_t.destroy.
760 */
761 static void destroy(private_ike_sa_manager_t *this)
762 {
763 /* destroy all list entries */
764 linked_list_t *list = this->ike_sa_list;
765 iterator_t *iterator;
766 ike_sa_entry_t *entry;
767
768 pthread_mutex_lock(&(this->mutex));
769
770 this->logger->log(this->logger, CONTROL|LEVEL1, "Going to destroy IKE_SA manager and all managed IKE_SA's");
771
772 /* Step 1: drive out all waiting threads */
773 iterator = list->create_iterator(list, TRUE);
774
775 this->logger->log(this->logger, CONTROL|LEVEL2, "Set driveout flags for all stored IKE_SA's");
776 while (iterator->has_next(iterator))
777 {
778 iterator->current(iterator, (void**)&entry);
779 /* do not accept new threads, drive out waiting threads */
780 entry->driveout_new_threads = TRUE;
781 entry->driveout_waiting_threads = TRUE;
782 }
783
784 this->logger->log(this->logger, CONTROL|LEVEL2, "Wait for all threads to leave IKE_SA's");
785 /* Step 2: wait until all are gone */
786 iterator->reset(iterator);
787 while (iterator->has_next(iterator))
788 {
789 iterator->current(iterator, (void**)&entry);
790 while (entry->waiting_threads)
791 {
792 /* wake up all */
793 pthread_cond_broadcast(&(entry->condvar));
794 /* go sleeping until they are gone */
795 pthread_cond_wait(&(entry->condvar), &(this->mutex));
796 }
797 }
798 this->logger->log(this->logger, CONTROL|LEVEL2, "Delete all IKE_SA's");
799 /* Step 3: initiate deletion of all IKE_SAs */
800 iterator->reset(iterator);
801 while (iterator->has_next(iterator))
802 {
803 iterator->current(iterator, (void**)&entry);
804 entry->ike_sa->delete(entry->ike_sa);
805 }
806 iterator->destroy(iterator);
807
808 this->logger->log(this->logger, CONTROL|LEVEL2, "Destroy all entries");
809 /* Step 4: destroy all entries */
810 while (list->get_count(list) > 0)
811 {
812 list->get_first(list, (void**)&entry);
813 this->delete_entry(this, entry);
814 }
815 list->destroy(list);
816 pthread_mutex_unlock(&(this->mutex));
817
818 this->randomizer->destroy(this->randomizer);
819
820 free(this);
821 }
822
823 /*
824 * Described in header.
825 */
826 ike_sa_manager_t *ike_sa_manager_create()
827 {
828 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
829
830 /* assign public functions */
831 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
832 this->public.create_and_checkout = (void(*)(ike_sa_manager_t*,ike_sa_t**))create_and_checkout;
833 this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t*,ike_sa_t**))checkout;
834 this->public.checkout_by_reqid = (status_t(*)(ike_sa_manager_t*,u_int32_t,ike_sa_t**))checkout_by_reqid;
835 this->public.get_ike_sa_list = (linked_list_t*(*)(ike_sa_manager_t*))get_ike_sa_list;
836 this->public.get_ike_sa_list_by_name = (linked_list_t*(*)(ike_sa_manager_t*,const char*))get_ike_sa_list_by_name;
837 this->public.log_status = (void(*)(ike_sa_manager_t*,logger_t*,char*))log_status;
838 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
839 this->public.delete = (status_t(*)(ike_sa_manager_t*,ike_sa_id_t*))delete_;
840 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
841
842 /* initialize private functions */
843 this->get_next_spi = get_next_spi;
844 this->get_entry_by_sa = get_entry_by_sa;
845 this->get_entry_by_id = get_entry_by_id;
846 this->delete_entry = delete_entry;
847
848 /* initialize private variables */
849 this->logger = logger_manager->get_logger(logger_manager, IKE_SA_MANAGER);
850
851 this->ike_sa_list = linked_list_create();
852
853 pthread_mutex_init(&(this->mutex), NULL);
854
855 this->randomizer = randomizer_create();
856
857 return (ike_sa_manager_t*)this;
858 }