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.20 by pcg, Tue Oct 14 15:48:15 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
805connection::send_data_packet (tap_packet *pkt, bool broadcast) 799connection::send_data_packet (tap_packet *pkt)
806{ 800{
807 vpndata_packet *p = new vpndata_packet; 801 vpndata_packet *p = new vpndata_packet;
808 int tos = 0; 802 int tos = 0;
809 803
810 // I am not hilarious about peeking into packets, but so be it. 804 // I am not hilarious about peeking into packets, but so be it.
811 if (conf->inherit_tos 805 if (conf->inherit_tos && pkt->is_ipv4 ())
812 && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP
813 && ((*pkt)[14] & 0xf0) == 0x40) // IPv4
814 tos = (*pkt)[15] & IPTOS_TOS_MASK; 806 tos = (*pkt)[15] & IPTOS_TOS_MASK;
815 807
816 p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs 808 p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs
817 send_vpn_packet (p, si, tos); 809 send_vpn_packet (p, si, tos);
818 810
819 delete p; 811 delete p;
820 812
821 if (oseqno > MAX_SEQNO) 813 if (oseqno > MAX_SEQNO)
822 rekey (); 814 rekey ();
823} 815}
824 816
825void 817void
826connection::inject_data_packet (tap_packet *pkt, bool broadcast) 818connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/)
827{ 819{
828 if (ictx && octx) 820 if (ictx && octx)
829 send_data_packet (pkt, broadcast); 821 send_data_packet (pkt);
830 else 822 else
831 { 823 {
832 if (!broadcast)//DDDD 824 if (!broadcast)//DDDD
833 data_queue.put (new tap_packet (*pkt)); 825 data_queue.put (new tap_packet (*pkt));
834 826
909 if (p->initiate) 901 if (p->initiate)
910 send_auth_request (rsi, false); 902 send_auth_request (rsi, false);
911 903
912 rsachallenge k; 904 rsachallenge k;
913 905
914 if (0 > RSA_private_decrypt (sizeof (p->encr), 906 if (!rsa_decrypt (::conf.rsa_key, p->encr, k))
915 (unsigned char *)&p->encr, (unsigned char *)&k, 907 {
916 ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING))
917 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?"),
918 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 }
919 else 912 else
920 { 913 {
921 delete octx; 914 delete octx;
922 915
923 octx = new crypto_ctx (k, 1); 916 octx = new crypto_ctx (k, 1);
967 crypto_ctx *cctx = new crypto_ctx (chg, 0); 960 crypto_ctx *cctx = new crypto_ctx (chg, 0);
968 961
969 if (!p->hmac_chk (cctx)) 962 if (!p->hmac_chk (cctx))
970 { 963 {
971 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"
972 "could be an attack, or just corruption or an synchronization error"), 965 "could be an attack, or just corruption or a synchronization error"),
973 conf->nodename, (const char *)rsi); 966 conf->nodename, (const char *)rsi);
974 break; 967 break;
975 } 968 }
976 else 969 else
977 { 970 {
1026 { 1019 {
1027 vpndata_packet *p = (vpndata_packet *)pkt; 1020 vpndata_packet *p = (vpndata_packet *)pkt;
1028 1021
1029 if (!p->hmac_chk (ictx)) 1022 if (!p->hmac_chk (ictx))
1030 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"
1031 "could be an attack, or just corruption or an synchronization error"), 1024 "could be an attack, or just corruption or a synchronization error"),
1032 conf->nodename, (const char *)rsi); 1025 conf->nodename, (const char *)rsi);
1033 else 1026 else
1034 { 1027 {
1035 u32 seqno; 1028 u32 seqno;
1036 tap_packet *d = p->unpack (this, seqno); 1029 tap_packet *d = p->unpack (this, seqno);
1037 1030
1038 if (iseqno.recv_ok (seqno)) 1031 if (iseqno.recv_ok (seqno))
1039 { 1032 {
1040 vpn->tap->send (d); 1033 vpn->tap->send (d);
1041
1042 if (p->dst () == 0) // re-broadcast
1043 for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i)
1044 {
1045 connection *c = *i;
1046
1047 if (c->conf != THISNODE && c->conf != conf)
1048 c->inject_data_packet (d);
1049 }
1050 1034
1051 if (si != rsi) 1035 if (si != rsi)
1052 { 1036 {
1053 // fast re-sync on connection changes, useful especially for tcp/ip 1037 // fast re-sync on connection changes, useful especially for tcp/ip
1054 si = rsi; 1038 si = rsi;
1128 { 1112 {
1129 reset_connection (); 1113 reset_connection ();
1130 establish_connection (); 1114 establish_connection ();
1131 } 1115 }
1132 else if (NOW < last_activity + ::conf.keepalive) 1116 else if (NOW < last_activity + ::conf.keepalive)
1133 w.at = last_activity + ::conf.keepalive; 1117 w.start (last_activity + ::conf.keepalive);
1134 else if (conf->connectmode != conf_node::C_ONDEMAND 1118 else if (conf->connectmode != conf_node::C_ONDEMAND
1135 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1119 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1136 { 1120 {
1137 send_ping (si); 1121 send_ping (si);
1138 w.at = NOW + 5; 1122 w.start (NOW + 5);
1139 } 1123 }
1140 else if (NOW < last_activity + ::conf.keepalive + 10) 1124 else if (NOW < last_activity + ::conf.keepalive + 10)
1141 // hold ondemand connections implicitly a few seconds longer 1125 // hold ondemand connections implicitly a few seconds longer
1142 // should delete octx, though, or something like that ;) 1126 // should delete octx, though, or something like that ;)
1143 w.at = last_activity + ::conf.keepalive + 10; 1127 w.start (last_activity + ::conf.keepalive + 10);
1144 else 1128 else
1145 reset_connection (); 1129 reset_connection ();
1146} 1130}
1147 1131
1148void connection::send_connect_request (int id) 1132void connection::send_connect_request (int id)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines