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.73 by pcg, Sun Aug 10 01:34:36 2008 UTC vs.
Revision 1.97 by root, Tue Dec 4 13:23:17 2012 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,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
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{
128rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) 132rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h)
129{ 133{
130 EVP_MD_CTX ctx; 134 EVP_MD_CTX ctx;
131 135
132 EVP_MD_CTX_init (&ctx); 136 EVP_MD_CTX_init (&ctx);
133 require (EVP_DigestInit (&ctx, RSA_HASH)); 137 require (EVP_DigestInit (&ctx, RSA_HASH));
134 require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); 138 require (EVP_DigestUpdate (&ctx, &chg, sizeof chg));
135 require (EVP_DigestUpdate(&ctx, &id, sizeof id)); 139 require (EVP_DigestUpdate (&ctx, &id, sizeof id));
136 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); 140 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0));
137 EVP_MD_CTX_cleanup (&ctx); 141 EVP_MD_CTX_cleanup (&ctx);
138} 142}
139 143
140struct rsa_entry 144struct rsa_entry
141{ 145{
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 }
672///////////////////////////////////////////////////////////////////////////// 685/////////////////////////////////////////////////////////////////////////////
673 686
674void 687void
675connection::connection_established () 688connection::connection_established ()
676{ 689{
677 slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); 690 slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx);
678 691
679 if (ictx && octx) 692 if (ictx && octx)
680 { 693 {
681 // make sure rekeying timeouts are slightly asymmetric 694 // make sure rekeying timeouts are slightly asymmetric
682 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 695 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
683 rekey.start (rekey_interval, rekey_interval); 696 rekey.start (rekey_interval, rekey_interval);
697
684 keepalive.start (::conf.keepalive); 698 keepalive.start (::conf.keepalive);
685 699
686 // send queued packets 700 // send queued packets
687 if (ictx && octx) 701 if (ictx && octx)
688 { 702 {
760{ 774{
761 ping_packet *pkt = new ping_packet; 775 ping_packet *pkt = new ping_packet;
762 776
763 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); 777 pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING);
764 778
765 slog (L_TRACE, ">>%d %s [%s]", conf->id, pong ? "PT_PONG" : "PT_PING", (const char *)si); 779 slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si);
766 780
767 send_vpn_packet (pkt, si, IPTOS_LOWDELAY); 781 send_vpn_packet (pkt, si, IPTOS_LOWDELAY);
768 782
769 delete pkt; 783 delete pkt;
770} 784}
790 804
791 rsachallenge chg; 805 rsachallenge chg;
792 rsa_cache.gen (pkt->id, chg); 806 rsa_cache.gen (pkt->id, chg);
793 rsa_encrypt (conf->rsa_key, chg, pkt->encr); 807 rsa_encrypt (conf->rsa_key, chg, pkt->encr);
794 808
795 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); 809 slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si);
796 810
797 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly 811 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly
798 812
799 delete pkt; 813 delete pkt;
800} 814}
808 822
809 rsa_hash (id, chg, pkt->response); 823 rsa_hash (id, chg, pkt->response);
810 824
811 pkt->hmac_set (octx); 825 pkt->hmac_set (octx);
812 826
813 slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)si); 827 slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si);
814 828
815 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly 829 send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly
816 830
817 delete pkt; 831 delete pkt;
818} 832}
819 833
820void 834void
821connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 835connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
822{ 836{
823 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", 837 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename,
824 conf->id, rid, (const char *)rsi); 838 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi,
839 conf->protocols);
825 840
826 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 841 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
827 842
828 r->hmac_set (octx); 843 r->hmac_set (octx);
829 send_vpn_packet (r, si); 844 send_vpn_packet (r, si);
832} 847}
833 848
834inline void 849inline void
835connection::establish_connection_cb (ev::timer &w, int revents) 850connection::establish_connection_cb (ev::timer &w, int revents)
836{ 851{
837 if (!ictx 852 if (!(ictx && octx)
838 && conf != THISNODE 853 && conf != THISNODE
839 && connectmode != conf_node::C_NEVER 854 && connectmode != conf_node::C_NEVER
840 && connectmode != conf_node::C_DISABLED 855 && connectmode != conf_node::C_DISABLED
841 && !w.is_active ()) 856 && !w.is_active ())
842 { 857 {
860 875
861 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) 876 if (si.prot && !si.host && vpn->can_direct (THISNODE, conf))
862 { 877 {
863 /*TODO*/ /* start the timer so we don't recurse endlessly */ 878 /*TODO*/ /* start the timer so we don't recurse endlessly */
864 w.start (1); 879 w.start (1);
865 vpn->send_connect_request (conf->id); 880 vpn->send_connect_request (this);
866 } 881 }
867 else 882 else
868 { 883 {
869 if (si.valid ()) 884 if (si.valid ())
870 slog (L_DEBUG, _("%s: sending direct connection request to %s."), 885 slog (L_DEBUG, _("%s: sending direct connection request to %s."),
910 } 925 }
911 } 926 }
912 927
913 delete ictx; ictx = 0; 928 delete ictx; ictx = 0;
914 delete octx; octx = 0; 929 delete octx; octx = 0;
915#if ENABLE_DNS
916 dnsv4_reset_connection ();
917#endif
918 930
919 si.host = 0; 931 si.host = 0;
920 932
921 last_activity = 0.; 933 last_activity = 0.;
922 //last_si_change = 0.; 934 //last_si_change = 0.;
983 data_queue.put (new tap_packet (*pkt)); 995 data_queue.put (new tap_packet (*pkt));
984 post_inject_queue (); 996 post_inject_queue ();
985 } 997 }
986} 998}
987 999
1000void
988void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 1001connection::inject_vpn_packet (vpn_packet *pkt, int tos)
989{ 1002{
990 if (ictx && octx) 1003 if (ictx && octx)
991 send_vpn_packet (pkt, si, tos); 1004 send_vpn_packet (pkt, si, tos);
992 else 1005 else
993 { 1006 {
999void 1012void
1000connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1013connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
1001{ 1014{
1002 last_activity = ev_now (); 1015 last_activity = ev_now ();
1003 1016
1004 slog (L_NOISE, "<<%d received packet type %d from %d to %d.", 1017 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1005 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 1018 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1006 1019
1007 if (connectmode == conf_node::C_DISABLED) 1020 if (connectmode == conf_node::C_DISABLED)
1008 return; 1021 return;
1009 1022
1010 switch (pkt->typ ()) 1023 switch (pkt->typ ())
1011 { 1024 {
1012 case vpn_packet::PT_PING: 1025 case vpn_packet::PT_PING:
1026 slog (L_TRACE, "%s >> PT_PING", conf->nodename);
1027
1013 // we send pings instead of auth packets after some retries, 1028 // we send pings instead of auth packets after some retries,
1014 // so reset the retry counter and establish a connection 1029 // so reset the retry counter and establish a connection
1015 // when we receive a ping. 1030 // when we receive a ping.
1016 if (!ictx) 1031 if (!ictx)
1017 { 1032 {
1018 if (auth_rate_limiter.can (rsi)) 1033 if (auth_rate_limiter.can (rsi))
1019 send_auth_request (rsi, true); 1034 send_auth_request (rsi, true);
1020 } 1035 }
1021 else 1036 else
1022 // we would love to change thre socket address here, but ping's aren't 1037 // we would love to change the socket address here, but ping's aren't
1023 // authenticated, so we best ignore it. 1038 // authenticated, so we best ignore it.
1024 send_ping (rsi, 1); // pong 1039 send_ping (rsi, 1); // pong
1025 1040
1026 break; 1041 break;
1027 1042
1028 case vpn_packet::PT_PONG: 1043 case vpn_packet::PT_PONG:
1044 slog (L_TRACE, "%s >> PT_PONG", conf->nodename);
1045
1046 // a PONG might mean that the other side doesn't really know
1047 // about our desire for communication.
1048 establish_connection ();
1029 break; 1049 break;
1030 1050
1031 case vpn_packet::PT_RESET: 1051 case vpn_packet::PT_RESET:
1032 { 1052 {
1033 reset_connection (); 1053 reset_connection ();
1046 break; 1066 break;
1047 1067
1048 case vpn_packet::PT_AUTH_REQ: 1068 case vpn_packet::PT_AUTH_REQ:
1049 if (auth_rate_limiter.can (rsi)) 1069 if (auth_rate_limiter.can (rsi))
1050 { 1070 {
1051 auth_req_packet *p = (auth_req_packet *) pkt; 1071 auth_req_packet *p = (auth_req_packet *)pkt;
1052 1072
1053 slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); 1073 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1074 conf->nodename, p->initiate ? "initiate" : "reply",
1075 p->protocols, p->features);
1054 1076
1055 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 1077 if (p->chk_config ()
1078 && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8)))
1056 { 1079 {
1057 if (p->prot_minor != PROTOCOL_MINOR) 1080 if (p->prot_minor != PROTOCOL_MINOR)
1058 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1081 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1059 conf->nodename, (const char *)rsi, 1082 conf->nodename, (const char *)rsi,
1060 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1083 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1096 1119
1097 break; 1120 break;
1098 1121
1099 case vpn_packet::PT_AUTH_RES: 1122 case vpn_packet::PT_AUTH_RES:
1100 { 1123 {
1101 auth_res_packet *p = (auth_res_packet *) pkt; 1124 auth_res_packet *p = (auth_res_packet *)pkt;
1102 1125
1103 slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); 1126 slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename);
1104 1127
1105 if (p->chk_config ()) 1128 if (p->chk_config ())
1106 { 1129 {
1107 if (p->prot_minor != PROTOCOL_MINOR) 1130 if (p->prot_minor != PROTOCOL_MINOR)
1108 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1131 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 1166 iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid
1144 1167
1145 si = rsi; 1168 si = rsi;
1146 protocol = rsi.prot; 1169 protocol = rsi.prot;
1147 1170
1148 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d."), 1171 slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."),
1149 conf->nodename, (const char *)rsi, 1172 conf->nodename, (const char *)rsi,
1173 is_direct ? "direct" : "forwarded",
1150 p->prot_major, p->prot_minor); 1174 p->prot_major, p->prot_minor);
1151 1175
1152 connection_established (); 1176 connection_established ();
1153 1177
1154 if (::conf.script_node_up) 1178 if (::conf.script_node_up)
1202 if (si != rsi) 1226 if (si != rsi)
1203 { 1227 {
1204 // fast re-sync on source address changes, useful especially for tcp/ip 1228 // fast re-sync on source address changes, useful especially for tcp/ip
1205 //if (last_si_change < ev_now () + 5.) 1229 //if (last_si_change < ev_now () + 5.)
1206 // { 1230 // {
1231 slog (L_INFO, _("%s(%s): changing socket address to %s."),
1232 conf->nodename, (const char *)si, (const char *)rsi);
1233
1207 si = rsi; 1234 si = rsi;
1208 1235
1209 slog (L_INFO, _("%s(%s): socket address changed to %s."), 1236 if (::conf.script_node_change)
1210 conf->nodename, (const char *)si, (const char *)rsi); 1237 {
1238 run_script_cb *cb = new run_script_cb;
1239 cb->set<connection, &connection::script_node_change> (this);
1240 run_script_queued (cb, _("node-change command execution failed, continuing."));
1241 }
1242
1211 // } 1243 // }
1212 //else 1244 //else
1213 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), 1245 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."),
1214 // conf->nodename, (const char *)si, (const char *)rsi); 1246 // conf->nodename, (const char *)si, (const char *)rsi);
1215 } 1247 }
1216 } 1248 }
1217 else if (seqclass == 1) // silently ignore 1249 else if (seqclass == 1) // far history
1250 slog (L_ERR, _("received very old packet (received %08lx, expected %08lx). "
1251 "possible replay attack, or just packet duplication/delay, ignoring."), seqno, iseqno.seq + 1);
1252 else if (seqclass == 2) // in-window duplicate, happens often on wireless
1218 slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" 1253 slog (L_DEBUG, _("received recent duplicated packet (received %08lx, expected %08lx). "
1219 "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1); 1254 "possible replay attack, or just packet duplication, ignoring."), seqno, iseqno.seq + 1);
1220 else if (seqclass == 2) // reset 1255 else if (seqclass == 3) // reset
1221 { 1256 {
1222 slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" 1257 slog (L_ERR, _("received out-of-sync (far future) packet (received %08lx, expected %08lx). "
1223 "possible replay attack, or just massive packet loss, resetting connection."), seqno, iseqno.seq + 1); 1258 "probably just massive packet loss, sending reset."), seqno, iseqno.seq + 1);
1224 send_reset (rsi); 1259 send_reset (rsi);
1225 } 1260 }
1226 1261
1227 delete d; 1262 delete d;
1228 break; 1263 break;
1233 break; 1268 break;
1234 1269
1235 case vpn_packet::PT_CONNECT_REQ: 1270 case vpn_packet::PT_CONNECT_REQ:
1236 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1271 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1237 { 1272 {
1238 connect_req_packet *p = (connect_req_packet *) pkt; 1273 connect_req_packet *p = (connect_req_packet *)pkt;
1239 1274
1240 if (p->id > 0 && p->id <= vpn->conns.size ()) 1275 if (p->id > 0 && p->id <= vpn->conns.size ())
1241 { 1276 {
1242 connection *c = vpn->conns[p->id - 1]; 1277 connection *c = vpn->conns[p->id - 1];
1243 conf->protocols = p->protocols; 1278 conf->protocols = p->protocols;
1244 1279
1245 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", 1280 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]",
1281 conf->nodename, vpn->conns[p->id - 1]->conf->nodename,
1282 p->protocols,
1246 conf->id, p->id, c->ictx && c->octx); 1283 c->ictx && c->octx);
1247 1284
1248 if (c->ictx && c->octx) 1285 if (c->ictx && c->octx)
1249 { 1286 {
1250 // send connect_info packets to both sides, in case one is 1287 // send connect_info packets to both sides, in case one is
1251 // behind a nat firewall (or both ;) 1288 // behind a nat firewall (or both ;)
1274 1311
1275 c->conf->protocols = p->protocols; 1312 c->conf->protocols = p->protocols;
1276 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1313 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1277 p->si.upgrade_protocol (protocol, c->conf); 1314 p->si.upgrade_protocol (protocol, c->conf);
1278 1315
1279 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1316 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,protocols=%02x,protocol=%02x,upgradable=%02x) [%d]",
1280 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1317 conf->nodename,
1318 vpn->conns[p->id - 1]->conf->nodename,
1319 (const char *)p->si,
1320 p->protocols,
1321 protocol,
1322 p->si.supported_protocols (c->conf),
1323 !c->ictx && !c->octx);
1281 1324
1282 const sockinfo &dsi = forward_si (p->si); 1325 const sockinfo &dsi = forward_si (p->si);
1283 1326
1284 if (dsi.valid ()) 1327 if (dsi.valid ())
1285 c->send_auth_request (dsi, true); 1328 c->send_auth_request (dsi, true);
1329 else
1330 slog (L_INFO, "connect info for %s received (%s), but still unable to contact.",
1331 vpn->conns[p->id - 1]->conf->nodename,
1332 (const char *)p->si);
1286 } 1333 }
1287 else 1334 else
1288 slog (L_WARN, 1335 slog (L_WARN,
1289 _("received authenticated connection request from unknown node #%d, config file mismatch?"), 1336 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1290 p->id); 1337 p->id);
1299} 1346}
1300 1347
1301inline void 1348inline void
1302connection::keepalive_cb (ev::timer &w, int revents) 1349connection::keepalive_cb (ev::timer &w, int revents)
1303{ 1350{
1304 if (ev_now () >= last_activity + ::conf.keepalive + 30) 1351 ev_tstamp when = last_activity + ::conf.keepalive - ev::now ();
1352
1353 if (when >= 0)
1354 w.start (when);
1355 else if (when < -15)
1305 { 1356 {
1306 reset_connection (); 1357 reset_connection ();
1307 establish_connection (); 1358 establish_connection ();
1308 } 1359 }
1309 else if (ev_now () < last_activity + ::conf.keepalive)
1310 w.start (last_activity + ::conf.keepalive - ev::now ());
1311 else if (conf->connectmode != conf_node::C_ONDEMAND 1360 else if (conf->connectmode != conf_node::C_ONDEMAND
1312 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1361 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1313 { 1362 {
1363 w.start (3);
1314 send_ping (si); 1364 send_ping (si);
1315 w.start (5);
1316 } 1365 }
1317 else if (ev_now () < last_activity + ::conf.keepalive + 10) 1366 else if (when >= -10)
1318 // hold ondemand connections implicitly a few seconds longer 1367 // hold ondemand connections implicitly a few seconds longer
1319 // should delete octx, though, or something like that ;) 1368 // should delete octx, though, or something like that ;)
1320 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1369 w.start (when + 10);
1321 else 1370 else
1322 reset_connection (); 1371 reset_connection ();
1323} 1372}
1324 1373
1374void
1325void connection::send_connect_request (int id) 1375connection::send_connect_request (int id)
1326{ 1376{
1327 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1377 connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols);
1328 1378
1329 slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", conf->id, id); 1379 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)",
1380 conf->nodename, vpn->conns[id - 1]->conf->nodename,
1381 THISNODE->protocols);
1330 p->hmac_set (octx); 1382 p->hmac_set (octx);
1331 send_vpn_packet (p, si); 1383 send_vpn_packet (p, si);
1332 1384
1333 delete p; 1385 delete p;
1334} 1386}
1335 1387
1388void
1336void connection::script_init_env (const char *ext) 1389connection::script_init_env (const char *ext)
1337{ 1390{
1338 char *env; 1391 char *env;
1339 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1392 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1340 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1393 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1341 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1394 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1342 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1395 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1343 conf->id & 0xff); putenv (env); 1396 conf->id & 0xff); putenv (env);
1344} 1397}
1345 1398
1399void
1346void connection::script_init_connect_env () 1400connection::script_init_connect_env ()
1347{ 1401{
1348 vpn->script_init_env (); 1402 vpn->script_init_env ();
1349 1403
1350 char *env; 1404 char *env;
1351 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1405 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1406 asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env);
1352 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1407 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1353 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1408 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1354 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1409 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1355} 1410}
1356 1411
1357inline const char * 1412inline const char *
1358connection::script_node_up () 1413connection::script_node_up ()
1359{ 1414{
1364 char *filename; 1419 char *filename;
1365 asprintf (&filename, 1420 asprintf (&filename,
1366 "%s/%s", 1421 "%s/%s",
1367 confbase, 1422 confbase,
1368 ::conf.script_node_up ? ::conf.script_node_up : "node-up"); 1423 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1424
1425 return filename;
1426}
1427
1428inline const char *
1429connection::script_node_change ()
1430{
1431 script_init_connect_env ();
1432
1433 putenv ((char *)"STATE=change");
1434
1435 char *filename;
1436 asprintf (&filename,
1437 "%s/%s",
1438 confbase,
1439 ::conf.script_node_change ? ::conf.script_node_change : "node-change");
1369 1440
1370 return filename; 1441 return filename;
1371} 1442}
1372 1443
1373inline const char * 1444inline const char *
1399 establish_connection.set<connection, &connection::establish_connection_cb> (this); 1470 establish_connection.set<connection, &connection::establish_connection_cb> (this);
1400 1471
1401 last_establish_attempt = 0.; 1472 last_establish_attempt = 0.;
1402 octx = ictx = 0; 1473 octx = ictx = 0;
1403 1474
1404 if (!conf->protocols) // make sure some protocol is enabled
1405 conf->protocols = PROT_UDPv4;
1406
1407 connectmode = conf->connectmode; 1475 connectmode = conf->connectmode;
1408 1476
1409 // queue a dummy packet to force an initial connection attempt 1477 // queue a dummy packet to force an initial connection attempt
1410 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1478 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1411 vpn_queue.put (new net_packet); 1479 vpn_queue.put (new net_packet);
1416connection::~connection () 1484connection::~connection ()
1417{ 1485{
1418 shutdown (); 1486 shutdown ();
1419} 1487}
1420 1488
1489void
1421void connection_init () 1490connection_init ()
1422{ 1491{
1423 auth_rate_limiter.clear (); 1492 auth_rate_limiter.clear ();
1424 reset_rate_limiter.clear (); 1493 reset_rate_limiter.clear ();
1425} 1494}
1426 1495

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines