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