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