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
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Support routines for adding/deleting network routes.
35 #define MAX_ROUTES_DEFAULT 100
39 * Windows route methods
41 #define ROUTE_METHOD_ADAPTIVE 0 /* try IP helper first then route.exe */
42 #define ROUTE_METHOD_IPAPI 1 /* use IP helper API */
43 #define ROUTE_METHOD_EXE 2 /* use route.exe */
44 #define ROUTE_METHOD_MASK 3
48 * Route add flags (must stay clear of ROUTE_METHOD bits)
50 #define ROUTE_DELETE_FIRST 4
54 # define N_ROUTE_BYPASS 8
56 in_addr_t bypass
[N_ROUTE_BYPASS
];
59 struct route_special_addr
61 in_addr_t remote_endpoint
;
62 bool remote_endpoint_defined
;
63 in_addr_t net_gateway
;
64 bool net_gateway_defined
;
65 in_addr_t remote_host
;
66 bool remote_host_defined
;
67 struct route_bypass bypass
;
69 bool default_metric_defined
;
79 /* redirect-gateway flags */
80 #define RG_ENABLE (1<<0)
81 #define RG_LOCAL (1<<1)
82 #define RG_DEF1 (1<<2)
83 #define RG_BYPASS_DHCP (1<<3)
84 #define RG_BYPASS_DNS (1<<4)
85 #define RG_REROUTE_GW (1<<5)
86 #define RG_AUTO_LOCAL (1<<6)
88 struct route_option_list
{
92 struct route_option routes
[EMPTY_ARRAY_SIZE
];
97 const struct route_option
*option
;
107 struct route_special_addr spec
;
109 bool did_redirect_default_gateway
;
113 struct route routes
[EMPTY_ARRAY_SIZE
];
117 /* internal OpenVPN route */
125 struct route_option_list
*new_route_option_list (const int max_routes
, struct gc_arena
*a
);
126 struct route_option_list
*clone_route_option_list (const struct route_option_list
*src
, struct gc_arena
*a
);
127 void copy_route_option_list (struct route_option_list
*dest
, const struct route_option_list
*src
);
129 struct route_list
*new_route_list (const int max_routes
, struct gc_arena
*a
);
131 void add_route (struct route
*r
, const struct tuntap
*tt
, unsigned int flags
, const struct env_set
*es
);
133 void add_route_to_option_list (struct route_option_list
*l
,
139 bool init_route_list (struct route_list
*rl
,
140 const struct route_option_list
*opt
,
141 const char *remote_endpoint
,
143 in_addr_t remote_host
,
146 void route_list_add_default_gateway (struct route_list
*rl
,
148 const in_addr_t addr
);
150 void add_routes (struct route_list
*rl
,
151 const struct tuntap
*tt
,
153 const struct env_set
*es
);
155 void delete_routes (struct route_list
*rl
,
156 const struct tuntap
*tt
,
158 const struct env_set
*es
);
160 void setenv_routes (struct env_set
*es
, const struct route_list
*rl
);
162 bool is_special_addr (const char *addr_str
);
164 bool get_default_gateway (in_addr_t
*ip
, in_addr_t
*netmask
);
167 * Test if addr is reachable via a local interface (return ILA_LOCAL),
168 * or if it needs to be routed via the default gateway (return
169 * ILA_NONLOCAL). If the current platform doesn't implement this
170 * function, return ILA_NOT_IMPLEMENTED.
172 #define TLA_NOT_IMPLEMENTED 0
173 #define TLA_NONLOCAL 1
175 int test_local_addr (const in_addr_t addr
);
178 bool get_default_gateway_mac_addr (unsigned char *macaddr
);
182 void print_route_options (const struct route_option_list
*rol
,
186 void print_routes (const struct route_list
*rl
, int level
);
190 void show_routes (int msglev
);
191 bool test_routes (const struct route_list
*rl
, const struct tuntap
*tt
);
192 bool add_route_ipapi (const struct route
*r
, const struct tuntap
*tt
);
193 bool del_route_ipapi (const struct route
*r
, const struct tuntap
*tt
);
196 static inline bool test_routes (const struct route_list
*rl
, const struct tuntap
*tt
) { return true; }
199 bool netmask_to_netbits (const in_addr_t network
, const in_addr_t netmask
, int *netbits
);
201 static inline in_addr_t
202 netbits_to_netmask (const int netbits
)
204 const int addrlen
= sizeof (in_addr_t
) * 8;
206 if (netbits
> 0 && netbits
<= addrlen
)
207 mask
= ~0 << (addrlen
-netbits
);
212 route_list_default_gateway_needed (const struct route_list
*rl
)
217 return !rl
->spec
.remote_endpoint_defined
;