1100280Sgordon#!/bin/sh
225184Sjkh#
3100280Sgordon# Configure routing and miscellaneous network tunables
466830Sobrien#
550472Speter# $FreeBSD: stable/11/etc/rc.d/routing 318965 2017-05-26 19:19:47Z n_hibma $
666830Sobrien#
725184Sjkh
8117019Smtm# PROVIDE: routing
9274331Smelifaro# REQUIRE: netif ppp stf
10250804Sjamie# KEYWORD: nojailvnet
1125184Sjkh
12100280Sgordon. /etc/rc.subr
13179079Sbrooks. /etc/network.subr
1425184Sjkh
15117019Smtmname="routing"
16298514Slmedesc="Routing setup"
17197719Shrsstart_cmd="routing_start doall"
18117019Smtmstop_cmd="routing_stop"
19117019Smtmextra_commands="options static"
20197719Shrsstatic_cmd="routing_start static"
21197719Shrsoptions_cmd="routing_start options"
2285831Sdes
23251584ShrsROUTE_CMD="/sbin/route"
24197719Shrs
25117019Smtmrouting_start()
26100280Sgordon{
27270783Shrs	local _cmd _af _if _a _ret
28197719Shrs	_cmd=$1
29197719Shrs	_af=$2
30251584Shrs	_if=$3
31270783Shrs	_ret=0
32197719Shrs
33251584Shrs	case $_if in
34251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
35251584Shrs	esac
36197719Shrs
37197719Shrs	case $_af in
38270783Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
39270783Shrs		for _a in inet inet6 atm; do
40270783Shrs			afexists $_a || continue
41270783Shrs			setroutes $_cmd $_a $_if || _ret=1
42270783Shrs		done
43270783Shrs	;;
44270783Shrs	*)
45251584Shrs		if afexists $_af; then
46270783Shrs			setroutes $_cmd $_af $_if || _ret=1
47251584Shrs		else
48251584Shrs			err 1 "Unsupported address family: $_af."
49251584Shrs		fi
50270783Shrs	;;
51197719Shrs	esac
52270783Shrs
53270783Shrs	return $_ret
54117019Smtm}
55117019Smtm
56117019Smtmrouting_stop()
57117019Smtm{
58251584Shrs	local _af _if _a
59197719Shrs	_af=$1
60251584Shrs	_if=$2
61197699Shrs
62251584Shrs	case $_if in
63251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
64251584Shrs	esac
65197719Shrs
66197719Shrs	case $_af in
67251584Shrs	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
68263140Sglebius		for _a in inet inet6 atm; do
69197719Shrs			afexists $_a || continue
70251584Shrs			eval static_${_a} delete $_if
71251584Shrs			# When $_if is specified, do not flush routes.
72251584Shrs			if ! [ -n "$_if" ]; then
73251584Shrs				eval routing_stop_${_a}
74251584Shrs			fi
75197719Shrs		done
76270783Shrs	;;
77251584Shrs	*)
78270783Shrs		if afexists $_af; then
79270783Shrs			eval static_${_af} delete $_if 
80270783Shrs			# When $_if is specified, do not flush routes.
81270783Shrs			if ! [ -n "$_if" ]; then
82270783Shrs				eval routing_stop_${_af}
83270783Shrs			fi
84270783Shrs		else
85270783Shrs			err 1 "Unsupported address family: $_af."
86270783Shrs		fi
87270783Shrs	;;
88197719Shrs	esac
89197699Shrs}
90197699Shrs
91197719Shrssetroutes()
92197719Shrs{
93301207Sasomers	local _ret
94301207Sasomers	_ret=0
95197719Shrs	case $1 in
96197719Shrs	static)
97251584Shrs		static_$2 add $3
98301207Sasomers		_ret=$?
99197719Shrs		;;
100197719Shrs	options)
101197719Shrs		options_$2
102197719Shrs		;;
103197719Shrs	doall)
104251584Shrs		static_$2 add $3
105301207Sasomers		_ret=$?
106197719Shrs		options_$2
107197719Shrs		;;
108197719Shrs	esac
109301207Sasomers	return $_ret
110197719Shrs}
111197719Shrs
112197699Shrsrouting_stop_inet()
113197699Shrs{
114251584Shrs	${ROUTE_CMD} -n flush -inet
115197699Shrs}
116197699Shrs
117197699Shrsrouting_stop_inet6()
118197699Shrs{
119197699Shrs	local i
120197699Shrs
121251584Shrs	${ROUTE_CMD} -n flush -inet6
122230991Shrs	for i in `list_net_interfaces`; do
123230991Shrs		if ipv6if $i; then
124230991Shrs			ifconfig $i inet6 -defaultif
125230991Shrs		fi
126197139Shrs	done
127117019Smtm}
128117019Smtm
129197719Shrsrouting_stop_atm()
130117019Smtm{
131197719Shrs	return 0
132197139Shrs}
133197139Shrs
134300882Sasomersget_fibmod()
135300882Sasomers{
136300882Sasomers	local _fibs
137300882Sasomers
138300882Sasomers	_fibs=$((`${SYSCTL_N} net.fibs` - 1))
139300882Sasomers	if [ ${_fibs} -gt 0 ]; then
140300882Sasomers		echo "-fib 0-${_fibs}"
141300882Sasomers	else
142300882Sasomers		echo
143300882Sasomers	fi
144300882Sasomers}
145300882Sasomers
146197699Shrsstatic_inet()
147197139Shrs{
148300882Sasomers	local _action _if _skip _fibmod
149197139Shrs	_action=$1
150251584Shrs	_if=$2
151197139Shrs
152300882Sasomers	_fibmod=`get_fibmod`
153300882Sasomers
154300882Sasomers	# Provide loopback route in all routing tables.  This has to come
155300882Sasomers	# first so that any following routes can be added.
156300882Sasomers	static_routes="_loopback ${static_routes}"
157300882Sasomers	route__loopback="-inet 127.0.0.1 -iface lo0 ${_fibmod}"
158300882Sasomers
159251584Shrs	# Add default route.
16051231Ssheldonh	case ${defaultrouter} in
16151231Ssheldonh	[Nn][Oo] | '')
16251231Ssheldonh		;;
16351231Ssheldonh	*)
164255163Sdelphij		static_routes="${static_routes} _default"
165251584Shrs		route__default="default ${defaultrouter}"
16651231Ssheldonh		;;
16751231Ssheldonh	esac
16840006Sphk
169251584Shrs	# Install configured routes.
17051231Ssheldonh	if [ -n "${static_routes}" ]; then
17151231Ssheldonh		for i in ${static_routes}; do
172251584Shrs			_skip=0
173251584Shrs			if [ -n "$_if" ]; then
174251584Shrs				case $i in
175251584Shrs				*:$_if)	;;
176251584Shrs				*)	_skip=1 ;;
177251584Shrs				esac
178251584Shrs			fi
179251584Shrs			if [ $_skip = 0 ]; then
180251584Shrs				route_args=`get_if_var ${i%:*} route_IF`
181251584Shrs				if [ -n "$route_args" ]; then
182251584Shrs					${ROUTE_CMD} ${_action} ${route_args}
183251584Shrs				else
184251584Shrs					warn "route_${i%:*} not found."
185251584Shrs				fi
186251584Shrs			fi
18751231Ssheldonh		done
18851231Ssheldonh	fi
189197139Shrs}
190197139Shrs
191197699Shrsstatic_inet6()
192197139Shrs{
193300882Sasomers	local _action _if _skip fibmod allfibs
194197139Shrs	_action=$1
195251584Shrs	_if=$2
196197139Shrs
197300882Sasomers	fibmod=`get_fibmod`
198231852Sbz
199251584Shrs	# Add pre-defined static routes first.
200251584Shrs	ipv6_static_routes="_v4mapped _v4compat ${ipv6_static_routes}"
201251584Shrs	ipv6_static_routes="_lla _llma ${ipv6_static_routes}"
202300882Sasomers	ipv6_static_routes="_loopback ${ipv6_static_routes}"
203251584Shrs
204197139Shrs	# disallow "internal" addresses to appear on the wire
205251584Shrs	ipv6_route__v4mapped="::ffff:0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
206251584Shrs	ipv6_route__v4compat="::0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
207197139Shrs
208300882Sasomers	# Create a loopback route in every fib
209300882Sasomers	ipv6_route__loopback="::1 -prefixlen 128 -iface lo0 ${fibmod}"
210300882Sasomers
211251584Shrs	# Disallow link-local unicast packets without outgoing scope
212251584Shrs	# identifiers.  However, if you set "ipv6_default_interface",
213251584Shrs	# for the host case, you will allow to omit the identifiers.
214251584Shrs	# Under this configuration, the packets will go to the default
215251584Shrs	# interface.
216251584Shrs	ipv6_route__lla="fe80:: -prefixlen 10 ::1 -reject ${fibmod}"
217251584Shrs	ipv6_route__llma="ff02:: -prefixlen 16 ::1 -reject ${fibmod}"
218251584Shrs
219251584Shrs	# Add default route.
220197139Shrs	case ${ipv6_defaultrouter} in
221197139Shrs	[Nn][Oo] | '')
222197139Shrs		;;
223197139Shrs	*)
224255163Sdelphij		ipv6_static_routes="${ipv6_static_routes} _default"
225251584Shrs		ipv6_route__default="default ${ipv6_defaultrouter}"
226197139Shrs		;;
227197139Shrs	esac
228197139Shrs
229251584Shrs	# Install configured routes.
230197139Shrs	if [ -n "${ipv6_static_routes}" ]; then
231197139Shrs		for i in ${ipv6_static_routes}; do
232251584Shrs			_skip=0
233251584Shrs			if [ -n "$_if" ]; then
234251584Shrs				case $i in
235251584Shrs				*:$_if)	;;
236251584Shrs				*)	_skip=1 ;;
237251584Shrs				esac
238251584Shrs			fi
239251584Shrs			if [ $_skip = 0 ]; then
240251584Shrs				ipv6_route_args=`get_if_var ${i%:*} ipv6_route_IF`
241251584Shrs				if [ -n "$ipv6_route_args" ]; then
242251584Shrs					${ROUTE_CMD} ${_action} \
243251584Shrs						-inet6 ${ipv6_route_args}
244251584Shrs				else
245251584Shrs					warn "route_${i%:*} not found"
246251584Shrs				fi
247251584Shrs			fi
248197139Shrs		done
249197139Shrs	fi
250197139Shrs
251251584Shrs	# Install the "default interface" to kernel, which will be used
252251584Shrs	# as the default route when there's no router.
253197139Shrs
254251584Shrs	# Disable installing the default interface when we act
255251584Shrs	# as router to avoid conflict between the default
256251584Shrs	# router list and the manual configured default route.
257197139Shrs	if checkyesno ipv6_gateway_enable; then
258251584Shrs		return
259197139Shrs	fi
260197139Shrs
261197139Shrs	case "${ipv6_default_interface}" in
262197139Shrs	[Nn][Oo] | [Nn][Oo][Nn][Ee])
263251584Shrs		return
264197139Shrs		;;
265197139Shrs	[Aa][Uu][Tt][Oo] | "")
266197139Shrs		for i in ${ipv6_network_interfaces}; do
267197139Shrs			case $i in
268251584Shrs			[Nn][Oo][Nn][Ee])
269251584Shrs				return
270251584Shrs				;;
271274331Smelifaro			lo0)
272197139Shrs				continue
273197139Shrs				;;
274197139Shrs			esac
275197139Shrs			laddr=`network6_getladdr $i exclude_tentative`
276197139Shrs			case ${laddr} in
277197139Shrs			'')
278197139Shrs				;;
279197139Shrs			*)
280197139Shrs				ipv6_default_interface=$i
281197139Shrs				break
282197139Shrs				;;
283197139Shrs			esac
284197139Shrs		done
285197139Shrs		;;
286197139Shrs	esac
287197139Shrs
288251584Shrs	ifconfig ${ipv6_default_interface} inet6 defaultif
289318965Sn_hibma	${SYSCTL} net.inet6.ip6.use_defaultzone=1 > /dev/null
290197139Shrs}
291197139Shrs
292197699Shrsstatic_atm()
293197139Shrs{
294197699Shrs	local _action i route_args
295197139Shrs	_action=$1
296197139Shrs
297118908Sharti	if [ -n "${natm_static_routes}" ]; then
298118908Sharti		for i in ${natm_static_routes}; do
299197139Shrs			route_args=`get_if_var $i route_IF`
300251584Shrs			if [ -n "$route_args" ]; then
301251584Shrs				atmconfig natm ${_action} ${route_args}
302251584Shrs			else
303251584Shrs				warn "route_${i} not found."
304251584Shrs			fi
305118908Sharti		done
306118908Sharti	fi
307117019Smtm}
30829300Sdanny
309179940Smtmropts_init()
310179940Smtm{
311179940Smtm	if [ -z "${_ropts_initdone}" ]; then
312224132Sjilles		echo -n "Additional $1 routing options:"
313179940Smtm		_ropts_initdone=yes
314179940Smtm	fi
315179940Smtm}
316179940Smtm
317197699Shrsoptions_inet()
318197699Shrs{
319224132Sjilles	_ropts_initdone=
320197139Shrs	if checkyesno icmp_bmcastecho; then
321224132Sjilles		ropts_init inet
32251231Ssheldonh		echo -n ' broadcast ping responses=YES'
323220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
324197699Shrs	else
325220153Semaste		${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
326197139Shrs	fi
32745096Simp
328197139Shrs	if checkyesno icmp_drop_redirect; then
329224132Sjilles		ropts_init inet
33051231Ssheldonh		echo -n ' ignore ICMP redirect=YES'
331220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
332197699Shrs	else
333220153Semaste		${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
334197139Shrs	fi
33539267Sjkoshy
336197139Shrs	if checkyesno icmp_log_redirect; then
337224132Sjilles		ropts_init inet
33851231Ssheldonh		echo -n ' log ICMP redirect=YES'
339220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
340197699Shrs	else
341220153Semaste		${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
342197139Shrs	fi
34333439Sguido
344197139Shrs	if checkyesno gateway_enable; then
345224132Sjilles		ropts_init inet
346224132Sjilles		echo -n ' gateway=YES'
347220153Semaste		${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
348197699Shrs	else
349220153Semaste		${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
350197139Shrs	fi
35133439Sguido
352271610Shrs	if checkyesno forward_sourceroute; then
353271610Shrs		ropts_init inet
354271610Shrs		echo -n ' do source routing=YES'
355271610Shrs		${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
356271610Shrs	else
357271610Shrs		${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
358271610Shrs	fi
35947752Sphk
360271610Shrs	if checkyesno accept_sourceroute; then
361271610Shrs		ropts_init inet
362271610Shrs		echo -n ' accept source routing=YES'
363271610Shrs		${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
364271610Shrs	else
365271610Shrs		${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
366197139Shrs	fi
36751209Sdes
368197699Shrs	if checkyesno arpproxy_all; then
369224132Sjilles		ropts_init inet
370197699Shrs		echo -n ' ARP proxyall=YES'
371220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
372197699Shrs	else
373220153Semaste		${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
374197139Shrs	fi
375224132Sjilles
376224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
377197699Shrs}
37851231Ssheldonh
379197699Shrsoptions_inet6()
380197699Shrs{
381224132Sjilles	_ropts_initdone=
382224132Sjilles
383197699Shrs	if checkyesno ipv6_gateway_enable; then
384224132Sjilles		ropts_init inet6
385224132Sjilles		echo -n ' gateway=YES'
386220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
387197699Shrs	else
388220153Semaste		${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
389197139Shrs	fi
390224132Sjilles
391224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
392197699Shrs}
39361961Sdillon
394197719Shrsoptions_atm()
395197719Shrs{
396224132Sjilles	_ropts_initdone=
397224132Sjilles
398224132Sjilles	[ -n "${_ropts_initdone}" ] && echo '.'
399197719Shrs}
400197719Shrs
401100280Sgordonload_rc_config $name
402197139Shrsrun_rc_command "$@"
403