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.115 by root, Thu Jun 30 16:31:00 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
101 103
102////////////////////////////////////////////////////////////////////////////// 104//////////////////////////////////////////////////////////////////////////////
103 105
104struct crypto_ctx 106struct crypto_ctx
105{ 107{
106 EVP_CIPHER_CTX cctx; 108 cipher cctx;
107 HMAC_CTX hctx; 109 hmac hctx;
108 110
109 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);
110 ~crypto_ctx (); 112 ~crypto_ctx ();
111}; 113};
112 114
119 { 121 {
120 u8 mac_key[MAC_KEYSIZE]; 122 u8 mac_key[MAC_KEYSIZE];
121 static const unsigned char mac_info[] = "gvpe mac key"; 123 static const unsigned char mac_info[] = "gvpe mac key";
122 124
123 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); 125 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)); 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";
137 137
138 hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); 138 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)); 139 kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key));
141 kdf.extract (s, sizeof (s)); 140 kdf.extract (s, sizeof (s));
142 kdf.extract_done (HKDF_PRF_HASH ()); 141 kdf.extract_done (HKDF_PRF_HASH ());
143 kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); 142 kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info));
144 143
145 EVP_CIPHER_CTX_init (&cctx); 144 EVP_CIPHER_CTX_init (cctx);
146 require (EVP_CipherInit_ex (&cctx, CIPHER (), 0, cipher_key, 0, enc)); 145 require (EVP_CipherInit_ex (cctx, CIPHER (), 0, cipher_key, 0, enc));
147 } 146 }
148} 147}
149 148
150crypto_ctx::~crypto_ctx () 149crypto_ctx::~crypto_ctx ()
151{ 150{
152 require (EVP_CIPHER_CTX_cleanup (&cctx)); 151 require (EVP_CIPHER_CTX_cleanup (cctx));
153 HMAC_CTX_cleanup (&hctx);
154} 152}
155 153
156static inline void 154static inline void
157auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr) 155auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr)
158{ 156{
159 if (RSA_public_encrypt (sizeof (auth.rsa), 157 if (RSA_public_encrypt (sizeof (auth.rsa),
160 (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa, 158 (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa,
161 key, RSA_PKCS1_OAEP_PADDING) < 0) 159 key, RSA_PKCS1_OAEP_PADDING) < 0)
162 fatal ("RSA_public_encrypt error"); 160 fatal ("RSA_public_encrypt error");
161
162 memcpy (&encr.ecdh, &auth.ecdh, sizeof (encr.ecdh));
163} 163}
164 164
165static inline bool 165static inline bool
166auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth) 166auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth)
167{ 167{
171 (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt, 171 (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt,
172 key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa)) 172 key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa))
173 return 0; 173 return 0;
174 174
175 memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa)); 175 memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa));
176 memcpy (&auth.ecdh, &encr.ecdh, sizeof (auth.ecdh));
176 177
177 return 1; 178 return 1;
178} 179}
179 180
180static void 181static void
181auth_hash (const auth_data &auth, auth_mac &mac) 182auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
182{ 183{
183 HMAC_CTX ctx; 184 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
184 185 kdf.extract (&auth.rsa, sizeof (auth.rsa));
185 HMAC_CTX_init (&ctx); 186 kdf.extract_done ();
186 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
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} 188}
191 189
192void 190void
193connection::generate_auth_data () 191connection::generate_auth_data ()
194{ 192{
195 if (auth_expire < ev_now ()) 193 if (auth_expire < ev_now ())
196 { 194 {
197 // request data 195 // request data
198 RAND_bytes ((unsigned char *)&snd_auth.rsa, sizeof snd_auth.rsa); 196 rand_fill (snd_auth.rsa);
199 curve25519_generate (snd_ecdh_a, snd_auth.ecdh); 197 curve25519_generate (snd_ecdh_a, snd_auth.ecdh);
200 auth_hash (snd_auth, snd_auth_mac);
201 198
202 // eventual response data 199 // eventual response data
203 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); 200 curve25519_generate (rcv_ecdh_a, rcv_ecdh_b);
204 } 201 }
205 202
361 } 358 }
362} 359}
363 360
364///////////////////////////////////////////////////////////////////////////// 361/////////////////////////////////////////////////////////////////////////////
365 362
366unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
367
368void 363void
369hmac_packet::hmac_gen (crypto_ctx *ctx) 364hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
370{ 365{
371 unsigned int xlen; 366 ctx->hctx.init ();
372 367 ctx->hctx.add (((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet));
373 HMAC_CTX *hctx = &ctx->hctx; 368 ctx->hctx.digest (hmac_digest);
374
375 require (HMAC_Init_ex (hctx, 0, 0, 0, 0));
376 require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet),
377 len - sizeof (hmac_packet)));
378 require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen));
379} 369}
380 370
381void 371void
382hmac_packet::hmac_set (crypto_ctx *ctx) 372hmac_packet::hmac_set (crypto_ctx *ctx)
383{ 373{
384 hmac_gen (ctx); 374 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
385 375 hmac_gen (ctx, hmac_digest);
386 memcpy (hmac, hmac_digest, HMACLENGTH); 376 memcpy (hmac, hmac_digest, HMACLENGTH);
387} 377}
388 378
389bool 379bool
390hmac_packet::hmac_chk (crypto_ctx *ctx) 380hmac_packet::hmac_chk (crypto_ctx *ctx)
391{ 381{
392 hmac_gen (ctx); 382 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
393 383 hmac_gen (ctx, hmac_digest);
394 return !memcmp (hmac, hmac_digest, HMACLENGTH); 384 return slow_memeq (hmac, hmac_digest, HMACLENGTH);
395} 385}
396 386
397void 387void
398vpn_packet::set_hdr (ptype type_, unsigned int dst) 388vpn_packet::set_hdr (ptype type_, unsigned int dst)
399{ 389{
405 srcdst = ((src >> 8) << 4) | (dst >> 8); 395 srcdst = ((src >> 8) << 4) | (dst >> 8);
406 dst1 = dst; 396 dst1 = dst;
407} 397}
408 398
409#define MAXVPNDATA (MAX_MTU - 6 - 6) 399#define MAXVPNDATA (MAX_MTU - 6 - 6)
410#define DATAHDR (sizeof (u32) + RAND_SIZE)
411 400
412struct vpndata_packet : vpn_packet 401struct vpndata_packet : vpn_packet
413{ 402{
414 u8 data[MAXVPNDATA + DATAHDR]; // seqno 403 u32 ctr; // seqno
404 u8 data[MAXVPNDATA];
415 405
416 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);
417 tap_packet *unpack (connection *conn, u32 &seqno); 407 tap_packet *unpack (connection *conn, u32 &seqno);
418 408
419private: 409private:
420 const u32 data_hdr_size () const 410 const u32 data_hdr_size () const
421 { 411 {
422 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 412 // the distance from beginning of packet to data member
413 return data - at (0);
423 } 414 }
424}; 415};
425 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}
438
426void 439void
427vpndata_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)
428{ 441{
429 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 442 EVP_CIPHER_CTX *cctx = conn->octx->cctx;
430 int outl = 0, outl2; 443 int outl = 0, outl2;
431 ptype type = PT_DATA_UNCOMPRESSED; 444 ptype type = PT_DATA_UNCOMPRESSED;
432 445
433#if ENABLE_COMPRESSION 446#if ENABLE_COMPRESSION
434 u8 cdata[MAX_MTU]; 447 u8 cdata[MAX_MTU];
447 d[1] = cl; 460 d[1] = cl;
448 } 461 }
449 } 462 }
450#endif 463#endif
451 464
452 require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 1)); 465 ctr = htonl (seqno);
453 466
454 struct { 467 require (EVP_EncryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr)));
455#if RAND_SIZE
456 u8 rnd[RAND_SIZE];
457#endif
458 u32 seqno;
459 } datahdr;
460
461 datahdr.seqno = ntohl (seqno);
462#if RAND_SIZE
463 RAND_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE);
464#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 *)d, l));
469 outl += outl2; 472 outl += outl2;
470 473
471 require (EVP_EncryptUpdate (cctx, 474 // it seems this is a nop for us, but we do it anyways
472 (unsigned char *) data + outl, &outl2, 475 require (EVP_EncryptFinal_ex (cctx, (unsigned char *)data + outl, &outl2));
473 (unsigned char *) d, l));
474 outl += outl2; 476 outl += outl2;
475 477
476 require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2));
477 outl += outl2;
478
479 len = outl + data_hdr_size (); 478 len = data_hdr_size () + outl;
480 479
481 set_hdr (type, dst); 480 set_hdr (type, dst);
482 481
483 hmac_set (conn->octx); 482 hmac_set (conn->octx);
484} 483}
485 484
486tap_packet * 485tap_packet *
487vpndata_packet::unpack (connection *conn, u32 &seqno) 486vpndata_packet::unpack (connection *conn, u32 &seqno)
488{ 487{
489 EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; 488 EVP_CIPHER_CTX *cctx = conn->ictx->cctx;
490 int outl = 0, outl2; 489 int outl = 0, outl2;
491 tap_packet *p = new tap_packet; 490 tap_packet *p = new tap_packet;
492 u8 *d; 491 u8 *d;
493 u32 l = len - data_hdr_size ();
494 492
495 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)));
496 496
497#if ENABLE_COMPRESSION 497#if ENABLE_COMPRESSION
498 u8 cdata[MAX_MTU]; 498 u8 cdata[MAX_MTU];
499 499
500 if (type == PT_DATA_COMPRESSED) 500 if (type == PT_DATA_COMPRESSED)
501 d = cdata; 501 d = cdata;
502 else 502 else
503#endif 503#endif
504 d = &(*p)[6 + 6 - DATAHDR]; 504 d = &(*p)[6 + 6];
505 505
506 /* this overwrites part of the src mac, but we fix that later */ 506 // this can overwrite the len/dst/src fields
507 require (EVP_DecryptUpdate (cctx, 507 require (EVP_DecryptUpdate (cctx,
508 d, &outl2, 508 d, &outl2,
509 (unsigned char *)&data, len - data_hdr_size ())); 509 (unsigned char *)&data, len - data_hdr_size ()));
510 outl += outl2; 510 outl += outl2;
511 511
512 // it seems this is a nop for us, but we do it anyways
512 require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); 513 require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2));
513 outl += outl2; 514 outl += outl2;
514 515
515 seqno = ntohl (*(u32 *)(d + RAND_SIZE));
516
517 id2mac (dst () ? dst() : THISNODE->id, p->dst); 516 id2mac (dst () ? dst() : THISNODE->id, p->dst);
518 id2mac (src (), p->src); 517 id2mac (src (), p->src);
519 518
520#if ENABLE_COMPRESSION 519#if ENABLE_COMPRESSION
521 if (type == PT_DATA_COMPRESSED) 520 if (type == PT_DATA_COMPRESSED)
522 { 521 {
523 u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; 522 u32 cl = (d[0] << 8) | d[1];
524 523
525 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,
526 &(*p)[6 + 6], MAX_MTU) 525 &(*p)[6 + 6], MAX_MTU)
527 + 6 + 6; 526 + 6 + 6;
528 } 527 }
529 else 528 else
530 p->len = outl + (6 + 6 - DATAHDR); 529 p->len = outl + (6 + 6);
531#endif 530#endif
532 531
533 return p; 532 return p;
534} 533}
535 534
542 } 541 }
543}; 542};
544 543
545struct config_packet : vpn_packet 544struct config_packet : vpn_packet
546{ 545{
546 u8 serial[SERIAL_SIZE];
547 u8 prot_major, prot_minor, randsize; 547 u8 prot_major, prot_minor, randsize;
548 u8 flags, features, pad6, pad7, pad8; 548 u8 flags, features, pad6, pad7, pad8;
549 u32 cipher_nid, mac_nid, auth_nid; 549 u32 cipher_nid, mac_nid, auth_nid;
550 550
551 void setup (ptype type, int dst); 551 void setup (ptype type, int dst);
552 bool chk_config () const; 552 bool chk_config (const conf_node *conf, const sockinfo &rsi) const;
553 553
554 static u8 get_features () 554 static u8 get_features ()
555 { 555 {
556 u8 f = 0; 556 u8 f = 0;
557#if ENABLE_COMPRESSION 557#if ENABLE_COMPRESSION
570void 570void
571config_packet::setup (ptype type, int dst) 571config_packet::setup (ptype type, int dst)
572{ 572{
573 prot_major = PROTOCOL_MAJOR; 573 prot_major = PROTOCOL_MAJOR;
574 prot_minor = PROTOCOL_MINOR; 574 prot_minor = PROTOCOL_MINOR;
575 randsize = RAND_SIZE;
576 flags = 0; 575 flags = 0;
577 features = get_features (); 576 features = get_features ();
577
578 strncpy ((char *)serial, conf.serial, sizeof (serial));
578 579
579 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); 580 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ()));
580 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); 581 mac_nid = htonl (EVP_MD_type (MAC_DIGEST ()));
581 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); 582 auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ()));
582 583
583 len = sizeof (*this) - sizeof (net_packet); 584 len = sizeof (*this) - sizeof (net_packet);
584 set_hdr (type, dst); 585 set_hdr (type, dst);
585} 586}
586 587
587bool 588bool
588config_packet::chk_config () const 589config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const
589{ 590{
590 if (prot_major != PROTOCOL_MAJOR) 591 if (prot_major != PROTOCOL_MAJOR)
591 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)"),
592 else if (randsize != RAND_SIZE) 593 conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR);
593 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
594 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) 594 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 ())); 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 ()));
596 else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) 597 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 ())); 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 ()));
598 else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) 600 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 ())); 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 ()));
600 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)
601 return true; 611 return true;
612 }
602 613
603 return false; 614 return false;
604} 615}
605 616
606struct auth_req_packet : config_packet // UNPROTECTED 617struct auth_req_packet : config_packet // UNPROTECTED
620 631
621 len = sizeof (*this) - sizeof (net_packet); 632 len = sizeof (*this) - sizeof (net_packet);
622 } 633 }
623}; 634};
624 635
625struct auth_res_packet : config_packet // UNPROTECTED 636struct auth_res_packet : vpn_packet // UNPROTECTED
626{ 637{
627 auth_response response; 638 auth_response response;
628 639
629 auth_res_packet (int dst) 640 auth_res_packet (int dst)
630 { 641 {
631 config_packet::setup (PT_AUTH_RES, dst); 642 set_hdr (PT_AUTH_RES, dst);
632 643
633 len = sizeof (*this) - sizeof (net_packet); 644 len = sizeof (*this) - sizeof (net_packet);
634 } 645 }
635}; 646};
636 647
676 si = rsi; 687 si = rsi;
677 protocol = rsi.prot; 688 protocol = rsi.prot;
678 689
679 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."),
680 conf->nodename, (const char *)rsi, 691 conf->nodename, (const char *)rsi,
681 is_direct ? "direct" : "forwarded", 692 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
682 PROTOCOL_MAJOR, prot_minor); 693 PROTOCOL_MAJOR, prot_minor);
683 694
684 if (::conf.script_node_up) 695 if (::conf.script_node_up)
685 { 696 {
686 run_script_cb *cb = new run_script_cb; 697 run_script_cb *cb = new run_script_cb;
692 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); 703 iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff);
693 704
694 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);
695 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; 706 oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff;
696 707
697 if (ictx && octx)
698 {
699 // make sure rekeying timeouts are slightly asymmetric 708 // make sure rekeying timeouts are slightly asymmetric
700 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);
701 rekey.start (rekey_interval, rekey_interval); 710 rekey.start (rekey_interval, rekey_interval);
702 711
712 hmac_error = 0.;
713
703 keepalive.start (::conf.keepalive); 714 keepalive.start (::conf.keepalive);
704 715
705 // send queued packets 716 // send queued packets
706 if (ictx && octx)
707 {
708 while (tap_packet *p = (tap_packet *)data_queue.get ()) 717 while (tap_packet *p = (tap_packet *)data_queue.get ())
709 { 718 {
710 if (p->len) send_data_packet (p); 719 if (p->len) send_data_packet (p);
711 delete p; 720 delete p;
712 } 721 }
713 722
714 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 723 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
715 { 724 {
716 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 725 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
717 delete p; 726 delete p;
718 } 727 }
719 }
720 728
721 vpn->connection_established (this); 729 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} 730}
733 731
734void 732void
735connection::reset_si () 733connection::reset_si ()
736{ 734{
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