]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/init.d/firewall
firewall: Make conntrack helpers configurable
[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 # FTP
109 if [ "${CONNTRACK_FTP}" = "on" ]; then
110 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
111 -m helper --helper ftp -p tcp --dport 1024: -j ACCEPT
112 iptables -t raw -A CONNTRACK -p tcp --dport 21 -j CT --helper ftp
113 fi
114
115 # PPTP
116 if [ "${CONNTRACK_PPTP}" = "on" ]; then
117 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
118 -m helper --helper pptp -j ACCEPT
119 iptables -t raw -A CONNTRACK -p udp --dport 1723 -j CT --helper pptp
120 fi
121
122 # TFTP
123 if [ "${CONNTRACK_TFTP}" = "on" ]; then
124 iptables -A CONNTRACK -m conntrack --ctstate RELATED \
125 -m helper --helper tftp -j ACCEPT
126 iptables -t raw -A CONNTRACK -p udp --dport 69 -j CT --helper tftp
127 fi
128
129 # Fix for braindead ISP's
130 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
131
132 # CUSTOM chains, can be used by the users themselves
133 iptables -N CUSTOMINPUT
134 iptables -A INPUT -j CUSTOMINPUT
135 iptables -N CUSTOMFORWARD
136 iptables -A FORWARD -j CUSTOMFORWARD
137 iptables -N CUSTOMOUTPUT
138 iptables -A OUTPUT -j CUSTOMOUTPUT
139 iptables -t nat -N CUSTOMPREROUTING
140 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
141 iptables -t nat -N CUSTOMPOSTROUTING
142 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
143
144 # P2PBLOCK
145 iptables -N P2PBLOCK
146 iptables -A INPUT -j P2PBLOCK
147 iptables -A FORWARD -j P2PBLOCK
148 iptables -A OUTPUT -j P2PBLOCK
149
150 # Guardian (IPS) chains
151 iptables -N GUARDIAN
152 iptables -A INPUT -j GUARDIAN
153 iptables -A FORWARD -j GUARDIAN
154
155 # Block OpenVPN transfer networks
156 iptables -N OVPNBLOCK
157 iptables -A INPUT -i tun+ -j OVPNBLOCK
158 iptables -A FORWARD -i tun+ -j OVPNBLOCK
159 iptables -A FORWARD -o tun+ -j OVPNBLOCK
160
161 # OpenVPN transfer network translation
162 iptables -t nat -N OVPNNAT
163 iptables -t nat -A POSTROUTING -j OVPNNAT
164
165 # IPTV chains for IGMPPROXY
166 iptables -N IPTVINPUT
167 iptables -A INPUT -j IPTVINPUT
168 iptables -N IPTVFORWARD
169 iptables -A FORWARD -j IPTVFORWARD
170
171 # Allow to ping the firewall.
172 iptables -N ICMPINPUT
173 iptables -A INPUT -j ICMPINPUT
174 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
175
176 # Accept everything on loopback
177 iptables -N LOOPBACK
178 iptables -A LOOPBACK -i lo -j ACCEPT
179 iptables -A LOOPBACK -o lo -j ACCEPT
180
181 # Filter all packets with loopback addresses on non-loopback interfaces.
182 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
183 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
184
185 for i in INPUT FORWARD OUTPUT; do
186 iptables -A ${i} -j LOOPBACK
187 done
188
189 # Accept everything connected
190 for i in INPUT FORWARD OUTPUT; do
191 iptables -A ${i} -j CONNTRACK
192 done
193
194 # Allow DHCP
195 iptables -N DHCPINPUT
196 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
197 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
198
199 iptables -N DHCPOUTPUT
200 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
201 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
202
203 # Allow DHCP on GREEN
204 iptables -N DHCPGREENINPUT
205 iptables -N DHCPGREENOUTPUT
206 if [ -n "${GREEN_DEV}" ]; then
207 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
208 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
209 fi
210
211 # allow DHCP on BLUE to be turned on/off
212 iptables -N DHCPBLUEINPUT
213 iptables -N DHCPBLUEOUTPUT
214 if [ -n "${BLUE_DEV}" ]; then
215 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
216 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
217 fi
218
219 # GeoIP block
220 iptables -N GEOIPBLOCK
221 iptables -A INPUT -j GEOIPBLOCK
222 iptables -A FORWARD -j GEOIPBLOCK
223
224 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
225 iptables -N IPSECINPUT
226 iptables -N IPSECFORWARD
227 iptables -N IPSECOUTPUT
228 iptables -A INPUT -j IPSECINPUT
229 iptables -A FORWARD -j IPSECFORWARD
230 iptables -A OUTPUT -j IPSECOUTPUT
231 iptables -t nat -N IPSECNAT
232 iptables -t nat -A POSTROUTING -j IPSECNAT
233
234 # localhost and ethernet.
235 # Always allow accessing the web GUI from GREEN.
236 iptables -N GUIINPUT
237 iptables -A INPUT -j GUIINPUT
238 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
239
240 # WIRELESS chains
241 iptables -N WIRELESSINPUT
242 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
243 iptables -N WIRELESSFORWARD
244 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
245
246 # OpenVPN
247 iptables -N OVPNINPUT
248 iptables -A INPUT -j OVPNINPUT
249
250 # TOR
251 iptables -N TOR_INPUT
252 iptables -A INPUT -j TOR_INPUT
253
254 # Jump into the actual firewall ruleset.
255 iptables -N INPUTFW
256 iptables -A INPUT -j INPUTFW
257
258 iptables -N OUTGOINGFW
259 iptables -A OUTPUT -j OUTGOINGFW
260
261 iptables -N FORWARDFW
262 iptables -A FORWARD -j FORWARDFW
263
264 # SNAT rules
265 iptables -t nat -N NAT_SOURCE
266 iptables -t nat -A POSTROUTING -j NAT_SOURCE
267
268 # Custom prerouting chains (for transparent proxy)
269 iptables -t nat -N SQUID
270 iptables -t nat -A PREROUTING -j SQUID
271
272 # DNAT rules
273 iptables -t nat -N NAT_DESTINATION
274 iptables -t nat -A PREROUTING -j NAT_DESTINATION
275 iptables -t nat -A OUTPUT -j NAT_DESTINATION
276
277 iptables -t mangle -N NAT_DESTINATION
278 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
279
280 iptables -t nat -N NAT_DESTINATION_FIX
281 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
282
283 iptables -t nat -A NAT_DESTINATION_FIX \
284 -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
285
286 if [ -n "${BLUE_ADDRESS}" ]; then
287 iptables -t nat -A NAT_DESTINATION_FIX \
288 -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
289 fi
290
291 if [ -n "${ORANGE_ADDRESS}" ]; then
292 iptables -t nat -A NAT_DESTINATION_FIX \
293 -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
294 fi
295
296 # upnp chain for our upnp daemon
297 iptables -t nat -N UPNPFW
298 iptables -t nat -A PREROUTING -j UPNPFW
299 iptables -N UPNPFW
300 iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
301
302 # RED chain, used for the red interface
303 iptables -N REDINPUT
304 iptables -A INPUT -j REDINPUT
305 iptables -N REDFORWARD
306 iptables -A FORWARD -j REDFORWARD
307 iptables -t nat -N REDNAT
308 iptables -t nat -A POSTROUTING -j REDNAT
309
310 # Apply OpenVPN firewall rules
311 /usr/local/bin/openvpnctrl --firewall-rules
312
313 # run wirelessctrl
314 /usr/local/bin/wirelessctrl
315
316 # POLICY CHAIN
317 iptables -N POLICYIN
318 iptables -A INPUT -j POLICYIN
319 iptables -N POLICYFWD
320 iptables -A FORWARD -j POLICYFWD
321 iptables -N POLICYOUT
322 iptables -A OUTPUT -j POLICYOUT
323
324 # Initialize firewall policies.
325 /usr/sbin/firewall-policy
326
327 # Install firewall rules for the red interface.
328 iptables_red_up
329
330 # If red has not been brought up yet, we will
331 # add the blocking rules for MASQUERADE
332 if [ ! -e "/var/ipfire/red/active" ]; then
333 iptables_red_down
334 fi
335 }
336
337 iptables_red_up() {
338 iptables -F REDINPUT
339 iptables -F REDFORWARD
340 iptables -t nat -F REDNAT
341
342 # PPPoE / PPTP Device
343 if [ "$IFACE" != "" ]; then
344 # PPPoE / PPTP
345 if [ "$DEVICE" != "" ]; then
346 iptables -A REDINPUT -i $DEVICE -j ACCEPT
347 fi
348 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
349 if [ "$RED_DEV" != "" ]; then
350 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
351 fi
352 fi
353 fi
354
355 # PPTP over DHCP
356 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
357 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
358 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
359 fi
360
361 # Orange pinholes
362 if [ "$ORANGE_DEV" != "" ]; then
363 # This rule enables a host on ORANGE network to connect to the outside
364 # (only if we have a red connection)
365 if [ "$IFACE" != "" ]; then
366 iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
367 fi
368 fi
369
370 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
371 # DHCP
372 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
373 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
374 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
375 fi
376 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
377 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
378 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
379 fi
380
381 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
382 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
383
384 if [ "$IFACE" = "$GREEN_DEV" ]; then
385 MASQUERADE_GREEN="off"
386 fi
387
388 local NO_MASQ_NETWORKS
389
390 if [ "${MASQUERADE_GREEN}" = "off" ]; then
391 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
392 fi
393
394 if [ "${MASQUERADE_BLUE}" = "off" ]; then
395 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
396 fi
397
398 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
399 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
400 fi
401
402 local network
403 for network in ${NO_MASQ_NETWORKS}; do
404 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
405 done
406
407 # Masquerade everything else
408 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
409 fi
410
411 # Reload all rules.
412 /usr/local/bin/firewallctrl
413 }
414
415 iptables_red_down() {
416 # Prohibit packets to reach the masquerading rule
417 # while the wan interface is down - this is required to
418 # circumvent udp related NAT issues
419 # http://forum.ipfire.org/index.php?topic=11127.0
420 if [ -n "${IFACE}" ]; then
421 iptables -F REDFORWARD
422 iptables -A REDFORWARD -o "${IFACE}" -j DROP
423 fi
424
425 # Reload all rules.
426 /usr/local/bin/firewallctrl
427 }
428
429 # See how we were called.
430 case "$1" in
431 start)
432 boot_mesg "Setting up firewall"
433 iptables_init
434 evaluate_retval
435 ;;
436 reload|up)
437 boot_mesg "Reloading firewall"
438 iptables_red_up
439 evaluate_retval
440 ;;
441 down)
442 boot_mesg "Disabling firewall access to RED"
443 iptables_red_down
444 evaluate_retval
445 ;;
446 restart)
447 $0 start
448 ;;
449 *)
450 echo "Usage: $0 {start|reload|restart}"
451 exit 1
452 ;;
453 esac
454
455 exit 0