1100280Sgordon#!/bin/sh
225184Sjkh#
3100280Sgordon# Configure routing and miscellaneous network tunables
466830Sobrien#
550472Speter# $FreeBSD$
666830Sobrien#
725184Sjkh
8117019Smtm# PROVIDE: routing
9197527Shrs# REQUIRE: faith netif ppp stf
10250804Sjamie# KEYWORD: nojailvnet
1125184Sjkh
12100280Sgordon. /etc/rc.subr
13179079Sbrooks. /etc/network.subr
1425184Sjkh
15117019Smtmname="routing"
16197719Shrsstart_cmd="routing_start doall"
17117019Smtmstop_cmd="routing_stop"
18117019Smtmextra_commands="options static"
19197719Shrsstatic_cmd="routing_start static"
20197719Shrsoptions_cmd="routing_start options"
2185831Sdes
22251584ShrsROUTE_CMD="/sbin/route"
23197719Shrs
24117019Smtmrouting_start()
25100280Sgordon{
26273188Shrs	local _cmd _af _if _a _ret
27197719Shrs	_cmd=$1
28197719Shrs	_af=$2
29251584Shrs	_if=$3
30273188Shrs	_ret=0
31197719Shrs
32251584Shrs	case $_if in
33251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
34251584Shrs	esac
35197719Shrs
36197719Shrs	case $_af in
37273188Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
38273188Shrs		for _a in inet inet6 atm; do
39273188Shrs			afexists $_a || continue
40273188Shrs			setroutes $_cmd $_a $_if || _ret=1
41273188Shrs		done
42273188Shrs	;;
43273188Shrs	*)
44251584Shrs		if afexists $_af; then
45273188Shrs			setroutes $_cmd $_af $_if || _ret=1
46251584Shrs		else
47251584Shrs			err 1 "Unsupported address family: $_af."
48251584Shrs		fi
49273188Shrs	;;
50197719Shrs	esac
51273188Shrs
52273188Shrs	return $_ret
53117019Smtm}
54117019Smtm
55117019Smtmrouting_stop()
56117019Smtm{
57251584Shrs	local _af _if _a
58197719Shrs	_af=$1
59251584Shrs	_if=$2
60197699Shrs
61251584Shrs	case $_if in
62251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
63251584Shrs	esac
64197719Shrs
65197719Shrs	case $_af in
66251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
67197719Shrs		for _a in inet inet6 ipx atm; do
68197719Shrs			afexists $_a || continue
69251584Shrs			eval static_${_a} delete $_if
70251584Shrs			# When $_if is specified, do not flush routes.
71251584Shrs			if ! [ -n "$_if" ]; then
72251584Shrs				eval routing_stop_${_a}
73251584Shrs			fi
74197719Shrs		done
75273188Shrs	;;
76251584Shrs	*)
77273188Shrs		if afexists $_af; then
78273188Shrs			eval static_${_af} delete $_if 
79273188Shrs			# When $_if is specified, do not flush routes.
80273188Shrs			if ! [ -n "$_if" ]; then
81273188Shrs				eval routing_stop_${_af}
82273188Shrs			fi
83273188Shrs		else
84273188Shrs			err 1 "Unsupported address family: $_af."
85273188Shrs		fi
86273188Shrs	;;
87197719Shrs	esac
88197699Shrs}
89197699Shrs
90197719Shrssetroutes()
91197719Shrs{
92197719Shrs	case $1 in
93197719Shrs	static)
94251584Shrs		static_$2 add $3
95197719Shrs		;;
96197719Shrs	options)
97197719Shrs		options_$2
98197719Shrs		;;
99197719Shrs	doall)
100251584Shrs		static_$2 add $3
101197719Shrs		options_$2
102197719Shrs		;;
103197719Shrs	esac
104197719Shrs}
105197719Shrs
106197699Shrsrouting_stop_inet()
107197699Shrs{
108251584Shrs	${ROUTE_CMD} -n flush -inet
109197699Shrs}
110197699Shrs
111197699Shrsrouting_stop_inet6()
112197699Shrs{
113197699Shrs	local i
114197699Shrs
115251584Shrs	${ROUTE_CMD} -n flush -inet6
116230991Shrs	for i in `list_net_interfaces`; do
117230991Shrs		if ipv6if $i; then
118230991Shrs			ifconfig $i inet6 -defaultif
119230991Shrs		fi
120197139Shrs	done
121117019Smtm}
122117019Smtm
123197719Shrsrouting_stop_atm()
124117019Smtm{
125197719Shrs	return 0
126197139Shrs}
127197139Shrs
128197719Shrsrouting_stop_ipx()
129197139Shrs{
130197719Shrs	return 0
131197139Shrs}
132197139Shrs
133197699Shrsstatic_inet()
134197139Shrs{
135251584Shrs	local _action _if _skip
136197139Shrs	_action=$1
137251584Shrs	_if=$2
138197139Shrs
139251584Shrs	# Add default route.
14051231Ssheldonh	case ${defaultrouter} in
14151231Ssheldonh	[Nn][Oo] | '')
14251231Ssheldonh		;;
14351231Ssheldonh	*)
144255163Sdelphij		static_routes="${static_routes} _default"
145251584Shrs		route__default="default ${defaultrouter}"
14651231Ssheldonh		;;
14751231Ssheldonh	esac
14840006Sphk
149251584Shrs	# Install configured routes.
15051231Ssheldonh	if [ -n "${static_routes}" ]; then
15151231Ssheldonh		for i in ${static_routes}; do
152251584Shrs			_skip=0
153251584Shrs			if [ -n "$_if" ]; then
154251584Shrs				case $i in
155251584Shrs				*:$_if)	;;
156251584Shrs				*)	_skip=1 ;;
157251584Shrs				esac
158251584Shrs			fi
159251584Shrs			if [ $_skip = 0 ]; then
160251584Shrs				route_args=`get_if_var ${i%:*} route_IF`
161251584Shrs				if [ -n "$route_args" ]; then
162251584Shrs					${ROUTE_CMD} ${_action} ${route_args}
163251584Shrs				else
164251584Shrs					warn "route_${i%:*} not found."
165251584Shrs				fi
166251584Shrs			fi
16751231Ssheldonh		done
16851231Ssheldonh	fi
169197139Shrs}
170197139Shrs
171197699Shrsstatic_inet6()
172197139Shrs{
173251584Shrs	local _action _if _skip fibmod fibs
174197139Shrs	_action=$1
175251584Shrs	_if=$2
176197139Shrs
177231852Sbz	# get the number of FIBs supported.
178243188Shrs	fibs=$((`${SYSCTL_N} net.fibs` - 1))
179243212Shrs	if [ "$fibs" -gt 0 ]; then
180243188Shrs		fibmod="-fib 0-$fibs"
181243188Shrs	else
182243188Shrs		fibmod=
183243188Shrs	fi
184231852Sbz
185251584Shrs	# Add pre-defined static routes first.
186251584Shrs	ipv6_static_routes="_v4mapped _v4compat ${ipv6_static_routes}"
187251584Shrs	ipv6_static_routes="_lla _llma ${ipv6_static_routes}"
188251584Shrs
189197139Shrs	# disallow "internal" addresses to appear on the wire
190251584Shrs	ipv6_route__v4mapped="::ffff:0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
191251584Shrs	ipv6_route__v4compat="::0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
192197139Shrs
193251584Shrs	# Disallow link-local unicast packets without outgoing scope
194251584Shrs	# identifiers.  However, if you set "ipv6_default_interface",
195251584Shrs	# for the host case, you will allow to omit the identifiers.
196251584Shrs	# Under this configuration, the packets will go to the default
197251584Shrs	# interface.
198251584Shrs	ipv6_route__lla="fe80:: -prefixlen 10 ::1 -reject ${fibmod}"
199251584Shrs	ipv6_route__llma="ff02:: -prefixlen 16 ::1 -reject ${fibmod}"
200251584Shrs
201251584Shrs	# Add default route.
202197139Shrs	case ${ipv6_defaultrouter} in
203197139Shrs	[Nn][Oo] | '')
204197139Shrs		;;
205197139Shrs	*)
206255163Sdelphij		ipv6_static_routes="${ipv6_static_routes} _default"
207251584Shrs		ipv6_route__default="default ${ipv6_defaultrouter}"
208197139Shrs		;;
209197139Shrs	esac
210197139Shrs
211251584Shrs	# Install configured routes.
212197139Shrs	if [ -n "${ipv6_static_routes}" ]; then
213197139Shrs		for i in ${ipv6_static_routes}; do
214251584Shrs			_skip=0
215251584Shrs			if [ -n "$_if" ]; then
216251584Shrs				case $i in
217251584Shrs				*:$_if)	;;
218251584Shrs				*)	_skip=1 ;;
219251584Shrs				esac
220251584Shrs			fi
221251584Shrs			if [ $_skip = 0 ]; then
222251584Shrs				ipv6_route_args=`get_if_var ${i%:*} ipv6_route_IF`
223251584Shrs				if [ -n "$ipv6_route_args" ]; then
224251584Shrs					${ROUTE_CMD} ${_action} \
225251584Shrs						-inet6 ${ipv6_route_args}
226251584Shrs				else
227251584Shrs					warn "route_${i%:*} not found"
228251584Shrs				fi
229251584Shrs			fi
230197139Shrs		done
231197139Shrs	fi
232197139Shrs
233251584Shrs	# Install the "default interface" to kernel, which will be used
234251584Shrs	# as the default route when there's no router.
235197139Shrs
236251584Shrs	# Disable installing the default interface when we act
237251584Shrs	# as router to avoid conflict between the default
238251584Shrs	# router list and the manual configured default route.
239197139Shrs	if checkyesno ipv6_gateway_enable; then
240251584Shrs		return
241197139Shrs	fi
242197139Shrs
243197139Shrs	case "${ipv6_default_interface}" in
244197139Shrs	[Nn][Oo] | [Nn][Oo][Nn][Ee])
245251584Shrs		return
246197139Shrs		;;
247197139Shrs	[Aa][Uu][Tt][Oo] | "")
248197139Shrs		for i in ${ipv6_network_interfaces}; do
249197139Shrs			case $i in
250251584Shrs			[Nn][Oo][Nn][Ee])
251251584Shrs				return
252251584Shrs				;;
253197139Shrs			lo0|faith[0-9]*)
254197139Shrs				continue
255197139Shrs				;;
256197139Shrs			esac
257197139Shrs			laddr=`network6_getladdr $i exclude_tentative`
258197139Shrs			case ${laddr} in
259197139Shrs			'')
260197139Shrs				;;
261197139Shrs			*)
262197139Shrs				ipv6_default_interface=$i
263197139Shrs				break
264197139Shrs				;;
265197139Shrs			esac
266197139Shrs		done
267197139Shrs		;;
268197139Shrs	esac
269197139Shrs
270251584Shrs	ifconfig ${ipv6_default_interface} inet6 defaultif
271251584Shrs	sysctl net.inet6.ip6.use_defaultzone=1
272197139Shrs}
273197139Shrs
274197699Shrsstatic_atm()
275197139Shrs{
276197699Shrs	local _action i route_args
277197139Shrs	_action=$1
278197139Shrs
279118908Sharti	if [ -n "${natm_static_routes}" ]; then
280118908Sharti		for i in ${natm_static_routes}; do
281197139Shrs			route_args=`get_if_var $i route_IF`
282251584Shrs			if [ -n "$route_args" ]; then
283251584Shrs				atmconfig natm ${_action} ${route_args}
284251584Shrs			else
285251584Shrs				warn "route_${i} not found."
286251584Shrs			fi
287118908Sharti		done
288118908Sharti	fi
289117019Smtm}
29029300Sdanny
291197719Shrsstatic_ipx()
292197719Shrs{
293227366Sjilles	:
294197719Shrs}
295197719Shrs
296179940Smtmropts_init()
297179940Smtm{
298179940Smtm	if [ -z "${_ropts_initdone}" ]; then
299224132Sjilles		echo -n "Additional $1 routing options:"
300179940Smtm		_ropts_initdone=yes
301179940Smtm	fi
302179940Smtm}
303179940Smtm
304197699Shrsoptions_inet()
305197699Shrs{
306224132Sjilles	_ropts_initdone=
307197139Shrs	if checkyesno icmp_bmcastecho; then
308224132Sjilles		ropts_init inet
30951231Ssheldonh		echo -n ' broadcast ping responses=YES'
310220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
311197699Shrs	else
312220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
313197139Shrs	fi
31445096Simp
315197139Shrs	if checkyesno icmp_drop_redirect; then
316224132Sjilles		ropts_init inet
31751231Ssheldonh		echo -n ' ignore ICMP redirect=YES'
318220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
319197699Shrs	else
320220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
321197139Shrs	fi
32239267Sjkoshy
323197139Shrs	if checkyesno icmp_log_redirect; then
324224132Sjilles		ropts_init inet
32551231Ssheldonh		echo -n ' log ICMP redirect=YES'
326220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
327197699Shrs	else
328220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
329197139Shrs	fi
33033439Sguido
331197139Shrs	if checkyesno gateway_enable; then
332224132Sjilles		ropts_init inet
333224132Sjilles		echo -n ' gateway=YES'
334220153Semaste		${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
335197699Shrs	else
336220153Semaste		${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
337197139Shrs	fi
33833439Sguido
339197139Shrs	if checkyesno forward_sourceroute; then
340224132Sjilles		ropts_init inet
34151231Ssheldonh		echo -n ' do source routing=YES'
342220153Semaste		${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
343197699Shrs	else
344220153Semaste		${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
345197139Shrs	fi
34647752Sphk
347197139Shrs	if checkyesno accept_sourceroute; then
348224132Sjilles		ropts_init inet
34951231Ssheldonh		echo -n ' accept source routing=YES'
350220153Semaste		${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
351197699Shrs	else
352220153Semaste		${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
353197139Shrs	fi
35451209Sdes
355197699Shrs	if checkyesno arpproxy_all; then
356224132Sjilles		ropts_init inet
357197699Shrs		echo -n ' ARP proxyall=YES'
358220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
359197699Shrs	else
360220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
361197139Shrs	fi
362224132Sjilles
363224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
364197699Shrs}
36551231Ssheldonh
366197699Shrsoptions_inet6()
367197699Shrs{
368224132Sjilles	_ropts_initdone=
369224132Sjilles
370197699Shrs	if checkyesno ipv6_gateway_enable; then
371224132Sjilles		ropts_init inet6
372224132Sjilles		echo -n ' gateway=YES'
373220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
374197699Shrs	else
375220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
376197139Shrs	fi
377224132Sjilles
378224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
379197699Shrs}
38061961Sdillon
381197719Shrsoptions_atm()
382197719Shrs{
383224132Sjilles	_ropts_initdone=
384224132Sjilles
385224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
386197719Shrs}
387197719Shrs
388197699Shrsoptions_ipx()
389197699Shrs{
390224132Sjilles	_ropts_initdone=
391224132Sjilles
392197699Shrs	if checkyesno ipxgateway_enable; then
393224132Sjilles		ropts_init ipx
394224132Sjilles		echo -n ' gateway=YES'
395220153Semaste		${SYSCTL} net.ipx.ipx.ipxforwarding=1 > /dev/null
396197699Shrs	else
397220153Semaste		${SYSCTL} net.ipx.ipx.ipxforwarding=0 > /dev/null
398197699Shrs	fi
399224132Sjilles
400224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
40125184Sjkh}
40225184Sjkh
403100280Sgordonload_rc_config $name
404197139Shrsrun_rc_command "$@"
405