… | |
… | |
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> |
… | |
… | |
848 | { |
846 | { |
849 | if (ictx && octx) |
847 | if (ictx && octx) |
850 | send_data_packet (pkt); |
848 | send_data_packet (pkt); |
851 | else |
849 | else |
852 | { |
850 | { |
853 | if (!broadcast)//DDDD |
851 | if (!broadcast) |
854 | data_queue.put (new tap_packet (*pkt)); |
852 | data_queue.put (new tap_packet (*pkt)); |
855 | |
853 | |
856 | establish_connection (); |
854 | establish_connection (); |
857 | } |
855 | } |
858 | } |
856 | } |
… | |
… | |
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_init_env () |
1192 | void connection::script_init_connect_env () |
1175 | { |
1193 | { |
1176 | vpn->script_init_env (); |
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); |
… | |
… | |
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_init_env (); |
1205 | script_init_connect_env (); |
1188 | |
1206 | |
1189 | putenv ("STATE=up"); |
1207 | putenv ("STATE=up"); |
1190 | |
1208 | |
1191 | char *filename; |
1209 | char *filename; |
1192 | asprintf (&filename, |
1210 | asprintf (&filename, |
1193 | "%s/%s", |
1211 | "%s/%s", |
1194 | confbase, |
1212 | confbase, |
1195 | ::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; |
1196 | } |
1216 | } |
1197 | |
1217 | |
1198 | const char *connection::script_node_down () |
1218 | const char *connection::script_node_down () |
1199 | { |
1219 | { |
1200 | script_init_env (); |
1220 | script_init_connect_env (); |
1201 | |
1221 | |
1202 | putenv ("STATE=down"); |
1222 | putenv ("STATE=down"); |
1203 | |
1223 | |
1204 | char *filename; |
1224 | char *filename; |
1205 | asprintf (&filename, |
1225 | asprintf (&filename, |
1206 | "%s/%s", |
1226 | "%s/%s", |
1207 | confbase, |
1227 | confbase, |
1208 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
1228 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
|
|
1229 | |
|
|
1230 | return filename; |
1209 | } |
1231 | } |
1210 | |
1232 | |
1211 | connection::connection (struct vpn *vpn, conf_node *conf) |
1233 | connection::connection (struct vpn *vpn, conf_node *conf) |
1212 | : vpn(vpn), conf(conf) |
1234 | : vpn(vpn), conf(conf) |
1213 | , rekey (this, &connection::rekey_cb) |
1235 | , rekey (this, &connection::rekey_cb) |