bridges.c 33 KB

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