--- gvpe/src/connection.C 2005/03/02 05:49:31 1.41 +++ gvpe/src/connection.C 2007/11/10 19:43:37 1.61 @@ -1,8 +1,10 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2004 Marc Lehmann + 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. @@ -13,14 +15,12 @@ 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 @@ -28,8 +28,6 @@ #include #include -#include "gettext.h" - #include "conf.h" #include "slog.h" #include "device.h" @@ -94,58 +92,60 @@ struct rsa_cache : list { - void cleaner_cb (time_watcher &w); time_watcher cleaner; + 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::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::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 (this, &rsa_cache::cleaner_cb) + { + 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::ev_now ()) i = erase (i); else ++i; @@ -222,7 +222,7 @@ for (i = begin (); i != end (); ) if (i->host == host) break; - else if (i->last < NOW - NRL_EXPIRE) + else if (i->last < ev::ev_now () - NRL_EXPIRE) i = erase (i); else i++; @@ -234,7 +234,7 @@ ri.host = host; ri.pcnt = 1.; ri.diff = NRL_MAXDIF; - ri.last = NOW; + ri.last = ev::ev_now (); push_front (ri); @@ -246,9 +246,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::ev_now () - ri.last); - ri.last = NOW; + ri.last = ev::ev_now (); double dif = ri.diff / ri.pcnt; @@ -468,6 +468,9 @@ #if ENABLE_ROHC f |= FEATURE_ROHC; #endif +#if ENABLE_BRIDGING + f |= FEATURE_BRIDGING; +#endif return f; } }; @@ -478,7 +481,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 +501,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,14 +586,16 @@ 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; // make sure rekeying timeouts are slightly asymmetric - rekey.start (NOW + ::conf.rekey - + (conf->id > THISNODE->id ? 10 : 0)); - keepalive.start (NOW + ::conf.keepalive); + 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) @@ -615,7 +616,7 @@ else { retry_cnt = 0; - establish_connection.start (NOW + 5); + establish_connection.start (5); keepalive.stop (); rekey.stop (); } @@ -631,6 +632,14 @@ if (!conf->tcp_port) protocol &= ~PROT_TCPv4; if (!conf->dns_port) protocol &= ~PROT_DNSv4; + if (protocol + && (!conf->can_direct (THISNODE) + || !THISNODE->can_direct (conf))) + { + slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); + protocol = 0; + } + si.set (conf, protocol); } @@ -644,8 +653,8 @@ if (r) { - slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), - conf->nodename, r->conf->nodename); + slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), + conf->nodename, r->conf->nodename, (const char *)r->si); return r->si; } else @@ -659,33 +668,7 @@ void connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) { - bool ok; - - switch (si.prot) - { - case PROT_IPv4: - ok = vpn->send_ipv4_packet (pkt, si, tos); break; - case PROT_UDPv4: - ok = vpn->send_udpv4_packet (pkt, si, tos); break; -#if ENABLE_TCP - case PROT_TCPv4: - ok = vpn->send_tcpv4_packet (pkt, si, tos); break; -#endif -#if ENABLE_ICMP - case PROT_ICMPv4: - ok = vpn->send_icmpv4_packet (pkt, si, tos); break; -#endif -#if ENABLE_DNS - case PROT_DNSv4: - ok = send_dnsv4_packet (pkt, si, tos); break; -#endif - - default: - slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); - ok = false; - } - - if (!ok) + if (!vpn->send_vpn_packet (pkt, si, tos)) reset_connection (); } @@ -751,7 +734,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); @@ -763,31 +746,37 @@ } void -connection::establish_connection_cb (time_watcher &w) +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; - - if (retry_int < conf->max_retry) - retry_cnt++; - else - retry_int = conf->max_retry; - - w.start (NOW + retry_int); + ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 + ? (retry_cnt & 3) + 1 + : 1 << (retry_cnt >> 2)); reset_si (); + bool slow = si.prot & PROT_SLOW; + if (si.prot && !si.host) - vpn->send_connect_request (conf->id); + { + 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); + } else { + slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); + const sockinfo &dsi = forward_si (si); + slow = slow || (dsi.prot & PROT_SLOW); + if (dsi.valid () && auth_rate_limiter.can (dsi)) { if (retry_cnt < 4) @@ -796,6 +785,15 @@ send_ping (dsi, 0); } } + + retry_int *= slow ? 8. : 0.7; + + if (retry_int < conf->max_retry) + retry_cnt++; + else + retry_int = conf->max_retry; + + w.start (retry_int); } } @@ -808,11 +806,15 @@ conf->nodename, (const char *)si); if (::conf.script_node_down) - run_script (run_script_cb (this, &connection::script_node_down), false); + if (!run_script (run_script_cb (this, &connection::script_node_down), false)) + slog (L_WARN, _("node-down command execution failed, continuing.")); } delete ictx; ictx = 0; delete octx; octx = 0; +#if ENABLE_DNS + dnsv4_reset_connection (); +#endif si.host = 0; @@ -834,7 +836,7 @@ } void -connection::rekey_cb (time_watcher &w) +connection::rekey_cb (ev::timer &w, int revents) { reset_connection (); establish_connection (); @@ -866,7 +868,7 @@ send_data_packet (pkt); else { - if (!broadcast)//DDDD + if (!broadcast) data_queue.put (new tap_packet (*pkt)); establish_connection (); @@ -888,11 +890,14 @@ void connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { - last_activity = NOW; + last_activity = ev::ev_now (); 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: @@ -961,9 +966,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 (); @@ -1039,7 +1041,8 @@ p->prot_major, p->prot_minor); if (::conf.script_node_up) - run_script (run_script_cb (this, &connection::script_node_up), false); + if (!run_script (run_script_cb (this, &connection::script_node_up), false)) + slog (L_WARN, _("node-up command execution failed, continuing.")); break; } @@ -1083,7 +1086,7 @@ if (si != rsi) { - // fast re-sync on connection changes, useful especially for tcp/ip + // fast re-sync on source address changes, useful especially for tcp/ip si = rsi; slog (L_INFO, _("%s(%s): socket address changed to %s"), @@ -1104,22 +1107,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; @@ -1129,7 +1138,7 @@ { connect_info_packet *p = (connect_info_packet *)pkt; - if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything + if (p->id > 0 && p->id <= vpn->conns.size ()) { connection *c = vpn->conns[p->id - 1]; @@ -1145,6 +1154,10 @@ 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; @@ -1155,25 +1168,25 @@ } } -void connection::keepalive_cb (time_watcher &w) +void connection::keepalive_cb (ev::timer &w, int revents) { - if (NOW >= last_activity + ::conf.keepalive + 30) + if (ev::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::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::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 (); } @@ -1189,9 +1202,19 @@ delete p; } -void connection::script_node () +void connection::script_init_env (const char *ext) { - vpn->script_if_up (); + 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_init_env (); char *env; asprintf (&env, "DESTID=%d", conf->id); putenv (env); @@ -1202,20 +1225,32 @@ const char *connection::script_node_up () { - script_node (); + script_init_connect_env (); - putenv ("STATE=up"); + putenv ((char *)"STATE=up"); - return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_node_up ? ::conf.script_node_up : "node-up"); + + return filename; } 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, conf_node *conf) @@ -1224,9 +1259,7 @@ , keepalive (this, &connection::keepalive_cb) , establish_connection (this, &connection::establish_connection_cb) #if ENABLE_DNS -, dnsv4_tw (this, &connection::dnsv4_cb) -, dns_rcvdq (0), dns_snddq (0) -, dns_rcvseq (0), dns_sndseq (0) +, dns (0) #endif { octx = ictx = 0;