1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
5 #include "dns-domain.h"
8 #include "memory-util.h"
9 #include "resolved-dns-answer.h"
10 #include "resolved-dns-packet.h"
11 #include "resolved-dns-question.h"
12 #include "resolved-dns-rr.h"
14 #include "siphash24.h"
15 #include "stdio-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "time-util.h"
19 #include "unaligned.h"
22 #define EDNS0_OPT_DO (1<<15)
24 assert_cc(DNS_PACKET_SIZE_START
> DNS_PACKET_HEADER_SIZE
);
26 typedef struct DnsPacketRewinder
{
31 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
33 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
36 #define REWINDER_INIT(p) { \
38 .saved_rindex = (p)->rindex, \
40 #define CANCEL_REWINDER(rewinder) do { (rewinder).packet = NULL; } while (0)
42 uint16_t dns_packet_rcode(DnsPacket
*p
) {
48 rcode
= (uint16_t) ((p
->opt
->ttl
>> 20) & 0xFF0);
52 return rcode
| (be16toh(DNS_PACKET_HEADER(p
)->flags
) & 0xF);
55 uint16_t dns_packet_payload_size_max(DnsPacket
*p
) {
58 /* Returns the advertised maximum size for replies, or the DNS default if there's nothing defined. */
60 if (p
->ipproto
== IPPROTO_TCP
) /* we ignore EDNS(0) size data on TCP, like everybody else */
61 return DNS_PACKET_SIZE_MAX
;
64 return MAX(DNS_PACKET_UNICAST_SIZE_MAX
, p
->opt
->key
->class);
66 return DNS_PACKET_UNICAST_SIZE_MAX
;
69 bool dns_packet_do(DnsPacket
*p
) {
75 return !!(p
->opt
->ttl
& (1U << 15));
78 bool dns_packet_version_supported(DnsPacket
*p
) {
81 /* Returns true if this packet is in a version we support. Which means either non-EDNS or EDNS(0), but not EDNS
82 * of any newer versions */
87 return DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(p
->opt
);
93 size_t min_alloc_dsize
,
100 assert(max_size
>= DNS_PACKET_HEADER_SIZE
);
102 if (max_size
> DNS_PACKET_SIZE_MAX
)
103 max_size
= DNS_PACKET_SIZE_MAX
;
105 /* The caller may not check what is going to be truly allocated, so do not allow to
106 * allocate a DNS packet bigger than DNS_PACKET_SIZE_MAX.
108 if (min_alloc_dsize
> DNS_PACKET_SIZE_MAX
)
109 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
110 "Requested packet data size too big: %zu",
113 /* When dns_packet_new() is called with min_alloc_dsize == 0, allocate more than the
114 * absolute minimum (which is the dns packet header size), to avoid
115 * resizing immediately again after appending the first data to the packet.
117 if (min_alloc_dsize
< DNS_PACKET_HEADER_SIZE
)
118 a
= DNS_PACKET_SIZE_START
;
122 /* round up to next page size */
123 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
125 /* make sure we never allocate more than useful */
129 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
135 .protocol
= protocol
,
136 .size
= DNS_PACKET_HEADER_SIZE
,
137 .rindex
= DNS_PACKET_HEADER_SIZE
,
139 .max_size
= max_size
,
140 .opt_start
= SIZE_MAX
,
141 .opt_size
= SIZE_MAX
,
149 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
155 h
= DNS_PACKET_HEADER(p
);
157 switch (p
->protocol
) {
158 case DNS_PROTOCOL_LLMNR
:
161 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
172 case DNS_PROTOCOL_MDNS
:
173 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
177 0 /* rd (ask for recursion) */,
187 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
191 1 /* rd (ask for recursion) */,
194 dnssec_checking_disabled
/* cd */,
199 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t min_alloc_dsize
, bool dnssec_checking_disabled
) {
205 r
= dns_packet_new(&p
, protocol
, min_alloc_dsize
, DNS_PACKET_SIZE_MAX
);
209 /* Always set the TC bit to 0 initially.
210 * If there are multiple packets later, we'll update the bit shortly before sending.
212 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
218 int dns_packet_dup(DnsPacket
**ret
, DnsPacket
*p
) {
225 r
= dns_packet_validate(p
);
229 c
= malloc(ALIGN(sizeof(DnsPacket
)) + p
->size
);
235 .protocol
= p
->protocol
,
237 .rindex
= DNS_PACKET_HEADER_SIZE
,
238 .allocated
= p
->size
,
239 .max_size
= p
->max_size
,
240 .opt_start
= SIZE_MAX
,
241 .opt_size
= SIZE_MAX
,
244 memcpy(DNS_PACKET_DATA(c
), DNS_PACKET_DATA(p
), p
->size
);
250 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
255 assert(!p
->on_stack
);
257 assert(p
->n_ref
> 0);
262 static void dns_packet_free(DnsPacket
*p
) {
267 dns_question_unref(p
->question
);
268 dns_answer_unref(p
->answer
);
269 dns_resource_record_unref(p
->opt
);
271 while ((s
= hashmap_steal_first_key(p
->names
)))
273 hashmap_free(p
->names
);
281 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
285 assert(p
->n_ref
> 0);
287 dns_packet_unref(p
->more
);
297 int dns_packet_validate(DnsPacket
*p
) {
300 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
303 if (p
->size
> DNS_PACKET_SIZE_MAX
)
309 int dns_packet_validate_reply(DnsPacket
*p
) {
314 r
= dns_packet_validate(p
);
318 if (DNS_PACKET_QR(p
) != 1)
321 if (DNS_PACKET_OPCODE(p
) != 0)
324 switch (p
->protocol
) {
326 case DNS_PROTOCOL_LLMNR
:
327 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
328 if (DNS_PACKET_QDCOUNT(p
) != 1)
333 case DNS_PROTOCOL_MDNS
:
334 /* RFC 6762, Section 18 */
335 if (dns_packet_rcode(p
) != 0)
347 int dns_packet_validate_query(DnsPacket
*p
) {
352 r
= dns_packet_validate(p
);
356 if (DNS_PACKET_QR(p
) != 0)
359 if (DNS_PACKET_OPCODE(p
) != 0)
362 switch (p
->protocol
) {
364 case DNS_PROTOCOL_DNS
:
365 if (DNS_PACKET_TC(p
))
368 if (DNS_PACKET_QDCOUNT(p
) != 1)
371 if (DNS_PACKET_ANCOUNT(p
) > 0)
374 /* Note, in most cases, DNS query packet does not have authority section. But some query
375 * types, e.g. IXFR, have Authority sections. Hence, unlike the check for LLMNR, we do not
376 * check DNS_PACKET_NSCOUNT(p) here. */
379 case DNS_PROTOCOL_LLMNR
:
380 if (DNS_PACKET_TC(p
))
383 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
384 if (DNS_PACKET_QDCOUNT(p
) != 1)
387 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
388 if (DNS_PACKET_ANCOUNT(p
) > 0)
391 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
392 if (DNS_PACKET_NSCOUNT(p
) > 0)
397 case DNS_PROTOCOL_MDNS
:
398 /* Note, mDNS query may have truncation flag. So, unlike the check for DNS and LLMNR,
399 * we do not check DNS_PACKET_TC(p) here. */
401 /* RFC 6762, Section 18 specifies that messages with non-zero RCODE
402 * must be silently ignored, and that we must ignore the values of
403 * AA, RD, RA, AD, and CD bits. */
404 if (dns_packet_rcode(p
) != 0)
416 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
419 if (p
->size
+ add
> p
->allocated
) {
422 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
424 ms
= dns_packet_size_max(p
);
428 if (p
->size
+ add
> a
)
434 d
= realloc(p
->_data
, a
);
440 p
->_data
= malloc(a
);
444 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
445 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
455 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
461 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
470 HASHMAP_FOREACH_KEY(n
, s
, p
->names
) {
472 if (PTR_TO_SIZE(n
) < sz
)
475 hashmap_remove(p
->names
, s
);
482 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
488 r
= dns_packet_extend(p
, l
, &q
, start
);
492 memcpy_safe(q
, d
, l
);
496 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
502 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
506 ((uint8_t*) d
)[0] = v
;
511 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
517 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
521 unaligned_write_be16(d
, v
);
526 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
532 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
536 unaligned_write_be32(d
, v
);
541 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
545 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
548 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
553 assert(s
|| size
== 0);
558 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
562 ((uint8_t*) d
)[0] = (uint8_t) size
;
564 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
569 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
573 /* Append a label to a packet. Optionally, does this in DNSSEC
574 * canonical form, if this label is marked as a candidate for
575 * it, and the canonical form logic is enabled for the
581 if (l
> DNS_LABEL_MAX
)
584 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
588 *(w
++) = (uint8_t) l
;
590 if (p
->canonical_form
&& canonical_candidate
)
591 /* Generate in canonical form, as defined by DNSSEC
592 * RFC 4034, Section 6.2, i.e. all lower-case. */
593 for (size_t i
= 0; i
< l
; i
++)
594 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
596 /* Otherwise, just copy the string unaltered. This is
597 * essential for DNS-SD, where the casing of labels
598 * matters and needs to be retained. */
604 int dns_packet_append_name(
607 bool allow_compression
,
608 bool canonical_candidate
,
611 _cleanup_free_
char **added_entries
= NULL
; /* doesn't own the strings! this is just regular pointer array, not a NULL-terminated strv! */
612 size_t n_added_entries
= 0, saved_size
;
618 r
= dns_name_is_valid(name
);
624 if (p
->refuse_compression
)
625 allow_compression
= false;
627 saved_size
= p
->size
;
629 while (!dns_name_is_root(name
)) {
630 const char *z
= name
;
631 char label
[DNS_LABEL_MAX
+1];
634 if (allow_compression
)
635 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
640 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
648 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
652 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
656 if (allow_compression
) {
657 _cleanup_free_
char *s
= NULL
;
659 if (!GREEDY_REALLOC(added_entries
, n_added_entries
+ 1)) {
670 r
= hashmap_ensure_put(&p
->names
, &dns_name_hash_ops
, s
, SIZE_TO_PTR(n
));
674 /* Keep track of the entries we just added (note that the string is owned by the hashtable, not this array!) */
675 added_entries
[n_added_entries
++] = TAKE_PTR(s
);
679 r
= dns_packet_append_uint8(p
, 0, NULL
);
690 /* Remove all label compression names we added again */
691 FOREACH_ARRAY(s
, added_entries
, n_added_entries
) {
692 hashmap_remove(p
->names
, *s
);
696 dns_packet_truncate(p
, saved_size
);
700 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, const DnsAnswerFlags flags
, size_t *start
) {
708 saved_size
= p
->size
;
710 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
714 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
718 class = flags
& DNS_ANSWER_CACHE_FLUSH
? k
->class | MDNS_RR_CACHE_FLUSH_OR_QU
: k
->class;
719 r
= dns_packet_append_uint16(p
, class, NULL
);
729 dns_packet_truncate(p
, saved_size
);
733 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
741 saved_size
= p
->size
;
743 r
= dns_packet_append_uint8(p
, window
, NULL
);
747 r
= dns_packet_append_uint8(p
, length
, NULL
);
751 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
760 dns_packet_truncate(p
, saved_size
);
764 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
767 uint8_t bitmaps
[32] = {};
774 saved_size
= p
->size
;
776 BITMAP_FOREACH(n
, types
) {
779 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
780 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
790 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
793 if (bitmaps
[entry
/ 8] != 0) {
794 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
804 dns_packet_truncate(p
, saved_size
);
808 /* Append the OPT pseudo-RR described in RFC6891 */
809 int dns_packet_append_opt(
811 uint16_t max_udp_size
,
813 bool include_rfc6975
,
822 /* we must never advertise supported packet size smaller than the legacy max */
823 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
825 assert(rcode
<= _DNS_RCODE_MAX
);
827 if (p
->opt_start
!= SIZE_MAX
)
830 assert(p
->opt_size
== SIZE_MAX
);
832 saved_size
= p
->size
;
835 r
= dns_packet_append_uint8(p
, 0, NULL
);
840 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
844 /* class: maximum udp packet that can be received */
845 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
849 /* extended RCODE and VERSION */
850 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
854 /* flags: DNSSEC OK (DO), see RFC3225 */
855 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
859 if (edns0_do
&& include_rfc6975
) {
860 /* If DO is on and this is requested, also append RFC6975 Algorithm data. This is supposed to
861 * be done on queries, not on replies, hencer callers should turn this off when finishing off
864 static const uint8_t rfc6975
[] = {
866 0, DNS_EDNS_OPT_DAU
, /* OPTION_CODE */
868 0, 7, /* LIST_LENGTH */
870 0, 6, /* LIST_LENGTH */
872 DNSSEC_ALGORITHM_RSASHA1
,
873 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
874 DNSSEC_ALGORITHM_RSASHA256
,
875 DNSSEC_ALGORITHM_RSASHA512
,
876 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
877 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
879 DNSSEC_ALGORITHM_ED25519
,
882 0, DNS_EDNS_OPT_DHU
, /* OPTION_CODE */
883 0, 3, /* LIST_LENGTH */
885 DNSSEC_DIGEST_SHA256
,
886 DNSSEC_DIGEST_SHA384
,
888 0, DNS_EDNS_OPT_N3U
, /* OPTION_CODE */
889 0, 1, /* LIST_LENGTH */
890 NSEC3_ALGORITHM_SHA1
,
893 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
); /* RDLENGTH */
897 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
); /* the payload, as defined above */
901 if (strlen(nsid
) > UINT16_MAX
- 4) {
906 r
= dns_packet_append_uint16(p
, 4 + strlen(nsid
), NULL
); /* RDLENGTH */
910 r
= dns_packet_append_uint16(p
, 3, NULL
); /* OPTION-CODE: NSID */
914 r
= dns_packet_append_uint16(p
, strlen(nsid
), NULL
); /* OPTION-LENGTH */
918 r
= dns_packet_append_blob(p
, nsid
, strlen(nsid
), NULL
);
920 r
= dns_packet_append_uint16(p
, 0, NULL
);
924 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
926 p
->opt_start
= saved_size
;
927 p
->opt_size
= p
->size
- saved_size
;
930 *ret_start
= saved_size
;
935 dns_packet_truncate(p
, saved_size
);
939 int dns_packet_truncate_opt(DnsPacket
*p
) {
942 if (p
->opt_start
== SIZE_MAX
) {
943 assert(p
->opt_size
== SIZE_MAX
);
947 assert(p
->opt_size
!= SIZE_MAX
);
948 assert(DNS_PACKET_ARCOUNT(p
) > 0);
950 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
953 dns_packet_truncate(p
, p
->opt_start
);
954 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
955 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
960 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, const DnsAnswerFlags flags
, size_t *start
, size_t *rdata_start
) {
962 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
969 saved_size
= p
->size
;
971 r
= dns_packet_append_key(p
, rr
->key
, flags
, NULL
);
975 ttl
= flags
& DNS_ANSWER_GOODBYE
? 0 : rr
->ttl
;
976 r
= dns_packet_append_uint32(p
, ttl
, NULL
);
980 /* Initially we write 0 here */
981 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
985 rds
= p
->size
- saved_size
;
987 switch (rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
990 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
994 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
998 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
1002 /* RFC 2782 states "Unless and until permitted by future standards action, name compression
1003 * is not to be used for this field." Hence we turn off compression here. */
1004 r
= dns_packet_append_name(p
, rr
->srv
.name
, /* allow_compression= */ false, /* canonical_candidate= */ true, NULL
);
1009 case DNS_TYPE_CNAME
:
1010 case DNS_TYPE_DNAME
:
1011 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, true, NULL
);
1014 case DNS_TYPE_HINFO
:
1015 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
1019 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
1022 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1025 if (!rr
->txt
.items
) {
1026 /* RFC 6763, section 6.1 suggests to generate
1027 * single empty string for an empty array. */
1029 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
1033 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
1034 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
1043 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1047 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1051 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, true, NULL
);
1055 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, true, NULL
);
1059 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
1063 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
1067 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
1071 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
1075 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
1079 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
1083 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, true, NULL
);
1087 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
1091 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
1095 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
1099 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
1103 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
1107 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
1111 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
1115 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
1119 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
1123 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
1127 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
1130 case DNS_TYPE_SSHFP
:
1131 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
1135 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
1139 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
1142 case DNS_TYPE_DNSKEY
:
1143 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
1147 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
1151 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
1155 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
1158 case DNS_TYPE_RRSIG
:
1159 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
1163 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1167 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1171 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1175 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1179 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1183 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1187 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1191 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1195 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1199 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1205 case DNS_TYPE_NSEC3
:
1206 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1210 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1214 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1218 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1222 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1226 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1230 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1234 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1241 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1245 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1249 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1253 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1257 case DNS_TYPE_HTTPS
:
1258 r
= dns_packet_append_uint16(p
, rr
->svcb
.priority
, NULL
);
1262 r
= dns_packet_append_name(p
, rr
->svcb
.target_name
, false, false, NULL
);
1266 LIST_FOREACH(params
, i
, rr
->svcb
.params
) {
1267 r
= dns_packet_append_uint16(p
, i
->key
, NULL
);
1271 r
= dns_packet_append_uint16(p
, i
->length
, NULL
);
1275 r
= dns_packet_append_blob(p
, i
->value
, i
->length
, NULL
);
1282 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1286 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1290 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1293 case DNS_TYPE_NAPTR
:
1294 r
= dns_packet_append_uint16(p
, rr
->naptr
.order
, NULL
);
1298 r
= dns_packet_append_uint16(p
, rr
->naptr
.preference
, NULL
);
1302 r
= dns_packet_append_string(p
, rr
->naptr
.flags
, NULL
);
1306 r
= dns_packet_append_string(p
, rr
->naptr
.services
, NULL
);
1310 r
= dns_packet_append_string(p
, rr
->naptr
.regexp
, NULL
);
1314 r
= dns_packet_append_name(p
, rr
->naptr
.replacement
, /* allow_compression= */ false, /* canonical_candidate= */ true, NULL
);
1318 case DNS_TYPE_OPENPGPKEY
:
1319 case _DNS_TYPE_INVALID
: /* unparsable */
1321 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1326 /* Let's calculate the actual data size and update the field */
1327 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1328 if (rdlength
> 0xFFFF) {
1334 p
->size
= rdlength_offset
;
1335 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1341 *start
= saved_size
;
1349 dns_packet_truncate(p
, saved_size
);
1353 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1354 DnsResourceKey
*key
;
1359 DNS_QUESTION_FOREACH(key
, q
) {
1360 r
= dns_packet_append_key(p
, key
, 0, NULL
);
1368 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
, unsigned *completed
) {
1369 DnsResourceRecord
*rr
;
1370 DnsAnswerFlags flags
;
1375 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, a
) {
1376 r
= dns_packet_append_rr(p
, rr
, flags
, NULL
, NULL
);
1387 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1389 assert(p
->rindex
<= p
->size
);
1391 if (sz
> p
->size
- p
->rindex
)
1395 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1404 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1406 assert(idx
<= p
->size
);
1407 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1412 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1419 r
= dns_packet_read(p
, sz
, &q
, start
);
1427 static int dns_packet_read_memdup(
1428 DnsPacket
*p
, size_t size
,
1429 void **ret
, size_t *ret_size
,
1430 size_t *ret_start
) {
1439 r
= dns_packet_read(p
, size
, &src
, &start
);
1448 copy
= memdup(src
, size
);
1463 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1469 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1473 *ret
= ((uint8_t*) d
)[0];
1477 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1483 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1488 *ret
= unaligned_read_be16(d
);
1493 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1499 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1503 *ret
= unaligned_read_be32(d
);
1508 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1509 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1510 _cleanup_free_
char *t
= NULL
;
1517 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1521 r
= dns_packet_read(p
, c
, &d
, NULL
);
1525 r
= make_cstring(d
, c
, MAKE_CSTRING_REFUSE_TRAILING_NUL
, &t
);
1529 if (!utf8_is_valid(t
))
1535 *start
= rewinder
.saved_rindex
;
1536 CANCEL_REWINDER(rewinder
);
1541 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1544 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1548 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1552 r
= dns_packet_read(p
, c
, ret
, NULL
);
1559 *start
= rewinder
.saved_rindex
;
1560 CANCEL_REWINDER(rewinder
);
1565 int dns_packet_read_name(
1568 bool allow_compression
,
1569 size_t *ret_start
) {
1573 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1574 size_t after_rindex
= 0, jump_barrier
= p
->rindex
;
1575 _cleanup_free_
char *name
= NULL
;
1577 size_t n
= 0, m
= 0;
1580 if (p
->refuse_compression
)
1581 allow_compression
= false;
1586 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1597 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1601 if (!GREEDY_REALLOC(name
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1611 r
= dns_label_escape(label
, c
, name
+ n
, DNS_LABEL_ESCAPED_MAX
);
1618 if (m
> DNS_HOSTNAME_MAX
)
1622 } else if (allow_compression
&& FLAGS_SET(c
, 0xc0)) {
1626 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1630 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1631 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1634 if (after_rindex
== 0)
1635 after_rindex
= p
->rindex
;
1637 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1644 if (!GREEDY_REALLOC(name
, n
+ 1))
1649 if (after_rindex
!= 0)
1650 p
->rindex
= after_rindex
;
1653 *ret
= TAKE_PTR(name
);
1655 *ret_start
= rewinder
.saved_rindex
;
1657 CANCEL_REWINDER(rewinder
);
1662 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1666 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1667 uint8_t window
, length
;
1668 const uint8_t *bitmap
;
1673 r
= bitmap_ensure_allocated(types
);
1677 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1681 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1685 if (length
== 0 || length
> 32)
1688 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1692 for (uint8_t i
= 0; i
< length
; i
++) {
1693 uint8_t bitmask
= 1 << 7;
1703 for (; bitmask
; bit
++, bitmask
>>= 1)
1704 if (bitmap
[i
] & bitmask
) {
1707 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1709 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1710 if (dns_type_is_pseudo(n
))
1713 r
= bitmap_set(*types
, n
);
1723 *start
= rewinder
.saved_rindex
;
1724 CANCEL_REWINDER(rewinder
);
1729 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1730 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1733 while (p
->rindex
- rewinder
.saved_rindex
< size
) {
1734 r
= dns_packet_read_type_window(p
, types
, NULL
);
1738 assert(p
->rindex
>= rewinder
.saved_rindex
);
1740 /* don't read past end of current RR */
1741 if (p
->rindex
- rewinder
.saved_rindex
> size
)
1745 if (p
->rindex
- rewinder
.saved_rindex
!= size
)
1749 *start
= rewinder
.saved_rindex
;
1750 CANCEL_REWINDER(rewinder
);
1755 int dns_packet_read_key(
1757 DnsResourceKey
**ret
,
1758 bool *ret_cache_flush_or_qu
,
1759 size_t *ret_start
) {
1763 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1764 _cleanup_free_
char *name
= NULL
;
1765 bool cache_flush_or_qu
= false;
1766 uint16_t class, type
;
1769 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1773 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1777 r
= dns_packet_read_uint16(p
, &class, NULL
);
1781 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1782 /* See RFC6762, sections 5.4 and 10.2 */
1784 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH_OR_QU
)) {
1785 class &= ~MDNS_RR_CACHE_FLUSH_OR_QU
;
1786 cache_flush_or_qu
= true;
1791 DnsResourceKey
*key
;
1793 key
= dns_resource_key_new_consume(class, type
, name
);
1801 if (ret_cache_flush_or_qu
)
1802 *ret_cache_flush_or_qu
= cache_flush_or_qu
;
1804 *ret_start
= rewinder
.saved_rindex
;
1806 CANCEL_REWINDER(rewinder
);
1810 static bool loc_size_ok(uint8_t size
) {
1811 uint8_t m
= size
>> 4, e
= size
& 0xF;
1813 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1816 static bool dns_svc_param_is_valid(DnsSvcParam
*i
) {
1821 /* RFC 9460, section 7.1.1: alpn-ids must exactly fill SvcParamValue */
1822 case DNS_SVC_PARAM_KEY_ALPN
: {
1826 while (sz
< i
->length
)
1827 sz
+= 1 + i
->value
[sz
]; /* N.B. will not overflow */
1828 return sz
== i
->length
;
1831 /* RFC 9460, section 7.1.1: value must be empty */
1832 case DNS_SVC_PARAM_KEY_NO_DEFAULT_ALPN
:
1833 return i
->length
== 0;
1835 /* RFC 9460, section 7.2 */
1836 case DNS_SVC_PARAM_KEY_PORT
:
1837 return i
->length
== 2;
1839 /* RFC 9460, section 7.3: addrs must exactly fill SvcParamValue */
1840 case DNS_SVC_PARAM_KEY_IPV4HINT
:
1841 return i
->length
> 0 && i
->length
% (sizeof (struct in_addr
)) == 0;
1842 case DNS_SVC_PARAM_KEY_IPV6HINT
:
1843 return i
->length
> 0 && i
->length
% (sizeof (struct in6_addr
)) == 0;
1845 /* Otherwise, permit any value */
1851 int dns_packet_read_rr(
1853 DnsResourceRecord
**ret
,
1854 bool *ret_cache_flush
,
1855 size_t *ret_start
) {
1859 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
1860 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1861 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1867 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1871 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1874 rr
= dns_resource_record_new(key
);
1878 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1882 /* RFC 2181, Section 8, suggests to treat a TTL with the MSB set as a zero TTL. We avoid doing this
1883 * for OPT records so that all 8 bits of the extended RCODE may be used. */
1884 if (key
->type
!= DNS_TYPE_OPT
&& rr
->ttl
& UINT32_C(0x80000000))
1887 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1891 if (rdlength
> p
->size
- p
->rindex
)
1896 switch (rr
->key
->type
) {
1899 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1902 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1905 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1909 /* RFC 2782 states "Unless and until permitted by future standards action, name compression
1910 * is not to be used for this field." Nonetheless, we support it here, in the interest of
1911 * increasing compatibility with implementations that do not implement this correctly. After
1912 * all we didn't do this right once upon a time ourselves (see
1913 * https://github.com/systemd/systemd/issues/9793). */
1914 r
= dns_packet_read_name(p
, &rr
->srv
.name
, /* allow_compression= */ true, NULL
);
1919 case DNS_TYPE_CNAME
:
1920 case DNS_TYPE_DNAME
:
1921 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1924 case DNS_TYPE_HINFO
:
1925 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1929 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1932 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1934 if (rdlength
<= 0) {
1935 r
= dns_txt_item_new_empty(&rr
->txt
.items
);
1939 DnsTxtItem
*last
= NULL
;
1941 while (p
->rindex
- offset
< rdlength
) {
1946 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1950 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1954 memcpy(i
->data
, data
, sz
);
1957 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1966 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1970 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1974 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1978 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1982 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1986 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1990 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1994 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1998 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
2002 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
2006 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
2009 case DNS_TYPE_LOC
: {
2013 r
= dns_packet_read_uint8(p
, &t
, &pos
);
2018 rr
->loc
.version
= t
;
2020 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
2024 if (!loc_size_ok(rr
->loc
.size
))
2027 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
2031 if (!loc_size_ok(rr
->loc
.horiz_pre
))
2034 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
2038 if (!loc_size_ok(rr
->loc
.vert_pre
))
2041 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
2045 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
2049 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
2055 dns_packet_rewind(p
, pos
);
2056 rr
->unparsable
= true;
2062 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
2066 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
2070 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
2077 r
= dns_packet_read_memdup(p
, rdlength
- 4,
2078 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
2083 if (rr
->ds
.digest_size
<= 0)
2084 /* the accepted size depends on the algorithm, but for now
2085 just ensure that the value is greater than zero */
2090 case DNS_TYPE_SSHFP
:
2091 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
2095 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
2102 r
= dns_packet_read_memdup(p
, rdlength
- 2,
2103 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
2106 if (rr
->sshfp
.fingerprint_size
<= 0)
2107 /* the accepted size depends on the algorithm, but for now
2108 just ensure that the value is greater than zero */
2113 case DNS_TYPE_DNSKEY
:
2114 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
2118 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
2122 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
2129 r
= dns_packet_read_memdup(p
, rdlength
- 4,
2130 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
2133 if (rr
->dnskey
.key_size
<= 0)
2134 /* the accepted size depends on the algorithm, but for now
2135 just ensure that the value is greater than zero */
2140 case DNS_TYPE_RRSIG
:
2141 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
2145 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
2149 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
2153 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
2157 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
2161 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
2165 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
2169 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
2173 if (rdlength
< p
->rindex
- offset
)
2176 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
2177 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
2180 if (rr
->rrsig
.signature_size
<= 0)
2181 /* the accepted size depends on the algorithm, but for now
2182 just ensure that the value is greater than zero */
2187 case DNS_TYPE_NSEC
: {
2190 * RFC6762, section 18.14 explicitly states mDNS should use name compression.
2191 * This contradicts RFC3845, section 2.1.1
2194 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
2196 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
2200 if (rdlength
< p
->rindex
- offset
)
2203 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2205 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
2206 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
2207 * without the NSEC bit set. */
2211 case DNS_TYPE_NSEC3
: {
2214 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
2218 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
2222 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
2226 /* this may be zero */
2227 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2231 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
2235 r
= dns_packet_read_uint8(p
, &size
, NULL
);
2242 r
= dns_packet_read_memdup(p
, size
,
2243 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
2248 if (rdlength
< p
->rindex
- offset
)
2251 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
2253 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
2259 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
2263 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
2267 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2274 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2275 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2278 if (rr
->tlsa
.data_size
<= 0)
2279 /* the accepted size depends on the algorithm, but for now
2280 just ensure that the value is greater than zero */
2286 case DNS_TYPE_HTTPS
:
2287 r
= dns_packet_read_uint16(p
, &rr
->svcb
.priority
, NULL
);
2291 r
= dns_packet_read_name(p
, &rr
->svcb
.target_name
, false /* uncompressed */, NULL
);
2295 DnsSvcParam
*last
= NULL
;
2296 while (p
->rindex
- offset
< rdlength
) {
2297 _cleanup_free_ DnsSvcParam
*i
= NULL
;
2298 uint16_t svc_param_key
;
2301 r
= dns_packet_read_uint16(p
, &svc_param_key
, NULL
);
2304 /* RFC 9460, section 2.2 says we must consider an RR malformed if SvcParamKeys are
2305 * not in strictly increasing order */
2306 if (last
&& last
->key
>= svc_param_key
)
2309 r
= dns_packet_read_uint16(p
, &sz
, NULL
);
2313 i
= malloc0(offsetof(DnsSvcParam
, value
) + sz
);
2317 i
->key
= svc_param_key
;
2319 r
= dns_packet_read_blob(p
, &i
->value
, sz
, NULL
);
2322 if (!dns_svc_param_is_valid(i
))
2325 LIST_INSERT_AFTER(params
, rr
->svcb
.params
, last
, i
);
2332 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2336 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2340 if (rdlength
< p
->rindex
- offset
)
2343 r
= dns_packet_read_memdup(p
,
2344 rdlength
+ offset
- p
->rindex
,
2345 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2349 case DNS_TYPE_NAPTR
:
2350 r
= dns_packet_read_uint16(p
, &rr
->naptr
.order
, NULL
);
2354 r
= dns_packet_read_uint16(p
, &rr
->naptr
.preference
, NULL
);
2358 r
= dns_packet_read_string(p
, &rr
->naptr
.flags
, NULL
);
2362 r
= dns_packet_read_string(p
, &rr
->naptr
.services
, NULL
);
2366 r
= dns_packet_read_string(p
, &rr
->naptr
.regexp
, NULL
);
2370 r
= dns_packet_read_name(p
, &rr
->naptr
.replacement
, /* allow_compression= */ false, NULL
);
2373 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2374 case DNS_TYPE_OPENPGPKEY
:
2377 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2381 if (p
->rindex
- offset
!= rdlength
)
2385 *ret
= TAKE_PTR(rr
);
2386 if (ret_cache_flush
)
2387 *ret_cache_flush
= cache_flush
;
2389 *ret_start
= rewinder
.saved_rindex
;
2391 CANCEL_REWINDER(rewinder
);
2395 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2397 bool found_dau_dhu_n3u
= false;
2400 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2404 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2406 /* Check that the version is 0 */
2407 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0) {
2409 return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */
2413 l
= rr
->opt
.data_size
;
2415 uint16_t option_code
, option_length
;
2417 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2421 option_code
= unaligned_read_be16(p
);
2422 option_length
= unaligned_read_be16(p
+ 2);
2424 if (l
< option_length
+ 4U)
2427 /* RFC 6975 DAU, DHU or N3U fields found. */
2428 if (IN_SET(option_code
, DNS_EDNS_OPT_DAU
, DNS_EDNS_OPT_DHU
, DNS_EDNS_OPT_N3U
))
2429 found_dau_dhu_n3u
= true;
2431 p
+= option_length
+ 4U;
2432 l
-= option_length
+ 4U;
2435 *rfc6975
= found_dau_dhu_n3u
;
2439 static int dns_packet_extract_question(DnsPacket
*p
, DnsQuestion
**ret_question
) {
2440 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2444 n
= DNS_PACKET_QDCOUNT(p
);
2446 question
= dns_question_new(n
);
2450 _cleanup_set_free_ Set
*keys
= NULL
; /* references to keys are kept by Question */
2452 keys
= set_new(&dns_resource_key_hash_ops
);
2456 r
= set_reserve(keys
, n
* 2); /* Higher multipliers give slightly higher efficiency through
2457 * hash collisions, but the gains quickly drop off after 2. */
2461 for (unsigned i
= 0; i
< n
; i
++) {
2462 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2465 r
= dns_packet_read_key(p
, &key
, &qu
, NULL
);
2469 if (!dns_type_is_valid_query(key
->type
))
2472 r
= set_put(keys
, key
);
2476 /* Already in the Question, let's skip */
2479 r
= dns_question_add_raw(question
, key
, qu
? DNS_QUESTION_WANTS_UNICAST_REPLY
: 0);
2485 *ret_question
= TAKE_PTR(question
);
2490 static int dns_packet_extract_answer(DnsPacket
*p
, DnsAnswer
**ret_answer
) {
2491 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2493 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2494 bool bad_opt
= false;
2497 n
= DNS_PACKET_RRCOUNT(p
);
2501 answer
= dns_answer_new(n
);
2505 for (unsigned i
= 0; i
< n
; i
++) {
2506 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2507 bool cache_flush
= false;
2510 if (p
->rindex
== p
->size
&& p
->opt
) {
2511 /* If we reached the end of the packet already, but there are still more RRs
2512 * declared, then that's a corrupt packet. Let's accept the packet anyway, since it's
2513 * apparently a common bug in routers. Let's however suppress OPT support in this
2514 * case, so that we force the rest of the logic into lowest DNS baseline support. Or
2515 * to say this differently: if the DNS server doesn't even get the RR counts right,
2516 * it's highly unlikely it gets EDNS right. */
2517 log_debug("More resource records declared in packet than included, suppressing OPT.");
2522 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, &start
);
2526 /* Try to reduce memory usage a bit */
2528 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2530 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2533 if (p
->opt
|| bad_opt
) {
2534 /* Multiple OPT RRs? if so, let's ignore all, because there's
2535 * something wrong with the server, and if one is valid we wouldn't
2536 * know which one. */
2537 log_debug("Multiple OPT RRs detected, ignoring all.");
2542 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2543 /* If the OPT RR is not owned by the root domain, then it is bad,
2544 * let's ignore it. */
2545 log_debug("OPT RR is not owned by root domain, ignoring.");
2550 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2551 /* OPT RR is in the wrong section? Some Belkin routers do this. This
2552 * is a hint the EDNS implementation is borked, like the Belkin one
2553 * is, hence ignore it. */
2554 log_debug("OPT RR in wrong section, ignoring.");
2559 if (!opt_is_good(rr
, &has_rfc6975
)) {
2560 log_debug("Malformed OPT RR, ignoring.");
2565 if (DNS_PACKET_QR(p
)) {
2566 /* Additional checks for responses */
2568 if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr
))
2569 /* If this is a reply and we don't know the EDNS version
2570 * then something is weird... */
2571 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2572 "EDNS version newer that our request, bad server.");
2575 /* If the OPT RR contains RFC6975 algorithm data, then this
2576 * is indication that the server just copied the OPT it got
2577 * from us (which contained that data) back into the reply.
2578 * If so, then it doesn't properly support EDNS, as RFC6975
2579 * makes it very clear that the algorithm data should only
2580 * be contained in questions, never in replies. Crappy
2581 * Belkin routers copy the OPT data for example, hence let's
2582 * detect this so that we downgrade early. */
2583 log_debug("OPT RR contains RFC6975 data, ignoring.");
2589 p
->opt
= dns_resource_record_ref(rr
);
2590 p
->opt_start
= start
;
2591 assert(p
->rindex
>= start
);
2592 p
->opt_size
= p
->rindex
- start
;
2594 DnsAnswerFlags flags
= 0;
2596 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
2597 flags
|= DNS_ANSWER_REFUSE_TTL_NO_MATCH
;
2599 flags
|= DNS_ANSWER_SHARED_OWNER
;
2602 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2603 * cached. Hence mark only those RRs as cacheable by default, but not the ones from
2604 * the Additional or Authority sections.
2605 * This restriction does not apply to mDNS records (RFC 6762). */
2606 if (i
< DNS_PACKET_ANCOUNT(p
))
2607 flags
|= DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SECTION_ANSWER
;
2608 else if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
))
2609 flags
|= DNS_ANSWER_SECTION_AUTHORITY
;
2611 flags
|= DNS_ANSWER_SECTION_ADDITIONAL
;
2612 if (p
->protocol
== DNS_PROTOCOL_MDNS
)
2613 flags
|= DNS_ANSWER_CACHEABLE
;
2616 r
= dns_answer_add(answer
, rr
, p
->ifindex
, flags
, NULL
);
2621 /* Remember this RR, so that we can potentially merge its ->key object with the
2622 * next RR. Note that we only do this if we actually decided to keep the RR around.
2624 DNS_RR_REPLACE(previous
, dns_resource_record_ref(rr
));
2628 p
->opt
= dns_resource_record_unref(p
->opt
);
2629 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
2632 *ret_answer
= TAKE_PTR(answer
);
2637 int dns_packet_extract(DnsPacket
*p
) {
2643 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2644 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2645 _unused_
_cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
2648 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2650 r
= dns_packet_extract_question(p
, &question
);
2654 r
= dns_packet_extract_answer(p
, &answer
);
2658 if (p
->rindex
< p
->size
) {
2659 log_debug("Trailing garbage in packet, suppressing OPT.");
2660 p
->opt
= dns_resource_record_unref(p
->opt
);
2661 p
->opt_start
= p
->opt_size
= SIZE_MAX
;
2664 p
->question
= TAKE_PTR(question
);
2665 p
->answer
= TAKE_PTR(answer
);
2666 p
->extracted
= true;
2668 /* no CANCEL, always rewind */
2672 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2678 /* Checks if the specified packet is a reply for the specified
2679 * key and the specified key is the only one in the question
2682 if (DNS_PACKET_QR(p
) != 1)
2685 /* Let's unpack the packet, if that hasn't happened yet. */
2686 r
= dns_packet_extract(p
);
2693 if (p
->question
->n_keys
!= 1)
2696 return dns_resource_key_equal(dns_question_first_key(p
->question
), key
);
2699 int dns_packet_patch_max_udp_size(DnsPacket
*p
, uint16_t max_udp_size
) {
2701 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
2703 if (p
->opt_start
== SIZE_MAX
) /* No OPT section, nothing to patch */
2706 assert(p
->opt_size
!= SIZE_MAX
);
2707 assert(p
->opt_size
>= 5);
2709 unaligned_write_be16(DNS_PACKET_DATA(p
) + p
->opt_start
+ 3, max_udp_size
);
2713 static int patch_rr(DnsPacket
*p
, usec_t age
) {
2714 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
2717 uint16_t type
, rdlength
;
2720 /* Patches the RR at the current rindex, subtracts the specified time from the TTL */
2722 r
= dns_packet_read_name(p
, NULL
, true, NULL
);
2726 r
= dns_packet_read_uint16(p
, &type
, NULL
);
2730 r
= dns_packet_read_uint16(p
, NULL
, NULL
);
2734 r
= dns_packet_read_uint32(p
, &ttl
, &ttl_index
);
2738 if (type
!= DNS_TYPE_OPT
) { /* The TTL of the OPT field is not actually a TTL, skip it */
2739 ttl
= LESS_BY(ttl
* USEC_PER_SEC
, age
) / USEC_PER_SEC
;
2740 unaligned_write_be32(DNS_PACKET_DATA(p
) + ttl_index
, ttl
);
2743 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
2747 r
= dns_packet_read(p
, rdlength
, NULL
, NULL
);
2751 CANCEL_REWINDER(rewinder
);
2755 int dns_packet_patch_ttls(DnsPacket
*p
, usec_t timestamp
) {
2757 assert(timestamp_is_set(timestamp
));
2759 /* Adjusts all TTLs in the packet by subtracting the time difference between now and the specified timestamp */
2761 _unused_
_cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= REWINDER_INIT(p
);
2766 k
= now(CLOCK_BOOTTIME
);
2767 assert(k
>= timestamp
);
2770 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2772 n
= DNS_PACKET_QDCOUNT(p
);
2773 for (unsigned i
= 0; i
< n
; i
++) {
2774 r
= dns_packet_read_key(p
, NULL
, NULL
, NULL
);
2779 n
= DNS_PACKET_RRCOUNT(p
);
2780 for (unsigned i
= 0; i
< n
; i
++) {
2782 /* DNS servers suck, hence the RR count is in many servers off. If we reached the end
2783 * prematurely, accept that, exit early */
2784 if (p
->rindex
== p
->size
)
2795 static void dns_packet_hash_func(const DnsPacket
*s
, struct siphash
*state
) {
2798 siphash24_compress_typesafe(s
->size
, state
);
2799 siphash24_compress(DNS_PACKET_DATA((DnsPacket
*) s
), s
->size
, state
);
2802 static int dns_packet_compare_func(const DnsPacket
*x
, const DnsPacket
*y
) {
2805 r
= CMP(x
->size
, y
->size
);
2809 return memcmp(DNS_PACKET_DATA((DnsPacket
*) x
), DNS_PACKET_DATA((DnsPacket
*) y
), x
->size
);
2812 DEFINE_HASH_OPS(dns_packet_hash_ops
, DnsPacket
, dns_packet_hash_func
, dns_packet_compare_func
);
2814 bool dns_packet_equal(const DnsPacket
*a
, const DnsPacket
*b
) {
2815 return dns_packet_compare_func(a
, b
) == 0;
2818 int dns_packet_ede_rcode(DnsPacket
*p
, int *ret_ede_rcode
, char **ret_ede_msg
) {
2828 d
= p
->opt
->opt
.data
;
2829 l
= p
->opt
->opt
.data_size
;
2832 uint16_t code
, length
;
2835 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2836 "EDNS0 variable part has invalid size.");
2838 code
= unaligned_read_be16(d
);
2839 length
= unaligned_read_be16(d
+ 2);
2841 if (l
< 4U + length
)
2842 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2843 "Truncated option in EDNS0 variable part.");
2845 if (code
== DNS_EDNS_OPT_EXT_ERROR
) {
2846 _cleanup_free_
char *msg
= NULL
;
2849 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2850 "EDNS0 truncated EDE info code.");
2852 r
= make_cstring((char *) d
+ 6, length
- 2U, MAKE_CSTRING_ALLOW_TRAILING_NUL
, &msg
);
2854 return log_debug_errno(r
, "Invalid EDE text in opt.");
2857 if (!utf8_is_valid(msg
)) {
2858 _cleanup_free_
char *msg_escaped
= NULL
;
2860 msg_escaped
= cescape(msg
);
2862 return log_oom_debug();
2864 *ret_ede_msg
= TAKE_PTR(msg_escaped
);
2866 *ret_ede_msg
= TAKE_PTR(msg
);
2870 *ret_ede_rcode
= unaligned_read_be16(d
+ 4);
2882 bool dns_ede_rcode_is_dnssec(int ede_rcode
) {
2883 return IN_SET(ede_rcode
,
2884 DNS_EDE_RCODE_UNSUPPORTED_DNSKEY_ALG
,
2885 DNS_EDE_RCODE_UNSUPPORTED_DS_DIGEST
,
2886 DNS_EDE_RCODE_DNSSEC_INDETERMINATE
,
2887 DNS_EDE_RCODE_DNSSEC_BOGUS
,
2888 DNS_EDE_RCODE_SIG_EXPIRED
,
2889 DNS_EDE_RCODE_SIG_NOT_YET_VALID
,
2890 DNS_EDE_RCODE_DNSKEY_MISSING
,
2891 DNS_EDE_RCODE_RRSIG_MISSING
,
2892 DNS_EDE_RCODE_NO_ZONE_KEY_BIT
,
2893 DNS_EDE_RCODE_NSEC_MISSING
2897 int dns_packet_has_nsid_request(DnsPacket
*p
) {
2898 bool has_nsid
= false;
2907 d
= p
->opt
->opt
.data
;
2908 l
= p
->opt
->opt
.data_size
;
2911 uint16_t code
, length
;
2914 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2915 "EDNS0 variable part has invalid size.");
2917 code
= unaligned_read_be16(d
);
2918 length
= unaligned_read_be16(d
+ 2);
2920 if (l
< 4U + length
)
2921 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2922 "Truncated option in EDNS0 variable part.");
2924 if (code
== DNS_EDNS_OPT_NSID
) {
2926 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2927 "Duplicate NSID option in EDNS0 variable part.");
2930 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2931 "Non-empty NSID option in DNS request.");
2943 size_t dns_packet_size_unfragmented(DnsPacket
*p
) {
2946 if (p
->fragsize
== 0) /* Wasn't fragmented */
2949 /* The fragment size (p->fragsize) covers the whole (fragmented) IP packet, while the regular packet
2950 * size (p->size) only covers the DNS part. Thus, subtract the UDP header from the largest fragment
2951 * size, in order to determine which size of DNS packet would have gone through without
2954 return LESS_BY(p
->fragsize
, udp_header_size(p
->family
));
2957 static const char* const dns_svc_param_key_table
[_DNS_SVC_PARAM_KEY_MAX_DEFINED
] = {
2958 [DNS_SVC_PARAM_KEY_MANDATORY
] = "mandatory",
2959 [DNS_SVC_PARAM_KEY_ALPN
] = "alpn",
2960 [DNS_SVC_PARAM_KEY_NO_DEFAULT_ALPN
] = "no-default-alpn",
2961 [DNS_SVC_PARAM_KEY_PORT
] = "port",
2962 [DNS_SVC_PARAM_KEY_IPV4HINT
] = "ipv4hint",
2963 [DNS_SVC_PARAM_KEY_ECH
] = "ech",
2964 [DNS_SVC_PARAM_KEY_IPV6HINT
] = "ipv6hint",
2965 [DNS_SVC_PARAM_KEY_DOHPATH
] = "dohpath",
2966 [DNS_SVC_PARAM_KEY_OHTTP
] = "ohttp",
2968 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dns_svc_param_key
, int);
2970 const char* format_dns_svc_param_key(uint16_t i
, char buf
[static DECIMAL_STR_MAX(uint16_t)+3]) {
2971 const char *p
= dns_svc_param_key_to_string(i
);
2975 return snprintf_ok(buf
, DECIMAL_STR_MAX(uint16_t)+3, "key%i", i
);
2978 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2979 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2980 [DNS_RCODE_FORMERR
] = "FORMERR",
2981 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2982 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2983 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2984 [DNS_RCODE_REFUSED
] = "REFUSED",
2985 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2986 [DNS_RCODE_YXRRSET
] = "YRRSET",
2987 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2988 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2989 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2990 [DNS_RCODE_DSOTYPENI
] = "DSOTYPENI",
2991 [DNS_RCODE_BADVERS
] = "BADVERS",
2992 [DNS_RCODE_BADKEY
] = "BADKEY",
2993 [DNS_RCODE_BADTIME
] = "BADTIME",
2994 [DNS_RCODE_BADMODE
] = "BADMODE",
2995 [DNS_RCODE_BADNAME
] = "BADNAME",
2996 [DNS_RCODE_BADALG
] = "BADALG",
2997 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2998 [DNS_RCODE_BADCOOKIE
] = "BADCOOKIE",
3000 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
3002 const char* format_dns_rcode(int i
, char buf
[static DECIMAL_STR_MAX(int)]) {
3003 const char *p
= dns_rcode_to_string(i
);
3007 return snprintf_ok(buf
, DECIMAL_STR_MAX(int), "%i", i
);
3010 static const char* const dns_ede_rcode_table
[_DNS_EDE_RCODE_MAX_DEFINED
] = {
3011 [DNS_EDE_RCODE_OTHER
] = "Other",
3012 [DNS_EDE_RCODE_UNSUPPORTED_DNSKEY_ALG
] = "Unsupported DNSKEY Algorithm",
3013 [DNS_EDE_RCODE_UNSUPPORTED_DS_DIGEST
] = "Unsupported DS Digest Type",
3014 [DNS_EDE_RCODE_STALE_ANSWER
] = "Stale Answer",
3015 [DNS_EDE_RCODE_FORGED_ANSWER
] = "Forged Answer",
3016 [DNS_EDE_RCODE_DNSSEC_INDETERMINATE
] = "DNSSEC Indeterminate",
3017 [DNS_EDE_RCODE_DNSSEC_BOGUS
] = "DNSSEC Bogus",
3018 [DNS_EDE_RCODE_SIG_EXPIRED
] = "Signature Expired",
3019 [DNS_EDE_RCODE_SIG_NOT_YET_VALID
] = "Signature Not Yet Valid",
3020 [DNS_EDE_RCODE_DNSKEY_MISSING
] = "DNSKEY Missing",
3021 [DNS_EDE_RCODE_RRSIG_MISSING
] = "RRSIG Missing",
3022 [DNS_EDE_RCODE_NO_ZONE_KEY_BIT
] = "No Zone Key Bit Set",
3023 [DNS_EDE_RCODE_NSEC_MISSING
] = "NSEC Missing",
3024 [DNS_EDE_RCODE_CACHED_ERROR
] = "Cached Error",
3025 [DNS_EDE_RCODE_NOT_READY
] = "Not Ready",
3026 [DNS_EDE_RCODE_BLOCKED
] = "Blocked",
3027 [DNS_EDE_RCODE_CENSORED
] = "Censored",
3028 [DNS_EDE_RCODE_FILTERED
] = "Filtered",
3029 [DNS_EDE_RCODE_PROHIBITED
] = "Prohibited",
3030 [DNS_EDE_RCODE_STALE_NXDOMAIN_ANSWER
] = "Stale NXDOMAIN Answer",
3031 [DNS_EDE_RCODE_NOT_AUTHORITATIVE
] = "Not Authoritative",
3032 [DNS_EDE_RCODE_NOT_SUPPORTED
] = "Not Supported",
3033 [DNS_EDE_RCODE_UNREACH_AUTHORITY
] = "No Reachable Authority",
3034 [DNS_EDE_RCODE_NET_ERROR
] = "Network Error",
3035 [DNS_EDE_RCODE_INVALID_DATA
] = "Invalid Data",
3036 [DNS_EDE_RCODE_SIG_NEVER
] = "Signature Never Valid",
3037 [DNS_EDE_RCODE_TOO_EARLY
] = "Too Early",
3038 [DNS_EDE_RCODE_UNSUPPORTED_NSEC3_ITER
] = "Unsupported NSEC3 Iterations",
3039 [DNS_EDE_RCODE_TRANSPORT_POLICY
] = "Impossible Transport Policy",
3040 [DNS_EDE_RCODE_SYNTHESIZED
] = "Synthesized",
3042 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dns_ede_rcode
, int);
3044 const char* format_dns_ede_rcode(int i
, char buf
[static DECIMAL_STR_MAX(int)]) {
3045 const char *p
= dns_ede_rcode_to_string(i
);
3049 return snprintf_ok(buf
, DECIMAL_STR_MAX(int), "%i", i
);
3052 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
3053 [DNS_PROTOCOL_DNS
] = "dns",
3054 [DNS_PROTOCOL_MDNS
] = "mdns",
3055 [DNS_PROTOCOL_LLMNR
] = "llmnr",
3057 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);