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.39 by pcg, Tue Oct 12 12:06:06 2004 UTC vs.
Revision 1.55 by pcg, Tue Apr 26 00:55:56 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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/ 20*/
19 21
20#include "config.h" 22#include "config.h"
21 23
22#include <cassert> 24#include <cassert>
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"
95struct rsa_cache : list<rsa_entry> 95struct rsa_cache : list<rsa_entry>
96{ 96{
97 void cleaner_cb (time_watcher &w); time_watcher cleaner; 97 void cleaner_cb (time_watcher &w); time_watcher cleaner;
98 98
99 bool find (const rsaid &id, rsachallenge &chg) 99 bool find (const rsaid &id, rsachallenge &chg)
100 { 100 {
101 for (iterator i = begin (); i != end (); ++i) 101 for (iterator i = begin (); i != end (); ++i)
102 { 102 {
103 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) 103 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW)
104 { 104 {
105 memcpy (&chg, &i->chg, sizeof chg); 105 memcpy (&chg, &i->chg, sizeof chg);
106 106
107 erase (i); 107 erase (i);
108 return true; 108 return true;
109 } 109 }
110 } 110 }
111 111
112 if (cleaner.at < NOW) 112 if (cleaner.at < NOW)
113 cleaner.start (NOW + RSA_TTL); 113 cleaner.start (NOW + RSA_TTL);
114 114
115 return false; 115 return false;
116 } 116 }
117 117
118 void gen (rsaid &id, rsachallenge &chg) 118 void gen (rsaid &id, rsachallenge &chg)
119 { 119 {
120 rsa_entry e; 120 rsa_entry e;
121 121
122 RAND_bytes ((unsigned char *)&id, sizeof id); 122 RAND_bytes ((unsigned char *)&id, sizeof id);
123 RAND_bytes ((unsigned char *)&chg, sizeof chg); 123 RAND_bytes ((unsigned char *)&chg, sizeof chg);
124 124
125 e.expire = NOW + RSA_TTL; 125 e.expire = NOW + RSA_TTL;
126 e.id = id; 126 e.id = id;
127 memcpy (&e.chg, &chg, sizeof chg); 127 memcpy (&e.chg, &chg, sizeof chg);
128 128
129 push_back (e); 129 push_back (e);
130 130
131 if (cleaner.at < NOW) 131 if (cleaner.at < NOW)
132 cleaner.start (NOW + RSA_TTL); 132 cleaner.start (NOW + RSA_TTL);
133 } 133 }
134 134
135 rsa_cache () 135 rsa_cache ()
136 : cleaner (this, &rsa_cache::cleaner_cb) 136 : cleaner (this, &rsa_cache::cleaner_cb)
137 { } 137 { }
138 138
139} rsa_cache; 139} rsa_cache;
140 140
141void rsa_cache::cleaner_cb (time_watcher &w) 141void rsa_cache::cleaner_cb (time_watcher &w)
142{ 142{
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 ()
779 { 786 {
780 slog (L_INFO, _("%s(%s): connection lost"), 787 slog (L_INFO, _("%s(%s): connection lost"),
781 conf->nodename, (const char *)si); 788 conf->nodename, (const char *)si);
782 789
783 if (::conf.script_node_down) 790 if (::conf.script_node_down)
784 run_script (run_script_cb (this, &connection::script_node_down), false); 791 if (!run_script (run_script_cb (this, &connection::script_node_down), false))
792 slog (L_WARN, _("node-down command execution failed, continuing."));
785 } 793 }
786 794
787 delete ictx; ictx = 0; 795 delete ictx; ictx = 0;
788 delete octx; octx = 0; 796 delete octx; octx = 0;
797#if ENABLE_DNS
798 dnsv4_reset_connection ();
799#endif
789 800
790 si.host = 0; 801 si.host = 0;
791 802
792 last_activity = 0; 803 last_activity = 0;
793 retry_cnt = 0; 804 retry_cnt = 0;
932 delete octx; 943 delete octx;
933 944
934 octx = new crypto_ctx (k, 1); 945 octx = new crypto_ctx (k, 1);
935 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 946 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
936 947
937 // compatibility code, remove when no longer required
938 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
939
940 conf->protocols = p->protocols; 948 conf->protocols = p->protocols;
941 features = p->features & config_packet::get_features (); 949 features = p->features & config_packet::get_features ();
942 950
943 send_auth_response (rsi, p->id, k); 951 send_auth_response (rsi, p->id, k);
944 952
1010 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1018 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1011 conf->nodename, (const char *)rsi, 1019 conf->nodename, (const char *)rsi,
1012 p->prot_major, p->prot_minor); 1020 p->prot_major, p->prot_minor);
1013 1021
1014 if (::conf.script_node_up) 1022 if (::conf.script_node_up)
1015 run_script (run_script_cb (this, &connection::script_node_up), false); 1023 if (!run_script (run_script_cb (this, &connection::script_node_up), false))
1024 slog (L_WARN, _("node-up command execution failed, continuing."));
1016 1025
1017 break; 1026 break;
1018 } 1027 }
1019 else 1028 else
1020 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1029 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1160 send_vpn_packet (p, si); 1169 send_vpn_packet (p, si);
1161 1170
1162 delete p; 1171 delete p;
1163} 1172}
1164 1173
1174void connection::script_init_env (const char *ext)
1175{
1176 char *env;
1177 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1178 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1179 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1180 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1181 conf->id & 0xff); putenv (env);
1182}
1183
1165void connection::script_node () 1184void connection::script_init_connect_env ()
1166{ 1185{
1167 vpn->script_if_up (); 1186 vpn->script_init_env ();
1168 1187
1169 char *env; 1188 char *env;
1170 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1189 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1171 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1190 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1172 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1191 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1173 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1192 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1174} 1193}
1175 1194
1176const char *connection::script_node_up () 1195const char *connection::script_node_up ()
1177{ 1196{
1178 script_node (); 1197 script_init_connect_env ();
1179 1198
1180 putenv ("STATE=up"); 1199 putenv ("STATE=up");
1181 1200
1201 char *filename;
1202 asprintf (&filename,
1203 "%s/%s",
1204 confbase,
1182 return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; 1205 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1206
1207 return filename;
1183} 1208}
1184 1209
1185const char *connection::script_node_down () 1210const char *connection::script_node_down ()
1186{ 1211{
1187 script_node (); 1212 script_init_connect_env ();
1188 1213
1189 putenv ("STATE=down"); 1214 putenv ("STATE=down");
1190 1215
1191 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1216 char *filename;
1192} 1217 asprintf (&filename,
1218 "%s/%s",
1219 confbase,
1220 ::conf.script_node_down ? ::conf.script_node_down : "node-down");
1193 1221
1222 return filename;
1223}
1224
1194connection::connection(struct vpn *vpn_) 1225connection::connection (struct vpn *vpn, conf_node *conf)
1195: vpn(vpn_) 1226: vpn(vpn), conf(conf)
1196, rekey (this, &connection::rekey_cb) 1227, rekey (this, &connection::rekey_cb)
1197, keepalive (this, &connection::keepalive_cb) 1228, keepalive (this, &connection::keepalive_cb)
1198, establish_connection (this, &connection::establish_connection_cb) 1229, establish_connection (this, &connection::establish_connection_cb)
1230#if ENABLE_DNS
1231, dns (0)
1232#endif
1199{ 1233{
1200 octx = ictx = 0; 1234 octx = ictx = 0;
1201 retry_cnt = 0; 1235 retry_cnt = 0;
1202 1236
1237 if (!conf->protocols) // make sure some protocol is enabled
1238 conf->protocols = PROT_UDPv4;
1239
1203 connectmode = conf_node::C_ALWAYS; // initial setting 1240 connectmode = conf_node::C_ALWAYS; // initial setting
1204 reset_connection (); 1241 reset_connection ();
1205} 1242}
1206 1243
1207connection::~connection () 1244connection::~connection ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines