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"
43 #include <threading/mutex.h>
44 #include <utils/linked_list.h>
46 typedef struct private_kernel_interface_t private_kernel_interface_t
;
48 typedef struct kernel_algorithm_t kernel_algorithm_t
;
51 * Mapping of IKE algorithms to kernel-specific algorithm identifiers
53 struct kernel_algorithm_t
{
56 * Transform type of the algorithm
58 transform_type_t type
;
61 * Identifier specified in IKE
66 * Identifier as defined in pfkeyv2.h
71 * Name of the algorithm in linux crypto API
77 * Private data of a kernel_interface_t object.
79 struct private_kernel_interface_t
{
82 * Public part of kernel_interface_t object.
84 kernel_interface_t
public;
87 * Registered IPsec constructor
89 kernel_ipsec_constructor_t ipsec_constructor
;
92 * Registered net constructor
94 kernel_net_constructor_t net_constructor
;
99 kernel_ipsec_t
*ipsec
;
107 * mutex for listeners
112 * list of registered listeners
114 linked_list_t
*listeners
;
117 * mutex for algorithm mappings
122 * List of algorithm mappings (kernel_algorithm_t*)
124 linked_list_t
*algorithms
;
127 METHOD(kernel_interface_t
, get_spi
, status_t
,
128 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
129 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
133 return NOT_SUPPORTED
;
135 return this->ipsec
->get_spi(this->ipsec
, src
, dst
, protocol
, reqid
, spi
);
138 METHOD(kernel_interface_t
, get_cpi
, status_t
,
139 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
140 u_int32_t reqid
, u_int16_t
*cpi
)
144 return NOT_SUPPORTED
;
146 return this->ipsec
->get_cpi(this->ipsec
, src
, dst
, reqid
, cpi
);
149 METHOD(kernel_interface_t
, add_sa
, status_t
,
150 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
151 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
152 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
153 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
154 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
155 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
159 return NOT_SUPPORTED
;
161 return this->ipsec
->add_sa(this->ipsec
, src
, dst
, spi
, protocol
, reqid
,
162 mark
, tfc
, lifetime
, enc_alg
, enc_key
, int_alg
, int_key
, mode
,
163 ipcomp
, cpi
, encap
, esn
, inbound
, src_ts
, dst_ts
);
166 METHOD(kernel_interface_t
, update_sa
, status_t
,
167 private_kernel_interface_t
*this, u_int32_t spi
, u_int8_t protocol
,
168 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
169 bool encap
, bool new_encap
, mark_t mark
)
173 return NOT_SUPPORTED
;
175 return this->ipsec
->update_sa(this->ipsec
, spi
, protocol
, cpi
, src
, dst
,
176 new_src
, new_dst
, encap
, new_encap
, mark
);
179 METHOD(kernel_interface_t
, query_sa
, status_t
,
180 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
181 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
185 return NOT_SUPPORTED
;
187 return this->ipsec
->query_sa(this->ipsec
, src
, dst
, spi
, protocol
, mark
, bytes
);
190 METHOD(kernel_interface_t
, del_sa
, status_t
,
191 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
, u_int32_t spi
,
192 u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
196 return NOT_SUPPORTED
;
198 return this->ipsec
->del_sa(this->ipsec
, src
, dst
, spi
, protocol
, cpi
, mark
);
201 METHOD(kernel_interface_t
, flush_sas
, status_t
,
202 private_kernel_interface_t
*this)
206 return NOT_SUPPORTED
;
208 return this->ipsec
->flush_sas(this->ipsec
);
211 METHOD(kernel_interface_t
, add_policy
, status_t
,
212 private_kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
213 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
214 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
215 mark_t mark
, policy_priority_t priority
)
219 return NOT_SUPPORTED
;
221 return this->ipsec
->add_policy(this->ipsec
, src
, dst
, src_ts
, dst_ts
,
222 direction
, type
, sa
, mark
, priority
);
225 METHOD(kernel_interface_t
, query_policy
, status_t
,
226 private_kernel_interface_t
*this, traffic_selector_t
*src_ts
,
227 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
232 return NOT_SUPPORTED
;
234 return this->ipsec
->query_policy(this->ipsec
, src_ts
, dst_ts
,
235 direction
, mark
, use_time
);
238 METHOD(kernel_interface_t
, del_policy
, status_t
,
239 private_kernel_interface_t
*this, traffic_selector_t
*src_ts
,
240 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
241 mark_t mark
, policy_priority_t priority
)
245 return NOT_SUPPORTED
;
247 return this->ipsec
->del_policy(this->ipsec
, src_ts
, dst_ts
,
248 direction
, reqid
, mark
, priority
);
251 METHOD(kernel_interface_t
, flush_policies
, status_t
,
252 private_kernel_interface_t
*this)
256 return NOT_SUPPORTED
;
258 return this->ipsec
->flush_policies(this->ipsec
);
261 METHOD(kernel_interface_t
, get_source_addr
, host_t
*,
262 private_kernel_interface_t
*this, host_t
*dest
, host_t
*src
)
268 return this->net
->get_source_addr(this->net
, dest
, src
);
271 METHOD(kernel_interface_t
, get_nexthop
, host_t
*,
272 private_kernel_interface_t
*this, host_t
*dest
, host_t
*src
)
278 return this->net
->get_nexthop(this->net
, dest
, src
);
281 METHOD(kernel_interface_t
, get_interface
, char*,
282 private_kernel_interface_t
*this, host_t
*host
)
288 return this->net
->get_interface(this->net
, host
);
291 METHOD(kernel_interface_t
, create_address_enumerator
, enumerator_t
*,
292 private_kernel_interface_t
*this, bool include_down_ifaces
,
293 bool include_virtual_ips
)
297 return enumerator_create_empty();
299 return this->net
->create_address_enumerator(this->net
, include_down_ifaces
,
300 include_virtual_ips
);
303 METHOD(kernel_interface_t
, add_ip
, status_t
,
304 private_kernel_interface_t
*this, host_t
*virtual_ip
, host_t
*iface_ip
)
308 return NOT_SUPPORTED
;
310 return this->net
->add_ip(this->net
, virtual_ip
, iface_ip
);
313 METHOD(kernel_interface_t
, del_ip
, status_t
,
314 private_kernel_interface_t
*this, host_t
*virtual_ip
)
318 return NOT_SUPPORTED
;
320 return this->net
->del_ip(this->net
, virtual_ip
);
323 METHOD(kernel_interface_t
, add_route
, status_t
,
324 private_kernel_interface_t
*this, chunk_t dst_net
,
325 u_int8_t prefixlen
, host_t
*gateway
, host_t
*src_ip
, char *if_name
)
329 return NOT_SUPPORTED
;
331 return this->net
->add_route(this->net
, dst_net
, prefixlen
, gateway
,
335 METHOD(kernel_interface_t
, del_route
, status_t
,
336 private_kernel_interface_t
*this, chunk_t dst_net
,
337 u_int8_t prefixlen
, host_t
*gateway
, host_t
*src_ip
, char *if_name
)
341 return NOT_SUPPORTED
;
343 return this->net
->del_route(this->net
, dst_net
, prefixlen
, gateway
,
347 METHOD(kernel_interface_t
, bypass_socket
, bool,
348 private_kernel_interface_t
*this, int fd
, int family
)
354 return this->ipsec
->bypass_socket(this->ipsec
, fd
, family
);
357 METHOD(kernel_interface_t
, enable_udp_decap
, bool,
358 private_kernel_interface_t
*this, int fd
, int family
, u_int16_t port
)
364 return this->ipsec
->enable_udp_decap(this->ipsec
, fd
, family
, port
);
367 METHOD(kernel_interface_t
, get_address_by_ts
, status_t
,
368 private_kernel_interface_t
*this, traffic_selector_t
*ts
, host_t
**ip
)
375 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
377 /* if we have a family which includes localhost, we do not
378 * search for an IP, we use the default */
379 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
381 if (family
== AF_INET
)
383 host
= host_create_from_string("127.0.0.1", 0);
387 host
= host_create_from_string("::1", 0);
390 if (ts
->includes(ts
, host
))
392 *ip
= host_create_any(family
);
394 DBG2(DBG_KNL
, "using host %H", *ip
);
399 addrs
= create_address_enumerator(this, TRUE
, TRUE
);
400 while (addrs
->enumerate(addrs
, (void**)&host
))
402 if (ts
->includes(ts
, host
))
405 *ip
= host
->clone(host
);
409 addrs
->destroy(addrs
);
413 DBG2(DBG_KNL
, "no local address found in traffic selector %R", ts
);
417 DBG2(DBG_KNL
, "using host %H", *ip
);
422 METHOD(kernel_interface_t
, add_ipsec_interface
, void,
423 private_kernel_interface_t
*this, kernel_ipsec_constructor_t constructor
)
427 this->ipsec_constructor
= constructor
;
428 this->ipsec
= constructor();
432 METHOD(kernel_interface_t
, remove_ipsec_interface
, void,
433 private_kernel_interface_t
*this, kernel_ipsec_constructor_t constructor
)
435 if (constructor
== this->ipsec_constructor
&& this->ipsec
)
437 this->ipsec
->destroy(this->ipsec
);
442 METHOD(kernel_interface_t
, add_net_interface
, void,
443 private_kernel_interface_t
*this, kernel_net_constructor_t constructor
)
447 this->net_constructor
= constructor
;
448 this->net
= constructor();
452 METHOD(kernel_interface_t
, remove_net_interface
, void,
453 private_kernel_interface_t
*this, kernel_net_constructor_t constructor
)
455 if (constructor
== this->net_constructor
&& this->net
)
457 this->net
->destroy(this->net
);
462 METHOD(kernel_interface_t
, add_listener
, void,
463 private_kernel_interface_t
*this, kernel_listener_t
*listener
)
465 this->mutex
->lock(this->mutex
);
466 this->listeners
->insert_last(this->listeners
, listener
);
467 this->mutex
->unlock(this->mutex
);
470 METHOD(kernel_interface_t
, remove_listener
, void,
471 private_kernel_interface_t
*this, kernel_listener_t
*listener
)
473 this->mutex
->lock(this->mutex
);
474 this->listeners
->remove(this->listeners
, listener
, NULL
);
475 this->mutex
->unlock(this->mutex
);
478 METHOD(kernel_interface_t
, acquire
, void,
479 private_kernel_interface_t
*this, u_int32_t reqid
,
480 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
482 kernel_listener_t
*listener
;
483 enumerator_t
*enumerator
;
484 this->mutex
->lock(this->mutex
);
485 enumerator
= this->listeners
->create_enumerator(this->listeners
);
486 while (enumerator
->enumerate(enumerator
, &listener
))
488 if (listener
->acquire
&&
489 !listener
->acquire(listener
, reqid
, src_ts
, dst_ts
))
491 this->listeners
->remove_at(this->listeners
, enumerator
);
494 enumerator
->destroy(enumerator
);
495 this->mutex
->unlock(this->mutex
);
498 METHOD(kernel_interface_t
, expire
, void,
499 private_kernel_interface_t
*this, u_int32_t reqid
, u_int8_t protocol
,
500 u_int32_t spi
, bool hard
)
502 kernel_listener_t
*listener
;
503 enumerator_t
*enumerator
;
504 this->mutex
->lock(this->mutex
);
505 enumerator
= this->listeners
->create_enumerator(this->listeners
);
506 while (enumerator
->enumerate(enumerator
, &listener
))
508 if (listener
->expire
&&
509 !listener
->expire(listener
, reqid
, protocol
, spi
, hard
))
511 this->listeners
->remove_at(this->listeners
, enumerator
);
514 enumerator
->destroy(enumerator
);
515 this->mutex
->unlock(this->mutex
);
518 METHOD(kernel_interface_t
, mapping
, void,
519 private_kernel_interface_t
*this, u_int32_t reqid
, u_int32_t spi
,
522 kernel_listener_t
*listener
;
523 enumerator_t
*enumerator
;
524 this->mutex
->lock(this->mutex
);
525 enumerator
= this->listeners
->create_enumerator(this->listeners
);
526 while (enumerator
->enumerate(enumerator
, &listener
))
528 if (listener
->mapping
&&
529 !listener
->mapping(listener
, reqid
, spi
, remote
))
531 this->listeners
->remove_at(this->listeners
, enumerator
);
534 enumerator
->destroy(enumerator
);
535 this->mutex
->unlock(this->mutex
);
538 METHOD(kernel_interface_t
, migrate
, void,
539 private_kernel_interface_t
*this, u_int32_t reqid
,
540 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
541 policy_dir_t direction
, host_t
*local
, host_t
*remote
)
543 kernel_listener_t
*listener
;
544 enumerator_t
*enumerator
;
545 this->mutex
->lock(this->mutex
);
546 enumerator
= this->listeners
->create_enumerator(this->listeners
);
547 while (enumerator
->enumerate(enumerator
, &listener
))
549 if (listener
->migrate
&&
550 !listener
->migrate(listener
, reqid
, src_ts
, dst_ts
, direction
,
553 this->listeners
->remove_at(this->listeners
, enumerator
);
556 enumerator
->destroy(enumerator
);
557 this->mutex
->unlock(this->mutex
);
560 static bool call_roam(kernel_listener_t
*listener
, bool *roam
)
562 return listener
->roam
&& !listener
->roam(listener
, *roam
);
565 METHOD(kernel_interface_t
, roam
, void,
566 private_kernel_interface_t
*this, bool address
)
568 this->mutex
->lock(this->mutex
);
569 this->listeners
->remove(this->listeners
, &address
, (void*)call_roam
);
570 this->mutex
->unlock(this->mutex
);
573 METHOD(kernel_interface_t
, register_algorithm
, void,
574 private_kernel_interface_t
*this, u_int16_t alg_id
, transform_type_t type
,
575 u_int16_t kernel_id
, char *kernel_name
)
577 kernel_algorithm_t
*algorithm
;
583 .name
= strdup(kernel_name
),
586 this->mutex_algs
->lock(this->mutex_algs
);
587 this->algorithms
->insert_first(this->algorithms
, algorithm
);
588 this->mutex_algs
->unlock(this->mutex_algs
);
591 METHOD(kernel_interface_t
, lookup_algorithm
, bool,
592 private_kernel_interface_t
*this, u_int16_t alg_id
, transform_type_t type
,
593 u_int16_t
*kernel_id
, char **kernel_name
)
595 kernel_algorithm_t
*algorithm
;
596 enumerator_t
*enumerator
;
599 this->mutex_algs
->lock(this->mutex_algs
);
600 enumerator
= this->algorithms
->create_enumerator(this->algorithms
);
601 while (enumerator
->enumerate(enumerator
, &algorithm
))
603 if (algorithm
->type
== type
&& algorithm
->ike
== alg_id
)
607 *kernel_id
= algorithm
->kernel
;
611 *kernel_name
= algorithm
->name
;
617 enumerator
->destroy(enumerator
);
618 this->mutex_algs
->unlock(this->mutex_algs
);
622 METHOD(kernel_interface_t
, destroy
, void,
623 private_kernel_interface_t
*this)
625 kernel_algorithm_t
*algorithm
;
627 while (this->algorithms
->remove_first(this->algorithms
,
628 (void**)&algorithm
) == SUCCESS
)
630 free(algorithm
->name
);
633 this->algorithms
->destroy(this->algorithms
);
634 this->mutex_algs
->destroy(this->mutex_algs
);
635 DESTROY_IF(this->ipsec
);
636 DESTROY_IF(this->net
);
637 this->listeners
->destroy(this->listeners
);
638 this->mutex
->destroy(this->mutex
);
643 * Described in header-file
645 kernel_interface_t
*kernel_interface_create()
647 private_kernel_interface_t
*this;
654 .update_sa
= _update_sa
,
655 .query_sa
= _query_sa
,
657 .flush_sas
= _flush_sas
,
658 .add_policy
= _add_policy
,
659 .query_policy
= _query_policy
,
660 .del_policy
= _del_policy
,
661 .flush_policies
= _flush_policies
,
662 .get_source_addr
= _get_source_addr
,
663 .get_nexthop
= _get_nexthop
,
664 .get_interface
= _get_interface
,
665 .create_address_enumerator
= _create_address_enumerator
,
668 .add_route
= _add_route
,
669 .del_route
= _del_route
,
670 .bypass_socket
= _bypass_socket
,
671 .enable_udp_decap
= _enable_udp_decap
,
673 .get_address_by_ts
= _get_address_by_ts
,
674 .add_ipsec_interface
= _add_ipsec_interface
,
675 .remove_ipsec_interface
= _remove_ipsec_interface
,
676 .add_net_interface
= _add_net_interface
,
677 .remove_net_interface
= _remove_net_interface
,
679 .add_listener
= _add_listener
,
680 .remove_listener
= _remove_listener
,
681 .register_algorithm
= _register_algorithm
,
682 .lookup_algorithm
= _lookup_algorithm
,
690 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
691 .listeners
= linked_list_create(),
692 .mutex_algs
= mutex_create(MUTEX_TYPE_DEFAULT
),
693 .algorithms
= linked_list_create(),
696 return &this->public;