DNS for sets
A set can answer DNS for the domains it targets: send them to another resolver, answer them with fixed addresses, or remove parts of the answers. Names that no set targets go to the resolver the device chose, unchanged.
The options are on the set's DNS & Routing tab, under DNS Redirect, see DNS & Routing.
What b4 sees
b4 sees DNS that goes to port 53, from the devices on the network and from the router itself. Encrypted DNS that a device sends by itself (DoH, DoT or DoQ) passes b4 unseen, and the sets do not apply to it. The usual sources are secure DNS in a browser such as Chrome or Firefox, Android Private DNS, and a DoH resolver set up on the device. The sets apply to such a device once encrypted DNS is turned off on it.
An encrypted forwarder on the router is not a problem, since devices still ask the router on port 53. The interception itself is described in How b4 handles DNS.
How a set handles a query
A query name matches a set the way a server name does: a domain covers its subdomains, and a domain listed in one set wins over a regexp: entry in another. A block set answers first, then the set's pins, then the set's resolvers while Enable DNS Redirect is on. Otherwise the query goes to the device's resolver unchanged. b4 can then remove ECH keys and IPv6 addresses from the answer, see the full sequence.
Sending a set's domains to another resolver
A set's resolver helps when the provider's DNS answers the set's domains with wrong addresses.
- Open Sets and open the set.
- On the DNS & Routing tab, under DNS Redirect, turn on Enable DNS Redirect.
- In Resolvers, enter the resolver's IP address or its
https://URL, or pick a server under Recommended DNS Servers. - Click Save Changes.
| Entry | When it fits |
|---|---|
| An IP address | b4 asks the resolver over plain DNS. It fits when plain DNS to other servers arrives intact. Fragment DNS Queries splits each query to such a resolver into IP fragments, for DPI that reads the names in DNS queries. |
An https:// URL | b4 asks the resolver over DNS over HTTPS (DoH), and the queries leave encrypted. It fits when the provider tampers with plain DNS to other servers too, or the resolver serves only DoH. A URL entered without a path gets /dns-query added. |
Queries that come from the address of one of the set's resolvers go on unchanged, so a resolver on the local network can serve a set. A resolver on the router itself has one more condition, see When it does not work.
A service that checks the address a connection comes from needs another exit rather than another resolver, see Routing sites through a VPN.
A set with more than one resolver
A set can ask more than one resolver, for the case when one of them does not answer. Resolvers holds every resolver of the set, up to eight. Each entry is an IP address or an https:// URL.
b4 asks the resolvers in the order of the field. It moves on to the next one when a resolver has not answered within 300 ms, and at once when a resolver answers with an error. The first answer without an error ends the lookup, also when it says that the name does not exist or carries no address. A later resolver never corrects the answer of an earlier one. The most trusted resolver belongs first.
Dragging an entry changes the order. A click on a server under Recommended DNS Servers adds it at the end, and a click on a server that is already in the field removes it. The details of the order are in How b4 handles DNS.
Lookups b4 makes itself
When b4 looks up a set's names itself, for routing, a DSCP value or a proxy upstream, it asks the set's resolvers in the same order while Enable DNS Redirect is on, and the router's resolver otherwise, see How b4 handles DNS.
When no resolver answers
When no resolver of the set answers, b4 answers from the last good reply it holds for the name, or else asks the resolver the device was addressing. Query timeout under Settings > Core > DNS sets how long b4 waits for each resolver.
Fail closed when no resolver is reachable turns the fallback off. The device then gets an error (SERVFAIL), and the set's names resolve through its resolvers or not at all.
With fail closed on, every name of the set stops resolving while none of its resolvers is reachable, even while the bypass strategy works. On a censored connection, a DoH server itself can get blocked.
Pinned addresses
A pin answers a name with fixed addresses, with no hosts file to edit on every device. It helps when DNS keeps handing out an address that does not answer.
Domain and addresses under Pinned addresses takes hosts file lines, one per address: the address first, then the names it answers for.
157.240.0.174 www.instagram.com
157.240.205.63 scontent.cdninstagram.com scontent-a.cdninstagram.com
- A pin covers each name and its subdomains. The longest matching entry wins.
- A pin applies only to a name the set targets. For other names the editor offers Add to this set's domains.
- Pins work with Enable DNS Redirect off too, and win over the set's resolvers.
- The pins are the whole address answer: a name pinned to IPv4 addresses only gets no IPv6 addresses.
- The resolver's answers for a pinned name lose the name's alias (CNAME) and keep the records of the type asked under that name, so that a resolver on the network does not keep the alias and follow it past the pins.
- In a set without routing, b4 leaves out pinned addresses that do not answer on TCP port 443, checked at start, on save and at the Retest interval (seconds) of IP Block Detection. When none answers, the pins are used anyway.
The IPv4 fallback
While Enable IPv6 Support is off, b4 processes IPv4 only, and a site with IPv6 addresses would be reached over IPv6, past every set. b4 therefore removes the IPv6 addresses (AAAA records) from the answers for names a set matches, and the device connects over IPv4. Other names keep their full answers.
- The switch is Force IPv4 for matched domains on the IPv4 / IPv6 card under Settings > Core > Packet Engine. It is on by default and unused while Enable IPv6 Support is on.
- A set limited to IPv6 by its IP Version Filter keeps its IPv6 answers.
- An address the device already has from its cache, a hosts file or its own encrypted DNS still leads over IPv6. Enable IPv6 Support closes that path.
Removing ECH keys
Sites behind some CDNs publish an Encrypted ClientHello (ECH) key in DNS. A browser that gets the key, such as Chrome, sends a ClientHello that names the CDN's shared name instead of the site: cloudflare-ech.com for every Cloudflare site. Where DPI drops such ClientHellos, all these sites stop loading at once.
Remove ECH keys from DNS answers, under Encrypted ClientHello (ECH), takes the key out, and the browser sends an ordinary ClientHello naming the site. It works with Enable DNS Redirect off too. The key is removed when:
- the name asked matches the set, such as
steamdb.infolisted in the set; - the key's public name is a domain the set matches. A set listing
cloudflare-ech.comremoves the key from every Cloudflare site's answers.
While Enable IPv6 Support is off, b4 in NFQUEUE mode also removes the key from answers that arrive over IPv6. A set that uses Source Devices leaves such answers alone, since b4 cannot tell which device they go to. A device added by hand by its IPv6 address is the exception.
Discovery proposes such a set, with no packet changes, for a site that loads without ECH but not with it. A key already in a browser's cache is used until it expires.
Sending every DNS query to DoH
A set that targets every name sends the DNS of the whole network and of the router to one DoH resolver. Domains listed in other sets keep their own sets' resolvers.
- Open Sets and click Create Set.
- On the Import/Export tab, paste the configuration below into Set Configuration JSON and click Import.
- To use another resolver, replace the entry in Resolvers on the DNS & Routing tab.
- Click Create Set.
{
"b4_version": "dev",
"name": "all DOH",
"tcp": { "dport_filter": "53" },
"udp": { "dport_filter": "53" },
"fragmentation": { "strategy": "none" },
"faking": { "sni": false },
"targets": { "sni_domains": ["regexp:.*"] },
"enabled": true,
"dns": {
"enabled": true,
"doh_url": "https://wikimedia-dns.org/dns-query"
}
}
The set changes no packets, and its port filters of 53 keep it off TLS and QUIC connections. Router host names and .lan names stop resolving, since the public resolver does not know them; pins keep the ones that matter working. When the resolver is unreachable, every name falls back, so a resolver reachable from the connection matters more than its features.
Global DNS settings
Settings > Core > DNS holds the interception of DNS over TCP and the timeouts for all sets. Without Intercept DNS over TCP, a device that retries a query over TCP reaches its own resolver, past the set.
Checking the result
- Open Traffic.
- Enter
flags:dnsin the filter to keep only DNS queries. - Open a site of the set on a device.
Each query shows the set that matched it and a label:
| Label | Meaning |
|---|---|
doh <server>, forward <address> | Sent to the set's resolvers. The label names the first of them that b4 is not leaving out after repeated failures. When another resolver answered, a second line names it |
pin, pin empty | Answered from the pins, empty when they hold no address of the type asked |
no alias | The pinned name's alias removed from the resolver's answer |
ipv4 only, no ech | IPv6 addresses or the ECH key removed from the answer |
heal, heal + ipv4 only | Unreachable addresses removed or replaced by Heal DNS answers, or banned addresses removed or replaced |
sinkhole, block | A block set answered "does not exist" or dropped the query |
fallback-cache, fallback-upstream | No resolver of the set answered: the last good reply was used, or the device's resolver answered |
servfail | No resolver of the set answered and nothing else could, or the set fails closed |
passthrough, bad target, from target, ipv6 off | Passed on unchanged: the set has no resolver, its first resolver is not a valid address or URL and it has no other, the query came from one of the set's own resolvers, or it came over IPv6 while IPv6 support is off |
escalate <set> | Lookups for the name kept failing, and the backup set of Escalation answered |
The log records the same decisions, as dns-doh->, dns-pin and so on. Both show DNS decisions while Log Level is Info, the default, or more detailed.
When it does not work
| What happens | Cause and fix |
|---|---|
| A device's lookups never show on the Traffic page | The device uses encrypted DNS. Turning off secure DNS in its browser, or Android Private DNS, sends its lookups to the router. |
| The site still goes to the old address | The device keeps an answer it already has until the answer expires. |
| A pinned site opens on the router, but devices behind a resolver on the network still get the old address | The resolver keeps the name's earlier answer, often an alias valid for up to an hour, until it expires. Clearing that resolver's cache brings in the pins at once. |
servfail, fallback-cache or fallback-upstream | None of the set's resolvers is reachable from this connection. Another resolver, or DoH in place of plain DNS, is the next try. |
| The set's names resolve slowly or not at all, with a resolver on the router among the set's resolvers | A resolver on the router, at any place in Resolvers, and the router's resolver when a resolver on the network uses it upstream, has to reach its own upstream over DoH or DoT, which b4 does not intercept. The router's own resolver does not need a place in Resolvers: a set without Fail closed when no resolver is reachable falls back to the resolver the device asked. |
| A browser still uses ECH, with a resolver on the network that checks DNSSEC, such as BIND or Unbound | The resolver gets the original answer on a path b4 does not see: DoT or DoH to its upstream, an interface left out of Capture Interfaces, or TCP while Intercept DNS over TCP is off. With Intercept DNS over TCP on, plain DNS through a captured interface closes these paths, see ECH keys. |
On KeeneticOS and older kernels
On KeeneticOS and some other routers with an older kernel, b4 works on copies of DNS requests and replaces the resolver's reply with the set's answer. The row DNS queries copied, not held (NFLOG) in System Info shows when. The resolver the device asked then still receives every request, and a resolver outside the router has to reply before the device gets the set's answer. Requests that a nat rule moves to another port, or that NAT sends from another source port, get no set handling, see Copied requests.