2 * Copyright (C) 2016 Andreas Steffen
3 * Copyright (C) 2006-2016 Tobias Brunner
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005-2006 Martin Willi
6 * Copyright (C) 2005 Jan Hutter
7 * HSR Hochschule fuer Technik Rapperswil
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * @defgroup kernel_ipsec kernel_ipsec
25 #ifndef KERNEL_IPSEC_H_
26 #define KERNEL_IPSEC_H_
28 typedef struct kernel_ipsec_t kernel_ipsec_t
;
29 typedef struct kernel_ipsec_sa_id_t kernel_ipsec_sa_id_t
;
30 typedef struct kernel_ipsec_add_sa_t kernel_ipsec_add_sa_t
;
31 typedef struct kernel_ipsec_update_sa_t kernel_ipsec_update_sa_t
;
32 typedef struct kernel_ipsec_query_sa_t kernel_ipsec_query_sa_t
;
33 typedef struct kernel_ipsec_del_sa_t kernel_ipsec_del_sa_t
;
34 typedef struct kernel_ipsec_policy_id_t kernel_ipsec_policy_id_t
;
35 typedef struct kernel_ipsec_manage_policy_t kernel_ipsec_manage_policy_t
;
36 typedef struct kernel_ipsec_query_policy_t kernel_ipsec_query_policy_t
;
38 #include <networking/host.h>
39 #include <ipsec/ipsec_types.h>
40 #include <selectors/traffic_selector.h>
41 #include <plugins/plugin.h>
42 #include <kernel/kernel_interface.h>
45 * Data required to identify an SA in the kernel
47 struct kernel_ipsec_sa_id_t
{
50 /** Destination address */
54 /** Protocol (ESP/AH) */
61 * Data required to add an SA to the kernel
63 struct kernel_ipsec_add_sa_t
{
66 /** Mode (tunnel, transport...) */
68 /** List of source traffic selectors */
69 linked_list_t
*src_ts
;
70 /** List of destination traffic selectors */
71 linked_list_t
*dst_ts
;
72 /** Lifetime configuration */
73 lifetime_cfg_t
*lifetime
;
74 /** Encryption algorithm */
78 /** Integrity protection algorithm */
80 /** Integrity protection key */
82 /** Anti-replay window size */
83 uint32_t replay_window
;
84 /** Traffic Flow Confidentiality padding */
86 /** IPComp transform */
90 /** TRUE to enable UDP encapsulation for NAT traversal */
92 /** TRUE to use Extended Sequence Numbers */
94 /** TRUE if initiator of the exchange creating the SA */
96 /** TRUE if this is an inbound SA */
98 /** TRUE if an SPI has already been allocated for this SA */
103 * Data required to update the hosts of an SA in the kernel
105 struct kernel_ipsec_update_sa_t
{
106 /** CPI in case IPComp is used */
108 /** New source address */
110 /** New destination address */
112 /** TRUE if UDP encapsulation is currently enabled */
114 /** TRUE to enable UDP encapsulation */
119 * Data required to query an SA in the kernel
121 struct kernel_ipsec_query_sa_t
{
126 * Data required to delete an SA in the kernel
128 struct kernel_ipsec_del_sa_t
{
129 /** CPI in case IPComp is used */
134 * Data identifying a policy in the kernel
136 struct kernel_ipsec_policy_id_t
{
137 /** Direction of traffic */
139 /** Source traffic selector */
140 traffic_selector_t
*src_ts
;
141 /** Destination traffic selector */
142 traffic_selector_t
*dst_ts
;
148 * Data required to add/delete a policy to/from the kernel
150 struct kernel_ipsec_manage_policy_t
{
151 /** Type of policy */
153 /** Priority class */
154 policy_priority_t prio
;
155 /** Manually-set priority (automatic if set to 0) */
156 uint32_t manual_prio
;
157 /** Source address of the SA(s) tied to this policy */
159 /** Destination address of the SA(s) tied to this policy */
161 /** Details about the SA(s) tied to this policy */
166 * Data required to query a policy in the kernel
168 struct kernel_ipsec_query_policy_t
{
172 * Interface to the ipsec subsystem of the kernel.
174 * The kernel ipsec interface handles the communication with the kernel
175 * for SA and policy management. It allows setup of these, and provides
176 * further the handling of kernel events.
177 * Policy information are cached in the interface. This is necessary to do
178 * reference counting. The Linux kernel does not allow the same policy
179 * installed twice, but we need this as CHILD_SA exist multiple times
180 * when rekeying. Thats why we do reference counting of policies.
182 struct kernel_ipsec_t
{
185 * Get the feature set supported by this kernel backend.
187 * @return ORed feature-set of backend
189 kernel_feature_t (*get_features
)(kernel_ipsec_t
*this);
192 * Get a SPI from the kernel.
194 * @param src source address of SA
195 * @param dst destination address of SA
196 * @param protocol protocol for SA (ESP/AH)
197 * @param spi allocated spi
198 * @return SUCCESS if operation completed
200 status_t (*get_spi
)(kernel_ipsec_t
*this, host_t
*src
, host_t
*dst
,
201 uint8_t protocol
, uint32_t *spi
);
204 * Get a Compression Parameter Index (CPI) from the kernel.
206 * @param src source address of SA
207 * @param dst destination address of SA
208 * @param cpi allocated cpi
209 * @return SUCCESS if operation completed
211 status_t (*get_cpi
)(kernel_ipsec_t
*this, host_t
*src
, host_t
*dst
,
215 * Add an SA to the SAD.
217 * This function does install a single SA for a single protocol in one
220 * @param id data identifying this SA
221 * @param data data for this SA
222 * @return SUCCESS if operation completed
224 status_t (*add_sa
)(kernel_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
225 kernel_ipsec_add_sa_t
*data
);
228 * Update the hosts on an installed SA.
230 * We cannot directly update the destination address as the kernel
231 * requires the spi, the protocol AND the destination address (and family)
232 * to identify SAs. Therefore if the destination address changed we
233 * create a new SA and delete the old one.
235 * @param id data identifying this SA
236 * @param data updated data for this SA
237 * @return SUCCESS if operation completed, NOT_SUPPORTED if
238 * the kernel interface can't update the SA
240 status_t (*update_sa
)(kernel_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
241 kernel_ipsec_update_sa_t
*data
);
244 * Query the number of bytes processed by an SA from the SAD.
246 * @param id data identifying this SA
247 * @param data data to query the SA
248 * @param[out] bytes the number of bytes processed by SA
249 * @param[out] packets number of packets processed by SA
250 * @param[out] time last (monotonic) time of SA use
251 * @return SUCCESS if operation completed
253 status_t (*query_sa
)(kernel_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
254 kernel_ipsec_query_sa_t
*data
, uint64_t *bytes
,
255 uint64_t *packets
, time_t *time
);
258 * Delete a previously installed SA from the SAD.
260 * @param id data identifying this SA
261 * @param data data to delete the SA
262 * @return SUCCESS if operation completed
264 status_t (*del_sa
)(kernel_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
265 kernel_ipsec_del_sa_t
*data
);
268 * Flush all SAs from the SAD.
270 * @return SUCCESS if operation completed
272 status_t (*flush_sas
)(kernel_ipsec_t
*this);
275 * Add a policy to the SPD.
277 * @param id data identifying this policy
278 * @param data data for this policy
279 * @return SUCCESS if operation completed
281 status_t (*add_policy
)(kernel_ipsec_t
*this,
282 kernel_ipsec_policy_id_t
*id
,
283 kernel_ipsec_manage_policy_t
*data
);
286 * Query the use time of a policy.
288 * The use time of a policy is the time the policy was used for the last
289 * time. It is not the system time, but a monotonic timestamp as returned
292 * @param id data identifying this policy
293 * @param data data to query the policy
294 * @param[out] use_time the monotonic timestamp of this SA's last use
295 * @return SUCCESS if operation completed
297 status_t (*query_policy
)(kernel_ipsec_t
*this,
298 kernel_ipsec_policy_id_t
*id
,
299 kernel_ipsec_query_policy_t
*data
,
303 * Remove a policy from the SPD.
305 * @param id data identifying this policy
306 * @param data data for this policy
307 * @return SUCCESS if operation completed
309 status_t (*del_policy
)(kernel_ipsec_t
*this,
310 kernel_ipsec_policy_id_t
*id
,
311 kernel_ipsec_manage_policy_t
*data
);
314 * Flush all policies from the SPD.
316 * @return SUCCESS if operation completed
318 status_t (*flush_policies
)(kernel_ipsec_t
*this);
321 * Install a bypass policy for the given socket.
323 * @param fd socket file descriptor to setup policy for
324 * @param family protocol family of the socket
325 * @return TRUE of policy set up successfully
327 bool (*bypass_socket
)(kernel_ipsec_t
*this, int fd
, int family
);
330 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
332 * @param fd socket file descriptor
333 * @param family protocol family of the socket
334 * @param port the UDP port
335 * @return TRUE if UDP decapsulation was enabled successfully
337 bool (*enable_udp_decap
)(kernel_ipsec_t
*this, int fd
, int family
,
341 * Destroy the implementation.
343 void (*destroy
)(kernel_ipsec_t
*this);
347 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
349 * This function is a plugin_feature_callback_t and can be used with the
350 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
352 * @param plugin plugin registering the kernel interface
353 * @param feature associated plugin feature
354 * @param reg TRUE to register, FALSE to unregister
355 * @param data data passed to callback, an kernel_ipsec_constructor_t
357 bool kernel_ipsec_register(plugin_t
*plugin
, plugin_feature_t
*feature
,
358 bool reg
, void *data
);
360 #endif /** KERNEL_IPSEC_H_ @}*/