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-2024 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24/*
25 * Support routines for adding/deleting network routes.
26 */
27
28#ifndef ROUTE_H
29#define ROUTE_H
30
31#include "basic.h"
32#include "tun.h"
33#include "misc.h"
34#include "networking.h"
35
36#ifdef _WIN32
37/*
38 * Windows route methods
39 */
40#define ROUTE_METHOD_ADAPTIVE 0 /* try IP helper first then route.exe */
41#define ROUTE_METHOD_IPAPI 1 /* use IP helper API */
42#define ROUTE_METHOD_EXE 2 /* use route.exe */
43#define ROUTE_METHOD_SERVICE 3 /* use the privileged Windows service */
44#define ROUTE_METHOD_MASK 3
45#endif
46
47/*
48 * Route add/delete flags (must stay clear of ROUTE_METHOD bits)
49 */
50#define ROUTE_DELETE_FIRST (1<<2)
51#define ROUTE_REF_GW (1<<3)
52
54{
55#define N_ROUTE_BYPASS 8
58};
59
61{
62 /* bits indicating which members below are defined */
63#define RTSA_REMOTE_ENDPOINT (1<<0)
64#define RTSA_REMOTE_HOST (1<<1)
65#define RTSA_DEFAULT_METRIC (1<<2)
66 unsigned int flags;
67
68 in_addr_t remote_endpoint;
69 in_addr_t remote_host;
70 int remote_host_local; /* TLA_x value */
73};
74
77 const char *network;
78 const char *netmask;
79 const char *gateway;
80 const char *metric;
81};
82
83/* redirect-gateway flags */
84#define RG_ENABLE (1<<0)
85#define RG_LOCAL (1<<1)
86#define RG_DEF1 (1<<2)
87#define RG_BYPASS_DHCP (1<<3)
88#define RG_BYPASS_DNS (1<<4)
89#define RG_REROUTE_GW (1<<5)
90#define RG_AUTO_LOCAL (1<<6)
91#define RG_BLOCK_LOCAL (1<<7)
92
94 unsigned int flags; /* RG_x flags */
96 struct gc_arena *gc;
97};
98
101 const char *prefix; /* e.g. "2001:db8:1::/64" */
102 const char *gateway; /* e.g. "2001:db8:0::2" */
103 const char *metric; /* e.g. "5" */
104};
105
107 unsigned int flags; /* RG_x flags, see route_option-list */
109 struct gc_arena *gc;
110};
111
113#define RT_DEFINED (1<<0)
114#define RT_ADDED (1<<1)
115#define RT_METRIC_DEFINED (1<<2)
117 unsigned int flags;
118 const struct route_option *option;
119 in_addr_t network;
120 in_addr_t netmask;
121 in_addr_t gateway;
123};
124
127 unsigned int flags; /* RT_ flags, see route_ipv4 */
128 struct in6_addr network;
129 unsigned int netbits;
130 struct in6_addr gateway;
132 /* gateway interface */
133#ifdef _WIN32
134 DWORD adapter_index; /* interface or ~0 if undefined */
135#else
136 char *iface; /* interface name (null terminated) */
137#endif
138};
139
140
142 in_addr_t addr;
143 in_addr_t netmask;
144};
145
147#define RGI_ADDR_DEFINED (1<<0) /* set if gateway.addr defined */
148#define RGI_NETMASK_DEFINED (1<<1) /* set if gateway.netmask defined */
149#define RGI_HWADDR_DEFINED (1<<2) /* set if hwaddr is defined */
150#define RGI_IFACE_DEFINED (1<<3) /* set if iface is defined */
151#define RGI_OVERFLOW (1<<4) /* set if more interface addresses than will fit in addrs */
152#define RGI_ON_LINK (1<<5)
153 unsigned int flags;
154
155 /* gateway interface */
156#ifdef _WIN32
157 DWORD adapter_index; /* interface or ~0 if undefined */
158#elif defined(TARGET_HAIKU)
159 char iface[PATH_MAX]; /* iface names are full /dev path with driver name */
160#else
161 char iface[16]; /* interface name (null terminated), may be empty */
162#endif
163
164 /* gateway interface hardware address */
165 uint8_t hwaddr[6];
166
167 /* gateway/router address */
169
170 /* address/netmask pairs bound to interface */
171#define RGI_N_ADDRESSES 8
172 int n_addrs; /* len of addrs, may be 0 */
173 struct route_gateway_address addrs[RGI_N_ADDRESSES]; /* local addresses attached to iface */
174};
175
177 struct in6_addr addr_ipv6;
179};
180
182/* RGI_ flags used as in route_gateway_info */
183 unsigned int flags;
184
185 /* gateway interface */
186#ifdef _WIN32
187 DWORD adapter_index; /* interface or ~0 if undefined */
188#else
189 /* non linux platform don't have this constant defined */
190#ifndef IFNAMSIZ
191#if defined(TARGET_HAIKU)
192/* iface names are full /dev path with driver name */
193#define IFNAMSIZ PATH_MAX
194#else
195#define IFNAMSIZ 16
196#endif
197#endif
198 char iface[IFNAMSIZ]; /* interface name (null terminated), may be empty */
199#endif
200
201 /* gateway interface hardware address */
202 uint8_t hwaddr[6];
203
204 /* gateway/router address */
206
207 /* address/netmask pairs bound to interface */
208#define RGI_N_ADDRESSES 8
209 int n_addrs; /* len of addrs, may be 0 */
210 struct route_ipv6_gateway_address addrs[RGI_N_ADDRESSES]; /* local addresses attached to iface */
211};
212
214#define RL_DID_REDIRECT_DEFAULT_GATEWAY (1<<0)
215#define RL_DID_LOCAL (1<<1)
216#define RL_ROUTES_ADDED (1<<2)
217 unsigned int iflags;
218
221 unsigned int flags; /* RG_x flags */
223 struct gc_arena gc;
224};
225
227 unsigned int iflags; /* RL_ flags, see route_list */
228
229 unsigned int spec_flags; /* RTSA_ flags, route_special_addr */
230 struct in6_addr remote_endpoint_ipv6; /* inside tun */
231 struct in6_addr remote_host_ipv6; /* --remote address */
233
235 unsigned int flags; /* RG_x flags, see route_option_list */
237 struct gc_arena gc;
238};
239
240/* internal OpenVPN route */
241struct iroute {
242 in_addr_t network;
244 struct iroute *next;
245};
246
248 struct in6_addr network;
249 unsigned int netbits;
251};
252
263bool block_local_needed(const struct route_list *rl);
264
266
268
269struct route_option_list *clone_route_option_list(const struct route_option_list *src, struct gc_arena *a);
270
272
273void copy_route_option_list(struct route_option_list *dest, const struct route_option_list *src, struct gc_arena *a);
274
276 const struct route_ipv6_option_list *src,
277 struct gc_arena *a);
278
279void route_ipv6_clear_host_bits( struct route_ipv6 *r6 );
280
281bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
282
283void delete_route_ipv6(const struct route_ipv6 *r, const struct tuntap *tt, const struct env_set *es, openvpn_net_ctx_t *ctx);
284
285bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
286 const struct route_gateway_info *rgi, const struct env_set *es,
287 openvpn_net_ctx_t *ctx);
288
290 const char *network,
291 const char *netmask,
292 const char *gateway,
293 const char *metric);
294
296 const char *prefix,
297 const char *gateway,
298 const char *metric);
299
300bool init_route_list(struct route_list *rl,
301 const struct route_option_list *opt,
302 const char *remote_endpoint,
303 int default_metric,
304 in_addr_t remote_host,
305 struct env_set *es,
306 openvpn_net_ctx_t *ctx);
307
309 const struct route_ipv6_option_list *opt6,
310 const char *remote_endpoint,
311 int default_metric,
312 const struct in6_addr *remote_host,
313 struct env_set *es,
314 openvpn_net_ctx_t *ctx);
315
317 struct env_set *es,
318 const in_addr_t addr);
319
320bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6,
321 const struct tuntap *tt, unsigned int flags,
322 const struct env_set *es, openvpn_net_ctx_t *ctx);
323
324void delete_routes(struct route_list *rl,
325 struct route_ipv6_list *rl6,
326 const struct tuntap *tt,
327 unsigned int flags,
328 const struct env_set *es,
329 openvpn_net_ctx_t *ctx);
330
331void setenv_routes(struct env_set *es, const struct route_list *rl);
332
333void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6);
334
335bool is_special_addr(const char *addr_str);
336
345 in_addr_t dest,
346 openvpn_net_ctx_t *ctx);
347
349 const struct in6_addr *dest,
350 openvpn_net_ctx_t *ctx);
351
352void print_default_gateway(const int msglevel,
353 const struct route_gateway_info *rgi,
354 const struct route_ipv6_gateway_info *rgi6);
355
356/*
357 * Test if addr is reachable via a local interface (return ILA_LOCAL),
358 * or if it needs to be routed via the default gateway (return
359 * ILA_NONLOCAL). If the current platform doesn't implement this
360 * function, return ILA_NOT_IMPLEMENTED.
361 */
362#define TLA_NOT_IMPLEMENTED 0
363#define TLA_NONLOCAL 1
364#define TLA_LOCAL 2
365int test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi);
366
367#ifndef ENABLE_SMALL
368void print_route_options(const struct route_option_list *rol,
369 int level);
370
371#endif
372
373void print_routes(const struct route_list *rl, int level);
374
375#ifdef _WIN32
376
377void show_routes(int msglev);
378
379bool test_routes(const struct route_list *rl, const struct tuntap *tt);
380
381#else /* ifdef _WIN32 */
382static inline bool
383test_routes(const struct route_list *rl, const struct tuntap *tt)
384{
385 return true;
386}
387#endif
388
389bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits);
390
391int netmask_to_netbits2(in_addr_t netmask);
392
393static inline in_addr_t
394netbits_to_netmask(const int netbits)
395{
396 const int addrlen = sizeof(in_addr_t) * 8;
397 in_addr_t mask = 0;
398 if (netbits > 0 && netbits <= addrlen)
399 {
400 mask = IPV4_NETMASK_HOST << (addrlen-netbits);
401 }
402 return mask;
403}
404
405static inline bool
407{
408 if (!rl)
409 {
410 return false;
411 }
412 else
413 {
414 return !(rl->spec.flags & RTSA_REMOTE_ENDPOINT);
415 }
416}
417
418static inline int
423
424#endif /* ifndef ROUTE_H */
void * openvpn_net_ctx_t
Definition networking.h:39
#define IPV4_NETMASK_HOST
Definition basic.h:35
#define BOOL_CAST(x)
Definition basic.h:27
static bool route_list_vpn_gateway_needed(const struct route_list *rl)
Definition route.h:406
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:1189
bool is_special_addr(const char *addr_str)
Definition route.c:306
struct route_option_list * clone_route_option_list(const struct route_option_list *src, struct gc_arena *a)
Definition route.c:155
void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6)
Definition route.c:1489
int netmask_to_netbits2(in_addr_t netmask)
Definition route.c:4139
struct route_ipv6_option_list * new_route_ipv6_option_list(struct gc_arena *a)
Definition route.c:139
#define RL_DID_REDIRECT_DEFAULT_GATEWAY
Definition route.h:214
#define RTSA_REMOTE_ENDPOINT
Definition route.h:63
bool test_routes(const struct route_list *rl, const struct tuntap *tt)
Definition route.c:2647
static int route_did_redirect_default_gateway(const struct route_list *rl)
Definition route.h:419
void add_route_ipv6_to_option_list(struct route_ipv6_option_list *l, const char *prefix, const char *gateway, const char *metric)
Definition route.c:528
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:621
static in_addr_t netbits_to_netmask(const int netbits)
Definition route.h:394
void show_routes(int msglev)
Definition route.c:3308
void print_route_options(const struct route_option_list *rol, int level)
Definition route.c:1319
void add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask, const char *gateway, const char *metric)
Definition route.c:510
void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr)
Definition route.c:557
void print_default_gateway(const int msglevel, const struct route_gateway_info *rgi, const struct route_ipv6_gateway_info *rgi6)
Definition route.c:1335
void copy_route_option_list(struct route_option_list *dest, const struct route_option_list *src, struct gc_arena *a)
Definition route.c:173
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:180
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:1567
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:1915
void get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi, const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
Definition route.c:2886
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:2785
struct route_ipv6_option_list * clone_route_ipv6_option_list(const struct route_ipv6_option_list *src, struct gc_arena *a)
Definition route.c:164
struct route_option_list * new_route_option_list(struct gc_arena *a)
Definition route.c:130
int test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi)
Definition route.c:4245
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:631
#define RGI_N_ADDRESSES
Definition route.h:171
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:790
#define N_ROUTE_BYPASS
Definition route.h:55
void route_ipv6_clear_host_bits(struct route_ipv6 *r6)
Definition route.c:1892
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:1256
void print_routes(const struct route_list *rl, int level)
Definition route.c:1421
bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
Definition route.c:4108
void setenv_routes(struct env_set *es, const struct route_list *rl)
Definition route.c:1451
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:2384
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
struct iroute_ipv6 * next
Definition route.h:250
unsigned int netbits
Definition route.h:249
struct in6_addr network
Definition route.h:248
in_addr_t network
Definition route.h:242
int netbits
Definition route.h:243
struct iroute * next
Definition route.h:244
int n_bypass
Definition route.h:56
in_addr_t bypass[N_ROUTE_BYPASS]
Definition route.h:57
in_addr_t netmask
Definition route.h:143
uint8_t hwaddr[6]
Definition route.h:165
unsigned int flags
Definition route.h:153
struct route_gateway_address addrs[RGI_N_ADDRESSES]
Definition route.h:173
DWORD adapter_index
Definition route.h:157
struct route_gateway_address gateway
Definition route.h:168
const struct route_option * option
Definition route.h:118
int metric
Definition route.h:122
struct route_ipv4 * next
Definition route.h:116
in_addr_t network
Definition route.h:119
in_addr_t netmask
Definition route.h:120
in_addr_t gateway
Definition route.h:121
unsigned int flags
Definition route.h:117
struct in6_addr addr_ipv6
Definition route.h:177
struct route_ipv6_gateway_address gateway
Definition route.h:205
struct route_ipv6_gateway_address addrs[RGI_N_ADDRESSES]
Definition route.h:210
unsigned int flags
Definition route.h:183
uint8_t hwaddr[6]
Definition route.h:202
unsigned int iflags
Definition route.h:227
unsigned int flags
Definition route.h:235
struct route_ipv6_gateway_info rgi6
Definition route.h:234
struct route_ipv6 * routes_ipv6
Definition route.h:236
unsigned int spec_flags
Definition route.h:229
int default_metric
Definition route.h:232
struct in6_addr remote_host_ipv6
Definition route.h:231
struct gc_arena gc
Definition route.h:237
struct in6_addr remote_endpoint_ipv6
Definition route.h:230
unsigned int flags
Definition route.h:107
struct gc_arena * gc
Definition route.h:109
struct route_ipv6_option * routes_ipv6
Definition route.h:108
struct route_ipv6_option * next
Definition route.h:100
const char * gateway
Definition route.h:102
const char * metric
Definition route.h:103
const char * prefix
Definition route.h:101
struct in6_addr gateway
Definition route.h:130
DWORD adapter_index
Definition route.h:134
unsigned int netbits
Definition route.h:129
struct route_ipv6 * next
Definition route.h:126
unsigned int flags
Definition route.h:127
struct in6_addr network
Definition route.h:128
int metric
Definition route.h:131
struct route_gateway_info rgi
Definition route.h:220
struct route_ipv4 * routes
Definition route.h:222
struct route_special_addr spec
Definition route.h:219
unsigned int flags
Definition route.h:221
struct gc_arena gc
Definition route.h:223
unsigned int iflags
Definition route.h:217
struct gc_arena * gc
Definition route.h:96
unsigned int flags
Definition route.h:94
struct route_option * routes
Definition route.h:95
const char * network
Definition route.h:77
const char * netmask
Definition route.h:78
struct route_option * next
Definition route.h:76
const char * gateway
Definition route.h:79
const char * metric
Definition route.h:80
in_addr_t remote_host
Definition route.h:69
struct route_bypass bypass
Definition route.h:71
unsigned int flags
Definition route.h:66
in_addr_t remote_endpoint
Definition route.h:68
int remote_host_local
Definition route.h:70
Definition tun.h:181
static char * iface
struct env_set * es