xref: /onnv-gate/usr/src/cmd/svc/milestone/net-routing-setup (revision 4216:db25a92a63bb)
13048Samaguire#!/sbin/sh
23048Samaguire#
33048Samaguire# CDDL HEADER START
43048Samaguire#
53048Samaguire# The contents of this file are subject to the terms of the
63048Samaguire# Common Development and Distribution License (the "License").
73048Samaguire# You may not use this file except in compliance with the License.
83048Samaguire#
93048Samaguire# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
103048Samaguire# or http://www.opensolaris.org/os/licensing.
113048Samaguire# See the License for the specific language governing permissions
123048Samaguire# and limitations under the License.
133048Samaguire#
143048Samaguire# When distributing Covered Code, include this CDDL HEADER in each
153048Samaguire# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
163048Samaguire# If applicable, add the following below this CDDL HEADER, with the
173048Samaguire# fields enclosed by brackets "[]" replaced with your own identifying
183048Samaguire# information: Portions Copyright [yyyy] [name of copyright owner]
193048Samaguire#
203048Samaguire# CDDL HEADER END
213048Samaguire#
223048Samaguire#
233448Sdh155122# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243048Samaguire# Use is subject to license terms.
253048Samaguire#
263048Samaguire# ident	"%Z%%M%	%I%	%E% SMI"
273048Samaguire
283048Samaguire# This script configures IP routing.
293048Samaguire
303048Samaguire. /lib/svc/share/smf_include.sh
314121Samaguire. /lib/svc/share/net_include.sh
323048Samaguire
333048Samaguire#
343448Sdh155122# In a shared-IP zone we need this service to be up, but all of the work
353048Samaguire# it tries to do is irrelevant (and will actually lead to the service
363048Samaguire# failing if we try to do it), so just bail out.
373448Sdh155122# In the global zone and exclusive-IP zones we proceed.
383048Samaguire#
393448Sdh155122smf_configure_ip || exit $SMF_EXIT_OK
403048Samaguire
413048Samaguire#
423048Samaguire# If routing.conf file is in place, and has not already been read in
433094Samaguire# by previous invokation of routeadm, legacy configuration is upgraded
443094Samaguire# by this call to "routeadm -u".  This call is also needed when
453048Samaguire# a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
463094Samaguire# which need to be applied.  Finally, routeadm starts in.ndpd by
473094Samaguire# enabling the ndp service (in.ndpd), which is required for IPv6 address
483094Samaguire# autoconfiguration. It would be nice if we could do this in
493048Samaguire# network/loopback, but since the SMF backend is read-only at that
503048Samaguire# point in boot, we cannot.
513048Samaguire#
523094Samaguire/sbin/routeadm -u
533048Samaguire
543048Samaguire#
553048Samaguire# Are we routing dynamically? routeadm(1M) reports this in the
563048Samaguire# "current" values of ipv4/6-routing - if either are true, we are running
573048Samaguire# routing daemons (or at least they are enabled to run).
583048Samaguire#
593048Samaguiredynamic_routing_test=`/sbin/routeadm -p | \
603048Samaguirenawk '/^ipv[46]-routing [.]*/ { print $2 }'  | /usr/bin/grep "current=enabled"`
613048Samaguireif [ -n "$dynamic_routing_test" ]; then
623048Samaguire	dynamic_routing="true"
633048Samaguirefi
643048Samaguire
654121Samaguire#
664121Samaguire# Add a static route for IPv6 multicast packets.
674121Samaguire#
684121Samaguireupdate_v6_multicast_route
693048Samaguire
703048Samaguire#
713048Samaguire# Configure default IPv4 routers using the local "/etc/defaultrouter"
723048Samaguire# configuration file.  The file can contain the hostnames or IP
733048Samaguire# addresses of one or more default routers.  If hostnames are used,
743048Samaguire# each hostname must also be listed in the local "/etc/hosts" file
753048Samaguire# because NIS and NIS+ are not running at the time that this script is
763048Samaguire# run.  Each router name or address is listed on a single line by
773048Samaguire# itself in the file.  Anything else on that line after the router's
783048Samaguire# name or address is ignored.  Lines that begin with "#" are
793048Samaguire# considered comments and ignored.
803048Samaguire#
813048Samaguire# The default routes listed in the "/etc/defaultrouter" file will
823048Samaguire# replace those added by the kernel during diskless booting.  An
833048Samaguire# empty "/etc/defaultrouter" file will cause the default route
843048Samaguire# added by the kernel to be deleted.
853048Samaguire#
863048Samaguire# Note that the default router file is ignored if we received routes
873048Samaguire# from a DHCP server.  Our policy is to always trust DHCP over local
883048Samaguire# administration.
893048Samaguire#
903048Samaguiresmf_netstrategy
913048Samaguire
923048Samaguireif [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
933048Samaguire    [ -n "`/sbin/dhcpinfo Router`" ]; then
943048Samaguire	defrouters=`/sbin/dhcpinfo Router`
953048Samaguireelif [ -f /etc/defaultrouter ]; then
963048Samaguire	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
973048Samaguire	    /usr/bin/awk '{print $1}'`
983048Samaguire	if [ -n "$defrouters" ]; then
993048Samaguire		#
1003048Samaguire		# We want the default router(s) listed in
1013048Samaguire		# /etc/defaultrouter to replace the one added from the
1023048Samaguire		# BOOTPARAMS WHOAMI response but we must avoid flushing
1033048Samaguire		# the last route between the running system and its
1043048Samaguire		# /usr file system.
1053048Samaguire		#
1063048Samaguire
1073048Samaguire		# First, remember the original route.
1083048Samaguire		shift $#
1093048Samaguire		set -- `/usr/bin/netstat -rn -f inet | \
1103048Samaguire		    /usr/bin/grep '^default'`
1113048Samaguire		route_IP="$2"
1123048Samaguire
1133048Samaguire		#
1143048Samaguire		# Next, add those from /etc/defaultrouter.  While doing
1153048Samaguire		# this, if one of the routes we add is for the route
1163048Samaguire		# previously added as a result of the BOOTPARAMS
1173048Samaguire		# response, we will see a message of the form:
1183048Samaguire		#       "add net default: gateway a.b.c.d: entry exists"
1193048Samaguire		#
1203048Samaguire		do_delete=yes
1213048Samaguire		for router in $defrouters; do
122*4216Samaguire			route_added=`/usr/sbin/route -n add default \
1233048Samaguire			    -gateway $router`
124*4216Samaguire			res=$?
125*4216Samaguire			set -- $route_added
126*4216Samaguire			[ $res -ne 0 -a "$5" = "$route_IP:" ] && do_delete=no
1273048Samaguire		done
1283048Samaguire
1293048Samaguire		#
1303048Samaguire		# Finally, delete the original default route unless it
1313048Samaguire		# was also listed in the defaultrouter file.
1323048Samaguire		#
1333048Samaguire		if [ -n "$route_IP" -a $do_delete = yes ]; then
1343048Samaguire			/usr/sbin/route -n delete default \
1353048Samaguire			    -gateway $route_IP >/dev/null
1363048Samaguire		fi
1373048Samaguire	else
1383048Samaguire		/usr/sbin/route -fn > /dev/null
1393048Samaguire	fi
1403048Samaguireelse
1413048Samaguire	defrouters=
1423048Samaguirefi
1433048Samaguire
1443048Samaguire#
1453048Samaguire# Use routeadm(1M) to configure forwarding and launch routing daemons
1463048Samaguire# for IPv4 and IPv6 based on preset values.  These settings only apply
1473048Samaguire# to the global zone.  For IPv4 dynamic routing, the system will default
1483048Samaguire# to disabled if a default route was previously added via BOOTP, DHCP,
1493048Samaguire# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
1503048Samaguire#
1513048Samaguireif [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
1523048Samaguire	#
1533048Samaguire	# No default routes were setup by "route" command above.
1543048Samaguire	# Check the kernel routing table for any other default
1553048Samaguire	# routes.
1563048Samaguire	#
1573048Samaguire	/usr/bin/netstat -rn -f inet | \
1583048Samaguire	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
1593048Samaguirefi
1603048Samaguire
1613048Samaguire#
1623048Samaguire# The routeadm/ipv4-routing-set property is true if the administrator
1633048Samaguire# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
1643048Samaguire# appropriate defaults.  We no longer run "routeadm -u" on every boot
1653048Samaguire# however, as persistent daemon state is now controlled by SMF.
1663048Samaguire#
1673048Samaguireipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
1683048Samaguireif [ -z "$defrouters" ]; then
1693048Samaguire	#
1703048Samaguire	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
1713048Samaguire	# has not yet been run by the administrator, we apply this default.
1723294Samaguire	# The -b option is project-private and informs routeadm not
1733294Samaguire	# to treat the enable as administrator-driven.
1743048Samaguire	#
1753048Samaguire	/usr/sbin/svccfg -s $SMF_FMRI \
1763048Samaguire	    setprop routeadm/default-ipv4-routing = true
1773048Samaguire	if [ "$ipv4_routing_set" = "false" ]; then
1783294Samaguire		/sbin/routeadm -b -e ipv4-routing -u
1793048Samaguire	fi
1803048Samaguireelse
1813048Samaguire	#
1823048Samaguire	# Default router(s) have been found,  so ipv4-routing default value
1833048Samaguire	# should be disabled.  If routaedm -e/d has not yet been run by
1843294Samaguire	# the administrator, we apply this default.  The -b option is
1853294Samaguire	# project-private and informs routeadm not to treat the disable as
1863294Samaguire	# administrator-driven.
1873294Samaguire	#
1883048Samaguire	/usr/sbin/svccfg -s $SMF_FMRI \
1893048Samaguire	    setprop routeadm/default-ipv4-routing = false
1903048Samaguire	if [ "$ipv4_routing_set" = "false" ]; then
1913294Samaguire		/sbin/routeadm -b -d ipv4-routing -u
1923048Samaguire	fi
1933048Samaguirefi
1943048Samaguire
1953048Samaguire#
1963048Samaguire# Set 6to4 Relay Router communication support policy and, if applicable,
1973048Samaguire# the destination Relay Router IPv4 address.  See /etc/default/inetinit for
1983048Samaguire# setting and further info on ACCEPT6TO4RELAY and RELAY6TO4ADDR.
1993048Samaguire# If ACCEPT6TO4RELAY=NO, the default value in the kernel will
2003048Samaguire# be used.
2013048Samaguire#
2023048SamaguireACCEPT6TO4RELAY=`echo "$ACCEPT6TO4RELAY" | /usr/bin/tr '[A-Z]' '[a-z]'`
2033048Samaguireif [ "$ACCEPT6TO4RELAY" = yes ]; then
2043048Samaguire	if [ "$RELAY6TO4ADDR" ]; then
2053048Samaguire		/usr/sbin/6to4relay -e -a $RELAY6TO4ADDR
2063048Samaguire	else
2073048Samaguire		/usr/sbin/6to4relay -e
2083048Samaguire	fi
2093048Samaguirefi
2103048Samaguire
2113048Samaguire#
2123048Samaguire# Read /etc/inet/static_routes and add each route.
2133048Samaguire#
2143048Samaguireif [ -f /etc/inet/static_routes ]; then
2153048Samaguire	echo "Adding persistent routes:"
2163048Samaguire	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
2173048Samaguire		/usr/sbin/route add $line
2183048Samaguire	done
2193048Samaguirefi
2203048Samaguire
2213048Samaguire# Clear exit status.
2223048Samaguireexit $SMF_EXIT_OK
223