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.103 by root, Thu Jul 18 17:35:10 2013 UTC vs.
Revision 1.113 by root, Thu Jan 29 00:21:39 2015 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"
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
53#define MAGIC "HUHN\xbd\xc6\xdb\x82" // 8 bytes of magic//D
54 61
55#define ULTRA_FAST 1 62#define ULTRA_FAST 1
56#define HLOG 15 63#define HLOG 15
57#include "lzf/lzf.h" 64#include "lzf/lzf.h"
58#include "lzf/lzf_c.c" 65#include "lzf/lzf_c.c"
126 kdf.extract (s, sizeof (s)); 133 kdf.extract (s, sizeof (s));
127 kdf.extract_done (HKDF_PRF_HASH ()); 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";
178 185
179 return 1; 186 return 1;
180} 187}
181 188
182static void 189static void
183auth_hash (const auth_data &auth, auth_mac &mac) 190auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
184{ 191{
185 HMAC_CTX ctx; 192 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
186 193 kdf.extract (&auth.rsa, sizeof (auth.rsa));
187 HMAC_CTX_init (&ctx); 194 kdf.extract_done ();
188 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
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} 196}
193 197
194void 198void
195connection::generate_auth_data () 199connection::generate_auth_data ()
196{ 200{
197 if (auth_expire < ev_now ()) 201 if (auth_expire < ev_now ())
198 { 202 {
199 // request data 203 // request data
200 rand_fill (snd_auth.rsa); 204 rand_fill (snd_auth.rsa);
201 curve25519_generate (snd_ecdh_a, snd_auth.ecdh); 205 curve25519_generate (snd_ecdh_a, snd_auth.ecdh);
202 auth_hash (snd_auth, snd_auth_mac);
203 206
204 // eventual response data 207 // eventual response data
205 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); 208 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b);
206 } 209 }
207 210
363 } 366 }
364} 367}
365 368
366///////////////////////////////////////////////////////////////////////////// 369/////////////////////////////////////////////////////////////////////////////
367 370
368unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
369
370void 371void
371hmac_packet::hmac_gen (crypto_ctx *ctx) 372hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
372{ 373{
373 unsigned int xlen;
374
375 HMAC_CTX *hctx = &ctx->hctx; 374 HMAC_CTX *hctx = &ctx->hctx;
376 375
377 require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); 376 require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0));
378 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)));
379 len - sizeof (hmac_packet)));
380 require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen)); 378 require101 (HMAC_Final (hctx, hmac_digest, 0));
381} 379}
382 380
383void 381void
384hmac_packet::hmac_set (crypto_ctx *ctx) 382hmac_packet::hmac_set (crypto_ctx *ctx)
385{ 383{
386 hmac_gen (ctx); 384 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
387 385 hmac_gen (ctx, hmac_digest);
388 memcpy (hmac, hmac_digest, HMACLENGTH); 386 memcpy (hmac, hmac_digest, HMACLENGTH);
389} 387}
390 388
391bool 389bool
392hmac_packet::hmac_chk (crypto_ctx *ctx) 390hmac_packet::hmac_chk (crypto_ctx *ctx)
393{ 391{
394 hmac_gen (ctx); 392 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
395 393 hmac_gen (ctx, hmac_digest);
396 return !memcmp (hmac, hmac_digest, HMACLENGTH); 394 return slow_memeq (hmac, hmac_digest, HMACLENGTH);
397} 395}
398 396
399void 397void
400vpn_packet::set_hdr (ptype type_, unsigned int dst) 398vpn_packet::set_hdr (ptype type_, unsigned int dst)
401{ 399{
407 srcdst = ((src >> 8) << 4) | (dst >> 8); 405 srcdst = ((src >> 8) << 4) | (dst >> 8);
408 dst1 = dst; 406 dst1 = dst;
409} 407}
410 408
411#define MAXVPNDATA (MAX_MTU - 6 - 6) 409#define MAXVPNDATA (MAX_MTU - 6 - 6)
412#define DATAHDR (sizeof (u32) + RAND_SIZE)
413 410
414struct vpndata_packet : vpn_packet 411struct vpndata_packet : vpn_packet
415{ 412{
416 u8 data[MAXVPNDATA + DATAHDR]; // seqno 413 u32 ctr; // seqno
414 u8 data[MAXVPNDATA];
417 415
418 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); 416 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
419 tap_packet *unpack (connection *conn, u32 &seqno); 417 tap_packet *unpack (connection *conn, u32 &seqno);
420 418
421private: 419private:
422 const u32 data_hdr_size () const 420 const u32 data_hdr_size () const
423 { 421 {
424 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 422 // the distance from beginning of packet to data member
423 return data - at (0);
425 } 424 }
426}; 425};
426
427// expands packet counter (unlike seqno, in network byte order) to counter mode IV
428static unsigned char *
429expand_iv (u32 ctr)
430{
431 static u32 iv[IV_SIZE (CIPHER) / 4];
432
433 require (sizeof (iv) == 4 * 4);
434 require (IV_SIZE (CIPHER) % 4 == 0);
435
436 iv[0] =
437 iv[1] =
438 iv[2] = ctr;
439
440 // I would reuse ctr here to to avoid potential endianness issues,
441 // but it seems openssl wraps around. While this would be still ok,
442 // and I don't even know if its true, let's play safe and initialise
443 // to 0.
444 iv[3] = 0;
445
446 return (unsigned char *)iv;
447}
427 448
428void 449void
429vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) 450vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno)
430{ 451{
431 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 452 EVP_CIPHER_CTX *cctx = &conn->octx->cctx;
449 d[1] = cl; 470 d[1] = cl;
450 } 471 }
451 } 472 }
452#endif 473#endif
453 474
454 require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 1)); 475 ctr = htonl (seqno);
455 476
456 struct { 477 require (EVP_EncryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr)));
457#if RAND_SIZE
458 u8 rnd[RAND_SIZE];
459#endif
460 u32 seqno;
461 } datahdr;
462
463 datahdr.seqno = ntohl (seqno);
464#if RAND_SIZE
465 // NB: a constant (per session) random prefix
466 // is likely enough, but we don't take any chances.
467 conn->oiv.get (datahdr.rnd, RAND_SIZE);
468#endif
469 478
470 require (EVP_EncryptUpdate (cctx, 479 require (EVP_EncryptUpdate (cctx,
471 (unsigned char *) data + outl, &outl2, 480 (unsigned char *)data + outl, &outl2,
472 (unsigned char *) &datahdr, DATAHDR)); 481 (unsigned char *)d, l));
473 outl += outl2; 482 outl += outl2;
474 483
475 require (EVP_EncryptUpdate (cctx, 484 // it seems this is a nop for us, but we do it anyways
476 (unsigned char *) data + outl, &outl2, 485 require (EVP_EncryptFinal_ex (cctx, (unsigned char *)data + outl, &outl2));
477 (unsigned char *) d, l));
478 outl += outl2; 486 outl += outl2;
479 487
480 require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2));
481 outl += outl2;
482
483 len = outl + data_hdr_size (); 488 len = data_hdr_size () + outl;
484 489
485 set_hdr (type, dst); 490 set_hdr (type, dst);
486 491
487 hmac_set (conn->octx); 492 hmac_set (conn->octx);
488} 493}
492{ 497{
493 EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; 498 EVP_CIPHER_CTX *cctx = &conn->ictx->cctx;
494 int outl = 0, outl2; 499 int outl = 0, outl2;
495 tap_packet *p = new tap_packet; 500 tap_packet *p = new tap_packet;
496 u8 *d; 501 u8 *d;
497 u32 l = len - data_hdr_size ();
498 502
499 require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 0)); 503 seqno = ntohl (ctr);
504
505 require (EVP_DecryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr)));
500 506
501#if ENABLE_COMPRESSION 507#if ENABLE_COMPRESSION
502 u8 cdata[MAX_MTU]; 508 u8 cdata[MAX_MTU];
503 509
504 if (type == PT_DATA_COMPRESSED) 510 if (type == PT_DATA_COMPRESSED)
505 d = cdata; 511 d = cdata;
506 else 512 else
507#endif 513#endif
508 d = &(*p)[6 + 6] - DATAHDR; 514 d = &(*p)[6 + 6];
509
510 // we play do evil games with the struct layout atm.
511 // pending better solutions, we at least do some verification.
512 // this is fine, as we left ISO territory long ago.
513 require (DATAHDR <= 16);
514 require ((u8 *)(&p->len + 1) == &(*p)[0]);
515 515
516 // this can overwrite the len/dst/src fields 516 // this can overwrite the len/dst/src fields
517 require (EVP_DecryptUpdate (cctx, 517 require (EVP_DecryptUpdate (cctx,
518 d, &outl2, 518 d, &outl2,
519 (unsigned char *)&data, len - data_hdr_size ())); 519 (unsigned char *)&data, len - data_hdr_size ()));
520 outl += outl2; 520 outl += outl2;
521 521
522 // it seems this is a nop for us, but we do it anyways
522 require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); 523 require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2));
523 outl += outl2; 524 outl += outl2;
524 525
525 seqno = ntohl (*(u32 *)(d + RAND_SIZE));
526
527 id2mac (dst () ? dst() : THISNODE->id, p->dst); 526 id2mac (dst () ? dst() : THISNODE->id, p->dst);
528 id2mac (src (), p->src); 527 id2mac (src (), p->src);
529 528
530#if ENABLE_COMPRESSION 529#if ENABLE_COMPRESSION
531 if (type == PT_DATA_COMPRESSED) 530 if (type == PT_DATA_COMPRESSED)
532 { 531 {
533 u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; 532 u32 cl = (d[0] << 8) | d[1];
534 533
535 p->len = lzf_decompress (d + DATAHDR + 2, cl < MAX_MTU ? cl : 0, 534 p->len = lzf_decompress (d + 2, cl < MAX_MTU - 2 ? cl : 0,
536 &(*p)[6 + 6], MAX_MTU) 535 &(*p)[6 + 6], MAX_MTU)
537 + 6 + 6; 536 + 6 + 6;
538 } 537 }
539 else 538 else
540 p->len = outl + (6 + 6 - DATAHDR); 539 p->len = outl + (6 + 6);
541#endif 540#endif
542 541
543 return p; 542 return p;
544} 543}
545 544
581void 580void
582config_packet::setup (ptype type, int dst) 581config_packet::setup (ptype type, int dst)
583{ 582{
584 prot_major = PROTOCOL_MAJOR; 583 prot_major = PROTOCOL_MAJOR;
585 prot_minor = PROTOCOL_MINOR; 584 prot_minor = PROTOCOL_MINOR;
586 randsize = RAND_SIZE;
587 flags = 0; 585 flags = 0;
588 features = get_features (); 586 features = get_features ();
589 587
590 strncpy ((char *)serial, conf.serial, sizeof (serial)); 588 strncpy ((char *)serial, conf.serial, sizeof (serial));
591 589
601config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const 599config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const
602{ 600{
603 if (prot_major != PROTOCOL_MAJOR) 601 if (prot_major != PROTOCOL_MAJOR)
604 slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"), 602 slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"),
605 conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); 603 conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR);
606 else if (randsize != RAND_SIZE)
607 slog (L_WARN, _("%s(%s): rand size mismatch (remote %d <=> local %d)"),
608 conf->nodename, (const char *)rsi, randsize, RAND_SIZE);
609 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) 604 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ())))
610 slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"), 605 slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"),
611 conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); 606 conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ()));
612 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) 607 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ())))
613 slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"), 608 slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"),
702 si = rsi; 697 si = rsi;
703 protocol = rsi.prot; 698 protocol = rsi.prot;
704 699
705 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), 700 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
706 conf->nodename, (const char *)rsi, 701 conf->nodename, (const char *)rsi,
707 is_direct ? "direct" : "forwarded", 702 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
708 PROTOCOL_MAJOR, prot_minor); 703 PROTOCOL_MAJOR, prot_minor);
709 704
710 if (::conf.script_node_up) 705 if (::conf.script_node_up)
711 { 706 {
712 run_script_cb *cb = new run_script_cb; 707 run_script_cb *cb = new run_script_cb;
718 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); 713 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff);
719 714
720 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); 715 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1);
721 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; 716 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff;
722 717
723 oiv.reset ();
724
725 // make sure rekeying timeouts are slightly asymmetric 718 // make sure rekeying timeouts are slightly asymmetric
726 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 719 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
727 rekey.start (rekey_interval, rekey_interval); 720 rekey.start (rekey_interval, rekey_interval);
728 721
722 hmac_error = 0.;
723
729 keepalive.start (::conf.keepalive); 724 keepalive.start (::conf.keepalive);
730 725
731 // send queued packets 726 // send queued packets
732 if (ictx && octx)
733 {
734 while (tap_packet *p = (tap_packet *)data_queue.get ()) 727 while (tap_packet *p = (tap_packet *)data_queue.get ())
735 { 728 {
736 if (p->len) send_data_packet (p); 729 if (p->len) send_data_packet (p);
737 delete p; 730 delete p;
738 } 731 }
739 732
740 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 733 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
741 { 734 {
742 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 735 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
743 delete p; 736 delete p;
744 }
745 } 737 }
746 738
747 vpn->connection_established (this); 739 vpn->connection_established (this);
748} 740}
749 741
757 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); 749 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
758 protocol = 0; 750 protocol = 0;
759 } 751 }
760 752
761 si.set (conf, protocol); 753 si.set (conf, protocol);
762
763 is_direct = si.valid ();
764} 754}
765 755
766// ensure sockinfo is valid, forward if necessary 756// ensure sockinfo is valid, forward if necessary
767const sockinfo & 757const sockinfo &
768connection::forward_si (const sockinfo &si) const 758connection::forward_si (const sockinfo &si) const
787 777
788void 778void
789connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 779connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
790{ 780{
791 if (!vpn->send_vpn_packet (pkt, si, tos)) 781 if (!vpn->send_vpn_packet (pkt, si, tos))
792 reset_connection (); 782 reset_connection ("packet send error");
793} 783}
794 784
795void 785void
796connection::send_ping (const sockinfo &si, u8 pong) 786connection::send_ping (const sockinfo &si, u8 pong)
797{ 787{
836void 826void
837connection::send_auth_response (const sockinfo &si) 827connection::send_auth_response (const sockinfo &si)
838{ 828{
839 auth_res_packet *pkt = new auth_res_packet (conf->id); 829 auth_res_packet *pkt = new auth_res_packet (conf->id);
840 830
841 auth_hash (rcv_auth, pkt->response.mac);
842 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof (rcv_ecdh_b)); 831 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b);
832 auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac);
843 833
844 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); 834 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si);
845 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 835 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
846 836
847 delete pkt; 837 delete pkt;
873 { 863 {
874 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection 864 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection
875 // and stop trying. should probably be handled by a per-connection expire handler. 865 // and stop trying. should probably be handled by a per-connection expire handler.
876 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) 866 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ())
877 { 867 {
878 reset_connection (); 868 reset_connection ("no demand");
879 return; 869 return;
880 } 870 }
881 871
882 last_establish_attempt = ev_now (); 872 last_establish_attempt = ev_now ();
883 873
885 ? (retry_cnt & 3) + 1 875 ? (retry_cnt & 3) + 1
886 : 1 << (retry_cnt >> 2)); 876 : 1 << (retry_cnt >> 2));
887 877
888 reset_si (); 878 reset_si ();
889 879
890 bool slow = si.prot & PROT_SLOW; 880 bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power);
891 881
892 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 882 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
893 { 883 {
894 /*TODO*/ /* start the timer so we don't recurse endlessly */ 884 /*TODO*/ /* start the timer so we don't recurse endlessly */
895 w.start (1); 885 w.start (1);
905 895
906 slow = slow || (dsi.prot & PROT_SLOW); 896 slow = slow || (dsi.prot & PROT_SLOW);
907 897
908 if (dsi.valid () && auth_rate_limiter.can (dsi)) 898 if (dsi.valid () && auth_rate_limiter.can (dsi))
909 { 899 {
910 if (retry_cnt < 4) 900 // use ping after the first few retries
901 // TODO: on rekeys, the other node might not interpret ping correctly,
902 // TODO: as it will still have a valid connection
903 if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power))
911 send_auth_request (dsi, true); 904 send_auth_request (dsi, true);
912 else 905 else
913 send_ping (dsi, 0); 906 send_ping (dsi, 0);
914 } 907 }
915 } 908 }
916 909
917 retry_int *= slow ? 8. : 0.9; 910 retry_int *= slow ? 4. : 0.9;
918 911
919 if (retry_int < conf->max_retry) 912 if (retry_int < conf->max_retry)
920 retry_cnt++; 913 retry_cnt++;
921 else 914 else
922 retry_int = conf->max_retry; 915 retry_int = conf->max_retry;
924 w.start (retry_int); 917 w.start (retry_int);
925 } 918 }
926} 919}
927 920
928void 921void
929connection::reset_connection () 922connection::reset_connection (const char *reason)
930{ 923{
931 if (ictx && octx) 924 if (ictx && octx)
932 { 925 {
933 slog (L_INFO, _("%s(%s): connection lost"), 926 slog (L_INFO, _("%s(%s): connection lost (%s)"),
934 conf->nodename, (const char *)si); 927 conf->nodename, (const char *)si, reason);
935 928
936 if (::conf.script_node_down) 929 if (::conf.script_node_down)
937 { 930 {
938 run_script_cb *cb = new run_script_cb; 931 run_script_cb *cb = new run_script_cb;
939 cb->set<connection, &connection::script_node_down> (this); 932 cb->set<connection, &connection::script_node_down> (this);
963connection::shutdown () 956connection::shutdown ()
964{ 957{
965 if (ictx && octx) 958 if (ictx && octx)
966 send_reset (si); 959 send_reset (si);
967 960
968 reset_connection (); 961 reset_connection ("shutdown");
969} 962}
970 963
971// poor-man's rekeying 964// poor-man's rekeying
972inline void 965inline void
973connection::rekey_cb (ev::timer &w, int revents) 966connection::rekey_cb (ev::timer &w, int revents)
974{ 967{
975 reset_connection (); 968 reset_connection ("rekeying");
976 establish_connection (); 969 establish_connection ();
977} 970}
978 971
979void 972void
980connection::send_data_packet (tap_packet *pkt) 973connection::send_data_packet (tap_packet *pkt)
997 990
998void 991void
999connection::post_inject_queue () 992connection::post_inject_queue ()
1000{ 993{
1001 // force a connection every now and when when packets are sent (max 1/s) 994 // force a connection every now and when when packets are sent (max 1/s)
1002 if (ev_now () - last_establish_attempt >= 0.95) // arbitrary 995 if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary
1003 establish_connection.stop (); 996 establish_connection.stop ();
1004 997
1005 establish_connection (); 998 establish_connection ();
1006} 999}
1007 1000
1067 // about our desire for communication. 1060 // about our desire for communication.
1068 establish_connection (); 1061 establish_connection ();
1069 break; 1062 break;
1070 1063
1071 case vpn_packet::PT_RESET: 1064 case vpn_packet::PT_RESET:
1065 slog (L_TRACE, "%s >> PT_RESET", conf->nodename);
1066
1067 if (ictx && octx)
1072 { 1068 {
1073 reset_connection (); 1069 reset_connection ("remote reset");
1074 1070
1075 config_packet *p = (config_packet *) pkt; 1071 config_packet *p = (config_packet *) pkt;
1076 1072
1077 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) 1073 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS)
1078 establish_connection (); 1074 establish_connection ();
1079 } 1075 }
1076
1080 break; 1077 break;
1081 1078
1082 case vpn_packet::PT_AUTH_REQ: 1079 case vpn_packet::PT_AUTH_REQ:
1083 if (auth_rate_limiter.can (rsi)) 1080 if (auth_rate_limiter.can (rsi))
1084 { 1081 {
1099 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1096 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1100 conf->nodename, (const char *)rsi, 1097 conf->nodename, (const char *)rsi,
1101 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1098 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1102 1099
1103 if (p->initiate) 1100 if (p->initiate)
1101 {
1104 send_auth_request (rsi, false); 1102 send_auth_request (rsi, false);
1103
1104 if (ictx && octx)
1105 reset_connection ("reconnect");
1106 }
1105 1107
1106 auth_data auth; 1108 auth_data auth;
1107 1109
1108 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1110 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1109 { 1111 {
1110 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), 1112 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"),
1111 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); 1113 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0));
1112 } 1114 }
1113 else 1115 else
1114 { 1116 {
1115 bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); 1117 bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth);
1116 1118
1117 rcv_auth = auth; 1119 rcv_auth = auth;
1118 have_rcv_auth = true; 1120 have_rcv_auth = true;
1119 1121
1120 send_auth_response (rsi); 1122 send_auth_response (rsi);
1140 { 1142 {
1141 auth_res_packet *p = (auth_res_packet *)pkt; 1143 auth_res_packet *p = (auth_res_packet *)pkt;
1142 1144
1143 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); 1145 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1144 1146
1147 auth_mac local_mac;
1148 auth_hash (snd_auth, p->response.ecdh, local_mac);
1149
1145 if (memcmp (&p->response.mac, snd_auth_mac, sizeof (snd_auth_mac))) 1150 if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac))
1146 { 1151 {
1147 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), 1152 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."),
1148 conf->nodename, (const char *)rsi); 1153 conf->nodename, (const char *)rsi);
1149 } 1154 }
1150 else if (!have_snd_auth) 1155 else if (!have_snd_auth)
1151 { 1156 {
1152 memcpy (snd_ecdh_b, p->response.ecdh, sizeof (snd_ecdh_b)); 1157 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1153 1158
1154 have_snd_auth = true; 1159 have_snd_auth = true;
1155 connection_established (rsi); 1160 connection_established (rsi);
1156 } 1161 }
1157
1158 break;
1159 } 1162 }
1160
1161 send_reset (rsi);
1162 break; 1163 break;
1163 1164
1164 case vpn_packet::PT_DATA_COMPRESSED: 1165 case vpn_packet::PT_DATA_COMPRESSED:
1165#if !ENABLE_COMPRESSION 1166#if !ENABLE_COMPRESSION
1166 send_reset (rsi); 1167 send_reset (rsi);
1172 if (ictx && octx) 1173 if (ictx && octx)
1173 { 1174 {
1174 vpndata_packet *p = (vpndata_packet *)pkt; 1175 vpndata_packet *p = (vpndata_packet *)pkt;
1175 1176
1176 if (!p->hmac_chk (ictx)) 1177 if (!p->hmac_chk (ictx))
1178 {
1179 // rekeying often creates temporary hmac auth floods
1180 // we assume they don't take longer than a few seconds normally,
1181 // and suppress messages and resets during that time.
1182 //TODO: should be done per source address
1183 if (!hmac_error)
1184 {
1185 hmac_error = ev_now () + 3;
1186 break;
1187 }
1188 else if (hmac_error >= ev_now ())
1189 break; // silently suppress
1190 else
1191 {
1177 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1192 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1178 "could be an attack, or just corruption or a synchronization error."), 1193 "could be an attack, or just corruption or a synchronization error."),
1179 conf->nodename, (const char *)rsi); 1194 conf->nodename, (const char *)rsi);
1195 // reset
1196 }
1197 }
1180 else 1198 else
1181 { 1199 {
1182 u32 seqno; 1200 u32 seqno;
1183 tap_packet *d = p->unpack (this, seqno); 1201 tap_packet *d = p->unpack (this, seqno);
1184 int seqclass = iseqno.seqno_classify (seqno); 1202 int seqclass = iseqno.seqno_classify (seqno);
1203
1204 hmac_error = 0;
1185 1205
1186 if (seqclass == 0) // ok 1206 if (seqclass == 0) // ok
1187 { 1207 {
1188 vpn->tap->send (d); 1208 vpn->tap->send (d);
1189 1209
1316 1336
1317 if (when >= 0) 1337 if (when >= 0)
1318 w.start (when); 1338 w.start (when);
1319 else if (when < -15) 1339 else if (when < -15)
1320 { 1340 {
1321 reset_connection (); 1341 reset_connection ("keepalive overdue");
1322 establish_connection (); 1342 establish_connection ();
1323 } 1343 }
1324 else if (conf->connectmode != conf_node::C_ONDEMAND 1344 else if (conf->connectmode != conf_node::C_ONDEMAND
1325 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1345 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1326 { 1346 {
1330 else if (when >= -10) 1350 else if (when >= -10)
1331 // hold ondemand connections implicitly a few seconds longer 1351 // hold ondemand connections implicitly a few seconds longer
1332 // should delete octx, though, or something like that ;) 1352 // should delete octx, though, or something like that ;)
1333 w.start (when + 10); 1353 w.start (when + 10);
1334 else 1354 else
1335 reset_connection (); 1355 reset_connection ("keepalive timeout");
1336} 1356}
1337 1357
1338void 1358void
1339connection::send_connect_request (int id) 1359connection::send_connect_request (int id)
1340{ 1360{
1440 1460
1441 // queue a dummy packet to force an initial connection attempt 1461 // queue a dummy packet to force an initial connection attempt
1442 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1462 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1443 vpn_queue.put (new net_packet); 1463 vpn_queue.put (new net_packet);
1444 1464
1445 reset_connection (); 1465 reset_connection ("startup");
1446} 1466}
1447 1467
1448connection::~connection () 1468connection::~connection ()
1449{ 1469{
1450 shutdown (); 1470 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines