xref: /titanic_51/usr/src/cmd/svc/milestone/net-routing-setup (revision 48bc00d6814e04ff3edb32cafe7d1bc580baff68)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25
26# This script configures IP routing.
27
28. /lib/svc/share/smf_include.sh
29
30#
31# In a shared-IP zone we need this service to be up, but all of the work
32# it tries to do is irrelevant (and will actually lead to the service
33# failing if we try to do it), so just bail out.
34# In the global zone and exclusive-IP zones we proceed.
35#
36smf_configure_ip || exit $SMF_EXIT_OK
37
38#
39# If routing.conf file is in place, and has not already been read in
40# by previous invokation of routeadm, legacy configuration is upgraded
41# by this call to "routeadm -u".  This call is also needed when
42# a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
43# which need to be applied.  Finally, routeadm starts in.ndpd by
44# enabling the ndp service (in.ndpd), which is required for IPv6 address
45# autoconfiguration. It would be nice if we could do this in
46# network/loopback, but since the SMF backend is read-only at that
47# point in boot, we cannot.
48#
49/sbin/routeadm -u
50
51#
52# Are we routing dynamically? routeadm(1M) reports this in the
53# "current" values of ipv4/6-routing - if either are true, we are running
54# routing daemons (or at least they are enabled to run).
55#
56dynamic_routing_test=`/sbin/routeadm -p | \
57nawk '/^ipv[46]-routing [.]*/ { print $2 }'  | /usr/bin/grep "current=enabled"`
58if [ -n "$dynamic_routing_test" ]; then
59	dynamic_routing="true"
60fi
61
62#
63# Configure default IPv4 routers using the local "/etc/defaultrouter"
64# configuration file.  The file can contain the hostnames or IP
65# addresses of one or more default routers.  If hostnames are used,
66# each hostname must also be listed in the local "/etc/hosts" file
67# because NIS and NIS+ are not running at the time that this script is
68# run.  Each router name or address is listed on a single line by
69# itself in the file.  Anything else on that line after the router's
70# name or address is ignored.  Lines that begin with "#" are
71# considered comments and ignored.
72#
73# The default routes listed in the "/etc/defaultrouter" file will
74# replace those added by the kernel during diskless booting.  An
75# empty "/etc/defaultrouter" file will cause the default route
76# added by the kernel to be deleted.
77#
78# Note that the default router file is ignored if we received routes
79# from a DHCP server.  Our policy is to always trust DHCP over local
80# administration.
81#
82smf_netstrategy
83
84if [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
85    [ -n "`/sbin/dhcpinfo Router`" ]; then
86	defrouters=`/sbin/dhcpinfo Router`
87elif [ -f /etc/defaultrouter ]; then
88	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
89	    /usr/bin/awk '{print $1}'`
90	if [ -n "$defrouters" ]; then
91		#
92		# We want the default router(s) listed in
93		# /etc/defaultrouter to replace the one added from the
94		# BOOTPARAMS WHOAMI response but we must avoid flushing
95		# the last route between the running system and its
96		# /usr file system.
97		#
98
99		# First, remember the original route.
100		shift $#
101		set -- `/usr/bin/netstat -rn -f inet | \
102		    /usr/bin/grep '^default'`
103		route_IP="$2"
104
105		#
106		# Next, add those from /etc/defaultrouter.  While doing
107		# this, if one of the routes we add is for the route
108		# previously added as a result of the BOOTPARAMS
109		# response, we will see a message of the form:
110		#       "add net default: gateway a.b.c.d: entry exists"
111		#
112		do_delete=yes
113		for router in $defrouters; do
114			route_added=`/usr/sbin/route -n add default \
115			    -gateway $router`
116			res=$?
117			set -- $route_added
118			[ $res -ne 0 -a "$5" = "$route_IP:" ] && do_delete=no
119		done
120
121		#
122		# Finally, delete the original default route unless it
123		# was also listed in the defaultrouter file.
124		#
125		if [ -n "$route_IP" -a $do_delete = yes ]; then
126			/usr/sbin/route -n delete default \
127			    -gateway $route_IP >/dev/null
128		fi
129	else
130		/usr/sbin/route -fn > /dev/null
131	fi
132else
133	defrouters=
134fi
135
136#
137# Use routeadm(1M) to configure forwarding and launch routing daemons
138# for IPv4 and IPv6 based on preset values.  These settings only apply
139# to the global zone.  For IPv4 dynamic routing, the system will default
140# to disabled if a default route was previously added via BOOTP, DHCP,
141# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
142#
143if [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
144	#
145	# No default routes were setup by "route" command above.
146	# Check the kernel routing table for any other default
147	# routes.
148	#
149	/usr/bin/netstat -rn -f inet | \
150	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
151fi
152
153#
154# The routeadm/ipv4-routing-set property is true if the administrator
155# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
156# appropriate defaults.  We no longer run "routeadm -u" on every boot
157# however, as persistent daemon state is now controlled by SMF.
158#
159ipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
160if [ -z "$defrouters" ]; then
161	#
162	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
163	# has not yet been run by the administrator, we apply this default.
164	# The -b option is project-private and informs routeadm not
165	# to treat the enable as administrator-driven.
166	#
167	/usr/sbin/svccfg -s $SMF_FMRI \
168	    setprop routeadm/default-ipv4-routing = true
169	if [ "$ipv4_routing_set" = "false" ]; then
170		/sbin/routeadm -b -e ipv4-routing -u
171	fi
172else
173	#
174	# Default router(s) have been found,  so ipv4-routing default value
175	# should be disabled.  If routaedm -e/d has not yet been run by
176	# the administrator, we apply this default.  The -b option is
177	# project-private and informs routeadm not to treat the disable as
178	# administrator-driven.
179	#
180	/usr/sbin/svccfg -s $SMF_FMRI \
181	    setprop routeadm/default-ipv4-routing = false
182	if [ "$ipv4_routing_set" = "false" ]; then
183		/sbin/routeadm -b -d ipv4-routing -u
184	fi
185fi
186
187#
188# Read /etc/inet/static_routes and add each route.
189#
190if [ -f /etc/inet/static_routes ]; then
191	echo "Adding persistent routes:"
192	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
193		/usr/sbin/route add $line
194	done
195fi
196
197# Clear exit status.
198exit $SMF_EXIT_OK
199