Patch-O-Matic in den Kernel eingebaut.
[people/teissler/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
36 /sbin/iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "TCP Scan? "
37 /sbin/iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "UDP Scan? "
38 /sbin/iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "ICMP Scan? "
39 /sbin/iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "FRAG Scan? "
40 /sbin/iptables -A PSCAN -j DROP
41
42 # New tcp packets without SYN set - could well be an obscure type of port scan
43 # that's not covered above, may just be a broken windows machine
44 /sbin/iptables -N NEWNOTSYN
45 /sbin/iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "NEW not SYN? "
46 /sbin/iptables -A NEWNOTSYN -j DROP
47
48 # Chain to contain all the rules relating to bad TCP flags
49 /sbin/iptables -N BADTCP
50
51 # Disallow packets frequently used by port-scanners
52 # nmap xmas
53 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
54 # Null
55 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
56 # FIN
57 /sbin/iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
58 # SYN/RST (also catches xmas variants that set SYN+RST+...)
59 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
60 # SYN/FIN (QueSO or nmap OS probe)
61 /sbin/iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
62 # NEW TCP without SYN
63 /sbin/iptables -A BADTCP -p tcp ! --syn -m state --state NEW -j NEWNOTSYN
64
65 /sbin/iptables -A INPUT -j BADTCP
66 /sbin/iptables -A FORWARD -j BADTCP
67
68}
69
70iptables_red() {
71 /sbin/iptables -F REDINPUT
72 /sbin/iptables -F REDFORWARD
73 /sbin/iptables -t nat -F REDNAT
74
75 # PPPoE / PPTP Device
76 if [ "$IFACE" != "" ]; then
77 # PPPoE / PPTP
78 if [ "$DEVICE" != "" ]; then
79 /sbin/iptables -A REDINPUT -i $DEVICE -j ACCEPT
80 fi
81 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
82 if [ "$RED_DEV" != "" ]; then
83 /sbin/iptables -A REDINPUT -i $RED_DEV -j ACCEPT
84 fi
85 fi
86 fi
87
88 # PPTP over DHCP
89 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
90 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
91 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
92 fi
93
94 # Orange pinholes
95 if [ "$ORANGE_DEV" != "" ]; then
96 # This rule enables a host on ORANGE network to connect to the outside
97 # (only if we have a red connection)
98 if [ "$IFACE" != "" ]; then
99 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -p tcp -o $IFACE -j ACCEPT
100 /sbin/iptables -A REDFORWARD -i $ORANGE_DEV -p udp -o $IFACE -j ACCEPT
101 fi
102 fi
103
104 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
105 # DHCP
106 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
107 /sbin/iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
108 /sbin/iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
109 fi
110 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; 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
115 # Outgoing masquerading
116 /sbin/iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
117
118 fi
119}
120
121# See how we were called.
122case "$1" in
123 start)
124 iptables_init
125
126 # Limit Packets- helps reduce dos/syn attacks
127 # original do nothing line
128 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 10/sec
129 # the correct one, but the negative '!' do nothing...
130 #/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit ! --limit 10/sec -j DROP
131
132 # Fix for braindead ISP's
133 /sbin/iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
134
135 # CUSTOM chains, can be used by the users themselves
136 /sbin/iptables -N CUSTOMINPUT
137 /sbin/iptables -A INPUT -j CUSTOMINPUT
138 /sbin/iptables -N CUSTOMFORWARD
139 /sbin/iptables -A FORWARD -j CUSTOMFORWARD
140 /sbin/iptables -N CUSTOMOUTPUT
141 /sbin/iptables -A OUTPUT -j CUSTOMOUTPUT
142 /sbin/iptables -t nat -N CUSTOMPREROUTING
143 /sbin/iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
144 /sbin/iptables -t nat -N CUSTOMPOSTROUTING
145 /sbin/iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
146
147 # filtering from GUI
148 /sbin/iptables -N GUIINPUT
149 /sbin/iptables -A INPUT -j GUIINPUT
150
151 # Accept everything connected
152 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
153 /sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
154
155 # localhost and ethernet.
156 /sbin/iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
157 /sbin/iptables -A INPUT -s 127.0.0.0/8 -m state --state NEW -j DROP # Loopback not on lo
158 /sbin/iptables -A INPUT -d 127.0.0.0/8 -m state --state NEW -j DROP
159 /sbin/iptables -A FORWARD -i lo -m state --state NEW -j ACCEPT
160 /sbin/iptables -A FORWARD -s 127.0.0.0/8 -m state --state NEW -j DROP
161 /sbin/iptables -A FORWARD -d 127.0.0.0/8 -m state --state NEW -j DROP
162 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT -p ! icmp
163 /sbin/iptables -A FORWARD -i $GREEN_DEV -m state --state NEW -j ACCEPT
164
165 # If a host on orange tries to initiate a connection to IPFire's red IP and
166 # the connection gets DNATed back through a port forward to a server on orange
167 # we end up with orange -> orange traffic passing through IPFire
168 [ "$ORANGE_DEV" != "" ] && /sbin/iptables -A FORWARD -i $ORANGE_DEV -o $ORANGE_DEV -m state --state NEW -j ACCEPT
169
170 # accept all traffic from ipsec interfaces
171 /sbin/iptables -A INPUT -i ipsec+ -j ACCEPT
172 /sbin/iptables -A FORWARD -i ipsec+ -j ACCEPT
173
174 # allow DHCP on BLUE to be turned on/off
175 /sbin/iptables -N DHCPBLUEINPUT
176 /sbin/iptables -A INPUT -j DHCPBLUEINPUT
177
178 # IPSec chains
179 /sbin/iptables -N IPSECRED
180 /sbin/iptables -A INPUT -j IPSECRED
181 /sbin/iptables -N IPSECBLUE
182 /sbin/iptables -A INPUT -j IPSECBLUE
183
184 # WIRELESS chains
185 /sbin/iptables -N WIRELESSINPUT
186 /sbin/iptables -A INPUT -m state --state NEW -j WIRELESSINPUT
187 /sbin/iptables -N WIRELESSFORWARD
188 /sbin/iptables -A FORWARD -m state --state NEW -j WIRELESSFORWARD
189
190 # RED chain, used for the red interface
191 /sbin/iptables -N REDINPUT
192 /sbin/iptables -A INPUT -j REDINPUT
193 /sbin/iptables -N REDFORWARD
194 /sbin/iptables -A FORWARD -j REDFORWARD
195 /sbin/iptables -t nat -N REDNAT
196 /sbin/iptables -t nat -A POSTROUTING -j REDNAT
197
198 iptables_red
199
200 # DMZ pinhole chain. setdmzholes setuid prog adds rules here to allow
201 # ORANGE to talk to GREEN / BLUE.
202 /sbin/iptables -N DMZHOLES
203 if [ "$ORANGE_DEV" != "" ]; then
204 /sbin/iptables -A FORWARD -i $ORANGE_DEV -m state --state NEW -j DMZHOLES
205 fi
206
207 # XTACCESS chain, used for external access
208 /sbin/iptables -N XTACCESS
209 /sbin/iptables -A INPUT -m state --state NEW -j XTACCESS
210
211 # PORTFWACCESS chain, used for portforwarding
212 /sbin/iptables -N PORTFWACCESS
213 /sbin/iptables -A FORWARD -m state --state NEW -j PORTFWACCESS
214
215 # Custom prerouting chains (for transparent proxy and port forwarding)
216 /sbin/iptables -t nat -N SQUID
217 /sbin/iptables -t nat -A PREROUTING -j SQUID
218 /sbin/iptables -t nat -N PORTFW
219 /sbin/iptables -t nat -A PREROUTING -j PORTFW
220
221
222 # Custom mangle chain (for port fowarding)
223 /sbin/iptables -t mangle -N PORTFWMANGLE
224 /sbin/iptables -t mangle -A PREROUTING -j PORTFWMANGLE
225
226 # Postrouting rules (for port forwarding)
227 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 1 -j SNAT \
228 --to-source $GREEN_ADDRESS
229 if [ "$BLUE_DEV" != "" ]; then
230 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 2 -j SNAT --to-source $BLUE_ADDRESS
231 fi
232 if [ "$ORANGE_DEV" != "" ]; then
233 /sbin/iptables -t nat -A POSTROUTING -m mark --mark 3 -j SNAT --to-source $ORANGE_ADDRESS
234 fi
235
236 # run openvpn
237 /usr/local/bin/openvpnctrl --create-chains-and-rules
238
239 # run local firewall configuration, if present
240 if [ -x /etc/sysconfig/firewall.local ]; then
241 /etc/sysconfig/firewall.local start
242 fi
243
244 # last rule in input and forward chain is for logging.
245 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "INPUT "
246 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "OUTPUT "
247 ;;
248 stop)
249 iptables_init
250 # Accept everyting connected
251 /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
252
253 # localhost and ethernet.
254 /sbin/iptables -A INPUT -i lo -j ACCEPT
255 /sbin/iptables -A INPUT -i $GREEN_DEV -m state --state NEW -j ACCEPT
256
257 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
258 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
259 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
260 fi
261 if [ "$PROTOCOL" == "RFC1483" -a "$METHOD" == "DHCP" ]; then
262 /sbin/iptables -A INPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
263 /sbin/iptables -A INPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
264 fi
265
266 # stop openvpn
267 /usr/local/bin/openvpnctrl --delete-chains-and-rules
268
269 # run local firewall configuration, if present
270 if [ -x /etc/sysconfig/firewall.local ]; then
271 /etc/sysconfig/firewall.local stop
272 fi
273
274 /sbin/iptables -A INPUT -m limit --limit 10/minute -j LOG --log-prefix "INPUT "
275 /sbin/iptables -A FORWARD -m limit --limit 10/minute -j LOG --log-prefix "OUTPUT "
276 ;;
277 reload)
278 iptables_red
279
280 # run local firewall configuration, if present
281 if [ -x /etc/sysconfig/firewall.local ]; then
282 /etc/sysconfig/firewall.local reload
283 fi
284 ;;
285 restart)
286 $0 stop
287 $0 start
288 ;;
289 *)
290 echo "Usage: $0 {start|stop|reload|restart}"
291 exit 1
292 ;;
293esac
294
295exit 0