--- gvpe/src/connection.C 2008/08/10 01:34:36 1.73 +++ gvpe/src/connection.C 2011/02/08 23:13:48 1.91 @@ -1,6 +1,6 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2008 Marc Lehmann + Copyright (C) 2003-2008,2010 Marc Lehmann This file is part of GVPE. @@ -52,7 +52,8 @@ # define RAND_pseudo_bytes RAND_bytes #endif -#define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic +#define MAGIC_OLD "vped\xbd\xc6\xdb\x82" // 8 bytes of magic (still used in the protocol) +#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic (understood but not generated) #define ULTRA_FAST 1 #define HLOG 15 @@ -65,26 +66,29 @@ static std::queue< std::pair > rs_queue; static ev::child rs_child_ev; -void // c++ requires external linkage here, apparently :( -rs_child_cb (ev::child &w, int revents) +namespace { - w.stop (); + void // c++ requires external linkage here, apparently :( + rs_child_cb (ev::child &w, int revents) + { + w.stop (); - if (rs_queue.empty ()) - return; + 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); + 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 (); -} + delete rs_queue.front ().first; + rs_queue.pop (); + } +}; // despite the fancy name, this is quite a hack static void @@ -192,7 +196,8 @@ } rsa_cache; -void rsa_cache::cleaner_cb (ev::timer &w, int revents) +void +rsa_cache::cleaner_cb (ev::timer &w, int revents) { if (empty ()) w.stop (); @@ -227,7 +232,8 @@ delete [] queue; } -void pkt_queue::expire_cb (ev::timer &w, int revents) +void +pkt_queue::expire_cb (ev::timer &w, int revents) { ev_tstamp expire = ev_now () - max_ttl; @@ -248,7 +254,8 @@ } } -void pkt_queue::put (net_packet *p) +void +pkt_queue::put (net_packet *p) { ev_tstamp now = ev_now (); @@ -267,7 +274,8 @@ i = ni; } -net_packet *pkt_queue::get () +net_packet * +pkt_queue::get () { if (empty ()) return 0; @@ -301,9 +309,10 @@ 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; @@ -360,7 +369,8 @@ 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) { unsigned int xlen; @@ -388,7 +398,8 @@ 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) { type = type_; @@ -403,18 +414,18 @@ #define DATAHDR (sizeof (u32) + RAND_SIZE) struct vpndata_packet : vpn_packet - { - u8 data[MAXVPNDATA + DATAHDR]; // seqno +{ + u8 data[MAXVPNDATA + DATAHDR]; // seqno - void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); - tap_packet *unpack (connection *conn, u32 &seqno); -private: + void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); + tap_packet *unpack (connection *conn, u32 &seqno); - const u32 data_hdr_size () const - { - return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; - } - }; +private: + const u32 data_hdr_size () const + { + return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; + } +}; void vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) @@ -426,7 +437,7 @@ #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; - if (conn->features & ENABLE_COMPRESSION) + if (conn->features & FEATURE_COMPRESSION) { u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); @@ -563,7 +574,8 @@ } }; -void config_packet::setup (ptype type, int dst) +void +config_packet::setup (ptype type, int dst) { prot_major = PROTOCOL_MAJOR; prot_minor = PROTOCOL_MINOR; @@ -581,7 +593,8 @@ set_hdr (type, dst); } -bool config_packet::chk_config () const +bool +config_packet::chk_config () const { if (prot_major != PROTOCOL_MAJOR) slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); @@ -615,7 +628,7 @@ auth_req_packet (int dst, bool initiate_, u8 protocols_) { config_packet::setup (PT_AUTH_REQ, dst); - strncpy (magic, MAGIC, 8); + strncpy (magic, MAGIC_OLD, 8); initiate = !!initiate_; protocols = protocols_; @@ -674,7 +687,7 @@ void connection::connection_established () { - slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); + slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); if (ictx && octx) { @@ -762,7 +775,7 @@ pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); - slog (L_TRACE, ">>%d %s [%s]", conf->id, pong ? "PT_PONG" : "PT_PING", (const char *)si); + slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si); send_vpn_packet (pkt, si, IPTOS_LOWDELAY); @@ -792,7 +805,7 @@ 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, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si); send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly @@ -810,7 +823,7 @@ pkt->hmac_set (octx); - slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); + 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 @@ -820,8 +833,8 @@ void connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) { - slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", - conf->id, rid, (const char *)rsi); + slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename, + vpn->conns[rid - 1]->conf->nodename, (const char *)rsi); connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); @@ -862,7 +875,7 @@ { /*TODO*/ /* start the timer so we don't recurse endlessly */ w.start (1); - vpn->send_connect_request (conf->id); + vpn->send_connect_request (this); } else { @@ -985,7 +998,8 @@ } } -void connection::inject_vpn_packet (vpn_packet *pkt, int tos) +void +connection::inject_vpn_packet (vpn_packet *pkt, int tos) { if (ictx && octx) send_vpn_packet (pkt, si, tos); @@ -1001,8 +1015,8 @@ { last_activity = ev_now (); - slog (L_NOISE, "<<%d received packet type %d from %d to %d.", - conf->id, pkt->typ (), pkt->src (), pkt->dst ()); + slog (L_NOISE, "%s >> received packet type %d from %d to %d.", + conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); if (connectmode == conf_node::C_DISABLED) return; @@ -1010,6 +1024,8 @@ 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. @@ -1019,13 +1035,18 @@ send_auth_request (rsi, true); } else - // we would love to change thre socket address here, but ping's aren't + // 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: @@ -1050,9 +1071,10 @@ { 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)", conf->nodename, p->initiate ? "initiate" : "reply"); - if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) + if (p->chk_config () + && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8))) { if (p->prot_minor != PROTOCOL_MINOR) slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), @@ -1098,9 +1120,9 @@ case vpn_packet::PT_AUTH_RES: { - auth_res_packet *p = (auth_res_packet *) pkt; + auth_res_packet *p = (auth_res_packet *)pkt; - slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); + slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); if (p->chk_config ()) { @@ -1145,8 +1167,9 @@ si = rsi; protocol = rsi.prot; - slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d."), + slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), conf->nodename, (const char *)rsi, + is_direct ? "direct" : "forwarded", p->prot_major, p->prot_minor); connection_established (); @@ -1204,23 +1227,34 @@ // 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; - slog (L_INFO, _("%s(%s): socket address changed to %s."), - conf->nodename, (const char *)si, (const char *)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) // silently ignore - slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" - "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1); - else if (seqclass == 2) // reset + 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 duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" - "possible replay attack, or just massive packet loss, resetting connection."), seqno, iseqno.seq + 1); + 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); } @@ -1235,15 +1269,15 @@ 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; 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]", - conf->id, p->id, c->ictx && c->octx); + slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]", + conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); if (c->ictx && c->octx) { @@ -1276,8 +1310,9 @@ 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, "%s >> PT_CONNECT_INFO(%s,%s) [%d]", + conf->nodename, vpn->conns[p->id - 1]->conf->nodename, + (const char *)p->si, !c->ictx && !c->octx); const sockinfo &dsi = forward_si (p->si); @@ -1301,7 +1336,7 @@ inline void connection::keepalive_cb (ev::timer &w, int revents) { - if (ev_now () >= last_activity + ::conf.keepalive + 30) + if (ev_now () >= last_activity + ::conf.keepalive + 15) { reset_connection (); establish_connection (); @@ -1312,7 +1347,7 @@ || THISNODE->connectmode != conf_node::C_ONDEMAND) { send_ping (si); - w.start (5); + w.start (3); } else if (ev_now () < last_activity + ::conf.keepalive + 10) // hold ondemand connections implicitly a few seconds longer @@ -1322,36 +1357,41 @@ reset_connection (); } -void connection::send_connect_request (int id) +void +connection::send_connect_request (int id) { connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); - slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); + slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", + conf->nodename, vpn->conns[id - 1]->conf->nodename); p->hmac_set (octx); send_vpn_packet (p, si); delete p; } -void connection::script_init_env (const char *ext) +void +connection::script_init_env (const char *ext) { 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, "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); + conf->id & 0xff); putenv (env); } -void connection::script_init_connect_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); } inline const char * @@ -1371,6 +1411,22 @@ } inline const char * +connection::script_node_change () +{ + script_init_connect_env (); + + putenv ((char *)"STATE=change"); + + char *filename; + asprintf (&filename, + "%s/%s", + confbase, + ::conf.script_node_change ? ::conf.script_node_change : "node-change"); + + return filename; +} + +inline const char * connection::script_node_down () { script_init_connect_env (); @@ -1418,7 +1474,8 @@ shutdown (); } -void connection_init () +void +connection_init () { auth_rate_limiter.clear (); reset_rate_limiter.clear ();