1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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
);
81 .size
= DNS_PACKET_HEADER_SIZE
,
82 .rindex
= DNS_PACKET_HEADER_SIZE
,
85 .opt_start
= (size_t) -1,
86 .opt_size
= (size_t) -1,
94 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
100 h
= DNS_PACKET_HEADER(p
);
102 switch(p
->protocol
) {
103 case DNS_PROTOCOL_LLMNR
:
106 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
117 case DNS_PROTOCOL_MDNS
:
118 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
122 0 /* rd (ask for recursion) */,
132 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
136 1 /* rd (ask for recursion) */,
139 dnssec_checking_disabled
/* cd */,
144 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t min_alloc_dsize
, bool dnssec_checking_disabled
) {
150 r
= dns_packet_new(&p
, protocol
, min_alloc_dsize
, DNS_PACKET_SIZE_MAX
);
154 /* Always set the TC bit to 0 initially.
155 * If there are multiple packets later, we'll update the bit shortly before sending.
157 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
163 int dns_packet_dup(DnsPacket
**ret
, DnsPacket
*p
) {
170 r
= dns_packet_validate(p
);
174 c
= malloc(ALIGN(sizeof(DnsPacket
)) + p
->size
);
180 .protocol
= p
->protocol
,
182 .rindex
= DNS_PACKET_HEADER_SIZE
,
183 .allocated
= p
->size
,
184 .max_size
= p
->max_size
,
185 .opt_start
= (size_t) -1,
186 .opt_size
= (size_t) -1,
189 memcpy(DNS_PACKET_DATA(c
), DNS_PACKET_DATA(p
), p
->size
);
195 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
200 assert(!p
->on_stack
);
202 assert(p
->n_ref
> 0);
207 static void dns_packet_free(DnsPacket
*p
) {
212 dns_question_unref(p
->question
);
213 dns_answer_unref(p
->answer
);
214 dns_resource_record_unref(p
->opt
);
216 while ((s
= hashmap_steal_first_key(p
->names
)))
218 hashmap_free(p
->names
);
226 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
230 assert(p
->n_ref
> 0);
232 dns_packet_unref(p
->more
);
242 int dns_packet_validate(DnsPacket
*p
) {
245 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
248 if (p
->size
> DNS_PACKET_SIZE_MAX
)
254 int dns_packet_validate_reply(DnsPacket
*p
) {
259 r
= dns_packet_validate(p
);
263 if (DNS_PACKET_QR(p
) != 1)
266 if (DNS_PACKET_OPCODE(p
) != 0)
269 switch (p
->protocol
) {
271 case DNS_PROTOCOL_LLMNR
:
272 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
273 if (DNS_PACKET_QDCOUNT(p
) != 1)
278 case DNS_PROTOCOL_MDNS
:
279 /* RFC 6762, Section 18 */
280 if (DNS_PACKET_RCODE(p
) != 0)
292 int dns_packet_validate_query(DnsPacket
*p
) {
297 r
= dns_packet_validate(p
);
301 if (DNS_PACKET_QR(p
) != 0)
304 if (DNS_PACKET_OPCODE(p
) != 0)
307 if (DNS_PACKET_TC(p
))
310 switch (p
->protocol
) {
312 case DNS_PROTOCOL_LLMNR
:
313 case DNS_PROTOCOL_DNS
:
314 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
315 if (DNS_PACKET_QDCOUNT(p
) != 1)
318 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
319 if (DNS_PACKET_ANCOUNT(p
) > 0)
322 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
323 if (DNS_PACKET_NSCOUNT(p
) > 0)
328 case DNS_PROTOCOL_MDNS
:
329 /* RFC 6762, Section 18 */
330 if (DNS_PACKET_AA(p
) != 0 ||
331 DNS_PACKET_RD(p
) != 0 ||
332 DNS_PACKET_RA(p
) != 0 ||
333 DNS_PACKET_AD(p
) != 0 ||
334 DNS_PACKET_CD(p
) != 0 ||
335 DNS_PACKET_RCODE(p
) != 0)
347 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
350 if (p
->size
+ add
> p
->allocated
) {
353 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
355 ms
= dns_packet_size_max(p
);
359 if (p
->size
+ add
> a
)
365 d
= realloc(p
->_data
, a
);
371 p
->_data
= malloc(a
);
375 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
376 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
386 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
392 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
401 HASHMAP_FOREACH_KEY(n
, s
, p
->names
) {
403 if (PTR_TO_SIZE(n
) < sz
)
406 hashmap_remove(p
->names
, s
);
413 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
419 r
= dns_packet_extend(p
, l
, &q
, start
);
423 memcpy_safe(q
, d
, l
);
427 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
433 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
437 ((uint8_t*) d
)[0] = v
;
442 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
448 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
452 unaligned_write_be16(d
, v
);
457 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
463 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
467 unaligned_write_be32(d
, v
);
472 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
476 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
479 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
484 assert(s
|| size
== 0);
489 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
493 ((uint8_t*) d
)[0] = (uint8_t) size
;
495 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
500 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
504 /* Append a label to a packet. Optionally, does this in DNSSEC
505 * canonical form, if this label is marked as a candidate for
506 * it, and the canonical form logic is enabled for the
512 if (l
> DNS_LABEL_MAX
)
515 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
519 *(w
++) = (uint8_t) l
;
521 if (p
->canonical_form
&& canonical_candidate
) {
524 /* Generate in canonical form, as defined by DNSSEC
525 * RFC 4034, Section 6.2, i.e. all lower-case. */
527 for (i
= 0; i
< l
; i
++)
528 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
530 /* Otherwise, just copy the string unaltered. This is
531 * essential for DNS-SD, where the casing of labels
532 * matters and needs to be retained. */
538 int dns_packet_append_name(
541 bool allow_compression
,
542 bool canonical_candidate
,
551 if (p
->refuse_compression
)
552 allow_compression
= false;
554 saved_size
= p
->size
;
556 while (!dns_name_is_root(name
)) {
557 const char *z
= name
;
558 char label
[DNS_LABEL_MAX
];
561 if (allow_compression
)
562 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
567 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
575 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
579 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
583 if (allow_compression
) {
584 _cleanup_free_
char *s
= NULL
;
592 r
= hashmap_ensure_put(&p
->names
, &dns_name_hash_ops
, s
, SIZE_TO_PTR(n
));
600 r
= dns_packet_append_uint8(p
, 0, NULL
);
611 dns_packet_truncate(p
, saved_size
);
615 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
623 saved_size
= p
->size
;
625 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
629 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
633 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH
: k
->class;
634 r
= dns_packet_append_uint16(p
, class, NULL
);
644 dns_packet_truncate(p
, saved_size
);
648 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
656 saved_size
= p
->size
;
658 r
= dns_packet_append_uint8(p
, window
, NULL
);
662 r
= dns_packet_append_uint8(p
, length
, NULL
);
666 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
675 dns_packet_truncate(p
, saved_size
);
679 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
682 uint8_t bitmaps
[32] = {};
689 saved_size
= p
->size
;
691 BITMAP_FOREACH(n
, types
) {
694 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
695 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
705 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
708 if (bitmaps
[entry
/ 8] != 0) {
709 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
719 dns_packet_truncate(p
, saved_size
);
723 /* Append the OPT pseudo-RR described in RFC6891 */
724 int dns_packet_append_opt(
726 uint16_t max_udp_size
,
728 bool include_rfc6975
,
737 /* we must never advertise supported packet size smaller than the legacy max */
738 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
740 assert(rcode
<= _DNS_RCODE_MAX
);
742 if (p
->opt_start
!= (size_t) -1)
745 assert(p
->opt_size
== (size_t) -1);
747 saved_size
= p
->size
;
750 r
= dns_packet_append_uint8(p
, 0, NULL
);
755 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
759 /* class: maximum udp packet that can be received */
760 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
764 /* extended RCODE and VERSION */
765 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
769 /* flags: DNSSEC OK (DO), see RFC3225 */
770 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
774 if (edns0_do
&& include_rfc6975
) {
775 /* If DO is on and this is requested, also append RFC6975 Algorithm data. This is supposed to
776 * be done on queries, not on replies, hencer callers should turn this off when finishing off
779 static const uint8_t rfc6975
[] = {
781 0, 5, /* OPTION_CODE: DAU */
782 #if HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600
783 0, 7, /* LIST_LENGTH */
785 0, 6, /* LIST_LENGTH */
787 DNSSEC_ALGORITHM_RSASHA1
,
788 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
789 DNSSEC_ALGORITHM_RSASHA256
,
790 DNSSEC_ALGORITHM_RSASHA512
,
791 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
792 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
793 #if HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600
794 DNSSEC_ALGORITHM_ED25519
,
797 0, 6, /* OPTION_CODE: DHU */
798 0, 3, /* LIST_LENGTH */
800 DNSSEC_DIGEST_SHA256
,
801 DNSSEC_DIGEST_SHA384
,
803 0, 7, /* OPTION_CODE: N3U */
804 0, 1, /* LIST_LENGTH */
805 NSEC3_ALGORITHM_SHA1
,
808 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
); /* RDLENGTH */
812 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
); /* the payload, as defined above */
816 if (strlen(nsid
) > UINT16_MAX
- 4) {
821 r
= dns_packet_append_uint16(p
, 4 + strlen(nsid
), NULL
); /* RDLENGTH */
825 r
= dns_packet_append_uint16(p
, 3, NULL
); /* OPTION-CODE: NSID */
829 r
= dns_packet_append_uint16(p
, strlen(nsid
), NULL
); /* OPTION-LENGTH */
833 r
= dns_packet_append_blob(p
, nsid
, strlen(nsid
), NULL
);
835 r
= dns_packet_append_uint16(p
, 0, NULL
);
839 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
841 p
->opt_start
= saved_size
;
842 p
->opt_size
= p
->size
- saved_size
;
845 *ret_start
= saved_size
;
850 dns_packet_truncate(p
, saved_size
);
854 int dns_packet_truncate_opt(DnsPacket
*p
) {
857 if (p
->opt_start
== (size_t) -1) {
858 assert(p
->opt_size
== (size_t) -1);
862 assert(p
->opt_size
!= (size_t) -1);
863 assert(DNS_PACKET_ARCOUNT(p
) > 0);
865 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
868 dns_packet_truncate(p
, p
->opt_start
);
869 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
870 p
->opt_start
= p
->opt_size
= (size_t) -1;
875 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
877 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
884 saved_size
= p
->size
;
886 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
890 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
891 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
895 /* Initially we write 0 here */
896 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
900 rds
= p
->size
- saved_size
;
902 switch (rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
905 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
909 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
913 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
917 /* RFC 2782 states "Unless and until permitted by future standards
918 * action, name compression is not to be used for this field." */
919 r
= dns_packet_append_name(p
, rr
->srv
.name
, false, true, NULL
);
926 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, true, NULL
);
930 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
934 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
937 case DNS_TYPE_SPF
: /* exactly the same as TXT */
940 if (!rr
->txt
.items
) {
941 /* RFC 6763, section 6.1 suggests to generate
942 * single empty string for an empty array. */
944 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
950 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
951 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
961 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
965 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
969 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, true, NULL
);
973 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, true, NULL
);
977 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
981 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
985 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
989 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
993 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
997 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
1001 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, true, NULL
);
1005 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
1009 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
1013 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
1017 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
1021 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
1025 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
1029 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
1033 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
1037 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
1041 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
1045 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
1048 case DNS_TYPE_SSHFP
:
1049 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
1053 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
1057 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
1060 case DNS_TYPE_DNSKEY
:
1061 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
1065 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
1069 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
1073 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1076 case DNS_TYPE_RRSIG
:
1077 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1081 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1085 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1089 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1093 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1097 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1101 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1105 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1109 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1113 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1117 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1123 case DNS_TYPE_NSEC3
:
1124 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1128 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1132 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1136 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1140 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1144 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1148 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1152 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1159 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1163 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1167 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1171 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1175 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1179 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1183 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1187 case DNS_TYPE_OPENPGPKEY
:
1188 case _DNS_TYPE_INVALID
: /* unparsable */
1191 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1197 /* Let's calculate the actual data size and update the field */
1198 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1199 if (rdlength
> 0xFFFF) {
1205 p
->size
= rdlength_offset
;
1206 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1212 *start
= saved_size
;
1220 dns_packet_truncate(p
, saved_size
);
1224 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1225 DnsResourceKey
*key
;
1230 DNS_QUESTION_FOREACH(key
, q
) {
1231 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1239 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
, unsigned *completed
) {
1240 DnsResourceRecord
*rr
;
1241 DnsAnswerFlags flags
;
1246 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1247 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1258 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1261 if (p
->rindex
+ sz
> p
->size
)
1265 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1274 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1276 assert(idx
<= p
->size
);
1277 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1282 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1289 r
= dns_packet_read(p
, sz
, &q
, start
);
1297 static int dns_packet_read_memdup(
1298 DnsPacket
*p
, size_t size
,
1299 void **ret
, size_t *ret_size
,
1300 size_t *ret_start
) {
1309 r
= dns_packet_read(p
, size
, &src
, &start
);
1318 copy
= memdup(src
, size
);
1333 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1339 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1343 *ret
= ((uint8_t*) d
)[0];
1347 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1353 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1358 *ret
= unaligned_read_be16(d
);
1363 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1369 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1373 *ret
= unaligned_read_be32(d
);
1378 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1379 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1386 INIT_REWINDER(rewinder
, p
);
1388 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1392 r
= dns_packet_read(p
, c
, &d
, NULL
);
1396 if (memchr(d
, 0, c
))
1403 if (!utf8_is_valid(t
)) {
1411 *start
= rewinder
.saved_rindex
;
1412 CANCEL_REWINDER(rewinder
);
1417 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1418 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1423 INIT_REWINDER(rewinder
, p
);
1425 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1429 r
= dns_packet_read(p
, c
, ret
, NULL
);
1436 *start
= rewinder
.saved_rindex
;
1437 CANCEL_REWINDER(rewinder
);
1442 int dns_packet_read_name(
1445 bool allow_compression
,
1446 size_t *ret_start
) {
1448 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1449 size_t after_rindex
= 0, jump_barrier
;
1450 _cleanup_free_
char *name
= NULL
;
1451 size_t n
= 0, allocated
= 0;
1457 INIT_REWINDER(rewinder
, p
);
1458 jump_barrier
= p
->rindex
;
1460 if (p
->refuse_compression
)
1461 allow_compression
= false;
1466 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1477 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1481 if (!GREEDY_REALLOC(name
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1489 r
= dns_label_escape(label
, c
, name
+ n
, DNS_LABEL_ESCAPED_MAX
);
1495 } else if (allow_compression
&& FLAGS_SET(c
, 0xc0)) {
1499 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1503 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1504 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1507 if (after_rindex
== 0)
1508 after_rindex
= p
->rindex
;
1510 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1517 if (!GREEDY_REALLOC(name
, allocated
, n
+ 1))
1522 if (after_rindex
!= 0)
1523 p
->rindex
= after_rindex
;
1526 *ret
= TAKE_PTR(name
);
1528 *ret_start
= rewinder
.saved_rindex
;
1530 CANCEL_REWINDER(rewinder
);
1535 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1538 const uint8_t *bitmap
;
1542 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1547 INIT_REWINDER(rewinder
, p
);
1549 r
= bitmap_ensure_allocated(types
);
1553 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1557 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1561 if (length
== 0 || length
> 32)
1564 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1568 for (i
= 0; i
< length
; i
++) {
1569 uint8_t bitmask
= 1 << 7;
1579 for (; bitmask
; bit
++, bitmask
>>= 1)
1580 if (bitmap
[i
] & bitmask
) {
1583 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1585 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1586 if (dns_type_is_pseudo(n
))
1589 r
= bitmap_set(*types
, n
);
1599 *start
= rewinder
.saved_rindex
;
1600 CANCEL_REWINDER(rewinder
);
1605 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1606 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1609 INIT_REWINDER(rewinder
, p
);
1611 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1612 r
= dns_packet_read_type_window(p
, types
, NULL
);
1616 /* don't read past end of current RR */
1617 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1621 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1625 *start
= rewinder
.saved_rindex
;
1626 CANCEL_REWINDER(rewinder
);
1631 int dns_packet_read_key(
1633 DnsResourceKey
**ret
,
1634 bool *ret_cache_flush
,
1635 size_t *ret_start
) {
1637 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1638 _cleanup_free_
char *name
= NULL
;
1639 bool cache_flush
= false;
1640 uint16_t class, type
;
1644 INIT_REWINDER(rewinder
, p
);
1646 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1650 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1654 r
= dns_packet_read_uint16(p
, &class, NULL
);
1658 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1659 /* See RFC6762, Section 10.2 */
1661 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1662 class &= ~MDNS_RR_CACHE_FLUSH
;
1668 DnsResourceKey
*key
;
1670 key
= dns_resource_key_new_consume(class, type
, name
);
1678 if (ret_cache_flush
)
1679 *ret_cache_flush
= cache_flush
;
1681 *ret_start
= rewinder
.saved_rindex
;
1683 CANCEL_REWINDER(rewinder
);
1687 static bool loc_size_ok(uint8_t size
) {
1688 uint8_t m
= size
>> 4, e
= size
& 0xF;
1690 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1693 int dns_packet_read_rr(
1695 DnsResourceRecord
**ret
,
1696 bool *ret_cache_flush
,
1697 size_t *ret_start
) {
1699 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1700 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1701 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1709 INIT_REWINDER(rewinder
, p
);
1711 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1715 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1718 rr
= dns_resource_record_new(key
);
1722 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1726 /* RFC 2181, Section 8, suggests to
1727 * treat a TTL with the MSB set as a zero TTL. */
1728 if (rr
->ttl
& UINT32_C(0x80000000))
1731 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1735 if (p
->rindex
+ rdlength
> p
->size
)
1740 switch (rr
->key
->type
) {
1743 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1746 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1749 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1752 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1757 case DNS_TYPE_CNAME
:
1758 case DNS_TYPE_DNAME
:
1759 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1762 case DNS_TYPE_HINFO
:
1763 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1767 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1770 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1772 if (rdlength
<= 0) {
1773 r
= dns_txt_item_new_empty(&rr
->txt
.items
);
1777 DnsTxtItem
*last
= NULL
;
1779 while (p
->rindex
< offset
+ rdlength
) {
1784 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1788 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1792 memcpy(i
->data
, data
, sz
);
1795 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1804 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1808 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1812 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1816 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1820 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1824 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1828 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1832 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1836 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1840 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1844 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1847 case DNS_TYPE_LOC
: {
1851 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1856 rr
->loc
.version
= t
;
1858 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1862 if (!loc_size_ok(rr
->loc
.size
))
1865 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1869 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1872 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1876 if (!loc_size_ok(rr
->loc
.vert_pre
))
1879 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1883 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1887 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1893 dns_packet_rewind(p
, pos
);
1894 rr
->unparsable
= true;
1900 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1904 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1908 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1915 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1916 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1921 if (rr
->ds
.digest_size
<= 0)
1922 /* the accepted size depends on the algorithm, but for now
1923 just ensure that the value is greater than zero */
1928 case DNS_TYPE_SSHFP
:
1929 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1933 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1940 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1941 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1944 if (rr
->sshfp
.fingerprint_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_DNSKEY
:
1952 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1956 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1960 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1967 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1968 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1971 if (rr
->dnskey
.key_size
<= 0)
1972 /* the accepted size depends on the algorithm, but for now
1973 just ensure that the value is greater than zero */
1978 case DNS_TYPE_RRSIG
:
1979 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1983 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1987 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1991 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1995 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1999 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
2003 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
2007 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
2011 if (rdlength
+ offset
< p
->rindex
)
2014 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
2015 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
2018 if (rr
->rrsig
.signature_size
<= 0)
2019 /* the accepted size depends on the algorithm, but for now
2020 just ensure that the value is greater than zero */
2025 case DNS_TYPE_NSEC
: {
2028 * RFC6762, section 18.14 explicitly states mDNS should use name compression.
2029 * This contradicts RFC3845, section 2.1.1
2032 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
2034 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
2038 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2040 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
2041 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
2042 * without the NSEC bit set. */
2046 case DNS_TYPE_NSEC3
: {
2049 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
2053 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
2057 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
2061 /* this may be zero */
2062 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2066 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
2070 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2077 r
= dns_packet_read_memdup(p
, size
,
2078 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
2083 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2085 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
2091 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2095 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2099 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2106 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2107 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2110 if (rr
->tlsa
.data_size
<= 0)
2111 /* the accepted size depends on the algorithm, but for now
2112 just ensure that the value is greater than zero */
2118 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2122 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2126 if (rdlength
+ offset
< p
->rindex
)
2129 r
= dns_packet_read_memdup(p
,
2130 rdlength
+ offset
- p
->rindex
,
2131 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2135 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2136 case DNS_TYPE_OPENPGPKEY
:
2139 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2145 if (p
->rindex
!= offset
+ rdlength
)
2149 *ret
= TAKE_PTR(rr
);
2150 if (ret_cache_flush
)
2151 *ret_cache_flush
= cache_flush
;
2153 *ret_start
= rewinder
.saved_rindex
;
2155 CANCEL_REWINDER(rewinder
);
2159 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2161 bool found_dau_dhu_n3u
= false;
2164 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2168 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2170 /* Check that the version is 0 */
2171 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2173 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2177 l
= rr
->opt
.data_size
;
2179 uint16_t option_code
, option_length
;
2181 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2185 option_code
= unaligned_read_be16(p
);
2186 option_length
= unaligned_read_be16(p
+ 2);
2188 if (l
< option_length
+ 4U)
2191 /* RFC 6975 DAU, DHU or N3U fields found. */
2192 if (IN_SET(option_code
, 5, 6, 7))
2193 found_dau_dhu_n3u
= true;
2195 p
+= option_length
+ 4U;
2196 l
-= option_length
+ 4U;
2199 *rfc6975
= found_dau_dhu_n3u
;
2203 static int dns_packet_extract_question(DnsPacket
*p
, DnsQuestion
**ret_question
) {
2204 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2208 n
= DNS_PACKET_QDCOUNT(p
);
2210 question
= dns_question_new(n
);
2214 _cleanup_set_free_ Set
*keys
= NULL
; /* references to keys are kept by Question */
2216 keys
= set_new(&dns_resource_key_hash_ops
);
2220 r
= set_reserve(keys
, n
* 2); /* Higher multipliers give slightly higher efficiency through
2221 * hash collisions, but the gains quickly drop off after 2. */
2225 for (i
= 0; i
< n
; i
++) {
2226 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2229 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2236 if (!dns_type_is_valid_query(key
->type
))
2239 r
= set_put(keys
, key
);
2243 /* Already in the Question, let's skip */
2246 r
= dns_question_add_raw(question
, key
);
2252 *ret_question
= TAKE_PTR(question
);
2257 static int dns_packet_extract_answer(DnsPacket
*p
, DnsAnswer
**ret_answer
) {
2258 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2260 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2261 bool bad_opt
= false;
2264 n
= DNS_PACKET_RRCOUNT(p
);
2268 answer
= dns_answer_new(n
);
2272 for (i
= 0; i
< n
; i
++) {
2273 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2274 bool cache_flush
= false;
2277 if (p
->rindex
== p
->size
) {
2278 /* If we reached the end of the packet already, but there are still more RRs
2279 * declared, then that's a corrupt packet. Let's accept the packet anyway, since it's
2280 * apparently a common bug in routers. Let's however suppress OPT support in this
2281 * case, so that we force the rest of the logic into lowest DNS baseline support. Or
2282 * to say this differently: if the DNS server doesn't even get the RR counts right,
2283 * it's highly unlikely it gets EDNS right. */
2284 log_debug("More resource records declared in packet than included, suppressing OPT.");
2289 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, &start
);
2293 /* Try to reduce memory usage a bit */
2295 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2297 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2300 if (p
->opt
|| bad_opt
) {
2301 /* Multiple OPT RRs? if so, let's ignore all, because there's
2302 * something wrong with the server, and if one is valid we wouldn't
2303 * know which one. */
2304 log_debug("Multiple OPT RRs detected, ignoring all.");
2309 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2310 /* If the OPT RR is not owned by the root domain, then it is bad,
2311 * let's ignore it. */
2312 log_debug("OPT RR is not owned by root domain, ignoring.");
2317 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2318 /* OPT RR is in the wrong section? Some Belkin routers do this. This
2319 * is a hint the EDNS implementation is borked, like the Belkin one
2320 * is, hence ignore it. */
2321 log_debug("OPT RR in wrong section, ignoring.");
2326 if (!opt_is_good(rr
, &has_rfc6975
)) {
2327 log_debug("Malformed OPT RR, ignoring.");
2332 if (DNS_PACKET_QR(p
)) {
2333 /* Additional checks for responses */
2335 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
))
2336 /* If this is a reply and we don't know the EDNS version
2337 * then something is weird... */
2338 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2339 "EDNS version newer that our request, bad server.");
2342 /* If the OPT RR contains RFC6975 algorithm data, then this
2343 * is indication that the server just copied the OPT it got
2344 * from us (which contained that data) back into the reply.
2345 * If so, then it doesn't properly support EDNS, as RFC6975
2346 * makes it very clear that the algorithm data should only
2347 * be contained in questions, never in replies. Crappy
2348 * Belkin routers copy the OPT data for example, hence let's
2349 * detect this so that we downgrade early. */
2350 log_debug("OPT RR contains RFC6975 data, ignoring.");
2356 p
->opt
= dns_resource_record_ref(rr
);
2357 p
->opt_start
= start
;
2358 assert(p
->rindex
>= start
);
2359 p
->opt_size
= p
->rindex
- start
;
2361 DnsAnswerFlags flags
= 0;
2363 if (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
)
2364 flags
|= DNS_ANSWER_SHARED_OWNER
;
2366 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2367 * cached. Hence mark only those RRs as cacheable by default, but not the ones from
2368 * the Additional or Authority sections. */
2369 if (i
< DNS_PACKET_ANCOUNT(p
))
2370 flags
|= DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SECTION_ANSWER
;
2371 else if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
))
2372 flags
|= DNS_ANSWER_SECTION_AUTHORITY
;
2374 flags
|= DNS_ANSWER_SECTION_ADDITIONAL
;
2376 r
= dns_answer_add(answer
, rr
, p
->ifindex
, flags
, NULL
);
2381 /* Remember this RR, so that we potentically can merge it's ->key object with the
2382 * next RR. Note that we only do this if we actually decided to keep the RR around.
2384 dns_resource_record_unref(previous
);
2385 previous
= dns_resource_record_ref(rr
);
2389 p
->opt
= dns_resource_record_unref(p
->opt
);
2390 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
2393 *ret_answer
= TAKE_PTR(answer
);
2398 int dns_packet_extract(DnsPacket
*p
) {
2399 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2400 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2401 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2407 INIT_REWINDER(rewinder
, p
);
2408 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2410 r
= dns_packet_extract_question(p
, &question
);
2414 r
= dns_packet_extract_answer(p
, &answer
);
2418 if (p
->rindex
< p
->size
) {
2419 log_debug("Trailing garbage in packet, suppressing OPT.");
2420 p
->opt
= dns_resource_record_unref(p
->opt
);
2421 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
2424 p
->question
= TAKE_PTR(question
);
2425 p
->answer
= TAKE_PTR(answer
);
2427 p
->extracted
= true;
2429 /* no CANCEL, always rewind */
2433 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2439 /* Checks if the specified packet is a reply for the specified
2440 * key and the specified key is the only one in the question
2443 if (DNS_PACKET_QR(p
) != 1)
2446 /* Let's unpack the packet, if that hasn't happened yet. */
2447 r
= dns_packet_extract(p
);
2454 if (p
->question
->n_keys
!= 1)
2457 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2460 int dns_packet_patch_max_udp_size(DnsPacket
*p
, uint16_t max_udp_size
) {
2462 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
2464 if (p
->opt_start
== (size_t) -1) /* No OPT section, nothing to patch */
2467 assert(p
->opt_size
!= (size_t) -1);
2468 assert(p
->opt_size
>= 5);
2470 unaligned_write_be16(DNS_PACKET_DATA(p
) + p
->opt_start
+ 3, max_udp_size
);
2474 static int patch_rr(DnsPacket
*p
, usec_t age
) {
2475 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
2478 uint16_t type
, rdlength
;
2481 INIT_REWINDER(rewinder
, p
);
2483 /* Patches the RR at the current rindex, subtracts the specified time from the TTL */
2485 r
= dns_packet_read_name(p
, NULL
, true, NULL
);
2489 r
= dns_packet_read_uint16(p
, &type
, NULL
);
2493 r
= dns_packet_read_uint16(p
, NULL
, NULL
);
2497 r
= dns_packet_read_uint32(p
, &ttl
, &ttl_index
);
2501 if (type
!= DNS_TYPE_OPT
) { /* The TTL of the OPT field is not actually a TTL, skip it */
2502 ttl
= LESS_BY(ttl
* USEC_PER_SEC
, age
) / USEC_PER_SEC
;
2503 unaligned_write_be32(DNS_PACKET_DATA(p
) + ttl_index
, ttl
);
2506 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
2510 r
= dns_packet_read(p
, rdlength
, NULL
, NULL
);
2514 CANCEL_REWINDER(rewinder
);
2518 int dns_packet_patch_ttls(DnsPacket
*p
, usec_t timestamp
) {
2519 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2525 assert(timestamp_is_set(timestamp
));
2527 /* Adjusts all TTLs in the packet by subtracting the time difference between now and the specified timestamp */
2529 k
= now(clock_boottime_or_monotonic());
2530 assert(k
>= timestamp
);
2533 INIT_REWINDER(rewinder
, p
);
2535 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2537 n
= DNS_PACKET_QDCOUNT(p
);
2538 for (i
= 0; i
< n
; i
++) {
2539 r
= dns_packet_read_key(p
, NULL
, NULL
, NULL
);
2544 n
= DNS_PACKET_RRCOUNT(p
);
2545 for (i
= 0; i
< n
; i
++) {
2547 /* DNS servers suck, hence the RR count is in many servers off. If we reached the end
2548 * prematurely, accept that, exit early */
2549 if (p
->rindex
== p
->size
)
2560 static void dns_packet_hash_func(const DnsPacket
*s
, struct siphash
*state
) {
2563 siphash24_compress(&s
->size
, sizeof(s
->size
), state
);
2564 siphash24_compress(DNS_PACKET_DATA((DnsPacket
*) s
), s
->size
, state
);
2567 static int dns_packet_compare_func(const DnsPacket
*x
, const DnsPacket
*y
) {
2570 r
= CMP(x
->size
, y
->size
);
2574 return memcmp(DNS_PACKET_DATA((DnsPacket
*) x
), DNS_PACKET_DATA((DnsPacket
*) y
), x
->size
);
2577 DEFINE_HASH_OPS(dns_packet_hash_ops
, DnsPacket
, dns_packet_hash_func
, dns_packet_compare_func
);
2579 bool dns_packet_equal(const DnsPacket
*a
, const DnsPacket
*b
) {
2580 return dns_packet_compare_func(a
, b
) == 0;
2583 int dns_packet_has_nsid_request(DnsPacket
*p
) {
2584 bool has_nsid
= false;
2593 d
= p
->opt
->opt
.data
;
2594 l
= p
->opt
->opt
.data_size
;
2597 uint16_t code
, length
;
2600 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2601 "EDNS0 variable part has invalid size.");
2603 code
= unaligned_read_be16(d
);
2604 length
= unaligned_read_be16(d
+ 2);
2606 if (l
< 4U + length
)
2607 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2608 "Truncated option in EDNS0 variable part.");
2612 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2613 "Duplicate NSID option in EDNS0 variable part.");
2616 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2617 "Non-empty NSID option in DNS request.");
2629 size_t dns_packet_size_unfragmented(DnsPacket
*p
) {
2632 if (p
->fragsize
== 0) /* Wasn't fragmented */
2635 /* The fragment size (p->fragsize) covers the whole (fragmented) IP packet, while the regular packet
2636 * size (p->size) only covers the DNS part. Thus, subtract the UDP header from the largest fragment
2637 * size, in order to determine which size of DNS packet would have gone through without
2640 return LESS_BY(p
->fragsize
, udp_header_size(p
->family
));
2643 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2644 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2645 [DNS_RCODE_FORMERR
] = "FORMERR",
2646 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2647 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2648 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2649 [DNS_RCODE_REFUSED
] = "REFUSED",
2650 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2651 [DNS_RCODE_YXRRSET
] = "YRRSET",
2652 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2653 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2654 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2655 [DNS_RCODE_BADVERS
] = "BADVERS",
2656 [DNS_RCODE_BADKEY
] = "BADKEY",
2657 [DNS_RCODE_BADTIME
] = "BADTIME",
2658 [DNS_RCODE_BADMODE
] = "BADMODE",
2659 [DNS_RCODE_BADNAME
] = "BADNAME",
2660 [DNS_RCODE_BADALG
] = "BADALG",
2661 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2662 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2664 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2666 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2667 [DNS_PROTOCOL_DNS
] = "dns",
2668 [DNS_PROTOCOL_MDNS
] = "mdns",
2669 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2671 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);