]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/init.d/firewall
firewall: rules.pl: Don't reload custom firewall rules here.
[ipfire-2.x.git] / src / initscripts / init.d / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
0f5c5ce7
MT
3. /etc/sysconfig/rc
4. ${rc_functions}
5
3a1019f6
MT
6eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
7eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
fe0cd647 8eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
3a1019f6
MT
9IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
10
11if [ -f /var/ipfire/red/device ]; then
12 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
13fi
14
c581b670
MT
15function iptables() {
16 /sbin/iptables --wait "$@"
17}
18
3a1019f6
MT
19iptables_init() {
20 # Flush all rules and delete all custom chains
c581b670
MT
21 iptables -F
22 iptables -t nat -F
23 iptables -t mangle -F
24 iptables -X
25 iptables -t nat -X
26 iptables -t mangle -X
3a1019f6
MT
27
28 # Set up policies
c581b670
MT
29 iptables -P INPUT DROP
30 iptables -P FORWARD DROP
31 iptables -P OUTPUT ACCEPT
3a1019f6
MT
32
33 # Empty LOG_DROP and LOG_REJECT chains
c581b670
MT
34 iptables -N LOG_DROP
35 iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
36 iptables -A LOG_DROP -j DROP
37 iptables -N LOG_REJECT
38 iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
39 iptables -A LOG_REJECT -j REJECT
3a1019f6
MT
40
41 # This chain will log, then DROPs packets with certain bad combinations
42 # of flags might indicate a port-scan attempt (xmas, null, etc)
c581b670 43 iptables -N PSCAN
5595bc03 44 if [ "$DROPPORTSCAN" == "on" ]; then
c581b670
MT
45 iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
46 iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
47 iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
48 iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 49 fi
c581b670 50 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
51
52 # New tcp packets without SYN set - could well be an obscure type of port scan
53 # that's not covered above, may just be a broken windows machine
c581b670 54 iptables -N NEWNOTSYN
5595bc03 55 if [ "$DROPNEWNOTSYN" == "on" ]; then
c581b670 56 iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 57 fi
c581b670 58 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6
MT
59
60 # Chain to contain all the rules relating to bad TCP flags
c581b670 61 iptables -N BADTCP
3a1019f6 62
c581b670
MT
63 # Don't check loopback
64 iptables -A BADTCP -i lo -j RETURN
d8158ca6 65
3a1019f6
MT
66 # Disallow packets frequently used by port-scanners
67 # nmap xmas
c581b670 68 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
3a1019f6 69 # Null
c581b670 70 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
3a1019f6 71 # FIN
c581b670 72 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
3a1019f6 73 # SYN/RST (also catches xmas variants that set SYN+RST+...)
c581b670 74 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
3a1019f6 75 # SYN/FIN (QueSO or nmap OS probe)
c581b670 76 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
3a1019f6 77 # NEW TCP without SYN
c581b670 78 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
b85d2a98 79
c581b670
MT
80 iptables -A INPUT -p tcp -j BADTCP
81 iptables -A FORWARD -p tcp -j BADTCP
c0359d6d 82
b85d2a98 83 # Connection tracking chain
c581b670
MT
84 iptables -N CONNTRACK
85 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
3a1019f6 86
3a1019f6 87 # Fix for braindead ISP's
c581b670 88 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
3a1019f6
MT
89
90 # CUSTOM chains, can be used by the users themselves
c581b670
MT
91 iptables -N CUSTOMINPUT
92 iptables -A INPUT -j CUSTOMINPUT
93 iptables -N CUSTOMFORWARD
94 iptables -A FORWARD -j CUSTOMFORWARD
95 iptables -N CUSTOMOUTPUT
96 iptables -A OUTPUT -j CUSTOMOUTPUT
97 iptables -t nat -N CUSTOMPREROUTING
98 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
99 iptables -t nat -N CUSTOMPOSTROUTING
100 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
3a1019f6 101
815eaff4 102 # Guardian (IPS) chains
c581b670
MT
103 iptables -N GUARDIAN
104 iptables -A INPUT -j GUARDIAN
105 iptables -A FORWARD -j GUARDIAN
815eaff4 106
1e555330 107 # Block OpenVPN transfer networks
c581b670 108 iptables -N OVPNBLOCK
c0f99754 109 for i in INPUT FORWARD; do
c581b670 110 iptables -A ${i} -j OVPNBLOCK
1e555330
MT
111 done
112
51ab1de1 113 # OpenVPN transfer network translation
c581b670
MT
114 iptables -t nat -N OVPNNAT
115 iptables -t nat -A POSTROUTING -j OVPNNAT
51ab1de1 116
daa1ceba 117 # IPTV chains for IGMPPROXY
c581b670
MT
118 iptables -N IPTVINPUT
119 iptables -A INPUT -j IPTVINPUT
120 iptables -N IPTVFORWARD
121 iptables -A FORWARD -j IPTVFORWARD
daa1ceba 122
3a1019f6 123 # filtering from GUI
c581b670
MT
124 iptables -N GUIINPUT
125 iptables -A INPUT -j GUIINPUT
126 iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6 127
afc611d4 128 # Accept everything on loopback
c581b670
MT
129 iptables -N LOOPBACK
130 iptables -A LOOPBACK -i lo -j ACCEPT
131 iptables -A LOOPBACK -o lo -j ACCEPT
afc611d4 132
3b9a23ce 133 # Filter all packets with loopback addresses on non-loopback interfaces.
c581b670
MT
134 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
135 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
3b9a23ce
MT
136
137 for i in INPUT FORWARD OUTPUT; do
c581b670 138 iptables -A ${i} -j LOOPBACK
3b9a23ce 139 done
afc611d4 140
3a1019f6 141 # Accept everything connected
b85d2a98 142 for i in INPUT FORWARD OUTPUT; do
c581b670 143 iptables -A ${i} -j CONNTRACK
b85d2a98
MT
144 done
145
5fd30232 146 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
c581b670
MT
147 iptables -N IPSECINPUT
148 iptables -N IPSECFORWARD
149 iptables -N IPSECOUTPUT
150 iptables -A INPUT -j IPSECINPUT
151 iptables -A FORWARD -j IPSECFORWARD
152 iptables -A OUTPUT -j IPSECOUTPUT
153 iptables -t nat -N IPSECNAT
154 iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 155
3a1019f6 156 # localhost and ethernet.
c581b670 157 iptables -A INPUT -i $GREEN_DEV -m conntrack --ctstate NEW -j ACCEPT ! -p icmp
218b3341 158
3a1019f6 159 # allow DHCP on BLUE to be turned on/off
c581b670
MT
160 iptables -N DHCPBLUEINPUT
161 iptables -A INPUT -j DHCPBLUEINPUT
81393987
AM
162
163 # WIRELESS chains
c581b670
MT
164 iptables -N WIRELESSINPUT
165 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
166 iptables -N WIRELESSFORWARD
167 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
987b75bc 168
ab4876ad 169 # OpenVPN
c581b670
MT
170 iptables -N OVPNINPUT
171 iptables -A INPUT -j OVPNINPUT
ab4876ad 172
987b75bc 173 # TOR
c581b670
MT
174 iptables -N TOR_INPUT
175 iptables -A INPUT -j TOR_INPUT
12dcfbbd 176
d5f1422d 177 # Jump into the actual firewall ruleset.
c581b670
MT
178 iptables -N INPUTFW
179 iptables -A INPUT -j INPUTFW
d5f1422d 180
c581b670
MT
181 iptables -N OUTGOINGFW
182 iptables -A OUTPUT -j OUTGOINGFW
d5f1422d 183
c581b670
MT
184 iptables -N FORWARDFW
185 iptables -A FORWARD -j FORWARDFW
d5f1422d 186
fac38614 187 # SNAT rules
c581b670
MT
188 iptables -t nat -N NAT_SOURCE
189 iptables -t nat -A POSTROUTING -j NAT_SOURCE
fac38614 190
3a1019f6 191 # RED chain, used for the red interface
c581b670
MT
192 iptables -N REDINPUT
193 iptables -A INPUT -j REDINPUT
194 iptables -N REDFORWARD
195 iptables -A FORWARD -j REDFORWARD
196 iptables -t nat -N REDNAT
197 iptables -t nat -A POSTROUTING -j REDNAT
3a1019f6
MT
198
199 iptables_red
bb12dd7b
MT
200
201 # Custom prerouting chains (for transparent proxy)
c581b670
MT
202 iptables -t nat -N SQUID
203 iptables -t nat -A PREROUTING -j SQUID
bb12dd7b
MT
204
205 # DNAT rules
c581b670
MT
206 iptables -t nat -N NAT_DESTINATION
207 iptables -t nat -A PREROUTING -j NAT_DESTINATION
bb12dd7b 208
7e7495b3 209 # upnp chain for our upnp daemon
c581b670
MT
210 iptables -t nat -N UPNPFW
211 iptables -t nat -A PREROUTING -j UPNPFW
212 iptables -N UPNPFW
213 iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
3a1019f6 214
ab4876ad
MT
215 # Apply OpenVPN firewall rules
216 /usr/local/bin/openvpnctrl --firewall-rules
ff4770c7
AM
217
218 # run wirelessctrl
219 /usr/local/bin/wirelessctrl
220
c581b670
MT
221 # POLICY CHAIN
222 iptables -N POLICYIN
223 iptables -A INPUT -j POLICYIN
224 iptables -N POLICYFWD
225 iptables -A FORWARD -j POLICYFWD
226 iptables -N POLICYOUT
227 iptables -A OUTPUT -j POLICYOUT
b324de14 228
5d7faa45 229 /usr/sbin/firewall-policy
690b0bd7 230
ff4770c7 231 # read new firewall
8039a710 232 /usr/local/bin/firewallctrl
ff4770c7 233}
3a1019f6 234
ff4770c7 235iptables_red() {
c581b670
MT
236 iptables -F REDINPUT
237 iptables -F REDFORWARD
238 iptables -t nat -F REDNAT
3a1019f6 239
ff4770c7
AM
240 # PPPoE / PPTP Device
241 if [ "$IFACE" != "" ]; then
242 # PPPoE / PPTP
243 if [ "$DEVICE" != "" ]; then
c581b670 244 iptables -A REDINPUT -i $DEVICE -j ACCEPT
ff4770c7
AM
245 fi
246 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
247 if [ "$RED_DEV" != "" ]; then
c581b670 248 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
ff4770c7
AM
249 fi
250 fi
3a1019f6 251 fi
ff4770c7
AM
252
253 # PPTP over DHCP
254 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
c581b670
MT
255 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
256 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
3a1019f6
MT
257 fi
258
ff4770c7
AM
259 # Orange pinholes
260 if [ "$ORANGE_DEV" != "" ]; then
261 # This rule enables a host on ORANGE network to connect to the outside
262 # (only if we have a red connection)
263 if [ "$IFACE" != "" ]; then
c581b670 264 iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
ff4770c7 265 fi
3a1019f6 266 fi
c400fe4c 267
ff4770c7
AM
268 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
269 # DHCP
270 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
c581b670
MT
271 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
272 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
273 fi
274 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
c581b670
MT
275 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
276 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
277 fi
278
279 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
c581b670
MT
280 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
281 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
c400fe4c 282
6be0579b 283 fi
66f6b279
MT
284
285 # Reload all rules.
55a5bcae 286 /usr/local/bin/firewallctrl
ff4770c7
AM
287}
288
289# See how we were called.
290case "$1" in
291 start)
cdb725da
MT
292 boot_mesg "Loading firewall modules into the kernel"
293 modprobe iptable_nat || failed=1
294 for i in $(find /lib/modules/$(uname -r) -name nf_conntrack*); do
295 modprobe $(basename $i | cut -d. -f1) || failed=1
296 done
297 for i in $(find /lib/modules/$(uname -r) -name nf_nat*); do
298 modprobe $(basename $i | cut -d. -f1) || failed=1
299 done
300 (exit ${failed})
301 evaluate_retval
302
303 if [ -e /var/ipfire/main/disable_nf_sip ]; then
304 rmmod nf_nat_sip
305 rmmod nf_conntrack_sip
306 rmmod nf_nat_h323
307 rmmod nf_conntrack_h323
308 fi
309
7d7740a4 310 boot_mesg "Setting up firewall"
ff4770c7 311 iptables_init
7d7740a4
MT
312 evaluate_retval
313
159c55c5
MT
314 # run local firewall configuration, if present
315 if [ -x /etc/sysconfig/firewall.local ]; then
316 /etc/sysconfig/firewall.local start
317 fi
6be0579b 318 ;;
3a1019f6 319 reload)
7d7740a4 320 boot_mesg "Reloading firewall"
3a1019f6 321 iptables_red
7d7740a4
MT
322 evaluate_retval
323
3a1019f6 324 # run local firewall configuration, if present
ff4770c7 325 if [ -x /etc/sysconfig/firewall.local ]; then
3a1019f6
MT
326 /etc/sysconfig/firewall.local reload
327 fi
328 ;;
329 restart)
ff4770c7
AM
330 # run local firewall configuration, if present
331 if [ -x /etc/sysconfig/firewall.local ]; then
332 /etc/sysconfig/firewall.local stop
333 fi
3a1019f6
MT
334 $0 start
335 ;;
336 *)
ff4770c7 337 echo "Usage: $0 {start|reload|restart}"
3a1019f6
MT
338 exit 1
339 ;;
340esac
341
342exit 0