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