156-tracking-blocked-ports.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. Filename: 156-tracking-blocked-ports.txt
  2. Title: Tracking blocked ports on the client side
  3. Author: Robert Hogan
  4. Created: 14-Oct-2008
  5. Status: Open
  6. Target: 0.2.?
  7. Motivation:
  8. Tor clients that are behind extremely restrictive firewalls can end up
  9. waiting a while for their first successful OR connection to a node on the
  10. network. Worse, the more restrictive their firewall the more susceptible
  11. they are to an attacker guessing their entry nodes. Tor routers that
  12. are behind extremely restrictive firewalls can only offer a limited,
  13. 'partitioned' service to other routers and clients on the network. Exit
  14. nodes behind extremely restrictive firewalls may advertise ports that they
  15. are actually not able to connect to, wasting network resources in circuit
  16. constructions that are doomed to fail at the last hop on first use.
  17. Proposal:
  18. When a client attempts to connect to an entry guard it should avoid
  19. further attempts on ports that fail once until it has connected to at
  20. least one entry guard successfully. (Maybe it should wait for more than
  21. one failure to reduce the skew on the first node selection.) Thereafter
  22. it should select entry guards regardless of port and warn the user if
  23. it observes that connections to a given port have failed every multiple
  24. of 5 times without success or since the last success.
  25. Tor should warn the operators of exit, middleman and entry nodes if it
  26. observes that connections to a given port have failed a multiple of 5
  27. times without success or since the last success. If attempts on a port
  28. fail 20 or more times without or since success, Tor should add the port
  29. to a 'blocked-ports' entry in its descriptor's extra-info. Some thought
  30. needs to be given to what the authorities might do with this information.
  31. Related TODO item:
  32. "- Automatically determine what ports are reachable and start using
  33. those, if circuits aren't working and it's a pattern we
  34. recognize ("port 443 worked once and port 9001 keeps not
  35. working")."
  36. I've had a go at implementing all of this in the attached.
  37. Addendum:
  38. Just a note on the patch, storing the digest of each router that uses the port
  39. is a bit of a memory hog, and its only real purpose is to provide a count of
  40. routers using that port when warning the user. That could be achieved when
  41. warning the user by iterating through the routerlist instead.
  42. Index: src/or/connection_or.c
  43. ===================================================================
  44. --- src/or/connection_or.c (revision 17104)
  45. +++ src/or/connection_or.c (working copy)
  46. @@ -502,6 +502,9 @@
  47. connection_or_connect_failed(or_connection_t *conn,
  48. int reason, const char *msg)
  49. {
  50. + if ((reason == END_OR_CONN_REASON_NO_ROUTE) ||
  51. + (reason == END_OR_CONN_REASON_REFUSED))
  52. + or_port_hist_failure(conn->identity_digest,TO_CONN(conn)->port);
  53. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED, reason);
  54. if (!authdir_mode_tests_reachability(get_options()))
  55. control_event_bootstrap_problem(msg, reason);
  56. @@ -580,6 +583,7 @@
  57. /* already marked for close */
  58. return NULL;
  59. }
  60. +
  61. return conn;
  62. }
  63. @@ -909,6 +913,7 @@
  64. control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED, 0);
  65. if (started_here) {
  66. + or_port_hist_success(TO_CONN(conn)->port);
  67. rep_hist_note_connect_succeeded(conn->identity_digest, now);
  68. if (entry_guard_register_connect_status(conn->identity_digest,
  69. 1, now) < 0) {
  70. Index: src/or/rephist.c
  71. ===================================================================
  72. --- src/or/rephist.c (revision 17104)
  73. +++ src/or/rephist.c (working copy)
  74. @@ -18,6 +18,7 @@
  75. static void bw_arrays_init(void);
  76. static void predicted_ports_init(void);
  77. static void hs_usage_init(void);
  78. +static void or_port_hist_init(void);
  79. /** Total number of bytes currently allocated in fields used by rephist.c. */
  80. uint64_t rephist_total_alloc=0;
  81. @@ -89,6 +90,25 @@
  82. digestmap_t *link_history_map;
  83. } or_history_t;
  84. +/** or_port_hist_t contains our router/client's knowledge of
  85. + all OR ports offered on the network, and how many servers with each port we
  86. + have succeeded or failed to connect to. */
  87. +typedef struct {
  88. + /** The port this entry is tracking. */
  89. + uint16_t or_port;
  90. + /** Have we ever connected to this port on another OR?. */
  91. + unsigned int success:1;
  92. + /** The ORs using this port. */
  93. + digestmap_t *ids;
  94. + /** The ORs using this port we have failed to connect to. */
  95. + digestmap_t *failure_ids;
  96. + /** Are we excluding ORs with this port during entry selection?*/
  97. + unsigned int excluded;
  98. +} or_port_hist_t;
  99. +
  100. +static unsigned int still_searching = 0;
  101. +static smartlist_t *or_port_hists;
  102. +
  103. /** When did we last multiply all routers' weighted_run_length and
  104. * total_run_weights by STABILITY_ALPHA? */
  105. static time_t stability_last_downrated = 0;
  106. @@ -164,6 +184,16 @@
  107. tor_free(hist);
  108. }
  109. +/** Helper: free storage held by a single OR port history entry. */
  110. +static void
  111. +or_port_hist_free(or_port_hist_t *p)
  112. +{
  113. + tor_assert(p);
  114. + digestmap_free(p->ids,NULL);
  115. + digestmap_free(p->failure_ids,NULL);
  116. + tor_free(p);
  117. +}
  118. +
  119. /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
  120. * count is up-to-date as of <b>when</b>.
  121. */
  122. @@ -1639,7 +1669,7 @@
  123. tmp_time = smartlist_get(predicted_ports_times, i);
  124. if (*tmp_time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
  125. tmp_port = smartlist_get(predicted_ports_list, i);
  126. - log_debug(LD_CIRC, "Expiring predicted port %d", *tmp_port);
  127. + log_debug(LD_HIST, "Expiring predicted port %d", *tmp_port);
  128. smartlist_del(predicted_ports_list, i);
  129. smartlist_del(predicted_ports_times, i);
  130. rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
  131. @@ -1821,6 +1851,12 @@
  132. tor_free(last_stability_doc);
  133. built_last_stability_doc_at = 0;
  134. predicted_ports_free();
  135. + if (or_port_hists) {
  136. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, p,
  137. + or_port_hist_free(p));
  138. + smartlist_free(or_port_hists);
  139. + or_port_hists = NULL;
  140. + }
  141. }
  142. /****************** hidden service usage statistics ******************/
  143. @@ -2356,3 +2392,225 @@
  144. tor_free(fname);
  145. }
  146. +/** Create a new entry in the port tracking cache for the or_port in
  147. + * <b>ri</b>. */
  148. +void
  149. +or_port_hist_new(const routerinfo_t *ri)
  150. +{
  151. + or_port_hist_t *result;
  152. + const char *id=ri->cache_info.identity_digest;
  153. +
  154. + if (!or_port_hists)
  155. + or_port_hist_init();
  156. +
  157. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp,
  158. + {
  159. + /* Cope with routers that change their advertised OR port or are
  160. + dropped from the networkstatus. We don't discard the failures of
  161. + dropped routers because they are still valid when counting
  162. + consecutive failures on a port.*/
  163. + if (digestmap_get(tp->ids, id) && (tp->or_port != ri->or_port)) {
  164. + digestmap_remove(tp->ids, id);
  165. + }
  166. + if (tp->or_port == ri->or_port) {
  167. + if (!(digestmap_get(tp->ids, id)))
  168. + digestmap_set(tp->ids, id, (void*)1);
  169. + return;
  170. + }
  171. + });
  172. +
  173. + result = tor_malloc_zero(sizeof(or_port_hist_t));
  174. + result->or_port=ri->or_port;
  175. + result->success=0;
  176. + result->ids=digestmap_new();
  177. + digestmap_set(result->ids, id, (void*)1);
  178. + result->failure_ids=digestmap_new();
  179. + result->excluded=0;
  180. + smartlist_add(or_port_hists, result);
  181. +}
  182. +
  183. +/** Create the port tracking cache. */
  184. +/*XXX: need to call this when we rebuild/update our network status */
  185. +static void
  186. +or_port_hist_init(void)
  187. +{
  188. + routerlist_t *rl = router_get_routerlist();
  189. +
  190. + if (!or_port_hists)
  191. + or_port_hists=smartlist_create();
  192. +
  193. + if (rl && rl->routers) {
  194. + SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  195. + {
  196. + or_port_hist_new(ri);
  197. + });
  198. + }
  199. +}
  200. +
  201. +#define NOT_BLOCKED 0
  202. +#define FAILURES_OBSERVED 1
  203. +#define POSSIBLY_BLOCKED 5
  204. +#define PROBABLY_BLOCKED 10
  205. +/** Return the list of blocked ports for our router's extra-info.*/
  206. +char *
  207. +or_port_hist_get_blocked_ports(void)
  208. +{
  209. + char blocked_ports[2048];
  210. + char *bp;
  211. +
  212. + tor_snprintf(blocked_ports,sizeof(blocked_ports),"blocked-ports");
  213. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp,
  214. + {
  215. + if (digestmap_size(tp->failure_ids) >= PROBABLY_BLOCKED)
  216. + tor_snprintf(blocked_ports+strlen(blocked_ports),
  217. + sizeof(blocked_ports)," %u,",tp->or_port);
  218. + });
  219. + if (strlen(blocked_ports) == 13)
  220. + return NULL;
  221. + bp=tor_strdup(blocked_ports);
  222. + bp[strlen(bp)-1]='\n';
  223. + bp[strlen(bp)]='\0';
  224. + return bp;
  225. +}
  226. +
  227. +/** Revert to client-only mode if we have seen to many failures on a port or
  228. + * range of ports.*/
  229. +static void
  230. +or_port_hist_report_block(unsigned int min_severity)
  231. +{
  232. + or_options_t *options=get_options();
  233. + char failures_observed[2048],possibly_blocked[2048],probably_blocked[2048];
  234. + char port[1024];
  235. +
  236. + memset(failures_observed,0,sizeof(failures_observed));
  237. + memset(possibly_blocked,0,sizeof(possibly_blocked));
  238. + memset(probably_blocked,0,sizeof(probably_blocked));
  239. +
  240. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp,
  241. + {
  242. + unsigned int failures = digestmap_size(tp->failure_ids);
  243. + if (failures >= min_severity) {
  244. + tor_snprintf(port, sizeof(port), " %u (%u failures %s out of %u on the"
  245. + " network)",tp->or_port,failures,
  246. + (!tp->success)?"and no successes": "since last success",
  247. + digestmap_size(tp->ids));
  248. + if (failures >= PROBABLY_BLOCKED) {
  249. + strlcat(probably_blocked, port, sizeof(probably_blocked));
  250. + } else if (failures >= POSSIBLY_BLOCKED)
  251. + strlcat(possibly_blocked, port, sizeof(possibly_blocked));
  252. + else if (failures >= FAILURES_OBSERVED)
  253. + strlcat(failures_observed, port, sizeof(failures_observed));
  254. + }
  255. + });
  256. +
  257. + log_warn(LD_HIST,"%s%s%s%s%s%s%s%s",
  258. + server_mode(options) &&
  259. + ((min_severity==FAILURES_OBSERVED) || strlen(probably_blocked))?
  260. + "You should consider disabling your Tor server.":"",
  261. + (min_severity==FAILURES_OBSERVED)?
  262. + "Tor appears to be blocked from connecting to a range of ports "
  263. + "with the result that it cannot connect to one tenth of the Tor "
  264. + "network. ":"",
  265. + strlen(failures_observed)?
  266. + "Tor has observed failures on the following ports: ":"",
  267. + failures_observed,
  268. + strlen(possibly_blocked)?
  269. + "Tor is possibly blocked on the following ports: ":"",
  270. + possibly_blocked,
  271. + strlen(probably_blocked)?
  272. + "Tor is almost certainly blocked on the following ports: ":"",
  273. + probably_blocked);
  274. +
  275. +}
  276. +
  277. +/** Record the success of our connection to <b>digest</b>'s
  278. + * OR port. */
  279. +void
  280. +or_port_hist_success(uint16_t or_port)
  281. +{
  282. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp,
  283. + {
  284. + if (tp->or_port != or_port)
  285. + continue;
  286. + /*Reset our failure stats so we can notice if this port ever gets
  287. + blocked again.*/
  288. + tp->success=1;
  289. + if (digestmap_size(tp->failure_ids)) {
  290. + digestmap_free(tp->failure_ids,NULL);
  291. + tp->failure_ids=digestmap_new();
  292. + }
  293. + if (still_searching) {
  294. + still_searching=0;
  295. + SMARTLIST_FOREACH(or_port_hists,or_port_hist_t *,t,t->excluded=0;);
  296. + }
  297. + return;
  298. + });
  299. +}
  300. +/** Record the failure of our connection to <b>digest</b>'s
  301. + * OR port. Warn, exclude the port from future entry guard selection, or
  302. + * add port to blocked-ports in our server's extra-info as appropriate. */
  303. +void
  304. +or_port_hist_failure(const char *digest, uint16_t or_port)
  305. +{
  306. + int total_failures=0, ports_excluded=0, report_block=0;
  307. + int total_routers=smartlist_len(router_get_routerlist()->routers);
  308. +
  309. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp,
  310. + {
  311. + ports_excluded += tp->excluded;
  312. + total_failures+=digestmap_size(tp->failure_ids);
  313. + if (tp->or_port != or_port)
  314. + continue;
  315. + /* We're only interested in unique failures */
  316. + if (digestmap_get(tp->failure_ids, digest))
  317. + return;
  318. +
  319. + total_failures++;
  320. + digestmap_set(tp->failure_ids, digest, (void*)1);
  321. + if (still_searching && !tp->success) {
  322. + tp->excluded=1;
  323. + ports_excluded++;
  324. + }
  325. + if ((digestmap_size(tp->ids) >= POSSIBLY_BLOCKED) &&
  326. + !(digestmap_size(tp->failure_ids) % POSSIBLY_BLOCKED))
  327. + report_block=POSSIBLY_BLOCKED;
  328. + });
  329. +
  330. + if (total_failures >= (int)(total_routers/10))
  331. + or_port_hist_report_block(FAILURES_OBSERVED);
  332. + else if (report_block)
  333. + or_port_hist_report_block(report_block);
  334. +
  335. + if (ports_excluded >= smartlist_len(or_port_hists)) {
  336. + log_warn(LD_HIST,"During entry node selection Tor tried every port "
  337. + "offered on the network on at least one server "
  338. + "and didn't manage a single "
  339. + "successful connection. This suggests you are behind an "
  340. + "extremely restrictive firewall. Tor will keep trying to find "
  341. + "a reachable entry node.");
  342. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp, tp->excluded=0;);
  343. + }
  344. +}
  345. +
  346. +/** Add any ports marked as excluded in or_port_hist_t to <b>rt</b> */
  347. +void
  348. +or_port_hist_exclude(routerset_t *rt)
  349. +{
  350. + SMARTLIST_FOREACH(or_port_hists, or_port_hist_t *, tp,
  351. + {
  352. + char portpolicy[9];
  353. + if (tp->excluded) {
  354. + tor_snprintf(portpolicy,sizeof(portpolicy),"*:%u", tp->or_port);
  355. + log_warn(LD_HIST,"Port %u may be blocked, excluding it temporarily "
  356. + "from entry guard selection.", tp->or_port);
  357. + routerset_parse(rt, portpolicy, "Ports");
  358. + }
  359. + });
  360. +}
  361. +
  362. +/** Allow the exclusion of ports during our search for an entry node. */
  363. +void
  364. +or_port_hist_search_again(void)
  365. +{
  366. + still_searching=1;
  367. +}
  368. Index: src/or/or.h
  369. ===================================================================
  370. --- src/or/or.h (revision 17104)
  371. +++ src/or/or.h (working copy)
  372. @@ -3864,6 +3864,13 @@
  373. int any_predicted_circuits(time_t now);
  374. int rep_hist_circbuilding_dormant(time_t now);
  375. +void or_port_hist_failure(const char *digest, uint16_t or_port);
  376. +void or_port_hist_success(uint16_t or_port);
  377. +void or_port_hist_new(const routerinfo_t *ri);
  378. +void or_port_hist_exclude(routerset_t *rt);
  379. +void or_port_hist_search_again(void);
  380. +char *or_port_hist_get_blocked_ports(void);
  381. +
  382. /** Possible public/private key operations in Tor: used to keep track of where
  383. * we're spending our time. */
  384. typedef enum {
  385. Index: src/or/routerparse.c
  386. ===================================================================
  387. --- src/or/routerparse.c (revision 17104)
  388. +++ src/or/routerparse.c (working copy)
  389. @@ -1401,6 +1401,8 @@
  390. goto err;
  391. }
  392. + or_port_hist_new(router);
  393. +
  394. if (!router->platform) {
  395. router->platform = tor_strdup("<unknown>");
  396. }
  397. Index: src/or/router.c
  398. ===================================================================
  399. --- src/or/router.c (revision 17104)
  400. +++ src/or/router.c (working copy)
  401. @@ -1818,6 +1818,7 @@
  402. char published[ISO_TIME_LEN+1];
  403. char digest[DIGEST_LEN];
  404. char *bandwidth_usage;
  405. + char *blocked_ports;
  406. int result;
  407. size_t len;
  408. @@ -1825,7 +1826,6 @@
  409. extrainfo->cache_info.identity_digest, DIGEST_LEN);
  410. format_iso_time(published, extrainfo->cache_info.published_on);
  411. bandwidth_usage = rep_hist_get_bandwidth_lines(1);
  412. -
  413. result = tor_snprintf(s, maxlen,
  414. "extra-info %s %s\n"
  415. "published %s\n%s",
  416. @@ -1835,6 +1835,16 @@
  417. if (result<0)
  418. return -1;
  419. + blocked_ports = or_port_hist_get_blocked_ports();
  420. + if (blocked_ports) {
  421. + result = tor_snprintf(s+strlen(s), maxlen-strlen(s),
  422. + "%s",
  423. + blocked_ports);
  424. + tor_free(blocked_ports);
  425. + if (result<0)
  426. + return -1;
  427. + }
  428. +
  429. if (should_record_bridge_info(options)) {
  430. static time_t last_purged_at = 0;
  431. char *geoip_summary;
  432. Index: src/or/circuitbuild.c
  433. ===================================================================
  434. --- src/or/circuitbuild.c (revision 17104)
  435. +++ src/or/circuitbuild.c (working copy)
  436. @@ -62,6 +62,7 @@
  437. static void entry_guards_changed(void);
  438. static time_t start_of_month(time_t when);
  439. +static int num_live_entry_guards(void);
  440. /** Iterate over values of circ_id, starting from conn-\>next_circ_id,
  441. * and with the high bit specified by conn-\>circ_id_type, until we get
  442. @@ -1627,12 +1628,14 @@
  443. smartlist_t *excluded;
  444. or_options_t *options = get_options();
  445. router_crn_flags_t flags = 0;
  446. + routerset_t *_ExcludeNodes;
  447. if (state && options->UseEntryGuards &&
  448. (purpose != CIRCUIT_PURPOSE_TESTING || options->BridgeRelay)) {
  449. return choose_random_entry(state);
  450. }
  451. + _ExcludeNodes = routerset_new();
  452. excluded = smartlist_create();
  453. if (state && (r = build_state_get_exit_router(state))) {
  454. @@ -1670,12 +1673,18 @@
  455. if (options->_AllowInvalid & ALLOW_INVALID_ENTRY)
  456. flags |= CRN_ALLOW_INVALID;
  457. + if (options->ExcludeNodes)
  458. + routerset_union(_ExcludeNodes,options->ExcludeNodes);
  459. +
  460. + or_port_hist_exclude(_ExcludeNodes);
  461. +
  462. choice = router_choose_random_node(
  463. NULL,
  464. excluded,
  465. - options->ExcludeNodes,
  466. + _ExcludeNodes,
  467. flags);
  468. smartlist_free(excluded);
  469. + routerset_free(_ExcludeNodes);
  470. return choice;
  471. }
  472. @@ -2727,6 +2736,7 @@
  473. entry_guards_update_state(or_state_t *state)
  474. {
  475. config_line_t **next, *line;
  476. + unsigned int have_reachable_entry=0;
  477. if (! entry_guards_dirty)
  478. return;
  479. @@ -2740,6 +2750,7 @@
  480. char dbuf[HEX_DIGEST_LEN+1];
  481. if (!e->made_contact)
  482. continue; /* don't write this one to disk */
  483. + have_reachable_entry=1;
  484. *next = line = tor_malloc_zero(sizeof(config_line_t));
  485. line->key = tor_strdup("EntryGuard");
  486. line->value = tor_malloc(HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2);
  487. @@ -2785,6 +2796,11 @@
  488. if (!get_options()->AvoidDiskWrites)
  489. or_state_mark_dirty(get_or_state(), 0);
  490. entry_guards_dirty = 0;
  491. +
  492. + /* XXX: Is this the place to decide that we no longer have any reachable
  493. + guards? */
  494. + if (!have_reachable_entry)
  495. + or_port_hist_search_again();
  496. }
  497. /** If <b>question</b> is the string "entry-guards", then dump