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 assert_cc(DNS_PACKET_SIZE_START
> DNS_PACKET_HEADER_SIZE
)
33 typedef struct DnsPacketRewinder
{
38 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
40 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
43 #define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
44 #define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
49 size_t min_alloc_dsize
,
56 assert(max_size
>= DNS_PACKET_HEADER_SIZE
);
58 if (max_size
> DNS_PACKET_SIZE_MAX
)
59 max_size
= DNS_PACKET_SIZE_MAX
;
61 /* The caller may not check what is going to be truly allocated, so do not allow to
62 * allocate a DNS packet bigger than DNS_PACKET_SIZE_MAX.
64 if (min_alloc_dsize
> DNS_PACKET_SIZE_MAX
) {
65 log_error("Requested packet data size too big: %zu", min_alloc_dsize
);
69 /* When dns_packet_new() is called with min_alloc_dsize == 0, allocate more than the
70 * absolute minimum (which is the dns packet header size), to avoid
71 * resizing immediately again after appending the first data to the packet.
73 if (min_alloc_dsize
< DNS_PACKET_HEADER_SIZE
)
74 a
= DNS_PACKET_SIZE_START
;
78 /* round up to next page size */
79 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
81 /* make sure we never allocate more than useful */
85 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
89 p
->size
= p
->rindex
= DNS_PACKET_HEADER_SIZE
;
91 p
->max_size
= max_size
;
92 p
->protocol
= protocol
;
93 p
->opt_start
= p
->opt_size
= (size_t) -1;
101 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
107 h
= DNS_PACKET_HEADER(p
);
109 switch(p
->protocol
) {
110 case DNS_PROTOCOL_LLMNR
:
113 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
124 case DNS_PROTOCOL_MDNS
:
125 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
129 0 /* rd (ask for recursion) */,
139 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
143 1 /* rd (ask for recursion) */,
146 dnssec_checking_disabled
/* cd */,
151 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t min_alloc_dsize
, bool dnssec_checking_disabled
) {
157 r
= dns_packet_new(&p
, protocol
, min_alloc_dsize
, DNS_PACKET_SIZE_MAX
);
161 /* Always set the TC bit to 0 initially.
162 * If there are multiple packets later, we'll update the bit shortly before sending.
164 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
170 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
175 assert(!p
->on_stack
);
177 assert(p
->n_ref
> 0);
182 static void dns_packet_free(DnsPacket
*p
) {
187 dns_question_unref(p
->question
);
188 dns_answer_unref(p
->answer
);
189 dns_resource_record_unref(p
->opt
);
191 while ((s
= hashmap_steal_first_key(p
->names
)))
193 hashmap_free(p
->names
);
201 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
205 assert(p
->n_ref
> 0);
207 dns_packet_unref(p
->more
);
217 int dns_packet_validate(DnsPacket
*p
) {
220 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
223 if (p
->size
> DNS_PACKET_SIZE_MAX
)
229 int dns_packet_validate_reply(DnsPacket
*p
) {
234 r
= dns_packet_validate(p
);
238 if (DNS_PACKET_QR(p
) != 1)
241 if (DNS_PACKET_OPCODE(p
) != 0)
244 switch (p
->protocol
) {
246 case DNS_PROTOCOL_LLMNR
:
247 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
248 if (DNS_PACKET_QDCOUNT(p
) != 1)
253 case DNS_PROTOCOL_MDNS
:
254 /* RFC 6762, Section 18 */
255 if (DNS_PACKET_RCODE(p
) != 0)
267 int dns_packet_validate_query(DnsPacket
*p
) {
272 r
= dns_packet_validate(p
);
276 if (DNS_PACKET_QR(p
) != 0)
279 if (DNS_PACKET_OPCODE(p
) != 0)
282 if (DNS_PACKET_TC(p
))
285 switch (p
->protocol
) {
287 case DNS_PROTOCOL_LLMNR
:
288 case DNS_PROTOCOL_DNS
:
289 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
290 if (DNS_PACKET_QDCOUNT(p
) != 1)
293 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
294 if (DNS_PACKET_ANCOUNT(p
) > 0)
297 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
298 if (DNS_PACKET_NSCOUNT(p
) > 0)
303 case DNS_PROTOCOL_MDNS
:
304 /* RFC 6762, Section 18 */
305 if (DNS_PACKET_AA(p
) != 0 ||
306 DNS_PACKET_RD(p
) != 0 ||
307 DNS_PACKET_RA(p
) != 0 ||
308 DNS_PACKET_AD(p
) != 0 ||
309 DNS_PACKET_CD(p
) != 0 ||
310 DNS_PACKET_RCODE(p
) != 0)
322 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
325 if (p
->size
+ add
> p
->allocated
) {
328 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
330 ms
= dns_packet_size_max(p
);
334 if (p
->size
+ add
> a
)
340 d
= realloc(p
->_data
, a
);
346 p
->_data
= malloc(a
);
350 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
351 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
361 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
367 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
377 HASHMAP_FOREACH_KEY(n
, s
, p
->names
, i
) {
379 if (PTR_TO_SIZE(n
) < sz
)
382 hashmap_remove(p
->names
, s
);
389 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
395 r
= dns_packet_extend(p
, l
, &q
, start
);
403 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
409 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
413 ((uint8_t*) d
)[0] = v
;
418 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
424 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
428 unaligned_write_be16(d
, v
);
433 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
439 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
443 unaligned_write_be32(d
, v
);
448 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
452 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
455 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
460 assert(s
|| size
== 0);
465 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
469 ((uint8_t*) d
)[0] = (uint8_t) size
;
471 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
476 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
480 /* Append a label to a packet. Optionally, does this in DNSSEC
481 * canonical form, if this label is marked as a candidate for
482 * it, and the canonical form logic is enabled for the
488 if (l
> DNS_LABEL_MAX
)
491 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
495 *(w
++) = (uint8_t) l
;
497 if (p
->canonical_form
&& canonical_candidate
) {
500 /* Generate in canonical form, as defined by DNSSEC
501 * RFC 4034, Section 6.2, i.e. all lower-case. */
503 for (i
= 0; i
< l
; i
++)
504 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
506 /* Otherwise, just copy the string unaltered. This is
507 * essential for DNS-SD, where the casing of labels
508 * matters and needs to be retained. */
514 int dns_packet_append_name(
517 bool allow_compression
,
518 bool canonical_candidate
,
527 if (p
->refuse_compression
)
528 allow_compression
= false;
530 saved_size
= p
->size
;
532 while (!dns_name_is_root(name
)) {
533 const char *z
= name
;
534 char label
[DNS_LABEL_MAX
];
537 if (allow_compression
)
538 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
543 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
551 r
= dns_label_unescape(&name
, label
, sizeof(label
));
555 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
559 if (allow_compression
) {
560 _cleanup_free_
char *s
= NULL
;
568 r
= hashmap_ensure_allocated(&p
->names
, &dns_name_hash_ops
);
572 r
= hashmap_put(p
->names
, s
, SIZE_TO_PTR(n
));
580 r
= dns_packet_append_uint8(p
, 0, NULL
);
591 dns_packet_truncate(p
, saved_size
);
595 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
603 saved_size
= p
->size
;
605 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
609 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
613 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH
: k
->class;
614 r
= dns_packet_append_uint16(p
, class, NULL
);
624 dns_packet_truncate(p
, saved_size
);
628 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
636 saved_size
= p
->size
;
638 r
= dns_packet_append_uint8(p
, window
, NULL
);
642 r
= dns_packet_append_uint8(p
, length
, NULL
);
646 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
655 dns_packet_truncate(p
, saved_size
);
659 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
663 uint8_t bitmaps
[32] = {};
670 saved_size
= p
->size
;
672 BITMAP_FOREACH(n
, types
, i
) {
675 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
676 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
686 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
689 if (bitmaps
[entry
/ 8] != 0) {
690 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
700 dns_packet_truncate(p
, saved_size
);
704 /* Append the OPT pseudo-RR described in RFC6891 */
705 int dns_packet_append_opt(DnsPacket
*p
, uint16_t max_udp_size
, bool edns0_do
, int rcode
, size_t *start
) {
710 /* we must never advertise supported packet size smaller than the legacy max */
711 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
713 assert(rcode
<= _DNS_RCODE_MAX
);
715 if (p
->opt_start
!= (size_t) -1)
718 assert(p
->opt_size
== (size_t) -1);
720 saved_size
= p
->size
;
723 r
= dns_packet_append_uint8(p
, 0, NULL
);
728 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
732 /* class: maximum udp packet that can be received */
733 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
737 /* extended RCODE and VERSION */
738 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
742 /* flags: DNSSEC OK (DO), see RFC3225 */
743 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
748 if (edns0_do
&& !DNS_PACKET_QR(p
)) {
749 /* If DO is on and this is not a reply, also append RFC6975 Algorithm data */
751 static const uint8_t rfc6975
[] = {
753 0, 5, /* OPTION_CODE: DAU */
754 0, 6, /* LIST_LENGTH */
755 DNSSEC_ALGORITHM_RSASHA1
,
756 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
757 DNSSEC_ALGORITHM_RSASHA256
,
758 DNSSEC_ALGORITHM_RSASHA512
,
759 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
760 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
762 0, 6, /* OPTION_CODE: DHU */
763 0, 3, /* LIST_LENGTH */
765 DNSSEC_DIGEST_SHA256
,
766 DNSSEC_DIGEST_SHA384
,
768 0, 7, /* OPTION_CODE: N3U */
769 0, 1, /* LIST_LENGTH */
770 NSEC3_ALGORITHM_SHA1
,
773 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
);
777 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
);
779 r
= dns_packet_append_uint16(p
, 0, NULL
);
783 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
785 p
->opt_start
= saved_size
;
786 p
->opt_size
= p
->size
- saved_size
;
794 dns_packet_truncate(p
, saved_size
);
798 int dns_packet_truncate_opt(DnsPacket
*p
) {
801 if (p
->opt_start
== (size_t) -1) {
802 assert(p
->opt_size
== (size_t) -1);
806 assert(p
->opt_size
!= (size_t) -1);
807 assert(DNS_PACKET_ARCOUNT(p
) > 0);
809 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
812 dns_packet_truncate(p
, p
->opt_start
);
813 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
814 p
->opt_start
= p
->opt_size
= (size_t) -1;
819 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
821 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
828 saved_size
= p
->size
;
830 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
834 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
835 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
839 /* Initially we write 0 here */
840 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
844 rds
= p
->size
- saved_size
;
846 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
849 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
853 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
857 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
861 r
= dns_packet_append_name(p
, rr
->srv
.name
, true, false, NULL
);
868 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, false, NULL
);
872 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
876 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
879 case DNS_TYPE_SPF
: /* exactly the same as TXT */
882 if (!rr
->txt
.items
) {
883 /* RFC 6763, section 6.1 suggests to generate
884 * single empty string for an empty array. */
886 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
892 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
893 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
903 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
907 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
911 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, false, NULL
);
915 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, false, NULL
);
919 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
923 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
927 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
931 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
935 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
939 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
943 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, false, NULL
);
947 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
951 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
955 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
959 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
963 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
967 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
971 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
975 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
979 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
983 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
987 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
991 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
995 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
999 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
1002 case DNS_TYPE_DNSKEY
:
1003 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
1007 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
1011 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
1015 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1018 case DNS_TYPE_RRSIG
:
1019 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1023 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1027 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1031 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1035 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1039 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1043 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1047 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1051 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1055 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1059 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1065 case DNS_TYPE_NSEC3
:
1066 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1070 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1074 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1078 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1082 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1086 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1090 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1094 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1101 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1105 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1109 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1113 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1117 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1121 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1125 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1129 case DNS_TYPE_OPENPGPKEY
:
1130 case _DNS_TYPE_INVALID
: /* unparseable */
1133 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1139 /* Let's calculate the actual data size and update the field */
1140 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1141 if (rdlength
> 0xFFFF) {
1147 p
->size
= rdlength_offset
;
1148 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1154 *start
= saved_size
;
1162 dns_packet_truncate(p
, saved_size
);
1166 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1167 DnsResourceKey
*key
;
1172 DNS_QUESTION_FOREACH(key
, q
) {
1173 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1181 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
) {
1182 DnsResourceRecord
*rr
;
1183 DnsAnswerFlags flags
;
1188 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1189 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1197 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1200 if (p
->rindex
+ sz
> p
->size
)
1204 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1213 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1215 assert(idx
<= p
->size
);
1216 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1221 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1228 r
= dns_packet_read(p
, sz
, &q
, start
);
1236 static int dns_packet_read_memdup(
1237 DnsPacket
*p
, size_t size
,
1238 void **ret
, size_t *ret_size
,
1239 size_t *ret_start
) {
1248 r
= dns_packet_read(p
, size
, &src
, &start
);
1257 copy
= memdup(src
, size
);
1272 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1278 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1282 *ret
= ((uint8_t*) d
)[0];
1286 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1292 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1296 *ret
= unaligned_read_be16(d
);
1301 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1307 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1311 *ret
= unaligned_read_be32(d
);
1316 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1317 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1324 INIT_REWINDER(rewinder
, p
);
1326 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1330 r
= dns_packet_read(p
, c
, &d
, NULL
);
1334 if (memchr(d
, 0, c
))
1341 if (!utf8_is_valid(t
)) {
1349 *start
= rewinder
.saved_rindex
;
1350 CANCEL_REWINDER(rewinder
);
1355 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1356 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1361 INIT_REWINDER(rewinder
, p
);
1363 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1367 r
= dns_packet_read(p
, c
, ret
, NULL
);
1374 *start
= rewinder
.saved_rindex
;
1375 CANCEL_REWINDER(rewinder
);
1380 int dns_packet_read_name(
1383 bool allow_compression
,
1386 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1387 size_t after_rindex
= 0, jump_barrier
;
1388 _cleanup_free_
char *ret
= NULL
;
1389 size_t n
= 0, allocated
= 0;
1395 INIT_REWINDER(rewinder
, p
);
1396 jump_barrier
= p
->rindex
;
1398 if (p
->refuse_compression
)
1399 allow_compression
= false;
1404 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1415 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1419 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1427 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
1433 } else if (allow_compression
&& (c
& 0xc0) == 0xc0) {
1437 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1441 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1442 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1445 if (after_rindex
== 0)
1446 after_rindex
= p
->rindex
;
1448 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1455 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
1460 if (after_rindex
!= 0)
1461 p
->rindex
= after_rindex
;
1467 *start
= rewinder
.saved_rindex
;
1468 CANCEL_REWINDER(rewinder
);
1473 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1476 const uint8_t *bitmap
;
1480 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1485 INIT_REWINDER(rewinder
, p
);
1487 r
= bitmap_ensure_allocated(types
);
1491 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1495 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1499 if (length
== 0 || length
> 32)
1502 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1506 for (i
= 0; i
< length
; i
++) {
1507 uint8_t bitmask
= 1 << 7;
1518 if (bitmap
[i
] & bitmask
) {
1521 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1523 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1524 if (dns_type_is_pseudo(n
))
1527 r
= bitmap_set(*types
, n
);
1541 *start
= rewinder
.saved_rindex
;
1542 CANCEL_REWINDER(rewinder
);
1547 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1548 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1551 INIT_REWINDER(rewinder
, p
);
1553 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1554 r
= dns_packet_read_type_window(p
, types
, NULL
);
1558 /* don't read past end of current RR */
1559 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1563 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1567 *start
= rewinder
.saved_rindex
;
1568 CANCEL_REWINDER(rewinder
);
1573 int dns_packet_read_key(DnsPacket
*p
, DnsResourceKey
**ret
, bool *ret_cache_flush
, size_t *start
) {
1574 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1575 _cleanup_free_
char *name
= NULL
;
1576 bool cache_flush
= false;
1577 uint16_t class, type
;
1578 DnsResourceKey
*key
;
1583 INIT_REWINDER(rewinder
, p
);
1585 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1589 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1593 r
= dns_packet_read_uint16(p
, &class, NULL
);
1597 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1598 /* See RFC6762, Section 10.2 */
1600 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1601 class &= ~MDNS_RR_CACHE_FLUSH
;
1606 key
= dns_resource_key_new_consume(class, type
, name
);
1613 if (ret_cache_flush
)
1614 *ret_cache_flush
= cache_flush
;
1616 *start
= rewinder
.saved_rindex
;
1617 CANCEL_REWINDER(rewinder
);
1622 static bool loc_size_ok(uint8_t size
) {
1623 uint8_t m
= size
>> 4, e
= size
& 0xF;
1625 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1628 int dns_packet_read_rr(DnsPacket
*p
, DnsResourceRecord
**ret
, bool *ret_cache_flush
, size_t *start
) {
1629 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1630 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1631 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1640 INIT_REWINDER(rewinder
, p
);
1642 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1646 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1649 rr
= dns_resource_record_new(key
);
1653 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1657 /* RFC 2181, Section 8, suggests to
1658 * treat a TTL with the MSB set as a zero TTL. */
1659 if (rr
->ttl
& UINT32_C(0x80000000))
1662 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1666 if (p
->rindex
+ rdlength
> p
->size
)
1671 switch (rr
->key
->type
) {
1674 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1677 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1680 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1683 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1688 case DNS_TYPE_CNAME
:
1689 case DNS_TYPE_DNAME
:
1690 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1693 case DNS_TYPE_HINFO
:
1694 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1698 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1701 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1703 if (rdlength
<= 0) {
1705 /* RFC 6763, section 6.1 suggests to treat
1706 * empty TXT RRs as equivalent to a TXT record
1707 * with a single empty string. */
1709 i
= malloc0(offsetof(DnsTxtItem
, data
) + 1); /* for safety reasons we add an extra NUL byte */
1715 DnsTxtItem
*last
= NULL
;
1717 while (p
->rindex
< offset
+ rdlength
) {
1722 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1726 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1730 memcpy(i
->data
, data
, sz
);
1733 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1742 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1746 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1750 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1754 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1758 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1762 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1766 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1770 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1774 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1778 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1782 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1785 case DNS_TYPE_LOC
: {
1789 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1794 rr
->loc
.version
= t
;
1796 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1800 if (!loc_size_ok(rr
->loc
.size
))
1803 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1807 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1810 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1814 if (!loc_size_ok(rr
->loc
.vert_pre
))
1817 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1821 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1825 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1831 dns_packet_rewind(p
, pos
);
1832 rr
->unparseable
= true;
1838 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1842 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1846 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1850 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1851 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1856 if (rr
->ds
.digest_size
<= 0)
1857 /* the accepted size depends on the algorithm, but for now
1858 just ensure that the value is greater than zero */
1863 case DNS_TYPE_SSHFP
:
1864 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1868 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1872 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1873 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1876 if (rr
->sshfp
.fingerprint_size
<= 0)
1877 /* the accepted size depends on the algorithm, but for now
1878 just ensure that the value is greater than zero */
1883 case DNS_TYPE_DNSKEY
:
1884 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1888 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1892 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1896 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1897 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1900 if (rr
->dnskey
.key_size
<= 0)
1901 /* the accepted size depends on the algorithm, but for now
1902 just ensure that the value is greater than zero */
1907 case DNS_TYPE_RRSIG
:
1908 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1912 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1916 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1920 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1924 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1928 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1932 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1936 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1940 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
1941 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
1944 if (rr
->rrsig
.signature_size
<= 0)
1945 /* the accepted size depends on the algorithm, but for now
1946 just ensure that the value is greater than zero */
1951 case DNS_TYPE_NSEC
: {
1954 * RFC6762, section 18.14 explictly states mDNS should use name compression.
1955 * This contradicts RFC3845, section 2.1.1
1958 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
1960 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
1964 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1966 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1967 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1968 * without the NSEC bit set. */
1972 case DNS_TYPE_NSEC3
: {
1975 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
1979 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
1983 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
1987 /* this may be zero */
1988 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1992 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
1996 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2003 r
= dns_packet_read_memdup(p
, size
,
2004 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
2009 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2011 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
2017 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2021 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2025 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2029 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2030 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2033 if (rr
->tlsa
.data_size
<= 0)
2034 /* the accepted size depends on the algorithm, but for now
2035 just ensure that the value is greater than zero */
2041 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2045 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2049 r
= dns_packet_read_memdup(p
,
2050 rdlength
+ offset
- p
->rindex
,
2051 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2055 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2056 case DNS_TYPE_OPENPGPKEY
:
2059 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2065 if (p
->rindex
!= offset
+ rdlength
)
2071 if (ret_cache_flush
)
2072 *ret_cache_flush
= cache_flush
;
2074 *start
= rewinder
.saved_rindex
;
2075 CANCEL_REWINDER(rewinder
);
2080 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2082 bool found_dau_dhu_n3u
= false;
2085 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2089 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2091 /* Check that the version is 0 */
2092 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2094 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2098 l
= rr
->opt
.data_size
;
2100 uint16_t option_code
, option_length
;
2102 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2106 option_code
= unaligned_read_be16(p
);
2107 option_length
= unaligned_read_be16(p
+ 2);
2109 if (l
< option_length
+ 4U)
2112 /* RFC 6975 DAU, DHU or N3U fields found. */
2113 if (IN_SET(option_code
, 5, 6, 7))
2114 found_dau_dhu_n3u
= true;
2116 p
+= option_length
+ 4U;
2117 l
-= option_length
+ 4U;
2120 *rfc6975
= found_dau_dhu_n3u
;
2124 int dns_packet_extract(DnsPacket
*p
) {
2125 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2126 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2127 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2134 INIT_REWINDER(rewinder
, p
);
2135 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2137 n
= DNS_PACKET_QDCOUNT(p
);
2139 question
= dns_question_new(n
);
2143 for (i
= 0; i
< n
; i
++) {
2144 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2147 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2154 if (!dns_type_is_valid_query(key
->type
))
2157 r
= dns_question_add(question
, key
);
2163 n
= DNS_PACKET_RRCOUNT(p
);
2165 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2166 bool bad_opt
= false;
2168 answer
= dns_answer_new(n
);
2172 for (i
= 0; i
< n
; i
++) {
2173 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2174 bool cache_flush
= false;
2176 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, NULL
);
2180 /* Try to reduce memory usage a bit */
2182 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2184 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2187 if (p
->opt
|| bad_opt
) {
2188 /* Multiple OPT RRs? if so, let's ignore all, because there's something wrong
2189 * with the server, and if one is valid we wouldn't know which one. */
2190 log_debug("Multiple OPT RRs detected, ignoring all.");
2195 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2196 /* If the OPT RR is not owned by the root domain, then it is bad, let's ignore
2198 log_debug("OPT RR is not owned by root domain, ignoring.");
2203 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2204 /* OPT RR is in the wrong section? Some Belkin routers do this. This is a hint
2205 * the EDNS implementation is borked, like the Belkin one is, hence ignore
2207 log_debug("OPT RR in wrong section, ignoring.");
2212 if (!opt_is_good(rr
, &has_rfc6975
)) {
2213 log_debug("Malformed OPT RR, ignoring.");
2218 if (DNS_PACKET_QR(p
)) {
2219 /* Additional checks for responses */
2221 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
)) {
2222 /* If this is a reply and we don't know the EDNS version then something
2224 log_debug("EDNS version newer that our request, bad server.");
2229 /* If the OPT RR contains RFC6975 algorithm data, then this is indication that
2230 * the server just copied the OPT it got from us (which contained that data)
2231 * back into the reply. If so, then it doesn't properly support EDNS, as
2232 * RFC6975 makes it very clear that the algorithm data should only be contained
2233 * in questions, never in replies. Crappy Belkin routers copy the OPT data for
2234 * example, hence let's detect this so that we downgrade early. */
2235 log_debug("OPT RR contained RFC6975 data, ignoring.");
2241 p
->opt
= dns_resource_record_ref(rr
);
2244 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2245 * cached. Hence mark only those RRs as cacheable by default, but not the ones from the
2246 * Additional or Authority sections. */
2248 r
= dns_answer_add(answer
, rr
, p
->ifindex
,
2249 (i
< DNS_PACKET_ANCOUNT(p
) ? DNS_ANSWER_CACHEABLE
: 0) |
2250 (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
? DNS_ANSWER_SHARED_OWNER
: 0));
2255 /* Remember this RR, so that we potentically can merge it's ->key object with the next RR. Note
2256 * that we only do this if we actually decided to keep the RR around. */
2257 dns_resource_record_unref(previous
);
2258 previous
= dns_resource_record_ref(rr
);
2262 p
->opt
= dns_resource_record_unref(p
->opt
);
2265 p
->question
= question
;
2271 p
->extracted
= true;
2273 /* no CANCEL, always rewind */
2277 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2283 /* Checks if the specified packet is a reply for the specified
2284 * key and the specified key is the only one in the question
2287 if (DNS_PACKET_QR(p
) != 1)
2290 /* Let's unpack the packet, if that hasn't happened yet. */
2291 r
= dns_packet_extract(p
);
2298 if (p
->question
->n_keys
!= 1)
2301 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2304 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2305 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2306 [DNS_RCODE_FORMERR
] = "FORMERR",
2307 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2308 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2309 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2310 [DNS_RCODE_REFUSED
] = "REFUSED",
2311 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2312 [DNS_RCODE_YXRRSET
] = "YRRSET",
2313 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2314 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2315 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2316 [DNS_RCODE_BADVERS
] = "BADVERS",
2317 [DNS_RCODE_BADKEY
] = "BADKEY",
2318 [DNS_RCODE_BADTIME
] = "BADTIME",
2319 [DNS_RCODE_BADMODE
] = "BADMODE",
2320 [DNS_RCODE_BADNAME
] = "BADNAME",
2321 [DNS_RCODE_BADALG
] = "BADALG",
2322 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2323 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2325 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2327 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2328 [DNS_PROTOCOL_DNS
] = "dns",
2329 [DNS_PROTOCOL_MDNS
] = "mdns",
2330 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2332 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);