LCOV - code coverage report
Current view: top level - feature/relay - relay_periodic.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 15 121 12.4 %
Date: 2021-11-24 03:28:48 Functions: 2 10 20.0 %

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

Generated by: LCOV version 1.14