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
67 in_addr_t remote_endpoint;
68 in_addr_t remote_host;
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;
126 in_addr_t network;
127 in_addr_t netmask;
128 in_addr_t gateway;
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
152{
153 in_addr_t addr;
154 in_addr_t netmask;
155};
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 unsigned int flags; /* RG_x flags */
239 struct gc_arena gc;
240};
241
243{
244 unsigned int iflags; /* RL_ flags, see route_list */
245
246 unsigned int spec_flags; /* RTSA_ flags, route_special_addr */
247 struct in6_addr remote_endpoint_ipv6; /* inside tun */
248 struct in6_addr remote_host_ipv6; /* --remote address */
250
252 unsigned int flags; /* RG_x flags, see route_option_list */
254 struct gc_arena gc;
255};
256
257/* internal OpenVPN route */
258struct iroute
259{
260 in_addr_t network;
262 struct iroute *next;
263};
264
266{
267 struct in6_addr network;
268 unsigned int netbits;
270};
271
282bool block_local_needed(const struct route_list *rl);
283
285
287
289 struct gc_arena *a);
290
292 const struct route_ipv6_option_list *src, struct gc_arena *a);
293
294void copy_route_option_list(struct route_option_list *dest, const struct route_option_list *src,
295 struct gc_arena *a);
296
298 const struct route_ipv6_option_list *src, struct gc_arena *a);
299
301
302bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags,
303 const struct env_set *es, openvpn_net_ctx_t *ctx);
304
305void delete_route_ipv6(const struct route_ipv6 *r, const struct tuntap *tt,
306 const struct env_set *es, openvpn_net_ctx_t *ctx);
307
308bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
309 const struct route_gateway_info *rgi, const struct env_set *es,
310 openvpn_net_ctx_t *ctx);
311
312void add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask,
313 const char *gateway, const char *metric, int table_id);
314
315void add_route_ipv6_to_option_list(struct route_ipv6_option_list *l, const char *prefix,
316 const char *gateway, const char *metric, int table_id);
317
318bool init_route_list(struct route_list *rl, const struct route_option_list *opt,
319 const char *remote_endpoint, int default_metric, in_addr_t remote_host,
320 struct env_set *es, openvpn_net_ctx_t *ctx);
321
322bool init_route_ipv6_list(struct route_ipv6_list *rl6, const struct route_ipv6_option_list *opt6,
323 const char *remote_endpoint, int default_metric,
324 const struct in6_addr *remote_host, struct env_set *es,
325 openvpn_net_ctx_t *ctx);
326
327void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr);
328
329bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt,
330 unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
331
332void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt,
333 unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
334
335void delete_routes_v4(struct route_list *rl, const struct tuntap *tt, unsigned int flags,
336 const struct env_set *es, openvpn_net_ctx_t *ctx);
337
338void delete_routes_v6(struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags,
339 const struct env_set *es, openvpn_net_ctx_t *ctx);
340
341void setenv_routes(struct env_set *es, const struct route_list *rl);
342
343void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6);
344
345bool is_special_addr(const char *addr_str);
346
354void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openvpn_net_ctx_t *ctx);
355
356void get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi, const struct in6_addr *dest,
357 openvpn_net_ctx_t *ctx);
358
359void print_default_gateway(const int msglevel, const struct route_gateway_info *rgi,
360 const struct route_ipv6_gateway_info *rgi6);
361
362/*
363 * Test if addr is reachable via a local interface (return ILA_LOCAL),
364 * or if it needs to be routed via the default gateway (return
365 * ILA_NONLOCAL). If the current platform doesn't implement this
366 * function, return ILA_NOT_IMPLEMENTED.
367 */
368#define TLA_NOT_IMPLEMENTED 0
369#define TLA_NONLOCAL 1
370#define TLA_LOCAL 2
371int test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi);
372
373#ifndef ENABLE_SMALL
374void print_route_options(const struct route_option_list *rol, int level);
375
376#endif
377
378void print_routes(const struct route_list *rl, int level);
379
380#ifdef _WIN32
381
382void show_routes(int msglev);
383
384bool test_routes(const struct route_list *rl, const struct tuntap *tt);
385
386#else /* ifdef _WIN32 */
387static inline bool
388test_routes(const struct route_list *rl, const struct tuntap *tt)
389{
390 return true;
391}
392#endif
393
394bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits);
395
396int netmask_to_netbits2(in_addr_t netmask);
397
398static inline in_addr_t
399netbits_to_netmask(const int netbits)
400{
401 const int addrlen = sizeof(in_addr_t) * 8;
402 in_addr_t mask = 0;
403 if (netbits > 0 && netbits <= addrlen)
404 {
405 mask = IPV4_NETMASK_HOST << (addrlen - netbits);
406 }
407 return mask;
408}
409
410static inline bool
412{
413 if (!rl)
414 {
415 return false;
416 }
417 else
418 {
419 return !(rl->spec.flags & RTSA_REMOTE_ENDPOINT);
420 }
421}
422
423static inline int
428
429#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
static bool route_list_vpn_gateway_needed(const struct route_list *rl)
Definition route.h:411
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:3891
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:2434
static int route_did_redirect_default_gateway(const struct route_list *rl)
Definition route.h:424
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:399
void show_routes(int msglev)
Definition route.c:3062
void print_route_options(const struct route_option_list *rol, int level)
Definition route.c:1238
void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr)
Definition route.c:535
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 print_default_gateway(const int msglevel, const struct route_gateway_info *rgi, const struct route_ipv6_gateway_info *rgi6)
Definition route.c:1252
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
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:1790
void get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi, const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
Definition route.c:2659
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:2561
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
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:3998
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:753
#define N_ROUTE_BYPASS
Definition route.h:54
void route_ipv6_clear_host_bits(struct route_ipv6 *r6)
Definition route.c:1765
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 print_routes(const struct route_list *rl, int level)
Definition route.c:1337
bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
Definition route.c:3860
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:2198
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
struct iroute_ipv6 * next
Definition route.h:269
unsigned int netbits
Definition route.h:268
struct in6_addr network
Definition route.h:267
in_addr_t network
Definition route.h:260
int netbits
Definition route.h:261
struct iroute * next
Definition route.h:262
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:244
unsigned int flags
Definition route.h:252
struct route_ipv6_gateway_info rgi6
Definition route.h:251
struct route_ipv6 * routes_ipv6
Definition route.h:253
unsigned int spec_flags
Definition route.h:246
int default_metric
Definition route.h:249
struct in6_addr remote_host_ipv6
Definition route.h:248
struct gc_arena gc
Definition route.h:254
struct in6_addr remote_endpoint_ipv6
Definition route.h:247
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:238
struct route_special_addr spec
Definition route.h:235
unsigned int flags
Definition route.h:237
struct gc_arena gc
Definition route.h:239
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
static char * iface
struct env_set * es
char * r6[]