1100280Sgordon#!/bin/sh
225184Sjkh#
3100280Sgordon# Configure routing and miscellaneous network tunables
466830Sobrien#
550472Speter# $FreeBSD: stable/10/etc/rc.d/routing 319220 2017-05-30 16:05:11Z asomers $
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{
26272863Shrs	local _cmd _af _if _a _ret
27197719Shrs	_cmd=$1
28197719Shrs	_af=$2
29251584Shrs	_if=$3
30272863Shrs	_ret=0
31197719Shrs
32251584Shrs	case $_if in
33251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
34251584Shrs	esac
35197719Shrs
36197719Shrs	case $_af in
37272863Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
38272863Shrs		for _a in inet inet6 atm; do
39272863Shrs			afexists $_a || continue
40272863Shrs			setroutes $_cmd $_a $_if || _ret=1
41272863Shrs		done
42272863Shrs	;;
43272863Shrs	*)
44251584Shrs		if afexists $_af; then
45272863Shrs			setroutes $_cmd $_af $_if || _ret=1
46251584Shrs		else
47251584Shrs			err 1 "Unsupported address family: $_af."
48251584Shrs		fi
49272863Shrs	;;
50197719Shrs	esac
51272863Shrs
52272863Shrs	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
75272863Shrs	;;
76251584Shrs	*)
77272863Shrs		if afexists $_af; then
78272863Shrs			eval static_${_af} delete $_if 
79272863Shrs			# When $_if is specified, do not flush routes.
80272863Shrs			if ! [ -n "$_if" ]; then
81272863Shrs				eval routing_stop_${_af}
82272863Shrs			fi
83272863Shrs		else
84272863Shrs			err 1 "Unsupported address family: $_af."
85272863Shrs		fi
86272863Shrs	;;
87197719Shrs	esac
88197699Shrs}
89197699Shrs
90197719Shrssetroutes()
91197719Shrs{
92319220Sasomers	local _ret
93319220Sasomers	_ret=0
94197719Shrs	case $1 in
95197719Shrs	static)
96251584Shrs		static_$2 add $3
97319220Sasomers		_ret=$?
98197719Shrs		;;
99197719Shrs	options)
100197719Shrs		options_$2
101197719Shrs		;;
102197719Shrs	doall)
103251584Shrs		static_$2 add $3
104319220Sasomers		_ret=$?
105197719Shrs		options_$2
106197719Shrs		;;
107197719Shrs	esac
108319220Sasomers	return $_ret
109197719Shrs}
110197719Shrs
111197699Shrsrouting_stop_inet()
112197699Shrs{
113251584Shrs	${ROUTE_CMD} -n flush -inet
114197699Shrs}
115197699Shrs
116197699Shrsrouting_stop_inet6()
117197699Shrs{
118197699Shrs	local i
119197699Shrs
120251584Shrs	${ROUTE_CMD} -n flush -inet6
121230991Shrs	for i in `list_net_interfaces`; do
122230991Shrs		if ipv6if $i; then
123230991Shrs			ifconfig $i inet6 -defaultif
124230991Shrs		fi
125197139Shrs	done
126117019Smtm}
127117019Smtm
128197719Shrsrouting_stop_atm()
129117019Smtm{
130197719Shrs	return 0
131197139Shrs}
132197139Shrs
133197719Shrsrouting_stop_ipx()
134197139Shrs{
135197719Shrs	return 0
136197139Shrs}
137197139Shrs
138197699Shrsstatic_inet()
139197139Shrs{
140251584Shrs	local _action _if _skip
141197139Shrs	_action=$1
142251584Shrs	_if=$2
143197139Shrs
144251584Shrs	# Add default route.
14551231Ssheldonh	case ${defaultrouter} in
14651231Ssheldonh	[Nn][Oo] | '')
14751231Ssheldonh		;;
14851231Ssheldonh	*)
149255163Sdelphij		static_routes="${static_routes} _default"
150251584Shrs		route__default="default ${defaultrouter}"
15151231Ssheldonh		;;
15251231Ssheldonh	esac
15340006Sphk
154251584Shrs	# Install configured routes.
15551231Ssheldonh	if [ -n "${static_routes}" ]; then
15651231Ssheldonh		for i in ${static_routes}; do
157251584Shrs			_skip=0
158251584Shrs			if [ -n "$_if" ]; then
159251584Shrs				case $i in
160251584Shrs				*:$_if)	;;
161251584Shrs				*)	_skip=1 ;;
162251584Shrs				esac
163251584Shrs			fi
164251584Shrs			if [ $_skip = 0 ]; then
165251584Shrs				route_args=`get_if_var ${i%:*} route_IF`
166251584Shrs				if [ -n "$route_args" ]; then
167251584Shrs					${ROUTE_CMD} ${_action} ${route_args}
168251584Shrs				else
169251584Shrs					warn "route_${i%:*} not found."
170251584Shrs				fi
171251584Shrs			fi
17251231Ssheldonh		done
17351231Ssheldonh	fi
174197139Shrs}
175197139Shrs
176197699Shrsstatic_inet6()
177197139Shrs{
178278659Srpaulo	local _action _if _skip fibmod fibs allfibs
179197139Shrs	_action=$1
180251584Shrs	_if=$2
181197139Shrs
182231852Sbz	# get the number of FIBs supported.
183243188Shrs	fibs=$((`${SYSCTL_N} net.fibs` - 1))
184278659Srpaulo	allfibs=`${SYSCTL_N} net.add_addr_allfibs`
185278659Srpaulo	if [ "$fibs" -gt 0 ] && [ "$allfibs" -ne 0 ]; then
186243188Shrs		fibmod="-fib 0-$fibs"
187243188Shrs	else
188243188Shrs		fibmod=
189243188Shrs	fi
190231852Sbz
191251584Shrs	# Add pre-defined static routes first.
192251584Shrs	ipv6_static_routes="_v4mapped _v4compat ${ipv6_static_routes}"
193251584Shrs	ipv6_static_routes="_lla _llma ${ipv6_static_routes}"
194251584Shrs
195197139Shrs	# disallow "internal" addresses to appear on the wire
196251584Shrs	ipv6_route__v4mapped="::ffff:0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
197251584Shrs	ipv6_route__v4compat="::0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
198197139Shrs
199251584Shrs	# Disallow link-local unicast packets without outgoing scope
200251584Shrs	# identifiers.  However, if you set "ipv6_default_interface",
201251584Shrs	# for the host case, you will allow to omit the identifiers.
202251584Shrs	# Under this configuration, the packets will go to the default
203251584Shrs	# interface.
204251584Shrs	ipv6_route__lla="fe80:: -prefixlen 10 ::1 -reject ${fibmod}"
205251584Shrs	ipv6_route__llma="ff02:: -prefixlen 16 ::1 -reject ${fibmod}"
206251584Shrs
207251584Shrs	# Add default route.
208197139Shrs	case ${ipv6_defaultrouter} in
209197139Shrs	[Nn][Oo] | '')
210197139Shrs		;;
211197139Shrs	*)
212255163Sdelphij		ipv6_static_routes="${ipv6_static_routes} _default"
213251584Shrs		ipv6_route__default="default ${ipv6_defaultrouter}"
214197139Shrs		;;
215197139Shrs	esac
216197139Shrs
217251584Shrs	# Install configured routes.
218197139Shrs	if [ -n "${ipv6_static_routes}" ]; then
219197139Shrs		for i in ${ipv6_static_routes}; do
220251584Shrs			_skip=0
221251584Shrs			if [ -n "$_if" ]; then
222251584Shrs				case $i in
223251584Shrs				*:$_if)	;;
224251584Shrs				*)	_skip=1 ;;
225251584Shrs				esac
226251584Shrs			fi
227251584Shrs			if [ $_skip = 0 ]; then
228251584Shrs				ipv6_route_args=`get_if_var ${i%:*} ipv6_route_IF`
229251584Shrs				if [ -n "$ipv6_route_args" ]; then
230251584Shrs					${ROUTE_CMD} ${_action} \
231251584Shrs						-inet6 ${ipv6_route_args}
232251584Shrs				else
233251584Shrs					warn "route_${i%:*} not found"
234251584Shrs				fi
235251584Shrs			fi
236197139Shrs		done
237197139Shrs	fi
238197139Shrs
239251584Shrs	# Install the "default interface" to kernel, which will be used
240251584Shrs	# as the default route when there's no router.
241197139Shrs
242251584Shrs	# Disable installing the default interface when we act
243251584Shrs	# as router to avoid conflict between the default
244251584Shrs	# router list and the manual configured default route.
245197139Shrs	if checkyesno ipv6_gateway_enable; then
246251584Shrs		return
247197139Shrs	fi
248197139Shrs
249197139Shrs	case "${ipv6_default_interface}" in
250197139Shrs	[Nn][Oo] | [Nn][Oo][Nn][Ee])
251251584Shrs		return
252197139Shrs		;;
253197139Shrs	[Aa][Uu][Tt][Oo] | "")
254197139Shrs		for i in ${ipv6_network_interfaces}; do
255197139Shrs			case $i in
256251584Shrs			[Nn][Oo][Nn][Ee])
257251584Shrs				return
258251584Shrs				;;
259197139Shrs			lo0|faith[0-9]*)
260197139Shrs				continue
261197139Shrs				;;
262197139Shrs			esac
263197139Shrs			laddr=`network6_getladdr $i exclude_tentative`
264197139Shrs			case ${laddr} in
265197139Shrs			'')
266197139Shrs				;;
267197139Shrs			*)
268197139Shrs				ipv6_default_interface=$i
269197139Shrs				break
270197139Shrs				;;
271197139Shrs			esac
272197139Shrs		done
273197139Shrs		;;
274197139Shrs	esac
275197139Shrs
276251584Shrs	ifconfig ${ipv6_default_interface} inet6 defaultif
277251584Shrs	sysctl net.inet6.ip6.use_defaultzone=1
278197139Shrs}
279197139Shrs
280197699Shrsstatic_atm()
281197139Shrs{
282197699Shrs	local _action i route_args
283197139Shrs	_action=$1
284197139Shrs
285118908Sharti	if [ -n "${natm_static_routes}" ]; then
286118908Sharti		for i in ${natm_static_routes}; do
287197139Shrs			route_args=`get_if_var $i route_IF`
288251584Shrs			if [ -n "$route_args" ]; then
289251584Shrs				atmconfig natm ${_action} ${route_args}
290251584Shrs			else
291251584Shrs				warn "route_${i} not found."
292251584Shrs			fi
293118908Sharti		done
294118908Sharti	fi
295117019Smtm}
29629300Sdanny
297197719Shrsstatic_ipx()
298197719Shrs{
299227366Sjilles	:
300197719Shrs}
301197719Shrs
302179940Smtmropts_init()
303179940Smtm{
304179940Smtm	if [ -z "${_ropts_initdone}" ]; then
305224132Sjilles		echo -n "Additional $1 routing options:"
306179940Smtm		_ropts_initdone=yes
307179940Smtm	fi
308179940Smtm}
309179940Smtm
310197699Shrsoptions_inet()
311197699Shrs{
312224132Sjilles	_ropts_initdone=
313197139Shrs	if checkyesno icmp_bmcastecho; then
314224132Sjilles		ropts_init inet
31551231Ssheldonh		echo -n ' broadcast ping responses=YES'
316220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
317197699Shrs	else
318220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
319197139Shrs	fi
32045096Simp
321197139Shrs	if checkyesno icmp_drop_redirect; then
322224132Sjilles		ropts_init inet
32351231Ssheldonh		echo -n ' ignore ICMP redirect=YES'
324220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
325197699Shrs	else
326220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
327197139Shrs	fi
32839267Sjkoshy
329197139Shrs	if checkyesno icmp_log_redirect; then
330224132Sjilles		ropts_init inet
33151231Ssheldonh		echo -n ' log ICMP redirect=YES'
332220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
333197699Shrs	else
334220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
335197139Shrs	fi
33633439Sguido
337197139Shrs	if checkyesno gateway_enable; then
338224132Sjilles		ropts_init inet
339224132Sjilles		echo -n ' gateway=YES'
340220153Semaste		${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
341197699Shrs	else
342220153Semaste		${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
343197139Shrs	fi
34433439Sguido
345197139Shrs	if checkyesno forward_sourceroute; then
346224132Sjilles		ropts_init inet
34751231Ssheldonh		echo -n ' do source routing=YES'
348220153Semaste		${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
349197699Shrs	else
350220153Semaste		${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
351197139Shrs	fi
35247752Sphk
353197139Shrs	if checkyesno accept_sourceroute; then
354224132Sjilles		ropts_init inet
35551231Ssheldonh		echo -n ' accept source routing=YES'
356220153Semaste		${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
357197699Shrs	else
358220153Semaste		${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
359197139Shrs	fi
36051209Sdes
361197699Shrs	if checkyesno arpproxy_all; then
362224132Sjilles		ropts_init inet
363197699Shrs		echo -n ' ARP proxyall=YES'
364220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
365197699Shrs	else
366220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
367197139Shrs	fi
368224132Sjilles
369224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
370197699Shrs}
37151231Ssheldonh
372197699Shrsoptions_inet6()
373197699Shrs{
374224132Sjilles	_ropts_initdone=
375224132Sjilles
376197699Shrs	if checkyesno ipv6_gateway_enable; then
377224132Sjilles		ropts_init inet6
378224132Sjilles		echo -n ' gateway=YES'
379220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
380197699Shrs	else
381220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
382197139Shrs	fi
383224132Sjilles
384224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
385197699Shrs}
38661961Sdillon
387197719Shrsoptions_atm()
388197719Shrs{
389224132Sjilles	_ropts_initdone=
390224132Sjilles
391224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
392197719Shrs}
393197719Shrs
394197699Shrsoptions_ipx()
395197699Shrs{
396224132Sjilles	_ropts_initdone=
397224132Sjilles
398197699Shrs	if checkyesno ipxgateway_enable; then
399224132Sjilles		ropts_init ipx
400224132Sjilles		echo -n ' gateway=YES'
401220153Semaste		${SYSCTL} net.ipx.ipx.ipxforwarding=1 > /dev/null
402197699Shrs	else
403220153Semaste		${SYSCTL} net.ipx.ipx.ipxforwarding=0 > /dev/null
404197699Shrs	fi
405224132Sjilles
406224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
40725184Sjkh}
40825184Sjkh
409100280Sgordonload_rc_config $name
410197139Shrsrun_rc_command "$@"
411