--- gvpe/src/connection.C 2004/05/10 21:24:48 1.35 +++ gvpe/src/connection.C 2008/08/11 16:37:47 1.77 @@ -1,35 +1,45 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2004 Marc Lehmann + Copyright (C) 2003-2008 Marc Lehmann - This program 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 this program; if not, write to the Free Software - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + 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 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 #include #include #include -#include "gettext.h" - #include "conf.h" #include "slog.h" #include "device.h" @@ -50,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; @@ -86,7 +137,8 @@ EVP_MD_CTX_cleanup (&ctx); } -struct rsa_entry { +struct rsa_entry +{ tstamp expire; rsaid id; rsachallenge chg; @@ -94,58 +146,60 @@ struct rsa_cache : list { - void cleaner_cb (time_watcher &w); time_watcher cleaner; + inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; bool find (const rsaid &id, rsachallenge &chg) - { - for (iterator i = begin (); i != end (); ++i) - { - if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) - { - memcpy (&chg, &i->chg, sizeof chg); + { + for (iterator i = begin (); i != end (); ++i) + { + if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ()) + { + memcpy (&chg, &i->chg, sizeof chg); - erase (i); - return true; - } - } + erase (i); + return true; + } + } - if (cleaner.at < NOW) - cleaner.start (NOW + RSA_TTL); + if (!cleaner.is_active ()) + cleaner.again (); - return false; - } + return false; + } void gen (rsaid &id, rsachallenge &chg) - { - rsa_entry e; + { + rsa_entry e; - RAND_bytes ((unsigned char *)&id, sizeof id); - RAND_bytes ((unsigned char *)&chg, sizeof chg); + RAND_bytes ((unsigned char *)&id, sizeof id); + RAND_bytes ((unsigned char *)&chg, sizeof chg); - e.expire = NOW + RSA_TTL; - e.id = id; - memcpy (&e.chg, &chg, sizeof chg); + e.expire = ev_now () + RSA_TTL; + e.id = id; + memcpy (&e.chg, &chg, sizeof chg); - push_back (e); + push_back (e); - if (cleaner.at < NOW) - cleaner.start (NOW + RSA_TTL); - } + if (!cleaner.is_active ()) + cleaner.again (); + } rsa_cache () - : cleaner (this, &rsa_cache::cleaner_cb) - { } + { + cleaner.set (this); + cleaner.set (RSA_TTL, RSA_TTL); + } } rsa_cache; -void rsa_cache::cleaner_cb (time_watcher &w) +void rsa_cache::cleaner_cb (ev::timer &w, int revents) { - if (!empty ()) + if (empty ()) + w.stop (); + else { - w.start (NOW + RSA_TTL); - for (iterator i = begin (); i != end (); ) - if (i->expire <= NOW) + if (i->expire <= ev_now ()) i = erase (i); else ++i; @@ -154,46 +208,80 @@ ////////////////////////////////////////////////////////////////////////////// -void pkt_queue::put (net_packet *p) +pkt_queue::pkt_queue (double max_ttl, int max_queue) +: max_ttl (max_ttl), max_queue (max_queue) { - if (queue[i]) - { - delete queue[i]; - j = (j + 1) % QUEUEDEPTH; - } + queue = new pkt [max_queue]; - queue[i] = p; + i = 0; + j = 0; - i = (i + 1) % QUEUEDEPTH; + expire.set (this); } -net_packet *pkt_queue::get () +pkt_queue::~pkt_queue () { - net_packet *p = queue[j]; + while (net_packet *p = get ()) + delete p; - if (p) + delete [] queue; +} + +void pkt_queue::expire_cb (ev::timer &w, int revents) +{ + ev_tstamp expire = ev_now () - max_ttl; + + for (;;) { - queue[j] = 0; - j = (j + 1) % QUEUEDEPTH; - } + if (empty ()) + break; - return p; + double diff = queue[j].tstamp - expire; + + if (diff >= 0.) + { + w.start (diff > 0.5 ? diff : 0.5); + break; + } + + delete get (); + } } -pkt_queue::pkt_queue () +void pkt_queue::put (net_packet *p) { - memset (queue, 0, sizeof (queue)); - i = 0; - j = 0; + ev_tstamp now = ev_now (); + + // start expiry timer + if (empty ()) + expire.start (max_ttl); + + int ni = i + 1 == max_queue ? 0 : i + 1; + + if (ni == j) + delete get (); + + queue[i].pkt = p; + queue[i].tstamp = now; + + i = ni; } -pkt_queue::~pkt_queue () +net_packet *pkt_queue::get () { - for (i = QUEUEDEPTH; --i > 0; ) - delete queue[i]; + if (empty ()) + return 0; + + net_packet *p = queue[j].pkt; + queue[j].pkt = 0; + + j = j + 1 == max_queue ? 0 : j + 1; + + return p; } -struct net_rateinfo { +struct net_rateinfo +{ u32 host; double pcnt, diff; tstamp last; @@ -222,7 +310,7 @@ for (i = begin (); i != end (); ) if (i->host == host) break; - else if (i->last < NOW - NRL_EXPIRE) + else if (i->last < ev_now () - NRL_EXPIRE) i = erase (i); else i++; @@ -234,7 +322,7 @@ ri.host = host; ri.pcnt = 1.; ri.diff = NRL_MAXDIF; - ri.last = NOW; + ri.last = ev_now (); push_front (ri); @@ -246,9 +334,9 @@ erase (i); ri.pcnt = ri.pcnt * NRL_ALPHA; - ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); + ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last); - ri.last = NOW; + ri.last = ev_now (); double dif = ri.diff / ri.pcnt; @@ -468,6 +556,9 @@ #if ENABLE_ROHC f |= FEATURE_ROHC; #endif +#if ENABLE_BRIDGING + f |= FEATURE_BRIDGING; +#endif return f; } }; @@ -478,7 +569,7 @@ prot_minor = PROTOCOL_MINOR; randsize = RAND_SIZE; hmaclen = HMACLENGTH; - flags = ENABLE_COMPRESSION ? 0x81 : 0x80; + flags = 0; challengelen = sizeof (rsachallenge); features = get_features (); @@ -498,10 +589,6 @@ slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); else if (hmaclen != HMACLENGTH) slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); -#if 0 // this implementation should handle all flag settings - else if (flags != curflags ()) - slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ()); -#endif else if (challengelen != sizeof (rsachallenge)) slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) @@ -587,33 +674,37 @@ void connection::connection_established () { + slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); + if (ictx && octx) { - connectmode = conf->connectmode; - - rekey.start (NOW + ::conf.rekey); - keepalive.start (NOW + ::conf.keepalive); + // make sure rekeying timeouts are slightly asymmetric + ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); + rekey.start (rekey_interval, rekey_interval); + keepalive.start (::conf.keepalive); // send queued packets if (ictx && octx) { while (tap_packet *p = (tap_packet *)data_queue.get ()) { - send_data_packet (p); + if (p->len) send_data_packet (p); delete p; } while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) { - send_vpn_packet (p, si, IPTOS_RELIABILITY); + if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); delete p; } } + + vpn->connection_established (this); } else { retry_cnt = 0; - establish_connection.start (NOW + 5); + establish_connection.start (5); keepalive.stop (); rekey.stop (); } @@ -622,13 +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 (vpn->can_direct (THISNODE, conf)) + protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ()); + else + { + 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 @@ -637,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"), + 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); } @@ -666,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, ">>%d %s [%s]", conf->id, pong ? "PT_PONG" : "PT_PING", (const char *)si); + send_vpn_packet (pkt, si, IPTOS_LOWDELAY); delete pkt; @@ -722,7 +820,7 @@ void connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) { - slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", + slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", conf->id, rid, (const char *)rsi); connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); @@ -733,30 +831,49 @@ delete r; } -void -connection::establish_connection_cb (time_watcher &w) +inline void +connection::establish_connection_cb (ev::timer &w, int revents) { if (!ictx && conf != THISNODE && connectmode != conf_node::C_NEVER && connectmode != conf_node::C_DISABLED - && NOW > w.at) + && !w.is_active ()) { - double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; + // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection + // and stop trying. should probably be handled by a per-connection expire handler. + if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) + { + reset_connection (); + return; + } - if (retry_int < 3600 * 8) - retry_cnt++; + last_establish_attempt = ev_now (); - w.start (NOW + retry_int); + ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 + ? (retry_cnt & 3) + 1 + : 1 << (retry_cnt >> 2)); reset_si (); - if (si.prot && !si.host) - vpn->send_connect_request (conf->id); + bool slow = si.prot & PROT_SLOW; + + if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) + { + /*TODO*/ /* start the timer so we don't recurse endlessly */ + w.start (1); + vpn->send_connect_request (this); + } else { + if (si.valid ()) + slog (L_DEBUG, _("%s: sending direct connection request to %s."), + conf->nodename, (const char *)si); + const sockinfo &dsi = forward_si (si); + slow = slow || (dsi.prot & PROT_SLOW); + if (dsi.valid () && auth_rate_limiter.can (dsi)) { if (retry_cnt < 4) @@ -765,6 +882,15 @@ send_ping (dsi, 0); } } + + retry_int *= slow ? 8. : 0.9; + + if (retry_int < conf->max_retry) + retry_cnt++; + else + retry_int = conf->max_retry; + + w.start (retry_int); } } @@ -777,15 +903,23 @@ conf->nodename, (const char *)si); if (::conf.script_node_down) - run_script (run_script_cb (this, &connection::script_node_down), false); + { + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-down command execution failed, continuing.")); + } } delete ictx; ictx = 0; delete octx; octx = 0; +#if ENABLE_DNS + dnsv4_reset_connection (); +#endif - si.host= 0; + si.host = 0; - last_activity = 0; + last_activity = 0.; + //last_si_change = 0.; retry_cnt = 0; rekey.stop (); @@ -802,8 +936,9 @@ reset_connection (); } -void -connection::rekey_cb (time_watcher &w) +// poor-man's rekeying +inline void +connection::rekey_cb (ev::timer &w, int revents) { reset_connection (); establish_connection (); @@ -829,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)//DDDD - data_queue.put (new tap_packet (*pkt)); - - establish_connection (); + data_queue.put (new tap_packet (*pkt)); + post_inject_queue (); } } @@ -849,19 +992,21 @@ else { vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); - - establish_connection (); + post_inject_queue (); } } void connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { - last_activity = NOW; + last_activity = ev_now (); - slog (L_NOISE, "<<%d received packet type %d from %d to %d", + slog (L_NOISE, "<<%d received packet type %d from %d to %d.", conf->id, pkt->typ (), pkt->src (), pkt->dst ()); + if (connectmode == conf_node::C_DISABLED) + return; + switch (pkt->typ ()) { case vpn_packet::PT_PING: @@ -874,6 +1019,8 @@ 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; @@ -889,7 +1036,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; } @@ -930,9 +1077,6 @@ octx = new crypto_ctx (k, 1); oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; - // compatibility code, remove when no longer required - if (p->flags & 1) p->features |= FEATURE_COMPRESSION; - conf->protocols = p->protocols; features = p->features & config_packet::get_features (); @@ -944,7 +1088,7 @@ } } else - slog (L_WARN, _("%s(%s): protocol mismatch"), + slog (L_WARN, _("%s(%s): protocol mismatch."), conf->nodename, (const char *)rsi); send_reset (rsi); @@ -969,7 +1113,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; } @@ -980,7 +1124,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; } @@ -1001,19 +1145,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" : "routed", p->prot_major, p->prot_minor); + connection_established (); + if (::conf.script_node_up) - run_script (run_script_cb (this, &connection::script_node_up), false); + { + 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); } @@ -1039,26 +1188,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 connection 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); + // fast re-sync on source address changes, useful especially for tcp/ip + //if (last_si_change < ev_now () + 5.) + // { + si = rsi; + + slog (L_INFO, _("%s(%s): socket address changed."), + conf->nodename, (const char *)si); + // } + //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; @@ -1073,22 +1241,28 @@ { connect_req_packet *p = (connect_req_packet *) pkt; - assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything - connection *c = vpn->conns[p->id - 1]; - conf->protocols = p->protocols; + if (p->id > 0 && p->id <= vpn->conns.size ()) + { + connection *c = vpn->conns[p->id - 1]; + conf->protocols = p->protocols; - slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", - conf->id, p->id, c->ictx && c->octx); + slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", + conf->id, p->id, c->ictx && c->octx); - if (c->ictx && c->octx) - { - // send connect_info packets to both sides, in case one is - // behind a nat firewall (or both ;) - c->send_connect_info (conf->id, si, conf->protocols); - send_connect_info (c->conf->id, c->si, c->conf->protocols); + if (c->ictx && c->octx) + { + // send connect_info packets to both sides, in case one is + // behind a nat firewall (or both ;) + c->send_connect_info (conf->id, si, conf->protocols); + send_connect_info (c->conf->id, c->si, c->conf->protocols); + } + else + c->establish_connection (); } else - c->establish_connection (); + slog (L_WARN, + _("received authenticated connection request from unknown node #%d, config file mismatch?"), + p->id); } break; @@ -1096,23 +1270,28 @@ case vpn_packet::PT_CONNECT_INFO: if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) { - connect_info_packet *p = (connect_info_packet *) pkt; - - assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything + connect_info_packet *p = (connect_info_packet *)pkt; - connection *c = vpn->conns[p->id - 1]; + if (p->id > 0 && p->id <= vpn->conns.size ()) + { + connection *c = vpn->conns[p->id - 1]; - c->conf->protocols = p->protocols; - protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); - p->si.upgrade_protocol (protocol, c->conf); + c->conf->protocols = p->protocols; + 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, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", + conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); - const sockinfo &dsi = forward_si (p->si); + const sockinfo &dsi = forward_si (p->si); - if (dsi.valid ()) - c->send_auth_request (dsi, true); + if (dsi.valid ()) + c->send_auth_request (dsi, true); + } + else + slog (L_WARN, + _("received authenticated connection request from unknown node #%d, config file mismatch?"), + p->id); } break; @@ -1123,25 +1302,26 @@ } } -void connection::keepalive_cb (time_watcher &w) +inline void +connection::keepalive_cb (ev::timer &w, int revents) { - if (NOW >= last_activity + ::conf.keepalive + 30) + if (ev_now () >= last_activity + ::conf.keepalive + 30) { reset_connection (); establish_connection (); } - else if (NOW < last_activity + ::conf.keepalive) - w.start (last_activity + ::conf.keepalive); + else if (ev_now () < last_activity + ::conf.keepalive) + w.start (last_activity + ::conf.keepalive - ev::now ()); else if (conf->connectmode != conf_node::C_ONDEMAND || THISNODE->connectmode != conf_node::C_ONDEMAND) { send_ping (si); - w.start (NOW + 5); + w.start (5); } - else if (NOW < last_activity + ::conf.keepalive + 10) + else if (ev_now () < last_activity + ::conf.keepalive + 10) // hold ondemand connections implicitly a few seconds longer // should delete octx, though, or something like that ;) - w.start (last_activity + ::conf.keepalive + 10); + w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); else reset_connection (); } @@ -1157,9 +1337,19 @@ delete p; } -void connection::script_node () +void connection::script_init_env (const char *ext) +{ + char *env; + asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); + asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); + asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, + 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, + conf->id & 0xff); putenv (env); +} + +void connection::script_init_connect_env () { - vpn->script_if_up (); + vpn->script_init_env (); char *env; asprintf (&env, "DESTID=%d", conf->id); putenv (env); @@ -1168,34 +1358,62 @@ asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); } -const char *connection::script_node_up () +inline const char * +connection::script_node_up () { - script_node (); + script_init_connect_env (); + + putenv ((char *)"STATE=up"); - putenv ("STATE=up"); + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_node_up ? ::conf.script_node_up : "node-up"); - return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; + return filename; } -const char *connection::script_node_down () +inline const char * +connection::script_node_down () { - script_node (); + script_init_connect_env (); + + putenv ((char *)"STATE=down"); - putenv ("STATE=down"); + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_node_down ? ::conf.script_node_down : "node-down"); - return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; + return filename; } -connection::connection(struct vpn *vpn_) -: vpn(vpn_) -, rekey (this, &connection::rekey_cb) -, keepalive (this, &connection::keepalive_cb) -, establish_connection (this, &connection::establish_connection_cb) +connection::connection (struct vpn *vpn, conf_node *conf) +: vpn(vpn), conf(conf), +#if ENABLE_DNS + dns (0), +#endif + 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; - connectmode = conf_node::C_ALWAYS; // initial setting + if (!conf->protocols) // make sure some protocol is enabled + conf->protocols = PROT_UDPv4; + + connectmode = conf->connectmode; + + // queue a dummy packet to force an initial connection attempt + if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) + vpn_queue.put (new net_packet); + reset_connection (); }