cosmetics
[tomato.git] / release / src / router / openvpn / route.h
blob6f713ca8f29c334111816224e069c0f888bcc90b
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.
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.
29 #ifndef ROUTE_H
30 #define ROUTE_H
32 #include "tun.h"
33 #include "misc.h"
35 #define MAX_ROUTES_DEFAULT 100
37 #ifdef WIN32
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
45 #endif
48 * Route add flags (must stay clear of ROUTE_METHOD bits)
50 #define ROUTE_DELETE_FIRST 4
52 struct route_bypass
54 # define N_ROUTE_BYPASS 8
55 int n_bypass;
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;
68 int default_metric;
69 bool default_metric_defined;
72 struct route_option {
73 const char *network;
74 const char *netmask;
75 const char *gateway;
76 const char *metric;
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 {
89 unsigned int flags;
90 int capacity;
91 int n;
92 struct route_option routes[EMPTY_ARRAY_SIZE];
95 struct route {
96 bool defined;
97 const struct route_option *option;
98 in_addr_t network;
99 in_addr_t netmask;
100 in_addr_t gateway;
101 bool metric_defined;
102 int metric;
105 struct route_list {
106 bool routes_added;
107 struct route_special_addr spec;
108 unsigned int flags;
109 bool did_redirect_default_gateway;
110 bool did_local;
111 int capacity;
112 int n;
113 struct route routes[EMPTY_ARRAY_SIZE];
116 #if P2MP
117 /* internal OpenVPN route */
118 struct iroute {
119 in_addr_t network;
120 int netbits;
121 struct iroute *next;
123 #endif
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,
134 const char *network,
135 const char *netmask,
136 const char *gateway,
137 const char *metric);
139 bool init_route_list (struct route_list *rl,
140 const struct route_option_list *opt,
141 const char *remote_endpoint,
142 int default_metric,
143 in_addr_t remote_host,
144 struct env_set *es);
146 void route_list_add_default_gateway (struct route_list *rl,
147 struct env_set *es,
148 const in_addr_t addr);
150 void add_routes (struct route_list *rl,
151 const struct tuntap *tt,
152 unsigned int flags,
153 const struct env_set *es);
155 void delete_routes (struct route_list *rl,
156 const struct tuntap *tt,
157 unsigned int flags,
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
174 #define TLA_LOCAL 2
175 int test_local_addr (const in_addr_t addr);
177 #if AUTO_USERID
178 bool get_default_gateway_mac_addr (unsigned char *macaddr);
179 #endif
181 #ifdef ENABLE_DEBUG
182 void print_route_options (const struct route_option_list *rol,
183 int level);
184 #endif
186 void print_routes (const struct route_list *rl, int level);
188 #ifdef WIN32
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);
195 #else
196 static inline bool test_routes (const struct route_list *rl, const struct tuntap *tt) { return true; }
197 #endif
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;
205 in_addr_t mask = 0;
206 if (netbits > 0 && netbits <= addrlen)
207 mask = ~0 << (addrlen-netbits);
208 return mask;
211 static inline bool
212 route_list_default_gateway_needed (const struct route_list *rl)
214 if (!rl)
215 return false;
216 else
217 return !rl->spec.remote_endpoint_defined;
220 #endif