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.37 by pcg, Fri Jun 11 15:56:34 2004 UTC vs.
Revision 1.50 by pcg, Fri Mar 18 01:53:05 2005 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 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 8 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 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 15 GNU General Public License for more details.
14 16
15 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/ 20*/
19 21
20#include "config.h" 22#include "config.h"
21 23
25 27
26#include <openssl/rand.h> 28#include <openssl/rand.h>
27#include <openssl/evp.h> 29#include <openssl/evp.h>
28#include <openssl/rsa.h> 30#include <openssl/rsa.h>
29#include <openssl/err.h> 31#include <openssl/err.h>
30
31#include "gettext.h"
32 32
33#include "conf.h" 33#include "conf.h"
34#include "slog.h" 34#include "slog.h"
35#include "device.h" 35#include "device.h"
36#include "vpn.h" 36#include "vpn.h"
476{ 476{
477 prot_major = PROTOCOL_MAJOR; 477 prot_major = PROTOCOL_MAJOR;
478 prot_minor = PROTOCOL_MINOR; 478 prot_minor = PROTOCOL_MINOR;
479 randsize = RAND_SIZE; 479 randsize = RAND_SIZE;
480 hmaclen = HMACLENGTH; 480 hmaclen = HMACLENGTH;
481 flags = ENABLE_COMPRESSION ? 0x81 : 0x80; 481 flags = 0;
482 challengelen = sizeof (rsachallenge); 482 challengelen = sizeof (rsachallenge);
483 features = get_features (); 483 features = get_features ();
484 484
485 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); 485 cipher_nid = htonl (EVP_CIPHER_nid (CIPHER));
486 digest_nid = htonl (EVP_MD_type (RSA_HASH)); 486 digest_nid = htonl (EVP_MD_type (RSA_HASH));
496 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 496 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
497 else if (randsize != RAND_SIZE) 497 else if (randsize != RAND_SIZE)
498 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 498 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
499 else if (hmaclen != HMACLENGTH) 499 else if (hmaclen != HMACLENGTH)
500 slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); 500 slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH);
501#if 0 // this implementation should handle all flag settings
502 else if (flags != curflags ())
503 slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ());
504#endif
505 else if (challengelen != sizeof (rsachallenge)) 501 else if (challengelen != sizeof (rsachallenge))
506 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); 502 slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge));
507 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) 503 else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER)))
508 slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); 504 slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER));
509 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) 505 else if (digest_nid != htonl (EVP_MD_type (RSA_HASH)))
627 protocol = best_protocol (THISNODE->protocols & conf->protocols); 623 protocol = best_protocol (THISNODE->protocols & conf->protocols);
628 624
629 // mask out protocols we cannot establish 625 // mask out protocols we cannot establish
630 if (!conf->udp_port) protocol &= ~PROT_UDPv4; 626 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
631 if (!conf->tcp_port) protocol &= ~PROT_TCPv4; 627 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
628 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
632 629
633 si.set (conf, protocol); 630 si.set (conf, protocol);
634} 631}
635 632
636// ensure sockinfo is valid, forward if necessary 633// ensure sockinfo is valid, forward if necessary
641 { 638 {
642 connection *r = vpn->find_router (); 639 connection *r = vpn->find_router ();
643 640
644 if (r) 641 if (r)
645 { 642 {
646 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), 643 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"),
647 conf->nodename, r->conf->nodename); 644 conf->nodename, r->conf->nodename, (const char *)r->si);
648 return r->si; 645 return r->si;
649 } 646 }
650 else 647 else
651 slog (L_DEBUG, _("%s: node unreachable, no common protocol"), 648 slog (L_DEBUG, _("%s: node unreachable, no common protocol"),
652 conf->nodename); 649 conf->nodename);
742 && conf != THISNODE 739 && conf != THISNODE
743 && connectmode != conf_node::C_NEVER 740 && connectmode != conf_node::C_NEVER
744 && connectmode != conf_node::C_DISABLED 741 && connectmode != conf_node::C_DISABLED
745 && NOW > w.at) 742 && NOW > w.at)
746 { 743 {
747 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; 744 w.at = TSTAMP_MAX; // first disable this watcher in case of recursion
748 745
749 if (retry_int < conf->max_retry) 746 double retry_int = double (retry_cnt & 3
750 retry_cnt++; 747 ? (retry_cnt & 3) + 1
751 else 748 : 1 << (retry_cnt >> 2));
752 retry_int = conf->max_retry;
753
754 w.start (NOW + retry_int);
755 749
756 reset_si (); 750 reset_si ();
751
752 bool slow = si.prot & PROT_SLOW;
757 753
758 if (si.prot && !si.host) 754 if (si.prot && !si.host)
759 vpn->send_connect_request (conf->id); 755 vpn->send_connect_request (conf->id);
760 else 756 else
761 { 757 {
762 const sockinfo &dsi = forward_si (si); 758 const sockinfo &dsi = forward_si (si);
759
760 slow = slow || (dsi.prot & PROT_SLOW);
763 761
764 if (dsi.valid () && auth_rate_limiter.can (dsi)) 762 if (dsi.valid () && auth_rate_limiter.can (dsi))
765 { 763 {
766 if (retry_cnt < 4) 764 if (retry_cnt < 4)
767 send_auth_request (dsi, true); 765 send_auth_request (dsi, true);
768 else 766 else
769 send_ping (dsi, 0); 767 send_ping (dsi, 0);
770 } 768 }
771 } 769 }
770
771 retry_int *= slow ? 8. : 0.7;
772
773 if (retry_int < conf->max_retry)
774 retry_cnt++;
775 else
776 retry_int = conf->max_retry;
777
778 w.start (NOW + retry_int);
772 } 779 }
773} 780}
774 781
775void 782void
776connection::reset_connection () 783connection::reset_connection ()
784 run_script (run_script_cb (this, &connection::script_node_down), false); 791 run_script (run_script_cb (this, &connection::script_node_down), false);
785 } 792 }
786 793
787 delete ictx; ictx = 0; 794 delete ictx; ictx = 0;
788 delete octx; octx = 0; 795 delete octx; octx = 0;
796#if ENABLE_DNS
797 dnsv4_reset_connection ();
798#endif
789 799
790 si.host= 0; 800 si.host = 0;
791 801
792 last_activity = 0; 802 last_activity = 0;
793 retry_cnt = 0; 803 retry_cnt = 0;
794 804
795 rekey.stop (); 805 rekey.stop ();
932 delete octx; 942 delete octx;
933 943
934 octx = new crypto_ctx (k, 1); 944 octx = new crypto_ctx (k, 1);
935 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 945 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
936 946
937 // compatibility code, remove when no longer required
938 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
939
940 conf->protocols = p->protocols; 947 conf->protocols = p->protocols;
941 features = p->features & config_packet::get_features (); 948 features = p->features & config_packet::get_features ();
942 949
943 send_auth_response (rsi, p->id, k); 950 send_auth_response (rsi, p->id, k);
944 951
1098 break; 1105 break;
1099 1106
1100 case vpn_packet::PT_CONNECT_INFO: 1107 case vpn_packet::PT_CONNECT_INFO:
1101 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1108 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1102 { 1109 {
1103 connect_info_packet *p = (connect_info_packet *) pkt; 1110 connect_info_packet *p = (connect_info_packet *)pkt;
1104 1111
1105 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1112 if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything
1106 1113 {
1107 connection *c = vpn->conns[p->id - 1]; 1114 connection *c = vpn->conns[p->id - 1];
1108 1115
1109 c->conf->protocols = p->protocols; 1116 c->conf->protocols = p->protocols;
1110 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1117 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1111 p->si.upgrade_protocol (protocol, c->conf); 1118 p->si.upgrade_protocol (protocol, c->conf);
1112 1119
1113 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1120 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)",
1114 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1121 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx);
1115 1122
1116 const sockinfo &dsi = forward_si (p->si); 1123 const sockinfo &dsi = forward_si (p->si);
1117 1124
1118 if (dsi.valid ()) 1125 if (dsi.valid ())
1119 c->send_auth_request (dsi, true); 1126 c->send_auth_request (dsi, true);
1127 }
1120 } 1128 }
1121 1129
1122 break; 1130 break;
1123 1131
1124 default: 1132 default:
1188 putenv ("STATE=down"); 1196 putenv ("STATE=down");
1189 1197
1190 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1198 return ::conf.script_node_up ? ::conf.script_node_down : "node-down";
1191} 1199}
1192 1200
1193connection::connection(struct vpn *vpn_) 1201connection::connection (struct vpn *vpn, conf_node *conf)
1194: vpn(vpn_) 1202: vpn(vpn), conf(conf)
1195, rekey (this, &connection::rekey_cb) 1203, rekey (this, &connection::rekey_cb)
1196, keepalive (this, &connection::keepalive_cb) 1204, keepalive (this, &connection::keepalive_cb)
1197, establish_connection (this, &connection::establish_connection_cb) 1205, establish_connection (this, &connection::establish_connection_cb)
1206#if ENABLE_DNS
1207, dns (0)
1208#endif
1198{ 1209{
1199 octx = ictx = 0; 1210 octx = ictx = 0;
1200 retry_cnt = 0; 1211 retry_cnt = 0;
1201 1212
1213 if (!conf->protocols) // make sure some protocol is enabled
1214 conf->protocols = PROT_UDPv4;
1215
1202 connectmode = conf_node::C_ALWAYS; // initial setting 1216 connectmode = conf_node::C_ALWAYS; // initial setting
1203 reset_connection (); 1217 reset_connection ();
1204} 1218}
1205 1219
1206connection::~connection () 1220connection::~connection ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines