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