--- gvpe/src/vpn.C 2003/04/02 21:02:25 1.3 +++ gvpe/src/vpn.C 2005/04/08 16:12:49 1.35 @@ -1,7 +1,10 @@ /* vpn.C -- handle the protocol, encryption, handshaking etc. + Copyright (C) 2003-2005 Marc Lehmann - This program is free software; you can redistribute it and/or modify + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -12,7 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -20,19 +23,20 @@ #include -#include -#include #include +#include +#include #include #include -#include #include -#include -#include #include #include #include +#include +#include + +#include "netcompat.h" #include "pidfile.h" @@ -40,16 +44,12 @@ #include "util.h" #include "vpn.h" -#if ENABLE_TCP -# include -# include -# include -# include -#endif +vpn network; // THE vpn (bad design...) ///////////////////////////////////////////////////////////////////////////// -const char *vpn::script_if_up () +void +vpn::script_init_env () { // the tunnel device mtu should be the physical mtu - overhead // the tricky part is rounding to the cipher key blocksize @@ -59,51 +59,97 @@ mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again char *env; - asprintf (&env, "CONFBASE=%s", confbase); - putenv (env); - asprintf (&env, "NODENAME=%s", THISNODE->nodename); - putenv (env); - asprintf (&env, "NODEID=%d", THISNODE->id); - putenv (env); - asprintf (&env, "IFNAME=%s", tap->interface ()); - putenv (env); - asprintf (&env, "MTU=%d", mtu); - putenv (env); - asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", - 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, - THISNODE->id & 0xff); - putenv (env); + asprintf (&env, "CONFBASE=%s", confbase); putenv (env); + asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); + asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); + asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); + asprintf (&env, "MTU=%d", mtu); putenv (env); + asprintf (&env, "NODES=%d", conns.size ()); putenv (env); + asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); + + conns [THISNODE->id - 1]->script_init_env (""); + + for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) + { + char ext[16]; + snprintf (ext, 16, "_%d", (*c)->conf->id); + (*c)->script_init_env (ext); + } +} + +const char *vpn::script_if_init () +{ + script_init_env (); - return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; + return tap->if_up (); +} + +const char *vpn::script_if_up () +{ + script_init_env (); + + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_if_up ? ::conf.script_if_up : "if-up"); + + return filename; } int vpn::setup () { + ipv4_fd = -1; + + if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) + { + ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); + + if (ipv4_fd < 0) + return -1; + + fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); + +#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) + // this I really consider a linux bug. I am neither connected + // nor do I fragment myself. Linux still sets DF and doesn't + // fragment for me sometimes. + { + int oval = IP_PMTUDISC_DONT; + setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + } +#endif + + sockinfo si (THISNODE, PROT_IPv4); + + if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); + exit (EXIT_FAILURE); + } + + ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); + } + udpv4_fd = -1; - if (THISNODE->protocols & PROT_UDPv4) + if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) { udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpv4_fd < 0) return -1; + fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); + // standard daemon practise... { int oval = 1; setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); } - sockinfo si (THISNODE, PROT_UDPv4); - - if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) - { - slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); - } - -#ifdef IP_MTU_DISCOVER +#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) // this I really consider a linux bug. I am neither connected // nor do I fragment myself. Linux still sets DF and doesn't // fragment for me sometimes. @@ -113,46 +159,74 @@ } #endif - udpv4_ev_watcher.start (udpv4_fd, POLLIN); + sockinfo si (THISNODE, PROT_UDPv4); + + if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); + exit (EXIT_FAILURE); + } + + udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); } - ipv4_fd = -1; - if (THISNODE->protocols & PROT_IPv4) + icmpv4_fd = -1; + +#if ENABLE_ICMP + if (THISNODE->protocols & PROT_ICMPv4) { - ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); + icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); - if (ipv4_fd < 0) + if (icmpv4_fd < 0) return -1; - sockinfo si (THISNODE, PROT_IPv4); + fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); - if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) - { - slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); - exit (1); - } +#ifdef ICMP_FILTER + { + icmp_filter oval; + oval.data = 0xffffffff; + if (::conf.icmp_type < 32) + oval.data &= ~(1 << ::conf.icmp_type); + + setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); + } +#endif -#ifdef IP_MTU_DISCOVER +#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) // this I really consider a linux bug. I am neither connected // nor do I fragment myself. Linux still sets DF and doesn't // fragment for me sometimes. { int oval = IP_PMTUDISC_DONT; - setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); } #endif - ipv4_ev_watcher.start (ipv4_fd, POLLIN); + sockinfo si (THISNODE, PROT_ICMPv4); + + if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); + exit (EXIT_FAILURE); + } + + icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); } +#endif + + tcpv4_fd = -1; #if ENABLE_TCP - if (THISNODE->protocols & PROT_TCPv4) + if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) { tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); if (tcpv4_fd < 0) return -1; + fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); + // standard daemon practise... { int oval = 1; @@ -164,47 +238,175 @@ if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) { slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); + exit (EXIT_FAILURE); } if (listen (tcpv4_fd, 5)) { slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); + exit (EXIT_FAILURE); + } + + tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); + } +#endif + +#if ENABLE_DNS + if (THISNODE->protocols & PROT_DNSv4) + { + dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); + + dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (dnsv4_fd < 0) + return -1; + +# if defined(SOL_IP) && defined(IP_MTU_DISCOVER) + // this I really consider a linux bug. I am neither connected + // nor do I fragment myself. Linux still sets DF and doesn't + // fragment for me sometimes. + { + int oval = IP_PMTUDISC_DONT; + setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + } +# endif + + // standard daemon practise... + { + int oval = 1; + setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); + } + + sockinfo si (THISNODE->dns_hostname, + THISNODE->dns_hostname ? THISNODE->dns_port : 0, + PROT_DNSv4); + + if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind dnsv4 on %s: %s"), (const char *)si, strerror (errno)); + exit (EXIT_FAILURE); } - tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); + dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); } #endif + ///////////////////////////////////////////////////////////////////////////// + + reconnect_all (); + + ///////////////////////////////////////////////////////////////////////////// + tap = new tap_device (); if (!tap) //D this, of course, never catches { slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); - exit (1); + exit (EXIT_FAILURE); } - run_script (run_script_cb (this, &vpn::script_if_up), true); + if (tap->if_up () && + !run_script (run_script_cb (this, &vpn::script_if_init), true)) + { + slog (L_ERR, _("interface initialization command '%s' failed, exiting."), + tap->if_up ()); + exit (EXIT_FAILURE); + } - tap_ev_watcher.start (tap->fd, POLLIN); + if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) + { + slog (L_ERR, _("if-up command execution failed, exiting.")); + exit (EXIT_FAILURE); + } - reconnect_all (); + tap_ev_watcher.start (tap->fd, EVENT_READ); return 0; } -void +bool vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { +#if defined(SOL_IP) && defined(IP_TOS) setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; } -void +static u16 +ipv4_checksum (u16 *data, unsigned int len) +{ + // use 32 bit accumulator and fold back carry bits at the end + u32 sum = 0; + + while (len > 1) + { + sum += *data++; + len -= 2; + } + + // odd byte left? + if (len) + sum += *(u8 *)data; + + // add back carry bits + sum = (sum >> 16) + (sum & 0xffff); // lo += hi + sum += (sum >> 16); // carry + + return ~sum; +} + +#if ENABLE_ICMP +bool +vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ +#if defined(SOL_IP) && defined(IP_TOS) + setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif + + pkt->unshift_hdr (4); + + icmp_header *hdr = (icmp_header *)&((*pkt)[0]); + hdr->type = ::conf.icmp_type; + hdr->code = 255; + hdr->checksum = 0; + hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); + + sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; +} +#endif + +bool vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { +#if defined(SOL_IP) && defined(IP_TOS) setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; +} + +void +vpn::inject_data_packet (tap_packet *pkt, int dst) +{ + if (dst) + { + // unicast + if (dst != THISNODE->id) + conns[dst - 1]->inject_data_packet (pkt); + } + else + { + // broadcast, this is ugly, but due to the security policy + // we have to connect to all hosts... + for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) + if ((*c)->conf != THISNODE) + (*c)->inject_data_packet (pkt, true); + } } void @@ -221,29 +423,65 @@ || pkt->typ () >= vpn_packet::PT_MAX) slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); + else if (dst > conns.size ()) + slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), + (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); else { connection *c = conns[src - 1]; - if (dst == 0 && !THISNODE->routerprio) - slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), + if (dst == 0) + slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), c->conf->nodename, (const char *)rsi); - else if (dst != 0 && dst != THISNODE->id) - // FORWARDING NEEDED ;) - slog (L_WARN, - _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), - dst, conns[dst - 1]->conf->nodename, - (const char *)rsi, - THISNODE->id, THISNODE->nodename); + else if (dst != THISNODE->id) + { + if (THISNODE->routerprio) + // the tos setting gets lost here. who cares. + conns[dst - 1]->inject_vpn_packet (pkt); + else + slog (L_WARN, + _("%s(%s): forwarding request (=> %s), but we are no router"), + c->conf->nodename, (const char *)rsi, + conns[dst - 1]->conf->nodename); + } else c->recv_vpn_packet (pkt, rsi); } } +bool +vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ + switch (si.prot) + { + case PROT_IPv4: + return send_ipv4_packet (pkt, si, tos); + case PROT_UDPv4: + return send_udpv4_packet (pkt, si, tos); +#if ENABLE_TCP + case PROT_TCPv4: + return send_tcpv4_packet (pkt, si, tos); +#endif +#if ENABLE_ICMP + case PROT_ICMPv4: + return send_icmpv4_packet (pkt, si, tos); +#endif +#if ENABLE_DNS + case PROT_DNSv4: + return send_dnsv4_packet (pkt, si, tos); +#endif + + default: + slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); + } + + return false; +} + void -vpn::udpv4_ev (io_watcher &w, short revents) +vpn::ipv4_ev (io_watcher &w, short revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EVENT_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -252,12 +490,15 @@ len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - sockinfo si(sa); + sockinfo si(sa, PROT_IPv4); if (len > 0) { pkt->len = len; + // raw sockets deliver the ipv4 header, but don't expect it on sends + pkt->skip_hdr (IP_OVERHEAD); + recv_vpn_packet (pkt, si); } else @@ -268,25 +509,20 @@ delete pkt; } - else if (revents & POLLHUP) - { - // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); - exit (1); - } else { slog (L_ERR, - _("FATAL: unknown revents %08x in socket, terminating\n"), - revents); - exit (1); + _("FATAL: unknown revents %08x in socket, terminating\n"), + revents); + exit (EXIT_FAILURE); } } +#if ENABLE_ICMP void -vpn::ipv4_ev (io_watcher &w, short revents) +vpn::icmpv4_ev (io_watcher &w, short revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EVENT_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -295,17 +531,23 @@ len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - sockinfo si(sa, PROT_IPv4); + sockinfo si(sa, PROT_ICMPv4); if (len > 0) { pkt->len = len; - // raw sockets deliver the ipv4, but don't expect it on sends - // this is slow, but... - pkt->skip_hdr (IP_OVERHEAD); + icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); - recv_vpn_packet (pkt, si); + if (hdr->type == ::conf.icmp_type + && hdr->code == 255) + { + // raw sockets deliver the ipv4, but don't expect it on sends + // this is slow, but... + pkt->skip_hdr (ICMP_OVERHEAD); + + recv_vpn_packet (pkt, si); + } } else { @@ -315,247 +557,91 @@ delete pkt; } - else if (revents & POLLHUP) - { - // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); - exit (1); - } else { slog (L_ERR, _("FATAL: unknown revents %08x in socket, terminating\n"), revents); - exit (1); + exit (EXIT_FAILURE); } } - -#if ENABLE_TCP - -struct tcp_connection; - -struct lt_sockinfo -{ - bool operator()(const sockinfo *a, const sockinfo *b) const - { - return *a < *b; - } -}; - -struct tcp_si_map : public map { - void cleaner_cb (time_watcher &w); time_watcher cleaner; - - tcp_si_map () - : cleaner(this, &tcp_si_map::cleaner_cb) - { - cleaner.start (0); - } -} tcp_si; - -struct tcp_connection : io_watcher { - tstamp last_activity; - const sockinfo si; - vpn &v; - bool ok; - - void tcpv4_ev (io_watcher &w, short revents); - - operator tcp_si_map::value_type() - { - return tcp_si_map::value_type (&si, this); - } - - tcp_connection (int fd_, const sockinfo &si_, vpn &v_) - : v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev), ok(false) - { - last_activity = NOW; - start (fd_, POLLOUT); - } - - ~tcp_connection () { close (fd); } -}; - -void tcp_si_map::cleaner_cb (time_watcher &w) -{ - w.at = NOW + 600; - tstamp to = NOW - ::conf.keepalive - 30; - - for (iterator i = begin (); i != end(); ) - if (i->second->last_activity >= to) - ++i; - else - { - erase (i); - i = begin (); - } -} +#endif void -vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) +vpn::udpv4_ev (io_watcher &w, short revents) { - tcp_si_map::iterator info = tcp_si.find (&si); - - if (info == tcp_si.end ()) + if (revents & EVENT_READ) { - // woaw, the first lost packet ;) - int fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); - - if (fd >= 0) - { - fcntl (fd, F_SETFL, O_NONBLOCK); - - if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0 - || errno == EINPROGRESS) - { - tcp_connection *i = new tcp_connection (fd, si, *this); + vpn_packet *pkt = new vpn_packet; + struct sockaddr_in sa; + socklen_t sa_len = sizeof (sa); + int len; - tcp_si.insert (*i); - } - else - close (fd); - } - } - else - { - tcp_connection *i = info->second; + len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); - i->last_activity = NOW; + sockinfo si(sa, PROT_UDPv4); - if (i->ok) + if (len > 0) { - setsockopt (i->fd, SOL_IP, IP_TOS, &tos, sizeof tos); + pkt->len = len; - // we use none of the advantages of tcp - write (i->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32); + recv_vpn_packet (pkt, si); } - } - -#if 0 - setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); - sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); -#endif -} - -void -tcp_connection::tcpv4_ev (io_watcher &w, short revents) -{ - last_activity = NOW; - - if (!ok) // just established? - { - ok = true; - fcntl (fd, F_SETFL, 0); - stop (); - start (fd, POLLIN); - } - - if (revents & (POLLIN | POLLERR)) - { - u32 len; - - if (sizeof (len) == read (fd, &len, sizeof (len))) + else { - vpn_packet *pkt = new vpn_packet; - - if (len == read (fd, &((*pkt)[0]), len)) - { - pkt->len = len; - - v.recv_vpn_packet (pkt, si); - return; - } - - delete pkt; + // probably ECONNRESET or somesuch + slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); } - tcp_si.erase (&si); - stop (); + delete pkt; } -} - -void -vpn::tcpv4_ev (io_watcher &w, short revents) -{ - if (revents & (POLLIN | POLLERR)) + else { - struct sockaddr_in sa; - socklen_t sa_len = sizeof (sa); - int len; - - int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); - - if (fd >= 0) - { - fcntl (fd, F_SETFL, O_NONBLOCK); - - sockinfo si(sa, PROT_TCPv4); - tcp_connection *i = new tcp_connection (fd, si, *this); - - slog (L_ERR, "accepted %d\n", fd);//D - - tcp_si.insert (*i); - } + slog (L_ERR, + _("FATAL: unknown revents %08x in socket, terminating\n"), + revents); + exit (EXIT_FAILURE); } } -#endif - void vpn::tap_ev (io_watcher &w, short revents) { - if (revents & POLLIN) + if (revents & EVENT_READ) { /* process data */ tap_packet *pkt; pkt = tap->recv (); - int dst = mac2id (pkt->dst); - int src = mac2id (pkt->src); + if (!pkt) + return; - if (src != THISNODE->id) + if (pkt->len > 14) { - slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); - exit (1); - } - - if (dst == THISNODE->id) - { - slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); - exit (1); - } + int dst = mac2id (pkt->dst); + int src = mac2id (pkt->src); - if (dst > conns.size ()) - slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); - else - { - if (dst) + if (src != THISNODE->id) { - // unicast - if (dst != THISNODE->id) - conns[dst - 1]->inject_data_packet (pkt); + slog (L_ERR, _("FATAL: tap packet not originating on current node received (if-up script not working properly?), exiting.")); + exit (EXIT_FAILURE); } - else - { - // broadcast, first check router, then self, then english - connection *router = find_router (); - if (router) - router->inject_data_packet (pkt, true); - else - for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) - if ((*c)->conf != THISNODE) - (*c)->inject_data_packet (pkt); + if (dst == THISNODE->id) + { + slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); + exit (EXIT_FAILURE); } + + if (dst > conns.size ()) + slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); + else + inject_data_packet (pkt, dst); } delete pkt; } - else if (revents & (POLLHUP | POLLERR)) - { - slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); - exit (1); - } else abort (); } @@ -570,12 +656,9 @@ slog (L_INFO, _("preparing shutdown...")); shutdown_all (); - - remove_pid (pidfilename); - + remove_pid (conf.pidfilename); slog (L_INFO, _("terminating")); - - exit (0); + exit (EXIT_SUCCESS); } if (events & EVENT_RECONNECT) @@ -587,8 +670,6 @@ events = 0; } - - w.at = TSTAMP_CANCEL; } void @@ -611,18 +692,15 @@ for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) { - connection *conn = new connection (this); - - conn->conf = *i; + connection *conn = new connection (this, *i); conns.push_back (conn); - conn->establish_connection (); } } connection *vpn::find_router () { - u32 prio = 0; + u32 prio = 1; connection *router = 0; for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) @@ -630,9 +708,9 @@ connection *c = *i; if (c->conf->routerprio > prio - && c->connectmode == conf_node::C_ALWAYS - && c->conf != THISNODE - && c->ictx && c->octx) + && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use + && c->ictx && c->octx + && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode { prio = c->conf->routerprio; router = c; @@ -642,17 +720,17 @@ return router; } -void vpn::connect_request (int id) +void vpn::send_connect_request (int id) { connection *c = find_router (); if (c) - c->connect_request (id); - //else // does not work, because all others must connect to the same router - // // no router found, aggressively connect to all routers - // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) - // if ((*i)->conf->routerprio) - // (*i)->establish_connection (); + c->send_connect_request (id); + else + // no router found, aggressively connect to all routers + for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) + if ((*i)->conf->routerprio && (*i)->conf != THISNODE) + (*i)->establish_connection (); } void @@ -679,13 +757,19 @@ } vpn::vpn (void) -: event(this, &vpn::event_cb) -, udpv4_ev_watcher(this, &vpn::udpv4_ev) -, ipv4_ev_watcher (this, &vpn::ipv4_ev) -, tap_ev_watcher (this, &vpn::tap_ev) +: event (this, &vpn::event_cb) +, udpv4_ev_watcher (this, &vpn::udpv4_ev) +, ipv4_ev_watcher (this, &vpn::ipv4_ev) #if ENABLE_TCP -, tcpv4_ev_watcher(this, &vpn::tcpv4_ev) +, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) +#endif +#if ENABLE_ICMP +, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) +#endif +#if ENABLE_DNS +, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) #endif +, tap_ev_watcher (this, &vpn::tap_ev) { }