resolved-dns-transaction.c revision d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "af-list.h"
#include "alloc-util.h"
#include "dns-domain.h"
#include "fd-util.h"
#include "random-util.h"
#include "resolved-dns-transaction.h"
#include "resolved-llmnr.h"
#include "string-table.h"
DnsZoneItem *i;
if (!t)
return NULL;
dns_packet_unref(t->sent);
dns_packet_unref(t->received);
dns_answer_unref(t->answer);
safe_close(t->dns_udp_fd);
dns_server_unref(t->server);
dns_stream_free(t->stream);
if (t->scope) {
if (t->id != 0)
}
while ((c = set_steal_first(t->query_candidates)))
set_remove(c->transactions, t);
set_free(t->query_candidates);
while ((i = set_steal_first(t->zone_items)))
i->probe_transaction = NULL;
set_free(t->zone_items);
free(t);
return NULL;
}
void dns_transaction_gc(DnsTransaction *t) {
assert(t);
if (t->block_gc > 0)
return;
}
int r;
assert(s);
if (r < 0)
return r;
if (r < 0)
return r;
if (!t)
return -ENOMEM;
t->dns_udp_fd = -1;
/* Find a fresh, unused transaction id */
do
while (t->id == 0 ||
if (r < 0) {
t->id = 0;
return r;
}
if (r < 0) {
return r;
}
t->scope = s;
if (ret)
*ret = t;
t = NULL;
return 0;
}
static void dns_transaction_stop(DnsTransaction *t) {
assert(t);
/* Note that we do not drop the UDP socket here, as we want to
* reuse it to repeat the interaction. */
}
DnsZoneItem *z;
assert(t);
assert(p);
return;
log_debug("Transaction on scope %s on %s/%s got tentative packet from %s",
pretty);
/* RFC 4795, Section 4.1 says that the peer with the
* lexicographically smaller IP address loses */
log_debug("Peer has lexicographically larger IP address and thus lost in the conflict.");
return;
}
log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
t->block_gc++;
while ((z = set_first(t->zone_items))) {
/* First, make sure the zone item drops the reference
* to us */
/* Secondly, report this as conflict, so that we might
* look for a different hostname */
}
t->block_gc--;
}
DnsZoneItem *z;
Iterator i;
assert(t);
/* Note that this call might invalidate the query. Callers
* should hence not attempt to access the query or transaction
* after calling this function. */
log_debug("Transaction on scope %s on %s/%s now complete with <%s> from %s",
/* Notify all queries that are interested, but make sure the
* transaction isn't freed while we are still looking at it */
t->block_gc++;
SET_FOREACH(c, t->query_candidates, i)
SET_FOREACH(z, t->zone_items, i)
t->block_gc--;
}
DnsTransaction *t;
assert(s);
assert(s->transaction);
/* Copy the data we care about out of the stream before we
* destroy it. */
t = s->transaction;
p = dns_packet_ref(s->read_packet);
if (error != 0) {
return 0;
}
if (dns_packet_validate_reply(p) <= 0) {
log_debug("Invalid LLMNR TCP packet.");
return 0;
}
dns_scope_check_conflicts(t->scope, p);
t->block_gc++;
t->block_gc--;
/* If the response wasn't useful, then complete the transition now */
if (t->state == DNS_TRANSACTION_PENDING)
return 0;
}
static int dns_transaction_open_tcp(DnsTransaction *t) {
int r;
assert(t);
if (t->stream)
return 0;
case DNS_PROTOCOL_DNS:
break;
case DNS_PROTOCOL_LLMNR:
/* When we already received a reply to this (but it was truncated), send to its sender address */
if (t->received)
fd = dns_scope_tcp_socket(t->scope, t->received->family, &t->received->sender, t->received->sender_port, NULL);
else {
union in_addr_union address;
/* Otherwise, try to talk to the owner of a
* the IP address, in case this is a reverse
* PTR lookup */
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
return -ESRCH;
}
break;
default:
return -EAFNOSUPPORT;
}
if (fd < 0)
return fd;
if (r < 0)
return r;
fd = -1;
if (r < 0) {
return r;
}
dns_server_unref(t->server);
t->answer_rcode = 0;
t->stream->transaction = t;
/* The interface index is difficult to determine if we are
* connecting to the local host, hence fill this in right away
* instead of determining it from the socket */
return 0;
}
static void dns_transaction_next_dns_server(DnsTransaction *t) {
assert(t);
}
int r;
assert(t);
assert(p);
/* Note that this call might invalidate the query. Callers
* should hence not attempt to access the query or transaction
* after calling this function. */
case DNS_PROTOCOL_LLMNR:
/* For LLMNR we will not accept any packets from other
* interfaces */
return;
return;
/* Tentative packets are not full responses but still
* useful for identifying uniqueness conflicts during
* probing. */
if (DNS_PACKET_LLMNR_T(p)) {
dns_transaction_tentative(t, p);
return;
}
break;
case DNS_PROTOCOL_DNS:
break;
default:
assert_not_reached("Invalid DNS protocol.");
}
if (t->received != p) {
dns_packet_unref(t->received);
t->received = dns_packet_ref(p);
}
if (p->ipproto == IPPROTO_TCP) {
if (DNS_PACKET_TC(p)) {
/* Truncated via TCP? Somebody must be fucking with us */
return;
}
if (DNS_PACKET_ID(p) != t->id) {
/* Not the reply to our query? Somebody must be fucking with us */
return;
}
}
case DNS_PROTOCOL_DNS:
break;
case DNS_PROTOCOL_LLMNR:
case DNS_PROTOCOL_MDNS:
break;
default:
break;
}
if (DNS_PACKET_TC(p)) {
/* Response was truncated, let's try again with good old TCP */
r = dns_transaction_open_tcp(t);
if (r == -ESRCH) {
/* No servers found? Damn! */
return;
}
if (r < 0) {
/* On LLMNR, if we cannot connect to the host,
* we immediately give up */
return;
}
/* On DNS, couldn't send? Try immediately again, with a new server */
r = dns_transaction_go(t);
if (r < 0) {
return;
}
return;
}
}
/* Parse and update the cache */
r = dns_packet_extract(p);
if (r < 0) {
return;
}
/* Install the answer as answer to the transaction */
dns_answer_unref(t->answer);
t->answer_rcode = DNS_PACKET_RCODE(p);
/* Only consider responses with equivalent query section to the request */
return;
}
/* According to RFC 4795, section 2.9. only the RRs from the answer section shall be cached */
if (DNS_PACKET_SHALL_CACHE(p))
dns_cache_put(&t->scope->cache, t->key, DNS_PACKET_RCODE(p), p->answer, DNS_PACKET_ANCOUNT(p), 0, p->family, &p->sender);
if (DNS_PACKET_RCODE(p) == DNS_RCODE_SUCCESS)
else
}
DnsTransaction *t = userdata;
int r;
assert(t);
if (r <= 0)
return r;
if (dns_packet_validate_reply(p) > 0 &&
DNS_PACKET_ID(p) == t->id)
else
log_debug("Invalid DNS packet.");
return 0;
}
static int dns_transaction_emit(DnsTransaction *t) {
int r;
assert(t);
if (fd < 0)
return fd;
r = sd_event_add_io(t->scope->manager->event, &t->dns_udp_event_source, fd, EPOLLIN, on_dns_packet, t);
if (r < 0)
return r;
t->dns_udp_fd = fd;
fd = -1;
}
if (r < 0)
return r;
return 0;
}
DnsTransaction *t = userdata;
int r;
assert(s);
assert(t);
/* Timeout reached? Try again, with a new server */
/* ... and possibly increased timeout */
if (t->server)
else
r = dns_transaction_go(t);
if (r < 0)
return 0;
}
static int dns_transaction_make_packet(DnsTransaction *t) {
int r;
assert(t);
if (t->sent)
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
if (r == 0)
return -EDOM;
if (r < 0)
return r;
t->sent = p;
p = NULL;
return 0;
}
assert(t);
case DNS_PROTOCOL_DNS:
return t->server->resend_timeout;
case DNS_PROTOCOL_LLMNR:
case DNS_PROTOCOL_MDNS:
return t->scope->resend_timeout;
default:
assert_not_reached("Invalid DNS protocol.");
}
}
int dns_transaction_go(DnsTransaction *t) {
bool had_stream;
int r;
assert(t);
had_stream = !!t->stream;
log_debug("Excercising transaction on scope %s on %s/%s",
return 0;
}
/* If we already tried via a stream, then we don't
* retry on LLMNR. See RFC 4795, Section 2.7. */
return 0;
}
t->n_attempts++;
t->start_usec = ts;
t->answer_rcode = 0;
/* Check the zone, but obly if this transaction is not used
* for probing or verifying a zone item. */
if (set_isempty(t->zone_items)) {
if (r < 0)
return r;
if (r > 0) {
return 0;
}
}
/* Check the cache, but only if this transaction is not used
* for probing or verifying a zone item. */
if (set_isempty(t->zone_items)) {
/* Before trying the cache, let's make sure we figured out a
* server to use. Should this cause a change of server this
* might flush the cache. */
/* Let's then prune all outdated entries */
if (r < 0)
return r;
if (r > 0) {
if (t->answer_rcode == DNS_RCODE_SUCCESS)
else
return 0;
}
}
/* RFC 4795 Section 2.7 suggests all queries should be
* delayed by a random time from 0 to JITTER_INTERVAL. */
t->initial_jitter = true;
r = sd_event_add_time(
&t->timeout_event_source,
if (r < 0)
return r;
t->n_attempts = 0;
t->state = DNS_TRANSACTION_PENDING;
return 0;
}
/* Otherwise, we need to ask the network */
r = dns_transaction_make_packet(t);
if (r == -EDOM) {
/* Not the right request to make on this network?
* (i.e. an A request made on IPv6 or an AAAA request
* made on IPv4, on LLMNR or mDNS.) */
return 0;
}
if (r < 0)
return r;
/* RFC 4795, Section 2.4. says reverse lookups shall
* always be made via TCP on LLMNR */
r = dns_transaction_open_tcp(t);
} else {
/* Try via UDP, and if that fails due to large size try via TCP */
r = dns_transaction_emit(t);
if (r == -EMSGSIZE)
r = dns_transaction_open_tcp(t);
}
if (r == -ESRCH) {
/* No servers to send this to? */
return 0;
} else if (r < 0) {
return 0;
}
/* Couldn't send? Try immediately again, with a new server */
return dns_transaction_go(t);
}
r = sd_event_add_time(
&t->timeout_event_source,
ts + transaction_get_resend_timeout(t), 0,
if (r < 0)
return r;
t->state = DNS_TRANSACTION_PENDING;
return 1;
}
static const char* const dns_transaction_state_table[_DNS_TRANSACTION_STATE_MAX] = {
[DNS_TRANSACTION_NULL] = "null",
[DNS_TRANSACTION_PENDING] = "pending",
[DNS_TRANSACTION_FAILURE] = "failure",
[DNS_TRANSACTION_SUCCESS] = "success",
[DNS_TRANSACTION_NO_SERVERS] = "no-servers",
[DNS_TRANSACTION_TIMEOUT] = "timeout",
[DNS_TRANSACTION_ATTEMPTS_MAX_REACHED] = "attempts-max-reached",
[DNS_TRANSACTION_INVALID_REPLY] = "invalid-reply",
[DNS_TRANSACTION_RESOURCES] = "resources",
[DNS_TRANSACTION_ABORTED] = "aborted",
};
static const char* const dns_transaction_source_table[_DNS_TRANSACTION_SOURCE_MAX] = {
[DNS_TRANSACTION_NETWORK] = "network",
[DNS_TRANSACTION_CACHE] = "cache",
[DNS_TRANSACTION_ZONE] = "zone",
};