1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "dns-domain.h"
9 #include "resolved-dns-packet.h"
11 #include "string-table.h"
13 #include "unaligned.h"
17 #define EDNS0_OPT_DO (1<<15)
19 assert_cc(DNS_PACKET_SIZE_START
> DNS_PACKET_HEADER_SIZE
)
21 typedef struct DnsPacketRewinder
{
26 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
28 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
31 #define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
32 #define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
37 size_t min_alloc_dsize
,
44 assert(max_size
>= DNS_PACKET_HEADER_SIZE
);
46 if (max_size
> DNS_PACKET_SIZE_MAX
)
47 max_size
= DNS_PACKET_SIZE_MAX
;
49 /* The caller may not check what is going to be truly allocated, so do not allow to
50 * allocate a DNS packet bigger than DNS_PACKET_SIZE_MAX.
52 if (min_alloc_dsize
> DNS_PACKET_SIZE_MAX
)
53 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
54 "Requested packet data size too big: %zu",
57 /* When dns_packet_new() is called with min_alloc_dsize == 0, allocate more than the
58 * absolute minimum (which is the dns packet header size), to avoid
59 * resizing immediately again after appending the first data to the packet.
61 if (min_alloc_dsize
< DNS_PACKET_HEADER_SIZE
)
62 a
= DNS_PACKET_SIZE_START
;
66 /* round up to next page size */
67 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
69 /* make sure we never allocate more than useful */
73 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
77 p
->size
= p
->rindex
= DNS_PACKET_HEADER_SIZE
;
79 p
->max_size
= max_size
;
80 p
->protocol
= protocol
;
81 p
->opt_start
= p
->opt_size
= (size_t) -1;
89 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
95 h
= DNS_PACKET_HEADER(p
);
98 case DNS_PROTOCOL_LLMNR
:
101 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
112 case DNS_PROTOCOL_MDNS
:
113 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
117 0 /* rd (ask for recursion) */,
127 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
131 1 /* rd (ask for recursion) */,
134 dnssec_checking_disabled
/* cd */,
139 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t min_alloc_dsize
, bool dnssec_checking_disabled
) {
145 r
= dns_packet_new(&p
, protocol
, min_alloc_dsize
, DNS_PACKET_SIZE_MAX
);
149 /* Always set the TC bit to 0 initially.
150 * If there are multiple packets later, we'll update the bit shortly before sending.
152 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
158 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
163 assert(!p
->on_stack
);
165 assert(p
->n_ref
> 0);
170 static void dns_packet_free(DnsPacket
*p
) {
175 dns_question_unref(p
->question
);
176 dns_answer_unref(p
->answer
);
177 dns_resource_record_unref(p
->opt
);
179 while ((s
= hashmap_steal_first_key(p
->names
)))
181 hashmap_free(p
->names
);
189 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
193 assert(p
->n_ref
> 0);
195 dns_packet_unref(p
->more
);
205 int dns_packet_validate(DnsPacket
*p
) {
208 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
211 if (p
->size
> DNS_PACKET_SIZE_MAX
)
217 int dns_packet_validate_reply(DnsPacket
*p
) {
222 r
= dns_packet_validate(p
);
226 if (DNS_PACKET_QR(p
) != 1)
229 if (DNS_PACKET_OPCODE(p
) != 0)
232 switch (p
->protocol
) {
234 case DNS_PROTOCOL_LLMNR
:
235 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
236 if (DNS_PACKET_QDCOUNT(p
) != 1)
241 case DNS_PROTOCOL_MDNS
:
242 /* RFC 6762, Section 18 */
243 if (DNS_PACKET_RCODE(p
) != 0)
255 int dns_packet_validate_query(DnsPacket
*p
) {
260 r
= dns_packet_validate(p
);
264 if (DNS_PACKET_QR(p
) != 0)
267 if (DNS_PACKET_OPCODE(p
) != 0)
270 if (DNS_PACKET_TC(p
))
273 switch (p
->protocol
) {
275 case DNS_PROTOCOL_LLMNR
:
276 case DNS_PROTOCOL_DNS
:
277 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
278 if (DNS_PACKET_QDCOUNT(p
) != 1)
281 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
282 if (DNS_PACKET_ANCOUNT(p
) > 0)
285 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
286 if (DNS_PACKET_NSCOUNT(p
) > 0)
291 case DNS_PROTOCOL_MDNS
:
292 /* RFC 6762, Section 18 */
293 if (DNS_PACKET_AA(p
) != 0 ||
294 DNS_PACKET_RD(p
) != 0 ||
295 DNS_PACKET_RA(p
) != 0 ||
296 DNS_PACKET_AD(p
) != 0 ||
297 DNS_PACKET_CD(p
) != 0 ||
298 DNS_PACKET_RCODE(p
) != 0)
310 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
313 if (p
->size
+ add
> p
->allocated
) {
316 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
318 ms
= dns_packet_size_max(p
);
322 if (p
->size
+ add
> a
)
328 d
= realloc(p
->_data
, a
);
334 p
->_data
= malloc(a
);
338 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
339 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
349 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
355 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
365 HASHMAP_FOREACH_KEY(n
, s
, p
->names
, i
) {
367 if (PTR_TO_SIZE(n
) < sz
)
370 hashmap_remove(p
->names
, s
);
377 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
383 r
= dns_packet_extend(p
, l
, &q
, start
);
387 memcpy_safe(q
, d
, l
);
391 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
397 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
401 ((uint8_t*) d
)[0] = v
;
406 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
412 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
416 unaligned_write_be16(d
, v
);
421 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
427 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
431 unaligned_write_be32(d
, v
);
436 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
440 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
443 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
448 assert(s
|| size
== 0);
453 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
457 ((uint8_t*) d
)[0] = (uint8_t) size
;
459 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
464 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
468 /* Append a label to a packet. Optionally, does this in DNSSEC
469 * canonical form, if this label is marked as a candidate for
470 * it, and the canonical form logic is enabled for the
476 if (l
> DNS_LABEL_MAX
)
479 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
483 *(w
++) = (uint8_t) l
;
485 if (p
->canonical_form
&& canonical_candidate
) {
488 /* Generate in canonical form, as defined by DNSSEC
489 * RFC 4034, Section 6.2, i.e. all lower-case. */
491 for (i
= 0; i
< l
; i
++)
492 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
494 /* Otherwise, just copy the string unaltered. This is
495 * essential for DNS-SD, where the casing of labels
496 * matters and needs to be retained. */
502 int dns_packet_append_name(
505 bool allow_compression
,
506 bool canonical_candidate
,
515 if (p
->refuse_compression
)
516 allow_compression
= false;
518 saved_size
= p
->size
;
520 while (!dns_name_is_root(name
)) {
521 const char *z
= name
;
522 char label
[DNS_LABEL_MAX
];
525 if (allow_compression
)
526 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
531 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
539 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
543 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
547 if (allow_compression
) {
548 _cleanup_free_
char *s
= NULL
;
556 r
= hashmap_ensure_allocated(&p
->names
, &dns_name_hash_ops
);
560 r
= hashmap_put(p
->names
, s
, SIZE_TO_PTR(n
));
568 r
= dns_packet_append_uint8(p
, 0, NULL
);
579 dns_packet_truncate(p
, saved_size
);
583 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
591 saved_size
= p
->size
;
593 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
597 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
601 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH
: k
->class;
602 r
= dns_packet_append_uint16(p
, class, NULL
);
612 dns_packet_truncate(p
, saved_size
);
616 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
624 saved_size
= p
->size
;
626 r
= dns_packet_append_uint8(p
, window
, NULL
);
630 r
= dns_packet_append_uint8(p
, length
, NULL
);
634 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
643 dns_packet_truncate(p
, saved_size
);
647 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
651 uint8_t bitmaps
[32] = {};
658 saved_size
= p
->size
;
660 BITMAP_FOREACH(n
, types
, i
) {
663 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
664 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
674 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
677 if (bitmaps
[entry
/ 8] != 0) {
678 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
688 dns_packet_truncate(p
, saved_size
);
692 /* Append the OPT pseudo-RR described in RFC6891 */
693 int dns_packet_append_opt(DnsPacket
*p
, uint16_t max_udp_size
, bool edns0_do
, int rcode
, size_t *start
) {
698 /* we must never advertise supported packet size smaller than the legacy max */
699 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
701 assert(rcode
<= _DNS_RCODE_MAX
);
703 if (p
->opt_start
!= (size_t) -1)
706 assert(p
->opt_size
== (size_t) -1);
708 saved_size
= p
->size
;
711 r
= dns_packet_append_uint8(p
, 0, NULL
);
716 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
720 /* class: maximum udp packet that can be received */
721 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
725 /* extended RCODE and VERSION */
726 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
730 /* flags: DNSSEC OK (DO), see RFC3225 */
731 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
736 if (edns0_do
&& !DNS_PACKET_QR(p
)) {
737 /* If DO is on and this is not a reply, also append RFC6975 Algorithm data */
739 static const uint8_t rfc6975
[] = {
741 0, 5, /* OPTION_CODE: DAU */
742 #if HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600
743 0, 7, /* LIST_LENGTH */
745 0, 6, /* LIST_LENGTH */
747 DNSSEC_ALGORITHM_RSASHA1
,
748 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
749 DNSSEC_ALGORITHM_RSASHA256
,
750 DNSSEC_ALGORITHM_RSASHA512
,
751 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
752 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
753 #if HAVE_GCRYPT && GCRYPT_VERSION_NUMBER >= 0x010600
754 DNSSEC_ALGORITHM_ED25519
,
757 0, 6, /* OPTION_CODE: DHU */
758 0, 3, /* LIST_LENGTH */
760 DNSSEC_DIGEST_SHA256
,
761 DNSSEC_DIGEST_SHA384
,
763 0, 7, /* OPTION_CODE: N3U */
764 0, 1, /* LIST_LENGTH */
765 NSEC3_ALGORITHM_SHA1
,
768 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
);
772 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
);
774 r
= dns_packet_append_uint16(p
, 0, NULL
);
778 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
780 p
->opt_start
= saved_size
;
781 p
->opt_size
= p
->size
- saved_size
;
789 dns_packet_truncate(p
, saved_size
);
793 int dns_packet_truncate_opt(DnsPacket
*p
) {
796 if (p
->opt_start
== (size_t) -1) {
797 assert(p
->opt_size
== (size_t) -1);
801 assert(p
->opt_size
!= (size_t) -1);
802 assert(DNS_PACKET_ARCOUNT(p
) > 0);
804 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
807 dns_packet_truncate(p
, p
->opt_start
);
808 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
809 p
->opt_start
= p
->opt_size
= (size_t) -1;
814 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
816 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
823 saved_size
= p
->size
;
825 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
829 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
830 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
834 /* Initially we write 0 here */
835 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
839 rds
= p
->size
- saved_size
;
841 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
844 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
848 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
852 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
856 /* RFC 2782 states "Unless and until permitted by future standards
857 * action, name compression is not to be used for this field." */
858 r
= dns_packet_append_name(p
, rr
->srv
.name
, false, false, NULL
);
865 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, false, NULL
);
869 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
873 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
876 case DNS_TYPE_SPF
: /* exactly the same as TXT */
879 if (!rr
->txt
.items
) {
880 /* RFC 6763, section 6.1 suggests to generate
881 * single empty string for an empty array. */
883 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
889 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
890 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
900 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
904 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
908 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, false, NULL
);
912 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, false, NULL
);
916 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
920 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
924 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
928 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
932 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
936 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
940 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, false, NULL
);
944 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
948 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
952 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
956 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
960 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
964 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
968 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
972 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
976 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
980 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
984 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
988 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
992 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
996 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
999 case DNS_TYPE_DNSKEY
:
1000 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
1004 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
1008 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
1012 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1015 case DNS_TYPE_RRSIG
:
1016 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1020 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1024 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1028 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1032 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1036 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1040 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1044 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1048 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1052 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1056 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1062 case DNS_TYPE_NSEC3
:
1063 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1067 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1071 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1075 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1079 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1083 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1087 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1091 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1098 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1102 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1106 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1110 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1114 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1118 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1122 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1126 case DNS_TYPE_OPENPGPKEY
:
1127 case _DNS_TYPE_INVALID
: /* unparseable */
1130 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1136 /* Let's calculate the actual data size and update the field */
1137 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1138 if (rdlength
> 0xFFFF) {
1144 p
->size
= rdlength_offset
;
1145 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1151 *start
= saved_size
;
1159 dns_packet_truncate(p
, saved_size
);
1163 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1164 DnsResourceKey
*key
;
1169 DNS_QUESTION_FOREACH(key
, q
) {
1170 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1178 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
) {
1179 DnsResourceRecord
*rr
;
1180 DnsAnswerFlags flags
;
1185 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1186 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1194 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1197 if (p
->rindex
+ sz
> p
->size
)
1201 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1210 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1212 assert(idx
<= p
->size
);
1213 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1218 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1225 r
= dns_packet_read(p
, sz
, &q
, start
);
1233 static int dns_packet_read_memdup(
1234 DnsPacket
*p
, size_t size
,
1235 void **ret
, size_t *ret_size
,
1236 size_t *ret_start
) {
1245 r
= dns_packet_read(p
, size
, &src
, &start
);
1254 copy
= memdup(src
, size
);
1269 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1275 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1279 *ret
= ((uint8_t*) d
)[0];
1283 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1289 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1293 *ret
= unaligned_read_be16(d
);
1298 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1304 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1308 *ret
= unaligned_read_be32(d
);
1313 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1314 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1321 INIT_REWINDER(rewinder
, p
);
1323 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1327 r
= dns_packet_read(p
, c
, &d
, NULL
);
1331 if (memchr(d
, 0, c
))
1338 if (!utf8_is_valid(t
)) {
1346 *start
= rewinder
.saved_rindex
;
1347 CANCEL_REWINDER(rewinder
);
1352 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1353 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1358 INIT_REWINDER(rewinder
, p
);
1360 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1364 r
= dns_packet_read(p
, c
, ret
, NULL
);
1371 *start
= rewinder
.saved_rindex
;
1372 CANCEL_REWINDER(rewinder
);
1377 int dns_packet_read_name(
1380 bool allow_compression
,
1383 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1384 size_t after_rindex
= 0, jump_barrier
;
1385 _cleanup_free_
char *ret
= NULL
;
1386 size_t n
= 0, allocated
= 0;
1392 INIT_REWINDER(rewinder
, p
);
1393 jump_barrier
= p
->rindex
;
1395 if (p
->refuse_compression
)
1396 allow_compression
= false;
1401 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1412 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1416 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1424 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
1430 } else if (allow_compression
&& (c
& 0xc0) == 0xc0) {
1434 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1438 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1439 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1442 if (after_rindex
== 0)
1443 after_rindex
= p
->rindex
;
1445 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1452 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
1457 if (after_rindex
!= 0)
1458 p
->rindex
= after_rindex
;
1460 *_ret
= TAKE_PTR(ret
);
1463 *start
= rewinder
.saved_rindex
;
1464 CANCEL_REWINDER(rewinder
);
1469 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1472 const uint8_t *bitmap
;
1476 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1481 INIT_REWINDER(rewinder
, p
);
1483 r
= bitmap_ensure_allocated(types
);
1487 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1491 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1495 if (length
== 0 || length
> 32)
1498 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1502 for (i
= 0; i
< length
; i
++) {
1503 uint8_t bitmask
= 1 << 7;
1513 for (; bitmask
; bit
++, bitmask
>>= 1)
1514 if (bitmap
[i
] & bitmask
) {
1517 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1519 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1520 if (dns_type_is_pseudo(n
))
1523 r
= bitmap_set(*types
, n
);
1533 *start
= rewinder
.saved_rindex
;
1534 CANCEL_REWINDER(rewinder
);
1539 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1540 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1543 INIT_REWINDER(rewinder
, p
);
1545 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1546 r
= dns_packet_read_type_window(p
, types
, NULL
);
1550 /* don't read past end of current RR */
1551 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1555 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1559 *start
= rewinder
.saved_rindex
;
1560 CANCEL_REWINDER(rewinder
);
1565 int dns_packet_read_key(DnsPacket
*p
, DnsResourceKey
**ret
, bool *ret_cache_flush
, size_t *start
) {
1566 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1567 _cleanup_free_
char *name
= NULL
;
1568 bool cache_flush
= false;
1569 uint16_t class, type
;
1570 DnsResourceKey
*key
;
1575 INIT_REWINDER(rewinder
, p
);
1577 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1581 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1585 r
= dns_packet_read_uint16(p
, &class, NULL
);
1589 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1590 /* See RFC6762, Section 10.2 */
1592 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1593 class &= ~MDNS_RR_CACHE_FLUSH
;
1598 key
= dns_resource_key_new_consume(class, type
, name
);
1605 if (ret_cache_flush
)
1606 *ret_cache_flush
= cache_flush
;
1608 *start
= rewinder
.saved_rindex
;
1609 CANCEL_REWINDER(rewinder
);
1614 static bool loc_size_ok(uint8_t size
) {
1615 uint8_t m
= size
>> 4, e
= size
& 0xF;
1617 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1620 int dns_packet_read_rr(DnsPacket
*p
, DnsResourceRecord
**ret
, bool *ret_cache_flush
, size_t *start
) {
1621 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1622 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1623 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1632 INIT_REWINDER(rewinder
, p
);
1634 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1638 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1641 rr
= dns_resource_record_new(key
);
1645 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1649 /* RFC 2181, Section 8, suggests to
1650 * treat a TTL with the MSB set as a zero TTL. */
1651 if (rr
->ttl
& UINT32_C(0x80000000))
1654 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1658 if (p
->rindex
+ rdlength
> p
->size
)
1663 switch (rr
->key
->type
) {
1666 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1669 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1672 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1675 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1680 case DNS_TYPE_CNAME
:
1681 case DNS_TYPE_DNAME
:
1682 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1685 case DNS_TYPE_HINFO
:
1686 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1690 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1693 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1695 if (rdlength
<= 0) {
1696 r
= dns_txt_item_new_empty(&rr
->txt
.items
);
1700 DnsTxtItem
*last
= NULL
;
1702 while (p
->rindex
< offset
+ rdlength
) {
1707 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1711 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1715 memcpy(i
->data
, data
, sz
);
1718 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1727 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1731 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1735 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1739 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1743 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1747 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1751 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1755 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1759 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1763 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1767 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1770 case DNS_TYPE_LOC
: {
1774 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1779 rr
->loc
.version
= t
;
1781 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1785 if (!loc_size_ok(rr
->loc
.size
))
1788 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1792 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1795 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1799 if (!loc_size_ok(rr
->loc
.vert_pre
))
1802 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1806 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1810 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1816 dns_packet_rewind(p
, pos
);
1817 rr
->unparseable
= true;
1823 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1827 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1831 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1838 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1839 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1844 if (rr
->ds
.digest_size
<= 0)
1845 /* the accepted size depends on the algorithm, but for now
1846 just ensure that the value is greater than zero */
1851 case DNS_TYPE_SSHFP
:
1852 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1856 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1863 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1864 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1867 if (rr
->sshfp
.fingerprint_size
<= 0)
1868 /* the accepted size depends on the algorithm, but for now
1869 just ensure that the value is greater than zero */
1874 case DNS_TYPE_DNSKEY
:
1875 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1879 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1883 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1890 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1891 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1894 if (rr
->dnskey
.key_size
<= 0)
1895 /* the accepted size depends on the algorithm, but for now
1896 just ensure that the value is greater than zero */
1901 case DNS_TYPE_RRSIG
:
1902 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1906 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1910 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1914 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1918 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1922 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1926 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1930 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1934 if (rdlength
+ offset
< p
->rindex
)
1937 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
1938 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
1941 if (rr
->rrsig
.signature_size
<= 0)
1942 /* the accepted size depends on the algorithm, but for now
1943 just ensure that the value is greater than zero */
1948 case DNS_TYPE_NSEC
: {
1951 * RFC6762, section 18.14 explictly states mDNS should use name compression.
1952 * This contradicts RFC3845, section 2.1.1
1955 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
1957 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
1961 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1963 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1964 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1965 * without the NSEC bit set. */
1969 case DNS_TYPE_NSEC3
: {
1972 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
1976 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
1980 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
1984 /* this may be zero */
1985 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1989 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
1993 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2000 r
= dns_packet_read_memdup(p
, size
,
2001 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
2006 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2008 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
2014 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2018 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2022 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2029 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2030 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2033 if (rr
->tlsa
.data_size
<= 0)
2034 /* the accepted size depends on the algorithm, but for now
2035 just ensure that the value is greater than zero */
2041 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2045 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2049 if (rdlength
+ offset
< p
->rindex
)
2052 r
= dns_packet_read_memdup(p
,
2053 rdlength
+ offset
- p
->rindex
,
2054 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2058 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2059 case DNS_TYPE_OPENPGPKEY
:
2062 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2068 if (p
->rindex
!= offset
+ rdlength
)
2071 *ret
= TAKE_PTR(rr
);
2073 if (ret_cache_flush
)
2074 *ret_cache_flush
= cache_flush
;
2076 *start
= rewinder
.saved_rindex
;
2077 CANCEL_REWINDER(rewinder
);
2082 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2084 bool found_dau_dhu_n3u
= false;
2087 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2091 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2093 /* Check that the version is 0 */
2094 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2096 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2100 l
= rr
->opt
.data_size
;
2102 uint16_t option_code
, option_length
;
2104 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2108 option_code
= unaligned_read_be16(p
);
2109 option_length
= unaligned_read_be16(p
+ 2);
2111 if (l
< option_length
+ 4U)
2114 /* RFC 6975 DAU, DHU or N3U fields found. */
2115 if (IN_SET(option_code
, 5, 6, 7))
2116 found_dau_dhu_n3u
= true;
2118 p
+= option_length
+ 4U;
2119 l
-= option_length
+ 4U;
2122 *rfc6975
= found_dau_dhu_n3u
;
2126 static int dns_packet_extract_question(DnsPacket
*p
, DnsQuestion
**ret_question
) {
2127 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2131 n
= DNS_PACKET_QDCOUNT(p
);
2133 question
= dns_question_new(n
);
2137 _cleanup_set_free_ Set
*keys
= NULL
; /* references to keys are kept by Question */
2139 keys
= set_new(&dns_resource_key_hash_ops
);
2143 r
= set_reserve(keys
, n
* 2); /* Higher multipliers give slightly higher efficiency through
2144 * hash collisions, but the gains quickly drop of after 2. */
2148 for (i
= 0; i
< n
; i
++) {
2149 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2152 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2159 if (!dns_type_is_valid_query(key
->type
))
2162 r
= set_put(keys
, key
);
2166 /* Already in the Question, let's skip */
2169 r
= dns_question_add_raw(question
, key
);
2175 *ret_question
= TAKE_PTR(question
);
2180 static int dns_packet_extract_answer(DnsPacket
*p
, DnsAnswer
**ret_answer
) {
2181 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2183 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2184 bool bad_opt
= false;
2187 n
= DNS_PACKET_RRCOUNT(p
);
2191 answer
= dns_answer_new(n
);
2195 for (i
= 0; i
< n
; i
++) {
2196 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2197 bool cache_flush
= false;
2199 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, NULL
);
2203 /* Try to reduce memory usage a bit */
2205 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2207 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2210 if (p
->opt
|| bad_opt
) {
2211 /* Multiple OPT RRs? if so, let's ignore all, because there's
2212 * something wrong with the server, and if one is valid we wouldn't
2213 * know which one. */
2214 log_debug("Multiple OPT RRs detected, ignoring all.");
2219 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2220 /* If the OPT RR is not owned by the root domain, then it is bad,
2221 * let's ignore it. */
2222 log_debug("OPT RR is not owned by root domain, ignoring.");
2227 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2228 /* OPT RR is in the wrong section? Some Belkin routers do this. This
2229 * is a hint the EDNS implementation is borked, like the Belkin one
2230 * is, hence ignore it. */
2231 log_debug("OPT RR in wrong section, ignoring.");
2236 if (!opt_is_good(rr
, &has_rfc6975
)) {
2237 log_debug("Malformed OPT RR, ignoring.");
2242 if (DNS_PACKET_QR(p
)) {
2243 /* Additional checks for responses */
2245 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
)) {
2246 /* If this is a reply and we don't know the EDNS version
2247 * then something is weird... */
2248 log_debug("EDNS version newer that our request, bad server.");
2253 /* If the OPT RR contains RFC6975 algorithm data, then this
2254 * is indication that the server just copied the OPT it got
2255 * from us (which contained that data) back into the reply.
2256 * If so, then it doesn't properly support EDNS, as RFC6975
2257 * makes it very clear that the algorithm data should only
2258 * be contained in questions, never in replies. Crappy
2259 * Belkin routers copy the OPT data for example, hence let's
2260 * detect this so that we downgrade early. */
2261 log_debug("OPT RR contained RFC6975 data, ignoring.");
2267 p
->opt
= dns_resource_record_ref(rr
);
2269 /* According to RFC 4795, section 2.9. only the RRs from the Answer section
2270 * shall be cached. Hence mark only those RRs as cacheable by default, but
2271 * not the ones from the Additional or Authority sections. */
2272 DnsAnswerFlags flags
=
2273 (i
< DNS_PACKET_ANCOUNT(p
) ? DNS_ANSWER_CACHEABLE
: 0) |
2274 (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
? DNS_ANSWER_SHARED_OWNER
: 0);
2276 r
= dns_answer_add(answer
, rr
, p
->ifindex
, flags
);
2281 /* Remember this RR, so that we potentically can merge it's ->key object with the
2282 * next RR. Note that we only do this if we actually decided to keep the RR around.
2284 dns_resource_record_unref(previous
);
2285 previous
= dns_resource_record_ref(rr
);
2289 p
->opt
= dns_resource_record_unref(p
->opt
);
2291 *ret_answer
= TAKE_PTR(answer
);
2296 int dns_packet_extract(DnsPacket
*p
) {
2297 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2298 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2299 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2305 INIT_REWINDER(rewinder
, p
);
2306 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2308 r
= dns_packet_extract_question(p
, &question
);
2312 r
= dns_packet_extract_answer(p
, &answer
);
2316 p
->question
= TAKE_PTR(question
);
2317 p
->answer
= TAKE_PTR(answer
);
2319 p
->extracted
= true;
2321 /* no CANCEL, always rewind */
2325 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2331 /* Checks if the specified packet is a reply for the specified
2332 * key and the specified key is the only one in the question
2335 if (DNS_PACKET_QR(p
) != 1)
2338 /* Let's unpack the packet, if that hasn't happened yet. */
2339 r
= dns_packet_extract(p
);
2346 if (p
->question
->n_keys
!= 1)
2349 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2352 static void dns_packet_hash_func(const DnsPacket
*s
, struct siphash
*state
) {
2355 siphash24_compress(&s
->size
, sizeof(s
->size
), state
);
2356 siphash24_compress(DNS_PACKET_DATA((DnsPacket
*) s
), s
->size
, state
);
2359 static int dns_packet_compare_func(const DnsPacket
*x
, const DnsPacket
*y
) {
2362 r
= CMP(x
->size
, y
->size
);
2366 return memcmp(DNS_PACKET_DATA((DnsPacket
*) x
), DNS_PACKET_DATA((DnsPacket
*) y
), x
->size
);
2369 DEFINE_HASH_OPS(dns_packet_hash_ops
, DnsPacket
, dns_packet_hash_func
, dns_packet_compare_func
);
2371 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2372 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2373 [DNS_RCODE_FORMERR
] = "FORMERR",
2374 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2375 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2376 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2377 [DNS_RCODE_REFUSED
] = "REFUSED",
2378 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2379 [DNS_RCODE_YXRRSET
] = "YRRSET",
2380 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2381 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2382 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2383 [DNS_RCODE_BADVERS
] = "BADVERS",
2384 [DNS_RCODE_BADKEY
] = "BADKEY",
2385 [DNS_RCODE_BADTIME
] = "BADTIME",
2386 [DNS_RCODE_BADMODE
] = "BADMODE",
2387 [DNS_RCODE_BADNAME
] = "BADNAME",
2388 [DNS_RCODE_BADALG
] = "BADALG",
2389 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2390 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
2392 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2394 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2395 [DNS_PROTOCOL_DNS
] = "dns",
2396 [DNS_PROTOCOL_MDNS
] = "mdns",
2397 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2399 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);