3 # Startup script to implement /etc/sysconfig/iptables pre-defined rules.
5 # chkconfig: 2345 08 92
7 # description: Automates a packet filtering firewall with iptables.
9 # by bero@redhat.com, based on the ipchains script:
10 # Script Author: Joshua Jensen <joshua@redhat.com>
11 # -- hacked up by gafton with help from notting
12 # modified by Anton Altaparmakov <aia21@cam.ac.uk>:
13 # modified by Nils Philippsen <nils@redhat.de>
15 # config: /etc/sysconfig/iptables
17 IPTABLES_CONFIG=/etc/sysconfig/iptables
18 if [ ! -f $IPTABLES_CONFIG ]; then
20 start|restart|force-reload)
27 . /etc/rc.d/init.d/functions
29 if [ "$(kernelver)" -lt "002003000" ]; then
33 if /sbin/lsmod 2>/dev/null | grep -q ipchains; then
38 IPTABLES_SAVE_ON_STOP="no"
39 IPTABLES_SAVE_COUNTER="no"
40 IPTABLES_STATUS_NUMERIC="yes"
41 IPTABLES_STATUS_VERBOSE="no"
42 IPTABLES_STATUS_LINENUMBERS="yes"
43 [ -f /etc/sysconfig/iptables-config ] && . /etc/sysconfig/iptables-config
45 is_yes $IPTABLES_SAVE_COUNTER && _SAVEOPT="-c"
47 if [ "$1" = "--quiet" ]; then
55 if fgrep -qsx $1 /proc/net/ip_tables_names; then
61 # don't do squat if we don't have the config file
62 if [ -f $IPTABLES_CONFIG ]; then
63 # If we don't clear these first, we might be adding to
65 tables=`cat /proc/net/ip_tables_names 2>/dev/null`
66 show "Flushing all current rules and user defined chains"
68 for i in $tables; do iptables -t $i -F; let ret+=$?; done
69 if [ $ret -eq 0 ]; then
74 show "Clearing all current rules and user defined chains"
76 for i in $tables; do iptables -t $i -X; let ret+=$?; done
77 if [ $ret -eq 0 ]; then
83 for i in $tables; do iptables -t $i -Z; done
85 show "Applying iptables firewall rules"
86 grep -v "^[[:space:]]*#" $IPTABLES_CONFIG | grep -v '^[[:space:]]*$' | /usr/sbin/iptables-restore $_SAVEOPT && \
89 touch /var/lock/subsys/iptables
94 tables=`cat /proc/net/ip_tables_names 2>/dev/null`
95 show "Flushing all chains"
97 for i in $tables; do iptables -t $i -F; let ret+=$?; done
98 if [ $ret -eq 0 ]; then
104 show "Removing user defined chains"
106 for i in $tables; do iptables -t $i -X; let ret+=$?; done
107 if [ $ret -eq 0 ]; then
112 show "Resetting built-in chains to the default ACCEPT policy"
113 iftable filter -P INPUT ACCEPT && \
114 iftable filter -P OUTPUT ACCEPT && \
115 iftable filter -P FORWARD ACCEPT && \
116 iftable nat -P PREROUTING ACCEPT && \
117 iftable nat -P POSTROUTING ACCEPT && \
118 iftable nat -P OUTPUT ACCEPT && \
119 iftable mangle -P PREROUTING ACCEPT && \
120 iftable mangle -P OUTPUT ACCEPT && \
122 rm -f /var/lock/subsys/iptables
126 show "Saving current rules to %s" $IPTABLES_CONFIG
127 touch $IPTABLES_CONFIG
128 chmod 600 $IPTABLES_CONFIG
129 /usr/sbin/iptables-save $_SAVEOPT > $IPTABLES_CONFIG 2>/dev/null && ok || fail
132 upstart_controlled --except status panic load save clear
139 is_yes $IPTABLES_SAVE_ON_STOP && save
145 restart|force-reload)
146 # "restart" is really just "start" as this isn't a daemon,
147 # and "start" clears any pre-defined rules anyway.
148 # This is really only here to make those who expect it happy
152 show "Changing target policies to DROP"
153 iftable filter -P INPUT DROP && \
154 iftable filter -P FORWARD DROP && \
155 iftable filter -P OUTPUT DROP && \
156 iftable nat -P PREROUTING DROP && \
157 iftable nat -P POSTROUTING DROP && \
158 iftable nat -P OUTPUT DROP && \
159 iftable mangle -P PREROUTING DROP && \
160 iftable mangle -P OUTPUT DROP && \
162 iftable filter -F INPUT && \
163 iftable filter -F FORWARD && \
164 iftable filter -F OUTPUT && \
165 iftable nat -F PREROUTING && \
166 iftable nat -F POSTROUTING && \
167 iftable nat -F OUTPUT && \
168 iftable mangle -F PREROUTING && \
169 iftable mangle -F OUTPUT && \
171 iftable filter -X INPUT && \
172 iftable filter -X FORWARD && \
173 iftable filter -X OUTPUT && \
174 iftable nat -X PREROUTING && \
175 iftable nat -X POSTROUTING && \
176 iftable nat -X OUTPUT && \
177 iftable mangle -X PREROUTING && \
178 iftable mangle -X OUTPUT && \
185 is_yes $IPTABLES_STATUS_NUMERIC && _NUMERIC="-n"
186 is_yes $IPTABLES_STATUS_VERBOSE && _VERBOSE="--verbose"
187 is_yes $IPTABLES_STATUS_LINENUMBERS && _LINES="--line-numbers"
188 tables=`cat /proc/net/ip_tables_names 2>/dev/null`
189 for table in $tables; do
191 iptables -t $table --list $_NUMERIC $_VERBOSE $_LINES
195 msg_usage "$0 {start|stop|restart|force-reload|panic|load|save|clear|status}"