… | |
… | |
701 | si = rsi; |
701 | si = rsi; |
702 | protocol = rsi.prot; |
702 | protocol = rsi.prot; |
703 | |
703 | |
704 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
704 | slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), |
705 | conf->nodename, (const char *)rsi, |
705 | conf->nodename, (const char *)rsi, |
706 | is_direct ? "direct" : "forwarded", |
706 | vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded", |
707 | PROTOCOL_MAJOR, prot_minor); |
707 | PROTOCOL_MAJOR, prot_minor); |
708 | |
708 | |
709 | if (::conf.script_node_up) |
709 | if (::conf.script_node_up) |
710 | { |
710 | { |
711 | run_script_cb *cb = new run_script_cb; |
711 | run_script_cb *cb = new run_script_cb; |
… | |
… | |
723 | |
723 | |
724 | // make sure rekeying timeouts are slightly asymmetric |
724 | // make sure rekeying timeouts are slightly asymmetric |
725 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
725 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
726 | rekey.start (rekey_interval, rekey_interval); |
726 | rekey.start (rekey_interval, rekey_interval); |
727 | |
727 | |
|
|
728 | hmac_error = 0.; |
|
|
729 | |
728 | keepalive.start (::conf.keepalive); |
730 | keepalive.start (::conf.keepalive); |
729 | |
731 | |
730 | // send queued packets |
732 | // send queued packets |
731 | if (ictx && octx) |
|
|
732 | { |
|
|
733 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
733 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
734 | { |
734 | { |
735 | if (p->len) send_data_packet (p); |
735 | if (p->len) send_data_packet (p); |
736 | delete p; |
736 | delete p; |
737 | } |
737 | } |
738 | |
738 | |
739 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
739 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
740 | { |
740 | { |
741 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
741 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
742 | delete p; |
742 | delete p; |
743 | } |
|
|
744 | } |
743 | } |
745 | |
744 | |
746 | vpn->connection_established (this); |
745 | vpn->connection_established (this); |
747 | } |
746 | } |
748 | |
747 | |
… | |
… | |
756 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
755 | slog (L_TRACE, _("%s: direct connection denied by config."), conf->nodename); |
757 | protocol = 0; |
756 | protocol = 0; |
758 | } |
757 | } |
759 | |
758 | |
760 | si.set (conf, protocol); |
759 | si.set (conf, protocol); |
761 | |
|
|
762 | is_direct = si.valid (); |
|
|
763 | } |
760 | } |
764 | |
761 | |
765 | // ensure sockinfo is valid, forward if necessary |
762 | // ensure sockinfo is valid, forward if necessary |
766 | const sockinfo & |
763 | const sockinfo & |
767 | connection::forward_si (const sockinfo &si) const |
764 | connection::forward_si (const sockinfo &si) const |
… | |
… | |
786 | |
783 | |
787 | void |
784 | void |
788 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
785 | connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
789 | { |
786 | { |
790 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
787 | if (!vpn->send_vpn_packet (pkt, si, tos)) |
791 | reset_connection (); |
788 | reset_connection ("packet send error"); |
792 | } |
789 | } |
793 | |
790 | |
794 | void |
791 | void |
795 | connection::send_ping (const sockinfo &si, u8 pong) |
792 | connection::send_ping (const sockinfo &si, u8 pong) |
796 | { |
793 | { |
… | |
… | |
872 | { |
869 | { |
873 | // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection |
870 | // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection |
874 | // and stop trying. should probably be handled by a per-connection expire handler. |
871 | // and stop trying. should probably be handled by a per-connection expire handler. |
875 | if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) |
872 | if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) |
876 | { |
873 | { |
877 | reset_connection (); |
874 | reset_connection ("no demand"); |
878 | return; |
875 | return; |
879 | } |
876 | } |
880 | |
877 | |
881 | last_establish_attempt = ev_now (); |
878 | last_establish_attempt = ev_now (); |
882 | |
879 | |
… | |
… | |
923 | w.start (retry_int); |
920 | w.start (retry_int); |
924 | } |
921 | } |
925 | } |
922 | } |
926 | |
923 | |
927 | void |
924 | void |
928 | connection::reset_connection () |
925 | connection::reset_connection (const char *reason) |
929 | { |
926 | { |
930 | if (ictx && octx) |
927 | if (ictx && octx) |
931 | { |
928 | { |
932 | slog (L_INFO, _("%s(%s): connection lost"), |
929 | slog (L_INFO, _("%s(%s): connection lost (%s)"), |
933 | conf->nodename, (const char *)si); |
930 | conf->nodename, (const char *)si, reason); |
934 | |
931 | |
935 | if (::conf.script_node_down) |
932 | if (::conf.script_node_down) |
936 | { |
933 | { |
937 | run_script_cb *cb = new run_script_cb; |
934 | run_script_cb *cb = new run_script_cb; |
938 | cb->set<connection, &connection::script_node_down> (this); |
935 | cb->set<connection, &connection::script_node_down> (this); |
… | |
… | |
962 | connection::shutdown () |
959 | connection::shutdown () |
963 | { |
960 | { |
964 | if (ictx && octx) |
961 | if (ictx && octx) |
965 | send_reset (si); |
962 | send_reset (si); |
966 | |
963 | |
967 | reset_connection (); |
964 | reset_connection ("shutdown"); |
968 | } |
965 | } |
969 | |
966 | |
970 | // poor-man's rekeying |
967 | // poor-man's rekeying |
971 | inline void |
968 | inline void |
972 | connection::rekey_cb (ev::timer &w, int revents) |
969 | connection::rekey_cb (ev::timer &w, int revents) |
973 | { |
970 | { |
974 | reset_connection (); |
971 | reset_connection ("rekeying"); |
975 | establish_connection (); |
972 | establish_connection (); |
976 | } |
973 | } |
977 | |
974 | |
978 | void |
975 | void |
979 | connection::send_data_packet (tap_packet *pkt) |
976 | connection::send_data_packet (tap_packet *pkt) |
… | |
… | |
1066 | // about our desire for communication. |
1063 | // about our desire for communication. |
1067 | establish_connection (); |
1064 | establish_connection (); |
1068 | break; |
1065 | break; |
1069 | |
1066 | |
1070 | case vpn_packet::PT_RESET: |
1067 | case vpn_packet::PT_RESET: |
|
|
1068 | slog (L_TRACE, "%s >> PT_RESET", conf->nodename); |
|
|
1069 | |
|
|
1070 | if (ictx && octx) |
1071 | { |
1071 | { |
1072 | reset_connection (); |
1072 | reset_connection ("remote reset"); |
1073 | |
1073 | |
1074 | config_packet *p = (config_packet *) pkt; |
1074 | config_packet *p = (config_packet *) pkt; |
1075 | |
1075 | |
1076 | if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) |
1076 | if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) |
1077 | establish_connection (); |
1077 | establish_connection (); |
1078 | } |
1078 | } |
|
|
1079 | |
1079 | break; |
1080 | break; |
1080 | |
1081 | |
1081 | case vpn_packet::PT_AUTH_REQ: |
1082 | case vpn_packet::PT_AUTH_REQ: |
1082 | if (auth_rate_limiter.can (rsi)) |
1083 | if (auth_rate_limiter.can (rsi)) |
1083 | { |
1084 | { |
… | |
… | |
1098 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1099 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
1099 | conf->nodename, (const char *)rsi, |
1100 | conf->nodename, (const char *)rsi, |
1100 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1101 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
1101 | |
1102 | |
1102 | if (p->initiate) |
1103 | if (p->initiate) |
|
|
1104 | { |
1103 | send_auth_request (rsi, false); |
1105 | send_auth_request (rsi, false); |
|
|
1106 | |
|
|
1107 | if (ictx && octx) |
|
|
1108 | reset_connection ("reconnect"); |
|
|
1109 | } |
1104 | |
1110 | |
1105 | auth_data auth; |
1111 | auth_data auth; |
1106 | |
1112 | |
1107 | if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) |
1113 | if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) |
1108 | { |
1114 | { |
… | |
… | |
1170 | if (ictx && octx) |
1176 | if (ictx && octx) |
1171 | { |
1177 | { |
1172 | vpndata_packet *p = (vpndata_packet *)pkt; |
1178 | vpndata_packet *p = (vpndata_packet *)pkt; |
1173 | |
1179 | |
1174 | if (!p->hmac_chk (ictx)) |
1180 | if (!p->hmac_chk (ictx)) |
|
|
1181 | { |
|
|
1182 | // rekeying often creates temporary hmac auth floods |
|
|
1183 | // we assume they don't take longer than a few seconds normally, |
|
|
1184 | // and suppress messages and resets during that time. |
|
|
1185 | //TODO: should be done per source address |
|
|
1186 | if (!hmac_error) |
|
|
1187 | { |
|
|
1188 | hmac_error = ev_now () + 3; |
|
|
1189 | break; |
|
|
1190 | } |
|
|
1191 | else if (hmac_error >= ev_now ()) |
|
|
1192 | break; // silently suppress |
|
|
1193 | else |
|
|
1194 | { |
1175 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1195 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
1176 | "could be an attack, or just corruption or a synchronization error."), |
1196 | "could be an attack, or just corruption or a synchronization error."), |
1177 | conf->nodename, (const char *)rsi); |
1197 | conf->nodename, (const char *)rsi); |
|
|
1198 | // reset |
|
|
1199 | } |
|
|
1200 | } |
1178 | else |
1201 | else |
1179 | { |
1202 | { |
1180 | u32 seqno; |
1203 | u32 seqno; |
1181 | tap_packet *d = p->unpack (this, seqno); |
1204 | tap_packet *d = p->unpack (this, seqno); |
1182 | int seqclass = iseqno.seqno_classify (seqno); |
1205 | int seqclass = iseqno.seqno_classify (seqno); |
|
|
1206 | |
|
|
1207 | hmac_error = 0; |
1183 | |
1208 | |
1184 | if (seqclass == 0) // ok |
1209 | if (seqclass == 0) // ok |
1185 | { |
1210 | { |
1186 | vpn->tap->send (d); |
1211 | vpn->tap->send (d); |
1187 | |
1212 | |
… | |
… | |
1314 | |
1339 | |
1315 | if (when >= 0) |
1340 | if (when >= 0) |
1316 | w.start (when); |
1341 | w.start (when); |
1317 | else if (when < -15) |
1342 | else if (when < -15) |
1318 | { |
1343 | { |
1319 | reset_connection (); |
1344 | reset_connection ("keepalive overdue"); |
1320 | establish_connection (); |
1345 | establish_connection (); |
1321 | } |
1346 | } |
1322 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1347 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1323 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1348 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1324 | { |
1349 | { |
… | |
… | |
1328 | else if (when >= -10) |
1353 | else if (when >= -10) |
1329 | // hold ondemand connections implicitly a few seconds longer |
1354 | // hold ondemand connections implicitly a few seconds longer |
1330 | // should delete octx, though, or something like that ;) |
1355 | // should delete octx, though, or something like that ;) |
1331 | w.start (when + 10); |
1356 | w.start (when + 10); |
1332 | else |
1357 | else |
1333 | reset_connection (); |
1358 | reset_connection ("keepalive timeout"); |
1334 | } |
1359 | } |
1335 | |
1360 | |
1336 | void |
1361 | void |
1337 | connection::send_connect_request (int id) |
1362 | connection::send_connect_request (int id) |
1338 | { |
1363 | { |
… | |
… | |
1438 | |
1463 | |
1439 | // queue a dummy packet to force an initial connection attempt |
1464 | // queue a dummy packet to force an initial connection attempt |
1440 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1465 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1441 | vpn_queue.put (new net_packet); |
1466 | vpn_queue.put (new net_packet); |
1442 | |
1467 | |
1443 | reset_connection (); |
1468 | reset_connection ("startup"); |
1444 | } |
1469 | } |
1445 | |
1470 | |
1446 | connection::~connection () |
1471 | connection::~connection () |
1447 | { |
1472 | { |
1448 | shutdown (); |
1473 | shutdown (); |