bridges.c 31 KB

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