bridges.c 37 KB

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