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.92 by root, Thu Feb 10 03:59:51 2011 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 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{
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);
696 { 709 {
697 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 710 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
698 delete p; 711 delete p;
699 } 712 }
700 } 713 }
714
715 vpn->connection_established (this);
701 } 716 }
702 else 717 else
703 { 718 {
704 retry_cnt = 0; 719 retry_cnt = 0;
705 establish_connection.start (5); 720 establish_connection.start (5);
709} 724}
710 725
711void 726void
712connection::reset_si () 727connection::reset_si ()
713{ 728{
729 if (vpn->can_direct (THISNODE, conf))
714 protocol = best_protocol (THISNODE->protocols & conf->protocols); 730 protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ());
715 731 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 { 732 {
725 slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); 733 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
726 protocol = 0; 734 protocol = 0;
727 } 735 }
728 736
729 si.set (conf, protocol); 737 si.set (conf, protocol);
738
739 is_direct = si.valid ();
730} 740}
731 741
732// ensure sockinfo is valid, forward if necessary 742// ensure sockinfo is valid, forward if necessary
733const sockinfo & 743const sockinfo &
734connection::forward_si (const sockinfo &si) const 744connection::forward_si (const sockinfo &si) const
735{ 745{
736 if (!si.valid ()) 746 if (!si.valid ())
737 { 747 {
738 connection *r = vpn->find_router (); 748 connection *r = vpn->find_router_for (this);
739 749
740 if (r) 750 if (r)
741 { 751 {
742 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), 752 slog (L_DEBUG, _("%s: no common protocol, trying to route through %s."),
743 conf->nodename, r->conf->nodename, (const char *)r->si); 753 conf->nodename, r->conf->nodename);
744 return r->si; 754 return r->si;
745 } 755 }
746 else 756 else
747 slog (L_DEBUG, _("%s: node unreachable, no common protocol"), 757 slog (L_DEBUG, _("%s: node unreachable, no common protocol or no router available."),
748 conf->nodename); 758 conf->nodename);
749 } 759 }
750 760
751 return si; 761 return si;
752} 762}
762connection::send_ping (const sockinfo &si, u8 pong) 772connection::send_ping (const sockinfo &si, u8 pong)
763{ 773{
764 ping_packet *pkt = new ping_packet; 774 ping_packet *pkt = new ping_packet;
765 775
766 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); 776 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING);
777
778 slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si);
779
767 send_vpn_packet (pkt, si, IPTOS_LOWDELAY); 780 send_vpn_packet (pkt, si, IPTOS_LOWDELAY);
768 781
769 delete pkt; 782 delete pkt;
770} 783}
771 784
790 803
791 rsachallenge chg; 804 rsachallenge chg;
792 rsa_cache.gen (pkt->id, chg); 805 rsa_cache.gen (pkt->id, chg);
793 rsa_encrypt (conf->rsa_key, chg, pkt->encr); 806 rsa_encrypt (conf->rsa_key, chg, pkt->encr);
794 807
795 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); 808 slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si);
796 809
797 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly 810 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly
798 811
799 delete pkt; 812 delete pkt;
800} 813}
808 821
809 rsa_hash (id, chg, pkt->response); 822 rsa_hash (id, chg, pkt->response);
810 823
811 pkt->hmac_set (octx); 824 pkt->hmac_set (octx);
812 825
813 slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); 826 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si);
814 827
815 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 828 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
816 829
817 delete pkt; 830 delete pkt;
818} 831}
819 832
820void 833void
821connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 834connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
822{ 835{
823 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", 836 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename,
824 conf->id, rid, (const char *)rsi); 837 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi);
825 838
826 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 839 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
827 840
828 r->hmac_set (octx); 841 r->hmac_set (octx);
829 send_vpn_packet (r, si); 842 send_vpn_packet (r, si);
832} 845}
833 846
834inline void 847inline void
835connection::establish_connection_cb (ev::timer &w, int revents) 848connection::establish_connection_cb (ev::timer &w, int revents)
836{ 849{
837 if (!ictx 850 if (!(ictx && octx)
838 && conf != THISNODE 851 && conf != THISNODE
839 && connectmode != conf_node::C_NEVER 852 && connectmode != conf_node::C_NEVER
840 && connectmode != conf_node::C_DISABLED 853 && connectmode != conf_node::C_DISABLED
841 && !w.is_active ()) 854 && !w.is_active ())
842 { 855 {
856 869
857 reset_si (); 870 reset_si ();
858 871
859 bool slow = si.prot & PROT_SLOW; 872 bool slow = si.prot & PROT_SLOW;
860 873
861 if (si.prot && !si.host) 874 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
862 { 875 {
863 slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename);
864 /*TODO*/ /* start the timer so we don't recurse endlessly */ 876 /*TODO*/ /* start the timer so we don't recurse endlessly */
865 w.start (1); 877 w.start (1);
866 vpn->send_connect_request (conf->id); 878 vpn->send_connect_request (this);
867 } 879 }
868 else 880 else
869 { 881 {
870 slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); 882 if (si.valid ())
883 slog (L_DEBUG, _("%s: sending direct connection request to %s."),
884 conf->nodename, (const char *)si);
871 885
872 const sockinfo &dsi = forward_si (si); 886 const sockinfo &dsi = forward_si (si);
873 887
874 slow = slow || (dsi.prot & PROT_SLOW); 888 slow = slow || (dsi.prot & PROT_SLOW);
875 889
915 dnsv4_reset_connection (); 929 dnsv4_reset_connection ();
916#endif 930#endif
917 931
918 si.host = 0; 932 si.host = 0;
919 933
920 last_activity = 0; 934 last_activity = 0.;
935 //last_si_change = 0.;
921 retry_cnt = 0; 936 retry_cnt = 0;
922 937
923 rekey.stop (); 938 rekey.stop ();
924 keepalive.stop (); 939 keepalive.stop ();
925 establish_connection.stop (); 940 establish_connection.stop ();
932 send_reset (si); 947 send_reset (si);
933 948
934 reset_connection (); 949 reset_connection ();
935} 950}
936 951
952// poor-man's rekeying
937inline void 953inline void
938connection::rekey_cb (ev::timer &w, int revents) 954connection::rekey_cb (ev::timer &w, int revents)
939{ 955{
940 reset_connection (); 956 reset_connection ();
941 establish_connection (); 957 establish_connection ();
980 data_queue.put (new tap_packet (*pkt)); 996 data_queue.put (new tap_packet (*pkt));
981 post_inject_queue (); 997 post_inject_queue ();
982 } 998 }
983} 999}
984 1000
1001void
985void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 1002connection::inject_vpn_packet (vpn_packet *pkt, int tos)
986{ 1003{
987 if (ictx && octx) 1004 if (ictx && octx)
988 send_vpn_packet (pkt, si, tos); 1005 send_vpn_packet (pkt, si, tos);
989 else 1006 else
990 { 1007 {
996void 1013void
997connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1014connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
998{ 1015{
999 last_activity = ev_now (); 1016 last_activity = ev_now ();
1000 1017
1001 slog (L_NOISE, "<<%d received packet type %d from %d to %d", 1018 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1002 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 1019 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1003 1020
1004 if (connectmode == conf_node::C_DISABLED) 1021 if (connectmode == conf_node::C_DISABLED)
1005 return; 1022 return;
1006 1023
1007 switch (pkt->typ ()) 1024 switch (pkt->typ ())
1008 { 1025 {
1009 case vpn_packet::PT_PING: 1026 case vpn_packet::PT_PING:
1027 slog (L_TRACE, "%s >> PT_PING", conf->nodename);
1028
1010 // we send pings instead of auth packets after some retries, 1029 // we send pings instead of auth packets after some retries,
1011 // so reset the retry counter and establish a connection 1030 // so reset the retry counter and establish a connection
1012 // when we receive a ping. 1031 // when we receive a ping.
1013 if (!ictx) 1032 if (!ictx)
1014 { 1033 {
1015 if (auth_rate_limiter.can (rsi)) 1034 if (auth_rate_limiter.can (rsi))
1016 send_auth_request (rsi, true); 1035 send_auth_request (rsi, true);
1017 } 1036 }
1018 else 1037 else
1038 // we would love to change the socket address here, but ping's aren't
1039 // authenticated, so we best ignore it.
1019 send_ping (rsi, 1); // pong 1040 send_ping (rsi, 1); // pong
1020 1041
1021 break; 1042 break;
1022 1043
1023 case vpn_packet::PT_PONG: 1044 case vpn_packet::PT_PONG:
1045 slog (L_TRACE, "%s >> PT_PONG", conf->nodename);
1046
1047 // a PONG might mean that the other side doesn't really know
1048 // about our desire for communication.
1049 establish_connection ();
1024 break; 1050 break;
1025 1051
1026 case vpn_packet::PT_RESET: 1052 case vpn_packet::PT_RESET:
1027 { 1053 {
1028 reset_connection (); 1054 reset_connection ();
1029 1055
1030 config_packet *p = (config_packet *) pkt; 1056 config_packet *p = (config_packet *) pkt;
1031 1057
1032 if (!p->chk_config ()) 1058 if (!p->chk_config ())
1033 { 1059 {
1034 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), 1060 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."),
1035 conf->nodename, (const char *)rsi); 1061 conf->nodename, (const char *)rsi);
1036 connectmode = conf_node::C_DISABLED; 1062 connectmode = conf_node::C_DISABLED;
1037 } 1063 }
1038 else if (connectmode == conf_node::C_ALWAYS) 1064 else if (connectmode == conf_node::C_ALWAYS)
1039 establish_connection (); 1065 establish_connection ();
1043 case vpn_packet::PT_AUTH_REQ: 1069 case vpn_packet::PT_AUTH_REQ:
1044 if (auth_rate_limiter.can (rsi)) 1070 if (auth_rate_limiter.can (rsi))
1045 { 1071 {
1046 auth_req_packet *p = (auth_req_packet *) pkt; 1072 auth_req_packet *p = (auth_req_packet *) pkt;
1047 1073
1048 slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); 1074 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply");
1049 1075
1050 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 1076 if (p->chk_config ()
1077 && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8)))
1051 { 1078 {
1052 if (p->prot_minor != PROTOCOL_MINOR) 1079 if (p->prot_minor != PROTOCOL_MINOR)
1053 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1080 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1054 conf->nodename, (const char *)rsi, 1081 conf->nodename, (const char *)rsi,
1055 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1082 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1081 1108
1082 break; 1109 break;
1083 } 1110 }
1084 } 1111 }
1085 else 1112 else
1086 slog (L_WARN, _("%s(%s): protocol mismatch"), 1113 slog (L_WARN, _("%s(%s): protocol mismatch."),
1087 conf->nodename, (const char *)rsi); 1114 conf->nodename, (const char *)rsi);
1088 1115
1089 send_reset (rsi); 1116 send_reset (rsi);
1090 } 1117 }
1091 1118
1092 break; 1119 break;
1093 1120
1094 case vpn_packet::PT_AUTH_RES: 1121 case vpn_packet::PT_AUTH_RES:
1095 { 1122 {
1096 auth_res_packet *p = (auth_res_packet *) pkt; 1123 auth_res_packet *p = (auth_res_packet *)pkt;
1097 1124
1098 slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); 1125 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1099 1126
1100 if (p->chk_config ()) 1127 if (p->chk_config ())
1101 { 1128 {
1102 if (p->prot_minor != PROTOCOL_MINOR) 1129 if (p->prot_minor != PROTOCOL_MINOR)
1103 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1130 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1106 1133
1107 rsachallenge chg; 1134 rsachallenge chg;
1108 1135
1109 if (!rsa_cache.find (p->id, chg)) 1136 if (!rsa_cache.find (p->id, chg))
1110 { 1137 {
1111 slog (L_ERR, _("%s(%s): unrequested auth response ignored"), 1138 slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."),
1112 conf->nodename, (const char *)rsi); 1139 conf->nodename, (const char *)rsi);
1113 break; 1140 break;
1114 } 1141 }
1115 else 1142 else
1116 { 1143 {
1117 crypto_ctx *cctx = new crypto_ctx (chg, 0); 1144 crypto_ctx *cctx = new crypto_ctx (chg, 0);
1118 1145
1119 if (!p->hmac_chk (cctx)) 1146 if (!p->hmac_chk (cctx))
1120 { 1147 {
1121 slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" 1148 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"), 1149 "could be an attack, or just corruption or a synchronization error."),
1123 conf->nodename, (const char *)rsi); 1150 conf->nodename, (const char *)rsi);
1124 break; 1151 break;
1125 } 1152 }
1126 else 1153 else
1127 { 1154 {
1138 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid 1165 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid
1139 1166
1140 si = rsi; 1167 si = rsi;
1141 protocol = rsi.prot; 1168 protocol = rsi.prot;
1142 1169
1170 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
1171 conf->nodename, (const char *)rsi,
1172 is_direct ? "direct" : "forwarded",
1173 p->prot_major, p->prot_minor);
1174
1143 connection_established (); 1175 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 1176
1149 if (::conf.script_node_up) 1177 if (::conf.script_node_up)
1150 { 1178 {
1151 run_script_cb *cb = new run_script_cb; 1179 run_script_cb *cb = new run_script_cb;
1152 cb->set<connection, &connection::script_node_up> (this); 1180 cb->set<connection, &connection::script_node_up> (this);
1154 } 1182 }
1155 1183
1156 break; 1184 break;
1157 } 1185 }
1158 else 1186 else
1159 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1187 slog (L_ERR, _("%s(%s): sent and received challenge do not match."),
1160 conf->nodename, (const char *)rsi); 1188 conf->nodename, (const char *)rsi);
1161 } 1189 }
1162 1190
1163 delete cctx; 1191 delete cctx;
1164 } 1192 }
1180 { 1208 {
1181 vpndata_packet *p = (vpndata_packet *)pkt; 1209 vpndata_packet *p = (vpndata_packet *)pkt;
1182 1210
1183 if (!p->hmac_chk (ictx)) 1211 if (!p->hmac_chk (ictx))
1184 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1212 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1185 "could be an attack, or just corruption or a synchronization error"), 1213 "could be an attack, or just corruption or a synchronization error."),
1186 conf->nodename, (const char *)rsi); 1214 conf->nodename, (const char *)rsi);
1187 else 1215 else
1188 { 1216 {
1189 u32 seqno; 1217 u32 seqno;
1190 tap_packet *d = p->unpack (this, seqno); 1218 tap_packet *d = p->unpack (this, seqno);
1219 int seqclass = iseqno.seqno_classify (seqno);
1191 1220
1192 if (iseqno.recv_ok (seqno)) 1221 if (seqclass == 0) // ok
1193 { 1222 {
1194 vpn->tap->send (d); 1223 vpn->tap->send (d);
1195 1224
1196 if (si != rsi) 1225 if (si != rsi)
1197 { 1226 {
1198 // fast re-sync on source address changes, useful especially for tcp/ip 1227 // fast re-sync on source address changes, useful especially for tcp/ip
1228 //if (last_si_change < ev_now () + 5.)
1199 si = rsi; 1229 // {
1200
1201 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1230 slog (L_INFO, _("%s(%s): changing socket address to %s."),
1202 conf->nodename, (const char *)si, (const char *)rsi); 1231 conf->nodename, (const char *)si, (const char *)rsi);
1232
1233 si = rsi;
1234
1235 if (::conf.script_node_change)
1236 {
1237 run_script_cb *cb = new run_script_cb;
1238 cb->set<connection, &connection::script_node_change> (this);
1239 run_script_queued (cb, _("node-change command execution failed, continuing."));
1240 }
1241
1242 // }
1243 //else
1244 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."),
1245 // conf->nodename, (const char *)si, (const char *)rsi);
1203 } 1246 }
1247 }
1248 else if (seqclass == 1) // far history
1249 slog (L_ERR, _("received very old packet (received %08lx, expected %08lx). "
1250 "possible replay attack, or just packet duplication/delay, ignoring."), seqno, iseqno.seq + 1);
1251 else if (seqclass == 2) // in-window duplicate, happens often on wireless
1252 slog (L_DEBUG, _("received recent duplicated packet (received %08lx, expected %08lx). "
1253 "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1);
1254 else if (seqclass == 3) // reset
1255 {
1256 slog (L_ERR, _("received out-of-sync (far future) packet (received %08lx, expected %08lx). "
1257 "probably just massive packet loss, sending reset."), seqno, iseqno.seq + 1);
1258 send_reset (rsi);
1204 } 1259 }
1205 1260
1206 delete d; 1261 delete d;
1207 break; 1262 break;
1208 } 1263 }
1212 break; 1267 break;
1213 1268
1214 case vpn_packet::PT_CONNECT_REQ: 1269 case vpn_packet::PT_CONNECT_REQ:
1215 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1270 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1216 { 1271 {
1217 connect_req_packet *p = (connect_req_packet *) pkt; 1272 connect_req_packet *p = (connect_req_packet *)pkt;
1218 1273
1219 if (p->id > 0 && p->id <= vpn->conns.size ()) 1274 if (p->id > 0 && p->id <= vpn->conns.size ())
1220 { 1275 {
1221 connection *c = vpn->conns[p->id - 1]; 1276 connection *c = vpn->conns[p->id - 1];
1222 conf->protocols = p->protocols; 1277 conf->protocols = p->protocols;
1223 1278
1224 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", 1279 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]",
1225 conf->id, p->id, c->ictx && c->octx); 1280 conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx);
1226 1281
1227 if (c->ictx && c->octx) 1282 if (c->ictx && c->octx)
1228 { 1283 {
1229 // send connect_info packets to both sides, in case one is 1284 // send connect_info packets to both sides, in case one is
1230 // behind a nat firewall (or both ;) 1285 // behind a nat firewall (or both ;)
1253 1308
1254 c->conf->protocols = p->protocols; 1309 c->conf->protocols = p->protocols;
1255 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1310 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1256 p->si.upgrade_protocol (protocol, c->conf); 1311 p->si.upgrade_protocol (protocol, c->conf);
1257 1312
1258 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1313 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]",
1314 conf->nodename, vpn->conns[p->id - 1]->conf->nodename,
1259 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1315 (const char *)p->si, !c->ictx && !c->octx);
1260 1316
1261 const sockinfo &dsi = forward_si (p->si); 1317 const sockinfo &dsi = forward_si (p->si);
1262 1318
1263 if (dsi.valid ()) 1319 if (dsi.valid ())
1264 c->send_auth_request (dsi, true); 1320 c->send_auth_request (dsi, true);
1278} 1334}
1279 1335
1280inline void 1336inline void
1281connection::keepalive_cb (ev::timer &w, int revents) 1337connection::keepalive_cb (ev::timer &w, int revents)
1282{ 1338{
1283 if (ev_now () >= last_activity + ::conf.keepalive + 30) 1339 if (ev_now () >= last_activity + ::conf.keepalive + 15)
1284 { 1340 {
1285 reset_connection (); 1341 reset_connection ();
1286 establish_connection (); 1342 establish_connection ();
1287 } 1343 }
1288 else if (ev_now () < last_activity + ::conf.keepalive) 1344 else if (ev_now () < last_activity + ::conf.keepalive)
1289 w.start (last_activity + ::conf.keepalive - ev::now ()); 1345 w.start (last_activity + ::conf.keepalive - ev::now ());
1290 else if (conf->connectmode != conf_node::C_ONDEMAND 1346 else if (conf->connectmode != conf_node::C_ONDEMAND
1291 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1347 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1292 { 1348 {
1293 send_ping (si); 1349 send_ping (si);
1294 w.start (5); 1350 w.start (3);
1295 } 1351 }
1296 else if (ev_now () < last_activity + ::conf.keepalive + 10) 1352 else if (ev_now () < last_activity + ::conf.keepalive + 10)
1297 // hold ondemand connections implicitly a few seconds longer 1353 // hold ondemand connections implicitly a few seconds longer
1298 // should delete octx, though, or something like that ;) 1354 // should delete octx, though, or something like that ;)
1299 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1355 w.start (last_activity + ::conf.keepalive + 10 - ev::now ());
1300 else 1356 else
1301 reset_connection (); 1357 reset_connection ();
1302} 1358}
1303 1359
1360void
1304void connection::send_connect_request (int id) 1361connection::send_connect_request (int id)
1305{ 1362{
1306 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1363 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols);
1307 1364
1308 slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); 1365 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)",
1366 conf->nodename, vpn->conns[id - 1]->conf->nodename);
1309 p->hmac_set (octx); 1367 p->hmac_set (octx);
1310 send_vpn_packet (p, si); 1368 send_vpn_packet (p, si);
1311 1369
1312 delete p; 1370 delete p;
1313} 1371}
1314 1372
1373void
1315void connection::script_init_env (const char *ext) 1374connection::script_init_env (const char *ext)
1316{ 1375{
1317 char *env; 1376 char *env;
1318 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1377 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1319 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1378 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1320 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1379 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1321 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1380 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1322 conf->id & 0xff); putenv (env); 1381 conf->id & 0xff); putenv (env);
1323} 1382}
1324 1383
1384void
1325void connection::script_init_connect_env () 1385connection::script_init_connect_env ()
1326{ 1386{
1327 vpn->script_init_env (); 1387 vpn->script_init_env ();
1328 1388
1329 char *env; 1389 char *env;
1330 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1390 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1391 asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env);
1331 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1392 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1332 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1393 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1333 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1394 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1334} 1395}
1335 1396
1336inline const char * 1397inline const char *
1337connection::script_node_up () 1398connection::script_node_up ()
1338{ 1399{
1348 1409
1349 return filename; 1410 return filename;
1350} 1411}
1351 1412
1352inline const char * 1413inline const char *
1414connection::script_node_change ()
1415{
1416 script_init_connect_env ();
1417
1418 putenv ((char *)"STATE=change");
1419
1420 char *filename;
1421 asprintf (&filename,
1422 "%s/%s",
1423 confbase,
1424 ::conf.script_node_change ? ::conf.script_node_change : "node-change");
1425
1426 return filename;
1427}
1428
1429inline const char *
1353connection::script_node_down () 1430connection::script_node_down ()
1354{ 1431{
1355 script_init_connect_env (); 1432 script_init_connect_env ();
1356 1433
1357 putenv ((char *)"STATE=down"); 1434 putenv ((char *)"STATE=down");
1368connection::connection (struct vpn *vpn, conf_node *conf) 1445connection::connection (struct vpn *vpn, conf_node *conf)
1369: vpn(vpn), conf(conf), 1446: vpn(vpn), conf(conf),
1370#if ENABLE_DNS 1447#if ENABLE_DNS
1371 dns (0), 1448 dns (0),
1372#endif 1449#endif
1373 data_queue(conf->max_ttl, conf->max_queue), 1450 data_queue(conf->max_ttl, conf->max_queue + 1),
1374 vpn_queue(conf->max_ttl, conf->max_queue) 1451 vpn_queue(conf->max_ttl, conf->max_queue + 1)
1375{ 1452{
1376 rekey .set<connection, &connection::rekey_cb > (this); 1453 rekey .set<connection, &connection::rekey_cb > (this);
1377 keepalive .set<connection, &connection::keepalive_cb > (this); 1454 keepalive .set<connection, &connection::keepalive_cb > (this);
1378 establish_connection.set<connection, &connection::establish_connection_cb> (this); 1455 establish_connection.set<connection, &connection::establish_connection_cb> (this);
1379 1456
1395connection::~connection () 1472connection::~connection ()
1396{ 1473{
1397 shutdown (); 1474 shutdown ();
1398} 1475}
1399 1476
1477void
1400void connection_init () 1478connection_init ()
1401{ 1479{
1402 auth_rate_limiter.clear (); 1480 auth_rate_limiter.clear ();
1403 reset_rate_limiter.clear (); 1481 reset_rate_limiter.clear ();
1404} 1482}
1405 1483

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines