kernel: Add option to control DS field behavior
[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 /** Network interface restricting policy */
73 char *interface;
74 /** Lifetime configuration */
75 lifetime_cfg_t *lifetime;
76 /** Encryption algorithm */
77 uint16_t enc_alg;
78 /** Encryption key */
79 chunk_t enc_key;
80 /** Integrity protection algorithm */
81 uint16_t int_alg;
82 /** Integrity protection key */
83 chunk_t int_key;
84 /** Anti-replay window size */
85 uint32_t replay_window;
86 /** Traffic Flow Confidentiality padding */
87 uint32_t tfc;
88 /** IPComp transform */
89 uint16_t ipcomp;
90 /** CPI for IPComp */
91 uint16_t cpi;
92 /** TRUE to enable UDP encapsulation for NAT traversal */
93 bool encap;
94 /** no (disabled), yes (enabled), auto (enabled if supported) */
95 hw_offload_t hw_offload;
96 /** TRUE to use Extended Sequence Numbers */
97 bool esn;
98 /** TRUE to copy the DF bit to the outer IPv4 header in tunnel mode */
99 bool copy_df;
100 /** TRUE to copy the ECN header field to/from the outer header */
101 bool copy_ecn;
102 /** Whether to copy the DSCP header field to/from the outer header */
103 dscp_copy_t copy_dscp;
104 /** TRUE if initiator of the exchange creating the SA */
105 bool initiator;
106 /** TRUE if this is an inbound SA */
107 bool inbound;
108 /** TRUE if an SPI has already been allocated for this SA */
109 bool update;
110 };
111
112 /**
113 * Data required to update the hosts of an SA in the kernel
114 */
115 struct kernel_ipsec_update_sa_t {
116 /** CPI in case IPComp is used */
117 uint16_t cpi;
118 /** New source address */
119 host_t *new_src;
120 /** New destination address */
121 host_t *new_dst;
122 /** TRUE if UDP encapsulation is currently enabled */
123 bool encap;
124 /** TRUE to enable UDP encapsulation */
125 bool new_encap;
126 };
127
128 /**
129 * Data required to query an SA in the kernel
130 */
131 struct kernel_ipsec_query_sa_t {
132 uint16_t cpi;
133 };
134
135 /**
136 * Data required to delete an SA in the kernel
137 */
138 struct kernel_ipsec_del_sa_t {
139 /** CPI in case IPComp is used */
140 uint16_t cpi;
141 };
142
143 /**
144 * Data identifying a policy in the kernel
145 */
146 struct kernel_ipsec_policy_id_t {
147 /** Direction of traffic */
148 policy_dir_t dir;
149 /** Source traffic selector */
150 traffic_selector_t *src_ts;
151 /** Destination traffic selector */
152 traffic_selector_t *dst_ts;
153 /** Optional mark */
154 mark_t mark;
155 /** Network interface restricting policy */
156 char *interface;
157 };
158
159 /**
160 * Data required to add/delete a policy to/from the kernel
161 */
162 struct kernel_ipsec_manage_policy_t {
163 /** Type of policy */
164 policy_type_t type;
165 /** Priority class */
166 policy_priority_t prio;
167 /** Manually-set priority (automatic if set to 0) */
168 uint32_t manual_prio;
169 /** Source address of the SA(s) tied to this policy */
170 host_t *src;
171 /** Destination address of the SA(s) tied to this policy */
172 host_t *dst;
173 /** Details about the SA(s) tied to this policy */
174 ipsec_sa_cfg_t *sa;
175 };
176
177 /**
178 * Data required to query a policy in the kernel
179 */
180 struct kernel_ipsec_query_policy_t {
181 };
182
183 /**
184 * Interface to the ipsec subsystem of the kernel.
185 *
186 * The kernel ipsec interface handles the communication with the kernel
187 * for SA and policy management. It allows setup of these, and provides
188 * further the handling of kernel events.
189 * Policy information are cached in the interface. This is necessary to do
190 * reference counting. The Linux kernel does not allow the same policy
191 * installed twice, but we need this as CHILD_SA exist multiple times
192 * when rekeying. That's why we do reference counting of policies.
193 */
194 struct kernel_ipsec_t {
195
196 /**
197 * Get the feature set supported by this kernel backend.
198 *
199 * @return ORed feature-set of backend
200 */
201 kernel_feature_t (*get_features)(kernel_ipsec_t *this);
202
203 /**
204 * Get a SPI from the kernel.
205 *
206 * @param src source address of SA
207 * @param dst destination address of SA
208 * @param protocol protocol for SA (ESP/AH)
209 * @param spi allocated spi
210 * @return SUCCESS if operation completed
211 */
212 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
213 uint8_t protocol, uint32_t *spi);
214
215 /**
216 * Get a Compression Parameter Index (CPI) from the kernel.
217 *
218 * @param src source address of SA
219 * @param dst destination address of SA
220 * @param cpi allocated cpi
221 * @return SUCCESS if operation completed
222 */
223 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
224 uint16_t *cpi);
225
226 /**
227 * Add an SA to the SAD.
228 *
229 * This function does install a single SA for a single protocol in one
230 * direction.
231 *
232 * @param id data identifying this SA
233 * @param data data for this SA
234 * @return SUCCESS if operation completed
235 */
236 status_t (*add_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
237 kernel_ipsec_add_sa_t *data);
238
239 /**
240 * Update the hosts on an installed SA.
241 *
242 * We cannot directly update the destination address as the kernel
243 * requires the spi, the protocol AND the destination address (and family)
244 * to identify SAs. Therefore if the destination address changed we
245 * create a new SA and delete the old one.
246 *
247 * @param id data identifying this SA
248 * @param data updated data for this SA
249 * @return SUCCESS if operation completed, NOT_SUPPORTED if
250 * the kernel interface can't update the SA
251 */
252 status_t (*update_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
253 kernel_ipsec_update_sa_t *data);
254
255 /**
256 * Query the number of bytes processed by an SA from the SAD.
257 *
258 * @param id data identifying this SA
259 * @param data data to query the SA
260 * @param[out] bytes the number of bytes processed by SA
261 * @param[out] packets number of packets processed by SA
262 * @param[out] time last (monotonic) time of SA use
263 * @return SUCCESS if operation completed
264 */
265 status_t (*query_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
266 kernel_ipsec_query_sa_t *data, uint64_t *bytes,
267 uint64_t *packets, time_t *time);
268
269 /**
270 * Delete a previously installed SA from the SAD.
271 *
272 * @param id data identifying this SA
273 * @param data data to delete the SA
274 * @return SUCCESS if operation completed
275 */
276 status_t (*del_sa)(kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
277 kernel_ipsec_del_sa_t *data);
278
279 /**
280 * Flush all SAs from the SAD.
281 *
282 * @return SUCCESS if operation completed
283 */
284 status_t (*flush_sas)(kernel_ipsec_t *this);
285
286 /**
287 * Add a policy to the SPD.
288 *
289 * @param id data identifying this policy
290 * @param data data for this policy
291 * @return SUCCESS if operation completed
292 */
293 status_t (*add_policy)(kernel_ipsec_t *this,
294 kernel_ipsec_policy_id_t *id,
295 kernel_ipsec_manage_policy_t *data);
296
297 /**
298 * Query the use time of a policy.
299 *
300 * The use time of a policy is the time the policy was used for the last
301 * time. It is not the system time, but a monotonic timestamp as returned
302 * by time_monotonic.
303 *
304 * @param id data identifying this policy
305 * @param data data to query the policy
306 * @param[out] use_time the monotonic timestamp of this SA's last use
307 * @return SUCCESS if operation completed
308 */
309 status_t (*query_policy)(kernel_ipsec_t *this,
310 kernel_ipsec_policy_id_t *id,
311 kernel_ipsec_query_policy_t *data,
312 time_t *use_time);
313
314 /**
315 * Remove a policy from the SPD.
316 *
317 * @param id data identifying this policy
318 * @param data data for this policy
319 * @return SUCCESS if operation completed
320 */
321 status_t (*del_policy)(kernel_ipsec_t *this,
322 kernel_ipsec_policy_id_t *id,
323 kernel_ipsec_manage_policy_t *data);
324
325 /**
326 * Flush all policies from the SPD.
327 *
328 * @return SUCCESS if operation completed
329 */
330 status_t (*flush_policies)(kernel_ipsec_t *this);
331
332 /**
333 * Install a bypass policy for the given socket.
334 *
335 * @param fd socket file descriptor to setup policy for
336 * @param family protocol family of the socket
337 * @return TRUE of policy set up successfully
338 */
339 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
340
341 /**
342 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
343 *
344 * @param fd socket file descriptor
345 * @param family protocol family of the socket
346 * @param port the UDP port
347 * @return TRUE if UDP decapsulation was enabled successfully
348 */
349 bool (*enable_udp_decap)(kernel_ipsec_t *this, int fd, int family,
350 uint16_t port);
351
352 /**
353 * Destroy the implementation.
354 */
355 void (*destroy)(kernel_ipsec_t *this);
356 };
357
358 /**
359 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
360 *
361 * This function is a plugin_feature_callback_t and can be used with the
362 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
363 *
364 * @param plugin plugin registering the kernel interface
365 * @param feature associated plugin feature
366 * @param reg TRUE to register, FALSE to unregister
367 * @param data data passed to callback, an kernel_ipsec_constructor_t
368 */
369 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
370 bool reg, void *data);
371
372 #endif /** KERNEL_IPSEC_H_ @}*/