Cache control file
[tor/appveyor.git] / src / or / bridges.c
blob29d00f37ba3d018c2711a9659caab2b85cc1c47b
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 */
7 /**
8 * \file bridges.c
9 * \brief Code to manage bridges and bridge selection.
11 * Bridges are fixed entry nodes, used for censorship circumvention.
12 **/
14 #include "or.h"
15 #include "bridges.h"
16 #include "circuitbuild.h"
17 #include "config.h"
18 #include "connection.h"
19 #include "directory.h"
20 #include "entrynodes.h"
21 #include "nodelist.h"
22 #include "policies.h"
23 #include "router.h"
24 #include "routerlist.h"
25 #include "routerset.h"
26 #include "transports.h"
28 /** Information about a configured bridge. Currently this just matches the
29 * ones in the torrc file, but one day we may be able to learn about new
30 * bridges on our own, and remember them in the state file. */
31 struct bridge_info_t {
32 /** Address and port of the bridge, as configured by the user.*/
33 tor_addr_port_t addrport_configured;
34 /** Address of the bridge. */
35 tor_addr_t addr;
36 /** TLS port for the bridge. */
37 uint16_t port;
38 /** Boolean: We are re-parsing our bridge list, and we are going to remove
39 * this one if we don't find it in the list of configured bridges. */
40 unsigned marked_for_removal : 1;
41 /** Expected identity digest, or all zero bytes if we don't know what the
42 * digest should be. */
43 char identity[DIGEST_LEN];
45 /** Name of pluggable transport protocol taken from its config line. */
46 char *transport_name;
48 /** When should we next try to fetch a descriptor for this bridge? */
49 download_status_t fetch_status;
51 /** A smartlist of k=v values to be passed to the SOCKS proxy, if
52 transports are used for this bridge. */
53 smartlist_t *socks_args;
56 #define bridge_free(bridge) \
57 FREE_AND_NULL(bridge_info_t, bridge_free_, (bridge))
59 static void bridge_free_(bridge_info_t *bridge);
60 static void rewrite_node_address_for_bridge(const bridge_info_t *bridge,
61 node_t *node);
63 /** A list of configured bridges. Whenever we actually get a descriptor
64 * for one, we add it as an entry guard. Note that the order of bridges
65 * in this list does not necessarily correspond to the order of bridges
66 * in the torrc. */
67 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)
74 if (!bridge_list)
75 bridge_list = smartlist_new();
76 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b,
77 b->marked_for_removal = 1);
80 /** Remove every entry of the bridge list that was marked with
81 * mark_bridge_list if it has not subsequently been un-marked. */
82 void
83 sweep_bridge_list(void)
85 if (!bridge_list)
86 bridge_list = smartlist_new();
87 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
88 if (b->marked_for_removal) {
89 SMARTLIST_DEL_CURRENT(bridge_list, b);
90 bridge_free(b);
92 } SMARTLIST_FOREACH_END(b);
95 /** Initialize the bridge list to empty, creating it if needed. */
96 static void
97 clear_bridge_list(void)
99 if (!bridge_list)
100 bridge_list = smartlist_new();
101 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, bridge_free(b));
102 smartlist_clear(bridge_list);
105 /** Free the bridge <b>bridge</b>. */
106 static void
107 bridge_free_(bridge_info_t *bridge)
109 if (!bridge)
110 return;
112 tor_free(bridge->transport_name);
113 if (bridge->socks_args) {
114 SMARTLIST_FOREACH(bridge->socks_args, char*, s, tor_free(s));
115 smartlist_free(bridge->socks_args);
118 tor_free(bridge);
121 /** Return a list of all the configured bridges, as bridge_info_t pointers. */
122 const smartlist_t *
123 bridge_list_get(void)
125 if (!bridge_list)
126 bridge_list = smartlist_new();
127 return bridge_list;
131 * Given a <b>bridge</b>, return a pointer to its RSA identity digest, or
132 * NULL if we don't know one for it.
134 const uint8_t *
135 bridge_get_rsa_id_digest(const bridge_info_t *bridge)
137 tor_assert(bridge);
138 if (tor_digest_is_zero(bridge->identity))
139 return NULL;
140 else
141 return (const uint8_t *) bridge->identity;
145 * Given a <b>bridge</b>, return a pointer to its configured addr:port
146 * combination.
148 const tor_addr_port_t *
149 bridge_get_addr_port(const bridge_info_t *bridge)
151 tor_assert(bridge);
152 return &bridge->addrport_configured;
155 /** If we have a bridge configured whose digest matches <b>digest</b>, or a
156 * bridge with no known digest whose address matches any of the
157 * tor_addr_port_t's in <b>orports</b>, return that bridge. Else return
158 * NULL. */
159 static bridge_info_t *
160 get_configured_bridge_by_orports_digest(const char *digest,
161 const smartlist_t *orports)
163 if (!bridge_list)
164 return NULL;
165 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
167 if (tor_digest_is_zero(bridge->identity)) {
168 SMARTLIST_FOREACH_BEGIN(orports, tor_addr_port_t *, ap)
170 if (tor_addr_compare(&bridge->addr, &ap->addr, CMP_EXACT) == 0 &&
171 bridge->port == ap->port)
172 return bridge;
174 SMARTLIST_FOREACH_END(ap);
176 if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
177 return bridge;
179 SMARTLIST_FOREACH_END(bridge);
180 return NULL;
183 /** If we have a bridge configured whose digest matches <b>digest</b>, or a
184 * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
185 * return that bridge. Else return NULL. If <b>digest</b> is NULL, check for
186 * address/port matches only. */
187 bridge_info_t *
188 get_configured_bridge_by_addr_port_digest(const tor_addr_t *addr,
189 uint16_t port,
190 const char *digest)
192 if (!bridge_list)
193 return NULL;
194 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
196 if ((tor_digest_is_zero(bridge->identity) || digest == NULL) &&
197 !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
198 bridge->port == port)
199 return bridge;
200 if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
201 return bridge;
203 SMARTLIST_FOREACH_END(bridge);
204 return NULL;
208 * As get_configured_bridge_by_addr_port, but require that the
209 * address match <b>addr</b>:<b>port</b>, and that the ID digest match
210 * <b>digest</b>. (The other function will ignore the address if the
211 * digest matches.)
213 bridge_info_t *
214 get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t *addr,
215 uint16_t port,
216 const char *digest)
218 if (!bridge_list)
219 return NULL;
220 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
221 if (!tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
222 bridge->port == port) {
224 if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
225 return bridge;
226 else if (!digest || tor_digest_is_zero(bridge->identity))
227 return bridge;
230 } SMARTLIST_FOREACH_END(bridge);
231 return NULL;
234 /** If we have a bridge configured whose digest matches <b>digest</b>, or a
235 * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
236 * return 1. Else return 0. If <b>digest</b> is NULL, check for
237 * address/port matches only. */
239 addr_is_a_configured_bridge(const tor_addr_t *addr,
240 uint16_t port,
241 const char *digest)
243 tor_assert(addr);
244 return get_configured_bridge_by_addr_port_digest(addr, port, digest) ? 1 : 0;
247 /** If we have a bridge configured whose digest matches
248 * <b>ei->identity_digest</b>, or a bridge with no known digest whose address
249 * matches <b>ei->addr</b>:<b>ei->port</b>, return 1. Else return 0.
250 * If <b>ei->onion_key</b> is NULL, check for address/port matches only. */
252 extend_info_is_a_configured_bridge(const extend_info_t *ei)
254 const char *digest = ei->onion_key ? ei->identity_digest : NULL;
255 return addr_is_a_configured_bridge(&ei->addr, ei->port, digest);
258 /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
259 * it up via router descriptor <b>ri</b>. */
260 static bridge_info_t *
261 get_configured_bridge_by_routerinfo(const routerinfo_t *ri)
263 bridge_info_t *bi = NULL;
264 smartlist_t *orports = router_get_all_orports(ri);
265 bi = get_configured_bridge_by_orports_digest(ri->cache_info.identity_digest,
266 orports);
267 SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
268 smartlist_free(orports);
269 return bi;
272 /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
274 routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
276 return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
279 /** Return 1 if <b>node</b> is one of our configured bridges, else 0. */
281 node_is_a_configured_bridge(const node_t *node)
283 int retval = 0;
284 smartlist_t *orports = node_get_all_orports(node);
285 retval = get_configured_bridge_by_orports_digest(node->identity,
286 orports) != NULL;
287 SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
288 smartlist_free(orports);
289 return retval;
292 /** We made a connection to a router at <b>addr</b>:<b>port</b>
293 * without knowing its digest. Its digest turned out to be <b>digest</b>.
294 * If it was a bridge, and we still don't know its digest, record it.
296 void
297 learned_router_identity(const tor_addr_t *addr, uint16_t port,
298 const char *digest,
299 const ed25519_public_key_t *ed_id)
301 // XXXX prop220 use ed_id here, once there is some way to specify
302 (void)ed_id;
303 int learned = 0;
304 bridge_info_t *bridge =
305 get_configured_bridge_by_exact_addr_port_digest(addr, port, digest);
306 if (bridge && tor_digest_is_zero(bridge->identity)) {
307 memcpy(bridge->identity, digest, DIGEST_LEN);
308 learned = 1;
310 /* XXXX prop220 remember bridge ed25519 identities -- add a field */
311 #if 0
312 if (bridge && ed_id &&
313 ed25519_public_key_is_zero(&bridge->ed25519_identity) &&
314 !ed25519_public_key_is_zero(ed_id)) {
315 memcpy(&bridge->ed25519_identity, ed_id, sizeof(*ed_id));
316 learned = 1;
318 #endif /* 0 */
319 if (learned) {
320 char *transport_info = NULL;
321 const char *transport_name =
322 find_transport_name_by_bridge_addrport(addr, port);
323 if (transport_name)
324 tor_asprintf(&transport_info, " (with transport '%s')", transport_name);
326 // XXXX prop220 log both fingerprints.
327 log_notice(LD_DIR, "Learned fingerprint %s for bridge %s%s.",
328 hex_str(digest, DIGEST_LEN), fmt_addrport(addr, port),
329 transport_info ? transport_info : "");
330 tor_free(transport_info);
331 entry_guard_learned_bridge_identity(&bridge->addrport_configured,
332 (const uint8_t *)digest);
336 /** Return true if <b>bridge</b> has the same identity digest as
337 * <b>digest</b>. If <b>digest</b> is NULL, it matches
338 * bridges with unspecified identity digests. */
339 static int
340 bridge_has_digest(const bridge_info_t *bridge, const char *digest)
342 if (digest)
343 return tor_memeq(digest, bridge->identity, DIGEST_LEN);
344 else
345 return tor_digest_is_zero(bridge->identity);
348 /** We are about to add a new bridge at <b>addr</b>:<b>port</b>, with optional
349 * <b>digest</b> and <b>transport_name</b>. Mark for removal any previously
350 * existing bridge with the same address and port, and warn the user as
351 * appropriate.
353 static void
354 bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port,
355 const char *digest, const char *transport_name)
357 /* Iterate the already-registered bridge list:
359 If you find a bridge with the same address and port, mark it for
360 removal. It doesn't make sense to have two active bridges with
361 the same IP:PORT. If the bridge in question has a different
362 digest or transport than <b>digest</b>/<b>transport_name</b>,
363 it's probably a misconfiguration and we should warn the user.
365 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
366 if (bridge->marked_for_removal)
367 continue;
369 if (tor_addr_eq(&bridge->addr, addr) && (bridge->port == port)) {
371 bridge->marked_for_removal = 1;
373 if (!bridge_has_digest(bridge, digest) ||
374 strcmp_opt(bridge->transport_name, transport_name)) {
375 /* warn the user */
376 char *bridge_description_new, *bridge_description_old;
377 tor_asprintf(&bridge_description_new, "%s:%s:%s",
378 fmt_addrport(addr, port),
379 digest ? hex_str(digest, DIGEST_LEN) : "",
380 transport_name ? transport_name : "");
381 tor_asprintf(&bridge_description_old, "%s:%s:%s",
382 fmt_addrport(&bridge->addr, bridge->port),
383 tor_digest_is_zero(bridge->identity) ?
384 "" : hex_str(bridge->identity,DIGEST_LEN),
385 bridge->transport_name ? bridge->transport_name : "");
387 log_warn(LD_GENERAL,"Tried to add bridge '%s', but we found a conflict"
388 " with the already registered bridge '%s'. We will discard"
389 " the old bridge and keep '%s'. If this is not what you"
390 " wanted, please change your configuration file accordingly.",
391 bridge_description_new, bridge_description_old,
392 bridge_description_new);
394 tor_free(bridge_description_new);
395 tor_free(bridge_description_old);
398 } SMARTLIST_FOREACH_END(bridge);
401 /** Return True if we have a bridge that uses a transport with name
402 * <b>transport_name</b>. */
403 MOCK_IMPL(int,
404 transport_is_needed, (const char *transport_name))
406 if (!bridge_list)
407 return 0;
409 SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
410 if (bridge->transport_name &&
411 !strcmp(bridge->transport_name, transport_name))
412 return 1;
413 } SMARTLIST_FOREACH_END(bridge);
415 return 0;
418 /** Register the bridge information in <b>bridge_line</b> to the
419 * bridge subsystem. Steals reference of <b>bridge_line</b>. */
420 void
421 bridge_add_from_config(bridge_line_t *bridge_line)
423 bridge_info_t *b;
425 // XXXX prop220 add a way to specify ed25519 ID to bridge_line_t.
427 { /* Log the bridge we are about to register: */
428 log_debug(LD_GENERAL, "Registering bridge at %s (transport: %s) (%s)",
429 fmt_addrport(&bridge_line->addr, bridge_line->port),
430 bridge_line->transport_name ?
431 bridge_line->transport_name : "no transport",
432 tor_digest_is_zero(bridge_line->digest) ?
433 "no key listed" : hex_str(bridge_line->digest, DIGEST_LEN));
435 if (bridge_line->socks_args) { /* print socks arguments */
436 int i = 0;
438 tor_assert(smartlist_len(bridge_line->socks_args) > 0);
440 log_debug(LD_GENERAL, "Bridge uses %d SOCKS arguments:",
441 smartlist_len(bridge_line->socks_args));
442 SMARTLIST_FOREACH(bridge_line->socks_args, const char *, arg,
443 log_debug(LD_CONFIG, "%d: %s", ++i, arg));
447 bridge_resolve_conflicts(&bridge_line->addr,
448 bridge_line->port,
449 bridge_line->digest,
450 bridge_line->transport_name);
452 b = tor_malloc_zero(sizeof(bridge_info_t));
453 tor_addr_copy(&b->addrport_configured.addr, &bridge_line->addr);
454 b->addrport_configured.port = bridge_line->port;
455 tor_addr_copy(&b->addr, &bridge_line->addr);
456 b->port = bridge_line->port;
457 memcpy(b->identity, bridge_line->digest, DIGEST_LEN);
458 if (bridge_line->transport_name)
459 b->transport_name = bridge_line->transport_name;
460 b->fetch_status.schedule = DL_SCHED_BRIDGE;
461 b->fetch_status.increment_on = DL_SCHED_INCREMENT_ATTEMPT;
462 /* We can't reset the bridge's download status here, because UseBridges
463 * might be 0 now, and it might be changed to 1 much later. */
464 b->socks_args = bridge_line->socks_args;
465 if (!bridge_list)
466 bridge_list = smartlist_new();
468 tor_free(bridge_line); /* Deallocate bridge_line now. */
470 smartlist_add(bridge_list, b);
473 /** If <b>digest</b> is one of our known bridges, return it. */
474 bridge_info_t *
475 find_bridge_by_digest(const char *digest)
477 if (! bridge_list)
478 return NULL;
479 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
481 if (tor_memeq(bridge->identity, digest, DIGEST_LEN))
482 return bridge;
484 return NULL;
487 /** Given the <b>addr</b> and <b>port</b> of a bridge, if that bridge
488 * supports a pluggable transport, return its name. Otherwise, return
489 * NULL. */
490 const char *
491 find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
493 if (!bridge_list)
494 return NULL;
496 SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
497 if (tor_addr_eq(&bridge->addr, addr) &&
498 (bridge->port == port))
499 return bridge->transport_name;
500 } SMARTLIST_FOREACH_END(bridge);
502 return NULL;
505 /** If <b>addr</b> and <b>port</b> match the address and port of a
506 * bridge of ours that uses pluggable transports, place its transport
507 * in <b>transport</b>.
509 * Return 0 on success (found a transport, or found a bridge with no
510 * transport, or found no bridge); return -1 if we should be using a
511 * transport, but the transport could not be found.
514 get_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
515 const transport_t **transport)
517 *transport = NULL;
518 if (!bridge_list)
519 return 0;
521 SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
522 if (tor_addr_eq(&bridge->addr, addr) &&
523 (bridge->port == port)) { /* bridge matched */
524 if (bridge->transport_name) { /* it also uses pluggable transports */
525 *transport = transport_get_by_name(bridge->transport_name);
526 if (*transport == NULL) { /* it uses pluggable transports, but
527 the transport could not be found! */
528 return -1;
530 return 0;
531 } else { /* bridge matched, but it doesn't use transports. */
532 break;
535 } SMARTLIST_FOREACH_END(bridge);
537 *transport = NULL;
538 return 0;
541 /** Return a smartlist containing all the SOCKS arguments that we
542 * should pass to the SOCKS proxy. */
543 const smartlist_t *
544 get_socks_args_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
546 bridge_info_t *bridge = get_configured_bridge_by_addr_port_digest(addr,
547 port,
548 NULL);
549 return bridge ? bridge->socks_args : NULL;
552 /** We need to ask <b>bridge</b> for its server descriptor. */
553 static void
554 launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
556 const or_options_t *options = get_options();
557 circuit_guard_state_t *guard_state = NULL;
559 if (connection_get_by_type_addr_port_purpose(
560 CONN_TYPE_DIR, &bridge->addr, bridge->port,
561 DIR_PURPOSE_FETCH_SERVERDESC))
562 return; /* it's already on the way */
564 if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
565 download_status_mark_impossible(&bridge->fetch_status);
566 log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
567 safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
568 return;
571 /* Until we get a descriptor for the bridge, we only know one address for
572 * it. */
573 if (!fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
574 FIREWALL_OR_CONNECTION, 0, 0)) {
575 log_notice(LD_CONFIG, "Tried to fetch a descriptor directly from a "
576 "bridge, but that bridge is not reachable through our "
577 "firewall.");
578 return;
581 /* If we already have a node_t for this bridge, rewrite its address now. */
582 node_t *node = node_get_mutable_by_id(bridge->identity);
583 if (node) {
584 rewrite_node_address_for_bridge(bridge, node);
587 tor_addr_port_t bridge_addrport;
588 memcpy(&bridge_addrport.addr, &bridge->addr, sizeof(tor_addr_t));
589 bridge_addrport.port = bridge->port;
591 guard_state = get_guard_state_for_bridge_desc_fetch(bridge->identity);
593 directory_request_t *req =
594 directory_request_new(DIR_PURPOSE_FETCH_SERVERDESC);
595 directory_request_set_or_addr_port(req, &bridge_addrport);
596 directory_request_set_directory_id_digest(req, bridge->identity);
597 directory_request_set_router_purpose(req, ROUTER_PURPOSE_BRIDGE);
598 directory_request_set_resource(req, "authority.z");
599 if (guard_state) {
600 directory_request_set_guard_state(req, guard_state);
602 directory_initiate_request(req);
603 directory_request_free(req);
606 /** Fetching the bridge descriptor from the bridge authority returned a
607 * "not found". Fall back to trying a direct fetch. */
608 void
609 retry_bridge_descriptor_fetch_directly(const char *digest)
611 bridge_info_t *bridge = find_bridge_by_digest(digest);
612 if (!bridge)
613 return; /* not found? oh well. */
615 launch_direct_bridge_descriptor_fetch(bridge);
618 /** For each bridge in our list for which we don't currently have a
619 * descriptor, fetch a new copy of its descriptor -- either directly
620 * from the bridge or via a bridge authority. */
621 void
622 fetch_bridge_descriptors(const or_options_t *options, time_t now)
624 int num_bridge_auths = get_n_authorities(BRIDGE_DIRINFO);
625 int ask_bridge_directly;
626 int can_use_bridge_authority;
628 if (!bridge_list)
629 return;
631 /* If we still have unconfigured managed proxies, don't go and
632 connect to a bridge. */
633 if (pt_proxies_configuration_pending())
634 return;
636 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
638 /* This resets the download status on first use */
639 if (!download_status_is_ready(&bridge->fetch_status, now))
640 continue; /* don't bother, no need to retry yet */
641 if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
642 download_status_mark_impossible(&bridge->fetch_status);
643 log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
644 safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
645 continue;
648 /* schedule the next attempt
649 * we can't increment after a failure, because sometimes we use the
650 * bridge authority, and sometimes we use the bridge direct */
651 download_status_increment_attempt(
652 &bridge->fetch_status,
653 safe_str_client(fmt_and_decorate_addr(&bridge->addr)),
654 now);
656 can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
657 num_bridge_auths;
658 ask_bridge_directly = !can_use_bridge_authority ||
659 !options->UpdateBridgesFromAuthority;
660 log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
661 ask_bridge_directly, tor_digest_is_zero(bridge->identity),
662 !options->UpdateBridgesFromAuthority, !num_bridge_auths);
664 if (ask_bridge_directly &&
665 !fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
666 FIREWALL_OR_CONNECTION, 0,
667 0)) {
668 log_notice(LD_DIR, "Bridge at '%s' isn't reachable by our "
669 "firewall policy. %s.",
670 fmt_addrport(&bridge->addr, bridge->port),
671 can_use_bridge_authority ?
672 "Asking bridge authority instead" : "Skipping");
673 if (can_use_bridge_authority)
674 ask_bridge_directly = 0;
675 else
676 continue;
679 if (ask_bridge_directly) {
680 /* we need to ask the bridge itself for its descriptor. */
681 launch_direct_bridge_descriptor_fetch(bridge);
682 } else {
683 /* We have a digest and we want to ask an authority. We could
684 * combine all the requests into one, but that may give more
685 * hints to the bridge authority than we want to give. */
686 char resource[10 + HEX_DIGEST_LEN];
687 memcpy(resource, "fp/", 3);
688 base16_encode(resource+3, HEX_DIGEST_LEN+1,
689 bridge->identity, DIGEST_LEN);
690 memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
691 log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
692 resource);
693 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
694 ROUTER_PURPOSE_BRIDGE, resource, 0, DL_WANT_AUTHORITY);
697 SMARTLIST_FOREACH_END(bridge);
700 /** If our <b>bridge</b> is configured to be a different address than
701 * the bridge gives in <b>node</b>, rewrite the routerinfo
702 * we received to use the address we meant to use. Now we handle
703 * multihomed bridges better.
705 static void
706 rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
708 /* XXXX move this function. */
709 /* XXXX overridden addresses should really live in the node_t, so that the
710 * routerinfo_t and the microdesc_t can be immutable. But we can only
711 * do that safely if we know that no function that connects to an OR
712 * does so through an address from any source other than node_get_addr().
714 tor_addr_t addr;
715 const or_options_t *options = get_options();
717 if (node->ri) {
718 routerinfo_t *ri = node->ri;
719 tor_addr_from_ipv4h(&addr, ri->addr);
720 if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
721 bridge->port == ri->or_port) ||
722 (!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
723 bridge->port == ri->ipv6_orport)) {
724 /* they match, so no need to do anything */
725 } else {
726 if (tor_addr_family(&bridge->addr) == AF_INET) {
727 ri->addr = tor_addr_to_ipv4h(&bridge->addr);
728 ri->or_port = bridge->port;
729 log_info(LD_DIR,
730 "Adjusted bridge routerinfo for '%s' to match configured "
731 "address %s:%d.",
732 ri->nickname, fmt_addr32(ri->addr), ri->or_port);
733 } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
734 tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
735 ri->ipv6_orport = bridge->port;
736 log_info(LD_DIR,
737 "Adjusted bridge routerinfo for '%s' to match configured "
738 "address %s.",
739 ri->nickname, fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
740 } else {
741 log_err(LD_BUG, "Address family not supported: %d.",
742 tor_addr_family(&bridge->addr));
743 return;
747 if (options->ClientPreferIPv6ORPort == -1) {
748 /* Mark which address to use based on which bridge_t we got. */
749 node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
750 !tor_addr_is_null(&node->ri->ipv6_addr));
751 } else {
752 /* Mark which address to use based on user preference */
753 node->ipv6_preferred = (fascist_firewall_prefer_ipv6_orport(options) &&
754 !tor_addr_is_null(&node->ri->ipv6_addr));
757 /* XXXipv6 we lack support for falling back to another address for
758 the same relay, warn the user */
759 if (!tor_addr_is_null(&ri->ipv6_addr)) {
760 tor_addr_port_t ap;
761 node_get_pref_orport(node, &ap);
762 log_notice(LD_CONFIG,
763 "Bridge '%s' has both an IPv4 and an IPv6 address. "
764 "Will prefer using its %s address (%s) based on %s.",
765 ri->nickname,
766 node->ipv6_preferred ? "IPv6" : "IPv4",
767 fmt_addrport(&ap.addr, ap.port),
768 options->ClientPreferIPv6ORPort == -1 ?
769 "the configured Bridge address" :
770 "ClientPreferIPv6ORPort");
773 if (node->rs) {
774 routerstatus_t *rs = node->rs;
775 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;
805 if (options->ClientPreferIPv6ORPort == -1) {
806 /* Mark which address to use based on which bridge_t we got. */
807 node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
808 !tor_addr_is_null(&node->rs->ipv6_addr));
809 } else {
810 /* Mark which address to use based on user preference */
811 node->ipv6_preferred = (fascist_firewall_prefer_ipv6_orport(options) &&
812 !tor_addr_is_null(&node->rs->ipv6_addr));
815 /* XXXipv6 we lack support for falling back to another address for
816 the same relay, warn the user */
817 if (!tor_addr_is_null(&rs->ipv6_addr)) {
818 tor_addr_port_t ap;
819 node_get_pref_orport(node, &ap);
820 log_notice(LD_CONFIG,
821 "Bridge '%s' has both an IPv4 and an IPv6 address. "
822 "Will prefer using its %s address (%s) based on %s.",
823 rs->nickname,
824 node->ipv6_preferred ? "IPv6" : "IPv4",
825 fmt_addrport(&ap.addr, ap.port),
826 options->ClientPreferIPv6ORPort == -1 ?
827 "the configured Bridge address" :
828 "ClientPreferIPv6ORPort");
833 /** We just learned a descriptor for a bridge. See if that
834 * digest is in our entry guard list, and add it if not. */
835 void
836 learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
838 tor_assert(ri);
839 tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
840 if (get_options()->UseBridges) {
841 /* Retry directory downloads whenever we get a bridge descriptor:
842 * - when bootstrapping, and
843 * - when we aren't sure if any of our bridges are reachable.
844 * Keep on retrying until we have at least one reachable bridge. */
845 int first = num_bridges_usable(0) < 1;
846 bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
847 time_t now = time(NULL);
848 router_set_status(ri->cache_info.identity_digest, 1);
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);
860 node = node_get_mutable_by_id(ri->cache_info.identity_digest);
861 tor_assert(node);
862 rewrite_node_address_for_bridge(bridge, node);
863 if (tor_digest_is_zero(bridge->identity)) {
864 memcpy(bridge->identity,ri->cache_info.identity_digest, DIGEST_LEN);
865 log_notice(LD_DIR, "Learned identity %s for bridge at %s:%d",
866 hex_str(bridge->identity, DIGEST_LEN),
867 fmt_and_decorate_addr(&bridge->addr),
868 (int) bridge->port);
870 entry_guard_learned_bridge_identity(&bridge->addrport_configured,
871 (const uint8_t*)ri->cache_info.identity_digest);
873 log_notice(LD_DIR, "new bridge descriptor '%s' (%s): %s", ri->nickname,
874 from_cache ? "cached" : "fresh", router_describe(ri));
875 /* If we didn't have a reachable bridge before this one, try directory
876 * documents again. */
877 if (first) {
878 routerlist_retry_directory_downloads(now);
884 /** Return a smartlist containing all bridge identity digests */
885 MOCK_IMPL(smartlist_t *,
886 list_bridge_identities, (void))
888 smartlist_t *result = NULL;
889 char *digest_tmp;
891 if (get_options()->UseBridges && bridge_list) {
892 result = smartlist_new();
894 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
895 digest_tmp = tor_malloc(DIGEST_LEN);
896 memcpy(digest_tmp, b->identity, DIGEST_LEN);
897 smartlist_add(result, digest_tmp);
898 } SMARTLIST_FOREACH_END(b);
901 return result;
904 /** Get the download status for a bridge descriptor given its identity */
905 MOCK_IMPL(download_status_t *,
906 get_bridge_dl_status_by_id, (const char *digest))
908 download_status_t *dl = NULL;
910 if (digest && get_options()->UseBridges && bridge_list) {
911 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
912 if (tor_memeq(digest, b->identity, DIGEST_LEN)) {
913 dl = &(b->fetch_status);
914 break;
916 } SMARTLIST_FOREACH_END(b);
919 return dl;
922 /** Release all storage held in bridges.c */
923 void
924 bridges_free_all(void)
926 clear_bridge_list();
927 smartlist_free(bridge_list);
928 bridge_list = NULL;