… | |
… | |
82 | require (EVP_DigestUpdate(&ctx, &id, sizeof id)); |
82 | require (EVP_DigestUpdate(&ctx, &id, sizeof id)); |
83 | require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
83 | require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); |
84 | EVP_MD_CTX_cleanup (&ctx); |
84 | EVP_MD_CTX_cleanup (&ctx); |
85 | } |
85 | } |
86 | |
86 | |
87 | struct rsa_entry { |
87 | struct rsa_entry |
|
|
88 | { |
88 | tstamp expire; |
89 | tstamp expire; |
89 | rsaid id; |
90 | rsaid id; |
90 | rsachallenge chg; |
91 | rsachallenge chg; |
91 | }; |
92 | }; |
92 | |
93 | |
93 | struct rsa_cache : list<rsa_entry> |
94 | struct rsa_cache : list<rsa_entry> |
94 | { |
95 | { |
95 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
96 | void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
96 | |
97 | |
97 | bool find (const rsaid &id, rsachallenge &chg) |
98 | bool find (const rsaid &id, rsachallenge &chg) |
98 | { |
99 | { |
99 | for (iterator i = begin (); i != end (); ++i) |
100 | for (iterator i = begin (); i != end (); ++i) |
100 | { |
101 | { |
101 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
102 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ()) |
102 | { |
103 | { |
103 | memcpy (&chg, &i->chg, sizeof chg); |
104 | memcpy (&chg, &i->chg, sizeof chg); |
104 | |
105 | |
105 | erase (i); |
106 | erase (i); |
106 | return true; |
107 | return true; |
107 | } |
108 | } |
108 | } |
109 | } |
109 | |
110 | |
110 | if (cleaner.at < NOW) |
111 | if (!cleaner.is_active ()) |
111 | cleaner.start (NOW + RSA_TTL); |
112 | cleaner.again (); |
112 | |
113 | |
113 | return false; |
114 | return false; |
114 | } |
115 | } |
115 | |
116 | |
116 | void gen (rsaid &id, rsachallenge &chg) |
117 | void gen (rsaid &id, rsachallenge &chg) |
… | |
… | |
118 | rsa_entry e; |
119 | rsa_entry e; |
119 | |
120 | |
120 | RAND_bytes ((unsigned char *)&id, sizeof id); |
121 | RAND_bytes ((unsigned char *)&id, sizeof id); |
121 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
122 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
122 | |
123 | |
123 | e.expire = NOW + RSA_TTL; |
124 | e.expire = ev_now () + RSA_TTL; |
124 | e.id = id; |
125 | e.id = id; |
125 | memcpy (&e.chg, &chg, sizeof chg); |
126 | memcpy (&e.chg, &chg, sizeof chg); |
126 | |
127 | |
127 | push_back (e); |
128 | push_back (e); |
128 | |
129 | |
129 | if (cleaner.at < NOW) |
130 | if (!cleaner.is_active ()) |
130 | cleaner.start (NOW + RSA_TTL); |
131 | cleaner.again (); |
131 | } |
132 | } |
132 | |
133 | |
133 | rsa_cache () |
134 | rsa_cache () |
134 | : cleaner (this, &rsa_cache::cleaner_cb) |
|
|
135 | { } |
135 | { |
|
|
136 | cleaner.set<rsa_cache, &rsa_cache::cleaner_cb> (this); |
|
|
137 | cleaner.set (RSA_TTL, RSA_TTL); |
|
|
138 | } |
136 | |
139 | |
137 | } rsa_cache; |
140 | } rsa_cache; |
138 | |
141 | |
139 | void rsa_cache::cleaner_cb (time_watcher &w) |
142 | void rsa_cache::cleaner_cb (ev::timer &w, int revents) |
140 | { |
143 | { |
141 | if (!empty ()) |
144 | if (empty ()) |
|
|
145 | w.stop (); |
|
|
146 | else |
142 | { |
147 | { |
143 | w.start (NOW + RSA_TTL); |
|
|
144 | |
|
|
145 | for (iterator i = begin (); i != end (); ) |
148 | for (iterator i = begin (); i != end (); ) |
146 | if (i->expire <= NOW) |
149 | if (i->expire <= ev_now ()) |
147 | i = erase (i); |
150 | i = erase (i); |
148 | else |
151 | else |
149 | ++i; |
152 | ++i; |
150 | } |
153 | } |
151 | } |
154 | } |
… | |
… | |
189 | { |
192 | { |
190 | for (i = QUEUEDEPTH; --i > 0; ) |
193 | for (i = QUEUEDEPTH; --i > 0; ) |
191 | delete queue[i]; |
194 | delete queue[i]; |
192 | } |
195 | } |
193 | |
196 | |
194 | struct net_rateinfo { |
197 | struct net_rateinfo |
|
|
198 | { |
195 | u32 host; |
199 | u32 host; |
196 | double pcnt, diff; |
200 | double pcnt, diff; |
197 | tstamp last; |
201 | tstamp last; |
198 | }; |
202 | }; |
199 | |
203 | |
… | |
… | |
218 | iterator i; |
222 | iterator i; |
219 | |
223 | |
220 | for (i = begin (); i != end (); ) |
224 | for (i = begin (); i != end (); ) |
221 | if (i->host == host) |
225 | if (i->host == host) |
222 | break; |
226 | break; |
223 | else if (i->last < NOW - NRL_EXPIRE) |
227 | else if (i->last < ev_now () - NRL_EXPIRE) |
224 | i = erase (i); |
228 | i = erase (i); |
225 | else |
229 | else |
226 | i++; |
230 | i++; |
227 | |
231 | |
228 | if (i == end ()) |
232 | if (i == end ()) |
… | |
… | |
230 | net_rateinfo ri; |
234 | net_rateinfo ri; |
231 | |
235 | |
232 | ri.host = host; |
236 | ri.host = host; |
233 | ri.pcnt = 1.; |
237 | ri.pcnt = 1.; |
234 | ri.diff = NRL_MAXDIF; |
238 | ri.diff = NRL_MAXDIF; |
235 | ri.last = NOW; |
239 | ri.last = ev_now (); |
236 | |
240 | |
237 | push_front (ri); |
241 | push_front (ri); |
238 | |
242 | |
239 | return true; |
243 | return true; |
240 | } |
244 | } |
… | |
… | |
242 | { |
246 | { |
243 | net_rateinfo ri (*i); |
247 | net_rateinfo ri (*i); |
244 | erase (i); |
248 | erase (i); |
245 | |
249 | |
246 | ri.pcnt = ri.pcnt * NRL_ALPHA; |
250 | ri.pcnt = ri.pcnt * NRL_ALPHA; |
247 | ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); |
251 | ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last); |
248 | |
252 | |
249 | ri.last = NOW; |
253 | ri.last = ev_now (); |
250 | |
254 | |
251 | double dif = ri.diff / ri.pcnt; |
255 | double dif = ri.diff / ri.pcnt; |
252 | |
256 | |
253 | bool send = dif > NRL_CUTOFF; |
257 | bool send = dif > NRL_CUTOFF; |
254 | |
258 | |
… | |
… | |
582 | ///////////////////////////////////////////////////////////////////////////// |
586 | ///////////////////////////////////////////////////////////////////////////// |
583 | |
587 | |
584 | void |
588 | void |
585 | connection::connection_established () |
589 | connection::connection_established () |
586 | { |
590 | { |
|
|
591 | slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
|
|
592 | |
587 | if (ictx && octx) |
593 | if (ictx && octx) |
588 | { |
594 | { |
589 | connectmode = conf->connectmode; |
595 | connectmode = conf->connectmode; |
590 | |
596 | |
591 | // make sure rekeying timeouts are slightly asymmetric |
597 | // make sure rekeying timeouts are slightly asymmetric |
592 | rekey.start (NOW + ::conf.rekey |
598 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
593 | + (conf->id > THISNODE->id ? 10 : 0)); |
599 | rekey.start (rekey_interval, rekey_interval); |
594 | keepalive.start (NOW + ::conf.keepalive); |
600 | keepalive.start (::conf.keepalive); |
595 | |
601 | |
596 | // send queued packets |
602 | // send queued packets |
597 | if (ictx && octx) |
603 | if (ictx && octx) |
598 | { |
604 | { |
599 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
605 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
… | |
… | |
610 | } |
616 | } |
611 | } |
617 | } |
612 | else |
618 | else |
613 | { |
619 | { |
614 | retry_cnt = 0; |
620 | retry_cnt = 0; |
615 | establish_connection.start (NOW + 5); |
621 | establish_connection.start (5); |
616 | keepalive.stop (); |
622 | keepalive.stop (); |
617 | rekey.stop (); |
623 | rekey.stop (); |
618 | } |
624 | } |
619 | } |
625 | } |
620 | |
626 | |
… | |
… | |
740 | |
746 | |
741 | delete r; |
747 | delete r; |
742 | } |
748 | } |
743 | |
749 | |
744 | void |
750 | void |
745 | connection::establish_connection_cb (time_watcher &w) |
751 | connection::establish_connection_cb (ev::timer &w, int revents) |
746 | { |
752 | { |
747 | if (!ictx |
753 | if (!ictx |
748 | && conf != THISNODE |
754 | && conf != THISNODE |
749 | && connectmode != conf_node::C_NEVER |
755 | && connectmode != conf_node::C_NEVER |
750 | && connectmode != conf_node::C_DISABLED |
756 | && connectmode != conf_node::C_DISABLED |
751 | && NOW > w.at) |
757 | && !w.is_active ()) |
752 | { |
758 | { |
753 | w.at = TSTAMP_MAX; // first disable this watcher in case of recursion |
759 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
754 | |
|
|
755 | double retry_int = double (retry_cnt & 3 |
|
|
756 | ? (retry_cnt & 3) + 1 |
760 | ? (retry_cnt & 3) + 1 |
757 | : 1 << (retry_cnt >> 2)); |
761 | : 1 << (retry_cnt >> 2)); |
758 | |
762 | |
759 | reset_si (); |
763 | reset_si (); |
760 | |
764 | |
761 | bool slow = si.prot & PROT_SLOW; |
765 | bool slow = si.prot & PROT_SLOW; |
762 | |
766 | |
763 | if (si.prot && !si.host) |
767 | if (si.prot && !si.host) |
|
|
768 | { |
|
|
769 | slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); |
|
|
770 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
|
|
771 | w.start (1); |
764 | vpn->send_connect_request (conf->id); |
772 | vpn->send_connect_request (conf->id); |
|
|
773 | } |
765 | else |
774 | else |
766 | { |
775 | { |
|
|
776 | slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); |
|
|
777 | |
767 | const sockinfo &dsi = forward_si (si); |
778 | const sockinfo &dsi = forward_si (si); |
768 | |
779 | |
769 | slow = slow || (dsi.prot & PROT_SLOW); |
780 | slow = slow || (dsi.prot & PROT_SLOW); |
770 | |
781 | |
771 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
782 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
… | |
… | |
782 | if (retry_int < conf->max_retry) |
793 | if (retry_int < conf->max_retry) |
783 | retry_cnt++; |
794 | retry_cnt++; |
784 | else |
795 | else |
785 | retry_int = conf->max_retry; |
796 | retry_int = conf->max_retry; |
786 | |
797 | |
787 | w.start (NOW + retry_int); |
798 | w.start (retry_int); |
788 | } |
799 | } |
789 | } |
800 | } |
790 | |
801 | |
791 | void |
802 | void |
792 | connection::reset_connection () |
803 | connection::reset_connection () |
… | |
… | |
795 | { |
806 | { |
796 | slog (L_INFO, _("%s(%s): connection lost"), |
807 | slog (L_INFO, _("%s(%s): connection lost"), |
797 | conf->nodename, (const char *)si); |
808 | conf->nodename, (const char *)si); |
798 | |
809 | |
799 | if (::conf.script_node_down) |
810 | if (::conf.script_node_down) |
800 | if (!run_script (run_script_cb (this, &connection::script_node_down), false)) |
811 | { |
|
|
812 | run_script_cb cb; |
|
|
813 | cb.set<connection, &connection::script_node_down> (this); |
|
|
814 | if (!run_script (cb, false)) |
801 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
815 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
|
|
816 | } |
802 | } |
817 | } |
803 | |
818 | |
804 | delete ictx; ictx = 0; |
819 | delete ictx; ictx = 0; |
805 | delete octx; octx = 0; |
820 | delete octx; octx = 0; |
806 | #if ENABLE_DNS |
821 | #if ENABLE_DNS |
… | |
… | |
825 | |
840 | |
826 | reset_connection (); |
841 | reset_connection (); |
827 | } |
842 | } |
828 | |
843 | |
829 | void |
844 | void |
830 | connection::rekey_cb (time_watcher &w) |
845 | connection::rekey_cb (ev::timer &w, int revents) |
831 | { |
846 | { |
832 | reset_connection (); |
847 | reset_connection (); |
833 | establish_connection (); |
848 | establish_connection (); |
834 | } |
849 | } |
835 | |
850 | |
… | |
… | |
879 | } |
894 | } |
880 | |
895 | |
881 | void |
896 | void |
882 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
897 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
883 | { |
898 | { |
884 | last_activity = NOW; |
899 | last_activity = ev_now (); |
885 | |
900 | |
886 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
901 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
887 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
902 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
903 | |
|
|
904 | if (connectmode == conf_node::C_DISABLED) |
|
|
905 | return; |
888 | |
906 | |
889 | switch (pkt->typ ()) |
907 | switch (pkt->typ ()) |
890 | { |
908 | { |
891 | case vpn_packet::PT_PING: |
909 | case vpn_packet::PT_PING: |
892 | // we send pings instead of auth packets after some retries, |
910 | // we send pings instead of auth packets after some retries, |
… | |
… | |
1027 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1045 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1028 | conf->nodename, (const char *)rsi, |
1046 | conf->nodename, (const char *)rsi, |
1029 | p->prot_major, p->prot_minor); |
1047 | p->prot_major, p->prot_minor); |
1030 | |
1048 | |
1031 | if (::conf.script_node_up) |
1049 | if (::conf.script_node_up) |
1032 | if (!run_script (run_script_cb (this, &connection::script_node_up), false)) |
1050 | { |
|
|
1051 | run_script_cb cb; |
|
|
1052 | cb.set<connection, &connection::script_node_up> (this); |
|
|
1053 | if (!run_script (cb, false)) |
1033 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
1054 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
|
|
1055 | } |
1034 | |
1056 | |
1035 | break; |
1057 | break; |
1036 | } |
1058 | } |
1037 | else |
1059 | else |
1038 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1060 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
… | |
… | |
1154 | send_reset (rsi); |
1176 | send_reset (rsi); |
1155 | break; |
1177 | break; |
1156 | } |
1178 | } |
1157 | } |
1179 | } |
1158 | |
1180 | |
1159 | void connection::keepalive_cb (time_watcher &w) |
1181 | void connection::keepalive_cb (ev::timer &w, int revents) |
1160 | { |
1182 | { |
1161 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1183 | if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1162 | { |
1184 | { |
1163 | reset_connection (); |
1185 | reset_connection (); |
1164 | establish_connection (); |
1186 | establish_connection (); |
1165 | } |
1187 | } |
1166 | else if (NOW < last_activity + ::conf.keepalive) |
1188 | else if (ev_now () < last_activity + ::conf.keepalive) |
1167 | w.start (last_activity + ::conf.keepalive); |
1189 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1168 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1190 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1169 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1191 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1170 | { |
1192 | { |
1171 | send_ping (si); |
1193 | send_ping (si); |
1172 | w.start (NOW + 5); |
1194 | w.start (5); |
1173 | } |
1195 | } |
1174 | else if (NOW < last_activity + ::conf.keepalive + 10) |
1196 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1175 | // hold ondemand connections implicitly a few seconds longer |
1197 | // hold ondemand connections implicitly a few seconds longer |
1176 | // should delete octx, though, or something like that ;) |
1198 | // should delete octx, though, or something like that ;) |
1177 | w.start (last_activity + ::conf.keepalive + 10); |
1199 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1178 | else |
1200 | else |
1179 | reset_connection (); |
1201 | reset_connection (); |
1180 | } |
1202 | } |
1181 | |
1203 | |
1182 | void connection::send_connect_request (int id) |
1204 | void connection::send_connect_request (int id) |
… | |
… | |
1213 | |
1235 | |
1214 | const char *connection::script_node_up () |
1236 | const char *connection::script_node_up () |
1215 | { |
1237 | { |
1216 | script_init_connect_env (); |
1238 | script_init_connect_env (); |
1217 | |
1239 | |
1218 | putenv ("STATE=up"); |
1240 | putenv ((char *)"STATE=up"); |
1219 | |
1241 | |
1220 | char *filename; |
1242 | char *filename; |
1221 | asprintf (&filename, |
1243 | asprintf (&filename, |
1222 | "%s/%s", |
1244 | "%s/%s", |
1223 | confbase, |
1245 | confbase, |
… | |
… | |
1228 | |
1250 | |
1229 | const char *connection::script_node_down () |
1251 | const char *connection::script_node_down () |
1230 | { |
1252 | { |
1231 | script_init_connect_env (); |
1253 | script_init_connect_env (); |
1232 | |
1254 | |
1233 | putenv ("STATE=down"); |
1255 | putenv ((char *)"STATE=down"); |
1234 | |
1256 | |
1235 | char *filename; |
1257 | char *filename; |
1236 | asprintf (&filename, |
1258 | asprintf (&filename, |
1237 | "%s/%s", |
1259 | "%s/%s", |
1238 | confbase, |
1260 | confbase, |
… | |
… | |
1241 | return filename; |
1263 | return filename; |
1242 | } |
1264 | } |
1243 | |
1265 | |
1244 | connection::connection (struct vpn *vpn, conf_node *conf) |
1266 | connection::connection (struct vpn *vpn, conf_node *conf) |
1245 | : vpn(vpn), conf(conf) |
1267 | : vpn(vpn), conf(conf) |
1246 | , rekey (this, &connection::rekey_cb) |
|
|
1247 | , keepalive (this, &connection::keepalive_cb) |
|
|
1248 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1249 | #if ENABLE_DNS |
1268 | #if ENABLE_DNS |
1250 | , dns (0) |
1269 | , dns (0) |
1251 | #endif |
1270 | #endif |
1252 | { |
1271 | { |
|
|
1272 | rekey .set<connection, &connection::rekey_cb > (this); |
|
|
1273 | keepalive .set<connection, &connection::keepalive_cb > (this); |
|
|
1274 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
|
|
1275 | |
1253 | octx = ictx = 0; |
1276 | octx = ictx = 0; |
1254 | retry_cnt = 0; |
1277 | retry_cnt = 0; |
1255 | |
1278 | |
1256 | if (!conf->protocols) // make sure some protocol is enabled |
1279 | if (!conf->protocols) // make sure some protocol is enabled |
1257 | conf->protocols = PROT_UDPv4; |
1280 | conf->protocols = PROT_UDPv4; |