mattoddie.dev

Making every device use your UniFi DNS

My Sony TV tried to open a connection to my UniFi gateway on port 853. A television, poking at my router, on a port most people have never heard of. That one line in a firewall log turned into an afternoon of finding out which gadgets in the house were quietly ignoring my DNS, and then politely making them stop.

I did all of it without opening the UniFi interface once, by asking questions in Claude through unifi-mcp, my open-source MCP server for UniFi Network. It reads the flow logs, the firewall policies and the network settings, and it can make changes once you allow it, with a dry run of each one first. If you haven't met it, the two-minute trailer is the quickest introduction (and it has a better soundtrack than this post):

VIDEOThe unifi-mcp trailer (1:55). The network in it is a demo with made-up data.

The setup below is a UCG Max on UniFi Network 11 with the zone-based firewall. The addresses in this post are examples, so please don't try to resolve anything against my house.

A TV knocking on port 853

Port 853 is DNS over TLS (DoT). I asked Claude why the TV would want it, and unifi-mcp pulled the flow from the gateway's traffic log: seven packets in about a second from the TV to the gateway's address on the IoT network, classified as DNS_OVER_TLS and dropped by the policy that stops IoT devices reaching the gateway for anything but DNS, DHCP and a few other basics.

The explanation turned out to be wonderfully boring. Sony TVs run Google TV, and Android's Private DNS setting defaults to Automatic. Each time the device joins a network it tries DoT on the DNS server DHCP gave it, and falls back to ordinary DNS on port 53 when that fails. The probe lined up exactly with the TV booting, and it carried on streaming happily afterwards. Not a hacker, then. Just a TV being keen.

It did raise a better question, though. If the TV goes shopping for its own DNS, what else in the house is doing the same behind my back?

Who is skipping my DNS?

Two follow-up questions covered the good news first.

  • Does the gateway encrypt its own lookups? Yes. Its encrypted DNS setting was on, using Cloudflare's security resolver (1.1.1.2, which also blocks malware and phishing domains) over DNS over HTTPS (DoH).
  • Does every network send its devices to the gateway? Also yes. None of the eight networks overrides the DHCP DNS server, so every device is told to use its gateway.

That's the polite version, where devices take the DNS server they're given. It's an honour system, and gadgets are not known for their honour. There were no outbound DNS rules and no NAT rules at all, so anything with 8.8.8.8 baked in, or its own encrypted DNS, went straight past the gateway. That matters for more than my sense of tidiness: the content filters on my Kids and Guest networks and Cloudflare's malware blocking all work through the gateway's DNS. A device that skips it skips them too.

There are three ways to dodge your DNS, and each needs its own fix:

HowPortFix
Plain DNS to a hard-coded server53Redirect it to the gateway with a NAT rule
DNS over TLS853Block it outbound; devices fall back to plain DNS
DNS over HTTPS443Block known DoH hostnames and resolver IPs where it matters

Redirect plain DNS, don't block it

The tempting fix is to block port 53 to the internet and be done with it. The trouble is that some devices, Google TV and Chromecast especially, have Google's DNS built in, and when 8.8.8.8 stops answering they sulk and announce there's no internet. A destination NAT (DNAT) rule is kinder: the device still thinks it's talking to Google, and the gateway answers in a convincing Google voice.

That's one rule per network, because each one points at that network's own gateway address. I asked for a dry run of the first, checked what would be sent, created it, read back how the controller stored it, and only then did the other six. Measure twice, NAT once. This is the IoT one:

Redirect DNS to gateway (IoT), as the controller stores itJSON
{
  "description": "Redirect DNS to gateway (IoT)",
  "type": "DNAT",
  "ip_version": "IPV4",
  "protocol": "tcp_udp",
  "in_interface": "<the IoT network>",
  "destination": {
    "address": "10.0.0.0/8",
    "invert_address": true,
    "port": "53"
  },
  "translated_ip": "10.30.0.1",
  "translated_port": "53"
}

The inverted destination is the important bit. The rule only catches DNS headed outside my private range, so a DNS server I run myself in the lab still works. Use whatever range covers your own networks.

Testing it needed an answer only the gateway could give. A lookup sent to 8.8.8.8 from the lab came back with my Apple TV's local hostname, which Google has definitely never heard of, and with Cloudflare's malware block for a test domain. Only one thing on earth could have answered that, so the gateway was doing the talking.

The NAT rules are IPv4 only. My Home network is the only one with IPv6, so it also got a firewall policy blocking port 53 to the internet over IPv6. Devices give up on IPv6 DNS, fall back to IPv4 and walk straight into the redirect.

Close the encrypted side doors

Redirecting port 53 does nothing for encrypted DNS, which is rather the point of encrypting it. DoT is the easy one, because it politely uses its own port: three firewall policies block TCP and UDP 853 to the internet, one each for the Unsecure zone (IoT, Kids, Guest and the alarm system), the lab, and Home with Default. Devices fall back to plain DNS, just as the TV did, and the redirect does the rest.

DoH is the sneaky one. It's ordinary HTTPS on port 443, and blocking that by port would block the entire internet. For most of the house I didn't try. On the Kids network, where the content filter is the point, two more policies cover the common routes:

  • A domain block for a dozen well-known DoH hostnames: dns.google, cloudflare-dns.com, Mozilla's and Chrome's Cloudflare endpoints, OpenDNS, Quad9, NextDNS and AdGuard. That catches browsers with "secure DNS" turned on.
  • An IP block on port 443 for the main public resolver addresses, for apps that skip the hostname and connect by IP.

I first tried these as traffic rules, but Network 11 refuses to create those (HTTP 501, "Not implemented"). Firewall policies with a web domain or IP destination do the same job. That error was unhelpful enough that I went and fixed it in unifi-mcp, which now explains what happened and points at the firewall policy tool instead. Every bug report I write to myself is a feature.

None of these rules touch the gateway's own DoH, because they only match traffic coming from the internal networks.

Three ways a device tries to resolve names. Plain DNS to 8.8.8.8 on port 53 is redirected by a NAT rule to the gateway. DNS over TLS on port 853 is blocked by a firewall policy, so the device falls back to plain DNS. The gateway sends every lookup on to Cloudflare over DNS over HTTPS. Your network Device TV, phone, plug Gateway DNS + filters 8.8.8.8 never reached Cloudflare 1.1.1.2 over DoH :53 → NAT DoT on :853, blocked DoH
FIG 1Plain DNS gets redirected and DoT gets blocked, so every lookup ends up at the gateway, which sends it on encrypted.

Then the Apple devices showed up

A few minutes after the DoT blocks went in, I asked unifi-mcp to go through the blocked traffic for anything odd. Most of it was expected, but there was a new pattern: my Mac and both HomePods trying to reach 1.1.1.2 on port 853, over and over, like someone rattling a locked door. Since then the Apple TV, iPhones, iPads and even a Watch have joined in, over IPv4 and IPv6. The whole orchard.

That's Apple's support for Discovery of Designated Resolvers (DDR, RFC 9462). An Apple device asks its DNS server a special question, _dns.resolver.arpa, which amounts to "do you have an encrypted version?" My gateway doesn't answer that itself. It passes the question on to Cloudflare, Cloudflare says "yes, use me on 1.1.1.2 over TLS or HTTPS", and the device upgrades. So my Apple devices had been using encrypted DNS straight to Cloudflare all along, going around the gateway, and my own router had been handing them the directions.

For most of them it made little difference, since they reached the same malware-filtering resolver the gateway uses. On the Kids network it was a real gap: an iPad doing this would skip the content filter entirely. The DoT block closes that, and the Kids DoH policies cover the HTTPS version.

Should I just let them?

I thought about letting the grown-ups' devices off the hook, and decided not to.

  • Local names would break. Encrypted DNS straight to Cloudflare skips the gateway, so names like my lab.home.arpa records stop resolving on those devices.
  • Gateway filtering and visibility go. Any DNS-based filtering on the gateway is bypassed, and per-device DNS no longer shows up in UniFi.
  • The privacy gain is small. DNS from a device to the gateway never leaves the house, and the gateway already encrypts the trip to Cloudflare. Encrypted DNS on the device matters on hotel and café WiFi, and Apple devices still use it there.

The blocked devices fall back to the gateway without any visible problem. The only cost is noise: about 1,950 log entries in 48 hours, Apple devices trying the same locked door, which buries anything that actually matters. Turning off logging on those four policies keeps them blocking without flooding the log.

UniFi already does DoH

This is the part that made me stop worrying and learn to love the block. The gateway runs its own encrypted DNS, so you get the benefits of DoH without each device doing its own thing. It's the Encrypted DNS setting in UniFi Network, and it has three modes:

  • Off, where the gateway uses plain DNS to whatever servers your WAN is set to.
  • Auto, where UniFi picks the providers for you.
  • Predefined or custom servers, where you choose from a long list of public resolvers, or paste in a DNS stamp for one that isn't listed.

The list is the interesting bit. The server names (cloudflare-security, cloudflare-security-ipv6 and so on) are the same names as in dnscrypt-proxy's public resolver list, and there's far more choice than plain Cloudflare and Google, including the filtering variants that block malware or adult content. A custom DNS stamp covers anything else, including a NextDNS or Control D profile of your own if you want per-profile filtering and logs.

I stayed on Cloudflare Security in both its IPv4 and IPv6 forms. It's fast from the UK and blocks known malware for every device. Quad9 is a good alternative if you want stricter privacy and don't mind it being a little slower here.

To check it's working, open one.one.one.one/help from any device on the network. "Using DNS over HTTPS (DoH)" should say yes, even though the device itself is sending plain DNS to the gateway.

What this doesn't stopA VPN app, or a DoH service that isn't on the block list, still gets around all of this. A determined teenager will find that out eventually, and I look forward to the conversation. For TVs, smart plugs and browsers with a "secure DNS" switch, these rules cover it.

The checklist

If you want the same setup, these are the pieces, in the order I'd add them:

  1. Turn on the gateway's Encrypted DNS and pick a resolver.
  2. Check that no network hands out a DNS server other than its gateway.
  3. Add a DNAT rule per network that redirects port 53 to anything outside your private range to that network's gateway.
  4. Block port 853 to the internet from every internal zone.
  5. On networks with IPv6, block port 53 to the internet over IPv6 too.
  6. Where filtering matters, block well-known DoH hostnames and resolver IPs on port 443.
  7. Turn off logging on the DoT blocks once you're happy, or Apple devices will fill the log.

Each one is a few minutes in the UniFi interface. Doing it by asking was quicker, and a lot more fun, mostly because unifi-mcp could show me the evidence first, from the TV's probe to the Apple devices' retries, and preview every change before it went in. It's free and open source at unifi-mcp.mattoddie.dev, and there's a write-up of how I built it in the AI section. If you're setting up networks to protect in the first place, start with the isolated lab VLAN post.

Written by · Markdown version