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.99 by root, Wed Jul 17 04:36:03 2013 UTC vs.
Revision 1.108 by root, Fri Oct 11 04:07:24 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"
48#include "hkdf.h" 56#include "hkdf.h"
49 57
50#include "netcompat.h" 58#include "netcompat.h"
51 59
52#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic 60#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic
61#define MAGIC "HUHN\xbd\xc6\xdb\x82" // 8 bytes of magic//D
53 62
54#define ULTRA_FAST 1 63#define ULTRA_FAST 1
55#define HLOG 15 64#define HLOG 15
56#include "lzf/lzf.h" 65#include "lzf/lzf.h"
57#include "lzf/lzf_c.c" 66#include "lzf/lzf_c.c"
119 { 128 {
120 u8 mac_key[MAC_KEYSIZE]; 129 u8 mac_key[MAC_KEYSIZE];
121 static const unsigned char mac_info[] = "gvpe mac key"; 130 static const unsigned char mac_info[] = "gvpe mac key";
122 131
123 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); 132 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)); 133 kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key));
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";
137 145
138 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); 146 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)); 147 kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key));
141 kdf.extract (s, sizeof (s)); 148 kdf.extract (s, sizeof (s));
142 kdf.extract_done (HKDF_PRF_HASH ()); 149 kdf.extract_done (HKDF_PRF_HASH ());
143 kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); 150 kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info));
144 151
158{ 165{
159 if (RSA_public_encrypt (sizeof (auth.rsa), 166 if (RSA_public_encrypt (sizeof (auth.rsa),
160 (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa, 167 (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa,
161 key, RSA_PKCS1_OAEP_PADDING) < 0) 168 key, RSA_PKCS1_OAEP_PADDING) < 0)
162 fatal ("RSA_public_encrypt error"); 169 fatal ("RSA_public_encrypt error");
170
171 memcpy (&encr.ecdh, &auth.ecdh, sizeof (encr.ecdh));
163} 172}
164 173
165static inline bool 174static inline bool
166auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth) 175auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth)
167{ 176{
171 (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt, 180 (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt,
172 key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa)) 181 key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa))
173 return 0; 182 return 0;
174 183
175 memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa)); 184 memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa));
185 memcpy (&auth.ecdh, &encr.ecdh, sizeof (auth.ecdh));
176 186
177 return 1; 187 return 1;
178} 188}
179 189
180static void 190static void
181auth_hash (const auth_data &auth, auth_mac &mac) 191auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
182{ 192{
183 HMAC_CTX ctx; 193 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
184 194 kdf.extract (&auth.rsa, sizeof (auth.rsa));
185 HMAC_CTX_init (&ctx); 195 kdf.extract_done ();
186 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
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} 197}
191 198
192void 199void
193connection::generate_auth_data () 200connection::generate_auth_data ()
194{ 201{
195 if (auth_expire < ev_now ()) 202 if (auth_expire < ev_now ())
196 { 203 {
197 // request data 204 // request data
198 RAND_bytes ((unsigned char *)&snd_auth.rsa, sizeof snd_auth.rsa); 205 rand_fill (snd_auth.rsa);
199 curve25519_generate (snd_ecdh_a, snd_auth.ecdh); 206 curve25519_generate (snd_ecdh_a, snd_auth.ecdh);
200 auth_hash (snd_auth, snd_auth_mac);
201 207
202 // eventual response data 208 // eventual response data
203 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); 209 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b);
204 } 210 }
205 211
361 } 367 }
362} 368}
363 369
364///////////////////////////////////////////////////////////////////////////// 370/////////////////////////////////////////////////////////////////////////////
365 371
366unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
367
368void 372void
369hmac_packet::hmac_gen (crypto_ctx *ctx) 373hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
370{ 374{
371 unsigned int xlen;
372
373 HMAC_CTX *hctx = &ctx->hctx; 375 HMAC_CTX *hctx = &ctx->hctx;
374 376
375 require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); 377 require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0));
376 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)));
377 len - sizeof (hmac_packet)));
378 require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen)); 379 require101 (HMAC_Final (hctx, hmac_digest, 0));
379} 380}
380 381
381void 382void
382hmac_packet::hmac_set (crypto_ctx *ctx) 383hmac_packet::hmac_set (crypto_ctx *ctx)
383{ 384{
384 hmac_gen (ctx); 385 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
385 386 hmac_gen (ctx, hmac_digest);
386 memcpy (hmac, hmac_digest, HMACLENGTH); 387 memcpy (hmac, hmac_digest, HMACLENGTH);
387} 388}
388 389
389bool 390bool
390hmac_packet::hmac_chk (crypto_ctx *ctx) 391hmac_packet::hmac_chk (crypto_ctx *ctx)
391{ 392{
392 hmac_gen (ctx); 393 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
393 394 hmac_gen (ctx, hmac_digest);
394 return !memcmp (hmac, hmac_digest, HMACLENGTH); 395 return !memcmp (hmac, hmac_digest, HMACLENGTH);
395} 396}
396 397
397void 398void
398vpn_packet::set_hdr (ptype type_, unsigned int dst) 399vpn_packet::set_hdr (ptype type_, unsigned int dst)
458 u32 seqno; 459 u32 seqno;
459 } datahdr; 460 } datahdr;
460 461
461 datahdr.seqno = ntohl (seqno); 462 datahdr.seqno = ntohl (seqno);
462#if RAND_SIZE 463#if RAND_SIZE
463 RAND_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); 464 // NB: a constant (per session) random prefix
465 // is likely enough, but we don't take any chances.
466 conn->oiv.get (datahdr.rnd, RAND_SIZE);
464#endif 467#endif
465 468
466 require (EVP_EncryptUpdate (cctx, 469 require (EVP_EncryptUpdate (cctx,
467 (unsigned char *) data + outl, &outl2, 470 (unsigned char *) data + outl, &outl2,
468 (unsigned char *) &datahdr, DATAHDR)); 471 (unsigned char *) &datahdr, DATAHDR));
499 502
500 if (type == PT_DATA_COMPRESSED) 503 if (type == PT_DATA_COMPRESSED)
501 d = cdata; 504 d = cdata;
502 else 505 else
503#endif 506#endif
504 d = &(*p)[6 + 6 - DATAHDR]; 507 d = &(*p)[6 + 6] - DATAHDR;
505 508
506 /* this overwrites part of the src mac, but we fix that later */ 509 // we play do evil games with the struct layout atm.
510 // pending better solutions, we at least do some verification.
511 // this is fine, as we left ISO territory long ago.
512 require (DATAHDR <= 16);
513 require ((u8 *)(&p->len + 1) == &(*p)[0]);
514
515 // this can overwrite the len/dst/src fields
507 require (EVP_DecryptUpdate (cctx, 516 require (EVP_DecryptUpdate (cctx,
508 d, &outl2, 517 d, &outl2,
509 (unsigned char *)&data, len - data_hdr_size ())); 518 (unsigned char *)&data, len - data_hdr_size ()));
510 outl += outl2; 519 outl += outl2;
511 520
542 } 551 }
543}; 552};
544 553
545struct config_packet : vpn_packet 554struct config_packet : vpn_packet
546{ 555{
556 u8 serial[SERIAL_SIZE];
547 u8 prot_major, prot_minor, randsize; 557 u8 prot_major, prot_minor, randsize;
548 u8 flags, features, pad6, pad7, pad8; 558 u8 flags, features, pad6, pad7, pad8;
549 u32 cipher_nid, mac_nid, auth_nid; 559 u32 cipher_nid, mac_nid, auth_nid;
550 560
551 void setup (ptype type, int dst); 561 void setup (ptype type, int dst);
552 bool chk_config () const; 562 bool chk_config (const conf_node *conf, const sockinfo &rsi) const;
553 563
554 static u8 get_features () 564 static u8 get_features ()
555 { 565 {
556 u8 f = 0; 566 u8 f = 0;
557#if ENABLE_COMPRESSION 567#if ENABLE_COMPRESSION
574 prot_minor = PROTOCOL_MINOR; 584 prot_minor = PROTOCOL_MINOR;
575 randsize = RAND_SIZE; 585 randsize = RAND_SIZE;
576 flags = 0; 586 flags = 0;
577 features = get_features (); 587 features = get_features ();
578 588
589 strncpy ((char *)serial, conf.serial, sizeof (serial));
590
579 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); 591 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ()));
580 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); 592 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ()));
581 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); 593 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ()));
582 594
583 len = sizeof (*this) - sizeof (net_packet); 595 len = sizeof (*this) - sizeof (net_packet);
584 set_hdr (type, dst); 596 set_hdr (type, dst);
585} 597}
586 598
587bool 599bool
588config_packet::chk_config () const 600config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const
589{ 601{
590 if (prot_major != PROTOCOL_MAJOR) 602 if (prot_major != PROTOCOL_MAJOR)
591 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);
592 else if (randsize != RAND_SIZE) 605 else if (randsize != RAND_SIZE)
593 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);
594 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) 608 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 ())); 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 ()));
596 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) 611 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 ())); 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 ()));
598 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) 614 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 ())); 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 ()));
600 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)
601 return true; 625 return true;
626 }
602 627
603 return false; 628 return false;
604} 629}
605 630
606struct auth_req_packet : config_packet // UNPROTECTED 631struct auth_req_packet : config_packet // UNPROTECTED
620 645
621 len = sizeof (*this) - sizeof (net_packet); 646 len = sizeof (*this) - sizeof (net_packet);
622 } 647 }
623}; 648};
624 649
625struct auth_res_packet : config_packet // UNPROTECTED 650struct auth_res_packet : vpn_packet // UNPROTECTED
626{ 651{
627 auth_response response; 652 auth_response response;
628 653
629 auth_res_packet (int dst) 654 auth_res_packet (int dst)
630 { 655 {
631 config_packet::setup (PT_AUTH_RES, dst); 656 set_hdr (PT_AUTH_RES, dst);
632 657
633 len = sizeof (*this) - sizeof (net_packet); 658 len = sizeof (*this) - sizeof (net_packet);
634 } 659 }
635}; 660};
636 661
676 si = rsi; 701 si = rsi;
677 protocol = rsi.prot; 702 protocol = rsi.prot;
678 703
679 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."),
680 conf->nodename, (const char *)rsi, 705 conf->nodename, (const char *)rsi,
681 is_direct ? "direct" : "forwarded", 706 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
682 PROTOCOL_MAJOR, prot_minor); 707 PROTOCOL_MAJOR, prot_minor);
683 708
684 if (::conf.script_node_up) 709 if (::conf.script_node_up)
685 { 710 {
686 run_script_cb *cb = new run_script_cb; 711 run_script_cb *cb = new run_script_cb;
692 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); 717 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff);
693 718
694 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); 719 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; 720 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff;
696 721
697 if (ictx && octx) 722 oiv.reset ();
698 { 723
699 // make sure rekeying timeouts are slightly asymmetric 724 // make sure rekeying timeouts are slightly asymmetric
700 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);
701 rekey.start (rekey_interval, rekey_interval); 726 rekey.start (rekey_interval, rekey_interval);
702 727
728 hmac_error = 0.;
729
703 keepalive.start (::conf.keepalive); 730 keepalive.start (::conf.keepalive);
704 731
705 // send queued packets 732 // send queued packets
706 if (ictx && octx)
707 {
708 while (tap_packet *p = (tap_packet *)data_queue.get ()) 733 while (tap_packet *p = (tap_packet *)data_queue.get ())
709 { 734 {
710 if (p->len) send_data_packet (p); 735 if (p->len) send_data_packet (p);
711 delete p; 736 delete p;
712 } 737 }
713 738
714 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 739 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
715 { 740 {
716 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 741 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
717 delete p; 742 delete p;
718 } 743 }
719 }
720 744
721 vpn->connection_established (this); 745 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} 746}
733 747
734void 748void
735connection::reset_si () 749connection::reset_si ()
736{ 750{
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
908 w.start (retry_int); 920 w.start (retry_int);
909 } 921 }
910} 922}
911 923
912void 924void
913connection::reset_connection () 925connection::reset_connection (const char *reason)
914{ 926{
915 if (ictx && octx) 927 if (ictx && octx)
916 { 928 {
917 slog (L_INFO, _("%s(%s): connection lost"), 929 slog (L_INFO, _("%s(%s): connection lost (%s)"),
918 conf->nodename, (const char *)si); 930 conf->nodename, (const char *)si, reason);
919 931
920 if (::conf.script_node_down) 932 if (::conf.script_node_down)
921 { 933 {
922 run_script_cb *cb = new run_script_cb; 934 run_script_cb *cb = new run_script_cb;
923 cb->set<connection, &connection::script_node_down> (this); 935 cb->set<connection, &connection::script_node_down> (this);
947connection::shutdown () 959connection::shutdown ()
948{ 960{
949 if (ictx && octx) 961 if (ictx && octx)
950 send_reset (si); 962 send_reset (si);
951 963
952 reset_connection (); 964 reset_connection ("shutdown");
953} 965}
954 966
955// poor-man's rekeying 967// poor-man's rekeying
956inline void 968inline void
957connection::rekey_cb (ev::timer &w, int revents) 969connection::rekey_cb (ev::timer &w, int revents)
958{ 970{
959 reset_connection (); 971 reset_connection ("rekeying");
960 establish_connection (); 972 establish_connection ();
961} 973}
962 974
963void 975void
964connection::send_data_packet (tap_packet *pkt) 976connection::send_data_packet (tap_packet *pkt)
1051 // about our desire for communication. 1063 // about our desire for communication.
1052 establish_connection (); 1064 establish_connection ();
1053 break; 1065 break;
1054 1066
1055 case vpn_packet::PT_RESET: 1067 case vpn_packet::PT_RESET:
1068 slog (L_TRACE, "%s >> PT_RESET", conf->nodename);
1069
1070 if (ictx && octx)
1056 { 1071 {
1057 reset_connection (); 1072 reset_connection ("remote reset");
1058 1073
1059 config_packet *p = (config_packet *) pkt; 1074 config_packet *p = (config_packet *) pkt;
1060 1075
1061 if (!p->chk_config ()) 1076 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 (); 1077 establish_connection ();
1069 } 1078 }
1079
1070 break; 1080 break;
1071 1081
1072 case vpn_packet::PT_AUTH_REQ: 1082 case vpn_packet::PT_AUTH_REQ:
1073 if (auth_rate_limiter.can (rsi)) 1083 if (auth_rate_limiter.can (rsi))
1074 { 1084 {
1076 1086
1077 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", 1087 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1078 conf->nodename, p->initiate ? "initiate" : "reply", 1088 conf->nodename, p->initiate ? "initiate" : "reply",
1079 p->protocols, p->features); 1089 p->protocols, p->features);
1080 1090
1081 if (p->chk_config () && !memcmp (p->magic, MAGIC, 8)) 1091 if (memcmp (p->magic, MAGIC, 8))
1092 {
1093 slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"),
1094 conf->nodename, (const char *)rsi);
1095 }
1096 else if (p->chk_config (conf, rsi))
1082 { 1097 {
1083 if (p->prot_minor != PROTOCOL_MINOR) 1098 if (p->prot_minor != PROTOCOL_MINOR)
1084 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1099 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1085 conf->nodename, (const char *)rsi, 1100 conf->nodename, (const char *)rsi,
1086 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1101 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1087 1102
1088 if (p->initiate) 1103 if (p->initiate)
1104 {
1089 send_auth_request (rsi, false); 1105 send_auth_request (rsi, false);
1106
1107 if (ictx && octx)
1108 reset_connection ("reconnect");
1109 }
1090 1110
1091 auth_data auth; 1111 auth_data auth;
1092 1112
1093 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1113 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1094 { 1114 {
1113 } 1133 }
1114 } 1134 }
1115 1135
1116 break; 1136 break;
1117 } 1137 }
1118 else
1119 slog (L_WARN, _("%s(%s): protocol mismatch."),
1120 conf->nodename, (const char *)rsi);
1121 1138
1122 send_reset (rsi); 1139 send_reset (rsi);
1123 } 1140 }
1124 1141
1125 break; 1142 break;
1128 { 1145 {
1129 auth_res_packet *p = (auth_res_packet *)pkt; 1146 auth_res_packet *p = (auth_res_packet *)pkt;
1130 1147
1131 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); 1148 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1132 1149
1133 if (p->chk_config ()) 1150 auth_mac local_mac;
1151 auth_hash (snd_auth, p->response.ecdh, local_mac);
1152
1153 if (memcmp (&p->response.mac, local_mac, sizeof local_mac))
1134 { 1154 {
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."), 1155 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."),
1138 conf->nodename, (const char *)rsi); 1156 conf->nodename, (const char *)rsi);
1139 } 1157 }
1140 else if (!have_snd_auth) 1158 else if (!have_snd_auth)
1141 { 1159 {
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)); 1160 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1149 1161
1150 have_snd_auth = true; 1162 have_snd_auth = true;
1151 connection_established (rsi); 1163 connection_established (rsi);
1152 }
1153
1154 break;
1155 } 1164 }
1156 } 1165 }
1157
1158 send_reset (rsi);
1159 break; 1166 break;
1160 1167
1161 case vpn_packet::PT_DATA_COMPRESSED: 1168 case vpn_packet::PT_DATA_COMPRESSED:
1162#if !ENABLE_COMPRESSION 1169#if !ENABLE_COMPRESSION
1163 send_reset (rsi); 1170 send_reset (rsi);
1169 if (ictx && octx) 1176 if (ictx && octx)
1170 { 1177 {
1171 vpndata_packet *p = (vpndata_packet *)pkt; 1178 vpndata_packet *p = (vpndata_packet *)pkt;
1172 1179
1173 if (!p->hmac_chk (ictx)) 1180 if (!p->hmac_chk (ictx))
1181 {
1182 // rekeying often creates temporary hmac auth floods
1183 // we assume they don't take longer than a few seconds normally,
1184 // and suppress messages and resets during that time.
1185 //TODO: should be done per source address
1186 if (!hmac_error)
1187 {
1188 hmac_error = ev_now () + 3;
1189 break;
1190 }
1191 else if (hmac_error >= ev_now ())
1192 break; // silently suppress
1193 else
1194 {
1174 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1195 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1175 "could be an attack, or just corruption or a synchronization error."), 1196 "could be an attack, or just corruption or a synchronization error."),
1176 conf->nodename, (const char *)rsi); 1197 conf->nodename, (const char *)rsi);
1198 // reset
1199 }
1200 }
1177 else 1201 else
1178 { 1202 {
1179 u32 seqno; 1203 u32 seqno;
1180 tap_packet *d = p->unpack (this, seqno); 1204 tap_packet *d = p->unpack (this, seqno);
1181 int seqclass = iseqno.seqno_classify (seqno); 1205 int seqclass = iseqno.seqno_classify (seqno);
1206
1207 hmac_error = 0;
1182 1208
1183 if (seqclass == 0) // ok 1209 if (seqclass == 0) // ok
1184 { 1210 {
1185 vpn->tap->send (d); 1211 vpn->tap->send (d);
1186 1212
1313 1339
1314 if (when >= 0) 1340 if (when >= 0)
1315 w.start (when); 1341 w.start (when);
1316 else if (when < -15) 1342 else if (when < -15)
1317 { 1343 {
1318 reset_connection (); 1344 reset_connection ("keepalive overdue");
1319 establish_connection (); 1345 establish_connection ();
1320 } 1346 }
1321 else if (conf->connectmode != conf_node::C_ONDEMAND 1347 else if (conf->connectmode != conf_node::C_ONDEMAND
1322 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1348 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1323 { 1349 {
1327 else if (when >= -10) 1353 else if (when >= -10)
1328 // hold ondemand connections implicitly a few seconds longer 1354 // hold ondemand connections implicitly a few seconds longer
1329 // should delete octx, though, or something like that ;) 1355 // should delete octx, though, or something like that ;)
1330 w.start (when + 10); 1356 w.start (when + 10);
1331 else 1357 else
1332 reset_connection (); 1358 reset_connection ("keepalive timeout");
1333} 1359}
1334 1360
1335void 1361void
1336connection::send_connect_request (int id) 1362connection::send_connect_request (int id)
1337{ 1363{
1437 1463
1438 // queue a dummy packet to force an initial connection attempt 1464 // queue a dummy packet to force an initial connection attempt
1439 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1465 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1440 vpn_queue.put (new net_packet); 1466 vpn_queue.put (new net_packet);
1441 1467
1442 reset_connection (); 1468 reset_connection ("startup");
1443} 1469}
1444 1470
1445connection::~connection () 1471connection::~connection ()
1446{ 1472{
1447 shutdown (); 1473 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines