selftest.c 11 KB

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