… | |
… | |
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 | { |
… | |
… | |
768 | else |
764 | else |
769 | send_ping (dsi, 0); |
765 | send_ping (dsi, 0); |
770 | } |
766 | } |
771 | } |
767 | } |
772 | |
768 | |
773 | retry_int *= slow ? 3. : 0.7; |
769 | retry_int *= slow ? 8. : 0.7; |
774 | |
770 | |
775 | if (retry_int < conf->max_retry) |
771 | if (retry_int < conf->max_retry) |
776 | retry_cnt++; |
772 | retry_cnt++; |
777 | else |
773 | else |
778 | retry_int = conf->max_retry; |
774 | retry_int = conf->max_retry; |
… | |
… | |
788 | { |
784 | { |
789 | slog (L_INFO, _("%s(%s): connection lost"), |
785 | slog (L_INFO, _("%s(%s): connection lost"), |
790 | conf->nodename, (const char *)si); |
786 | conf->nodename, (const char *)si); |
791 | |
787 | |
792 | if (::conf.script_node_down) |
788 | if (::conf.script_node_down) |
793 | 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.")); |
794 | } |
791 | } |
795 | |
792 | |
796 | delete ictx; ictx = 0; |
793 | delete ictx; ictx = 0; |
797 | delete octx; octx = 0; |
794 | delete octx; octx = 0; |
798 | #if ENABLE_DNS |
795 | #if ENABLE_DNS |
… | |
… | |
849 | { |
846 | { |
850 | if (ictx && octx) |
847 | if (ictx && octx) |
851 | send_data_packet (pkt); |
848 | send_data_packet (pkt); |
852 | else |
849 | else |
853 | { |
850 | { |
854 | if (!broadcast)//DDDD |
851 | if (!broadcast) |
855 | data_queue.put (new tap_packet (*pkt)); |
852 | data_queue.put (new tap_packet (*pkt)); |
856 | |
853 | |
857 | establish_connection (); |
854 | establish_connection (); |
858 | } |
855 | } |
859 | } |
856 | } |
… | |
… | |
1019 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1016 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1020 | conf->nodename, (const char *)rsi, |
1017 | conf->nodename, (const char *)rsi, |
1021 | p->prot_major, p->prot_minor); |
1018 | p->prot_major, p->prot_minor); |
1022 | |
1019 | |
1023 | if (::conf.script_node_up) |
1020 | if (::conf.script_node_up) |
1024 | 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.")); |
1025 | |
1023 | |
1026 | break; |
1024 | break; |
1027 | } |
1025 | } |
1028 | else |
1026 | else |
1029 | 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"), |
… | |
… | |
1063 | { |
1061 | { |
1064 | vpn->tap->send (d); |
1062 | vpn->tap->send (d); |
1065 | |
1063 | |
1066 | if (si != rsi) |
1064 | if (si != rsi) |
1067 | { |
1065 | { |
1068 | // fast re-sync on connection changes, useful especially for tcp/ip |
1066 | // fast re-sync on source address changes, useful especially for tcp/ip |
1069 | si = rsi; |
1067 | si = rsi; |
1070 | |
1068 | |
1071 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1069 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1072 | conf->nodename, (const char *)si, (const char *)rsi); |
1070 | conf->nodename, (const char *)si, (const char *)rsi); |
1073 | } |
1071 | } |
… | |
… | |
1084 | case vpn_packet::PT_CONNECT_REQ: |
1082 | case vpn_packet::PT_CONNECT_REQ: |
1085 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1083 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1086 | { |
1084 | { |
1087 | connect_req_packet *p = (connect_req_packet *) pkt; |
1085 | connect_req_packet *p = (connect_req_packet *) pkt; |
1088 | |
1086 | |
1089 | 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 ()) |
1090 | connection *c = vpn->conns[p->id - 1]; |
|
|
1091 | conf->protocols = p->protocols; |
|
|
1092 | |
|
|
1093 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
|
|
1094 | conf->id, p->id, c->ictx && c->octx); |
|
|
1095 | |
|
|
1096 | if (c->ictx && c->octx) |
|
|
1097 | { |
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 | { |
1098 | // send connect_info packets to both sides, in case one is |
1097 | // send connect_info packets to both sides, in case one is |
1099 | // behind a nat firewall (or both ;) |
1098 | // behind a nat firewall (or both ;) |
1100 | c->send_connect_info (conf->id, si, conf->protocols); |
1099 | c->send_connect_info (conf->id, si, conf->protocols); |
1101 | 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 (); |
1102 | } |
1104 | } |
1103 | else |
1105 | else |
1104 | c->establish_connection (); |
1106 | slog (L_WARN, |
|
|
1107 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1108 | p->id); |
1105 | } |
1109 | } |
1106 | |
1110 | |
1107 | break; |
1111 | break; |
1108 | |
1112 | |
1109 | case vpn_packet::PT_CONNECT_INFO: |
1113 | case vpn_packet::PT_CONNECT_INFO: |
1110 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1114 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1111 | { |
1115 | { |
1112 | connect_info_packet *p = (connect_info_packet *)pkt; |
1116 | connect_info_packet *p = (connect_info_packet *)pkt; |
1113 | |
1117 | |
1114 | 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 ()) |
1115 | { |
1119 | { |
1116 | connection *c = vpn->conns[p->id - 1]; |
1120 | connection *c = vpn->conns[p->id - 1]; |
1117 | |
1121 | |
1118 | c->conf->protocols = p->protocols; |
1122 | c->conf->protocols = p->protocols; |
1119 | 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)); |
… | |
… | |
1125 | const sockinfo &dsi = forward_si (p->si); |
1129 | const sockinfo &dsi = forward_si (p->si); |
1126 | |
1130 | |
1127 | if (dsi.valid ()) |
1131 | if (dsi.valid ()) |
1128 | c->send_auth_request (dsi, true); |
1132 | c->send_auth_request (dsi, true); |
1129 | } |
1133 | } |
|
|
1134 | else |
|
|
1135 | slog (L_WARN, |
|
|
1136 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1137 | p->id); |
1130 | } |
1138 | } |
1131 | |
1139 | |
1132 | break; |
1140 | break; |
1133 | |
1141 | |
1134 | default: |
1142 | default: |
… | |
… | |
1169 | send_vpn_packet (p, si); |
1177 | send_vpn_packet (p, si); |
1170 | |
1178 | |
1171 | delete p; |
1179 | delete p; |
1172 | } |
1180 | } |
1173 | |
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 | |
1174 | void connection::script_node () |
1192 | void connection::script_init_connect_env () |
1175 | { |
1193 | { |
1176 | vpn->script_if_up (); |
1194 | vpn->script_init_env (); |
1177 | |
1195 | |
1178 | char *env; |
1196 | char *env; |
1179 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1197 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1180 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1198 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1181 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1199 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1182 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1200 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1183 | } |
1201 | } |
1184 | |
1202 | |
1185 | const char *connection::script_node_up () |
1203 | const char *connection::script_node_up () |
1186 | { |
1204 | { |
1187 | script_node (); |
1205 | script_init_connect_env (); |
1188 | |
1206 | |
1189 | putenv ("STATE=up"); |
1207 | putenv ("STATE=up"); |
1190 | |
1208 | |
|
|
1209 | char *filename; |
|
|
1210 | asprintf (&filename, |
|
|
1211 | "%s/%s", |
|
|
1212 | confbase, |
1191 | 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; |
1192 | } |
1216 | } |
1193 | |
1217 | |
1194 | const char *connection::script_node_down () |
1218 | const char *connection::script_node_down () |
1195 | { |
1219 | { |
1196 | script_node (); |
1220 | script_init_connect_env (); |
1197 | |
1221 | |
1198 | putenv ("STATE=down"); |
1222 | putenv ("STATE=down"); |
1199 | |
1223 | |
1200 | 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; |
1201 | } |
1231 | } |
1202 | |
1232 | |
1203 | connection::connection (struct vpn *vpn, conf_node *conf) |
1233 | connection::connection (struct vpn *vpn, conf_node *conf) |
1204 | : vpn(vpn), conf(conf) |
1234 | : vpn(vpn), conf(conf) |
1205 | , rekey (this, &connection::rekey_cb) |
1235 | , rekey (this, &connection::rekey_cb) |