--- gvpe/src/connection.C 2003/04/02 21:43:44 1.5 +++ gvpe/src/connection.C 2005/03/23 21:55:39 1.52 @@ -1,7 +1,10 @@ /* connection.C -- manage a single connection + 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. @@ -12,15 +15,13 @@ 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 + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" -extern "C" { -# include "lzf/lzf.h" -} +#include #include @@ -29,20 +30,26 @@ #include #include -#include "gettext.h" - #include "conf.h" #include "slog.h" #include "device.h" #include "vpn.h" #include "connection.h" +#include "netcompat.h" + #if !HAVE_RAND_PSEUDO_BYTES # define RAND_pseudo_bytes RAND_bytes #endif #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; @@ -55,14 +62,14 @@ crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) { EVP_CIPHER_CTX_init (&cctx); - EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); + require (EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc)); HMAC_CTX_init (&hctx); HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); } crypto_ctx::~crypto_ctx () { - EVP_CIPHER_CTX_cleanup (&cctx); + require (EVP_CIPHER_CTX_cleanup (&cctx)); HMAC_CTX_cleanup (&hctx); } @@ -72,10 +79,10 @@ EVP_MD_CTX ctx; EVP_MD_CTX_init (&ctx); - EVP_DigestInit (&ctx, RSA_HASH); - EVP_DigestUpdate(&ctx, &chg, sizeof chg); - EVP_DigestUpdate(&ctx, &id, sizeof id); - EVP_DigestFinal (&ctx, (unsigned char *)&h, 0); + require (EVP_DigestInit (&ctx, RSA_HASH)); + require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); + require (EVP_DigestUpdate(&ctx, &id, sizeof id)); + require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); EVP_MD_CTX_cleanup (&ctx); } @@ -90,54 +97,52 @@ 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; void rsa_cache::cleaner_cb (time_watcher &w) { - if (empty ()) - w.at = TSTAMP_CANCEL; - else + if (!empty ()) { - w.at = NOW + RSA_TTL; + w.start (NOW + RSA_TTL); for (iterator i = begin (); i != end (); ) if (i->expire <= NOW) @@ -149,7 +154,7 @@ ////////////////////////////////////////////////////////////////////////////// -void pkt_queue::put (tap_packet *p) +void pkt_queue::put (net_packet *p) { if (queue[i]) { @@ -162,9 +167,9 @@ i = (i + 1) % QUEUEDEPTH; } -tap_packet *pkt_queue::get () +net_packet *pkt_queue::get () { - tap_packet *p = queue[j]; + net_packet *p = queue[j]; if (p) { @@ -199,11 +204,12 @@ // but low on resources. struct net_rate_limiter : list { - static const double ALPHA = 1. - 1. / 90.; // allow bursts - static const double CUTOFF = 20.; // one event every CUTOFF seconds - static const double EXPIRE = CUTOFF * 30.; // expire entries after this time +# 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 (const sockinfo &si) { return can((u32)si.host); } bool can (u32 host); }; @@ -216,7 +222,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++; @@ -227,7 +233,7 @@ ri.host = host; ri.pcnt = 1.; - ri.diff = CUTOFF * (1. / (1. - ALPHA)); + ri.diff = NRL_MAXDIF; ri.last = NOW; push_front (ri); @@ -239,14 +245,21 @@ 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; - bool send = ri.diff / ri.pcnt > CUTOFF; + double dif = ri.diff / ri.pcnt; + + bool send = dif > NRL_CUTOFF; - if (send) + if (dif > NRL_MAXDIF) + { + ri.pcnt = 1.; + ri.diff = NRL_MAXDIF; + } + else if (send) ri.pcnt++; push_front (ri); @@ -287,9 +300,9 @@ return !memcmp (hmac, hmac_digest, HMACLENGTH); } -void vpn_packet::set_hdr (ptype type, unsigned int dst) +void vpn_packet::set_hdr (ptype type_, unsigned int dst) { - this->type = type; + type = type_; int src = THISNODE->id; @@ -301,7 +314,7 @@ #define MAXVPNDATA (MAX_MTU - 6 - 6) #define DATAHDR (sizeof (u32) + RAND_SIZE) -struct vpndata_packet:vpn_packet +struct vpndata_packet : vpn_packet { u8 data[MAXVPNDATA + DATAHDR]; // seqno @@ -324,21 +337,24 @@ #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 - EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); + require (EVP_EncryptInit_ex (cctx, 0, 0, 0, 0)); struct { #if RAND_SIZE @@ -352,17 +368,17 @@ RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); #endif - EVP_EncryptUpdate (cctx, + require (EVP_EncryptUpdate (cctx, (unsigned char *) data + outl, &outl2, - (unsigned char *) &datahdr, DATAHDR); + (unsigned char *) &datahdr, DATAHDR)); outl += outl2; - EVP_EncryptUpdate (cctx, + require (EVP_EncryptUpdate (cctx, (unsigned char *) data + outl, &outl2, - (unsigned char *) d, l); + (unsigned char *) d, l)); outl += outl2; - EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2); + require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2)); outl += outl2; len = outl + data_hdr_size (); @@ -381,7 +397,7 @@ u8 *d; u32 l = len - data_hdr_size (); - EVP_DecryptInit_ex (cctx, 0, 0, 0, 0); + require (EVP_DecryptInit_ex (cctx, 0, 0, 0, 0)); #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; @@ -393,12 +409,12 @@ d = &(*p)[6 + 6 - DATAHDR]; /* this overwrites part of the src mac, but we fix that later */ - EVP_DecryptUpdate (cctx, + require (EVP_DecryptUpdate (cctx, d, &outl2, - (unsigned char *)&data, len - data_hdr_size ()); + (unsigned char *)&data, len - data_hdr_size ())); outl += outl2; - EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); + require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); outl += outl2; seqno = ntohl (*(u32 *)(d + RAND_SIZE)); @@ -437,17 +453,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) @@ -456,8 +478,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)); @@ -469,21 +492,31 @@ bool config_packet::chk_config () const { - return prot_major == PROTOCOL_MAJOR - && randsize == RAND_SIZE - && hmaclen == HMACLENGTH - && flags == curflags () - && challengelen == sizeof (rsachallenge) - && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) - && digest_nid == htonl (EVP_MD_type (RSA_HASH)) - && hmac_nid == htonl (EVP_MD_type (DIGEST)); + if (prot_major != PROTOCOL_MAJOR) + slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); + else if (randsize != RAND_SIZE) + 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 (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))) + slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); + else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) + slog (L_WARN, _("digest mismatch (remote %x <=> local %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); + else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) + slog (L_WARN, _("hmac mismatch (remote %x <=> local %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); + else + return true; + + return false; } struct auth_req_packet : config_packet { char magic[8]; u8 initiate; // false if this is just an automatic reply - u8 protocols; // supported protocols (will get patches on forward) + u8 protocols; // supported protocols (will be patched on forward) u8 pad2, pad3; rsaid id; rsaencrdata encr; @@ -548,9 +581,82 @@ ///////////////////////////////////////////////////////////////////////////// void -connection::reset_dstaddr () +connection::connection_established () +{ + 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); + + // send queued packets + if (ictx && octx) + { + while (tap_packet *p = (tap_packet *)data_queue.get ()) + { + send_data_packet (p); + delete p; + } + + while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) + { + send_vpn_packet (p, si, IPTOS_RELIABILITY); + delete p; + } + } + } + else + { + retry_cnt = 0; + establish_connection.start (NOW + 5); + keepalive.stop (); + rekey.stop (); + } +} + +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; + + si.set (conf, protocol); +} + +// ensure sockinfo is valid, forward if necessary +const sockinfo & +connection::forward_si (const sockinfo &si) const +{ + if (!si.valid ()) + { + connection *r = vpn->find_router (); + + if (r) + { + 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 + slog (L_DEBUG, _("%s: node unreachable, no common protocol"), + conf->nodename); + } + + return si; +} + +void +connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) { - si.set (conf); + if (!vpn->send_vpn_packet (pkt, si, tos)) + reset_connection (); } void @@ -583,31 +689,15 @@ { auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); - 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 (protocol) - { - rsachallenge chg; - - rsa_cache.gen (pkt->id, chg); - - if (0 > RSA_public_encrypt (sizeof chg, - (unsigned char *)&chg, (unsigned char *)&pkt->encr, - conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) - fatal ("RSA_public_encrypt error"); + rsachallenge chg; + 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, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); - send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly + send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly - delete pkt; - } - else - ; // silently fail + delete pkt; } void @@ -645,32 +735,47 @@ void connection::establish_connection_cb (time_watcher &w) { - if (ictx || conf == THISNODE - || connectmode == conf_node::C_NEVER - || connectmode == conf_node::C_DISABLED) - w.at = TSTAMP_CANCEL; - else if (w.at <= NOW) + if (!ictx + && conf != THISNODE + && connectmode != conf_node::C_NEVER + && 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++; + double retry_int = double (retry_cnt & 3 + ? (retry_cnt & 3) + 1 + : 1 << (retry_cnt >> 2)); - w.at = NOW + retry_int; + reset_si (); - if (conf->hostname) + bool slow = si.prot & PROT_SLOW; + + if (si.prot && !si.host) + vpn->send_connect_request (conf->id); + else { - reset_dstaddr (); - if (si.host && auth_rate_limiter.can (si)) - { - if (retry_cnt < 4) - send_auth_request (si, true); - else - send_ping (si, 0); - } + const sockinfo &dsi = forward_si (si); + + slow = slow || (dsi.prot & PROT_SLOW); + + if (dsi.valid () && auth_rate_limiter.can (dsi)) + { + if (retry_cnt < 4) + send_auth_request (dsi, true); + else + send_ping (dsi, 0); + } } + + retry_int *= slow ? 8. : 0.7; + + if (retry_int < conf->max_retry) + retry_cnt++; else - vpn->connect_request (conf->id); + retry_int = conf->max_retry; + + w.start (NOW + retry_int); } } @@ -683,20 +788,24 @@ 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; - rekey.reset (); - keepalive.reset (); - establish_connection.reset (); + rekey.stop (); + keepalive.stop (); + establish_connection.stop (); } void @@ -711,24 +820,21 @@ void connection::rekey_cb (time_watcher &w) { - w.at = TSTAMP_CANCEL; - reset_connection (); establish_connection (); } void -connection::send_data_packet (tap_packet *pkt, bool broadcast) +connection::send_data_packet (tap_packet *pkt) { vpndata_packet *p = new vpndata_packet; int tos = 0; - if (conf->inherit_tos - && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP - && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 + // I am not hilarious about peeking into packets, but so be it. + if (conf->inherit_tos && pkt->is_ipv4 ()) tos = (*pkt)[15] & IPTOS_TOS_MASK; - p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs + p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs send_vpn_packet (p, si, tos); delete p; @@ -738,14 +844,26 @@ } void -connection::inject_data_packet (tap_packet *pkt, bool broadcast) +connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) { if (ictx && octx) - send_data_packet (pkt, broadcast); + send_data_packet (pkt); else { if (!broadcast)//DDDD - queue.put (new tap_packet (*pkt)); + data_queue.put (new tap_packet (*pkt)); + + establish_connection (); + } +} + +void connection::inject_vpn_packet (vpn_packet *pkt, int tos) +{ + if (ictx && octx) + send_vpn_packet (pkt, si, tos); + else + { + vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); establish_connection (); } @@ -814,32 +932,32 @@ rsachallenge k; - if (0 > RSA_private_decrypt (sizeof (p->encr), - (unsigned char *)&p->encr, (unsigned char *)&k, - ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) - slog (L_ERR, _("%s(%s): challenge illegal or corrupted"), - conf->nodename, (const char *)rsi); + if (!rsa_decrypt (::conf.rsa_key, p->encr, k)) + { + slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), + conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); + break; + } else { - retry_cnt = 0; - establish_connection.set (NOW + 8); //? ;) - keepalive.reset (); - rekey.reset (); - - delete ictx; - ictx = 0; - delete octx; octx = new crypto_ctx (k, 1); 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); + connection_established (); + break; } } + else + slog (L_WARN, _("%s(%s): protocol mismatch"), + conf->nodename, (const char *)rsi); send_reset (rsi); } @@ -862,16 +980,22 @@ rsachallenge chg; if (!rsa_cache.find (p->id, chg)) - slog (L_ERR, _("%s(%s): unrequested auth response"), - conf->nodename, (const char *)rsi); + { + slog (L_ERR, _("%s(%s): unrequested auth response ignored"), + conf->nodename, (const char *)rsi); + break; + } else { crypto_ctx *cctx = new crypto_ctx (chg, 0); 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 an synchronization error"), - conf->nodename, (const char *)rsi); + { + 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"), + conf->nodename, (const char *)rsi); + break; + } else { rsaresponse h; @@ -887,26 +1011,17 @@ iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid si = rsi; + protocol = rsi.prot; - rekey.set (NOW + ::conf.rekey); - keepalive.set (NOW + ::conf.keepalive); - - // send queued packets - while (tap_packet *p = queue.get ()) - { - send_data_packet (p); - delete p; - } + connection_established (); - connectmode = conf->connectmode; - - slog (L_INFO, _("%s(%s): %s connection established, protocol version %d.%d"), + slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), conf->nodename, (const char *)rsi, - strprotocol (protocol), 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; } @@ -935,38 +1050,32 @@ { vpndata_packet *p = (vpndata_packet *)pkt; - if (rsi == si) + 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"), + conf->nodename, (const char *)rsi); + else { - 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 an synchronization error"), - conf->nodename, (const char *)rsi); - else + u32 seqno; + tap_packet *d = p->unpack (this, seqno); + + if (iseqno.recv_ok (seqno)) { - u32 seqno; - tap_packet *d = p->unpack (this, seqno); + vpn->tap->send (d); - if (iseqno.recv_ok (seqno)) + if (si != rsi) { - vpn->tap->send (d); - - if (p->dst () == 0) // re-broadcast - for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) - { - connection *c = *i; - - if (c->conf != THISNODE && c->conf != conf) - c->inject_data_packet (d); - } + // fast re-sync on connection changes, useful especially for tcp/ip + si = rsi; - delete d; - - break; + slog (L_INFO, _("%s(%s): socket address changed to %s"), + conf->nodename, (const char *)si, (const char *)rsi); } } + + delete d; + break; } - else - slog (L_ERR, _("received data packet from unknown source %s"), (const char *)rsi); } send_reset (rsi); @@ -978,8 +1087,8 @@ 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 - conf->protocols = p->protocols; 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); @@ -991,6 +1100,8 @@ c->send_connect_info (conf->id, si, conf->protocols); send_connect_info (c->conf->id, c->si, c->conf->protocols); } + else + c->establish_connection (); } break; @@ -998,16 +1109,24 @@ 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 - conf->protocols = p->protocols; - connection *c = vpn->conns[p->id - 1]; + if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything + { + connection *c = vpn->conns[p->id - 1]; - slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", - conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); + 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->send_auth_request (p->si, true); + 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); + + if (dsi.valid ()) + c->send_auth_request (dsi, true); + } } break; @@ -1026,18 +1145,22 @@ establish_connection (); } else if (NOW < last_activity + ::conf.keepalive) - w.at = last_activity + ::conf.keepalive; + w.start (last_activity + ::conf.keepalive); else if (conf->connectmode != conf_node::C_ONDEMAND || THISNODE->connectmode != conf_node::C_ONDEMAND) { send_ping (si); - w.at = NOW + 5; + w.start (NOW + 5); } + else if (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); else reset_connection (); } -void connection::connect_request (int id) +void connection::send_connect_request (int id) { connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); @@ -1048,9 +1171,9 @@ delete p; } -void connection::script_node () +void connection::script_init_env () { - vpn->script_if_up (); + vpn->script_init_env (); char *env; asprintf (&env, "DESTID=%d", conf->id); putenv (env); @@ -1061,53 +1184,45 @@ const char *connection::script_node_up () { - script_node (); + script_init_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"); } const char *connection::script_node_down () { - script_node (); + script_init_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"); } -// send a vpn packet out to other hosts -void -connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) -{ - switch (protocol) - { - case PROT_IPv4: - vpn->send_ipv4_packet (pkt, si, tos); - break; - - case PROT_UDPv4: - vpn->send_udpv4_packet (pkt, si, tos); - break; - -#if ENABLE_TCP - case PROT_TCPv4: - vpn->send_tcpv4_packet (pkt, si, tos); - break; -#endif - } -} - -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 (); }