kernel-interface: Always set `vip` if get_address_by_ts() returns successfully
[strongswan.git] / src / libcharon / kernel / kernel_interface.c
1 /*
2 * Copyright (C) 2008-2016 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /*
20 * Copyright (c) 2012 Nanoteq Pty Ltd
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #include "kernel_interface.h"
42
43 #include <utils/debug.h>
44 #include <threading/mutex.h>
45 #include <collections/linked_list.h>
46 #include <collections/hashtable.h>
47 #include <collections/array.h>
48
49 typedef struct private_kernel_interface_t private_kernel_interface_t;
50
51 typedef struct kernel_algorithm_t kernel_algorithm_t;
52
53 /**
54 * Mapping of IKE algorithms to kernel-specific algorithm identifiers
55 */
56 struct kernel_algorithm_t {
57
58 /**
59 * Transform type of the algorithm
60 */
61 transform_type_t type;
62
63 /**
64 * Identifier specified in IKE
65 */
66 uint16_t ike;
67
68 /**
69 * Identifier as defined in pfkeyv2.h
70 */
71 uint16_t kernel;
72
73 /**
74 * Name of the algorithm in linux crypto API
75 */
76 char *name;
77 };
78
79 /**
80 * Private data of a kernel_interface_t object.
81 */
82 struct private_kernel_interface_t {
83
84 /**
85 * Public part of kernel_interface_t object.
86 */
87 kernel_interface_t public;
88
89 /**
90 * Registered IPsec constructor
91 */
92 kernel_ipsec_constructor_t ipsec_constructor;
93
94 /**
95 * Registered net constructor
96 */
97 kernel_net_constructor_t net_constructor;
98
99 /**
100 * ipsec interface
101 */
102 kernel_ipsec_t *ipsec;
103
104 /**
105 * network interface
106 */
107 kernel_net_t *net;
108
109 /**
110 * mutex for listeners
111 */
112 mutex_t *mutex;
113
114 /**
115 * list of registered listeners
116 */
117 linked_list_t *listeners;
118
119 /**
120 * Reqid entries indexed by reqids
121 */
122 hashtable_t *reqids;
123
124 /**
125 * Reqid entries indexed by traffic selectors
126 */
127 hashtable_t *reqids_by_ts;
128
129 /**
130 * mutex for algorithm mappings
131 */
132 mutex_t *mutex_algs;
133
134 /**
135 * List of algorithm mappings (kernel_algorithm_t*)
136 */
137 linked_list_t *algorithms;
138
139 /**
140 * List of interface names to include or exclude (char*), NULL if interfaces
141 * are not filtered
142 */
143 linked_list_t *ifaces_filter;
144
145 /**
146 * TRUE to exclude interfaces listed in ifaces_filter, FALSE to consider
147 * only those listed there
148 */
149 bool ifaces_exclude;
150 };
151
152 METHOD(kernel_interface_t, get_features, kernel_feature_t,
153 private_kernel_interface_t *this)
154 {
155 kernel_feature_t features = 0;
156
157 if (this->ipsec && this->ipsec->get_features)
158 {
159 features |= this->ipsec->get_features(this->ipsec);
160 }
161 if (this->net && this->net->get_features)
162 {
163 features |= this->net->get_features(this->net);
164 }
165 return features;
166 }
167
168 METHOD(kernel_interface_t, get_spi, status_t,
169 private_kernel_interface_t *this, host_t *src, host_t *dst,
170 uint8_t protocol, uint32_t *spi)
171 {
172 if (!this->ipsec)
173 {
174 return NOT_SUPPORTED;
175 }
176 return this->ipsec->get_spi(this->ipsec, src, dst, protocol, spi);
177 }
178
179 METHOD(kernel_interface_t, get_cpi, status_t,
180 private_kernel_interface_t *this, host_t *src, host_t *dst,
181 uint16_t *cpi)
182 {
183 if (!this->ipsec)
184 {
185 return NOT_SUPPORTED;
186 }
187 return this->ipsec->get_cpi(this->ipsec, src, dst, cpi);
188 }
189
190 /**
191 * Reqid mapping entry
192 */
193 typedef struct {
194 /** allocated reqid */
195 uint32_t reqid;
196 /** references to this entry */
197 u_int refs;
198 /** inbound mark used for SA */
199 mark_t mark_in;
200 /** outbound mark used for SA */
201 mark_t mark_out;
202 /** local traffic selectors */
203 array_t *local;
204 /** remote traffic selectors */
205 array_t *remote;
206 } reqid_entry_t;
207
208 /**
209 * Destroy a reqid mapping entry
210 */
211 static void reqid_entry_destroy(reqid_entry_t *entry)
212 {
213 array_destroy_offset(entry->local, offsetof(traffic_selector_t, destroy));
214 array_destroy_offset(entry->remote, offsetof(traffic_selector_t, destroy));
215 free(entry);
216 }
217
218 /**
219 * Hashtable hash function for reqid entries using reqid as key
220 */
221 static u_int hash_reqid(reqid_entry_t *entry)
222 {
223 return chunk_hash_inc(chunk_from_thing(entry->reqid),
224 chunk_hash_inc(chunk_from_thing(entry->mark_in),
225 chunk_hash(chunk_from_thing(entry->mark_out))));
226 }
227
228 /**
229 * Hashtable equals function for reqid entries using reqid as key
230 */
231 static bool equals_reqid(reqid_entry_t *a, reqid_entry_t *b)
232 {
233 return a->reqid == b->reqid &&
234 a->mark_in.value == b->mark_in.value &&
235 a->mark_in.mask == b->mark_in.mask &&
236 a->mark_out.value == b->mark_out.value &&
237 a->mark_out.mask == b->mark_out.mask;
238 }
239
240 /**
241 * Hash an array of traffic selectors
242 */
243 static u_int hash_ts_array(array_t *array, u_int hash)
244 {
245 enumerator_t *enumerator;
246 traffic_selector_t *ts;
247
248 enumerator = array_create_enumerator(array);
249 while (enumerator->enumerate(enumerator, &ts))
250 {
251 hash = ts->hash(ts, hash);
252 }
253 enumerator->destroy(enumerator);
254
255 return hash;
256 }
257
258 /**
259 * Hashtable hash function for reqid entries using traffic selectors as key
260 */
261 static u_int hash_reqid_by_ts(reqid_entry_t *entry)
262 {
263 return hash_ts_array(entry->local, hash_ts_array(entry->remote,
264 chunk_hash_inc(chunk_from_thing(entry->mark_in),
265 chunk_hash(chunk_from_thing(entry->mark_out)))));
266 }
267
268 /**
269 * Compare two array with traffic selectors for equality
270 */
271 static bool ts_array_equals(array_t *a, array_t *b)
272 {
273 traffic_selector_t *tsa, *tsb;
274 enumerator_t *ae, *be;
275 bool equal = TRUE;
276
277 if (array_count(a) != array_count(b))
278 {
279 return FALSE;
280 }
281
282 ae = array_create_enumerator(a);
283 be = array_create_enumerator(b);
284 while (equal && ae->enumerate(ae, &tsa) && be->enumerate(be, &tsb))
285 {
286 equal = tsa->equals(tsa, tsb);
287 }
288 ae->destroy(ae);
289 be->destroy(be);
290
291 return equal;
292 }
293
294 /**
295 * Hashtable equals function for reqid entries using traffic selectors as key
296 */
297 static bool equals_reqid_by_ts(reqid_entry_t *a, reqid_entry_t *b)
298 {
299 return ts_array_equals(a->local, b->local) &&
300 ts_array_equals(a->remote, b->remote) &&
301 a->mark_in.value == b->mark_in.value &&
302 a->mark_in.mask == b->mark_in.mask &&
303 a->mark_out.value == b->mark_out.value &&
304 a->mark_out.mask == b->mark_out.mask;
305 }
306
307 /**
308 * Create an array from copied traffic selector list items
309 */
310 static array_t *array_from_ts_list(linked_list_t *list)
311 {
312 enumerator_t *enumerator;
313 traffic_selector_t *ts;
314 array_t *array;
315
316 array = array_create(0, 0);
317
318 enumerator = list->create_enumerator(list);
319 while (enumerator->enumerate(enumerator, &ts))
320 {
321 array_insert(array, ARRAY_TAIL, ts->clone(ts));
322 }
323 enumerator->destroy(enumerator);
324
325 return array;
326 }
327
328 METHOD(kernel_interface_t, alloc_reqid, status_t,
329 private_kernel_interface_t *this,
330 linked_list_t *local_ts, linked_list_t *remote_ts,
331 mark_t mark_in, mark_t mark_out, uint32_t *reqid)
332 {
333 static uint32_t counter = 0;
334 reqid_entry_t *entry = NULL, *tmpl;
335 status_t status = SUCCESS;
336
337 INIT(tmpl,
338 .local = array_from_ts_list(local_ts),
339 .remote = array_from_ts_list(remote_ts),
340 .mark_in = mark_in,
341 .mark_out = mark_out,
342 .reqid = *reqid,
343 );
344
345 this->mutex->lock(this->mutex);
346 if (tmpl->reqid)
347 {
348 /* search by reqid if given */
349 entry = this->reqids->get(this->reqids, tmpl);
350 }
351 if (entry)
352 {
353 /* we don't require a traffic selector match for explicit reqids,
354 * as we wan't to reuse a reqid for trap-triggered policies that
355 * got narrowed during negotiation. */
356 reqid_entry_destroy(tmpl);
357 }
358 else
359 {
360 /* search by traffic selectors */
361 entry = this->reqids_by_ts->get(this->reqids_by_ts, tmpl);
362 if (entry)
363 {
364 reqid_entry_destroy(tmpl);
365 }
366 else
367 {
368 /* none found, create a new entry, allocating a reqid */
369 entry = tmpl;
370 entry->reqid = ++counter;
371 this->reqids_by_ts->put(this->reqids_by_ts, entry, entry);
372 this->reqids->put(this->reqids, entry, entry);
373 }
374 *reqid = entry->reqid;
375 }
376 entry->refs++;
377 this->mutex->unlock(this->mutex);
378
379 return status;
380 }
381
382 METHOD(kernel_interface_t, release_reqid, status_t,
383 private_kernel_interface_t *this, uint32_t reqid,
384 mark_t mark_in, mark_t mark_out)
385 {
386 reqid_entry_t *entry, tmpl = {
387 .reqid = reqid,
388 .mark_in = mark_in,
389 .mark_out = mark_out,
390 };
391
392 this->mutex->lock(this->mutex);
393 entry = this->reqids->remove(this->reqids, &tmpl);
394 if (entry)
395 {
396 if (--entry->refs == 0)
397 {
398 entry = this->reqids_by_ts->remove(this->reqids_by_ts, entry);
399 if (entry)
400 {
401 reqid_entry_destroy(entry);
402 }
403 }
404 else
405 {
406 this->reqids->put(this->reqids, entry, entry);
407 }
408 }
409 this->mutex->unlock(this->mutex);
410
411 if (entry)
412 {
413 return SUCCESS;
414 }
415 return NOT_FOUND;
416 }
417
418 METHOD(kernel_interface_t, add_sa, status_t,
419 private_kernel_interface_t *this, kernel_ipsec_sa_id_t *id,
420 kernel_ipsec_add_sa_t *data)
421 {
422 if (!this->ipsec)
423 {
424 return NOT_SUPPORTED;
425 }
426 return this->ipsec->add_sa(this->ipsec, id, data);
427 }
428
429 METHOD(kernel_interface_t, update_sa, status_t,
430 private_kernel_interface_t *this, kernel_ipsec_sa_id_t *id,
431 kernel_ipsec_update_sa_t *data)
432 {
433 if (!this->ipsec)
434 {
435 return NOT_SUPPORTED;
436 }
437 return this->ipsec->update_sa(this->ipsec, id, data);
438 }
439
440 METHOD(kernel_interface_t, query_sa, status_t,
441 private_kernel_interface_t *this, kernel_ipsec_sa_id_t *id,
442 kernel_ipsec_query_sa_t *data, uint64_t *bytes, uint64_t *packets,
443 time_t *time)
444 {
445 if (!this->ipsec)
446 {
447 return NOT_SUPPORTED;
448 }
449 return this->ipsec->query_sa(this->ipsec, id, data, bytes, packets, time);
450 }
451
452 METHOD(kernel_interface_t, del_sa, status_t,
453 private_kernel_interface_t *this, kernel_ipsec_sa_id_t *id,
454 kernel_ipsec_del_sa_t *data)
455 {
456 if (!this->ipsec)
457 {
458 return NOT_SUPPORTED;
459 }
460 return this->ipsec->del_sa(this->ipsec, id, data);
461 }
462
463 METHOD(kernel_interface_t, flush_sas, status_t,
464 private_kernel_interface_t *this)
465 {
466 if (!this->ipsec)
467 {
468 return NOT_SUPPORTED;
469 }
470 return this->ipsec->flush_sas(this->ipsec);
471 }
472
473 METHOD(kernel_interface_t, add_policy, status_t,
474 private_kernel_interface_t *this, kernel_ipsec_policy_id_t *id,
475 kernel_ipsec_manage_policy_t *data)
476 {
477 if (!this->ipsec)
478 {
479 return NOT_SUPPORTED;
480 }
481 return this->ipsec->add_policy(this->ipsec, id, data);
482 }
483
484 METHOD(kernel_interface_t, query_policy, status_t,
485 private_kernel_interface_t *this, kernel_ipsec_policy_id_t *id,
486 kernel_ipsec_query_policy_t *data, time_t *use_time)
487 {
488 if (!this->ipsec)
489 {
490 return NOT_SUPPORTED;
491 }
492 return this->ipsec->query_policy(this->ipsec, id, data, use_time);
493 }
494
495 METHOD(kernel_interface_t, del_policy, status_t,
496 private_kernel_interface_t *this, kernel_ipsec_policy_id_t *id,
497 kernel_ipsec_manage_policy_t *data)
498 {
499 if (!this->ipsec)
500 {
501 return NOT_SUPPORTED;
502 }
503 return this->ipsec->del_policy(this->ipsec, id, data);
504 }
505
506 METHOD(kernel_interface_t, flush_policies, status_t,
507 private_kernel_interface_t *this)
508 {
509 if (!this->ipsec)
510 {
511 return NOT_SUPPORTED;
512 }
513 return this->ipsec->flush_policies(this->ipsec);
514 }
515
516 METHOD(kernel_interface_t, get_source_addr, host_t*,
517 private_kernel_interface_t *this, host_t *dest, host_t *src)
518 {
519 if (!this->net)
520 {
521 return NULL;
522 }
523 return this->net->get_source_addr(this->net, dest, src);
524 }
525
526 METHOD(kernel_interface_t, get_nexthop, host_t*,
527 private_kernel_interface_t *this, host_t *dest, int prefix, host_t *src)
528 {
529 if (!this->net)
530 {
531 return NULL;
532 }
533 return this->net->get_nexthop(this->net, dest, prefix, src);
534 }
535
536 METHOD(kernel_interface_t, get_interface, bool,
537 private_kernel_interface_t *this, host_t *host, char **name)
538 {
539 if (!this->net)
540 {
541 return NULL;
542 }
543 return this->net->get_interface(this->net, host, name);
544 }
545
546 METHOD(kernel_interface_t, create_address_enumerator, enumerator_t*,
547 private_kernel_interface_t *this, kernel_address_type_t which)
548 {
549 if (!this->net)
550 {
551 return enumerator_create_empty();
552 }
553 return this->net->create_address_enumerator(this->net, which);
554 }
555
556 METHOD(kernel_interface_t, add_ip, status_t,
557 private_kernel_interface_t *this, host_t *virtual_ip, int prefix,
558 char *iface)
559 {
560 if (!this->net)
561 {
562 return NOT_SUPPORTED;
563 }
564 return this->net->add_ip(this->net, virtual_ip, prefix, iface);
565 }
566
567 METHOD(kernel_interface_t, del_ip, status_t,
568 private_kernel_interface_t *this, host_t *virtual_ip, int prefix, bool wait)
569 {
570 if (!this->net)
571 {
572 return NOT_SUPPORTED;
573 }
574 return this->net->del_ip(this->net, virtual_ip, prefix, wait);
575 }
576
577 METHOD(kernel_interface_t, add_route, status_t,
578 private_kernel_interface_t *this, chunk_t dst_net,
579 uint8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
580 {
581 if (!this->net)
582 {
583 return NOT_SUPPORTED;
584 }
585 return this->net->add_route(this->net, dst_net, prefixlen, gateway,
586 src_ip, if_name);
587 }
588
589 METHOD(kernel_interface_t, del_route, status_t,
590 private_kernel_interface_t *this, chunk_t dst_net,
591 uint8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
592 {
593 if (!this->net)
594 {
595 return NOT_SUPPORTED;
596 }
597 return this->net->del_route(this->net, dst_net, prefixlen, gateway,
598 src_ip, if_name);
599 }
600
601 METHOD(kernel_interface_t, bypass_socket, bool,
602 private_kernel_interface_t *this, int fd, int family)
603 {
604 if (!this->ipsec)
605 {
606 return FALSE;
607 }
608 return this->ipsec->bypass_socket(this->ipsec, fd, family);
609 }
610
611 METHOD(kernel_interface_t, enable_udp_decap, bool,
612 private_kernel_interface_t *this, int fd, int family, uint16_t port)
613 {
614 if (!this->ipsec)
615 {
616 return FALSE;
617 }
618 return this->ipsec->enable_udp_decap(this->ipsec, fd, family, port);
619 }
620
621 METHOD(kernel_interface_t, is_interface_usable, bool,
622 private_kernel_interface_t *this, const char *iface)
623 {
624 status_t expected;
625
626 if (!this->ifaces_filter)
627 {
628 return TRUE;
629 }
630 expected = this->ifaces_exclude ? NOT_FOUND : SUCCESS;
631 return this->ifaces_filter->find_first(this->ifaces_filter, (void*)streq,
632 NULL, iface) == expected;
633 }
634
635 METHOD(kernel_interface_t, all_interfaces_usable, bool,
636 private_kernel_interface_t *this)
637 {
638 return this->ifaces_filter == NULL;
639 }
640
641 METHOD(kernel_interface_t, get_address_by_ts, status_t,
642 private_kernel_interface_t *this, traffic_selector_t *ts,
643 host_t **ip, bool *vip)
644 {
645 enumerator_t *addrs;
646 host_t *host;
647 int family;
648 bool found = FALSE;
649
650 DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
651
652 /* if we have a family which includes localhost, we do not
653 * search for an IP, we use the default */
654 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
655
656 if (family == AF_INET)
657 {
658 host = host_create_from_string("127.0.0.1", 0);
659 }
660 else
661 {
662 host = host_create_from_string("::1", 0);
663 }
664
665 if (ts->includes(ts, host))
666 {
667 *ip = host_create_any(family);
668 if (vip)
669 {
670 *vip = FALSE;
671 }
672 host->destroy(host);
673 DBG2(DBG_KNL, "using host %H", *ip);
674 return SUCCESS;
675 }
676 host->destroy(host);
677
678 /* try virtual IPs only first (on all interfaces) */
679 addrs = create_address_enumerator(this,
680 ADDR_TYPE_ALL ^ ADDR_TYPE_REGULAR);
681 while (addrs->enumerate(addrs, (void**)&host))
682 {
683 if (ts->includes(ts, host))
684 {
685 found = TRUE;
686 *ip = host->clone(host);
687 if (vip)
688 {
689 *vip = TRUE;
690 }
691 break;
692 }
693 }
694 addrs->destroy(addrs);
695
696 if (!found)
697 { /* then try the regular addresses (on all interfaces) */
698 addrs = create_address_enumerator(this,
699 ADDR_TYPE_ALL ^ ADDR_TYPE_VIRTUAL);
700 while (addrs->enumerate(addrs, (void**)&host))
701 {
702 if (ts->includes(ts, host))
703 {
704 found = TRUE;
705 *ip = host->clone(host);
706 if (vip)
707 {
708 *vip = FALSE;
709 }
710 break;
711 }
712 }
713 addrs->destroy(addrs);
714 }
715
716 if (!found)
717 {
718 DBG2(DBG_KNL, "no local address found in traffic selector %R", ts);
719 return FAILED;
720 }
721
722 DBG2(DBG_KNL, "using host %H", *ip);
723 return SUCCESS;
724 }
725
726
727 METHOD(kernel_interface_t, add_ipsec_interface, bool,
728 private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
729 {
730 if (!this->ipsec)
731 {
732 this->ipsec_constructor = constructor;
733 this->ipsec = constructor();
734 return this->ipsec != NULL;
735 }
736 return FALSE;
737 }
738
739 METHOD(kernel_interface_t, remove_ipsec_interface, bool,
740 private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
741 {
742 if (constructor == this->ipsec_constructor && this->ipsec)
743 {
744 this->ipsec->destroy(this->ipsec);
745 this->ipsec = NULL;
746 return TRUE;
747 }
748 return FALSE;
749 }
750
751 METHOD(kernel_interface_t, add_net_interface, bool,
752 private_kernel_interface_t *this, kernel_net_constructor_t constructor)
753 {
754 if (!this->net)
755 {
756 this->net_constructor = constructor;
757 this->net = constructor();
758 return this->net != NULL;
759 }
760 return FALSE;
761 }
762
763 METHOD(kernel_interface_t, remove_net_interface, bool,
764 private_kernel_interface_t *this, kernel_net_constructor_t constructor)
765 {
766 if (constructor == this->net_constructor && this->net)
767 {
768 this->net->destroy(this->net);
769 this->net = NULL;
770 return TRUE;
771 }
772 return FALSE;
773 }
774
775 METHOD(kernel_interface_t, add_listener, void,
776 private_kernel_interface_t *this, kernel_listener_t *listener)
777 {
778 this->mutex->lock(this->mutex);
779 this->listeners->insert_last(this->listeners, listener);
780 this->mutex->unlock(this->mutex);
781 }
782
783 METHOD(kernel_interface_t, remove_listener, void,
784 private_kernel_interface_t *this, kernel_listener_t *listener)
785 {
786 this->mutex->lock(this->mutex);
787 this->listeners->remove(this->listeners, listener, NULL);
788 this->mutex->unlock(this->mutex);
789 }
790
791 METHOD(kernel_interface_t, acquire, void,
792 private_kernel_interface_t *this, uint32_t reqid,
793 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
794 {
795 kernel_listener_t *listener;
796 enumerator_t *enumerator;
797 this->mutex->lock(this->mutex);
798 enumerator = this->listeners->create_enumerator(this->listeners);
799 while (enumerator->enumerate(enumerator, &listener))
800 {
801 if (listener->acquire &&
802 !listener->acquire(listener, reqid, src_ts, dst_ts))
803 {
804 this->listeners->remove_at(this->listeners, enumerator);
805 }
806 }
807 enumerator->destroy(enumerator);
808 this->mutex->unlock(this->mutex);
809 }
810
811 METHOD(kernel_interface_t, expire, void,
812 private_kernel_interface_t *this, uint8_t protocol, uint32_t spi,
813 host_t *dst, bool hard)
814 {
815 kernel_listener_t *listener;
816 enumerator_t *enumerator;
817
818 this->mutex->lock(this->mutex);
819 enumerator = this->listeners->create_enumerator(this->listeners);
820 while (enumerator->enumerate(enumerator, &listener))
821 {
822 if (listener->expire &&
823 !listener->expire(listener, protocol, spi, dst, hard))
824 {
825 this->listeners->remove_at(this->listeners, enumerator);
826 }
827 }
828 enumerator->destroy(enumerator);
829 this->mutex->unlock(this->mutex);
830 }
831
832 METHOD(kernel_interface_t, mapping, void,
833 private_kernel_interface_t *this, uint8_t protocol, uint32_t spi,
834 host_t *dst, host_t *remote)
835 {
836 kernel_listener_t *listener;
837 enumerator_t *enumerator;
838
839 this->mutex->lock(this->mutex);
840 enumerator = this->listeners->create_enumerator(this->listeners);
841 while (enumerator->enumerate(enumerator, &listener))
842 {
843 if (listener->mapping &&
844 !listener->mapping(listener, protocol, spi, dst, remote))
845 {
846 this->listeners->remove_at(this->listeners, enumerator);
847 }
848 }
849 enumerator->destroy(enumerator);
850 this->mutex->unlock(this->mutex);
851 }
852
853 METHOD(kernel_interface_t, migrate, void,
854 private_kernel_interface_t *this, uint32_t reqid,
855 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
856 policy_dir_t direction, host_t *local, host_t *remote)
857 {
858 kernel_listener_t *listener;
859 enumerator_t *enumerator;
860 this->mutex->lock(this->mutex);
861 enumerator = this->listeners->create_enumerator(this->listeners);
862 while (enumerator->enumerate(enumerator, &listener))
863 {
864 if (listener->migrate &&
865 !listener->migrate(listener, reqid, src_ts, dst_ts, direction,
866 local, remote))
867 {
868 this->listeners->remove_at(this->listeners, enumerator);
869 }
870 }
871 enumerator->destroy(enumerator);
872 this->mutex->unlock(this->mutex);
873 }
874
875 static bool call_roam(kernel_listener_t *listener, bool *roam)
876 {
877 return listener->roam && !listener->roam(listener, *roam);
878 }
879
880 METHOD(kernel_interface_t, roam, void,
881 private_kernel_interface_t *this, bool address)
882 {
883 this->mutex->lock(this->mutex);
884 this->listeners->remove(this->listeners, &address, (void*)call_roam);
885 this->mutex->unlock(this->mutex);
886 }
887
888 METHOD(kernel_interface_t, tun, void,
889 private_kernel_interface_t *this, tun_device_t *tun, bool created)
890 {
891 kernel_listener_t *listener;
892 enumerator_t *enumerator;
893 this->mutex->lock(this->mutex);
894 enumerator = this->listeners->create_enumerator(this->listeners);
895 while (enumerator->enumerate(enumerator, &listener))
896 {
897 if (listener->tun &&
898 !listener->tun(listener, tun, created))
899 {
900 this->listeners->remove_at(this->listeners, enumerator);
901 }
902 }
903 enumerator->destroy(enumerator);
904 this->mutex->unlock(this->mutex);
905 }
906
907 METHOD(kernel_interface_t, register_algorithm, void,
908 private_kernel_interface_t *this, uint16_t alg_id, transform_type_t type,
909 uint16_t kernel_id, char *kernel_name)
910 {
911 kernel_algorithm_t *algorithm;
912
913 INIT(algorithm,
914 .type = type,
915 .ike = alg_id,
916 .kernel = kernel_id,
917 .name = strdup(kernel_name),
918 );
919
920 this->mutex_algs->lock(this->mutex_algs);
921 this->algorithms->insert_first(this->algorithms, algorithm);
922 this->mutex_algs->unlock(this->mutex_algs);
923 }
924
925 METHOD(kernel_interface_t, lookup_algorithm, bool,
926 private_kernel_interface_t *this, uint16_t alg_id, transform_type_t type,
927 uint16_t *kernel_id, char **kernel_name)
928 {
929 kernel_algorithm_t *algorithm;
930 enumerator_t *enumerator;
931 bool found = FALSE;
932
933 this->mutex_algs->lock(this->mutex_algs);
934 enumerator = this->algorithms->create_enumerator(this->algorithms);
935 while (enumerator->enumerate(enumerator, &algorithm))
936 {
937 if (algorithm->type == type && algorithm->ike == alg_id)
938 {
939 if (kernel_id)
940 {
941 *kernel_id = algorithm->kernel;
942 }
943 if (kernel_name)
944 {
945 *kernel_name = algorithm->name;
946 }
947 found = TRUE;
948 break;
949 }
950 }
951 enumerator->destroy(enumerator);
952 this->mutex_algs->unlock(this->mutex_algs);
953 return found;
954 }
955
956 METHOD(kernel_interface_t, destroy, void,
957 private_kernel_interface_t *this)
958 {
959 kernel_algorithm_t *algorithm;
960
961 while (this->algorithms->remove_first(this->algorithms,
962 (void**)&algorithm) == SUCCESS)
963 {
964 free(algorithm->name);
965 free(algorithm);
966 }
967 this->algorithms->destroy(this->algorithms);
968 this->mutex_algs->destroy(this->mutex_algs);
969 DESTROY_IF(this->ipsec);
970 DESTROY_IF(this->net);
971 DESTROY_FUNCTION_IF(this->ifaces_filter, (void*)free);
972 this->reqids->destroy(this->reqids);
973 this->reqids_by_ts->destroy(this->reqids_by_ts);
974 this->listeners->destroy(this->listeners);
975 this->mutex->destroy(this->mutex);
976 free(this);
977 }
978
979 /*
980 * Described in header-file
981 */
982 kernel_interface_t *kernel_interface_create()
983 {
984 private_kernel_interface_t *this;
985 char *ifaces;
986
987 INIT(this,
988 .public = {
989 .get_features = _get_features,
990 .get_spi = _get_spi,
991 .get_cpi = _get_cpi,
992 .alloc_reqid = _alloc_reqid,
993 .release_reqid = _release_reqid,
994 .add_sa = _add_sa,
995 .update_sa = _update_sa,
996 .query_sa = _query_sa,
997 .del_sa = _del_sa,
998 .flush_sas = _flush_sas,
999 .add_policy = _add_policy,
1000 .query_policy = _query_policy,
1001 .del_policy = _del_policy,
1002 .flush_policies = _flush_policies,
1003 .get_source_addr = _get_source_addr,
1004 .get_nexthop = _get_nexthop,
1005 .get_interface = _get_interface,
1006 .create_address_enumerator = _create_address_enumerator,
1007 .add_ip = _add_ip,
1008 .del_ip = _del_ip,
1009 .add_route = _add_route,
1010 .del_route = _del_route,
1011 .bypass_socket = _bypass_socket,
1012 .enable_udp_decap = _enable_udp_decap,
1013
1014 .is_interface_usable = _is_interface_usable,
1015 .all_interfaces_usable = _all_interfaces_usable,
1016 .get_address_by_ts = _get_address_by_ts,
1017 .add_ipsec_interface = _add_ipsec_interface,
1018 .remove_ipsec_interface = _remove_ipsec_interface,
1019 .add_net_interface = _add_net_interface,
1020 .remove_net_interface = _remove_net_interface,
1021
1022 .add_listener = _add_listener,
1023 .remove_listener = _remove_listener,
1024 .register_algorithm = _register_algorithm,
1025 .lookup_algorithm = _lookup_algorithm,
1026 .acquire = _acquire,
1027 .expire = _expire,
1028 .mapping = _mapping,
1029 .migrate = _migrate,
1030 .roam = _roam,
1031 .tun = _tun,
1032 .destroy = _destroy,
1033 },
1034 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1035 .listeners = linked_list_create(),
1036 .mutex_algs = mutex_create(MUTEX_TYPE_DEFAULT),
1037 .algorithms = linked_list_create(),
1038 .reqids = hashtable_create((hashtable_hash_t)hash_reqid,
1039 (hashtable_equals_t)equals_reqid, 8),
1040 .reqids_by_ts = hashtable_create((hashtable_hash_t)hash_reqid_by_ts,
1041 (hashtable_equals_t)equals_reqid_by_ts, 8),
1042 );
1043
1044 ifaces = lib->settings->get_str(lib->settings,
1045 "%s.interfaces_use", NULL, lib->ns);
1046 if (!ifaces)
1047 {
1048 this->ifaces_exclude = TRUE;
1049 ifaces = lib->settings->get_str(lib->settings,
1050 "%s.interfaces_ignore", NULL, lib->ns);
1051 }
1052 if (ifaces)
1053 {
1054 enumerator_t *enumerator;
1055 char *iface;
1056
1057 enumerator = enumerator_create_token(ifaces, ",", " ");
1058 while (enumerator->enumerate(enumerator, &iface))
1059 {
1060 if (!this->ifaces_filter)
1061 {
1062 this->ifaces_filter = linked_list_create();
1063 }
1064 this->ifaces_filter->insert_last(this->ifaces_filter,
1065 strdup(iface));
1066 }
1067 enumerator->destroy(enumerator);
1068 }
1069
1070 return &this->public;
1071 }