… | |
… | |
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 | |
|
|
24 | #include <cassert> |
|
|
25 | |
23 | |
26 | #include <list> |
24 | #include <list> |
27 | |
25 | |
28 | #include <openssl/rand.h> |
26 | #include <openssl/rand.h> |
29 | #include <openssl/evp.h> |
27 | #include <openssl/evp.h> |
30 | #include <openssl/rsa.h> |
28 | #include <openssl/rsa.h> |
31 | #include <openssl/err.h> |
29 | #include <openssl/err.h> |
32 | |
|
|
33 | #include "gettext.h" |
|
|
34 | |
30 | |
35 | #include "conf.h" |
31 | #include "conf.h" |
36 | #include "slog.h" |
32 | #include "slog.h" |
37 | #include "device.h" |
33 | #include "device.h" |
38 | #include "vpn.h" |
34 | #include "vpn.h" |
… | |
… | |
97 | struct rsa_cache : list<rsa_entry> |
93 | struct rsa_cache : list<rsa_entry> |
98 | { |
94 | { |
99 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
95 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
100 | |
96 | |
101 | bool find (const rsaid &id, rsachallenge &chg) |
97 | bool find (const rsaid &id, rsachallenge &chg) |
102 | { |
98 | { |
103 | for (iterator i = begin (); i != end (); ++i) |
99 | for (iterator i = begin (); i != end (); ++i) |
104 | { |
100 | { |
105 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
101 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
106 | { |
102 | { |
107 | memcpy (&chg, &i->chg, sizeof chg); |
103 | memcpy (&chg, &i->chg, sizeof chg); |
108 | |
104 | |
109 | erase (i); |
105 | erase (i); |
110 | return true; |
106 | return true; |
111 | } |
107 | } |
112 | } |
108 | } |
113 | |
109 | |
114 | if (cleaner.at < NOW) |
110 | if (cleaner.at < NOW) |
115 | cleaner.start (NOW + RSA_TTL); |
111 | cleaner.start (NOW + RSA_TTL); |
116 | |
112 | |
117 | return false; |
113 | return false; |
118 | } |
114 | } |
119 | |
115 | |
120 | void gen (rsaid &id, rsachallenge &chg) |
116 | void gen (rsaid &id, rsachallenge &chg) |
121 | { |
117 | { |
122 | rsa_entry e; |
118 | rsa_entry e; |
123 | |
119 | |
124 | RAND_bytes ((unsigned char *)&id, sizeof id); |
120 | RAND_bytes ((unsigned char *)&id, sizeof id); |
125 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
121 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
126 | |
122 | |
127 | e.expire = NOW + RSA_TTL; |
123 | e.expire = NOW + RSA_TTL; |
128 | e.id = id; |
124 | e.id = id; |
129 | memcpy (&e.chg, &chg, sizeof chg); |
125 | memcpy (&e.chg, &chg, sizeof chg); |
130 | |
126 | |
131 | push_back (e); |
127 | push_back (e); |
132 | |
128 | |
133 | if (cleaner.at < NOW) |
129 | if (cleaner.at < NOW) |
134 | cleaner.start (NOW + RSA_TTL); |
130 | cleaner.start (NOW + RSA_TTL); |
135 | } |
131 | } |
136 | |
132 | |
137 | rsa_cache () |
133 | rsa_cache () |
138 | : cleaner (this, &rsa_cache::cleaner_cb) |
134 | : cleaner (this, &rsa_cache::cleaner_cb) |
139 | { } |
135 | { } |
140 | |
136 | |
141 | } rsa_cache; |
137 | } rsa_cache; |
142 | |
138 | |
143 | void rsa_cache::cleaner_cb (time_watcher &w) |
139 | void rsa_cache::cleaner_cb (time_watcher &w) |
144 | { |
140 | { |
… | |
… | |
478 | { |
474 | { |
479 | prot_major = PROTOCOL_MAJOR; |
475 | prot_major = PROTOCOL_MAJOR; |
480 | prot_minor = PROTOCOL_MINOR; |
476 | prot_minor = PROTOCOL_MINOR; |
481 | randsize = RAND_SIZE; |
477 | randsize = RAND_SIZE; |
482 | hmaclen = HMACLENGTH; |
478 | hmaclen = HMACLENGTH; |
483 | flags = ENABLE_COMPRESSION ? 0x81 : 0x80; |
479 | flags = 0; |
484 | challengelen = sizeof (rsachallenge); |
480 | challengelen = sizeof (rsachallenge); |
485 | features = get_features (); |
481 | features = get_features (); |
486 | |
482 | |
487 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
483 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
488 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
484 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
… | |
… | |
498 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
494 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
499 | else if (randsize != RAND_SIZE) |
495 | else if (randsize != RAND_SIZE) |
500 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
496 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
501 | else if (hmaclen != HMACLENGTH) |
497 | else if (hmaclen != HMACLENGTH) |
502 | slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
498 | 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)) |
499 | else if (challengelen != sizeof (rsachallenge)) |
508 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
500 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
509 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
501 | 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)); |
502 | 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))) |
503 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
… | |
… | |
772 | else |
764 | else |
773 | send_ping (dsi, 0); |
765 | send_ping (dsi, 0); |
774 | } |
766 | } |
775 | } |
767 | } |
776 | |
768 | |
777 | retry_int *= slow ? 3. : 0.7; |
769 | retry_int *= slow ? 8. : 0.7; |
778 | |
770 | |
779 | if (retry_int < conf->max_retry) |
771 | if (retry_int < conf->max_retry) |
780 | retry_cnt++; |
772 | retry_cnt++; |
781 | else |
773 | else |
782 | retry_int = conf->max_retry; |
774 | retry_int = conf->max_retry; |
… | |
… | |
792 | { |
784 | { |
793 | slog (L_INFO, _("%s(%s): connection lost"), |
785 | slog (L_INFO, _("%s(%s): connection lost"), |
794 | conf->nodename, (const char *)si); |
786 | conf->nodename, (const char *)si); |
795 | |
787 | |
796 | if (::conf.script_node_down) |
788 | if (::conf.script_node_down) |
797 | run_script (run_script_cb (this, &connection::script_node_down), false); |
789 | if (!run_script (run_script_cb (this, &connection::script_node_down), false)) |
|
|
790 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
798 | } |
791 | } |
799 | |
792 | |
800 | delete ictx; ictx = 0; |
793 | delete ictx; ictx = 0; |
801 | delete octx; octx = 0; |
794 | delete octx; octx = 0; |
802 | #if ENABLE_DNS |
795 | #if ENABLE_DNS |
… | |
… | |
853 | { |
846 | { |
854 | if (ictx && octx) |
847 | if (ictx && octx) |
855 | send_data_packet (pkt); |
848 | send_data_packet (pkt); |
856 | else |
849 | else |
857 | { |
850 | { |
858 | if (!broadcast)//DDDD |
851 | if (!broadcast) |
859 | data_queue.put (new tap_packet (*pkt)); |
852 | data_queue.put (new tap_packet (*pkt)); |
860 | |
853 | |
861 | establish_connection (); |
854 | establish_connection (); |
862 | } |
855 | } |
863 | } |
856 | } |
… | |
… | |
948 | delete octx; |
941 | delete octx; |
949 | |
942 | |
950 | octx = new crypto_ctx (k, 1); |
943 | octx = new crypto_ctx (k, 1); |
951 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
944 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
952 | |
945 | |
953 | // compatibility code, remove when no longer required |
|
|
954 | if (p->flags & 1) p->features |= FEATURE_COMPRESSION; |
|
|
955 | |
|
|
956 | conf->protocols = p->protocols; |
946 | conf->protocols = p->protocols; |
957 | features = p->features & config_packet::get_features (); |
947 | features = p->features & config_packet::get_features (); |
958 | |
948 | |
959 | send_auth_response (rsi, p->id, k); |
949 | send_auth_response (rsi, p->id, k); |
960 | |
950 | |
… | |
… | |
1026 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1016 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1027 | conf->nodename, (const char *)rsi, |
1017 | conf->nodename, (const char *)rsi, |
1028 | p->prot_major, p->prot_minor); |
1018 | p->prot_major, p->prot_minor); |
1029 | |
1019 | |
1030 | if (::conf.script_node_up) |
1020 | if (::conf.script_node_up) |
1031 | run_script (run_script_cb (this, &connection::script_node_up), false); |
1021 | if (!run_script (run_script_cb (this, &connection::script_node_up), false)) |
|
|
1022 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
1032 | |
1023 | |
1033 | break; |
1024 | break; |
1034 | } |
1025 | } |
1035 | else |
1026 | else |
1036 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1027 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
… | |
… | |
1070 | { |
1061 | { |
1071 | vpn->tap->send (d); |
1062 | vpn->tap->send (d); |
1072 | |
1063 | |
1073 | if (si != rsi) |
1064 | if (si != rsi) |
1074 | { |
1065 | { |
1075 | // fast re-sync on connection changes, useful especially for tcp/ip |
1066 | // fast re-sync on source address changes, useful especially for tcp/ip |
1076 | si = rsi; |
1067 | si = rsi; |
1077 | |
1068 | |
1078 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1069 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1079 | conf->nodename, (const char *)si, (const char *)rsi); |
1070 | conf->nodename, (const char *)si, (const char *)rsi); |
1080 | } |
1071 | } |
… | |
… | |
1091 | case vpn_packet::PT_CONNECT_REQ: |
1082 | case vpn_packet::PT_CONNECT_REQ: |
1092 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1083 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1093 | { |
1084 | { |
1094 | connect_req_packet *p = (connect_req_packet *) pkt; |
1085 | connect_req_packet *p = (connect_req_packet *) pkt; |
1095 | |
1086 | |
1096 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1087 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1097 | connection *c = vpn->conns[p->id - 1]; |
|
|
1098 | conf->protocols = p->protocols; |
|
|
1099 | |
|
|
1100 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
|
|
1101 | conf->id, p->id, c->ictx && c->octx); |
|
|
1102 | |
|
|
1103 | if (c->ictx && c->octx) |
|
|
1104 | { |
1088 | { |
|
|
1089 | connection *c = vpn->conns[p->id - 1]; |
|
|
1090 | conf->protocols = p->protocols; |
|
|
1091 | |
|
|
1092 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
|
|
1093 | conf->id, p->id, c->ictx && c->octx); |
|
|
1094 | |
|
|
1095 | if (c->ictx && c->octx) |
|
|
1096 | { |
1105 | // send connect_info packets to both sides, in case one is |
1097 | // send connect_info packets to both sides, in case one is |
1106 | // behind a nat firewall (or both ;) |
1098 | // behind a nat firewall (or both ;) |
1107 | c->send_connect_info (conf->id, si, conf->protocols); |
1099 | c->send_connect_info (conf->id, si, conf->protocols); |
1108 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
1100 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
|
|
1101 | } |
|
|
1102 | else |
|
|
1103 | c->establish_connection (); |
1109 | } |
1104 | } |
1110 | else |
1105 | else |
1111 | c->establish_connection (); |
1106 | slog (L_WARN, |
|
|
1107 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1108 | p->id); |
1112 | } |
1109 | } |
1113 | |
1110 | |
1114 | break; |
1111 | break; |
1115 | |
1112 | |
1116 | case vpn_packet::PT_CONNECT_INFO: |
1113 | case vpn_packet::PT_CONNECT_INFO: |
1117 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1114 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1118 | { |
1115 | { |
1119 | connect_info_packet *p = (connect_info_packet *)pkt; |
1116 | connect_info_packet *p = (connect_info_packet *)pkt; |
1120 | |
1117 | |
1121 | if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything |
1118 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1122 | { |
1119 | { |
1123 | connection *c = vpn->conns[p->id - 1]; |
1120 | connection *c = vpn->conns[p->id - 1]; |
1124 | |
1121 | |
1125 | c->conf->protocols = p->protocols; |
1122 | c->conf->protocols = p->protocols; |
1126 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1123 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
… | |
… | |
1132 | const sockinfo &dsi = forward_si (p->si); |
1129 | const sockinfo &dsi = forward_si (p->si); |
1133 | |
1130 | |
1134 | if (dsi.valid ()) |
1131 | if (dsi.valid ()) |
1135 | c->send_auth_request (dsi, true); |
1132 | c->send_auth_request (dsi, true); |
1136 | } |
1133 | } |
|
|
1134 | else |
|
|
1135 | slog (L_WARN, |
|
|
1136 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1137 | p->id); |
1137 | } |
1138 | } |
1138 | |
1139 | |
1139 | break; |
1140 | break; |
1140 | |
1141 | |
1141 | default: |
1142 | default: |
… | |
… | |
1176 | send_vpn_packet (p, si); |
1177 | send_vpn_packet (p, si); |
1177 | |
1178 | |
1178 | delete p; |
1179 | delete p; |
1179 | } |
1180 | } |
1180 | |
1181 | |
|
|
1182 | void connection::script_init_env (const char *ext) |
|
|
1183 | { |
|
|
1184 | char *env; |
|
|
1185 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
|
|
1186 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
|
|
1187 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
|
|
1188 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
|
|
1189 | conf->id & 0xff); putenv (env); |
|
|
1190 | } |
|
|
1191 | |
1181 | void connection::script_node () |
1192 | void connection::script_init_connect_env () |
1182 | { |
1193 | { |
1183 | vpn->script_if_up (); |
1194 | vpn->script_init_env (); |
1184 | |
1195 | |
1185 | char *env; |
1196 | char *env; |
1186 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1197 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1187 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1198 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1188 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1199 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1189 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1200 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1190 | } |
1201 | } |
1191 | |
1202 | |
1192 | const char *connection::script_node_up () |
1203 | const char *connection::script_node_up () |
1193 | { |
1204 | { |
1194 | script_node (); |
1205 | script_init_connect_env (); |
1195 | |
1206 | |
1196 | putenv ("STATE=up"); |
1207 | putenv ("STATE=up"); |
1197 | |
1208 | |
|
|
1209 | char *filename; |
|
|
1210 | asprintf (&filename, |
|
|
1211 | "%s/%s", |
|
|
1212 | confbase, |
1198 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1213 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
|
|
1214 | |
|
|
1215 | return filename; |
1199 | } |
1216 | } |
1200 | |
1217 | |
1201 | const char *connection::script_node_down () |
1218 | const char *connection::script_node_down () |
1202 | { |
1219 | { |
1203 | script_node (); |
1220 | script_init_connect_env (); |
1204 | |
1221 | |
1205 | putenv ("STATE=down"); |
1222 | putenv ("STATE=down"); |
1206 | |
1223 | |
1207 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1224 | char *filename; |
|
|
1225 | asprintf (&filename, |
|
|
1226 | "%s/%s", |
|
|
1227 | confbase, |
|
|
1228 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
|
|
1229 | |
|
|
1230 | return filename; |
1208 | } |
1231 | } |
1209 | |
1232 | |
1210 | connection::connection (struct vpn *vpn, conf_node *conf) |
1233 | connection::connection (struct vpn *vpn, conf_node *conf) |
1211 | : vpn(vpn), conf(conf) |
1234 | : vpn(vpn), conf(conf) |
1212 | , rekey (this, &connection::rekey_cb) |
1235 | , rekey (this, &connection::rekey_cb) |