child-sa: Replace reqid based marks by "unique" marks
[strongswan.git] / src / libhydra / kernel / kernel_interface.c
1 /*
2 * Copyright (C) 2008-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2010 Martin Willi
5 * Copyright (C) 2010 revosec AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /*
19 * Copyright (c) 2012 Nanoteq Pty Ltd
20 *
21 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 * of this software and associated documentation files (the "Software"), to deal
23 * in the Software without restriction, including without limitation the rights
24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the Software is
26 * furnished to do so, subject to the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 * THE SOFTWARE.
38 */
39
40 #include "kernel_interface.h"
41
42 #include <hydra.h>
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 u_int16_t ike;
67
68 /**
69 * Identifier as defined in pfkeyv2.h
70 */
71 u_int16_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 u_int8_t protocol, u_int32_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 u_int16_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 u_int32_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, u_int32_t *reqid)
332 {
333 static u_int32_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, u_int32_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, host_t *src, host_t *dst,
420 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
421 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
422 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
423 u_int16_t ipcomp, u_int16_t cpi, u_int32_t replay_window,
424 bool initiator, bool encap, bool esn, bool inbound,
425 linked_list_t *src_ts, linked_list_t *dst_ts)
426 {
427 if (!this->ipsec)
428 {
429 return NOT_SUPPORTED;
430 }
431 return this->ipsec->add_sa(this->ipsec, src, dst, spi, protocol, reqid,
432 mark, tfc, lifetime, enc_alg, enc_key, int_alg, int_key, mode,
433 ipcomp, cpi, replay_window, initiator, encap, esn, inbound,
434 src_ts, dst_ts);
435 }
436
437 METHOD(kernel_interface_t, update_sa, status_t,
438 private_kernel_interface_t *this, u_int32_t spi, u_int8_t protocol,
439 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
440 bool encap, bool new_encap, mark_t mark)
441 {
442 if (!this->ipsec)
443 {
444 return NOT_SUPPORTED;
445 }
446 return this->ipsec->update_sa(this->ipsec, spi, protocol, cpi, src, dst,
447 new_src, new_dst, encap, new_encap, mark);
448 }
449
450 METHOD(kernel_interface_t, query_sa, status_t,
451 private_kernel_interface_t *this, host_t *src, host_t *dst,
452 u_int32_t spi, u_int8_t protocol, mark_t mark,
453 u_int64_t *bytes, u_int64_t *packets, time_t *time)
454 {
455 if (!this->ipsec)
456 {
457 return NOT_SUPPORTED;
458 }
459 return this->ipsec->query_sa(this->ipsec, src, dst, spi, protocol, mark,
460 bytes, packets, time);
461 }
462
463 METHOD(kernel_interface_t, del_sa, status_t,
464 private_kernel_interface_t *this, host_t *src, host_t *dst, u_int32_t spi,
465 u_int8_t protocol, u_int16_t cpi, mark_t mark)
466 {
467 if (!this->ipsec)
468 {
469 return NOT_SUPPORTED;
470 }
471 return this->ipsec->del_sa(this->ipsec, src, dst, spi, protocol, cpi, mark);
472 }
473
474 METHOD(kernel_interface_t, flush_sas, status_t,
475 private_kernel_interface_t *this)
476 {
477 if (!this->ipsec)
478 {
479 return NOT_SUPPORTED;
480 }
481 return this->ipsec->flush_sas(this->ipsec);
482 }
483
484 METHOD(kernel_interface_t, add_policy, status_t,
485 private_kernel_interface_t *this, host_t *src, host_t *dst,
486 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
487 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
488 mark_t mark, policy_priority_t priority)
489 {
490 if (!this->ipsec)
491 {
492 return NOT_SUPPORTED;
493 }
494 return this->ipsec->add_policy(this->ipsec, src, dst, src_ts, dst_ts,
495 direction, type, sa, mark, priority);
496 }
497
498 METHOD(kernel_interface_t, query_policy, status_t,
499 private_kernel_interface_t *this, traffic_selector_t *src_ts,
500 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
501 time_t *use_time)
502 {
503 if (!this->ipsec)
504 {
505 return NOT_SUPPORTED;
506 }
507 return this->ipsec->query_policy(this->ipsec, src_ts, dst_ts,
508 direction, mark, use_time);
509 }
510
511 METHOD(kernel_interface_t, del_policy, status_t,
512 private_kernel_interface_t *this, traffic_selector_t *src_ts,
513 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
514 mark_t mark, policy_priority_t priority)
515 {
516 if (!this->ipsec)
517 {
518 return NOT_SUPPORTED;
519 }
520 return this->ipsec->del_policy(this->ipsec, src_ts, dst_ts,
521 direction, reqid, mark, priority);
522 }
523
524 METHOD(kernel_interface_t, flush_policies, status_t,
525 private_kernel_interface_t *this)
526 {
527 if (!this->ipsec)
528 {
529 return NOT_SUPPORTED;
530 }
531 return this->ipsec->flush_policies(this->ipsec);
532 }
533
534 METHOD(kernel_interface_t, get_source_addr, host_t*,
535 private_kernel_interface_t *this, host_t *dest, host_t *src)
536 {
537 if (!this->net)
538 {
539 return NULL;
540 }
541 return this->net->get_source_addr(this->net, dest, src);
542 }
543
544 METHOD(kernel_interface_t, get_nexthop, host_t*,
545 private_kernel_interface_t *this, host_t *dest, int prefix, host_t *src)
546 {
547 if (!this->net)
548 {
549 return NULL;
550 }
551 return this->net->get_nexthop(this->net, dest, prefix, src);
552 }
553
554 METHOD(kernel_interface_t, get_interface, bool,
555 private_kernel_interface_t *this, host_t *host, char **name)
556 {
557 if (!this->net)
558 {
559 return NULL;
560 }
561 return this->net->get_interface(this->net, host, name);
562 }
563
564 METHOD(kernel_interface_t, create_address_enumerator, enumerator_t*,
565 private_kernel_interface_t *this, kernel_address_type_t which)
566 {
567 if (!this->net)
568 {
569 return enumerator_create_empty();
570 }
571 return this->net->create_address_enumerator(this->net, which);
572 }
573
574 METHOD(kernel_interface_t, add_ip, status_t,
575 private_kernel_interface_t *this, host_t *virtual_ip, int prefix,
576 char *iface)
577 {
578 if (!this->net)
579 {
580 return NOT_SUPPORTED;
581 }
582 return this->net->add_ip(this->net, virtual_ip, prefix, iface);
583 }
584
585 METHOD(kernel_interface_t, del_ip, status_t,
586 private_kernel_interface_t *this, host_t *virtual_ip, int prefix, bool wait)
587 {
588 if (!this->net)
589 {
590 return NOT_SUPPORTED;
591 }
592 return this->net->del_ip(this->net, virtual_ip, prefix, wait);
593 }
594
595 METHOD(kernel_interface_t, add_route, status_t,
596 private_kernel_interface_t *this, chunk_t dst_net,
597 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
598 {
599 if (!this->net)
600 {
601 return NOT_SUPPORTED;
602 }
603 return this->net->add_route(this->net, dst_net, prefixlen, gateway,
604 src_ip, if_name);
605 }
606
607 METHOD(kernel_interface_t, del_route, status_t,
608 private_kernel_interface_t *this, chunk_t dst_net,
609 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
610 {
611 if (!this->net)
612 {
613 return NOT_SUPPORTED;
614 }
615 return this->net->del_route(this->net, dst_net, prefixlen, gateway,
616 src_ip, if_name);
617 }
618
619 METHOD(kernel_interface_t, bypass_socket, bool,
620 private_kernel_interface_t *this, int fd, int family)
621 {
622 if (!this->ipsec)
623 {
624 return FALSE;
625 }
626 return this->ipsec->bypass_socket(this->ipsec, fd, family);
627 }
628
629 METHOD(kernel_interface_t, enable_udp_decap, bool,
630 private_kernel_interface_t *this, int fd, int family, u_int16_t port)
631 {
632 if (!this->ipsec)
633 {
634 return FALSE;
635 }
636 return this->ipsec->enable_udp_decap(this->ipsec, fd, family, port);
637 }
638
639 METHOD(kernel_interface_t, is_interface_usable, bool,
640 private_kernel_interface_t *this, const char *iface)
641 {
642 status_t expected;
643
644 if (!this->ifaces_filter)
645 {
646 return TRUE;
647 }
648 expected = this->ifaces_exclude ? NOT_FOUND : SUCCESS;
649 return this->ifaces_filter->find_first(this->ifaces_filter, (void*)streq,
650 NULL, iface) == expected;
651 }
652
653 METHOD(kernel_interface_t, all_interfaces_usable, bool,
654 private_kernel_interface_t *this)
655 {
656 return this->ifaces_filter == NULL;
657 }
658
659 METHOD(kernel_interface_t, get_address_by_ts, status_t,
660 private_kernel_interface_t *this, traffic_selector_t *ts,
661 host_t **ip, bool *vip)
662 {
663 enumerator_t *addrs;
664 host_t *host;
665 int family;
666 bool found = FALSE;
667
668 DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
669
670 /* if we have a family which includes localhost, we do not
671 * search for an IP, we use the default */
672 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
673
674 if (family == AF_INET)
675 {
676 host = host_create_from_string("127.0.0.1", 0);
677 }
678 else
679 {
680 host = host_create_from_string("::1", 0);
681 }
682
683 if (ts->includes(ts, host))
684 {
685 *ip = host_create_any(family);
686 host->destroy(host);
687 DBG2(DBG_KNL, "using host %H", *ip);
688 return SUCCESS;
689 }
690 host->destroy(host);
691
692 /* try virtual IPs only first (on all interfaces) */
693 addrs = create_address_enumerator(this,
694 ADDR_TYPE_ALL ^ ADDR_TYPE_REGULAR);
695 while (addrs->enumerate(addrs, (void**)&host))
696 {
697 if (ts->includes(ts, host))
698 {
699 found = TRUE;
700 *ip = host->clone(host);
701 if (vip)
702 {
703 *vip = TRUE;
704 }
705 break;
706 }
707 }
708 addrs->destroy(addrs);
709
710 if (!found)
711 { /* then try the regular addresses (on all interfaces) */
712 addrs = create_address_enumerator(this,
713 ADDR_TYPE_ALL ^ ADDR_TYPE_VIRTUAL);
714 while (addrs->enumerate(addrs, (void**)&host))
715 {
716 if (ts->includes(ts, host))
717 {
718 found = TRUE;
719 *ip = host->clone(host);
720 if (vip)
721 {
722 *vip = FALSE;
723 }
724 break;
725 }
726 }
727 addrs->destroy(addrs);
728 }
729
730 if (!found)
731 {
732 DBG2(DBG_KNL, "no local address found in traffic selector %R", ts);
733 return FAILED;
734 }
735
736 DBG2(DBG_KNL, "using host %H", *ip);
737 return SUCCESS;
738 }
739
740
741 METHOD(kernel_interface_t, add_ipsec_interface, void,
742 private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
743 {
744 if (!this->ipsec)
745 {
746 this->ipsec_constructor = constructor;
747 this->ipsec = constructor();
748 }
749 }
750
751 METHOD(kernel_interface_t, remove_ipsec_interface, void,
752 private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
753 {
754 if (constructor == this->ipsec_constructor && this->ipsec)
755 {
756 this->ipsec->destroy(this->ipsec);
757 this->ipsec = NULL;
758 }
759 }
760
761 METHOD(kernel_interface_t, add_net_interface, void,
762 private_kernel_interface_t *this, kernel_net_constructor_t constructor)
763 {
764 if (!this->net)
765 {
766 this->net_constructor = constructor;
767 this->net = constructor();
768 }
769 }
770
771 METHOD(kernel_interface_t, remove_net_interface, void,
772 private_kernel_interface_t *this, kernel_net_constructor_t constructor)
773 {
774 if (constructor == this->net_constructor && this->net)
775 {
776 this->net->destroy(this->net);
777 this->net = NULL;
778 }
779 }
780
781 METHOD(kernel_interface_t, add_listener, void,
782 private_kernel_interface_t *this, kernel_listener_t *listener)
783 {
784 this->mutex->lock(this->mutex);
785 this->listeners->insert_last(this->listeners, listener);
786 this->mutex->unlock(this->mutex);
787 }
788
789 METHOD(kernel_interface_t, remove_listener, void,
790 private_kernel_interface_t *this, kernel_listener_t *listener)
791 {
792 this->mutex->lock(this->mutex);
793 this->listeners->remove(this->listeners, listener, NULL);
794 this->mutex->unlock(this->mutex);
795 }
796
797 METHOD(kernel_interface_t, acquire, void,
798 private_kernel_interface_t *this, u_int32_t reqid,
799 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
800 {
801 kernel_listener_t *listener;
802 enumerator_t *enumerator;
803 this->mutex->lock(this->mutex);
804 enumerator = this->listeners->create_enumerator(this->listeners);
805 while (enumerator->enumerate(enumerator, &listener))
806 {
807 if (listener->acquire &&
808 !listener->acquire(listener, reqid, src_ts, dst_ts))
809 {
810 this->listeners->remove_at(this->listeners, enumerator);
811 }
812 }
813 enumerator->destroy(enumerator);
814 this->mutex->unlock(this->mutex);
815 }
816
817 METHOD(kernel_interface_t, expire, void,
818 private_kernel_interface_t *this, u_int32_t reqid, u_int8_t protocol,
819 u_int32_t spi, bool hard)
820 {
821 kernel_listener_t *listener;
822 enumerator_t *enumerator;
823 this->mutex->lock(this->mutex);
824 enumerator = this->listeners->create_enumerator(this->listeners);
825 while (enumerator->enumerate(enumerator, &listener))
826 {
827 if (listener->expire &&
828 !listener->expire(listener, reqid, protocol, spi, hard))
829 {
830 this->listeners->remove_at(this->listeners, enumerator);
831 }
832 }
833 enumerator->destroy(enumerator);
834 this->mutex->unlock(this->mutex);
835 }
836
837 METHOD(kernel_interface_t, mapping, void,
838 private_kernel_interface_t *this, u_int32_t reqid, u_int32_t spi,
839 host_t *remote)
840 {
841 kernel_listener_t *listener;
842 enumerator_t *enumerator;
843 this->mutex->lock(this->mutex);
844 enumerator = this->listeners->create_enumerator(this->listeners);
845 while (enumerator->enumerate(enumerator, &listener))
846 {
847 if (listener->mapping &&
848 !listener->mapping(listener, reqid, spi, remote))
849 {
850 this->listeners->remove_at(this->listeners, enumerator);
851 }
852 }
853 enumerator->destroy(enumerator);
854 this->mutex->unlock(this->mutex);
855 }
856
857 METHOD(kernel_interface_t, migrate, void,
858 private_kernel_interface_t *this, u_int32_t reqid,
859 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
860 policy_dir_t direction, host_t *local, host_t *remote)
861 {
862 kernel_listener_t *listener;
863 enumerator_t *enumerator;
864 this->mutex->lock(this->mutex);
865 enumerator = this->listeners->create_enumerator(this->listeners);
866 while (enumerator->enumerate(enumerator, &listener))
867 {
868 if (listener->migrate &&
869 !listener->migrate(listener, reqid, src_ts, dst_ts, direction,
870 local, remote))
871 {
872 this->listeners->remove_at(this->listeners, enumerator);
873 }
874 }
875 enumerator->destroy(enumerator);
876 this->mutex->unlock(this->mutex);
877 }
878
879 static bool call_roam(kernel_listener_t *listener, bool *roam)
880 {
881 return listener->roam && !listener->roam(listener, *roam);
882 }
883
884 METHOD(kernel_interface_t, roam, void,
885 private_kernel_interface_t *this, bool address)
886 {
887 this->mutex->lock(this->mutex);
888 this->listeners->remove(this->listeners, &address, (void*)call_roam);
889 this->mutex->unlock(this->mutex);
890 }
891
892 METHOD(kernel_interface_t, tun, void,
893 private_kernel_interface_t *this, tun_device_t *tun, bool created)
894 {
895 kernel_listener_t *listener;
896 enumerator_t *enumerator;
897 this->mutex->lock(this->mutex);
898 enumerator = this->listeners->create_enumerator(this->listeners);
899 while (enumerator->enumerate(enumerator, &listener))
900 {
901 if (listener->tun &&
902 !listener->tun(listener, tun, created))
903 {
904 this->listeners->remove_at(this->listeners, enumerator);
905 }
906 }
907 enumerator->destroy(enumerator);
908 this->mutex->unlock(this->mutex);
909 }
910
911 METHOD(kernel_interface_t, register_algorithm, void,
912 private_kernel_interface_t *this, u_int16_t alg_id, transform_type_t type,
913 u_int16_t kernel_id, char *kernel_name)
914 {
915 kernel_algorithm_t *algorithm;
916
917 INIT(algorithm,
918 .type = type,
919 .ike = alg_id,
920 .kernel = kernel_id,
921 .name = strdup(kernel_name),
922 );
923
924 this->mutex_algs->lock(this->mutex_algs);
925 this->algorithms->insert_first(this->algorithms, algorithm);
926 this->mutex_algs->unlock(this->mutex_algs);
927 }
928
929 METHOD(kernel_interface_t, lookup_algorithm, bool,
930 private_kernel_interface_t *this, u_int16_t alg_id, transform_type_t type,
931 u_int16_t *kernel_id, char **kernel_name)
932 {
933 kernel_algorithm_t *algorithm;
934 enumerator_t *enumerator;
935 bool found = FALSE;
936
937 this->mutex_algs->lock(this->mutex_algs);
938 enumerator = this->algorithms->create_enumerator(this->algorithms);
939 while (enumerator->enumerate(enumerator, &algorithm))
940 {
941 if (algorithm->type == type && algorithm->ike == alg_id)
942 {
943 if (kernel_id)
944 {
945 *kernel_id = algorithm->kernel;
946 }
947 if (kernel_name)
948 {
949 *kernel_name = algorithm->name;
950 }
951 found = TRUE;
952 break;
953 }
954 }
955 enumerator->destroy(enumerator);
956 this->mutex_algs->unlock(this->mutex_algs);
957 return found;
958 }
959
960 METHOD(kernel_interface_t, destroy, void,
961 private_kernel_interface_t *this)
962 {
963 kernel_algorithm_t *algorithm;
964
965 while (this->algorithms->remove_first(this->algorithms,
966 (void**)&algorithm) == SUCCESS)
967 {
968 free(algorithm->name);
969 free(algorithm);
970 }
971 this->algorithms->destroy(this->algorithms);
972 this->mutex_algs->destroy(this->mutex_algs);
973 DESTROY_IF(this->ipsec);
974 DESTROY_IF(this->net);
975 DESTROY_FUNCTION_IF(this->ifaces_filter, (void*)free);
976 this->reqids->destroy(this->reqids);
977 this->reqids_by_ts->destroy(this->reqids_by_ts);
978 this->listeners->destroy(this->listeners);
979 this->mutex->destroy(this->mutex);
980 free(this);
981 }
982
983 /*
984 * Described in header-file
985 */
986 kernel_interface_t *kernel_interface_create()
987 {
988 private_kernel_interface_t *this;
989 char *ifaces;
990
991 INIT(this,
992 .public = {
993 .get_features = _get_features,
994 .get_spi = _get_spi,
995 .get_cpi = _get_cpi,
996 .alloc_reqid = _alloc_reqid,
997 .release_reqid = _release_reqid,
998 .add_sa = _add_sa,
999 .update_sa = _update_sa,
1000 .query_sa = _query_sa,
1001 .del_sa = _del_sa,
1002 .flush_sas = _flush_sas,
1003 .add_policy = _add_policy,
1004 .query_policy = _query_policy,
1005 .del_policy = _del_policy,
1006 .flush_policies = _flush_policies,
1007 .get_source_addr = _get_source_addr,
1008 .get_nexthop = _get_nexthop,
1009 .get_interface = _get_interface,
1010 .create_address_enumerator = _create_address_enumerator,
1011 .add_ip = _add_ip,
1012 .del_ip = _del_ip,
1013 .add_route = _add_route,
1014 .del_route = _del_route,
1015 .bypass_socket = _bypass_socket,
1016 .enable_udp_decap = _enable_udp_decap,
1017
1018 .is_interface_usable = _is_interface_usable,
1019 .all_interfaces_usable = _all_interfaces_usable,
1020 .get_address_by_ts = _get_address_by_ts,
1021 .add_ipsec_interface = _add_ipsec_interface,
1022 .remove_ipsec_interface = _remove_ipsec_interface,
1023 .add_net_interface = _add_net_interface,
1024 .remove_net_interface = _remove_net_interface,
1025
1026 .add_listener = _add_listener,
1027 .remove_listener = _remove_listener,
1028 .register_algorithm = _register_algorithm,
1029 .lookup_algorithm = _lookup_algorithm,
1030 .acquire = _acquire,
1031 .expire = _expire,
1032 .mapping = _mapping,
1033 .migrate = _migrate,
1034 .roam = _roam,
1035 .tun = _tun,
1036 .destroy = _destroy,
1037 },
1038 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1039 .listeners = linked_list_create(),
1040 .mutex_algs = mutex_create(MUTEX_TYPE_DEFAULT),
1041 .algorithms = linked_list_create(),
1042 .reqids = hashtable_create((hashtable_hash_t)hash_reqid,
1043 (hashtable_equals_t)equals_reqid, 8),
1044 .reqids_by_ts = hashtable_create((hashtable_hash_t)hash_reqid_by_ts,
1045 (hashtable_equals_t)equals_reqid_by_ts, 8),
1046 );
1047
1048 ifaces = lib->settings->get_str(lib->settings,
1049 "%s.interfaces_use", NULL, lib->ns);
1050 if (!ifaces)
1051 {
1052 this->ifaces_exclude = TRUE;
1053 ifaces = lib->settings->get_str(lib->settings,
1054 "%s.interfaces_ignore", NULL, lib->ns);
1055 }
1056 if (ifaces)
1057 {
1058 enumerator_t *enumerator;
1059 char *iface;
1060
1061 enumerator = enumerator_create_token(ifaces, ",", " ");
1062 while (enumerator->enumerate(enumerator, &iface))
1063 {
1064 if (!this->ifaces_filter)
1065 {
1066 this->ifaces_filter = linked_list_create();
1067 }
1068 this->ifaces_filter->insert_last(this->ifaces_filter,
1069 strdup(iface));
1070 }
1071 enumerator->destroy(enumerator);
1072 }
1073
1074 return &this->public;
1075 }