What's new

Blocking IP Range

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

First thing: INPUT and OUTPUT tables are for traffic coming to and from the router. In this case, you want to control traffic that passes through the router. So, the correct table to manipulate is the FORWARD table.

Therefore:

Code:
iptables -I FORWARD -d ad-g.doubleclick.net -j REJECT

is what you want if your goal is to prevent connecting to these servers.

This isn't a very efficient or reliable way to implement ad blocking however. The more rules you add, the highest impact it will have on your network, since every packet must be checked against every rule in the table. This is where ipset will provide a far more efficient method of implementing blacklisting.

Writing a script that would download a blocklist and generate a proper ipset list would be the ideal. Unfortunately, many blocklists seem to ship in a p2p format, and they require you to pay to get these lists in a more compatible format (cidr format, for example)...
 
Last edited:
Thanks. That does work.

How would the blocklist need to be written ? If I had a list of domains could I create one myself ?

IP ranges have to be in a CIDR format to be easily pluggable into an ipset list. For example, to block 192.168.1.1 through 192.168.1.254, it would have to be entered as 192.168.1.0/24.
 
If you want to take it one step forward, the following will force all DNS queries to go through your router (which will in turn go through OpenDNS). That way, a misconfigured client will still have Internet access, just that it will be forced to to through your configured DNS.

Code:
iptables -I PREROUTING -t nat -p udp -s `nvram get lan_ipaddr`/`nvram get lan_netmask` ! -d `nvram get lan_ipaddr`/`nvram get lan_netmask` --dport 53 -j DNAT --to-destination `nvram get lan_ipaddr`

Repeat the same for TCP if you wish. I haven't tested it myself, this is just based on Tomato code that I looked at a few weeks ago.

Cleaner than just dropping connection to other DNS servers.

Hello RMerlin,
Is the below code supposed to modify the '--to-destination' with Opendns IP or just matter of using as is -
#!/bin/sh
iptables -I PREROUTING -t nat -p udp -s `nvram get lan_ipaddr`/`nvram get lan_netmask` ! -d `nvram get lan_ipaddr`/`nvram get lan_netmask` --dport 53 -j DNAT --to-destination `nvram get lan_ipaddr`
iptables -I PREROUTING -t nat -p tcp -s `nvram get lan_ipaddr`/`nvram get lan_netmask` ! -d `nvram get lan_ipaddr`/`nvram get lan_netmask` --dport 53 -j DNAT --to-destination `nvram get lan_ipaddr`
 
Hello RMerlin,
Is the below code supposed to modify the '--to-destination' with Opendns IP or just matter of using as is -
#!/bin/sh
iptables -I PREROUTING -t nat -p udp -s `nvram get lan_ipaddr`/`nvram get lan_netmask` ! -d `nvram get lan_ipaddr`/`nvram get lan_netmask` --dport 53 -j DNAT --to-destination `nvram get lan_ipaddr`
iptables -I PREROUTING -t nat -p tcp -s `nvram get lan_ipaddr`/`nvram get lan_netmask` ! -d `nvram get lan_ipaddr`/`nvram get lan_netmask` --dport 53 -j DNAT --to-destination `nvram get lan_ipaddr`

That code is obsolete now. Use the built-in DNSFilter instead, you will be able to configure everything through the web interface - much simpler.
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top