Skip to main content

DNS & Routing

The DNS & Routing tab of the set editor decides which resolver answers the set's domains and where the set's traffic goes. Guides that use it:

DNS Redirect​

The sub-tab sends the DNS queries for the set's domains to the set's own resolvers. Other domains keep the resolver the device uses.

The DNS Redirect sub-tab with two DoH resolvers and one plain DNS resolver

FieldWhat it doesDefault
Enable DNS RedirectSends the queries for the set's domains to the resolvers below.Off
Fail closed when no resolver is reachableOn, the set's names stop resolving while none of its resolvers is reachable. Off, b4 answers from the last good reply or from the device's own resolver.Off
ResolversThe set's resolvers, up to eight: IP addresses for plain DNS and https:// URLs for DoH. b4 asks them in the order shown, see A set with more than one resolver.Empty
Fragment DNS QueriesSplits each query to a resolver given by its IP address into IP fragments, for DPI that reads names in DNS queries. Shown when Resolvers holds an IP address.Off
Recommended DNS ServersPublic resolvers to pick from, all of them or only Plain DNS or DNS-over-HTTPS. A click adds a server to Resolvers, and a click on an added server removes it.All
Remove ECH keys from DNS answersBrowsers then send an ordinary ClientHello. See Removing ECH keys.Off
Domain and addressesPinned addresses in hosts file order: 157.240.0.174 www.instagram.com. A pin applies only to names the set targets, also with the redirect off.Empty

Traffic Routing​

The sub-tab sends the set's traffic through a network interface or a SOCKS5 proxy, relays it to Telegram, or blocks it.

The Traffic Routing sub-tab with routing on

FieldWhat it doesDefault
Enable RoutingTurns routing on for the set.Off
Routing modeNetwork interface, Upstream SOCKS5 proxy, Telegram over WebSocket (built-in) or Block (blackhole), described below.Network interface
Source InterfacesLimits the set to traffic that arrives on the selected interfaces, such as the LAN bridge. See Which interface is which.None, every interface
Resolved IP TTL (seconds)How long an address learned for the set's domains stays in the set. IP targets stay as long as the set lists them.3600

Routing matches destination addresses: the set's IP, CIDR, GeoIP and ASN targets, the collected IPs while Use collected IPs is on, and the addresses b4 learns for its domains. A set higher in the list with IP priority on can keep its addresses out of what this set learns. A set with no domain, IP, GeoIP or ASN target and with Use collected IPs off routes nothing, and the tab warns about it. Block routing on a set that handles only calls is an exception: it blocks the calls the set takes. A set in Upstream SOCKS5 proxy mode is another: it can route UDP by port. Source Devices only limit whose traffic the set takes: routing everything a device sends also needs Match any IP address under Targets > Bypass IPs.

Network interface​

The set's traffic leaves through the selected interface, such as a WireGuard or OpenVPN tunnel or a second uplink.

FieldWhat it doesDefault
Output InterfaceThe interface the set's traffic leaves by. One that is down or missing is marked unavailable, and the set's routing stops until it is back.None
Egress IP (optional)Rewrites the source address of the set's traffic. b4 adds the address to the output interface itself. The device upstream then routes by that source.Empty
Gateway (optional)The device on the output interface's network that receives the set's traffic, such as a VPN gateway or another router. Empty uses the gateway b4 finds on the interface, or a plain route through it.Empty
Router's own trafficWhether the router's own connections to the set's addresses use the output interface too. See below.Automatic
Kill switchDrops the set's traffic while the output interface is gone. See below.Off

Traffic that arrives on the output interface itself does not follow the set. With an IPv4 address in Gateway (optional) on an Ethernet port, bridge, VLAN, Wi-Fi or TAP interface, IPv4 devices on that network follow the set too, apart from the gateway itself. b4 ignores a gateway that is outside the interface's network or is one of the router's own addresses, and logs the reason. See Gateway and Sites through a VPN on another device.

When the output interface is a tunnel that is up, such as WireGuard or a TUN or TAP device, b4 leaves the set's packets alone: the strategy tabs have no effect, and the set's TCP connections end with [routed->wg0] on the Logs page. A connection that the router sends out by another interface, such as one from a device in a KeeneticOS internet access policy, ends with reroute-> and that interface's name instead, or tunnel-> when the interface is a tunnel. A set routed to a plain interface, such as a second uplink, keeps its bypass strategy.

Router's own traffic​

The setting covers connections the router opens itself, such as package updates or a proxy running on it. Traffic from the devices on the network follows the set either way.

ValueWhat happens
AutomaticThe router's own traffic follows the set, except into a TUN or TAP device read by a program on the router, such as Xray or sing-box
Route it tooAlways follows the set
Leave it aloneAlways takes the normal route

The exception prevents a loop: a proxy behind a TUN device opens its own connection to each destination, which would go back into the same TUN until the router runs out of memory. See Using b4 with Xray or XrayUI. The field is greyed out on a set limited to a source interface or to included source devices, which never takes the router's own traffic.

Kill switch​

Without the kill switch, the set's traffic leaves through the normal uplink with the router's real address while the output interface is gone, for example while a VPN client reconnects. With it on, that traffic is dropped. Other traffic is not affected, and routing resumes by itself when the interface returns.

Upstream SOCKS5 proxy​

b4 takes over the set's TCP connections and opens each one again through a SOCKS5 proxy, such as Xray or sing-box on the router or on another host. The set's bypass strategy does not apply in this mode. The router's own TCP connections to the set's addresses go through the proxy as well. This includes the lookups of a DNS over TLS resolver on the router when its server is in the set. A set limited to a source interface or to included source devices leaves them alone.

FieldWhat it doesDefault
Upstream SOCKS5 hostThe proxy's host name or address, 127.0.0.1 for a proxy on the router itself.Empty
Upstream SOCKS5 portThe proxy's port, commonly 1080.0
Username (optional), Password (optional)For a proxy that requires authentication.Empty
Send domain name to upstreamSends the proxy the host name instead of the address when b4 can tie a name to the connection. Names pinned in the set always go as addresses. TCP only.On
Route UDP through upstreamSends the set's UDP through the proxy too. Off, or with Block QUIC (force TCP) on the UDP tab, b4 refuses the set's QUIC, and browsers fall back to TCP. A set without targets needs it to route UDP by port.Off
Fall back to direct on upstream failureConnects directly while the proxy cannot be reached.Off

UDP by port​

A set in this mode with no domain, IP, GeoSite, GeoIP or ASN target can send UDP through the proxy by destination port alone, whatever the address. It suits games, calls and other applications whose servers have no fixed addresses. The set routes by port when all of this holds:

  • Route UDP through upstream is on.
  • Port Filter on the UDP tab lists the ports.
  • Calls on that tab is not set to Handle only calls.
  • Source Interfaces names an interface, such as the LAN bridge, or the set has included source devices.

The tab then names the ports the set routes. Without a source interface or an included source device, b4 installs no rule, and a warning on the tab says what to select. A rule that matched by port alone would also take replies and connections that come in from the internet.

TrafficWhat happens
UDP that the selected interfaces or devices send to the listed portsGoes through the proxy
UDP to port 53Stays with b4's DNS handling, also when the port is listed
TCPNot routed
The router's own UDPNot taken
IPv6 UDP to the listed ports while Enable IPv6 Support is off under Settings > CoreLeaves directly

The other settings of the UDP tab do not apply to such a set, and the tab hides them. Routing UDP by port sets one up step by step.

Requirements​

This mode, the Telegram over WebSocket (built-in) mode and the Telegram over WebSocket switch need TPROXY support in the kernel. On OpenWrt with nftables it comes from kmod-nft-tproxy and kmod-nft-socket, with iptables from kmod-ipt-tproxy, kmod-ipt-socket and iptables-mod-tproxy. Keenetic firmware has the modules. kmod-nft-fib, or kmod-ipt-extra with iptables-mod-extra, lets the rule skip the router's own addresses by type; without them b4 lists the addresses. System Info on Settings > System > Service shows under Kernel Capabilities whether Transparent proxy (TPROXY) is available, and names the missing packages.

Verifying that it works​

A connection the set sends through the proxy ends with [proxy] on the Logs page, and so does UDP the set routes by port. Without that line the connection was not diverted, usually because its address is not in the set. See Checking the result.

When the upstream itself is down​

While the proxy does not answer, every connection to the set's addresses hangs and fails, on every TCP port, including other sites on a shared CDN address. UDP the set sends through the proxy is dropped, or leaves directly with Fall back to direct on upstream failure on. The dashboard shows Set X: proxy ... unreachable and upstream down. See When the upstream is down.

Connections through the built-in SOCKS5 proxy​

A client of b4's own SOCKS5 proxy that asks for a host name a proxy set matches goes straight to that set's upstream. See The built-in SOCKS5 proxy.

Bridge netfilter​

With bridge netfilter on, as OpenWrt's dockerd package sets it, connections from devices behind a network bridge such as br-lan hang in this mode and in the Telegram modes. See Bridge netfilter for the fix.

Telegram over WebSocket (built-in)​

The set's Telegram traffic is relayed over Telegram's WebSocket edge, with no proxy in the Telegram app. The switch on Settings > Telegram does the same for every device without a set. See Telegram over WebSocket.

Block (blackhole)​

The set's traffic never leaves the router. Block action decides how: Reject (fail fast), the default, ends the connection at once, and Drop (silent blackhole) lets it time out. See Blocking ads and sites.

DSCP​

The DSCP block at the bottom of Traffic Routing writes the set's own DSCP value into the packets sent to the set's addresses, with routing on or off. The router in front of b4 reads the value and routes the set's traffic, for example through a VPN. See Passing sets to another router.

Fields​

FieldWhat it doesDefault
Enable per-set DSCPWrites the set's value. Turned on with the value at 0, it fills in the first of 7, 31, 6 and 25 that no other set and no Set DSCP uses.Off
DSCP value0-63. 0 clears the field.0

The value replaces the value of Set DSCP under Settings > Core > Firewall in those packets, whether Set DSCP is on or off, and the interface list there limits it too. The value stays on this router: a shared set never carries it. Under the TUN engine, Discovery and the watchdog's heals cannot test a set with its own value.

Choosing the value​

7, 31, 6 and 25 keep normal priority in Linux Wi-Fi, cake and pfifo_fast; the hint under the field lists where other values land. pfSense cannot match these four, and there AF11-AF13 (10, 12, 14) disturb traffic least. Sets meant for different routes need different values.

Which packets carry the value​

The value goes by destination address alone, whatever the protocol and port: the set's IP, CIDR, GeoIP and ASN targets, the collected IPs while Use collected IPs is on, and the addresses b4 learned for its domains. A set higher in the list with IP priority on can keep its addresses out of what this set learns. A CDN address shared by several sites carries the value for all of them. See Which packets get the value for overlapping sets.

The first connection​

A packet to an address b4 has not written for the set yet leaves without the value. A domain's address is written when b4 sees its DNS answer. Where the devices' DNS does not pass through b4, as with b4 in a container on MikroTik, IP, CIDR, GeoIP and ASN targets give the value from the first packet.

When the value is not written​

The block names the reason, and the switch cannot be turned on while it holds. No value is written:

  • in the Upstream SOCKS5 proxy, Telegram over WebSocket (built-in) and Block (blackhole) modes;
  • for a set with source devices, included or excluded, or with routing limited to Source Interfaces;
  • for any set while Device Filtering is on with a device selected;
  • for a set with no IP, GeoIP or ASN target and Use collected IPs off, when it also has no domain or has Domain-only matching on;
  • while Skip IPTables/NFTables Setup is on.

Through a tunnel such as WireGuard, the router in front of b4 sees only the tunnel's packets, which usually do not carry the value.

Reading the value on the next router​

See Reading the value on the next router.

Linux with nftables​

See Linux with nftables.

RouterOS​

See Routing a set's traffic by its DSCP value.

Firewall rules​

With iptables, the sets' values need the ipset command and the xt_set kernel module. The rules and their cost per packet are in Firewall rules and cost.

How it works in detail​

See How routing sets work, DSCP rules and Packet marks.

When the rules cannot be installed​

The Routing Sets row under Firewall in System Info shows how many routing sets are installed, and the error while installing fails. See When the rules cannot be installed.