--- gvpe/src/connection.C 2008/08/07 16:34:21 1.67 +++ gvpe/src/connection.C 2008/08/15 18:35:24 1.82 @@ -1,27 +1,39 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2005 Marc Lehmann + Copyright (C) 2003-2008 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" #include +#include +#include #include #include @@ -48,6 +60,47 @@ #include "lzf/lzf_c.c" #include "lzf/lzf_d.c" +////////////////////////////////////////////////////////////////////////////// + +static std::queue< std::pair > rs_queue; +static ev::child rs_child_ev; + +void // c++ requires external linkage here, apparently :( +rs_child_cb (ev::child &w, int revents) +{ + w.stop (); + + if (rs_queue.empty ()) + return; + + pid_t pid = run_script (*rs_queue.front ().first, false); + if (pid) + { + w.set (pid); + w.start (); + } + else + slog (L_WARN, rs_queue.front ().second); + + delete rs_queue.front ().first; + rs_queue.pop (); +} + +// despite the fancy name, this is quite a hack +static void +run_script_queued (run_script_cb *cb, const char *warnmsg) +{ + rs_queue.push (std::make_pair (cb, warnmsg)); + + if (!rs_child_ev.is_active ()) + { + rs_child_ev.set (); + rs_child_ev (); + } +} + +////////////////////////////////////////////////////////////////////////////// + struct crypto_ctx { EVP_CIPHER_CTX cctx; @@ -621,7 +674,7 @@ void connection::connection_established () { - slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); + slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); if (ictx && octx) { @@ -645,6 +698,8 @@ delete p; } } + + vpn->connection_established (this); } else { @@ -658,22 +713,17 @@ void connection::reset_si () { - protocol = best_protocol (THISNODE->protocols & conf->protocols); - - // mask out protocols we cannot establish - if (!conf->udp_port) protocol &= ~PROT_UDPv4; - if (!conf->tcp_port) protocol &= ~PROT_TCPv4; - if (!conf->dns_port) protocol &= ~PROT_DNSv4; - - if (protocol - && (!conf->can_direct (THISNODE) - || !THISNODE->can_direct (conf))) + if (vpn->can_direct (THISNODE, conf)) + protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ()); + else { - slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); + slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); protocol = 0; } si.set (conf, protocol); + + is_direct = si.valid (); } // ensure sockinfo is valid, forward if necessary @@ -682,16 +732,16 @@ { if (!si.valid ()) { - connection *r = vpn->find_router (); + connection *r = vpn->find_router_for (this); if (r) { - slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), - conf->nodename, r->conf->nodename, (const char *)r->si); + slog (L_DEBUG, _("%s: no common protocol, trying to route through %s."), + conf->nodename, r->conf->nodename); return r->si; } else - slog (L_DEBUG, _("%s: node unreachable, no common protocol"), + slog (L_DEBUG, _("%s: node unreachable, no common protocol or no router available."), conf->nodename); } @@ -711,6 +761,9 @@ ping_packet *pkt = new ping_packet; pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); + + slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si); + send_vpn_packet (pkt, si, IPTOS_LOWDELAY); delete pkt; @@ -739,7 +792,7 @@ rsa_cache.gen (pkt->id, chg); rsa_encrypt (conf->rsa_key, chg, pkt->encr); - slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); + slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si); send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly @@ -757,7 +810,7 @@ pkt->hmac_set (octx); - slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); + slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly @@ -767,8 +820,8 @@ void connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) { - slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", - conf->id, rid, (const char *)rsi); + slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename, + vpn->conns[rid - 1]->conf->nodename, (const char *)rsi); connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); @@ -795,6 +848,8 @@ return; } + last_establish_attempt = ev_now (); + ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 ? (retry_cnt & 3) + 1 : 1 << (retry_cnt >> 2)); @@ -803,16 +858,17 @@ bool slow = si.prot & PROT_SLOW; - if (si.prot && !si.host) + if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) { - slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); /*TODO*/ /* start the timer so we don't recurse endlessly */ w.start (1); - vpn->send_connect_request (conf->id); + vpn->send_connect_request (this); } else { - slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); + if (si.valid ()) + slog (L_DEBUG, _("%s: sending direct connection request to %s."), + conf->nodename, (const char *)si); const sockinfo &dsi = forward_si (si); @@ -827,7 +883,7 @@ } } - retry_int *= slow ? 8. : 0.7; + retry_int *= slow ? 8. : 0.9; if (retry_int < conf->max_retry) retry_cnt++; @@ -848,10 +904,9 @@ if (::conf.script_node_down) { - run_script_cb cb; - cb.set (this); - if (!run_script (cb, false)) - slog (L_WARN, _("node-down command execution failed, continuing.")); + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-down command execution failed, continuing.")); } } @@ -863,7 +918,8 @@ si.host = 0; - last_activity = 0; + last_activity = 0.; + //last_si_change = 0.; retry_cnt = 0; rekey.stop (); @@ -880,6 +936,7 @@ reset_connection (); } +// poor-man's rekeying inline void connection::rekey_cb (ev::timer &w, int revents) { @@ -907,16 +964,24 @@ } void -connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) +connection::post_inject_queue () +{ + // force a connection every now and when when packets are sent (max 1/s) + if (ev_now () - last_establish_attempt >= 0.95) // arbitrary + establish_connection.stop (); + + establish_connection (); +} + +void +connection::inject_data_packet (tap_packet *pkt) { if (ictx && octx) send_data_packet (pkt); else { - if (!broadcast) - data_queue.put (new tap_packet (*pkt)); - - establish_connection (); + data_queue.put (new tap_packet (*pkt)); + post_inject_queue (); } } @@ -927,8 +992,7 @@ else { vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); - - establish_connection (); + post_inject_queue (); } } @@ -937,8 +1001,8 @@ { last_activity = ev_now (); - slog (L_NOISE, "<<%d received packet type %d from %d to %d", - conf->id, pkt->typ (), pkt->src (), pkt->dst ()); + slog (L_NOISE, "%s >> received packet type %d from %d to %d.", + conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); if (connectmode == conf_node::C_DISABLED) return; @@ -946,6 +1010,8 @@ switch (pkt->typ ()) { case vpn_packet::PT_PING: + slog (L_TRACE, "%s >> PT_PING", conf->nodename); + // we send pings instead of auth packets after some retries, // so reset the retry counter and establish a connection // when we receive a ping. @@ -955,11 +1021,14 @@ send_auth_request (rsi, true); } else + // we would love to change thre socket address here, but ping's aren't + // authenticated, so we best ignore it. send_ping (rsi, 1); // pong break; case vpn_packet::PT_PONG: + slog (L_TRACE, "%s >> PT_PONG", conf->nodename); break; case vpn_packet::PT_RESET: @@ -970,7 +1039,7 @@ if (!p->chk_config ()) { - slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), + slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."), conf->nodename, (const char *)rsi); connectmode = conf_node::C_DISABLED; } @@ -984,7 +1053,7 @@ { auth_req_packet *p = (auth_req_packet *) pkt; - slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); + slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) { @@ -1022,7 +1091,7 @@ } } else - slog (L_WARN, _("%s(%s): protocol mismatch"), + slog (L_WARN, _("%s(%s): protocol mismatch."), conf->nodename, (const char *)rsi); send_reset (rsi); @@ -1032,9 +1101,9 @@ case vpn_packet::PT_AUTH_RES: { - auth_res_packet *p = (auth_res_packet *) pkt; + auth_res_packet *p = (auth_res_packet *)pkt; - slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); + slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); if (p->chk_config ()) { @@ -1047,7 +1116,7 @@ if (!rsa_cache.find (p->id, chg)) { - slog (L_ERR, _("%s(%s): unrequested auth response ignored"), + slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."), conf->nodename, (const char *)rsi); break; } @@ -1058,7 +1127,7 @@ if (!p->hmac_chk (cctx)) { slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" - "could be an attack, or just corruption or a synchronization error"), + "could be an attack, or just corruption or a synchronization error."), conf->nodename, (const char *)rsi); break; } @@ -1079,24 +1148,24 @@ si = rsi; protocol = rsi.prot; - connection_established (); - - slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), + slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), conf->nodename, (const char *)rsi, + is_direct ? "direct" : "forwarded", p->prot_major, p->prot_minor); + connection_established (); + if (::conf.script_node_up) { - run_script_cb cb; - cb.set (this); - if (!run_script (cb, false)) - slog (L_WARN, _("node-up command execution failed, continuing.")); + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-up command execution failed, continuing.")); } break; } else - slog (L_ERR, _("%s(%s): sent and received challenge do not match"), + slog (L_ERR, _("%s(%s): sent and received challenge do not match."), conf->nodename, (const char *)rsi); } @@ -1122,26 +1191,45 @@ if (!p->hmac_chk (ictx)) slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" - "could be an attack, or just corruption or a synchronization error"), + "could be an attack, or just corruption or a synchronization error."), conf->nodename, (const char *)rsi); else { u32 seqno; tap_packet *d = p->unpack (this, seqno); + int seqclass = iseqno.seqno_classify (seqno); - if (iseqno.recv_ok (seqno)) + if (seqclass == 0) // ok { vpn->tap->send (d); if (si != rsi) { // fast re-sync on source address changes, useful especially for tcp/ip - si = rsi; - - slog (L_INFO, _("%s(%s): socket address changed to %s"), - conf->nodename, (const char *)si, (const char *)rsi); + //if (last_si_change < ev_now () + 5.) + // { + slog (L_INFO, _("%s(%s): changing socket address to %s."), + conf->nodename, (const char *)si, (const char *)rsi); + + si = rsi; + // } + //else + // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), + // conf->nodename, (const char *)si, (const char *)rsi); } } + else if (seqclass == 1) // far history + slog (L_ERR, _("received very old packet (received %08lx, expected %08lx). " + "possible replay attack, or just packet duplication/delay, ignoring."), seqno, iseqno.seq + 1); + else if (seqclass == 2) // in-window duplicate, happens often on wireless + slog (L_DEBUG, _("received recent duplicated packet (received %08lx, expected %08lx). " + "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1); + else if (seqclass == 3) // reset + { + slog (L_ERR, _("received out-of-sync (far future) packet (received %08lx, expected %08lx). " + "probably just massive packet loss, sending reset."), seqno, iseqno.seq + 1); + send_reset (rsi); + } delete d; break; @@ -1161,8 +1249,8 @@ connection *c = vpn->conns[p->id - 1]; conf->protocols = p->protocols; - slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", - conf->id, p->id, c->ictx && c->octx); + slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]", + conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); if (c->ictx && c->octx) { @@ -1195,8 +1283,9 @@ protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); p->si.upgrade_protocol (protocol, c->conf); - slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", - conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); + slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]", + conf->nodename, vpn->conns[p->id - 1]->conf->nodename, + (const char *)p->si, !c->ictx && !c->octx); const sockinfo &dsi = forward_si (p->si); @@ -1245,7 +1334,8 @@ { connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); - slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); + slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", + conf->nodename, vpn->conns[id - 1]->conf->nodename); p->hmac_set (octx); send_vpn_packet (p, si); @@ -1310,15 +1400,15 @@ #if ENABLE_DNS dns (0), #endif - data_queue(conf->max_ttl, conf->max_queue), - vpn_queue(conf->max_ttl, conf->max_queue) + data_queue(conf->max_ttl, conf->max_queue + 1), + vpn_queue(conf->max_ttl, conf->max_queue + 1) { rekey .set (this); keepalive .set (this); establish_connection.set (this); + last_establish_attempt = 0.; octx = ictx = 0; - retry_cnt = 0; if (!conf->protocols) // make sure some protocol is enabled conf->protocols = PROT_UDPv4; @@ -1327,11 +1417,7 @@ // queue a dummy packet to force an initial connection attempt if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) - { - net_packet *p = new net_packet; - p->len = 0; - vpn_queue.put (p); - } + vpn_queue.put (new net_packet); reset_connection (); }