bridges.c 36 KB

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