--- gvpe/src/connection.C 2003/04/13 00:35:46 1.11 +++ gvpe/src/connection.C 2003/10/14 03:22:09 1.19 @@ -37,6 +37,23 @@ #include "vpn.h" #include "connection.h" +#include +#ifdef HAVE_NETINET_IN_H +# include +#endif +#include +#include +#ifdef HAVE_NETINET_IN_SYSTM_H +# include +#endif +#ifdef HAVE_NETINET_IP_H +# include +#endif + +#ifndef IPTOS_TOS_MASK +# define IPTOS_TOS_MASK (IPTOS_LOWDELAY | IPTOS_THROUGHPUT | IPTOS_RELIABILITY | IPTOS_MINCOST) +#endif + #if !HAVE_RAND_PSEUDO_BYTES # define RAND_pseudo_bytes RAND_bytes #endif @@ -149,7 +166,7 @@ ////////////////////////////////////////////////////////////////////////////// -void pkt_queue::put (tap_packet *p) +void pkt_queue::put (net_packet *p) { if (queue[i]) { @@ -162,9 +179,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 +216,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 +494,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 (%d <=> %d)"), prot_major, PROTOCOL_MAJOR); + else if (randsize != RAND_SIZE) + slog (L_WARN, _("rand size mismatch (%d <=> %d)"), randsize, RAND_SIZE); + else if (hmaclen != HMACLENGTH) + slog (L_WARN, _("hmac length mismatch (%d <=> %d)"), hmaclen, HMACLENGTH); + else if (flags != curflags ()) + slog (L_WARN, _("flag mismatch (%x <=> %x)"), flags, curflags ()); + else if (challengelen != sizeof (rsachallenge)) + slog (L_WARN, _("challenge length mismatch (%d <=> %d)"), challengelen, sizeof (rsachallenge)); + else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) + slog (L_WARN, _("cipher mismatch (%x <=> %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); + else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) + slog (L_WARN, _("digest mismatch (%x <=> %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); + else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) + slog (L_WARN, _("hmac mismatch (%x <=> %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); + else + return true; + + return false; } struct auth_req_packet : config_packet @@ -567,11 +596,19 @@ // 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 { @@ -785,6 +822,7 @@ vpndata_packet *p = new vpndata_packet; int tos = 0; + // I am not hilarious about peeking into packets, but so be it. if (conf->inherit_tos && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 @@ -807,7 +845,7 @@ else { if (!broadcast)//DDDD - queue.put (new tap_packet (*pkt)); + data_queue.put (new tap_packet (*pkt)); establish_connection (); } @@ -818,7 +856,11 @@ if (ictx && octx) send_vpn_packet (pkt, si, tos); else - establish_connection (); + { + vpn_queue.put (new vpn_packet (*pkt)); + + establish_connection (); + } } void @@ -887,8 +929,8 @@ 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); + 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 { delete octx; @@ -905,6 +947,9 @@ break; } } + else + slog (L_WARN, _("%s(%s): protocol mismatch"), + conf->nodename, (const char *)rsi); send_reset (rsi); } @@ -927,16 +972,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 an synchronization error"), + conf->nodename, (const char *)rsi); + break; + } else { rsaresponse h; @@ -1014,7 +1065,7 @@ 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"), @@ -1101,6 +1152,10 @@ send_ping (si); w.at = 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.at = last_activity + ::conf.keepalive + 10; else reset_connection (); }