… | |
… | |
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> |
… | |
… | |
465 | #if ENABLE_COMPRESSION |
463 | #if ENABLE_COMPRESSION |
466 | f |= FEATURE_COMPRESSION; |
464 | f |= FEATURE_COMPRESSION; |
467 | #endif |
465 | #endif |
468 | #if ENABLE_ROHC |
466 | #if ENABLE_ROHC |
469 | f |= FEATURE_ROHC; |
467 | f |= FEATURE_ROHC; |
|
|
468 | #endif |
|
|
469 | #if ENABLE_BRIDGING |
|
|
470 | f |= FEATURE_BRIDGING; |
470 | #endif |
471 | #endif |
471 | return f; |
472 | return f; |
472 | } |
473 | } |
473 | }; |
474 | }; |
474 | |
475 | |
… | |
… | |
625 | // mask out protocols we cannot establish |
626 | // mask out protocols we cannot establish |
626 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
627 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
627 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
628 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
628 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
629 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
629 | |
630 | |
|
|
631 | if (protocol |
|
|
632 | && (!conf->can_direct (THISNODE) |
|
|
633 | || !THISNODE->can_direct (conf))) |
|
|
634 | { |
|
|
635 | slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); |
|
|
636 | protocol = 0; |
|
|
637 | } |
|
|
638 | |
630 | si.set (conf, protocol); |
639 | si.set (conf, protocol); |
631 | } |
640 | } |
632 | |
641 | |
633 | // ensure sockinfo is valid, forward if necessary |
642 | // ensure sockinfo is valid, forward if necessary |
634 | const sockinfo & |
643 | const sockinfo & |
… | |
… | |
719 | } |
728 | } |
720 | |
729 | |
721 | void |
730 | void |
722 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
731 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
723 | { |
732 | { |
724 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", |
733 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", |
725 | conf->id, rid, (const char *)rsi); |
734 | conf->id, rid, (const char *)rsi); |
726 | |
735 | |
727 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
736 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
728 | |
737 | |
729 | r->hmac_set (octx); |
738 | r->hmac_set (octx); |
… | |
… | |
848 | { |
857 | { |
849 | if (ictx && octx) |
858 | if (ictx && octx) |
850 | send_data_packet (pkt); |
859 | send_data_packet (pkt); |
851 | else |
860 | else |
852 | { |
861 | { |
853 | if (!broadcast)//DDDD |
862 | if (!broadcast) |
854 | data_queue.put (new tap_packet (*pkt)); |
863 | data_queue.put (new tap_packet (*pkt)); |
855 | |
864 | |
856 | establish_connection (); |
865 | establish_connection (); |
857 | } |
866 | } |
858 | } |
867 | } |
… | |
… | |
1063 | { |
1072 | { |
1064 | vpn->tap->send (d); |
1073 | vpn->tap->send (d); |
1065 | |
1074 | |
1066 | if (si != rsi) |
1075 | if (si != rsi) |
1067 | { |
1076 | { |
1068 | // fast re-sync on connection changes, useful especially for tcp/ip |
1077 | // fast re-sync on source address changes, useful especially for tcp/ip |
1069 | si = rsi; |
1078 | si = rsi; |
1070 | |
1079 | |
1071 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1080 | slog (L_INFO, _("%s(%s): socket address changed to %s"), |
1072 | conf->nodename, (const char *)si, (const char *)rsi); |
1081 | conf->nodename, (const char *)si, (const char *)rsi); |
1073 | } |
1082 | } |
… | |
… | |
1084 | case vpn_packet::PT_CONNECT_REQ: |
1093 | case vpn_packet::PT_CONNECT_REQ: |
1085 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1094 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1086 | { |
1095 | { |
1087 | connect_req_packet *p = (connect_req_packet *) pkt; |
1096 | connect_req_packet *p = (connect_req_packet *) pkt; |
1088 | |
1097 | |
1089 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
1098 | 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 | { |
1099 | { |
|
|
1100 | connection *c = vpn->conns[p->id - 1]; |
|
|
1101 | conf->protocols = p->protocols; |
|
|
1102 | |
|
|
1103 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", |
|
|
1104 | conf->id, p->id, c->ictx && c->octx); |
|
|
1105 | |
|
|
1106 | if (c->ictx && c->octx) |
|
|
1107 | { |
1098 | // send connect_info packets to both sides, in case one is |
1108 | // send connect_info packets to both sides, in case one is |
1099 | // behind a nat firewall (or both ;) |
1109 | // behind a nat firewall (or both ;) |
1100 | c->send_connect_info (conf->id, si, conf->protocols); |
1110 | c->send_connect_info (conf->id, si, conf->protocols); |
1101 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
1111 | send_connect_info (c->conf->id, c->si, c->conf->protocols); |
|
|
1112 | } |
|
|
1113 | else |
|
|
1114 | c->establish_connection (); |
1102 | } |
1115 | } |
1103 | else |
1116 | else |
1104 | c->establish_connection (); |
1117 | slog (L_WARN, |
|
|
1118 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1119 | p->id); |
1105 | } |
1120 | } |
1106 | |
1121 | |
1107 | break; |
1122 | break; |
1108 | |
1123 | |
1109 | case vpn_packet::PT_CONNECT_INFO: |
1124 | case vpn_packet::PT_CONNECT_INFO: |
1110 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1125 | if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) |
1111 | { |
1126 | { |
1112 | connect_info_packet *p = (connect_info_packet *)pkt; |
1127 | connect_info_packet *p = (connect_info_packet *)pkt; |
1113 | |
1128 | |
1114 | if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything |
1129 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1115 | { |
1130 | { |
1116 | connection *c = vpn->conns[p->id - 1]; |
1131 | connection *c = vpn->conns[p->id - 1]; |
1117 | |
1132 | |
1118 | c->conf->protocols = p->protocols; |
1133 | c->conf->protocols = p->protocols; |
1119 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
1134 | protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); |
… | |
… | |
1125 | const sockinfo &dsi = forward_si (p->si); |
1140 | const sockinfo &dsi = forward_si (p->si); |
1126 | |
1141 | |
1127 | if (dsi.valid ()) |
1142 | if (dsi.valid ()) |
1128 | c->send_auth_request (dsi, true); |
1143 | c->send_auth_request (dsi, true); |
1129 | } |
1144 | } |
|
|
1145 | else |
|
|
1146 | slog (L_WARN, |
|
|
1147 | _("received authenticated connection request from unknown node #%d, config file mismatch?"), |
|
|
1148 | p->id); |
1130 | } |
1149 | } |
1131 | |
1150 | |
1132 | break; |
1151 | break; |
1133 | |
1152 | |
1134 | default: |
1153 | default: |
… | |
… | |
1169 | send_vpn_packet (p, si); |
1188 | send_vpn_packet (p, si); |
1170 | |
1189 | |
1171 | delete p; |
1190 | delete p; |
1172 | } |
1191 | } |
1173 | |
1192 | |
|
|
1193 | void connection::script_init_env (const char *ext) |
|
|
1194 | { |
|
|
1195 | char *env; |
|
|
1196 | asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); |
|
|
1197 | asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); |
|
|
1198 | asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, |
|
|
1199 | 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, |
|
|
1200 | conf->id & 0xff); putenv (env); |
|
|
1201 | } |
|
|
1202 | |
1174 | void connection::script_init_env () |
1203 | void connection::script_init_connect_env () |
1175 | { |
1204 | { |
1176 | vpn->script_init_env (); |
1205 | vpn->script_init_env (); |
1177 | |
1206 | |
1178 | char *env; |
1207 | char *env; |
1179 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1208 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
… | |
… | |
1182 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1211 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1183 | } |
1212 | } |
1184 | |
1213 | |
1185 | const char *connection::script_node_up () |
1214 | const char *connection::script_node_up () |
1186 | { |
1215 | { |
1187 | script_init_env (); |
1216 | script_init_connect_env (); |
1188 | |
1217 | |
1189 | putenv ("STATE=up"); |
1218 | putenv ("STATE=up"); |
1190 | |
1219 | |
1191 | char *filename; |
1220 | char *filename; |
1192 | asprintf (&filename, |
1221 | asprintf (&filename, |
1193 | "%s/%s", |
1222 | "%s/%s", |
1194 | confbase, |
1223 | confbase, |
1195 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1224 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
|
|
1225 | |
|
|
1226 | return filename; |
1196 | } |
1227 | } |
1197 | |
1228 | |
1198 | const char *connection::script_node_down () |
1229 | const char *connection::script_node_down () |
1199 | { |
1230 | { |
1200 | script_init_env (); |
1231 | script_init_connect_env (); |
1201 | |
1232 | |
1202 | putenv ("STATE=down"); |
1233 | putenv ("STATE=down"); |
1203 | |
1234 | |
1204 | char *filename; |
1235 | char *filename; |
1205 | asprintf (&filename, |
1236 | asprintf (&filename, |
1206 | "%s/%s", |
1237 | "%s/%s", |
1207 | confbase, |
1238 | confbase, |
1208 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
1239 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
|
|
1240 | |
|
|
1241 | return filename; |
1209 | } |
1242 | } |
1210 | |
1243 | |
1211 | connection::connection (struct vpn *vpn, conf_node *conf) |
1244 | connection::connection (struct vpn *vpn, conf_node *conf) |
1212 | : vpn(vpn), conf(conf) |
1245 | : vpn(vpn), conf(conf) |
1213 | , rekey (this, &connection::rekey_cb) |
1246 | , rekey (this, &connection::rekey_cb) |