Implemented Traffic Flow Confidentiality padding in kernel_interface
[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 tfc Traffic Flow Confidentiality padding 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 u_int8_t protocol, u_int32_t reqid, mark_t mark,
112 u_int32_t tfc, 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, u_int8_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, u_int8_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, u_int8_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 type type of policy, POLICY_(IPSEC|PASS|DROP)
188 * @param sa details about the SA(s) tied to this policy
189 * @param mark mark for this policy
190 * @param routed TRUE, if this policy is routed in the kernel
191 * @return SUCCESS if operation completed
192 */
193 status_t (*add_policy) (kernel_interface_t *this,
194 host_t *src, host_t *dst,
195 traffic_selector_t *src_ts,
196 traffic_selector_t *dst_ts,
197 policy_dir_t direction, policy_type_t type,
198 ipsec_sa_cfg_t *sa, mark_t mark, bool routed);
199
200 /**
201 * Query the use time of a policy.
202 *
203 * The use time of a policy is the time the policy was used
204 * for the last time.
205 *
206 * @param src_ts traffic selector to match traffic source
207 * @param dst_ts traffic selector to match traffic dest
208 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
209 * @param mark optional mark
210 * @param[out] use_time the time of this SA's last use
211 * @return SUCCESS if operation completed
212 */
213 status_t (*query_policy) (kernel_interface_t *this,
214 traffic_selector_t *src_ts,
215 traffic_selector_t *dst_ts,
216 policy_dir_t direction, mark_t mark,
217 u_int32_t *use_time);
218
219 /**
220 * Remove a policy from the SPD.
221 *
222 * The kernel interface implements reference counting for policies.
223 * If the same policy is installed multiple times (in the case of rekeying),
224 * the reference counter is increased. del_policy() decreases the ref counter
225 * and removes the policy only when no more references are available.
226 *
227 * @param src_ts traffic selector to match traffic source
228 * @param dst_ts traffic selector to match traffic dest
229 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
230 * @param mark optional mark
231 * @param unrouted TRUE, if this policy is unrouted from the kernel
232 * @return SUCCESS if operation completed
233 */
234 status_t (*del_policy) (kernel_interface_t *this,
235 traffic_selector_t *src_ts,
236 traffic_selector_t *dst_ts,
237 policy_dir_t direction, mark_t mark,
238 bool unrouted);
239
240 /**
241 * Get our outgoing source address for a destination.
242 *
243 * Does a route lookup to get the source address used to reach dest.
244 * The returned host is allocated and must be destroyed.
245 * An optional src address can be used to check if a route is available
246 * for given source to dest.
247 *
248 * @param dest target destination address
249 * @param src source address to check, or NULL
250 * @return outgoing source address, NULL if unreachable
251 */
252 host_t* (*get_source_addr)(kernel_interface_t *this,
253 host_t *dest, host_t *src);
254
255 /**
256 * Get the next hop for a destination.
257 *
258 * Does a route lookup to get the next hop used to reach dest.
259 * The returned host is allocated and must be destroyed.
260 *
261 * @param dest target destination address
262 * @return next hop address, NULL if unreachable
263 */
264 host_t* (*get_nexthop)(kernel_interface_t *this, host_t *dest);
265
266 /**
267 * Get the interface name of a local address.
268 *
269 * @param host address to get interface name from
270 * @return allocated interface name, or NULL if not found
271 */
272 char* (*get_interface) (kernel_interface_t *this, host_t *host);
273
274 /**
275 * Creates an enumerator over all local addresses.
276 *
277 * This function blocks an internal cached address list until the
278 * enumerator gets destroyed.
279 * The hosts are read-only, do not modify of free.
280 *
281 * @param include_down_ifaces TRUE to enumerate addresses from down interfaces
282 * @param include_virtual_ips TRUE to enumerate virtual ip addresses
283 * @return enumerator over host_t's
284 */
285 enumerator_t *(*create_address_enumerator) (kernel_interface_t *this,
286 bool include_down_ifaces, bool include_virtual_ips);
287
288 /**
289 * Add a virtual IP to an interface.
290 *
291 * Virtual IPs are attached to an interface. If an IP is added multiple
292 * times, the IP is refcounted and not removed until del_ip() was called
293 * as many times as add_ip().
294 * The virtual IP is attached to the interface where the iface_ip is found.
295 *
296 * @param virtual_ip virtual ip address to assign
297 * @param iface_ip IP of an interface to attach virtual IP
298 * @return SUCCESS if operation completed
299 */
300 status_t (*add_ip) (kernel_interface_t *this, host_t *virtual_ip,
301 host_t *iface_ip);
302
303 /**
304 * Remove a virtual IP from an interface.
305 *
306 * The kernel interface uses refcounting, see add_ip().
307 *
308 * @param virtual_ip virtual ip address to assign
309 * @return SUCCESS if operation completed
310 */
311 status_t (*del_ip) (kernel_interface_t *this, host_t *virtual_ip);
312
313 /**
314 * Add a route.
315 *
316 * @param dst_net destination net
317 * @param prefixlen destination net prefix length
318 * @param gateway gateway for this route
319 * @param src_ip sourc ip of the route
320 * @param if_name name of the interface the route is bound to
321 * @return SUCCESS if operation completed
322 * ALREADY_DONE if the route already exists
323 */
324 status_t (*add_route) (kernel_interface_t *this, chunk_t dst_net,
325 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
326 char *if_name);
327
328 /**
329 * Delete a route.
330 *
331 * @param dst_net destination net
332 * @param prefixlen destination net prefix length
333 * @param gateway gateway for this route
334 * @param src_ip sourc ip of the route
335 * @param if_name name of the interface the route is bound to
336 * @return SUCCESS if operation completed
337 */
338 status_t (*del_route) (kernel_interface_t *this, chunk_t dst_net,
339 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
340 char *if_name);
341
342 /**
343 * Set up a bypass policy for a given socket.
344 *
345 * @param fd socket file descriptor to setup policy for
346 * @param family protocol family of the socket
347 * @return TRUE of policy set up successfully
348 */
349 bool (*bypass_socket)(kernel_interface_t *this, int fd, int family);
350
351 /**
352 * manager methods
353 */
354
355 /**
356 * Tries to find an ip address of a local interface that is included in the
357 * supplied traffic selector.
358 *
359 * @param ts traffic selector
360 * @param ip returned ip (has to be destroyed)
361 * @return SUCCESS if address found
362 */
363 status_t (*get_address_by_ts)(kernel_interface_t *this,
364 traffic_selector_t *ts, host_t **ip);
365
366 /**
367 * Register an ipsec kernel interface constructor on the manager.
368 *
369 * @param create constructor to register
370 */
371 void (*add_ipsec_interface)(kernel_interface_t *this,
372 kernel_ipsec_constructor_t create);
373
374 /**
375 * Unregister an ipsec kernel interface constructor.
376 *
377 * @param create constructor to unregister
378 */
379 void (*remove_ipsec_interface)(kernel_interface_t *this,
380 kernel_ipsec_constructor_t create);
381
382 /**
383 * Register a network kernel interface constructor on the manager.
384 *
385 * @param create constructor to register
386 */
387 void (*add_net_interface)(kernel_interface_t *this,
388 kernel_net_constructor_t create);
389
390 /**
391 * Unregister a network kernel interface constructor.
392 *
393 * @param create constructor to unregister
394 */
395 void (*remove_net_interface)(kernel_interface_t *this,
396 kernel_net_constructor_t create);
397
398 /**
399 * Add a listener to the kernel interface.
400 *
401 * @param listener listener to add
402 */
403 void (*add_listener)(kernel_interface_t *this,
404 kernel_listener_t *listener);
405
406 /**
407 * Remove a listener from the kernel interface.
408 *
409 * @param listener listener to remove
410 */
411 void (*remove_listener)(kernel_interface_t *this,
412 kernel_listener_t *listener);
413
414 /**
415 * Raise an acquire event.
416 *
417 * @param reqid reqid of the policy to acquire
418 * @param src_ts source traffic selector
419 * @param dst_ts destination traffic selector
420 */
421 void (*acquire)(kernel_interface_t *this, u_int32_t reqid,
422 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
423
424 /**
425 * Raise an expire event.
426 *
427 * @param reqid reqid of the expired SA
428 * @param protocol protocol of the expired SA
429 * @param spi spi of the expired SA
430 * @param hard TRUE if it is a hard expire, FALSE otherwise
431 */
432 void (*expire)(kernel_interface_t *this, u_int32_t reqid,
433 u_int8_t protocol, u_int32_t spi, bool hard);
434
435 /**
436 * Raise a mapping event.
437 *
438 * @param reqid reqid of the SA
439 * @param spi spi of the SA
440 * @param remote new remote host
441 */
442 void (*mapping)(kernel_interface_t *this, u_int32_t reqid, u_int32_t spi,
443 host_t *remote);
444
445 /**
446 * Raise a migrate event.
447 *
448 * @param reqid reqid of the policy
449 * @param src_ts source traffic selector
450 * @param dst_ts destination traffic selector
451 * @param direction direction of the policy (in|out)
452 * @param local local host address to be used in the IKE_SA
453 * @param remote remote host address to be used in the IKE_SA
454 */
455 void (*migrate)(kernel_interface_t *this, u_int32_t reqid,
456 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
457 policy_dir_t direction, host_t *local, host_t *remote);
458
459 /**
460 * Raise a roam event.
461 *
462 * @param address TRUE if address list, FALSE if routing changed
463 */
464 void (*roam)(kernel_interface_t *this, bool address);
465
466 /**
467 * Destroys a kernel_interface_manager_t object.
468 */
469 void (*destroy) (kernel_interface_t *this);
470 };
471
472 /**
473 * Creates an object of type kernel_interface_t.
474 */
475 kernel_interface_t *kernel_interface_create(void);
476
477 #endif /** KERNEL_INTERFACE_H_ @}*/