]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/system/firewall
firewall: Ensure the xt_geoip module is always loaded
[ipfire-2.x.git] / src / initscripts / system / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
0f5c5ce7
MT
3. /etc/sysconfig/rc
4. ${rc_functions}
5
3a1019f6
MT
6eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
7eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
fe0cd647 8eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
3a1019f6
MT
9IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
10
11if [ -f /var/ipfire/red/device ]; then
12 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
13fi
14
ce31144c
MT
15NAT_MASK="0x0f000000"
16
3fa8300e
MT
17IPS_REPEAT_MARK="0x80000000"
18IPS_REPEAT_MASK="0x80000000"
19IPS_BYPASS_MARK="0x40000000"
20IPS_BYPASS_MASK="0x40000000"
21
c581b670
MT
22function iptables() {
23 /sbin/iptables --wait "$@"
24}
25
3a1019f6
MT
26iptables_init() {
27 # Flush all rules and delete all custom chains
c581b670
MT
28 iptables -F
29 iptables -t nat -F
30 iptables -t mangle -F
b1109b8a 31 iptables -t raw -F
c581b670
MT
32 iptables -X
33 iptables -t nat -X
34 iptables -t mangle -X
b1109b8a 35 iptables -t raw -X
3a1019f6
MT
36
37 # Set up policies
c581b670
MT
38 iptables -P INPUT DROP
39 iptables -P FORWARD DROP
40 iptables -P OUTPUT ACCEPT
3a1019f6 41
816b0e08
PM
42 # Ensure the xt_geoip module is always loaded (#12767)
43 modprobe xt_geoip
44
78b65ea7
MT
45 # Enable TRACE logging to syslog
46 modprobe nf_log_ipv4
47 sysctl -q -w net.netfilter.nf_log.2=nf_log_ipv4
48
3fa8300e
MT
49 # IPS Bypass Chain which stores the BYPASS bit in connection tracking
50 iptables -N IPSBYPASS
19357bc5 51 iptables -A IPSBYPASS -j CONNMARK --save-mark --mask "$(( ~IPS_REPEAT_MASK & 0xffffffff ))"
3fa8300e
MT
52
53 # Jump into bypass chain when the BYPASS bit is set
54 for chain in INPUT FORWARD OUTPUT; do
55 iptables -A "${chain}" -m mark \
56 --mark "$(( IPS_REPEAT_MARK | IPS_BYPASS_MARK ))/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j IPSBYPASS
57 done
58
3a1019f6 59 # Empty LOG_DROP and LOG_REJECT chains
c581b670 60 iptables -N LOG_DROP
8ee3a135 61 iptables -A LOG_DROP -m limit --limit 10/second -j LOG
c581b670
MT
62 iptables -A LOG_DROP -j DROP
63 iptables -N LOG_REJECT
8ee3a135 64 iptables -A LOG_REJECT -m limit --limit 10/second -j LOG
c581b670 65 iptables -A LOG_REJECT -j REJECT
3a1019f6
MT
66
67 # This chain will log, then DROPs packets with certain bad combinations
ef7e9e52 68 # of flags might indicate a port-scan attempt (xmas, null, etc.)
c581b670 69 iptables -N PSCAN
5595bc03 70 if [ "$DROPPORTSCAN" == "on" ]; then
ef7e9e52
PM
71 iptables -A PSCAN -p tcp -m limit --limit 10/second -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
72 iptables -A PSCAN -p udp -m limit --limit 10/second -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
8ee3a135
PM
73 iptables -A PSCAN -p icmp -m limit --limit 10/second -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
74 iptables -A PSCAN -f -m limit --limit 10/second -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 75 fi
c581b670 76 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
77
78 # New tcp packets without SYN set - could well be an obscure type of port scan
ef7e9e52 79 # that's not covered above, may just be a broken Windows machine
c581b670 80 iptables -N NEWNOTSYN
5595bc03 81 if [ "$DROPNEWNOTSYN" == "on" ]; then
8ee3a135 82 iptables -A NEWNOTSYN -m limit --limit 10/second -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 83 fi
c581b670 84 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6 85
a36cd34e
PM
86 # Log and subsequently drop spoofed packets or "martians", arriving from sources
87 # on interfaces where we don't expect them
88 iptables -N SPOOFED_MARTIAN
89 if [ "$DROPSPOOFEDMARTIAN" == "on" ]; then
90 iptables -A SPOOFED_MARTIAN -m limit --limit 10/second -j LOG --log-prefix "DROP_SPOOFED_MARTIAN "
91 fi
92 iptables -A SPOOFED_MARTIAN -j DROP -m comment --comment "DROP_SPOOFED_MARTIAN"
93
3a1019f6 94 # Chain to contain all the rules relating to bad TCP flags
c581b670 95 iptables -N BADTCP
3a1019f6 96
c581b670
MT
97 # Don't check loopback
98 iptables -A BADTCP -i lo -j RETURN
d8158ca6 99
3a1019f6 100 # Disallow packets frequently used by port-scanners
dccbf1bf
AF
101 # NMAP FIN/URG/PSH (XMAS scan)
102 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
103 # SYN/RST/ACK/FIN/URG
104 iptables -A BADTCP -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PSCAN
105 # ALL/ALL
106 iptables -A BADTCP -p tcp --tcp-flags ALL ALL -j PSCAN
107 # FIN Stealth
c581b670 108 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
3a1019f6 109 # SYN/RST (also catches xmas variants that set SYN+RST+...)
c581b670 110 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
3a1019f6 111 # SYN/FIN (QueSO or nmap OS probe)
c581b670 112 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
dccbf1bf
AF
113 # Null
114 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
3a1019f6 115 # NEW TCP without SYN
c581b670 116 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
b85d2a98 117
c581b670
MT
118 iptables -A INPUT -p tcp -j BADTCP
119 iptables -A FORWARD -p tcp -j BADTCP
c0359d6d 120
b1109b8a 121 # Connection tracking chains
c581b670 122 iptables -N CONNTRACK
b1109b8a 123 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED -j ACCEPT
0e7bfb13 124 iptables -A CONNTRACK -m conntrack --ctstate INVALID -j LOG_DROP
0f535060 125 iptables -A CONNTRACK -p icmp -m conntrack --ctstate RELATED -j ACCEPT
b1109b8a 126
c825fcef
MT
127 # Restore any connection marks
128 iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
129
ef7e9e52 130 # Fix for braindead ISPs
c581b670 131 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
3a1019f6
MT
132
133 # CUSTOM chains, can be used by the users themselves
c581b670
MT
134 iptables -N CUSTOMINPUT
135 iptables -A INPUT -j CUSTOMINPUT
136 iptables -N CUSTOMFORWARD
137 iptables -A FORWARD -j CUSTOMFORWARD
138 iptables -N CUSTOMOUTPUT
139 iptables -A OUTPUT -j CUSTOMOUTPUT
140 iptables -t nat -N CUSTOMPREROUTING
141 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
142 iptables -t nat -N CUSTOMPOSTROUTING
143 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
3a1019f6 144
97154d05
PM
145 # Log and drop any traffic from and to networks known as being hostile, posing
146 # a technical threat to our users (i. e. listed at Spamhaus DROP et al.)
37a9a0ba 147 iptables -N HOSTILE
97154d05 148 if [ "$DROPHOSTILE" == "on" ]; then
37a9a0ba
PM
149 iptables -A HOSTILE -m limit --limit 10/second -j LOG --log-prefix "DROP_HOSTILE "
150 iptables -A INPUT -i $IFACE -m geoip --src-cc XD -j HOSTILE
151 iptables -A FORWARD -i $IFACE -m geoip --src-cc XD -j HOSTILE
152 iptables -A FORWARD -o $IFACE -m geoip --dst-cc XD -j HOSTILE
153 iptables -A OUTPUT -o $IFACE -m geoip --src-cc XD -j HOSTILE
97154d05 154 fi
37a9a0ba 155 iptables -A HOSTILE -j DROP -m comment --comment "DROP_HOSTILE"
97154d05 156
2a5b19c5
AF
157 # P2PBLOCK
158 iptables -N P2PBLOCK
159 iptables -A INPUT -j P2PBLOCK
160 iptables -A FORWARD -j P2PBLOCK
161 iptables -A OUTPUT -j P2PBLOCK
0e7bfb13 162
ef7e9e52 163 # IPS (Guardian) chains
c581b670
MT
164 iptables -N GUARDIAN
165 iptables -A INPUT -j GUARDIAN
166 iptables -A FORWARD -j GUARDIAN
815eaff4 167
80fbd899
MT
168 # Block non-established IPsec networks
169 iptables -N IPSECBLOCK
170 iptables -A FORWARD -m policy --dir out --pol none -j IPSECBLOCK
171 iptables -A OUTPUT -m policy --dir out --pol none -j IPSECBLOCK
172
1e555330 173 # Block OpenVPN transfer networks
c581b670 174 iptables -N OVPNBLOCK
a0a5c14f 175 iptables -A INPUT -i tun+ -j OVPNBLOCK
a0a5c14f
MT
176 iptables -A FORWARD -i tun+ -j OVPNBLOCK
177 iptables -A FORWARD -o tun+ -j OVPNBLOCK
1e555330 178
ef7e9e52 179 # IPS (Suricata) chains
5dba8382
PM
180 iptables -N IPS_INPUT
181 iptables -N IPS_FORWARD
182 iptables -N IPS_OUTPUT
3fa8300e
MT
183
184 for chain in INPUT FORWARD OUTPUT; do
185 iptables -A "${chain}" -m mark --mark "0x0/$(( IPS_REPEAT_MASK | IPS_BYPASS_MASK ))" -j "IPS_${chain}"
186 done
5dba8382 187
51ab1de1 188 # OpenVPN transfer network translation
c581b670
MT
189 iptables -t nat -N OVPNNAT
190 iptables -t nat -A POSTROUTING -j OVPNNAT
51ab1de1 191
daa1ceba 192 # IPTV chains for IGMPPROXY
c581b670
MT
193 iptables -N IPTVINPUT
194 iptables -A INPUT -j IPTVINPUT
195 iptables -N IPTVFORWARD
196 iptables -A FORWARD -j IPTVFORWARD
daa1ceba 197
8e59a602
MT
198 # Allow to ping the firewall.
199 iptables -N ICMPINPUT
200 iptables -A INPUT -j ICMPINPUT
201 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6 202
a36cd34e 203 # Accept everything on loopback if source/destination is loopback space...
c581b670 204 iptables -N LOOPBACK
a36cd34e
PM
205 iptables -A LOOPBACK -i lo -s 127.0.0.0/8 -j ACCEPT
206 iptables -A LOOPBACK -o lo -d 127.0.0.0/8 -j ACCEPT
207
208 # ... and drop everything else on the loopback interface, since no other traffic should appear there
209 iptables -A LOOPBACK -i lo -j SPOOFED_MARTIAN
210 iptables -A LOOPBACK -o lo -j SPOOFED_MARTIAN
afc611d4 211
a36cd34e
PM
212 # Filter all packets with loopback addresses on non-loopback interfaces (spoofed)
213 iptables -A LOOPBACK -s 127.0.0.0/8 -j SPOOFED_MARTIAN
214 iptables -A LOOPBACK -d 127.0.0.0/8 -j SPOOFED_MARTIAN
3b9a23ce
MT
215
216 for i in INPUT FORWARD OUTPUT; do
c581b670 217 iptables -A ${i} -j LOOPBACK
3b9a23ce 218 done
afc611d4 219
bbaa3613
AM
220 # Captive portal
221 iptables -N CAPTIVE_PORTAL
222 iptables -N CAPTIVE_PORTAL_CLIENTS
223 for i in INPUT FORWARD; do
224 iptables -A ${i} -j CAPTIVE_PORTAL
225 done
226
3a1019f6 227 # Accept everything connected
b85d2a98 228 for i in INPUT FORWARD OUTPUT; do
c581b670 229 iptables -A ${i} -j CONNTRACK
b85d2a98
MT
230 done
231
8490e496
MT
232 # Allow DHCP
233 iptables -N DHCPINPUT
234 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
235 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
236
237 iptables -N DHCPOUTPUT
238 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
239 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
240
241 # Allow DHCP on GREEN
242 iptables -N DHCPGREENINPUT
243 iptables -N DHCPGREENOUTPUT
244 if [ -n "${GREEN_DEV}" ]; then
245 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
246 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
247 fi
248
ef7e9e52 249 # Allow DHCP on BLUE to be turned on/off
8490e496
MT
250 iptables -N DHCPBLUEINPUT
251 iptables -N DHCPBLUEOUTPUT
252 if [ -n "${BLUE_DEV}" ]; then
253 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
254 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
255 fi
256
4d25c1f3
PM
257 # Tor (inbound)
258 iptables -N TOR_INPUT
259 iptables -A INPUT -j TOR_INPUT
260
0e6eca78
MT
261 # Location Block
262 iptables -N LOCATIONBLOCK
263 iptables -A INPUT -j LOCATIONBLOCK
264 iptables -A FORWARD -j LOCATIONBLOCK
cab02e2a 265
5fd30232 266 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
c581b670
MT
267 iptables -N IPSECINPUT
268 iptables -N IPSECFORWARD
269 iptables -N IPSECOUTPUT
270 iptables -A INPUT -j IPSECINPUT
271 iptables -A FORWARD -j IPSECFORWARD
272 iptables -A OUTPUT -j IPSECOUTPUT
273 iptables -t nat -N IPSECNAT
274 iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 275
3a1019f6 276 # localhost and ethernet.
c0e0848f
MT
277 # Always allow accessing the web GUI from GREEN.
278 iptables -N GUIINPUT
279 iptables -A INPUT -j GUIINPUT
48a7737f
MT
280 if [ -n "${GREEN_DEV}" ]; then
281 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
282 fi
8490e496 283
81393987 284 # WIRELESS chains
c581b670
MT
285 iptables -N WIRELESSINPUT
286 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
287 iptables -N WIRELESSFORWARD
288 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
987b75bc 289
ab4876ad 290 # OpenVPN
c581b670
MT
291 iptables -N OVPNINPUT
292 iptables -A INPUT -j OVPNINPUT
ab4876ad 293
4d25c1f3 294 # Tor (outbound)
5fc5f703
PM
295 iptables -N TOR_OUTPUT
296 iptables -A OUTPUT -j TOR_OUTPUT
0e7bfb13 297
d5f1422d 298 # Jump into the actual firewall ruleset.
c581b670
MT
299 iptables -N INPUTFW
300 iptables -A INPUT -j INPUTFW
d5f1422d 301
c581b670
MT
302 iptables -N OUTGOINGFW
303 iptables -A OUTPUT -j OUTGOINGFW
d5f1422d 304
c581b670
MT
305 iptables -N FORWARDFW
306 iptables -A FORWARD -j FORWARDFW
d5f1422d 307
fac38614 308 # SNAT rules
c581b670
MT
309 iptables -t nat -N NAT_SOURCE
310 iptables -t nat -A POSTROUTING -j NAT_SOURCE
fac38614 311
9bb40553
MT
312 # Captive Portal
313 iptables -t nat -N CAPTIVE_PORTAL
314 iptables -t nat -A PREROUTING -j CAPTIVE_PORTAL
315
bb12dd7b 316 # Custom prerouting chains (for transparent proxy)
c581b670
MT
317 iptables -t nat -N SQUID
318 iptables -t nat -A PREROUTING -j SQUID
bb12dd7b
MT
319
320 # DNAT rules
c581b670
MT
321 iptables -t nat -N NAT_DESTINATION
322 iptables -t nat -A PREROUTING -j NAT_DESTINATION
99f11a16 323 iptables -t nat -A OUTPUT -j NAT_DESTINATION
bb12dd7b 324
6e87f0aa
MT
325 iptables -t mangle -N NAT_DESTINATION
326 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
327
328 iptables -t nat -N NAT_DESTINATION_FIX
329 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
330
48a7737f
MT
331 if [ -n "${GREEN_ADDRESS}" ]; then
332 iptables -t nat -A NAT_DESTINATION_FIX \
ce31144c 333 -m mark --mark "0x01000000/${NAT_MASK}" -j SNAT --to-source "${GREEN_ADDRESS}"
48a7737f 334 fi
6e87f0aa
MT
335
336 if [ -n "${BLUE_ADDRESS}" ]; then
337 iptables -t nat -A NAT_DESTINATION_FIX \
ce31144c 338 -m mark --mark "0x02000000/${NAT_MASK}" -j SNAT --to-source "${BLUE_ADDRESS}"
6e87f0aa
MT
339 fi
340
341 if [ -n "${ORANGE_ADDRESS}" ]; then
342 iptables -t nat -A NAT_DESTINATION_FIX \
ce31144c 343 -m mark --mark "0x04000000/${NAT_MASK}" -j SNAT --to-source "${ORANGE_ADDRESS}"
6e87f0aa
MT
344 fi
345
6e87f0aa
MT
346 # RED chain, used for the red interface
347 iptables -N REDINPUT
348 iptables -A INPUT -j REDINPUT
349 iptables -N REDFORWARD
350 iptables -A FORWARD -j REDFORWARD
351 iptables -t nat -N REDNAT
352 iptables -t nat -A POSTROUTING -j REDNAT
353
6c920b19
MT
354 # Populate IPsec chains
355 /usr/lib/firewall/ipsec-policy
80fbd899 356
ab4876ad
MT
357 # Apply OpenVPN firewall rules
358 /usr/local/bin/openvpnctrl --firewall-rules
ff4770c7
AM
359
360 # run wirelessctrl
361 /usr/local/bin/wirelessctrl
362
1722701a
AM
363 # run captivectrl
364 /usr/local/bin/captivectrl
365
c581b670
MT
366 # POLICY CHAIN
367 iptables -N POLICYIN
368 iptables -A INPUT -j POLICYIN
369 iptables -N POLICYFWD
370 iptables -A FORWARD -j POLICYFWD
371 iptables -N POLICYOUT
372 iptables -A OUTPUT -j POLICYOUT
b324de14 373
bb383423 374 # Initialize firewall policies.
5d7faa45 375 /usr/sbin/firewall-policy
690b0bd7 376
bb383423 377 # Install firewall rules for the red interface.
4b12aa41
TE
378 iptables_red_up
379
380 # If red has not been brought up yet, we will
381 # add the blocking rules for MASQUERADE
382 if [ ! -e "/var/ipfire/red/active" ]; then
383 iptables_red_down
384 fi
ff4770c7 385}
3a1019f6 386
4b12aa41 387iptables_red_up() {
c581b670
MT
388 iptables -F REDINPUT
389 iptables -F REDFORWARD
390 iptables -t nat -F REDNAT
3a1019f6 391
e83ae0d4
PM
392 # Prohibit spoofing our own IP address on RED
393 if [ -f /var/ipfire/red/active ]; then
394 REDIP="$( cat /var/ipfire/red/local-ipaddress )";
395
396 if [ "$IFACE" != "" ]; then
397 iptables -A REDINPUT -s $REDIP -i $IFACE -j SPOOFED_MARTIAN
398 elif [ "$DEVICE" != "" ]; then
399 iptables -A REDINPUT -s $REDIP -i $DEVICE -j SPOOFED_MARTIAN
400 fi
401 fi
402
ff4770c7
AM
403 # PPPoE / PPTP Device
404 if [ "$IFACE" != "" ]; then
405 # PPPoE / PPTP
406 if [ "$DEVICE" != "" ]; then
c581b670 407 iptables -A REDINPUT -i $DEVICE -j ACCEPT
ff4770c7
AM
408 fi
409 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
410 if [ "$RED_DEV" != "" ]; then
c581b670 411 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
ff4770c7
AM
412 fi
413 fi
3a1019f6 414 fi
ff4770c7
AM
415
416 # PPTP over DHCP
417 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
c581b670
MT
418 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
419 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
3a1019f6
MT
420 fi
421
ff4770c7
AM
422 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
423 # DHCP
424 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
c581b670
MT
425 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
426 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
427 fi
428 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
c581b670
MT
429 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
430 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
431 fi
432
51c4b73f 433 # Outgoing masquerading (don't masqerade IPsec)
e850a614 434 iptables -t nat -A REDNAT -m policy --pol ipsec --dir=out -o "${IFACE}" -j RETURN
c926c637 435
60fcb241
AF
436 if [ "${IFACE}" = "${GREEN_DEV}" ]; then
437 iptables -t nat -A REDNAT -i "${GREEN_DEV}" -o "${IFACE}" -j RETURN
c926c637 438 fi
c400fe4c 439
983d471f 440 local NO_MASQ_NETWORKS
83ef9c40
MT
441
442 if [ "${MASQUERADE_GREEN}" = "off" ]; then
983d471f 443 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${GREEN_NETADDRESS}/${GREEN_NETMASK}"
83ef9c40
MT
444 fi
445
446 if [ "${MASQUERADE_BLUE}" = "off" ]; then
983d471f 447 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${BLUE_NETADDRESS}/${BLUE_NETMASK}"
83ef9c40
MT
448 fi
449
450 if [ "${MASQUERADE_ORANGE}" = "off" ]; then
983d471f 451 NO_MASQ_NETWORKS="${NO_MASQ_NETWORKS} ${ORANGE_NETADDRESS}/${ORANGE_NETMASK}"
83ef9c40
MT
452 fi
453
983d471f
MT
454 local network
455 for network in ${NO_MASQ_NETWORKS}; do
456 iptables -t nat -A REDNAT -s "${network}" -o "${IFACE}" -j RETURN
83ef9c40
MT
457 done
458
459 # Masquerade everything else
460 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
6be0579b 461 fi
66f6b279
MT
462
463 # Reload all rules.
55a5bcae 464 /usr/local/bin/firewallctrl
ff4770c7
AM
465}
466
4b12aa41
TE
467iptables_red_down() {
468 # Prohibit packets to reach the masquerading rule
e01e07ec 469 # while the WAN interface is down - this is required to
4b12aa41
TE
470 # circumvent udp related NAT issues
471 # http://forum.ipfire.org/index.php?topic=11127.0
e7204c2d
MT
472 if [ -n "${IFACE}" ]; then
473 iptables -F REDFORWARD
474 iptables -A REDFORWARD -o "${IFACE}" -j DROP
475 fi
4b12aa41
TE
476
477 # Reload all rules.
478 /usr/local/bin/firewallctrl
479}
480
ff4770c7
AM
481# See how we were called.
482case "$1" in
483 start)
7d7740a4 484 boot_mesg "Setting up firewall"
ff4770c7 485 iptables_init
7d7740a4 486 evaluate_retval
6be0579b 487 ;;
4b12aa41 488 reload|up)
7d7740a4 489 boot_mesg "Reloading firewall"
4b12aa41 490 iptables_red_up
7d7740a4 491 evaluate_retval
3a1019f6 492 ;;
4b12aa41
TE
493 down)
494 boot_mesg "Disabling firewall access to RED"
495 iptables_red_down
496 evaluate_retval
497 ;;
3a1019f6 498 restart)
3a1019f6
MT
499 $0 start
500 ;;
501 *)
ff4770c7 502 echo "Usage: $0 {start|reload|restart}"
3a1019f6
MT
503 exit 1
504 ;;
505esac
506
507exit 0