Moved data structures to new collections subfolder
[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
32 /**
33 * Type of addresses (e.g. when enumerating them)
34 */
35 enum kernel_address_type_t {
36 /** normal addresses (on regular, up, non-ignored) interfaces */
37 ADDR_TYPE_REGULAR = 0,
38 /** addresses on down interfaces */
39 ADDR_TYPE_DOWN = (1 << 0),
40 /** addresses on ignored interfaces */
41 ADDR_TYPE_IGNORED = (1 << 1),
42 /** addresses on loopback interfaces */
43 ADDR_TYPE_LOOPBACK = (1 << 2),
44 /** virtual IP addresses */
45 ADDR_TYPE_VIRTUAL = (1 << 3),
46 /** to enumerate all available addresses */
47 ADDR_TYPE_ALL = (1 << 4) - 1,
48 };
49
50 /**
51 * Interface to the network subsystem of the kernel.
52 *
53 * The kernel network interface handles the communication with the kernel
54 * for interface and IP address management.
55 */
56 struct kernel_net_t {
57
58 /**
59 * Get our outgoing source address for a destination.
60 *
61 * Does a route lookup to get the source address used to reach dest.
62 * The returned host is allocated and must be destroyed.
63 * An optional src address can be used to check if a route is available
64 * for the given source to dest.
65 *
66 * @param dest target destination address
67 * @param src source address to check, or NULL
68 * @return outgoing source address, NULL if unreachable
69 */
70 host_t* (*get_source_addr)(kernel_net_t *this, host_t *dest, host_t *src);
71
72 /**
73 * Get the next hop for a destination.
74 *
75 * Does a route lookup to get the next hop used to reach dest.
76 * The returned host is allocated and must be destroyed.
77 * An optional src address can be used to check if a route is available
78 * for the given source to dest.
79 *
80 * @param dest target destination address
81 * @param src source address to check, or NULL
82 * @return next hop address, NULL if unreachable
83 */
84 host_t* (*get_nexthop)(kernel_net_t *this, host_t *dest, host_t *src);
85
86 /**
87 * Get the interface name of a local address. Interfaces that are down or
88 * ignored by config are not considered.
89 *
90 * @param host address to get interface name from
91 * @param name allocated interface name (optional)
92 * @return TRUE if interface found and usable
93 */
94 bool (*get_interface) (kernel_net_t *this, host_t *host, char **name);
95
96 /**
97 * Creates an enumerator over all local addresses.
98 *
99 * This function blocks an internal cached address list until the
100 * enumerator gets destroyed.
101 * The hosts are read-only, do not modify of free.
102 *
103 * @param which a combination of address types to enumerate
104 * @return enumerator over host_t's
105 */
106 enumerator_t *(*create_address_enumerator) (kernel_net_t *this,
107 kernel_address_type_t which);
108
109 /**
110 * Add a virtual IP to an interface.
111 *
112 * Virtual IPs are attached to an interface. If an IP is added multiple
113 * times, the IP is refcounted and not removed until del_ip() was called
114 * as many times as add_ip().
115 * The virtual IP is attached to the interface where the iface_ip is found.
116 *
117 * @param virtual_ip virtual ip address to assign
118 * @param iface_ip IP of an interface to attach virtual IP
119 * @return SUCCESS if operation completed
120 */
121 status_t (*add_ip) (kernel_net_t *this, host_t *virtual_ip,
122 host_t *iface_ip);
123
124 /**
125 * Remove a virtual IP from an interface.
126 *
127 * The kernel interface uses refcounting, see add_ip().
128 *
129 * @param virtual_ip virtual ip address to assign
130 * @return SUCCESS if operation completed
131 */
132 status_t (*del_ip) (kernel_net_t *this, host_t *virtual_ip);
133
134 /**
135 * Add a route.
136 *
137 * @param dst_net destination net
138 * @param prefixlen destination net prefix length
139 * @param gateway gateway for this route
140 * @param src_ip sourc ip of the route
141 * @param if_name name of the interface the route is bound to
142 * @return SUCCESS if operation completed
143 * ALREADY_DONE if the route already exists
144 */
145 status_t (*add_route) (kernel_net_t *this, chunk_t dst_net,
146 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
147 char *if_name);
148
149 /**
150 * Delete a route.
151 *
152 * @param dst_net destination net
153 * @param prefixlen destination net prefix length
154 * @param gateway gateway for this route
155 * @param src_ip sourc ip of the route
156 * @param if_name name of the interface the route is bound to
157 * @return SUCCESS if operation completed
158 */
159 status_t (*del_route) (kernel_net_t *this, chunk_t dst_net,
160 u_int8_t prefixlen, host_t *gateway, host_t *src_ip,
161 char *if_name);
162
163 /**
164 * Destroy the implementation.
165 */
166 void (*destroy) (kernel_net_t *this);
167 };
168
169 /**
170 * Helper function to (un-)register net kernel interfaces from plugin features.
171 *
172 * This function is a plugin_feature_callback_t and can be used with the
173 * PLUGIN_CALLBACK macro to register an net kernel interface constructor.
174 *
175 * @param plugin plugin registering the kernel interface
176 * @param feature associated plugin feature
177 * @param reg TRUE to register, FALSE to unregister
178 * @param data data passed to callback, an kernel_net_constructor_t
179 */
180 bool kernel_net_register(plugin_t *plugin, plugin_feature_t *feature,
181 bool reg, void *data);
182
183 #endif /** KERNEL_NET_H_ @}*/