resolved-dns-transaction.c revision 0eb99d0a6a7d28a16e739b3a0e4900b9e4dc76f9
/*-*- 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 "resolved-llmnr.h"
#include "resolved-dns-transaction.h"
#include "random-util.h"
DnsQuery *q;
DnsZoneItem *i;
if (!t)
return NULL;
dns_packet_unref(t->sent);
dns_packet_unref(t->received);
dns_answer_unref(t->cached);
safe_close(t->dns_fd);
dns_server_unref(t->server);
dns_stream_free(t->stream);
if (t->scope) {
if (t->id != 0)
}
while ((q = set_steal_first(t->queries)))
set_remove(q->transactions, t);
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);
assert(q);
if (r < 0)
return r;
if (!t)
return -ENOMEM;
t->dns_fd = -1;
t->question = dns_question_ref(q);
do
while (t->id == 0 ||
if (r < 0) {
t->id = 0;
return r;
}
t->scope = s;
if (ret)
*ret = t;
t = NULL;
return 0;
}
static void dns_transaction_stop(DnsTransaction *t) {
assert(t);
}
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--;
}
DnsQuery *q;
DnsZoneItem *z;
Iterator i;
assert(t);
return;
/* 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>",
/* 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(q, t->queries, i)
dns_query_ready(q);
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;
/* When we already received a query 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;
}
} else
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->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;
}
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. */
/* 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;
}
}
/* For DNS we are fine with accepting packets on any
* interface, but the source IP address must be the
* one of the DNS server we queried */
return;
return;
if (p->sender_port != 53)
return;
}
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;
}
}
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;
}
/* 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 */
dns_cache_put(&t->scope->cache, p->question, 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(s);
assert(t);
/* Timeout reached? Try again, with a new server */
r = dns_transaction_go(t);
if (r < 0)
return 0;
}
static int dns_transaction_make_packet(DnsTransaction *t) {
unsigned n, added = 0;
int r;
assert(t);
if (t->sent)
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
if (r == 0)
continue;
if (r < 0)
return r;
added++;
}
if (added <= 0)
return -EDOM;
t->sent = p;
p = NULL;
return 0;
}
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->cached_rcode = 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) {
log_debug("Cache hit!");
if (t->cached_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;
}
log_debug("Cache miss!");
/* 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 */
if (r >= 0)
else 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,
if (r < 0)
return r;
t->state = DNS_TRANSACTION_PENDING;
return 1;
}
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;
}
int transaction_dns_fd(DnsTransaction *t) {
const int one = 1;
int r;
assert(t);
if (t->dns_fd >= 0)
return t->dns_fd;
if (t->dns_fd < 0)
return -errno;
case AF_INET:
break;
case AF_INET6:
break;
default:
return -EAFNOSUPPORT;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(t->scope->manager->event, &t->dns_event_source, t->dns_fd, EPOLLIN, on_dns_packet, t);
if (r < 0)
goto fail;
return t->dns_fd;
fail:
return r;
}
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",
};