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.102 by root, Thu Jul 18 13:35:16 2013 UTC vs.
Revision 1.109 by root, Fri Oct 11 07:56:07 2013 UTC

33 33
34#include <list> 34#include <list>
35#include <queue> 35#include <queue>
36#include <utility> 36#include <utility>
37 37
38#include <openssl/opensslv.h>
38#include <openssl/rand.h> 39#include <openssl/rand.h>
39#include <openssl/evp.h> 40#include <openssl/evp.h>
40#include <openssl/rsa.h> 41#include <openssl/rsa.h>
41#include <openssl/err.h> 42#include <openssl/err.h>
43
44// openssl 0.9.8 compatibility
45#if OPENSSL_VERSION_NUMBER < 0x10100000
46 #define require101(exp) exp
47#else
48 #define require101(exp) equire (exp)
49#endif
42 50
43#include "conf.h" 51#include "conf.h"
44#include "slog.h" 52#include "slog.h"
45#include "device.h" 53#include "device.h"
46#include "vpn.h" 54#include "vpn.h"
126 kdf.extract (s, sizeof (s)); 134 kdf.extract (s, sizeof (s));
127 kdf.extract_done (HKDF_PRF_HASH ()); 135 kdf.extract_done (HKDF_PRF_HASH ());
128 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); 136 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info));
129 137
130 HMAC_CTX_init (&hctx); 138 HMAC_CTX_init (&hctx);
131 require (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); 139 require101 (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0));
132 } 140 }
133 141
134 { 142 {
135 u8 cipher_key[CIPHER_KEYSIZE]; 143 u8 cipher_key[CIPHER_KEYSIZE];
136 static const unsigned char cipher_info[] = "gvpe cipher key"; 144 static const unsigned char cipher_info[] = "gvpe cipher key";
178 186
179 return 1; 187 return 1;
180} 188}
181 189
182static void 190static void
183auth_hash (const auth_data &auth, auth_mac &mac) 191auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
184{ 192{
185 HMAC_CTX ctx; 193 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
186 194 kdf.extract (&auth.rsa, sizeof (auth.rsa));
187 HMAC_CTX_init (&ctx); 195 kdf.extract_done ();
188 require (HMAC_Init_ex (&ctx, auth.rsa.auth_key, sizeof (auth.rsa.auth_key), AUTH_DIGEST (), 0)); 196 kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info
189 require (HMAC_Update (&ctx, (const unsigned char *)&auth, sizeof auth));
190 require (HMAC_Final (&ctx, (unsigned char *)&mac, 0));
191 HMAC_CTX_cleanup (&ctx);
192} 197}
193 198
194void 199void
195connection::generate_auth_data () 200connection::generate_auth_data ()
196{ 201{
197 if (auth_expire < ev_now ()) 202 if (auth_expire < ev_now ())
198 { 203 {
199 // request data 204 // request data
200 rand_fill (snd_auth.rsa); 205 rand_fill (snd_auth.rsa);
201 curve25519_generate (snd_ecdh_a, snd_auth.ecdh); 206 curve25519_generate (snd_ecdh_a, snd_auth.ecdh);
202 auth_hash (snd_auth, snd_auth_mac);
203 207
204 // eventual response data 208 // eventual response data
205 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); 209 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b);
206 } 210 }
207 211
363 } 367 }
364} 368}
365 369
366///////////////////////////////////////////////////////////////////////////// 370/////////////////////////////////////////////////////////////////////////////
367 371
368unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
369
370void 372void
371hmac_packet::hmac_gen (crypto_ctx *ctx) 373hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
372{ 374{
373 unsigned int xlen;
374
375 HMAC_CTX *hctx = &ctx->hctx; 375 HMAC_CTX *hctx = &ctx->hctx;
376 376
377 require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); 377 require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0));
378 require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), 378 require101 (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)));
379 len - sizeof (hmac_packet)));
380 require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen)); 379 require101 (HMAC_Final (hctx, hmac_digest, 0));
381} 380}
382 381
383void 382void
384hmac_packet::hmac_set (crypto_ctx *ctx) 383hmac_packet::hmac_set (crypto_ctx *ctx)
385{ 384{
386 hmac_gen (ctx); 385 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
387 386 hmac_gen (ctx, hmac_digest);
388 memcpy (hmac, hmac_digest, HMACLENGTH); 387 memcpy (hmac, hmac_digest, HMACLENGTH);
389} 388}
390 389
391bool 390bool
392hmac_packet::hmac_chk (crypto_ctx *ctx) 391hmac_packet::hmac_chk (crypto_ctx *ctx)
393{ 392{
394 hmac_gen (ctx); 393 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
395 394 hmac_gen (ctx, hmac_digest);
396 return !memcmp (hmac, hmac_digest, HMACLENGTH); 395 return !memcmp (hmac, hmac_digest, HMACLENGTH);
397} 396}
398 397
399void 398void
400vpn_packet::set_hdr (ptype type_, unsigned int dst) 399vpn_packet::set_hdr (ptype type_, unsigned int dst)
552 } 551 }
553}; 552};
554 553
555struct config_packet : vpn_packet 554struct config_packet : vpn_packet
556{ 555{
556 u8 serial[SERIAL_SIZE];
557 u8 prot_major, prot_minor, randsize; 557 u8 prot_major, prot_minor, randsize;
558 u8 flags, features, pad6, pad7, pad8; 558 u8 flags, features, pad6, pad7, pad8;
559 u32 cipher_nid, mac_nid, auth_nid; 559 u32 cipher_nid, mac_nid, auth_nid;
560 560
561 void setup (ptype type, int dst); 561 void setup (ptype type, int dst);
562 bool chk_config () const; 562 bool chk_config (const conf_node *conf, const sockinfo &rsi) const;
563 563
564 static u8 get_features () 564 static u8 get_features ()
565 { 565 {
566 u8 f = 0; 566 u8 f = 0;
567#if ENABLE_COMPRESSION 567#if ENABLE_COMPRESSION
584 prot_minor = PROTOCOL_MINOR; 584 prot_minor = PROTOCOL_MINOR;
585 randsize = RAND_SIZE; 585 randsize = RAND_SIZE;
586 flags = 0; 586 flags = 0;
587 features = get_features (); 587 features = get_features ();
588 588
589 strncpy ((char *)serial, conf.serial, sizeof (serial));
590
589 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); 591 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ()));
590 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); 592 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ()));
591 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); 593 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ()));
592 594
593 len = sizeof (*this) - sizeof (net_packet); 595 len = sizeof (*this) - sizeof (net_packet);
594 set_hdr (type, dst); 596 set_hdr (type, dst);
595} 597}
596 598
597bool 599bool
598config_packet::chk_config () const 600config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const
599{ 601{
600 if (prot_major != PROTOCOL_MAJOR) 602 if (prot_major != PROTOCOL_MAJOR)
601 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 603 slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"),
604 conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR);
602 else if (randsize != RAND_SIZE) 605 else if (randsize != RAND_SIZE)
603 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 606 slog (L_WARN, _("%s(%s): rand size mismatch (remote %d <=> local %d)"),
607 conf->nodename, (const char *)rsi, randsize, RAND_SIZE);
604 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) 608 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ())))
605 slog (L_WARN, _("cipher algo mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); 609 slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"),
610 conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ()));
606 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) 611 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ())))
607 slog (L_WARN, _("mac algo mismatch (remote %x <=> local %x)"), ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ())); 612 slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"),
613 conf->nodename, (const char *)rsi, ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ()));
608 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) 614 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ())))
609 slog (L_WARN, _("auth algo mismatch (remote %x <=> local %x)"), ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ())); 615 slog (L_WARN, _("%s(%s): auth algo mismatch (remote %x <=> local %x)"),
616 conf->nodename, (const char *)rsi, ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ()));
610 else 617 else
618 {
619 int cmp = memcmp (serial, ::conf.serial, sizeof (serial));
620
621 if (cmp > 0)
622 slog (L_WARN, _("%s(%s): remote serial newer than local serial - outdated config?"),
623 conf->nodename, (const char *)rsi);
624 else if (cmp == 0)
611 return true; 625 return true;
626 }
612 627
613 return false; 628 return false;
614} 629}
615 630
616struct auth_req_packet : config_packet // UNPROTECTED 631struct auth_req_packet : config_packet // UNPROTECTED
630 645
631 len = sizeof (*this) - sizeof (net_packet); 646 len = sizeof (*this) - sizeof (net_packet);
632 } 647 }
633}; 648};
634 649
635struct auth_res_packet : config_packet // UNPROTECTED 650struct auth_res_packet : vpn_packet // UNPROTECTED
636{ 651{
637 auth_response response; 652 auth_response response;
638 653
639 auth_res_packet (int dst) 654 auth_res_packet (int dst)
640 { 655 {
641 config_packet::setup (PT_AUTH_RES, dst); 656 set_hdr (PT_AUTH_RES, dst);
642 657
643 len = sizeof (*this) - sizeof (net_packet); 658 len = sizeof (*this) - sizeof (net_packet);
644 } 659 }
645}; 660};
646 661
686 si = rsi; 701 si = rsi;
687 protocol = rsi.prot; 702 protocol = rsi.prot;
688 703
689 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), 704 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
690 conf->nodename, (const char *)rsi, 705 conf->nodename, (const char *)rsi,
691 is_direct ? "direct" : "forwarded", 706 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
692 PROTOCOL_MAJOR, prot_minor); 707 PROTOCOL_MAJOR, prot_minor);
693 708
694 if (::conf.script_node_up) 709 if (::conf.script_node_up)
695 { 710 {
696 run_script_cb *cb = new run_script_cb; 711 run_script_cb *cb = new run_script_cb;
708 723
709 // make sure rekeying timeouts are slightly asymmetric 724 // make sure rekeying timeouts are slightly asymmetric
710 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 725 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
711 rekey.start (rekey_interval, rekey_interval); 726 rekey.start (rekey_interval, rekey_interval);
712 727
728 hmac_error = 0.;
729
713 keepalive.start (::conf.keepalive); 730 keepalive.start (::conf.keepalive);
714 731
715 // send queued packets 732 // send queued packets
716 if (ictx && octx)
717 {
718 while (tap_packet *p = (tap_packet *)data_queue.get ()) 733 while (tap_packet *p = (tap_packet *)data_queue.get ())
719 { 734 {
720 if (p->len) send_data_packet (p); 735 if (p->len) send_data_packet (p);
721 delete p; 736 delete p;
722 } 737 }
723 738
724 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 739 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
725 { 740 {
726 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 741 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
727 delete p; 742 delete p;
728 }
729 } 743 }
730 744
731 vpn->connection_established (this); 745 vpn->connection_established (this);
732} 746}
733 747
741 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); 755 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
742 protocol = 0; 756 protocol = 0;
743 } 757 }
744 758
745 si.set (conf, protocol); 759 si.set (conf, protocol);
746
747 is_direct = si.valid ();
748} 760}
749 761
750// ensure sockinfo is valid, forward if necessary 762// ensure sockinfo is valid, forward if necessary
751const sockinfo & 763const sockinfo &
752connection::forward_si (const sockinfo &si) const 764connection::forward_si (const sockinfo &si) const
771 783
772void 784void
773connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 785connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
774{ 786{
775 if (!vpn->send_vpn_packet (pkt, si, tos)) 787 if (!vpn->send_vpn_packet (pkt, si, tos))
776 reset_connection (); 788 reset_connection ("packet send error");
777} 789}
778 790
779void 791void
780connection::send_ping (const sockinfo &si, u8 pong) 792connection::send_ping (const sockinfo &si, u8 pong)
781{ 793{
820void 832void
821connection::send_auth_response (const sockinfo &si) 833connection::send_auth_response (const sockinfo &si)
822{ 834{
823 auth_res_packet *pkt = new auth_res_packet (conf->id); 835 auth_res_packet *pkt = new auth_res_packet (conf->id);
824 836
825 auth_hash (rcv_auth, pkt->response.mac);
826 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof (rcv_ecdh_b)); 837 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b);
838 auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac);
827 839
828 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); 840 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si);
829 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 841 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
830 842
831 delete pkt; 843 delete pkt;
857 { 869 {
858 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection 870 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection
859 // and stop trying. should probably be handled by a per-connection expire handler. 871 // and stop trying. should probably be handled by a per-connection expire handler.
860 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) 872 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ())
861 { 873 {
862 reset_connection (); 874 reset_connection ("no demand");
863 return; 875 return;
864 } 876 }
865 877
866 last_establish_attempt = ev_now (); 878 last_establish_attempt = ev_now ();
867 879
869 ? (retry_cnt & 3) + 1 881 ? (retry_cnt & 3) + 1
870 : 1 << (retry_cnt >> 2)); 882 : 1 << (retry_cnt >> 2));
871 883
872 reset_si (); 884 reset_si ();
873 885
874 bool slow = si.prot & PROT_SLOW; 886 bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power);
875 887
876 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 888 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
877 { 889 {
878 /*TODO*/ /* start the timer so we don't recurse endlessly */ 890 /*TODO*/ /* start the timer so we don't recurse endlessly */
879 w.start (1); 891 w.start (1);
889 901
890 slow = slow || (dsi.prot & PROT_SLOW); 902 slow = slow || (dsi.prot & PROT_SLOW);
891 903
892 if (dsi.valid () && auth_rate_limiter.can (dsi)) 904 if (dsi.valid () && auth_rate_limiter.can (dsi))
893 { 905 {
894 if (retry_cnt < 4) 906 // use ping after the first few retries
907 // TODO: on rekeys, the other node might not interpret ping correctly,
908 // TODO: as it will still have a valid connection
909 if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power))
895 send_auth_request (dsi, true); 910 send_auth_request (dsi, true);
896 else 911 else
897 send_ping (dsi, 0); 912 send_ping (dsi, 0);
898 } 913 }
899 } 914 }
900 915
901 retry_int *= slow ? 8. : 0.9; 916 retry_int *= slow ? 4. : 0.9;
902 917
903 if (retry_int < conf->max_retry) 918 if (retry_int < conf->max_retry)
904 retry_cnt++; 919 retry_cnt++;
905 else 920 else
906 retry_int = conf->max_retry; 921 retry_int = conf->max_retry;
908 w.start (retry_int); 923 w.start (retry_int);
909 } 924 }
910} 925}
911 926
912void 927void
913connection::reset_connection () 928connection::reset_connection (const char *reason)
914{ 929{
915 if (ictx && octx) 930 if (ictx && octx)
916 { 931 {
917 slog (L_INFO, _("%s(%s): connection lost"), 932 slog (L_INFO, _("%s(%s): connection lost (%s)"),
918 conf->nodename, (const char *)si); 933 conf->nodename, (const char *)si, reason);
919 934
920 if (::conf.script_node_down) 935 if (::conf.script_node_down)
921 { 936 {
922 run_script_cb *cb = new run_script_cb; 937 run_script_cb *cb = new run_script_cb;
923 cb->set<connection, &connection::script_node_down> (this); 938 cb->set<connection, &connection::script_node_down> (this);
947connection::shutdown () 962connection::shutdown ()
948{ 963{
949 if (ictx && octx) 964 if (ictx && octx)
950 send_reset (si); 965 send_reset (si);
951 966
952 reset_connection (); 967 reset_connection ("shutdown");
953} 968}
954 969
955// poor-man's rekeying 970// poor-man's rekeying
956inline void 971inline void
957connection::rekey_cb (ev::timer &w, int revents) 972connection::rekey_cb (ev::timer &w, int revents)
958{ 973{
959 reset_connection (); 974 reset_connection ("rekeying");
960 establish_connection (); 975 establish_connection ();
961} 976}
962 977
963void 978void
964connection::send_data_packet (tap_packet *pkt) 979connection::send_data_packet (tap_packet *pkt)
981 996
982void 997void
983connection::post_inject_queue () 998connection::post_inject_queue ()
984{ 999{
985 // force a connection every now and when when packets are sent (max 1/s) 1000 // force a connection every now and when when packets are sent (max 1/s)
986 if (ev_now () - last_establish_attempt >= 0.95) // arbitrary 1001 if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary
987 establish_connection.stop (); 1002 establish_connection.stop ();
988 1003
989 establish_connection (); 1004 establish_connection ();
990} 1005}
991 1006
1051 // about our desire for communication. 1066 // about our desire for communication.
1052 establish_connection (); 1067 establish_connection ();
1053 break; 1068 break;
1054 1069
1055 case vpn_packet::PT_RESET: 1070 case vpn_packet::PT_RESET:
1071 slog (L_TRACE, "%s >> PT_RESET", conf->nodename);
1072
1073 if (ictx && octx)
1056 { 1074 {
1057 reset_connection (); 1075 reset_connection ("remote reset");
1058 1076
1059 config_packet *p = (config_packet *) pkt; 1077 config_packet *p = (config_packet *) pkt;
1060 1078
1061 if (!p->chk_config ()) 1079 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS)
1062 {
1063 slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."),
1064 conf->nodename, (const char *)rsi);
1065 connectmode = conf_node::C_DISABLED;
1066 }
1067 else if (connectmode == conf_node::C_ALWAYS)
1068 establish_connection (); 1080 establish_connection ();
1069 } 1081 }
1082
1070 break; 1083 break;
1071 1084
1072 case vpn_packet::PT_AUTH_REQ: 1085 case vpn_packet::PT_AUTH_REQ:
1073 if (auth_rate_limiter.can (rsi)) 1086 if (auth_rate_limiter.can (rsi))
1074 { 1087 {
1076 1089
1077 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", 1090 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1078 conf->nodename, p->initiate ? "initiate" : "reply", 1091 conf->nodename, p->initiate ? "initiate" : "reply",
1079 p->protocols, p->features); 1092 p->protocols, p->features);
1080 1093
1081 if (p->chk_config () && !memcmp (p->magic, MAGIC, 8)) 1094 if (memcmp (p->magic, MAGIC, 8))
1095 {
1096 slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"),
1097 conf->nodename, (const char *)rsi);
1098 }
1099 else if (p->chk_config (conf, rsi))
1082 { 1100 {
1083 if (p->prot_minor != PROTOCOL_MINOR) 1101 if (p->prot_minor != PROTOCOL_MINOR)
1084 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1102 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1085 conf->nodename, (const char *)rsi, 1103 conf->nodename, (const char *)rsi,
1086 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1104 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1087 1105
1088 if (p->initiate) 1106 if (p->initiate)
1107 {
1089 send_auth_request (rsi, false); 1108 send_auth_request (rsi, false);
1109
1110 if (ictx && octx)
1111 reset_connection ("reconnect");
1112 }
1090 1113
1091 auth_data auth; 1114 auth_data auth;
1092 1115
1093 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1116 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1094 { 1117 {
1113 } 1136 }
1114 } 1137 }
1115 1138
1116 break; 1139 break;
1117 } 1140 }
1118 else
1119 slog (L_WARN, _("%s(%s): protocol mismatch."),
1120 conf->nodename, (const char *)rsi);
1121 1141
1122 send_reset (rsi); 1142 send_reset (rsi);
1123 } 1143 }
1124 1144
1125 break; 1145 break;
1128 { 1148 {
1129 auth_res_packet *p = (auth_res_packet *)pkt; 1149 auth_res_packet *p = (auth_res_packet *)pkt;
1130 1150
1131 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); 1151 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1132 1152
1133 if (p->chk_config ()) 1153 auth_mac local_mac;
1154 auth_hash (snd_auth, p->response.ecdh, local_mac);
1155
1156 if (memcmp (&p->response.mac, local_mac, sizeof local_mac))
1134 { 1157 {
1135 if (memcmp (&p->response.mac, snd_auth_mac, sizeof (snd_auth_mac)))
1136 {
1137 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), 1158 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."),
1138 conf->nodename, (const char *)rsi); 1159 conf->nodename, (const char *)rsi);
1139 } 1160 }
1140 else if (!have_snd_auth) 1161 else if (!have_snd_auth)
1141 { 1162 {
1142 if (p->prot_minor != PROTOCOL_MINOR)
1143 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1144 conf->nodename, (const char *)rsi,
1145 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1146
1147 prot_minor = p->prot_minor;
1148 memcpy (snd_ecdh_b, p->response.ecdh, sizeof (snd_ecdh_b)); 1163 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1149 1164
1150 have_snd_auth = true; 1165 have_snd_auth = true;
1151 connection_established (rsi); 1166 connection_established (rsi);
1152 }
1153
1154 break;
1155 } 1167 }
1156 } 1168 }
1157
1158 send_reset (rsi);
1159 break; 1169 break;
1160 1170
1161 case vpn_packet::PT_DATA_COMPRESSED: 1171 case vpn_packet::PT_DATA_COMPRESSED:
1162#if !ENABLE_COMPRESSION 1172#if !ENABLE_COMPRESSION
1163 send_reset (rsi); 1173 send_reset (rsi);
1169 if (ictx && octx) 1179 if (ictx && octx)
1170 { 1180 {
1171 vpndata_packet *p = (vpndata_packet *)pkt; 1181 vpndata_packet *p = (vpndata_packet *)pkt;
1172 1182
1173 if (!p->hmac_chk (ictx)) 1183 if (!p->hmac_chk (ictx))
1184 {
1185 // rekeying often creates temporary hmac auth floods
1186 // we assume they don't take longer than a few seconds normally,
1187 // and suppress messages and resets during that time.
1188 //TODO: should be done per source address
1189 if (!hmac_error)
1190 {
1191 hmac_error = ev_now () + 3;
1192 break;
1193 }
1194 else if (hmac_error >= ev_now ())
1195 break; // silently suppress
1196 else
1197 {
1174 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1198 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1175 "could be an attack, or just corruption or a synchronization error."), 1199 "could be an attack, or just corruption or a synchronization error."),
1176 conf->nodename, (const char *)rsi); 1200 conf->nodename, (const char *)rsi);
1201 // reset
1202 }
1203 }
1177 else 1204 else
1178 { 1205 {
1179 u32 seqno; 1206 u32 seqno;
1180 tap_packet *d = p->unpack (this, seqno); 1207 tap_packet *d = p->unpack (this, seqno);
1181 int seqclass = iseqno.seqno_classify (seqno); 1208 int seqclass = iseqno.seqno_classify (seqno);
1209
1210 hmac_error = 0;
1182 1211
1183 if (seqclass == 0) // ok 1212 if (seqclass == 0) // ok
1184 { 1213 {
1185 vpn->tap->send (d); 1214 vpn->tap->send (d);
1186 1215
1313 1342
1314 if (when >= 0) 1343 if (when >= 0)
1315 w.start (when); 1344 w.start (when);
1316 else if (when < -15) 1345 else if (when < -15)
1317 { 1346 {
1318 reset_connection (); 1347 reset_connection ("keepalive overdue");
1319 establish_connection (); 1348 establish_connection ();
1320 } 1349 }
1321 else if (conf->connectmode != conf_node::C_ONDEMAND 1350 else if (conf->connectmode != conf_node::C_ONDEMAND
1322 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1351 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1323 { 1352 {
1327 else if (when >= -10) 1356 else if (when >= -10)
1328 // hold ondemand connections implicitly a few seconds longer 1357 // hold ondemand connections implicitly a few seconds longer
1329 // should delete octx, though, or something like that ;) 1358 // should delete octx, though, or something like that ;)
1330 w.start (when + 10); 1359 w.start (when + 10);
1331 else 1360 else
1332 reset_connection (); 1361 reset_connection ("keepalive timeout");
1333} 1362}
1334 1363
1335void 1364void
1336connection::send_connect_request (int id) 1365connection::send_connect_request (int id)
1337{ 1366{
1437 1466
1438 // queue a dummy packet to force an initial connection attempt 1467 // queue a dummy packet to force an initial connection attempt
1439 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1468 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1440 vpn_queue.put (new net_packet); 1469 vpn_queue.put (new net_packet);
1441 1470
1442 reset_connection (); 1471 reset_connection ("startup");
1443} 1472}
1444 1473
1445connection::~connection () 1474connection::~connection ()
1446{ 1475{
1447 shutdown (); 1476 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines