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