]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/init.d/firewall
Revert "firewall: Filter logging of broadcasts from the internal networks."
[people/pmueller/ipfire-2.x.git] / src / initscripts / init.d / firewall
1 #!/bin/sh
2
3 . /etc/sysconfig/rc
4 . ${rc_functions}
5
6 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
7 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
8 eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
9 IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
10
11 if [ -f /var/ipfire/red/device ]; then
12 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
13 fi
14
15 function iptables() {
16 /sbin/iptables --wait "$@"
17 }
18
19 iptables_init() {
20 # Flush all rules and delete all custom chains
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
27
28 # Set up policies
29 iptables -P INPUT DROP
30 iptables -P FORWARD DROP
31 iptables -P OUTPUT ACCEPT
32
33 # Empty LOG_DROP and LOG_REJECT chains
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
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)
43 iptables -N PSCAN
44 if [ "$DROPPORTSCAN" == "on" ]; then
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"
49 fi
50 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
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
54 iptables -N NEWNOTSYN
55 if [ "$DROPNEWNOTSYN" == "on" ]; then
56 iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
57 fi
58 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
59
60 # Chain to contain all the rules relating to bad TCP flags
61 iptables -N BADTCP
62
63 # Don't check loopback
64 iptables -A BADTCP -i lo -j RETURN
65
66 # Disallow packets frequently used by port-scanners
67 # nmap xmas
68 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
69 # Null
70 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
71 # FIN
72 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
73 # SYN/RST (also catches xmas variants that set SYN+RST+...)
74 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
75 # SYN/FIN (QueSO or nmap OS probe)
76 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
77 # NEW TCP without SYN
78 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
79
80 iptables -A INPUT -p tcp -j BADTCP
81 iptables -A FORWARD -p tcp -j BADTCP
82
83 # Connection tracking chain
84 iptables -N CONNTRACK
85 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
86
87 # Fix for braindead ISP's
88 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
89
90 # CUSTOM chains, can be used by the users themselves
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
101
102 # Guardian (IPS) chains
103 iptables -N GUARDIAN
104 iptables -A INPUT -j GUARDIAN
105 iptables -A FORWARD -j GUARDIAN
106
107 # Block OpenVPN transfer networks
108 iptables -N OVPNBLOCK
109 iptables -A INPUT -i tun+ -j OVPNBLOCK
110 iptables -A FORWARD -i tun+ -j OVPNBLOCK
111 iptables -A FORWARD -o tun+ -j OVPNBLOCK
112
113 # OpenVPN transfer network translation
114 iptables -t nat -N OVPNNAT
115 iptables -t nat -A POSTROUTING -j OVPNNAT
116
117 # IPTV chains for IGMPPROXY
118 iptables -N IPTVINPUT
119 iptables -A INPUT -j IPTVINPUT
120 iptables -N IPTVFORWARD
121 iptables -A FORWARD -j IPTVFORWARD
122
123 # Allow to ping the firewall.
124 iptables -N ICMPINPUT
125 iptables -A INPUT -j ICMPINPUT
126 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
127
128 # Accept everything on loopback
129 iptables -N LOOPBACK
130 iptables -A LOOPBACK -i lo -j ACCEPT
131 iptables -A LOOPBACK -o lo -j ACCEPT
132
133 # Filter all packets with loopback addresses on non-loopback interfaces.
134 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
135 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
136
137 for i in INPUT FORWARD OUTPUT; do
138 iptables -A ${i} -j LOOPBACK
139 done
140
141 # Accept everything connected
142 for i in INPUT FORWARD OUTPUT; do
143 iptables -A ${i} -j CONNTRACK
144 done
145
146 # Allow DHCP
147 iptables -N DHCPINPUT
148 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
149 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
150
151 iptables -N DHCPOUTPUT
152 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
153 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
154
155 # Allow DHCP on GREEN
156 iptables -N DHCPGREENINPUT
157 iptables -N DHCPGREENOUTPUT
158 if [ -n "${GREEN_DEV}" ]; then
159 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
160 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
161 fi
162
163 # allow DHCP on BLUE to be turned on/off
164 iptables -N DHCPBLUEINPUT
165 iptables -N DHCPBLUEOUTPUT
166 if [ -n "${BLUE_DEV}" ]; then
167 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
168 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
169 fi
170
171 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
172 iptables -N IPSECINPUT
173 iptables -N IPSECFORWARD
174 iptables -N IPSECOUTPUT
175 iptables -A INPUT -j IPSECINPUT
176 iptables -A FORWARD -j IPSECFORWARD
177 iptables -A OUTPUT -j IPSECOUTPUT
178 iptables -t nat -N IPSECNAT
179 iptables -t nat -A POSTROUTING -j IPSECNAT
180
181 # localhost and ethernet.
182 # Always allow accessing the web GUI from GREEN.
183 iptables -N GUIINPUT
184 iptables -A INPUT -j GUIINPUT
185 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
186
187 # WIRELESS chains
188 iptables -N WIRELESSINPUT
189 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
190 iptables -N WIRELESSFORWARD
191 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
192
193 # OpenVPN
194 iptables -N OVPNINPUT
195 iptables -A INPUT -j OVPNINPUT
196
197 # TOR
198 iptables -N TOR_INPUT
199 iptables -A INPUT -j TOR_INPUT
200
201 # Jump into the actual firewall ruleset.
202 iptables -N INPUTFW
203 iptables -A INPUT -j INPUTFW
204
205 iptables -N OUTGOINGFW
206 iptables -A OUTPUT -j OUTGOINGFW
207
208 iptables -N FORWARDFW
209 iptables -A FORWARD -j FORWARDFW
210
211 # SNAT rules
212 iptables -t nat -N NAT_SOURCE
213 iptables -t nat -A POSTROUTING -j NAT_SOURCE
214
215 # Custom prerouting chains (for transparent proxy)
216 iptables -t nat -N SQUID
217 iptables -t nat -A PREROUTING -j SQUID
218
219 # DNAT rules
220 iptables -t nat -N NAT_DESTINATION
221 iptables -t nat -A PREROUTING -j NAT_DESTINATION
222 iptables -t nat -A OUTPUT -j NAT_DESTINATION
223
224 iptables -t mangle -N NAT_DESTINATION
225 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
226
227 iptables -t nat -N NAT_DESTINATION_FIX
228 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
229
230 iptables -t nat -A NAT_DESTINATION_FIX \
231 -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
232
233 if [ -n "${BLUE_ADDRESS}" ]; then
234 iptables -t nat -A NAT_DESTINATION_FIX \
235 -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
236 fi
237
238 if [ -n "${ORANGE_ADDRESS}" ]; then
239 iptables -t nat -A NAT_DESTINATION_FIX \
240 -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
241 fi
242
243 # upnp chain for our upnp daemon
244 iptables -t nat -N UPNPFW
245 iptables -t nat -A PREROUTING -j UPNPFW
246 iptables -N UPNPFW
247 iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
248
249 # RED chain, used for the red interface
250 iptables -N REDINPUT
251 iptables -A INPUT -j REDINPUT
252 iptables -N REDFORWARD
253 iptables -A FORWARD -j REDFORWARD
254 iptables -t nat -N REDNAT
255 iptables -t nat -A POSTROUTING -j REDNAT
256
257 # Apply OpenVPN firewall rules
258 /usr/local/bin/openvpnctrl --firewall-rules
259
260 # run wirelessctrl
261 /usr/local/bin/wirelessctrl
262
263 # POLICY CHAIN
264 iptables -N POLICYIN
265 iptables -A INPUT -j POLICYIN
266 iptables -N POLICYFWD
267 iptables -A FORWARD -j POLICYFWD
268 iptables -N POLICYOUT
269 iptables -A OUTPUT -j POLICYOUT
270
271 # Initialize firewall policies.
272 /usr/sbin/firewall-policy
273
274 # Install firewall rules for the red interface.
275 iptables_red
276 }
277
278 iptables_red() {
279 iptables -F REDINPUT
280 iptables -F REDFORWARD
281 iptables -t nat -F REDNAT
282
283 # PPPoE / PPTP Device
284 if [ "$IFACE" != "" ]; then
285 # PPPoE / PPTP
286 if [ "$DEVICE" != "" ]; then
287 iptables -A REDINPUT -i $DEVICE -j ACCEPT
288 fi
289 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
290 if [ "$RED_DEV" != "" ]; then
291 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
292 fi
293 fi
294 fi
295
296 # PPTP over DHCP
297 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
298 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
299 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
300 fi
301
302 # Orange pinholes
303 if [ "$ORANGE_DEV" != "" ]; then
304 # This rule enables a host on ORANGE network to connect to the outside
305 # (only if we have a red connection)
306 if [ "$IFACE" != "" ]; then
307 iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
308 fi
309 fi
310
311 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
312 # DHCP
313 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
314 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
315 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
316 fi
317 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
318 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
319 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
320 fi
321
322 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
323 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
324
325 if [ "$IFACE" != "$GREEN_DEV" ]; then
326 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
327 fi
328
329 fi
330
331 # Reload all rules.
332 /usr/local/bin/firewallctrl
333 }
334
335 # See how we were called.
336 case "$1" in
337 start)
338 boot_mesg "Loading firewall modules into the kernel"
339 modprobe iptable_nat || failed=1
340 for i in $(find /lib/modules/$(uname -r) -name nf_conntrack*); do
341 modprobe $(basename $i | cut -d. -f1) || failed=1
342 done
343 for i in $(find /lib/modules/$(uname -r) -name nf_nat*); do
344 modprobe $(basename $i | cut -d. -f1) || failed=1
345 done
346 (exit ${failed})
347 evaluate_retval
348
349 if [ -e /var/ipfire/main/disable_nf_sip ]; then
350 rmmod nf_nat_sip
351 rmmod nf_conntrack_sip
352 rmmod nf_nat_h323
353 rmmod nf_conntrack_h323
354 fi
355
356 boot_mesg "Setting up firewall"
357 iptables_init
358 evaluate_retval
359
360 # run local firewall configuration, if present
361 if [ -x /etc/sysconfig/firewall.local ]; then
362 /etc/sysconfig/firewall.local start
363 fi
364 ;;
365 reload)
366 boot_mesg "Reloading firewall"
367 iptables_red
368 evaluate_retval
369
370 # run local firewall configuration, if present
371 if [ -x /etc/sysconfig/firewall.local ]; then
372 /etc/sysconfig/firewall.local reload
373 fi
374 ;;
375 restart)
376 # run local firewall configuration, if present
377 if [ -x /etc/sysconfig/firewall.local ]; then
378 /etc/sysconfig/firewall.local stop
379 fi
380 $0 start
381 ;;
382 *)
383 echo "Usage: $0 {start|reload|restart}"
384 exit 1
385 ;;
386 esac
387
388 exit 0