… | |
… | |
30 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
31 | #include <errno.h> |
31 | #include <errno.h> |
32 | #include <time.h> |
32 | #include <time.h> |
33 | #include <unistd.h> |
33 | #include <unistd.h> |
34 | #include <fcntl.h> |
34 | #include <fcntl.h> |
|
|
35 | #include <sys/socket.h> |
|
|
36 | #include <netinet/in.h> |
35 | #include <arpa/inet.h> |
37 | #include <arpa/inet.h> |
|
|
38 | #include <net/if.h> |
|
|
39 | #ifdef HAVE_NETINET_IN_SYSTM_H |
|
|
40 | # include <netinet/in_systm.h> |
|
|
41 | #endif |
|
|
42 | #ifdef HAVE_NETINET_IP_H |
36 | #include <netinet/in.h> |
43 | # include <netinet/ip.h> |
|
|
44 | #endif |
|
|
45 | #ifdef HAVE_NETINET_TCP_H |
|
|
46 | # include <netinet/tcp.h> |
|
|
47 | #endif |
|
|
48 | #if ENABLE_ICMP |
37 | #include <netinet/ip_icmp.h> |
49 | # include <netinet/ip_icmp.h> |
|
|
50 | #endif |
38 | |
51 | |
39 | #include "pidfile.h" |
52 | #include "pidfile.h" |
40 | |
53 | |
41 | #include "connection.h" |
54 | #include "connection.h" |
42 | #include "util.h" |
55 | #include "util.h" |
43 | #include "vpn.h" |
56 | #include "vpn.h" |
|
|
57 | |
|
|
58 | #if !defined(SOL_IP) && defined(IPPROTO_IP) |
|
|
59 | # define SOL_IP IPPROTO_IP |
|
|
60 | #endif |
44 | |
61 | |
45 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
46 | |
63 | |
47 | const char *vpn::script_if_up () |
64 | const char *vpn::script_if_up () |
48 | { |
65 | { |
… | |
… | |
84 | if (ipv4_fd < 0) |
101 | if (ipv4_fd < 0) |
85 | return -1; |
102 | return -1; |
86 | |
103 | |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
104 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
88 | |
105 | |
89 | #ifdef IP_MTU_DISCOVER |
106 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
90 | // this I really consider a linux bug. I am neither connected |
107 | // this I really consider a linux bug. I am neither connected |
91 | // nor do I fragment myself. Linux still sets DF and doesn't |
108 | // nor do I fragment myself. Linux still sets DF and doesn't |
92 | // fragment for me sometimes. |
109 | // fragment for me sometimes. |
93 | { |
110 | { |
94 | int oval = IP_PMTUDISC_DONT; |
111 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
122 | { |
139 | { |
123 | int oval = 1; |
140 | int oval = 1; |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
141 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
125 | } |
142 | } |
126 | |
143 | |
127 | #ifdef IP_MTU_DISCOVER |
144 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
128 | // this I really consider a linux bug. I am neither connected |
145 | // this I really consider a linux bug. I am neither connected |
129 | // nor do I fragment myself. Linux still sets DF and doesn't |
146 | // nor do I fragment myself. Linux still sets DF and doesn't |
130 | // fragment for me sometimes. |
147 | // fragment for me sometimes. |
131 | { |
148 | { |
132 | int oval = IP_PMTUDISC_DONT; |
149 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
166 | |
183 | |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
184 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
168 | } |
185 | } |
169 | #endif |
186 | #endif |
170 | |
187 | |
171 | #ifdef IP_MTU_DISCOVER |
188 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
172 | // this I really consider a linux bug. I am neither connected |
189 | // this I really consider a linux bug. I am neither connected |
173 | // nor do I fragment myself. Linux still sets DF and doesn't |
190 | // nor do I fragment myself. Linux still sets DF and doesn't |
174 | // fragment for me sometimes. |
191 | // fragment for me sometimes. |
175 | { |
192 | { |
176 | int oval = IP_PMTUDISC_DONT; |
193 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
271 | } |
288 | } |
272 | |
289 | |
273 | bool |
290 | bool |
274 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
291 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
275 | { |
292 | { |
|
|
293 | #if defined(SOL_IP) && defined(IP_TOS) |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
294 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
295 | #endif |
277 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
296 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
278 | |
297 | |
279 | return true; |
298 | return true; |
280 | } |
299 | } |
281 | |
300 | |
… | |
… | |
300 | sum += (sum >> 16); // carry |
319 | sum += (sum >> 16); // carry |
301 | |
320 | |
302 | return ~sum; |
321 | return ~sum; |
303 | } |
322 | } |
304 | |
323 | |
|
|
324 | #if ENABLE_ICMP |
|
|
325 | struct icmp_header { |
|
|
326 | u8 type; |
|
|
327 | u8 code; |
|
|
328 | u16 checksum; |
|
|
329 | union { |
|
|
330 | struct { |
|
|
331 | u16 id; |
|
|
332 | u16 sequence; |
|
|
333 | } echo; |
|
|
334 | u32 gateway; |
|
|
335 | struct { |
|
|
336 | u16 unused; |
|
|
337 | u16 mtu; |
|
|
338 | } frag; |
|
|
339 | } un; |
|
|
340 | }; |
|
|
341 | |
305 | bool |
342 | bool |
306 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
343 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
307 | { |
344 | { |
|
|
345 | #if defined(SOL_IP) && defined(IP_TOS) |
308 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
346 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
347 | #endif |
309 | |
348 | |
310 | pkt->unshift_hdr (4); |
349 | pkt->unshift_hdr (4); |
311 | |
350 | |
312 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
351 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
313 | hdr->type = ::conf.icmp_type; |
352 | hdr->type = ::conf.icmp_type; |
314 | hdr->code = 255; |
353 | hdr->code = 255; |
315 | hdr->checksum = 0; |
354 | hdr->checksum = 0; |
316 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
355 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
317 | |
356 | |
318 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
357 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
319 | |
358 | |
320 | return true; |
359 | return true; |
321 | } |
360 | } |
|
|
361 | #endif |
322 | |
362 | |
323 | bool |
363 | bool |
324 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
364 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
325 | { |
365 | { |
|
|
366 | #if defined(SOL_IP) && defined(IP_TOS) |
326 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
367 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
368 | #endif |
327 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
369 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
328 | |
370 | |
329 | return true; |
371 | return true; |
330 | } |
372 | } |
331 | |
373 | |
… | |
… | |
348 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
390 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
349 | else |
391 | else |
350 | { |
392 | { |
351 | connection *c = conns[src - 1]; |
393 | connection *c = conns[src - 1]; |
352 | |
394 | |
353 | if (dst == 0 && !THISNODE->routerprio) |
395 | if (dst == 0) |
354 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
396 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
355 | c->conf->nodename, (const char *)rsi); |
397 | c->conf->nodename, (const char *)rsi); |
356 | else if (dst != 0 && dst != THISNODE->id) |
398 | else if (dst != 0 && dst != THISNODE->id) |
357 | { |
399 | { |
358 | if (THISNODE->routerprio) |
400 | if (THISNODE->routerprio) |
359 | // the tos setting gets lost here. who cares. |
401 | // the tos setting gets lost here. who cares. |
… | |
… | |
414 | revents); |
456 | revents); |
415 | exit (1); |
457 | exit (1); |
416 | } |
458 | } |
417 | } |
459 | } |
418 | |
460 | |
|
|
461 | #if ENABLE_ICMP |
419 | void |
462 | void |
420 | vpn::icmpv4_ev (io_watcher &w, short revents) |
463 | vpn::icmpv4_ev (io_watcher &w, short revents) |
421 | { |
464 | { |
422 | if (revents & (POLLIN | POLLERR)) |
465 | if (revents & (POLLIN | POLLERR)) |
423 | { |
466 | { |
… | |
… | |
432 | |
475 | |
433 | if (len > 0) |
476 | if (len > 0) |
434 | { |
477 | { |
435 | pkt->len = len; |
478 | pkt->len = len; |
436 | |
479 | |
437 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
480 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
438 | |
481 | |
439 | if (hdr->type == ::conf.icmp_type |
482 | if (hdr->type == ::conf.icmp_type |
440 | && hdr->code == 255) |
483 | && hdr->code == 255) |
441 | { |
484 | { |
442 | // raw sockets deliver the ipv4, but don't expect it on sends |
485 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
466 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
509 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
467 | revents); |
510 | revents); |
468 | exit (1); |
511 | exit (1); |
469 | } |
512 | } |
470 | } |
513 | } |
|
|
514 | #endif |
471 | |
515 | |
472 | void |
516 | void |
473 | vpn::udpv4_ev (io_watcher &w, short revents) |
517 | vpn::udpv4_ev (io_watcher &w, short revents) |
474 | { |
518 | { |
475 | if (revents & (POLLIN | POLLERR)) |
519 | if (revents & (POLLIN | POLLERR)) |
… | |
… | |
525 | int dst = mac2id (pkt->dst); |
569 | int dst = mac2id (pkt->dst); |
526 | int src = mac2id (pkt->src); |
570 | int src = mac2id (pkt->src); |
527 | |
571 | |
528 | if (src != THISNODE->id) |
572 | if (src != THISNODE->id) |
529 | { |
573 | { |
530 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
574 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
531 | exit (1); |
575 | exit (1); |
532 | } |
576 | } |
533 | |
577 | |
534 | if (dst == THISNODE->id) |
578 | if (dst == THISNODE->id) |
535 | { |
579 | { |
536 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
580 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
537 | exit (1); |
581 | exit (1); |
538 | } |
582 | } |
539 | |
583 | |
540 | if (dst > conns.size ()) |
584 | if (dst > conns.size ()) |
541 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
585 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
… | |
… | |
547 | if (dst != THISNODE->id) |
591 | if (dst != THISNODE->id) |
548 | conns[dst - 1]->inject_data_packet (pkt); |
592 | conns[dst - 1]->inject_data_packet (pkt); |
549 | } |
593 | } |
550 | else |
594 | else |
551 | { |
595 | { |
552 | // broadcast, first check router, then self, then english |
596 | // broadcast, this is ugly, but due to the security policy |
553 | connection *router = find_router (); |
597 | // we have to connect to all hosts... |
554 | |
|
|
555 | if (router) |
|
|
556 | router->inject_data_packet (pkt, true); |
|
|
557 | else |
|
|
558 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
598 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
559 | if ((*c)->conf != THISNODE) |
599 | if ((*c)->conf != THISNODE) |
560 | (*c)->inject_data_packet (pkt); |
600 | (*c)->inject_data_packet (pkt); |
561 | } |
601 | } |
562 | } |
602 | } |
563 | |
603 | |
564 | delete pkt; |
604 | delete pkt; |
565 | } |
605 | } |
… | |
… | |
632 | } |
672 | } |
633 | } |
673 | } |
634 | |
674 | |
635 | connection *vpn::find_router () |
675 | connection *vpn::find_router () |
636 | { |
676 | { |
637 | u32 prio = 0; |
677 | u32 prio = 1; |
638 | connection *router = 0; |
678 | connection *router = 0; |
639 | |
679 | |
640 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
680 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
641 | { |
681 | { |
642 | connection *c = *i; |
682 | connection *c = *i; |
643 | |
683 | |
644 | if (c->conf->routerprio > prio |
684 | if (c->conf->routerprio > prio |
645 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
685 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
646 | && c->ictx && c->octx |
686 | && c->ictx && c->octx |
647 | && c->conf != THISNODE // redundant, since ictx==octx==0 always on thisnode |
687 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
648 | && (!THISNODE->routerprio |
|
|
649 | || c->conf->routerprio <= THISNODE->routerprio)) |
|
|
650 | { |
688 | { |
651 | prio = c->conf->routerprio; |
689 | prio = c->conf->routerprio; |
652 | router = c; |
690 | router = c; |
653 | } |
691 | } |
654 | } |
692 | } |
… | |
… | |
663 | if (c) |
701 | if (c) |
664 | c->send_connect_request (id); |
702 | c->send_connect_request (id); |
665 | else |
703 | else |
666 | // no router found, aggressively connect to all routers |
704 | // no router found, aggressively connect to all routers |
667 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
705 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
668 | if ((*i)->conf->routerprio) |
706 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
669 | (*i)->establish_connection (); |
707 | (*i)->establish_connection (); |
670 | } |
708 | } |
671 | |
709 | |
672 | void |
710 | void |
673 | connection::dump_status () |
711 | connection::dump_status () |