xref: /illumos-gate/usr/src/cmd/svc/milestone/net-loopback (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# Copyright 2024 Oxide Computer Company
27#
28
29. /lib/svc/share/smf_include.sh
30
31#
32# In a shared-IP zone we need this service to be up, but all of the work
33# it tries to do is irrelevant (and will actually lead to the service
34# failing if we try to do it), so just bail out.
35# In the global zone and exclusive-IP zones we proceed.
36#
37smf_configure_ip || exit $SMF_EXIT_OK
38
39rc=$SMF_EXIT_OK
40
41degrade() {
42	echo "$@" >&2
43	rc=$SMF_EXIT_MON_DEGRADE
44}
45
46if [ -f /etc/hostname.lo0 ] || [ -f /etc/hostname6.lo0 ]; then
47	echo "found /etc/hostname.lo0 or /etc/hostname6.lo0; "\
48		    "using ifconfig to create lo0" > /dev/msglog
49	# IPv4 loopback
50	if ! /sbin/ifconfig lo0 >/dev/null 2>&1; then
51		/sbin/ifconfig lo0 plumb 127.0.0.1 up || \
52		    degrade "Failed to create IPv4 loopback"
53	fi
54
55	# IPv6 loopback
56	if ! /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
57		/sbin/ifconfig lo0 inet6 plumb ::1 up || \
58		    degrade "Failed to create IPv6 loopback"
59	fi
60
61	# Trusted Extensions shares the loopback interface with all zones
62	if (smf_is_system_labeled); then
63		if smf_is_globalzone; then
64			/sbin/ifconfig lo0 all-zones
65			/sbin/ifconfig lo0 inet6 all-zones
66		fi
67	fi
68else
69	state=`/sbin/ipadm show-if -p -o state lo0 2>/dev/null`
70	if [ $? -eq 0 -a "$state" = "disabled" ]; then
71		/sbin/ipadm enable-if -t lo0 || \
72		    degrade "Failed to enable loopback interface"
73	else
74		# IPv4 loopback
75		state=`/sbin/ipadm show-addr -po state lo0/v4`
76		if [ $? -ne 0 -o -z "$state" ]; then
77			/sbin/ipadm create-addr -t \
78			    -T static -a 127.0.0.1/8 lo0/v4 || \
79			    degrade "Failed to create IPv4 loopback"
80		fi
81
82		# IPv6 loopback
83		state=`/sbin/ipadm show-addr -po state lo0/v6`
84		if [ $? -ne 0 -o -z "$state" ]; then
85			/sbin/ipadm create-addr -t \
86			    -T static -a ::1/128 lo0/v6 || \
87			    degrade "Failed to create IPv6 loopback"
88		fi
89	fi
90
91	# Trusted Extensions shares the loopback interface with all zones
92	if (smf_is_system_labeled); then
93		if smf_is_globalzone; then
94			/sbin/ipadm set-addrprop -t -p zone=all-zones lo0/v4
95			/sbin/ipadm set-addrprop -t -p zone=all-zones lo0/v6
96		fi
97	fi
98fi
99
100exit $rc
101