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
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>.
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
20 * @defgroup kernel_ipsec kernel_ipsec
24 #ifndef KERNEL_IPSEC_H_
25 #define KERNEL_IPSEC_H_
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
;
37 #include <utils/host.h>
38 #include <crypto/prf_plus.h>
39 #include <selectors/traffic_selector.h>
40 #include <plugins/plugin.h>
43 * Mode of an IPsec SA.
46 /** not using any encapsulation */
48 /** transport mode, no inner address */
50 /** tunnel mode, inner and outer addresses */
52 /** BEET mode, tunnel mode but fixed, bound inner addresses */
54 /** passthrough policy for traffic without an IPsec SA */
56 /** drop policy discarding traffic */
61 * enum names for ipsec_mode_t.
63 extern enum_name_t
*ipsec_mode_names
;
66 * Direction of a policy. These are equal to those
67 * defined in xfrm.h, but we want to stay implementation
71 /** Policy for inbound traffic */
73 /** Policy for outbound traffic */
75 /** Policy for forwarded traffic */
80 * enum names for policy_dir_t.
82 extern enum_name_t
*policy_dir_names
;
88 /** Normal IPsec policy */
90 /** Passthrough policy (traffic is ignored by IPsec) */
92 /** Drop policy (traffic is discarded) */
97 * High-level priority of a policy.
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
,
109 * IPComp transform IDs, as in RFC 4306
111 enum ipcomp_transform_t
{
120 * enum strings for ipcomp_transform_t.
122 extern enum_name_t
*ipcomp_transform_names
;
125 * This struct contains details about IPsec SA(s) tied to a policy.
127 struct ipsec_sa_cfg_t
{
128 /** mode of SA (tunnel, transport) */
132 /** details about ESP/AH */
134 /** TRUE if this protocol is used */
136 /** SPI for ESP/AH */
139 /** details about IPComp */
141 /** the IPComp transform used */
143 /** CPI for IPComp */
149 * A lifetime_cfg_t defines the lifetime limits of an SA.
151 * Set any of these values to 0 to ignore.
153 struct lifetime_cfg_t
{
155 /** Limit before the SA gets invalid. */
157 /** Limit before the SA gets rekeyed. */
159 /** The range of a random value subtracted from rekey. */
161 } time
, bytes
, packets
;
165 * A mark_t defines an optional mark in an IPsec SA.
175 * Interface to the ipsec subsystem of the kernel.
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.
185 struct kernel_ipsec_t
{
188 * Get a SPI from the kernel.
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
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
);
201 * Get a Compression Parameter Index (CPI) from the kernel.
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
209 status_t (*get_cpi
)(kernel_ipsec_t
*this, host_t
*src
, host_t
*dst
,
210 u_int32_t reqid
, u_int16_t
*cpi
);
213 * Add an SA to the SAD.
215 * add_sa() may update an already allocated
216 * SPI (via get_spi). In this case, the replace
218 * This function does install a single SA for a
219 * single protocol in one direction.
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
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
);
254 * Update the hosts on an installed SA.
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.
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
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
);
281 * Query the number of bytes processed by an SA from the SAD.
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
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
,
296 * Delete a previusly installed SA from the SAD.
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
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
,
311 * Flush all SAs from the SAD.
313 * @return SUCCESS if operation completed
315 status_t (*flush_sas
) (kernel_ipsec_t
*this);
318 * Add a policy to the SPD.
320 * A policy is always associated to an SA. Traffic which matches a
321 * policy is handled by the SA with the same reqid.
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
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
);
343 * Query the use time of a policy.
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
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
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
);
363 * Remove a policy from the SPD.
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.
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
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
);
385 * Flush all policies from the SPD.
387 * @return SUCCESS if operation completed
389 status_t (*flush_policies
) (kernel_ipsec_t
*this);
392 * Install a bypass policy for the given socket.
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
398 bool (*bypass_socket
)(kernel_ipsec_t
*this, int fd
, int family
);
401 * Destroy the implementation.
403 void (*destroy
) (kernel_ipsec_t
*this);
407 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
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.
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
417 bool kernel_ipsec_register(plugin_t
*plugin
, plugin_feature_t
*feature
,
418 bool reg
, void *data
);
420 #endif /** KERNEL_IPSEC_H_ @}*/