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