xref: /NextBSD/etc/rc.d/routing (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1#!/bin/sh
2#
3# Configure routing and miscellaneous network tunables
4#
5# $FreeBSD$
6#
7
8# PROVIDE: routing
9# REQUIRE: netif ppp stf
10# KEYWORD: nojailvnet
11
12. /etc/rc.subr
13. /etc/network.subr
14
15name="routing"
16start_cmd="routing_start doall"
17stop_cmd="routing_stop"
18extra_commands="options static"
19static_cmd="routing_start static"
20options_cmd="routing_start options"
21
22ROUTE_CMD="/sbin/route"
23
24routing_start()
25{
26	local _cmd _af _if _a _ret
27	_cmd=$1
28	_af=$2
29	_if=$3
30	_ret=0
31
32	case $_if in
33	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
34	esac
35
36	case $_af in
37	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
38		for _a in inet inet6 atm; do
39			afexists $_a || continue
40			setroutes $_cmd $_a $_if || _ret=1
41		done
42	;;
43	*)
44		if afexists $_af; then
45			setroutes $_cmd $_af $_if || _ret=1
46		else
47			err 1 "Unsupported address family: $_af."
48		fi
49	;;
50	esac
51
52	return $_ret
53}
54
55routing_stop()
56{
57	local _af _if _a
58	_af=$1
59	_if=$2
60
61	case $_if in
62	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])	_if="" ;;
63	esac
64
65	case $_af in
66	""|[Aa][Ll][Ll]|[Aa][Nn][Yy])
67		for _a in inet inet6 atm; do
68			afexists $_a || continue
69			eval static_${_a} delete $_if
70			# When $_if is specified, do not flush routes.
71			if ! [ -n "$_if" ]; then
72				eval routing_stop_${_a}
73			fi
74		done
75	;;
76	*)
77		if afexists $_af; then
78			eval static_${_af} delete $_if
79			# When $_if is specified, do not flush routes.
80			if ! [ -n "$_if" ]; then
81				eval routing_stop_${_af}
82			fi
83		else
84			err 1 "Unsupported address family: $_af."
85		fi
86	;;
87	esac
88}
89
90setroutes()
91{
92	case $1 in
93	static)
94		static_$2 add $3
95		;;
96	options)
97		options_$2
98		;;
99	doall)
100		static_$2 add $3
101		options_$2
102		;;
103	esac
104}
105
106routing_stop_inet()
107{
108	${ROUTE_CMD} -n flush -inet
109}
110
111routing_stop_inet6()
112{
113	local i
114
115	${ROUTE_CMD} -n flush -inet6
116	for i in `list_net_interfaces`; do
117		if ipv6if $i; then
118			ifconfig $i inet6 -defaultif
119		fi
120	done
121}
122
123routing_stop_atm()
124{
125	return 0
126}
127
128static_inet()
129{
130	local _action _if _skip
131	_action=$1
132	_if=$2
133
134	# Add default route.
135	case ${defaultrouter} in
136	[Nn][Oo] | '')
137		;;
138	*)
139		static_routes="${static_routes} _default"
140		route__default="default ${defaultrouter}"
141		;;
142	esac
143
144	# Install configured routes.
145	if [ -n "${static_routes}" ]; then
146		for i in ${static_routes}; do
147			_skip=0
148			if [ -n "$_if" ]; then
149				case $i in
150				*:$_if)	;;
151				*)	_skip=1 ;;
152				esac
153			fi
154			if [ $_skip = 0 ]; then
155				route_args=`get_if_var ${i%:*} route_IF`
156				if [ -n "$route_args" ]; then
157					${ROUTE_CMD} ${_action} ${route_args}
158				else
159					warn "route_${i%:*} not found."
160				fi
161			fi
162		done
163	fi
164}
165
166static_inet6()
167{
168	local _action _if _skip fibmod fibs allfibs
169	_action=$1
170	_if=$2
171
172	# get the number of FIBs supported.
173	fibs=$((`${SYSCTL_N} net.fibs` - 1))
174	allfibs=`${SYSCTL_N} net.add_addr_allfibs`
175	if [ "$fibs" -gt 0 ] && [ "$allfibs" -ne 0 ]; then
176		fibmod="-fib 0-$fibs"
177	else
178		fibmod=
179	fi
180
181	# Add pre-defined static routes first.
182	ipv6_static_routes="_v4mapped _v4compat ${ipv6_static_routes}"
183	ipv6_static_routes="_lla _llma ${ipv6_static_routes}"
184
185	# disallow "internal" addresses to appear on the wire
186	ipv6_route__v4mapped="::ffff:0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
187	ipv6_route__v4compat="::0.0.0.0 -prefixlen 96 ::1 -reject ${fibmod}"
188
189	# Disallow link-local unicast packets without outgoing scope
190	# identifiers.  However, if you set "ipv6_default_interface",
191	# for the host case, you will allow to omit the identifiers.
192	# Under this configuration, the packets will go to the default
193	# interface.
194	ipv6_route__lla="fe80:: -prefixlen 10 ::1 -reject ${fibmod}"
195	ipv6_route__llma="ff02:: -prefixlen 16 ::1 -reject ${fibmod}"
196
197	# Add default route.
198	case ${ipv6_defaultrouter} in
199	[Nn][Oo] | '')
200		;;
201	*)
202		ipv6_static_routes="${ipv6_static_routes} _default"
203		ipv6_route__default="default ${ipv6_defaultrouter}"
204		;;
205	esac
206
207	# Install configured routes.
208	if [ -n "${ipv6_static_routes}" ]; then
209		for i in ${ipv6_static_routes}; do
210			_skip=0
211			if [ -n "$_if" ]; then
212				case $i in
213				*:$_if)	;;
214				*)	_skip=1 ;;
215				esac
216			fi
217			if [ $_skip = 0 ]; then
218				ipv6_route_args=`get_if_var ${i%:*} ipv6_route_IF`
219				if [ -n "$ipv6_route_args" ]; then
220					${ROUTE_CMD} ${_action} \
221						-inet6 ${ipv6_route_args}
222				else
223					warn "route_${i%:*} not found"
224				fi
225			fi
226		done
227	fi
228
229	# Install the "default interface" to kernel, which will be used
230	# as the default route when there's no router.
231
232	# Disable installing the default interface when we act
233	# as router to avoid conflict between the default
234	# router list and the manual configured default route.
235	if checkyesno ipv6_gateway_enable; then
236		return
237	fi
238
239	case "${ipv6_default_interface}" in
240	[Nn][Oo] | [Nn][Oo][Nn][Ee])
241		return
242		;;
243	[Aa][Uu][Tt][Oo] | "")
244		for i in ${ipv6_network_interfaces}; do
245			case $i in
246			[Nn][Oo][Nn][Ee])
247				return
248				;;
249			lo0)
250				continue
251				;;
252			esac
253			laddr=`network6_getladdr $i exclude_tentative`
254			case ${laddr} in
255			'')
256				;;
257			*)
258				ipv6_default_interface=$i
259				break
260				;;
261			esac
262		done
263		;;
264	esac
265
266	ifconfig ${ipv6_default_interface} inet6 defaultif
267	sysctl net.inet6.ip6.use_defaultzone=1
268}
269
270static_atm()
271{
272	local _action i route_args
273	_action=$1
274
275	if [ -n "${natm_static_routes}" ]; then
276		for i in ${natm_static_routes}; do
277			route_args=`get_if_var $i route_IF`
278			if [ -n "$route_args" ]; then
279				atmconfig natm ${_action} ${route_args}
280			else
281				warn "route_${i} not found."
282			fi
283		done
284	fi
285}
286
287ropts_init()
288{
289	if [ -z "${_ropts_initdone}" ]; then
290		echo -n "Additional $1 routing options:"
291		_ropts_initdone=yes
292	fi
293}
294
295options_inet()
296{
297	_ropts_initdone=
298	if checkyesno icmp_bmcastecho; then
299		ropts_init inet
300		echo -n ' broadcast ping responses=YES'
301		${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
302	else
303		${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
304	fi
305
306	if checkyesno icmp_drop_redirect; then
307		ropts_init inet
308		echo -n ' ignore ICMP redirect=YES'
309		${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
310	else
311		${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
312	fi
313
314	if checkyesno icmp_log_redirect; then
315		ropts_init inet
316		echo -n ' log ICMP redirect=YES'
317		${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
318	else
319		${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
320	fi
321
322	if checkyesno gateway_enable; then
323		ropts_init inet
324		echo -n ' gateway=YES'
325		${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
326	else
327		${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
328	fi
329
330	if checkyesno forward_sourceroute; then
331		ropts_init inet
332		echo -n ' do source routing=YES'
333		${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
334	else
335		${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
336	fi
337
338	if checkyesno accept_sourceroute; then
339		ropts_init inet
340		echo -n ' accept source routing=YES'
341		${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
342	else
343		${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
344	fi
345
346	if checkyesno arpproxy_all; then
347		ropts_init inet
348		echo -n ' ARP proxyall=YES'
349		${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
350	else
351		${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
352	fi
353
354	[ -n "${_ropts_initdone}" ] && echo '.'
355}
356
357options_inet6()
358{
359	_ropts_initdone=
360
361	if checkyesno ipv6_gateway_enable; then
362		ropts_init inet6
363		echo -n ' gateway=YES'
364		${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
365	else
366		${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
367	fi
368
369	[ -n "${_ropts_initdone}" ] && echo '.'
370}
371
372options_atm()
373{
374	_ropts_initdone=
375
376	[ -n "${_ropts_initdone}" ] && echo '.'
377}
378
379load_rc_config $name
380run_rc_command "$@"
381