]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/suricata
initscripts/suricata: Move functions order and always use flush_fw_chain function
[ipfire-2.x.git] / src / initscripts / system / suricata
1 #!/bin/sh
2 ########################################################################
3 # Begin $rc_base/init.d/suricata
4 #
5 # Description : Suricata Initscript
6 #
7 # Author : Stefan Schantl <stefan.schantl@ipfire.org>
8 #
9 # Version : 01.00
10 #
11 # Notes :
12 #
13 ########################################################################
14
15 . /etc/sysconfig/rc
16 . ${rc_functions}
17
18 PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin; export PATH
19
20 eval $(/usr/local/bin/readhash /var/ipfire/suricata/settings)
21 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
22
23 # Name of the firewall chain.
24 FW_CHAIN="IPS"
25
26 # Optional options for the Netfilter queue.
27 NFQ_OPTS="--queue-bypass "
28
29 # Array containing the 4 possible network zones.
30 network_zones=( red green blue orange )
31
32 # Mark and Mask options.
33 MARK="0x70000000"
34 MASK="0x70000000"
35
36 # PID file of suricata.
37 PID_FILE="/var/run/suricata.pid"
38
39 # Function to get the amount of CPU cores of the system.
40 function get_cpu_count {
41 CPUCOUNT=0
42
43 # Loop through "/proc/cpuinfo" and count the amount of CPU cores.
44 while read line; do
45 [ "$line" ] && [ -z "${line%processor*}" ] && ((CPUCOUNT++))
46 done </proc/cpuinfo
47
48 echo $CPUCOUNT
49 }
50
51 # Function to flush the firewall chain.
52 function flush_fw_chain {
53 # Call iptables and flush the chain
54 iptables -F "$FW_CHAIN"
55 }
56
57 # Function to create the firewall rules to pass the traffic to suricata.
58 function generate_fw_rules {
59 cpu_count=$(get_cpu_count)
60
61 # Flush the firewall chain.
62 flush_fw_chain
63
64 # Loop through the array of network zones.
65 for zone in "${network_zones[@]}"; do
66 # Convert zone into upper case.
67 zone_upper=${zone^^}
68
69 # Generate variable name for checking if the IDS is
70 # enabled on the zone.
71 enable_ids_zone="ENABLE_IDS_$zone_upper"
72
73 # Check if the IDS is enabled for this network zone.
74 if [ "${!enable_ids_zone}" == "on" ]; then
75 # Check if the current processed zone is "red" and the configured type is PPPoE dialin.
76 if [ "$zone" == "red" ] && [ "$RED_TYPE" == "PPPOE" ]; then
77 # Set device name to ppp0.
78 network_device="ppp0"
79 else
80 # Generate variable name which contains the device name.
81 zone_name="$zone_upper"
82 zone_name+="_DEV"
83
84 # Grab device name.
85 network_device=${!zone_name}
86 fi
87
88 # Assign NFQ_OPTS
89 NFQ_OPTIONS=$NFQ_OPTS
90
91 # Check if there are multiple cpu cores available.
92 if [ "$cpu_count" -gt "1" ]; then
93 # Balance beetween all queues.
94 NFQ_OPTIONS+="--queue-balance 0:$(($cpu_count-1))"
95 NFQ_OPTIONS+=" --queue-cpu-fanout"
96 else
97 # Send all packets to queue 0.
98 NFQ_OPTIONS+="--queue-num 0"
99 fi
100
101 # Create firewall rules to queue the traffic and pass to
102 # the IDS.
103 iptables -I "$FW_CHAIN" -i "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
104 iptables -I "$FW_CHAIN" -o "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS
105 fi
106 done
107
108 # Clear repeat bit, so that it does not confuse IPsec or QoS
109 iptables -A "${FW_CHAIN}" -j MARK --set-xmark "0x0/${MASK}"
110 }
111
112 case "$1" in
113 start)
114 # Get amount of CPU cores.
115 cpu_count=$(get_cpu_count)
116
117 # Numer of NFQUES.
118 NFQUEUES=
119
120 for i in $(seq 0 $((cpu_count-1)) ); do
121 NFQUEUES+="-q $i "
122 done
123
124 # Check if the IDS should be started.
125 if [ "$ENABLE_IDS" == "on" ]; then
126 # Start the IDS.
127 boot_mesg "Starting Intrusion Detection System..."
128 /usr/bin/suricata -c /etc/suricata/suricata.yaml -D $NFQUEUES >/dev/null 2>/dev/null
129 evaluate_retval
130
131 # Allow reading the pidfile.
132 chmod 644 $PID_FILE
133
134 # Flush the firewall chain
135 flush_fw_chain
136
137 # Generate firewall rules
138 generate_fw_rules
139 fi
140 ;;
141
142 stop)
143 boot_mesg "Stopping Intrusion Detection System..."
144 killproc -p $PID_FILE /var/run
145
146 # Flush firewall chain.
147 flush_fw_chain
148
149 # Remove suricata control socket.
150 rm /var/run/suricata/* >/dev/null 2>/dev/null
151
152 # Don't report returncode of rm if suricata was not started
153 exit 0
154 ;;
155
156 status)
157 statusproc /usr/bin/suricata
158 ;;
159
160 restart)
161 $0 stop
162 $0 start
163 ;;
164 reload)
165 # Send SIGUSR2 to the suricata process to perform a reload
166 # of the ruleset.
167 kill -USR2 $(pidof suricata)
168
169 # Flush the firewall chain.
170 flush_fw_chain
171
172 # Generate firewall rules.
173 generate_fw_rules
174 ;;
175
176 *)
177 echo "Usage: $0 {start|stop|restart|reload|status}"
178 exit 1
179 ;;
180 esac
181
182 chmod 644 /var/log/suricata/* 2>/dev/null
183
184 # End $rc_base/init.d/suricata