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.46 by pcg, Sat Mar 5 19:13:15 2005 UTC vs.
Revision 1.58 by pcg, Sat Jul 9 02:35:42 2005 UTC

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with gvpe; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/ 20*/
21 21
22#include "config.h" 22#include "config.h"
23
24#include <cassert>
25 23
26#include <list> 24#include <list>
27 25
28#include <openssl/rand.h> 26#include <openssl/rand.h>
29#include <openssl/evp.h> 27#include <openssl/evp.h>
30#include <openssl/rsa.h> 28#include <openssl/rsa.h>
31#include <openssl/err.h> 29#include <openssl/err.h>
32
33#include "gettext.h"
34 30
35#include "conf.h" 31#include "conf.h"
36#include "slog.h" 32#include "slog.h"
37#include "device.h" 33#include "device.h"
38#include "vpn.h" 34#include "vpn.h"
97struct rsa_cache : list<rsa_entry> 93struct rsa_cache : list<rsa_entry>
98{ 94{
99 void cleaner_cb (time_watcher &w); time_watcher cleaner; 95 void cleaner_cb (time_watcher &w); time_watcher cleaner;
100 96
101 bool find (const rsaid &id, rsachallenge &chg) 97 bool find (const rsaid &id, rsachallenge &chg)
102 { 98 {
103 for (iterator i = begin (); i != end (); ++i) 99 for (iterator i = begin (); i != end (); ++i)
104 { 100 {
105 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) 101 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW)
106 { 102 {
107 memcpy (&chg, &i->chg, sizeof chg); 103 memcpy (&chg, &i->chg, sizeof chg);
108 104
109 erase (i); 105 erase (i);
110 return true; 106 return true;
111 } 107 }
112 } 108 }
113 109
114 if (cleaner.at < NOW) 110 if (cleaner.at < NOW)
115 cleaner.start (NOW + RSA_TTL); 111 cleaner.start (NOW + RSA_TTL);
116 112
117 return false; 113 return false;
118 } 114 }
119 115
120 void gen (rsaid &id, rsachallenge &chg) 116 void gen (rsaid &id, rsachallenge &chg)
121 { 117 {
122 rsa_entry e; 118 rsa_entry e;
123 119
124 RAND_bytes ((unsigned char *)&id, sizeof id); 120 RAND_bytes ((unsigned char *)&id, sizeof id);
125 RAND_bytes ((unsigned char *)&chg, sizeof chg); 121 RAND_bytes ((unsigned char *)&chg, sizeof chg);
126 122
127 e.expire = NOW + RSA_TTL; 123 e.expire = NOW + RSA_TTL;
128 e.id = id; 124 e.id = id;
129 memcpy (&e.chg, &chg, sizeof chg); 125 memcpy (&e.chg, &chg, sizeof chg);
130 126
131 push_back (e); 127 push_back (e);
132 128
133 if (cleaner.at < NOW) 129 if (cleaner.at < NOW)
134 cleaner.start (NOW + RSA_TTL); 130 cleaner.start (NOW + RSA_TTL);
135 } 131 }
136 132
137 rsa_cache () 133 rsa_cache ()
138 : cleaner (this, &rsa_cache::cleaner_cb) 134 : cleaner (this, &rsa_cache::cleaner_cb)
139 { } 135 { }
140 136
141} rsa_cache; 137} rsa_cache;
142 138
143void rsa_cache::cleaner_cb (time_watcher &w) 139void rsa_cache::cleaner_cb (time_watcher &w)
144{ 140{
478{ 474{
479 prot_major = PROTOCOL_MAJOR; 475 prot_major = PROTOCOL_MAJOR;
480 prot_minor = PROTOCOL_MINOR; 476 prot_minor = PROTOCOL_MINOR;
481 randsize = RAND_SIZE; 477 randsize = RAND_SIZE;
482 hmaclen = HMACLENGTH; 478 hmaclen = HMACLENGTH;
483 flags = ENABLE_COMPRESSION ? 0x81 : 0x80; 479 flags = 0;
484 challengelen = sizeof (rsachallenge); 480 challengelen = sizeof (rsachallenge);
485 features = get_features (); 481 features = get_features ();
486 482
487 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); 483 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER));
488 digest_nid = htonl (EVP_MD_type (RSA_HASH)); 484 digest_nid = htonl (EVP_MD_type (RSA_HASH));
498 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 494 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
499 else if (randsize != RAND_SIZE) 495 else if (randsize != RAND_SIZE)
500 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 496 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
501 else if (hmaclen != HMACLENGTH) 497 else if (hmaclen != HMACLENGTH)
502 slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); 498 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)) 499 else if (challengelen != sizeof (rsachallenge))
508 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); 500 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge));
509 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) 501 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)); 502 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))) 503 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH)))
725} 717}
726 718
727void 719void
728connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 720connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
729{ 721{
730 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", 722 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)",
731 conf->id, rid, (const char *)rsi); 723 conf->id, rid, (const char *)rsi);
732 724
733 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 725 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
734 726
735 r->hmac_set (octx); 727 r->hmac_set (octx);
745 && conf != THISNODE 737 && conf != THISNODE
746 && connectmode != conf_node::C_NEVER 738 && connectmode != conf_node::C_NEVER
747 && connectmode != conf_node::C_DISABLED 739 && connectmode != conf_node::C_DISABLED
748 && NOW > w.at) 740 && NOW > w.at)
749 { 741 {
750 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; 742 w.at = TSTAMP_MAX; // first disable this watcher in case of recursion
751 743
752 if (retry_int < conf->max_retry) 744 double retry_int = double (retry_cnt & 3
753 retry_cnt++; 745 ? (retry_cnt & 3) + 1
754 else 746 : 1 << (retry_cnt >> 2));
755 retry_int = conf->max_retry;
756
757 w.start (NOW + retry_int);
758 747
759 reset_si (); 748 reset_si ();
749
750 bool slow = si.prot & PROT_SLOW;
760 751
761 if (si.prot && !si.host) 752 if (si.prot && !si.host)
762 vpn->send_connect_request (conf->id); 753 vpn->send_connect_request (conf->id);
763 else 754 else
764 { 755 {
765 const sockinfo &dsi = forward_si (si); 756 const sockinfo &dsi = forward_si (si);
757
758 slow = slow || (dsi.prot & PROT_SLOW);
766 759
767 if (dsi.valid () && auth_rate_limiter.can (dsi)) 760 if (dsi.valid () && auth_rate_limiter.can (dsi))
768 { 761 {
769 if (retry_cnt < 4) 762 if (retry_cnt < 4)
770 send_auth_request (dsi, true); 763 send_auth_request (dsi, true);
771 else 764 else
772 send_ping (dsi, 0); 765 send_ping (dsi, 0);
773 } 766 }
774 } 767 }
768
769 retry_int *= slow ? 8. : 0.7;
770
771 if (retry_int < conf->max_retry)
772 retry_cnt++;
773 else
774 retry_int = conf->max_retry;
775
776 w.start (NOW + retry_int);
775 } 777 }
776} 778}
777 779
778void 780void
779connection::reset_connection () 781connection::reset_connection ()
782 { 784 {
783 slog (L_INFO, _("%s(%s): connection lost"), 785 slog (L_INFO, _("%s(%s): connection lost"),
784 conf->nodename, (const char *)si); 786 conf->nodename, (const char *)si);
785 787
786 if (::conf.script_node_down) 788 if (::conf.script_node_down)
787 run_script (run_script_cb (this, &connection::script_node_down), false); 789 if (!run_script (run_script_cb (this, &connection::script_node_down), false))
790 slog (L_WARN, _("node-down command execution failed, continuing."));
788 } 791 }
789 792
790 delete ictx; ictx = 0; 793 delete ictx; ictx = 0;
791 delete octx; octx = 0; 794 delete octx; octx = 0;
792#if ENABLE_DNS 795#if ENABLE_DNS
843{ 846{
844 if (ictx && octx) 847 if (ictx && octx)
845 send_data_packet (pkt); 848 send_data_packet (pkt);
846 else 849 else
847 { 850 {
848 if (!broadcast)//DDDD 851 if (!broadcast)
849 data_queue.put (new tap_packet (*pkt)); 852 data_queue.put (new tap_packet (*pkt));
850 853
851 establish_connection (); 854 establish_connection ();
852 } 855 }
853} 856}
938 delete octx; 941 delete octx;
939 942
940 octx = new crypto_ctx (k, 1); 943 octx = new crypto_ctx (k, 1);
941 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 944 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
942 945
943 // compatibility code, remove when no longer required
944 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
945
946 conf->protocols = p->protocols; 946 conf->protocols = p->protocols;
947 features = p->features & config_packet::get_features (); 947 features = p->features & config_packet::get_features ();
948 948
949 send_auth_response (rsi, p->id, k); 949 send_auth_response (rsi, p->id, k);
950 950
1016 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1016 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1017 conf->nodename, (const char *)rsi, 1017 conf->nodename, (const char *)rsi,
1018 p->prot_major, p->prot_minor); 1018 p->prot_major, p->prot_minor);
1019 1019
1020 if (::conf.script_node_up) 1020 if (::conf.script_node_up)
1021 run_script (run_script_cb (this, &connection::script_node_up), false); 1021 if (!run_script (run_script_cb (this, &connection::script_node_up), false))
1022 slog (L_WARN, _("node-up command execution failed, continuing."));
1022 1023
1023 break; 1024 break;
1024 } 1025 }
1025 else 1026 else
1026 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1027 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1060 { 1061 {
1061 vpn->tap->send (d); 1062 vpn->tap->send (d);
1062 1063
1063 if (si != rsi) 1064 if (si != rsi)
1064 { 1065 {
1065 // fast re-sync on connection changes, useful especially for tcp/ip 1066 // fast re-sync on source address changes, useful especially for tcp/ip
1066 si = rsi; 1067 si = rsi;
1067 1068
1068 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1069 slog (L_INFO, _("%s(%s): socket address changed to %s"),
1069 conf->nodename, (const char *)si, (const char *)rsi); 1070 conf->nodename, (const char *)si, (const char *)rsi);
1070 } 1071 }
1081 case vpn_packet::PT_CONNECT_REQ: 1082 case vpn_packet::PT_CONNECT_REQ:
1082 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1083 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1083 { 1084 {
1084 connect_req_packet *p = (connect_req_packet *) pkt; 1085 connect_req_packet *p = (connect_req_packet *) pkt;
1085 1086
1086 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1087 if (p->id > 0 && p->id <= vpn->conns.size ())
1087 connection *c = vpn->conns[p->id - 1];
1088 conf->protocols = p->protocols;
1089
1090 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n",
1091 conf->id, p->id, c->ictx && c->octx);
1092
1093 if (c->ictx && c->octx)
1094 { 1088 {
1089 connection *c = vpn->conns[p->id - 1];
1090 conf->protocols = p->protocols;
1091
1092 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]",
1093 conf->id, p->id, c->ictx && c->octx);
1094
1095 if (c->ictx && c->octx)
1096 {
1095 // send connect_info packets to both sides, in case one is 1097 // send connect_info packets to both sides, in case one is
1096 // behind a nat firewall (or both ;) 1098 // behind a nat firewall (or both ;)
1097 c->send_connect_info (conf->id, si, conf->protocols); 1099 c->send_connect_info (conf->id, si, conf->protocols);
1098 send_connect_info (c->conf->id, c->si, c->conf->protocols); 1100 send_connect_info (c->conf->id, c->si, c->conf->protocols);
1101 }
1102 else
1103 c->establish_connection ();
1099 } 1104 }
1100 else 1105 else
1101 c->establish_connection (); 1106 slog (L_WARN,
1107 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1108 p->id);
1102 } 1109 }
1103 1110
1104 break; 1111 break;
1105 1112
1106 case vpn_packet::PT_CONNECT_INFO: 1113 case vpn_packet::PT_CONNECT_INFO:
1107 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1114 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1108 { 1115 {
1109 connect_info_packet *p = (connect_info_packet *)pkt; 1116 connect_info_packet *p = (connect_info_packet *)pkt;
1110 1117
1111 if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything 1118 if (p->id > 0 && p->id <= vpn->conns.size ())
1112 { 1119 {
1113 connection *c = vpn->conns[p->id - 1]; 1120 connection *c = vpn->conns[p->id - 1];
1114 1121
1115 c->conf->protocols = p->protocols; 1122 c->conf->protocols = p->protocols;
1116 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1123 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1122 const sockinfo &dsi = forward_si (p->si); 1129 const sockinfo &dsi = forward_si (p->si);
1123 1130
1124 if (dsi.valid ()) 1131 if (dsi.valid ())
1125 c->send_auth_request (dsi, true); 1132 c->send_auth_request (dsi, true);
1126 } 1133 }
1134 else
1135 slog (L_WARN,
1136 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1137 p->id);
1127 } 1138 }
1128 1139
1129 break; 1140 break;
1130 1141
1131 default: 1142 default:
1166 send_vpn_packet (p, si); 1177 send_vpn_packet (p, si);
1167 1178
1168 delete p; 1179 delete p;
1169} 1180}
1170 1181
1182void connection::script_init_env (const char *ext)
1183{
1184 char *env;
1185 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1186 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1187 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1188 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1189 conf->id & 0xff); putenv (env);
1190}
1191
1171void connection::script_node () 1192void connection::script_init_connect_env ()
1172{ 1193{
1173 vpn->script_if_up (); 1194 vpn->script_init_env ();
1174 1195
1175 char *env; 1196 char *env;
1176 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1197 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1177 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1198 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1178 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1199 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1179 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1200 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1180} 1201}
1181 1202
1182const char *connection::script_node_up () 1203const char *connection::script_node_up ()
1183{ 1204{
1184 script_node (); 1205 script_init_connect_env ();
1185 1206
1186 putenv ("STATE=up"); 1207 putenv ("STATE=up");
1187 1208
1209 char *filename;
1210 asprintf (&filename,
1211 "%s/%s",
1212 confbase,
1188 return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; 1213 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1214
1215 return filename;
1189} 1216}
1190 1217
1191const char *connection::script_node_down () 1218const char *connection::script_node_down ()
1192{ 1219{
1193 script_node (); 1220 script_init_connect_env ();
1194 1221
1195 putenv ("STATE=down"); 1222 putenv ("STATE=down");
1196 1223
1197 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1224 char *filename;
1225 asprintf (&filename,
1226 "%s/%s",
1227 confbase,
1228 ::conf.script_node_down ? ::conf.script_node_down : "node-down");
1229
1230 return filename;
1198} 1231}
1199 1232
1200connection::connection (struct vpn *vpn, conf_node *conf) 1233connection::connection (struct vpn *vpn, conf_node *conf)
1201: vpn(vpn), conf(conf) 1234: vpn(vpn), conf(conf)
1202, rekey (this, &connection::rekey_cb) 1235, rekey (this, &connection::rekey_cb)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines