relay_periodic.c (11785B)
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-2021, The Tor Project, Inc. */ 5 /* See LICENSE for licensing information */ 6 7 /** 8 * @file relay_periodic.c 9 * @brief Periodic functions for the relay subsystem 10 **/ 11 12 #include "orconfig.h" 13 #include "core/or/or.h" 14 15 #include "app/config/resolve_addr.h" 16 17 #include "core/mainloop/periodic.h" 18 #include "core/mainloop/cpuworker.h" // XXXX use a pubsub event. 19 #include "core/mainloop/mainloop.h" 20 #include "core/mainloop/netstatus.h" 21 #include "core/or/circuituse.h" // XXXX move have_performed_bandwidth_test 22 23 #include "feature/relay/dns.h" 24 #include "feature/relay/relay_periodic.h" 25 #include "feature/relay/router.h" 26 #include "feature/relay/routerkeys.h" 27 #include "feature/relay/routermode.h" 28 #include "feature/relay/selftest.h" 29 #include "feature/stats/predict_ports.h" 30 31 #include "lib/crypt_ops/crypto_rand.h" 32 33 #include "feature/nodelist/routerinfo_st.h" 34 #include "feature/control/control_events.h" 35 36 #ifndef COCCI 37 #define DECLARE_EVENT(name, roles, flags) \ 38 static periodic_event_item_t name ## _event = \ 39 PERIODIC_EVENT(name, \ 40 PERIODIC_EVENT_ROLE_##roles, \ 41 flags) 42 #endif /* !defined(COCCI) */ 43 44 #define FL(name) (PERIODIC_EVENT_FLAG_##name) 45 46 /** 47 * Periodic callback: If we're a server and initializing dns failed, retry. 48 */ 49 static int 50 retry_dns_callback(time_t now, const or_options_t *options) 51 { 52 (void)now; 53 #define RETRY_DNS_INTERVAL (10*60) 54 if (server_mode(options) && has_dns_init_failed()) 55 dns_init(); 56 return RETRY_DNS_INTERVAL; 57 } 58 59 DECLARE_EVENT(retry_dns, ROUTER, 0); 60 61 static int dns_honesty_first_time = 1; 62 63 /** 64 * Periodic event: if we're an exit, see if our DNS server is telling us 65 * obvious lies. 66 */ 67 static int 68 check_dns_honesty_callback(time_t now, const or_options_t *options) 69 { 70 (void)now; 71 /* 9. and if we're an exit node, check whether our DNS is telling stories 72 * to us. */ 73 if (net_is_disabled() || 74 ! public_server_mode(options) || 75 router_my_exit_policy_is_reject_star()) 76 return PERIODIC_EVENT_NO_UPDATE; 77 78 if (dns_honesty_first_time) { 79 /* Don't launch right when we start */ 80 dns_honesty_first_time = 0; 81 return crypto_rand_int_range(60, 180); 82 } 83 84 dns_launch_correctness_checks(); 85 return 12*3600 + crypto_rand_int(12*3600); 86 } 87 88 DECLARE_EVENT(check_dns_honesty, RELAY, FL(NEED_NET)); 89 90 /* Periodic callback: rotate the onion keys after the period defined by the 91 * "onion-key-rotation-days" consensus parameter, shut down and restart all 92 * cpuworkers, and update our descriptor if necessary. 93 */ 94 static int 95 rotate_onion_key_callback(time_t now, const or_options_t *options) 96 { 97 if (server_mode(options)) { 98 int onion_key_lifetime = get_onion_key_lifetime(); 99 time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime; 100 if (rotation_time > now) { 101 return ONION_KEY_CONSENSUS_CHECK_INTERVAL; 102 } 103 104 log_info(LD_GENERAL,"Rotating onion key."); 105 if (!rotate_onion_key()) { 106 return ONION_KEY_CONSENSUS_CHECK_INTERVAL; 107 } 108 cpuworkers_rotate_keyinfo(); 109 if (!router_rebuild_descriptor(1)) { 110 log_info(LD_CONFIG, "Couldn't rebuild router descriptor"); 111 } 112 if (advertised_server_mode() && !net_is_disabled()) 113 router_upload_dir_desc_to_dirservers(0); 114 return ONION_KEY_CONSENSUS_CHECK_INTERVAL; 115 } 116 return PERIODIC_EVENT_NO_UPDATE; 117 } 118 119 DECLARE_EVENT(rotate_onion_key, ROUTER, 0); 120 121 /** Periodic callback: consider rebuilding or and re-uploading our descriptor 122 * (if we've passed our internal checks). */ 123 static int 124 check_descriptor_callback(time_t now, const or_options_t *options) 125 { 126 /** How often do we check whether part of our router info has changed in a 127 * way that would require an upload? That includes checking whether our IP 128 * address has changed. */ 129 #define CHECK_DESCRIPTOR_INTERVAL (60) 130 131 (void)options; 132 133 /* 2b. Once per minute, regenerate and upload the descriptor if the old 134 * one is inaccurate. */ 135 if (!net_is_disabled()) { 136 check_descriptor_bandwidth_changed(now); 137 check_descriptor_ipaddress_changed(now); 138 mark_my_descriptor_dirty_if_too_old(now); 139 consider_publishable_server(0); 140 } 141 142 return CHECK_DESCRIPTOR_INTERVAL; 143 } 144 145 DECLARE_EVENT(check_descriptor, ROUTER, FL(NEED_NET)); 146 147 static int dirport_reachability_count = 0; 148 149 /** 150 * Periodic callback: check whether we're reachable (as a relay), and 151 * whether our bandwidth has changed enough that we need to 152 * publish a new descriptor. 153 */ 154 static int 155 check_for_reachability_bw_callback(time_t now, const or_options_t *options) 156 { 157 /* XXXX This whole thing was stuck in the middle of what is now 158 * XXXX check_descriptor_callback. I'm not sure it's right. */ 159 /** How often should we consider launching reachability tests in our first 160 * TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT seconds? */ 161 #define EARLY_CHECK_REACHABILITY_INTERVAL (60) 162 163 /* also, check religiously for reachability, if it's within the first 164 * 20 minutes of our uptime. */ 165 if (server_mode(options) && 166 (have_completed_a_circuit() || !any_predicted_circuits(now)) && 167 !net_is_disabled()) { 168 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) { 169 router_do_reachability_checks(); 170 return EARLY_CHECK_REACHABILITY_INTERVAL; 171 } else { 172 /* If we haven't checked for 12 hours and our bandwidth estimate is 173 * low, do another bandwidth test. This is especially important for 174 * bridges, since they might go long periods without much use. */ 175 const routerinfo_t *me = router_get_my_routerinfo(); 176 static int first_time = 1; 177 if (!first_time && me && 178 me->bandwidthcapacity < me->bandwidthrate && 179 me->bandwidthcapacity < 51200) { 180 reset_bandwidth_test(); 181 } 182 first_time = 0; 183 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60) 184 return BANDWIDTH_RECHECK_INTERVAL; 185 } 186 } 187 return CHECK_DESCRIPTOR_INTERVAL; 188 } 189 190 DECLARE_EVENT(check_for_reachability_bw, ROUTER, FL(NEED_NET)); 191 192 /** 193 * Callback: Send warnings if Tor doesn't find its ports reachable. 194 */ 195 static int 196 reachability_warnings_callback(time_t now, const or_options_t *options) 197 { 198 (void) now; 199 200 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) { 201 return (int)(TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT - get_uptime()); 202 } 203 204 if (server_mode(options) && 205 !net_is_disabled() && 206 have_completed_a_circuit()) { 207 /* every 20 minutes, check and complain if necessary */ 208 const routerinfo_t *me = router_get_my_routerinfo(); 209 bool v4_ok = 210 router_orport_seems_reachable(options,AF_INET); 211 bool v6_ok = 212 router_orport_seems_reachable(options,AF_INET6); 213 if (me && !(v4_ok && v6_ok)) { 214 /* We need to warn that one or more of our ORPorts isn't reachable. 215 * Determine which, and give a reasonable warning. */ 216 char *address4 = tor_addr_to_str_dup(&me->ipv4_addr); 217 char *address6 = tor_addr_to_str_dup(&me->ipv6_addr); 218 if (address4 || address6) { 219 char *where4=NULL, *where6=NULL; 220 if (!v4_ok) 221 tor_asprintf(&where4, "%s:%d", address4, me->ipv4_orport); 222 if (!v6_ok) 223 tor_asprintf(&where6, "[%s]:%d", address6, me->ipv6_orport); 224 const char *opt_and = (!v4_ok && !v6_ok) ? " and " : ""; 225 226 /* IPv4 reachability test worked but not the IPv6. We will _not_ 227 * publish the descriptor if our IPv6 was configured. We will if it 228 * was auto discovered. */ 229 if (v4_ok && !v6_ok && !resolved_addr_is_configured(AF_INET6)) { 230 static ratelim_t rlim = RATELIM_INIT(3600); 231 log_fn_ratelim(&rlim, LOG_NOTICE, LD_CONFIG, 232 "Auto-discovered IPv6 address %s has not been found " 233 "reachable. However, IPv4 address is reachable. " 234 "Publishing server descriptor without IPv6 address.", 235 where6 ? where6 : ""); 236 /* Indicate we want to publish even if reachability test failed. */ 237 mark_my_descriptor_if_omit_ipv6_changes("IPv4 is reachable. " 238 "IPv6 is not but was " 239 "auto-discovered", true); 240 } else { 241 log_warn(LD_CONFIG, 242 "Your server has not managed to confirm reachability for " 243 "its ORPort(s) at %s%s%s. Relays do not publish " 244 "descriptors until their ORPort(s) are " 245 "reachable. Please check your firewalls, ports, address, " 246 "/etc/hosts file, etc.", 247 where4?where4:"", 248 opt_and, 249 where6?where6:""); 250 } 251 tor_free(where4); 252 tor_free(where6); 253 if (!v4_ok) { 254 control_event_server_status(LOG_WARN, 255 "REACHABILITY_FAILED ORADDRESS=%s:%d", 256 address4, me->ipv4_orport); 257 } 258 if (!v6_ok) { 259 control_event_server_status(LOG_WARN, 260 "REACHABILITY_FAILED ORADDRESS=[%s]:%d", 261 address6, me->ipv6_orport); 262 } 263 } 264 tor_free(address4); 265 tor_free(address6); 266 } 267 } 268 269 return TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT; 270 } 271 272 DECLARE_EVENT(reachability_warnings, ROUTER, FL(NEED_NET)); 273 274 /* Periodic callback: Every 30 seconds, check whether it's time to make new 275 * Ed25519 subkeys. 276 */ 277 static int 278 check_ed_keys_callback(time_t now, const or_options_t *options) 279 { 280 if (server_mode(options)) { 281 if (should_make_new_ed_keys(options, now)) { 282 int new_signing_key = load_ed_keys(options, now); 283 if (new_signing_key < 0 || 284 generate_ed_link_cert(options, now, new_signing_key > 0)) { 285 log_err(LD_OR, "Unable to update Ed25519 keys! Exiting."); 286 tor_shutdown_event_loop_and_exit(1); 287 } 288 } 289 return 30; 290 } 291 return PERIODIC_EVENT_NO_UPDATE; 292 } 293 294 DECLARE_EVENT(check_ed_keys, ROUTER, 0); 295 296 /* Period callback: Check if our old onion keys are still valid after the 297 * period of time defined by the consensus parameter 298 * "onion-key-grace-period-days", otherwise expire them by setting them to 299 * NULL. 300 */ 301 static int 302 check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options) 303 { 304 if (server_mode(options)) { 305 int onion_key_grace_period = get_onion_key_grace_period(); 306 time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period; 307 if (expiry_time > now) { 308 return ONION_KEY_CONSENSUS_CHECK_INTERVAL; 309 } 310 311 log_info(LD_GENERAL, "Expiring old onion keys."); 312 expire_old_onion_keys(); 313 cpuworkers_rotate_keyinfo(); 314 return ONION_KEY_CONSENSUS_CHECK_INTERVAL; 315 } 316 317 return PERIODIC_EVENT_NO_UPDATE; 318 } 319 320 DECLARE_EVENT(check_onion_keys_expiry_time, ROUTER, 0); 321 322 void 323 relay_register_periodic_events(void) 324 { 325 periodic_events_register(&retry_dns_event); 326 periodic_events_register(&check_dns_honesty_event); 327 periodic_events_register(&rotate_onion_key_event); 328 periodic_events_register(&check_descriptor_event); 329 periodic_events_register(&check_for_reachability_bw_event); 330 periodic_events_register(&reachability_warnings_event); 331 periodic_events_register(&check_ed_keys_event); 332 periodic_events_register(&check_onion_keys_expiry_time_event); 333 334 dns_honesty_first_time = 1; 335 dirport_reachability_count = 0; 336 } 337 338 /** 339 * Update our schedule so that we'll check whether we need to update our 340 * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL 341 * seconds. 342 */ 343 void 344 reschedule_descriptor_update_check(void) 345 { 346 periodic_event_reschedule(&check_descriptor_event); 347 }