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.98 by root, Tue Jul 16 16:44:36 2013 UTC vs.
Revision 1.112 by root, Fri Sep 12 10:40:43 2014 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) require (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"
118 126
119 { 127 {
120 u8 mac_key[MAC_KEYSIZE]; 128 u8 mac_key[MAC_KEYSIZE];
121 static const unsigned char mac_info[] = "gvpe mac key"; 129 static const unsigned char mac_info[] = "gvpe mac key";
122 130
123 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt)); 131 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ());
124 kdf.extract (auth2.rsa.ikm, sizeof (auth2.rsa.ikm));
125 kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key)); 132 kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key));
126 kdf.extract (s, sizeof (s)); 133 kdf.extract (s, sizeof (s));
127 kdf.extract_done (); 134 kdf.extract_done (HKDF_PRF_HASH ());
128 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); 135 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info));
129 136
130 HMAC_CTX_init (&hctx); 137 HMAC_CTX_init (&hctx);
131 require (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); 138 require101 (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0));
132 } 139 }
133 140
134 { 141 {
135 u8 cipher_key[CIPHER_KEYSIZE]; 142 u8 cipher_key[CIPHER_KEYSIZE];
136 static const unsigned char cipher_info[] = "gvpe cipher key"; 143 static const unsigned char cipher_info[] = "gvpe cipher key";
137 144
138 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt)); 145 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ());
139 kdf.extract (auth2.rsa.ikm, sizeof (auth2.rsa.ikm));
140 kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key)); 146 kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key));
141 kdf.extract (s, sizeof (s)); 147 kdf.extract (s, sizeof (s));
142 kdf.extract_done (); 148 kdf.extract_done (HKDF_PRF_HASH ());
143 kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); 149 kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info));
144 150
145 EVP_CIPHER_CTX_init (&cctx); 151 EVP_CIPHER_CTX_init (&cctx);
146 require (EVP_CipherInit_ex (&cctx, CIPHER (), 0, cipher_key, 0, enc)); 152 require (EVP_CipherInit_ex (&cctx, CIPHER (), 0, cipher_key, 0, enc));
147 } 153 }
158{ 164{
159 if (RSA_public_encrypt (sizeof (auth.rsa), 165 if (RSA_public_encrypt (sizeof (auth.rsa),
160 (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa, 166 (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa,
161 key, RSA_PKCS1_OAEP_PADDING) < 0) 167 key, RSA_PKCS1_OAEP_PADDING) < 0)
162 fatal ("RSA_public_encrypt error"); 168 fatal ("RSA_public_encrypt error");
169
170 memcpy (&encr.ecdh, &auth.ecdh, sizeof (encr.ecdh));
163} 171}
164 172
165static inline bool 173static inline bool
166auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth) 174auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth)
167{ 175{
171 (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt, 179 (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt,
172 key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa)) 180 key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa))
173 return 0; 181 return 0;
174 182
175 memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa)); 183 memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa));
184 memcpy (&auth.ecdh, &encr.ecdh, sizeof (auth.ecdh));
176 185
177 return 1; 186 return 1;
178} 187}
179 188
180static void 189static void
181auth_hash (const auth_data &auth, auth_mac &mac) 190auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
182{ 191{
183 HMAC_CTX ctx; 192 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
184 193 kdf.extract (&auth.rsa, sizeof (auth.rsa));
185 HMAC_CTX_init (&ctx); 194 kdf.extract_done ();
186 require (HMAC_Init_ex (&ctx, auth.rsa.auth_key, sizeof (auth.rsa.auth_key), AUTH_DIGEST (), 0)); 195 kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info
187 require (HMAC_Update (&ctx, (const unsigned char *)&auth, sizeof auth));
188 require (HMAC_Final (&ctx, (unsigned char *)&mac, 0));
189 HMAC_CTX_cleanup (&ctx);
190} 196}
191 197
192void 198void
193connection::generate_auth_data () 199connection::generate_auth_data ()
194{ 200{
195 if (auth_expire < ev_now ()) 201 if (auth_expire < ev_now ())
196 { 202 {
197 // request data 203 // request data
198 RAND_pseudo_bytes ((unsigned char *)&snd_auth.rsa, sizeof snd_auth.rsa); 204 rand_fill (snd_auth.rsa);
199 curve25519_generate (snd_ecdh_a, snd_auth.ecdh); 205 curve25519_generate (snd_ecdh_a, snd_auth.ecdh);
200 auth_hash (snd_auth, snd_auth_mac);
201 206
202 // eventual response data 207 // eventual response data
203 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); 208 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b);
204 } 209 }
205 210
361 } 366 }
362} 367}
363 368
364///////////////////////////////////////////////////////////////////////////// 369/////////////////////////////////////////////////////////////////////////////
365 370
366unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
367
368void 371void
369hmac_packet::hmac_gen (crypto_ctx *ctx) 372hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
370{ 373{
371 unsigned int xlen;
372
373 HMAC_CTX *hctx = &ctx->hctx; 374 HMAC_CTX *hctx = &ctx->hctx;
374 375
375 require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); 376 require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0));
376 require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), 377 require101 (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)));
377 len - sizeof (hmac_packet)));
378 require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen)); 378 require101 (HMAC_Final (hctx, hmac_digest, 0));
379} 379}
380 380
381void 381void
382hmac_packet::hmac_set (crypto_ctx *ctx) 382hmac_packet::hmac_set (crypto_ctx *ctx)
383{ 383{
384 hmac_gen (ctx); 384 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
385 385 hmac_gen (ctx, hmac_digest);
386 memcpy (hmac, hmac_digest, HMACLENGTH); 386 memcpy (hmac, hmac_digest, HMACLENGTH);
387} 387}
388 388
389bool 389bool
390hmac_packet::hmac_chk (crypto_ctx *ctx) 390hmac_packet::hmac_chk (crypto_ctx *ctx)
391{ 391{
392 hmac_gen (ctx); 392 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
393 393 hmac_gen (ctx, hmac_digest);
394 return !memcmp (hmac, hmac_digest, HMACLENGTH); 394 return slow_memeq (hmac, hmac_digest, HMACLENGTH);
395} 395}
396 396
397void 397void
398vpn_packet::set_hdr (ptype type_, unsigned int dst) 398vpn_packet::set_hdr (ptype type_, unsigned int dst)
399{ 399{
458 u32 seqno; 458 u32 seqno;
459 } datahdr; 459 } datahdr;
460 460
461 datahdr.seqno = ntohl (seqno); 461 datahdr.seqno = ntohl (seqno);
462#if RAND_SIZE 462#if RAND_SIZE
463 RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); 463 // NB: a constant (per session) random prefix
464 // is likely enough, but we don't take any chances.
465 conn->oiv.get (datahdr.rnd, RAND_SIZE);
464#endif 466#endif
465 467
466 require (EVP_EncryptUpdate (cctx, 468 require (EVP_EncryptUpdate (cctx,
467 (unsigned char *) data + outl, &outl2, 469 (unsigned char *) data + outl, &outl2,
468 (unsigned char *) &datahdr, DATAHDR)); 470 (unsigned char *) &datahdr, DATAHDR));
499 501
500 if (type == PT_DATA_COMPRESSED) 502 if (type == PT_DATA_COMPRESSED)
501 d = cdata; 503 d = cdata;
502 else 504 else
503#endif 505#endif
504 d = &(*p)[6 + 6 - DATAHDR]; 506 d = &(*p)[6 + 6] - DATAHDR;
505 507
506 /* this overwrites part of the src mac, but we fix that later */ 508 // we play do evil games with the struct layout atm.
509 // pending better solutions, we at least do some verification.
510 // this is fine, as we left ISO territory long ago.
511 require (DATAHDR <= 16);
512 require ((u8 *)(&p->len + 1) == &(*p)[0]);
513
514 // this can overwrite the len/dst/src fields
507 require (EVP_DecryptUpdate (cctx, 515 require (EVP_DecryptUpdate (cctx,
508 d, &outl2, 516 d, &outl2,
509 (unsigned char *)&data, len - data_hdr_size ())); 517 (unsigned char *)&data, len - data_hdr_size ()));
510 outl += outl2; 518 outl += outl2;
511 519
542 } 550 }
543}; 551};
544 552
545struct config_packet : vpn_packet 553struct config_packet : vpn_packet
546{ 554{
555 u8 serial[SERIAL_SIZE];
547 u8 prot_major, prot_minor, randsize; 556 u8 prot_major, prot_minor, randsize;
548 u8 flags, features, pad6, pad7, pad8; 557 u8 flags, features, pad6, pad7, pad8;
549 u32 cipher_nid, mac_nid, auth_nid; 558 u32 cipher_nid, mac_nid, auth_nid;
550 559
551 void setup (ptype type, int dst); 560 void setup (ptype type, int dst);
552 bool chk_config () const; 561 bool chk_config (const conf_node *conf, const sockinfo &rsi) const;
553 562
554 static u8 get_features () 563 static u8 get_features ()
555 { 564 {
556 u8 f = 0; 565 u8 f = 0;
557#if ENABLE_COMPRESSION 566#if ENABLE_COMPRESSION
574 prot_minor = PROTOCOL_MINOR; 583 prot_minor = PROTOCOL_MINOR;
575 randsize = RAND_SIZE; 584 randsize = RAND_SIZE;
576 flags = 0; 585 flags = 0;
577 features = get_features (); 586 features = get_features ();
578 587
588 strncpy ((char *)serial, conf.serial, sizeof (serial));
589
579 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); 590 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ()));
580 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); 591 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ()));
581 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); 592 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ()));
582 593
583 len = sizeof (*this) - sizeof (net_packet); 594 len = sizeof (*this) - sizeof (net_packet);
584 set_hdr (type, dst); 595 set_hdr (type, dst);
585} 596}
586 597
587bool 598bool
588config_packet::chk_config () const 599config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const
589{ 600{
590 if (prot_major != PROTOCOL_MAJOR) 601 if (prot_major != PROTOCOL_MAJOR)
591 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 602 slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"),
603 conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR);
592 else if (randsize != RAND_SIZE) 604 else if (randsize != RAND_SIZE)
593 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 605 slog (L_WARN, _("%s(%s): rand size mismatch (remote %d <=> local %d)"),
606 conf->nodename, (const char *)rsi, randsize, RAND_SIZE);
594 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) 607 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ())))
595 slog (L_WARN, _("cipher algo mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); 608 slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"),
609 conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ()));
596 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) 610 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ())))
597 slog (L_WARN, _("mac algo mismatch (remote %x <=> local %x)"), ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ())); 611 slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"),
612 conf->nodename, (const char *)rsi, ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ()));
598 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) 613 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ())))
599 slog (L_WARN, _("auth algo mismatch (remote %x <=> local %x)"), ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ())); 614 slog (L_WARN, _("%s(%s): auth algo mismatch (remote %x <=> local %x)"),
615 conf->nodename, (const char *)rsi, ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ()));
600 else 616 else
617 {
618 int cmp = memcmp (serial, ::conf.serial, sizeof (serial));
619
620 if (cmp > 0)
621 slog (L_WARN, _("%s(%s): remote serial newer than local serial - outdated config?"),
622 conf->nodename, (const char *)rsi);
623 else if (cmp == 0)
601 return true; 624 return true;
625 }
602 626
603 return false; 627 return false;
604} 628}
605 629
606struct auth_req_packet : config_packet // UNPROTECTED 630struct auth_req_packet : config_packet // UNPROTECTED
620 644
621 len = sizeof (*this) - sizeof (net_packet); 645 len = sizeof (*this) - sizeof (net_packet);
622 } 646 }
623}; 647};
624 648
625struct auth_res_packet : config_packet // UNPROTECTED 649struct auth_res_packet : vpn_packet // UNPROTECTED
626{ 650{
627 auth_response response; 651 auth_response response;
628 652
629 auth_res_packet (int dst) 653 auth_res_packet (int dst)
630 { 654 {
631 config_packet::setup (PT_AUTH_RES, dst); 655 set_hdr (PT_AUTH_RES, dst);
632 656
633 len = sizeof (*this) - sizeof (net_packet); 657 len = sizeof (*this) - sizeof (net_packet);
634 } 658 }
635}; 659};
636 660
676 si = rsi; 700 si = rsi;
677 protocol = rsi.prot; 701 protocol = rsi.prot;
678 702
679 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), 703 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
680 conf->nodename, (const char *)rsi, 704 conf->nodename, (const char *)rsi,
681 is_direct ? "direct" : "forwarded", 705 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
682 PROTOCOL_MAJOR, prot_minor); 706 PROTOCOL_MAJOR, prot_minor);
683 707
684 if (::conf.script_node_up) 708 if (::conf.script_node_up)
685 { 709 {
686 run_script_cb *cb = new run_script_cb; 710 run_script_cb *cb = new run_script_cb;
692 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); 716 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff);
693 717
694 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); 718 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1);
695 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; 719 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff;
696 720
697 if (ictx && octx) 721 oiv.reset ();
698 { 722
699 // make sure rekeying timeouts are slightly asymmetric 723 // make sure rekeying timeouts are slightly asymmetric
700 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 724 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
701 rekey.start (rekey_interval, rekey_interval); 725 rekey.start (rekey_interval, rekey_interval);
702 726
727 hmac_error = 0.;
728
703 keepalive.start (::conf.keepalive); 729 keepalive.start (::conf.keepalive);
704 730
705 // send queued packets 731 // send queued packets
706 if (ictx && octx)
707 {
708 while (tap_packet *p = (tap_packet *)data_queue.get ()) 732 while (tap_packet *p = (tap_packet *)data_queue.get ())
709 { 733 {
710 if (p->len) send_data_packet (p); 734 if (p->len) send_data_packet (p);
711 delete p; 735 delete p;
712 } 736 }
713 737
714 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 738 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
715 { 739 {
716 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 740 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
717 delete p; 741 delete p;
718 } 742 }
719 }
720 743
721 vpn->connection_established (this); 744 vpn->connection_established (this);
722 }
723#if 0
724 else
725 {
726 retry_cnt = 0;
727 establish_connection.start (5);
728 keepalive.stop ();
729 rekey.stop ();
730 }
731#endif
732} 745}
733 746
734void 747void
735connection::reset_si () 748connection::reset_si ()
736{ 749{
741 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); 754 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
742 protocol = 0; 755 protocol = 0;
743 } 756 }
744 757
745 si.set (conf, protocol); 758 si.set (conf, protocol);
746
747 is_direct = si.valid ();
748} 759}
749 760
750// ensure sockinfo is valid, forward if necessary 761// ensure sockinfo is valid, forward if necessary
751const sockinfo & 762const sockinfo &
752connection::forward_si (const sockinfo &si) const 763connection::forward_si (const sockinfo &si) const
771 782
772void 783void
773connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 784connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
774{ 785{
775 if (!vpn->send_vpn_packet (pkt, si, tos)) 786 if (!vpn->send_vpn_packet (pkt, si, tos))
776 reset_connection (); 787 reset_connection ("packet send error");
777} 788}
778 789
779void 790void
780connection::send_ping (const sockinfo &si, u8 pong) 791connection::send_ping (const sockinfo &si, u8 pong)
781{ 792{
820void 831void
821connection::send_auth_response (const sockinfo &si) 832connection::send_auth_response (const sockinfo &si)
822{ 833{
823 auth_res_packet *pkt = new auth_res_packet (conf->id); 834 auth_res_packet *pkt = new auth_res_packet (conf->id);
824 835
825 auth_hash (rcv_auth, pkt->response.mac);
826 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof (rcv_ecdh_b)); 836 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b);
837 auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac);
827 838
828 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); 839 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 840 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
830 841
831 delete pkt; 842 delete pkt;
857 { 868 {
858 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection 869 // 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. 870 // 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 ()) 871 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ())
861 { 872 {
862 reset_connection (); 873 reset_connection ("no demand");
863 return; 874 return;
864 } 875 }
865 876
866 last_establish_attempt = ev_now (); 877 last_establish_attempt = ev_now ();
867 878
869 ? (retry_cnt & 3) + 1 880 ? (retry_cnt & 3) + 1
870 : 1 << (retry_cnt >> 2)); 881 : 1 << (retry_cnt >> 2));
871 882
872 reset_si (); 883 reset_si ();
873 884
874 bool slow = si.prot & PROT_SLOW; 885 bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power);
875 886
876 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 887 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
877 { 888 {
878 /*TODO*/ /* start the timer so we don't recurse endlessly */ 889 /*TODO*/ /* start the timer so we don't recurse endlessly */
879 w.start (1); 890 w.start (1);
889 900
890 slow = slow || (dsi.prot & PROT_SLOW); 901 slow = slow || (dsi.prot & PROT_SLOW);
891 902
892 if (dsi.valid () && auth_rate_limiter.can (dsi)) 903 if (dsi.valid () && auth_rate_limiter.can (dsi))
893 { 904 {
894 if (retry_cnt < 4) 905 // use ping after the first few retries
906 // TODO: on rekeys, the other node might not interpret ping correctly,
907 // TODO: as it will still have a valid connection
908 if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power))
895 send_auth_request (dsi, true); 909 send_auth_request (dsi, true);
896 else 910 else
897 send_ping (dsi, 0); 911 send_ping (dsi, 0);
898 } 912 }
899 } 913 }
900 914
901 retry_int *= slow ? 8. : 0.9; 915 retry_int *= slow ? 4. : 0.9;
902 916
903 if (retry_int < conf->max_retry) 917 if (retry_int < conf->max_retry)
904 retry_cnt++; 918 retry_cnt++;
905 else 919 else
906 retry_int = conf->max_retry; 920 retry_int = conf->max_retry;
908 w.start (retry_int); 922 w.start (retry_int);
909 } 923 }
910} 924}
911 925
912void 926void
913connection::reset_connection () 927connection::reset_connection (const char *reason)
914{ 928{
915 if (ictx && octx) 929 if (ictx && octx)
916 { 930 {
917 slog (L_INFO, _("%s(%s): connection lost"), 931 slog (L_INFO, _("%s(%s): connection lost (%s)"),
918 conf->nodename, (const char *)si); 932 conf->nodename, (const char *)si, reason);
919 933
920 if (::conf.script_node_down) 934 if (::conf.script_node_down)
921 { 935 {
922 run_script_cb *cb = new run_script_cb; 936 run_script_cb *cb = new run_script_cb;
923 cb->set<connection, &connection::script_node_down> (this); 937 cb->set<connection, &connection::script_node_down> (this);
947connection::shutdown () 961connection::shutdown ()
948{ 962{
949 if (ictx && octx) 963 if (ictx && octx)
950 send_reset (si); 964 send_reset (si);
951 965
952 reset_connection (); 966 reset_connection ("shutdown");
953} 967}
954 968
955// poor-man's rekeying 969// poor-man's rekeying
956inline void 970inline void
957connection::rekey_cb (ev::timer &w, int revents) 971connection::rekey_cb (ev::timer &w, int revents)
958{ 972{
959 reset_connection (); 973 reset_connection ("rekeying");
960 establish_connection (); 974 establish_connection ();
961} 975}
962 976
963void 977void
964connection::send_data_packet (tap_packet *pkt) 978connection::send_data_packet (tap_packet *pkt)
981 995
982void 996void
983connection::post_inject_queue () 997connection::post_inject_queue ()
984{ 998{
985 // force a connection every now and when when packets are sent (max 1/s) 999 // force a connection every now and when when packets are sent (max 1/s)
986 if (ev_now () - last_establish_attempt >= 0.95) // arbitrary 1000 if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary
987 establish_connection.stop (); 1001 establish_connection.stop ();
988 1002
989 establish_connection (); 1003 establish_connection ();
990} 1004}
991 1005
1051 // about our desire for communication. 1065 // about our desire for communication.
1052 establish_connection (); 1066 establish_connection ();
1053 break; 1067 break;
1054 1068
1055 case vpn_packet::PT_RESET: 1069 case vpn_packet::PT_RESET:
1070 slog (L_TRACE, "%s >> PT_RESET", conf->nodename);
1071
1072 if (ictx && octx)
1056 { 1073 {
1057 reset_connection (); 1074 reset_connection ("remote reset");
1058 1075
1059 config_packet *p = (config_packet *) pkt; 1076 config_packet *p = (config_packet *) pkt;
1060 1077
1061 if (!p->chk_config ()) 1078 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 (); 1079 establish_connection ();
1069 } 1080 }
1081
1070 break; 1082 break;
1071 1083
1072 case vpn_packet::PT_AUTH_REQ: 1084 case vpn_packet::PT_AUTH_REQ:
1073 if (auth_rate_limiter.can (rsi)) 1085 if (auth_rate_limiter.can (rsi))
1074 { 1086 {
1076 1088
1077 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", 1089 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1078 conf->nodename, p->initiate ? "initiate" : "reply", 1090 conf->nodename, p->initiate ? "initiate" : "reply",
1079 p->protocols, p->features); 1091 p->protocols, p->features);
1080 1092
1081 if (p->chk_config () && !memcmp (p->magic, MAGIC, 8)) 1093 if (memcmp (p->magic, MAGIC, 8))
1094 {
1095 slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"),
1096 conf->nodename, (const char *)rsi);
1097 }
1098 else if (p->chk_config (conf, rsi))
1082 { 1099 {
1083 if (p->prot_minor != PROTOCOL_MINOR) 1100 if (p->prot_minor != PROTOCOL_MINOR)
1084 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1101 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1085 conf->nodename, (const char *)rsi, 1102 conf->nodename, (const char *)rsi,
1086 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1103 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1087 1104
1088 if (p->initiate) 1105 if (p->initiate)
1106 {
1089 send_auth_request (rsi, false); 1107 send_auth_request (rsi, false);
1108
1109 if (ictx && octx)
1110 reset_connection ("reconnect");
1111 }
1090 1112
1091 auth_data auth; 1113 auth_data auth;
1092 1114
1093 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1115 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1094 { 1116 {
1095 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), 1117 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"),
1096 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); 1118 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0));
1097 } 1119 }
1098 else 1120 else
1099 { 1121 {
1100 bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); 1122 bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth);
1101 1123
1102 rcv_auth = auth; 1124 rcv_auth = auth;
1103 have_rcv_auth = true; 1125 have_rcv_auth = true;
1104 1126
1105 send_auth_response (rsi); 1127 send_auth_response (rsi);
1113 } 1135 }
1114 } 1136 }
1115 1137
1116 break; 1138 break;
1117 } 1139 }
1118 else
1119 slog (L_WARN, _("%s(%s): protocol mismatch."),
1120 conf->nodename, (const char *)rsi);
1121 1140
1122 send_reset (rsi); 1141 send_reset (rsi);
1123 } 1142 }
1124 1143
1125 break; 1144 break;
1128 { 1147 {
1129 auth_res_packet *p = (auth_res_packet *)pkt; 1148 auth_res_packet *p = (auth_res_packet *)pkt;
1130 1149
1131 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); 1150 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1132 1151
1133 if (p->chk_config ()) 1152 auth_mac local_mac;
1153 auth_hash (snd_auth, p->response.ecdh, local_mac);
1154
1155 if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac))
1134 { 1156 {
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."), 1157 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."),
1138 conf->nodename, (const char *)rsi); 1158 conf->nodename, (const char *)rsi);
1139 } 1159 }
1140 else if (!have_snd_auth) 1160 else if (!have_snd_auth)
1141 { 1161 {
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)); 1162 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1149 1163
1150 have_snd_auth = true; 1164 have_snd_auth = true;
1151 connection_established (rsi); 1165 connection_established (rsi);
1152 }
1153
1154 break;
1155 } 1166 }
1156 } 1167 }
1157
1158 send_reset (rsi);
1159 break; 1168 break;
1160 1169
1161 case vpn_packet::PT_DATA_COMPRESSED: 1170 case vpn_packet::PT_DATA_COMPRESSED:
1162#if !ENABLE_COMPRESSION 1171#if !ENABLE_COMPRESSION
1163 send_reset (rsi); 1172 send_reset (rsi);
1169 if (ictx && octx) 1178 if (ictx && octx)
1170 { 1179 {
1171 vpndata_packet *p = (vpndata_packet *)pkt; 1180 vpndata_packet *p = (vpndata_packet *)pkt;
1172 1181
1173 if (!p->hmac_chk (ictx)) 1182 if (!p->hmac_chk (ictx))
1183 {
1184 // rekeying often creates temporary hmac auth floods
1185 // we assume they don't take longer than a few seconds normally,
1186 // and suppress messages and resets during that time.
1187 //TODO: should be done per source address
1188 if (!hmac_error)
1189 {
1190 hmac_error = ev_now () + 3;
1191 break;
1192 }
1193 else if (hmac_error >= ev_now ())
1194 break; // silently suppress
1195 else
1196 {
1174 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1197 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1175 "could be an attack, or just corruption or a synchronization error."), 1198 "could be an attack, or just corruption or a synchronization error."),
1176 conf->nodename, (const char *)rsi); 1199 conf->nodename, (const char *)rsi);
1200 // reset
1201 }
1202 }
1177 else 1203 else
1178 { 1204 {
1179 u32 seqno; 1205 u32 seqno;
1180 tap_packet *d = p->unpack (this, seqno); 1206 tap_packet *d = p->unpack (this, seqno);
1181 int seqclass = iseqno.seqno_classify (seqno); 1207 int seqclass = iseqno.seqno_classify (seqno);
1208
1209 hmac_error = 0;
1182 1210
1183 if (seqclass == 0) // ok 1211 if (seqclass == 0) // ok
1184 { 1212 {
1185 vpn->tap->send (d); 1213 vpn->tap->send (d);
1186 1214
1313 1341
1314 if (when >= 0) 1342 if (when >= 0)
1315 w.start (when); 1343 w.start (when);
1316 else if (when < -15) 1344 else if (when < -15)
1317 { 1345 {
1318 reset_connection (); 1346 reset_connection ("keepalive overdue");
1319 establish_connection (); 1347 establish_connection ();
1320 } 1348 }
1321 else if (conf->connectmode != conf_node::C_ONDEMAND 1349 else if (conf->connectmode != conf_node::C_ONDEMAND
1322 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1350 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1323 { 1351 {
1327 else if (when >= -10) 1355 else if (when >= -10)
1328 // hold ondemand connections implicitly a few seconds longer 1356 // hold ondemand connections implicitly a few seconds longer
1329 // should delete octx, though, or something like that ;) 1357 // should delete octx, though, or something like that ;)
1330 w.start (when + 10); 1358 w.start (when + 10);
1331 else 1359 else
1332 reset_connection (); 1360 reset_connection ("keepalive timeout");
1333} 1361}
1334 1362
1335void 1363void
1336connection::send_connect_request (int id) 1364connection::send_connect_request (int id)
1337{ 1365{
1437 1465
1438 // queue a dummy packet to force an initial connection attempt 1466 // queue a dummy packet to force an initial connection attempt
1439 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1467 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1440 vpn_queue.put (new net_packet); 1468 vpn_queue.put (new net_packet);
1441 1469
1442 reset_connection (); 1470 reset_connection ("startup");
1443} 1471}
1444 1472
1445connection::~connection () 1473connection::~connection ()
1446{ 1474{
1447 shutdown (); 1475 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines