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.36 by pcg, Sun May 30 17:36:00 2004 UTC vs.
Revision 1.56 by pcg, Fri Jun 3 05:07:31 2005 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 15 GNU General Public License for more details.
14 16
15 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
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 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
18*/ 20*/
19 21
20#include "config.h" 22#include "config.h"
21 23
22#include <cassert> 24#include <cassert>
25 27
26#include <openssl/rand.h> 28#include <openssl/rand.h>
27#include <openssl/evp.h> 29#include <openssl/evp.h>
28#include <openssl/rsa.h> 30#include <openssl/rsa.h>
29#include <openssl/err.h> 31#include <openssl/err.h>
30
31#include "gettext.h"
32 32
33#include "conf.h" 33#include "conf.h"
34#include "slog.h" 34#include "slog.h"
35#include "device.h" 35#include "device.h"
36#include "vpn.h" 36#include "vpn.h"
95struct rsa_cache : list<rsa_entry> 95struct rsa_cache : list<rsa_entry>
96{ 96{
97 void cleaner_cb (time_watcher &w); time_watcher cleaner; 97 void cleaner_cb (time_watcher &w); time_watcher cleaner;
98 98
99 bool find (const rsaid &id, rsachallenge &chg) 99 bool find (const rsaid &id, rsachallenge &chg)
100 { 100 {
101 for (iterator i = begin (); i != end (); ++i) 101 for (iterator i = begin (); i != end (); ++i)
102 { 102 {
103 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) 103 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW)
104 { 104 {
105 memcpy (&chg, &i->chg, sizeof chg); 105 memcpy (&chg, &i->chg, sizeof chg);
106 106
107 erase (i); 107 erase (i);
108 return true; 108 return true;
109 } 109 }
110 } 110 }
111 111
112 if (cleaner.at < NOW) 112 if (cleaner.at < NOW)
113 cleaner.start (NOW + RSA_TTL); 113 cleaner.start (NOW + RSA_TTL);
114 114
115 return false; 115 return false;
116 } 116 }
117 117
118 void gen (rsaid &id, rsachallenge &chg) 118 void gen (rsaid &id, rsachallenge &chg)
119 { 119 {
120 rsa_entry e; 120 rsa_entry e;
121 121
122 RAND_bytes ((unsigned char *)&id, sizeof id); 122 RAND_bytes ((unsigned char *)&id, sizeof id);
123 RAND_bytes ((unsigned char *)&chg, sizeof chg); 123 RAND_bytes ((unsigned char *)&chg, sizeof chg);
124 124
125 e.expire = NOW + RSA_TTL; 125 e.expire = NOW + RSA_TTL;
126 e.id = id; 126 e.id = id;
127 memcpy (&e.chg, &chg, sizeof chg); 127 memcpy (&e.chg, &chg, sizeof chg);
128 128
129 push_back (e); 129 push_back (e);
130 130
131 if (cleaner.at < NOW) 131 if (cleaner.at < NOW)
132 cleaner.start (NOW + RSA_TTL); 132 cleaner.start (NOW + RSA_TTL);
133 } 133 }
134 134
135 rsa_cache () 135 rsa_cache ()
136 : cleaner (this, &rsa_cache::cleaner_cb) 136 : cleaner (this, &rsa_cache::cleaner_cb)
137 { } 137 { }
138 138
139} rsa_cache; 139} rsa_cache;
140 140
141void rsa_cache::cleaner_cb (time_watcher &w) 141void rsa_cache::cleaner_cb (time_watcher &w)
142{ 142{
476{ 476{
477 prot_major = PROTOCOL_MAJOR; 477 prot_major = PROTOCOL_MAJOR;
478 prot_minor = PROTOCOL_MINOR; 478 prot_minor = PROTOCOL_MINOR;
479 randsize = RAND_SIZE; 479 randsize = RAND_SIZE;
480 hmaclen = HMACLENGTH; 480 hmaclen = HMACLENGTH;
481 flags = ENABLE_COMPRESSION ? 0x81 : 0x80; 481 flags = 0;
482 challengelen = sizeof (rsachallenge); 482 challengelen = sizeof (rsachallenge);
483 features = get_features (); 483 features = get_features ();
484 484
485 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); 485 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER));
486 digest_nid = htonl (EVP_MD_type (RSA_HASH)); 486 digest_nid = htonl (EVP_MD_type (RSA_HASH));
496 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 496 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
497 else if (randsize != RAND_SIZE) 497 else if (randsize != RAND_SIZE)
498 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 498 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
499 else if (hmaclen != HMACLENGTH) 499 else if (hmaclen != HMACLENGTH)
500 slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); 500 slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH);
501#if 0 // this implementation should handle all flag settings
502 else if (flags != curflags ())
503 slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ());
504#endif
505 else if (challengelen != sizeof (rsachallenge)) 501 else if (challengelen != sizeof (rsachallenge))
506 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); 502 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge));
507 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) 503 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER)))
508 slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); 504 slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER));
509 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) 505 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH)))
627 protocol = best_protocol (THISNODE->protocols & conf->protocols); 623 protocol = best_protocol (THISNODE->protocols & conf->protocols);
628 624
629 // mask out protocols we cannot establish 625 // mask out protocols we cannot establish
630 if (!conf->udp_port) protocol &= ~PROT_UDPv4; 626 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
631 if (!conf->tcp_port) protocol &= ~PROT_TCPv4; 627 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
628 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
632 629
633 si.set (conf, protocol); 630 si.set (conf, protocol);
634} 631}
635 632
636// ensure sockinfo is valid, forward if necessary 633// ensure sockinfo is valid, forward if necessary
641 { 638 {
642 connection *r = vpn->find_router (); 639 connection *r = vpn->find_router ();
643 640
644 if (r) 641 if (r)
645 { 642 {
646 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), 643 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"),
647 conf->nodename, r->conf->nodename); 644 conf->nodename, r->conf->nodename, (const char *)r->si);
648 return r->si; 645 return r->si;
649 } 646 }
650 else 647 else
651 slog (L_DEBUG, _("%s: node unreachable, no common protocol"), 648 slog (L_DEBUG, _("%s: node unreachable, no common protocol"),
652 conf->nodename); 649 conf->nodename);
742 && conf != THISNODE 739 && conf != THISNODE
743 && connectmode != conf_node::C_NEVER 740 && connectmode != conf_node::C_NEVER
744 && connectmode != conf_node::C_DISABLED 741 && connectmode != conf_node::C_DISABLED
745 && NOW > w.at) 742 && NOW > w.at)
746 { 743 {
747 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; 744 w.at = TSTAMP_MAX; // first disable this watcher in case of recursion
748 745
749 if (retry_int < 3600 * 8) 746 double retry_int = double (retry_cnt & 3
750 retry_cnt++; 747 ? (retry_cnt & 3) + 1
751 748 : 1 << (retry_cnt >> 2));
752 w.start (NOW + retry_int);
753 749
754 reset_si (); 750 reset_si ();
751
752 bool slow = si.prot & PROT_SLOW;
755 753
756 if (si.prot && !si.host) 754 if (si.prot && !si.host)
757 vpn->send_connect_request (conf->id); 755 vpn->send_connect_request (conf->id);
758 else 756 else
759 { 757 {
760 const sockinfo &dsi = forward_si (si); 758 const sockinfo &dsi = forward_si (si);
759
760 slow = slow || (dsi.prot & PROT_SLOW);
761 761
762 if (dsi.valid () && auth_rate_limiter.can (dsi)) 762 if (dsi.valid () && auth_rate_limiter.can (dsi))
763 { 763 {
764 if (retry_cnt < 4) 764 if (retry_cnt < 4)
765 send_auth_request (dsi, true); 765 send_auth_request (dsi, true);
766 else 766 else
767 send_ping (dsi, 0); 767 send_ping (dsi, 0);
768 } 768 }
769 } 769 }
770
771 retry_int *= slow ? 8. : 0.7;
772
773 if (retry_int < conf->max_retry)
774 retry_cnt++;
775 else
776 retry_int = conf->max_retry;
777
778 w.start (NOW + retry_int);
770 } 779 }
771} 780}
772 781
773void 782void
774connection::reset_connection () 783connection::reset_connection ()
777 { 786 {
778 slog (L_INFO, _("%s(%s): connection lost"), 787 slog (L_INFO, _("%s(%s): connection lost"),
779 conf->nodename, (const char *)si); 788 conf->nodename, (const char *)si);
780 789
781 if (::conf.script_node_down) 790 if (::conf.script_node_down)
782 run_script (run_script_cb (this, &connection::script_node_down), false); 791 if (!run_script (run_script_cb (this, &connection::script_node_down), false))
792 slog (L_WARN, _("node-down command execution failed, continuing."));
783 } 793 }
784 794
785 delete ictx; ictx = 0; 795 delete ictx; ictx = 0;
786 delete octx; octx = 0; 796 delete octx; octx = 0;
797#if ENABLE_DNS
798 dnsv4_reset_connection ();
799#endif
787 800
788 si.host= 0; 801 si.host = 0;
789 802
790 last_activity = 0; 803 last_activity = 0;
791 retry_cnt = 0; 804 retry_cnt = 0;
792 805
793 rekey.stop (); 806 rekey.stop ();
835{ 848{
836 if (ictx && octx) 849 if (ictx && octx)
837 send_data_packet (pkt); 850 send_data_packet (pkt);
838 else 851 else
839 { 852 {
840 if (!broadcast)//DDDD 853 if (!broadcast)
841 data_queue.put (new tap_packet (*pkt)); 854 data_queue.put (new tap_packet (*pkt));
842 855
843 establish_connection (); 856 establish_connection ();
844 } 857 }
845} 858}
930 delete octx; 943 delete octx;
931 944
932 octx = new crypto_ctx (k, 1); 945 octx = new crypto_ctx (k, 1);
933 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 946 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
934 947
935 // compatibility code, remove when no longer required
936 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
937
938 conf->protocols = p->protocols; 948 conf->protocols = p->protocols;
939 features = p->features & config_packet::get_features (); 949 features = p->features & config_packet::get_features ();
940 950
941 send_auth_response (rsi, p->id, k); 951 send_auth_response (rsi, p->id, k);
942 952
1008 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1018 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1009 conf->nodename, (const char *)rsi, 1019 conf->nodename, (const char *)rsi,
1010 p->prot_major, p->prot_minor); 1020 p->prot_major, p->prot_minor);
1011 1021
1012 if (::conf.script_node_up) 1022 if (::conf.script_node_up)
1013 run_script (run_script_cb (this, &connection::script_node_up), false); 1023 if (!run_script (run_script_cb (this, &connection::script_node_up), false))
1024 slog (L_WARN, _("node-up command execution failed, continuing."));
1014 1025
1015 break; 1026 break;
1016 } 1027 }
1017 else 1028 else
1018 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1029 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1052 { 1063 {
1053 vpn->tap->send (d); 1064 vpn->tap->send (d);
1054 1065
1055 if (si != rsi) 1066 if (si != rsi)
1056 { 1067 {
1057 // fast re-sync on connection changes, useful especially for tcp/ip 1068 // fast re-sync on source address changes, useful especially for tcp/ip
1058 si = rsi; 1069 si = rsi;
1059 1070
1060 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1071 slog (L_INFO, _("%s(%s): socket address changed to %s"),
1061 conf->nodename, (const char *)si, (const char *)rsi); 1072 conf->nodename, (const char *)si, (const char *)rsi);
1062 } 1073 }
1096 break; 1107 break;
1097 1108
1098 case vpn_packet::PT_CONNECT_INFO: 1109 case vpn_packet::PT_CONNECT_INFO:
1099 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1110 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1100 { 1111 {
1101 connect_info_packet *p = (connect_info_packet *) pkt; 1112 connect_info_packet *p = (connect_info_packet *)pkt;
1102 1113
1103 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1114 if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything
1104 1115 {
1105 connection *c = vpn->conns[p->id - 1]; 1116 connection *c = vpn->conns[p->id - 1];
1106 1117
1107 c->conf->protocols = p->protocols; 1118 c->conf->protocols = p->protocols;
1108 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1119 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1109 p->si.upgrade_protocol (protocol, c->conf); 1120 p->si.upgrade_protocol (protocol, c->conf);
1110 1121
1111 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1122 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)",
1112 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1123 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx);
1113 1124
1114 const sockinfo &dsi = forward_si (p->si); 1125 const sockinfo &dsi = forward_si (p->si);
1115 1126
1116 if (dsi.valid ()) 1127 if (dsi.valid ())
1117 c->send_auth_request (dsi, true); 1128 c->send_auth_request (dsi, true);
1129 }
1118 } 1130 }
1119 1131
1120 break; 1132 break;
1121 1133
1122 default: 1134 default:
1157 send_vpn_packet (p, si); 1169 send_vpn_packet (p, si);
1158 1170
1159 delete p; 1171 delete p;
1160} 1172}
1161 1173
1174void connection::script_init_env (const char *ext)
1175{
1176 char *env;
1177 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1178 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1179 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1180 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1181 conf->id & 0xff); putenv (env);
1182}
1183
1162void connection::script_node () 1184void connection::script_init_connect_env ()
1163{ 1185{
1164 vpn->script_if_up (); 1186 vpn->script_init_env ();
1165 1187
1166 char *env; 1188 char *env;
1167 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1189 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1168 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1190 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1169 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1191 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1170 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1192 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1171} 1193}
1172 1194
1173const char *connection::script_node_up () 1195const char *connection::script_node_up ()
1174{ 1196{
1175 script_node (); 1197 script_init_connect_env ();
1176 1198
1177 putenv ("STATE=up"); 1199 putenv ("STATE=up");
1178 1200
1201 char *filename;
1202 asprintf (&filename,
1203 "%s/%s",
1204 confbase,
1179 return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; 1205 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1206
1207 return filename;
1180} 1208}
1181 1209
1182const char *connection::script_node_down () 1210const char *connection::script_node_down ()
1183{ 1211{
1184 script_node (); 1212 script_init_connect_env ();
1185 1213
1186 putenv ("STATE=down"); 1214 putenv ("STATE=down");
1187 1215
1188 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1216 char *filename;
1189} 1217 asprintf (&filename,
1218 "%s/%s",
1219 confbase,
1220 ::conf.script_node_down ? ::conf.script_node_down : "node-down");
1190 1221
1222 return filename;
1223}
1224
1191connection::connection(struct vpn *vpn_) 1225connection::connection (struct vpn *vpn, conf_node *conf)
1192: vpn(vpn_) 1226: vpn(vpn), conf(conf)
1193, rekey (this, &connection::rekey_cb) 1227, rekey (this, &connection::rekey_cb)
1194, keepalive (this, &connection::keepalive_cb) 1228, keepalive (this, &connection::keepalive_cb)
1195, establish_connection (this, &connection::establish_connection_cb) 1229, establish_connection (this, &connection::establish_connection_cb)
1230#if ENABLE_DNS
1231, dns (0)
1232#endif
1196{ 1233{
1197 octx = ictx = 0; 1234 octx = ictx = 0;
1198 retry_cnt = 0; 1235 retry_cnt = 0;
1199 1236
1237 if (!conf->protocols) // make sure some protocol is enabled
1238 conf->protocols = PROT_UDPv4;
1239
1200 connectmode = conf_node::C_ALWAYS; // initial setting 1240 connectmode = conf_node::C_ALWAYS; // initial setting
1201 reset_connection (); 1241 reset_connection ();
1202} 1242}
1203 1243
1204connection::~connection () 1244connection::~connection ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines