FireHOL support for ipset

ipset is command line utility that allows the firewall admins to manage large lists of IPs.

ipset is independent of iptables. Once a collection of IPs has been created with ipset, iptables and FireHOL can use it. Adding or removing IPs to/from the collection, does not need any change at the firewall. Collections are manipulated by the ipset command and the firewall will automatically use the new IPs.

An ipset collection is defined by its name. To create an new collection run on a shell:

ipset create NAME hash:ip

To see the active collections, run:

ipset list -n

-n is required to show just the names. Without it, ipset will also dump the entire collection.

To add IPs to our collection, run:

ipset add NAME

to delete IPs from our collection, run:

ipset del NAME

Check the manual page of ipset for more information.

FireHOL support for ipset has two aspects:

Matching ipset collections in FireHOL rules

FireHOL can use ipset collections for matching packets in all its statements. They are part of the src and dst keywords. For example, to allow smtp requests from all the clients in a collection, use:

server smtp accept src ipset:NAME

To all the servers IPs of a collection:

server smtp accept dst ipset:NAME

Matching both clients' and servers' IPs is also possible:

server smtp accept src ipset:NAME dst ipset:NAME

You can actually use ipset:NAME like an IP, in all statements:

blacklist full ipset:BADGUYS
transparent_squid 3128 "root squid proxy" inface eth0 \
         src ipset:mylans \
         dst not ipset:servers_that_dont_like_proxies
mark 1 OUTPUT dst "ipset:NAME"
server smtp accept src " ipset:NAME1 ipset:NAME2" \
                   dst not "ipset:NAME3 ipset:NAME4"

The good thing about ipset is that you can manipulate the IPs without restarting the firewall. Just add or remove IPs or networks with the ipset command, and immediately the firewall will use the new IPs.

The bad thing is that the ipset collection must exist before activating the firewall. This is why FireHOL can initialize the ipset collections for you:

Defining ipset collections in FireHOL

FireHOL has an ipset helper. It is a wrapper around the real ipset command and is handled internally within FireHOL in such a way so that the ipset collections defined in the configuration will be activated before activating the firewall.

FireHOL is also smart enough to restore the ipsets after a reboot, before it restores the firewall, so that everything will work as seamlessly as possible.

The ipset helper has the same syntax with the real ipset command. So in FireHOL you just add the ipset statements you need, and FireHOL will do the rest.

Keep in mind that each ipset collection is either IPv4 or IPv6. In FireHOL prefix ipset with either ipv4 or ipv6 and FireHOL will choose the right IP version.

The FireHOL helper also allows mass import of ipset collections from files. This is done with ipset addfile command. This command is only supported from within firehol.conf. It will not work on your terminal.

The ipset addfile command will get a filename, remove all comments (anything after a # on the same line), trim any empty lines and spaces, and add all the remaining lines to ipset, as if each line of the file was run as ipset add COLLECTION_NAME IP_FROM_FILE [other options].

The syntax of the ipset addfile command is:

ipset addfile COLLECTION_NAME [ip|net] filename [other ipset add options]

Example in firehol.conf

ipv4 ipset create badguys hash:ip
ipv4 ipset add badguys
ipv4 ipset addfile badguys file-with-the-bad-guys-ips.txt


ipv4 blacklist full ipset:badguys

The ipset add command implemented in FireHOL also allows you to give multiple IPs separated by comma or enclosed in quotes and separated by space. This will also not work on your terminal.

ipv4 ipset create badguys hash:ip
ipv4 ipset add badguys,, # << comma separated
ipv4 ipset add badguys ""  # << space separated in quotes

Real life example

The url contains a list of IPs that we would like to block.

Get this file to /etc/firehol/ with:

cd /etc/firehol
wget ""

open /etc/firehol/firehol.conf and add these:

# one collection for the single IPs
ipv4 ipset create emerging_block_ips hash:ip
ipv4 ipset addfile emerging_block_ips ips emerging-Block-IPs.txt

# another collection for the networks
ipv4 ipset create emerging_block_nets hash:net
ipv4 ipset addfile emerging_block_nets nets emerging-Block-IPs.txt

# blacklist them
ipv4 blacklist full ipset:emerging_block_ips ipset:emerging_block_nets

Now, create a small script to update it daily:


tmp=$(mktemp) || exit 1
wget -O $tmp ""
if [ $? -ne 0 -o ! -s $tmp ]
    rm $tmp
    echo >&2 "Cannot download blacklist."
    exit 1

# update the ipsets
firehol ipset_update_from_file emerging_block_ips ips $tmp
firehol ipset_update_from_file emerging_block_nets nets $tmp
rm $tmp

As you can see we called FireHOL, but this just updates the IPs in the ipsets. It does not touch the firewall. After the ipset_update_from_file parameter, FireHOL accepts everything ipset addfile accepts.