printing the checklist, two bugfixes
[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 * Prints all the pairs on a checklist
694 */
695 static void print_checklist(check_list_t *checklist)
696 {
697 iterator_t *iterator;
698 endpoint_pair_t *current;
699
700 DBG1(DBG_IKE, "pairs on checklist %#B:", &checklist->connect_id);
701 iterator = checklist->pairs->create_iterator(checklist->pairs, TRUE);
702 while (iterator->iterate(iterator, (void**)&current))
703 {
704 DBG1(DBG_IKE, " * %#H - %#H (%d)", current->local, current->remote,
705 current->priority);
706 }
707 iterator->destroy(iterator);
708 }
709
710 /**
711 * Prunes identical pairs with lower priority from the list
712 * Note: this function also numbers the remaining pairs serially
713 */
714 static void prune_pairs(linked_list_t *pairs)
715 {
716 iterator_t *iterator, *search;
717 endpoint_pair_t *current, *other;
718 u_int32_t id = 0;
719
720 iterator = pairs->create_iterator(pairs, TRUE);
721 search = pairs->create_iterator(pairs, TRUE);
722 while (iterator->iterate(iterator, (void**)&current))
723 {
724 current->id = ++id;
725
726 while (search->iterate(search, (void**)&other))
727 {
728 if (current == other)
729 {
730 continue;
731 }
732
733 if (current->local->equals(current->local, other->local) &&
734 current->remote->equals(current->remote, other->remote))
735 {
736 /* since the list of pairs is sorted by priority in descending
737 * order, and we iterate the list from the beginning, we are
738 * sure that the priority of 'other' is lower than that of
739 * 'current', remove it */
740 DBG1(DBG_IKE, "pruning endpoint pair %#H - %#H with priority %d",
741 other->local, other->remote, other->priority);
742 search->remove(search);
743 endpoint_pair_destroy(other);
744 }
745 }
746 search->reset(search);
747 }
748 search->destroy(search);
749 iterator->destroy(iterator);
750 }
751
752 /**
753 * Builds a list of endpoint pairs
754 */
755 static void build_pairs(check_list_t *checklist)
756 {
757 iterator_t *iterator_i, *iterator_r;
758 endpoint_notify_t *initiator, *responder;
759
760 iterator_i = checklist->initiator.endpoints->create_iterator(checklist->initiator.endpoints, TRUE);
761 while (iterator_i->iterate(iterator_i, (void**)&initiator))
762 {
763 iterator_r = checklist->responder.endpoints->create_iterator(checklist->responder.endpoints, TRUE);
764 while (iterator_r->iterate(iterator_r, (void**)&responder))
765 {
766 if (initiator->get_family(initiator) != responder->get_family(responder))
767 {
768 continue;
769 }
770
771 insert_pair_by_priority(checklist->pairs,
772 endpoint_pair_create(initiator, responder, checklist->is_initiator));
773 }
774 iterator_r->destroy(iterator_r);
775 }
776 iterator_i->destroy(iterator_i);
777
778 print_checklist(checklist);
779
780 prune_pairs(checklist->pairs);
781 }
782
783 /**
784 * Processes the payloads of a connectivity check and returns the extracted data
785 */
786 static status_t process_payloads(message_t *message, check_t *check)
787 {
788 iterator_t *iterator;
789 payload_t *payload;
790
791 iterator = message->get_payload_iterator(message);
792 while (iterator->iterate(iterator, (void**)&payload))
793 {
794 if (payload->get_type(payload) != NOTIFY)
795 {
796 DBG1(DBG_IKE, "ignoring payload of type '%N' while processing "
797 "connectivity check", payload_type_names, payload->get_type(payload));
798 continue;
799 }
800
801 notify_payload_t *notify = (notify_payload_t*)payload;
802
803 switch (notify->get_notify_type(notify))
804 {
805 case ME_ENDPOINT:
806 {
807 if (check->endpoint)
808 {
809 DBG1(DBG_IKE, "connectivity check contains multiple ME_ENDPOINT notifies");
810 break;
811 }
812
813 endpoint_notify_t *endpoint = endpoint_notify_create_from_payload(notify);
814 if (!endpoint)
815 {
816 DBG1(DBG_IKE, "received invalid ME_ENDPOINT notify");
817 break;
818 }
819 check->endpoint = endpoint;
820 check->endpoint_raw = chunk_clone(notify->get_notification_data(notify));
821 DBG2(DBG_IKE, "received ME_ENDPOINT notify");
822 break;
823 }
824 case ME_CONNECTID:
825 {
826 if (check->connect_id.ptr)
827 {
828 DBG1(DBG_IKE, "connectivity check contains multiple ME_CONNECTID notifies");
829 break;
830 }
831 check->connect_id = chunk_clone(notify->get_notification_data(notify));
832 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &check->connect_id);
833 break;
834 }
835 case ME_CONNECTAUTH:
836 {
837 if (check->auth.ptr)
838 {
839 DBG1(DBG_IKE, "connectivity check contains multiple ME_CONNECTAUTH notifies");
840 break;
841 }
842 check->auth = chunk_clone(notify->get_notification_data(notify));
843 DBG2(DBG_IKE, "received ME_CONNECTAUTH %#B", &check->auth);
844 break;
845 }
846 default:
847 break;
848 }
849 }
850 iterator->destroy(iterator);
851
852 if (!check->connect_id.ptr || !check->endpoint || !check->auth.ptr)
853 {
854 DBG1(DBG_IKE, "at least one payload was missing from the connectivity check");
855 return FAILED;
856 }
857
858 return SUCCESS;
859 }
860
861 /**
862 * Builds the signature for a connectivity check
863 */
864 static chunk_t build_signature(private_connect_manager_t *this,
865 check_list_t *checklist, check_t *check, bool outbound)
866 {
867 chunk_t mid_chunk, key_chunk, sig_chunk;
868 chunk_t sig_hash;
869
870 mid_chunk = chunk_from_thing(check->mid);
871
872 key_chunk = (checklist->is_initiator && outbound) || (!checklist->is_initiator && !outbound)
873 ? checklist->initiator.key : checklist->responder.key;
874
875 /* signature = SHA1( MID | ME_CONNECTID | ME_ENDPOINT | ME_CONNECTKEY ) */
876 sig_chunk = chunk_cat("cccc", mid_chunk, check->connect_id, check->endpoint_raw, key_chunk);
877 this->hasher->allocate_hash(this->hasher, sig_chunk, &sig_hash);
878 DBG3(DBG_IKE, "sig_chunk %#B", &sig_chunk);
879 DBG3(DBG_IKE, "sig_hash %#B", &sig_hash);
880
881 chunk_free(&sig_chunk);
882 return sig_hash;
883 }
884
885 static void queue_retransmission(private_connect_manager_t *this, check_list_t *checklist, endpoint_pair_t *pair);
886 static void schedule_checks(private_connect_manager_t *this, check_list_t *checklist, u_int32_t time);
887 static void finish_checks(private_connect_manager_t *this, check_list_t *checklist);
888
889 /**
890 * After one of the initiator's pairs has succeeded we finish the checks without
891 * waiting for all the timeouts
892 */
893 static job_requeue_t initiator_finish(callback_data_t *data)
894 {
895 private_connect_manager_t *this = data->connect_manager;
896
897 pthread_mutex_lock(&(this->mutex));
898
899 check_list_t *checklist;
900 if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
901 {
902 DBG1(DBG_IKE, "checklist with id '%#B' not found, can't finish connectivity checks",
903 &data->connect_id);
904 pthread_mutex_unlock(&(this->mutex));
905 return JOB_REQUEUE_NONE;
906 }
907
908 finish_checks(this, checklist);
909
910 pthread_mutex_unlock(&(this->mutex));
911
912 return JOB_REQUEUE_NONE;
913 }
914
915 /**
916 * Updates the state of the whole checklist
917 */
918 static void update_checklist_state(private_connect_manager_t *this, check_list_t *checklist)
919 {
920 iterator_t *iterator;
921 endpoint_pair_t *current;
922 bool in_progress = FALSE, succeeded = FALSE;
923
924 iterator = checklist->pairs->create_iterator(checklist->pairs, TRUE);
925 while (iterator->iterate(iterator, (void**)&current))
926 {
927 switch(current->state)
928 {
929 case CHECK_WAITING:
930 /* at least one is still waiting -> checklist remains
931 * in waiting state */
932 iterator->destroy(iterator);
933 return;
934 case CHECK_IN_PROGRESS:
935 in_progress = TRUE;
936 break;
937 case CHECK_SUCCEEDED:
938 succeeded = TRUE;
939 break;
940 default:
941 break;
942 }
943 }
944 iterator->destroy(iterator);
945
946 if (checklist->is_initiator && succeeded && !checklist->is_finishing)
947 {
948 /* instead of waiting until all checks have finished (i.e. all
949 * retransmissions have failed) the initiator finishes the checks
950 * right after the first check has succeeded. to allow a probably
951 * better pair to succeed, we still wait a certain time */
952 DBG2(DBG_IKE, "fast finishing checks for checklist '%#B'", &checklist->connect_id);
953
954 callback_data_t *data = callback_data_create(this, checklist->connect_id);
955 job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiator_finish, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
956 charon->scheduler->schedule_job(charon->scheduler, job, ME_WAIT_TO_FINISH);
957 checklist->is_finishing = TRUE;
958 }
959
960 if (in_progress)
961 {
962 checklist->state = CHECK_IN_PROGRESS;
963 }
964 else if (succeeded)
965 {
966 checklist->state = CHECK_SUCCEEDED;
967 }
968 else
969 {
970 checklist->state = CHECK_FAILED;
971 }
972 }
973
974 /**
975 * This function is triggered for each sent check after a specific timeout
976 */
977 static job_requeue_t retransmit(callback_data_t *data)
978 {
979 private_connect_manager_t *this = data->connect_manager;
980
981 pthread_mutex_lock(&(this->mutex));
982
983 check_list_t *checklist;
984 if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
985 {
986 DBG1(DBG_IKE, "checklist with id '%#B' not found, can't retransmit connectivity check",
987 &data->connect_id);
988 pthread_mutex_unlock(&(this->mutex));
989 return JOB_REQUEUE_NONE;
990 }
991
992 endpoint_pair_t *pair;
993 if (get_pair_by_id(checklist, data->mid, &pair) != SUCCESS)
994 {
995 DBG1(DBG_IKE, "pair with id '%d' not found, can't retransmit connectivity check",
996 data->mid);
997 goto retransmit_end;
998 }
999
1000 if (pair->state != CHECK_IN_PROGRESS)
1001 {
1002 DBG2(DBG_IKE, "pair with id '%d' is in wrong state [%d], don't retransmit the connectivity check",
1003 data->mid, pair->state);
1004 goto retransmit_end;
1005 }
1006
1007 if (++pair->retransmitted > ME_MAX_RETRANS)
1008 {
1009 DBG2(DBG_IKE, "pair with id '%d' failed after %d retransmissions",
1010 data->mid, ME_MAX_RETRANS);
1011 pair->state = CHECK_FAILED;
1012 goto retransmit_end;
1013 }
1014
1015 charon->sender->send(charon->sender, pair->packet->clone(pair->packet));
1016
1017 queue_retransmission(this, checklist, pair);
1018
1019 retransmit_end:
1020 update_checklist_state(this, checklist);
1021
1022 switch(checklist->state)
1023 {
1024 case CHECK_SUCCEEDED:
1025 case CHECK_FAILED:
1026 finish_checks(this, checklist);
1027 break;
1028 default:
1029 break;
1030 }
1031
1032 pthread_mutex_unlock(&(this->mutex));
1033
1034 /* we reschedule it manually */
1035 return JOB_REQUEUE_NONE;
1036 }
1037
1038 /**
1039 * Queues a retransmission job
1040 */
1041 static void queue_retransmission(private_connect_manager_t *this, check_list_t *checklist, endpoint_pair_t *pair)
1042 {
1043 callback_data_t *data = retransmit_data_create(this, checklist->connect_id, pair->id);
1044 job_t *job = (job_t*)callback_job_create((callback_job_cb_t)retransmit, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
1045
1046 u_int32_t retransmission = pair->retransmitted + 1;
1047 u_int32_t rto = ME_INTERVAL;
1048 if (retransmission > ME_BOOST)
1049 {
1050 rto = (u_int32_t)(ME_INTERVAL * pow(ME_RETRANS_BASE, retransmission - ME_BOOST));
1051 }
1052 DBG2(DBG_IKE, "scheduling retransmission %d of pair '%d' in %dms", retransmission, pair->id, rto);
1053
1054 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job, rto);
1055 }
1056
1057 /**
1058 * Sends a check
1059 */
1060 static void send_check(private_connect_manager_t *this, check_list_t *checklist,
1061 check_t *check, endpoint_pair_t *pair, bool request)
1062 {
1063 message_t *message = message_create();
1064 message->set_message_id(message, check->mid);
1065 message->set_exchange_type(message, INFORMATIONAL);
1066 message->set_request(message, request);
1067 message->set_destination(message, check->dst->clone(check->dst));
1068 message->set_source(message, check->src->clone(check->src));
1069
1070 ike_sa_id_t *ike_sa_id = ike_sa_id_create(0, 0, request);
1071 message->set_ike_sa_id(message, ike_sa_id);
1072 ike_sa_id->destroy(ike_sa_id);
1073
1074 message->add_notify(message, FALSE, ME_CONNECTID, check->connect_id);
1075 DBG2(DBG_IKE, "send ME_CONNECTID %#B", &check->connect_id);
1076
1077 notify_payload_t *endpoint = check->endpoint->build_notify(check->endpoint);
1078 check->endpoint_raw = chunk_clone(endpoint->get_notification_data(endpoint));
1079 message->add_payload(message, (payload_t*)endpoint);
1080 DBG2(DBG_IKE, "send ME_ENDPOINT notify");
1081
1082 check->auth = build_signature(this, checklist, check, TRUE);
1083 message->add_notify(message, FALSE, ME_CONNECTAUTH, check->auth);
1084 DBG2(DBG_IKE, "send ME_CONNECTAUTH %#B", &check->auth);
1085
1086 packet_t *packet;
1087 if (message->generate(message, NULL, NULL, &packet) == SUCCESS)
1088 {
1089 charon->sender->send(charon->sender, packet->clone(packet));
1090
1091 if (request)
1092 {
1093 DESTROY_IF(pair->packet);
1094 pair->packet = packet;
1095 pair->retransmitted = 0;
1096 queue_retransmission(this, checklist, pair);
1097 }
1098 else
1099 {
1100 packet->destroy(packet);
1101 }
1102 }
1103 message->destroy(message);
1104 }
1105
1106 /**
1107 * Queues a triggered check
1108 */
1109 static void queue_triggered_check(private_connect_manager_t *this,
1110 check_list_t *checklist, endpoint_pair_t *pair)
1111 {
1112 DBG2(DBG_IKE, "queueing triggered check for pair '%d'", pair->id);
1113 pair->state = CHECK_WAITING;
1114 checklist->triggered->insert_last(checklist->triggered, pair);
1115
1116 if (!checklist->sender)
1117 {
1118 /* if the sender is not running we restart it */
1119 schedule_checks(this, checklist, ME_INTERVAL);
1120 }
1121 }
1122
1123 /**
1124 * This function is triggered for each checklist at a specific interval
1125 */
1126 static job_requeue_t sender(callback_data_t *data)
1127 {
1128 private_connect_manager_t *this = data->connect_manager;
1129
1130 pthread_mutex_lock(&(this->mutex));
1131
1132 check_list_t *checklist;
1133 if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
1134 {
1135 DBG1(DBG_IKE, "checklist with id '%#B' not found, can't send connectivity check",
1136 &data->connect_id);
1137 pthread_mutex_unlock(&(this->mutex));
1138 return JOB_REQUEUE_NONE;
1139 }
1140
1141 /* reset the sender */
1142 checklist->sender = NULL;
1143
1144 endpoint_pair_t *pair;
1145 if (get_triggered_pair(checklist, &pair) != SUCCESS)
1146 {
1147 DBG1(DBG_IKE, "no triggered check queued, sending an ordinary check");
1148
1149 if (checklist->pairs->find_first(checklist->pairs,
1150 (linked_list_match_t)match_waiting_pair, (void**)&pair) != SUCCESS)
1151 {
1152 pthread_mutex_unlock(&(this->mutex));
1153 DBG1(DBG_IKE, "no pairs in waiting state, aborting");
1154 return JOB_REQUEUE_NONE;
1155 }
1156 }
1157 else
1158 {
1159 DBG1(DBG_IKE, "triggered check found");
1160 }
1161
1162 check_t *check = check_create();
1163 check->mid = pair->id;
1164 check->src = pair->local->clone(pair->local);
1165 check->dst = pair->remote->clone(pair->remote);
1166 check->connect_id = chunk_clone(checklist->connect_id);
1167 check->endpoint = endpoint_notify_create();
1168
1169 pair->state = CHECK_IN_PROGRESS;
1170
1171 send_check(this, checklist, check, pair, TRUE);
1172
1173 check_destroy(check);
1174
1175 /* schedule this job again */
1176 schedule_checks(this, checklist, ME_INTERVAL);
1177
1178 pthread_mutex_unlock(&(this->mutex));
1179
1180 /* we reschedule it manually */
1181 return JOB_REQUEUE_NONE;
1182 }
1183
1184 /**
1185 * Schedules checks for a checklist (time in ms)
1186 */
1187 static void schedule_checks(private_connect_manager_t *this, check_list_t *checklist, u_int32_t time)
1188 {
1189 callback_data_t *data = callback_data_create(this, checklist->connect_id);
1190 checklist->sender = (job_t*)callback_job_create((callback_job_cb_t)sender, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
1191 charon->scheduler->schedule_job(charon->scheduler, checklist->sender, time);
1192 }
1193
1194 /**
1195 * Initiates waiting mediated connections
1196 */
1197 static job_requeue_t initiate_mediated(initiate_data_t *data)
1198 {
1199 check_list_t *checklist = data->checklist;
1200 initiated_t *initiated = data->initiated;
1201
1202 endpoint_pair_t *pair;
1203 if (get_best_valid_pair(checklist, &pair) == SUCCESS)
1204 {
1205 waiting_sa_t *waiting_sa;
1206 iterator_t *iterator = initiated->mediated->create_iterator(initiated->mediated, TRUE);
1207 while (iterator->iterate(iterator, (void**)&waiting_sa))
1208 {
1209 ike_sa_t *sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, waiting_sa->ike_sa_id);
1210 if (sa->initiate_mediated(sa, pair->local, pair->remote, waiting_sa->childs,
1211 checklist->connect_id) != SUCCESS)
1212 {
1213 SIG(IKE_UP_FAILED, "establishing the mediated connection failed");
1214 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, sa);
1215 }
1216 charon->ike_sa_manager->checkin(charon->ike_sa_manager, sa);
1217 }
1218 iterator->destroy(iterator);
1219 }
1220 else
1221 {
1222 /* this should (can?) not happen */
1223 }
1224
1225 return JOB_REQUEUE_NONE;
1226 }
1227
1228 /**
1229 * Finishes checks for a checklist
1230 */
1231 static void finish_checks(private_connect_manager_t *this, check_list_t *checklist)
1232 {
1233 if (checklist->is_initiator)
1234 {
1235 initiated_t *initiated;
1236 if (get_initiated_by_ids(this, checklist->initiator.id,
1237 checklist->responder.id, &initiated) == SUCCESS)
1238 {
1239 remove_checklist(this, checklist);
1240 remove_initiated(this, initiated);
1241
1242 initiate_data_t *data = initiate_data_create(checklist, initiated);
1243 job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiate_mediated, data, (callback_job_cleanup_t)initiate_data_destroy, NULL);
1244 charon->processor->queue_job(charon->processor, job);
1245 return;
1246 }
1247 else
1248 {
1249 DBG1(DBG_IKE, "there is no mediated connection waiting between '%D' "
1250 "and '%D'", checklist->initiator.id, checklist->responder.id);
1251 }
1252 }
1253 }
1254
1255 /**
1256 * Process the response to one of our requests
1257 */
1258 static void process_response(private_connect_manager_t *this, check_t *check,
1259 check_list_t *checklist)
1260 {
1261 endpoint_pair_t *pair;
1262 if (get_pair_by_id(checklist, check->mid, &pair) == SUCCESS)
1263 {
1264 if (pair->local->equals(pair->local, check->dst) &&
1265 pair->remote->equals(pair->remote, check->src))
1266 {
1267 DBG1(DBG_IKE, "endpoint pair '%d' is valid: '%#H' - '%#H'", pair->id,
1268 pair->local, pair->remote);
1269 pair->state = CHECK_SUCCEEDED;
1270 }
1271
1272 linked_list_t *local_endpoints = checklist->is_initiator ?
1273 checklist->initiator.endpoints : checklist->responder.endpoints;
1274
1275 endpoint_notify_t *local_endpoint;
1276 if (endpoints_contain(local_endpoints,
1277 check->endpoint->get_host(check->endpoint), &local_endpoint) != SUCCESS)
1278 {
1279 local_endpoint = endpoint_notify_create_from_host(PEER_REFLEXIVE,
1280 check->endpoint->get_host(check->endpoint), pair->local);
1281 local_endpoint->set_priority(local_endpoint, check->endpoint->get_priority(check->endpoint));
1282 local_endpoints->insert_last(local_endpoints, local_endpoint);
1283 }
1284
1285 update_checklist_state(this, checklist);
1286
1287 switch(checklist->state)
1288 {
1289 case CHECK_SUCCEEDED:
1290 case CHECK_FAILED:
1291 finish_checks(this, checklist);
1292 break;
1293 default:
1294 break;
1295 }
1296 }
1297 else
1298 {
1299 DBG1(DBG_IKE, "pair with id '%d' not found", check->mid);
1300 }
1301 }
1302
1303 static void process_request(private_connect_manager_t *this, check_t *check,
1304 check_list_t *checklist)
1305 {
1306 linked_list_t *remote_endpoints = checklist->is_initiator ?
1307 checklist->responder.endpoints : checklist->initiator.endpoints;
1308
1309 endpoint_notify_t *peer_reflexive, *remote_endpoint;
1310 peer_reflexive = endpoint_notify_create_from_host(PEER_REFLEXIVE, check->src, NULL);
1311 peer_reflexive->set_priority(peer_reflexive, check->endpoint->get_priority(check->endpoint));
1312
1313 if (endpoints_contain(remote_endpoints, check->src, &remote_endpoint) != SUCCESS)
1314 {
1315 remote_endpoint = peer_reflexive->clone(peer_reflexive);
1316 remote_endpoints->insert_last(remote_endpoints, remote_endpoint);
1317 }
1318
1319 endpoint_pair_t *pair;
1320 if (get_pair_by_hosts(checklist->pairs, check->dst, check->src, &pair) == SUCCESS)
1321 {
1322 switch(pair->state)
1323 {
1324 case CHECK_IN_PROGRESS:
1325 /* prevent retransmissions */
1326 pair->retransmitted = ME_MAX_RETRANS;
1327 /* FIXME: we should wait to the next rto to send the triggered check
1328 * fall-through */
1329 case CHECK_WAITING:
1330 case CHECK_FAILED:
1331 queue_triggered_check(this, checklist, pair);
1332 break;
1333 case CHECK_SUCCEEDED:
1334 default:
1335 break;
1336 }
1337 }
1338 else
1339 {
1340 endpoint_notify_t *local_endpoint = endpoint_notify_create_from_host(HOST, check->dst, NULL);
1341
1342 endpoint_notify_t *initiator = checklist->is_initiator ? local_endpoint : remote_endpoint;
1343 endpoint_notify_t *responder = checklist->is_initiator ? remote_endpoint : local_endpoint;
1344
1345 pair = endpoint_pair_create(initiator, responder, checklist->is_initiator);
1346 pair->id = checklist->pairs->get_count(checklist->pairs) + 1;
1347
1348 insert_pair_by_priority(checklist->pairs, pair);
1349
1350 queue_triggered_check(this, checklist, pair);
1351
1352 local_endpoint->destroy(local_endpoint);
1353 }
1354
1355
1356 check_t *response = check_create();
1357
1358 response->mid = check->mid;
1359 response->src = check->dst->clone(check->dst);
1360 response->dst = check->src->clone(check->src);
1361 response->connect_id = chunk_clone(check->connect_id);
1362 response->endpoint = peer_reflexive;
1363
1364 send_check(this, checklist, response, pair, FALSE);
1365
1366 check_destroy(response);
1367 }
1368
1369 /**
1370 * Implementation of connect_manager_t.process_check.
1371 */
1372 static void process_check(private_connect_manager_t *this, message_t *message)
1373 {
1374 if (message->parse_body(message, NULL, NULL) != SUCCESS)
1375 {
1376 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1377 exchange_type_names, message->get_exchange_type(message),
1378 message->get_request(message) ? "request" : "response",
1379 message->get_message_id(message));
1380 return;
1381 }
1382
1383 check_t *check = check_create();
1384 check->mid = message->get_message_id(message);
1385 check->src = message->get_source(message);
1386 check->src = check->src->clone(check->src);
1387 check->dst = message->get_destination(message);
1388 check->dst = check->dst->clone(check->dst);
1389
1390 if (process_payloads(message, check) != SUCCESS)
1391 {
1392 DBG1(DBG_IKE, "invalid connectivity check %s received",
1393 message->get_request(message) ? "request" : "response");
1394 check_destroy(check);
1395 return;
1396 }
1397
1398 pthread_mutex_lock(&(this->mutex));
1399
1400 check_list_t *checklist;
1401 if (get_checklist_by_id(this, check->connect_id, &checklist) != SUCCESS)
1402 {
1403 DBG1(DBG_IKE, "checklist with id '%#B' not found",
1404 &check->connect_id);
1405 check_destroy(check);
1406 pthread_mutex_unlock(&(this->mutex));
1407 return;
1408 }
1409
1410 chunk_t sig = build_signature(this, checklist, check, FALSE);
1411 if (!chunk_equals(sig, check->auth))
1412 {
1413 DBG1(DBG_IKE, "connectivity check verification failed");
1414 check_destroy(check);
1415 chunk_free(&sig);
1416 pthread_mutex_unlock(&(this->mutex));
1417 return;
1418 }
1419 chunk_free(&sig);
1420
1421 if (message->get_request(message))
1422 {
1423 process_request(this, check, checklist);
1424 }
1425 else
1426 {
1427 process_response(this, check, checklist);
1428 }
1429
1430 pthread_mutex_unlock(&(this->mutex));
1431
1432 check_destroy(check);
1433 }
1434
1435 /**
1436 * Implementation of connect_manager_t.check_and_register.
1437 */
1438 static bool check_and_register(private_connect_manager_t *this,
1439 identification_t *id, identification_t *peer_id,
1440 ike_sa_id_t *mediated_sa, child_cfg_t *child)
1441 {
1442 initiated_t *initiated;
1443 bool already_there = TRUE;
1444
1445 pthread_mutex_lock(&(this->mutex));
1446
1447 if (get_initiated_by_ids(this, id, peer_id, &initiated) != SUCCESS)
1448 {
1449 DBG2(DBG_IKE, "registered waiting mediated connection with '%D'", peer_id);
1450 initiated = initiated_create(id, peer_id);
1451 this->initiated->insert_last(this->initiated, initiated);
1452 already_there = FALSE;
1453 }
1454
1455 waiting_sa_t *waiting_sa;
1456 if (get_waiting_sa(initiated, mediated_sa, &waiting_sa) != SUCCESS)
1457 {
1458 waiting_sa = waiting_sa_create(mediated_sa);
1459 initiated->mediated->insert_last(initiated->mediated, waiting_sa);
1460 }
1461
1462 child->get_ref(child);
1463 waiting_sa->childs->insert_last(waiting_sa->childs, child);
1464
1465 pthread_mutex_unlock(&(this->mutex));
1466
1467 return already_there;
1468 }
1469
1470 /**
1471 * Implementation of connect_manager_t.check_and_initiate.
1472 */
1473 static void check_and_initiate(private_connect_manager_t *this, ike_sa_id_t *mediation_sa,
1474 identification_t *id, identification_t *peer_id)
1475 {
1476 initiated_t *initiated;
1477
1478 pthread_mutex_lock(&(this->mutex));
1479
1480 if (get_initiated_by_ids(this, id, peer_id, &initiated) != SUCCESS)
1481 {
1482 DBG2(DBG_IKE, "no waiting mediated connections with '%D'", peer_id);
1483 pthread_mutex_unlock(&(this->mutex));
1484 return;
1485 }
1486
1487 waiting_sa_t *waiting_sa;
1488 iterator_t *iterator = initiated->mediated->create_iterator(initiated->mediated, TRUE);
1489 while (iterator->iterate(iterator, (void**)&waiting_sa))
1490 {
1491 job_t *job = (job_t*)reinitiate_mediation_job_create(mediation_sa,
1492 waiting_sa->ike_sa_id);
1493 charon->processor->queue_job(charon->processor, job);
1494 }
1495 iterator->destroy(iterator);
1496
1497 pthread_mutex_unlock(&(this->mutex));
1498 }
1499
1500 /**
1501 * Implementation of connect_manager_t.set_initiator_data.
1502 */
1503 static status_t set_initiator_data(private_connect_manager_t *this,
1504 identification_t *initiator, identification_t *responder,
1505 chunk_t connect_id, chunk_t key, linked_list_t *endpoints, bool is_initiator)
1506 {
1507 check_list_t *checklist;
1508
1509 pthread_mutex_lock(&(this->mutex));
1510
1511 if (get_checklist_by_id(this, connect_id, NULL) == SUCCESS)
1512 {
1513 DBG1(DBG_IKE, "checklist with id '%#B' already exists, aborting",
1514 &connect_id);
1515 pthread_mutex_unlock(&(this->mutex));
1516 return FAILED;
1517 }
1518
1519 checklist = check_list_create(initiator, responder, connect_id, key, endpoints, is_initiator);
1520 this->checklists->insert_last(this->checklists, checklist);
1521
1522 pthread_mutex_unlock(&(this->mutex));
1523
1524 return SUCCESS;
1525 }
1526
1527 /**
1528 * Implementation of connect_manager_t.set_responder_data.
1529 */
1530 static status_t set_responder_data(private_connect_manager_t *this,
1531 chunk_t connect_id, chunk_t key, linked_list_t *endpoints)
1532 {
1533 check_list_t *checklist;
1534
1535 pthread_mutex_lock(&(this->mutex));
1536
1537 if (get_checklist_by_id(this, connect_id, &checklist) != SUCCESS)
1538 {
1539 DBG1(DBG_IKE, "checklist with id '%#B' not found",
1540 &connect_id);
1541 pthread_mutex_unlock(&(this->mutex));
1542 return NOT_FOUND;
1543 }
1544
1545 checklist->responder.key = chunk_clone(key);
1546 checklist->responder.endpoints = endpoints->clone_offset(endpoints, offsetof(endpoint_notify_t, clone));
1547 checklist->state = CHECK_WAITING;
1548
1549 build_pairs(checklist);
1550
1551 /* send the first check immediately */
1552 schedule_checks(this, checklist, 0);
1553
1554 pthread_mutex_unlock(&(this->mutex));
1555
1556 return SUCCESS;
1557 }
1558
1559 /**
1560 * Implementation of connect_manager_t.stop_checks.
1561 */
1562 static status_t stop_checks(private_connect_manager_t *this, chunk_t connect_id)
1563 {
1564 check_list_t *checklist;
1565
1566 pthread_mutex_lock(&(this->mutex));
1567
1568 if (get_checklist_by_id(this, connect_id, &checklist) != SUCCESS)
1569 {
1570 DBG1(DBG_IKE, "checklist with id '%#B' not found",
1571 &connect_id);
1572 pthread_mutex_unlock(&(this->mutex));
1573 return NOT_FOUND;
1574 }
1575
1576 DBG1(DBG_IKE, "removing checklist with id '%#B'", &connect_id);
1577
1578 remove_checklist(this, checklist);
1579 check_list_destroy(checklist);
1580
1581 pthread_mutex_unlock(&(this->mutex));
1582
1583 return SUCCESS;
1584 }
1585
1586 /**
1587 * Implementation of connect_manager_t.destroy.
1588 */
1589 static void destroy(private_connect_manager_t *this)
1590 {
1591 pthread_mutex_lock(&(this->mutex));
1592
1593 this->hasher->destroy(this->hasher);
1594 this->checklists->destroy_function(this->checklists, (void*)check_list_destroy);
1595 this->initiated->destroy_function(this->initiated, (void*)initiated_destroy);
1596
1597 pthread_mutex_unlock(&(this->mutex));
1598 pthread_mutex_destroy(&(this->mutex));
1599 free(this);
1600 }
1601
1602 /*
1603 * Described in header.
1604 */
1605 connect_manager_t *connect_manager_create()
1606 {
1607 private_connect_manager_t *this = malloc_thing(private_connect_manager_t);
1608
1609 this->public.destroy = (void(*)(connect_manager_t*))destroy;
1610 this->public.check_and_register = (bool(*)(connect_manager_t*,identification_t*,identification_t*,ike_sa_id_t*,child_cfg_t*))check_and_register;
1611 this->public.check_and_initiate = (void(*)(connect_manager_t*,ike_sa_id_t*,identification_t*,identification_t*))check_and_initiate;
1612 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;
1613 this->public.set_responder_data = (status_t(*)(connect_manager_t*,chunk_t,chunk_t,linked_list_t*))set_responder_data;
1614 this->public.process_check = (void(*)(connect_manager_t*,message_t*))process_check;
1615 this->public.stop_checks = (status_t(*)(connect_manager_t*,chunk_t))stop_checks;
1616
1617 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1618 if (this->hasher == NULL)
1619 {
1620 DBG1(DBG_IKE, "unable to create connect manager, SHA1 not supported");
1621 free(this);
1622 return NULL;
1623 }
1624
1625 this->checklists = linked_list_create();
1626 this->initiated = linked_list_create();
1627
1628 pthread_mutex_init(&(this->mutex), NULL);
1629
1630 return (connect_manager_t*)this;
1631 }