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