Usually, dnsmasq will occasionally query all https-dns-proxy DoH listeners at once, then it chooses the fastest-responding server(s) and sends the majority of traffic to it (them).
I’ve been told repeatedly that we cannot do true round-robin DNS resolution with dnsmasq, like other DNS resolvers can do to load-level between the various https-dns-proxy DoH listeners.
But now we can.
Create a file at:
/etc/nftables.d/11-doh-round-robin.nft
… and chmod it as 644 to match the other files in /etc/nftables.d.
Then paste in the code below. You’ll have to set it up to mirror your https-dns-proxy setup.
For instance, I’ve got 6 IPv4 DoH listeners, and 6 IPv6 listeners (which are identical servers as the IPv4 servers, just used via IPv6), so I have to set up tcp and udp for IPv4, and tcp and udp for IPv6, then tell it to rotate between them via the numgen inc mod 6 map blurb.
If you’ve got, say, 4 DoH listeners (2 IPv4 and 2 IPv6), you’d use mod 2.
chain doh_output { type nat hook output priority -100; policy accept; # 6 IPv4 DoH Listeners (ports 5050, 5052, 5054, 5056, 5058, 5060) ip daddr 127.0.0.1 udp dport 53 dnat ip to 127.0.0.1 : numgen inc mod 6 map { \ 0 : 5050, 1 : 5052, 2 : 5054, 3 : 5056, 4 : 5058, 5 : 5060 \ } ip daddr 127.0.0.1 tcp dport 53 dnat ip to 127.0.0.1 : numgen inc mod 6 map { \ 0 : 5050, 1 : 5052, 2 : 5054, 3 : 5056, 4 : 5058, 5 : 5060 \ } # 6 IPv6 DoH Listeners (ports 5051, 5053, 5055, 5057, 5059, 5061) ip6 daddr ::1 udp dport 53 dnat ip6 to [::1] : numgen inc mod 6 map { \ 0 : 5051, 1 : 5053, 2 : 5055, 3 : 5057, 4 : 5059, 5 : 5061 \ } ip6 daddr ::1 tcp dport 53 dnat ip6 to [::1] : numgen inc mod 6 map { \ 0 : 5051, 1 : 5053, 2 : 5055, 3 : 5057, 4 : 5059, 5 : 5061 \ } }
IMPORTANT: Ensure there’s a blank line at the end of the code… the firewall doesn’t like it at all if that blank line isn’t there.
Then issue:
fw4 restart
Ensure the firewall properly picked up the settings:
nft list chain inet fw4 doh_output
table inet fw4 { chain doh_output { type nat hook output priority dstnat; policy accept; ip daddr 127.0.0.1 udp dport 53 dnat ip to 127.0.0.1:numgen inc mod 6 map { 0 : 5050, 1 : 5052, 2 : 5054, 3 : 5056, 4 : 5058, 5 : 5060 } ip daddr 127.0.0.1 tcp dport 53 dnat ip to 127.0.0.1:numgen inc mod 6 map { 0 : 5050, 1 : 5052, 2 : 5054, 3 : 5056, 4 : 5058, 5 : 5060 } ip6 daddr ::1 udp dport 53 dnat ip6 to [::1]:numgen inc mod 6 map { 0 : 5051, 1 : 5053, 2 : 5055, 3 : 5057, 4 : 5059, 5 : 5061 } ip6 daddr ::1 tcp dport 53 dnat ip6 to [::1]:numgen inc mod 6 map { 0 : 5051, 1 : 5053, 2 : 5055, 3 : 5057, 4 : 5059, 5 : 5061 } } }
Then test it:
tcpdump -ni lo portrange 5050-5061 -c 12
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes 18:22:27.018255 IP 127.0.0.1.24632 > 127.0.0.1.5050: UDP, length 63 18:22:27.018605 IP6 ::1.53122 > ::1.5051: UDP, length 63 18:22:27.018878 IP 127.0.0.1.24632 > 127.0.0.1.5052: UDP, length 63 18:22:27.019184 IP6 ::1.53122 > ::1.5053: UDP, length 63 18:22:27.021856 IP 127.0.0.1.24632 > 127.0.0.1.5054: UDP, length 63 18:22:27.022123 IP6 ::1.53122 > ::1.5055: UDP, length 63 18:22:27.022394 IP 127.0.0.1.24632 > 127.0.0.1.5056: UDP, length 63 18:22:27.022683 IP6 ::1.53122 > ::1.5057: UDP, length 63 18:22:27.022948 IP 127.0.0.1.24632 > 127.0.0.1.5058: UDP, length 63 18:22:27.023219 IP6 ::1.53122 > ::1.5059: UDP, length 63 18:22:27.029674 IP 127.0.0.1.24632 > 127.0.0.1.5060: SIP 18:22:27.030015 IP6 ::1.53122 > ::1.5061: UDP, length 63 12 packets captured 30 packets received by filter 0 packets dropped by kernel
The ‘SIP’ on port 5060 is just the firewall mislabeling the packet… port 5060 is usually used for SIP phones. It doesn’t change the packet.