kernel: Use structs to pass information to the kernel-ipsec interface
[strongswan.git] / src / libcharon / kernel / kernel_ipsec.h
1 /*
2 * Copyright (C) 2006-2016 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * HSR Hochschule fuer Technik Rapperswil
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 * @defgroup kernel_ipsec kernel_ipsec
21 * @{ @ingroup kernel
22 */
23
24 #ifndef KERNEL_IPSEC_H_
25 #define KERNEL_IPSEC_H_
26
27 typedef struct kernel_ipsec_t kernel_ipsec_t;
28 typedef struct kernel_ipsec_sa_id_t kernel_ipsec_sa_id_t;
29 typedef struct kernel_ipsec_add_sa_t kernel_ipsec_add_sa_t;
30 typedef struct kernel_ipsec_update_sa_t kernel_ipsec_update_sa_t;
31 typedef struct kernel_ipsec_query_sa_t kernel_ipsec_query_sa_t;
32 typedef struct kernel_ipsec_del_sa_t kernel_ipsec_del_sa_t;
33 typedef struct kernel_ipsec_policy_id_t kernel_ipsec_policy_id_t;
34 typedef struct kernel_ipsec_manage_policy_t kernel_ipsec_manage_policy_t;
35 typedef struct kernel_ipsec_query_policy_t kernel_ipsec_query_policy_t;
36
37 #include <networking/host.h>
38 #include <ipsec/ipsec_types.h>
39 #include <selectors/traffic_selector.h>
40 #include <plugins/plugin.h>
41 #include <kernel/kernel_interface.h>
42
43 /**
44 * Data required to identify an SA in the kernel
45 */
46 struct kernel_ipsec_sa_id_t {
47 /** Source address */
48 host_t *src;
49 /** Destination address */
50 host_t *dst;
51 /** SPI */
52 uint32_t spi;
53 /** Protocol (ESP/AH) */
54 uint8_t proto;
55 /** Optional mark */
56 mark_t mark;
57 };
58
59 /**
60 * Data required to add an SA to the kernel
61 */
62 struct kernel_ipsec_add_sa_t {
63 /** Reqid */
64 uint32_t reqid;
65 /** Mode (tunnel, transport...) */
66 ipsec_mode_t mode;
67 /** List of source traffic selectors */
68 linked_list_t *src_ts;
69 /** List of destination traffic selectors */
70 linked_list_t *dst_ts;
71 /** Lifetime configuration */
72 lifetime_cfg_t *lifetime;
73 /** Encryption algorithm */
74 uint16_t enc_alg;
75 /** Encryption key */
76 chunk_t enc_key;
77 /** Integrity protection algorithm */
78 uint16_t int_alg;
79 /** Integrity protection key */
80 chunk_t int_key;
81 /** Anti-replay window size */
82 uint32_t replay_window;
83 /** Traffic Flow Confidentiality padding */
84 uint32_t tfc;
85 /** IPComp transform */
86 uint16_t ipcomp;
87 /** CPI for IPComp */
88 uint16_t cpi;
89 /** TRUE to enable UDP encapsulation for NAT traversal */
90 bool encap;
91 /** TRUE to use Extended Sequence Numbers */
92 bool esn;
93 /** TRUE if initiator of the exchange creating the SA */
94 bool initiator;
95 /** TRUE if this is an inbound SA */
96 bool inbound;
97 /** TRUE if an SPI has already been allocated for this SA */
98 bool update;
99 };
100
101 /**
102 * Data required to update the hosts of an SA in the kernel
103 */
104 struct kernel_ipsec_update_sa_t {
105 /** CPI in case IPComp is used */
106 uint16_t cpi;
107 /** New source address */
108 host_t *new_src;
109 /** New destination address */
110 host_t *new_dst;
111 /** TRUE if UDP encapsulation is currently enabled */
112 bool encap;
113 /** TRUE to enable UDP encapsulation */
114 bool new_encap;
115 };
116
117 /**
118 * Data required to query an SA in the kernel
119 */
120 struct kernel_ipsec_query_sa_t {
121 uint16_t cpi;
122 };
123
124 /**
125 * Data required to delete an SA in the kernel
126 */
127 struct kernel_ipsec_del_sa_t {
128 /** CPI in case IPComp is used */
129 uint16_t cpi;
130 };
131
132 /**
133 * Data identifying a policy in the kernel
134 */
135 struct kernel_ipsec_policy_id_t {
136 /** Direction of traffic */
137 policy_dir_t dir;
138 /** Source traffic selector */
139 traffic_selector_t *src_ts;
140 /** Destination traffic selector */
141 traffic_selector_t *dst_ts;
142 /** Optional mark */
143 mark_t mark;
144 };
145
146 /**
147 * Data required to add/delete a policy to/from the kernel
148 */
149 struct kernel_ipsec_manage_policy_t {
150 /** Type of policy */
151 policy_type_t type;
152 /** Priority class */
153 policy_priority_t prio;
154 /** Source address of the SA(s) tied to this policy */
155 host_t *src;
156 /** Destination address of the SA(s) tied to this policy */
157 host_t *dst;
158 /** Details about the SA(s) tied to this policy */
159 ipsec_sa_cfg_t *sa;
160 };
161
162 /**
163 * Data required to query a policy in the kernel
164 */
165 struct kernel_ipsec_query_policy_t {
166 };
167
168 /**
169 * Interface to the ipsec subsystem of the kernel.
170 *
171 * The kernel ipsec interface handles the communication with the kernel
172 * for SA and policy management. It allows setup of these, and provides
173 * further the handling of kernel events.
174 * Policy information are cached in the interface. This is necessary to do
175 * reference counting. The Linux kernel does not allow the same policy
176 * installed twice, but we need this as CHILD_SA exist multiple times
177 * when rekeying. Thats why we do reference counting of policies.
178 */
179 struct kernel_ipsec_t {
180
181 /**
182 * Get the feature set supported by this kernel backend.
183 *
184 * @return ORed feature-set of backend
185 */
186 kernel_feature_t (*get_features)(kernel_ipsec_t *this);
187
188 /**
189 * Get a SPI from the kernel.
190 *
191 * @param src source address of SA
192 * @param dst destination address of SA
193 * @param protocol protocol for SA (ESP/AH)
194 * @param spi allocated spi
195 * @return SUCCESS if operation completed
196 */
197 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
198 uint8_t protocol, uint32_t *spi);
199
200 /**
201 * Get a Compression Parameter Index (CPI) from the kernel.
202 *
203 * @param src source address of SA
204 * @param dst destination address of SA
205 * @param cpi allocated cpi
206 * @return SUCCESS if operation completed
207 */
208 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
209 uint16_t *cpi);
210
211 /**
212 * Add an SA to the SAD.
213 *
214 * This function does install a single SA for a single protocol in one
215 * direction.
216 *
217 * @param id data identifying this SA
218 * @param data data for this SA
219 * @return SUCCESS if operation completed
220 */
221 status_t (*add_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
222 kernel_ipsec_add_sa_t *data);
223
224 /**
225 * Update the hosts on an installed SA.
226 *
227 * We cannot directly update the destination address as the kernel
228 * requires the spi, the protocol AND the destination address (and family)
229 * to identify SAs. Therefore if the destination address changed we
230 * create a new SA and delete the old one.
231 *
232 * @param id data identifying this SA
233 * @param data updated data for this SA
234 * @return SUCCESS if operation completed, NOT_SUPPORTED if
235 * the kernel interface can't update the SA
236 */
237 status_t (*update_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
238 kernel_ipsec_update_sa_t *data);
239
240 /**
241 * Query the number of bytes processed by an SA from the SAD.
242 *
243 * @param id data identifying this SA
244 * @param data data to query the SA
245 * @param[out] bytes the number of bytes processed by SA
246 * @param[out] packets number of packets processed by SA
247 * @param[out] time last (monotonic) time of SA use
248 * @return SUCCESS if operation completed
249 */
250 status_t (*query_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
251 kernel_ipsec_query_sa_t *data, uint64_t *bytes,
252 uint64_t *packets, time_t *time);
253
254 /**
255 * Delete a previously installed SA from the SAD.
256 *
257 * @param id data identifying this SA
258 * @param data data to delete the SA
259 * @return SUCCESS if operation completed
260 */
261 status_t (*del_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
262 kernel_ipsec_del_sa_t *data);
263
264 /**
265 * Flush all SAs from the SAD.
266 *
267 * @return SUCCESS if operation completed
268 */
269 status_t (*flush_sas)(kernel_ipsec_t *this);
270
271 /**
272 * Add a policy to the SPD.
273 *
274 * @param id data identifying this policy
275 * @param data data for this policy
276 * @return SUCCESS if operation completed
277 */
278 status_t (*add_policy)(kernel_ipsec_t *this,
279 kernel_ipsec_policy_id_t *id,
280 kernel_ipsec_manage_policy_t *data);
281
282 /**
283 * Query the use time of a policy.
284 *
285 * The use time of a policy is the time the policy was used for the last
286 * time. It is not the system time, but a monotonic timestamp as returned
287 * by time_monotonic.
288 *
289 * @param id data identifying this policy
290 * @param data data to query the policy
291 * @param[out] use_time the monotonic timestamp of this SA's last use
292 * @return SUCCESS if operation completed
293 */
294 status_t (*query_policy)(kernel_ipsec_t *this,
295 kernel_ipsec_policy_id_t *id,
296 kernel_ipsec_query_policy_t *data,
297 time_t *use_time);
298
299 /**
300 * Remove a policy from the SPD.
301 *
302 * @param id data identifying this policy
303 * @param data data for this policy
304 * @return SUCCESS if operation completed
305 */
306 status_t (*del_policy)(kernel_ipsec_t *this,
307 kernel_ipsec_policy_id_t *id,
308 kernel_ipsec_manage_policy_t *data);
309
310 /**
311 * Flush all policies from the SPD.
312 *
313 * @return SUCCESS if operation completed
314 */
315 status_t (*flush_policies)(kernel_ipsec_t *this);
316
317 /**
318 * Install a bypass policy for the given socket.
319 *
320 * @param fd socket file descriptor to setup policy for
321 * @param family protocol family of the socket
322 * @return TRUE of policy set up successfully
323 */
324 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
325
326 /**
327 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
328 *
329 * @param fd socket file descriptor
330 * @param family protocol family of the socket
331 * @param port the UDP port
332 * @return TRUE if UDP decapsulation was enabled successfully
333 */
334 bool (*enable_udp_decap)(kernel_ipsec_t *this, int fd, int family,
335 uint16_t port);
336
337 /**
338 * Destroy the implementation.
339 */
340 void (*destroy)(kernel_ipsec_t *this);
341 };
342
343 /**
344 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
345 *
346 * This function is a plugin_feature_callback_t and can be used with the
347 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
348 *
349 * @param plugin plugin registering the kernel interface
350 * @param feature associated plugin feature
351 * @param reg TRUE to register, FALSE to unregister
352 * @param data data passed to callback, an kernel_ipsec_constructor_t
353 */
354 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
355 bool reg, void *data);
356
357 #endif /** KERNEL_IPSEC_H_ @}*/