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.43 by pcg, Fri Mar 4 04:52:38 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)))
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)
751} 717}
752 718
753void 719void
754connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 720connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
755{ 721{
756 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", 722 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)",
757 conf->id, rid, (const char *)rsi); 723 conf->id, rid, (const char *)rsi);
758 724
759 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);
760 726
761 r->hmac_set (octx); 727 r->hmac_set (octx);
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;
795#if ENABLE_DNS
796 dnsv4_reset_connection ();
797#endif
818 798
819 si.host = 0; 799 si.host = 0;
820 800
821 last_activity = 0; 801 last_activity = 0;
822 retry_cnt = 0; 802 retry_cnt = 0;
866{ 846{
867 if (ictx && octx) 847 if (ictx && octx)
868 send_data_packet (pkt); 848 send_data_packet (pkt);
869 else 849 else
870 { 850 {
871 if (!broadcast)//DDDD 851 if (!broadcast)
872 data_queue.put (new tap_packet (*pkt)); 852 data_queue.put (new tap_packet (*pkt));
873 853
874 establish_connection (); 854 establish_connection ();
875 } 855 }
876} 856}
961 delete octx; 941 delete octx;
962 942
963 octx = new crypto_ctx (k, 1); 943 octx = new crypto_ctx (k, 1);
964 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 944 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
965 945
966 // compatibility code, remove when no longer required
967 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
968
969 conf->protocols = p->protocols; 946 conf->protocols = p->protocols;
970 features = p->features & config_packet::get_features (); 947 features = p->features & config_packet::get_features ();
971 948
972 send_auth_response (rsi, p->id, k); 949 send_auth_response (rsi, p->id, k);
973 950
1039 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1016 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1040 conf->nodename, (const char *)rsi, 1017 conf->nodename, (const char *)rsi,
1041 p->prot_major, p->prot_minor); 1018 p->prot_major, p->prot_minor);
1042 1019
1043 if (::conf.script_node_up) 1020 if (::conf.script_node_up)
1044 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."));
1045 1023
1046 break; 1024 break;
1047 } 1025 }
1048 else 1026 else
1049 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"),
1083 { 1061 {
1084 vpn->tap->send (d); 1062 vpn->tap->send (d);
1085 1063
1086 if (si != rsi) 1064 if (si != rsi)
1087 { 1065 {
1088 // fast re-sync on connection changes, useful especially for tcp/ip 1066 // fast re-sync on source address changes, useful especially for tcp/ip
1089 si = rsi; 1067 si = rsi;
1090 1068
1091 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1069 slog (L_INFO, _("%s(%s): socket address changed to %s"),
1092 conf->nodename, (const char *)si, (const char *)rsi); 1070 conf->nodename, (const char *)si, (const char *)rsi);
1093 } 1071 }
1104 case vpn_packet::PT_CONNECT_REQ: 1082 case vpn_packet::PT_CONNECT_REQ:
1105 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1083 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1106 { 1084 {
1107 connect_req_packet *p = (connect_req_packet *) pkt; 1085 connect_req_packet *p = (connect_req_packet *) pkt;
1108 1086
1109 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 ())
1110 connection *c = vpn->conns[p->id - 1];
1111 conf->protocols = p->protocols;
1112
1113 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n",
1114 conf->id, p->id, c->ictx && c->octx);
1115
1116 if (c->ictx && c->octx)
1117 { 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 {
1118 // send connect_info packets to both sides, in case one is 1097 // send connect_info packets to both sides, in case one is
1119 // behind a nat firewall (or both ;) 1098 // behind a nat firewall (or both ;)
1120 c->send_connect_info (conf->id, si, conf->protocols); 1099 c->send_connect_info (conf->id, si, conf->protocols);
1121 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 ();
1122 } 1104 }
1123 else 1105 else
1124 c->establish_connection (); 1106 slog (L_WARN,
1107 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1108 p->id);
1125 } 1109 }
1126 1110
1127 break; 1111 break;
1128 1112
1129 case vpn_packet::PT_CONNECT_INFO: 1113 case vpn_packet::PT_CONNECT_INFO:
1130 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1114 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1131 { 1115 {
1132 connect_info_packet *p = (connect_info_packet *)pkt; 1116 connect_info_packet *p = (connect_info_packet *)pkt;
1133 1117
1134 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 ())
1135 { 1119 {
1136 connection *c = vpn->conns[p->id - 1]; 1120 connection *c = vpn->conns[p->id - 1];
1137 1121
1138 c->conf->protocols = p->protocols; 1122 c->conf->protocols = p->protocols;
1139 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));
1145 const sockinfo &dsi = forward_si (p->si); 1129 const sockinfo &dsi = forward_si (p->si);
1146 1130
1147 if (dsi.valid ()) 1131 if (dsi.valid ())
1148 c->send_auth_request (dsi, true); 1132 c->send_auth_request (dsi, true);
1149 } 1133 }
1134 else
1135 slog (L_WARN,
1136 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1137 p->id);
1150 } 1138 }
1151 1139
1152 break; 1140 break;
1153 1141
1154 default: 1142 default:
1189 send_vpn_packet (p, si); 1177 send_vpn_packet (p, si);
1190 1178
1191 delete p; 1179 delete p;
1192} 1180}
1193 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
1194void connection::script_node () 1192void connection::script_init_connect_env ()
1195{ 1193{
1196 vpn->script_if_up (); 1194 vpn->script_init_env ();
1197 1195
1198 char *env; 1196 char *env;
1199 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1197 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1200 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1198 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1201 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1199 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1202 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1200 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1203} 1201}
1204 1202
1205const char *connection::script_node_up () 1203const char *connection::script_node_up ()
1206{ 1204{
1207 script_node (); 1205 script_init_connect_env ();
1208 1206
1209 putenv ("STATE=up"); 1207 putenv ("STATE=up");
1210 1208
1209 char *filename;
1210 asprintf (&filename,
1211 "%s/%s",
1212 confbase,
1211 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;
1212} 1216}
1213 1217
1214const char *connection::script_node_down () 1218const char *connection::script_node_down ()
1215{ 1219{
1216 script_node (); 1220 script_init_connect_env ();
1217 1221
1218 putenv ("STATE=down"); 1222 putenv ("STATE=down");
1219 1223
1220 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;
1221} 1231}
1222 1232
1223connection::connection (struct vpn *vpn, conf_node *conf) 1233connection::connection (struct vpn *vpn, conf_node *conf)
1224: vpn(vpn), conf(conf) 1234: vpn(vpn), conf(conf)
1225, rekey (this, &connection::rekey_cb) 1235, rekey (this, &connection::rekey_cb)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines