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