… | |
… | |
28 | #include <openssl/rand.h> |
28 | #include <openssl/rand.h> |
29 | #include <openssl/evp.h> |
29 | #include <openssl/evp.h> |
30 | #include <openssl/rsa.h> |
30 | #include <openssl/rsa.h> |
31 | #include <openssl/err.h> |
31 | #include <openssl/err.h> |
32 | |
32 | |
33 | #include "gettext.h" |
|
|
34 | |
|
|
35 | #include "conf.h" |
33 | #include "conf.h" |
36 | #include "slog.h" |
34 | #include "slog.h" |
37 | #include "device.h" |
35 | #include "device.h" |
38 | #include "vpn.h" |
36 | #include "vpn.h" |
39 | #include "connection.h" |
37 | #include "connection.h" |
… | |
… | |
478 | { |
476 | { |
479 | prot_major = PROTOCOL_MAJOR; |
477 | prot_major = PROTOCOL_MAJOR; |
480 | prot_minor = PROTOCOL_MINOR; |
478 | prot_minor = PROTOCOL_MINOR; |
481 | randsize = RAND_SIZE; |
479 | randsize = RAND_SIZE; |
482 | hmaclen = HMACLENGTH; |
480 | hmaclen = HMACLENGTH; |
483 | flags = ENABLE_COMPRESSION ? 0x81 : 0x80; |
481 | flags = 0; |
484 | challengelen = sizeof (rsachallenge); |
482 | challengelen = sizeof (rsachallenge); |
485 | features = get_features (); |
483 | features = get_features (); |
486 | |
484 | |
487 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
485 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
488 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
486 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
… | |
… | |
498 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
496 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
499 | else if (randsize != RAND_SIZE) |
497 | else if (randsize != RAND_SIZE) |
500 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
498 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
501 | else if (hmaclen != HMACLENGTH) |
499 | else if (hmaclen != HMACLENGTH) |
502 | slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
500 | slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
503 | #if 0 // this implementation should handle all flag settings |
|
|
504 | else if (flags != curflags ()) |
|
|
505 | slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ()); |
|
|
506 | #endif |
|
|
507 | else if (challengelen != sizeof (rsachallenge)) |
501 | else if (challengelen != sizeof (rsachallenge)) |
508 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
502 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
509 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
503 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
510 | slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
504 | slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
511 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
505 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
… | |
… | |
644 | { |
638 | { |
645 | connection *r = vpn->find_router (); |
639 | connection *r = vpn->find_router (); |
646 | |
640 | |
647 | if (r) |
641 | if (r) |
648 | { |
642 | { |
649 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), |
643 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), |
650 | conf->nodename, r->conf->nodename); |
644 | conf->nodename, r->conf->nodename, (const char *)r->si); |
651 | return r->si; |
645 | return r->si; |
652 | } |
646 | } |
653 | else |
647 | else |
654 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
648 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
655 | conf->nodename); |
649 | conf->nodename); |
… | |
… | |
659 | } |
653 | } |
660 | |
654 | |
661 | void |
655 | void |
662 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
656 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
663 | { |
657 | { |
664 | bool ok; |
658 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
665 | |
|
|
666 | switch (si.prot) |
|
|
667 | { |
|
|
668 | case PROT_IPv4: |
|
|
669 | ok = vpn->send_ipv4_packet (pkt, si, tos); break; |
|
|
670 | case PROT_UDPv4: |
|
|
671 | ok = vpn->send_udpv4_packet (pkt, si, tos); break; |
|
|
672 | #if ENABLE_TCP |
|
|
673 | case PROT_TCPv4: |
|
|
674 | ok = vpn->send_tcpv4_packet (pkt, si, tos); break; |
|
|
675 | #endif |
|
|
676 | #if ENABLE_ICMP |
|
|
677 | case PROT_ICMPv4: |
|
|
678 | ok = vpn->send_icmpv4_packet (pkt, si, tos); break; |
|
|
679 | #endif |
|
|
680 | #if ENABLE_DNS |
|
|
681 | case PROT_DNSv4: |
|
|
682 | ok = send_dnsv4_packet (pkt, si, tos); break; |
|
|
683 | #endif |
|
|
684 | |
|
|
685 | default: |
|
|
686 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
|
|
687 | ok = false; |
|
|
688 | } |
|
|
689 | |
|
|
690 | if (!ok) |
|
|
691 | reset_connection (); |
659 | reset_connection (); |
692 | } |
660 | } |
693 | |
661 | |
694 | void |
662 | void |
695 | connection::send_ping (const sockinfo &si, u8 pong) |
663 | connection::send_ping (const sockinfo &si, u8 pong) |
… | |
… | |
771 | && conf != THISNODE |
739 | && conf != THISNODE |
772 | && connectmode != conf_node::C_NEVER |
740 | && connectmode != conf_node::C_NEVER |
773 | && connectmode != conf_node::C_DISABLED |
741 | && connectmode != conf_node::C_DISABLED |
774 | && NOW > w.at) |
742 | && NOW > w.at) |
775 | { |
743 | { |
776 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
744 | w.at = TSTAMP_MAX; // first disable this watcher in case of recursion |
777 | |
745 | |
778 | if (retry_int < conf->max_retry) |
746 | double retry_int = double (retry_cnt & 3 |
779 | retry_cnt++; |
747 | ? (retry_cnt & 3) + 1 |
780 | else |
748 | : 1 << (retry_cnt >> 2)); |
781 | retry_int = conf->max_retry; |
|
|
782 | |
|
|
783 | w.start (NOW + retry_int); |
|
|
784 | |
749 | |
785 | reset_si (); |
750 | reset_si (); |
|
|
751 | |
|
|
752 | bool slow = si.prot & PROT_SLOW; |
786 | |
753 | |
787 | if (si.prot && !si.host) |
754 | if (si.prot && !si.host) |
788 | vpn->send_connect_request (conf->id); |
755 | vpn->send_connect_request (conf->id); |
789 | else |
756 | else |
790 | { |
757 | { |
791 | const sockinfo &dsi = forward_si (si); |
758 | const sockinfo &dsi = forward_si (si); |
|
|
759 | |
|
|
760 | slow = slow || (dsi.prot & PROT_SLOW); |
792 | |
761 | |
793 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
762 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
794 | { |
763 | { |
795 | if (retry_cnt < 4) |
764 | if (retry_cnt < 4) |
796 | send_auth_request (dsi, true); |
765 | send_auth_request (dsi, true); |
797 | else |
766 | else |
798 | send_ping (dsi, 0); |
767 | send_ping (dsi, 0); |
799 | } |
768 | } |
800 | } |
769 | } |
|
|
770 | |
|
|
771 | retry_int *= slow ? 8. : 0.7; |
|
|
772 | |
|
|
773 | if (retry_int < conf->max_retry) |
|
|
774 | retry_cnt++; |
|
|
775 | else |
|
|
776 | retry_int = conf->max_retry; |
|
|
777 | |
|
|
778 | w.start (NOW + retry_int); |
801 | } |
779 | } |
802 | } |
780 | } |
803 | |
781 | |
804 | void |
782 | void |
805 | connection::reset_connection () |
783 | connection::reset_connection () |
… | |
… | |
814 | } |
792 | } |
815 | |
793 | |
816 | delete ictx; ictx = 0; |
794 | delete ictx; ictx = 0; |
817 | delete octx; octx = 0; |
795 | delete octx; octx = 0; |
818 | #if ENABLE_DNS |
796 | #if ENABLE_DNS |
819 | delete dns; dns = 0; |
797 | dnsv4_reset_connection (); |
820 | #endif |
798 | #endif |
821 | |
799 | |
822 | si.host = 0; |
800 | si.host = 0; |
823 | |
801 | |
824 | last_activity = 0; |
802 | last_activity = 0; |
… | |
… | |
964 | delete octx; |
942 | delete octx; |
965 | |
943 | |
966 | octx = new crypto_ctx (k, 1); |
944 | octx = new crypto_ctx (k, 1); |
967 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
945 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
968 | |
946 | |
969 | // compatibility code, remove when no longer required |
|
|
970 | if (p->flags & 1) p->features |= FEATURE_COMPRESSION; |
|
|
971 | |
|
|
972 | conf->protocols = p->protocols; |
947 | conf->protocols = p->protocols; |
973 | features = p->features & config_packet::get_features (); |
948 | features = p->features & config_packet::get_features (); |
974 | |
949 | |
975 | send_auth_response (rsi, p->id, k); |
950 | send_auth_response (rsi, p->id, k); |
976 | |
951 | |