… | |
… | |
33 | |
33 | |
34 | #include <list> |
34 | #include <list> |
35 | #include <queue> |
35 | #include <queue> |
36 | #include <utility> |
36 | #include <utility> |
37 | |
37 | |
|
|
38 | #include <openssl/opensslv.h> |
38 | #include <openssl/rand.h> |
39 | #include <openssl/rand.h> |
39 | #include <openssl/evp.h> |
40 | #include <openssl/evp.h> |
40 | #include <openssl/rsa.h> |
41 | #include <openssl/rsa.h> |
41 | #include <openssl/err.h> |
42 | #include <openssl/err.h> |
|
|
43 | |
|
|
44 | // openssl 0.9.8 compatibility |
|
|
45 | #if OPENSSL_VERSION_NUMBER < 0x10100000 |
|
|
46 | #define require101(exp) exp |
|
|
47 | #else |
|
|
48 | #define require101(exp) equire (exp) |
|
|
49 | #endif |
42 | |
50 | |
43 | #include "conf.h" |
51 | #include "conf.h" |
44 | #include "slog.h" |
52 | #include "slog.h" |
45 | #include "device.h" |
53 | #include "device.h" |
46 | #include "vpn.h" |
54 | #include "vpn.h" |
… | |
… | |
119 | { |
127 | { |
120 | u8 mac_key[MAC_KEYSIZE]; |
128 | u8 mac_key[MAC_KEYSIZE]; |
121 | static const unsigned char mac_info[] = "gvpe mac key"; |
129 | static const unsigned char mac_info[] = "gvpe mac key"; |
122 | |
130 | |
123 | hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); |
131 | 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)); |
132 | kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key)); |
126 | kdf.extract (s, sizeof (s)); |
133 | kdf.extract (s, sizeof (s)); |
127 | kdf.extract_done (HKDF_PRF_HASH ()); |
134 | kdf.extract_done (HKDF_PRF_HASH ()); |
128 | kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); |
135 | kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); |
129 | |
136 | |
130 | HMAC_CTX_init (&hctx); |
137 | HMAC_CTX_init (&hctx); |
131 | require (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); |
138 | require101 (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); |
132 | } |
139 | } |
133 | |
140 | |
134 | { |
141 | { |
135 | u8 cipher_key[CIPHER_KEYSIZE]; |
142 | u8 cipher_key[CIPHER_KEYSIZE]; |
136 | static const unsigned char cipher_info[] = "gvpe cipher key"; |
143 | static const unsigned char cipher_info[] = "gvpe cipher key"; |
137 | |
144 | |
138 | hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); |
145 | 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)); |
146 | kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key)); |
141 | kdf.extract (s, sizeof (s)); |
147 | kdf.extract (s, sizeof (s)); |
142 | kdf.extract_done (HKDF_PRF_HASH ()); |
148 | kdf.extract_done (HKDF_PRF_HASH ()); |
143 | kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); |
149 | kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); |
144 | |
150 | |
… | |
… | |
179 | |
185 | |
180 | return 1; |
186 | return 1; |
181 | } |
187 | } |
182 | |
188 | |
183 | static void |
189 | static void |
184 | auth_hash (const auth_data &auth, auth_mac &mac) |
190 | auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) |
185 | { |
191 | { |
186 | HMAC_CTX ctx; |
192 | hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt |
187 | |
193 | kdf.extract (&auth.rsa, sizeof (auth.rsa)); |
188 | HMAC_CTX_init (&ctx); |
194 | kdf.extract_done (); |
189 | require (HMAC_Init_ex (&ctx, auth.rsa.auth_key, sizeof (auth.rsa.auth_key), AUTH_DIGEST (), 0)); |
195 | kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info |
190 | require (HMAC_Update (&ctx, (const unsigned char *)&auth, sizeof auth)); |
|
|
191 | require (HMAC_Final (&ctx, (unsigned char *)&mac, 0)); |
|
|
192 | HMAC_CTX_cleanup (&ctx); |
|
|
193 | } |
196 | } |
194 | |
197 | |
195 | void |
198 | void |
196 | connection::generate_auth_data () |
199 | connection::generate_auth_data () |
197 | { |
200 | { |
198 | if (auth_expire < ev_now ()) |
201 | if (auth_expire < ev_now ()) |
199 | { |
202 | { |
200 | // request data |
203 | // request data |
201 | RAND_bytes ((unsigned char *)&snd_auth.rsa, sizeof snd_auth.rsa); |
204 | rand_fill (snd_auth.rsa); |
202 | curve25519_generate (snd_ecdh_a, snd_auth.ecdh); |
205 | curve25519_generate (snd_ecdh_a, snd_auth.ecdh); |
203 | auth_hash (snd_auth, snd_auth_mac); |
|
|
204 | |
206 | |
205 | // eventual response data |
207 | // eventual response data |
206 | curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); |
208 | curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); |
207 | } |
209 | } |
208 | |
210 | |
… | |
… | |
364 | } |
366 | } |
365 | } |
367 | } |
366 | |
368 | |
367 | ///////////////////////////////////////////////////////////////////////////// |
369 | ///////////////////////////////////////////////////////////////////////////// |
368 | |
370 | |
369 | unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; |
|
|
370 | |
|
|
371 | void |
371 | void |
372 | hmac_packet::hmac_gen (crypto_ctx *ctx) |
372 | hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) |
373 | { |
373 | { |
374 | unsigned int xlen; |
|
|
375 | |
|
|
376 | HMAC_CTX *hctx = &ctx->hctx; |
374 | HMAC_CTX *hctx = &ctx->hctx; |
377 | |
375 | |
378 | require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); |
376 | require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0)); |
379 | require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
377 | require101 (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet))); |
380 | len - sizeof (hmac_packet))); |
|
|
381 | require (HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen)); |
378 | require101 (HMAC_Final (hctx, hmac_digest, 0)); |
382 | } |
379 | } |
383 | |
380 | |
384 | void |
381 | void |
385 | hmac_packet::hmac_set (crypto_ctx *ctx) |
382 | hmac_packet::hmac_set (crypto_ctx *ctx) |
386 | { |
383 | { |
387 | hmac_gen (ctx); |
384 | unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
388 | |
385 | hmac_gen (ctx, hmac_digest); |
389 | memcpy (hmac, hmac_digest, HMACLENGTH); |
386 | memcpy (hmac, hmac_digest, HMACLENGTH); |
390 | } |
387 | } |
391 | |
388 | |
392 | bool |
389 | bool |
393 | hmac_packet::hmac_chk (crypto_ctx *ctx) |
390 | hmac_packet::hmac_chk (crypto_ctx *ctx) |
394 | { |
391 | { |
395 | hmac_gen (ctx); |
392 | unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
396 | |
393 | hmac_gen (ctx, hmac_digest); |
397 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
394 | return slow_memeq (hmac, hmac_digest, HMACLENGTH); |
398 | } |
395 | } |
399 | |
396 | |
400 | void |
397 | void |
401 | vpn_packet::set_hdr (ptype type_, unsigned int dst) |
398 | vpn_packet::set_hdr (ptype type_, unsigned int dst) |
402 | { |
399 | { |
… | |
… | |
461 | u32 seqno; |
458 | u32 seqno; |
462 | } datahdr; |
459 | } datahdr; |
463 | |
460 | |
464 | datahdr.seqno = ntohl (seqno); |
461 | datahdr.seqno = ntohl (seqno); |
465 | #if RAND_SIZE |
462 | #if RAND_SIZE |
466 | RAND_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
463 | // NB: a constant (per session) random prefix |
|
|
464 | // is likely enough, but we don't take any chances. |
|
|
465 | conn->oiv.get (datahdr.rnd, RAND_SIZE); |
467 | #endif |
466 | #endif |
468 | |
467 | |
469 | require (EVP_EncryptUpdate (cctx, |
468 | require (EVP_EncryptUpdate (cctx, |
470 | (unsigned char *) data + outl, &outl2, |
469 | (unsigned char *) data + outl, &outl2, |
471 | (unsigned char *) &datahdr, DATAHDR)); |
470 | (unsigned char *) &datahdr, DATAHDR)); |
… | |
… | |
502 | |
501 | |
503 | if (type == PT_DATA_COMPRESSED) |
502 | if (type == PT_DATA_COMPRESSED) |
504 | d = cdata; |
503 | d = cdata; |
505 | else |
504 | else |
506 | #endif |
505 | #endif |
507 | d = &(*p)[6 + 6 - DATAHDR]; |
506 | d = &(*p)[6 + 6] - DATAHDR; |
508 | |
507 | |
509 | /* this overwrites part of the src mac, but we fix that later */ |
508 | // we play do evil games with the struct layout atm. |
|
|
509 | // pending better solutions, we at least do some verification. |
|
|
510 | // this is fine, as we left ISO territory long ago. |
|
|
511 | require (DATAHDR <= 16); |
|
|
512 | require ((u8 *)(&p->len + 1) == &(*p)[0]); |
|
|
513 | |
|
|
514 | // this can overwrite the len/dst/src fields |
510 | require (EVP_DecryptUpdate (cctx, |
515 | require (EVP_DecryptUpdate (cctx, |
511 | d, &outl2, |
516 | d, &outl2, |
512 | (unsigned char *)&data, len - data_hdr_size ())); |
517 | (unsigned char *)&data, len - data_hdr_size ())); |
513 | outl += outl2; |
518 | outl += outl2; |
514 | |
519 | |
… | |
… | |
545 | } |
550 | } |
546 | }; |
551 | }; |
547 | |
552 | |
548 | struct config_packet : vpn_packet |
553 | struct config_packet : vpn_packet |
549 | { |
554 | { |
|
|
555 | u8 serial[SERIAL_SIZE]; |
550 | u8 prot_major, prot_minor, randsize; |
556 | u8 prot_major, prot_minor, randsize; |
551 | u8 flags, features, pad6, pad7, pad8; |
557 | u8 flags, features, pad6, pad7, pad8; |
552 | u32 cipher_nid, mac_nid, auth_nid; |
558 | u32 cipher_nid, mac_nid, auth_nid; |
553 | |
559 | |
554 | void setup (ptype type, int dst); |
560 | void setup (ptype type, int dst); |
555 | bool chk_config () const; |
561 | bool chk_config (const conf_node *conf, const sockinfo &rsi) const; |
556 | |
562 | |
557 | static u8 get_features () |
563 | static u8 get_features () |
558 | { |
564 | { |
559 | u8 f = 0; |
565 | u8 f = 0; |
560 | #if ENABLE_COMPRESSION |
566 | #if ENABLE_COMPRESSION |
… | |
… | |
577 | prot_minor = PROTOCOL_MINOR; |
583 | prot_minor = PROTOCOL_MINOR; |
578 | randsize = RAND_SIZE; |
584 | randsize = RAND_SIZE; |
579 | flags = 0; |
585 | flags = 0; |
580 | features = get_features (); |
586 | features = get_features (); |
581 | |
587 | |
|
|
588 | strncpy ((char *)serial, conf.serial, sizeof (serial)); |
|
|
589 | |
582 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); |
590 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); |
583 | mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); |
591 | mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); |
584 | auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); |
592 | auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); |
585 | |
593 | |
586 | len = sizeof (*this) - sizeof (net_packet); |
594 | len = sizeof (*this) - sizeof (net_packet); |
587 | set_hdr (type, dst); |
595 | set_hdr (type, dst); |
588 | } |
596 | } |
589 | |
597 | |
590 | bool |
598 | bool |
591 | config_packet::chk_config () const |
599 | config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const |
592 | { |
600 | { |
593 | if (prot_major != PROTOCOL_MAJOR) |
601 | if (prot_major != PROTOCOL_MAJOR) |
594 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
602 | slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"), |
|
|
603 | conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); |
595 | else if (randsize != RAND_SIZE) |
604 | else if (randsize != RAND_SIZE) |
596 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
605 | slog (L_WARN, _("%s(%s): rand size mismatch (remote %d <=> local %d)"), |
|
|
606 | conf->nodename, (const char *)rsi, randsize, RAND_SIZE); |
597 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) |
607 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) |
598 | slog (L_WARN, _("cipher algo mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); |
608 | slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"), |
|
|
609 | conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); |
599 | else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) |
610 | else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) |
600 | slog (L_WARN, _("mac algo mismatch (remote %x <=> local %x)"), ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ())); |
611 | slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"), |
|
|
612 | conf->nodename, (const char *)rsi, ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ())); |
601 | else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) |
613 | else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) |
602 | slog (L_WARN, _("auth algo mismatch (remote %x <=> local %x)"), ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ())); |
614 | slog (L_WARN, _("%s(%s): auth algo mismatch (remote %x <=> local %x)"), |
|
|
615 | conf->nodename, (const char *)rsi, ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ())); |
603 | else |
616 | else |
|
|
617 | { |
|
|
618 | int cmp = memcmp (serial, ::conf.serial, sizeof (serial)); |
|
|
619 | |
|
|
620 | if (cmp > 0) |
|
|
621 | slog (L_WARN, _("%s(%s): remote serial newer than local serial - outdated config?"), |
|
|
622 | conf->nodename, (const char *)rsi); |
|
|
623 | else if (cmp == 0) |
604 | return true; |
624 | return true; |
|
|
625 | } |
605 | |
626 | |
606 | return false; |
627 | return false; |
607 | } |
628 | } |
608 | |
629 | |
609 | struct auth_req_packet : config_packet // UNPROTECTED |
630 | struct auth_req_packet : config_packet // UNPROTECTED |
… | |
… | |
623 | |
644 | |
624 | len = sizeof (*this) - sizeof (net_packet); |
645 | len = sizeof (*this) - sizeof (net_packet); |
625 | } |
646 | } |
626 | }; |
647 | }; |
627 | |
648 | |
628 | struct auth_res_packet : config_packet // UNPROTECTED |
649 | struct auth_res_packet : vpn_packet // UNPROTECTED |
629 | { |
650 | { |
630 | auth_response response; |
651 | auth_response response; |
631 | |
652 | |
632 | auth_res_packet (int dst) |
653 | auth_res_packet (int dst) |
633 | { |
654 | { |
634 | config_packet::setup (PT_AUTH_RES, dst); |
655 | set_hdr (PT_AUTH_RES, dst); |
635 | |
656 | |
636 | len = sizeof (*this) - sizeof (net_packet); |
657 | len = sizeof (*this) - sizeof (net_packet); |
637 | } |
658 | } |
638 | }; |
659 | }; |
639 | |
660 | |
… | |
… | |
679 | si = rsi; |
700 | si = rsi; |
680 | protocol = rsi.prot; |
701 | protocol = rsi.prot; |
681 | |
702 | |
682 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
703 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
683 | conf->nodename, (const char *)rsi, |
704 | conf->nodename, (const char *)rsi, |
684 | is_direct ? "direct" : "forwarded", |
705 | vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded", |
685 | PROTOCOL_MAJOR, prot_minor); |
706 | PROTOCOL_MAJOR, prot_minor); |
686 | |
707 | |
687 | if (::conf.script_node_up) |
708 | if (::conf.script_node_up) |
688 | { |
709 | { |
689 | run_script_cb *cb = new run_script_cb; |
710 | run_script_cb *cb = new run_script_cb; |
… | |
… | |
695 | iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); |
716 | iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); |
696 | |
717 | |
697 | delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); |
718 | delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); |
698 | oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; |
719 | oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; |
699 | |
720 | |
700 | if (ictx && octx) |
721 | oiv.reset (); |
701 | { |
722 | |
702 | // make sure rekeying timeouts are slightly asymmetric |
723 | // make sure rekeying timeouts are slightly asymmetric |
703 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
724 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
704 | rekey.start (rekey_interval, rekey_interval); |
725 | rekey.start (rekey_interval, rekey_interval); |
705 | |
726 | |
|
|
727 | hmac_error = 0.; |
|
|
728 | |
706 | keepalive.start (::conf.keepalive); |
729 | keepalive.start (::conf.keepalive); |
707 | |
730 | |
708 | // send queued packets |
731 | // send queued packets |
709 | if (ictx && octx) |
|
|
710 | { |
|
|
711 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
732 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
712 | { |
733 | { |
713 | if (p->len) send_data_packet (p); |
734 | if (p->len) send_data_packet (p); |
714 | delete p; |
735 | delete p; |
715 | } |
736 | } |
716 | |
737 | |
717 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
738 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
718 | { |
739 | { |
719 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
740 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
720 | delete p; |
741 | delete p; |
721 | } |
742 | } |
722 | } |
|
|
723 | |
743 | |
724 | vpn->connection_established (this); |
744 | vpn->connection_established (this); |
725 | } |
|
|
726 | #if 0 |
|
|
727 | else |
|
|
728 | { |
|
|
729 | retry_cnt = 0; |
|
|
730 | establish_connection.start (5); |
|
|
731 | keepalive.stop (); |
|
|
732 | rekey.stop (); |
|
|
733 | } |
|
|
734 | #endif |
|
|
735 | } |
745 | } |
736 | |
746 | |
737 | void |
747 | void |
738 | connection::reset_si () |
748 | connection::reset_si () |
739 | { |
749 | { |
… | |
… | |
744 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
754 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
745 | protocol = 0; |
755 | protocol = 0; |
746 | } |
756 | } |
747 | |
757 | |
748 | si.set (conf, protocol); |
758 | si.set (conf, protocol); |
749 | |
|
|
750 | is_direct = si.valid (); |
|
|
751 | } |
759 | } |
752 | |
760 | |
753 | // ensure sockinfo is valid, forward if necessary |
761 | // ensure sockinfo is valid, forward if necessary |
754 | const sockinfo & |
762 | const sockinfo & |
755 | connection::forward_si (const sockinfo &si) const |
763 | connection::forward_si (const sockinfo &si) const |
… | |
… | |
774 | |
782 | |
775 | void |
783 | void |
776 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
784 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
777 | { |
785 | { |
778 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
786 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
779 | reset_connection (); |
787 | reset_connection ("packet send error"); |
780 | } |
788 | } |
781 | |
789 | |
782 | void |
790 | void |
783 | connection::send_ping (const sockinfo &si, u8 pong) |
791 | connection::send_ping (const sockinfo &si, u8 pong) |
784 | { |
792 | { |
… | |
… | |
823 | void |
831 | void |
824 | connection::send_auth_response (const sockinfo &si) |
832 | connection::send_auth_response (const sockinfo &si) |
825 | { |
833 | { |
826 | auth_res_packet *pkt = new auth_res_packet (conf->id); |
834 | auth_res_packet *pkt = new auth_res_packet (conf->id); |
827 | |
835 | |
828 | auth_hash (rcv_auth, pkt->response.mac); |
|
|
829 | memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof (rcv_ecdh_b)); |
836 | memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b); |
|
|
837 | auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac); |
830 | |
838 | |
831 | slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); |
839 | slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); |
832 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
840 | send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly |
833 | |
841 | |
834 | delete pkt; |
842 | delete pkt; |
… | |
… | |
860 | { |
868 | { |
861 | // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection |
869 | // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection |
862 | // and stop trying. should probably be handled by a per-connection expire handler. |
870 | // and stop trying. should probably be handled by a per-connection expire handler. |
863 | if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) |
871 | if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) |
864 | { |
872 | { |
865 | reset_connection (); |
873 | reset_connection ("no demand"); |
866 | return; |
874 | return; |
867 | } |
875 | } |
868 | |
876 | |
869 | last_establish_attempt = ev_now (); |
877 | last_establish_attempt = ev_now (); |
870 | |
878 | |
… | |
… | |
872 | ? (retry_cnt & 3) + 1 |
880 | ? (retry_cnt & 3) + 1 |
873 | : 1 << (retry_cnt >> 2)); |
881 | : 1 << (retry_cnt >> 2)); |
874 | |
882 | |
875 | reset_si (); |
883 | reset_si (); |
876 | |
884 | |
877 | bool slow = si.prot & PROT_SLOW; |
885 | bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power); |
878 | |
886 | |
879 | if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) |
887 | if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) |
880 | { |
888 | { |
881 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
889 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
882 | w.start (1); |
890 | w.start (1); |
… | |
… | |
892 | |
900 | |
893 | slow = slow || (dsi.prot & PROT_SLOW); |
901 | slow = slow || (dsi.prot & PROT_SLOW); |
894 | |
902 | |
895 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
903 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
896 | { |
904 | { |
897 | if (retry_cnt < 4) |
905 | // use ping after the first few retries |
|
|
906 | // TODO: on rekeys, the other node might not interpret ping correctly, |
|
|
907 | // TODO: as it will still have a valid connection |
|
|
908 | if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power)) |
898 | send_auth_request (dsi, true); |
909 | send_auth_request (dsi, true); |
899 | else |
910 | else |
900 | send_ping (dsi, 0); |
911 | send_ping (dsi, 0); |
901 | } |
912 | } |
902 | } |
913 | } |
903 | |
914 | |
904 | retry_int *= slow ? 8. : 0.9; |
915 | retry_int *= slow ? 4. : 0.9; |
905 | |
916 | |
906 | if (retry_int < conf->max_retry) |
917 | if (retry_int < conf->max_retry) |
907 | retry_cnt++; |
918 | retry_cnt++; |
908 | else |
919 | else |
909 | retry_int = conf->max_retry; |
920 | retry_int = conf->max_retry; |
… | |
… | |
911 | w.start (retry_int); |
922 | w.start (retry_int); |
912 | } |
923 | } |
913 | } |
924 | } |
914 | |
925 | |
915 | void |
926 | void |
916 | connection::reset_connection () |
927 | connection::reset_connection (const char *reason) |
917 | { |
928 | { |
918 | if (ictx && octx) |
929 | if (ictx && octx) |
919 | { |
930 | { |
920 | slog (L_INFO, _("%s(%s): connection lost"), |
931 | slog (L_INFO, _("%s(%s): connection lost (%s)"), |
921 | conf->nodename, (const char *)si); |
932 | conf->nodename, (const char *)si, reason); |
922 | |
933 | |
923 | if (::conf.script_node_down) |
934 | if (::conf.script_node_down) |
924 | { |
935 | { |
925 | run_script_cb *cb = new run_script_cb; |
936 | run_script_cb *cb = new run_script_cb; |
926 | cb->set<connection, &connection::script_node_down> (this); |
937 | cb->set<connection, &connection::script_node_down> (this); |
… | |
… | |
950 | connection::shutdown () |
961 | connection::shutdown () |
951 | { |
962 | { |
952 | if (ictx && octx) |
963 | if (ictx && octx) |
953 | send_reset (si); |
964 | send_reset (si); |
954 | |
965 | |
955 | reset_connection (); |
966 | reset_connection ("shutdown"); |
956 | } |
967 | } |
957 | |
968 | |
958 | // poor-man's rekeying |
969 | // poor-man's rekeying |
959 | inline void |
970 | inline void |
960 | connection::rekey_cb (ev::timer &w, int revents) |
971 | connection::rekey_cb (ev::timer &w, int revents) |
961 | { |
972 | { |
962 | reset_connection (); |
973 | reset_connection ("rekeying"); |
963 | establish_connection (); |
974 | establish_connection (); |
964 | } |
975 | } |
965 | |
976 | |
966 | void |
977 | void |
967 | connection::send_data_packet (tap_packet *pkt) |
978 | connection::send_data_packet (tap_packet *pkt) |
… | |
… | |
984 | |
995 | |
985 | void |
996 | void |
986 | connection::post_inject_queue () |
997 | connection::post_inject_queue () |
987 | { |
998 | { |
988 | // force a connection every now and when when packets are sent (max 1/s) |
999 | // force a connection every now and when when packets are sent (max 1/s) |
989 | if (ev_now () - last_establish_attempt >= 0.95) // arbitrary |
1000 | if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary |
990 | establish_connection.stop (); |
1001 | establish_connection.stop (); |
991 | |
1002 | |
992 | establish_connection (); |
1003 | establish_connection (); |
993 | } |
1004 | } |
994 | |
1005 | |
… | |
… | |
1054 | // about our desire for communication. |
1065 | // about our desire for communication. |
1055 | establish_connection (); |
1066 | establish_connection (); |
1056 | break; |
1067 | break; |
1057 | |
1068 | |
1058 | case vpn_packet::PT_RESET: |
1069 | case vpn_packet::PT_RESET: |
|
|
1070 | slog (L_TRACE, "%s >> PT_RESET", conf->nodename); |
|
|
1071 | |
|
|
1072 | if (ictx && octx) |
1059 | { |
1073 | { |
1060 | reset_connection (); |
1074 | reset_connection ("remote reset"); |
1061 | |
1075 | |
1062 | config_packet *p = (config_packet *) pkt; |
1076 | config_packet *p = (config_packet *) pkt; |
1063 | |
1077 | |
1064 | if (!p->chk_config ()) |
1078 | if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) |
1065 | { |
|
|
1066 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."), |
|
|
1067 | conf->nodename, (const char *)rsi); |
|
|
1068 | connectmode = conf_node::C_DISABLED; |
|
|
1069 | } |
|
|
1070 | else if (connectmode == conf_node::C_ALWAYS) |
|
|
1071 | establish_connection (); |
1079 | establish_connection (); |
1072 | } |
1080 | } |
|
|
1081 | |
1073 | break; |
1082 | break; |
1074 | |
1083 | |
1075 | case vpn_packet::PT_AUTH_REQ: |
1084 | case vpn_packet::PT_AUTH_REQ: |
1076 | if (auth_rate_limiter.can (rsi)) |
1085 | if (auth_rate_limiter.can (rsi)) |
1077 | { |
1086 | { |
… | |
… | |
1079 | |
1088 | |
1080 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", |
1089 | slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)", |
1081 | conf->nodename, p->initiate ? "initiate" : "reply", |
1090 | conf->nodename, p->initiate ? "initiate" : "reply", |
1082 | p->protocols, p->features); |
1091 | p->protocols, p->features); |
1083 | |
1092 | |
1084 | if (p->chk_config () && !memcmp (p->magic, MAGIC, 8)) |
1093 | if (memcmp (p->magic, MAGIC, 8)) |
|
|
1094 | { |
|
|
1095 | slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"), |
|
|
1096 | conf->nodename, (const char *)rsi); |
|
|
1097 | } |
|
|
1098 | else if (p->chk_config (conf, rsi)) |
1085 | { |
1099 | { |
1086 | if (p->prot_minor != PROTOCOL_MINOR) |
1100 | if (p->prot_minor != PROTOCOL_MINOR) |
1087 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1101 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1088 | conf->nodename, (const char *)rsi, |
1102 | conf->nodename, (const char *)rsi, |
1089 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1103 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1090 | |
1104 | |
1091 | if (p->initiate) |
1105 | if (p->initiate) |
|
|
1106 | { |
1092 | send_auth_request (rsi, false); |
1107 | send_auth_request (rsi, false); |
|
|
1108 | |
|
|
1109 | if (ictx && octx) |
|
|
1110 | reset_connection ("reconnect"); |
|
|
1111 | } |
1093 | |
1112 | |
1094 | auth_data auth; |
1113 | auth_data auth; |
1095 | |
1114 | |
1096 | if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) |
1115 | if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) |
1097 | { |
1116 | { |
1098 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
1117 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
1099 | conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
1118 | conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
1100 | } |
1119 | } |
1101 | else |
1120 | else |
1102 | { |
1121 | { |
1103 | bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); |
1122 | bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth); |
1104 | |
1123 | |
1105 | rcv_auth = auth; |
1124 | rcv_auth = auth; |
1106 | have_rcv_auth = true; |
1125 | have_rcv_auth = true; |
1107 | |
1126 | |
1108 | send_auth_response (rsi); |
1127 | send_auth_response (rsi); |
… | |
… | |
1116 | } |
1135 | } |
1117 | } |
1136 | } |
1118 | |
1137 | |
1119 | break; |
1138 | break; |
1120 | } |
1139 | } |
1121 | else |
|
|
1122 | slog (L_WARN, _("%s(%s): protocol mismatch."), |
|
|
1123 | conf->nodename, (const char *)rsi); |
|
|
1124 | |
1140 | |
1125 | send_reset (rsi); |
1141 | send_reset (rsi); |
1126 | } |
1142 | } |
1127 | |
1143 | |
1128 | break; |
1144 | break; |
… | |
… | |
1131 | { |
1147 | { |
1132 | auth_res_packet *p = (auth_res_packet *)pkt; |
1148 | auth_res_packet *p = (auth_res_packet *)pkt; |
1133 | |
1149 | |
1134 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1150 | slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); |
1135 | |
1151 | |
1136 | if (p->chk_config ()) |
1152 | auth_mac local_mac; |
|
|
1153 | auth_hash (snd_auth, p->response.ecdh, local_mac); |
|
|
1154 | |
|
|
1155 | if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac)) |
1137 | { |
1156 | { |
1138 | if (memcmp (&p->response.mac, snd_auth_mac, sizeof (snd_auth_mac))) |
|
|
1139 | { |
|
|
1140 | slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), |
1157 | slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), |
1141 | conf->nodename, (const char *)rsi); |
1158 | conf->nodename, (const char *)rsi); |
1142 | } |
1159 | } |
1143 | else if (!have_snd_auth) |
1160 | else if (!have_snd_auth) |
1144 | { |
1161 | { |
1145 | if (p->prot_minor != PROTOCOL_MINOR) |
|
|
1146 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
|
|
1147 | conf->nodename, (const char *)rsi, |
|
|
1148 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
|
|
1149 | |
|
|
1150 | prot_minor = p->prot_minor; |
|
|
1151 | memcpy (snd_ecdh_b, p->response.ecdh, sizeof (snd_ecdh_b)); |
1162 | memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b); |
1152 | |
1163 | |
1153 | have_snd_auth = true; |
1164 | have_snd_auth = true; |
1154 | connection_established (rsi); |
1165 | connection_established (rsi); |
1155 | } |
|
|
1156 | |
|
|
1157 | break; |
|
|
1158 | } |
1166 | } |
1159 | } |
1167 | } |
1160 | |
|
|
1161 | send_reset (rsi); |
|
|
1162 | break; |
1168 | break; |
1163 | |
1169 | |
1164 | case vpn_packet::PT_DATA_COMPRESSED: |
1170 | case vpn_packet::PT_DATA_COMPRESSED: |
1165 | #if !ENABLE_COMPRESSION |
1171 | #if !ENABLE_COMPRESSION |
1166 | send_reset (rsi); |
1172 | send_reset (rsi); |
… | |
… | |
1172 | if (ictx && octx) |
1178 | if (ictx && octx) |
1173 | { |
1179 | { |
1174 | vpndata_packet *p = (vpndata_packet *)pkt; |
1180 | vpndata_packet *p = (vpndata_packet *)pkt; |
1175 | |
1181 | |
1176 | if (!p->hmac_chk (ictx)) |
1182 | if (!p->hmac_chk (ictx)) |
|
|
1183 | { |
|
|
1184 | // rekeying often creates temporary hmac auth floods |
|
|
1185 | // we assume they don't take longer than a few seconds normally, |
|
|
1186 | // and suppress messages and resets during that time. |
|
|
1187 | //TODO: should be done per source address |
|
|
1188 | if (!hmac_error) |
|
|
1189 | { |
|
|
1190 | hmac_error = ev_now () + 3; |
|
|
1191 | break; |
|
|
1192 | } |
|
|
1193 | else if (hmac_error >= ev_now ()) |
|
|
1194 | break; // silently suppress |
|
|
1195 | else |
|
|
1196 | { |
1177 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1197 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1178 | "could be an attack, or just corruption or a synchronization error."), |
1198 | "could be an attack, or just corruption or a synchronization error."), |
1179 | conf->nodename, (const char *)rsi); |
1199 | conf->nodename, (const char *)rsi); |
|
|
1200 | // reset |
|
|
1201 | } |
|
|
1202 | } |
1180 | else |
1203 | else |
1181 | { |
1204 | { |
1182 | u32 seqno; |
1205 | u32 seqno; |
1183 | tap_packet *d = p->unpack (this, seqno); |
1206 | tap_packet *d = p->unpack (this, seqno); |
1184 | int seqclass = iseqno.seqno_classify (seqno); |
1207 | int seqclass = iseqno.seqno_classify (seqno); |
|
|
1208 | |
|
|
1209 | hmac_error = 0; |
1185 | |
1210 | |
1186 | if (seqclass == 0) // ok |
1211 | if (seqclass == 0) // ok |
1187 | { |
1212 | { |
1188 | vpn->tap->send (d); |
1213 | vpn->tap->send (d); |
1189 | |
1214 | |
… | |
… | |
1316 | |
1341 | |
1317 | if (when >= 0) |
1342 | if (when >= 0) |
1318 | w.start (when); |
1343 | w.start (when); |
1319 | else if (when < -15) |
1344 | else if (when < -15) |
1320 | { |
1345 | { |
1321 | reset_connection (); |
1346 | reset_connection ("keepalive overdue"); |
1322 | establish_connection (); |
1347 | establish_connection (); |
1323 | } |
1348 | } |
1324 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1349 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1325 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1350 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1326 | { |
1351 | { |
… | |
… | |
1330 | else if (when >= -10) |
1355 | else if (when >= -10) |
1331 | // hold ondemand connections implicitly a few seconds longer |
1356 | // hold ondemand connections implicitly a few seconds longer |
1332 | // should delete octx, though, or something like that ;) |
1357 | // should delete octx, though, or something like that ;) |
1333 | w.start (when + 10); |
1358 | w.start (when + 10); |
1334 | else |
1359 | else |
1335 | reset_connection (); |
1360 | reset_connection ("keepalive timeout"); |
1336 | } |
1361 | } |
1337 | |
1362 | |
1338 | void |
1363 | void |
1339 | connection::send_connect_request (int id) |
1364 | connection::send_connect_request (int id) |
1340 | { |
1365 | { |
… | |
… | |
1440 | |
1465 | |
1441 | // queue a dummy packet to force an initial connection attempt |
1466 | // queue a dummy packet to force an initial connection attempt |
1442 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1467 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1443 | vpn_queue.put (new net_packet); |
1468 | vpn_queue.put (new net_packet); |
1444 | |
1469 | |
1445 | reset_connection (); |
1470 | reset_connection ("startup"); |
1446 | } |
1471 | } |
1447 | |
1472 | |
1448 | connection::~connection () |
1473 | connection::~connection () |
1449 | { |
1474 | { |
1450 | shutdown (); |
1475 | shutdown (); |