Tor 0.4.9.0-alpha-dev
relay_periodic.c
Go to the documentation of this file.
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
16
18#include "core/mainloop/cpuworker.h" // XXXX use a pubsub event.
21#include "core/or/circuituse.h" // XXXX move have_performed_bandwidth_test
22
23#include "feature/relay/dns.h"
30
32
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 */
49static int
50retry_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
59DECLARE_EVENT(retry_dns, ROUTER, 0);
60
61static 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 */
67static int
68check_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) ||
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
85 return 12*3600 + crypto_rand_int(12*3600);
86}
87
88DECLARE_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 */
94static int
95rotate_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) {
102 }
103
104 log_info(LD_GENERAL,"Rotating onion key.");
105 if (!rotate_onion_key()) {
107 }
110 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
111 }
115 }
116 return PERIODIC_EVENT_NO_UPDATE;
117}
118
119DECLARE_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). */
123static int
124check_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()) {
140 }
141
142 return CHECK_DESCRIPTOR_INTERVAL;
143}
144
145DECLARE_EVENT(check_descriptor, ROUTER, FL(NEED_NET));
146
147static 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 */
154static int
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) &&
167 !net_is_disabled()) {
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. */
176 static int first_time = 1;
177 if (!first_time && me &&
179 me->bandwidthcapacity < 51200) {
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
190DECLARE_EVENT(check_for_reachability_bw, ROUTER, FL(NEED_NET));
191
192/**
193 * Callback: Send warnings if Tor doesn't find its ports reachable.
194 */
195static int
197{
198 (void) now;
199
202 }
203
204 if (server_mode(options) &&
205 !net_is_disabled() &&
207 /* every 20 minutes, check and complain if necessary */
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);
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 and DirPort 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) {
255 "REACHABILITY_FAILED ORADDRESS=%s:%d",
256 address4, me->ipv4_orport);
257 }
258 if (!v6_ok) {
260 "REACHABILITY_FAILED ORADDRESS=[%s]:%d",
261 address6, me->ipv6_orport);
262 }
263 }
264 tor_free(address4);
265 tor_free(address6);
266 }
267 }
268
270}
271
272DECLARE_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 */
277static int
278check_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.");
287 }
288 }
289 return 30;
290 }
291 return PERIODIC_EVENT_NO_UPDATE;
292}
293
294DECLARE_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 */
301static int
302check_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) {
309 }
310
311 log_info(LD_GENERAL, "Expiring old onion keys.");
315 }
316
317 return PERIODIC_EVENT_NO_UPDATE;
318}
319
320DECLARE_EVENT(check_onion_keys_expiry_time, ROUTER, 0);
321
322void
323relay_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 */
343void
345{
346 periodic_event_reschedule(&check_descriptor_event);
347}
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1164
void reset_bandwidth_test(void)
Definition: circuituse.c:1582
Header file for circuituse.c.
int control_event_server_status(int severity, const char *format,...)
Header file for control_events.c.
void cpuworkers_rotate_keyinfo(void)
Definition: cpuworker.c:242
Header file for cpuworker.c.
Common functions for using (pseudo-)random number generators.
int crypto_rand_int_range(unsigned int min, unsigned int max)
int crypto_rand_int(unsigned int max)
int dns_init(void)
Definition: dns.c:233
int has_dns_init_failed(void)
Definition: dns.c:274
void dns_launch_correctness_checks(void)
Definition: dns.c:2117
Header file for dns.c.
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:288
#define LD_OR
Definition: log.h:92
#define LD_GENERAL
Definition: log.h:62
#define LOG_NOTICE
Definition: log.h:50
#define LD_CONFIG
Definition: log.h:68
#define LOG_WARN
Definition: log.h:53
int have_completed_a_circuit(void)
Definition: mainloop.c:218
void tor_shutdown_event_loop_and_exit(int exitcode)
Definition: mainloop.c:773
long get_uptime(void)
Definition: mainloop.c:2557
Header file for mainloop.c.
#define tor_free(p)
Definition: malloc.h:56
int net_is_disabled(void)
Definition: netstatus.c:25
Header for netstatus.c.
Master header file for Tor-specific functionality.
#define ONION_KEY_CONSENSUS_CHECK_INTERVAL
Definition: or.h:151
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
Definition: or.h:442
void periodic_events_register(periodic_event_item_t *item)
Definition: periodic.c:219
void periodic_event_reschedule(periodic_event_item_t *event)
Definition: periodic.c:106
Header for periodic.c.
int any_predicted_circuits(time_t now)
Header file for predict_ports.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
void reschedule_descriptor_update_check(void)
static int reachability_warnings_callback(time_t now, const or_options_t *options)
static int check_dns_honesty_callback(time_t now, const or_options_t *options)
static int check_for_reachability_bw_callback(time_t now, const or_options_t *options)
static int check_descriptor_callback(time_t now, const or_options_t *options)
static int retry_dns_callback(time_t now, const or_options_t *options)
Header for feature/relay/relay_periodic.c.
bool resolved_addr_is_configured(int family)
Definition: resolve_addr.c:109
Header file for resolve_addr.c.
void consider_publishable_server(int force)
Definition: router.c:1466
void router_upload_dir_desc_to_dirservers(int force)
Definition: router.c:1651
int get_onion_key_grace_period(void)
Definition: router.c:804
void check_descriptor_ipaddress_changed(time_t now)
Definition: router.c:2681
void mark_my_descriptor_if_omit_ipv6_changes(const char *reason, bool omit_ipv6)
Definition: router.c:2511
time_t get_onion_key_set_at(void)
Definition: router.c:344
bool rotate_onion_key(void)
Definition: router.c:489
void expire_old_onion_keys(void)
Definition: router.c:252
bool router_rebuild_descriptor(int force)
Definition: router.c:2456
void check_descriptor_bandwidth_changed(time_t now)
Definition: router.c:2603
int get_onion_key_lifetime(void)
Definition: router.c:794
void mark_my_descriptor_dirty_if_too_old(time_t now)
Definition: router.c:2533
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1806
int router_my_exit_policy_is_reject_star(void)
Definition: router.c:1732
Header file for router.c.
Router descriptor structure.
int load_ed_keys(const or_options_t *options, time_t now)
Definition: routerkeys.c:55
int should_make_new_ed_keys(const or_options_t *options, const time_t now)
Definition: routerkeys.c:419
int generate_ed_link_cert(const or_options_t *options, time_t now, int force)
Definition: routerkeys.c:365
Header for routerkeys.c.
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
int advertised_server_mode(void)
Definition: routermode.c:55
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
int router_orport_seems_reachable(const or_options_t *options, int family)
Definition: selftest.c:101
void router_do_reachability_checks(void)
Definition: selftest.c:292
Header file for selftest.c.
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:30
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:25
uint32_t bandwidthrate
Definition: routerinfo_st.h:54
uint32_t bandwidthcapacity
Definition: routerinfo_st.h:58