ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/protocol.C
(Generate patch)

Comparing gvpe/src/protocol.C (file contents):
Revision 1.8 by pcg, Mon Mar 17 15:20:18 2003 UTC vs.
Revision 1.12 by pcg, Sat Mar 22 22:28:34 2003 UTC

16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/ 17*/
18 18
19#include "config.h" 19#include "config.h"
20 20
21#include <list>
22
21#include <cstdlib> 23#include <cstdlib>
22#include <cstring> 24#include <cstring>
23#include <cstdio> 25#include <cstdio>
24 26
25#include <sys/types.h> 27#include <sys/types.h>
60 62
61static const rsachallenge & 63static const rsachallenge &
62challenge_bytes () 64challenge_bytes ()
63{ 65{
64 static rsachallenge challenge; 66 static rsachallenge challenge;
65 static time_t challenge_ttl; // time this challenge needs to be recreated 67 static double challenge_ttl; // time this challenge needs to be recreated
66 68
67 if (now > challenge_ttl) 69 if (NOW > challenge_ttl)
68 { 70 {
69 RAND_bytes ((unsigned char *)&challenge, sizeof (challenge)); 71 RAND_bytes ((unsigned char *)&challenge, sizeof (challenge));
70 challenge_ttl = now + CHALLENGE_TTL; 72 challenge_ttl = NOW + CHALLENGE_TTL;
71 } 73 }
72 74
73 return challenge; 75 return challenge;
76}
77
78// caching of rsa operations really helps slow computers
79struct rsa_entry {
80 tstamp expire;
81 rsachallenge chg;
82 RSA *key; // which key
83 rsaencrdata encr;
84
85 rsa_entry ()
86 {
87 expire = NOW + CHALLENGE_TTL;
88 }
89};
90
91struct rsa_cache : list<rsa_entry>
92{
93 void cleaner_cb (tstamp &ts); time_watcher cleaner;
94
95 const rsaencrdata *public_encrypt (RSA *key, const rsachallenge &chg)
96 {
97 for (iterator i = begin (); i != end (); ++i)
98 {
99 if (i->key == key && !memcmp (&chg, &i->chg, sizeof chg))
100 return &i->encr;
101 }
102
103 if (cleaner.at < NOW)
104 cleaner.start (NOW + CHALLENGE_TTL);
105
106 resize (size () + 1);
107 rsa_entry *e = &(*rbegin ());
108
109 e->key = key;
110 memcpy (&e->chg, &chg, sizeof chg);
111
112 if (0 > RSA_public_encrypt (sizeof chg,
113 (unsigned char *)&chg, (unsigned char *)&e->encr,
114 key, RSA_PKCS1_OAEP_PADDING))
115 fatal ("RSA_public_encrypt error");
116
117 return &e->encr;
118 }
119
120 const rsachallenge *private_decrypt (RSA *key, const rsaencrdata &encr)
121 {
122 for (iterator i = begin (); i != end (); ++i)
123 if (i->key == key && !memcmp (&encr, &i->encr, sizeof encr))
124 return &i->chg;
125
126 if (cleaner.at < NOW)
127 cleaner.start (NOW + CHALLENGE_TTL);
128
129 resize (size () + 1);
130 rsa_entry *e = &(*rbegin ());
131
132 e->key = key;
133 memcpy (&e->encr, &encr, sizeof encr);
134
135 if (0 > RSA_private_decrypt (sizeof encr,
136 (unsigned char *)&encr, (unsigned char *)&e->chg,
137 key, RSA_PKCS1_OAEP_PADDING))
138 {
139 pop_back ();
140 return 0;
141 }
142
143 return &e->chg;
144 }
145
146 rsa_cache ()
147 : cleaner (this, &rsa_cache::cleaner_cb)
148 { }
149
150} rsa_cache;
151
152void rsa_cache::cleaner_cb (tstamp &ts)
153{
154 if (empty ())
155 ts = TSTAMP_CANCEL;
156 else
157 {
158 ts = NOW + 3;
159 for (iterator i = begin (); i != end (); )
160 {
161 if (i->expire >= NOW)
162 i = erase (i);
163 else
164 ++i;
165 }
166 }
74} 167}
75 168
76// run a script. yes, it's a template function. yes, c++ 169// run a script. yes, it's a template function. yes, c++
77// is not a functional language. yes, this suxx. 170// is not a functional language. yes, this suxx.
78template<class owner> 171template<class owner>
112struct crypto_ctx 205struct crypto_ctx
113 { 206 {
114 EVP_CIPHER_CTX cctx; 207 EVP_CIPHER_CTX cctx;
115 HMAC_CTX hctx; 208 HMAC_CTX hctx;
116 209
117 crypto_ctx (rsachallenge &challenge, int enc); 210 crypto_ctx (const rsachallenge &challenge, int enc);
118 ~crypto_ctx (); 211 ~crypto_ctx ();
119 }; 212 };
120 213
121crypto_ctx::crypto_ctx (rsachallenge &challenge, int enc) 214crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc)
122{ 215{
123 EVP_CIPHER_CTX_init (&cctx); 216 EVP_CIPHER_CTX_init (&cctx);
124 EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); 217 EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc);
125 HMAC_CTX_init (&hctx); 218 HMAC_CTX_init (&hctx);
126 HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); 219 HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0);
128 221
129crypto_ctx::~crypto_ctx () 222crypto_ctx::~crypto_ctx ()
130{ 223{
131 EVP_CIPHER_CTX_cleanup (&cctx); 224 EVP_CIPHER_CTX_cleanup (&cctx);
132 HMAC_CTX_cleanup (&hctx); 225 HMAC_CTX_cleanup (&hctx);
226}
227
228//////////////////////////////////////////////////////////////////////////////
229
230void pkt_queue::put (tap_packet *p)
231{
232 if (queue[i])
233 {
234 delete queue[i];
235 j = (j + 1) % QUEUEDEPTH;
236 }
237
238 queue[i] = p;
239
240 i = (i + 1) % QUEUEDEPTH;
241}
242
243tap_packet *pkt_queue::get ()
244{
245 tap_packet *p = queue[j];
246
247 if (p)
248 {
249 queue[j] = 0;
250 j = (j + 1) % QUEUEDEPTH;
251 }
252
253 return p;
254}
255
256pkt_queue::pkt_queue ()
257{
258 memset (queue, 0, sizeof (queue));
259 i = 0;
260 j = 0;
261}
262
263pkt_queue::~pkt_queue ()
264{
265 for (i = QUEUEDEPTH; --i > 0; )
266 delete queue[i];
267}
268
269// only do action once every x seconds per host.
270// currently this is quite a slow implementation,
271// but suffices for normal operation.
272struct u32_rate_limiter : private map<u32, tstamp>
273 {
274 tstamp every;
275
276 bool can (u32 host);
277
278 u32_rate_limiter (tstamp every = 1)
279 {
280 this->every = every;
281 }
282 };
283
284struct net_rate_limiter : u32_rate_limiter
285 {
286 bool can (SOCKADDR *sa) { return u32_rate_limiter::can((u32)sa->sin_addr.s_addr); }
287 bool can (sockinfo &si) { return u32_rate_limiter::can((u32)si.host); }
288
289 net_rate_limiter (tstamp every) : u32_rate_limiter (every) {}
290 };
291
292bool u32_rate_limiter::can (u32 host)
293{
294 iterator i;
295
296 for (i = begin (); i != end (); )
297 if (i->second <= NOW)
298 {
299 erase (i);
300 i = begin ();
301 }
302 else
303 ++i;
304
305 i = find (host);
306
307 if (i != end ())
308 return false;
309
310 insert (value_type (host, NOW + every));
311
312 return true;
133} 313}
134 314
135///////////////////////////////////////////////////////////////////////////// 315/////////////////////////////////////////////////////////////////////////////
136 316
137static void next_wakeup (time_t next) 317static void next_wakeup (time_t next)
200 380
201 unsigned int src () 381 unsigned int src ()
202 { 382 {
203 return src1 | ((srcdst >> 4) << 8); 383 return src1 | ((srcdst >> 4) << 8);
204 } 384 }
385
205 unsigned int dst () 386 unsigned int dst ()
206 { 387 {
207 return dst1 | ((srcdst & 0xf) << 8); 388 return dst1 | ((srcdst & 0xf) << 8);
208 } 389 }
390
209 ptype typ () 391 ptype typ ()
210 { 392 {
211 return (ptype) type; 393 return (ptype) type;
212 } 394 }
213 }; 395 };
252 u32 cl; 434 u32 cl;
253 435
254 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); 436 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7);
255 if (cl) 437 if (cl)
256 { 438 {
257 //printf ("compressed packet, %d => %d\n", l, cl);//D
258 type = PT_DATA_COMPRESSED; 439 type = PT_DATA_COMPRESSED;
259 d = cdata; 440 d = cdata;
260 l = cl + 2; 441 l = cl + 2;
261 442
262 d[0] = cl >> 8; 443 d[0] = cl >> 8;
270 struct { 451 struct {
271 u8 rnd[RAND_SIZE]; 452 u8 rnd[RAND_SIZE];
272 u32 seqno; 453 u32 seqno;
273 } datahdr; 454 } datahdr;
274 455
275 datahdr.seqno = seqno; 456 datahdr.seqno = ntohl (seqno);
276 RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); 457 RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE);
277 458
278 EVP_EncryptUpdate (cctx, 459 EVP_EncryptUpdate (cctx,
279 (unsigned char *) data + outl, &outl2, 460 (unsigned char *) data + outl, &outl2,
280 (unsigned char *) &datahdr, DATAHDR); 461 (unsigned char *) &datahdr, DATAHDR);
328 outl += outl2; 509 outl += outl2;
329 510
330 EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); 511 EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2);
331 outl += outl2; 512 outl += outl2;
332 513
333 seqno = *(u32 *)(d + RAND_SIZE); 514 seqno = ntohl (*(u32 *)(d + RAND_SIZE));
334 515
335 id2mac (dst () ? dst() : THISNODE->id, p->dst); 516 id2mac (dst () ? dst() : THISNODE->id, p->dst);
336 id2mac (src (), p->src); 517 id2mac (src (), p->src);
337 518
338#if ENABLE_COMPRESSION 519#if ENABLE_COMPRESSION
339 if (type == PT_DATA_COMPRESSED) 520 if (type == PT_DATA_COMPRESSED)
340 { 521 {
341 u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; 522 u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1];
342 p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; 523 p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6;
343 //printf ("decompressxed %d(%d) => %d\n", cl, len - data_hdr_size (), p->len);//D
344 } 524 }
345 else 525 else
346 p->len = outl + (6 + 6 - DATAHDR); 526 p->len = outl + (6 + 6 - DATAHDR);
347#endif 527#endif
348 528
369 u32 digest_nid; 549 u32 digest_nid;
370 550
371 const u8 curflags () const 551 const u8 curflags () const
372 { 552 {
373 return 0x80 553 return 0x80
554 | 0x02
555#if PROTOCOL_MAJOR != 2
556#error hi
557#endif
374 | (ENABLE_COMPRESSION ? 0x01 : 0x00) 558 | (ENABLE_COMPRESSION ? 0x01 : 0x00);
375 | (ENABLE_TRUST ? 0x02 : 0x00);
376 } 559 }
377 560
378 void setup (ptype type, int dst) 561 void setup (ptype type, int dst)
379 { 562 {
380 prot_major = PROTOCOL_MAJOR; 563 prot_major = PROTOCOL_MAJOR;
491void 674void
492connection::send_reset (SOCKADDR *dsa) 675connection::send_reset (SOCKADDR *dsa)
493{ 676{
494 static net_rate_limiter limiter(1); 677 static net_rate_limiter limiter(1);
495 678
496 if (limiter.can (dsa)) 679 if (limiter.can (dsa) && connectmode != conf_node::C_DISABLED)
497 { 680 {
498 config_packet *pkt = new config_packet; 681 config_packet *pkt = new config_packet;
499 682
500 pkt->setup (vpn_packet::PT_RESET, conf->id); 683 pkt->setup (vpn_packet::PT_RESET, conf->id);
501 vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); 684 vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST);
503 delete pkt; 686 delete pkt;
504 } 687 }
505} 688}
506 689
507static rsachallenge * 690static rsachallenge *
508gen_challenge (SOCKADDR *sa) 691gen_challenge (u32 seqrand, SOCKADDR *sa)
509{ 692{
510 static rsachallenge k; 693 static rsachallenge k;
511 694
512 memcpy (&k, &challenge_bytes (), sizeof (k)); 695 memcpy (&k, &challenge_bytes (), sizeof (k));
513 RAND_bytes ((unsigned char *)&k[CHG_SEQNO], sizeof (u32)); 696 *(u32 *)&k[CHG_SEQNO] ^= seqrand;
514 xor_sa (k, sa); 697 xor_sa (k, sa);
515 698
516 return &k; 699 return &k;
517} 700}
518 701
519void 702void
520connection::send_auth (auth_subtype subtype, SOCKADDR *sa, rsachallenge *k) 703connection::send_auth (auth_subtype subtype, SOCKADDR *sa, const rsachallenge *k)
521{ 704{
522 static net_rate_limiter limiter(2); 705 static net_rate_limiter limiter(0.2);
523 706
524 if (subtype != AUTH_INIT || limiter.can (sa)) 707 if (subtype != AUTH_INIT || limiter.can (sa))
525 { 708 {
709 if (!k)
710 k = gen_challenge (seqrand, sa);
711
526 auth_packet *pkt = new auth_packet (conf->id, subtype); 712 auth_packet *pkt = new auth_packet (conf->id, subtype);
527 713
528 //printf ("send auth_packet subtype %d\n", subtype);//D 714 memcpy (pkt->challenge, rsa_cache.public_encrypt (conf->rsa_key, *k), sizeof (rsaencrdata));
529
530 if (!k)
531 k = gen_challenge (sa);
532
533#if ENABLE_TRUST
534 if (0 > RSA_public_encrypt (sizeof (*k),
535 (unsigned char *)k, (unsigned char *)&pkt->challenge,
536 conf->rsa_key, RSA_PKCS1_OAEP_PADDING))
537 fatal ("RSA_public_encrypt error");
538#else
539# error untrusted mode not yet implemented: programemr does not know how to
540 rsaencrdata enc;
541
542 if (0 > RSA_private_encrypt (sizeof (*k),
543 (unsigned char *)k, (unsigned char *)&enc,
544 ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING))
545 fatal ("RSA_private_encrypt error");
546
547 if (0 > RSA_public_encrypt (sizeof (enc),
548 (unsigned char *)enc, (unsigned char *)&pkt->challenge,
549 conf->rsa_key, RSA_NO_PADDING))
550 fatal ("RSA_public_encrypt error");
551#endif
552 715
553 slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); 716 slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa));
554 717
555 vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); 718 vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY);
556 719
557 delete pkt; 720 delete pkt;
558 } 721 }
559} 722}
560 723
561void 724void
562connection::establish_connection () 725connection::establish_connection_cb (tstamp &ts)
563{ 726{
564 if (!ictx && conf != THISNODE && connectmode != conf_node::C_NEVER) 727 if (ictx || conf == THISNODE || connectmode == conf_node::C_NEVER)
728 ts = TSTAMP_CANCEL;
729 else if (ts <= NOW)
565 { 730 {
566 if (now >= next_retry) 731 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.25;
567 {
568 int retry_int = retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2);
569 732
570 if (retry_cnt < (17 << 2) | 3) 733 if (retry_int < 3600 * 8)
571 retry_cnt++; 734 retry_cnt++;
572 735
573 if (connectmode == conf_node::C_ONDEMAND 736 if (connectmode == conf_node::C_ONDEMAND
574 && retry_int > ::conf.keepalive) 737 && retry_int > ::conf.keepalive)
575 retry_int = ::conf.keepalive; 738 retry_int = ::conf.keepalive;
576 739
577 next_retry = now + retry_int; 740 ts = NOW + retry_int;
578 next_wakeup (next_retry);
579 741
580 if (conf->hostname) 742 if (conf->hostname)
581 { 743 {
582 reset_dstaddr (); 744 reset_dstaddr ();
583 if (sa.sin_addr.s_addr) 745 if (sa.sin_addr.s_addr)
584 if (retry_cnt < 4) 746 if (retry_cnt < 4)
585 send_auth (AUTH_INIT, &sa); 747 send_auth (AUTH_INIT, &sa);
586 else 748 else
587 send_ping (&sa, 0); 749 send_ping (&sa, 0);
588 } 750 }
589 else 751 else
590 vpn->connect_request (conf->id); 752 vpn->connect_request (conf->id);
591 }
592 } 753 }
593} 754}
594 755
595void 756void
596connection::reset_connection () 757connection::reset_connection ()
601 762
602 if (::conf.script_node_down) 763 if (::conf.script_node_down)
603 run_script (this, &connection::script_node_down, false); 764 run_script (this, &connection::script_node_down, false);
604 } 765 }
605 766
606 delete ictx; 767 delete ictx; ictx = 0;
607 ictx = 0; 768 delete octx; octx = 0;
608 769
609 delete octx; 770 RAND_bytes ((unsigned char *)&seqrand, sizeof (u32));
610 octx = 0;
611 771
612 sa.sin_port = 0; 772 sa.sin_port = 0;
613 sa.sin_addr.s_addr = 0; 773 sa.sin_addr.s_addr = 0;
614 774
615 next_retry = 0;
616 next_rekey = 0;
617 last_activity = 0; 775 last_activity = 0;
776
777 rekey.reset ();
778 keepalive.reset ();
779 establish_connection.reset ();
618} 780}
619 781
620void 782void
621connection::shutdown () 783connection::shutdown ()
622{ 784{
625 787
626 reset_connection (); 788 reset_connection ();
627} 789}
628 790
629void 791void
630connection::rekey () 792connection::rekey_cb (tstamp &ts)
631{ 793{
794 ts = TSTAMP_CANCEL;
795
632 reset_connection (); 796 reset_connection ();
633 establish_connection (); 797 establish_connection ();
634} 798}
635 799
636void 800void
668} 832}
669 833
670void 834void
671connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) 835connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa)
672{ 836{
673 last_activity = now; 837 last_activity = NOW;
674 838
675 slog (L_NOISE, "<<%d received packet type %d from %d to %d", 839 slog (L_NOISE, "<<%d received packet type %d from %d to %d",
676 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 840 conf->id, pkt->typ (), pkt->src (), pkt->dst ());
677 841
678 switch (pkt->typ ()) 842 switch (pkt->typ ())
686 // so reset the retry counter and establish a conenction 850 // so reset the retry counter and establish a conenction
687 // when we receive a pong. 851 // when we receive a pong.
688 if (!ictx && !octx) 852 if (!ictx && !octx)
689 { 853 {
690 retry_cnt = 0; 854 retry_cnt = 0;
691 next_retry = 0; 855 establish_connection.at = 0;
692 establish_connection (); 856 establish_connection ();
693 } 857 }
694 858
695 break; 859 break;
696 860
697 case vpn_packet::PT_RESET: 861 case vpn_packet::PT_RESET:
698 { 862 {
699 reset_connection (); 863 reset_connection ();
700 864
701 config_packet *p = (config_packet *) pkt; 865 config_packet *p = (config_packet *) pkt;
702 if (p->chk_config ()) 866 if (!p->chk_config ())
867 {
868 slog (L_WARN, _("protocol mismatch, disabling node '%s'"), conf->nodename);
869 connectmode = conf_node::C_DISABLED;
870 }
703 if (connectmode == conf_node::C_ALWAYS) 871 else if (connectmode == conf_node::C_ALWAYS)
704 establish_connection (); 872 establish_connection ();
705
706 //D slog the protocol mismatch?
707 } 873 }
708 break; 874 break;
709 875
710 case vpn_packet::PT_AUTH: 876 case vpn_packet::PT_AUTH:
711 { 877 {
721 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 887 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
722 888
723 if (p->subtype == AUTH_INIT) 889 if (p->subtype == AUTH_INIT)
724 send_auth (AUTH_INITREPLY, ssa); 890 send_auth (AUTH_INITREPLY, ssa);
725 891
726 rsachallenge k; 892 const rsachallenge *k = rsa_cache.private_decrypt (::conf.rsa_key, p->challenge);
727 893
728#if ENABLE_TRUST
729 if (0 > RSA_private_decrypt (sizeof (rsaencrdata),
730 (unsigned char *)&p->challenge, (unsigned char *)&k,
731 ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING))
732 // continued below
733#else
734 rsaencrdata j;
735 894 if (!k)
736 if (0 > RSA_private_decrypt (sizeof (rsaencrdata),
737 (unsigned char *)&p->challenge, (unsigned char *)&j,
738 ::conf.rsa_key, RSA_NO_PADDING))
739 fatal ("RSA_private_decrypt error");
740
741 if (0 > RSA_public_decrypt (sizeof (k),
742 (unsigned char *)&j, (unsigned char *)&k,
743 conf->rsa_key, RSA_PKCS1_OAEP_PADDING))
744 // continued below
745#endif
746 { 895 {
747 slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), 896 slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"),
748 conf->nodename, (const char *)sockinfo (ssa)); 897 conf->nodename, (const char *)sockinfo (ssa));
749 break; 898 break;
750 } 899 }
751 900
752 retry_cnt = 0; 901 retry_cnt = 0;
753 next_retry = now + 8; 902 establish_connection.set (NOW + 8); //? ;)
903 keepalive.reset ();
904 rekey.reset ();
754 905
755 switch (p->subtype) 906 switch (p->subtype)
756 { 907 {
757 case AUTH_INIT: 908 case AUTH_INIT:
758 case AUTH_INITREPLY: 909 case AUTH_INITREPLY:
759 delete ictx; 910 delete ictx;
760 ictx = 0; 911 ictx = 0;
761 912
762 delete octx; 913 delete octx;
763 914
764 octx = new crypto_ctx (k, 1); 915 octx = new crypto_ctx (*k, 1);
765 oseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; 916 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO] & 0x7fffffff);
766 917
767 send_auth (AUTH_REPLY, ssa, &k); 918 send_auth (AUTH_REPLY, ssa, k);
768 break; 919 break;
769 920
770 case AUTH_REPLY: 921 case AUTH_REPLY:
771 922
772 if (!memcmp ((u8 *)gen_challenge (ssa) + sizeof (u32), (u8 *)&k + sizeof (u32), 923 if (!memcmp ((u8 *)gen_challenge (seqrand, ssa), (u8 *)k, sizeof (rsachallenge)))
773 sizeof (rsachallenge) - sizeof (u32)))
774 { 924 {
775 delete ictx; 925 delete ictx;
776 926
777 ictx = new crypto_ctx (k, 0); 927 ictx = new crypto_ctx (*k, 0);
778 iseqno.reset (*(u32 *)&k[CHG_SEQNO] & 0x7fffffff); // at least 2**31 sequence numbers are valid 928 iseqno.reset (ntohl (*(u32 *)&k[CHG_SEQNO] & 0x7fffffff)); // at least 2**31 sequence numbers are valid
779 929
780 sa = *ssa; 930 sa = *ssa;
781 931
782 next_rekey = now + ::conf.rekey; 932 rekey.set (NOW + ::conf.rekey);
783 next_wakeup (next_rekey); 933 keepalive.set (NOW + ::conf.keepalive);
784 934
785 // send queued packets 935 // send queued packets
786 while (tap_packet *p = queue.get ()) 936 while (tap_packet *p = queue.get ())
787 { 937 {
788 send_data_packet (p); 938 send_data_packet (p);
929 send_reset (ssa); 1079 send_reset (ssa);
930 break; 1080 break;
931 } 1081 }
932} 1082}
933 1083
934void connection::timer () 1084void connection::keepalive_cb (tstamp &ts)
935{ 1085{
936 if (conf != THISNODE) 1086 if (NOW >= last_activity + ::conf.keepalive + 30)
937 { 1087 {
938 if (now >= next_retry && connectmode == conf_node::C_ALWAYS) 1088 reset_connection ();
939 establish_connection (); 1089 establish_connection ();
940 1090 }
941 if (ictx && octx)
942 {
943 if (now >= next_rekey)
944 rekey ();
945 else if (now >= last_activity + ::conf.keepalive + 30)
946 {
947 reset_connection ();
948 establish_connection ();
949 }
950 else if (now >= last_activity + ::conf.keepalive) 1091 else if (NOW < last_activity + ::conf.keepalive)
1092 ts = last_activity + ::conf.keepalive;
951 if (conf->connectmode != conf_node::C_ONDEMAND 1093 else if (conf->connectmode != conf_node::C_ONDEMAND
952 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1094 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1095 {
953 send_ping (&sa); 1096 send_ping (&sa);
954 else 1097 ts = NOW + 5;
1098 }
1099 else
955 reset_connection (); 1100 reset_connection ();
956 1101
957 }
958 }
959} 1102}
960 1103
961void connection::connect_request (int id) 1104void connection::connect_request (int id)
962{ 1105{
963 connect_req_packet *p = new connect_req_packet (conf->id, id); 1106 connect_req_packet *p = new connect_req_packet (conf->id, id);
1000 putenv ("STATE=down"); 1143 putenv ("STATE=down");
1001 1144
1002 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1145 return ::conf.script_node_up ? ::conf.script_node_down : "node-down";
1003} 1146}
1004 1147
1148connection::connection(struct vpn *vpn_)
1149: vpn(vpn_)
1150, rekey (this, &connection::rekey_cb)
1151, keepalive (this, &connection::keepalive_cb)
1152, establish_connection (this, &connection::establish_connection_cb)
1153{
1154 octx = ictx = 0;
1155 retry_cnt = 0;
1156
1157 connectmode = conf_node::C_ALWAYS; // initial setting
1158 reset_connection ();
1159}
1160
1161connection::~connection ()
1162{
1163 shutdown ();
1164}
1165
1005///////////////////////////////////////////////////////////////////////////// 1166/////////////////////////////////////////////////////////////////////////////
1006
1007vpn::vpn (void)
1008{}
1009 1167
1010const char *vpn::script_if_up () 1168const char *vpn::script_if_up ()
1011{ 1169{
1012 // the tunnel device mtu should be the physical mtu - overhead 1170 // the tunnel device mtu should be the physical mtu - overhead
1013 // the tricky part is rounding to the cipher key blocksize 1171 // the tricky part is rounding to the cipher key blocksize
1064 { 1222 {
1065 int oval = 1; 1223 int oval = 1;
1066 setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); 1224 setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
1067 } 1225 }
1068 1226
1227 udp_ev_watcher.start (socket_fd, POLLIN);
1228
1069 tap = new tap_device (); 1229 tap = new tap_device ();
1070 if (!tap) //D this, of course, never catches 1230 if (!tap) //D this, of course, never catches
1071 { 1231 {
1072 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 1232 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
1073 exit (1); 1233 exit (1);
1074 } 1234 }
1075 1235
1076 run_script (this, &vpn::script_if_up, true); 1236 run_script (this, &vpn::script_if_up, true);
1237
1238 vpn_ev_watcher.start (tap->fd, POLLIN);
1239
1240 reconnect_all ();
1077 1241
1078 return 0; 1242 return 0;
1079} 1243}
1080 1244
1081void 1245void
1146 // if ((*i)->conf->routerprio) 1310 // if ((*i)->conf->routerprio)
1147 // (*i)->establish_connection (); 1311 // (*i)->establish_connection ();
1148} 1312}
1149 1313
1150void 1314void
1151vpn::main_loop () 1315vpn::udp_ev (short revents)
1152{ 1316{
1153 struct pollfd pollfd[2]; 1317 if (revents & (POLLIN | POLLERR))
1154
1155 pollfd[0].fd = tap->fd;
1156 pollfd[0].events = POLLIN;
1157 pollfd[1].fd = socket_fd;
1158 pollfd[1].events = POLLIN;
1159
1160 events = 0;
1161 now = time (0);
1162 next_timecheck = now + 1;
1163
1164 reconnect_all ();
1165
1166 for (;;)
1167 { 1318 {
1168 int npoll = poll (pollfd, 2, (next_timecheck - now) * 1000); 1319 vpn_packet *pkt = new vpn_packet;
1169 1320 struct sockaddr_in sa;
1170 now = time (0); 1321 socklen_t sa_len = sizeof (sa);
1322 int len;
1171 1323
1324 len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
1325
1172 if (npoll > 0) 1326 if (len > 0)
1327 {
1328 pkt->len = len;
1329
1330 unsigned int src = pkt->src ();
1331 unsigned int dst = pkt->dst ();
1332
1333 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"),
1334 (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
1335
1336 if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX)
1337 slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"),
1338 pkt->typ (), pkt->src (), pkt->dst ());
1339 else if (dst == 0 && !THISNODE->routerprio)
1340 slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst);
1341 else if (dst != 0 && dst != THISNODE->id)
1342 slog (L_WARN,
1343 _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"),
1344 dst, conns[dst - 1]->conf->nodename,
1345 (const char *)sockinfo (sa),
1346 THISNODE->id, THISNODE->nodename);
1347 else if (src == 0 || src > conns.size ())
1348 slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa));
1349 else
1350 conns[src - 1]->recv_vpn_packet (pkt, &sa);
1173 { 1351 }
1174 if (pollfd[1].revents) 1352 else
1353 {
1354 // probably ECONNRESET or somesuch
1355 slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno));
1356 }
1357
1358 delete pkt;
1359 }
1360 else if (revents & POLLHUP)
1361 {
1362 // this cannot ;) happen on udp sockets
1363 slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating."));
1364 exit (1);
1365 }
1366 else
1367 {
1368 slog (L_ERR,
1369 _("FATAL: unknown revents %08x in socket, terminating\n"),
1370 revents);
1371 exit (1);
1372 }
1373}
1374
1375void
1376vpn::vpn_ev (short revents)
1377{
1378 if (revents & POLLIN)
1379 {
1380 /* process data */
1381 tap_packet *pkt;
1382
1383 pkt = tap->recv ();
1384
1385 int dst = mac2id (pkt->dst);
1386 int src = mac2id (pkt->src);
1387
1388 if (src != THISNODE->id)
1389 {
1390 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating."));
1391 exit (1);
1392 }
1393
1394 if (dst == THISNODE->id)
1395 {
1396 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating."));
1397 exit (1);
1398 }
1399
1400 if (dst > conns.size ())
1401 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
1402 else
1403 {
1404 if (dst)
1175 { 1405 {
1176 if (pollfd[1].revents & (POLLIN | POLLERR)) 1406 // unicast
1177 {
1178 vpn_packet *pkt = new vpn_packet;
1179 struct sockaddr_in sa;
1180 socklen_t sa_len = sizeof (sa);
1181 int len;
1182
1183 len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
1184
1185 if (len > 0)
1186 {
1187 pkt->len = len;
1188
1189 unsigned int src = pkt->src ();
1190 unsigned int dst = pkt->dst ();
1191
1192 slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"),
1193 (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len);
1194
1195 if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX)
1196 slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"),
1197 pkt->typ (), pkt->src (), pkt->dst ());
1198 else if (dst == 0 && !THISNODE->routerprio)
1199 slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst);
1200 else if (dst != 0 && dst != THISNODE->id) 1407 if (dst != THISNODE->id)
1201 slog (L_WARN,
1202 _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"),
1203 dst, conns[dst - 1]->conf->nodename,
1204 (const char *)sockinfo (sa),
1205 THISNODE->id, THISNODE->nodename);
1206 else if (src == 0 || src > conns.size ())
1207 slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa));
1208 else
1209 conns[src - 1]->recv_vpn_packet (pkt, &sa); 1408 conns[dst - 1]->inject_data_packet (pkt);
1210 }
1211 else
1212 {
1213 // probably ECONNRESET or somesuch
1214 slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno));
1215 }
1216
1217 delete pkt;
1218 } 1409 }
1219 else if (pollfd[1].revents & POLLHUP) 1410 else
1220 { 1411 {
1221 // this cannot ;) happen on udp sockets 1412 // broadcast, first check router, then self, then english
1222 slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); 1413 connection *router = find_router ();
1223 exit (1); 1414
1224 } 1415 if (router)
1416 router->inject_data_packet (pkt, true);
1225 else 1417 else
1226 { 1418 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
1227 slog (L_ERR, 1419 if ((*c)->conf != THISNODE)
1228 _("FATAL: unknown revents %08x in socket, terminating\n"), 1420 (*c)->inject_data_packet (pkt);
1229 pollfd[1].revents);
1230 exit (1);
1231 }
1232 } 1421 }
1422 }
1233 1423
1234 // I use else here to give vpn_packets absolute priority 1424 delete pkt;
1235 else if (pollfd[0].revents) 1425 }
1236 {
1237 if (pollfd[0].revents & POLLIN)
1238 {
1239 /* process data */
1240 tap_packet *pkt;
1241
1242 pkt = tap->recv ();
1243
1244 int dst = mac2id (pkt->dst);
1245 int src = mac2id (pkt->src);
1246
1247 if (src != THISNODE->id)
1248 {
1249 slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating."));
1250 exit (1);
1251 }
1252
1253 if (dst == THISNODE->id)
1254 {
1255 slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating."));
1256 exit (1);
1257 }
1258
1259 if (dst > conns.size ())
1260 slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst);
1261 else
1262 {
1263 if (dst)
1264 {
1265 // unicast
1266 if (dst != THISNODE->id)
1267 conns[dst - 1]->inject_data_packet (pkt);
1268 }
1269 else
1270 {
1271 // broadcast, first check router, then self, then english
1272 connection *router = find_router ();
1273
1274 if (router)
1275 router->inject_data_packet (pkt, true);
1276 else
1277 for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c)
1278 if ((*c)->conf != THISNODE)
1279 (*c)->inject_data_packet (pkt);
1280 }
1281 }
1282
1283 delete pkt;
1284 }
1285 else if (pollfd[0].revents & (POLLHUP | POLLERR)) 1426 else if (revents & (POLLHUP | POLLERR))
1286 { 1427 {
1287 slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); 1428 slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating."));
1288 exit (1); 1429 exit (1);
1289 } 1430 }
1290 else 1431 else
1291 abort (); 1432 abort ();
1292 } 1433}
1293 }
1294 1434
1435void
1436vpn::event_cb (tstamp &ts)
1437{
1295 if (events) 1438 if (events)
1296 { 1439 {
1297 if (events & EVENT_SHUTDOWN) 1440 if (events & EVENT_SHUTDOWN)
1298 { 1441 {
1299 shutdown_all (); 1442 shutdown_all ();
1300 1443
1301 remove_pid (pidfilename); 1444 remove_pid (pidfilename);
1302 1445
1303 slog (L_INFO, _("vped terminating")); 1446 slog (L_INFO, _("vped terminating"));
1304 1447
1305 exit (0); 1448 exit (0);
1306 } 1449 }
1307 1450
1308 if (events & EVENT_RECONNECT) 1451 if (events & EVENT_RECONNECT)
1309 reconnect_all (); 1452 reconnect_all ();
1310 1453
1311 events = 0; 1454 events = 0;
1312 }
1313
1314 // very very very dumb and crude and inefficient timer handling, or maybe not?
1315 if (now >= next_timecheck)
1316 {
1317 next_timecheck = now + TIMER_GRANULARITY;
1318
1319 for (conns_vector::iterator c = conns.begin ();
1320 c != conns.end (); ++c)
1321 (*c)->timer ();
1322 }
1323 } 1455 }
1456
1457 ts = TSTAMP_CANCEL;
1458}
1459
1460vpn::vpn (void)
1461: udp_ev_watcher (this, &vpn::udp_ev)
1462, vpn_ev_watcher (this, &vpn::vpn_ev)
1463, event (this, &vpn::event_cb)
1464{
1324} 1465}
1325 1466
1326vpn::~vpn () 1467vpn::~vpn ()
1327{} 1468{
1469}
1328 1470

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines