97d5ef1f71751c080487b271a436534f6c0ca203
[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 host->destroy(host);
669 DBG2(DBG_KNL, "using host %H", *ip);
670 return SUCCESS;
671 }
672 host->destroy(host);
673
674 /* try virtual IPs only first (on all interfaces) */
675 addrs = create_address_enumerator(this,
676 ADDR_TYPE_ALL ^ ADDR_TYPE_REGULAR);
677 while (addrs->enumerate(addrs, (void**)&host))
678 {
679 if (ts->includes(ts, host))
680 {
681 found = TRUE;
682 *ip = host->clone(host);
683 if (vip)
684 {
685 *vip = TRUE;
686 }
687 break;
688 }
689 }
690 addrs->destroy(addrs);
691
692 if (!found)
693 { /* then try the regular addresses (on all interfaces) */
694 addrs = create_address_enumerator(this,
695 ADDR_TYPE_ALL ^ ADDR_TYPE_VIRTUAL);
696 while (addrs->enumerate(addrs, (void**)&host))
697 {
698 if (ts->includes(ts, host))
699 {
700 found = TRUE;
701 *ip = host->clone(host);
702 if (vip)
703 {
704 *vip = FALSE;
705 }
706 break;
707 }
708 }
709 addrs->destroy(addrs);
710 }
711
712 if (!found)
713 {
714 DBG2(DBG_KNL, "no local address found in traffic selector %R", ts);
715 return FAILED;
716 }
717
718 DBG2(DBG_KNL, "using host %H", *ip);
719 return SUCCESS;
720 }
721
722
723 METHOD(kernel_interface_t, add_ipsec_interface, bool,
724 private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
725 {
726 if (!this->ipsec)
727 {
728 this->ipsec_constructor = constructor;
729 this->ipsec = constructor();
730 return this->ipsec != NULL;
731 }
732 return FALSE;
733 }
734
735 METHOD(kernel_interface_t, remove_ipsec_interface, bool,
736 private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
737 {
738 if (constructor == this->ipsec_constructor && this->ipsec)
739 {
740 this->ipsec->destroy(this->ipsec);
741 this->ipsec = NULL;
742 return TRUE;
743 }
744 return FALSE;
745 }
746
747 METHOD(kernel_interface_t, add_net_interface, bool,
748 private_kernel_interface_t *this, kernel_net_constructor_t constructor)
749 {
750 if (!this->net)
751 {
752 this->net_constructor = constructor;
753 this->net = constructor();
754 return this->net != NULL;
755 }
756 return FALSE;
757 }
758
759 METHOD(kernel_interface_t, remove_net_interface, bool,
760 private_kernel_interface_t *this, kernel_net_constructor_t constructor)
761 {
762 if (constructor == this->net_constructor && this->net)
763 {
764 this->net->destroy(this->net);
765 this->net = NULL;
766 return TRUE;
767 }
768 return FALSE;
769 }
770
771 METHOD(kernel_interface_t, add_listener, void,
772 private_kernel_interface_t *this, kernel_listener_t *listener)
773 {
774 this->mutex->lock(this->mutex);
775 this->listeners->insert_last(this->listeners, listener);
776 this->mutex->unlock(this->mutex);
777 }
778
779 METHOD(kernel_interface_t, remove_listener, void,
780 private_kernel_interface_t *this, kernel_listener_t *listener)
781 {
782 this->mutex->lock(this->mutex);
783 this->listeners->remove(this->listeners, listener, NULL);
784 this->mutex->unlock(this->mutex);
785 }
786
787 METHOD(kernel_interface_t, acquire, void,
788 private_kernel_interface_t *this, uint32_t reqid,
789 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
790 {
791 kernel_listener_t *listener;
792 enumerator_t *enumerator;
793 this->mutex->lock(this->mutex);
794 enumerator = this->listeners->create_enumerator(this->listeners);
795 while (enumerator->enumerate(enumerator, &listener))
796 {
797 if (listener->acquire &&
798 !listener->acquire(listener, reqid, src_ts, dst_ts))
799 {
800 this->listeners->remove_at(this->listeners, enumerator);
801 }
802 }
803 enumerator->destroy(enumerator);
804 this->mutex->unlock(this->mutex);
805 }
806
807 METHOD(kernel_interface_t, expire, void,
808 private_kernel_interface_t *this, uint8_t protocol, uint32_t spi,
809 host_t *dst, bool hard)
810 {
811 kernel_listener_t *listener;
812 enumerator_t *enumerator;
813
814 this->mutex->lock(this->mutex);
815 enumerator = this->listeners->create_enumerator(this->listeners);
816 while (enumerator->enumerate(enumerator, &listener))
817 {
818 if (listener->expire &&
819 !listener->expire(listener, protocol, spi, dst, hard))
820 {
821 this->listeners->remove_at(this->listeners, enumerator);
822 }
823 }
824 enumerator->destroy(enumerator);
825 this->mutex->unlock(this->mutex);
826 }
827
828 METHOD(kernel_interface_t, mapping, void,
829 private_kernel_interface_t *this, uint8_t protocol, uint32_t spi,
830 host_t *dst, host_t *remote)
831 {
832 kernel_listener_t *listener;
833 enumerator_t *enumerator;
834
835 this->mutex->lock(this->mutex);
836 enumerator = this->listeners->create_enumerator(this->listeners);
837 while (enumerator->enumerate(enumerator, &listener))
838 {
839 if (listener->mapping &&
840 !listener->mapping(listener, protocol, spi, dst, remote))
841 {
842 this->listeners->remove_at(this->listeners, enumerator);
843 }
844 }
845 enumerator->destroy(enumerator);
846 this->mutex->unlock(this->mutex);
847 }
848
849 METHOD(kernel_interface_t, migrate, void,
850 private_kernel_interface_t *this, uint32_t reqid,
851 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
852 policy_dir_t direction, host_t *local, host_t *remote)
853 {
854 kernel_listener_t *listener;
855 enumerator_t *enumerator;
856 this->mutex->lock(this->mutex);
857 enumerator = this->listeners->create_enumerator(this->listeners);
858 while (enumerator->enumerate(enumerator, &listener))
859 {
860 if (listener->migrate &&
861 !listener->migrate(listener, reqid, src_ts, dst_ts, direction,
862 local, remote))
863 {
864 this->listeners->remove_at(this->listeners, enumerator);
865 }
866 }
867 enumerator->destroy(enumerator);
868 this->mutex->unlock(this->mutex);
869 }
870
871 static bool call_roam(kernel_listener_t *listener, bool *roam)
872 {
873 return listener->roam && !listener->roam(listener, *roam);
874 }
875
876 METHOD(kernel_interface_t, roam, void,
877 private_kernel_interface_t *this, bool address)
878 {
879 this->mutex->lock(this->mutex);
880 this->listeners->remove(this->listeners, &address, (void*)call_roam);
881 this->mutex->unlock(this->mutex);
882 }
883
884 METHOD(kernel_interface_t, tun, void,
885 private_kernel_interface_t *this, tun_device_t *tun, bool created)
886 {
887 kernel_listener_t *listener;
888 enumerator_t *enumerator;
889 this->mutex->lock(this->mutex);
890 enumerator = this->listeners->create_enumerator(this->listeners);
891 while (enumerator->enumerate(enumerator, &listener))
892 {
893 if (listener->tun &&
894 !listener->tun(listener, tun, created))
895 {
896 this->listeners->remove_at(this->listeners, enumerator);
897 }
898 }
899 enumerator->destroy(enumerator);
900 this->mutex->unlock(this->mutex);
901 }
902
903 METHOD(kernel_interface_t, register_algorithm, void,
904 private_kernel_interface_t *this, uint16_t alg_id, transform_type_t type,
905 uint16_t kernel_id, char *kernel_name)
906 {
907 kernel_algorithm_t *algorithm;
908
909 INIT(algorithm,
910 .type = type,
911 .ike = alg_id,
912 .kernel = kernel_id,
913 .name = strdup(kernel_name),
914 );
915
916 this->mutex_algs->lock(this->mutex_algs);
917 this->algorithms->insert_first(this->algorithms, algorithm);
918 this->mutex_algs->unlock(this->mutex_algs);
919 }
920
921 METHOD(kernel_interface_t, lookup_algorithm, bool,
922 private_kernel_interface_t *this, uint16_t alg_id, transform_type_t type,
923 uint16_t *kernel_id, char **kernel_name)
924 {
925 kernel_algorithm_t *algorithm;
926 enumerator_t *enumerator;
927 bool found = FALSE;
928
929 this->mutex_algs->lock(this->mutex_algs);
930 enumerator = this->algorithms->create_enumerator(this->algorithms);
931 while (enumerator->enumerate(enumerator, &algorithm))
932 {
933 if (algorithm->type == type && algorithm->ike == alg_id)
934 {
935 if (kernel_id)
936 {
937 *kernel_id = algorithm->kernel;
938 }
939 if (kernel_name)
940 {
941 *kernel_name = algorithm->name;
942 }
943 found = TRUE;
944 break;
945 }
946 }
947 enumerator->destroy(enumerator);
948 this->mutex_algs->unlock(this->mutex_algs);
949 return found;
950 }
951
952 METHOD(kernel_interface_t, destroy, void,
953 private_kernel_interface_t *this)
954 {
955 kernel_algorithm_t *algorithm;
956
957 while (this->algorithms->remove_first(this->algorithms,
958 (void**)&algorithm) == SUCCESS)
959 {
960 free(algorithm->name);
961 free(algorithm);
962 }
963 this->algorithms->destroy(this->algorithms);
964 this->mutex_algs->destroy(this->mutex_algs);
965 DESTROY_IF(this->ipsec);
966 DESTROY_IF(this->net);
967 DESTROY_FUNCTION_IF(this->ifaces_filter, (void*)free);
968 this->reqids->destroy(this->reqids);
969 this->reqids_by_ts->destroy(this->reqids_by_ts);
970 this->listeners->destroy(this->listeners);
971 this->mutex->destroy(this->mutex);
972 free(this);
973 }
974
975 /*
976 * Described in header-file
977 */
978 kernel_interface_t *kernel_interface_create()
979 {
980 private_kernel_interface_t *this;
981 char *ifaces;
982
983 INIT(this,
984 .public = {
985 .get_features = _get_features,
986 .get_spi = _get_spi,
987 .get_cpi = _get_cpi,
988 .alloc_reqid = _alloc_reqid,
989 .release_reqid = _release_reqid,
990 .add_sa = _add_sa,
991 .update_sa = _update_sa,
992 .query_sa = _query_sa,
993 .del_sa = _del_sa,
994 .flush_sas = _flush_sas,
995 .add_policy = _add_policy,
996 .query_policy = _query_policy,
997 .del_policy = _del_policy,
998 .flush_policies = _flush_policies,
999 .get_source_addr = _get_source_addr,
1000 .get_nexthop = _get_nexthop,
1001 .get_interface = _get_interface,
1002 .create_address_enumerator = _create_address_enumerator,
1003 .add_ip = _add_ip,
1004 .del_ip = _del_ip,
1005 .add_route = _add_route,
1006 .del_route = _del_route,
1007 .bypass_socket = _bypass_socket,
1008 .enable_udp_decap = _enable_udp_decap,
1009
1010 .is_interface_usable = _is_interface_usable,
1011 .all_interfaces_usable = _all_interfaces_usable,
1012 .get_address_by_ts = _get_address_by_ts,
1013 .add_ipsec_interface = _add_ipsec_interface,
1014 .remove_ipsec_interface = _remove_ipsec_interface,
1015 .add_net_interface = _add_net_interface,
1016 .remove_net_interface = _remove_net_interface,
1017
1018 .add_listener = _add_listener,
1019 .remove_listener = _remove_listener,
1020 .register_algorithm = _register_algorithm,
1021 .lookup_algorithm = _lookup_algorithm,
1022 .acquire = _acquire,
1023 .expire = _expire,
1024 .mapping = _mapping,
1025 .migrate = _migrate,
1026 .roam = _roam,
1027 .tun = _tun,
1028 .destroy = _destroy,
1029 },
1030 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1031 .listeners = linked_list_create(),
1032 .mutex_algs = mutex_create(MUTEX_TYPE_DEFAULT),
1033 .algorithms = linked_list_create(),
1034 .reqids = hashtable_create((hashtable_hash_t)hash_reqid,
1035 (hashtable_equals_t)equals_reqid, 8),
1036 .reqids_by_ts = hashtable_create((hashtable_hash_t)hash_reqid_by_ts,
1037 (hashtable_equals_t)equals_reqid_by_ts, 8),
1038 );
1039
1040 ifaces = lib->settings->get_str(lib->settings,
1041 "%s.interfaces_use", NULL, lib->ns);
1042 if (!ifaces)
1043 {
1044 this->ifaces_exclude = TRUE;
1045 ifaces = lib->settings->get_str(lib->settings,
1046 "%s.interfaces_ignore", NULL, lib->ns);
1047 }
1048 if (ifaces)
1049 {
1050 enumerator_t *enumerator;
1051 char *iface;
1052
1053 enumerator = enumerator_create_token(ifaces, ",", " ");
1054 while (enumerator->enumerate(enumerator, &iface))
1055 {
1056 if (!this->ifaces_filter)
1057 {
1058 this->ifaces_filter = linked_list_create();
1059 }
1060 this->ifaces_filter->insert_last(this->ifaces_filter,
1061 strdup(iface));
1062 }
1063 enumerator->destroy(enumerator);
1064 }
1065
1066 return &this->public;
1067 }