… | |
… | |
30 | */ |
30 | */ |
31 | |
31 | |
32 | #include "config.h" |
32 | #include "config.h" |
33 | |
33 | |
34 | #include <list> |
34 | #include <list> |
|
|
35 | #include <queue> |
|
|
36 | #include <utility> |
35 | |
37 | |
36 | #include <openssl/rand.h> |
38 | #include <openssl/rand.h> |
37 | #include <openssl/evp.h> |
39 | #include <openssl/evp.h> |
38 | #include <openssl/rsa.h> |
40 | #include <openssl/rsa.h> |
39 | #include <openssl/err.h> |
41 | #include <openssl/err.h> |
… | |
… | |
55 | #define ULTRA_FAST 1 |
57 | #define ULTRA_FAST 1 |
56 | #define HLOG 15 |
58 | #define HLOG 15 |
57 | #include "lzf/lzf.h" |
59 | #include "lzf/lzf.h" |
58 | #include "lzf/lzf_c.c" |
60 | #include "lzf/lzf_c.c" |
59 | #include "lzf/lzf_d.c" |
61 | #include "lzf/lzf_d.c" |
|
|
62 | |
|
|
63 | ////////////////////////////////////////////////////////////////////////////// |
|
|
64 | |
|
|
65 | static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; |
|
|
66 | static ev::child rs_child_ev; |
|
|
67 | |
|
|
68 | void // c++ requires external linkage here, apparently :( |
|
|
69 | rs_child_cb (ev::child &w, int revents) |
|
|
70 | { |
|
|
71 | w.stop (); |
|
|
72 | |
|
|
73 | if (rs_queue.empty ()) |
|
|
74 | return; |
|
|
75 | |
|
|
76 | pid_t pid = run_script (*rs_queue.front ().first, false); |
|
|
77 | if (pid) |
|
|
78 | { |
|
|
79 | w.set (pid); |
|
|
80 | w.start (); |
|
|
81 | } |
|
|
82 | else |
|
|
83 | slog (L_WARN, rs_queue.front ().second); |
|
|
84 | |
|
|
85 | delete rs_queue.front ().first; |
|
|
86 | rs_queue.pop (); |
|
|
87 | } |
|
|
88 | |
|
|
89 | // despite the fancy name, this is quite a hack |
|
|
90 | static void |
|
|
91 | run_script_queued (run_script_cb *cb, const char *warnmsg) |
|
|
92 | { |
|
|
93 | rs_queue.push (std::make_pair (cb, warnmsg)); |
|
|
94 | |
|
|
95 | if (!rs_child_ev.is_active ()) |
|
|
96 | { |
|
|
97 | rs_child_ev.set<rs_child_cb> (); |
|
|
98 | rs_child_ev (); |
|
|
99 | } |
|
|
100 | } |
|
|
101 | |
|
|
102 | ////////////////////////////////////////////////////////////////////////////// |
60 | |
103 | |
61 | struct crypto_ctx |
104 | struct crypto_ctx |
62 | { |
105 | { |
63 | EVP_CIPHER_CTX cctx; |
106 | EVP_CIPHER_CTX cctx; |
64 | HMAC_CTX hctx; |
107 | HMAC_CTX hctx; |
… | |
… | |
653 | { |
696 | { |
654 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
697 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
655 | delete p; |
698 | delete p; |
656 | } |
699 | } |
657 | } |
700 | } |
|
|
701 | |
|
|
702 | vpn->connection_established (this); |
658 | } |
703 | } |
659 | else |
704 | else |
660 | { |
705 | { |
661 | retry_cnt = 0; |
706 | retry_cnt = 0; |
662 | establish_connection.start (5); |
707 | establish_connection.start (5); |
… | |
… | |
666 | } |
711 | } |
667 | |
712 | |
668 | void |
713 | void |
669 | connection::reset_si () |
714 | connection::reset_si () |
670 | { |
715 | { |
|
|
716 | if (vpn->can_direct (THISNODE, conf)) |
671 | protocol = best_protocol (THISNODE->protocols & conf->protocols); |
717 | protocol = best_protocol (THISNODE->protocols & conf->connectable_protocols ()); |
672 | |
718 | else |
673 | // mask out protocols we cannot establish |
|
|
674 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
|
|
675 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
|
|
676 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
|
|
677 | |
|
|
678 | if (protocol |
|
|
679 | && (!conf->can_direct (THISNODE) |
|
|
680 | || !THISNODE->can_direct (conf))) |
|
|
681 | { |
719 | { |
682 | slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); |
720 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
683 | protocol = 0; |
721 | protocol = 0; |
684 | } |
722 | } |
685 | |
723 | |
686 | si.set (conf, protocol); |
724 | si.set (conf, protocol); |
|
|
725 | |
|
|
726 | is_direct = si.valid (); |
687 | } |
727 | } |
688 | |
728 | |
689 | // ensure sockinfo is valid, forward if necessary |
729 | // ensure sockinfo is valid, forward if necessary |
690 | const sockinfo & |
730 | const sockinfo & |
691 | connection::forward_si (const sockinfo &si) const |
731 | connection::forward_si (const sockinfo &si) const |
692 | { |
732 | { |
693 | if (!si.valid ()) |
733 | if (!si.valid ()) |
694 | { |
734 | { |
695 | connection *r = vpn->find_router (); |
735 | connection *r = vpn->find_router_for (this); |
696 | |
736 | |
697 | if (r) |
737 | if (r) |
698 | { |
738 | { |
699 | slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"), |
739 | slog (L_DEBUG, _("%s: no common protocol, trying to route through %s."), |
700 | conf->nodename, r->conf->nodename, (const char *)r->si); |
740 | conf->nodename, r->conf->nodename); |
701 | return r->si; |
741 | return r->si; |
702 | } |
742 | } |
703 | else |
743 | else |
704 | slog (L_DEBUG, _("%s: node unreachable, no common protocol"), |
744 | slog (L_DEBUG, _("%s: node unreachable, no common protocol or no router available."), |
705 | conf->nodename); |
745 | conf->nodename); |
706 | } |
746 | } |
707 | |
747 | |
708 | return si; |
748 | return si; |
709 | } |
749 | } |
… | |
… | |
719 | connection::send_ping (const sockinfo &si, u8 pong) |
759 | connection::send_ping (const sockinfo &si, u8 pong) |
720 | { |
760 | { |
721 | ping_packet *pkt = new ping_packet; |
761 | ping_packet *pkt = new ping_packet; |
722 | |
762 | |
723 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
763 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
|
|
764 | |
|
|
765 | slog (L_TRACE, ">>%d %s [%s]", conf->id, pong ? "PT_PONG" : "PT_PING", (const char *)si); |
|
|
766 | |
724 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
767 | send_vpn_packet (pkt, si, IPTOS_LOWDELAY); |
725 | |
768 | |
726 | delete pkt; |
769 | delete pkt; |
727 | } |
770 | } |
728 | |
771 | |
… | |
… | |
813 | |
856 | |
814 | reset_si (); |
857 | reset_si (); |
815 | |
858 | |
816 | bool slow = si.prot & PROT_SLOW; |
859 | bool slow = si.prot & PROT_SLOW; |
817 | |
860 | |
818 | if (si.prot && !si.host) |
861 | if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) |
819 | { |
862 | { |
820 | slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); |
|
|
821 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
863 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
822 | w.start (1); |
864 | w.start (1); |
823 | vpn->send_connect_request (conf->id); |
865 | vpn->send_connect_request (conf->id); |
824 | } |
866 | } |
825 | else |
867 | else |
826 | { |
868 | { |
827 | slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); |
869 | if (si.valid ()) |
|
|
870 | slog (L_DEBUG, _("%s: sending direct connection request to %s."), |
|
|
871 | conf->nodename, (const char *)si); |
828 | |
872 | |
829 | const sockinfo &dsi = forward_si (si); |
873 | const sockinfo &dsi = forward_si (si); |
830 | |
874 | |
831 | slow = slow || (dsi.prot & PROT_SLOW); |
875 | slow = slow || (dsi.prot & PROT_SLOW); |
832 | |
876 | |
… | |
… | |
858 | slog (L_INFO, _("%s(%s): connection lost"), |
902 | slog (L_INFO, _("%s(%s): connection lost"), |
859 | conf->nodename, (const char *)si); |
903 | conf->nodename, (const char *)si); |
860 | |
904 | |
861 | if (::conf.script_node_down) |
905 | if (::conf.script_node_down) |
862 | { |
906 | { |
863 | run_script_cb cb; |
907 | run_script_cb *cb = new run_script_cb; |
864 | cb.set<connection, &connection::script_node_down> (this); |
908 | cb->set<connection, &connection::script_node_down> (this); |
865 | if (!run_script (cb, false)) |
|
|
866 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
909 | run_script_queued (cb, _("node-down command execution failed, continuing.")); |
867 | } |
910 | } |
868 | } |
911 | } |
869 | |
912 | |
870 | delete ictx; ictx = 0; |
913 | delete ictx; ictx = 0; |
871 | delete octx; octx = 0; |
914 | delete octx; octx = 0; |
… | |
… | |
873 | dnsv4_reset_connection (); |
916 | dnsv4_reset_connection (); |
874 | #endif |
917 | #endif |
875 | |
918 | |
876 | si.host = 0; |
919 | si.host = 0; |
877 | |
920 | |
878 | last_activity = 0; |
921 | last_activity = 0.; |
|
|
922 | //last_si_change = 0.; |
879 | retry_cnt = 0; |
923 | retry_cnt = 0; |
880 | |
924 | |
881 | rekey.stop (); |
925 | rekey.stop (); |
882 | keepalive.stop (); |
926 | keepalive.stop (); |
883 | establish_connection.stop (); |
927 | establish_connection.stop (); |
… | |
… | |
890 | send_reset (si); |
934 | send_reset (si); |
891 | |
935 | |
892 | reset_connection (); |
936 | reset_connection (); |
893 | } |
937 | } |
894 | |
938 | |
|
|
939 | // poor-man's rekeying |
895 | inline void |
940 | inline void |
896 | connection::rekey_cb (ev::timer &w, int revents) |
941 | connection::rekey_cb (ev::timer &w, int revents) |
897 | { |
942 | { |
898 | reset_connection (); |
943 | reset_connection (); |
899 | establish_connection (); |
944 | establish_connection (); |
… | |
… | |
954 | void |
999 | void |
955 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1000 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
956 | { |
1001 | { |
957 | last_activity = ev_now (); |
1002 | last_activity = ev_now (); |
958 | |
1003 | |
959 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
1004 | slog (L_NOISE, "<<%d received packet type %d from %d to %d.", |
960 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
1005 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
961 | |
1006 | |
962 | if (connectmode == conf_node::C_DISABLED) |
1007 | if (connectmode == conf_node::C_DISABLED) |
963 | return; |
1008 | return; |
964 | |
1009 | |
… | |
… | |
972 | { |
1017 | { |
973 | if (auth_rate_limiter.can (rsi)) |
1018 | if (auth_rate_limiter.can (rsi)) |
974 | send_auth_request (rsi, true); |
1019 | send_auth_request (rsi, true); |
975 | } |
1020 | } |
976 | else |
1021 | else |
|
|
1022 | // we would love to change thre socket address here, but ping's aren't |
|
|
1023 | // authenticated, so we best ignore it. |
977 | send_ping (rsi, 1); // pong |
1024 | send_ping (rsi, 1); // pong |
978 | |
1025 | |
979 | break; |
1026 | break; |
980 | |
1027 | |
981 | case vpn_packet::PT_PONG: |
1028 | case vpn_packet::PT_PONG: |
… | |
… | |
987 | |
1034 | |
988 | config_packet *p = (config_packet *) pkt; |
1035 | config_packet *p = (config_packet *) pkt; |
989 | |
1036 | |
990 | if (!p->chk_config ()) |
1037 | if (!p->chk_config ()) |
991 | { |
1038 | { |
992 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), |
1039 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."), |
993 | conf->nodename, (const char *)rsi); |
1040 | conf->nodename, (const char *)rsi); |
994 | connectmode = conf_node::C_DISABLED; |
1041 | connectmode = conf_node::C_DISABLED; |
995 | } |
1042 | } |
996 | else if (connectmode == conf_node::C_ALWAYS) |
1043 | else if (connectmode == conf_node::C_ALWAYS) |
997 | establish_connection (); |
1044 | establish_connection (); |
… | |
… | |
1039 | |
1086 | |
1040 | break; |
1087 | break; |
1041 | } |
1088 | } |
1042 | } |
1089 | } |
1043 | else |
1090 | else |
1044 | slog (L_WARN, _("%s(%s): protocol mismatch"), |
1091 | slog (L_WARN, _("%s(%s): protocol mismatch."), |
1045 | conf->nodename, (const char *)rsi); |
1092 | conf->nodename, (const char *)rsi); |
1046 | |
1093 | |
1047 | send_reset (rsi); |
1094 | send_reset (rsi); |
1048 | } |
1095 | } |
1049 | |
1096 | |
… | |
… | |
1064 | |
1111 | |
1065 | rsachallenge chg; |
1112 | rsachallenge chg; |
1066 | |
1113 | |
1067 | if (!rsa_cache.find (p->id, chg)) |
1114 | if (!rsa_cache.find (p->id, chg)) |
1068 | { |
1115 | { |
1069 | slog (L_ERR, _("%s(%s): unrequested auth response ignored"), |
1116 | slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."), |
1070 | conf->nodename, (const char *)rsi); |
1117 | conf->nodename, (const char *)rsi); |
1071 | break; |
1118 | break; |
1072 | } |
1119 | } |
1073 | else |
1120 | else |
1074 | { |
1121 | { |
1075 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
1122 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
1076 | |
1123 | |
1077 | if (!p->hmac_chk (cctx)) |
1124 | if (!p->hmac_chk (cctx)) |
1078 | { |
1125 | { |
1079 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
1126 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
1080 | "could be an attack, or just corruption or a synchronization error"), |
1127 | "could be an attack, or just corruption or a synchronization error."), |
1081 | conf->nodename, (const char *)rsi); |
1128 | conf->nodename, (const char *)rsi); |
1082 | break; |
1129 | break; |
1083 | } |
1130 | } |
1084 | else |
1131 | else |
1085 | { |
1132 | { |
… | |
… | |
1096 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
1143 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
1097 | |
1144 | |
1098 | si = rsi; |
1145 | si = rsi; |
1099 | protocol = rsi.prot; |
1146 | protocol = rsi.prot; |
1100 | |
1147 | |
1101 | connection_established (); |
|
|
1102 | |
|
|
1103 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1148 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d."), |
1104 | conf->nodename, (const char *)rsi, |
1149 | conf->nodename, (const char *)rsi, |
1105 | p->prot_major, p->prot_minor); |
1150 | p->prot_major, p->prot_minor); |
1106 | |
1151 | |
|
|
1152 | connection_established (); |
|
|
1153 | |
1107 | if (::conf.script_node_up) |
1154 | if (::conf.script_node_up) |
1108 | { |
1155 | { |
1109 | run_script_cb cb; |
1156 | run_script_cb *cb = new run_script_cb; |
1110 | cb.set<connection, &connection::script_node_up> (this); |
1157 | cb->set<connection, &connection::script_node_up> (this); |
1111 | if (!run_script (cb, false)) |
|
|
1112 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
1158 | run_script_queued (cb, _("node-up command execution failed, continuing.")); |
1113 | } |
1159 | } |
1114 | |
1160 | |
1115 | break; |
1161 | break; |
1116 | } |
1162 | } |
1117 | else |
1163 | else |
1118 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1164 | slog (L_ERR, _("%s(%s): sent and received challenge do not match."), |
1119 | conf->nodename, (const char *)rsi); |
1165 | conf->nodename, (const char *)rsi); |
1120 | } |
1166 | } |
1121 | |
1167 | |
1122 | delete cctx; |
1168 | delete cctx; |
1123 | } |
1169 | } |
… | |
… | |
1139 | { |
1185 | { |
1140 | vpndata_packet *p = (vpndata_packet *)pkt; |
1186 | vpndata_packet *p = (vpndata_packet *)pkt; |
1141 | |
1187 | |
1142 | if (!p->hmac_chk (ictx)) |
1188 | if (!p->hmac_chk (ictx)) |
1143 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1189 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1144 | "could be an attack, or just corruption or a synchronization error"), |
1190 | "could be an attack, or just corruption or a synchronization error."), |
1145 | conf->nodename, (const char *)rsi); |
1191 | conf->nodename, (const char *)rsi); |
1146 | else |
1192 | else |
1147 | { |
1193 | { |
1148 | u32 seqno; |
1194 | u32 seqno; |
1149 | tap_packet *d = p->unpack (this, seqno); |
1195 | tap_packet *d = p->unpack (this, seqno); |
|
|
1196 | int seqclass = iseqno.seqno_classify (seqno); |
1150 | |
1197 | |
1151 | if (iseqno.recv_ok (seqno)) |
1198 | if (seqclass == 0) // ok |
1152 | { |
1199 | { |
1153 | vpn->tap->send (d); |
1200 | vpn->tap->send (d); |
1154 | |
1201 | |
1155 | if (si != rsi) |
1202 | if (si != rsi) |
1156 | { |
1203 | { |
1157 | // fast re-sync on source address changes, useful especially for tcp/ip |
1204 | // fast re-sync on source address changes, useful especially for tcp/ip |
|
|
1205 | //if (last_si_change < ev_now () + 5.) |
|
|
1206 | // { |
1158 | si = rsi; |
1207 | si = rsi; |
1159 | |
1208 | |
1160 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1209 | slog (L_INFO, _("%s(%s): socket address changed to %s."), |
1161 | conf->nodename, (const char *)si, (const char *)rsi); |
1210 | conf->nodename, (const char *)si, (const char *)rsi); |
|
|
1211 | // } |
|
|
1212 | //else |
|
|
1213 | // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), |
|
|
1214 | // conf->nodename, (const char *)si, (const char *)rsi); |
1162 | } |
1215 | } |
|
|
1216 | } |
|
|
1217 | else if (seqclass == 1) // silently ignore |
|
|
1218 | slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" |
|
|
1219 | "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1); |
|
|
1220 | else if (seqclass == 2) // reset |
|
|
1221 | { |
|
|
1222 | slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" |
|
|
1223 | "possible replay attack, or just massive packet loss, resetting connection."), seqno, iseqno.seq + 1); |
|
|
1224 | send_reset (rsi); |
1163 | } |
1225 | } |
1164 | |
1226 | |
1165 | delete d; |
1227 | delete d; |
1166 | break; |
1228 | break; |
1167 | } |
1229 | } |
… | |
… | |
1327 | connection::connection (struct vpn *vpn, conf_node *conf) |
1389 | connection::connection (struct vpn *vpn, conf_node *conf) |
1328 | : vpn(vpn), conf(conf), |
1390 | : vpn(vpn), conf(conf), |
1329 | #if ENABLE_DNS |
1391 | #if ENABLE_DNS |
1330 | dns (0), |
1392 | dns (0), |
1331 | #endif |
1393 | #endif |
1332 | data_queue(conf->max_ttl, conf->max_queue), |
1394 | data_queue(conf->max_ttl, conf->max_queue + 1), |
1333 | vpn_queue(conf->max_ttl, conf->max_queue) |
1395 | vpn_queue(conf->max_ttl, conf->max_queue + 1) |
1334 | { |
1396 | { |
1335 | rekey .set<connection, &connection::rekey_cb > (this); |
1397 | rekey .set<connection, &connection::rekey_cb > (this); |
1336 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1398 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1337 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1399 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1338 | |
1400 | |