Improve the IPFilter rc.d file, mainly bring it in line with changes
[dragonfly.git] / etc / rc.d / ipfilter
1 #!/bin/sh
2 #
3 # $NetBSD: ipfilter,v 1.10 2001/02/28 17:03:50 lukem Exp $
4 # $FreeBSD: src/etc/rc.d/ipfilter,v 1.10 2003/04/30 02:54:17 mtm Exp $
5 # $DragonFly: src/etc/rc.d/ipfilter,v 1.4 2005/02/25 18:14:38 hmp Exp $
6 #
7
8 # PROVIDE: ipfilter
9 # REQUIRE: root beforenetlkm mountcritlocal tty ipmon
10 # BEFORE:  netif
11 # KEYWORD: DragonFly nojail
12
13 . /etc/rc.subr
14
15 name="ipfilter"
16 rcvar=`set_rcvar`
17 load_rc_config $name
18 stop_precmd="test -f ${ipfilter_rules} -o -f ${ipv6_ipfilter_rules}"
19
20 start_precmd="ipfilter_prestart"
21 start_cmd="ipfilter_start"
22 stop_cmd="ipfilter_stop"
23 reload_precmd="$stop_precmd"
24 reload_cmd="ipfilter_reload"
25 resync_precmd="$stop_precmd"
26 resync_cmd="ipfilter_resync"
27 status_precmd="$stop_precmd"
28 status_cmd="ipfilter_status"
29 extra_commands="reload resync status"
30
31 ipfilter_loaded()
32 {
33         if ! kldstat -v | grep "IP Filter" > /dev/null 2>&1; then
34                 return 1
35         else
36                 return 0
37         fi
38 }
39
40 ipfilter_prestart()
41 {
42         # load ipfilter kernel module if needed
43         if ! ipfilter_loaded; then
44                 if kldload ipl; then
45                         info 'IP-filter module loaded.'
46                 else
47                         err 1 'IP-filter module failed to load.'
48                 fi
49         fi
50
51         # check for ipfilter rules
52         if [ ! -r "${ipfilter_rules}" ] && [ ! -r "${ipv6_ipfilter_rules}" ]
53         then
54                 warn 'IP-filter: NO IPF RULES'
55                 return 1
56         fi
57         return 0
58 }
59
60 ipfilter_start()
61 {
62         echo "Enabling ipfilter."
63         if [ `sysctl -n net.inet.ipf.fr_running` -eq 0 ]; then
64                 ${ipfilter_program:-/sbin/ipf} -E
65         fi
66         ${ipfilter_program:-/sbin/ipf} -Fa
67         if [ -r "${ipfilter_rules}" ]; then
68                 ${ipfilter_program:-/sbin/ipf} \
69                     -f "${ipfilter_rules}" ${ipfilter_flags}
70         fi
71         ${ipfilter_program:-/sbin/ipf} -6 -Fa
72         if [ -r "${ipv6_ipfilter_rules}" ]; then
73                 ${ipfilter_program:-/sbin/ipf} -6 \
74                     -f "${ipv6_ipfilter_rules}" ${ipfilter_flags}
75         fi
76 }
77
78 ipfilter_stop()
79 {
80         # XXX - The ipf -D command is not effective for 'lkm's
81         if [ `sysctl -n net.inet.ipf.fr_running` -eq 1 ]; then
82                 echo "Saving firewall state tables"
83                 ${ipfs_program:-/sbin/ipfs} -W ${ipfs_flags}
84                 echo "Disabling ipfilter."
85                 ${ipfilter_program:-/sbin/ipf} -D
86         fi
87 }
88
89 ipfilter_reload()
90 {
91         echo "Reloading ipfilter rules."
92
93         ${ipfilter_program:-/sbin/ipf} -I -Fa
94         if [ -r "${ipfilter_rules}" ]; then
95                 ${ipfilter_program:-/sbin/ipf} -I \
96                     -f "${ipfilter_rules}" ${ipfilter_flags}
97         fi
98         ${ipfilter_program:-/sbin/ipf} -I -6 -Fa
99         if [ -r "${ipv6_ipfilter_rules}" ]; then
100                 ${ipfilter_program:-/sbin/ipf} -I -6 \
101                     -f "${ipv6_ipfilter_rules}" ${ipfilter_flags}
102         fi
103         ${ipfilter_program:-/sbin/ipf} -s
104
105 }
106
107 ipfilter_resync()
108 {
109         # Don't resync if ipfilter is not loaded
110         if ! ipfilter_loaded; then
111                  return
112         fi
113         ${ipfilter_program:-/sbin/ipf} -y ${ipfilter_flags}
114 }
115
116 ipfilter_status()
117 {
118         ${ipfilter_program:-/sbin/ipf} -V
119 }
120
121 run_rc_command "$1"