xref: /freebsd/libexec/rc/rc.d/ipfw (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5
6# PROVIDE: ipfw
7# REQUIRE: ppp
8# KEYWORD: nojailvnet
9
10. /etc/rc.subr
11. /etc/network.subr
12
13name="ipfw"
14desc="Firewall, traffic shaper, packet scheduler, in-kernel NAT"
15rcvar="firewall_enable"
16start_cmd="ipfw_start"
17start_precmd="ipfw_prestart"
18start_postcmd="ipfw_poststart"
19stop_cmd="ipfw_stop"
20status_cmd="ipfw_status"
21required_modules="ipfw"
22extra_commands="status"
23
24set_rcvar_obsolete ipv6_firewall_enable
25
26ipfw_prestart()
27{
28	if checkyesno dummynet_enable; then
29		required_modules="$required_modules dummynet"
30	fi
31	if checkyesno natd_enable; then
32		required_modules="$required_modules ipdivert"
33	fi
34	if checkyesno firewall_nat_enable; then
35		required_modules="$required_modules ipfw_nat"
36	fi
37	if checkyesno firewall_nat64_enable; then
38		required_modules="$required_modules ipfw_nat64"
39	fi
40	if checkyesno firewall_nptv6_enable; then
41		required_modules="$required_modules ipfw_nptv6"
42	fi
43	if checkyesno firewall_pmod_enable; then
44		required_modules="$required_modules ipfw_pmod"
45	fi
46}
47
48ipfw_start()
49{
50	local   _firewall_type _module _sysctl_reload
51
52	if [ -n "${1}" ]; then
53		_firewall_type=$1
54	else
55		_firewall_type=${firewall_type}
56	fi
57
58	_sysctl_reload=no
59	for _module in ${required_modules}
60	do
61		if kldstat -qn ${_module}; then
62			_sysctl_reload=yes
63			break
64		fi
65	done
66
67	if [ ${_sysctl_reload} = yes ]; then
68		/etc/rc.d/sysctl reload
69	fi
70
71	# set the firewall rules script if none was specified
72	[ -z "${firewall_script}" ] && firewall_script=/etc/rc.firewall
73
74	if [ -r "${firewall_script}" ]; then
75		/bin/sh "${firewall_script}" "${_firewall_type}"
76		echo 'Firewall rules loaded.'
77	elif [ "`ipfw list 65535`" = "65535 deny ip from any to any" ]; then
78		echo 'Warning: kernel has firewall functionality, but' \
79		    'firewall rules are not enabled.'
80		echo '           All ip services are disabled.'
81	fi
82
83	# Firewall logging
84	#
85	if checkyesno firewall_logging; then
86		echo 'Firewall logging enabled.'
87		${SYSCTL} net.inet.ip.fw.verbose=1 >/dev/null
88	fi
89	if checkyesno firewall_logif; then
90		if ! ifconfig ipfw0 >/dev/null 2>&1; then
91			ifconfig ipfw0 create
92			echo 'Firewall logging pseudo-interface (ipfw0)' \
93			    'created.'
94		else
95			echo 'Firewall logging pseudo-interface (ipfw0)' \
96			    'already created.'
97		fi
98	fi
99}
100
101ipfw_poststart()
102{
103	local	_coscript
104
105	# Start firewall coscripts
106	#
107	for _coscript in ${firewall_coscripts} ; do
108		if [ -f "${_coscript}" ]; then
109			${_coscript} quietstart
110		fi
111	done
112
113	# Enable the firewall
114	#
115	if ! ${SYSCTL} net.inet.ip.fw.enable=1 >/dev/null 2>&1; then
116		warn "failed to enable IPv4 firewall"
117	fi
118	if afexists inet6; then
119		if ! ${SYSCTL} net.inet6.ip6.fw.enable=1 >/dev/null 2>&1
120		then
121			warn "failed to enable IPv6 firewall"
122		fi
123	fi
124}
125
126ipfw_stop()
127{
128	local	_coscript
129
130	# Disable the firewall
131	#
132	${SYSCTL} net.inet.ip.fw.enable=0 >/dev/null
133	if afexists inet6; then
134		${SYSCTL} net.inet6.ip6.fw.enable=0 >/dev/null
135	fi
136
137	# Stop firewall coscripts
138	#
139	for _coscript in `reverse_list ${firewall_coscripts}` ; do
140		if [ -f "${_coscript}" ]; then
141			${_coscript} quietstop
142		fi
143	done
144}
145
146ipfw_status()
147{
148	status=$(sysctl -i -n net.inet.ip.fw.enable)
149	: ${status:=0}
150	if afexists inet6; then
151		status6=$(sysctl -i -n net.inet6.ip6.fw.enable)
152		: ${status6:=0}
153		status=$((${status} + ${status6}))
154	fi
155	if [ ${status} -eq 0 ]; then
156		echo "ipfw is not enabled"
157		exit 1
158	else
159		echo "ipfw is enabled"
160		exit 0
161	fi
162}
163
164load_rc_config $name
165firewall_coscripts="/etc/rc.d/natd ${firewall_coscripts}"
166
167run_rc_command $*
168