2 * @file ike_sa_manager.c
4 * @brief Central point for managing IKE-SAs (creation, locking, deleting...)
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
26 #include "ike_sa_manager.h"
29 #include "ike_sa_id.h"
30 #include "utils/allocator.h"
31 #include "utils/logger.h"
32 #include "utils/logger_manager.h"
33 #include "utils/linked_list.h"
36 * @brief An entry in the linked list, contains IKE_SA, locking and lookup data.
38 typedef struct ike_sa_entry_s ike_sa_entry_t
;
39 struct ike_sa_entry_s
{
41 * destructor, also destroys ike_sa
43 status_t (*destroy
) (ike_sa_entry_t
*this);
45 * Number of threads waiting for this ike_sa
49 * condvar where threads can wait until it's free again
51 pthread_cond_t condvar
;
53 * is this ike_sa currently checked out?
57 * Does this SA drives out new threads?
59 bool driveout_new_threads
;
61 * Does this SA drives out waiting threads?
63 bool driveout_waiting_threads
;
65 * identifiaction of ike_sa (SPIs)
67 ike_sa_id_t
*ike_sa_id
;
69 * the contained ike_sa
75 * @see ike_sa_entry_t.destroy
77 static status_t
ike_sa_entry_destroy(ike_sa_entry_t
*this)
79 this->ike_sa
->destroy(this->ike_sa
);
80 this->ike_sa_id
->destroy(this->ike_sa_id
);
86 * @brief creates a new entry for the ike_sa list
88 * This constructor additionaly creates a new and empty SA
90 * @param ike_sa_id the associated ike_sa_id_t, will be cloned
91 * @return created entry, with ike_sa and ike_sa_id
93 static ike_sa_entry_t
*ike_sa_entry_create(ike_sa_id_t
*ike_sa_id
)
95 ike_sa_entry_t
*this = allocator_alloc_thing(ike_sa_entry_t
);
97 /* destroy function */
98 this->destroy
= ike_sa_entry_destroy
;
100 this->waiting_threads
= 0;
101 pthread_cond_init(&(this->condvar
), NULL
);
103 /* we set checkout flag when we really give it out */
104 this->checked_out
= FALSE
;
105 this->driveout_new_threads
= FALSE
;
106 this->driveout_waiting_threads
= FALSE
;
108 /* ike_sa_id is always cloned */
109 ike_sa_id
->clone(ike_sa_id
, &(this->ike_sa_id
));
110 this->ike_sa
= ike_sa_create(ike_sa_id
);
115 * Additional private members to ike_sa_manager_t
117 typedef struct private_ike_sa_manager_s private_ike_sa_manager_t
;
118 struct private_ike_sa_manager_s
{
122 ike_sa_manager_t
public;
125 * @brief get next spi
127 * we give out SPIs incremental
129 * @param this the ike_sa_manager
130 * @param spi[out] spi will be written here
131 * @return SUCCESS or,
132 * OUT_OF_RES when we already served 2^64 SPIs ;-)
134 status_t (*get_next_spi
) (private_ike_sa_manager_t
*this, u_int64_t
*spi
);
137 * @brief find the ike_sa_entry in the list by SPIs
139 * This function simply iterates over the linked list. A hash-table
140 * would be more efficient when storing a lot of IKE_SAs...
142 * @param this the ike_sa_manager containing the list
143 * @param ike_sa_id id of the ike_sa, containing SPIs
144 * @param entry[out] pointer to set to the found entry
146 * - SUCCESS when found,
147 * - NOT_FOUND when no such ike_sa_id in list
150 status_t (*get_entry_by_id
) (private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
, ike_sa_entry_t
**entry
);
153 * @brief find the ike_sa_entry in the list by pointer to SA.
155 * This function simply iterates over the linked list. A hash-table
156 * would be more efficient when storing a lot of IKE_SAs...
158 * @param this the ike_sa_manager containing the list
159 * @param ike_sa pointer to the ike_sa
160 * @param entry[out] pointer to set to the found entry
162 * - SUCCESS when found,
163 * - NOT_FOUND when no such ike_sa_id in list
166 status_t (*get_entry_by_sa
) (private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, ike_sa_entry_t
**entry
);
169 * @brief delete an entry from the linked list
171 * @param this the ike_sa_manager containing the list
172 * @param entry entry to delete
174 * - SUCCESS when found,
175 * - NOT_FOUND when no such ike_sa_id in list
177 status_t (*delete_entry
) (private_ike_sa_manager_t
*this, ike_sa_entry_t
*entry
);
180 * lock for exclusivly accessing the manager
182 pthread_mutex_t mutex
;
185 * Logger used for this IKE SA Manager
190 * Linked list with entries for the ike_sa
194 * Next SPI, needed for incremental creation of SPIs
201 * @see private_ike_sa_manager_t.get_entry_by_id
203 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
)
205 linked_list_t
*list
= this->list
;
206 linked_list_iterator_t
*iterator
;
208 status
= list
->create_iterator(list
, &iterator
, TRUE
);
209 if (status
!= SUCCESS
)
217 while (iterator
->has_next(iterator
))
219 ike_sa_entry_t
*current
;
220 bool are_equal
= FALSE
;
221 iterator
->current(iterator
, (void**)¤t
);
222 current
->ike_sa_id
->equals(current
->ike_sa_id
, ike_sa_id
, &are_equal
);
231 iterator
->destroy(iterator
);
236 * @see private_ike_sa_manager_t.get_entry_by_sa
238 static status_t
get_entry_by_sa(private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, ike_sa_entry_t
**entry
)
240 linked_list_t
*list
= this->list
;
241 linked_list_iterator_t
*iterator
;
243 status
= list
->create_iterator(list
, &iterator
, TRUE
);
244 if (status
!= SUCCESS
)
252 while (iterator
->has_next(iterator
))
254 ike_sa_entry_t
*current
;
255 iterator
->current(iterator
, (void**)¤t
);
256 /* only pointers are compared */
257 if (current
->ike_sa
== ike_sa
)
264 iterator
->destroy(iterator
);
269 * @see private_ike_sa_manager_s.delete_entry
271 static status_t
delete_entry(private_ike_sa_manager_t
*this, ike_sa_entry_t
*entry
)
273 linked_list_t
*list
= this->list
;
274 linked_list_iterator_t
*iterator
;
275 list
->create_iterator(list
, &iterator
, TRUE
);
276 while (iterator
->has_next(iterator
))
278 ike_sa_entry_t
*current
;
279 iterator
->current(iterator
, (void**)¤t
);
280 if (current
== entry
)
282 list
->remove(list
, iterator
);
283 entry
->destroy(entry
);
284 iterator
->destroy(iterator
);
288 iterator
->destroy(iterator
);
294 * @see private_ike_sa_manager_t.get_next_spi
296 static status_t
get_next_spi(private_ike_sa_manager_t
*this, u_int64_t
*spi
)
299 if (this->next_spi
== 0) {
300 /* our software ran so incredible stable, we have no more
301 * SPIs to give away :-/. */
304 *spi
= this->next_spi
;
309 * @see ike_sa_manager_s.checkout_ike_sa
311 static status_t
checkout(private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
, ike_sa_t
**ike_sa
)
313 bool responder_spi_set
;
314 bool initiator_spi_set
;
317 pthread_mutex_lock(&(this->mutex
));
319 responder_spi_set
= ike_sa_id
->responder_spi_is_set(ike_sa_id
);
320 initiator_spi_set
= ike_sa_id
->initiator_spi_is_set(ike_sa_id
);
322 if (initiator_spi_set
&& responder_spi_set
)
324 /* we SHOULD have an IKE_SA for these SPIs in the list,
325 * if not, we can't handle the request...
327 ike_sa_entry_t
*entry
;
328 /* look for the entry */
329 if (this->get_entry_by_id(this, ike_sa_id
, &entry
) == SUCCESS
)
331 /* can we give this out to new requesters?*/
332 if (entry
->driveout_new_threads
)
334 this->logger
->log(this->logger
,CONTROL_MORE
,"Drive out new thread for existing IKE_SA");
340 /* is this IKE_SA already checked out ??
341 * are we welcome to get this SA ? */
342 while (entry
->checked_out
&& !entry
->driveout_waiting_threads
)
344 /* so wait until we can get it for us.
345 * we register us as waiting.
347 entry
->waiting_threads
++;
348 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
349 entry
->waiting_threads
--;
352 /* hm, a deletion request forbids us to get this SA, go home */
353 if (entry
->driveout_waiting_threads
)
355 /* we must signal here, others are interested that we leave */
356 pthread_cond_signal(&(entry
->condvar
));
357 this->logger
->log(this->logger
,CONTROL_MORE
,"Drive out waiting thread for existing IKE_SA");
362 /* ok, this IKE_SA is finally ours */
363 entry
->checked_out
= TRUE
;
364 *ike_sa
= entry
->ike_sa
;
365 /* DON'T use return, we must unlock the mutex! */
372 /* looks like there is no such IKE_SA, better luck next time... */
373 /* DON'T use return, we must unlock the mutex! */
377 else if (initiator_spi_set
&& !responder_spi_set
)
379 /* an IKE_SA_INIT from an another endpoint,
380 * he is the initiator.
381 * For simplicity, we do NOT check for retransmitted
382 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
383 * Request (even a retransmitted one) will result in a
384 * IKE_SA. This could be improved...
386 u_int64_t responder_spi
;
387 ike_sa_entry_t
*new_ike_sa_entry
;
389 /* set SPIs, we are the responder */
390 retval
= this->get_next_spi(this, &responder_spi
);
392 if (retval
== SUCCESS
)
393 { /* next SPI could be created */
395 /* we also set arguments spi, so its still valid */
396 ike_sa_id
->set_responder_spi(ike_sa_id
, responder_spi
);
399 new_ike_sa_entry
= ike_sa_entry_create(ike_sa_id
);
400 if (new_ike_sa_entry
!= NULL
)
402 retval
= this->list
->insert_last(this->list
, new_ike_sa_entry
);
403 if (retval
== SUCCESS
)
405 /* check ike_sa out */
406 new_ike_sa_entry
->checked_out
= TRUE
;
407 *ike_sa
= new_ike_sa_entry
->ike_sa
;
409 /* DON'T use return, we must unlock the mutex! */
413 /* ike_sa_entry could not be added to list*/
414 this->logger
->log(this->logger
,CONTROL
,"Fatal error: ike_sa_entry could not be added to list");
419 /* new ike_sa_entry could not be created */
420 this->logger
->log(this->logger
,CONTROL
,"Fatal error: ike_sa_entry could not be created");
424 { /* next SPI could not be created */
425 this->logger
->log(this->logger
,CONTROL
,"Fatal error: Next SPI could not be created");
429 else if (!initiator_spi_set
&& !responder_spi_set
)
431 /* creation of an IKE_SA from local site,
432 * we are the initiator!
434 u_int64_t initiator_spi
;
435 ike_sa_entry_t
*new_ike_sa_entry
;
437 retval
= this->get_next_spi(this, &initiator_spi
);
438 if (retval
== SUCCESS
)
440 /* we also set arguments SPI, so its still valid */
441 ike_sa_id
->set_initiator_spi(ike_sa_id
, initiator_spi
);
444 new_ike_sa_entry
= ike_sa_entry_create(ike_sa_id
);
445 if (new_ike_sa_entry
!= NULL
)
447 retval
= this->list
->insert_last(this->list
, new_ike_sa_entry
);
449 if (retval
== SUCCESS
)
451 /* check ike_sa out */
452 new_ike_sa_entry
->checked_out
= TRUE
;
453 *ike_sa
= new_ike_sa_entry
->ike_sa
;
455 /* DON'T use return, we must unlock the mutex! */
459 /* ike_sa_entry could not be added to list*/
460 this->logger
->log(this->logger
,CONTROL
,"Fatal error: ike_sa_entry could not be added to list");
465 /* new ike_sa_entry could not be created */
466 this->logger
->log(this->logger
,CONTROL
,"Fatal error: ike_sa_entry could not be created");
471 /* next SPI could not be created */
472 this->logger
->log(this->logger
,CONTROL
,"Fatal error: Next SPI could not be created");
479 /* responder set, initiator not: here is something seriously wrong! */
480 this->logger
->log(this->logger
,CONTROL_MORE
,"Invalid IKE_SA SPI's");
481 /* DON'T use return, we must unlock the mutex! */
482 retval
= INVALID_ARG
;
485 pthread_mutex_unlock(&(this->mutex
));
486 /* OK, unlocked... */
491 * Implements ike_sa_manager_t-function checkin.
492 * @see ike_sa_manager_t.checkin.
494 static status_t
checkin(private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
496 /* to check the SA back in, we look for the pointer of the ike_sa
498 * We can't search by SPI's since the MAY have changed (e.g. on reception
499 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
502 ike_sa_entry_t
*entry
;
504 pthread_mutex_lock(&(this->mutex
));
506 /* look for the entry */
507 if (this->get_entry_by_sa(this, ike_sa
, &entry
) == SUCCESS
)
509 /* ike_sa_id must be updated */
510 entry
->ike_sa_id
->replace_values(entry
->ike_sa_id
, ike_sa
->get_id(ike_sa
));
511 /* signal waiting threads */
512 entry
->checked_out
= FALSE
;
513 pthread_cond_signal(&(entry
->condvar
));
518 this->logger
->log(this->logger
,CONTROL
,"Fatal Error: Tried to checkin nonexisting IKE_SA");
519 /* this SA is no more, this REALLY should not happen */
522 pthread_mutex_unlock(&(this->mutex
));
528 * Implements ike_sa_manager_t-function checkin_and_delete.
529 * @see ike_sa_manager_t.checkin_and_delete.
531 static status_t
checkin_and_delete(private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
533 /* deletion is a bit complex, we must garant that no thread is waiting for
535 * We take this SA from the list, and start signaling while threads
536 * are in the condvar.
538 ike_sa_entry_t
*entry
;
541 pthread_mutex_lock(&(this->mutex
));
543 if (this->get_entry_by_sa(this, ike_sa
, &entry
) == SUCCESS
)
545 /* mark it, so now new threads can acquire this SA */
546 entry
->driveout_new_threads
= TRUE
;
547 /* additionaly, drive out waiting threads */
548 entry
->driveout_waiting_threads
= TRUE
;
550 /* wait until all workers have done their work */
551 while (entry
->waiting_threads
> 0)
553 /* let the other threads do some work*/
554 pthread_cond_signal(&(entry
->condvar
));
555 /* and the nice thing, they will wake us again when their work is done */
556 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
558 /* ok, we are alone now, no threads waiting in the entry's condvar */
559 this->delete_entry(this, entry
);
564 this->logger
->log(this->logger
,CONTROL
,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA");
568 pthread_mutex_unlock(&(this->mutex
));
573 * Implements ike_sa_manager_t-function delete.
574 * @see ike_sa_manager_t.delete.
576 static status_t
delete(private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
578 /* deletion is a bit complex, we must garant that no thread is waiting for
580 * We take this SA from the list, and start signaling while threads
581 * are in the condvar.
583 ike_sa_entry_t
*entry
;
586 pthread_mutex_lock(&(this->mutex
));
588 if (this->get_entry_by_id(this, ike_sa_id
, &entry
) == SUCCESS
)
590 /* mark it, so now new threads can acquire this SA */
591 entry
->driveout_new_threads
= TRUE
;
593 /* wait until all workers have done their work */
594 while (entry
->waiting_threads
)
597 pthread_cond_signal(&(entry
->condvar
));
598 /* and the nice thing, they will wake us again when their work is done */
599 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
601 /* ok, we are alone now, no threads waiting in the entry's condvar */
602 this->delete_entry(this, entry
);
607 this->logger
->log(this->logger
,CONTROL
,"Fatal Error: Tried to delete nonexisting IKE_SA");
611 pthread_mutex_unlock(&(this->mutex
));
616 * Implements ike_sa_manager_t-function destroy.
617 * @see ike_sa_manager_t.destroy.
619 static status_t
destroy(private_ike_sa_manager_t
*this)
621 /* destroy all list entries */
622 linked_list_t
*list
= this->list
;
623 linked_list_iterator_t
*iterator
;
626 pthread_mutex_lock(&(this->mutex
));
628 /* Step 1: drive out all waiting threads */
629 status
= list
->create_iterator(list
, &iterator
, TRUE
);
631 if (status
!= SUCCESS
)
633 this->logger
->log(this->logger
,CONTROL
,"Fatal Error: Out of Ressources to greate interator while destroying IKE_SA-Manager");
637 while (iterator
->has_next(iterator
))
639 ike_sa_entry_t
*entry
;
640 iterator
->current(iterator
, (void**)&entry
);
641 /* do not accept new threads, drive out waiting threads */
642 entry
->driveout_new_threads
= TRUE
;
643 entry
->driveout_waiting_threads
= TRUE
;
645 /* Step 2: wait until all are gone */
646 iterator
->reset(iterator
);
647 while (iterator
->has_next(iterator
))
649 ike_sa_entry_t
*entry
;
650 iterator
->current(iterator
, (void**)&entry
);
651 while (entry
->waiting_threads
)
654 pthread_cond_signal(&(entry
->condvar
));
655 /* go sleeping until they are gone */
656 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
659 /* Step 3: delete all entries */
660 iterator
->reset(iterator
);
661 while (iterator
->has_next(iterator
))
663 ike_sa_entry_t
*entry
;
664 iterator
->current(iterator
, (void**)&entry
);
665 this->delete_entry(this, entry
);
667 iterator
->destroy(iterator
);
670 pthread_mutex_unlock(&(this->mutex
));
671 allocator_free(this);
677 * Described in header
679 ike_sa_manager_t
*ike_sa_manager_create()
681 private_ike_sa_manager_t
*this = allocator_alloc_thing(private_ike_sa_manager_t
);
683 /* assign public functions */
684 this->public.destroy
= (status_t(*)(ike_sa_manager_t
*))destroy
;
685 this->public.checkout
= (status_t(*)(ike_sa_manager_t
*, ike_sa_id_t
*sa_id
, ike_sa_t
**sa
))checkout
;
686 this->public.checkin
= (status_t(*)(ike_sa_manager_t
*, ike_sa_t
*sa
))checkin
;
687 this->public.delete = (status_t(*)(ike_sa_manager_t
*, ike_sa_id_t
*sa_id
))delete;
688 this->public.checkin_and_delete
= (status_t(*)(ike_sa_manager_t
*, ike_sa_t
*ike_sa
))checkin_and_delete
;
690 /* initialize private functions */
691 this->get_next_spi
= get_next_spi
;
692 this->get_entry_by_sa
= get_entry_by_sa
;
693 this->get_entry_by_id
= get_entry_by_id
;
694 this->delete_entry
= delete_entry
;
696 /* initialize private variables */
697 this->list
= linked_list_create();
698 if (this->list
== NULL
)
700 allocator_free(this);
704 this->logger
= global_logger_manager
->create_logger(global_logger_manager
,IKE_SA_MANAGER
,NULL
);
705 if (this->logger
== NULL
)
707 this->list
->destroy(this->list
);
708 allocator_free(this);
712 pthread_mutex_init(&(this->mutex
), NULL
);
716 return (ike_sa_manager_t
*)this;