routing revision 251584
1100280Sgordon#!/bin/sh
225184Sjkh#
3100280Sgordon# Configure routing and miscellaneous network tunables
466830Sobrien#
550472Speter# $FreeBSD: head/etc/rc.d/routing 251584 2013-06-09 18:11:36Z hrs $
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{
26251584Shrs	local _cmd _af _if _a
27197719Shrs	_cmd=$1
28197719Shrs	_af=$2
29251584Shrs	_if=$3
30197719Shrs
31251584Shrs	case $_if in
32251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
33251584Shrs	esac
34197719Shrs
35197719Shrs	case $_af in
36197719Shrs	inet|inet6|ipx|atm)
37251584Shrs		if afexists $_af; then
38251584Shrs			setroutes $_cmd $_af $_if
39251584Shrs		else
40251584Shrs			err 1 "Unsupported address family: $_af."
41251584Shrs		fi
42197719Shrs		;;
43251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
44197719Shrs		for _a in inet inet6 ipx atm; do
45251584Shrs			afexists $_a && setroutes $_cmd $_a $_if
46197719Shrs		done
47197719Shrs		;;
48251584Shrs	*)
49251584Shrs		err 1 "Unsupported address family: $_af."
50251584Shrs		;;
51197719Shrs	esac
52117019Smtm}
53117019Smtm
54117019Smtmrouting_stop()
55117019Smtm{
56251584Shrs	local _af _if _a
57197719Shrs	_af=$1
58251584Shrs	_if=$2
59197699Shrs
60251584Shrs	case $_if in
61251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
62251584Shrs	esac
63197719Shrs
64197719Shrs	case $_af in
65197719Shrs	inet|inet6|ipx|atm)
66251584Shrs		if afexists $_af; then
67251584Shrs			eval static_${_af} delete $_if 
68251584Shrs			# When $_if is specified, do not flush routes.
69251584Shrs			if ! [ -n "$_if" ]; then
70251584Shrs				eval routing_stop_${_af}
71251584Shrs			fi
72251584Shrs		else
73251584Shrs			err 1 "Unsupported address family: $_af."
74251584Shrs		fi
75197719Shrs		;;
76251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
77197719Shrs		for _a in inet inet6 ipx atm; do
78197719Shrs			afexists $_a || continue
79251584Shrs			eval static_${_a} delete $_if
80251584Shrs			# When $_if is specified, do not flush routes.
81251584Shrs			if ! [ -n "$_if" ]; then
82251584Shrs				eval routing_stop_${_a}
83251584Shrs			fi
84197719Shrs		done
85197719Shrs		;;
86251584Shrs	*)
87251584Shrs		err 1 "Unsupported address family: $_af."
88251584Shrs		;;
89197719Shrs	esac
90197699Shrs}
91197699Shrs
92197719Shrssetroutes()
93197719Shrs{
94197719Shrs	case $1 in
95197719Shrs	static)
96251584Shrs		static_$2 add $3
97197719Shrs		;;
98197719Shrs	options)
99197719Shrs		options_$2
100197719Shrs		;;
101197719Shrs	doall)
102251584Shrs		static_$2 add $3
103197719Shrs		options_$2
104197719Shrs		;;
105197719Shrs	esac
106197719Shrs}
107197719Shrs
108197699Shrsrouting_stop_inet()
109197699Shrs{
110251584Shrs	${ROUTE_CMD} -n flush -inet
111197699Shrs}
112197699Shrs
113197699Shrsrouting_stop_inet6()
114197699Shrs{
115197699Shrs	local i
116197699Shrs
117251584Shrs	${ROUTE_CMD} -n flush -inet6
118230991Shrs	for i in `list_net_interfaces`; do
119230991Shrs		if ipv6if $i; then
120230991Shrs			ifconfig $i inet6 -defaultif
121230991Shrs		fi
122197139Shrs	done
123117019Smtm}
124117019Smtm
125197719Shrsrouting_stop_atm()
126117019Smtm{
127197719Shrs	return 0
128197139Shrs}
129197139Shrs
130197719Shrsrouting_stop_ipx()
131197139Shrs{
132197719Shrs	return 0
133197139Shrs}
134197139Shrs
135197699Shrsstatic_inet()
136197139Shrs{
137251584Shrs	local _action _if _skip
138197139Shrs	_action=$1
139251584Shrs	_if=$2
140197139Shrs
141251584Shrs	# Add default route.
14251231Ssheldonh	case ${defaultrouter} in
14351231Ssheldonh	[Nn][Oo] | '')
14451231Ssheldonh		;;
14551231Ssheldonh	*)
146251584Shrs		static_routes="_default ${static_routes}"
147251584Shrs		route__default="default ${defaultrouter}"
14851231Ssheldonh		;;
14951231Ssheldonh	esac
15040006Sphk
151251584Shrs	# Install configured routes.
15251231Ssheldonh	if [ -n "${static_routes}" ]; then
15351231Ssheldonh		for i in ${static_routes}; do
154251584Shrs			_skip=0
155251584Shrs			if [ -n "$_if" ]; then
156251584Shrs				case $i in
157251584Shrs				*:$_if)	;;
158251584Shrs				*)	_skip=1 ;;
159251584Shrs				esac
160251584Shrs			fi
161251584Shrs			if [ $_skip = 0 ]; then
162251584Shrs				route_args=`get_if_var ${i%:*} route_IF`
163251584Shrs				if [ -n "$route_args" ]; then
164251584Shrs					${ROUTE_CMD} ${_action} ${route_args}
165251584Shrs				else
166251584Shrs					warn "route_${i%:*} not found."
167251584Shrs				fi
168251584Shrs			fi
16951231Ssheldonh		done
17051231Ssheldonh	fi
171197139Shrs}
172197139Shrs
173197699Shrsstatic_inet6()
174197139Shrs{
175251584Shrs	local _action _if _skip fibmod fibs
176197139Shrs	_action=$1
177251584Shrs	_if=$2
178197139Shrs
179231852Sbz	# get the number of FIBs supported.
180243188Shrs	fibs=$((`${SYSCTL_N} net.fibs` - 1))
181243212Shrs	if [ "$fibs" -gt 0 ]; then
182243188Shrs		fibmod="-fib 0-$fibs"
183243188Shrs	else
184243188Shrs		fibmod=
185243188Shrs	fi
186231852Sbz
187251584Shrs	# Add pre-defined static routes first.
188251584Shrs	ipv6_static_routes="_v4mapped _v4compat ${ipv6_static_routes}"
189251584Shrs	ipv6_static_routes="_lla _llma ${ipv6_static_routes}"
190251584Shrs
191197139Shrs	# disallow "internal" addresses to appear on the wire
192251584Shrs	ipv6_route__v4mapped="::ffff:0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
193251584Shrs	ipv6_route__v4compat="::0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
194197139Shrs
195251584Shrs	# Disallow link-local unicast packets without outgoing scope
196251584Shrs	# identifiers.  However, if you set "ipv6_default_interface",
197251584Shrs	# for the host case, you will allow to omit the identifiers.
198251584Shrs	# Under this configuration, the packets will go to the default
199251584Shrs	# interface.
200251584Shrs	ipv6_route__lla="fe80:: -prefixlen 10 ::1 -reject ${fibmod}"
201251584Shrs	ipv6_route__llma="ff02:: -prefixlen 16 ::1 -reject ${fibmod}"
202251584Shrs
203251584Shrs	# Add default route.
204197139Shrs	case ${ipv6_defaultrouter} in
205197139Shrs	[Nn][Oo] | '')
206197139Shrs		;;
207197139Shrs	*)
208251584Shrs		ipv6_static_routes="_default ${ipv6_static_routes}"
209251584Shrs		ipv6_route__default="default ${ipv6_defaultrouter}"
210197139Shrs		;;
211197139Shrs	esac
212197139Shrs
213251584Shrs	# Install configured routes.
214197139Shrs	if [ -n "${ipv6_static_routes}" ]; then
215197139Shrs		for i in ${ipv6_static_routes}; do
216251584Shrs			_skip=0
217251584Shrs			if [ -n "$_if" ]; then
218251584Shrs				case $i in
219251584Shrs				*:$_if)	;;
220251584Shrs				*)	_skip=1 ;;
221251584Shrs				esac
222251584Shrs			fi
223251584Shrs			if [ $_skip = 0 ]; then
224251584Shrs				ipv6_route_args=`get_if_var ${i%:*} ipv6_route_IF`
225251584Shrs				if [ -n "$ipv6_route_args" ]; then
226251584Shrs					${ROUTE_CMD} ${_action} \
227251584Shrs						-inet6 ${ipv6_route_args}
228251584Shrs				else
229251584Shrs					warn "route_${i%:*} not found"
230251584Shrs				fi
231251584Shrs			fi
232197139Shrs		done
233197139Shrs	fi
234197139Shrs
235251584Shrs	# Install the "default interface" to kernel, which will be used
236251584Shrs	# as the default route when there's no router.
237197139Shrs
238251584Shrs	# Disable installing the default interface when we act
239251584Shrs	# as router to avoid conflict between the default
240251584Shrs	# router list and the manual configured default route.
241197139Shrs	if checkyesno ipv6_gateway_enable; then
242251584Shrs		return
243197139Shrs	fi
244197139Shrs
245197139Shrs	case "${ipv6_default_interface}" in
246197139Shrs	[Nn][Oo] | [Nn][Oo][Nn][Ee])
247251584Shrs		return
248197139Shrs		;;
249197139Shrs	[Aa][Uu][Tt][Oo] | "")
250197139Shrs		for i in ${ipv6_network_interfaces}; do
251197139Shrs			case $i in
252251584Shrs			[Nn][Oo][Nn][Ee])
253251584Shrs				return
254251584Shrs				;;
255197139Shrs			lo0|faith[0-9]*)
256197139Shrs				continue
257197139Shrs				;;
258197139Shrs			esac
259197139Shrs			laddr=`network6_getladdr $i exclude_tentative`
260197139Shrs			case ${laddr} in
261197139Shrs			'')
262197139Shrs				;;
263197139Shrs			*)
264197139Shrs				ipv6_default_interface=$i
265197139Shrs				break
266197139Shrs				;;
267197139Shrs			esac
268197139Shrs		done
269197139Shrs		;;
270197139Shrs	esac
271197139Shrs
272251584Shrs	ifconfig ${ipv6_default_interface} inet6 defaultif
273251584Shrs	sysctl net.inet6.ip6.use_defaultzone=1
274197139Shrs}
275197139Shrs
276197699Shrsstatic_atm()
277197139Shrs{
278197699Shrs	local _action i route_args
279197139Shrs	_action=$1
280197139Shrs
281118908Sharti	if [ -n "${natm_static_routes}" ]; then
282118908Sharti		for i in ${natm_static_routes}; do
283197139Shrs			route_args=`get_if_var $i route_IF`
284251584Shrs			if [ -n "$route_args" ]; then
285251584Shrs				atmconfig natm ${_action} ${route_args}
286251584Shrs			else
287251584Shrs				warn "route_${i} not found."
288251584Shrs			fi
289118908Sharti		done
290118908Sharti	fi
291117019Smtm}
29229300Sdanny
293197719Shrsstatic_ipx()
294197719Shrs{
295227366Sjilles	:
296197719Shrs}
297197719Shrs
298179940Smtmropts_init()
299179940Smtm{
300179940Smtm	if [ -z "${_ropts_initdone}" ]; then
301224132Sjilles		echo -n "Additional $1 routing options:"
302179940Smtm		_ropts_initdone=yes
303179940Smtm	fi
304179940Smtm}
305179940Smtm
306197699Shrsoptions_inet()
307197699Shrs{
308224132Sjilles	_ropts_initdone=
309197139Shrs	if checkyesno icmp_bmcastecho; then
310224132Sjilles		ropts_init inet
31151231Ssheldonh		echo -n ' broadcast ping responses=YES'
312220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
313197699Shrs	else
314220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
315197139Shrs	fi
31645096Simp
317197139Shrs	if checkyesno icmp_drop_redirect; then
318224132Sjilles		ropts_init inet
31951231Ssheldonh		echo -n ' ignore ICMP redirect=YES'
320220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
321197699Shrs	else
322220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
323197139Shrs	fi
32439267Sjkoshy
325197139Shrs	if checkyesno icmp_log_redirect; then
326224132Sjilles		ropts_init inet
32751231Ssheldonh		echo -n ' log ICMP redirect=YES'
328220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
329197699Shrs	else
330220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
331197139Shrs	fi
33233439Sguido
333197139Shrs	if checkyesno gateway_enable; then
334224132Sjilles		ropts_init inet
335224132Sjilles		echo -n ' gateway=YES'
336220153Semaste		${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
337197699Shrs	else
338220153Semaste		${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
339197139Shrs	fi
34033439Sguido
341197139Shrs	if checkyesno forward_sourceroute; then
342224132Sjilles		ropts_init inet
34351231Ssheldonh		echo -n ' do source routing=YES'
344220153Semaste		${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
345197699Shrs	else
346220153Semaste		${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
347197139Shrs	fi
34847752Sphk
349197139Shrs	if checkyesno accept_sourceroute; then
350224132Sjilles		ropts_init inet
35151231Ssheldonh		echo -n ' accept source routing=YES'
352220153Semaste		${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
353197699Shrs	else
354220153Semaste		${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
355197139Shrs	fi
35651209Sdes
357197699Shrs	if checkyesno arpproxy_all; then
358224132Sjilles		ropts_init inet
359197699Shrs		echo -n ' ARP proxyall=YES'
360220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
361197699Shrs	else
362220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
363197139Shrs	fi
364224132Sjilles
365224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
366197699Shrs}
36751231Ssheldonh
368197699Shrsoptions_inet6()
369197699Shrs{
370224132Sjilles	_ropts_initdone=
371224132Sjilles
372197699Shrs	if checkyesno ipv6_gateway_enable; then
373224132Sjilles		ropts_init inet6
374224132Sjilles		echo -n ' gateway=YES'
375220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
376197699Shrs	else
377220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
378197139Shrs	fi
379224132Sjilles
380224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
381197699Shrs}
38261961Sdillon
383197719Shrsoptions_atm()
384197719Shrs{
385224132Sjilles	_ropts_initdone=
386224132Sjilles
387224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
388197719Shrs}
389197719Shrs
390197699Shrsoptions_ipx()
391197699Shrs{
392224132Sjilles	_ropts_initdone=
393224132Sjilles
394197699Shrs	if checkyesno ipxgateway_enable; then
395224132Sjilles		ropts_init ipx
396224132Sjilles		echo -n ' gateway=YES'
397220153Semaste		${SYSCTL} net.ipx.ipx.ipxforwarding=1 > /dev/null
398197699Shrs	else
399220153Semaste		${SYSCTL} net.ipx.ipx.ipxforwarding=0 > /dev/null
400197699Shrs	fi
401224132Sjilles
402224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
40325184Sjkh}
40425184Sjkh
405100280Sgordonload_rc_config $name
406197139Shrsrun_rc_command "$@"
407