1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "dns-domain.h"
10 #include "memory-util.h"
11 #include "resolved-dns-packet.h"
13 #include "stdio-util.h"
14 #include "string-table.h"
16 #include "unaligned.h"
19 #define EDNS0_OPT_DO (1<<15)
21 assert_cc(DNS_PACKET_SIZE_START
> DNS_PACKET_HEADER_SIZE
);
23 typedef struct DnsPacketRewinder
{
28 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
30 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
33 #define REWINDER_INIT(p) { \
35 .saved_rindex = (p)->rindex, \
37 #define CANCEL_REWINDER(rewinder) do { (rewinder).packet = NULL; } while (0)
42 size_t min_alloc_dsize
,
49 assert(max_size
>= DNS_PACKET_HEADER_SIZE
);
51 if (max_size
> DNS_PACKET_SIZE_MAX
)
52 max_size
= DNS_PACKET_SIZE_MAX
;
54 /* The caller may not check what is going to be truly allocated, so do not allow to
55 * allocate a DNS packet bigger than DNS_PACKET_SIZE_MAX.
57 if (min_alloc_dsize
> DNS_PACKET_SIZE_MAX
)
58 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
59 "Requested packet data size too big: %zu",
62 /* When dns_packet_new() is called with min_alloc_dsize == 0, allocate more than the
63 * absolute minimum (which is the dns packet header size), to avoid
64 * resizing immediately again after appending the first data to the packet.
66 if (min_alloc_dsize
< DNS_PACKET_HEADER_SIZE
)
67 a
= DNS_PACKET_SIZE_START
;
71 /* round up to next page size */
72 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
74 /* make sure we never allocate more than useful */
78 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
85 .size
= DNS_PACKET_HEADER_SIZE
,
86 .rindex
= DNS_PACKET_HEADER_SIZE
,
89 .opt_start
= SIZE_MAX
,
98 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
104 h
= DNS_PACKET_HEADER(p
);
106 switch (p
->protocol
) {
107 case DNS_PROTOCOL_LLMNR
:
110 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
121 case DNS_PROTOCOL_MDNS
:
122 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
126 0 /* rd (ask for recursion) */,
136 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
140 1 /* rd (ask for recursion) */,
143 dnssec_checking_disabled
/* cd */,
148 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t min_alloc_dsize
, bool dnssec_checking_disabled
) {
154 r
= dns_packet_new(&p
, protocol
, min_alloc_dsize
, DNS_PACKET_SIZE_MAX
);
158 /* Always set the TC bit to 0 initially.
159 * If there are multiple packets later, we'll update the bit shortly before sending.
161 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
167 int dns_packet_dup(DnsPacket
**ret
, DnsPacket
*p
) {
174 r
= dns_packet_validate(p
);
178 c
= malloc(ALIGN(sizeof(DnsPacket
)) + p
->size
);
184 .protocol
= p
->protocol
,
186 .rindex
= DNS_PACKET_HEADER_SIZE
,
187 .allocated
= p
->size
,
188 .max_size
= p
->max_size
,
189 .opt_start
= SIZE_MAX
,
190 .opt_size
= SIZE_MAX
,
193 memcpy(DNS_PACKET_DATA(c
), DNS_PACKET_DATA(p
), p
->size
);
199 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
204 assert(!p
->on_stack
);
206 assert(p
->n_ref
> 0);
211 static void dns_packet_free(DnsPacket
*p
) {
216 dns_question_unref(p
->question
);
217 dns_answer_unref(p
->answer
);
218 dns_resource_record_unref(p
->opt
);
220 while ((s
= hashmap_steal_first_key(p
->names
)))
222 hashmap_free(p
->names
);
230 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
234 assert(p
->n_ref
> 0);
236 dns_packet_unref(p
->more
);
246 int dns_packet_validate(DnsPacket
*p
) {
249 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
252 if (p
->size
> DNS_PACKET_SIZE_MAX
)
258 int dns_packet_validate_reply(DnsPacket
*p
) {
263 r
= dns_packet_validate(p
);
267 if (DNS_PACKET_QR(p
) != 1)
270 if (DNS_PACKET_OPCODE(p
) != 0)
273 switch (p
->protocol
) {
275 case DNS_PROTOCOL_LLMNR
:
276 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
277 if (DNS_PACKET_QDCOUNT(p
) != 1)
282 case DNS_PROTOCOL_MDNS
:
283 /* RFC 6762, Section 18 */
284 if (DNS_PACKET_RCODE(p
) != 0)
296 int dns_packet_validate_query(DnsPacket
*p
) {
301 r
= dns_packet_validate(p
);
305 if (DNS_PACKET_QR(p
) != 0)
308 if (DNS_PACKET_OPCODE(p
) != 0)
311 switch (p
->protocol
) {
313 case DNS_PROTOCOL_LLMNR
:
314 case DNS_PROTOCOL_DNS
:
315 if (DNS_PACKET_TC(p
)) /* mDNS query may have truncation flag. */
318 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
319 if (DNS_PACKET_QDCOUNT(p
) != 1)
322 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
323 if (DNS_PACKET_ANCOUNT(p
) > 0)
326 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
327 if (DNS_PACKET_NSCOUNT(p
) > 0)
332 case DNS_PROTOCOL_MDNS
:
333 /* RFC 6762, Section 18 specifies that messages with non-zero RCODE
334 * must be silently ignored, and that we must ignore the values of
335 * AA, RD, RA, AD, and CD bits. */
336 if (DNS_PACKET_RCODE(p
) != 0)
348 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
351 if (p
->size
+ add
> p
->allocated
) {
354 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
356 ms
= dns_packet_size_max(p
);
360 if (p
->size
+ add
> a
)
366 d
= realloc(p
->_data
, a
);
372 p
->_data
= malloc(a
);
376 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
377 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
387 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
393 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
402 HASHMAP_FOREACH_KEY(n
, s
, p
->names
) {
404 if (PTR_TO_SIZE(n
) < sz
)
407 hashmap_remove(p
->names
, s
);
414 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
420 r
= dns_packet_extend(p
, l
, &q
, start
);
424 memcpy_safe(q
, d
, l
);
428 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
434 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
438 ((uint8_t*) d
)[0] = v
;
443 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
449 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
453 unaligned_write_be16(d
, v
);
458 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
464 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
468 unaligned_write_be32(d
, v
);
473 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
477 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
480 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
485 assert(s
|| size
== 0);
490 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
494 ((uint8_t*) d
)[0] = (uint8_t) size
;
496 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
501 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
505 /* Append a label to a packet. Optionally, does this in DNSSEC
506 * canonical form, if this label is marked as a candidate for
507 * it, and the canonical form logic is enabled for the
513 if (l
> DNS_LABEL_MAX
)
516 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
520 *(w
++) = (uint8_t) l
;
522 if (p
->canonical_form
&& canonical_candidate
)
523 /* Generate in canonical form, as defined by DNSSEC
524 * RFC 4034, Section 6.2, i.e. all lower-case. */
525 for (size_t i
= 0; i
< l
; i
++)
526 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
528 /* Otherwise, just copy the string unaltered. This is
529 * essential for DNS-SD, where the casing of labels
530 * matters and needs to be retained. */
536 int dns_packet_append_name(
539 bool allow_compression
,
540 bool canonical_candidate
,
549 if (p
->refuse_compression
)
550 allow_compression
= false;
552 saved_size
= p
->size
;
554 while (!dns_name_is_root(name
)) {
555 const char *z
= name
;
556 char label
[DNS_LABEL_MAX
+1];
559 if (allow_compression
)
560 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
565 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
573 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
577 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
581 if (allow_compression
) {
582 _cleanup_free_
char *s
= NULL
;
590 r
= hashmap_ensure_put(&p
->names
, &dns_name_hash_ops
, s
, SIZE_TO_PTR(n
));
598 r
= dns_packet_append_uint8(p
, 0, NULL
);
609 dns_packet_truncate(p
, saved_size
);
613 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
621 saved_size
= p
->size
;
623 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
627 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
631 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH_OR_QU
: k
->class;
632 r
= dns_packet_append_uint16(p
, class, NULL
);
642 dns_packet_truncate(p
, saved_size
);
646 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
654 saved_size
= p
->size
;
656 r
= dns_packet_append_uint8(p
, window
, NULL
);
660 r
= dns_packet_append_uint8(p
, length
, NULL
);
664 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
673 dns_packet_truncate(p
, saved_size
);
677 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
680 uint8_t bitmaps
[32] = {};
687 saved_size
= p
->size
;
689 BITMAP_FOREACH(n
, types
) {
692 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
693 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
703 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
706 if (bitmaps
[entry
/ 8] != 0) {
707 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
717 dns_packet_truncate(p
, saved_size
);
721 /* Append the OPT pseudo-RR described in RFC6891 */
722 int dns_packet_append_opt(
724 uint16_t max_udp_size
,
726 bool include_rfc6975
,
735 /* we must never advertise supported packet size smaller than the legacy max */
736 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
738 assert(rcode
<= _DNS_RCODE_MAX
);
740 if (p
->opt_start
!= SIZE_MAX
)
743 assert(p
->opt_size
== SIZE_MAX
);
745 saved_size
= p
->size
;
748 r
= dns_packet_append_uint8(p
, 0, NULL
);
753 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
757 /* class: maximum udp packet that can be received */
758 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
762 /* extended RCODE and VERSION */
763 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
767 /* flags: DNSSEC OK (DO), see RFC3225 */
768 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
772 if (edns0_do
&& include_rfc6975
) {
773 /* If DO is on and this is requested, also append RFC6975 Algorithm data. This is supposed to
774 * be done on queries, not on replies, hencer callers should turn this off when finishing off
777 static const uint8_t rfc6975
[] = {
779 0, DNS_EDNS_OPT_DAU
, /* OPTION_CODE */
780 #if PREFER_OPENSSL || (HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600)
781 0, 7, /* LIST_LENGTH */
783 0, 6, /* LIST_LENGTH */
785 DNSSEC_ALGORITHM_RSASHA1
,
786 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
787 DNSSEC_ALGORITHM_RSASHA256
,
788 DNSSEC_ALGORITHM_RSASHA512
,
789 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
790 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
791 #if PREFER_OPENSSL || (HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600)
792 DNSSEC_ALGORITHM_ED25519
,
795 0, DNS_EDNS_OPT_DHU
, /* OPTION_CODE */
796 0, 3, /* LIST_LENGTH */
798 DNSSEC_DIGEST_SHA256
,
799 DNSSEC_DIGEST_SHA384
,
801 0, DNS_EDNS_OPT_N3U
, /* OPTION_CODE */
802 0, 1, /* LIST_LENGTH */
803 NSEC3_ALGORITHM_SHA1
,
806 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
); /* RDLENGTH */
810 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
); /* the payload, as defined above */
814 if (strlen(nsid
) > UINT16_MAX
- 4) {
819 r
= dns_packet_append_uint16(p
, 4 + strlen(nsid
), NULL
); /* RDLENGTH */
823 r
= dns_packet_append_uint16(p
, 3, NULL
); /* OPTION-CODE: NSID */
827 r
= dns_packet_append_uint16(p
, strlen(nsid
), NULL
); /* OPTION-LENGTH */
831 r
= dns_packet_append_blob(p
, nsid
, strlen(nsid
), NULL
);
833 r
= dns_packet_append_uint16(p
, 0, NULL
);
837 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
839 p
->opt_start
= saved_size
;
840 p
->opt_size
= p
->size
- saved_size
;
843 *ret_start
= saved_size
;
848 dns_packet_truncate(p
, saved_size
);
852 int dns_packet_truncate_opt(DnsPacket
*p
) {
855 if (p
->opt_start
== SIZE_MAX
) {
856 assert(p
->opt_size
== SIZE_MAX
);
860 assert(p
->opt_size
!= SIZE_MAX
);
861 assert(DNS_PACKET_ARCOUNT(p
) > 0);
863 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
866 dns_packet_truncate(p
, p
->opt_start
);
867 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
868 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
873 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
875 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
882 saved_size
= p
->size
;
884 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
888 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
889 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
893 /* Initially we write 0 here */
894 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
898 rds
= p
->size
- saved_size
;
900 switch (rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
903 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
907 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
911 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
915 /* RFC 2782 states "Unless and until permitted by future standards action, name compression
916 * is not to be used for this field." Hence we turn off compression here. */
917 r
= dns_packet_append_name(p
, rr
->srv
.name
, /* allow_compression= */ false, /* canonical_candidate= */ true, NULL
);
924 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, true, NULL
);
928 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
932 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
935 case DNS_TYPE_SPF
: /* exactly the same as TXT */
938 if (!rr
->txt
.items
) {
939 /* RFC 6763, section 6.1 suggests to generate
940 * single empty string for an empty array. */
942 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
946 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
947 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
956 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
960 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
964 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, true, NULL
);
968 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, true, NULL
);
972 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
976 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
980 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
984 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
988 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
992 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
996 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, true, NULL
);
1000 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
1004 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
1008 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
1012 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
1016 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
1020 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
1024 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
1028 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
1032 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
1036 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
1040 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
1043 case DNS_TYPE_SSHFP
:
1044 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
1048 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
1052 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
1055 case DNS_TYPE_DNSKEY
:
1056 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
1060 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
1064 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
1068 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1071 case DNS_TYPE_RRSIG
:
1072 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1076 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1080 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1084 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1088 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1092 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1096 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1100 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1104 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1108 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1112 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1118 case DNS_TYPE_NSEC3
:
1119 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1123 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1127 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1131 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1135 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1139 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1143 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1147 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1154 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1158 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1162 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1166 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1170 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1174 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1178 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1182 case DNS_TYPE_OPENPGPKEY
:
1183 case _DNS_TYPE_INVALID
: /* unparsable */
1186 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1192 /* Let's calculate the actual data size and update the field */
1193 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1194 if (rdlength
> 0xFFFF) {
1200 p
->size
= rdlength_offset
;
1201 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1207 *start
= saved_size
;
1215 dns_packet_truncate(p
, saved_size
);
1219 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1220 DnsResourceKey
*key
;
1225 DNS_QUESTION_FOREACH(key
, q
) {
1226 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1234 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
, unsigned *completed
) {
1235 DnsResourceRecord
*rr
;
1236 DnsAnswerFlags flags
;
1241 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1242 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1253 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1255 assert(p
->rindex
<= p
->size
);
1257 if (sz
> p
->size
- p
->rindex
)
1261 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1270 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1272 assert(idx
<= p
->size
);
1273 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1278 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1285 r
= dns_packet_read(p
, sz
, &q
, start
);
1293 static int dns_packet_read_memdup(
1294 DnsPacket
*p
, size_t size
,
1295 void **ret
, size_t *ret_size
,
1296 size_t *ret_start
) {
1305 r
= dns_packet_read(p
, size
, &src
, &start
);
1314 copy
= memdup(src
, size
);
1329 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1335 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1339 *ret
= ((uint8_t*) d
)[0];
1343 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1349 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1354 *ret
= unaligned_read_be16(d
);
1359 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1365 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1369 *ret
= unaligned_read_be32(d
);
1374 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1375 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1376 _cleanup_free_
char *t
= NULL
;
1383 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1387 r
= dns_packet_read(p
, c
, &d
, NULL
);
1391 r
= make_cstring(d
, c
, MAKE_CSTRING_REFUSE_TRAILING_NUL
, &t
);
1395 if (!utf8_is_valid(t
))
1401 *start
= rewinder
.saved_rindex
;
1402 CANCEL_REWINDER(rewinder
);
1407 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1410 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1414 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1418 r
= dns_packet_read(p
, c
, ret
, NULL
);
1425 *start
= rewinder
.saved_rindex
;
1426 CANCEL_REWINDER(rewinder
);
1431 int dns_packet_read_name(
1434 bool allow_compression
,
1435 size_t *ret_start
) {
1439 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1440 size_t after_rindex
= 0, jump_barrier
= p
->rindex
;
1441 _cleanup_free_
char *name
= NULL
;
1446 if (p
->refuse_compression
)
1447 allow_compression
= false;
1452 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1463 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1467 if (!GREEDY_REALLOC(name
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1475 r
= dns_label_escape(label
, c
, name
+ n
, DNS_LABEL_ESCAPED_MAX
);
1481 } else if (allow_compression
&& FLAGS_SET(c
, 0xc0)) {
1485 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1489 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1490 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1493 if (after_rindex
== 0)
1494 after_rindex
= p
->rindex
;
1496 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1503 if (!GREEDY_REALLOC(name
, n
+ 1))
1508 if (after_rindex
!= 0)
1509 p
->rindex
= after_rindex
;
1512 *ret
= TAKE_PTR(name
);
1514 *ret_start
= rewinder
.saved_rindex
;
1516 CANCEL_REWINDER(rewinder
);
1521 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1525 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1526 uint8_t window
, length
;
1527 const uint8_t *bitmap
;
1532 r
= bitmap_ensure_allocated(types
);
1536 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1540 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1544 if (length
== 0 || length
> 32)
1547 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1551 for (uint8_t i
= 0; i
< length
; i
++) {
1552 uint8_t bitmask
= 1 << 7;
1562 for (; bitmask
; bit
++, bitmask
>>= 1)
1563 if (bitmap
[i
] & bitmask
) {
1566 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1568 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1569 if (dns_type_is_pseudo(n
))
1572 r
= bitmap_set(*types
, n
);
1582 *start
= rewinder
.saved_rindex
;
1583 CANCEL_REWINDER(rewinder
);
1588 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1589 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1592 while (p
->rindex
- rewinder
.saved_rindex
< size
) {
1593 r
= dns_packet_read_type_window(p
, types
, NULL
);
1597 assert(p
->rindex
>= rewinder
.saved_rindex
);
1599 /* don't read past end of current RR */
1600 if (p
->rindex
- rewinder
.saved_rindex
> size
)
1604 if (p
->rindex
- rewinder
.saved_rindex
!= size
)
1608 *start
= rewinder
.saved_rindex
;
1609 CANCEL_REWINDER(rewinder
);
1614 int dns_packet_read_key(
1616 DnsResourceKey
**ret
,
1617 bool *ret_cache_flush_or_qu
,
1618 size_t *ret_start
) {
1622 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1623 _cleanup_free_
char *name
= NULL
;
1624 bool cache_flush_or_qu
= false;
1625 uint16_t class, type
;
1628 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1632 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1636 r
= dns_packet_read_uint16(p
, &class, NULL
);
1640 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1641 /* See RFC6762, sections 5.4 and 10.2 */
1643 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH_OR_QU
)) {
1644 class &= ~MDNS_RR_CACHE_FLUSH_OR_QU
;
1645 cache_flush_or_qu
= true;
1650 DnsResourceKey
*key
;
1652 key
= dns_resource_key_new_consume(class, type
, name
);
1660 if (ret_cache_flush_or_qu
)
1661 *ret_cache_flush_or_qu
= cache_flush_or_qu
;
1663 *ret_start
= rewinder
.saved_rindex
;
1665 CANCEL_REWINDER(rewinder
);
1669 static bool loc_size_ok(uint8_t size
) {
1670 uint8_t m
= size
>> 4, e
= size
& 0xF;
1672 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1675 int dns_packet_read_rr(
1677 DnsResourceRecord
**ret
,
1678 bool *ret_cache_flush
,
1679 size_t *ret_start
) {
1683 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1684 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1685 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1691 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1695 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1698 rr
= dns_resource_record_new(key
);
1702 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1706 /* RFC 2181, Section 8, suggests to
1707 * treat a TTL with the MSB set as a zero TTL. */
1708 if (rr
->ttl
& UINT32_C(0x80000000))
1711 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1715 if (rdlength
> p
->size
- p
->rindex
)
1720 switch (rr
->key
->type
) {
1723 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1726 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1729 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1733 /* RFC 2782 states "Unless and until permitted by future standards action, name compression
1734 * is not to be used for this field." Nonetheless, we support it here, in the interest of
1735 * increasing compatibility with implementations that do not implement this correctly. After
1736 * all we didn't do this right once upon a time ourselves (see
1737 * https://github.com/systemd/systemd/issues/9793). */
1738 r
= dns_packet_read_name(p
, &rr
->srv
.name
, /* allow_compression= */ true, NULL
);
1743 case DNS_TYPE_CNAME
:
1744 case DNS_TYPE_DNAME
:
1745 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1748 case DNS_TYPE_HINFO
:
1749 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1753 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1756 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1758 if (rdlength
<= 0) {
1759 r
= dns_txt_item_new_empty(&rr
->txt
.items
);
1763 DnsTxtItem
*last
= NULL
;
1765 while (p
->rindex
- offset
< rdlength
) {
1770 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1774 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1778 memcpy(i
->data
, data
, sz
);
1781 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1790 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1794 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1798 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1802 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1806 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1810 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1814 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1818 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1822 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1826 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1830 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1833 case DNS_TYPE_LOC
: {
1837 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1842 rr
->loc
.version
= t
;
1844 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1848 if (!loc_size_ok(rr
->loc
.size
))
1851 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1855 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1858 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1862 if (!loc_size_ok(rr
->loc
.vert_pre
))
1865 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1869 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1873 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1879 dns_packet_rewind(p
, pos
);
1880 rr
->unparsable
= true;
1886 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1890 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1894 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1901 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1902 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1907 if (rr
->ds
.digest_size
<= 0)
1908 /* the accepted size depends on the algorithm, but for now
1909 just ensure that the value is greater than zero */
1914 case DNS_TYPE_SSHFP
:
1915 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1919 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1926 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1927 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1930 if (rr
->sshfp
.fingerprint_size
<= 0)
1931 /* the accepted size depends on the algorithm, but for now
1932 just ensure that the value is greater than zero */
1937 case DNS_TYPE_DNSKEY
:
1938 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1942 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1946 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1953 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1954 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1957 if (rr
->dnskey
.key_size
<= 0)
1958 /* the accepted size depends on the algorithm, but for now
1959 just ensure that the value is greater than zero */
1964 case DNS_TYPE_RRSIG
:
1965 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1969 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1973 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1977 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1981 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1985 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1989 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1993 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1997 if (rdlength
< p
->rindex
- offset
)
2000 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
2001 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
2004 if (rr
->rrsig
.signature_size
<= 0)
2005 /* the accepted size depends on the algorithm, but for now
2006 just ensure that the value is greater than zero */
2011 case DNS_TYPE_NSEC
: {
2014 * RFC6762, section 18.14 explicitly states mDNS should use name compression.
2015 * This contradicts RFC3845, section 2.1.1
2018 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
2020 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
2024 if (rdlength
< p
->rindex
- offset
)
2027 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2029 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
2030 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
2031 * without the NSEC bit set. */
2035 case DNS_TYPE_NSEC3
: {
2038 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
2042 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
2046 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
2050 /* this may be zero */
2051 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2055 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
2059 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2066 r
= dns_packet_read_memdup(p
, size
,
2067 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
2072 if (rdlength
< p
->rindex
- offset
)
2075 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2077 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
2083 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2087 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2091 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2098 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2099 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2102 if (rr
->tlsa
.data_size
<= 0)
2103 /* the accepted size depends on the algorithm, but for now
2104 just ensure that the value is greater than zero */
2110 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2114 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2118 if (rdlength
< p
->rindex
- offset
)
2121 r
= dns_packet_read_memdup(p
,
2122 rdlength
+ offset
- p
->rindex
,
2123 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2127 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2128 case DNS_TYPE_OPENPGPKEY
:
2131 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2137 if (p
->rindex
- offset
!= rdlength
)
2141 *ret
= TAKE_PTR(rr
);
2142 if (ret_cache_flush
)
2143 *ret_cache_flush
= cache_flush
;
2145 *ret_start
= rewinder
.saved_rindex
;
2147 CANCEL_REWINDER(rewinder
);
2151 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2153 bool found_dau_dhu_n3u
= false;
2156 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2160 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2162 /* Check that the version is 0 */
2163 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2165 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2169 l
= rr
->opt
.data_size
;
2171 uint16_t option_code
, option_length
;
2173 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2177 option_code
= unaligned_read_be16(p
);
2178 option_length
= unaligned_read_be16(p
+ 2);
2180 if (l
< option_length
+ 4U)
2183 /* RFC 6975 DAU, DHU or N3U fields found. */
2184 if (IN_SET(option_code
, DNS_EDNS_OPT_DAU
, DNS_EDNS_OPT_DHU
, DNS_EDNS_OPT_N3U
))
2185 found_dau_dhu_n3u
= true;
2187 p
+= option_length
+ 4U;
2188 l
-= option_length
+ 4U;
2191 *rfc6975
= found_dau_dhu_n3u
;
2195 static int dns_packet_extract_question(DnsPacket
*p
, DnsQuestion
**ret_question
) {
2196 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2200 n
= DNS_PACKET_QDCOUNT(p
);
2202 question
= dns_question_new(n
);
2206 _cleanup_set_free_ Set
*keys
= NULL
; /* references to keys are kept by Question */
2208 keys
= set_new(&dns_resource_key_hash_ops
);
2212 r
= set_reserve(keys
, n
* 2); /* Higher multipliers give slightly higher efficiency through
2213 * hash collisions, but the gains quickly drop off after 2. */
2217 for (unsigned i
= 0; i
< n
; i
++) {
2218 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2221 r
= dns_packet_read_key(p
, &key
, &qu
, NULL
);
2225 if (!dns_type_is_valid_query(key
->type
))
2228 r
= set_put(keys
, key
);
2232 /* Already in the Question, let's skip */
2235 r
= dns_question_add_raw(question
, key
, qu
? DNS_QUESTION_WANTS_UNICAST_REPLY
: 0);
2241 *ret_question
= TAKE_PTR(question
);
2246 static int dns_packet_extract_answer(DnsPacket
*p
, DnsAnswer
**ret_answer
) {
2247 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2249 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2250 bool bad_opt
= false;
2253 n
= DNS_PACKET_RRCOUNT(p
);
2257 answer
= dns_answer_new(n
);
2261 for (unsigned i
= 0; i
< n
; i
++) {
2262 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2263 bool cache_flush
= false;
2266 if (p
->rindex
== p
->size
&& p
->opt
) {
2267 /* If we reached the end of the packet already, but there are still more RRs
2268 * declared, then that's a corrupt packet. Let's accept the packet anyway, since it's
2269 * apparently a common bug in routers. Let's however suppress OPT support in this
2270 * case, so that we force the rest of the logic into lowest DNS baseline support. Or
2271 * to say this differently: if the DNS server doesn't even get the RR counts right,
2272 * it's highly unlikely it gets EDNS right. */
2273 log_debug("More resource records declared in packet than included, suppressing OPT.");
2278 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, &start
);
2282 /* Try to reduce memory usage a bit */
2284 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2286 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2289 if (p
->opt
|| bad_opt
) {
2290 /* Multiple OPT RRs? if so, let's ignore all, because there's
2291 * something wrong with the server, and if one is valid we wouldn't
2292 * know which one. */
2293 log_debug("Multiple OPT RRs detected, ignoring all.");
2298 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2299 /* If the OPT RR is not owned by the root domain, then it is bad,
2300 * let's ignore it. */
2301 log_debug("OPT RR is not owned by root domain, ignoring.");
2306 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2307 /* OPT RR is in the wrong section? Some Belkin routers do this. This
2308 * is a hint the EDNS implementation is borked, like the Belkin one
2309 * is, hence ignore it. */
2310 log_debug("OPT RR in wrong section, ignoring.");
2315 if (!opt_is_good(rr
, &has_rfc6975
)) {
2316 log_debug("Malformed OPT RR, ignoring.");
2321 if (DNS_PACKET_QR(p
)) {
2322 /* Additional checks for responses */
2324 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
))
2325 /* If this is a reply and we don't know the EDNS version
2326 * then something is weird... */
2327 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2328 "EDNS version newer that our request, bad server.");
2331 /* If the OPT RR contains RFC6975 algorithm data, then this
2332 * is indication that the server just copied the OPT it got
2333 * from us (which contained that data) back into the reply.
2334 * If so, then it doesn't properly support EDNS, as RFC6975
2335 * makes it very clear that the algorithm data should only
2336 * be contained in questions, never in replies. Crappy
2337 * Belkin routers copy the OPT data for example, hence let's
2338 * detect this so that we downgrade early. */
2339 log_debug("OPT RR contains RFC6975 data, ignoring.");
2345 p
->opt
= dns_resource_record_ref(rr
);
2346 p
->opt_start
= start
;
2347 assert(p
->rindex
>= start
);
2348 p
->opt_size
= p
->rindex
- start
;
2350 DnsAnswerFlags flags
= 0;
2352 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
2353 flags
|= DNS_ANSWER_REFUSE_TTL_NO_MATCH
;
2355 flags
|= DNS_ANSWER_SHARED_OWNER
;
2358 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2359 * cached. Hence mark only those RRs as cacheable by default, but not the ones from
2360 * the Additional or Authority sections.
2361 * This restriction does not apply to mDNS records (RFC 6762). */
2362 if (i
< DNS_PACKET_ANCOUNT(p
))
2363 flags
|= DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SECTION_ANSWER
;
2364 else if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
))
2365 flags
|= DNS_ANSWER_SECTION_AUTHORITY
;
2367 flags
|= DNS_ANSWER_SECTION_ADDITIONAL
;
2368 if (p
->protocol
== DNS_PROTOCOL_MDNS
)
2369 flags
|= DNS_ANSWER_CACHEABLE
;
2372 r
= dns_answer_add(answer
, rr
, p
->ifindex
, flags
, NULL
);
2377 /* Remember this RR, so that we can potentially merge its ->key object with the
2378 * next RR. Note that we only do this if we actually decided to keep the RR around.
2380 DNS_RR_REPLACE(previous
, dns_resource_record_ref(rr
));
2384 p
->opt
= dns_resource_record_unref(p
->opt
);
2385 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
2388 *ret_answer
= TAKE_PTR(answer
);
2393 int dns_packet_extract(DnsPacket
*p
) {
2399 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2400 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2401 _unused_
_cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
2404 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2406 r
= dns_packet_extract_question(p
, &question
);
2410 r
= dns_packet_extract_answer(p
, &answer
);
2414 if (p
->rindex
< p
->size
) {
2415 log_debug("Trailing garbage in packet, suppressing OPT.");
2416 p
->opt
= dns_resource_record_unref(p
->opt
);
2417 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
2420 p
->question
= TAKE_PTR(question
);
2421 p
->answer
= TAKE_PTR(answer
);
2422 p
->extracted
= true;
2424 /* no CANCEL, always rewind */
2428 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2434 /* Checks if the specified packet is a reply for the specified
2435 * key and the specified key is the only one in the question
2438 if (DNS_PACKET_QR(p
) != 1)
2441 /* Let's unpack the packet, if that hasn't happened yet. */
2442 r
= dns_packet_extract(p
);
2449 if (p
->question
->n_keys
!= 1)
2452 return dns_resource_key_equal(dns_question_first_key(p
->question
), key
);
2455 int dns_packet_patch_max_udp_size(DnsPacket
*p
, uint16_t max_udp_size
) {
2457 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
2459 if (p
->opt_start
== SIZE_MAX
) /* No OPT section, nothing to patch */
2462 assert(p
->opt_size
!= SIZE_MAX
);
2463 assert(p
->opt_size
>= 5);
2465 unaligned_write_be16(DNS_PACKET_DATA(p
) + p
->opt_start
+ 3, max_udp_size
);
2469 static int patch_rr(DnsPacket
*p
, usec_t age
) {
2470 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
2473 uint16_t type
, rdlength
;
2476 /* Patches the RR at the current rindex, subtracts the specified time from the TTL */
2478 r
= dns_packet_read_name(p
, NULL
, true, NULL
);
2482 r
= dns_packet_read_uint16(p
, &type
, NULL
);
2486 r
= dns_packet_read_uint16(p
, NULL
, NULL
);
2490 r
= dns_packet_read_uint32(p
, &ttl
, &ttl_index
);
2494 if (type
!= DNS_TYPE_OPT
) { /* The TTL of the OPT field is not actually a TTL, skip it */
2495 ttl
= LESS_BY(ttl
* USEC_PER_SEC
, age
) / USEC_PER_SEC
;
2496 unaligned_write_be32(DNS_PACKET_DATA(p
) + ttl_index
, ttl
);
2499 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
2503 r
= dns_packet_read(p
, rdlength
, NULL
, NULL
);
2507 CANCEL_REWINDER(rewinder
);
2511 int dns_packet_patch_ttls(DnsPacket
*p
, usec_t timestamp
) {
2513 assert(timestamp_is_set(timestamp
));
2515 /* Adjusts all TTLs in the packet by subtracting the time difference between now and the specified timestamp */
2517 _unused_
_cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
2522 k
= now(CLOCK_BOOTTIME
);
2523 assert(k
>= timestamp
);
2526 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2528 n
= DNS_PACKET_QDCOUNT(p
);
2529 for (unsigned i
= 0; i
< n
; i
++) {
2530 r
= dns_packet_read_key(p
, NULL
, NULL
, NULL
);
2535 n
= DNS_PACKET_RRCOUNT(p
);
2536 for (unsigned i
= 0; i
< n
; i
++) {
2538 /* DNS servers suck, hence the RR count is in many servers off. If we reached the end
2539 * prematurely, accept that, exit early */
2540 if (p
->rindex
== p
->size
)
2551 static void dns_packet_hash_func(const DnsPacket
*s
, struct siphash
*state
) {
2554 siphash24_compress_typesafe(s
->size
, state
);
2555 siphash24_compress(DNS_PACKET_DATA((DnsPacket
*) s
), s
->size
, state
);
2558 static int dns_packet_compare_func(const DnsPacket
*x
, const DnsPacket
*y
) {
2561 r
= CMP(x
->size
, y
->size
);
2565 return memcmp(DNS_PACKET_DATA((DnsPacket
*) x
), DNS_PACKET_DATA((DnsPacket
*) y
), x
->size
);
2568 DEFINE_HASH_OPS(dns_packet_hash_ops
, DnsPacket
, dns_packet_hash_func
, dns_packet_compare_func
);
2570 bool dns_packet_equal(const DnsPacket
*a
, const DnsPacket
*b
) {
2571 return dns_packet_compare_func(a
, b
) == 0;
2574 int dns_packet_ede_rcode(DnsPacket
*p
, char **ret_ede_msg
) {
2577 _cleanup_free_
char *msg
= NULL
, *msg_escaped
= NULL
;
2578 int ede_rcode
= _DNS_EDNS_OPT_MAX_DEFINED
;
2584 return _DNS_EDE_RCODE_INVALID
;
2586 d
= p
->opt
->opt
.data
;
2587 l
= p
->opt
->opt
.data_size
;
2590 uint16_t code
, length
;
2593 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2594 "EDNS0 variable part has invalid size.");
2596 code
= unaligned_read_be16(d
);
2597 length
= unaligned_read_be16(d
+ 2);
2599 if (l
< 4U + length
)
2600 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2601 "Truncated option in EDNS0 variable part.");
2603 if (code
== DNS_EDNS_OPT_EXT_ERROR
) {
2605 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2606 "EDNS0 truncated EDE info code.");
2607 ede_rcode
= unaligned_read_be16(d
+ 4);
2608 r
= make_cstring((char *)d
+ 6, length
- 2U, MAKE_CSTRING_ALLOW_TRAILING_NUL
, &msg
);
2610 return log_debug_errno(r
, "Invalid EDE text in opt");
2611 else if (!utf8_is_valid(msg
))
2612 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
), "Invalid EDE text in opt");
2613 else if (ede_rcode
< _DNS_EDNS_OPT_MAX_DEFINED
) {
2614 msg_escaped
= cescape(msg
);
2626 *ret_ede_msg
= TAKE_PTR(msg_escaped
);
2631 bool dns_ede_rcode_is_dnssec(int ede_rcode
) {
2632 return IN_SET(ede_rcode
,
2633 DNS_EDE_RCODE_UNSUPPORTED_DNSKEY_ALG
,
2634 DNS_EDE_RCODE_UNSUPPORTED_DS_DIGEST
,
2635 DNS_EDE_RCODE_DNSSEC_INDETERMINATE
,
2636 DNS_EDE_RCODE_DNSSEC_BOGUS
,
2637 DNS_EDE_RCODE_SIG_EXPIRED
,
2638 DNS_EDE_RCODE_SIG_NOT_YET_VALID
,
2639 DNS_EDE_RCODE_DNSKEY_MISSING
,
2640 DNS_EDE_RCODE_RRSIG_MISSING
,
2641 DNS_EDE_RCODE_NO_ZONE_KEY_BIT
,
2642 DNS_EDE_RCODE_NSEC_MISSING
2646 int dns_packet_has_nsid_request(DnsPacket
*p
) {
2647 bool has_nsid
= false;
2656 d
= p
->opt
->opt
.data
;
2657 l
= p
->opt
->opt
.data_size
;
2660 uint16_t code
, length
;
2663 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2664 "EDNS0 variable part has invalid size.");
2666 code
= unaligned_read_be16(d
);
2667 length
= unaligned_read_be16(d
+ 2);
2669 if (l
< 4U + length
)
2670 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2671 "Truncated option in EDNS0 variable part.");
2673 if (code
== DNS_EDNS_OPT_NSID
) {
2675 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2676 "Duplicate NSID option in EDNS0 variable part.");
2679 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2680 "Non-empty NSID option in DNS request.");
2692 size_t dns_packet_size_unfragmented(DnsPacket
*p
) {
2695 if (p
->fragsize
== 0) /* Wasn't fragmented */
2698 /* The fragment size (p->fragsize) covers the whole (fragmented) IP packet, while the regular packet
2699 * size (p->size) only covers the DNS part. Thus, subtract the UDP header from the largest fragment
2700 * size, in order to determine which size of DNS packet would have gone through without
2703 return LESS_BY(p
->fragsize
, udp_header_size(p
->family
));
2706 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2707 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2708 [DNS_RCODE_FORMERR
] = "FORMERR",
2709 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2710 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2711 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2712 [DNS_RCODE_REFUSED
] = "REFUSED",
2713 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2714 [DNS_RCODE_YXRRSET
] = "YRRSET",
2715 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2716 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2717 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2718 [DNS_RCODE_BADVERS
] = "BADVERS",
2719 [DNS_RCODE_BADKEY
] = "BADKEY",
2720 [DNS_RCODE_BADTIME
] = "BADTIME",
2721 [DNS_RCODE_BADMODE
] = "BADMODE",
2722 [DNS_RCODE_BADNAME
] = "BADNAME",
2723 [DNS_RCODE_BADALG
] = "BADALG",
2724 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2725 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2727 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2729 const char *format_dns_rcode(int i
, char buf
[static DECIMAL_STR_MAX(int)]) {
2730 const char *p
= dns_rcode_to_string(i
);
2734 return snprintf_ok(buf
, DECIMAL_STR_MAX(int), "%i", i
);
2737 static const char* const dns_ede_rcode_table
[_DNS_EDE_RCODE_MAX_DEFINED
] = {
2738 [DNS_EDE_RCODE_OTHER
] = "Other",
2739 [DNS_EDE_RCODE_UNSUPPORTED_DNSKEY_ALG
] = "Unsupported DNSKEY Algorithm",
2740 [DNS_EDE_RCODE_UNSUPPORTED_DS_DIGEST
] = "Unsupported DS Digest Type",
2741 [DNS_EDE_RCODE_STALE_ANSWER
] = "Stale Answer",
2742 [DNS_EDE_RCODE_FORGED_ANSWER
] = "Forged Answer",
2743 [DNS_EDE_RCODE_DNSSEC_INDETERMINATE
] = "DNSSEC Indeterminate",
2744 [DNS_EDE_RCODE_DNSSEC_BOGUS
] = "DNSSEC Bogus",
2745 [DNS_EDE_RCODE_SIG_EXPIRED
] = "Signature Expired",
2746 [DNS_EDE_RCODE_SIG_NOT_YET_VALID
] = "Signature Not Yet Valid",
2747 [DNS_EDE_RCODE_DNSKEY_MISSING
] = "DNSKEY Missing",
2748 [DNS_EDE_RCODE_RRSIG_MISSING
] = "RRSIG Missing",
2749 [DNS_EDE_RCODE_NO_ZONE_KEY_BIT
] = "No Zone Key Bit Set",
2750 [DNS_EDE_RCODE_NSEC_MISSING
] = "NSEC Missing",
2751 [DNS_EDE_RCODE_CACHED_ERROR
] = "Cached Error",
2752 [DNS_EDE_RCODE_NOT_READY
] = "Not Ready",
2753 [DNS_EDE_RCODE_BLOCKED
] = "Blocked",
2754 [DNS_EDE_RCODE_CENSORED
] = "Censored",
2755 [DNS_EDE_RCODE_FILTERED
] = "Filtered",
2756 [DNS_EDE_RCODE_PROHIBITIED
] = "Prohibited",
2757 [DNS_EDE_RCODE_STALE_NXDOMAIN_ANSWER
] = "Stale NXDOMAIN Answer",
2758 [DNS_EDE_RCODE_NOT_AUTHORITATIVE
] = "Not Authoritative",
2759 [DNS_EDE_RCODE_NOT_SUPPORTED
] = "Not Supported",
2760 [DNS_EDE_RCODE_UNREACH_AUTHORITY
] = "No Reachable Authority",
2761 [DNS_EDE_RCODE_NET_ERROR
] = "Network Error",
2762 [DNS_EDE_RCODE_INVALID_DATA
] = "Invalid Data",
2763 [DNS_EDE_RCODE_SIG_NEVER
] = "Signature Never Valid",
2764 [DNS_EDE_RCODE_TOO_EARLY
] = "Too Early",
2765 [DNS_EDE_RCODE_UNSUPPORTED_NSEC3_ITER
] = "Unsupported NSEC3 Iterations",
2766 [DNS_EDE_RCODE_TRANSPORT_POLICY
] = "Impossible Transport Policy",
2767 [DNS_EDE_RCODE_SYNTHESIZED
] = "Synthesized",
2769 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dns_ede_rcode
, int);
2771 const char *format_dns_ede_rcode(int i
, char buf
[static DECIMAL_STR_MAX(int)]) {
2772 const char *p
= dns_ede_rcode_to_string(i
);
2776 return snprintf_ok(buf
, DECIMAL_STR_MAX(int), "%i", i
);
2779 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2780 [DNS_PROTOCOL_DNS
] = "dns",
2781 [DNS_PROTOCOL_MDNS
] = "mdns",
2782 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2784 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);