Forward Firewall: some changes in firewall script to make collectd work
[people/teissler/ipfire-2.x.git] / src / initscripts / init.d / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
3eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
4eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
fe0cd647 5eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
3a1019f6
MT
6IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
7
8if [ -f /var/ipfire/red/device ]; then
9 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
10fi
11
12iptables_init() {
13 # Flush all rules and delete all custom chains
14 /sbin/iptables -F
15 /sbin/iptables -t nat -F
16 /sbin/iptables -t mangle -F
17 /sbin/iptables -X
18 /sbin/iptables -t nat -X
19 /sbin/iptables -t mangle -X
20
21 # Set up policies
22 /sbin/iptables -P INPUT DROP
23 /sbin/iptables -P FORWARD DROP
24 /sbin/iptables -P OUTPUT ACCEPT
25
26 # Empty LOG_DROP and LOG_REJECT chains
27 /sbin/iptables -N LOG_DROP
28 /sbin/iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
29 /sbin/iptables -A LOG_DROP -j DROP
30 /sbin/iptables -N LOG_REJECT
31 /sbin/iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
32 /sbin/iptables -A LOG_REJECT -j REJECT
33
34 # This chain will log, then DROPs packets with certain bad combinations
35 # of flags might indicate a port-scan attempt (xmas, null, etc)
36 /sbin/iptables -N PSCAN
5595bc03 37 if [ "$DROPPORTSCAN" == "on" ]; then
97fe1741 38 /sbin/iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
fe0cd647 39 /sbin/iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
97fe1741
CS
40 /sbin/iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
41 /sbin/iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 42 fi
97fe1741 43 /sbin/iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
44
45 # New tcp packets without SYN set - could well be an obscure type of port scan
46 # that's not covered above, may just be a broken windows machine
47 /sbin/iptables -N NEWNOTSYN
5595bc03 48 if [ "$DROPNEWNOTSYN" == "on" ]; then
97fe1741 49 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 50 fi
97fe1741 51 /sbin/iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6
MT
52
53 # Chain to contain all the rules relating to bad TCP flags
54 /sbin/iptables -N BADTCP
55
d8158ca6 56 #Don't check loopback
a56df4ee 57 /sbin/iptables -A BADTCP -i lo -j RETURN
d8158ca6 58
3a1019f6
MT
59 # Disallow packets frequently used by port-scanners
60 # nmap xmas
61 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
62 # Null
63 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
64 # FIN
65 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
66 # SYN/RST (also catches xmas variants that set SYN+RST+...)
67 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
68 # SYN/FIN (QueSO or nmap OS probe)
69 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
70 # NEW TCP without SYN
71 /sbin/iptables -A BADTCP -p tcp ! --syn -m state --state NEW -j NEWNOTSYN
72
73 /sbin/iptables -A INPUT -j BADTCP
74 /sbin/iptables -A FORWARD -j BADTCP
75
76}
77
78iptables_red() {
79 /sbin/iptables -F REDINPUT
80 /sbin/iptables -F REDFORWARD
81 /sbin/iptables -t nat -F REDNAT
82
83 # PPPoE / PPTP Device
84 if [ "$IFACE" != "" ]; then
85 # PPPoE / PPTP
86 if [ "$DEVICE" != "" ]; then
87 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
88 fi
89 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
90 if [ "$RED_DEV" != "" ]; then
91 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
92 fi
93 fi
94 fi
95
96 # PPTP over DHCP
97 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
98 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
99 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
100 fi
101
102 # Orange pinholes
103 if [ "$ORANGE_DEV" != "" ]; then
104 # This rule enables a host on ORANGE network to connect to the outside
105 # (only if we have a red connection)
106 if [ "$IFACE" != "" ]; then
139a9b4f 107 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
3a1019f6
MT
108 fi
109 fi
110
111 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
112 # DHCP
113 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
114 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
115 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
116 fi
117 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
118 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
119 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
120 fi
121
db073a10 122 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
53f4c74d
AM
123 #/sbin/iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
124 #/sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
3a1019f6
MT
125
126 fi
127}
128
129# See how we were called.
130case "$1" in
131 start)
132 iptables_init
133
134 # Limit Packets- helps reduce dos/syn attacks
135 # original do nothing line
136 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 10/sec
137 # the correct one, but the negative '!' do nothing...
9efdd899 138 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN ! -m limit --limit 10/sec -j DROP
3a1019f6
MT
139
140 # Fix for braindead ISP's
141 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
142
143 # CUSTOM chains, can be used by the users themselves
144 /sbin/iptables -N CUSTOMINPUT
145 /sbin/iptables -A INPUT -j CUSTOMINPUT
057249ba
CS
146 /sbin/iptables -N GUARDIAN
147 /sbin/iptables -A INPUT -j GUARDIAN
148 /sbin/iptables -A FORWARD -j GUARDIAN
3a1019f6
MT
149 /sbin/iptables -N CUSTOMFORWARD
150 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
151 /sbin/iptables -N CUSTOMOUTPUT
ef6f983b 152 /sbin/iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
3a1019f6 153 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
4cb74dce
MT
154 /sbin/iptables -N OUTGOINGFW
155 /sbin/iptables -A OUTPUT -j OUTGOINGFW
3a1019f6
MT
156 /sbin/iptables -t nat -N CUSTOMPREROUTING
157 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
158 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
159 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
160
daa1ceba
AF
161 # IPTV chains for IGMPPROXY
162 /sbin/iptables -N IPTVINPUT
163 /sbin/iptables -A INPUT -j IPTVINPUT
164 /sbin/iptables -N IPTVFORWARD
165 /sbin/iptables -A FORWARD -j IPTVFORWARD
166
3a1019f6
MT
167 # filtering from GUI
168 /sbin/iptables -N GUIINPUT
169 /sbin/iptables -A INPUT -j GUIINPUT
905fbf3e 170 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6
MT
171
172 # Accept everything connected
173 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
174 /sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
5fd30232 175
ef6f983b
AM
176 # Accept everything on lo
177 iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
178 iptables -A OUTPUT -o lo -m state --state NEW -j ACCEPT
179
5fd30232 180 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
6652626c
AF
181 /sbin/iptables -N IPSECINPUT
182 /sbin/iptables -N IPSECFORWARD
183 /sbin/iptables -N IPSECOUTPUT
5fd30232 184 /sbin/iptables -N OPENSSLVIRTUAL
6652626c 185 /sbin/iptables -A INPUT -j IPSECINPUT
5595bc03 186 /sbin/iptables -A INPUT -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL INPUT"
6652626c 187 /sbin/iptables -A FORWARD -j IPSECFORWARD
5595bc03 188 /sbin/iptables -A FORWARD -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL FORWARD"
6652626c 189 /sbin/iptables -A OUTPUT -j IPSECOUTPUT
fcbf5eef 190 /sbin/iptables -t nat -N OVPNNAT
c4cd0f7b 191 /sbin/iptables -t nat -N IPSECNAT
b2e333d4 192 /sbin/iptables -t nat -A POSTROUTING -j OVPNNAT
c4cd0f7b 193 /sbin/iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 194
231499fc
AM
195 # Input Firewall
196 /sbin/iptables -N INPUTFW
197 /sbin/iptables -A INPUT -m state --state NEW -j INPUTFW
198
3a1019f6 199 # localhost and ethernet.
d8158ca6 200 /sbin/iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
3a1019f6
MT
201 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m state --state NEW -j DROP # Loopback not on lo
202 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m state --state NEW -j DROP
cae0079c 203 /sbin/iptables -A FORWARD -i lo -m state --state NEW -j ACCEPT
3a1019f6
MT
204 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m state --state NEW -j DROP
205 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m state --state NEW -j DROP
dd79c399 206 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT ! -p icmp
218b3341 207
3a1019f6
MT
208 # allow DHCP on BLUE to be turned on/off
209 /sbin/iptables -N DHCPBLUEINPUT
210 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
81393987
AM
211
212 # WIRELESS chains
213 /sbin/iptables -N WIRELESSINPUT
214 /sbin/iptables -A INPUT -m state --state NEW -j WIRELESSINPUT
215 /sbin/iptables -N WIRELESSFORWARD
9efd8d1c 216 /sbin/iptables -A FORWARD -m state --state NEW -j WIRELESSFORWARD
12dcfbbd 217
93b75f31
AM
218 # Forward Firewall
219 /sbin/iptables -N FORWARDFW
220 /sbin/iptables -A FORWARD -j FORWARDFW
221
12dcfbbd
AM
222 # PORTFWACCESS chain, used for portforwarding
223 /sbin/iptables -N PORTFWACCESS
224 /sbin/iptables -A FORWARD -m state --state NEW -j PORTFWACCESS
225
5fd30232
MT
226 # OPenSSL
227 /sbin/iptables -N OPENSSLPHYSICAL
228 /sbin/iptables -A INPUT -j OPENSSLPHYSICAL
3a1019f6 229
3a1019f6
MT
230 # RED chain, used for the red interface
231 /sbin/iptables -N REDINPUT
232 /sbin/iptables -A INPUT -j REDINPUT
233 /sbin/iptables -N REDFORWARD
234 /sbin/iptables -A FORWARD -j REDFORWARD
235 /sbin/iptables -t nat -N REDNAT
236 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
237
238 iptables_red
218b3341 239
3a1019f6
MT
240 # Custom prerouting chains (for transparent proxy and port forwarding)
241 /sbin/iptables -t nat -N SQUID
242 /sbin/iptables -t nat -A PREROUTING -j SQUID
9efd8d1c
AM
243 /sbin/iptables -t nat -N NAT_DESTINATION
244 /sbin/iptables -t nat -N NAT_SOURCE
245 /sbin/iptables -t nat -A PREROUTING -j NAT_DESTINATION
246 /sbin/iptables -t nat -A POSTROUTING -j NAT_SOURCE
247
248
7e7495b3
MT
249 # upnp chain for our upnp daemon
250 /sbin/iptables -t nat -N UPNPFW
251 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
54194ba4 252 /sbin/iptables -N UPNPFW
42cb3860 253 /sbin/iptables -A FORWARD -m state --state NEW -j UPNPFW
3a1019f6 254
3a1019f6
MT
255 # Postrouting rules (for port forwarding)
256 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT \
257 --to-source $GREEN_ADDRESS
258 if [ "$BLUE_DEV" != "" ]; then
259 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
260 fi
261 if [ "$ORANGE_DEV" != "" ]; then
262 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
263 fi
264
3a1019f6
MT
265 # run local firewall configuration, if present
266 if [ -x /etc/sysconfig/firewall.local ]; then
267 /etc/sysconfig/firewall.local start
268 fi
269
62fc8511 270 #POLICY CHAIN
a9b3ae26
AM
271 /sbin/iptables -N POLICYIN
272 /sbin/iptables -A INPUT -j POLICYIN
5d7faa45
AM
273 /sbin/iptables -N POLICYFWD
274 /sbin/iptables -A FORWARD -j POLICYFWD
275 /sbin/iptables -N POLICYOUT
276 /sbin/iptables -A OUTPUT -j POLICYOUT
b324de14 277
5d7faa45 278 /usr/sbin/firewall-policy
b324de14 279
94ea1f03
AM
280 #Only for firewall Hits statistik
281 /sbin/iptables -A POLICYFWD -j DROP -m comment --comment "DROP_FORWARD"
282 /sbin/iptables -A POLICYOUT -j DROP -m comment --comment "DROP_OUTPUT"
fd10a52c 283 ;;
13211b21
CS
284 startovpn)
285 # run openvpn
286 /usr/local/bin/openvpnctrl --create-chains-and-rules
287 ;;
3a1019f6
MT
288 stop)
289 iptables_init
290 # Accept everyting connected
291 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
292
293 # localhost and ethernet.
294 /sbin/iptables -A INPUT -i lo -j ACCEPT
295 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT
296
297 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
298 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
299 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
300 fi
301 if [ "$PROTOCOL" == "RFC1483" -a "$METHOD" == "DHCP" ]; then
302 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
303 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
304 fi
305
3a1019f6
MT
306 # run local firewall configuration, if present
307 if [ -x /etc/sysconfig/firewall.local ]; then
308 /etc/sysconfig/firewall.local stop
309 fi
310
5595bc03 311 if [ "$DROPINPUT" == "on" ]; then
97fe1741 312 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
5595bc03 313 fi
97fe1741 314 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
6be0579b 315 if [ "$DROPFORWARD" == "on" ]; then
62fc8511 316 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD "
6be0579b 317 fi
62fc8511 318 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_FORWARD"
ed31c098
AM
319
320 #Only for firewall Hits statistik
53f4c74d
AM
321 #/sbin/iptables -A POLICYFWD -j DROP -m comment --comment "DROP_FORWARD"
322 #/sbin/iptables -A POLICYOUT -j DROP -m comment --comment "DROP_OUTPUT"
ed31c098
AM
323
324
325
6be0579b 326 ;;
13211b21
CS
327 stopovpn)
328 # stop openvpn
329 /usr/local/bin/openvpnctrl --delete-chains-and-rules
330 ;;
3a1019f6
MT
331 reload)
332 iptables_red
333
334 # run local firewall configuration, if present
335 if [ -x /etc/sysconfig/firewall.local ]; then
336 /etc/sysconfig/firewall.local reload
337 fi
338 ;;
339 restart)
340 $0 stop
341 $0 start
210ee67b
AM
342 /usr/local/bin/forwardfwctrl
343 /usr/local/bin/openvpnctrl -s > /dev/null 2>&1
344 /usr/local/bin/openvpnctrl -sn2n > /dev/null 2>&1
3a1019f6
MT
345 ;;
346 *)
347 echo "Usage: $0 {start|stop|reload|restart}"
348 exit 1
349 ;;
350esac
351
352exit 0