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 */
9 * \brief Code to manage bridges and bridge selection.
11 * Bridges are fixed entry nodes, used for censorship circumvention.
16 #include "circuitbuild.h"
18 #include "connection.h"
19 #include "directory.h"
20 #include "entrynodes.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. */
36 /** TLS port for the bridge. */
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. */
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
,
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
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. */
72 mark_bridge_list(void)
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. */
83 sweep_bridge_list(void)
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
);
92 } SMARTLIST_FOREACH_END(b
);
95 /** Initialize the bridge list to empty, creating it if needed. */
97 clear_bridge_list(void)
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>. */
107 bridge_free_(bridge_info_t
*bridge
)
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
);
121 /** Return a list of all the configured bridges, as bridge_info_t pointers. */
123 bridge_list_get(void)
126 bridge_list
= smartlist_new();
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.
135 bridge_get_rsa_id_digest(const bridge_info_t
*bridge
)
138 if (tor_digest_is_zero(bridge
->identity
))
141 return (const uint8_t *) bridge
->identity
;
145 * Given a <b>bridge</b>, return a pointer to its configured addr:port
148 const tor_addr_port_t
*
149 bridge_get_addr_port(const bridge_info_t
*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
159 static bridge_info_t
*
160 get_configured_bridge_by_orports_digest(const char *digest
,
161 const smartlist_t
*orports
)
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
)
174 SMARTLIST_FOREACH_END(ap
);
176 if (digest
&& tor_memeq(bridge
->identity
, digest
, DIGEST_LEN
))
179 SMARTLIST_FOREACH_END(bridge
);
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. */
188 get_configured_bridge_by_addr_port_digest(const tor_addr_t
*addr
,
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
)
200 if (digest
&& tor_memeq(bridge
->identity
, digest
, DIGEST_LEN
))
203 SMARTLIST_FOREACH_END(bridge
);
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
214 get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t
*addr
,
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
))
226 else if (!digest
|| tor_digest_is_zero(bridge
->identity
))
230 } SMARTLIST_FOREACH_END(bridge
);
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
,
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
,
267 SMARTLIST_FOREACH(orports
, tor_addr_port_t
*, p
, tor_free(p
));
268 smartlist_free(orports
);
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
)
284 smartlist_t
*orports
= node_get_all_orports(node
);
285 retval
= get_configured_bridge_by_orports_digest(node
->identity
,
287 SMARTLIST_FOREACH(orports
, tor_addr_port_t
*, p
, tor_free(p
));
288 smartlist_free(orports
);
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.
297 learned_router_identity(const tor_addr_t
*addr
, uint16_t port
,
299 const ed25519_public_key_t
*ed_id
)
301 // XXXX prop220 use ed_id here, once there is some way to specify
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
);
310 /* XXXX prop220 remember bridge ed25519 identities -- add a field */
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
));
320 char *transport_info
= NULL
;
321 const char *transport_name
=
322 find_transport_name_by_bridge_addrport(addr
, port
);
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. */
340 bridge_has_digest(const bridge_info_t
*bridge
, const char *digest
)
343 return tor_memeq(digest
, bridge
->identity
, DIGEST_LEN
);
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
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
)
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
)) {
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>. */
404 transport_is_needed
, (const char *transport_name
))
409 SMARTLIST_FOREACH_BEGIN(bridge_list
, const bridge_info_t
*, bridge
) {
410 if (bridge
->transport_name
&&
411 !strcmp(bridge
->transport_name
, transport_name
))
413 } SMARTLIST_FOREACH_END(bridge
);
418 /** Register the bridge information in <b>bridge_line</b> to the
419 * bridge subsystem. Steals reference of <b>bridge_line</b>. */
421 bridge_add_from_config(bridge_line_t
*bridge_line
)
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 */
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
,
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
;
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. */
475 find_bridge_by_digest(const char *digest
)
479 SMARTLIST_FOREACH(bridge_list
, bridge_info_t
*, bridge
,
481 if (tor_memeq(bridge
->identity
, digest
, DIGEST_LEN
))
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
491 find_transport_name_by_bridge_addrport(const tor_addr_t
*addr
, uint16_t port
)
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
);
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
)
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! */
531 } else { /* bridge matched, but it doesn't use transports. */
535 } SMARTLIST_FOREACH_END(bridge
);
541 /** Return a smartlist containing all the SOCKS arguments that we
542 * should pass to the SOCKS proxy. */
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
,
549 return bridge
? bridge
->socks_args
: NULL
;
552 /** We need to ask <b>bridge</b> for its server descriptor. */
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
)));
571 /* Until we get a descriptor for the bridge, we only know one address for
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 "
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
);
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");
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. */
609 retry_bridge_descriptor_fetch_directly(const char *digest
)
611 bridge_info_t
*bridge
= find_bridge_by_digest(digest
);
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. */
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
;
631 /* If we still have unconfigured managed proxies, don't go and
632 connect to a bridge. */
633 if (pt_proxies_configuration_pending())
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
)));
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
)),
656 can_use_bridge_authority
= !tor_digest_is_zero(bridge
->identity
) &&
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,
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;
679 if (ask_bridge_directly
) {
680 /* we need to ask the bridge itself for its descriptor. */
681 launch_direct_bridge_descriptor_fetch(bridge
);
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.",
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.
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().
715 const or_options_t
*options
= get_options();
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 */
726 if (tor_addr_family(&bridge
->addr
) == AF_INET
) {
727 ri
->addr
= tor_addr_to_ipv4h(&bridge
->addr
);
728 ri
->or_port
= bridge
->port
;
730 "Adjusted bridge routerinfo for '%s' to match configured "
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
;
737 "Adjusted bridge routerinfo for '%s' to match configured "
739 ri
->nickname
, fmt_addrport(&ri
->ipv6_addr
, ri
->ipv6_orport
));
741 log_err(LD_BUG
, "Address family not supported: %d.",
742 tor_addr_family(&bridge
->addr
));
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
));
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
)) {
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.",
766 node
->ipv6_preferred
? "IPv6" : "IPv4",
767 fmt_addrport(&ap
.addr
, ap
.port
),
768 options
->ClientPreferIPv6ORPort
== -1 ?
769 "the configured Bridge address" :
770 "ClientPreferIPv6ORPort");
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 */
783 if (tor_addr_family(&bridge
->addr
) == AF_INET
) {
784 rs
->addr
= tor_addr_to_ipv4h(&bridge
->addr
);
785 rs
->or_port
= bridge
->port
;
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
;
795 "Adjusted bridge routerstatus for '%s' to match configured"
797 rs
->nickname
, fmt_addrport(&rs
->ipv6_addr
, rs
->ipv6_orport
));
799 log_err(LD_BUG
, "Address family not supported: %d.",
800 tor_addr_family(&bridge
->addr
));
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
));
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
)) {
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.",
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. */
836 learned_bridge_descriptor(routerinfo_t
*ri
, int from_cache
)
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 */
852 /* it's here; schedule its re-fetch for a long time from now. */
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
);
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
),
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. */
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
;
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
);
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
);
916 } SMARTLIST_FOREACH_END(b
);
922 /** Release all storage held in bridges.c */
924 bridges_free_all(void)
927 smartlist_free(bridge_list
);