c391577fc44723e7087e7073e385c26ae531af05
[strongswan.git] / src / charon / sa / connect_manager.c
1 /*
2 * Copyright (C) 2007-2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "connect_manager.h"
19
20 #include <pthread.h>
21 #include <math.h>
22
23 #include <daemon.h>
24 #include <utils/linked_list.h>
25 #include <crypto/hashers/hasher.h>
26
27 #include <processing/jobs/callback_job.h>
28 #include <processing/jobs/initiate_mediation_job.h>
29 #include <encoding/payloads/endpoint_notify.h>
30
31 /* base timeout
32 * the check interval is ME_INTERVAL */
33 #define ME_INTERVAL 25 /* ms */
34 /* retransmission timeout is first ME_INTERVAL for ME_BOOST retransmissions
35 * then gets reduced to ME_INTERVAL * ME_RETRANS_BASE ^ (sent retransmissions - ME_BOOST). */
36 /* number of initial retransmissions sent in short interval */
37 #define ME_BOOST 2
38 /* base for retransmissions */
39 #define ME_RETRANS_BASE 1.8
40 /* max number of retransmissions */
41 #define ME_MAX_RETRANS 13
42
43 /* time to wait before the initiator finishes the connectivity checks after
44 * the first check has succeeded */
45 #define ME_WAIT_TO_FINISH 1000 /* ms */
46
47
48 typedef struct private_connect_manager_t private_connect_manager_t;
49
50 /**
51 * Additional private members of connect_manager_t.
52 */
53 struct private_connect_manager_t {
54 /**
55 * Public interface of connect_manager_t.
56 */
57 connect_manager_t public;
58
59 /**
60 * Lock for exclusivly accessing the manager.
61 */
62 pthread_mutex_t mutex;
63
64 /**
65 * Hasher to generate signatures
66 */
67 hasher_t *hasher;
68
69 /**
70 * Linked list with initiated mediated connections
71 */
72 linked_list_t *initiated;
73
74 /**
75 * Linked list with checklists (hash table with connect ID as key would be better).
76 */
77 linked_list_t *checklists;
78 };
79
80 typedef enum check_state_t check_state_t;
81
82 enum check_state_t {
83 CHECK_NONE,
84 CHECK_WAITING,
85 CHECK_IN_PROGRESS,
86 CHECK_SUCCEEDED,
87 CHECK_FAILED
88 };
89
90 typedef struct endpoint_pair_t endpoint_pair_t;
91
92 /**
93 * An entry in the check list.
94 */
95 struct endpoint_pair_t {
96 /** pair id */
97 u_int32_t id;
98
99 /** priority */
100 u_int64_t priority;
101
102 /** local endpoint */
103 host_t *local;
104
105 /** remote endpoint */
106 host_t *remote;
107
108 /** state */
109 check_state_t state;
110
111 /** number of retransmissions */
112 u_int32_t retransmitted;
113
114 /** the generated packet */
115 packet_t *packet;
116 };
117
118 /**
119 * Destroys an endpoint pair
120 */
121 static void endpoint_pair_destroy(endpoint_pair_t *this)
122 {
123 DESTROY_IF(this->local);
124 DESTROY_IF(this->remote);
125 DESTROY_IF(this->packet);
126 free(this);
127 }
128
129 /**
130 * Creates a new entry for the list.
131 */
132 static endpoint_pair_t *endpoint_pair_create(endpoint_notify_t *initiator,
133 endpoint_notify_t *responder, bool initiator_is_local)
134 {
135 endpoint_pair_t *this = malloc_thing(endpoint_pair_t);
136
137 this->id = 0;
138
139 u_int32_t pi = initiator->get_priority(initiator);
140 u_int32_t pr = responder->get_priority(responder);
141 this->priority = pow(2, 32) * min(pi, pr) + 2 * max(pi, pr) + (pi > pr ? 1 : 0);
142
143 this->local = initiator_is_local ? initiator->get_base(initiator) : responder->get_base(responder);
144 this->local = this->local->clone(this->local);
145 this->remote = initiator_is_local ? responder->get_host(responder) : initiator->get_host(initiator);
146 this->remote = this->remote->clone(this->remote);
147
148 this->state = CHECK_WAITING;
149 this->retransmitted = 0;
150 this->packet = NULL;
151
152 return this;
153 }
154
155
156 typedef struct check_list_t check_list_t;
157
158 /**
159 * An entry in the linked list.
160 */
161 struct check_list_t {
162
163 struct {
164 /** initiator's id */
165 identification_t *id;
166
167 /** initiator's key */
168 chunk_t key;
169
170 /** initiator's endpoints */
171 linked_list_t *endpoints;
172 } initiator;
173
174 struct {
175 /** responder's id */
176 identification_t *id;
177
178 /** responder's key */
179 chunk_t key;
180
181 /** responder's endpoints */
182 linked_list_t *endpoints;
183 } responder;
184
185 /** connect id */
186 chunk_t connect_id;
187
188 /** list of endpoint pairs */
189 linked_list_t *pairs;
190
191 /** pairs queued for triggered checks */
192 linked_list_t *triggered;
193
194 /** state */
195 check_state_t state;
196
197 /** TRUE if this is the initiator */
198 bool is_initiator;
199
200 /** TRUE if the initiator is finishing the checks */
201 bool is_finishing;
202
203 /** the current sender job */
204 job_t *sender;
205
206 };
207
208 /**
209 * Destroys a checklist
210 */
211 static void check_list_destroy(check_list_t *this)
212 {
213 DESTROY_IF(this->initiator.id);
214 DESTROY_IF(this->responder.id);
215
216 chunk_free(&this->connect_id);
217 chunk_free(&this->initiator.key);
218 chunk_free(&this->responder.key);
219
220 DESTROY_OFFSET_IF(this->initiator.endpoints, offsetof(endpoint_notify_t, destroy));
221 DESTROY_OFFSET_IF(this->responder.endpoints, offsetof(endpoint_notify_t, destroy));
222
223 DESTROY_FUNCTION_IF(this->pairs, (void*)endpoint_pair_destroy);
224 /* this list contains some of the same elements as contained in this->pairs */
225 DESTROY_IF(this->triggered);
226
227 free(this);
228 }
229
230 /**
231 * Creates a new checklist
232 */
233 static check_list_t *check_list_create(identification_t *initiator, identification_t *responder,
234 chunk_t connect_id, chunk_t initiator_key, linked_list_t *initiator_endpoints,
235 bool is_initiator)
236 {
237 check_list_t *this = malloc_thing(check_list_t);
238
239 this->connect_id = chunk_clone(connect_id);
240
241 this->initiator.id = initiator->clone(initiator);
242 this->initiator.key = chunk_clone(initiator_key);
243 this->initiator.endpoints = initiator_endpoints->clone_offset(initiator_endpoints, offsetof(endpoint_notify_t, clone));
244
245 this->responder.id = responder->clone(responder);
246 this->responder.key = chunk_empty;
247 this->responder.endpoints = NULL;
248
249 this->pairs = linked_list_create();
250 this->triggered = linked_list_create();
251 this->state = CHECK_NONE;
252 this->is_initiator = is_initiator;
253 this->is_finishing = FALSE;
254
255 return this;
256 }
257
258
259 typedef struct waiting_sa_t waiting_sa_t;
260
261 /**
262 * For an initiator, the data stored about a waiting mediated sa
263 */
264 struct waiting_sa_t {
265 /** ike sa id */
266 ike_sa_id_t *ike_sa_id;
267
268 /** list of child_cfg_t */
269 linked_list_t *childs;
270 };
271
272 /**
273 * Destroys a queued mediated sa
274 */
275 static void waiting_sa_destroy(waiting_sa_t *this)
276 {
277 DESTROY_IF(this->ike_sa_id);
278 this->childs->destroy_offset(this->childs, offsetof(child_cfg_t, destroy));
279 free(this);
280 }
281
282 /**
283 * Creates a new mediated sa object
284 */
285 static waiting_sa_t *waiting_sa_create(ike_sa_id_t *ike_sa_id)
286 {
287 waiting_sa_t *this = malloc_thing(waiting_sa_t);
288
289 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
290 this->childs = linked_list_create();
291
292 return this;
293 }
294
295 typedef struct initiated_t initiated_t;
296
297 /**
298 * For an initiator, the data stored about initiated mediation connections
299 */
300 struct initiated_t {
301 /** my id */
302 identification_t *id;
303
304 /** peer id */
305 identification_t *peer_id;
306
307 /** list of mediated sas */
308 linked_list_t *mediated;
309 };
310
311 /**
312 * Destroys a queued initiation
313 */
314 static void initiated_destroy(initiated_t *this)
315 {
316 DESTROY_IF(this->id);
317 DESTROY_IF(this->peer_id);
318 this->mediated->destroy_function(this->mediated, (void*)waiting_sa_destroy);
319 free(this);
320 }
321
322 /**
323 * Creates a queued initiation
324 */
325 static initiated_t *initiated_create(identification_t *id, identification_t *peer_id)
326 {
327 initiated_t *this = malloc_thing(initiated_t);
328
329 this->id = id->clone(id);
330 this->peer_id = peer_id->clone(peer_id);
331 this->mediated = linked_list_create();
332
333 return this;
334 }
335
336
337 typedef struct check_t check_t;
338
339 /**
340 * Data exchanged in a connectivity check
341 */
342 struct check_t {
343 /** message id */
344 u_int32_t mid;
345
346 /** source of the connectivity check */
347 host_t *src;
348
349 /** destination of the connectivity check */
350 host_t *dst;
351
352 /** connect id */
353 chunk_t connect_id;
354
355 /** endpoint */
356 endpoint_notify_t *endpoint;
357
358 /** raw endpoint payload (to verify the signature) */
359 chunk_t endpoint_raw;
360
361 /** connect auth */
362 chunk_t auth;
363 };
364
365 /**
366 * Destroys a connectivity check
367 */
368 static void check_destroy(check_t *this)
369 {
370 chunk_free(&this->connect_id);
371 chunk_free(&this->endpoint_raw);
372 chunk_free(&this->auth);
373 DESTROY_IF(this->src);
374 DESTROY_IF(this->dst);
375 DESTROY_IF(this->endpoint);
376 free(this);
377 }
378
379 /**
380 * Creates a new connectivity check
381 */
382 static check_t *check_create()
383 {
384 check_t *this = malloc_thing(check_t);
385
386 this->connect_id = chunk_empty;
387 this->auth = chunk_empty;
388 this->endpoint_raw = chunk_empty;
389 this->src = NULL;
390 this->dst = NULL;
391 this->endpoint = NULL;
392
393 this->mid = 0;
394
395 return this;
396 }
397
398 typedef struct callback_data_t callback_data_t;
399
400 /**
401 * Data required by several callback jobs used in this file
402 */
403 struct callback_data_t {
404 /** connect manager */
405 private_connect_manager_t *connect_manager;
406
407 /** connect id */
408 chunk_t connect_id;
409
410 /** message (pair) id */
411 u_int32_t mid;
412 };
413
414 /**
415 * Destroys a callback data object
416 */
417 static void callback_data_destroy(callback_data_t *this)
418 {
419 chunk_free(&this->connect_id);
420 free(this);
421 }
422
423 /**
424 * Creates a new callback data object
425 */
426 static callback_data_t *callback_data_create(private_connect_manager_t *connect_manager,
427 chunk_t connect_id)
428 {
429 callback_data_t *this = malloc_thing(callback_data_t);
430 this->connect_manager = connect_manager;
431 this->connect_id = chunk_clone(connect_id);
432 this->mid = 0;
433 return this;
434 }
435
436 /**
437 * Creates a new retransmission data object
438 */
439 static callback_data_t *retransmit_data_create(private_connect_manager_t *connect_manager,
440 chunk_t connect_id, u_int32_t mid)
441 {
442 callback_data_t *this = callback_data_create(connect_manager, connect_id);
443 this->mid = mid;
444 return this;
445 }
446
447 typedef struct initiate_data_t initiate_data_t;
448
449 /**
450 * Data required by the initiate mediated
451 */
452 struct initiate_data_t {
453 /** checklist */
454 check_list_t *checklist;
455
456 /** waiting mediated connections */
457 initiated_t *initiated;
458 };
459
460 /**
461 * Destroys a initiate data object
462 */
463 static void initiate_data_destroy(initiate_data_t *this)
464 {
465 check_list_destroy(this->checklist);
466 initiated_destroy(this->initiated);
467 free(this);
468 }
469
470 /**
471 * Creates a new initiate data object
472 */
473 static initiate_data_t *initiate_data_create(check_list_t *checklist, initiated_t *initiated)
474 {
475 initiate_data_t *this = malloc_thing(initiate_data_t);
476
477 this->checklist = checklist;
478 this->initiated = initiated;
479
480 return this;
481 }
482
483 /**
484 * Find an initiated connection by the peers' ids
485 */
486 static bool match_initiated_by_ids(initiated_t *current, identification_t *id,
487 identification_t *peer_id)
488 {
489 return id->equals(id, current->id) && peer_id->equals(peer_id, current->peer_id);
490 }
491
492 static status_t get_initiated_by_ids(private_connect_manager_t *this,
493 identification_t *id, identification_t *peer_id, initiated_t **initiated)
494 {
495 return this->initiated->find_first(this->initiated,
496 (linked_list_match_t)match_initiated_by_ids,
497 (void**)initiated, id, peer_id);
498 }
499
500 /**
501 * Removes data about initiated connections
502 */
503 static void remove_initiated(private_connect_manager_t *this, initiated_t *initiated)
504 {
505 iterator_t *iterator;
506 initiated_t *current;
507
508 iterator = this->initiated->create_iterator(this->initiated, TRUE);
509 while (iterator->iterate(iterator, (void**)&current))
510 {
511 if (current == initiated)
512 {
513 iterator->remove(iterator);
514 break;
515 }
516 }
517 iterator->destroy(iterator);
518 }
519
520 /**
521 * Finds a waiting sa
522 */
523 static bool match_waiting_sa(waiting_sa_t *current, ike_sa_id_t *ike_sa_id)
524 {
525 return ike_sa_id->equals(ike_sa_id, current->ike_sa_id);
526 }
527
528 static status_t get_waiting_sa(initiated_t *initiated, ike_sa_id_t *ike_sa_id, waiting_sa_t **waiting_sa)
529 {
530 return initiated->mediated->find_first(initiated->mediated,
531 (linked_list_match_t)match_waiting_sa,
532 (void**)waiting_sa, ike_sa_id);
533 }
534
535 /**
536 * Find the checklist with a specific connect ID
537 */
538 static bool match_checklist_by_id(check_list_t *current, chunk_t *connect_id)
539 {
540 return chunk_equals(*connect_id, current->connect_id);
541 }
542
543 static status_t get_checklist_by_id(private_connect_manager_t *this,
544 chunk_t connect_id, check_list_t **check_list)
545 {
546 return this->checklists->find_first(this->checklists,
547 (linked_list_match_t)match_checklist_by_id,
548 (void**)check_list, &connect_id);
549 }
550
551 /**
552 * Removes a checklist
553 */
554 static void remove_checklist(private_connect_manager_t *this, check_list_t *checklist)
555 {
556 iterator_t *iterator;
557 check_list_t *current;
558
559 iterator = this->checklists->create_iterator(this->checklists, TRUE);
560 while (iterator->iterate(iterator, (void**)&current))
561 {
562 if (current == checklist)
563 {
564 iterator->remove(iterator);
565 break;
566 }
567 }
568 iterator->destroy(iterator);
569 }
570
571 /**
572 * Checks if a list of endpoint_notify_t contains a certain host_t
573 */
574 static bool match_endpoint_by_host(endpoint_notify_t *current, host_t *host)
575 {
576 return host->equals(host, current->get_host(current));
577 }
578
579 static status_t endpoints_contain(linked_list_t *endpoints, host_t *host, endpoint_notify_t **endpoint)
580 {
581 return endpoints->find_first(endpoints,
582 (linked_list_match_t)match_endpoint_by_host,
583 (void**)endpoint, host);
584 }
585
586 /**
587 * Inserts an endpoint pair into the list of pairs ordered by priority (high to low)
588 */
589 static void insert_pair_by_priority(linked_list_t *pairs, endpoint_pair_t *pair)
590 {
591 iterator_t *iterator;
592 endpoint_pair_t *current;
593 bool inserted = FALSE;
594
595 iterator = pairs->create_iterator(pairs, TRUE);
596 while (iterator->iterate(iterator, (void**)&current))
597 {
598 if (current->priority < pair->priority)
599 {
600 iterator->insert_before(iterator, pair);
601 inserted = TRUE;
602 break;
603 }
604 }
605 iterator->destroy(iterator);
606
607 if (!inserted)
608 {
609 pairs->insert_last(pairs, pair);
610 }
611 }
612
613 /**
614 * Searches a list of endpoint_pair_t for a pair with specific host_ts
615 */
616 static bool match_pair_by_hosts(endpoint_pair_t *current, host_t *local, host_t *remote)
617 {
618 return local->equals(local, current->local) && remote->equals(remote, current->remote);
619 }
620
621 static status_t get_pair_by_hosts(linked_list_t *pairs, host_t *local, host_t *remote, endpoint_pair_t **pair)
622 {
623 return pairs->find_first(pairs,
624 (linked_list_match_t)match_pair_by_hosts,
625 (void**)pair, local, remote);
626 }
627
628 static bool match_pair_by_id(endpoint_pair_t *current, u_int32_t *id)
629 {
630 return current->id == *id;
631 }
632
633 /**
634 * Searches for a pair with a specific id
635 */
636 static status_t get_pair_by_id(check_list_t *checklist, u_int32_t id, endpoint_pair_t **pair)
637 {
638 return checklist->pairs->find_first(checklist->pairs,
639 (linked_list_match_t)match_pair_by_id,
640 (void**)pair, &id);
641 }
642
643 static bool match_succeeded_pair(endpoint_pair_t *current)
644 {
645 return current->state == CHECK_SUCCEEDED;
646 }
647
648 /**
649 * Returns the best pair of state CHECK_SUCCEEDED from a checklist.
650 */
651 static status_t get_best_valid_pair(check_list_t *checklist, endpoint_pair_t **pair)
652 {
653 return checklist->pairs->find_first(checklist->pairs,
654 (linked_list_match_t)match_succeeded_pair,
655 (void**)pair);
656 }
657
658 static bool match_waiting_pair(endpoint_pair_t *current)
659 {
660 return current->state == CHECK_WAITING;
661 }
662
663 /**
664 * Returns and *removes* the first triggered pair in state CHECK_WAITING.
665 */
666 static status_t get_triggered_pair(check_list_t *checklist, endpoint_pair_t **pair)
667 {
668 iterator_t *iterator;
669 endpoint_pair_t *current;
670 status_t status = NOT_FOUND;
671
672 iterator = checklist->triggered->create_iterator(checklist->triggered, TRUE);
673 while (iterator->iterate(iterator, (void**)&current))
674 {
675 iterator->remove(iterator);
676
677 if (current->state == CHECK_WAITING)
678 {
679 if (pair)
680 {
681 *pair = current;
682 }
683 status = SUCCESS;
684 break;
685 }
686 }
687 iterator->destroy(iterator);
688
689 return status;
690 }
691
692 /**
693 * Prunes identical pairs with lower priority from the list
694 * Note: this function also numbers the remaining pairs serially
695 */
696 static void prune_pairs(linked_list_t *pairs)
697 {
698 iterator_t *iterator, *search;
699 endpoint_pair_t *current, *other;
700 u_int32_t id = 0;
701
702 iterator = pairs->create_iterator(pairs, TRUE);
703 search = pairs->create_iterator(pairs, TRUE);
704 while (iterator->iterate(iterator, (void**)&current))
705 {
706 current->id = ++id;
707
708 while (search->iterate(search, (void**)&other))
709 {
710 if (current == other)
711 {
712 continue;
713 }
714
715 if (current->local->equals(current->local, other->local) &&
716 current->remote->equals(current->remote, other->remote))
717 {
718 /* since the list of pairs is sorted by priority in descending
719 * order, and we iterate the list from the beginning, we are
720 * sure that the priority of 'other' is lower than that of
721 * 'current', remove it */
722 DBG1(DBG_IKE, "pruning endpoint pair %H - %H with priority %d",
723 other->local, other->remote, other->priority);
724 search->remove(search);
725 endpoint_pair_destroy(other);
726 }
727 }
728 search->reset(search);
729 }
730 search->destroy(search);
731 iterator->destroy(iterator);
732 }
733
734 /**
735 * Builds a list of endpoint pairs
736 */
737 static void build_pairs(check_list_t *checklist)
738 {
739 iterator_t *iterator_i, *iterator_r;
740 endpoint_notify_t *initiator, *responder;
741
742 iterator_i = checklist->initiator.endpoints->create_iterator(checklist->initiator.endpoints, TRUE);
743 while (iterator_i->iterate(iterator_i, (void**)&initiator))
744 {
745 iterator_r = checklist->responder.endpoints->create_iterator(checklist->responder.endpoints, TRUE);
746 while (iterator_r->iterate(iterator_r, (void**)&responder))
747 {
748 if (initiator->get_family(initiator) != responder->get_family(responder))
749 {
750 continue;
751 }
752
753 insert_pair_by_priority(checklist->pairs,
754 endpoint_pair_create(initiator, responder, checklist->is_initiator));
755 }
756 iterator_r->destroy(iterator_r);
757 }
758 iterator_i->destroy(iterator_i);
759
760 prune_pairs(checklist->pairs);
761 }
762
763 /**
764 * Processes the payloads of a connectivity check and returns the extracted data
765 */
766 static status_t process_payloads(message_t *message, check_t *check)
767 {
768 iterator_t *iterator;
769 payload_t *payload;
770
771 iterator = message->get_payload_iterator(message);
772 while (iterator->iterate(iterator, (void**)&payload))
773 {
774 if (payload->get_type(payload) != NOTIFY)
775 {
776 DBG1(DBG_IKE, "ignoring payload of type '%N' while processing "
777 "connectivity check", payload_type_names, payload->get_type(payload));
778 continue;
779 }
780
781 notify_payload_t *notify = (notify_payload_t*)payload;
782
783 switch (notify->get_notify_type(notify))
784 {
785 case ME_ENDPOINT:
786 {
787 if (check->endpoint)
788 {
789 DBG1(DBG_IKE, "connectivity check contains multiple ME_ENDPOINT notifies");
790 break;
791 }
792
793 endpoint_notify_t *endpoint = endpoint_notify_create_from_payload(notify);
794 if (!endpoint)
795 {
796 DBG1(DBG_IKE, "received invalid ME_ENDPOINT notify");
797 break;
798 }
799 check->endpoint = endpoint;
800 check->endpoint_raw = chunk_clone(notify->get_notification_data(notify));
801 DBG2(DBG_IKE, "received ME_ENDPOINT notify");
802 break;
803 }
804 case ME_CONNECTID:
805 {
806 if (check->connect_id.ptr)
807 {
808 DBG1(DBG_IKE, "connectivity check contains multiple ME_CONNECTID notifies");
809 break;
810 }
811 check->connect_id = chunk_clone(notify->get_notification_data(notify));
812 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &check->connect_id);
813 break;
814 }
815 case ME_CONNECTAUTH:
816 {
817 if (check->auth.ptr)
818 {
819 DBG1(DBG_IKE, "connectivity check contains multiple ME_CONNECTAUTH notifies");
820 break;
821 }
822 check->auth = chunk_clone(notify->get_notification_data(notify));
823 DBG2(DBG_IKE, "received ME_CONNECTAUTH %#B", &check->auth);
824 break;
825 }
826 default:
827 break;
828 }
829 }
830 iterator->destroy(iterator);
831
832 if (!check->connect_id.ptr || !check->endpoint || !check->auth.ptr)
833 {
834 DBG1(DBG_IKE, "at least one payload was missing from the connectivity check");
835 return FAILED;
836 }
837
838 return SUCCESS;
839 }
840
841 /**
842 * Builds the signature for a connectivity check
843 */
844 static chunk_t build_signature(private_connect_manager_t *this,
845 check_list_t *checklist, check_t *check, bool outbound)
846 {
847 chunk_t mid_chunk, key_chunk, sig_chunk;
848 chunk_t sig_hash;
849
850 mid_chunk = chunk_from_thing(check->mid);
851
852 key_chunk = (checklist->is_initiator && outbound) || (!checklist->is_initiator && !outbound)
853 ? checklist->initiator.key : checklist->responder.key;
854
855 /* signature = SHA1( MID | ME_CONNECTID | ME_ENDPOINT | ME_CONNECTKEY ) */
856 sig_chunk = chunk_cat("cccc", mid_chunk, check->connect_id, check->endpoint_raw, key_chunk);
857 this->hasher->allocate_hash(this->hasher, sig_chunk, &sig_hash);
858 DBG3(DBG_IKE, "sig_chunk %#B", &sig_chunk);
859 DBG3(DBG_IKE, "sig_hash %#B", &sig_hash);
860
861 chunk_free(&sig_chunk);
862 return sig_hash;
863 }
864
865 static void queue_retransmission(private_connect_manager_t *this, check_list_t *checklist, endpoint_pair_t *pair);
866 static void schedule_checks(private_connect_manager_t *this, check_list_t *checklist, u_int32_t time);
867 static void finish_checks(private_connect_manager_t *this, check_list_t *checklist);
868
869 /**
870 * After one of the initiator's pairs has succeeded we finish the checks without
871 * waiting for all the timeouts
872 */
873 static job_requeue_t initiator_finish(callback_data_t *data)
874 {
875 private_connect_manager_t *this = data->connect_manager;
876
877 pthread_mutex_lock(&(this->mutex));
878
879 check_list_t *checklist;
880 if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
881 {
882 DBG1(DBG_IKE, "checklist with id '%#B' not found, can't finish connectivity checks",
883 &data->connect_id);
884 pthread_mutex_unlock(&(this->mutex));
885 return JOB_REQUEUE_NONE;
886 }
887
888 finish_checks(this, checklist);
889
890 pthread_mutex_unlock(&(this->mutex));
891
892 return JOB_REQUEUE_NONE;
893 }
894
895 /**
896 * Updates the state of the whole checklist
897 */
898 static void update_checklist_state(private_connect_manager_t *this, check_list_t *checklist)
899 {
900 iterator_t *iterator;
901 endpoint_pair_t *current;
902 bool in_progress = FALSE, succeeded = FALSE;
903
904 iterator = checklist->pairs->create_iterator(checklist->pairs, TRUE);
905 while (iterator->iterate(iterator, (void**)&current))
906 {
907 switch(current->state)
908 {
909 case CHECK_WAITING:
910 /* at least one is still waiting -> checklist remains
911 * in waiting state */
912 iterator->destroy(iterator);
913 return;
914 case CHECK_IN_PROGRESS:
915 in_progress = TRUE;
916 break;
917 case CHECK_SUCCEEDED:
918 succeeded = TRUE;
919 break;
920 default:
921 break;
922 }
923 }
924 iterator->destroy(iterator);
925
926 if (checklist->is_initiator && succeeded && !checklist->is_finishing)
927 {
928 /* instead of waiting until all checks have finished (i.e. all
929 * retransmissions have failed) the initiator finishes the checks
930 * right after the first check has succeeded. to allow a probably
931 * better pair to succeed, we still wait a certain time */
932 DBG2(DBG_IKE, "fast finishing checks for checklist '%#B'", &checklist->connect_id);
933
934 callback_data_t *data = callback_data_create(this, checklist->connect_id);
935 job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiator_finish, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
936 charon->scheduler->schedule_job(charon->scheduler, job, ME_WAIT_TO_FINISH);
937 checklist->is_finishing = TRUE;
938 }
939
940 if (in_progress)
941 {
942 checklist->state = CHECK_IN_PROGRESS;
943 }
944 else if (succeeded)
945 {
946 checklist->state = CHECK_SUCCEEDED;
947 }
948 else
949 {
950 checklist->state = CHECK_FAILED;
951 }
952 }
953
954 /**
955 * This function is triggered for each sent check after a specific timeout
956 */
957 static job_requeue_t retransmit(callback_data_t *data)
958 {
959 private_connect_manager_t *this = data->connect_manager;
960
961 pthread_mutex_lock(&(this->mutex));
962
963 check_list_t *checklist;
964 if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
965 {
966 DBG1(DBG_IKE, "checklist with id '%#B' not found, can't retransmit connectivity check",
967 &data->connect_id);
968 pthread_mutex_unlock(&(this->mutex));
969 return JOB_REQUEUE_NONE;
970 }
971
972 endpoint_pair_t *pair;
973 if (get_pair_by_id(checklist, data->mid, &pair) != SUCCESS)
974 {
975 DBG1(DBG_IKE, "pair with id '%d' not found, can't retransmit connectivity check",
976 data->mid);
977 goto retransmit_end;
978 }
979
980 if (pair->state != CHECK_IN_PROGRESS)
981 {
982 DBG2(DBG_IKE, "pair with id '%d' is in wrong state [%d], don't retransmit the connectivity check",
983 data->mid, pair->state);
984 goto retransmit_end;
985 }
986
987 if (++pair->retransmitted > ME_MAX_RETRANS)
988 {
989 DBG2(DBG_IKE, "pair with id '%d' failed after %d retransmissions",
990 data->mid, ME_MAX_RETRANS);
991 pair->state = CHECK_FAILED;
992 goto retransmit_end;
993 }
994
995 charon->sender->send(charon->sender, pair->packet->clone(pair->packet));
996
997 queue_retransmission(this, checklist, pair);
998
999 retransmit_end:
1000 update_checklist_state(this, checklist);
1001
1002 switch(checklist->state)
1003 {
1004 case CHECK_SUCCEEDED:
1005 case CHECK_FAILED:
1006 finish_checks(this, checklist);
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 pthread_mutex_unlock(&(this->mutex));
1013
1014 /* we reschedule it manually */
1015 return JOB_REQUEUE_NONE;
1016 }
1017
1018 /**
1019 * Queues a retransmission job
1020 */
1021 static void queue_retransmission(private_connect_manager_t *this, check_list_t *checklist, endpoint_pair_t *pair)
1022 {
1023 callback_data_t *data = retransmit_data_create(this, checklist->connect_id, pair->id);
1024 job_t *job = (job_t*)callback_job_create((callback_job_cb_t)retransmit, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
1025
1026 u_int32_t retransmission = pair->retransmitted + 1;
1027 u_int32_t rto = ME_INTERVAL;
1028 if (retransmission > ME_BOOST)
1029 {
1030 rto = (u_int32_t)(ME_INTERVAL * pow(ME_RETRANS_BASE, retransmission - ME_BOOST));
1031 }
1032 DBG2(DBG_IKE, "scheduling retransmission %d of pair '%d' in %dms", retransmission, pair->id, rto);
1033
1034 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job, rto);
1035 }
1036
1037 /**
1038 * Sends a check
1039 */
1040 static void send_check(private_connect_manager_t *this, check_list_t *checklist,
1041 check_t *check, endpoint_pair_t *pair, bool request)
1042 {
1043 message_t *message = message_create();
1044 message->set_message_id(message, check->mid);
1045 message->set_exchange_type(message, INFORMATIONAL);
1046 message->set_request(message, request);
1047 message->set_destination(message, check->dst->clone(check->dst));
1048 message->set_source(message, check->src->clone(check->src));
1049
1050 ike_sa_id_t *ike_sa_id = ike_sa_id_create(0, 0, request);
1051 message->set_ike_sa_id(message, ike_sa_id);
1052 ike_sa_id->destroy(ike_sa_id);
1053
1054 message->add_notify(message, FALSE, ME_CONNECTID, check->connect_id);
1055 DBG2(DBG_IKE, "send ME_CONNECTID %#B", &check->connect_id);
1056
1057 notify_payload_t *endpoint = check->endpoint->build_notify(check->endpoint);
1058 check->endpoint_raw = chunk_clone(endpoint->get_notification_data(endpoint));
1059 message->add_payload(message, (payload_t*)endpoint);
1060 DBG2(DBG_IKE, "send ME_ENDPOINT notify");
1061
1062 check->auth = build_signature(this, checklist, check, TRUE);
1063 message->add_notify(message, FALSE, ME_CONNECTAUTH, check->auth);
1064 DBG2(DBG_IKE, "send ME_CONNECTAUTH %#B", &check->auth);
1065
1066 packet_t *packet;
1067 if (message->generate(message, NULL, NULL, &packet) == SUCCESS)
1068 {
1069 charon->sender->send(charon->sender, packet->clone(packet));
1070
1071 if (request)
1072 {
1073 DESTROY_IF(pair->packet);
1074 pair->packet = packet;
1075 queue_retransmission(this, checklist, pair);
1076 }
1077 else
1078 {
1079 packet->destroy(packet);
1080 }
1081 }
1082 message->destroy(message);
1083 }
1084
1085 /**
1086 * Queues a triggered check
1087 */
1088 static void queue_triggered_check(private_connect_manager_t *this,
1089 check_list_t *checklist, endpoint_pair_t *pair)
1090 {
1091 pair->state = CHECK_WAITING;
1092 checklist->triggered->insert_last(checklist->triggered, pair);
1093
1094 if (checklist->sender)
1095 {
1096 /* if the sender is not running we restart it */
1097 schedule_checks(this, checklist, ME_INTERVAL);
1098 }
1099 }
1100
1101 /**
1102 * This function is triggered for each checklist at a specific interval
1103 */
1104 static job_requeue_t sender(callback_data_t *data)
1105 {
1106 private_connect_manager_t *this = data->connect_manager;
1107
1108 pthread_mutex_lock(&(this->mutex));
1109
1110 check_list_t *checklist;
1111 if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
1112 {
1113 DBG1(DBG_IKE, "checklist with id '%#B' not found, can't send connectivity check",
1114 &data->connect_id);
1115 pthread_mutex_unlock(&(this->mutex));
1116 return JOB_REQUEUE_NONE;
1117 }
1118
1119 /* reset the sender */
1120 checklist->sender = NULL;
1121
1122 endpoint_pair_t *pair;
1123 if (get_triggered_pair(checklist, &pair) != SUCCESS)
1124 {
1125 DBG1(DBG_IKE, "no triggered check queued, sending an ordinary check");
1126
1127 if (checklist->pairs->find_first(checklist->pairs,
1128 (linked_list_match_t)match_waiting_pair, (void**)&pair) != SUCCESS)
1129 {
1130 pthread_mutex_unlock(&(this->mutex));
1131 DBG1(DBG_IKE, "no pairs in waiting state, aborting");
1132 return JOB_REQUEUE_NONE;
1133 }
1134 }
1135 else
1136 {
1137 DBG1(DBG_IKE, "triggered check found");
1138 }
1139
1140 check_t *check = check_create();
1141 check->mid = pair->id;
1142 check->src = pair->local->clone(pair->local);
1143 check->dst = pair->remote->clone(pair->remote);
1144 check->connect_id = chunk_clone(checklist->connect_id);
1145 check->endpoint = endpoint_notify_create();
1146
1147 pair->state = CHECK_IN_PROGRESS;
1148
1149 send_check(this, checklist, check, pair, TRUE);
1150
1151 check_destroy(check);
1152
1153 /* schedule this job again */
1154 schedule_checks(this, checklist, ME_INTERVAL);
1155
1156 pthread_mutex_unlock(&(this->mutex));
1157
1158 /* we reschedule it manually */
1159 return JOB_REQUEUE_NONE;
1160 }
1161
1162 /**
1163 * Schedules checks for a checklist (time in ms)
1164 */
1165 static void schedule_checks(private_connect_manager_t *this, check_list_t *checklist, u_int32_t time)
1166 {
1167 callback_data_t *data = callback_data_create(this, checklist->connect_id);
1168 checklist->sender = (job_t*)callback_job_create((callback_job_cb_t)sender, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
1169 charon->scheduler->schedule_job(charon->scheduler, checklist->sender, time);
1170 }
1171
1172 /**
1173 * Initiates waiting mediated connections
1174 */
1175 static job_requeue_t initiate_mediated(initiate_data_t *data)
1176 {
1177 check_list_t *checklist = data->checklist;
1178 initiated_t *initiated = data->initiated;
1179
1180 endpoint_pair_t *pair;
1181 if (get_best_valid_pair(checklist, &pair) == SUCCESS)
1182 {
1183 waiting_sa_t *waiting_sa;
1184 iterator_t *iterator = initiated->mediated->create_iterator(initiated->mediated, TRUE);
1185 while (iterator->iterate(iterator, (void**)&waiting_sa))
1186 {
1187 ike_sa_t *sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, waiting_sa->ike_sa_id);
1188 if (sa->initiate_mediated(sa, pair->local, pair->remote, waiting_sa->childs,
1189 checklist->connect_id) != SUCCESS)
1190 {
1191 SIG(IKE_UP_FAILED, "establishing the mediated connection failed");
1192 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, sa);
1193 }
1194 charon->ike_sa_manager->checkin(charon->ike_sa_manager, sa);
1195 }
1196 iterator->destroy(iterator);
1197 }
1198 else
1199 {
1200 /* this should (can?) not happen */
1201 }
1202
1203 return JOB_REQUEUE_NONE;
1204 }
1205
1206 /**
1207 * Finishes checks for a checklist
1208 */
1209 static void finish_checks(private_connect_manager_t *this, check_list_t *checklist)
1210 {
1211 if (checklist->is_initiator)
1212 {
1213 initiated_t *initiated;
1214 if (get_initiated_by_ids(this, checklist->initiator.id,
1215 checklist->responder.id, &initiated) == SUCCESS)
1216 {
1217 remove_checklist(this, checklist);
1218 remove_initiated(this, initiated);
1219
1220 initiate_data_t *data = initiate_data_create(checklist, initiated);
1221 job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiate_mediated, data, (callback_job_cleanup_t)initiate_data_destroy, NULL);
1222 charon->processor->queue_job(charon->processor, job);
1223 return;
1224 }
1225 else
1226 {
1227 DBG1(DBG_IKE, "there is no mediated connection waiting between '%D' "
1228 "and '%D'", checklist->initiator.id, checklist->responder.id);
1229 }
1230 }
1231 }
1232
1233 /**
1234 * Process the response to one of our requests
1235 */
1236 static void process_response(private_connect_manager_t *this, check_t *check,
1237 check_list_t *checklist)
1238 {
1239 endpoint_pair_t *pair;
1240 if (get_pair_by_id(checklist, check->mid, &pair) == SUCCESS)
1241 {
1242 if (pair->local->equals(pair->local, check->dst) &&
1243 pair->remote->equals(pair->remote, check->src))
1244 {
1245 DBG1(DBG_IKE, "endpoint pair '%d' is valid: '%#H' - '%#H'", pair->id,
1246 pair->local, pair->remote);
1247 pair->state = CHECK_SUCCEEDED;
1248 }
1249
1250 linked_list_t *local_endpoints = checklist->is_initiator ?
1251 checklist->initiator.endpoints : checklist->responder.endpoints;
1252
1253 endpoint_notify_t *local_endpoint;
1254 if (endpoints_contain(local_endpoints,
1255 check->endpoint->get_host(check->endpoint), &local_endpoint) != SUCCESS)
1256 {
1257 local_endpoint = endpoint_notify_create_from_host(PEER_REFLEXIVE,
1258 check->endpoint->get_host(check->endpoint), pair->local);
1259 local_endpoint->set_priority(local_endpoint, check->endpoint->get_priority(check->endpoint));
1260 local_endpoints->insert_last(local_endpoints, local_endpoint);
1261 }
1262
1263 update_checklist_state(this, checklist);
1264
1265 switch(checklist->state)
1266 {
1267 case CHECK_SUCCEEDED:
1268 case CHECK_FAILED:
1269 finish_checks(this, checklist);
1270 break;
1271 default:
1272 break;
1273 }
1274 }
1275 else
1276 {
1277 DBG1(DBG_IKE, "pair with id '%d' not found", check->mid);
1278 }
1279 }
1280
1281 static void process_request(private_connect_manager_t *this, check_t *check,
1282 check_list_t *checklist)
1283 {
1284 linked_list_t *remote_endpoints = checklist->is_initiator ?
1285 checklist->responder.endpoints : checklist->initiator.endpoints;
1286
1287 endpoint_notify_t *peer_reflexive, *remote_endpoint;
1288 peer_reflexive = endpoint_notify_create_from_host(PEER_REFLEXIVE, check->src, NULL);
1289 peer_reflexive->set_priority(peer_reflexive, check->endpoint->get_priority(check->endpoint));
1290
1291 if (endpoints_contain(remote_endpoints, check->src, &remote_endpoint) != SUCCESS)
1292 {
1293 remote_endpoint = peer_reflexive->clone(peer_reflexive);
1294 remote_endpoints->insert_last(remote_endpoints, remote_endpoint);
1295 }
1296
1297 endpoint_pair_t *pair;
1298 if (get_pair_by_hosts(checklist->pairs, check->dst, check->src, &pair) == SUCCESS)
1299 {
1300 switch(pair->state)
1301 {
1302 case CHECK_IN_PROGRESS:
1303 /* prevent retransmissions */
1304 pair->retransmitted = ME_MAX_RETRANS;
1305 /* FIXME: we should wait to the next rto to send the triggered check
1306 * fall-through */
1307 case CHECK_WAITING:
1308 case CHECK_FAILED:
1309 queue_triggered_check(this, checklist, pair);
1310 break;
1311 case CHECK_SUCCEEDED:
1312 default:
1313 break;
1314 }
1315 }
1316 else
1317 {
1318 endpoint_notify_t *local_endpoint = endpoint_notify_create_from_host(HOST, check->dst, NULL);
1319
1320 endpoint_notify_t *initiator = checklist->is_initiator ? local_endpoint : remote_endpoint;
1321 endpoint_notify_t *responder = checklist->is_initiator ? remote_endpoint : local_endpoint;
1322
1323 pair = endpoint_pair_create(initiator, responder, checklist->is_initiator);
1324 pair->id = checklist->pairs->get_count(checklist->pairs) + 1;
1325
1326 insert_pair_by_priority(checklist->pairs, pair);
1327
1328 queue_triggered_check(this, checklist, pair);
1329
1330 local_endpoint->destroy(local_endpoint);
1331 }
1332
1333
1334 check_t *response = check_create();
1335
1336 response->mid = check->mid;
1337 response->src = check->dst->clone(check->dst);
1338 response->dst = check->src->clone(check->src);
1339 response->connect_id = chunk_clone(check->connect_id);
1340 response->endpoint = peer_reflexive;
1341
1342 send_check(this, checklist, response, pair, FALSE);
1343
1344 check_destroy(response);
1345 }
1346
1347 /**
1348 * Implementation of connect_manager_t.process_check.
1349 */
1350 static void process_check(private_connect_manager_t *this, message_t *message)
1351 {
1352 if (message->parse_body(message, NULL, NULL) != SUCCESS)
1353 {
1354 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1355 exchange_type_names, message->get_exchange_type(message),
1356 message->get_request(message) ? "request" : "response",
1357 message->get_message_id(message));
1358 return;
1359 }
1360
1361 check_t *check = check_create();
1362 check->mid = message->get_message_id(message);
1363 check->src = message->get_source(message);
1364 check->src = check->src->clone(check->src);
1365 check->dst = message->get_destination(message);
1366 check->dst = check->dst->clone(check->dst);
1367
1368 if (process_payloads(message, check) != SUCCESS)
1369 {
1370 DBG1(DBG_IKE, "invalid connectivity check %s received",
1371 message->get_request(message) ? "request" : "response");
1372 check_destroy(check);
1373 return;
1374 }
1375
1376 pthread_mutex_lock(&(this->mutex));
1377
1378 check_list_t *checklist;
1379 if (get_checklist_by_id(this, check->connect_id, &checklist) != SUCCESS)
1380 {
1381 DBG1(DBG_IKE, "checklist with id '%#B' not found",
1382 &check->connect_id);
1383 check_destroy(check);
1384 pthread_mutex_unlock(&(this->mutex));
1385 return;
1386 }
1387
1388 chunk_t sig = build_signature(this, checklist, check, FALSE);
1389 if (!chunk_equals(sig, check->auth))
1390 {
1391 DBG1(DBG_IKE, "connectivity check verification failed");
1392 check_destroy(check);
1393 chunk_free(&sig);
1394 pthread_mutex_unlock(&(this->mutex));
1395 return;
1396 }
1397 chunk_free(&sig);
1398
1399 if (message->get_request(message))
1400 {
1401 process_request(this, check, checklist);
1402 }
1403 else
1404 {
1405 process_response(this, check, checklist);
1406 }
1407
1408 pthread_mutex_unlock(&(this->mutex));
1409
1410 check_destroy(check);
1411 }
1412
1413 /**
1414 * Implementation of connect_manager_t.check_and_register.
1415 */
1416 static bool check_and_register(private_connect_manager_t *this,
1417 identification_t *id, identification_t *peer_id,
1418 ike_sa_id_t *mediated_sa, child_cfg_t *child)
1419 {
1420 initiated_t *initiated;
1421 bool already_there = TRUE;
1422
1423 pthread_mutex_lock(&(this->mutex));
1424
1425 if (get_initiated_by_ids(this, id, peer_id, &initiated) != SUCCESS)
1426 {
1427 DBG2(DBG_IKE, "registered waiting mediated connection with '%D'", peer_id);
1428 initiated = initiated_create(id, peer_id);
1429 this->initiated->insert_last(this->initiated, initiated);
1430 already_there = FALSE;
1431 }
1432
1433 waiting_sa_t *waiting_sa;
1434 if (get_waiting_sa(initiated, mediated_sa, &waiting_sa) != SUCCESS)
1435 {
1436 waiting_sa = waiting_sa_create(mediated_sa);
1437 initiated->mediated->insert_last(initiated->mediated, waiting_sa);
1438 }
1439
1440 child->get_ref(child);
1441 waiting_sa->childs->insert_last(waiting_sa->childs, child);
1442
1443 pthread_mutex_unlock(&(this->mutex));
1444
1445 return already_there;
1446 }
1447
1448 /**
1449 * Implementation of connect_manager_t.check_and_initiate.
1450 */
1451 static void check_and_initiate(private_connect_manager_t *this, ike_sa_id_t *mediation_sa,
1452 identification_t *id, identification_t *peer_id)
1453 {
1454 initiated_t *initiated;
1455
1456 pthread_mutex_lock(&(this->mutex));
1457
1458 if (get_initiated_by_ids(this, id, peer_id, &initiated) != SUCCESS)
1459 {
1460 DBG2(DBG_IKE, "no waiting mediated connections with '%D'", peer_id);
1461 pthread_mutex_unlock(&(this->mutex));
1462 return;
1463 }
1464
1465 waiting_sa_t *waiting_sa;
1466 iterator_t *iterator = initiated->mediated->create_iterator(initiated->mediated, TRUE);
1467 while (iterator->iterate(iterator, (void**)&waiting_sa))
1468 {
1469 job_t *job = (job_t*)reinitiate_mediation_job_create(mediation_sa,
1470 waiting_sa->ike_sa_id);
1471 charon->processor->queue_job(charon->processor, job);
1472 }
1473 iterator->destroy(iterator);
1474
1475 pthread_mutex_unlock(&(this->mutex));
1476 }
1477
1478 /**
1479 * Implementation of connect_manager_t.set_initiator_data.
1480 */
1481 static status_t set_initiator_data(private_connect_manager_t *this,
1482 identification_t *initiator, identification_t *responder,
1483 chunk_t connect_id, chunk_t key, linked_list_t *endpoints, bool is_initiator)
1484 {
1485 check_list_t *checklist;
1486
1487 pthread_mutex_lock(&(this->mutex));
1488
1489 if (get_checklist_by_id(this, connect_id, NULL) == SUCCESS)
1490 {
1491 DBG1(DBG_IKE, "checklist with id '%#B' already exists, aborting",
1492 &connect_id);
1493 pthread_mutex_unlock(&(this->mutex));
1494 return FAILED;
1495 }
1496
1497 checklist = check_list_create(initiator, responder, connect_id, key, endpoints, is_initiator);
1498 this->checklists->insert_last(this->checklists, checklist);
1499
1500 pthread_mutex_unlock(&(this->mutex));
1501
1502 return SUCCESS;
1503 }
1504
1505 /**
1506 * Implementation of connect_manager_t.set_responder_data.
1507 */
1508 static status_t set_responder_data(private_connect_manager_t *this,
1509 chunk_t connect_id, chunk_t key, linked_list_t *endpoints)
1510 {
1511 check_list_t *checklist;
1512
1513 pthread_mutex_lock(&(this->mutex));
1514
1515 if (get_checklist_by_id(this, connect_id, &checklist) != SUCCESS)
1516 {
1517 DBG1(DBG_IKE, "checklist with id '%#B' not found",
1518 &connect_id);
1519 pthread_mutex_unlock(&(this->mutex));
1520 return NOT_FOUND;
1521 }
1522
1523 checklist->responder.key = chunk_clone(key);
1524 checklist->responder.endpoints = endpoints->clone_offset(endpoints, offsetof(endpoint_notify_t, clone));
1525 checklist->state = CHECK_WAITING;
1526
1527 build_pairs(checklist);
1528
1529 /* send the first check immediately */
1530 schedule_checks(this, checklist, 0);
1531
1532 pthread_mutex_unlock(&(this->mutex));
1533
1534 return SUCCESS;
1535 }
1536
1537 /**
1538 * Implementation of connect_manager_t.stop_checks.
1539 */
1540 static status_t stop_checks(private_connect_manager_t *this, chunk_t connect_id)
1541 {
1542 check_list_t *checklist;
1543
1544 pthread_mutex_lock(&(this->mutex));
1545
1546 if (get_checklist_by_id(this, connect_id, &checklist) != SUCCESS)
1547 {
1548 DBG1(DBG_IKE, "checklist with id '%#B' not found",
1549 &connect_id);
1550 pthread_mutex_unlock(&(this->mutex));
1551 return NOT_FOUND;
1552 }
1553
1554 DBG1(DBG_IKE, "removing checklist with id '%#B'", &connect_id);
1555
1556 remove_checklist(this, checklist);
1557 check_list_destroy(checklist);
1558
1559 pthread_mutex_unlock(&(this->mutex));
1560
1561 return SUCCESS;
1562 }
1563
1564 /**
1565 * Implementation of connect_manager_t.destroy.
1566 */
1567 static void destroy(private_connect_manager_t *this)
1568 {
1569 pthread_mutex_lock(&(this->mutex));
1570
1571 this->hasher->destroy(this->hasher);
1572 this->checklists->destroy_function(this->checklists, (void*)check_list_destroy);
1573 this->initiated->destroy_function(this->initiated, (void*)initiated_destroy);
1574
1575 pthread_mutex_unlock(&(this->mutex));
1576 pthread_mutex_destroy(&(this->mutex));
1577 free(this);
1578 }
1579
1580 /*
1581 * Described in header.
1582 */
1583 connect_manager_t *connect_manager_create()
1584 {
1585 private_connect_manager_t *this = malloc_thing(private_connect_manager_t);
1586
1587 this->public.destroy = (void(*)(connect_manager_t*))destroy;
1588 this->public.check_and_register = (bool(*)(connect_manager_t*,identification_t*,identification_t*,ike_sa_id_t*,child_cfg_t*))check_and_register;
1589 this->public.check_and_initiate = (void(*)(connect_manager_t*,ike_sa_id_t*,identification_t*,identification_t*))check_and_initiate;
1590 this->public.set_initiator_data = (status_t(*)(connect_manager_t*,identification_t*,identification_t*,chunk_t,chunk_t,linked_list_t*,bool))set_initiator_data;
1591 this->public.set_responder_data = (status_t(*)(connect_manager_t*,chunk_t,chunk_t,linked_list_t*))set_responder_data;
1592 this->public.process_check = (void(*)(connect_manager_t*,message_t*))process_check;
1593 this->public.stop_checks = (status_t(*)(connect_manager_t*,chunk_t))stop_checks;
1594
1595 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1596 if (this->hasher == NULL)
1597 {
1598 DBG1(DBG_IKE, "unable to create connect manager, SHA1 not supported");
1599 free(this);
1600 return NULL;
1601 }
1602
1603 this->checklists = linked_list_create();
1604 this->initiated = linked_list_create();
1605
1606 pthread_mutex_init(&(this->mutex), NULL);
1607
1608 return (connect_manager_t*)this;
1609 }