1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
9 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
10 | (at your option) any later version. |
10 | option) any later version. |
11 | |
11 | |
12 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 | GNU General Public License for more details. |
15 | Public License for more details. |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License along |
18 | along with gvpe; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
20 | */ |
30 | */ |
21 | |
31 | |
22 | #include "config.h" |
32 | #include "config.h" |
23 | |
33 | |
24 | #include <list> |
34 | #include <list> |
… | |
… | |
87 | snprintf (ext, 16, "_%d", (*c)->conf->id); |
97 | snprintf (ext, 16, "_%d", (*c)->conf->id); |
88 | (*c)->script_init_env (ext); |
98 | (*c)->script_init_env (ext); |
89 | } |
99 | } |
90 | } |
100 | } |
91 | |
101 | |
|
|
102 | inline const char * |
92 | const char *vpn::script_if_init () |
103 | vpn::script_if_init () |
93 | { |
104 | { |
94 | script_init_env (); |
105 | script_init_env (); |
95 | |
106 | |
96 | return tap->if_up (); |
107 | return tap->if_up (); |
97 | } |
108 | } |
98 | |
109 | |
|
|
110 | inline const char * |
99 | const char *vpn::script_if_up () |
111 | vpn::script_if_up () |
100 | { |
112 | { |
101 | script_init_env (); |
113 | script_init_env (); |
102 | |
114 | |
103 | char *filename; |
115 | char *filename; |
104 | asprintf (&filename, |
116 | asprintf (&filename, |
… | |
… | |
110 | } |
122 | } |
111 | |
123 | |
112 | int |
124 | int |
113 | vpn::setup () |
125 | vpn::setup () |
114 | { |
126 | { |
|
|
127 | int success = 0; |
|
|
128 | |
115 | ipv4_tos = -1; |
129 | ipv4_tos = -1; |
116 | ipv4_fd = -1; |
130 | ipv4_fd = -1; |
117 | |
131 | |
118 | if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
132 | if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
119 | { |
133 | { |
… | |
… | |
137 | |
151 | |
138 | sockinfo si (THISNODE, PROT_IPv4); |
152 | sockinfo si (THISNODE, PROT_IPv4); |
139 | |
153 | |
140 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
154 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
141 | { |
155 | { |
142 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
156 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s, exiting."), (const char *)si, strerror (errno)); |
143 | exit (EXIT_FAILURE); |
157 | exit (EXIT_FAILURE); |
144 | } |
158 | } |
145 | |
159 | |
146 | ipv4_ev_watcher.start (ipv4_fd, EV_READ); |
160 | ipv4_ev_watcher.start (ipv4_fd, EV_READ); |
|
|
161 | ++success; |
147 | } |
162 | } |
|
|
163 | else |
|
|
164 | THISNODE->protocols &= ~PROT_IPv4; |
148 | |
165 | |
149 | udpv4_tos = -1; |
166 | udpv4_tos = -1; |
150 | udpv4_fd = -1; |
167 | udpv4_fd = -1; |
151 | |
168 | |
152 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
169 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
… | |
… | |
177 | |
194 | |
178 | sockinfo si (THISNODE, PROT_UDPv4); |
195 | sockinfo si (THISNODE, PROT_UDPv4); |
179 | |
196 | |
180 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
197 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
181 | { |
198 | { |
182 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
199 | slog (L_ERR, _("can't bind udpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
183 | exit (EXIT_FAILURE); |
200 | exit (EXIT_FAILURE); |
184 | } |
201 | } |
185 | |
202 | |
186 | udpv4_ev_watcher.start (udpv4_fd, EV_READ); |
203 | udpv4_ev_watcher.start (udpv4_fd, EV_READ); |
|
|
204 | ++success; |
187 | } |
205 | } |
|
|
206 | else |
|
|
207 | THISNODE->protocols &= ~PROT_UDPv4; |
188 | |
208 | |
189 | icmpv4_tos = -1; |
209 | icmpv4_tos = -1; |
190 | icmpv4_fd = -1; |
210 | icmpv4_fd = -1; |
191 | |
211 | |
192 | #if ENABLE_ICMP |
212 | #if ENABLE_ICMP |
… | |
… | |
223 | |
243 | |
224 | sockinfo si (THISNODE, PROT_ICMPv4); |
244 | sockinfo si (THISNODE, PROT_ICMPv4); |
225 | |
245 | |
226 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
246 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
227 | { |
247 | { |
228 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
248 | slog (L_ERR, _("can't bind icmpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
229 | exit (EXIT_FAILURE); |
249 | exit (EXIT_FAILURE); |
230 | } |
250 | } |
231 | |
251 | |
232 | icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); |
252 | icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); |
|
|
253 | ++success; |
233 | } |
254 | } |
234 | #endif |
255 | #endif |
235 | |
256 | |
236 | tcpv4_fd = -1; |
257 | tcpv4_fd = -1; |
237 | |
258 | |
… | |
… | |
254 | |
275 | |
255 | sockinfo si (THISNODE, PROT_TCPv4); |
276 | sockinfo si (THISNODE, PROT_TCPv4); |
256 | |
277 | |
257 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
278 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
258 | { |
279 | { |
259 | slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
280 | slog (L_ERR, _("can't bind tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
260 | exit (EXIT_FAILURE); |
281 | exit (EXIT_FAILURE); |
261 | } |
282 | } |
262 | |
283 | |
263 | if (listen (tcpv4_fd, 5)) |
284 | if (listen (tcpv4_fd, 5)) |
264 | { |
285 | { |
265 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
286 | slog (L_ERR, _("can't listen tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
266 | exit (EXIT_FAILURE); |
287 | exit (EXIT_FAILURE); |
267 | } |
288 | } |
268 | |
289 | |
269 | tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); |
290 | tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); |
|
|
291 | ++success; |
270 | } |
292 | } |
|
|
293 | else |
|
|
294 | THISNODE->protocols &= ~PROT_TCPv4; |
271 | #endif |
295 | #endif |
272 | |
296 | |
273 | dnsv4_tos = -1; |
297 | dnsv4_tos = -1; |
274 | dnsv4_fd = -1; |
298 | dnsv4_fd = -1; |
275 | |
299 | |
… | |
… | |
306 | THISNODE->dns_hostname ? THISNODE->dns_port : 0, |
330 | THISNODE->dns_hostname ? THISNODE->dns_port : 0, |
307 | PROT_DNSv4); |
331 | PROT_DNSv4); |
308 | |
332 | |
309 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
333 | if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) |
310 | { |
334 | { |
311 | slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); |
335 | slog (L_ERR, _("can't bind dnsv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); |
312 | exit (EXIT_FAILURE); |
336 | exit (EXIT_FAILURE); |
313 | } |
337 | } |
314 | |
338 | |
315 | dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); |
339 | dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); |
|
|
340 | ++success; |
316 | } |
341 | } |
317 | #endif |
342 | #endif |
318 | |
343 | |
319 | ///////////////////////////////////////////////////////////////////////////// |
344 | ///////////////////////////////////////////////////////////////////////////// |
|
|
345 | |
|
|
346 | if (!success) |
|
|
347 | { |
|
|
348 | slog (L_ERR, _("no protocols enabled, exiting.")); |
|
|
349 | exit (EXIT_FAILURE); |
|
|
350 | } |
320 | |
351 | |
321 | reconnect_all (); |
352 | reconnect_all (); |
322 | |
353 | |
323 | ///////////////////////////////////////////////////////////////////////////// |
354 | ///////////////////////////////////////////////////////////////////////////// |
324 | |
355 | |
325 | tap = new tap_device (); |
356 | tap = new tap_device (); |
326 | if (!tap) //D this, of course, never catches |
357 | if (!tap) //D this, of course, never catches |
327 | { |
358 | { |
328 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
359 | slog (L_ERR, _("cannot create network interface '%s', exiting."), conf.ifname); |
329 | exit (EXIT_FAILURE); |
360 | exit (EXIT_FAILURE); |
330 | } |
361 | } |
331 | |
362 | |
332 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
363 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
333 | |
364 | |
|
|
365 | run_script_cb cb; |
|
|
366 | cb.set<vpn, &vpn::script_if_init> (this); |
|
|
367 | |
334 | if (tap->if_up () && |
368 | if (tap->if_up () && |
335 | !run_script (run_script_cb (this, &vpn::script_if_init), true)) |
369 | !run_script (cb, true)) |
336 | { |
370 | { |
337 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
371 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
338 | tap->if_up ()); |
372 | tap->if_up ()); |
339 | exit (EXIT_FAILURE); |
373 | exit (EXIT_FAILURE); |
340 | } |
374 | } |
341 | |
375 | |
342 | if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) |
376 | cb.set<vpn, &vpn::script_if_up> (this); |
|
|
377 | if (!run_script (cb, true)) |
343 | { |
378 | { |
344 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
379 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
345 | exit (EXIT_FAILURE); |
380 | exit (EXIT_FAILURE); |
346 | } |
381 | } |
347 | |
382 | |
… | |
… | |
423 | { |
458 | { |
424 | // broadcast, this is ugly, but due to the security policy |
459 | // broadcast, this is ugly, but due to the security policy |
425 | // we have to connect to all hosts... |
460 | // we have to connect to all hosts... |
426 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
461 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
427 | if ((*c)->conf != THISNODE) |
462 | if ((*c)->conf != THISNODE) |
428 | (*c)->inject_data_packet (pkt, true); |
463 | (*c)->inject_data_packet (pkt); |
429 | } |
464 | } |
430 | } |
465 | } |
431 | |
466 | |
432 | void |
467 | void |
433 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
468 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
434 | { |
469 | { |
435 | unsigned int src = pkt->src (); |
470 | unsigned int src = pkt->src (); |
436 | unsigned int dst = pkt->dst (); |
471 | unsigned int dst = pkt->dst (); |
437 | |
472 | |
438 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
473 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d."), |
439 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
474 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
440 | |
475 | |
441 | if (src == 0 || src > conns.size () |
476 | if (src == 0 || src > conns.size () |
442 | || dst > conns.size () |
477 | || dst > conns.size () |
443 | || pkt->typ () >= vpn_packet::PT_MAX) |
478 | || pkt->typ () >= vpn_packet::PT_MAX) |
444 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
479 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
445 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
480 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
446 | else if (dst > conns.size ()) |
481 | else if (dst > conns.size ()) |
447 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
482 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)."), |
448 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
483 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
449 | else |
484 | else |
450 | { |
485 | { |
451 | connection *c = conns[src - 1]; |
486 | connection *c = conns[src - 1]; |
452 | |
487 | |
453 | if (dst == 0) |
488 | if (dst == 0) |
454 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
489 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)."), |
455 | c->conf->nodename, (const char *)rsi); |
490 | c->conf->nodename, (const char *)rsi); |
456 | else if (dst != THISNODE->id) |
491 | else if (dst != THISNODE->id) |
457 | { |
492 | { |
458 | if (THISNODE->routerprio) |
493 | if (THISNODE->routerprio) |
459 | // the tos setting gets lost here. who cares. |
494 | // the tos setting gets lost here. who cares. |
460 | conns[dst - 1]->inject_vpn_packet (pkt); |
495 | conns[dst - 1]->inject_vpn_packet (pkt); |
461 | else |
496 | else |
462 | slog (L_WARN, |
497 | slog (L_WARN, |
463 | _("%s(%s): forwarding request (=> %s), but we are no router"), |
498 | _("%s(%s): request to forward packet to %s, but we are no router (config mismatch?)."), |
464 | c->conf->nodename, (const char *)rsi, |
499 | c->conf->nodename, (const char *)rsi, |
465 | conns[dst - 1]->conf->nodename); |
500 | conns[dst - 1]->conf->nodename); |
466 | } |
501 | } |
467 | else |
502 | else |
468 | c->recv_vpn_packet (pkt, rsi); |
503 | c->recv_vpn_packet (pkt, rsi); |
… | |
… | |
491 | #if ENABLE_DNS |
526 | #if ENABLE_DNS |
492 | case PROT_DNSv4: |
527 | case PROT_DNSv4: |
493 | return send_dnsv4_packet (pkt, si, tos); |
528 | return send_dnsv4_packet (pkt, si, tos); |
494 | #endif |
529 | #endif |
495 | default: |
530 | default: |
496 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
531 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); |
497 | } |
532 | } |
498 | |
533 | |
499 | return false; |
534 | return false; |
500 | } |
535 | } |
501 | |
536 | |
502 | void |
537 | inline void |
503 | vpn::ipv4_ev (ev::io &w, int revents) |
538 | vpn::ipv4_ev (ev::io &w, int revents) |
504 | { |
539 | { |
505 | if (revents & EV_READ) |
540 | if (revents & EV_READ) |
506 | { |
541 | { |
507 | vpn_packet *pkt = new vpn_packet; |
542 | vpn_packet *pkt = new vpn_packet; |
… | |
… | |
523 | recv_vpn_packet (pkt, si); |
558 | recv_vpn_packet (pkt, si); |
524 | } |
559 | } |
525 | else |
560 | else |
526 | { |
561 | { |
527 | // probably ECONNRESET or somesuch |
562 | // probably ECONNRESET or somesuch |
528 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
563 | slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno)); |
529 | } |
564 | } |
530 | |
565 | |
531 | delete pkt; |
566 | delete pkt; |
532 | } |
567 | } |
533 | else |
568 | else |
534 | { |
569 | { |
535 | slog (L_ERR, |
570 | slog (L_ERR, |
536 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
571 | _("FATAL: unknown revents %08x in socket, exiting.\n"), |
537 | revents); |
572 | revents); |
538 | exit (EXIT_FAILURE); |
573 | exit (EXIT_FAILURE); |
539 | } |
574 | } |
540 | } |
575 | } |
541 | |
576 | |
542 | #if ENABLE_ICMP |
577 | #if ENABLE_ICMP |
543 | void |
578 | inline void |
544 | vpn::icmpv4_ev (ev::io &w, int revents) |
579 | vpn::icmpv4_ev (ev::io &w, int revents) |
545 | { |
580 | { |
546 | if (revents & EV_READ) |
581 | if (revents & EV_READ) |
547 | { |
582 | { |
548 | vpn_packet *pkt = new vpn_packet; |
583 | vpn_packet *pkt = new vpn_packet; |
… | |
… | |
571 | } |
606 | } |
572 | } |
607 | } |
573 | else |
608 | else |
574 | { |
609 | { |
575 | // probably ECONNRESET or somesuch |
610 | // probably ECONNRESET or somesuch |
576 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
611 | slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno)); |
577 | } |
612 | } |
578 | |
613 | |
579 | delete pkt; |
614 | delete pkt; |
580 | } |
615 | } |
581 | else |
616 | else |
582 | { |
617 | { |
583 | slog (L_ERR, |
618 | slog (L_ERR, |
584 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
619 | _("FATAL: unknown revents %08x in socket, exiting.\n"), |
585 | revents); |
620 | revents); |
586 | exit (EXIT_FAILURE); |
621 | exit (EXIT_FAILURE); |
587 | } |
622 | } |
588 | } |
623 | } |
589 | #endif |
624 | #endif |
590 | |
625 | |
591 | void |
626 | inline void |
592 | vpn::udpv4_ev (ev::io &w, int revents) |
627 | vpn::udpv4_ev (ev::io &w, int revents) |
593 | { |
628 | { |
594 | if (revents & EV_READ) |
629 | if (revents & EV_READ) |
595 | { |
630 | { |
596 | vpn_packet *pkt = new vpn_packet; |
631 | vpn_packet *pkt = new vpn_packet; |
… | |
… | |
609 | recv_vpn_packet (pkt, si); |
644 | recv_vpn_packet (pkt, si); |
610 | } |
645 | } |
611 | else |
646 | else |
612 | { |
647 | { |
613 | // probably ECONNRESET or somesuch |
648 | // probably ECONNRESET or somesuch |
614 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
649 | slog (L_DEBUG, _("%s: fd %d, %s."), (const char *)si, w.fd, strerror (errno)); |
615 | } |
650 | } |
616 | |
651 | |
617 | delete pkt; |
652 | delete pkt; |
618 | } |
653 | } |
619 | else |
654 | else |
620 | { |
655 | { |
621 | slog (L_ERR, |
656 | slog (L_ERR, |
622 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
657 | _("FATAL: unknown revents %08x in socket, exiting.\n"), |
623 | revents); |
658 | revents); |
624 | exit (EXIT_FAILURE); |
659 | exit (EXIT_FAILURE); |
625 | } |
660 | } |
626 | } |
661 | } |
627 | |
662 | |
628 | void |
663 | inline void |
629 | vpn::tap_ev (ev::io &w, int revents) |
664 | vpn::tap_ev (ev::io &w, int revents) |
630 | { |
665 | { |
631 | if (revents & EV_READ) |
666 | if (revents & EV_READ) |
632 | { |
667 | { |
633 | /* process data */ |
668 | /* process data */ |
… | |
… | |
665 | } |
700 | } |
666 | else |
701 | else |
667 | abort (); |
702 | abort (); |
668 | } |
703 | } |
669 | |
704 | |
670 | void |
705 | inline void |
671 | vpn::event_cb (ev::timer &w, int) |
706 | vpn::event_cb (ev::timer &w, int) |
672 | { |
707 | { |
673 | if (events) |
708 | if (events) |
674 | { |
709 | { |
675 | if (events & EVENT_SHUTDOWN) |
710 | if (events & EVENT_SHUTDOWN) |
676 | { |
711 | { |
677 | slog (L_INFO, _("preparing shutdown...")); |
712 | slog (L_INFO, _("preparing shutdown...")); |
678 | |
713 | |
679 | shutdown_all (); |
714 | shutdown_all (); |
680 | remove_pid (conf.pidfilename); |
715 | remove_pid (conf.pidfilename); |
681 | slog (L_INFO, _("terminating")); |
716 | slog (L_INFO, _("exiting.")); |
682 | exit (EXIT_SUCCESS); |
717 | exit (EXIT_SUCCESS); |
683 | } |
718 | } |
684 | |
719 | |
685 | if (events & EVENT_RECONNECT) |
720 | if (events & EVENT_RECONNECT) |
686 | { |
721 | { |
687 | slog (L_INFO, _("forced reconnect")); |
722 | slog (L_INFO, _("forced reconnect.")); |
688 | |
723 | |
689 | reconnect_all (); |
724 | reconnect_all (); |
690 | } |
725 | } |
691 | |
726 | |
692 | events = 0; |
727 | events = 0; |
… | |
… | |
708 | |
743 | |
709 | conns.clear (); |
744 | conns.clear (); |
710 | |
745 | |
711 | connection_init (); |
746 | connection_init (); |
712 | |
747 | |
713 | for (configuration::node_vector::iterator i = conf.nodes.begin (); |
748 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
714 | i != conf.nodes.end (); ++i) |
749 | conns.push_back (new connection (this, *i)); |
715 | { |
750 | |
716 | connection *conn = new connection (this, *i); |
751 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
717 | conns.push_back (conn); |
|
|
718 | conn->establish_connection (); |
752 | (*c)->establish_connection (); |
719 | } |
|
|
720 | } |
753 | } |
721 | |
754 | |
722 | connection *vpn::find_router () |
755 | bool vpn::can_direct (conf_node *src, conf_node *dst) const |
723 | { |
756 | { |
724 | u32 prio = 1; |
757 | return src != dst |
|
|
758 | && src->may_direct (dst) |
|
|
759 | && dst->may_direct (src) |
|
|
760 | && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS) |
|
|
761 | || (src->protocols & dst->connectable_protocols ())); |
|
|
762 | } |
|
|
763 | |
|
|
764 | // only works for indirect and routed connections: find a router |
|
|
765 | // from THISNODE to dst |
|
|
766 | connection *vpn::find_router_for (const connection *dst) |
|
|
767 | { |
725 | connection *router = 0; |
768 | connection *router = 0; |
726 | |
769 | |
|
|
770 | // first try to find a router with a direct connection, route there |
|
|
771 | // regardless of any other considerations. |
|
|
772 | { |
|
|
773 | u32 prio = 1; |
|
|
774 | |
|
|
775 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
|
|
776 | { |
|
|
777 | connection *c = *i; |
|
|
778 | |
|
|
779 | if (c->conf->routerprio > prio |
|
|
780 | && c->conf != THISNODE |
|
|
781 | && can_direct (c->conf, dst->conf) |
|
|
782 | && c->ictx && c->octx) |
|
|
783 | { |
|
|
784 | prio = c->conf->routerprio; |
|
|
785 | router = c; |
|
|
786 | } |
|
|
787 | } |
|
|
788 | } |
|
|
789 | |
|
|
790 | if (router) |
|
|
791 | return router; |
|
|
792 | |
|
|
793 | // second try find the router with the highest priority, higher than ours |
|
|
794 | { |
|
|
795 | u32 prio = THISNODE->routerprio ? THISNODE->routerprio : 1; |
|
|
796 | |
|
|
797 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
|
|
798 | { |
|
|
799 | connection *c = *i; |
|
|
800 | |
|
|
801 | if (c->conf->routerprio > prio |
|
|
802 | && c != dst |
|
|
803 | && c->conf != THISNODE |
|
|
804 | && c->ictx && c->octx) |
|
|
805 | { |
|
|
806 | prio = c->conf->routerprio; |
|
|
807 | router = c; |
|
|
808 | } |
|
|
809 | } |
|
|
810 | } |
|
|
811 | |
|
|
812 | return router; |
|
|
813 | } |
|
|
814 | |
|
|
815 | void vpn::connection_established (connection *c) |
|
|
816 | { |
727 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
817 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
728 | { |
818 | { |
729 | connection *c = *i; |
819 | connection *o = *i; |
730 | |
820 | |
731 | if (c->conf->routerprio > prio |
821 | if (!o->is_direct |
732 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
822 | && o->si.valid () |
733 | && c->ictx && c->octx |
823 | && c->si != o->si |
734 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
824 | && c == find_router_for (o)) |
735 | { |
|
|
736 | prio = c->conf->routerprio; |
|
|
737 | router = c; |
|
|
738 | } |
825 | { |
|
|
826 | slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."), |
|
|
827 | o->conf->nodename, c->conf->nodename); |
|
|
828 | o->rekey (); |
|
|
829 | } |
739 | } |
830 | } |
740 | |
|
|
741 | return router; |
|
|
742 | } |
831 | } |
743 | |
832 | |
744 | void vpn::send_connect_request (int id) |
833 | void vpn::send_connect_request (connection *c) |
745 | { |
834 | { |
746 | connection *c = find_router (); |
835 | connection *r = find_router_for (c); |
747 | |
836 | |
748 | if (c) |
837 | if (r) |
|
|
838 | { |
|
|
839 | slog (L_TRACE, _("%s: no way to connect, sending mediated connection request via %s."), |
|
|
840 | c->conf->nodename, r->conf->nodename); |
749 | c->send_connect_request (id); |
841 | r->send_connect_request (c->conf->id); |
|
|
842 | } |
750 | else |
843 | else |
751 | // no router found, aggressively connect to all routers |
844 | slog (L_DEBUG, _("%s: no way to connect and no router found: unable to connect."), |
752 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
845 | c->conf->nodename); |
753 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
|
|
754 | (*i)->establish_connection (); |
|
|
755 | } |
846 | } |
756 | |
847 | |
757 | void |
848 | void |
758 | connection::dump_status () |
849 | connection::dump_status () |
759 | { |
850 | { |
760 | slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); |
851 | slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); |
761 | slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), |
852 | slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), |
762 | connectmode, conf->connectmode, (const char *)si, (int)prot_minor); |
853 | connectmode, conf->connectmode, (const char *)si, (int)prot_minor); |
763 | slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), |
854 | slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), |
764 | (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); |
855 | (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); |
765 | slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"), |
|
|
766 | (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at)); |
|
|
767 | } |
856 | } |
768 | |
857 | |
769 | void |
858 | void |
770 | vpn::dump_status () |
859 | vpn::dump_status () |
771 | { |
860 | { |
… | |
… | |
776 | |
865 | |
777 | slog (L_NOTICE, _("END status dump")); |
866 | slog (L_NOTICE, _("END status dump")); |
778 | } |
867 | } |
779 | |
868 | |
780 | vpn::vpn (void) |
869 | vpn::vpn (void) |
|
|
870 | { |
781 | : event (this, &vpn::event_cb) |
871 | event .set<vpn, &vpn::event_cb > (this); |
782 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
872 | udpv4_ev_watcher .set<vpn, &vpn::udpv4_ev > (this); |
783 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
873 | ipv4_ev_watcher .set<vpn, &vpn::ipv4_ev > (this); |
784 | #if ENABLE_TCP |
874 | #if ENABLE_TCP |
785 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
875 | tcpv4_ev_watcher .set<vpn, &vpn::tcpv4_ev > (this); |
786 | #endif |
876 | #endif |
787 | #if ENABLE_ICMP |
877 | #if ENABLE_ICMP |
788 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
878 | icmpv4_ev_watcher.set<vpn, &vpn::icmpv4_ev> (this); |
789 | #endif |
879 | #endif |
790 | #if ENABLE_DNS |
880 | #if ENABLE_DNS |
791 | , dnsv4_ev_watcher (this, &vpn::dnsv4_ev) |
881 | dnsv4_ev_watcher .set<vpn, &vpn::dnsv4_ev > (this); |
792 | #endif |
882 | #endif |
793 | , tap_ev_watcher (this, &vpn::tap_ev) |
883 | tap_ev_watcher .set<vpn, &vpn::tap_ev > (this); |
794 | { |
|
|
795 | } |
884 | } |
796 | |
885 | |
797 | vpn::~vpn () |
886 | vpn::~vpn () |
798 | { |
887 | { |
799 | } |
888 | } |