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.49 by pcg, Sat Mar 12 18:10:40 2005 UTC vs.
Revision 1.59 by pcg, Mon Dec 5 12:58:09 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{
467#if ENABLE_COMPRESSION 463#if ENABLE_COMPRESSION
468 f |= FEATURE_COMPRESSION; 464 f |= FEATURE_COMPRESSION;
469#endif 465#endif
470#if ENABLE_ROHC 466#if ENABLE_ROHC
471 f |= FEATURE_ROHC; 467 f |= FEATURE_ROHC;
468#endif
469#if ENABLE_BRIDGING
470 f |= FEATURE_BRIDGING;
472#endif 471#endif
473 return f; 472 return f;
474 } 473 }
475}; 474};
476 475
627 // mask out protocols we cannot establish 626 // mask out protocols we cannot establish
628 if (!conf->udp_port) protocol &= ~PROT_UDPv4; 627 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
629 if (!conf->tcp_port) protocol &= ~PROT_TCPv4; 628 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
630 if (!conf->dns_port) protocol &= ~PROT_DNSv4; 629 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
631 630
631 if (protocol
632 && (!conf->can_direct (THISNODE)
633 || !THISNODE->can_direct (conf)))
634 {
635 slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename);
636 protocol = 0;
637 }
638
632 si.set (conf, protocol); 639 si.set (conf, protocol);
633} 640}
634 641
635// ensure sockinfo is valid, forward if necessary 642// ensure sockinfo is valid, forward if necessary
636const sockinfo & 643const sockinfo &
721} 728}
722 729
723void 730void
724connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 731connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
725{ 732{
726 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", 733 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)",
727 conf->id, rid, (const char *)rsi); 734 conf->id, rid, (const char *)rsi);
728 735
729 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 736 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
730 737
731 r->hmac_set (octx); 738 r->hmac_set (octx);
788 { 795 {
789 slog (L_INFO, _("%s(%s): connection lost"), 796 slog (L_INFO, _("%s(%s): connection lost"),
790 conf->nodename, (const char *)si); 797 conf->nodename, (const char *)si);
791 798
792 if (::conf.script_node_down) 799 if (::conf.script_node_down)
793 run_script (run_script_cb (this, &connection::script_node_down), false); 800 if (!run_script (run_script_cb (this, &connection::script_node_down), false))
801 slog (L_WARN, _("node-down command execution failed, continuing."));
794 } 802 }
795 803
796 delete ictx; ictx = 0; 804 delete ictx; ictx = 0;
797 delete octx; octx = 0; 805 delete octx; octx = 0;
798#if ENABLE_DNS 806#if ENABLE_DNS
849{ 857{
850 if (ictx && octx) 858 if (ictx && octx)
851 send_data_packet (pkt); 859 send_data_packet (pkt);
852 else 860 else
853 { 861 {
854 if (!broadcast)//DDDD 862 if (!broadcast)
855 data_queue.put (new tap_packet (*pkt)); 863 data_queue.put (new tap_packet (*pkt));
856 864
857 establish_connection (); 865 establish_connection ();
858 } 866 }
859} 867}
1019 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1027 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1020 conf->nodename, (const char *)rsi, 1028 conf->nodename, (const char *)rsi,
1021 p->prot_major, p->prot_minor); 1029 p->prot_major, p->prot_minor);
1022 1030
1023 if (::conf.script_node_up) 1031 if (::conf.script_node_up)
1024 run_script (run_script_cb (this, &connection::script_node_up), false); 1032 if (!run_script (run_script_cb (this, &connection::script_node_up), false))
1033 slog (L_WARN, _("node-up command execution failed, continuing."));
1025 1034
1026 break; 1035 break;
1027 } 1036 }
1028 else 1037 else
1029 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1038 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1063 { 1072 {
1064 vpn->tap->send (d); 1073 vpn->tap->send (d);
1065 1074
1066 if (si != rsi) 1075 if (si != rsi)
1067 { 1076 {
1068 // fast re-sync on connection changes, useful especially for tcp/ip 1077 // fast re-sync on source address changes, useful especially for tcp/ip
1069 si = rsi; 1078 si = rsi;
1070 1079
1071 slog (L_INFO, _("%s(%s): socket address changed to %s"), 1080 slog (L_INFO, _("%s(%s): socket address changed to %s"),
1072 conf->nodename, (const char *)si, (const char *)rsi); 1081 conf->nodename, (const char *)si, (const char *)rsi);
1073 } 1082 }
1084 case vpn_packet::PT_CONNECT_REQ: 1093 case vpn_packet::PT_CONNECT_REQ:
1085 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1094 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1086 { 1095 {
1087 connect_req_packet *p = (connect_req_packet *) pkt; 1096 connect_req_packet *p = (connect_req_packet *) pkt;
1088 1097
1089 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1098 if (p->id > 0 && p->id <= vpn->conns.size ())
1090 connection *c = vpn->conns[p->id - 1];
1091 conf->protocols = p->protocols;
1092
1093 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n",
1094 conf->id, p->id, c->ictx && c->octx);
1095
1096 if (c->ictx && c->octx)
1097 { 1099 {
1100 connection *c = vpn->conns[p->id - 1];
1101 conf->protocols = p->protocols;
1102
1103 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]",
1104 conf->id, p->id, c->ictx && c->octx);
1105
1106 if (c->ictx && c->octx)
1107 {
1098 // send connect_info packets to both sides, in case one is 1108 // send connect_info packets to both sides, in case one is
1099 // behind a nat firewall (or both ;) 1109 // behind a nat firewall (or both ;)
1100 c->send_connect_info (conf->id, si, conf->protocols); 1110 c->send_connect_info (conf->id, si, conf->protocols);
1101 send_connect_info (c->conf->id, c->si, c->conf->protocols); 1111 send_connect_info (c->conf->id, c->si, c->conf->protocols);
1112 }
1113 else
1114 c->establish_connection ();
1102 } 1115 }
1103 else 1116 else
1104 c->establish_connection (); 1117 slog (L_WARN,
1118 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1119 p->id);
1105 } 1120 }
1106 1121
1107 break; 1122 break;
1108 1123
1109 case vpn_packet::PT_CONNECT_INFO: 1124 case vpn_packet::PT_CONNECT_INFO:
1110 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1125 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1111 { 1126 {
1112 connect_info_packet *p = (connect_info_packet *)pkt; 1127 connect_info_packet *p = (connect_info_packet *)pkt;
1113 1128
1114 if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything 1129 if (p->id > 0 && p->id <= vpn->conns.size ())
1115 { 1130 {
1116 connection *c = vpn->conns[p->id - 1]; 1131 connection *c = vpn->conns[p->id - 1];
1117 1132
1118 c->conf->protocols = p->protocols; 1133 c->conf->protocols = p->protocols;
1119 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1134 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1125 const sockinfo &dsi = forward_si (p->si); 1140 const sockinfo &dsi = forward_si (p->si);
1126 1141
1127 if (dsi.valid ()) 1142 if (dsi.valid ())
1128 c->send_auth_request (dsi, true); 1143 c->send_auth_request (dsi, true);
1129 } 1144 }
1145 else
1146 slog (L_WARN,
1147 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1148 p->id);
1130 } 1149 }
1131 1150
1132 break; 1151 break;
1133 1152
1134 default: 1153 default:
1169 send_vpn_packet (p, si); 1188 send_vpn_packet (p, si);
1170 1189
1171 delete p; 1190 delete p;
1172} 1191}
1173 1192
1193void connection::script_init_env (const char *ext)
1194{
1195 char *env;
1196 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1197 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1198 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1199 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1200 conf->id & 0xff); putenv (env);
1201}
1202
1174void connection::script_node () 1203void connection::script_init_connect_env ()
1175{ 1204{
1176 vpn->script_if_up (); 1205 vpn->script_init_env ();
1177 1206
1178 char *env; 1207 char *env;
1179 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1208 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1180 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1209 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1181 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1210 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1182 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1211 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1183} 1212}
1184 1213
1185const char *connection::script_node_up () 1214const char *connection::script_node_up ()
1186{ 1215{
1187 script_node (); 1216 script_init_connect_env ();
1188 1217
1189 putenv ("STATE=up"); 1218 putenv ("STATE=up");
1190 1219
1220 char *filename;
1221 asprintf (&filename,
1222 "%s/%s",
1223 confbase,
1191 return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; 1224 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1225
1226 return filename;
1192} 1227}
1193 1228
1194const char *connection::script_node_down () 1229const char *connection::script_node_down ()
1195{ 1230{
1196 script_node (); 1231 script_init_connect_env ();
1197 1232
1198 putenv ("STATE=down"); 1233 putenv ("STATE=down");
1199 1234
1200 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1235 char *filename;
1236 asprintf (&filename,
1237 "%s/%s",
1238 confbase,
1239 ::conf.script_node_down ? ::conf.script_node_down : "node-down");
1240
1241 return filename;
1201} 1242}
1202 1243
1203connection::connection (struct vpn *vpn, conf_node *conf) 1244connection::connection (struct vpn *vpn, conf_node *conf)
1204: vpn(vpn), conf(conf) 1245: vpn(vpn), conf(conf)
1205, rekey (this, &connection::rekey_cb) 1246, rekey (this, &connection::rekey_cb)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines