[DNS Fundamentals Part 4] DNS Messages and Transport: Reading UDP/TCP, EDNS, and Response Codes

DNS Fundamentals Part 4 DNS Messages and Transport: Reading UDP/TCP, EDNS, and Response Codes DNS

“For DNS, I only need to open UDP port 53, right?”

This is actually a common misunderstanding. It is also the starting point of hard-to-diagnose failures such as “everything works except a few domains” or “name resolution fails once in a while after we enabled DNSSEC.”

Through Part 3 we followed the “flow” of how a caching DNS server looks for an answer. In Part 4 we change perspective and look at what is inside a DNS message and how it is carried over UDP and TCP. This is also the foundation you need to read dig output.

▶ This part is also available as a YouTube video (about 19 min) (it is also embedded at the end of this article).


The structure of a DNS message

DNS queries and responses are both exchanged in the same DNS message format.

Diagram showing the five parts of a DNS message (header, question, answer, authority, additional) and how they correspond to HEADER, QUESTION SECTION, ANSWER SECTION, AUTHORITY SECTION, and ADDITIONAL SECTION in dig output
PartWhat it holds
Header (12 bytes)The ID that matches queries to responses, flags, the response code (RCODE), and the number of entries in each section
QuestionThe name, type, and class that were asked. Copied unchanged into the response
AnswerThe records that answer the question
AuthorityNS records of the delegated servers, or the SOA record when the answer is “does not exist”
AdditionalGlue A/AAAA records for the NS host names, and the EDNS OPT pseudo-record

“QUESTION SECTION,” “ANSWER SECTION,” and the rest of dig’s output are these five parts, displayed as they are (Part 8). In a query the Answer and Authority parts are empty, and often only OPT appears in Additional. Queries and responses have the same shape; what differs is the flags and the contents of each part.

Key points
• A message has five parts: header, question, answer, authority, additional
• Authority holds the delegated NS records, or the SOA when there is “no such data”
• Additional holds glue and the EDNS OPT pseudo-record


Header flags

The flags in the header are some of the most frequently used information when you isolate a failure.

FlagNameMeaningWhen to look at it
QRQuery/Response0 = query, 1 = responseAlways set on responses
AAAuthoritative AnswerThe answer comes from the authoritative DNS server itselfWhether the answer came from a cache or from an authoritative server
TCTrunCationThe response did not fit and was truncatedA signal to retry over TCP
RDRecursion DesiredRecursive query requestedNormally 1 for stub resolvers
RARecursion AvailableRecursive queries are accepted0 on authoritative-only servers
ADAuthentic DataDNSSEC validation succeededResponses from validating resolvers (Part 9)
CDChecking DisabledPlease do not perform DNSSEC validationIsolating validation failures (dig +cd, Resolve-DnsName -DnssecCd)

Here is how this looks in real dig output.

$ dig www.example.com                          # ask the caching DNS
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

$ dig @ns1.example.com www.example.com +norec   # ask the authoritative DNS directly
;; flags: qr aa; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

An answer from a caching DNS server does not carry aa, while asking the authoritative DNS directly does. This is the first thing to check when you are separating “the authoritative answer differs from the cached answer.” The numbers after the flags line are the entry counts of each section.

Note that PowerShell’s Resolve-DnsName does not display flags, so use dig on Linux or similar when you need to see them.


UDP and TCP

DNS uses port 53 and communicates over both UDP and TCP.

Diagram showing a resolver querying over UDP and, on receiving a response with TC=1, repeating the same query over TCP, and that both firewalls, between client and caching DNS and between caching DNS and authoritative DNS, must allow UDP 53 and TCP 53

Normal queries use UDP. One round trip is enough, so it is light and fast.

When a response does not fit in UDP, the server returns a response with the TC bit set in the header, and the client repeats the same query over TCP. This is called TCP fallback. Zone transfers (Part 7) also use TCP.

TCP is not an occasional spare

This is the key to the misunderstanding at the start.

  • RFC 7766 (2016): requires all general-purpose implementations to support both UDP and TCP
  • RFC 9210 (2022): asks operators not to block TCP

On firewalls, allow UDP/53 and TCP/53 both between the client and the caching DNS and between the caching DNS and the authoritative DNS. If TCP/53 is closed on either side, the result is a hard-to-see failure in which only large responses fail.

Key points
• Normally UDP; if the response does not fit, check the TC bit and retry over TCP
• TCP support is mandatory (RFC 7766) and must not be blocked (RFC 9210)
• Allow UDP/53 and TCP/53 on both segments


How TCP fallback came about, and the “512-byte wall”

Why is it designed this way? Let’s go back through a little history.

Timeline from the 512-byte limit in RFC 1034/1035 (1987), TCP fallback in RFC 1123 (1989), EDNS(0), RFC 7766 (2016), Flag Day 2020, to RFC 9210 (2022)
  • 1983 (RFC 883) and 1987 (RFC 1034/1035): DNS communicated over either UDP or TCP, and UDP messages were limited to 512 bytes at most. The basis for 512 is the 576-byte datagram that every host must be able to receive according to RFC 791 (512 bytes of data plus 64 bytes of header); the limit was set at a size that is guaranteed to arrive in a single packet.
  • 1989 (RFC 1123): queries must always be sent over UDP first, and if the response is truncated, retried over TCP. The aim was to reduce the processing load for DNS, which exchanges small pieces of data frequently. This is TCP fallback.
  • 2010 (RFC 5966): made TCP support mandatory, and relaxed “UDP first” to “UDP should be tried first (SHOULD).”
  • 2016 (RFC 7766, replacing 5966): stated explicitly that starting with TCP is allowed (MAY). RFC 9210 in 2022 added operational requirements.

The “512-byte wall”

Because UDP is tried first and the query is redone over TCP only after truncation, the number of round trips doubles for a single name resolution. As DNSSEC and IPv6 spread and responses grew, this inefficiency became a real problem. This is the “512-byte wall.” What appeared to solve it is EDNS(0), explained next.

You can try it yourself. If you go back to the 512-byte limit with +noedns, a large TXT or MX response triggers TCP fallback.

$ dig +noedns example.com TXT
;; Truncated, retrying in TCP mode.
(omitted)
;; SERVER: 192.0.2.53#53(192.0.2.53) (TCP)

In PowerShell, Resolve-DnsName -TcpOnly queries over TCP from the start. In a packet capture you can also see a TCP connection start right after a UDP response with the TC bit set.


EDNS(0) and the OPT pseudo-record

EDNS(0) (RFC 6891) is a mechanism that extends DNS through the OPT pseudo-record in the Additional section.

Diagram showing a client announcing that it can receive up to 1232 bytes over UDP, the server replying over UDP even above 512 bytes, and how the CLASS, TTL, and RDATA fields of the OPT pseudo-record are used

When the client announces “I can receive up to this size over UDP,” the server replies over UDP even above 512 bytes. What is interesting is that it borrows the fields of an existing record format to carry the extension information.

OPT fieldMeaning in an ordinary recordHow OPT uses it
NAMEThe nameRoot (empty)
TYPEThe type41 (OPT)
CLASSThe class (IN)The UDP size the sender can receive (e.g., 1232 bytes)
TTLHow long to cacheExtended RCODE, EDNS version (0), and the DO bit (a request for DNSSEC data)
RDATAThe valueOptions (COOKIE, EDE, NSID, Client Subnet, Padding, and others)

In dig it appears like this.

$ dig www.example.com
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232

With DNS Flag Day 2019 on February 1, 2019, major resolvers stopped working around servers that do not respond correctly to EDNS. In other words, old authoritative DNS servers that cannot handle EDNS properly have been failing name resolution since that point. Note that Resolve-DnsName does not show the contents of OPT.


Fragmentation and DNS Flag Day 2020

Once EDNS(0) made large UDP responses possible, IP fragmentation became the next problem.

Diagram showing a large 4096-byte UDP response split into three by IP, the second and later fragments, which have no UDP header, being dropped by a firewall or NAT, and the calculation behind the 1232-byte recommendation of Flag Day 2020

A UDP response larger than the path MTU (normally 1500 bytes on Ethernet, and smaller on VPNs and the like) is split at the IP level. The second and later fragments have no UDP header, so firewalls and NAT devices tend to drop them, and cache poisoning techniques that forge fragments are also known.

So DNS Flag Day 2020 on October 1, 2020 recommended setting the EDNS buffer size to 1232 bytes.

The 1232 calculation
1280 (IPv6 minimum MTU) − 40 (IPv6 header) − 8 (UDP header) = 1232

At this size the response arrives without fragmentation on almost every path. Responses that do not fit are carried over TCP, so allowing TCP/53 is a prerequisite. BIND and Unbound (1.12.0 and later) default to 1232.

Key points
• Large UDP responses are fragmented by IP, and the fragments tend to be dropped
• The recommended buffer size is 1232 bytes (1280 − 48, Flag Day 2020)
• Responses that do not fit are carried over TCP. Allowing TCP/53 is a prerequisite


Problems caused by firewalls and devices on the path

Turning the discussion so far into symptoms you see in the field gives the following.

SymptomCommon causeRemedy
Only large responses (DNSSEC, long TXT, many MX) failTCP/53 is closedAllow both UDP/53 and TCP/53
Only UDP responses over 512 bytes do not arriveAn old device’s DNS inspection limit is still 512 bytes (e.g., Cisco ASA’s message-length maximum 512)Change the setting to follow the size announced via EDNS (e.g., ASA’s message-length maximum client auto)
Only certain domains time out occasionallyFragments of split UDP packets are being droppedSet the buffer to 1232 and allow TCP
FORMERR is returned, or EDNS does not get throughA DNS proxy or ALG in a router or similar does not understand or strips OPTDisable the DNS proxy or ALG, or update the device
Return responses are droppedReturn traffic is allowed only for source port 53The source port is random (RFC 5452). Allow it statefully

When a firewall drops a UDP response, the client receives nothing and gets a timeout. A response code such as REFUSED comes back only when the DNS server itself responded.

You can check path support by comparing dig +bufsize=1232 with +bufsize=4096 (a feature not available in the standard Windows commands, so do this on Linux or similar), or with ISC’s EDNS Compliance Tester (ednscomp.isc.org).


DNS Cookies

UDP has no connection setup, so it has a weakness: queries with a forged source IP address, or forged responses, are easy to inject. DNS Cookies (RFC 7873, 2016) lightly compensate for this weakness using an EDNS option.

Diagram showing a client sending a query with a client cookie (C), the server returning a response with a server cookie (S), the pair of C and S then being used to verify that each side is genuine, and an attacker with a spoofed source not being treated as genuine
  1. The client attaches a client cookie (8 bytes) to the query
  2. The server attaches a server cookie to the response
  3. From then on, both are attached to each query

This lets the server confirm “this is a peer that really talked to this address before,” and the client confirm “this response is a genuine response to my own query.” It reduces reflection attacks with a forged source and forged responses from off the path.

RFC 9018 (2021) standardized how server cookies are generated so they can be used even in anycast deployments (Part 7) that mix multiple products. In dig you can check the ; COOKIE: line.


Response codes (RCODE)

The RCODE is the first thing to look at in incident response.

ValueNameMeaningCommon causes
0NOERRORProcessed normallyEven zero answers is NOERROR (see NODATA below)
1FORMERRThe query format could not be interpretedOld servers or DNS proxies that cannot understand the EDNS OPT
2SERVFAILProcessing failed on the server sideCannot reach the authoritative DNS, DNSSEC validation failure
3NXDOMAINThe name itself does not existMisspelled name, not registered, asking the wrong server
4NOTIMPThe requested function is not implementedA kind of query that is not supported
5REFUSEDRefused by policyDenied by an ACL, or asked an authoritative DNS about a name it is not responsible for
(none)NODATANot an RCODE but a name for a stateThe name exists but that type does not (NOERROR with zero answers)

Note in particular that NODATA is not an RCODE. It refers to the state where the RCODE is NOERROR, there are zero answers, and the SOA is in the Authority section. When you see “NOERROR but no answer,” it means the name exists but there is no record of that type.

Values 6 to 10 (YXDOMAIN and others) are used by dynamic updates (Part 7), and 16 and above are codes extended through EDNS. How each kind of response looks in dig is covered in Part 8.


Extended DNS Errors (EDE)

SERVFAIL says only that “something failed on the server side,” so it does not tell you the cause. EDE (Extended DNS Errors, RFC 8914, 2020) was introduced for this. It returns the reason for SERVFAIL and similar errors in an EDNS option.

INFO-CODENameMeaning
3Stale AnswerAnswered from an expired cache entry (serve-stale)
6DNSSEC BogusDNSSEC validation failed
7Signature ExpiredThe signature has expired
9DNSKEY MissingThere is no DNSKEY matching the DS
15 / 17Blocked / FilteredBlocked by the resolver’s policy
18ProhibitedThe querier is not allowed
22No Reachable AuthorityCannot reach the authoritative DNS servers
23Network ErrorAn error occurred communicating with the authoritative DNS

If you query a public resolver for the test domain dnssec-failed.org, EDE comes back like this (excerpt).

;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 34492
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags: do; udp: 1232
; EDE: 9 (DNSKEY Missing): (no SEP matching the DS found for dnssec-failed.org.)

With this, you can tell from the response alone whether the cause is “a DNSSEC failure” or “the authoritative server cannot be reached.” Make it a habit to check EDE first when you see SERVFAIL.


Think about it

  1. Is DNS TCP/53 allowed on the firewalls in your environment? Check each of client ⇔ caching DNS and caching DNS ⇔ authoritative DNS.
  2. You receive a report that “name resolution is slow once in a while, but only for some domains.” Based on this article, what causes can you think of?
  3. When your internal caching DNS returns SERVFAIL, what information (flags, EDE, results from another resolver) should you look at to get more clues about the cause?

Hint: Changing options such as dig +bufsize=512, +tcp, +norec, and +cd and comparing the results shows you at which stage things fail. In PowerShell, -TcpOnly, -NoRecursion, and -DnssecCd play the same roles.


Summary

  • A DNS message has five parts: header, question, answer, authority, and additional. dig’s display corresponds to them directly
  • aa means an authoritative answer, TC is the signal to retry over TCP, and ad means DNSSEC validation succeeded
  • DNS requires TCP too. Allow UDP/53 and TCP/53 on both segments
  • The EDNS buffer is 1232 bytes. Anything beyond that is carried over TCP
  • NODATA is not an RCODE. When you see SERVFAIL, check the reason with EDE

Starting next time, we look at the kinds and syntax of resource records, the contents of the message, over two parts.


Watch the video

This part is also explained in a YouTube video (about 19 min).

All parts of the series are in the DNS Fundamentals playlist on YouTube.

The DNS Fundamentals series (12 parts)

All 12 parts with a reading guide: DNS Fundamentals

  1. What Is DNS? How Name Resolution Works and Its Three Key Players
  2. The domain name hierarchy and delegation
  3. How name resolution works
  4. DNS messages and transport (this article)
  5. Resource records, part 1
  6. Resource records, part 2
  7. Running authoritative DNS servers
  8. Investigating with commands
  9. DNS security
  10. DNS in the cloud and on corporate networks
  11. DNS troubleshooting
  12. DNS design exercises

This series is the English edition of 【DNS入門】, originally written in Japanese.

コメント

Copied title and URL