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