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