1 /*
2 * Generic interface to platform specific networking code
3 *
4 * Copyright (C) 2016-2018 Antonio Quartulli <a@unstable.cc>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program (see the file COPYING included with this
17 * distribution); if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef NETWORKING_H_
22 #define NETWORKING_H_
23
24 #include "syshead.h"
25
26 struct context;
27
28 #ifdef ENABLE_SITNL
29 #include "networking_sitnl.h"
30 #elif ENABLE_IPROUTE
31 #include "networking_iproute2.h"
32 #else
33 /* define mock types to ensure code builds on any platform */
34 typedef void *openvpn_net_ctx_t;
35 typedef void *openvpn_net_iface_t;
36
37 static inline int
net_ctx_init(struct context * c,openvpn_net_ctx_t * ctx)38 net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)
39 {
40 return 0;
41 }
42
43 static inline void
net_ctx_reset(openvpn_net_ctx_t * ctx)44 net_ctx_reset(openvpn_net_ctx_t *ctx)
45 {
46 (void)ctx;
47 }
48
49 static inline void
net_ctx_free(openvpn_net_ctx_t * ctx)50 net_ctx_free(openvpn_net_ctx_t *ctx)
51 {
52 (void)ctx;
53 }
54 #endif /* ifdef ENABLE_SITNL */
55
56 #if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
57
58 /**
59 * Initialize the platform specific context object
60 *
61 * @param c openvpn generic context
62 * @param ctx the implementation specific context to initialize
63 *
64 * @return 0 on success, a negative error code otherwise
65 */
66 int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx);
67
68 /**
69 * Release resources allocated by the internal garbage collector
70 *
71 * @param ctx the implementation specific context
72 */
73 void net_ctx_reset(openvpn_net_ctx_t *ctx);
74
75 /**
76 * Release all resources allocated within the platform specific context object
77 *
78 * @param ctx the implementation specific context to release
79 */
80 void net_ctx_free(openvpn_net_ctx_t *ctx);
81
82 /**
83 * Bring interface up or down.
84 *
85 * @param ctx the implementation specific context
86 * @param iface the interface to modify
87 * @param up true if the interface has to be brought up, false otherwise
88 *
89 * @return 0 on success, a negative error code otherwise
90 */
91 int net_iface_up(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
92 bool up);
93
94 /**
95 * Set the MTU for an interface
96 *
97 * @param ctx the implementation specific context
98 * @param iface the interface to modify
99 * @param mtru the new MTU
100 *
101 * @return 0 on success, a negative error code otherwise
102 */
103 int net_iface_mtu_set(openvpn_net_ctx_t *ctx,
104 const openvpn_net_iface_t *iface, uint32_t mtu);
105
106 /**
107 * Add an IPv4 address to an interface
108 *
109 * @param ctx the implementation specific context
110 * @param iface the interface where the address has to be added
111 * @param addr the address to add
112 * @param prefixlen the prefix length of the network associated with the address
113 *
114 * @return 0 on success, a negative error code otherwise
115 */
116 int net_addr_v4_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
117 const in_addr_t *addr, int prefixlen);
118
119 /**
120 * Add an IPv6 address to an interface
121 *
122 * @param ctx the implementation specific context
123 * @param iface the interface where the address has to be added
124 * @param addr the address to add
125 * @param prefixlen the prefix length of the network associated with the address
126 *
127 * @return 0 on success, a negative error code otherwise
128 */
129
130 int net_addr_v6_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
131 const struct in6_addr *addr, int prefixlen);
132
133 /**
134 * Remove an IPv4 from an interface
135 *
136 * @param ctx the implementation specific context
137 * @param iface the interface to remove the address from
138 * @param prefixlen the prefix length of the network associated with the address
139 *
140 * @return 0 on success, a negative error code otherwise
141 */
142 int net_addr_v4_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
143 const in_addr_t *addr, int prefixlen);
144
145 /**
146 * Remove an IPv6 from an interface
147 *
148 * @param ctx the implementation specific context
149 * @param iface the interface to remove the address from
150 * @param prefixlen the prefix length of the network associated with the address
151 *
152 * @return 0 on success, a negative error code otherwise
153 */
154 int net_addr_v6_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
155 const struct in6_addr *addr, int prefixlen);
156
157 /**
158 * Add a point-to-point IPv4 address to an interface
159 *
160 * @param ctx the implementation specific context
161 * @param iface the interface where the address has to be added
162 * @param local the address to add
163 * @param remote the associated p-t-p remote address
164 *
165 * @return 0 on success, a negative error code otherwise
166 */
167 int net_addr_ptp_v4_add(openvpn_net_ctx_t *ctx,
168 const openvpn_net_iface_t *iface,
169 const in_addr_t *local, const in_addr_t *remote);
170
171 /**
172 * Remove a point-to-point IPv4 address from an interface
173 *
174 * @param ctx the implementation specific context
175 * @param iface the interface to remove the address from
176 * @param local the address to remove
177 * @param remote the associated p-t-p remote address
178 *
179 * @return 0 on success, a negative error code otherwise
180 */
181 int net_addr_ptp_v4_del(openvpn_net_ctx_t *ctx,
182 const openvpn_net_iface_t *iface,
183 const in_addr_t *local, const in_addr_t *remote);
184
185
186 /**
187 * Add a route for an IPv4 address/network
188 *
189 * @param ctx the implementation specific context
190 * @param dst the destination of the route
191 * @param prefixlen the length of the prefix of the destination
192 * @param gw the gateway for this route
193 * @param iface the interface for this route (can be NULL)
194 * @param table the table to add this route to (if 0, will be added to the
195 * main table)
196 * @param metric the metric associated with the route
197 *
198 * @return 0 on success, a negative error code otherwise
199 */
200 int net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
201 int prefixlen, const in_addr_t *gw,
202 const openvpn_net_iface_t *iface, uint32_t table,
203 int metric);
204
205 /**
206 * Add a route for an IPv6 address/network
207 *
208 * @param ctx the implementation specific context
209 * @param dst the destination of the route
210 * @param prefixlen the length of the prefix of the destination
211 * @param gw the gateway for this route
212 * @param iface the interface for this route (can be NULL)
213 * @param table the table to add this route to (if 0, will be added to the
214 * main table)
215 * @param metric the metric associated with the route
216 *
217 * @return 0 on success, a negative error code otherwise
218 */
219 int net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
220 int prefixlen, const struct in6_addr *gw,
221 const openvpn_net_iface_t *iface,
222 uint32_t table, int metric);
223
224 /**
225 * Delete a route for an IPv4 address/network
226 *
227 * @param ctx the implementation specific context
228 * @param dst the destination of the route
229 * @param prefixlen the length of the prefix of the destination
230 * @param gw the gateway for this route
231 * @param iface the interface for this route (can be NULL)
232 * @param table the table to add this route to (if 0, will be added to the
233 * main table)
234 * @param metric the metric associated with the route
235 *
236 * @return 0 on success, a negative error code otherwise
237 */
238 int net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
239 int prefixlen, const in_addr_t *gw,
240 const openvpn_net_iface_t *iface, uint32_t table,
241 int metric);
242
243 /**
244 * Delete a route for an IPv4 address/network
245 *
246 * @param ctx the implementation specific context
247 * @param dst the destination of the route
248 * @param prefixlen the length of the prefix of the destination
249 * @param gw the gateway for this route
250 * @param iface the interface for this route (can be NULL)
251 * @param table the table to add this route to (if 0, will be added to the
252 * main table)
253 * @param metric the metric associated with the route
254 *
255 * @return 0 on success, a negative error code otherwise
256 */
257 int net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
258 int prefixlen, const struct in6_addr *gw,
259 const openvpn_net_iface_t *iface,
260 uint32_t table, int metric);
261
262 /**
263 * Retrieve the gateway and outgoing interface for the specified IPv4
264 * address/network
265 *
266 * @param ctx the implementation specific context
267 * @param dst The destination to lookup
268 * @param best_gw Location where the retrieved GW has to be stored
269 * @param best_iface Location where the retrieved interface has to be stored
270 *
271 * @return 0 on success, a negative error code otherwise
272 */
273 int net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
274 in_addr_t *best_gw, openvpn_net_iface_t *best_iface);
275
276 /**
277 * Retrieve the gateway and outgoing interface for the specified IPv6
278 * address/network
279 *
280 * @param ctx the implementation specific context
281 * @param dst The destination to lookup
282 * @param best_gw Location where the retrieved GW has to be stored
283 * @param best_iface Location where the retrieved interface has to be stored
284 *
285 * @return 0 on success, a negative error code otherwise
286 */
287 int net_route_v6_best_gw(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
288 struct in6_addr *best_gw,
289 openvpn_net_iface_t *best_iface);
290
291 #endif /* ENABLE_SITNL || ENABLE_IPROUTE */
292
293 #endif /* NETWORKING_H_ */
294