iptables: Create LOOPBACK chain.
[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 conntrack --ctstate NEW -j NEWNOTSYN
72
73 /sbin/iptables -A INPUT -p tcp -j BADTCP
74 /sbin/iptables -A FORWARD -p tcp -j BADTCP
75
76 # Connection tracking chain
77 /sbin/iptables -N CONNTRACK
78 /sbin/iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
79
80 # Fix for braindead ISP's
81 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
82
83 # CUSTOM chains, can be used by the users themselves
84 /sbin/iptables -N CUSTOMINPUT
85 /sbin/iptables -A INPUT -j CUSTOMINPUT
86 /sbin/iptables -N GUARDIAN
87 /sbin/iptables -A INPUT -j GUARDIAN
88 /sbin/iptables -N OVPNBLOCK
89 /sbin/iptables -A FORWARD -j OVPNBLOCK
90 /sbin/iptables -A FORWARD -j GUARDIAN
91 /sbin/iptables -N CUSTOMFORWARD
92 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
93 /sbin/iptables -N CUSTOMOUTPUT
94 /sbin/iptables -A OUTPUT -j OVPNBLOCK
95 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
96 /sbin/iptables -N OUTGOINGFW
97 /sbin/iptables -A OUTPUT -j OUTGOINGFW
98 /sbin/iptables -t nat -N CUSTOMPREROUTING
99 /sbin/iptables -t nat -N OVPNNAT
100 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
101 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
102 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
103 /sbin/iptables -t nat -A POSTROUTING -j OVPNNAT
104
105 # IPTV chains for IGMPPROXY
106 /sbin/iptables -N IPTVINPUT
107 /sbin/iptables -A INPUT -j IPTVINPUT
108 /sbin/iptables -N IPTVFORWARD
109 /sbin/iptables -A FORWARD -j IPTVFORWARD
110
111 # Filtering ovpn networks INPUT
112 /sbin/iptables -A INPUT -j OVPNBLOCK
113
114 # filtering from GUI
115 /sbin/iptables -N GUIINPUT
116 /sbin/iptables -A INPUT -j GUIINPUT
117 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
118
119 # Accept everything on loopback
120 /sbin/iptables -N LOOPBACK
121 /sbin/iptables -A LOOPBACK -i lo -j ACCEPT
122 /sbin/iptables -A LOOPBACK -o lo -j ACCEPT
123
124 /sbin/iptables -A INPUT -j LOOPBACK
125 /sbin/iptables -A OUTPUT -j LOOPBACK
126
127 # Accept everything connected
128 for i in INPUT FORWARD OUTPUT; do
129 /sbin/iptables -A ${i} -j CONNTRACK
130 done
131
132 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
133 /sbin/iptables -N IPSECINPUT
134 /sbin/iptables -N IPSECFORWARD
135 /sbin/iptables -N IPSECOUTPUT
136 /sbin/iptables -N OPENSSLVIRTUAL
137 /sbin/iptables -A INPUT -j IPSECINPUT
138 /sbin/iptables -A INPUT -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL INPUT"
139 /sbin/iptables -A FORWARD -j IPSECFORWARD
140 /sbin/iptables -A FORWARD -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL FORWARD"
141 /sbin/iptables -A OUTPUT -j IPSECOUTPUT
142 /sbin/iptables -t nat -N IPSECNAT
143 /sbin/iptables -t nat -A POSTROUTING -j IPSECNAT
144
145 # Input Firewall
146 /sbin/iptables -N INPUTFW
147 /sbin/iptables -A INPUT -m conntrack --ctstate NEW -j INPUTFW
148
149 # localhost and ethernet.
150 /sbin/iptables -A INPUT -i lo -m conntrack --ctstate NEW -j ACCEPT
151 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP # Loopback not on lo
152 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP
153 /sbin/iptables -A FORWARD -i lo -m conntrack --ctstate NEW -j ACCEPT
154 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP
155 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m conntrack --ctstate NEW -j DROP
156 /sbin/iptables -A INPUT -i $GREEN_DEV -m conntrack --ctstate NEW -j ACCEPT ! -p icmp
157
158 # allow DHCP on BLUE to be turned on/off
159 /sbin/iptables -N DHCPBLUEINPUT
160 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
161
162 # WIRELESS chains
163 /sbin/iptables -N WIRELESSINPUT
164 /sbin/iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
165 /sbin/iptables -N WIRELESSFORWARD
166 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
167
168 # Forward Firewall
169 /sbin/iptables -N FORWARDFW
170 /sbin/iptables -A FORWARD -j FORWARDFW
171
172 # OPenSSL
173 /sbin/iptables -N OPENSSLPHYSICAL
174 /sbin/iptables -A INPUT -j OPENSSLPHYSICAL
175
176 # RED chain, used for the red interface
177 /sbin/iptables -N REDINPUT
178 /sbin/iptables -A INPUT -j REDINPUT
179 /sbin/iptables -N REDFORWARD
180 /sbin/iptables -A FORWARD -j REDFORWARD
181 /sbin/iptables -t nat -N REDNAT
182 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
183
184 iptables_red
185
186 # Custom prerouting chains (for transparent proxy and port forwarding)
187 /sbin/iptables -t nat -N SQUID
188 /sbin/iptables -t nat -A PREROUTING -j SQUID
189 /sbin/iptables -t nat -N NAT_DESTINATION
190 /sbin/iptables -t nat -N NAT_SOURCE
191 /sbin/iptables -t nat -A PREROUTING -j NAT_DESTINATION
192 /sbin/iptables -t nat -I POSTROUTING 3 -j NAT_SOURCE
193
194
195
196 # upnp chain for our upnp daemon
197 /sbin/iptables -t nat -N UPNPFW
198 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
199 /sbin/iptables -N UPNPFW
200 /sbin/iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
201
202 # Postrouting rules (for port forwarding)
203 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT --to-source $GREEN_ADDRESS
204 if [ "$BLUE_DEV" != "" ]; then
205 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
206 fi
207 if [ "$ORANGE_DEV" != "" ]; then
208 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
209 fi
210
211 # run local firewall configuration, if present
212 if [ -x /etc/sysconfig/firewall.local ]; then
213 /etc/sysconfig/firewall.local start
214 fi
215
216 # run openvpn
217 /usr/local/bin/openvpnctrl --create-chains-and-rules
218
219 # run wirelessctrl
220 /usr/local/bin/wirelessctrl
221
222 #POLICY CHAIN
223 /sbin/iptables -N POLICYIN
224 /sbin/iptables -A INPUT -j POLICYIN
225 /sbin/iptables -N POLICYFWD
226 /sbin/iptables -A FORWARD -j POLICYFWD
227 /sbin/iptables -N POLICYOUT
228 /sbin/iptables -A OUTPUT -j POLICYOUT
229
230 /usr/sbin/firewall-policy
231
232 # read new firewall
233 /usr/local/bin/forwardfwctrl
234
235 if [ "$DROPINPUT" == "on" ]; then
236 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT"
237 fi
238 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
239 if [ "$DROPFORWARD" == "on" ]; then
240 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_FORWARD"
241 fi
242 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_FORWARD"
243 }
244
245 iptables_red() {
246 /sbin/iptables -F REDINPUT
247 /sbin/iptables -F REDFORWARD
248 /sbin/iptables -t nat -F REDNAT
249
250 # PPPoE / PPTP Device
251 if [ "$IFACE" != "" ]; then
252 # PPPoE / PPTP
253 if [ "$DEVICE" != "" ]; then
254 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
255 fi
256 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
257 if [ "$RED_DEV" != "" ]; then
258 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
259 fi
260 fi
261 fi
262
263 # PPTP over DHCP
264 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
265 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
266 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
267 fi
268
269 # Orange pinholes
270 if [ "$ORANGE_DEV" != "" ]; then
271 # This rule enables a host on ORANGE network to connect to the outside
272 # (only if we have a red connection)
273 if [ "$IFACE" != "" ]; then
274 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
275 fi
276 fi
277
278 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
279 # DHCP
280 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
281 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
282 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
283 fi
284 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
285 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
286 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
287 fi
288
289 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
290 /sbin/iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
291 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
292
293 fi
294 }
295
296 # See how we were called.
297 case "$1" in
298 start)
299 iptables_init
300 ;;
301 reload)
302 iptables_red
303 # run local firewall configuration, if present
304 if [ -x /etc/sysconfig/firewall.local ]; then
305 /etc/sysconfig/firewall.local reload
306 fi
307 ;;
308 restart)
309 # run local firewall configuration, if present
310 if [ -x /etc/sysconfig/firewall.local ]; then
311 /etc/sysconfig/firewall.local stop
312 fi
313 $0 start
314 ;;
315 *)
316 echo "Usage: $0 {start|reload|restart}"
317 exit 1
318 ;;
319 esac
320
321 exit 0