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