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.109 by root, Fri Oct 11 07:56:07 2013 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) 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"
126 kdf.extract (s, sizeof (s)); 134 kdf.extract (s, sizeof (s));
127 kdf.extract_done (HKDF_PRF_HASH ()); 135 kdf.extract_done (HKDF_PRF_HASH ());
128 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); 136 kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info));
129 137
130 HMAC_CTX_init (&hctx); 138 HMAC_CTX_init (&hctx);
131 require (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); 139 require101 (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0));
132 } 140 }
133 141
134 { 142 {
135 u8 cipher_key[CIPHER_KEYSIZE]; 143 u8 cipher_key[CIPHER_KEYSIZE];
136 static const unsigned char cipher_info[] = "gvpe cipher key"; 144 static const unsigned char cipher_info[] = "gvpe cipher key";
180} 188}
181 189
182static void 190static void
183auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) 191auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac)
184{ 192{
185 hkdf kdf (&auth.ecdh, sizeof (auth.ecdh), AUTH_DIGEST ()); // use remote ecdh b as salt 193 hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt
186 kdf.extract (&auth.rsa, sizeof (auth.rsa)); 194 kdf.extract (&auth.rsa, sizeof (auth.rsa));
187 kdf.extract_done (); 195 kdf.extract_done ();
188 kdf.expand (mac, sizeof mac, b, sizeof b); // use response ecdh b as info 196 kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info
189} 197}
190 198
191void 199void
192connection::generate_auth_data () 200connection::generate_auth_data ()
193{ 201{
364void 372void
365hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) 373hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest)
366{ 374{
367 HMAC_CTX *hctx = &ctx->hctx; 375 HMAC_CTX *hctx = &ctx->hctx;
368 376
369 require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); 377 require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0));
370 require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet))); 378 require101 (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)));
371 require (HMAC_Final (hctx, hmac_digest, 0)); 379 require101 (HMAC_Final (hctx, hmac_digest, 0));
372} 380}
373 381
374void 382void
375hmac_packet::hmac_set (crypto_ctx *ctx) 383hmac_packet::hmac_set (crypto_ctx *ctx)
376{ 384{
693 si = rsi; 701 si = rsi;
694 protocol = rsi.prot; 702 protocol = rsi.prot;
695 703
696 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), 704 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
697 conf->nodename, (const char *)rsi, 705 conf->nodename, (const char *)rsi,
698 is_direct ? "direct" : "forwarded", 706 vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded",
699 PROTOCOL_MAJOR, prot_minor); 707 PROTOCOL_MAJOR, prot_minor);
700 708
701 if (::conf.script_node_up) 709 if (::conf.script_node_up)
702 { 710 {
703 run_script_cb *cb = new run_script_cb; 711 run_script_cb *cb = new run_script_cb;
715 723
716 // make sure rekeying timeouts are slightly asymmetric 724 // make sure rekeying timeouts are slightly asymmetric
717 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 725 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
718 rekey.start (rekey_interval, rekey_interval); 726 rekey.start (rekey_interval, rekey_interval);
719 727
728 hmac_error = 0.;
729
720 keepalive.start (::conf.keepalive); 730 keepalive.start (::conf.keepalive);
721 731
722 // send queued packets 732 // send queued packets
723 if (ictx && octx)
724 {
725 while (tap_packet *p = (tap_packet *)data_queue.get ()) 733 while (tap_packet *p = (tap_packet *)data_queue.get ())
726 { 734 {
727 if (p->len) send_data_packet (p); 735 if (p->len) send_data_packet (p);
728 delete p; 736 delete p;
729 } 737 }
730 738
731 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 739 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
732 { 740 {
733 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); 741 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
734 delete p; 742 delete p;
735 }
736 } 743 }
737 744
738 vpn->connection_established (this); 745 vpn->connection_established (this);
739} 746}
740 747
748 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); 755 slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename);
749 protocol = 0; 756 protocol = 0;
750 } 757 }
751 758
752 si.set (conf, protocol); 759 si.set (conf, protocol);
753
754 is_direct = si.valid ();
755} 760}
756 761
757// ensure sockinfo is valid, forward if necessary 762// ensure sockinfo is valid, forward if necessary
758const sockinfo & 763const sockinfo &
759connection::forward_si (const sockinfo &si) const 764connection::forward_si (const sockinfo &si) const
778 783
779void 784void
780connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 785connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
781{ 786{
782 if (!vpn->send_vpn_packet (pkt, si, tos)) 787 if (!vpn->send_vpn_packet (pkt, si, tos))
783 reset_connection (); 788 reset_connection ("packet send error");
784} 789}
785 790
786void 791void
787connection::send_ping (const sockinfo &si, u8 pong) 792connection::send_ping (const sockinfo &si, u8 pong)
788{ 793{
864 { 869 {
865 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection 870 // 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. 871 // 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 ()) 872 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ())
868 { 873 {
869 reset_connection (); 874 reset_connection ("no demand");
870 return; 875 return;
871 } 876 }
872 877
873 last_establish_attempt = ev_now (); 878 last_establish_attempt = ev_now ();
874 879
876 ? (retry_cnt & 3) + 1 881 ? (retry_cnt & 3) + 1
877 : 1 << (retry_cnt >> 2)); 882 : 1 << (retry_cnt >> 2));
878 883
879 reset_si (); 884 reset_si ();
880 885
881 bool slow = si.prot & PROT_SLOW; 886 bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power);
882 887
883 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 888 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
884 { 889 {
885 /*TODO*/ /* start the timer so we don't recurse endlessly */ 890 /*TODO*/ /* start the timer so we don't recurse endlessly */
886 w.start (1); 891 w.start (1);
896 901
897 slow = slow || (dsi.prot & PROT_SLOW); 902 slow = slow || (dsi.prot & PROT_SLOW);
898 903
899 if (dsi.valid () && auth_rate_limiter.can (dsi)) 904 if (dsi.valid () && auth_rate_limiter.can (dsi))
900 { 905 {
901 if (retry_cnt < 4) 906 // use ping after the first few retries
907 // TODO: on rekeys, the other node might not interpret ping correctly,
908 // TODO: as it will still have a valid connection
909 if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power))
902 send_auth_request (dsi, true); 910 send_auth_request (dsi, true);
903 else 911 else
904 send_ping (dsi, 0); 912 send_ping (dsi, 0);
905 } 913 }
906 } 914 }
907 915
908 retry_int *= slow ? 8. : 0.9; 916 retry_int *= slow ? 4. : 0.9;
909 917
910 if (retry_int < conf->max_retry) 918 if (retry_int < conf->max_retry)
911 retry_cnt++; 919 retry_cnt++;
912 else 920 else
913 retry_int = conf->max_retry; 921 retry_int = conf->max_retry;
915 w.start (retry_int); 923 w.start (retry_int);
916 } 924 }
917} 925}
918 926
919void 927void
920connection::reset_connection () 928connection::reset_connection (const char *reason)
921{ 929{
922 if (ictx && octx) 930 if (ictx && octx)
923 { 931 {
924 slog (L_INFO, _("%s(%s): connection lost"), 932 slog (L_INFO, _("%s(%s): connection lost (%s)"),
925 conf->nodename, (const char *)si); 933 conf->nodename, (const char *)si, reason);
926 934
927 if (::conf.script_node_down) 935 if (::conf.script_node_down)
928 { 936 {
929 run_script_cb *cb = new run_script_cb; 937 run_script_cb *cb = new run_script_cb;
930 cb->set<connection, &connection::script_node_down> (this); 938 cb->set<connection, &connection::script_node_down> (this);
954connection::shutdown () 962connection::shutdown ()
955{ 963{
956 if (ictx && octx) 964 if (ictx && octx)
957 send_reset (si); 965 send_reset (si);
958 966
959 reset_connection (); 967 reset_connection ("shutdown");
960} 968}
961 969
962// poor-man's rekeying 970// poor-man's rekeying
963inline void 971inline void
964connection::rekey_cb (ev::timer &w, int revents) 972connection::rekey_cb (ev::timer &w, int revents)
965{ 973{
966 reset_connection (); 974 reset_connection ("rekeying");
967 establish_connection (); 975 establish_connection ();
968} 976}
969 977
970void 978void
971connection::send_data_packet (tap_packet *pkt) 979connection::send_data_packet (tap_packet *pkt)
988 996
989void 997void
990connection::post_inject_queue () 998connection::post_inject_queue ()
991{ 999{
992 // force a connection every now and when when packets are sent (max 1/s) 1000 // force a connection every now and when when packets are sent (max 1/s)
993 if (ev_now () - last_establish_attempt >= 0.95) // arbitrary 1001 if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary
994 establish_connection.stop (); 1002 establish_connection.stop ();
995 1003
996 establish_connection (); 1004 establish_connection ();
997} 1005}
998 1006
1058 // about our desire for communication. 1066 // about our desire for communication.
1059 establish_connection (); 1067 establish_connection ();
1060 break; 1068 break;
1061 1069
1062 case vpn_packet::PT_RESET: 1070 case vpn_packet::PT_RESET:
1071 slog (L_TRACE, "%s >> PT_RESET", conf->nodename);
1072
1073 if (ictx && octx)
1063 { 1074 {
1064 reset_connection (); 1075 reset_connection ("remote reset");
1065 1076
1066 config_packet *p = (config_packet *) pkt; 1077 config_packet *p = (config_packet *) pkt;
1067 1078
1068 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) 1079 if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS)
1069 establish_connection (); 1080 establish_connection ();
1070 } 1081 }
1082
1071 break; 1083 break;
1072 1084
1073 case vpn_packet::PT_AUTH_REQ: 1085 case vpn_packet::PT_AUTH_REQ:
1074 if (auth_rate_limiter.can (rsi)) 1086 if (auth_rate_limiter.can (rsi))
1075 { 1087 {
1090 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1102 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1091 conf->nodename, (const char *)rsi, 1103 conf->nodename, (const char *)rsi,
1092 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1104 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1093 1105
1094 if (p->initiate) 1106 if (p->initiate)
1107 {
1095 send_auth_request (rsi, false); 1108 send_auth_request (rsi, false);
1109
1110 if (ictx && octx)
1111 reset_connection ("reconnect");
1112 }
1096 1113
1097 auth_data auth; 1114 auth_data auth;
1098 1115
1099 if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) 1116 if (!auth_decrypt (::conf.rsa_key, p->encr, auth))
1100 { 1117 {
1146 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b); 1163 memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b);
1147 1164
1148 have_snd_auth = true; 1165 have_snd_auth = true;
1149 connection_established (rsi); 1166 connection_established (rsi);
1150 } 1167 }
1151
1152 break;
1153 } 1168 }
1154
1155 send_reset (rsi);
1156 break; 1169 break;
1157 1170
1158 case vpn_packet::PT_DATA_COMPRESSED: 1171 case vpn_packet::PT_DATA_COMPRESSED:
1159#if !ENABLE_COMPRESSION 1172#if !ENABLE_COMPRESSION
1160 send_reset (rsi); 1173 send_reset (rsi);
1166 if (ictx && octx) 1179 if (ictx && octx)
1167 { 1180 {
1168 vpndata_packet *p = (vpndata_packet *)pkt; 1181 vpndata_packet *p = (vpndata_packet *)pkt;
1169 1182
1170 if (!p->hmac_chk (ictx)) 1183 if (!p->hmac_chk (ictx))
1184 {
1185 // rekeying often creates temporary hmac auth floods
1186 // we assume they don't take longer than a few seconds normally,
1187 // and suppress messages and resets during that time.
1188 //TODO: should be done per source address
1189 if (!hmac_error)
1190 {
1191 hmac_error = ev_now () + 3;
1192 break;
1193 }
1194 else if (hmac_error >= ev_now ())
1195 break; // silently suppress
1196 else
1197 {
1171 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1198 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1172 "could be an attack, or just corruption or a synchronization error."), 1199 "could be an attack, or just corruption or a synchronization error."),
1173 conf->nodename, (const char *)rsi); 1200 conf->nodename, (const char *)rsi);
1201 // reset
1202 }
1203 }
1174 else 1204 else
1175 { 1205 {
1176 u32 seqno; 1206 u32 seqno;
1177 tap_packet *d = p->unpack (this, seqno); 1207 tap_packet *d = p->unpack (this, seqno);
1178 int seqclass = iseqno.seqno_classify (seqno); 1208 int seqclass = iseqno.seqno_classify (seqno);
1209
1210 hmac_error = 0;
1179 1211
1180 if (seqclass == 0) // ok 1212 if (seqclass == 0) // ok
1181 { 1213 {
1182 vpn->tap->send (d); 1214 vpn->tap->send (d);
1183 1215
1310 1342
1311 if (when >= 0) 1343 if (when >= 0)
1312 w.start (when); 1344 w.start (when);
1313 else if (when < -15) 1345 else if (when < -15)
1314 { 1346 {
1315 reset_connection (); 1347 reset_connection ("keepalive overdue");
1316 establish_connection (); 1348 establish_connection ();
1317 } 1349 }
1318 else if (conf->connectmode != conf_node::C_ONDEMAND 1350 else if (conf->connectmode != conf_node::C_ONDEMAND
1319 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1351 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1320 { 1352 {
1324 else if (when >= -10) 1356 else if (when >= -10)
1325 // hold ondemand connections implicitly a few seconds longer 1357 // hold ondemand connections implicitly a few seconds longer
1326 // should delete octx, though, or something like that ;) 1358 // should delete octx, though, or something like that ;)
1327 w.start (when + 10); 1359 w.start (when + 10);
1328 else 1360 else
1329 reset_connection (); 1361 reset_connection ("keepalive timeout");
1330} 1362}
1331 1363
1332void 1364void
1333connection::send_connect_request (int id) 1365connection::send_connect_request (int id)
1334{ 1366{
1434 1466
1435 // queue a dummy packet to force an initial connection attempt 1467 // queue a dummy packet to force an initial connection attempt
1436 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1468 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1437 vpn_queue.put (new net_packet); 1469 vpn_queue.put (new net_packet);
1438 1470
1439 reset_connection (); 1471 reset_connection ("startup");
1440} 1472}
1441 1473
1442connection::~connection () 1474connection::~connection ()
1443{ 1475{
1444 shutdown (); 1476 shutdown ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines