resolved-dns-packet.c revision 50f1e641a93cacfc693b0c3d300bee5df0c8c460
/*-*- 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 "utf8.h"
#include "util.h"
#include "strv.h"
#include "unaligned.h"
#include "dns-domain.h"
#include "resolved-dns-packet.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;
}
DnsPacket *p;
DnsPacketHeader *h;
int r;
if (r < 0)
return r;
h = DNS_PACKET_HEADER(p);
if (protocol == DNS_PROTOCOL_LLMNR)
0 /* opcode */,
0 /* c */,
0 /* tc */,
0 /* t */,
0 /* ra */,
0 /* ad */,
0 /* cd */,
0 /* rcode */));
else
0 /* opcode */,
0 /* aa */,
0 /* tc */,
1 /* rd (ask for recursion) */,
0 /* ra */,
0 /* ad */,
0 /* cd */,
0 /* rcode */));
*ret = p;
return 0;
}
if (!p)
return NULL;
p->n_ref++;
return p;
}
static void dns_packet_free(DnsPacket *p) {
char *s;
assert(p);
dns_answer_unref(p->answer);
while ((s = hashmap_steal_first_key(p->names)))
free(s);
hashmap_free(p->names);
free(p);
}
if (!p)
return NULL;
if (p->n_ref == 1)
dns_packet_free(p);
else
p->n_ref--;
return NULL;
}
int dns_packet_validate(DnsPacket *p) {
assert(p);
if (p->size < DNS_PACKET_HEADER_SIZE)
return -EBADMSG;
if (p->size > DNS_PACKET_SIZE_MAX)
return -EBADMSG;
return 1;
}
int dns_packet_validate_reply(DnsPacket *p) {
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;
default:
break;
}
return 1;
}
int dns_packet_validate_query(DnsPacket *p) {
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;
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(s, n, 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;
}
void *d;
size_t l;
int r;
assert(p);
assert(s);
l = strlen(s);
if (l > 255)
return -E2BIG;
if (r < 0)
return r;
return 0;
}
void *w;
int r;
assert(p);
assert(d);
if (l > DNS_LABEL_MAX)
return -E2BIG;
if (r < 0)
return r;
return 0;
}
int r;
assert(p);
saved_size = p->size;
while (*name) {
_cleanup_free_ char *s = NULL;
char label[DNS_LABEL_MAX];
size_t n = 0;
int k;
if (allow_compression)
if (n > 0) {
if (n < 0x4000) {
if (r < 0)
goto fail;
goto done;
}
}
if (!s) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
if (p->protocol == DNS_PROTOCOL_DNS)
else
if (k < 0) {
r = k;
goto fail;
}
if (k > 0)
r = k;
r = dns_packet_append_label(p, label, r, &n);
if (r < 0)
goto fail;
if (allow_compression) {
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, uint8_t *types, size_t *start) {
int r;
assert(p);
if (length == 0)
return 0;
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;
}
unsigned n;
int r;
assert(p);
saved_size = p->size;
BITMAP_FOREACH(n, types) {
assert(n <= 0xffff);
if ((n << 8) != window) {
if (r < 0)
goto fail;
if (len > 0) {
len = 0;
}
}
window = n << 8;
len ++;
entry = n & 255;
}
if (r < 0)
goto fail;
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
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: {
char **s;
/* RFC 6763, section 6.1 suggests to generate
* single empty string for an empty array. */
if (r < 0)
goto fail;
} else {
r = dns_packet_append_string(p, *s, NULL);
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_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;
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;
}
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;
}
bool first = true;
int r;
assert(p);
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) {
_cleanup_free_ char *t = NULL;
const char *label;
/* Literal label */
if (r < 0)
goto fail;
r = dns_label_escape(label, c, &t);
if (r < 0)
goto fail;
r = -ENOMEM;
goto fail;
}
if (!first)
ret[n++] = '.';
else
first = false;
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;
continue;
}
found = true;
while (bitmask) {
uint16_t n;
/* XXX: ignore pseudo-types? see RFC4034 section 4.1.2 */
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;
assert(p);
saved_rindex = p->rindex;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (!key) {
r = -ENOMEM;
goto fail;
}
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
int r;
const void *d;
void *d2;
if (r < 0)
return r;
if (!d2)
return -ENOMEM;
return 0;
}
return m <= 9 && e <= 9 && (m > 0 || e == 0);
}
return -EBADMSG;
return 0;
}
const void *d;
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;
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) {
/* RFC 6763, section 6.1 suggests to treat
* empty TXT RRs as equivalent to a TXT record
* with a single empty string. */
if (r < 0)
goto fail;
} else {
char *s;
r = dns_packet_read_string(p, &s, NULL);
if (r < 0)
goto fail;
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: {
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;
break;
case DNS_TYPE_SSHFP:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
NULL);
break;
case DNS_TYPE_DNSKEY: {
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
/* protocol is required to be always 3 */
if (proto != 3) {
r = -EBADMSG;
goto fail;
}
if (r < 0)
goto fail;
NULL);
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);
break;
case DNS_TYPE_NSEC:
if (r < 0)
goto fail;
if (r < 0)
goto fail;
}
break;
default:
if (r < 0)
goto fail;
r = -ENOMEM;
goto fail;
}
break;
}
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
int dns_packet_extract(DnsPacket *p) {
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++) {
if (r < 0)
goto finish;
if (r < 0)
goto finish;
}
}
n = DNS_PACKET_RRCOUNT(p);
if (n > 0) {
answer = dns_answer_new(n);
if (!answer) {
r = -ENOMEM;
goto finish;
}
for (i = 0; i < n; i++) {
if (r < 0)
goto finish;
if (r < 0)
goto finish;
}
}
p->extracted = true;
r = 0;
p->rindex = saved_rindex;
return r;
}
static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = {
[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",
};
static const char* const dns_protocol_table[_DNS_PROTOCOL_MAX] = {
[DNS_PROTOCOL_DNS] = "dns",
[DNS_PROTOCOL_MDNS] = "mdns",
[DNS_PROTOCOL_LLMNR] = "llmnr",
};
static const char* const dnssec_algorithm_table[_DNSSEC_ALGORITHM_MAX_DEFINED] = {
[DNSSEC_ALGORITHM_RSAMD5] = "RSAMD5",
[DNSSEC_ALGORITHM_DH] = "DH",
[DNSSEC_ALGORITHM_DSA] = "DSA",
[DNSSEC_ALGORITHM_ECC] = "ECC",
[DNSSEC_ALGORITHM_RSASHA1] = "RSASHA1",
[DNSSEC_ALGORITHM_INDIRECT] = "INDIRECT",
[DNSSEC_ALGORITHM_PRIVATEDNS] = "PRIVATEDNS",
[DNSSEC_ALGORITHM_PRIVATEOID] = "PRIVATEOID",
};