… | |
… | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
16 | |
16 | |
17 | 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 |
18 | along with gvpe; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
19 | 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 |
20 | */ |
20 | */ |
21 | |
21 | |
22 | #include "config.h" |
22 | #include "config.h" |
23 | |
23 | |
24 | #include <cassert> |
24 | #include <cassert> |
… | |
… | |
27 | |
27 | |
28 | #include <openssl/rand.h> |
28 | #include <openssl/rand.h> |
29 | #include <openssl/evp.h> |
29 | #include <openssl/evp.h> |
30 | #include <openssl/rsa.h> |
30 | #include <openssl/rsa.h> |
31 | #include <openssl/err.h> |
31 | #include <openssl/err.h> |
32 | |
|
|
33 | #include "gettext.h" |
|
|
34 | |
32 | |
35 | #include "conf.h" |
33 | #include "conf.h" |
36 | #include "slog.h" |
34 | #include "slog.h" |
37 | #include "device.h" |
35 | #include "device.h" |
38 | #include "vpn.h" |
36 | #include "vpn.h" |
… | |
… | |
97 | struct rsa_cache : list<rsa_entry> |
95 | struct rsa_cache : list<rsa_entry> |
98 | { |
96 | { |
99 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
97 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
100 | |
98 | |
101 | bool find (const rsaid &id, rsachallenge &chg) |
99 | bool find (const rsaid &id, rsachallenge &chg) |
102 | { |
100 | { |
103 | for (iterator i = begin (); i != end (); ++i) |
101 | for (iterator i = begin (); i != end (); ++i) |
104 | { |
102 | { |
105 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
103 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
106 | { |
104 | { |
107 | memcpy (&chg, &i->chg, sizeof chg); |
105 | memcpy (&chg, &i->chg, sizeof chg); |
108 | |
106 | |
109 | erase (i); |
107 | erase (i); |
110 | return true; |
108 | return true; |
111 | } |
109 | } |
112 | } |
110 | } |
113 | |
111 | |
114 | if (cleaner.at < NOW) |
112 | if (cleaner.at < NOW) |
115 | cleaner.start (NOW + RSA_TTL); |
113 | cleaner.start (NOW + RSA_TTL); |
116 | |
114 | |
117 | return false; |
115 | return false; |
118 | } |
116 | } |
119 | |
117 | |
120 | void gen (rsaid &id, rsachallenge &chg) |
118 | void gen (rsaid &id, rsachallenge &chg) |
121 | { |
119 | { |
122 | rsa_entry e; |
120 | rsa_entry e; |
123 | |
121 | |
124 | RAND_bytes ((unsigned char *)&id, sizeof id); |
122 | RAND_bytes ((unsigned char *)&id, sizeof id); |
125 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
123 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
126 | |
124 | |
127 | e.expire = NOW + RSA_TTL; |
125 | e.expire = NOW + RSA_TTL; |
128 | e.id = id; |
126 | e.id = id; |
129 | memcpy (&e.chg, &chg, sizeof chg); |
127 | memcpy (&e.chg, &chg, sizeof chg); |
130 | |
128 | |
131 | push_back (e); |
129 | push_back (e); |
132 | |
130 | |
133 | if (cleaner.at < NOW) |
131 | if (cleaner.at < NOW) |
134 | cleaner.start (NOW + RSA_TTL); |
132 | cleaner.start (NOW + RSA_TTL); |
135 | } |
133 | } |
136 | |
134 | |
137 | rsa_cache () |
135 | rsa_cache () |
138 | : cleaner (this, &rsa_cache::cleaner_cb) |
136 | : cleaner (this, &rsa_cache::cleaner_cb) |
139 | { } |
137 | { } |
140 | |
138 | |
141 | } rsa_cache; |
139 | } rsa_cache; |
142 | |
140 | |
143 | void rsa_cache::cleaner_cb (time_watcher &w) |
141 | void rsa_cache::cleaner_cb (time_watcher &w) |
144 | { |
142 | { |
… | |
… | |
478 | { |
476 | { |
479 | prot_major = PROTOCOL_MAJOR; |
477 | prot_major = PROTOCOL_MAJOR; |
480 | prot_minor = PROTOCOL_MINOR; |
478 | prot_minor = PROTOCOL_MINOR; |
481 | randsize = RAND_SIZE; |
479 | randsize = RAND_SIZE; |
482 | hmaclen = HMACLENGTH; |
480 | hmaclen = HMACLENGTH; |
483 | flags = ENABLE_COMPRESSION ? 0x81 : 0x80; |
481 | flags = 0; |
484 | challengelen = sizeof (rsachallenge); |
482 | challengelen = sizeof (rsachallenge); |
485 | features = get_features (); |
483 | features = get_features (); |
486 | |
484 | |
487 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
485 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
488 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
486 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
… | |
… | |
498 | 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); |
499 | else if (randsize != RAND_SIZE) |
497 | else if (randsize != RAND_SIZE) |
500 | 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); |
501 | else if (hmaclen != HMACLENGTH) |
499 | else if (hmaclen != HMACLENGTH) |
502 | 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); |
503 | #if 0 // this implementation should handle all flag settings |
|
|
504 | else if (flags != curflags ()) |
|
|
505 | slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ()); |
|
|
506 | #endif |
|
|
507 | else if (challengelen != sizeof (rsachallenge)) |
501 | else if (challengelen != sizeof (rsachallenge)) |
508 | 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)); |
509 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
503 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
510 | 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)); |
511 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
505 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
… | |
… | |
745 | && conf != THISNODE |
739 | && conf != THISNODE |
746 | && connectmode != conf_node::C_NEVER |
740 | && connectmode != conf_node::C_NEVER |
747 | && connectmode != conf_node::C_DISABLED |
741 | && connectmode != conf_node::C_DISABLED |
748 | && NOW > w.at) |
742 | && NOW > w.at) |
749 | { |
743 | { |
750 | 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 |
751 | |
745 | |
752 | if (retry_int < conf->max_retry) |
746 | double retry_int = double (retry_cnt & 3 |
753 | retry_cnt++; |
747 | ? (retry_cnt & 3) + 1 |
754 | else |
748 | : 1 << (retry_cnt >> 2)); |
755 | retry_int = conf->max_retry; |
|
|
756 | |
|
|
757 | w.start (NOW + retry_int); |
|
|
758 | |
749 | |
759 | reset_si (); |
750 | reset_si (); |
|
|
751 | |
|
|
752 | bool slow = si.prot & PROT_SLOW; |
760 | |
753 | |
761 | if (si.prot && !si.host) |
754 | if (si.prot && !si.host) |
762 | vpn->send_connect_request (conf->id); |
755 | vpn->send_connect_request (conf->id); |
763 | else |
756 | else |
764 | { |
757 | { |
765 | const sockinfo &dsi = forward_si (si); |
758 | const sockinfo &dsi = forward_si (si); |
|
|
759 | |
|
|
760 | slow = slow || (dsi.prot & PROT_SLOW); |
766 | |
761 | |
767 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
762 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
768 | { |
763 | { |
769 | if (retry_cnt < 4) |
764 | if (retry_cnt < 4) |
770 | send_auth_request (dsi, true); |
765 | send_auth_request (dsi, true); |
771 | else |
766 | else |
772 | send_ping (dsi, 0); |
767 | send_ping (dsi, 0); |
773 | } |
768 | } |
774 | } |
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); |
775 | } |
779 | } |
776 | } |
780 | } |
777 | |
781 | |
778 | void |
782 | void |
779 | connection::reset_connection () |
783 | connection::reset_connection () |
… | |
… | |
782 | { |
786 | { |
783 | slog (L_INFO, _("%s(%s): connection lost"), |
787 | slog (L_INFO, _("%s(%s): connection lost"), |
784 | conf->nodename, (const char *)si); |
788 | conf->nodename, (const char *)si); |
785 | |
789 | |
786 | if (::conf.script_node_down) |
790 | if (::conf.script_node_down) |
787 | 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.")); |
788 | } |
793 | } |
789 | |
794 | |
790 | delete ictx; ictx = 0; |
795 | delete ictx; ictx = 0; |
791 | delete octx; octx = 0; |
796 | delete octx; octx = 0; |
792 | #if ENABLE_DNS |
797 | #if ENABLE_DNS |
… | |
… | |
938 | delete octx; |
943 | delete octx; |
939 | |
944 | |
940 | octx = new crypto_ctx (k, 1); |
945 | octx = new crypto_ctx (k, 1); |
941 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
946 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
942 | |
947 | |
943 | // compatibility code, remove when no longer required |
|
|
944 | if (p->flags & 1) p->features |= FEATURE_COMPRESSION; |
|
|
945 | |
|
|
946 | conf->protocols = p->protocols; |
948 | conf->protocols = p->protocols; |
947 | features = p->features & config_packet::get_features (); |
949 | features = p->features & config_packet::get_features (); |
948 | |
950 | |
949 | send_auth_response (rsi, p->id, k); |
951 | send_auth_response (rsi, p->id, k); |
950 | |
952 | |
… | |
… | |
1016 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1018 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1017 | conf->nodename, (const char *)rsi, |
1019 | conf->nodename, (const char *)rsi, |
1018 | p->prot_major, p->prot_minor); |
1020 | p->prot_major, p->prot_minor); |
1019 | |
1021 | |
1020 | if (::conf.script_node_up) |
1022 | if (::conf.script_node_up) |
1021 | 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.")); |
1022 | |
1025 | |
1023 | break; |
1026 | break; |
1024 | } |
1027 | } |
1025 | else |
1028 | else |
1026 | 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"), |
… | |
… | |
1166 | send_vpn_packet (p, si); |
1169 | send_vpn_packet (p, si); |
1167 | |
1170 | |
1168 | delete p; |
1171 | delete p; |
1169 | } |
1172 | } |
1170 | |
1173 | |
|
|
1174 | void 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 | |
1171 | void connection::script_node () |
1184 | void connection::script_init_connect_env () |
1172 | { |
1185 | { |
1173 | vpn->script_if_up (); |
1186 | vpn->script_init_env (); |
1174 | |
1187 | |
1175 | char *env; |
1188 | char *env; |
1176 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1189 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1177 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1190 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1178 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1191 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1179 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1192 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1180 | } |
1193 | } |
1181 | |
1194 | |
1182 | const char *connection::script_node_up () |
1195 | const char *connection::script_node_up () |
1183 | { |
1196 | { |
1184 | script_node (); |
1197 | script_init_connect_env (); |
1185 | |
1198 | |
1186 | putenv ("STATE=up"); |
1199 | putenv ("STATE=up"); |
1187 | |
1200 | |
|
|
1201 | char *filename; |
|
|
1202 | asprintf (&filename, |
|
|
1203 | "%s/%s", |
|
|
1204 | confbase, |
1188 | 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; |
1189 | } |
1208 | } |
1190 | |
1209 | |
1191 | const char *connection::script_node_down () |
1210 | const char *connection::script_node_down () |
1192 | { |
1211 | { |
1193 | script_node (); |
1212 | script_init_connect_env (); |
1194 | |
1213 | |
1195 | putenv ("STATE=down"); |
1214 | putenv ("STATE=down"); |
1196 | |
1215 | |
1197 | 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; |
1198 | } |
1223 | } |
1199 | |
1224 | |
1200 | connection::connection (struct vpn *vpn, conf_node *conf) |
1225 | connection::connection (struct vpn *vpn, conf_node *conf) |
1201 | : vpn(vpn), conf(conf) |
1226 | : vpn(vpn), conf(conf) |
1202 | , rekey (this, &connection::rekey_cb) |
1227 | , rekey (this, &connection::rekey_cb) |