--- gvpe/src/connection.C 2003/04/13 00:35:46 1.11 +++ gvpe/src/connection.C 2004/01/25 21:47:14 1.27 @@ -1,5 +1,6 @@ /* connection.C -- manage a single connection + Copyright (C) 2003-2004 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 @@ -22,6 +23,8 @@ # include "lzf/lzf.h" } +#include + #include #include @@ -37,6 +40,8 @@ #include "vpn.h" #include "connection.h" +#include "netcompat.h" + #if !HAVE_RAND_PSEUDO_BYTES # define RAND_pseudo_bytes RAND_bytes #endif @@ -133,11 +138,9 @@ 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 +152,7 @@ ////////////////////////////////////////////////////////////////////////////// -void pkt_queue::put (tap_packet *p) +void pkt_queue::put (net_packet *p) { if (queue[i]) { @@ -162,9 +165,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,12 +202,12 @@ // but low on resources. struct net_rate_limiter : list { - static const double ALPHA = 1. - 1. / 180.; // allow bursts + 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 - bool can (const sockinfo &si) { return can((u32)si.host); } + bool can (const sockinfo &si) { return can((u32)si.host); } bool can (u32 host); }; @@ -477,14 +480,26 @@ 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 (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))) + 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 @@ -567,18 +582,26 @@ // send queued packets if (ictx && octx) - while (tap_packet *p = queue.get ()) - { - send_data_packet (p); - delete p; - } + { + 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.reset (); - rekey.reset (); + keepalive.stop (); + rekey.stop (); } } @@ -654,13 +677,8 @@ 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"); + rsa_encrypt (conf->rsa_key, chg, pkt->encr); slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); @@ -704,18 +722,18 @@ 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; if (retry_int < 3600 * 8) retry_cnt++; - w.at = NOW + retry_int; + w.start (NOW + retry_int); reset_si (); @@ -756,9 +774,9 @@ last_activity = 0; retry_cnt = 0; - rekey.reset (); - keepalive.reset (); - establish_connection.reset (); + rekey.stop (); + keepalive.stop (); + establish_connection.stop (); } void @@ -773,24 +791,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; @@ -800,14 +815,14 @@ } 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 (); } @@ -818,7 +833,11 @@ if (ictx && octx) send_vpn_packet (pkt, si, tos); else - establish_connection (); + { + vpn_queue.put (new vpn_packet (*pkt)); + + establish_connection (); + } } void @@ -884,11 +903,12 @@ 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 { delete octx; @@ -905,6 +925,9 @@ break; } } + else + slog (L_WARN, _("%s(%s): protocol mismatch"), + conf->nodename, (const char *)rsi); send_reset (rsi); } @@ -927,16 +950,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; @@ -992,7 +1021,7 @@ 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"), + "could be an attack, or just corruption or a synchronization error"), conf->nodename, (const char *)rsi); else { @@ -1003,18 +1032,9 @@ { 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); - } - if (si != rsi) { - // fast re-sync on conneciton changes, useful especially for tcp/ip + // fast re-sync on connection changes, useful especially for tcp/ip si = rsi; slog (L_INFO, _("%s(%s): socket address changed to %s"), @@ -1094,13 +1114,17 @@ 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 (); }