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 /* also destroy IKE SA */
80 this->ike_sa
->destroy(this->ike_sa
);
81 this->ike_sa_id
->destroy(this->ike_sa_id
);
87 * @brief creates a new entry for the ike_sa list
89 * This constructor additionaly creates a new and empty SA
91 * @param ike_sa_id the associated ike_sa_id_t, will be cloned
92 * @return created entry, with ike_sa and ike_sa_id
94 static ike_sa_entry_t
*ike_sa_entry_create(ike_sa_id_t
*ike_sa_id
)
96 ike_sa_entry_t
*this = allocator_alloc_thing(ike_sa_entry_t
);
103 /* destroy function */
104 this->destroy
= ike_sa_entry_destroy
;
106 this->waiting_threads
= 0;
107 pthread_cond_init(&(this->condvar
), NULL
);
109 /* we set checkout flag when we really give it out */
110 this->checked_out
= FALSE
;
111 this->driveout_new_threads
= FALSE
;
112 this->driveout_waiting_threads
= FALSE
;
114 /* ike_sa_id is always cloned */
115 ike_sa_id
->clone(ike_sa_id
, &(this->ike_sa_id
));
116 if (this->ike_sa_id
== NULL
)
118 allocator_free(this);
122 /* create new ike_sa */
123 this->ike_sa
= ike_sa_create(ike_sa_id
);
124 if (this->ike_sa
== NULL
)
126 this->ike_sa_id
->destroy(this->ike_sa_id
);
127 allocator_free(this);
135 * Additional private members to ike_sa_manager_t
137 typedef struct private_ike_sa_manager_s private_ike_sa_manager_t
;
138 struct private_ike_sa_manager_s
{
142 ike_sa_manager_t
public;
145 * @brief get next spi
147 * we give out SPIs incremental
149 * @param this the ike_sa_manager
150 * @param spi[out] spi will be written here
151 * @return SUCCESS or,
152 * OUT_OF_RES when we already served 2^64 SPIs ;-)
154 status_t (*get_next_spi
) (private_ike_sa_manager_t
*this, u_int64_t
*spi
);
157 * @brief find the ike_sa_entry in the list by SPIs
159 * This function simply iterates over the linked list. A hash-table
160 * would be more efficient when storing a lot of IKE_SAs...
162 * @param this the ike_sa_manager containing the list
163 * @param ike_sa_id id of the ike_sa, containing SPIs
164 * @param entry[out] pointer to set to the found entry
166 * - SUCCESS when found,
167 * - NOT_FOUND when no such ike_sa_id in list
170 status_t (*get_entry_by_id
) (private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
, ike_sa_entry_t
**entry
);
173 * @brief find the ike_sa_entry in the list by pointer to SA.
175 * This function simply iterates over the linked list. A hash-table
176 * would be more efficient when storing a lot of IKE_SAs...
178 * @param this the ike_sa_manager containing the list
179 * @param ike_sa pointer to the ike_sa
180 * @param entry[out] pointer to set to the found entry
182 * - SUCCESS when found,
183 * - NOT_FOUND when no such ike_sa_id in list
186 status_t (*get_entry_by_sa
) (private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, ike_sa_entry_t
**entry
);
189 * @brief delete an entry from the linked list
191 * @param this the ike_sa_manager containing the list
192 * @param entry entry to delete
194 * - SUCCESS when found,
195 * - NOT_FOUND when no such ike_sa_id in list
197 status_t (*delete_entry
) (private_ike_sa_manager_t
*this, ike_sa_entry_t
*entry
);
200 * lock for exclusivly accessing the manager
202 pthread_mutex_t mutex
;
205 * Logger used for this IKE SA Manager
210 * Linked list with entries for the ike_sa
212 linked_list_t
*ike_sa_list
;
215 * Next SPI, needed for incremental creation of SPIs
222 * @see private_ike_sa_manager_t.get_entry_by_id
224 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
)
226 linked_list_t
*list
= this->ike_sa_list
;
227 linked_list_iterator_t
*iterator
;
230 /* create iterator over list of ike_sa's */
231 status
= list
->create_iterator(list
, &iterator
, TRUE
);
232 if (status
!= SUCCESS
)
234 this->logger
->log(this->logger
,ERROR
,"Fatal Error: could not create iterator: %s",mapping_find(status_m
,status
));
243 while (iterator
->has_next(iterator
))
245 ike_sa_entry_t
*current
;
246 bool are_equal
= FALSE
;
248 iterator
->current(iterator
, (void**)¤t
);
249 if (current
->ike_sa_id
->get_responder_spi(current
->ike_sa_id
) == 0) {
250 /* seems to be a half ready ike_sa */
251 if ((current
->ike_sa_id
->get_initiator_spi(current
->ike_sa_id
) == ike_sa_id
->get_initiator_spi(ike_sa_id
))
252 && (ike_sa_id
->is_initiator(ike_sa_id
) == current
->ike_sa_id
->is_initiator(current
->ike_sa_id
)))
254 this->logger
->log(this->logger
,CONTROL
| MOST
,"Found entry by initiator spi %d",ike_sa_id
->get_initiator_spi(ike_sa_id
));
260 current
->ike_sa_id
->equals(current
->ike_sa_id
, ike_sa_id
, &are_equal
);
263 this->logger
->log(this->logger
,CONTROL
| MOST
,"Found entry by full ID");
270 iterator
->destroy(iterator
);
275 * @see private_ike_sa_manager_t.get_entry_by_sa
277 static status_t
get_entry_by_sa(private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, ike_sa_entry_t
**entry
)
279 linked_list_t
*list
= this->ike_sa_list
;
280 linked_list_iterator_t
*iterator
;
283 status
= list
->create_iterator(list
, &iterator
, TRUE
);
284 if (status
!= SUCCESS
)
286 this->logger
->log(this->logger
,ERROR
,"Fatal Error: could not create iterator: %s",mapping_find(status_m
,status
));
293 while (iterator
->has_next(iterator
))
295 ike_sa_entry_t
*current
;
296 iterator
->current(iterator
, (void**)¤t
);
297 /* only pointers are compared */
298 if (current
->ike_sa
== ike_sa
)
300 this->logger
->log(this->logger
,CONTROL
| MOST
,"Found entry by pointer");
306 iterator
->destroy(iterator
);
311 * @see private_ike_sa_manager_s.delete_entry
313 static status_t
delete_entry(private_ike_sa_manager_t
*this, ike_sa_entry_t
*entry
)
315 linked_list_t
*list
= this->ike_sa_list
;
316 linked_list_iterator_t
*iterator
;
319 status
= list
->create_iterator(list
, &iterator
, TRUE
);
321 if (status
!= SUCCESS
)
323 this->logger
->log(this->logger
,ERROR
,"Fatal Error: could not create iterator: %s",mapping_find(status_m
,status
));
329 while (iterator
->has_next(iterator
))
331 ike_sa_entry_t
*current
;
332 iterator
->current(iterator
, (void**)¤t
);
333 if (current
== entry
)
335 this->logger
->log(this->logger
,CONTROL
| MOST
,"Found entry by pointer. Going to delete it.");
336 list
->remove(list
, iterator
);
337 entry
->destroy(entry
);
342 iterator
->destroy(iterator
);
348 * @see private_ike_sa_manager_t.get_next_spi
350 static status_t
get_next_spi(private_ike_sa_manager_t
*this, u_int64_t
*spi
)
353 if (this->next_spi
== 0) {
354 /* our software ran so incredible stable, we have no more
355 * SPIs to give away :-/. */
356 this->logger
->log(this->logger
,CONTROL
| MOST
,"No more SPI values available. WOW!");
359 *spi
= this->next_spi
;
364 * @see ike_sa_manager_s.checkout_ike_sa
366 static status_t
checkout(private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
, ike_sa_t
**ike_sa
)
368 bool responder_spi_set
;
369 bool initiator_spi_set
;
372 /* each access is locked */
373 pthread_mutex_lock(&(this->mutex
));
375 responder_spi_set
= (FALSE
!= ike_sa_id
->get_responder_spi(ike_sa_id
));
376 initiator_spi_set
= (FALSE
!= ike_sa_id
->get_initiator_spi(ike_sa_id
));
378 if (initiator_spi_set
&& responder_spi_set
)
380 /* we SHOULD have an IKE_SA for these SPIs in the list,
381 * if not, we can't handle the request...
383 ike_sa_entry_t
*entry
;
384 /* look for the entry */
385 if (this->get_entry_by_id(this, ike_sa_id
, &entry
) == SUCCESS
)
387 /* can we give this ike_sa out to new requesters?*/
388 if (entry
->driveout_new_threads
)
390 this->logger
->log(this->logger
,CONTROL
|MORE
,"Drive out new thread for existing IKE_SA");
396 /* is this IKE_SA already checked out ??
397 * are we welcome to get this SA ? */
398 while (entry
->checked_out
&& !entry
->driveout_waiting_threads
)
400 /* so wait until we can get it for us.
401 * we register us as waiting.
403 entry
->waiting_threads
++;
404 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
405 entry
->waiting_threads
--;
408 /* hm, a deletion request forbids us to get this SA, go home */
409 if (entry
->driveout_waiting_threads
)
411 /* we must signal here, others are interested that we leave */
412 pthread_cond_signal(&(entry
->condvar
));
413 this->logger
->log(this->logger
,CONTROL
|MORE
,"Drive out waiting thread for existing IKE_SA");
418 this->logger
->log(this->logger
,CONTROL
|MOST
,"IKE SA successfully checked out");
419 /* ok, this IKE_SA is finally ours */
420 entry
->checked_out
= TRUE
;
421 *ike_sa
= entry
->ike_sa
;
422 /* DON'T use return, we must unlock the mutex! */
429 this->logger
->log(this->logger
,ERROR
| MORE
,"IKE SA not stored in known IKE_SA list");
430 /* looks like there is no such IKE_SA, better luck next time... */
431 /* DON'T use return, we must unlock the mutex! */
435 else if (initiator_spi_set
&& !responder_spi_set
)
437 /* an IKE_SA_INIT from an another endpoint,
438 * he is the initiator.
439 * For simplicity, we do NOT check for retransmitted
440 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
441 * Request (even a retransmitted one) will result in a
442 * IKE_SA. This could be improved...
444 u_int64_t responder_spi
;
445 ike_sa_entry_t
*new_ike_sa_entry
;
448 /* set SPIs, we are the responder */
449 retval
= this->get_next_spi(this, &responder_spi
);
451 if (retval
== SUCCESS
)
452 { /* next SPI could be created */
454 /* we also set arguments spi, so its still valid */
455 ike_sa_id
->set_responder_spi(ike_sa_id
, responder_spi
);
458 new_ike_sa_entry
= ike_sa_entry_create(ike_sa_id
);
459 if (new_ike_sa_entry
!= NULL
)
461 retval
= this->ike_sa_list
->insert_last(this->ike_sa_list
, new_ike_sa_entry
);
462 if (retval
== SUCCESS
)
464 /* check ike_sa out */
465 this->logger
->log(this->logger
,CONTROL
| MORE
,"IKE_SA added to list of known IKE_SA's");
466 new_ike_sa_entry
->checked_out
= TRUE
;
467 *ike_sa
= new_ike_sa_entry
->ike_sa
;
469 /* DON'T use return, we must unlock the mutex! */
473 /* ike_sa_entry could not be added to list*/
474 this->logger
->log(this->logger
,ERROR
,"Fatal error: ike_sa_entry could not be added to list");
479 /* new ike_sa_entry could not be created */
480 this->logger
->log(this->logger
,ERROR
,"Fatal error: ike_sa_entry could not be created");
484 { /* next SPI could not be created */
485 this->logger
->log(this->logger
,ERROR
,"Fatal error: Next SPI could not be created");
489 else if (!initiator_spi_set
&& !responder_spi_set
)
491 /* creation of an IKE_SA from local site,
492 * we are the initiator!
494 u_int64_t initiator_spi
;
495 ike_sa_entry_t
*new_ike_sa_entry
;
497 retval
= this->get_next_spi(this, &initiator_spi
);
498 if (retval
== SUCCESS
)
500 /* we also set arguments SPI, so its still valid */
501 ike_sa_id
->set_initiator_spi(ike_sa_id
, initiator_spi
);
504 new_ike_sa_entry
= ike_sa_entry_create(ike_sa_id
);
505 if (new_ike_sa_entry
!= NULL
)
507 retval
= this->ike_sa_list
->insert_last(this->ike_sa_list
, new_ike_sa_entry
);
509 if (retval
== SUCCESS
)
511 /* check ike_sa out */
512 this->logger
->log(this->logger
,CONTROL
| MORE
,"New IKE_SA created and added to list of known IKE_SA's");
513 new_ike_sa_entry
->checked_out
= TRUE
;
514 *ike_sa
= new_ike_sa_entry
->ike_sa
;
516 /* DON'T use return, we must unlock the mutex! */
520 /* ike_sa_entry could not be added to list*/
521 this->logger
->log(this->logger
,ERROR
,"Fatal error: ike_sa_entry could not be added to list");
526 /* new ike_sa_entry could not be created */
527 this->logger
->log(this->logger
,ERROR
,"Fatal error: ike_sa_entry could not be created");
532 /* next SPI could not be created */
533 this->logger
->log(this->logger
,ERROR
,"Fatal error: Next SPI could not be created");
540 /* responder set, initiator not: here is something seriously wrong! */
541 this->logger
->log(this->logger
,ERROR
| MORE
,"Invalid IKE_SA SPI's");
542 /* DON'T use return, we must unlock the mutex! */
543 retval
= INVALID_ARG
;
546 pthread_mutex_unlock(&(this->mutex
));
547 /* OK, unlocked... */
552 * Implements ike_sa_manager_t-function checkin.
553 * @see ike_sa_manager_t.checkin.
555 static status_t
checkin(private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
557 /* to check the SA back in, we look for the pointer of the ike_sa
559 * We can't search by SPI's since the MAY have changed (e.g. on reception
560 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
563 ike_sa_entry_t
*entry
;
565 pthread_mutex_lock(&(this->mutex
));
567 /* look for the entry */
568 if (this->get_entry_by_sa(this, ike_sa
, &entry
) == SUCCESS
)
570 /* ike_sa_id must be updated */
571 entry
->ike_sa_id
->replace_values(entry
->ike_sa_id
, ike_sa
->get_id(ike_sa
));
572 /* signal waiting threads */
573 entry
->checked_out
= FALSE
;
574 this->logger
->log(this->logger
,CONTROL
| MORE
,"Checkin of IKE_SA successful.");
575 pthread_cond_signal(&(entry
->condvar
));
580 this->logger
->log(this->logger
,ERROR
,"Fatal Error: Tried to checkin nonexisting IKE_SA");
581 /* this SA is no more, this REALLY should not happen */
584 pthread_mutex_unlock(&(this->mutex
));
590 * Implements ike_sa_manager_t-function checkin_and_delete.
591 * @see ike_sa_manager_t.checkin_and_delete.
593 static status_t
checkin_and_delete(private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
595 /* deletion is a bit complex, we must garant that no thread is waiting for
597 * We take this SA from the list, and start signaling while threads
598 * are in the condvar.
600 ike_sa_entry_t
*entry
;
603 pthread_mutex_lock(&(this->mutex
));
605 if (this->get_entry_by_sa(this, ike_sa
, &entry
) == SUCCESS
)
607 /* mark it, so now new threads can acquire this SA */
608 entry
->driveout_new_threads
= TRUE
;
609 /* additionaly, drive out waiting threads */
610 entry
->driveout_waiting_threads
= TRUE
;
612 /* wait until all workers have done their work */
613 while (entry
->waiting_threads
> 0)
615 /* let the other threads do some work*/
616 pthread_cond_signal(&(entry
->condvar
));
617 /* and the nice thing, they will wake us again when their work is done */
618 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
620 /* ok, we are alone now, no threads waiting in the entry's condvar */
621 this->delete_entry(this, entry
);
622 this->logger
->log(this->logger
,CONTROL
| MORE
,"Checkin and delete of IKE_SA successful");
627 this->logger
->log(this->logger
,ERROR
,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA");
631 pthread_mutex_unlock(&(this->mutex
));
636 * Implements ike_sa_manager_t-function delete.
637 * @see ike_sa_manager_t.delete.
639 static status_t
delete(private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
641 /* deletion is a bit complex, we must garant that no thread is waiting for
643 * We take this SA from the list, and start signaling while threads
644 * are in the condvar.
646 ike_sa_entry_t
*entry
;
649 pthread_mutex_lock(&(this->mutex
));
651 if (this->get_entry_by_id(this, ike_sa_id
, &entry
) == SUCCESS
)
653 /* mark it, so now new threads can acquire this SA */
654 entry
->driveout_new_threads
= TRUE
;
656 /* wait until all workers have done their work */
657 while (entry
->waiting_threads
)
660 pthread_cond_signal(&(entry
->condvar
));
661 /* and the nice thing, they will wake us again when their work is done */
662 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
664 /* ok, we are alone now, no threads waiting in the entry's condvar */
665 this->delete_entry(this, entry
);
666 this->logger
->log(this->logger
,CONTROL
| MORE
,"Delete of IKE_SA successful");
671 this->logger
->log(this->logger
,ERROR
,"Fatal Error: Tried to delete nonexisting IKE_SA");
675 pthread_mutex_unlock(&(this->mutex
));
680 * Implements ike_sa_manager_t-function destroy.
681 * @see ike_sa_manager_t.destroy.
683 static status_t
destroy(private_ike_sa_manager_t
*this)
685 /* destroy all list entries */
686 linked_list_t
*list
= this->ike_sa_list
;
687 linked_list_iterator_t
*iterator
;
690 pthread_mutex_lock(&(this->mutex
));
692 this->logger
->log(this->logger
,CONTROL
| MORE
,"Going to destroy IKE_SA manager and all managed IKE_SA's");
694 /* Step 1: drive out all waiting threads */
695 status
= list
->create_iterator(list
, &iterator
, TRUE
);
697 if (status
!= SUCCESS
)
699 this->logger
->log(this->logger
,ERROR
,"Fatal Error: Create of iterator while destroying IKE_SA-Manager failed");
703 this->logger
->log(this->logger
,CONTROL
| MOST
,"Set driveout flags for all stored IKE_SA's");
704 while (iterator
->has_next(iterator
))
706 ike_sa_entry_t
*entry
;
707 iterator
->current(iterator
, (void**)&entry
);
708 /* do not accept new threads, drive out waiting threads */
709 entry
->driveout_new_threads
= TRUE
;
710 entry
->driveout_waiting_threads
= TRUE
;
713 this->logger
->log(this->logger
,CONTROL
| MOST
,"Wait for all threads to leave IKE_SA's");
714 /* Step 2: wait until all are gone */
715 iterator
->reset(iterator
);
716 while (iterator
->has_next(iterator
))
718 ike_sa_entry_t
*entry
;
719 iterator
->current(iterator
, (void**)&entry
);
720 while (entry
->waiting_threads
)
723 pthread_cond_signal(&(entry
->condvar
));
724 /* go sleeping until they are gone */
725 pthread_cond_wait(&(entry
->condvar
), &(this->mutex
));
728 this->logger
->log(this->logger
,CONTROL
| MOST
,"Delete all IKE_SA's");
729 /* Step 3: delete all entries */
730 iterator
->reset(iterator
);
731 while (iterator
->has_next(iterator
))
733 ike_sa_entry_t
*entry
;
734 iterator
->current(iterator
, (void**)&entry
);
735 this->delete_entry(this, entry
);
737 iterator
->destroy(iterator
);
740 pthread_mutex_unlock(&(this->mutex
));
742 /* destroy logger at end */
743 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
744 allocator_free(this);
750 * Described in header
752 ike_sa_manager_t
*ike_sa_manager_create()
754 private_ike_sa_manager_t
*this = allocator_alloc_thing(private_ike_sa_manager_t
);
756 /* assign public functions */
757 this->public.destroy
= (status_t(*)(ike_sa_manager_t
*))destroy
;
758 this->public.checkout
= (status_t(*)(ike_sa_manager_t
*, ike_sa_id_t
*sa_id
, ike_sa_t
**sa
))checkout
;
759 this->public.checkin
= (status_t(*)(ike_sa_manager_t
*, ike_sa_t
*sa
))checkin
;
760 this->public.delete = (status_t(*)(ike_sa_manager_t
*, ike_sa_id_t
*sa_id
))delete;
761 this->public.checkin_and_delete
= (status_t(*)(ike_sa_manager_t
*, ike_sa_t
*ike_sa
))checkin_and_delete
;
763 /* initialize private functions */
764 this->get_next_spi
= get_next_spi
;
765 this->get_entry_by_sa
= get_entry_by_sa
;
766 this->get_entry_by_id
= get_entry_by_id
;
767 this->delete_entry
= delete_entry
;
769 /* initialize private variables */
770 this->logger
= global_logger_manager
->create_logger(global_logger_manager
,IKE_SA_MANAGER
,NULL
);
771 if (this->logger
== NULL
)
773 allocator_free(this);
777 this->ike_sa_list
= linked_list_create();
778 if (this->ike_sa_list
== NULL
)
780 this->logger
->log(this->logger
,ERROR
,"Fatal error: Failed to create list for managed IKE_SA");
781 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
782 allocator_free(this);
786 pthread_mutex_init(&(this->mutex
), NULL
);
790 return (ike_sa_manager_t
*)this;