selftest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file selftest.c
  8. * \brief Relay self-testing
  9. *
  10. * Relays need to make sure that their own ports are reasonable, and estimate
  11. * their own bandwidth, before publishing.
  12. */
  13. #define SELFTEST_PRIVATE
  14. #include "core/or/or.h"
  15. #include "app/config/config.h"
  16. #include "core/mainloop/connection.h"
  17. #include "core/mainloop/mainloop.h"
  18. #include "core/or/circuitbuild.h"
  19. #include "core/or/circuitlist.h"
  20. #include "core/or/circuituse.h"
  21. #include "core/or/crypt_path_st.h"
  22. #include "core/or/origin_circuit_st.h"
  23. #include "core/or/relay.h"
  24. #include "feature/control/control.h"
  25. #include "feature/dirclient/dirclient.h"
  26. #include "feature/dircommon/directory.h"
  27. #include "feature/nodelist/authority_cert_st.h"
  28. #include "feature/nodelist/routerinfo_st.h"
  29. #include "feature/nodelist/routerlist.h" // but...
  30. #include "feature/nodelist/routerset.h"
  31. #include "feature/nodelist/torcert.h"
  32. #include "feature/relay/router.h"
  33. #include "feature/relay/selftest.h"
  34. /** Whether we can reach our ORPort from the outside. */
  35. static int can_reach_or_port = 0;
  36. /** Whether we can reach our DirPort from the outside. */
  37. static int can_reach_dir_port = 0;
  38. /** Forget what we have learned about our reachability status. */
  39. void
  40. router_reset_reachability(void)
  41. {
  42. can_reach_or_port = can_reach_dir_port = 0;
  43. }
  44. /** Return 1 if we won't do reachability checks, because:
  45. * - AssumeReachable is set, or
  46. * - the network is disabled.
  47. * Otherwise, return 0.
  48. */
  49. static int
  50. router_reachability_checks_disabled(const or_options_t *options)
  51. {
  52. return options->AssumeReachable ||
  53. net_is_disabled();
  54. }
  55. /** Return 0 if we need to do an ORPort reachability check, because:
  56. * - no reachability check has been done yet, or
  57. * - we've initiated reachability checks, but none have succeeded.
  58. * Return 1 if we don't need to do an ORPort reachability check, because:
  59. * - we've seen a successful reachability check, or
  60. * - AssumeReachable is set, or
  61. * - the network is disabled.
  62. */
  63. int
  64. check_whether_orport_reachable(const or_options_t *options)
  65. {
  66. int reach_checks_disabled = router_reachability_checks_disabled(options);
  67. return reach_checks_disabled ||
  68. can_reach_or_port;
  69. }
  70. /** Return 0 if we need to do a DirPort reachability check, because:
  71. * - no reachability check has been done yet, or
  72. * - we've initiated reachability checks, but none have succeeded.
  73. * Return 1 if we don't need to do a DirPort reachability check, because:
  74. * - we've seen a successful reachability check, or
  75. * - there is no DirPort set, or
  76. * - AssumeReachable is set, or
  77. * - the network is disabled.
  78. */
  79. int
  80. check_whether_dirport_reachable(const or_options_t *options)
  81. {
  82. int reach_checks_disabled = router_reachability_checks_disabled(options) ||
  83. !options->DirPort_set;
  84. return reach_checks_disabled ||
  85. can_reach_dir_port;
  86. }
  87. /** See if we currently believe our ORPort or DirPort to be
  88. * unreachable. If so, return 1 else return 0.
  89. */
  90. static int
  91. router_should_check_reachability(int test_or, int test_dir)
  92. {
  93. const routerinfo_t *me = router_get_my_routerinfo();
  94. const or_options_t *options = get_options();
  95. if (!me)
  96. return 0;
  97. if (routerset_contains_router(options->ExcludeNodes, me, -1) &&
  98. options->StrictNodes) {
  99. /* If we've excluded ourself, and StrictNodes is set, we can't test
  100. * ourself. */
  101. if (test_or || test_dir) {
  102. #define SELF_EXCLUDED_WARN_INTERVAL 3600
  103. static ratelim_t warning_limit=RATELIM_INIT(SELF_EXCLUDED_WARN_INTERVAL);
  104. log_fn_ratelim(&warning_limit, LOG_WARN, LD_CIRC,
  105. "Can't peform self-tests for this relay: we have "
  106. "listed ourself in ExcludeNodes, and StrictNodes is set. "
  107. "We cannot learn whether we are usable, and will not "
  108. "be able to advertise ourself.");
  109. }
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. /** Allocate and return a new extend_info_t that can be used to build
  115. * a circuit to or through the router <b>r</b>. Uses the primary
  116. * address of the router, so should only be called on a server. */
  117. static extend_info_t *
  118. extend_info_from_router(const routerinfo_t *r)
  119. {
  120. crypto_pk_t *rsa_pubkey;
  121. extend_info_t *info;
  122. tor_addr_port_t ap;
  123. tor_assert(r);
  124. /* Make sure we don't need to check address reachability */
  125. tor_assert_nonfatal(router_skip_or_reachability(get_options(), 0));
  126. const ed25519_public_key_t *ed_id_key;
  127. if (r->cache_info.signing_key_cert)
  128. ed_id_key = &r->cache_info.signing_key_cert->signing_key;
  129. else
  130. ed_id_key = NULL;
  131. router_get_prim_orport(r, &ap);
  132. rsa_pubkey = router_get_rsa_onion_pkey(r->onion_pkey, r->onion_pkey_len);
  133. info = extend_info_new(r->nickname, r->cache_info.identity_digest,
  134. ed_id_key,
  135. rsa_pubkey, r->onion_curve25519_pkey,
  136. &ap.addr, ap.port);
  137. crypto_pk_free(rsa_pubkey);
  138. return info;
  139. }
  140. /** Some time has passed, or we just got new directory information.
  141. * See if we currently believe our ORPort or DirPort to be
  142. * unreachable. If so, launch a new test for it.
  143. *
  144. * For ORPort, we simply try making a circuit that ends at ourselves.
  145. * Success is noticed in onionskin_answer().
  146. *
  147. * For DirPort, we make a connection via Tor to our DirPort and ask
  148. * for our own server descriptor.
  149. * Success is noticed in connection_dir_client_reached_eof().
  150. */
  151. void
  152. router_do_reachability_checks(int test_or, int test_dir)
  153. {
  154. const routerinfo_t *me = router_get_my_routerinfo();
  155. const or_options_t *options = get_options();
  156. int orport_reachable = check_whether_orport_reachable(options);
  157. tor_addr_t addr;
  158. if (router_should_check_reachability(test_or, test_dir)) {
  159. if (test_or && (!orport_reachable || !circuit_enough_testing_circs())) {
  160. extend_info_t *ei = extend_info_from_router(me);
  161. /* XXX IPv6 self testing */
  162. log_info(LD_CIRC, "Testing %s of my ORPort: %s:%d.",
  163. !orport_reachable ? "reachability" : "bandwidth",
  164. fmt_addr32(me->addr), me->or_port);
  165. circuit_launch_by_extend_info(CIRCUIT_PURPOSE_TESTING, ei,
  166. CIRCLAUNCH_NEED_CAPACITY|CIRCLAUNCH_IS_INTERNAL);
  167. extend_info_free(ei);
  168. }
  169. /* XXX IPv6 self testing */
  170. tor_addr_from_ipv4h(&addr, me->addr);
  171. if (test_dir && !check_whether_dirport_reachable(options) &&
  172. !connection_get_by_type_addr_port_purpose(
  173. CONN_TYPE_DIR, &addr, me->dir_port,
  174. DIR_PURPOSE_FETCH_SERVERDESC)) {
  175. tor_addr_port_t my_orport, my_dirport;
  176. memcpy(&my_orport.addr, &addr, sizeof(addr));
  177. memcpy(&my_dirport.addr, &addr, sizeof(addr));
  178. my_orport.port = me->or_port;
  179. my_dirport.port = me->dir_port;
  180. /* ask myself, via tor, for my server descriptor. */
  181. directory_request_t *req =
  182. directory_request_new(DIR_PURPOSE_FETCH_SERVERDESC);
  183. directory_request_set_or_addr_port(req, &my_orport);
  184. directory_request_set_dir_addr_port(req, &my_dirport);
  185. directory_request_set_directory_id_digest(req,
  186. me->cache_info.identity_digest);
  187. // ask via an anon circuit, connecting to our dirport.
  188. directory_request_set_indirection(req, DIRIND_ANON_DIRPORT);
  189. directory_request_set_resource(req, "authority.z");
  190. directory_initiate_request(req);
  191. directory_request_free(req);
  192. }
  193. }
  194. }
  195. /** Annotate that we found our ORPort reachable. */
  196. void
  197. router_orport_found_reachable(void)
  198. {
  199. const routerinfo_t *me = router_get_my_routerinfo();
  200. const or_options_t *options = get_options();
  201. if (!can_reach_or_port && me) {
  202. char *address = tor_dup_ip(me->addr);
  203. log_notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
  204. "the outside. Excellent.%s",
  205. options->PublishServerDescriptor_ != NO_DIRINFO
  206. && check_whether_dirport_reachable(options) ?
  207. " Publishing server descriptor." : "");
  208. can_reach_or_port = 1;
  209. mark_my_descriptor_dirty("ORPort found reachable");
  210. /* This is a significant enough change to upload immediately,
  211. * at least in a test network */
  212. if (options->TestingTorNetwork == 1) {
  213. reschedule_descriptor_update_check();
  214. }
  215. control_event_server_status(LOG_NOTICE,
  216. "REACHABILITY_SUCCEEDED ORADDRESS=%s:%d",
  217. address, me->or_port);
  218. tor_free(address);
  219. }
  220. }
  221. /** Annotate that we found our DirPort reachable. */
  222. void
  223. router_dirport_found_reachable(void)
  224. {
  225. const routerinfo_t *me = router_get_my_routerinfo();
  226. const or_options_t *options = get_options();
  227. if (!can_reach_dir_port && me) {
  228. char *address = tor_dup_ip(me->addr);
  229. log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
  230. "from the outside. Excellent.%s",
  231. options->PublishServerDescriptor_ != NO_DIRINFO
  232. && check_whether_orport_reachable(options) ?
  233. " Publishing server descriptor." : "");
  234. can_reach_dir_port = 1;
  235. if (router_should_advertise_dirport(options, me->dir_port)) {
  236. mark_my_descriptor_dirty("DirPort found reachable");
  237. /* This is a significant enough change to upload immediately,
  238. * at least in a test network */
  239. if (options->TestingTorNetwork == 1) {
  240. reschedule_descriptor_update_check();
  241. }
  242. }
  243. control_event_server_status(LOG_NOTICE,
  244. "REACHABILITY_SUCCEEDED DIRADDRESS=%s:%d",
  245. address, me->dir_port);
  246. tor_free(address);
  247. }
  248. }
  249. /** We have enough testing circuits open. Send a bunch of "drop"
  250. * cells down each of them, to exercise our bandwidth. */
  251. void
  252. router_perform_bandwidth_test(int num_circs, time_t now)
  253. {
  254. int num_cells = (int)(get_options()->BandwidthRate * 10 /
  255. CELL_MAX_NETWORK_SIZE);
  256. int max_cells = num_cells < CIRCWINDOW_START ?
  257. num_cells : CIRCWINDOW_START;
  258. int cells_per_circuit = max_cells / num_circs;
  259. origin_circuit_t *circ = NULL;
  260. log_notice(LD_OR,"Performing bandwidth self-test...done.");
  261. while ((circ = circuit_get_next_by_pk_and_purpose(circ, NULL,
  262. CIRCUIT_PURPOSE_TESTING))) {
  263. /* dump cells_per_circuit drop cells onto this circ */
  264. int i = cells_per_circuit;
  265. if (circ->base_.state != CIRCUIT_STATE_OPEN)
  266. continue;
  267. circ->base_.timestamp_dirty = now;
  268. while (i-- > 0) {
  269. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  270. RELAY_COMMAND_DROP,
  271. NULL, 0, circ->cpath->prev)<0) {
  272. return; /* stop if error */
  273. }
  274. }
  275. }
  276. }