--- gvpe/src/vpn.C 2004/07/25 18:11:54 1.26 +++ gvpe/src/vpn.C 2007/12/06 00:35:29 1.45 @@ -1,8 +1,10 @@ /* vpn.C -- handle the protocol, encryption, handshaking etc. - Copyright (C) 2003-2004 Marc Lehmann + Copyright (C) 2003-2007 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. @@ -13,8 +15,8 @@ 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" @@ -46,7 +48,20 @@ ///////////////////////////////////////////////////////////////////////////// -const char *vpn::script_if_up () +static void inline +set_tos (int fd, int &tos_prev, int tos) +{ +#if defined(SOL_IP) && defined(IP_TOS) + if (tos_prev == tos) + return; + + tos_prev = tos; + setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); +#endif +} + +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 @@ -57,24 +72,50 @@ 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, "IFTYPE=%s", IFTYPE); putenv (env); asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); 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, "NODES=%d", conns.size ()); putenv (env); + asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); - return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; + 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); + } +} + +inline const char * +vpn::script_if_init () +{ + script_init_env (); + + return tap->if_up (); +} + +inline 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; + ipv4_tos = -1; + ipv4_fd = -1; if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) { @@ -84,6 +125,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 @@ -103,10 +145,11 @@ exit (EXIT_FAILURE); } - ipv4_ev_watcher.start (ipv4_fd, EVENT_READ); + ipv4_ev_watcher.start (ipv4_fd, EV_READ); } - udpv4_fd = -1; + udpv4_tos = -1; + udpv4_fd = -1; if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) { @@ -116,6 +159,7 @@ return -1; fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); + fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); // standard daemon practise... { @@ -141,10 +185,11 @@ exit (EXIT_FAILURE); } - udpv4_ev_watcher.start (udpv4_fd, EVENT_READ); + udpv4_ev_watcher.start (udpv4_fd, EV_READ); } - icmpv4_fd = -1; + icmpv4_tos = -1; + icmpv4_fd = -1; #if ENABLE_ICMP if (THISNODE->protocols & PROT_ICMPv4) @@ -155,6 +200,7 @@ return -1; fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); + fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); #ifdef ICMP_FILTER { @@ -173,7 +219,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 @@ -185,7 +231,7 @@ exit (EXIT_FAILURE); } - icmpv4_ev_watcher.start (icmpv4_fd, EVENT_READ); + icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); } #endif @@ -200,6 +246,7 @@ return -1; fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); + fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); // standard daemon practise... { @@ -221,25 +268,45 @@ exit (EXIT_FAILURE); } - tcpv4_ev_watcher.start (tcpv4_fd, EVENT_READ); + tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); } #endif + dnsv4_tos = -1; + dnsv4_fd = -1; + #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; + 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 (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); + setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); } - sockinfo si (THISNODE, PROT_DNSv4); + sockinfo si (THISNODE->dns_hostname, + THISNODE->dns_hostname ? THISNODE->dns_port : 0, + PROT_DNSv4); if (bind (dnsv4_fd, si.sav4 (), si.salenv4 ())) { @@ -247,10 +314,16 @@ exit (EXIT_FAILURE); } - dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); + dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); } #endif + ///////////////////////////////////////////////////////////////////////////// + + reconnect_all (); + + ///////////////////////////////////////////////////////////////////////////// + tap = new tap_device (); if (!tap) //D this, of course, never catches { @@ -258,54 +331,35 @@ exit (EXIT_FAILURE); } - run_script (run_script_cb (this, &vpn::script_if_up), true); + fcntl (tap->fd, F_SETFD, FD_CLOEXEC); - tap_ev_watcher.start (tap->fd, EVENT_READ); + run_script_cb cb; + cb.set (this); - 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) + if (tap->if_up () && + !run_script (cb, true)) { - 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 + slog (L_ERR, _("interface initialization command '%s' failed, exiting."), + tap->if_up ()); + exit (EXIT_FAILURE); + } -#if ENABLE_ICMP - case PROT_ICMPv4: - return send_icmpv4_packet (pkt, si, tos); -#endif + cb.set (this); + if (!run_script (cb, true)) + { + slog (L_ERR, _("if-up command execution failed, exiting.")); + exit (EXIT_FAILURE); + } -#if ENABLE_DNS - case PROT_DNSv4: - return send_dnsv4_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 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 + set_tos (ipv4_fd, ipv4_tos, tos); sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); return true; @@ -338,10 +392,6 @@ 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]); @@ -350,6 +400,7 @@ hdr->checksum = 0; hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); + set_tos (icmpv4_fd, icmpv4_tos, tos); sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); return true; @@ -359,9 +410,7 @@ 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 + set_tos (udpv4_fd, udpv4_tos, tos); sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); return true; @@ -426,10 +475,40 @@ } } -void -vpn::ipv4_ev (io_watcher &w, short revents) +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; +} + +inline void +vpn::ipv4_ev (ev::io &w, int revents) { - if (revents & EVENT_READ) + if (revents & EV_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -444,8 +523,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); @@ -461,17 +539,17 @@ else { slog (L_ERR, - _("FATAL: unknown revents %08x in socket, terminating\n"), - revents); + _("FATAL: unknown revents %08x in socket, terminating\n"), + revents); exit (EXIT_FAILURE); } } #if ENABLE_ICMP -void -vpn::icmpv4_ev (io_watcher &w, short revents) +inline void +vpn::icmpv4_ev (ev::io &w, int revents) { - if (revents & EVENT_READ) + if (revents & EV_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -516,10 +594,10 @@ } #endif -void -vpn::udpv4_ev (io_watcher &w, short revents) +inline void +vpn::udpv4_ev (ev::io &w, int revents) { - if (revents & EVENT_READ) + if (revents & EV_READ) { vpn_packet *pkt = new vpn_packet; struct sockaddr_in sa; @@ -553,10 +631,10 @@ } } -void -vpn::tap_ev (io_watcher &w, short revents) +inline void +vpn::tap_ev (ev::io &w, int revents) { - if (revents & EVENT_READ) + if (revents & EV_READ) { /* process data */ tap_packet *pkt; @@ -573,7 +651,7 @@ if (src != THISNODE->id) { - slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); + slog (L_ERR, _("FATAL: tap packet not originating on current node received (if-up script not working properly?), exiting.")); exit (EXIT_FAILURE); } @@ -595,8 +673,8 @@ abort (); } -void -vpn::event_cb (time_watcher &w) +inline void +vpn::event_cb (ev::timer &w, int) { if (events) { @@ -641,11 +719,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 (); } } @@ -700,7 +775,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 (); @@ -709,20 +784,20 @@ } vpn::vpn (void) -: event (this, &vpn::event_cb) -, udpv4_ev_watcher (this, &vpn::udpv4_ev) -, ipv4_ev_watcher (this, &vpn::ipv4_ev) +{ + event .set (this); + udpv4_ev_watcher .set (this); + ipv4_ev_watcher .set (this); #if ENABLE_TCP -, tcpv4_ev_watcher (this, &vpn::tcpv4_ev) + tcpv4_ev_watcher .set (this); #endif #if ENABLE_ICMP -, icmpv4_ev_watcher(this, &vpn::icmpv4_ev) + icmpv4_ev_watcher.set (this); #endif #if ENABLE_DNS -, dnsv4_ev_watcher (this, &vpn::dnsv4_ev) + dnsv4_ev_watcher .set (this); #endif -, tap_ev_watcher (this, &vpn::tap_ev) -{ + tap_ev_watcher .set (this); } vpn::~vpn ()