Introduce "features" for the kernel backends returning kernel capabilities
[strongswan.git] / src / libhydra / kernel / kernel_ipsec.h
1 /*
2 * Copyright (C) 2006-2012 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 struct kernel_ipsec_t kernel_ipsec_t;
28
29 #include <networking/host.h>
30 #include <ipsec/ipsec_types.h>
31 #include <selectors/traffic_selector.h>
32 #include <plugins/plugin.h>
33 #include <kernel/kernel_interface.h>
34
35 /**
36 * Interface to the ipsec subsystem of the kernel.
37 *
38 * The kernel ipsec interface handles the communication with the kernel
39 * for SA and policy management. It allows setup of these, and provides
40 * further the handling of kernel events.
41 * Policy information are cached in the interface. This is necessary to do
42 * reference counting. The Linux kernel does not allow the same policy
43 * installed twice, but we need this as CHILD_SA exist multiple times
44 * when rekeying. Thats why we do reference counting of policies.
45 */
46 struct kernel_ipsec_t {
47
48 /**
49 * Get the feature set supported by this kernel backend.
50 *
51 * @return ORed feature-set of backend
52 */
53 kernel_feature_t (*get_features)(kernel_ipsec_t *this);
54
55 /**
56 * Get a SPI from the kernel.
57 *
58 * @param src source address of SA
59 * @param dst destination address of SA
60 * @param protocol protocol for SA (ESP/AH)
61 * @param reqid unique ID for this SA
62 * @param spi allocated spi
63 * @return SUCCESS if operation completed
64 */
65 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
66 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi);
67
68 /**
69 * Get a Compression Parameter Index (CPI) from the kernel.
70 *
71 * @param src source address of SA
72 * @param dst destination address of SA
73 * @param reqid unique ID for the corresponding SA
74 * @param cpi allocated cpi
75 * @return SUCCESS if operation completed
76 */
77 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
78 u_int32_t reqid, u_int16_t *cpi);
79
80 /**
81 * Add an SA to the SAD.
82 *
83 * add_sa() may update an already allocated
84 * SPI (via get_spi). In this case, the replace
85 * flag must be set.
86 * This function does install a single SA for a
87 * single protocol in one direction.
88 *
89 * @param src source address for this SA
90 * @param dst destination address for this SA
91 * @param spi SPI allocated by us or remote peer
92 * @param protocol protocol for this SA (ESP/AH)
93 * @param reqid unique ID for this SA
94 * @param mark mark for this SA
95 * @param tfc Traffic Flow Confidentiality padding for this SA
96 * @param lifetime lifetime_cfg_t for this SA
97 * @param enc_alg Algorithm to use for encryption (ESP only)
98 * @param enc_key key to use for encryption
99 * @param int_alg Algorithm to use for integrity protection
100 * @param int_key key to use for integrity protection
101 * @param mode mode of the SA (tunnel, transport)
102 * @param ipcomp IPComp transform to use
103 * @param cpi CPI for IPComp
104 * @param encap enable UDP encapsulation for NAT traversal
105 * @param esn TRUE to use Extended Sequence Numbers
106 * @param inbound TRUE if this is an inbound SA
107 * @param src_ts traffic selector with BEET source address
108 * @param dst_ts traffic selector with BEET destination address
109 * @return SUCCESS if operation completed
110 */
111 status_t (*add_sa) (kernel_ipsec_t *this,
112 host_t *src, host_t *dst, u_int32_t spi,
113 u_int8_t protocol, u_int32_t reqid,
114 mark_t mark, u_int32_t tfc, lifetime_cfg_t *lifetime,
115 u_int16_t enc_alg, chunk_t enc_key,
116 u_int16_t int_alg, chunk_t int_key,
117 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
118 bool encap, bool esn, bool inbound,
119 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
120
121 /**
122 * Update the hosts on an installed SA.
123 *
124 * We cannot directly update the destination address as the kernel
125 * requires the spi, the protocol AND the destination address (and family)
126 * to identify SAs. Therefore if the destination address changed we
127 * create a new SA and delete the old one.
128 *
129 * @param spi SPI of the SA
130 * @param protocol protocol for this SA (ESP/AH)
131 * @param cpi CPI for IPComp, 0 if no IPComp is used
132 * @param src current source address
133 * @param dst current destination address
134 * @param new_src new source address
135 * @param new_dst new destination address
136 * @param encap current use of UDP encapsulation
137 * @param new_encap new use of UDP encapsulation
138 * @param mark optional mark for this SA
139 * @return SUCCESS if operation completed, NOT_SUPPORTED if
140 * the kernel interface can't update the SA
141 */
142 status_t (*update_sa)(kernel_ipsec_t *this,
143 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
144 host_t *src, host_t *dst,
145 host_t *new_src, host_t *new_dst,
146 bool encap, bool new_encap, mark_t mark);
147
148 /**
149 * Query the number of bytes processed by an SA from the SAD.
150 *
151 * @param src source address for this SA
152 * @param dst destination address for this SA
153 * @param spi SPI allocated by us or remote peer
154 * @param protocol protocol for this SA (ESP/AH)
155 * @param mark optional mark for this SA
156 * @param[out] bytes the number of bytes processed by SA
157 * @return SUCCESS if operation completed
158 */
159 status_t (*query_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
160 u_int32_t spi, u_int8_t protocol, mark_t mark,
161 u_int64_t *bytes);
162
163 /**
164 * Delete a previusly installed SA from the SAD.
165 *
166 * @param src source address for this SA
167 * @param dst destination address for this SA
168 * @param spi SPI allocated by us or remote peer
169 * @param protocol protocol for this SA (ESP/AH)
170 * @param cpi CPI for IPComp or 0
171 * @param mark optional mark for this SA
172 * @return SUCCESS if operation completed
173 */
174 status_t (*del_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
175 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
176 mark_t mark);
177
178 /**
179 * Flush all SAs from the SAD.
180 *
181 * @return SUCCESS if operation completed
182 */
183 status_t (*flush_sas) (kernel_ipsec_t *this);
184
185 /**
186 * Add a policy to the SPD.
187 *
188 * A policy is always associated to an SA. Traffic which matches a
189 * policy is handled by the SA with the same reqid.
190 *
191 * @param src source address of SA
192 * @param dst dest address of SA
193 * @param src_ts traffic selector to match traffic source
194 * @param dst_ts traffic selector to match traffic dest
195 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
196 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
197 * @param sa details about the SA(s) tied to this policy
198 * @param mark mark for this policy
199 * @param priority priority of this policy
200 * @return SUCCESS if operation completed
201 */
202 status_t (*add_policy) (kernel_ipsec_t *this,
203 host_t *src, host_t *dst,
204 traffic_selector_t *src_ts,
205 traffic_selector_t *dst_ts,
206 policy_dir_t direction, policy_type_t type,
207 ipsec_sa_cfg_t *sa, mark_t mark,
208 policy_priority_t priority);
209
210 /**
211 * Query the use time of a policy.
212 *
213 * The use time of a policy is the time the policy was used for the last
214 * time. It is not the system time, but a monotonic timestamp as returned
215 * by time_monotonic.
216 *
217 * @param src_ts traffic selector to match traffic source
218 * @param dst_ts traffic selector to match traffic dest
219 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
220 * @param mark optional mark
221 * @param[out] use_time the monotonic timestamp of this SA's last use
222 * @return SUCCESS if operation completed
223 */
224 status_t (*query_policy) (kernel_ipsec_t *this,
225 traffic_selector_t *src_ts,
226 traffic_selector_t *dst_ts,
227 policy_dir_t direction, mark_t mark,
228 u_int32_t *use_time);
229
230 /**
231 * Remove a policy from the SPD.
232 *
233 * The kernel interface implements reference counting for policies.
234 * If the same policy is installed multiple times (in the case of rekeying),
235 * the reference counter is increased. del_policy() decreases the ref counter
236 * and removes the policy only when no more references are available.
237 *
238 * @param src_ts traffic selector to match traffic source
239 * @param dst_ts traffic selector to match traffic dest
240 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
241 * @param reqid unique ID of the associated SA
242 * @param mark optional mark
243 * @param priority priority of the policy
244 * @return SUCCESS if operation completed
245 */
246 status_t (*del_policy) (kernel_ipsec_t *this,
247 traffic_selector_t *src_ts,
248 traffic_selector_t *dst_ts,
249 policy_dir_t direction, u_int32_t reqid,
250 mark_t mark, policy_priority_t priority);
251
252 /**
253 * Flush all policies from the SPD.
254 *
255 * @return SUCCESS if operation completed
256 */
257 status_t (*flush_policies) (kernel_ipsec_t *this);
258
259 /**
260 * Install a bypass policy for the given socket.
261 *
262 * @param fd socket file descriptor to setup policy for
263 * @param family protocol family of the socket
264 * @return TRUE of policy set up successfully
265 */
266 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
267
268 /**
269 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
270 *
271 * @param fd socket file descriptor
272 * @param family protocol family of the socket
273 * @param port the UDP port
274 * @return TRUE if UDP decapsulation was enabled successfully
275 */
276 bool (*enable_udp_decap)(kernel_ipsec_t *this, int fd, int family,
277 u_int16_t port);
278
279 /**
280 * Destroy the implementation.
281 */
282 void (*destroy) (kernel_ipsec_t *this);
283 };
284
285 /**
286 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
287 *
288 * This function is a plugin_feature_callback_t and can be used with the
289 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
290 *
291 * @param plugin plugin registering the kernel interface
292 * @param feature associated plugin feature
293 * @param reg TRUE to register, FALSE to unregister
294 * @param data data passed to callback, an kernel_ipsec_constructor_t
295 */
296 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
297 bool reg, void *data);
298
299 #endif /** KERNEL_IPSEC_H_ @}*/