]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - src/initscripts/system/suricata
suricata: Use device ppp0 if PPPoE dialin is used.
[ipfire-2.x.git] / src / initscripts / system / suricata
index 45e04d4639246c299acd0ab3fe9b186663db20c6..ecd6930541f03bfd3a8b1c9faad0392ea5e3b07a 100644 (file)
@@ -18,6 +18,7 @@
 PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin; export PATH
 
 eval $(/usr/local/bin/readhash /var/ipfire/suricata/settings)
+eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
 
 # Name of the firewall chain.
 FW_CHAIN="IPS"
@@ -29,61 +30,112 @@ NFQ_OPTS="--queue-bypass "
 network_zones=( red green blue orange )
 
 # Mark and Mask options.
-MARK="0x1"
-MASK="0x1"
+MARK="0x70000000"
+MASK="0x70000000"
 
 # PID file of suricata.
 PID_FILE="/var/run/suricata.pid"
 
+# Function to get the amount of CPU cores of the system.
+function get_cpu_count {
+       CPUCOUNT=0
+
+       # Loop through "/proc/cpuinfo" and count the amount of CPU cores.
+       while read line; do
+               [ "$line" ] && [ -z "${line%processor*}" ]  && ((CPUCOUNT++))
+       done </proc/cpuinfo
+
+       echo $CPUCOUNT
+}
+
+# Function to create the firewall rules to pass the traffic to suricata.
+function generate_fw_rules {
+       cpu_count=$(get_cpu_count)
+
+       # Flush the firewall chain.
+       iptables -F "$FW_CHAIN"
+
+       # Loop through the array of network zones.
+       for zone in "${network_zones[@]}"; do
+               # Convert zone into upper case.
+               zone_upper=${zone^^}
+
+               # Generate variable name for checking if the IDS is
+               # enabled on the zone.
+               enable_ids_zone="ENABLE_IDS_$zone_upper"
+
+               # Check if the IDS is enabled for this network zone.
+               if [ "${!enable_ids_zone}" == "on" ]; then
+                       # Check if the current processed zone is "red" and the configured type is PPPoE dialin.
+                       if [ "$zone" == "red" ] && [ "$RED_TYPE" == "PPPOE" ]; then
+                               # Set device name to ppp0.
+                               network_device="ppp0"
+                       else
+                               # Generate variable name which contains the device name.
+                               zone_name="$zone_upper"
+                               zone_name+="_DEV"
+
+                               # Grab device name.
+                               network_device=${!zone_name}
+                       fi
+
+                       # Assign NFQ_OPTS
+                       NFQ_OPTIONS=$NFQ_OPTS
+
+                       # Check if there are multiple cpu cores available.
+                       if [ "$cpu_count" -gt "1" ]; then
+                               # Balance beetween all queues.
+                               NFQ_OPTIONS+="--queue-balance 0:$(($cpu_count-1))"
+                               NFQ_OPTIONS+=" --queue-cpu-fanout"
+                       else
+                               # Send all packets to queue 0.
+                               NFQ_OPTIONS+="--queue-num 0"
+                       fi
+
+                       # Create firewall rules to queue the traffic and pass to
+                       # the IDS.
+                       iptables -I "$FW_CHAIN" -i "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
+                       iptables -I "$FW_CHAIN" -o "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
+               fi
+       done
+
+       # Clear repeat bit, so that it does not confuse IPsec or QoS
+       iptables -A "${FW_CHAIN}" -j MARK --set-xmark "0x0/${MASK}"
+}
+
+# Function to flush the firewall chain.
+function flush_fw_chain {
+       # Call iptables and flush the chain
+       iptables -F "$FW_CHAIN"
+}
+
 case "$1" in
         start)
                # Get amount of CPU cores.
+               cpu_count=$(get_cpu_count)
+
+               # Numer of NFQUES.
                NFQUEUES=
-               CPUCOUNT=0
-               while read line; do
-                       [ "$line" ] && [ -z "${line%processor*}" ] && NFQUEUES+="-q $CPUCOUNT " && ((CPUCOUNT++))
-               done </proc/cpuinfo
+
+               for i in $(seq 0 $((cpu_count-1)) ); do
+                       NFQUEUES+="-q $i "
+               done
 
                # Check if the IDS should be started.
                if [ "$ENABLE_IDS" == "on" ]; then
-                       # Loop through the array of network zones.
-                       for zone in "${network_zones[@]}"; do
-                               # Convert zone into upper case.
-                               zone_upper=${zone^^}
-
-                               # Check if the IDS is enabled for this network zone.
-                               if [ "$ENABLE_IDS_$$zone_upper" == "on" ]; then
-                                       # Generate name of the network interface.
-                                       network_device=$zone
-                                       network_device+="0"
-
-                                       # Assign NFQ_OPTS
-                                       NFQ_OPTIONS=$NFQ_OPTS
-
-                                       # Check if there are multiple cpu cores available.
-                                       if [ "$CPUCOUNT" > 0 ]; then
-                                               # Balance beetween all queues.
-                                               NFQ_OPTIONS+="--queue-balance 0:"
-                                               NFQ_OPTIONS+=$(($CPUCOUNT-1))
-                                       else
-                                               # Send all packets to queue 0.
-                                               NFQ_OPTIONS+="--queue-num 0"
-                                       fi
-
-                                       # Create firewall rules to queue the traffic and pass to
-                                       # the IDS.
-                                       iptables -I "$FW_CHAIN" -i "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE "$NFQ_OPTIONS"
-                                       iptables -I "$FW_CHAIN" -o "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE "$NFQ_OPTIONS"
-                               fi
-                       done
-
                        # Start the IDS.
                        boot_mesg "Starting Intrusion Detection System..."
-                       /usr/bin/suricata -c /etc/suricata/suricata.yaml -D $NFQUEUES
+                       /usr/bin/suricata -c /etc/suricata/suricata.yaml -D $NFQUEUES >/dev/null 2>/dev/null
                        evaluate_retval
 
                        # Allow reading the pidfile.
                        chmod 644 $PID_FILE
+
+                       # Flush the firewall chain
+                       flush_fw_chain
+
+                       # Generate firewall rules
+                       generate_fw_rules
                fi
        ;;
 
@@ -92,7 +144,7 @@ case "$1" in
                killproc -p $PID_FILE /var/run
 
                # Flush firewall chain.
-               iptables -F $FW_CHAIN
+               flush_fw_chain
 
                # Remove suricata control socket.              
                rm /var/run/suricata/* >/dev/null 2>/dev/null
@@ -113,6 +165,12 @@ case "$1" in
                # Send SIGUSR2 to the suricata process to perform a reload
                # of the ruleset.
                kill -USR2 $(pidof suricata)
+
+               # Flush the firewall chain.
+               flush_fw_chain
+
+               # Generate firewall rules.
+               generate_fw_rules
                ;;
                 
         *)