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.45 by pcg, Fri Mar 4 09:36:45 2005 UTC vs.
Revision 1.57 by pcg, Thu Jul 7 14:41:51 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)))
644 { 636 {
645 connection *r = vpn->find_router (); 637 connection *r = vpn->find_router ();
646 638
647 if (r) 639 if (r)
648 { 640 {
649 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), 641 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"),
650 conf->nodename, r->conf->nodename); 642 conf->nodename, r->conf->nodename, (const char *)r->si);
651 return r->si; 643 return r->si;
652 } 644 }
653 else 645 else
654 slog (L_DEBUG, _("%s: node unreachable, no common protocol"), 646 slog (L_DEBUG, _("%s: node unreachable, no common protocol"),
655 conf->nodename); 647 conf->nodename);
659} 651}
660 652
661void 653void
662connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 654connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
663{ 655{
664 bool ok; 656 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 (); 657 reset_connection ();
692} 658}
693 659
694void 660void
695connection::send_ping (const sockinfo &si, u8 pong) 661connection::send_ping (const sockinfo &si, u8 pong)
771 && conf != THISNODE 737 && conf != THISNODE
772 && connectmode != conf_node::C_NEVER 738 && connectmode != conf_node::C_NEVER
773 && connectmode != conf_node::C_DISABLED 739 && connectmode != conf_node::C_DISABLED
774 && NOW > w.at) 740 && NOW > w.at)
775 { 741 {
776 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
777 743
778 if (retry_int < conf->max_retry) 744 double retry_int = double (retry_cnt & 3
779 retry_cnt++; 745 ? (retry_cnt & 3) + 1
780 else 746 : 1 << (retry_cnt >> 2));
781 retry_int = conf->max_retry;
782
783 w.start (NOW + retry_int);
784 747
785 reset_si (); 748 reset_si ();
749
750 bool slow = si.prot & PROT_SLOW;
786 751
787 if (si.prot && !si.host) 752 if (si.prot && !si.host)
788 vpn->send_connect_request (conf->id); 753 vpn->send_connect_request (conf->id);
789 else 754 else
790 { 755 {
791 const sockinfo &dsi = forward_si (si); 756 const sockinfo &dsi = forward_si (si);
757
758 slow = slow || (dsi.prot & PROT_SLOW);
792 759
793 if (dsi.valid () && auth_rate_limiter.can (dsi)) 760 if (dsi.valid () && auth_rate_limiter.can (dsi))
794 { 761 {
795 if (retry_cnt < 4) 762 if (retry_cnt < 4)
796 send_auth_request (dsi, true); 763 send_auth_request (dsi, true);
797 else 764 else
798 send_ping (dsi, 0); 765 send_ping (dsi, 0);
799 } 766 }
800 } 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);
801 } 777 }
802} 778}
803 779
804void 780void
805connection::reset_connection () 781connection::reset_connection ()
808 { 784 {
809 slog (L_INFO, _("%s(%s): connection lost"), 785 slog (L_INFO, _("%s(%s): connection lost"),
810 conf->nodename, (const char *)si); 786 conf->nodename, (const char *)si);
811 787
812 if (::conf.script_node_down) 788 if (::conf.script_node_down)
813 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."));
814 } 791 }
815 792
816 delete ictx; ictx = 0; 793 delete ictx; ictx = 0;
817 delete octx; octx = 0; 794 delete octx; octx = 0;
818#if ENABLE_DNS 795#if ENABLE_DNS
869{ 846{
870 if (ictx && octx) 847 if (ictx && octx)
871 send_data_packet (pkt); 848 send_data_packet (pkt);
872 else 849 else
873 { 850 {
874 if (!broadcast)//DDDD 851 if (!broadcast)
875 data_queue.put (new tap_packet (*pkt)); 852 data_queue.put (new tap_packet (*pkt));
876 853
877 establish_connection (); 854 establish_connection ();
878 } 855 }
879} 856}
964 delete octx; 941 delete octx;
965 942
966 octx = new crypto_ctx (k, 1); 943 octx = new crypto_ctx (k, 1);
967 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 944 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
968 945
969 // compatibility code, remove when no longer required
970 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
971
972 conf->protocols = p->protocols; 946 conf->protocols = p->protocols;
973 features = p->features & config_packet::get_features (); 947 features = p->features & config_packet::get_features ();
974 948
975 send_auth_response (rsi, p->id, k); 949 send_auth_response (rsi, p->id, k);
976 950
1042 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1016 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1043 conf->nodename, (const char *)rsi, 1017 conf->nodename, (const char *)rsi,
1044 p->prot_major, p->prot_minor); 1018 p->prot_major, p->prot_minor);
1045 1019
1046 if (::conf.script_node_up) 1020 if (::conf.script_node_up)
1047 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."));
1048 1023
1049 break; 1024 break;
1050 } 1025 }
1051 else 1026 else
1052 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"),
1086 { 1061 {
1087 vpn->tap->send (d); 1062 vpn->tap->send (d);
1088 1063
1089 if (si != rsi) 1064 if (si != rsi)
1090 { 1065 {
1091 // fast re-sync on connection changes, useful especially for tcp/ip 1066 // fast re-sync on source address changes, useful especially for tcp/ip
1092 si = rsi; 1067 si = rsi;
1093 1068
1094 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1069 slog (L_INFO, _("%s(%s): socket address changed to %s"),
1095 conf->nodename, (const char *)si, (const char *)rsi); 1070 conf->nodename, (const char *)si, (const char *)rsi);
1096 } 1071 }
1107 case vpn_packet::PT_CONNECT_REQ: 1082 case vpn_packet::PT_CONNECT_REQ:
1108 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1083 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1109 { 1084 {
1110 connect_req_packet *p = (connect_req_packet *) pkt; 1085 connect_req_packet *p = (connect_req_packet *) pkt;
1111 1086
1112 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 ())
1113 connection *c = vpn->conns[p->id - 1];
1114 conf->protocols = p->protocols;
1115
1116 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n",
1117 conf->id, p->id, c->ictx && c->octx);
1118
1119 if (c->ictx && c->octx)
1120 { 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]\n",
1093 conf->id, p->id, c->ictx && c->octx);
1094
1095 if (c->ictx && c->octx)
1096 {
1121 // send connect_info packets to both sides, in case one is 1097 // send connect_info packets to both sides, in case one is
1122 // behind a nat firewall (or both ;) 1098 // behind a nat firewall (or both ;)
1123 c->send_connect_info (conf->id, si, conf->protocols); 1099 c->send_connect_info (conf->id, si, conf->protocols);
1124 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 ();
1125 } 1104 }
1126 else 1105 else
1127 c->establish_connection (); 1106 slog (L_WARN,
1107 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1108 p->id);
1128 } 1109 }
1129 1110
1130 break; 1111 break;
1131 1112
1132 case vpn_packet::PT_CONNECT_INFO: 1113 case vpn_packet::PT_CONNECT_INFO:
1133 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1114 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1134 { 1115 {
1135 connect_info_packet *p = (connect_info_packet *)pkt; 1116 connect_info_packet *p = (connect_info_packet *)pkt;
1136 1117
1137 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 ())
1138 { 1119 {
1139 connection *c = vpn->conns[p->id - 1]; 1120 connection *c = vpn->conns[p->id - 1];
1140 1121
1141 c->conf->protocols = p->protocols; 1122 c->conf->protocols = p->protocols;
1142 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));
1148 const sockinfo &dsi = forward_si (p->si); 1129 const sockinfo &dsi = forward_si (p->si);
1149 1130
1150 if (dsi.valid ()) 1131 if (dsi.valid ())
1151 c->send_auth_request (dsi, true); 1132 c->send_auth_request (dsi, true);
1152 } 1133 }
1134 else
1135 slog (L_WARN,
1136 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1137 p->id);
1153 } 1138 }
1154 1139
1155 break; 1140 break;
1156 1141
1157 default: 1142 default:
1192 send_vpn_packet (p, si); 1177 send_vpn_packet (p, si);
1193 1178
1194 delete p; 1179 delete p;
1195} 1180}
1196 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
1197void connection::script_node () 1192void connection::script_init_connect_env ()
1198{ 1193{
1199 vpn->script_if_up (); 1194 vpn->script_init_env ();
1200 1195
1201 char *env; 1196 char *env;
1202 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1197 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1203 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1198 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1204 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1199 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1205 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1200 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1206} 1201}
1207 1202
1208const char *connection::script_node_up () 1203const char *connection::script_node_up ()
1209{ 1204{
1210 script_node (); 1205 script_init_connect_env ();
1211 1206
1212 putenv ("STATE=up"); 1207 putenv ("STATE=up");
1213 1208
1209 char *filename;
1210 asprintf (&filename,
1211 "%s/%s",
1212 confbase,
1214 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;
1215} 1216}
1216 1217
1217const char *connection::script_node_down () 1218const char *connection::script_node_down ()
1218{ 1219{
1219 script_node (); 1220 script_init_connect_env ();
1220 1221
1221 putenv ("STATE=down"); 1222 putenv ("STATE=down");
1222 1223
1223 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;
1224} 1231}
1225 1232
1226connection::connection (struct vpn *vpn, conf_node *conf) 1233connection::connection (struct vpn *vpn, conf_node *conf)
1227: vpn(vpn), conf(conf) 1234: vpn(vpn), conf(conf)
1228, rekey (this, &connection::rekey_cb) 1235, rekey (this, &connection::rekey_cb)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines