f4cc2afa8780cf528f3b0cfc756834a56bb61c32
[people/teissler/ipfire-2.x.git] / src / initscripts / init.d / firewall
1 #!/bin/sh
2
3 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
4 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
5 eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
6 IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
7
8 if [ -f /var/ipfire/red/device ]; then
9 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
10 fi
11
12 iptables_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
37 if [ "$DROPPORTSCAN" == "on" ]; then
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"
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"
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"
42 fi
43 /sbin/iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
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
48 if [ "$DROPNEWNOTSYN" == "on" ]; then
49 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
50 fi
51 /sbin/iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
52
53 # Chain to contain all the rules relating to bad TCP flags
54 /sbin/iptables -N BADTCP
55
56 #Don't check loopback
57 /sbin/iptables -A BADTCP -i lo -j RETURN
58
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
78 iptables_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
107 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
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
122 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
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
125
126 fi
127 }
128
129 # See how we were called.
130 case "$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...
138 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN ! -m limit --limit 10/sec -j DROP
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
146 /sbin/iptables -N GUARDIAN
147 /sbin/iptables -A INPUT -j GUARDIAN
148 /sbin/iptables -A FORWARD -j GUARDIAN
149 /sbin/iptables -N CUSTOMFORWARD
150 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
151 /sbin/iptables -N CUSTOMOUTPUT
152 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
153 /sbin/iptables -N OUTGOINGFW
154 /sbin/iptables -N OUTGOINGFWMAC
155 /sbin/iptables -A OUTPUT -j OUTGOINGFW
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
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
167 # filtering from GUI
168 /sbin/iptables -N GUIINPUT
169 /sbin/iptables -A INPUT -j GUIINPUT
170 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
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
175
176 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
177 /sbin/iptables -N IPSECINPUT
178 /sbin/iptables -N IPSECFORWARD
179 /sbin/iptables -N IPSECOUTPUT
180 /sbin/iptables -N OPENSSLVIRTUAL
181 /sbin/iptables -A INPUT -j IPSECINPUT
182 /sbin/iptables -A INPUT -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL INPUT"
183 /sbin/iptables -A FORWARD -j IPSECFORWARD
184 /sbin/iptables -A FORWARD -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL FORWARD"
185 /sbin/iptables -A OUTPUT -j IPSECOUTPUT
186 /sbin/iptables -t nat -N OVPNNAT
187 /sbin/iptables -t nat -N IPSECNAT
188 /sbin/iptables -t nat -A POSTROUTING -j OVPNNAT
189 /sbin/iptables -t nat -A POSTROUTING -j IPSECNAT
190
191 # TOR
192 /sbin/iptables -N TOR_INPUT
193 /sbin/iptables -A INPUT -j TOR_INPUT
194
195 # Outgoing Firewall
196 /sbin/iptables -A FORWARD -j OUTGOINGFWMAC
197
198 # Forward Firewall
199 /sbin/iptables -N FORWARDFW
200 /sbin/iptables -A FORWARD -j FORWARDFW
201
202 # Input Firewall
203 /sbin/iptables -N INPUTFW
204 /sbin/iptables -A INPUT -m state --state NEW -j INPUTFW
205
206 # localhost and ethernet.
207 /sbin/iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
208 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m state --state NEW -j DROP # Loopback not on lo
209 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m state --state NEW -j DROP
210 /sbin/iptables -A FORWARD -i lo -m state --state NEW -j ACCEPT
211 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m state --state NEW -j DROP
212 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m state --state NEW -j DROP
213 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT ! -p icmp
214 /sbin/iptables -A FORWARD -i $GREEN_DEV -m state --state NEW -j ACCEPT
215
216 # If a host on orange tries to initiate a connection to IPFire's red IP and
217 # the connection gets DNATed back through a port forward to a server on orange
218 # we end up with orange -> orange traffic passing through IPFire
219 [ "$ORANGE_DEV" != "" ] && /sbin/iptables -A FORWARD -i $ORANGE_DEV -o $ORANGE_DEV -m state --state NEW -j ACCEPT
220
221 # allow DHCP on BLUE to be turned on/off
222 /sbin/iptables -N DHCPBLUEINPUT
223 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
224
225 # OPenSSL
226 /sbin/iptables -N OPENSSLPHYSICAL
227 /sbin/iptables -A INPUT -j OPENSSLPHYSICAL
228
229 # WIRELESS chains
230 /sbin/iptables -N WIRELESSINPUT
231 /sbin/iptables -A INPUT -m state --state NEW -j WIRELESSINPUT
232 /sbin/iptables -N WIRELESSFORWARD
233 /sbin/iptables -A FORWARD -m state --state NEW -j WIRELESSFORWARD
234
235 # RED chain, used for the red interface
236 /sbin/iptables -N REDINPUT
237 /sbin/iptables -A INPUT -j REDINPUT
238 /sbin/iptables -N REDFORWARD
239 /sbin/iptables -A FORWARD -j REDFORWARD
240 /sbin/iptables -t nat -N REDNAT
241 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
242
243 iptables_red
244
245 # PORTFWACCESS chain, used for portforwarding
246 /sbin/iptables -N PORTFWACCESS
247 /sbin/iptables -A FORWARD -m state --state NEW -j PORTFWACCESS
248
249 # Custom prerouting chains (for transparent proxy and port forwarding)
250 /sbin/iptables -t nat -N SQUID
251 /sbin/iptables -t nat -A PREROUTING -j SQUID
252 /sbin/iptables -t nat -N PORTFW
253 /sbin/iptables -t nat -A PREROUTING -j PORTFW
254
255 # upnp chain for our upnp daemon
256 /sbin/iptables -t nat -N UPNPFW
257 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
258 /sbin/iptables -N UPNPFW
259 /sbin/iptables -A FORWARD -m state --state NEW -j UPNPFW
260
261 # Custom mangle chain (for port fowarding)
262 /sbin/iptables -t mangle -N PORTFWMANGLE
263 /sbin/iptables -t mangle -A PREROUTING -j PORTFWMANGLE
264
265 # Postrouting rules (for port forwarding)
266 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT \
267 --to-source $GREEN_ADDRESS
268 if [ "$BLUE_DEV" != "" ]; then
269 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
270 fi
271 if [ "$ORANGE_DEV" != "" ]; then
272 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
273 fi
274
275 # run local firewall configuration, if present
276 if [ -x /etc/sysconfig/firewall.local ]; then
277 /etc/sysconfig/firewall.local start
278 fi
279
280 # last rule in input and forward chain is for logging.
281
282 if [ "$DROPINPUT" == "on" ]; then
283 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
284 fi
285 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
286 if [ "$DROPOUTPUT" == "on" ]; then
287 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_OUTPUT "
288 fi
289 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_OUTPUT"
290
291 if [ "$DROPFORWARD" == "on" ]; then
292 /sbin/iptables -A FORWARDFW -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARDFW "
293 fi
294 /sbin/iptables -A FORWARDFW -j DROP -m comment --comment "DROP_FORWARDFW"
295
296
297 ;;
298 startovpn)
299 # run openvpn
300 /usr/local/bin/openvpnctrl --create-chains-and-rules
301 ;;
302 stop)
303 iptables_init
304 # Accept everyting connected
305 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
306
307 # localhost and ethernet.
308 /sbin/iptables -A INPUT -i lo -j ACCEPT
309 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT
310
311 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
312 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
313 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
314 fi
315 if [ "$PROTOCOL" == "RFC1483" -a "$METHOD" == "DHCP" ]; then
316 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
317 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
318 fi
319
320 # run local firewall configuration, if present
321 if [ -x /etc/sysconfig/firewall.local ]; then
322 /etc/sysconfig/firewall.local stop
323 fi
324
325 if [ "$DROPINPUT" == "on" ]; then
326 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
327 fi
328 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
329 if [ "$DROPOUTPUT" == "on" ]; then
330 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_OUTPUT "
331 fi
332 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_OUTPUT"
333 if [ "$DROPFORWARD" == "on" ]; then
334 /sbin/iptables -A FORWARDFW -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARDFW "
335 fi
336 /sbin/iptables -A FORWARDFW -j DROP -m comment --comment "DROP_FORWARDFW"
337 ;;
338 stopovpn)
339 # stop openvpn
340 /usr/local/bin/openvpnctrl --delete-chains-and-rules
341 ;;
342 reload)
343 iptables_red
344
345 # run local firewall configuration, if present
346 if [ -x /etc/sysconfig/firewall.local ]; then
347 /etc/sysconfig/firewall.local reload
348 fi
349 ;;
350 restart)
351 $0 stop
352 $0 stopovpn
353 $0 start
354 $0 startovpn
355 ;;
356 *)
357 echo "Usage: $0 {start|stop|reload|restart}"
358 exit 1
359 ;;
360 esac
361
362 exit 0