--- gvpe/src/connection.C 2008/08/08 16:48:00 1.72 +++ gvpe/src/connection.C 2011/02/10 03:59:51 1.92 @@ -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) { @@ -698,6 +711,8 @@ delete p; } } + + vpn->connection_established (this); } else { @@ -711,22 +726,17 @@ void connection::reset_si () { - protocol = best_protocol (THISNODE->protocols & conf->protocols); - - // mask out endpoints we can't connect to - if (!conf->udp_port) protocol &= ~PROT_UDPv4; - if (!conf->tcp_port) protocol &= ~PROT_TCPv4; - if (!conf->dns_port) protocol &= ~PROT_DNSv4; - - if (protocol - && (!conf->can_direct (THISNODE) - || !THISNODE->can_direct (conf))) + if (vpn->can_direct (THISNODE, conf)) + protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ()); + else { - slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); + slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); protocol = 0; } si.set (conf, protocol); + + is_direct = si.valid (); } // ensure sockinfo is valid, forward if necessary @@ -735,16 +745,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 (%s)"), - conf->nodename, r->conf->nodename, (const char *)r->si); + 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, no router"), + slog (L_DEBUG, _("%s: node unreachable, no common protocol or no router available."), conf->nodename); } @@ -764,6 +774,9 @@ ping_packet *pkt = new ping_packet; pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); + + 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; @@ -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); @@ -834,7 +847,7 @@ inline void connection::establish_connection_cb (ev::timer &w, int revents) { - if (!ictx + if (!(ictx && octx) && conf != THISNODE && connectmode != conf_node::C_NEVER && connectmode != conf_node::C_DISABLED @@ -858,16 +871,17 @@ bool slow = si.prot & PROT_SLOW; - if (si.prot && !si.host) + if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) { - slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); /*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 { - slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); + if (si.valid ()) + slog (L_DEBUG, _("%s: sending direct connection request to %s."), + conf->nodename, (const char *)si); const sockinfo &dsi = forward_si (si); @@ -917,7 +931,8 @@ si.host = 0; - last_activity = 0; + last_activity = 0.; + //last_si_change = 0.; retry_cnt = 0; rekey.stop (); @@ -934,6 +949,7 @@ reset_connection (); } +// poor-man's rekeying inline void connection::rekey_cb (ev::timer &w, int revents) { @@ -982,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); @@ -998,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; @@ -1007,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. @@ -1016,11 +1035,18 @@ 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: @@ -1031,7 +1057,7 @@ if (!p->chk_config ()) { - slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), + slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."), conf->nodename, (const char *)rsi); connectmode = conf_node::C_DISABLED; } @@ -1045,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."), @@ -1083,7 +1110,7 @@ } } else - slog (L_WARN, _("%s(%s): protocol mismatch"), + slog (L_WARN, _("%s(%s): protocol mismatch."), conf->nodename, (const char *)rsi); send_reset (rsi); @@ -1093,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 ()) { @@ -1108,7 +1135,7 @@ if (!rsa_cache.find (p->id, chg)) { - slog (L_ERR, _("%s(%s): unrequested auth response ignored"), + slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."), conf->nodename, (const char *)rsi); break; } @@ -1119,7 +1146,7 @@ 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 a synchronization error"), + "could be an attack, or just corruption or a synchronization error."), conf->nodename, (const char *)rsi); break; } @@ -1140,12 +1167,13 @@ si = rsi; protocol = rsi.prot; - connection_established (); - - 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 (); + if (::conf.script_node_up) { run_script_cb *cb = new run_script_cb; @@ -1156,7 +1184,7 @@ break; } else - slog (L_ERR, _("%s(%s): sent and received challenge do not match"), + slog (L_ERR, _("%s(%s): sent and received challenge do not match."), conf->nodename, (const char *)rsi); } @@ -1182,26 +1210,53 @@ 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"), + "could be an attack, or just corruption or a synchronization error."), conf->nodename, (const char *)rsi); else { u32 seqno; tap_packet *d = p->unpack (this, seqno); + int seqclass = iseqno.seqno_classify (seqno); - if (iseqno.recv_ok (seqno)) + if (seqclass == 0) // ok { vpn->tap->send (d); if (si != rsi) { // fast re-sync on source address changes, useful especially for tcp/ip - si = rsi; - - slog (L_INFO, _("%s(%s): socket address changed to %s"), - conf->nodename, (const char *)si, (const char *)rsi); + //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; @@ -1214,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) { @@ -1255,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); @@ -1280,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 (); @@ -1291,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 @@ -1301,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 * @@ -1350,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 (); @@ -1397,7 +1474,8 @@ shutdown (); } -void connection_init () +void +connection_init () { auth_rate_limiter.clear (); reset_rate_limiter.clear ();