tor  0.4.2.1-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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
12 #include "orconfig.h"
13 #include "core/or/or.h"
14 
15 #include "core/mainloop/periodic.h"
16 #include "core/mainloop/cpuworker.h" // XXXX use a pubsub event.
17 #include "core/mainloop/mainloop.h"
18 #include "core/mainloop/netstatus.h"
19 #include "core/or/circuituse.h" // XXXX move have_performed_bandwidth_test
20 
21 #include "feature/relay/dns.h"
23 #include "feature/relay/router.h"
24 #include "feature/relay/routerkeys.h"
26 #include "feature/relay/selftest.h"
27 #include "feature/stats/predict_ports.h"
28 
30 
31 #include "feature/nodelist/routerinfo_st.h"
33 
34 #define DECLARE_EVENT(name, roles, flags) \
35  static periodic_event_item_t name ## _event = \
36  PERIODIC_EVENT(name, \
37  PERIODIC_EVENT_ROLE_##roles, \
38  flags)
39 
40 #define FL(name) (PERIODIC_EVENT_FLAG_##name)
41 
45 static int
46 retry_dns_callback(time_t now, const or_options_t *options)
47 {
48  (void)now;
49 #define RETRY_DNS_INTERVAL (10*60)
50  if (server_mode(options) && has_dns_init_failed())
51  dns_init();
52  return RETRY_DNS_INTERVAL;
53 }
54 
55 DECLARE_EVENT(retry_dns, ROUTER, 0);
56 
57 static int dns_honesty_first_time = 1;
58 
63 static int
64 check_dns_honesty_callback(time_t now, const or_options_t *options)
65 {
66  (void)now;
67  /* 9. and if we're an exit node, check whether our DNS is telling stories
68  * to us. */
69  if (net_is_disabled() ||
70  ! public_server_mode(options) ||
71  router_my_exit_policy_is_reject_star())
72  return PERIODIC_EVENT_NO_UPDATE;
73 
74  if (dns_honesty_first_time) {
75  /* Don't launch right when we start */
76  dns_honesty_first_time = 0;
77  return crypto_rand_int_range(60, 180);
78  }
79 
81  return 12*3600 + crypto_rand_int(12*3600);
82 }
83 
84 DECLARE_EVENT(check_dns_honesty, RELAY, FL(NEED_NET));
85 
86 /* Periodic callback: rotate the onion keys after the period defined by the
87  * "onion-key-rotation-days" consensus parameter, shut down and restart all
88  * cpuworkers, and update our descriptor if necessary.
89  */
90 static int
91 rotate_onion_key_callback(time_t now, const or_options_t *options)
92 {
93  if (server_mode(options)) {
94  int onion_key_lifetime = get_onion_key_lifetime();
95  time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime;
96  if (rotation_time > now) {
98  }
99 
100  log_info(LD_GENERAL,"Rotating onion key.");
103  if (router_rebuild_descriptor(1)<0) {
104  log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
105  }
106  if (advertised_server_mode() && !net_is_disabled())
109  }
110  return PERIODIC_EVENT_NO_UPDATE;
111 }
112 
113 DECLARE_EVENT(rotate_onion_key, ROUTER, 0);
114 
117 static int
118 check_descriptor_callback(time_t now, const or_options_t *options)
119 {
123 #define CHECK_DESCRIPTOR_INTERVAL (60)
124 
125  (void)options;
126 
127  /* 2b. Once per minute, regenerate and upload the descriptor if the old
128  * one is inaccurate. */
129  if (!net_is_disabled()) {
134  }
135 
136  return CHECK_DESCRIPTOR_INTERVAL;
137 }
138 
139 DECLARE_EVENT(check_descriptor, ROUTER, FL(NEED_NET));
140 
141 static int dirport_reachability_count = 0;
142 
148 static int
150 {
151  /* XXXX This whole thing was stuck in the middle of what is now
152  * XXXX check_descriptor_callback. I'm not sure it's right. */
153 
154  /* also, check religiously for reachability, if it's within the first
155  * 20 minutes of our uptime. */
156  if (server_mode(options) &&
158  !net_is_disabled()) {
159  if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
160  router_do_reachability_checks(1, dirport_reachability_count==0);
161  if (++dirport_reachability_count > 5)
162  dirport_reachability_count = 0;
163  return 1;
164  } else {
165  /* If we haven't checked for 12 hours and our bandwidth estimate is
166  * low, do another bandwidth test. This is especially important for
167  * bridges, since they might go long periods without much use. */
168  const routerinfo_t *me = router_get_my_routerinfo();
169  static int first_time = 1;
170  if (!first_time && me &&
171  me->bandwidthcapacity < me->bandwidthrate &&
172  me->bandwidthcapacity < 51200) {
174  }
175  first_time = 0;
176 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
177  return BANDWIDTH_RECHECK_INTERVAL;
178  }
179  }
180  return CHECK_DESCRIPTOR_INTERVAL;
181 }
182 
183 DECLARE_EVENT(check_for_reachability_bw, ROUTER, FL(NEED_NET));
184 
188 static int
189 reachability_warnings_callback(time_t now, const or_options_t *options)
190 {
191  (void) now;
192 
193  if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
194  return (int)(TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT - get_uptime());
195  }
196 
197  if (server_mode(options) &&
198  !net_is_disabled() &&
200  /* every 20 minutes, check and complain if necessary */
201  const routerinfo_t *me = router_get_my_routerinfo();
202  if (me && !check_whether_orport_reachable(options)) {
203  char *address = tor_dup_ip(me->addr);
204  log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
205  "its ORPort is reachable. Relays do not publish descriptors "
206  "until their ORPort and DirPort are reachable. Please check "
207  "your firewalls, ports, address, /etc/hosts file, etc.",
208  address, me->or_port);
210  "REACHABILITY_FAILED ORADDRESS=%s:%d",
211  address, me->or_port);
212  tor_free(address);
213  }
214 
215  if (me && !check_whether_dirport_reachable(options)) {
216  char *address = tor_dup_ip(me->addr);
217  log_warn(LD_CONFIG,
218  "Your server (%s:%d) has not managed to confirm that its "
219  "DirPort is reachable. Relays do not publish descriptors "
220  "until their ORPort and DirPort are reachable. Please check "
221  "your firewalls, ports, address, /etc/hosts file, etc.",
222  address, me->dir_port);
224  "REACHABILITY_FAILED DIRADDRESS=%s:%d",
225  address, me->dir_port);
226  tor_free(address);
227  }
228  }
229 
231 }
232 
233 DECLARE_EVENT(reachability_warnings, ROUTER, FL(NEED_NET));
234 
235 /* Periodic callback: Every 30 seconds, check whether it's time to make new
236  * Ed25519 subkeys.
237  */
238 static int
239 check_ed_keys_callback(time_t now, const or_options_t *options)
240 {
241  if (server_mode(options)) {
242  if (should_make_new_ed_keys(options, now)) {
243  int new_signing_key = load_ed_keys(options, now);
244  if (new_signing_key < 0 ||
245  generate_ed_link_cert(options, now, new_signing_key > 0)) {
246  log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
248  }
249  }
250  return 30;
251  }
252  return PERIODIC_EVENT_NO_UPDATE;
253 }
254 
255 DECLARE_EVENT(check_ed_keys, ROUTER, 0);
256 
257 /* Period callback: Check if our old onion keys are still valid after the
258  * period of time defined by the consensus parameter
259  * "onion-key-grace-period-days", otherwise expire them by setting them to
260  * NULL.
261  */
262 static int
263 check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options)
264 {
265  if (server_mode(options)) {
266  int onion_key_grace_period = get_onion_key_grace_period();
267  time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period;
268  if (expiry_time > now) {
270  }
271 
272  log_info(LD_GENERAL, "Expiring old onion keys.");
276  }
277 
278  return PERIODIC_EVENT_NO_UPDATE;
279 }
280 
281 DECLARE_EVENT(check_onion_keys_expiry_time, ROUTER, 0);
282 
283 void
284 relay_register_periodic_events(void)
285 {
286  periodic_events_register(&retry_dns_event);
287  periodic_events_register(&check_dns_honesty_event);
288  periodic_events_register(&rotate_onion_key_event);
289  periodic_events_register(&check_descriptor_event);
290  periodic_events_register(&check_for_reachability_bw_event);
291  periodic_events_register(&reachability_warnings_event);
292  periodic_events_register(&check_ed_keys_event);
293  periodic_events_register(&check_onion_keys_expiry_time_event);
294 
295  dns_honesty_first_time = 1;
296  dirport_reachability_count = 0;
297 }
298 
304 void
306 {
307  periodic_event_reschedule(&check_descriptor_event);
308 }
static int check_descriptor_callback(time_t now, const or_options_t *options)
int router_rebuild_descriptor(int force)
Definition: router.c:2332
Common functions for using (pseudo-)random number generators.
#define ONION_KEY_CONSENSUS_CHECK_INTERVAL
Definition: or.h:152
static int check_for_reachability_bw_callback(time_t now, const or_options_t *options)
#define LD_GENERAL
Definition: log.h:60
void reschedule_descriptor_update_check(void)
int dns_init(void)
Definition: dns.c:224
int any_predicted_circuits(time_t now)
static int retry_dns_callback(time_t now, const or_options_t *options)
void router_do_reachability_checks(int test_or, int test_dir)
Definition: selftest.c:172
Header file for cpuworker.c.
int crypto_rand_int_range(unsigned int min, unsigned int max)
time_t get_onion_key_set_at(void)
Definition: router.c:329
#define tor_free(p)
Definition: malloc.h:52
void tor_shutdown_event_loop_and_exit(int exitcode)
Definition: mainloop.c:764
Header file for mainloop.c.
static int check_dns_honesty_callback(time_t now, const or_options_t *options)
void check_descriptor_ipaddress_changed(time_t now)
Definition: router.c:2524
void periodic_events_register(periodic_event_item_t *item)
Definition: periodic.c:215
uint32_t bandwidthrate
Definition: routerinfo_st.h:51
Header for feature/relay/relay_periodic.c.
static int reachability_warnings_callback(time_t now, const or_options_t *options)
Header file for routermode.c.
int has_dns_init_failed(void)
Definition: dns.c:266
int load_ed_keys(const or_options_t *options, time_t now)
Definition: routerkeys.c:55
void mark_my_descriptor_dirty_if_too_old(time_t now)
Definition: router.c:2386
Master header file for Tor-specific functionality.
int crypto_rand_int(unsigned int max)
int should_make_new_ed_keys(const or_options_t *options, const time_t now)
Definition: routerkeys.c:421
void periodic_event_reschedule(periodic_event_item_t *event)
Definition: periodic.c:107
#define LOG_WARN
Definition: log.h:51
Header file for circuituse.c.
void cpuworkers_rotate_keyinfo(void)
Definition: cpuworker.c:188
#define LD_OR
Definition: log.h:90
int get_onion_key_grace_period(void)
Definition: router.c:777
int generate_ed_link_cert(const or_options_t *options, time_t now, int force)
Definition: routerkeys.c:365
void router_upload_dir_desc_to_dirservers(int force)
Definition: router.c:1499
void expire_old_onion_keys(void)
Definition: router.c:235
void rotate_onion_key(void)
Definition: router.c:465
Header file for router.c.
void consider_publishable_server(int force)
Definition: router.c:1387
uint16_t dir_port
Definition: routerinfo_st.h:21
uint32_t bandwidthcapacity
Definition: routerinfo_st.h:55
int get_onion_key_lifetime(void)
Definition: router.c:767
uint32_t addr
Definition: routerinfo_st.h:19
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
Definition: or.h:549
Header file for selftest.c.
void reset_bandwidth_test(void)
Definition: circuituse.c:1611
int control_event_server_status(int severity, const char *format,...)
Header file for control_events.c.
void check_descriptor_bandwidth_changed(time_t now)
Definition: router.c:2456
int check_whether_orport_reachable(const or_options_t *options)
Definition: selftest.c:75
Header file for dns.c.
#define LD_CONFIG
Definition: log.h:66
uint16_t or_port
Definition: routerinfo_st.h:20
void dns_launch_correctness_checks(void)
Definition: dns.c:2044
int check_whether_dirport_reachable(const or_options_t *options)
Definition: selftest.c:92
int have_completed_a_circuit(void)
Definition: mainloop.c:218
char * tor_dup_ip(uint32_t addr)
Definition: address.c:1948