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.41 by pcg, Wed Mar 2 05:49:31 2005 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)))
642 { 638 {
643 connection *r = vpn->find_router (); 639 connection *r = vpn->find_router ();
644 640
645 if (r) 641 if (r)
646 { 642 {
647 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s"), 643 slog (L_DEBUG, _("%s: no common protocol, trying indirectly through %s (%s)"),
648 conf->nodename, r->conf->nodename); 644 conf->nodename, r->conf->nodename, (const char *)r->si);
649 return r->si; 645 return r->si;
650 } 646 }
651 else 647 else
652 slog (L_DEBUG, _("%s: node unreachable, no common protocol"), 648 slog (L_DEBUG, _("%s: node unreachable, no common protocol"),
653 conf->nodename); 649 conf->nodename);
657} 653}
658 654
659void 655void
660connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 656connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
661{ 657{
662 bool ok; 658 if (!vpn->send_vpn_packet (pkt, si, tos))
663
664 switch (si.prot)
665 {
666 case PROT_IPv4:
667 ok = vpn->send_ipv4_packet (pkt, si, tos); break;
668 case PROT_UDPv4:
669 ok = vpn->send_udpv4_packet (pkt, si, tos); break;
670#if ENABLE_TCP
671 case PROT_TCPv4:
672 ok = vpn->send_tcpv4_packet (pkt, si, tos); break;
673#endif
674#if ENABLE_ICMP
675 case PROT_ICMPv4:
676 ok = vpn->send_icmpv4_packet (pkt, si, tos); break;
677#endif
678#if ENABLE_DNS
679 case PROT_DNSv4:
680 ok = send_dnsv4_packet (pkt, si, tos); break;
681#endif
682
683 default:
684 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
685 ok = false;
686 }
687
688 if (!ok)
689 reset_connection (); 659 reset_connection ();
690} 660}
691 661
692void 662void
693connection::send_ping (const sockinfo &si, u8 pong) 663connection::send_ping (const sockinfo &si, u8 pong)
769 && conf != THISNODE 739 && conf != THISNODE
770 && connectmode != conf_node::C_NEVER 740 && connectmode != conf_node::C_NEVER
771 && connectmode != conf_node::C_DISABLED 741 && connectmode != conf_node::C_DISABLED
772 && NOW > w.at) 742 && NOW > w.at)
773 { 743 {
774 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
775 745
776 if (retry_int < conf->max_retry) 746 double retry_int = double (retry_cnt & 3
777 retry_cnt++; 747 ? (retry_cnt & 3) + 1
778 else 748 : 1 << (retry_cnt >> 2));
779 retry_int = conf->max_retry;
780
781 w.start (NOW + retry_int);
782 749
783 reset_si (); 750 reset_si ();
751
752 bool slow = si.prot & PROT_SLOW;
784 753
785 if (si.prot && !si.host) 754 if (si.prot && !si.host)
786 vpn->send_connect_request (conf->id); 755 vpn->send_connect_request (conf->id);
787 else 756 else
788 { 757 {
789 const sockinfo &dsi = forward_si (si); 758 const sockinfo &dsi = forward_si (si);
759
760 slow = slow || (dsi.prot & PROT_SLOW);
790 761
791 if (dsi.valid () && auth_rate_limiter.can (dsi)) 762 if (dsi.valid () && auth_rate_limiter.can (dsi))
792 { 763 {
793 if (retry_cnt < 4) 764 if (retry_cnt < 4)
794 send_auth_request (dsi, true); 765 send_auth_request (dsi, true);
795 else 766 else
796 send_ping (dsi, 0); 767 send_ping (dsi, 0);
797 } 768 }
798 } 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);
799 } 779 }
800} 780}
801 781
802void 782void
803connection::reset_connection () 783connection::reset_connection ()
806 { 786 {
807 slog (L_INFO, _("%s(%s): connection lost"), 787 slog (L_INFO, _("%s(%s): connection lost"),
808 conf->nodename, (const char *)si); 788 conf->nodename, (const char *)si);
809 789
810 if (::conf.script_node_down) 790 if (::conf.script_node_down)
811 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."));
812 } 793 }
813 794
814 delete ictx; ictx = 0; 795 delete ictx; ictx = 0;
815 delete octx; octx = 0; 796 delete octx; octx = 0;
797#if ENABLE_DNS
798 dnsv4_reset_connection ();
799#endif
816 800
817 si.host = 0; 801 si.host = 0;
818 802
819 last_activity = 0; 803 last_activity = 0;
820 retry_cnt = 0; 804 retry_cnt = 0;
959 delete octx; 943 delete octx;
960 944
961 octx = new crypto_ctx (k, 1); 945 octx = new crypto_ctx (k, 1);
962 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; 946 oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff;
963 947
964 // compatibility code, remove when no longer required
965 if (p->flags & 1) p->features |= FEATURE_COMPRESSION;
966
967 conf->protocols = p->protocols; 948 conf->protocols = p->protocols;
968 features = p->features & config_packet::get_features (); 949 features = p->features & config_packet::get_features ();
969 950
970 send_auth_response (rsi, p->id, k); 951 send_auth_response (rsi, p->id, k);
971 952
1037 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1018 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1038 conf->nodename, (const char *)rsi, 1019 conf->nodename, (const char *)rsi,
1039 p->prot_major, p->prot_minor); 1020 p->prot_major, p->prot_minor);
1040 1021
1041 if (::conf.script_node_up) 1022 if (::conf.script_node_up)
1042 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."));
1043 1025
1044 break; 1026 break;
1045 } 1027 }
1046 else 1028 else
1047 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"),
1187 send_vpn_packet (p, si); 1169 send_vpn_packet (p, si);
1188 1170
1189 delete p; 1171 delete p;
1190} 1172}
1191 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
1192void connection::script_node () 1184void connection::script_init_connect_env ()
1193{ 1185{
1194 vpn->script_if_up (); 1186 vpn->script_init_env ();
1195 1187
1196 char *env; 1188 char *env;
1197 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1189 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1198 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1190 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1199 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1191 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1200 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1192 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1201} 1193}
1202 1194
1203const char *connection::script_node_up () 1195const char *connection::script_node_up ()
1204{ 1196{
1205 script_node (); 1197 script_init_connect_env ();
1206 1198
1207 putenv ("STATE=up"); 1199 putenv ("STATE=up");
1208 1200
1201 char *filename;
1202 asprintf (&filename,
1203 "%s/%s",
1204 confbase,
1209 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;
1210} 1208}
1211 1209
1212const char *connection::script_node_down () 1210const char *connection::script_node_down ()
1213{ 1211{
1214 script_node (); 1212 script_init_connect_env ();
1215 1213
1216 putenv ("STATE=down"); 1214 putenv ("STATE=down");
1217 1215
1218 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1216 char *filename;
1217 asprintf (&filename,
1218 "%s/%s",
1219 confbase,
1220 ::conf.script_node_down ? ::conf.script_node_down : "node-down");
1221
1222 return filename;
1219} 1223}
1220 1224
1221connection::connection (struct vpn *vpn, conf_node *conf) 1225connection::connection (struct vpn *vpn, conf_node *conf)
1222: vpn(vpn), conf(conf) 1226: vpn(vpn), conf(conf)
1223, rekey (this, &connection::rekey_cb) 1227, rekey (this, &connection::rekey_cb)
1224, keepalive (this, &connection::keepalive_cb) 1228, keepalive (this, &connection::keepalive_cb)
1225, establish_connection (this, &connection::establish_connection_cb) 1229, establish_connection (this, &connection::establish_connection_cb)
1226#if ENABLE_DNS 1230#if ENABLE_DNS
1227, dnsv4_tw (this, &connection::dnsv4_cb) 1231, dns (0)
1228, dns_rcvdq (0), dns_snddq (0)
1229, dns_rcvseq (0), dns_sndseq (0)
1230#endif 1232#endif
1231{ 1233{
1232 octx = ictx = 0; 1234 octx = ictx = 0;
1233 retry_cnt = 0; 1235 retry_cnt = 0;
1234 1236

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines