--- gvpe/src/vpn.C 2003/10/14 03:22:09 1.14 +++ gvpe/src/vpn.C 2007/12/01 23:35:31 1.40 @@ -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,42 +15,28 @@ 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 - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + along with gvpe; if not, write to the Free Software + Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h" #include -#include -#include #include +#include +#include #include #include -#include #include #include #include #include #include #include -#include -#include -#include -#ifdef HAVE_NETINET_IN_SYSTM_H -# include -#endif -#ifdef HAVE_NETINET_IP_H -# include -#endif -#ifdef HAVE_NETINET_TCP_H -# include -#endif -#if ENABLE_ICMP -# include -#endif + +#include "netcompat.h" #include "pidfile.h" @@ -55,13 +44,12 @@ #include "util.h" #include "vpn.h" -#if !defined(SOL_IP) && defined(IPPROTO_IP) -# define SOL_IP IPPROTO_IP -#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 @@ -71,22 +59,42 @@ 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 (""); - return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; + 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 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 @@ -102,6 +110,7 @@ return -1; fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); + fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC); #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) // this I really consider a linux bug. I am neither connected @@ -118,10 +127,10 @@ 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); + exit (EXIT_FAILURE); } - ipv4_ev_watcher.start (ipv4_fd, POLLIN); + ipv4_ev_watcher.start (ipv4_fd, EV_READ); } udpv4_fd = -1; @@ -134,6 +143,7 @@ return -1; fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); + fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); // standard daemon practise... { @@ -156,10 +166,10 @@ 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); + exit (EXIT_FAILURE); } - udpv4_ev_watcher.start (udpv4_fd, POLLIN); + udpv4_ev_watcher.start (udpv4_fd, EV_READ); } icmpv4_fd = -1; @@ -173,6 +183,7 @@ return -1; fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); + fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); #ifdef ICMP_FILTER { @@ -191,7 +202,7 @@ // fragment for me sometimes. { int oval = IP_PMTUDISC_DONT; - setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); } #endif @@ -200,10 +211,10 @@ if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) { slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (1); + exit (EXIT_FAILURE); } - icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); + icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); } #endif @@ -218,6 +229,7 @@ return -1; fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); + fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); // standard daemon practise... { @@ -230,61 +242,94 @@ 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, POLLIN); + tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); } #endif - tap = new tap_device (); - if (!tap) //D this, of course, never catches +#if ENABLE_DNS + if (THISNODE->protocols & PROT_DNSv4) { - slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); - exit (1); + 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; + + fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK); + fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC); + +# 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); + } + + dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); } - - run_script (run_script_cb (this, &vpn::script_if_up), true); +#endif - tap_ev_watcher.start (tap->fd, POLLIN); + ///////////////////////////////////////////////////////////////////////////// reconnect_all (); - return 0; -} + ///////////////////////////////////////////////////////////////////////////// -// send a vpn packet out to other hosts -bool -vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) -{ - switch (si.prot) + tap = new tap_device (); + if (!tap) //D this, of course, never catches { - case PROT_IPv4: - return send_ipv4_packet (pkt, si, tos); + slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); + exit (EXIT_FAILURE); + } + + fcntl (tap->fd, F_SETFD, FD_CLOEXEC); - case PROT_UDPv4: - return send_udpv4_packet (pkt, si, tos); + 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); + } -#if ENABLE_TCP - case PROT_TCPv4: - return send_tcpv4_packet (pkt, si, tos); -#endif + if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) + { + slog (L_ERR, _("if-up command execution failed, exiting.")); + exit (EXIT_FAILURE); + } -#if ENABLE_ICMP - case PROT_ICMPv4: - return send_icmpv4_packet (pkt, si, tos); -#endif + tap_ev_watcher.start (tap->fd, EV_READ); - default: - slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); - return false; - } + return 0; } bool @@ -322,23 +367,6 @@ } #if ENABLE_ICMP -struct icmp_header { - u8 type; - u8 code; - u16 checksum; - union { - struct { - u16 id; - u16 sequence; - } echo; - u32 gateway; - struct { - u16 unused; - u16 mtu; - } frag; - } un; -}; - bool vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { @@ -372,6 +400,25 @@ } 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 vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { unsigned int src = pkt->src (); @@ -395,7 +442,7 @@ 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) + else if (dst != THISNODE->id) { if (THISNODE->routerprio) // the tos setting gets lost here. who cares. @@ -411,10 +458,39 @@ } } +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::ipv4_ev (io_watcher &w, short revents) +vpn::ipv4_ev (ev::io &w, int revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EV_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -429,8 +505,7 @@ { pkt->len = len; - // raw sockets deliver the ipv4, but don't expect it on sends - // this is slow, but... + // raw sockets deliver the ipv4 header, but don't expect it on sends pkt->skip_hdr (IP_OVERHEAD); recv_vpn_packet (pkt, si); @@ -443,26 +518,20 @@ 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); + _("FATAL: unknown revents %08x in socket, terminating\n"), + revents); + exit (EXIT_FAILURE); } } #if ENABLE_ICMP void -vpn::icmpv4_ev (io_watcher &w, short revents) +vpn::icmpv4_ev (ev::io &w, int revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EV_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -497,26 +566,20 @@ delete pkt; } - else if (revents & POLLHUP) - { - // this cannot ;) happen on udp sockets - slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); - exit (1); - } else { slog (L_ERR, _("FATAL: unknown revents %08x in socket, terminating\n"), revents); - exit (1); + exit (EXIT_FAILURE); } } #endif void -vpn::udpv4_ev (io_watcher &w, short revents) +vpn::udpv4_ev (ev::io &w, int revents) { - if (revents & (POLLIN | POLLERR)) + if (revents & EV_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -541,79 +604,59 @@ 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); + exit (EXIT_FAILURE); } } void -vpn::tap_ev (io_watcher &w, short revents) +vpn::tap_ev (ev::io &w, int revents) { - if (revents & POLLIN) + if (revents & EV_READ) { /* process data */ tap_packet *pkt; pkt = tap->recv (); - int dst = mac2id (pkt->dst); - int src = mac2id (pkt->src); - - if (src != THISNODE->id) - { - slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); - exit (1); - } + if (!pkt) + return; - if (dst == THISNODE->id) + if (pkt->len > 14) { - slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); - 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 + + if (dst == THISNODE->id) { - // 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); + 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 (); } void -vpn::event_cb (time_watcher &w) +vpn::event_cb (ev::timer &w, int) { if (events) { @@ -622,12 +665,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) @@ -639,8 +679,6 @@ events = 0; } - - w.at = TSTAMP_CANCEL; } void @@ -663,11 +701,8 @@ 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 (); } } @@ -722,7 +757,7 @@ void vpn::dump_status () { - slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); + slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)ev_now ()); for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) (*c)->dump_status (); @@ -740,6 +775,9 @@ #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) { }