]> git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blobdiff - src/initscripts/system/suricata
Merge branch 'master' of ssh://git.ipfire.org/pub/git/ipfire-2.x
[people/mfischer/ipfire-2.x.git] / src / initscripts / system / suricata
index c9f131fcae3dfee203ce3058a90a803eef1c7347..938ea66de6ccb55388bbf334455b10ddc58ac478 100644 (file)
@@ -1,16 +1,23 @@
 #!/bin/sh
-########################################################################
-# Begin $rc_base/init.d/suricata
-#
-# Description : Suricata Initscript
-#
-# Author      : Stefan Schantl <stefan.schantl@ipfire.org>
-#
-# Version     : 01.01
-#
-# Notes       :
-#
-########################################################################
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
 
 . /etc/sysconfig/rc
 . ${rc_functions}
@@ -29,15 +36,11 @@ IPS_OUTPUT_CHAIN="IPS_OUTPUT"
 NFQ_OPTS="--queue-bypass "
 
 # Array containing the 4 possible network zones.
-network_zones=( red green blue orange )
+network_zones=( red green blue orange ovpn )
 
 # Array to store the network zones weather the IPS is enabled for.
 enabled_ips_zones=()
 
-# Mark and Mask options.
-MARK="0x70000000"
-MASK="0x70000000"
-
 # PID file of suricata.
 PID_FILE="/var/run/suricata.pid"
 
@@ -50,15 +53,21 @@ function get_cpu_count {
                [ "$line" ] && [ -z "${line%processor*}" ]  && ((CPUCOUNT++))
        done </proc/cpuinfo
 
-       echo $CPUCOUNT
+       # Limit to a maximum of 16 cores, because suricata does not support more than
+       # 16 netfilter queues at the moment.
+       if [ $CPUCOUNT -gt "16" ]; then
+               echo "16"
+       else
+               echo $CPUCOUNT
+       fi
 }
 
 # Function to flush the firewall chains.
 function flush_fw_chain {
        # Call iptables and flush the chains
-       iptables -F "$IPS_INPUT_CHAIN"
-       iptables -F "$IPS_FORWARD_CHAIN"
-       iptables -F "$IPS_OUTPUT_CHAIN"
+       iptables -w -F "$IPS_INPUT_CHAIN"
+       iptables -w -F "$IPS_FORWARD_CHAIN"
+       iptables -w -F "$IPS_OUTPUT_CHAIN"
 }
 
 # Function to create the firewall rules to pass the traffic to suricata.
@@ -80,6 +89,22 @@ function generate_fw_rules {
                        if [ "$zone" == "red" ] && [ "$RED_TYPE" == "PPPOE" ]; then
                                # Set device name to ppp0.
                                network_device="ppp0"
+                       elif [ "$zone" == "ovpn" ]; then
+                               # Get all virtual net devices because the RW server and each
+                               # N2N connection creates it's own tun device.
+                               for virt_dev in /sys/devices/virtual/net/*; do
+                                       # Cut-off the directory.
+                                       dev="${virt_dev##*/}"
+
+                                       # Only process tun devices.
+                                       if [[ $dev =~ "tun" ]]; then
+                                               # Add the network device to the array of enabled zones.
+                                               enabled_ips_zones+=( "$dev" )
+                                       fi
+                               done
+
+                               # Process next zone.
+                               continue
                        else
                                # Generate variable name which contains the device name.
                                zone_name="$zone_upper"
@@ -115,19 +140,14 @@ function generate_fw_rules {
                # Loop through the array and create firewall rules.
                for enabled_ips_zone in "${enabled_ips_zones[@]}"; do
                        # Create rules queue input and output related traffic and pass it to the IPS.
-                       iptables -I "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
-                       iptables -I "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
+                       iptables -w -A "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -j NFQUEUE $NFQ_OPTIONS
+                       iptables -w -A "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -j NFQUEUE $NFQ_OPTIONS
 
                        # Create rules which are required to handle forwarded traffic.
                        for enabled_ips_zone_forward in "${enabled_ips_zones[@]}"; do
-                               iptables -I "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
+                               iptables -w -A "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" -j NFQUEUE $NFQ_OPTIONS
                        done
                done
-
-               # Clear repeat bit, so that it does not confuse IPsec or QoS
-               iptables -A "${IPS_INPUT_CHAIN}" -j MARK --set-xmark "0x0/${MASK}"
-               iptables -A "${IPS_FORWARD_CHAIN}" -j MARK --set-xmark "0x0/${MASK}"
-               iptables -A "${IPS_OUTPUT_CHAIN}" -j MARK --set-xmark "0x0/${MASK}"
        fi
 }
 
@@ -137,11 +157,11 @@ case "$1" in
                cpu_count=$(get_cpu_count)
 
                # Numer of NFQUES.
-               NFQUEUES=
+               NFQUEUES="-q 0"
 
-               for i in $(seq 0 $((cpu_count-1)) ); do
-                       NFQUEUES+="-q $i "
-               done
+               if [ $cpu_count -gt "1" ]; then
+                       NFQUEUES+=":$(($cpu_count-1))"
+               fi
 
                # Check if the IDS should be started.
                if [ "$ENABLE_IDS" == "on" ]; then
@@ -168,17 +188,23 @@ case "$1" in
                # Flush firewall chain.
                flush_fw_chain
 
-               # Remove suricata control socket.              
+               # Sometimes suricata not correct shutdown. So killall.
+               killall -KILL /usr/bin/suricata 2>/dev/null
+
+               # Remove suricata control socket.
                rm /var/run/suricata/* >/dev/null 2>/dev/null
 
+               # Trash remain pid file if still exists.
+               rm -f $PID_FILE >/dev/null 2>/dev/null
+
                # Don't report returncode of rm if suricata was not started
                exit 0
         ;;
-                
+
         status)
                 statusproc /usr/bin/suricata
                 ;;
-                
+
         restart)
                 $0 stop
                 $0 start
@@ -194,7 +220,7 @@ case "$1" in
                # Generate firewall rules.
                generate_fw_rules
                ;;
-                
+
         *)
                 echo "Usage: $0 {start|stop|restart|reload|status}"
                 exit 1
@@ -202,5 +228,3 @@ case "$1" in
 esac
 
 chmod 644 /var/log/suricata/* 2>/dev/null
-
-# End $rc_base/init.d/suricata