ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/connection.C
(Generate patch)

Comparing gvpe/src/connection.C (file contents):
Revision 1.70 by pcg, Thu Aug 7 19:07:02 2008 UTC vs.
Revision 1.97 by root, Tue Dec 4 13:23:17 2012 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008,2010,2011 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify it 7 GVPE is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
50 50
51#if !HAVE_RAND_PSEUDO_BYTES 51#if !HAVE_RAND_PSEUDO_BYTES
52# define RAND_pseudo_bytes RAND_bytes 52# define RAND_pseudo_bytes RAND_bytes
53#endif 53#endif
54 54
55#define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic 55#define MAGIC_OLD "vped\xbd\xc6\xdb\x82" // 8 bytes of magic (still used in the protocol)
56#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic (understood but not generated)
56 57
57#define ULTRA_FAST 1 58#define ULTRA_FAST 1
58#define HLOG 15 59#define HLOG 15
59#include "lzf/lzf.h" 60#include "lzf/lzf.h"
60#include "lzf/lzf_c.c" 61#include "lzf/lzf_c.c"
63////////////////////////////////////////////////////////////////////////////// 64//////////////////////////////////////////////////////////////////////////////
64 65
65static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; 66static std::queue< std::pair<run_script_cb *, const char *> > rs_queue;
66static ev::child rs_child_ev; 67static ev::child rs_child_ev;
67 68
69namespace
70{
68void // c++ requires external linkage here, apparently :( 71 void // c++ requires external linkage here, apparently :(
69rs_child_cb (ev::child &w, int revents) 72 rs_child_cb (ev::child &w, int revents)
70{ 73 {
71 w.stop (); 74 w.stop ();
72 75
73 if (rs_queue.empty ()) 76 if (rs_queue.empty ())
74 return; 77 return;
75 78
76 pid_t pid = run_script (*rs_queue.front ().first, false); 79 pid_t pid = run_script (*rs_queue.front ().first, false);
77 if (pid) 80 if (pid)
78 { 81 {
79 w.set (pid); 82 w.set (pid);
80 w.start (); 83 w.start ();
81 } 84 }
82 else 85 else
83 slog (L_WARN, rs_queue.front ().second); 86 slog (L_WARN, rs_queue.front ().second);
84 87
85 delete rs_queue.front ().first; 88 delete rs_queue.front ().first;
86 rs_queue.pop (); 89 rs_queue.pop ();
87} 90 }
91};
88 92
89// despite the fancy name, this is quite a hack 93// despite the fancy name, this is quite a hack
90static void 94static void
91run_script_queued (run_script_cb *cb, const char *warnmsg) 95run_script_queued (run_script_cb *cb, const char *warnmsg)
92{ 96{
128rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) 132rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h)
129{ 133{
130 EVP_MD_CTX ctx; 134 EVP_MD_CTX ctx;
131 135
132 EVP_MD_CTX_init (&ctx); 136 EVP_MD_CTX_init (&ctx);
133 require (EVP_DigestInit (&ctx, RSA_HASH)); 137 require (EVP_DigestInit (&ctx, RSA_HASH));
134 require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); 138 require (EVP_DigestUpdate (&ctx, &chg, sizeof chg));
135 require (EVP_DigestUpdate(&ctx, &id, sizeof id)); 139 require (EVP_DigestUpdate (&ctx, &id, sizeof id));
136 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); 140 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0));
137 EVP_MD_CTX_cleanup (&ctx); 141 EVP_MD_CTX_cleanup (&ctx);
138} 142}
139 143
140struct rsa_entry 144struct rsa_entry
141{ 145{
190 cleaner.set (RSA_TTL, RSA_TTL); 194 cleaner.set (RSA_TTL, RSA_TTL);
191 } 195 }
192 196
193} rsa_cache; 197} rsa_cache;
194 198
199void
195void rsa_cache::cleaner_cb (ev::timer &w, int revents) 200rsa_cache::cleaner_cb (ev::timer &w, int revents)
196{ 201{
197 if (empty ()) 202 if (empty ())
198 w.stop (); 203 w.stop ();
199 else 204 else
200 { 205 {
225 delete p; 230 delete p;
226 231
227 delete [] queue; 232 delete [] queue;
228} 233}
229 234
235void
230void pkt_queue::expire_cb (ev::timer &w, int revents) 236pkt_queue::expire_cb (ev::timer &w, int revents)
231{ 237{
232 ev_tstamp expire = ev_now () - max_ttl; 238 ev_tstamp expire = ev_now () - max_ttl;
233 239
234 for (;;) 240 for (;;)
235 { 241 {
246 252
247 delete get (); 253 delete get ();
248 } 254 }
249} 255}
250 256
257void
251void pkt_queue::put (net_packet *p) 258pkt_queue::put (net_packet *p)
252{ 259{
253 ev_tstamp now = ev_now (); 260 ev_tstamp now = ev_now ();
254 261
255 // start expiry timer 262 // start expiry timer
256 if (empty ()) 263 if (empty ())
265 queue[i].tstamp = now; 272 queue[i].tstamp = now;
266 273
267 i = ni; 274 i = ni;
268} 275}
269 276
270net_packet *pkt_queue::get () 277net_packet *
278pkt_queue::get ()
271{ 279{
272 if (empty ()) 280 if (empty ())
273 return 0; 281 return 0;
274 282
275 net_packet *p = queue[j].pkt; 283 net_packet *p = queue[j].pkt;
299 307
300 bool can (const sockinfo &si) { return can((u32)si.host); } 308 bool can (const sockinfo &si) { return can((u32)si.host); }
301 bool can (u32 host); 309 bool can (u32 host);
302}; 310};
303 311
304net_rate_limiter auth_rate_limiter, reset_rate_limiter; 312static net_rate_limiter auth_rate_limiter, reset_rate_limiter;
305 313
314bool
306bool net_rate_limiter::can (u32 host) 315net_rate_limiter::can (u32 host)
307{ 316{
308 iterator i; 317 iterator i;
309 318
310 for (i = begin (); i != end (); ) 319 for (i = begin (); i != end (); )
311 if (i->host == host) 320 if (i->host == host)
358 367
359///////////////////////////////////////////////////////////////////////////// 368/////////////////////////////////////////////////////////////////////////////
360 369
361unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; 370unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
362 371
372void
363void hmac_packet::hmac_gen (crypto_ctx *ctx) 373hmac_packet::hmac_gen (crypto_ctx *ctx)
364{ 374{
365 unsigned int xlen; 375 unsigned int xlen;
366 376
367 HMAC_CTX *hctx = &ctx->hctx; 377 HMAC_CTX *hctx = &ctx->hctx;
368 378
386 hmac_gen (ctx); 396 hmac_gen (ctx);
387 397
388 return !memcmp (hmac, hmac_digest, HMACLENGTH); 398 return !memcmp (hmac, hmac_digest, HMACLENGTH);
389} 399}
390 400
401void
391void vpn_packet::set_hdr (ptype type_, unsigned int dst) 402vpn_packet::set_hdr (ptype type_, unsigned int dst)
392{ 403{
393 type = type_; 404 type = type_;
394 405
395 int src = THISNODE->id; 406 int src = THISNODE->id;
396 407
401 412
402#define MAXVPNDATA (MAX_MTU - 6 - 6) 413#define MAXVPNDATA (MAX_MTU - 6 - 6)
403#define DATAHDR (sizeof (u32) + RAND_SIZE) 414#define DATAHDR (sizeof (u32) + RAND_SIZE)
404 415
405struct vpndata_packet : vpn_packet 416struct vpndata_packet : vpn_packet
417{
418 u8 data[MAXVPNDATA + DATAHDR]; // seqno
419
420 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
421 tap_packet *unpack (connection *conn, u32 &seqno);
422
423private:
424 const u32 data_hdr_size () const
406 { 425 {
407 u8 data[MAXVPNDATA + DATAHDR]; // seqno
408
409 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
410 tap_packet *unpack (connection *conn, u32 &seqno);
411private:
412
413 const u32 data_hdr_size () const
414 {
415 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 426 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR;
416 }
417 }; 427 }
428};
418 429
419void 430void
420vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) 431vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno)
421{ 432{
422 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 433 EVP_CIPHER_CTX *cctx = &conn->octx->cctx;
424 ptype type = PT_DATA_UNCOMPRESSED; 435 ptype type = PT_DATA_UNCOMPRESSED;
425 436
426#if ENABLE_COMPRESSION 437#if ENABLE_COMPRESSION
427 u8 cdata[MAX_MTU]; 438 u8 cdata[MAX_MTU];
428 439
429 if (conn->features & ENABLE_COMPRESSION) 440 if (conn->features & FEATURE_COMPRESSION)
430 { 441 {
431 u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); 442 u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7);
432 443
433 if (cl) 444 if (cl)
434 { 445 {
561#endif 572#endif
562 return f; 573 return f;
563 } 574 }
564}; 575};
565 576
577void
566void config_packet::setup (ptype type, int dst) 578config_packet::setup (ptype type, int dst)
567{ 579{
568 prot_major = PROTOCOL_MAJOR; 580 prot_major = PROTOCOL_MAJOR;
569 prot_minor = PROTOCOL_MINOR; 581 prot_minor = PROTOCOL_MINOR;
570 randsize = RAND_SIZE; 582 randsize = RAND_SIZE;
571 hmaclen = HMACLENGTH; 583 hmaclen = HMACLENGTH;
579 591
580 len = sizeof (*this) - sizeof (net_packet); 592 len = sizeof (*this) - sizeof (net_packet);
581 set_hdr (type, dst); 593 set_hdr (type, dst);
582} 594}
583 595
596bool
584bool config_packet::chk_config () const 597config_packet::chk_config () const
585{ 598{
586 if (prot_major != PROTOCOL_MAJOR) 599 if (prot_major != PROTOCOL_MAJOR)
587 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 600 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
588 else if (randsize != RAND_SIZE) 601 else if (randsize != RAND_SIZE)
589 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 602 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
613 rsaencrdata encr; 626 rsaencrdata encr;
614 627
615 auth_req_packet (int dst, bool initiate_, u8 protocols_) 628 auth_req_packet (int dst, bool initiate_, u8 protocols_)
616 { 629 {
617 config_packet::setup (PT_AUTH_REQ, dst); 630 config_packet::setup (PT_AUTH_REQ, dst);
618 strncpy (magic, MAGIC, 8); 631 strncpy (magic, MAGIC_OLD, 8);
619 initiate = !!initiate_; 632 initiate = !!initiate_;
620 protocols = protocols_; 633 protocols = protocols_;
621 634
622 len = sizeof (*this) - sizeof (net_packet); 635 len = sizeof (*this) - sizeof (net_packet);
623 } 636 }
672///////////////////////////////////////////////////////////////////////////// 685/////////////////////////////////////////////////////////////////////////////
673 686
674void 687void
675connection::connection_established () 688connection::connection_established ()
676{ 689{
677 slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); 690 slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx);
678 691
679 if (ictx && octx) 692 if (ictx && octx)
680 { 693 {
681 // make sure rekeying timeouts are slightly asymmetric 694 // make sure rekeying timeouts are slightly asymmetric
682 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 695 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
683 rekey.start (rekey_interval, rekey_interval); 696 rekey.start (rekey_interval, rekey_interval);
697
684 keepalive.start (::conf.keepalive); 698 keepalive.start (::conf.keepalive);
685 699
686 // send queued packets 700 // send queued packets
687 if (ictx && octx) 701 if (ictx && octx)
688 { 702 {
696 { 710 {
697 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 711 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
698 delete p; 712 delete p;
699 } 713 }
700 } 714 }
715
716 vpn->connection_established (this);
701 } 717 }
702 else 718 else
703 { 719 {
704 retry_cnt = 0; 720 retry_cnt = 0;
705 establish_connection.start (5); 721 establish_connection.start (5);
709} 725}
710 726
711void 727void
712connection::reset_si () 728connection::reset_si ()
713{ 729{
730 if (vpn->can_direct (THISNODE, conf))
714 protocol = best_protocol (THISNODE->protocols & conf->protocols); 731 protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ());
715 732 else
716 // mask out protocols we cannot establish
717 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
718 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
719 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
720
721 if (protocol
722 && (!conf->can_direct (THISNODE)
723 || !THISNODE->can_direct (conf)))
724 { 733 {
725 slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); 734 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
726 protocol = 0; 735 protocol = 0;
727 } 736 }
728 737
729 si.set (conf, protocol); 738 si.set (conf, protocol);
739
740 is_direct = si.valid ();
730} 741}
731 742
732// ensure sockinfo is valid, forward if necessary 743// ensure sockinfo is valid, forward if necessary
733const sockinfo & 744const sockinfo &
734connection::forward_si (const sockinfo &si) const 745connection::forward_si (const sockinfo &si) const
735{ 746{
736 if (!si.valid ()) 747 if (!si.valid ())
737 { 748 {
738 connection *r = vpn->find_router (); 749 connection *r = vpn->find_router_for (this);
739 750
740 if (r) 751 if (r)
741 { 752 {
742 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), 753 slog (L_DEBUG, _("%s: no common protocol, trying to route through %s."),
743 conf->nodename, r->conf->nodename, (const char *)r->si); 754 conf->nodename, r->conf->nodename);
744 return r->si; 755 return r->si;
745 } 756 }
746 else 757 else
747 slog (L_DEBUG, _("%s: node unreachable, no common protocol"), 758 slog (L_DEBUG, _("%s: node unreachable, no common protocol or no router available."),
748 conf->nodename); 759 conf->nodename);
749 } 760 }
750 761
751 return si; 762 return si;
752} 763}
762connection::send_ping (const sockinfo &si, u8 pong) 773connection::send_ping (const sockinfo &si, u8 pong)
763{ 774{
764 ping_packet *pkt = new ping_packet; 775 ping_packet *pkt = new ping_packet;
765 776
766 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); 777 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING);
778
779 slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si);
780
767 send_vpn_packet (pkt, si, IPTOS_LOWDELAY); 781 send_vpn_packet (pkt, si, IPTOS_LOWDELAY);
768 782
769 delete pkt; 783 delete pkt;
770} 784}
771 785
790 804
791 rsachallenge chg; 805 rsachallenge chg;
792 rsa_cache.gen (pkt->id, chg); 806 rsa_cache.gen (pkt->id, chg);
793 rsa_encrypt (conf->rsa_key, chg, pkt->encr); 807 rsa_encrypt (conf->rsa_key, chg, pkt->encr);
794 808
795 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); 809 slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si);
796 810
797 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly 811 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly
798 812
799 delete pkt; 813 delete pkt;
800} 814}
808 822
809 rsa_hash (id, chg, pkt->response); 823 rsa_hash (id, chg, pkt->response);
810 824
811 pkt->hmac_set (octx); 825 pkt->hmac_set (octx);
812 826
813 slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); 827 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si);
814 828
815 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 829 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
816 830
817 delete pkt; 831 delete pkt;
818} 832}
819 833
820void 834void
821connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 835connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
822{ 836{
823 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", 837 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename,
824 conf->id, rid, (const char *)rsi); 838 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi,
839 conf->protocols);
825 840
826 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 841 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
827 842
828 r->hmac_set (octx); 843 r->hmac_set (octx);
829 send_vpn_packet (r, si); 844 send_vpn_packet (r, si);
832} 847}
833 848
834inline void 849inline void
835connection::establish_connection_cb (ev::timer &w, int revents) 850connection::establish_connection_cb (ev::timer &w, int revents)
836{ 851{
837 if (!ictx 852 if (!(ictx && octx)
838 && conf != THISNODE 853 && conf != THISNODE
839 && connectmode != conf_node::C_NEVER 854 && connectmode != conf_node::C_NEVER
840 && connectmode != conf_node::C_DISABLED 855 && connectmode != conf_node::C_DISABLED
841 && !w.is_active ()) 856 && !w.is_active ())
842 { 857 {
856 871
857 reset_si (); 872 reset_si ();
858 873
859 bool slow = si.prot & PROT_SLOW; 874 bool slow = si.prot & PROT_SLOW;
860 875
861 if (si.prot && !si.host) 876 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
862 { 877 {
863 slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename);
864 /*TODO*/ /* start the timer so we don't recurse endlessly */ 878 /*TODO*/ /* start the timer so we don't recurse endlessly */
865 w.start (1); 879 w.start (1);
866 vpn->send_connect_request (conf->id); 880 vpn->send_connect_request (this);
867 } 881 }
868 else 882 else
869 { 883 {
870 slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); 884 if (si.valid ())
885 slog (L_DEBUG, _("%s: sending direct connection request to %s."),
886 conf->nodename, (const char *)si);
871 887
872 const sockinfo &dsi = forward_si (si); 888 const sockinfo &dsi = forward_si (si);
873 889
874 slow = slow || (dsi.prot & PROT_SLOW); 890 slow = slow || (dsi.prot & PROT_SLOW);
875 891
909 } 925 }
910 } 926 }
911 927
912 delete ictx; ictx = 0; 928 delete ictx; ictx = 0;
913 delete octx; octx = 0; 929 delete octx; octx = 0;
914#if ENABLE_DNS
915 dnsv4_reset_connection ();
916#endif
917 930
918 si.host = 0; 931 si.host = 0;
919 932
920 last_activity = 0; 933 last_activity = 0.;
934 //last_si_change = 0.;
921 retry_cnt = 0; 935 retry_cnt = 0;
922 936
923 rekey.stop (); 937 rekey.stop ();
924 keepalive.stop (); 938 keepalive.stop ();
925 establish_connection.stop (); 939 establish_connection.stop ();
932 send_reset (si); 946 send_reset (si);
933 947
934 reset_connection (); 948 reset_connection ();
935} 949}
936 950
951// poor-man's rekeying
937inline void 952inline void
938connection::rekey_cb (ev::timer &w, int revents) 953connection::rekey_cb (ev::timer &w, int revents)
939{ 954{
940 reset_connection (); 955 reset_connection ();
941 establish_connection (); 956 establish_connection ();
980 data_queue.put (new tap_packet (*pkt)); 995 data_queue.put (new tap_packet (*pkt));
981 post_inject_queue (); 996 post_inject_queue ();
982 } 997 }
983} 998}
984 999
1000void
985void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 1001connection::inject_vpn_packet (vpn_packet *pkt, int tos)
986{ 1002{
987 if (ictx && octx) 1003 if (ictx && octx)
988 send_vpn_packet (pkt, si, tos); 1004 send_vpn_packet (pkt, si, tos);
989 else 1005 else
990 { 1006 {
996void 1012void
997connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1013connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
998{ 1014{
999 last_activity = ev_now (); 1015 last_activity = ev_now ();
1000 1016
1001 slog (L_NOISE, "<<%d received packet type %d from %d to %d", 1017 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1002 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 1018 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1003 1019
1004 if (connectmode == conf_node::C_DISABLED) 1020 if (connectmode == conf_node::C_DISABLED)
1005 return; 1021 return;
1006 1022
1007 switch (pkt->typ ()) 1023 switch (pkt->typ ())
1008 { 1024 {
1009 case vpn_packet::PT_PING: 1025 case vpn_packet::PT_PING:
1026 slog (L_TRACE, "%s >> PT_PING", conf->nodename);
1027
1010 // we send pings instead of auth packets after some retries, 1028 // we send pings instead of auth packets after some retries,
1011 // so reset the retry counter and establish a connection 1029 // so reset the retry counter and establish a connection
1012 // when we receive a ping. 1030 // when we receive a ping.
1013 if (!ictx) 1031 if (!ictx)
1014 { 1032 {
1015 if (auth_rate_limiter.can (rsi)) 1033 if (auth_rate_limiter.can (rsi))
1016 send_auth_request (rsi, true); 1034 send_auth_request (rsi, true);
1017 } 1035 }
1018 else 1036 else
1037 // we would love to change the socket address here, but ping's aren't
1038 // authenticated, so we best ignore it.
1019 send_ping (rsi, 1); // pong 1039 send_ping (rsi, 1); // pong
1020 1040
1021 break; 1041 break;
1022 1042
1023 case vpn_packet::PT_PONG: 1043 case vpn_packet::PT_PONG:
1044 slog (L_TRACE, "%s >> PT_PONG", conf->nodename);
1045
1046 // a PONG might mean that the other side doesn't really know
1047 // about our desire for communication.
1048 establish_connection ();
1024 break; 1049 break;
1025 1050
1026 case vpn_packet::PT_RESET: 1051 case vpn_packet::PT_RESET:
1027 { 1052 {
1028 reset_connection (); 1053 reset_connection ();
1029 1054
1030 config_packet *p = (config_packet *) pkt; 1055 config_packet *p = (config_packet *) pkt;
1031 1056
1032 if (!p->chk_config ()) 1057 if (!p->chk_config ())
1033 { 1058 {
1034 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), 1059 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."),
1035 conf->nodename, (const char *)rsi); 1060 conf->nodename, (const char *)rsi);
1036 connectmode = conf_node::C_DISABLED; 1061 connectmode = conf_node::C_DISABLED;
1037 } 1062 }
1038 else if (connectmode == conf_node::C_ALWAYS) 1063 else if (connectmode == conf_node::C_ALWAYS)
1039 establish_connection (); 1064 establish_connection ();
1041 break; 1066 break;
1042 1067
1043 case vpn_packet::PT_AUTH_REQ: 1068 case vpn_packet::PT_AUTH_REQ:
1044 if (auth_rate_limiter.can (rsi)) 1069 if (auth_rate_limiter.can (rsi))
1045 { 1070 {
1046 auth_req_packet *p = (auth_req_packet *) pkt; 1071 auth_req_packet *p = (auth_req_packet *)pkt;
1047 1072
1048 slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); 1073 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1074 conf->nodename, p->initiate ? "initiate" : "reply",
1075 p->protocols, p->features);
1049 1076
1050 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 1077 if (p->chk_config ()
1078 && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8)))
1051 { 1079 {
1052 if (p->prot_minor != PROTOCOL_MINOR) 1080 if (p->prot_minor != PROTOCOL_MINOR)
1053 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1081 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1054 conf->nodename, (const char *)rsi, 1082 conf->nodename, (const char *)rsi,
1055 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1083 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1081 1109
1082 break; 1110 break;
1083 } 1111 }
1084 } 1112 }
1085 else 1113 else
1086 slog (L_WARN, _("%s(%s): protocol mismatch"), 1114 slog (L_WARN, _("%s(%s): protocol mismatch."),
1087 conf->nodename, (const char *)rsi); 1115 conf->nodename, (const char *)rsi);
1088 1116
1089 send_reset (rsi); 1117 send_reset (rsi);
1090 } 1118 }
1091 1119
1092 break; 1120 break;
1093 1121
1094 case vpn_packet::PT_AUTH_RES: 1122 case vpn_packet::PT_AUTH_RES:
1095 { 1123 {
1096 auth_res_packet *p = (auth_res_packet *) pkt; 1124 auth_res_packet *p = (auth_res_packet *)pkt;
1097 1125
1098 slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); 1126 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1099 1127
1100 if (p->chk_config ()) 1128 if (p->chk_config ())
1101 { 1129 {
1102 if (p->prot_minor != PROTOCOL_MINOR) 1130 if (p->prot_minor != PROTOCOL_MINOR)
1103 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1131 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1106 1134
1107 rsachallenge chg; 1135 rsachallenge chg;
1108 1136
1109 if (!rsa_cache.find (p->id, chg)) 1137 if (!rsa_cache.find (p->id, chg))
1110 { 1138 {
1111 slog (L_ERR, _("%s(%s): unrequested auth response ignored"), 1139 slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."),
1112 conf->nodename, (const char *)rsi); 1140 conf->nodename, (const char *)rsi);
1113 break; 1141 break;
1114 } 1142 }
1115 else 1143 else
1116 { 1144 {
1117 crypto_ctx *cctx = new crypto_ctx (chg, 0); 1145 crypto_ctx *cctx = new crypto_ctx (chg, 0);
1118 1146
1119 if (!p->hmac_chk (cctx)) 1147 if (!p->hmac_chk (cctx))
1120 { 1148 {
1121 slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" 1149 slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n"
1122 "could be an attack, or just corruption or a synchronization error"), 1150 "could be an attack, or just corruption or a synchronization error."),
1123 conf->nodename, (const char *)rsi); 1151 conf->nodename, (const char *)rsi);
1124 break; 1152 break;
1125 } 1153 }
1126 else 1154 else
1127 { 1155 {
1138 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid 1166 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid
1139 1167
1140 si = rsi; 1168 si = rsi;
1141 protocol = rsi.prot; 1169 protocol = rsi.prot;
1142 1170
1171 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
1172 conf->nodename, (const char *)rsi,
1173 is_direct ? "direct" : "forwarded",
1174 p->prot_major, p->prot_minor);
1175
1143 connection_established (); 1176 connection_established ();
1144
1145 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1146 conf->nodename, (const char *)rsi,
1147 p->prot_major, p->prot_minor);
1148 1177
1149 if (::conf.script_node_up) 1178 if (::conf.script_node_up)
1150 { 1179 {
1151 run_script_cb *cb = new run_script_cb; 1180 run_script_cb *cb = new run_script_cb;
1152 cb->set<connection, &connection::script_node_up> (this); 1181 cb->set<connection, &connection::script_node_up> (this);
1154 } 1183 }
1155 1184
1156 break; 1185 break;
1157 } 1186 }
1158 else 1187 else
1159 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1188 slog (L_ERR, _("%s(%s): sent and received challenge do not match."),
1160 conf->nodename, (const char *)rsi); 1189 conf->nodename, (const char *)rsi);
1161 } 1190 }
1162 1191
1163 delete cctx; 1192 delete cctx;
1164 } 1193 }
1180 { 1209 {
1181 vpndata_packet *p = (vpndata_packet *)pkt; 1210 vpndata_packet *p = (vpndata_packet *)pkt;
1182 1211
1183 if (!p->hmac_chk (ictx)) 1212 if (!p->hmac_chk (ictx))
1184 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1213 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1185 "could be an attack, or just corruption or a synchronization error"), 1214 "could be an attack, or just corruption or a synchronization error."),
1186 conf->nodename, (const char *)rsi); 1215 conf->nodename, (const char *)rsi);
1187 else 1216 else
1188 { 1217 {
1189 u32 seqno; 1218 u32 seqno;
1190 tap_packet *d = p->unpack (this, seqno); 1219 tap_packet *d = p->unpack (this, seqno);
1220 int seqclass = iseqno.seqno_classify (seqno);
1191 1221
1192 if (iseqno.recv_ok (seqno)) 1222 if (seqclass == 0) // ok
1193 { 1223 {
1194 vpn->tap->send (d); 1224 vpn->tap->send (d);
1195 1225
1196 if (si != rsi) 1226 if (si != rsi)
1197 { 1227 {
1198 // fast re-sync on source address changes, useful especially for tcp/ip 1228 // fast re-sync on source address changes, useful especially for tcp/ip
1229 //if (last_si_change < ev_now () + 5.)
1199 si = rsi; 1230 // {
1200
1201 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1231 slog (L_INFO, _("%s(%s): changing socket address to %s."),
1202 conf->nodename, (const char *)si, (const char *)rsi); 1232 conf->nodename, (const char *)si, (const char *)rsi);
1233
1234 si = rsi;
1235
1236 if (::conf.script_node_change)
1237 {
1238 run_script_cb *cb = new run_script_cb;
1239 cb->set<connection, &connection::script_node_change> (this);
1240 run_script_queued (cb, _("node-change command execution failed, continuing."));
1241 }
1242
1243 // }
1244 //else
1245 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."),
1246 // conf->nodename, (const char *)si, (const char *)rsi);
1203 } 1247 }
1248 }
1249 else if (seqclass == 1) // far history
1250 slog (L_ERR, _("received very old packet (received %08lx, expected %08lx). "
1251 "possible replay attack, or just packet duplication/delay, ignoring."), seqno, iseqno.seq + 1);
1252 else if (seqclass == 2) // in-window duplicate, happens often on wireless
1253 slog (L_DEBUG, _("received recent duplicated packet (received %08lx, expected %08lx). "
1254 "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1);
1255 else if (seqclass == 3) // reset
1256 {
1257 slog (L_ERR, _("received out-of-sync (far future) packet (received %08lx, expected %08lx). "
1258 "probably just massive packet loss, sending reset."), seqno, iseqno.seq + 1);
1259 send_reset (rsi);
1204 } 1260 }
1205 1261
1206 delete d; 1262 delete d;
1207 break; 1263 break;
1208 } 1264 }
1212 break; 1268 break;
1213 1269
1214 case vpn_packet::PT_CONNECT_REQ: 1270 case vpn_packet::PT_CONNECT_REQ:
1215 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1271 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1216 { 1272 {
1217 connect_req_packet *p = (connect_req_packet *) pkt; 1273 connect_req_packet *p = (connect_req_packet *)pkt;
1218 1274
1219 if (p->id > 0 && p->id <= vpn->conns.size ()) 1275 if (p->id > 0 && p->id <= vpn->conns.size ())
1220 { 1276 {
1221 connection *c = vpn->conns[p->id - 1]; 1277 connection *c = vpn->conns[p->id - 1];
1222 conf->protocols = p->protocols; 1278 conf->protocols = p->protocols;
1223 1279
1224 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", 1280 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]",
1281 conf->nodename, vpn->conns[p->id - 1]->conf->nodename,
1282 p->protocols,
1225 conf->id, p->id, c->ictx && c->octx); 1283 c->ictx && c->octx);
1226 1284
1227 if (c->ictx && c->octx) 1285 if (c->ictx && c->octx)
1228 { 1286 {
1229 // send connect_info packets to both sides, in case one is 1287 // send connect_info packets to both sides, in case one is
1230 // behind a nat firewall (or both ;) 1288 // behind a nat firewall (or both ;)
1253 1311
1254 c->conf->protocols = p->protocols; 1312 c->conf->protocols = p->protocols;
1255 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1313 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1256 p->si.upgrade_protocol (protocol, c->conf); 1314 p->si.upgrade_protocol (protocol, c->conf);
1257 1315
1258 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1316 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,protocols=%02x,protocol=%02x,upgradable=%02x) [%d]",
1259 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1317 conf->nodename,
1318 vpn->conns[p->id - 1]->conf->nodename,
1319 (const char *)p->si,
1320 p->protocols,
1321 protocol,
1322 p->si.supported_protocols (c->conf),
1323 !c->ictx && !c->octx);
1260 1324
1261 const sockinfo &dsi = forward_si (p->si); 1325 const sockinfo &dsi = forward_si (p->si);
1262 1326
1263 if (dsi.valid ()) 1327 if (dsi.valid ())
1264 c->send_auth_request (dsi, true); 1328 c->send_auth_request (dsi, true);
1329 else
1330 slog (L_INFO, "connect info for %s received (%s), but still unable to contact.",
1331 vpn->conns[p->id - 1]->conf->nodename,
1332 (const char *)p->si);
1265 } 1333 }
1266 else 1334 else
1267 slog (L_WARN, 1335 slog (L_WARN,
1268 _("received authenticated connection request from unknown node #%d, config file mismatch?"), 1336 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1269 p->id); 1337 p->id);
1278} 1346}
1279 1347
1280inline void 1348inline void
1281connection::keepalive_cb (ev::timer &w, int revents) 1349connection::keepalive_cb (ev::timer &w, int revents)
1282{ 1350{
1283 if (ev_now () >= last_activity + ::conf.keepalive + 30) 1351 ev_tstamp when = last_activity + ::conf.keepalive - ev::now ();
1352
1353 if (when >= 0)
1354 w.start (when);
1355 else if (when < -15)
1284 { 1356 {
1285 reset_connection (); 1357 reset_connection ();
1286 establish_connection (); 1358 establish_connection ();
1287 } 1359 }
1288 else if (ev_now () < last_activity + ::conf.keepalive)
1289 w.start (last_activity + ::conf.keepalive - ev::now ());
1290 else if (conf->connectmode != conf_node::C_ONDEMAND 1360 else if (conf->connectmode != conf_node::C_ONDEMAND
1291 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1361 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1292 { 1362 {
1363 w.start (3);
1293 send_ping (si); 1364 send_ping (si);
1294 w.start (5);
1295 } 1365 }
1296 else if (ev_now () < last_activity + ::conf.keepalive + 10) 1366 else if (when >= -10)
1297 // hold ondemand connections implicitly a few seconds longer 1367 // hold ondemand connections implicitly a few seconds longer
1298 // should delete octx, though, or something like that ;) 1368 // should delete octx, though, or something like that ;)
1299 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1369 w.start (when + 10);
1300 else 1370 else
1301 reset_connection (); 1371 reset_connection ();
1302} 1372}
1303 1373
1374void
1304void connection::send_connect_request (int id) 1375connection::send_connect_request (int id)
1305{ 1376{
1306 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1377 connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols);
1307 1378
1308 slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); 1379 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)",
1380 conf->nodename, vpn->conns[id - 1]->conf->nodename,
1381 THISNODE->protocols);
1309 p->hmac_set (octx); 1382 p->hmac_set (octx);
1310 send_vpn_packet (p, si); 1383 send_vpn_packet (p, si);
1311 1384
1312 delete p; 1385 delete p;
1313} 1386}
1314 1387
1388void
1315void connection::script_init_env (const char *ext) 1389connection::script_init_env (const char *ext)
1316{ 1390{
1317 char *env; 1391 char *env;
1318 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1392 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1319 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1393 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1320 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1394 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1321 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1395 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1322 conf->id & 0xff); putenv (env); 1396 conf->id & 0xff); putenv (env);
1323} 1397}
1324 1398
1399void
1325void connection::script_init_connect_env () 1400connection::script_init_connect_env ()
1326{ 1401{
1327 vpn->script_init_env (); 1402 vpn->script_init_env ();
1328 1403
1329 char *env; 1404 char *env;
1330 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1405 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1406 asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env);
1331 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1407 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1332 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1408 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1333 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1409 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1334} 1410}
1335 1411
1336inline const char * 1412inline const char *
1337connection::script_node_up () 1413connection::script_node_up ()
1338{ 1414{
1348 1424
1349 return filename; 1425 return filename;
1350} 1426}
1351 1427
1352inline const char * 1428inline const char *
1429connection::script_node_change ()
1430{
1431 script_init_connect_env ();
1432
1433 putenv ((char *)"STATE=change");
1434
1435 char *filename;
1436 asprintf (&filename,
1437 "%s/%s",
1438 confbase,
1439 ::conf.script_node_change ? ::conf.script_node_change : "node-change");
1440
1441 return filename;
1442}
1443
1444inline const char *
1353connection::script_node_down () 1445connection::script_node_down ()
1354{ 1446{
1355 script_init_connect_env (); 1447 script_init_connect_env ();
1356 1448
1357 putenv ((char *)"STATE=down"); 1449 putenv ((char *)"STATE=down");
1368connection::connection (struct vpn *vpn, conf_node *conf) 1460connection::connection (struct vpn *vpn, conf_node *conf)
1369: vpn(vpn), conf(conf), 1461: vpn(vpn), conf(conf),
1370#if ENABLE_DNS 1462#if ENABLE_DNS
1371 dns (0), 1463 dns (0),
1372#endif 1464#endif
1373 data_queue(conf->max_ttl, conf->max_queue), 1465 data_queue(conf->max_ttl, conf->max_queue + 1),
1374 vpn_queue(conf->max_ttl, conf->max_queue) 1466 vpn_queue(conf->max_ttl, conf->max_queue + 1)
1375{ 1467{
1376 rekey .set<connection, &connection::rekey_cb > (this); 1468 rekey .set<connection, &connection::rekey_cb > (this);
1377 keepalive .set<connection, &connection::keepalive_cb > (this); 1469 keepalive .set<connection, &connection::keepalive_cb > (this);
1378 establish_connection.set<connection, &connection::establish_connection_cb> (this); 1470 establish_connection.set<connection, &connection::establish_connection_cb> (this);
1379 1471
1380 last_establish_attempt = 0.; 1472 last_establish_attempt = 0.;
1381 octx = ictx = 0; 1473 octx = ictx = 0;
1382 1474
1383 if (!conf->protocols) // make sure some protocol is enabled
1384 conf->protocols = PROT_UDPv4;
1385
1386 connectmode = conf->connectmode; 1475 connectmode = conf->connectmode;
1387 1476
1388 // queue a dummy packet to force an initial connection attempt 1477 // queue a dummy packet to force an initial connection attempt
1389 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1478 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1390 vpn_queue.put (new net_packet); 1479 vpn_queue.put (new net_packet);
1395connection::~connection () 1484connection::~connection ()
1396{ 1485{
1397 shutdown (); 1486 shutdown ();
1398} 1487}
1399 1488
1489void
1400void connection_init () 1490connection_init ()
1401{ 1491{
1402 auth_rate_limiter.clear (); 1492 auth_rate_limiter.clear ();
1403 reset_rate_limiter.clear (); 1493 reset_rate_limiter.clear ();
1404} 1494}
1405 1495

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines