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.84 by pcg, Sat Jul 18 05:59:16 2009 UTC vs.
Revision 1.91 by root, Tue Feb 8 23:13:48 2011 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008,2010 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
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"
63////////////////////////////////////////////////////////////////////////////// 64//////////////////////////////////////////////////////////////////////////////
64 65
65static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; 66static std::queue< std::pair<run_script_cb *, const char *> > rs_queue;
66static ev::child rs_child_ev; 67static ev::child rs_child_ev;
67 68
69namespace
70{
68void // c++ requires external linkage here, apparently :( 71 void // c++ requires external linkage here, apparently :(
69rs_child_cb (ev::child &w, int revents) 72 rs_child_cb (ev::child &w, int revents)
70{ 73 {
71 w.stop (); 74 w.stop ();
72 75
73 if (rs_queue.empty ()) 76 if (rs_queue.empty ())
74 return; 77 return;
75 78
76 pid_t pid = run_script (*rs_queue.front ().first, false); 79 pid_t pid = run_script (*rs_queue.front ().first, false);
77 if (pid) 80 if (pid)
78 { 81 {
79 w.set (pid); 82 w.set (pid);
80 w.start (); 83 w.start ();
81 } 84 }
82 else 85 else
83 slog (L_WARN, rs_queue.front ().second); 86 slog (L_WARN, rs_queue.front ().second);
84 87
85 delete rs_queue.front ().first; 88 delete rs_queue.front ().first;
86 rs_queue.pop (); 89 rs_queue.pop ();
87} 90 }
91};
88 92
89// despite the fancy name, this is quite a hack 93// despite the fancy name, this is quite a hack
90static void 94static void
91run_script_queued (run_script_cb *cb, const char *warnmsg) 95run_script_queued (run_script_cb *cb, const char *warnmsg)
92{ 96{
190 cleaner.set (RSA_TTL, RSA_TTL); 194 cleaner.set (RSA_TTL, RSA_TTL);
191 } 195 }
192 196
193} rsa_cache; 197} rsa_cache;
194 198
199void
195void rsa_cache::cleaner_cb (ev::timer &w, int revents) 200rsa_cache::cleaner_cb (ev::timer &w, int revents)
196{ 201{
197 if (empty ()) 202 if (empty ())
198 w.stop (); 203 w.stop ();
199 else 204 else
200 { 205 {
225 delete p; 230 delete p;
226 231
227 delete [] queue; 232 delete [] queue;
228} 233}
229 234
235void
230void pkt_queue::expire_cb (ev::timer &w, int revents) 236pkt_queue::expire_cb (ev::timer &w, int revents)
231{ 237{
232 ev_tstamp expire = ev_now () - max_ttl; 238 ev_tstamp expire = ev_now () - max_ttl;
233 239
234 for (;;) 240 for (;;)
235 { 241 {
246 252
247 delete get (); 253 delete get ();
248 } 254 }
249} 255}
250 256
257void
251void pkt_queue::put (net_packet *p) 258pkt_queue::put (net_packet *p)
252{ 259{
253 ev_tstamp now = ev_now (); 260 ev_tstamp now = ev_now ();
254 261
255 // start expiry timer 262 // start expiry timer
256 if (empty ()) 263 if (empty ())
265 queue[i].tstamp = now; 272 queue[i].tstamp = now;
266 273
267 i = ni; 274 i = ni;
268} 275}
269 276
270net_packet *pkt_queue::get () 277net_packet *
278pkt_queue::get ()
271{ 279{
272 if (empty ()) 280 if (empty ())
273 return 0; 281 return 0;
274 282
275 net_packet *p = queue[j].pkt; 283 net_packet *p = queue[j].pkt;
299 307
300 bool can (const sockinfo &si) { return can((u32)si.host); } 308 bool can (const sockinfo &si) { return can((u32)si.host); }
301 bool can (u32 host); 309 bool can (u32 host);
302}; 310};
303 311
304net_rate_limiter auth_rate_limiter, reset_rate_limiter; 312static net_rate_limiter auth_rate_limiter, reset_rate_limiter;
305 313
314bool
306bool net_rate_limiter::can (u32 host) 315net_rate_limiter::can (u32 host)
307{ 316{
308 iterator i; 317 iterator i;
309 318
310 for (i = begin (); i != end (); ) 319 for (i = begin (); i != end (); )
311 if (i->host == host) 320 if (i->host == host)
358 367
359///////////////////////////////////////////////////////////////////////////// 368/////////////////////////////////////////////////////////////////////////////
360 369
361unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; 370unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
362 371
372void
363void hmac_packet::hmac_gen (crypto_ctx *ctx) 373hmac_packet::hmac_gen (crypto_ctx *ctx)
364{ 374{
365 unsigned int xlen; 375 unsigned int xlen;
366 376
367 HMAC_CTX *hctx = &ctx->hctx; 377 HMAC_CTX *hctx = &ctx->hctx;
368 378
386 hmac_gen (ctx); 396 hmac_gen (ctx);
387 397
388 return !memcmp (hmac, hmac_digest, HMACLENGTH); 398 return !memcmp (hmac, hmac_digest, HMACLENGTH);
389} 399}
390 400
401void
391void vpn_packet::set_hdr (ptype type_, unsigned int dst) 402vpn_packet::set_hdr (ptype type_, unsigned int dst)
392{ 403{
393 type = type_; 404 type = type_;
394 405
395 int src = THISNODE->id; 406 int src = THISNODE->id;
396 407
401 412
402#define MAXVPNDATA (MAX_MTU - 6 - 6) 413#define MAXVPNDATA (MAX_MTU - 6 - 6)
403#define DATAHDR (sizeof (u32) + RAND_SIZE) 414#define DATAHDR (sizeof (u32) + RAND_SIZE)
404 415
405struct 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
406 { 425 {
407 u8 data[MAXVPNDATA + DATAHDR]; // seqno
408
409 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
410 tap_packet *unpack (connection *conn, u32 &seqno);
411private:
412
413 const u32 data_hdr_size () const
414 {
415 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 426 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR;
416 }
417 }; 427 }
428};
418 429
419void 430void
420vpndata_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)
421{ 432{
422 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 433 EVP_CIPHER_CTX *cctx = &conn->octx->cctx;
424 ptype type = PT_DATA_UNCOMPRESSED; 435 ptype type = PT_DATA_UNCOMPRESSED;
425 436
426#if ENABLE_COMPRESSION 437#if ENABLE_COMPRESSION
427 u8 cdata[MAX_MTU]; 438 u8 cdata[MAX_MTU];
428 439
429 if (conn->features & ENABLE_COMPRESSION) 440 if (conn->features & FEATURE_COMPRESSION)
430 { 441 {
431 u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); 442 u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7);
432 443
433 if (cl) 444 if (cl)
434 { 445 {
561#endif 572#endif
562 return f; 573 return f;
563 } 574 }
564}; 575};
565 576
577void
566void config_packet::setup (ptype type, int dst) 578config_packet::setup (ptype type, int dst)
567{ 579{
568 prot_major = PROTOCOL_MAJOR; 580 prot_major = PROTOCOL_MAJOR;
569 prot_minor = PROTOCOL_MINOR; 581 prot_minor = PROTOCOL_MINOR;
570 randsize = RAND_SIZE; 582 randsize = RAND_SIZE;
571 hmaclen = HMACLENGTH; 583 hmaclen = HMACLENGTH;
579 591
580 len = sizeof (*this) - sizeof (net_packet); 592 len = sizeof (*this) - sizeof (net_packet);
581 set_hdr (type, dst); 593 set_hdr (type, dst);
582} 594}
583 595
596bool
584bool config_packet::chk_config () const 597config_packet::chk_config () const
585{ 598{
586 if (prot_major != PROTOCOL_MAJOR) 599 if (prot_major != PROTOCOL_MAJOR)
587 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);
588 else if (randsize != RAND_SIZE) 601 else if (randsize != RAND_SIZE)
589 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);
613 rsaencrdata encr; 626 rsaencrdata encr;
614 627
615 auth_req_packet (int dst, bool initiate_, u8 protocols_) 628 auth_req_packet (int dst, bool initiate_, u8 protocols_)
616 { 629 {
617 config_packet::setup (PT_AUTH_REQ, dst); 630 config_packet::setup (PT_AUTH_REQ, dst);
618 strncpy (magic, MAGIC, 8); 631 strncpy (magic, MAGIC_OLD, 8);
619 initiate = !!initiate_; 632 initiate = !!initiate_;
620 protocols = protocols_; 633 protocols = protocols_;
621 634
622 len = sizeof (*this) - sizeof (net_packet); 635 len = sizeof (*this) - sizeof (net_packet);
623 } 636 }
983 data_queue.put (new tap_packet (*pkt)); 996 data_queue.put (new tap_packet (*pkt));
984 post_inject_queue (); 997 post_inject_queue ();
985 } 998 }
986} 999}
987 1000
1001void
988void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 1002connection::inject_vpn_packet (vpn_packet *pkt, int tos)
989{ 1003{
990 if (ictx && octx) 1004 if (ictx && octx)
991 send_vpn_packet (pkt, si, tos); 1005 send_vpn_packet (pkt, si, tos);
992 else 1006 else
993 { 1007 {
999void 1013void
1000connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1014connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
1001{ 1015{
1002 last_activity = ev_now (); 1016 last_activity = ev_now ();
1003 1017
1004 slog (L_NOISE, "%s >> received packet type %d from %d to %d.", 1018 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1005 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); 1019 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1006 1020
1007 if (connectmode == conf_node::C_DISABLED) 1021 if (connectmode == conf_node::C_DISABLED)
1008 return; 1022 return;
1009 1023
1019 { 1033 {
1020 if (auth_rate_limiter.can (rsi)) 1034 if (auth_rate_limiter.can (rsi))
1021 send_auth_request (rsi, true); 1035 send_auth_request (rsi, true);
1022 } 1036 }
1023 else 1037 else
1024 // we would love to change thre socket address here, but ping's aren't 1038 // we would love to change the socket address here, but ping's aren't
1025 // authenticated, so we best ignore it. 1039 // authenticated, so we best ignore it.
1026 send_ping (rsi, 1); // pong 1040 send_ping (rsi, 1); // pong
1027 1041
1028 break; 1042 break;
1029 1043
1030 case vpn_packet::PT_PONG: 1044 case vpn_packet::PT_PONG:
1031 slog (L_TRACE, "%s >> PT_PONG", conf->nodename); 1045 slog (L_TRACE, "%s >> PT_PONG", conf->nodename);
1046
1047 // a PONG might mean that the other side doesn't really know
1048 // about our desire for communication.
1049 establish_connection ();
1032 break; 1050 break;
1033 1051
1034 case vpn_packet::PT_RESET: 1052 case vpn_packet::PT_RESET:
1035 { 1053 {
1036 reset_connection (); 1054 reset_connection ();
1053 { 1071 {
1054 auth_req_packet *p = (auth_req_packet *) pkt; 1072 auth_req_packet *p = (auth_req_packet *) pkt;
1055 1073
1056 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); 1074 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply");
1057 1075
1058 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 1076 if (p->chk_config ()
1077 && (!strncmp (p->magic, MAGIC_OLD, 8) || !strncmp (p->magic, MAGIC, 8)))
1059 { 1078 {
1060 if (p->prot_minor != PROTOCOL_MINOR) 1079 if (p->prot_minor != PROTOCOL_MINOR)
1061 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."),
1062 conf->nodename, (const char *)rsi, 1081 conf->nodename, (const char *)rsi,
1063 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1082 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1336 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1355 w.start (last_activity + ::conf.keepalive + 10 - ev::now ());
1337 else 1356 else
1338 reset_connection (); 1357 reset_connection ();
1339} 1358}
1340 1359
1360void
1341void connection::send_connect_request (int id) 1361connection::send_connect_request (int id)
1342{ 1362{
1343 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1363 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols);
1344 1364
1345 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", 1365 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)",
1346 conf->nodename, vpn->conns[id - 1]->conf->nodename); 1366 conf->nodename, vpn->conns[id - 1]->conf->nodename);
1348 send_vpn_packet (p, si); 1368 send_vpn_packet (p, si);
1349 1369
1350 delete p; 1370 delete p;
1351} 1371}
1352 1372
1373void
1353void connection::script_init_env (const char *ext) 1374connection::script_init_env (const char *ext)
1354{ 1375{
1355 char *env; 1376 char *env;
1356 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1377 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1357 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1378 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1358 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1379 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1359 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1380 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1360 conf->id & 0xff); putenv (env); 1381 conf->id & 0xff); putenv (env);
1361} 1382}
1362 1383
1384void
1363void connection::script_init_connect_env () 1385connection::script_init_connect_env ()
1364{ 1386{
1365 vpn->script_init_env (); 1387 vpn->script_init_env ();
1366 1388
1367 char *env; 1389 char *env;
1368 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1390 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1450connection::~connection () 1472connection::~connection ()
1451{ 1473{
1452 shutdown (); 1474 shutdown ();
1453} 1475}
1454 1476
1477void
1455void connection_init () 1478connection_init ()
1456{ 1479{
1457 auth_rate_limiter.clear (); 1480 auth_rate_limiter.clear ();
1458 reset_rate_limiter.clear (); 1481 reset_rate_limiter.clear ();
1459} 1482}
1460 1483

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines