Introduce "features" for the kernel backends returning kernel capabilities
[strongswan.git] / src / libhydra / kernel / kernel_net.h
1 /*
2 * Copyright (C) 2008-2012 Tobias Brunner
3 * Copyright (C) 2007 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /**
18 * @defgroup kernel_net kernel_net
19 * @{ @ingroup hkernel
20 */
21
22 #ifndef KERNEL_NET_H_
23 #define KERNEL_NET_H_
24
25 typedef struct kernel_net_t kernel_net_t;
26 typedef enum kernel_address_type_t kernel_address_type_t;
27
28 #include <collections/enumerator.h>
29 #include <networking/host.h>
30 #include <plugins/plugin.h>
31 #include <kernel/kernel_interface.h>
32
33 /**
34 * Type of addresses (e.g. when enumerating them)
35 */
36 enum kernel_address_type_t {
37 /** normal addresses (on regular, up, non-ignored) interfaces */
38 ADDR_TYPE_REGULAR = 0,
39 /** addresses on down interfaces */
40 ADDR_TYPE_DOWN = (1 << 0),
41 /** addresses on ignored interfaces */
42 ADDR_TYPE_IGNORED = (1 << 1),
43 /** addresses on loopback interfaces */
44 ADDR_TYPE_LOOPBACK = (1 << 2),
45 /** virtual IP addresses */
46 ADDR_TYPE_VIRTUAL = (1 << 3),
47 /** to enumerate all available addresses */
48 ADDR_TYPE_ALL = (1 << 4) - 1,
49 };
50
51 /**
52 * Interface to the network subsystem of the kernel.
53 *
54 * The kernel network interface handles the communication with the kernel
55 * for interface and IP address management.
56 */
57 struct kernel_net_t {
58
59 /**
60 * Get the feature set supported by this kernel backend.
61 *
62 * @return ORed feature-set of backend
63 */
64 kernel_feature_t (*get_features)(kernel_net_t *this);
65
66 /**
67 * Get our outgoing source address for a destination.
68 *
69 * Does a route lookup to get the source address used to reach dest.
70 * The returned host is allocated and must be destroyed.
71 * An optional src address can be used to check if a route is available
72 * for the given source to dest.
73 *
74 * @param dest target destination address
75 * @param src source address to check, or NULL
76 * @return outgoing source address, NULL if unreachable
77 */
78 host_t* (*get_source_addr)(kernel_net_t *this, host_t *dest, host_t *src);
79
80 /**
81 * Get the next hop for a destination.
82 *
83 * Does a route lookup to get the next hop used to reach dest.
84 * The returned host is allocated and must be destroyed.
85 * An optional src address can be used to check if a route is available
86 * for the given source to dest.
87 *
88 * @param dest target destination address
89 * @param src source address to check, or NULL
90 * @return next hop address, NULL if unreachable
91 */
92 host_t* (*get_nexthop)(kernel_net_t *this, host_t *dest, host_t *src);
93
94 /**
95 * Get the interface name of a local address. Interfaces that are down or
96 * ignored by config are not considered.
97 *
98 * @param host address to get interface name from
99 * @param name allocated interface name (optional)
100 * @return TRUE if interface found and usable
101 */
102 bool (*get_interface) (kernel_net_t *this, host_t *host, char **name);
103
104 /**
105 * Creates an enumerator over all local addresses.
106 *
107 * This function blocks an internal cached address list until the
108 * enumerator gets destroyed.
109 * The hosts are read-only, do not modify of free.
110 *
111 * @param which a combination of address types to enumerate
112 * @return enumerator over host_t's
113 */
114 enumerator_t *(*create_address_enumerator) (kernel_net_t *this,
115 kernel_address_type_t which);
116
117 /**
118 * Add a virtual IP to an interface.
119 *
120 * Virtual IPs are attached to an interface. If an IP is added multiple
121 * times, the IP is refcounted and not removed until del_ip() was called
122 * as many times as add_ip().
123 *
124 * @param virtual_ip virtual ip address to assign
125 * @param prefix prefix length to install with IP address, -1 for auto
126 * @param iface interface to install virtual IP on
127 * @return SUCCESS if operation completed
128 */
129 status_t (*add_ip) (kernel_net_t *this, host_t *virtual_ip, int prefix,
130 char *iface);
131
132 /**
133 * Remove a virtual IP from an interface.
134 *
135 * The kernel interface uses refcounting, see add_ip().
136 *
137 * @param virtual_ip virtual ip address to assign
138 * @param prefix prefix length of the IP to uninstall, -1 for auto
139 * @param wait TRUE to wait until IP is gone
140 * @return SUCCESS if operation completed
141 */
142 status_t (*del_ip) (kernel_net_t *this, host_t *virtual_ip, int prefix,
143 bool wait);
144
145 /**
146 * Add a route.
147 *
148 * @param dst_net destination net
149 * @param prefixlen destination net prefix length
150 * @param gateway gateway for this route
151 * @param src_ip sourc ip of the route
152 * @param if_name name of the interface the route is bound to
153 * @return SUCCESS if operation completed
154 * ALREADY_DONE if the route already exists
155 */
156 status_t (*add_route) (kernel_net_t *this, chunk_t dst_net,
157 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
158 char *if_name);
159
160 /**
161 * Delete a route.
162 *
163 * @param dst_net destination net
164 * @param prefixlen destination net prefix length
165 * @param gateway gateway for this route
166 * @param src_ip sourc ip of the route
167 * @param if_name name of the interface the route is bound to
168 * @return SUCCESS if operation completed
169 */
170 status_t (*del_route) (kernel_net_t *this, chunk_t dst_net,
171 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
172 char *if_name);
173
174 /**
175 * Destroy the implementation.
176 */
177 void (*destroy) (kernel_net_t *this);
178 };
179
180 /**
181 * Helper function to (un-)register net kernel interfaces from plugin features.
182 *
183 * This function is a plugin_feature_callback_t and can be used with the
184 * PLUGIN_CALLBACK macro to register an net kernel interface constructor.
185 *
186 * @param plugin plugin registering the kernel interface
187 * @param feature associated plugin feature
188 * @param reg TRUE to register, FALSE to unregister
189 * @param data data passed to callback, an kernel_net_constructor_t
190 */
191 bool kernel_net_register(plugin_t *plugin, plugin_feature_t *feature,
192 bool reg, void *data);
193
194 #endif /** KERNEL_NET_H_ @}*/