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