- refactored ike_sa_manager_t class
[strongswan.git] / Source / charon / ike_sa_manager.c
1 /**
2 * @file ike_sa_manager.c
3 *
4 * @brief Central point for managing IKE-SAs (creation, locking, deleting...)
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 "ike_sa_id.h"
29 #include "utils/allocator.h"
30 #include "utils/logger.h"
31 #include "utils/logger_manager.h"
32 #include "utils/linked_list.h"
33
34 extern logger_manager_t *global_logger_manager;
35
36 /**
37 * @brief An entry in the linked list, contains IKE_SA, locking and lookup data.
38 */
39 typedef struct ike_sa_entry_s ike_sa_entry_t;
40 struct ike_sa_entry_s {
41 /**
42 * destructor, also destroys ike_sa
43 */
44 status_t (*destroy) (ike_sa_entry_t *this);
45 /**
46 * Number of threads waiting for this ike_sa
47 */
48 int waiting_threads;
49 /**
50 * condvar where threads can wait until it's free again
51 */
52 pthread_cond_t condvar;
53 /**
54 * is this ike_sa currently checked out?
55 */
56 bool checked_out;
57 /**
58 * Does this SA drives out new threads?
59 */
60 bool driveout_new_threads;
61 /**
62 * Does this SA drives out waiting threads?
63 */
64 bool driveout_waiting_threads;
65 /**
66 * identifiaction of ike_sa (SPIs)
67 */
68 ike_sa_id_t *ike_sa_id;
69 /**
70 * the contained ike_sa
71 */
72 ike_sa_t *ike_sa;
73 };
74
75 /**
76 * @see ike_sa_entry_t.destroy
77 */
78 static status_t ike_sa_entry_destroy(ike_sa_entry_t *this)
79 {
80 this->ike_sa->destroy(this->ike_sa);
81 this->ike_sa_id->destroy(this->ike_sa_id);
82 allocator_free(this);
83 return SUCCESS;
84 }
85
86 /**
87 * @brief creates a new entry for the ike_sa list
88 *
89 * This constructor additionaly creates a new and empty SA
90 *
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
93 */
94 static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id)
95 {
96 ike_sa_entry_t *this = allocator_alloc_thing(ike_sa_entry_t);
97
98 /* destroy function */
99 this->destroy = ike_sa_entry_destroy;
100
101 this->waiting_threads = 0;
102 pthread_cond_init(&(this->condvar), NULL);
103
104 /* we set checkout flag when we really give it out */
105 this->checked_out = FALSE;
106 this->driveout_new_threads = FALSE;
107 this->driveout_waiting_threads = FALSE;
108
109 /* ike_sa_id is always cloned */
110 ike_sa_id->clone(ike_sa_id, &(this->ike_sa_id));
111 this->ike_sa = ike_sa_create(ike_sa_id);
112 return this;
113 }
114
115 /**
116 * Additional private members to ike_sa_manager_t
117 */
118 typedef struct private_ike_sa_manager_s private_ike_sa_manager_t;
119 struct private_ike_sa_manager_s {
120 /**
121 * Public members
122 */
123 ike_sa_manager_t public;
124
125 /**
126 * @brief get next spi
127 *
128 * we give out SPIs incremental
129 *
130 * @param this the ike_sa_manager
131 * @param spi[out] spi will be written here
132 * @return SUCCESS or,
133 * OUT_OF_RES when we already served 2^64 SPIs ;-)
134 */
135 status_t (*get_next_spi) (private_ike_sa_manager_t *this, spi_t *spi);
136
137 /**
138 * @brief find the ike_sa_entry in the list by SPIs
139 *
140 * This function simply iterates over the linked list. A hash-table
141 * would be more efficient when storing a lot of IKE_SAs...
142 *
143 * @param this the ike_sa_manager containing the list
144 * @param ike_sa_id id of the ike_sa, containing SPIs
145 * @param entry[out] pointer to set to the found entry
146 * @return
147 * - SUCCESS when found,
148 * - NOT_FOUND when no such ike_sa_id in list
149 * - OUT_OF_RES
150 */
151 status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry);
152
153 /**
154 * @brief find the ike_sa_entry in the list by pointer to SA.
155 *
156 * This function simply iterates over the linked list. A hash-table
157 * would be more efficient when storing a lot of IKE_SAs...
158 *
159 * @param this the ike_sa_manager containing the list
160 * @param ike_sa pointer to the ike_sa
161 * @param entry[out] pointer to set to the found entry
162 * @return
163 * - SUCCESS when found,
164 * - NOT_FOUND when no such ike_sa_id in list
165 * - OUT_OF_RES
166 */
167 status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry);
168
169 /**
170 * @brief delete an entry from the linked list
171 *
172 * @param this the ike_sa_manager containing the list
173 * @param entry entry to delete
174 * @return
175 * - SUCCESS when found,
176 * - NOT_FOUND when no such ike_sa_id in list
177 */
178 status_t (*delete_entry) (private_ike_sa_manager_t *this, ike_sa_entry_t *entry);
179
180 /**
181 * lock for exclusivly accessing the manager
182 */
183 pthread_mutex_t mutex;
184
185 /**
186 * Logger used for this IKE SA Manager
187 */
188 logger_t *logger;
189
190 /**
191 * Linked list with entries for the ike_sa
192 */
193 linked_list_t *list;
194 /**
195 * Next SPI, needed for incremental creation of SPIs
196 */
197 spi_t next_spi;
198 };
199
200
201 /**
202 * @see private_ike_sa_manager_t.get_entry_by_id
203 */
204 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 {
206 linked_list_t *list = this->list;
207 linked_list_iterator_t *iterator;
208 status_t status;
209 status = list->create_iterator(list, &iterator, TRUE);
210 if (status != SUCCESS)
211 {
212 return status;
213 }
214
215 /* default status */
216 status = NOT_FOUND;
217
218 while (iterator->has_next(iterator))
219 {
220 ike_sa_entry_t *current;
221 bool are_equal = FALSE;
222 iterator->current(iterator, (void**)&current);
223 current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id, &are_equal);
224 if (are_equal)
225 {
226 *entry = current;
227 status = SUCCESS;
228 break;
229 }
230 }
231
232 iterator->destroy(iterator);
233 return status;
234 }
235
236 /**
237 * @see private_ike_sa_manager_t.get_entry_by_sa
238 */
239 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry)
240 {
241 linked_list_t *list = this->list;
242 linked_list_iterator_t *iterator;
243 status_t status;
244 status = list->create_iterator(list, &iterator, TRUE);
245 if (status != SUCCESS)
246 {
247 return status;
248 }
249
250 /* default status */
251 status = NOT_FOUND;
252
253 while (iterator->has_next(iterator))
254 {
255 ike_sa_entry_t *current;
256 iterator->current(iterator, (void**)&current);
257 /* only pointers are compared */
258 if (current->ike_sa == ike_sa)
259 {
260 *entry = current;
261 status = SUCCESS;
262 break;
263 }
264 }
265 iterator->destroy(iterator);
266 return status;
267 }
268
269 /**
270 * @see private_ike_sa_manager_s.delete_entry
271 */
272 static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry)
273 {
274 linked_list_t *list = this->list;
275 linked_list_iterator_t *iterator;
276 list->create_iterator(list, &iterator, TRUE);
277 while (iterator->has_next(iterator))
278 {
279 ike_sa_entry_t *current;
280 iterator->current(iterator, (void**)&current);
281 if (current == entry)
282 {
283 list->remove(list, iterator);
284 entry->destroy(entry);
285 iterator->destroy(iterator);
286 return SUCCESS;
287 }
288 }
289 iterator->destroy(iterator);
290 return NOT_FOUND;
291 }
292
293
294 /**
295 * @see private_ike_sa_manager_t.get_next_spi
296 */
297 static status_t get_next_spi(private_ike_sa_manager_t *this, spi_t *spi)
298 {
299 this->next_spi.low ++;
300 if (this->next_spi.low == 0) {
301 /* overflow of lower int in spi */
302 this->next_spi.high ++;
303 if (this->next_spi.high == 0) {
304 /* our software ran so incredible stable, we have no more
305 * SPIs to give away :-/. */
306 return OUT_OF_RES;
307 }
308 }
309 *spi = this->next_spi;
310 return SUCCESS;
311 }
312
313 /**
314 * @see ike_sa_manager_s.checkout_ike_sa
315 */
316 static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_t **ike_sa)
317 {
318 bool responder_spi_set;
319 bool initiator_spi_set;
320 status_t retval;
321
322 pthread_mutex_lock(&(this->mutex));
323
324 responder_spi_set = ike_sa_id->responder_spi_is_set(ike_sa_id);
325 initiator_spi_set = ike_sa_id->initiator_spi_is_set(ike_sa_id);
326
327 if (initiator_spi_set && responder_spi_set)
328 {
329 /* we SHOULD have an IKE_SA for these SPIs in the list,
330 * if not, we can't handle the request...
331 */
332 ike_sa_entry_t *entry;
333 /* look for the entry */
334 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
335 {
336 /* can we give this out to new requesters?*/
337 if (entry->driveout_new_threads)
338 {
339 this->logger->log(this->logger,CONTROL_MORE,"Drive out new thread for existing IKE_SA");
340 /* no we can't */
341 retval = NOT_FOUND;
342 }
343 else
344 {
345 /* is this IKE_SA already checked out ??
346 * are we welcome to get this SA ? */
347 while (entry->checked_out && !entry->driveout_waiting_threads)
348 {
349 /* so wait until we can get it for us.
350 * we register us as waiting.
351 */
352 entry->waiting_threads++;
353 pthread_cond_wait(&(entry->condvar), &(this->mutex));
354 entry->waiting_threads--;
355 }
356
357 /* hm, a deletion request forbids us to get this SA, go home */
358 if (entry->driveout_waiting_threads)
359 {
360 /* we must signal here, others are interested that we leave */
361 pthread_cond_signal(&(entry->condvar));
362 this->logger->log(this->logger,CONTROL_MORE,"Drive out waiting thread for existing IKE_SA");
363 retval = NOT_FOUND;
364 }
365 else
366 {
367 /* ok, this IKE_SA is finally ours */
368 entry->checked_out = TRUE;
369 *ike_sa = entry->ike_sa;
370 /* DON'T use return, we must unlock the mutex! */
371 retval = SUCCESS;
372 }
373 }
374 }
375 else
376 {
377 /* looks like there is no such IKE_SA, better luck next time... */
378 /* DON'T use return, we must unlock the mutex! */
379 retval = NOT_FOUND;
380 }
381 }
382 else if (initiator_spi_set && !responder_spi_set)
383 {
384 /* an IKE_SA_INIT from an another endpoint,
385 * he is the initiator.
386 * For simplicity, we do NOT check for retransmitted
387 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
388 * Request (even a retransmitted one) will result in a
389 * IKE_SA. This could be improved...
390 */
391 spi_t responder_spi;
392 ike_sa_entry_t *new_ike_sa_entry;
393
394 /* set SPIs, we are the responder */
395 retval = this->get_next_spi(this, &responder_spi);
396
397 if (retval == SUCCESS)
398 { /* next SPI could be created */
399
400 /* we also set arguments spi, so its still valid */
401 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
402
403 /* create entry */
404 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
405 if (new_ike_sa_entry != NULL)
406 {
407 retval = this->list->insert_last(this->list, new_ike_sa_entry);
408 if (retval == SUCCESS)
409 {
410 /* check ike_sa out */
411 new_ike_sa_entry->checked_out = TRUE;
412 *ike_sa = new_ike_sa_entry->ike_sa;
413
414 /* DON'T use return, we must unlock the mutex! */
415 }
416 else
417 {
418 /* ike_sa_entry could not be added to list*/
419 this->logger->log(this->logger,CONTROL,"Fatal error: ike_sa_entry could not be added to list");
420 }
421 }
422 else
423 {
424 /* new ike_sa_entry could not be created */
425 this->logger->log(this->logger,CONTROL,"Fatal error: ike_sa_entry could not be created");
426 }
427 }
428 else
429 { /* next SPI could not be created */
430 this->logger->log(this->logger,CONTROL,"Fatal error: Next SPI could not be created");
431 }
432
433 }
434 else if (!initiator_spi_set && !responder_spi_set)
435 {
436 /* creation of an IKE_SA from local site,
437 * we are the initiator!
438 */
439 spi_t initiator_spi;
440 ike_sa_entry_t *new_ike_sa_entry;
441
442 retval = this->get_next_spi(this, &initiator_spi);
443 if (retval == SUCCESS)
444 {
445 /* we also set arguments SPI, so its still valid */
446 ike_sa_id->set_initiator_spi(ike_sa_id, initiator_spi);
447
448 /* create entry */
449 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
450 if (new_ike_sa_entry != NULL)
451 {
452 retval = this->list->insert_last(this->list, new_ike_sa_entry);
453
454 if (retval == SUCCESS)
455 {
456 /* check ike_sa out */
457 new_ike_sa_entry->checked_out = TRUE;
458 *ike_sa = new_ike_sa_entry->ike_sa;
459
460 /* DON'T use return, we must unlock the mutex! */
461 }
462 else
463 {
464 /* ike_sa_entry could not be added to list*/
465 this->logger->log(this->logger,CONTROL,"Fatal error: ike_sa_entry could not be added to list");
466 }
467 }
468 else
469 {
470 /* new ike_sa_entry could not be created */
471 this->logger->log(this->logger,CONTROL,"Fatal error: ike_sa_entry could not be created");
472 }
473 }
474 else
475 {
476 /* next SPI could not be created */
477 this->logger->log(this->logger,CONTROL,"Fatal error: Next SPI could not be created");
478 }
479
480
481 }
482 else
483 {
484 /* responder set, initiator not: here is something seriously wrong! */
485 this->logger->log(this->logger,CONTROL_MORE,"Invalid IKE_SA SPI's");
486 /* DON'T use return, we must unlock the mutex! */
487 retval = INVALID_ARG;
488 }
489
490 pthread_mutex_unlock(&(this->mutex));
491 /* OK, unlocked... */
492 return retval;
493 }
494
495 /**
496 * Implements ike_sa_manager_t-function checkin.
497 * @see ike_sa_manager_t.checkin.
498 */
499 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
500 {
501 /* to check the SA back in, we look for the pointer of the ike_sa
502 * in all entries.
503 * We can't search by SPI's since the MAY have changed (e.g. on reception
504 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
505 */
506 status_t retval;
507 ike_sa_entry_t *entry;
508
509 pthread_mutex_lock(&(this->mutex));
510
511 /* look for the entry */
512 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
513 {
514 /* ike_sa_id must be updated */
515 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
516 /* signal waiting threads */
517 entry->checked_out = FALSE;
518 pthread_cond_signal(&(entry->condvar));
519 retval = SUCCESS;
520 }
521 else
522 {
523 this->logger->log(this->logger,CONTROL,"Fatal Error: Tried to checkin nonexisting IKE_SA");
524 /* this SA is no more, this REALLY should not happen */
525 retval = NOT_FOUND;
526 }
527 pthread_mutex_unlock(&(this->mutex));
528 return retval;
529 }
530
531
532 /**
533 * Implements ike_sa_manager_t-function checkin_and_delete.
534 * @see ike_sa_manager_t.checkin_and_delete.
535 */
536 static status_t checkin_and_delete(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
537 {
538 /* deletion is a bit complex, we must garant that no thread is waiting for
539 * this SA.
540 * We take this SA from the list, and start signaling while threads
541 * are in the condvar.
542 */
543 ike_sa_entry_t *entry;
544 status_t retval;
545
546 pthread_mutex_lock(&(this->mutex));
547
548 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
549 {
550 /* mark it, so now new threads can acquire this SA */
551 entry->driveout_new_threads = TRUE;
552 /* additionaly, drive out waiting threads */
553 entry->driveout_waiting_threads = TRUE;
554
555 /* wait until all workers have done their work */
556 while (entry->waiting_threads > 0)
557 {
558 /* let the other threads do some work*/
559 pthread_cond_signal(&(entry->condvar));
560 /* and the nice thing, they will wake us again when their work is done */
561 pthread_cond_wait(&(entry->condvar), &(this->mutex));
562 }
563 /* ok, we are alone now, no threads waiting in the entry's condvar */
564 this->delete_entry(this, entry);
565 retval = SUCCESS;
566 }
567 else
568 {
569 this->logger->log(this->logger,CONTROL,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA");
570 retval = NOT_FOUND;
571 }
572
573 pthread_mutex_unlock(&(this->mutex));
574 return retval;
575 }
576
577 /**
578 * Implements ike_sa_manager_t-function delete.
579 * @see ike_sa_manager_t.delete.
580 */
581 static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
582 {
583 /* deletion is a bit complex, we must garant that no thread is waiting for
584 * this SA.
585 * We take this SA from the list, and start signaling while threads
586 * are in the condvar.
587 */
588 ike_sa_entry_t *entry;
589 status_t retval;
590
591 pthread_mutex_lock(&(this->mutex));
592
593 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
594 {
595 /* mark it, so now new threads can acquire this SA */
596 entry->driveout_new_threads = TRUE;
597
598 /* wait until all workers have done their work */
599 while (entry->waiting_threads)
600 {
601 /* wake up all */
602 pthread_cond_signal(&(entry->condvar));
603 /* and the nice thing, they will wake us again when their work is done */
604 pthread_cond_wait(&(entry->condvar), &(this->mutex));
605 }
606 /* ok, we are alone now, no threads waiting in the entry's condvar */
607 this->delete_entry(this, entry);
608 retval = SUCCESS;
609 }
610 else
611 {
612 this->logger->log(this->logger,CONTROL,"Fatal Error: Tried to delete nonexisting IKE_SA");
613 retval = NOT_FOUND;
614 }
615
616 pthread_mutex_unlock(&(this->mutex));
617 return retval;
618 }
619
620 /**
621 * Implements ike_sa_manager_t-function destroy.
622 * @see ike_sa_manager_t.destroy.
623 */
624 static status_t destroy(private_ike_sa_manager_t *this)
625 {
626 /* destroy all list entries */
627 linked_list_t *list = this->list;
628 linked_list_iterator_t *iterator;
629 status_t status;
630
631 pthread_mutex_lock(&(this->mutex));
632
633 /* Step 1: drive out all waiting threads */
634 status = list->create_iterator(list, &iterator, TRUE);
635
636 if (status != SUCCESS)
637 {
638 this->logger->log(this->logger,CONTROL,"Fatal Error: Out of Ressources to greate interator while destroying IKE_SA-Manager");
639 return FAILED;
640 }
641
642 while (iterator->has_next(iterator))
643 {
644 ike_sa_entry_t *entry;
645 iterator->current(iterator, (void**)&entry);
646 /* do not accept new threads, drive out waiting threads */
647 entry->driveout_new_threads = TRUE;
648 entry->driveout_waiting_threads = TRUE;
649 }
650 /* Step 2: wait until all are gone */
651 iterator->reset(iterator);
652 while (iterator->has_next(iterator))
653 {
654 ike_sa_entry_t *entry;
655 iterator->current(iterator, (void**)&entry);
656 while (entry->waiting_threads)
657 {
658 /* wake up all */
659 pthread_cond_signal(&(entry->condvar));
660 /* go sleeping until they are gone */
661 pthread_cond_wait(&(entry->condvar), &(this->mutex));
662 }
663 }
664 /* Step 3: delete all entries */
665 iterator->reset(iterator);
666 while (iterator->has_next(iterator))
667 {
668 ike_sa_entry_t *entry;
669 iterator->current(iterator, (void**)&entry);
670 this->delete_entry(this, entry);
671 }
672 iterator->destroy(iterator);
673
674 list->destroy(list);
675 pthread_mutex_unlock(&(this->mutex));
676 allocator_free(this);
677
678 return SUCCESS;
679 }
680
681 /*
682 * Described in header
683 */
684 ike_sa_manager_t *ike_sa_manager_create()
685 {
686 private_ike_sa_manager_t *this = allocator_alloc_thing(private_ike_sa_manager_t);
687
688 /* assign public functions */
689 this->public.destroy = (status_t(*)(ike_sa_manager_t*))destroy;
690 this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id, ike_sa_t **sa))checkout;
691 this->public.checkin = (status_t(*)(ike_sa_manager_t*, ike_sa_t *sa))checkin;
692 this->public.delete = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id))delete;
693 this->public.checkin_and_delete = (status_t(*)(ike_sa_manager_t*, ike_sa_t *ike_sa))checkin_and_delete;
694
695 /* initialize private functions */
696 this->get_next_spi = get_next_spi;
697 this->get_entry_by_sa = get_entry_by_sa;
698 this->get_entry_by_id = get_entry_by_id;
699 this->delete_entry = delete_entry;
700
701 /* initialize private variables */
702 this->list = linked_list_create();
703 if (this->list == NULL)
704 {
705 allocator_free(this);
706 return NULL;
707 }
708
709 this->logger = global_logger_manager->create_logger(global_logger_manager,IKE_SA_MANAGER,NULL);
710 if (this->logger == NULL)
711 {
712 this->list->destroy(this->list);
713 allocator_free(this);
714 return NULL;
715 }
716
717 pthread_mutex_init(&(this->mutex), NULL);
718
719 this->next_spi.low = 1;
720 this->next_spi.high = 0;
721
722 return (ike_sa_manager_t*)this;
723 }