2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "dns-domain.h"
22 #include "resolved-dns-packet.h"
23 #include "string-table.h"
25 #include "unaligned.h"
29 #define EDNS0_OPT_DO (1<<15)
31 typedef struct DnsPacketRewinder
{
36 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
38 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
41 #define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
42 #define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
44 int dns_packet_new(DnsPacket
**ret
, DnsProtocol protocol
, size_t mtu
) {
50 if (mtu
<= UDP_PACKET_HEADER_SIZE
)
51 a
= DNS_PACKET_SIZE_START
;
53 a
= mtu
- UDP_PACKET_HEADER_SIZE
;
55 if (a
< DNS_PACKET_HEADER_SIZE
)
56 a
= DNS_PACKET_HEADER_SIZE
;
58 /* round up to next page size */
59 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
61 /* make sure we never allocate more than useful */
62 if (a
> DNS_PACKET_SIZE_MAX
)
63 a
= DNS_PACKET_SIZE_MAX
;
65 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
69 p
->size
= p
->rindex
= DNS_PACKET_HEADER_SIZE
;
71 p
->protocol
= protocol
;
72 p
->opt_start
= p
->opt_size
= (size_t) -1;
80 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
86 h
= DNS_PACKET_HEADER(p
);
89 case DNS_PROTOCOL_LLMNR
:
92 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
103 case DNS_PROTOCOL_MDNS
:
104 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
108 0 /* rd (ask for recursion) */,
118 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
122 1 /* rd (ask for recursion) */,
125 dnssec_checking_disabled
/* cd */,
130 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t mtu
, bool dnssec_checking_disabled
) {
136 r
= dns_packet_new(&p
, protocol
, mtu
);
140 /* Always set the TC bit to 0 initially.
141 * If there are multiple packets later, we'll update the bit shortly before sending.
143 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
149 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
154 assert(!p
->on_stack
);
156 assert(p
->n_ref
> 0);
161 static void dns_packet_free(DnsPacket
*p
) {
166 dns_question_unref(p
->question
);
167 dns_answer_unref(p
->answer
);
168 dns_resource_record_unref(p
->opt
);
170 while ((s
= hashmap_steal_first_key(p
->names
)))
172 hashmap_free(p
->names
);
180 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
184 assert(p
->n_ref
> 0);
186 dns_packet_unref(p
->more
);
196 int dns_packet_validate(DnsPacket
*p
) {
199 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
202 if (p
->size
> DNS_PACKET_SIZE_MAX
)
208 int dns_packet_validate_reply(DnsPacket
*p
) {
213 r
= dns_packet_validate(p
);
217 if (DNS_PACKET_QR(p
) != 1)
220 if (DNS_PACKET_OPCODE(p
) != 0)
223 switch (p
->protocol
) {
225 case DNS_PROTOCOL_LLMNR
:
226 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
227 if (DNS_PACKET_QDCOUNT(p
) != 1)
232 case DNS_PROTOCOL_MDNS
:
233 /* RFC 6762, Section 18 */
234 if (DNS_PACKET_RCODE(p
) != 0)
246 int dns_packet_validate_query(DnsPacket
*p
) {
251 r
= dns_packet_validate(p
);
255 if (DNS_PACKET_QR(p
) != 0)
258 if (DNS_PACKET_OPCODE(p
) != 0)
261 if (DNS_PACKET_TC(p
))
264 switch (p
->protocol
) {
266 case DNS_PROTOCOL_LLMNR
:
267 case DNS_PROTOCOL_DNS
:
268 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
269 if (DNS_PACKET_QDCOUNT(p
) != 1)
272 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
273 if (DNS_PACKET_ANCOUNT(p
) > 0)
276 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
277 if (DNS_PACKET_NSCOUNT(p
) > 0)
282 case DNS_PROTOCOL_MDNS
:
283 /* RFC 6762, Section 18 */
284 if (DNS_PACKET_AA(p
) != 0 ||
285 DNS_PACKET_RD(p
) != 0 ||
286 DNS_PACKET_RA(p
) != 0 ||
287 DNS_PACKET_AD(p
) != 0 ||
288 DNS_PACKET_CD(p
) != 0 ||
289 DNS_PACKET_RCODE(p
) != 0)
301 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
304 if (p
->size
+ add
> p
->allocated
) {
307 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
308 if (a
> DNS_PACKET_SIZE_MAX
)
309 a
= DNS_PACKET_SIZE_MAX
;
311 if (p
->size
+ add
> a
)
317 d
= realloc(p
->_data
, a
);
323 p
->_data
= malloc(a
);
327 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
328 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
338 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
344 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
354 HASHMAP_FOREACH_KEY(n
, s
, p
->names
, i
) {
356 if (PTR_TO_SIZE(n
) < sz
)
359 hashmap_remove(p
->names
, s
);
366 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
372 r
= dns_packet_extend(p
, l
, &q
, start
);
380 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
386 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
390 ((uint8_t*) d
)[0] = v
;
395 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
401 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
405 unaligned_write_be16(d
, v
);
410 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
416 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
420 unaligned_write_be32(d
, v
);
425 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
429 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
432 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
437 assert(s
|| size
== 0);
442 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
446 ((uint8_t*) d
)[0] = (uint8_t) size
;
448 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
453 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
457 /* Append a label to a packet. Optionally, does this in DNSSEC
458 * canonical form, if this label is marked as a candidate for
459 * it, and the canonical form logic is enabled for the
465 if (l
> DNS_LABEL_MAX
)
468 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
472 *(w
++) = (uint8_t) l
;
474 if (p
->canonical_form
&& canonical_candidate
) {
477 /* Generate in canonical form, as defined by DNSSEC
478 * RFC 4034, Section 6.2, i.e. all lower-case. */
480 for (i
= 0; i
< l
; i
++)
481 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
483 /* Otherwise, just copy the string unaltered. This is
484 * essential for DNS-SD, where the casing of labels
485 * matters and needs to be retained. */
491 int dns_packet_append_name(
494 bool allow_compression
,
495 bool canonical_candidate
,
504 if (p
->refuse_compression
)
505 allow_compression
= false;
507 saved_size
= p
->size
;
509 while (!dns_name_is_root(name
)) {
510 const char *z
= name
;
511 char label
[DNS_LABEL_MAX
];
514 if (allow_compression
)
515 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
520 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
528 r
= dns_label_unescape(&name
, label
, sizeof(label
));
532 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
536 if (allow_compression
) {
537 _cleanup_free_
char *s
= NULL
;
545 r
= hashmap_ensure_allocated(&p
->names
, &dns_name_hash_ops
);
549 r
= hashmap_put(p
->names
, s
, SIZE_TO_PTR(n
));
557 r
= dns_packet_append_uint8(p
, 0, NULL
);
568 dns_packet_truncate(p
, saved_size
);
572 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
580 saved_size
= p
->size
;
582 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
586 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
590 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH
: k
->class;
591 r
= dns_packet_append_uint16(p
, class, NULL
);
601 dns_packet_truncate(p
, saved_size
);
605 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
613 saved_size
= p
->size
;
615 r
= dns_packet_append_uint8(p
, window
, NULL
);
619 r
= dns_packet_append_uint8(p
, length
, NULL
);
623 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
632 dns_packet_truncate(p
, saved_size
);
636 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
640 uint8_t bitmaps
[32] = {};
647 saved_size
= p
->size
;
649 BITMAP_FOREACH(n
, types
, i
) {
652 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
653 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
663 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
666 if (bitmaps
[entry
/ 8] != 0) {
667 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
677 dns_packet_truncate(p
, saved_size
);
681 /* Append the OPT pseudo-RR described in RFC6891 */
682 int dns_packet_append_opt(DnsPacket
*p
, uint16_t max_udp_size
, bool edns0_do
, int rcode
, size_t *start
) {
687 /* we must never advertise supported packet size smaller than the legacy max */
688 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
690 assert(rcode
<= _DNS_RCODE_MAX
);
692 if (p
->opt_start
!= (size_t) -1)
695 assert(p
->opt_size
== (size_t) -1);
697 saved_size
= p
->size
;
700 r
= dns_packet_append_uint8(p
, 0, NULL
);
705 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
709 /* class: maximum udp packet that can be received */
710 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
714 /* extended RCODE and VERSION */
715 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
719 /* flags: DNSSEC OK (DO), see RFC3225 */
720 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
725 if (edns0_do
&& !DNS_PACKET_QR(p
)) {
726 /* If DO is on and this is not a reply, also append RFC6975 Algorithm data */
728 static const uint8_t rfc6975
[] = {
730 0, 5, /* OPTION_CODE: DAU */
731 0, 6, /* LIST_LENGTH */
732 DNSSEC_ALGORITHM_RSASHA1
,
733 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
734 DNSSEC_ALGORITHM_RSASHA256
,
735 DNSSEC_ALGORITHM_RSASHA512
,
736 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
737 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
739 0, 6, /* OPTION_CODE: DHU */
740 0, 3, /* LIST_LENGTH */
742 DNSSEC_DIGEST_SHA256
,
743 DNSSEC_DIGEST_SHA384
,
745 0, 7, /* OPTION_CODE: N3U */
746 0, 1, /* LIST_LENGTH */
747 NSEC3_ALGORITHM_SHA1
,
750 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
);
754 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
);
756 r
= dns_packet_append_uint16(p
, 0, NULL
);
760 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
762 p
->opt_start
= saved_size
;
763 p
->opt_size
= p
->size
- saved_size
;
771 dns_packet_truncate(p
, saved_size
);
775 int dns_packet_truncate_opt(DnsPacket
*p
) {
778 if (p
->opt_start
== (size_t) -1) {
779 assert(p
->opt_size
== (size_t) -1);
783 assert(p
->opt_size
!= (size_t) -1);
784 assert(DNS_PACKET_ARCOUNT(p
) > 0);
786 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
789 dns_packet_truncate(p
, p
->opt_start
);
790 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
791 p
->opt_start
= p
->opt_size
= (size_t) -1;
796 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
798 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
804 saved_size
= p
->size
;
806 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
810 r
= dns_packet_append_uint32(p
, rr
->ttl
, NULL
);
814 /* Initially we write 0 here */
815 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
819 rds
= p
->size
- saved_size
;
821 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
824 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
828 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
832 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
836 r
= dns_packet_append_name(p
, rr
->srv
.name
, true, false, NULL
);
843 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, false, NULL
);
847 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
851 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
854 case DNS_TYPE_SPF
: /* exactly the same as TXT */
857 if (!rr
->txt
.items
) {
858 /* RFC 6763, section 6.1 suggests to generate
859 * single empty string for an empty array. */
861 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
867 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
868 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
878 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
882 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
886 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, false, NULL
);
890 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, false, NULL
);
894 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
898 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
902 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
906 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
910 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
914 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
918 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, false, NULL
);
922 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
926 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
930 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
934 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
938 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
942 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
946 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
950 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
954 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
958 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
962 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
966 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
970 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
974 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
977 case DNS_TYPE_DNSKEY
:
978 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
982 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
986 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
990 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
994 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
998 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1002 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1006 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1010 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1014 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1018 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1022 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1026 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1030 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1034 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1040 case DNS_TYPE_NSEC3
:
1041 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1045 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1049 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1053 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1057 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1061 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1065 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1069 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1076 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1080 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1084 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1088 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1092 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1096 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1100 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1104 case DNS_TYPE_OPENPGPKEY
:
1105 case _DNS_TYPE_INVALID
: /* unparseable */
1108 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1114 /* Let's calculate the actual data size and update the field */
1115 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1116 if (rdlength
> 0xFFFF) {
1122 p
->size
= rdlength_offset
;
1123 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1129 *start
= saved_size
;
1137 dns_packet_truncate(p
, saved_size
);
1141 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1142 DnsResourceKey
*key
;
1147 DNS_QUESTION_FOREACH(key
, q
) {
1148 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1156 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
) {
1157 DnsResourceRecord
*rr
;
1158 DnsAnswerFlags flags
;
1163 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1164 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1172 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1175 if (p
->rindex
+ sz
> p
->size
)
1179 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1188 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1190 assert(idx
<= p
->size
);
1191 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1196 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1203 r
= dns_packet_read(p
, sz
, &q
, start
);
1211 static int dns_packet_read_memdup(
1212 DnsPacket
*p
, size_t size
,
1213 void **ret
, size_t *ret_size
,
1214 size_t *ret_start
) {
1223 r
= dns_packet_read(p
, size
, &src
, &start
);
1232 copy
= memdup(src
, size
);
1247 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1253 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1257 *ret
= ((uint8_t*) d
)[0];
1261 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1267 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1271 *ret
= unaligned_read_be16(d
);
1276 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1282 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1286 *ret
= unaligned_read_be32(d
);
1291 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1292 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1299 INIT_REWINDER(rewinder
, p
);
1301 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1305 r
= dns_packet_read(p
, c
, &d
, NULL
);
1309 if (memchr(d
, 0, c
))
1316 if (!utf8_is_valid(t
)) {
1324 *start
= rewinder
.saved_rindex
;
1325 CANCEL_REWINDER(rewinder
);
1330 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1331 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1336 INIT_REWINDER(rewinder
, p
);
1338 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1342 r
= dns_packet_read(p
, c
, ret
, NULL
);
1349 *start
= rewinder
.saved_rindex
;
1350 CANCEL_REWINDER(rewinder
);
1355 int dns_packet_read_name(
1358 bool allow_compression
,
1361 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1362 size_t after_rindex
= 0, jump_barrier
;
1363 _cleanup_free_
char *ret
= NULL
;
1364 size_t n
= 0, allocated
= 0;
1370 INIT_REWINDER(rewinder
, p
);
1371 jump_barrier
= p
->rindex
;
1373 if (p
->refuse_compression
)
1374 allow_compression
= false;
1379 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1390 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1394 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1402 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
1408 } else if (allow_compression
&& (c
& 0xc0) == 0xc0) {
1412 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1416 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1417 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1420 if (after_rindex
== 0)
1421 after_rindex
= p
->rindex
;
1423 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1430 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
1435 if (after_rindex
!= 0)
1436 p
->rindex
= after_rindex
;
1442 *start
= rewinder
.saved_rindex
;
1443 CANCEL_REWINDER(rewinder
);
1448 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1451 const uint8_t *bitmap
;
1455 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1460 INIT_REWINDER(rewinder
, p
);
1462 r
= bitmap_ensure_allocated(types
);
1466 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1470 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1474 if (length
== 0 || length
> 32)
1477 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1481 for (i
= 0; i
< length
; i
++) {
1482 uint8_t bitmask
= 1 << 7;
1493 if (bitmap
[i
] & bitmask
) {
1496 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1498 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1499 if (dns_type_is_pseudo(n
))
1502 r
= bitmap_set(*types
, n
);
1516 *start
= rewinder
.saved_rindex
;
1517 CANCEL_REWINDER(rewinder
);
1522 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1523 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1526 INIT_REWINDER(rewinder
, p
);
1528 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1529 r
= dns_packet_read_type_window(p
, types
, NULL
);
1533 /* don't read past end of current RR */
1534 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1538 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1542 *start
= rewinder
.saved_rindex
;
1543 CANCEL_REWINDER(rewinder
);
1548 int dns_packet_read_key(DnsPacket
*p
, DnsResourceKey
**ret
, bool *ret_cache_flush
, size_t *start
) {
1549 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1550 _cleanup_free_
char *name
= NULL
;
1551 bool cache_flush
= false;
1552 uint16_t class, type
;
1553 DnsResourceKey
*key
;
1558 INIT_REWINDER(rewinder
, p
);
1560 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1564 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1568 r
= dns_packet_read_uint16(p
, &class, NULL
);
1572 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1573 /* See RFC6762, Section 10.2 */
1575 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1576 class &= ~MDNS_RR_CACHE_FLUSH
;
1581 key
= dns_resource_key_new_consume(class, type
, name
);
1588 if (ret_cache_flush
)
1589 *ret_cache_flush
= cache_flush
;
1591 *start
= rewinder
.saved_rindex
;
1592 CANCEL_REWINDER(rewinder
);
1597 static bool loc_size_ok(uint8_t size
) {
1598 uint8_t m
= size
>> 4, e
= size
& 0xF;
1600 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1603 int dns_packet_read_rr(DnsPacket
*p
, DnsResourceRecord
**ret
, bool *ret_cache_flush
, size_t *start
) {
1604 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1605 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1606 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1615 INIT_REWINDER(rewinder
, p
);
1617 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1621 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1624 rr
= dns_resource_record_new(key
);
1628 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1632 /* RFC 2181, Section 8, suggests to
1633 * treat a TTL with the MSB set as a zero TTL. */
1634 if (rr
->ttl
& UINT32_C(0x80000000))
1637 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1641 if (p
->rindex
+ rdlength
> p
->size
)
1646 switch (rr
->key
->type
) {
1649 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1652 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1655 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1658 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1663 case DNS_TYPE_CNAME
:
1664 case DNS_TYPE_DNAME
:
1665 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1668 case DNS_TYPE_HINFO
:
1669 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1673 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1676 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1678 if (rdlength
<= 0) {
1680 /* RFC 6763, section 6.1 suggests to treat
1681 * empty TXT RRs as equivalent to a TXT record
1682 * with a single empty string. */
1684 i
= malloc0(offsetof(DnsTxtItem
, data
) + 1); /* for safety reasons we add an extra NUL byte */
1690 DnsTxtItem
*last
= NULL
;
1692 while (p
->rindex
< offset
+ rdlength
) {
1697 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1701 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1705 memcpy(i
->data
, data
, sz
);
1708 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1717 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1721 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1725 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1729 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1733 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1737 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1741 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1745 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1749 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1753 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1757 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1760 case DNS_TYPE_LOC
: {
1764 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1769 rr
->loc
.version
= t
;
1771 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1775 if (!loc_size_ok(rr
->loc
.size
))
1778 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1782 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1785 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1789 if (!loc_size_ok(rr
->loc
.vert_pre
))
1792 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1796 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1800 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1806 dns_packet_rewind(p
, pos
);
1807 rr
->unparseable
= true;
1813 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1817 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1821 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1825 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1826 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1831 if (rr
->ds
.digest_size
<= 0)
1832 /* the accepted size depends on the algorithm, but for now
1833 just ensure that the value is greater than zero */
1838 case DNS_TYPE_SSHFP
:
1839 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1843 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1847 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1848 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1851 if (rr
->sshfp
.fingerprint_size
<= 0)
1852 /* the accepted size depends on the algorithm, but for now
1853 just ensure that the value is greater than zero */
1858 case DNS_TYPE_DNSKEY
:
1859 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1863 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1867 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1871 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1872 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1875 if (rr
->dnskey
.key_size
<= 0)
1876 /* the accepted size depends on the algorithm, but for now
1877 just ensure that the value is greater than zero */
1882 case DNS_TYPE_RRSIG
:
1883 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1887 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1891 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1895 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1899 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1903 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1907 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1911 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1915 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
1916 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
1919 if (rr
->rrsig
.signature_size
<= 0)
1920 /* the accepted size depends on the algorithm, but for now
1921 just ensure that the value is greater than zero */
1926 case DNS_TYPE_NSEC
: {
1929 * RFC6762, section 18.14 explictly states mDNS should use name compression.
1930 * This contradicts RFC3845, section 2.1.1
1933 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
1935 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
1939 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1941 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1942 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1943 * without the NSEC bit set. */
1947 case DNS_TYPE_NSEC3
: {
1950 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
1954 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
1958 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
1962 /* this may be zero */
1963 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1967 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
1971 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1978 r
= dns_packet_read_memdup(p
, size
,
1979 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
1984 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1986 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
1992 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
1996 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2000 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2004 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2005 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2008 if (rr
->tlsa
.data_size
<= 0)
2009 /* the accepted size depends on the algorithm, but for now
2010 just ensure that the value is greater than zero */
2016 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2020 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2024 r
= dns_packet_read_memdup(p
,
2025 rdlength
+ offset
- p
->rindex
,
2026 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2030 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2031 case DNS_TYPE_OPENPGPKEY
:
2034 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2040 if (p
->rindex
!= offset
+ rdlength
)
2046 if (ret_cache_flush
)
2047 *ret_cache_flush
= cache_flush
;
2049 *start
= rewinder
.saved_rindex
;
2050 CANCEL_REWINDER(rewinder
);
2055 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2057 bool found_dau_dhu_n3u
= false;
2060 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2064 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2066 /* Check that the version is 0 */
2067 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2069 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2073 l
= rr
->opt
.data_size
;
2075 uint16_t option_code
, option_length
;
2077 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2081 option_code
= unaligned_read_be16(p
);
2082 option_length
= unaligned_read_be16(p
+ 2);
2084 if (l
< option_length
+ 4U)
2087 /* RFC 6975 DAU, DHU or N3U fields found. */
2088 if (IN_SET(option_code
, 5, 6, 7))
2089 found_dau_dhu_n3u
= true;
2091 p
+= option_length
+ 4U;
2092 l
-= option_length
+ 4U;
2095 *rfc6975
= found_dau_dhu_n3u
;
2099 int dns_packet_extract(DnsPacket
*p
) {
2100 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2101 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2102 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2109 INIT_REWINDER(rewinder
, p
);
2110 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2112 n
= DNS_PACKET_QDCOUNT(p
);
2114 question
= dns_question_new(n
);
2118 for (i
= 0; i
< n
; i
++) {
2119 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2122 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2129 if (!dns_type_is_valid_query(key
->type
))
2132 r
= dns_question_add(question
, key
);
2138 n
= DNS_PACKET_RRCOUNT(p
);
2140 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2141 bool bad_opt
= false;
2143 answer
= dns_answer_new(n
);
2147 for (i
= 0; i
< n
; i
++) {
2148 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2149 bool cache_flush
= false;
2151 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, NULL
);
2155 /* Try to reduce memory usage a bit */
2157 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2159 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2162 if (p
->opt
|| bad_opt
) {
2163 /* Multiple OPT RRs? if so, let's ignore all, because there's something wrong
2164 * with the server, and if one is valid we wouldn't know which one. */
2165 log_debug("Multiple OPT RRs detected, ignoring all.");
2170 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2171 /* If the OPT RR is not owned by the root domain, then it is bad, let's ignore
2173 log_debug("OPT RR is not owned by root domain, ignoring.");
2178 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2179 /* OPT RR is in the wrong section? Some Belkin routers do this. This is a hint
2180 * the EDNS implementation is borked, like the Belkin one is, hence ignore
2182 log_debug("OPT RR in wrong section, ignoring.");
2187 if (!opt_is_good(rr
, &has_rfc6975
)) {
2188 log_debug("Malformed OPT RR, ignoring.");
2193 if (DNS_PACKET_QR(p
)) {
2194 /* Additional checks for responses */
2196 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
)) {
2197 /* If this is a reply and we don't know the EDNS version then something
2199 log_debug("EDNS version newer that our request, bad server.");
2204 /* If the OPT RR contains RFC6975 algorithm data, then this is indication that
2205 * the server just copied the OPT it got from us (which contained that data)
2206 * back into the reply. If so, then it doesn't properly support EDNS, as
2207 * RFC6975 makes it very clear that the algorithm data should only be contained
2208 * in questions, never in replies. Crappy Belkin routers copy the OPT data for
2209 * example, hence let's detect this so that we downgrade early. */
2210 log_debug("OPT RR contained RFC6975 data, ignoring.");
2216 p
->opt
= dns_resource_record_ref(rr
);
2219 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2220 * cached. Hence mark only those RRs as cacheable by default, but not the ones from the
2221 * Additional or Authority sections. */
2223 r
= dns_answer_add(answer
, rr
, p
->ifindex
,
2224 (i
< DNS_PACKET_ANCOUNT(p
) ? DNS_ANSWER_CACHEABLE
: 0) |
2225 (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
? DNS_ANSWER_SHARED_OWNER
: 0));
2230 /* Remember this RR, so that we potentically can merge it's ->key object with the next RR. Note
2231 * that we only do this if we actually decided to keep the RR around. */
2232 dns_resource_record_unref(previous
);
2233 previous
= dns_resource_record_ref(rr
);
2237 p
->opt
= dns_resource_record_unref(p
->opt
);
2240 p
->question
= question
;
2246 p
->extracted
= true;
2248 /* no CANCEL, always rewind */
2252 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2258 /* Checks if the specified packet is a reply for the specified
2259 * key and the specified key is the only one in the question
2262 if (DNS_PACKET_QR(p
) != 1)
2265 /* Let's unpack the packet, if that hasn't happened yet. */
2266 r
= dns_packet_extract(p
);
2270 if (p
->question
->n_keys
!= 1)
2273 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2276 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2277 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2278 [DNS_RCODE_FORMERR
] = "FORMERR",
2279 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2280 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2281 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2282 [DNS_RCODE_REFUSED
] = "REFUSED",
2283 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2284 [DNS_RCODE_YXRRSET
] = "YRRSET",
2285 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2286 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2287 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2288 [DNS_RCODE_BADVERS
] = "BADVERS",
2289 [DNS_RCODE_BADKEY
] = "BADKEY",
2290 [DNS_RCODE_BADTIME
] = "BADTIME",
2291 [DNS_RCODE_BADMODE
] = "BADMODE",
2292 [DNS_RCODE_BADNAME
] = "BADNAME",
2293 [DNS_RCODE_BADALG
] = "BADALG",
2294 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2295 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2297 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2299 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2300 [DNS_PROTOCOL_DNS
] = "dns",
2301 [DNS_PROTOCOL_MDNS
] = "mdns",
2302 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2304 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);