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