]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/init.d/firewall
Some smaller enhancements for the firewall script
[ipfire-2.x.git] / src / initscripts / init.d / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
3eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
4eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
5IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
6
7if [ -f /var/ipfire/red/device ]; then
8 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
9fi
10
11iptables_init() {
12 # Flush all rules and delete all custom chains
13 /sbin/iptables -F
14 /sbin/iptables -t nat -F
15 /sbin/iptables -t mangle -F
16 /sbin/iptables -X
17 /sbin/iptables -t nat -X
18 /sbin/iptables -t mangle -X
19
20 # Set up policies
21 /sbin/iptables -P INPUT DROP
22 /sbin/iptables -P FORWARD DROP
23 /sbin/iptables -P OUTPUT ACCEPT
24
25 # Empty LOG_DROP and LOG_REJECT chains
26 /sbin/iptables -N LOG_DROP
27 /sbin/iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
28 /sbin/iptables -A LOG_DROP -j DROP
29 /sbin/iptables -N LOG_REJECT
30 /sbin/iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
31 /sbin/iptables -A LOG_REJECT -j REJECT
32
33 # This chain will log, then DROPs packets with certain bad combinations
34 # of flags might indicate a port-scan attempt (xmas, null, etc)
35 /sbin/iptables -N PSCAN
5595bc03 36 if [ "$DROPPORTSCAN" == "on" ]; then
97fe1741
CS
37 /sbin/iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
38 /sbin/iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UPD PScan"
39 /sbin/iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
40 /sbin/iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 41 fi
97fe1741 42 /sbin/iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
43
44 # New tcp packets without SYN set - could well be an obscure type of port scan
45 # that's not covered above, may just be a broken windows machine
46 /sbin/iptables -N NEWNOTSYN
5595bc03 47 if [ "$DROPNEWNOTSYN" == "on" ]; then
97fe1741 48 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 49 fi
97fe1741 50 /sbin/iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6
MT
51
52 # Chain to contain all the rules relating to bad TCP flags
53 /sbin/iptables -N BADTCP
54
55 # Disallow packets frequently used by port-scanners
56 # nmap xmas
57 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
58 # Null
59 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
60 # FIN
61 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
62 # SYN/RST (also catches xmas variants that set SYN+RST+...)
63 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
64 # SYN/FIN (QueSO or nmap OS probe)
65 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
66 # NEW TCP without SYN
67 /sbin/iptables -A BADTCP -p tcp ! --syn -m state --state NEW -j NEWNOTSYN
68
69 /sbin/iptables -A INPUT -j BADTCP
70 /sbin/iptables -A FORWARD -j BADTCP
71
72}
73
74iptables_red() {
75 /sbin/iptables -F REDINPUT
76 /sbin/iptables -F REDFORWARD
77 /sbin/iptables -t nat -F REDNAT
78
79 # PPPoE / PPTP Device
80 if [ "$IFACE" != "" ]; then
81 # PPPoE / PPTP
82 if [ "$DEVICE" != "" ]; then
83 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
84 fi
85 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
86 if [ "$RED_DEV" != "" ]; then
87 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
88 fi
89 fi
90 fi
91
92 # PPTP over DHCP
93 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
94 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
95 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
96 fi
97
98 # Orange pinholes
99 if [ "$ORANGE_DEV" != "" ]; then
100 # This rule enables a host on ORANGE network to connect to the outside
101 # (only if we have a red connection)
102 if [ "$IFACE" != "" ]; then
103 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -p tcp -o $IFACE -j ACCEPT
104 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -p udp -o $IFACE -j ACCEPT
105 fi
106 fi
107
108 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
109 # DHCP
110 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
111 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
112 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
113 fi
114 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
115 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
116 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
117 fi
118
119 # Outgoing masquerading
120 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
121
122 fi
123}
124
125# See how we were called.
126case "$1" in
127 start)
128 iptables_init
129
130 # Limit Packets- helps reduce dos/syn attacks
131 # original do nothing line
132 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 10/sec
133 # the correct one, but the negative '!' do nothing...
134 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit ! --limit 10/sec -j DROP
135
136 # Fix for braindead ISP's
137 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
138
139 # CUSTOM chains, can be used by the users themselves
140 /sbin/iptables -N CUSTOMINPUT
141 /sbin/iptables -A INPUT -j CUSTOMINPUT
142 /sbin/iptables -N CUSTOMFORWARD
143 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
144 /sbin/iptables -N CUSTOMOUTPUT
145 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
4cb74dce
MT
146 /sbin/iptables -N OUTGOINGFW
147 /sbin/iptables -A OUTPUT -j OUTGOINGFW
3a1019f6
MT
148 /sbin/iptables -t nat -N CUSTOMPREROUTING
149 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
150 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
151 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
152
153 # filtering from GUI
154 /sbin/iptables -N GUIINPUT
155 /sbin/iptables -A INPUT -j GUIINPUT
905fbf3e 156 /sbin/iptables -A GUIINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6
MT
157
158 # Accept everything connected
159 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
160 /sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
5fd30232
MT
161
162 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
163 /sbin/iptables -N IPSECVIRTUAL
164 /sbin/iptables -N OPENSSLVIRTUAL
5595bc03
CS
165 /sbin/iptables -A INPUT -j IPSECVIRTUAL -m comment --comment "IPSECVIRTUAL INPUT"
166 /sbin/iptables -A INPUT -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL INPUT"
167 /sbin/iptables -A FORWARD -j IPSECVIRTUAL -m comment --comment "IPSECVIRTUAL FORWARD"
168 /sbin/iptables -A FORWARD -j OPENSSLVIRTUAL -m comment --comment "OPENSSLVIRTUAL FORWARD"
4cb74dce
MT
169
170 # Outgoing Firewall
171 /sbin/iptables -A FORWARD -j OUTGOINGFW
3a1019f6
MT
172
173 # localhost and ethernet.
174 /sbin/iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
175 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m state --state NEW -j DROP # Loopback not on lo
176 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m state --state NEW -j DROP
177 /sbin/iptables -A FORWARD -i lo -m state --state NEW -j ACCEPT
178 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m state --state NEW -j DROP
179 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m state --state NEW -j DROP
180 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT -p ! icmp
181 /sbin/iptables -A FORWARD -i $GREEN_DEV -m state --state NEW -j ACCEPT
182
183 # If a host on orange tries to initiate a connection to IPFire's red IP and
184 # the connection gets DNATed back through a port forward to a server on orange
185 # we end up with orange -> orange traffic passing through IPFire
186 [ "$ORANGE_DEV" != "" ] && /sbin/iptables -A FORWARD -i $ORANGE_DEV -o $ORANGE_DEV -m state --state NEW -j ACCEPT
187
3a1019f6
MT
188 # allow DHCP on BLUE to be turned on/off
189 /sbin/iptables -N DHCPBLUEINPUT
190 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
191
5fd30232
MT
192 # IPSec
193 /sbin/iptables -N IPSECPHYSICAL
194 /sbin/iptables -A INPUT -j IPSECPHYSICAL
195
196 # OPenSSL
197 /sbin/iptables -N OPENSSLPHYSICAL
198 /sbin/iptables -A INPUT -j OPENSSLPHYSICAL
3a1019f6
MT
199
200 # WIRELESS chains
201 /sbin/iptables -N WIRELESSINPUT
202 /sbin/iptables -A INPUT -m state --state NEW -j WIRELESSINPUT
203 /sbin/iptables -N WIRELESSFORWARD
204 /sbin/iptables -A FORWARD -m state --state NEW -j WIRELESSFORWARD
205
206 # RED chain, used for the red interface
207 /sbin/iptables -N REDINPUT
208 /sbin/iptables -A INPUT -j REDINPUT
209 /sbin/iptables -N REDFORWARD
210 /sbin/iptables -A FORWARD -j REDFORWARD
211 /sbin/iptables -t nat -N REDNAT
212 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
213
214 iptables_red
215
216 # DMZ pinhole chain. setdmzholes setuid prog adds rules here to allow
217 # ORANGE to talk to GREEN / BLUE.
218 /sbin/iptables -N DMZHOLES
219 if [ "$ORANGE_DEV" != "" ]; then
220 /sbin/iptables -A FORWARD -i $ORANGE_DEV -m state --state NEW -j DMZHOLES
221 fi
222
223 # XTACCESS chain, used for external access
224 /sbin/iptables -N XTACCESS
225 /sbin/iptables -A INPUT -m state --state NEW -j XTACCESS
226
227 # PORTFWACCESS chain, used for portforwarding
228 /sbin/iptables -N PORTFWACCESS
229 /sbin/iptables -A FORWARD -m state --state NEW -j PORTFWACCESS
230
231 # Custom prerouting chains (for transparent proxy and port forwarding)
232 /sbin/iptables -t nat -N SQUID
233 /sbin/iptables -t nat -A PREROUTING -j SQUID
234 /sbin/iptables -t nat -N PORTFW
235 /sbin/iptables -t nat -A PREROUTING -j PORTFW
236
7e7495b3
MT
237 # upnp chain for our upnp daemon
238 /sbin/iptables -t nat -N UPNPFW
239 /sbin/iptables -t nat -A PREROUTING -j UPNPFW
240
3a1019f6
MT
241
242 # Custom mangle chain (for port fowarding)
243 /sbin/iptables -t mangle -N PORTFWMANGLE
244 /sbin/iptables -t mangle -A PREROUTING -j PORTFWMANGLE
245
246 # Postrouting rules (for port forwarding)
247 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT \
248 --to-source $GREEN_ADDRESS
249 if [ "$BLUE_DEV" != "" ]; then
250 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
251 fi
252 if [ "$ORANGE_DEV" != "" ]; then
253 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
254 fi
255
3a1019f6
MT
256 # run local firewall configuration, if present
257 if [ -x /etc/sysconfig/firewall.local ]; then
258 /etc/sysconfig/firewall.local start
259 fi
260
261 # last rule in input and forward chain is for logging.
5595bc03
CS
262
263 if [ "$DROPINPUT" == "on" ]; then
97fe1741 264 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
5595bc03 265 fi
97fe1741 266 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
5595bc03 267 if [ "$DROPOUTPUT" == "on" ]; then
97fe1741 268 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_OUTPUT "
5595bc03 269 fi
97fe1741 270 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_OUTPUT"
3a1019f6 271 ;;
13211b21
CS
272 startovpn)
273 # run openvpn
274 /usr/local/bin/openvpnctrl --create-chains-and-rules
275 ;;
3a1019f6
MT
276 stop)
277 iptables_init
278 # Accept everyting connected
279 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
280
281 # localhost and ethernet.
282 /sbin/iptables -A INPUT -i lo -j ACCEPT
283 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT
284
285 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
286 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
287 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
288 fi
289 if [ "$PROTOCOL" == "RFC1483" -a "$METHOD" == "DHCP" ]; then
290 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
291 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
292 fi
293
3a1019f6
MT
294 # run local firewall configuration, if present
295 if [ -x /etc/sysconfig/firewall.local ]; then
296 /etc/sysconfig/firewall.local stop
297 fi
298
5595bc03 299 if [ "$DROPINPUT" == "on" ]; then
97fe1741 300 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "DROP_INPUT "
5595bc03 301 fi
97fe1741 302 /sbin/iptables -A INPUT -j DROP -m comment --comment "DROP_INPUT"
5595bc03 303 if [ "$DROPOUTPUT" == "on" ]; then
97fe1741 304 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "DROP_OUTPUT "
5595bc03 305 fi
97fe1741 306 /sbin/iptables -A FORWARD -j DROP -m comment --comment "DROP_OUTPUT"
3a1019f6 307 ;;
13211b21
CS
308 stopovpn)
309 # stop openvpn
310 /usr/local/bin/openvpnctrl --delete-chains-and-rules
311 ;;
3a1019f6
MT
312 reload)
313 iptables_red
314
315 # run local firewall configuration, if present
316 if [ -x /etc/sysconfig/firewall.local ]; then
317 /etc/sysconfig/firewall.local reload
318 fi
319 ;;
320 restart)
321 $0 stop
322 $0 start
323 ;;
324 *)
325 echo "Usage: $0 {start|stop|reload|restart}"
326 exit 1
327 ;;
328esac
329
330exit 0