/***
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 "alloc-util.h"
#include "dns-domain.h"
#include "resolved-dns-packet.h"
#include "string-table.h"
#include "strv.h"
#include "unaligned.h"
#include "utf8.h"
#include "util.h"
DnsPacket *p;
size_t a;
if (mtu <= UDP_PACKET_HEADER_SIZE)
else
a = mtu - UDP_PACKET_HEADER_SIZE;
if (a < DNS_PACKET_HEADER_SIZE)
/* round up to next page size */
/* make sure we never allocate more than useful */
if (a > DNS_PACKET_SIZE_MAX)
a = DNS_PACKET_SIZE_MAX;
if (!p)
return -ENOMEM;
p->allocated = a;
p->n_ref = 1;
*ret = p;
return 0;
}
DnsPacketHeader *h;
assert(p);
h = DNS_PACKET_HEADER(p);
switch(p->protocol) {
case DNS_PROTOCOL_LLMNR:
0 /* opcode */,
0 /* c */,
0 /* tc */,
0 /* t */,
0 /* ra */,
0 /* ad */,
0 /* cd */,
0 /* rcode */));
break;
case DNS_PROTOCOL_MDNS:
0 /* opcode */,
0 /* aa */,
truncated /* tc */,
0 /* rd (ask for recursion) */,
0 /* ra */,
0 /* ad */,
0 /* cd */,
0 /* rcode */));
break;
default:
0 /* opcode */,
0 /* aa */,
0 /* tc */,
1 /* rd (ask for recursion) */,
0 /* ra */,
0 /* ad */,
dnssec_checking_disabled /* cd */,
0 /* rcode */));
}
}
int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled) {
DnsPacket *p;
int r;
if (r < 0)
return r;
/* Always set the TC bit to 0 initially.
* If there are multiple packets later, we'll update the bit shortly before sending.
*/
dns_packet_set_flags(p, dnssec_checking_disabled, false);
*ret = p;
return 0;
}
if (!p)
return NULL;
p->n_ref++;
return p;
}
char *s;
assert(p);
dns_answer_unref(p->answer);
while ((s = hashmap_steal_first_key(p->names)))
free(s);
hashmap_free(p->names);
if (!p->on_stack)
free(p);
}
if (!p)
return NULL;
dns_packet_unref(p->more);
if (p->n_ref == 1)
dns_packet_free(p);
else
p->n_ref--;
return NULL;
}
assert(p);
if (p->size < DNS_PACKET_HEADER_SIZE)
return -EBADMSG;
if (p->size > DNS_PACKET_SIZE_MAX)
return -EBADMSG;
return 1;
}
int r;
assert(p);
r = dns_packet_validate(p);
if (r < 0)
return r;
if (DNS_PACKET_QR(p) != 1)
return 0;
if (DNS_PACKET_OPCODE(p) != 0)
return -EBADMSG;
switch (p->protocol) {
case DNS_PROTOCOL_LLMNR:
/* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
if (DNS_PACKET_QDCOUNT(p) != 1)
return -EBADMSG;
break;
case DNS_PROTOCOL_MDNS:
/* RFC 6762, Section 18 */
if (DNS_PACKET_RCODE(p) != 0)
return -EBADMSG;
break;
default:
break;
}
return 1;
}
int r;
assert(p);
r = dns_packet_validate(p);
if (r < 0)
return r;
if (DNS_PACKET_QR(p) != 0)
return 0;
if (DNS_PACKET_OPCODE(p) != 0)
return -EBADMSG;
if (DNS_PACKET_TC(p))
return -EBADMSG;
switch (p->protocol) {
case DNS_PROTOCOL_LLMNR:
/* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
if (DNS_PACKET_QDCOUNT(p) != 1)
return -EBADMSG;
/* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
if (DNS_PACKET_ANCOUNT(p) > 0)
return -EBADMSG;
/* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
if (DNS_PACKET_NSCOUNT(p) > 0)
return -EBADMSG;
break;
case DNS_PROTOCOL_MDNS:
/* RFC 6762, Section 18 */
if (DNS_PACKET_AA(p) != 0 ||
DNS_PACKET_RD(p) != 0 ||
DNS_PACKET_RA(p) != 0 ||
DNS_PACKET_AD(p) != 0 ||
DNS_PACKET_CD(p) != 0 ||
DNS_PACKET_RCODE(p) != 0)
return -EBADMSG;
break;
default:
break;
}
return 1;
}
assert(p);
size_t a;
if (a > DNS_PACKET_SIZE_MAX)
a = DNS_PACKET_SIZE_MAX;
return -EMSGSIZE;
if (p->_data) {
void *d;
if (!d)
return -ENOMEM;
p->_data = d;
} else {
if (!p->_data)
return -ENOMEM;
}
p->allocated = a;
}
if (start)
if (ret)
return 0;
}
Iterator i;
char *s;
void *n;
assert(p);
return;
HASHMAP_FOREACH_KEY(n, s, p->names, i) {
if (PTR_TO_SIZE(n) < sz)
continue;
hashmap_remove(p->names, s);
free(s);
}
}
void *q;
int r;
assert(p);
r = dns_packet_extend(p, l, &q, start);
if (r < 0)
return r;
memcpy(q, d, l);
return 0;
}
void *d;
int r;
assert(p);
if (r < 0)
return r;
((uint8_t*) d)[0] = v;
return 0;
}
void *d;
int r;
assert(p);
if (r < 0)
return r;
unaligned_write_be16(d, v);
return 0;
}
void *d;
int r;
assert(p);
if (r < 0)
return r;
unaligned_write_be32(d, v);
return 0;
}
assert(p);
assert(s);
}
void *d;
int r;
assert(p);
if (size > 255)
return -E2BIG;
if (r < 0)
return r;
if (size > 0)
return 0;
}
int dns_packet_append_label(DnsPacket *p, const char *d, size_t l, bool canonical_candidate, size_t *start) {
uint8_t *w;
int r;
/* Append a label to a packet. Optionally, does this in DNSSEC
* canonical form, if this label is marked as a candidate for
* it, and the canonical form logic is enabled for the
* packet */
assert(p);
assert(d);
if (l > DNS_LABEL_MAX)
return -E2BIG;
if (r < 0)
return r;
*(w++) = (uint8_t) l;
if (p->canonical_form && canonical_candidate) {
size_t i;
/* Generate in canonical form, as defined by DNSSEC
* RFC 4034, Section 6.2, i.e. all lower-case. */
for (i = 0; i < l; i++)
w[i] = (uint8_t) ascii_tolower(d[i]);
} else
/* Otherwise, just copy the string unaltered. This is
* essential for DNS-SD, where the casing of labels
* matters and needs to be retained. */
memcpy(w, d, l);
return 0;
}
DnsPacket *p,
const char *name,
bool allow_compression,
bool canonical_candidate,
int r;
assert(p);
if (p->refuse_compression)
allow_compression = false;
saved_size = p->size;
while (!dns_name_is_root(name)) {
const char *z = name;
size_t n = 0;
if (allow_compression)
if (n > 0) {
if (n < 0x4000) {
if (r < 0)
goto fail;
goto done;
}
}
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (allow_compression) {
_cleanup_free_ char *s = NULL;
s = strdup(z);
if (!s) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
if (r < 0)
goto fail;
s = NULL;
}
}
r = dns_packet_append_uint8(p, 0, NULL);
if (r < 0)
return r;
done:
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
int r;
assert(p);
assert(k);
saved_size = p->size;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
static int dns_packet_append_type_window(DnsPacket *p, uint8_t window, uint8_t length, const uint8_t *types, size_t *start) {
int r;
assert(p);
saved_size = p->size;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
Iterator i;
unsigned n;
int r;
assert(p);
saved_size = p->size;
assert(n <= 0xffff);
if (r < 0)
goto fail;
}
window = n >> 8;
entry = n & 255;
}
if (r < 0)
goto fail;
}
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
/* Append the OPT pseudo-RR described in RFC6891 */
int r;
assert(p);
/* we must never advertise supported packet size smaller than the legacy max */
return -EBUSY;
saved_size = p->size;
/* empty name */
r = dns_packet_append_uint8(p, 0, NULL);
if (r < 0)
return r;
/* type */
if (r < 0)
goto fail;
/* maximum udp packet that can be received */
if (r < 0)
goto fail;
/* extended RCODE and VERSION */
r = dns_packet_append_uint16(p, 0, NULL);
if (r < 0)
goto fail;
/* flags: DNSSEC OK (DO), see RFC3225 */
if (r < 0)
goto fail;
/* RDLENGTH */
if (edns0_do) {
/* If DO is on, also append RFC6975 Algorithm data */
0, 5, /* OPTION_CODE: DAU */
0, 6, /* LIST_LENGTH */
0, 6, /* OPTION_CODE: DHU */
0, 3, /* LIST_LENGTH */
0, 7, /* OPTION_CODE: N3U */
0, 1, /* LIST_LENGTH */
};
if (r < 0)
goto fail;
} else
r = dns_packet_append_uint16(p, 0, NULL);
if (r < 0)
goto fail;
p->opt_start = saved_size;
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
assert(p);
return 0;
}
assert(DNS_PACKET_ARCOUNT(p) > 0);
return -EBUSY;
dns_packet_truncate(p, p->opt_start);
return 1;
}
int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start, size_t *rdata_start) {
int r;
assert(p);
saved_size = p->size;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
/* Initially we write 0 here */
r = dns_packet_append_uint16(p, 0, &rdlength_offset);
if (r < 0)
goto fail;
case DNS_TYPE_SRV:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
break;
case DNS_TYPE_HINFO:
if (r < 0)
goto fail;
break;
case DNS_TYPE_SPF: /* exactly the same as TXT */
case DNS_TYPE_TXT:
/* RFC 6763, section 6.1 suggests to generate
* single empty string for an empty array. */
if (r < 0)
goto fail;
} else {
DnsTxtItem *i;
if (r < 0)
goto fail;
}
}
r = 0;
break;
case DNS_TYPE_A:
break;
case DNS_TYPE_AAAA:
break;
case DNS_TYPE_SOA:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_MX:
if (r < 0)
goto fail;
break;
case DNS_TYPE_LOC:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_DS:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_SSHFP:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_DNSKEY:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_RRSIG:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_NSEC:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_NSEC3:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_TLSA:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_OPT:
case DNS_TYPE_OPENPGPKEY:
case _DNS_TYPE_INVALID: /* unparseable */
default:
break;
}
if (r < 0)
goto fail;
/* Let's calculate the actual data size and update the field */
if (rdlength > 0xFFFF) {
r = -ENOSPC;
goto fail;
}
p->size = rdlength_offset;
if (r < 0)
goto fail;
if (start)
*start = saved_size;
if (rdata_start)
*rdata_start = rds;
return 0;
fail:
return r;
}
assert(p);
return -EMSGSIZE;
if (ret)
if (start)
return 0;
}
assert(p);
}
const void *q;
int r;
assert(p);
assert(d);
if (r < 0)
return r;
return 0;
}
static int dns_packet_read_memdup(
const void *src;
int r;
assert(p);
if (r < 0)
return r;
if (size <= 0)
else {
void *copy;
if (!copy)
return -ENOMEM;
}
if (ret_size)
if (ret_start)
return 0;
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
return 0;
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
*ret = unaligned_read_be16(d);
return 0;
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
*ret = unaligned_read_be32(d);
return 0;
}
const void *d;
char *t;
uint8_t c;
int r;
assert(p);
saved_rindex = p->rindex;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
r = dns_packet_read(p, c, &d, NULL);
if (r < 0)
goto fail;
if (memchr(d, 0, c)) {
r = -EBADMSG;
goto fail;
}
t = strndup(d, c);
if (!t) {
r = -ENOMEM;
goto fail;
}
if (!utf8_is_valid(t)) {
free(t);
r = -EBADMSG;
goto fail;
}
*ret = t;
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
uint8_t c;
int r;
assert(p);
saved_rindex = p->rindex;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (size)
*size = c;
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
int dns_packet_read_name(
DnsPacket *p,
char **_ret,
bool allow_compression,
bool first = true;
int r;
assert(p);
if (p->refuse_compression)
allow_compression = false;
saved_rindex = p->rindex;
jump_barrier = p->rindex;
for (;;) {
uint8_t c, d;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
if (c == 0)
/* End of name */
break;
else if (c <= 63) {
const char *label;
/* Literal label */
if (r < 0)
goto fail;
r = -ENOMEM;
goto fail;
}
if (first)
first = false;
else
ret[n++] = '.';
if (r < 0)
goto fail;
n += r;
continue;
/* Pointer */
r = dns_packet_read_uint8(p, &d, NULL);
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (after_rindex == 0)
after_rindex = p->rindex;
/* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
jump_barrier = ptr;
} else {
r = -EBADMSG;
goto fail;
}
}
r = -ENOMEM;
goto fail;
}
ret[n] = 0;
if (after_rindex != 0)
p->rindex= after_rindex;
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
unsigned i;
bool found = false;
int r;
assert(p);
saved_rindex = p->rindex;
r = bitmap_ensure_allocated(types);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
return -EBADMSG;
if (r < 0)
goto fail;
for (i = 0; i < length; i++) {
if (!bitmap[i]) {
found = false;
bit += 8;
continue;
}
found = true;
while (bitmask) {
uint16_t n;
/* Ignore pseudo-types. see RFC4034 section 4.1.2 */
if (dns_type_is_pseudo(n))
continue;
r = bitmap_set(*types, n);
if (r < 0)
goto fail;
}
bit ++;
bitmask >>= 1;
}
}
if (!found)
return -EBADMSG;
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
int r;
saved_rindex = p->rindex;
if (r < 0)
goto fail;
/* don't read past end of current RR */
r = -EBADMSG;
goto fail;
}
}
r = -EBADMSG;
goto fail;
}
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
bool cache_flush = false;
int r;
assert(p);
saved_rindex = p->rindex;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (p->protocol == DNS_PROTOCOL_MDNS) {
/* See RFC6762, Section 10.2 */
class &= ~MDNS_RR_CACHE_FLUSH;
cache_flush = true;
}
}
if (!key) {
r = -ENOMEM;
goto fail;
}
if (ret_cache_flush)
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
return m <= 9 && e <= 9 && (m > 0 || e == 0);
}
int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, bool *ret_cache_flush, size_t *start) {
bool cache_flush;
int r;
assert(p);
saved_rindex = p->rindex;
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (!rr) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
/* RFC 2181, Section 8, suggests to
* treat a TTL with the MSB set as a zero TTL. */
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
case DNS_TYPE_SRV:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
break;
case DNS_TYPE_HINFO:
if (r < 0)
goto fail;
break;
case DNS_TYPE_SPF: /* exactly the same as TXT */
case DNS_TYPE_TXT:
if (rdlength <= 0) {
DnsTxtItem *i;
/* RFC 6763, section 6.1 suggests to treat
* empty TXT RRs as equivalent to a TXT record
* with a single empty string. */
if (!i)
return -ENOMEM;
} else {
DnsTxtItem *i;
const void *data;
if (r < 0)
return r;
if (!i)
return -ENOMEM;
last = i;
}
}
r = 0;
break;
case DNS_TYPE_A:
break;
case DNS_TYPE_AAAA:
break;
case DNS_TYPE_SOA:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
case DNS_TYPE_MX:
if (r < 0)
goto fail;
break;
case DNS_TYPE_LOC: {
uint8_t t;
r = dns_packet_read_uint8(p, &t, &pos);
if (r < 0)
goto fail;
if (t == 0) {
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
break;
} else {
dns_packet_rewind(p, pos);
rr->unparseable = true;
goto unparseable;
}
}
case DNS_TYPE_DS:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
NULL);
if (r < 0)
goto fail;
/* the accepted size depends on the algorithm, but for now
just ensure that the value is greater than zero */
r = -EBADMSG;
goto fail;
}
break;
case DNS_TYPE_SSHFP:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
NULL);
/* the accepted size depends on the algorithm, but for now
just ensure that the value is greater than zero */
r = -EBADMSG;
goto fail;
}
break;
case DNS_TYPE_DNSKEY:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
NULL);
/* the accepted size depends on the algorithm, but for now
just ensure that the value is greater than zero */
r = -EBADMSG;
goto fail;
}
break;
case DNS_TYPE_RRSIG:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
NULL);
/* the accepted size depends on the algorithm, but for now
just ensure that the value is greater than zero */
r = -EBADMSG;
goto fail;
}
break;
case DNS_TYPE_NSEC: {
/*
* RFC6762, section 18.14 explictly states mDNS should use name compression.
* This contradicts RFC3845, section 2.1.1
*/
if (r < 0)
goto fail;
if (r < 0)
goto fail;
/* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
* is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
* without the NSEC bit set. */
break;
}
case DNS_TYPE_NSEC3: {
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
/* this may be zero */
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (size <= 0) {
r = -EBADMSG;
goto fail;
}
r = dns_packet_read_memdup(p, size, &rr->nsec3.next_hashed_name, &rr->nsec3.next_hashed_name_size, NULL);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
/* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
break;
}
case DNS_TYPE_TLSA:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
NULL);
/* the accepted size depends on the algorithm, but for now
just ensure that the value is greater than zero */
r = -EBADMSG;
goto fail;
}
break;
case DNS_TYPE_OPT: /* we only care about the header of OPT for now. */
case DNS_TYPE_OPENPGPKEY:
default:
if (r < 0)
goto fail;
break;
}
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (ret_cache_flush)
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
const uint8_t* p;
bool found_dau_dhu_n3u = false;
size_t l;
/* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
* a reply). */
/* Check that the version is 0 */
return false;
while (l > 0) {
/* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
if (l < 4U)
return false;
if (l < option_length + 4U)
return false;
/* RFC 6975 DAU, DHU or N3U fields found. */
found_dau_dhu_n3u = true;
p += option_length + 4U;
l -= option_length + 4U;
}
return true;
}
unsigned n, i;
int r;
if (p->extracted)
return 0;
saved_rindex = p->rindex;
n = DNS_PACKET_QDCOUNT(p);
if (n > 0) {
question = dns_question_new(n);
if (!question) {
r = -ENOMEM;
goto finish;
}
for (i = 0; i < n; i++) {
bool cache_flush;
if (r < 0)
goto finish;
if (cache_flush) {
r = -EBADMSG;
goto finish;
}
r = -EBADMSG;
goto finish;
}
if (r < 0)
goto finish;
}
}
n = DNS_PACKET_RRCOUNT(p);
if (n > 0) {
bool bad_opt = false;
answer = dns_answer_new(n);
if (!answer) {
r = -ENOMEM;
goto finish;
}
for (i = 0; i < n; i++) {
bool cache_flush;
if (r < 0)
goto finish;
/* Try to reduce memory usage a bit */
if (previous)
bool has_rfc6975;
/* Multiple OPT RRs? if so, let's ignore all, because there's something wrong
* with the server, and if one is valid we wouldn't know which one. */
log_debug("Multiple OPT RRs detected, ignoring all.");
bad_opt = true;
continue;
}
/* If the OPT RR is not owned by the root domain, then it is bad, let's ignore
* it. */
log_debug("OPT RR is not owned by root domain, ignoring.");
bad_opt = true;
continue;
}
if (i < DNS_PACKET_ANCOUNT(p) + DNS_PACKET_NSCOUNT(p)) {
/* OPT RR is in the wrong section? Some Belkin routers do this. This is a hint
* the EDNS implementation is borked, like the Belkin one is, hence ignore
* it. */
log_debug("OPT RR in wrong section, ignoring.");
bad_opt = true;
continue;
}
log_debug("Malformed OPT RR, ignoring.");
bad_opt = true;
continue;
}
if (has_rfc6975) {
/* If the OPT RR contains RFC6975 algorithm data, then this is indication that
* the server just copied the OPT it got from us (which contained that data)
* back into the reply. If so, then it doesn't properly support EDNS, as
* RFC6975 makes it very clear that the algorithm data should only be contained
* in questions, never in replies. Crappy Belkin routers copy the OPT data for
* example, hence let's detect this so that we downgrade early. */
log_debug("OPT RR contained RFC6975 data, ignoring.");
bad_opt = true;
continue;
}
} else {
/* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
* cached. Hence mark only those RRs as cacheable by default, but not the ones from the
* Additional or Authority sections. */
(i < DNS_PACKET_ANCOUNT(p) ? DNS_ANSWER_CACHEABLE : 0) |
if (r < 0)
goto finish;
}
/* Remember this RR, so that we potentically can merge it's ->key object with the next RR. Note
* that we only do this if we actually decided to keep the RR around. */
}
if (bad_opt)
}
p->extracted = true;
r = 0;
p->rindex = saved_rindex;
return r;
}
int r;
assert(p);
/* Checks if the specified packet is a reply for the specified
* key and the specified key is the only one in the question
* section. */
if (DNS_PACKET_QR(p) != 1)
return 0;
/* Let's unpack the packet, if that hasn't happened yet. */
r = dns_packet_extract(p);
if (r < 0)
return r;
return 0;
}
[DNS_RCODE_SUCCESS] = "SUCCESS",
[DNS_RCODE_FORMERR] = "FORMERR",
[DNS_RCODE_SERVFAIL] = "SERVFAIL",
[DNS_RCODE_NXDOMAIN] = "NXDOMAIN",
[DNS_RCODE_NOTIMP] = "NOTIMP",
[DNS_RCODE_REFUSED] = "REFUSED",
[DNS_RCODE_YXDOMAIN] = "YXDOMAIN",
[DNS_RCODE_YXRRSET] = "YRRSET",
[DNS_RCODE_NXRRSET] = "NXRRSET",
[DNS_RCODE_NOTAUTH] = "NOTAUTH",
[DNS_RCODE_NOTZONE] = "NOTZONE",
[DNS_RCODE_BADVERS] = "BADVERS",
[DNS_RCODE_BADKEY] = "BADKEY",
[DNS_RCODE_BADTIME] = "BADTIME",
[DNS_RCODE_BADMODE] = "BADMODE",
[DNS_RCODE_BADNAME] = "BADNAME",
[DNS_RCODE_BADALG] = "BADALG",
[DNS_RCODE_BADTRUNC] = "BADTRUNC",
};
[DNS_PROTOCOL_DNS] = "dns",
[DNS_PROTOCOL_MDNS] = "mdns",
[DNS_PROTOCOL_LLMNR] = "llmnr",
};