--- gvpe/src/connection.C 2004/01/29 18:55:10 1.29 +++ gvpe/src/connection.C 2005/07/07 14:41:51 1.57 @@ -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,18 +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" -extern "C" { -# include "lzf/lzf.h" -} - -#include - #include #include @@ -32,8 +28,6 @@ #include #include -#include "gettext.h" - #include "conf.h" #include "slog.h" #include "device.h" @@ -48,6 +42,12 @@ #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic +#define ULTRA_FAST 1 +#define HLOG 15 +#include "lzf/lzf.h" +#include "lzf/lzf_c.c" +#include "lzf/lzf_d.c" + struct crypto_ctx { EVP_CIPHER_CTX cctx; @@ -95,44 +95,44 @@ void cleaner_cb (time_watcher &w); time_watcher 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 > 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.at < NOW) + cleaner.start (NOW + RSA_TTL); - 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 = 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.at < NOW) + cleaner.start (NOW + RSA_TTL); + } rsa_cache () - : cleaner (this, &rsa_cache::cleaner_cb) - { } + : cleaner (this, &rsa_cache::cleaner_cb) + { } } rsa_cache; @@ -202,10 +202,10 @@ // but low on resources. struct net_rate_limiter : list { - static const double ALPHA = 1. - 1. / 600.; // allow bursts - static const double CUTOFF = 10.; // one event every CUTOFF seconds - static const double EXPIRE = CUTOFF * 30.; // expire entries after this time - static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value +# define NRL_ALPHA (1. - 1. / 600.) // allow bursts +# define NRL_CUTOFF 10. // one event every CUTOFF seconds +# define NRL_EXPIRE (NRL_CUTOFF * 30.) // expire entries after this time +# define NRL_MAXDIF (NRL_CUTOFF * (1. / (1. - NRL_ALPHA))) // maximum diff /count value bool can (const sockinfo &si) { return can((u32)si.host); } bool can (u32 host); @@ -220,7 +220,7 @@ for (i = begin (); i != end (); ) if (i->host == host) break; - else if (i->last < NOW - EXPIRE) + else if (i->last < NOW - NRL_EXPIRE) i = erase (i); else i++; @@ -231,7 +231,7 @@ ri.host = host; ri.pcnt = 1.; - ri.diff = MAXDIF; + ri.diff = NRL_MAXDIF; ri.last = NOW; push_front (ri); @@ -243,19 +243,19 @@ net_rateinfo ri (*i); erase (i); - ri.pcnt = ri.pcnt * ALPHA; - ri.diff = ri.diff * ALPHA + (NOW - ri.last); + ri.pcnt = ri.pcnt * NRL_ALPHA; + ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); ri.last = NOW; double dif = ri.diff / ri.pcnt; - bool send = dif > CUTOFF; + bool send = dif > NRL_CUTOFF; - if (dif > MAXDIF) + if (dif > NRL_MAXDIF) { ri.pcnt = 1.; - ri.diff = MAXDIF; + ri.diff = NRL_MAXDIF; } else if (send) ri.pcnt++; @@ -335,17 +335,20 @@ #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; - u32 cl; - cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); - if (cl) + if (conn->features & ENABLE_COMPRESSION) { - type = PT_DATA_COMPRESSED; - d = cdata; - l = cl + 2; + u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); + + if (cl) + { + type = PT_DATA_COMPRESSED; + d = cdata; + l = cl + 2; - d[0] = cl >> 8; - d[1] = cl; + d[0] = cl >> 8; + d[1] = cl; + } } #endif @@ -448,17 +451,23 @@ // field comes before this data, so peers with other // hmacs simply will not work. u8 prot_major, prot_minor, randsize, hmaclen; - u8 flags, challengelen, pad2, pad3; + u8 flags, challengelen, features, pad3; u32 cipher_nid, digest_nid, hmac_nid; - const u8 curflags () const - { - return 0x80 - | (ENABLE_COMPRESSION ? 0x01 : 0x00); - } - void setup (ptype type, int dst); bool chk_config () const; + + static u8 get_features () + { + u8 f = 0; +#if ENABLE_COMPRESSION + f |= FEATURE_COMPRESSION; +#endif +#if ENABLE_ROHC + f |= FEATURE_ROHC; +#endif + return f; + } }; void config_packet::setup (ptype type, int dst) @@ -467,8 +476,9 @@ prot_minor = PROTOCOL_MINOR; randsize = RAND_SIZE; hmaclen = HMACLENGTH; - flags = curflags (); + flags = 0; challengelen = sizeof (rsachallenge); + features = get_features (); cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); digest_nid = htonl (EVP_MD_type (RSA_HASH)); @@ -486,8 +496,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); - else if (flags != curflags ()) - slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ()); 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))) @@ -577,7 +585,9 @@ { connectmode = conf->connectmode; - rekey.start (NOW + ::conf.rekey); + // make sure rekeying timeouts are slightly asymmetric + rekey.start (NOW + ::conf.rekey + + (conf->id > THISNODE->id ? 10 : 0)); keepalive.start (NOW + ::conf.keepalive); // send queued packets @@ -613,6 +623,7 @@ // 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; si.set (conf, protocol); } @@ -627,8 +638,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 @@ -728,21 +739,24 @@ && connectmode != conf_node::C_DISABLED && NOW > w.at) { - double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; + w.at = TSTAMP_MAX; // first disable this watcher in case of recursion - if (retry_int < 3600 * 8) - retry_cnt++; - - w.start (NOW + retry_int); + double retry_int = double (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); else { const sockinfo &dsi = forward_si (si); + slow = slow || (dsi.prot & PROT_SLOW); + if (dsi.valid () && auth_rate_limiter.can (dsi)) { if (retry_cnt < 4) @@ -751,6 +765,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 (NOW + retry_int); } } @@ -763,13 +786,17 @@ 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; + si.host = 0; last_activity = 0; retry_cnt = 0; @@ -821,7 +848,7 @@ send_data_packet (pkt); else { - if (!broadcast)//DDDD + if (!broadcast) data_queue.put (new tap_packet (*pkt)); establish_connection (); @@ -917,6 +944,7 @@ oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; conf->protocols = p->protocols; + features = p->features & config_packet::get_features (); send_auth_response (rsi, p->id, k); @@ -990,7 +1018,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; } @@ -1034,17 +1063,16 @@ 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"), conf->nodename, (const char *)si, (const char *)rsi); } - - delete d; - - break; } + + delete d; + break; } } @@ -1056,22 +1084,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]\n", + 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; @@ -1079,23 +1113,28 @@ case vpn_packet::PT_CONNECT_INFO: if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) { - connect_info_packet *p = (connect_info_packet *) pkt; + 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 - - 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; @@ -1140,9 +1179,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); @@ -1153,31 +1202,49 @@ const char *connection::script_node_up () { - script_node (); + script_init_connect_env (); putenv ("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 ("STATE=down"); - return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_node_down ? ::conf.script_node_down : "node-down"); + + return filename; } -connection::connection(struct vpn *vpn_) -: vpn(vpn_) +connection::connection (struct vpn *vpn, conf_node *conf) +: vpn(vpn), conf(conf) , rekey (this, &connection::rekey_cb) , keepalive (this, &connection::keepalive_cb) , establish_connection (this, &connection::establish_connection_cb) +#if ENABLE_DNS +, dns (0) +#endif { octx = ictx = 0; retry_cnt = 0; + if (!conf->protocols) // make sure some protocol is enabled + conf->protocols = PROT_UDPv4; + connectmode = conf_node::C_ALWAYS; // initial setting reset_connection (); }