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