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