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