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