xref: /illumos-gate/usr/src/cmd/svc/milestone/net-routing-setup (revision dd72704bd9e794056c558153663c739e2012d721)
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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24# Copyright (c) 2021 H. William Welliver
25
26# This script configures IP routing.
27
28. /lib/svc/share/smf_include.sh
29
30#
31# In a shared-IP zone we need this service to be up, but all of the work
32# it tries to do is irrelevant (and will actually lead to the service
33# failing if we try to do it), so just bail out.
34# In the global zone and exclusive-IP zones we proceed.
35#
36smf_configure_ip || exit $SMF_EXIT_OK
37
38#
39# If routing.conf file is in place, and has not already been read in
40# by previous invokation of routeadm, legacy configuration is upgraded
41# by this call to "routeadm -u".  This call is also needed when
42# a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
43# which need to be applied.  Finally, routeadm starts in.ndpd by
44# enabling the ndp service (in.ndpd), which is required for IPv6 address
45# autoconfiguration. It would be nice if we could do this in
46# network/loopback, but since the SMF backend is read-only at that
47# point in boot, we cannot.
48#
49/sbin/routeadm -u
50
51#
52# Are we routing dynamically? routeadm(8) reports this in the
53# "current" values of ipv4/6-routing - if either are true, we are running
54# routing daemons (or at least they are enabled to run).
55#
56dynamic_routing_test=`/sbin/routeadm -p | \
57nawk '/^ipv[46]-routing [.]*/ { print $2 }'  | /usr/bin/grep "current=enabled"`
58if [ -n "$dynamic_routing_test" ]; then
59	dynamic_routing="true"
60fi
61
62#
63# Configure default IPv4 routers using the local "/etc/defaultrouter"
64# configuration file.  The file can contain the hostnames or IP
65# addresses of one or more default routers.  If hostnames are used,
66# each hostname must also be listed in the local "/etc/hosts" file
67# because NIS is not running at the time that this script is
68# run.  Each router name or address is listed on a single line by
69# itself in the file.  Anything else on that line after the router's
70# name or address is ignored.  Lines that begin with "#" are
71# considered comments and ignored.
72#
73# The default routes listed in the "/etc/defaultrouter" file will
74# replace those added by the kernel during diskless booting.  An
75# empty "/etc/defaultrouter" file will cause the default route
76# added by the kernel to be deleted.
77#
78# Note that the default router file is ignored if we received routes
79# from a DHCP server.  Our policy is to always trust DHCP over local
80# administration.
81#
82smf_netstrategy
83
84#
85# See if static routes were created by install. If so, they were created
86# under /etc/svc/volatile. Copy them into their proper place.
87#
88if [ -f /etc/svc/volatile/etc/inet/static_routes ]; then
89	echo "Installing persistent routes"
90	if [ -f /etc/inet/static_routes ]; then
91		cat /etc/svc/volatile/etc/inet/static_routes | grep -v '^#' \
92		    >> /etc/inet/static_routes
93	else
94		cp /etc/svc/volatile/etc/inet/static_routes \
95		    /etc/inet/static_routes
96	fi
97	/usr/bin/rm /etc/svc/volatile/etc/inet/static_routes
98
99fi
100
101#
102# Read /etc/inet/static_routes and add each link-local route.
103#
104if [ -f /etc/inet/static_routes ]; then
105	echo "Adding persistent link-local routes:"
106	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes \
107	    | /usr/bin/grep -E -- "-interface |-iface " | while read line; do
108		/usr/sbin/route add $line
109	done
110fi
111
112if [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
113    [ -n "`/sbin/dhcpinfo Router`" ]; then
114	defrouters=`/sbin/dhcpinfo Router`
115elif [ -f /etc/defaultrouter ]; then
116	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
117	    /usr/bin/awk '{print $1}'`
118	if [ -n "$defrouters" ]; then
119		#
120		# We want the default router(s) listed in
121		# /etc/defaultrouter to replace the one added from the
122		# BOOTPARAMS WHOAMI response but we must avoid flushing
123		# the last route between the running system and its
124		# /usr file system.
125		#
126
127		# First, remember the original route.
128		shift $#
129		set -- `/usr/bin/netstat -rn -f inet | \
130		    /usr/bin/grep '^default'`
131		route_IP="$2"
132
133		#
134		# Next, add those from /etc/defaultrouter.  While doing
135		# this, if one of the routes we add is for the route
136		# previously added as a result of the BOOTPARAMS
137		# response, we will see a message of the form:
138		#       "add net default: gateway a.b.c.d: entry exists"
139		#
140		do_delete=yes
141		for router in $defrouters; do
142			route_added=`/usr/sbin/route -n add default \
143			    -gateway $router`
144			res=$?
145			set -- $route_added
146			[ $res -ne 0 -a "$5" = "$route_IP:" ] && 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(8) 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	# The -b option is project-private and informs routeadm not
193	# to treat the enable as administrator-driven.
194	#
195	/usr/sbin/svccfg -s $SMF_FMRI \
196	    setprop routeadm/default-ipv4-routing = true
197	if [ "$ipv4_routing_set" = "false" ]; then
198		/sbin/routeadm -b -e ipv4-routing -u
199	fi
200else
201	#
202	# Default router(s) have been found,  so ipv4-routing default value
203	# should be disabled.  If routaedm -e/d has not yet been run by
204	# the administrator, we apply this default.  The -b option is
205	# project-private and informs routeadm not to treat the disable as
206	# administrator-driven.
207	#
208	/usr/sbin/svccfg -s $SMF_FMRI \
209	    setprop routeadm/default-ipv4-routing = false
210	if [ "$ipv4_routing_set" = "false" ]; then
211		/sbin/routeadm -b -d ipv4-routing -u
212	fi
213fi
214
215#
216# Read /etc/inet/static_routes and add each non-link-local route.
217#
218if [ -f /etc/inet/static_routes ]; then
219	echo "Adding persistent routes:"
220	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes \
221	    | /usr/bin/grep -v -E -- "-interface |-iface " | while read line; do
222		/usr/sbin/route add $line
223	done
224fi
225
226# Clear exit status.
227exit $SMF_EXIT_OK
228