ddb63283c8cf6bd1420b18dfc6ec389d916b38dc
[strongswan.git] / src / libhydra / kernel / kernel_ipsec.h
1 /*
2 * Copyright (C) 2006-2011 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * 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 hkernel
22 */
23
24 #ifndef KERNEL_IPSEC_H_
25 #define KERNEL_IPSEC_H_
26
27 typedef enum ipsec_mode_t ipsec_mode_t;
28 typedef enum policy_dir_t policy_dir_t;
29 typedef enum policy_type_t policy_type_t;
30 typedef enum policy_priority_t policy_priority_t;
31 typedef enum ipcomp_transform_t ipcomp_transform_t;
32 typedef struct kernel_ipsec_t kernel_ipsec_t;
33 typedef struct ipsec_sa_cfg_t ipsec_sa_cfg_t;
34 typedef struct lifetime_cfg_t lifetime_cfg_t;
35 typedef struct mark_t mark_t;
36
37 #include <utils/host.h>
38 #include <crypto/prf_plus.h>
39 #include <selectors/traffic_selector.h>
40 #include <plugins/plugin.h>
41
42 /**
43 * Mode of an IPsec SA.
44 */
45 enum ipsec_mode_t {
46 /** transport mode, no inner address */
47 MODE_TRANSPORT = 1,
48 /** tunnel mode, inner and outer addresses */
49 MODE_TUNNEL,
50 /** BEET mode, tunnel mode but fixed, bound inner addresses */
51 MODE_BEET,
52 /** passthrough policy for traffic without an IPsec SA */
53 MODE_PASS,
54 /** drop policy discarding traffic */
55 MODE_DROP
56 };
57
58 /**
59 * enum names for ipsec_mode_t.
60 */
61 extern enum_name_t *ipsec_mode_names;
62
63 /**
64 * Direction of a policy. These are equal to those
65 * defined in xfrm.h, but we want to stay implementation
66 * neutral here.
67 */
68 enum policy_dir_t {
69 /** Policy for inbound traffic */
70 POLICY_IN = 0,
71 /** Policy for outbound traffic */
72 POLICY_OUT = 1,
73 /** Policy for forwarded traffic */
74 POLICY_FWD = 2,
75 };
76
77 /**
78 * enum names for policy_dir_t.
79 */
80 extern enum_name_t *policy_dir_names;
81
82 /**
83 * Type of a policy.
84 */
85 enum policy_type_t {
86 /** Normal IPsec policy */
87 POLICY_IPSEC = 1,
88 /** Passthrough policy (traffic is ignored by IPsec) */
89 POLICY_PASS,
90 /** Drop policy (traffic is discarded) */
91 POLICY_DROP,
92 };
93
94 /**
95 * High-level priority of a policy.
96 */
97 enum policy_priority_t {
98 /** Default priority */
99 POLICY_PRIORITY_DEFAULT,
100 /** Priority for trap policies */
101 POLICY_PRIORITY_ROUTED,
102 /** Priority for fallback drop policies */
103 POLICY_PRIORITY_FALLBACK,
104 };
105
106 /**
107 * IPComp transform IDs, as in RFC 4306
108 */
109 enum ipcomp_transform_t {
110 IPCOMP_NONE = 0,
111 IPCOMP_OUI = 1,
112 IPCOMP_DEFLATE = 2,
113 IPCOMP_LZS = 3,
114 IPCOMP_LZJH = 4,
115 };
116
117 /**
118 * enum strings for ipcomp_transform_t.
119 */
120 extern enum_name_t *ipcomp_transform_names;
121
122 /**
123 * This struct contains details about IPsec SA(s) tied to a policy.
124 */
125 struct ipsec_sa_cfg_t {
126 /** mode of SA (tunnel, transport) */
127 ipsec_mode_t mode;
128 /** unique ID */
129 u_int32_t reqid;
130 /** details about ESP/AH */
131 struct {
132 /** TRUE if this protocol is used */
133 bool use;
134 /** SPI for ESP/AH */
135 u_int32_t spi;
136 } esp, ah;
137 /** details about IPComp */
138 struct {
139 /** the IPComp transform used */
140 u_int16_t transform;
141 /** CPI for IPComp */
142 u_int16_t cpi;
143 } ipcomp;
144 };
145
146 /**
147 * A lifetime_cfg_t defines the lifetime limits of an SA.
148 *
149 * Set any of these values to 0 to ignore.
150 */
151 struct lifetime_cfg_t {
152 struct {
153 /** Limit before the SA gets invalid. */
154 u_int64_t life;
155 /** Limit before the SA gets rekeyed. */
156 u_int64_t rekey;
157 /** The range of a random value subtracted from rekey. */
158 u_int64_t jitter;
159 } time, bytes, packets;
160 };
161
162 /**
163 * A mark_t defines an optional mark in an IPsec SA.
164 */
165 struct mark_t {
166 /** Mark value */
167 u_int32_t value;
168 /** Mark mask */
169 u_int32_t mask;
170 };
171
172 /**
173 * Interface to the ipsec subsystem of the kernel.
174 *
175 * The kernel ipsec interface handles the communication with the kernel
176 * for SA and policy management. It allows setup of these, and provides
177 * further the handling of kernel events.
178 * Policy information are cached in the interface. This is necessary to do
179 * reference counting. The Linux kernel does not allow the same policy
180 * installed twice, but we need this as CHILD_SA exist multiple times
181 * when rekeying. Thats why we do reference counting of policies.
182 */
183 struct kernel_ipsec_t {
184
185 /**
186 * Get a SPI from the kernel.
187 *
188 * @param src source address of SA
189 * @param dst destination address of SA
190 * @param protocol protocol for SA (ESP/AH)
191 * @param reqid unique ID for this SA
192 * @param spi allocated spi
193 * @return SUCCESS if operation completed
194 */
195 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
196 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi);
197
198 /**
199 * Get a Compression Parameter Index (CPI) from the kernel.
200 *
201 * @param src source address of SA
202 * @param dst destination address of SA
203 * @param reqid unique ID for the corresponding SA
204 * @param cpi allocated cpi
205 * @return SUCCESS if operation completed
206 */
207 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
208 u_int32_t reqid, u_int16_t *cpi);
209
210 /**
211 * Add an SA to the SAD.
212 *
213 * add_sa() may update an already allocated
214 * SPI (via get_spi). In this case, the replace
215 * flag must be set.
216 * This function does install a single SA for a
217 * single protocol in one direction.
218 *
219 * @param src source address for this SA
220 * @param dst destination address for this SA
221 * @param spi SPI allocated by us or remote peer
222 * @param protocol protocol for this SA (ESP/AH)
223 * @param reqid unique ID for this SA
224 * @param mark mark for this SA
225 * @param tfc Traffic Flow Confidentiality padding for this SA
226 * @param lifetime lifetime_cfg_t for this SA
227 * @param enc_alg Algorithm to use for encryption (ESP only)
228 * @param enc_key key to use for encryption
229 * @param int_alg Algorithm to use for integrity protection
230 * @param int_key key to use for integrity protection
231 * @param mode mode of the SA (tunnel, transport)
232 * @param ipcomp IPComp transform to use
233 * @param cpi CPI for IPComp
234 * @param encap enable UDP encapsulation for NAT traversal
235 * @param esn TRUE to use Extended Sequence Numbers
236 * @param inbound TRUE if this is an inbound SA
237 * @param src_ts traffic selector with BEET source address
238 * @param dst_ts traffic selector with BEET destination address
239 * @return SUCCESS if operation completed
240 */
241 status_t (*add_sa) (kernel_ipsec_t *this,
242 host_t *src, host_t *dst, u_int32_t spi,
243 u_int8_t protocol, u_int32_t reqid,
244 mark_t mark, u_int32_t tfc, lifetime_cfg_t *lifetime,
245 u_int16_t enc_alg, chunk_t enc_key,
246 u_int16_t int_alg, chunk_t int_key,
247 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
248 bool encap, bool esn, bool inbound,
249 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
250
251 /**
252 * Update the hosts on an installed SA.
253 *
254 * We cannot directly update the destination address as the kernel
255 * requires the spi, the protocol AND the destination address (and family)
256 * to identify SAs. Therefore if the destination address changed we
257 * create a new SA and delete the old one.
258 *
259 * @param spi SPI of the SA
260 * @param protocol protocol for this SA (ESP/AH)
261 * @param cpi CPI for IPComp, 0 if no IPComp is used
262 * @param src current source address
263 * @param dst current destination address
264 * @param new_src new source address
265 * @param new_dst new destination address
266 * @param encap current use of UDP encapsulation
267 * @param new_encap new use of UDP encapsulation
268 * @param mark optional mark for this SA
269 * @return SUCCESS if operation completed, NOT_SUPPORTED if
270 * the kernel interface can't update the SA
271 */
272 status_t (*update_sa)(kernel_ipsec_t *this,
273 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
274 host_t *src, host_t *dst,
275 host_t *new_src, host_t *new_dst,
276 bool encap, bool new_encap, mark_t mark);
277
278 /**
279 * Query the number of bytes processed by an SA from the SAD.
280 *
281 * @param src source address for this SA
282 * @param dst destination address for this SA
283 * @param spi SPI allocated by us or remote peer
284 * @param protocol protocol for this SA (ESP/AH)
285 * @param mark optional mark for this SA
286 * @param[out] bytes the number of bytes processed by SA
287 * @return SUCCESS if operation completed
288 */
289 status_t (*query_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
290 u_int32_t spi, u_int8_t protocol, mark_t mark,
291 u_int64_t *bytes);
292
293 /**
294 * Delete a previusly installed SA from the SAD.
295 *
296 * @param src source address for this SA
297 * @param dst destination address for this SA
298 * @param spi SPI allocated by us or remote peer
299 * @param protocol protocol for this SA (ESP/AH)
300 * @param cpi CPI for IPComp or 0
301 * @param mark optional mark for this SA
302 * @return SUCCESS if operation completed
303 */
304 status_t (*del_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
305 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
306 mark_t mark);
307
308 /**
309 * Flush all SAs from the SAD.
310 *
311 * @return SUCCESS if operation completed
312 */
313 status_t (*flush_sas) (kernel_ipsec_t *this);
314
315 /**
316 * Add a policy to the SPD.
317 *
318 * A policy is always associated to an SA. Traffic which matches a
319 * policy is handled by the SA with the same reqid.
320 *
321 * @param src source address of SA
322 * @param dst dest address of SA
323 * @param src_ts traffic selector to match traffic source
324 * @param dst_ts traffic selector to match traffic dest
325 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
326 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
327 * @param sa details about the SA(s) tied to this policy
328 * @param mark mark for this policy
329 * @param priority priority of this policy
330 * @return SUCCESS if operation completed
331 */
332 status_t (*add_policy) (kernel_ipsec_t *this,
333 host_t *src, host_t *dst,
334 traffic_selector_t *src_ts,
335 traffic_selector_t *dst_ts,
336 policy_dir_t direction, policy_type_t type,
337 ipsec_sa_cfg_t *sa, mark_t mark,
338 policy_priority_t priority);
339
340 /**
341 * Query the use time of a policy.
342 *
343 * The use time of a policy is the time the policy was used for the last
344 * time. It is not the system time, but a monotonic timestamp as returned
345 * by time_monotonic.
346 *
347 * @param src_ts traffic selector to match traffic source
348 * @param dst_ts traffic selector to match traffic dest
349 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
350 * @param mark optional mark
351 * @param[out] use_time the monotonic timestamp of this SA's last use
352 * @return SUCCESS if operation completed
353 */
354 status_t (*query_policy) (kernel_ipsec_t *this,
355 traffic_selector_t *src_ts,
356 traffic_selector_t *dst_ts,
357 policy_dir_t direction, mark_t mark,
358 u_int32_t *use_time);
359
360 /**
361 * Remove a policy from the SPD.
362 *
363 * The kernel interface implements reference counting for policies.
364 * If the same policy is installed multiple times (in the case of rekeying),
365 * the reference counter is increased. del_policy() decreases the ref counter
366 * and removes the policy only when no more references are available.
367 *
368 * @param src_ts traffic selector to match traffic source
369 * @param dst_ts traffic selector to match traffic dest
370 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
371 * @param reqid unique ID of the associated SA
372 * @param mark optional mark
373 * @param priority priority of the policy
374 * @return SUCCESS if operation completed
375 */
376 status_t (*del_policy) (kernel_ipsec_t *this,
377 traffic_selector_t *src_ts,
378 traffic_selector_t *dst_ts,
379 policy_dir_t direction, u_int32_t reqid,
380 mark_t mark, policy_priority_t priority);
381
382 /**
383 * Flush all policies from the SPD.
384 *
385 * @return SUCCESS if operation completed
386 */
387 status_t (*flush_policies) (kernel_ipsec_t *this);
388
389 /**
390 * Install a bypass policy for the given socket.
391 *
392 * @param fd socket file descriptor to setup policy for
393 * @param family protocol family of the socket
394 * @return TRUE of policy set up successfully
395 */
396 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
397
398 /**
399 * Destroy the implementation.
400 */
401 void (*destroy) (kernel_ipsec_t *this);
402 };
403
404 /**
405 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
406 *
407 * This function is a plugin_feature_callback_t and can be used with the
408 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
409 *
410 * @param plugin plugin registering the kernel interface
411 * @param feature associated plugin feature
412 * @param reg TRUE to register, FALSE to unregister
413 * @param data data passed to callback, an kernel_ipsec_constructor_t
414 */
415 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
416 bool reg, void *data);
417
418 #endif /** KERNEL_IPSEC_H_ @}*/