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