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.75 by pcg, Sun Aug 10 15:04:55 2008 UTC vs.
Revision 1.85 by pcg, Tue Jul 28 00:42:14 2009 UTC

50 50
51#if !HAVE_RAND_PSEUDO_BYTES 51#if !HAVE_RAND_PSEUDO_BYTES
52# define RAND_pseudo_bytes RAND_bytes 52# define RAND_pseudo_bytes RAND_bytes
53#endif 53#endif
54 54
55#define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic 55#define MAGIC_OLD "vped\xbd\xc6\xdb\x82" // 8 bytes of magic (still used in the protocol)
56#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic (understood but not generated)
56 57
57#define ULTRA_FAST 1 58#define ULTRA_FAST 1
58#define HLOG 15 59#define HLOG 15
59#include "lzf/lzf.h" 60#include "lzf/lzf.h"
60#include "lzf/lzf_c.c" 61#include "lzf/lzf_c.c"
613 rsaencrdata encr; 614 rsaencrdata encr;
614 615
615 auth_req_packet (int dst, bool initiate_, u8 protocols_) 616 auth_req_packet (int dst, bool initiate_, u8 protocols_)
616 { 617 {
617 config_packet::setup (PT_AUTH_REQ, dst); 618 config_packet::setup (PT_AUTH_REQ, dst);
618 strncpy (magic, MAGIC, 8); 619 strncpy (magic, MAGIC_OLD, 8);
619 initiate = !!initiate_; 620 initiate = !!initiate_;
620 protocols = protocols_; 621 protocols = protocols_;
621 622
622 len = sizeof (*this) - sizeof (net_packet); 623 len = sizeof (*this) - sizeof (net_packet);
623 } 624 }
672///////////////////////////////////////////////////////////////////////////// 673/////////////////////////////////////////////////////////////////////////////
673 674
674void 675void
675connection::connection_established () 676connection::connection_established ()
676{ 677{
677 slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); 678 slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx);
678 679
679 if (ictx && octx) 680 if (ictx && octx)
680 { 681 {
681 // make sure rekeying timeouts are slightly asymmetric 682 // make sure rekeying timeouts are slightly asymmetric
682 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 683 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
760{ 761{
761 ping_packet *pkt = new ping_packet; 762 ping_packet *pkt = new ping_packet;
762 763
763 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); 764 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING);
764 765
765 slog (L_TRACE, ">>%d %s [%s]", conf->id, pong ? "PT_PONG" : "PT_PING", (const char *)si); 766 slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si);
766 767
767 send_vpn_packet (pkt, si, IPTOS_LOWDELAY); 768 send_vpn_packet (pkt, si, IPTOS_LOWDELAY);
768 769
769 delete pkt; 770 delete pkt;
770} 771}
790 791
791 rsachallenge chg; 792 rsachallenge chg;
792 rsa_cache.gen (pkt->id, chg); 793 rsa_cache.gen (pkt->id, chg);
793 rsa_encrypt (conf->rsa_key, chg, pkt->encr); 794 rsa_encrypt (conf->rsa_key, chg, pkt->encr);
794 795
795 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); 796 slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si);
796 797
797 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly 798 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly
798 799
799 delete pkt; 800 delete pkt;
800} 801}
808 809
809 rsa_hash (id, chg, pkt->response); 810 rsa_hash (id, chg, pkt->response);
810 811
811 pkt->hmac_set (octx); 812 pkt->hmac_set (octx);
812 813
813 slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); 814 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si);
814 815
815 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 816 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
816 817
817 delete pkt; 818 delete pkt;
818} 819}
819 820
820void 821void
821connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 822connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
822{ 823{
823 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", 824 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename,
824 conf->id, rid, (const char *)rsi); 825 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi);
825 826
826 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 827 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
827 828
828 r->hmac_set (octx); 829 r->hmac_set (octx);
829 send_vpn_packet (r, si); 830 send_vpn_packet (r, si);
999void 1000void
1000connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1001connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
1001{ 1002{
1002 last_activity = ev_now (); 1003 last_activity = ev_now ();
1003 1004
1004 slog (L_NOISE, "<<%d received packet type %d from %d to %d.", 1005 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1005 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 1006 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1006 1007
1007 if (connectmode == conf_node::C_DISABLED) 1008 if (connectmode == conf_node::C_DISABLED)
1008 return; 1009 return;
1009 1010
1010 switch (pkt->typ ()) 1011 switch (pkt->typ ())
1011 { 1012 {
1012 case vpn_packet::PT_PING: 1013 case vpn_packet::PT_PING:
1014 slog (L_TRACE, "%s >> PT_PING", conf->nodename);
1015
1013 // we send pings instead of auth packets after some retries, 1016 // we send pings instead of auth packets after some retries,
1014 // so reset the retry counter and establish a connection 1017 // so reset the retry counter and establish a connection
1015 // when we receive a ping. 1018 // when we receive a ping.
1016 if (!ictx) 1019 if (!ictx)
1017 { 1020 {
1024 send_ping (rsi, 1); // pong 1027 send_ping (rsi, 1); // pong
1025 1028
1026 break; 1029 break;
1027 1030
1028 case vpn_packet::PT_PONG: 1031 case vpn_packet::PT_PONG:
1032 slog (L_TRACE, "%s >> PT_PONG", conf->nodename);
1029 break; 1033 break;
1030 1034
1031 case vpn_packet::PT_RESET: 1035 case vpn_packet::PT_RESET:
1032 { 1036 {
1033 reset_connection (); 1037 reset_connection ();
1048 case vpn_packet::PT_AUTH_REQ: 1052 case vpn_packet::PT_AUTH_REQ:
1049 if (auth_rate_limiter.can (rsi)) 1053 if (auth_rate_limiter.can (rsi))
1050 { 1054 {
1051 auth_req_packet *p = (auth_req_packet *) pkt; 1055 auth_req_packet *p = (auth_req_packet *) pkt;
1052 1056
1053 slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); 1057 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply");
1054 1058
1055 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 1059 if (p->chk_config ()
1060 && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8)))
1056 { 1061 {
1057 if (p->prot_minor != PROTOCOL_MINOR) 1062 if (p->prot_minor != PROTOCOL_MINOR)
1058 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1063 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1059 conf->nodename, (const char *)rsi, 1064 conf->nodename, (const char *)rsi,
1060 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1065 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1096 1101
1097 break; 1102 break;
1098 1103
1099 case vpn_packet::PT_AUTH_RES: 1104 case vpn_packet::PT_AUTH_RES:
1100 { 1105 {
1101 auth_res_packet *p = (auth_res_packet *) pkt; 1106 auth_res_packet *p = (auth_res_packet *)pkt;
1102 1107
1103 slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); 1108 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1104 1109
1105 if (p->chk_config ()) 1110 if (p->chk_config ())
1106 { 1111 {
1107 if (p->prot_minor != PROTOCOL_MINOR) 1112 if (p->prot_minor != PROTOCOL_MINOR)
1108 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1113 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1143 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid 1148 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid
1144 1149
1145 si = rsi; 1150 si = rsi;
1146 protocol = rsi.prot; 1151 protocol = rsi.prot;
1147 1152
1148 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d."), 1153 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
1149 conf->nodename, (const char *)rsi, 1154 conf->nodename, (const char *)rsi,
1155 is_direct ? "direct" : "forwarded",
1150 p->prot_major, p->prot_minor); 1156 p->prot_major, p->prot_minor);
1151 1157
1152 connection_established (); 1158 connection_established ();
1153 1159
1154 if (::conf.script_node_up) 1160 if (::conf.script_node_up)
1202 if (si != rsi) 1208 if (si != rsi)
1203 { 1209 {
1204 // fast re-sync on source address changes, useful especially for tcp/ip 1210 // fast re-sync on source address changes, useful especially for tcp/ip
1205 //if (last_si_change < ev_now () + 5.) 1211 //if (last_si_change < ev_now () + 5.)
1206 // { 1212 // {
1213 slog (L_INFO, _("%s(%s): changing socket address to %s."),
1214 conf->nodename, (const char *)si, (const char *)rsi);
1215
1207 si = rsi; 1216 si = rsi;
1208 1217
1209 slog (L_INFO, _("%s(%s): socket address changed to %s."), 1218 if (::conf.script_node_change)
1210 conf->nodename, (const char *)si, (const char *)rsi); 1219 {
1220 run_script_cb *cb = new run_script_cb;
1221 cb->set<connection, &connection::script_node_change> (this);
1222 run_script_queued (cb, _("node-change command execution failed, continuing."));
1223 }
1224
1211 // } 1225 // }
1212 //else 1226 //else
1213 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), 1227 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."),
1214 // conf->nodename, (const char *)si, (const char *)rsi); 1228 // conf->nodename, (const char *)si, (const char *)rsi);
1215 } 1229 }
1236 break; 1250 break;
1237 1251
1238 case vpn_packet::PT_CONNECT_REQ: 1252 case vpn_packet::PT_CONNECT_REQ:
1239 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1253 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1240 { 1254 {
1241 connect_req_packet *p = (connect_req_packet *) pkt; 1255 connect_req_packet *p = (connect_req_packet *)pkt;
1242 1256
1243 if (p->id > 0 && p->id <= vpn->conns.size ()) 1257 if (p->id > 0 && p->id <= vpn->conns.size ())
1244 { 1258 {
1245 connection *c = vpn->conns[p->id - 1]; 1259 connection *c = vpn->conns[p->id - 1];
1246 conf->protocols = p->protocols; 1260 conf->protocols = p->protocols;
1247 1261
1248 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", 1262 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]",
1249 conf->id, p->id, c->ictx && c->octx); 1263 conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx);
1250 1264
1251 if (c->ictx && c->octx) 1265 if (c->ictx && c->octx)
1252 { 1266 {
1253 // send connect_info packets to both sides, in case one is 1267 // send connect_info packets to both sides, in case one is
1254 // behind a nat firewall (or both ;) 1268 // behind a nat firewall (or both ;)
1277 1291
1278 c->conf->protocols = p->protocols; 1292 c->conf->protocols = p->protocols;
1279 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1293 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1280 p->si.upgrade_protocol (protocol, c->conf); 1294 p->si.upgrade_protocol (protocol, c->conf);
1281 1295
1282 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1296 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]",
1297 conf->nodename, vpn->conns[p->id - 1]->conf->nodename,
1283 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1298 (const char *)p->si, !c->ictx && !c->octx);
1284 1299
1285 const sockinfo &dsi = forward_si (p->si); 1300 const sockinfo &dsi = forward_si (p->si);
1286 1301
1287 if (dsi.valid ()) 1302 if (dsi.valid ())
1288 c->send_auth_request (dsi, true); 1303 c->send_auth_request (dsi, true);
1302} 1317}
1303 1318
1304inline void 1319inline void
1305connection::keepalive_cb (ev::timer &w, int revents) 1320connection::keepalive_cb (ev::timer &w, int revents)
1306{ 1321{
1307 if (ev_now () >= last_activity + ::conf.keepalive + 30) 1322 if (ev_now () >= last_activity + ::conf.keepalive + 15)
1308 { 1323 {
1309 reset_connection (); 1324 reset_connection ();
1310 establish_connection (); 1325 establish_connection ();
1311 } 1326 }
1312 else if (ev_now () < last_activity + ::conf.keepalive) 1327 else if (ev_now () < last_activity + ::conf.keepalive)
1313 w.start (last_activity + ::conf.keepalive - ev::now ()); 1328 w.start (last_activity + ::conf.keepalive - ev::now ());
1314 else if (conf->connectmode != conf_node::C_ONDEMAND 1329 else if (conf->connectmode != conf_node::C_ONDEMAND
1315 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1330 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1316 { 1331 {
1317 send_ping (si); 1332 send_ping (si);
1318 w.start (5); 1333 w.start (3);
1319 } 1334 }
1320 else if (ev_now () < last_activity + ::conf.keepalive + 10) 1335 else if (ev_now () < last_activity + ::conf.keepalive + 10)
1321 // hold ondemand connections implicitly a few seconds longer 1336 // hold ondemand connections implicitly a few seconds longer
1322 // should delete octx, though, or something like that ;) 1337 // should delete octx, though, or something like that ;)
1323 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1338 w.start (last_activity + ::conf.keepalive + 10 - ev::now ());
1327 1342
1328void connection::send_connect_request (int id) 1343void connection::send_connect_request (int id)
1329{ 1344{
1330 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1345 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols);
1331 1346
1332 slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); 1347 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)",
1348 conf->nodename, vpn->conns[id - 1]->conf->nodename);
1333 p->hmac_set (octx); 1349 p->hmac_set (octx);
1334 send_vpn_packet (p, si); 1350 send_vpn_packet (p, si);
1335 1351
1336 delete p; 1352 delete p;
1337} 1353}
1338 1354
1339void connection::script_init_env (const char *ext) 1355void connection::script_init_env (const char *ext)
1340{ 1356{
1341 char *env; 1357 char *env;
1342 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1358 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1343 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1359 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1344 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1360 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1345 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1361 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1346 conf->id & 0xff); putenv (env); 1362 conf->id & 0xff); putenv (env);
1347} 1363}
1348 1364
1349void connection::script_init_connect_env () 1365void connection::script_init_connect_env ()
1350{ 1366{
1351 vpn->script_init_env (); 1367 vpn->script_init_env ();
1352 1368
1353 char *env; 1369 char *env;
1354 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1370 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1371 asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env);
1355 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1372 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1356 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1373 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1357 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1374 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1358} 1375}
1359 1376
1360inline const char * 1377inline const char *
1361connection::script_node_up () 1378connection::script_node_up ()
1362{ 1379{
1367 char *filename; 1384 char *filename;
1368 asprintf (&filename, 1385 asprintf (&filename,
1369 "%s/%s", 1386 "%s/%s",
1370 confbase, 1387 confbase,
1371 ::conf.script_node_up ? ::conf.script_node_up : "node-up"); 1388 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1389
1390 return filename;
1391}
1392
1393inline const char *
1394connection::script_node_change ()
1395{
1396 script_init_connect_env ();
1397
1398 putenv ((char *)"STATE=change");
1399
1400 char *filename;
1401 asprintf (&filename,
1402 "%s/%s",
1403 confbase,
1404 ::conf.script_node_change ? ::conf.script_node_change : "node-change");
1372 1405
1373 return filename; 1406 return filename;
1374} 1407}
1375 1408
1376inline const char * 1409inline const char *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines