User Tools

Site Tools


DNS-based firewall with IP sets


  • This how-to describes the method for setting up DNS-based firewall with IP sets on OpenWrt.
  • It relies on Dnsmasq, ipset-dns and firewall with IP sets to resolve and filter domains.
  • Follow DNS hijacking to intercept DNS queries from your LAN clients.


  • Filter LAN client traffic based on DNS with IP sets.


Install the packages and configure IP sets for domains to filter. Set up firewall rules to filter LAN client traffic which destination matches the IP sets. Configure the domains which addresses should be stored in the IP sets.

# Install packages
opkg update
opkg install dnsmasq ipset ipset-dns
# Configure IP sets
uci -q delete firewall.filter
uci set firewall.filter="ipset"
uci set"filter"
uci set"ipv4"
uci set"hash"
uci set firewall.filter.match="ip"
uci -q delete firewall.filter6
uci set firewall.filter6="ipset"
uci set"filter6"
uci set"ipv6"
uci set"hash"
uci set firewall.filter6.match="ip"
# Filter LAN client traffic with IP sets
uci -q delete firewall.filter_fwd
uci set firewall.filter_fwd="rule"
uci set"Filter-IPset-DNS-Forward"
uci set firewall.filter_fwd.src="lan"
uci set firewall.filter_fwd.dest="wan"
uci set firewall.filter_fwd.ipset="filter dest"
uci set"ipv4"
uci set firewall.filter_fwd.proto="all"
uci set"REJECT"
uci -q delete firewall.filter6_fwd
uci set firewall.filter6_fwd="rule"
uci set"Filter-IPset-DNS-Forward"
uci set firewall.filter6_fwd.src="lan"
uci set firewall.filter6_fwd.dest="wan"
uci set firewall.filter6_fwd.ipset="filter6 dest"
uci set"ipv6"
uci set firewall.filter6_fwd.proto="all"
uci set"REJECT"
uci commit firewall
/etc/init.d/firewall restart
# Configure ipset-dns
uci set ipset-dns.@ipset-dns[0].ipset="filter"
uci set ipset-dns.@ipset-dns[0].ipset6="filter6"
uci commit ipset-dns
/etc/init.d/ipset-dns restart
# Resolve race conditions for ipset-dns
cat << "EOF" > /etc/firewall.ipsetdns
/etc/init.d/ipset-dns restart
cat << "EOF" >> /etc/sysupgrade.conf
uci -q delete firewall.ipsetdns
uci set firewall.ipsetdns="include"
uci set firewall.ipsetdns.path="/etc/firewall.ipsetdns"
uci set firewall.ipsetdns.reload="1"
uci commit firewall
/etc/init.d/firewall restart
# Configure domains to filter
uci add_list dhcp.@dnsmasq[0].server="/"
uci add_list dhcp.@dnsmasq[0].server="/"
uci commit dhcp
/etc/init.d/dnsmasq restart


Flush DNS cache on the clients and restart the client browser. Verify your client traffic is properly filtered on the router.


Collect and analyze the following information.

# Restart services
/etc/init.d/log restart; /etc/init.d/firewall restart
/etc/init.d/ipset-dns restart; /etc/init.d/dnsmasq restart
# Log and status
logread -e dnsmasq; netstat -l -n -p | grep -e dnsmasq
logread -e ipset-dns; netstat -l -n -p | grep -e ipset-dns
# Runtime configuration
pgrep -f -a dnsmasq; pgrep -f -a ipset-dns
iptables-save; ip6tables-save; ipset list
# Persistent configuration
uci show firewall; uci show dhcp; uci show ipset-dns


Preresolve domains

Preresolve domains on startup.

# Preresolve domains
cat << "EOF" > /etc/firewall.dnsmasq
/etc/init.d/dnsmasq restart
uci get dhcp.@dnsmasq[0].server \
| sed -e "s/\s/\n/g" \
| sed -n -e "s/^\///;s/\/.*$//p" \
| while read -r IPSET_DOMAIN
do nslookup "${IPSET_DOMAIN}" localhost
cat << "EOF" >> /etc/sysupgrade.conf
uci -q delete firewall.dnsmasq
uci set firewall.dnsmasq="include"
uci set firewall.dnsmasq.path="/etc/firewall.dnsmasq"
uci set firewall.dnsmasq.reload="1"
uci commit firewall
/etc/init.d/firewall restart

Source restriction

Limit the restriction scope to a specific source MAC address.

# Apply source restriction
for FW_RULE in filter_fwd filter6_fwd
do uci add_list firewall.${FW_RULE}.src_mac="00:11:22:33:44:55"
uci commit firewall
/etc/init.d/firewall restart

Time restriction

Reorder firewall rules and enable time restriction to keep the rules active.

# Apply time restriction
for FW_RULE in filter_fwd filter6_fwd
uci set firewall.${FW_RULE}.start_time="21:00:00"
uci set firewall.${FW_RULE}.stop_time="09:00:00"
uci set firewall.${FW_RULE}.weekdays="Mon Tue Wed Thu Fri"
uci commit firewall
/etc/init.d/firewall restart

Established connections

Reorder firewall rules to properly apply time restrictions.

# Reorder firewall rules
cat << "EOF" > /etc/firewall.estab
for IPT in iptables ip6tables
do ${IPT}-save -c -t filter \
/FORWARD.*reject/i $(${IPT}-save -c -t filter \
| sed -n -e "/FORWARD.*ESTABLISHED/p")" \
| ${IPT}-restore -c -T filter
cat << "EOF" >> /etc/sysupgrade.conf
uci -q delete firewall.estab
uci set firewall.estab="include"
uci set firewall.estab.path="/etc/firewall.estab"
uci set firewall.estab.reload="1"
uci commit firewall
/etc/init.d/firewall restart
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
docs/guide-user/firewall/fw3_configurations/dns_ipset.txt · Last modified: 2020/10/09 10:32 by tmomas