xref: /illumos-gate/usr/src/cmd/svc/milestone/net-routing-setup (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
1a192e900Samaguire#!/sbin/sh
2a192e900Samaguire#
3a192e900Samaguire# CDDL HEADER START
4a192e900Samaguire#
5a192e900Samaguire# The contents of this file are subject to the terms of the
6a192e900Samaguire# Common Development and Distribution License (the "License").
7a192e900Samaguire# You may not use this file except in compliance with the License.
8a192e900Samaguire#
9a192e900Samaguire# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10a192e900Samaguire# or http://www.opensolaris.org/os/licensing.
11a192e900Samaguire# See the License for the specific language governing permissions
12a192e900Samaguire# and limitations under the License.
13a192e900Samaguire#
14a192e900Samaguire# When distributing Covered Code, include this CDDL HEADER in each
15a192e900Samaguire# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16a192e900Samaguire# If applicable, add the following below this CDDL HEADER, with the
17a192e900Samaguire# fields enclosed by brackets "[]" replaced with your own identifying
18a192e900Samaguire# information: Portions Copyright [yyyy] [name of copyright owner]
19a192e900Samaguire#
20a192e900Samaguire# CDDL HEADER END
21a192e900Samaguire#
22a192e900Samaguire#
239b5bf10aSMark Haywood# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
2410c0779dSWilliam Welliver# Copyright (c) 2021 H. William Welliver
25a192e900Samaguire
26a192e900Samaguire# This script configures IP routing.
27a192e900Samaguire
28a192e900Samaguire. /lib/svc/share/smf_include.sh
29a192e900Samaguire
30a192e900Samaguire#
31f4b3ec61Sdh155122# In a shared-IP zone we need this service to be up, but all of the work
32a192e900Samaguire# it tries to do is irrelevant (and will actually lead to the service
33a192e900Samaguire# failing if we try to do it), so just bail out.
34f4b3ec61Sdh155122# In the global zone and exclusive-IP zones we proceed.
35a192e900Samaguire#
36f4b3ec61Sdh155122smf_configure_ip || exit $SMF_EXIT_OK
37a192e900Samaguire
38a192e900Samaguire#
39a192e900Samaguire# If routing.conf file is in place, and has not already been read in
40ceb97a6aSamaguire# by previous invokation of routeadm, legacy configuration is upgraded
41ceb97a6aSamaguire# by this call to "routeadm -u".  This call is also needed when
42a192e900Samaguire# a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
43ceb97a6aSamaguire# which need to be applied.  Finally, routeadm starts in.ndpd by
44ceb97a6aSamaguire# enabling the ndp service (in.ndpd), which is required for IPv6 address
45ceb97a6aSamaguire# autoconfiguration. It would be nice if we could do this in
46a192e900Samaguire# network/loopback, but since the SMF backend is read-only at that
47a192e900Samaguire# point in boot, we cannot.
48a192e900Samaguire#
49a192e900Samaguire/sbin/routeadm -u
50a192e900Samaguire
51a192e900Samaguire#
52*bbf21555SRichard Lowe# Are we routing dynamically? routeadm(8) reports this in the
53a192e900Samaguire# "current" values of ipv4/6-routing - if either are true, we are running
54a192e900Samaguire# routing daemons (or at least they are enabled to run).
55a192e900Samaguire#
56a192e900Samaguiredynamic_routing_test=`/sbin/routeadm -p | \
57a192e900Samaguirenawk '/^ipv[46]-routing [.]*/ { print $2 }'  | /usr/bin/grep "current=enabled"`
58a192e900Samaguireif [ -n "$dynamic_routing_test" ]; then
59a192e900Samaguire	dynamic_routing="true"
60a192e900Samaguirefi
61a192e900Samaguire
62a192e900Samaguire#
63a192e900Samaguire# Configure default IPv4 routers using the local "/etc/defaultrouter"
64a192e900Samaguire# configuration file.  The file can contain the hostnames or IP
65a192e900Samaguire# addresses of one or more default routers.  If hostnames are used,
66a192e900Samaguire# each hostname must also be listed in the local "/etc/hosts" file
6736e852a1SRaja Andra# because NIS is not running at the time that this script is
68a192e900Samaguire# run.  Each router name or address is listed on a single line by
69a192e900Samaguire# itself in the file.  Anything else on that line after the router's
70a192e900Samaguire# name or address is ignored.  Lines that begin with "#" are
71a192e900Samaguire# considered comments and ignored.
72a192e900Samaguire#
73a192e900Samaguire# The default routes listed in the "/etc/defaultrouter" file will
74a192e900Samaguire# replace those added by the kernel during diskless booting.  An
75a192e900Samaguire# empty "/etc/defaultrouter" file will cause the default route
76a192e900Samaguire# added by the kernel to be deleted.
77a192e900Samaguire#
78a192e900Samaguire# Note that the default router file is ignored if we received routes
79a192e900Samaguire# from a DHCP server.  Our policy is to always trust DHCP over local
80a192e900Samaguire# administration.
81a192e900Samaguire#
82a192e900Samaguiresmf_netstrategy
83a192e900Samaguire
8410c0779dSWilliam Welliver#
8510c0779dSWilliam Welliver# See if static routes were created by install. If so, they were created
8610c0779dSWilliam Welliver# under /etc/svc/volatile. Copy them into their proper place.
8710c0779dSWilliam Welliver#
8810c0779dSWilliam Welliverif [ -f /etc/svc/volatile/etc/inet/static_routes ]; then
8910c0779dSWilliam Welliver	echo "Installing persistent routes"
9010c0779dSWilliam Welliver	if [ -f /etc/inet/static_routes ]; then
9110c0779dSWilliam Welliver		cat /etc/svc/volatile/etc/inet/static_routes | grep -v '^#' \
9210c0779dSWilliam Welliver		    >> /etc/inet/static_routes
9310c0779dSWilliam Welliver	else
9410c0779dSWilliam Welliver		cp /etc/svc/volatile/etc/inet/static_routes \
9510c0779dSWilliam Welliver		    /etc/inet/static_routes
9610c0779dSWilliam Welliver	fi
9710c0779dSWilliam Welliver	/usr/bin/rm /etc/svc/volatile/etc/inet/static_routes
9810c0779dSWilliam Welliver
9910c0779dSWilliam Welliverfi
10010c0779dSWilliam Welliver
10110c0779dSWilliam Welliver#
10210c0779dSWilliam Welliver# Read /etc/inet/static_routes and add each link-local route.
10310c0779dSWilliam Welliver#
10410c0779dSWilliam Welliverif [ -f /etc/inet/static_routes ]; then
10510c0779dSWilliam Welliver	echo "Adding persistent link-local routes:"
10610c0779dSWilliam Welliver	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes \
10710c0779dSWilliam Welliver	    | /usr/bin/grep -E -- "-interface |-iface " | while read line; do
10810c0779dSWilliam Welliver		/usr/sbin/route add $line
10910c0779dSWilliam Welliver	done
11010c0779dSWilliam Welliverfi
11110c0779dSWilliam Welliver
112a192e900Samaguireif [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
113a192e900Samaguire    [ -n "`/sbin/dhcpinfo Router`" ]; then
114a192e900Samaguire	defrouters=`/sbin/dhcpinfo Router`
115a192e900Samaguireelif [ -f /etc/defaultrouter ]; then
116a192e900Samaguire	defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
117a192e900Samaguire	    /usr/bin/awk '{print $1}'`
118a192e900Samaguire	if [ -n "$defrouters" ]; then
119a192e900Samaguire		#
120a192e900Samaguire		# We want the default router(s) listed in
121a192e900Samaguire		# /etc/defaultrouter to replace the one added from the
122a192e900Samaguire		# BOOTPARAMS WHOAMI response but we must avoid flushing
123a192e900Samaguire		# the last route between the running system and its
124a192e900Samaguire		# /usr file system.
125a192e900Samaguire		#
126a192e900Samaguire
127a192e900Samaguire		# First, remember the original route.
128a192e900Samaguire		shift $#
129a192e900Samaguire		set -- `/usr/bin/netstat -rn -f inet | \
130a192e900Samaguire		    /usr/bin/grep '^default'`
131a192e900Samaguire		route_IP="$2"
132a192e900Samaguire
133a192e900Samaguire		#
134a192e900Samaguire		# Next, add those from /etc/defaultrouter.  While doing
135a192e900Samaguire		# this, if one of the routes we add is for the route
136a192e900Samaguire		# previously added as a result of the BOOTPARAMS
137a192e900Samaguire		# response, we will see a message of the form:
138a192e900Samaguire		#       "add net default: gateway a.b.c.d: entry exists"
139a192e900Samaguire		#
140a192e900Samaguire		do_delete=yes
141a192e900Samaguire		for router in $defrouters; do
142e7ad388aSamaguire			route_added=`/usr/sbin/route -n add default \
143a192e900Samaguire			    -gateway $router`
144e7ad388aSamaguire			res=$?
145e7ad388aSamaguire			set -- $route_added
146e7ad388aSamaguire			[ $res -ne 0 -a "$5" = "$route_IP:" ] && do_delete=no
147a192e900Samaguire		done
148a192e900Samaguire
149a192e900Samaguire		#
150a192e900Samaguire		# Finally, delete the original default route unless it
151a192e900Samaguire		# was also listed in the defaultrouter file.
152a192e900Samaguire		#
153a192e900Samaguire		if [ -n "$route_IP" -a $do_delete = yes ]; then
154a192e900Samaguire			/usr/sbin/route -n delete default \
155a192e900Samaguire			    -gateway $route_IP >/dev/null
156a192e900Samaguire		fi
157a192e900Samaguire	else
158a192e900Samaguire		/usr/sbin/route -fn > /dev/null
159a192e900Samaguire	fi
160a192e900Samaguireelse
161a192e900Samaguire	defrouters=
162a192e900Samaguirefi
163a192e900Samaguire
164a192e900Samaguire#
165*bbf21555SRichard Lowe# Use routeadm(8) to configure forwarding and launch routing daemons
166a192e900Samaguire# for IPv4 and IPv6 based on preset values.  These settings only apply
167a192e900Samaguire# to the global zone.  For IPv4 dynamic routing, the system will default
168a192e900Samaguire# to disabled if a default route was previously added via BOOTP, DHCP,
169a192e900Samaguire# or the /etc/defaultrouter file.  routeadm also starts in.ndpd.
170a192e900Samaguire#
171a192e900Samaguireif [ "$dynamic_routing" != "true"  ] && [ -z "$defrouters" ]; then
172a192e900Samaguire	#
173a192e900Samaguire	# No default routes were setup by "route" command above.
174a192e900Samaguire	# Check the kernel routing table for any other default
175a192e900Samaguire	# routes.
176a192e900Samaguire	#
177a192e900Samaguire	/usr/bin/netstat -rn -f inet | \
178a192e900Samaguire	    /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
179a192e900Samaguirefi
180a192e900Samaguire
181a192e900Samaguire#
182a192e900Samaguire# The routeadm/ipv4-routing-set property is true if the administrator
183a192e900Samaguire# has run "routeadm -e/-d ipv4-routing".  If not, we revert to the
184a192e900Samaguire# appropriate defaults.  We no longer run "routeadm -u" on every boot
185a192e900Samaguire# however, as persistent daemon state is now controlled by SMF.
186a192e900Samaguire#
187a192e900Samaguireipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
188a192e900Samaguireif [ -z "$defrouters" ]; then
189a192e900Samaguire	#
190a192e900Samaguire	# Set default value for ipv4-routing to enabled.  If routeadm -e/-d
191a192e900Samaguire	# has not yet been run by the administrator, we apply this default.
192ef2c19a1Samaguire	# The -b option is project-private and informs routeadm not
193ef2c19a1Samaguire	# to treat the enable as administrator-driven.
194a192e900Samaguire	#
195a192e900Samaguire	/usr/sbin/svccfg -s $SMF_FMRI \
196a192e900Samaguire	    setprop routeadm/default-ipv4-routing = true
197a192e900Samaguire	if [ "$ipv4_routing_set" = "false" ]; then
198ef2c19a1Samaguire		/sbin/routeadm -b -e ipv4-routing -u
199a192e900Samaguire	fi
200a192e900Samaguireelse
201a192e900Samaguire	#
202a192e900Samaguire	# Default router(s) have been found,  so ipv4-routing default value
203a192e900Samaguire	# should be disabled.  If routaedm -e/d has not yet been run by
204ef2c19a1Samaguire	# the administrator, we apply this default.  The -b option is
205ef2c19a1Samaguire	# project-private and informs routeadm not to treat the disable as
206ef2c19a1Samaguire	# administrator-driven.
207ef2c19a1Samaguire	#
208a192e900Samaguire	/usr/sbin/svccfg -s $SMF_FMRI \
209a192e900Samaguire	    setprop routeadm/default-ipv4-routing = false
210a192e900Samaguire	if [ "$ipv4_routing_set" = "false" ]; then
211ef2c19a1Samaguire		/sbin/routeadm -b -d ipv4-routing -u
212a192e900Samaguire	fi
213a192e900Samaguirefi
214a192e900Samaguire
215a192e900Samaguire#
21610c0779dSWilliam Welliver# Read /etc/inet/static_routes and add each non-link-local route.
217a192e900Samaguire#
218a192e900Samaguireif [ -f /etc/inet/static_routes ]; then
219a192e900Samaguire	echo "Adding persistent routes:"
22010c0779dSWilliam Welliver	/usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes \
22110c0779dSWilliam Welliver	    | /usr/bin/grep -v -E -- "-interface |-iface " | while read line; do
222a192e900Samaguire		/usr/sbin/route add $line
223a192e900Samaguire	done
224a192e900Samaguirefi
225a192e900Samaguire
226a192e900Samaguire# Clear exit status.
227a192e900Samaguireexit $SMF_EXIT_OK
228