- spi_t is a u_int64_t now
[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, spi_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 spi_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 while (iterator->has_next(iterator))
218 {
219 ike_sa_entry_t *current;
220 bool are_equal = FALSE;
221 iterator->current(iterator, (void**)&current);
222 current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id, &are_equal);
223 if (are_equal)
224 {
225 *entry = current;
226 status = SUCCESS;
227 break;
228 }
229 }
230
231 iterator->destroy(iterator);
232 return status;
233 }
234
235 /**
236 * @see private_ike_sa_manager_t.get_entry_by_sa
237 */
238 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry)
239 {
240 linked_list_t *list = this->list;
241 linked_list_iterator_t *iterator;
242 status_t status;
243 status = list->create_iterator(list, &iterator, TRUE);
244 if (status != SUCCESS)
245 {
246 return status;
247 }
248
249 /* default status */
250 status = NOT_FOUND;
251
252 while (iterator->has_next(iterator))
253 {
254 ike_sa_entry_t *current;
255 iterator->current(iterator, (void**)&current);
256 /* only pointers are compared */
257 if (current->ike_sa == ike_sa)
258 {
259 *entry = current;
260 status = SUCCESS;
261 break;
262 }
263 }
264 iterator->destroy(iterator);
265 return status;
266 }
267
268 /**
269 * @see private_ike_sa_manager_s.delete_entry
270 */
271 static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry)
272 {
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))
277 {
278 ike_sa_entry_t *current;
279 iterator->current(iterator, (void**)&current);
280 if (current == entry)
281 {
282 list->remove(list, iterator);
283 entry->destroy(entry);
284 iterator->destroy(iterator);
285 return SUCCESS;
286 }
287 }
288 iterator->destroy(iterator);
289 return NOT_FOUND;
290 }
291
292
293 /**
294 * @see private_ike_sa_manager_t.get_next_spi
295 */
296 static status_t get_next_spi(private_ike_sa_manager_t *this, spi_t *spi)
297 {
298 this->next_spi++;
299 if (this->next_spi == 0) {
300 /* our software ran so incredible stable, we have no more
301 * SPIs to give away :-/. */
302 return OUT_OF_RES;
303 }
304 *spi = this->next_spi;
305 return SUCCESS;
306 }
307
308 /**
309 * @see ike_sa_manager_s.checkout_ike_sa
310 */
311 static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_t **ike_sa)
312 {
313 bool responder_spi_set;
314 bool initiator_spi_set;
315 status_t retval;
316
317 pthread_mutex_lock(&(this->mutex));
318
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);
321
322 if (initiator_spi_set && responder_spi_set)
323 {
324 /* we SHOULD have an IKE_SA for these SPIs in the list,
325 * if not, we can't handle the request...
326 */
327 ike_sa_entry_t *entry;
328 /* look for the entry */
329 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
330 {
331 /* can we give this out to new requesters?*/
332 if (entry->driveout_new_threads)
333 {
334 this->logger->log(this->logger,CONTROL_MORE,"Drive out new thread for existing IKE_SA");
335 /* no we can't */
336 retval = NOT_FOUND;
337 }
338 else
339 {
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)
343 {
344 /* so wait until we can get it for us.
345 * we register us as waiting.
346 */
347 entry->waiting_threads++;
348 pthread_cond_wait(&(entry->condvar), &(this->mutex));
349 entry->waiting_threads--;
350 }
351
352 /* hm, a deletion request forbids us to get this SA, go home */
353 if (entry->driveout_waiting_threads)
354 {
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");
358 retval = NOT_FOUND;
359 }
360 else
361 {
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! */
366 retval = SUCCESS;
367 }
368 }
369 }
370 else
371 {
372 /* looks like there is no such IKE_SA, better luck next time... */
373 /* DON'T use return, we must unlock the mutex! */
374 retval = NOT_FOUND;
375 }
376 }
377 else if (initiator_spi_set && !responder_spi_set)
378 {
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...
385 */
386 spi_t responder_spi;
387 ike_sa_entry_t *new_ike_sa_entry;
388
389 /* set SPIs, we are the responder */
390 retval = this->get_next_spi(this, &responder_spi);
391
392 if (retval == SUCCESS)
393 { /* next SPI could be created */
394
395 /* we also set arguments spi, so its still valid */
396 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
397
398 /* create entry */
399 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
400 if (new_ike_sa_entry != NULL)
401 {
402 retval = this->list->insert_last(this->list, new_ike_sa_entry);
403 if (retval == SUCCESS)
404 {
405 /* check ike_sa out */
406 new_ike_sa_entry->checked_out = TRUE;
407 *ike_sa = new_ike_sa_entry->ike_sa;
408
409 /* DON'T use return, we must unlock the mutex! */
410 }
411 else
412 {
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");
415 }
416 }
417 else
418 {
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");
421 }
422 }
423 else
424 { /* next SPI could not be created */
425 this->logger->log(this->logger,CONTROL,"Fatal error: Next SPI could not be created");
426 }
427
428 }
429 else if (!initiator_spi_set && !responder_spi_set)
430 {
431 /* creation of an IKE_SA from local site,
432 * we are the initiator!
433 */
434 spi_t initiator_spi;
435 ike_sa_entry_t *new_ike_sa_entry;
436
437 retval = this->get_next_spi(this, &initiator_spi);
438 if (retval == SUCCESS)
439 {
440 /* we also set arguments SPI, so its still valid */
441 ike_sa_id->set_initiator_spi(ike_sa_id, initiator_spi);
442
443 /* create entry */
444 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
445 if (new_ike_sa_entry != NULL)
446 {
447 retval = this->list->insert_last(this->list, new_ike_sa_entry);
448
449 if (retval == SUCCESS)
450 {
451 /* check ike_sa out */
452 new_ike_sa_entry->checked_out = TRUE;
453 *ike_sa = new_ike_sa_entry->ike_sa;
454
455 /* DON'T use return, we must unlock the mutex! */
456 }
457 else
458 {
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");
461 }
462 }
463 else
464 {
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");
467 }
468 }
469 else
470 {
471 /* next SPI could not be created */
472 this->logger->log(this->logger,CONTROL,"Fatal error: Next SPI could not be created");
473 }
474
475
476 }
477 else
478 {
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;
483 }
484
485 pthread_mutex_unlock(&(this->mutex));
486 /* OK, unlocked... */
487 return retval;
488 }
489
490 /**
491 * Implements ike_sa_manager_t-function checkin.
492 * @see ike_sa_manager_t.checkin.
493 */
494 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
495 {
496 /* to check the SA back in, we look for the pointer of the ike_sa
497 * in all entries.
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...
500 */
501 status_t retval;
502 ike_sa_entry_t *entry;
503
504 pthread_mutex_lock(&(this->mutex));
505
506 /* look for the entry */
507 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
508 {
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));
514 retval = SUCCESS;
515 }
516 else
517 {
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 */
520 retval = NOT_FOUND;
521 }
522 pthread_mutex_unlock(&(this->mutex));
523 return retval;
524 }
525
526
527 /**
528 * Implements ike_sa_manager_t-function checkin_and_delete.
529 * @see ike_sa_manager_t.checkin_and_delete.
530 */
531 static status_t checkin_and_delete(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
532 {
533 /* deletion is a bit complex, we must garant that no thread is waiting for
534 * this SA.
535 * We take this SA from the list, and start signaling while threads
536 * are in the condvar.
537 */
538 ike_sa_entry_t *entry;
539 status_t retval;
540
541 pthread_mutex_lock(&(this->mutex));
542
543 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
544 {
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;
549
550 /* wait until all workers have done their work */
551 while (entry->waiting_threads > 0)
552 {
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));
557 }
558 /* ok, we are alone now, no threads waiting in the entry's condvar */
559 this->delete_entry(this, entry);
560 retval = SUCCESS;
561 }
562 else
563 {
564 this->logger->log(this->logger,CONTROL,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA");
565 retval = NOT_FOUND;
566 }
567
568 pthread_mutex_unlock(&(this->mutex));
569 return retval;
570 }
571
572 /**
573 * Implements ike_sa_manager_t-function delete.
574 * @see ike_sa_manager_t.delete.
575 */
576 static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
577 {
578 /* deletion is a bit complex, we must garant that no thread is waiting for
579 * this SA.
580 * We take this SA from the list, and start signaling while threads
581 * are in the condvar.
582 */
583 ike_sa_entry_t *entry;
584 status_t retval;
585
586 pthread_mutex_lock(&(this->mutex));
587
588 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
589 {
590 /* mark it, so now new threads can acquire this SA */
591 entry->driveout_new_threads = TRUE;
592
593 /* wait until all workers have done their work */
594 while (entry->waiting_threads)
595 {
596 /* wake up all */
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));
600 }
601 /* ok, we are alone now, no threads waiting in the entry's condvar */
602 this->delete_entry(this, entry);
603 retval = SUCCESS;
604 }
605 else
606 {
607 this->logger->log(this->logger,CONTROL,"Fatal Error: Tried to delete nonexisting IKE_SA");
608 retval = NOT_FOUND;
609 }
610
611 pthread_mutex_unlock(&(this->mutex));
612 return retval;
613 }
614
615 /**
616 * Implements ike_sa_manager_t-function destroy.
617 * @see ike_sa_manager_t.destroy.
618 */
619 static status_t destroy(private_ike_sa_manager_t *this)
620 {
621 /* destroy all list entries */
622 linked_list_t *list = this->list;
623 linked_list_iterator_t *iterator;
624 status_t status;
625
626 pthread_mutex_lock(&(this->mutex));
627
628 /* Step 1: drive out all waiting threads */
629 status = list->create_iterator(list, &iterator, TRUE);
630
631 if (status != SUCCESS)
632 {
633 this->logger->log(this->logger,CONTROL,"Fatal Error: Out of Ressources to greate interator while destroying IKE_SA-Manager");
634 return FAILED;
635 }
636
637 while (iterator->has_next(iterator))
638 {
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;
644 }
645 /* Step 2: wait until all are gone */
646 iterator->reset(iterator);
647 while (iterator->has_next(iterator))
648 {
649 ike_sa_entry_t *entry;
650 iterator->current(iterator, (void**)&entry);
651 while (entry->waiting_threads)
652 {
653 /* wake up all */
654 pthread_cond_signal(&(entry->condvar));
655 /* go sleeping until they are gone */
656 pthread_cond_wait(&(entry->condvar), &(this->mutex));
657 }
658 }
659 /* Step 3: delete all entries */
660 iterator->reset(iterator);
661 while (iterator->has_next(iterator))
662 {
663 ike_sa_entry_t *entry;
664 iterator->current(iterator, (void**)&entry);
665 this->delete_entry(this, entry);
666 }
667 iterator->destroy(iterator);
668
669 list->destroy(list);
670 pthread_mutex_unlock(&(this->mutex));
671 allocator_free(this);
672
673 return SUCCESS;
674 }
675
676 /*
677 * Described in header
678 */
679 ike_sa_manager_t *ike_sa_manager_create()
680 {
681 private_ike_sa_manager_t *this = allocator_alloc_thing(private_ike_sa_manager_t);
682
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;
689
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;
695
696 /* initialize private variables */
697 this->list = linked_list_create();
698 if (this->list == NULL)
699 {
700 allocator_free(this);
701 return NULL;
702 }
703
704 this->logger = global_logger_manager->create_logger(global_logger_manager,IKE_SA_MANAGER,NULL);
705 if (this->logger == NULL)
706 {
707 this->list->destroy(this->list);
708 allocator_free(this);
709 return NULL;
710 }
711
712 pthread_mutex_init(&(this->mutex), NULL);
713
714 this->next_spi = 0;
715
716 return (ike_sa_manager_t*)this;
717 }