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