bridges.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 bridges.c
  8. * \brief Code to manage bridges and bridge selection.
  9. *
  10. * Bridges are fixed entry nodes, used for censorship circumvention.
  11. **/
  12. #define TOR_BRIDGES_PRIVATE
  13. #include "core/or/or.h"
  14. #include "feature/client/bridges.h"
  15. #include "core/or/circuitbuild.h"
  16. #include "app/config/config.h"
  17. #include "core/mainloop/connection.h"
  18. #include "feature/dircache/directory.h"
  19. #include "feature/client/entrynodes.h"
  20. #include "feature/nodelist/nodelist.h"
  21. #include "core/or/policies.h"
  22. #include "feature/relay/router.h"
  23. #include "feature/nodelist/routerlist.h"
  24. #include "feature/nodelist/routerset.h"
  25. #include "feature/client/transports.h"
  26. #include "core/or/extend_info_st.h"
  27. #include "feature/nodelist/node_st.h"
  28. #include "feature/nodelist/routerinfo_st.h"
  29. #include "feature/nodelist/routerstatus_st.h"
  30. #include "feature/nodelist/microdesc_st.h"
  31. /** Information about a configured bridge. Currently this just matches the
  32. * ones in the torrc file, but one day we may be able to learn about new
  33. * bridges on our own, and remember them in the state file. */
  34. struct bridge_info_t {
  35. /** Address and port of the bridge, as configured by the user.*/
  36. tor_addr_port_t addrport_configured;
  37. /** Address of the bridge. */
  38. tor_addr_t addr;
  39. /** TLS port for the bridge. */
  40. uint16_t port;
  41. /** Boolean: We are re-parsing our bridge list, and we are going to remove
  42. * this one if we don't find it in the list of configured bridges. */
  43. unsigned marked_for_removal : 1;
  44. /** Expected identity digest, or all zero bytes if we don't know what the
  45. * digest should be. */
  46. char identity[DIGEST_LEN];
  47. /** Name of pluggable transport protocol taken from its config line. */
  48. char *transport_name;
  49. /** When should we next try to fetch a descriptor for this bridge? */
  50. download_status_t fetch_status;
  51. /** A smartlist of k=v values to be passed to the SOCKS proxy, if
  52. transports are used for this bridge. */
  53. smartlist_t *socks_args;
  54. };
  55. #define bridge_free(bridge) \
  56. FREE_AND_NULL(bridge_info_t, bridge_free_, (bridge))
  57. static void bridge_free_(bridge_info_t *bridge);
  58. static void rewrite_node_address_for_bridge(const bridge_info_t *bridge,
  59. node_t *node);
  60. /** A list of configured bridges. Whenever we actually get a descriptor
  61. * for one, we add it as an entry guard. Note that the order of bridges
  62. * in this list does not necessarily correspond to the order of bridges
  63. * in the torrc. */
  64. static smartlist_t *bridge_list = NULL;
  65. /** Mark every entry of the bridge list to be removed on our next call to
  66. * sweep_bridge_list unless it has first been un-marked. */
  67. void
  68. mark_bridge_list(void)
  69. {
  70. if (!bridge_list)
  71. bridge_list = smartlist_new();
  72. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b,
  73. b->marked_for_removal = 1);
  74. }
  75. /** Remove every entry of the bridge list that was marked with
  76. * mark_bridge_list if it has not subsequently been un-marked. */
  77. void
  78. sweep_bridge_list(void)
  79. {
  80. if (!bridge_list)
  81. bridge_list = smartlist_new();
  82. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  83. if (b->marked_for_removal) {
  84. SMARTLIST_DEL_CURRENT(bridge_list, b);
  85. bridge_free(b);
  86. }
  87. } SMARTLIST_FOREACH_END(b);
  88. }
  89. /** Initialize the bridge list to empty, creating it if needed. */
  90. STATIC void
  91. clear_bridge_list(void)
  92. {
  93. if (!bridge_list)
  94. bridge_list = smartlist_new();
  95. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, bridge_free(b));
  96. smartlist_clear(bridge_list);
  97. }
  98. /** Free the bridge <b>bridge</b>. */
  99. static void
  100. bridge_free_(bridge_info_t *bridge)
  101. {
  102. if (!bridge)
  103. return;
  104. tor_free(bridge->transport_name);
  105. if (bridge->socks_args) {
  106. SMARTLIST_FOREACH(bridge->socks_args, char*, s, tor_free(s));
  107. smartlist_free(bridge->socks_args);
  108. }
  109. tor_free(bridge);
  110. }
  111. /** Return a list of all the configured bridges, as bridge_info_t pointers. */
  112. const smartlist_t *
  113. bridge_list_get(void)
  114. {
  115. if (!bridge_list)
  116. bridge_list = smartlist_new();
  117. return bridge_list;
  118. }
  119. /**
  120. * Given a <b>bridge</b>, return a pointer to its RSA identity digest, or
  121. * NULL if we don't know one for it.
  122. */
  123. const uint8_t *
  124. bridge_get_rsa_id_digest(const bridge_info_t *bridge)
  125. {
  126. tor_assert(bridge);
  127. if (tor_digest_is_zero(bridge->identity))
  128. return NULL;
  129. else
  130. return (const uint8_t *) bridge->identity;
  131. }
  132. /**
  133. * Given a <b>bridge</b>, return a pointer to its configured addr:port
  134. * combination.
  135. */
  136. const tor_addr_port_t *
  137. bridge_get_addr_port(const bridge_info_t *bridge)
  138. {
  139. tor_assert(bridge);
  140. return &bridge->addrport_configured;
  141. }
  142. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  143. * bridge with no known digest whose address matches any of the
  144. * tor_addr_port_t's in <b>orports</b>, return that bridge. Else return
  145. * NULL. */
  146. STATIC bridge_info_t *
  147. get_configured_bridge_by_orports_digest(const char *digest,
  148. const smartlist_t *orports)
  149. {
  150. if (!bridge_list)
  151. return NULL;
  152. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  153. {
  154. if (tor_digest_is_zero(bridge->identity)) {
  155. SMARTLIST_FOREACH_BEGIN(orports, tor_addr_port_t *, ap)
  156. {
  157. if (tor_addr_compare(&bridge->addr, &ap->addr, CMP_EXACT) == 0 &&
  158. bridge->port == ap->port)
  159. return bridge;
  160. }
  161. SMARTLIST_FOREACH_END(ap);
  162. }
  163. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  164. return bridge;
  165. }
  166. SMARTLIST_FOREACH_END(bridge);
  167. return NULL;
  168. }
  169. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  170. * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
  171. * return that bridge. Else return NULL. If <b>digest</b> is NULL, check for
  172. * address/port matches only. */
  173. bridge_info_t *
  174. get_configured_bridge_by_addr_port_digest(const tor_addr_t *addr,
  175. uint16_t port,
  176. const char *digest)
  177. {
  178. if (!bridge_list)
  179. return NULL;
  180. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  181. {
  182. if ((tor_digest_is_zero(bridge->identity) || digest == NULL) &&
  183. !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
  184. bridge->port == port)
  185. return bridge;
  186. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  187. return bridge;
  188. }
  189. SMARTLIST_FOREACH_END(bridge);
  190. return NULL;
  191. }
  192. /**
  193. * As get_configured_bridge_by_addr_port, but require that the
  194. * address match <b>addr</b>:<b>port</b>, and that the ID digest match
  195. * <b>digest</b>. (The other function will ignore the address if the
  196. * digest matches.)
  197. */
  198. bridge_info_t *
  199. get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t *addr,
  200. uint16_t port,
  201. const char *digest)
  202. {
  203. if (!bridge_list)
  204. return NULL;
  205. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
  206. if (!tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
  207. bridge->port == port) {
  208. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  209. return bridge;
  210. else if (!digest || tor_digest_is_zero(bridge->identity))
  211. return bridge;
  212. }
  213. } SMARTLIST_FOREACH_END(bridge);
  214. return NULL;
  215. }
  216. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  217. * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
  218. * return 1. Else return 0. If <b>digest</b> is NULL, check for
  219. * address/port matches only. */
  220. int
  221. addr_is_a_configured_bridge(const tor_addr_t *addr,
  222. uint16_t port,
  223. const char *digest)
  224. {
  225. tor_assert(addr);
  226. return get_configured_bridge_by_addr_port_digest(addr, port, digest) ? 1 : 0;
  227. }
  228. /** If we have a bridge configured whose digest matches
  229. * <b>ei->identity_digest</b>, or a bridge with no known digest whose address
  230. * matches <b>ei->addr</b>:<b>ei->port</b>, return 1. Else return 0.
  231. * If <b>ei->onion_key</b> is NULL, check for address/port matches only. */
  232. int
  233. extend_info_is_a_configured_bridge(const extend_info_t *ei)
  234. {
  235. const char *digest = ei->onion_key ? ei->identity_digest : NULL;
  236. return addr_is_a_configured_bridge(&ei->addr, ei->port, digest);
  237. }
  238. /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
  239. * it up via router descriptor <b>ri</b>. */
  240. static bridge_info_t *
  241. get_configured_bridge_by_routerinfo(const routerinfo_t *ri)
  242. {
  243. bridge_info_t *bi = NULL;
  244. smartlist_t *orports = router_get_all_orports(ri);
  245. bi = get_configured_bridge_by_orports_digest(ri->cache_info.identity_digest,
  246. orports);
  247. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  248. smartlist_free(orports);
  249. return bi;
  250. }
  251. /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
  252. int
  253. routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
  254. {
  255. return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
  256. }
  257. /**
  258. * Return 1 iff <b>bridge_list</b> contains entry matching
  259. * given; IPv4 address in host byte order (<b>ipv4_addr</b>
  260. * and <b>port</b> (and no identity digest) OR it contains an
  261. * entry whose identity matches <b>digest</b>. Otherwise,
  262. * return 0.
  263. */
  264. static int
  265. bridge_exists_with_ipv4h_addr_and_port(const uint32_t ipv4_addr,
  266. const uint16_t port,
  267. const char *digest)
  268. {
  269. tor_addr_t node_ipv4;
  270. if (tor_addr_port_is_valid_ipv4h(ipv4_addr, port, 0)) {
  271. tor_addr_from_ipv4h(&node_ipv4, ipv4_addr);
  272. bridge_info_t *bridge =
  273. get_configured_bridge_by_addr_port_digest(&node_ipv4,
  274. port,
  275. digest);
  276. return (bridge != NULL);
  277. }
  278. return 0;
  279. }
  280. /**
  281. * Return 1 iff <b>bridge_list</b> contains entry matching given
  282. * <b>ipv6_addr</b> and <b>port</b> (and no identity digest) OR
  283. * it contains an entry whose identity matches <b>digest</b>.
  284. * Otherwise, return 0.
  285. */
  286. static int
  287. bridge_exists_with_ipv6_addr_and_port(const tor_addr_t *ipv6_addr,
  288. const uint16_t port,
  289. const char *digest)
  290. {
  291. if (!tor_addr_port_is_valid(ipv6_addr, port, 0))
  292. return 0;
  293. bridge_info_t *bridge =
  294. get_configured_bridge_by_addr_port_digest(ipv6_addr,
  295. port,
  296. digest);
  297. return (bridge != NULL);
  298. }
  299. /** Return 1 if <b>node</b> is one of our configured bridges, else 0.
  300. * More specifically, return 1 iff: a bridge_info_t object exists in
  301. * <b>bridge_list</b> such that: 1) It's identity is equal to node
  302. * identity OR 2) It's identity digest is zero, but it matches
  303. * address and port of any ORPort in the node.
  304. */
  305. int
  306. node_is_a_configured_bridge(const node_t *node)
  307. {
  308. /* First, let's try searching for a bridge with matching identity. */
  309. if (BUG(tor_digest_is_zero(node->identity)))
  310. return 0;
  311. if (find_bridge_by_digest(node->identity) != NULL)
  312. return 1;
  313. /* At this point, we have established that no bridge exists with
  314. * matching identity digest. However, we still pass it into
  315. * bridge_exists_* functions because we want further code to
  316. * check for absence of identity digest in a bridge.
  317. */
  318. if (node->ri) {
  319. if (bridge_exists_with_ipv4h_addr_and_port(node->ri->addr,
  320. node->ri->or_port,
  321. node->identity))
  322. return 1;
  323. if (bridge_exists_with_ipv6_addr_and_port(&node->ri->ipv6_addr,
  324. node->ri->ipv6_orport,
  325. node->identity))
  326. return 1;
  327. } else if (node->rs) {
  328. if (bridge_exists_with_ipv4h_addr_and_port(node->rs->addr,
  329. node->rs->or_port,
  330. node->identity))
  331. return 1;
  332. if (bridge_exists_with_ipv6_addr_and_port(&node->rs->ipv6_addr,
  333. node->rs->ipv6_orport,
  334. node->identity))
  335. return 1;
  336. } else if (node->md) {
  337. if (bridge_exists_with_ipv6_addr_and_port(&node->md->ipv6_addr,
  338. node->md->ipv6_orport,
  339. node->identity))
  340. return 1;
  341. }
  342. return 0;
  343. }
  344. /** We made a connection to a router at <b>addr</b>:<b>port</b>
  345. * without knowing its digest. Its digest turned out to be <b>digest</b>.
  346. * If it was a bridge, and we still don't know its digest, record it.
  347. */
  348. void
  349. learned_router_identity(const tor_addr_t *addr, uint16_t port,
  350. const char *digest,
  351. const ed25519_public_key_t *ed_id)
  352. {
  353. // XXXX prop220 use ed_id here, once there is some way to specify
  354. (void)ed_id;
  355. int learned = 0;
  356. bridge_info_t *bridge =
  357. get_configured_bridge_by_exact_addr_port_digest(addr, port, digest);
  358. if (bridge && tor_digest_is_zero(bridge->identity)) {
  359. memcpy(bridge->identity, digest, DIGEST_LEN);
  360. learned = 1;
  361. }
  362. /* XXXX prop220 remember bridge ed25519 identities -- add a field */
  363. #if 0
  364. if (bridge && ed_id &&
  365. ed25519_public_key_is_zero(&bridge->ed25519_identity) &&
  366. !ed25519_public_key_is_zero(ed_id)) {
  367. memcpy(&bridge->ed25519_identity, ed_id, sizeof(*ed_id));
  368. learned = 1;
  369. }
  370. #endif /* 0 */
  371. if (learned) {
  372. char *transport_info = NULL;
  373. const char *transport_name =
  374. find_transport_name_by_bridge_addrport(addr, port);
  375. if (transport_name)
  376. tor_asprintf(&transport_info, " (with transport '%s')", transport_name);
  377. // XXXX prop220 log both fingerprints.
  378. log_notice(LD_DIR, "Learned fingerprint %s for bridge %s%s.",
  379. hex_str(digest, DIGEST_LEN), fmt_addrport(addr, port),
  380. transport_info ? transport_info : "");
  381. tor_free(transport_info);
  382. entry_guard_learned_bridge_identity(&bridge->addrport_configured,
  383. (const uint8_t *)digest);
  384. }
  385. }
  386. /** Return true if <b>bridge</b> has the same identity digest as
  387. * <b>digest</b>. If <b>digest</b> is NULL, it matches
  388. * bridges with unspecified identity digests. */
  389. static int
  390. bridge_has_digest(const bridge_info_t *bridge, const char *digest)
  391. {
  392. if (digest)
  393. return tor_memeq(digest, bridge->identity, DIGEST_LEN);
  394. else
  395. return tor_digest_is_zero(bridge->identity);
  396. }
  397. /** We are about to add a new bridge at <b>addr</b>:<b>port</b>, with optional
  398. * <b>digest</b> and <b>transport_name</b>. Mark for removal any previously
  399. * existing bridge with the same address and port, and warn the user as
  400. * appropriate.
  401. */
  402. STATIC void
  403. bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port,
  404. const char *digest, const char *transport_name)
  405. {
  406. /* Iterate the already-registered bridge list:
  407. If you find a bridge with the same address and port, mark it for
  408. removal. It doesn't make sense to have two active bridges with
  409. the same IP:PORT. If the bridge in question has a different
  410. digest or transport than <b>digest</b>/<b>transport_name</b>,
  411. it's probably a misconfiguration and we should warn the user.
  412. */
  413. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
  414. if (bridge->marked_for_removal)
  415. continue;
  416. if (tor_addr_eq(&bridge->addr, addr) && (bridge->port == port)) {
  417. bridge->marked_for_removal = 1;
  418. if (!bridge_has_digest(bridge, digest) ||
  419. strcmp_opt(bridge->transport_name, transport_name)) {
  420. /* warn the user */
  421. char *bridge_description_new, *bridge_description_old;
  422. tor_asprintf(&bridge_description_new, "%s:%s:%s",
  423. fmt_addrport(addr, port),
  424. digest ? hex_str(digest, DIGEST_LEN) : "",
  425. transport_name ? transport_name : "");
  426. tor_asprintf(&bridge_description_old, "%s:%s:%s",
  427. fmt_addrport(&bridge->addr, bridge->port),
  428. tor_digest_is_zero(bridge->identity) ?
  429. "" : hex_str(bridge->identity,DIGEST_LEN),
  430. bridge->transport_name ? bridge->transport_name : "");
  431. log_warn(LD_GENERAL,"Tried to add bridge '%s', but we found a conflict"
  432. " with the already registered bridge '%s'. We will discard"
  433. " the old bridge and keep '%s'. If this is not what you"
  434. " wanted, please change your configuration file accordingly.",
  435. bridge_description_new, bridge_description_old,
  436. bridge_description_new);
  437. tor_free(bridge_description_new);
  438. tor_free(bridge_description_old);
  439. }
  440. }
  441. } SMARTLIST_FOREACH_END(bridge);
  442. }
  443. /** Return True if we have a bridge that uses a transport with name
  444. * <b>transport_name</b>. */
  445. MOCK_IMPL(int,
  446. transport_is_needed, (const char *transport_name))
  447. {
  448. if (!bridge_list)
  449. return 0;
  450. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  451. if (bridge->transport_name &&
  452. !strcmp(bridge->transport_name, transport_name))
  453. return 1;
  454. } SMARTLIST_FOREACH_END(bridge);
  455. return 0;
  456. }
  457. /** Register the bridge information in <b>bridge_line</b> to the
  458. * bridge subsystem. Steals reference of <b>bridge_line</b>. */
  459. void
  460. bridge_add_from_config(bridge_line_t *bridge_line)
  461. {
  462. bridge_info_t *b;
  463. // XXXX prop220 add a way to specify ed25519 ID to bridge_line_t.
  464. { /* Log the bridge we are about to register: */
  465. log_debug(LD_GENERAL, "Registering bridge at %s (transport: %s) (%s)",
  466. fmt_addrport(&bridge_line->addr, bridge_line->port),
  467. bridge_line->transport_name ?
  468. bridge_line->transport_name : "no transport",
  469. tor_digest_is_zero(bridge_line->digest) ?
  470. "no key listed" : hex_str(bridge_line->digest, DIGEST_LEN));
  471. if (bridge_line->socks_args) { /* print socks arguments */
  472. int i = 0;
  473. tor_assert(smartlist_len(bridge_line->socks_args) > 0);
  474. log_debug(LD_GENERAL, "Bridge uses %d SOCKS arguments:",
  475. smartlist_len(bridge_line->socks_args));
  476. SMARTLIST_FOREACH(bridge_line->socks_args, const char *, arg,
  477. log_debug(LD_CONFIG, "%d: %s", ++i, arg));
  478. }
  479. }
  480. bridge_resolve_conflicts(&bridge_line->addr,
  481. bridge_line->port,
  482. bridge_line->digest,
  483. bridge_line->transport_name);
  484. b = tor_malloc_zero(sizeof(bridge_info_t));
  485. tor_addr_copy(&b->addrport_configured.addr, &bridge_line->addr);
  486. b->addrport_configured.port = bridge_line->port;
  487. tor_addr_copy(&b->addr, &bridge_line->addr);
  488. b->port = bridge_line->port;
  489. memcpy(b->identity, bridge_line->digest, DIGEST_LEN);
  490. if (bridge_line->transport_name)
  491. b->transport_name = bridge_line->transport_name;
  492. b->fetch_status.schedule = DL_SCHED_BRIDGE;
  493. b->fetch_status.increment_on = DL_SCHED_INCREMENT_ATTEMPT;
  494. /* We can't reset the bridge's download status here, because UseBridges
  495. * might be 0 now, and it might be changed to 1 much later. */
  496. b->socks_args = bridge_line->socks_args;
  497. if (!bridge_list)
  498. bridge_list = smartlist_new();
  499. tor_free(bridge_line); /* Deallocate bridge_line now. */
  500. smartlist_add(bridge_list, b);
  501. }
  502. /** If <b>digest</b> is one of our known bridges, return it. */
  503. STATIC bridge_info_t *
  504. find_bridge_by_digest(const char *digest)
  505. {
  506. if (! bridge_list)
  507. return NULL;
  508. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
  509. {
  510. if (tor_memeq(bridge->identity, digest, DIGEST_LEN))
  511. return bridge;
  512. });
  513. return NULL;
  514. }
  515. /** Given the <b>addr</b> and <b>port</b> of a bridge, if that bridge
  516. * supports a pluggable transport, return its name. Otherwise, return
  517. * NULL. */
  518. const char *
  519. find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
  520. {
  521. if (!bridge_list)
  522. return NULL;
  523. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  524. if (tor_addr_eq(&bridge->addr, addr) &&
  525. (bridge->port == port))
  526. return bridge->transport_name;
  527. } SMARTLIST_FOREACH_END(bridge);
  528. return NULL;
  529. }
  530. /** If <b>addr</b> and <b>port</b> match the address and port of a
  531. * bridge of ours that uses pluggable transports, place its transport
  532. * in <b>transport</b>.
  533. *
  534. * Return 0 on success (found a transport, or found a bridge with no
  535. * transport, or found no bridge); return -1 if we should be using a
  536. * transport, but the transport could not be found.
  537. */
  538. int
  539. get_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
  540. const transport_t **transport)
  541. {
  542. *transport = NULL;
  543. if (!bridge_list)
  544. return 0;
  545. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  546. if (tor_addr_eq(&bridge->addr, addr) &&
  547. (bridge->port == port)) { /* bridge matched */
  548. if (bridge->transport_name) { /* it also uses pluggable transports */
  549. *transport = transport_get_by_name(bridge->transport_name);
  550. if (*transport == NULL) { /* it uses pluggable transports, but
  551. the transport could not be found! */
  552. return -1;
  553. }
  554. return 0;
  555. } else { /* bridge matched, but it doesn't use transports. */
  556. break;
  557. }
  558. }
  559. } SMARTLIST_FOREACH_END(bridge);
  560. *transport = NULL;
  561. return 0;
  562. }
  563. /** Return a smartlist containing all the SOCKS arguments that we
  564. * should pass to the SOCKS proxy. */
  565. const smartlist_t *
  566. get_socks_args_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
  567. {
  568. bridge_info_t *bridge = get_configured_bridge_by_addr_port_digest(addr,
  569. port,
  570. NULL);
  571. return bridge ? bridge->socks_args : NULL;
  572. }
  573. /** We need to ask <b>bridge</b> for its server descriptor. */
  574. static void
  575. launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
  576. {
  577. const or_options_t *options = get_options();
  578. circuit_guard_state_t *guard_state = NULL;
  579. if (connection_get_by_type_addr_port_purpose(
  580. CONN_TYPE_DIR, &bridge->addr, bridge->port,
  581. DIR_PURPOSE_FETCH_SERVERDESC))
  582. return; /* it's already on the way */
  583. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  584. download_status_mark_impossible(&bridge->fetch_status);
  585. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  586. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  587. return;
  588. }
  589. /* Until we get a descriptor for the bridge, we only know one address for
  590. * it. */
  591. if (!fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
  592. FIREWALL_OR_CONNECTION, 0, 0)) {
  593. log_notice(LD_CONFIG, "Tried to fetch a descriptor directly from a "
  594. "bridge, but that bridge is not reachable through our "
  595. "firewall.");
  596. return;
  597. }
  598. /* If we already have a node_t for this bridge, rewrite its address now. */
  599. node_t *node = node_get_mutable_by_id(bridge->identity);
  600. if (node) {
  601. rewrite_node_address_for_bridge(bridge, node);
  602. }
  603. tor_addr_port_t bridge_addrport;
  604. memcpy(&bridge_addrport.addr, &bridge->addr, sizeof(tor_addr_t));
  605. bridge_addrport.port = bridge->port;
  606. guard_state = get_guard_state_for_bridge_desc_fetch(bridge->identity);
  607. directory_request_t *req =
  608. directory_request_new(DIR_PURPOSE_FETCH_SERVERDESC);
  609. directory_request_set_or_addr_port(req, &bridge_addrport);
  610. directory_request_set_directory_id_digest(req, bridge->identity);
  611. directory_request_set_router_purpose(req, ROUTER_PURPOSE_BRIDGE);
  612. directory_request_set_resource(req, "authority.z");
  613. if (guard_state) {
  614. directory_request_set_guard_state(req, guard_state);
  615. }
  616. directory_initiate_request(req);
  617. directory_request_free(req);
  618. }
  619. /** Fetching the bridge descriptor from the bridge authority returned a
  620. * "not found". Fall back to trying a direct fetch. */
  621. void
  622. retry_bridge_descriptor_fetch_directly(const char *digest)
  623. {
  624. bridge_info_t *bridge = find_bridge_by_digest(digest);
  625. if (!bridge)
  626. return; /* not found? oh well. */
  627. launch_direct_bridge_descriptor_fetch(bridge);
  628. }
  629. /** For each bridge in our list for which we don't currently have a
  630. * descriptor, fetch a new copy of its descriptor -- either directly
  631. * from the bridge or via a bridge authority. */
  632. void
  633. fetch_bridge_descriptors(const or_options_t *options, time_t now)
  634. {
  635. int num_bridge_auths = get_n_authorities(BRIDGE_DIRINFO);
  636. int ask_bridge_directly;
  637. int can_use_bridge_authority;
  638. if (!bridge_list)
  639. return;
  640. /* If we still have unconfigured managed proxies, don't go and
  641. connect to a bridge. */
  642. if (pt_proxies_configuration_pending())
  643. return;
  644. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  645. {
  646. /* This resets the download status on first use */
  647. if (!download_status_is_ready(&bridge->fetch_status, now))
  648. continue; /* don't bother, no need to retry yet */
  649. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  650. download_status_mark_impossible(&bridge->fetch_status);
  651. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  652. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  653. continue;
  654. }
  655. /* schedule the next attempt
  656. * we can't increment after a failure, because sometimes we use the
  657. * bridge authority, and sometimes we use the bridge direct */
  658. download_status_increment_attempt(
  659. &bridge->fetch_status,
  660. safe_str_client(fmt_and_decorate_addr(&bridge->addr)),
  661. now);
  662. can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
  663. num_bridge_auths;
  664. ask_bridge_directly = !can_use_bridge_authority ||
  665. !options->UpdateBridgesFromAuthority;
  666. log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
  667. ask_bridge_directly, tor_digest_is_zero(bridge->identity),
  668. !options->UpdateBridgesFromAuthority, !num_bridge_auths);
  669. if (ask_bridge_directly &&
  670. !fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
  671. FIREWALL_OR_CONNECTION, 0,
  672. 0)) {
  673. log_notice(LD_DIR, "Bridge at '%s' isn't reachable by our "
  674. "firewall policy. %s.",
  675. fmt_addrport(&bridge->addr, bridge->port),
  676. can_use_bridge_authority ?
  677. "Asking bridge authority instead" : "Skipping");
  678. if (can_use_bridge_authority)
  679. ask_bridge_directly = 0;
  680. else
  681. continue;
  682. }
  683. if (ask_bridge_directly) {
  684. /* we need to ask the bridge itself for its descriptor. */
  685. launch_direct_bridge_descriptor_fetch(bridge);
  686. } else {
  687. /* We have a digest and we want to ask an authority. We could
  688. * combine all the requests into one, but that may give more
  689. * hints to the bridge authority than we want to give. */
  690. char resource[10 + HEX_DIGEST_LEN];
  691. memcpy(resource, "fp/", 3);
  692. base16_encode(resource+3, HEX_DIGEST_LEN+1,
  693. bridge->identity, DIGEST_LEN);
  694. memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
  695. log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
  696. resource);
  697. directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
  698. ROUTER_PURPOSE_BRIDGE, resource, 0, DL_WANT_AUTHORITY);
  699. }
  700. }
  701. SMARTLIST_FOREACH_END(bridge);
  702. }
  703. /** If our <b>bridge</b> is configured to be a different address than
  704. * the bridge gives in <b>node</b>, rewrite the routerinfo
  705. * we received to use the address we meant to use. Now we handle
  706. * multihomed bridges better.
  707. */
  708. static void
  709. rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
  710. {
  711. /* XXXX move this function. */
  712. /* XXXX overridden addresses should really live in the node_t, so that the
  713. * routerinfo_t and the microdesc_t can be immutable. But we can only
  714. * do that safely if we know that no function that connects to an OR
  715. * does so through an address from any source other than node_get_addr().
  716. */
  717. tor_addr_t addr;
  718. const or_options_t *options = get_options();
  719. if (node->ri) {
  720. routerinfo_t *ri = node->ri;
  721. tor_addr_from_ipv4h(&addr, ri->addr);
  722. if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  723. bridge->port == ri->or_port) ||
  724. (!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
  725. bridge->port == ri->ipv6_orport)) {
  726. /* they match, so no need to do anything */
  727. } else {
  728. if (tor_addr_family(&bridge->addr) == AF_INET) {
  729. ri->addr = tor_addr_to_ipv4h(&bridge->addr);
  730. ri->or_port = bridge->port;
  731. log_info(LD_DIR,
  732. "Adjusted bridge routerinfo for '%s' to match configured "
  733. "address %s:%d.",
  734. ri->nickname, fmt_addr32(ri->addr), ri->or_port);
  735. } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
  736. tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
  737. ri->ipv6_orport = bridge->port;
  738. log_info(LD_DIR,
  739. "Adjusted bridge routerinfo for '%s' to match configured "
  740. "address %s.",
  741. ri->nickname, fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
  742. } else {
  743. log_err(LD_BUG, "Address family not supported: %d.",
  744. tor_addr_family(&bridge->addr));
  745. return;
  746. }
  747. }
  748. if (options->ClientPreferIPv6ORPort == -1) {
  749. /* Mark which address to use based on which bridge_t we got. */
  750. node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
  751. !tor_addr_is_null(&node->ri->ipv6_addr));
  752. } else {
  753. /* Mark which address to use based on user preference */
  754. node->ipv6_preferred = (fascist_firewall_prefer_ipv6_orport(options) &&
  755. !tor_addr_is_null(&node->ri->ipv6_addr));
  756. }
  757. /* XXXipv6 we lack support for falling back to another address for
  758. the same relay, warn the user */
  759. if (!tor_addr_is_null(&ri->ipv6_addr)) {
  760. tor_addr_port_t ap;
  761. node_get_pref_orport(node, &ap);
  762. log_notice(LD_CONFIG,
  763. "Bridge '%s' has both an IPv4 and an IPv6 address. "
  764. "Will prefer using its %s address (%s) based on %s.",
  765. ri->nickname,
  766. node->ipv6_preferred ? "IPv6" : "IPv4",
  767. fmt_addrport(&ap.addr, ap.port),
  768. options->ClientPreferIPv6ORPort == -1 ?
  769. "the configured Bridge address" :
  770. "ClientPreferIPv6ORPort");
  771. }
  772. }
  773. if (node->rs) {
  774. routerstatus_t *rs = node->rs;
  775. tor_addr_from_ipv4h(&addr, rs->addr);
  776. if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  777. bridge->port == rs->or_port) ||
  778. (!tor_addr_compare(&bridge->addr, &rs->ipv6_addr, CMP_EXACT) &&
  779. bridge->port == rs->ipv6_orport)) {
  780. /* they match, so no need to do anything */
  781. } else {
  782. if (tor_addr_family(&bridge->addr) == AF_INET) {
  783. rs->addr = tor_addr_to_ipv4h(&bridge->addr);
  784. rs->or_port = bridge->port;
  785. log_info(LD_DIR,
  786. "Adjusted bridge routerstatus for '%s' to match "
  787. "configured address %s.",
  788. rs->nickname, fmt_addrport(&bridge->addr, rs->or_port));
  789. /* set IPv6 preferences even if there is no ri */
  790. } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
  791. tor_addr_copy(&rs->ipv6_addr, &bridge->addr);
  792. rs->ipv6_orport = bridge->port;
  793. log_info(LD_DIR,
  794. "Adjusted bridge routerstatus for '%s' to match configured"
  795. " address %s.",
  796. rs->nickname, fmt_addrport(&rs->ipv6_addr, rs->ipv6_orport));
  797. } else {
  798. log_err(LD_BUG, "Address family not supported: %d.",
  799. tor_addr_family(&bridge->addr));
  800. return;
  801. }
  802. }
  803. if (options->ClientPreferIPv6ORPort == -1) {
  804. /* Mark which address to use based on which bridge_t we got. */
  805. node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
  806. !tor_addr_is_null(&node->rs->ipv6_addr));
  807. } else {
  808. /* Mark which address to use based on user preference */
  809. node->ipv6_preferred = (fascist_firewall_prefer_ipv6_orport(options) &&
  810. !tor_addr_is_null(&node->rs->ipv6_addr));
  811. }
  812. /* XXXipv6 we lack support for falling back to another address for
  813. the same relay, warn the user */
  814. if (!tor_addr_is_null(&rs->ipv6_addr)) {
  815. tor_addr_port_t ap;
  816. node_get_pref_orport(node, &ap);
  817. log_notice(LD_CONFIG,
  818. "Bridge '%s' has both an IPv4 and an IPv6 address. "
  819. "Will prefer using its %s address (%s) based on %s.",
  820. rs->nickname,
  821. node->ipv6_preferred ? "IPv6" : "IPv4",
  822. fmt_addrport(&ap.addr, ap.port),
  823. options->ClientPreferIPv6ORPort == -1 ?
  824. "the configured Bridge address" :
  825. "ClientPreferIPv6ORPort");
  826. }
  827. }
  828. }
  829. /** We just learned a descriptor for a bridge. See if that
  830. * digest is in our entry guard list, and add it if not. */
  831. void
  832. learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
  833. {
  834. tor_assert(ri);
  835. tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
  836. if (get_options()->UseBridges) {
  837. /* Retry directory downloads whenever we get a bridge descriptor:
  838. * - when bootstrapping, and
  839. * - when we aren't sure if any of our bridges are reachable.
  840. * Keep on retrying until we have at least one reachable bridge. */
  841. int first = num_bridges_usable(0) < 1;
  842. bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
  843. time_t now = time(NULL);
  844. router_set_status(ri->cache_info.identity_digest, 1);
  845. if (bridge) { /* if we actually want to use this one */
  846. node_t *node;
  847. /* it's here; schedule its re-fetch for a long time from now. */
  848. if (!from_cache) {
  849. /* This schedules the re-fetch at a constant interval, which produces
  850. * a pattern of bridge traffic. But it's better than trying all
  851. * configured briges several times in the first few minutes. */
  852. download_status_reset(&bridge->fetch_status);
  853. }
  854. node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  855. tor_assert(node);
  856. rewrite_node_address_for_bridge(bridge, node);
  857. if (tor_digest_is_zero(bridge->identity)) {
  858. memcpy(bridge->identity,ri->cache_info.identity_digest, DIGEST_LEN);
  859. log_notice(LD_DIR, "Learned identity %s for bridge at %s:%d",
  860. hex_str(bridge->identity, DIGEST_LEN),
  861. fmt_and_decorate_addr(&bridge->addr),
  862. (int) bridge->port);
  863. }
  864. entry_guard_learned_bridge_identity(&bridge->addrport_configured,
  865. (const uint8_t*)ri->cache_info.identity_digest);
  866. log_notice(LD_DIR, "new bridge descriptor '%s' (%s): %s", ri->nickname,
  867. from_cache ? "cached" : "fresh", router_describe(ri));
  868. /* If we didn't have a reachable bridge before this one, try directory
  869. * documents again. */
  870. if (first) {
  871. routerlist_retry_directory_downloads(now);
  872. }
  873. }
  874. }
  875. }
  876. /** Return a smartlist containing all bridge identity digests */
  877. MOCK_IMPL(smartlist_t *,
  878. list_bridge_identities, (void))
  879. {
  880. smartlist_t *result = NULL;
  881. char *digest_tmp;
  882. if (get_options()->UseBridges && bridge_list) {
  883. result = smartlist_new();
  884. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  885. digest_tmp = tor_malloc(DIGEST_LEN);
  886. memcpy(digest_tmp, b->identity, DIGEST_LEN);
  887. smartlist_add(result, digest_tmp);
  888. } SMARTLIST_FOREACH_END(b);
  889. }
  890. return result;
  891. }
  892. /** Get the download status for a bridge descriptor given its identity */
  893. MOCK_IMPL(download_status_t *,
  894. get_bridge_dl_status_by_id, (const char *digest))
  895. {
  896. download_status_t *dl = NULL;
  897. if (digest && get_options()->UseBridges && bridge_list) {
  898. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  899. if (tor_memeq(digest, b->identity, DIGEST_LEN)) {
  900. dl = &(b->fetch_status);
  901. break;
  902. }
  903. } SMARTLIST_FOREACH_END(b);
  904. }
  905. return dl;
  906. }
  907. /** Release all storage held in bridges.c */
  908. void
  909. bridges_free_all(void)
  910. {
  911. clear_bridge_list();
  912. smartlist_free(bridge_list);
  913. bridge_list = NULL;
  914. }