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