simple roaming of the client works (not MOBIKE conform yet!)
[strongswan.git] / src / charon / kernel / kernel_interface.h
1 /**
2 * @file kernel_interface.h
3 *
4 * @brief Interface of kernel_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #ifndef KERNEL_INTERFACE_H_
26 #define KERNEL_INTERFACE_H_
27
28 typedef struct natt_conf_t natt_conf_t;
29 typedef enum policy_dir_t policy_dir_t;
30 typedef struct kernel_interface_t kernel_interface_t;
31
32 #include <utils/host.h>
33 #include <crypto/prf_plus.h>
34 #include <encoding/payloads/proposal_substructure.h>
35
36 /**
37 * Configuration for NAT-T
38 *
39 * @ingroup kernel
40 */
41 struct natt_conf_t {
42 /** source port to use for UDP-encapsulated packets */
43 u_int16_t sport;
44 /** dest port to use for UDP-encapsulated packets */
45 u_int16_t dport;
46 };
47
48 /**
49 * Direction of a policy. These are equal to those
50 * defined in xfrm.h, but we want to stay implementation
51 * neutral here.
52 *
53 * @ingroup kernel
54 */
55 enum policy_dir_t {
56 /** Policy for inbound traffic */
57 POLICY_IN = 0,
58 /** Policy for outbound traffic */
59 POLICY_OUT = 1,
60 /** Policy for forwarded traffic */
61 POLICY_FWD = 2,
62 };
63
64 /**
65 * @brief Interface to the kernel.
66 *
67 * The kernel interface handles the communication with the kernel
68 * for SA and policy management. It allows setup of these, and provides
69 * further the handling of kernel events.
70 * Policy information are cached in the interface. This is necessary to do
71 * reference counting. The Linux kernel does not allow the same policy
72 * installed twice, but we need this as CHILD_SA exist multiple times
73 * when rekeying. Thats why we do reference counting of policies.
74 *
75 * @b Constructors:
76 * - kernel_interface_create()
77 *
78 * @ingroup kernel
79 */
80 struct kernel_interface_t {
81
82 /**
83 * @brief Get a SPI from the kernel.
84 *
85 * @warning get_spi() implicitely creates an SA with
86 * the allocated SPI, therefore the replace flag
87 * in add_sa() must be set when installing this SA.
88 *
89 * @param this calling object
90 * @param src source address of SA
91 * @param dst destination address of SA
92 * @param protocol protocol for SA (ESP/AH)
93 * @param reqid unique ID for this SA
94 * @param[out] spi allocated spi
95 * @return
96 * - SUCCESS
97 * - FAILED if kernel comm failed
98 */
99 status_t (*get_spi)(kernel_interface_t *this, host_t *src, host_t *dst,
100 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi);
101
102 /**
103 * @brief Add an SA to the SAD.
104 *
105 * add_sa() may update an already allocated
106 * SPI (via get_spi). In this case, the replace
107 * flag must be set.
108 * This function does install a single SA for a
109 * single protocol in one direction. The kernel-interface
110 * gets the keys itself from the PRF, as we don't know
111 * his algorithms and key sizes.
112 *
113 * @param this calling object
114 * @param src source address for this SA
115 * @param dst destination address for this SA
116 * @param spi SPI allocated by us or remote peer
117 * @param protocol protocol for this SA (ESP/AH)
118 * @param reqid unique ID for this SA
119 * @param expire_soft lifetime in seconds before rekeying
120 * @param expire_hard lieftime in seconds before delete
121 * @param enc_alg Algorithm to use for encryption (ESP only)
122 * @param int_alg Algorithm to use for integrity protection
123 * @param prf_plus PRF to derive keys from
124 * @param natt NAT-T Configuration, or NULL of no NAT-T used
125 * @param mode mode of the SA (tunnel, transport)
126 * @param replace Should an already installed SA be updated?
127 * @return
128 * - SUCCESS
129 * - FAILED if kernel comm failed
130 */
131 status_t (*add_sa) (kernel_interface_t *this,
132 host_t *src, host_t *dst, u_int32_t spi,
133 protocol_id_t protocol, u_int32_t reqid,
134 u_int64_t expire_soft, u_int64_t expire_hard,
135 algorithm_t *enc_alg, algorithm_t *int_alg,
136 prf_plus_t *prf_plus, natt_conf_t *natt,
137 mode_t mode, bool update);
138
139 /**
140 * @brief Update the hosts on an installed SA.
141 *
142 * We cannot directly update the destination address as the kernel
143 * requires the spi, the protocol AND the destination address (and family)
144 * to identify SAs. Therefore if the destination address changed we
145 * create a new SA and delete the old one.
146 *
147 * @param this calling object
148 * @param spi SPI of the SA
149 * @param protocol protocol for this SA (ESP/AH)
150 * @param src current source address
151 * @param dst current destination address
152 * @param new_src new source address
153 * @param new_dst new destination address
154 * @return
155 * - SUCCESS
156 * - FAILED if kernel comm failed
157 */
158 status_t (*update_sa)(kernel_interface_t *this,
159 u_int32_t spi, protocol_id_t protocol,
160 host_t *src, host_t *dst,
161 host_t *new_src, host_t *new_dst);
162
163 /**
164 * @brief Query the use time of an SA.
165 *
166 * The use time of an SA is not the time of the last usage, but
167 * the time of the first usage of the SA.
168 *
169 * @param this calling object
170 * @param dst destination address for this SA
171 * @param spi SPI allocated by us or remote peer
172 * @param protocol protocol for this SA (ESP/AH)
173 * @param[out] use_time the time of this SA's last use
174 * @return
175 * - SUCCESS
176 * - FAILED if kernel comm failed
177 */
178 status_t (*query_sa) (kernel_interface_t *this, host_t *dst, u_int32_t spi,
179 protocol_id_t protocol, u_int32_t *use_time);
180
181 /**
182 * @brief Delete a previusly installed SA from the SAD.
183 *
184 * @param this calling object
185 * @param dst destination address for this SA
186 * @param spi SPI allocated by us or remote peer
187 * @param protocol protocol for this SA (ESP/AH)
188 * @return
189 * - SUCCESS
190 * - FAILED if kernel comm failed
191 */
192 status_t (*del_sa) (kernel_interface_t *this, host_t *dst, u_int32_t spi,
193 protocol_id_t protocol);
194
195 /**
196 * @brief Add a policy to the SPD.
197 *
198 * A policy is always associated to an SA. Traffic which matches a
199 * policy is handled by the SA with the same reqid.
200 * If the update flag is set, the policy is updated with the new
201 * src/dst addresses.
202 * If the update flag is not set, but a such policy is already in the
203 * kernel, the reference count to this policy is increased.
204 *
205 * @param this calling object
206 * @param src source address of SA
207 * @param dst dest address of SA
208 * @param src_ts traffic selector to match traffic source
209 * @param dst_ts traffic selector to match traffic dest
210 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
211 * @param protocol protocol to use to protect traffic (AH/ESP)
212 * @param reqid uniqe ID of an SA to use to enforce policy
213 * @param high_prio if TRUE, uses a higher priority than any with FALSE
214 * @param mode mode of SA (tunnel, transport)
215 * @param update update an existing policy, if TRUE
216 * @return
217 * - SUCCESS
218 * - FAILED if kernel comm failed
219 */
220 status_t (*add_policy) (kernel_interface_t *this,
221 host_t *src, host_t *dst,
222 traffic_selector_t *src_ts,
223 traffic_selector_t *dst_ts,
224 policy_dir_t direction, protocol_id_t protocol,
225 u_int32_t reqid, bool high_prio,
226 mode_t mode, bool update);
227
228 /**
229 * @brief Query the use time of a policy.
230 *
231 * The use time of a policy is the time the policy was used
232 * for the last time.
233 *
234 * @param this calling object
235 * @param src_ts traffic selector to match traffic source
236 * @param dst_ts traffic selector to match traffic dest
237 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
238 * @param[out] use_time the time of this SA's last use
239 * @return
240 * - SUCCESS
241 * - FAILED if kernel comm failed
242 */
243 status_t (*query_policy) (kernel_interface_t *this,
244 traffic_selector_t *src_ts,
245 traffic_selector_t *dst_ts,
246 policy_dir_t direction, u_int32_t *use_time);
247
248 /**
249 * @brief Remove a policy from the SPD.
250 *
251 * The kernel interface implements reference counting for policies.
252 * If the same policy is installed multiple times (in the case of rekeying),
253 * the reference counter is increased. del_policy() decreases the ref counter
254 * and removes the policy only when no more references are available.
255 *
256 * @param this calling object
257 * @param src_ts traffic selector to match traffic source
258 * @param dst_ts traffic selector to match traffic dest
259 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
260 * @return
261 * - SUCCESS
262 * - FAILED if kernel comm failed
263 */
264 status_t (*del_policy) (kernel_interface_t *this,
265 traffic_selector_t *src_ts,
266 traffic_selector_t *dst_ts,
267 policy_dir_t direction);
268
269 /**
270 * @brief Get our outgoing source address for a destination.
271 *
272 * Does a route lookup to get the source address used to reach dest.
273 * The returned host is allocated and must be destroyed.
274 *
275 * @param this calling object
276 * @param dest target destination address
277 * @return outgoing source address, NULL if unreachable
278 */
279 host_t* (*get_source_addr)(kernel_interface_t *this, host_t *dest);
280
281 /**
282 * @brief Get the interface name of a local address.
283 *
284 * @param this calling object
285 * @param host address to get interface name from
286 * @return allocated interface name, or NULL if not found
287 */
288 char* (*get_interface) (kernel_interface_t *this, host_t *host);
289
290 /**
291 * @brief Creates an iterator over all local addresses.
292 *
293 * This function blocks an internal cached address list until the
294 * iterator gets destroyed.
295 * These hosts are read-only, do not modify or free.
296 *
297 * @param this calling object
298 * @return iterator over host_t's
299 */
300 iterator_t *(*create_address_iterator) (kernel_interface_t *this);
301
302 /**
303 * @brief Add a virtual IP to an interface.
304 *
305 * Virtual IPs are attached to an interface. If an IP is added multiple
306 * times, the IP is refcounted and not removed until del_ip() was called
307 * as many times as add_ip().
308 * The virtual IP is attached to the interface where the iface_ip is found.
309 *
310 * @param this calling object
311 * @param virtual_ip virtual ip address to assign
312 * @param iface_ip IP of an interface to attach virtual IP
313 * @return
314 * - SUCCESS
315 * - FAILED if kernel comm failed
316 */
317 status_t (*add_ip) (kernel_interface_t *this, host_t *virtual_ip,
318 host_t *iface_ip);
319
320 /**
321 * @brief Remove a virtual IP from an interface.
322 *
323 * The kernel interface uses refcounting, see add_ip().
324 *
325 * @param this calling object
326 * @param virtual_ip virtual ip address to assign
327 * @return
328 * - SUCCESS
329 * - FAILED if kernel comm failed
330 */
331 status_t (*del_ip) (kernel_interface_t *this, host_t *virtual_ip);
332
333 /**
334 * @brief Destroys a kernel_interface object.
335 *
336 * @param kernel_interface_t calling object
337 */
338 void (*destroy) (kernel_interface_t *kernel_interface);
339 };
340
341 /**
342 * @brief Creates an object of type kernel_interface_t.
343 *
344 * @ingroup kernel
345 */
346 kernel_interface_t *kernel_interface_create(void);
347
348 #endif /*KERNEL_INTERFACE_H_*/