3a06ce66b23477e67e2433d3f57a3662707c9813
[strongswan.git] / src / libcharon / kernel / kernel_ipsec.h
1 /*
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
8 *
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>.
13 *
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
17 * for more details.
18 */
19
20 /**
21 * @defgroup kernel_ipsec kernel_ipsec
22 * @{ @ingroup kernel
23 */
24
25 #ifndef KERNEL_IPSEC_H_
26 #define KERNEL_IPSEC_H_
27
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;
37
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>
43
44 /**
45 * Data required to identify an SA in the kernel
46 */
47 struct kernel_ipsec_sa_id_t {
48 /** Source address */
49 host_t *src;
50 /** Destination address */
51 host_t *dst;
52 /** SPI */
53 uint32_t spi;
54 /** Protocol (ESP/AH) */
55 uint8_t proto;
56 /** Optional mark */
57 mark_t mark;
58 };
59
60 /**
61 * Data required to add an SA to the kernel
62 */
63 struct kernel_ipsec_add_sa_t {
64 /** Reqid */
65 uint32_t reqid;
66 /** Mode (tunnel, transport...) */
67 ipsec_mode_t mode;
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 */
75 uint16_t enc_alg;
76 /** Encryption key */
77 chunk_t enc_key;
78 /** Integrity protection algorithm */
79 uint16_t int_alg;
80 /** Integrity protection key */
81 chunk_t int_key;
82 /** Anti-replay window size */
83 uint32_t replay_window;
84 /** Traffic Flow Confidentiality padding */
85 uint32_t tfc;
86 /** IPComp transform */
87 uint16_t ipcomp;
88 /** CPI for IPComp */
89 uint16_t cpi;
90 /** TRUE to enable UDP encapsulation for NAT traversal */
91 bool encap;
92 /** TRUE to use Extended Sequence Numbers */
93 bool esn;
94 /** TRUE if initiator of the exchange creating the SA */
95 bool initiator;
96 /** TRUE if this is an inbound SA */
97 bool inbound;
98 /** TRUE if an SPI has already been allocated for this SA */
99 bool update;
100 };
101
102 /**
103 * Data required to update the hosts of an SA in the kernel
104 */
105 struct kernel_ipsec_update_sa_t {
106 /** CPI in case IPComp is used */
107 uint16_t cpi;
108 /** New source address */
109 host_t *new_src;
110 /** New destination address */
111 host_t *new_dst;
112 /** TRUE if UDP encapsulation is currently enabled */
113 bool encap;
114 /** TRUE to enable UDP encapsulation */
115 bool new_encap;
116 };
117
118 /**
119 * Data required to query an SA in the kernel
120 */
121 struct kernel_ipsec_query_sa_t {
122 uint16_t cpi;
123 };
124
125 /**
126 * Data required to delete an SA in the kernel
127 */
128 struct kernel_ipsec_del_sa_t {
129 /** CPI in case IPComp is used */
130 uint16_t cpi;
131 };
132
133 /**
134 * Data identifying a policy in the kernel
135 */
136 struct kernel_ipsec_policy_id_t {
137 /** Direction of traffic */
138 policy_dir_t dir;
139 /** Source traffic selector */
140 traffic_selector_t *src_ts;
141 /** Destination traffic selector */
142 traffic_selector_t *dst_ts;
143 /** Optional mark */
144 mark_t mark;
145 };
146
147 /**
148 * Data required to add/delete a policy to/from the kernel
149 */
150 struct kernel_ipsec_manage_policy_t {
151 /** Type of policy */
152 policy_type_t type;
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 */
158 host_t *src;
159 /** Destination address of the SA(s) tied to this policy */
160 host_t *dst;
161 /** Details about the SA(s) tied to this policy */
162 ipsec_sa_cfg_t *sa;
163 };
164
165 /**
166 * Data required to query a policy in the kernel
167 */
168 struct kernel_ipsec_query_policy_t {
169 };
170
171 /**
172 * Interface to the ipsec subsystem of the kernel.
173 *
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.
181 */
182 struct kernel_ipsec_t {
183
184 /**
185 * Get the feature set supported by this kernel backend.
186 *
187 * @return ORed feature-set of backend
188 */
189 kernel_feature_t (*get_features)(kernel_ipsec_t *this);
190
191 /**
192 * Get a SPI from the kernel.
193 *
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
199 */
200 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
201 uint8_t protocol, uint32_t *spi);
202
203 /**
204 * Get a Compression Parameter Index (CPI) from the kernel.
205 *
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
210 */
211 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
212 uint16_t *cpi);
213
214 /**
215 * Add an SA to the SAD.
216 *
217 * This function does install a single SA for a single protocol in one
218 * direction.
219 *
220 * @param id data identifying this SA
221 * @param data data for this SA
222 * @return SUCCESS if operation completed
223 */
224 status_t (*add_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
225 kernel_ipsec_add_sa_t *data);
226
227 /**
228 * Update the hosts on an installed SA.
229 *
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.
234 *
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
239 */
240 status_t (*update_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
241 kernel_ipsec_update_sa_t *data);
242
243 /**
244 * Query the number of bytes processed by an SA from the SAD.
245 *
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
252 */
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);
256
257 /**
258 * Delete a previously installed SA from the SAD.
259 *
260 * @param id data identifying this SA
261 * @param data data to delete the SA
262 * @return SUCCESS if operation completed
263 */
264 status_t (*del_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
265 kernel_ipsec_del_sa_t *data);
266
267 /**
268 * Flush all SAs from the SAD.
269 *
270 * @return SUCCESS if operation completed
271 */
272 status_t (*flush_sas)(kernel_ipsec_t *this);
273
274 /**
275 * Add a policy to the SPD.
276 *
277 * @param id data identifying this policy
278 * @param data data for this policy
279 * @return SUCCESS if operation completed
280 */
281 status_t (*add_policy)(kernel_ipsec_t *this,
282 kernel_ipsec_policy_id_t *id,
283 kernel_ipsec_manage_policy_t *data);
284
285 /**
286 * Query the use time of a policy.
287 *
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
290 * by time_monotonic.
291 *
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
296 */
297 status_t (*query_policy)(kernel_ipsec_t *this,
298 kernel_ipsec_policy_id_t *id,
299 kernel_ipsec_query_policy_t *data,
300 time_t *use_time);
301
302 /**
303 * Remove a policy from the SPD.
304 *
305 * @param id data identifying this policy
306 * @param data data for this policy
307 * @return SUCCESS if operation completed
308 */
309 status_t (*del_policy)(kernel_ipsec_t *this,
310 kernel_ipsec_policy_id_t *id,
311 kernel_ipsec_manage_policy_t *data);
312
313 /**
314 * Flush all policies from the SPD.
315 *
316 * @return SUCCESS if operation completed
317 */
318 status_t (*flush_policies)(kernel_ipsec_t *this);
319
320 /**
321 * Install a bypass policy for the given socket.
322 *
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
326 */
327 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
328
329 /**
330 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
331 *
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
336 */
337 bool (*enable_udp_decap)(kernel_ipsec_t *this, int fd, int family,
338 uint16_t port);
339
340 /**
341 * Destroy the implementation.
342 */
343 void (*destroy)(kernel_ipsec_t *this);
344 };
345
346 /**
347 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
348 *
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.
351 *
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
356 */
357 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
358 bool reg, void *data);
359
360 #endif /** KERNEL_IPSEC_H_ @}*/