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.89 by root, Thu Dec 2 08:15:09 2010 UTC vs.
Revision 1.95 by root, Tue Mar 8 17:33:30 2011 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2008,2010 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008,2010,2011 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify it 7 GVPE is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
64////////////////////////////////////////////////////////////////////////////// 64//////////////////////////////////////////////////////////////////////////////
65 65
66static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; 66static std::queue< std::pair<run_script_cb *, const char *> > rs_queue;
67static ev::child rs_child_ev; 67static ev::child rs_child_ev;
68 68
69namespace
70{
69void // c++ requires external linkage here, apparently :( 71 void // c++ requires external linkage here, apparently :(
70rs_child_cb (ev::child &w, int revents) 72 rs_child_cb (ev::child &w, int revents)
71{ 73 {
72 w.stop (); 74 w.stop ();
73 75
74 if (rs_queue.empty ()) 76 if (rs_queue.empty ())
75 return; 77 return;
76 78
77 pid_t pid = run_script (*rs_queue.front ().first, false); 79 pid_t pid = run_script (*rs_queue.front ().first, false);
78 if (pid) 80 if (pid)
79 { 81 {
80 w.set (pid); 82 w.set (pid);
81 w.start (); 83 w.start ();
82 } 84 }
83 else 85 else
84 slog (L_WARN, rs_queue.front ().second); 86 slog (L_WARN, rs_queue.front ().second);
85 87
86 delete rs_queue.front ().first; 88 delete rs_queue.front ().first;
87 rs_queue.pop (); 89 rs_queue.pop ();
88} 90 }
91};
89 92
90// despite the fancy name, this is quite a hack 93// despite the fancy name, this is quite a hack
91static void 94static void
92run_script_queued (run_script_cb *cb, const char *warnmsg) 95run_script_queued (run_script_cb *cb, const char *warnmsg)
93{ 96{
129rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) 132rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h)
130{ 133{
131 EVP_MD_CTX ctx; 134 EVP_MD_CTX ctx;
132 135
133 EVP_MD_CTX_init (&ctx); 136 EVP_MD_CTX_init (&ctx);
134 require (EVP_DigestInit (&ctx, RSA_HASH)); 137 require (EVP_DigestInit (&ctx, RSA_HASH));
135 require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); 138 require (EVP_DigestUpdate (&ctx, &chg, sizeof chg));
136 require (EVP_DigestUpdate(&ctx, &id, sizeof id)); 139 require (EVP_DigestUpdate (&ctx, &id, sizeof id));
137 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); 140 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0));
138 EVP_MD_CTX_cleanup (&ctx); 141 EVP_MD_CTX_cleanup (&ctx);
139} 142}
140 143
141struct rsa_entry 144struct rsa_entry
142{ 145{
191 cleaner.set (RSA_TTL, RSA_TTL); 194 cleaner.set (RSA_TTL, RSA_TTL);
192 } 195 }
193 196
194} rsa_cache; 197} rsa_cache;
195 198
199void
196void rsa_cache::cleaner_cb (ev::timer &w, int revents) 200rsa_cache::cleaner_cb (ev::timer &w, int revents)
197{ 201{
198 if (empty ()) 202 if (empty ())
199 w.stop (); 203 w.stop ();
200 else 204 else
201 { 205 {
226 delete p; 230 delete p;
227 231
228 delete [] queue; 232 delete [] queue;
229} 233}
230 234
235void
231void pkt_queue::expire_cb (ev::timer &w, int revents) 236pkt_queue::expire_cb (ev::timer &w, int revents)
232{ 237{
233 ev_tstamp expire = ev_now () - max_ttl; 238 ev_tstamp expire = ev_now () - max_ttl;
234 239
235 for (;;) 240 for (;;)
236 { 241 {
247 252
248 delete get (); 253 delete get ();
249 } 254 }
250} 255}
251 256
257void
252void pkt_queue::put (net_packet *p) 258pkt_queue::put (net_packet *p)
253{ 259{
254 ev_tstamp now = ev_now (); 260 ev_tstamp now = ev_now ();
255 261
256 // start expiry timer 262 // start expiry timer
257 if (empty ()) 263 if (empty ())
266 queue[i].tstamp = now; 272 queue[i].tstamp = now;
267 273
268 i = ni; 274 i = ni;
269} 275}
270 276
271net_packet *pkt_queue::get () 277net_packet *
278pkt_queue::get ()
272{ 279{
273 if (empty ()) 280 if (empty ())
274 return 0; 281 return 0;
275 282
276 net_packet *p = queue[j].pkt; 283 net_packet *p = queue[j].pkt;
300 307
301 bool can (const sockinfo &si) { return can((u32)si.host); } 308 bool can (const sockinfo &si) { return can((u32)si.host); }
302 bool can (u32 host); 309 bool can (u32 host);
303}; 310};
304 311
305net_rate_limiter auth_rate_limiter, reset_rate_limiter; 312static net_rate_limiter auth_rate_limiter, reset_rate_limiter;
306 313
314bool
307bool net_rate_limiter::can (u32 host) 315net_rate_limiter::can (u32 host)
308{ 316{
309 iterator i; 317 iterator i;
310 318
311 for (i = begin (); i != end (); ) 319 for (i = begin (); i != end (); )
312 if (i->host == host) 320 if (i->host == host)
359 367
360///////////////////////////////////////////////////////////////////////////// 368/////////////////////////////////////////////////////////////////////////////
361 369
362unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; 370unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
363 371
372void
364void hmac_packet::hmac_gen (crypto_ctx *ctx) 373hmac_packet::hmac_gen (crypto_ctx *ctx)
365{ 374{
366 unsigned int xlen; 375 unsigned int xlen;
367 376
368 HMAC_CTX *hctx = &ctx->hctx; 377 HMAC_CTX *hctx = &ctx->hctx;
369 378
387 hmac_gen (ctx); 396 hmac_gen (ctx);
388 397
389 return !memcmp (hmac, hmac_digest, HMACLENGTH); 398 return !memcmp (hmac, hmac_digest, HMACLENGTH);
390} 399}
391 400
401void
392void vpn_packet::set_hdr (ptype type_, unsigned int dst) 402vpn_packet::set_hdr (ptype type_, unsigned int dst)
393{ 403{
394 type = type_; 404 type = type_;
395 405
396 int src = THISNODE->id; 406 int src = THISNODE->id;
397 407
402 412
403#define MAXVPNDATA (MAX_MTU - 6 - 6) 413#define MAXVPNDATA (MAX_MTU - 6 - 6)
404#define DATAHDR (sizeof (u32) + RAND_SIZE) 414#define DATAHDR (sizeof (u32) + RAND_SIZE)
405 415
406struct vpndata_packet : vpn_packet 416struct vpndata_packet : vpn_packet
417{
418 u8 data[MAXVPNDATA + DATAHDR]; // seqno
419
420 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
421 tap_packet *unpack (connection *conn, u32 &seqno);
422
423private:
424 const u32 data_hdr_size () const
407 { 425 {
408 u8 data[MAXVPNDATA + DATAHDR]; // seqno
409
410 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
411 tap_packet *unpack (connection *conn, u32 &seqno);
412private:
413
414 const u32 data_hdr_size () const
415 {
416 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 426 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR;
417 }
418 }; 427 }
428};
419 429
420void 430void
421vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) 431vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno)
422{ 432{
423 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 433 EVP_CIPHER_CTX *cctx = &conn->octx->cctx;
562#endif 572#endif
563 return f; 573 return f;
564 } 574 }
565}; 575};
566 576
577void
567void config_packet::setup (ptype type, int dst) 578config_packet::setup (ptype type, int dst)
568{ 579{
569 prot_major = PROTOCOL_MAJOR; 580 prot_major = PROTOCOL_MAJOR;
570 prot_minor = PROTOCOL_MINOR; 581 prot_minor = PROTOCOL_MINOR;
571 randsize = RAND_SIZE; 582 randsize = RAND_SIZE;
572 hmaclen = HMACLENGTH; 583 hmaclen = HMACLENGTH;
580 591
581 len = sizeof (*this) - sizeof (net_packet); 592 len = sizeof (*this) - sizeof (net_packet);
582 set_hdr (type, dst); 593 set_hdr (type, dst);
583} 594}
584 595
596bool
585bool config_packet::chk_config () const 597config_packet::chk_config () const
586{ 598{
587 if (prot_major != PROTOCOL_MAJOR) 599 if (prot_major != PROTOCOL_MAJOR)
588 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 600 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
589 else if (randsize != RAND_SIZE) 601 else if (randsize != RAND_SIZE)
590 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 602 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
819} 831}
820 832
821void 833void
822connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 834connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
823{ 835{
824 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename, 836 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename,
825 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi); 837 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi,
838 conf->protocols);
826 839
827 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 840 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
828 841
829 r->hmac_set (octx); 842 r->hmac_set (octx);
830 send_vpn_packet (r, si); 843 send_vpn_packet (r, si);
833} 846}
834 847
835inline void 848inline void
836connection::establish_connection_cb (ev::timer &w, int revents) 849connection::establish_connection_cb (ev::timer &w, int revents)
837{ 850{
838 if (!ictx 851 if (!(ictx && octx)
839 && conf != THISNODE 852 && conf != THISNODE
840 && connectmode != conf_node::C_NEVER 853 && connectmode != conf_node::C_NEVER
841 && connectmode != conf_node::C_DISABLED 854 && connectmode != conf_node::C_DISABLED
842 && !w.is_active ()) 855 && !w.is_active ())
843 { 856 {
911 } 924 }
912 } 925 }
913 926
914 delete ictx; ictx = 0; 927 delete ictx; ictx = 0;
915 delete octx; octx = 0; 928 delete octx; octx = 0;
916#if ENABLE_DNS
917 dnsv4_reset_connection ();
918#endif
919 929
920 si.host = 0; 930 si.host = 0;
921 931
922 last_activity = 0.; 932 last_activity = 0.;
923 //last_si_change = 0.; 933 //last_si_change = 0.;
984 data_queue.put (new tap_packet (*pkt)); 994 data_queue.put (new tap_packet (*pkt));
985 post_inject_queue (); 995 post_inject_queue ();
986 } 996 }
987} 997}
988 998
999void
989void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 1000connection::inject_vpn_packet (vpn_packet *pkt, int tos)
990{ 1001{
991 if (ictx && octx) 1002 if (ictx && octx)
992 send_vpn_packet (pkt, si, tos); 1003 send_vpn_packet (pkt, si, tos);
993 else 1004 else
994 { 1005 {
1000void 1011void
1001connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1012connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
1002{ 1013{
1003 last_activity = ev_now (); 1014 last_activity = ev_now ();
1004 1015
1005 slog (L_NOISE, "%s >> received packet type %d from %d to %d.", 1016 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1006 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); 1017 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1007 1018
1008 if (connectmode == conf_node::C_DISABLED) 1019 if (connectmode == conf_node::C_DISABLED)
1009 return; 1020 return;
1010 1021
1054 break; 1065 break;
1055 1066
1056 case vpn_packet::PT_AUTH_REQ: 1067 case vpn_packet::PT_AUTH_REQ:
1057 if (auth_rate_limiter.can (rsi)) 1068 if (auth_rate_limiter.can (rsi))
1058 { 1069 {
1059 auth_req_packet *p = (auth_req_packet *) pkt; 1070 auth_req_packet *p = (auth_req_packet *)pkt;
1060 1071
1061 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); 1072 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1073 conf->nodename, p->initiate ? "initiate" : "reply",
1074 p->protocols, p->features);
1062 1075
1063 if (p->chk_config () 1076 if (p->chk_config ()
1064 && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8))) 1077 && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8)))
1065 { 1078 {
1066 if (p->prot_minor != PROTOCOL_MINOR) 1079 if (p->prot_minor != PROTOCOL_MINOR)
1067 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1080 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1068 conf->nodename, (const char *)rsi, 1081 conf->nodename, (const char *)rsi,
1069 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1082 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1261 if (p->id > 0 && p->id <= vpn->conns.size ()) 1274 if (p->id > 0 && p->id <= vpn->conns.size ())
1262 { 1275 {
1263 connection *c = vpn->conns[p->id - 1]; 1276 connection *c = vpn->conns[p->id - 1];
1264 conf->protocols = p->protocols; 1277 conf->protocols = p->protocols;
1265 1278
1266 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]", 1279 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]",
1267 conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); 1280 conf->nodename, vpn->conns[p->id - 1]->conf->nodename,
1281 p->protocols,
1282 c->ictx && c->octx);
1268 1283
1269 if (c->ictx && c->octx) 1284 if (c->ictx && c->octx)
1270 { 1285 {
1271 // send connect_info packets to both sides, in case one is 1286 // send connect_info packets to both sides, in case one is
1272 // behind a nat firewall (or both ;) 1287 // behind a nat firewall (or both ;)
1295 1310
1296 c->conf->protocols = p->protocols; 1311 c->conf->protocols = p->protocols;
1297 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1312 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1298 p->si.upgrade_protocol (protocol, c->conf); 1313 p->si.upgrade_protocol (protocol, c->conf);
1299 1314
1300 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]", 1315 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,p%02x) [%d]",
1316 conf->nodename,
1301 conf->nodename, vpn->conns[p->id - 1]->conf->nodename, 1317 vpn->conns[p->id - 1]->conf->nodename,
1318 (const char *)p->si,
1319 p->protocols,
1302 (const char *)p->si, !c->ictx && !c->octx); 1320 !c->ictx && !c->octx);
1303 1321
1304 const sockinfo &dsi = forward_si (p->si); 1322 const sockinfo &dsi = forward_si (p->si);
1305 1323
1306 if (dsi.valid ()) 1324 if (dsi.valid ())
1307 c->send_auth_request (dsi, true); 1325 c->send_auth_request (dsi, true);
1342 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1360 w.start (last_activity + ::conf.keepalive + 10 - ev::now ());
1343 else 1361 else
1344 reset_connection (); 1362 reset_connection ();
1345} 1363}
1346 1364
1365void
1347void connection::send_connect_request (int id) 1366connection::send_connect_request (int id)
1348{ 1367{
1349 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1368 connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols);
1350 1369
1351 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", 1370 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)",
1352 conf->nodename, vpn->conns[id - 1]->conf->nodename); 1371 conf->nodename, vpn->conns[id - 1]->conf->nodename,
1372 THISNODE->protocols);
1353 p->hmac_set (octx); 1373 p->hmac_set (octx);
1354 send_vpn_packet (p, si); 1374 send_vpn_packet (p, si);
1355 1375
1356 delete p; 1376 delete p;
1357} 1377}
1358 1378
1379void
1359void connection::script_init_env (const char *ext) 1380connection::script_init_env (const char *ext)
1360{ 1381{
1361 char *env; 1382 char *env;
1362 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1383 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1363 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1384 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1364 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1385 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1365 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1386 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1366 conf->id & 0xff); putenv (env); 1387 conf->id & 0xff); putenv (env);
1367} 1388}
1368 1389
1390void
1369void connection::script_init_connect_env () 1391connection::script_init_connect_env ()
1370{ 1392{
1371 vpn->script_init_env (); 1393 vpn->script_init_env ();
1372 1394
1373 char *env; 1395 char *env;
1374 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1396 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1439 establish_connection.set<connection, &connection::establish_connection_cb> (this); 1461 establish_connection.set<connection, &connection::establish_connection_cb> (this);
1440 1462
1441 last_establish_attempt = 0.; 1463 last_establish_attempt = 0.;
1442 octx = ictx = 0; 1464 octx = ictx = 0;
1443 1465
1444 if (!conf->protocols) // make sure some protocol is enabled
1445 conf->protocols = PROT_UDPv4;
1446
1447 connectmode = conf->connectmode; 1466 connectmode = conf->connectmode;
1448 1467
1449 // queue a dummy packet to force an initial connection attempt 1468 // queue a dummy packet to force an initial connection attempt
1450 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1469 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1451 vpn_queue.put (new net_packet); 1470 vpn_queue.put (new net_packet);
1456connection::~connection () 1475connection::~connection ()
1457{ 1476{
1458 shutdown (); 1477 shutdown ();
1459} 1478}
1460 1479
1480void
1461void connection_init () 1481connection_init ()
1462{ 1482{
1463 auth_rate_limiter.clear (); 1483 auth_rate_limiter.clear ();
1464 reset_rate_limiter.clear (); 1484 reset_rate_limiter.clear ();
1465} 1485}
1466 1486

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines