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.114 by root, Thu Jun 30 11:43:38 2016 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2008,2010,2011,2013 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008,2010,2011,2013,2016 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify it 7 GVPE is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
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>
42 43
43#include "conf.h" 44#include "conf.h"
44#include "slog.h" 45#include "slog.h"
46#include "crypto.h"
45#include "device.h" 47#include "device.h"
46#include "vpn.h" 48#include "vpn.h"
47#include "connection.h" 49#include "connection.h"
48#include "hkdf.h" 50#include "hkdf.h"
49 51
50#include "netcompat.h" 52#include "netcompat.h"
51 53
52#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic 54#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 55
55#define ULTRA_FAST 1 56#define ULTRA_FAST 1
56#define HLOG 15 57#define HLOG 15
57#include "lzf/lzf.h" 58#include "lzf/lzf.h"
58#include "lzf/lzf_c.c" 59#include "lzf/lzf_c.c"
103////////////////////////////////////////////////////////////////////////////// 104//////////////////////////////////////////////////////////////////////////////
104 105
105struct crypto_ctx 106struct crypto_ctx
106{ 107{
107 EVP_CIPHER_CTX cctx; 108 EVP_CIPHER_CTX cctx;
108 HMAC_CTX hctx; 109 hmac hctx;
109 110
110 crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc); 111 crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc);
111 ~crypto_ctx (); 112 ~crypto_ctx ();
112}; 113};
113 114
125 kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key)); 126 kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key));
126 kdf.extract (s, sizeof (s)); 127 kdf.extract (s, sizeof (s));
127 kdf.extract_done (HKDF_PRF_HASH ()); 128 kdf.extract_done (HKDF_PRF_HASH ());
128 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); 129 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info));
129 130
130 HMAC_CTX_init (&hctx); 131 hctx.init (mac_key, MAC_KEYSIZE, MAC_DIGEST ());
131 require (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0));
132 } 132 }
133 133
134 { 134 {
135 u8 cipher_key[CIPHER_KEYSIZE]; 135 u8 cipher_key[CIPHER_KEYSIZE];
136 static const unsigned char cipher_info[] = "gvpe cipher key"; 136 static const unsigned char cipher_info[] = "gvpe cipher key";
147} 147}
148 148
149crypto_ctx::~crypto_ctx () 149crypto_ctx::~crypto_ctx ()
150{ 150{
151 require (EVP_CIPHER_CTX_cleanup (&cctx)); 151 require (EVP_CIPHER_CTX_cleanup (&cctx));
152 HMAC_CTX_cleanup (&hctx);
153} 152}
154 153
155static inline void 154static inline void
156auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr) 155auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr)
157{ 156{
178 177
179 return 1; 178 return 1;
180} 179}
181 180
182static void 181static void
183auth_hash (const auth_data &auth, auth_mac &mac) 182auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
184{ 183{
185 HMAC_CTX ctx; 184 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
186 185 kdf.extract (&auth.rsa, sizeof (auth.rsa));
187 HMAC_CTX_init (&ctx); 186 kdf.extract_done ();
188 require (HMAC_Init_ex (&ctx, auth.rsa.auth_key, sizeof (auth.rsa.auth_key), AUTH_DIGEST (), 0)); 187 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} 188}
193 189
194void 190void
195connection::generate_auth_data () 191connection::generate_auth_data ()
196{ 192{
197 if (auth_expire < ev_now ()) 193 if (auth_expire < ev_now ())
198 { 194 {
199 // request data 195 // request data
200 rand_fill (snd_auth.rsa); 196 rand_fill (snd_auth.rsa);
201 curve25519_generate (snd_ecdh_a, snd_auth.ecdh); 197 curve25519_generate (snd_ecdh_a, snd_auth.ecdh);
202 auth_hash (snd_auth, snd_auth_mac);
203 198
204 // eventual response data 199 // eventual response data
205 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); 200 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b);
206 } 201 }
207 202
363 } 358 }
364} 359}
365 360
366///////////////////////////////////////////////////////////////////////////// 361/////////////////////////////////////////////////////////////////////////////
367 362
368unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
369
370void 363void
371hmac_packet::hmac_gen (crypto_ctx *ctx) 364hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
372{ 365{
373 unsigned int xlen; 366 ctx->hctx.init ();
374 367 ctx->hctx.add (((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet));
375 HMAC_CTX *hctx = &ctx->hctx; 368 ctx->hctx.digest (hmac_digest);
376
377 require (HMAC_Init_ex (hctx, 0, 0, 0, 0));
378 require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet),
379 len - sizeof (hmac_packet)));
380 require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen));
381} 369}
382 370
383void 371void
384hmac_packet::hmac_set (crypto_ctx *ctx) 372hmac_packet::hmac_set (crypto_ctx *ctx)
385{ 373{
386 hmac_gen (ctx); 374 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
387 375 hmac_gen (ctx, hmac_digest);
388 memcpy (hmac, hmac_digest, HMACLENGTH); 376 memcpy (hmac, hmac_digest, HMACLENGTH);
389} 377}
390 378
391bool 379bool
392hmac_packet::hmac_chk (crypto_ctx *ctx) 380hmac_packet::hmac_chk (crypto_ctx *ctx)
393{ 381{
394 hmac_gen (ctx); 382 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
395 383 hmac_gen (ctx, hmac_digest);
396 return !memcmp (hmac, hmac_digest, HMACLENGTH); 384 return slow_memeq (hmac, hmac_digest, HMACLENGTH);
397} 385}
398 386
399void 387void
400vpn_packet::set_hdr (ptype type_, unsigned int dst) 388vpn_packet::set_hdr (ptype type_, unsigned int dst)
401{ 389{
407 srcdst = ((src >> 8) << 4) | (dst >> 8); 395 srcdst = ((src >> 8) << 4) | (dst >> 8);
408 dst1 = dst; 396 dst1 = dst;
409} 397}
410 398
411#define MAXVPNDATA (MAX_MTU - 6 - 6) 399#define MAXVPNDATA (MAX_MTU - 6 - 6)
412#define DATAHDR (sizeof (u32) + RAND_SIZE)
413 400
414struct vpndata_packet : vpn_packet 401struct vpndata_packet : vpn_packet
415{ 402{
416 u8 data[MAXVPNDATA + DATAHDR]; // seqno 403 u32 ctr; // seqno
404 u8 data[MAXVPNDATA];
417 405
418 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); 406 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
419 tap_packet *unpack (connection *conn, u32 &seqno); 407 tap_packet *unpack (connection *conn, u32 &seqno);
420 408
421private: 409private:
422 const u32 data_hdr_size () const 410 const u32 data_hdr_size () const
423 { 411 {
424 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 412 // the distance from beginning of packet to data member
413 return data - at (0);
425 } 414 }
426}; 415};
416
417// expands packet counter (unlike seqno, in network byte order) to counter mode IV
418static unsigned char *
419expand_iv (u32 ctr)
420{
421 static u32 iv[IV_SIZE (CIPHER) / 4];
422
423 require (sizeof (iv) == 4 * 4);
424 require (IV_SIZE (CIPHER) % 4 == 0);
425
426 iv[0] =
427 iv[1] =
428 iv[2] = ctr;
429
430 // I would reuse ctr here to to avoid potential endianness issues,
431 // but it seems openssl wraps around. While this would be still ok,
432 // and I don't even know if its true, let's play safe and initialise
433 // to 0.
434 iv[3] = 0;
435
436 return (unsigned char *)iv;
437}
427 438
428void 439void
429vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) 440vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno)
430{ 441{
431 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 442 EVP_CIPHER_CTX *cctx = &conn->octx->cctx;
449 d[1] = cl; 460 d[1] = cl;
450 } 461 }
451 } 462 }
452#endif 463#endif
453 464
454 require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 1)); 465 ctr = htonl (seqno);
455 466
456 struct { 467 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 468
470 require (EVP_EncryptUpdate (cctx, 469 require (EVP_EncryptUpdate (cctx,
471 (unsigned char *) data + outl, &outl2, 470 (unsigned char *)data + outl, &outl2,
472 (unsigned char *) &datahdr, DATAHDR)); 471 (unsigned char *)d, l));
473 outl += outl2; 472 outl += outl2;
474 473
475 require (EVP_EncryptUpdate (cctx, 474 // it seems this is a nop for us, but we do it anyways
476 (unsigned char *) data + outl, &outl2, 475 require (EVP_EncryptFinal_ex (cctx, (unsigned char *)data + outl, &outl2));
477 (unsigned char *) d, l));
478 outl += outl2; 476 outl += outl2;
479 477
480 require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2));
481 outl += outl2;
482
483 len = outl + data_hdr_size (); 478 len = data_hdr_size () + outl;
484 479
485 set_hdr (type, dst); 480 set_hdr (type, dst);
486 481
487 hmac_set (conn->octx); 482 hmac_set (conn->octx);
488} 483}
492{ 487{
493 EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; 488 EVP_CIPHER_CTX *cctx = &conn->ictx->cctx;
494 int outl = 0, outl2; 489 int outl = 0, outl2;
495 tap_packet *p = new tap_packet; 490 tap_packet *p = new tap_packet;
496 u8 *d; 491 u8 *d;
497 u32 l = len - data_hdr_size ();
498 492
499 require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 0)); 493 seqno = ntohl (ctr);
494
495 require (EVP_DecryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr)));
500 496
501#if ENABLE_COMPRESSION 497#if ENABLE_COMPRESSION
502 u8 cdata[MAX_MTU]; 498 u8 cdata[MAX_MTU];
503 499
504 if (type == PT_DATA_COMPRESSED) 500 if (type == PT_DATA_COMPRESSED)
505 d = cdata; 501 d = cdata;
506 else 502 else
507#endif 503#endif
508 d = &(*p)[6 + 6] - DATAHDR; 504 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 505
516 // this can overwrite the len/dst/src fields 506 // this can overwrite the len/dst/src fields
517 require (EVP_DecryptUpdate (cctx, 507 require (EVP_DecryptUpdate (cctx,
518 d, &outl2, 508 d, &outl2,
519 (unsigned char *)&data, len - data_hdr_size ())); 509 (unsigned char *)&data, len - data_hdr_size ()));
520 outl += outl2; 510 outl += outl2;
521 511
512 // it seems this is a nop for us, but we do it anyways
522 require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); 513 require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2));
523 outl += outl2; 514 outl += outl2;
524 515
525 seqno = ntohl (*(u32 *)(d + RAND_SIZE));
526
527 id2mac (dst () ? dst() : THISNODE->id, p->dst); 516 id2mac (dst () ? dst() : THISNODE->id, p->dst);
528 id2mac (src (), p->src); 517 id2mac (src (), p->src);
529 518
530#if ENABLE_COMPRESSION 519#if ENABLE_COMPRESSION
531 if (type == PT_DATA_COMPRESSED) 520 if (type == PT_DATA_COMPRESSED)
532 { 521 {
533 u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; 522 u32 cl = (d[0] << 8) | d[1];
534 523
535 p->len = lzf_decompress (d + DATAHDR + 2, cl < MAX_MTU ? cl : 0, 524 p->len = lzf_decompress (d + 2, cl < MAX_MTU - 2 ? cl : 0,
536 &(*p)[6 + 6], MAX_MTU) 525 &(*p)[6 + 6], MAX_MTU)
537 + 6 + 6; 526 + 6 + 6;
538 } 527 }
539 else 528 else
540 p->len = outl + (6 + 6 - DATAHDR); 529 p->len = outl + (6 + 6);
541#endif 530#endif
542 531
543 return p; 532 return p;
544} 533}
545 534
552 } 541 }
553}; 542};
554 543
555struct config_packet : vpn_packet 544struct config_packet : vpn_packet
556{ 545{
546 u8 serial[SERIAL_SIZE];
557 u8 prot_major, prot_minor, randsize; 547 u8 prot_major, prot_minor, randsize;
558 u8 flags, features, pad6, pad7, pad8; 548 u8 flags, features, pad6, pad7, pad8;
559 u32 cipher_nid, mac_nid, auth_nid; 549 u32 cipher_nid, mac_nid, auth_nid;
560 550
561 void setup (ptype type, int dst); 551 void setup (ptype type, int dst);
562 bool chk_config () const; 552 bool chk_config (const conf_node *conf, const sockinfo &rsi) const;
563 553
564 static u8 get_features () 554 static u8 get_features ()
565 { 555 {
566 u8 f = 0; 556 u8 f = 0;
567#if ENABLE_COMPRESSION 557#if ENABLE_COMPRESSION
580void 570void
581config_packet::setup (ptype type, int dst) 571config_packet::setup (ptype type, int dst)
582{ 572{
583 prot_major = PROTOCOL_MAJOR; 573 prot_major = PROTOCOL_MAJOR;
584 prot_minor = PROTOCOL_MINOR; 574 prot_minor = PROTOCOL_MINOR;
585 randsize = RAND_SIZE;
586 flags = 0; 575 flags = 0;
587 features = get_features (); 576 features = get_features ();
577
578 strncpy ((char *)serial, conf.serial, sizeof (serial));
588 579
589 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); 580 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ()));
590 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); 581 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ()));
591 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); 582 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ()));
592 583
593 len = sizeof (*this) - sizeof (net_packet); 584 len = sizeof (*this) - sizeof (net_packet);
594 set_hdr (type, dst); 585 set_hdr (type, dst);
595} 586}
596 587
597bool 588bool
598config_packet::chk_config () const 589config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const
599{ 590{
600 if (prot_major != PROTOCOL_MAJOR) 591 if (prot_major != PROTOCOL_MAJOR)
601 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 592 slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"),
602 else if (randsize != RAND_SIZE) 593 conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR);
603 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
604 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) 594 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 ())); 595 slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"),
596 conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ()));
606 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) 597 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 ())); 598 slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"),
599 conf->nodename, (const char *)rsi, ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ()));
608 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) 600 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 ())); 601 slog (L_WARN, _("%s(%s): auth algo mismatch (remote %x <=> local %x)"),
602 conf->nodename, (const char *)rsi, ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ()));
610 else 603 else
604 {
605 int cmp = memcmp (serial, ::conf.serial, sizeof (serial));
606
607 if (cmp > 0)
608 slog (L_WARN, _("%s(%s): remote serial newer than local serial - outdated config?"),
609 conf->nodename, (const char *)rsi);
610 else if (cmp == 0)
611 return true; 611 return true;
612 }
612 613
613 return false; 614 return false;
614} 615}
615 616
616struct auth_req_packet : config_packet // UNPROTECTED 617struct auth_req_packet : config_packet // UNPROTECTED
630 631
631 len = sizeof (*this) - sizeof (net_packet); 632 len = sizeof (*this) - sizeof (net_packet);
632 } 633 }
633}; 634};
634 635
635struct auth_res_packet : config_packet // UNPROTECTED 636struct auth_res_packet : vpn_packet // UNPROTECTED
636{ 637{
637 auth_response response; 638 auth_response response;
638 639
639 auth_res_packet (int dst) 640 auth_res_packet (int dst)
640 { 641 {
641 config_packet::setup (PT_AUTH_RES, dst); 642 set_hdr (PT_AUTH_RES, dst);
642 643
643 len = sizeof (*this) - sizeof (net_packet); 644 len = sizeof (*this) - sizeof (net_packet);
644 } 645 }
645}; 646};
646 647
686 si = rsi; 687 si = rsi;
687 protocol = rsi.prot; 688 protocol = rsi.prot;
688 689
689 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), 690 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
690 conf->nodename, (const char *)rsi, 691 conf->nodename, (const char *)rsi,
691 is_direct ? "direct" : "forwarded", 692 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
692 PROTOCOL_MAJOR, prot_minor); 693 PROTOCOL_MAJOR, prot_minor);
693 694
694 if (::conf.script_node_up) 695 if (::conf.script_node_up)
695 { 696 {
696 run_script_cb *cb = new run_script_cb; 697 run_script_cb *cb = new run_script_cb;
702 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); 703 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff);
703 704
704 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); 705 delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1);
705 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; 706 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff;
706 707
707 oiv.reset ();
708
709 // make sure rekeying timeouts are slightly asymmetric 708 // make sure rekeying timeouts are slightly asymmetric
710 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 709 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
711 rekey.start (rekey_interval, rekey_interval); 710 rekey.start (rekey_interval, rekey_interval);
712 711
712 hmac_error = 0.;
713
713 keepalive.start (::conf.keepalive); 714 keepalive.start (::conf.keepalive);
714 715
715 // send queued packets 716 // send queued packets
716 if (ictx && octx)
717 {
718 while (tap_packet *p = (tap_packet *)data_queue.get ()) 717 while (tap_packet *p = (tap_packet *)data_queue.get ())
719 { 718 {
720 if (p->len) send_data_packet (p); 719 if (p->len) send_data_packet (p);
721 delete p; 720 delete p;
722 } 721 }
723 722
724 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 723 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
725 { 724 {
726 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 725 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
727 delete p; 726 delete p;
728 }
729 } 727 }
730 728
731 vpn->connection_established (this); 729 vpn->connection_established (this);
732} 730}
733 731
741 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); 739 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
742 protocol = 0; 740 protocol = 0;
743 } 741 }
744 742
745 si.set (conf, protocol); 743 si.set (conf, protocol);
746
747 is_direct = si.valid ();
748} 744}
749 745
750// ensure sockinfo is valid, forward if necessary 746// ensure sockinfo is valid, forward if necessary
751const sockinfo & 747const sockinfo &
752connection::forward_si (const sockinfo &si) const 748connection::forward_si (const sockinfo &si) const
771 767
772void 768void
773connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 769connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
774{ 770{
775 if (!vpn->send_vpn_packet (pkt, si, tos)) 771 if (!vpn->send_vpn_packet (pkt, si, tos))
776 reset_connection (); 772 reset_connection ("packet send error");
777} 773}
778 774
779void 775void
780connection::send_ping (const sockinfo &si, u8 pong) 776connection::send_ping (const sockinfo &si, u8 pong)
781{ 777{
820void 816void
821connection::send_auth_response (const sockinfo &si) 817connection::send_auth_response (const sockinfo &si)
822{ 818{
823 auth_res_packet *pkt = new auth_res_packet (conf->id); 819 auth_res_packet *pkt = new auth_res_packet (conf->id);
824 820
825 auth_hash (rcv_auth, pkt->response.mac);
826 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof (rcv_ecdh_b)); 821 memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b);
822 auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac);
827 823
828 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); 824 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 825 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
830 826
831 delete pkt; 827 delete pkt;
857 { 853 {
858 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection 854 // 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. 855 // 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 ()) 856 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ())
861 { 857 {
862 reset_connection (); 858 reset_connection ("no demand");
863 return; 859 return;
864 } 860 }
865 861
866 last_establish_attempt = ev_now (); 862 last_establish_attempt = ev_now ();
867 863
869 ? (retry_cnt & 3) + 1 865 ? (retry_cnt & 3) + 1
870 : 1 << (retry_cnt >> 2)); 866 : 1 << (retry_cnt >> 2));
871 867
872 reset_si (); 868 reset_si ();
873 869
874 bool slow = si.prot & PROT_SLOW; 870 bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power);
875 871
876 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 872 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
877 { 873 {
878 /*TODO*/ /* start the timer so we don't recurse endlessly */ 874 /*TODO*/ /* start the timer so we don't recurse endlessly */
879 w.start (1); 875 w.start (1);
889 885
890 slow = slow || (dsi.prot & PROT_SLOW); 886 slow = slow || (dsi.prot & PROT_SLOW);
891 887
892 if (dsi.valid () && auth_rate_limiter.can (dsi)) 888 if (dsi.valid () && auth_rate_limiter.can (dsi))
893 { 889 {
894 if (retry_cnt < 4) 890 // use ping after the first few retries
891 // TODO: on rekeys, the other node might not interpret ping correctly,
892 // TODO: as it will still have a valid connection
893 if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power))
895 send_auth_request (dsi, true); 894 send_auth_request (dsi, true);
896 else 895 else
897 send_ping (dsi, 0); 896 send_ping (dsi, 0);
898 } 897 }
899 } 898 }
900 899
901 retry_int *= slow ? 8. : 0.9; 900 retry_int *= slow ? 4. : 0.9;
902 901
903 if (retry_int < conf->max_retry) 902 if (retry_int < conf->max_retry)
904 retry_cnt++; 903 retry_cnt++;
905 else 904 else
906 retry_int = conf->max_retry; 905 retry_int = conf->max_retry;
908 w.start (retry_int); 907 w.start (retry_int);
909 } 908 }
910} 909}
911 910
912void 911void
913connection::reset_connection () 912connection::reset_connection (const char *reason)
914{ 913{
915 if (ictx && octx) 914 if (ictx && octx)
916 { 915 {
917 slog (L_INFO, _("%s(%s): connection lost"), 916 slog (L_INFO, _("%s(%s): connection lost (%s)"),
918 conf->nodename, (const char *)si); 917 conf->nodename, (const char *)si, reason);
919 918
920 if (::conf.script_node_down) 919 if (::conf.script_node_down)
921 { 920 {
922 run_script_cb *cb = new run_script_cb; 921 run_script_cb *cb = new run_script_cb;
923 cb->set<connection, &connection::script_node_down> (this); 922 cb->set<connection, &connection::script_node_down> (this);
947connection::shutdown () 946connection::shutdown ()
948{ 947{
949 if (ictx && octx) 948 if (ictx && octx)
950 send_reset (si); 949 send_reset (si);
951 950
952 reset_connection (); 951 reset_connection ("shutdown");
953} 952}
954 953
955// poor-man's rekeying 954// poor-man's rekeying
956inline void 955inline void
957connection::rekey_cb (ev::timer &w, int revents) 956connection::rekey_cb (ev::timer &w, int revents)
958{ 957{
959 reset_connection (); 958 reset_connection ("rekeying");
960 establish_connection (); 959 establish_connection ();
961} 960}
962 961
963void 962void
964connection::send_data_packet (tap_packet *pkt) 963connection::send_data_packet (tap_packet *pkt)
981 980
982void 981void
983connection::post_inject_queue () 982connection::post_inject_queue ()
984{ 983{
985 // force a connection every now and when when packets are sent (max 1/s) 984 // force a connection every now and when when packets are sent (max 1/s)
986 if (ev_now () - last_establish_attempt >= 0.95) // arbitrary 985 if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary
987 establish_connection.stop (); 986 establish_connection.stop ();
988 987
989 establish_connection (); 988 establish_connection ();
990} 989}
991 990
1051 // about our desire for communication. 1050 // about our desire for communication.
1052 establish_connection (); 1051 establish_connection ();
1053 break; 1052 break;
1054 1053
1055 case vpn_packet::PT_RESET: 1054 case vpn_packet::PT_RESET:
1055 slog (L_TRACE, "%s >> PT_RESET", conf->nodename);
1056
1057 if (ictx && octx)
1056 { 1058 {
1057 reset_connection (); 1059 reset_connection ("remote reset");
1058 1060
1059 config_packet *p = (config_packet *) pkt; 1061 config_packet *p = (config_packet *) pkt;
1060 1062
1061 if (!p->chk_config ()) 1063 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 (); 1064 establish_connection ();
1069 } 1065 }
1066
1070 break; 1067 break;
1071 1068
1072 case vpn_packet::PT_AUTH_REQ: 1069 case vpn_packet::PT_AUTH_REQ:
1073 if (auth_rate_limiter.can (rsi)) 1070 if (auth_rate_limiter.can (rsi))
1074 { 1071 {
1076 1073
1077 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", 1074 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1078 conf->nodename, p->initiate ? "initiate" : "reply", 1075 conf->nodename, p->initiate ? "initiate" : "reply",
1079 p->protocols, p->features); 1076 p->protocols, p->features);
1080 1077
1081 if (p->chk_config () && !memcmp (p->magic, MAGIC, 8)) 1078 if (memcmp (p->magic, MAGIC, 8))
1079 {
1080 slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"),
1081 conf->nodename, (const char *)rsi);
1082 }
1083 else if (p->chk_config (conf, rsi))
1082 { 1084 {
1083 if (p->prot_minor != PROTOCOL_MINOR) 1085 if (p->prot_minor != PROTOCOL_MINOR)
1084 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1086 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1085 conf->nodename, (const char *)rsi, 1087 conf->nodename, (const char *)rsi,
1086 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1088 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1087 1089
1088 if (p->initiate) 1090 if (p->initiate)
1091 {
1089 send_auth_request (rsi, false); 1092 send_auth_request (rsi, false);
1093
1094 if (ictx && octx)
1095 reset_connection ("reconnect");
1096 }
1090 1097
1091 auth_data auth; 1098 auth_data auth;
1092 1099
1093 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1100 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1094 { 1101 {
1095 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), 1102 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)); 1103 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0));
1097 } 1104 }
1098 else 1105 else
1099 { 1106 {
1100 bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); 1107 bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth);
1101 1108
1102 rcv_auth = auth; 1109 rcv_auth = auth;
1103 have_rcv_auth = true; 1110 have_rcv_auth = true;
1104 1111
1105 send_auth_response (rsi); 1112 send_auth_response (rsi);
1113 } 1120 }
1114 } 1121 }
1115 1122
1116 break; 1123 break;
1117 } 1124 }
1118 else
1119 slog (L_WARN, _("%s(%s): protocol mismatch."),
1120 conf->nodename, (const char *)rsi);
1121 1125
1122 send_reset (rsi); 1126 send_reset (rsi);
1123 } 1127 }
1124 1128
1125 break; 1129 break;
1128 { 1132 {
1129 auth_res_packet *p = (auth_res_packet *)pkt; 1133 auth_res_packet *p = (auth_res_packet *)pkt;
1130 1134
1131 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); 1135 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1132 1136
1133 if (p->chk_config ()) 1137 auth_mac local_mac;
1138 auth_hash (snd_auth, p->response.ecdh, local_mac);
1139
1140 if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac))
1134 { 1141 {
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."), 1142 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."),
1138 conf->nodename, (const char *)rsi); 1143 conf->nodename, (const char *)rsi);
1139 } 1144 }
1140 else if (!have_snd_auth) 1145 else if (!have_snd_auth)
1141 { 1146 {
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)); 1147 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1149 1148
1150 have_snd_auth = true; 1149 have_snd_auth = true;
1151 connection_established (rsi); 1150 connection_established (rsi);
1152 }
1153
1154 break;
1155 } 1151 }
1156 } 1152 }
1157
1158 send_reset (rsi);
1159 break; 1153 break;
1160 1154
1161 case vpn_packet::PT_DATA_COMPRESSED: 1155 case vpn_packet::PT_DATA_COMPRESSED:
1162#if !ENABLE_COMPRESSION 1156#if !ENABLE_COMPRESSION
1163 send_reset (rsi); 1157 send_reset (rsi);
1169 if (ictx && octx) 1163 if (ictx && octx)
1170 { 1164 {
1171 vpndata_packet *p = (vpndata_packet *)pkt; 1165 vpndata_packet *p = (vpndata_packet *)pkt;
1172 1166
1173 if (!p->hmac_chk (ictx)) 1167 if (!p->hmac_chk (ictx))
1168 {
1169 // rekeying often creates temporary hmac auth floods
1170 // we assume they don't take longer than a few seconds normally,
1171 // and suppress messages and resets during that time.
1172 //TODO: should be done per source address
1173 if (!hmac_error)
1174 {
1175 hmac_error = ev_now () + 3;
1176 break;
1177 }
1178 else if (hmac_error >= ev_now ())
1179 break; // silently suppress
1180 else
1181 {
1174 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1182 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1175 "could be an attack, or just corruption or a synchronization error."), 1183 "could be an attack, or just corruption or a synchronization error."),
1176 conf->nodename, (const char *)rsi); 1184 conf->nodename, (const char *)rsi);
1185 // reset
1186 }
1187 }
1177 else 1188 else
1178 { 1189 {
1179 u32 seqno; 1190 u32 seqno;
1180 tap_packet *d = p->unpack (this, seqno); 1191 tap_packet *d = p->unpack (this, seqno);
1181 int seqclass = iseqno.seqno_classify (seqno); 1192 int seqclass = iseqno.seqno_classify (seqno);
1193
1194 hmac_error = 0;
1182 1195
1183 if (seqclass == 0) // ok 1196 if (seqclass == 0) // ok
1184 { 1197 {
1185 vpn->tap->send (d); 1198 vpn->tap->send (d);
1186 1199
1313 1326
1314 if (when >= 0) 1327 if (when >= 0)
1315 w.start (when); 1328 w.start (when);
1316 else if (when < -15) 1329 else if (when < -15)
1317 { 1330 {
1318 reset_connection (); 1331 reset_connection ("keepalive overdue");
1319 establish_connection (); 1332 establish_connection ();
1320 } 1333 }
1321 else if (conf->connectmode != conf_node::C_ONDEMAND 1334 else if (conf->connectmode != conf_node::C_ONDEMAND
1322 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1335 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1323 { 1336 {
1327 else if (when >= -10) 1340 else if (when >= -10)
1328 // hold ondemand connections implicitly a few seconds longer 1341 // hold ondemand connections implicitly a few seconds longer
1329 // should delete octx, though, or something like that ;) 1342 // should delete octx, though, or something like that ;)
1330 w.start (when + 10); 1343 w.start (when + 10);
1331 else 1344 else
1332 reset_connection (); 1345 reset_connection ("keepalive timeout");
1333} 1346}
1334 1347
1335void 1348void
1336connection::send_connect_request (int id) 1349connection::send_connect_request (int id)
1337{ 1350{
1437 1450
1438 // queue a dummy packet to force an initial connection attempt 1451 // queue a dummy packet to force an initial connection attempt
1439 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1452 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1440 vpn_queue.put (new net_packet); 1453 vpn_queue.put (new net_packet);
1441 1454
1442 reset_connection (); 1455 reset_connection ("startup");
1443} 1456}
1444 1457
1445connection::~connection () 1458connection::~connection ()
1446{ 1459{
1447 shutdown (); 1460 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines