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