OpenVPN
route.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23/*
24 * Support routines for adding/deleting network routes.
25 */
26
27#ifndef ROUTE_H
28#define ROUTE_H
29
30#include "basic.h"
31#include "tun.h"
32#include "misc.h"
33#include "networking.h"
34
35#ifdef _WIN32
36/*
37 * Windows route methods
38 */
39#define ROUTE_METHOD_ADAPTIVE 0 /* try IP helper first then route.exe */
40#define ROUTE_METHOD_IPAPI 1 /* use IP helper API */
41#define ROUTE_METHOD_EXE 2 /* use route.exe */
42#define ROUTE_METHOD_SERVICE 3 /* use the privileged Windows service */
43#define ROUTE_METHOD_MASK 3
44#endif
45
46/*
47 * Route add/delete flags (must stay clear of ROUTE_METHOD bits)
48 */
49#define ROUTE_DELETE_FIRST (1 << 2)
50#define ROUTE_REF_GW (1 << 3)
51
53{
54#define N_ROUTE_BYPASS 8
57};
58
60{
61 /* bits indicating which members below are defined */
62#define RTSA_REMOTE_ENDPOINT (1 << 0)
63#define RTSA_REMOTE_HOST (1 << 1)
64#define RTSA_DEFAULT_METRIC (1 << 2)
65 unsigned int flags;
66
69 int remote_host_local; /* TLA_x value */
73};
74
76{
78 const char *network;
79 const char *netmask;
80 const char *gateway;
82 const char *metric;
83};
84
85/* redirect-gateway flags */
86#define RG_ENABLE (1u << 0)
87#define RG_LOCAL (1u << 1)
88#define RG_DEF1 (1u << 2)
89#define RG_BYPASS_DHCP (1u << 3)
90#define RG_BYPASS_DNS (1u << 4)
91#define RG_REROUTE_GW (1u << 5)
92#define RG_AUTO_LOCAL (1u << 6)
93#define RG_BLOCK_LOCAL (1u << 7)
94
96{
97 unsigned int flags; /* RG_x flags */
99 struct gc_arena *gc;
100};
101
103{
105 const char *prefix; /* e.g. "2001:db8:1::/64" */
106 const char *gateway; /* e.g. "2001:db8:0::2" */
107 const char *metric; /* e.g. "5" */
109};
110
112{
113 unsigned int flags; /* RG_x flags, see route_option-list */
115 struct gc_arena *gc;
116};
117
119{
120#define RT_DEFINED (1u << 0)
121#define RT_ADDED (1u << 1)
122#define RT_METRIC_DEFINED (1u << 2)
124 unsigned int flags;
125 const struct route_option *option;
131};
132
134{
136 unsigned int flags; /* RT_ flags, see route_ipv4 */
137 struct in6_addr network;
138 unsigned int netbits;
139 struct in6_addr gateway;
142 /* gateway interface */
143#ifdef _WIN32
144 DWORD adapter_index; /* interface or ~0 if undefined */
145#else
146 char *iface; /* interface name (null terminated) */
147#endif
148};
149
150
156
158{
159#define RGI_ADDR_DEFINED (1 << 0) /* set if gateway.addr defined */
160#define RGI_NETMASK_DEFINED (1 << 1) /* set if gateway.netmask defined */
161#define RGI_HWADDR_DEFINED (1 << 2) /* set if hwaddr is defined */
162#define RGI_IFACE_DEFINED (1 << 3) /* set if iface is defined */
163#define RGI_OVERFLOW (1 << 4) /* set if more interface addresses than will fit in addrs */
164#define RGI_ON_LINK (1 << 5)
165 unsigned int flags;
166
167 /* gateway interface */
168#ifdef _WIN32
169 DWORD adapter_index; /* interface or ~0 if undefined */
170#elif defined(TARGET_HAIKU)
171 char iface[PATH_MAX]; /* iface names are full /dev path with driver name */
172#else
173 char iface[16]; /* interface name (null terminated), may be empty */
174#endif
175
176 /* gateway interface hardware address */
177 uint8_t hwaddr[6];
178
179 /* gateway/router address */
181
182 /* address/netmask pairs bound to interface */
183#define RGI_N_ADDRESSES 8
184 int n_addrs; /* len of addrs, may be 0 */
185 struct route_gateway_address addrs[RGI_N_ADDRESSES]; /* local addresses attached to iface */
186};
187
189{
190 struct in6_addr addr_ipv6;
192};
193
195{
196 /* RGI_ flags used as in route_gateway_info */
197 unsigned int flags;
198
199 /* gateway interface */
200#ifdef _WIN32
201 DWORD adapter_index; /* interface or ~0 if undefined */
202#else
203 /* non linux platform don't have this constant defined */
204#ifndef IFNAMSIZ
205#if defined(TARGET_HAIKU)
206/* iface names are full /dev path with driver name */
207#define IFNAMSIZ PATH_MAX
208#else
209#define IFNAMSIZ 16
210#endif
211#endif
212 char iface[IFNAMSIZ]; /* interface name (null terminated), may be empty */
213#endif
214
215 /* gateway interface hardware address */
216 uint8_t hwaddr[6];
217
218 /* gateway/router address */
220
221 /* address/netmask pairs bound to interface */
222#define RGI_N_ADDRESSES 8
223 int n_addrs; /* len of addrs, may be 0 */
225 addrs[RGI_N_ADDRESSES]; /* local addresses attached to iface */
226};
227
229{
230#define RL_DID_REDIRECT_DEFAULT_GATEWAY (1u << 0)
231#define RL_DID_LOCAL (1u << 1)
232#define RL_ROUTES_ADDED (1u << 2)
233 unsigned int iflags;
234
237 struct route_gateway_info ngi; /* net_gateway */
238 unsigned int flags; /* RG_x flags */
240 struct gc_arena gc;
241};
242
244{
245 unsigned int iflags; /* RL_ flags, see route_list */
246
247 unsigned int spec_flags; /* RTSA_ flags, route_special_addr */
248 struct in6_addr remote_endpoint_ipv6; /* inside tun */
249 struct in6_addr remote_host_ipv6; /* --remote address */
251
253 struct route_ipv6_gateway_info ngi6; /* net_gateway_ipv6 */
254 unsigned int flags; /* RG_x flags, see route_option_list */
256 struct gc_arena gc;
257};
258
259/* internal OpenVPN route */
260struct iroute
261{
264 struct iroute *next;
265};
266
268{
269 struct in6_addr network;
270 unsigned int netbits;
272};
273
284bool block_local_needed(const struct route_list *rl);
285
287
289
291 struct gc_arena *a);
292
294 const struct route_ipv6_option_list *src, struct gc_arena *a);
295
296void copy_route_option_list(struct route_option_list *dest, const struct route_option_list *src,
297 struct gc_arena *a);
298
300 const struct route_ipv6_option_list *src, struct gc_arena *a);
301
303
304bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags,
305 const struct env_set *es, openvpn_net_ctx_t *ctx);
306
307void delete_route_ipv6(const struct route_ipv6 *r, const struct tuntap *tt,
308 const struct env_set *es, openvpn_net_ctx_t *ctx);
309
310bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
311 const struct route_gateway_info *rgi, const struct env_set *es,
312 openvpn_net_ctx_t *ctx);
313
314void add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask,
315 const char *gateway, const char *metric, int table_id);
316
317void add_route_ipv6_to_option_list(struct route_ipv6_option_list *l, const char *prefix,
318 const char *gateway, const char *metric, int table_id);
319
320bool init_route_list(struct route_list *rl, const struct route_option_list *opt,
321 const char *remote_endpoint, int default_metric, in_addr_t remote_host,
322 struct env_set *es, openvpn_net_ctx_t *ctx);
323
324bool init_route_ipv6_list(struct route_ipv6_list *rl6, const struct route_ipv6_option_list *opt6,
325 const char *remote_endpoint, int default_metric,
326 const struct in6_addr *remote_host, struct env_set *es,
327 openvpn_net_ctx_t *ctx);
328
329void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr);
330
331bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt,
332 unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
333
334void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt,
335 unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
336
337void delete_routes_v4(struct route_list *rl, const struct tuntap *tt, unsigned int flags,
338 const struct env_set *es, openvpn_net_ctx_t *ctx);
339
340void delete_routes_v6(struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags,
341 const struct env_set *es, openvpn_net_ctx_t *ctx);
342
343void setenv_routes(struct env_set *es, const struct route_list *rl);
344
345void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6);
346
347bool is_special_addr(const char *addr_str);
348
357
358void get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi, const struct in6_addr *dest,
359 openvpn_net_ctx_t *ctx);
360
361void print_default_gateway(const msglvl_t msglevel, const struct route_gateway_info *rgi,
362 const struct route_ipv6_gateway_info *rgi6);
363
364/*
365 * Test if addr is reachable via a local interface (return ILA_LOCAL),
366 * or if it needs to be routed via the default gateway (return
367 * ILA_NONLOCAL). If the current platform doesn't implement this
368 * function, return ILA_NOT_IMPLEMENTED.
369 */
370#define TLA_NOT_IMPLEMENTED 0
371#define TLA_NONLOCAL 1
372#define TLA_LOCAL 2
373int test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi);
374
375#ifndef ENABLE_SMALL
376void print_route_options(const struct route_option_list *rol, msglvl_t msglevel);
377
378#endif
379
380void print_routes(const struct route_list *rl, msglvl_t msglevel);
381
382#ifdef _WIN32
383
384void show_routes(msglvl_t msglevel);
385
386bool test_routes(const struct route_list *rl, const struct tuntap *tt);
387
388#else /* ifdef _WIN32 */
389static inline bool
390test_routes(const struct route_list *rl, const struct tuntap *tt)
391{
392 return true;
393}
394#endif
395
396bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits);
397
398int netmask_to_netbits2(in_addr_t netmask);
399
400static inline in_addr_t
401netbits_to_netmask(const int netbits)
402{
403 const int addrlen = sizeof(in_addr_t) * 8;
404 in_addr_t mask = 0;
405 if (netbits > 0 && netbits <= addrlen)
406 {
407 mask = IPV4_NETMASK_HOST << (addrlen - netbits);
408 }
409 return mask;
410}
411
412static inline bool
414{
415 if (!rl)
416 {
417 return false;
418 }
419 else
420 {
421 return !(rl->spec.flags & RTSA_REMOTE_ENDPOINT);
422 }
423}
424
425static inline int
430
431
441bool
442ipv6_net_contains_host(const struct in6_addr *network, unsigned int bits, const struct in6_addr *host);
443
444#endif /* ifndef ROUTE_H */
void * openvpn_net_ctx_t
Definition networking.h:38
#define IPV4_NETMASK_HOST
Definition basic.h:34
#define BOOL_CAST(x)
Definition basic.h:26
unsigned int msglvl_t
Definition error.h:77
void print_route_options(const struct route_option_list *rol, msglvl_t msglevel)
Definition route.c:1238
static bool route_list_vpn_gateway_needed(const struct route_list *rl)
Definition route.h:413
bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1101
bool is_special_addr(const char *addr_str)
Definition route.c:299
struct route_option_list * clone_route_option_list(const struct route_option_list *src, struct gc_arena *a)
Definition route.c:153
void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6)
Definition route.c:1404
int netmask_to_netbits2(in_addr_t netmask)
Definition route.c:3909
struct route_ipv6_option_list * new_route_ipv6_option_list(struct gc_arena *a)
Definition route.c:137
#define RL_DID_REDIRECT_DEFAULT_GATEWAY
Definition route.h:230
void delete_routes_v6(struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1196
#define RTSA_REMOTE_ENDPOINT
Definition route.h:62
bool test_routes(const struct route_list *rl, const struct tuntap *tt)
Definition route.c:2443
static int route_did_redirect_default_gateway(const struct route_list *rl)
Definition route.h:426
bool block_local_needed(const struct route_list *rl)
Get the decision whether to block traffic to local networks while the VPN is connected.
Definition route.c:596
static in_addr_t netbits_to_netmask(const int netbits)
Definition route.h:401
void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr)
Definition route.c:535
bool ipv6_net_contains_host(const struct in6_addr *network, unsigned int bits, const struct in6_addr *host)
check whether an IPv6 host address is covered by a given network/bits
Definition route.c:716
void add_route_ipv6_to_option_list(struct route_ipv6_option_list *l, const char *prefix, const char *gateway, const char *metric, int table_id)
Definition route.c:507
void copy_route_option_list(struct route_option_list *dest, const struct route_option_list *src, struct gc_arena *a)
Definition route.c:171
void copy_route_ipv6_option_list(struct route_ipv6_option_list *dest, const struct route_ipv6_option_list *src, struct gc_arena *a)
Definition route.c:179
bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1480
void print_default_gateway(const msglvl_t msglevel, const struct route_gateway_info *rgi, const struct route_ipv6_gateway_info *rgi6)
Definition route.c:1252
bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1794
void get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi, const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
Definition route.c:2668
void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openvpn_net_ctx_t *ctx)
Retrieves the best gateway for a given destination based on the routing table.
Definition route.c:2570
struct route_ipv6_option_list * clone_route_ipv6_option_list(const struct route_ipv6_option_list *src, struct gc_arena *a)
Definition route.c:162
void print_routes(const struct route_list *rl, msglvl_t msglevel)
Definition route.c:1337
struct route_option_list * new_route_option_list(struct gc_arena *a)
Definition route.c:128
int test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi)
Definition route.c:4016
bool init_route_list(struct route_list *rl, const struct route_option_list *opt, const char *remote_endpoint, int default_metric, in_addr_t remote_host, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:604
#define RGI_N_ADDRESSES
Definition route.h:183
bool init_route_ipv6_list(struct route_ipv6_list *rl6, const struct route_ipv6_option_list *opt6, const char *remote_endpoint, int default_metric, const struct in6_addr *remote_host, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:750
#define N_ROUTE_BYPASS
Definition route.h:54
void route_ipv6_clear_host_bits(struct route_ipv6 *r6)
Definition route.c:1769
void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1166
void show_routes(msglvl_t msglevel)
Definition route.c:3071
bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
Definition route.c:3878
void setenv_routes(struct env_set *es, const struct route_list *rl)
Definition route.c:1367
void delete_routes_v4(struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1174
void add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask, const char *gateway, const char *metric, int table_id)
Definition route.c:492
void delete_route_ipv6(const struct route_ipv6 *r, const struct tuntap *tt, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:2202
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
struct iroute_ipv6 * next
Definition route.h:271
unsigned int netbits
Definition route.h:270
struct in6_addr network
Definition route.h:269
in_addr_t network
Definition route.h:262
int netbits
Definition route.h:263
struct iroute * next
Definition route.h:264
int n_bypass
Definition route.h:55
in_addr_t bypass[N_ROUTE_BYPASS]
Definition route.h:56
in_addr_t netmask
Definition route.h:154
uint8_t hwaddr[6]
Definition route.h:177
unsigned int flags
Definition route.h:165
struct route_gateway_address addrs[RGI_N_ADDRESSES]
Definition route.h:185
DWORD adapter_index
Definition route.h:169
struct route_gateway_address gateway
Definition route.h:180
const struct route_option * option
Definition route.h:125
int metric
Definition route.h:130
struct route_ipv4 * next
Definition route.h:123
int table_id
Definition route.h:129
in_addr_t network
Definition route.h:126
in_addr_t netmask
Definition route.h:127
in_addr_t gateway
Definition route.h:128
unsigned int flags
Definition route.h:124
struct in6_addr addr_ipv6
Definition route.h:190
struct route_ipv6_gateway_address gateway
Definition route.h:219
struct route_ipv6_gateway_address addrs[RGI_N_ADDRESSES]
Definition route.h:224
unsigned int flags
Definition route.h:197
uint8_t hwaddr[6]
Definition route.h:216
unsigned int iflags
Definition route.h:245
unsigned int flags
Definition route.h:254
struct route_ipv6_gateway_info rgi6
Definition route.h:252
struct route_ipv6 * routes_ipv6
Definition route.h:255
unsigned int spec_flags
Definition route.h:247
struct route_ipv6_gateway_info ngi6
Definition route.h:253
int default_metric
Definition route.h:250
struct in6_addr remote_host_ipv6
Definition route.h:249
struct gc_arena gc
Definition route.h:256
struct in6_addr remote_endpoint_ipv6
Definition route.h:248
unsigned int flags
Definition route.h:113
struct gc_arena * gc
Definition route.h:115
struct route_ipv6_option * routes_ipv6
Definition route.h:114
struct route_ipv6_option * next
Definition route.h:104
const char * gateway
Definition route.h:106
const char * metric
Definition route.h:107
const char * prefix
Definition route.h:105
struct in6_addr gateway
Definition route.h:139
DWORD adapter_index
Definition route.h:144
unsigned int netbits
Definition route.h:138
struct route_ipv6 * next
Definition route.h:135
unsigned int flags
Definition route.h:136
struct in6_addr network
Definition route.h:137
int table_id
Definition route.h:141
int metric
Definition route.h:140
struct route_gateway_info rgi
Definition route.h:236
struct route_ipv4 * routes
Definition route.h:239
struct route_special_addr spec
Definition route.h:235
unsigned int flags
Definition route.h:238
struct gc_arena gc
Definition route.h:240
struct route_gateway_info ngi
Definition route.h:237
unsigned int iflags
Definition route.h:233
struct gc_arena * gc
Definition route.h:99
unsigned int flags
Definition route.h:97
struct route_option * routes
Definition route.h:98
int table_id
Definition route.h:81
const char * network
Definition route.h:78
const char * netmask
Definition route.h:79
struct route_option * next
Definition route.h:77
const char * gateway
Definition route.h:80
const char * metric
Definition route.h:82
in_addr_t remote_host
Definition route.h:68
struct route_bypass bypass
Definition route.h:70
unsigned int flags
Definition route.h:65
in_addr_t remote_endpoint
Definition route.h:67
int remote_host_local
Definition route.h:69
Definition tun.h:183
uint32_t in_addr_t
Definition syshead.h:52
static char * iface
struct env_set * es
char * r6[]