a2a83b60824d4849eb9214b133b0956c5287f903
[strongswan.git] / src / charon / kernel / kernel_interface.h
1 /*
2 * Copyright (C) 2006-2008 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 * $Id$
19 */
20
21 /**
22 * @defgroup kernel_interface kernel_interface
23 * @{ @ingroup kernel
24 */
25
26 #ifndef KERNEL_INTERFACE_H_
27 #define KERNEL_INTERFACE_H_
28
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 #include <kernel/kernel_ipsec.h>
36 #include <kernel/kernel_net.h>
37
38 /**
39 * Constructor function for ipsec kernel interface
40 */
41 typedef kernel_ipsec_t* (*kernel_ipsec_constructor_t)(void);
42
43 /**
44 * Constructor function for network kernel interface
45 */
46 typedef kernel_net_t* (*kernel_net_constructor_t)(void);
47
48 /**
49 * Manager and wrapper for different kernel interfaces.
50 *
51 * The kernel interface handles the communication with the kernel
52 * for SA and policy management and interface and IP address management.
53 */
54 struct kernel_interface_t {
55
56 /**
57 * Get a SPI from the kernel.
58 *
59 * @warning get_spi() implicitly creates an SA with
60 * the allocated SPI, therefore the replace flag
61 * in add_sa() must be set when installing this SA.
62 *
63 * @param src source address of SA
64 * @param dst destination address of SA
65 * @param protocol protocol for SA (ESP/AH)
66 * @param reqid unique ID for this SA
67 * @param spi allocated spi
68 * @return SUCCESS if operation completed
69 */
70 status_t (*get_spi)(kernel_interface_t *this, host_t *src, host_t *dst,
71 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi);
72
73 /**
74 * Get a Compression Parameter Index (CPI) from the kernel.
75 *
76 * @param src source address of SA
77 * @param dst destination address of SA
78 * @param reqid unique ID for the corresponding SA
79 * @param cpi allocated cpi
80 * @return SUCCESS if operation completed
81 */
82 status_t (*get_cpi)(kernel_interface_t *this, host_t *src, host_t *dst,
83 u_int32_t reqid, u_int16_t *cpi);
84
85 /**
86 * Add an SA to the SAD.
87 *
88 * add_sa() may update an already allocated
89 * SPI (via get_spi). In this case, the replace
90 * flag must be set.
91 * This function does install a single SA for a
92 * single protocol in one direction. The kernel-interface
93 * gets the keys itself from the PRF, as we don't know
94 * his algorithms and key sizes.
95 *
96 * @param src source address for this SA
97 * @param dst destination address for this SA
98 * @param spi SPI allocated by us or remote peer
99 * @param protocol protocol for this SA (ESP/AH)
100 * @param reqid unique ID for this SA
101 * @param expire_soft lifetime in seconds before rekeying
102 * @param expire_hard lifetime in seconds before delete
103 * @param enc_alg Algorithm to use for encryption (ESP only)
104 * @param enc_size key length of encryption algorithm, if dynamic
105 * @param int_alg Algorithm to use for integrity protection
106 * @param int_size key length of integrity algorithm, if dynamic
107 * @param prf_plus PRF to derive keys from
108 * @param mode mode of the SA (tunnel, transport)
109 * @param ipcomp IPComp transform to use
110 * @param encap enable UDP encapsulation for NAT traversal
111 * @param replace Should an already installed SA be updated?
112 * @return SUCCESS if operation completed
113 */
114 status_t (*add_sa) (kernel_interface_t *this,
115 host_t *src, host_t *dst, u_int32_t spi,
116 protocol_id_t protocol, u_int32_t reqid,
117 u_int64_t expire_soft, u_int64_t expire_hard,
118 u_int16_t enc_alg, u_int16_t enc_size,
119 u_int16_t int_alg, u_int16_t int_size,
120 prf_plus_t *prf_plus, ipsec_mode_t mode,
121 u_int16_t ipcomp, bool encap,
122 bool update);
123
124 /**
125 * Update the hosts on an installed SA.
126 *
127 * We cannot directly update the destination address as the kernel
128 * requires the spi, the protocol AND the destination address (and family)
129 * to identify SAs. Therefore if the destination address changed we
130 * create a new SA and delete the old one.
131 *
132 * @param spi SPI of the SA
133 * @param protocol protocol for this SA (ESP/AH)
134 * @param src current source address
135 * @param dst current destination address
136 * @param new_src new source address
137 * @param new_dst new destination address
138 * @param encap use UDP encapsulation
139 * @return SUCCESS if operation completed
140 */
141 status_t (*update_sa)(kernel_interface_t *this,
142 u_int32_t spi, protocol_id_t protocol,
143 host_t *src, host_t *dst,
144 host_t *new_src, host_t *new_dst, bool encap);
145
146 /**
147 * Delete a previously installed SA from the SAD.
148 *
149 * @param dst destination address for this SA
150 * @param spi SPI allocated by us or remote peer
151 * @param protocol protocol for this SA (ESP/AH)
152 * @return SUCCESS if operation completed
153 */
154 status_t (*del_sa) (kernel_interface_t *this, host_t *dst, u_int32_t spi,
155 protocol_id_t protocol);
156
157 /**
158 * Add a policy to the SPD.
159 *
160 * A policy is always associated to an SA. Traffic which matches a
161 * policy is handled by the SA with the same reqid.
162 *
163 * @param src source address of SA
164 * @param dst dest address of SA
165 * @param src_ts traffic selector to match traffic source
166 * @param dst_ts traffic selector to match traffic dest
167 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
168 * @param protocol protocol to use to protect traffic (AH/ESP)
169 * @param reqid unique ID of an SA to use to enforce policy
170 * @param high_prio if TRUE, uses a higher priority than any with FALSE
171 * @param mode mode of SA (tunnel, transport)
172 * @param ipcomp the IPComp transform used
173 * @return SUCCESS if operation completed
174 */
175 status_t (*add_policy) (kernel_interface_t *this,
176 host_t *src, host_t *dst,
177 traffic_selector_t *src_ts,
178 traffic_selector_t *dst_ts,
179 policy_dir_t direction, protocol_id_t protocol,
180 u_int32_t reqid, bool high_prio, ipsec_mode_t mode,
181 u_int16_t ipcomp);
182
183 /**
184 * Query the use time of a policy.
185 *
186 * The use time of a policy is the time the policy was used
187 * for the last time.
188 *
189 * @param src_ts traffic selector to match traffic source
190 * @param dst_ts traffic selector to match traffic dest
191 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
192 * @param[out] use_time the time of this SA's last use
193 * @return SUCCESS if operation completed
194 */
195 status_t (*query_policy) (kernel_interface_t *this,
196 traffic_selector_t *src_ts,
197 traffic_selector_t *dst_ts,
198 policy_dir_t direction, u_int32_t *use_time);
199
200 /**
201 * Remove a policy from the SPD.
202 *
203 * The kernel interface implements reference counting for policies.
204 * If the same policy is installed multiple times (in the case of rekeying),
205 * the reference counter is increased. del_policy() decreases the ref counter
206 * and removes the policy only when no more references are available.
207 *
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 * @return SUCCESS if operation completed
212 */
213 status_t (*del_policy) (kernel_interface_t *this,
214 traffic_selector_t *src_ts,
215 traffic_selector_t *dst_ts,
216 policy_dir_t direction);
217
218 /**
219 * Get our outgoing source address for a destination.
220 *
221 * Does a route lookup to get the source address used to reach dest.
222 * The returned host is allocated and must be destroyed.
223 * An optional src address can be used to check if a route is available
224 * for given source to dest.
225 *
226 * @param dest target destination address
227 * @param src source address to check, or NULL
228 * @return outgoing source address, NULL if unreachable
229 */
230 host_t* (*get_source_addr)(kernel_interface_t *this,
231 host_t *dest, host_t *src);
232
233 /**
234 * Get the next hop for a destination.
235 *
236 * Does a route lookup to get the next hop used to reach dest.
237 * The returned host is allocated and must be destroyed.
238 *
239 * @param dest target destination address
240 * @return next hop address, NULL if unreachable
241 */
242 host_t* (*get_nexthop)(kernel_interface_t *this, host_t *dest);
243
244 /**
245 * Get the interface name of a local address.
246 *
247 * @param host address to get interface name from
248 * @return allocated interface name, or NULL if not found
249 */
250 char* (*get_interface) (kernel_interface_t *this, host_t *host);
251
252 /**
253 * Creates an enumerator over all local addresses.
254 *
255 * This function blocks an internal cached address list until the
256 * enumerator gets destroyed.
257 * The hosts are read-only, do not modify of free.
258 *
259 * @param include_down_ifaces TRUE to enumerate addresses from down interfaces
260 * @param include_virtual_ips TRUE to enumerate virtual ip addresses
261 * @return enumerator over host_t's
262 */
263 enumerator_t *(*create_address_enumerator) (kernel_interface_t *this,
264 bool include_down_ifaces, bool include_virtual_ips);
265
266 /**
267 * Add a virtual IP to an interface.
268 *
269 * Virtual IPs are attached to an interface. If an IP is added multiple
270 * times, the IP is refcounted and not removed until del_ip() was called
271 * as many times as add_ip().
272 * The virtual IP is attached to the interface where the iface_ip is found.
273 *
274 * @param virtual_ip virtual ip address to assign
275 * @param iface_ip IP of an interface to attach virtual IP
276 * @return SUCCESS if operation completed
277 */
278 status_t (*add_ip) (kernel_interface_t *this, host_t *virtual_ip,
279 host_t *iface_ip);
280
281 /**
282 * Remove a virtual IP from an interface.
283 *
284 * The kernel interface uses refcounting, see add_ip().
285 *
286 * @param virtual_ip virtual ip address to assign
287 * @return SUCCESS if operation completed
288 */
289 status_t (*del_ip) (kernel_interface_t *this, host_t *virtual_ip);
290
291 /**
292 * Add a route.
293 *
294 * @param dst_net destination net
295 * @param prefixlen destination net prefix length
296 * @param gateway gateway for this route
297 * @param src_ip sourc ip of the route
298 * @param if_name name of the interface the route is bound to
299 * @return SUCCESS if operation completed
300 * ALREADY_DONE if the route already exists
301 */
302 status_t (*add_route) (kernel_interface_t *this, chunk_t dst_net, u_int8_t prefixlen,
303 host_t *gateway, host_t *src_ip, char *if_name);
304
305 /**
306 * Delete a route.
307 *
308 * @param dst_net destination net
309 * @param prefixlen destination net prefix length
310 * @param gateway gateway for this route
311 * @param src_ip sourc ip of the route
312 * @param if_name name of the interface the route is bound to
313 * @return SUCCESS if operation completed
314 */
315 status_t (*del_route) (kernel_interface_t *this, chunk_t dst_net, u_int8_t prefixlen,
316 host_t *gateway, host_t *src_ip, char *if_name);
317
318 /**
319 * manager methods
320 */
321
322 /**
323 * Tries to find an ip address of a local interface that is included in the
324 * supplied traffic selector.
325 *
326 * @param ts traffic selector
327 * @param ip returned ip (has to be destroyed)
328 * @return SUCCESS if address found
329 */
330 status_t (*get_address_by_ts) (kernel_interface_t *this,
331 traffic_selector_t *ts, host_t **ip);
332
333 /**
334 * Register an ipsec kernel interface constructor on the manager.
335 *
336 * @param create constructor to register
337 */
338 void (*add_ipsec_interface)(kernel_interface_t *this, kernel_ipsec_constructor_t create);
339
340 /**
341 * Unregister an ipsec kernel interface constructor.
342 *
343 * @param create constructor to unregister
344 */
345 void (*remove_ipsec_interface)(kernel_interface_t *this, kernel_ipsec_constructor_t create);
346
347 /**
348 * Register a network kernel interface constructor on the manager.
349 *
350 * @param create constructor to register
351 */
352 void (*add_net_interface)(kernel_interface_t *this, kernel_net_constructor_t create);
353
354 /**
355 * Unregister a network kernel interface constructor.
356 *
357 * @param create constructor to unregister
358 */
359 void (*remove_net_interface)(kernel_interface_t *this, kernel_net_constructor_t create);
360
361 /**
362 * Create the kernel interfaces classes.
363 */
364 void (*create_interfaces)(kernel_interface_t *this);
365
366 /**
367 * Destroys a kernel_interface_manager_t object.
368 */
369 void (*destroy) (kernel_interface_t *this);
370 };
371
372 /**
373 * Creates an object of type kernel_interface_t.
374 */
375 kernel_interface_t *kernel_interface_create(void);
376
377 #endif /* KERNEL_INTERFACE_H_ @} */