bridges.c 37 KB

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