3 # iptables Start iptables firewall
5 # chkconfig: 2345 08 92
6 # description: Starts, stops and saves iptables firewall
8 # config: /etc/sysconfig/iptables
9 # config: /etc/sysconfig/iptables-config
15 # Default-Start: 2 3 4 5
17 # Short-Description: start and stop iptables firewall
18 # Description: Start, stop and save iptables firewall
21 # Source function library.
22 . /etc/init.d/functions
25 IPTABLES_DATA=/etc/sysconfig/$IPTABLES
26 IPTABLES_CONFIG=/etc/sysconfig/${IPTABLES}-config
27 IPV=${IPTABLES%tables} # ip for ipv4 | ip6 for ipv6
28 [ "$IPV" = "ip" ] && _IPV="ipv4" || _IPV="ipv6"
29 PROC_IPTABLES_NAMES=/proc/net/${IPV}_tables_names
30 VAR_SUBSYS_IPTABLES=/var/lock/subsys/$IPTABLES
32 if [ ! -x /sbin/$IPTABLES ]; then
33 echo -n $"${IPTABLES}: /sbin/$IPTABLES does not exist."; warning; echo
38 /sbin/modprobe --version 2>&1 | grep -q module-init-tools \
42 # Default firewall configuration:
44 IPTABLES_MODULES_UNLOAD="yes"
45 IPTABLES_SAVE_ON_STOP="no"
46 IPTABLES_SAVE_ON_RESTART="no"
47 IPTABLES_SAVE_COUNTER="no"
48 IPTABLES_STATUS_NUMERIC="yes"
49 IPTABLES_STATUS_VERBOSE="no"
50 IPTABLES_STATUS_LINENUMBERS="yes"
52 # Load firewall configuration.
53 [ -f "$IPTABLES_CONFIG" ] && . "$IPTABLES_CONFIG"
56 NF_MODULES=($(lsmod | awk "/^${IPV}table_/ {print \$1}") ${IPV}_tables)
57 NF_MODULES_COMMON=(x_tables nf_nat nf_conntrack) # Used by netfilter v4 and v6
60 NF_TABLES=$(cat "$PROC_IPTABLES_NAMES" 2>/dev/null)
64 # Unload module with all referring modules.
65 # At first all referring modules will be unloaded, then the module itself.
70 # Get referring modules.
71 # New modutils have another output format.
72 [ $NEW_MODUTILS = 1 ] \
73 && ref=$(lsmod | awk "/^${mod}/ { print \$4; }" | tr ',' ' ') \
74 || ref=$(lsmod | grep ^${mod} | cut -d "[" -s -f 2 | cut -d "]" -s -f 1)
76 # recursive call for all referring modules
83 # The extra test is for 2.6: The module might have autocleaned,
84 # after all referring modules are unloaded.
85 if grep -q "^${mod}" /proc/modules ; then
86 modprobe -r $mod > /dev/null 2>&1
88 [ $res -eq 0 ] || echo -n " $mod"
96 # Flush firewall rules and delete chains.
97 [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
99 # Check if firewall is configured (has tables)
100 [ -z "$NF_TABLES" ] && return 1
102 echo -n $"${IPTABLES}: Flushing firewall rules: "
105 for i in $NF_TABLES; do
106 # Flush firewall rules.
110 # Delete firewall chains.
114 # Set counter to zero.
119 [ $ret -eq 0 ] && success || failure
125 # Set policy for configured tables.
128 # Check if iptable module is loaded
129 [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
131 # Check if firewall is configured (has tables)
132 tables=$(cat "$PROC_IPTABLES_NAMES" 2>/dev/null)
133 [ -z "$tables" ] && return 1
135 echo -n $"${IPTABLES}: Setting chains to policy $policy: "
141 $IPTABLES -t raw -P PREROUTING $policy \
142 && $IPTABLES -t raw -P OUTPUT $policy \
146 $IPTABLES -t filter -P INPUT $policy \
147 && $IPTABLES -t filter -P OUTPUT $policy \
148 && $IPTABLES -t filter -P FORWARD $policy \
152 $IPTABLES -t nat -P PREROUTING $policy \
153 && $IPTABLES -t nat -P POSTROUTING $policy \
154 && $IPTABLES -t nat -P OUTPUT $policy \
158 $IPTABLES -t mangle -P PREROUTING $policy \
159 && $IPTABLES -t mangle -P POSTROUTING $policy \
160 && $IPTABLES -t mangle -P INPUT $policy \
161 && $IPTABLES -t mangle -P OUTPUT $policy \
162 && $IPTABLES -t mangle -P FORWARD $policy \
171 [ $ret -eq 0 ] && success || failure
177 # Do not start if there is no config file.
178 [ ! -f "$IPTABLES_DATA" ] && return 6
180 # check if ipv6 module load is deactivated
181 if [ "${_IPV}" = "ipv6" ] \
182 && grep -qIsE "^install[[:space:]]+${_IPV}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
183 echo $"${IPTABLES}: ${_IPV} is disabled."
187 echo -n $"${IPTABLES}: Applying firewall rules: "
190 [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
192 $IPTABLES-restore $OPT $IPTABLES_DATA
193 if [ $? -eq 0 ]; then
196 failure; echo; return 1
199 # Tuntap initialization
201 if [ -z "$taps" -a -r /etc/planetlab/node_id ] ; then
202 # If this node is not "virtually multi-homed", just bring up
203 # the tap interface with a PLB private address. The PLB
204 # convention is to assign a unique 10.x.y.0/24 network to each
205 # node where x.y is the PlanetLab node ID of the machine in
208 # x = (node_id / 256) % 256
211 node_id=$(cat /etc/planetlab/node_id)
213 tap0=$(printf 10.%d.%d.1 $((($node_id / 256) % 256)) $(($node_id % 256)))
217 # Load additional modules (helpers)
218 if [ -n "$IPTABLES_MODULES" ]; then
219 echo -n $"${IPTABLES}: Loading additional modules: "
221 for mod in $IPTABLES_MODULES; do
223 modprobe $mod > /dev/null 2>&1
226 [ $ret -eq 0 ] && success || failure
230 for tap in $taps ; do
231 # Configuration for this tap (address/proxy)
236 # Set MAC address to something predictable
237 mac=$(printf 00:FF:%X:%X:%X:%X $(echo $addr | sed -e 's/\./ /g'))
239 # Bring up this interface. Optimize the MTU for the PlanetLab
240 # Backbone (1500/Ethernet - 4/GRE - 8/UDP - 20/IP = 1468).
241 ifconfig $tap down && \
242 ifconfig $tap hw ether $mac mtu 1468 && \
243 ifconfig $tap $addr ${proxy:+pointopoint $proxy} netmask ${tapmask:=255.255.255.255} up
245 # Stuffing the proxy for this address in the pointopoint field
246 # creates a static route to the proxy that we do not want
248 if [ -n "$proxy" -a "$proxy" != "$addr" ] ; then
252 # Enable route through this interface
253 ip route add default dev $tap tab 1 && \
254 ip rule add from $addr tab 1
258 touch $VAR_SUBSYS_IPTABLES
263 # Do not stop if iptables module is not loaded.
264 [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
269 if [ "x$IPTABLES_MODULES_UNLOAD" = "xyes" ]; then
270 echo -n $"${IPTABLES}: Unloading modules: "
272 for mod in ${NF_MODULES[*]}; do
276 # try to unload remaining netfilter modules used by ipv4 and ipv6
278 for mod in ${NF_MODULES_COMMON[*]}; do
279 rmmod_r $mod >/dev/null
281 [ $ret -eq 0 ] && success || failure
285 # Take down vnet interfaces
286 for dev in $taps tap0 ; do
287 action $"Shutting down interface $dev: " \
288 ifconfig $dev 0.0.0.0 down
291 rm -f $VAR_SUBSYS_IPTABLES
296 # Check if iptable module is loaded
297 [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
299 # Check if firewall is configured (has tables)
300 [ -z "$NF_TABLES" ] && return 6
302 echo -n $"${IPTABLES}: Saving firewall rules to $IPTABLES_DATA: "
305 [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
308 TMP_FILE=$(/bin/mktemp -q /tmp/$IPTABLES.XXXXXX) \
309 && chmod 600 "$TMP_FILE" \
310 && $IPTABLES-save $OPT > $TMP_FILE 2>/dev/null \
311 && size=$(stat -c '%s' $TMP_FILE) && [ $size -gt 0 ] \
313 if [ $ret -eq 0 ]; then
314 if [ -e $IPTABLES_DATA ]; then
315 cp -f $IPTABLES_DATA $IPTABLES_DATA.save \
316 && chmod 600 $IPTABLES_DATA.save \
319 if [ $ret -eq 0 ]; then
320 cp -f $TMP_FILE $IPTABLES_DATA \
321 && chmod 600 $IPTABLES_DATA \
325 [ $ret -eq 0 ] && success || failure
332 if [ ! -f "$VAR_SUBSYS_IPTABLES" -a -z "$NF_TABLES" ]; then
333 echo $"${IPTABLES}: Firewall is not running."
337 # Do not print status if lockfile is missing and iptables modules are not
339 # Check if iptable modules are loaded
340 if [ ! -e "$PROC_IPTABLES_NAMES" ]; then
341 echo $"${IPTABLES}: Firewall modules are not loaded."
345 # Check if firewall is configured (has tables)
346 if [ -z "$NF_TABLES" ]; then
347 echo $"${IPTABLES}: Firewall is not configured. "
352 [ "x$IPTABLES_STATUS_NUMERIC" = "xyes" ] && NUM="-n"
354 [ "x$IPTABLES_STATUS_VERBOSE" = "xyes" ] && VERBOSE="--verbose"
356 [ "x$IPTABLES_STATUS_LINENUMBERS" = "xyes" ] && COUNT="--line-numbers"
358 for table in $NF_TABLES; do
359 echo $"Table: $table"
360 $IPTABLES -t $table --list $NUM $VERBOSE $COUNT && echo
367 [ "x$IPTABLES_SAVE_ON_RESTART" = "xyes" ] && save
375 [ -f "$VAR_SUBSYS_IPTABLES" ] && exit 0
380 [ "x$IPTABLES_SAVE_ON_STOP" = "xyes" ] && save
384 restart|force-reload)
388 condrestart|try-restart)
389 [ ! -e "$VAR_SUBSYS_IPTABLES" ] && exit 0
407 echo $"Usage: ${IPTABLES} {start|stop|restart|condrestart|status|panic|save}"