Table of Contents

, , ,

NFTables IP Control

I primarily set this feature up to act as a form of parental control on my home internet access.

Disable Range of IP addresses, with count-down timer

The DHCP assigned addresses in the range 100 - 254 were to be disabled (dropped) in the evening. The addresses below 100 were assigned a specific IP address in the DHCP based upon MAC.

I added a NFtables named set with ipV4 address and timeout function:

     set controllist {
         type ipv4_addr
         flags interval
         flags timeout
     }

In the forward chain the controllist IP set was dropped for all the source and destination packets.

       ip daddr @controllist counter drop
       ip saddr @controllist counter drop

Router NFtable Setup - with named set IP filtering

Some important NFtables Commands with named sets:

I made a couple of bash scripts to assist with the use of these controllist name sets.

controllist.sh

deletecontrol.sh

I then created a cron job to run the script as required every evening

crontab is finicky! crontab does not necessary use BASH and the full path to the command must be given for reliable performance. Further to this cron error messages are sent to the system mail server, so if this is not setup or otherwise not working the error message go nowhere. Systemd has a service to redirect output of cron jobs to systemd's journal: /usr/bin/systemd-cat -t controllist, again the full path is given. The command path can be found using which, e.g. which nft. In any case the final crontab command entry would look like: /usr/bin/systemd-cat -t controllist /home/baumkp/controllist.sh, remembering everything after the 5th space is passed to the system shell command interpreter.

example: ''sudo crontab -e''

The crontab files are stored at /var/spool/cron/crontabs/$USER. You should not edit these files directly, use crontab -e for current user or sudo crontab -e for root.

Limit Rate on IP Address Range

The rate limit command needs to be placed before the other commands that could accept packets before reaching the rate limit command, e.g. ct state established, related counter accept. The following command will add the command at handle 29:

The existing rules with handles displayed can be displayed with:

The above command will accept packets according to filter that do not exceed 1200kbytes/second with a burst of 9000kbytes. Another form of syntax would be to drop packets that exceed the limit, this allows the amount of drop packets to be seen with the counter enabled:

My internet bandwidth is currently limited to about 25Mbit/s, dividing by 8 give approximate MByte/s, i.e. about 3MB/s or 3000mbytes/s or 3000kbytes/s, hence I limit the kids bandwidth to 1200kbytes/s with an allowed burst of 4000kbytes.

Some other tools