xref: /titanic_50/usr/src/cmd/svc/milestone/net-routing-setup (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27
28# This script configures IP routing.
29
30. /lib/svc/share/smf_include.sh
31
32#
33# In a zone we need this service to be up, but all of the work
34# it tries to do is irrelevant (and will actually lead to the service
35# failing if we try to do it), so just bail out.
36#
37smf_is_globalzone || exit $SMF_EXIT_OK
38
39#
40# If routing.conf file is in place, and has not already been read in
41# by previous invokation of routeadm, legacy configuration is upgraded
42# by this call to "routeadm -u".  This call is also needed when
43# a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
44# which need to be applied.  Finally, routeadm starts in.ndpd by
45# enabling the ndp service (in.ndpd), which is required for IPv6 address
46# autoconfiguration. It would be nice if we could do this in
47# network/loopback, but since the SMF backend is read-only at that
48# point in boot, we cannot.
49#
50/sbin/routeadm -u
51
52#
53# Are we routing dynamically? routeadm(1M) reports this in the
54# "current" values of ipv4/6-routing - if either are true, we are running
55# routing daemons (or at least they are enabled to run).
56#
57dynamic_routing_test=`/sbin/routeadm -p | \
58nawk '/^ipv[46]-routing [.]*/ { print $2 }'  | /usr/bin/grep "current=enabled"`
59if [ -n "$dynamic_routing_test" ]; then
60	dynamic_routing="true"
61fi
62
63/usr/sbin/ifconfig -a6u >/etc/svc/volatile/ifconfig.$$
64numv6ifs=`/usr/bin/grep -c inet6 /etc/svc/volatile/ifconfig.$$`
65
66if  [ $numv6ifs -gt 1 ]; then
67	#
68	# Add a static route for multicast packets out of a link-local
69	# interface, although would like to specify multicast interface using
70	# an interface name!
71	#
72	set -- `/usr/bin/awk '
73		/inet6 fe80:/ {
74			print substr($2, 1, index($2, "/") - 1)
75		}' /etc/svc/volatile/ifconfig.$$`
76
77	if [ -n "$1" ]; then
78		echo "Setting default IPv6 interface for multicast:" \
79			"add net ff00::/8: gateway $1"
80		/usr/sbin/route -n add -interface -inet6 "ff00::/8" "$1" \
81			>/dev/null
82	fi
83fi
84/usr/bin/rm -f /etc/svc/volatile/ifconfig.$$
85
86#
87# Configure default IPv4 routers using the local "/etc/defaultrouter"
88# configuration file.  The file can contain the hostnames or IP
89# addresses of one or more default routers.  If hostnames are used,
90# each hostname must also be listed in the local "/etc/hosts" file
91# because NIS and NIS+ are not running at the time that this script is
92# run.  Each router name or address is listed on a single line by
93# itself in the file.  Anything else on that line after the router's
94# name or address is ignored.  Lines that begin with "#" are
95# considered comments and ignored.
96#
97# The default routes listed in the "/etc/defaultrouter" file will
98# replace those added by the kernel during diskless booting.  An
99# empty "/etc/defaultrouter" file will cause the default route
100# added by the kernel to be deleted.
101#
102# Note that the default router file is ignored if we received routes
103# from a DHCP server.  Our policy is to always trust DHCP over local
104# administration.
105#
106smf_netstrategy
107
108if [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
109    [ -n "`/sbin/dhcpinfo Router`" ]; then
110	defrouters=`/sbin/dhcpinfo Router`
111elif [ -f /etc/defaultrouter ]; then
112	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
113	    /usr/bin/awk '{print $1}'`
114	if [ -n "$defrouters" ]; then
115		#
116		# We want the default router(s) listed in
117		# /etc/defaultrouter to replace the one added from the
118		# BOOTPARAMS WHOAMI response but we must avoid flushing
119		# the last route between the running system and its
120		# /usr file system.
121		#
122
123		# First, remember the original route.
124		shift $#
125		set -- `/usr/bin/netstat -rn -f inet | \
126		    /usr/bin/grep '^default'`
127		route_IP="$2"
128
129		#
130		# Next, add those from /etc/defaultrouter.  While doing
131		# this, if one of the routes we add is for the route
132		# previously added as a result of the BOOTPARAMS
133		# response, we will see a message of the form:
134		#       "add net default: gateway a.b.c.d: entry exists"
135		#
136		do_delete=yes
137		for router in $defrouters; do
138			set -- `/usr/sbin/route -n add default \
139			    -gateway $router`
140			[ $? -ne 0 -a "x$5" = "x$route_IP:" ] \
141			    && do_delete=no
142		done
143
144		#
145		# Finally, delete the original default route unless it
146		# was also listed in the defaultrouter file.
147		#
148		if [ -n "$route_IP" -a $do_delete = yes ]; then
149			/usr/sbin/route -n delete default \
150			    -gateway $route_IP >/dev/null
151		fi
152	else
153		/usr/sbin/route -fn > /dev/null
154	fi
155else
156	defrouters=
157fi
158
159#
160# Use routeadm(1M) to configure forwarding and launch routing daemons
161# for IPv4 and IPv6 based on preset values.  These settings only apply
162# to the global zone.  For IPv4 dynamic routing, the system will default
163# to disabled if a default route was previously added via BOOTP, DHCP,
164# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
165#
166if [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
167	#
168	# No default routes were setup by "route" command above.
169	# Check the kernel routing table for any other default
170	# routes.
171	#
172	/usr/bin/netstat -rn -f inet | \
173	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
174fi
175
176#
177# The routeadm/ipv4-routing-set property is true if the administrator
178# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
179# appropriate defaults.  We no longer run "routeadm -u" on every boot
180# however, as persistent daemon state is now controlled by SMF.
181#
182ipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
183if [ -z "$defrouters" ]; then
184	#
185	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
186	# has not yet been run by the administrator, we apply this default.
187	# The -b option is project-private and informs routeadm not
188	# to treat the enable as administrator-driven.
189	#
190	/usr/sbin/svccfg -s $SMF_FMRI \
191	    setprop routeadm/default-ipv4-routing = true
192	if [ "$ipv4_routing_set" = "false" ]; then
193		/sbin/routeadm -b -e ipv4-routing -u
194	fi
195else
196	#
197	# Default router(s) have been found,  so ipv4-routing default value
198	# should be disabled.  If routaedm -e/d has not yet been run by
199	# the administrator, we apply this default.  The -b option is
200	# project-private and informs routeadm not to treat the disable as
201	# administrator-driven.
202	#
203	/usr/sbin/svccfg -s $SMF_FMRI \
204	    setprop routeadm/default-ipv4-routing = false
205	if [ "$ipv4_routing_set" = "false" ]; then
206		/sbin/routeadm -b -d ipv4-routing -u
207	fi
208fi
209
210#
211# Set 6to4 Relay Router communication support policy and, if applicable,
212# the destination Relay Router IPv4 address.  See /etc/default/inetinit for
213# setting and further info on ACCEPT6TO4RELAY and RELAY6TO4ADDR.
214# If ACCEPT6TO4RELAY=NO, the default value in the kernel will
215# be used.
216#
217ACCEPT6TO4RELAY=`echo "$ACCEPT6TO4RELAY" | /usr/bin/tr '[A-Z]' '[a-z]'`
218if [ "$ACCEPT6TO4RELAY" = yes ]; then
219	if [ "$RELAY6TO4ADDR" ]; then
220		/usr/sbin/6to4relay -e -a $RELAY6TO4ADDR
221	else
222		/usr/sbin/6to4relay -e
223	fi
224fi
225
226#
227# Read /etc/inet/static_routes and add each route.
228#
229if [ -f /etc/inet/static_routes ]; then
230	echo "Adding persistent routes:"
231	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
232		/usr/sbin/route add $line
233	done
234fi
235
236# Clear exit status.
237exit $SMF_EXIT_OK
238