2 * Copyright (C) 2008-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2010 Martin Willi
5 * Copyright (C) 2010 revosec AG
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>.
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
19 * Copyright (c) 2012 Nanoteq Pty Ltd
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:
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
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
40 #include "kernel_interface.h"
44 #include <threading/mutex.h>
45 #include <utils/linked_list.h>
47 typedef struct private_kernel_interface_t private_kernel_interface_t
;
49 typedef struct kernel_algorithm_t kernel_algorithm_t
;
52 * Mapping of IKE algorithms to kernel-specific algorithm identifiers
54 struct kernel_algorithm_t
{
57 * Transform type of the algorithm
59 transform_type_t type
;
62 * Identifier specified in IKE
67 * Identifier as defined in pfkeyv2.h
72 * Name of the algorithm in linux crypto API
78 * Private data of a kernel_interface_t object.
80 struct private_kernel_interface_t
{
83 * Public part of kernel_interface_t object.
85 kernel_interface_t
public;
88 * Registered IPsec constructor
90 kernel_ipsec_constructor_t ipsec_constructor
;
93 * Registered net constructor
95 kernel_net_constructor_t net_constructor
;
100 kernel_ipsec_t
*ipsec
;
108 * mutex for listeners
113 * list of registered listeners
115 linked_list_t
*listeners
;
118 * mutex for algorithm mappings
123 * List of algorithm mappings (kernel_algorithm_t*)
125 linked_list_t
*algorithms
;
128 * List of interface names to include or exclude (char*), NULL if interfaces
131 linked_list_t
*ifaces_filter
;
134 * TRUE to exclude interfaces listed in ifaces_filter, FALSE to consider
135 * only those listed there
140 METHOD(kernel_interface_t
, get_spi
, status_t
,
141 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
142 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
146 return NOT_SUPPORTED
;
148 return this->ipsec
->get_spi(this->ipsec
, src
, dst
, protocol
, reqid
, spi
);
151 METHOD(kernel_interface_t
, get_cpi
, status_t
,
152 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
153 u_int32_t reqid
, u_int16_t
*cpi
)
157 return NOT_SUPPORTED
;
159 return this->ipsec
->get_cpi(this->ipsec
, src
, dst
, reqid
, cpi
);
162 METHOD(kernel_interface_t
, add_sa
, status_t
,
163 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
164 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
165 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
166 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
167 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
168 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
172 return NOT_SUPPORTED
;
174 return this->ipsec
->add_sa(this->ipsec
, src
, dst
, spi
, protocol
, reqid
,
175 mark
, tfc
, lifetime
, enc_alg
, enc_key
, int_alg
, int_key
, mode
,
176 ipcomp
, cpi
, encap
, esn
, inbound
, src_ts
, dst_ts
);
179 METHOD(kernel_interface_t
, update_sa
, status_t
,
180 private_kernel_interface_t
*this, u_int32_t spi
, u_int8_t protocol
,
181 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
182 bool encap
, bool new_encap
, mark_t mark
)
186 return NOT_SUPPORTED
;
188 return this->ipsec
->update_sa(this->ipsec
, spi
, protocol
, cpi
, src
, dst
,
189 new_src
, new_dst
, encap
, new_encap
, mark
);
192 METHOD(kernel_interface_t
, query_sa
, status_t
,
193 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
194 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
198 return NOT_SUPPORTED
;
200 return this->ipsec
->query_sa(this->ipsec
, src
, dst
, spi
, protocol
, mark
, bytes
);
203 METHOD(kernel_interface_t
, del_sa
, status_t
,
204 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
, u_int32_t spi
,
205 u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
209 return NOT_SUPPORTED
;
211 return this->ipsec
->del_sa(this->ipsec
, src
, dst
, spi
, protocol
, cpi
, mark
);
214 METHOD(kernel_interface_t
, flush_sas
, status_t
,
215 private_kernel_interface_t
*this)
219 return NOT_SUPPORTED
;
221 return this->ipsec
->flush_sas(this->ipsec
);
224 METHOD(kernel_interface_t
, add_policy
, status_t
,
225 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
226 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
227 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
228 mark_t mark
, policy_priority_t priority
)
232 return NOT_SUPPORTED
;
234 return this->ipsec
->add_policy(this->ipsec
, src
, dst
, src_ts
, dst_ts
,
235 direction
, type
, sa
, mark
, priority
);
238 METHOD(kernel_interface_t
, query_policy
, status_t
,
239 private_kernel_interface_t
*this, traffic_selector_t
*src_ts
,
240 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
245 return NOT_SUPPORTED
;
247 return this->ipsec
->query_policy(this->ipsec
, src_ts
, dst_ts
,
248 direction
, mark
, use_time
);
251 METHOD(kernel_interface_t
, del_policy
, status_t
,
252 private_kernel_interface_t
*this, traffic_selector_t
*src_ts
,
253 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
254 mark_t mark
, policy_priority_t priority
)
258 return NOT_SUPPORTED
;
260 return this->ipsec
->del_policy(this->ipsec
, src_ts
, dst_ts
,
261 direction
, reqid
, mark
, priority
);
264 METHOD(kernel_interface_t
, flush_policies
, status_t
,
265 private_kernel_interface_t
*this)
269 return NOT_SUPPORTED
;
271 return this->ipsec
->flush_policies(this->ipsec
);
274 METHOD(kernel_interface_t
, get_source_addr
, host_t
*,
275 private_kernel_interface_t
*this, host_t
*dest
, host_t
*src
)
281 return this->net
->get_source_addr(this->net
, dest
, src
);
284 METHOD(kernel_interface_t
, get_nexthop
, host_t
*,
285 private_kernel_interface_t
*this, host_t
*dest
, host_t
*src
)
291 return this->net
->get_nexthop(this->net
, dest
, src
);
294 METHOD(kernel_interface_t
, get_interface
, bool,
295 private_kernel_interface_t
*this, host_t
*host
, char **name
)
301 return this->net
->get_interface(this->net
, host
, name
);
304 METHOD(kernel_interface_t
, create_address_enumerator
, enumerator_t
*,
305 private_kernel_interface_t
*this, bool include_down_ifaces
,
306 bool include_virtual_ips
, bool include_loopback
)
310 return enumerator_create_empty();
312 return this->net
->create_address_enumerator(this->net
, include_down_ifaces
,
313 include_virtual_ips
, include_loopback
);
316 METHOD(kernel_interface_t
, add_ip
, status_t
,
317 private_kernel_interface_t
*this, host_t
*virtual_ip
, host_t
*iface_ip
)
321 return NOT_SUPPORTED
;
323 return this->net
->add_ip(this->net
, virtual_ip
, iface_ip
);
326 METHOD(kernel_interface_t
, del_ip
, status_t
,
327 private_kernel_interface_t
*this, host_t
*virtual_ip
)
331 return NOT_SUPPORTED
;
333 return this->net
->del_ip(this->net
, virtual_ip
);
336 METHOD(kernel_interface_t
, add_route
, status_t
,
337 private_kernel_interface_t
*this, chunk_t dst_net
,
338 u_int8_t prefixlen
, host_t
*gateway
, host_t
*src_ip
, char *if_name
)
342 return NOT_SUPPORTED
;
344 return this->net
->add_route(this->net
, dst_net
, prefixlen
, gateway
,
348 METHOD(kernel_interface_t
, del_route
, status_t
,
349 private_kernel_interface_t
*this, chunk_t dst_net
,
350 u_int8_t prefixlen
, host_t
*gateway
, host_t
*src_ip
, char *if_name
)
354 return NOT_SUPPORTED
;
356 return this->net
->del_route(this->net
, dst_net
, prefixlen
, gateway
,
360 METHOD(kernel_interface_t
, bypass_socket
, bool,
361 private_kernel_interface_t
*this, int fd
, int family
)
367 return this->ipsec
->bypass_socket(this->ipsec
, fd
, family
);
370 METHOD(kernel_interface_t
, enable_udp_decap
, bool,
371 private_kernel_interface_t
*this, int fd
, int family
, u_int16_t port
)
377 return this->ipsec
->enable_udp_decap(this->ipsec
, fd
, family
, port
);
380 METHOD(kernel_interface_t
, is_interface_usable
, bool,
381 private_kernel_interface_t
*this, const char *iface
)
385 if (!this->ifaces_filter
)
389 expected
= this->ifaces_exclude ? NOT_FOUND
: SUCCESS
;
390 return this->ifaces_filter
->find_first(this->ifaces_filter
, (void*)streq
,
391 NULL
, iface
) == expected
;
394 METHOD(kernel_interface_t
, get_address_by_ts
, status_t
,
395 private_kernel_interface_t
*this, traffic_selector_t
*ts
, host_t
**ip
)
402 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
404 /* if we have a family which includes localhost, we do not
405 * search for an IP, we use the default */
406 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
408 if (family
== AF_INET
)
410 host
= host_create_from_string("127.0.0.1", 0);
414 host
= host_create_from_string("::1", 0);
417 if (ts
->includes(ts
, host
))
419 *ip
= host_create_any(family
);
421 DBG2(DBG_KNL
, "using host %H", *ip
);
426 addrs
= create_address_enumerator(this, TRUE
, TRUE
, TRUE
);
427 while (addrs
->enumerate(addrs
, (void**)&host
))
429 if (ts
->includes(ts
, host
))
432 *ip
= host
->clone(host
);
436 addrs
->destroy(addrs
);
440 DBG2(DBG_KNL
, "no local address found in traffic selector %R", ts
);
444 DBG2(DBG_KNL
, "using host %H", *ip
);
449 METHOD(kernel_interface_t
, add_ipsec_interface
, void,
450 private_kernel_interface_t
*this, kernel_ipsec_constructor_t constructor
)
454 this->ipsec_constructor
= constructor
;
455 this->ipsec
= constructor();
459 METHOD(kernel_interface_t
, remove_ipsec_interface
, void,
460 private_kernel_interface_t
*this, kernel_ipsec_constructor_t constructor
)
462 if (constructor
== this->ipsec_constructor
&& this->ipsec
)
464 this->ipsec
->destroy(this->ipsec
);
469 METHOD(kernel_interface_t
, add_net_interface
, void,
470 private_kernel_interface_t
*this, kernel_net_constructor_t constructor
)
474 this->net_constructor
= constructor
;
475 this->net
= constructor();
479 METHOD(kernel_interface_t
, remove_net_interface
, void,
480 private_kernel_interface_t
*this, kernel_net_constructor_t constructor
)
482 if (constructor
== this->net_constructor
&& this->net
)
484 this->net
->destroy(this->net
);
489 METHOD(kernel_interface_t
, add_listener
, void,
490 private_kernel_interface_t
*this, kernel_listener_t
*listener
)
492 this->mutex
->lock(this->mutex
);
493 this->listeners
->insert_last(this->listeners
, listener
);
494 this->mutex
->unlock(this->mutex
);
497 METHOD(kernel_interface_t
, remove_listener
, void,
498 private_kernel_interface_t
*this, kernel_listener_t
*listener
)
500 this->mutex
->lock(this->mutex
);
501 this->listeners
->remove(this->listeners
, listener
, NULL
);
502 this->mutex
->unlock(this->mutex
);
505 METHOD(kernel_interface_t
, acquire
, void,
506 private_kernel_interface_t
*this, u_int32_t reqid
,
507 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
509 kernel_listener_t
*listener
;
510 enumerator_t
*enumerator
;
511 this->mutex
->lock(this->mutex
);
512 enumerator
= this->listeners
->create_enumerator(this->listeners
);
513 while (enumerator
->enumerate(enumerator
, &listener
))
515 if (listener
->acquire
&&
516 !listener
->acquire(listener
, reqid
, src_ts
, dst_ts
))
518 this->listeners
->remove_at(this->listeners
, enumerator
);
521 enumerator
->destroy(enumerator
);
522 this->mutex
->unlock(this->mutex
);
525 METHOD(kernel_interface_t
, expire
, void,
526 private_kernel_interface_t
*this, u_int32_t reqid
, u_int8_t protocol
,
527 u_int32_t spi
, bool hard
)
529 kernel_listener_t
*listener
;
530 enumerator_t
*enumerator
;
531 this->mutex
->lock(this->mutex
);
532 enumerator
= this->listeners
->create_enumerator(this->listeners
);
533 while (enumerator
->enumerate(enumerator
, &listener
))
535 if (listener
->expire
&&
536 !listener
->expire(listener
, reqid
, protocol
, spi
, hard
))
538 this->listeners
->remove_at(this->listeners
, enumerator
);
541 enumerator
->destroy(enumerator
);
542 this->mutex
->unlock(this->mutex
);
545 METHOD(kernel_interface_t
, mapping
, void,
546 private_kernel_interface_t
*this, u_int32_t reqid
, u_int32_t spi
,
549 kernel_listener_t
*listener
;
550 enumerator_t
*enumerator
;
551 this->mutex
->lock(this->mutex
);
552 enumerator
= this->listeners
->create_enumerator(this->listeners
);
553 while (enumerator
->enumerate(enumerator
, &listener
))
555 if (listener
->mapping
&&
556 !listener
->mapping(listener
, reqid
, spi
, remote
))
558 this->listeners
->remove_at(this->listeners
, enumerator
);
561 enumerator
->destroy(enumerator
);
562 this->mutex
->unlock(this->mutex
);
565 METHOD(kernel_interface_t
, migrate
, void,
566 private_kernel_interface_t
*this, u_int32_t reqid
,
567 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
568 policy_dir_t direction
, host_t
*local
, host_t
*remote
)
570 kernel_listener_t
*listener
;
571 enumerator_t
*enumerator
;
572 this->mutex
->lock(this->mutex
);
573 enumerator
= this->listeners
->create_enumerator(this->listeners
);
574 while (enumerator
->enumerate(enumerator
, &listener
))
576 if (listener
->migrate
&&
577 !listener
->migrate(listener
, reqid
, src_ts
, dst_ts
, direction
,
580 this->listeners
->remove_at(this->listeners
, enumerator
);
583 enumerator
->destroy(enumerator
);
584 this->mutex
->unlock(this->mutex
);
587 static bool call_roam(kernel_listener_t
*listener
, bool *roam
)
589 return listener
->roam
&& !listener
->roam(listener
, *roam
);
592 METHOD(kernel_interface_t
, roam
, void,
593 private_kernel_interface_t
*this, bool address
)
595 this->mutex
->lock(this->mutex
);
596 this->listeners
->remove(this->listeners
, &address
, (void*)call_roam
);
597 this->mutex
->unlock(this->mutex
);
600 METHOD(kernel_interface_t
, register_algorithm
, void,
601 private_kernel_interface_t
*this, u_int16_t alg_id
, transform_type_t type
,
602 u_int16_t kernel_id
, char *kernel_name
)
604 kernel_algorithm_t
*algorithm
;
610 .name
= strdup(kernel_name
),
613 this->mutex_algs
->lock(this->mutex_algs
);
614 this->algorithms
->insert_first(this->algorithms
, algorithm
);
615 this->mutex_algs
->unlock(this->mutex_algs
);
618 METHOD(kernel_interface_t
, lookup_algorithm
, bool,
619 private_kernel_interface_t
*this, u_int16_t alg_id
, transform_type_t type
,
620 u_int16_t
*kernel_id
, char **kernel_name
)
622 kernel_algorithm_t
*algorithm
;
623 enumerator_t
*enumerator
;
626 this->mutex_algs
->lock(this->mutex_algs
);
627 enumerator
= this->algorithms
->create_enumerator(this->algorithms
);
628 while (enumerator
->enumerate(enumerator
, &algorithm
))
630 if (algorithm
->type
== type
&& algorithm
->ike
== alg_id
)
634 *kernel_id
= algorithm
->kernel
;
638 *kernel_name
= algorithm
->name
;
644 enumerator
->destroy(enumerator
);
645 this->mutex_algs
->unlock(this->mutex_algs
);
649 METHOD(kernel_interface_t
, destroy
, void,
650 private_kernel_interface_t
*this)
652 kernel_algorithm_t
*algorithm
;
654 while (this->algorithms
->remove_first(this->algorithms
,
655 (void**)&algorithm
) == SUCCESS
)
657 free(algorithm
->name
);
660 this->algorithms
->destroy(this->algorithms
);
661 this->mutex_algs
->destroy(this->mutex_algs
);
662 DESTROY_IF(this->ipsec
);
663 DESTROY_IF(this->net
);
664 DESTROY_FUNCTION_IF(this->ifaces_filter
, (void*)free
);
665 this->listeners
->destroy(this->listeners
);
666 this->mutex
->destroy(this->mutex
);
671 * Described in header-file
673 kernel_interface_t
*kernel_interface_create()
675 private_kernel_interface_t
*this;
683 .update_sa
= _update_sa
,
684 .query_sa
= _query_sa
,
686 .flush_sas
= _flush_sas
,
687 .add_policy
= _add_policy
,
688 .query_policy
= _query_policy
,
689 .del_policy
= _del_policy
,
690 .flush_policies
= _flush_policies
,
691 .get_source_addr
= _get_source_addr
,
692 .get_nexthop
= _get_nexthop
,
693 .get_interface
= _get_interface
,
694 .create_address_enumerator
= _create_address_enumerator
,
697 .add_route
= _add_route
,
698 .del_route
= _del_route
,
699 .bypass_socket
= _bypass_socket
,
700 .enable_udp_decap
= _enable_udp_decap
,
702 .is_interface_usable
= _is_interface_usable
,
703 .get_address_by_ts
= _get_address_by_ts
,
704 .add_ipsec_interface
= _add_ipsec_interface
,
705 .remove_ipsec_interface
= _remove_ipsec_interface
,
706 .add_net_interface
= _add_net_interface
,
707 .remove_net_interface
= _remove_net_interface
,
709 .add_listener
= _add_listener
,
710 .remove_listener
= _remove_listener
,
711 .register_algorithm
= _register_algorithm
,
712 .lookup_algorithm
= _lookup_algorithm
,
720 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
721 .listeners
= linked_list_create(),
722 .mutex_algs
= mutex_create(MUTEX_TYPE_DEFAULT
),
723 .algorithms
= linked_list_create(),
726 ifaces
= lib
->settings
->get_str(lib
->settings
,
727 "%s.interfaces_use", NULL
, hydra
->daemon
);
730 ifaces
= lib
->settings
->get_str(lib
->settings
,
731 "%s.interfaces_ignore", NULL
, hydra
->daemon
);
734 this->ifaces_exclude
= TRUE
;
739 enumerator_t
*enumerator
;
742 enumerator
= enumerator_create_token(ifaces
, ",", " ");
743 while (enumerator
->enumerate(enumerator
, &iface
))
745 if (!this->ifaces_filter
)
747 this->ifaces_filter
= linked_list_create();
749 this->ifaces_filter
->insert_last(this->ifaces_filter
,
752 enumerator
->destroy(enumerator
);
755 return &this->public;