--- gvpe/src/vpn.C 2007/12/02 00:20:19 1.41 +++ gvpe/src/vpn.C 2011/12/17 22:05:34 1.62 @@ -1,22 +1,32 @@ /* vpn.C -- handle the protocol, encryption, handshaking etc. - Copyright (C) 2003-2005 Marc Lehmann + Copyright (C) 2003-2008,2010,2011 Marc Lehmann 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. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with gvpe; if not, write to the Free Software - Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 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 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ #include "config.h" @@ -89,14 +99,16 @@ } } -const char *vpn::script_if_init () +inline const char * +vpn::script_if_init () { script_init_env (); return tap->if_up (); } -const char *vpn::script_if_up () +inline const char * +vpn::script_if_up () { script_init_env (); @@ -110,21 +122,79 @@ } int +vpn::setup_socket (u8 prot, int family, int type, int proto) +{ + int fd = socket (family, type, proto); + + if (fd < 0) + { + slog (L_ERR, _("unable to create %s socket: %s."), strprotocol (prot), strerror (errno)); + return fd; + } + + fcntl (fd, F_SETFL, O_NONBLOCK); + fcntl (fd, F_SETFD, FD_CLOEXEC); + +#ifdef SO_MARK + if (::conf.nfmark) + if (setsockopt (fd, SOL_SOCKET, SO_MARK, &::conf.nfmark, sizeof ::conf.nfmark)) + slog (L_WARN, _("unable to set nfmark on %s socket: %s"), strprotocol (prot), strerror (errno)); +#endif + + return fd; +} + +int vpn::setup () { + int success = 0; + +#if 1//D2 + ipv42_tos = -1; + ipv42_fd = -1; + + if (THISNODE->protocols & PROT_IPv42 && ::conf.ip_proto) + { + ipv42_fd = setup_socket (PROT_IPv42, PF_INET, SOCK_RAW, ::conf.ip2_proto); + + if (ipv42_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 (ipv42_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); + } +#endif + + sockinfo si (THISNODE, PROT_IPv42); + + if (bind (ipv42_fd, si.sav4 (), si.salenv4 ())) + { + slog (L_ERR, _("can't bind ipv42 socket on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; + } + + ipv42_ev_watcher.start (ipv42_fd, EV_READ); + ++success; + } + else + THISNODE->protocols &= ~PROT_IPv42; +#endif + ipv4_tos = -1; ipv4_fd = -1; if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) { - ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); + ipv4_fd = setup_socket (PROT_IPv4, PF_INET, SOCK_RAW, ::conf.ip_proto); if (ipv4_fd < 0) 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 // nor do I fragment myself. Linux still sets DF and doesn't @@ -139,26 +209,26 @@ 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); + slog (L_ERR, _("can't bind ipv4 socket on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; } ipv4_ev_watcher.start (ipv4_fd, EV_READ); + ++success; } + else + THISNODE->protocols &= ~PROT_IPv4; udpv4_tos = -1; udpv4_fd = -1; if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) { - udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); + udpv4_fd = setup_socket (PROT_UDPv4, PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpv4_fd < 0) return -1; - fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); - fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); - // standard daemon practise... { int oval = 1; @@ -179,12 +249,15 @@ 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); + slog (L_ERR, _("can't bind udpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; } udpv4_ev_watcher.start (udpv4_fd, EV_READ); + ++success; } + else + THISNODE->protocols &= ~PROT_UDPv4; icmpv4_tos = -1; icmpv4_fd = -1; @@ -192,14 +265,11 @@ #if ENABLE_ICMP if (THISNODE->protocols & PROT_ICMPv4) { - icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); + icmpv4_fd = setup_socket (PROT_ICMPv4, PF_INET, SOCK_RAW, IPPROTO_ICMP); if (icmpv4_fd < 0) return -1; - fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); - fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); - #ifdef ICMP_FILTER { icmp_filter oval; @@ -225,11 +295,12 @@ 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); + slog (L_ERR, _("can't bind icmpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; } icmpv4_ev_watcher.start (icmpv4_fd, EV_READ); + ++success; } #endif @@ -238,14 +309,11 @@ #if ENABLE_TCP if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) { - tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); + tcpv4_fd = setup_socket (PROT_TCPv4, PF_INET, SOCK_STREAM, IPPROTO_TCP); if (tcpv4_fd < 0) return -1; - fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); - fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); - // standard daemon practise... { int oval = 1; @@ -256,18 +324,21 @@ if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) { - slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (EXIT_FAILURE); + slog (L_ERR, _("can't bind tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; } if (listen (tcpv4_fd, 5)) { - slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); - exit (EXIT_FAILURE); + slog (L_ERR, _("can't listen tcpv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; } tcpv4_ev_watcher.start (tcpv4_fd, EV_READ); + ++success; } + else + THISNODE->protocols &= ~PROT_TCPv4; #endif dnsv4_tos = -1; @@ -278,14 +349,11 @@ { dns_forwarder.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); - dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); + dnsv4_fd = setup_socket (PROT_DNSv4, 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 @@ -308,16 +376,23 @@ 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); + slog (L_ERR, _("can't bind dnsv4 on %s: %s, exiting."), (const char *)si, strerror (errno)); + return -1; } dnsv4_ev_watcher.start (dnsv4_fd, EV_READ); + ++success; } #endif ///////////////////////////////////////////////////////////////////////////// + if (!success) + { + slog (L_ERR, _("no protocols enabled.")); + return -1; + } + reconnect_all (); ///////////////////////////////////////////////////////////////////////////// @@ -325,24 +400,28 @@ tap = new tap_device (); if (!tap) //D this, of course, never catches { - slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); - exit (EXIT_FAILURE); + slog (L_ERR, _("cannot create network interface '%s'."), conf.ifname); + return -1; } fcntl (tap->fd, F_SETFD, FD_CLOEXEC); + run_script_cb cb; + cb.set (this); + if (tap->if_up () && - !run_script (run_script_cb (this, &vpn::script_if_init), true)) + !run_script (cb, true)) { - slog (L_ERR, _("interface initialization command '%s' failed, exiting."), + slog (L_ERR, _("interface initialization command '%s' failed."), tap->if_up ()); - exit (EXIT_FAILURE); + return -1; } - if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) + cb.set (this); + if (!run_script (cb, true)) { - slog (L_ERR, _("if-up command execution failed, exiting.")); - exit (EXIT_FAILURE); + slog (L_ERR, _("if-up command execution failed.")); + return -1; } tap_ev_watcher.start (tap->fd, EV_READ); @@ -359,6 +438,17 @@ return true; } +#if 1 //D +bool +vpn::send_ipv42_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ + set_tos (ipv42_fd, ipv42_tos, tos); + sendto (ipv42_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); + + return true; +} +#endif + static u16 ipv4_checksum (u16 *data, unsigned int len) { @@ -425,7 +515,7 @@ // 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); + (*c)->inject_data_packet (pkt); } } @@ -435,23 +525,20 @@ unsigned int src = pkt->src (); unsigned int dst = pkt->dst (); - slog (L_NOISE, _("<typ (), pkt->src (), pkt->dst (), pkt->len); if (src == 0 || src > conns.size () || dst > conns.size () || 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)"), + 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) - slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), + slog (L_WARN, _("%s(%s): received broadcast (protocol violation)."), c->conf->nodename, (const char *)rsi); else if (dst != THISNODE->id) { @@ -460,7 +547,7 @@ conns[dst - 1]->inject_vpn_packet (pkt); else slog (L_WARN, - _("%s(%s): forwarding request (=> %s), but we are no router"), + _("%s(%s): request to forward packet to %s, but we are no router (config mismatch?)."), c->conf->nodename, (const char *)rsi, conns[dst - 1]->conf->nodename); } @@ -474,13 +561,18 @@ { switch (si.prot) { +#if 1//D2 + case PROT_IPv42: + return send_ipv42_packet (pkt, si, tos); +#endif + case PROT_IPv4: return send_ipv4_packet (pkt, si, tos); case PROT_UDPv4: return send_udpv4_packet (pkt, si, tos); -#if ENABLE_TCP +#if ENABLE_TCP case PROT_TCPv4: return send_tcpv4_packet (pkt, si, tos); #endif @@ -493,13 +585,55 @@ return send_dnsv4_packet (pkt, si, tos); #endif default: - slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); + slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol."), (const char *)si); } return false; } -void +#if 1//D2 +inline void +vpn::ipv42_ev (ev::io &w, int revents) +{ + if (revents & EV_READ) + { + vpn_packet *pkt = new vpn_packet; + struct sockaddr_in sa; + socklen_t sa_len = sizeof (sa); + int len; + + len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); + + sockinfo si(sa, PROT_IPv42); + + if (len > 0) + { + pkt->len = len; + + // raw sockets deliver the ipv4 header, but don't expect it on sends + pkt->skip_hdr (pkt->ipv4_hdr_len ()); + + recv_vpn_packet (pkt, si); + } + else + { + // probably ECONNRESET or somesuch + slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno)); + } + + delete pkt; + } + else + { + slog (L_ERR, + _("FATAL: unknown revents %08x in socket, exiting.\n"), + revents); + exit (EXIT_FAILURE); + } +} +#endif + +inline void vpn::ipv4_ev (ev::io &w, int revents) { if (revents & EV_READ) @@ -518,14 +652,14 @@ pkt->len = len; // raw sockets deliver the ipv4 header, but don't expect it on sends - pkt->skip_hdr (IP_OVERHEAD); + pkt->skip_hdr (pkt->ipv4_hdr_len ()); recv_vpn_packet (pkt, si); } else { // probably ECONNRESET or somesuch - slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); + slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno)); } delete pkt; @@ -533,14 +667,14 @@ else { slog (L_ERR, - _("FATAL: unknown revents %08x in socket, terminating\n"), + _("FATAL: unknown revents %08x in socket, exiting.\n"), revents); exit (EXIT_FAILURE); } } #if ENABLE_ICMP -void +inline void vpn::icmpv4_ev (ev::io &w, int revents) { if (revents & EV_READ) @@ -565,7 +699,7 @@ { // raw sockets deliver the ipv4, but don't expect it on sends // this is slow, but... - pkt->skip_hdr (ICMP_OVERHEAD); + pkt->skip_hdr (pkt->ipv4_hdr_len () + (ICMP_OVERHEAD - IP_OVERHEAD)); recv_vpn_packet (pkt, si); } @@ -573,7 +707,7 @@ else { // probably ECONNRESET or somesuch - slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); + slog (L_DEBUG, _("%s: %s."), (const char *)si, strerror (errno)); } delete pkt; @@ -581,14 +715,14 @@ else { slog (L_ERR, - _("FATAL: unknown revents %08x in socket, terminating\n"), + _("FATAL: unknown revents %08x in socket, exiting.\n"), revents); exit (EXIT_FAILURE); } } #endif -void +inline void vpn::udpv4_ev (ev::io &w, int revents) { if (revents & EV_READ) @@ -611,7 +745,7 @@ else { // probably ECONNRESET or somesuch - slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); + slog (L_DEBUG, _("%s: fd %d, %s."), (const char *)si, w.fd, strerror (errno)); } delete pkt; @@ -619,13 +753,13 @@ else { slog (L_ERR, - _("FATAL: unknown revents %08x in socket, terminating\n"), + _("FATAL: unknown revents %08x in socket, exiting.\n"), revents); exit (EXIT_FAILURE); } } -void +inline void vpn::tap_ev (ev::io &w, int revents) { if (revents & EV_READ) @@ -667,7 +801,7 @@ abort (); } -void +inline void vpn::event_cb (ev::timer &w, int) { if (events) @@ -678,13 +812,13 @@ shutdown_all (); remove_pid (conf.pidfilename); - slog (L_INFO, _("terminating")); + slog (L_INFO, _("exiting.")); exit (EXIT_SUCCESS); } if (events & EVENT_RECONNECT) { - slog (L_INFO, _("forced reconnect")); + slog (L_INFO, _("forced reconnect.")); reconnect_all (); } @@ -710,48 +844,108 @@ connection_init (); - for (configuration::node_vector::iterator i = conf.nodes.begin (); - i != conf.nodes.end (); ++i) - { - connection *conn = new connection (this, *i); - conns.push_back (conn); - conn->establish_connection (); - } + for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) + conns.push_back (new connection (this, *i)); + + for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) + (*c)->establish_connection (); +} + +bool +vpn::can_direct (conf_node *src, conf_node *dst) const +{ + return src != dst + && src->may_direct (dst) + && dst->may_direct (src) + && (((src->protocols & dst->protocols) && src->connectmode == conf_node::C_ALWAYS) + || (src->protocols & dst->connectable_protocols ())); } -connection *vpn::find_router () +// only works for indirect and routed connections: find a router +// from THISNODE to dst +connection * +vpn::find_router_for (const connection *dst) { - u32 prio = 1; connection *router = 0; + // first try to find a router with a direct connection, route there + // regardless of any other considerations. + { + u32 prio = 1; + + for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) + { + connection *c = *i; + + if (c->conf->routerprio > prio + && c->conf != THISNODE + && can_direct (c->conf, dst->conf) + && c->ictx && c->octx) + { + prio = c->conf->routerprio; + router = c; + } + } + } + + if (router) + return router; + + // second try find the router with the highest priority, higher than ours + { + u32 prio = THISNODE->routerprio ? THISNODE->routerprio : 1; + + for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) + { + connection *c = *i; + + if (c->conf->routerprio > prio + && c != dst + && c->conf != THISNODE + && c->ictx && c->octx) + { + prio = c->conf->routerprio; + router = c; + } + } + } + + return router; +} + +void +vpn::connection_established (connection *c) +{ for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) { - connection *c = *i; + connection *o = *i; - if (c->conf->routerprio > prio - && 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 + if (!o->is_direct + && o->si.valid () + && c->si != o->si + && c == find_router_for (o)) { - prio = c->conf->routerprio; - router = c; + slog (L_DEBUG, _("%s: can now route packets via %s, re-keying connection."), + o->conf->nodename, c->conf->nodename); + o->rekey (); } } - - return router; } -void vpn::send_connect_request (int id) +void +vpn::send_connect_request (connection *c) { - connection *c = find_router (); + connection *r = find_router_for (c); - if (c) - c->send_connect_request (id); + if (r) + { + slog (L_TRACE, _("%s: no address known, sending mediated connection request via %s."), + c->conf->nodename, r->conf->nodename); + r->send_connect_request (c->conf->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 (); + slog (L_DEBUG, _("%s: no way to connect and no router found: unable to connect at this time."), + c->conf->nodename); } void @@ -762,8 +956,6 @@ connectmode, conf->connectmode, (const char *)si, (int)prot_minor); slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); - slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"), - (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at)); } void @@ -778,20 +970,23 @@ } 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 1//D2 + ipv42_ev_watcher .set (this); +#endif #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 ()