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.21 by pcg, Thu Oct 16 02:28:36 2003 UTC vs.
Revision 1.27 by pcg, Sun Jan 25 21:47:14 2004 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com>
3 4
4 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 8 (at your option) any later version.
20 21
21extern "C" { 22extern "C" {
22# include "lzf/lzf.h" 23# include "lzf/lzf.h"
23} 24}
24 25
26#include <cassert>
27
25#include <list> 28#include <list>
26 29
27#include <openssl/rand.h> 30#include <openssl/rand.h>
28#include <openssl/evp.h> 31#include <openssl/evp.h>
29#include <openssl/rsa.h> 32#include <openssl/rsa.h>
133 136
134} rsa_cache; 137} rsa_cache;
135 138
136void rsa_cache::cleaner_cb (time_watcher &w) 139void rsa_cache::cleaner_cb (time_watcher &w)
137{ 140{
138 if (empty ()) 141 if (!empty ())
139 w.at = TSTAMP_CANCEL;
140 else
141 { 142 {
142 w.at = NOW + RSA_TTL; 143 w.start (NOW + RSA_TTL);
143 144
144 for (iterator i = begin (); i != end (); ) 145 for (iterator i = begin (); i != end (); )
145 if (i->expire <= NOW) 146 if (i->expire <= NOW)
146 i = erase (i); 147 i = erase (i);
147 else 148 else
478} 479}
479 480
480bool config_packet::chk_config () const 481bool config_packet::chk_config () const
481{ 482{
482 if (prot_major != PROTOCOL_MAJOR) 483 if (prot_major != PROTOCOL_MAJOR)
483 slog (L_WARN, _("major version mismatch (%d <=> %d)"), prot_major, PROTOCOL_MAJOR); 484 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
484 else if (randsize != RAND_SIZE) 485 else if (randsize != RAND_SIZE)
485 slog (L_WARN, _("rand size mismatch (%d <=> %d)"), randsize, RAND_SIZE); 486 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
486 else if (hmaclen != HMACLENGTH) 487 else if (hmaclen != HMACLENGTH)
487 slog (L_WARN, _("hmac length mismatch (%d <=> %d)"), hmaclen, HMACLENGTH); 488 slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH);
488 else if (flags != curflags ()) 489 else if (flags != curflags ())
489 slog (L_WARN, _("flag mismatch (%x <=> %x)"), flags, curflags ()); 490 slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ());
490 else if (challengelen != sizeof (rsachallenge)) 491 else if (challengelen != sizeof (rsachallenge))
491 slog (L_WARN, _("challenge length mismatch (%d <=> %d)"), challengelen, sizeof (rsachallenge)); 492 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge));
492 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) 493 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER)))
493 slog (L_WARN, _("cipher mismatch (%x <=> %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); 494 slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER));
494 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) 495 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH)))
495 slog (L_WARN, _("digest mismatch (%x <=> %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); 496 slog (L_WARN, _("digest mismatch (remote %x <=> local %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH));
496 else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) 497 else if (hmac_nid != htonl (EVP_MD_type (DIGEST)))
497 slog (L_WARN, _("hmac mismatch (%x <=> %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); 498 slog (L_WARN, _("hmac mismatch (remote %x <=> local %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST));
498 else 499 else
499 return true; 500 return true;
500 501
501 return false; 502 return false;
502} 503}
597 } 598 }
598 else 599 else
599 { 600 {
600 retry_cnt = 0; 601 retry_cnt = 0;
601 establish_connection.start (NOW + 5); 602 establish_connection.start (NOW + 5);
602 keepalive.reset (); 603 keepalive.stop ();
603 rekey.reset (); 604 rekey.stop ();
604 } 605 }
605} 606}
606 607
607void 608void
608connection::reset_si () 609connection::reset_si ()
674connection::send_auth_request (const sockinfo &si, bool initiate) 675connection::send_auth_request (const sockinfo &si, bool initiate)
675{ 676{
676 auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); 677 auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols);
677 678
678 rsachallenge chg; 679 rsachallenge chg;
679
680 rsa_cache.gen (pkt->id, chg); 680 rsa_cache.gen (pkt->id, chg);
681 681 rsa_encrypt (conf->rsa_key, chg, pkt->encr);
682 if (0 > RSA_public_encrypt (sizeof chg,
683 (unsigned char *)&chg, (unsigned char *)&pkt->encr,
684 conf->rsa_key, RSA_PKCS1_OAEP_PADDING))
685 fatal ("RSA_public_encrypt error");
686 682
687 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si); 683 slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)si);
688 684
689 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly 685 send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly
690 686
724} 720}
725 721
726void 722void
727connection::establish_connection_cb (time_watcher &w) 723connection::establish_connection_cb (time_watcher &w)
728{ 724{
729 if (ictx || conf == THISNODE 725 if (!ictx
726 && conf != THISNODE
730 || connectmode == conf_node::C_NEVER 727 && connectmode != conf_node::C_NEVER
731 || connectmode == conf_node::C_DISABLED) 728 && connectmode != conf_node::C_DISABLED
732 w.at = TSTAMP_CANCEL; 729 && NOW > w.at)
733 else if (w.at <= NOW)
734 { 730 {
735 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; 731 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6;
736 732
737 if (retry_int < 3600 * 8) 733 if (retry_int < 3600 * 8)
738 retry_cnt++; 734 retry_cnt++;
739 735
740 w.at = NOW + retry_int; 736 w.start (NOW + retry_int);
741 737
742 reset_si (); 738 reset_si ();
743 739
744 if (si.prot && !si.host) 740 if (si.prot && !si.host)
745 vpn->send_connect_request (conf->id); 741 vpn->send_connect_request (conf->id);
776 si.host= 0; 772 si.host= 0;
777 773
778 last_activity = 0; 774 last_activity = 0;
779 retry_cnt = 0; 775 retry_cnt = 0;
780 776
781 rekey.reset (); 777 rekey.stop ();
782 keepalive.reset (); 778 keepalive.stop ();
783 establish_connection.reset (); 779 establish_connection.stop ();
784} 780}
785 781
786void 782void
787connection::shutdown () 783connection::shutdown ()
788{ 784{
793} 789}
794 790
795void 791void
796connection::rekey_cb (time_watcher &w) 792connection::rekey_cb (time_watcher &w)
797{ 793{
798 w.at = TSTAMP_CANCEL;
799
800 reset_connection (); 794 reset_connection ();
801 establish_connection (); 795 establish_connection ();
802} 796}
803 797
804void 798void
907 if (p->initiate) 901 if (p->initiate)
908 send_auth_request (rsi, false); 902 send_auth_request (rsi, false);
909 903
910 rsachallenge k; 904 rsachallenge k;
911 905
912 if (0 > RSA_private_decrypt (sizeof (p->encr), 906 if (!rsa_decrypt (::conf.rsa_key, p->encr, k))
913 (unsigned char *)&p->encr, (unsigned char *)&k, 907 {
914 ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING))
915 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), 908 slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"),
916 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); 909 conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0));
910 break;
911 }
917 else 912 else
918 { 913 {
919 delete octx; 914 delete octx;
920 915
921 octx = new crypto_ctx (k, 1); 916 octx = new crypto_ctx (k, 1);
965 crypto_ctx *cctx = new crypto_ctx (chg, 0); 960 crypto_ctx *cctx = new crypto_ctx (chg, 0);
966 961
967 if (!p->hmac_chk (cctx)) 962 if (!p->hmac_chk (cctx))
968 { 963 {
969 slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" 964 slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n"
970 "could be an attack, or just corruption or an synchronization error"), 965 "could be an attack, or just corruption or a synchronization error"),
971 conf->nodename, (const char *)rsi); 966 conf->nodename, (const char *)rsi);
972 break; 967 break;
973 } 968 }
974 else 969 else
975 { 970 {
1024 { 1019 {
1025 vpndata_packet *p = (vpndata_packet *)pkt; 1020 vpndata_packet *p = (vpndata_packet *)pkt;
1026 1021
1027 if (!p->hmac_chk (ictx)) 1022 if (!p->hmac_chk (ictx))
1028 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" 1023 slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n"
1029 "could be an attack, or just corruption or an synchronization error"), 1024 "could be an attack, or just corruption or a synchronization error"),
1030 conf->nodename, (const char *)rsi); 1025 conf->nodename, (const char *)rsi);
1031 else 1026 else
1032 { 1027 {
1033 u32 seqno; 1028 u32 seqno;
1034 tap_packet *d = p->unpack (this, seqno); 1029 tap_packet *d = p->unpack (this, seqno);
1117 { 1112 {
1118 reset_connection (); 1113 reset_connection ();
1119 establish_connection (); 1114 establish_connection ();
1120 } 1115 }
1121 else if (NOW < last_activity + ::conf.keepalive) 1116 else if (NOW < last_activity + ::conf.keepalive)
1122 w.at = last_activity + ::conf.keepalive; 1117 w.start (last_activity + ::conf.keepalive);
1123 else if (conf->connectmode != conf_node::C_ONDEMAND 1118 else if (conf->connectmode != conf_node::C_ONDEMAND
1124 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1119 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1125 { 1120 {
1126 send_ping (si); 1121 send_ping (si);
1127 w.at = NOW + 5; 1122 w.start (NOW + 5);
1128 } 1123 }
1129 else if (NOW < last_activity + ::conf.keepalive + 10) 1124 else if (NOW < last_activity + ::conf.keepalive + 10)
1130 // hold ondemand connections implicitly a few seconds longer 1125 // hold ondemand connections implicitly a few seconds longer
1131 // should delete octx, though, or something like that ;) 1126 // should delete octx, though, or something like that ;)
1132 w.at = last_activity + ::conf.keepalive + 10; 1127 w.start (last_activity + ::conf.keepalive + 10);
1133 else 1128 else
1134 reset_connection (); 1129 reset_connection ();
1135} 1130}
1136 1131
1137void connection::send_connect_request (int id) 1132void connection::send_connect_request (int id)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines