Filter ignored interfaces in kernel interfaces (for events, address enumeration,...
[strongswan.git] / src / libhydra / kernel / kernel_interface.h
1 /*
2 * Copyright (C) 2006-2012 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 * Copyright (c) 2012 Nanoteq Pty Ltd
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 /**
42 * @defgroup kernel_interface kernel_interface
43 * @{ @ingroup hkernel
44 */
45
46 #ifndef KERNEL_INTERFACE_H_
47 #define KERNEL_INTERFACE_H_
48
49 typedef struct kernel_interface_t kernel_interface_t;
50
51 #include <utils/host.h>
52 #include <crypto/prf_plus.h>
53
54 #include <kernel/kernel_listener.h>
55 #include <kernel/kernel_ipsec.h>
56 #include <kernel/kernel_net.h>
57
58 /**
59 * Constructor function for ipsec kernel interface
60 */
61 typedef kernel_ipsec_t* (*kernel_ipsec_constructor_t)(void);
62
63 /**
64 * Constructor function for network kernel interface
65 */
66 typedef kernel_net_t* (*kernel_net_constructor_t)(void);
67
68 /**
69 * Manager and wrapper for different kernel interfaces.
70 *
71 * The kernel interface handles the communication with the kernel
72 * for SA and policy management and interface and IP address management.
73 */
74 struct kernel_interface_t {
75
76 /**
77 * Get a SPI from the kernel.
78 *
79 * @param src source address of SA
80 * @param dst destination address of SA
81 * @param protocol protocol for SA (ESP/AH)
82 * @param reqid unique ID for this SA
83 * @param spi allocated spi
84 * @return SUCCESS if operation completed
85 */
86 status_t (*get_spi)(kernel_interface_t *this, host_t *src, host_t *dst,
87 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi);
88
89 /**
90 * Get a Compression Parameter Index (CPI) from the kernel.
91 *
92 * @param src source address of SA
93 * @param dst destination address of SA
94 * @param reqid unique ID for the corresponding SA
95 * @param cpi allocated cpi
96 * @return SUCCESS if operation completed
97 */
98 status_t (*get_cpi)(kernel_interface_t *this, host_t *src, host_t *dst,
99 u_int32_t reqid, u_int16_t *cpi);
100
101 /**
102 * Add an SA to the SAD.
103 *
104 * add_sa() may update an already allocated
105 * SPI (via get_spi). In this case, the replace
106 * flag must be set.
107 * This function does install a single SA for a
108 * single protocol in one direction.
109 *
110 * @param src source address for this SA
111 * @param dst destination address for this SA
112 * @param spi SPI allocated by us or remote peer
113 * @param protocol protocol for this SA (ESP/AH)
114 * @param reqid unique ID for this SA
115 * @param mark optional mark for this SA
116 * @param tfc Traffic Flow Confidentiality padding for this SA
117 * @param lifetime lifetime_cfg_t for this SA
118 * @param enc_alg Algorithm to use for encryption (ESP only)
119 * @param enc_key key to use for encryption
120 * @param int_alg Algorithm to use for integrity protection
121 * @param int_key key to use for integrity protection
122 * @param mode mode of the SA (tunnel, transport)
123 * @param ipcomp IPComp transform to use
124 * @param cpi CPI for IPComp
125 * @param encap enable UDP encapsulation for NAT traversal
126 * @param esn TRUE to use Extended Sequence Numbers
127 * @param inbound TRUE if this is an inbound SA
128 * @param src_ts traffic selector with BEET source address
129 * @param dst_ts traffic selector with BEET destination address
130 * @return SUCCESS if operation completed
131 */
132 status_t (*add_sa) (kernel_interface_t *this,
133 host_t *src, host_t *dst, u_int32_t spi,
134 u_int8_t protocol, u_int32_t reqid, mark_t mark,
135 u_int32_t tfc, lifetime_cfg_t *lifetime,
136 u_int16_t enc_alg, chunk_t enc_key,
137 u_int16_t int_alg, chunk_t int_key,
138 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
139 bool encap, bool esn, bool inbound,
140 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
141
142 /**
143 * Update the hosts on an installed SA.
144 *
145 * We cannot directly update the destination address as the kernel
146 * requires the spi, the protocol AND the destination address (and family)
147 * to identify SAs. Therefore if the destination address changed we
148 * create a new SA and delete the old one.
149 *
150 * @param spi SPI of the SA
151 * @param protocol protocol for this SA (ESP/AH)
152 * @param cpi CPI for IPComp, 0 if no IPComp is used
153 * @param src current source address
154 * @param dst current destination address
155 * @param new_src new source address
156 * @param new_dst new destination address
157 * @param encap current use of UDP encapsulation
158 * @param new_encap new use of UDP encapsulation
159 * @param mark optional mark for this SA
160 * @return SUCCESS if operation completed, NOT_SUPPORTED if
161 * the kernel interface can't update the SA
162 */
163 status_t (*update_sa)(kernel_interface_t *this,
164 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
165 host_t *src, host_t *dst,
166 host_t *new_src, host_t *new_dst,
167 bool encap, bool new_encap, mark_t mark);
168
169 /**
170 * Query the number of bytes processed by an SA from the SAD.
171 *
172 * @param src source address for this SA
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 * @param mark optional mark for this SA
177 * @param[out] bytes the number of bytes processed by SA
178 * @return SUCCESS if operation completed
179 */
180 status_t (*query_sa) (kernel_interface_t *this, host_t *src, host_t *dst,
181 u_int32_t spi, u_int8_t protocol, mark_t mark,
182 u_int64_t *bytes);
183
184 /**
185 * Delete a previously installed SA from the SAD.
186 *
187 * @param src source address for this SA
188 * @param dst destination address for this SA
189 * @param spi SPI allocated by us or remote peer
190 * @param protocol protocol for this SA (ESP/AH)
191 * @param cpi CPI for IPComp or 0
192 * @param mark optional mark for this SA
193 * @return SUCCESS if operation completed
194 */
195 status_t (*del_sa) (kernel_interface_t *this, host_t *src, host_t *dst,
196 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
197 mark_t mark);
198
199 /**
200 * Flush all SAs from the SAD.
201 *
202 * @return SUCCESS if operation completed
203 */
204 status_t (*flush_sas) (kernel_interface_t *this);
205
206 /**
207 * Add a policy to the SPD.
208 *
209 * A policy is always associated to an SA. Traffic which matches a
210 * policy is handled by the SA with the same reqid.
211 *
212 * @param src source address of SA
213 * @param dst dest address of SA
214 * @param src_ts traffic selector to match traffic source
215 * @param dst_ts traffic selector to match traffic dest
216 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
217 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
218 * @param sa details about the SA(s) tied to this policy
219 * @param mark mark for this policy
220 * @param priority priority of this policy
221 * @return SUCCESS if operation completed
222 */
223 status_t (*add_policy) (kernel_interface_t *this,
224 host_t *src, host_t *dst,
225 traffic_selector_t *src_ts,
226 traffic_selector_t *dst_ts,
227 policy_dir_t direction, policy_type_t type,
228 ipsec_sa_cfg_t *sa, mark_t mark,
229 policy_priority_t priority);
230
231 /**
232 * Query the use time of a policy.
233 *
234 * The use time of a policy is the time the policy was used
235 * for the last time.
236 *
237 * @param src_ts traffic selector to match traffic source
238 * @param dst_ts traffic selector to match traffic dest
239 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
240 * @param mark optional mark
241 * @param[out] use_time the time of this SA's last use
242 * @return SUCCESS if operation completed
243 */
244 status_t (*query_policy) (kernel_interface_t *this,
245 traffic_selector_t *src_ts,
246 traffic_selector_t *dst_ts,
247 policy_dir_t direction, mark_t mark,
248 u_int32_t *use_time);
249
250 /**
251 * Remove a policy from the SPD.
252 *
253 * The kernel interface implements reference counting for policies.
254 * If the same policy is installed multiple times (in the case of rekeying),
255 * the reference counter is increased. del_policy() decreases the ref counter
256 * and removes the policy only when no more references are available.
257 *
258 * @param src_ts traffic selector to match traffic source
259 * @param dst_ts traffic selector to match traffic dest
260 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
261 * @param reqid unique ID of the associated SA
262 * @param mark optional mark
263 * @param priority priority of the policy
264 * @return SUCCESS if operation completed
265 */
266 status_t (*del_policy) (kernel_interface_t *this,
267 traffic_selector_t *src_ts,
268 traffic_selector_t *dst_ts,
269 policy_dir_t direction, u_int32_t reqid,
270 mark_t mark, policy_priority_t priority);
271
272 /**
273 * Flush all policies from the SPD.
274 *
275 * @return SUCCESS if operation completed
276 */
277 status_t (*flush_policies) (kernel_interface_t *this);
278
279 /**
280 * Get our outgoing source address for a destination.
281 *
282 * Does a route lookup to get the source address used to reach dest.
283 * The returned host is allocated and must be destroyed.
284 * An optional src address can be used to check if a route is available
285 * for the given source to dest.
286 *
287 * @param dest target destination address
288 * @param src source address to check, or NULL
289 * @return outgoing source address, NULL if unreachable
290 */
291 host_t* (*get_source_addr)(kernel_interface_t *this,
292 host_t *dest, host_t *src);
293
294 /**
295 * Get the next hop for a destination.
296 *
297 * Does a route lookup to get the next hop used to reach dest.
298 * The returned host is allocated and must be destroyed.
299 * An optional src address can be used to check if a route is available
300 * for the given source to dest.
301 *
302 * @param dest target destination address
303 * @return next hop address, NULL if unreachable
304 */
305 host_t* (*get_nexthop)(kernel_interface_t *this, host_t *dest, host_t *src);
306
307 /**
308 * Get the interface name of a local address. Interfaces that are down or
309 * ignored by config are not considered.
310 *
311 * @param host address to get interface name from
312 * @param name allocated interface name (optional)
313 * @return TRUE if interface found and usable
314 */
315 bool (*get_interface) (kernel_interface_t *this, host_t *host, char **name);
316
317 /**
318 * Creates an enumerator over all local addresses.
319 *
320 * This function blocks an internal cached address list until the
321 * enumerator gets destroyed.
322 * The hosts are read-only, do not modify of free.
323 *
324 * @param include_down_ifaces TRUE to enumerate addresses from down interfaces
325 * @param include_virtual_ips TRUE to enumerate virtual IP addresses
326 * @param include_loopback TRUE to enumerate addresses on loopback interfaces
327 * @return enumerator over host_t's
328 */
329 enumerator_t *(*create_address_enumerator) (kernel_interface_t *this,
330 bool include_down_ifaces, bool include_virtual_ips,
331 bool include_loopback);
332
333 /**
334 * Add a virtual IP to an interface.
335 *
336 * Virtual IPs are attached to an interface. If an IP is added multiple
337 * times, the IP is refcounted and not removed until del_ip() was called
338 * as many times as add_ip().
339 * The virtual IP is attached to the interface where the iface_ip is found.
340 *
341 * @param virtual_ip virtual ip address to assign
342 * @param iface_ip IP of an interface to attach virtual IP
343 * @return SUCCESS if operation completed
344 */
345 status_t (*add_ip) (kernel_interface_t *this, host_t *virtual_ip,
346 host_t *iface_ip);
347
348 /**
349 * Remove a virtual IP from an interface.
350 *
351 * The kernel interface uses refcounting, see add_ip().
352 *
353 * @param virtual_ip virtual ip address to assign
354 * @return SUCCESS if operation completed
355 */
356 status_t (*del_ip) (kernel_interface_t *this, host_t *virtual_ip);
357
358 /**
359 * Add a route.
360 *
361 * @param dst_net destination net
362 * @param prefixlen destination net prefix length
363 * @param gateway gateway for this route
364 * @param src_ip sourc ip of the route
365 * @param if_name name of the interface the route is bound to
366 * @return SUCCESS if operation completed
367 * ALREADY_DONE if the route already exists
368 */
369 status_t (*add_route) (kernel_interface_t *this, chunk_t dst_net,
370 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
371 char *if_name);
372
373 /**
374 * Delete a route.
375 *
376 * @param dst_net destination net
377 * @param prefixlen destination net prefix length
378 * @param gateway gateway for this route
379 * @param src_ip sourc ip of the route
380 * @param if_name name of the interface the route is bound to
381 * @return SUCCESS if operation completed
382 */
383 status_t (*del_route) (kernel_interface_t *this, chunk_t dst_net,
384 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
385 char *if_name);
386
387 /**
388 * Set up a bypass policy for a given socket.
389 *
390 * @param fd socket file descriptor to setup policy for
391 * @param family protocol family of the socket
392 * @return TRUE if policy set up successfully
393 */
394 bool (*bypass_socket)(kernel_interface_t *this, int fd, int family);
395
396 /**
397 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
398 *
399 * @param fd socket file descriptor
400 * @param family protocol family of the socket
401 * @param port the UDP port
402 * @return TRUE if UDP decapsulation was enabled successfully
403 */
404 bool (*enable_udp_decap)(kernel_interface_t *this, int fd, int family,
405 u_int16_t port);
406
407
408 /**
409 * manager methods
410 */
411
412 /**
413 * Verifies that the given interface is usable and not excluded by
414 * configuration.
415 *
416 * @param iface interface name
417 * @return TRUE if usable
418 */
419 bool (*is_interface_usable)(kernel_interface_t *this, const char *iface);
420
421 /**
422 * Tries to find an IP address of a local interface that is included in the
423 * supplied traffic selector.
424 *
425 * @param ts traffic selector
426 * @param ip returned IP address (has to be destroyed)
427 * @return SUCCESS if address found
428 */
429 status_t (*get_address_by_ts)(kernel_interface_t *this,
430 traffic_selector_t *ts, host_t **ip);
431
432 /**
433 * Register an ipsec kernel interface constructor on the manager.
434 *
435 * @param create constructor to register
436 */
437 void (*add_ipsec_interface)(kernel_interface_t *this,
438 kernel_ipsec_constructor_t create);
439
440 /**
441 * Unregister an ipsec kernel interface constructor.
442 *
443 * @param create constructor to unregister
444 */
445 void (*remove_ipsec_interface)(kernel_interface_t *this,
446 kernel_ipsec_constructor_t create);
447
448 /**
449 * Register a network kernel interface constructor on the manager.
450 *
451 * @param create constructor to register
452 */
453 void (*add_net_interface)(kernel_interface_t *this,
454 kernel_net_constructor_t create);
455
456 /**
457 * Unregister a network kernel interface constructor.
458 *
459 * @param create constructor to unregister
460 */
461 void (*remove_net_interface)(kernel_interface_t *this,
462 kernel_net_constructor_t create);
463
464 /**
465 * Add a listener to the kernel interface.
466 *
467 * @param listener listener to add
468 */
469 void (*add_listener)(kernel_interface_t *this,
470 kernel_listener_t *listener);
471
472 /**
473 * Remove a listener from the kernel interface.
474 *
475 * @param listener listener to remove
476 */
477 void (*remove_listener)(kernel_interface_t *this,
478 kernel_listener_t *listener);
479
480 /**
481 * Raise an acquire event.
482 *
483 * @param reqid reqid of the policy to acquire
484 * @param src_ts source traffic selector
485 * @param dst_ts destination traffic selector
486 */
487 void (*acquire)(kernel_interface_t *this, u_int32_t reqid,
488 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
489
490 /**
491 * Raise an expire event.
492 *
493 * @param reqid reqid of the expired SA
494 * @param protocol protocol of the expired SA
495 * @param spi spi of the expired SA
496 * @param hard TRUE if it is a hard expire, FALSE otherwise
497 */
498 void (*expire)(kernel_interface_t *this, u_int32_t reqid,
499 u_int8_t protocol, u_int32_t spi, bool hard);
500
501 /**
502 * Raise a mapping event.
503 *
504 * @param reqid reqid of the SA
505 * @param spi spi of the SA
506 * @param remote new remote host
507 */
508 void (*mapping)(kernel_interface_t *this, u_int32_t reqid, u_int32_t spi,
509 host_t *remote);
510
511 /**
512 * Raise a migrate event.
513 *
514 * @param reqid reqid of the policy
515 * @param src_ts source traffic selector
516 * @param dst_ts destination traffic selector
517 * @param direction direction of the policy (in|out)
518 * @param local local host address to be used in the IKE_SA
519 * @param remote remote host address to be used in the IKE_SA
520 */
521 void (*migrate)(kernel_interface_t *this, u_int32_t reqid,
522 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
523 policy_dir_t direction, host_t *local, host_t *remote);
524
525 /**
526 * Raise a roam event.
527 *
528 * @param address TRUE if address list, FALSE if routing changed
529 */
530 void (*roam)(kernel_interface_t *this, bool address);
531
532 /**
533 * Register a new algorithm with the kernel interface.
534 *
535 * @param alg_id the IKE id of the algorithm
536 * @param type the transform type of the algorithm
537 * @param kernel_id the kernel id of the algorithm
538 * @param kernel_name the kernel name of the algorithm
539 */
540 void (*register_algorithm)(kernel_interface_t *this, u_int16_t alg_id,
541 transform_type_t type, u_int16_t kernel_id,
542 char *kernel_name);
543
544 /**
545 * Return the kernel-specific id and/or name for an algorithms depending on
546 * the arguments specified.
547 *
548 * @param alg_id the IKE id of the algorithm
549 * @param type the transform type of the algorithm
550 * @param kernel_id the kernel id of the algorithm (optional)
551 * @param kernel_name the kernel name of the algorithm (optional)
552 * @return TRUE if algorithm was found
553 */
554 bool (*lookup_algorithm)(kernel_interface_t *this, u_int16_t alg_id,
555 transform_type_t type, u_int16_t *kernel_id,
556 char **kernel_name);
557
558 /**
559 * Destroys a kernel_interface_manager_t object.
560 */
561 void (*destroy) (kernel_interface_t *this);
562 };
563
564 /**
565 * Creates an object of type kernel_interface_t.
566 */
567 kernel_interface_t *kernel_interface_create(void);
568
569 #endif /** KERNEL_INTERFACE_H_ @}*/