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 | |
105 | struct crypto_ctx |
106 | struct 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 | |
149 | crypto_ctx::~crypto_ctx () |
149 | crypto_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 | |
155 | static inline void |
154 | static inline void |
156 | auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr) |
155 | auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr) |
157 | { |
156 | { |
… | |
… | |
180 | } |
179 | } |
181 | |
180 | |
182 | static void |
181 | static void |
183 | auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) |
182 | auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) |
184 | { |
183 | { |
185 | hkdf kdf (&auth.ecdh, sizeof (auth.ecdh), AUTH_DIGEST ()); // use remote ecdh b as salt |
184 | hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt |
186 | kdf.extract (&auth.rsa, sizeof (auth.rsa)); |
185 | kdf.extract (&auth.rsa, sizeof (auth.rsa)); |
187 | kdf.extract_done (); |
186 | kdf.extract_done (); |
188 | kdf.expand (mac, sizeof mac, b, sizeof b); // use response ecdh b as info |
187 | kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info |
189 | } |
188 | } |
190 | |
189 | |
191 | void |
190 | void |
192 | connection::generate_auth_data () |
191 | connection::generate_auth_data () |
193 | { |
192 | { |
… | |
… | |
362 | ///////////////////////////////////////////////////////////////////////////// |
361 | ///////////////////////////////////////////////////////////////////////////// |
363 | |
362 | |
364 | void |
363 | void |
365 | hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) |
364 | hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) |
366 | { |
365 | { |
367 | HMAC_CTX *hctx = &ctx->hctx; |
366 | ctx->hctx.init (); |
368 | |
|
|
369 | require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); |
|
|
370 | require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet))); |
367 | ctx->hctx.add (((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)); |
371 | require (HMAC_Final (hctx, hmac_digest, 0)); |
368 | ctx->hctx.digest (hmac_digest); |
372 | } |
369 | } |
373 | |
370 | |
374 | void |
371 | void |
375 | hmac_packet::hmac_set (crypto_ctx *ctx) |
372 | hmac_packet::hmac_set (crypto_ctx *ctx) |
376 | { |
373 | { |
… | |
… | |
382 | bool |
379 | bool |
383 | hmac_packet::hmac_chk (crypto_ctx *ctx) |
380 | hmac_packet::hmac_chk (crypto_ctx *ctx) |
384 | { |
381 | { |
385 | unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
382 | unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
386 | hmac_gen (ctx, hmac_digest); |
383 | hmac_gen (ctx, hmac_digest); |
387 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
384 | return slow_memeq (hmac, hmac_digest, HMACLENGTH); |
388 | } |
385 | } |
389 | |
386 | |
390 | void |
387 | void |
391 | vpn_packet::set_hdr (ptype type_, unsigned int dst) |
388 | vpn_packet::set_hdr (ptype type_, unsigned int dst) |
392 | { |
389 | { |
… | |
… | |
398 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
395 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
399 | dst1 = dst; |
396 | dst1 = dst; |
400 | } |
397 | } |
401 | |
398 | |
402 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
399 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
403 | #define DATAHDR (sizeof (u32) + RAND_SIZE) |
|
|
404 | |
400 | |
405 | struct vpndata_packet : vpn_packet |
401 | struct vpndata_packet : vpn_packet |
406 | { |
402 | { |
407 | u8 data[MAXVPNDATA + DATAHDR]; // seqno |
403 | u32 ctr; // seqno |
|
|
404 | u8 data[MAXVPNDATA]; |
408 | |
405 | |
409 | 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); |
410 | tap_packet *unpack (connection *conn, u32 &seqno); |
407 | tap_packet *unpack (connection *conn, u32 &seqno); |
411 | |
408 | |
412 | private: |
409 | private: |
413 | const u32 data_hdr_size () const |
410 | const u32 data_hdr_size () const |
414 | { |
411 | { |
415 | return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
412 | // the distance from beginning of packet to data member |
|
|
413 | return data - at (0); |
416 | } |
414 | } |
417 | }; |
415 | }; |
|
|
416 | |
|
|
417 | // expands packet counter (unlike seqno, in network byte order) to counter mode IV |
|
|
418 | static unsigned char * |
|
|
419 | expand_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 | } |
418 | |
438 | |
419 | void |
439 | void |
420 | vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) |
440 | vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) |
421 | { |
441 | { |
422 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
442 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
… | |
… | |
440 | d[1] = cl; |
460 | d[1] = cl; |
441 | } |
461 | } |
442 | } |
462 | } |
443 | #endif |
463 | #endif |
444 | |
464 | |
445 | require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 1)); |
465 | ctr = htonl (seqno); |
446 | |
466 | |
447 | struct { |
467 | require (EVP_EncryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr))); |
448 | #if RAND_SIZE |
|
|
449 | u8 rnd[RAND_SIZE]; |
|
|
450 | #endif |
|
|
451 | u32 seqno; |
|
|
452 | } datahdr; |
|
|
453 | |
|
|
454 | datahdr.seqno = ntohl (seqno); |
|
|
455 | #if RAND_SIZE |
|
|
456 | // NB: a constant (per session) random prefix |
|
|
457 | // is likely enough, but we don't take any chances. |
|
|
458 | conn->oiv.get (datahdr.rnd, RAND_SIZE); |
|
|
459 | #endif |
|
|
460 | |
468 | |
461 | require (EVP_EncryptUpdate (cctx, |
469 | require (EVP_EncryptUpdate (cctx, |
462 | (unsigned char *) data + outl, &outl2, |
470 | (unsigned char *)data + outl, &outl2, |
463 | (unsigned char *) &datahdr, DATAHDR)); |
471 | (unsigned char *)d, l)); |
464 | outl += outl2; |
472 | outl += outl2; |
465 | |
473 | |
466 | require (EVP_EncryptUpdate (cctx, |
474 | // it seems this is a nop for us, but we do it anyways |
467 | (unsigned char *) data + outl, &outl2, |
475 | require (EVP_EncryptFinal_ex (cctx, (unsigned char *)data + outl, &outl2)); |
468 | (unsigned char *) d, l)); |
|
|
469 | outl += outl2; |
476 | outl += outl2; |
470 | |
477 | |
471 | require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2)); |
|
|
472 | outl += outl2; |
|
|
473 | |
|
|
474 | len = outl + data_hdr_size (); |
478 | len = data_hdr_size () + outl; |
475 | |
479 | |
476 | set_hdr (type, dst); |
480 | set_hdr (type, dst); |
477 | |
481 | |
478 | hmac_set (conn->octx); |
482 | hmac_set (conn->octx); |
479 | } |
483 | } |
… | |
… | |
483 | { |
487 | { |
484 | EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; |
488 | EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; |
485 | int outl = 0, outl2; |
489 | int outl = 0, outl2; |
486 | tap_packet *p = new tap_packet; |
490 | tap_packet *p = new tap_packet; |
487 | u8 *d; |
491 | u8 *d; |
488 | u32 l = len - data_hdr_size (); |
|
|
489 | |
492 | |
490 | 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))); |
491 | |
496 | |
492 | #if ENABLE_COMPRESSION |
497 | #if ENABLE_COMPRESSION |
493 | u8 cdata[MAX_MTU]; |
498 | u8 cdata[MAX_MTU]; |
494 | |
499 | |
495 | if (type == PT_DATA_COMPRESSED) |
500 | if (type == PT_DATA_COMPRESSED) |
496 | d = cdata; |
501 | d = cdata; |
497 | else |
502 | else |
498 | #endif |
503 | #endif |
499 | d = &(*p)[6 + 6] - DATAHDR; |
504 | d = &(*p)[6 + 6]; |
500 | |
|
|
501 | // we play do evil games with the struct layout atm. |
|
|
502 | // pending better solutions, we at least do some verification. |
|
|
503 | // this is fine, as we left ISO territory long ago. |
|
|
504 | require (DATAHDR <= 16); |
|
|
505 | require ((u8 *)(&p->len + 1) == &(*p)[0]); |
|
|
506 | |
505 | |
507 | // this can overwrite the len/dst/src fields |
506 | // this can overwrite the len/dst/src fields |
508 | require (EVP_DecryptUpdate (cctx, |
507 | require (EVP_DecryptUpdate (cctx, |
509 | d, &outl2, |
508 | d, &outl2, |
510 | (unsigned char *)&data, len - data_hdr_size ())); |
509 | (unsigned char *)&data, len - data_hdr_size ())); |
511 | outl += outl2; |
510 | outl += outl2; |
512 | |
511 | |
|
|
512 | // it seems this is a nop for us, but we do it anyways |
513 | require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); |
513 | require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); |
514 | outl += outl2; |
514 | outl += outl2; |
515 | |
515 | |
516 | seqno = ntohl (*(u32 *)(d + RAND_SIZE)); |
|
|
517 | |
|
|
518 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
516 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
519 | id2mac (src (), p->src); |
517 | id2mac (src (), p->src); |
520 | |
518 | |
521 | #if ENABLE_COMPRESSION |
519 | #if ENABLE_COMPRESSION |
522 | if (type == PT_DATA_COMPRESSED) |
520 | if (type == PT_DATA_COMPRESSED) |
523 | { |
521 | { |
524 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
522 | u32 cl = (d[0] << 8) | d[1]; |
525 | |
523 | |
526 | 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, |
527 | &(*p)[6 + 6], MAX_MTU) |
525 | &(*p)[6 + 6], MAX_MTU) |
528 | + 6 + 6; |
526 | + 6 + 6; |
529 | } |
527 | } |
530 | else |
528 | else |
531 | p->len = outl + (6 + 6 - DATAHDR); |
529 | p->len = outl + (6 + 6); |
532 | #endif |
530 | #endif |
533 | |
531 | |
534 | return p; |
532 | return p; |
535 | } |
533 | } |
536 | |
534 | |
… | |
… | |
572 | void |
570 | void |
573 | config_packet::setup (ptype type, int dst) |
571 | config_packet::setup (ptype type, int dst) |
574 | { |
572 | { |
575 | prot_major = PROTOCOL_MAJOR; |
573 | prot_major = PROTOCOL_MAJOR; |
576 | prot_minor = PROTOCOL_MINOR; |
574 | prot_minor = PROTOCOL_MINOR; |
577 | randsize = RAND_SIZE; |
|
|
578 | flags = 0; |
575 | flags = 0; |
579 | features = get_features (); |
576 | features = get_features (); |
580 | |
577 | |
581 | strncpy ((char *)serial, conf.serial, sizeof (serial)); |
578 | strncpy ((char *)serial, conf.serial, sizeof (serial)); |
582 | |
579 | |
… | |
… | |
592 | config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const |
589 | config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const |
593 | { |
590 | { |
594 | if (prot_major != PROTOCOL_MAJOR) |
591 | if (prot_major != PROTOCOL_MAJOR) |
595 | slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"), |
592 | slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"), |
596 | conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); |
593 | conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); |
597 | else if (randsize != RAND_SIZE) |
|
|
598 | slog (L_WARN, _("%s(%s): rand size mismatch (remote %d <=> local %d)"), |
|
|
599 | conf->nodename, (const char *)rsi, randsize, RAND_SIZE); |
|
|
600 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) |
594 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) |
601 | slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"), |
595 | slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"), |
602 | conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); |
596 | conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); |
603 | else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) |
597 | else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) |
604 | slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"), |
598 | slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"), |
… | |
… | |
693 | si = rsi; |
687 | si = rsi; |
694 | protocol = rsi.prot; |
688 | protocol = rsi.prot; |
695 | |
689 | |
696 | 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."), |
697 | conf->nodename, (const char *)rsi, |
691 | conf->nodename, (const char *)rsi, |
698 | is_direct ? "direct" : "forwarded", |
692 | vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded", |
699 | PROTOCOL_MAJOR, prot_minor); |
693 | PROTOCOL_MAJOR, prot_minor); |
700 | |
694 | |
701 | if (::conf.script_node_up) |
695 | if (::conf.script_node_up) |
702 | { |
696 | { |
703 | run_script_cb *cb = new run_script_cb; |
697 | run_script_cb *cb = new run_script_cb; |
… | |
… | |
709 | iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); |
703 | iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); |
710 | |
704 | |
711 | 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); |
712 | oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; |
706 | oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; |
713 | |
707 | |
714 | oiv.reset (); |
|
|
715 | |
|
|
716 | // make sure rekeying timeouts are slightly asymmetric |
708 | // make sure rekeying timeouts are slightly asymmetric |
717 | 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); |
718 | rekey.start (rekey_interval, rekey_interval); |
710 | rekey.start (rekey_interval, rekey_interval); |
719 | |
711 | |
|
|
712 | hmac_error = 0.; |
|
|
713 | |
720 | keepalive.start (::conf.keepalive); |
714 | keepalive.start (::conf.keepalive); |
721 | |
715 | |
722 | // send queued packets |
716 | // send queued packets |
723 | if (ictx && octx) |
|
|
724 | { |
|
|
725 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
717 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
726 | { |
718 | { |
727 | if (p->len) send_data_packet (p); |
719 | if (p->len) send_data_packet (p); |
728 | delete p; |
720 | delete p; |
729 | } |
721 | } |
730 | |
722 | |
731 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
723 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
732 | { |
724 | { |
733 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
725 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
734 | delete p; |
726 | delete p; |
735 | } |
|
|
736 | } |
727 | } |
737 | |
728 | |
738 | vpn->connection_established (this); |
729 | vpn->connection_established (this); |
739 | } |
730 | } |
740 | |
731 | |
… | |
… | |
748 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
739 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
749 | protocol = 0; |
740 | protocol = 0; |
750 | } |
741 | } |
751 | |
742 | |
752 | si.set (conf, protocol); |
743 | si.set (conf, protocol); |
753 | |
|
|
754 | is_direct = si.valid (); |
|
|
755 | } |
744 | } |
756 | |
745 | |
757 | // ensure sockinfo is valid, forward if necessary |
746 | // ensure sockinfo is valid, forward if necessary |
758 | const sockinfo & |
747 | const sockinfo & |
759 | connection::forward_si (const sockinfo &si) const |
748 | connection::forward_si (const sockinfo &si) const |
… | |
… | |
778 | |
767 | |
779 | void |
768 | void |
780 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
769 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
781 | { |
770 | { |
782 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
771 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
783 | reset_connection (); |
772 | reset_connection ("packet send error"); |
784 | } |
773 | } |
785 | |
774 | |
786 | void |
775 | void |
787 | connection::send_ping (const sockinfo &si, u8 pong) |
776 | connection::send_ping (const sockinfo &si, u8 pong) |
788 | { |
777 | { |
… | |
… | |
864 | { |
853 | { |
865 | // 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 |
866 | // 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. |
867 | 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 ()) |
868 | { |
857 | { |
869 | reset_connection (); |
858 | reset_connection ("no demand"); |
870 | return; |
859 | return; |
871 | } |
860 | } |
872 | |
861 | |
873 | last_establish_attempt = ev_now (); |
862 | last_establish_attempt = ev_now (); |
874 | |
863 | |
… | |
… | |
876 | ? (retry_cnt & 3) + 1 |
865 | ? (retry_cnt & 3) + 1 |
877 | : 1 << (retry_cnt >> 2)); |
866 | : 1 << (retry_cnt >> 2)); |
878 | |
867 | |
879 | reset_si (); |
868 | reset_si (); |
880 | |
869 | |
881 | bool slow = si.prot & PROT_SLOW; |
870 | bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power); |
882 | |
871 | |
883 | if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) |
872 | if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) |
884 | { |
873 | { |
885 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
874 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
886 | w.start (1); |
875 | w.start (1); |
… | |
… | |
896 | |
885 | |
897 | slow = slow || (dsi.prot & PROT_SLOW); |
886 | slow = slow || (dsi.prot & PROT_SLOW); |
898 | |
887 | |
899 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
888 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
900 | { |
889 | { |
901 | 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)) |
902 | send_auth_request (dsi, true); |
894 | send_auth_request (dsi, true); |
903 | else |
895 | else |
904 | send_ping (dsi, 0); |
896 | send_ping (dsi, 0); |
905 | } |
897 | } |
906 | } |
898 | } |
907 | |
899 | |
908 | retry_int *= slow ? 8. : 0.9; |
900 | retry_int *= slow ? 4. : 0.9; |
909 | |
901 | |
910 | if (retry_int < conf->max_retry) |
902 | if (retry_int < conf->max_retry) |
911 | retry_cnt++; |
903 | retry_cnt++; |
912 | else |
904 | else |
913 | retry_int = conf->max_retry; |
905 | retry_int = conf->max_retry; |
… | |
… | |
915 | w.start (retry_int); |
907 | w.start (retry_int); |
916 | } |
908 | } |
917 | } |
909 | } |
918 | |
910 | |
919 | void |
911 | void |
920 | connection::reset_connection () |
912 | connection::reset_connection (const char *reason) |
921 | { |
913 | { |
922 | if (ictx && octx) |
914 | if (ictx && octx) |
923 | { |
915 | { |
924 | slog (L_INFO, _("%s(%s): connection lost"), |
916 | slog (L_INFO, _("%s(%s): connection lost (%s)"), |
925 | conf->nodename, (const char *)si); |
917 | conf->nodename, (const char *)si, reason); |
926 | |
918 | |
927 | if (::conf.script_node_down) |
919 | if (::conf.script_node_down) |
928 | { |
920 | { |
929 | run_script_cb *cb = new run_script_cb; |
921 | run_script_cb *cb = new run_script_cb; |
930 | cb->set<connection, &connection::script_node_down> (this); |
922 | cb->set<connection, &connection::script_node_down> (this); |
… | |
… | |
954 | connection::shutdown () |
946 | connection::shutdown () |
955 | { |
947 | { |
956 | if (ictx && octx) |
948 | if (ictx && octx) |
957 | send_reset (si); |
949 | send_reset (si); |
958 | |
950 | |
959 | reset_connection (); |
951 | reset_connection ("shutdown"); |
960 | } |
952 | } |
961 | |
953 | |
962 | // poor-man's rekeying |
954 | // poor-man's rekeying |
963 | inline void |
955 | inline void |
964 | connection::rekey_cb (ev::timer &w, int revents) |
956 | connection::rekey_cb (ev::timer &w, int revents) |
965 | { |
957 | { |
966 | reset_connection (); |
958 | reset_connection ("rekeying"); |
967 | establish_connection (); |
959 | establish_connection (); |
968 | } |
960 | } |
969 | |
961 | |
970 | void |
962 | void |
971 | connection::send_data_packet (tap_packet *pkt) |
963 | connection::send_data_packet (tap_packet *pkt) |
… | |
… | |
988 | |
980 | |
989 | void |
981 | void |
990 | connection::post_inject_queue () |
982 | connection::post_inject_queue () |
991 | { |
983 | { |
992 | // 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) |
993 | 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 |
994 | establish_connection.stop (); |
986 | establish_connection.stop (); |
995 | |
987 | |
996 | establish_connection (); |
988 | establish_connection (); |
997 | } |
989 | } |
998 | |
990 | |
… | |
… | |
1058 | // about our desire for communication. |
1050 | // about our desire for communication. |
1059 | establish_connection (); |
1051 | establish_connection (); |
1060 | break; |
1052 | break; |
1061 | |
1053 | |
1062 | 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) |
1063 | { |
1058 | { |
1064 | reset_connection (); |
1059 | reset_connection ("remote reset"); |
1065 | |
1060 | |
1066 | config_packet *p = (config_packet *) pkt; |
1061 | config_packet *p = (config_packet *) pkt; |
1067 | |
1062 | |
1068 | if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) |
1063 | if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) |
1069 | establish_connection (); |
1064 | establish_connection (); |
1070 | } |
1065 | } |
|
|
1066 | |
1071 | break; |
1067 | break; |
1072 | |
1068 | |
1073 | case vpn_packet::PT_AUTH_REQ: |
1069 | case vpn_packet::PT_AUTH_REQ: |
1074 | if (auth_rate_limiter.can (rsi)) |
1070 | if (auth_rate_limiter.can (rsi)) |
1075 | { |
1071 | { |
… | |
… | |
1090 | 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."), |
1091 | conf->nodename, (const char *)rsi, |
1087 | conf->nodename, (const char *)rsi, |
1092 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1088 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1093 | |
1089 | |
1094 | if (p->initiate) |
1090 | if (p->initiate) |
|
|
1091 | { |
1095 | send_auth_request (rsi, false); |
1092 | send_auth_request (rsi, false); |
|
|
1093 | |
|
|
1094 | if (ictx && octx) |
|
|
1095 | reset_connection ("reconnect"); |
|
|
1096 | } |
1096 | |
1097 | |
1097 | auth_data auth; |
1098 | auth_data auth; |
1098 | |
1099 | |
1099 | if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) |
1100 | if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) |
1100 | { |
1101 | { |
1101 | 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?"), |
1102 | 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)); |
1103 | } |
1104 | } |
1104 | else |
1105 | else |
1105 | { |
1106 | { |
1106 | bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); |
1107 | bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth); |
1107 | |
1108 | |
1108 | rcv_auth = auth; |
1109 | rcv_auth = auth; |
1109 | have_rcv_auth = true; |
1110 | have_rcv_auth = true; |
1110 | |
1111 | |
1111 | send_auth_response (rsi); |
1112 | send_auth_response (rsi); |
… | |
… | |
1134 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1135 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1135 | |
1136 | |
1136 | auth_mac local_mac; |
1137 | auth_mac local_mac; |
1137 | auth_hash (snd_auth, p->response.ecdh, local_mac); |
1138 | auth_hash (snd_auth, p->response.ecdh, local_mac); |
1138 | |
1139 | |
1139 | if (memcmp (&p->response.mac, local_mac, sizeof local_mac)) |
1140 | if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac)) |
1140 | { |
1141 | { |
1141 | slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), |
1142 | slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), |
1142 | conf->nodename, (const char *)rsi); |
1143 | conf->nodename, (const char *)rsi); |
1143 | } |
1144 | } |
1144 | else if (!have_snd_auth) |
1145 | else if (!have_snd_auth) |
… | |
… | |
1162 | if (ictx && octx) |
1163 | if (ictx && octx) |
1163 | { |
1164 | { |
1164 | vpndata_packet *p = (vpndata_packet *)pkt; |
1165 | vpndata_packet *p = (vpndata_packet *)pkt; |
1165 | |
1166 | |
1166 | 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 | { |
1167 | 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" |
1168 | "could be an attack, or just corruption or a synchronization error."), |
1183 | "could be an attack, or just corruption or a synchronization error."), |
1169 | conf->nodename, (const char *)rsi); |
1184 | conf->nodename, (const char *)rsi); |
|
|
1185 | // reset |
|
|
1186 | } |
|
|
1187 | } |
1170 | else |
1188 | else |
1171 | { |
1189 | { |
1172 | u32 seqno; |
1190 | u32 seqno; |
1173 | tap_packet *d = p->unpack (this, seqno); |
1191 | tap_packet *d = p->unpack (this, seqno); |
1174 | int seqclass = iseqno.seqno_classify (seqno); |
1192 | int seqclass = iseqno.seqno_classify (seqno); |
|
|
1193 | |
|
|
1194 | hmac_error = 0; |
1175 | |
1195 | |
1176 | if (seqclass == 0) // ok |
1196 | if (seqclass == 0) // ok |
1177 | { |
1197 | { |
1178 | vpn->tap->send (d); |
1198 | vpn->tap->send (d); |
1179 | |
1199 | |
… | |
… | |
1306 | |
1326 | |
1307 | if (when >= 0) |
1327 | if (when >= 0) |
1308 | w.start (when); |
1328 | w.start (when); |
1309 | else if (when < -15) |
1329 | else if (when < -15) |
1310 | { |
1330 | { |
1311 | reset_connection (); |
1331 | reset_connection ("keepalive overdue"); |
1312 | establish_connection (); |
1332 | establish_connection (); |
1313 | } |
1333 | } |
1314 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1334 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1315 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1335 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1316 | { |
1336 | { |
… | |
… | |
1320 | else if (when >= -10) |
1340 | else if (when >= -10) |
1321 | // hold ondemand connections implicitly a few seconds longer |
1341 | // hold ondemand connections implicitly a few seconds longer |
1322 | // should delete octx, though, or something like that ;) |
1342 | // should delete octx, though, or something like that ;) |
1323 | w.start (when + 10); |
1343 | w.start (when + 10); |
1324 | else |
1344 | else |
1325 | reset_connection (); |
1345 | reset_connection ("keepalive timeout"); |
1326 | } |
1346 | } |
1327 | |
1347 | |
1328 | void |
1348 | void |
1329 | connection::send_connect_request (int id) |
1349 | connection::send_connect_request (int id) |
1330 | { |
1350 | { |
… | |
… | |
1430 | |
1450 | |
1431 | // queue a dummy packet to force an initial connection attempt |
1451 | // queue a dummy packet to force an initial connection attempt |
1432 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1452 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1433 | vpn_queue.put (new net_packet); |
1453 | vpn_queue.put (new net_packet); |
1434 | |
1454 | |
1435 | reset_connection (); |
1455 | reset_connection ("startup"); |
1436 | } |
1456 | } |
1437 | |
1457 | |
1438 | connection::~connection () |
1458 | connection::~connection () |
1439 | { |
1459 | { |
1440 | shutdown (); |
1460 | shutdown (); |