xref: /illumos-gate/usr/src/cmd/svc/milestone/net-routing-setup (revision a192e900f6d2b0e1a822e3252c0dfd795ed49d76)
1*a192e900Samaguire#!/sbin/sh
2*a192e900Samaguire#
3*a192e900Samaguire# CDDL HEADER START
4*a192e900Samaguire#
5*a192e900Samaguire# The contents of this file are subject to the terms of the
6*a192e900Samaguire# Common Development and Distribution License (the "License").
7*a192e900Samaguire# You may not use this file except in compliance with the License.
8*a192e900Samaguire#
9*a192e900Samaguire# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*a192e900Samaguire# or http://www.opensolaris.org/os/licensing.
11*a192e900Samaguire# See the License for the specific language governing permissions
12*a192e900Samaguire# and limitations under the License.
13*a192e900Samaguire#
14*a192e900Samaguire# When distributing Covered Code, include this CDDL HEADER in each
15*a192e900Samaguire# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*a192e900Samaguire# If applicable, add the following below this CDDL HEADER, with the
17*a192e900Samaguire# fields enclosed by brackets "[]" replaced with your own identifying
18*a192e900Samaguire# information: Portions Copyright [yyyy] [name of copyright owner]
19*a192e900Samaguire#
20*a192e900Samaguire# CDDL HEADER END
21*a192e900Samaguire#
22*a192e900Samaguire#
23*a192e900Samaguire# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*a192e900Samaguire# Use is subject to license terms.
25*a192e900Samaguire#
26*a192e900Samaguire# ident	"%Z%%M%	%I%	%E% SMI"
27*a192e900Samaguire
28*a192e900Samaguire# This script configures IP routing.
29*a192e900Samaguire
30*a192e900Samaguire. /lib/svc/share/smf_include.sh
31*a192e900Samaguire
32*a192e900Samaguire#
33*a192e900Samaguire# In a zone we need this service to be up, but all of the work
34*a192e900Samaguire# it tries to do is irrelevant (and will actually lead to the service
35*a192e900Samaguire# failing if we try to do it), so just bail out.
36*a192e900Samaguire#
37*a192e900Samaguiresmf_is_globalzone || exit $SMF_EXIT_OK
38*a192e900Samaguire
39*a192e900Samaguire#
40*a192e900Samaguire# If routing.conf file is in place, and has not already been read in
41*a192e900Samaguire# by previous invokation of routeadm, we run routeadm -u here to get
42*a192e900Samaguire# and apply legacy configuration.  We also run "routeadm -u" when
43*a192e900Samaguire# a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
44*a192e900Samaguire# which need to be applied. It would be nice if we could do this in
45*a192e900Samaguire# network/loopback, but since the SMF backend is read-only at that
46*a192e900Samaguire# point in boot, we cannot.
47*a192e900Samaguire#
48*a192e900Samaguire
49*a192e900Samaguireupgrade_routing_conf=""
50*a192e900Samaguirerouting_conf_read=`/usr/bin/svcprop -p routeadm/routing-conf-read $SMF_FMRI`
51*a192e900Samaguireif [ -f /etc/inet/routing.conf -a "$routing_conf_read" = "false" ]; then
52*a192e900Samaguire        upgrade_routing_conf="true"
53*a192e900Samaguirefi
54*a192e900Samaguireif [ "$upgrade_routing_conf" = "true" -o -f /var/svc/profile/upgrade ]; then
55*a192e900Samaguire        /sbin/routeadm -u
56*a192e900Samaguirefi
57*a192e900Samaguire
58*a192e900Samaguire#
59*a192e900Samaguire# Are we routing dynamically? routeadm(1M) reports this in the
60*a192e900Samaguire# "current" values of ipv4/6-routing - if either are true, we are running
61*a192e900Samaguire# routing daemons (or at least they are enabled to run).
62*a192e900Samaguire#
63*a192e900Samaguiredynamic_routing_test=`/sbin/routeadm -p | \
64*a192e900Samaguirenawk '/^ipv[46]-routing [.]*/ { print $2 }'  | /usr/bin/grep "current=enabled"`
65*a192e900Samaguireif [ -n "$dynamic_routing_test" ]; then
66*a192e900Samaguire	dynamic_routing="true"
67*a192e900Samaguirefi
68*a192e900Samaguire
69*a192e900Samaguire/usr/sbin/ifconfig -a6u >/etc/svc/volatile/ifconfig.$$
70*a192e900Samaguirenumv6ifs=`/usr/bin/grep -c inet6 /etc/svc/volatile/ifconfig.$$`
71*a192e900Samaguireif  [ $numv6ifs -gt 1 ]; then
72*a192e900Samaguire	#
73*a192e900Samaguire	# Add a static route for multicast packets out of a link-local
74*a192e900Samaguire	# interface, although would like to specify multicast interface using
75*a192e900Samaguire	# an interface name!
76*a192e900Samaguire	#
77*a192e900Samaguire	set -- `/usr/bin/awk '
78*a192e900Samaguire		/inet6 fe80:/ {
79*a192e900Samaguire			print substr($2, 1, index($2, "/") - 1)
80*a192e900Samaguire		}' /etc/svc/volatile/ifconfig.$$`
81*a192e900Samaguire
82*a192e900Samaguire	if [ -n "$1" ]; then
83*a192e900Samaguire		echo "Setting default IPv6 interface for multicast:" \
84*a192e900Samaguire			"add net ff00::/8: gateway $1"
85*a192e900Samaguire		/usr/sbin/route -n add -interface -inet6 "ff00::/8" "$1" \
86*a192e900Samaguire			>/dev/null
87*a192e900Samaguire	fi
88*a192e900Samaguirefi
89*a192e900Samaguire/usr/bin/rm -f /etc/svc/volatile/ifconfig.$$
90*a192e900Samaguire
91*a192e900Samaguire#
92*a192e900Samaguire# Configure default IPv4 routers using the local "/etc/defaultrouter"
93*a192e900Samaguire# configuration file.  The file can contain the hostnames or IP
94*a192e900Samaguire# addresses of one or more default routers.  If hostnames are used,
95*a192e900Samaguire# each hostname must also be listed in the local "/etc/hosts" file
96*a192e900Samaguire# because NIS and NIS+ are not running at the time that this script is
97*a192e900Samaguire# run.  Each router name or address is listed on a single line by
98*a192e900Samaguire# itself in the file.  Anything else on that line after the router's
99*a192e900Samaguire# name or address is ignored.  Lines that begin with "#" are
100*a192e900Samaguire# considered comments and ignored.
101*a192e900Samaguire#
102*a192e900Samaguire# The default routes listed in the "/etc/defaultrouter" file will
103*a192e900Samaguire# replace those added by the kernel during diskless booting.  An
104*a192e900Samaguire# empty "/etc/defaultrouter" file will cause the default route
105*a192e900Samaguire# added by the kernel to be deleted.
106*a192e900Samaguire#
107*a192e900Samaguire# Note that the default router file is ignored if we received routes
108*a192e900Samaguire# from a DHCP server.  Our policy is to always trust DHCP over local
109*a192e900Samaguire# administration.
110*a192e900Samaguire#
111*a192e900Samaguiresmf_netstrategy
112*a192e900Samaguire
113*a192e900Samaguireif [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
114*a192e900Samaguire    [ -n "`/sbin/dhcpinfo Router`" ]; then
115*a192e900Samaguire	defrouters=`/sbin/dhcpinfo Router`
116*a192e900Samaguireelif [ -f /etc/defaultrouter ]; then
117*a192e900Samaguire	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
118*a192e900Samaguire	    /usr/bin/awk '{print $1}'`
119*a192e900Samaguire	if [ -n "$defrouters" ]; then
120*a192e900Samaguire		#
121*a192e900Samaguire		# We want the default router(s) listed in
122*a192e900Samaguire		# /etc/defaultrouter to replace the one added from the
123*a192e900Samaguire		# BOOTPARAMS WHOAMI response but we must avoid flushing
124*a192e900Samaguire		# the last route between the running system and its
125*a192e900Samaguire		# /usr file system.
126*a192e900Samaguire		#
127*a192e900Samaguire
128*a192e900Samaguire		# First, remember the original route.
129*a192e900Samaguire		shift $#
130*a192e900Samaguire		set -- `/usr/bin/netstat -rn -f inet | \
131*a192e900Samaguire		    /usr/bin/grep '^default'`
132*a192e900Samaguire		route_IP="$2"
133*a192e900Samaguire
134*a192e900Samaguire		#
135*a192e900Samaguire		# Next, add those from /etc/defaultrouter.  While doing
136*a192e900Samaguire		# this, if one of the routes we add is for the route
137*a192e900Samaguire		# previously added as a result of the BOOTPARAMS
138*a192e900Samaguire		# response, we will see a message of the form:
139*a192e900Samaguire		#       "add net default: gateway a.b.c.d: entry exists"
140*a192e900Samaguire		#
141*a192e900Samaguire		do_delete=yes
142*a192e900Samaguire		for router in $defrouters; do
143*a192e900Samaguire			set -- `/usr/sbin/route -n add default \
144*a192e900Samaguire			    -gateway $router`
145*a192e900Samaguire			[ $? -ne 0 -a "x$5" = "x$route_IP:" ] \
146*a192e900Samaguire			    && do_delete=no
147*a192e900Samaguire		done
148*a192e900Samaguire
149*a192e900Samaguire		#
150*a192e900Samaguire		# Finally, delete the original default route unless it
151*a192e900Samaguire		# was also listed in the defaultrouter file.
152*a192e900Samaguire		#
153*a192e900Samaguire		if [ -n "$route_IP" -a $do_delete = yes ]; then
154*a192e900Samaguire			/usr/sbin/route -n delete default \
155*a192e900Samaguire			    -gateway $route_IP >/dev/null
156*a192e900Samaguire		fi
157*a192e900Samaguire	else
158*a192e900Samaguire		/usr/sbin/route -fn > /dev/null
159*a192e900Samaguire	fi
160*a192e900Samaguireelse
161*a192e900Samaguire	defrouters=
162*a192e900Samaguirefi
163*a192e900Samaguire
164*a192e900Samaguire#
165*a192e900Samaguire# Use routeadm(1M) to configure forwarding and launch routing daemons
166*a192e900Samaguire# for IPv4 and IPv6 based on preset values.  These settings only apply
167*a192e900Samaguire# to the global zone.  For IPv4 dynamic routing, the system will default
168*a192e900Samaguire# to disabled if a default route was previously added via BOOTP, DHCP,
169*a192e900Samaguire# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
170*a192e900Samaguire#
171*a192e900Samaguireif [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
172*a192e900Samaguire	#
173*a192e900Samaguire	# No default routes were setup by "route" command above.
174*a192e900Samaguire	# Check the kernel routing table for any other default
175*a192e900Samaguire	# routes.
176*a192e900Samaguire	#
177*a192e900Samaguire	/usr/bin/netstat -rn -f inet | \
178*a192e900Samaguire	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
179*a192e900Samaguirefi
180*a192e900Samaguire
181*a192e900Samaguire#
182*a192e900Samaguire# The routeadm/ipv4-routing-set property is true if the administrator
183*a192e900Samaguire# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
184*a192e900Samaguire# appropriate defaults.  We no longer run "routeadm -u" on every boot
185*a192e900Samaguire# however, as persistent daemon state is now controlled by SMF.
186*a192e900Samaguire#
187*a192e900Samaguireipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
188*a192e900Samaguireif [ -z "$defrouters" ]; then
189*a192e900Samaguire	#
190*a192e900Samaguire	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
191*a192e900Samaguire	# has not yet been run by the administrator, we apply this default.
192*a192e900Samaguire	#
193*a192e900Samaguire	/usr/sbin/svccfg -s $SMF_FMRI \
194*a192e900Samaguire	    setprop routeadm/default-ipv4-routing = true
195*a192e900Samaguire	if [ "$ipv4_routing_set" = "false" ]; then
196*a192e900Samaguire		/sbin/routeadm -e ipv4-routing -u
197*a192e900Samaguire	fi
198*a192e900Samaguireelse
199*a192e900Samaguire	#
200*a192e900Samaguire	# Default router(s) have been found,  so ipv4-routing default value
201*a192e900Samaguire	# should be disabled.  If routaedm -e/d has not yet been run by
202*a192e900Samaguire	# the administrator, we apply this default.
203*a192e900Samaguire	/usr/sbin/svccfg -s $SMF_FMRI \
204*a192e900Samaguire	    setprop routeadm/default-ipv4-routing = false
205*a192e900Samaguire	if [ "$ipv4_routing_set" = "false" ]; then
206*a192e900Samaguire		/sbin/routeadm -d ipv4-routing -u
207*a192e900Samaguire	fi
208*a192e900Samaguirefi
209*a192e900Samaguire
210*a192e900Samaguire#
211*a192e900Samaguire# Set 6to4 Relay Router communication support policy and, if applicable,
212*a192e900Samaguire# the destination Relay Router IPv4 address.  See /etc/default/inetinit for
213*a192e900Samaguire# setting and further info on ACCEPT6TO4RELAY and RELAY6TO4ADDR.
214*a192e900Samaguire# If ACCEPT6TO4RELAY=NO, the default value in the kernel will
215*a192e900Samaguire# be used.
216*a192e900Samaguire#
217*a192e900SamaguireACCEPT6TO4RELAY=`echo "$ACCEPT6TO4RELAY" | /usr/bin/tr '[A-Z]' '[a-z]'`
218*a192e900Samaguireif [ "$ACCEPT6TO4RELAY" = yes ]; then
219*a192e900Samaguire	if [ "$RELAY6TO4ADDR" ]; then
220*a192e900Samaguire		/usr/sbin/6to4relay -e -a $RELAY6TO4ADDR
221*a192e900Samaguire	else
222*a192e900Samaguire		/usr/sbin/6to4relay -e
223*a192e900Samaguire	fi
224*a192e900Samaguirefi
225*a192e900Samaguire
226*a192e900Samaguire#
227*a192e900Samaguire# Read /etc/inet/static_routes and add each route.
228*a192e900Samaguire#
229*a192e900Samaguireif [ -f /etc/inet/static_routes ]; then
230*a192e900Samaguire	echo "Adding persistent routes:"
231*a192e900Samaguire	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
232*a192e900Samaguire		/usr/sbin/route add $line
233*a192e900Samaguire	done
234*a192e900Samaguirefi
235*a192e900Samaguire
236*a192e900Samaguire# Clear exit status.
237*a192e900Samaguireexit $SMF_EXIT_OK
238