--- gvpe/src/connection.C 2003/04/06 04:17:36 1.8 +++ gvpe/src/connection.C 2019/04/01 03:10:26 1.116 @@ -1,194 +1,289 @@ /* connection.C -- manage a single connection + Copyright (C) 2003-2008,2010,2011,2013,2016 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" -extern "C" { -# include "lzf/lzf.h" -} - #include +#include +#include +#include #include #include #include #include -#include "gettext.h" - #include "conf.h" #include "slog.h" +#include "crypto.h" #include "device.h" #include "vpn.h" #include "connection.h" +#include "hkdf.h" -#if !HAVE_RAND_PSEUDO_BYTES -# define RAND_pseudo_bytes RAND_bytes -#endif +#include "netcompat.h" -#define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic +#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic -struct crypto_ctx +#define LZF_STATE_ARG 1 +#define ULTRA_FAST 1 +#define HLOG 15 +#define INIT_HTAB 0 +#include "lzf/lzf_c.c" +#include "lzf/lzf_d.c" + +////////////////////////////////////////////////////////////////////////////// + +static std::queue< std::pair > rs_queue; +static ev::child rs_child_ev; + +namespace { - EVP_CIPHER_CTX cctx; - HMAC_CTX hctx; + void // c++ requires external linkage here, apparently :( + rs_child_cb (ev::child &w, int revents) + { + w.stop (); - crypto_ctx (const rsachallenge &challenge, int enc); - ~crypto_ctx (); + 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 (); + } }; -crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) +// despite the fancy name, this is quite a hack +static void +run_script_queued (run_script_cb *cb, const char *warnmsg) { - EVP_CIPHER_CTX_init (&cctx); - 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); -} + rs_queue.push (std::make_pair (cb, warnmsg)); -crypto_ctx::~crypto_ctx () -{ - EVP_CIPHER_CTX_cleanup (&cctx); - HMAC_CTX_cleanup (&hctx); + if (!rs_child_ev.is_active ()) + { + rs_child_ev.set (); + rs_child_ev (); + } } -static void -rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) +////////////////////////////////////////////////////////////////////////////// + +struct crypto_ctx { - EVP_MD_CTX ctx; + cipher cctx; + hmac hctx; - 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); - EVP_MD_CTX_cleanup (&ctx); -} - -struct rsa_entry { - tstamp expire; - rsaid id; - rsachallenge chg; + crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc); + ~crypto_ctx (); }; -struct rsa_cache : list +crypto_ctx::crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc) { - 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); + ecdh_key s; - erase (i); - return true; - } - } + curve25519_combine (a, b, s); - if (cleaner.at < NOW) - cleaner.start (NOW + RSA_TTL); + { + u8 mac_key[MAC_KEYSIZE]; + static const unsigned char mac_info[] = "gvpe mac key"; - return false; - } + hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); + kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key)); + kdf.extract (s, sizeof (s)); + kdf.extract_done (HKDF_PRF_HASH ()); + kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); - void gen (rsaid &id, rsachallenge &chg) - { - rsa_entry e; + hctx.init (mac_key, MAC_KEYSIZE, MAC_DIGEST ()); + } - RAND_bytes ((unsigned char *)&id, sizeof id); - RAND_bytes ((unsigned char *)&chg, sizeof chg); + { + u8 cipher_key[CIPHER_KEYSIZE]; + static const unsigned char cipher_info[] = "gvpe cipher key"; - e.expire = NOW + RSA_TTL; - e.id = id; - memcpy (&e.chg, &chg, sizeof chg); + hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); + kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key)); + kdf.extract (s, sizeof (s)); + kdf.extract_done (HKDF_PRF_HASH ()); + kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); - push_back (e); + EVP_CIPHER_CTX_init (cctx); + require (EVP_CipherInit_ex (cctx, CIPHER (), 0, cipher_key, 0, enc)); + } +} - if (cleaner.at < NOW) - cleaner.start (NOW + RSA_TTL); - } +crypto_ctx::~crypto_ctx () +{ + require (EVP_CIPHER_CTX_cleanup (cctx)); +} - rsa_cache () - : cleaner (this, &rsa_cache::cleaner_cb) - { } +static inline void +auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr) +{ + if (RSA_public_encrypt (sizeof (auth.rsa), + (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa, + key, RSA_PKCS1_OAEP_PADDING) < 0) + fatal ("RSA_public_encrypt error"); -} rsa_cache; + memcpy (&encr.ecdh, &auth.ecdh, sizeof (encr.ecdh)); +} -void rsa_cache::cleaner_cb (time_watcher &w) +static inline bool +auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth) { - if (empty ()) - w.at = TSTAMP_CANCEL; - else + u8 rsa_decrypt[RSA_KEYLEN]; + + if (RSA_private_decrypt (sizeof (encr.rsa), + (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt, + key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa)) + return 0; + + memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa)); + memcpy (&auth.ecdh, &encr.ecdh, sizeof (auth.ecdh)); + + return 1; +} + +static void +auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) +{ + hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt + kdf.extract (&auth.rsa, sizeof (auth.rsa)); + kdf.extract_done (); + kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info +} + +void +connection::generate_auth_data () +{ + if (auth_expire < ev_now ()) { - w.at = NOW + RSA_TTL; + // request data + rand_fill (snd_auth.rsa); + curve25519_generate (snd_ecdh_a, snd_auth.ecdh); - for (iterator i = begin (); i != end (); ) - if (i->expire <= NOW) - i = erase (i); - else - ++i; + // eventual response data + curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); } + + // every use prolongs the expiry + auth_expire = ev_now () + AUTH_TTL; } ////////////////////////////////////////////////////////////////////////////// -void pkt_queue::put (tap_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); } -tap_packet *pkt_queue::get () +pkt_queue::~pkt_queue () { - tap_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; @@ -199,24 +294,26 @@ // 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); }; -net_rate_limiter auth_rate_limiter, reset_rate_limiter; +static net_rate_limiter auth_rate_limiter, reset_rate_limiter; -bool net_rate_limiter::can (u32 host) +bool +net_rate_limiter::can (u32 host) { iterator i; for (i = begin (); i != end (); ) if (i->host == host) break; - else if (i->last < NOW - EXPIRE) + else if (i->last < ev_now () - NRL_EXPIRE) i = erase (i); else i++; @@ -227,8 +324,8 @@ ri.host = host; ri.pcnt = 1.; - ri.diff = CUTOFF * (1. / (1. - ALPHA)); - ri.last = NOW; + ri.diff = NRL_MAXDIF; + ri.last = ev_now (); push_front (ri); @@ -239,14 +336,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 + (ev_now () - ri.last); - ri.last = NOW; + ri.last = ev_now (); - bool send = ri.diff / ri.pcnt > CUTOFF; + double dif = ri.diff / ri.pcnt; - if (send) + bool send = dif > NRL_CUTOFF; + + if (dif > NRL_MAXDIF) + { + ri.pcnt = 1.; + ri.diff = NRL_MAXDIF; + } + else if (send) ri.pcnt++; push_front (ri); @@ -257,37 +361,32 @@ ///////////////////////////////////////////////////////////////////////////// -unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; - -void hmac_packet::hmac_gen (crypto_ctx *ctx) +void +hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) { - unsigned int xlen; - - HMAC_CTX *hctx = &ctx->hctx; - - HMAC_Init_ex (hctx, 0, 0, 0, 0); - HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), - len - sizeof (hmac_packet)); - HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); + ctx->hctx.init (); + ctx->hctx.add (((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)); + ctx->hctx.digest (hmac_digest); } void hmac_packet::hmac_set (crypto_ctx *ctx) { - hmac_gen (ctx); - + unsigned char hmac_digest[EVP_MAX_MD_SIZE]; + hmac_gen (ctx, hmac_digest); memcpy (hmac, hmac_digest, HMACLENGTH); } bool hmac_packet::hmac_chk (crypto_ctx *ctx) { - hmac_gen (ctx); - - return !memcmp (hmac, hmac_digest, HMACLENGTH); + unsigned char hmac_digest[EVP_MAX_MD_SIZE]; + hmac_gen (ctx, hmac_digest); + return slow_memeq (hmac, hmac_digest, HMACLENGTH); } -void vpn_packet::set_hdr (ptype type_, unsigned int dst) +void +vpn_packet::set_hdr (ptype type_, unsigned int dst) { type = type_; @@ -299,73 +398,86 @@ } #define MAXVPNDATA (MAX_MTU - 6 - 6) -#define DATAHDR (sizeof (u32) + RAND_SIZE) -struct vpndata_packet:vpn_packet - { - u8 data[MAXVPNDATA + DATAHDR]; // seqno +struct vpndata_packet : vpn_packet +{ + u32 ctr; // seqno + u8 data[MAXVPNDATA]; + + void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); + tap_packet *unpack (connection *conn, u32 &seqno); - void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); - tap_packet *unpack (connection *conn, u32 &seqno); private: + const u32 data_hdr_size () const + { + // the distance from beginning of packet to data member + return data - at (0); + } +}; - const u32 data_hdr_size () const - { - return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; - } - }; +// expands packet counter (unlike seqno, in network byte order) to counter mode IV +static unsigned char * +expand_iv (u32 ctr) +{ + static u32 iv[IV_SIZE (CIPHER) / 4]; + + require (sizeof (iv) == 4 * 4); + require (IV_SIZE (CIPHER) % 4 == 0); + + iv[0] = + iv[1] = + iv[2] = ctr; + + // I would reuse ctr here to to avoid potential endianness issues, + // but it seems openssl wraps around. While this would be still ok, + // and I don't even know if its true, let's play safe and initialise + // to 0. + iv[3] = 0; + + return (unsigned char *)iv; +} void vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) { - EVP_CIPHER_CTX *cctx = &conn->octx->cctx; + EVP_CIPHER_CTX *cctx = conn->octx->cctx; int outl = 0, outl2; ptype type = PT_DATA_UNCOMPRESSED; #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; - u32 cl; - cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); - if (cl) + if (conn->features & FEATURE_COMPRESSION) { - type = PT_DATA_COMPRESSED; - d = cdata; - l = cl + 2; + static LZF_STATE lzf_state; + u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7, lzf_state); - d[0] = cl >> 8; - d[1] = cl; - } -#endif - - EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); + if (cl) + { + type = PT_DATA_COMPRESSED; + d = cdata; + l = cl + 2; - struct { -#if RAND_SIZE - u8 rnd[RAND_SIZE]; + d[0] = cl >> 8; + d[1] = cl; + } + } #endif - u32 seqno; - } datahdr; - datahdr.seqno = ntohl (seqno); -#if RAND_SIZE - RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); -#endif + ctr = htonl (seqno); - EVP_EncryptUpdate (cctx, - (unsigned char *) data + outl, &outl2, - (unsigned char *) &datahdr, DATAHDR); - outl += outl2; + require (EVP_EncryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr))); - EVP_EncryptUpdate (cctx, - (unsigned char *) data + outl, &outl2, - (unsigned char *) d, l); + require (EVP_EncryptUpdate (cctx, + (unsigned char *)data + outl, &outl2, + (unsigned char *)d, l)); outl += outl2; - EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2); + // it seems this is a nop for us, but we do it anyways + require (EVP_EncryptFinal_ex (cctx, (unsigned char *)data + outl, &outl2)); outl += outl2; - len = outl + data_hdr_size (); + len = data_hdr_size () + outl; set_hdr (type, dst); @@ -375,13 +487,14 @@ tap_packet * vpndata_packet::unpack (connection *conn, u32 &seqno) { - EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; + EVP_CIPHER_CTX *cctx = conn->ictx->cctx; int outl = 0, outl2; tap_packet *p = new tap_packet; u8 *d; - u32 l = len - data_hdr_size (); - EVP_DecryptInit_ex (cctx, 0, 0, 0, 0); + seqno = ntohl (ctr); + + require (EVP_DecryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr))); #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; @@ -390,33 +503,32 @@ d = cdata; else #endif - d = &(*p)[6 + 6 - DATAHDR]; + d = &(*p)[6 + 6]; - /* this overwrites part of the src mac, but we fix that later */ - EVP_DecryptUpdate (cctx, + // this can overwrite the len/dst/src fields + 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); + // it seems this is a nop for us, but we do it anyways + require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); outl += outl2; - seqno = ntohl (*(u32 *)(d + RAND_SIZE)); - id2mac (dst () ? dst() : THISNODE->id, p->dst); id2mac (src (), p->src); #if ENABLE_COMPRESSION if (type == PT_DATA_COMPRESSED) { - u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; + u32 cl = (d[0] << 8) | d[1]; - p->len = lzf_decompress (d + DATAHDR + 2, cl < MAX_MTU ? cl : 0, + p->len = lzf_decompress (d + 2, cl < MAX_MTU - 2 ? cl : 0, &(*p)[6 + 6], MAX_MTU) + 6 + 6; } else - p->len = outl + (6 + 6 - DATAHDR); + p->len = outl + (6 + 6); #endif return p; @@ -433,65 +545,89 @@ struct config_packet : vpn_packet { - // actually, hmaclen cannot be checked because the hmac - // 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; - u32 cipher_nid, digest_nid, hmac_nid; + u8 serial[SERIAL_SIZE]; + u8 prot_major, prot_minor, randsize; + u8 flags, features, pad6, pad7, pad8; + u32 cipher_nid, mac_nid, auth_nid; + + void setup (ptype type, int dst); + bool chk_config (const conf_node *conf, const sockinfo &rsi) const; - const u8 curflags () const + static u8 get_features () { - return 0x80 - | (ENABLE_COMPRESSION ? 0x01 : 0x00); + u8 f = 0; +#if ENABLE_COMPRESSION + f |= FEATURE_COMPRESSION; +#endif +#if ENABLE_ROHC + f |= FEATURE_ROHC; +#endif +#if ENABLE_BRIDGING + f |= FEATURE_BRIDGING; +#endif + return f; } - - void setup (ptype type, int dst); - bool chk_config () const; }; -void config_packet::setup (ptype type, int dst) +void +config_packet::setup (ptype type, int dst) { prot_major = PROTOCOL_MAJOR; prot_minor = PROTOCOL_MINOR; - 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)); + flags = 0; + features = get_features (); + + strncpy ((char *)serial, conf.serial, sizeof (serial)); + + cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); + mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); + auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); len = sizeof (*this) - sizeof (net_packet); set_hdr (type, dst); } -bool config_packet::chk_config () const +bool +config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) 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, _("%s(%s): major version mismatch (remote %d <=> local %d)"), + conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); + else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) + slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"), + conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); + else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) + slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"), + conf->nodename, (const char *)rsi, ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ())); + else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) + slog (L_WARN, _("%s(%s): auth algo mismatch (remote %x <=> local %x)"), + conf->nodename, (const char *)rsi, ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ())); + else + { + int cmp = memcmp (serial, ::conf.serial, sizeof (serial)); + + if (cmp > 0) + slog (L_WARN, _("%s(%s): remote serial newer than local serial - outdated config?"), + conf->nodename, (const char *)rsi); + else if (cmp == 0) + return true; + } + + return false; } -struct auth_req_packet : config_packet +struct auth_req_packet : config_packet // UNPROTECTED { 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; + auth_encr encr; auth_req_packet (int dst, bool initiate_, u8 protocols_) { config_packet::setup (PT_AUTH_REQ, dst); - strncpy (magic, MAGIC, 8); + memcpy (magic, MAGIC, 8); initiate = !!initiate_; protocols = protocols_; @@ -499,16 +635,13 @@ } }; -struct auth_res_packet : config_packet +struct auth_res_packet : vpn_packet // UNPROTECTED { - rsaid id; - u8 pad1, pad2, pad3; - u8 response_len; // encrypted length - rsaresponse response; + auth_response response; auth_res_packet (int dst) { - config_packet::setup (PT_AUTH_RES, dst); + set_hdr (PT_AUTH_RES, dst); len = sizeof (*this) - sizeof (net_packet); } @@ -548,13 +681,66 @@ ///////////////////////////////////////////////////////////////////////////// void -connection::reset_si () +connection::connection_established (const sockinfo &rsi) { - protocol = best_protocol (THISNODE->protocols & conf->protocols); + if (!have_snd_auth || !have_rcv_auth) + return; + + si = rsi; + protocol = rsi.prot; + + slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), + conf->nodename, (const char *)rsi, + vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded", + PROTOCOL_MAJOR, prot_minor); + + if (::conf.script_node_up) + { + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-up command execution failed, continuing.")); + } + + delete ictx; ictx = new crypto_ctx (rcv_auth, snd_auth, rcv_ecdh_a, rcv_auth.ecdh, 0); + iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); + + delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); + oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; + + // 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); + + hmac_error = 0.; + + keepalive.start (::conf.keepalive); - // mask out protocols we cannot establish - if (!conf->udp_port) protocol &= ~PROT_UDPv4; - if (!conf->tcp_port) protocol &= ~PROT_TCPv4; + // send queued packets + while (tap_packet *p = (tap_packet *)data_queue.get ()) + { + if (p->len) send_data_packet (p); + delete p; + } + + while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) + { + if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); + delete p; + } + + vpn->connection_established (this); +} + +void +connection::reset_si () +{ + 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); } @@ -565,16 +751,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); } @@ -582,12 +768,21 @@ } void +connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) +{ + if (!vpn->send_vpn_packet (pkt, si, tos)) + reset_connection ("packet send error"); +} + +void connection::send_ping (const sockinfo &si, u8 pong) { ping_packet *pkt = new ping_packet; pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); - vpn->send_vpn_packet (pkt, si, IPTOS_LOWDELAY); + + 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; } @@ -600,7 +795,7 @@ config_packet *pkt = new config_packet; pkt->setup (vpn_packet::PT_RESET, conf->id); - vpn->send_vpn_packet (pkt, si, IPTOS_MINCOST); + send_vpn_packet (pkt, si, IPTOS_MINCOST); delete pkt; } @@ -611,37 +806,25 @@ { auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); - 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"); - - slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); - - vpn->send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly + generate_auth_data (); + auth_encrypt (conf->rsa_key, snd_auth, pkt->encr); + 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 delete pkt; } void -connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) +connection::send_auth_response (const sockinfo &si) { auth_res_packet *pkt = new auth_res_packet (conf->id); - pkt->id = id; - - rsa_hash (id, chg, pkt->response); + memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b); + auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac); - pkt->hmac_set (octx); - - slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); - - vpn->send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly + 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 delete pkt; } @@ -649,75 +832,116 @@ void connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) { - slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", - conf->id, rid, (const char *)rsi); + slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename, + vpn->conns[rid - 1]->conf->nodename, (const char *)rsi, + conf->protocols); connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); r->hmac_set (octx); - vpn->send_vpn_packet (r, si); + send_vpn_packet (r, si); 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) - w.at = TSTAMP_CANCEL; - else if (w.at <= NOW) + if (!(ictx && octx) + && conf != THISNODE + && connectmode != conf_node::C_NEVER + && connectmode != conf_node::C_DISABLED + && !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 ("no demand"); + return; + } - if (retry_int < 3600 * 8) - retry_cnt++; + last_establish_attempt = ev_now (); - w.at = 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->connect_request (conf->id); + bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power); + + 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) + // use ping after the first few retries + // TODO: on rekeys, the other node might not interpret ping correctly, + // TODO: as it will still have a valid connection + if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power)) send_auth_request (dsi, true); else send_ping (dsi, 0); } } + + retry_int *= slow ? 4. : 0.9; + + if (retry_int < conf->max_retry) + retry_cnt++; + else + retry_int = conf->max_retry; + + w.start (retry_int); } } void -connection::reset_connection () +connection::reset_connection (const char *reason) { if (ictx && octx) { - slog (L_INFO, _("%s(%s): connection lost"), - conf->nodename, (const char *)si); + slog (L_INFO, _("%s(%s): connection lost (%s)"), + conf->nodename, (const char *)si, reason); 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; - si.host= 0; + si.host = 0; - last_activity = 0; + have_snd_auth = false; + have_rcv_auth = false; + auth_expire = 0.; + + last_activity = 0.; + //last_si_change = 0.; retry_cnt = 0; - rekey.reset (); - keepalive.reset (); - establish_connection.reset (); + rekey.stop (); + keepalive.stop (); + establish_connection.stop (); } void @@ -726,31 +950,29 @@ if (ictx && octx) send_reset (si); - reset_connection (); + reset_connection ("shutdown"); } -void -connection::rekey_cb (time_watcher &w) +// poor-man's rekeying +inline void +connection::rekey_cb (ev::timer &w, int revents) { - w.at = TSTAMP_CANCEL; - - reset_connection (); + reset_connection ("rekeying"); 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 - vpn->send_vpn_packet (p, si, tos); + p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs + send_vpn_packet (p, si, tos); delete p; @@ -759,38 +981,55 @@ } void -connection::inject_data_packet (tap_packet *pkt, bool broadcast) +connection::post_inject_queue () +{ + // force a connection every now and when when packets are sent (max 1/s) + if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary + establish_connection.stop (); + + establish_connection (); +} + +void +connection::inject_data_packet (tap_packet *pkt) { if (ictx && octx) - send_data_packet (pkt, broadcast); + send_data_packet (pkt); else { - if (!broadcast)//DDDD - queue.put (new tap_packet (*pkt)); - - establish_connection (); + data_queue.put (new tap_packet (*pkt)); + post_inject_queue (); } } -void connection::inject_vpn_packet (vpn_packet *pkt, int tos) +void +connection::inject_vpn_packet (vpn_packet *pkt, int tos) { if (ictx && octx) - vpn->send_vpn_packet (pkt, si, tos); + send_vpn_packet (pkt, si, tos); else - establish_connection (); + { + vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); + post_inject_queue (); + } } void connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) { - last_activity = NOW; + last_activity = ev_now (); + + slog (L_NOISE, "%s >> received packet type %d from %d to %d.", + conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); - 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: + 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. @@ -800,38 +1039,50 @@ send_auth_request (rsi, true); } else + // we would love to change the 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); + + // a PONG might mean that the other side doesn't really know + // about our desire for communication. + establish_connection (); break; case vpn_packet::PT_RESET: - { - reset_connection (); + slog (L_TRACE, "%s >> PT_RESET", conf->nodename); - config_packet *p = (config_packet *) pkt; + if (ictx && octx) + { + reset_connection ("remote reset"); + + config_packet *p = (config_packet *) pkt; + + if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) + establish_connection (); + } - if (!p->chk_config ()) - { - slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), - conf->nodename, (const char *)rsi); - connectmode = conf_node::C_DISABLED; - } - else if (connectmode == conf_node::C_ALWAYS) - establish_connection (); - } break; case vpn_packet::PT_AUTH_REQ: if (auth_rate_limiter.can (rsi)) { - auth_req_packet *p = (auth_req_packet *) pkt; + 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,p%02x,f%02x)", + conf->nodename, p->initiate ? "initiate" : "reply", + p->protocols, p->features); - if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) + if (memcmp (p->magic, MAGIC, 8)) + { + slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"), + conf->nodename, (const char *)rsi); + } + else if (p->chk_config (conf, rsi)) { if (p->prot_minor != PROTOCOL_MINOR) slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), @@ -839,35 +1090,39 @@ PROTOCOL_MINOR, conf->nodename, p->prot_minor); if (p->initiate) - send_auth_request (rsi, false); + { + send_auth_request (rsi, false); - rsachallenge k; + if (ictx && octx) + reset_connection ("reconnect"); + } - 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); + auth_data auth; + + if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) + { + 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)); + } else { - retry_cnt = 0; - establish_connection.start (NOW + 8); //? ;) - keepalive.reset (); - rekey.reset (); - - delete ictx; - ictx = 0; + bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth); - delete octx; + rcv_auth = auth; + have_rcv_auth = true; - octx = new crypto_ctx (k, 1); - oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; + send_auth_response (rsi); - conf->protocols = p->protocols; - send_auth_response (rsi, p->id, k); + if (chg) + { + conf->protocols = p->protocols; + features = p->features & config_packet::get_features (); - break; + connection_established (rsi); + } } + + break; } send_reset (rsi); @@ -877,79 +1132,26 @@ case vpn_packet::PT_AUTH_RES: { - auth_res_packet *p = (auth_res_packet *) pkt; - - slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); - - if (p->chk_config ()) - { - if (p->prot_minor != PROTOCOL_MINOR) - slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), - conf->nodename, (const char *)rsi, - PROTOCOL_MINOR, conf->nodename, p->prot_minor); - - rsachallenge chg; - - if (!rsa_cache.find (p->id, chg)) - slog (L_ERR, _("%s(%s): unrequested auth response"), - conf->nodename, (const char *)rsi); - 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); - else - { - rsaresponse h; - - rsa_hash (p->id, chg, h); - - if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) - { - prot_minor = p->prot_minor; - - delete ictx; ictx = cctx; - - iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid - - si = rsi; - protocol = rsi.prot; - - rekey.start (NOW + ::conf.rekey); - keepalive.start (NOW + ::conf.keepalive); - - // send queued packets - while (tap_packet *p = queue.get ()) - { - send_data_packet (p); - delete p; - } + auth_res_packet *p = (auth_res_packet *)pkt; - connectmode = conf->connectmode; + slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); - slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), - conf->nodename, (const char *)rsi, - p->prot_major, p->prot_minor); + auth_mac local_mac; + auth_hash (snd_auth, p->response.ecdh, local_mac); - if (::conf.script_node_up) - run_script (run_script_cb (this, &connection::script_node_up), false); - - break; - } - else - slog (L_ERR, _("%s(%s): sent and received challenge do not match"), - conf->nodename, (const char *)rsi); - } + if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac)) + { + slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), + conf->nodename, (const char *)rsi); + } + else if (!have_snd_auth) + { + memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b); - delete cctx; - } + have_snd_auth = true; + connection_established (rsi); } } - - send_reset (rsi); break; case vpn_packet::PT_DATA_COMPRESSED: @@ -964,38 +1166,78 @@ { vpndata_packet *p = (vpndata_packet *)pkt; - if (rsi == si) + if (!p->hmac_chk (ictx)) { - 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); + // rekeying often creates temporary hmac auth floods + // we assume they don't take longer than a few seconds normally, + // and suppress messages and resets during that time. + //TODO: should be done per source address + if (!hmac_error) + { + hmac_error = ev_now () + 3; + break; + } + else if (hmac_error >= ev_now ()) + break; // silently suppress else { - u32 seqno; - tap_packet *d = p->unpack (this, seqno); - - if (iseqno.recv_ok (seqno)) - { - 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; + 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); + // reset + } + } + else + { + u32 seqno; + tap_packet *d = p->unpack (this, seqno); + int seqclass = iseqno.seqno_classify (seqno); - if (c->conf != THISNODE && c->conf != conf) - c->inject_data_packet (d); - } + hmac_error = 0; - delete d; + if (seqclass == 0) // ok + { + vpn->tap->send (d); - break; + if (si != rsi) + { + // fast re-sync on source address changes, useful especially for tcp/ip + //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; + + if (::conf.script_node_change) + { + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-change command execution failed, continuing.")); + } + + // } + //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; } - else - slog (L_ERR, _("received data packet from unknown source %s"), (const char *)rsi); } send_reset (rsi); @@ -1004,22 +1246,32 @@ case vpn_packet::PT_CONNECT_REQ: if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) { - connect_req_packet *p = (connect_req_packet *) pkt; + 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, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]", + conf->nodename, vpn->conns[p->id - 1]->conf->nodename, + p->protocols, + 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 + slog (L_WARN, + _("received authenticated connection request from unknown node #%d, config file mismatch?"), + p->id); } break; @@ -1027,23 +1279,38 @@ 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 - - connection *c = vpn->conns[p->id - 1]; + connect_info_packet *p = (connect_info_packet *)pkt; - 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); + if (p->id > 0 && p->id <= vpn->conns.size ()) + { + 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); + + slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,protocols=%02x,protocol=%02x,upgradable=%02x) [%d]", + conf->nodename, + vpn->conns[p->id - 1]->conf->nodename, + (const char *)p->si, + p->protocols, + protocol, + p->si.supported_protocols (c->conf), + !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_INFO, "connect info for %s received (%s), but still unable to contact.", + vpn->conns[p->id - 1]->conf->nodename, + (const char *)p->si); + } + else + slog (L_WARN, + _("received authenticated connection request from unknown node #%d, config file mismatch?"), + p->id); } break; @@ -1054,76 +1321,140 @@ } } -void connection::keepalive_cb (time_watcher &w) +inline void +connection::keepalive_cb (ev::timer &w, int revents) { - if (NOW >= last_activity + ::conf.keepalive + 30) + ev_tstamp when = last_activity + ::conf.keepalive - ev::now (); + + if (when >= 0) + w.start (when); + else if (when < -15) { - reset_connection (); + reset_connection ("keepalive overdue"); establish_connection (); } - else if (NOW < last_activity + ::conf.keepalive) - w.at = last_activity + ::conf.keepalive; else if (conf->connectmode != conf_node::C_ONDEMAND || THISNODE->connectmode != conf_node::C_ONDEMAND) { + w.start (3); send_ping (si); - w.at = NOW + 5; } + else if (when >= -10) + // hold ondemand connections implicitly a few seconds longer + // should delete octx, though, or something like that ;) + w.start (when + 10); else - reset_connection (); + reset_connection ("keepalive timeout"); } -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); + connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols); - slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); + slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)", + conf->nodename, vpn->conns[id - 1]->conf->nodename, + THISNODE->protocols); p->hmac_set (octx); - vpn->send_vpn_packet (p, si); + send_vpn_packet (p, si); 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); - asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); - asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); - asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); + asprintf (&env, "DESTID=%d", conf->id); putenv (env); + asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env); + asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); + asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); + 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 ("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 () +inline const char * +connection::script_node_change () { - script_node (); + script_init_connect_env (); + + putenv ((char *)"STATE=change"); - putenv ("STATE=down"); + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_node_change ? ::conf.script_node_change : "node-change"); - 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) +inline const char * +connection::script_node_down () { + script_init_connect_env (); + + putenv ((char *)"STATE=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, 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 - reset_connection (); + 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 ("startup"); } connection::~connection () @@ -1131,7 +1462,8 @@ shutdown (); } -void connection_init () +void +connection_init () { auth_rate_limiter.clear (); reset_rate_limiter.clear ();