1 |
pcg |
1.1 |
/* |
2 |
|
|
vpn.C -- handle the protocol, encryption, handshaking etc. |
3 |
pcg |
1.28 |
Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 |
pcg |
1.1 |
|
5 |
pcg |
1.28 |
This file is part of GVPE. |
6 |
|
|
|
7 |
|
|
GVPE is free software; you can redistribute it and/or modify |
8 |
pcg |
1.1 |
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
pcg |
1.28 |
along with gvpe; if not, write to the Free Software |
19 |
pcg |
1.1 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include "config.h" |
23 |
|
|
|
24 |
|
|
#include <list> |
25 |
|
|
|
26 |
pcg |
1.23 |
#include <cstdio> |
27 |
|
|
#include <cstring> |
28 |
pcg |
1.1 |
#include <cstdlib> |
29 |
|
|
|
30 |
|
|
#include <sys/types.h> |
31 |
|
|
#include <sys/socket.h> |
32 |
|
|
#include <sys/wait.h> |
33 |
|
|
#include <errno.h> |
34 |
|
|
#include <time.h> |
35 |
|
|
#include <unistd.h> |
36 |
pcg |
1.11 |
#include <fcntl.h> |
37 |
pcg |
1.14 |
#include <sys/socket.h> |
38 |
pcg |
1.15 |
|
39 |
|
|
#include "netcompat.h" |
40 |
pcg |
1.1 |
|
41 |
|
|
#include "pidfile.h" |
42 |
|
|
|
43 |
|
|
#include "connection.h" |
44 |
|
|
#include "util.h" |
45 |
|
|
#include "vpn.h" |
46 |
|
|
|
47 |
pcg |
1.20 |
vpn network; // THE vpn (bad design...) |
48 |
|
|
|
49 |
pcg |
1.1 |
///////////////////////////////////////////////////////////////////////////// |
50 |
|
|
|
51 |
pcg |
1.33 |
void |
52 |
|
|
vpn::script_init_env () |
53 |
pcg |
1.1 |
{ |
54 |
|
|
// the tunnel device mtu should be the physical mtu - overhead |
55 |
|
|
// the tricky part is rounding to the cipher key blocksize |
56 |
|
|
int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
57 |
|
|
mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
58 |
|
|
mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
59 |
|
|
mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
60 |
|
|
|
61 |
|
|
char *env; |
62 |
pcg |
1.21 |
asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
63 |
|
|
asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
64 |
|
|
asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
65 |
|
|
asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
66 |
|
|
asprintf (&env, "MTU=%d", mtu); putenv (env); |
67 |
pcg |
1.34 |
asprintf (&env, "NODES=%d", conns.size ()); putenv (env); |
68 |
|
|
asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
69 |
|
|
|
70 |
|
|
conns [THISNODE->id - 1]->script_init_env (""); |
71 |
pcg |
1.33 |
|
72 |
pcg |
1.34 |
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
73 |
|
|
{ |
74 |
|
|
char ext[16]; |
75 |
|
|
snprintf (ext, 16, "_%d", (*c)->conf->id); |
76 |
|
|
(*c)->script_init_env (ext); |
77 |
|
|
} |
78 |
pcg |
1.33 |
} |
79 |
|
|
|
80 |
|
|
const char *vpn::script_if_init () |
81 |
|
|
{ |
82 |
|
|
script_init_env (); |
83 |
pcg |
1.1 |
|
84 |
pcg |
1.33 |
return tap->if_up (); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
const char *vpn::script_if_up () |
88 |
|
|
{ |
89 |
|
|
script_init_env (); |
90 |
|
|
|
91 |
|
|
char *filename; |
92 |
|
|
asprintf (&filename, |
93 |
|
|
"%s/%s", |
94 |
|
|
confbase, |
95 |
|
|
::conf.script_if_up ? ::conf.script_if_up : "if-up"); |
96 |
|
|
|
97 |
|
|
return filename; |
98 |
pcg |
1.1 |
} |
99 |
|
|
|
100 |
|
|
int |
101 |
|
|
vpn::setup () |
102 |
|
|
{ |
103 |
pcg |
1.8 |
ipv4_fd = -1; |
104 |
pcg |
1.1 |
|
105 |
pcg |
1.8 |
if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
106 |
pcg |
1.1 |
{ |
107 |
pcg |
1.8 |
ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
108 |
pcg |
1.1 |
|
109 |
pcg |
1.8 |
if (ipv4_fd < 0) |
110 |
pcg |
1.1 |
return -1; |
111 |
|
|
|
112 |
pcg |
1.11 |
fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
113 |
pcg |
1.37 |
fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC); |
114 |
pcg |
1.11 |
|
115 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
116 |
pcg |
1.1 |
// this I really consider a linux bug. I am neither connected |
117 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
118 |
|
|
// fragment for me sometimes. |
119 |
|
|
{ |
120 |
|
|
int oval = IP_PMTUDISC_DONT; |
121 |
pcg |
1.8 |
setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
122 |
pcg |
1.1 |
} |
123 |
|
|
#endif |
124 |
|
|
|
125 |
pcg |
1.9 |
sockinfo si (THISNODE, PROT_IPv4); |
126 |
|
|
|
127 |
|
|
if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
128 |
|
|
{ |
129 |
|
|
slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
130 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
131 |
pcg |
1.9 |
} |
132 |
|
|
|
133 |
pcg |
1.22 |
ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); |
134 |
pcg |
1.1 |
} |
135 |
|
|
|
136 |
pcg |
1.8 |
udpv4_fd = -1; |
137 |
|
|
|
138 |
|
|
if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
139 |
pcg |
1.1 |
{ |
140 |
pcg |
1.8 |
udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
141 |
pcg |
1.1 |
|
142 |
pcg |
1.8 |
if (udpv4_fd < 0) |
143 |
pcg |
1.1 |
return -1; |
144 |
|
|
|
145 |
pcg |
1.11 |
fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
146 |
pcg |
1.37 |
fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); |
147 |
pcg |
1.11 |
|
148 |
pcg |
1.8 |
// standard daemon practise... |
149 |
|
|
{ |
150 |
|
|
int oval = 1; |
151 |
|
|
setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
152 |
|
|
} |
153 |
|
|
|
154 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
155 |
pcg |
1.9 |
// this I really consider a linux bug. I am neither connected |
156 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
157 |
|
|
// fragment for me sometimes. |
158 |
|
|
{ |
159 |
|
|
int oval = IP_PMTUDISC_DONT; |
160 |
|
|
setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
161 |
|
|
} |
162 |
|
|
#endif |
163 |
|
|
|
164 |
pcg |
1.8 |
sockinfo si (THISNODE, PROT_UDPv4); |
165 |
pcg |
1.2 |
|
166 |
pcg |
1.8 |
if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
167 |
pcg |
1.1 |
{ |
168 |
pcg |
1.8 |
slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
169 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
170 |
pcg |
1.1 |
} |
171 |
|
|
|
172 |
pcg |
1.22 |
udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); |
173 |
pcg |
1.9 |
} |
174 |
|
|
|
175 |
|
|
icmpv4_fd = -1; |
176 |
|
|
|
177 |
|
|
#if ENABLE_ICMP |
178 |
|
|
if (THISNODE->protocols & PROT_ICMPv4) |
179 |
|
|
{ |
180 |
|
|
icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
181 |
|
|
|
182 |
|
|
if (icmpv4_fd < 0) |
183 |
|
|
return -1; |
184 |
|
|
|
185 |
pcg |
1.11 |
fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
186 |
pcg |
1.37 |
fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); |
187 |
pcg |
1.11 |
|
188 |
pcg |
1.9 |
#ifdef ICMP_FILTER |
189 |
|
|
{ |
190 |
|
|
icmp_filter oval; |
191 |
|
|
oval.data = 0xffffffff; |
192 |
|
|
if (::conf.icmp_type < 32) |
193 |
|
|
oval.data &= ~(1 << ::conf.icmp_type); |
194 |
|
|
|
195 |
|
|
setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
196 |
|
|
} |
197 |
|
|
#endif |
198 |
|
|
|
199 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
200 |
pcg |
1.1 |
// this I really consider a linux bug. I am neither connected |
201 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
202 |
|
|
// fragment for me sometimes. |
203 |
|
|
{ |
204 |
|
|
int oval = IP_PMTUDISC_DONT; |
205 |
pcg |
1.36 |
setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
206 |
pcg |
1.1 |
} |
207 |
|
|
#endif |
208 |
|
|
|
209 |
pcg |
1.9 |
sockinfo si (THISNODE, PROT_ICMPv4); |
210 |
|
|
|
211 |
|
|
if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
212 |
|
|
{ |
213 |
|
|
slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
214 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
215 |
pcg |
1.9 |
} |
216 |
|
|
|
217 |
pcg |
1.22 |
icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); |
218 |
pcg |
1.1 |
} |
219 |
pcg |
1.9 |
#endif |
220 |
pcg |
1.1 |
|
221 |
pcg |
1.8 |
tcpv4_fd = -1; |
222 |
|
|
|
223 |
pcg |
1.2 |
#if ENABLE_TCP |
224 |
pcg |
1.8 |
if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
225 |
pcg |
1.2 |
{ |
226 |
|
|
tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
227 |
|
|
|
228 |
|
|
if (tcpv4_fd < 0) |
229 |
|
|
return -1; |
230 |
|
|
|
231 |
pcg |
1.11 |
fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
232 |
pcg |
1.37 |
fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); |
233 |
pcg |
1.11 |
|
234 |
pcg |
1.3 |
// standard daemon practise... |
235 |
|
|
{ |
236 |
|
|
int oval = 1; |
237 |
|
|
setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
238 |
|
|
} |
239 |
|
|
|
240 |
pcg |
1.2 |
sockinfo si (THISNODE, PROT_TCPv4); |
241 |
|
|
|
242 |
|
|
if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
243 |
|
|
{ |
244 |
|
|
slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
245 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
246 |
pcg |
1.2 |
} |
247 |
|
|
|
248 |
|
|
if (listen (tcpv4_fd, 5)) |
249 |
|
|
{ |
250 |
|
|
slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
251 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
252 |
pcg |
1.2 |
} |
253 |
|
|
|
254 |
pcg |
1.22 |
tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); |
255 |
pcg |
1.2 |
} |
256 |
|
|
#endif |
257 |
|
|
|
258 |
pcg |
1.24 |
#if ENABLE_DNS |
259 |
|
|
if (THISNODE->protocols & PROT_DNSv4) |
260 |
|
|
{ |
261 |
pcg |
1.29 |
dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
262 |
|
|
|
263 |
pcg |
1.24 |
dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
264 |
|
|
|
265 |
|
|
if (dnsv4_fd < 0) |
266 |
|
|
return -1; |
267 |
|
|
|
268 |
pcg |
1.37 |
fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK); |
269 |
|
|
fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC); |
270 |
|
|
|
271 |
pcg |
1.35 |
# if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
272 |
pcg |
1.32 |
// this I really consider a linux bug. I am neither connected |
273 |
|
|
// nor do I fragment myself. Linux still sets DF and doesn't |
274 |
|
|
// fragment for me sometimes. |
275 |
|
|
{ |
276 |
|
|
int oval = IP_PMTUDISC_DONT; |
277 |
pcg |
1.35 |
setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
278 |
pcg |
1.32 |
} |
279 |
pcg |
1.35 |
# endif |
280 |
pcg |
1.32 |
|
281 |
pcg |
1.24 |
// standard daemon practise... |
282 |
|
|
{ |
283 |
|
|
int oval = 1; |
284 |
pcg |
1.31 |
setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
285 |
pcg |
1.24 |
} |
286 |
|
|
|
287 |
pcg |
1.31 |
sockinfo si (THISNODE->dns_hostname, |
288 |
|
|
THISNODE->dns_hostname ? THISNODE->dns_port : 0, |
289 |
|
|
PROT_DNSv4); |
290 |
pcg |
1.24 |
|
291 |
|
|
if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
292 |
|
|
{ |
293 |
|
|
slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
294 |
|
|
exit (EXIT_FAILURE); |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
298 |
|
|
} |
299 |
|
|
#endif |
300 |
|
|
|
301 |
pcg |
1.34 |
///////////////////////////////////////////////////////////////////////////// |
302 |
|
|
|
303 |
|
|
reconnect_all (); |
304 |
|
|
|
305 |
|
|
///////////////////////////////////////////////////////////////////////////// |
306 |
|
|
|
307 |
pcg |
1.1 |
tap = new tap_device (); |
308 |
|
|
if (!tap) //D this, of course, never catches |
309 |
|
|
{ |
310 |
|
|
slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
311 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
312 |
pcg |
1.1 |
} |
313 |
|
|
|
314 |
pcg |
1.37 |
fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
315 |
|
|
|
316 |
pcg |
1.33 |
if (tap->if_up () && |
317 |
|
|
!run_script (run_script_cb (this, &vpn::script_if_init), true)) |
318 |
|
|
{ |
319 |
|
|
slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
320 |
|
|
tap->if_up ()); |
321 |
|
|
exit (EXIT_FAILURE); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) |
325 |
|
|
{ |
326 |
|
|
slog (L_ERR, _("if-up command execution failed, exiting.")); |
327 |
|
|
exit (EXIT_FAILURE); |
328 |
|
|
} |
329 |
pcg |
1.1 |
|
330 |
pcg |
1.22 |
tap_ev_watcher.start (tap->fd, EVENT_READ); |
331 |
pcg |
1.1 |
|
332 |
|
|
return 0; |
333 |
|
|
} |
334 |
|
|
|
335 |
pcg |
1.7 |
bool |
336 |
pcg |
1.1 |
vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
337 |
|
|
{ |
338 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_TOS) |
339 |
pcg |
1.1 |
setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
340 |
pcg |
1.14 |
#endif |
341 |
pcg |
1.1 |
sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
342 |
pcg |
1.7 |
|
343 |
|
|
return true; |
344 |
pcg |
1.1 |
} |
345 |
|
|
|
346 |
pcg |
1.9 |
static u16 |
347 |
|
|
ipv4_checksum (u16 *data, unsigned int len) |
348 |
|
|
{ |
349 |
|
|
// use 32 bit accumulator and fold back carry bits at the end |
350 |
|
|
u32 sum = 0; |
351 |
|
|
|
352 |
|
|
while (len > 1) |
353 |
|
|
{ |
354 |
|
|
sum += *data++; |
355 |
|
|
len -= 2; |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
// odd byte left? |
359 |
|
|
if (len) |
360 |
|
|
sum += *(u8 *)data; |
361 |
|
|
|
362 |
|
|
// add back carry bits |
363 |
|
|
sum = (sum >> 16) + (sum & 0xffff); // lo += hi |
364 |
|
|
sum += (sum >> 16); // carry |
365 |
|
|
|
366 |
|
|
return ~sum; |
367 |
|
|
} |
368 |
|
|
|
369 |
pcg |
1.13 |
#if ENABLE_ICMP |
370 |
pcg |
1.9 |
bool |
371 |
|
|
vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
372 |
|
|
{ |
373 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_TOS) |
374 |
pcg |
1.9 |
setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
375 |
pcg |
1.14 |
#endif |
376 |
pcg |
1.9 |
|
377 |
|
|
pkt->unshift_hdr (4); |
378 |
|
|
|
379 |
pcg |
1.14 |
icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
380 |
pcg |
1.9 |
hdr->type = ::conf.icmp_type; |
381 |
|
|
hdr->code = 255; |
382 |
|
|
hdr->checksum = 0; |
383 |
|
|
hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
384 |
|
|
|
385 |
|
|
sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
386 |
|
|
|
387 |
|
|
return true; |
388 |
|
|
} |
389 |
pcg |
1.13 |
#endif |
390 |
pcg |
1.9 |
|
391 |
pcg |
1.7 |
bool |
392 |
pcg |
1.1 |
vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
393 |
|
|
{ |
394 |
pcg |
1.14 |
#if defined(SOL_IP) && defined(IP_TOS) |
395 |
pcg |
1.1 |
setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
396 |
pcg |
1.14 |
#endif |
397 |
pcg |
1.1 |
sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
398 |
pcg |
1.7 |
|
399 |
|
|
return true; |
400 |
pcg |
1.1 |
} |
401 |
|
|
|
402 |
|
|
void |
403 |
pcg |
1.18 |
vpn::inject_data_packet (tap_packet *pkt, int dst) |
404 |
|
|
{ |
405 |
|
|
if (dst) |
406 |
|
|
{ |
407 |
|
|
// unicast |
408 |
|
|
if (dst != THISNODE->id) |
409 |
|
|
conns[dst - 1]->inject_data_packet (pkt); |
410 |
|
|
} |
411 |
|
|
else |
412 |
|
|
{ |
413 |
|
|
// broadcast, this is ugly, but due to the security policy |
414 |
|
|
// we have to connect to all hosts... |
415 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
416 |
|
|
if ((*c)->conf != THISNODE) |
417 |
|
|
(*c)->inject_data_packet (pkt, true); |
418 |
|
|
} |
419 |
|
|
} |
420 |
|
|
|
421 |
|
|
void |
422 |
pcg |
1.1 |
vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
423 |
|
|
{ |
424 |
|
|
unsigned int src = pkt->src (); |
425 |
|
|
unsigned int dst = pkt->dst (); |
426 |
|
|
|
427 |
|
|
slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
428 |
|
|
(const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
429 |
|
|
|
430 |
|
|
if (src == 0 || src > conns.size () |
431 |
|
|
|| dst > conns.size () |
432 |
|
|
|| pkt->typ () >= vpn_packet::PT_MAX) |
433 |
|
|
slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
434 |
|
|
(const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
435 |
pcg |
1.6 |
else if (dst > conns.size ()) |
436 |
|
|
slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
437 |
|
|
(const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
438 |
pcg |
1.1 |
else |
439 |
|
|
{ |
440 |
|
|
connection *c = conns[src - 1]; |
441 |
|
|
|
442 |
pcg |
1.13 |
if (dst == 0) |
443 |
|
|
slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
444 |
pcg |
1.1 |
c->conf->nodename, (const char *)rsi); |
445 |
pcg |
1.18 |
else if (dst != THISNODE->id) |
446 |
pcg |
1.6 |
{ |
447 |
|
|
if (THISNODE->routerprio) |
448 |
|
|
// the tos setting gets lost here. who cares. |
449 |
|
|
conns[dst - 1]->inject_vpn_packet (pkt); |
450 |
|
|
else |
451 |
|
|
slog (L_WARN, |
452 |
|
|
_("%s(%s): forwarding request (=> %s), but we are no router"), |
453 |
|
|
c->conf->nodename, (const char *)rsi, |
454 |
|
|
conns[dst - 1]->conf->nodename); |
455 |
|
|
} |
456 |
pcg |
1.1 |
else |
457 |
|
|
c->recv_vpn_packet (pkt, rsi); |
458 |
|
|
} |
459 |
|
|
} |
460 |
|
|
|
461 |
pcg |
1.31 |
bool |
462 |
|
|
vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
463 |
|
|
{ |
464 |
|
|
switch (si.prot) |
465 |
|
|
{ |
466 |
|
|
case PROT_IPv4: |
467 |
|
|
return send_ipv4_packet (pkt, si, tos); |
468 |
|
|
case PROT_UDPv4: |
469 |
|
|
return send_udpv4_packet (pkt, si, tos); |
470 |
|
|
#if ENABLE_TCP |
471 |
|
|
case PROT_TCPv4: |
472 |
|
|
return send_tcpv4_packet (pkt, si, tos); |
473 |
|
|
#endif |
474 |
|
|
#if ENABLE_ICMP |
475 |
|
|
case PROT_ICMPv4: |
476 |
|
|
return send_icmpv4_packet (pkt, si, tos); |
477 |
|
|
#endif |
478 |
|
|
#if ENABLE_DNS |
479 |
|
|
case PROT_DNSv4: |
480 |
|
|
return send_dnsv4_packet (pkt, si, tos); |
481 |
|
|
#endif |
482 |
|
|
|
483 |
|
|
default: |
484 |
|
|
slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
485 |
|
|
} |
486 |
|
|
|
487 |
|
|
return false; |
488 |
|
|
} |
489 |
|
|
|
490 |
pcg |
1.1 |
void |
491 |
pcg |
1.9 |
vpn::ipv4_ev (io_watcher &w, short revents) |
492 |
pcg |
1.1 |
{ |
493 |
pcg |
1.22 |
if (revents & EVENT_READ) |
494 |
pcg |
1.1 |
{ |
495 |
|
|
vpn_packet *pkt = new vpn_packet; |
496 |
|
|
struct sockaddr_in sa; |
497 |
|
|
socklen_t sa_len = sizeof (sa); |
498 |
|
|
int len; |
499 |
|
|
|
500 |
pcg |
1.5 |
len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
501 |
pcg |
1.1 |
|
502 |
pcg |
1.9 |
sockinfo si(sa, PROT_IPv4); |
503 |
pcg |
1.1 |
|
504 |
|
|
if (len > 0) |
505 |
|
|
{ |
506 |
|
|
pkt->len = len; |
507 |
|
|
|
508 |
pcg |
1.30 |
// raw sockets deliver the ipv4 header, but don't expect it on sends |
509 |
pcg |
1.9 |
pkt->skip_hdr (IP_OVERHEAD); |
510 |
|
|
|
511 |
pcg |
1.1 |
recv_vpn_packet (pkt, si); |
512 |
|
|
} |
513 |
|
|
else |
514 |
|
|
{ |
515 |
|
|
// probably ECONNRESET or somesuch |
516 |
pcg |
1.9 |
slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
517 |
pcg |
1.1 |
} |
518 |
|
|
|
519 |
|
|
delete pkt; |
520 |
|
|
} |
521 |
|
|
else |
522 |
|
|
{ |
523 |
|
|
slog (L_ERR, |
524 |
pcg |
1.30 |
_("FATAL: unknown revents %08x in socket, terminating\n"), |
525 |
|
|
revents); |
526 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
527 |
pcg |
1.1 |
} |
528 |
|
|
} |
529 |
|
|
|
530 |
pcg |
1.13 |
#if ENABLE_ICMP |
531 |
pcg |
1.1 |
void |
532 |
pcg |
1.9 |
vpn::icmpv4_ev (io_watcher &w, short revents) |
533 |
pcg |
1.1 |
{ |
534 |
pcg |
1.22 |
if (revents & EVENT_READ) |
535 |
pcg |
1.1 |
{ |
536 |
|
|
vpn_packet *pkt = new vpn_packet; |
537 |
|
|
struct sockaddr_in sa; |
538 |
|
|
socklen_t sa_len = sizeof (sa); |
539 |
|
|
int len; |
540 |
|
|
|
541 |
pcg |
1.5 |
len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
542 |
pcg |
1.1 |
|
543 |
pcg |
1.9 |
sockinfo si(sa, PROT_ICMPv4); |
544 |
pcg |
1.1 |
|
545 |
|
|
if (len > 0) |
546 |
|
|
{ |
547 |
|
|
pkt->len = len; |
548 |
|
|
|
549 |
pcg |
1.14 |
icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
550 |
pcg |
1.9 |
|
551 |
|
|
if (hdr->type == ::conf.icmp_type |
552 |
|
|
&& hdr->code == 255) |
553 |
|
|
{ |
554 |
|
|
// raw sockets deliver the ipv4, but don't expect it on sends |
555 |
|
|
// this is slow, but... |
556 |
|
|
pkt->skip_hdr (ICMP_OVERHEAD); |
557 |
|
|
|
558 |
|
|
recv_vpn_packet (pkt, si); |
559 |
|
|
} |
560 |
|
|
} |
561 |
|
|
else |
562 |
|
|
{ |
563 |
|
|
// probably ECONNRESET or somesuch |
564 |
|
|
slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
565 |
|
|
} |
566 |
|
|
|
567 |
|
|
delete pkt; |
568 |
|
|
} |
569 |
|
|
else |
570 |
|
|
{ |
571 |
|
|
slog (L_ERR, |
572 |
|
|
_("FATAL: unknown revents %08x in socket, terminating\n"), |
573 |
|
|
revents); |
574 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
575 |
pcg |
1.9 |
} |
576 |
|
|
} |
577 |
pcg |
1.13 |
#endif |
578 |
pcg |
1.9 |
|
579 |
|
|
void |
580 |
|
|
vpn::udpv4_ev (io_watcher &w, short revents) |
581 |
|
|
{ |
582 |
pcg |
1.22 |
if (revents & EVENT_READ) |
583 |
pcg |
1.9 |
{ |
584 |
|
|
vpn_packet *pkt = new vpn_packet; |
585 |
|
|
struct sockaddr_in sa; |
586 |
|
|
socklen_t sa_len = sizeof (sa); |
587 |
|
|
int len; |
588 |
|
|
|
589 |
|
|
len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
590 |
|
|
|
591 |
|
|
sockinfo si(sa, PROT_UDPv4); |
592 |
|
|
|
593 |
|
|
if (len > 0) |
594 |
|
|
{ |
595 |
|
|
pkt->len = len; |
596 |
pcg |
1.1 |
|
597 |
|
|
recv_vpn_packet (pkt, si); |
598 |
|
|
} |
599 |
|
|
else |
600 |
|
|
{ |
601 |
|
|
// probably ECONNRESET or somesuch |
602 |
pcg |
1.9 |
slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
603 |
pcg |
1.1 |
} |
604 |
|
|
|
605 |
|
|
delete pkt; |
606 |
|
|
} |
607 |
|
|
else |
608 |
|
|
{ |
609 |
|
|
slog (L_ERR, |
610 |
|
|
_("FATAL: unknown revents %08x in socket, terminating\n"), |
611 |
|
|
revents); |
612 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
613 |
pcg |
1.1 |
} |
614 |
|
|
} |
615 |
pcg |
1.2 |
|
616 |
|
|
void |
617 |
pcg |
1.3 |
vpn::tap_ev (io_watcher &w, short revents) |
618 |
pcg |
1.1 |
{ |
619 |
pcg |
1.22 |
if (revents & EVENT_READ) |
620 |
pcg |
1.1 |
{ |
621 |
|
|
/* process data */ |
622 |
|
|
tap_packet *pkt; |
623 |
|
|
|
624 |
|
|
pkt = tap->recv (); |
625 |
|
|
|
626 |
pcg |
1.17 |
if (!pkt) |
627 |
|
|
return; |
628 |
pcg |
1.1 |
|
629 |
pcg |
1.17 |
if (pkt->len > 14) |
630 |
pcg |
1.1 |
{ |
631 |
pcg |
1.17 |
int dst = mac2id (pkt->dst); |
632 |
|
|
int src = mac2id (pkt->src); |
633 |
pcg |
1.1 |
|
634 |
pcg |
1.17 |
if (src != THISNODE->id) |
635 |
|
|
{ |
636 |
pcg |
1.30 |
slog (L_ERR, _("FATAL: tap packet not originating on current node received (if-up script not working properly?), exiting.")); |
637 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
638 |
pcg |
1.17 |
} |
639 |
pcg |
1.1 |
|
640 |
pcg |
1.17 |
if (dst == THISNODE->id) |
641 |
pcg |
1.1 |
{ |
642 |
pcg |
1.17 |
slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
643 |
pcg |
1.23 |
exit (EXIT_FAILURE); |
644 |
pcg |
1.1 |
} |
645 |
pcg |
1.17 |
|
646 |
|
|
if (dst > conns.size ()) |
647 |
|
|
slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
648 |
pcg |
1.1 |
else |
649 |
pcg |
1.18 |
inject_data_packet (pkt, dst); |
650 |
pcg |
1.1 |
} |
651 |
|
|
|
652 |
|
|
delete pkt; |
653 |
|
|
} |
654 |
|
|
else |
655 |
|
|
abort (); |
656 |
|
|
} |
657 |
|
|
|
658 |
|
|
void |
659 |
pcg |
1.3 |
vpn::event_cb (time_watcher &w) |
660 |
pcg |
1.1 |
{ |
661 |
|
|
if (events) |
662 |
|
|
{ |
663 |
|
|
if (events & EVENT_SHUTDOWN) |
664 |
|
|
{ |
665 |
|
|
slog (L_INFO, _("preparing shutdown...")); |
666 |
|
|
|
667 |
|
|
shutdown_all (); |
668 |
pcg |
1.24 |
remove_pid (conf.pidfilename); |
669 |
pcg |
1.1 |
slog (L_INFO, _("terminating")); |
670 |
pcg |
1.23 |
exit (EXIT_SUCCESS); |
671 |
pcg |
1.1 |
} |
672 |
|
|
|
673 |
|
|
if (events & EVENT_RECONNECT) |
674 |
|
|
{ |
675 |
|
|
slog (L_INFO, _("forced reconnect")); |
676 |
|
|
|
677 |
|
|
reconnect_all (); |
678 |
|
|
} |
679 |
|
|
|
680 |
|
|
events = 0; |
681 |
|
|
} |
682 |
|
|
} |
683 |
|
|
|
684 |
|
|
void |
685 |
|
|
vpn::shutdown_all () |
686 |
|
|
{ |
687 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
688 |
|
|
(*c)->shutdown (); |
689 |
|
|
} |
690 |
|
|
|
691 |
|
|
void |
692 |
|
|
vpn::reconnect_all () |
693 |
|
|
{ |
694 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
695 |
|
|
delete *c; |
696 |
|
|
|
697 |
|
|
conns.clear (); |
698 |
|
|
|
699 |
|
|
connection_init (); |
700 |
|
|
|
701 |
|
|
for (configuration::node_vector::iterator i = conf.nodes.begin (); |
702 |
|
|
i != conf.nodes.end (); ++i) |
703 |
|
|
{ |
704 |
pcg |
1.27 |
connection *conn = new connection (this, *i); |
705 |
pcg |
1.1 |
conns.push_back (conn); |
706 |
|
|
conn->establish_connection (); |
707 |
|
|
} |
708 |
|
|
} |
709 |
|
|
|
710 |
|
|
connection *vpn::find_router () |
711 |
|
|
{ |
712 |
pcg |
1.26 |
u32 prio = 1; |
713 |
pcg |
1.1 |
connection *router = 0; |
714 |
|
|
|
715 |
|
|
for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
716 |
|
|
{ |
717 |
|
|
connection *c = *i; |
718 |
|
|
|
719 |
pcg |
1.13 |
if (c->conf->routerprio > prio |
720 |
pcg |
1.11 |
&& c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
721 |
|
|
&& c->ictx && c->octx |
722 |
pcg |
1.13 |
&& c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
723 |
pcg |
1.1 |
{ |
724 |
|
|
prio = c->conf->routerprio; |
725 |
|
|
router = c; |
726 |
|
|
} |
727 |
|
|
} |
728 |
|
|
|
729 |
|
|
return router; |
730 |
|
|
} |
731 |
|
|
|
732 |
pcg |
1.7 |
void vpn::send_connect_request (int id) |
733 |
pcg |
1.1 |
{ |
734 |
|
|
connection *c = find_router (); |
735 |
|
|
|
736 |
|
|
if (c) |
737 |
pcg |
1.7 |
c->send_connect_request (id); |
738 |
|
|
else |
739 |
|
|
// no router found, aggressively connect to all routers |
740 |
|
|
for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
741 |
pcg |
1.13 |
if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
742 |
pcg |
1.7 |
(*i)->establish_connection (); |
743 |
pcg |
1.1 |
} |
744 |
|
|
|
745 |
|
|
void |
746 |
|
|
connection::dump_status () |
747 |
|
|
{ |
748 |
|
|
slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); |
749 |
|
|
slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), |
750 |
|
|
connectmode, conf->connectmode, (const char *)si, (int)prot_minor); |
751 |
|
|
slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), |
752 |
|
|
(long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); |
753 |
|
|
slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"), |
754 |
|
|
(long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at)); |
755 |
|
|
} |
756 |
|
|
|
757 |
|
|
void |
758 |
|
|
vpn::dump_status () |
759 |
|
|
{ |
760 |
|
|
slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); |
761 |
|
|
|
762 |
|
|
for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
763 |
|
|
(*c)->dump_status (); |
764 |
|
|
|
765 |
|
|
slog (L_NOTICE, _("END status dump")); |
766 |
|
|
} |
767 |
|
|
|
768 |
|
|
vpn::vpn (void) |
769 |
pcg |
1.9 |
: event (this, &vpn::event_cb) |
770 |
|
|
, udpv4_ev_watcher (this, &vpn::udpv4_ev) |
771 |
|
|
, ipv4_ev_watcher (this, &vpn::ipv4_ev) |
772 |
pcg |
1.2 |
#if ENABLE_TCP |
773 |
pcg |
1.9 |
, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
774 |
|
|
#endif |
775 |
|
|
#if ENABLE_ICMP |
776 |
|
|
, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
777 |
pcg |
1.24 |
#endif |
778 |
|
|
#if ENABLE_DNS |
779 |
|
|
, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) |
780 |
pcg |
1.2 |
#endif |
781 |
pcg |
1.9 |
, tap_ev_watcher (this, &vpn::tap_ev) |
782 |
pcg |
1.1 |
{ |
783 |
|
|
} |
784 |
|
|
|
785 |
|
|
vpn::~vpn () |
786 |
|
|
{ |
787 |
|
|
} |
788 |
|
|
|