relay_periodic.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * @file relay_periodic.c
  8. * @brief Periodic functions for the relay subsytem
  9. **/
  10. #include "orconfig.h"
  11. #include "core/or/or.h"
  12. #include "core/mainloop/periodic.h"
  13. #include "core/mainloop/cpuworker.h" // XXXX use a pubsub event.
  14. #include "core/mainloop/mainloop.h"
  15. #include "core/mainloop/netstatus.h"
  16. #include "core/or/circuituse.h" // XXXX move have_performed_bandwidth_test
  17. #include "feature/relay/dns.h"
  18. #include "feature/relay/relay_periodic.h"
  19. #include "feature/relay/router.h"
  20. #include "feature/relay/routerkeys.h"
  21. #include "feature/relay/routermode.h"
  22. #include "feature/relay/selftest.h"
  23. #include "feature/stats/predict_ports.h"
  24. #include "lib/crypt_ops/crypto_rand.h"
  25. #include "feature/nodelist/routerinfo_st.h"
  26. #include "feature/control/control_events.h"
  27. #define DECLARE_EVENT(name, roles, flags) \
  28. static periodic_event_item_t name ## _event = \
  29. PERIODIC_EVENT(name, \
  30. PERIODIC_EVENT_ROLE_##roles, \
  31. flags)
  32. #define FL(name) (PERIODIC_EVENT_FLAG_##name)
  33. /**
  34. * Periodic callback: If we're a server and initializing dns failed, retry.
  35. */
  36. static int
  37. retry_dns_callback(time_t now, const or_options_t *options)
  38. {
  39. (void)now;
  40. #define RETRY_DNS_INTERVAL (10*60)
  41. if (server_mode(options) && has_dns_init_failed())
  42. dns_init();
  43. return RETRY_DNS_INTERVAL;
  44. }
  45. DECLARE_EVENT(retry_dns, ROUTER, 0);
  46. static int dns_honesty_first_time = 1;
  47. /**
  48. * Periodic event: if we're an exit, see if our DNS server is telling us
  49. * obvious lies.
  50. */
  51. static int
  52. check_dns_honesty_callback(time_t now, const or_options_t *options)
  53. {
  54. (void)now;
  55. /* 9. and if we're an exit node, check whether our DNS is telling stories
  56. * to us. */
  57. if (net_is_disabled() ||
  58. ! public_server_mode(options) ||
  59. router_my_exit_policy_is_reject_star())
  60. return PERIODIC_EVENT_NO_UPDATE;
  61. if (dns_honesty_first_time) {
  62. /* Don't launch right when we start */
  63. dns_honesty_first_time = 0;
  64. return crypto_rand_int_range(60, 180);
  65. }
  66. dns_launch_correctness_checks();
  67. return 12*3600 + crypto_rand_int(12*3600);
  68. }
  69. DECLARE_EVENT(check_dns_honesty, RELAY, FL(NEED_NET));
  70. /* Periodic callback: rotate the onion keys after the period defined by the
  71. * "onion-key-rotation-days" consensus parameter, shut down and restart all
  72. * cpuworkers, and update our descriptor if necessary.
  73. */
  74. static int
  75. rotate_onion_key_callback(time_t now, const or_options_t *options)
  76. {
  77. if (server_mode(options)) {
  78. int onion_key_lifetime = get_onion_key_lifetime();
  79. time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime;
  80. if (rotation_time > now) {
  81. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  82. }
  83. log_info(LD_GENERAL,"Rotating onion key.");
  84. rotate_onion_key();
  85. cpuworkers_rotate_keyinfo();
  86. if (router_rebuild_descriptor(1)<0) {
  87. log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
  88. }
  89. if (advertised_server_mode() && !net_is_disabled())
  90. router_upload_dir_desc_to_dirservers(0);
  91. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  92. }
  93. return PERIODIC_EVENT_NO_UPDATE;
  94. }
  95. DECLARE_EVENT(rotate_onion_key, ROUTER, 0);
  96. /** Periodic callback: consider rebuilding or and re-uploading our descriptor
  97. * (if we've passed our internal checks). */
  98. static int
  99. check_descriptor_callback(time_t now, const or_options_t *options)
  100. {
  101. /** How often do we check whether part of our router info has changed in a
  102. * way that would require an upload? That includes checking whether our IP
  103. * address has changed. */
  104. #define CHECK_DESCRIPTOR_INTERVAL (60)
  105. (void)options;
  106. /* 2b. Once per minute, regenerate and upload the descriptor if the old
  107. * one is inaccurate. */
  108. if (!net_is_disabled()) {
  109. check_descriptor_bandwidth_changed(now);
  110. check_descriptor_ipaddress_changed(now);
  111. mark_my_descriptor_dirty_if_too_old(now);
  112. consider_publishable_server(0);
  113. }
  114. return CHECK_DESCRIPTOR_INTERVAL;
  115. }
  116. DECLARE_EVENT(check_descriptor, ROUTER, FL(NEED_NET));
  117. static int dirport_reachability_count = 0;
  118. /**
  119. * Periodic callback: check whether we're reachable (as a relay), and
  120. * whether our bandwidth has changed enough that we need to
  121. * publish a new descriptor.
  122. */
  123. static int
  124. check_for_reachability_bw_callback(time_t now, const or_options_t *options)
  125. {
  126. /* XXXX This whole thing was stuck in the middle of what is now
  127. * XXXX check_descriptor_callback. I'm not sure it's right. */
  128. /* also, check religiously for reachability, if it's within the first
  129. * 20 minutes of our uptime. */
  130. if (server_mode(options) &&
  131. (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
  132. !net_is_disabled()) {
  133. if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
  134. router_do_reachability_checks(1, dirport_reachability_count==0);
  135. if (++dirport_reachability_count > 5)
  136. dirport_reachability_count = 0;
  137. return 1;
  138. } else {
  139. /* If we haven't checked for 12 hours and our bandwidth estimate is
  140. * low, do another bandwidth test. This is especially important for
  141. * bridges, since they might go long periods without much use. */
  142. const routerinfo_t *me = router_get_my_routerinfo();
  143. static int first_time = 1;
  144. if (!first_time && me &&
  145. me->bandwidthcapacity < me->bandwidthrate &&
  146. me->bandwidthcapacity < 51200) {
  147. reset_bandwidth_test();
  148. }
  149. first_time = 0;
  150. #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
  151. return BANDWIDTH_RECHECK_INTERVAL;
  152. }
  153. }
  154. return CHECK_DESCRIPTOR_INTERVAL;
  155. }
  156. DECLARE_EVENT(check_for_reachability_bw, ROUTER, FL(NEED_NET));
  157. /**
  158. * Callback: Send warnings if Tor doesn't find its ports reachable.
  159. */
  160. static int
  161. reachability_warnings_callback(time_t now, const or_options_t *options)
  162. {
  163. (void) now;
  164. if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
  165. return (int)(TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT - get_uptime());
  166. }
  167. if (server_mode(options) &&
  168. !net_is_disabled() &&
  169. have_completed_a_circuit()) {
  170. /* every 20 minutes, check and complain if necessary */
  171. const routerinfo_t *me = router_get_my_routerinfo();
  172. if (me && !check_whether_orport_reachable(options)) {
  173. char *address = tor_dup_ip(me->addr);
  174. log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
  175. "its ORPort is reachable. Relays do not publish descriptors "
  176. "until their ORPort and DirPort are reachable. Please check "
  177. "your firewalls, ports, address, /etc/hosts file, etc.",
  178. address, me->or_port);
  179. control_event_server_status(LOG_WARN,
  180. "REACHABILITY_FAILED ORADDRESS=%s:%d",
  181. address, me->or_port);
  182. tor_free(address);
  183. }
  184. if (me && !check_whether_dirport_reachable(options)) {
  185. char *address = tor_dup_ip(me->addr);
  186. log_warn(LD_CONFIG,
  187. "Your server (%s:%d) has not managed to confirm that its "
  188. "DirPort is reachable. Relays do not publish descriptors "
  189. "until their ORPort and DirPort are reachable. Please check "
  190. "your firewalls, ports, address, /etc/hosts file, etc.",
  191. address, me->dir_port);
  192. control_event_server_status(LOG_WARN,
  193. "REACHABILITY_FAILED DIRADDRESS=%s:%d",
  194. address, me->dir_port);
  195. tor_free(address);
  196. }
  197. }
  198. return TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT;
  199. }
  200. DECLARE_EVENT(reachability_warnings, ROUTER, FL(NEED_NET));
  201. /* Periodic callback: Every 30 seconds, check whether it's time to make new
  202. * Ed25519 subkeys.
  203. */
  204. static int
  205. check_ed_keys_callback(time_t now, const or_options_t *options)
  206. {
  207. if (server_mode(options)) {
  208. if (should_make_new_ed_keys(options, now)) {
  209. int new_signing_key = load_ed_keys(options, now);
  210. if (new_signing_key < 0 ||
  211. generate_ed_link_cert(options, now, new_signing_key > 0)) {
  212. log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
  213. tor_shutdown_event_loop_and_exit(1);
  214. }
  215. }
  216. return 30;
  217. }
  218. return PERIODIC_EVENT_NO_UPDATE;
  219. }
  220. DECLARE_EVENT(check_ed_keys, ROUTER, 0);
  221. /* Period callback: Check if our old onion keys are still valid after the
  222. * period of time defined by the consensus parameter
  223. * "onion-key-grace-period-days", otherwise expire them by setting them to
  224. * NULL.
  225. */
  226. static int
  227. check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options)
  228. {
  229. if (server_mode(options)) {
  230. int onion_key_grace_period = get_onion_key_grace_period();
  231. time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period;
  232. if (expiry_time > now) {
  233. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  234. }
  235. log_info(LD_GENERAL, "Expiring old onion keys.");
  236. expire_old_onion_keys();
  237. cpuworkers_rotate_keyinfo();
  238. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  239. }
  240. return PERIODIC_EVENT_NO_UPDATE;
  241. }
  242. DECLARE_EVENT(check_onion_keys_expiry_time, ROUTER, 0);
  243. void
  244. relay_register_periodic_events(void)
  245. {
  246. periodic_events_register(&retry_dns_event);
  247. periodic_events_register(&check_dns_honesty_event);
  248. periodic_events_register(&rotate_onion_key_event);
  249. periodic_events_register(&check_descriptor_event);
  250. periodic_events_register(&check_for_reachability_bw_event);
  251. periodic_events_register(&reachability_warnings_event);
  252. periodic_events_register(&check_ed_keys_event);
  253. periodic_events_register(&check_onion_keys_expiry_time_event);
  254. dns_honesty_first_time = 1;
  255. dirport_reachability_count = 0;
  256. }
  257. /**
  258. * Update our schedule so that we'll check whether we need to update our
  259. * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
  260. * seconds.
  261. */
  262. void
  263. reschedule_descriptor_update_check(void)
  264. {
  265. periodic_event_reschedule(&check_descriptor_event);
  266. }