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