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-2005 OpenVPN Solutions LLC <info@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 100
39 * Windows route methods
41 #define ROUTE_METHOD_IPAPI 0 /* use IP helper API */
42 #define ROUTE_METHOD_EXE 1 /* use route.exe */
43 #define ROUTE_METHOD_MASK 1
47 * Route add flags (must stay clear of ROUTE_METHOD bits)
49 #define ROUTE_DELETE_FIRST 2
51 struct route_special_addr
53 in_addr_t remote_endpoint
;
54 bool remote_endpoint_defined
;
55 in_addr_t net_gateway
;
56 bool net_gateway_defined
;
57 in_addr_t remote_host
;
58 bool remote_host_defined
;
68 struct route_option_list
{
70 bool redirect_default_gateway
;
73 struct route_option routes
[MAX_ROUTES
];
78 const struct route_option
*option
;
88 struct route_special_addr spec
;
89 bool redirect_default_gateway
;
92 bool did_redirect_default_gateway
;
95 struct route routes
[MAX_ROUTES
];
99 /* internal OpenVPN route */
107 struct route_option_list
*new_route_option_list (struct gc_arena
*a
);
109 struct route_list
*new_route_list (struct gc_arena
*a
);
111 void add_route_to_option_list (struct route_option_list
*l
,
117 void clear_route_list (struct route_list
*rl
);
119 bool init_route_list (struct route_list
*rl
,
120 const struct route_option_list
*opt
,
121 const char *remote_endpoint
,
122 in_addr_t remote_host
,
125 void add_routes (struct route_list
*rl
,
126 const struct tuntap
*tt
,
128 const struct env_set
*es
);
130 void delete_routes (struct route_list
*rl
,
131 const struct tuntap
*tt
,
133 const struct env_set
*es
);
135 void setenv_routes (struct env_set
*es
, const struct route_list
*rl
);
138 void print_route_options (const struct route_option_list
*rol
,
142 void print_routes (const struct route_list
*rl
, int level
);
146 void show_routes (int msglev
);
147 bool test_routes (const struct route_list
*rl
, const struct tuntap
*tt
);
148 bool add_route_ipapi (const struct route
*r
, const struct tuntap
*tt
);
149 bool del_route_ipapi (const struct route
*r
, const struct tuntap
*tt
);
152 static inline bool test_routes (const struct route_list
*rl
, const struct tuntap
*tt
) { return true; }
155 bool netmask_to_netbits (const in_addr_t network
, const in_addr_t netmask
, int *netbits
);
157 static inline in_addr_t
158 netbits_to_netmask (const int netbits
)
160 const int addrlen
= sizeof (in_addr_t
) * 8;
162 if (netbits
> 0 && netbits
<= addrlen
)
163 mask
= ~0 << (addrlen
-netbits
);