xref: /illumos-gate/usr/src/cmd/svc/milestone/net-routing-setup (revision 150d2c5288c645a1c1a7d2bee61199a3729406c7)
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/usr/sbin/ifconfig -a6u >/etc/svc/volatile/ifconfig.$$
65numv6ifs=`/usr/bin/grep -c inet6 /etc/svc/volatile/ifconfig.$$`
66
67if  [ $numv6ifs -gt 1 ]; then
68	#
69	# Add a static route for multicast packets out of a link-local
70	# interface, although would like to specify multicast interface using
71	# an interface name!
72	#
73	set -- `/usr/bin/awk '
74		/inet6 fe80:/ {
75			print substr($2, 1, index($2, "/") - 1)
76		}' /etc/svc/volatile/ifconfig.$$`
77
78	if [ -n "$1" ]; then
79		echo "Setting default IPv6 interface for multicast:" \
80			"add net ff00::/8: gateway $1"
81		/usr/sbin/route -n add -interface -inet6 "ff00::/8" "$1" \
82			>/dev/null
83	fi
84fi
85/usr/bin/rm -f /etc/svc/volatile/ifconfig.$$
86
87#
88# Configure default IPv4 routers using the local "/etc/defaultrouter"
89# configuration file.  The file can contain the hostnames or IP
90# addresses of one or more default routers.  If hostnames are used,
91# each hostname must also be listed in the local "/etc/hosts" file
92# because NIS and NIS+ are not running at the time that this script is
93# run.  Each router name or address is listed on a single line by
94# itself in the file.  Anything else on that line after the router's
95# name or address is ignored.  Lines that begin with "#" are
96# considered comments and ignored.
97#
98# The default routes listed in the "/etc/defaultrouter" file will
99# replace those added by the kernel during diskless booting.  An
100# empty "/etc/defaultrouter" file will cause the default route
101# added by the kernel to be deleted.
102#
103# Note that the default router file is ignored if we received routes
104# from a DHCP server.  Our policy is to always trust DHCP over local
105# administration.
106#
107smf_netstrategy
108
109if [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
110    [ -n "`/sbin/dhcpinfo Router`" ]; then
111	defrouters=`/sbin/dhcpinfo Router`
112elif [ -f /etc/defaultrouter ]; then
113	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
114	    /usr/bin/awk '{print $1}'`
115	if [ -n "$defrouters" ]; then
116		#
117		# We want the default router(s) listed in
118		# /etc/defaultrouter to replace the one added from the
119		# BOOTPARAMS WHOAMI response but we must avoid flushing
120		# the last route between the running system and its
121		# /usr file system.
122		#
123
124		# First, remember the original route.
125		shift $#
126		set -- `/usr/bin/netstat -rn -f inet | \
127		    /usr/bin/grep '^default'`
128		route_IP="$2"
129
130		#
131		# Next, add those from /etc/defaultrouter.  While doing
132		# this, if one of the routes we add is for the route
133		# previously added as a result of the BOOTPARAMS
134		# response, we will see a message of the form:
135		#       "add net default: gateway a.b.c.d: entry exists"
136		#
137		do_delete=yes
138		for router in $defrouters; do
139			set -- `/usr/sbin/route -n add default \
140			    -gateway $router`
141			[ $? -ne 0 -a "x$5" = "x$route_IP:" ] \
142			    && do_delete=no
143		done
144
145		#
146		# Finally, delete the original default route unless it
147		# was also listed in the defaultrouter file.
148		#
149		if [ -n "$route_IP" -a $do_delete = yes ]; then
150			/usr/sbin/route -n delete default \
151			    -gateway $route_IP >/dev/null
152		fi
153	else
154		/usr/sbin/route -fn > /dev/null
155	fi
156else
157	defrouters=
158fi
159
160#
161# Use routeadm(1M) to configure forwarding and launch routing daemons
162# for IPv4 and IPv6 based on preset values.  These settings only apply
163# to the global zone.  For IPv4 dynamic routing, the system will default
164# to disabled if a default route was previously added via BOOTP, DHCP,
165# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
166#
167if [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
168	#
169	# No default routes were setup by "route" command above.
170	# Check the kernel routing table for any other default
171	# routes.
172	#
173	/usr/bin/netstat -rn -f inet | \
174	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
175fi
176
177#
178# The routeadm/ipv4-routing-set property is true if the administrator
179# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
180# appropriate defaults.  We no longer run "routeadm -u" on every boot
181# however, as persistent daemon state is now controlled by SMF.
182#
183ipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
184if [ -z "$defrouters" ]; then
185	#
186	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
187	# has not yet been run by the administrator, we apply this default.
188	# The -b option is project-private and informs routeadm not
189	# to treat the enable as administrator-driven.
190	#
191	/usr/sbin/svccfg -s $SMF_FMRI \
192	    setprop routeadm/default-ipv4-routing = true
193	if [ "$ipv4_routing_set" = "false" ]; then
194		/sbin/routeadm -b -e ipv4-routing -u
195	fi
196else
197	#
198	# Default router(s) have been found,  so ipv4-routing default value
199	# should be disabled.  If routaedm -e/d has not yet been run by
200	# the administrator, we apply this default.  The -b option is
201	# project-private and informs routeadm not to treat the disable as
202	# administrator-driven.
203	#
204	/usr/sbin/svccfg -s $SMF_FMRI \
205	    setprop routeadm/default-ipv4-routing = false
206	if [ "$ipv4_routing_set" = "false" ]; then
207		/sbin/routeadm -b -d ipv4-routing -u
208	fi
209fi
210
211#
212# Set 6to4 Relay Router communication support policy and, if applicable,
213# the destination Relay Router IPv4 address.  See /etc/default/inetinit for
214# setting and further info on ACCEPT6TO4RELAY and RELAY6TO4ADDR.
215# If ACCEPT6TO4RELAY=NO, the default value in the kernel will
216# be used.
217#
218ACCEPT6TO4RELAY=`echo "$ACCEPT6TO4RELAY" | /usr/bin/tr '[A-Z]' '[a-z]'`
219if [ "$ACCEPT6TO4RELAY" = yes ]; then
220	if [ "$RELAY6TO4ADDR" ]; then
221		/usr/sbin/6to4relay -e -a $RELAY6TO4ADDR
222	else
223		/usr/sbin/6to4relay -e
224	fi
225fi
226
227#
228# Read /etc/inet/static_routes and add each route.
229#
230if [ -f /etc/inet/static_routes ]; then
231	echo "Adding persistent routes:"
232	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
233		/usr/sbin/route add $line
234	done
235fi
236
237# Clear exit status.
238exit $SMF_EXIT_OK
239