1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "dns-domain.h"
9 #include "memory-util.h"
10 #include "resolved-dns-packet.h"
12 #include "string-table.h"
14 #include "unaligned.h"
18 #define EDNS0_OPT_DO (1<<15)
20 assert_cc(DNS_PACKET_SIZE_START
> DNS_PACKET_HEADER_SIZE
)
22 typedef struct DnsPacketRewinder
{
27 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
29 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
32 #define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
33 #define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
38 size_t min_alloc_dsize
,
45 assert(max_size
>= DNS_PACKET_HEADER_SIZE
);
47 if (max_size
> DNS_PACKET_SIZE_MAX
)
48 max_size
= DNS_PACKET_SIZE_MAX
;
50 /* The caller may not check what is going to be truly allocated, so do not allow to
51 * allocate a DNS packet bigger than DNS_PACKET_SIZE_MAX.
53 if (min_alloc_dsize
> DNS_PACKET_SIZE_MAX
)
54 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
55 "Requested packet data size too big: %zu",
58 /* When dns_packet_new() is called with min_alloc_dsize == 0, allocate more than the
59 * absolute minimum (which is the dns packet header size), to avoid
60 * resizing immediately again after appending the first data to the packet.
62 if (min_alloc_dsize
< DNS_PACKET_HEADER_SIZE
)
63 a
= DNS_PACKET_SIZE_START
;
67 /* round up to next page size */
68 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
70 /* make sure we never allocate more than useful */
74 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
78 p
->size
= p
->rindex
= DNS_PACKET_HEADER_SIZE
;
80 p
->max_size
= max_size
;
81 p
->protocol
= protocol
;
82 p
->opt_start
= p
->opt_size
= (size_t) -1;
90 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
96 h
= DNS_PACKET_HEADER(p
);
99 case DNS_PROTOCOL_LLMNR
:
102 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
113 case DNS_PROTOCOL_MDNS
:
114 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
118 0 /* rd (ask for recursion) */,
128 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
132 1 /* rd (ask for recursion) */,
135 dnssec_checking_disabled
/* cd */,
140 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t min_alloc_dsize
, bool dnssec_checking_disabled
) {
146 r
= dns_packet_new(&p
, protocol
, min_alloc_dsize
, DNS_PACKET_SIZE_MAX
);
150 /* Always set the TC bit to 0 initially.
151 * If there are multiple packets later, we'll update the bit shortly before sending.
153 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
159 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
164 assert(!p
->on_stack
);
166 assert(p
->n_ref
> 0);
171 static void dns_packet_free(DnsPacket
*p
) {
176 dns_question_unref(p
->question
);
177 dns_answer_unref(p
->answer
);
178 dns_resource_record_unref(p
->opt
);
180 while ((s
= hashmap_steal_first_key(p
->names
)))
182 hashmap_free(p
->names
);
190 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
194 assert(p
->n_ref
> 0);
196 dns_packet_unref(p
->more
);
206 int dns_packet_validate(DnsPacket
*p
) {
209 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
212 if (p
->size
> DNS_PACKET_SIZE_MAX
)
218 int dns_packet_validate_reply(DnsPacket
*p
) {
223 r
= dns_packet_validate(p
);
227 if (DNS_PACKET_QR(p
) != 1)
230 if (DNS_PACKET_OPCODE(p
) != 0)
233 switch (p
->protocol
) {
235 case DNS_PROTOCOL_LLMNR
:
236 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
237 if (DNS_PACKET_QDCOUNT(p
) != 1)
242 case DNS_PROTOCOL_MDNS
:
243 /* RFC 6762, Section 18 */
244 if (DNS_PACKET_RCODE(p
) != 0)
256 int dns_packet_validate_query(DnsPacket
*p
) {
261 r
= dns_packet_validate(p
);
265 if (DNS_PACKET_QR(p
) != 0)
268 if (DNS_PACKET_OPCODE(p
) != 0)
271 if (DNS_PACKET_TC(p
))
274 switch (p
->protocol
) {
276 case DNS_PROTOCOL_LLMNR
:
277 case DNS_PROTOCOL_DNS
:
278 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
279 if (DNS_PACKET_QDCOUNT(p
) != 1)
282 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
283 if (DNS_PACKET_ANCOUNT(p
) > 0)
286 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
287 if (DNS_PACKET_NSCOUNT(p
) > 0)
292 case DNS_PROTOCOL_MDNS
:
293 /* RFC 6762, Section 18 */
294 if (DNS_PACKET_AA(p
) != 0 ||
295 DNS_PACKET_RD(p
) != 0 ||
296 DNS_PACKET_RA(p
) != 0 ||
297 DNS_PACKET_AD(p
) != 0 ||
298 DNS_PACKET_CD(p
) != 0 ||
299 DNS_PACKET_RCODE(p
) != 0)
311 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
314 if (p
->size
+ add
> p
->allocated
) {
317 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
319 ms
= dns_packet_size_max(p
);
323 if (p
->size
+ add
> a
)
329 d
= realloc(p
->_data
, a
);
335 p
->_data
= malloc(a
);
339 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
340 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
350 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
356 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
366 HASHMAP_FOREACH_KEY(n
, s
, p
->names
, i
) {
368 if (PTR_TO_SIZE(n
) < sz
)
371 hashmap_remove(p
->names
, s
);
378 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
384 r
= dns_packet_extend(p
, l
, &q
, start
);
388 memcpy_safe(q
, d
, l
);
392 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
398 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
402 ((uint8_t*) d
)[0] = v
;
407 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
413 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
417 unaligned_write_be16(d
, v
);
422 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
428 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
432 unaligned_write_be32(d
, v
);
437 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
441 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
444 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
449 assert(s
|| size
== 0);
454 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
458 ((uint8_t*) d
)[0] = (uint8_t) size
;
460 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
465 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
469 /* Append a label to a packet. Optionally, does this in DNSSEC
470 * canonical form, if this label is marked as a candidate for
471 * it, and the canonical form logic is enabled for the
477 if (l
> DNS_LABEL_MAX
)
480 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
484 *(w
++) = (uint8_t) l
;
486 if (p
->canonical_form
&& canonical_candidate
) {
489 /* Generate in canonical form, as defined by DNSSEC
490 * RFC 4034, Section 6.2, i.e. all lower-case. */
492 for (i
= 0; i
< l
; i
++)
493 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
495 /* Otherwise, just copy the string unaltered. This is
496 * essential for DNS-SD, where the casing of labels
497 * matters and needs to be retained. */
503 int dns_packet_append_name(
506 bool allow_compression
,
507 bool canonical_candidate
,
516 if (p
->refuse_compression
)
517 allow_compression
= false;
519 saved_size
= p
->size
;
521 while (!dns_name_is_root(name
)) {
522 const char *z
= name
;
523 char label
[DNS_LABEL_MAX
];
526 if (allow_compression
)
527 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
532 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
540 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
544 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
548 if (allow_compression
) {
549 _cleanup_free_
char *s
= NULL
;
557 r
= hashmap_ensure_allocated(&p
->names
, &dns_name_hash_ops
);
561 r
= hashmap_put(p
->names
, s
, SIZE_TO_PTR(n
));
569 r
= dns_packet_append_uint8(p
, 0, NULL
);
580 dns_packet_truncate(p
, saved_size
);
584 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
592 saved_size
= p
->size
;
594 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
598 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
602 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH
: k
->class;
603 r
= dns_packet_append_uint16(p
, class, NULL
);
613 dns_packet_truncate(p
, saved_size
);
617 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
625 saved_size
= p
->size
;
627 r
= dns_packet_append_uint8(p
, window
, NULL
);
631 r
= dns_packet_append_uint8(p
, length
, NULL
);
635 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
644 dns_packet_truncate(p
, saved_size
);
648 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
652 uint8_t bitmaps
[32] = {};
659 saved_size
= p
->size
;
661 BITMAP_FOREACH(n
, types
, i
) {
664 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
665 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
675 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
678 if (bitmaps
[entry
/ 8] != 0) {
679 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
689 dns_packet_truncate(p
, saved_size
);
693 /* Append the OPT pseudo-RR described in RFC6891 */
694 int dns_packet_append_opt(DnsPacket
*p
, uint16_t max_udp_size
, bool edns0_do
, int rcode
, size_t *start
) {
699 /* we must never advertise supported packet size smaller than the legacy max */
700 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
702 assert(rcode
<= _DNS_RCODE_MAX
);
704 if (p
->opt_start
!= (size_t) -1)
707 assert(p
->opt_size
== (size_t) -1);
709 saved_size
= p
->size
;
712 r
= dns_packet_append_uint8(p
, 0, NULL
);
717 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
721 /* class: maximum udp packet that can be received */
722 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
726 /* extended RCODE and VERSION */
727 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
731 /* flags: DNSSEC OK (DO), see RFC3225 */
732 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
737 if (edns0_do
&& !DNS_PACKET_QR(p
)) {
738 /* If DO is on and this is not a reply, also append RFC6975 Algorithm data */
740 static const uint8_t rfc6975
[] = {
742 0, 5, /* OPTION_CODE: DAU */
743 #if HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600
744 0, 7, /* LIST_LENGTH */
746 0, 6, /* LIST_LENGTH */
748 DNSSEC_ALGORITHM_RSASHA1
,
749 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
750 DNSSEC_ALGORITHM_RSASHA256
,
751 DNSSEC_ALGORITHM_RSASHA512
,
752 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
753 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
754 #if HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600
755 DNSSEC_ALGORITHM_ED25519
,
758 0, 6, /* OPTION_CODE: DHU */
759 0, 3, /* LIST_LENGTH */
761 DNSSEC_DIGEST_SHA256
,
762 DNSSEC_DIGEST_SHA384
,
764 0, 7, /* OPTION_CODE: N3U */
765 0, 1, /* LIST_LENGTH */
766 NSEC3_ALGORITHM_SHA1
,
769 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
);
773 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
);
775 r
= dns_packet_append_uint16(p
, 0, NULL
);
779 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
781 p
->opt_start
= saved_size
;
782 p
->opt_size
= p
->size
- saved_size
;
790 dns_packet_truncate(p
, saved_size
);
794 int dns_packet_truncate_opt(DnsPacket
*p
) {
797 if (p
->opt_start
== (size_t) -1) {
798 assert(p
->opt_size
== (size_t) -1);
802 assert(p
->opt_size
!= (size_t) -1);
803 assert(DNS_PACKET_ARCOUNT(p
) > 0);
805 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
808 dns_packet_truncate(p
, p
->opt_start
);
809 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
810 p
->opt_start
= p
->opt_size
= (size_t) -1;
815 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
817 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
824 saved_size
= p
->size
;
826 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
830 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
831 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
835 /* Initially we write 0 here */
836 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
840 rds
= p
->size
- saved_size
;
842 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
845 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
849 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
853 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
857 /* RFC 2782 states "Unless and until permitted by future standards
858 * action, name compression is not to be used for this field." */
859 r
= dns_packet_append_name(p
, rr
->srv
.name
, false, false, NULL
);
866 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, false, NULL
);
870 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
874 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
877 case DNS_TYPE_SPF
: /* exactly the same as TXT */
880 if (!rr
->txt
.items
) {
881 /* RFC 6763, section 6.1 suggests to generate
882 * single empty string for an empty array. */
884 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
890 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
891 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
901 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
905 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
909 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, false, NULL
);
913 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, false, NULL
);
917 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
921 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
925 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
929 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
933 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
937 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
941 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, false, NULL
);
945 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
949 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
953 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
957 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
961 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
965 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
969 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
973 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
977 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
981 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
985 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
989 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
993 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
997 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
1000 case DNS_TYPE_DNSKEY
:
1001 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
1005 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
1009 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
1013 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1016 case DNS_TYPE_RRSIG
:
1017 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1021 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1025 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1029 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1033 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1037 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1041 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1045 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1049 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1053 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1057 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1063 case DNS_TYPE_NSEC3
:
1064 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1068 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1072 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1076 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1080 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1084 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1088 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1092 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1099 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1103 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1107 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1111 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1115 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1119 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1123 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1127 case DNS_TYPE_OPENPGPKEY
:
1128 case _DNS_TYPE_INVALID
: /* unparseable */
1131 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1137 /* Let's calculate the actual data size and update the field */
1138 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1139 if (rdlength
> 0xFFFF) {
1145 p
->size
= rdlength_offset
;
1146 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1152 *start
= saved_size
;
1160 dns_packet_truncate(p
, saved_size
);
1164 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1165 DnsResourceKey
*key
;
1170 DNS_QUESTION_FOREACH(key
, q
) {
1171 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1179 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
) {
1180 DnsResourceRecord
*rr
;
1181 DnsAnswerFlags flags
;
1186 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1187 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1195 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1198 if (p
->rindex
+ sz
> p
->size
)
1202 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1211 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1213 assert(idx
<= p
->size
);
1214 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1219 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1226 r
= dns_packet_read(p
, sz
, &q
, start
);
1234 static int dns_packet_read_memdup(
1235 DnsPacket
*p
, size_t size
,
1236 void **ret
, size_t *ret_size
,
1237 size_t *ret_start
) {
1246 r
= dns_packet_read(p
, size
, &src
, &start
);
1255 copy
= memdup(src
, size
);
1270 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1276 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1280 *ret
= ((uint8_t*) d
)[0];
1284 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1290 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1294 *ret
= unaligned_read_be16(d
);
1299 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1305 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1309 *ret
= unaligned_read_be32(d
);
1314 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1315 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1322 INIT_REWINDER(rewinder
, p
);
1324 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1328 r
= dns_packet_read(p
, c
, &d
, NULL
);
1332 if (memchr(d
, 0, c
))
1339 if (!utf8_is_valid(t
)) {
1347 *start
= rewinder
.saved_rindex
;
1348 CANCEL_REWINDER(rewinder
);
1353 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1354 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1359 INIT_REWINDER(rewinder
, p
);
1361 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1365 r
= dns_packet_read(p
, c
, ret
, NULL
);
1372 *start
= rewinder
.saved_rindex
;
1373 CANCEL_REWINDER(rewinder
);
1378 int dns_packet_read_name(
1381 bool allow_compression
,
1384 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1385 size_t after_rindex
= 0, jump_barrier
;
1386 _cleanup_free_
char *ret
= NULL
;
1387 size_t n
= 0, allocated
= 0;
1393 INIT_REWINDER(rewinder
, p
);
1394 jump_barrier
= p
->rindex
;
1396 if (p
->refuse_compression
)
1397 allow_compression
= false;
1402 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1413 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1417 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1425 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
1431 } else if (allow_compression
&& (c
& 0xc0) == 0xc0) {
1435 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1439 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1440 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1443 if (after_rindex
== 0)
1444 after_rindex
= p
->rindex
;
1446 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1453 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
1458 if (after_rindex
!= 0)
1459 p
->rindex
= after_rindex
;
1461 *_ret
= TAKE_PTR(ret
);
1464 *start
= rewinder
.saved_rindex
;
1465 CANCEL_REWINDER(rewinder
);
1470 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1473 const uint8_t *bitmap
;
1477 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1482 INIT_REWINDER(rewinder
, p
);
1484 r
= bitmap_ensure_allocated(types
);
1488 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1492 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1496 if (length
== 0 || length
> 32)
1499 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1503 for (i
= 0; i
< length
; i
++) {
1504 uint8_t bitmask
= 1 << 7;
1514 for (; bitmask
; bit
++, bitmask
>>= 1)
1515 if (bitmap
[i
] & bitmask
) {
1518 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1520 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1521 if (dns_type_is_pseudo(n
))
1524 r
= bitmap_set(*types
, n
);
1534 *start
= rewinder
.saved_rindex
;
1535 CANCEL_REWINDER(rewinder
);
1540 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1541 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1544 INIT_REWINDER(rewinder
, p
);
1546 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1547 r
= dns_packet_read_type_window(p
, types
, NULL
);
1551 /* don't read past end of current RR */
1552 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1556 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1560 *start
= rewinder
.saved_rindex
;
1561 CANCEL_REWINDER(rewinder
);
1566 int dns_packet_read_key(DnsPacket
*p
, DnsResourceKey
**ret
, bool *ret_cache_flush
, size_t *start
) {
1567 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1568 _cleanup_free_
char *name
= NULL
;
1569 bool cache_flush
= false;
1570 uint16_t class, type
;
1571 DnsResourceKey
*key
;
1576 INIT_REWINDER(rewinder
, p
);
1578 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1582 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1586 r
= dns_packet_read_uint16(p
, &class, NULL
);
1590 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1591 /* See RFC6762, Section 10.2 */
1593 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1594 class &= ~MDNS_RR_CACHE_FLUSH
;
1599 key
= dns_resource_key_new_consume(class, type
, name
);
1606 if (ret_cache_flush
)
1607 *ret_cache_flush
= cache_flush
;
1609 *start
= rewinder
.saved_rindex
;
1610 CANCEL_REWINDER(rewinder
);
1615 static bool loc_size_ok(uint8_t size
) {
1616 uint8_t m
= size
>> 4, e
= size
& 0xF;
1618 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1621 int dns_packet_read_rr(DnsPacket
*p
, DnsResourceRecord
**ret
, bool *ret_cache_flush
, size_t *start
) {
1622 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1623 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1624 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1633 INIT_REWINDER(rewinder
, p
);
1635 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1639 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1642 rr
= dns_resource_record_new(key
);
1646 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1650 /* RFC 2181, Section 8, suggests to
1651 * treat a TTL with the MSB set as a zero TTL. */
1652 if (rr
->ttl
& UINT32_C(0x80000000))
1655 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1659 if (p
->rindex
+ rdlength
> p
->size
)
1664 switch (rr
->key
->type
) {
1667 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1670 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1673 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1676 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1681 case DNS_TYPE_CNAME
:
1682 case DNS_TYPE_DNAME
:
1683 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1686 case DNS_TYPE_HINFO
:
1687 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1691 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1694 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1696 if (rdlength
<= 0) {
1697 r
= dns_txt_item_new_empty(&rr
->txt
.items
);
1701 DnsTxtItem
*last
= NULL
;
1703 while (p
->rindex
< offset
+ rdlength
) {
1708 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1712 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1716 memcpy(i
->data
, data
, sz
);
1719 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1728 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1732 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1736 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1740 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1744 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1748 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1752 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1756 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1760 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1764 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1768 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1771 case DNS_TYPE_LOC
: {
1775 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1780 rr
->loc
.version
= t
;
1782 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1786 if (!loc_size_ok(rr
->loc
.size
))
1789 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1793 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1796 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1800 if (!loc_size_ok(rr
->loc
.vert_pre
))
1803 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1807 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1811 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1817 dns_packet_rewind(p
, pos
);
1818 rr
->unparseable
= true;
1824 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1828 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1832 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1839 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1840 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1845 if (rr
->ds
.digest_size
<= 0)
1846 /* the accepted size depends on the algorithm, but for now
1847 just ensure that the value is greater than zero */
1852 case DNS_TYPE_SSHFP
:
1853 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1857 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1864 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1865 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1868 if (rr
->sshfp
.fingerprint_size
<= 0)
1869 /* the accepted size depends on the algorithm, but for now
1870 just ensure that the value is greater than zero */
1875 case DNS_TYPE_DNSKEY
:
1876 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1880 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1884 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1891 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1892 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1895 if (rr
->dnskey
.key_size
<= 0)
1896 /* the accepted size depends on the algorithm, but for now
1897 just ensure that the value is greater than zero */
1902 case DNS_TYPE_RRSIG
:
1903 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1907 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1911 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1915 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1919 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1923 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1927 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1931 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1935 if (rdlength
+ offset
< p
->rindex
)
1938 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
1939 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
1942 if (rr
->rrsig
.signature_size
<= 0)
1943 /* the accepted size depends on the algorithm, but for now
1944 just ensure that the value is greater than zero */
1949 case DNS_TYPE_NSEC
: {
1952 * RFC6762, section 18.14 explicitly states mDNS should use name compression.
1953 * This contradicts RFC3845, section 2.1.1
1956 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
1958 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
1962 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1964 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1965 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1966 * without the NSEC bit set. */
1970 case DNS_TYPE_NSEC3
: {
1973 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
1977 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
1981 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
1985 /* this may be zero */
1986 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1990 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
1994 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2001 r
= dns_packet_read_memdup(p
, size
,
2002 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
2007 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2009 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
2015 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2019 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2023 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2030 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2031 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2034 if (rr
->tlsa
.data_size
<= 0)
2035 /* the accepted size depends on the algorithm, but for now
2036 just ensure that the value is greater than zero */
2042 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2046 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2050 if (rdlength
+ offset
< p
->rindex
)
2053 r
= dns_packet_read_memdup(p
,
2054 rdlength
+ offset
- p
->rindex
,
2055 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2059 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2060 case DNS_TYPE_OPENPGPKEY
:
2063 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2069 if (p
->rindex
!= offset
+ rdlength
)
2072 *ret
= TAKE_PTR(rr
);
2074 if (ret_cache_flush
)
2075 *ret_cache_flush
= cache_flush
;
2077 *start
= rewinder
.saved_rindex
;
2078 CANCEL_REWINDER(rewinder
);
2083 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2085 bool found_dau_dhu_n3u
= false;
2088 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2092 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2094 /* Check that the version is 0 */
2095 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2097 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2101 l
= rr
->opt
.data_size
;
2103 uint16_t option_code
, option_length
;
2105 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2109 option_code
= unaligned_read_be16(p
);
2110 option_length
= unaligned_read_be16(p
+ 2);
2112 if (l
< option_length
+ 4U)
2115 /* RFC 6975 DAU, DHU or N3U fields found. */
2116 if (IN_SET(option_code
, 5, 6, 7))
2117 found_dau_dhu_n3u
= true;
2119 p
+= option_length
+ 4U;
2120 l
-= option_length
+ 4U;
2123 *rfc6975
= found_dau_dhu_n3u
;
2127 static int dns_packet_extract_question(DnsPacket
*p
, DnsQuestion
**ret_question
) {
2128 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2132 n
= DNS_PACKET_QDCOUNT(p
);
2134 question
= dns_question_new(n
);
2138 _cleanup_set_free_ Set
*keys
= NULL
; /* references to keys are kept by Question */
2140 keys
= set_new(&dns_resource_key_hash_ops
);
2144 r
= set_reserve(keys
, n
* 2); /* Higher multipliers give slightly higher efficiency through
2145 * hash collisions, but the gains quickly drop of after 2. */
2149 for (i
= 0; i
< n
; i
++) {
2150 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2153 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2160 if (!dns_type_is_valid_query(key
->type
))
2163 r
= set_put(keys
, key
);
2167 /* Already in the Question, let's skip */
2170 r
= dns_question_add_raw(question
, key
);
2176 *ret_question
= TAKE_PTR(question
);
2181 static int dns_packet_extract_answer(DnsPacket
*p
, DnsAnswer
**ret_answer
) {
2182 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2184 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2185 bool bad_opt
= false;
2188 n
= DNS_PACKET_RRCOUNT(p
);
2192 answer
= dns_answer_new(n
);
2196 for (i
= 0; i
< n
; i
++) {
2197 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2198 bool cache_flush
= false;
2200 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, NULL
);
2204 /* Try to reduce memory usage a bit */
2206 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2208 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2211 if (p
->opt
|| bad_opt
) {
2212 /* Multiple OPT RRs? if so, let's ignore all, because there's
2213 * something wrong with the server, and if one is valid we wouldn't
2214 * know which one. */
2215 log_debug("Multiple OPT RRs detected, ignoring all.");
2220 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2221 /* If the OPT RR is not owned by the root domain, then it is bad,
2222 * let's ignore it. */
2223 log_debug("OPT RR is not owned by root domain, ignoring.");
2228 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2229 /* OPT RR is in the wrong section? Some Belkin routers do this. This
2230 * is a hint the EDNS implementation is borked, like the Belkin one
2231 * is, hence ignore it. */
2232 log_debug("OPT RR in wrong section, ignoring.");
2237 if (!opt_is_good(rr
, &has_rfc6975
)) {
2238 log_debug("Malformed OPT RR, ignoring.");
2243 if (DNS_PACKET_QR(p
)) {
2244 /* Additional checks for responses */
2246 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
)) {
2247 /* If this is a reply and we don't know the EDNS version
2248 * then something is weird... */
2249 log_debug("EDNS version newer that our request, bad server.");
2254 /* If the OPT RR contains RFC6975 algorithm data, then this
2255 * is indication that the server just copied the OPT it got
2256 * from us (which contained that data) back into the reply.
2257 * If so, then it doesn't properly support EDNS, as RFC6975
2258 * makes it very clear that the algorithm data should only
2259 * be contained in questions, never in replies. Crappy
2260 * Belkin routers copy the OPT data for example, hence let's
2261 * detect this so that we downgrade early. */
2262 log_debug("OPT RR contains RFC6975 data, ignoring.");
2268 p
->opt
= dns_resource_record_ref(rr
);
2270 /* According to RFC 4795, section 2.9. only the RRs from the Answer section
2271 * shall be cached. Hence mark only those RRs as cacheable by default, but
2272 * not the ones from the Additional or Authority sections. */
2273 DnsAnswerFlags flags
=
2274 (i
< DNS_PACKET_ANCOUNT(p
) ? DNS_ANSWER_CACHEABLE
: 0) |
2275 (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
? DNS_ANSWER_SHARED_OWNER
: 0);
2277 r
= dns_answer_add(answer
, rr
, p
->ifindex
, flags
);
2282 /* Remember this RR, so that we potentically can merge it's ->key object with the
2283 * next RR. Note that we only do this if we actually decided to keep the RR around.
2285 dns_resource_record_unref(previous
);
2286 previous
= dns_resource_record_ref(rr
);
2290 p
->opt
= dns_resource_record_unref(p
->opt
);
2292 *ret_answer
= TAKE_PTR(answer
);
2297 int dns_packet_extract(DnsPacket
*p
) {
2298 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2299 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2300 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2306 INIT_REWINDER(rewinder
, p
);
2307 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2309 r
= dns_packet_extract_question(p
, &question
);
2313 r
= dns_packet_extract_answer(p
, &answer
);
2317 p
->question
= TAKE_PTR(question
);
2318 p
->answer
= TAKE_PTR(answer
);
2320 p
->extracted
= true;
2322 /* no CANCEL, always rewind */
2326 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2332 /* Checks if the specified packet is a reply for the specified
2333 * key and the specified key is the only one in the question
2336 if (DNS_PACKET_QR(p
) != 1)
2339 /* Let's unpack the packet, if that hasn't happened yet. */
2340 r
= dns_packet_extract(p
);
2347 if (p
->question
->n_keys
!= 1)
2350 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2353 static void dns_packet_hash_func(const DnsPacket
*s
, struct siphash
*state
) {
2356 siphash24_compress(&s
->size
, sizeof(s
->size
), state
);
2357 siphash24_compress(DNS_PACKET_DATA((DnsPacket
*) s
), s
->size
, state
);
2360 static int dns_packet_compare_func(const DnsPacket
*x
, const DnsPacket
*y
) {
2363 r
= CMP(x
->size
, y
->size
);
2367 return memcmp(DNS_PACKET_DATA((DnsPacket
*) x
), DNS_PACKET_DATA((DnsPacket
*) y
), x
->size
);
2370 DEFINE_HASH_OPS(dns_packet_hash_ops
, DnsPacket
, dns_packet_hash_func
, dns_packet_compare_func
);
2372 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2373 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2374 [DNS_RCODE_FORMERR
] = "FORMERR",
2375 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2376 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2377 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2378 [DNS_RCODE_REFUSED
] = "REFUSED",
2379 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2380 [DNS_RCODE_YXRRSET
] = "YRRSET",
2381 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2382 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2383 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2384 [DNS_RCODE_BADVERS
] = "BADVERS",
2385 [DNS_RCODE_BADKEY
] = "BADKEY",
2386 [DNS_RCODE_BADTIME
] = "BADTIME",
2387 [DNS_RCODE_BADMODE
] = "BADMODE",
2388 [DNS_RCODE_BADNAME
] = "BADNAME",
2389 [DNS_RCODE_BADALG
] = "BADALG",
2390 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2391 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2393 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2395 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2396 [DNS_PROTOCOL_DNS
] = "dns",
2397 [DNS_PROTOCOL_MDNS
] = "mdns",
2398 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2400 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);