Implemented Traffic Flow Confidentiality padding in kernel_interface
[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 tfc Traffic Flow Confidentiality padding for this SA
208 * @param lifetime lifetime_cfg_t for this SA
209 * @param enc_alg Algorithm to use for encryption (ESP only)
210 * @param enc_key key to use for encryption
211 * @param int_alg Algorithm to use for integrity protection
212 * @param int_key key to use for integrity protection
213 * @param mode mode of the SA (tunnel, transport)
214 * @param ipcomp IPComp transform to use
215 * @param cpi CPI for IPComp
216 * @param encap enable UDP encapsulation for NAT traversal
217 * @param inbound TRUE if this is an inbound SA
218 * @param src_ts traffic selector with BEET source address
219 * @param dst_ts traffic selector with BEET destination address
220 * @return SUCCESS if operation completed
221 */
222 status_t (*add_sa) (kernel_ipsec_t *this,
223 host_t *src, host_t *dst, u_int32_t spi,
224 u_int8_t protocol, u_int32_t reqid,
225 mark_t mark, u_int32_t tfc, lifetime_cfg_t *lifetime,
226 u_int16_t enc_alg, chunk_t enc_key,
227 u_int16_t int_alg, chunk_t int_key,
228 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
229 bool encap, bool inbound,
230 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
231
232 /**
233 * Update the hosts on an installed SA.
234 *
235 * We cannot directly update the destination address as the kernel
236 * requires the spi, the protocol AND the destination address (and family)
237 * to identify SAs. Therefore if the destination address changed we
238 * create a new SA and delete the old one.
239 *
240 * @param spi SPI of the SA
241 * @param protocol protocol for this SA (ESP/AH)
242 * @param cpi CPI for IPComp, 0 if no IPComp is used
243 * @param src current source address
244 * @param dst current destination address
245 * @param new_src new source address
246 * @param new_dst new destination address
247 * @param encap current use of UDP encapsulation
248 * @param new_encap new use of UDP encapsulation
249 * @param mark optional mark for this SA
250 * @return SUCCESS if operation completed, NOT_SUPPORTED if
251 * the kernel interface can't update the SA
252 */
253 status_t (*update_sa)(kernel_ipsec_t *this,
254 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
255 host_t *src, host_t *dst,
256 host_t *new_src, host_t *new_dst,
257 bool encap, bool new_encap, mark_t mark);
258
259 /**
260 * Query the number of bytes processed by an SA from the SAD.
261 *
262 * @param src source address for this SA
263 * @param dst destination address for this SA
264 * @param spi SPI allocated by us or remote peer
265 * @param protocol protocol for this SA (ESP/AH)
266 * @param mark optional mark for this SA
267 * @param[out] bytes the number of bytes processed by SA
268 * @return SUCCESS if operation completed
269 */
270 status_t (*query_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
271 u_int32_t spi, u_int8_t protocol, mark_t mark,
272 u_int64_t *bytes);
273
274 /**
275 * Delete a previusly installed SA from the SAD.
276 *
277 * @param src source address for this SA
278 * @param dst destination address for this SA
279 * @param spi SPI allocated by us or remote peer
280 * @param protocol protocol for this SA (ESP/AH)
281 * @param cpi CPI for IPComp or 0
282 * @param mark optional mark for this SA
283 * @return SUCCESS if operation completed
284 */
285 status_t (*del_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
286 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
287 mark_t mark);
288
289 /**
290 * Add a policy to the SPD.
291 *
292 * A policy is always associated to an SA. Traffic which matches a
293 * policy is handled by the SA with the same reqid.
294 *
295 * @param src source address of SA
296 * @param dst dest address of SA
297 * @param src_ts traffic selector to match traffic source
298 * @param dst_ts traffic selector to match traffic dest
299 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
300 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
301 * @param sa details about the SA(s) tied to this policy
302 * @param mark mark for this policy
303 * @param routed TRUE, if this policy is routed in the kernel
304 * @return SUCCESS if operation completed
305 */
306 status_t (*add_policy) (kernel_ipsec_t *this,
307 host_t *src, host_t *dst,
308 traffic_selector_t *src_ts,
309 traffic_selector_t *dst_ts,
310 policy_dir_t direction, policy_type_t type,
311 ipsec_sa_cfg_t *sa, mark_t mark, bool routed);
312
313 /**
314 * Query the use time of a policy.
315 *
316 * The use time of a policy is the time the policy was used for the last
317 * time. It is not the system time, but a monotonic timestamp as returned
318 * by time_monotonic.
319 *
320 * @param src_ts traffic selector to match traffic source
321 * @param dst_ts traffic selector to match traffic dest
322 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
323 * @param mark optional mark
324 * @param[out] use_time the monotonic timestamp of this SA's last use
325 * @return SUCCESS if operation completed
326 */
327 status_t (*query_policy) (kernel_ipsec_t *this,
328 traffic_selector_t *src_ts,
329 traffic_selector_t *dst_ts,
330 policy_dir_t direction, mark_t mark,
331 u_int32_t *use_time);
332
333 /**
334 * Remove a policy from the SPD.
335 *
336 * The kernel interface implements reference counting for policies.
337 * If the same policy is installed multiple times (in the case of rekeying),
338 * the reference counter is increased. del_policy() decreases the ref counter
339 * and removes the policy only when no more references are available.
340 *
341 * @param src_ts traffic selector to match traffic source
342 * @param dst_ts traffic selector to match traffic dest
343 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
344 * @param mark optional mark
345 * @param unrouted TRUE, if this policy is unrouted from the kernel
346 * @return SUCCESS if operation completed
347 */
348 status_t (*del_policy) (kernel_ipsec_t *this,
349 traffic_selector_t *src_ts,
350 traffic_selector_t *dst_ts,
351 policy_dir_t direction, mark_t mark,
352 bool unrouted);
353
354 /**
355 * Install a bypass policy for the given socket.
356 *
357 * @param fd socket file descriptor to setup policy for
358 * @param family protocol family of the socket
359 * @return TRUE of policy set up successfully
360 */
361 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
362
363 /**
364 * Destroy the implementation.
365 */
366 void (*destroy) (kernel_ipsec_t *this);
367 };
368
369 #endif /** KERNEL_IPSEC_H_ @}*/