2020-05-05 13:24:03 -04:00
|
|
|
/* Copyright (c) 2001-2020, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
2020-05-05 15:34:58 -04:00
|
|
|
* \file relay_find_addr.c
|
|
|
|
* \brief Implement mechanism for a relay to find its address.
|
2020-05-05 13:24:03 -04:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "core/or/or.h"
|
|
|
|
|
|
|
|
#include "app/config/config.h"
|
2020-05-05 13:42:52 -04:00
|
|
|
#include "app/config/resolve_addr.h"
|
2020-05-05 13:24:03 -04:00
|
|
|
|
|
|
|
#include "core/mainloop/mainloop.h"
|
|
|
|
|
|
|
|
#include "feature/control/control_events.h"
|
|
|
|
#include "feature/dircommon/dir_connection_st.h"
|
2020-07-06 09:42:10 -04:00
|
|
|
#include "feature/nodelist/dirlist.h"
|
2020-05-05 15:34:58 -04:00
|
|
|
#include "feature/relay/relay_find_addr.h"
|
2020-05-05 13:24:03 -04:00
|
|
|
#include "feature/relay/router.h"
|
|
|
|
#include "feature/relay/routermode.h"
|
|
|
|
|
2020-07-06 09:42:10 -04:00
|
|
|
/** Consider the address suggestion suggested_addr as a possible one to use as
|
|
|
|
* our address.
|
|
|
|
*
|
2020-07-17 10:34:30 -04:00
|
|
|
* This is called when a valid NETINFO cell is received containing a candidate
|
|
|
|
* for our address or when a directory sends us back the X-Your-Address-Is
|
|
|
|
* header.
|
2020-07-06 09:42:10 -04:00
|
|
|
*
|
|
|
|
* The suggested address is ignored if it does NOT come from a trusted source.
|
|
|
|
* At the moment, we only look a trusted directory authorities.
|
|
|
|
*
|
|
|
|
* The suggested address is ignored if it is internal or it is the same as the
|
|
|
|
* given peer_addr which is the address from the endpoint that sent the
|
|
|
|
* NETINFO cell.
|
|
|
|
*
|
2020-07-17 10:34:30 -04:00
|
|
|
* The identity_digest is NULL if this is an address suggested by a directory
|
|
|
|
* since this is a plaintext connection.
|
|
|
|
*
|
2020-07-06 09:42:10 -04:00
|
|
|
* The suggested address is set in our suggested address cache if everything
|
|
|
|
* passes. */
|
|
|
|
void
|
|
|
|
relay_address_new_suggestion(const tor_addr_t *suggested_addr,
|
2020-07-08 07:46:12 -04:00
|
|
|
const tor_addr_t *peer_addr,
|
|
|
|
const char *identity_digest)
|
2020-07-06 09:42:10 -04:00
|
|
|
{
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
|
|
|
|
tor_assert(suggested_addr);
|
|
|
|
tor_assert(peer_addr);
|
|
|
|
|
2020-07-10 13:06:20 -04:00
|
|
|
/* Non server should just ignore this suggestion. Clients don't need to
|
|
|
|
* learn their address let alone cache it. */
|
|
|
|
if (!server_mode(options)) {
|
2020-07-06 09:42:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the peer a trusted source? Ignore anything coming from non trusted
|
2020-07-08 07:46:12 -04:00
|
|
|
* source. In this case, we only look at trusted directory authorities. */
|
|
|
|
if (!router_addr_is_trusted_dir(peer_addr) ||
|
2020-07-17 10:34:30 -04:00
|
|
|
(identity_digest && !router_digest_is_trusted_dir(identity_digest))) {
|
2020-07-06 09:42:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore a suggestion that is an internal address or the same as the one
|
|
|
|
* the peer address. */
|
|
|
|
if (tor_addr_is_internal(suggested_addr, 0)) {
|
|
|
|
/* Do not believe anyone who says our address is internal. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tor_addr_eq(suggested_addr, peer_addr)) {
|
|
|
|
/* Do not believe anyone who says our address is their address. */
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"A relay endpoint %s is telling us that their address is ours.",
|
|
|
|
fmt_addr(peer_addr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the suggestion in our cache. */
|
|
|
|
resolved_addr_set_suggested(suggested_addr);
|
|
|
|
}
|
|
|
|
|
2020-07-13 08:23:20 -04:00
|
|
|
/** Find our address to be published in our descriptor. Three places are
|
|
|
|
* looked at:
|
|
|
|
*
|
|
|
|
* 1. Resolved cache. Populated by find_my_address() during the relay
|
|
|
|
* periodic event that attempts to learn if our address has changed.
|
|
|
|
*
|
2020-07-17 11:10:56 -04:00
|
|
|
* 2. If flags is set with RELAY_FIND_ADDR_CACHE_ONLY, only the resolved
|
|
|
|
* and suggested cache are looked at. No address discovery will be done.
|
2020-07-13 08:23:20 -04:00
|
|
|
*
|
|
|
|
* 3. Finally, if all fails, use the suggested address cache which is
|
2020-07-17 11:10:56 -04:00
|
|
|
* populated by the NETINFO cell content or HTTP header from a
|
|
|
|
* directory.
|
2020-07-13 08:23:20 -04:00
|
|
|
*
|
|
|
|
* Return true on success and addr_out contains the address to use for the
|
|
|
|
* given family. On failure to find the address, false is returned and
|
|
|
|
* addr_out is set to an AF_UNSPEC address. */
|
2020-07-14 13:07:53 -04:00
|
|
|
MOCK_IMPL(bool,
|
|
|
|
relay_find_addr_to_publish, (const or_options_t *options, int family,
|
2020-07-17 11:10:56 -04:00
|
|
|
int flags, tor_addr_t *addr_out))
|
2020-07-13 08:23:20 -04:00
|
|
|
{
|
|
|
|
tor_assert(options);
|
|
|
|
tor_assert(addr_out);
|
|
|
|
|
|
|
|
tor_addr_make_unspec(addr_out);
|
|
|
|
|
|
|
|
/* First, check our resolved address cache. It should contain the address
|
|
|
|
* we've discovered from the periodic relay event. */
|
|
|
|
resolved_addr_get_last(family, addr_out);
|
|
|
|
if (!tor_addr_is_null(addr_out)) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second, attempt to find our address. The following can do a DNS resolve
|
|
|
|
* thus only do it when the no cache only flag is flipped. */
|
2020-07-17 11:10:56 -04:00
|
|
|
if (!(flags & RELAY_FIND_ADDR_CACHE_ONLY)) {
|
2020-07-13 08:23:20 -04:00
|
|
|
if (find_my_address(options, family, LOG_INFO, addr_out, NULL, NULL)) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Third, consider address from our suggestion cache. */
|
|
|
|
resolved_addr_get_suggested(family, addr_out);
|
|
|
|
if (!tor_addr_is_null(addr_out)) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No publishable address was found. */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
found:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:58:36 -04:00
|
|
|
/** Return true iff this relay has an address set for the given family.
|
|
|
|
*
|
|
|
|
* This only checks the caches so it will not trigger a full discovery of the
|
|
|
|
* address. */
|
|
|
|
bool
|
|
|
|
relay_has_address_set(int family)
|
|
|
|
{
|
|
|
|
tor_addr_t addr;
|
2020-07-17 11:10:56 -04:00
|
|
|
return relay_find_addr_to_publish(get_options(), family,
|
|
|
|
RELAY_FIND_ADDR_CACHE_ONLY, &addr);
|
2020-07-14 12:58:36 -04:00
|
|
|
}
|