Each Netfilter module needs to operate at two layers, the kernel space and user space. The user space supports the arguments for the "iptables" binary needed for that specific module. For example the user space code supports the "--funccode", "--unitid" "--len" etc. It parses the arguments, passes them to the kernel space module and it registers itself with the kernel space module. The kernel space module is the one which actually parses the packets hitting it and checks its contents against the user supplied values. As with any other Netfilter module, the compilation for this module should be done at both kernel and userspace as explained below. The kernel space patch goes into the Linux kernel code, into net/ipv4/netfilter/ directory, adds a new kernel module(ipt_modbus.o) upon compilation. The user space patch patches the iptables source code and adds a new shared library(libipt_modbus.so) upon compilation. The kernel module should be loaded into the kernel and the shared library should be placed inside the directory where all other shared libraries for iptables exist(usually /lib/iptables) so that iptables will go find it when a "-m modbus" argument is specified.
# iptables -A INPUT -p tcp -m modbus --funccode 16 –allowtcp 0 -j DROP
(Drops whenever the functioni code of the received packet is 16}
# iptables -A INPUT -p tcp -m modbus --funccode ! 16 –allowtcp 0 -j DROP(Drops whenever the functioni code of the received packet is NOT 16)
# iptables -A INPUT -p tcp -m modbus --funccode 16 –allowtcp 0 --unitid !3 --refnum 5433 -j DROP(The packet will the dropped when either function code is 16, OR unitid is not 3 OR reference number is 5433)
# iptables -A INPUT -p tcp -m modbus --funccode 16-23 –allowtcp 0 --unitid !3 --refnum 5433 –len 64 -j DROP(The packet will the dropped when either function code is between 16 and 23, OR unitid is not 3 OR reference number is 5433 OR the length is 64)
# iptables -A INPUT -p tcp -m modbus --funccode 16-23 --unitid !3 --refnum 5433 –len lt64 –allowtcp 0 -j DROP(The packet will the dropped when either function code is between 16 and 23, OR unitid is not 3 OR reference number is 5433 OR the length less than 64)