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 #define DNS_PACKET_SIZE_START 512u
32 assert_cc(DNS_PACKET_SIZE_START
> UDP_PACKET_HEADER_SIZE
)
34 typedef struct DnsPacketRewinder
{
39 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
41 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
44 #define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
45 #define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
47 int dns_packet_new(DnsPacket
**ret
, DnsProtocol protocol
, size_t mtu
) {
53 /* When dns_packet_new() is called with mtu == 0, allocate more than the
54 * absolute minimum (which is the dns packet header size), to avoid
55 * resizing immediately again after appending the first data to the packet.
57 if (mtu
< UDP_PACKET_HEADER_SIZE
)
58 a
= DNS_PACKET_SIZE_START
;
60 a
= MAX(mtu
, DNS_PACKET_HEADER_SIZE
);
62 /* round up to next page size */
63 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
65 /* make sure we never allocate more than useful */
66 if (a
> DNS_PACKET_SIZE_MAX
)
67 a
= DNS_PACKET_SIZE_MAX
;
69 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
73 p
->size
= p
->rindex
= DNS_PACKET_HEADER_SIZE
;
75 p
->protocol
= protocol
;
76 p
->opt_start
= p
->opt_size
= (size_t) -1;
84 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
90 h
= DNS_PACKET_HEADER(p
);
93 case DNS_PROTOCOL_LLMNR
:
96 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
107 case DNS_PROTOCOL_MDNS
:
108 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
112 0 /* rd (ask for recursion) */,
122 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
126 1 /* rd (ask for recursion) */,
129 dnssec_checking_disabled
/* cd */,
134 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t mtu
, bool dnssec_checking_disabled
) {
140 r
= dns_packet_new(&p
, protocol
, mtu
);
144 /* Always set the TC bit to 0 initially.
145 * If there are multiple packets later, we'll update the bit shortly before sending.
147 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
153 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
158 assert(!p
->on_stack
);
160 assert(p
->n_ref
> 0);
165 static void dns_packet_free(DnsPacket
*p
) {
170 dns_question_unref(p
->question
);
171 dns_answer_unref(p
->answer
);
172 dns_resource_record_unref(p
->opt
);
174 while ((s
= hashmap_steal_first_key(p
->names
)))
176 hashmap_free(p
->names
);
184 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
188 assert(p
->n_ref
> 0);
190 dns_packet_unref(p
->more
);
200 int dns_packet_validate(DnsPacket
*p
) {
203 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
206 if (p
->size
> DNS_PACKET_SIZE_MAX
)
212 int dns_packet_validate_reply(DnsPacket
*p
) {
217 r
= dns_packet_validate(p
);
221 if (DNS_PACKET_QR(p
) != 1)
224 if (DNS_PACKET_OPCODE(p
) != 0)
227 switch (p
->protocol
) {
229 case DNS_PROTOCOL_LLMNR
:
230 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
231 if (DNS_PACKET_QDCOUNT(p
) != 1)
236 case DNS_PROTOCOL_MDNS
:
237 /* RFC 6762, Section 18 */
238 if (DNS_PACKET_RCODE(p
) != 0)
250 int dns_packet_validate_query(DnsPacket
*p
) {
255 r
= dns_packet_validate(p
);
259 if (DNS_PACKET_QR(p
) != 0)
262 if (DNS_PACKET_OPCODE(p
) != 0)
265 if (DNS_PACKET_TC(p
))
268 switch (p
->protocol
) {
270 case DNS_PROTOCOL_LLMNR
:
271 case DNS_PROTOCOL_DNS
:
272 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
273 if (DNS_PACKET_QDCOUNT(p
) != 1)
276 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
277 if (DNS_PACKET_ANCOUNT(p
) > 0)
280 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
281 if (DNS_PACKET_NSCOUNT(p
) > 0)
286 case DNS_PROTOCOL_MDNS
:
287 /* RFC 6762, Section 18 */
288 if (DNS_PACKET_AA(p
) != 0 ||
289 DNS_PACKET_RD(p
) != 0 ||
290 DNS_PACKET_RA(p
) != 0 ||
291 DNS_PACKET_AD(p
) != 0 ||
292 DNS_PACKET_CD(p
) != 0 ||
293 DNS_PACKET_RCODE(p
) != 0)
305 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
308 if (p
->size
+ add
> p
->allocated
) {
311 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
312 if (a
> DNS_PACKET_SIZE_MAX
)
313 a
= DNS_PACKET_SIZE_MAX
;
315 if (p
->size
+ add
> a
)
321 d
= realloc(p
->_data
, a
);
327 p
->_data
= malloc(a
);
331 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
332 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
342 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
348 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
358 HASHMAP_FOREACH_KEY(n
, s
, p
->names
, i
) {
360 if (PTR_TO_SIZE(n
) < sz
)
363 hashmap_remove(p
->names
, s
);
370 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
376 r
= dns_packet_extend(p
, l
, &q
, start
);
384 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
390 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
394 ((uint8_t*) d
)[0] = v
;
399 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
405 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
409 unaligned_write_be16(d
, v
);
414 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
420 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
424 unaligned_write_be32(d
, v
);
429 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
433 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
436 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
441 assert(s
|| size
== 0);
446 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
450 ((uint8_t*) d
)[0] = (uint8_t) size
;
452 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
457 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
461 /* Append a label to a packet. Optionally, does this in DNSSEC
462 * canonical form, if this label is marked as a candidate for
463 * it, and the canonical form logic is enabled for the
469 if (l
> DNS_LABEL_MAX
)
472 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
476 *(w
++) = (uint8_t) l
;
478 if (p
->canonical_form
&& canonical_candidate
) {
481 /* Generate in canonical form, as defined by DNSSEC
482 * RFC 4034, Section 6.2, i.e. all lower-case. */
484 for (i
= 0; i
< l
; i
++)
485 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
487 /* Otherwise, just copy the string unaltered. This is
488 * essential for DNS-SD, where the casing of labels
489 * matters and needs to be retained. */
495 int dns_packet_append_name(
498 bool allow_compression
,
499 bool canonical_candidate
,
508 if (p
->refuse_compression
)
509 allow_compression
= false;
511 saved_size
= p
->size
;
513 while (!dns_name_is_root(name
)) {
514 const char *z
= name
;
515 char label
[DNS_LABEL_MAX
];
518 if (allow_compression
)
519 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
524 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
532 r
= dns_label_unescape(&name
, label
, sizeof(label
));
536 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
540 if (allow_compression
) {
541 _cleanup_free_
char *s
= NULL
;
549 r
= hashmap_ensure_allocated(&p
->names
, &dns_name_hash_ops
);
553 r
= hashmap_put(p
->names
, s
, SIZE_TO_PTR(n
));
561 r
= dns_packet_append_uint8(p
, 0, NULL
);
572 dns_packet_truncate(p
, saved_size
);
576 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
584 saved_size
= p
->size
;
586 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
590 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
594 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH
: k
->class;
595 r
= dns_packet_append_uint16(p
, class, NULL
);
605 dns_packet_truncate(p
, saved_size
);
609 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
617 saved_size
= p
->size
;
619 r
= dns_packet_append_uint8(p
, window
, NULL
);
623 r
= dns_packet_append_uint8(p
, length
, NULL
);
627 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
636 dns_packet_truncate(p
, saved_size
);
640 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
644 uint8_t bitmaps
[32] = {};
651 saved_size
= p
->size
;
653 BITMAP_FOREACH(n
, types
, i
) {
656 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
657 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
667 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
670 if (bitmaps
[entry
/ 8] != 0) {
671 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
681 dns_packet_truncate(p
, saved_size
);
685 /* Append the OPT pseudo-RR described in RFC6891 */
686 int dns_packet_append_opt(DnsPacket
*p
, uint16_t max_udp_size
, bool edns0_do
, int rcode
, size_t *start
) {
691 /* we must never advertise supported packet size smaller than the legacy max */
692 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
694 assert(rcode
<= _DNS_RCODE_MAX
);
696 if (p
->opt_start
!= (size_t) -1)
699 assert(p
->opt_size
== (size_t) -1);
701 saved_size
= p
->size
;
704 r
= dns_packet_append_uint8(p
, 0, NULL
);
709 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
713 /* class: maximum udp packet that can be received */
714 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
718 /* extended RCODE and VERSION */
719 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
723 /* flags: DNSSEC OK (DO), see RFC3225 */
724 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
729 if (edns0_do
&& !DNS_PACKET_QR(p
)) {
730 /* If DO is on and this is not a reply, also append RFC6975 Algorithm data */
732 static const uint8_t rfc6975
[] = {
734 0, 5, /* OPTION_CODE: DAU */
735 0, 6, /* LIST_LENGTH */
736 DNSSEC_ALGORITHM_RSASHA1
,
737 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
738 DNSSEC_ALGORITHM_RSASHA256
,
739 DNSSEC_ALGORITHM_RSASHA512
,
740 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
741 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
743 0, 6, /* OPTION_CODE: DHU */
744 0, 3, /* LIST_LENGTH */
746 DNSSEC_DIGEST_SHA256
,
747 DNSSEC_DIGEST_SHA384
,
749 0, 7, /* OPTION_CODE: N3U */
750 0, 1, /* LIST_LENGTH */
751 NSEC3_ALGORITHM_SHA1
,
754 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
);
758 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
);
760 r
= dns_packet_append_uint16(p
, 0, NULL
);
764 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
766 p
->opt_start
= saved_size
;
767 p
->opt_size
= p
->size
- saved_size
;
775 dns_packet_truncate(p
, saved_size
);
779 int dns_packet_truncate_opt(DnsPacket
*p
) {
782 if (p
->opt_start
== (size_t) -1) {
783 assert(p
->opt_size
== (size_t) -1);
787 assert(p
->opt_size
!= (size_t) -1);
788 assert(DNS_PACKET_ARCOUNT(p
) > 0);
790 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
793 dns_packet_truncate(p
, p
->opt_start
);
794 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
795 p
->opt_start
= p
->opt_size
= (size_t) -1;
800 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
802 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
809 saved_size
= p
->size
;
811 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
815 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
816 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
820 /* Initially we write 0 here */
821 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
825 rds
= p
->size
- saved_size
;
827 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
830 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
834 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
838 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
842 r
= dns_packet_append_name(p
, rr
->srv
.name
, true, false, NULL
);
849 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, false, NULL
);
853 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
857 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
860 case DNS_TYPE_SPF
: /* exactly the same as TXT */
863 if (!rr
->txt
.items
) {
864 /* RFC 6763, section 6.1 suggests to generate
865 * single empty string for an empty array. */
867 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
873 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
874 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
884 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
888 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
892 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, false, NULL
);
896 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, false, NULL
);
900 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
904 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
908 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
912 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
916 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
920 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
924 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, false, NULL
);
928 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
932 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
936 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
940 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
944 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
948 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
952 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
956 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
960 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
964 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
968 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
972 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
976 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
980 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
983 case DNS_TYPE_DNSKEY
:
984 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
988 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
992 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
996 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1000 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1004 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1008 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1012 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1016 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1020 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1024 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1028 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1032 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1036 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1040 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1046 case DNS_TYPE_NSEC3
:
1047 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1051 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1055 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1059 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1063 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1067 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1071 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1075 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1082 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1086 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1090 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1094 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1098 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1102 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1106 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1110 case DNS_TYPE_OPENPGPKEY
:
1111 case _DNS_TYPE_INVALID
: /* unparseable */
1114 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1120 /* Let's calculate the actual data size and update the field */
1121 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1122 if (rdlength
> 0xFFFF) {
1128 p
->size
= rdlength_offset
;
1129 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1135 *start
= saved_size
;
1143 dns_packet_truncate(p
, saved_size
);
1147 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1148 DnsResourceKey
*key
;
1153 DNS_QUESTION_FOREACH(key
, q
) {
1154 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1162 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
) {
1163 DnsResourceRecord
*rr
;
1164 DnsAnswerFlags flags
;
1169 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1170 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1178 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1181 if (p
->rindex
+ sz
> p
->size
)
1185 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1194 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1196 assert(idx
<= p
->size
);
1197 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1202 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1209 r
= dns_packet_read(p
, sz
, &q
, start
);
1217 static int dns_packet_read_memdup(
1218 DnsPacket
*p
, size_t size
,
1219 void **ret
, size_t *ret_size
,
1220 size_t *ret_start
) {
1229 r
= dns_packet_read(p
, size
, &src
, &start
);
1238 copy
= memdup(src
, size
);
1253 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1259 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1263 *ret
= ((uint8_t*) d
)[0];
1267 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1273 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1277 *ret
= unaligned_read_be16(d
);
1282 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1288 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1292 *ret
= unaligned_read_be32(d
);
1297 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1298 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1305 INIT_REWINDER(rewinder
, p
);
1307 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1311 r
= dns_packet_read(p
, c
, &d
, NULL
);
1315 if (memchr(d
, 0, c
))
1322 if (!utf8_is_valid(t
)) {
1330 *start
= rewinder
.saved_rindex
;
1331 CANCEL_REWINDER(rewinder
);
1336 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1337 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1342 INIT_REWINDER(rewinder
, p
);
1344 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1348 r
= dns_packet_read(p
, c
, ret
, NULL
);
1355 *start
= rewinder
.saved_rindex
;
1356 CANCEL_REWINDER(rewinder
);
1361 int dns_packet_read_name(
1364 bool allow_compression
,
1367 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1368 size_t after_rindex
= 0, jump_barrier
;
1369 _cleanup_free_
char *ret
= NULL
;
1370 size_t n
= 0, allocated
= 0;
1376 INIT_REWINDER(rewinder
, p
);
1377 jump_barrier
= p
->rindex
;
1379 if (p
->refuse_compression
)
1380 allow_compression
= false;
1385 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1396 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1400 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1408 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
1414 } else if (allow_compression
&& (c
& 0xc0) == 0xc0) {
1418 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1422 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1423 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1426 if (after_rindex
== 0)
1427 after_rindex
= p
->rindex
;
1429 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1436 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
1441 if (after_rindex
!= 0)
1442 p
->rindex
= after_rindex
;
1448 *start
= rewinder
.saved_rindex
;
1449 CANCEL_REWINDER(rewinder
);
1454 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1457 const uint8_t *bitmap
;
1461 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1466 INIT_REWINDER(rewinder
, p
);
1468 r
= bitmap_ensure_allocated(types
);
1472 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1476 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1480 if (length
== 0 || length
> 32)
1483 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1487 for (i
= 0; i
< length
; i
++) {
1488 uint8_t bitmask
= 1 << 7;
1499 if (bitmap
[i
] & bitmask
) {
1502 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1504 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1505 if (dns_type_is_pseudo(n
))
1508 r
= bitmap_set(*types
, n
);
1522 *start
= rewinder
.saved_rindex
;
1523 CANCEL_REWINDER(rewinder
);
1528 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1529 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1532 INIT_REWINDER(rewinder
, p
);
1534 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1535 r
= dns_packet_read_type_window(p
, types
, NULL
);
1539 /* don't read past end of current RR */
1540 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1544 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1548 *start
= rewinder
.saved_rindex
;
1549 CANCEL_REWINDER(rewinder
);
1554 int dns_packet_read_key(DnsPacket
*p
, DnsResourceKey
**ret
, bool *ret_cache_flush
, size_t *start
) {
1555 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1556 _cleanup_free_
char *name
= NULL
;
1557 bool cache_flush
= false;
1558 uint16_t class, type
;
1559 DnsResourceKey
*key
;
1564 INIT_REWINDER(rewinder
, p
);
1566 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1570 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1574 r
= dns_packet_read_uint16(p
, &class, NULL
);
1578 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1579 /* See RFC6762, Section 10.2 */
1581 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1582 class &= ~MDNS_RR_CACHE_FLUSH
;
1587 key
= dns_resource_key_new_consume(class, type
, name
);
1594 if (ret_cache_flush
)
1595 *ret_cache_flush
= cache_flush
;
1597 *start
= rewinder
.saved_rindex
;
1598 CANCEL_REWINDER(rewinder
);
1603 static bool loc_size_ok(uint8_t size
) {
1604 uint8_t m
= size
>> 4, e
= size
& 0xF;
1606 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1609 int dns_packet_read_rr(DnsPacket
*p
, DnsResourceRecord
**ret
, bool *ret_cache_flush
, size_t *start
) {
1610 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1611 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1612 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1621 INIT_REWINDER(rewinder
, p
);
1623 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1627 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1630 rr
= dns_resource_record_new(key
);
1634 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1638 /* RFC 2181, Section 8, suggests to
1639 * treat a TTL with the MSB set as a zero TTL. */
1640 if (rr
->ttl
& UINT32_C(0x80000000))
1643 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1647 if (p
->rindex
+ rdlength
> p
->size
)
1652 switch (rr
->key
->type
) {
1655 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1658 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1661 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1664 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1669 case DNS_TYPE_CNAME
:
1670 case DNS_TYPE_DNAME
:
1671 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1674 case DNS_TYPE_HINFO
:
1675 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1679 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1682 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1684 if (rdlength
<= 0) {
1686 /* RFC 6763, section 6.1 suggests to treat
1687 * empty TXT RRs as equivalent to a TXT record
1688 * with a single empty string. */
1690 i
= malloc0(offsetof(DnsTxtItem
, data
) + 1); /* for safety reasons we add an extra NUL byte */
1696 DnsTxtItem
*last
= NULL
;
1698 while (p
->rindex
< offset
+ rdlength
) {
1703 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1707 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1711 memcpy(i
->data
, data
, sz
);
1714 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1723 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1727 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1731 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1735 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1739 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1743 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1747 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1751 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1755 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1759 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1763 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1766 case DNS_TYPE_LOC
: {
1770 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1775 rr
->loc
.version
= t
;
1777 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1781 if (!loc_size_ok(rr
->loc
.size
))
1784 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1788 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1791 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1795 if (!loc_size_ok(rr
->loc
.vert_pre
))
1798 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1802 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1806 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1812 dns_packet_rewind(p
, pos
);
1813 rr
->unparseable
= true;
1819 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1823 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1827 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1831 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1832 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1837 if (rr
->ds
.digest_size
<= 0)
1838 /* the accepted size depends on the algorithm, but for now
1839 just ensure that the value is greater than zero */
1844 case DNS_TYPE_SSHFP
:
1845 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1849 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1853 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1854 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1857 if (rr
->sshfp
.fingerprint_size
<= 0)
1858 /* the accepted size depends on the algorithm, but for now
1859 just ensure that the value is greater than zero */
1864 case DNS_TYPE_DNSKEY
:
1865 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1869 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1873 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1877 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1878 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1881 if (rr
->dnskey
.key_size
<= 0)
1882 /* the accepted size depends on the algorithm, but for now
1883 just ensure that the value is greater than zero */
1888 case DNS_TYPE_RRSIG
:
1889 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1893 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1897 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1901 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1905 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1909 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1913 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1917 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1921 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
1922 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
1925 if (rr
->rrsig
.signature_size
<= 0)
1926 /* the accepted size depends on the algorithm, but for now
1927 just ensure that the value is greater than zero */
1932 case DNS_TYPE_NSEC
: {
1935 * RFC6762, section 18.14 explictly states mDNS should use name compression.
1936 * This contradicts RFC3845, section 2.1.1
1939 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
1941 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
1945 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1947 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1948 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1949 * without the NSEC bit set. */
1953 case DNS_TYPE_NSEC3
: {
1956 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
1960 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
1964 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
1968 /* this may be zero */
1969 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1973 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
1977 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1984 r
= dns_packet_read_memdup(p
, size
,
1985 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
1990 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1992 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
1998 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2002 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2006 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2010 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2011 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2014 if (rr
->tlsa
.data_size
<= 0)
2015 /* the accepted size depends on the algorithm, but for now
2016 just ensure that the value is greater than zero */
2022 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2026 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2030 r
= dns_packet_read_memdup(p
,
2031 rdlength
+ offset
- p
->rindex
,
2032 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2036 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2037 case DNS_TYPE_OPENPGPKEY
:
2040 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2046 if (p
->rindex
!= offset
+ rdlength
)
2052 if (ret_cache_flush
)
2053 *ret_cache_flush
= cache_flush
;
2055 *start
= rewinder
.saved_rindex
;
2056 CANCEL_REWINDER(rewinder
);
2061 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2063 bool found_dau_dhu_n3u
= false;
2066 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2070 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2072 /* Check that the version is 0 */
2073 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2075 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2079 l
= rr
->opt
.data_size
;
2081 uint16_t option_code
, option_length
;
2083 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2087 option_code
= unaligned_read_be16(p
);
2088 option_length
= unaligned_read_be16(p
+ 2);
2090 if (l
< option_length
+ 4U)
2093 /* RFC 6975 DAU, DHU or N3U fields found. */
2094 if (IN_SET(option_code
, 5, 6, 7))
2095 found_dau_dhu_n3u
= true;
2097 p
+= option_length
+ 4U;
2098 l
-= option_length
+ 4U;
2101 *rfc6975
= found_dau_dhu_n3u
;
2105 int dns_packet_extract(DnsPacket
*p
) {
2106 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2107 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2108 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2115 INIT_REWINDER(rewinder
, p
);
2116 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2118 n
= DNS_PACKET_QDCOUNT(p
);
2120 question
= dns_question_new(n
);
2124 for (i
= 0; i
< n
; i
++) {
2125 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2128 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2135 if (!dns_type_is_valid_query(key
->type
))
2138 r
= dns_question_add(question
, key
);
2144 n
= DNS_PACKET_RRCOUNT(p
);
2146 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2147 bool bad_opt
= false;
2149 answer
= dns_answer_new(n
);
2153 for (i
= 0; i
< n
; i
++) {
2154 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2155 bool cache_flush
= false;
2157 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, NULL
);
2161 /* Try to reduce memory usage a bit */
2163 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2165 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2168 if (p
->opt
|| bad_opt
) {
2169 /* Multiple OPT RRs? if so, let's ignore all, because there's something wrong
2170 * with the server, and if one is valid we wouldn't know which one. */
2171 log_debug("Multiple OPT RRs detected, ignoring all.");
2176 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2177 /* If the OPT RR is not owned by the root domain, then it is bad, let's ignore
2179 log_debug("OPT RR is not owned by root domain, ignoring.");
2184 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2185 /* OPT RR is in the wrong section? Some Belkin routers do this. This is a hint
2186 * the EDNS implementation is borked, like the Belkin one is, hence ignore
2188 log_debug("OPT RR in wrong section, ignoring.");
2193 if (!opt_is_good(rr
, &has_rfc6975
)) {
2194 log_debug("Malformed OPT RR, ignoring.");
2199 if (DNS_PACKET_QR(p
)) {
2200 /* Additional checks for responses */
2202 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
)) {
2203 /* If this is a reply and we don't know the EDNS version then something
2205 log_debug("EDNS version newer that our request, bad server.");
2210 /* If the OPT RR contains RFC6975 algorithm data, then this is indication that
2211 * the server just copied the OPT it got from us (which contained that data)
2212 * back into the reply. If so, then it doesn't properly support EDNS, as
2213 * RFC6975 makes it very clear that the algorithm data should only be contained
2214 * in questions, never in replies. Crappy Belkin routers copy the OPT data for
2215 * example, hence let's detect this so that we downgrade early. */
2216 log_debug("OPT RR contained RFC6975 data, ignoring.");
2222 p
->opt
= dns_resource_record_ref(rr
);
2225 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2226 * cached. Hence mark only those RRs as cacheable by default, but not the ones from the
2227 * Additional or Authority sections. */
2229 r
= dns_answer_add(answer
, rr
, p
->ifindex
,
2230 (i
< DNS_PACKET_ANCOUNT(p
) ? DNS_ANSWER_CACHEABLE
: 0) |
2231 (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
? DNS_ANSWER_SHARED_OWNER
: 0));
2236 /* Remember this RR, so that we potentically can merge it's ->key object with the next RR. Note
2237 * that we only do this if we actually decided to keep the RR around. */
2238 dns_resource_record_unref(previous
);
2239 previous
= dns_resource_record_ref(rr
);
2243 p
->opt
= dns_resource_record_unref(p
->opt
);
2246 p
->question
= question
;
2252 p
->extracted
= true;
2254 /* no CANCEL, always rewind */
2258 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2264 /* Checks if the specified packet is a reply for the specified
2265 * key and the specified key is the only one in the question
2268 if (DNS_PACKET_QR(p
) != 1)
2271 /* Let's unpack the packet, if that hasn't happened yet. */
2272 r
= dns_packet_extract(p
);
2279 if (p
->question
->n_keys
!= 1)
2282 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2285 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2286 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2287 [DNS_RCODE_FORMERR
] = "FORMERR",
2288 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2289 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2290 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2291 [DNS_RCODE_REFUSED
] = "REFUSED",
2292 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2293 [DNS_RCODE_YXRRSET
] = "YRRSET",
2294 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2295 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2296 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2297 [DNS_RCODE_BADVERS
] = "BADVERS",
2298 [DNS_RCODE_BADKEY
] = "BADKEY",
2299 [DNS_RCODE_BADTIME
] = "BADTIME",
2300 [DNS_RCODE_BADMODE
] = "BADMODE",
2301 [DNS_RCODE_BADNAME
] = "BADNAME",
2302 [DNS_RCODE_BADALG
] = "BADALG",
2303 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2304 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2306 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2308 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2309 [DNS_PROTOCOL_DNS
] = "dns",
2310 [DNS_PROTOCOL_MDNS
] = "mdns",
2311 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2313 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);