ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/connection.C
(Generate patch)

Comparing gvpe/src/connection.C (file contents):
Revision 1.104 by root, Fri Jul 19 18:18:27 2013 UTC vs.
Revision 1.112 by root, Fri Sep 12 10:40:43 2014 UTC

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) require (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"
48#include "hkdf.h" 56#include "hkdf.h"
49 57
50#include "netcompat.h" 58#include "netcompat.h"
51 59
52#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic 60#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 61
55#define ULTRA_FAST 1 62#define ULTRA_FAST 1
56#define HLOG 15 63#define HLOG 15
57#include "lzf/lzf.h" 64#include "lzf/lzf.h"
58#include "lzf/lzf_c.c" 65#include "lzf/lzf_c.c"
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";
180} 187}
181 188
182static void 189static void
183auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) 190auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
184{ 191{
185 hkdf kdf (&auth.ecdh, sizeof (auth.ecdh), AUTH_DIGEST ()); // use remote ecdh b as salt 192 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
186 kdf.extract (&auth.rsa, sizeof (auth.rsa)); 193 kdf.extract (&auth.rsa, sizeof (auth.rsa));
187 kdf.extract_done (); 194 kdf.extract_done ();
188 kdf.expand (mac, sizeof mac, b, sizeof b); // use response ecdh b as info 195 kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info
189} 196}
190 197
191void 198void
192connection::generate_auth_data () 199connection::generate_auth_data ()
193{ 200{
364void 371void
365hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) 372hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
366{ 373{
367 HMAC_CTX *hctx = &ctx->hctx; 374 HMAC_CTX *hctx = &ctx->hctx;
368 375
369 require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); 376 require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0));
370 require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet))); 377 require101 (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)));
371 require (HMAC_Final (hctx, hmac_digest, 0)); 378 require101 (HMAC_Final (hctx, hmac_digest, 0));
372} 379}
373 380
374void 381void
375hmac_packet::hmac_set (crypto_ctx *ctx) 382hmac_packet::hmac_set (crypto_ctx *ctx)
376{ 383{
382bool 389bool
383hmac_packet::hmac_chk (crypto_ctx *ctx) 390hmac_packet::hmac_chk (crypto_ctx *ctx)
384{ 391{
385 unsigned char hmac_digest[EVP_MAX_MD_SIZE]; 392 unsigned char hmac_digest[EVP_MAX_MD_SIZE];
386 hmac_gen (ctx, hmac_digest); 393 hmac_gen (ctx, hmac_digest);
387 return !memcmp (hmac, hmac_digest, HMACLENGTH); 394 return slow_memeq (hmac, hmac_digest, HMACLENGTH);
388} 395}
389 396
390void 397void
391vpn_packet::set_hdr (ptype type_, unsigned int dst) 398vpn_packet::set_hdr (ptype type_, unsigned int dst)
392{ 399{
693 si = rsi; 700 si = rsi;
694 protocol = rsi.prot; 701 protocol = rsi.prot;
695 702
696 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."),
697 conf->nodename, (const char *)rsi, 704 conf->nodename, (const char *)rsi,
698 is_direct ? "direct" : "forwarded", 705 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
699 PROTOCOL_MAJOR, prot_minor); 706 PROTOCOL_MAJOR, prot_minor);
700 707
701 if (::conf.script_node_up) 708 if (::conf.script_node_up)
702 { 709 {
703 run_script_cb *cb = new run_script_cb; 710 run_script_cb *cb = new run_script_cb;
715 722
716 // make sure rekeying timeouts are slightly asymmetric 723 // make sure rekeying timeouts are slightly asymmetric
717 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);
718 rekey.start (rekey_interval, rekey_interval); 725 rekey.start (rekey_interval, rekey_interval);
719 726
727 hmac_error = 0.;
728
720 keepalive.start (::conf.keepalive); 729 keepalive.start (::conf.keepalive);
721 730
722 // send queued packets 731 // send queued packets
723 if (ictx && octx)
724 {
725 while (tap_packet *p = (tap_packet *)data_queue.get ()) 732 while (tap_packet *p = (tap_packet *)data_queue.get ())
726 { 733 {
727 if (p->len) send_data_packet (p); 734 if (p->len) send_data_packet (p);
728 delete p; 735 delete p;
729 } 736 }
730 737
731 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 738 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
732 { 739 {
733 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 740 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
734 delete p; 741 delete p;
735 }
736 } 742 }
737 743
738 vpn->connection_established (this); 744 vpn->connection_established (this);
739} 745}
740 746
748 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); 754 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
749 protocol = 0; 755 protocol = 0;
750 } 756 }
751 757
752 si.set (conf, protocol); 758 si.set (conf, protocol);
753
754 is_direct = si.valid ();
755} 759}
756 760
757// ensure sockinfo is valid, forward if necessary 761// ensure sockinfo is valid, forward if necessary
758const sockinfo & 762const sockinfo &
759connection::forward_si (const sockinfo &si) const 763connection::forward_si (const sockinfo &si) const
778 782
779void 783void
780connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 784connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
781{ 785{
782 if (!vpn->send_vpn_packet (pkt, si, tos)) 786 if (!vpn->send_vpn_packet (pkt, si, tos))
783 reset_connection (); 787 reset_connection ("packet send error");
784} 788}
785 789
786void 790void
787connection::send_ping (const sockinfo &si, u8 pong) 791connection::send_ping (const sockinfo &si, u8 pong)
788{ 792{
864 { 868 {
865 // 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
866 // 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.
867 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 ())
868 { 872 {
869 reset_connection (); 873 reset_connection ("no demand");
870 return; 874 return;
871 } 875 }
872 876
873 last_establish_attempt = ev_now (); 877 last_establish_attempt = ev_now ();
874 878
876 ? (retry_cnt & 3) + 1 880 ? (retry_cnt & 3) + 1
877 : 1 << (retry_cnt >> 2)); 881 : 1 << (retry_cnt >> 2));
878 882
879 reset_si (); 883 reset_si ();
880 884
881 bool slow = si.prot & PROT_SLOW; 885 bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power);
882 886
883 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 887 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
884 { 888 {
885 /*TODO*/ /* start the timer so we don't recurse endlessly */ 889 /*TODO*/ /* start the timer so we don't recurse endlessly */
886 w.start (1); 890 w.start (1);
896 900
897 slow = slow || (dsi.prot & PROT_SLOW); 901 slow = slow || (dsi.prot & PROT_SLOW);
898 902
899 if (dsi.valid () && auth_rate_limiter.can (dsi)) 903 if (dsi.valid () && auth_rate_limiter.can (dsi))
900 { 904 {
901 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))
902 send_auth_request (dsi, true); 909 send_auth_request (dsi, true);
903 else 910 else
904 send_ping (dsi, 0); 911 send_ping (dsi, 0);
905 } 912 }
906 } 913 }
907 914
908 retry_int *= slow ? 8. : 0.9; 915 retry_int *= slow ? 4. : 0.9;
909 916
910 if (retry_int < conf->max_retry) 917 if (retry_int < conf->max_retry)
911 retry_cnt++; 918 retry_cnt++;
912 else 919 else
913 retry_int = conf->max_retry; 920 retry_int = conf->max_retry;
915 w.start (retry_int); 922 w.start (retry_int);
916 } 923 }
917} 924}
918 925
919void 926void
920connection::reset_connection () 927connection::reset_connection (const char *reason)
921{ 928{
922 if (ictx && octx) 929 if (ictx && octx)
923 { 930 {
924 slog (L_INFO, _("%s(%s): connection lost"), 931 slog (L_INFO, _("%s(%s): connection lost (%s)"),
925 conf->nodename, (const char *)si); 932 conf->nodename, (const char *)si, reason);
926 933
927 if (::conf.script_node_down) 934 if (::conf.script_node_down)
928 { 935 {
929 run_script_cb *cb = new run_script_cb; 936 run_script_cb *cb = new run_script_cb;
930 cb->set<connection, &connection::script_node_down> (this); 937 cb->set<connection, &connection::script_node_down> (this);
954connection::shutdown () 961connection::shutdown ()
955{ 962{
956 if (ictx && octx) 963 if (ictx && octx)
957 send_reset (si); 964 send_reset (si);
958 965
959 reset_connection (); 966 reset_connection ("shutdown");
960} 967}
961 968
962// poor-man's rekeying 969// poor-man's rekeying
963inline void 970inline void
964connection::rekey_cb (ev::timer &w, int revents) 971connection::rekey_cb (ev::timer &w, int revents)
965{ 972{
966 reset_connection (); 973 reset_connection ("rekeying");
967 establish_connection (); 974 establish_connection ();
968} 975}
969 976
970void 977void
971connection::send_data_packet (tap_packet *pkt) 978connection::send_data_packet (tap_packet *pkt)
988 995
989void 996void
990connection::post_inject_queue () 997connection::post_inject_queue ()
991{ 998{
992 // 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)
993 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
994 establish_connection.stop (); 1001 establish_connection.stop ();
995 1002
996 establish_connection (); 1003 establish_connection ();
997} 1004}
998 1005
1058 // about our desire for communication. 1065 // about our desire for communication.
1059 establish_connection (); 1066 establish_connection ();
1060 break; 1067 break;
1061 1068
1062 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)
1063 { 1073 {
1064 reset_connection (); 1074 reset_connection ("remote reset");
1065 1075
1066 config_packet *p = (config_packet *) pkt; 1076 config_packet *p = (config_packet *) pkt;
1067 1077
1068 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) 1078 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS)
1069 establish_connection (); 1079 establish_connection ();
1070 } 1080 }
1081
1071 break; 1082 break;
1072 1083
1073 case vpn_packet::PT_AUTH_REQ: 1084 case vpn_packet::PT_AUTH_REQ:
1074 if (auth_rate_limiter.can (rsi)) 1085 if (auth_rate_limiter.can (rsi))
1075 { 1086 {
1090 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."),
1091 conf->nodename, (const char *)rsi, 1102 conf->nodename, (const char *)rsi,
1092 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1103 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1093 1104
1094 if (p->initiate) 1105 if (p->initiate)
1106 {
1095 send_auth_request (rsi, false); 1107 send_auth_request (rsi, false);
1108
1109 if (ictx && octx)
1110 reset_connection ("reconnect");
1111 }
1096 1112
1097 auth_data auth; 1113 auth_data auth;
1098 1114
1099 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1115 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1100 { 1116 {
1101 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?"),
1102 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));
1103 } 1119 }
1104 else 1120 else
1105 { 1121 {
1106 bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); 1122 bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth);
1107 1123
1108 rcv_auth = auth; 1124 rcv_auth = auth;
1109 have_rcv_auth = true; 1125 have_rcv_auth = true;
1110 1126
1111 send_auth_response (rsi); 1127 send_auth_response (rsi);
1134 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); 1150 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1135 1151
1136 auth_mac local_mac; 1152 auth_mac local_mac;
1137 auth_hash (snd_auth, p->response.ecdh, local_mac); 1153 auth_hash (snd_auth, p->response.ecdh, local_mac);
1138 1154
1139 if (memcmp (&p->response.mac, local_mac, sizeof local_mac)) 1155 if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac))
1140 { 1156 {
1141 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), 1157 slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."),
1142 conf->nodename, (const char *)rsi); 1158 conf->nodename, (const char *)rsi);
1143 } 1159 }
1144 else if (!have_snd_auth) 1160 else if (!have_snd_auth)
1146 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b); 1162 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1147 1163
1148 have_snd_auth = true; 1164 have_snd_auth = true;
1149 connection_established (rsi); 1165 connection_established (rsi);
1150 } 1166 }
1151
1152 break;
1153 } 1167 }
1154
1155 send_reset (rsi);
1156 break; 1168 break;
1157 1169
1158 case vpn_packet::PT_DATA_COMPRESSED: 1170 case vpn_packet::PT_DATA_COMPRESSED:
1159#if !ENABLE_COMPRESSION 1171#if !ENABLE_COMPRESSION
1160 send_reset (rsi); 1172 send_reset (rsi);
1166 if (ictx && octx) 1178 if (ictx && octx)
1167 { 1179 {
1168 vpndata_packet *p = (vpndata_packet *)pkt; 1180 vpndata_packet *p = (vpndata_packet *)pkt;
1169 1181
1170 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 {
1171 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"
1172 "could be an attack, or just corruption or a synchronization error."), 1198 "could be an attack, or just corruption or a synchronization error."),
1173 conf->nodename, (const char *)rsi); 1199 conf->nodename, (const char *)rsi);
1200 // reset
1201 }
1202 }
1174 else 1203 else
1175 { 1204 {
1176 u32 seqno; 1205 u32 seqno;
1177 tap_packet *d = p->unpack (this, seqno); 1206 tap_packet *d = p->unpack (this, seqno);
1178 int seqclass = iseqno.seqno_classify (seqno); 1207 int seqclass = iseqno.seqno_classify (seqno);
1208
1209 hmac_error = 0;
1179 1210
1180 if (seqclass == 0) // ok 1211 if (seqclass == 0) // ok
1181 { 1212 {
1182 vpn->tap->send (d); 1213 vpn->tap->send (d);
1183 1214
1310 1341
1311 if (when >= 0) 1342 if (when >= 0)
1312 w.start (when); 1343 w.start (when);
1313 else if (when < -15) 1344 else if (when < -15)
1314 { 1345 {
1315 reset_connection (); 1346 reset_connection ("keepalive overdue");
1316 establish_connection (); 1347 establish_connection ();
1317 } 1348 }
1318 else if (conf->connectmode != conf_node::C_ONDEMAND 1349 else if (conf->connectmode != conf_node::C_ONDEMAND
1319 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1350 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1320 { 1351 {
1324 else if (when >= -10) 1355 else if (when >= -10)
1325 // hold ondemand connections implicitly a few seconds longer 1356 // hold ondemand connections implicitly a few seconds longer
1326 // should delete octx, though, or something like that ;) 1357 // should delete octx, though, or something like that ;)
1327 w.start (when + 10); 1358 w.start (when + 10);
1328 else 1359 else
1329 reset_connection (); 1360 reset_connection ("keepalive timeout");
1330} 1361}
1331 1362
1332void 1363void
1333connection::send_connect_request (int id) 1364connection::send_connect_request (int id)
1334{ 1365{
1434 1465
1435 // queue a dummy packet to force an initial connection attempt 1466 // queue a dummy packet to force an initial connection attempt
1436 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1467 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1437 vpn_queue.put (new net_packet); 1468 vpn_queue.put (new net_packet);
1438 1469
1439 reset_connection (); 1470 reset_connection ("startup");
1440} 1471}
1441 1472
1442connection::~connection () 1473connection::~connection ()
1443{ 1474{
1444 shutdown (); 1475 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines