resolved-dns-packet.c revision 9de3e3294065e8697ff10130b53f274319cdcf6f
/*-*- 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 "resolved-dns-domain.h"
#include "resolved-dns-packet.h"
DnsPacket *p;
size_t a;
if (mtu <= 0)
else
a = mtu;
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;
/* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
if (p->protocol == DNS_PROTOCOL_LLMNR &&
DNS_PACKET_QDCOUNT(p) != 1)
return -EBADMSG;
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;
/* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
if (p->protocol == DNS_PROTOCOL_LLMNR &&
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;
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;
return 0;
}
void *d;
int r;
assert(p);
if (r < 0)
return r;
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;
if (n > 0) {
if (n < 0x4000) {
if (r < 0)
goto fail;
goto done;
}
}
if (!s) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
r = dns_packet_append_label(p, label, r, &n);
if (r < 0)
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;
}
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_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
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;
r = dns_packet_append_string(p, *s, NULL);
if (r < 0)
goto fail;
}
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_SRV:
case DNS_TYPE_DNAME:
case DNS_TYPE_SSHFP:
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;
return 0;
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
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;
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;
} else if ((c & 0xc0) == 0xc0) {
/* 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;
} else
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;
}
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;
}
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_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
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;
r = dns_packet_read_string(p, &s, NULL);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
};
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_SRV:
case DNS_TYPE_DNAME:
case DNS_TYPE_SSHFP:
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;
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;
}
}
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",
};