8b0c7a2968e4fb25d6439bcbc2d85e59dc60a755
[strongswan.git] / src / libhydra / kernel / kernel_interface.h
1 /*
2 * Copyright (C) 2006-2010 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_interface kernel_interface
21 * @{ @ingroup hkernel
22 */
23
24 #ifndef KERNEL_INTERFACE_H_
25 #define KERNEL_INTERFACE_H_
26
27 typedef struct kernel_interface_t kernel_interface_t;
28
29 #include <utils/host.h>
30 #include <crypto/prf_plus.h>
31
32 #include <kernel/kernel_listener.h>
33 #include <kernel/kernel_ipsec.h>
34 #include <kernel/kernel_net.h>
35
36 /**
37 * Constructor function for ipsec kernel interface
38 */
39 typedef kernel_ipsec_t* (*kernel_ipsec_constructor_t)(void);
40
41 /**
42 * Constructor function for network kernel interface
43 */
44 typedef kernel_net_t* (*kernel_net_constructor_t)(void);
45
46 /**
47 * Manager and wrapper for different kernel interfaces.
48 *
49 * The kernel interface handles the communication with the kernel
50 * for SA and policy management and interface and IP address management.
51 */
52 struct kernel_interface_t {
53
54 /**
55 * Get a SPI from the kernel.
56 *
57 * @param src source address of SA
58 * @param dst destination address of SA
59 * @param protocol protocol for SA (ESP/AH)
60 * @param reqid unique ID for this SA
61 * @param spi allocated spi
62 * @return SUCCESS if operation completed
63 */
64 status_t (*get_spi)(kernel_interface_t *this, host_t *src, host_t *dst,
65 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi);
66
67 /**
68 * Get a Compression Parameter Index (CPI) from the kernel.
69 *
70 * @param src source address of SA
71 * @param dst destination address of SA
72 * @param reqid unique ID for the corresponding SA
73 * @param cpi allocated cpi
74 * @return SUCCESS if operation completed
75 */
76 status_t (*get_cpi)(kernel_interface_t *this, host_t *src, host_t *dst,
77 u_int32_t reqid, u_int16_t *cpi);
78
79 /**
80 * Add an SA to the SAD.
81 *
82 * add_sa() may update an already allocated
83 * SPI (via get_spi). In this case, the replace
84 * flag must be set.
85 * This function does install a single SA for a
86 * single protocol in one direction.
87 *
88 * @param src source address for this SA
89 * @param dst destination address for this SA
90 * @param spi SPI allocated by us or remote peer
91 * @param protocol protocol for this SA (ESP/AH)
92 * @param reqid unique ID for this SA
93 * @param mark optional mark for this SA
94 * @param lifetime lifetime_cfg_t for this SA
95 * @param enc_alg Algorithm to use for encryption (ESP only)
96 * @param enc_key key to use for encryption
97 * @param int_alg Algorithm to use for integrity protection
98 * @param int_key key to use for integrity protection
99 * @param mode mode of the SA (tunnel, transport)
100 * @param ipcomp IPComp transform to use
101 * @param cpi CPI for IPComp
102 * @param encap enable UDP encapsulation for NAT traversal
103 * @param inbound TRUE if this is an inbound SA
104 * @param src_ts traffic selector with BEET source address
105 * @param dst_ts traffic selector with BEET destination address
106 * @return SUCCESS if operation completed
107 */
108 status_t (*add_sa) (kernel_interface_t *this,
109 host_t *src, host_t *dst, u_int32_t spi,
110 u_int8_t protocol, u_int32_t reqid, mark_t mark,
111 lifetime_cfg_t *lifetime,
112 u_int16_t enc_alg, chunk_t enc_key,
113 u_int16_t int_alg, chunk_t int_key,
114 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
115 bool encap, bool inbound,
116 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
117
118 /**
119 * Update the hosts on an installed SA.
120 *
121 * We cannot directly update the destination address as the kernel
122 * requires the spi, the protocol AND the destination address (and family)
123 * to identify SAs. Therefore if the destination address changed we
124 * create a new SA and delete the old one.
125 *
126 * @param spi SPI of the SA
127 * @param protocol protocol for this SA (ESP/AH)
128 * @param cpi CPI for IPComp, 0 if no IPComp is used
129 * @param src current source address
130 * @param dst current destination address
131 * @param new_src new source address
132 * @param new_dst new destination address
133 * @param encap current use of UDP encapsulation
134 * @param new_encap new use of UDP encapsulation
135 * @param mark optional mark for this SA
136 * @return SUCCESS if operation completed, NOT_SUPPORTED if
137 * the kernel interface can't update the SA
138 */
139 status_t (*update_sa)(kernel_interface_t *this,
140 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
141 host_t *src, host_t *dst,
142 host_t *new_src, host_t *new_dst,
143 bool encap, bool new_encap, mark_t mark);
144
145 /**
146 * Query the number of bytes processed by an SA from the SAD.
147 *
148 * @param src source address for this SA
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 * @param mark optional mark for this SA
153 * @param[out] bytes the number of bytes processed by SA
154 * @return SUCCESS if operation completed
155 */
156 status_t (*query_sa) (kernel_interface_t *this, host_t *src, host_t *dst,
157 u_int32_t spi, u_int8_t protocol, mark_t mark,
158 u_int64_t *bytes);
159
160 /**
161 * Delete a previously installed SA from the SAD.
162 *
163 * @param src source address for this SA
164 * @param dst destination address for this SA
165 * @param spi SPI allocated by us or remote peer
166 * @param protocol protocol for this SA (ESP/AH)
167 * @param cpi CPI for IPComp or 0
168 * @param mark optional mark for this SA
169 * @return SUCCESS if operation completed
170 */
171 status_t (*del_sa) (kernel_interface_t *this, host_t *src, host_t *dst,
172 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
173 mark_t mark);
174
175 /**
176 * Add a policy to the SPD.
177 *
178 * A policy is always associated to an SA. Traffic which matches a
179 * policy is handled by the SA with the same reqid.
180 *
181 * @param src source address of SA
182 * @param dst dest address of SA
183 * @param src_ts traffic selector to match traffic source
184 * @param dst_ts traffic selector to match traffic dest
185 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
186 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
187 * @param sa details about the SA(s) tied to this policy
188 * @param mark mark for this policy
189 * @param routed TRUE, if this policy is routed in the kernel
190 * @return SUCCESS if operation completed
191 */
192 status_t (*add_policy) (kernel_interface_t *this,
193 host_t *src, host_t *dst,
194 traffic_selector_t *src_ts,
195 traffic_selector_t *dst_ts,
196 policy_dir_t direction, policy_type_t type,
197 ipsec_sa_cfg_t *sa, mark_t mark, bool routed);
198
199 /**
200 * Query the use time of a policy.
201 *
202 * The use time of a policy is the time the policy was used
203 * for the last time.
204 *
205 * @param src_ts traffic selector to match traffic source
206 * @param dst_ts traffic selector to match traffic dest
207 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
208 * @param mark optional mark
209 * @param[out] use_time the time of this SA's last use
210 * @return SUCCESS if operation completed
211 */
212 status_t (*query_policy) (kernel_interface_t *this,
213 traffic_selector_t *src_ts,
214 traffic_selector_t *dst_ts,
215 policy_dir_t direction, mark_t mark,
216 u_int32_t *use_time);
217
218 /**
219 * Remove a policy from the SPD.
220 *
221 * The kernel interface implements reference counting for policies.
222 * If the same policy is installed multiple times (in the case of rekeying),
223 * the reference counter is increased. del_policy() decreases the ref counter
224 * and removes the policy only when no more references are available.
225 *
226 * @param src_ts traffic selector to match traffic source
227 * @param dst_ts traffic selector to match traffic dest
228 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
229 * @param mark optional mark
230 * @param unrouted TRUE, if this policy is unrouted from the kernel
231 * @return SUCCESS if operation completed
232 */
233 status_t (*del_policy) (kernel_interface_t *this,
234 traffic_selector_t *src_ts,
235 traffic_selector_t *dst_ts,
236 policy_dir_t direction, mark_t mark,
237 bool unrouted);
238
239 /**
240 * Get our outgoing source address for a destination.
241 *
242 * Does a route lookup to get the source address used to reach dest.
243 * The returned host is allocated and must be destroyed.
244 * An optional src address can be used to check if a route is available
245 * for given source to dest.
246 *
247 * @param dest target destination address
248 * @param src source address to check, or NULL
249 * @return outgoing source address, NULL if unreachable
250 */
251 host_t* (*get_source_addr)(kernel_interface_t *this,
252 host_t *dest, host_t *src);
253
254 /**
255 * Get the next hop for a destination.
256 *
257 * Does a route lookup to get the next hop used to reach dest.
258 * The returned host is allocated and must be destroyed.
259 *
260 * @param dest target destination address
261 * @return next hop address, NULL if unreachable
262 */
263 host_t* (*get_nexthop)(kernel_interface_t *this, host_t *dest);
264
265 /**
266 * Get the interface name of a local address.
267 *
268 * @param host address to get interface name from
269 * @return allocated interface name, or NULL if not found
270 */
271 char* (*get_interface) (kernel_interface_t *this, host_t *host);
272
273 /**
274 * Creates an enumerator over all local addresses.
275 *
276 * This function blocks an internal cached address list until the
277 * enumerator gets destroyed.
278 * The hosts are read-only, do not modify of free.
279 *
280 * @param include_down_ifaces TRUE to enumerate addresses from down interfaces
281 * @param include_virtual_ips TRUE to enumerate virtual ip addresses
282 * @return enumerator over host_t's
283 */
284 enumerator_t *(*create_address_enumerator) (kernel_interface_t *this,
285 bool include_down_ifaces, bool include_virtual_ips);
286
287 /**
288 * Add a virtual IP to an interface.
289 *
290 * Virtual IPs are attached to an interface. If an IP is added multiple
291 * times, the IP is refcounted and not removed until del_ip() was called
292 * as many times as add_ip().
293 * The virtual IP is attached to the interface where the iface_ip is found.
294 *
295 * @param virtual_ip virtual ip address to assign
296 * @param iface_ip IP of an interface to attach virtual IP
297 * @return SUCCESS if operation completed
298 */
299 status_t (*add_ip) (kernel_interface_t *this, host_t *virtual_ip,
300 host_t *iface_ip);
301
302 /**
303 * Remove a virtual IP from an interface.
304 *
305 * The kernel interface uses refcounting, see add_ip().
306 *
307 * @param virtual_ip virtual ip address to assign
308 * @return SUCCESS if operation completed
309 */
310 status_t (*del_ip) (kernel_interface_t *this, host_t *virtual_ip);
311
312 /**
313 * Add a route.
314 *
315 * @param dst_net destination net
316 * @param prefixlen destination net prefix length
317 * @param gateway gateway for this route
318 * @param src_ip sourc ip of the route
319 * @param if_name name of the interface the route is bound to
320 * @return SUCCESS if operation completed
321 * ALREADY_DONE if the route already exists
322 */
323 status_t (*add_route) (kernel_interface_t *this, chunk_t dst_net,
324 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
325 char *if_name);
326
327 /**
328 * Delete a route.
329 *
330 * @param dst_net destination net
331 * @param prefixlen destination net prefix length
332 * @param gateway gateway for this route
333 * @param src_ip sourc ip of the route
334 * @param if_name name of the interface the route is bound to
335 * @return SUCCESS if operation completed
336 */
337 status_t (*del_route) (kernel_interface_t *this, chunk_t dst_net,
338 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
339 char *if_name);
340
341 /**
342 * Set up a bypass policy for a given socket.
343 *
344 * @param fd socket file descriptor to setup policy for
345 * @param family protocol family of the socket
346 * @return TRUE of policy set up successfully
347 */
348 bool (*bypass_socket)(kernel_interface_t *this, int fd, int family);
349
350 /**
351 * manager methods
352 */
353
354 /**
355 * Tries to find an ip address of a local interface that is included in the
356 * supplied traffic selector.
357 *
358 * @param ts traffic selector
359 * @param ip returned ip (has to be destroyed)
360 * @return SUCCESS if address found
361 */
362 status_t (*get_address_by_ts)(kernel_interface_t *this,
363 traffic_selector_t *ts, host_t **ip);
364
365 /**
366 * Register an ipsec kernel interface constructor on the manager.
367 *
368 * @param create constructor to register
369 */
370 void (*add_ipsec_interface)(kernel_interface_t *this,
371 kernel_ipsec_constructor_t create);
372
373 /**
374 * Unregister an ipsec kernel interface constructor.
375 *
376 * @param create constructor to unregister
377 */
378 void (*remove_ipsec_interface)(kernel_interface_t *this,
379 kernel_ipsec_constructor_t create);
380
381 /**
382 * Register a network kernel interface constructor on the manager.
383 *
384 * @param create constructor to register
385 */
386 void (*add_net_interface)(kernel_interface_t *this,
387 kernel_net_constructor_t create);
388
389 /**
390 * Unregister a network kernel interface constructor.
391 *
392 * @param create constructor to unregister
393 */
394 void (*remove_net_interface)(kernel_interface_t *this,
395 kernel_net_constructor_t create);
396
397 /**
398 * Add a listener to the kernel interface.
399 *
400 * @param listener listener to add
401 */
402 void (*add_listener)(kernel_interface_t *this,
403 kernel_listener_t *listener);
404
405 /**
406 * Remove a listener from the kernel interface.
407 *
408 * @param listener listener to remove
409 */
410 void (*remove_listener)(kernel_interface_t *this,
411 kernel_listener_t *listener);
412
413 /**
414 * Raise an acquire event.
415 *
416 * @param reqid reqid of the policy to acquire
417 * @param src_ts source traffic selector
418 * @param dst_ts destination traffic selector
419 */
420 void (*acquire)(kernel_interface_t *this, u_int32_t reqid,
421 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
422
423 /**
424 * Raise an expire event.
425 *
426 * @param reqid reqid of the expired SA
427 * @param protocol protocol of the expired SA
428 * @param spi spi of the expired SA
429 * @param hard TRUE if it is a hard expire, FALSE otherwise
430 */
431 void (*expire)(kernel_interface_t *this, u_int32_t reqid,
432 u_int8_t protocol, u_int32_t spi, bool hard);
433
434 /**
435 * Raise a mapping event.
436 *
437 * @param reqid reqid of the SA
438 * @param spi spi of the SA
439 * @param remote new remote host
440 */
441 void (*mapping)(kernel_interface_t *this, u_int32_t reqid, u_int32_t spi,
442 host_t *remote);
443
444 /**
445 * Raise a migrate event.
446 *
447 * @param reqid reqid of the policy
448 * @param src_ts source traffic selector
449 * @param dst_ts destination traffic selector
450 * @param direction direction of the policy (in|out)
451 * @param local local host address to be used in the IKE_SA
452 * @param remote remote host address to be used in the IKE_SA
453 */
454 void (*migrate)(kernel_interface_t *this, u_int32_t reqid,
455 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
456 policy_dir_t direction, host_t *local, host_t *remote);
457
458 /**
459 * Raise a roam event.
460 *
461 * @param address TRUE if address list, FALSE if routing changed
462 */
463 void (*roam)(kernel_interface_t *this, bool address);
464
465 /**
466 * Destroys a kernel_interface_manager_t object.
467 */
468 void (*destroy) (kernel_interface_t *this);
469 };
470
471 /**
472 * Creates an object of type kernel_interface_t.
473 */
474 kernel_interface_t *kernel_interface_create(void);
475
476 #endif /** KERNEL_INTERFACE_H_ @}*/