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