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.24 by pcg, Wed Oct 22 01:05:23 2003 UTC vs.
Revision 1.28 by pcg, Tue Jan 27 05:56:35 2004 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com>
4 4
5 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
6 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
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
21 21
22extern "C" { 22extern "C" {
23# include "lzf/lzf.h" 23# include "lzf/lzf.h"
24} 24}
25 25
26#include <cassert>
27
26#include <list> 28#include <list>
27 29
28#include <openssl/rand.h> 30#include <openssl/rand.h>
29#include <openssl/evp.h> 31#include <openssl/evp.h>
30#include <openssl/rsa.h> 32#include <openssl/rsa.h>
134 136
135} rsa_cache; 137} rsa_cache;
136 138
137void rsa_cache::cleaner_cb (time_watcher &w) 139void rsa_cache::cleaner_cb (time_watcher &w)
138{ 140{
139 if (empty ()) 141 if (!empty ())
140 w.at = TSTAMP_CANCEL;
141 else
142 { 142 {
143 w.at = NOW + RSA_TTL; 143 w.start (NOW + RSA_TTL);
144 144
145 for (iterator i = begin (); i != end (); ) 145 for (iterator i = begin (); i != end (); )
146 if (i->expire <= NOW) 146 if (i->expire <= NOW)
147 i = erase (i); 147 i = erase (i);
148 else 148 else
310} 310}
311 311
312#define MAXVPNDATA (MAX_MTU - 6 - 6) 312#define MAXVPNDATA (MAX_MTU - 6 - 6)
313#define DATAHDR (sizeof (u32) + RAND_SIZE) 313#define DATAHDR (sizeof (u32) + RAND_SIZE)
314 314
315struct vpndata_packet:vpn_packet 315struct vpndata_packet : vpn_packet
316 { 316 {
317 u8 data[MAXVPNDATA + DATAHDR]; // seqno 317 u8 data[MAXVPNDATA + DATAHDR]; // seqno
318 318
319 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); 319 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
320 tap_packet *unpack (connection *conn, u32 &seqno); 320 tap_packet *unpack (connection *conn, u32 &seqno);
504 504
505struct auth_req_packet : config_packet 505struct auth_req_packet : config_packet
506{ 506{
507 char magic[8]; 507 char magic[8];
508 u8 initiate; // false if this is just an automatic reply 508 u8 initiate; // false if this is just an automatic reply
509 u8 protocols; // supported protocols (will get patches on forward) 509 u8 protocols; // supported protocols (will be patched on forward)
510 u8 pad2, pad3; 510 u8 pad2, pad3;
511 rsaid id; 511 rsaid id;
512 rsaencrdata encr; 512 rsaencrdata encr;
513 513
514 auth_req_packet (int dst, bool initiate_, u8 protocols_) 514 auth_req_packet (int dst, bool initiate_, u8 protocols_)
598 } 598 }
599 else 599 else
600 { 600 {
601 retry_cnt = 0; 601 retry_cnt = 0;
602 establish_connection.start (NOW + 5); 602 establish_connection.start (NOW + 5);
603 keepalive.reset (); 603 keepalive.stop ();
604 rekey.reset (); 604 rekey.stop ();
605 } 605 }
606} 606}
607 607
608void 608void
609connection::reset_si () 609connection::reset_si ()
720} 720}
721 721
722void 722void
723connection::establish_connection_cb (time_watcher &w) 723connection::establish_connection_cb (time_watcher &w)
724{ 724{
725 if (ictx || conf == THISNODE 725 if (!ictx
726 && conf != THISNODE
726 || connectmode == conf_node::C_NEVER 727 && connectmode != conf_node::C_NEVER
727 || connectmode == conf_node::C_DISABLED) 728 && connectmode != conf_node::C_DISABLED
728 w.at = TSTAMP_CANCEL; 729 && NOW > w.at)
729 else if (w.at <= NOW)
730 { 730 {
731 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;
732 732
733 if (retry_int < 3600 * 8) 733 if (retry_int < 3600 * 8)
734 retry_cnt++; 734 retry_cnt++;
735 735
736 w.at = NOW + retry_int; 736 w.start (NOW + retry_int);
737 737
738 reset_si (); 738 reset_si ();
739 739
740 if (si.prot && !si.host) 740 if (si.prot && !si.host)
741 vpn->send_connect_request (conf->id); 741 vpn->send_connect_request (conf->id);
772 si.host= 0; 772 si.host= 0;
773 773
774 last_activity = 0; 774 last_activity = 0;
775 retry_cnt = 0; 775 retry_cnt = 0;
776 776
777 rekey.reset (); 777 rekey.stop ();
778 keepalive.reset (); 778 keepalive.stop ();
779 establish_connection.reset (); 779 establish_connection.stop ();
780} 780}
781 781
782void 782void
783connection::shutdown () 783connection::shutdown ()
784{ 784{
789} 789}
790 790
791void 791void
792connection::rekey_cb (time_watcher &w) 792connection::rekey_cb (time_watcher &w)
793{ 793{
794 w.at = TSTAMP_CANCEL;
795
796 reset_connection (); 794 reset_connection ();
797 establish_connection (); 795 establish_connection ();
798} 796}
799 797
800void 798void
834{ 832{
835 if (ictx && octx) 833 if (ictx && octx)
836 send_vpn_packet (pkt, si, tos); 834 send_vpn_packet (pkt, si, tos);
837 else 835 else
838 { 836 {
839 vpn_queue.put (new vpn_packet (*pkt)); 837 vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt));
840 838
841 establish_connection (); 839 establish_connection ();
842 } 840 }
843} 841}
844 842
962 crypto_ctx *cctx = new crypto_ctx (chg, 0); 960 crypto_ctx *cctx = new crypto_ctx (chg, 0);
963 961
964 if (!p->hmac_chk (cctx)) 962 if (!p->hmac_chk (cctx))
965 { 963 {
966 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"
967 "could be an attack, or just corruption or an synchronization error"), 965 "could be an attack, or just corruption or a synchronization error"),
968 conf->nodename, (const char *)rsi); 966 conf->nodename, (const char *)rsi);
969 break; 967 break;
970 } 968 }
971 else 969 else
972 { 970 {
1021 { 1019 {
1022 vpndata_packet *p = (vpndata_packet *)pkt; 1020 vpndata_packet *p = (vpndata_packet *)pkt;
1023 1021
1024 if (!p->hmac_chk (ictx)) 1022 if (!p->hmac_chk (ictx))
1025 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"
1026 "could be an attack, or just corruption or an synchronization error"), 1024 "could be an attack, or just corruption or a synchronization error"),
1027 conf->nodename, (const char *)rsi); 1025 conf->nodename, (const char *)rsi);
1028 else 1026 else
1029 { 1027 {
1030 u32 seqno; 1028 u32 seqno;
1031 tap_packet *d = p->unpack (this, seqno); 1029 tap_packet *d = p->unpack (this, seqno);
1114 { 1112 {
1115 reset_connection (); 1113 reset_connection ();
1116 establish_connection (); 1114 establish_connection ();
1117 } 1115 }
1118 else if (NOW < last_activity + ::conf.keepalive) 1116 else if (NOW < last_activity + ::conf.keepalive)
1119 w.at = last_activity + ::conf.keepalive; 1117 w.start (last_activity + ::conf.keepalive);
1120 else if (conf->connectmode != conf_node::C_ONDEMAND 1118 else if (conf->connectmode != conf_node::C_ONDEMAND
1121 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1119 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1122 { 1120 {
1123 send_ping (si); 1121 send_ping (si);
1124 w.at = NOW + 5; 1122 w.start (NOW + 5);
1125 } 1123 }
1126 else if (NOW < last_activity + ::conf.keepalive + 10) 1124 else if (NOW < last_activity + ::conf.keepalive + 10)
1127 // hold ondemand connections implicitly a few seconds longer 1125 // hold ondemand connections implicitly a few seconds longer
1128 // should delete octx, though, or something like that ;) 1126 // should delete octx, though, or something like that ;)
1129 w.at = last_activity + ::conf.keepalive + 10; 1127 w.start (last_activity + ::conf.keepalive + 10);
1130 else 1128 else
1131 reset_connection (); 1129 reset_connection ();
1132} 1130}
1133 1131
1134void connection::send_connect_request (int id) 1132void connection::send_connect_request (int id)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines