f55821997557950f38c6938a292fae020c67d4f0
[strongswan.git] / src / libcharon / sa / trap_manager.c
1 /*
2 * Copyright (C) 2011-2017 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "trap_manager.h"
18
19 #include <daemon.h>
20 #include <threading/mutex.h>
21 #include <threading/rwlock.h>
22 #include <threading/rwlock_condvar.h>
23 #include <collections/linked_list.h>
24
25 #define INSTALL_DISABLED ((u_int)~0)
26
27 typedef struct private_trap_manager_t private_trap_manager_t;
28 typedef struct trap_listener_t trap_listener_t;
29
30 /**
31 * listener to track acquires
32 */
33 struct trap_listener_t {
34
35 /**
36 * Implements listener interface
37 */
38 listener_t listener;
39
40 /**
41 * points to trap_manager
42 */
43 private_trap_manager_t *traps;
44 };
45
46 /**
47 * Private data of an trap_manager_t object.
48 */
49 struct private_trap_manager_t {
50
51 /**
52 * Public trap_manager_t interface.
53 */
54 trap_manager_t public;
55
56 /**
57 * Installed traps, as entry_t
58 */
59 linked_list_t *traps;
60
61 /**
62 * read write lock for traps list
63 */
64 rwlock_t *lock;
65
66 /**
67 * listener to track acquiring IKE_SAs
68 */
69 trap_listener_t listener;
70
71 /**
72 * list of acquires we currently handle
73 */
74 linked_list_t *acquires;
75
76 /**
77 * mutex for list of acquires
78 */
79 mutex_t *mutex;
80
81 /**
82 * number of threads currently installing trap policies, or INSTALL_DISABLED
83 */
84 u_int installing;
85
86 /**
87 * condvar to signal trap policy installation
88 */
89 rwlock_condvar_t *condvar;
90
91 /**
92 * Whether to ignore traffic selectors from acquires
93 */
94 bool ignore_acquire_ts;
95 };
96
97 /**
98 * A installed trap entry
99 */
100 typedef struct {
101 /** name of the trapped CHILD_SA */
102 char *name;
103 /** ref to peer_cfg to initiate */
104 peer_cfg_t *peer_cfg;
105 /** ref to instantiated CHILD_SA (i.e the trap policy) */
106 child_sa_t *child_sa;
107 /** TRUE in case of wildcard Transport Mode SA */
108 bool wildcard;
109 } entry_t;
110
111 /**
112 * A handled acquire
113 */
114 typedef struct {
115 /** pending IKE_SA connecting upon acquire */
116 ike_sa_t *ike_sa;
117 /** reqid of pending trap policy */
118 uint32_t reqid;
119 /** destination address (wildcard case) */
120 host_t *dst;
121 } acquire_t;
122
123 /**
124 * actually uninstall and destroy an installed entry
125 */
126 static void destroy_entry(entry_t *this)
127 {
128 this->child_sa->destroy(this->child_sa);
129 this->peer_cfg->destroy(this->peer_cfg);
130 free(this->name);
131 free(this);
132 }
133
134 /**
135 * destroy a cached acquire entry
136 */
137 static void destroy_acquire(acquire_t *this)
138 {
139 DESTROY_IF(this->dst);
140 free(this);
141 }
142
143 CALLBACK(acquire_by_reqid, bool,
144 acquire_t *this, va_list args)
145 {
146 uint32_t reqid;
147
148 VA_ARGS_VGET(args, reqid);
149 return this->reqid == reqid;
150 }
151
152 CALLBACK(acquire_by_dst, bool,
153 acquire_t *this, va_list args)
154 {
155 host_t *dst;
156
157 VA_ARGS_VGET(args, dst);
158 return this->dst && this->dst->ip_equals(this->dst, dst);
159 }
160
161 /**
162 * Check if any remote TS are dynamic
163 */
164 static bool dynamic_remote_ts(child_cfg_t *child)
165 {
166 enumerator_t *enumerator;
167 linked_list_t *other_ts;
168 traffic_selector_t *ts;
169 bool found = FALSE;
170
171 other_ts = child->get_traffic_selectors(child, FALSE, NULL, NULL);
172 enumerator = other_ts->create_enumerator(other_ts);
173 while (enumerator->enumerate(enumerator, &ts))
174 {
175 if (ts->is_dynamic(ts))
176 {
177 found = TRUE;
178 break;
179 }
180 }
181 enumerator->destroy(enumerator);
182 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
183 return found;
184 }
185
186 METHOD(trap_manager_t, install, bool,
187 private_trap_manager_t *this, peer_cfg_t *peer, child_cfg_t *child)
188 {
189 entry_t *entry, *found = NULL;
190 ike_cfg_t *ike_cfg;
191 child_sa_t *child_sa;
192 host_t *me, *other;
193 linked_list_t *my_ts, *other_ts, *list;
194 enumerator_t *enumerator;
195 status_t status;
196 linked_list_t *proposals;
197 proposal_t *proposal;
198 protocol_id_t proto = PROTO_ESP;
199 bool result = FALSE, wildcard = FALSE;
200
201 /* try to resolve addresses */
202 ike_cfg = peer->get_ike_cfg(peer);
203 other = ike_cfg->resolve_other(ike_cfg, AF_UNSPEC);
204 if (other && other->is_anyaddr(other) &&
205 child->get_mode(child) == MODE_TRANSPORT)
206 {
207 /* allow wildcard for Transport Mode SAs */
208 me = host_create_any(other->get_family(other));
209 wildcard = TRUE;
210 }
211 else if (other && other->is_anyaddr(other))
212 {
213 other->destroy(other);
214 DBG1(DBG_CFG, "installing trap failed, remote address unknown");
215 return FALSE;
216 }
217 else
218 { /* depending on the traffic selectors we don't really need a remote
219 * host yet, but we might fail later if no IP can be resolved */
220 if (!other && dynamic_remote_ts(child))
221 { /* with dynamic TS we do need a host, otherwise 0.0.0.0/0 is used,
222 * which is probably not what users expect*/
223 DBG1(DBG_CFG, "installing trap failed, remote address unknown with "
224 "dynamic traffic selector");
225 return FALSE;
226 }
227 me = ike_cfg->resolve_me(ike_cfg, other ? other->get_family(other)
228 : AF_UNSPEC);
229 if (!other)
230 {
231 other = host_create_any(me ? me->get_family(me) : AF_INET);
232 }
233 other->set_port(other, ike_cfg->get_other_port(ike_cfg));
234 if ((!me || me->is_anyaddr(me)) && !other->is_anyaddr(other))
235 {
236 DESTROY_IF(me);
237 me = charon->kernel->get_source_addr(charon->kernel, other, NULL);
238 }
239 if (!me)
240 {
241 me = host_create_any(other->get_family(other));
242 }
243 me->set_port(me, ike_cfg->get_my_port(ike_cfg));
244 }
245
246 this->lock->write_lock(this->lock);
247 if (this->installing == INSTALL_DISABLED)
248 { /* flush() has been called */
249 this->lock->unlock(this->lock);
250 other->destroy(other);
251 me->destroy(me);
252 return FALSE;
253 }
254 enumerator = this->traps->create_enumerator(this->traps);
255 while (enumerator->enumerate(enumerator, &entry))
256 {
257 if (streq(entry->name, child->get_name(child)) &&
258 streq(entry->peer_cfg->get_name(entry->peer_cfg),
259 peer->get_name(peer)))
260 {
261 found = entry;
262 if (entry->child_sa)
263 { /* replace it with an updated version, if already installed */
264 this->traps->remove_at(this->traps, enumerator);
265 }
266 break;
267 }
268 }
269 enumerator->destroy(enumerator);
270
271 if (found)
272 {
273 if (!found->child_sa)
274 {
275 DBG1(DBG_CFG, "CHILD_SA '%s' is already being routed", found->name);
276 this->lock->unlock(this->lock);
277 other->destroy(other);
278 me->destroy(me);
279 return FALSE;
280 }
281 /* config might have changed so update everything */
282 DBG1(DBG_CFG, "updating already routed CHILD_SA '%s'", found->name);
283 }
284
285 INIT(entry,
286 .name = strdup(child->get_name(child)),
287 .peer_cfg = peer->get_ref(peer),
288 .wildcard = wildcard,
289 );
290 this->traps->insert_first(this->traps, entry);
291 this->installing++;
292 /* don't hold lock while creating CHILD_SA and installing policies */
293 this->lock->unlock(this->lock);
294
295 /* create and route CHILD_SA */
296 child_sa = child_sa_create(me, other, child, 0, FALSE, 0, 0);
297
298 list = linked_list_create_with_items(me, NULL);
299 my_ts = child->get_traffic_selectors(child, TRUE, NULL, list);
300 list->destroy_offset(list, offsetof(host_t, destroy));
301
302 list = linked_list_create_with_items(other, NULL);
303 other_ts = child->get_traffic_selectors(child, FALSE, NULL, list);
304 list->destroy_offset(list, offsetof(host_t, destroy));
305
306 /* We don't know the finally negotiated protocol (ESP|AH), we install
307 * the SA with the protocol of the first proposal */
308 proposals = child->get_proposals(child, TRUE);
309 if (proposals->get_first(proposals, (void**)&proposal) == SUCCESS)
310 {
311 proto = proposal->get_protocol(proposal);
312 }
313 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
314 child_sa->set_protocol(child_sa, proto);
315 child_sa->set_mode(child_sa, child->get_mode(child));
316 child_sa->set_policies(child_sa, my_ts, other_ts);
317 status = child_sa->install_policies(child_sa);
318 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
319 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
320 if (status != SUCCESS)
321 {
322 DBG1(DBG_CFG, "installing trap failed");
323 this->lock->write_lock(this->lock);
324 this->traps->remove(this->traps, entry, NULL);
325 this->lock->unlock(this->lock);
326 entry->child_sa = child_sa;
327 destroy_entry(entry);
328 }
329 else
330 {
331 this->lock->write_lock(this->lock);
332 entry->child_sa = child_sa;
333 this->lock->unlock(this->lock);
334 result = TRUE;
335 }
336 if (found)
337 {
338 destroy_entry(found);
339 }
340 this->lock->write_lock(this->lock);
341 /* do this at the end, so entries created temporarily are also destroyed */
342 this->installing--;
343 this->condvar->signal(this->condvar);
344 this->lock->unlock(this->lock);
345 return result;
346 }
347
348 METHOD(trap_manager_t, uninstall, bool,
349 private_trap_manager_t *this, char *peer, char *child)
350 {
351 enumerator_t *enumerator;
352 entry_t *entry, *found = NULL;
353
354 this->lock->write_lock(this->lock);
355 enumerator = this->traps->create_enumerator(this->traps);
356 while (enumerator->enumerate(enumerator, &entry))
357 {
358 if (streq(entry->name, child) &&
359 (!peer || streq(peer, entry->peer_cfg->get_name(entry->peer_cfg))))
360 {
361 this->traps->remove_at(this->traps, enumerator);
362 found = entry;
363 break;
364 }
365 }
366 enumerator->destroy(enumerator);
367 this->lock->unlock(this->lock);
368
369 if (!found)
370 {
371 return FALSE;
372 }
373 destroy_entry(found);
374 return TRUE;
375 }
376
377 CALLBACK(trap_filter, bool,
378 rwlock_t *lock, enumerator_t *orig, va_list args)
379 {
380 entry_t *entry;
381 peer_cfg_t **peer_cfg;
382 child_sa_t **child_sa;
383
384 VA_ARGS_VGET(args, peer_cfg, child_sa);
385
386 while (orig->enumerate(orig, &entry))
387 {
388 if (!entry->child_sa)
389 { /* skip entries that are currently being installed */
390 continue;
391 }
392 if (peer_cfg)
393 {
394 *peer_cfg = entry->peer_cfg;
395 }
396 if (child_sa)
397 {
398 *child_sa = entry->child_sa;
399 }
400 return TRUE;
401 }
402 return FALSE;
403 }
404
405 METHOD(trap_manager_t, create_enumerator, enumerator_t*,
406 private_trap_manager_t *this)
407 {
408 this->lock->read_lock(this->lock);
409 return enumerator_create_filter(this->traps->create_enumerator(this->traps),
410 trap_filter, this->lock,
411 (void*)this->lock->unlock);
412 }
413
414 METHOD(trap_manager_t, find_reqid, uint32_t,
415 private_trap_manager_t *this, child_cfg_t *child)
416 {
417 enumerator_t *enumerator;
418 entry_t *entry;
419 uint32_t reqid = 0;
420
421 this->lock->read_lock(this->lock);
422 enumerator = this->traps->create_enumerator(this->traps);
423 while (enumerator->enumerate(enumerator, &entry))
424 {
425 if (streq(entry->name, child->get_name(child)))
426 {
427 if (entry->child_sa)
428 {
429 reqid = entry->child_sa->get_reqid(entry->child_sa);
430 }
431 break;
432 }
433 }
434 enumerator->destroy(enumerator);
435 this->lock->unlock(this->lock);
436 return reqid;
437 }
438
439 METHOD(trap_manager_t, acquire, void,
440 private_trap_manager_t *this, uint32_t reqid,
441 traffic_selector_t *src, traffic_selector_t *dst)
442 {
443 enumerator_t *enumerator;
444 entry_t *entry, *found = NULL;
445 acquire_t *acquire;
446 peer_cfg_t *peer;
447 child_cfg_t *child;
448 ike_sa_t *ike_sa;
449 host_t *host;
450 bool wildcard, ignore = FALSE;
451
452 this->lock->read_lock(this->lock);
453 enumerator = this->traps->create_enumerator(this->traps);
454 while (enumerator->enumerate(enumerator, &entry))
455 {
456 if (entry->child_sa &&
457 entry->child_sa->get_reqid(entry->child_sa) == reqid)
458 {
459 found = entry;
460 break;
461 }
462 }
463 enumerator->destroy(enumerator);
464
465 if (!found)
466 {
467 DBG1(DBG_CFG, "trap not found, unable to acquire reqid %d", reqid);
468 this->lock->unlock(this->lock);
469 return;
470 }
471 reqid = found->child_sa->get_reqid(found->child_sa);
472 wildcard = found->wildcard;
473
474 this->mutex->lock(this->mutex);
475 if (wildcard)
476 { /* for wildcard acquires we check that we don't have a pending acquire
477 * with the same peer */
478 uint8_t mask;
479
480 dst->to_subnet(dst, &host, &mask);
481 if (this->acquires->find_first(this->acquires, acquire_by_dst,
482 (void**)&acquire, host))
483 {
484 host->destroy(host);
485 ignore = TRUE;
486 }
487 else
488 {
489 INIT(acquire,
490 .dst = host,
491 .reqid = reqid,
492 );
493 this->acquires->insert_last(this->acquires, acquire);
494 }
495 }
496 else
497 {
498 if (this->acquires->find_first(this->acquires, acquire_by_reqid,
499 (void**)&acquire, reqid))
500 {
501 ignore = TRUE;
502 }
503 else
504 {
505 INIT(acquire,
506 .reqid = reqid,
507 );
508 this->acquires->insert_last(this->acquires, acquire);
509 }
510 }
511 this->mutex->unlock(this->mutex);
512 if (ignore)
513 {
514 DBG1(DBG_CFG, "ignoring acquire, connection attempt pending");
515 this->lock->unlock(this->lock);
516 return;
517 }
518 peer = found->peer_cfg->get_ref(found->peer_cfg);
519 child = found->child_sa->get_config(found->child_sa);
520 child = child->get_ref(child);
521 /* don't hold the lock while checking out the IKE_SA */
522 this->lock->unlock(this->lock);
523
524 if (wildcard)
525 { /* the peer config would match IKE_SAs with other peers */
526 ike_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
527 peer->get_ike_version(peer), TRUE);
528 if (ike_sa)
529 {
530 ike_cfg_t *ike_cfg;
531 uint16_t port;
532 uint8_t mask;
533
534 ike_sa->set_peer_cfg(ike_sa, peer);
535 ike_cfg = ike_sa->get_ike_cfg(ike_sa);
536
537 port = ike_cfg->get_other_port(ike_cfg);
538 dst->to_subnet(dst, &host, &mask);
539 host->set_port(host, port);
540 ike_sa->set_other_host(ike_sa, host);
541
542 port = ike_cfg->get_my_port(ike_cfg);
543 src->to_subnet(src, &host, &mask);
544 host->set_port(host, port);
545 ike_sa->set_my_host(ike_sa, host);
546
547 charon->bus->set_sa(charon->bus, ike_sa);
548 }
549 }
550 else
551 {
552 ike_sa = charon->ike_sa_manager->checkout_by_config(
553 charon->ike_sa_manager, peer);
554 }
555 if (ike_sa)
556 {
557 if (ike_sa->get_peer_cfg(ike_sa) == NULL)
558 {
559 ike_sa->set_peer_cfg(ike_sa, peer);
560 }
561 if (this->ignore_acquire_ts || ike_sa->get_version(ike_sa) == IKEV1)
562 { /* in IKEv1, don't prepend the acquiring packet TS, as we only
563 * have a single TS that we can establish in a Quick Mode. */
564 src = dst = NULL;
565 }
566
567 this->mutex->lock(this->mutex);
568 acquire->ike_sa = ike_sa;
569 this->mutex->unlock(this->mutex);
570
571 if (ike_sa->initiate(ike_sa, child, reqid, src, dst) != DESTROY_ME)
572 {
573 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
574 }
575 else
576 {
577 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
578 ike_sa);
579 }
580 }
581 else
582 {
583 this->mutex->lock(this->mutex);
584 this->acquires->remove(this->acquires, acquire, NULL);
585 this->mutex->unlock(this->mutex);
586 destroy_acquire(acquire);
587 child->destroy(child);
588 }
589 peer->destroy(peer);
590 }
591
592 /**
593 * Complete the acquire, if successful or failed
594 */
595 static void complete(private_trap_manager_t *this, ike_sa_t *ike_sa,
596 child_sa_t *child_sa)
597 {
598 enumerator_t *enumerator;
599 acquire_t *acquire;
600
601 this->mutex->lock(this->mutex);
602 enumerator = this->acquires->create_enumerator(this->acquires);
603 while (enumerator->enumerate(enumerator, &acquire))
604 {
605 if (!acquire->ike_sa || acquire->ike_sa != ike_sa)
606 {
607 continue;
608 }
609 if (child_sa)
610 {
611 if (acquire->dst)
612 {
613 /* since every wildcard acquire results in a separate IKE_SA
614 * there is no need to compare the destination address */
615 }
616 else if (child_sa->get_reqid(child_sa) != acquire->reqid)
617 {
618 continue;
619 }
620 }
621 this->acquires->remove_at(this->acquires, enumerator);
622 destroy_acquire(acquire);
623 }
624 enumerator->destroy(enumerator);
625 this->mutex->unlock(this->mutex);
626 }
627
628 METHOD(listener_t, ike_state_change, bool,
629 trap_listener_t *listener, ike_sa_t *ike_sa, ike_sa_state_t state)
630 {
631 switch (state)
632 {
633 case IKE_DESTROYING:
634 complete(listener->traps, ike_sa, NULL);
635 return TRUE;
636 default:
637 return TRUE;
638 }
639 }
640
641 METHOD(listener_t, child_state_change, bool,
642 trap_listener_t *listener, ike_sa_t *ike_sa, child_sa_t *child_sa,
643 child_sa_state_t state)
644 {
645 switch (state)
646 {
647 case CHILD_INSTALLED:
648 case CHILD_DESTROYING:
649 complete(listener->traps, ike_sa, child_sa);
650 return TRUE;
651 default:
652 return TRUE;
653 }
654 }
655
656 METHOD(trap_manager_t, flush, void,
657 private_trap_manager_t *this)
658 {
659 this->lock->write_lock(this->lock);
660 while (this->installing)
661 {
662 this->condvar->wait(this->condvar, this->lock);
663 }
664 this->traps->destroy_function(this->traps, (void*)destroy_entry);
665 this->traps = linked_list_create();
666 this->installing = INSTALL_DISABLED;
667 this->lock->unlock(this->lock);
668 }
669
670 METHOD(trap_manager_t, destroy, void,
671 private_trap_manager_t *this)
672 {
673 charon->bus->remove_listener(charon->bus, &this->listener.listener);
674 this->traps->destroy_function(this->traps, (void*)destroy_entry);
675 this->acquires->destroy_function(this->acquires, (void*)destroy_acquire);
676 this->condvar->destroy(this->condvar);
677 this->mutex->destroy(this->mutex);
678 this->lock->destroy(this->lock);
679 free(this);
680 }
681
682 /**
683 * See header
684 */
685 trap_manager_t *trap_manager_create(void)
686 {
687 private_trap_manager_t *this;
688
689 INIT(this,
690 .public = {
691 .install = _install,
692 .uninstall = _uninstall,
693 .create_enumerator = _create_enumerator,
694 .find_reqid = _find_reqid,
695 .acquire = _acquire,
696 .flush = _flush,
697 .destroy = _destroy,
698 },
699 .listener = {
700 .traps = this,
701 .listener = {
702 .ike_state_change = _ike_state_change,
703 .child_state_change = _child_state_change,
704 },
705 },
706 .traps = linked_list_create(),
707 .acquires = linked_list_create(),
708 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
709 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
710 .condvar = rwlock_condvar_create(),
711 .ignore_acquire_ts = lib->settings->get_bool(lib->settings,
712 "%s.ignore_acquire_ts", FALSE, lib->ns),
713 );
714 charon->bus->add_listener(charon->bus, &this->listener.listener);
715
716 return &this->public;
717 }