]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/init.d/firewall
Merge remote-tracking branch 'ms/iptables-conntrack' into next
[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 -t raw -F
25 iptables -X
26 iptables -t nat -X
27 iptables -t mangle -X
28 iptables -t raw -X
29
30 # Set up policies
31 iptables -P INPUT DROP
32 iptables -P FORWARD DROP
33 iptables -P OUTPUT ACCEPT
34
35 # Empty LOG_DROP and LOG_REJECT chains
36 iptables -N LOG_DROP
37 iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
38 iptables -A LOG_DROP -j DROP
39 iptables -N LOG_REJECT
40 iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
41 iptables -A LOG_REJECT -j REJECT
42
43 # This chain will log, then DROPs packets with certain bad combinations
44 # of flags might indicate a port-scan attempt (xmas, null, etc)
45 iptables -N PSCAN
46 if [ "$DROPPORTSCAN" == "on" ]; then
47 iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
48 iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
49 iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
50 iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
51 fi
52 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
53
54 # New tcp packets without SYN set - could well be an obscure type of port scan
55 # that's not covered above, may just be a broken windows machine
56 iptables -N NEWNOTSYN
57 if [ "$DROPNEWNOTSYN" == "on" ]; then
58 iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
59 fi
60 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
61
62 # Chain to contain all the rules relating to bad TCP flags
63 iptables -N BADTCP
64
65 # Don't check loopback
66 iptables -A BADTCP -i lo -j RETURN
67
68 # Disallow packets frequently used by port-scanners
69 # NMAP FIN/URG/PSH (XMAS scan)
70 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
71 # SYN/RST/ACK/FIN/URG
72 iptables -A BADTCP -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PSCAN
73 # ALL/ALL
74 iptables -A BADTCP -p tcp --tcp-flags ALL ALL -j PSCAN
75 # FIN Stealth
76 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
77 # SYN/RST (also catches xmas variants that set SYN+RST+...)
78 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
79 # SYN/FIN (QueSO or nmap OS probe)
80 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
81 # Null
82 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
83 # NEW TCP without SYN
84 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
85
86 iptables -A INPUT -p tcp -j BADTCP
87 iptables -A FORWARD -p tcp -j BADTCP
88
89 # Connection tracking chains
90 iptables -N CONNTRACK
91 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED -j ACCEPT
92 iptables -A CONNTRACK -m conntrack --ctstate INVALID -j DROP
93 iptables -A CONNTRACK -p icmp -m conntrack --ctstate RELATED -j ACCEPT
94 iptables -t raw -N CONNTRACK
95 iptables -t raw -A PREROUTING -j CONNTRACK
96
97 # Conntrack helpers (https://home.regit.org/netfilter-en/secure-use-of-helpers/)
98
99 # SIP
100 if [ "${CONNTRACK_SIP}" = "on" ]; then
101 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
102 -m helper --helper sip -j ACCEPT
103 for proto in udp tcp; do
104 iptables -t raw -A CONNTRACK -p "${proto}" --dport 5060 -j CT --helper sip
105 done
106 fi
107
108 # H.323
109 if [ "${CONNTRACK_H323}" = "on" ]; then
110 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
111 -m helper --helper h323 -j ACCEPT
112
113 # Gatekeeper RAS
114 iptables -t raw -A CONNTRACK -p udp --dport 1719 -j CT --helper RAS
115
116 # Q.931
117 iptables -t raw -A CONNTRACK -p tcp --dport 1720 -j CT --helper Q.931
118 fi
119
120 # FTP
121 if [ "${CONNTRACK_FTP}" = "on" ]; then
122 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
123 -m helper --helper ftp -p tcp --dport 1024: -j ACCEPT
124 iptables -t raw -A CONNTRACK -p tcp --dport 21 -j CT --helper ftp
125 fi
126
127 # PPTP
128 if [ "${CONNTRACK_PPTP}" = "on" ]; then
129 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
130 -m helper --helper pptp -j ACCEPT
131 iptables -t raw -A CONNTRACK -p udp --dport 1723 -j CT --helper pptp
132 fi
133
134 # TFTP
135 if [ "${CONNTRACK_TFTP}" = "on" ]; then
136 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
137 -m helper --helper tftp -j ACCEPT
138 iptables -t raw -A CONNTRACK -p udp --dport 69 -j CT --helper tftp
139 fi
140
141 # IRC
142 if [ "${CONNTRACK_IRC}" = "on" ]; then
143 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
144 -m helper --helper irc -j ACCEPT
145 iptables -t raw -A CONNTRACK -p tcp --dport 6667 -j CT --helper irc
146 fi
147
148 # Amanda
149 if [ "${CONNTRACK_AMANDA}" = "on" ]; then
150 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
151 -m helper --helper amanda -j ACCEPT
152 iptables -t raw -A CONNTRACK -p tcp -j CT --helper amanda
153 fi
154
155 # Fix for braindead ISP's
156 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
157
158 # CUSTOM chains, can be used by the users themselves
159 iptables -N CUSTOMINPUT
160 iptables -A INPUT -j CUSTOMINPUT
161 iptables -N CUSTOMFORWARD
162 iptables -A FORWARD -j CUSTOMFORWARD
163 iptables -N CUSTOMOUTPUT
164 iptables -A OUTPUT -j CUSTOMOUTPUT
165 iptables -t nat -N CUSTOMPREROUTING
166 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
167 iptables -t nat -N CUSTOMPOSTROUTING
168 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
169
170 # P2PBLOCK
171 iptables -N P2PBLOCK
172 iptables -A INPUT -j P2PBLOCK
173 iptables -A FORWARD -j P2PBLOCK
174 iptables -A OUTPUT -j P2PBLOCK
175
176 # Guardian (IPS) chains
177 iptables -N GUARDIAN
178 iptables -A INPUT -j GUARDIAN
179 iptables -A FORWARD -j GUARDIAN
180
181 # Block non-established IPsec networks
182 iptables -N IPSECBLOCK
183 iptables -A FORWARD -m policy --dir out --pol none -j IPSECBLOCK
184 iptables -A OUTPUT -m policy --dir out --pol none -j IPSECBLOCK
185
186 # Block OpenVPN transfer networks
187 iptables -N OVPNBLOCK
188 iptables -A INPUT -i tun+ -j OVPNBLOCK
189 iptables -A FORWARD -i tun+ -j OVPNBLOCK
190 iptables -A FORWARD -o tun+ -j OVPNBLOCK
191
192 # OpenVPN transfer network translation
193 iptables -t nat -N OVPNNAT
194 iptables -t nat -A POSTROUTING -j OVPNNAT
195
196 # IPTV chains for IGMPPROXY
197 iptables -N IPTVINPUT
198 iptables -A INPUT -j IPTVINPUT
199 iptables -N IPTVFORWARD
200 iptables -A FORWARD -j IPTVFORWARD
201
202 # Allow to ping the firewall.
203 iptables -N ICMPINPUT
204 iptables -A INPUT -j ICMPINPUT
205 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
206
207 # Accept everything on loopback
208 iptables -N LOOPBACK
209 iptables -A LOOPBACK -i lo -j ACCEPT
210 iptables -A LOOPBACK -o lo -j ACCEPT
211
212 # Filter all packets with loopback addresses on non-loopback interfaces.
213 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
214 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
215
216 for i in INPUT FORWARD OUTPUT; do
217 iptables -A ${i} -j LOOPBACK
218 done
219
220 # Accept everything connected
221 for i in INPUT FORWARD OUTPUT; do
222 iptables -A ${i} -j CONNTRACK
223 done
224
225 # Allow DHCP
226 iptables -N DHCPINPUT
227 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
228 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
229
230 iptables -N DHCPOUTPUT
231 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
232 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
233
234 # Allow DHCP on GREEN
235 iptables -N DHCPGREENINPUT
236 iptables -N DHCPGREENOUTPUT
237 if [ -n "${GREEN_DEV}" ]; then
238 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
239 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
240 fi
241
242 # allow DHCP on BLUE to be turned on/off
243 iptables -N DHCPBLUEINPUT
244 iptables -N DHCPBLUEOUTPUT
245 if [ -n "${BLUE_DEV}" ]; then
246 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
247 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
248 fi
249
250 # GeoIP block
251 iptables -N GEOIPBLOCK
252 iptables -A INPUT -j GEOIPBLOCK
253 iptables -A FORWARD -j GEOIPBLOCK
254
255 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
256 iptables -N IPSECINPUT
257 iptables -N IPSECFORWARD
258 iptables -N IPSECOUTPUT
259 iptables -A INPUT -j IPSECINPUT
260 iptables -A FORWARD -j IPSECFORWARD
261 iptables -A OUTPUT -j IPSECOUTPUT
262 iptables -t nat -N IPSECNAT
263 iptables -t nat -A POSTROUTING -j IPSECNAT
264
265 # localhost and ethernet.
266 # Always allow accessing the web GUI from GREEN.
267 iptables -N GUIINPUT
268 iptables -A INPUT -j GUIINPUT
269 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
270
271 # WIRELESS chains
272 iptables -N WIRELESSINPUT
273 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
274 iptables -N WIRELESSFORWARD
275 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
276
277 # OpenVPN
278 iptables -N OVPNINPUT
279 iptables -A INPUT -j OVPNINPUT
280
281 # TOR
282 iptables -N TOR_INPUT
283 iptables -A INPUT -j TOR_INPUT
284
285 # Jump into the actual firewall ruleset.
286 iptables -N INPUTFW
287 iptables -A INPUT -j INPUTFW
288
289 iptables -N OUTGOINGFW
290 iptables -A OUTPUT -j OUTGOINGFW
291
292 iptables -N FORWARDFW
293 iptables -A FORWARD -j FORWARDFW
294
295 # SNAT rules
296 iptables -t nat -N NAT_SOURCE
297 iptables -t nat -A POSTROUTING -j NAT_SOURCE
298
299 # Custom prerouting chains (for transparent proxy)
300 iptables -t nat -N SQUID
301 iptables -t nat -A PREROUTING -j SQUID
302
303 # DNAT rules
304 iptables -t nat -N NAT_DESTINATION
305 iptables -t nat -A PREROUTING -j NAT_DESTINATION
306 iptables -t nat -A OUTPUT -j NAT_DESTINATION
307
308 iptables -t mangle -N NAT_DESTINATION
309 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
310
311 iptables -t nat -N NAT_DESTINATION_FIX
312 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
313
314 iptables -t nat -A NAT_DESTINATION_FIX \
315 -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
316
317 if [ -n "${BLUE_ADDRESS}" ]; then
318 iptables -t nat -A NAT_DESTINATION_FIX \
319 -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
320 fi
321
322 if [ -n "${ORANGE_ADDRESS}" ]; then
323 iptables -t nat -A NAT_DESTINATION_FIX \
324 -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
325 fi
326
327 # upnp chain for our upnp daemon
328 iptables -t nat -N UPNPFW
329 iptables -t nat -A PREROUTING -j UPNPFW
330 iptables -N UPNPFW
331 iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
332
333 # RED chain, used for the red interface
334 iptables -N REDINPUT
335 iptables -A INPUT -j REDINPUT
336 iptables -N REDFORWARD
337 iptables -A FORWARD -j REDFORWARD
338 iptables -t nat -N REDNAT
339 iptables -t nat -A POSTROUTING -j REDNAT
340
341 # Populate IPsec block chain
342 /usr/lib/firewall/ipsec-block
343
344 # Apply OpenVPN firewall rules
345 /usr/local/bin/openvpnctrl --firewall-rules
346
347 # run wirelessctrl
348 /usr/local/bin/wirelessctrl
349
350 # POLICY CHAIN
351 iptables -N POLICYIN
352 iptables -A INPUT -j POLICYIN
353 iptables -N POLICYFWD
354 iptables -A FORWARD -j POLICYFWD
355 iptables -N POLICYOUT
356 iptables -A OUTPUT -j POLICYOUT
357
358 # Initialize firewall policies.
359 /usr/sbin/firewall-policy
360
361 # Install firewall rules for the red interface.
362 iptables_red_up
363
364 # If red has not been brought up yet, we will
365 # add the blocking rules for MASQUERADE
366 if [ ! -e "/var/ipfire/red/active" ]; then
367 iptables_red_down
368 fi
369 }
370
371 iptables_red_up() {
372 iptables -F REDINPUT
373 iptables -F REDFORWARD
374 iptables -t nat -F REDNAT
375
376 # PPPoE / PPTP Device
377 if [ "$IFACE" != "" ]; then
378 # PPPoE / PPTP
379 if [ "$DEVICE" != "" ]; then
380 iptables -A REDINPUT -i $DEVICE -j ACCEPT
381 fi
382 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
383 if [ "$RED_DEV" != "" ]; then
384 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
385 fi
386 fi
387 fi
388
389 # PPTP over DHCP
390 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
391 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
392 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
393 fi
394
395 # Orange pinholes
396 if [ "$ORANGE_DEV" != "" ]; then
397 # This rule enables a host on ORANGE network to connect to the outside
398 # (only if we have a red connection)
399 if [ "$IFACE" != "" ]; then
400 iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
401 fi
402 fi
403
404 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
405 # DHCP
406 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
407 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
408 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
409 fi
410 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
411 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
412 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
413 fi
414
415 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
416 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
417
418 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
419 iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
420 fi
421
422 local NO_MASQ_NETWORKS
423
424 if [ "${MASQUERADE_GREEN}" = "off" ]; then
425 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
426 fi
427
428 if [ "${MASQUERADE_BLUE}" = "off" ]; then
429 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
430 fi
431
432 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
433 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
434 fi
435
436 local network
437 for network in ${NO_MASQ_NETWORKS}; do
438 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
439 done
440
441 # Masquerade everything else
442 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
443 fi
444
445 # Reload all rules.
446 /usr/local/bin/firewallctrl
447 }
448
449 iptables_red_down() {
450 # Prohibit packets to reach the masquerading rule
451 # while the wan interface is down - this is required to
452 # circumvent udp related NAT issues
453 # http://forum.ipfire.org/index.php?topic=11127.0
454 if [ -n "${IFACE}" ]; then
455 iptables -F REDFORWARD
456 iptables -A REDFORWARD -o "${IFACE}" -j DROP
457 fi
458
459 # Reload all rules.
460 /usr/local/bin/firewallctrl
461 }
462
463 # See how we were called.
464 case "$1" in
465 start)
466 boot_mesg "Setting up firewall"
467 iptables_init
468 evaluate_retval
469 ;;
470 reload|up)
471 boot_mesg "Reloading firewall"
472 iptables_red_up
473 evaluate_retval
474 ;;
475 down)
476 boot_mesg "Disabling firewall access to RED"
477 iptables_red_down
478 evaluate_retval
479 ;;
480 restart)
481 $0 start
482 ;;
483 *)
484 echo "Usage: $0 {start|reload|restart}"
485 exit 1
486 ;;
487 esac
488
489 exit 0