xref: /illumos-gate/usr/src/cmd/svc/milestone/net-routing-setup (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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			route_added=`/usr/sbin/route -n add default \
123			    -gateway $router`
124			res=$?
125			set -- $route_added
126			[ $res -ne 0 -a "$5" = "$route_IP:" ] && do_delete=no
127		done
128
129		#
130		# Finally, delete the original default route unless it
131		# was also listed in the defaultrouter file.
132		#
133		if [ -n "$route_IP" -a $do_delete = yes ]; then
134			/usr/sbin/route -n delete default \
135			    -gateway $route_IP >/dev/null
136		fi
137	else
138		/usr/sbin/route -fn > /dev/null
139	fi
140else
141	defrouters=
142fi
143
144#
145# Use routeadm(1M) to configure forwarding and launch routing daemons
146# for IPv4 and IPv6 based on preset values.  These settings only apply
147# to the global zone.  For IPv4 dynamic routing, the system will default
148# to disabled if a default route was previously added via BOOTP, DHCP,
149# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
150#
151if [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
152	#
153	# No default routes were setup by "route" command above.
154	# Check the kernel routing table for any other default
155	# routes.
156	#
157	/usr/bin/netstat -rn -f inet | \
158	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
159fi
160
161#
162# The routeadm/ipv4-routing-set property is true if the administrator
163# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
164# appropriate defaults.  We no longer run "routeadm -u" on every boot
165# however, as persistent daemon state is now controlled by SMF.
166#
167ipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
168if [ -z "$defrouters" ]; then
169	#
170	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
171	# has not yet been run by the administrator, we apply this default.
172	# The -b option is project-private and informs routeadm not
173	# to treat the enable as administrator-driven.
174	#
175	/usr/sbin/svccfg -s $SMF_FMRI \
176	    setprop routeadm/default-ipv4-routing = true
177	if [ "$ipv4_routing_set" = "false" ]; then
178		/sbin/routeadm -b -e ipv4-routing -u
179	fi
180else
181	#
182	# Default router(s) have been found,  so ipv4-routing default value
183	# should be disabled.  If routaedm -e/d has not yet been run by
184	# the administrator, we apply this default.  The -b option is
185	# project-private and informs routeadm not to treat the disable as
186	# administrator-driven.
187	#
188	/usr/sbin/svccfg -s $SMF_FMRI \
189	    setprop routeadm/default-ipv4-routing = false
190	if [ "$ipv4_routing_set" = "false" ]; then
191		/sbin/routeadm -b -d ipv4-routing -u
192	fi
193fi
194
195#
196# Set 6to4 Relay Router communication support policy and, if applicable,
197# the destination Relay Router IPv4 address.  See /etc/default/inetinit for
198# setting and further info on ACCEPT6TO4RELAY and RELAY6TO4ADDR.
199# If ACCEPT6TO4RELAY=NO, the default value in the kernel will
200# be used.
201#
202ACCEPT6TO4RELAY=`echo "$ACCEPT6TO4RELAY" | /usr/bin/tr '[A-Z]' '[a-z]'`
203if [ "$ACCEPT6TO4RELAY" = yes ]; then
204	if [ "$RELAY6TO4ADDR" ]; then
205		/usr/sbin/6to4relay -e -a $RELAY6TO4ADDR
206	else
207		/usr/sbin/6to4relay -e
208	fi
209fi
210
211#
212# Read /etc/inet/static_routes and add each route.
213#
214if [ -f /etc/inet/static_routes ]; then
215	echo "Adding persistent routes:"
216	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
217		/usr/sbin/route add $line
218	done
219fi
220
221# Clear exit status.
222exit $SMF_EXIT_OK
223